"""Record for out of band invitations."""

import json
from typing import Any, Mapping, Optional, Union

from marshmallow import fields, validate

from .....connections.models.conn_record import ConnRecord
from .....core.profile import ProfileSession
from .....messaging.decorators.service_decorator import (
    ServiceDecorator,
    ServiceDecoratorSchema,
)
from .....messaging.models.base_record import BaseExchangeRecord, BaseExchangeSchema
from .....messaging.valid import UUID4_EXAMPLE
from .....storage.base import BaseStorage
from .....storage.error import StorageNotFoundError
from .....storage.record import StorageRecord
from ..messages.invitation import InvitationMessage, InvitationMessageSchema

class OobRecord(BaseExchangeRecord):
    """Represents an out of band record."""

    class Meta:
        """OobRecord metadata."""

        schema_class = "OobRecordSchema"

    RECORD_TYPE = "oob_record"
    RECORD_TYPE_METADATA = ConnRecord.RECORD_TYPE_METADATA
    RECORD_ID_NAME = "oob_id"
    RECORD_TOPIC = "out_of_band"
    TAG_NAMES = {
        "invi_msg_id",
        "attach_thread_id",
        "our_recipient_key",
        "connection_id",
        "reuse_msg_id",
    }

    STATE_INITIAL = "initial"
    STATE_PREPARE_RESPONSE = "prepare-response"
    STATE_AWAIT_RESPONSE = "await-response"
    STATE_NOT_ACCEPTED = "reuse-not-accepted"
    STATE_ACCEPTED = "reuse-accepted"
    STATE_DONE = "done"

    ROLE_SENDER = "sender"
    ROLE_RECEIVER = "receiver"

    def __init__(
        self,
        *,
        state: str,
        invi_msg_id: str,
        role: str,
        invitation: Union[InvitationMessage, Mapping[str, Any]],
        their_service: Optional[ServiceDecorator] = None,
        connection_id: Optional[str] = None,
        reuse_msg_id: Optional[str] = None,
        oob_id: Optional[str] = None,
        attach_thread_id: Optional[str] = None,
        our_recipient_key: Optional[str] = None,
        our_service: Optional[ServiceDecorator] = None,
        multi_use: bool = False,
        trace: bool = False,
        **kwargs,
    ):
        """Initialize a new OobRecord."""
        super().__init__(oob_id, state, trace=trace, **kwargs)
        self._id = oob_id
        self.state = state
        self.invi_msg_id = invi_msg_id
        self.role = role
        self._invitation = InvitationMessage.serde(invitation)
        self.connection_id = connection_id
        self.reuse_msg_id = reuse_msg_id
        self.their_service = their_service
        self.our_service = our_service
        self.attach_thread_id = attach_thread_id
        self.our_recipient_key = our_recipient_key
        self.multi_use = multi_use
        self.trace = trace

    @property
    def oob_id(self) -> str:
        """Accessor for the ID associated with this exchange."""
        return self._id

    @property
    def invitation(self) -> InvitationMessage:
        """Accessor; get deserialized view."""
        return None if self._invitation is None else self._invitation.de

    @invitation.setter
    def invitation(self, value):
        """Setter; store de/serialized views."""
        self._invitation = InvitationMessage.serde(value)

    @property
    def record_value(self) -> dict:
        """Accessor for the JSON record value generated for this invitation."""
        return {
            **{
                prop: getattr(self, prop)
                for prop in (
                    "state",
                    "their_service",
                    "connection_id",
                    "role",
                    "our_service",
                    "invi_msg_id",
                )
            },
            **{
                prop: getattr(self, f"_{prop}").ser
                for prop in ("invitation",)
                if getattr(self, prop) is not None
            },
        }

    async def delete_record(self, session: ProfileSession):
        """Perform connection record deletion actions.

        Args:
            session (ProfileSession): session

        """
        await super().delete_record(session)

        # Delete metadata
        if self.connection_id:
            storage = session.inject(BaseStorage)
            await storage.delete_all_records(
                self.RECORD_TYPE_METADATA,
                {"connection_id": self.connection_id},
            )

    async def metadata_get(
        self, session: ProfileSession, key: str, default: Any = None
    ) -> Any:
        """Retrieve arbitrary metadata associated with this connection.

        Args:
            session (ProfileSession): session used for storage
            key (str): key identifying metadata
            default (Any): default value to get; type should be a JSON
                compatible value.

        Returns:
            Any: metadata stored by key

        """
        assert self.connection_id
        storage: BaseStorage = session.inject(BaseStorage)
        try:
            record = await storage.find_record(
                self.RECORD_TYPE_METADATA,
                {"key": key, "connection_id": self.connection_id},
            )
            return json.loads(record.value)
        except StorageNotFoundError:
            return default

    async def metadata_set(self, session: ProfileSession, key: str, value: Any):
        """Set arbitrary metadata associated with this connection.

        Args:
            session (ProfileSession): session used for storage
            key (str): key identifying metadata
            value (Any): value to set
        """
        assert self.connection_id
        value = json.dumps(value)
        storage: BaseStorage = session.inject(BaseStorage)
        try:
            record = await storage.find_record(
                self.RECORD_TYPE_METADATA,
                {"key": key, "connection_id": self.connection_id},
            )
            await storage.update_record(record, value, record.tags)
        except StorageNotFoundError:
            record = StorageRecord(
                self.RECORD_TYPE_METADATA,
                value,
                {"key": key, "connection_id": self.connection_id},
            )
            await storage.add_record(record)

    async def metadata_delete(self, session: ProfileSession, key: str):
        """Delete custom metadata associated with this connection.

        Args:
            session (ProfileSession): session used for storage
            key (str): key of metadata to delete
        """
        assert self.connection_id
        storage: BaseStorage = session.inject(BaseStorage)
        try:
            record = await storage.find_record(
                self.RECORD_TYPE_METADATA,
                {"key": key, "connection_id": self.connection_id},
            )
            await storage.delete_record(record)
        except StorageNotFoundError as err:
            raise KeyError(f"{key} not found in connection metadata") from err

    async def metadata_get_all(self, session: ProfileSession) -> dict:
        """Return all custom metadata associated with this connection.

        Args:
            session (ProfileSession): session used for storage

        Returns:
            dict: dictionary representation of all metadata values

        """
        assert self.connection_id
        storage: BaseStorage = session.inject(BaseStorage)
        records = await storage.find_all_records(
            self.RECORD_TYPE_METADATA,
            {"connection_id": self.connection_id},
        )
        return {record.tags["key"]: json.loads(record.value) for record in records}

    def __eq__(self, other: Any) -> bool:
        """Comparison between records."""
        return super().__eq__(other)

class OobRecordSchema(BaseExchangeSchema):
    """Schema to allow serialization/deserialization of invitation records."""

    class Meta:
        """OobRecordSchema metadata."""

        model_class = OobRecord

    oob_id = fields.Str(
        required=True,
        metadata={"description": "Oob record identifier", "example": UUID4_EXAMPLE},
    )
    state = fields.Str(
        required=True,
        validate=validate.OneOf(
            OobRecord.get_attributes_by_prefix("STATE_", walk_mro=True)
        ),
        metadata={
            "description": "Out of band message exchange state",
            "example": OobRecord.STATE_AWAIT_RESPONSE,
        },
    )
    invi_msg_id = fields.Str(
        required=True,
        metadata={
            "description": "Invitation message identifier",
            "example": UUID4_EXAMPLE,
        },
    )
    invitation = fields.Nested(
        InvitationMessageSchema(),
        required=True,
        metadata={"description": "Out of band invitation message"},
    )

    their_service = fields.Nested(ServiceDecoratorSchema(), required=False)

    connection_id = fields.Str(
        required=False,
        metadata={
            "description": "Connection record identifier",
            "example": UUID4_EXAMPLE,
        },
    )

    attach_thread_id = fields.Str(
        required=False,
        metadata={
            "description": "Connection record identifier",
            "example": UUID4_EXAMPLE,
        },
    )

    our_recipient_key = fields.Str(
        required=False,
        metadata={
            "description": "Recipient key used for oob invitation",
            "example": UUID4_EXAMPLE,
        },
    )

    role = fields.Str(
        required=False,
        validate=validate.OneOf(
            OobRecord.get_attributes_by_prefix("ROLE_", walk_mro=False)
        ),
        metadata={"description": "OOB Role", "example": OobRecord.ROLE_RECEIVER},
    )


import matplotlib.pyplot as plt
import numpy as np

from neat_algoritm.evolving import evolve
from neat_algoritm.genome import Genome

def evolve_xor():
    n_features = 2

    n_actions = 1  # Either 0 or 1, such that we don't have to do thresholding

    innovation_number = n_features * n_actions + 1
    representatives = []

    xor_input = [
        [0, 0],
        [0, 1],
        [1, 0],
        [1, 1],
    ]

    xor_output = [
        0,
        1,
        1,
        0,
    ]

    # xor_output = [
    #     1,
    #     1,
    #     1,
    #     1,
    # ]

    population = [Genome() for _ in range(population_size)]
    for genome in population:
        genome.initialize(n_features, n_actions)
        innovation_number += 2
        genome.add_node(innovation_number)

    epoch_fitnesses = []
    epoch_activations = []

    for _ in range(n_epochs):
        best_fitness = 0

        # calculate fitness
        print('calculating fitness')

        # buffer for storing fitness per genome
        genome_fitnesses = []
        genome_activations = []

        for i, genome in enumerate(population):
            # Run XOR inputs
            fitness = 1

            # rebuild the number of nodes etc
            genome.reset_activations()
            genome_activation = []

            for X, y in zip(xor_input, xor_output):
                # Evaluate the current input and perform the best action
                output = genome.evaluate_input(X, steps=2)[0]
                genome_activation.append(output)

                mean_squared = False
                if mean_squared:
                    # this fitness still crashes
                    fitness += 1 - (y - output) * (y - output)
                    # fitness += 1 - abs((y - output))

                else:
                    y_pred = output > 0.5
                    if y_pred == y:
                        fitness += 1

                genome.reset_activations()

            genome_fitnesses.append(fitness)  # fitness
            genome_activations.append(np.array(genome_activation))
            if fitness > best_fitness:
                best_fitness = fitness

            genome.fitness_number = fitness

        epoch_fitnesses.append(np.array(genome_fitnesses))
        epoch_activations.append(np.array(genome_activations))

        print('crossing and mutating genes')
        population, representatives, innovation_number = evolve(
            population,
            representatives,
            innovation_number,
            delta_t=1.5
        )

        print(len(population))
        print('generation ', _)
        print("Best Epoch Fitness:", best_fitness)

    epoch_fitnesses = np.array(epoch_fitnesses)

    plt.figure(1)
    print(epoch_fitnesses)
    for i in range(epoch_fitnesses.shape[1]):
        plt.plot(epoch_fitnesses[:, i] - 1, label="specie {}".format(i))

    plt.title("Fitness per epoch per specie")
    plt.xlabel("epoch")
    plt.ylabel("fitness")
    plt.legend()
    # plt.show()

    plt.figure(2)

    epoch_activations = np.array(epoch_activations)
    for specie in range(epoch_activations.shape[1]):
        average_activation = 0
        for datapoint in range(epoch_activations.shape[2]):
            average_activation += epoch_activations[:, specie, datapoint]

            plt.plot(epoch_activations[:, specie, datapoint], label="specie{}_{}".format(specie, xor_input[datapoint]))
        # plt.plot(average_activation / 4, label="specie{}_".format(specie))

    plt.title("Activation per epoch per specie per XOR input")
    plt.xlabel("epoch")
    plt.ylabel("activation")
    plt.legend()
    plt.show()

    gs = sorted(population, key=lambda g: g.fitness_number, )

    return gs

if __name__ == "__main__":
    population_size = 100
    n_epochs = 100
    best_genome = evolve_xor()


# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""
sbalance=int(50000)
cbalance=int(600000)
toal=cbalance+sbalance
print('1. Withdrwal ')
print('2. Enquiry aamount ')
print('3. Deposit')
def withdrwal(sbalance,cbalance):
    print('Welcomr ot cash withdrwal menu: ');
    print('1. for saving account ')
    print('2. for Current account ')
    choice=int(input("ENter the choice: "))
    amt=int(input("Amount you want: " ))
    if choice==1:
        sbalance-=amt
        print("Thankyou for using our service: ")
        print("balance amount is: ",sbalance)
    elif choice==2:
        cbalance-=amt
        print("Balance amount is: ",cbalance)
        print("Thankyou for using our service: ")
    else:
        print("Not valid choice")
def beqr():
    print("Amount in account is: ",toal)
def depositt(toal):
    print("Enter the amount to be deposited: ")
    depo=int(input("Enter the amount: "))
    toal+=depo
    print("deposited amount is {} and total amount is {}".format(depo,toal ))

choicee=int(input("ENter your choice: "))
if choicee==1:
    withdrwal(sbalance,cbalance)
elif choicee==2:
    beqr()
elif choicee==3:
    depositt(toal)
else:
    print("Enter valid value: ")


"""
This module contains StructuredLogEvent and EventSummary classes.
"""

from collections import defaultdict
import json
import logging
import os
import sys
from datetime import datetime
from pathlib import Path

import pandas as pd
from prettytable import PrettyTable

from jade.common import JOBS_OUTPUT_DIR, EVENTS_DIR
from jade.exceptions import InvalidConfiguration
from jade.utils.utils import dump_data, load_data

EVENTS_FILENAME = "events.json"

EVENT_CATEGORY_ERROR = "Error"
EVENT_CATEGORY_HPC = "HPC"
EVENT_CATEGORY_RESOURCE_UTIL = "ResourceUtilization"

EVENT_NAME_HPC_SUBMIT = "hpc_submit"
EVENT_NAME_HPC_JOB_ASSIGNED = "hpc_job_assigned"
EVENT_NAME_HPC_JOB_STATE_CHANGE = "hpc_job_state_change"
EVENT_NAME_CPU_STATS = "cpu_stats"
EVENT_NAME_DISK_STATS = "disk_stats"
EVENT_NAME_MEMORY_STATS = "mem_stats"
EVENT_NAME_NETWORK_STATS = "net_stats"
EVENT_NAME_PROCESS_STATS = "process_stats"
EVENT_NAME_BYTES_CONSUMED = "bytes_consumed"
EVENT_NAME_UNHANDLED_ERROR = "unhandled_error"
EVENT_NAME_ERROR_LOG = "log_error"
EVENT_NAME_SUBMIT_STARTED = "submit_started"
EVENT_NAME_SUBMIT_COMPLETED = "submit_completed"
EVENT_NAME_CONFIG_EXEC_SUMMARY = "config_exec_summary"

logger = logging.getLogger(__name__)

class StructuredLogEvent:
    """
    A class for recording structured log events.
    """

    def __init__(self, source, category, name, message, **kwargs):
        """
        Initialize the class

        Parameters
        ----------
        source: str,
            The source of the event.
        category: str,
            An event category given by the user.
        name: str,
            An event name given by the user.
        message:
            An event message given the user.

        kwargs:
            Other information that the user needs to record into event.
        """
        self.source = source
        self.category = category
        self.name = name
        self.message = message
        self.event_class = self.__class__.__name__

        if "timestamp" in kwargs:
            self.timestamp = kwargs.pop("timestamp")
        else:
            self.timestamp = str(datetime.now())

        self.data = kwargs

    def base_field_names(self):
        """Return the base field names for the event.

        Returns
        -------
        list

        """
        return self._base_field_names()

    @staticmethod
    def _base_field_names():
        return ["timestamp", "source", "message"]

    def field_names(self):
        """Return all field names for the event.

        Returns
        -------
        list

        """
        return self.base_field_names() + list(self.data.keys())

    def values(self):
        """Return the values for all fields in the event.

        Returns
        -------
        list

        """
        # Account for events generated with different versions of code.
        values = [getattr(self, x, "") for x in self.base_field_names()]
        values += [self.data.get(x, "") for x in self.data]
        return values

    @classmethod
    def deserialize(cls, record):
        """Deserialize event from JSON.

        Parameters
        ----------
        record : dict

        Returns
        -------
        StructuredLogEvent

        """
        return cls(
            source=record.get("source", ""),
            category=record.get("category", ""),
            name=record.get("name", ""),
            message=record.get("message", ""),
            timestamp=record.get("timestamp", ""),
            **record["data"],
        )

    def __str__(self):
        """To format a event instance to string"""
        return json.dumps(self.__dict__, sort_keys=True)

    def to_dict(self):
        """Convert event object to dict"""
        return self.__dict__

class StructuredErrorLogEvent(StructuredLogEvent):
    """Event specific to exceptions"""

    def __init__(self, source, category, name, message, **kwargs):
        """Must be called in an exception context."""
        super().__init__(source, category, name, message, **kwargs)
        if "exception" not in kwargs:
            self._parse_traceback()

    def base_field_names(self):
        return self._base_field_names()

    def _parse_traceback(self):
        """
        Parse the system exception information - exception, filename, and lineno.
        """
        exc_type, exc_obj, tb = sys.exc_info()
        assert tb is not None, "must be called in an exception context"

        self.data["exception"] = str(exc_type)
        self.data["error"] = str(exc_obj).strip()
        self.data["filename"] = os.path.basename(tb.tb_frame.f_code.co_filename)
        self.data["lineno"] = tb.tb_lineno

def deserialize_event(data):
    """Construct an event from raw  data.

    Parameters
    ----------
    data : dict

    Returns
    -------
    StructuredLogEvent

    """
    if data["event_class"] == "StructuredLogEvent":
        return StructuredLogEvent.deserialize(data)
    if data["event_class"] == "StructuredErrorLogEvent":
        return StructuredErrorLogEvent.deserialize(data)

    raise Exception(f"unknown event class {data['event_class']}")

class EventsSummary:
    """Provides summary of all events."""

    RESOURCE_STATS = set(
        (
            EVENT_NAME_CPU_STATS,
            EVENT_NAME_DISK_STATS,
            EVENT_NAME_MEMORY_STATS,
            EVENT_NAME_NETWORK_STATS,
            EVENT_NAME_PROCESS_STATS,
        )
    )

    def __init__(self, output_dir, preload=False, optimize_resoure_stats=True):
        """
        Initialize EventsSummary class

        Parameters
        ----------
        output_dir: str
            Path of jade output directory.
        preload: bool
            Load all events into memory; otherwise, load by name on demand.

        """
        self._optimize_resource_stats = optimize_resoure_stats
        self._events = defaultdict(list)
        self._output_dir = output_dir
        self._event_dir = Path(output_dir) / EVENTS_DIR
        self._event_dir.mkdir(exist_ok=True)
        self._job_outputs_dir = os.path.join(output_dir, JOBS_OUTPUT_DIR)
        event_files = list(self._event_dir.iterdir())
        if not event_files:
            self._consolidate_events()
            self._save_events_summary()
        elif preload:
            self._load_all_events()
        # else, events have already been consolidated, load them on demand

    def _iter_event_files(self):
        return Path(self._output_dir).glob("*events.log")

    def _consolidate_events(self):
        """Find most recent event log files, and merge event data together."""
        # PERF: This could easily be parallelized across processes. Need to be sure we aren't on
        # a login node.
        for event_file in self._iter_event_files():
            with open(event_file, "r") as f:
                for line in f:
                    record = json.loads(line)
                    event = deserialize_event(record)
                    self._events[event.name].append(event)

        for name in self._events.keys():
            self._events[name].sort(key=lambda x: x.timestamp)

    def _deserialize_events(self, name, path):
        self._events[name] = [deserialize_event(x) for x in load_data(path)]

    def _get_events(self, name):
        if name not in self._events:
            self._load_event_file(name)
        return self._events.get(name, [])

    def _load_all_events(self):
        for filename in self._event_dir.iterdir():
            name = filename.stem
            if name not in self._events and name not in self.RESOURCE_STATS:
                self._deserialize_events(name, filename)

    def _load_event_file(self, name):
        if name in self.RESOURCE_STATS:
            raise Exception(f"event {name} is only available in Parquet")

        filename = self._make_event_filename(name)
        if filename.exists():
            self._deserialize_events(name, filename)

    def _make_data_filename(self, name):
        return self._event_dir / (name + ".parquet")

    def _make_event_filename(self, name):
        return self._event_dir / (name + ".json")

    def _save_events_summary(self):
        """Save events to one file per event name."""
        for name, events in self._events.items():
            if name in self.RESOURCE_STATS:
                dict_events = []
                for event in events:
                    data = {"timestamp": event.timestamp, "source": event.source}
                    if name == EVENT_NAME_PROCESS_STATS:
                        for process in event.data["processes"]:
                            _data = {"timestamp": event.timestamp, "source": event.source}
                            _data.update(process)
                            dict_events.append(_data)
                    else:
                        data.update(event.data)
                        dict_events.append(data)
                df = pd.DataFrame.from_records(dict_events, index="timestamp")
                filename = self._make_data_filename(name)
                df.to_parquet(filename)
            else:
                dict_events = [event.to_dict() for event in events]
                filename = self._make_event_filename(name)
                dump_data(dict_events, filename)

        for name in self.RESOURCE_STATS:
            self._events.pop(name, None)

    def get_bytes_consumed(self):
        """Return a sum of all bytes_consumed events.

        Returns
        -------
        int
            Size in bytes of files produced by all jobs

        """
        total = 0
        for event in self.iter_events(EVENT_NAME_BYTES_CONSUMED):
            total += event.data["bytes_consumed"]

        return total

    def get_config_exec_time(self):
        """Return the total number of seconds to run all jobs in the config.

        Returns
        -------
        int

        """
        events = self.list_events(EVENT_NAME_CONFIG_EXEC_SUMMARY)
        if not events:
            raise InvalidConfiguration("no batch summary events found")

        return events[0].data["config_execution_time"]

    def get_dataframe(self, name):
        """Return the dataframe for this event name. Only applicable to resource stats."""
        filename = self._make_data_filename(name)
        if not filename.exists():
            return pd.DataFrame()
        return pd.read_parquet(filename)

    def iter_events(self, name):
        """Return a generator over events with name.

        Parameters
        ----------
        name : str

        Yields
        ------
        event : StructuredLogEvent

        """
        for event in self._get_events(name):
            yield event

    def list_events(self, name):
        """Return the events of type name.

        Returns
        -------
        list
            list of StructuredLogEvent

        """
        return list(self._get_events(name))

    def list_unique_categories(self):
        """Return the unique event categories in the log. Will cause all events
        to get loaded into memory.

        Returns
        -------
        list

        """
        self._load_all_events()
        categories = set()
        for events in self._events.values():
            if not events:
                continue
            categories.add(events[0].category)

        categories = list(categories)
        categories.sort()
        return categories

    def list_unique_names(self):
        """Return the unique event names in the log.

        Returns
        -------
        list

        """
        return [x.name for x in self._event_dir.iterdir() if x.suffix == ".json"]

    def show_events(self, name):
        """Print tabular events in terminal"""
        table = PrettyTable()

        field_names = None
        count = 0
        for event in self.iter_events(name):
            if field_names is None:
                field_names = event.field_names()
            table.add_row(event.values())
            count += 1

        if count == 0:
            print(f"No events of type {name}")
            return

        table.field_names = field_names
        print(f"Events of type {name} from directory: {self._output_dir}")
        print(table)
        print(f"Total events: {count}\n")

    def show_events_in_category(self, category):
        """Print tabular events matching category in terminal. Will cause all
        events to get loaded into memory.

        """
        event_names = []
        self._load_all_events()
        for name, events in self._events.items():
            if not events:
                continue
            if events[0].category == category:
                event_names.append(name)

        if not event_names:
            print(f"There are no events in category {category}")
            return

        for event_name in sorted(event_names):
            self.show_events(event_name)

    def show_event_categories(self):
        """Show the unique event categories in the log."""
        print("Catgories:  {}".format(" ".join(self.list_unique_categories())))

    def show_event_names(self):
        """Show the unique event names in the log."""
        print("Names:  {}".format(" ".join(self.list_unique_names())))

    def to_json(self):
        """Return all events in JSON format.

        Returns
        -------
        str

        """
        self._load_all_events()
        return json.dumps(
            [x.to_dict() for events in self._events.values() for x in events], indent=2
        )


#!/usr/bin/python
# -*- coding: utf-8 -*-

import time
import fdb
import psycopg2
import argparse
import logging
import re

parser = argparse.ArgumentParser(description='fb2psql')
parser.add_argument('--fb-host', dest='fb_host', default='localhost')
parser.add_argument('--fb-database', dest='fb_database', default='')
parser.add_argument('--fb-user', dest='fb_user', default='sysdba')
parser.add_argument('--fb-password', dest='fb_password', default='masterkey')

parser.add_argument('--pq-host', dest='pq_host', default='localhost')
parser.add_argument('--pq-database', dest='pq_database', default='postgres')
parser.add_argument('--pq-user', dest='pq_user', default='postgres')
parser.add_argument('--pq-password', dest='pq_password', default='postgres')

args = parser.parse_args()

logger = logging.getLogger('fb2psql')
formatter = logging.Formatter(
    fmt='%(asctime)s.%(msecs)03d, %(name)s, %(levelname)s, %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S'
)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)

def run_query(cursor, qry):
    start = time.time()
    qry = re.sub('^([ \n]+)', '', qry)
    qry = re.sub('[ \n]+', ' ', qry)
    cursor.execute(qry)
    try:
        retval = cursor.fetchall()
    except psycopg2.ProgrammingError:
        retval = []
    logger.info('(%.3fs) %s' % (time.time() - start, qry.replace('\n', ' ')[:1000]))
    return retval

def drop_table(cursor, table):
    return run_query(cursor, 'drop table if exists %(table)s' % locals())

def null_str(nullable):
    return 'NULL' if nullable else 'NOT NULL'

def default_str(cursor, default):
    if default is not None:
        # default = "'%s'" % default if isinstance(default, (str, unicode)) else default
        return 'DEFAULT %s' % default
    return ''

def unique_str(unique):
    return 'UNIQUE ' if unique else ''

def create_column(cursor, column):
    datatype = column.datatype
    if datatype.startswith('BLOB '):
        datatype = 'bytea'
    return '%s %s %s %s' % (
        column.name,
        datatype,
        null_str(column.isnullable()),
        default_str(pq_cursor, column.default)
    )

def create_constraint(cursor, constraint):
    if constraint.ispkey():
        return 'PRIMARY KEY (%s)' % ','.join([s.name for s in constraint.index.segments])

def consider_constraint(constraint):
    return constraint.ispkey()

def insert_data(cursor, rows, it):
        fields = len(rows) // it
        template = '(' + ', '.join(['%s'] * fields) + ')'
        template = ', '.join([template] * it)
        qry = 'INSERT INTO %s values %s' % (table.name, template)
        cursor.execute(qry, rows)
        table_name = table.name
        logger.info('Saving %(it)s elements in %(table_name)s' % locals())

def copy_data(table, fb_cursor, pq_cursor, bulk_nb=1000):
    values = []
    it = 0
    for row in run_query(fb_cursor, 'SELECT * FROM %s' % table.name):
        values.extend(row)
        it += 1
        if it % bulk_nb == 0:
            insert_data(pq_cursor, values, it)
            values = []
            it = 0
    if it:
        insert_data(pq_cursor, values, it)

if __name__ == '__main__':
    fb_args = dict([(k[3:], v) for k, v in args.__dict__.items() if k.startswith('fb_')])
    fb_conn = fdb.connect(**fb_args)
    fb_cursor = fb_conn.cursor()
    pq_args = dict([(k[3:], v) for k, v in args.__dict__.items() if k.startswith('pq_')])
    pq_conn = psycopg2.connect(**pq_args)
    pq_cursor = pq_conn.cursor()

    for table in fb_conn.schema.tables:
        drop_table(pq_cursor, table.name)
        columns = [create_column(pq_cursor, column) for column in table.columns]
        constraints = [create_constraint(pq_cursor, constraint) for constraint in table.constraints if consider_constraint(constraint)]
        qry = """
            create table %s
            (%s)
        """ % (table.name, ', '.join(columns + constraints))
        run_query(pq_cursor, qry)
        for index in table.indices:
            if not index.constraint or not index.constraint.ispkey():
                run_query(pq_cursor, 'create %s index %s on %s(%s)' % (unique_str(index.isunique()), index.name, table.name, ', '.join([c.name for c in index.segments])))
        copy_data(table, fb_cursor, pq_cursor)

    pq_conn.commit()


# -*- coding: cp1252 -*-
dateiname = 'dateineu2.txt'
file = open(dateiname,'r')
dok=''
h=0
for line in file:
    if '\n' and ('e' or 'i' or 'a' or 'u' or 'o') in line:
        dok = dok + line

    if "==" in line:
        dok = dok + ("\n")

print(dok)
print(h)
file.close
input = raw_input('wie soll die neue datei heissen?  :   ')
file=open(input+".txt",'w')
file.write(dok)
file.close


from saleor.graphql.tests.utils import get_graphql_content

MUTATION_CHECKOUT_UPDATE_LANGUAGE_CODE = """
mutation checkoutLanguageCodeUpdate($token: UUID, $languageCode: LanguageCodeEnum!){
  checkoutLanguageCodeUpdate(token: $token, languageCode: $languageCode){
    checkout{
      id
      languageCode
    }
    errors{
      field
      message
      code
    }
  }
}
"""

def test_checkout_update_language_code(
    user_api_client,
    checkout_with_gift_card,
):
    language_code = "PL"
    checkout = checkout_with_gift_card
    variables = {"token": checkout.token, "languageCode": language_code}

    response = user_api_client.post_graphql(
        MUTATION_CHECKOUT_UPDATE_LANGUAGE_CODE, variables
    )

    content = get_graphql_content(response)
    data = content["data"]["checkoutLanguageCodeUpdate"]
    assert not data["errors"]

    assert data["checkout"]["languageCode"] == language_code
    checkout.refresh_from_db()
    assert checkout.language_code == language_code.lower()


import random

# guesses_record = 0
lowest_score = []

def guess_number():
    secret_number = random.randint(1, 101)
    print("Hello! What is your name?")
    name = input("> ")
    print(f"Hello {name}!")
    num_guesses = 0

    while True:
        print("please choose a number between 1 and 100")
        guess = input("> ")
        if guess.isnumeric():
            guess = int(guess)
            if guess in range(1, 101):
                if guess > secret_number:
                    print("Too high!")
                    num_guesses += 1
                    print(num_guesses)
                elif guess < secret_number:
                    print("Too low!")
                    num_guesses += 1
                    print(num_guesses)
                else:
                    num_guesses += 1
                    print(f"Congrats {name}! You guessed the number! You got it in {num_guesses} guesses.")
                    lowest_score.insert(0, num_guesses)
                    get_high_score(num_guesses)
                    break
            else:
                print("ERROR: Please only type a number between 1 and 100")
        else:
            print("ERROR: Please only type a number between 1 and 100")

def get_high_score(guesses):
    if lowest_score > guesses:
        lowest_score = guesses
        print(f"Your best score so far is : {lowest_score}")
        return lowest_score

def start_new():
    print("Would you like to play again? Type 'Yes' or 'No'")
    while True:
        answer = input("> ")
        if answer == "Yes":
            guess_number()
        elif answer == "No":
            break
        else:
            print("Error. Please type 'Yes' or 'No'")

guess_number()
start_new()
get_high_score()


from __future__ import absolute_import
from __future__ import division

import argparse
import logging
import sys
import time
from datetime import datetime
import os

import numpy as np
import tensorflow as tf
from models.model import Model
from lstm_model import LSTMModel
from lib.glove import loadWordVectors
from lib.progbar import Progbar
from data.wrapper_class import WrapperClass
from test import top10, eval_test

logger = logging.getLogger("stacked_lstm_model")
logger.setLevel(logging.DEBUG)
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)

class Config(object):
    max_length=40
    embed_size=300
    lstm_dimension=300
    n_features=1
    n_epochs=200
    batch_size=64
    dropout=0.5
    def __init__(self):
        #self.cell = args.cell

        self.vocab_size = None
        self.output_path = "results/{}/{:%Y%m%d_%H%M%S}".format('stacked', datetime.now())
        self.model_output = self.output_path + "model.weights"
        self.eval_output = self.output_path + "results.txt"
        self.conll_output = self.output_path + "{}_predictions.conll".format('stacked')
        self.log_output = self.output_path + "log"
        self.summary_index = 0
        self.summary_path = self.output_path + 'summary'
        self.saved_input = '/home/toast/backwards_dict/results/stacked/20170314_062215model.weights'

def pad_sequences(data, max_length):
    ret = []

    # Use this zero vector when padding sequences.
    zero_vector = [0] * Config.n_features
    zero_label = -1 # corresponds to the 'O' tag

    for sentence, labels in data:
        ### YOUR CODE HERE (~4-6 lines)
            mask = None
            sentence_ = None
            labels_ = None
            if len(sentence) > max_length:
                sentence_ = sentence[:max_length]
                labels_ = labels[:max_length]
                mask = [True] * max_length
            else:
                sentence_ = sentence[:]
                labels_ = labels[:]
                for _ in range(max_length - len(sentence)):
                    sentence_.append(zero_vector)
                    labels_.append(zero_label)
                mask = [True] * len(sentence)
                mask.extend([False] * (max_length - len(sentence)))
            ret.append((sentence_, labels_, mask))
        ### END YOUR CODE ###
    return ret

class StackedLSTMModel(LSTMModel):

    def add_prediction_op(self):
        x = self.add_embedding()
        dropout_rate = self.dropout_placeholder
        cell = tf.contrib.rnn.LSTMCell(Config.lstm_dimension)
        stacked_lstm = tf.contrib.rnn.MultiRNNCell([cell]*3)
        outputs, state = tf.nn.dynamic_rnn(stacked_lstm, x, dtype=tf.float32, sequence_length=self.length_placeholder)
        with tf.name_scope('U'):
            U = tf.get_variable('U', (self.config.lstm_dimension, self.config.embed_size),
                            initializer=tf.contrib.layers.xavier_initializer())
            tf.summary.histogram('U',U)
        with tf.name_scope('b1'):
            b1 = tf.get_variable('b1',(self.config.embed_size))
            tf.summary.histogram('b1',b1)
        init = tf.cast(tf.transpose(tf.constant(self.pretrained_embeddings)), tf.float32)
        with tf.name_scope('W'):
            W = tf.get_variable('W', initializer=init)
            tf.summary.histogram('W',W)
        with tf.name_scope('b2'):
            b2 = tf.get_variable('b2', (self.config.vocab_size))
            tf.summary.histogram('b2',b2)
        with tf.name_scope('h1'):
            h1 = tf.nn.relu(tf.matmul((state[2]).c,U) + b1)
            tf.summary.histogram('h1',h1)
        with tf.name_scope('h_drop'):
            h_drop = tf.nn.dropout(h1, dropout_rate)
            tf.summary.histogram('h_drop',h_drop)
        with tf.name_scope('pred'):
            pred = tf.matmul(h_drop, W) + b2
            tf.summary.histogram('pred',pred)
        return pred

def main(config, embeddings, tokens):
    config.embed_size = embeddings.shape[1]
    config.vocab_size = len(tokens)

    handler = logging.FileHandler(config.log_output)
    handler.setLevel(logging.DEBUG)
    handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s: %(message)s'))
    logging.getLogger().addHandler(handler)

    # TODO: get examples
    graph = tf.Graph()
    with graph.as_default():
        logger.info("Building model...",)
        start = time.time()
        model = StackedLSTMModel(config, embeddings, tokens)
        logger.info("took %.2f seconds", time.time() - start)

        model.writer = tf.summary.FileWriter(model.config.summary_path,graph)
        summary_op = tf.summary.merge_all()
        init = tf.global_variables_initializer()

        with tf.Session() as session:
            session.run(init)
            saver = tf.train.Saver()
            model.fit(session, saver,summary_op)

            with open(model.config.conll_output, 'w') as f:
                write_conll(f, output)
            with open(model.config.eval_output, 'w') as f:
                for sentence, labels, predictions in output:
                    print_sentence(f, sentence, labels, predictions)

if __name__=='__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-t', action='store_true')
    parser.add_argument('--top10eval', action='store_true')
    x = parser.parse_args()
    config = Config()
    embeddings, tokens = loadWordVectors()
    config.embed_size = embeddings.shape[1]
    config.vocab_size = len(tokens)
    if x.t:
        graph = tf.Graph()
        with graph.as_default():
            model = LSTMModel(config, embeddings, tokens)
            top10(config, embeddings, tokens, model)
    elif x.top10eval:
        graph = tf.Graph()
        with graph.as_default():
            model = LSTMModel(config, embeddings, tokens)
            eval_test(embeddings, tokens, model)
    else:
        main(config, embeddings, tokens)


import os

import omegaconf
import pytest
import torch

from src.models.model import GCN
from tests import _PATH_CONF, _PATH_DATA

def _init_dataset_and_model():
    data = torch.load(_PATH_DATA)[0]

    cfg = omegaconf.OmegaConf.load(_PATH_CONF)
    model = GCN(
        hidden_channels=cfg.hyperparameters.hidden_channels,
        learning_rate=cfg.hyperparameters.learning_rate,
        weight_decay=cfg.hyperparameters.weight_decay,
    )
    model.eval()

    return data, model

@pytest.mark.skipif(not os.path.exists(_PATH_DATA), reason="Data files not found")
def test_model_output_shape():
    data, model = _init_dataset_and_model()
    out = model(data.x, data.edge_index)
    assert out.shape == torch.Size([2708, 7])  # predictions for all 2708 nodes

@pytest.mark.skipif(not os.path.exists(_PATH_DATA), reason="Data files not found")
def test_model_evaluate():
    data, model = _init_dataset_and_model()
    acc = model.evaluate(data)
    assert isinstance(acc, float)

@pytest.mark.skipif(not os.path.exists(_PATH_DATA), reason="Data files not found")
def test_model_predict():
    data, model = _init_dataset_and_model()
    pred, pred_int = model.predict(data, 0)
    assert isinstance(pred, str)
    assert isinstance(pred_int.item(), int)


# This document contains no USA or EU export controlled technical data.
#
# CARVE has received funding from the European Union's Horizon 2020 research
# and innovation programme under grant agreement No 732410, in the form of
# financial support to third parties of the RobMoSys project.

import unittest
import subprocess

class RunBTPropertiesVerification(unittest.TestCase):

    def _is_property_line(self, line):
        return line.startswith('-- ') and (line.endswith('true') or
                                           line.endswith('false'))

    def _is_parse_error_line(self, line):
        return line.endswith(': Parser error') or line.endswith('" undefined')

    def _run_nusmv(self, smv_file):
        # use 'universal_newlines' to create pipes in text mode.
        process = subprocess.Popen(['nuXmv', '-cpp', smv_file],
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE,
                                   universal_newlines=True)
        out, err = process.communicate()
        print(smv_file + ':')
        process.wait()
        return out, err

    def _check_nuxmv_output_line(self, line):
        line = line.strip()
        if self._is_property_line(line):
            print(line)
            self.assertTrue(line.endswith('true'))

    def _parse_nuxmv_output(self, out):
        # NuSMV does not seem to use its exit code to signal a failure in the
        # verificaiton of the properties.  We have to parse its output.
        for line in out.splitlines():
            self._check_nuxmv_output_line(line)

    def _look_for_nuxmv_parse_errors(self, err):
        for line in err.splitlines():
            if self._is_parse_error_line(line):
                print(line)
                self.fail("SMV Syntax error.")

    def _run_smv_test(self, smv_file):
        out, err = self._run_nusmv(smv_file)
        # Parse errors are dumped to stderr instead of stdout.
        self._look_for_nuxmv_parse_errors(err)
        self._parse_nuxmv_output(out)

    def test_uc1_tick_props(self):
        self._run_smv_test('skill_test.smv')
        self._run_smv_test('skill_loop_test.smv')
        self._run_smv_test('sequence_test.smv')
        self._run_smv_test('sequence_loop_test.smv')
        self._run_smv_test('parallel_test.smv')
        self._run_smv_test('parallel_loop_test.smv')
        self._run_smv_test('uc1_single_tick.smv')
        self._run_smv_test('uc1.smv')
        self._run_smv_test('uc1_parallel.smv')
        self._run_smv_test('uc1_single_tick_ext.smv')
        self._run_smv_test('uc1_ext.smv')
        self._run_smv_test('uc1_parallel_ext.smv')
        self._run_smv_test('uc1_contract.smv')
        self._run_smv_test('with_memory.smv')
        self._run_smv_test('without_memory.smv')

    def test_bt_sequence(self):
        self._run_smv_test('test_bt_sequence.smv')
        self._run_smv_test('test_bt_with_reset_sequence.smv')
        self._run_smv_test('test_bt_sequence_with_memory.smv')
        self._run_smv_test('test_bt_with_reset_sequence_with_memory.smv')

    def test_bt_fallback(self):
        self._run_smv_test('test_bt_fallback.smv')
        self._run_smv_test('test_bt_with_reset_fallback.smv')
        self._run_smv_test('test_bt_fallback_with_memory.smv')
        self._run_smv_test('test_bt_with_reset_fallback_with_memory.smv')

    def test_bt_resource_allocation(self):
        self._run_smv_test('test_bt_resource_allocation_01.smv')

if __name__ == '__main__':
    unittest.main()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/9/19 22:15
# @Author  : Jasontang
# @Site    :
# @File    : kNN.py
# @ToDo    : K-近邻算法

import numpy as np
import operator, os

# 创建数据集
def createDataSet():
	group = np.array([[1.0, 1.1],
				   [1.0, 1.0],
				   [0, 0],
				   [0, 0.1]])
	labels = ['A', 'A', 'B', 'B']
	return group, labels

# 分类算法
def classify0 (inX, dataSet, labels, k):
	dataSetSize = dataSet.shape[0]
	# ---- 开始计算输入点与每一个样本点的欧式距离 -----
	# print np.tile(inX, (dataSetSize, 1))
	diffMat = np.tile(inX, (dataSetSize, 1)) - dataSet
	# print diffMat
	sqDiffMat = diffMat ** 2
	# print sqDiffMat
	# print sqDiffMat.sum()
	# print sqDiffMat.sum(axis=1)
	sqDistances = sqDiffMat.sum(axis=1)
	distances = sqDistances ** 0.5
	# ---- 计算结束 ------
	# print distances
	# 进行排序并返回索引
	sortedDistIndicies = distances.argsort()
	# print sortedDistIndicies
	classCount = {}

	# 选择距离最小的k个点
	for i in range(k):
		# print labels[sortedDistIndicies[i]]
		voteIlabel = labels[sortedDistIndicies[i]]
		classCount[voteIlabel] = classCount.get(voteIlabel, 0) + 1
	# 排序
	# for item in classCount.iteritems():
	# 	print item

	sortedClassCount = sorted(classCount.items(),
							  key = operator.itemgetter(1), reverse=True)
	return sortedClassCount[0][0]

# 将数据从文件读入到矩阵中
def file2matrix(filename):
	fr = open(filename, 'r')
	# 得到文件的行数
	arrayOLines = fr.readlines()
	numberOfLines = len(arrayOLines)
	# 零填充矩阵
	returnMat = np.zeros((numberOfLines, 3))
	classLabelVector = []
	index = 0
	for line in arrayOLines:
		line = line.strip()
		listFromLine = line.split('\t')
		returnMat[index, :] = listFromLine[0: 3]
		classLabelVector.append(int(listFromLine[-1]))
		index += 1
	return returnMat, classLabelVector

# 归一化特征值
def autoNorm(dataSet):
	# print dataSet.min()
	# print dataSet.max()
	minVals = dataSet.min(0)
	maxVals = dataSet.max(0)
	# print minVals, maxVals
	ranges = maxVals - minVals
	# normDataSet = np.zeros(np.shape(dataSet))
	# print np.shape(dataSet)
	m = dataSet.shape[0]
	normDataSet = dataSet - np.tile(minVals, (m, 1))
	# print "--------", type(normDataSet)
	# print "+++++", type(np.tile(ranges, (m, 1)))
	normDataSet /= np.tile(ranges, (m, 1))
	return normDataSet, ranges, minVals

def datingClassTest(filename):
	hoRatio = 0.20 # 10%的测试数据
	datingDataMat, datingLabels = file2matrix(filename)
	normMat, ranges, minVals = autoNorm(datingDataMat)
	m = normMat.shape[0]
	numTestVects = int(m * hoRatio)
	errorCount = 0.0

	for i in range(numTestVects):
		# 前10%测试，后90%训练
		classifyResult = classify0(normMat[i, :], normMat[numTestVects:m, :],
								   datingLabels[numTestVects:m], 3)
		print('the classifier came back with: %d, the real answer is: %d' % \
			  (classifyResult, datingLabels[i]))
		if classifyResult != datingLabels[i]:
			errorCount += 1.0
	print ("the total(%d) error rate is: %0.2f%%" % (numTestVects, errorCount/float(numTestVects)*100.0))

def classifyPerson():
	resultList = ['not at all', 'in small doses', 'in large doses']
	percentTats = float(input("percentage of time spent playing video games?"))
	ffMiles = float(input("frequent flier miles earned per year?"))
	iceCream = float(input("liters of ice cream consumed per year?"))

	datingDataMat, datingLabels = file2matrix('../datingTestSet2.txt')
	normMat, ranges, minVals = autoNorm(datingDataMat)
	print(ranges)
	print(minVals)
	inArr = np.array([ffMiles, percentTats, iceCream])
	classifierResult = classify0((inArr - minVals)/ranges, normMat, datingLabels, 3)
	print('You will probably like this person:', resultList[classifierResult - 1])

def img2Vector(filename):
	returnVect = np.zeros((1, 1024))
	fr = open(filename)
	for i in range(32):
		lineStr = fr.readline()
		for j in range(32):
			returnVect[0, 32*i + j] = int(lineStr[j])
	return returnVect

def handwritingClassTest():
	hwLabels = []
	# 获取目录的内容
	trainingFileList = os.listdir("trainingDigits")
	m = len(trainingFileList)
	trainingMat = np.zeros((m, 1024))
	for i in range(m):
		# 这里获取的是文件名，如0_0.txt
		fileNameStr = trainingFileList[i]
		# 去掉.txt
		fileStr = fileNameStr.split(".")[0]
		# 去掉_0,提取文件名第一个数字
		classNumStr = int(fileStr.split("_")[0])
		hwLabels.append(classNumStr)
		trainingMat[i, :] = img2Vector('trainingDigits/%s' % fileNameStr)

	testFileList = os.listdir("testDigits")
	errorCount = 0.0
	mTest = len(testFileList)
	for i in range(mTest):
		fileNameStr = testFileList[i]
		fileStr = fileNameStr.split(".")[0]
		classNumStr = int(fileStr.split("_")[0])
		vectorUnderTest = img2Vector('testDigits/%s' % fileNameStr)
		classifierResult = classify0(vectorUnderTest, trainingMat, hwLabels, 3)
		print("the classifier came back with: %d, the real answer is : %d" % (classifierResult, classNumStr))
		if classifierResult != classNumStr:
			errorCount += 1.0
	print("\nthe total(%d) number of errors is: %d" % (mTest, errorCount))
	print("\nthe total(%d) error rate is: %0.2f%%" % (mTest, errorCount/float(mTest)*100.0))


import argparse
import collections
import functools
import glob
import json
import logging
import os
import pathlib
import re
import shlex
import shutil
import subprocess
import sys
import tempfile
import urllib.request

from concurrent import futures
from pathlib import Path

BASE_URL = "https://ci.android.com/builds/submitted/{build_id}/{target}/latest/raw"
SUPPORTED_ARCHS = ["arm64"]
VARIANTS = ["userdebug"]
BOOT_PREBUILT_REL_DIR = "packages/modules/BootPrebuilt"
ANDROID_BUILD_TOP = os.environ["ANDROID_BUILD_TOP"]

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)

def parse_args():
  parser = argparse.ArgumentParser()
  parser.add_argument(
      "build_id",
      type=int,
      help="the build id to download the build for, e.g. 6148204")
  parser.add_argument(
      "--bug",
      type=str,
      default=None,
      help="optional bug number for git commit.")

  return parser.parse_args()

def download_file(url, dest_filename):
  logger.info("Downloading %s -> %s", url, dest_filename)
  urllib.request.urlretrieve(url, dest_filename)

def get_artifact_download_spec(build_id, device, variant, dest_dir):
  target = "{}-{}".format(device, variant)
  url_base = BASE_URL.format(build_id=build_id, target=target)
  filename = "{}-img-{}.zip".format(device, build_id)
  url = os.path.join(url_base, filename)
  dest_filename = os.path.join(dest_dir, filename)
  return url, dest_filename

def update_prebuilt(build_id, boot_prebuilt, ver, arch, variant, bug):
  device = "aosp_" + arch
  arch_dir = os.path.join(boot_prebuilt, ver, arch)
  variant_dir = os.path.join(arch_dir, variant)
  boot_img_name = "boot-{}.img".format(ver)
  stored_img_name = "boot-{}.img".format(variant)
  try:
    subprocess.check_call(["repo", "start", "boot-prebuilt-{}".format(build_id)], cwd=arch_dir)

    os.makedirs(variant_dir)
    url, dest_filename = get_artifact_download_spec(build_id, device, variant, variant_dir)
    download_file(url, dest_filename)
    args = ["unzip", "-d", variant_dir, dest_filename, boot_img_name]
    logger.info("Calling: %s", " ".join(args))
    subprocess.check_call(args)
    shutil.move(os.path.join(variant_dir, boot_img_name), os.path.join(arch_dir, stored_img_name))

  finally:
    shutil.rmtree(variant_dir)

    message = """Update prebuilts to {build_id}.

Test: Treehugger
Bug: {bug}
""".format(build_id=build_id, bug=bug or "N/A")

    logger.info("Creating commit for %s", arch_dir)
    subprocess.check_call(["git", "add", "."], cwd=arch_dir)
    subprocess.check_call(["git", "commit", "-m", message], cwd=arch_dir)

def main():
  args = parse_args()
  with futures.ThreadPoolExecutor(max_workers=10) as pool:
    fs = []
    boot_prebuilt = os.path.join(ANDROID_BUILD_TOP, BOOT_PREBUILT_REL_DIR)
    for ver in os.listdir(boot_prebuilt):
      if not re.match(r'\d+[.]\d+', ver):
        continue
      for arch in os.listdir(os.path.join(boot_prebuilt, ver)):
        if arch not in SUPPORTED_ARCHS:
          continue
        for variant in VARIANTS:
          fs.append((ver, arch, pool.submit(update_prebuilt, args.build_id, boot_prebuilt, ver,
                                            arch, variant, args.bug)))

    futures.wait([f for ver, arch, f in fs])
    success_dirs = []
    logger.info("===============")
    logger.info("Summary:")
    for ver, arch, future in fs:
      if future.exception():
        logger.error("%s/%s: %s", ver, arch, future.exception())
      else:
        logger.info("%s/%s: Updated.", ver, arch)
        success_dirs.append(os.path.join(BOOT_PREBUILT_REL_DIR, ver, arch))

    if success_dirs:
      args = ["repo", "upload", "--verify", "--hashtag-branch", "--br",
              "boot-prebuilt-{}".format(args.build_id)] + success_dirs
      logger.info(" ".join(args))
      subprocess.check_call(args, cwd=ANDROID_BUILD_TOP)

if __name__ == "__main__":
  sys.exit(main())


import unittest
from EmployeesCoincidence import organize_data, calculate_coincidence, print_results

class Coincidence(unittest.TestCase):

    def test_data_valid(self):
        data = ['MARCO=TU18:00-20:00\n', 'GRACE=MO12:00-14:00']
        expected_res = [{'TU_entry': '18:00', 'TU_departure': '20:00', 'employee': 'MARCO'}, {'MO_entry': '12:00',
                                                                                'MO_departure': '14:00',
                                                                                'employee': 'GRACE'}]
        self.assertEqual(organize_data(sets_of_data=data), expected_res)

    def test_data_invalid(self):
        data = ['MARCO=TU25:00-30:00\n', 'GRACE=MO12:00s-14:00']
        with self.assertRaises(ValueError):
            organize_data(sets_of_data=data)

    def test_coincidence_false(self):
        data = [{'TU_entry': '18:00', 'TU_departure': '20:00', 'employee': 'MARCO'}, {'MO_entry': '12:00',
                                                                                'MO_departure': '14:00',
                                                                                'employee': 'GRACE'}]
        expected_res = 0
        self.assertEqual(calculate_coincidence(data), expected_res)

    def test_coincidence_true(self):
        data = [{'MO_entry': '18:00', 'MO_departure': '20:00', 'employee': 'MARCO'}, {'MO_entry': '19:00',
                                                                                'MO_departure': '21:00',
                                                                                'employee': 'GRACE'}]
        expected_res = 1
        self.assertEqual(calculate_coincidence(data), expected_res)

    def test_result_false(self):
        employee1 = {'employee': 'MARCO'}
        employee2 = {'employee': 'GRACE'}
        count = 0
        expected_res = "No employee coincides in the work schedule"
        self.assertEqual(print_results(employee1=employee1, employee2=employee2, count=count), expected_res)

    def test_result_true(self):
        employee1 = {'employee': 'MARCO'}
        employee2 = {'employee': 'GRACE'}
        count = 3
        expected_res = "MARCO-GRACE:3"
        self.assertEqual(print_results(employee1=employee1, employee2=employee2, count=count), expected_res)

if __name__ == '__main__':
    unittest.main()


import os
import webapp2
import json
import httplib2
import requests
import pprint
from weather import get_all_weather as get_all_weather

from Handler import Handler

def casing(name):
    words = name.split(" ")
    return " ".join(list(map(lambda x: x[0].capitalize()+x[1:],words)))

class MainPage(Handler):
    """
    This class is a child of Handler and is for FrontPage.
    """
    def get(self):

        KEY = json.loads(open('client_secrets.json', 'r').read())['UNDERGROUND']

        # USING IP ADDRESS, NOT RELIABLE
        # TEST_IP = json.loads(open('client_secrets.json', 'r').read())['TEST_IP']
        # ip = self.request.remote_addr

        # USING GEO COORDINATES
        geo = self.request.headers['X-Appengine-CityLatLong']

        # geo = json.loads(open('client_secrets.json', 'r').read())['TEST_GEO']

        city_name = casing(self.request.headers['X-AppEngine-City'])
        # city_name = "New York, NY"

        hourly_url = "http://api.wunderground.com/api/%s/hourly10day/q/%s.json" % (KEY, geo)

        h = httplib2.Http()
        hourly_result = json.loads(h.request(hourly_url,'GET')[1])

        if hourly_result.get('error') is not None:
            response = make_response(json.dumps(hourly_result.get('error')), 500)
            response.heads['Content-Type'] = 'application/json'
            return response

        forecast_url = "http://api.wunderground.com/api/%s/forecast10day/q/%s.json" % (KEY, geo)
        j = httplib2.Http()
        forecast_result = json.loads(j.request(forecast_url,'GET')[1])

        if forecast_result.get('error') is not None:
            response = make_response(json.dumps(forecast_result.get('error')), 500)
            response.heads['Content-Type'] = 'application/json'
            return response

        today, rest = get_all_weather(hourly_result, forecast_result)

        error = self.request.get('error')
        if error:
            error = "%s is not valid!  Retry search." % error

        return self.render("main_page.html", today=today, rest=rest,
                            city_name=city_name, error=error)

    def post(self):
        if self.request.get("city"):
            city = self.request.get('city')
            city_n = "_".join(city.split(" "))

            state = self.request.get('state')
            location = ",".join([city_n, state])

            city = casing(city)
            city_name = ", ".join([city, state])

            KEY = json.loads(open('client_secrets.json', 'r').read())['UNDERGROUND']

            hourly_url = "http://api.wunderground.com/api/%s/hourly10day/q/%s.json" % (KEY, location)

            h = httplib2.Http()
            hourly_result = json.loads(h.request(hourly_url,'GET')[1])

            if hourly_result.get('error') is not None:
                response = make_response(json.dumps(hourly_result.get('error')), 500)
                response.heads['Content-Type'] = 'application/json'
                return response

            forecast_url = "http://api.wunderground.com/api/%s/forecast10day/q/%s.json" % (KEY, location)

            j = httplib2.Http()
            forecast_result = json.loads(j.request(forecast_url,'GET')[1])

            if forecast_result.get('error') is not None:
                response = make_response(json.dumps(forecast_result.get('error')), 500)
                response.heads['Content-Type'] = 'application/json'
                return response

            today, rest = get_all_weather(hourly_result, forecast_result)
            if today == None:
                error = city_name
                return self.redirect('/?error=%s' % error)
            else:
                error = None
                return self.render("main_page.html", today=today, rest=rest,
                                    city_name=city_name, error=error)

#
# class TestPage(Handler):
#         def get(self):
#             ip = self.request.remote_addr
#             test = self.request.headers['X-Appengine-CityLatLong']
#
#
#             return self.render("test.html", ip=ip, test=test)

app = webapp2.WSGIApplication([ ("/", MainPage)
                                ])
                                # debug=True)


'''
Question1- Create a class that will model a Food.  It will have a property for a name, a category (Meat, Canned Soup, Dairy, Spreads, etc.) it will have a retail price (5.99),
it will have a weight in grams (25.2) and it will have an aisle number where it can be found (5, 23, etc.).
It will have a method that will describe the food and include details about every property.
For example - "We have 25.4 g containers of the dairy product Nacho Cheese that retails for $5.99 and can be found in aisle 45."
The bold text represents the properties - your output does not require bold text, this is just to demonstrate your method details.
Create an instance of this food -  Heinz Baked Beans in 400 g cans located in aisle 23 in the category of Canned Vegetables for $1.06 a can.  Once you have created the instance, call the method.
Create an instance of this food - Kraft Peanut Butter in 1000g container.  This is located in the with the Spreads in aisle 6 for $5.77 each.   Once you have created this instance, call the method.
'''
# Program name:- Food Model Program
# Program Purpose:- To create a class and call its instances
# Name:- Saurabh Chawla
# Date:- 3 November 2023

# Program Introduction
print('Welcome to our first OOPS program')

# Creating the class that is model a food
class foodModel:
    def __init__(self, name, category, retailPrice, weight, aisleNumber):
        self.name = name
        self.category = category
        self.retailPrice = retailPrice
        self.weight = weight
        self.aisleNumber = aisleNumber

    # Creating a method that describe the food and include details about every property
    def description(self):
        print(f'We have {self.weight} g containers of the {self.category} product {self.name} that $ {self.retailPrice} and can be found in {self.aisleNumber}. ')

# Creating an instance of the food - Heinz Baked Beans
heinzBakedBeans = foodModel('Heinz Baked Beans', 'Canned Vegetables', 1.06, 400, 23 )
heinzBakedBeans.description()

# Creating an instance of the food - Kraft Peanut Butter
kraftPeanutButter = foodModel('Kraft Peanut Butter', 'Spreads', 5.77, 1000, 6)
kraftPeanutButter.description()

# Thank the user
print('Thank you for using our program')


from apicoreservices import api_lib
from apicoreservices.log_lib import Log

class PutServiceTest(api_lib.ApiCore):

    def test_case1(self):
        '''
        Update a resouce at 'https://jsonplaceholder.typicode.com/posts/1'
        '''
        header = {"Content-type": "application/json",
                  "charset": "UTF-8",
                  }
        url = 'https://jsonplaceholder.typicode.com/posts/1'
        payload = {"id" : 1,
                   "title" : "abc",
                   "body": "xyz",
                   "userId" : 1
                   }
        data = self.update_data(url, data=payload, headers=header)
        return data

    def test_case2(self):

        logging = Log.log_config(userLevel=40)
        res_status_code = self.test_case1().status_code
        if res_status_code == 200:
            logging.info(msg='Update is successfull{}'.format(res_status_code))
        else:
            logging.error(msg='Update is unsuccessfull {}'.format(res_status_code))
            raise AssertionError('The post call is unsuccessfull {}'.format(res_status_code))

    def test_case3(self):
        '''
        Verify schema after put call.
        Note : This validates the original json as the PUT call on
        https://jsonplaceholder.typicode.com/posts/1 fails with 500 internal server error
        '''
        self.validate_json_schema('schema.json', 'dict', response_json=self.get_json())


import torch
import torch.nn as nn
import torch.nn.functional as F
import models.AGCN as AGCN

class MyModel(nn.Module):
    def __init__(self, batch_size, hidden_size=32, time_steps=50, feature=4, num_car=7):
        """
        :param batch_size:
        :param hidden_size:
        :param time_steps:
        :param feature:
        :param num_car:
        """
        super(MyModel, self).__init__()
        self.AGC = AGCN.AdaptiveGraphConv(hidden_size=hidden_size)
        self.batch_size = batch_size
        self.hidden_size = hidden_size
        self.time_steps = time_steps
        self.features = feature
        self.num_car = num_car
        self.lstm1 = nn.LSTM(hidden_size, hidden_size, batch_first=True)  # position_x
        self.lstm2 = nn.LSTM(hidden_size, hidden_size, batch_first=True)  # position_y
        self.lstm3 = nn.LSTM(hidden_size, hidden_size, batch_first=True)  # speed_x
        self.lstm4 = nn.LSTM(hidden_size, hidden_size, batch_first=True)  # speed_y

        # 输出网络
        self.output_layer = nn.Linear(self.hidden_size * 4 * self.num_car, 1 * 3)

    def forward(self, GCNinput):
        """
        计算前向转播
        :param GCNinput: (batch, time, num_area, feature_
        :return: flow, empty, demand
        """
        # GCNinput shape torch.Size([64, 20, 100, 10])
        # ## 输入GCN
        GCNoutput = self.AGC.forward(GCNinput)
        # output: batch, time, num_area, hidden_size ? hidden_size ?

        # ##输入LSTM
        # 1.time和num_area位置交换 (batch, num_area, time, feature) -> (batch, time, num_area, feature)
        # 2.batch和num_area合并 (batch, time, num_area, feature) -> (batch*time, num_area, feature)

        # GCNoutput shape torch.Size([64, 20, 100, 64])
        GCNoutput = GCNoutput.reshape(self.batch_size * self.num_car, self.time_steps, self.hidden_size)
        # 3.输入Lstm网络
        GCNoutput = F.sigmoid(GCNoutput)
        Lstm_output_positionX, _ = self.lstm1(GCNoutput)
        Lstm_output_positionY, _ = self.lstm2(GCNoutput)
        Lstm_output_speedX, _ = self.lstm3(GCNoutput)
        Lstm_output_speedY, _ = self.lstm4(GCNoutput)

        # 输出 LSTM_output_*: batch_size * num_area, time, hidden_size

        # 4.抽取时间维上取最后一个数据
        Lstm_output_positionX = Lstm_output_positionX[:, -1, :].squeeze()
        Lstm_output_positionY = Lstm_output_positionY[:, -1, :].squeeze()
        Lstm_output_speedX = Lstm_output_speedX[:, -1, :].squeeze()
        Lstm_output_speedY = Lstm_output_speedY[:, -1, :].squeeze()

        # 6.构造输出[0, 1, 0]
        # 首先拼接时空融合结果
        splicing = torch.cat([Lstm_output_positionX, Lstm_output_positionY, Lstm_output_speedX, Lstm_output_speedY], dim=-1)

        splicing = splicing.reshape(self.batch_size, -1)
        output = self.output_layer(splicing)
        output = F.sigmoid(output)
        return output


#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Implement additional codes to help explore the Boston data set

@author: ucaiado

Created on Februery 20, 2015
"""
# import libraries
import numpy as np
import matplotlib.pyplot as pl
import seaborn as sns
import seaborn as sns
from sklearn import datasets
from sklearn.tree import DecisionTreeRegressor
import pandas as pd

# configure charts
sns.set_palette("deep", desat=0.6)
sns.set_context(rc={"figure.figsize": (8, 4)})
sns.set_style("darkgrid")

'''
Begin of Help Functions
'''

'''
End of Help Functions
'''

# implement function to reshape data

def inplitData(df_housing_features, df_housing_prices):
    """
    Return a data frame with all features mearged in a single column and
    calculate how this data it spread out related to their own average
    :param df_housing_features: Data Frame. All features of Boston data set
    :param df_housing_prices: Data Frame. House prices (target) of Boston
    dataset.
    """
    # initiate lists
    l_aux = []
    l_aux2 = []
    l_aux3 = []
    l_aux4 = []
    # merge each list in an unique column and keep the names to identify them
    for s_aux in df_housing_features.columns:
        # normalize the featues values to number of standard deviations
        f_mean = df_housing_features[s_aux].mean()
        df_error = (df_housing_features[s_aux]-f_mean)
        df_error /= df_housing_features[s_aux].std()
        f_correct = 2
        # f_correct = (df_error.max()-df_error.min())/5  # group the data
        df_error = (df_error*f_correct).round()/f_correct
        na_aux2 = np.round(df_error, decimals=1)

        # agregate data
        l_aux += list(df_housing_features[s_aux].values)
        l_aux4 += list(na_aux2)
        l_aux2 += [x[0] for x in df_housing_prices.values]
        l_aux3 += ([s_aux]*df_housing_features.shape[0])

    # create a dataframe with the data agregated
    df_aux = pd.DataFrame([l_aux, l_aux4, l_aux2, l_aux3],
                          index=['FEAT_VAL', 'FEAT_NORML', 'MEDV',
                                 'FEAT_NAME']).T
    return df_aux


from new_model.my_model import BertSeq2SeqModel
from src.transformers.configuration_bert import BertConfig

import torch
import torch.nn as nn
import torch.nn.functional as F

import os,json,codecs,logging,random
from tqdm import trange,tqdm
import numpy as np
import argparse

from torch.utils.data import Dataset, DataLoader

class tokendataset(Dataset):
    def __init__(self,list_file):
        self.dataset=list_file

    def __len__(self):
        return len(self.dataset)

    def __getitem__(self, idx):
        data=self.dataset[idx]
        return(data)

def padding_fn(data_list):
    length=[]
    for item in data_list:
        length.append(len(item))
    pad_len=max(length)
    for item in data_list:
        if len(item)<pad_len:
            item.extend([0]*(pad_len-len(item)))
    return data_list

def padding_all_fn(data_list):
    input_ids=[]
    input_attention_mask=[]
    labels=[]
    decoder_mask=[]
    for item in data_list:
        input_ids.append(item["input_ids"])
        input_attention_mask.append(item["input_attention_mask"])
        labels.append(item["labels"])
        decoder_mask.append(item["decoder_mask"])
    input_ids=padding_fn(input_ids)
    input_attention_mask=padding_fn(input_attention_mask)
    labels=padding_fn(labels)
    decoder_mask=padding_fn(decoder_mask)
    batch={}
    batch["input_ids"]=input_ids
    batch["input_attention_mask"]=input_attention_mask
    batch["labels"]=labels
    batch["decoder_mask"]=decoder_mask
    return batch

def padding_unilm_fn(data_list):
    input_ids=[]
    attention_mask=[]
    token_type_ids=[]
    labels=[]
    length=[]
    for item in data_list:
        input_ids.append(item["input_ids"])
        attention_mask.append(item["attention_mask"])
        token_type_ids.append(item["token_type_ids"])
        labels.append(item["labels"])
        length.append(len(item["input_ids"]))
    max_len=max(length)
    for i ,_ in enumerate(input_ids):
        pad_len=max_len-len(input_ids[i])
        input_ids[i].extend([0]*pad_len)
        attention_mask[i].extend([0]*pad_len)
        token_type_ids[i].extend([1]*pad_len)
        labels[i].extend([0]*pad_len)
    batch={}
    batch["input_ids"]=input_ids
    batch["attention_mask"]=attention_mask
    batch["token_type_ids"]=token_type_ids
    batch["labels"]=labels
    return batch

logger = logging.getLogger(__name__)

def set_seed(args):
    random.seed(args.seed)
    np.random.seed(args.seed)
    torch.manual_seed(args.seed)
    torch.cuda.manual_seed(args.seed)

def main():
    parser = argparse.ArgumentParser()
    #data and model
    parser.add_argument("--data_path",default='E:\\tokenized_ids.json',type=str,required=False,)
    parser.add_argument("--model_name_or_path",default='E:\\bert',type=str,required=False,)
    parser.add_argument("--output_dir",default='./model_output',type=str,required=False,)

    parser.add_argument("--batch_size", default=4, type=int,)
    parser.add_argument("--gradient_accumulation_steps",type=int,default=1,)
    parser.add_argument("--learning_rate", default=3e-5, type=float, help="学习率")
    parser.add_argument("--weight_decay", default=0.0, type=float, help="学习率衰减")
    parser.add_argument("--adam_epsilon", default=1e-8, type=float,)
    parser.add_argument("--do_train",default=True, action="store_true", help="是否fine tuning")
    parser.add_argument("--max_grad_norm", default=1.0, type=float, help="梯度裁减值")
    parser.add_argument("--num_train_epochs", default=1, type=int, help="训练epochs次数",)
    parser.add_argument("--warmup_steps", default=0, type=int, help="学习率线性预热步数")
    parser.add_argument("--logging_steps", type=int, default=500, help="每多少步打印日志")
    parser.add_argument("--seed", type=int, default=42, help="初始化随机种子")
    parser.add_argument("--max_steps",default=200000,type=int,help="训练的总步数",)
    parser.add_argument("--save_steps", default=20000, type=int, help="保存的间隔steps", )
    parser.add_argument("--overwrite_output_dir",default=False, action="store_true", help="是否覆盖输出文件夹")
    args=parser.parse_args()

    if (
        os.path.exists(args.output_dir) and os.listdir(args.output_dir)
        and args.do_train and not args.overwrite_output_dir
    ):
        raise ValueError(
            "Output directory ({}) already exists and is not empty. Use --overwrite_output_dir to overcome.".format(
                args.output_dir)
        )

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    # Setup logging
    logging.basicConfig(
        format="%(asctime)s - %(levelname)s - %(name)s -   %(message)s",
        datefmt="%m/%d/%Y %H:%M:%S",
        level=logging.INFO,
    )

    # Set seed
    set_seed(args)

    #load json data
    with open(args.data_path, 'r')as f:
        data_file = json.load(f)

    train_dataset = tokendataset(data_file)
    train_dataloader = DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True, drop_last=True, collate_fn=padding_all_fn)

    if args.do_train:
        model = BertSeq2SeqModel.from_pretrained('E:\\bert', double_model=True)
        model.to(device)
        t_total = args.max_steps
        optimizer = torch.optim.Adam(model.parameters(),lr=args.learning_rate,eps=args.adam_epsilon)
        global_step = 0
        tr_loss, logging_loss = 0.0, 0.0
        model.train()
        model.zero_grad()
        logger.info("start_training")

        for epoch in trange(int(args.num_train_epochs),desc='Epoch'):
            for step,batch in enumerate(tqdm(train_dataloader,desc='Iteration')):
                input_ids, input_attention_mask, labels, decoder_mask = \
                    batch["input_ids"], batch["input_attention_mask"], batch["labels"], batch["decoder_mask"]

                input_ids = torch.tensor(input_ids).to(device)
                input_attention_mask = torch.tensor(input_attention_mask).to(device)
                labels = torch.tensor(labels).to(device)
                decoder_mask = torch.tensor(decoder_mask).to(device)
                #preare for decode input
                decoder_input_ids=labels.clone()[:,:-1]
                labels=labels[:,1:]
                decoder_mask=decoder_mask[:,:-1]
                outputs=model(input_ids=input_ids,
                              attention_mask=input_attention_mask,
                              decoder_input_ids=decoder_input_ids,
                              decoder_padding_mask=decoder_mask,
                              labels=labels)
                loss=outputs[0]
                loss.backward()
                tr_loss += loss.item()
                if (step + 1) % args.gradient_accumulation_steps == 0:
                    torch.nn.utils.clip_grad_norm_(model.parameters(), args.max_grad_norm)  # 梯度截断
                    optimizer.step()
                    model.zero_grad()
                    global_step += 1

                    if args.logging_steps > 0 and global_step % args.logging_steps == 0:
                        logs = {}
                        loss_scalar = (tr_loss - logging_loss) / args.logging_steps
                        logs["loss"] = loss_scalar
                        logging_loss = tr_loss

                    if args.save_steps > 0 and global_step % args.save_steps == 0:
                        output_dir = os.path.join(args.output_dir, 'checkpoint-{}'.format(global_step))
                        if not os.path.exists(output_dir):
                            os.makedirs(output_dir)
                        model_to_save = (model.module if hasattr(model, "module") else model)
                        model_to_save.save_pretrained(output_dir)

                if args.max_steps > 0 and global_step > t_total:
                    break
            if args.max_steps > 0 and global_step > t_total:
                break

        logger.info(" global_step = %s, average loss = %s", global_step, tr_loss / global_step)
        if not os.path.exists(args.output_dir):
            os.makedirs(args.output_dir)
        model_to_save = (
            model.module if hasattr(model, "module") else model
        )
        model_to_save.save_pretrained(args.output_dir)

if __name__ =='__main__':
    main()


# PROGRAM CheckPrime:

##################
# ERROR CHECKING #
##################
c = str(input("Do you want error checking on? (y/n)"))
if c == 'y':
# THEN
    MyErrorCheck = True
else:
    MyErrorCheck = False
# ENDIF;

##################
# PRIME CHECKING #
##################

a = int(input("Please input value:"))
b = a - 1
IsPrime = True
while b != 1:
# DO
    if a % b == 0:
        # THEN
        IsPrime = False
        if MyErrorCheck == True:
        # THEN
            print("****** Division with no remainder found, with ", b, "*********")
        # ENDIF;
    # ENDIF;
    if MyErrorCheck == True:
    # THEN
        print(">> a is ",a,">> b is ",b, ">> IsPrime is ",IsPrime)
    # ENDIF;
    b = b - 1
# ENDWHILE;
if IsPrime:
# THEN
    print(a, "is a prime number")
else:
    print(a, "is not a prime number")
# ENDIF;
# END.


import os
import numpy as np
import argparse

from time import time

# ----------------------------------------------------------------------------------------------------------------------
# Arguments
# ----------------------------------------------------------------------------------------------------------------------
print('..received ssh command')
parser = argparse.ArgumentParser(description='fly-anesthesia-analysis')
parser.add_argument('--recordings', type=str, required=False, default=[])
args = parser.parse_args()

# Override inputs
recordings = [args.recordings]

# TODO: Create basic plots
# recordings = ['2021-09-13-12-28-46']

for r in recordings:
    datadir = '/local/anesthesia/data/' + r + '/'

    # -----------------------------------------------------------------
    # Convert any .h264 to .mp4 using ffmpeg
    # -----------------------------------------------------------------
    print(datadir)
    # Lazy way to read in frame rate value
    if os.path.isfile(datadir + 'info.txt'):
        info = open(datadir + 'info.txt', 'r')
        for param in info:
            param = param.replace('\n', '').split(',')
            if param[0] == 'frame-rate':
                frame_rate = int(float(param[1]))
            elif param[0] == 'n-flies':
                n_flies = [int(float(p)) for p in param[1::]]

        print('framerate: ' + str(frame_rate))
        print('n_flies: ' + str(n_flies))

        # ffmpeg compression
        if os.path.isfile(datadir + 'video.h264'):  # Check for .h264 file
            if os.path.isfile(datadir + 'video-c.mp4'):  # Check for .mp4 file
                print('video-c.mp4 already exists, skipping conversion')
            else:
                print('compressing .h264 to .mp4...')
                s = time()
                os.system('/usr/bin/ffmpeg -framerate ' + str(frame_rate) +
                          ' -i ' + datadir + 'video.h264 -vf "transpose=2,transpose=2",lenscorrection=k1=-0.15:k2=0 ' + datadir + 'video-c.mp4')
                print('converted video using ffmpeg in ' + '{:.1f}'.format(time()-s) + ' seconds')
        else:
            print('video.h264 not found, skipping compression')

        # ffmpeg cropping wells
        if os.path.isfile(datadir + 'video.h264'):  # Check for .h264 file
            for w in range(1, 7):
                if os.path.isfile(datadir + 'video-c-well-' + str(w) + '.mp4'):  # Check for .mp4 file
                    print('video-c-well-' + str(w) + '.mp4 already exists, skipping conversion')
                else:
                    print('compressing well ' + str(w) + ' from .h264 to .mp4...')

                    '''
                    # Old camera position (pre 7-7-2021)
                    if w == 1:
                        x = '0'
                        y = '0'
                        wi = '750'
                        he = '604'
                    elif w == 2:
                        x = '700'
                        y = '0'
                        wi = '523'
                        he = '604'
                    elif w == 3:
                        x = '1223'
                        y = '0'
                        wi = '697'
                        he = '604'
                    elif w == 4:
                        x = '0'
                        y = '604'
                        wi = '750'
                        he = '476'
                    elif w == 5:
                        x = '700'
                        y = '604'
                        wi = '523'
                        he = '476'
                    elif w == 6:
                        x = '1223'
                        y = '604'
                        wi = '697'
                        he = '476'
                    '''
                    if w == 1:
                        x = '323'
                        y = '163'
                        wi = '350'
                        he = '350'
                    elif w == 2:
                        x = '731'
                        y = '169'
                        wi = '350'
                        he = '350'
                    elif w == 3:
                        x = '1138'
                        y = '174'
                        wi = '350'
                        he = '350'
                    elif w == 4:
                        x = '316'
                        y = '572'
                        wi = '350'
                        he = '350'
                    elif w == 5:
                        x = '729'
                        y = '574'
                        wi = '350'
                        he = '350'
                    elif w == 6:
                        x = '1134'
                        y = '579'
                        wi = '350'
                        he = '350'

                    # -c:v h264_nvenc
                    s = time()
                    os.system('/usr/bin/ffmpeg -framerate ' + str(frame_rate) +
                              ' -i ' + datadir + 'video.h264 -vf "transpose=2,transpose=2",lenscorrection=k1=-0.15:k2=0,"crop=' + wi + ':' + he + ':'
                              + x + ':' + y + '" ' + datadir +
                              'video-c-well-' + str(w) + '.mp4')
                    print('converted video using ffmpeg in ' + '{:.1f}'.format(time()-s) + ' seconds')
        else:
            print('video.h264 not found, skipping well compression')

        w = 1
        for nf in n_flies:
            if nf > 0:
                # -----------------------------------------------------------------
                # Convert .mp4 compressed to .pv using tgrabs
                # -----------------------------------------------------------------
                if os.path.isfile(datadir + 'tracking-well-' + str(w) + '.pv'):  # Check for .pv file
                    print('tracking-well-' + str(w) + '.pv already exists')
                elif os.path.isfile(datadir + 'video-c-well-' + str(w) + '.mp4'):  # Check for .mp4 file
                    print('running tgrabs on video-c-well-' + str(w) + '...')
                    s = time()
                    os.system('/home/toor/miniconda3/envs/tracking/bin/tgrabs -i ' + datadir +
                              'video-c-well-' + str(w) + '.mp4 -o ' + datadir +
                              'tracking-well-' + str(w) + ' -threshold 15 -average_samples 100 -averaging_method mode ' +
                              '-meta_real_width 3.3697 -reset_average -nowindow >> ' + datadir + 'tgrabs-' + str(w) + '.log')
                    print('tgrabs completed in ' + '{:.1f}'.format(time() - s) + ' seconds')
                else:
                    print('video-c-well-' + str(w) + '.mp4 not found, nothing to convert')

                # -----------------------------------------------------------------
                # Analyze .pv using trex
                # -----------------------------------------------------------------
                if os.path.isdir(datadir + 'tracking-well-' + str(w)):
                    print('tracking-well-' + str(w) + ' files already found')
                else:
                    print('running trex on video-c-well-' + str(w) + '...')
                    s = time()
                    os.system('/home/toor/miniconda3/envs/tracking/bin/trex -i ' + datadir +
                              'tracking-well-' + str(w) + '.pv -output_dir ' + datadir +
                              ' -track_max_individuals ' + str(nf) +
                              ' -individual_prefix fly -fishdata_dir tracking-well-' + str(w) + ' ' +
                              '-auto_no_results -nowindow -auto_quit >> ' + datadir + 'trex-' + str(w) + '.log')
                    print('trex completed in ' + '{:.1f}'.format(time() - s) + ' seconds')

                # -----------------------------------------------------------------
                # Split npz into npy
                # -----------------------------------------------------------------
                if os.path.isdir(datadir + 'tracking-well-' + str(w)) and len(os.listdir(datadir + 'tracking-well-' + str(w))) > 0:
                    for i in range(0, nf):
                        # Check for tracking files
                        if os.path.isfile(datadir + 'tracking-well-' + str(w) + '/tracking-well-' + str(w) + '_fly' + str(i) + '.npz'):
                            # Make tracking npz directory if it doesn't already exist
                            if not os.path.isdir(datadir + 'tracking-well-' + str(w) + '/tracking-well-' + str(w) + '_fly' + str(i) + '/'):
                                os.mkdir(datadir + 'tracking-well-' + str(w) + '/tracking-well-' + str(w) + '_fly' + str(i) + '/')

                            data = np.load(datadir + 'tracking-well-' + str(w) + '/tracking-well-' + str(w) + '_fly' + str(i) + '.npz')
                            for d in data:
                                if not os.path.isfile(datadir + 'tracking-well-' + str(w) + '/tracking-well-' + str(w) + '_fly' + str(i) + '/' + d + '.npy'):
                                    np.save(datadir + 'tracking-well-' + str(w) + '/tracking-well-' + str(w) + '_fly' + str(i) + '/' + d + '.npy', data[d])

                        # Check for posture files
                        if os.path.isfile(datadir + 'tracking-well-' + str(w) + '/tracking-well-' + str(w) + '_posture_fly' + str(i) + '.npz'):
                            # Make posture npz directory if it doesn't already exist
                            if not os.path.isdir(datadir + 'tracking-well-' + str(w) + '/tracking-well-' + str(w) + '_posture_fly' + str(i) + '/'):
                                os.mkdir(datadir + 'tracking-well-' + str(w) + '/tracking-well-' + str(w) + '_posture_fly' + str(i) + '/')

                            data = np.load(datadir + 'tracking-well-' + str(w) + '/tracking-well-' + str(w) + '_posture_fly' + str(i) + '.npz')
                            for d in data:
                                if not os.path.isfile(datadir + 'tracking-well-' + str(w) + '/tracking-well-' + str(w) + '_posture_fly' + str(i) + '/' + d + '.npy'):
                                    np.save(datadir + 'tracking-well-' + str(w) + '/tracking-well-' + str(w) + '_posture_fly' + str(i) + '/' + d + '.npy', data[d])
            w = w + 1

    print('')


#1.11 Transportation.
def transportation(distance):
    if distance =='Yes':
        return 'Better go by bus.'
    if distance =='No':
      distance_1 = int(input('How many kilometers is the workplace? '))
      if distance_1 > 10 :
        return "Take the bus."
      elif distance_1 >=2 and distance_1 <=10:
        return 'Better to ride a bike.'
      elif distance_1 <2:
         return "Better to walk."

distance = input('is it raining? ')

print(transportation(distance))


#Server Side -  Centralize all clients and aplication control
#Router runs physics protocolls -> Modem transmits information to the world.
#Your modem has one IP address.. It is Public
#Behind Router there are Local IPs - NAT
#
#Extending for multiclients

import socket
import threading #for message handling - IO Bound

HEADER = 64 #Bytes
PORT = 5060 #Port thta are not beeing used
SERVER = "127.0.0.1" #Local IP
SERVER = socket.gethostbyname(socket.gethostname())#get local ip address and use it as your server IP
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!DISCONECT"

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #first =  family (INET is IPV4) / SOCK_STREAM is for TCP
server.bind(ADDR) #IP and PORT will be transfered to this aplication

def handle_client(conn , addr): #Handle individually each connection
    print(f"[NEW CONNECTION] {addr} connected.")

    connected = True
    while connected:
        msg_length = conn.recv(HEADER).decode(FORMAT) #blocking line of code. Args is a bytes number. decode is getting up the bytes and translating them into utf-8 - char.
        #Header will bring us how many bytes this messages brang
        #Header might not be big enought
        if msg_length:
            msg_length = int(msg_length) #string to int
            msg = conn.recv(msg_length).decode(FORMAT) #Now you receive yhe payload.
            # If you dont disconnect properly, the server might not accept same connection (because it is already connected). Due to that, it is important to sent a proper message to end a connection the right way
            if msg == DISCONNECT_MESSAGE:
                connected = False
        print(f"[{addr}] - {msg}" )
    conn.close() #disconnecting socket!

def start():#Allow Client to connect to our aplication and create a new thread to handle it
    server.listen()
    print(f"[LISTENING] Server is listening on {SERVER}")
    while(True):
        conn, addr = server.accept() #That line blocks your code by waiting you client .. conn is a socket obj and addr is the information about the connection
        thread = threading.Thread(target=handle_client, args=(conn, addr)) #Creating thread obj that handles that new client!
        thread.start()
        print(f"[ACTIVE CONNECTIONS: {threading.active_count()-1}]")
print("[STARTING] Server is starting...")
start()