#!/usr/bin/env python
from sklearn.metrics import precision_recall_fscore_support
import numpy as numpy
import pandas as panda
import re
import nl_core_news_sm

from medstruct.classify.tnm.extractmeasurement import ExtractMeasurement

concept_tumor = "massa|tumor|nodu|densiteit|haarden|carcino|meningeno|laesie|letsel|weke delen|rip|maligniteit|afwijking" #afwijking
concept_kwab = "kwab"
unit_mm_pattern = r'mm|millimeter'
unit_cm_pattern = r'cm\b|centimeter\b'
unit_dm_pattern = r'dm\b|decimeter\b'
unit_pattern = unit_mm_pattern+'|'+unit_cm_pattern+'|'+unit_dm_pattern
number_attribute_pattern = r'(x|bij|maal|op|plus.*minus|ongeveer|om en nabij|a|circa|en)'
ima_attribute_pattern = r'(-|//)'
decrease_synonyms = 'afname|afgenomen|gekrompen'

trachea = "trachea|luchtpijp"
bronchus = "bronchus"
right_bronchus = "rechter bronchus"
left_bronchus = "left bronches"

lung = "lung|long"
lung_right = "pulmo dexter|recht.*long|long.*recht"
lung_left = "pulmo sinister|link.*long|long.*link"

superior_lobe = "bovenste longlob|bovenste kwab|bovenste lob"
superior_lobe_right = "recht.*boven.*(kwab|lob)|boven.*(lob|kwab).*recht|\bRBK\b|\bRBL\b"
# superior_lobe_left = "link.*boven.*(kwab|lob)|boven.*(lob|kwab).*link|\bLBK\b|\bLBL\b"
superior_lobe_left = "linker bovenkwab"

middle_lobe = "midde.*(lob|kwab)|\bMK\b|\bML\b"
inferior_lobe = "onderste longlob|onderste kwab|onderste lob"
inferior_lobe_right = "recht.*onder.*(kwab|lob)|onder.*(lob|kwab).*recht|\bROK\b|\bROL\b"
# inferior_lobe_left = "link.*onder.*(kwab|lob)|onder.*(lob|kwab).*link|\bLOK\b|\bLOL\b"
inferior_lobe_left = "linker onderkwab"

fissura_obliqua = ""
fissura_horizontalis = ""
arteria_pulmonalis = ""

multiple = "meerdere|verschillende|verscheidenen|tweetal|drietal|viertal|vijftal|zestal|zevental"
multiple_tumors = "noduli"

"apicaal"
"basaal"
"craniaal"
"caudaal"

#parietal pleural|PL3|borstwand|borstkaswand|thoraxwand|phrenic|parietal pericard|hartzak|longvlies|rib|spier|kraakbeen|diafragma

# t2_exceptions_manual = "inv.*pleur|pleur.*inv|hilu|hila|hilum pulmonis|(atelect|obstruct).*pneum|atelect"
t2_exceptions_atelect = "atelect"
t2_exceptions_main_bronchus = "(centrale|hoofd).*bronch"
t2_exceptions_visceral_pleura = "pleura\b"

t3_exceptions_borstwand = "borst.*wan|thorax.*wan"
t3_exceptions_parietale_pericard = "pericard"
t3_exceptions_nervus_phrenicus = "nervus.*phrenicus"
t3_exceptions_satellite_same_lobe = "satelliet"

t4_exceptions_mediastinum = "geen.*afgrenzing.*mediast|ingroei.*mediast"
t4_exceptions_diafragma = "diafragm|middenrif"
t4_exceptions_carina = "carina"

t4_exceptions_large_vessels = "grote vaten|aorta|vena cava|vat\b|arteri.*pul|ven.*pul"
t4_exceptions_trachea = "trachea|luchtpijp"
t4_exceptions_nervus_laryngeus_recurrens = "nervus.*laryngeus.*recurrens|strot|slijmvlies|stembanden"
t4_exceptions_oesophagus = "oesophagus|slok"
t4_exceptions_wervels = "wervel|bot|rib|kraak|cartilago|thoracicae|ruggengraat|columna|vertebra" #niet specifiek

# unit_pattern = r'(mm\b|millimeter\b|cm\b|centimeter\b|dm\b|decimeter\b)'

lympfe_filter = 'lymf|lymph'

#exclude lympfe?

class ReportT(object):

    t_column = 3

    def __init__(self):
        self.nlp = nl_core_news_sm.load()

    # Preprocess documents using spaCy's nlp pipeline
    def run_spacy(self, doc):
        return self.nlp(str(doc))

    # Create function that processes new dataframe in one go.
    def NLP_pipeline(self, df, report_column, t_column):
        """
        Input = dataframe where the first column contains all the raw text files and column 15 contains the label.

        Output = dataframe of 4 columns, namely:
                            - original raw text
                            - sentences with numbers
                            - sentences with sizes
                            - sizes

        Necessary packages: - numpy
                            - pandas
                            - re
                            - itertools
                            - spacy
                            - nl_core_news_sm
                            - precision_recall_fscore_support from sklearn.metrics

        """
        self.t_column = t_column
        self.report_column = report_column

        print("Preprocessing raw text...")
        result_df = df.iloc[:, self.report_column]
        result_df = result_df.apply(ExtractMeasurement.preprocess_raw_numbers_dictation_artifacts_correction)
        result_df = result_df.apply(ReportT.preprocess_raw_abbreviations)

        result_df = result_df.apply(self.run_spacy) # Preprocess raw text using spaCy's nlp function
        result_df = panda.concat([result_df, result_df.apply(get_num_sents)], axis=1) # Get sentences with numbers
        result_df.columns = ['document', 'num_sents'] # Rename columns
        result_df['size_sents'] = result_df['num_sents'].apply(get_size_sents) # Get sentences with sizes
        result_df['tumor_sents'] = result_df['document'].apply(get_tumor_sents) # Get sentences with 'tumor' synoniems

        result_df['tumor_sents_unit'] = result_df['num_sents'].apply(ExtractMeasurement.get_measurements_length)
        result_df['tumor_sents_cm'] = result_df['tumor_sents_unit'].apply(ExtractMeasurement.get_measurements_in_cm)
        result_df["class_size_cm"] = result_df["tumor_sents_cm"].apply(ExtractMeasurement.get_max_size)

        result_df["two_tumors"] = result_df.iloc[:, 0].apply(has_tumors_in_multiple_lobes_same_lung)

        print("Extracting T classification...")
        result_df["naive_T_class"] = result_df["class_size_cm"].apply(get_naive_t_class)
        result_df["naive_T_class_simple"] = result_df["naive_T_class"].apply(get_t_score_label)

        result_df["T2"] = result_df['tumor_sents'].apply(t2_classification)

        result_df["T3"] = result_df.iloc[:,0].apply(t3_classification)
        result_df["T3+naive_class"] = numpy.where(
            (result_df["T3"] == "T3") & (result_df["naive_T_class_simple"] != "T4"), "T3",
            result_df["naive_T_class_simple"])

        result_df["T4"] = result_df.iloc[:,0].apply(t4_classification)
        result_df["T4+naive_class"] = numpy.where(result_df["T4"] == "T4", "T4", result_df["naive_T_class_simple"])

        result_df["T4+T3+naive_class"] = result_df.apply(lambda x: get_class(x['naive_T_class_simple'], x['T2'], x['T3'], x['T4']), axis=1)
        result_df = panda.concat([result_df, df.iloc[:, self.t_column]], axis=1)
        result_df = result_df.rename(columns={result_df.columns[-1]:'label'})
        result_df["label_T"] = result_df["label"].apply(get_t_score_label)

        print("Computing evaluation metrics...")
        acc_df = result_df[result_df["label_T"] != "?"] # Filter dataframe on reports that have a valid label
        print("")
        print("ACCURACY RESULTS FROM VARIOUS METHODS:")
        print("-----------------------------------------------")
        acc_naive = acc_df[acc_df["label_T"] == acc_df["naive_T_class_simple"]].shape[0]/acc_df.shape[0] # Compute accuracy
        print("Naive classification (only tumor measurements):")
        print("Accuracy = " + str(round(acc_naive*100, 2)) + "%") # Print result in percentages
        print("-----------------------------------------------")
        acc_t4 = acc_df[acc_df["T4+naive_class"] == acc_df["label_T"]].shape[0]/acc_df.shape[0] # Compute accuracy
        print("Classification using additional T4 rules:")
        print("Accuracy = " + str(round(acc_t4*100, 2)) + "%") # Print result in percentages
        print("-----------------------------------------------")
        acc_t3 = acc_df[acc_df["T3+naive_class"] == acc_df["label_T"]].shape[0]/acc_df.shape[0] # Compute accuracy
        print("Classification using additional T3 rules:")
        print("Accuracy = " + str(round(acc_t3*100, 2)) + "%") # Print result in percentages
        print("-----------------------------------------------")
        acc_t4_t3 = acc_df[acc_df["T4+T3+naive_class"] == acc_df["label_T"]].shape[0]/acc_df.shape[0] # Compute accuracy
        print("Classification using additional T4 & T3 rules:")
        print("Accuracy = " + str(round(acc_t4_t3*100, 2)) + "%") # Print result in percentages
        print("-----------------------------------------------")
        print("")

        y_actu = panda.Series(acc_df['label_T'], name='Actual')
        y_pred = panda.Series(acc_df['naive_T_class_simple'], name='Predicted')
        df_confusion = panda.crosstab(y_actu, y_pred, rownames=['Actual'], colnames=['Predicted'], margins=True)
        print("CONFUSION MATRIX OF CLASSIFICATION USING ADDITIONAL naive_T_class_simple:")
        print("------------------------------------------------------------------")
        print("")
        print(df_confusion)

        y_actu = panda.Series(acc_df['label_T'], name='Actual')
        y_pred = panda.Series(acc_df['T4+T3+naive_class'], name='Predicted')
        df_confusion = panda.crosstab(y_actu, y_pred, rownames=['Actual'], colnames=['Predicted'], margins=True)
        print("CONFUSION MATRIX OF CLASSIFICATION USING ADDITIONAL T4 & T3 RULES:")
        print("------------------------------------------------------------------")
        print("")
        print(df_confusion)
        print("")
        prf = precision_recall_fscore_support(y_actu, y_pred, average=None, labels=['T1', 'T2', 'T3', 'T4'])
        evaldf = panda.DataFrame({"Precision": prf[0], "Recall": prf[1], "F-score": prf[2]}, index=["T1", "T2", "T3", "T4"])
        print(evaldf)
        print("-------------------------------------------------------------------")

        return result_df

    @staticmethod
    def preprocess_raw_abbreviations(text):
        """
        Map abbreviations targeting numbers to its full form
        :param text including abbrevations
        :return: text including full form of selected abbreviations
        """
        abbreviations = {"ca.": "circa"}
        for key in abbreviations:
            text = text.replace(key, abbreviations[key])
        return text

# Extract all sentences from a document that contain numerical tokens using spaCy's PoS tagger
def get_num_sents(doc):
    sentlist = list()
    for sent in list(doc.sents):
        if 'NUM' in [token.pos_ for token in sent]:
            sentlist.append(sent)
    return sentlist

# Extract all sentences that contain 'mm', 'cm', '...meter'.
# mm can't be followed by another letter to ensure we don't include sentences with mmol in result.
def get_size_sents(sentences):
    sizelist = list()
    for sent in filter_out_lymph(sentences):
        if len(re.findall(r'('+unit_pattern+')', sent.string)) > 0:
            sizelist.append(sent)
    return sizelist

def get_tumor_size_sents(sentences):
    tumorlist = list()
    for sent in sentences:
        if len(re.findall(r'('+concept_tumor+')', sent.string, re.IGNORECASE)) > 0:
            tumorlist.append(sent)
    return tumorlist

def get_tumor_sents(doc):
    tumorlist = list()
    for sent in list(doc.sents):
        if len(re.findall(r'('+concept_tumor+')', sent.string, re.IGNORECASE)) > 0:
            tumorlist.append(sent)
    return tumorlist

def has_multiple_tumors(doc):
    tumor_sents = remove_negex_sents(get_tumor_sents(doc))
    for sent in tumor_sents:
        if re.search(r'('+multiple_tumors+')', sent.string, re.IGNORECASE) \
                or re.search(r'('+multiple+')', sent.string, re.IGNORECASE):
            return True
    return False

def filter_out_lymph(sents):
    clean = list()
    for sent in sents:
        if not re.search(r'('+lympfe_filter+')', sent.string, re.IGNORECASE):
            clean.append(sent)
    return clean

def has_tumors_in_multiple_lobes_same_lung(doc):
    tumor_sent = remove_uncertain_sents(remove_negex_sents(get_tumor_sents(doc)))

    for sent in tumor_sent:
        slr_found = 0
        sll_found = 0
        ml_found = 0
        irl_found = 0
        ill_found = 0

        if re.search(r'('+superior_lobe_right+')', sent.string, re.IGNORECASE):
            slr_found = 1
        if re.search(r'('+superior_lobe_left+')', sent.string, re.IGNORECASE):
            sll_found = 1
        if re.search(r'('+middle_lobe+')', sent.string, re.IGNORECASE):
            ml_found = 1
        if re.search(r'('+inferior_lobe_right+')', sent.string, re.IGNORECASE):
            irl_found = 1
        if re.search(r'('+inferior_lobe_left+')', sent.string,  re.IGNORECASE):
            ill_found = 1

        multiple_lobes_right = slr_found + irl_found + ml_found
        multiple_lobes_left = sll_found + ill_found
        multiple_lobes_same_lung = (multiple_lobes_right > 1) or (multiple_lobes_left > 1)
        if multiple_lobes_same_lung:
            print("multiple_lobes", multiple_lobes_same_lung)
            return True

    return False

def has_multiple_tumors_in_same_lobe(doc):
    tumor_sent = remove_negex_sents(get_tumor_sents(doc))

    ull_found = 0
    url_found = 0
    ml_found = 0
    lrl_found = 0
    lll_found = 0

    for sent in tumor_sent:
        if re.search(r'('+superior_lobe_right+')', sent.string, re.IGNORECASE):
            url_found = 1
        if re.search(r'('+superior_lobe_left+')', sent.string, re.IGNORECASE):
            ull_found = 1
        if re.search(r'('+middle_lobe+')', sent.string, re.IGNORECASE):
            ml_found = 1
        if re.search(r'('+inferior_lobe_right+')', sent.string, re.IGNORECASE):
            lrl_found = 1
        if re.search(r'('+inferior_lobe_left+')', sent.string,  re.IGNORECASE):
            lll_found = 1

    multiple_lobes_right = ull_found + url_found + ml_found > 1
    multiple_lobes_left = lrl_found + lll_found > 1

    multiple_lobes_same_lung = multiple_lobes_right > 1 or multiple_lobes_left > 1
    if multiple_lobes_same_lung:
        print("multiple_lobes", multiple_lobes_same_lung)
    return multiple_lobes_same_lung

def get_class(naive, t2, t3, t4):
    if naive == "T4" or t4 == "T4":
        return "T4"
    if naive == "T3" or t3 == "T3":
        return "T3"
    if naive == "T2" or t2 == "T2":
        return "T2"
    return "T1"

def get_naive_t_class(max_size):
    if type(max_size) == float:
        if 7 < max_size <= 40: # Sanity check in case max_size is an illogical value
            return 'T4'
        elif 5 < max_size <= 7:
            return 'T3'
        elif 4 < max_size <= 5:
            return 'T2b'
        elif 3 < max_size <= 4:
            return 'T2a'
        elif 2 < max_size <= 3:
            return 'T1c'
        elif 1 < max_size <= 2:
            return 'T1b'
        elif 0 < max_size <= 1:
            return 'T1a'
        else:
            return '?'
    else:
        return '?'

def t2_classification(sents):
    t2list = t2_exceptions(sents)
    if len(t2list) > 0:
        tclass = "T2"
    else:
        tclass = "?"
    return tclass

def t3_classification(doc):
    t3list = t3_exceptions(doc)
    t3tumorlist = get_tumor_size_sents(t3list)
    nonneglist = remove_negex_sents(t3tumorlist)
    if len(nonneglist) > 0:
        tclass = "T3"
    else:
        tclass = "?"
    return tclass

def t4_classification(doc):
    t4list = t4_exceptions(doc)
    t4tumorlist = get_tumor_size_sents(t4list)
    nonneglist = remove_negex_sents(t4tumorlist)
    if len(nonneglist) > 0:
        tclass = "T4"
    elif has_tumors_in_multiple_lobes_same_lung(doc):
        tclass = "T4"
    else:
        tclass = "?"
    return tclass

def t2_exceptions(sents):
    t2list = list()
    regex = t2_exceptions_atelect \
            + "|" + t2_exceptions_main_bronchus \
            + "|" + t2_exceptions_visceral_pleura

    for sent in sents:
        if len(re.findall(r'('+regex+')', sent.string, re.IGNORECASE)) > 0:
            t2list.append(sent)
    return t2list

# Detect exception areas for T3 Classification
def t3_exceptions(doc):
    t3list = list()

    regex = t3_exceptions_borstwand \
            + "|" + t3_exceptions_nervus_phrenicus \
            + "|" + t3_exceptions_parietale_pericard \
            + "|" + t3_exceptions_satellite_same_lobe

    for sent in get_tumor_sents(doc):
        if len(re.findall(r'('+regex+')', sent.string, re.IGNORECASE)) > 0:
            t3list.append(sent)
    return t3list

# Detect exception areas for T4 Classification
def t4_exceptions(doc):
    t4list = list()

    regex = t4_exceptions_diafragma \
            + "|" + t4_exceptions_mediastinum

    for sent in get_tumor_sents(doc):
        if len(re.findall(r'('+regex+')', sent.string, re.IGNORECASE)) > 0:
            t4list.append(sent)
    return t4list

# Detect negations for T3/T4 exceptions
def remove_negex_sents(sentlist):
    resultlist = list()
    for sent in sentlist:
        if len(re.findall(r'(geen|niet|normaal|zonder|conform|ongewijzigd)', sent.string, re.IGNORECASE)) == 0:
            resultlist.append(sent)
        if len(re.findall(r'(geen.*afgrenzing)', sent.string, re.IGNORECASE)) > 0:
            resultlist.append(sent)
    return resultlist

def remove_uncertain_sents(sentlist):
    resultlist = list()
    for sent in sentlist:
        if len(re.findall(r'(verdenking|lijkt|mogelijk)', sent.string, re.IGNORECASE)) == 0:
            resultlist.append(sent)
    return resultlist

# Extract first two characters from TNM code
def get_t_score_label(label):
    return str(label)[:2] if (str(label)[0] == "T") and ((str(label)[1] == "1") or (str(label)[1] == "2") or (str(label)[1] == "3") or (str(label)[1] == "4")) else "?"


'''
TCP是建立可靠的连接并且通信双方都可以以流的方式发送数据
UDP是不需要建立连接，只需要知道对方的IP和端口就可以直接发送数据，但是能否到达不确定，虽然不可靠但是速度快
对于要求不高的数据可以使用UDP，是主要做网络广播
流程：服务器：sockt()->bind()->readform()->阻塞，等待客户数->处理服务请求->sendto()---服务应答--▽--->close()
    客户端：socket()->bind()->sendto()------服务请求---------^----------------------readform()--->close()
'''
import socket
import time
udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp.connect("此处输入IP和端口号")
udp.send("i am a good man".encode("utf-8"))
while True:
    udp.sendto("i am not a good man".encode("utf-8"))
    time.sleep(1)


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

from odoo import models, fields, api, _
from odoo.exceptions import AccessError, UserError, RedirectWarning, ValidationError, Warning
import logging

_logger = logging.getLogger(__name__)

class account_analytic_line(models.Model):
    _inherit='account.analytic.line'

    journal_id=fields.Many2one('account.analytic.journal', string='Analytic Journal')

    def _timesheet_preprocess(self, vals):

        vals=super(account_analytic_line,self)._timesheet_preprocess(vals)
        # employee implies analytic journal
        if vals.get('employee_id'):
            employee = self.env['hr.employee'].browse(vals['employee_id'])
            vals['journal_id'] = employee.journal_id.id
        return vals

    @api.model
    def create(self, values):
        # compute employee only for timesheet lines, makes no sense for other lines
        if not values.get('employee_id') and values.get('project_id'):
            if values.get('user_id'):
                ts_user_id = values['user_id']
            else:
                ts_user_id = self._default_user()
            values['employee_id'] = self.env['hr.employee'].search([('user_id', '=', ts_user_id),'|',('active','=',False),('active','=',True)], limit=1).id
        _logger.info("values1=%s",values)
        values = self._timesheet_preprocess(values)
        _logger.info("values2=%s",values)
        result = super(account_analytic_line, self).create(values)
        if result.project_id:  # applied only for timesheet
            result._timesheet_postprocess(values)
        return result

class account_analytic_journal(models.Model):
    _name = 'account.analytic.journal'
    _description = 'Analytic Journal'

    name=fields.Char('Journal Name', required=True)
    code=fields.Char('Journal Code', size=8)
    active=fields.Boolean('Active', help="If the active field is set to False, it will allow you to hide the analytic journal without removing it.", default=True)
    type=fields.Selection([('sale','Sale'), ('purchase','Purchase'), ('cash','Cash'), ('general','General'), ('situation','Situation')], 'Type', default='general', required=True, help="Gives the type of the analytic journal. When it needs for a document (eg: an invoice) to create analytic entries, Odoo will look for a matching journal of the same type.")
    line_ids=fields.One2many('account.analytic.line', 'journal_id', 'Lines', copy=False)
    company_id=fields.Many2one('res.company', 'Company', required=True, default=lambda self: self.env.user.company_id)

class hr_employee(models.Model):
    _inherit='hr.employee'

    journal_id=fields.Many2one('account.analytic.journal', string='Analytic Journal')

class account_journal(models.Model):
    _inherit="account.journal"

    analytic_journal_id=fields.Many2one('account.analytic.journal','Analytic Journal', help="Journal for analytic entries")

class account_move(models.Model):
    _inherit='account.move'

    line_ids = fields.One2many('account.move.line', 'move_id', string='Journal Items',states={'posted': [('readonly', False)]}, copy=True)

class account_move_line(models.Model):
    _inherit='account.move.line'

    analytic_journal_id=fields.Many2one('account.analytic.journal','Analytic Journal', help="Journal for analytic entries")

    @api.one
    def _prepare_analytic_line(self):
        res=super(account_move_line,self)._prepare_analytic_line()

        if not self.analytic_journal_id:
            raise ValidationError(_("Move line does not have an analytic journal specified. Cannot create the analytic line"))

        for line in res:
            line['journal_id']=self.analytic_journal_id.id
        return res

    @api.multi
    def write(self, vals):

        self.env.context = dict(self.env.context)
        allow_analytic_update=True
        if any(key in vals for key in ('account_id', 'journal_id', 'date', 'move_id', 'debit', 'credit')) and not any(key in vals for key in ('analytic_account_id', 'analytic_journal_id')):
            allow_analytic_update=False
        self.env.context.update({'allow_analytic_update': allow_analytic_update})

        _logger.info("allowup=%s,%s",allow_analytic_update,vals)

        res = super(account_move_line, self).write(vals)
        if 'analytic_account_id' in vals or 'analytic_journal_id' in vals and self.move_id.state=='posted':
            self.env.context.update({'allow_analytic_update': allow_analytic_update})
            self.mapped('analytic_line_ids').unlink()
            self.create_analytic_lines()
        return res

    @api.multi
    def _update_check(self):
        """ Raise Warning to cause rollback if the move is posted, some entries are reconciled or the move is older than the lock date"""
        move_ids = set()
        for line in self:

            err_msg = _('Move name (id): %s (%s)') % (line.move_id.name, str(line.move_id.id))
            if line.move_id.state != 'draft' and not self._context.get('allow_analytic_update', False):
                raise UserError(_('You cannot do this modification on a posted journal entry, you can just change some non legal fields. You must revert the journal entry to cancel it.\n%s.') % err_msg)
            if line.reconciled and not (line.debit == 0 and line.credit == 0):
                raise UserError(_('You cannot do this modification on a reconciled entry. You can just change some non legal fields or you must unreconcile first.\n%s.') % err_msg)
            if line.move_id.id not in move_ids:
                move_ids.add(line.move_id.id)
        self.env['account.move'].browse(list(move_ids))._check_lock_date()
        return True

class AccountInvoice(models.Model):
    _inherit='account.invoice'

    @api.model
    def invoice_line_move_line_get(self):
        res = super(AccountInvoice, self).invoice_line_move_line_get()

        for dict_data in res:
            dict_data['analytic_journal_id'] = self.journal_id.analytic_journal_id.id

        return res

    @api.model
    def tax_line_move_line_get(self):
        res = super(AccountInvoice, self).tax_line_move_line_get()

        for dict_data in res:
            dict_data['analytic_journal_id'] = self.journal_id.analytic_journal_id.id

        _logger.info("restaxline=%s",res)
        return res

#apiux override this method in account/product.py to include invl_id when groupedlines on journal is false
class ProductProduct(models.Model):
    _inherit = "product.product"

    @api.model
    def _convert_prepared_anglosaxon_line(self, line, partner):

        res=super(ProductProduct,self)._convert_prepared_anglosaxon_line(line, partner)
        res['analytic_journal_id']=line.get('analytic_journal_id', False)
        return res


#calss header
class _GRANDSTAND():
	def __init__(self,):
		self.name = "GRANDSTAND"
		self.definitions = [u'a set of seats arranged in rising rows, sometimes covered by a roof, from which people can easily watch sports or other events']

		self.parents = []
		self.childen = []
		self.properties = []
		self.jsondata = {}

		self.specie = 'nouns'

	def run(self, obj1 = [], obj2 = []):
		return self.jsondata


from pygame import display

class Settings():
    """Класс для хранения всех настроек игры Alien Invansion"""

    def __init__(self):
        """Инициализирует статические настройки игры"""
        #Парамаетры экрана.
        self.screen_width = 1200            #display.Info().current_w
        self.screen_heigth = 900            #display.Info().current_h
        self.bg_collor = (170, 230, 230)
        #Настройки коробля
        self.ship_limit = 3
        #Параметры пули
        self.bullet_width = 10
        self.bullet_heigth = 30
        self.bullet_color = 60, 60, 60
        self.bullet_allowed = 100
        # Настройки пришельцев
        self.fleet_drop_speed = 15

        #Темп ускорения игры
        self.speedup_scale = 1.2

        #Темп роста стоймости пришельца
        self.score_scale = 1.5

        self.initialize_dynamic_settings()

    def initialize_dynamic_settings(self):
        """Инициализирует настройки, изхменяющиеся в ходе игры"""
        self.ship_speed_factory = 1.5
        self.bullet_speed_factory = 3
        self.alien_speed_factor = 1

        # fleet_direction = 1 обозначает движение вправо; а -1 - влево
        self.fleet_direction = 1

        #Подсчет очков
        self.alien_points = 50

    def increase_speed(self):
        """Увеличивает настройки скорости"""
        self.ship_speed_factory *= self.speedup_scale
        self.bullet_speed_factory *= self.speedup_scale
        self.alien_speed_factor *= self.speedup_scale
        self.alien_points = int(self.alien_points * self.score_scale)
        print(self.alien_points)


# -*- coding: utf-8 -*-
# coders Nikolasi
from Tools.LoadPixmap import LoadPixmap
from Components.Pixmap import Pixmap
from Renderer import Renderer
from enigma import ePixmap, loadPic, eTimer, iServiceInformation, iPlayableService, eDVBFrontendParametersSatellite, eDVBFrontendParametersCable
from Tools.Directories import fileExists, SCOPE_SKIN_IMAGE, SCOPE_CURRENT_SKIN, resolveFilename
from Components.config import *
from Components.Converter.Poll import Poll

class PiconRefPS2(Renderer, Poll):
    searchPaths = ('/usr/share/enigma2/%s/', '/media/ba/%s/', '/media/hdd/%s/', '/media/sda1/%s/', '/media/sda/%s/', '/media/usb/%s/')

    def __init__(self):
        Poll.__init__(self)
        Renderer.__init__(self)
        self.control = 1
        self.piconWidth = 100
        self.piconHeight = 60
        self.path = 'picon'
        self.nameCache = {}
        self.nameCache1 = {}
        self.nameCache2 = {}
        self.pngname = ''
        self.pngname1 = ''
        self.pngname2 = ''
        self.timerpicsPS = eTimer()
        self.timerpicsPS.callback.append(self.timerpicsPSEvent)
        self.timerpicsPS1 = eTimer()
        self.timerpicsPS1.callback.append(self.timerpicsPSEvent1)

    def applySkin(self, desktop, parent):
        attribs = []
        for attrib, value in self.skinAttributes:
            if attrib == 'path':
                self.path = value
            elif attrib == 'piconWidth':
                self.piconWidth = int(value)
            elif attrib == 'piconHeight':
                self.piconHeight = int(value)
            elif attrib == 'control':
                self.control = int(value)
            else:
                attribs.append((attrib, value))

        self.skinAttributes = attribs
        return Renderer.applySkin(self, desktop, parent)

    GUI_WIDGET = ePixmap

    def changed(self, what):
        self.poll_interval = 3000
        self.poll_enabled = True
        sname = ''
        sname_ref = ''
        sname_rpov = ''
        sname_sat = ''
        if self.instance:
            pngname = ''
            if what[0] != self.CHANGED_CLEAR:
                text = self.source.text
                if text:
                    self.path = 'picon'
                    if ',,,' in text:
                        sname_ref, sname_rpov, sname_sat = text.split(',,,')
                    else:
                        sname_ref = text
                    swt = True
                    if swt == True:
                        sname2 = '%s' % (sname_ref)
                        pos = sname_ref.rfind(':')
                        if pos != -1:
                            sname = '_'.join(sname_ref.split(':', 10)[:10])
                        pngname = self.nameCache.get(sname, '')
                        if pngname == '':
                            pngname = self.findPicon(sname)
                            if pngname != '':
                                self.nameCache[sname] = pngname
                        if pngname == '':
                            pngname = self.nameCache.get('default', '')
                            if pngname == '':
                                pngname = self.findPicon('picon_default')
                                if pngname == '':
                                    tmp = resolveFilename(SCOPE_CURRENT_SKIN, 'picon_default.png')
                                    if fileExists(tmp):
                                        pngname = tmp
                                    else:
                                        pngname = resolveFilename(SCOPE_SKIN_IMAGE, 'skin_default/picon_default.png')
                                self.nameCache['default'] = pngname
                        if self.pngname != pngname:
                            self.pngname = pngname
                        if SCOPE_CURRENT_SKIN:
                                provname = sname_rpov
                                pngname1 = ''
                                #provname = '%s' % (provname)
                                if 'tvshka' in sname2:
                                    sname = "SCHURA"
                                elif 'vsadmin' in sname2:
                                    sname = "Vsadmin"
                                else:
                                    if provname.startswith('T-K'):
                                        sname = 'T-KABEL'
                                    elif provname == 'T-Systems/MTI':
                                        sname = 'T-SYSTEMS'
                                    else:
                                        sname = provname.upper()
                                pngname1 = self.nameCache1.get(sname, '')
                                self.path = 'piconProv'
                                if pngname1 == '':
                                    pngname1 = self.findPicon(sname)
                                    if pngname1 != '':
                                        self.nameCache1[sname] = pngname1
                                if pngname1 == '':
                                    pngname1 = self.nameCache1.get('default', '')
                                    if pngname1 == '':
                                        pngname1 = self.findPicon('picon_default')
                                        if pngname1 == '':
                                            tmp = resolveFilename(SCOPE_CURRENT_SKIN, 'picon_default.png')
                                            if fileExists(tmp):
                                                pngname1 = tmp
                                            else:
                                                pngname1 = resolveFilename(SCOPE_SKIN_IMAGE, 'skin_default/picon_default.png')
                                        self.nameCache1['default'] = pngname1
                                if self.pngname1 != pngname1:
                                    self.pngname1 = pngname1
                                if 'http' in sname2:
                                       sname = "00E"
                                else:
                                       sname = sname_sat
                                       sname = sname.replace('.', '').replace('°', '')
                                pngname2 = self.nameCache2.get(sname, '')
                                self.path = 'piconSat'
                                if pngname2 == '':
                                    pngname2 = self.findPicon(sname)
                                    if pngname2 != '':
                                        self.nameCache2[sname] = pngname2
                                if pngname2 == '':
                                    pngname2 = self.nameCache2.get('default', '')
                                    if pngname2 == '':
                                        pngname2 = self.findPicon('picon_default')
                                        if pngname2 == '':
                                            tmp = resolveFilename(SCOPE_CURRENT_SKIN, 'picon_default.png')
                                            if fileExists(tmp):
                                                pngname2 = tmp
                                            else:
                                                pngname2 = resolveFilename(SCOPE_SKIN_IMAGE, 'skin_default/picon_default.png')
                                        self.nameCache2['default'] = pngname2
                                if self.pngname2 != pngname2:
                                    self.pngname2 = pngname2
                                if self.control == 1:
                                    self.runanim2(pngname, pngname1, pngname2)
                                else:
                                    self.runanim1(pngname1, pngname2)

    def findPicon(self, serviceName):
        for path in self.searchPaths:
            pngname = path % self.path + serviceName + '.png'
            if fileExists(pngname):
                return pngname

        return ''

    def runanim1(self, pic1, pic2):
        self.slide = 2
        self.steps = 9
        self.pics = []
        self.pics.append(loadPic(pic1, self.piconWidth, self.piconHeight, 0, 0, 0, 1))
        self.pics.append(loadPic(pic2, self.piconWidth, self.piconHeight, 0, 0, 0, 1))
        self.timerpicsPS1.start(100, True)

    def timerpicsPSEvent1(self):
        if self.steps != 0:
            self.timerpicsPS1.stop()
            self.instance.setPixmap(self.pics[self.slide - 1])
            self.steps = self.steps - 1
            self.slide = self.slide - 1
            if self.slide == 0:
                self.slide = 2
            self.timerpicsPS1.start(1000, True)
        else:
            self.timerpicsPS1.stop()
            self.instance.setScale(1)
            self.instance.setPixmapFromFile(self.pngname)

    def runanim2(self, pic1, pic2, pic3):
        self.slide = 3
        self.steps = 9
        self.pics = []
        self.pics.append(loadPic(pic3, self.piconWidth, self.piconHeight, 0, 0, 0, 1))
        self.pics.append(loadPic(pic2, self.piconWidth, self.piconHeight, 0, 0, 0, 1))
        self.pics.append(loadPic(pic1, self.piconWidth, self.piconHeight, 0, 0, 0, 1))
        self.timerpicsPS.start(100, True)

    def timerpicsPSEvent(self):
        if self.steps != 0:
            self.timerpicsPS.stop()
            self.instance.setPixmap(self.pics[self.slide - 1])
            self.steps = self.steps - 1
            self.slide = self.slide - 1
            if self.slide == 0:
                self.slide = 3
            self.timerpicsPS.start(1000, True)
        else:
            self.timerpicsPS.stop()
            self.instance.setScale(1)
            self.instance.setPixmapFromFile(self.pngname)


import torch
import torch.nn as nn
import torch.nn.functional as F
import random
from torch.distributions import Beta

class Flatten(nn.Module):
    def __init__(self):
        super(Flatten, self).__init__()

    def forward(self, x):
        return x.view(x.size(0), -1)

class Conv_Standard(nn.Module):
    def __init__(self, args, x_dim, hid_dim, z_dim, final_layer_size):
        super(Conv_Standard, self).__init__()
        self.args = args
        self.net = nn.Sequential(self.conv_block(x_dim, hid_dim), self.conv_block(hid_dim, hid_dim),
                                 self.conv_block(hid_dim, hid_dim), self.conv_block(hid_dim, z_dim))
        self.dist = Beta(torch.FloatTensor([2]), torch.FloatTensor([2]))
        self.hid_dim = hid_dim

        self.logits = nn.Linear(final_layer_size, self.args.num_classes)

    def conv_block(self, in_channels, out_channels):
        return nn.Sequential(
            nn.Conv2d(in_channels, out_channels, 3, padding=1),
            nn.BatchNorm2d(out_channels),
            nn.ReLU(),
            nn.MaxPool2d(2)
        )

    def functional_conv_block(self, x, weights, biases,
                              bn_weights, bn_biases, is_training):
        x = F.conv2d(x, weights, biases, padding=1)
        x = F.batch_norm(x, running_mean=None, running_var=None, weight=bn_weights, bias=bn_biases,
                         training=is_training)
        x = F.relu(x)
        return x

    def mixup_data(self, xs, ys, xq, yq):
        query_size = xq.shape[0]

        shuffled_index = torch.randperm(query_size)

        xs = xs[shuffled_index]
        ys = ys[shuffled_index]
        lam = self.dist.sample().cuda()
        mixed_x = lam * xq + (1 - lam) * xs

        return mixed_x, yq, ys, lam

    def forward(self, x):
        x = self.net(x)

        x = torch.mean(x, dim=(2, 3))

        return self.logits(x)

    def functional_forward(self, x, weights, is_training=True):
        for block in range(4):
            x = self.functional_conv_block(x, weights[f'net.{block}.0.weight'], weights[f'net.{block}.0.bias'],
                                           weights.get(f'net.{block}.1.weight'), weights.get(f'net.{block}.1.bias'),
                                           is_training)

        x = torch.mean(x, dim=(2, 3))

        x = F.linear(x, weights['logits.weight'], weights['logits.bias'])

        return x

    def channel_shuffle(self, hidden, label, shuffle_dict, shuffle_channel_id):
        concept_idx = [0, 12, 22, 32, 44, 54, 64]

        new_data = []

        start = concept_idx[shuffle_channel_id]
        end = concept_idx[shuffle_channel_id + 1]

        for i in range(self.args.num_classes):
            cur_class_1 = hidden[label == i]
            cur_class_2 = hidden[label == shuffle_dict[i]]

            new_data.append(
                torch.cat((cur_class_1[:, :start], cur_class_2[:, start:end], cur_class_1[:, end:]), dim=1))

        new_data = torch.cat(new_data, dim=0)

        indexes = torch.randperm(new_data.shape[0])

        new_data = new_data[indexes]
        new_label = label[indexes]

        return new_data, new_label

    def forward_metamix(self, hidden_support, label_support, hidden_query, label_query, weights, is_training=True):

        sel_layer = random.randint(0, 3)
        flag = 0

        for layer in range(4):
            if layer == sel_layer:
                hidden_query, reweighted_query, reweighted_support, lam = self.mixup_data(hidden_support, label_support,
                                                                                hidden_query, label_query)
                flag = 1

            if not flag:
                hidden_support = self.functional_conv_block(hidden_support, weights[f'net.{layer}.0.weight'],
                                                            weights[f'net.{layer}.0.bias'],
                                                            weights.get(f'net.{layer}.1.weight'),
                                                            weights.get(f'net.{layer}.1.bias'),
                                                            is_training)
            hidden_query = self.functional_conv_block(hidden_query, weights[f'net.{layer}.0.weight'],
                                                      weights[f'net.{layer}.0.bias'],
                                                      weights.get(f'net.{layer}.1.weight'),
                                                      weights.get(f'net.{layer}.1.bias'),
                                                      is_training)

        hidden_query = torch.mean(hidden_query, dim=(2, 3))

        x = F.linear(hidden_query, weights['logits.weight'], weights['logits.bias'])

        return x, reweighted_query, reweighted_support, lam

    def functional_forward_cf(self, hidden, label, sel_layer, shuffle_list, shuffle_channel_id, weights,
                                           is_training=True):

        label_new = label

        for layer in range(4):
            if layer == sel_layer:
                hidden, label_new = self.channel_shuffle(hidden, label, shuffle_list, shuffle_channel_id)

            hidden = self.functional_conv_block(hidden, weights[f'net.{layer}.0.weight'],
                                                weights[f'net.{layer}.0.bias'],
                                                weights.get(f'net.{layer}.1.weight'),
                                                weights.get(f'net.{layer}.1.bias'),
                                                is_training)

        hidden = torch.mean(hidden, dim=(2, 3))

        x = F.linear(hidden, weights['logits.weight'], weights['logits.bias'])

        return x, label_new

    def mix_cf(self, hidden_support, label_new_support, hidden_query, label_new_query, shuffle_list,
               shuffle_channel_id):
        hidden_support, label_new_support = self.channel_shuffle(hidden_support, label_new_support, shuffle_list,
                                                                 shuffle_channel_id)
        hidden_query, label_new_query = self.channel_shuffle(hidden_query, label_new_query, shuffle_list,
                                                             shuffle_channel_id)

        hidden_query, label_new_query, label_new_support, lam = self.mixup_data(hidden_support, label_new_support, hidden_query,
                                                        label_new_query)

        return hidden_support, label_new_support, hidden_query, label_new_query, lam

    def functional_forward_cf_mix_query(self, hidden_support, label_support, hidden_query, label_query, sel_layer,
                                                 shuffle_list, shuffle_channel_id, weights,
                                                 is_training=True):

        flag = 0

        for layer in range(4):
            if layer == sel_layer:
                hidden_support, label_new_support, hidden_query, label_new_query, lam = self.mix_cf(hidden_support,
                                                                                label_support,
                                                                                hidden_query,
                                                                                label_query,
                                                                                shuffle_list,
                                                                                shuffle_channel_id)
                flag = 1

            if not flag:
                hidden_support = self.functional_conv_block(hidden_support, weights[f'net.{layer}.0.weight'],
                                                            weights[f'net.{layer}.0.bias'],
                                                            weights.get(f'net.{layer}.1.weight'),
                                                            weights.get(f'net.{layer}.1.bias'),
                                                            is_training)

            hidden_query = self.functional_conv_block(hidden_query, weights[f'net.{layer}.0.weight'],
                                                      weights[f'net.{layer}.0.bias'],
                                                      weights.get(f'net.{layer}.1.weight'),
                                                      weights.get(f'net.{layer}.1.bias'),
                                                      is_training)

        hidden_query = torch.mean(hidden_query, dim=(2, 3))

        x = F.linear(hidden_query, weights['logits.weight'], weights['logits.bias'])

        return x, label_new_query, label_new_support, lam


import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.optim import Adam
from models.gcn import GCNConv

class GAE(nn.Module):
    def __init__(self, data, nhid, nlat, dropout):
        super(GAE, self).__init__()
        self.gc1 = GCNConv(data.num_features, nhid)
        self.gc2 = GCNConv(nhid, nlat)
        self.dropout = dropout

    def reset_parameters(self):
        self.gc1.reset_parameters()
        self.gc2.reset_parameters()

    def forward(self, data):
        x, adj = data.features, data.norm_adj
        x = F.dropout(x, p=self.dropout, training=self.training)
        x = self.gc1(x, adj)
        x = F.dropout(x, p=self.dropout, training=self.training)
        x = F.relu(x)
        x = self.gc2(x, adj)
        x = F.dropout(x, p=self.dropout, training=self.training)
        x = torch.mm(x, x.t())
        return x

def create_gae_model(data, nhid=32, nlat=16, dropout=0., lr=0.01, weight_decay=0.):
    model = GAE(data, nhid, nlat, dropout)
    optimizer = Adam(model.parameters(), lr=lr, weight_decay=weight_decay)
    return model, optimizer


# !/usr/bin/env python
# -*- coding: utf-8 -*-
# author: wangzq <wangzhenqing1008@163.com>

from fixture import SQLAlchemyFixture, DataSet
from oslo_db import options
from oslo_config import cfg
from testtools import TestCase
from qg.db import api as db_api

from itdblib.dal.asset_consume_info_dal import AssetConsumeInfoDal
from itdblib.models.tb_asset_consume_info import AssetConsumeInfoModel
from itdblib.models.base import BASE
import fixtures

CONF = cfg.CONF
CONF.register_opts(options.database_opts, 'database')

class AssetConsumeInfoMock(DataSet):

    class AssetConsumeInfoModel():
        id=1
        asset_type_id = 1
        model_id = 1
        user_name = "abc"
        store_place_id = 1
        store_state_id = 1
        device_state_id = 1
        in_num = 0
        out_num = 0
        in_out_reason_id = 0
        remark = 'remark'

class StorePlaceMock(DataSet):
    class StorePlace():
        id = 1
        ch_name = 'store_place1'

class ModelMock(DataSet):
    class Model():
        id = 1
        provider_id = 1
        name = 'model1'

class AssetTypeMock(DataSet):
    class AssetTypeModel():
        id = 1
        ch_name = 'asset_path1'
        asset_path = 'asset_path1'

class StoreStateMock(DataSet):
    class StoreState():
        id = 1
        ch_name = 'state1'

class DBConnectionFx(fixtures.Fixture):

    def setUp(self):
        super(DBConnectionFx, self).setUp()
        CONF(['--config-file', 'tests/etc/unittest.ini'])

class DBFixture(fixtures.Fixture):
    def setUp(self):
        super(DBFixture, self).setUp()
        self.engine = db_api.get_engine()
        self.engine.connect()
        self.dbfixture = SQLAlchemyFixture(
            engine=self.engine,
            env={
                "AssetConsumeInfoMock": AssetConsumeInfoModel
            }
        )
        self.data = self.dbfixture.data(AssetConsumeInfoMock)
        self.session = db_api.get_session()
        BASE.metadata.create_all(bind=self.engine)
        self.data.setup()
        self.addCleanup(self.data.teardown)

class TestAssetConsumeInfoDal(TestCase):

    def setUp(self):
        super(TestAssetConsumeInfoDal, self).setUp()
        self.useFixture(DBConnectionFx())
        self.dbfixture = self.useFixture(DBFixture())
        self.session = self.dbfixture.session

    def test_store_new_consume_data(self):

        data = {
            'asset_type': 1,  #id, not name
            'model': 1,       #id, not name
            'store_place': 1, #id, not name
            'in_num': 3,
            'remark': 'test',
            'user_name': 'ops'
        }
        AssetConsumeInfoDal().store_new_consume_data(data)
        model = AssetConsumeInfoDal().get_actual_asset_by_type_model_place(
            asset_type=1,
            model_id=1,
            store_place_id=1,
            store_state=1
        )
        self.assertEqual('test', model.remark)
        self.assertEqual('ops', model.user_name)


import arrow
import re

from django.conf import settings
from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseForbidden
from django.views.decorators.csrf import csrf_exempt
from linebot import LineBotApi, WebhookHandler
from linebot.exceptions import InvalidSignatureError, LineBotApiError
from linebot.models import MessageEvent, TextSendMessage, TextMessage

from .models import ValidUsers, UserLogs

line_bot_api = LineBotApi(settings.LINE_CHANNEL_ACCESS_TOKEN)
handler = WebhookHandler(settings.LINE_CHANNEL_SECRET)
annotation_pattern = re.compile("(\d+)\/(\d+)\/(\d+)\/(\d+)\/.*.jpg")

@handler.add(MessageEvent, message=TextMessage)
def handle_text_message(event):
    content = event.message.text
    line_id = event.source.user_id
    arrive_timestamp = arrow.now(settings.TZINFO).datetime
    if 'open the door' in content:
        obj, created = ValidUsers.objects.get_or_create(line_id=line_id)
        if not created:
            line_bot_api.reply_message(
                event.reply_token,
                TextSendMessage(text="you already register the service")
            )
        else:
            setattr(obj, 'register_time', arrive_timestamp)
            obj.save()
            line_bot_api.reply_message(
                event.reply_token,
                TextSendMessage(text="you subscribe the service successfully")
            )
    else:
        if annotation_check(content):
            is_annotation = True
            response_prefix = "thank for your annotation: %s"
        else:
            is_annotation = False
            response_prefix = "thank for your feedback: %s"

        obj = UserLogs(line_id=line_id, arrive_time=arrive_timestamp,
                       msg=content, is_annotation=is_annotation)
        obj.save()
        line_bot_api.reply_message(
            event.reply_token,
            TextSendMessage(text=response_prefix % content)
        )

def annotation_check(text):
    """
    check if the message has the annotation information

    Parameters
    ----------
    text: str
        the string to check if having annotation information
    Returns
    ----------
    is_annotation: bool
        if is annotation or not
    """
    return bool(annotation_pattern.match(text))

@handler.default()
def default(event):
    print(event)
    line_bot_api.reply_message(
        event.reply_token,
        TextSendMessage(text='Currently Not Support None Text Message')
    )

@csrf_exempt
def callback(request):
    if request.method == 'POST':
        signature = request.META['HTTP_X_LINE_SIGNATURE']
        body = request.body.decode('utf-8')
        try:
            handler.handle(body, signature)
        except InvalidSignatureError:
            return HttpResponseForbidden()
        except LineBotApiError:
            return HttpResponseBadRequest()
        return HttpResponse()
    else:
        return HttpResponseBadRequest()


import os
import psycopg2
import psycopg2.extras

def get_all_coins() -> list:
    conn = psycopg2.connect(os.environ['COINALERT_DB_CONN_STR'])

    cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)

    cursor.execute('SELECT (sym, usd, name) FROM coins')

    raw_coins = cursor.fetchall()

    parsed_coins = [tuple(row[0].replace('(', '').replace(')', '').split(',')) for row in raw_coins]

    return parsed_coins


import torch
import torch.nn as nn
import torch.nn.init as init
import torch.nn.functional as F
import math
import numpy as np

class BaseNet(nn.Module):
    def __init__(self):
        super(BaseNet, self).__init__()
        self.conv = nn.Sequential(
            nn.Conv3d(3, 32, kernel_size=(3, 5, 5), stride=(1, 2, 2), padding=(1, 2, 2)),
            nn.BatchNorm3d(32),
            nn.ReLU(True),
            nn.MaxPool3d(kernel_size=(1, 2, 2), stride=(1, 2, 2)),
            nn.Conv3d(32, 64, kernel_size=(4, 5, 5), stride=(1, 1, 1), padding=(1, 2, 2)),
            nn.ReLU(True),
            nn.BatchNorm3d(64),
            nn.MaxPool3d(kernel_size=(1, 2, 2), stride=(1, 2, 2)),
            nn.Conv2d(64, 128, kernel_size=5, stride=2, padding=2),
            nn.BatchNorm2d(128),
            nn.ReLU(True),
            nn.Conv2d(128, 8, kernel_size=3, stride=2, padding=2),
            nn.BatchNorm2d(8),
            nn.ReLU(True),
            nn.Linear(48, 48),
            nn.LSTM(bidirectional=True, num_layers=2, hidden_size=200, input_size=48)
        )

    def forward(self, x):

        x = self.conv(x)
        return x


# -*- coding: utf-8 -*-
"""
Created on Sun Jul 11 13:35:20 2021

@author: Rafa
"""

'''
Realizar un programa que simule un cajero automático
con cinco billetes para cada denominación. El
usuario debe poder hacer retiros haciendo uso de
los billetes en existencia con cantidades exactas
'''
veinte = 5              # $100
cincuenta = 5           # $250
cien = 5                # $500
doscientos = 5          # $1,000
quinientos = 5          # $2,500
mil = 5                 # $5,000
total = 9350           #Total = $9,350

print("Bienvenido a nuestro sistema")
user = input("por introdusca su Usario ")
passw = input("Ahora su Password ")
if(user == passw):
    print("Bienvenido sr",user)

while(total != 0):
    cantidad = int(input("Por favor introdusca la cantidad que desea retirar "))
    sale = cantidad
    if (cantidad <= 9350 and cantidad > 19 and sale <= total): #evalua la cantidad a retirar
        print("¿Seguro que quiere retirar ",cantidad," pesos?")
        respuesta = input(" ")
        if(respuesta == "si" or "s"):
            if(cantidad >= 1000 and mil != 0):
                billete = cantidad // 1000
                if(billete <= 5 and billete <= mil):
                    mil = mil - billete
                    cantidad = cantidad % 1000
                    total = total - (billete* 1000)
                    print(billete, "de Mil pesos")
                else:
                    data = billete - mil
                    dado = data * 1000
                    cantidad = (cantidad % 1000)+dado
                    print(mil, "billetes de Mil pesos")
                    total = total - (mil*1000)
                    mil = 0

            if(cantidad >= 500 and quinientos != 0):
                billete = cantidad //500
                if (billete <= 5):
                    quinientos = quinientos - billete
                    cantidad = cantidad % 500
                    total = total - (billete * 500)
                    print(billete," billetes de Quinientos pesos")
                else:
                    data = billete - quinientos
                    dado = data * 500
                    cantidad = (cantidad % 500)+dado
                    print(quinientos, "billetes de Quinientos pesos")
                    total = total - (quinientos * 500)
                    quinientos = 0
            if(cantidad >= 200 and doscientos != 0):
                billete = cantidad // 200
                if(billete <= 5):
                    doscientos = doscientos - billete
                    cantidad = cantidad % 200
                    total = total - (billete * 200)
                    print(billete," billetes de Doscientos pesos")
                else:
                    data = billete - doscientos
                    dado = data * 200
                    cantidad = (cantidad % 200)+dado
                    print(doscientos, "billetes de Doscientos pesos")
                    total = total - (doscientos * 200)
                    doscientos = 0
            if(cantidad >= 100 and cien != 0):
                billete = cantidad // 100
                if(billete <= 5):
                    cien = cien - billete
                    cantidad = cantidad % 100
                    total = total - (billete * 100)
                    print(billete," billetes de Cien pesos")
                else:
                    data = billete - cien
                    dado = data * 100
                    cantidad = (cantidad % 100)+dado
                    print(cien, "billetes de Cien pesos")
                    total = total - (cien * 100)
                    cien = 0
            if (cantidad >= 50 and cincuenta != 0): #arreglar bug
                billete = cantidad // 50
                if(billete <= 5):
                    cincuenta = cincuenta - billete
                    cantidad = cantidad % 50
                    total = total - (billete * 50)
                    print(billete," billetes de Cincuenta pesos")
                else:
                    data = billete - cincuenta
                    dado = data * 50
                    cantidad = (cantidad % 50)+dado
                    print(cincuenta, "billetes de Cincuenta pesos")
                    total = total - (cincuenta * 50)
                    cincuenta = 0
            if (cantidad >= 20 and veinte != 0):
                billete = cantidad // 20
                if(billete <= 5):
                    veinte = veinte - billete
                    cantidad = cantidad % 20
                    total = total - (billete * 20)
                    print(billete, "billetes de Veinte pesos")

            #total = total - sale
        else:
            break

    else:
        print("Usted no cuenta con ese efectivo en su cuenta para retirar esa cantidad")

print("Usted ha agotado su saldo")


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

from django.db import migrations, models
import django.utils.timezone

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Cliente',
            fields=[
                ('id', models.AutoField(auto_created=True, verbose_name='ID', primary_key=True, serialize=False)),
                ('nome', models.CharField(max_length=200)),
                ('tipo', models.CharField(max_length=200)),
                ('email', models.EmailField(max_length=200)),
                ('data_nascimento', models.DateField()),
                ('primeira_consulta', models.DateField()),
            ],
        ),
        migrations.CreateModel(
            name='Consulta',
            fields=[
                ('id', models.AutoField(auto_created=True, verbose_name='ID', primary_key=True, serialize=False)),
                ('texto', models.TextField()),
                ('data', models.DateTimeField(default=django.utils.timezone.now)),
                ('nome', models.ForeignKey(to='climed.Cliente')),
            ],
        ),
    ]


from django.contrib.auth.models import User, Group
from rest_framework import serializers
from reala_api.models import Parcel, Owner, Event
from reala_api.helpers import search_parcel, get_user_created_event
from django.utils import timezone

class UserSerializer(serializers.ModelSerializer):
    """
    Simple User Serializer for read/write API
    """

    class Meta:
        model = User
        fields = ('url', 'username', 'email', 'groups')

class GroupSerializer(serializers.ModelSerializer):
    """
    Simple Group Serializer for read/write API
    """

    class Meta:
        model = Group
        fields = ('url', 'name')

class ParcelSerializer(serializers.ModelSerializer):
    street_number = serializers.CharField(read_only=True)
    route = serializers.CharField(read_only=True)
    city = serializers.CharField(read_only=True)
    state = serializers.CharField(read_only=True)
    postal_code = serializers.CharField(read_only=True)
    lat = serializers.CharField(read_only=True)
    lng = serializers.CharField(read_only=True)

    class Meta:
        model = Parcel
        fields = (
            'id', 'contact', 'formatted_address', 'street_number', 'route', 'city', 'state', 'postal_code', 'lat',
            'lng', 'swis', 'assessment', 'frontage', 'depth', 'acres', 'year_built', 'beds', 'baths', 'fireplace',
            'description', 'school', 'notes')

    def create(self, validated_data):
        parcel = search_parcel(validated_data.pop('formatted_address'))
        for attr, val in validated_data.items():
            setattr(parcel, attr, val)
        parcel.save()

        return parcel

    def update(self, instance, validated_data):
        # We don't let Parcel objects update address data
        # Since every address given exists, we want a new
        # Parcel instance to be created instead.
        parcel = search_parcel(validated_data.pop('formatted_address'))
        if parcel is not None and parcel.formatted_address != instance.formatted_address:
            return parcel

        for attr, val in validated_data.items():
            setattr(instance, attr, val)
        instance.save()

        return instance

class ParcelCompactSerializer(serializers.ModelSerializer):
    formatted_address = serializers.CharField(allow_blank=True)

    class Meta:
        model = Parcel
        fields = ('id', 'formatted_address')

class EventCompactSerializer(serializers.ModelSerializer):
    class Meta:
        model = Event
        fields = ('id', 'occurred', 'user', 'type')

class OwnerSerializer(serializers.ModelSerializer):
    events = EventCompactSerializer(many=True, read_only=True)
    home = ParcelCompactSerializer()
    parcels = ParcelCompactSerializer(many=True, read_only=True)

    gender = serializers.CharField(allow_blank=True)
    phone_number = serializers.CharField(allow_blank=True)
    email_address = serializers.CharField(allow_blank=True)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        """
        user = self.context['request'].user
        owner_id = args[0].id
        e = get_user_created_event(user, owner_id)
        e.type = "Contact"
        e.occurred = timezone.now()
        e.save()
        """

    class Meta:
        model = Owner
        fields = ('id', 'first_name', 'last_name', 'parcels', 'events',
                  'phone_number', 'phone_verified', 'do_not_contact',
                  'email_address', 'age', 'gender', 'home')

    def create(self, validated_data):
        home_data = validated_data.pop('home')
        owner = Owner.objects.create(**validated_data)
        owner.save()
        owner.home = search_parcel(home_data['formatted_address'])
        if owner.home is not None:
            owner.home.contact = owner
            owner.home.save()
        owner.save()

        return owner

    def update(self, instance, validated_data):
        home_data = validated_data['home']
        parcel = search_parcel(home_data['formatted_address'])
        instance.home = parcel

        instance.first_name = validated_data.get('first_name', instance.first_name)
        instance.last_name = validated_data.get('last_name', instance.last_name)
        instance.phone_number = validated_data.get('phone_number', instance.phone_number)
        instance.phone_verified = validated_data.get('phone_verified', instance.phone_verified)
        instance.do_not_contact = validated_data.get('do_not_contact', instance.do_not_contact)
        instance.email_address = validated_data.get('email_address', instance.email_address)
        instance.age = validated_data.get('age', instance.age)
        instance.gender = validated_data.get('gender', instance.gender)

        parcel.contact = instance
        parcel.save()
        instance.save()

        return instance

class EventSerializer(serializers.ModelSerializer):
    user = UserSerializer(read_only=True)
    # owner = serializers.CharField(required=True)

    class Meta:
        model = Event
        fields = ('id', 'occurred', 'user', 'owner', 'type', 'details', 'notes',
                  'claimed', 'called', 'mailed', 'social', 'other')

    def create(self, validated_data):
        user = self.context['request'].user
        owner = validated_data['owner']

        occurred = timezone.now()
        etype = 'Contact'
        if 'occurred' in validated_data:
            occurred = validated_data['occurred']
        if 'type' in validated_data:
            etype = validated_data['type']

        e = get_user_created_event(user, owner.id)
        e.occurred = occurred
        e.type = etype

        if 'details' in validated_data:
            e.details = validated_data['details']
        if 'notes' in validated_data:
            e.notes = validated_data['notes']

        if 'claimed' in validated_data:
            e.claimed = validated_data['claimed']
        if 'called' in validated_data:
            e.called = validated_data['called']
        if 'mailed' in validated_data:
            e.mailed = validated_data['mailed']
        if 'social' in validated_data:
            e.social = validated_data['social']
        if 'other' in validated_data:
            e.other = validated_data['other']

        e.save()
        return e

class LeadSerializer(serializers.ModelSerializer):
    home = ParcelCompactSerializer()
    events = EventCompactSerializer(many=True, read_only=True)

    class Meta:
        model = Owner
        fields = ('id', 'first_name', 'last_name', 'home', 'events')

    def create(self, validated_data):
        home_data = validated_data.pop('home')
        owner = Owner.objects.create(**validated_data)
        owner.home = search_parcel(home_data['formatted_address'])
        owner.home.contact = owner
        owner.home.save()
        owner.save()

        return owner

    def update(self, instance, validated_data):
        home_data = validated_data['home']
        parcel = search_parcel(home_data['formatted_address'])
        instance.home = parcel
        parcel.contact = instance
        parcel.save()
        instance.save()

        return instance


# -*- coding: utf-8 -*-
"""
Created on Wed Sep  4 20:57:54 2019

@author: Puneet_Dubey
"""
#import basic packages

import numpy as np
import pandas as pd

#read data

dataset = pd.read_csv('ML.csv')

Cust_ID= dataset['customerID']
out_data= dataset

dataset.info()

dataset_dt=dataset

#Total chrarges is type objects..needs conversion

dataset['TotalCharges'] = pd.to_numeric(dataset.TotalCharges, errors='coerce')

dataset.head()

#customer Id has high cardinality, hence wont be of use
dataset.drop('customerID',axis=1 ,inplace = True)

dataset.head()

dataset.describe(include=[np.object])

column_nm=list(dataset.columns)

column_nm.remove('tenure')
column_nm.remove('MonthlyCharges')
column_nm.remove('TotalCharges')

for i in column_nm:
    j=dataset[i].value_counts()
    print('__________________')
    print(j)

dataset.describe(include=[np.number])

#Checking for missing values

dataset.isnull().sum()

#filling missing values with median

dataset.TotalCharges.fillna(dataset.TotalCharges.median(), inplace= True)

dataset.isnull().sum()

"""correlation analysis from tableau it is clear that there is a strong correlation within
Total charges and tenure """

print(dataset[['MonthlyCharges','TotalCharges','tenure']].corr())

dataset.drop(['TotalCharges'],axis=1,inplace= True)

dataset.head()

print(dataset.corr()) #no strong co-relations

#create dummy variables

copy_dataset = dataset

dummy=pd.get_dummies(copy_dataset,drop_first=True)

dummy.head()

len(dummy.columns)

#Building predictive models
X=dummy.iloc[:,0:22]
y=dummy.iloc[:,-1]

X_New = X

from sklearn.model_selection import train_test_split

X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.35,random_state=5)

from sklearn.preprocessing import StandardScaler

sc=StandardScaler()

X_train=sc.fit_transform(X_train)

X_test=sc.transform(X_test)

#Create model

from sklearn.linear_model import LogisticRegression

logreg = LogisticRegression()
logreg.fit(X_train, y_train)

y_pred = logreg.predict(X_test)

print('Accuracy on test set: {:.2f}'.format(logreg.score(X_test, y_test)))

y_pred1 = logreg.predict(X_train)
print('Accuracy  on train set: {:.2f}'.format(logreg.score(X_train, y_train)))

from sklearn.metrics import classification_report
print(classification_report(y_test,y_pred))

'''
y_pred_list=list(y_pred)

y_prob=logreg.predict_proba(X_test)

y_prob_list=list(y_prob)

'''

X_New=sc.fit_transform(X_New)

Y_pred_Final = logreg.predict(X_New)

Y_Pred_Final_List = list(Y_pred_Final)

dataset['Prediction']=Y_Pred_Final_List

dataset_final= dataset

#decision tree
'''
from sklearn.tree import DecisionTreeClassifier

dt = DecisionTreeClassifier()
dt.fit(X_train, y_train)

y_pred_dt = dt.predict(X_test)

print('Accuracy on test set: {:.2f}'.format(dt.score(X_test, y_test)))

y_pred1_dt = logreg.predict(X_train)

print('Accuracy  on train set: {:.2f}'.format(logreg.score(X_train, y_train)))

X_New=sc.fit_transform(X_New)

Y_pred_Final_dt= dt.predict(X_New)

Y_Pred_Final_List_dt = list(Y_pred_Final)

dataset_dt['Prediction']=Y_Pred_Final_List_dt'''


"""Hangups - open client for the undocumented Hangouts chat API."""

import re
import random
import json
import requests
import time
import hashlib
import datetime

from hangups import javascript, longpoll

class HangupsClient(object):

    def __init__(self, cookies, origin_url):
        self._cookies = cookies
        self._origin_url = origin_url

        # discovered automatically:

        # the api key sent with every request
        self.api_key = None
        # fields sent in request headers
        self.header_date = None
        self.header_version = None
        self.header_id = None
        self.header_client = None
        # parameters related talkgadget channel requests
        self.channel_path = None
        self.gsessionid = None
        self.clid = None
        self.channel_ec_param = None
        self.channel_prop_param = None
        self.channel_session_id = None

        # make initialization requests
        self._init_talkgadget_1()
        self._init_talkgadget_2()

    def _init_talkgadget_1(self):
        """Make first talkgadget request and parse response.

        The response body is a HTML document containing a series of script tags
        containing JavaScript object. We need to parse the object to get at the
        data.
        """
        url = 'https://talkgadget.google.com/u/0/talkgadget/_/chat'
        params = {
            'prop': 'aChromeExtension',
            'fid': 'gtn-roster-iframe-id',
            'ec': '["ci:ec",true,true,false]',
        }
        headers = {
            # appears to require a browser user agent
            'user-agent': (
                'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36'
                '(KHTML, like Gecko) Chrome/34.0.1847.132 Safari/537.36'
            ),
        }
        res = requests.get(url, cookies=self._cookies, params=params,
                           headers=headers)
        if res.status_code != 200:
            raise ValueError("First talkgadget request failed")
        res = res.text

        # Parse the response by using a regex to find all the JS objects, and
        # parsing them.
        res = res.replace('\n', '')
        regex = re.compile(
            r"(?:<script>AF_initDataCallback\((.*?)\);</script>)"
        )
        data_dict = {}
        for data in regex.findall(res):
            try:
                data = javascript.loads(data)
                data_dict[data['key']] = data['data']
            except ValueError:
                pass # not everything will be parsable, but we don't care

        # TODO: handle errors here
        self.api_key = data_dict['ds:7'][0][2]
        self.header_date = data_dict['ds:2'][0][4]
        self.header_version = data_dict['ds:2'][0][6]
        self.header_id = data_dict['ds:4'][0][7]
        self.channel_path = data_dict['ds:4'][0][1]
        self.gsessionid = data_dict['ds:4'][0][3]
        self.clid = data_dict['ds:4'][0][7]
        self.channel_ec_param = data_dict['ds:4'][0][4]
        self.channel_prop_param = data_dict['ds:4'][0][5]

    def _init_talkgadget_2(self):
        """Make second talkgadget request and parse response."""
        url = 'https://talkgadget.google.com{}bind'.format(self.channel_path)
        params = {
            'VER': 8,
            'clid': self.clid,
            'prop': self.channel_prop_param,
            'ec': self.channel_ec_param,
            'gsessionid': self.gsessionid,
            'RID': 81187, # TODO
            'CVER': 1,
            't': 1, # trial
        }
        res = requests.post(url, cookies=self._cookies, params=params,
                            data='count=0', stream=True)
        if res.status_code != 200:
            raise ValueError("Second talkgadget request failed with {}: {}"
                             .format(res.status_code, res.raw.read()))
        res = list(longpoll.load(res.raw))[0]
        val = res[3][1][1][1][1] # ex. foo@bar.com/AChromeExtensionBEEFBEEF
        self.header_client = val.split('/')[1] # ex. AChromeExtensionwBEEFBEEF
        self.channel_session_id = res[0][1][1]

    def _receive_push_events(self):
        """Open channel to receive push events."""
        url = 'https://talkgadget.google.com/u/0/talkgadget/_/channel/bind'
        params = {
            'VER': 8,
            'clid': self.clid,
            'prop': self.channel_prop_param,
            'ec': self.channel_ec_param,
            'gsessionid': self.gsessionid,
            'RID': 'rpc',
            't': 1, # trial
            'SID': self.channel_session_id,
            'CI': 0,
        }
        res = requests.get(url, params=params, cookies=self._cookies,
                           stream=True)
        if res.status_code != 200:
            raise ValueError('Push channel request returned {}: {}'
                             .format(res.status_code, res.raw.read()))
        for message in longpoll.load(res.raw):
            yield message

    def _get_authorization_header(self):
        # technically, it doesn't matter what the url and time are
        time_msec = int(time.time() * 1000)
        auth_string = '{} {} {}'.format(time_msec, self._get_cookie("SAPISID"),
                                        self._origin_url)
        auth_hash = hashlib.sha1(auth_string.encode()).hexdigest()
        return 'SAPISIDHASH {}_{}'.format(time_msec, auth_hash)

    def _get_cookie(self, name):
        try:
            return self._cookies[name]
        except KeyError:
            raise KeyError("Cookie '{}' is required".format(name))

    def _get_request_header(self):
        return [
            [3, 3, self.header_version, self.header_date],
            [self.header_client, self.header_id],
            None,
            "en"
        ]

    def _request(self, endpoint, body_json):
        url = 'https://clients6.google.com/chat/v1/{}'.format(endpoint)
        headers = {
            'authorization': self._get_authorization_header(),
            'x-origin': self._origin_url,
            'x-goog-authuser': '0',
            'content-type': 'application/json+protobuf',
        }
        required_cookies = ['SAPISID', 'HSID', 'SSID', 'APISID', 'SID']
        cookies = {cookie: self._get_cookie(cookie)
                   for cookie in required_cookies}
        params = {
            'key': self.api_key,
            'alt': 'json', # json or protojson
        }
        return requests.post(url, headers=headers, cookies=cookies,
                             params=params, data=json.dumps(body_json))

    def getselfinfo(self):
        """Return information about your account."""
        res = self._request('contacts/getselfinfo', [
            self._get_request_header(),
            [], []
        ])
        return json.loads(res.text)

    def setfocus(self, conversation_id):
        """Set focus (occurs whenever you give focus to a client)."""
        res = self._request('conversations/setfocus', [
            self._get_request_header(),
            [conversation_id],
            1,
            20
        ])
        return json.loads(res.text)

    def searchentities(self, search_string, max_results):
        """Search for people."""
        res = self._request('contacts/searchentities', [
            self._get_request_header(),
            [],
            search_string,
            max_results
        ])
        return json.loads(res.text)

    def querypresence(self, chat_id):
        """Check someone's presence status."""
        res = self._request('presence/querypresence', [
            self._get_request_header(),
            [
                [chat_id]
            ],
            [1, 2, 5, 7, 8]
        ])
        return json.loads(res.text)

    def getentitybyid(self, chat_id_list):
        """Return information about a list of contacts."""
        res = self._request('contacts/getentitybyid', [
            self._get_request_header(),
            None,
            [[str(chat_id)] for chat_id in chat_id_list],
        ])
        return json.loads(res.text)

    def getconversation(self, conversation_id, num_events,
                        storage_continuation_token, event_timestamp):
        """Return data about a conversation.

        Seems to require both a timestamp and a token from a previous event
        """
        res = self._request('conversations/getconversation', [
            self._get_request_header(),
            [
                [conversation_id], [], []
            ],
            True, True, None, num_events,
            [None, storage_continuation_token, event_timestamp]
        ])
        return json.loads(res.text)

    def syncallnewevents(self, after_timestamp):
        """List all events occuring at or after timestamp."""
        res = self._request('conversations/syncallnewevents', [
            self._get_request_header(),
            after_timestamp,
            [], None, [], False, [],
            1048576
        ])
        return json.loads(res.text)

    def sendchatmessage(self, conversation_id, message, is_bold=False,
                        is_italic=False, is_strikethrough=False,
                        is_underlined=False):
        """Send a chat message to a conversation."""
        client_generated_id = random.randint(0, 2**32)
        res = self._request('conversations/sendchatmessage', [
            self._get_request_header(),
            None, None, None, [],
            [
                [
                    [0, message, [is_bold, is_italic, is_strikethrough,
                                  is_underlined]]
                ],
                []
            ],
            None,
            [
                [conversation_id], client_generated_id, 2
            ],
            None, None, None, []
        ])
        return json.loads(res.text)

def load_cookies_txt():
    """Return cookies dictionary loaded from cookies.txt file.

    Expected format is the same as the body of an HTTP cookie header.
    """
    cookies_str = open('cookies.txt').read()
    cookies_list = [s.split('=', 1) for s in
                    cookies_str.strip('\n').split('; ')]
    return {cookie[0]: cookie[1] for cookie in cookies_list}

def main():
    """Make a chat request."""
    cookies = load_cookies_txt()
    hangups = HangupsClient(cookies, 'https://talkgadget.google.com')

    # Get all events in the past hour
    now = time.time() * 1000000
    one_hour = 60 * 60 * 1000000
    events = hangups.syncallnewevents(now - one_hour)

    conversations = {}
    for conversation in events['conversation_state']:
        id_ = conversation['conversation']['id']['id']
        participants = {
            (p['id']['chat_id'], p['id']['gaia_id']): p['fallback_name']
            for p in conversation['conversation']['participant_data']
        }
        conversations[id_] = {
            'participants': participants,
        }

    conversations_list = list(enumerate(conversations.items()))
    print('Activity has recently occurred in the conversations:')
    for n, (_, conversation) in conversations_list:
        print(' [{}] {}'
              .format(n,
                      ', '.join(sorted(conversation['participants'].values()))))
    conversation_index = int(input('Select a conversation to listen to: '))
    conversation_id = conversations_list[conversation_index][1][0]
    conversation = conversations_list[conversation_index][1][1]
    print('Now listening to conversation\n')

    for msg in hangups._receive_push_events():
        msg = longpoll.parse_message(msg)
        if 'payload_type' in msg and msg['payload_type'] == 'list':
            for submsg in longpoll.parse_list_payload(msg['payload']):
                if submsg['conversation_id'] == conversation_id:
                    print('({}) {}: {}'.format(
                        datetime.datetime.fromtimestamp(
                            submsg['timestamp'] / 1000000
                        ).strftime('%I:%M:%S %p'),
                        conversation['participants'][submsg['sender_ids']],
                        submsg['text']
                    ))

    print('Connection closed')

if __name__ == '__main__':
    main()


"""
NSLS2 Visualization Lib

:Author: Lingyun Yang <lyyang@bnl.gov>
:Date: 2014-07-11 18:21:20

"""

from __future__ import print_function
import time
import numpy as np
import aphla as ap
import matplotlib.pylab as plt
from datetime import datetime
from matplotlib.dates import YEARLY, DateFormatter, rrulewrapper, RRuleLocator, drange
import numpy as np
import datetime

_figsize = (12, 4)

def pltOrbit(**kwargs):
    """
    figsize -
    """
    fig = plt.figure(figsize=kwargs.pop("figsize", _figsize))
    ax1 = fig.add_subplot(2,1,1)
    ax2 = fig.add_subplot(2,1,2)
    obt = ap.getOrbit(spos=True)
    ax1.plot(obt[:,-1], obt[:,0], label="std(X)= %.2f" % np.std(obt[:,0]))
    ax1.set_ylabel("X [mm]", fontdict={"size": 18})
    ax1.legend(loc="best")
    ax1.grid(True)
    ax2.plot(obt[:,-1], obt[:,1], label="std(Y)= %.2f" % np.std(obt[:,1]))
    ax2.set_ylabel("Y [mm]", fontdict={"size": 18})
    ax2.legend(loc="best")
    ax2.grid(True)
    return [ax1, ax2]

def pltFullOrbit(**kwargs):
    """
    figsize -
    """
    fig = plt.figure(figsize=kwargs.pop("figsize", _figsize))
    ax1 = fig.add_subplot(2,1,1)
    ax2 = fig.add_subplot(2,1,2)
    allbpms = ap.getGroupMembers(["BPM", "UBPM"], op="union")
    s = [b.sb for b in allbpms]
    x = ap.fget(allbpms, 'x', unitsys=None)
    y = ap.fget(allbpms, 'y', unitsys=None)
    ax1.plot(s, x, label="std(X)= %.2f" % np.std(x))
    ax1.set_ylabel("X [mm]")
    ax1.legend(loc="best")
    ax1.grid(True)
    ax2.plot(s, y, label="std(Y)= %.2f" % np.std(y))
    ax2.set_ylabel("Y [mm]")
    ax2.legend(loc="best")
    ax2.grid(True)
    return [ax1, ax2]

def pltOrbitRms(**kwargs):
    nsample = kwargs.get("nsample", 10)
    obt0 = ap.getOrbit(spos=True)
    nbpm = len(obt0)
    dat = np.zeros((nbpm, 2, nsample), 'd')
    for i in range(nsample):
        dat[:,:,i] = ap.getOrbit(spos=False)
        time.sleep(0.12)
    err = np.std(dat, axis=-1) * 1000.0
    fig = plt.figure(figsize=kwargs.get("figsize", _figsize))
    ax1 = plt.subplot(2,1,1)
    ax1.plot(obt0[:,-1], err[:,0])
    ax1.set_ylabel("X [um]", fontdict={"size": 18})
    ax1.grid(True)
    ax2 = plt.subplot(2,1,2)
    ax2.plot(obt0[:,-1], err[:,1])
    ax2.set_ylabel("Y [um]", fontdict={"size": 18})
    ax2.grid(True)
    return ax1, ax2

def pltAllBpmRms(**kwargs):
    nsample = kwargs.get("nsample", 10)
    ignore = kwargs.get("ignore", [])
    allbpms = [b for b in ap.getGroupMembers(["BPM", "UBPM"], op="union")
               if b.name not in ignore and b.devname not in ignore]
    s = [b.sb for b in allbpms]
    obtx0 = ap.fget(allbpms, "x0", unitsys=None)
    obty0 = ap.fget(allbpms, "y0", unitsys=None)
    nbpm = len(allbpms)
    dat = np.zeros((nbpm, 2, nsample), 'd')
    for i in range(nsample):
        dat[:,0,i] = ap.fget(allbpms, "x0", unitsys=None)
        dat[:,1,i] = ap.fget(allbpms, "y0", unitsys=None)
        time.sleep(0.12)
    err = np.std(dat, axis=-1) * 1000.0
    ix = np.argmax(err[:,0])
    iy = np.argmax(err[:,1])
    fig = plt.figure(figsize=kwargs.get("figsize", _figsize))
    ax1 = plt.subplot(2,1,1)
    b180 = [i for i,b in enumerate(allbpms) if "BPM" in b.group]
    uidx = [i for i,b in enumerate(allbpms) if "UBPM" in b.group]
    b60 = [i for i,b in enumerate(allbpms) if b.devname[-1] in ["3", "4"]]
    ax1.semilogy(np.take(s, b180), np.take(err[:,0], b180), '-b', lw=2)
    ax1.semilogy(np.take(s, uidx), np.take(err[:,0], uidx), 'og')
    ax1.semilogy(np.take(s, b60), np.take(err[:,0], b60), 'bv')
    ax1.set_ylabel("Xstd [um]", fontdict={"size": 18})
    ax1.grid(True)
    ax1.annotate("peak={0}".format(allbpms[ix].devname),
                 xy=(0.02, 0.85), xycoords="axes fraction", size=18)
    ax2 = plt.subplot(2,1,2)
    #ax2.plot(s, err[:,1])
    ax2.semilogy(np.take(s, b180), np.take(err[:,1], b180), '-b', lw=2)
    ax2.semilogy(np.take(s, uidx), np.take(err[:,1], uidx), 'og')
    ax2.semilogy(np.take(s, b60), np.take(err[:,1], b60), 'bv')
    ax2.set_ylabel("Ystd [um]", fontdict={"size": 18})
    ax2.grid(True)
    ax2.annotate("peak={0}".format(allbpms[iy].devname),
                 xy=(0.02, 0.85), xycoords="axes fraction", size=18)
    return ax1, ax2

def pltOrbitFft(x, y, **kwargs):
    fig = plt.figure(figsize=kwargs.get("figsize", (16,7)))
    nbpm, nturns = np.shape(x)
    obt = np.zeros((nbpm, nturns, 3), 'd')
    obt[:,:,0] = x
    obt[:,:,1] = y
    if "Isum" in kwargs:
        obt[:,:,2] =kwargs["Isum"]
    #
    Fs = np.zeros_like(obt)

    n = nturns // 2
    c = [('g-', "X"), ("b-", "Y")]
    obtc = np.zeros((nbpm, 2), 'd')
    for iplane in range(2):
        obtc[:,iplane] = np.average(obt[:,:,iplane], axis=1)
        #Fx = np.zeros((nbpm, nturns), 'd')

        for ibpm in range(180): #[1,2,3,4,5,6]:
            dobt = obt[ibpm,:,iplane]-obtc[ibpm,iplane]
            Fs[ibpm,:,iplane] = np.abs(np.fft.fft(dobt))

    # plotting
    vc = np.average(obtc, axis=-1)*1000.0
    vr = np.std(obtc, axis=-1) * 1000.0
    plt.subplot(3,1,1)
    plt.plot(obtc[:,0], 'r-', label="X %.1f+/-%.1f [um]" % (vc[0], vr[0]))
    plt.plot(obtc[:,1], 'b-', label="Y %.1f+/-%.1f [um]" % (vc[1], vr[1]))
    plt.grid(True)
    plt.legend(loc="best")

    freq, p = np.fft.fftfreq(nturns), np.sum(np.abs(Fs), axis=0)
    # print np.shape(Fs), np.shape(p)
    i0, i1 = 2+int(1e-4*nturns), nturns // 2
    if "tune_range" in kwargs:
        nuref = kwargs["tune_range"]
        # ref tune and +/- range
        #nu0 = [(0.2, 0.04), (0.18, 0.03), (0.01, 0.008)]
        nu = [0.0, 0.0, 0.0]
        for i in range(3):
            j0 = max(i0, int((nuref[i][0] - nuref[i][1])/(1.0/nturns)))
            j1 = min(i1, (nuref[i][0] + nuref[i][1])/(1.0/nturns))
            print(i, j0, j1, np.shape(p), freq[j0], freq[j1])
            if i < 2:
                im = j0 + np.argmax(p[j0:j1,i])
            elif i == 2:
                im = j0 + np.argmax(p[j0:j1,0] + p[j0:j1,1])
            nu[i] = freq[im]
    else:
        ff = 1.0/nturns
        psum = p[:,0]/np.max(p[:,0]) + p[:,1]/np.max(p[:,1])
        nu = [(i0+np.argmax(p[i0:i1,0]))*ff, (i0+np.argmax(p[i0:i1,1]))*ff,
              (i0+np.argmax(psum[i0:int(0.05*nturns)]))*ff]

    print(nu)
    plt.subplot(3,1,2)
    plt.plot(obt[3,:,0], 'r-')
    plt.plot(obt[5,:,0], 'r-')
    plt.plot(obt[3,:,1], 'b-')
    plt.plot(obt[5,:,1], 'b-')
    plt.grid(True)

    ax1 = plt.subplot(3,1,3)
    ax2 = ax1.twinx()
    p1, = ax1.semilogy(freq[i0:i1], p[i0:i1,0], 'g-', linewidth=2)
    p2, = ax2.semilogy(freq[i0:i1], p[i0:i1,1], 'b-', linewidth=2)
    #ax1.set_xlim([freq[i0], freq[i1]])
    ax1.grid(True)
    p1 = ax1.axvline(nu[0], color='g', linestyle='--')
    p2 = ax1.axvline(nu[1], color='b', linestyle='--')
    labels= ["X: %.4f" % nu[0], "Y: %.4f" % nu[1]]
    if nu[2]:
        p3 = ax1.axvline(nu[2], color='k', linestyle='--')
        labels.append("S: %.4f (%.2fkHz)" % (nu[2], nu[2]*500e3/1320.))
        plt.legend([p1, p2, p3], labels, loc="best")
    else:
        plt.legend([p1, p2], labels)
    #plt.savefig("tunes_1.png")

def plotArchivedData(data, **kwargs):
    fig = plt.figure(figsize=kwargs.pop("figsize", _figsize))
    formatter = DateFormatter('%m/%d/%y')
    t0 = kwargs.get("t0", datetime.now())
    delta = np.min([v[0][0] for k,v in data.items()])
    t1 = t0 + datetime.delta

def plotChromaticity(chrom, info, **kwargs):
    """
    plot with returns from measChromaticity.
    """
    fig = plt.figure(figsize=kwargs.pop("figsize", _figsize))
    #df = 1e3*(info["freq"] - info["freq0"])
    dpp = info["dpp"]
    w = dpp[-1] - dpp[0]
    t = np.linspace(dpp[0]-0.1*w, dpp[-1]+0.1*w, 100)
    # plt.clf()
    ax1 = plt.subplot(1,2,1)
    ax1.plot(dpp, info["tune"][:,0], '-rx')
    ax1.set_ylabel(r"$\nu_x$", fontdict={"size": 18})

    ax2 = plt.subplot(1,2,2)
    ax2.plot(dpp, info["tune"][:,1], '-go')
    ax2.set_ylabel(r"$\nu_y$", fontdict={"size": 18})

    if info["deg"] < 2:
        pass
    else:
        p2, p1, p0 = info["p"][:,0]
        nux = p2*t*t + p1*t + p0
        ax1.plot(t, nux, '--r', label=r"H: $%.1fx^2%+.2fx%+.2f$" % (p2, p1, p0))
        p2, p1, p0 = info["p"][:,1]
        nuy = p2*t*t + p1*t + p0
        ax2.plot(t, nuy, '--r', label=r"V: $%.1fx^2%+.2fx%+.2f$" % (p2, p1, p0))

    ax1.annotate(r"$C_x=%.2f$" %  (chrom[0], ),
                 xy=(0.02, 0.85), xycoords="axes fraction", size=18)
    ax2.annotate(r"$C_y=%.2f$" %  (chrom[1], ),
                 xy=(0.02, 0.85), xycoords="axes fraction", size=18)

    for ax in [ax1, ax2]:
        ax.legend(loc='lower right')
        ax.set_xlabel(r"$\delta p/p$", fontdict={"size": 18})
        ax.grid(True)
    plt.savefig('measchrom.png')


import os

from moviepy.editor import VideoFileClip

target_dir = r'/home/wangzheng/视频/Python分布式爬虫打造搜索引擎 Scrapy精讲/1/第3章 爬虫基础知识回顾'

movie_ext_list = ['.mp4', '.avi']

def task_run(dir_name, file):
    os.chdir(dir_name)
    clip = VideoFileClip(file)
    minutes = int(clip.duration // 60)
    path = os.path.join(dir_name, file)
    return path, minutes

def start():
    total_minutes = 0
    for root, dirs, files in os.walk(target_dir):
        if files:
            for file in files:
                ext = os.path.splitext(file)[1]
                if ext in movie_ext_list:
                    dir_name = root
                    path, minutes = task_run(dir_name, file)
                    print('{}:{}分钟'.format(path, minutes))
                    total_minutes += minutes
    total_hours = total_minutes // 60
    extra_minutes = total_minutes % 60
    print('共计{}小时{}分钟'.format(total_hours, extra_minutes))

if __name__ == '__main__':
    start()

文件处理/视频时长.py


N = int(input())
A = [int(x) for x in input().split()]

def main():
    A.sort()
    max_num = A[len(A) - 1]
    mid_num = max_num / 2

    curr = A.pop(0)
    d = abs(mid_num - curr)
    for a in A:
        next_d = abs(mid_num - a)
        if d <= next_d:
            break
        curr = a
        d = next_d
    print(max_num, curr)

if __name__ == "__main__":
    main()


import json
import os.path

from collections import defaultdict

def check_and_save_path(config, key, file_path):
    if os.path.exists(file_path):
        config[key] = file_path

def init_section(config: "Config", section_name, defaults=None):
    if not defaults:
        defaults = dict()
    section = defaults
    section.update(config[section_name])
    config[section_name] = section
    return section

class Config:
    def __init__(self, defaults=None, config_path=None):
        self.config = defaultdict(dict)
        if defaults is not None:
            self.config.update(defaults)

        self.config_path = config_path

    def save_settings(self):
        with open(self.config_path, 'w', encoding='utf-8') as config_file:
            json.dump(self.config, config_file, indent=4, sort_keys=True)

    def load_settings(self, config_path=None):
        if config_path is None:
            config_path = self.config_path
        else:
            self.config_path = config_path

        try:
            with open(config_path, encoding='utf-8') as config_file:
                self.config.update(json.load(config_file))
        except (FileNotFoundError, ValueError):
            pass

    def __getitem__(self, key):
        return self.config[key]

    def __setitem__(self, key, value):
        self.config[key] = value