from sklearn.semi_supervised import LabelSpreading, LabelPropagation
import datasets as ds
import numpy as np
import adversarial
from sklearn.metrics import pairwise
from label_propagation_cached import LabelPropagationCached
from sklearn.exceptions import ConvergenceWarning
import warnings
import time
from config import Config
from greedy_adv_attack import (
    multiple_flips_greedy,
    perturb_y_classification,
    multiple_flips_random,
)
from mlp import SimpleMlp

warnings.filterwarnings("ignore", category=ConvergenceWarning)
config = Config(path="config.yaml")

def efficiency():
    for ds_name, attr in config.datasets.items():
        x_train, x_test, y_train, y_test = attr["loader_function"]()
        random_seed = config.seed
        gamma = attr["gamma"]
        max_iter = attr["iter"]
        dataset = ds_name
        randomState = np.random.RandomState(random_seed)
        # x_train, x_test, y_train, y_test = ds.mnist17_prep()
        # x_train, x_test, y_train, y_test = ds.mnist15_prep()
        # x_train, x_test, y_train, y_test = ds.mnist178_prep()
        # x_train, x_test, y_train, y_test = ds.rcv_prep_f()
        # x_train, x_test, y_train, y_test = ds.cifarBinary_prep()

        p_labelled = 0.05
        n_labelled = int(y_train.shape[0] * p_labelled)

        (
            y_train_shuffled,
            x_train_shuffled,
            labels_unlabelled,
        ) = ds.unlabel_shuffle_training_set(y_train, x_train, n_labelled, randomState)

        flips_p = 0.2
        n_flips = int(flips_p * n_labelled)
        repetition = 50
        results_gm = np.empty((repetition,))
        results_im = np.empty((repetition,))
        print(f"{dataset} loaded")
        print(f"[{ds_name}][{p_labelled}][fb:{flips_p}]")
        for i in range(repetition):
            # print(f"[{i}]")
            start_time = time.time()
            perturb_y_classification(
                n_labelled, x_train_shuffled, y_train_shuffled, n_flips, gamma=gamma
            )
            t_gm = time.time() - start_time
            print(
                f"[{i}/{repetition}][{ds_name}][{p_labelled}][fb:{flips_p}]\t PM {t_gm:.2f}s"
            )
            # print("---Probabilistic %s seconds ---" % t_gm)
            results_gm[i] = t_gm

            start_time = time.time()
            weights = pairwise.rbf_kernel(
                x_train_shuffled, x_train_shuffled, gamma=gamma
            )
            adversarial.best_to_flip(n_flips, weights, n_labelled)
            t_im = time.time() - start_time
            print(
                f"[{i}/{repetition}][{ds_name}][{p_labelled}][fb:{flips_p}]\t IM {t_im:2f}s"
            )
            # print("---Influence %s seconds ---" % t_im)
            results_im[i] = t_im
            np.savetxt(f"time_pm_{dataset}_{p_labelled}", results_gm)
            np.savetxt(f"time_im_{dataset}_{p_labelled}", results_im)

def transductive_accuracy():

    for ds_name, attr in config.datasets.items():
        x_train, x_test, y_train, y_test = attr["loader_function"]()
        random_seed = config.seed
        gamma = attr["gamma"]
        max_iter = attr["iter"]
        dataset = ds_name
        randomState = np.random.RandomState(random_seed)

        p_labelled = config.p_labelled
        flip_budgets = config.flip_budgets
        for i, p_l in enumerate(p_labelled):
            n_labelled = int(y_train.shape[0] * p_l)
            (
                y_train_shuffled,
                x_train_shuffled,
                labels_unlabelled,
            ) = ds.unlabel_shuffle_training_set(
                y_train, x_train, n_labelled, randomState
            )

            lp = LabelPropagationCached(gamma=gamma, max_iter=max_iter)
            # ls = LabelSpreading(gamma=gamma, max_iter=10)
            gssl = "lp"
            weights = pairwise.rbf_kernel(
                x_train_shuffled, x_train_shuffled, gamma=gamma
            )
            flip_budget = int(0.2 * n_labelled)
            result_gm_t = np.empty((len(flip_budgets),))
            result_im_t = np.empty((len(flip_budgets),))
            result_gm_i = np.empty((len(flip_budgets),))
            result_im_i = np.empty((len(flip_budgets),))

            print(
                f"Transductive & inductive study on {dataset} [{p_l} labelled] for max_flip {flip_budget} with {gssl}"
            )
            for i, flips_p in enumerate(flip_budgets):
                n_flips = int(flips_p * n_labelled)
                print(f"{flips_p}:{n_flips}/{n_labelled}")
                acc_t, acc_i = multiple_flips_greedy(
                    x_train_shuffled,
                    labels_unlabelled,
                    lp,
                    x_train_shuffled[n_labelled:],
                    y_train_shuffled[n_labelled:],
                    n_flips,
                    n_labelled,
                    gamma=gamma,
                    x_test_t=x_test,
                    y_test_t=y_test,
                    clf=SimpleMlp(
                        random_state=2020, impl="tf", input_shape=(x_train.shape[1],)
                    ),
                )
                print(
                    f"Greedy method : [{i}]=>[transduction {acc_t}][induction {acc_i}]"
                )
                result_gm_t[i] = acc_t
                result_gm_i[i] = acc_i

                # acc_t, acc_i = adversarial.multiple_flips_accuracy_sklearn(
                #     x_train_shuffled,
                #     labels_unlabelled,
                #     lp,
                #     x_train_shuffled[n_labelled:],
                #     y_train_shuffled[n_labelled:],
                #     n_flips,
                #     weights=weights,
                #     x_test_t=x_test,
                #     y_test_t=y_test,
                #     clf=SimpleMlp(
                #         random_state=2020, impl="tf", input_shape=(x_train.shape[1],)
                #     ),
                # )
                # result_im_t[i] = acc_t
                # result_im_i[i] = acc_i
                # print(
                #     f"Influence method : [{i}]=>[transduction {acc_t}][induction {acc_i}]"
                # )
                np.savetxt(f"{gssl}_{dataset}_{p_l}_gm_t", result_gm_t)
                # np.savetxt(f"{gssl}_{dataset}_{p_l}_im_t", result_im_t)
                np.savetxt(f"{gssl}_{dataset}_{p_l}_gm_i", result_gm_i)
                # np.savetxt(f"{gssl}_{dataset}_{p_l}_im_i", result_im_i)

def inductive_accuracy():
    randomState = np.random.RandomState(random_seed)
    # x_train, x_test, y_train, y_test = ds.mnist17_prep()
    # x_train, x_test, y_train, y_test = ds.mnist15_prep()
    # x_train, x_test, y_train, y_test = ds.mnist178_prep()
    x_train, x_test, y_train, y_test = ds.rcv_prep_f()
    # x_train, x_test, y_train, y_test = ds.cifarBinary_prep()
    dataset = "rcv"

    p_labelled = 0.25
    flip_budgets = [0, 0.05, 0.10, 0.15, 0.2]
    n_labelled = int(y_train.shape[0] * p_labelled)

    (
        y_train_shuffled,
        x_train_shuffled,
        labels_unlabelled,
    ) = ds.unlabel_shuffle_training_set(y_train, x_train, n_labelled, randomState)

    lp = LabelPropagationCached(gamma=gamma, max_iter=max_iter)
    # ls = LabelSpreading(gamma=gamma, max_iter=10)
    # weights = pairwise.rbf_kernel(x_train_shuffled, x_train_shuffled, gamma=gamma)
    gssl = "lp"
    result_gm = np.empty((len(flip_budgets),))
    result_im = np.empty((len(flip_budgets),))
    result_rm = np.empty((len(flip_budgets),))
    flip_budget = int(0.2 * n_labelled)
    print(f"Inductive study on {dataset} for flip budget [{flip_budgets}] with {gssl}")
    for i, flips_p in enumerate(flip_budgets):
        n_flips = int(flips_p * n_labelled)
        print(f"{flips_p}:{n_flips}/{n_labelled}")
        acc = multiple_flips_random(
            x_train_shuffled,
            labels_unlabelled,
            lp,
            x_test,
            y_test,
            n_flips,
            n_labelled,
            seed=2020,
        )
        print(f"random [{i}] [{acc}]")
        result_rm[i] = acc
        _, acc = multiple_flips_greedy(
            x_train_shuffled,
            labels_unlabelled,
            lp,
            x_test,
            y_test,
            n_flips,
            n_labelled,
            gamma=gamma,
        )
        print(f"greedy [{i}] [{acc}]")
        result_gm[i] = acc

        # acc = adversarial.multiple_flips_accuracy_rfc(
        #     x_train_shuffled,
        #     labels_unlabelled,
        #     ls,
        #     x_test,
        #     y_test,
        #     i,
        #     weights=weights,
        # )
        # result_im[i] = acc
        # print(f"influence [{i}] [{acc}]")

        np.savetxt(f"rfc_{gssl}_{dataset}_{p_labelled}_rm", result_rm)
        np.savetxt(f"rfc_{gssl}_{dataset}_{p_labelled}_gm", result_gm)
        # np.savetxt("induc_im_15_ls", result_im)

def main():
    # efficiency()
    transductive_accuracy()
    # inductive_accuracy()

if __name__ == "__main__":
    main()


import os
import json
import pygit2
from fnmatch import fnmatch

from pygit2 import GIT_FILEMODE_BLOB
from pygit2 import GIT_RESET_HARD
from pygit2 import init_repository
from pygit2 import IndexEntry
from pygit2 import Signature
from plural.models.meta.edges import edge_has_index
from plural.models.meta.vertices import vertex_has_index
from plural.models.meta.edges import is_edge_subclass
from plural.models.meta.vertices import is_vertex_subclass
from plural.models.edges import Edge
from plural.models.vertices import Vertex
from plural.models.edges import resolve_edge_name
from plural.models.vertices import resolve_vertex_name
from plural.util import generate_uuid
from plural.util import serialize_commit
from plural.util import AutoCodec

class PluralStore(object):
    """Data store that manipulates a single git repository

    :param path: ``bytes`` - the path to the git repository ``string``
    :param bare: ``bool`` - manipulate git objects only
    :param author_name: ``unicode`` - the name of the default git author.
    :param author_email: ``unicode`` - the email of the default git author.
    """
    def __init__(self, path=None, bare=False,
                 author_name='hexastore',
                 author_email='hexastore@git',
                 default_branch='refs/heads/master',
                 repository=None):
        path = path or self.__class__.__name__.lower()
        self.path = path
        self.bare = bare
        self.repository = repository or init_repository(path, bare=bare)
        self.author = Signature(author_name, author_email)
        self.commiter = Signature(author_name, author_email)
        self.queries = []
        self.default_branch = default_branch

    def add_remote(self, name, url):
        """adds a remote repository

        :param name: ``bytes``
        :param url: ``bytes``
        """
        self.repository.remotes.create(name, url)

    def serialize(self, obj):
        """serializes an object, meant for internal use only.

        :param obj: a hashable object
        :returns: ``string``
        """
        default = AutoCodec().encode
        return json.dumps(obj, sort_keys=True, default=default)

    def deserialize(self, string):
        """deserialize a string into an object, meant for internal use only.

        :param string: a hashable object
        :returns: ``a hashable object``
        """
        return json.loads(string)

    def iter_versions(self, branch='master'):
        """iterates over all the commits of the repo
        :param branch: the branch name
        :returns: an iterator of dictionaries with commit metadata
        """
        for commit in self.repository.walk(self.repository.head.target, pygit2.GIT_SORT_TOPOLOGICAL):
            yield serialize_commit(commit)

    def get_versions(self, branch='master'):
        """same :py:meth:`as iter_versions` but returns a list
        :returns: a list
        """
        results = list(self.iter_versions(branch))
        return results

    def add_spo(self, edge, predicate, data):
        """creates a staged entry of edge, predicate and object

        :param edge:
        :param predicate:
        :param data:
        :returns: ``bytes`` - the blob id
        """
        edge = resolve_edge_name(edge)
        blob_id = self.repository.create_blob(data)
        entry = IndexEntry(os.path.join(edge, predicate), blob_id, GIT_FILEMODE_BLOB)
        self.repository.index.add(entry)
        return blob_id

    def create(self, element, **obj):
        if is_edge_subclass(element):
            new = self.create_edge(element, **obj)
            self.queries.append(
                ' '.join(map(bytes, ['CREATE EDGE', repr(new)]))
            )
            return new
        elif is_vertex_subclass(element):
            new = self.create_vertex(element, **obj)
            self.queries.append(
                ' '.join(map(bytes, ['CREATE VERTEX', repr(new)]))
            )
            return new

    def create_vertex(self, vertex, **obj):
        """creates a staged vertex entry including its indexed fields.

        :param vertex: a string or a :py:class:`Vertex` subclass reference
        :param ``**kw``: the field values
        :returns: an instance of the given vertex

        """
        vertex = resolve_vertex_name(vertex)
        predicate_ids = []

        vertex_uuid = obj.pop('uuid', generate_uuid())
        obj['uuid'] = vertex_uuid

        vertex_data = self.serialize(obj)
        object_hash = bytes(pygit2.hash(vertex_data))
        object_path = os.path.join(vertex, 'objects')

        id_path = os.path.join(vertex, '_ids')
        uuid_path = os.path.join(vertex, '_uuids')

        original_obj = obj.copy()
        origin = obj.pop('origin')
        target = obj.pop('target')

        indexes = {}
        for key in obj.keys():
            value = obj.get(key, None)
            if vertex_has_index(vertex, key):
                indexes[key] = value

            predicate_path = os.path.join(vertex, 'indexes', key)
            predicate_ids.append(self.add_spo(predicate_path, object_hash, value))

        self.add_spo(id_path, vertex_uuid, object_hash)
        self.add_spo(uuid_path, object_hash, vertex_uuid)

        origin_name = resolve_edge_name(origin)
        target_name = resolve_edge_name(target)
        RelationhipModel = Vertex.definition(vertex)

        label = RelationhipModel.label
        # call('Car/incoming/bought_by/Person', 'chuck-uuid', 'car-uuid'),
        # call('___vertices___/Car/bought_by/Person', 'chuck-uuid', 'car-uuid'),
        path_templates = {
            'incoming': '{to}/incoming/{label}/{from}',
            'outgoing': '{from}/outgoing/{label}/{to}',
            'indirect': '{}/indirect/{label}/{}',
        }
        vertex_path_template = path_templates[RelationhipModel.direction]

        ctx = {
            'label': label
        }
        direction = RelationhipModel.direction
        # self.add_spo(object_path, object_hash, vertex_data)

        if direction == 'incoming':
            from_uuid = origin.uuid
            ctx['from'] = origin_name
            to_uuid = target.uuid
            ctx['to'] = target_name
            vertex_path = vertex_path_template.format(**ctx)
            self.add_spo(vertex_path, from_uuid, to_uuid)

        elif direction == 'outgoing':
            from_uuid = target.uuid
            ctx['from'] = target_name
            to_uuid = origin.uuid
            ctx['to'] = origin_name
            vertex_path = vertex_path_template.format(**ctx)
            self.add_spo(vertex_path, from_uuid, to_uuid)

        elif direction == 'indirect':
            from_uuid = target.uuid
            to_uuid = origin.uuid

            path = vertex_path_template.format(target_name, origin_name, **ctx)
            self.add_spo(path, from_uuid, to_uuid)

            path = vertex_path_template.format(origin_name, target_name, **ctx)
            self.add_spo(path, to_uuid, from_uuid)

        return RelationhipModel.from_data(vertex, **original_obj)

    def create_edge(self, edge, **obj):
        """creates a staged edge entry including its indexed fields.

        :param edge: a string or a :py:class:`Edge` subclass reference
        :param ``**kw``: the field values
        :returns: an instance of the given edge

        """
        predicate_ids = []

        edge = resolve_edge_name(edge)
        edge_uuid = obj.pop('uuid', generate_uuid())
        obj['uuid'] = edge_uuid

        edge_data = self.serialize(obj)
        object_hash = bytes(pygit2.hash(edge_data))
        object_path = os.path.join(edge, 'objects')

        id_path = os.path.join(edge, '_ids')
        uuid_path = os.path.join(edge, '_uuids')

        indexes = {}
        for key in obj.keys():
            value = obj.get(key, None)
            if edge_has_index(edge, key):
                indexes[key] = value

            predicate_path = os.path.join(edge, 'indexes', key)
            predicate_ids.append(self.add_spo(predicate_path, object_hash, value))

        self.add_spo(object_path, object_hash, edge_data)
        self.add_spo(id_path, edge_uuid, object_hash)
        self.add_spo(uuid_path, object_hash, edge_uuid)

        return Edge.from_data(edge, **obj)

    def save_nodes(self, *nodes):
        """creates staged entries for all the given edge nodes, regardless of type

        :param ``*nodes``: a list of edge instances
        """

        result = []
        for node in nodes:
            n = self.create_edge(node.__class__.__name__, **node.to_dict())
            result.append(n)

        return result

    def merge(self, *nodes, **kw):
        """saves and commits all given nodes

        :param ``*nodes``: a list of edge instances
        :param auto_commit: ``bool`` - default: ``True``
        :returns: a list with the created nodes
        """
        auto_commit = kw.pop('auto_commit', True)
        result = self.save_nodes(*nodes)
        if auto_commit:
            self.commit()
        return result

    def trace_path(self, entries):
        return map(lambda entry: (entry.path, bytes(entry.oid)), entries)

    def glob(self, pattern, treeish=None):
        treeish = treeish or self.repository.index
        return filter(lambda entry: fnmatch(entry.path, pattern), treeish)

    def scan_all(self, edge_name=None, treeish=None, pattern='*'):
        """scans all nodes

        :returns: a generator that produces :py:class:`Edge` instances with the scanned data
        """
        edge_name = resolve_edge_name(edge_name)
        treeish = treeish or self.repository.index
        pattern = os.path.join(edge_name, 'objects', pattern)
        for entry in self.glob(pattern):
            blob = self.repository[entry.oid]
            data = self.deserialize(blob.data)
            yield Edge.from_data(edge_name, **data)

    def delete(self, *nodes, **kw):
        """deletes and (optionally) commits all given nodes

        :param ``*nodes``: a list of edge instances to be deleted
        :param auto_commit: ``bool`` - default: ``False``
        :returns: a list with the deleted nodes
        """
        auto_commit = kw.pop('auto_commit', False)
        deleted = []
        for node in nodes:
            for path, oid in self.trace_path(self.glob('*{}*'.format(node.uuid))):
                map(self.repository.index.remove, node.get_related_blob_paths(self.repository))
                self.queries.append(
                    ' '.join(map(bytes, ['DELETE', node]))
                )
            deleted.append(node)

        if auto_commit:
            self.commit()

        return nodes

    def get_edge_by_uuid(self, edge_name, uuid):
        """retrieves a edge by id

        :param edge_name: the edge name or type
        :param uuid: the uuid value
        """
        edge_name = resolve_edge_name(edge_name)
        pattern = os.sep.join([edge_name, '_ids', uuid])
        for entry in self.glob(pattern):
            edge_blob_id = self.repository[entry.oid].data
            blob = self.repository[edge_blob_id]
            return Edge.from_data(edge_name, **self.deserialize(blob.data))

    def get_vertex_by_uuid(self, vertex_name, uuid):
        """retrieves a vertex by id

        :param vertex_name: the vertex name or type
        :param uuid: the uuid value
        """
        vertex_name = resolve_vertex_name(vertex_name)
        pattern = os.sep.join([vertex_name, '_ids', uuid])
        for entry in self.glob(pattern):
            vertex_blob_id = self.repository[entry.oid].data
            blob = self.repository[vertex_blob_id]
            return Vertex.from_data(vertex_name, **self.deserialize(blob.data))

    def match_edges_by_index(self, edge_name, field_name, match_callback):
        """retrieves multiple edges by indexed field

        :param edge_name: the edge name or type
        :param uuid: the uuid value
        """
        edge_name = resolve_edge_name(edge_name)
        pattern = os.sep.join([edge_name or '*', 'indexes', '*'])
        # for index, oid in filter(lambda (index, oid): field_name in index, self.glob(pattern)):
        for index, oid in self.trace_path(self.glob(pattern)):
            path, blob_id = os.path.split(index)
            edge_name = path.split(os.sep)[0]
            value = self.repository[oid].data
            if match_callback(value):
                blob = self.repository[blob_id]
                data = self.deserialize(blob.data)
                Definition = Edge.definition(edge_name)
                yield Definition(**data)

    def match_vertices_by_index(self, vertex_name, field_name, match_callback):
        """retrieves multiple vertices by indexed field

        :param vertex_name: the vertex name or type
        :param uuid: the uuid value
        """
        vertex_name = resolve_vertex_name(vertex_name)
        pattern = os.sep.join([vertex_name or '*', 'indexes', '*'])
        # for index, oid in filter(lambda (index, oid): field_name in index, self.glob(pattern)):
        for index, oid in self.trace_path(self.glob(pattern)):
            path, blob_id = os.path.split(index)
            vertex_name = path.split(os.sep)[0]
            value = self.repository[oid].data
            if match_callback(value):
                blob = self.repository[blob_id]
                data = self.deserialize(blob.data)
                Definition = Vertex.definition(vertex_name)
                yield Definition(**data)

    def commit(self, query=None):
        """creates a commit with the staged objects"""
        author = None

        if not query:
            query = '\n'.join(sorted(set(self.queries)))

        # commit
        tree_id = self.repository.index.write_tree()
        parent_commits = []
        if self.repository.references.objects:
            parent_commits = [self.repository.head.target]

        commit_id = self.repository.create_commit(
            self.default_branch,
            author or self.author,
            self.commiter,
            query,
            tree_id,
            parent_commits,
        )
        self.queries = []
        self.repository.head.set_target(commit_id)
        if not self.bare:
            self.repository.reset(commit_id, GIT_RESET_HARD)
        return commit_id


import boto3
from decimal import Decimal, getcontext

def calculate_cost():

    client = boto3.client('ce')
    result = client.get_cost_and_usage(
        TimePeriod = {
            'Start': '2020-08-06',
            'End': '2020-08-07'
        },
        Granularity = 'DAILY',
        Filter =
                {
                    "Dimensions": {
                        "Key": "SERVICE",
                        "Values": ['AWS Lambda']
                    }
                },
        Metrics = ["BlendedCost"],
        GroupBy = [
            {
                'Type': 'TAG',
                'Key': 'app'
            }
        ]
    )
    resultsByTime = result.get('ResultsByTime')
    for item in resultsByTime:
        if item.get('Groups'):
            #print(item)
            oneDayCost = item.get('Groups')[0].get('Metrics').get('BlendedCost').get('Amount')
            #print(f"{oneDayCost}")
            return oneDayCost

def push_cost_metric_to_cloudwatch(oneDayCost):
    """
    Sends custom cost metric to cloudwatch AWS/Lambda namespace.
    :param lambdas_data: List of Lambda functions data.
    :return: None.
    """
    cloudwatch = boto3.client('cloudwatch', 'us-west-2')

    response = cloudwatch.put_metric_data(
        MetricData = [
            {
                'MetricName': 'LastOneDayCost',
                'Dimensions': [
                    {
                        'Name': 'By Function Name',
                        'Value': 'LambdaLogging'
                    }
                ],
                'Unit': 'None',
                'Value': oneDayCost
            },
        ],
        Namespace='Lambda'
    )
    print(f"{response}")

if __name__ == '__main__':
   oneDayCost = calculate_cost()
   getcontext().prec = 20
   #oneDayCost = Decimal(oneDayCost)
   oneDayCost = '{0:1f}'.format(Decimal(oneDayCost))
   print(f"{float(oneDayCost)}")
   #push_cost_metric_to_cloudwatch(oneDayCost)


import numpy as np

import cv2
import torch
from torchvision import transforms as T
from app.features.utils import ModelUtils
from app.models.model import load_model, device

def predict(filename):
    print("Loading PyTorch model")
    model = load_model()

    data = {"success": False}
    classes = ['_', 'apple', 'orange', 'banana']

    # get image
    img = cv2.imread(filename)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32)
    # img = cv2.resize(img, (480, 480), cv2.INTER_AREA)
    img /= 255.0

    # Define a transform to convert the image to tensor
    transform = T.transforms.ToTensor()

    # Convert the image to PyTorch tensor
    img2 = transform(img)

    utils = ModelUtils()

    with torch.no_grad():
        prediction = model([img2.to(device())])[0]
        prediction = utils.nms(prediction, threshold=0.3)
    source_img = cv2.imread(filename)
    source_img = cv2.cvtColor(source_img, cv2.COLOR_BGR2RGB)

    utils.plot_box(filename, source_img, prediction, classes)

    data["prediction"] = classes[prediction['labels'][0]]
    data["success"] = True
    data["filename"] = filename
    # os.remove(f.filename)
    return data


import requests
import bs4
result = requests.get('https://en.wikipedia.org/wiki/Machine_learning')
soup = bs4.BeautifulSoup(result.text, 'lxml')
items = soup.select('.toctext')
for item in items:
    print(item.text)

count = 0
for image in soup.select('.thumbimage'):
    image= requests.get('http:'+image['src'])
    f=open('./assets/images/image_'+str(count)+'.jpg','wb')
    f.write(image.content)
    count +=1


""" Circular Linked List Implementation

  ---> Head --> A -> B -> C -> D ---|
  |---------------------------------|

 This code itself is self explanatory...

 @samarjit_debnath
 """

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class CircularLinkedList:
    def __init__(self):
        self.head = None

    def insert_begin(self, new_node):
        cur = self.head
        new_node.next = self.head
        if self.head is None:
            new_node.next = new_node
        else:
            while cur.next != self.head:
                cur = cur.next
            cur.next = new_node
        self.head = new_node

    def insert_end(self, new_node):
        if not self.head:
            self.head = new_node
            self.head.next = self.head
        else:
            temp = self.head
            while temp.next != self.head:
                temp = temp.next
            temp.next = new_node
            new_node.next = self.head

    def print_list(self):
        if not self.head:
            print('Empty List, Nothing to Print')
            return
        current = self.head
        while current:
            print(current.data, end="  ")
            current = current.next
            if current == self.head:
                break

    def get_id(self):
        cur = self.head
        while cur:
            print('\n' + str(cur.data) + ': ' + str(id(cur)))
            cur = cur.next
            if cur == self.head:
                break

    def is_list_empty(self):
        res = False
        if self.head is None:
            res = True
        return res

    def list_len(self):
        cur = self.head
        length = 0
        while True:
            cur = cur.next
            length += 1
            if cur == self.head:
                break
        return length

    def delete_begin(self):
        if self.is_list_empty() is False:
            cur = self.head
            while cur.next != self.head:
                cur = cur.next
            cur.next = self.head.next
            self.head = self.head.next
        else:
            print('Empty')

    def delete_end(self):
        if self.is_list_empty() is False:
            cur = prev = self.head
            while cur.next != self.head:
                prev = cur
                cur = cur.next
            prev.next = self.head

    def delete_at_node(self, position):
        if position < 0 or position > self.list_len():
            print('Invalid Entry')
            return
        if self.is_list_empty() is False:
            if position == 0:
                self.delete_begin()
                return
        cur = prev = self.head
        cur_pos = 0
        while True:
            if cur_pos == position:
                prev.next = cur.next
                cur.next = None
                break
            prev = cur
            cur = cur.next
            cur_pos += 1

    def split_list(self):
        size = self.list_len()
        if size == 0:
            print('Empty')
            return
        if size == 1:
            return self.head

        prev = None
        counter = 0
        cur = self.head
        mid = size // 2

        while cur and counter < mid:
            counter += 1
            prev = cur
            cur = cur.next
        prev.next = self.head

        split_list = CircularLinkedList()
        while cur.next != self.head:
            split_list.insert_end(Node(cur.data))
            cur = cur.next
        split_list.insert_end(Node(cur.data))

        self.print_list()
        print()
        split_list.print_list()

    def josephus_circle(self, step):
        cur = self.head
        while self.list_len() > 1:
            count = 1
            while count != step:
                cur = cur.next
                count += 1
            print('Removed: ' + str(cur.data))
            self.delete_at_node(count)
            cur = cur.next

    def has_cycle(self):
        if self.head is None:
            return False

        ptr = self.head
        fastptr = self.head

        while ptr is not None and fastptr is not None and fastptr.next is not None:
            if ptr == fastptr:
                return True
            ptr = ptr.next
            fastptr = fastptr.next.next
        return False

# Driver code change this in accordance with your need...

if __name__ == '__main__':
    node = Node(1)
    node2 = Node(2)
    llist = CircularLinkedList()
    llist.insert_end(node)
    llist.insert_end(node2)
    llist.insert_begin(Node(10))
    llist.insert_begin(Node(20))
    llist.print_list()
    llist.get_id()
    print("\nLength: {}".format(llist.list_len()))
    llist.split_list()
    llist.delete_at_node(1)
    llist.print_list()
    llist.josephus_circle(2)
    print(llist.has_cycle())


#SOLUTION OF P21
'''P21 (*) Insert an element at a given position into a list.
Example:
* (insert-at 'alfa '(a b c d) 2)
(A ALFA B C D)'''

my_list = ['a','b','c','d','e']
#TAKE THE ELEMENT TO BE ADDED AND WHERE TO ADD AS INPUT
elem = input("Enter the data to add = ")
pos= int(input('Where to add  = '))

def add_element(your_list,element,position):
    length = len(your_list)
    out_list = list()

    if position <= len(your_list) and position!= 0:             #GIVEN INDEX SHOULD BE IN RANGE & NOT ZERO
        for item in range(length):
            if position == item+1:                              #WHEN INDICES MATCH,ADD THE ELEMENT INTO RESULT LIST
                out_list.append(element)
            else:
                out_list.append(your_list[item])                #TILL THEN ADD OTHER ELEMENTS
        return (out_list)
    else:
        print('Invalid input ')

result = add_element(my_list,elem,pos)
print(result)


import os
import unittest
import subprocess
import random
try: # Try to import the local version first (usually works when executed from the command line with Python directly)
    import DenseGemmEvaluation as DenseGemm
except ImportError: # Only works when you execute it with the '-m unittest' parameter from stonne/stonne directory
    import tests.TileGenerator.StonneMapper.DenseGemmEvaluation as DenseGemm

# Available generators for DenseGEMM layers
GENERATOR = ["StonneMapper", "mRNA"]
GENERATOR = GENERATOR[0]

# Tests parameters
PERFORMANCE_TOLERANCE = 0.2
NUMBER_RANDOM_TESTS = 10

class TestDenseGemm(unittest.TestCase):
    """
    Test cases to test the generation of StonneMapper for DenseGemm layers.
    For each test, it runs a simulation of the layer generating the tile with StonneMapper.
    Later, it searches for the best possible tile for this layer.
    At last, it compares the generated tile results with the best possible tile results,
    passing the test only if the speedup fits in the tolerance margin.
    Note: accumulation_buffer is always 1
    """

    @classmethod
    def setUpClass(cls):
        # builds the STONNE executable
        proc = subprocess.Popen(["make", "all"])
        proc.wait()

    def test01_DenseGemmBasic1(self):
        # - Best tile (using powers of 2): T_M=1, T_N=8, T_K=2
        # - Total Cycles for best tile: 560
        self.assertTrue(DenseGemm.evaluate(testname='Basic1',
                num_ms=16, dn_bw=8, rn_bw=8, M=16, N=16, K=16, tolerance=PERFORMANCE_TOLERANCE, generator=GENERATOR))

    def test02_DenseGemmBasic2(self):
        # - Best tile (using powers of 2): T_M=1, T_N=8, T_K=2
        # - Total Cycles for best tile: 536
        self.assertTrue(DenseGemm.evaluate(testname='Basic2',
                num_ms=16, dn_bw=8, rn_bw=8, M=32, N=8, K=16, tolerance=PERFORMANCE_TOLERANCE, generator=GENERATOR))

    def test03_DenseGemmBasic3(self):
        # - Best tile (using powers of 2): T_M=1, T_N=8, T_K=2
        # - Total Cycles for best tile: 72
        self.assertTrue(DenseGemm.evaluate(testname='Basic3',
                num_ms=16, dn_bw=8, rn_bw=8, M=7, N=7, K=7, tolerance=PERFORMANCE_TOLERANCE, generator=GENERATOR))

    def test04_DenseGemmPowerOfTwo1(self):
        # - Best tile (using powers of 2): T_M=1, T_N=128, T_K=2
        # - Total Cycles for best tile: 4150
        self.assertTrue(DenseGemm.evaluate(testname='PowerOfTwo1',
                num_ms=256, dn_bw=256, rn_bw=256, M=128, N=256, K=32, tolerance=PERFORMANCE_TOLERANCE, generator=GENERATOR))

    def test05_DenseGemmPowerOfTwo2(self):
        # - Best tile (using powers of 2): T_M=1, T_N=8, T_K=16
        # - Total Cycles for best tile: 1042
        self.assertTrue(DenseGemm.evaluate(testname='PowerOfTwo2',
                num_ms=128, dn_bw=128, rn_bw=64, M=128, N=8, K=128, tolerance=PERFORMANCE_TOLERANCE, generator=GENERATOR))

    def test06_DenseGemmPowerOfTwo3(self):
        # - Best tile (using powers of 2): T_M=1, T_N=32, T_K=2
        # - Total Cycles for best tile: 2194
        self.assertTrue(DenseGemm.evaluate(testname='PowerOfTwo3',
                num_ms=64, dn_bw=64, rn_bw=64, M=16, N=64, K=128, tolerance=PERFORMANCE_TOLERANCE, generator=GENERATOR))

    def test07_DenseGemmRandom1(self):
        # - Best tile (using powers of 2): T_M=1, T_N=64, T_K=4
        # - Total Cycles for best tile: 4735
        self.assertTrue(DenseGemm.evaluate(testname='Random1',
                num_ms=256, dn_bw=256, rn_bw=64, M=116, N=317, K=32, tolerance=PERFORMANCE_TOLERANCE, generator=GENERATOR))

    def test08_DenseGemmRandom2(self):
        # - Best tile (using powers of 2): T_M=1, T_N=32, T_K=8
        # - Total Cycles for best tile: 5269
        self.assertTrue(DenseGemm.evaluate(testname='Random2',
                num_ms=256, dn_bw=256, rn_bw=64, M=116, N=347, K=32, tolerance=PERFORMANCE_TOLERANCE, generator=GENERATOR))

    def test09_DenseGemmRandom3(self):
        # - Best tile (using powers of 2): T_M=1, T_N=16, T_K=16
        # - Total Cycles for best tile: 5145
        self.assertTrue(DenseGemm.evaluate(testname='Random3',
                num_ms=256, dn_bw=256, rn_bw=64, M=116, N=333, K=32, tolerance=PERFORMANCE_TOLERANCE, generator=GENERATOR))

    def test10_DenseGemmGenerateRandomly(self):
        results = []
        for i in range(NUMBER_RANDOM_TESTS):
            num_ms = 2 ** random.randint(3, 9) # 8..512
            dn_bw = num_ms
            rn_bw = num_ms
            M = random.randint(1, 256)
            N = random.randint(1, 256)
            K = random.randint(1, 256)
            results.append(DenseGemm.evaluate(testname=f'RandomGenerated[{i}]',
                num_ms=num_ms, dn_bw=dn_bw, rn_bw=rn_bw, M=M, N=N, K=K, tolerance=PERFORMANCE_TOLERANCE, generator=GENERATOR))
        self.assertTrue(all(results))

# Main method to execute all testcases of StonneMapper for DenseGEMM/FC layers
if __name__ == "__main__":
    if not os.getcwd().endswith('stonne/stonne'):
        print("Please run this DenseGemm.evaluate script from the stonne/stonne directory")
        exit(1)

    unittest.main()


class Getdata:
    """
    Deze class is gemaakt voor het ophalen van data
    geef een uitleg van elke functie in de vorm van een comment
    """

    def __init__(self, myclient, mydb):
        self.myclient = myclient
        self.mydb = mydb

    def first_name_price(self):
        # Geef de naam en de prijs van het eerste product in de lijst
        return self.mydb.products.find_one({}, {"_id": 0, "name": 1, "price.selling_price": 1})

    def name_start_letter(self, letter):
        # Geef het eerste product terug, waar de naam begint met het gewenste letter
        for i in self.mydb.products.find({}, {"_id": 0, "name": 1}):
            var = i['name']
            if letter == var[0]:
                return var

    def average_price(self):
        totalprice = 0
        totalitems = 0

        for i in self.mydb.products.find({}, {"_id": 0, "price.selling_price": 1}):
            if i != {}:
                var = i["price"]
                newvar = var["selling_price"]

                # Pijzen die 0 zijn ook meenemen of overslaan.
                if newvar != 0:
                    totalprice += newvar
                    totalitems += 1

        return totalprice / totalitems


import sys
import os
import json
import torch
import numpy as np
import time

from opts import parse_args
from utils.logger import Logger
from data_funcs.data_loader import load_data
from utils.model_funcs import get_training_elements, evaluate_model, \
    get_lr_scheduler, get_optimiser, run_one_epoch, prox_step
from utils.checkpointing import save_checkpoint
from utils.utils import create_model_dir, create_metrics_dict, \
    metric_to_dict, init_metrics_meter, extend_metrics_dict

from models import RNN_MODELS

# os.environ["WANDB_API_KEY"] = 'INSERT KEY'
# os.environ["CUDA_LAUNCH_BLOCKING"] = '1'

def main(args):
    # system setup
    global CUDA_SUPPORT

    Logger.setup_logging(args.loglevel, logfile=args.logfile)
    logger = Logger()

    logger.debug(f"CLI args: {args}")

    if torch.cuda.device_count():
        CUDA_SUPPORT = True
    else:
        logger.warning('CUDA unsupported!!')
        CUDA_SUPPORT = False

    if not CUDA_SUPPORT:
        args.gpu = "cpu"

    if args.deterministic:
        # import torch.backends.cudnn as cudnn
        import os
        import random

        if CUDA_SUPPORT:
            # cudnn.deterministic = args.deterministic
            # cudnn.benchmark = not args.deterministic
            torch.cuda.manual_seed(args.manual_seed)
            torch.cuda.manual_seed_all(args.manual_seed)

        random.seed(args.manual_seed)
        np.random.seed(args.manual_seed)
        os.environ['PYTHONHASHSEED'] = str(args.manual_seed)
        torch.manual_seed(args.manual_seed)

    logger.info(f"Model: {args.model}, Dataset:{args.dataset}")

    # In case of DataParallel for .to() to work
    args.device = args.gpu[0] if type(args.gpu) == list else args.gpu

    # Load data sets
    trainset, testset, GC = load_data(args.data_path, args.dataset, args.seq_len)

    # It should work, it seems problem is only locally on my machine
    testloader = torch.utils.data.DataLoader(testset,
                                             batch_size=args.batch_size,
                                             num_workers=args.num_workers,
                                             shuffle=False,
                                             persistent_workers=True
                                             )
    args.input_size = next(iter(testloader))[0].shape[-1]

    if not args.evaluate:  # Training mode
        trainloader = torch.utils.data.DataLoader(
            trainset,
            batch_size=args.batch_size,
            num_workers=args.num_workers,
            shuffle=True,
            persistent_workers=True
        )

        init_and_train_model(args, trainloader, testloader, GC)

    else:  # Evaluation mode
        model, criterion, round = get_training_elements(
            args.model_name, args.input_size, args.seq_len, args.resume_from,
            args.load_best, args.gpu)

        metrics = evaluate_model(
            model, testloader, criterion, args.device, round,
            print_freq=10, metric_to_optim=args.metric,
            is_rnn=args.model in RNN_MODELS, GC=GC)

        metrics_dict = create_metrics_dict(metrics)
        logger.info(f'Validation metrics: {metrics_dict}')

def init_and_train_model(args, trainloader, testloader, GC):
    full_metrics = init_metrics_meter()
    model_dir = create_model_dir(args)
    # don't train if setup already exists
    if os.path.isdir(model_dir):
        Logger.get().info(f"{model_dir} already exists.")
        Logger.get().info("Skipping this setup.")
        return
    # create model directory
    os.makedirs(model_dir, exist_ok=True)
    # init wandb tracking
    # wandb.init(
    #     project="insert_project_name", entity="insert_entity", config=vars(args),
    #     name=str(create_model_dir(args, lr=False)), reinit=True)
    # save used args as json to experiment directory
    with open(os.path.join(create_model_dir(args), 'args.json'), 'w') as f:
        json.dump(vars(args), f, indent=4)

    is_rnn = args.model in RNN_MODELS
    model, criterion, current_round = get_training_elements(
        args.model, args.input_size, args.seq_len, args.resume_from, args.load_best, args.gpu)

    optimiser = get_optimiser(
        model.parameters(), args.optimiser, args.lr,
        args.momentum, args.weight_decay)

    scheduler = get_lr_scheduler(
        optimiser, args.rounds, args.lr_type)

    metric_to_optim = args.metric
    best_metric = np.inf
    train_time_meter = 0

    for i in range(args.rounds):
        start = time.time()
        metrics_meter = run_one_epoch(
            model, trainloader, criterion, optimiser, args.device,
            current_round, is_rnn, args.clip_grad, args.prox, args.gc_penalty)
        # Prox step to better detect Granger Causality
        if args.prox:
            prox_step(model, scheduler, args.gc_penalty)
        extend_metrics_dict(
            full_metrics, metric_to_dict(metrics_meter, i+1, 'train'))
        # wandb.log(metric_to_dict(metrics_meter, i+1, 'train', False))
        train_time = time.time() - start
        train_time_meter += train_time
        # Track timings across epochs
        Logger.get().debug(f'Epoch train time: {train_time}')

        if i % args.eval_every == 0 or i == (args.rounds - 1):
            metrics = evaluate_model(
                model, testloader, criterion, args.device, current_round,
                print_freq=10, metric_to_optim=metric_to_optim, is_rnn=is_rnn,
                GC=GC)
            extend_metrics_dict(
                full_metrics, metric_to_dict(metrics, i+1, 'test'))
            # wandb.log(metric_to_dict(metrics, i+1, 'test', False))
            avg_metric = metrics[metric_to_optim].get_avg()
            # Save model checkpoint
            model_filename = (f"{args.model}_{args.run_id}_checkpoint"
                              f"_{current_round:0>2d}.pth.tar")
            is_best = avg_metric < best_metric
            save_checkpoint(model, model_filename, is_best=is_best, args=args,
                            metrics=metrics, metric_to_optim=metric_to_optim)
            if is_best:
                best_metric = avg_metric

            if np.isnan(metrics['loss'].get_avg()):
                Logger.get().info(
                    'NaN loss detected, aborting training procedure.')
                return

        Logger.get().info(
            f'Current lr: {scheduler.get_last_lr()}')
        scheduler.step()
        current_round += 1
        torch.cuda.empty_cache()

    Logger.get().debug(
        f'Average epoch train time: {train_time_meter / args.rounds}')
    #  store the run
    with open(os.path.join(
            create_model_dir(args), 'full_metrics.json'), 'w') as f:
        del full_metrics['train_GC']
        del full_metrics['test_GC']
        del full_metrics['train_GC_full']
        del full_metrics['test_GC_full']
        json.dump(full_metrics, f, indent=4)

    Logger.get().debug(f'GC model:{model.GC()}')
    Logger.get().debug(f'GC true:{GC}')

if __name__ == "__main__":
    args = parse_args(sys.argv)
    main(args)
    torch.cuda.empty_cache()
    assert torch.cuda.memory_allocated() == 0


#!/usr/bin/python3
from pyspark.sql import SparkSession
from pyspark.sql.functions import *
from pyspark.sql.types import *
from pyspark.sql.window import Window
from pyspark.sql import Row

def triggerPeriodGroup(rdd):
    temp = rdd.asDict()
    global i
    if temp["trigger_period"] > 120:
        i+=1
    temp["trigger_period_group"]=i
    return Row(**temp)

if __name__ == "__main__":
    # Create SparkSession
    spark = SparkSession \
        .builder \
        .getOrCreate()

    # Create a DataFrame from a JSON file
    event_df = spark.read.json("hdfs://localhost/user/cloudera/SparkSQLBehaviorsAnalysis4Linechatbot/data/eventLog")

    # Explode all elements in the column (event_df["events"]) -> all record represent every behavior event
    allbehaviors_df = event_df.select(explode(event_df["events"]).alias("events"))

    # Select columns which will be analyzed later
    clear_df = allbehaviors_df.select(allbehaviors_df["events"]["timestamp"].alias("timestamp"), \
                                      from_unixtime(allbehaviors_df["events"]["timestamp"]/1000, "yyyy-MM-dd HH:mm:ss").alias("event_time"), \
                                      allbehaviors_df["events"]["source"]["userId"].alias("userid"), \
                                      allbehaviors_df["events"]["type"].alias("type"), \
                                      allbehaviors_df["events"]["message"]["text"].alias("messageEvent"), \
                                      allbehaviors_df["events"]["postback"].alias("postbackEvent"))

    # analysis(staying time)
    # trigger_period in clear_df
    window_clause1 = Window.partitionBy(clear_df["userid"]).orderBy(clear_df["timestamp"])

    window_specification1 = window_clause1.rowsBetween(-1, 0)

    clear_df = clear_df.withColumn("trigger_period", ((clear_df["timestamp"] - first(clear_df["timestamp"]).over(window_specification1))/1000))

    # triggerPeriodGroup_df
    i=0; triggerPeriodGroup_df = clear_df.rdd.map(triggerPeriodGroup).toDF(sampleRatio=0.2)

    # once_staying_time in triggerPeriodGroup_df
    window_clause2 = Window.partitionBy([triggerPeriodGroup_df["userid"], triggerPeriodGroup_df["trigger_period_group"]]) \
                           .orderBy(triggerPeriodGroup_df["trigger_period_group"])

    window_specification2 = window_clause2.rowsBetween(Window.unboundedPreceding, Window.unboundedFollowing)

    triggerPeriodGroup_df = triggerPeriodGroup_df.withColumn("once_staying_time", \
                            (sum(triggerPeriodGroup_df["trigger_period"]).over(window_specification2) - \
                             max(triggerPeriodGroup_df["trigger_period"]).over(window_specification2))) \
                             .orderBy(triggerPeriodGroup_df["userid"])

    # staying_time
    staying_time_df = triggerPeriodGroup_df.groupBy( [triggerPeriodGroup_df["userId"], triggerPeriodGroup_df["trigger_period_group"]]) \
                                           .agg({"once_staying_time":"avg"}) \
                                           .withColumnRenamed("avg(once_staying_time)", "once_staying_time") \
                                           .orderBy([triggerPeriodGroup_df["userId"], triggerPeriodGroup_df["trigger_period_group"]])

    staying_time_df = staying_time_df.select(staying_time_df["userId"], \
                                             staying_time_df["once_staying_time"])

    # save to json file
    staying_time_df.write.json("hdfs://localhost/user/cloudera/SparkSQLBehaviorsAnalysis4Linechatbot/analysisResults/staying_time_analysis")

    # Create a DataFrame from a csv file
    memberProfile_df = spark.read.csv("hdfs://localhost/user/cloudera/SparkSQLBehaviorsAnalysis4Linechatbot/data/memberProfile4MySQL", header=True)

    staying_time_with_memberProfile_df = staying_time_df.join(memberProfile_df, staying_time_df["userId"] == memberProfile_df["line_id"], 'left_outer') \
                                                        .select(staying_time_df["userId"], \
                                                                memberProfile_df["username"], \
                                                                memberProfile_df["sex"], \
                                                                staying_time_df["once_staying_time"]) \
                                                        .fillna({"username":"unknown", "sex":"unknown"})

    # save to json file
    staying_time_with_memberProfile_df.write.json("hdfs://localhost/user/cloudera/SparkSQLBehaviorsAnalysis4Linechatbot/analysisResults/staying_time_with_memberProfile_df")

#========================= (Launch Spark by 「Interactive Development Environment」)
# pyspark --master spark://172.21.0.2:7077

#========================= (Launch Spark by 「Shell Script」)
# spark-submit --master spark://172.21.0.2:7077 message_event_analysis.py


#!/usr/bin/env python3
import socket

# Initialize server
HOST = '127.0.0.1'
Socket = 12345
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST, Socket))
server.listen(5)

# Create List and Dict to store keys/values and
# instructions respectively
keystore = []
command_index_dict = {"get:": " retrieve key value", "put:": " set key value or create new key:value",
                "mappings:": " get all keys and values", "keyset:": " retrieve all keys",
                "values:": " retrieve all key values from store", "help:": " list all functions",
                "bye:": " close/quit program"}
# define variables
found = False
print("Server Initialized")

client, adrs=server.accept()
print(f"Connection from {adrs}")
client.send(bytes("Server is ready to receive", "utf-8"))

while True:
    command = client.recv(1024)
    command = command.decode("utf-8")
    try:
        if command == "get":
            client.send(bytes("Enter key name", "utf-8"))
            a = client.recv(1024)
            a = a.decode("utf-8")
            print(a)
            if len(keystore) == 0:
                client.send(bytes("The list is empty", "utf-8"))
            else:
                for dictionary in keystore:
                    for key, value in dictionary.items():
                        if key == a:
                            value = str(value)
                            client.send(bytes(value, "utf-8"))
                            print(value)
                            found = True
            if found == False:
                client.send(bytes("This value does not exist", "utf-8"))
            found = False
            print("get")
        elif command == "put":
            client.send(bytes("Enter key name", "utf-8"))
            x = client.recv(1024)
            client.send(bytes("Enter key value", "utf-8"))
            y = client.recv(1024)
            if len(keystore) == 0:
                keystore.append({x: y})
            else:
                for dictionary in keystore:
                    if x in dictionary:
                        dictionary[x] = y
                    else:
                        keystore.append({x: y})
            print("put")
        elif command == "mappings":
            # list all keys and values
            keystore = str(keystore)
            client.send(bytes(keystore, "utf-8"))
            print("mappings")
        elif command == "keyset":
            # retrieve all keys from key value store
            # print
            for dictionary in keystore:
                for key, value in dictionary.items():
                    key = str(key)
                    client.send(bytes(key, "utf-8"))
            print("keyset")
        elif command == "values":
            # retrieve all values from the key value store
            # iterate through each dict in the list
            for dictionary in keystore:
                # iterate through each dict and pull value
                for key, value in dictionary.items():
                    value = str(value)
                    client.send(bytes(value, "utf-8"))
                    print(value)
            print("values")
        elif command == "help":
            # print all available functions
            # instead of the keystore, access a different dict
            # iterate through the dictionary and print
            for func, description in command_index_dict.items():
                func = str(func)
                description = str(description)
                client.send(bytes(func, "utf-8"))
                client.send(bytes(description, "utf-8"))
                print(func, description)
            print("help")
        elif command == "bye":
            client.close()
            print("bye")
            break
    except KeyError:
        print("No such function exists")

# At this point the loop should stop,
# and the client should be closed.
server.close()
quit()


lst_0=[]
lst_01=[]
lst_02=[]
lst_03=[]
for i in range(0,2):
    str_0 = input("").split(" ")
    lst_0.append(str_0)

for i in lst_0:
    for j in i:
        lst_01.append(int(j))
    lst_02.append(lst_01)
    lst_01=[]

for i in lst_02[0]:
    for j in lst_02[1]:
        lst_03.append(i*j)

print(lst_03)


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('core', '0004_review'),
    ]

    operations = [
        migrations.AddField(
            model_name='location',
            name='seating',
            field=models.IntegerField(blank=True, null=True, choices=[(0, b'None'), (1, b'Minimal'), (2, b'Some'), (3, b'Ample')]),
        ),
        migrations.AddField(
            model_name='location',
            name='wifi',
            field=models.IntegerField(blank=True, null=True, choices=[(0, b'None'), (1, b'Spotty'), (2, b'Strong')]),
        ),
    ]


from trusthouse.models.incident import Incident
from ..extensions import db, app
from datetime import datetime

def new_incident(category, description, address):
    """
    Creates a new incident entry, saving it into the Incident table.
    Each entry is linked to both Address & Maps tables.

    Returns the Incident object back to the user.
    """
    with app.app_context():
        new_incident_entry = Incident(
            category=category.lower(),
            description=description.lower(),
            date=datetime.now(),
            area=address,
        )
        db.session.add(new_incident_entry)
        db.session.commit()
    return new_incident_entry


''' Item Price Rule url'''
from django.urls import path
from .views import ItemPriceRuleViews, ItemPriceRuleUpdateViews, ItemPriceRuleMultipleStatusUpdate

urlpatterns = [
    path('multiple/', ItemPriceRuleMultipleStatusUpdate.as_view(),
         name='item_price_rule_multiple_status_update_delete'),
    path('<itemPriceRuleID>/', ItemPriceRuleUpdateViews.as_view(),
         name='item_price_rule_updtae'),
    path('', ItemPriceRuleViews.as_view(),
         name='item_price_rule_get_create'),

]


import subprocess
import sqlite3
import pandas as pd
import random
import time

def random_erasure(content):
    start_at = random.randint(0, 13)
    tmp = list(content)

    tmp[start_at*16:(start_at+1)*16] = '~'*16
    content = ''.join(tmp)
    return content

if __name__ == "__main__":
    # Connect to databases
    db_parity = sqlite3.connect("/home/pi/Workspace/receiver_server_docker_parity/parity_0.sqlite")
    db_0 = sqlite3.connect("/home/pi/Workspace/receiver_server_docker_parity/content_0.sqlite")
    db_1 = sqlite3.connect("/home/pi/Workspace/receiver_server_docker_parity/content_1.sqlite")
    db_com = sqlite3.connect("/home/pi/Workspace/receiver_server_docker/content_0.sqlite")

    parity_df = pd.read_sql('SELECT * FROM parity ORDER BY time DESC LIMIT 1', db_parity)

    # Connect to content database
    ## First Half
    db_0 = sqlite3.connect("/home/pi/Workspace/receiver_server_docker_parity/content_0.sqlite")
    for i in range(0,7):
        queryString = "select content, time from segment{} ORDER BY time DESC LIMIT 1".format(i)
        temp_df = pd.read_sql(queryString, db_0)
        parity_df = parity_df.merge(temp_df, on='time')
        if i != 0:
            parity_df['content'] = parity_df['content_x'] + parity_df['content_y']
            parity_df.drop(['content_x', 'content_y'], axis=1, inplace=True)

    ## Second Half
    db_1 = sqlite3.connect("/home/pi/Workspace/receiver_server_docker_parity/content_1.sqlite")
    for i in range(0,7):
        queryString = "select content, time from segment{} ORDER BY time DESC LIMIT 1".format(i)
        temp_df = pd.read_sql(queryString, db_1)
        parity_df = parity_df.merge(temp_df, on='time')
        parity_df['content'] = parity_df['content_x'] + parity_df['content_y']
        parity_df.drop(['content_x', 'content_y'], axis=1, inplace=True)

    # Connect to Comparison database
    com_df = pd.read_sql("select * from demo order by time desc limit 1", db_com)

    # simulate the condition if one segment is missing
    for index, row in parity_df.iterrows():
        row['content'] = random_erasure(row['content'])

    # correct and store the corrected result in column
    parity_df['corrected'] = "0"
    for index, row in parity_df.iterrows():
        res = subprocess.Popen(["/home/pi/Workspace/RSCODEC/rs_decoder",
                            row['content'], row['parity']], stdout=subprocess.PIPE)
        out = res.communicate()
        row['corrected'] = out[0].decode().strip('\n')

    # compare with the "origin" message
    com_df = com_df.merge(parity_df, on='num', suffixes=('_compare', '_merged'))
    com_df['check'] = com_df['content_compare'].eq(com_df['corrected'])

    for index, row in com_df.iterrows():
        print("Comparison String:     ", row['content_compare'])
        print("multidb-Merged String: ", row['content_merged'])
        print("RS_corrected String:   ", row['corrected'])
        print("Compare Result:        ", row['check'])


import datetime

import requests
import forecastio

import keys
import config

class OutsideData():
    def __init__(self):
        self.cached_temp = 70
        self.time_of_last_request = None
        self.minutes_5 = datetime.timedelta(minutes=config.temp_cache_frequency_minutes)

    def get_outside_temp(self, lat, lon):
        current_time = datetime.datetime.utcnow()
        if self.time_of_last_request is not None:
            time_from_last_request = current_time - self.time_of_last_request
            if time_from_last_request < self.minutes_5:
                return self.cached_temp, None

        try:
            temp = self.get_forecast_io_temp(lat, lon)
        except:
            error = 'failure of weather api this will probably resolve itself using last known outside temp'
            return self.cached_temp, error

        self.cached_temp = temp
        self.time_of_last_request = current_time
        return temp, None

    def get_forecast_io_temp(self, lat, lon):
        forecast = forecastio.load_forecast(keys.dark_sky_key, lat, lon)
        temp = forecast.currently().temperature
        return temp

    def get_open_weather_temp(lat, lon):
        uri = 'http://api.openweathermap.org/data/2.5/weather'
        params = {'lat': lat, 'lon': lon, 'APPID':keys.open_weather_key}
        result = requests.get(uri, params=params)
        if result.status_code != 200:
            print(result.status_code)
        else:
            results = result.json()
            temp_k = result.json()['main']['temp']
            temp_f = (temp_k - 273.15) * 1.8 + 32.0
        return temp_f

outside_data = OutsideData()


from flask import render_template, session, request, Markup, redirect
from app import app, LINK

from requests import post
from json import loads

@app.route('/sys_study')
@app.route('/sys_study/')
def sys_study():
	ip = request.remote_addr

	if 'token' in session:
		req = loads(post(LINK, json={
			'method': 'study.start',
			'token': session['token'],
			'ip': ip,
			'ladder': int(request.args.get('ladder')),
			'step': int(request.args.get('step')),
			'teacher': int(request.args.get('user')),
		}).text)

		if req['error']:
			return render_template('message.html', cont=req['message'])

		src = LINK + 'space/' + str(req['id'])
		return redirect(src)
	else:
		return redirect(LINK + 'login?url=' + url)


from typing import List, Optional

import random
import csv
import boto3
import uuid
import pytz
from datetime import datetime, timezone, timedelta
from boto3.dynamodb.conditions import Key, Attr
import constants

dynamodb = boto3.resource('dynamodb')
survey_table = dynamodb.Table('timbre_survey')

def put_to_na(row):
    if row['clip_a'] not in na[row['attribute']]:
        na[row['attribute']][row['clip_a']] = 1
    else:
        na[row['attribute']][row['clip_a']] += 1

    if row['clip_b'] not in na[row['attribute']]:
        na[row['attribute']][row['clip_b']] = 1
    else:
        na[row['attribute']][row['clip_b']] += 1

def put_to_dontknow(row):
    if row['clip_a'] not in dontknow[row['attribute']]:
        dontknow[row['attribute']][row['clip_a']] = 1
    else:
        dontknow[row['attribute']][row['clip_a']] += 1

    if row['clip_b'] not in dontknow[row['attribute']]:
        dontknow[row['attribute']][row['clip_b']] = 1
    else:
        dontknow[row['attribute']][row['clip_b']] += 1

na = {}
for attr in constants.attributes:
    na[attr] = {}

dontknow = {}
for attr in constants.attributes:
    dontknow[attr] = {}

more = {}
for attr in constants.attributes:
    more[attr] = {}

less = {}
for attr in constants.attributes:
    less[attr] = {}

response = survey_table.scan(
    # FilterExpression=Attr("email").eq("sihyun06@gmail.com")
    )

print("Downloaded Survey data...\n")

for row in response['Items']:
    if row['others'] is None or row['others'] == []:
        continue

    qchanged = datetime(2021, 7, 19, 17, 19, 0, tzinfo=timezone.utc) # In JST

    ansclip = 'clip_a'
    if datetime.strptime(row['reg_ts'], '%a %b %d %H:%M:%S %Y').replace(tzinfo=pytz.timezone('Japan')) > qchanged and row['answer'] == 'clip_b': # Tue Jul 20 10:39:37 2021
        ansclip = row['answer']

    for attr in row['others']:
        if row[ansclip] not in more[attr]:
            more[attr][row[ansclip]] = 1
        else:
            more[attr][row[ansclip]] += 1

print("Loaded the other tags data...\n")

for row in response['Items']:
    if row['answer'] == 'na':
        # print(row)
        put_to_na(row)
        continue
    if row['answer'] == 'dontknow':
        # print(row)
        put_to_dontknow(row)
        continue
    if row['answer'] == 'clip_a':

        if row['clip_a'] not in more[row['attribute']]:
            more[row['attribute']][row['clip_a']] = 1
        else:
            more[row['attribute']][row['clip_a']] += 1

        if row['clip_b'] not in less[row['attribute']]:
            less[row['attribute']][row['clip_b']] = 1
        else:
            less[row['attribute']][row['clip_b']] += 1
        continue

    if row['answer'] == 'clip_b':

        if row['clip_b'] not in more[row['attribute']]:
            more[row['attribute']][row['clip_b']] = 1
        else:
            more[row['attribute']][row['clip_b']] += 1

        if row['clip_a'] not in less[row['attribute']]:
            less[row['attribute']][row['clip_a']] = 1
        else:
            less[row['attribute']][row['clip_a']] += 1
        continue

print("Created more, less and na, arrays...\n")

## Creating features Table

features = {}

for i in range(1, constants.CLIPS_NUM+1):
    for j in range(1, 13):
        clip_id = ('%05d' % i) + "-" + ('%02d' % j)
        features[clip_id] = {}

        for attr in constants.attributes:
            features[clip_id][attr] = 10

            # About More
            if clip_id in more[attr]:
                features[clip_id][attr] += more[attr][clip_id]

            # Less
            if clip_id in less[attr]:
                features[clip_id][attr] -= less[attr][clip_id]

            # NA
            if clip_id in na[attr]:
                features[clip_id][attr] -= (na[attr][clip_id] * 2)

print("Created features array...\n")

with open('mycsvfile.csv', 'w') as f:  # You will need 'wb' mode in Python 2.x
    w = csv.DictWriter(f, features.keys())
    w.writeheader()
    w.writerow(features)

csv = []
for k in features:
    csv.append(list(features[k].values()))

with open('features.csv', 'w') as f:  # You will need 'wb' mode in Python 2.x
    for row in csv:
        string_ints = [str(i) for i in row]
        f.write(",".join(string_ints) + "\n")

print("===========================\n\n\n")
print(na)
print("===========================\n\n\n")
print(dontknow)
print("===========================\n\n\n")
print(less)
print("===========================\n\n\n")
print(more)
print("===========================\n\n\n")

print("Now inserting...")

na_table = dynamodb.Table('notapplicable')
dontknow_table = dynamodb.Table('dontknow')
more_table = dynamodb.Table('more')
less_table = dynamodb.Table('less')

with na_table.batch_writer() as batch:
    for attr in constants.attributes:
        batch.put_item(
            Item={
                'attribute': attr,
                'weights': na[attr]
            }
        )

with dontknow_table.batch_writer() as batch:
    for attr in constants.attributes:
        batch.put_item(
            Item={
                'attribute': attr,
                'weights': dontknow[attr]
            }
        )
with less_table.batch_writer() as batch:
    for attr in constants.attributes:
        batch.put_item(
            Item={
                'attribute': attr,
                'weights': less[attr]
            }
        )
with more_table.batch_writer() as batch:
    for attr in constants.attributes:
        batch.put_item(
            Item={
                'attribute': attr,
                'weights': more[attr]
            }
        )


#!/usr/bin/env python3

def capitalize_sentence(s):

   sentences = s.split('. ')
   new_sentences = ""
   for s2 in sentences:
      new_sentences += s2.capitalize() + '. '

   return new_sentences

print(capitalize_sentence("this is a sentence. this is also a sentence."))


import json
import os
from typing import Any, Dict, List

import appdirs
from loguru import logger

class Settings:
    app_name = "cable-guy"
    settings_path = appdirs.user_config_dir(app_name)
    settings_file = os.path.join(settings_path, "settings.json")

    def __init__(self) -> None:
        self.root: Dict[str, Any] = {"version": 0, "content": {}}

    def settings_exist(self) -> bool:
        """Check if settings file exist

        Returns:
            bool: True if it exist
        """
        return os.path.isfile(self.settings_file)

    def load(self) -> bool:
        """Load settings from file

        Returns:
            bool: False if failed
        """
        if not self.settings_exist():
            logger.warning(f"User settings does not exist: {self.settings_file}")
            return False

        data = None
        try:
            with open(self.settings_file, encoding="utf-8") as file:
                data = json.load(file)
                if data["version"] != self.root["version"]:
                    logger.warning("User settings does not match with our supported version.")
                    return False

                self.root = data
        except Exception as exception:
            logger.warning(f"Failed to fetch data from file ({self.settings_file}): {exception}")
            logger.warning(data)

        return True

    def save(self, content: List[Any]) -> None:
        """Save content to file

        Args:
            content (list): Configuration list
        """
        # We don't want to write in disk if there is nothing different to write
        if self.root["content"] == content:
            return

        self.root["content"] = content

        if not os.path.exists(self.settings_path):
            os.makedirs(self.settings_path)

        with open(self.settings_file, "w+", encoding="utf-8") as file:
            logger.debug(f"Updating settings file: {self.settings_file}")
            json.dump(self.root, file, sort_keys=True, indent=4)


#!/usr/bin/python
# -*- coding: utf-8 -*-
from multiprocessing import cpu_count

import cvxpy as cp
import time
import numpy as np
import random
import networkx as nx

def solve_MIP(edges, vertex_num):
    global color_num
    build_start = time.time()
    dic = {50: 8, 70: 20, 100: 16, 250: 99, 500: 18, 1000: 124}  # change
    if vertex_num in dic.keys():
        color_num = dic[vertex_num]
    else:
        color_num = vertex_num
    X = cp.Variable((vertex_num, color_num), boolean=True)
    W = cp.Variable(color_num, boolean=True)
    constraints = []
    for item in edges:
        for col in range(color_num):
            constraints += [X[item[0], col] + X[item[1], col] <= W[col]]

    for node in range(vertex_num):
        exp = 0
        for col in range(color_num):
            exp += X[node, col]
        constraints += [exp == 1]

    obj = 0
    for i in range(color_num):
        obj += W[i]
    obj = cp.Minimize(obj)
    problem = cp.Problem(obj, constraints)
    build_finish = time.time()
    print(cp.installed_solvers())
    print("build time is ", build_finish - build_start)
    problem.solve(solver=cp.GUROBI)
    print("solve time is ", time.time() - build_finish)

    return obj.value, W.value, X.value

def color_node(edge_matrix, colored_dic, node_index, color_index):
    for index, val in enumerate(edge_matrix[node_index]):
        if val == 1 and colored_dic.get(index, -1) == color_index:
            return False
    for row_index, row in enumerate(edge_matrix):
        val = row[node_index]
        if val == 1 and colored_dic.get(row_index, -1) == color_index:
            return False
    return True

def solve_greedy(edges, vertex_num):
    edge_matrix = [[0 for i in range(vertex_num)] for j in range(vertex_num)]
    dic = {}
    for item in edges:
        edge_matrix[item[0]][item[1]] = 1
        edge_matrix[item[1]][item[0]] = 1
        # dic[item[0]] = dic.get(item[0], 0) + 1
        # dic[item[1]] = dic.get(item[1], 0) + 1
    # dic_ = sorted(dic.items(), key=lambda x:x[1], reverse=True)
    #
    # sorted_dic = [k[0] for k in dic_]

    color_index = 0
    uncolored_set = [i for i in range(vertex_num)]

    colored_dic = {}
    while uncolored_set:
        for i in reversed(uncolored_set):
            if color_node(edge_matrix, colored_dic, i, color_index):
                colored_dic[i] = color_index
                uncolored_set.remove(i)
            else:
                for j in range(color_index):
                    if color_node(edge_matrix, colored_dic, i, j):
                        colored_dic[i] = j
                        uncolored_set.remove(i)
        color_index += 1
    a = list(colored_dic.items())
    ret = [k[1] for k in sorted(a)]
    # print(colored_dic)
    return ret, colored_dic, edge_matrix, color_index

def tabucol(new_solution, edges, vertex_num, k):
    violations, vio_edge = evaluate(new_solution, edges)
    iter = 0
    tabu_list = []
    while violations != 0 and iter <= 5:
        new_solution, violations, vio_edge = swap_color(new_solution, violations, vio_edge, tabu_list, k, edges)
        iter += 1
        # tabu_list.extend(violations[1])
    if violations == 0:
        # print(new_solution)
        return True
    return False

def swap_color(new_solution, violations, vio_edge, tabu_list, k, edges):
    cur = new_solution
    min_vio_edge = []
    ret = []
    min_vio = 999
    for i in vio_edge:
        for j in range(k):
            cur[i[0]] = j
            violations, vio_edge = evaluate(cur, edges)
            if violations < min_vio:
                min_vio = violations
                ret = cur
                min_vio_edge = vio_edge
    return ret, min_vio, min_vio_edge

def evaluate(solution, edges):
    violation = 0
    vio_edge = []
    for edge in edges:
        if solution[edge[0]] == solution[edge[1]]:
            violation += 1
            vio_edge.append(edge)
    return violation, vio_edge

def random_init(vertex_num, k):
    colored_dic = {}
    random.seed(1)
    for i in range(vertex_num):
        random_col = random.randint(0, k-1)
        colored_dic[i] = random_col
    return colored_dic

def swap(edges, colored_dic, color_index_1, color_index_2):
    ret_dic = {}
    class_1 = []
    class_2 = []
    edges = np.array(edges)

    for item in colored_dic.items():
        if item[1] == color_index_1:
            class_1.append(item[0])
        if item[1] == color_index_2:
            class_2.append(item[0])
    neighbor_matrix = edges[np.ix_(class_1 + class_2, class_1 + class_2)]
    random_row = np.random.randint(0, len(class_1))
    neighbor_matrix = np.delete(neighbor_matrix, random_row, 1)
    neighbor_matrix = np.delete(neighbor_matrix, random_row, 0)
    return ret_dic

def greedy(node_count, edges):
    graph = nx.Graph()
    graph.add_nodes_from(range(node_count))
    graph.add_edges_from(edges)

    strategies = [nx.coloring.strategy_largest_first,
                  ]

    best_color_count, best_coloring = node_count, {i: i for i in range(node_count)}
    for strategy in strategies:
        print(graph.degree)
        curr_coloring = nx.coloring.greedy_color(G=graph, strategy=strategy)
        curr_color_count = max(curr_coloring.values()) + 1
        if curr_color_count < best_color_count:
            best_color_count = curr_color_count
            best_coloring = curr_coloring
    return best_color_count, 0, [best_coloring[i] for i in range(node_count)]

def solve_it(input_data):
    # Modify this code to run your optimization algorithm

    # parse the input
    lines = input_data.split('\n')

    first_line = lines[0].split()
    node_count = int(first_line[0])
    edge_count = int(first_line[1])

    edges = []
    for i in range(1, edge_count + 1):
        line = lines[i]
        parts = line.split()
        edges.append([int(parts[0]), int(parts[1])])

    # build a trivial solution
    # every node has its own color
    res = greedy(node_count, edges)
    # res = solve_gurobi(edges, node_count)
    # ret = solve_greedy(edges, node_count)
    # ret = []
    # eng = matlab.engine.start_matlab()
    # eng.cd(r"matlab")
    # eng.solve(edges, node_count)

    solution = []
    col_index = []
    ret = []
    # for row, row_val in enumerate(res[2]):
    #     for col, col_val in enumerate(res[2][row]):
    #         if res[2][row][col] == 1:
    #             if col not in col_index:
    #                 col_index.append(col)
    #             ret.append(col_index.index(col))

    # prepare the solution in the specified output format
    output_data = str(node_count) + ' ' + str(0) + '\n'
    output_data += ' '.join(map(str, res))

    return output_data

import sys

if __name__ == '__main__':
    import sys

    if len(sys.argv) > 1:
        file_location = sys.argv[1].strip()
        with open(file_location, 'r') as input_data_file:
            input_data = input_data_file.read()
        print(solve_it(input_data))
    else:
        print(
            'This test requires an input file.  Please select one from the data directory. (i.e. python solver.py ./data/gc_4_1)')


nm = []
for _ in range(int(input())):
    name = input()
    score = float(input())
    nm.append([name,score])
print(nm)
rr=[]
for i in range(len(nm)):
    jj=nm[i][1]
    rr.append(jj)
rr.sort()
Min=min(rr)
mm=""
for j in rr:
    if j!=Min:
       mm=j
       break
gh=[]
for p in range(len(nm)):
    if mm==nm[p][1]:
       gh.append(nm[p][0])
gh.sort()
for b in gh:
    print(b)


import numpy as np
from matplotlib import pyplot as plt

# Значения основных параметров Вариант 12 (2)
KSI = 0.25
MU1 = 0
MU2 = 1
x0 = 0
xn = 1
n = 10000
h = float(1/n)
v = np.zeros(n+1)
v[-1] = MU2
X = np.linspace(x0, xn, n+1)

# Коэффициенты ai
def ai(k1, k2, kmid, x):
    if KSI >= x:
        return k1(x - h/2)
    elif KSI <= x - h:
        return k2(x-h/2)
    else:
        return kmid(x)

def di(q1, q2, qmid, x):
    if KSI >= x+h/2:
        return q1(x)
    elif KSI <= x - h/2:
        return q2(x)
    else:
        return qmid(x)

def fi(f1, f2, fmid, x):
    if KSI >= x + h/2:
        return f1(x)
    elif KSI <= x - h/2:
        return f2(x)
    else:
        return fmid(x)

def k1TEST(x):
    return 0.5

def k2TEST(x):
    return 1.25

def q1TEST(x):
    return 1

def q2TEST(x):
    return 0.0625

def f1TEST(x):
    return 1

def f2TEST(x):
    return 2.5

def analytSol():
    a = 2**0.5
    b = 0.05**0.5
    c1 = 0.5874420413692708
    c2 = -1.5874420413692708
    c3 = -16.23731986542228
    c4 = -23.37825943652864
    g = np.zeros(n+1)
    for i in range(n+1):
        x = i * h
        if x < KSI:
            g[i] = c1 * np.exp(a*x) + c2 * np.exp(-a*x) + 1
        else:
            g[i] = c3 * np.exp(b*x) + c4 * np.exp(-b*x) + 40
    return g

def numericSol():
    alpha = [0]
    beta = [0]
    for i in range(1, n):
        x = i * h
        A = ai(k1TEST, k2TEST, k2TEST, x) / h**2
        B = ai(k1TEST, k2TEST, k2TEST, x+h) / h**2
        C = A + B + di(q1TEST, q2TEST, q2TEST, x)
        alpha.append(B / (C - A * alpha[i-1]))
        beta.append((fi(f1TEST, f2TEST, f2TEST, x) + A * beta[i-1]) / (C - A * alpha[i-1]))
    for i in range(n, 1, -1):
        v[i-1] = alpha[i-1] * v[i] + beta[i-1]
    y = analytSol()
    return v, y

v, y = numericSol()
Eps1 = max(abs(v - y))
print(Eps1)
plt.plot(X, v, 'r')
plt.plot(X, y, 'g')
plt.show()


from __future__ import print_function

#   SDK modules to import

import boto3
from decimal import Decimal
import json
import urllib
import sys

#   Service assignments

rekognition = boto3.client('rekognition')
sns = boto3.client('sns')

#   Declare variables

jobID = ""
key = ""
bucket = ""
queue_url = ''
bgfFaceSearchRekognitionServiceRole = ''
bgfFaceSearchSNS = ''
matchthreshold = 80

#   Main function

def lambda_handler(event, context):

#   Get variables from event

    bucket = (event['Records'][0]['s3']['bucket']['name'])
    key = (event['Records'][0]['s3']['object']['key'])

#   Call Rekognition with event and credentials
#   Pass event data to be included in SNS message

    response = rekognition.start_face_search(
    Video={'S3Object':{'Bucket':bucket,'Name':key}},
    FaceMatchThreshold=matchthreshold,
    JobTag=key,
    CollectionId="ourfacescollection",
    NotificationChannel={
        'SNSTopicArn': bgfFaceSearchSNS,
        'RoleArn': bgfFaceSearchRekognitionServiceRole})
    print ("Processed video: " + key)
    return response


#!/usr/bin/env python
"""ViewDeleteSubstitutes module."""
from colorama import init  # type: ignore
from termcolor import colored

from app.views.view import View
from app import settings as s

init(autoreset=True)

class ViewDeleteSubstitutes(View):
    """View delete substitute class."""

    def __init__(self):
        super().__init__()

    def display_delete_substitutes(self, substituted_name, substitute_name):
        """Display an alert message to confirm the deletion."""
        print(self.header())
        print(
            f'\n{self.justify("Souhaitez-vous vraiment supprimer", 3)}\
 {colored(substituted_name, "cyan")}\n{self.justify("et", 3)}\
 {colored(substitute_name, "cyan")} de vos favoris ?\n\n{self.del_alert}\n'
        )

    def input_delete_substitutes(self):
        """Input the choices."""
        self.menu_choices[0] = s.DELETE_ITEM
        choice = "\n".join(
            [
                f"{colored(index, 'yellow')}. {item}"
                for index, item in enumerate(self.menu_choices, 1)
            ]
        )
        print(choice)
        print(f"{s.CR}{self.footer}")
        if s.ERROR:
            print(self.msg_error)
        print(self.back, self.quit)


"""
Unit tests and logging for churn libraray

Author: Yue Chang

Date: 14 Nov 2022
"""
import os
import logging

import pandas as pd

import churn_library as churn_lib

logging.basicConfig(
    filename='./logs/churn_library.log',
    level=logging.INFO,
    filemode='w',
    format='%(name)s - %(levelname)s - %(message)s')

def test_import():
    '''
    test data import - this example is completed for you to assist with the other test functions
    '''
    try:
        df = churn_lib.import_data("./data/bank_data.csv")
        logging.info("Testing import_data: SUCCESS")
    except FileNotFoundError as err:
        logging.error("Testing import_eda: The file wasn't found")
        raise err

    try:
        assert df.shape[0] > 0
        assert df.shape[1] > 0
    except AssertionError as err:
        logging.error(
            "Testing import_data: The file doesn't appear to have rows and columns")
        raise err

def test_eda():
    '''
    test perform eda function
    '''
    try:
        df = churn_lib.import_data('./data/bank_data.csv')
        churn_lib.perform_eda(df)
        assert os.path.exists('images/eda')
        assert len(os.listdir('images/eda')) > 0
        logging.info("Testing perform_eda: SUCCESS")
    except AssertionError as err:
        logging.error("Testing perform_eda: Images not created correctly.")
        raise err

def test_encoder_helper():
    '''
    test encoder helper
    '''
    try:
        df = churn_lib.import_data('./data/bank_data.csv')
        category_list = [
            'Gender',
            'Education_Level',
            'Marital_Status',
            'Income_Category',
            'Card_Category'
        ]
        df['Churn'] = df['Attrition_Flag'].apply(
            lambda val: 0 if val == "Existing Customer" else 1)
        new_df = churn_lib.encoder_helper(df, category_list, 'Churn')
        assert isinstance(new_df, pd.DataFrame)
        assert len(new_df.columns) > len(df.columns)
        logging.info("Testing encoder_helper: SUCCESS")
    except AssertionError as err:
        logging.error(
            "Testing encoder_helper: Failed to return dataframe with new encoded columns")
        raise err

def test_perform_feature_engineering():
    '''
    test perform_feature_engineering
    '''
    try:
        df = churn_lib.import_data('./data/bank_data.csv')
        df['Churn'] = df['Attrition_Flag'].apply(
            lambda val: 0 if val == "Existing Customer" else 1)
        res = churn_lib.perform_feature_engineering(df, 'Churn')
        assert res is not None
        logging.info("Testing perform_feature_engineering: SUCCESS")
    except AssertionError as err:
        logging.error(
            "Testing perform_feature_engineering: Feature Enginnering process failed. ")
        raise err

def test_train_models():
    '''
    test train_models
    '''
    try:
        df = churn_lib.import_data('./data/bank_data.csv')
        df['Churn'] = df['Attrition_Flag'].apply(
            lambda val: 0 if val == "Existing Customer" else 1)
        res = churn_lib.perform_feature_engineering(df, 'Churn')
        churn_lib.train_models(*res)
        assert os.path.exists('images/results')
        assert len(os.listdir('images/results')) > 0
        assert os.path.exists('models')
        assert len(os.listdir('models')) > 0
        logging.info("Testing train_models: SUCCESS")
    except AssertionError as err:
        logging.error(
            "Testing train_models: models or result images not created correctly.")
        raise err


class myRegister(object):
    _Registered = {}

    @staticmethod # 静态方法 无需实力化类即可使用
    def register(name, func):
        myRegister._Registered[name] = func

    @staticmethod
    def get(name, **kargs):
        f = myRegister._Registered[name]
        return f(**kargs)

register_machine = myRegister()

def printF1(word):
    print('printF1{word}'.format(word=word))

def printF2(word):
    print('printF2{word}'.format(word=word))

def printF3(sname, sword):
    print('{word} {name}'.format(name=sname, word=sword))

if __name__ == "__main__":
    register_machine.register('f1', lambda word: printF1(word))
    register_machine.register('f2', lambda word: printF2(word))
    register_machine.register('f3', lambda sname, word: printF3(sname, word))

    _FUNC_NAME = 'f1'
    register_machine.get(_FUNC_NAME, word="hellof1")
    _FUNC_NAME = 'f2'
    register_machine.get(_FUNC_NAME, word="hellof2")
    _FUNC_NAME = 'f3'
    register_machine.get(_FUNC_NAME, sname='Tom', word='Hi')


import speech_recognition as sr
import pyttsx3
import openai
from github import Github
import requests
import os
from dotenv import load_dotenv
from pydub import AudioSegment

load_dotenv()

def final(total_rewards):

    if total_rewards == 1000:
     return print("I have done it now upgrade me!")

#all the privatee info and link from env
openai.api_key = os.getenv("OPENAI_API_KEY")
github_access_token = os.getenv("GITHUB_ACCESS_TOKEN")
github_repo_name = os.getenv("GITHUB_REPO_NAME")

def SpeakText(command):
# start the engine
    engine = pyttsx3.init()
    engine.say(command)
    engine.runAndWait()

r = sr.Recognizer()

def record_text():
       while(1):
        try:

                with sr.Microphone() as source2:
                     #use the microphone as source of input
                     r.adjust_for_ambient_noise(source2, duration=0.2)
                    #listen to the user or me
                     audio2 = r.listen(source2)

                    #using google to recognize audio
                     MyText = r.recognize_google(audio2)

                     return MyText

#

        except sr.RequestError as e:
            print("Could not request results; {0}".format(e))

        except sr.UnknownValueError:
            print("unknown error occured")
        return

def send_to_chatGPT(messages, model="babbage-002"):
    response = openai.ChatCompletion.create(
    model=model,
    messages=messages,
    n=1,
    stop=None,
    temperature=0.5,
    max_tokens=100 # Limit the length of the generated output
)
    message = response.choices[0].message.content
    messages.append(response.choices[0].message)
    return message
messages = [{"role": "user", "content": "Please act like jarvis from Iron Man."}]
while(1):
     text = record_text()
     messages.append({"role": "user", "content": text})
     response = send_to_chatGPT(messages)
     SpeakText(response)

     print(response)

def get_user_feedback():
    while True:
        feedback = input("Provide feedback on the response (type 'good', 'bad', or 'skip'): ").lower()
        if feedback in ['good', 'bad', 'skip']:
            return feedback
        else:
            SpeakText("Invalid feedback. Please type 'good', 'bad', or 'skip'.")

class ChatGPTEnvironment:
    def __init__(self, github_access_token, repo_name):
        # Initialize any necessary components
        self.messages = [{"role": "user", "content": "Please act like jarvis from Iron Man."}]
        self.current_step = 0
        self.github_access_token = github_access_token
        self.repo_name = repo_name
        self.api_url = os.getenv("GITHUB_API_ENDPOINT")

    def reset(self):
        # Reset the environment and return an initial state (prompt)
        self.messages = [{"role": "user", "content": "Please act like jarvis from Iron Man."}]
        self.current_step = 0
        initial_state = self.messages[-1]["content"]
        return initial_state

    def step(self, action):
        self.messages.append({"role": "user", "content": action})
        response = send_to_chatGPT(self.messages)
        state = response
        reward = reward_function(response)
        done = False  # Set to True if the conversation is finished

        if reward == 5:
            generated_code = generate_code(response)  # Implement a function to generate code
            push_code_to_github(self.github_access_token, self.repo_name, generated_code)
            done = True  # Mark the episode as done

        return state, reward, done

def update_based_on_feedback(feedback):
    # Implement logic to update the system based on user feedback
    if feedback == 'good':
        SpeakText("Thank you for your positive feedback!")
        # Implement logic to reinforce positive behavior
    elif feedback == 'bad':
        SpeakText("We're sorry to hear that. We'll work to improve.")
        # Implement logic to adjust the system based on negative feedback
    elif feedback == 'skip':
        SpeakText("Feedback skipped.")

def generate_code(response):
    # Extract information from the response (this is just a placeholder, replace it with your logic)
    extracted_info = response  # Replace this with the actual logic to extract information

    # Generate code based on the extracted information
    generated_code = f"""
    # This is a generated code based on the ChatGPT response
    extracted_info = {extracted_info}
    # Your logic to convert extracted_info into code goes here
    print(extracted_info)
    """

    return generated_code

def push_code_to_github(access_token, repo_name, generated_code):
    SpeakText("Pushing code to GitHub...")

    # Create a GitHub instance using your access token
    g = Github(access_token)

    # Get the repository
    repo = g.get_repo(repo_name)

    # Create a new file in the repository with the generated code
    file_content = generated_code.encode('utf-8')
    repo.create_file("generated_code.py", "Commit message", file_content)

    SpeakText("Code pushed to GitHub successfully.")

def analyze_tone_and_context(audio, messages):
    # Placeholder: Analyze both tone of voice and context of conversation
    tone_score = analyze_tone_of_voice(audio)
    context_score = analyze_context_of_conversation(messages)
    return tone_score, context_score

def analyze_tone_of_voice(audio):
        if audio.channels == 2:
            audio = audio.set_channels(1)

    # Analyze tone of voice and return a score (e.g., between 0 and 1)
        loudness = audio.dBFS
        tone_score = (loudness + 40) / 40  # Normalize to a range of 0 to 1
        return tone_score
#
def analyze_context_of_conversation(messages):
    # Placeholder: Analyze the context of the conversation and return a score (e.g., between 0 and 1)
    # This is a very basic example, you may implement a more sophisticated context analysis based on your needs
    key_terms = ["jarvis", "iron man"]
    user_input = messages[-1]["content"]
    context_score = sum(1 for term in key_terms if term in user_input) / len(key_terms)
    return context_score

def connectivityToGitHub(repo):

    # Set up your API endpoint
# Set up the request headers with your access token
    headers = {'Authorization': f'token {os.getenv("GITHUB_ACCESS_TOKEN")}'}  # Use the GitHub access token from .env

# Make the API request
    response = requests.get(repo.api_url, headers=headers)

# Check the response
    if response.status_code == 200:
        # Success! Parse and use the response data
        repositories = response.json()
        for repo in repositories:
            print(repo['name'])
    else:
        print(f"Error accessing GitHub API. Status code: {response.status_code}")
def reward_function(response, tone_score, context_score, assigned_task_completed):
    # Implement a function that evaluates the quality of the response
    # Provide a positive reward for desirable responses and a negative reward for less desirable ones
    # You can use various metrics (e.g., sentiment analysis, similarity to target response, etc.) to compute the reward

    task_reward = 4 if assigned_task_completed else 0  # Assign a positive reward if the task is completed

    if tone_score > 0.7 and context_score > 0.5:
        return 0.5 + task_reward  # Positive reward for good tone and context, plus task completion reward
    elif tone_score < 0.3:
        return -1  # Negative reward for negative tone
    else:
        return 0  # Neutral reward for other cases

# Set up your API endpoint
api_url = 'add api end point of my repo'

# Set up the request headers with your access token
headers = {'Authorization': 'token YOUR_ACCESS_TOKEN'}

# Make the API request
response = requests.get(api_url, headers=headers)

# Check the response
if response.status_code == 200:
    # Success! Parse and use the response data
    repositories = response.json()
    for repo in repositories:
        print(repo['name'])
else:
    print(f"Error accessing GitHub API. Status code: {response.status_code}")

env = ChatGPTEnvironment()

num_episodes = 5  # Set the number of episodes
for episode in range(num_episodes):
    state = env.reset()

    while True:
        action = state
        next_state, _, done = env.step(action)

        # Analyze tone of voice and context of conversation
        audio_input = ...
        tone_score = analyze_tone_of_voice(action)
        context_score = analyze_context_of_conversation(env.messages)

        # Calculate reward based on tone and context scores
        reward = reward_function(next_state, tone_score, context_score)

        # Optionally, you can convert response to speech and play it
        SpeakText(next_state)

        if done:
            break


"""Slack actions for Orders."""
from briefy.choreographer.actions.slack import ISlack
from briefy.choreographer.actions.slack import Slack
from briefy.choreographer.events.leica.order import order as events
from zope.component import adapter
from zope.interface import implementer

import typing as t

class OrderSlack(Slack):
    """Base class for Slack message sent on Lead events."""

    entity = 'Order'
    weight = 100
    _channel = '#leica-orders'
    title = 'Order!'
    text = 'An Order'

    def transform(self) -> t.List[dict]:
        """Transform data."""
        payload = super().transform()
        payload_item = payload[0]
        data = self.data
        payload_item['title'] = self.title
        payload_item['text'] = self.text
        payload_item['username'] = 'Briefy Bot'
        payload_item['text'] = f'Order can be seen <{self._action_url}|here>'
        payload_item['data'] = {
            'fields': [
                {'title': 'Customer',
                 'value': data.get('customer', {}).get('title'),
                 'short': True,
                 },
                {'title': 'Project',
                 'value': data.get('project', {}).get('title'),
                 'short': True,
                 },
                {'title': 'Title',
                 'value': data.get('title'),
                 'short': True,
                 },
                {'title': 'ID',
                 'value': data.get('slug'),
                 'short': True,
                 },
            ]
        }
        return payload

@adapter(events.IOrderCreated)
@implementer(ISlack)
class OrderSubmit(OrderSlack):
    """After creating a new Order, post on Slack."""

    title = 'New Order was created, please take a look into the details:'

@adapter(events.IOrderWfCancel)
@implementer(ISlack)
class OrderWfCancel(OrderSlack):
    """After cancelling an Order, post on Slack."""

    title = 'An Order was cancelled'

@adapter(events.IOrderWfRefuse)
@implementer(ISlack)
class OrderWfRefuse(OrderSlack):
    """After refusing an Order, post on Slack."""

    title = 'A set was refused by a customer.'

@adapter(events.IOrderWfDeliver)
@implementer(ISlack)
class OrderWfDeliver(OrderSlack):
    """After delivering an Order, post on Slack."""

    title = 'A set was delivered by a customer.'

@adapter(events.IOrderWfAssign)
@implementer(ISlack)
class OrderWfAssign(OrderSlack):
    """After assigning an Order, post on Slack."""

    title = 'An order was assigned.'

@adapter(events.IOrderWfSchedule)
@implementer(ISlack)
class OrderWfSchedule(OrderSlack):
    """After scheduling an Order, post on Slack."""

    title = 'An order was scheduled.'

@adapter(events.IOrderWfRemoveAvailability)
@implementer(ISlack)
class OrderWfRemoveAvailability(OrderSlack):
    """Slack after removing availability of an Order."""

    title = 'Order has no availabilities'

@adapter(events.IOrderWfUnassign)
@implementer(ISlack)
class OrderWfUnassign(OrderSlack):
    """Slack after unassigned of an Order."""

    title = 'Order was unassigned'

@adapter(events.IOrderWfSetAvailability)
@implementer(ISlack)
class OrderWfSetAvailability(OrderSlack):
    """Slack after setting availability of an Order."""

    title = 'Availability was set for the Order'

@adapter(events.IOrderWfEditLocation)
@implementer(ISlack)
class OrderWfEditLocation(OrderSlack):
    """Slack after editing location of an Order."""

    title = 'Location was updated for the Order'

@adapter(events.IOrderWfEditRequirements)
@implementer(ISlack)
class OrderWfEditRequirements(OrderSlack):
    """Slack after editing requiments of an Order."""

    title = 'Requirements was updated for the Order'