from xml.dom import minidom
from random import choice
from env import XMI_FILE

file = minidom.parse(f"models/{XMI_FILE}")
xmi = file.firstChild
# doc = xmi.firstChild
model = xmi.getElementsByTagName("uml:Model")[0]
packaged_elements = model.getElementsByTagName("packagedElement")

remove_percent_20 = lambda x: x.replace("%20", " ")

def get_classes(for_baserow=True):
    classes = [el for el in packaged_elements if el.getAttribute("xmi:type") == "uml:Class"]

    br_tables = [{
        "name": remove_percent_20(el.getAttribute("name")),
        "data": [["/"]], # Empty table with a primary key named "/"
        "first_row_header": True
    } for el in classes]

    return br_tables if for_baserow else classes

def get_associations():
    classes = get_classes(for_baserow=False)
    class_associations = {}

    for _class in classes:
        class_name = remove_percent_20(_class.getAttribute("name"))
        owned_members = _class.getElementsByTagName("ownedMember")
        owned_ends = (member.getElementsByTagName("ownedEnd") for member in owned_members)

        associations = []
        for pair in owned_ends:
            ends = [{
                "name": curr.getAttribute("name").title().replace("_", " "),
                "class_name": next(remove_percent_20(__class.getAttribute("name"))
                                    for __class in classes
                                    if __class.getAttribute("xmi:id") == curr.getAttribute("type")),
                # "aggregation": curr.getAttribute("aggregation"),
                # "lower_value": curr.getElementsByTagName("lowerValue")[0].getAttribute("value"),
                # "upper_value": curr.getElementsByTagName("upperValue")[0].getAttribute("value")
            } for curr in pair]

            if ends[0]["class_name"] != class_name:
                associations.append(ends[0])
            associations.append(ends[1])

        class_associations[class_name] = associations
    return class_associations

def get_attributes(classes, data_types):
    list_attributes = {}

    for _class in classes:
        class_name = remove_percent_20(_class.getAttribute("name"))
        owned_attributes = _class.getElementsByTagName("ownedAttribute")

        attributes = []
        for attribute in owned_attributes:
            # print("attribute type:", attribute.getAttribute("type"))
            # print("data_type name:", [data_type for data_type in data_types])
            found_type = ""
            if attribute.getAttribute("type") != "":
                found_type = next(data_type["name"] for data_type in data_types
                                  if data_type["id"] == attribute.getAttribute("type"))

            attributes.append({
                "name": attribute.getAttribute("name"),
                "type": found_type
            })

        # print(f"{attributes=}")
        list_attributes[class_name] = attributes
    return list_attributes

def get_enumerations():
    # Enumerations
    enumerations = [element for element in packaged_elements
                    if element.getAttribute("xmi:type") == "uml:Enumeration"]
    all_literals = [enumeration.getElementsByTagName("ownedLiteral")
                    for enumeration in enumerations]

    return [{
        "id": enum.getAttribute("xmi:id"),
        "name": enum.getAttribute("name"),
        "literals": {lit.getAttribute("name")
                     for lit in literals}
    } for enum, literals in zip(enumerations, all_literals)]

def get_data_types():
    # Data Types
    data_type_elements = (element for element in packaged_elements
                          if element.getAttribute("xmi:type") == "uml:DataType")

    data_types = [{
        "id":  data_type.getAttribute("xmi:id"),
        "name" : remove_percent_20(data_type.getAttribute("name"))
    } for data_type in data_type_elements]

    data_types.extend(get_enumerations())
    return data_types

def get_ready_attributes(class_attributes):
    colors = (
        "light-blue", "blue", "dark-blue",
        "light-green", "green", "dark-green",
        "light-yellow", "yellow", "dark-yellow",
        "light-red", "red", "dark-red",
        "light-gray", "gray", "dark-gray"
    )

    enums = get_enumerations()
    for class_name, attributes in class_attributes.items():
        upgraded_attributes = []
        for attr in attributes:
            field = {}
            field["name"] = attr["name"].title().replace("_", " ")
            lower_name = attr["type"].lower()

            # Handling Number Type
            if lower_name in ["integer", "int", "unsigned integer", "unsigned int", "float", "real"]:
                field["type"] = "number"
                field["number_decimal_places"] = 0
                field["number_negative"] = True
                if lower_name in ["unsigned integer", "unsigned int"]:
                    field["number_negative"] = False
                if lower_name in ["float", "real"]:
                    field["number_decimal_places"] = 5

            # Handling Long Text Type
            elif lower_name in ["blob", "text", "long text"]:
                field["type"] = "long_text"

            # Handling URL Type
            elif lower_name == "url":
                field["type"] = "url"

            # Handling Email Type
            elif lower_name == "email":
                field["type"] = "email"

            # Handling Rating Type
            elif lower_name == "rating":
                field["type"] = "rating"
                field["max_value"] = 5
                field["color"] = "yellow"
                field["style"] = "star"

            # Handling Boolean Type
            elif lower_name in ["boolean", "bool"]:
                field["type"] = "boolean"

            # Handling Date, Last Modified Type and Created on Type
            elif lower_name in ["last modified", "created on", "date", "time", "datetime"]:
                if lower_name in ["date", "time", "datetime"]:
                    field["type"] = "date"
                if lower_name == "last modified":
                    field["type"] = "last_modified"
                if lower_name == "created on":
                    field["type"] = "created_on"
                field["date_format"] = "EU"
                field["date_include_time"] = False
                if lower_name in ["time", "datetime"]:
                    field["date_include_time"] = True
                    field["date_time_format"] = "24"
                if lower_name in ["last modified", "created on"]:
                    field["date_include_time"] = True
                    field["timezone"] = "Europe/Zagreb"

            # Handling File Type
            elif lower_name == "file":
                field["type"] = "file"

            # Handling Phone Number Type
            elif lower_name in ["phone", "phone number", "telephone", "telephone number"]:
                field["type"] = "phone_number"

            # Handling Multiple_Collaborators Type
            elif lower_name in ["collaborators", "multiple collaborators"]:
                field["type"] = "multiple_collaborators"

            # Handling Multiple Select Type
            elif lower_name in [enum["name"].lower() for enum in enums]:
                exact_enum = next(enum for enum in enums if enum["name"] == attr["type"])
                field["type"] = "multiple_select"
                field["select_options"] = [{
                    "value": value,
                    "color": choice(colors)
                } for value in exact_enum["literals"]]

            # If a Data Type is not defined or is a string, it's probably just a text
            else:
                field["type"] = "text"
                field["text_default"] = "" # for now

            upgraded_attributes.append(field)

        class_attributes[class_name] = upgraded_attributes

    return class_attributes

def get_full_tables():
    classes = get_classes(for_baserow=False)
    data_types = get_data_types()
    attributes = get_attributes(classes, data_types)

    return get_ready_attributes(attributes)

"""
if __name__ == '__main__':
    classes = get_classes(for_baserow=False)
    #tables = get_classes()
    #assoc = get_associations(classes)
    #for i, aso in assoc.items():
    #    print(i, aso)
    # print(classes)
    # print(tables)
    # enums = get_enumerations()
    # print(enums)
    data_types = get_data_types()
    # print(data_types)
    attribute = get_attributes(classes, data_types)
    print(attribute)
    # table_fields = get_ready_attributes(attribute_names)
    # for class_name, fields in table_fields.items():
    #     print(class_name, fields)
"""


import uuid

from celery.signals import worker_process_init

from strongr.core.domain.schedulerdomain import SchedulerDomain
from .wrapper import Command

import strongr.core
from celery import Celery, signals

class RunWorkerCommand(Command):
    """
    Runs a strongr worker.

    worker:run
        {--p|profile=elasticworker : The worker profile to be used}
    """
    def handle(self):
        config = strongr.core.Core.config()
        broker = config.celery.broker
        backend = config.celery.backend

        celery = Celery('strongr', broker=broker, backend=backend)

        scheduler = SchedulerDomain.schedulerService()
        scheduler.getCommandBus() # we need to initiate this as it self-registers it's commands to celery
        scheduler.getQueryBus() # we need to initiate this as it self-registers it's queries to celery

        domains = getattr(config.celery.workers, self.option('profile')).as_dict()

        commands = []
        for domain in domains:
            for command in domains[domain]:
                commands.append('{}.{}'.format(domain, command))

        for command in commands:
            strongr.core.Core.command_router().enable_worker_route_for_command(celery, command)

        workername = '%h@{}'.format(uuid.uuid4())

        argv = [
            'worker',
            '--loglevel=DEBUG',
            '-Q=' + ','.join(commands),
            #'--pool=solo',
            #'--concurrency=1',
            #'--max-tasks-per-child=1',
            '--loglevel=INFO',
            '-n {}'.format(workername)
        ]

        # crude fix for celery daemonized threads causing salt to fail
        # https://github.com/celery/celery/issues/1709
        @worker_process_init.connect
        def fix_multiprocessing(**kwargs):
            # don't be a daemon, so we can create new subprocesses
            from multiprocessing import current_process
            current_process().daemon = False

        @signals.setup_logging.connect
        def setup_celery_logging(**kwargs):
            pass

        celery.worker_main(argv)


# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import scrapy
from pymongo import MongoClient
from scrapy.exceptions import DropItem

URL = scrapy.conf.settings['MONGODB_URL']
# DB = scrapy.conf.settings['MONGODB_DATABASE']
# COLLECTION = scrapy.conf.settings["MONGO_COLLECTION"]

class MongoDBPipeline(object):

    def __init__(self):
        client = MongoClient(URL)
        self.db = client.scrapy
        self.collection = self.db.quotes

    def process_item(self, item, spider):
        post = {
            'content':item['content'],
            'author':item['author'],
            'author_url':item['author_url'],
            'tags':item['tags'],
            'description':item['description']
        }
        self.collection.insert(post)
        return item


#!/usr/bin/env python
"""REPL arguments tokenizer."""

import sys
import re

class Tokenizer(object):  # noqa
    """Main class for the Tokenizer.

    Tokenize all the arguments passed into the REPL for parsing.
    """

    def __init__(self, chars):
        """Initialize the Tokenizer class.

        :chars: Passed in arguments to parse from the the repl.
        """
        self.chars = chars

        bounds = 'BOUNDS'
        operator = 'OPERATOR'
        variable = 'VARIABLE'
        assignment = 'ASSIGNMENT'
        ints = 'INTS'

        # the expression used to parse and group the arguments
        expressions = [
            (r'[ \n\t ]+', None),
            (r'#[^\n]*', None),
            (r'inv\([a-zA-Z]\)', operator),
            (r'[a-zA-Z]', variable),
            (r'\=', assignment),
            (r'\[([0-9, ;]+)\]', bounds),
            (r'\d', ints),
            (r'\+', operator),
            (r'-', operator),
            (r'\*', operator),
            (r'\'', operator)
        ]

        self.exprs = expressions

    @staticmethod
    def tokenizer(arguments, patterns_exprs):
        """Parse the arguments against the expressions.

        :arguments: The arguments to be parsed.
        :patterns_exprs: the pattern expression for matching.
        """
        pos = 0
        tokens = []
        while pos < len(arguments):
            match = None
            for expr in patterns_exprs:
                pattern, tag = expr
                regex = re.compile(pattern)
                match = regex.match(arguments, pos)
                if match:
                    text = match.group(0)
                    if tag:
                        token = (tag, text)
                        tokens.append(token)
                    break
            if not match:
                sys.stdout.write(
                    'Illegal character: `%s`\n' % arguments[pos])
                break
            else:
                pos = match.end(0)
        return tokens

    def lexer(self):
        """Re-format the tokens for arithmetic operations."""
        tokens = self.tokenizer(self.chars, self.exprs)
        return tokens


from __future__ import unicode_literals, print_function, division

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import Parameter
from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence

import sys
import math

from numpy import random
import numpy as np
import itertools

class LsrHierGNNLayer(nn.Module):
    def __init__(self, args):
        super(LsrHierGNNLayer, self).__init__()
        self.args = args
        self.hop_layers = self.args.hop_layer
        self.matrix_tree_layer = Matrix_Tree_Layer(self.args)
        self.msg_psg_layer = nn.ModuleList([Message_Passing_Layer(self.args) for i in range(self.hop_layers)])
        self.dropouts = nn.ModuleList([nn.Dropout(self.args.gnn_dp_rate, inplace=True) for i in range(self.hop_layers)])

    def forward(self, sent_embedding, enc_sent_padding_mask):

        mask = enc_sent_padding_mask.unsqueeze(-1) #.repeat(1, 1, sent_embedding.size(-1))
        sent_embedding = sent_embedding * mask

        d, d_0 = self.matrix_tree_layer(sent_embedding, enc_sent_padding_mask)

        d = (d.transpose(-1,-2) * mask).transpose(-1,-2) * mask
        d_0 = d_0 * enc_sent_padding_mask

        gnn_out = []
        for i in range(self.hop_layers):
            sent_embedding = self.msg_psg_layer[i](sent_embedding, d, d_0)
            sent_embedding = self.dropouts[i](sent_embedding)
            gnn_out.append(sent_embedding)

        return gnn_out, d, d_0

class HierGNNLayer(nn.Module):
    def __init__(self, args):
        super(HierGNNLayer, self).__init__()
        self.args = args
        self.matrix_tree_layer = Matrix_Tree_Layer(self.args)
        self.msg_psg_layer = Message_Passing_Layer(self.args)
        self.dropout = nn.Dropout(self.args.gnn_dp_rate, inplace=True)

    def forward(self, sent_embedding, enc_sent_padding_mask):

        mask = enc_sent_padding_mask.unsqueeze(-1) #.repeat(1, 1, sent_embedding.size(-1))
        sent_embedding = sent_embedding * mask

        d, d_0 = self.matrix_tree_layer(sent_embedding, enc_sent_padding_mask)

        d = (d.transpose(-1,-2) * mask).transpose(-1,-2) * mask
        d_0 = d_0 * enc_sent_padding_mask

        out = self.msg_psg_layer(sent_embedding, d, d_0)
        out = self.dropout(out)

        return out, d, d_0

class Message_Passing_Layer(nn.Module):
    def __init__(self, args):
        super(Message_Passing_Layer, self).__init__()
        self.args = args
        self.h_dim = 1024
        self.weight_gate = nn.Linear(2 * self.h_dim, self.h_dim)
        self.W_i = nn.Linear(self.h_dim, self.h_dim)
        self.layer_norm = nn.LayerNorm(self.h_dim)

    def forward(self, h, int_mat, root_ps):
        """
        Input:
        h: hidden representation for each node (sentence embedding): (bs, MAX_NUM_SENT, sent_emb_dim)
        int_mat: interaction matrix: (bs, MAX_NUM_SENT, MAX_NUM_SENT)
        root_ps: roots probability: (bs, MAX_NUM_SENT)

        Eqt:
        𝑠_𝑖^((𝑙+1))  =(1−𝑎_𝑖^𝑟)𝑠_𝑖^((𝑙))+(𝑎_𝑖^𝑟)∑_(𝑘=1)^𝐾▒〖𝑎_ki 𝑠_𝑘^((𝑙)) 〗
        """

        # Calculate the msg, guided by interaction matrix (Adj)
        msg = torch.matmul(int_mat, h) # (bs, sent_num, sent_emb_dim)

        root_ps = root_ps.unsqueeze(-1)

        # structured guidance - large root prob, receive more msg
        update = root_ps * msg + (1.0-root_ps) * (self.W_i(h))

        # Gate mechanism by considering update and original representation
        gate = self.weight_gate(torch.cat((update, h), -1))
        gate = torch.sigmoid(gate)

        # 2. Update by gating
        h = gate * torch.tanh(update) + (1 - gate) * h
        h = self.layer_norm(h)

        return h

class Matrix_Tree_Layer(nn.Module):
    def __init__(self, args):
        super(Matrix_Tree_Layer, self).__init__()
        self.args = args
        self.str_dim_size = 1024
        self.h_dim = 1024

        # Projection for parent and child representation
        self.tp_linear = nn.Linear(self.h_dim, self.str_dim_size, bias=True)
        self.tc_linear = nn.Linear(self.h_dim, self.str_dim_size, bias=True)
        self.bilinear = BilinearMatrixAttention(self.str_dim_size, self.str_dim_size, use_input_biases=False, label_dim=1)

        self.fi_linear = nn.Linear(self.str_dim_size, 1, bias=False)

    def forward(self, sent_vecs, enc_sent_padding_mask):

        batch_size = sent_vecs.shape[0]
        sent_num = sent_vecs.shape[1]

        tp = torch.relu(self.tp_linear(sent_vecs))
        tc = torch.relu(self.tc_linear(sent_vecs))

        # Using the bilinear attention to compute f_jk: fjk = u^T_k W_a u_j
        scores = self.bilinear(tp, tc).view(batch_size, sent_num, sent_num)
        root = self.fi_linear(tp).view(batch_size, sent_num)  # bs, SENT_NUM, 1

        # masking out diagonal elements, see Eqt 2.1
        mask = scores.new_ones((scores.size(1), scores.size(1))) - scores.new_tensor(torch.eye(scores.size(1), scores.size(1))).cuda()
        mask = mask.unsqueeze(0).expand(scores.size(0), mask.size(0), mask.size(1))

        if self.args.not_sparse:
            A_ij = torch.exp(torch.tanh(scores))
            A_ij = (A_ij.transpose(-1,-2) * enc_sent_padding_mask.unsqueeze(-1)).transpose(-1,-2) * enc_sent_padding_mask.unsqueeze(-1) + 1e-6
            A_ij = A_ij * mask
            f_i = (torch.exp(torch.tanh(root)) * enc_sent_padding_mask) + 1e-6
        else:
            A_ij = torch.relu(scores)
            A_ij = (A_ij.transpose(-1,-2) * enc_sent_padding_mask.unsqueeze(-1)).transpose(-1,-2) * enc_sent_padding_mask.unsqueeze(-1) + 1e-6
            A_ij = A_ij * mask
            f_i = (torch.relu(root) * enc_sent_padding_mask) + 1e-6

        tmp = torch.sum(A_ij, dim=1)
        res = A_ij.new_zeros((batch_size, sent_num, sent_num)).cuda() #.to(self.device)
        #tmp = torch.stack([torch.diag(t) for t in tmp])
        res.as_strided(tmp.size(), [res.stride(0), res.size(2) + 1]).copy_(tmp)

        L_ij = -A_ij + res   #A_ij has 0s as diagonals

        L_ij_bar = L_ij.clone()
        L_ij_bar[:,0,:] = f_i

        LLinv = None
        LLinv = torch.inverse(L_ij_bar)

        d0 = f_i * LLinv[:,:,0]

        LLinv_diag = torch.diagonal(LLinv, dim1=-2, dim2=-1).unsqueeze(2)

        tmp1 = (A_ij.transpose(1,2) * LLinv_diag ).transpose(1,2)
        tmp2 = A_ij * LLinv.transpose(1,2)

        temp11 = A_ij.new_zeros((batch_size, sent_num, 1))
        temp21 = A_ij.new_zeros((batch_size, 1, sent_num))

        temp12 = A_ij.new_ones((batch_size, sent_num, sent_num-1))
        temp22 = A_ij.new_ones((batch_size, sent_num-1, sent_num))

        mask1 = torch.cat([temp11,temp12],2).cuda() #.to(self.device)
        mask2 = torch.cat([temp21,temp22],1).cuda() #.to(self.device)

        # Eqt: P(zjk = 1) = (1 − δ(j, k))AjkL¯−1kk − (1 − δ(j, 1))AjkL¯−1
        dx = mask1 * tmp1 - mask2 * tmp2

        return dx, d0

class BilinearMatrixAttention(nn.Module):
    """
    Computes attention between two matrices using a bilinear attention function.  This function has
    a matrix of weights ``W`` and a bias ``b``, and the similarity between the two matrices ``X``
    and ``Y`` is computed as ``X W Y^T + b``.
    Parameters
    ----------
    matrix_1_dim : ``int``
        The dimension of the matrix ``X``, described above.  This is ``X.size()[-1]`` - the length
        of the vector that will go into the similarity computation.  We need this so we can build
        the weight matrix correctly.
    matrix_2_dim : ``int``
        The dimension of the matrix ``Y``, described above.  This is ``Y.size()[-1]`` - the length
        of the vector that will go into the similarity computation.  We need this so we can build
        the weight matrix correctly.
    activation : ``Activation``, optional (default=linear (i.e. no activation))
        An activation function applied after the ``X W Y^T + b`` calculation.  Default is no
        activation.
    use_input_biases : ``bool``, optional (default = False)
        If True, we add biases to the inputs such that the final computation
        is equivalent to the original bilinear matrix multiplication plus a
        projection of both inputs.
    label_dim : ``int``, optional (default = 1)
        The number of output classes. Typically in an attention setting this will be one,
        but this parameter allows this class to function as an equivalent to ``torch.nn.Bilinear``
        for matrices, rather than vectors.
    """
    def __init__(self,
                 matrix_1_dim: int,
                 matrix_2_dim: int,
                 use_input_biases: bool = False,
                 label_dim: int = 1) -> None:
        super(BilinearMatrixAttention, self).__init__()
        if use_input_biases:
            matrix_1_dim += 1
            matrix_2_dim += 1

        if label_dim == 1:
            self._weight_matrix = Parameter(torch.Tensor(matrix_1_dim, matrix_2_dim))
        else:
            self._weight_matrix = Parameter(torch.Tensor(label_dim, matrix_1_dim, matrix_2_dim))

        self._bias = Parameter(torch.Tensor(1))
        self._use_input_biases = use_input_biases
        self.reset_parameters()

    def reset_parameters(self):
        torch.nn.init.xavier_uniform_(self._weight_matrix)
        self._bias.data.fill_(0)

    def forward(self, matrix_1: torch.Tensor, matrix_2: torch.Tensor) -> torch.Tensor:

        if self._use_input_biases:
            bias1 = matrix_1.new_ones(matrix_1.size()[:-1] + (1,))
            bias2 = matrix_2.new_ones(matrix_2.size()[:-1] + (1,))

            matrix_1 = torch.cat([matrix_1, bias1], -1)
            matrix_2 = torch.cat([matrix_2, bias2], -1)

        weight = self._weight_matrix
        if weight.dim() == 2:
            weight = weight.unsqueeze(0)
        intermediate = torch.matmul(matrix_1.unsqueeze(1), weight)
        final = torch.matmul(intermediate, matrix_2.unsqueeze(1).transpose(2, 3))
        return final.squeeze(1)


# -*- coding: utf-8 -*-
"""
Created on Tue May 4 14:37:57 2021

@author: weixiong001

Use my ml script with multiple models, on multiple targes
Customised for non DGE and GO class labels. Modified from multi_go_wrapper.py
in ~/machine_learning/ml_go_workflow/cores_test
"""
import csv
import os
import sys

# GO target as class label
TARGET = sys.argv[1]
JOB_ID = sys.argv[2]
ML_FILE = 'ml_dataset_dc.txt'

# Class label, ressaignment of variable is done to make it compatible with
# downstream code
one_target = '"' +  TARGET + '"'
# Hack just to take care of this outlier when there's a space at the end of the feature name
if one_target == '"cin_ATB2/AtbZIP53/AtbZIP44/GBF5 BS in ProDH"':
    one_target = '"cin_ATB2/AtbZIP53/AtbZIP44/GBF5 BS in ProDH "'
# Various scripts are possible
os.system('python rf_contf_v2.py ' + one_target + ' ' + ML_FILE + ' ' + JOB_ID)


"""
Base implementation of fullCBF format - as used with Dectris detectors
amongst others - this will read the header and construct the full model,
but will allow for extension for specific implementations of CBF.
"""

from __future__ import annotations

import sys

import numpy as np
import pycbf

from iotbx.detectors.cbf import CBFImage
from scitbx.array_family import flex

from dxtbx.format.Format import bz2, gzip
from dxtbx.format.FormatCBF import FormatCBF
from dxtbx.format.FormatStill import FormatStill

# Does the pybf library expose read_buffer?
if hasattr(pycbf.cbf_handle_struct, "read_buffer"):

    def cbf_read_buffer(handle, buffer, flags):
        return handle.read_buffer(buffer, flags)

else:
    from dxtbx.format.image import cbf_read_buffer

class FormatCBFFull(FormatCBF):
    """An image reading class for full CBF format images i.e. those from
    a variety of cameras which support this format."""

    @staticmethod
    def understand(image_file):
        """Check to see if this looks like an CBF format image, i.e. we can
        make sense of it."""

        header = FormatCBF.get_cbf_header(image_file)

        if "_diffrn.id" not in header and "_diffrn_source" not in header:
            return False

        return True

    def __init__(self, image_file, **kwargs):
        """Initialise the image structure from the given file."""

        # It appears Pycbf can not handle unicode filenames (see dials/dials#256)
        self._raw_data = None
        super().__init__(str(image_file), **kwargs)

    def __del__(self):
        self._cbf_handle.__swig_destroy__(self._cbf_handle)

    def _get_cbf_handle(self):
        try:
            return self._cbf_handle
        except AttributeError:
            self._cbf_handle = pycbf.cbf_handle_struct()

            buffer = None
            # Reopen to tell if it's a gzip - this should be cached, so fast
            with FormatCBF.open_file(self._image_file, "rb") as fin:
                # If this was a gzip or bzip file, read as a buffer
                if (gzip and isinstance(fin._cache_object._file, gzip.GzipFile)) or (
                    bz2 and isinstance(fin._cache_object._file, bz2.BZ2File)
                ):
                    buffer = fin.read()

            if buffer:
                cbf_read_buffer(self._cbf_handle, buffer, pycbf.MSG_DIGEST)
            else:
                self._cbf_handle.read_widefile(
                    self._image_file.encode(), pycbf.MSG_DIGEST
                )

            return self._cbf_handle

    def _goniometer(self):
        """Return a working goniometer instance."""

        return self._goniometer_factory.imgCIF_H(self._get_cbf_handle())

    def _detector(self):
        """Return a working detector instance."""

        return self._detector_factory.imgCIF_H(self._get_cbf_handle(), "unknown")

    def _beam(self):
        """Return a working beam instance."""

        return self._beam_factory.imgCIF_H(self._get_cbf_handle())

    def _scan(self):
        """Return a working scan instance."""

        return self._scan_factory.imgCIF_H(self._image_file, self._get_cbf_handle())

    def detectorbase_start(self):
        self.detectorbase = CBFImage(self._image_file)
        self.detectorbase.readHeader()

    def get_raw_data(self):
        """Get the pixel intensities (i.e. read the image and return as a
        flex array."""
        if self._raw_data is not None:
            return self._raw_data

        self.detectorbase_start()
        try:
            image = self.detectorbase
            image.read()
            self._raw_data = image.get_raw_data()

            return self._raw_data
        except Exception:
            return None

class FormatCBFFullStill(FormatStill, FormatCBFFull):
    """An image reading class for full CBF format images i.e. those from
    a variety of cameras which support this format. Custom derived from
    the FormatStill to handle images without a gonimeter or scan"""

    @staticmethod
    def understand(image_file):
        """Check to see if this looks like an CBF format image, i.e. we can
        make sense of it."""

        header = FormatCBF.get_cbf_header(image_file)

        if "_diffrn.id" not in header and "_diffrn_source" not in header:
            return False

        # According to ImageCIF, "Data items in the DIFFRN_MEASUREMENT_AXIS
        # category associate axes with goniometers."
        # http://www.iucr.org/__data/iucr/cifdic_html/2/cif_img.dic/Cdiffrn_measurement_axis.html
        if "diffrn_measurement_axis" in header:
            return False

        # This implementation only supports single panel.
        try:
            cbf_handle = pycbf.cbf_handle_struct()
            cbf_handle.read_widefile(image_file.encode(), pycbf.MSG_DIGEST)
        except Exception as e:
            if "CBFlib Error" in str(e):
                return False

        # check if multiple arrays
        try:
            return cbf_handle.count_elements() == 1
        except Exception as e:
            if "CBFlib Error" in str(e):
                return False

        return True

    def get_raw_data(self):
        """Get the pixel intensities (i.e. read the image and return as a
        flex array."""
        if self._raw_data is not None:
            return self._raw_data

        # Override parent's get_raw_data, which relies on iotbx, which in turn
        # relies on cbflib_adaptbx, which in turn expects a gonio
        cbf = self._get_cbf_handle()

        cbf.find_category(b"array_structure")
        cbf.find_column(b"encoding_type")
        cbf.select_row(0)
        types = []
        for i in range(cbf.count_rows()):
            types.append(cbf.get_value())
            cbf.next_row()
        assert (
            len(types) == cbf.count_rows() == 1
        )  # multi-tile data read by different class
        dtype = types[0]

        # find the data
        cbf.select_category(0)
        while cbf.category_name().lower() != b"array_data":
            try:
                cbf.next_category()
            except Exception:
                return None
        cbf.select_column(0)
        cbf.select_row(0)

        cbf.find_column(b"data")
        assert cbf.get_typeofvalue().find(b"bnry") > -1

        # handle floats vs ints
        if dtype == b"signed 32-bit integer":
            array_string = cbf.get_integerarray_as_string()
            self._raw_data = flex.int(np.fromstring(array_string, np.int32))
            parameters = cbf.get_integerarrayparameters_wdims_fs()
            slow, mid, fast = (parameters[11], parameters[10], parameters[9])
            assert slow == 1  # sections not supported
            array_size = mid, fast
        elif dtype == b"signed 64-bit real IEEE":
            array_string = cbf.get_realarray_as_string()
            self._raw_data = flex.double(np.fromstring(array_string, float))
            parameters = cbf.get_realarrayparameters_wdims_fs()
            slow, mid, fast = (parameters[7], parameters[6], parameters[5])
            assert slow == 1  # sections not supported
            array_size = mid, fast
        else:
            return None  # type not supported

        self._raw_data.reshape(flex.grid(*array_size))
        return self._raw_data

if __name__ == "__main__":
    for arg in sys.argv[1:]:
        print(FormatCBFFull.understand(arg))


import gtk
import vte

"""
This module implements a class responsible for create a window
with a terminal and turn possible to execute programs in this terminal.
"""

class RunTerminal(object):
    def __init__(self):
        """
        Constructor.
        """
        self.window = gtk.Window()
        self.window.set_title("Running Python Script")

        self.terminal = vte.Terminal()

        self.window.add(self.terminal)
        self.window.show_all()
        self.__apply_preferences()

        self.terminal.connect("child-exited", self.child_exited)
        self.window.connect("delete-event", lambda *x: self.window.destroy())

    #################### Public Methods ####################

    def run(self, *command):
        """
        Run a command in terminal window

        @param *command: first parameter is the command and
                         the next ones are the first command arguments.
        @type *command: Strings.
        """
        pid = self.terminal.fork_command(command[0], command)

    #################### Private Methods ####################

    def __apply_preferences(self):
        """
        Set terminal font, size and colors.
        """
        self.terminal.set_font_from_string("Bitstream Vera Sans Mono 12")
        self.terminal.set_color_foreground(gtk.gdk.color_parse('#444444'))
        self.terminal.set_color_background(gtk.gdk.color_parse('#ffffff'))
        self.terminal.set_color_cursor(gtk.gdk.color_parse('#444444'))
        self.terminal.set_cursor_shape(vte.CURSOR_SHAPE_UNDERLINE)
        self.terminal.set_size(30, 19)

    #################### Callbacks ####################

    def child_exited(self, widget):
        self.terminal.connect("key-press-event", self.key_press)

    def key_press(self, widget, event):
        exit_values = (gtk.keysyms.Escape, gtk.keysyms.Return)
        exit_ctrl_values = (gtk.keysyms.c, gtk.keysyms.d)

        if event.state & gtk.gdk.CONTROL_MASK and event.keyval in exit_ctrl_values:
            self.window.destroy()
        elif event.keyval in exit_values:
            self.window.destroy()


import pymongo
from config import MONGO_PORT

def searchRower(name):

	client = pymongo.MongoClient('localhost', MONGO_PORT)
	db = client['C150']
	db.drop_collection(name)
	RowerDB = db[name]
	RowerData = []

	Tests = ['Max Watt', 'Twenty Minute', 'One Minute', 'Forty Minute', 'Rep Max', '4 by 5 Minute', \
				'5 by 5 Minute', '2 by 15 Minute', '2 by 20 Minute', '3 by 3 by 90 Second', '3 by 3 by 2 Minute']

	# Put all test items specific to one rower into a new collection
	for test in Tests:
		database = db[test]
		i = 1
		for rower in database.find({'Name': name}).sort([('Year', pymongo.ASCENDING), \
														('Month', pymongo.ASCENDING), \
														('Day', pymongo.ASCENDING), \
														('Rank', pymongo.ASCENDING)] ):
			# print rower
			RowerData.append(rower)
			query = {'Day': rower['Day'], \
					'Month': rower['Month'], \
					'Year': rower['Year'], \
					'Name': rower['Name'], \
					'Test': rower['Test']}
			update = rower
			update['index'] = i
			RowerDB.update(query, update, True)
			i += 1

	rower_items = {'items': RowerData}
	return rower_items

	client.close()

def searchTest(test):
	client = pymongo.MongoClient('localhost', MONGO_PORT)
	db = client['C150']
	RowerDB = db[test]
	RowerData = []
	Dates = []
	result_dicts = []
	# Put all test items specific to one test into a new collection
	for rower in RowerDB.find({'Test': test}).sort([('Year', pymongo.ASCENDING), \
													('Month', pymongo.ASCENDING), \
													('Day', pymongo.ASCENDING), \
													('Rank', pymongo.ASCENDING)] ):
		if rower['Date'] not in Dates:
			Dates.append(rower['Date'])
		RowerData.append(rower)

	i = 1
	for date in Dates:
		temp = []
		for rower in RowerData:
			if rower['Date'] == date:
				temp.append(rower)
		temp_dict = {}
		temp_dict['rowers'] = temp
		temp_dict['date'] = date
		result_dicts.append(temp_dict)
		i += 1

	return result_dicts

	client.close()


import requests
import urllib
import csv
import json

API_BASE_URL = "https://apis.datos.gob.ar/georef/api/"

def get_similar_bulk(direcciones):
    """Normaliza una lista de nombres de alguna de las entidades geográficas."""

    # realiza consulta a la API
    endpoint = "direcciones"
    data = {
        "direcciones": [
            {"direccion": direccion["direccion"], "departamento": direccion["departamento"], "provincia": "buenos aires", "localidad": direccion["localidad"], "orden": "id","campos": "ubicacion.lat, ubicacion.lon", "max": "1"}
            for direccion in direcciones
    ]}
    url = API_BASE_URL + endpoint
    results = requests.post(
        url, json=data, headers={"Content-Type": "application/json"}
    ).json()

    # convierte a una lista de "resultado más probable" o "vacío" cuando no hay
    parsed_results = [
        single_result[endpoint][0] if single_result[endpoint] else {}
        for single_result in results["resultados"]
    ]

    return parsed_results

#direcciones = get_similar_bulk([{"direccion": "pueyrredon", "localidad": "gral rod", "departamento": "general rodriguez"}])
#print(direcciones)

f = open(r'C:\Users\esosa\Desktop\Nominatim\rsvdinamicas\informes\personal.json')
data=json.load(f)
json_str = json.dumps(data)
resp = json.loads(json_str)

with open('informes\personal.json', 'r', encoding='utf-8') as json_file:
    for x in resp:
        id=x['record_id']
        provincias = get_similar_bulk([{"direccion":x["direccion"] + " " + x["altura"], "departamento": x["partido"], "localidad": x["localidad"]}])
        #with open('informes/normalizar.csv', 'a', newline='') as csvfile:
            #spamwriter = csv.writer(csvfile)
            #spamwriter.writerow(["{};{};{};{};{};{}" .format(x['record_id'],x["hospital"],x["fecha_ingreso"],provincias,x["uti"],x["dx_egreso"])])
        with open('informes/normalizar.json', 'a') as json_file1:
            json.dump(provincias, json_file1)


import numpy as np
import pandas as pd
import datetime

from copy import deepcopy
from scipy.stats import norm

from propdesk_estimators.data_raw_transformation import make_datetime_index, drop_columns_df

from pandarallel import pandarallel
pandarallel.initialize()

def ZMA(prices_df, bandwidth: int = 5, annualized_volatility: bool = False, log_price: bool = True):
    """
    Two-scale realized variance
    Inputs:
        - prices_df: pd.dataframe
        - bandwith: int | size of sliding window
        - annualized_volatility: bool | use annualized volatility if True
    Output:
        - estimation: float
    """
    M = prices_df.shape[0]

    if log_price:
        prices_df = np.log(prices_df)

    returns = prices_df.diff().dropna()

    M_bar = (M - bandwidth + 1)/bandwidth
    gamma01 = (returns**2).sum()
    avg = 0

    for q in range(bandwidth):
        returns = prices_df.iloc[q::bandwidth].diff().dropna()
        avg += (returns**2).sum()

    avg = avg/bandwidth

    estimation = (avg - (M_bar/M) * gamma01) / (1 - M_bar/M)

    estimation = estimation / M

    if annualized_volatility:
        estimation = np.sqrt(estimation / ((prices_df.index[1] - prices_df.index[0]).total_seconds() / 3600) * 24 * 365) / prices_df.mean()

    return estimation

def calc_volatility(t_datetime, quotes_df, resample_rule_str, seconds_int, bandwidth_int, annualized_volatility_bool, log_price_bool, mid_quote_change_bool):
    """
    Function to prepare and apply ZMA estimation to each t_datetime row from volatility_estimation_df
    Inputs:
        - t_datetime : datetime
        - start_date_datetime: first date to calculate volatility
        - quotes_df: the quotes dataset to calculate volatility from
        - resample_rule_str: resampling rule to apply on quotes (e.g. 100mS)
        - seconds_int: lookback time, in seconds, to slice quotes dataset
        - bandwidth_int: ZMA estimator parameter
        - annualized_volatility_bool: ZMA estimator parameter
        - log_price_bool: use log of prices as input
    Output:
        - volatility_estimation: single estimation for t_datetime
    """

    # get exactly the midquotes we need between t_datetime and lookback_seconds
    delta_t_datetime = t_datetime - datetime.timedelta(seconds=seconds_int)
    prices_df = quotes_df['mid_quote'][quotes_df['datetime'].between(delta_t_datetime, t_datetime)]
    # resampling mid quotes to compute volatility
    prices_df = resample_mid_quote(prices_df, resample_rule_str)

    if mid_quote_change_bool:
        prices_df = prices_df[prices_df.diff()!=0]

    if not prices_df.any():
        return np.NaN

    volatility_estimation = ZMA(prices_df, bandwidth_int, annualized_volatility_bool, log_price_bool)

    return volatility_estimation

def vpin(bucket_i, vpin_bucket_df, col, volume_bucket_size, n=50):

    n_buckets = bucket_i+n-1
    available_buckets = vpin_bucket_df.index.max()

    # we need at least 80% of specified number of buckets
    if n_buckets > available_buckets:
        if int(0.8*n_buckets) > available_buckets:
            return np.nan
        n_buckets = available_buckets-1

    window_df = vpin_bucket_df.loc[bucket_i: n_buckets]
    vpin = window_df[col].sum()/(n*volume_bucket_size)

    return vpin

def calc_vpin_df(trades_df, resampling_rule_str='15T', original_trades_sampling_rule='1T', n_buckets=50):
    '''
    trades_df must have columns volume and return per time bar
    '''

    if 'volume' not in trades_df.columns or 'return' not in trades_df.columns:
        raise Exception('Input trades df in vpin calculation must contain columns volume and return')

    vpin_df = deepcopy(trades_df)
    return_std = vpin_df['return'].std()

    # calculate volume bars (buckets) looking back on data
    volume_bucket_size = np.mean(vpin_df[['volume']].resample(resampling_rule_str).sum().values)

    vpin_df['buy_prob'] = norm(loc=0, scale=1).cdf(vpin_df['return']/return_std)
    vpin_df['sell_prob'] = 1 - vpin_df['buy_prob']
    vpin_df['buy_volume'] = vpin_df['volume']*vpin_df['buy_prob']
    vpin_df['sell_volume'] = vpin_df['volume']*vpin_df['sell_prob']

    cumm_volume = 0
    bucket = 0
    bucket_array = []

    for volume in vpin_df.volume:
        cumm_volume += volume
        bucket_array.append(bucket)
        if cumm_volume >= volume_bucket_size:
            bucket += 1
            cumm_volume = 0
        if bucket > n_buckets:
            break

    vpin_df = vpin_df.iloc[:len(bucket_array)]
    vpin_df['bucket'] = bucket_array

    vpin_bucket_df = vpin_df[['volume', 'buy_volume', 'sell_volume', 'bucket']].groupby(by=['bucket']).sum()

    vpin_bucket_df['abs_order_imbalance'] = np.abs(vpin_bucket_df['buy_volume'] - vpin_bucket_df['sell_volume'])
    vpin_bucket_df['order_imbalance'] = vpin_bucket_df['buy_volume'] - vpin_bucket_df['sell_volume']

    vpin_bucket_df['datetime_end'] = vpin_df[['datetime', 'bucket']].groupby(by=['bucket']).max()
    vpin_bucket_df['datetime_start'] = vpin_df[['datetime', 'bucket']].groupby(by=['bucket']).min()

    vpin_bucket_df['bucket'] = vpin_bucket_df.index
    vpin_bucket_0 = vpin(0, vpin_bucket_df, 'abs_order_imbalance', volume_bucket_size, n=n_buckets)
    vpin_signed_bucket_0 = vpin(0, vpin_bucket_df, 'order_imbalance', volume_bucket_size, n=n_buckets)
    sign_vpin = np.sign(vpin_signed_bucket_0)

    return pd.Series([vpin_bucket_0, vpin_signed_bucket_0, sign_vpin])

def calc_vpin_out_of_sample_df(trades_df, window_days, real_time=False, resampling_rule_str='15T', original_trades_sampling_rule='1T', n_buckets=50):
    '''
    trades_df must have columns volume and return per time bar
    '''

    if 'volume' not in trades_df.columns or 'return' not in trades_df.columns:
        raise Exception('Input trades_df in vpin calculation must contain columns volume and return')

    if not trades_df.index.is_monotonic_increasing:
        raise Exception('Input trades_df in vpin calculation must be indexed by ascending timestamp')

    vpin_df = deepcopy(trades_df)[['volume', 'return']]

    if trades_df.index.is_monotonic_increasing:
        vpin_df = vpin_df.sort_index(ascending=False)

    vpin_df['datetime'] = vpin_df.index

    last = max(vpin_df.index)

    if real_time:
        real_time_vpin_df = deepcopy(vpin_df.iloc[[0]])
        real_time_vpin_df[['vpin', 'vpin_signed', 'sign_vpin']] = real_time_vpin_df['datetime'].apply(lambda x: calc_vpin_df(vpin_df.loc[x:str(pd.to_datetime(last)-datetime.timedelta(days=window_days))]))
        return real_time_vpin_df[['vpin', 'vpin_signed', 'sign_vpin']]

    vpin_df[['vpin', 'vpin_signed', 'sign_vpin']] = vpin_df['datetime'].parallel_apply(lambda x: calc_vpin_df(vpin_df.loc[x:str(pd.to_datetime(x)-datetime.timedelta(days=window_days))]))
    return vpin_df[['vpin', 'vpin_signed', 'sign_vpin']].sort_index(ascending=True)


import psutil
import socket
import customtkinter

def internet():
    UPDATE_DELAY = 1 # in seconds

    def get_size(bytes):
        """
        Returns size of bytes in a nice format
        """
        for unit in ['', 'K', 'M', 'G', 'T', 'P']:
            if bytes < 1024:
                return f"{bytes:.2f}{unit}B"
            bytes /= 1024

    # get the network I/O stats from psutil
    io = psutil.net_io_counters()
    # extract the total bytes sent and received
    bytes_sent, bytes_recv = io.bytes_sent, io.bytes_recv
    io_2 = psutil.net_io_counters()

    root = customtkinter.CTk()

    # This is the section of code which creates the main window
    root.geometry('307x90')
    root.title('Internet')

    connection = customtkinter.CTkLabel(root,text='Checking ...')
    connection.pack()

    def is_connected():
        try:
            socket.create_connection(("zlelo.github.io", 80))
            state = "Online"
        except OSError:
            state = "Offline"
        connection.configure(text = f'Internet status: {state}')
        root.after(100000, is_connected) # do checking again 10 seconds later

    is_connected() # start the checking

    download = customtkinter.CTkLabel(root, text=f'Total Download: {get_size(io_2.bytes_recv)}', anchor="center").pack()

    upload = customtkinter.CTkLabel(root, text=f'Total Upload: {get_size(io_2.bytes_sent)}', anchor="center").pack()

    root.mainloop()


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

from django.db import models, migrations
from django.conf import settings

class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='Developer',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('extra_credit_tokens', models.IntegerField(default=0)),
                ('manager', models.CharField(max_length=30)),
                ('title', models.CharField(max_length=30)),
                ('user', models.OneToOneField(to=settings.AUTH_USER_MODEL)),
            ],
        ),
        migrations.CreateModel(
            name='DeveloperSkill',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('proficiency', models.CharField(max_length=30)),
                ('years_of_experience', models.IntegerField()),
                ('developer', models.ForeignKey(to='skills_site.Developer')),
            ],
        ),
        migrations.CreateModel(
            name='ExtraCredit',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('description', models.TextField(null=True, blank=True)),
                ('date_credited', models.DateField(auto_now_add=True)),
                ('recipient', models.ForeignKey(related_name='extracredit_recipient', to='skills_site.Developer')),
                ('sender', models.ForeignKey(related_name='extracredit_sender', to='skills_site.Developer')),
            ],
        ),
        migrations.CreateModel(
            name='Skill',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=512)),
                ('difficulty', models.IntegerField()),
                ('family', models.CharField(max_length=256)),
            ],
        ),
        migrations.AddField(
            model_name='extracredit',
            name='skill',
            field=models.ForeignKey(related_name='extracredit_skill', to='skills_site.Skill'),
        ),
        migrations.AddField(
            model_name='developerskill',
            name='skill',
            field=models.ForeignKey(to='skills_site.Skill'),
        ),
    ]


"""def decorator_func(my_function):

	def wrapper_func(*args, **kwargs):

		print (f"This is the wrapper function executiong before {my_function.__name__}")

		return my_function(*args, **kwargs)
	return wrapper_func

@decorator_func
def test():
	print ("This is a test that worked")

@decorator_func
def display_info(name, age):
	print (f"This function is running with the [ {name} ] and [ {age} ] arguments")

test()
display_info("Robert", 55)"""

def my_logger(original_function):
	import logging
	logging.basicConfig(filename=f'{original_function.__name__}', level = logging.INFO)

	def wrapper(*args, **kwargs):
		logging.info(
			f'ran with args {args} and kwargs {kwargs}')

		return original_function(*args, **kwargs)
	return wrapper

def my_timer (original_function):
	import time
	def wrapper(*args, **kwargs):
		t1 = time.time()
		result = original_function(*args, **kwargs)
		t2 = time.time() - t1
		print (f"{original_function} ran in {t2} seconds")
	return wrapper

import time

@my_logger
@my_timer
def display_info(name, age):
	time.sleep(2)
	print (f"The name is {name} and the age is {age}")

display_info("Robert", 27)
display_info("Breanna", 78)
display_info("Alicia", 17)
display_info("Jason", 4)


from numpy import asarray, rot90
import numpy as np
import trimesh
#from PIL import Image

dry_run=False ## Change this to False to execute creating for real

startx=60 ## Positions to start building in Minecraft world
starty=78
startz=60
block_type=1 # Tyfe of block used (41 is gold)

if dry_run==False:
    from mcpi import minecraft
    ## connecting using ip and port for your Spigot server + RaspberryJuice mod installed
    mc = minecraft.Minecraft.create(address="192.168.0.15", port=4712)

mesh = trimesh.load_mesh('/home/yurick/Eiffel.stl') ## This is a 3d - model file from Thingiverse
assert(mesh.is_watertight) # you cannot build a solid if your volume is not tight
volume = mesh.voxelized(pitch=2) ## we make it twice smaller here
mat = volume.matrix # matrix of boolean
m=rot90(asarray(mat),3,axes=(2, 1)) ##rotate to correct(vertical) position

#from IPython.display import Image
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

### Let's show the model we are creating !

fig = plt.figure(figsize=((20,20)))
ax = fig.gca(projection='3d', ymargin=0.2)
ax.voxels (mat,  edgecolor='k')
plt.show()
### Let's build in Minecraft world !
if dry_run==False:
    mc.setBlocks(startx,starty,startz,startx+m.shape[0],starty+m.shape[1],startz+m.shape[2],0)
    for ix,x in enumerate (m):

        for iy,y in enumerate (x):

            for iz,z in enumerate(y):
                if z:
                    mc.setBlock(startx+ix,m.shape[1]+starty-iy,startz+iz,block_type)

    mc.player.setPos(startx-5, starty+1, startz-5)# let's send a player to see what we have built

#print (mat.shape)


from peewee import *
from fsttrpgcharloader.database import Actor, DBManager as ActorDBManager

lifepath_db = SqliteDatabase('lifepath.db')

class Event(Model):
    actor = ForeignKeyField(Actor, related_name='actors')
    year = IntegerField()
    month = IntegerField(default=1)
    length = IntegerField(default=1)
    event_chain = CharField()

    def add_event(self, actor_role, actor_name, year, event_chain, month=1, length=1):
        act = Actor.add_or_get(role=actor_role, name=actor_name)
        event, created = Event.get_or_create(actor=act,
                                             year=year,
                                             month=month,
                                             defaults={'event_chain': event_chain,
                                                       'length': length})
        if created:
            print('created new event')
            return True
        else:
            print('didnt create new event')
            return False

    class Meta:
        database = lifepath_db

class Enemy(Model):
    life_event = ForeignKeyField(Event, related_name='events')
    enemy_actor = ForeignKeyField(Actor, related_name='enemy_actors')
    relationship = CharField()
    resources = CharField()

    class Meta:
        database = lifepath_db

class DBManager(object):
    def __init__(self):
        self.actor_db_mgr = ActorDBManager()
        lifepath_db.connect()
        lifepath_db.create_tables([Event], safe=True)

        self.events = Event()

    def __del__(self):
        if lifepath_db:
            lifepath_db.close()


import numpy as np
import scipy.sparse as sp
import torch
import pandas as pd

def citeceer_loader(train_val_test=[0.2,0.2,0.6]):
    """loading data from the data set """
    print('Loading citeseer dataset...')
    # get the data from dataset
    part_sum=train_val_test[0]+train_val_test[1]+train_val_test[2]
    assert part_sum==1,"sum of train,val,test should be one "
    data_content = pd.read_csv('../data/citeseer/citeseer.content',sep='\t',header=None)
    data_edge = pd.read_csv('../data/citeseer/citeseer.cites',sep='\t',header=None)

    data_idx=list(data_content.index)
    paper_id=list(data_content.iloc[:,0])
    data_map=dict(zip(paper_id,data_idx))
    bad_data_index=[]
    for i in range(data_edge.shape[0]):
        if (not data_edge.iloc[i][0] in data_map.keys()) or (not data_edge.iloc[i][1] in data_map.keys()) or  (data_edge.iloc[i][1] ==data_edge.iloc[i][0]):
            bad_data_index.append(i)
    data_edge=data_edge.drop(bad_data_index,axis=0)
    data_edge=data_edge.applymap(data_map.get)
    labels=data_content.iloc[:,-1]
    labels=pd.get_dummies(labels)
    features= data_content.iloc[:,1:-1]
    node_number=data_content.shape[0]
    adj=np.eye(node_number)
    for i,j in zip(data_edge[0],data_edge[1]):
        adj[i][j]=adj[j][i]=1

    # build graph# idx_map is maping the index of city to the consecutive integer

    idx_test = range(int(node_number*train_val_test[0]))
    idx_train = range(int(node_number*train_val_test[0]),int(node_number*train_val_test[1]+node_number*train_val_test[0]))
    idx_val = range(int(node_number*train_val_test[1]+node_number*train_val_test[0]), node_number)

    idx_train = torch.LongTensor(idx_train)
    idx_val = torch.LongTensor(idx_val)
    idx_test = torch.LongTensor(idx_test)

    adj = torch.FloatTensor(np.array(adj))
    features = torch.FloatTensor(np.array(features))
    labels = torch.LongTensor(np.where(labels)[1])

    adj=normalize_adj(adj)
    features=normalize_features(features)
    adj = torch.FloatTensor(np.array(adj.todense()))
    features = torch.FloatTensor(np.array(features))
    labels = torch.LongTensor(labels)
    print("successfully loading data from dataset citeceer")
    return adj, features,idx_train, idx_val, idx_test, labels

def normalize_adj(mx):
    """Row-normalize sparse matrix"""
    mx=sp.csr_matrix(mx)
    rowsum = np.array(mx.sum(1))
    r_inv_sqrt = np.power(rowsum, -0.5).flatten()
    r_inv_sqrt[np.isinf(r_inv_sqrt)] = 0.
    r_mat_inv_sqrt = sp.diags(r_inv_sqrt)
    return mx.dot(r_mat_inv_sqrt).transpose().dot(r_mat_inv_sqrt)

def normalize_features(mx):
    """Row-normalize sparse matrix"""
    rowsum = np.array(mx.sum(1))
    r_inv = np.power(rowsum, -1).flatten()
    r_inv[np.isinf(r_inv)] = 0.
    r_mat_inv = sp.diags(r_inv)
    mx = r_mat_inv.dot(mx)
    return mx


import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk, ImageDraw, ImageFont

class WatermarkApp(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Image Watermark App")
        self.minsize(width=1000, height=700)
        self.config(padx=20, pady=30)

        self.canvas = tk.Canvas(width=500, height=680)
        self.welcome_img = tk.PhotoImage(file=r"welcome.png")
        self.canvas_img = self.canvas.create_image((0, 0), image=self.welcome_img, anchor="nw")
        self.canvas.grid(row=0, column=0, rowspan=5)

        self.select_button = tk.Button(text="Choose an Image", command=self.select_image)
        self.select_button.grid(row=0, column=1, columnspan=2)

        self.watermark_label = tk.Label(text="Enter Your Watermark")
        self.watermark_label.grid(row=1, column=1)

        self.watermark_entry = tk.Entry(width=50)
        self.watermark_entry.grid(row=1, column=2, padx=40)

        self.add_button = tk.Button(text="Add Watermark", command=self.add_watermark)
        self.add_button.grid(row=2, column=1, columnspan=2)

        self.save_button = tk.Button(text="Save Watermarked Image", command=self.save_watermarked_image)
        self.save_button.grid(row=3, column=1, columnspan=2)

        self.img_name = None
        self.selected_img = None
        self.watermark = None
        self.filename = None
        self.result_img = None

    def select_image(self):
        f_types = [('Jpg Files', '*.jpg'), ('PNG Files', '*.png')]
        self.filename = filedialog.askopenfilename(initialdir="C:/Users/MUC/Downloads/",
                                                   title="Select an Image",
                                                   filetypes=f_types)
        self.img_name = self.filename.split("/")[4]
        img = Image.open(self.filename)
        resized_img = img.resize((400, 600))
        self.selected_img = ImageTk.PhotoImage(resized_img)
        self.canvas.itemconfig(self.canvas_img, image=self.selected_img)

    def add_watermark(self):
        opened_image = Image.open(self.filename)

        self.watermark = self.watermark_entry.get()

        img_width, img_height = opened_image.size
        draw = ImageDraw.Draw(opened_image)

        font_size = int(img_width / 10)
        font = ImageFont.truetype("arial.ttf", font_size)

        draw.text((0, 0), self.watermark, font=font, fill="#FFF8E1", stroke_width=5, stroke_fill="#FFE5F1")

        resized_opened_image = opened_image.resize((400, 600))
        self.result_img = ImageTk.PhotoImage(resized_opened_image)

        self.canvas.itemconfig(self.canvas_img, image=self.result_img)

    def save_watermarked_image(self):
        path = filedialog.asksaveasfile(title="Save as",
                                        confirmoverwrite=True,
                                        defaultextension="png",
                                        filetypes=[
                                            ("jpeg", ".jpg"),
                                            ("png", ".png"),
                                            ("bitmap", "bmp"),
                                            ("gif", ".gif")
                                        ])
        if path is not None:
            watermarked_img = ImageTk.getimage(self.result_img)
            watermarked_img.convert("RGB").save(path)

        self.watermark_entry.delete(0, tk.END)
        self.canvas.itemconfig(self.canvas_img, image=self.welcome_img)
        print("Saved Successfully")

app = WatermarkApp()
app.mainloop()


from bs4 import BeautifulSoup
import pandas as pd
import requests
import re
import json
from tqdm import tqdm
import sys
import time
import pandas as pd

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

# Initiate the browser
browser  = webdriver.Chrome(ChromeDriverManager().install())

# Open the Website
browser.get('https://www.investing.com/')

# ========= Prealocacion Variables =========

precioFinal = 0
delta = 0
deltaPorcentual = 0

# ========= Buscar e ingresar a instrumento =========
# Ingresar a bucador
buscador = browser.find_element_by_xpath('//input[@placeholder="Search the website..."]')

try:
    buscador.click()
except:
    browser.find_element_by_xpath('//i[@class="popupCloseIcon largeBannerCloser"]').click()

# Se ingresa instrumento
buscador.send_keys("Apple")
time.sleep(1.5)

# Se ingresa al primer resultado
buscador  = browser.find_element_by_xpath('//span[@class="second js-quote-item-symbol symbolName"]')
buscador.click()

# Se obtiene nemotecnico del instrumento
nemotecnico  = browser.find_element_by_xpath('//h2[@class="text-lg font-semibold"]').text.split()[0]

# Se obtiene delta del precio
delta = float(browser.find_element_by_xpath('//span[@data-test="instrument-price-change"]').text)

# ========= Data Historica =========

browser.find_element_by_link_text("Historical Data").click()

fecha = pd.to_datetime(browser.find_element_by_xpath('//td[@class="datatable_cell__3gwri font-bold"]').text)

precioFinal = float(browser.find_element_by_xpath('//td[@class="datatable_cell__3gwri datatable_cell--align-end__Wua8C datatable_cell--down__2CL8n"]').text)

volumen =  browser.find_element_by_xpath('//td[@data-test="volume-cell"]').text
volumen = float(volumen.replace("M", ""))

deltaPorcentual = browser.find_element_by_xpath('//td[@class="datatable_cell__3gwri datatable_cell--align-end__Wua8C datatable_cell--down__2CL8n font-bold"]').text
deltaPorcentual = deltaPorcentual[deltaPorcentual.find("(")+1:deltaPorcentual.rfind(")")]
deltaPorcentual = float(deltaPorcentual.replace("%", ""))

# ========= Articulos =========

# Se ingresa a seccion de noticias
browser.find_element_by_link_text("News & Analysis").click()

# Se obtiene url de noticias de articulo
urlArticulos = browser.current_url #+ "/"

browser.close()

flag = True
# numPagina = 1

# while flag:
url = requests.get(urlArticulos,  headers={'User-Agent': 'Mozilla/5.0'})
soup = BeautifulSoup(url.content, "html.parser")

primerArticulo =  soup.find("article", class_ = "js-article-item articleItem   ")

flag = False

# titulosSelenium = browser.find_elements_by_xpath('//a[@class="title"]')
# titulos = [i.text for i in titulosSelenium]

# fechasSelenium = browser.find_elements_by_xpath('//span[@class="date"]')
# fechasArticulos = [i.text for i in fechasSelenium]

# Cada articulo por pagina
# articulos = browser.find_elements_by_xpath('//article[@class="js-article-item articleItem   "]')

# for articulo in articulos:
#     contienePro = len(articulo.find_elements_by_xpath('.//img[@class="pro-title-list-icon"]'))

#     if not contienePro:
#         a = articulo.find_element_by_xpath('//a[@class="title"]') #get_attribute('href')
#         # linkArticulo.click()
#         browser.implicitly_wait(10)
#         ActionChains(browser).move_to_element(a).click(a).perform()

diccionario = {"Nemotecnico": nemotecnico,
               "Fecha" : fecha,
               "Precio final": precioFinal,
               "Variacion en USD": delta,
               "Variacion Porcentual": deltaPorcentual,
               "Volumen (M)": volumen}

# //a[@class="title"]


"""
===============================
app.py: RESTful API for GeMS
===============================
__contact__ = 'swk30.cam@gmail.com'
__date__ = '10.10.2018'
__status__ = 'MVP Complete'
"""

# Dependencies

import os
import sys

from flask import Flask, jsonify, request, make_response
from flask_restful import Api, Resource

from app_utils import similarity_jaccard, similarity_overlap
from upload import api_insert
from db_config import COLLECTION_NAME
from db_utils import db, INDEX_LIST

# Code

app = Flask(__name__)
api = Api(app, catch_all_404s=True)

class Genesets(Resource):
	def __init__(self):
		self.returnParamNames = 'returnParams'
		self.genesParam = 'genes'

	def getOutput(self, query, returnParams, geneList):
		"""
		:param query: Dict
		:param returnParams: List<String> or None
		:param geneList: List<String> or None
		:return: List
		"""
		if geneList is not None:
			geneMatchList = [{'genes': {'$elemMatch': {'$elemMatch': {'$elemMatch': {'$in': [gene]}}}}} for gene in geneList]
			otherQueryList = [{k: v} for k, v in query.items()]
			geneQuery = {'$and': geneMatchList + otherQueryList}
			query = geneQuery

		if 'setName' in query:
			queryString = query['setName']
			queryList = queryString.split(",")
			queryJSON = {'$in':queryList}
			query['setName'] = queryJSON

		output = []
		q = db[COLLECTION_NAME].find(query)
		for p in q:
			if returnParams == None:
				docReturn = {k: v for k, v in p.items() if k != '_id'}
			else:
				docReturn = dict()
				for param in returnParams:
					if param in p:
						docReturn[param] = p[param]
					else:
						docReturn[param] = ""
			output.append(docReturn)
		return output

	def get(self):
		parsedArgs = request.args
		data = {k: v for k, v in parsedArgs.items()}
		hasGenes = False
		params = data.pop(self.returnParamNames, None)
		genes = data.pop(self.genesParam, None)
		if params is not None:
			params = params.split(',')
		if genes is not None:
			genes = genes.split(',')
			hasGenes = True

		# Determine output format (JSON or GMT file)
		getGmt = data.pop('getGmt', None)
		if getGmt in ['True', 'true']:
			getGmt = True
		else:
			getGmt = False

		# Scrape data and present in established format
		if len(data) == 0 and hasGenes is False:
			# Error logic
			output = 404
			return jsonify({"response": output})
		elif getGmt:
			gmtParams = ['setName', 'desc', 'genes']
			output = self.getOutput(data, gmtParams, genes)

			# outputList: list of tab-delimited String (setName-desc-genes)
			outputList = []
			for gs in output:
				name = [gs['setName']]
				desc = [gs['desc']]
				genes = [gene[0][2] for gene in gs['genes'] if gene[0][2] != '']
				combined = name + desc + genes
				combinedStr = '\t'.join(combined)
				outputList.append(combinedStr)

			# Output: GMT file
			tsv = '\n'.join(outputList)
			response = make_response(tsv)
			response.headers['content-type'] = 'application/octet-stream'
			return response
		else:
			# Output: JSON
			output = self.getOutput(data, params, genes)
			return jsonify({"response": output})

	def post(self):
		data = request.get_json()
		hasGenes = False
		params = data.pop(self.returnParamNames, None)
		genes = data.pop(self.genesParam, None)
		if genes is not None:
			hasGenes = True

		if len(data) == 0 and hasGenes is False:
			output = 404
		else:
			output = self.getOutput(data, params, genes)
		return jsonify({"response": output})

class Similar(Resource):
	def __init__(self):
		self.requiredParams = {'setName', 'source', 'subtype', 'user', 'method', 'threshold'}
		self.acceptedMethods = {'jaccard', 'overlap'}
		self.getGeneSet = lambda l : {gene[0][2] for gene in l if gene[0][2] != ''}

	def getGeneMembers(self, inputDict):
		"""
		:param inputDict: Dict
		:return: Set - Empty if invalid
		"""
		testParams = {x for x in inputDict.keys()}
		findGeneset = db[COLLECTION_NAME].find_one({
			'setName': inputDict['setName'],
			'source': inputDict['source'],
			'subtype': inputDict['subtype'],
			'user': inputDict['user']
		})
		if testParams != self.requiredParams:
			return set()
		elif not inputDict['threshold'].replace('.', '').isdigit():
			return set()
		elif inputDict['method'] not in self.acceptedMethods:
			return set()
		elif findGeneset is None:
			return set()
		else:
			returnGenes = self.getGeneSet(findGeneset['genes'])
			return returnGenes

	def get(self):
		parsedArgs = request.args
		input = {k: v for k, v in parsedArgs.items()}

		genes = self.getGeneMembers(input)
		if len(genes) == 0:
			output = 404
		else:
			output = []
			method = input['method']
			threshold = float(input['threshold'])

			# Optimisation: ignore geneset with which the intersection will just be 0
			allGenesets = db[COLLECTION_NAME].find({'genes': {'$elemMatch': {'$elemMatch': {'$elemMatch': {'$in': list(genes)}}}}})
			for geneset in allGenesets:
				compareGenes = self.getGeneSet(geneset['genes'])
				if method == 'jaccard':
					sim = similarity_jaccard(compareGenes, genes)
				elif method == 'overlap':
					sim = similarity_overlap(compareGenes, genes)

				if sim >= threshold:
					output.append({'setName': geneset['setName'],
									'source': geneset['source'],
									'coeff': sim
					})

		return jsonify({"response": output})

class addGenesets(Resource):
	def post(self):
		data = request.get_json()

		if set(data.keys()) != {'headers', 'parsed', 'params'}:
			output = 404
		else:
			try:
				output = [200] + api_insert(data['headers'], data['parsed'], data['params'])
			except AssertionError:
				output = 404
			except Exception:
				output = 404

		return jsonify({"response": output})

class delGenesets(Resource):
	def post(self):
		data = request.get_json()

		try:
			assert 'genesets' in data
			removeList = data['genesets']
			for geneset in removeList:
				assert set(geneset.keys()) == set(INDEX_LIST)
				assert geneset['user'] != 'Public'
				db[COLLECTION_NAME].remove(geneset)
			output = 200
		except AssertionError:
			output = 404
		except Exception:
			output = 404

		return jsonify({"response": output})

api.add_resource(Genesets, "/api/genesets")
api.add_resource(Similar, "/api/similar")
api.add_resource(addGenesets, "/api/insert")
api.add_resource(delGenesets, "/api/remove")

if __name__ == "__main__":
	app.run(host='0.0.0.0', port=1234, debug=True)


import json
import boto3

def lambda_handler(event, context):
    print(event)
    access_key = "*****************"
    secret_key = "****************************"
    session=boto3.Session(aws_access_key_id=access_key,aws_secret_access_key=secret_key, region_name='us-east-1')
    dynamo=session.resource('dynamodb')
    table = dynamo.Table('Todo-k7i42s4bp5a4xj4bokrdjuvcke-dev')
    response = table.get_item(
    Key={
        'id': event['userId'].strip()
    })
    print(response)
    l = [i.capitalize() for i in set(response['Item']["ingredients"])]
    return {
        'statusCode': 200,
        'headers': {
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Headers' : 'Content-Type',
            'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'

        },
        "text":l
    }


import pandas as pd
import math

elec = pd.read_csv('data/DLE-elec-2019-CCPM-geocoded.csv', sep = ';')
gaz = pd.read_csv('data/DLE-gaz-2019-CCPM-geocoded.csv', sep = ';')
dpe = pd.read_csv('data/DPE_CCPM.csv', sep = ';')

def convert_e2f(string):
    return float(string[0]+string[2:8]) * 10**(int(string[9:]) - 6)

limite_classe_energie = {50 : 'A', 90 : 'B', 150 : 'C', 230 : 'D', 330 : 'E', 450 : 'F', math.inf : 'G'}
limite_classe_ges = {5 : 'A', 10 : 'B', 20 : 'C', 35 : 'D', 55 : 'E', 80 : 'F', math.inf : 'G'}
type_encadrement = {'consommation_energie' : limite_classe_energie, 'estimation_ges' : limite_classe_ges}
def encadrement(val, inter):
    for key in inter:
        classe = inter[key]
        if key > val:
            break
    return classe

elec['CONSO'] = elec['CONSO'].apply(convert_e2f)
gaz['CONSO'] = gaz['CONSO'].apply(convert_e2f)
# Aggregation sur l'id d'etalab

# toBfirsted = {elec : ['FILIERE', 'CODE_GRAND_SECTEUR']}
# toBmeaned = {elec : }
# toBsumed

# bases_useful = []
# for base in [elec, gaz]:
#     groupe = base.groupby('id')
#     firsted = groupe[['FILIERE', 'CODE_GRAND_SECTEUR', 'ADRESSE', 'NOM_COMMUNE']].nth(0)
#     meaned = groupe[['latitude', 'longitude']].mean()
#     sumed = groupe[['PDL', 'CONSO']].sum()
#     grouped = firsted.merge(meaned, on = 'id')
#     base_useful = grouped.merge(sumed, on = 'id')
#     bases_useful.append(base_useful)
#     print(base_useful)

# elec, gaz = bases_useful

### On met en forme gaz et elec, et on les fusionne sur leurs adresses
elec.rename(columns = {'CONSO': 'CONSO_elec', 'PDL' : 'PDL_elec', 'id': 'id_geo'}, inplace = True)
gaz.rename(columns = {'CONSO': 'CONSO_gaz', 'PDL' : 'PDL_gaz', 'id': 'id_geo'}, inplace = True)
keys = ('FILIERE', 'CODE_GRAND_SECTEUR', 'ADRESSE', 'NOM_COMMUNE', 'latitude', 'longitude', 'id_geo', 'score')
joined = elec.merge(gaz, on = keys, how = 'outer')

### On introduit la consommation brute du logement, qui nous servira par la suite pour faire des groupements
for elem in {'consommation_energie', 'estimation_ges'}:
    dpe[elem + '_brut'] = (dpe[elem] * dpe['surface_habitable'])

### On traite les DPE
# print(dpe[['consommation_energie', 'consommation_energie_brut', 'surface_habitable', 'estimation_ges', 'estimation_ges_brut']])
dpe.rename(columns = {elem : elem + '_dpe' for elem in ['commune', 'type_voie', 'numero_rue', 'nom_rue', 'id']} , inplace = True)
dpe.rename(columns = {'result_id': 'id_geo', 'result_score' : 'score'} , inplace = True)

### On joint tout
all_join = dpe.merge(joined, on = ('latitude', 'longitude', 'score', 'id_geo'), how = 'outer')

### On va distinguer les colonnes intéressantes en fonction de l'opération d'agrégation, et on les joint de nouveau
grouped = all_join.groupby('id_geo')
sumed = grouped[['consommation_energie_brut', 'estimation_ges_brut', 'surface_habitable', 'PDL_elec', 'CONSO_elec', 'PDL_gaz', 'CONSO_gaz']].sum()
meaned = grouped[['latitude', 'longitude', 'score']].mean()
firsted = grouped[['secteur_activite', 'result_label',
                    'result_type', 'result_housenumber', 'result_name', 'NOM_COMMUNE', 'ADRESSE',
                    'FILIERE', 'CODE_GRAND_SECTEUR']].first()
merge1 = firsted.merge(meaned, on = 'id_geo')
dpe_dle = merge1.merge(sumed, on = 'id_geo')

### On rajoute les classes de consommation et d'émission
for elem in {'consommation_energie', 'estimation_ges'}:
    dens = (dpe_dle[elem + '_brut'] / dpe_dle['surface_habitable'])
    classes = dens.apply(encadrement, args = (type_encadrement[elem],))
    dpe_dle['classe_' + elem] = classes

dpe_dle.to_csv('data/dpe_dle-pertinent.csv', ';')


from itertools import permutations

def calc(priority, n, expression):
    if n == 2:
        return str(eval(expression))
    elif priority[n] == "*":
        return str(eval('*'.join([calc(priority, n+1, e) for e in expression.split("*")])))
    elif priority[n] == "+":
        return str(eval('+'.join([calc(priority, n + 1, e) for e in expression.split("+")])))
    elif priority[n] == "-":
        return str(eval('-'.join([calc(priority, n+1, e) for e in expression.split("-")])))

def solution(expression):
    answer = 0
    priority = list(permutations(['+','-','*'],3))
    for p in priority:
        answer = max(answer, abs(int(calc(p, 0, expression))))

    return answer

프로그래머스/수식 최대화.py


# importando bibliotecas
import pandas as pd
import numpy as np
from scipy.sparse import csr_matrix
from sklearn.neighbors import NearestNeighbors

# Obtendo os dados da biblioteca

movies_df=pd.read_csv('movies.csv', usecols=['movieId', 'title'], dtype={'movieId': 'int32', 'title': 'str'})
rating_df=pd.read_csv('ratings.csv', usecols=['userId', 'movieId', 'rating'], dtype={'userId': 'int32', 'movieId': 'int32', 'rating': 'float32'})

print('\nExibindo dados dos filmes\n', movies_df.head())
print('\nExibindo dados das classificações\n', movies_df.head())

df=pd.merge(rating_df, movies_df, on="movieId")
print(df.head())

combine_movie_rating = df.dropna(axis=0, subset=['title'])
movie_ratingCount = (combine_movie_rating.groupby(by=['title'])['rating'].count().reset_index().rename(columns={'rating': 'totalRatingCount'})[['title', 'totalRatingCount']])
print('\n', movie_ratingCount.head())

rating_with_totalRatingCount=combine_movie_rating.merge(movie_ratingCount, left_on='title', right_on='title', how='left')
print('\n', rating_with_totalRatingCount.head())

pd.set_option('display.float_format', lambda x: '%.3f' % x)
print(movie_ratingCount['totalRatingCount'].describe())

populaty_threshold=50
rating_popular_movie=rating_with_totalRatingCount.query('totalRatingCount >= @populaty_threshold')
rating_popular_movie.head()

rating_popular_movie.shape

movie_features_df=rating_popular_movie.pivot_table(index='title', columns='userId', values='rating').fillna(0)
print('\n', movie_features_df.head())

movie_features_df_matrix=csr_matrix(movie_features_df.values)
model_knn=NearestNeighbors(metric='cosine', algorithm='brute')
model_knn.fit(movie_features_df_matrix)

print(movie_features_df.shape)

query_index=np.random.choice(movie_features_df.shape[0])
print(query_index)

distances, indices = model_knn.kneighbors(movie_features_df.iloc[query_index,:].values.reshape(1, -1), n_neighbors=6)

print('\n', movie_features_df.head())

for i in range(0, len(distances.flatten())):
  if i == 0:
    print('Recomendação para {0}: \n'.format(movie_features_df.index[query_index]))

  else:
    print('{0}:{1}, de distância de {2}'.format(i, movie_features_df.index[indices.flatten()[i]], distances.flatten()[i]))


""" print out binary place value patterns """

def placevalue_patterner(function, height, width, placevalue, offset_y=0):
    """create a visualization of a place value pattern"""
    visual = []
    for i in range(offset_y, offset_y + height):
        row = ""
        for j in range(width):
            value = function(i, j)
            binary = "{0:b}".format(int(value))
            if len(binary) > placevalue and binary[-1 * placevalue] == "1":
                row += "*"
            else:
                row += " "
        visual.append(row)
    return "\n".join(visual)

# example
if __name__ == "__main__":
    func = lambda x, y: x**2 * y**2
    print(placevalue_patterner(func, 128, 128, 7))


starting_gold = 5
starting_level = 1
tier1_summon_quantity = 45
tier2_summon_quantity = 30
tier3_summon_quantity = 25
tier4_summon_quantity = 20
tier5_summon_quantity = 10
draw_percentages = {
    1:{1:100, 2:0, 3:0, 4:0, 5:0},
    2:{1:70, 2:30, 3:0, 4:0, 5:0},
    3:{1:60, 2:35, 3:5, 4:0, 5:0},
    4:{1:50, 2:35, 3:15, 4:0, 5:0},
    5:{1:40, 2:35, 3:25, 4:0, 5:0},
    6:{1:30, 2:30, 3:30, 4:10, 5:0},
    7:{1:25, 2:30, 3:30, 4:15, 5:0},
    8:{1:22, 2:27, 3:30, 4:20, 5:1},
    9:{1:20, 2:23, 3:29, 4:25, 5:3},
    10:{1:15, 2:21, 3:28, 4:30, 5:6},
}
tier1_summon_names = ['Bomb', 'Flan', 'Cockatrice', 'Funguar', 'Goblin', 'Lamia', 'Basilisk', 'Ahirman', 'Coeurl']
tier2_summon_names = ['Chocobo', 'Tonberry', 'Cactuar', 'Behemoth', 'Iron Giant', 'Marlboro', 'Ochu', 'Adamantoise']
tier3_summon_names = ['Ramuh', 'Shiva', 'Ifrit', 'Carbuncle', 'Siren', 'Valefor', 'Titan']
tier4_summon_names = ['Leviathan', 'Pheonix', 'Diablos', 'Odin', 'Alexander', 'Doomtrain']
tier5_summon_names = ['Bahamut', 'Eden', 'Anima', 'Knights of the Round', 'Zodiark']


# Standard Libs
import decouple
import pytest
from unittest.mock import patch

# INTERNAL LIBS
from persephone_client import Persephone

from src.domain.exceptions.service.auditing_trail.model import FailToSaveAuditingTrail
from src.domain.exceptions.service.bank_account.model import BankAccountNotExists
from tests.src.stubs.bank_account_stubs.stub_get_account import (
    jwt_with_bank_account_to_update,
)
from src.services.bank_account.service import UserBankAccountService
from src.repositories.bank_account.repository import UserBankAccountRepository
from tests.src.stubs.project_stubs.stub_data import payload_data_dummy

@pytest.mark.asyncio
@patch.object(
    UserBankAccountRepository, "user_bank_account_id_exists", return_value=True
)
@patch.object(
    UserBankAccountRepository, "update_registered_user_bank_accounts", return_value=True
)
@patch.object(UserBankAccountService, "bank_code_from_client_exists", return_value=True)
@patch.object(Persephone, "send_to_persephone", return_value=[True, True])
@patch.object(decouple.Config, "get", return_value="lala")
async def test_update_user_when_sending_the_right_params_then_return_the_duly_updated_message(
    mock_get,
    mock_send_to_persephone,
    mock_bank_code_from_client_exists,
    mock_update_registered_user_bank_accounts,
    mock_user_bank_account_id_exists,
):
    response = await UserBankAccountService.update_user_bank_account(
        jwt_data=jwt_with_bank_account_to_update,
        bank_account_repository=UserBankAccountRepository,
    )
    updating_message = {"message": "Updated"}
    assert response == updating_message
    assert isinstance(response, dict)
    assert response.get("message") == "Updated"

@pytest.mark.asyncio
async def test_update_user_when_sending_an_invalid_jwt_data_then_return_the_expected_exception():
    with pytest.raises(KeyError):
        await UserBankAccountService.update_user_bank_account(
            jwt_data=payload_data_dummy,
            bank_account_repository=UserBankAccountRepository,
        )

@pytest.mark.asyncio
async def test_update_user_when_sending_an_invalid_bank_repository_call_then_return_the_expected_exception():
    with pytest.raises(AttributeError):
        await UserBankAccountService.update_user_bank_account(
            jwt_data=jwt_with_bank_account_to_update, bank_account_repository=""
        )

@pytest.mark.asyncio
@patch.object(
    UserBankAccountRepository, "user_bank_account_id_exists", return_value=False
)
@patch.object(
    UserBankAccountRepository,
    "update_registered_user_bank_accounts",
    return_value=False,
)
@patch.object(Persephone, "send_to_persephone", return_value=[True, False])
async def test_when_bank_account_and_register_account_are_false_then_raise_the_expected_bad_request(
    mock_get_registered_user_bank_accounts,
    mock_update_registered_user_bank_accounts,
    mock_send_to_persephone,
):
    with pytest.raises(BankAccountNotExists):
        await UserBankAccountService.update_user_bank_account(
            jwt_data=jwt_with_bank_account_to_update,
            bank_account_repository=UserBankAccountRepository,
        )

@pytest.mark.asyncio
@patch.object(
    UserBankAccountRepository, "user_bank_account_id_exists", return_value=True
)
@patch.object(
    UserBankAccountRepository,
    "update_registered_user_bank_accounts",
    return_value=False,
)
@patch.object(Persephone, "send_to_persephone", return_value=[False, False])
@patch.object(decouple.Config, "get", return_value="lala")
async def test_when_register_account_is_false_then_raise_the_expected_fail_to_save_auditing_trail(
    mock_send_to_persephone,
    mock_update_registered_user_bank_accounts,
    mock_user_bank_account_id_exists,
    mock_get,
):
    with pytest.raises(FailToSaveAuditingTrail):
        await UserBankAccountService.update_user_bank_account(
            jwt_data=jwt_with_bank_account_to_update,
            bank_account_repository=UserBankAccountRepository,
        )

@pytest.mark.asyncio
async def test_when_sending_wrong_params_of_repository_then_raise_key_error_as_expected():
    with pytest.raises(AttributeError):
        await UserBankAccountService.update_user_bank_account(
            bank_account_repository=None, jwt_data=jwt_with_bank_account_to_update
        )

@pytest.mark.asyncio
async def test_when_jwt_data_payload_is_invalid_then_raise_type_error_as_expected():
    with pytest.raises(TypeError):
        await UserBankAccountService.update_user_bank_account(
            jwt_data=None, bank_account_repository=UserBankAccountRepository
        )


import turtle
import pandas as pd

from state_position import StatePosition

# screen set up
screen = turtle.Screen()
screen.title('U.S States Game')

image = 'images/blank_states_img.gif'
turtle.addshape(image)

turtle.shape(image)

data = pd.read_csv('data/50_states.csv')

list_states_names = data.state.to_list()
correct_user_answers = []

while len(correct_user_answers) <= len(list_states_names):
    user_guess = (screen.textinput(f'{len(correct_user_answers)}/{len(list_states_names)} '
                                   f'States correct', 'What\'s another state name?')).title()
    if user_guess=='Exit':
        states_to_learn = [state for state in list_states_names if state not in correct_user_answers]
        df = pd.DataFrame(states_to_learn, columns=['States to learn'])
        df.to_csv('data/states_to_learn.csv')
        break

    if user_guess in list_states_names and user_guess not in correct_user_answers:
        x = int(data.x[data.state==user_guess])
        y = int(data.y[data.state==user_guess])
        StatePosition(x, y, user_guess)
        correct_user_answers.append(user_guess)

screen.mainloop()


from IPython import get_ipython
from prompt_toolkit.enums import DEFAULT_BUFFER
from prompt_toolkit.filters import (HasFocus, HasSelection, ViInsertMode)
from prompt_toolkit.key_binding.vi_state import InputMode

ip = get_ipython()

def switch_mode(event):
    " Map 'kj' to Escape. "
    vi_state = event.cli.vi_state
    vi_state.input_mode = InputMode.NAVIGATION

# Register the shortcut if IPython is using prompt_toolkit
if hasattr(ip, 'pt_app') and ip.pt_app:
    insert_mode = ViInsertMode()

    registry = ip.pt_app.key_bindings
    registry.add_binding(
        'k', 'j',
        filter=(HasFocus(DEFAULT_BUFFER)
                & ~HasSelection()
                & insert_mode))(switch_mode)

    # Add back standard readline-like mappings, even when using ViInsertMode
    from prompt_toolkit.key_binding.bindings.named_commands import get_by_name
    handle = registry.add
    handle('c-a')(get_by_name('beginning-of-line'))
    handle('c-b')(get_by_name('backward-char'))
    handle('c-e')(get_by_name('end-of-line'))
    handle('c-f')(get_by_name('forward-char'))
    handle('c-left')(get_by_name('backward-word'))
    handle('c-right')(get_by_name('forward-word'))
    handle('escape', 'b')(get_by_name('backward-word'))
    handle('escape', 'c', filter=insert_mode)(get_by_name('capitalize-word'))
    handle('escape', 'd', filter=insert_mode)(get_by_name('kill-word'))
    handle('escape', 'f')(get_by_name('forward-word'))
    handle('escape', 'l', filter=insert_mode)(get_by_name('downcase-word'))
    handle('escape', 'u', filter=insert_mode)(get_by_name('uppercase-word'))
    handle('escape', 'y', filter=insert_mode)(get_by_name('yank-pop'))
    handle('escape', 'backspace', filter=insert_mode)(get_by_name('backward-kill-word'))
    handle('escape', '\\', filter=insert_mode)(get_by_name('delete-horizontal-space'))


import numpy as np
import pdb
import math
import scipy.special
from . import ModelObjs, Utils

"""
This module contains definitions for Stochs with standard probability
distributions that are used regularly when defining models, such as Gaussian,
Uniform, and Gamma. Additional Stoch definitions can be trivially added.
"""

def Gaussian( name, mu=0.0, sigma=1.0, value=None, observed=False, dtype=float ):
    """
    Gaussian random variable.

    CALLING

      y = pyhm.Gaussian( 'y', parents={ 'mu':0, 'sig':1 }, value=3.4, observed=False, dtype=float )

    The above would generate an unobserved Stoch that has a Gaussian probability
    distribution with a mean of 0, a standard deviation of 1, and a current value of 3.4.
    """
    parents = { 'mu':mu, 'sigma':sigma }
    parent_values = Utils.extract_stochastics_values( parents )

    def logp( value, parent_values ):
        mu_value = parent_values['mu']
        sigma_value = parent_values['sigma']
        if np.ndim( value )==0:
            logp = -0.5*math.log( 2*np.pi*( sigma_value**2. ) ) \
                   - ( ( value - mu_value )**2. )/( 2*( sigma_value**2. ) )
        else:
            logp = np.sum( -0.5*np.log( 2*np.pi*( sigma_value**2. ) ) \
                           - ( ( value - mu_value )**2. )/( 2*( sigma_value**2. ) ) )
        return logp

    def random( parent_values ):
        mu = parent_values['mu']
        sigma = parent_values['sigma']
        return np.random.normal( mu, sigma )

    if value is None:
        value = random( parent_values )

    dictionary = { 'name':name, 'observed':observed, 'dtype':dtype, 'parents':parents, \
                   'value':value, 'logp':logp, 'random':random }

    return ModelObjs.Stoch( dictionary )

def Uniform( name, lower=0.0, upper=1.0, value=None, observed=False, dtype=float ):
    """
    Uniform random variable.

    CALLING

      y = pyhm.Uniform( 'y', lower=0, upper=1, value=0.4, observed=False, dtype=float )

    The above would generate an unobserved Stoch that has a Uniform probability
    distribution between 0 and 1, with a current value of 0.4.
    """

    parents = { 'lower':lower, 'upper':upper }
    parent_values = Utils.extract_stochastics_values( parents )

    if ( value is not None )*( np.ndim( value )>0 ):
        n = len( value.flatten() )
    else:
        n = 1

    def logp( value, parent_values ):
        lower_value = parent_values['lower']
        upper_value = parent_values['upper']
        if np.any( value>=lower_value )*np.any( value<=upper_value ):
            logp = 0#1.0
        else:
            logp = -np.inf

        return logp

    def random( parent_values ):
        lower = parent_values['lower']
        upper = parent_values['upper']
        if n>1:
            return np.random.uniform( low=lower, high=upper, size=n )
        else:
            return np.random.uniform( low=lower, high=upper )

    if value is None:
        value = random( parent_values )

    dictionary = { 'name':name, 'observed':observed, 'dtype':dtype, 'parents':parents, \
                   'value':value, 'logp':logp, 'random':random }

    return ModelObjs.Stoch( dictionary )

def Gamma( name, alpha=1, beta=1, value=None, observed=False, dtype=float ):
    """
    Gamma random variable.

    CALLING

      y = pyhm.Gamma( 'y', alpha=1, beta=10, value=3.4, observed=False, dtype=float )

    The above would generate an unobserved Stoch that has a Gamma probability
    distribution with a shape parameter of 1 and a scale parameter of 10. More explicitly,
    the probability distribution is defined as:

      log_pdf = sum( -log_gamma( alpha ) + alpha*np.log( beta ) \
                     + (alpha-1)*log( value ) - beta*value )
    """

    parents = { 'alpha':alpha, 'beta':beta }
    parent_values = Utils.extract_stochastics_values( parents )

    def logp( value, parent_values ):
        alpha_value = parent_values['alpha']
        beta_value = parent_values['beta']

        if np.any( value<=0 ):
            logp_value = -np.inf
        else:

            # The Python math routine is faster than numpy for single-valued inputs:
            if np.ndim( value )==0:
                logp_value = -math.lgamma( alpha_value ) + alpha_value*math.log( beta_value ) \
                             + (alpha_value-1)*math.log( value ) - beta_value*value

            # Numpy is much faster for arrays of inputs:
            else:
                logp_value = np.sum( -math.lgamma( alpha_value ) + alpha_value*np.log( beta_value ) \
                                     + (alpha_value-1)*np.log( value ) - beta_value*value )

        return float( logp_value )

    def random( parent_values ):
        alpha = parent_values['alpha']
        beta = parent_values['beta']
        return np.random.gamma( shape=alpha, scale=1./beta )

    if ( parent_values['alpha']<=0 )+( parent_values['beta']<=0 ):
        err_str = 'alpha and beta parameters must both be >0'
        raise ValueError( err_str )

    if value is None:
        value = random( parent_values )
    dictionary = { 'name':name, 'observed':observed, 'dtype':dtype, 'parents':parents, \
                   'value':value, 'logp':logp, 'random':random }

    return ModelObjs.Stoch( dictionary )