# -*- coding: utf-8 -*-
"""
Client module for Apple Push Notification service.

The algorithm used to send bulk push notifications is optimized to eagerly
check for errors using a single thread. Error checking is performed after each
batch send (bulk notifications may be broken up into multiple batches) and is
non-blocking until the last notification is sent. A final, blocking error check
is performed using a customizable error timeout. This style of error checking
is done to ensure that no errors are missed (e.g. by waiting too long to check
errors before the connection is closed by the APNS server) without having to
use two threads to read and write.

The return from a send operation will contain a response object that includes
any errors encountered while sending. These errors will be associated with the
failed tokens.

For more details regarding Apple's APNS documentation, consult the following:

- `Apple Push Notification Service <http://goo.gl/wFVr2S>`_
- `Provider Communication with APNS <http://goo.gl/qMfByr>`_
"""

from binascii import hexlify, unhexlify
from collections import namedtuple
import logging
import select
import socket
import ssl
import struct
import time

from .utils import json_dumps, chunk, compact_dict
from .exceptions import (
    APNSError,
    APNSAuthError,
    APNSInvalidTokenError,
    APNSInvalidPayloadSizeError,
    APNSMissingPayloadError,
    APNSServerError,
    APNSTimeoutError,
    APNSUnsendableError,
    raise_apns_server_error,
)

__all__ = (
    "APNSClient",
    "APNSSandboxClient",
    "APNSResponse",
    "APNSExpiredToken",
)

log = logging.getLogger(__name__)

APNS_HOST = "gateway.push.apple.com"
APNS_SANDBOX_HOST = "gateway.sandbox.push.apple.com"
APNS_PORT = 2195
APNS_FEEDBACK_HOST = "feedback.push.apple.com"
APNS_FEEDBACK_SANDBOX_HOST = "feedback.sandbox.push.apple.com"
APNS_FEEDBACK_PORT = 2196

APNS_DEFAULT_EXPIRATION_OFFSET = 60 * 60 * 24 * 30  # 1 month
APNS_DEFAULT_BATCH_SIZE = 100
APNS_DEFAULT_ERROR_TIMEOUT = 10
APNS_DEFAULT_MAX_PAYLOAD_LENGTH = 0
APNS_DEFAULT_RETRIES = 5

# Constants derived from http://goo.gl/wFVr2S
APNS_PUSH_COMMAND = 2
APNS_PUSH_FRAME_ITEM_COUNT = 5
APNS_PUSH_FRAME_ITEM_PREFIX_LEN = 3
APNS_PUSH_IDENTIFIER_LEN = 4
APNS_PUSH_EXPIRATION_LEN = 4
APNS_PUSH_PRIORITY_LEN = 1

APNS_ERROR_RESPONSE_COMMAND = 8
APNS_ERROR_RESPONSE_LEN = 6
APNS_FEEDBACK_HEADER_LEN = 6
APNS_MAX_NOTIFICATION_SIZE = 2048

#: Indicates that the push message should be sent at a time that conserves
#: power on the device receiving it.
APNS_LOW_PRIORITY = 5

#: Indicates that the push message should be sent immediately. The remote
#: notification must trigger an alert, sound, or badge on the device. It is an
#: error to use this priority for a push that contains only the
#: ``content_available`` key.
APNS_HIGH_PRIORITY = 10

class APNSClient(object):
    """APNS client class."""

    host = APNS_HOST
    port = APNS_PORT
    feedback_host = APNS_FEEDBACK_HOST
    feedback_port = APNS_FEEDBACK_PORT

    def __init__(
        self,
        certificate,
        default_error_timeout=APNS_DEFAULT_ERROR_TIMEOUT,
        default_expiration_offset=APNS_DEFAULT_EXPIRATION_OFFSET,
        default_batch_size=APNS_DEFAULT_BATCH_SIZE,
        default_max_payload_length=APNS_DEFAULT_MAX_PAYLOAD_LENGTH,
        default_retries=APNS_DEFAULT_RETRIES,
    ):
        self.certificate = certificate
        self.default_error_timeout = default_error_timeout
        self.default_expiration_offset = default_expiration_offset
        self.default_batch_size = default_batch_size
        self.default_max_payload_length = default_max_payload_length
        self.default_retries = default_retries
        self._conn = None

    @property
    def conn(self):
        """Reference to lazy APNS connection."""
        if not self._conn:
            self._conn = self.create_connection()
        return self._conn

    def create_connection(self):
        """Create and return new APNS connection to push server."""
        return APNSConnection(self.host, self.port, self.certificate)

    def create_feedback_connection(self):
        """Create and return new APNS connection to feedback server."""
        return APNSConnection(self.feedback_host, self.feedback_port, self.certificate)

    def close(self):
        """Close APNS connection."""
        self.conn.close()

    def send(
        self,
        ids,
        message=None,
        expiration=None,
        low_priority=None,
        batch_size=None,
        error_timeout=None,
        max_payload_length=None,
        retries=None,
        **options
    ):
        """
        Send push notification to single or multiple recipients.

        Args:
            ids (list): APNS device tokens. Each item is expected to be a hex
                string.
            message (str|dict): Message string or APS dictionary. Set to
                ``None`` to send an empty alert notification.
            expiration (int, optional): Expiration time of message in seconds
                offset from now. Defaults to ``None`` which uses
                :attr:`default_expiration_offset`.
            low_priority (boolean, optional): Whether to send notification with
                the low priority flag. Defaults to ``False``.
            batch_size (int, optional): Number of notifications to group
                together when sending. Defaults to ``None`` which uses
                attr:`default_batch_size`.
            error_timeout (int, optional): Time in seconds to wait for the
                error response after sending messages. Defaults to ``None``
                which uses attr:`default_error_timeout`.
            max_payload_length (int, optional): The maximum length of the
                payload to send. Message will be trimmed if the size is
                exceeded. Use 0 to turn off. Defaults to ``None`` which uses
                attr:`default_max_payload_length`.
            retries (int, optional): Number of times to retry when the send
                operation fails. Defaults to ``None`` which uses
                :attr:`default_retries`.

        Keyword Args:
            badge (int, optional): Badge number count for alert. Defaults to
                ``None``.
            sound (str, optional): Name of the sound file to play for alert.
                Defaults to ``None``.
            category (str, optional): Name of category. Defaults to ``None``.
            content_available (bool, optional): If ``True``, indicate that new
                content is available. Defaults to ``None``.
            title (str, optional): Alert title.
            title_loc_key (str, optional): The key to a title string in the
                ``Localizable.strings`` file for the current localization.
            title_loc_args (list, optional): List of string values to appear in
                place of the format specifiers in `title_loc_key`.
            action_loc_key (str, optional): Display an alert that includes the
                ``Close`` and ``View`` buttons. The string is used as a key to
                get a localized string in the current localization to use for
                the right button’s title instead of ``"View"``.
            loc_key (str, optional): A key to an alert-message string in a
                ``Localizable.strings`` file for the current localization.
            loc_args (list, optional): List of string values to appear in place
                of the format specifiers in ``loc_key``.
            launch_image (str, optional): The filename of an image file in the
                app bundle; it may include the extension or omit it.
            mutable_content (bool, optional): if ``True``, triggers Apple
                Notification Service Extension. Defaults to ``None``.
            thread_id (str, optional): Identifier for grouping notifications.
                iOS groups notifications with the same thread identifier
                together in Notification Center. Defaults to ``None``.
            extra (dict, optional): Extra data to include with the alert.

        Returns:
            :class:`APNSResponse`: Response from APNS containing tokens sent
                and any errors encountered.

        Raises:
            APNSInvalidTokenError: Invalid token format.
                :class:`.APNSInvalidTokenError`
            APNSInvalidPayloadSizeError: Notification payload size too large.
                :class:`.APNSInvalidPayloadSizeError`
            APNSMissingPayloadError: Notificationpayload is empty.
                :class:`.APNSMissingPayloadError`

        .. versionadded:: 0.0.1

        .. versionchanged:: 0.4.0
            - Added support for bulk sending.
            - Made sending and error checking non-blocking.
            - Removed `sock`, `payload`, and `identifer` arguments.

        .. versionchanged:: 0.5.0
            - Added ``batch_size`` argument.
            - Added ``error_timeout`` argument.
            - Replaced ``priority`` argument with ``low_priority=False``.
            - Resume sending notifications when a sent token has an error
              response.
            - Raise ``APNSSendError`` if any tokens
              have an error response.

        .. versionchanged:: 1.0.0
            - Return :class:`APNSResponse` instead of raising
              ``APNSSendError``.
            - Raise :class:`.APNSMissingPayloadError` if
              payload is empty.

        .. versionchanged:: 1.4.0
            Added ``retries`` argument.
        """
        if not isinstance(ids, (list, tuple)):
            ids = [ids]

        if max_payload_length is None:
            max_payload_length = self.default_max_payload_length

        message = APNSMessage(message, max_payload_length=max_payload_length, **options)

        validate_tokens(ids)
        validate_message(message)

        if low_priority:
            priority = APNS_LOW_PRIORITY
        else:
            priority = APNS_HIGH_PRIORITY

        if expiration is None:
            expiration = int(time.time() + self.default_expiration_offset)

        if batch_size is None:
            batch_size = self.default_batch_size

        if error_timeout is None:
            error_timeout = self.default_error_timeout

        if retries is None:
            retries = self.default_retries

        stream = APNSMessageStream(ids, message, expiration, priority, batch_size)

        return self.conn.sendall(stream, error_timeout, retries=retries)

    def get_expired_tokens(self):
        """
        Return inactive device tokens that are no longer registered to receive
        notifications.

        Returns:
            list: List of :class:`APNSExpiredToken` instances.

        .. versionadded:: 0.0.1
        """
        log.debug("Preparing to check for expired APNS tokens.")

        conn = self.create_feedback_connection()
        tokens = list(APNSFeedbackStream(conn))
        conn.close()

        log.debug("Received {0} expired APNS tokens.".format(len(tokens)))

        return tokens

class APNSSandboxClient(APNSClient):
    """APNS client class for sandbox server."""

    host = APNS_SANDBOX_HOST
    feedback_host = APNS_FEEDBACK_SANDBOX_HOST

class APNSConnection(object):
    """Manager for APNS socket connection."""

    def __init__(self, host, port, certificate):
        self.host = host
        self.port = port
        self.certificate = certificate
        self.sock = None

    def connect(self):
        """
        Lazily connect to APNS server.

        Re-establish connection if previously closed.
        """
        if self.sock:
            return

        log.debug(
            "Establishing connection to APNS on {0}:{1} using "
            "certificate at {2}".format(self.host, self.port, self.certificate)
        )

        self.sock = create_socket(self.host, self.port, self.certificate)

        log.debug(
            "Established connection to APNS on {0}:{1}.".format(self.host, self.port)
        )

    def close(self):
        """Disconnect from APNS server."""
        if self.sock:
            log.debug("Closing connection to APNS.")
            self.sock.close()
        self.sock = None

    @property
    def client(self):
        """Return client socket connection to APNS server."""
        self.connect()
        return self.sock

    def writable(self, timeout):
        """Return whether connection is writable."""
        try:
            return select.select([], [self.client], [], timeout)[1]
        except Exception:  # pragma: no cover
            log.debug("Error while waiting for APNS socket to become " "writable.")
            self.close()
            raise

    def readable(self, timeout):
        """Return whether connection is readable."""
        try:
            return select.select([self.client], [], [], timeout)[0]
        except Exception:  # pragma: no cover
            log.debug("Error while waiting for APNS socket to become " "readable.")
            self.close()
            raise

    def read(self, buffsize, timeout=10):
        """Return read data up to `buffsize`."""
        data = b""

        while True:
            if not self.readable(timeout):  # pragma: no cover
                self.close()
                raise socket.timeout

            chunk = self.client.read(buffsize - len(data))
            data += chunk

            if not chunk or len(data) >= buffsize or not timeout:
                # Either we've read all data or this is a nonblocking read.
                break

        return data

    def write(self, data, timeout=10):
        """Write data to socket."""
        if not self.writable(timeout):  # pragma: no cover
            self.close()
            raise socket.timeout

        log.debug(
            "Sending APNS notification batch containing {0} bytes.".format(len(data))
        )

        return self.client.sendall(data)

    def check_error(self, timeout=10):
        """Check for APNS errors."""
        if not self.readable(timeout):
            # No error response.
            return

        try:
            data = self.read(APNS_ERROR_RESPONSE_LEN, timeout=0)
        except socket.error as ex:  # pragma: no cover
            log.error("Could not read response: {0}.".format(ex))
            self.close()
            return

        if not data:  # pragma: no cover
            return

        command = struct.unpack(">B", data[:1])[0]

        if command != APNS_ERROR_RESPONSE_COMMAND:  # pragma: no cover
            self.close()
            return

        code, identifier = struct.unpack(">BI", data[1:])

        log.debug(
            "Received APNS error response with "
            "code={0} for identifier={1}.".format(code, identifier)
        )

        self.close()
        raise_apns_server_error(code, identifier)

    def send(self, frames, retries=APNS_DEFAULT_RETRIES):
        """Send stream of frames to APNS server."""
        if retries <= 0:  # pragma: no cover
            retries = 1

        current_identifier = frames.next_identifier

        for frame in frames:
            success = False
            last_ex = None

            while not success and retries:
                try:
                    self.write(frame)
                    success = True
                except socket.error as ex:
                    last_ex = ex
                    log.warning(
                        "Could not send frame to server: {0}. "
                        "Retrying send operation.".format(ex)
                    )
                    self.close()
                    retries -= 1

            if not success:
                log.error("Could not send frame to server: {0}.".format(last_ex))
                raise APNSTimeoutError(current_identifier)

            self.check_error(0)
            current_identifier = frames.next_identifier

    def sendall(
        self,
        stream,
        error_timeout=APNS_DEFAULT_ERROR_TIMEOUT,
        retries=APNS_DEFAULT_RETRIES,
    ):
        """
        Send all notifications while handling errors.

        If an error occurs, then resume sending starting from after the token that
        failed. If any tokens failed, raise an error after sending all tokens.
        """
        log.debug("Preparing to send {0} notifications to APNS.".format(len(stream)))

        errors = []

        while True:
            try:
                self.send(stream, retries=retries)

                # Perform the final error check here before exiting. A large
                # enough timeout should be used so that no errors are missed.
                self.check_error(error_timeout)
            except APNSServerError as ex:
                errors.append(ex)
                next_identifier = ex.identifier + 1
                stream.seek(next_identifier)

                if ex.fatal:
                    # We can't continue due to a fatal error. Go ahead and
                    # convert remaining notifications to errors.
                    errors += [
                        APNSUnsendableError(next_identifier + i)
                        for i, _ in enumerate(stream.peek())
                    ]
                    break

            if stream.eof():
                break

        log.debug("Sent {0} notifications to APNS.".format(len(stream)))

        if errors:
            log.debug(
                "Encountered {0} errors while sending to APNS.".format(len(errors))
            )

        return APNSResponse(stream.tokens, stream.message, errors)

class APNSMessage(object):
    """APNs message object that serializes to JSON."""

    def __init__(
        self,
        message=None,
        badge=None,
        sound=None,
        category=None,
        content_available=None,
        title=None,
        title_loc_key=None,
        title_loc_args=None,
        action_loc_key=None,
        loc_key=None,
        loc_args=None,
        launch_image=None,
        mutable_content=None,
        thread_id=None,
        extra=None,
        max_payload_length=None,
    ):
        self.message = message
        self.badge = badge
        self.sound = sound
        self.category = category
        self.content_available = content_available
        self.title = title
        self.title_loc_key = title_loc_key
        self.title_loc_args = title_loc_args
        self.action_loc_key = action_loc_key
        self.loc_key = loc_key
        self.loc_args = loc_args
        self.launch_image = launch_image
        self.mutable_content = mutable_content
        self.thread_id = thread_id
        self.extra = extra
        self.max_payload_length = max_payload_length

    def _construct_dict(self, message=None):
        """Return message as dictionary, overriding message."""
        msg = {}

        if any(
            [
                self.title,
                self.title_loc_key,
                self.title_loc_args,
                self.action_loc_key,
                self.loc_key,
                self.loc_args,
                self.launch_image,
            ]
        ):
            alert = {
                "body": message,
                "title": self.title,
                "title-loc-key": self.title_loc_key,
                "title-loc-args": self.title_loc_args,
                "action-loc-key": self.action_loc_key,
                "loc-key": self.loc_key,
                "loc-args": self.loc_args,
                "launch-image": self.launch_image,
            }

            alert = compact_dict(alert)
        else:
            alert = message

        msg.update(self.extra or {})
        msg["aps"] = compact_dict(
            {
                "alert": alert,
                "badge": self.badge,
                "sound": self.sound,
                "category": self.category,
                "content-available": 1 if self.content_available else None,
                "mutable-content": 1 if self.mutable_content else None,
                "thread-id": self.thread_id,
            }
        )

        return msg

    def _construct_truncated_dict(self, message):
        """Return truncated message as dictionary."""
        msg = None
        ending = ""

        while message:
            data = self._construct_dict(message + ending)

            if len(json_dumps(data)) <= self.max_payload_length:
                msg = data
                break

            message = message[0:-1]
            ending = "..."

        if msg is None:  # pragma: no cover
            msg = self._construct_dict()

        return msg

    def to_dict(self):
        """Return message as dictionary, truncating if needed."""
        if self.message and self.max_payload_length:
            return self._construct_truncated_dict(self.message)

        return self._construct_dict(self.message)

    def to_json(self):
        """Return message as JSON string."""
        return json_dumps(self.to_dict())

    def __len__(self):
        """Return length of serialized message."""
        return len(self.to_json())

class APNSMessageStream(object):
    """Iterable object that yields a binary APNS socket frame for each device token."""

    def __init__(self, tokens, message, expiration, priority, batch_size=1):
        self.tokens = tokens
        self.message = message
        self.expiration = expiration
        self.priority = priority
        self.batch_size = batch_size
        self.next_identifier = 0

    def seek(self, identifier):
        """
        Move token index to resume processing after token with index equal to
        `identifier`.

        Typically, `identifier` will be the token index that generated an error
        during send. Seeking to this identifier will result in processing the
        tokens that come after the error-causing token.

        Args:
            identifier (int): Index of tokens to skip.
        """
        self.next_identifier = identifier

    def peek(self, n=None):
        return self.tokens[self.next_identifier : n]

    def eof(self):
        """Return whether all tokens have been processed."""
        return self.next_identifier >= len(self.tokens)

    def pack(self, token, identifier, message, expiration, priority):
        """Return a packed APNS socket frame for given token."""
        token_bin = unhexlify(token)
        token_len = len(token_bin)
        message_len = len(message)

        # |CMD|FRAMELEN|{token}|{message}|{id:4}|{expiration:4}|{priority:1}
        # 5 items, each 3 bytes prefix, then each item length
        frame_len = (
            APNS_PUSH_FRAME_ITEM_COUNT * APNS_PUSH_FRAME_ITEM_PREFIX_LEN
            + token_len
            + message_len
            + APNS_PUSH_IDENTIFIER_LEN
            + APNS_PUSH_EXPIRATION_LEN
            + APNS_PUSH_PRIORITY_LEN
        )
        frame_fmt = ">BIBH{0}sBH{1}sBHIBHIBHB".format(token_len, message_len)

        # NOTE: Each bare int below is the corresponding frame item ID.
        frame = struct.pack(
            frame_fmt,
            APNS_PUSH_COMMAND,
            frame_len,  # BI
            1,
            token_len,
            token_bin,  # BH{token_len}s
            2,
            message_len,
            message,  # BH{message_len}s
            3,
            APNS_PUSH_IDENTIFIER_LEN,
            identifier,  # BHI
            4,
            APNS_PUSH_EXPIRATION_LEN,
            expiration,  # BHI
            5,
            APNS_PUSH_PRIORITY_LEN,
            priority,
        )  # BHB

        return frame

    def __len__(self):
        """Return count of number of notifications."""
        return len(self.tokens)

    def __iter__(self):
        """Iterate through each device token and yield APNS socket frame."""
        message = self.message.to_json()

        data = b""
        tokens = self.tokens[self.next_identifier :]

        for token_chunk in chunk(tokens, self.batch_size):
            for token in token_chunk:
                log.debug("Preparing notification for APNS token {0}".format(token))

                data += self.pack(
                    token, self.next_identifier, message, self.expiration, self.priority
                )
                self.next_identifier += 1

            yield data

            data = b""

class APNSFeedbackStream(object):
    """An iterable object that yields an expired device token."""

    def __init__(self, conn):
        self.conn = conn

    def __iter__(self):
        """Iterate through and yield expired device tokens."""
        header_format = "!LH"

        while True:
            data = self.conn.read(APNS_FEEDBACK_HEADER_LEN)

            if not data:
                break

            timestamp, token_len = struct.unpack(header_format, data)
            token_data = self.conn.read(token_len)

            if token_data:
                token = struct.unpack("{0}s".format(token_len), token_data)
                token = hexlify(token[0]).decode("utf8")

                yield APNSExpiredToken(token, timestamp)

class APNSResponse(object):
    """
    Response from APNS after sending tokens.

    Attributes:
        tokens (list): List of all tokens sent during bulk sending.
        message (APNSMessage): :class:`APNSMessage` object sent.
        errors (list): List of APNS exceptions for each failed token.
        failures (list): List of all failed tokens.
        successes (list): List of all successful tokens.
        token_errors (dict): Dict mapping the failed tokens to their respective
            APNS exception.

    .. versionadded:: 1.0.0
    """

    def __init__(self, tokens, message, errors):
        self.tokens = tokens
        self.message = message
        self.errors = errors
        self.failures = []
        self.successes = []
        self.token_errors = {}

        for err in errors:
            tok = tokens[err.identifier]
            self.failures.append(tok)
            self.token_errors[tok] = err

        self.successes = [token for token in tokens if token not in self.failures]

class APNSExpiredToken(namedtuple("APNSExpiredToken", ["token", "timestamp"])):
    """
    Represents an expired APNS token with the timestamp of when it expired.

    Attributes:
        token (str): Expired APNS token.
        timestamp (int): Epoch timestamp.
    """

    pass

def create_socket(host, port, certificate):
    """Create a socket connection to the APNS server."""
    try:
        with open(certificate, "r") as fileobj:
            fileobj.read()
    except Exception as ex:
        raise APNSAuthError(
            "The certificate at {0} is not readable: {1}".format(certificate, ex)
        )

    sock = socket.socket()

    sock = ssl.wrap_socket(sock, certfile=certificate, do_handshake_on_connect=False)
    sock.connect((host, port))
    sock.setblocking(0)

    log.debug("Performing SSL handshake with APNS on {0}:{1}".format(host, port))

    do_ssl_handshake(sock)

    return sock

def do_ssl_handshake(sock):
    """Perform SSL socket handshake for non-blocking socket."""
    while True:
        try:
            sock.do_handshake()
            break
        except ssl.SSLError as ex:  # pragma: no cover
            # For some reason, pylint on TravisCI's Python 2.7 platform
            # complains that these members don't exist. Add a disable flag to
            # bypass this.
            # pylint: disable=no-member
            if ex.args[0] == ssl.SSL_ERROR_WANT_READ:
                select.select([sock], [], [])
            elif ex.args[0] == ssl.SSL_ERROR_WANT_WRITE:
                select.select([], [sock], [])
            else:
                raise

def valid_token(token):
    """Return whether token is in valid format."""
    try:
        valid = token and unhexlify(token)
    except Exception:
        valid = False

    return valid

def invalid_tokens(tokens):
    """Return list of invalid APNS tokens."""
    return [token for token in tokens if not valid_token(token)]

def validate_tokens(tokens):
    """Check whether `tokens` are all valid."""
    invalid = invalid_tokens(tokens)

    if invalid:
        raise APNSInvalidTokenError(
            "Invalid token format. "
            "Expected hex string: {0}".format(", ".join(invalid))
        )

def validate_message(message):
    """Check whether `message` is valid."""
    if len(message) > APNS_MAX_NOTIFICATION_SIZE:
        raise APNSInvalidPayloadSizeError(
            "Notification body cannot exceed "
            "{0} bytes".format(APNS_MAX_NOTIFICATION_SIZE)
        )


#!/usr/bin/python
from brownie import PhotographyNFT, accounts, config, network
from scripts.util import OPENSEA_FORMAT
from metadata import sample_metadata
from pathlib import Path
from dotenv import load_dotenv
import yaml
import os
import requests
import json

PINATA_BASE_URL='https://api.pinata.cloud/'
PINATA_FILE_ENDPOINT = 'pinning/pinFileToIPFS'

load_dotenv()

def write_metadata(token_id):
    with open("./nft_info.yaml", "rb") as nft_info:
        nft_metadata = yaml.safe_load(nft_info)
    name = nft_metadata["name"]
    photo_metadata = sample_metadata.metadata_template
    network_dir = "./metadata/{}/".format(network.show_active())
    if not Path(network_dir).exists():
        os.mkdir(network_dir)
    metadata_file_name = (
        network_dir
        + name
        + "-"
        + str(token_id)
        + ".json"
    )
    print("Creating Metadata file: " + metadata_file_name)
    photo_metadata["name"] = nft_metadata["name"]
    photo_metadata["description"] = nft_metadata["description"]
    photo_metadata["external_url"] = nft_metadata["external_url"]
    image_path = nft_metadata["image_path"]
    image_uri = None
    if os.getenv("UPLOAD_PINATA") == "true":
        image_uri = upload_to_pinata(image_path)
    photo_metadata["image"] = image_uri
    with open(metadata_file_name, "w") as file:
        json.dump(photo_metadata, file)
    if os.getenv("UPLOAD_PINATA") == "true":
        return upload_to_pinata(metadata_file_name)

def upload_to_pinata(filepath):
    headers = {
        'pinata_api_key': os.getenv('PINATA_API_KEY'),
        'pinata_secret_api_key': os.getenv('PINATA_API_SECRET')
    }
    filename = filepath.split("/")[-1:][0]
    with Path(filepath).open("rb") as fp:
        file_binary = fp.read()
        response = requests.post(
            PINATA_BASE_URL + PINATA_FILE_ENDPOINT,
            files={"file": (filename, file_binary)},
            headers=headers,
        )
        ipfs_hash = response.json()["IpfsHash"]
        file_uri = "ipfs://{}?filename={}".format(
            ipfs_hash, filename)
        print(file_uri)
    return file_uri

def main():
    print("Working on " + network.show_active())
    account = accounts.add(config["wallets"]["from_key"])
    contract = PhotographyNFT[-1]
    num_of_tokens = contract.currentTokenId()

    # CREATE METADATA
    print(
        "The number of tokens you've deployed is: "
        + str(num_of_tokens)
    )
    token_id = num_of_tokens + 1
    token_uri = write_metadata(token_id)

    # CREATE NFT TOKEN
    with open("./nft_info.yaml", "rb") as nft_info:
        info = yaml.safe_load(nft_info)
    nft_name = info["name"]
    transaction = contract.createCollectible(nft_name, token_uri, {"from": account})
    transaction.wait(1)
    print("New PhotographyNFT tokenId is {} with name {}".format(token_id, nft_name))
    print(
        "Done! You can view your NFT at {}".format(
            OPENSEA_FORMAT.format(contract.address, token_id)
        )
    )
    print('Give up to 20 minutes, and hit the "refresh metadata" button')


from rest_framework import serializers
from proyectos.models import *
from kanban.serializers import *

class SprintSerializer(serializers.ModelSerializer):

    class Meta:
        model = Sprint
        fields = '__all__'

class ProyectoSerializer(serializers.ModelSerializer):
    sprints = serializers.SerializerMethodField()

    def get_sprints(self, obj):
        sprints = Sprint.objects.filter(proyecto_id=obj.id)
        serializer = SprintSerializer(sprints, many=True)
        return serializer.data

    class Meta:
        model = Proyecto
        fields = '__all__'

class EquipoSerializer(serializers.ModelSerializer):
    usuario = AutorSerializer(many=False, read_only=True)

    class Meta:
        model = Equipo
        fields = ['usuario']


from math import radians, cos, sin, asin, sqrt, fabs
from app.models import *
from django.db import transaction

@transaction.commit_manually
def manual_transaction():
    dec_cords = {}
    avaluos = Avaluo.objects.all()
    def iround(x):
        """iround(number) -> integer
        Round a number to the nearest integer."""
        y = round(x) - .5
        return int(y) + (y > 0)
    for avaluo in avaluos:
        if avaluo.LatitudG and avaluo.LatitudM and avaluo.LatitudS and avaluo.LongitudG and avaluo.LongitudM and avaluo.LongitudS:
            latsign = 1
            if(avaluo.LatitudG < 0):
                latsign = -1

            lonsign = 1
            if(avaluo.LongitudG < 0):
                lonsign = -1

            declat = round((
            fabs(iround(float(avaluo.LatitudG)* 1000000.00)) +
            (fabs(iround(float(avaluo.LatitudM)* 1000000.00))/60) +
            (fabs(iround(float(avaluo.LatitudS)* 1000000.00))/3600)
            )*latsign/1000000,5)

            declon = round((
            fabs(iround(float(avaluo.LongitudG)* 1000000.00)) +
            (fabs(iround(float(avaluo.LongitudM)* 1000000.00))/60) +
            (fabs(iround(float(avaluo.LongitudS)* 1000000.00))/3600)
            )*lonsign/1000000,5)

            avaluo.Declat = declat
            avaluo.Declon = declon
            avaluo.save()
            #print avaluo.FolioK, avaluo.Declat, avaluo.Declon
            transaction.commit()
        else:
            avaluo.Declat = 0
            avaluo.Declat = 0
            avaluo.save()
            #print avaluo.FolioK, avaluo.Declat, avaluo.Declon
            transaction.commit()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Aug  4 16:22:25 2022

@author: juan
"""

import pandas as pd
import numpy as np
from getData import sentinel
#%% reads dataset
f = open("SecretString.txt", "r")
NAME_DB=f.read()
#initializes the class
db=sentinel(NAME_DB)
#gets derived gas output dataframe
MIN_BLOCK=2000000
df=db.getGasfromBlock(minBlock=MIN_BLOCK,unique=False)
base_fee=db.getParentBaseFee(MIN_BLOCK)
df=df.sort_values('height')
#%% groups by block.
dfg=df.groupby(by=['height','actor_family','method'])['gas_used'].sum()
number_of_messages_per_block=df.groupby(by='height').count()
df_per_block=df.groupby(by=['height']).sum()
G_LIMIT=10_000_000_000.
G_TARGET=10_000_000_000./2.
#%%
import matplotlib.pyplot as plt

base_fees=np.array(df.groupby(by=['height'])['parent_base_fee'].unique())
base_fees=np.concatenate(base_fees)
plt.subplot(131)
plt.plot(base_fees/1E9)
plt.subplot(132)
G_tilde=8*np.diff(base_fees)/base_fees[:-1]
plt.plot(G_tilde)
plt.subplot(133)
plt.hist(G_tilde,bins=30)
plt.tight_layout()


#%%
from numpy import testing
from numpy.lib.function_base import median
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import lightgbm as lgb
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# %%
data = pd.read_csv("hydropower_time_series.csv")

# Identify transition points

data['medianpow'] = data['power'].rolling(window=20,center=True).median()
data['mapow'] = data['medianpow'].rolling(window=20,center=True).mean()
#%%

next5= data['mapow'].iloc[5:]
next5.reset_index(drop=True, inplace=True)
prev5= data['mapow'].iloc[:-5]
prev5.reset_index(drop=True, inplace=True)

data.reset_index(drop=True, inplace=True)
data['deriv'] = np.abs(next5-prev5)

# %%
data['transition'] = np.where(data['deriv']>0.15,30,0)

# %%
# ax = data.plot(figsize=(500,10))
# fig = ax.get_figure()
# fig.savefig('task1_tran_id.png')
# %%

# %%

#Extract data with sound value

rawdata = data[data['octave_62_5']>0]
# %%
clist = list(rawdata.columns)
fclist = clist[2:7]
fclist.append('transition')
fclist
#%%
rawdata = rawdata[fclist]
# %%
rawdata['transition'] = rawdata['transition']/30
rawdata['transition'] = rawdata['transition'].astype(int)
# %%
rawdata.info()
# %%
#unbalance data, try to discard some data with trans. =0

tran_pos_data = rawdata.loc[rawdata.transition == 1]
tran_non_data = rawdata.loc[rawdata.transition == 0]

discard_data,used_tran_non_data = train_test_split(tran_non_data,test_size = 0.007)

#combined_data is final data set
combined_data = pd.concat([tran_pos_data,used_tran_non_data],ignore_index=True)

# %%
dftrain,dftest = train_test_split(combined_data,test_size=0.1)
# %%
lgb_train = lgb.Dataset(dftrain.drop(['transition'],axis = 1),label=dftrain['transition'])

lgb_valid = lgb.Dataset(dftest.drop(['transition'],axis = 1),label=dftest['transition'],reference=lgb_train)
# %%
params = {
    'boosting_type': 'gbdt',
    'objective':'binary',
    'metric': 'auc',
    'num_leaves': 50,
    'learning_rate': 0.05,
    'feature_fraction': 0.9,
    'bagging_fraction': 0.8,
    'bagging_freq': 5,
    'verbose': 0,
    'max_depth':25

}

results = {}
gbm = lgb.train(params,
                lgb_train,
                num_boost_round= 50,
                valid_sets=(lgb_valid, lgb_train),
                valid_names=('validate','train'),
                early_stopping_rounds = 10,
                evals_result= results)

#%%

y_pred_train = gbm.predict(dftrain.drop('transition',axis = 1), num_iteration=gbm.best_iteration)
y_pred_test = gbm.predict(dftest.drop('transition',axis = 1), num_iteration=gbm.best_iteration)

#all y_pred >0.5 is predicted as 1
print('train accuracy: {:.5} '.format(accuracy_score(dftrain['transition'],y_pred_train>0.5)))
print('valid accuracy: {:.5} \n'.format(accuracy_score(dftest['transition'],y_pred_test>0.5)))

lgb.plot_metric(results)
lgb.plot_importance(gbm,importance_type = "gain")


from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk

# frame initiation
root = Tk()
root.geometry("1920x1000")
root.propagate(False)

# frame style
style = ttk.Style()
style.configure("ok.TButton", font=("Calibri",20,"bold"), justify='center')
style.configure("desc.TLabel", font=("Calibri",16))

# load the image

# for main page
image_data = ['Image\\image1.png']
image = []
for path in image_data:
    image1 = Image.open(path)
    image1 = image1.resize((1000,600))
    image1 = ImageTk.PhotoImage(image1)
    image.append(image1)

# for stories
image_data = ['Image\\image2.png',
    'Image\\image3a.png','Image\\image3b.png',
    'Image\\image4a.png','Image\\image4b.png','Image\\image4c.png','Image\\image4d.png',
    'Image\\image5a.png','Image\\image5b.png','Image\\image5c.png','Image\\image5d.png',
    'Image\\image5e.png','Image\\image5f.png','Image\\image5g.png','Image\\image5h.png']
for path in image_data:
    image1 = Image.open(path)
    image1 = image1.resize((900,550))
    image1 = ImageTk.PhotoImage(image1)
    image.append(image1)

# Define the main window for each page
page1 = ttk.Frame(root)
page2 = ttk.Frame(root)

page3 =[]
for data in range(2):
     page3.append(ttk.Frame(root))

page4 = []
for data in range(4):
    page4.append(ttk.Frame(root))

page5 = []
for data in range(8):
    page5.append(ttk.Frame(root))

# Create a function to easily use tkraise method
def raise_frame(frame):
    frame.tkraise()

def next_step():
    if mandatory_entry.get():
        # the user entered data in the mandatory entry: proceed to next step
        raise_frame(page2)
    else:
        # the mandatory field is empty
        mandatory_entry.focus_set()

#------PAGE 1---------------------

# Create the widgets
mandatory_entry = ttk.Entry(page1, width=30, font=('Calibri',16))
page1image = ttk.Label(page1, text='Image Page 1', image=image[0])
page1_entrylabel = ttk.Label(page1, text='Input Name', style="desc.TLabel")
page1button = ttk.Button(page1, text='Start', style="ok.TButton", command=next_step)

# Place the widgets
page1['padding'] = (250,50)

page1.grid(column=0, row=0)
page1image.grid(column=0,row=1, columnspan=3, pady=(0,25))
page1_entrylabel.grid(column=0, row = 2)
mandatory_entry.grid(column=1, row = 2)
page1button.grid(column=2, row=2)

def create_page_story(root_frame, title_text, image, button_a_label, button_b_label, page_back_act, page_a_act, page_b_act, lastpage = False):
    # Create the widgets
    frame_a = ttk.Frame(root_frame, width=1920,height=200)
    page_label = ttk.Label(frame_a, text=title_text, style="desc.TLabel")

    frame_b = ttk.Frame(root_frame, width=1920, height=800)
    page_image = ttk.Label(frame_b, text='Image Page', image=image)
    page_button1 = ttk.Button(frame_b, text='Back', style="ok.TButton", command=lambda:raise_frame(page_back_act))

    if not lastpage:
        page_button2 = ttk.Button(frame_b, text=button_a_label, style="ok.TButton", command=lambda:raise_frame(page_a_act))
        page_button3 = ttk.Button(frame_b, text=button_b_label, style="ok.TButton", command=lambda:raise_frame(page_b_act))
    else:
        page_button2 = ttk.Button(frame_b, text="Restart", style="ok.TButton", command=lambda:raise_frame(page1))

    # Place the widgets
    root_frame['padding'] = (250,0)
    frame_a['padding'] = (0,0,0,10)

    root_frame.grid(column=0, row=0)
    frame_a.grid(column=0, row=0)
    frame_b.grid(column=0, row=1)

    page_label.grid(column=0, row=0)

    if not lastpage:
        page_image.grid(column=0, row=2, columnspan=3, pady=(0,10))
        page_button1.grid(column=0, row=3)
        page_button2.grid(column=1, row=3)
        page_button3.grid(column=2, row=3)
    else:
        page_image.grid(column=0, row=2, columnspan=2, pady=(0,40))
        page_button1.grid(column=0, row=3)
        page_button2.grid(column=1, row=3)

    return root_frame

title_text = '''Boby is a 23-year-old male. He is from Canada and his tour group has fled away. You are now
in charge of bossing him around the city. Remember, you're not touring him; you're bossing him
because, apparently, he doesn't have a choice in this game, unlike you.'''

# page2
page2 = create_page_story(page2, title_text, image[1], "Bring him to\nSanta Monica Pier", "Bring him to\nThe Griffith Observatory", page1, page3[0], page3[1])

# # page3
title_text='''
You've decided to take Bop to the Santa Monica Pier. He is exhausted and sweating profusely
as a result of the hot weather. He needs food.'''
create_page_story(page3[0], title_text, image[2], "Ask him to\nFish", "5-star\nrestaurant", page2, page4[0], page4[1])

title_text='''
You've decided to take Bop to Griffith Observatory. You arrived a little early, so sunset will be
just around the corner by the time his free parking runs out.'''
create_page_story(page3[1], title_text, image[3], "Wait for\nthe sunset", "We should\ngo!", page2, page4[2], page4[3])

# page4
title_text='''
Oh no! His fishing rod was bitten and dragged away by a shark. Bop has entered the waters.
He meets a cute shark. What should he do now?'''
create_page_story(page4[0], title_text, image[4], "Pet the\nShark", "Fight the\nShark", page3[0], page5[0], page5[1])

title_text='''
The restaurant charged Bop $740 for his wagyu steak. He can’t afford to pay.
What should Bop do?'''
create_page_story(page4[1], title_text, image[5], "Run\nAway", "Sell all\nBop's assets", page3[0], page5[2], page5[3])

title_text='''
Poor Bop got his car towed away.
He is now carless, what should he do now?'''
create_page_story(page4[2], title_text, image[6], "Steal \na Car", "Run back\nhome", page3[1], page5[4], page5[5])

title_text='''
Bop sprained his ankle rushing to his car to avoid the parking fee. When he tripped, he fractured
his ribs. Remember, medical school taught you nothing, you didn’t graduate and you got broke. How to start?'''
create_page_story(page4[3], title_text, image[7], "Call\n911", "Use your\nKnowledge", page3[1], page5[6], page5[7])

# page5
title_text='''
The shark has made the conscious decision to interpret Bop's following movement as a hostile
one. The cuteness of his appearance belies his unfriendly nature. Poor Bop.'''
create_page_story(page5[0], title_text, image[8], "", "", page4[0], page4[0], page4[0], lastpage=True)

title_text='''
What was Bop thinking?! Oh well, you managed to kill him with your domineering ways. Pray
that Bop finally finds some peace.'''
create_page_story(page5[1], title_text, image[9], "", "", page4[0], page4[0], page4[0], lastpage=True)

title_text='''
Bop might as well be the steak now.
Rest in peace Bop.'''
create_page_story(page5[2], title_text, image[10], "", "", page4[1], page4[0], page4[0], lastpage=True)

title_text='''
Terrible! Bop sold his one and only valuable item: the ring he stole from the pawn shop.
This might as well be his karma.'''
create_page_story(page5[3], title_text, image[11], "", "", page4[1], page4[0], page4[0], lastpage=True)

title_text='''
Sadly, Bop attempted to rob an undercover cop's car.
What an adventure, Bop!'''
create_page_story(page5[4], title_text, image[12], "", "", page4[2], page4[0], page4[0], lastpage=True)

title_text='''
Bop encountered a cardiac arrest after walking 20km back home.
What a tragic and unfortunate death.'''
create_page_story(page5[5], title_text, image[13], "", "", page4[2], page4[0], page4[0], lastpage=True)

title_text='''
They didn’t get there on time.
Internal bleeding got Bop dead.'''
create_page_story(page5[6], title_text, image[14], "", "", page4[3], page4[0], page4[0], lastpage=True)

title_text='''
Yucks! You accidentally poked his heart
during the surgery.'''
create_page_story(page5[7], title_text, image[15], "", "", page4[3], page4[0], page4[0], lastpage=True)

#---RAISING PAGE 1 TO START PROGRAM--------
raise_frame(page1)
root.mainloop()


#!/usr/bin/env python

"""Tests for Alpha neuron."""

import pytest
import snntorch as snn
import torch

@pytest.fixture(scope="module")
def input_():
    return torch.Tensor([0.25, 0]).unsqueeze(-1)

@pytest.fixture(scope="module")
def alpha_instance():
    return snn.Alpha(alpha=0.6, beta=0.5, reset_mechanism="subtract")

@pytest.fixture(scope="module")
def alpha_reset_zero_instance():
    return snn.Alpha(alpha=0.6, beta=0.5, reset_mechanism="zero")

@pytest.fixture(scope="module")
def alpha_reset_none_instance():
    return snn.Alpha(alpha=0.6, beta=0.5, reset_mechanism="none")

@pytest.fixture(scope="module")
def alpha_hidden_instance():
    return snn.Alpha(alpha=0.6, beta=0.5, init_hidden=True)

@pytest.fixture(scope="module")
def alpha_hidden_reset_zero_instance():
    return snn.Alpha(
        alpha=0.6, beta=0.5, init_hidden=True, reset_mechanism="zero"
    )

@pytest.fixture(scope="module")
def alpha_hidden_reset_none_instance():
    return snn.Alpha(
        alpha=0.6, beta=0.5, init_hidden=True, reset_mechanism="none"
    )

class TestAlpha:

    with pytest.raises(ValueError):
        snn.Alpha(0.5, 0.5)

    def test_alpha(self, alpha_instance, input_):
        syn_exc, syn_inh, mem = alpha_instance.init_alpha()

        syn_exc_rec = []
        syn_inh_rec = []
        mem_rec = []
        spk_rec = []

        for i in range(2):
            spk, syn_exc, syn_inh, mem = alpha_instance(
                input_[i], syn_exc, syn_inh, mem
            )

            syn_exc_rec.append(syn_exc)
            syn_inh_rec.append(syn_inh)
            mem_rec.append(mem)
            spk_rec.append(spk)

        assert spk_rec[0] == spk_rec[1]
        print(spk_rec)
        print(syn_exc_rec)
        print(syn_inh_rec)
        assert syn_exc_rec[0] + syn_inh_rec[0] == 0
        assert syn_exc_rec[1] + syn_inh_rec[1] > 0
        assert mem_rec[0] < mem_rec[1]

    def test_alpha_reset(
        self,
        alpha_instance,
        alpha_reset_zero_instance,
        alpha_reset_none_instance,
    ):
        lif1 = alpha_instance
        lif2 = alpha_reset_zero_instance
        lif3 = alpha_reset_none_instance

        assert lif1.reset_mechanism_val == 0
        assert lif2.reset_mechanism_val == 1
        assert lif3.reset_mechanism_val == 2

        lif1.reset_mechanism = "zero"
        lif2.reset_mechanism = "none"
        lif3.reset_mechanism = "subtract"

        assert lif1.reset_mechanism_val == 1
        assert lif2.reset_mechanism_val == 2
        assert lif3.reset_mechanism_val == 0

    def test_alpha_init_hidden(self, alpha_hidden_instance, input_):

        spk_rec = []

        for i in range(2):
            spk = alpha_hidden_instance(input_[i])
            spk_rec.append(spk)

        assert spk_rec[0] == spk_rec[1]

    def test_alpha_init_hidden_reset_zero(
        self, alpha_hidden_reset_zero_instance, input_
    ):

        spk_rec = []

        for i in range(2):
            spk = alpha_hidden_reset_zero_instance(input_[i])
            spk_rec.append(spk)

        assert spk_rec[0] == spk_rec[1]

    def test_alpha_init_hidden_reset_none(
        self, alpha_hidden_reset_none_instance, input_
    ):

        spk_rec = []

        for i in range(2):
            spk = alpha_hidden_reset_none_instance(input_[i])
            spk_rec.append(spk)

        assert spk_rec[0] == spk_rec[1]

    def test_alpha_cases(self, alpha_hidden_instance, input_):
        with pytest.raises(TypeError):
            alpha_hidden_instance(input_, input_)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('core', '0006_auto_20150608_2105'),
    ]

    operations = [
        migrations.AlterField(
            model_name='post',
            name='id',
            field=models.TextField(max_length=255, unique=True, serialize=False, primary_key=True),
        ),
    ]


import requests
import hashlib
import difflib

from panopuppet.pano.puppetdb.puppetdb import api_get as pdb_api_get, get_server, mk_puppetdb_query

__author__ = 'takeshi'

requests.packages.urllib3.disable_warnings()

def get_hash(data):
    m = hashlib.md5()
    m.update(data.encode('utf-8'))
    return m.hexdigest()

def get_file(request, certname, environment, rtitle, rtype, md5sum_from=None, md5sum_to=None, diff=False,
             file_status='from'):
    puppetdb_source, puppetdb_certs, puppetdb_verify = get_server(request=request)
    filebucket_source, filebucket_certs, filebucket_verify, filebucket_show = get_server(request=request,
                                                                                type='filebucket')
    fileserver_source, fileserver_certs, fileserver_verify, fileserver_show = get_server(request=request,
                                                                                type='fileserver')
    # If Clientbucket is enabled continue else return False

    def fetch_filebucket(url, method):
        headers = {
            'Accept': 's',
        }
        methods = {'get': requests.get,
                   'head': requests.head,
                   }
        if method not in methods:
            print('No can has method: %s' % method)
            return False
        resp = methods[method](url,
                               headers=headers,
                               verify=filebucket_verify,
                               cert=filebucket_certs)
        if resp.status_code != 200:
            return False
        else:
            return resp.text

    def fetch_fileserver(url, method):
        methods = {'get': requests.get,
                   }

        if method not in methods:
            print('No can has method: %s' % method)
            return False
        resp = methods[method](url,
                               verify=fileserver_verify,
                               cert=fileserver_certs)
        if resp.status_code != 200:
            return False
        else:
            return resp.text

    def get_resource(certname, rtype, rtitle):
        resource_params = {
            'query':
                {
                    'operator': 'and',
                    1: '["=", "certname", "' + certname + '"]',
                    2: '["=", "type", "' + rtype + '"]',
                    3: '["=", "title", "' + rtitle + '"]'

                },
        }
        data = pdb_api_get(
            api_url=puppetdb_source,
            path='resources',
            verify=puppetdb_verify,
            cert=puppetdb_certs,
            params=mk_puppetdb_query(resource_params, request))
        if not data:
            return False
        else:
            return data

    if not filebucket_show or not fileserver_source:
        return False
    if file_status == 'both':
        if md5sum_to and md5sum_from and certname and rtitle and rtype:
            if diff:
                # is the hash from puppetdb resource same as md5sum_to
                hash_matches = False

                md5sum_from = md5sum_from.replace('{md5}', '')
                md5sum_to = md5sum_to.replace('{md5}', '')

                from_url = filebucket_source + environment + '/file_bucket_file/md5/' + md5sum_from
                to_url = filebucket_source + environment + '/file_bucket_file/md5/' + md5sum_to

                if fetch_filebucket(from_url, 'head') is not False:
                    resource_from = fetch_filebucket(from_url, 'get')
                else:
                    # Could not find old MD5 in Filebucket
                    return False
                if fetch_filebucket(to_url, 'head') is not False:
                    resource_to = fetch_filebucket(to_url, 'get')
                # Try puppetdb resources if not found in filebucket.
                else:
                    resource_to = get_resource(certname=certname, rtype=rtype, rtitle=rtitle)
                    if resource_to is False:
                        # Could not find new file in Filebucket or as a PuppetDB Resource
                        return False
                    else:
                        resource_to = resource_to[0]
                    if 'content' in resource_to['parameters']:
                        resource_to = resource_to['parameters']['content']
                        hash_of_resource = get_hash(resource_to)
                        if hash_of_resource == md5sum_to:
                            # file from resource matches filebucket md5 hash
                            hash_matches = True
                    # Solve the viewing of source files by retrieving it from Puppetmaster
                    elif 'source' in resource_to['parameters'] and fileserver_show is True:
                        source_path = resource_to['parameters']['source']
                        if source_path.startswith('puppet://'):
                            # extract the path for the file
                            source_path = source_path.split('/')  # ['puppet:', '', '', 'files', 'autofs', 'auto.home']
                            source_path = '/'.join(source_path[3:])  # Skip first 3 entries since they are not needed
                            # https://puppetmaster.example.com:8140/production/file_content/files/autofs/auto.home
                            url = fileserver_source + environment + '/file_content/' + source_path
                            resource_to = fetch_fileserver(url, 'get')
                    else:
                        return False
                # now that we have come this far, we have both files.
                # Lets differentiate the shit out of these files.
                from_split_lines = resource_from.split('\n')
                to_split_lines = resource_to.split('\n')
                diff = difflib.unified_diff(from_split_lines, to_split_lines)
                diff = ('\n'.join(list(diff))).split('\n')
                return diff
            else:
                return False
        else:
            return False

    if file_status == 'from' and md5sum_from:
        md5sum = md5sum_from.replace('{md5}', '')
    elif file_status == 'to' and md5sum_to:
        md5sum = md5sum_to.replace('{md5}', '')
    else:
        return False
    # Creates headers and url from the data we got

    url_clientbucket = filebucket_source + environment + '/file_bucket_file/md5/' + md5sum
    if fetch_filebucket(url_clientbucket, 'head') is False:
        # Check if theres a resource available for the latest file available
        if file_status == 'to':
            resp_pdb = get_resource(certname=certname, rtype=rtype, rtitle=rtitle)
            # we got the data lets give the user the good news.
            if resp_pdb:
                resource_data = resp_pdb[0]
                if 'content' in resource_data['parameters']:
                    prepend_text = 'This file with MD5 %s was found in PuppetDB Resources.\n\n' % (
                        get_hash(resource_data['parameters']['content']))
                    return prepend_text + resource_data['parameters']['content']
                elif 'source' in resource_data['parameters'] and fileserver_show is True:
                    source_path = resource_data['parameters']['source']
                    if source_path.startswith('puppet://'):
                        # extract the path for the file
                        source_path = source_path.split('/')  # ['puppet:', '', '', 'files', 'autofs', 'auto.home']
                        source_path = '/'.join(source_path[3:])  # Skip first 3 entries since they are not needed
                        # https://puppetmaster.example.com:8140/production/file_content/files/autofs/auto.home
                        url = fileserver_source + environment + '/file_content/' + source_path
                        source_content = fetch_fileserver(url, 'get')
                        prepend_text = 'This file with MD5 %s was retrieved from the PuppetMaster Fileserver.\n\n' % (
                            get_hash(source_content))
                        return prepend_text + source_content
                    else:
                        return False
                else:
                    return False
            # the file can't be found as a resource and or fileserver support not enabled
            else:
                return False
        # We probably don't want to search for resources if its the old file.
        else:
            return False
    else:
        filebucket_results = fetch_filebucket(url_clientbucket, 'get')
        prepend_text = 'This file with MD5 %s was found in Filebucket.\n\n' % (md5sum)
        return prepend_text + filebucket_results


import boto3
from datetime import datetime

client = boto3.client('rds')
response = client.restore_db_instance_to_point_in_time(
    RestoreTime=datetime(2021, 9, 15),
    SourceDBInstanceIdentifier='database-instance-01',
    TargetDBInstanceIdentifier='restored-db-01'
)
print(response)


import os
import trimesh
import pyrender
import numpy as np
from get_mesh import as_mesh
import matplotlib.pyplot as plt
import imageio

egypt = trimesh.load('../Dataset/models/model_normalized.obj')
egypt_trimesh = as_mesh(egypt)
egypt_mesh = pyrender.Mesh.from_trimesh(egypt_trimesh)

picture_folder = 'model_picture_data'
depth_folder = 'model_depth_data'
os.makedirs(picture_folder, exist_ok=True)
os.makedirs(depth_folder, exist_ok=True)
def generate_points(radius, num_points):
    theta_values = np.linspace(0, np.pi, num_points)
    phi_values = np.linspace(0, np.pi, num_points)
    points = []

    for phi in phi_values:
        for theta in theta_values:
            x = radius * np.cos(theta) * np.cos(phi)
            y = radius * np.sin(theta) * np.cos(phi)
            z = radius * np.sin(phi)
            points.append((x, z, y))

    return points

focal = 1.0

points_on_semicircle = generate_points(focal, 10)

camera_positions = []

for i, point in enumerate(points_on_semicircle):
    camera_position =  np.array(point)
    camera_target = np.array([0,0,0])
    D = camera_position - camera_target
    D = D / np.linalg.norm(D) if np.linalg.norm(D) != 0 else D
    # print(f'D {D}')

    up = np.array([0,1,0])
    R = np.cross(up, D)
    R = R / np.linalg.norm(R) if np.linalg.norm(R) != 0 else R
    # print(f'R {R}')

    U = np.cross(D, R)
    # print(f'U {U}')

    rotation = np.eye(4)
    rotation[0:3, 0] = R
    rotation[0:3, 1] = U
    rotation[0:3, 2] = D
    # print(rotation)

    import copy
    r_t = copy.deepcopy(rotation)
    r_t[0:3,3] = camera_position
    camera_positions.append(r_t)

np.savez('camera_positions.npz', arr1=camera_positions)

scene = pyrender.Scene(bg_color=[0, 0, 0, 0])
scene.add(egypt_mesh, name='model')

for i, camera_pose in enumerate(camera_positions):
    camera = pyrender.PerspectiveCamera(yfov=np.pi / 3.0, aspectRatio=1.0)
    camera_node = scene.add(camera, pose=camera_pose)

    DireLight = pyrender.DirectionalLight(color=np.ones(3), intensity=15.0)
    light_node = scene.add(DireLight, pose=camera_pose)

    r = pyrender.OffscreenRenderer(viewport_height=200, viewport_width=200)

    color, depth = r.render(scene)

    img_filename = os.path.join(picture_folder, f'model_image_{i}.png')
    imageio.imwrite(img_filename, color)

    depth_normalized = (depth - depth.min()) / (depth.max() - depth.min())
    depth_image = (255 * depth_normalized).astype(np.uint8)

    depth_filename = os.path.join(depth_folder, f'model_depth_{i}.png')
    imageio.imwrite(depth_filename, depth_image)

    scene.remove_node(camera_node)
    scene.remove_node(light_node)
    r.delete()

# Create Cloud Point Data
def sample_points(obj_path, n_points=10000):
    mesh = trimesh.load_mesh(obj_path)
    points, _ = trimesh.sample.sample_surface(mesh, n_points)
    return points

def rotate_model(points):
    rotated_points = []

    rotation_matrix = np.array([[1, 0, 0],
                                [0, 0, -1],
                                [0, 1, 0]])

    for point in points:
        rotated_point = np.dot(rotation_matrix, point)
        rotated_points.append(rotated_point)

    return rotated_points

def sample_points(obj_path, n_points=10000):
    # Load the content at obj_path
    loaded = trimesh.load(obj_path)

    # Check if it's a single Trimesh or a Scene
    if isinstance(loaded, trimesh.Scene):
        # Extract the first mesh from the scene (you may need to adjust if you want a different mesh)
        mesh = loaded.dump(concatenate=True)
    else:
        mesh = loaded

    points, _ = trimesh.sample.sample_surface(mesh, n_points)
    return points

obj_path = "../Dataset/egypt_model/baked_mesh.obj"
points = sample_points(obj_path, n_points=10000)
points = rotate_model(points)
with open("model_point.ply", 'w') as ply:
        ply.write("ply\n")
        ply.write("format ascii 1.0\n")
        ply.write(f"element vertex {len(points)}\n")
        ply.write("property float x\n")
        ply.write("property float y\n")
        ply.write("property float z\n")
        ply.write("end_header\n")
        for point in points:
            ply.write(" ".join(map(str, point)) + '\n')


import atexit
import datetime
import logging
import re
import signal
import threading
import time
from typing import Any, List, Optional, Sequence, Tuple

import lxml
import lxml.etree
import requests

from ngk import config, parser_xyz
from ngk.comments_processor import CommentsProcessor
from ngk.log import get_logger, redirect_basic_logging
from ngk.html_util import inner_html_ru, normalize_text
from ngk.parse_error import ParseError
from ngk.parser import ParsedComment, ParsedPost, ParsedUser
from ngk.parser_ru import parse_sink, parse_post
from ngk.schema import Comment, CommentIdStorage, ScopedSession, SyncState

L = get_logger('scan_comments', logging.INFO)
redirect_basic_logging(L)

COMMENTS_URL = 'http://govnokod.ru/comments'
COMMENTS_URL_XYZ = 'https://govnokod.xyz/comments/'
FAST_DELAY = 8
SLOW_DELAY = 60
FAST_TO_SLOW_STEPS = 20

exit_event = threading.Event()
threads_exited_events: List[threading.Event] = []

def fetch_latest_comments() -> List[ParsedComment]:
    L.debug("Fetching comments from ru...")
    r = requests.get(COMMENTS_URL, headers=config.DEFAULT_HEADERS, timeout=30)
    r.raise_for_status()
    return parse_sink(r.content)

def fetch_latest_comments_xyz() -> List[parser_xyz.CommentXyz]:
    L.debug("Fetching comments from xyz...")
    r = requests.get(COMMENTS_URL_XYZ, headers=config.DEFAULT_HEADERS, timeout=30)
    r.raise_for_status()
    root = lxml.etree.HTML(r.content)
    return parser_xyz.parse_comments(root)

def update_sync_states(comments: Sequence[ParsedComment], processor: CommentsProcessor) -> bool:
    has_updates = False
    updated_comments: List[Comment] = []
    with ScopedSession() as session:
        for comment_sink in comments:
            post_id, comment_id, comment_text = comment_sink.post_id, comment_sink.id_ru, comment_sink.text
            comment_db = session.query(Comment).filter(Comment.comment_id == comment_id).first()
            if comment_db is not None and comment_db.text != comment_text:
                comment_db.text = comment_text
                updated_comments.append(comment_db)

            state = session.query(SyncState).filter(SyncState.post_id == post_id).one_or_none()
            if not state:
                L.info("Got new comment %d for new post %d", comment_id, post_id)
                has_updates = True
                state = SyncState(post_id=post_id, last_comment_id=comment_id, pending=True, priority=SyncState.PRIORITY_HAS_COMMENTS)
                session.add(state)
            else:
                if state.last_comment_id is None or comment_id > state.last_comment_id:
                    L.info("Got new comment %d for post %d", comment_id, post_id)
                    has_updates = True
                    state.last_comment_id = comment_id
                    state.pending = True
                    state.priority=SyncState.PRIORITY_HAS_COMMENTS

        if len(updated_comments) > 0:
            L.info(f'Fast-fetched {len(updated_comments)} updated ' + \
                   f'comment{"s" if len(updated_comments) > 1 else ""}: {[c.comment_id for c in updated_comments]}')
            session.commit()
            processor.on_comments_update([], updated_comments)

    return has_updates

def update_xyz_states(comments: Sequence[parser_xyz.CommentXyz], processor: CommentsProcessor) -> None:
    with ScopedSession() as session:
        updated_comments = []
        prefetched_comment_id_pairs = []
        for comment in comments:
            if comment.id_xyz is not None and comment.id_ru is not None:
                comment_db = session.query(Comment).filter(Comment.comment_id == comment.id_ru).first()
                if comment_db is None or comment_db.comment_id_storage is None:
                    id_storage = session.query(CommentIdStorage).filter(CommentIdStorage.comment_id_ru == comment.id_ru).first()
                    if id_storage is None:
                        id_storage = CommentIdStorage()
                        id_storage.comment_id_ru = comment.id_ru
                        id_storage.comment_id_xyz = comment.id_xyz
                        session.add(id_storage)
                        prefetched_comment_id_pairs.append((comment.id_ru, comment.id_xyz))
                    elif id_storage.comment_id_xyz != comment.id_xyz:
                        id_storage.comment_id_xyz = comment.id_xyz
                        prefetched_comment_id_pairs.append((comment.id_ru, comment.id_xyz))

                    if comment_db is not None:
                        comment_db.comment_id_storage = id_storage
                        updated_comments.append(comment_db)

                elif comment_db.comment_id_storage.comment_id_xyz != comment.id_xyz:
                    comment_db.comment_id_storage.comment_id_xyz = comment.id_xyz
                    session.merge(comment_db.comment_id_storage)
                    updated_comments.append(comment_db)

        if len(prefetched_comment_id_pairs) > 0:
            session.flush()
            to_print: list = prefetched_comment_id_pairs[:5]
            if len(prefetched_comment_id_pairs) > 5:
                to_print.append('...')
            L.info(f'Prefetched xyz ids for {len(prefetched_comment_id_pairs)} comments: [{", ".join(map(str, to_print))}]')

        if len(updated_comments) > 0:
            session.commit()
            to_print = [(c.comment_id, c.comment_id_storage.comment_id_xyz) for c in updated_comments[:5]]
            if len(updated_comments) > 5:
                to_print.append('...')
            L.info(f'Fetched xyz ids for {len(updated_comments)} comments: [{", ".join(map(str, to_print))}]')
            processor.on_comments_update([], updated_comments)

def worker_ru(thread_exited_event: threading.Event) -> None:
    thread_exited_event.clear()
    processor = CommentsProcessor(config.REDIS_HOST,
                                  config.REDIS_PORT,
                                  config.REDIS_PASSWORD,
                                  config.REDIS_CHANNEL,
                                  L)
    L.info("=== ru worker started ===")
    fast_requests = 0
    while True:
        try:
            comments = fetch_latest_comments()
            has_updates = update_sync_states(comments, processor)
            if has_updates:
                fast_requests = FAST_TO_SLOW_STEPS
        except Exception as e:
            L.exception(e)
            fast_requests = 0

        if fast_requests > 0:
            delay = FAST_DELAY
            fast_requests -= 1
        else:
            delay = SLOW_DELAY

        if exit_event.wait(delay):
            break

    thread_exited_event.set()

def worker_xyz(thread_exited_event: threading.Event) -> None:
    thread_exited_event.clear()
    processor = CommentsProcessor(config.REDIS_HOST,
                                  config.REDIS_PORT,
                                  config.REDIS_PASSWORD,
                                  config.REDIS_CHANNEL,
                                  L)
    L.info("=== xyz worker started ===")
    fast_requests = 0
    last_xyz_id: Optional[int] = -1
    while True:
        try:
            comments = fetch_latest_comments_xyz()
            update_xyz_states(comments, processor)

            if comments[0].id_xyz != last_xyz_id:
                fast_requests = FAST_TO_SLOW_STEPS
                last_xyz_id = comments[0].id_xyz
        except Exception as e:
            L.exception(e)
            fast_requests = 0

        if fast_requests > 0:
            delay = FAST_DELAY
            fast_requests -= 1
        else:
            delay = SLOW_DELAY

        if exit_event.wait(delay):
            break

    thread_exited_event.set()

def graceful_exit(signum: int, frames: Any) -> None:
    L.info('Exiting...')
    exit_event.set()
    while not all((e.is_set() for e in threads_exited_events)):
        for e in threads_exited_events:
            e.wait()
    L.info('All threads stopped. Goodbye!')

def main() -> None:
    threads_exited_events.append(threading.Event())
    thread_ru = threading.Thread(target=worker_ru, name='RU', args=(threads_exited_events[-1],))

    threads_exited_events.append(threading.Event())
    thread_xyz = threading.Thread(target=worker_xyz, name='XYZ', args=(threads_exited_events[-1],))

    signal.signal(signal.SIGINT, graceful_exit)
    signal.signal(signal.SIGTERM, graceful_exit)

    thread_ru.start()
    thread_xyz.start()

    thread_ru.join()
    thread_xyz.join()

if __name__ == '__main__':
    main()


from Méthode import *
import numpy as np
import matplotlib.pyplot as plt
import math

################################################

initialisation = "carre"
#initialisation = "dirac"

trace = True
#trace = False

mesure_erreur = True
#mesure_erreur = False
val_erreur = 0

u0 = condition_initiale(initialisation)
plt.plot(x, u0[:,0],label="solution numerique")
plt.title("Resolution de l'équation de Fokker-Plank t = 0 s")
plt.legend()
plt.show()

if trace == True:
    for j in range(1,Nt+1):
        u1 = solution_numerique(u0)
        u0 = u1

        if j == 1:
            x_trace = [x[15 * i] for i in range(len(x) // 15 - 1)]
            u0_trace = [u0[15 * i, 0] for i in range(len(x) // 15 - 1)]
            #line, = plt.plot(x, u0[:, 0], "o", color="red", label="solution numerique")
            line, = plt.plot(x_trace, u0_trace, "o", color="red", label="solution numerique")
            if initialisation == "carre":
                line2, = plt.plot(x, carre_analytique(dt), label="solution analytique")
            elif initialisation == "dirac":
                line2, = plt.plot(x, dirac_analytique(dt), label="solution analytique")

        else:
            u0_trace = [u0[15*i,0] for i in range(len(x)//15-1)]
#            line.set_ydata(u0[:,0])
            line.set_ydata(u0_trace)
            if initialisation == "carre":
                line2.set_ydata(carre_analytique(j*dt)[:,0])
            elif initialisation == "dirac":
                line2.set_ydata(dirac_analytique(j*dt)[:,0])

        plt.pause(0.01)
        plt.xlabel("x")
        plt.ylabel("u(x)")
        instant = int(dt * j * 10) / 10
        plt.title("Resolution de l'équation de Fokker-Plank \n t = "+str(instant)+" s")
        plt.legend()

        if mesure_erreur == True and j == Nt:
            val_erreur = erreur(u0[:,0],carre_analytique(dt*j)[:,0],Nx)

    print(val_erreur)

plt.show()

tab_dt = [2**(-i) for i in range(3,8)]
tab_valeur =[0.2200586273302155,0.09174098937465444,0.03948221060137927,0.01643585075612612,0.006610093036526104]
plt.plot(np.log(tab_dt),-1*np.log(tab_valeur),"o")

a = -regression_linéaire(np.log(tab_dt),np.log(tab_valeur))[0]
b = -regression_linéaire(np.log(tab_dt),np.log(tab_valeur))[1]
x2=np.linspace(2**(-7),2**(-3),100)
plt.plot(np.log(x2),a*np.log(x2)+b, label=str(a) + "x + " + str(b))
plt.title("Evolution de l'erreur")
print(a,b)
plt.legend()
plt.show()

Programme de résolution de l'équation de Fokker-Planck/Principal.py


import uuid

from django import http
from django.core.urlresolvers import reverse
from django.utils.datastructures import SortedDict

from mox import IsA

from openstack_dashboard import api
from openstack_dashboard.test import helpers as test

class InstanceViewTest(test.BaseAdminViewTests):
    @test.create_stubs({api.nova: ('flavor_list', 'server_list',),
                        api.keystone: ('tenant_list',)})
    def test_index(self):
        servers = self.servers.list()
        flavors = self.flavors.list()
        tenants = self.tenants.list()
        api.keystone.tenant_list(IsA(http.HttpRequest), admin=True).\
                                 AndReturn(tenants)
        api.nova.server_list(IsA(http.HttpRequest),
                             all_tenants=True).AndReturn(servers)
        api.nova.flavor_list(IsA(http.HttpRequest)).AndReturn(flavors)
        self.mox.ReplayAll()

        res = self.client.get(reverse('horizon:admin:instances:index'))
        self.assertTemplateUsed(res, 'admin/instances/index.html')
        instances = res.context['table'].data
        self.assertItemsEqual(instances, servers)

    @test.create_stubs({api.nova: ('flavor_list', 'flavor_get',
                                    'server_list',),
                        api.keystone: ('tenant_list',)})
    def test_index_flavor_list_exception(self):
        servers = self.servers.list()
        tenants = self.tenants.list()
        flavors = self.flavors.list()
        full_flavors = SortedDict([(f.id, f) for f in flavors])

        api.nova.server_list(IsA(http.HttpRequest),
                             all_tenants=True).AndReturn(servers)
        api.nova.flavor_list(IsA(http.HttpRequest)). \
                            AndRaise(self.exceptions.nova)
        api.keystone.tenant_list(IsA(http.HttpRequest), admin=True).\
                                 AndReturn(tenants)
        for server in servers:
            api.nova.flavor_get(IsA(http.HttpRequest), server.flavor["id"]). \
                                AndReturn(full_flavors[server.flavor["id"]])

        self.mox.ReplayAll()

        res = self.client.get(reverse('horizon:admin:instances:index'))
        self.assertTemplateUsed(res, 'admin/instances/index.html')
        instances = res.context['table'].data
        self.assertItemsEqual(instances, servers)

    @test.create_stubs({api.nova: ('flavor_list', 'flavor_get',
                                    'server_list',),
                        api.keystone: ('tenant_list',)})
    def test_index_flavor_get_exception(self):
        servers = self.servers.list()
        flavors = self.flavors.list()
        tenants = self.tenants.list()
        # UUIDs generated using indexes are unlikely to match
        # any of existing flavor ids and are guaranteed to be deterministic.
        for i, server in enumerate(servers):
            server.flavor['id'] = str(uuid.UUID(int=i))

        api.nova.server_list(IsA(http.HttpRequest),
                             all_tenants=True).AndReturn(servers)
        api.nova.flavor_list(IsA(http.HttpRequest)). \
                            AndReturn(flavors)
        api.keystone.tenant_list(IsA(http.HttpRequest), admin=True).\
                                 AndReturn(tenants)
        for server in servers:
            api.nova.flavor_get(IsA(http.HttpRequest), server.flavor["id"]). \
                                AndRaise(self.exceptions.nova)
        self.mox.ReplayAll()

        res = self.client.get(reverse('horizon:admin:instances:index'))
        instances = res.context['table'].data
        self.assertTemplateUsed(res, 'admin/instances/index.html')
        self.assertMessageCount(res, error=len(servers))
        self.assertItemsEqual(instances, servers)

    @test.create_stubs({api.nova: ('server_list',)})
    def test_index_server_list_exception(self):
        api.nova.server_list(IsA(http.HttpRequest),
                             all_tenants=True).AndRaise(self.exceptions.nova)

        self.mox.ReplayAll()

        res = self.client.get(reverse('horizon:admin:instances:index'))
        self.assertTemplateUsed(res, 'admin/instances/index.html')
        self.assertEqual(len(res.context['instances_table'].data), 0)

    @test.create_stubs({api.nova: ('server_get', 'flavor_get',),
                        api.keystone: ('tenant_get',)})
    def test_ajax_loading_instances(self):
        server = self.servers.first()
        flavor = self.flavors.list()[0]
        tenant = self.tenants.list()[0]

        api.nova.server_get(IsA(http.HttpRequest), server.id).AndReturn(server)
        api.nova.flavor_get(IsA(http.HttpRequest),
                            server.flavor['id']).AndReturn(flavor)
        api.keystone.tenant_get(IsA(http.HttpRequest),
                                server.tenant_id,
                                admin=True).AndReturn(tenant)
        self.mox.ReplayAll()

        url = reverse('horizon:admin:instances:index') + \
                "?action=row_update&table=instances&obj_id=" + server.id

        res = self.client.get(url, {},
                               HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        self.assertTemplateUsed(res, "horizon/common/_data_table_row.html")

        self.assertContains(res, "test_tenant", 1, 200)
        self.assertContains(res, "instance-host", 1, 200)
        self.assertContains(res, "server_1", 1, 200)
        self.assertContains(res, "10.0.0.1", 1, 200)
        self.assertContains(res, "512MB RAM | 1 VCPU | 0 Disk", 1, 200)
        self.assertContains(res, "Active", 1, 200)
        self.assertContains(res, "Running", 1, 200)

    @test.create_stubs({api.nova: ('flavor_list', 'server_list',),
                        api.keystone: ('tenant_list',)})
    def test_index_options_before_migrate(self):
        api.keystone.tenant_list(IsA(http.HttpRequest), admin=True).\
                                 AndReturn(self.tenants.list())
        api.nova.server_list(IsA(http.HttpRequest),
                             all_tenants=True).AndReturn(self.servers.list())
        api.nova.flavor_list(IsA(http.HttpRequest)).\
                             AndReturn(self.flavors.list())
        self.mox.ReplayAll()

        res = self.client.get(reverse('horizon:admin:instances:index'))
        self.assertContains(res, "instances__migrate")
        self.assertNotContains(res, "instances__confirm")
        self.assertNotContains(res, "instances__revert")

    @test.create_stubs({api.nova: ('flavor_list', 'server_list',),
                        api.keystone: ('tenant_list',)})
    def test_index_options_after_migrate(self):
        server = self.servers.first()
        server.status = "VERIFY_RESIZE"
        api.keystone.tenant_list(IsA(http.HttpRequest), admin=True).\
                                 AndReturn(self.tenants.list())
        api.nova.server_list(IsA(http.HttpRequest),
                             all_tenants=True).AndReturn(self.servers.list())
        api.nova.flavor_list(IsA(http.HttpRequest)).\
                             AndReturn(self.flavors.list())
        self.mox.ReplayAll()

        res = self.client.get(reverse('horizon:admin:instances:index'))
        self.assertContains(res, "instances__confirm")
        self.assertContains(res, "instances__revert")
        self.assertNotContains(res, "instances__migrate")


import dash
from covid19_plotlyfunctions import *
import matplotlib as plt
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import pandas as pd

fig1 = NationalDailyDeaths()
fig2 = NationalDailyIcuAdmissions()
fig3 = GenderDifference1()
fig4 = CasesVsDeaths()
fig5 = GenderDifference2()
fig6 = GenderDifference3()

apps = dash.Dash(
    external_stylesheets=[dbc.themes.BOOTSTRAP]
)

title = {
    'display':'flex',
    'justify-content':'center',
    'font-size':'100px',
    'font-family':'impact'
}

center = {
    'display':'flex',
    'justify-content':'center',
}

apps.layout = row = html.Div(
    [
        html.Div(
            dbc.Row(
            [
                dbc.Col(html.Div("COVID-19 VIZUALIZER")),
            ],
        ),
        style=title
        ),

        html.H4('Conrad Fogdestam TE19D', style=center),
        html.Br(),

        html.Div(
            dbc.Row(
            [
                dbc.Col(dcc.Graph(id='NationalDailyDeaths', figure=fig1),
                ),
                dbc.Col(dcc.Graph(id='NationalDailyIcuAdmissions', figure=fig2),
                ),
            ],
        ),
        style=center
        ),
        html.Br(),

        html.H5('Deathrate By COVID-19 (%)', style=center),

        dbc.Row(
            [
                dbc.Col(dcc.Graph(id='DeathRate', figure=fig4),
                ),
            ],

        ),
        html.Br(),
        dbc.Row(
            [
                dbc.Col(dcc.Graph(id='GenderDifferences1', figure=fig3),
                ),
                dbc.Col(dcc.Graph(id='GenderDifferences2', figure=fig6),
                ),
                dbc.Col(dcc.Graph(id='GenderDifferences3', figure=fig6),
                ),

            ]
        ),
        html.Br(),
        html.Br(),
        html.Br(),
        html.Br(),
        html.Div('© 2011-2021 MICRODOSINGLLC™. All Rights Reserved', style=center),
        html.Br(),
        html.Br(),
        html.Br(),
    ],

)

if __name__ == "__main__":
    apps.run_server(debug=True)


import sys
sys.path.append('..')
from absl import app, flags
from absl.flags import FLAGS
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
from lab01.fileLoader import wczytaj_baze_probek_z_tekstem
from lab02.chart import Chart
from convert import probki_str_na_liczby
from fuzzy_kmeans import fuzzy_kmeans

flags.DEFINE_integer('groups', 3, '')
flags.DEFINE_integer('iterations', 100, '')
flags.DEFINE_float('fcm', 1.5, '')

chart = Chart()
progress = []
samples = []

def Animate(frame_idx):
  groups = progress[frame_idx]['groups']
  centers = progress[frame_idx]['centers']
  chart.clear()

  # Assign samples to closest groups
  samples_groups = [[] for _ in range(len(groups))]
  for sample_idx in range(len(samples)):
    largest_participation_idx = None
    largest_participation_value = -1
    for group_idx in range(len(groups)):
      if groups[group_idx, sample_idx] > largest_participation_value:
        largest_participation_value = groups[group_idx, sample_idx]
        largest_participation_idx = group_idx

    samples_groups[largest_participation_idx].append(samples[sample_idx])

  for i in range(len(samples_groups)):
    if len(samples_groups[i]) == 0:
      continue

    samples_group_np = np.array(samples_groups[i])
    chart.wykres_punkty_rysuj(samples_group_np[:,0], samples_group_np[:,1], 'group {}'.format(i+1))

  chart.wykres_punkty_rysuj(centers[:,0], centers[:,1], 'centers')

  plt.legend(loc='upper right')
  plt.title('Iteration {}'.format(frame_idx+1))

def main(_argv):
  probki_string, nazwy_atr, czy_atr_symb = wczytaj_baze_probek_z_tekstem('spirala.txt', 'spirala-type.txt')

  global samples
  samples = probki_str_na_liczby(probki_string, (0,1))
  fuzzy_kmeans(samples, FLAGS.groups, FLAGS.iterations, FLAGS.fcm, progress)

  fig = plt.figure(1)
  anim = animation.FuncAnimation(fig, Animate, frames=len(progress), repeat=False, interval=500)
  chart.show()

if __name__ == '__main__':
  app.run(main)


#!/usr/local/bin/python3.7
import asyncio
from music import MusicCore

async def main():
    c = MusicCore()
    await c.queue(sys.argv[1:])
    await c.process_queue()

if __name__ == "__main__":
    import sys
    if (sys.argv is None) or (len(sys.argv) <= 1):
        print("Can't load program without at least one (1) URL")
        exit()
    asyncio.run(main())
else:
    print("This script is a program, not a module")


"""Utilities for loading data."""
import os

import pandas as pd
import timezonefinder as tzf

def load_all_data():
    """Get all available WN flight data"""
    # Get the directory of the current file
    script_directory = os.path.dirname(os.path.abspath(__file__))

    # Construct the relative path to the CSV file
    nominal_file_path = os.path.join(
        script_directory, "../..", "data", "wn_dec01_dec20.csv"
    )
    disrupted_file_path = os.path.join(
        script_directory, "../..", "data", "wn_dec21_dec30.csv"
    )
    airport_locations_file_path = os.path.join(
        script_directory, "../..", "data", "airport_locations.csv"
    )

    # Read the CSV files into a DataFrame
    nominal_df = pd.read_csv(nominal_file_path)
    disrupted_df = pd.read_csv(disrupted_file_path)
    airport_locations_df = pd.read_csv(airport_locations_file_path)

    # Filter airport locations to only keep the latest
    airport_locations_df = airport_locations_df[
        airport_locations_df.AIRPORT_IS_LATEST.astype(bool)
    ]

    # Concatenate the two DataFrames
    df = pd.concat([nominal_df, disrupted_df])

    return df, airport_locations_df

def split_nominal_disrupted_data(df: pd.DataFrame):
    """Split dataset into nominal data and disrupted data.

    The disruption occurred between 2022-12-21 and 2023-1-1

    Args:
        df: the dataframe of flight data

    Returns:
        A dataframe filtered to include only flights outside the disrupted period
        A dataframe filtered to include flights within the disrupted period
    """
    # Filter rows based on the date condition
    disrupted_start = pd.to_datetime("12/21/2022")
    disrupted_end = pd.to_datetime("12/30/2022")

    # Filter rows based on the date condition
    nominal_data = df[(df["date"] < disrupted_start) | (df["date"] > disrupted_end)]
    disrupted_data = df[(df["date"] >= disrupted_start) & (df["date"] <= disrupted_end)]

    return nominal_data, disrupted_data

def split_by_date(df: pd.DataFrame):
    """Split a DataFrame of flights into a list of DataFrames, one for each date.

    Args:
        df: the dataframe of flight data with a "date" column

    Returns:
        A list of DataFrames, each containing data for a specific date
    """
    # Group the DataFrame by the "date" column
    grouped_df = df.groupby("date")

    # Create a list of DataFrames, one for each date
    date_dataframes = [group for _, group in grouped_df]

    # Sort within each date by scheduled departure time
    for date_df in date_dataframes:
        date_df.sort_values(by="scheduled_departure_time", inplace=True)

    return date_dataframes

def convert_to_float_hours_optimized(time_series, time_zone_series):
    """Convert time in 24-hour format to float hours since midnight.

    Args:
        time_series: a pandas Series representing time in 24-hour format (HH:MM)
        time_zone_series: a pandas Series representing the time zone of each time

    Returns:
        Float hours since midnight, or None for canceled flights
    """
    # Replace "--:--" with "23:59" (delay cancelled flights to end of day)
    time_series.replace("--:--", "23:59", inplace=True)

    # Replace "24:00" with "23:59" (midnight)
    time_series.replace("24:00", "23:59", inplace=True)

    # Convert time strings to datetime objects
    time_objects = pd.to_datetime(time_series, format="%H:%M")

    # Convert time objects to UTC
    combined_df = pd.concat([time_objects, time_zone_series], axis=1)
    time_objects = combined_df.apply(
        lambda row: row.iloc[0].tz_localize(row.iloc[1]).tz_convert("UTC"), axis=1
    )

    # Extract hour and minute components
    hours_since_midnight = time_objects.dt.hour + time_objects.dt.minute / 60.0

    return hours_since_midnight

def time_zone_for_airports(airport_codes, airport_locations_df):
    """Get the time zone for each airport.

    Args:
        airport_codes: a list of airport codes
        airport_locations_df: a dataframe containing airport locations

    Returns:
        A list of time zones corresponding to each airport
    """
    # Get the time zone for each airport
    finder = tzf.TimezoneFinder()
    time_zones = []
    for airport_code in airport_codes:
        latitude = airport_locations_df[
            airport_locations_df.AIRPORT == airport_code
        ].LATITUDE.iat[0]
        longitude = airport_locations_df[
            airport_locations_df.AIRPORT == airport_code
        ].LONGITUDE.iat[0]
        time_zones.append(finder.timezone_at(lng=longitude, lat=latitude))

    return time_zones

def remap_columns(df, airport_locations_df):
    """Remap columns in the DataFrame to the names that we expect.

    Args:
        df: the original dataframe
        airport_locations_df: a dataframe containing airport locations

    Returns:
        A new dataframe with remapped columns
    """
    # Define the mapping
    column_mapping = {
        "Flight Number": "flight_number",
        "Date": "date",
        "Origin Airport Code": "origin_airport",
        "Dest Airport Code": "destination_airport",
        "Scheduled Departure Time": "scheduled_departure_time",
        "Scheduled Arrival Time": "scheduled_arrival_time",
        "Actual Departure Time": "actual_departure_time",
        "Actual Arrival Time": "actual_arrival_time",
    }

    # Filter the original DataFrame based on the desired columns
    remapped_df = df[column_mapping.keys()]

    # Rename the columns based on the mapping
    remapped_df = remapped_df.rename(columns=column_mapping)

    # Get a list of airport time zones
    airport_codes = pd.concat(
        [
            remapped_df["origin_airport"],
            remapped_df["destination_airport"],
        ]
    ).unique()
    airport_time_zones = pd.DataFrame(
        {
            "airport_code": airport_codes,
            "time_zone": time_zone_for_airports(airport_codes, airport_locations_df),
        }
    )

    # Add a column for time zones to remapped_df using a merge
    remapped_df = remapped_df.merge(
        airport_time_zones,
        left_on="origin_airport",
        right_on="airport_code",
    )
    remapped_df = remapped_df.rename(columns={"time_zone": "origin_time_zone"})
    remapped_df = remapped_df.merge(
        airport_time_zones,
        left_on="destination_airport",
        right_on="airport_code",
    )
    remapped_df = remapped_df.rename(columns={"time_zone": "destination_time_zone"})

    # Convert all times to hours since midnight
    remapped_df["scheduled_departure_time"] = convert_to_float_hours_optimized(
        remapped_df["scheduled_departure_time"],
        remapped_df["origin_time_zone"],
    )
    remapped_df["scheduled_arrival_time"] = convert_to_float_hours_optimized(
        remapped_df["scheduled_arrival_time"],
        remapped_df["destination_time_zone"],
    )
    remapped_df["actual_departure_time"] = convert_to_float_hours_optimized(
        remapped_df["actual_departure_time"],
        remapped_df["origin_time_zone"],
    )
    remapped_df["actual_arrival_time"] = convert_to_float_hours_optimized(
        remapped_df["actual_arrival_time"],
        remapped_df["destination_time_zone"],
    )

    # If a flight is en-route at midnight, it's duration will be negative unless we add 24 hours
    # to the actual and scheduled arrival times
    scheduled_duration = (
        remapped_df.scheduled_arrival_time - remapped_df.scheduled_departure_time
    )
    actual_duration = (
        remapped_df.actual_arrival_time - remapped_df.actual_departure_time
    )
    remapped_df.loc[
        actual_duration < 0, "actual_arrival_time"
    ] += 24  # Add 24 hours to actual arrival time
    remapped_df.loc[
        scheduled_duration < 0, "scheduled_arrival_time"
    ] += 24  # Add 24 hours to scheduled arrival time

    # If a flight is delayed so that both the actual departure is the next day,
    # we need to add 24 hours to the actual departure and arrival times
    departure_delay = (
        remapped_df.actual_departure_time - remapped_df.scheduled_departure_time
    )
    # Departure delay will be positive for flights that depart late within the same day,
    # slightly negative for flights that depart early within the same day, and very
    # negative for flights that depart late the next day
    remapped_df.loc[
        departure_delay < -3.0,
        "actual_departure_time",
    ] += 24  # Add 24 hours to actual departure time
    remapped_df.loc[
        departure_delay < -3.0,
        "actual_arrival_time",
    ] += 24  # Add 24 hours to actual arrival time

    # Convert date to datetime type
    remapped_df["date"] = pd.to_datetime(remapped_df["date"])

    return remapped_df

def top_N_df(df, number_of_airports: int):
    """
    Get the top N airports by arrivals and filter the dataframe to include only
    flights between those airports.

    Args:
        df: the original dataframe
        number_of_airports: the number of airports to include
    """
    # Get the top-N airports by arrivals
    top_N_airports = (
        df["destination_airport"].value_counts().head(number_of_airports).index
    )

    # Filter the original DataFrame based on the desired airports
    filtered_df = df[
        df["origin_airport"].isin(top_N_airports)
        & df["destination_airport"].isin(top_N_airports)
    ]

    return filtered_df

if __name__ == "__main__":
    import matplotlib.pyplot as plt

    # Load data, filter, and split by date
    df, airport_locations_df = load_all_data()
    df = remap_columns(df, airport_locations_df)
    filtered_df = top_N_df(df, 6)
    nominal_df, disrupted_df = split_nominal_disrupted_data(filtered_df)
    nominal_dfs, disrupted_dfs = split_by_date(nominal_df), split_by_date(disrupted_df)

    # Save remapped data to file
    script_directory = os.path.dirname(os.path.abspath(__file__))
    df.to_pickle(os.path.join(script_directory, "../..", "data", "wn_data_clean.csv"))

    # Plot a histogram of the total number of flights between top-N airports
    N_range = [2, 3, 4, 5, 6, 7, 8, 9, 10]
    num_flights = []
    for top_N in N_range:
        filtered_df = top_N_df(df, top_N)
        num_flights.append(len(filtered_df))

    plt.plot(N_range, num_flights, "o-")
    plt.xlabel("Number of airports kept in dataset")
    plt.ylabel("Total number of flights")
    plt.show()