import requests

async def getBalances(timestamp_start, period, address):
    timestamp_end = timestamp_start + 86400*period

    queryString = f"""query getDAO {{
        dailyBalances(orderBy: timestamp, where: {{address: "{address}", timestamp_gt: "{timestamp_start}", timestamp_lt: "{timestamp_end}"}}) {{
            ohmBalance
            day
        }}
    }}
    """

    request = requests.post('https://api.thegraph.com/subgraphs/name/deltax2016/olympus-wallets', json={'query': queryString})
    result = request.json()['data']

    array_balance = []
    for day in result['dailyBalances']:
        tempBalance = {}
        tempBalance['balance'] = int(day['ohmBalance']) / 1000000000
        tempBalance['timestamp'] = 1609459200 + 86400*int(day['day'])
        array_balance.append(tempBalance)

    return array_balance


#!/usr/bin/env python
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.progressbar import ProgressBar
import time, thread

class Tela(BoxLayout):
    def __init__(self, **kwargs):
        super(Tela, self).__init__(**kwargs)
        self.pb = ProgressBar(max=100)
        self.pb.value = 0  # 0%
        self.add_widget(self.pb)

    def progresso_barra(self):
        while True:
            if self.pb.value < 100:
                self.pb.value = self.pb.value + 1
                time.sleep(.1)
            else:
                self.pb.value = 0

class myApp(App):
    def __init__(self, **kwargs):
        super(myApp, self).__init__(**kwargs)
        self.tela = Tela()

    def build(self):
        return  self.tela

    def on_start(self):
        thread.start_new_thread(self.tela.progresso_barra, ())
        pass

if __name__ == '__main__':
    myApp().run()


import time
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()

try:
    driver.get('https://formy-project.herokuapp.com/modal')

    modalButton = driver.find_element(By.ID,"modal-button")
    modalButton.click()

    closeButton = driver.find_element(By.ID,"close-button")
    #it is used to execute the javascript into the script
    driver.execute_script("arguments[0].click();",closeButton)
finally:
    driver.quit()


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

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QIcon

class Example(QWidget):

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

        self.init_ui()

    def init_ui(self):
        self.setGeometry(400, 300, 300, 200)
        self.setWindowTitle('Icon')
        self.setWindowIcon(QIcon("timg.jpg"))
        self.show()

# if __name__ == "__main__":
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec())


import itertools

def parse_expenses(lines):
    for l in lines:
        if l.strip().isnumeric():
            yield int(l.strip())

def find_tuple(expenses, n=2):
    # Can you do any better than O(m^(n-1))?
    expenses = set(expenses)

    for tup in itertools.product(*itertools.tee(iter(expenses), n-1)):
        if 2020 - sum(tup) in expenses:
            return (2020 - sum(tup), *tup)

def get_prod(lines, n=2):
    tup = find_tuple(parse_expenses(lines), n=n)
    assert sum(tup) == 2020  # sanity check

    result = 1
    for value in tup:
        result *= value
    return result

def solve(input):
    lines = list(input)
    print("Pair:", get_prod(lines, 2))
    print("Triple:", get_prod(lines, 3))


# -*- encoding: utf-8 -*-
import pandas as pd
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
import numpy as np

if __name__ == '__main__':
    df = pd.DataFrame(data={"X": [1, 2, 3, 4],
                            "Y": [1, 2, 4, 5]})
    # 通过pandas读取为DataFrame，回归用的是矩阵数据而不是列表，数据为n个样品点和m个特征值，这里特征值只有一个因此换证nx1的矩阵
    dataSet_x = df.loc[:, 'X'].as_matrix(columns=None)  # T为矩阵转置把1xn变成nx1
    dataSet_x = np.array([dataSet_x]).T
    dataSet_y = df.loc[:, 'Y'].as_matrix(columns=None)
    dataSet_y = np.array([dataSet_y]).T  # regr为回归过程，fit(x,y)进行回归
    regr = LinearRegression().fit(dataSet_x, dataSet_y)
    # 输出R的平方print(regr.score(dataSet_x, dataSet_y))
    plt.scatter(dataSet_x, dataSet_y, color='black')
    # 用predic预测，这里预测输入x对应的值，进行画线
    plt.plot(dataSet_x, regr.predict(dataSet_x), color='red', linewidth=1)
    plt.show()


from http.server import BaseHTTPRequestHandler, HTTPServer
import socket
import time
class handler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.end_headers()

        ts= time.time()
        message = "Timestamp: " +str(ts) +'\n'+ '<br>' +'\n'+ 'Hostname: '+socket.gethostname() +'\n'
        self.wfile.write(bytes(message, "utf8"))

with HTTPServer(('', 8080), handler) as server:
    server.serve_forever()


import numpy as np
import regex as re
from os import path
from sys import argv

if len(argv) != 2:
    print('Error: Insufficient amount of arguments')
    exit(1)
fileName = argv[1]

class GraphicalModel:
    networkType = ""
    varN = 0
    card = np.array([])
    cliques = 0
    cliqueScopes = np.array([])
    functionTables = []
    stride = []
    evidence = np.array([])
    order = []

    def __init__(self, uaiFile: str):
        self.parseUAI(uaiFile + ".uai")
        if (path.exists(uaiFile + ".uai.evid")):
            self.parseUAIEvidence(uaiFile + ".uai.evid")
            self.instantiateEvidence()
        self.order = self.getOrder()

    def getIndex(self, assignment: np.array, stride: np.array):
        if len(assignment) == 0:
            return 0
        return sum(assignment * stride)

    def getAssignment(self, index: int, stride: int, card: int):
        return index // stride % card

    def getAssignments(self, index: int, stride: list, card: np.array):
        if (len(stride) == 0):
            return 0
        return [self.getAssignment(index, stride[i], card[i]) for i in range(len(stride))]

    def getOrder(self):
        cliqueSets = [set(cs) for cs in self.cliqueScopes]
        vars = set()
        for cs in cliqueSets:
            vars = vars | cs
        varD = {v: set() for v in vars}
        for v in vars:
            for cs in cliqueSets:
                if v in cs:
                    varD[v] = varD[v] | cs
        order = []
        # Iterate through all edges now, select min degree variable and all add those edges to each other variable that
        # the edge was connected to.
        for i in range(len(vars)):
            minVar = list(varD.keys())[0]
            minDegree = len(varD[minVar])
            for var in vars:
                if len(varD[var]) < minDegree:
                    minVar = var
                    minDegree = len(varD[minVar])
            order.append(minVar)
            for var in vars:
                if minVar in varD[var]:
                    varD[var] = varD[var] | varD[minVar]
                    varD[var] = varD[var] - {minVar}
            del(varD[minVar])
            vars.remove(minVar)
        return order

    def getStride(self, cliqueScope):
        prod = 1
        a = []
        for i in reversed(cliqueScope):
            a.append(prod)
            prod = prod * self.card[i]
        return list(reversed(a))

    def factorProduct(self, X1, F1, S1, X2, F2, S2):
        j = 0
        k = 0
        clique = np.array(list(set().union(X1, X2)))
        stride = self.getStride(np.array(clique))
        x1i = np.ndarray.flatten(np.array([np.argwhere(clique == i) for i in X1])).astype(np.int64)
        x2i = np.ndarray.flatten(np.array([np.argwhere(clique == i) for i in X2])).astype(np.int64)
        vn = np.product(self.card[clique])
        psi = np.full(vn, 0.0)
        for i in range(np.product(self.card[clique])):
            assign = np.array(self.getAssignments(i, stride, self.card[clique]))
            psi[i] = F1[self.getIndex(assign[x1i], S1)] * F2[self.getIndex(assign[x2i], S2)]
        return clique, psi, stride

    def sumOut(self):
        functions = [(self.cliqueScopes[i], self.functionTables[i], self.stride[i]) for i in
                     range(len(self.cliqueScopes))]
        print([f[0] for f in functions])
        for o in self.order:
            phi = [(cs, f, s) for cs, f, s in functions if o in cs]
            functions = [(cs, f, s) for cs, f, s in functions if o not in cs]
            newPhi = None
            if len(phi) == 0:
                continue
            elif len(phi) > 1:
                newPhi = self.factorProduct(phi[0][0], phi[0][1], phi[0][2],
                                            phi[1][0], phi[1][1], phi[1][2])
                phi.pop(0)
                phi.pop(0)
                for p in phi:
                    newPhi = self.factorProduct(newPhi[0], newPhi[1], newPhi[2], p[0], p[1], p[2])
            else:
                newPhi = phi[0]
            functions.append(self.sumVariable(newPhi[0], newPhi[1], newPhi[2], o))
            print([f[0] for f in functions])
        return np.sum(np.log10([f[1][0] for f in functions]))

    def sumVariable(self, X, F, S, v):
        vi = int(np.argwhere(X == v))
        n = np.product(self.card[X])
        newCs = [x for x in X if x != v]
        newPhi = newCs, np.full(n // self.card[v], 0.0), self.getStride(self.card[newCs])
        for i in range(np.product(self.card[X])):
            assignment = self.getAssignments(i, S, self.card[X])
            newPhi[1][self.getIndex(np.delete(assignment, vi), newPhi[2])] += F[i]
        return newPhi

    def instantiateEvidence(self):
        cliqueScopes = [np.array(cs) for cs in self.cliqueScopes]
        for var, val in self.evidence:
            for i, cs in enumerate(cliqueScopes):
                varI = np.argwhere(cs == var)
                if len(varI) != 1:
                    continue
                varI = int(varI)
                newI = []
                for j in range(len(self.functionTables[i])):
                    assignment = self.getAssignments(j, self.stride[i], self.card[cs])
                    if assignment[varI] == val:
                        newI.append(j)
                self.functionTables[i] = self.functionTables[i][newI]
                cliqueScopes[i] = np.delete(cs, varI)
                self.stride[i] = self.getStride(cliqueScopes[i])
        self.cliqueScopes = cliqueScopes

    def parseUAIEvidence(self, evidenceFile: str):
        s = [t for t in open(evidenceFile, "r").read().split(' ') if t]
        observedVariables = int(s.pop(0))
        self.evidence = [(int(s[2 * i]), int(s[2 * i + 1])) for i in range(0, observedVariables)]

    def parseUAI(self, uaiFile: str):
        s = re.sub('^c.*\n?', '', open(uaiFile, "r").read(), flags=re.MULTILINE)
        s = [l for l in s.split('\n') if l]
        s = [l.replace('\t', ' ').replace('\r', ' ').replace('\f', ' ').replace('\v', ' ') for l in s]
        # Below parses the UAI file
        while (len(s) != 0):
            data = s.pop(0)
            if data.upper() in {'MARKOV', 'BAYES'}:
                self.networkType = data
                self.varN = int(s.pop(0))
                self.card = np.array([int(d) for d in s.pop(0).split(' ') if d])
                self.cliques = int(s.pop(0))
                cliqueScopes = []
                while (True):
                    cs = s.pop(0)
                    cliqueScopes += [[int(d) for d in cs.split(' ') if d][1:]]
                    if len([t for t in s[0] if t]) == 1:
                        break
                self.cliqueScopes = cliqueScopes
            if (data.isdigit()):
                entriesN = int(data)
                entries = []
                entriesAdded = 0
                while entriesAdded != entriesN:
                    newEntries = [float(d) for d in s.pop(0).split(' ') if d]
                    entriesAdded += len(newEntries)
                    entries += newEntries
                self.functionTables += [np.array(entries)]
            self.stride = [self.getStride(cs) for cs in self.cliqueScopes]

network = GraphicalModel(fileName)
print(network.sumOut())


import torch
from mpc.factors.gp_factor import GPFactor
from mpc.factors.unary_factor import UnaryFactor
from mpc.obstacle_map.obs_map import ObstacleMap

class CostComposite:

    def __init__(
        self,
        cost_func_list,
        traj_len,
        dim,
        tensor_args=None
    ):
        self.dim = dim
        self.traj_len = traj_len
        self.cost_func_list = cost_func_list
        self.tensor_args = tensor_args

    def eval(self, trajs, observation):
        trajs = trajs.reshape(-1, self.traj_len, self.dim)
        costs = torch.zeros(trajs.shape[0], **self.tensor_args)

        for cost_func in self.cost_func_list:
            costs += cost_func(trajs, observation)

        # # Prior cost
        # costs = self.cost_prior_func(trajs, observation)
        #
        # # Obstacle cost
        # costs += self.cost_2D_obst(trajs, observation)

        return costs

class CostFactoredMultiGoal:

    def __init__(
        self,
        n_dof,
        start_state,
        goal_states,
        traj_len,
        dt,
        sigma_params=None,
        tensor_args=None,
    ):
        self.n_dof = n_dof
        self.dim = n_dof * 2 # Pos + Vel
        self.start_state = start_state
        self.goal_states = goal_states
        self.num_goals = goal_states.shape[0]
        self.traj_len = traj_len
        self.dt = dt

        self.sigma_start = sigma_params['sigma_start']
        self.sigma_goal = sigma_params['sigma_goal']
        self.sigma_gp = sigma_params['sigma_gp']
        self.tensor_args = tensor_args

        self.set_cost_factors()

    def set_cost_factors(self):

        #========= Cost factors ===============
        self.start_prior = UnaryFactor(
            self.dim,
            self.sigma_start,
            self.start_state,
            self.tensor_args,
        )

        self.gp_prior = GPFactor(
            self.n_dof,
            self.sigma_gp,
            self.dt,
            self.traj_len - 1,
            self.tensor_args,
        )

        self.multi_goal_prior = []
        for i in range(self.num_goals):
            self.multi_goal_prior.append(
                UnaryFactor(
                    self.dim,
                    self.sigma_goal,   # Assume same goal Cov.
                    self.goal_states[i],
                    self.tensor_args,
                )
            )

    def eval(self, trajs, observation=None):

        trajs = trajs.reshape(-1, self.traj_len, self.dim)

        # Start cost
        err_p = self.start_prior.get_error(trajs[:, [0]], calc_jacobian=False)
        w_mat = self.start_prior.K
        start_costs = err_p @ w_mat.unsqueeze(0) @ err_p.transpose(1, 2)
        start_costs = start_costs.squeeze()

        # GP cost
        err_gp = self.gp_prior.get_error(trajs, calc_jacobian=False)
        w_mat = self.gp_prior.Q_inv[0] # repeated Q_inv
        w_mat = w_mat.reshape(1, 1, self.dim, self.dim)
        gp_costs = err_gp.transpose(2, 3) @ w_mat @ err_gp
        gp_costs = gp_costs.sum(1)
        gp_costs = gp_costs.squeeze()

        costs = start_costs + gp_costs

        # Multi-Goal cost
        trajs = trajs.reshape(self.num_goals, -1, self.traj_len, self.dim)
        costs = costs.reshape(self.num_goals, -1)
        for i in range(self.num_goals):
            err_g = self.multi_goal_prior[i].get_error(trajs[i, :, [-1]], calc_jacobian=False)
            w_mat = self.multi_goal_prior[i].K
            goal_costs = err_g @ w_mat.unsqueeze(0) @ err_g.transpose(1, 2)
            goal_costs = goal_costs.squeeze()
            costs[i] += goal_costs
        costs = costs.reshape(-1)

        return costs

class CostObst2D:

    def __init__(
        self,
        traj_len,
        weight_obst,
        tensor_args=None,
    ):
        self.dim = 4  # 2D Pos + Vel
        self.traj_len = traj_len
        self.weight_obst = weight_obst
        self.tensor_args = tensor_args

    def eval(self, trajs, observation):
        """ """
        assert trajs.shape[-1] == self.dim
        trajs = trajs.reshape(-1, self.traj_len, self.dim)

        # Obstacle cost
        obst_cost = torch.zeros(trajs.shape[0], **self.tensor_args)
        if 'obst_map' in observation:
            obst_map = observation['obst_map']
            assert isinstance(obst_map, ObstacleMap)
            obst_cost = obst_map.get_collisions(trajs[..., :2]) * self.weight_obst
            obst_cost = obst_cost.sum(1)

        return obst_cost


'''
0. Initialize parameters

1. E-step - VI-algorithm

  1.1 For every document

    1.1.1 Calculate gamma

    1.1.2 Calculate phi

  1.2 Calculate lambdas

  1.3 Check for convergence in lower-bound

2. M-step

  2.1 NR for alpha

  2.2 NR for eta

3 Check for convergence in lower-bound

'''

import numpy as np
from scipy.special import digamma, polygamma
from scipy.special import gamma as gamma_function
from scipy.special import loggamma as loggamma
import scipy
from DataLoader import DataLoader
import time, csv
from datetime import datetime
import EachMovieParser.Eachmovie_parser as eachmovie

## Load data
def load_data(filename, num_documents = 10**6):

  data_loader = DataLoader(filename)
  data, V = data_loader.load(num_documents)

  return data, V

## Derivative of the digamma function (help-function)
def trigamma(a):
  return polygamma(1, a)

## VI-algorithm run during the E-step for every document m
def VI_algorithm(k, V, corpus, phis_old, gammas_old, lambda_old, alpha, eta, alpha_sum, tolerance = 1e-2, debug = False):
  lower_bound_old = lower_bound_corpus(alpha, eta, lambda_old, phis_old, gammas_old, alpha_sum, k, V, corpus)

  # Extended pseudocode from page 1005
  it = 0
  while True:
    it += 1

    phis_new = []

    gammas_new = []

    for (d, document) in enumerate(corpus):
      # Calculate the new phis
      phi_new = calculate_phi(gammas_old[d], lambda_old, document, k)

      phis_new.append(phi_new)

      # Calculate the new gammas
      gamma_new = calculate_gamma(phis_new[d], alpha, k)

      gammas_new.append(gamma_new)

    # Calculate the new lambdas (not sure about this one)
    lambda_new = calculate_lambda(phis_new, eta, corpus, V, k)

    lower_bound_new = lower_bound_corpus(alpha, eta, lambda_new, phis_new, gammas_new, alpha_sum, k, V, corpus)

    # "bad" convergence criteria
    # convergence = np.linalg.norm(np.array(gammas_new) - np.array(gammas_old)) /np.linalg.norm(np.array(gammas_old))

    # Correct convergence criteria
    convergence = abs((lower_bound_old-lower_bound_new) / lower_bound_old)

    if convergence < tolerance:
      break
    else:
      phis_old = phis_new
      gammas_old = gammas_new
      lambda_old = lambda_new
      lower_bound_old = lower_bound_new
  print("\tVI finished in", it, "iterations!")
  return phis_new, gammas_new, lambda_new

## Calculate phi for document m
def calculate_phi3(gamma, lambdas, document, k):
  N = len(document)

  # According to step 6
  phi = lambdas[:, document[:]].T * np.tile(np.exp(digamma(gamma) - digamma(np.sum(gamma))),(N,1))

  # phi2 = np.log(lambdas[:, document[:]].T) + np.tile(digamma(gamma) - digamma(np.sum(gamma)),(N,1))

  # Normalize phi since it's a probability (must sum up to 1)
  phi = phi/np.sum(phi, axis = 1)[:, np.newaxis]
  # phi2 = np.exp(phi2 - np.tile(scipy.special.logsumexp(phi2, axis = 1), (k,1)).T)

  return phi

## Calculate phi for document m
def calculate_phi(gamma, lambdas, document, k):
  N = len(document)

  phi = []
  for n in range(N):
    # According to step 6
    # phi_n = np.exp(digamma(lambdas[:, document[n]]) - digamma(np.sum(lambdas, axis=1))) * np.exp(digamma(gamma) - digamma(np.sum(gamma)))
    log_phi_n = digamma(lambdas[:, document[n]]) - digamma(np.sum(lambdas, axis=1)) + digamma(gamma) - digamma(np.sum(gamma))

    log_phi_n = log_phi_n - scipy.special.logsumexp(log_phi_n)
    # phi_n = beta[:, document[n]] * np.exp(digamma(gamma))
    # print(phi_n)
    # Normalize phi since it's a probability (must sum up to 1)
    # phi_n = phi_n / np.sum(phi_n)
    phi_n = np.exp(log_phi_n)

    phi.append(phi_n)

  phi = np.array(phi)

  return phi

## Calculate phi for document m
def calculate_phi_bad(gamma, lambdas, document, k):
  N = len(document)

  phi = []
  for n in range(N):
    # According to step 6
    phi_n = lambdas[:, document[n]] / np.sum(lambdas[:, document[n]]) * np.exp(digamma(gamma) - digamma(np.sum(gamma)))
    # log_phi_n = digamma(lambdas[:, document[n]]) - digamma(np.sum(lambdas, axis=1)) + digamma(gamma) - digamma(np.sum(gamma))

    # log_phi_n = log_phi_n - scipy.special.logsumexp(log_phi_n)
    # phi_n = beta[:, document[n]] * np.exp(digamma(gamma))
    # print(phi_n)
    # Normalize phi since it's a probability (must sum up to 1)
    phi_n = phi_n / np.sum(phi_n)
    # phi_n = np.exp(log_phi_n)

    phi.append(phi_n)

  phi = np.array(phi)

  return phi

## Calculate gamma for document m
def calculate_gamma(phi, alpha, k):
  # According to equation 7 on page 1004
  gamma = alpha + np.sum(phi, axis = 0)

  return gamma

## To calculate beta in the M-step
def calculate_lambda(phis, eta, corpus, V, k):
  lambdas = np.ones((k,V)) * eta

  for d in range(len(corpus)):
    N = len(corpus[d])
    for n in range(N):
      j = corpus[d][n]
      lambdas[:, j] += phis[d][n,:]

  # lambdas = lambdas/np.sum(lambdas, axis = 1)[:, np.newaxis]

  return lambdas

## The lower bound for a single document
def lower_bound_single(alpha, lambdas, phi, gamma, alpha_sum, k, document, debug = False):
  # Helpful things

  digamma_gamma_sum = digamma(np.sum(gamma))

  N = len(document)

  # Calculating the lower bound according to page 1020

  # First row
  likelihood = alpha_sum # First part of first row

  likelihood += np.sum((alpha-1)*(digamma(gamma) - digamma_gamma_sum)) # Second part of first row

  # Second row
  likelihood += np.sum(phi * np.tile((digamma(gamma) - digamma_gamma_sum), (N,1)))

  # Third row
  for n in range(N):
    # likelihood += np.sum(phi[n,:]*np.log(np.maximum(lambdas[:,document[n]], 1e-90)))
    likelihood += np.sum(phi[n,:] * (digamma(lambdas[:,document[n]]) - digamma(np.sum(lambdas, axis = 1))))

  # The fourth row
  likelihood += -loggamma(np.sum(gamma)) + np.sum(loggamma(gamma)) - np.sum((gamma-1)*(digamma(gamma) - digamma_gamma_sum))

  # The fifth row
  likelihood += np.sum(np.log(phi ** phi))

  return likelihood

## The lower bound for the whole corpus
def lower_bound_corpus(alpha, eta, lambdas, phi, gamma, alpha_sum, k, V, corpus):
  likelihood = 0

  for (d, document) in enumerate(corpus):
    likelihood += lower_bound_single(alpha, lambdas, phi[d], gamma[d], alpha_sum, k, document)

  likelihood += k * (loggamma(V * eta) - V * loggamma(eta))

  likelihood += (eta - 1) * ((np.sum(digamma(lambdas))) - V * np.sum(digamma(np.sum(lambdas, axis = 1))))

  likelihood -= np.sum(loggamma(np.sum(lambdas, axis = 1))) - np.sum(loggamma(lambdas)) + np.sum((lambdas-1) * (digamma(lambdas) - np.tile(digamma(np.sum(lambdas, axis = 1)),(V,1)).T))

  return likelihood

## Initialize EM parameters
def initialize_parameters_EM(V, k):
  np.random.seed(1)

  # E) I think that we should maybe encode sparcity into each into the Dirichlet. See https://youtu.be/o22cA1DhSMQ?t=1566 for how an alpha < 1 does this.
  """ approx_alpha = 0.01 # 0.1
  alpha = np.random.uniform(approx_alpha - 0.1 * approx_alpha, approx_alpha + 0.1 * approx_alpha, k)
  input(alpha) """

  approx_alpha = 0.001
  alpha = np.random.uniform(approx_alpha - 0.1 * approx_alpha, approx_alpha + 0.1 * approx_alpha, k)

  approx_eta = 0.0001
  eta = np.random.uniform(approx_eta - 0.1 * approx_eta, approx_eta + 0.1 * approx_eta)

  return alpha, eta

## Initialize VI parameters
def initialize_parameters_VI(alpha, eta, corpus, k, V):
  np.random.seed(1)
  # M x N_d x k
  phi = []
  for document in corpus:
    phi.append(np.ones((len(document),k)) * 1/k)

  # M x k
  gamma = np.tile(alpha.copy(),(len(corpus),1)) + np.tile(np.array(list(map(lambda x: len(x),corpus))),(k,1)).T / k

  # lambdas = np.random.rand(k, V)

  # for i in range(k):
  #   # Normalizing
  #   lambdas[i,:] = lambdas[i,:] / sum(lambdas[i,:])

  lambdas = np.ones((k,V)) * (eta + len(corpus[0])/V) + np.random.rand(k, V) * 2

  return phi, gamma, lambdas

## Newton-Raphson function to calculate new alpha in the M-step
def calculate_alpha(gamma, alpha, M, k, nr_max_iterations = 1000, tolerance = 10 ** -4):
  # Use Newton-Raphson method with linear complexity suggested by Thomas P. Minka in
  # Estimating a Dirichlet distribution

  gamma = np.array(gamma)

  log_p_mean = np.sum((digamma(gamma)-np.tile(digamma(np.sum(gamma,axis=1)),(k,1)).T),axis=0)

  for it in range(nr_max_iterations):
    alpha_old = alpha

    # Calculate the observed efficient statistic
    # Here we are using that the expected sufficient statistics are equal to the observed sufficient statistics
    # for distributions in the exponential family when the gradient is zero
    # log_p_mean = np.sum((digamma(gamma)-np.tile(digamma(np.sum(gamma,axis=1)),(k,1)).T),axis=0)

    g = M * (digamma(np.sum(alpha)) - digamma(alpha)) + log_p_mean

    # Calculate the diagonal of the Hessian
    h = - M * trigamma(alpha)

    # Calculate the constant component of the Hessian
    z = M * trigamma(np.sum(alpha))

    # Calculate the constant
    b = np.sum(g/h) / (1/z + np.sum(1/h))

    # Update equation for alpha
    alpha = alpha - (g - b) / h
    # log_alpha = np.log(alpha) - (g - b) / h
    # alpha = np.exp(log_alpha)

    # if np.all(np.abs(g) < tolerance):
    if np.linalg.norm(alpha-alpha_old) < tolerance:
      break

    if np.any(alpha < 0):
      print("Alpha is negative!")

  return np.abs(alpha)

## Newton-Raphson function to calculate new eta in the M-step
def calculate_eta(lambdas, eta, V, k, nr_max_iterations = 1000, tolerance = 10 ** -4):

  for it in range(nr_max_iterations):
    # print(eta)
    eta_old = eta

    df = V * k * (digamma(V * eta) - digamma(eta)) + np.sum(digamma(lambdas)) - V * np.sum(digamma(np.sum(lambdas, axis = 1)))

    df2 = V * k * (V * trigamma(V * eta) - trigamma(eta))

    step = - df / df2

    # Update equation for alpha
    eta = eta + step

    if np.abs((eta-eta_old) / eta_old) < tolerance:
      break

  if np.any(eta < 0):
    print("Eta is negative!")

  return np.abs(eta)

## Taken from Blei
def calculate_eta2(lambdas, eta, V, k, nr_max_iterations = 1000, tolerance = 10 ** -4):
  initial_eta = 100

  di_lambda_sum = np.sum(digamma(lambdas)) - V * np.sum(digamma(np.sum(lambdas, axis = 1)))

  log_eta = np.log(initial_eta)

  for it in range(nr_max_iterations):
    eta = np.exp(log_eta)

    df = V * k * (digamma(V * eta) - digamma(eta)) + di_lambda_sum

    df2 = V * k * (V * trigamma(V * eta) - trigamma(eta))

    log_eta = log_eta - df / (df2 * eta + df)

    if np.abs(df) < tolerance:
      break
  # print(it)
  if it == nr_max_iterations - 1:
    print("Max reached!")

  return np.exp(log_eta)

## LDA function
def LDA_algorithm(corpus, V, k, tolerance = 1e-4):
  alpha_old, eta_old = initialize_parameters_EM(V, k)
  M = len(corpus)

  phis_old, gammas_old, lambda_old = initialize_parameters_VI(alpha_old, eta_old, corpus, k, V)

  alpha_sum_old = loggamma(np.sum(alpha_old)) - np.sum(loggamma(alpha_old))

  lower_bound_old = lower_bound_corpus(alpha_old, eta_old, lambda_old, phis_old, gammas_old, alpha_sum_old, k, V, corpus)

  print("Starting lower bound:", lower_bound_old)

  start_EM = time.time()
  it = 0
  ########################
  # --- EM-algorithm --- #
  ########################
  while True:
    # print(gammas_old.shape)
    it += 1

    print("\nEM-iteration:", it)

    ##################
    # --- E-step --- #
    ##################
    print("E-step...")
    start = time.time()

    # phi_old, gamma_old = phi_new, gamma_new
    phis_new, gammas_new, lambda_new = VI_algorithm(k, V, corpus, phis_old, gammas_old, lambda_old, alpha_old, eta_old, alpha_sum_old)

    stop = time.time()
    print('...completed in:', stop - start)
    ##################
    # --- M-step --- #
    ##################
    print("M-step...")
    start = time.time()

    print("\t... calculate alpha")
    alpha_new = calculate_alpha(gammas_old, alpha_old, M, k)

    alpha_sum_new = loggamma(np.sum(alpha_old)) - np.sum(loggamma(alpha_old))

    print("\t... calculate eta")

    eta_new = calculate_eta(lambda_old, eta_old, V, k)
    print(eta_old, "-->", eta_new)
    # eta_new_vector = calculate_alpha(lambda_old, eta_old * np.ones(V), k, V)
    # print(eta_new_vector)
    # eta_new = eta_new_vector[0]

    stop = time.time()
    print('...completed in:', stop - start)
    ########################
    # --- Convergence? --- #
    ########################

    print("Computing the lower bound...")
    start_n = time.time()
    lower_bound_new = lower_bound_corpus(alpha_new, eta_new, lambda_new, phis_new, gammas_new, alpha_sum_new, k, V, corpus)
    print("\t", lower_bound_new)
    stop_n = time.time()
    print('...completed in:', stop_n - start_n)

    if lower_bound_new < lower_bound_old:
      print("Oh no! The lower bound decreased...")

    # The change of the lower bound
    delta_lower_bound = abs((lower_bound_old-lower_bound_new) / lower_bound_old)
    print("The lower bound changed: {}%".format(np.round(100*delta_lower_bound, 5)))
    if delta_lower_bound < tolerance:
      print("Convergence after", it, "iterations!")
      break

    else:
      alpha_old = alpha_new
      eta_old = eta_new
      lower_bound_old = lower_bound_new
      alpha_sum_old = alpha_sum_new

  stop_EM = time.time()
  print('\nThe algorithm converged in', stop_EM - start_EM, "seconds")
  return [alpha_new, eta_new, phis_old, gammas_old, lambda_new]

## Print parameters
def print_parameters(parameters, printing = True):
  if printing:
    params = ["alpha", "eta", "phi", "gamma", "lambda"]

    for (i,param) in enumerate(params):
      print(param + ":")
      print(parameters[i])

## Main function reuters
def main_Reuters():
  # Initial parameters
  k = 10              # Number of topics
  num_documents = 200 #10**6

  # File directories
  vocab_file = './Code/Reuters_Corpus_Vocabulary.csv'
  filename = './Code/Reuters_Corpus_Vectorized.csv'

  # Load data
  corpus, V = load_data(filename, num_documents)

  nTraining = int(num_documents * 0.9)

  test = corpus[nTraining:]
  corpus = corpus[:nTraining]

  # Run the algorithm
  parameters = LDA_algorithm(corpus, V, k)
  print(parameters[0])
  print(parameters[1])
  # Print the parameters
  print_parameters(parameters, False)

  # # Print most likely topics and words
  # alpha = parameters[0]
  # num_topics = 5 # The number of topics that should be printed
  # topic_indices = print_likely_topics(alpha, num_topics)
  # beta = parameters[1]
  # print_top_words_for_all_topics(vocab_file, beta, top_x=15, k=k, indices = topic_indices)

  # phi = parameters[2]
  # gamma = parameters[3]
  # print(len(corpus), len(test))
  # print_perplexity(alpha, beta, phi, gamma, k, corpus, test)

  # print()

main_Reuters()


import argparse
import os

import megengine as mge
import numpy as np
import torch
import torch.nn as nn

from models.senet import (senet154, seresnet18, seresnet34, seresnet50,
                          seresnet101, seresnet152, seresnext26_32x4d,
                          seresnext50_32x4d, seresnext101_32x4d)
from models.torch_models import (torch_senet154, torch_seresnet18,
                                 torch_seresnet34, torch_seresnet50,
                                 torch_seresnet101, torch_seresnet152,
                                 torch_seresnext26_32x4d,
                                 torch_seresnext50_32x4d,
                                 torch_seresnext101_32x4d)

def get_atttr_by_name(torch_module, k):
    name_list = k.split('.')
    sub_module = getattr(torch_module, name_list[0])
    if len(name_list) != 1:
        for i in name_list[1:-1]:
            try:
                sub_module = getattr(sub_module, i)
            except:
                sub_module = sub_module[int(i)]
    return sub_module

def convert(torch_model, torch_dict):
    new_dict = {}
    for k, v in torch_dict.items():
        data = v.numpy()
        sub_module = get_atttr_by_name(torch_model, k)
        is_conv = isinstance(sub_module, nn.Conv2d)
        if is_conv:
            groups = sub_module.groups
            is_group = groups > 1
        else:
            is_group = False
        if "weight" in k and is_group:
            out_ch, in_ch, h, w = data.shape
            data = data.reshape(groups, out_ch // groups, in_ch, h, w)
        if "bias" in k:
            if is_conv:
                data = data.reshape(1, -1, 1, 1)
        if "num_batches_tracked" in k:
            continue
        new_dict[k.replace("se_module", "se_block")] = data
    return new_dict

def main(torch_name):
    try:
        torch_model = eval("torch_" + torch_name)(pretrained=True)
    except:
        print("If you can't download the pretrained weights, just download it from timm repo in github")
        torch_model = eval("torch_" + torch_name)()
        torch_model.load_state_dict(torch.load(
            './senet154-c7b49a05.pth', map_location='cpu'))
    torch_state_dict = torch_model.state_dict()
    model = eval(torch_name)()

    new_dict = convert(torch_model, torch_state_dict)

    model.load_state_dict(new_dict)
    os.makedirs('pretrained', exist_ok=True)
    mge.save(new_dict, os.path.join('pretrained', torch_name + '.pkl'))

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "-m",
        "--model",
        type=str,
        default='senet154',
        help=f"which model to convert from torch to megengine",
    )
    args = parser.parse_args()
    main(args.model)


"""
Contains code for all the game objects.
"""
import os

import pyglet
from random import random

# Obtain the path to this script. This workaround is
# just for importing as a module; it's not needed otherwise.
# We then use this to add the image path to where pyglet searches.
pyglet.resource.path.append(
    os.path.join(os.path.dirname(os.path.realpath(__file__)), 'images'))

class GameObject(object):
    """
    Basic code for something that has a sprite and
    will check for collisions.

    Image anchor defaults to bottom left.

    Class variables:

    image -- pyglet image for the sprite (default player.png)
    explosion_image -- the same but for post-hit (default explosion.png)
    scale -- the scaling factor that should be applied to the sprite
    explosion_time -- the time in seconds that the explosion sprite lingers

    Instance variables:
    sprite -- the pyglet sprite, made from the image.
    exploded -- has this object exploded? Boolean.
    destroyed -- is this object no longer needed? Boolean.

    Methods:
    has_hit -- Naively checks a collision with another object.
    draw -- draws the sprite
    destroy -- set destroyed to True
    explode -- switch to the explosion sprite and then destroy
    """
    # Default image will be the player.
    image = pyglet.resource.image("player.png")
    explosion_image = pyglet.resource.image("explosion.png")
    scale = 1
    explosion_time = 0.2

    def __init__(self, x_pos, y_pos):
        """
        Initialise the object, forming a sprite at the given location.

        Arguments:
        x_pos -- The x coordinate of the sprite's anchor point.
        y_pos -- Ditto, but y.
        """
        self.sprite = pyglet.sprite.Sprite(
            self.image,
            x=x_pos,
            y=y_pos)
        self.sprite.scale = self.scale

        # Set state variables to be False
        self.exploded = self.destroyed = False

    def has_hit(self, other_object):
        """
        Check whether this object has collided with another.

        Simplistic check - we check if our anchor point is within
        their sprite. We can fiddle with the anchor points in our
        subclasses so that this makes the most sense.

        Arguments:
        other_object -- the object we are checking against.
        """

        # First we check the x direction
        if self.sprite.x > other_object.sprite.x \
                and self.sprite.x < \
                other_object.sprite.x + other_object.sprite.width:

            # Then we check the y.
            if self.sprite.y > other_object.sprite.y \
                    and self.sprite.y < \
                    other_object.sprite.y + other_object.sprite.height:
                return True

        # By default say no.
        return False

    def draw(self):
        """
        Simply draw the sprite.
        """
        self.sprite.draw()

    def destroy(self, elapsed_time=None):
        """
        Mark yourself has destroyed so the game will get rid of you.

        Arguments:
        elapsed_time -- not used, but required by the clock system in pyglet
        """
        self.destroyed = True

    def explode(self):
        """
        Make yourself explode, and then destroy yourself after a delay!

        This swaps the sprite in place to be the explosion image, and uses
        the pyglet clock to schedule calling self.destroy after a delay that
        is set in the class variable explosion_time.
        """
        self.exploded = True
        self.sprite = pyglet.sprite.Sprite(
            self.explosion_image,
            x=self.sprite.x,
            y=self.sprite.y)
        pyglet.clock.schedule_once(self.destroy, self.explosion_time)

class Bullet(GameObject):
    """
    This class specifies the behaviour for the projectiles that we will fire,
    and inherits from GameObject

    Changed Class Variables:
    image -- Now set to the bullet image with the anchor at the top centre.
    scale -- Set to 0.2 to make the bullets smaller.

    New Class Variables:
    speed: Bullet speed in pixels per second. (Default 180)

    Methods:
    update -- Moves the bullet along the y axis.
    """
    image = pyglet.resource.image("bullet.png")
    image.anchor_x = image.width / 2
    image.anchor_y = image.height
    speed = 180
    scale = 0.2

    def __init__(self, x_pos):
        """
        Initialise a newly created bullet.

        Assumes that the starting y coordinate is the height of the Player
        plus the scaled height of a bullet (because the anchor is at the top).

        Arguments:
        x_pos -- the x coordinate, intended to be the middle of the Player
        """
        super(Bullet, self).__init__(
            x_pos=x_pos,
            y_pos=Player.image.height + self.image.height * self.scale)

    def update(self, elapsed_time):
        """
        Move the bullet up, according to the speed and elapsed time.

        Arguments:
        elapsed_time -- The time since we last moved.
        """
        self.sprite.y += self.speed * elapsed_time

class Laser(Bullet):
    """
    Like bullets, but green and they travel downwards! Extends Bullet.

    Changed Class Variables:
    image -- Set to laser.png, and the anchor point at middle bottom
    speed -- Set to -180, as lasers travel downwards.
    """
    image = pyglet.resource.image("laser.png")
    image.anchor_y = 0
    speed = -180

    def __init__(self, x_pos, y_pos):
        """
        Initialise a newly created laser.

        Takes both x and y as alien could be anywhere on screen.
        We adjust the y-coordinate so that the laser looks like it
        comes from the middle of the alien.
        """
        super(Laser, self).__init__(x_pos)
        self.sprite.y = y_pos + self.sprite.height / 2

class Player(GameObject):
    """
    Handles the details of the player's tank. Extends GameObject.

    Contains a pyglet KeyStateHandler for tracking the keyboard.

    Changed Class Variables:
    image -- now set to player.png

    New Class Variables:
    speed -- pixels per second, like the bullet. (default 150)
    cooldown_time -- minimum time between shots, in seconds. (default 0.5)
    left_key -- the keyboard key for moving left (default left arrow)
    right_key -- ditto, but for moving right (default right arrow)
    fire_key -- Again, but for firing! (default space)

    Instance Variables:
    cooldown -- Boolean for whether you are on cooldown. Reset by end_cooldown.
    window -- The window where everything is being drawn. Used to add bullets.
    key_handler -- The KeyStateHandler for tracking buttons

    Methods:
    update -- Call move and fire based on keyboard and cooldown.
    move -- Called by update to move along the x direction appropriately
    end_cooldown -- Called when it is ok to fire again.
    fire -- Fires a bullet!
    """

    image = pyglet.resource.image("player.png")
    speed = 150
    cooldown_time = 0.5

    left_key = pyglet.window.key.LEFT
    right_key = pyglet.window.key.RIGHT
    fire_key = pyglet.window.key.SPACE

    def __init__(self, window):
        """
        Create a new Player instance.

        Starts player off at standard location, and makes a KeyStateHandler.
        Sets the cooldown to false, so we can fire immediately.

        Arguments:
        window -- the main game window. Used for referring to bullet list.
        """
        super(Player, self).__init__(x_pos=20, y_pos=20)
        self.key_handler = pyglet.window.key.KeyStateHandler()
        self.window = window
        self.cooldown = False

    def update(self, elapsed_time=0):
        """
        Move and fire if appropriate.

        Uses the key_handler to see what buttons are active.
        Will only fire if not on cooldown, and only move if just
        one of the movement buttons is being pressed. Uses the speed
        class variable, negating it for moving left.

        Arguments:
        elapsed_time -- Time in seconds since last update. Passed to move.
        """
        # Check player holding left ONLY
        if self.key_handler[Player.left_key] and \
                not self.key_handler[Player.right_key]:
            self.move(-Player.speed, elapsed_time)
        # Alternatively holding right ONLY
        elif self.key_handler[Player.right_key] and \
                not self.key_handler[Player.left_key]:
            self.move(Player.speed, elapsed_time)

        # Prevent a swarm of bullets by only firing if cooldown is false
        if self.key_handler[self.fire_key] and not self.cooldown:
            self.fire()
            self.cooldown = True
            pyglet.clock.schedule_once(self.end_cooldown, Player.cooldown_time)

    def move(self, speed, elapsed_time):
        """
        Simply do speed * time to get a movement distance.

        Arguments:
        speed -- The horizontal speed.
        elapsed_time -- Time the tank has been travelling at that speed.
        """
        self.sprite.x += speed * elapsed_time

    def end_cooldown(self, elapsed_time=None):
        """
        Set cooldown to false, enabling you to fire again

        Arguments:
        elapsed_time -- Ignored. Required by pyglet scheduling system.
        """
        self.cooldown = False

    def fire(self):
        """
        Fire a bullet!

        Uses the window variable to add a bullet into the list
        managed by the main game window.
        """
        self.window.bullets.append(
            Bullet(self.sprite.x + self.sprite.width / 2))

class Alien(GameObject):
    """
    Handles all of the joys of being an alien. Extends GameObject.

    Changed Class Variables:
    image -- Now invader.png. Anchor point unchanged.

    New Class Variables:
    strafe_step -- The number of pixels aliens should move sideways at a time (default 50)
    strafe_delay -- The time in seconds between sideways movements. (default 1)
    lurch_delay -- The time in seconds between downwards jumps. (default 5)
    victory_threshold -- The height that an alien needs to reach to win (default Player height)
    likelihood_to_fire -- Between 0 and 1. Percentage chance of firing on update (default 0.001)
    row_size -- How many aliens to a row (default 4)

    Instance variables:
    head_right -- Boolean. True for strafing right, false for left.

    Methods:
    strafe -- Jump right or left depending on head_right's value.
    lurch -- Jump downwards, towards player. Return false if victory_threshold reached.
    fire -- Pick a random percentage. Fire if it is less than likelihood_to_fire.
    """
    image = pyglet.resource.image("invader.png")
    strafe_step = 50
    strafe_delay = 1
    lurch_delay = 5
    victory_threshold = Player.image.height
    likelihood_to_fire = 0.001

    def __init__(self, window, x_pos):
        """
        Set up a new alien.

        Assumes the y coordinate is at the top of the screen.

        Arguments:
        window -- The window. Used for firing lasers.
        x_pos -- The x coordinate."""
        super(Alien, self).__init__(
            x_pos=x_pos,
            y_pos=window.window.height - Alien.image.height)

        self.head_right = True

        pyglet.clock.schedule_interval(self.strafe, Alien.strafe_delay)

        self.window = window

    def strafe(self, elapsed_time=None):
        """
        Do that characteristic strafe across the screen.

        Arguments:
        elapsed_time -- ignored, as aliens jump same distance each time.
        """
        if self.head_right:
            self.sprite.x += Alien.strafe_step
        else:
            self.sprite.x -= Alien.strafe_step

    def lurch(self):
        """
        Lurch forward, towards the player.
        Returns false if the alien has reached the victory threshold

        Moves a distance of 1 alien plus 5 pixels.
        """
        self.sprite.y -= self.sprite.height + 5
        return self.sprite.y > Alien.victory_threshold

    def fire(self):
        """
        Fire the LASERS! Maybe.

        Percentages are represented as decimal numbers between 0 and 1.
        We choose a random percentage, and fire if it is below the
        likelihood_to_fire value.

        The laser is spawned from the centre bottom of the aliens.
        """
        if random() < Alien.likelihood_to_fire:
            self.window.lasers.append(
                Laser(
                    self.sprite.x + self.sprite.width / 2,
                    self.sprite.y + self.sprite.height))


"""
This example uses docopt with the built in cmd module to demonstrate an
interactive command application.
Usage:
    contact_manager -n <name> -p <phonenumber> [--timeout=<seconds>]
    contact_manager search <name> [--timeout=<seconds>]
    contact_manager text <name> -m <message> [--timeout=<seconds>]
    contact_manager serial <port> [--baud=<n>] [--timeout=<seconds>]
    contact_manager (-i | --interactive)
    contact_manager (-h | --help | --version)
Options:
    -i, --interactive  Interactive Mode
    -h, --help  Show this screen and exit.
    --baud=<n>  Baudrate [default: 9600]
"""

import sys
import cmd
from docopt import docopt, DocoptExit
from contacts import ContactEntry, ContactSearch
from smsApi import SendSms

def docopt_cmd(func):
    """
    This decorator is used to simplify the try/except block and pass the result
    of the docopt parsing to the called action.
    """
    def fn(self, arg):
        try:
            opt = docopt(fn.__doc__, arg)

        except DocoptExit as e:
            # The DocoptExit is thrown when the args do not match.
            # We print a message to the user and the usage block.

            print("Request not delivered try again!")
            print(e)
            return

        except SystemExit:
            # The SystemExit exception prints the usage for --help
            # We do not need to do the print here.

            return

        return func(self, opt)

    fn.__name__ = func.__name__
    fn.__doc__ = func.__doc__
    fn.__dict__.update(func.__dict__)
    return fn

class MyInteractive (cmd.Cmd):
    intro = 'Welcome to my interactive program!' \
        + ' (type help for a list of commands.)'
    prompt = '(contact_manager) '
    file = None

    def add_contact(self, name, number):
        new_contact = ContactEntry(name,number)
        new_contact.add_contact()

    def search(self, name):
        search_item = ContactSearch(name)
        search_item.search_contact_list()

    def sms(self, name, message):
        send_msg = SendSms(name, message)
        send_msg.send_sms()

    @docopt_cmd
    def do_add(self,args):
        """Usage: add -n <name> -p <phonenumber>"""
        # print/ args['<name>'], "number is ", args['<phonenumber>']
        self.add_contact(args['<name>'], args['<phonenumber>'])

    @docopt_cmd
    def do_search(self, args):
        """Usage: search search <name>"""
        self.search(args['<name>'])

    @docopt_cmd
    def do_text(self, args):
        """Usage: text <name> -m <message>..."""

        self.sms(args['<name>'],(" ".join(args['<message>'])))

    def do_quit(self, args):
        """Quits out of Interactive Mode."""

        print('Awesome saving contacts')
        exit()

opt = docopt(__doc__, sys.argv[1:])

if opt['--interactive']:
    MyInteractive().cmdloop()

print(opt)


import psycopg2
import psycopg2.extras

import pandas as pd
import pandas.io.sql as psql
import random
from datetime import timedelta, datetime, date
import string
from .constants import WINNING_TRANSACTION_SCHEMA, CITIZEN_RANKING_SCHEMA, ACQUIRER_CODES, CIRCUIT_TYPES, ACQUIRER_ID, BIN

class Transaction():
  def __init__(self, args):
    self.args = args
    self.db_connection = psycopg2.connect(args.connection_string)

  def generate(self):

    award_period_df = self._read_award_period()
    assert award_period_df.shape[0] > 0, "Award Period n. %s doesn't exist." % (self.args.award_period)

    citizen_df = self._read_fiscal_code()
    assert citizen_df.shape[0] > 0, "Citizen %s is not onboarded." % (self.args.fiscal_code)

    payment_instrument_df = self._read_enrolled_payment_instruments()
    assert payment_instrument_df.shape[0] > 0, "Citizen %s didn't enrolled payment instruments" % (self.args.fiscal_code)

    transactions_df = pd.DataFrame(columns=WINNING_TRANSACTION_SCHEMA)
    for _ in range(self.args.number) :
      my_transaction = self._generate_single_transaction(award_period_df,
        citizen_df, hpan=payment_instrument_df.at[0, 'hpan_s'])

      transactions_df = transactions_df.append(my_transaction, True)

    # Write Transactions in Winning Transactions
    self._create_transactions(transactions_df)

  def enable(self):

    award_period_df = self._read_award_period()
    assert award_period_df.shape[0] > 0, "Award Period n. %s doesn't exist." % (self.args.award_period)

    citizen_df = self._read_fiscal_code()
    assert citizen_df.shape[0] > 0, "Citizen %s is not onboarded." % (self.args.fiscal_code)

    transactions_disabled_df = self._read_disabled_transactions()
    print(transactions_disabled_df)

    table = "bpd_winning_transaction.bpd_winning_transaction"

    set_values = (
      "update_date_t = (%s), "
      "update_user_s = (%s), "
      "enabled_b = (%s) ")

    conditions = (
      "award_period_id_n = (%s) AND "
      "fiscal_code_s = (%s) AND "
      "update_date_t > (%s) AND "
      "enabled_b = false"
    )

    #   #create INSERT INTO table (columns) VALUES('%s',...)
    update_q = "UPDATE {} SET {} WHERE {};".format(
         table, set_values, conditions)

    cursor = self.db_connection.cursor()
    cursor.execute(update_q, ['now()', self.args.update_user, "true", self.args.award_period, self.args.fiscal_code, self.args.from_date])
    self.db_connection.commit()
    cursor.close()

  def update_ranking(self) :
     # Must read all transactions
    summary_df = self._summarize_ranking_by_fiscal_code()

    ranking_df = pd.DataFrame(columns=CITIZEN_RANKING_SCHEMA).append(
      {
        'fiscal_code_c' : self.args.fiscal_code,
        'award_period_id_n' : self.args.award_period,
        'cashback_n': summary_df.at[0, 'score'],
        'insert_date_t' : datetime.now().isoformat(),
        'insert_user_s' : "PAGOPATEST",
        'update_date_t' : None,
        'update_user_s' : None,
        'transaction_n' : summary_df.at[0, 'transactions_num'],
        'ranking_n' : random.randint(0, 100000),
        'id_trx_pivot' : None,
        'cashback_norm_pivot' : None,
        'id_trx_min_transaction_number' : None,
        'last_trx_timestamp_t' : None
      }
      , True)
    self._update_ranking(ranking_df)

  def check(self):
    transactions_df = pd.read_csv(self.args.file, sep=';')

    match = lambda x : self._read_matching_transactions(x)

    print(transactions_df.apply(match, axis=1))

  def check_unique(self):
    transactions_df = pd.read_csv(self.args.file, sep=';', dtype={"operation_type_c": str, "acquirer_id_s" : str})
    transactions_df["unique"] = False
    check_unique = lambda x : self._read_unique_transaction(x)
    transactions_df = transactions_df.apply(check_unique, axis=1)
    result = transactions_df[transactions_df["unique"] == False]
    if(result.shape[0] > 0 ):
      print(result.to_csv())
    else :
      print ("Check OK")

  def _read_unique_transaction(self, transaction):
    self.db_cursor = self.db_connection.cursor()

    transactions_unique_q = self.db_cursor.mogrify(
      "SELECT * "
      "FROM bpd_winning_transaction.bpd_winning_transaction "
      "WHERE trx_timestamp_t = %(trx_timestamp)s "
      "AND id_trx_acquirer_s = %(id_trx_acquirer)s "
      "AND acquirer_c = %(acquirer_c)s "
      "AND acquirer_id_s = %(acquirer_id)s "
      "AND operation_type_c = %(operation_type)s;",
      {
        "trx_timestamp": transaction.trx_timestamp_t,
        "id_trx_acquirer" : transaction.id_trx_acquirer_s,
        "acquirer_c" : transaction.acquirer_c,
        "acquirer_id" : transaction.acquirer_id_s,
        "operation_type" : transaction.operation_type_c
      })

    transaction_db = pd.read_sql(transactions_unique_q, self.db_connection)
    transaction["unique"] = transaction_db.shape[0] == 1

    return transaction

  def _read_transactions_by_fiscal_code(self) :
    self.db_cursor = self.db_connection.cursor()

    transactions_q = self.db_cursor.mogrify(
      "SELECT * "
      "FROM bpd_winning_transaction.bpd_winning_transaction "
      "WHERE fiscal_code_s = %(fiscal_code)s "
      "AND enabled_b = true;", {"fiscal_code": self.args.fiscal_code})

    return pd.read_sql(transactions_q, self.db_connection)

  def _read_matching_transactions (self, transaction):

    print("\n===========\nTO MATCH: ", transaction)

    self.db_cursor = self.db_connection.cursor()

    transactions_match_q = self.db_cursor.mogrify(
      "SELECT trx_timestamp_t, amount_i, fiscal_code_s, id_trx_acquirer_s, id_trx_issuer_s "
      "FROM bpd_winning_transaction.bpd_winning_transaction "
      "WHERE fiscal_code_s = %(fiscal_code)s "
      # "AND DATE_PART('day', trx_timestamp_t - %(trx_date)s) = 0 "
      "AND id_trx_acquirer_s = %(id_trx_acquirer)s ;",
      # "AND amount_i = %(amount)s ;",
      #"AND enabled_b = true;",
      {
        "fiscal_code": transaction.fiscal_code_s.strip(),
        "trx_date": datetime.strptime(transaction.trx_date_t, "%m/%d/%y").date().isoformat(),
        "amount" : transaction.amount_i,
        "id_trx_acquirer" : transaction.id_trx_acquirer_s.strip()
      }
    )

    print("MATCHED: ")
    if not pd.isna(transaction.id_trx_acquirer_s):
      print(pd.read_sql(transactions_match_q, self.db_connection))

  def _summarize_ranking_by_fiscal_code(self) :

    ranking_summary_q = self.db_cursor.mogrify(
      "SELECT sum(amount_i) AS amount, sum(score_n) AS score, count(*) AS transactions_num "
      "FROM bpd_winning_transaction.bpd_winning_transaction "
      "WHERE fiscal_code_s = %(fiscal_code)s "
      "AND enabled_b = true "
      "GROUP BY fiscal_code_s;", {"fiscal_code": self.args.fiscal_code})

    return pd.read_sql(ranking_summary_q, self.db_connection)

  def _update_ranking(self, ranking_df):
    if len(ranking_df) > 0:

      columns = list(ranking_df)

      # create VALUES('%s', '%s",...) one '%s' per column
      values = "VALUES({})".format(",".join(["%s" for _ in columns]))

      #create INSERT INTO table (columns) VALUES('%s',...)
      insert = "INSERT INTO {} ({}) {}".format(
        "bpd_citizen.bpd_citizen_ranking", ",".join(columns), values)

      cursor = self.db_connection.cursor()
      psycopg2.extras.execute_batch(cursor, insert, ranking_df.values)
      self.db_connection.commit()
      cursor.close()

  def _read_award_period(self) :
    self.db_cursor = self.db_connection.cursor()

    award_period_q = self.db_cursor.mogrify(
      "SELECT * "
      "FROM bpd_award_period.bpd_award_period "
      "WHERE award_period_id_n = %(id)s;", {"id": self.args.award_period})

    return pd.read_sql(award_period_q, self.db_connection)

  def _read_fiscal_code(self) :
    self.db_cursor = self.db_connection.cursor()

    citizen_q = self.db_cursor.mogrify(
      "SELECT * "
      "FROM bpd_citizen.bpd_citizen "
      "WHERE fiscal_code_s = %(fiscal_code)s "
      "AND enabled_b = true", {"fiscal_code": self.args.fiscal_code})

    return pd.read_sql(citizen_q, self.db_connection)

  def _read_enrolled_payment_instruments(self) :
    self.db_cursor = self.db_connection.cursor()

    payment_instruments_q = self.db_cursor.mogrify(
      "SELECT hpan_s, fiscal_code_s "
      "FROM bpd_payment_instrument.bpd_payment_instrument "
      "WHERE fiscal_code_s = %(fiscal_code)s "
      "AND enabled_b = true", {"fiscal_code": self.args.fiscal_code})

    return pd.read_sql(payment_instruments_q, self.db_connection)

  def _read_disabled_transactions(self):
    self.db_cursor = self.db_connection.cursor()

    transactions_disabled_q = self.db_cursor.mogrify(
      "SELECT update_date_t, fiscal_code_s "
      "FROM bpd_winning_transaction.bpd_winning_transaction "
      "WHERE fiscal_code_s = %(fiscal_code)s "
      "AND update_date_t > %(from_date)s"
      "AND award_period_id_n = %(award_period)s"
      "AND enabled_b = false;",
      {
        "fiscal_code": self.args.fiscal_code,
        "from_date": self.args.from_date,
        "award_period" : self.args.award_period
      }
    )

    return pd.read_sql(transactions_disabled_q, self.db_connection)

  def _create_transactions(self, transactions_df):
    if len(transactions_df) > 0:

      columns = list(transactions_df)

      # create VALUES('%s', '%s",...) one '%s' per column
      values = "VALUES({})".format(",".join(["%s" for _ in columns]))

      #create INSERT INTO table (columns) VALUES('%s',...)
      insert = "INSERT INTO {} ({}) {}".format(
        "bpd_winning_transaction.bpd_winning_transaction", ",".join(columns), values)

      cursor = self.db_connection.cursor()
      psycopg2.extras.execute_batch(cursor, insert, transactions_df.values)
      self.db_connection.commit()
      cursor.close()

  def _generate_single_transaction(self, award_period_df, citizen_df, hpan) :
    amount = random.random() * 100
    id_trx = list(''.join(random.choice(string.ascii_letters) for _ in range(2))
      + ''.join(random.choice(string.digits) for _ in range(4)))
    random.shuffle(id_trx)
    award_period_start = award_period_df.at[0,'aw_period_start_d']
    delta = date.today() - award_period_start
    trx_time = ( datetime.combine( award_period_start, datetime.min.time())
      + timedelta(seconds=random.random() * delta.total_seconds()) )
    insert_time = trx_time + timedelta(days=random.randint(0,5), seconds=random.random() * 3600 * 24)

    assert trx_time <= insert_time

    return {
      'acquirer_c' : random.choice(ACQUIRER_CODES),
      'trx_timestamp_t': trx_time.isoformat(),
      'hpan_s': hpan,
      'operation_type_c': '00',
      'circuit_type_c': random.choice(CIRCUIT_TYPES),
      'amount_i': f'{amount:.2f}',
      'amount_currency_c': '978',
      'mcc_c': '0000',
      'mcc_descr_s' : None,
      'score_n' : f'{0.1 * amount:.2f}',
      'award_period_id_n' : self.args.award_period,
      'insert_date_t' : insert_time.isoformat(),
      'insert_user_s' : 'PAGOPATEST',
      'update_date_t' : None,
      'update_user_s' : None,
      'enabled_b' : True,
      'merchant_id_s' : ''.join(random.choice(string.digits) for _ in range(15)),
      'correlation_id_s': ''.join(random.choice(string.digits) for _ in range(12)),
      'acquirer_id_s' : random.choice(ACQUIRER_ID),
      'id_trx_issuer_s' : ''.join(id_trx).upper(),
      'id_trx_acquirer_s' : ''.join(random.choice(string.digits) for _ in range(12)),
      'bin_s' : random.choice(BIN),
      'terminal_id_s' : ''.join(random.choice(string.digits) for _ in range(8)),
      'fiscal_code_s' : self.args.fiscal_code,
      'elab_ranking_new_b' : True,
      'elab_ranking_b' : True
    }


from django import forms
from django.forms import ModelForm, fields, widgets
from .models import Producto

class ProductoForm(ModelForm):
    class Meta :
        model=Producto
        fields=['numeroSerie','marca','modelo','categoria']

        labels={
            'numeroSerie':'ingrese numero serie',
            'marca':'ingrese la marca',
            'modelo':'ingrese modelo',
            'categoria':'seleccione categoria'

        }

        widgets={
            'numeroSerie': forms.TextInput(
                attrs={
                    'class':'form-control',
                    'placeholder':'numeroSerie',
                    'id':'numeroSerie'

                }
            ),
            'marca': forms.TextInput(
                attrs={
                    'class':'form-control',
                    'placeholder':'marca',
                    'id':'marca'

                }

            ),
            'modelo':forms.TextInput(
                attrs={
                    'class':'form-control',
                    'placeholder':'modelo',
                    'id':'modelo'

                }
            ),
            'categoria':forms.Select(
                attrs={
                    'class':'form-control',
                    'id':'categoria'

                }

            )

        }


a = [4,5,8] # 458
b = [6,5,8,0]
# c = a + b
from collections import deque
stack_a = deque()
stack_b = deque()
result = deque()

def is_empty(queue):
    return queue == deque()

for i in a:
    stack_b.append(i)
print(stack_b)
for i in b:
    stack_a.append(i)
print(stack_a)

add_one_flag = 0
while not is_empty(stack_a) and not is_empty(stack_b):
    num = stack_a.pop() + stack_b.pop() + add_one_flag
    result.appendleft(num % 10)
    add_one_flag = 1 if num - 10  >= 0 else 0

stack_c = deque()
if not is_empty(stack_a):
    stack_c = stack_a
elif not is_empty(stack_b):
    stack_c = stack_b

while not is_empty(stack_c):
    num = stack_c.pop() + add_one_flag
    result.appendleft(num % 10)
    add_one_flag = 1 if num - 10  >= 0 else 0

if add_one_flag == 1:
    result.appendleft(1)

print(result)


import requests
import json
import chardet
import re

def get_cid(bvid):
    """根据 bvid 得到 cid"""
    url = 'https://api.bilibili.com/x/player/pagelist?bvid={}&jsonp=jsonp'.format(bvid)
    res = requests.get(url).text
    json_dict = json.loads(res)
    return json_dict["data"][0]["cid"]

"""
注意：哔哩哔哩的网页现在已经换了，那个list.so接口已经找不到，但是我们现在记住这个接口就行了。
"""
def get_data(cid):
    # 根据cid请求弹幕，解析弹幕得到最终的数据
    final_url = "https://api.bilibili.com/x/v1/dm/list.so?oid=" + str(cid)
    final_res = requests.get(final_url)
    final_res.encoding = chardet.detect(final_res.content)['encoding']
    final_res = final_res.text
    pattern = re.compile('<d.*?>(.*?)</d>')
    data = pattern.findall(final_res)
    return data

def save_to_file(data):
    # 保存弹幕列表
    with open(".\\dan_mu.txt",'w', encoding="utf-8") as f:
        count = 0
        for i in data:
            f.write(i)
            f.write("\n")
            count += 1
        print("{0}条弹幕写入完成！".format(count))

if __name__ == "__main__":
    bvid = "BV17s411B73s"
    cid = get_cid(bvid)
    data = get_data(cid)
    save_to_file(data)
    # print(data)


from django.contrib.auth.models import User
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from django.core.mail import send_mail
from django.conf import settings

from .models import Profile

# @receiver(post_save, sender = Profile)
def create_profile(sender, instance, created, **kwargs):
    """If the account is a new user, create the user in the main
    Django users database and create a profile for that user"""

    if created:
        user = instance
        profile = Profile.objects.create(user = user,
                                         username = user.username,
                                         email = user.email,
                                         name = user.first_name)

        # send_mail(
        #     subject = "Welcome to DevSearch",
        #     message = "Welcome to DevSearch",
        #     from_email = settings.EMAIL_HOST_USER,
        #     recipient_list = [profile.email],
        #     fail_silently = False
        # )

post_save.connect(create_profile, sender = User)

# @receiver(post_delete)
def delete_profile(sender, instance, **kwargs):
    """If the user profile is deleted, this function will also delete
    the user account that is stored in the main Django users database."""

    user = instance.user
    user.delete()

post_delete.connect(delete_profile, sender = Profile)

def update_user(sender, instance, created, **kwargs):
    profile = instance
    user = profile.user

    if created == False:
        user.first_name = profile.name
        user.username = profile.username
        user.email = profile.email
        user.save()

post_save.connect(update_user, sender=Profile)


import os
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent

SECRET_KEY = 'django-insecure-yfl1$ax779wo*#oqveol((&3%3)vgbp12mv+t*j!w+w8+wg2v7'

DEBUG = True

ALLOWED_HOSTS = ["*"]

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]


import json

import pandas as pd

def duplicate2json(path):

    df = pd.read_csv(path)

    #df.drop(['map'], axis=1, inplace=True)
    df2 = df.copy(deep=True)
    df2.set_index(['topic'], drop=True, inplace=True)
    confidence = df2['confidence_score'].to_dict()
    df.drop(['confidence_score'], axis=1, inplace=True)

    res = df.groupby('Cluster ID')['topic'].apply(list).to_dict()
    with open('csv_example_output_list.json', 'wt') as outf:
        for k in res:
            topics = sorted([(x, confidence[x]) for x in res[k]], key=lambda x: x[1], reverse=False)
            r = {'cluster': k, 'topics': topics}
            kv = json.dumps(r) + '\n'
            outf.write(kv)


import asyncio
import contextlib
import logging

from terra_proto.cosmos.authz.v1beta1 import QueryStub
from terra_proto.cosmos.bank.v1beta1 import QueryStub as QueryStubBank

from atomkraft.chain import Testnet

def query_grants(testnet: Testnet, granter, grantee):
    if logging.getLogger().isEnabledFor(logging.DEBUG):
        with contextlib.closing(testnet.get_grpc_channel()) as channel:
            stub = QueryStub(channel)
            result = asyncio.run(
                stub.grants(
                    granter=testnet.acc_addr(granter), grantee=testnet.acc_addr(grantee)
                )
            )
            logging.debug(f"grants from {granter} to {grantee}: {result.grants}")

def query_balance(testnet: Testnet, address):
    with contextlib.closing(testnet.get_grpc_channel()) as channel:
        stub = QueryStubBank(channel)
        result = asyncio.run(
            stub.balance(address=testnet.acc_addr(address), denom=testnet.denom)
        )
        return result.balance.amount

def query_all_balances(testnet: Testnet):
    if logging.getLogger().isEnabledFor(logging.DEBUG):
        balances = [f"{a}: {query_balance(testnet, a)}" for a in testnet.accounts.keys()]
        logging.debug(f"balances: {balances}")


import hashlib
import logging
import pathlib

from tealpkg.cli.colorprint import cprint

def verify_checksum(path_to_checksums, path_to_check, entry_name):
    result = False
    compare = ''
    logger = logging.getLogger(__name__)

    checksums_path = pathlib.PosixPath(path_to_checksums)
    if checksums_path.exists():
        logger.debug('Looking for %s in CHECKSUMS.md5', entry_name)
        with open(path_to_checksums, 'r') as fh:
            line = fh.readline()
            while line:
                parts = line.split()
                if len(parts) == 2 and parts[1] == entry_name:
                    compare = parts[0].lower()
                    break
                #
                line = fh.readline()
            #
        #

        if compare:
            check_path = pathlib.PosixPath(path_to_check)
            if check_path.exists():
                with open(path_to_check, 'rb') as fh:
                    md = hashlib.md5()
                    md.update(fh.read())
                    if md.hexdigest().lower() == compare:
                        result = True
                    else:
                        cprint('Incorrect MD5 checksum:', path_to_check, style='error', stderr=True)
                        logger.error('Incorrect MD5 checksum for %s', check_path)
                    #
                #
            else:
                cprint('Missing file:', path_to_check, style='error', stderr=True)
                logger.error('No comparison file: %s', check_path)
        else:
            cprint('Incomplete CHECKSUMS.md5:', path_to_checksums, style='error', stderr=True)
            logger.error('Missing %s from CHECKSUMS.md5', compare)
        #
    else:
        cprint('Missing CHECKSUMS.md5', style='error', stderr=True)
        logger.error('CHECKSUMS.md5 not found: %s', checksums_path)
    #

    return result
#


import json
import math

from sym_cps.contract.tool.contract_tool import ContractManager, ContractTemplate
from sym_cps.representation.library.elements.library_component import LibraryComponent
from sym_cps.shared.paths import data_folder

class MotorPropellerAnalysis(object):
    def __init__(self, table_dict, c_library):
        self._table_dict = table_dict
        self._c_library = c_library
        self.collect_abstracted_battery()
        self._rpm_static = 10000
        self._solver_verbose = False
        ret = []
        for np, prop in enumerate(list(self._c_library.components_in_type["Propeller"])):
            for nm, motor in enumerate(list(self._c_library.components_in_type["Motor"])):
                ##### first find the prop motor that can generates thrust
                ##### Then find the maximum voltage to break the system... save the voltage in result.
                # for np, prop in enumerate([self._c_library.components["apc_propellers_7x5E"]]):#apc_propellers_6x4E
                #     for nm, motor in enumerate([self._c_library.components["kde_direct_KDE2315XF_885"]]): #t_motor_AT2312KV1400
                print(f"{np} {nm} Analyze ({prop.id}, {motor.id})", end="")
                self.battery_system_analysis_thrust(add_weight=0.5, motors=[motor], propellers=[prop])
                is_sat_thrust = self._manager.solve()
                if not is_sat_thrust:
                    print("     Incompatible")
                    continue
                v_motor = self._manager.get_metric("MotorInst", "V_motor")
                i_motor = self._manager.get_metric("MotorInst", "I_motor")

                self.battery_system_analysis_voltage(add_weight=0, motors=[motor], propellers=[prop], check_power=True)
                is_sat_voltage_power = self._manager.solve()
                v_current = None
                v_power = None
                if is_sat_voltage_power:
                    v_power = self._manager.get_metric("MotorInst", "V_motor")
                self.battery_system_analysis_voltage(add_weight=0, motors=[motor], propellers=[prop], check_power=False)
                is_sat_voltage_current = self._manager.solve()
                if is_sat_voltage_current:
                    v_current = self._manager.get_metric("MotorInst", "V_motor")
                if is_sat_voltage_current or is_sat_voltage_power:
                    print(f" V power {v_power}, V Current {v_current} V thrust {v_motor}", end="")
                    if v_current is None:
                        v_ret = v_power
                    elif v_power is None:
                        v_ret = v_current
                    else:
                        v_ret = min(v_power, v_current)
                    ret.append([prop.id, motor.id, {"min_fly": {"I": i_motor, "V": v_motor}, "max_v": v_ret}])
                    print("     OK")
                else:
                    print("     Error")
                    print("     Incompatible?")
        file_path = data_folder / "ComponentLibrary" / "component_selection_analysis" / "motor_propeller_pair.json"

        with open(file_path, "w") as outfile:
            print(len(ret))
            json.dump(ret, outfile, indent=4)

    def collect_abstracted_battery(self):
        self._num_battery = 2
        range_dict: dict[str, tuple[float, float]] = {}  # prop_name, (min, max)
        for battery in list(self._c_library.components_in_type["Battery"]):
            props = self.hackathon_property_interface_fn(battery)
            for prop_name, val in props.items():
                if prop_name not in range_dict:
                    range_dict[prop_name] = (val, val)
                else:
                    min_val = min(range_dict[prop_name][0], val)
                    max_val = max(range_dict[prop_name][1], val)
                    range_dict[prop_name] = (min_val, max_val)

        print(range_dict)
        self._battery_dict: dict[str, tuple[float, float]] = range_dict
        return range_dict

    def battery_system_analysis_thrust(
        self,
        add_weight=0,
        motors: list[LibraryComponent] = None,
        propellers: list[LibraryComponent] = None,
    ):
        """Analyze requirement for motor and propeller"""
        if propellers is None:
            propellers = list(self._c_library.components_in_type["Propeller"])
            # propellers = [self._c_library.components["apc_propellers_6x4E"]]

            # propellers = [[self._c_library.components["apc_propellers_6x4E"],
            #                self._c_library.components["apc_propellers_15x6E"],
            #                self._c_library.components["apc_propellers_26x15E"]] ] * num_motor
        if motors is None:
            motors = list(self._c_library.components_in_type["Motor"])
            # motors = [self._c_library.components["t_motor_AT2312KV1400"]]
            #             self._c_library.components["t_motor_AntigravityMN4006KV380"],
            #             self._c_library.components["t_motor_AntigravityMN1005V2KV90"]
        self.set_contract()
        self.set_rpm(rpm=10000)
        self.set_system_battery_thrust(add_weight=add_weight)
        self._manager = ContractManager(self.hackathon_property_interface_fn, verbose=self._solver_verbose)

        motor_inst = self._contracts["Motor"].instantiate(f"MotorInst")
        prop_inst = self._contracts["Propeller"].instantiate(f"PropellerInst")
        system_inst = self._contracts["System"].instantiate(f"System")
        batt_inst = self._contracts["Battery"].instantiate(f"Battery")
        self._manager.add_instance(motor_inst)
        self._manager.add_instance(prop_inst)
        self._manager.add_instance(system_inst)
        self._manager.add_instance(batt_inst)
        self._manager.set_selection(inst=prop_inst, candidate_list=propellers)
        self._manager.set_selection(inst=motor_inst, candidate_list=motors)

        propeller_motor_connection = [
            ("torque_prop", "torque_motor"),
            ("omega_prop", "omega_motor"),
            ("shaft_motor", "shaft_motor"),
        ]
        self._manager.compose(prop_inst, motor_inst, propeller_motor_connection)

        motor_battery_connection = [
            ("I_motor", "I_batt"),
        ]
        self._manager.compose(motor_inst, batt_inst, motor_battery_connection)

        system_prop_connection = [("W_prop", "W_prop"), ("thrust", f"thrust"), ("rho", "rho")]
        self._manager.compose(system_inst, prop_inst, system_prop_connection)
        system_motor_connection = [
            (f"W_motor", f"W_motor"),
            ("I_motor", "I_motor"),
            ("V_motor", "V_motor"),
            ("P_max_motor", "P_max_motor"),
            ("I_max_motor", "I_max_motor"),
        ]
        self._manager.compose(system_inst, motor_inst, system_motor_connection)
        system_batt_connection = [("capacity", "capacity"), ("V_battery", "V_battery")]
        self._manager.compose(system_inst, batt_inst, system_batt_connection)

        objective_expr = system_inst.get_var("thrust") - system_inst.get_var("weight_sum")
        objective_val = -100

        def objective_fn():
            thrust_sum = self._manager.get_metric_inst(system_inst, "thrust")
            weight_sum = self._manager.get_metric_inst(system_inst, "weight_sum")
            return thrust_sum - weight_sum

        self._manager.set_objective(expr=objective_expr, value=objective_val, evaluate_fn=objective_fn)

    def battery_system_analysis_voltage(
        self,
        add_weight=0,
        check_power=False,
        motors: list[LibraryComponent] = None,
        propellers: list[LibraryComponent] = None,
    ):
        """Analyze requirement for motor and propeller"""
        if propellers is None:
            propellers = list(self._c_library.components_in_type["Propeller"])
            # propellers = [self._c_library.components["apc_propellers_6x4E"]]

            # propellers = [[self._c_library.components["apc_propellers_6x4E"],
            #                self._c_library.components["apc_propellers_15x6E"],
            #                self._c_library.components["apc_propellers_26x15E"]] ] * num_motor
        if motors is None:
            motors = list(self._c_library.components_in_type["Motor"])
            # motors = [self._c_library.components["t_motor_AT2312KV1400"]]
            #             self._c_library.components["t_motor_AntigravityMN4006KV380"],
            #             self._c_library.components["t_motor_AntigravityMN1005V2KV90"]
        self.set_contract()
        self.set_rpm(rpm=18000)
        self.set_system_battery_voltage(add_weight=add_weight, check_power=check_power)
        self._manager = ContractManager(self.hackathon_property_interface_fn, verbose=self._solver_verbose)

        motor_inst = self._contracts["Motor"].instantiate(f"MotorInst")
        prop_inst = self._contracts["Propeller"].instantiate(f"PropellerInst")
        system_inst = self._contracts["System"].instantiate(f"System")
        batt_inst = self._contracts["Battery"].instantiate(f"Battery")
        self._manager.add_instance(motor_inst)
        self._manager.add_instance(prop_inst)
        self._manager.add_instance(system_inst)
        self._manager.add_instance(batt_inst)
        self._manager.set_selection(inst=prop_inst, candidate_list=propellers)
        self._manager.set_selection(inst=motor_inst, candidate_list=motors)

        propeller_motor_connection = [
            ("torque_prop", "torque_motor"),
            ("omega_prop", "omega_motor"),
            ("shaft_motor", "shaft_motor"),
        ]
        self._manager.compose(prop_inst, motor_inst, propeller_motor_connection)

        motor_battery_connection = [
            ("I_motor", "I_batt"),
        ]
        self._manager.compose(motor_inst, batt_inst, motor_battery_connection)

        system_prop_connection = [("W_prop", "W_prop"), ("thrust", f"thrust"), ("rho", "rho")]
        self._manager.compose(system_inst, prop_inst, system_prop_connection)
        system_motor_connection = [
            (f"W_motor", f"W_motor"),
            ("I_motor", "I_motor"),
            ("V_motor", "V_motor"),
            ("P_max_motor", "P_max_motor"),
            ("I_max_motor", "I_max_motor"),
        ]
        self._manager.compose(system_inst, motor_inst, system_motor_connection)
        system_batt_connection = [("capacity", "capacity"), ("V_battery", "V_battery")]
        self._manager.compose(system_inst, batt_inst, system_batt_connection)

        objective_expr = system_inst.get_var("thrust") - system_inst.get_var("weight_sum")
        objective_val = -100

        def objective_fn():
            thrust_sum = self._manager.get_metric_inst(system_inst, "thrust")
            weight_sum = self._manager.get_metric_inst(system_inst, "weight_sum")
            return thrust_sum - weight_sum

        self._manager.set_objective(expr=objective_expr, value=objective_val, evaluate_fn=objective_fn)

    def analysis(self, add_weight=0, motors: list[LibraryComponent] = None, propellers: list[LibraryComponent] = None):

        """Analyze requirement for motor and propeller"""
        if propellers is None:
            propellers = list(self._c_library.components_in_type["Propeller"])
            # propellers = [self._c_library.components["apc_propellers_6x4E"]]

            # propellers = [[self._c_library.components["apc_propellers_6x4E"],
            #                self._c_library.components["apc_propellers_15x6E"],
            #                self._c_library.components["apc_propellers_26x15E"]] ] * num_motor
        if motors is None:
            motors = list(self._c_library.components_in_type["Motor"])
            # motors = [self._c_library.components["t_motor_AT2312KV1400"]]
            #             self._c_library.components["t_motor_AntigravityMN4006KV380"],
            #             self._c_library.components["t_motor_AntigravityMN1005V2KV90"]
        self.set_contract()
        self.set_rpm(rpm=10000)
        self.set_system(add_weight=add_weight)
        self._manager = ContractManager(self.hackathon_property_interface_fn, verbose=self._solver_verbose)

        motor_inst = self._contracts["Motor"].instantiate(f"MotorInst")
        prop_inst = self._contracts["Propeller"].instantiate(f"PropellerInst")
        system_inst = self._contracts["System"].instantiate(f"System")
        self._manager.add_instance(motor_inst)
        self._manager.add_instance(prop_inst)
        self._manager.add_instance(system_inst)
        self._manager.set_selection(inst=prop_inst, candidate_list=propellers)
        self._manager.set_selection(inst=motor_inst, candidate_list=motors)

        propeller_motor_connection = [
            ("torque_prop", "torque_motor"),
            ("omega_prop", "omega_motor"),
            ("shaft_motor", "shaft_motor"),
        ]
        self._manager.compose(prop_inst, motor_inst, propeller_motor_connection)

        system_prop_connection = [("W_prop", "W_prop"), ("thrust", f"thrust"), ("rho", "rho")]
        self._manager.compose(system_inst, prop_inst, system_prop_connection)
        system_motor_connection = [(f"W_motor", f"W_motor"), ("I_motor", "I_motor"), ("V_motor", "V_motor")]
        self._manager.compose(system_inst, motor_inst, system_motor_connection)

        objective_expr = system_inst.get_var("thrust") - system_inst.get_var("weight_sum")
        objective_val = -100

        def objective_fn():
            thrust_sum = self._manager.get_metric_inst(system_inst, "thrust")
            weight_sum = self._manager.get_metric_inst(system_inst, "weight_sum")
            return thrust_sum - weight_sum

        self._manager.set_objective(expr=objective_expr, value=objective_val, evaluate_fn=objective_fn)

    def set_system_battery_voltage(self, add_weight=0, check_power=False):
        system_port_name_list = [
            "rho",
            "thrust",
            "weight_sum",
            "I_motor",
            "W_motor",
            "W_prop",
            "thrust",
            "V_motor",
            "capacity",
            "V_battery",
            "P_max_motor",
            "I_max_motor",
        ]
        system_property_name_list = []

        def system_assumption(vs):
            weight_sum = (add_weight + vs["W_prop"] + vs["W_motor"]) * 9.81
            return [vs["weight_sum"] == weight_sum, vs["rho"] == 1.225]

        def system_guarantee(vs):
            if check_power:
                ret_clauses = [vs["P_max_motor"] == vs["I_motor"] * vs["V_motor"]]
            else:
                ret_clauses = [vs["I_max_motor"] == vs["I_motor"]]

            return ret_clauses

        system_contract = ContractTemplate(
            name="System",
            port_name_list=system_port_name_list,
            property_name_list=system_property_name_list,
            guarantee=system_guarantee,
            assumption=system_assumption,
        )
        self._contracts["System"] = system_contract

    def set_system_battery_thrust(self, add_weight=0):
        system_port_name_list = [
            "rho",
            "thrust",
            "weight_sum",
            "I_motor",
            "W_motor",
            "W_prop",
            "thrust",
            "V_motor",
            "capacity",
            "V_battery",
            "P_max_motor",
            "I_max_motor",
        ]
        system_property_name_list = []

        def system_assumption(vs):
            weight_sum = (add_weight + vs["W_prop"] + vs["W_motor"]) * 9.81
            return [
                vs["I_motor"] <= vs["capacity"] * 3600 / 400 / 4,
                vs["weight_sum"] == weight_sum,
                vs["rho"] == 1.225,
            ]

        def system_guarantee(vs):
            ret_clauses = [vs["thrust"] == vs["weight_sum"]]

            return ret_clauses

        system_contract = ContractTemplate(
            name="System",
            port_name_list=system_port_name_list,
            property_name_list=system_property_name_list,
            guarantee=system_guarantee,
            assumption=system_assumption,
        )
        self._contracts["System"] = system_contract

    def set_system(self, add_weight=0):
        # TODO should be able to be generated automatically from contract_tool
        system_port_name_list = ["rho", "thrust", "weight_sum", "I_motor", "W_motor", "W_prop", "thrust", "V_motor"]
        system_property_name_list = []

        def system_assumption(vs):
            return [vs["I_motor"] <= 5]

        def system_guarantee(vs):
            weight_sum = (add_weight + vs["W_prop"] + vs["W_motor"]) * 9.81
            ret_clauses = [
                vs["weight_sum"] == weight_sum,
                vs["rho"] == 1.225,
                vs["thrust"] / vs["weight_sum"] > 1.5,
                vs["V_motor"] <= 7,
            ]

            return ret_clauses

        system_contract = ContractTemplate(
            name="System",
            port_name_list=system_port_name_list,
            property_name_list=system_property_name_list,
            guarantee=system_guarantee,
            assumption=system_assumption,
        )
        self._contracts["System"] = system_contract

    def set_contract(self):
        self._contracts = {}

        propeller_port_name_list = ["rho", "omega_prop", "torque_prop", "thrust", "shaft_motor"]
        propeller_property_name_list = ["C_p", "C_t", "diameter", "shaft_prop", "W_prop"]

        def propeller_assumption(vs):
            return [vs["shaft_prop"] >= vs["shaft_motor"], vs["C_p"] >= 0]

        def propeller_guarantee(vs):
            return [
                vs["torque_prop"]
                == vs["C_p"] * vs["rho"] * vs["omega_prop"] ** 2 * vs["diameter"] ** 5 / (2 * 3.14159265) ** 3,
                vs["thrust"]
                == (vs["C_t"] * vs["rho"] * vs["omega_prop"] ** 2 * vs["diameter"] ** 4 / (2 * 3.14159265) ** 2),
                vs["omega_prop"] >= 0,
            ]

        propeller_contract = ContractTemplate(
            name="Propeller",
            port_name_list=propeller_port_name_list,
            property_name_list=propeller_property_name_list,
            guarantee=propeller_guarantee,
            assumption=propeller_assumption,
        )
        self._contracts["Propeller"] = propeller_contract

        motor_port_name_list = ["torque_motor", "omega_motor", "I_motor", "V_motor"]
        motor_property_name_list = [
            "I_max_motor",
            "P_max_motor",
            "K_t",
            "K_v",
            "W_motor",
            "R_w",
            "I_idle",
            "shaft_motor",
        ]

        def motor_assumption(vs):
            return [vs["V_motor"] * vs["I_motor"] <= vs["P_max_motor"], vs["I_motor"] <= vs["I_max_motor"]]

        def motor_guarantee(vs):
            return [
                vs["I_motor"] * vs["R_w"] == vs["V_motor"] - vs["omega_motor"] / vs["K_v"],
                vs["torque_motor"]
                == vs["K_t"] / vs["R_w"] * (vs["V_motor"] - vs["R_w"] * vs["I_idle"] - vs["omega_motor"] / vs["K_v"]),
            ]

        motor_contract = ContractTemplate(
            name="Motor",
            port_name_list=motor_port_name_list,
            property_name_list=motor_property_name_list,
            guarantee=motor_guarantee,
            assumption=motor_assumption,
        )
        self._contracts["Motor"] = motor_contract

        battery_port_name_list = ["I_batt", "capacity", "W_batt", "I_max", "V_battery"]
        battery_property_name_list = []

        def battery_assumption(vs):
            return [vs["I_batt"] < vs["I_max"]]

        def battery_guarantee(vs):
            clauses = []
            for prop_name, (min_val, max_val) in self._battery_dict.items():
                if prop_name == "name":
                    continue
                clauses.extend([vs[prop_name] >= min_val, vs[prop_name] <= max_val])
            return clauses

        battery_contract = ContractTemplate(
            name="Battery",
            port_name_list=battery_port_name_list,
            property_name_list=battery_property_name_list,
            guarantee=battery_guarantee,
            assumption=battery_assumption,
        )
        self._contracts["Battery"] = battery_contract

    def hackathon_property_interface_fn(self, component: LibraryComponent):
        if component.comp_type.id == "Propeller":
            return self.hackthon_get_propeller_property(
                propeller=component, table_dict=self._table_dict, rpm=self._rpm_static
            )
        elif component.comp_type.id == "Battery":
            return self.hackthon_get_battery_property(battery=component, num_battery=self._num_battery)
        elif component.comp_type.id == "Motor":
            return self.hackthon_get_motor_property(motor=component)
        elif component.comp_type.id == "BatteryController":
            return {}

    def hackthon_get_propeller_property(self, propeller, table_dict, rpm):
        # ports get value

        diameter: float = propeller.properties["DIAMETER"].value / 1000
        shaft_prop: float = propeller.properties["SHAFT_DIAMETER"].value
        # print(table.get_value(rpm = 13000, v = 90, label="Cp"))
        table = table_dict[propeller]
        C_p: float = table.get_value(rpm=rpm, v=0, label="Cp")  # 0.0659
        C_t: float = table.get_value(rpm=rpm, v=0, label="Ct")  # 0.1319

        W_prop = propeller.properties["WEIGHT"].value
        return {
            "C_p": C_p,
            "C_t": C_t,
            "W_prop": W_prop,
            "diameter": diameter,
            "shaft_prop": shaft_prop,
            "name": propeller.id,
        }

    def hackthon_get_motor_property(self, motor):
        R_w: float = motor.properties["INTERNAL_RESISTANCE"].value / 1000  # Ohm
        K_t: float = motor.properties["KT"].value
        K_v: float = motor.properties["KV"].value * (2 * math.pi) / 60  # rpm/V to rad/(V-sec)
        I_idle: float = motor.properties["IO_IDLE_CURRENT_10V"].value
        W_motor = motor.properties["WEIGHT"].value
        I_max_motor = motor.properties["MAX_CURRENT"].value
        P_max_motor = motor.properties["MAX_POWER"].value
        shaft__motor: float = motor.properties["SHAFT_DIAMETER"].value
        return {
            "R_w": R_w,
            "K_t": K_t,
            "K_v": K_v,
            "I_idle": I_idle,
            "W_motor": W_motor,
            "I_max_motor": I_max_motor,
            "P_max_motor": P_max_motor,
            "shaft_motor": shaft__motor,
            "name": motor.id,
        }

    def hackthon_get_battery_property(self, battery, num_battery):
        capacity: float = battery.properties["CAPACITY"].value * num_battery / 1000  # mAh -> Ah
        W_batt: float = battery.properties["WEIGHT"].value * num_battery
        V_battery: float = battery.properties["VOLTAGE"].value
        I_max: float = (battery.properties["CONT_DISCHARGE_RATE"].value) * capacity  # convert to mA
        return {"capacity": capacity, "W_batt": W_batt, "V_battery": V_battery, "I_max": I_max, "name": battery.id}

    def set_rpm(self, rpm):
        self._rpm_static = rpm

if __name__ == "__main__":
    from sym_cps.representation.tools.parsers.parse import parse_library_and_seed_designs
    from sym_cps.representation.tools.parsers.parsing_prop_table import parsing_prop_table

    c_library, designs = parse_library_and_seed_designs()
    table_dict = parsing_prop_table(c_library)
    MotorPropellerAnalysis(table_dict=table_dict, c_library=c_library)


import sys
import numpy as np
import cv2

im = cv2.imread('CombinedTraining1.PNG')

gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray, 255, 1, 1, 11, 2)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

samples = np.empty((0, 400))
responses = []
keys = [i for i in range(48, 58)]

for area in contours:
    if cv2.contourArea(area) > 50:
        [x, y, width, height] = cv2.boundingRect(area)

        if 200 > height > 20 and width > 10:
            cv2.rectangle(im, (x, y), (x + width, y + height), (0, 0, 255), 2)
            roi = thresh[y:y + height, x:x + width]
            roi = cv2.resize(roi, (20, 20))
            cv2.imshow('norm', im)
            key = cv2.waitKey(0)

            if key == 27:  # (escape to quit)
                sys.exit()
            elif key in keys:
                responses.append(int(chr(key)))
                sample = roi.reshape((1, 400))
                samples = np.append(samples, sample, 0)

responses = np.array(responses, np.float32)
responses = responses.reshape((responses.size, 1))
print("training complete")

np.savetxt('samples.data', samples)
np.savetxt('responses.data', responses)


'''
Código para testar funcionalidades do Apache Airflow.
'''

from airflow import DAG
from airflow.operators.dummy_operator import DummyOperator
from airflow.utils.dates import days_ago
from airflow.operators.python_operator import PythonOperator
from airflow.operators.python_operator import BranchPythonOperator
from datetime import timedelta, datetime
import time

# DAG args set

default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'start_date': days_ago(2),
    # 'email': ['airflow@example.com'],
    # 'email_on_failure': False,
    # 'email_on_retry': False,
    'retries': 1,
    'retry_delay': timedelta(minutes=1),
    # 'queue': 'bash_queue',
    # 'pool': 'backfill',
    # 'priority_weight': 10,
    # 'end_date': datetime(2016, 1, 1),
    # 'wait_for_downstream': False,
    # 'dag': dag,
    # 'sla': timedelta(hours=2),
    # 'execution_timeout': timedelta(seconds=300),
    # 'on_failure_callback': some_function,
    # 'on_success_callback': some_other_function,
    # 'on_retry_callback': another_function,
    # 'sla_miss_callback': yet_another_function,
    # 'trigger_rule': 'all_success'
}

# Test variables
run_id = 12
number_of_files = 6
missing_files = ['arquivo1', 'arquivo2', 'arquivo3']

def upload_files():
    print("Files uploaded")

def is_file_enought(**kwargs):

    if number_of_files <= 10:  # Dummy check
        task_instance = kwargs['ti']
        m_files = task_instance.xcom_pull(key=('missing_files ' + str(run_id)))
        return 'table_of_missing_files'

    else:
        return 'processing_report_data'

def table_of_missing_files(**kwargs):

    task_instance = kwargs['ti']
    task_instance.xcom_push(key=('missing files ' + str(run_id)), value=missing_files)

    # Código para rodar a verificação dos arquivos novamente (com o mesmo run id)

def report_process():
    print("PROCESSANDO DADOS")
    print("PROCESSAMENTO TERMINADO")

def report_deploy():
    print("RELATORIO DE X, Y, Z")

# Main DAG code

with DAG('test_DAG', start_date=datetime(2020, 4, 14), schedule_interval='@once', default_args=default_args,
         catchup=False) as dag:
    files_upload = PythonOperator(task_id='files_upload', python_callable=upload_files)

    checking_number_of_files = BranchPythonOperator(task_id='checking_number_of_files', python_callable=is_file_enought, provide_context=True)

    processing_report_data = PythonOperator(task_id='processing_report_data', python_callable=report_process)

    report_deployment = PythonOperator(task_id='report_deployment', python_callable=report_deploy)

    table_of_missing_files = BranchPythonOperator(task_id='table_of_missing_files', python_callable=table_of_missing_files, provide_context=True)

    upload_retry = DummyOperator(task_id='upload_retry')

    files_upload >> checking_number_of_files >> processing_report_data >> report_deployment
    files_upload >> checking_number_of_files >> table_of_missing_files >> upload_retry >> processing_report_data


import time
import urllib.request
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

try:
    os.mkdir('downloads')
except FileExistsError:
    pass

#path to web driver chrome or firefox any browser. You can download that from selenium official site
browser = webdriver.Chrome("C:\\Users\\ACER\\Downloads\\chromedriver_win32\\chromedriver.exe")
browser.get("https://www.google.com/")
url = browser.current_url
#print(url)
name = browser.find_element_by_name('q')
name.send_keys('sushant singn rajput',Keys.ENTER)
elem = browser.find_element_by_link_text('Images')
elem.get_attribute('href')
elem.click()
value = 0
for i in range(20):
    #browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    #for scrolling down to load more images
    browser.execute_script("scrollBy("+ str(value) +",+1000);")
    value += 1000
    time.sleep(3)

elem1 = browser.find_element_by_id('rg_s')
sub = elem1.find_elements_by_tag_name("img")
count = 0
for i in sub:
    src = i.get_attribute('src')
    try:
        if src != None:
            src  = str(src)
            print(src)
            count+=1
            urllib.request.urlretrieve(src, os.path.join('downloads','image'+str(count)+'.jpg'))
        else:
            raise TypeError
    except TypeError:
        print('fail')