# -*- coding: utf-8 -*-
"""Defines the Hydro class performing hydrodynamic updates of the state.
"""

from __future__ import print_function

import numpy as np
from pyrodeo.state import State
from pyrodeo.linear_advection import LinearAdvection
from pyrodeo.roe import Roe

class Hydro:
    """Construct class for hydrodynamic updates.

    Construct from existing instances of :class:`.Param` and :class:`.Coordinates`. It constructs a :class:`.LinearAdvection` instance which will deal with orbital advection, and a :class:`.Roe` instance dealing with hydrodynamics of residual velocities.

    Args:
        param (:class:`.Param`): Valid Param object, containing simulation parameters.
        coords (:class:`.Coordinates`): Valid Coordinates object, containing x, y and z coordinates. Used to calculate orbital advection velocity.

    The following attributes and methods are available:

    Attributes:
        orbital_advection (:class:`.LinearAdvection`): Instance of :class:`.LinearAdvection` class used to do orbital advection.
        roe (:class:`.Roe`): Instance of :class:`.Roe` class dealing with hydrodynamics of residual velocities.

    """
    def __init__(self, param, coords):
        v_adv = 0.0*coords.x
        if param.geometry == 'sheet':
            v_adv = (-1.5*coords.x).transpose((1,0,2))
        if param.geometry == 'cyl':
            v_adv = (np.power(coords.x, -1.5) -
                     param.frame_rotation).transpose((1,0,2))
        if param.geometry == 'sph':
            v_adv = (np.power(coords.x, -1.5)/np.sin(coords.z) -
                     param.frame_rotation).transpose((1,0,2))
        self.orbital_advection = LinearAdvection(v_adv, param.limiter_param)
        self.roe = Roe(param.limiter_param, param.min_dens)

    def calc_time_step(self, geometry, coords, state, log_radial=False):
        """Calculate time step obeying the CFL condition.

        Args:
            geometry (str): 'cart', 'sheet' or 'cyl'.
            coords (:class:`.Coordinates`): Valid :class:`.Coordinates` object, containing x, y and z coordinates.
            state (:class:`.State`): Valid :class:`.State` object, containing density and velocity.
            log_radial (:obj:`bool`, optional): Flag indicating whether a logarithmic radial coordinate is used

        Returns:
            float: Maximum time step obeying the CFL condition.

        """
        cs = state.soundspeed

        dtx = 1.0e10
        dty = dtx
        dtz = dtx
        if len(state.dens[:,0,0]) > 1:
            abs_speed = (np.abs(state.velx) + cs)*state.no_ghost
            if log_radial is True:
                abs_speed /= coords.x
            dtx = coords.dxyz[0]/np.max(abs_speed)

        if len(state.dens[0,:,0]) > 1:
            abs_speed = (np.abs(state.vely) + cs)*state.no_ghost
            if geometry == 'cyl':
                abs_speed /= coords.x
            if geometry == 'sph':
                abs_speed /= (coords.x*np.sin(coords.z))
            dty = coords.dxyz[1]/np.max(abs_speed)

        if len(state.dens[0,0,:]) > 1:
            abs_speed = (np.abs(state.velz) + cs)*state.no_ghost
            if geometry == 'sph':
                abs_speed /= coords.x
            dtz = coords.dxyz[2]/np.max(abs_speed)

        return np.min([dtx, dty, dtz])

    def shear_periodic_boundaries(self, t, coords, state):
        """Set shear-periodic boundary conditions.

        In the shearing sheet geometry, the x direction can be quasi-periodic, i.e. periodic but modified for the shear. Imagine neighbouring sheets shearing past the center sheet.

        Args:
            t (float): Current simulation time. Used to calculate over what distance neighbouring sheets have shifted since t = 0.
            coords (:class:`.Coordinates`): Valid :class:`.Coordinates` object, containing x and y coordinates.
            state (:class:`.State`): Valid :class:`.State` object, containing density and velocity.
        """
        # Single cell in x; nothing to do
        if len(state.dens[:,0,0]) <= 1:
            return

        # Create State made just of ghost zones
        dens = np.concatenate((state.dens[-4:-2,:,:], state.dens[2:4,:,:]))
        velx = np.concatenate((state.velx[-4:-2,:,:], state.velx[2:4,:,:]))
        vely = np.concatenate((state.vely[-4:-2,:,:], state.vely[2:4,:,:]))
        velz = np.concatenate((state.velz[-4:-2,:,:], state.velz[2:4,:,:]))
        temp_state = State(dens, velx, vely, velz, velz)

        # Advection velocity is speed of next box
        v_adv = 0.0*dens + 1.5*(coords.x[-2:-1,0,0] + coords.x[-3:-2,0,0])
        v_adv[2:4,:] = -v_adv[2:4,:,:]

        # Linear advection since t = 0 when solution was periodic
        temp_state.transpose((1,0,2))
        v_adv = v_adv.transpose((1,0,2))

        la = LinearAdvection(v_adv, self.orbital_advection.sb)
        la.step(t, coords.dxyz[1], temp_state)

        temp_state.transpose((1,0,2))

        # Store in ghost zones
        state.dens[:2,:,:]  = temp_state.dens[:2,:,:]
        state.velx[:2,:,:]  = temp_state.velx[:2,:,:]
        state.vely[:2,:,:]  = temp_state.vely[:2,:,:]
        state.velz[:2,:,:]  = temp_state.velz[:2,:,:]

        state.dens[-2:,:,:] = temp_state.dens[2:,:,:]
        state.velx[-2:,:,:] = temp_state.velx[2:,:,:]
        state.vely[-2:,:,:] = temp_state.vely[2:,:,:]
        state.velz[-2:,:,:] = temp_state.velz[2:,:,:]

    def preprocess(self, coords, param, state, direction):
        """Modify state to quasi-cartesian form and calculate geometric source terms.

        Isothermal hydrodynamics allows for a generic form of the equations in all geometries, subject only to modifications in the geometric source terms.

        Args:
            coords (:class:`.Coordinates`): Valid :class:`.Coordinates` object, containing x and y coordinates.
            param (:class:`.Param`): Valid :class:`.Param` object, containing simulation parameters.
            state (:class:`.State`): Valid :class:`.State` object, containing density and velocity.
            direction (int): 0 (integrating x) or 1 (integrating y).

        Returns:
            ndarray: Geometric source term of the same shape as state.dens.

        """
        source = 0.0*state.dens

        if param.geometry == 'sheet':
            if direction == 0:
                state.vely += 0.5*param.frame_rotation*coords.x
                source = 2.0*state.dens*param.frame_rotation*\
                  (state.vely - 0.5*param.frame_rotation*coords.x)
            if direction == 2:
                source = -state.dens*param.frame_rotation*\
                  param.frame_rotation*coords.z

        if param.geometry == 'cyl':
            if direction == 0:
                state.dens *= coords.x
                state.vely = coords.x*state.vely + np.sqrt(coords.x)
                dpot = coords.x*np.power(coords.x*coords.x +
                                         coords.z*coords.z, -1.5)
                source = \
                    state.dens*state.vely*state.vely/ \
                    (coords.x*coords.x*coords.x) - \
                    state.dens*dpot + \
                state.soundspeed*state.soundspeed*state.dens/coords.x
                if param.log_radial is True:
                    state.velx = state.velx/coords.x
                    state.dens *= coords.x
                    state.soundspeed /= coords.x
                    source = source - state.dens*state.velx*state.velx - \
                      state.soundspeed*state.soundspeed*state.dens
            if direction == 1:
                state.vely /= coords.x
                state.soundspeed /= coords.x
            if direction == 2:
                dpot = coords.z*np.power(coords.x*coords.x +
                                         coords.z*coords.z, -1.5)
                source = -state.dens*dpot

        if param.geometry == 'sph':
            if direction == 0:
                c = state.soundspeed

                state.dens *= coords.x*coords.x
                state.vely = state.vely*coords.x + np.sqrt(coords.x)
                state.velz *= coords.x
                source = state.dens*(((state.vely*state.vely +
                                       state.velz*state.velz)/coords.x -
                                       1.0)/(coords.x*coords.x) +
                                       2.0*c*c/coords.x)
                if param.log_radial is True:
                    state.velx = state.velx/coords.x
                    state.dens *= coords.x
                    state.soundspeed /= coords.x
                    source = source - state.dens*state.velx*state.velx - \
                      state.soundspeed*state.soundspeed*state.dens
            if direction == 1:
                g = 1.0/(coords.x*np.sin(coords.z))
                state.vely *= g
                state.soundspeed *= g

            if direction == 2:
                sint = np.sin(coords.z)

                state.dens *= sint
                state.vely = sint*(state.vely/coords.x +
                                   np.power(coords.x, -1.5))
                state.velz /= coords.x
                state.soundspeed /= coords.x

                cott = np.cos(coords.z)/sint
                source = state.dens*cott*(state.vely*state.vely/(sint*sint) +
                                          state.soundspeed*state.soundspeed)

        if direction == 1:
            source = source.transpose((1,0,2))
            state.transpose((1,0,2))
            state.swap_velocities(1)

        if direction == 2:
            source = source.transpose((2,1,0))
            state.transpose((2,1,0))
            state.swap_velocities(2)

        return source

    def postprocess(self, coords, param, state, direction):
        """Inverse of :meth:`.preprocess`.

        Reverse modifications by :meth:`.preprocess`.

        Args:
            coords (:class:`.Coordinates`): Valid :class:`.Coordinates` object, containing x and y coordinates.
            param (:class:`.Param`): Valid :class:`.Param` object, containing simulation parameters.
            state (:class:`.State`): Valid :class:`.State` object, containing density and velocity.
            direction (int): 0 (integrating x) or 1 (integrating y).

        """
        if direction == 1:
            state.transpose((1,0,2))
            state.swap_velocities(1)

        if direction == 2:
            state.transpose((2,1,0))
            state.swap_velocities(2)

        if param.geometry == 'sheet':
            if direction == 0:
                state.vely -= 0.5*param.frame_rotation*coords.x

        if param.geometry == 'cyl':
            if direction == 0:
                state.dens /= coords.x
                state.vely = state.vely/coords.x - np.power(coords.x, -0.5)
                if param.log_radial is True:
                    state.velx *= coords.x
                    state.dens /= coords.x
                    state.soundspeed *= coords.x

            if direction == 1:
                state.vely *= coords.x
                state.soundspeed *= coords.x

        if param.geometry == 'sph':
            if direction == 0:
                state.dens /= (coords.x*coords.x)
                state.vely = (state.vely - np.sqrt(coords.x))/coords.x
                state.velz /= coords.x
                if param.log_radial is True:
                    state.velx *= coords.x
                    state.dens /= coords.x
                    state.soundspeed *= coords.x

            if direction == 1:
                g = coords.x*np.sin(coords.z)
                state.vely *= g
                state.soundspeed *= g

            if direction == 2:
                sint = np.sin(coords.z)
                state.dens /= sint
                state.vely = state.vely*coords.x/sint - \
                  np.power(coords.x, -0.5)
                state.velz *= coords.x
                state.soundspeed *= coords.x

    def evolve(self, t, t_max, coords, param, state,
               source_func, source_param):
        """Evolve state from t to tmax.

        Args:
            t (float): Current simulation time.
            t_max (float): Simulation time to reach before stopping.
            coords (:class:`.Coordinates`): Valid :class:`.Coordinates` object, containing x and y coordinates.
            param (:class:`.Param`): Valid :class:`.Param` object, containing simulation parameters.
            state (:class:`.State`): Valid :class:`.State` object, containing density and velocity.
            source_func (callable): Function integrating any extra source terms (non-geometric). It should accept the following arguments: t, dt, coords, state, source_param.
            source_param (array-like): Extra parameters for source_func.

        Returns:
            (tuple): Tuple consisting of:

                t (float): new simulation time (= t_max if no problems encountered).

                :class:`.State`: Updated :class:`.State`.

        """
        # Evolve until t = t_max
        while (t < t_max):
            # Calculate time step
            dt = param.courant*self.calc_time_step(param.geometry,
                                                   coords, state,
                                                   param.log_radial)
            # End exactly on t_max
            if (t + dt > t_max):
                dt = t_max - t

            # Copy state in case something goes wrong
            old_state = State.copy(state)

            # Continue until everything OK
            success = False
            while success is False:
                print("Current time: {}, time step: {}".format(t, dt))

                # Shear periodic x boundaries if necessary
                if param.boundaries[0][0] == 'shear periodic':
                    self.shear_periodic_boundaries(t, coords, state)

                # Dimensional split: do all dimensions independently
                for dim in (0,1,2):
                    if np.shape(state.dens)[dim] > 1:
                        source = self.preprocess(coords, param, state, dim)

                        # Hydrodynamic update
                        self.roe.step(dt, coords.dxyz[dim], state,
                                      source, param.boundaries[dim])

                        # Orbital advection
                        if dim == 1:
                            self.orbital_advection.step(dt,
                                                        coords.dxyz[dim],
                                                        state)

                        self.postprocess(coords, param, state, dim)

                # Check if density positive
                success = True
                mindens = np.min(state.dens)

                # If not, restore state and try with smaller time step
                if mindens < 0.0:
                    state = State.copy(old_state)
                    dt = 0.5*dt
                    success = False

            # Integrate extra source terms (ignoring ghost zones!)
            if source_func is not None:
                source_func(t, dt, coords, state, source_param)

            # Update simulation time
            t = t + dt

        return t, state


from collections import deque
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
    def addOneRow_dfs(self, root, v, d):  # O(n) both
        def dfs(node, v, d, height, direction):
            if not node:
                if height == d:
                    return TreeNode(v)
                else:
                    return None
            if height == d:
                temp = TreeNode(v)
                if direction == 0:
                    temp.left = node
                elif direction == 1:
                    temp.right = node
                return temp
            node.left = dfs(node.left, v, d, height + 1, 0)
            node.right = dfs(node.right, v, d, height + 1, 1)
            return node
        return dfs(root, v, d, 1, 0)

    def addOneRow_dfs_short(self, root, v, d):
        if d == 1:
            return TreeNode(v, root if left else None, root if not left else None)
        elif root:
            root.left = addOneRow(root.left, v, d - 1, True)
            root.right = addOneRow(root.right, v, d - 1, False)
        return root

    def addOneRow_BFS(self, root, v, d):  # O(n) and O(x) where x is the max number of nodes in a level
        # case when the level is 1
        if d == 1:
            new_root = TreeNode(v)
            new_root.left = root
            return new_root

        dq = deque()
        dq.append(root)
        level = 1
        while level != d - 1:
            for _ in range(len(dq)):
                curr = dq.popleft()
                if curr.left:
                    dq.append(curr.left)
                if curr.right:
                    dq.append(curr.right)
            level += 1
        for _ in range(len(dq)):
            curr = dq.popleft()
            left = curr.left
            right = curr.right
            new1 = TreeNode(v)
            new2 = TreeNode(v)
            curr.left = new1
            new1.left = left
            curr.right = new2
            new2.right = right
        return root


from itertools import permutations
temp=input().split(',')
array=[]
for i in range(len(temp)):
    array.append(int(temp[i]))
ans=[]
for i in permutations(array,len(array)):
    res=[]
    flag=True
    for j in range(len(i)-1):
        temp=i[j]+i[j+1]
        temp=int(temp**0.5)
        temp=temp**2
        if temp!=i[j]+i[j+1]:
            flag=False
    if flag:
        for k in range(len(i)):
            res.append(i[k])
        if ans.count(res)==0:
            ans.append(res)
print(len(ans))


import pandas as pd
import numpy as np
import json

xls = pd.ExcelFile('Clientes.xlsx')

df = xls.parse('Carga de clientes', skiprows=6, index_col=None, na_values=['NA'])

df = df.fillna("")

df = df.rename(
    columns={'Unnamed: 0': '#',
             'Unnamed: 1': 'Tipo',
             'Unnamed: 2': 'CNPJ/CPF',
             'Unnamed: 3': 'Razão social / Nome',
             'Unnamed: 4': 'Nome fantasia',
             'Unnamed: 5': 'Inscrição estadual',
             'Unnamed: 6': 'Inscrição municipal',
             'Unnamed: 7': 'Código do cliente',
             'Unnamed: 8': 'É cliente final?',
             'Unnamed: 9': 'CEP',
             'Unnamed: 10': 'Endereço',
             'Unnamed: 11': 'Número',
             'Unnamed: 12': 'Complemento',
             'Unnamed: 13': 'Bairro',
             'Unnamed: 14': 'Cidade',
             'Unnamed: 15': 'Estado',
             'Unnamed: 17': 'Contato',
             'Unnamed: 24': 'Anotações',
             'Unnamed: 25': 'Limite de crédito',
             'Unnamed: 26': 'Bloquear venda para o cliente?'
            })

df = df.dropna(how='all', axis=1)

df = df.drop(columns=['Inscrição municipal', 'Contato', 'Tipo', 'Tipo.1', 'Contato.1', 'Tipo.2', 'Contato.2', 'Tipo.3', 'Contato.3', 'Anotações'])

df = pd.DataFrame(df)

df

for row in df.iterrows():
    data = {
        "Status": "1",
        "Resale": False,
        "Notes": "",
        "PriceTableId": "",
        "CustomerCode": "2",
        "CreditLimit": "0",
        "BlockSale": False,
        "Tags": [],
        "Person": {
            "Type": "2",
            "RegistrationCode": "000000000000000000",
            "CompanyName": "BLA BLA LGPD",
            "SocialName": "BLA BLA LGPD",
            "RegistrationState": "000.000.000.000",
            "RegistrationCity": "",
            "Taxpayer": "",
            "Taxing": "",
            "BusinessSituation": "",
            "PaymentMethodId": None,
            "PreferredSalesPersonId": "",
            "FinalCostumer": False,
            "RegistrationStateRequired": False
        },
    }
    #print(row[1]['CNPJ/CPF'])
    #print(row[1]['CNPJ/CPF'])

import requests
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

TOKEN =''
hed = {'Authorization': 'Bearer ' + TOKEN}
api = 'https://localhost:5001/client'
dt = {'JSON CLIENT'}
response = requests.post(api, json=dt, headers=hed, verify=False)
print(response.status_code)
print(response.json())

contador = 0
for row in df.iterrows():
    contador = contador + 1
    TOKEN ='TOKEN'
    hed = {'Authorization': 'Bearer ' + TOKEN}
    api = 'https://localhost:5001/client/'
    response = requests.get(api, headers=hed, verify=False)
    if response.status_code == 200:
        print('Salvo com sucesso ' + row[1]['CNPJ/CPF'] + ' ' + row[1]['Razão social / Nome'])
    else:
        print(response.json()['errorMessages'] + row[1]['CNPJ/CPF'] + ' ' + row[1]['Razão social / Nome'])
        print(response.json())
    print(contador)


# Implement a basic measure of tracking the space used by an algorithm

from memory_profiler import profile

@profile # the function decorator that will on every process track storage used by the function
def algorithm():
    # An algorithm to print the x values whose f(x) for f(x) = x^2 are divisible by 2
    x = 1
    # x and y lists for x and y values to be saved
    x_values = []
    y_values = []
    while x < 10:
        y = x ** 2
        if y % 2 == 0:
            x_values.append(x)
            y_values.append(y)
        x = x + 1
    print("The x values are", x_values)
    print("The y values are", y_values)

algorithm() # function call


'''

Program 6 Written by Derek Hickey

'''

courses = {'CIS3389':['0342', 'Keefe', '10:00 a.m.'],'CS101':[3004, 'Haynes', '8:00 a.m.'],
           'CS102':[4501, 'Alvarado', '9:00 a.m.'],'CS103':[6755, 'Rich', '10:00 a.m.'],
           'NT110':[1244, 'Burke', '11:00 a.m.'], 'CM241':[1411, 'Lee', '1:00 p.m.']}

try:

        entry = input("Enter a course or enter n to quit: ").upper()

        while entry != 'N':

                    print('Course Number: ', entry)
                    print('Room Number: ', courses[entry][0])
                    print('Instructor: ', courses[entry][1])
                    print('Meeting Time: ', courses[entry][2])
                    entry = input("Enter another course or enter n to quit: ").upper()

except Exception as err:
        print('invalid course number')


from pymongo import MongoClient
from dotenv import load_dotenv
from datetime import datetime
import os
import pymongo

load_dotenv()
MONGO_SERVER = os.getenv("MONGO_SERVER")

class GoogleFeudDB:
    def __init__(self, guild, channel):
        client = MongoClient(MONGO_SERVER)
        self.db = client.gfeuddb
        self.guild = guild
        self.channel = channel
        self.updateLastModifiedTime()

    def createSession(self):
        """
        Creates a session with guild, channel and an empty set of player scores
        """
        try:
            session = {
                "guild": self.guild,
                "channel": self.channel,
                "phrase": str(),
                "scores": dict(),
                "suggestions": dict(),
                "turns": 5,
                "last_modified": datetime.utcnow(),
            }
            # suggestions contains objects with the following shape
            # <suggestion_phrase>: Object
            #   solved: boolean
            #   score: int
            #   solvedBy: string
            #
            # scores has the following shape
            #   scores: object {
            #       id: object { score: int, display_name: string }
            #   }
            self.db.sessions.insert(session)
        except Exception as error:
            print("Failed to create a session: ", error)

    def terminateSession(self):
        """
        Deletes game session
        """
        try:
            return self.db.sessions.delete_one(
                {"guild": self.guild, "channel": self.channel}
            )
        except Exception as error:
            print("Failed terminate game session: ", error)

    def updateLastModifiedTime(self):
        """
        Adds and updates the last_modified time field for a session.
        Used to expire a game session after the expiration time has passed
        """
        try:
            return self.db.sessions.update_one(
                {"guild": self.guild, "channel": self.channel},
                {"$set": {"last_modified": datetime.utcnow()}},
            )
        except Exception as error:
            print("Failed to update last modified time: ", error)

    def getSession(self):
        """
        Returns session document contained as a Python dict using guild and channel. If it doesn't exist, return None
        """
        try:
            return self.db.sessions.find_one(
                {"guild": self.guild, "channel": self.channel}
            )
        except Exception as error:
            print("Failed to retrieve a game session: ", error)

    def getDisplayNameForUser(self, user_id):
        """
        Returns the display_name for a user with the given user_id from scores.
        """
        try:
            session = self.db.sessions.find_one(
                {"guild": self.guild, "channel": self.channel}
            )
            return session["scores"][user_id]["display_name"]
        except Exception as error:
            print("Failed to retrieve the display name: ", error)

    def updatePhrase(self, phrase):
        """
        Updates the phrase in a session.
        """
        try:
            new_phrase = {"phrase": phrase}

            result = self.db.sessions.update_one(
                {"guild": self.guild, "channel": self.channel}, {"$set": new_phrase}
            )
        except Exception as error:
            print("Failed to update phrase: ", error)

    def updateScoreForUser(self, user, score):
        """
        Increase the score for a user. Creates and sets score for user if it doesnt exist
        """
        user_id = str(user.id)
        display_name = str(user.display_name)
        try:
            if self.db.sessions.find_one({f"scores.{user_id}": {"$exists": True}}):
                new_score = {f"scores.{user_id}.score": score}
                operation = {"$inc": new_score}
            else:
                new_entry = {
                    f"scores.{user_id}": {"score": score, "display_name": display_name}
                }
                operation = {"$set": new_entry}

            result = self.db.sessions.update_one(
                {"guild": self.guild, "channel": self.channel}, operation
            )
        except Exception as error:
            print("Failed to update score for user: ", error)

    def insertSuggestions(self, phrase_suggestions):
        """
        Inserts suggestions dict into db
        @param suggestions dictionary
        """
        try:
            suggestions = {"suggestions": phrase_suggestions}
            result = self.db.sessions.update_one(
                {"guild": self.guild, "channel": self.channel}, {"$set": suggestions}
            )
        except Exception as error:
            print("Failed to insert suggestions: ", error)

    def updateSuggestionSolved(self, suggestion, user_id):
        """
        Finds and updates suggestion for user in a session using the given guild and channel values.
        https://stackoverflow.com/questions/28828825/updating-an-object-inside-an-array-with-pymongo
        https://docs.mongodb.com/manual/core/document/#dot-notation
        """
        try:
            solved_suggestion = {"$set": {f"suggestions.{suggestion}.solved": True}}

            result = self.db.sessions.update_one(
                {"guild": self.guild, "channel": self.channel}, solved_suggestion
            )

            solvedBy_suggestion = {
                "$set": {f"suggestions.{suggestion}.solvedBy": user_id}
            }

            result = self.db.sessions.update_one(
                {"guild": self.guild, "channel": self.channel}, solvedBy_suggestion
            )
        except Exception as error:
            print("Failed to update suggestion: ", error)

    def updateTurn(self):
        """
        Update turn count by decrementing turn value.
        """
        try:
            new_turn = {"turns": -1}

            result = self.db.sessions.update_one(
                {"guild": self.guild, "channel": self.channel}, {"$inc": new_turn}
            )
        except Exception as error:
            print("Failed update the turns for this session: ", error)

    def getGoogleSearchPhrase(self):
        """
        Returns a random Google search phrase string.
        Connect to MongoDB server to add/remove phrases.
        """
        try:
            search_phrase = self.db.searchphrases.aggregate([{"$sample": {"size": 1}}])
            # Aggregate returns list of objects. We only need the first random sample
            for phrase in search_phrase:
                return phrase["phrase"]

        except Exception as error:
            print("Failed to fetch search phrase: ", error)

    def checkIfUserIsAdmin(self, discord_author):
        """
        Returns user contained as a Python dict using Discord author's id. If it doesn't exist, return None
        """
        try:
            return self.db.roles.find_one({"user_id": discord_author.id})
        except Exception as error:
            print("Failed to retrieve admin user: ", error)

    def addGoogleSearchPhrase(self, phrase):
        """
        ADMIN: Adds a Google search phrase.
        """
        try:
            trimmed_phrase = phrase.strip()
            self.db.searchphrases.insert({"phrase": phrase})
        except Exception as error:
            print("Failed to add a search phrase: ", error)

    def add_contribution(self, phrase, suggestions, discord_author):
        try:
            trimmed_phrase = phrase.strip()
            # Shape of a contribution document
            self.db.contributions.insert(
                {
                    "user_id": str(discord_author.id),
                    "phrase": phrase,
                    "suggestions": suggestions,
                    "submitted_on": datetime.utcnow(),
                }
            )
            if self.db.contributors.find_one({"user_id": str(discord_author.id)}):
                result = self.db.contributors.update_one(
                    {"user_id": str(discord_author.id)},
                    {
                        "$inc": {
                            "num_of_contributions_today": 1,
                            "num_of_contributions": 1,
                        }
                    },
                )
            else:
                # Shape of a contributor document
                self.db.contributors.insert(
                    {
                        "user_id": str(discord_author.id),
                        "num_of_contributions": 1,
                        "num_of_approved_contributions": 0,
                        "num_of_contributions_today": 1,
                        "daily_limit": 10,
                    }
                )
        except Exception as error:
            print("Failed to add a contribution: ", error)

    def get_oldest_contribution(self):
        """
        Returns the oldest submitted contribution from our collection. Returns None if the collection is empty
        """
        try:
            return self.db.contributions.find().sort("submitted_on", 1).limit(1)[0]
        except Exception as error:
            print("Failed to get oldest contribution: ", error)

    def delete_contribution(self, phrase):
        """
        Deletes a contribution based on its phrase
        """
        try:
            return self.db.contributions.delete_one({"phrase": phrase})
        except Exception as error:
            print("Failed to delete contribution: ", error)

    def increment_approved_contribution(self, user_id):
        """
        Credit a user for their contribution and increase their contributions approved tally
        """
        try:
            return self.db.contributors.update_one(
                {"user_id": str(user_id)},
                {"$inc": {"num_of_approved_contributions": 1}},
            )
        except Exception as error:
            print("Failed to increased number of approved contributions: ", error)

    def get_contributor(self, discord_author):
        try:
            return self.db.contributors.find_one({"user_id": str(discord_author.id)})
        except Exception as error:
            print("Failed to get contributor: ", error)

    def check_if_phrase_is_in_contributions(self, phrase):
        try:
            trimmed_phrase = phrase.strip()
            return self.db.contributions.find_one({"phrase": phrase})
        except Exception as error:
            print("Failed to look for the phrase in contributions: ", error)

    def checkIfSearchPhraseExists(self, phrase):
        """
        ADMIN: Checks if the given phrase already exists
        """
        try:
            trimmed_phrase = phrase.strip()
            return self.db.searchphrases.find_one({"phrase": phrase})
        except Exception as error:
            print("Failed to look for the given phrase: ", error)

    def updateLeaderboard(self, user_id: str) -> pymongo.UpdateOne:
        try:
            return self.db.leaderboard.update_one(
                {"user_id": user_id}, {"$inc": {"wins": 1}}, upsert=True
            )
        except Exception as error:
            print("Failed to update leaderboard for user", user_id, error)

    def getLeaderboard(self, user_ids: list[str]) -> pymongo.CursorType:
        try:
            return self.db.leaderboard.find({"user_id": {"$in": user_ids}})
        except Exception as error:
            print("Failed to get leaderboard for users", user_ids, error)


import numpy as np
import scipy
import ldpc

def gauss(X):
    m, n = X.shape

    full_ind = np.arange(m)
    w_ind = np.zeros(n, dtype=bool)
    A = np.copy(X)

    for i in range(0, m):
        if np.sum(A[i:, ]) == 0:
            raise ldpc.DegenerateMatrixError
            break

        ones_ind = np.argwhere(A[i:, ])
        ind = np.argmin(ones_ind[:, 1])
        col = np.min(ones_ind[:, 1])
        w_ind[col] = True
        first_in = ones_ind[ind, 0] + i

        t = np.copy(A[first_in, :])
        A[first_in, :] = np.copy(A[i, :])
        A[i, :] = t

        w = (A[:, col] == 1) & (full_ind != i)
        A[w, i:] = (A[w, i:] + A[i, i:]) % 2
        A[:i, col] = 0

    return A, w_ind

def trimmer(a, trim):
    t_a = np.copy(a)
    t_a[t_a < trim] = trim
    t_a[t_a > (1 - trim)] = 1 - trim
    return t_a


import os
import shutil

# Directorio donde se encuentra el script
directorio_actual = os.getcwd()

# Define las carpetas de destino para cada tipo de archivo
carpeta_fotos = os.path.join(directorio_actual, "Fotos")
carpeta_videos = os.path.join(directorio_actual, "Videos")
carpeta_instaladores = os.path.join(directorio_actual, "Instaladores")
carpeta_documentos = os.path.join(directorio_actual, "Documentos")
carpeta_adobe = os.path.join(directorio_actual, "Adobe")
carpeta_scripts = os.path.join(directorio_actual, "Scripts")
carpeta_audio = os.path.join(directorio_actual, "Audio")
carpeta_otros = os.path.join(directorio_actual, "Otros")

# Lista de carpetas de destino
carpetas_destino = [
    carpeta_fotos, carpeta_videos, carpeta_instaladores,
    carpeta_adobe, carpeta_scripts,
    carpeta_audio, carpeta_otros
]

# Crea las carpetas de destino si no existen
for carpeta in carpetas_destino:
    if not os.path.exists(carpeta):
        os.makedirs(carpeta)

# Carpeta de destino para documentos
carpeta_documentos_destino = os.path.join(carpeta_documentos, "PDF")

# Verifica y crea la carpeta de destino para documentos si no existe
if not os.path.exists(carpeta_documentos_destino):
    os.makedirs(carpeta_documentos_destino)

# Extensiones de archivo para documentos de Microsoft
extensiones_microsoft = {
    ".pdf": "PDF",
    ".doc": "Word",
    ".docx": "Word",
    ".ppt": "PowerPoint",
    ".pptx": "PowerPoint",
    ".xls": "Excel",
    ".xlsx": "Excel"
}

# Extensiones de archivo para scripts de programación
extensiones_scripts = (".py", ".cs", ".java", ".cpp", ".rb", ".js", ".html", ".css", ".php", ".sql")

# Extensiones de archivo para archivos de audio
extensiones_audio = (".mp3", ".wav", ".flac", ".aac")

# Itera a través de los archivos en el directorio de entrada (directorio_actual)
for archivo in os.listdir(directorio_actual):
    ruta_archivo = os.path.join(directorio_actual, archivo)

    # Ignora si es un directorio
    if os.path.isdir(ruta_archivo):
        continue

    # Determina el tipo de archivo según su extensión
    extension = os.path.splitext(archivo)[1].lower()

    try:
        # Mueve el archivo a la carpeta correspondiente
        if extension in (".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tiff"):
            shutil.move(ruta_archivo, os.path.join(carpeta_fotos, archivo))
        elif extension in (".mp4", ".avi", ".mov", ".mkv"):
            shutil.move(ruta_archivo, os.path.join(carpeta_videos, archivo))
        elif extension in (".exe", ".iso"):
            shutil.move(ruta_archivo, os.path.join(carpeta_instaladores, archivo))
        elif extension in extensiones_microsoft:
            # Si es un documento de Microsoft, organiza en subcarpetas
            subcarpeta = extensiones_microsoft[extension]
            carpeta_destino = os.path.join(carpeta_documentos, subcarpeta)
            if not os.path.exists(carpeta_destino):
                os.makedirs(carpeta_destino)
            shutil.move(ruta_archivo, os.path.join(carpeta_destino, archivo))
        elif extension in extensiones_scripts:
            shutil.move(ruta_archivo, os.path.join(carpeta_scripts, archivo))
        elif extension in extensiones_audio:
            shutil.move(ruta_archivo, os.path.join(carpeta_audio, archivo))
        elif extension in (".pdf", ".ai", ".eps", ".indd", ".psd"):
            shutil.move(ruta_archivo, os.path.join(carpeta_adobe, archivo))
        else:
            shutil.move(ruta_archivo, os.path.join(carpeta_otros, archivo))
    except Exception as e:
        print(f"Error al mover el archivo {archivo}: {e}")

print("Archivos organizados con éxito en el directorio actual.")


import pandas as pd

from cellphonedb.utils import dataframe_format

def dataframes_has_same_data(dataframe1: pd.DataFrame, dataframe2: pd.DataFrame,
                             round_decimals: bool = False) -> pd.DataFrame:
    dataframe1 = dataframe1.copy(deep=True)
    dataframe2 = dataframe2.copy(deep=True)

    columns_names_1 = list(dataframe1.columns.values)
    columns_names_1.sort()

    dataframe1 = dataframe_format.bring_columns_to_end(columns_names_1, dataframe1)

    columns_names_2 = list(dataframe2.columns.values)
    columns_names_2.sort()
    dataframe2 = dataframe_format.bring_columns_to_end(columns_names_2, dataframe2)

    if not dataframe1.empty:
        dataframe1 = dataframe1.sort_values(columns_names_1).reset_index(drop=True)

        if round_decimals:
            dataframe1 = dataframe1.round(5)

    if not dataframe2.empty:
        dataframe2 = dataframe2.sort_values(columns_names_2).reset_index(drop=True)

        if round_decimals:
            dataframe2 = dataframe2.round(5)

    if dataframe1.empty and dataframe2.empty:
        return pd.Series(dataframe1.columns.values).equals(pd.Series(dataframe2.columns.values))

    return dataframe1.equals(dataframe2)


from django.contrib import admin
from django.urls import path, include

import oprosnik

from django.urls import path
from . import views

app_name = 'polls'
urlpatterns = [
    path('login/', views.login, name='login'),
    # poll
    path('poll/', views.PollView.as_view({'get': 'list', 'post': 'create'})),
    path('poll/<int:pk>/', views.PollView.as_view({'get': 'retrieve', 'put': 'update', 'delete': 'destroy'})),
    path('poll/active/', views.ActivePollView.as_view({'get': 'list'})),
    # question
    path('question/', views.QuestionView.as_view({'get': 'list', 'post': 'create'})),
    path('question/<int:pk>/', views.QuestionView.as_view({'get': 'retrieve', 'put': 'update', 'delete': 'destroy'})),

    # choice
    path('choice/', views.ChoiceView.as_view({'get': 'list', 'post': 'create'})),
    path('choice/<int:pk>/', views.ChoiceView.as_view({'get': 'retrieve', 'put': 'update', 'delete': 'destroy'})),
    # answer
    path('answer/', views.AnswerView.as_view({'get': 'list', 'post': 'create'})),
    path('answer/<int:pk>/', views.AnswerView.as_view({'get': 'retrieve', 'put': 'update', 'delete': 'destroy'})),

]


from __future__ import absolute_import
from cli.utils.services import *
from cli.utils.models.app import App

class AppService(BaseService):

  @classmethod
  def fetch(cls, app_id):
    request = Request(
      method = "get",
      authenticated = True,
      endpoint = "/v1/apps/{}".format(app_id),
    )

    response = cls.request(request)
    if not response: return
    return App.build(response["app"])


import vectorbt as vbt

import numpy as np
import pandas as pd
from datetime import datetime, timedelta
import pytz
from dateutil.parser import parse
import ipywidgets as widgets
from copy import deepcopy
from tqdm import tqdm
import imageio
from IPython import display
import plotly.graph_objects as go
import itertools
import dateparser
import gc

# Enter your parameters here
seed = 42
symbol = 'BTC-USD'
metric = 'total_return'

start_date = datetime(2018, 1, 1, tzinfo=pytz.utc)  # time period for analysis, must be timezone-aware
end_date = datetime(2020, 1, 1, tzinfo=pytz.utc)
time_buffer = timedelta(days=100)  # buffer before to pre-calculate SMA/EMA, best to set to max window
freq = '1D'

vbt.settings.portfolio['init_cash'] = 100.  # 100$
vbt.settings.portfolio['fees'] = 0.0025  # 0.25%
vbt.settings.portfolio['slippage'] = 0.0025  # 0.25%

# Download data with time buffer
cols = ['Open', 'High', 'Low', 'Close', 'Volume']
ohlcv_wbuf = vbt.YFData.download(symbol, start=start_date-time_buffer, end=end_date).get(cols)

ohlcv_wbuf = ohlcv_wbuf.astype(np.float64)

# print(ohlcv_wbuf.shape)
# print(ohlcv_wbuf.columns)

# Create a copy of data without time buffer
wobuf_mask = (ohlcv_wbuf.index >= start_date) & (ohlcv_wbuf.index <= end_date) # mask without buffer

ohlcv = ohlcv_wbuf.loc[wobuf_mask, :]

# print(ohlcv.shape)

# Plot the OHLC data
# ohlcv_wbuf.vbt.ohlcv.plot().show_svg()
# remove show_svg() to display interactive chart!
ohlcv_wbuf.vbt.ohlcv.plot().show()

## SINGLE WINDOW COMBINATION

fast_window = 30
slow_window = 80

# Pre-calculate running windows on data with time buffer
fast_ma = vbt.MA.run(ohlcv_wbuf['Open'], fast_window)
slow_ma = vbt.MA.run(ohlcv_wbuf['Open'], slow_window)

print(fast_ma.ma.shape)
print(slow_ma.ma.shape)

# Remove time buffer
fast_ma = fast_ma[wobuf_mask]
slow_ma = slow_ma[wobuf_mask]

# there should be no nans after removing time buffer
assert(~fast_ma.ma.isnull().any())
assert(~slow_ma.ma.isnull().any())

print(fast_ma.ma.shape)
print(slow_ma.ma.shape)

# Generate crossover signals
dmac_entries = fast_ma.ma_crossed_above(slow_ma)
dmac_exits = fast_ma.ma_crossed_below(slow_ma)

fig = ohlcv['Open'].vbt.plot(trace_kwargs=dict(name='Price'))
fig = fast_ma.ma.vbt.plot(trace_kwargs=dict(name='Fast MA'), fig=fig)
fig = slow_ma.ma.vbt.plot(trace_kwargs=dict(name='Slow MA'), fig=fig)
fig = dmac_entries.vbt.signals.plot_as_entry_markers(ohlcv['Open'], fig=fig)
fig = dmac_exits.vbt.signals.plot_as_exit_markers(ohlcv['Open'], fig=fig)

fig.show_svg()

# Signal stats
print(dmac_entries.vbt.signals.stats(settings=dict(other=dmac_exits)))

# Plot signals
fig = dmac_entries.vbt.signals.plot(trace_kwargs=dict(name='Entries'))
dmac_exits.vbt.signals.plot(trace_kwargs=dict(name='Exits'), fig=fig).show_svg()

# Build partfolio, which internally calculates the equity curve

# Volume is set to np.inf by default to buy/sell everything
# You don't have to pass freq here because our data is already perfectly time-indexed
dmac_pf = vbt.Portfolio.from_signals(ohlcv['Close'], dmac_entries, dmac_exits)

# Print stats
print(dmac_pf.stats())

# Plot trades
print(dmac_pf.trades.records)
dmac_pf.trades.plot().show_svg()

# Now build portfolio for a "Hold" strategy
# Here we buy once at the beginning and sell at the end
hold_entries = pd.Series.vbt.signals.empty_like(dmac_entries)
hold_entries.iloc[0] = True
hold_exits = pd.Series.vbt.signals.empty_like(hold_entries)
hold_exits.iloc[-1] = True
hold_pf = vbt.Portfolio.from_signals(ohlcv['Close'], hold_entries, hold_exits)

# Equity
fig = dmac_pf.value().vbt.plot(trace_kwargs=dict(name='Value (DMAC)'))
hold_pf.value().vbt.plot(trace_kwargs=dict(name='Value (Hold)'), fig=fig).show_svg()

# Now we will implement an interactive window slider to easily compare windows by their performance.

min_window = 2
max_window = 100

perf_metrics = ['total_return', 'positions.win_rate', 'positions.expectancy', 'max_drawdown']
perf_metric_names = ['Total return', 'Win rate', 'Expectancy', 'Max drawdown']

windows_slider = widgets.IntRangeSlider(
    value=[fast_window, slow_window],
    min=min_window,
    max=max_window,
    step=1,
    layout=dict(width='500px'),
    continuous_update=True
)
dmac_fig = None
dmac_img = widgets.Image(
    format='png',
    width=vbt.settings['plotting']['layout']['width'],
    height=vbt.settings['plotting']['layout']['height']
)
metrics_html = widgets.HTML()

def on_value_change(value):
    global dmac_fig

    # Calculate portfolio
    fast_window, slow_window = value['new']
    fast_ma = vbt.MA.run(ohlcv_wbuf['Open'], fast_window)
    slow_ma = vbt.MA.run(ohlcv_wbuf['Open'], slow_window)
    fast_ma = fast_ma[wobuf_mask]
    slow_ma = slow_ma[wobuf_mask]
    dmac_entries = fast_ma.ma_crossed_above(slow_ma)
    dmac_exits = fast_ma.ma_crossed_below(slow_ma)
    dmac_pf = vbt.Portfolio.from_signals(ohlcv['Close'], dmac_entries, dmac_exits)

    # Update figure
    if dmac_fig is None:
        dmac_fig = ohlcv['Open'].vbt.plot(trace_kwargs=dict(name='Price'))
        fast_ma.ma.vbt.plot(trace_kwargs=dict(name='Fast MA'), fig=dmac_fig)
        slow_ma.ma.vbt.plot(trace_kwargs=dict(name='Slow MA'), fig=dmac_fig)
        dmac_entries.vbt.signals.plot_as_entry_markers(ohlcv['Open'], fig=dmac_fig)
        dmac_exits.vbt.signals.plot_as_exit_markers(ohlcv['Open'], fig=dmac_fig)
    else:
        with dmac_fig.batch_update():
            dmac_fig.data[1].y = fast_ma.ma
            dmac_fig.data[2].y = slow_ma.ma
            dmac_fig.data[3].x = ohlcv['Open'].index[dmac_entries]
            dmac_fig.data[3].y = ohlcv['Open'][dmac_entries]
            dmac_fig.data[4].x = ohlcv['Open'].index[dmac_exits]
            dmac_fig.data[4].y = ohlcv['Open'][dmac_exits]
    dmac_img.value = dmac_fig.to_image(format="png")

    # Update metrics table
    sr = pd.Series([dmac_pf.deep_getattr(m) for m in perf_metrics],
                   index=perf_metric_names, name='Performance')
    metrics_html.value = sr.to_frame().style.set_properties(**{'text-align': 'right'}).render()

windows_slider.observe(on_value_change, names='value')
on_value_change({'new': windows_slider.value})

dashboard = widgets.VBox([
    widgets.HBox([widgets.Label('Fast and slow window:'), windows_slider]),
    dmac_img,
    metrics_html
])
dashboard

dashboard.close() # after using, release memory and notebook metadata

gc.collect()


import sys
import numpy as np

from typing import List, Callable, Optional, Tuple, Sequence
from collections import UserDict
from EmbeddingDictionary import EntryKey, EntryValue, EmbeddingDictionary
from Jsonifiable import JsonEncodable

class WordSimilarity(JsonEncodable):
    def __init__(self, word: str, similarityScore: float):
        if not isinstance(word, str) or not isinstance(similarityScore, float):
            raise TypeError('WordSimilarity expects (str, float), but got (%s, %s)' % \
                            (type(word), type(similarityScore)))
        self._word = word
        self._similarityScore = similarityScore

    @property
    def word(self):
        return self._word

    @property
    def similarity(self):
        return self._similarityScore * 100

    def __str__(self):
        return "\"%s\": %.2f%%" % (self.word, self.similarity)

    def __repr__(self):
        return "WordSimilarity(%s, %.2f)" % (self.word, self._similarityScore)

    def __lt__(self, other):
        return self.similarity < other.similarity

    def jsonDefault(self):
        return dict(word=self.word, similarityScore=self.similarity)

class WordSimilarityList(np.ndarray, JsonEncodable):

    def __new__(cls, inputArray):
        obj = np.asarray(inputArray).view(cls)
        return obj

    def sortBySimilarity(self, reverse=False):
        if reverse:
            self[::-1].sort()
        else:
            self.sort()

        return self

    def stringify(self, strFormat: str, start: Optional[int] = None, end: Optional[int] = None):
        """
        stringify the list in given format and range [start, end)
        :param (str) strFormat: format to print each element
        :param (int) start: start index of element to be printed (inclusive)
        :param (int) end: end index of element to be printed (exclusive)
        :return (str): stringified list
        """
        start = 0 if start is None else start
        end = len(self) if end is None else end
        ret = ""
        for i in range(start, start + end):
            if not isinstance(self[i], WordSimilarity):
                raise TypeError('WordSimilarityList expects element type WordSimilarity, '
                                'but got %s' % type(self[i]))
            ret += strFormat % (i+1, self[i])
        ret = ret[:-1]  # ignore the additional ending format
        return ret

    def __str__(self):
        return self.stringify("%d. %s\n")

    def jsonDefault(self):
        return self.tolist()

class CategorySimilarityDict(UserDict, JsonEncodable):
    def stringify(self, strFormat):
        ret = ""
        for cat, simList in self.items():
            ret += strFormat % (cat, simList.stringify("\t%d. %s\n"))

        return ret[:-1]

    def __str__(self):
        return self.stringify("%s:\n%s\n")

    def __repr__(self):
        return "CategorySimilarityDict(%s)" % super().__repr__()

    def jsonDefault(self):
        return self.data

class SimilarityDictionary(object):
    def __init__(self, embedDict: EmbeddingDictionary,
                 similarityFunction: Callable[[Sequence, Sequence], float]):
        """
        :param embedDict: a reference to initialized an EmbeddingDictionary object
        :param similarityFunction: function to compute similarity with
        """
        self._embedDict = embedDict
        self._simFunc = similarityFunction
        self._data = dict()

    def __contains__(self, item: EntryKey):
        if not isinstance(item, EntryKey):
            raise TypeError('SimilarityDictionary expects type EntryKey as key, '
                            'but got %s' % type(item))
        if item.isEmpty() or item.word is None:
            raise KeyError("EntryKey.word must be set!")
        if item.category is None:
            return item.word in self._data
        return item.word in self._data and item.category in self._data[item.word]

    def __str__(self):
        ret = ""
        for word in self._data.keys():
            ret += "%s:\n" % word
            for cat, simList in self._data[word].items():
                ret += "\t%s:\n%s\n" % (cat, simList.stringify('\t\t%d. %s\n'))
            # ret += "%s:\n%s\n" % (key, self.data[key].stringify('\t%d. %s\n'))
        return ret[:-1]

    @property
    def embedDict(self):
        return self._embedDict

    @embedDict.setter
    def embedDict(self, newDict: EmbeddingDictionary):
        if self._embedDict is newDict:
            return
        if not isinstance(newDict, EmbeddingDictionary):
            raise TypeError("expects ")
        print("[INFO] SimilarityDictionary dictionary changed, clearing similarity data",
              file=sys.stderr)
        self._data.clear()      # clears data as new dict is used and old data should be nullified
        self._embedDict = newDict

    @property
    def similarityFunction(self):
        return self._simFunc

    @similarityFunction.setter
    def similarityFunction(self, newSimFunc):
        if self._simFunc is newSimFunc:
            return
        print("[INFO] SimilarityDictionary similarity function changed, "
              "clearing similarity data", file=sys.stderr)
        self._data.clear()
        self._simFunc = newSimFunc

    def similarityBetweenWords(self, key1: EntryKey, key2: EntryKey) -> float:
        if not key1.isFull() or not key2.isFull():
            raise ValueError("EntryKeys must be full (both word and category provided)")
        return self._simFunc(self.embedDict[key1].embedding, self.embedDict[key2].embedding)

    def similarityInCategory(self, key: EntryKey, forceUpdate=False) -> WordSimilarityList:
        if not key.isFull():
            raise ValueError("EntryKey must be fully set!")
        # if the similarity for this word in this category is not computed,
        # compute
        if forceUpdate or key not in self:
            val: EntryValue = self.embedDict[key]
            otherVals: List[Tuple[str, EntryValue]] = \
                [(w, v) for w, v in self.embedDict.category[key.category].items()
                 if w != key.word]

            if key.word not in self._data:
                self._data[key.word] = CategorySimilarityDict()
            self._data[key.word][key.category] = WordSimilarityList(
                [WordSimilarity(w, self._simFunc(val.embedding, v.embedding))
                 for w, v in otherVals]
            )
        return self._data[key.word][key.category]

    def similarity(self, key: EntryKey, forceUpdate=False):
        if key.isEmpty() or key.word is None:
            raise ValueError("EntryKey.word must be set!")
        simsByCat = CategorySimilarityDict()
        if key.category is None:
            for cat in self.embedDict[key].keys():
                simsByCat[cat] = self.similarityInCategory(EntryKey(key.word, cat), forceUpdate)
        else:
            simsByCat[key.category] = self.similarityInCategory(key, forceUpdate)
        return simsByCat

    def rankedSimilarity(self, key: EntryKey, top: Optional[int] = None,
                         bottom: Optional[int] = None, forceUpdate=False):
        if key.isEmpty() or key.word is None:
            raise ValueError("EntryKey.word must be set!")
        simsByCat = self.similarity(key, forceUpdate)
        if top is not None and bottom is not None:
            raise ValueError("parameters top and bottom are mutually exclusive")
        for cat in simsByCat.keys():
            if top:
                simsByCat[cat] = simsByCat[cat].sortBySimilarity(reverse=True)[:top]
            elif bottom:
                simsByCat[cat] = simsByCat[cat].sortBySimilarity(reverse=True)[bottom:]
            else:
                simsByCat[cat] = simsByCat[cat].sortBySimilarity(reverse=True)

        return simsByCat


from constraint import *

class RAWCS:
    def __init__(self, registers, graph, solver=RecursiveBacktrackingSolver):
        """
        RAWCS class constructor

        :param registers: Amount of registers available for allocation
        :type registers: int
        :param graph: Interference graph of variables
        :type graph: Instance of ExtendedDiGraph
        """
        self.graph = graph
        self.registers = registers
        self.spilledOut = []
        self.solver = solver

    def _allocate(self, singleSolution=True):
        """
        Tries to allocate the interference graph variables into the available registers

        :return: List of the found allocation or the first valid allocation found,
                 returns None in case there's no possible allocation.
        :rtype: [{str: int}], {str: int} or None
        """
        solution = self._prepare().getSolution() if singleSolution else self._prepare().getSolutions()
        while solution == [] or solution == None:
            self._spill()
            solution = self._prepare().getSolution() if singleSolution else self._prepare().getSolutions()
        return solution

    def getAllocation(self):
        """
        Return the first valid allocation found

        :return: The first valid allocation found or None if there's no possible allocation
        :rtype : {str: int} or None
        """
        return self._allocate()

    def getAllocations(self):
        """
        Return the list with all the valid allocations found

        :return: List with allocations or None is case there's no possible allocation
        :rtype : [{str: int}] or None
        """
        return self._allocate(singleSolution=False)

    def _prepare(self):
        """
        Creates an python-constraint problem adding variables, domain and constraints

        :return: Instance of Problem
        :rtype: Problem
        """
        if any(self.graph.adjList):
            allocation = Problem(self.solver())
            registers_list = ['R' + str(n) for n in range(0, self.registers)]
            vars_list = [var for var in self.graph.adjList]
            allocation.addVariables(vars_list, registers_list)
            for var in vars_list:
                for adjacent in self.graph.adjList[var]:
                    allocation.addConstraint(AllDifferentConstraint(), [var, adjacent])
            return allocation
        return None

    def _spill(self, vertex = None):
        """
        Finds the vertex with the highest output degree
        and spill it out to the memory

        :param vertex: Vertex to be removed from the graph
        """
        if not vertex:
            vertex = self.graph.maxOutputDegree()
        self.spilledOut.append(vertex)
        self.graph.removeVertex(vertex)

    def _preSpill(self):
        """
        Executes an pre spill process searching for vertex which
        the output degree is to high to have an possible allocation
        then executes the spill process on then
        """
        outputDegrees = self.graph.allGraphOutputDegrees()
        for vertex in outputDegrees:
            if outputDegrees[vertex] > self.registers:
                self._spill(vertex)


import cv2
import numpy

#rgb分量
fn='timg.jpeg'
image1=cv2.imread(fn)
image=numpy.mat
bgrs=numpy.mat
hsv=numpy.mat
hsvs=numpy.mat
image=cv2.pyrDown(image1)
bgrs=cv2.split(image)
cv2.imshow('source image',image)
cv2.imshow('b',bgrs[0])
cv2.imshow('g',bgrs[1])
cv2.imshow('r',bgrs[2])
cv2.waitKey()

#hsv分量
hsv=cv2.cvtColor(image1,cv2.COLOR_BGR2HSV)
hsvs=cv2.split(hsv)
cv2.imshow('Hue',hsvs[0])
cv2.imshow('Saturation',hsvs[1])
cv2.imshow('Value',hsvs[2])
cv2.waitKey()


import cerebrate_config as cc
from definitions import Command
from utilities import aprint
import cerebratesinfo, communication, mysysteminfo
import socket, struct

_commands = {
    '_message_': {Command.NAME: 'Message', Command.DESCRIPTION: 'Communicates a message to intended cerebrate(s) through text and audio, if possible.', Command.USE: 'message [target(s)][message]', Command.FUNCTION: "send_message"},
    '_wake': {Command.NAME: 'Wake Cerebrate', Command.DESCRIPTION: 'Wake up intended cerebrate(s), if possible.', Command.USE: 'wake [cerebrate specifier(s)]', Command.FUNCTION: "wake"}
}

async def send_message(msg):
    data = msg.data
    recipients = []
    for cerebrate in cerebratesinfo.get_cerebrate_records():
        name = cerebrate.get(cerebratesinfo.Record.NAME, "")
        if name in data:
            mac = cerebrate.get(cerebratesinfo.Record.MAC, "")
            if not mac in recipients:
                recipients.append(mac)
                communication.distill_msg(msg, name)
    for cerebrate in cerebratesinfo.get_cerebrate_records():
        location = cerebrate.get(cerebratesinfo.Record.LOCATION, "")
        if location in data:
            mac = cerebrate.get(cerebratesinfo.Record.MAC, "")
            if not mac in recipients:
                recipients.append(mac)
                communication.distill_msg(msg, location)
    if recipients.__len__() <= 0:
        recipients = cerebratesinfo.get_cerebrate_macs()
        recipients.remove(mysysteminfo.get_mac_address())
    for recipient in recipients:
        await communication.Secretary.communicate_message(cerebrate_mac=recipient, msg=communication.Message('display_message', data=msg.data))
    # speak msg
    #aprint("Message communicated")
    return True

def wake_cerebrate(cerebrate_mac):
    '''Attempts to wake given cerebrate.
    Target system must have wake on magic packet enabled.
    Raises a ValueError if the given mac is unrecognizable.
    '''
    # Check macaddress format and try to compensate.
    cerebrate_name = cerebratesinfo.get_cerebrate_attribute(cerebrate_mac=cerebrate_mac, record_attribute=cerebratesinfo.Record.NAME)
    if len(cerebrate_mac) == 12:
        pass
    elif len(cerebrate_mac) == 12 + 5:
        sep = cerebrate_mac[2]
        cerebrate_mac = cerebrate_mac.replace(sep, '')
    else:
        raise ValueError('Incorrect MAC address format')

    # Pad the synchronization stream.
    data = ''.join(['FFFFFFFFFFFF', cerebrate_mac * 20])
    send_data = b''

    # Split up the hex values and pack.
    for i in range(0, len(data), 2):
        send_data = b''.join([send_data, struct.pack('B', int(data[i: i + 2], 16))])

    # Broadcast it to the LAN.
    # This is done manually here because the communication module pickles the data, so won't work for this.
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    sock.sendto(send_data, ('<broadcast>', 7))
    if cc.feedback_on_commands():
        print(cerebrate_name + " prodded.")
    return True

async def wake(msg):
    '''Tries to wake a cerebrate based on given name or location
    '''
    for cerebrate in cerebratesinfo.get_cerebrate_records():
        name_matched = cerebrate.get(cerebratesinfo.Record.NAME, "unknown").lower() in msg.data.lower()
        location_matched = cerebrate.get(cerebratesinfo.Record.LOCATION, "unknown").lower() in msg.data.lower()
        if name_matched or location_matched:
            return wake_cerebrate(cerebrate.get(cerebratesinfo.Record.MAC, None))

    # for name in cerebratesinfo.get_cerebrate_names():
    #     if name.lower() in msg.data.lower():
    #         record = cerebratesinfo.get_cerebrate_record(record_attribute=cerebratesinfo.Record.NAME, attribute_value=name)
    #         if record != None:
    #             return wake_cerebrate(record.get(cerebratesinfo.Record.MAC))
    # for location in cerebratesinfo.get_cerebrate_locations():
    #     if location.lower() in msg.data.lower():
    #         record = cerebratesinfo.get_cerebrate_record(record_attribute=cerebratesinfo.Record.LOCATION, attribute_value=location)
    #         if record != None:
    #             return wake_cerebrate(record.get(cerebratesinfo.Record.MAC))
    #aprint("No target recognized in given wake command\n'", msg.header, msg.data, "'")
    return False


from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import os

from ansible import constants as C
from ansible.errors import AnsibleError
from ansible.inventory.host import Host
from ansible.module_utils._text import to_bytes
from ansible.plugins.loader import vars_loader
from ansible.utils.collection_loader import AnsibleCollectionRef
from ansible.utils.display import Display
from ansible.utils.vars import combine_vars

display = Display()

def get_plugin_vars(loader, plugin, path, entities):

    data = {}
    try:
        data = plugin.get_vars(loader, path, entities)
    except AttributeError:
        try:
            for entity in entities:
                if isinstance(entity, Host):
                    data.update(plugin.get_host_vars(entity.name))
                else:
                    data.update(plugin.get_group_vars(entity.name))
        except AttributeError:
            if hasattr(plugin, 'run'):
                raise AnsibleError("Cannot use v1 type vars plugin %s from %s" % (plugin._load_name, plugin._original_path))
            else:
                raise AnsibleError("Invalid vars plugin %s from %s" % (plugin._load_name, plugin._original_path))
    return data

def get_vars_from_path(loader, path, entities, stage):

    data = {}

    vars_plugin_list = list(vars_loader.all())
    for plugin_name in C.VARIABLE_PLUGINS_ENABLED:
        if AnsibleCollectionRef.is_valid_fqcr(plugin_name):
            vars_plugin = vars_loader.get(plugin_name)
            if vars_plugin is None:
                # Error if there's no play directory or the name is wrong?
                continue
            if vars_plugin not in vars_plugin_list:
                vars_plugin_list.append(vars_plugin)

    for plugin in vars_plugin_list:
        if plugin._load_name not in C.VARIABLE_PLUGINS_ENABLED and getattr(plugin, 'REQUIRES_WHITELIST', False):
            # 2.x plugins shipped with ansible should require whitelisting, older or non shipped should load automatically
            continue

        has_stage = hasattr(plugin, 'get_option') and plugin.has_option('stage')

        # if a plugin-specific setting has not been provided, use the global setting
        # older/non shipped plugins that don't support the plugin-specific setting should also use the global setting
        use_global = (has_stage and plugin.get_option('stage') is None) or not has_stage

        if use_global:
            if C.RUN_VARS_PLUGINS == 'demand' and stage == 'inventory':
                continue
            elif C.RUN_VARS_PLUGINS == 'start' and stage == 'task':
                continue
        elif has_stage and plugin.get_option('stage') not in ('all', stage):
            continue

        data = combine_vars(data, get_plugin_vars(loader, plugin, path, entities))

    return data

def get_vars_from_inventory_sources(loader, sources, entities, stage):

    data = {}
    for path in sources:

        if path is None:
            continue
        if ',' in path and not os.path.exists(path):  # skip host lists
            continue
        elif not os.path.isdir(to_bytes(path)):
            # always pass the directory of the inventory source file
            path = os.path.dirname(path)

        data = combine_vars(data, get_vars_from_path(loader, path, entities, stage))

    return data


import cv2
import os
import argparse
import sys
import shutil
from tqdm import tqdm

parser = argparse.ArgumentParser(description='Convert video to videos of smaller length')
parser.add_argument('-i', type=str, help='input video path')
parser.add_argument('-o', type=str, help='output FOLDER path')
parser.add_argument('-f', type=str, help='output videos format (such as mp4/avi)')
parser.add_argument('-p', type=int, help='small videos of how many SECONDS')
parser.add_argument('-s', type=tuple, default=(0, 0), help='[OPTIONAL] resize output videos frames')

args = parser.parse_args()

def main(inpVideoPath, outFolderPath, outFormat, seconds, resize):
    print(inpVideoPath)
    cap = cv2.VideoCapture(inpVideoPath)
    if not cap.isOpened():
        print("could not open :", inpVideoPath)
        sys.exit()

    numFrames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fps = int(cap.get(cv2.CAP_PROP_FPS))

    NUM_OUTPUT_VIDEOS = int(numFrames / (fps * seconds))
    FRAMES_PER_VIDEO = fps * seconds
    OUTPUT_VIDEO_SIZE = resize if resize[0] != 0 else (width, height)
    print('[video loaded succesfully]')

    if os.path.exists(outFolderPath):
        shutil.rmtree(outFolderPath)

    outs = {}

    os.mkdir(outFolderPath)
    print("NUM_OUTPUT_VIDEOS", NUM_OUTPUT_VIDEOS)
    for ii in range(NUM_OUTPUT_VIDEOS):
        # os.mkdir( os.path.join(outFolderPath,str(i)) )
        outs[ii] = cv2.VideoWriter(os.path.join(outFolderPath, str(ii) + '.' + outFormat),
                                   cv2.VideoWriter_fourcc(*'DIVX'), fps, frameSize=OUTPUT_VIDEO_SIZE)
    print('[output folder & subfolders created]')

    j = 0

    print('[saving videos]')
    for i in tqdm(range(numFrames)):

        if i % FRAMES_PER_VIDEO == 0 and i != 0:
            outs[j].release()
            j += 1
            if j == NUM_OUTPUT_VIDEOS:
                j -= 1

        ret, frame = cap.read()
        frame = cv2.resize(frame, OUTPUT_VIDEO_SIZE)

        outs[j].write(frame)

    print('[done!]')

if __name__ == '__main__':
    main(args.i, args.o, args.f, args.p, args.s)


#!/usr/bin/python3
""" this is for init a class square"""

class Square():
    """this class is a square
    Args: None
    Attributes:
        size (int):is a integer.
    """
    def __init__(self, size=0):
        """ this for init fuction
        Args:
            size(int)
        """
        if (type(size) == int):
            if (int(size) >= 0):
                self.__size = int(size)
            else:
                raise ValueError('size must be >= 0')
        else:
            raise TypeError('size must be an integer')


tests  = ["test1", 2, "test3", "test4", "test5", "test6"]

for i in tests:
    print("The element is : " + str(i) )
    print(type(i))

even = 0

for x in range(0,100):
    if x%2 == 0:
        even = even + 1

print(even)

# For 1st loop
# i in tests (i = test1)
# print(i) ==> print(test1)

# For 2nd Loop
# i in tests (i = 2 )
# print(i) ==> print(test2)
# type(i) --> int


from PyQt4 import QtGui
from PyQt4.QtCore import pyqtSlot

from views.core import centraltabwidget
from db.windows.wellplot.wellplotdata.wellplotdata import WellPlotData
from gui.wellplot.toolbar import logsettingstoolbar
from gui.signals.wellplotsignals import WellPlotSignals
from gui.wellplot.wellplotdialog.uiwellplotpg import UIWellPlotPG
from gui.wellplot.wellplotdialog.headerviewer import HeaderViewer
from gui.wellplot.model.wellplotmodelaccess import WellPlotModelAccess

import logging
from gui.wellplot.wellplotdialog.trackviewer import TrackViewer

from gui.wellplot.overview.overviewwidget import OverviewWidget
from gui.util.qt.widgetutils import WidgetUtils

logger = logging.getLogger('console')

class WellPlotPG(UIWellPlotPG):
    '''Main well plot class, extends  UIWellPlotPG'''

    def __init__(self, logs, well, template, wellPlotData = None, logSet = None, parent = None):
        '''Logs are a selected logs that are plotting
        All logs can be accessed via '''
        super(WellPlotPG, self).__init__(parent)
        if (logs is not None) and (well is not None) and (template is not None):

            self._well = well
            self._logSet = logSet
            self._logs = logs
            self._template = template
            self.trackViewer = None
            self.headerViewer = None
            self.overviewWidget = None
            self.canvas = None
            self.depthPlot = None
            self.headerPlot = None
            self.centralWidget = centraltabwidget.CentralTabWidget()
            #id(self) returns the 'hash' of this object
            self.uid = (self.centralWidget.count(), id(self))
            self.wellPlotSignals = WellPlotSignals()
            self.setupUI()
            self.createTabView()
            self.connectSlots()
            if wellPlotData is None:
                self.wellPlotData = self.generateInitialData()
            else:
                self.wellPlotData = wellPlotData

            self.plotMultiLogs(self.wellPlotData)

            self.createToolWidget()
        else:
            logger.error("Can't open well plot, input data is None: logs:{0}, well:{1}, template:{2}".format(logs, well, template))

    def generateInitialData(self):
        wellPlotModelAccess = WellPlotModelAccess()
        wellPlotData = wellPlotModelAccess.createWellPlotData(self._logs, self.uid, self._well, self._template, self._logSet)
        return wellPlotData

    def plotMultiLogs(self, wellPlotData):
        if (wellPlotData != None):
            self.clearWidgets(wellPlotData)

            self.mainWidget.setLogPlotData(wellPlotData)
            self.trackViewer = TrackViewer(wellPlotData, parent = self.scrollArea)
            self.scrollArea.setWidget(self.trackViewer)
            self.headerViewer = HeaderViewer(wellPlotData = wellPlotData, logTracks = self.trackViewer.allTracks)
            self.headerScrollArea.setWidget(self.headerViewer)

            self.setSplitterStretch()
            #create overview here so is recreated on redraw signal
            self.createOverviewWidget()

    def clearWidgets(self, wellPlotData):
        '''delete everything if existing'''
        if self.trackViewer is not None:
            WidgetUtils.removeWidgets(self.scrollArea.layout())
            self.trackViewer = None
            self.wellPlotData = wellPlotData

        if self.headerViewer is not None:
            WidgetUtils.removeWidgets(self.headerScrollArea.layout())
            self.headerViewer = None

        if (self.overviewWidget is not None) and (self.scaleWidgetLayout is not None):
            WidgetUtils.removeWidgets(self.scaleWidgetLayout)
            self.overviewWidget = None

    def setSplitterStretch(self):
        #totalWidth = self.trackViewer.getMinWidgetWidth()
        trackHeight = self.trackViewer.geometry().height()
        headerHeight = self.headerViewer.geometry().height()
        #self.trackViewer.setMinimumSize(totalWidth, trackHeight)
        if headerHeight > 0:
            ratio = int((trackHeight+headerHeight)/headerHeight)
        else:
            ratio = 10
        logger.debug("trackHeight:{0} headerHeight:{1} ratio:{2}".format(trackHeight,headerHeight, ratio))
        self.splitter.setStretchFactor(1, ratio)

    def connectSlots(self):
        logger.debug(">>connectSlots")
        self.wellPlotSignals.logPlotSettingsModified.connect(self.replotLogs)

    @pyqtSlot(WellPlotData)
    def replotLogs(self, wellPlotData):
        #check uid's before accessing them, where uid is a (number widgets in central widget, id) tuple
        logger.debug("--replotLogs() len(self.uid):{0}, len(logPlotData.uid):{1}".format(len(self.uid), len(wellPlotData.uid)))
        if (len(self.uid) == 2) and (len(wellPlotData.uid) == 2):
            #ensure this object is associated with the plot object
            if self.uid[0] == wellPlotData.uid[0] and self.uid[1] == wellPlotData.uid[1]:
                logger.debug("--replotLogs() match found uid: "+str(self.uid[0]))
                self.plotMultiLogs(wellPlotData)

    def spacePlots(self, bottomLayout):
        rightSpacer = QtGui.QWidget()
        rightSpacer.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        #topLayout.addWidget(rightSpacer)
        bottomLayout.addWidget(rightSpacer)
        #self.topWidget.setLayout(topLayout)
        self.dataWidget.setLayout(bottomLayout)

    def createTabView(self):
        self.mainWidget.setData(self.uid)
        self.centralWidget.addTab(self.mainWidget, "Well plot "+str(self.uid[0]))

    def createToolWidget(self):
        logger.debug(">>createToolWidget()")
        if len(self._logs) > 0:
            toolbar = logsettingstoolbar.LogSettingsToolbar()
            #toolbar.setData(self._well, self._logSet, self.canvas, self.depthPlot, self.headerPlot)
            toolbar.emitShowToolbarSignal()
            logger.debug("<<createToolWidget() toolbar created")

    def createOverviewWidget(self):
        #TODO find longest GR track and use as default
        #for logTrackData in self.wellPlotData.log_track_datas:
        #   logTrackData.getLogs()
        #also for Active logs raises index error
        logTrackDatas= self.wellPlotData.getLogTrackDatas()
        if len(logTrackDatas) > 0:
            logTrackData = logTrackDatas[0]
            logger.debug("--createOverviewWidget() self.wellPlotData.uid:{0}".format(self.wellPlotData.uid))
            self.overviewWidget = OverviewWidget(self.wellPlotData, self.trackViewer, wellPlotPGSplitter = self.splitter)
            #revise these 3 lines to log want to plot and data range
            log = logTrackData.getLogs()[0]
            yData = log.z_measure_data
            xData = log.log_data

            self.overviewWidget.generatePlot(logTrackData, log, xData, yData)
            self.scaleWidgetLayout.addWidget(self.overviewWidget)
            #scaleWidget.primaryDomainWidget = self.trackViewer.primaryDomainTrackWidget
        else:
            logger.debug("No log track data, cannot create scale widget, ")


#!/usr/bin/env python3
import sys
import os

def rename_all(target_directory):
    count = 0
    for path, dirs, files in os.walk(target_directory):
        for filename in files:
            if "female" in filename:
                new_filename = filename.replace("female", "male", 1)
            else:
                new_filename = filename.replace("male", "female", 1)

            if filename != new_filename:
                os.rename(path + "/" + filename, path + "/" + new_filename)
                count += 1

    print("Finished! Renamed", count, "files.")

if len(sys.argv) == 1:
    no_args_prompt = input("Would you like to rename the files in this directory? (y/n): ")
    if no_args_prompt.lower() != 'y':
        print("Did nothing.")
        exit()
    else:
        rename_all(os.getcwd())
elif len(sys.argv) == 2:
    if os.path.isdir(sys.argv[1]):
        rename_all(sys.argv[1])
    else:
        print("Error: " + sys.argv[1] + " is not a valid directory.")
else:
    print("Too many arguments. Got:", sys.argv)


from sklearn.linear_model.base import LinearRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.preprocessing.data import StandardScaler, PolynomialFeatures
from sklearn.svm import OneClassSVM
from sklearn.cross_validation import cross_val_predict
from sklearn import cross_validation
from sklearn import tree
from sklearn import metrics
from sklearn import ensemble
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.svm.classes import SVR

import data_preprocess
import metadata
import logging
from clusterization_model import ClusterizationModel
from sklearn import preprocessing as preproc

np.set_printoptions(precision=5, suppress=True)  # suppress scientific float notation

# reload(logging)
logging.basicConfig(format='%(levelname)s %(asctime)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', level=logging.DEBUG)
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

def remove_columns(df, columns):
    new_columns = list(filter(lambda x: x not in columns, df.columns))
    return df[new_columns]

def get_columns(df, columns, get_all_match=True):
    new_columns = []
    for column in columns:
        all_match = list(filter(lambda x: column in x, df.columns))
        if get_all_match or column not in df.columns:
            new_columns += all_match
        else:
            new_columns.append(column)
    return new_columns

def plot_statistic(statistic, title, names=None, conf_interv=None):
    fig, ax = plt.subplots()
    index = np.arange(len(statistic))
    plt.bar(index, statistic, yerr=conf_interv, color='b', alpha=0.7)
    if names is not None:
        plt.xticks(index + 0.4, names)
    # plt.xlabel("Stocks")
    plt.ylabel(title)
    plt.show()

data = data_preprocess.get_data(calc=True)

X = data.main_data_frame

def remove_outliers(data):
    clf = OneClassSVM(nu=0.2, kernel="rbf", gamma=0.00001)
    clf.fit(data)
    logging.info("%s outliers removed from %s elements" % ((clf.predict(data) == -1).sum(), len(data)))
    return data[clf.predict(data) == 1]

def clients_clusterization(X):
    clients_df = X[get_columns(X, ['stock_id', 'customer_id', 'quantity', 'customer_status', 'license_type', 'iso',
                                   'amount_in_usd', 'GDP', 'HDI', 'population', 'urban', 'avg_income',
                                   'discount_perc'])]
    clients_df.reset_index(inplace=True)

    clients_loyalty = clients_df.groupby('customer_id')[['customer_id']].mean()
    clients_loyalty['loyalty'] = 0
    clients_loyalty['Renew loyalty'] = 0
    del clients_loyalty['customer_id']
    stock_quantity_columns = []
    for stock_id in metadata.stock_ids:
        stock_name = metadata.stock_ids[stock_id][0]
        if stock_name == "None":
            continue
        add_N = clients_df[clients_df['stock_id'] == stock_id].groupby('customer_id')['quantity'].sum()
        add_Renew = clients_df[(clients_df['stock_id'] == stock_id) &
                               (clients_df['license_type'] == 2)].groupby('customer_id')['quantity'].count()
        if len(add_N) == 0:
            continue
        col_name = stock_name + '_quantity'
        stock_quantity_columns.append(col_name)
        add_N_df = add_N.to_frame(col_name)
        clients_loyalty = pd.merge(clients_loyalty, add_N_df, how='left', left_index=True, right_index=True)
        clients_loyalty[col_name] = np.nan_to_num(clients_loyalty[col_name])

        add_N2 = add_N * add_N
        add_N2_df = add_N2.to_frame('loyalty')
        add_Renew2 = add_Renew * add_Renew
        add_Renew2_df = add_Renew2.to_frame('Renew loyalty')
        clients_loyalty = pd.merge(clients_loyalty, add_N2_df, how='left', left_index=True, right_index=True)
        clients_loyalty['loyalty'] = clients_loyalty['loyalty_x'] + np.nan_to_num(clients_loyalty['loyalty_y'])
        clients_loyalty = pd.merge(clients_loyalty, add_Renew2_df, how='left', left_index=True, right_index=True)
        clients_loyalty['Renew loyalty'] = clients_loyalty['Renew loyalty_x'] + np.nan_to_num(
            clients_loyalty['Renew loyalty_y'])
        del clients_loyalty['loyalty_x']
        del clients_loyalty['loyalty_y']
        del clients_loyalty['Renew loyalty_x']
        del clients_loyalty['Renew loyalty_y']

    clients_loyalty['loyalty'] **= 0.5
    clients_loyalty['Renew loyalty'] **= 0.5

    clients_df = pd.merge(clients_df, clients_loyalty, how='left', left_on='customer_id', right_index=True)

    def calc_feature(df_from, df_to, base_param_name, new_param_name, group_id='customer_id'):
        df_from = df_from.groupby(group_id).sum().reset_index()
        df_from[new_param_name] = df_from[base_param_name]
        result = pd.merge(df_to, df_from[[group_id, new_param_name]], how='left', on=group_id)
        result[new_param_name] = np.nan_to_num(result[new_param_name])
        return result

    clients_new_pursh = clients_df[['customer_id', 'quantity', 'license_type']].loc[clients_df['license_type'] == 0]
    clients_df = calc_feature(clients_new_pursh, clients_df, 'quantity', 'Products number')

    clients_df = calc_feature(clients_df[['customer_id', 'amount_in_usd']], clients_df, 'amount_in_usd', 'USD_total')

    clients_sale_filtered = clients_df[['customer_id', 'amount_in_usd']][clients_df['discount_perc'] > 10]
    clients_df = calc_feature(clients_sale_filtered, clients_df, 'amount_in_usd', 'Dicsount_amount_USD')

    clients_df['Sale part'] = clients_df['Dicsount_amount_USD'] / clients_df['USD_total']

    old_columns = ['GDP', 'HDI', 'population', 'urban', 'avg_income']
    chosen_columns = ['customer_id', 'loyalty', 'Products number', 'USD_total', 'Renew loyalty',
                      'Sale part'] + old_columns  # + stock_quantity_columns
    clients_df = clients_df[chosen_columns]
    print(clients_df.corr())

    clients_df = clients_df.groupby('customer_id').mean().reset_index()
    print(clients_df)
    # drop few outliers, which have been found by hierarchy clusterization
    drop_index = clients_df['customer_id'].apply(
        lambda x: x not in [10402, 10518, 3513, 3795, 5632, 6372, 8325, 1031, 3384,
                            5586, 6308, 8249, 10100, 2857, 5555, 6265, 8183, 10009, 2850, 907, 1006, 37, 904])
    clients_df = clients_df.drop(drop_index)

    columns_without_id = list(clients_df.columns)
    columns_without_id.remove('customer_id')
    clients = clients_df[columns_without_id]

    n_clusters = 4
    # f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex='col', sharey='row')
    # clustering_model = ClusterizationModel(model="agglomerative").fit(clients)
    # clustering_model.draw_clusters(method="", axis=ax1, show=False)
    # clustering_model = ClusterizationModel(model="dbscan", eps=5e4, min_samples=1).fit(clients)
    # clustering_model.draw_clusters(method="", axis=ax2, show=False)
    clustering_model = ClusterizationModel(n_clusters=n_clusters, model="KMeans").fit(clients)
    clustering_model.draw_clusters(method="", show=True)
    # clustering_model = ClusterizationModel(model="hierarchy").fit(clients)
    # clustering_model.draw_clusters(method="dendrogram", show=True)

    mean_values = clustering_model.get_mean_values()
    mean_values = mean_values.sort_values(by='loyalty', ascending=[1])

    labels = clustering_model.get_labels()
    clients_df['label'] = pd.Series(labels)
    return mean_values, clients_df

mean_values, clients_df = clients_clusterization(X)
print(mean_values)

resellers_df = X[['reseller_id', 'reseller_volume', 'reseller_discount', 'amount_in_usd', 'customer_id']]
clusters_labels = list(clients_df['label'].unique())
clients_labels = clients_df[['customer_id', 'label']]
clients_labels.columns = ['customer_id', 'client_label']
resellers_grouped_df_mean = resellers_df.groupby('reseller_id').mean().reset_index()
resellers_df = pd.merge(resellers_df, clients_labels, on='customer_id')
resellers_df = data_preprocess.preprocess_categorical(resellers_df, ['client_label'])

clients_labels_full = list(filter(lambda x: 'client_label' in x, resellers_df.columns))
resellers_grouped_df_sum = resellers_df[['reseller_id', 'amount_in_usd'] + clients_labels_full].groupby(
    'reseller_id').sum().reset_index()
resellers_grouped_df = pd.merge(resellers_grouped_df_mean, resellers_grouped_df_sum, on='reseller_id')

resellers_grouped_clean_df = remove_columns(resellers_grouped_df, ['reseller_id', 'customer_id'])
n_clusters = 5
# f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex='col', sharey='row')
# cluster_reseller_model = ClusterizationModel(model="agglomerative").fit(resellers_grouped_clean_df)
# cluster_reseller_model.draw_clusters(method="", axis=ax1, show=False)
# cluster_reseller_model = ClusterizationModel(model="dbscan", eps=5e4, min_samples=1).fit(resellers_grouped_clean_df)
# cluster_reseller_model.draw_clusters(method="", axis=ax2, show=False)
cluster_reseller_model = ClusterizationModel(n_clusters=n_clusters, model="KMeans").fit(resellers_grouped_clean_df)
cluster_reseller_model.draw_clusters(method="", show=True)
# cluster_reseller_model = ClusterizationModel(model="hierarchy").fit(resellers_grouped_clean_df)
# cluster_reseller_model.draw_clusters(method="dendrogram", show=True)
print(cluster_reseller_model.get_mean_values())

print(resellers_grouped_clean_df.corr())

# plot resellers volume, grouped by discount
discount_groups = resellers_grouped_df[['reseller_volume', 'reseller_discount']].groupby('reseller_discount').agg(
    ['mean', 'count'])
discount_groups['reseller_volume'].plot(kind='bar', subplots=True)
plt.ylabel('reseller_volume')
plt.show()

resellers_grouped_df['reseller_cluster'] = pd.Series(cluster_reseller_model.get_labels())
X = pd.merge(X, resellers_grouped_df[['reseller_id', 'reseller_cluster'] + clients_labels_full], how='left',
             on='reseller_id')

import statsmodels.api as sm
from scipy import stats
reseller_clusters = list(X['reseller_cluster'].unique())
for cluster in reseller_clusters:
    resellers_df = X[['reseller_cluster', 'amount_in_usd', 'placed_date']][X['reseller_cluster'] == cluster]
    resellers_df['placed_date'] = pd.to_datetime(resellers_df['placed_date'])
    resellers_df = resellers_df[(resellers_df['placed_date'] >= '2012-01-01') & (resellers_df['placed_date'] < '2016-01-01')]
    resellers_df = resellers_df.set_index(resellers_df['placed_date'])
    resellers_df = resellers_df.sort_index()
    resellers_df = resellers_df[['amount_in_usd']]
    resellers_df = resellers_df.groupby(pd.TimeGrouper(freq='W')).sum()
    resellers_df.loc[pd.to_datetime('2014-12-31')] = float(np.nan)
    resellers_df.loc[pd.to_datetime('2015-12-31')] = float(np.nan)
    resellers_df['amount_in_usd'].interpolate(method="values", inplace=True)
    time_series_df = pd.DataFrame(resellers_df[['amount_in_usd']])
    time_series_df.plot(figsize=(15,5))
    plt.show()
    # fig = plt.figure(figsize=(12, 8))
    # ax1 = fig.add_subplot(211)
    # fig = sm.graphics.tsa.plot_acf(resellers_df.values.squeeze(), lags=48, ax=ax1)
    # ax2 = fig.add_subplot(212)
    # fig = sm.graphics.tsa.plot_pacf(resellers_df, lags=48, ax=ax2)
    # arma_mod30 = sm.tsa.ARMA(resellers_df, (12, 3)).fit()
    # print(arma_mod30.params)
    # fig, ax = plt.subplots(figsize=(12, 8))
    # ax = resellers_df.ix['2012':].plot(ax=ax)
    # fig = arma_mod30.plot_predict('2014', '2015', dynamic=True, ax=ax, plot_insample=False)


import numpy as np
import pandas as pd
from scipy.integrate import ode

# Численный интегратор
def nintegrate(F, y0, t0, tk, steps):
    intgr = ode(F).set_integrator('Dopri5') # Рунге-Кутта 4-5
    intgr.set_initial_value(y0, t0)
    time_range = np.linspace(t0, tk, steps)
    dt = (tk - t0)/(steps - 1)
    # Интегрируем. Он каждый шажок делает отдельно
    result_temp = np.array([np.append(np.array([t0]), y0)])
    for i in range(steps):
        t_curr = intgr.t
        y_curr = intgr.integrate(intgr.t+dt)
        row_curr = [np.append(np.array([t_curr]), y_curr)]
        result_temp = np.append(result_temp, row_curr, axis=0)
    result_np = np.array(result_temp)
    return result_np

# Функция, делающая по векторам ГУ слева и справа матрицу ГУ слева для метода начальных параметров
def Y0_create(y_begin_, y_end_):
    y_begin = np.array(y_begin_)
    y_end = np.array(y_end_)
    dim = len(y_begin)
    # Индексы тех элементов, которые мы задали как известные
    begin_ind = np.where(~np.isnan(y_begin))[0]
    end_ind = np.where(~np.isnan(y_end))[0]
    # И тех, которые задали неизвестными
    begin_nan_ind = np.where(np.isnan(y_begin))[0]
    end_nan_ind = np.where(np.isnan(y_end))[0]
    # Количество заданных ГУ слева и справа
    begin_num = begin_ind.size
    end_num = end_ind.size
    # Создаем матрицу
    Y0 = np.zeros((dim, end_num + 1))
    Y0[:, end_num][begin_ind] = y_begin[begin_ind] # Это для частного неоднородного, y0
    for i in range(end_num):
        Y0[:, i][begin_nan_ind[i]] = 1
    return Y0

# Функция, которая принимает Y-матрицу в начале участка и возвращает в конце
# Y-матрица в numpy-виде
def Y_nintegrate(F_, F_plus_g_, Y0_, t0_, tk_, steps_):
    dim_n = Y0_.shape[0]     # Размерность вектора состояния, т.е. высота матрицы
    dim_r = Y0_.shape[1] - 1 # Количество решений однородного, т.е. ширина матрицы - 1
    Yk = np.copy(Y0_)
    Yk_full = []
    for i in range(dim_r):
        Yk_full.append(nintegrate(F_, Y0_[:, i], t0_, tk_, steps_))
        Yk[:, i] = Yk_full[i][steps_, 1:]
    Yk_full.append(nintegrate(F_plus_g_, Y0_[:, dim_r], t0_, tk_, steps_))
    Yk[:, dim_r] = Yk_full[dim_r][steps_, 1:]
    return [Yk, Yk_full]

def set_Wij(Y_, W_, Z_, i_, j_):
    r = Y_.shape[1]
    i = i_
    j = j_
    if (i == j):
        if (i == r - 1):
            W_[i, j] = 1
        else:
            W_[i, j] = np.sqrt(Y_[:, i].dot(Y_[:, i]) - np.sum(np.power(W_[:, i], 2)[range(i)]))
    else:
        W_[i, j] = Z_[:, i].dot(Y_[:, j])
    #print('set_Wij, i, j:', i, j, W_[i, j])

def set_Zi(Y_, W_, Z_, i_):
    r = Y_.shape[1]
    i = i_
    ZW_sum = 0.
    for j in range(i):
        ZW_sum += Z_[:, j]*W_[j, i]
    if (i == r - 1):
        Z_[:, i] = (Y_[:, i] - ZW_sum)
    else:
        Z_[:, i] = 1/W_[i, i] * (Y_[:, i] - ZW_sum)
    #print('set_Zi, i:', i)

# Функия, формирующая саму новую ортогонализованную матрицу
# И омега-матрицу для оротогонализации
def orthogonalization(Y_):
    dim_n = Y_.shape[0]     # Размерность вектора состояния, т.е. высота матрицы
    dim_r = Y_.shape[1]     # Количество векторов однородного, т.е. ширина матрицы - 1
    W = np.zeros((dim_r, dim_r))
    Z = np.zeros((dim_n, dim_r))
    for i in range(dim_r):
        for j in range(i+1):
            set_Wij(Y_, W, Z, j, i)
        set_Zi(Y_, W, Z, i)
    return [Z, W]

# Функция, которая решает СЛАУ и находит вектор констант
# C_vec = [C1, C2, ..., Cr, 1]
def solve_c0_vec(Y_, y_end_):
    # Индексы тех элементов, которые мы задали как известные
    end_ind = np.where(~np.isnan(y_end_))[0]
    # И тех, которые задали неизвестными
    end_nan_ind = np.where(np.isnan(y_end_))[0]
    # Количество заданных ГУ справа
    end_num = end_ind.size
    # Теперь составляем слау и находим константы
    # y0k + Y.dot(c_vec) = y_end -> Y.dot(c_vec) = (y_end - y0k)
    Y_matr = Y_[:, :-1][end_ind]
    y0k = Y_[:, -1]
    r_vec = y_end_[end_ind] - y0k[end_ind]
    c_vec = np.linalg.solve(Y_matr, r_vec)
    c_vec_full = np.append(c_vec, 1.)
    return c_vec_full

def solve_ci_vec(Wi, c_prev):
    dim_r = c_prev.shape[0] - 1
    c_i = np.zeros(dim_r + 1)
    for i in range(dim_r + 1):
        c_sum = 0
        for j in range(i):
            c_sum += Wi[-1-i, -1-(i-j-1)]*c_i[-1-(i-j-1)]
        c_i[-1-i] = 1/Wi[-1-i, -1-i] * (c_prev[-1-i] - c_sum)
    return c_i

# Функция, собирающая нормальную функцию y
# из Y_full_list и c_list
# Нужна для Годунова, см. следующую функцию
# Возвращает красивый pandas.DataFrame
def compile_y_func(Y_full_list, c_list_):
    c_list = c_list_.copy()
    c_list.reverse() # Он был задом наперед
    num_of_parts = len(c_list)
    num_of_c = c_list[0].shape[0] # Сколько констант в одном векторе из c_list
    num_of_steps = Y_full_list[0][0].shape[0] # Сколько шагов интегрирования в каждом part
    num_of_func = Y_full_list[0][0].shape[1]-1 # Размерноесть вектора состояния, -1 ибо один столбец t
    # Имена для столбцов функций в DataFrame
    names = ['t']
    for i in range(num_of_func):
        names.append('y' + str(i))
    # Лист с DataFrame каждого part
    y_df_list = []
    for i in range(num_of_parts):
        time_vec_curr = Y_full_list[i][0][:,0]
        y_data_curr = np.zeros((num_of_steps, num_of_func))
        for j in range(num_of_c):
            y_data_curr += Y_full_list[i][j][:,1:] * c_list[i][j]
        y_data_curr_with_t = np.append(time_vec_curr.reshape((time_vec_curr.size, 1)), y_data_curr, axis=1)
        y_df_curr = pd.DataFrame(y_data_curr_with_t, columns=names)
        y_df_list.append(y_df_curr)
    # И лепим все фреймы в первый
    for i in range(1, len(y_df_list)):
        y_df_list[0] = y_df_list[0].append(y_df_list[i])
    return y_df_list[0].drop_duplicates('t')

# Собственно ортогонализация и есть
# Возвращает DataFrame с приблизительным решением
def godunov_orthogonalization_solve(F_, F_plus_g_, y_begin_, y_end_, t0_, tk_, parts_, steps_):
    y_begin = np.array(y_begin_)
    y_end = np.array(y_end_)
    t_range = np.linspace(t0_, tk_, num=parts_+1)
    #print('t_range:', t_range)
    Y0 = Y0_create(y_begin, y_end)
    # Листы матриц решений и матриц ортогонализации
    Y_list = [Y0]
    W_list = []
    Y_full_list = []
    for i in range(parts_):
        print('Integrate part', i)
        Yi_temp, Yi_full_temp = Y_nintegrate(F_, F_plus_g_, Y_list[i], t_range[i], t_range[i+1], steps_)
        if (i != parts_ - 1):
            Yi, Wi = np.linalg.qr(Yi_temp)
            Yi[:, -1] = Yi[:, -1] * Wi[-1, -1]
            Wi[-1, -1] = 1.

            #Yi, Wi = orthogonalization(Yi_temp)
            Y_list.append(Yi)
            Y_full_list.append(Yi_full_temp)
            W_list.append(Wi)
        else:
            Y_list.append(Yi_temp)
            Y_full_list.append(Yi_full_temp)
    # Получаем лист векторов состояния по краям parts начиная с последнего
    print('Сalculate c_list')
    c_list = [solve_c0_vec(Y_list[-1], y_end)]
    for i in range(parts_ - 1):
        c_list.append(solve_ci_vec(W_list[-(1+i)], c_list[i]))
    # Теперь формируем по данным DataFrame, который и вернем
    print('Compile y_func')
    ret_res = compile_y_func(Y_full_list, c_list)
    return ret_res


from tkinter import *

root = Tk()
root.title('Sliders')
root.geometry('300x500')

def showVertical():
	lblShow = Label(root, text="vertical: " + str(vertical.get()))
	lblShow.pack()

def showHorizontal():
	lblShow = Label(root, text="horizontal: " + str(horizontal.get()))
	lblShow.pack()

vertical = Scale(root, from_=0, to=200)
vertical.pack()

horizontal = Scale(root, from_=0, to=200, orient=HORIZONTAL)
horizontal.pack()

btnShowVerticalSlider = Button(root, text='Show vertical value', command=showVertical)
btnShowVerticalSlider.pack()

btnShowHorizontalSlider = Button(root, text='Show horizontal value', command=showHorizontal)
btnShowHorizontalSlider.pack()

root.mainloop() #keep main window


from django.core.exceptions import ObjectDoesNotExist
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage, InvalidPage
from django.db.models import Sum

from .models import User, Transaction, Profile

import calendar

monthsConverter = {
    month: index for index, month in enumerate(calendar.month_abbr) if month
}

# Helper to paginate query sets passed from views.py
def paginate(queryset, page, per_page):
    paginator = Paginator(queryset, per_page)

    try:
        transactions = paginator.page(page)
    except PageNotAnInteger:
        transactions = paginator.page(1)
    except EmptyPage:
        transactions = paginator.page(paginator.num_pages)

    return transactions

def get_profile(user):
    try:
        profile = Profile.objects.get(user=user)
    except ObjectDoesNotExist:
        return 404

    return profile

def get_transactions(user):
    user_profile = get_profile(user)
    return user_profile.transactions.all()

def order_transactions_by(field, transactions_list):
    return transactions_list.order_by(f"-{field}")

def transactions_in(category, transactions_list):
    category = category.upper()
    return transactions_list.filter(category=category)

def transactions_in_month(month, transactions_list):
    monthNumber = monthsConverter[month]
    return transactions_list.filter(timestamp__month=monthNumber)

def sum_amounts_in_a_year(transactions_list):
    return transactions_list.aggregate(Sum("amount"))["amount__sum"]


#!/usr/bin/env python3

import os
import string
import sys
import re

if __name__ == '__main__':
    dir = sys.argv[1]
    core = []

    filenames = next(os.walk(dir), (None, None, []))[2]

    for filename in filenames:
        core.append(int(os.path.splitext(filename)[1][1:]))

    core.sort()

    print("#num_threads, fast, slow, tot ops/s")
    for i in core:
        file = "./"+dir+"/readrandom."+str(i)
        #  print(dir_path)
        stats = []
        with open(file) as f:
            lines = f.readlines()

            for line in lines:
                if line.startswith('readrandom'):
                    run = re.findall('[0-9.]+', line)
                    stats.append(run[0])

            if i == 1:
                print("{}, {}, {}, {}".format(i, stats[0], 0, stats[1]))
            else:
                print("{}, {}, {}, {}".format(i, stats[0], stats[1], stats[2]))


'''
Created on Sep 16, 2017

@author: anhvu
'''

ws = 0;
lo = 0;
up = 0;
sy = 0;
s = input();
l = len(s);
for c in s:
    if c == '_':
        ws += 1;
    elif ('a' <= c) & (c <= 'z'):
        lo += 1;
    elif ('A' <= c) & (c <= 'Z'):
        up += 1;
print(ws / l);
print(lo / l);
print(up / l);
print((l - ws - lo - up) / l);