######>>>>>>  LISTOS PARA DESPEGAR <<<<<<###################
# Aplicacion desarrollada para Nasa Space Apps Rosario #####
######>>>>>>   DATOS DE CONTACTO   <<<<<<###################
#      Avila, Leilen      leilenavila@gmail.com          ###
# Bolzan, Ana Emilia      anaemiliabolzan@hotmail.com    ###
# Ladreyt, Alejandro      ladreyt.alejandro@hotmail.com  ###
#     Ladreyt, Pablo      pabloladreyt@gmail.com         ###
######>>>>>>     OTROS DATOS     <<<<<<#####################
# Provincia: Sante Fe      Ano: 2016   #####################
# Localidad: Rosario                   #####################
############################################################

import requests
import math
from lxml import etree

def getDatosXML(AeroPartida, OrientacionPista):
    xmlcontenido = requests.get('https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&stationString=' + AeroPartida + '&hoursBeforeNow=24&mostRecent=true')
    xmltemporal = str(xmlcontenido.content).replace('\n', "")
    xmltemporal = xmltemporal.replace('<?xml version="1.0" encoding="UTF-8"?>',"")
    xmltemporal = xmltemporal.replace(' /', '/')
    xml = open('xml.xml', 'wb')
    xml.write(xmltemporal)
    xml.close()
    doc = etree.parse('xml.xml')
    root = doc.getroot()
    METAR = root.find("data/METAR")
    for child in METAR:
        if child.tag == "temp_c":
            Temperatura = float(child.text)
        if child.tag == "dewpoint_c":
            TemperaturaRocio = float(child.text)
        if child.tag == "wind_dir_degrees":
            AnguloViento = child.text
        if child.tag == "wind_speed_kt":
            VelocidadViento = child.text
        if child.tag == "visibility_statute_mi":
            Visibilidad = child.text
        if child.tag == "sky_condition":
            EstadoClima = child.attrib['sky_cover']
    Humedad = 100 * (math.exp((17.625 * TemperaturaRocio) / (243.04 +
              TemperaturaRocio)) / math.exp((17.625 * Temperatura) /
              (243.04 + Temperatura)))
    VientoCruzado = (float(VelocidadViento) *
                     math.sin(int(AnguloViento) - int(OrientacionPista)))
    return [Humedad, EstadoClima, Visibilidad, VientoCruzado]


from mypython.persistencia.BancoDeDados import BancoDeDados
import mysql.connector

class ImobiliariaDAO():
    def __init__(self):
        self.bd = BancoDeDados()

    #INSERT: Insere uma nova imobiliaria
    def insert(self, objImobi: object):
        try:
            #O Banco é Conectado
            bd = self.bd.conectar()
            print("Banco de dados conectado...")

            #O cursor será preparado para executar os Comandos SQL
            cursor = bd.cursor()

            #Comando SQL
            add_imobi = "INSERT INTO tb_imobiliaria(cd_imobiliaria, nome, endereco) VALUES (%s, %s, %s)"

            #Parametros do Comando SQL
            data_imobi = (objImobi.getId, objImobi.getNome, objImobi.getEndereco)

            #Ordem para Execução do comando SQL, passado o camando SQL e os parametros
            cursor.execute(add_imobi, data_imobi)

        #Captura a exceção
        except mysql.connector.Error as err:
            print("Erro ao realizar o cadastro de imobiliaria.")

        #Executa após o try catch
        finally:
            response = self.bd.desconectar(bd, cursor)
            print(response)

    #DELETE: Deleta uma imobiliaria
    def delete(self, id_imobiliaria: int):
        #Deleta imoveis relacionados
        try:
            #Instancia Banco de Dados e conecta ao banco
            bd = self.bd.conectar()
            print("Banco de dados conectado...")

            #Prepara o cursor
            cursor = bd.cursor()

            #Prepara comando SQL para excluir os imoveis realcionadas a imobiliaria
            delete = "delete from tb_imovel where cd_imobiliaria = %s"
            delete_param = (id_imobiliaria,)

            #executa o comando SQL
            cursor.execute(delete, delete_param)
        except mysql.connector.Error as err:
            print("Erro ao Excluir imoveis. Erro:{}".format(err))

        finally:
            #Deleta Imobiliarias
            try:
                #Define comando SQL
                delete = "delete from tb_imobiliaria where cd_imobiliaria = %s"
                delete_param = (id_imobiliaria,)

                cursor.execute(delete, delete_param)
                print("Imobiliaria Excluída com sucesso!")

            except mysql.connector.Error as err:
                print("Erro ao Excluir Imobiliaria. Erro{}".format(err))

            finally:
                response = self.bd.desconectar(bd, cursor)
                print(response)

    #UPDATE: Atualiza um registro de a partir do codigo da Imobiliaria
    def update(self, objImobi: object):
        try:
            #O Banco é Conectado
            bd = self.bd.conectar()
            print("Banco de dados conectado...")

            #O cursor será preparado para executar os Comandos SQL
            cursor = bd.cursor()

            #Comando SQL
            add_imobi = "update tb_imobiliaria set nome = %s, endereco = %s where cd_imobiliaria = %s"

            #Parametros do Comando SQL
            data_imobi = (objImobi.getNome, objImobi.getEndereco, objImobi.getId)

            #Ordem para Execução do comando SQL, passado o camando SQL e os parametros
            cursor.execute(add_imobi, data_imobi)

        #Captura a exceção
        except mysql.connector.Error as err:
            print("Erro ao realizar o update de imobiliaria.")

        #Executa após o try catch
        finally:
            response = self.bd.desconectar(bd, cursor)
            print(response)

    #SELECT: Recupera os dados referentes as imobiliarias
    #Recupera todos os dados
    def selectAll(self):
        try:
            #O Banco é Conectado
            bd = self.bd.conectar()
            print("Banco de dados conectado...")

            #O cursor será preparado para executar os Comandos SQL
            cursor = bd.cursor()

            #Comando SQL
            add_imobi = "select * from tb_imobiliaria"

            #Ordem para Execução do comando SQL, passado o camando SQL e os parametros
            cursor.execute(add_imobi)

            #Captura as linhas consultadas e salva como uma lista
            result = cursor.fetchall()

            return result
        #Captura a exceção
        except mysql.connector.Error as err:
            print("Erro ao realizar a consulta de imobiliaria.")

        #Termina a execução do método
        finally:
            response = self.bd.desconectar(bd, cursor)
            print(response)

    #SELECT
    #Seleciona os dados de acordo com o informado e retorna os valores mais proximos do nome
    def selectNome(self, nome: str):
        try:
            #O Banco é Conectado
            bd = self.bd.conectar()
            print("Banco de dados conectado...")

            #O cursor será preparado para executar os Comandos SQL
            cursor = bd.cursor()

            #Comando SQL
            add_imobi = "select * from tb_imobiliaria where nome like concat('%', %s, '%')"

            #Parametro para a Stored Procedure
            data_imobi = (nome,)

            #Ordem para Execução do comando SQL, passado o camando SQL e os parametros
            cursor.execute(add_imobi, data_imobi)

            #Captura as linhas consultadas e salva como uma lista
            result = cursor.fetchall()

            return result
        #Captura a exceção
        except mysql.connector.Error as err:
            print("Erro ao realizar a consulta de imobiliaria!")

        #Termina a execução do método
        finally:
            response = self.bd.desconectar(bd, cursor)
            print(response)


from pymixcloud import mixcloud
import requests
import os
import json

def main():
    mc = mixcloud.Mixcloud()
    cloudcasts = mc.user_cloudcasts('ogsy', True)

    cloudcasts = map(lambda x: add_info_from_headers(x, 'http://s3.amazonaws.com/ogsy_podcast/%s.mp3' % x['slug'], ['content-length']), cloudcasts)

    podcasts, mixes = split_on_condition(
        cloudcasts,
        lambda x: x['slug'].startswith('ogscast')
    )

    pages_dir = os.path.join(os.getcwd(), 'pages')

    for podcast in podcasts:
        podcast['type'] = 'podcast'
        write_cloudcast(os.path.join(pages_dir, 'podcast'), podcast)
    for mix in mixes:
        mix['type'] = 'mix'
        write_cloudcast(os.path.join(pages_dir, 'mix'), mix)

def add_info_from_headers(item, source_url, headers, requests=requests):
    r = requests.head(source_url)

    for header in headers:
        if r.headers[header]:
            item[header.replace( '-', '_')] = r.headers[header]

    return item

def split_on_condition(seq, condition):
    a, b = [], []
    for item in seq:
        (a if condition(item) else b).append(item)
    return a, b

def write_cloudcast(path, cloudcast, filename_key='slug'):
    with open(os.path.join(path, cloudcast[filename_key] + '.json'), 'w') as file:
        file.write(json.dumps(cloudcast, indent=4))

if __name__ == "__main__":
    main()


import numpy as np
import scipy
from burf.data.util import remove_nan

def variance(x):
    x = remove_nan("or", x)
    e_x = x - np.nanmean(x)
    a = np.nansum(np.power(e_x))
    n = len(x.reshape(-1))
    return a / (n - 1)

def covariance(x, y):
    x, y = remove_nan("or", x, y)
    e_x = x - np.nanmean(x)
    e_y = y - np.nanmean(y)
    a = np.nansum(e_x * e_y)
    n = len(x.reshape(-1))
    return a / (n - 1)

def t_test(ori, pred, equal = True):
    ori, pred = remove_nan("or", ori, pred)
    return scipy.stats.ttest_ind(ori, pred, equal_var = equal)


# Customer Organizer - Johnathon Kwisses
from cmd_functions import *

def main():
    """Switch for Customer Organizer program.

    The user input correlates to function calls within 'cmd_functions.py'. The function calls then call the various
    class instances based on what the user wants to do. Note: The user must type 'q' to quit the program.

    """
    while True:
        menu()
        cmd = input("> ").lower()

        if cmd == "q":
            break
        elif cmd == "help":
            help_menu()
        elif cmd == "totals":
            get_totals_customers_money()

        else:

            name = input("Customer's name: ").lower()

            if cmd == "new customer":
                new_customer(name)
            elif cmd == "customer info":
                customer_info(name)
            elif cmd == "customer notes":
                customer_notes(name)

            elif cmd == "money received":
                customer_money_received(name)
            elif cmd == "money not received":
                customer_money_not_received(name)

            elif cmd == "add money received":
                add_money_received(name)
            elif cmd == "sub money received":
                sub_money_received(name)

            elif cmd == "add money not received":
                add_money_not_received(name)
            elif cmd == "sub money not received":
                sub_from_money_not_received(name)

            elif cmd == "clear":
                clear(name)
            elif cmd == "clear add":
                clear_add(name)

            # Can be confusing; just remember what you want to do.
            elif cmd == "move new customer":
                old_customer_move(name)
            elif cmd == "move old customer":
                new_customer_move(name)

if __name__ == "__main__":
    main()


from ryu.lib.packet import packet
from ryu.topology.switches import LLDPPacket
import time

class lldp_handler:

    def __init__(self, networkMap):
        self.networkMap = networkMap

    def _find_protocol(self, pkt, name):
        for p in pkt.protocols:
            if hasattr(p, 'protocol_name'):
                if p.protocol_name == name:
                    return p

    def handle(self, msg, callback):
        datapath = msg.datapath
        pkt = packet.Packet(data=msg.data)
        p_eth = self._find_protocol(pkt, "ethernet")
        if (self.networkMap.findPortbyPortMac(p_eth.src) and self.networkMap.findPortByPath(datapath.id, msg.match['in_port'])):
            # Adds edge directly
            self.networkMap.networkMap.add_edge(self.networkMap.findPortbyPortMac(
                p_eth.src), self.networkMap.findPortByPath(datapath.id, msg.match['in_port']))
        else:
            # Drops invalid lldp
            print("%s konnte nicht gefunden werden", datapath.id)

    def _execute_lldp(self, s, callback):
        time.sleep(s)
        for switch in self.networkMap.getAllSwitches():
            parser = switch.dp.ofproto_parser
            ofproto = switch.dp.ofproto
            for port in self.networkMap.getAllSwitchPorts(switch):
                pkt = LLDPPacket.lldp_packet(switch.dp.id, 1, port.hw_addr, 1)
                actions = [parser.OFPActionOutput(port=port.port_no)]
                callback(switch.dp, actions, pkt, ofproto.OFPP_CONTROLLER)
        self.networkMap.report()
        self._execute_lldp(s, callback)


valor = float(input("Digite o valor da casa: "))
salario = float(input("Digite seu salario: "))
anos = int(input("Em quantos anos quer pagar? "))
meses = anos*12
prestacao_mensal = valor/meses

if prestacao_mensal > (0.30*salario):
    print("Empréstimo não aprovado")
else:
    print("Empréstimo aprovado")


#!/usr/bin/env python3
import argparse
import sys
import toml
import json
from datetime import datetime
from haddock.modules.analysis.ana import Ana
from haddock.modules.cns.engine import CNS, CNSError
from haddock.modules.cns.input import InputGenerator
from haddock.modules.docking.it0 import RigidBody
from haddock.modules.docking.it1 import SemiFlexible
from haddock.modules.docking.itw import WaterRefinement
from haddock.modules.setup import Setup, SetupError
from haddock.modules.worker.distribution import JobCreator
from haddock.modules.functions import *
from haddock.version import CURRENT_VERSION

etc_folder = get_full_path('haddock', 'etc')
with open(f'{etc_folder}/default.json', 'r') as fh:
    default_recipes = json.load(fh)
fh.close()

def greeting():
    now = datetime.now().replace(second=0, microsecond=0)
    python_version = sys.version
    print(f'''##############################################
#                                            #
#                   HADDOCK                  #
#                                            #
#             EXPERIMENTAL BUILD             #
#                                            #
##############################################

Starting HADDOCK {CURRENT_VERSION} on {now}

Python {python_version}
''')

def adieu():
    end = datetime.now().strftime('%d/%m/%Y %H:%M:%S')
    salut = bye()
    print(f'''
 Finished at {end}

{salut}
''')

def generate_topology(mol_dic, run_param):
    print('\n++ Generating topologies')

    recipe_name = run_param['stage']['topology']['recipe']
    if recipe_name == 'default':
        recipe_name = default_recipes['topology']

    recipe = f'topology/template/{recipe_name}'
    if not os.path.isfile(recipe):
        print('+ ERROR: Template recipe for topology not found')

    topo_gen = InputGenerator(recipe_file=recipe,
                              input_folder='topology')

    jobs = JobCreator(job_id='generate',
                      job_folder='topology')

    job_counter = 1
    for mol in mol_dic:
        for input_strc in mol_dic[mol]:
            output_strct = input_strc.split('/')[1].split('.')[0]
            input_f = topo_gen.generate(protonation_dic={},
                                        output_pdb=True,
                                        output_psf=True,
                                        input_pdb=input_strc,
                                        input_psf=None,
                                        output_fname=output_strct)

            jobs.delegate(job_num=job_counter,
                          input_file_str=input_f)

            job_counter += 1

    cns = CNS()
    cns.run(jobs)
    output = retrieve_output(jobs)
    topology_dic = molecularize(output)
    return topology_dic

def run_it0(model_dic, run_param):
    print('\n++ Running it0')
    file_list = []
    supported_modules = []

    recipe_name = run_param['stage']['rigid_body']['recipe']
    if recipe_name == 'default':
        recipe_name = default_recipes['rigid_body']

    recipe = f'rigid_body/template/{recipe_name}'
    if not os.path.isfile(recipe):
        print('+ ERROR: Template recipe for rigid-body not found')

    if '.cns' not in recipe:
        # Its a module, look for it
        if recipe not in supported_modules:
            print(f'+ ERROR: {recipe} not supported.')
            exit()

    else:
        # Its a HADDOCK recipe
        it0 = RigidBody()
        job_dic = it0.init(recipe, model_dic, run_param)
        complex_list = it0.run(job_dic)
        file_list = it0.output(complex_list)

    return file_list

def run_it1(model_list, run_param):
    print('\n++ Running it1')
    file_list = []
    supported_modules = []

    recipe_name = run_param['stage']['semi_flexible']['recipe']
    if recipe_name == 'default':
        recipe_name = default_recipes['semi_flexible']

    recipe = f'semi_flexible/template/{recipe_name}'
    if not os.path.isfile(recipe):
        print('+ ERROR: Template recipe for semi-flexible not found')

    if '.cns' not in recipe:
        # Its a module, look for it
        if recipe not in supported_modules:
            print(f'+ ERROR: {recipe} not supported.')
            exit()

    else:
        # Its a HADDOCK recipe
        it1 = SemiFlexible()
        job_dic = it1.init(recipe, model_list)
        complex_list = it1.run(job_dic)

        file_list = it1.output(complex_list)

    return file_list

def run_itw(model_list, run_param):
    print('\n++ Running itw')
    file_list = []
    supported_modules = []

    recipe_name = run_param['stage']['water_refinement']['recipe']
    if recipe_name == 'default':
        recipe_name = default_recipes['water_refinement']

    recipe = f'water_refinement/template/{recipe_name}'
    if not os.path.isfile(recipe):
        print('+ ERROR: Template recipe for water-refinement not found')

    if '.cns' not in recipe:
        # Its a module, look for it
        if recipe not in supported_modules:
            print(f'+ ERROR: {recipe} not supported.')
            exit()

    else:
        # Its a HADDOCK recipe
        itw = WaterRefinement()
        job_dic = itw.init(recipe, model_list)
        complex_list = itw.run(job_dic)
        file_list = itw.output(complex_list)

    return file_list

def run_analysis(pdb_l, reference=None):
    print('\n++ Running analysis')
    ana = Ana(pdb_l)

    ana.extract_energies()

    ana.calculate_haddock_score()

    ana.cluster(cutoff=0.60, size=4)

    ana.run_fastcontact()
    ana.run_dfire()
    ana.run_dockq(reference)

    ana.output()

def pre_process(raw_molecule_dic):

    p = PDB()

    # 1. Add or assign ChainID/SegID
    chainseg_dic = p.fix_chainseg(raw_molecule_dic)

    # 2. Check if it is an ensemble and split it
    ensemble_dic = p.treat_ensemble(chainseg_dic)

    # 3. Clean problematic parts and remove aa not present in the topology
    clean_molecule_dic = p.sanitize(ensemble_dic)

    return clean_molecule_dic

def clean_run():
    allowed_folders = ['topology/', 'data/', 'analysis/']
    folder_l = glob.glob('*/')
    for f in folder_l:
        if f not in allowed_folders:
            out_l = glob.glob(f'{f}*.out')
            out_l.sort()
            for out_f in out_l[1:]:
                os.remove(out_f)
            inp_l = glob.glob(f'{f}*.inp')
            inp_l.sort()
            for inp_f in inp_l[1:]:
                os.remove(inp_f)

if __name__ == '__main__':

    greeting()

    parser = argparse.ArgumentParser(description='Setup your HADDOCK run')
    parser.add_argument("run_file", help="The run file containing the parameters of your run (.toml)")
    parser.add_argument("--setup", help="Only setup the run, do not execute", action="store_true", default=False)
    args = parser.parse_args()

    setup_dictionary = toml.load(args.run_file)
    s = Setup(setup_dictionary)
    s.prepare_folders()
    s.configure_recipes()

    if args.setup:
        adieu()
        SystemExit(f'+ Running Setup only mode (--setup), exiting now')

    processed_molecules = pre_process(setup_dictionary['molecules'])

    print(f'+ Executing {s.run_dir}')
    os.chdir(s.run_dir)

    valid_cns = CNS().validate_cns()
    if not valid_cns:
        raise CNSError('+ ERROR: There is something wrong with CNS')

    run_f = 'data/run.toml'
    if not os.path.isfile(run_f):
        raise SetupError('+ ERROR: data/run.toml not found, make sure you are in the correct folder.')
    run_parameters = toml.load(run_f)

    molecules = get_begin_molecules('data/')

    # 1. Generate topologies ==========================================================================================#
    begin_models = generate_topology(molecules, run_parameters)

    # 2. Dock =========================================================================================================#

    # Input:
    #  molecule dictionary, keys=molecule, values=list of tuples containing .psf and .pdb
    #   example:
    # {
    #  'mol1': [('begin/mol1_1.psf', 'begin/mol1_1.pdb')],
    #  'mol2': [('begin/mol2_1.psf', 'begin/mol2_1.pdb'),
    #           ('begin/mol2_2.psf', 'begin/mol2_2.pdb')]
    #  'mol3': [('begin/mol3_1.psf', 'begin/mol3_1.pdb')]
    #   }

    # Output:
    #  List of complexes, SORTED according to ranking!
    #   example:
    # ['complex_42.pdb', 'complex_10.pdb', 'complex_23.pdb']

    # Rigid-body (it0)
    # > Get sampling parameters
    rigid_sampling = run_parameters['stage']['rigid_body']['sampling']

    # > Run rigid-body docking
    rigid_complexes = run_it0(begin_models, run_parameters)
    # rigid_complexes = run_lightdock(begin_models)

    # Semi-flexible (it1)
    semiflex_sampling = run_parameters['stage']['semi_flexible']['sampling']

    if semiflex_sampling > rigid_sampling:
        print(f'+ WARNING: Semi Flexible sampling ({semiflex_sampling}) is higher than Rigid-body sampling ({rigid_sampling})')
        print(f'++ Passing {rigid_sampling} complexes to Semi Flexible stage')

    # > Select top N models
    semi_flexible_input_complexes = rigid_complexes[:semiflex_sampling]

    # > Run semi-flexible refinement
    semi_flexible_complexes = run_it1(semi_flexible_input_complexes, run_parameters)

    # Water refinement (itw)
    waterref_sampling = run_parameters['stage']['water_refinement']['sampling']

    # > Select top N models
    if semiflex_sampling > rigid_sampling:
        print(f'+ WARNING: Water refinement n ({waterref_sampling}) is higher than Semi-flexible sampling {semiflex_sampling}')
        print(f'++ Passing {semiflex_sampling} complexes to Water refinement')

    water_refinement_input_complexes = semi_flexible_complexes[:waterref_sampling]

    water_refinement_complexes = run_itw(water_refinement_input_complexes, run_parameters)

    # 3. Analysis =====================================================================================================#

    try:
        reference_structure = setup_dictionary['execution_parameters']['reference']['analysis']
    except KeyError:
        reference_structure = None

    run_analysis(water_refinement_complexes, reference_structure)

    # 4. Done! ========================================================================================================#

    clean_run()

    adieu()

    # =================================================================================================================#


# -*- coding: utf8 -*-
import os
import gensim
from nltk.corpus import stopwords

import settings
from lib.topic_models import pre_process

from time import time
import logging

logger = logging.getLogger('text_similar')
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s',
                    level=logging.INFO)

additional_stops = pre_process.STOPWORDS

class VectorModels(object):
    def __init__(self, data_loc, tmp_folder,
                 stopwords=additional_stops, sample_size=None):

        self.data_loc = data_loc
        self.tmp_folder = tmp_folder
        self.stopwords = stopwords

    def build_corpus(self, no_below=15, no_above=0.5):
        mycorpus = pre_process.MyCorpus(self.data_loc)
        mycorpus = self.filter_stopwords(mycorpus, self.stopwords)

        mycorpus.dictionary.filter_extremes(no_below=no_below,
                                            no_above=no_above
                                            )

        mycorpus.dictionary.compactify()
        return mycorpus, mycorpus.dictionary

    def filter_stopwords(self, mycorpus,
                         additional_stopwords=additional_stops):
        stopset = set(stopwords.words('english')).union(additional_stopwords)

        stop_ids = [mycorpus.dictionary.token2id[stopword]
                    for stopword in stopset
                    if stopword in mycorpus.dictionary.token2id]

        remove_ids = [mycorpus.dictionary.token2id[token]
                      for token in mycorpus.dictionary.itervalues()
                      if len(token) < 3]

        mycorpus.dictionary.filter_tokens(stop_ids + remove_ids)
        mycorpus.dictionary.compactify()
        return mycorpus

    def load_corpus(self, no_below=15, no_above=0.5):
        corpus_file = os.path.join(self.tmp_folder, 'corpus.mm')
        dictionary_file = os.path.join(self.tmp_folder, 'corpus.dict')
        if os.path.isfile(corpus_file) and os.path.isfile(dictionary_file):
            mycorpus = gensim.corpora.mmcorpus.MmCorpus(corpus_file)
            dictionary = gensim.corpora.Dictionary.load(dictionary_file)
        else:
            mycorpus, dictionary = self.build_corpus(no_below, no_above)
            dictionary.save(dictionary_file)

            gensim.corpora.MmCorpus.serialize(corpus_file,
                                              mycorpus,
                                              id2word=dictionary
                                              )
        return mycorpus, dictionary

    def build_tfidf_model(self, dictionary):
        tfidf_file = os.path.join(self.tmp_folder, 'tfidf_model.mm')
        if os.path.isfile(tfidf_file):
            tfidf_model = gensim.models.TfidfModel.load(tfidf_file)
        else:
            tfidf_model = gensim.models.TfidfModel(id2word=dictionary,
                                                   dictionary=dictionary
                                                   )
            tfidf_model.save(tfidf_file)
        return tfidf_model

    def build_tfidf_corpus(self, corpus, dictionary):
        tfidf_file = os.path.join(self.tmp_folder, 'tfidf_corpus.mm')
        if os.path.isfile(tfidf_file):
            tfidf_corpus = gensim.corpora.mmcorpus.MmCorpus(tfidf_file)
        else:
            tfidf_model = self.build_tfidf_model(dictionary)
            tfidf_corpus = tfidf_model[corpus]
            gensim.corpora.MmCorpus.serialize(tfidf_file, tfidf_corpus)
        return tfidf_corpus

if __name__ == '__main__':
    # Build the Corpus
    start_corpus = time()
    data_loc = os.path.join(settings.project_root, 'tmp', 'text_corpus.dat')
    tmp_folder = os.path.join(settings.project_root, 'tmp', 'modeldir')

    vectors = VectorModels(data_loc, tmp_folder)
    corpus, dictionary = vectors.load_corpus()
    vectors.build_tfidf_corpus(corpus, dictionary)

    stop_corpus = time()
    corpus_time = round(stop_corpus - start_corpus, 3)
    logger.info('time taken to build corpus = {}s'.format(corpus_time))


from abc import ABC, abstractmethod
import numpy as np
import random

class Dataset(ABC):
    """Input to all GenericModel subclasses. Overload to implement a variety of datasets.
    For a dataset to apply, must be able to index into dataset to return dictionary of features
    for a single example. Must implement __len__ function to specify size of dataset. Complete
    processing of dataset is encouraged to occur within a Dataset object. If retrieving single
    examples during training is too slow, the generate_batches() function can be overridden."""

    @abstractmethod
    def __getitem__(self, index):
        # Get a single item as an index from the dataset.
        # {'feature1': np_array1, 'feature2': np_array2}
        # {'history': np_array1, 'response': np_array2}
        pass

    @abstractmethod
    def __len__(self):
        # Return the length of the dataset.
        pass

    def generate_batches(self, batch_size, shuffle=False):
        # Yield one batch from the dataset
        m = len(self)

        # We want the ability to have an empty dataset,
        # for output tensors which require no input
        if m == 0:
            return

        indices = np.arange(m)
        if shuffle:
            np.random.shuffle(indices)
        num_batches = m // batch_size + 1

        for i in range(num_batches):
            index_batch = indices[i * batch_size:i * batch_size+batch_size]
            if len(index_batch) == 0:
                break

            batch_data = [self[each_index] for each_index in index_batch]
            #
            # if len(batch_data) > 32:
            #     print('batch_data > 32!')
            #     print('batch_size: %s' % batch_size)
            #     print('num_batches: %s' % num_batches)
            #     print('Dataset len: %s' % len(self))
            #     print('Num indices: %s' % len(indices))

            result = {}

            assert batch_data is not None
            assert batch_data[0] is not None

            for key in batch_data[0]:
                result[key] = np.stack([d[key] for d in batch_data], axis=0)

            yield concatenate_batch_dictionaries(batch_data, single_examples=True)

    def split(self, fraction, seed='seed', max_examples=None):
        """Split dataset into two subset datasets. 'fraction' argument decides
        what fraction of the original dataset is used to make the first subset.
        The remaining examples are used to create the second subset. Typically
        used for train/test splits. By DEFAULT there is a seed, as randomly
        splitting training and test sets on each run can leave training examples
        in the validation set and ultimately a higher validation accuracy.

        Arguments:
            - fraction: fraction of examples in first subset
            - seed: random seed for splitting dataset
            - max_examples: number of examples to use from dataset in split
        """

        m = len(self) if max_examples is None else max_examples
        dataset_indices = list(range(m))

        if seed is not None:
            random.seed(seed)

        random.shuffle(dataset_indices)
        subset_divider_index = int(m * fraction)
        first_subset = DatasetPtr(self, dataset_indices[:subset_divider_index])
        second_subset = DatasetPtr(self, dataset_indices[subset_divider_index:])
        return first_subset, second_subset

    def to_numpy(self, feature):
        """Grab all examples of feature and concatenate them into a numpy array.

        Returns: a numpy array"""
        examples = []
        for index in range(len(self)):
            examples.append(self[index][feature])

        return np.stack(examples, axis=0)

class RenameDataset(Dataset):
    def __init__(self, dataset, mappings):
        """Takes features from dataset and renames
        them.

        mappings - dictionary with keys as
        features in original dataset and values
        as the renamed versions"""
        self.dataset = dataset
        self.mappings = mappings

        # add remaining features back as pointing to themselves
        features = dataset[0].keys()
        for feature in features:
            if feature not in mappings:
                self.mappings[feature] = feature

    def __getitem__(self, index):
        return {self.mappings[feature]: self.dataset[index][feature] for feature in self.dataset[index]}

    def __len__(self):
        return len(self.dataset)

class DatasetPtr(Dataset):
    def __init__(self, dataset, indices):
        """Take as argument a dataset, and a list of indices
        into that dataset. Sample from those indices to create
        a subset dataset of the original dataset. Typically used
        for breaking a dataset into subsets."""
        self.dataset = dataset
        self.indices = indices

    def __getitem__(self, index):
        """Grab the index'th index into the dataset
        and return that data example."""
        return self.dataset[self.indices[index]]

    def __len__(self):
        return len(self.indices)

class EmptyDataset(Dataset):
    """Dataset with no features,
    runs for only a single batch"""
    def __getitem__(self, index):
        return {}

    def __len__(self):
        return 1

class DictionaryDataset(Dataset):
    def __init__(self, batch_feature_dict):
        """Create simple dataset from a dictionary of feature_name:numpy_feature entries.
        Interpret as a series of features, each having a name the model can refer to.
        Optionally, define dictionary of scalar features which remain the same across
        examples. The first dimension of all features must be equal to the length
        of the dataset."""
        self.batch_feature_dict = batch_feature_dict

        # Determine length of dataset
        self.length = -1
        for feature_name in self.batch_feature_dict:
            feature_length = self.batch_feature_dict[feature_name].shape[0]
            if self.length == -1:
                self.length = feature_length
            else:
                if self.length != feature_length:
                    raise ValueError('All batch features must have same length')

        if self.length <= 0:
            raise ValueError('Cannot have zero-length dataset.')

    def __getitem__(self, index):
        item_dict = {}
        for feature_name in self.batch_feature_dict:
            item_dict[feature_name] = self.batch_feature_dict[feature_name][index]
        return item_dict

    def __len__(self):
        return self.length

    def to_dict(self):
        return self.batch_feature_dict

class MergeDataset(Dataset):
    def __init__(self, datasets, concat_duplicates=False):
        """Dataset intended to merge the features of multiple datasets.

        Arguments:
            - datasets: list of Dataset objects, all with the same length
        """
        self.datasets = datasets.copy()

        for index in range(len(self.datasets)):
            if isinstance(self.datasets[index], dict):
                self.datasets[index] = DictionaryDataset(self.datasets[index])

        # Get all keys
        self.keys = []
        for dataset in self.datasets:
            for feature in dataset[0]:
                if feature not in self.keys:
                    self.keys.append(feature)

        # Create mapping from keys (features) to datasets that contain them
        self.has_duplicates = False
        self.feat2data = {}
        for feature in self.keys:
            self.feat2data[feature] = [dataset for dataset in self.datasets if feature in dataset[0]]
            if len(self.feat2data[feature]) == 1:
                self.feat2data[feature] = self.feat2data[feature][0]  # only one item in list, remove list wrapper
            else:
                self.has_duplicates = True

        assert not self.has_duplicates or concat_duplicates

        self.length = len(self.datasets[0])
        for each_dataset in self.datasets:
            assert len(each_dataset) == self.length

    def __getitem__(self, index):
        output_dict = {}
        for feature in self.feat2data:
            data = self.feat2data[feature]  # datasets having this key (feature name)
            if isinstance(data, Dataset):
                output_dict[feature] = data[index][feature]
            else:
                # it is a list of datasets, we concatenate everything along the feature axis
                output_dict[feature] = np.concatenate([d[index][feature] for d in data], axis=0)

        return output_dict

    def __len__(self):
        return self.length

def concatenate_batch_dictionaries(batch_dictionaries, single_examples=False):
    """Concatenates numpy dictionaries. If numpy arrays represent single examples (no batch axis),
    set single_examples=True. Otherwise false.
    batch_dictionaries - list of dictionaries, all containing identical keys, each key being
    a feature name
    single_examples - decides whether to concatenate (for batches) or to stack (for single vectors)"""
    result = {}

    # If no features, return None
    # if len(batch_dictionaries) == 0:
    #     return None

    for key in batch_dictionaries[0]:
        if single_examples or len(batch_dictionaries[0][key].shape) == 0:
            tensors = [d[key] for d in batch_dictionaries]
            result[key] = np.stack(tensors, axis=0)
        else:
            result[key] = np.concatenate([d[key] for d in batch_dictionaries], axis=0)

    return result


# -*- coding: utf-8 -*-
"""
Created on Mon Mar 12 16:40:15 2018

@author: woon.zhenhao
"""

import requests
import json
import pandas as pd
import MPCall
import IMSCall
import dbconnector as db
import time
from rq import Queue
from worker import conn
import logging
import sys

#logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)

def dataFrameToJsonConverter(df):
    columns=list(df)
    length=len(df)
    result={}

    for i in list(range(length)):
        line=df.iloc[[i]]
        lineres={}
        j=0

        for tit in columns:
            lineres[tit]=str(line.iloc[0,j])
            j=j+1

        result[str(i)]=lineres

    return result

def updateOrders():
    path="orderupdate.csv"
    df=pd.read_csv(path)
    MPCall.updateOrders(df)

def updateInventories(sellerid, recon):
    start = time.time()
    skulist=MPCall.getMCCPInventories(sellerid)
    end1 = time.time()
    print("skulist completed: "+ str(end1-start))
    collatedinven=IMSCall.getIMSInventory(sellerid, skulist)
    end2 = time.time()
    print("collated inven completed: "+ str(end2-start))
    if (recon=="true"):
        reconResult=MPCall.reconInven(sellerid, collatedinven)
        result=dataFrameToJsonConverter(reconResult)
        print("return recon result")
        return result
    else:
        result=dataFrameToJsonConverter(collatedinven)
        print("return collated inven")
        return result

def updateInventories2(sellerid, recon):
    start=time.time()
    print("seller id: "+ str(sellerid))
    skulist=MPCall.getMCCPInventories(sellerid)
    end1 = time.time()
    print("skulist completed: "+ str(end1-start))
    collatedinven=IMSCall.getIMSInventory2(sellerid, skulist)
    end2 = time.time()
    print("collated inven completed: "+ str(end2-start))
    if (recon=="true"):
        reconResult=MPCall.reconInven(sellerid, collatedinven)
        result=dataFrameToJsonConverter(reconResult)
        print("return recon result")
        print(result)
        return result
    else:
        result=dataFrameToJsonConverter(collatedinven)
        print("return collated inven")
        print(result)
        return result

def updateSingularSKU(imssku, sellerid):
    qty=IMSCall.getSingleIMSInventory(imssku, sellerid)
    result=MPCall.updateInven(imssku, qty, sellerid)
    retVal={
        "result": result,
        "qty": qty
            }
    return retVal

def getAccountDetails():
    df=db.getAccountDetails()
    result=dataFrameToJsonConverter(df)
    return result

def testworker():
    print("test worker ")

#df=updateSingularSKU("merries3",1)
#df=updateInventories2('1', "true")


import os
import sys
import pygame
import random
from pygame.locals import *

if not pygame.font: print ('Warning, fonts disabled')
if not pygame.mixer: print ('Warning, sound disabled')

pygame.init()

running = True

# COLORS
RED = (255, 0, 0)
WHITE = (255, 255, 255)
GREY = (32, 32, 32)
LIGHTER_GREY = (48, 48, 48)

# Screen settings
WIDTH = 800
HEIGHT = 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))

# FPS and time module
FPS = 60
clock = pygame.time.Clock()

# Window Title
pygame.display.set_caption('GOTY')

# LOADING IMAGES

#ICON
gameIcon = pygame.image.load(os.path.join("data", "imgs", "icon.png"))
pygame.display.set_icon(gameIcon)

# BG img
bg = pygame.image.load(os.path.join("data", "imgs", "bg.png")).convert_alpha()
bg_y = 0

#Cat boss imgs loading
cat_boss = []
for i in range(7):
    filename = 'catbo0{}.png'.format(i)
    img = pygame.image.load(os.path.join("data", "imgs", filename)).convert_alpha()
    cat_boss.append(img)

# Explosion animations loading
explosion_anim = []
for i in range(3):
    filename = 'explosion{}.png'.format(i)
    img = pygame.image.load(os.path.join("data", "imgs", filename)).convert_alpha()
    explosion_anim.append(img)

# Player img
player_img = pygame.image.load(os.path.join
    ("data", "imgs", "player_ship.png")).convert_alpha()

player_img2 = pygame.image.load(os.path.join
    ("data", "imgs", "player_ship2.png")).convert_alpha()

player_img3 = pygame.image.load(os.path.join
    ("data", "imgs", "player_ship3.png")).convert_alpha()

player_img4 = pygame.image.load(os.path.join
    ("data", "imgs", "player_ship4.png")).convert_alpha()

player_dmg = pygame.image.load(os.path.join
    ("data", "imgs", "player_ship_damage.png")).convert_alpha()

player_blinking_sprites = [player_img]*3 + [player_dmg]*3

# Img for hp
player_hp_img = pygame.image.load(os.path.join("data", "imgs", "hp.png")).convert_alpha()

# Enemy img
enemy_img = pygame.image.load(os.path.join("data", "imgs", "enemy_ship.png")).convert_alpha()
enemy_img2 = pygame.image.load(os.path.join("data", "imgs", "enemy_ship2.png")).convert_alpha()

# Player bullet
player_bullet = pygame.image.load(os.path.join("data", "imgs", "player_bullet.png")).convert_alpha()

# Enemy bullet
enemy_bullet = pygame.image.load(os.path.join("data", "imgs", "enemy_bullet.png")).convert_alpha()

# BOSS bullet
boss_bullet = pygame.image.load(os.path.join("data", "imgs", "boss_bull.png")).convert_alpha()

# POWERUP
hp_powerup = pygame.image.load(os.path.join("data", "imgs", "powerup_hp.png")).convert_alpha()

# FIRST BOSS
boss_v1_img = pygame.image.load(os.path.join("data", "imgs", "boss_v1.png")).convert_alpha()
boss_v1_death = pygame.image.load(os.path.join("data", "imgs", "boss_v228.png")).convert_alpha()

# LOADING IMGS END

# LOADING SOUNDS

# Player shooting sound
player_shoot_sound = pygame.mixer.Sound(os.path.join("data", "sounds", "player_shoot.ogg"))

# Cat sounds
cat_dmg_sound = pygame.mixer.Sound(os.path.join("data", "sounds", "cat_dmg.wav"))

# Loading and playing bg sound
pygame.mixer.music.load(os.path.join("data", "sounds", "bg3.ogg"))
pygame.mixer.music.play(-1,0.0)

# LOADING SOUNDS END

# FUNC START

# FONTS
font_name = pygame.font.match_font('arial')
def draw_text(surf, text, size, x, y):
    font = pygame.font.Font(font_name, size)
    text_surface = font.render(text, True, WHITE)
    text_rect = text_surface.get_rect()
    text_rect.midtop = (x, y)
    surf.blit(text_surface, text_rect)

# FUNCTION FOR DRAWING HP
def draw_hp(surf, x, y, hp, img):
    for i in range(hp):
        img_rect = img.get_rect()
        img_rect.x = x + 40 * i
        img_rect.y = y
        surf.blit(img, img_rect)

def draw_boss_hp(surf, x, y, amount):
    if amount < 0:
        amount = 0

    BAR_LENGTH = 400
    BAR_HEIGHT = 25
    fill = (amount / 100) * BAR_LENGTH
    outline_rect = pygame.Rect(x, y, BAR_LENGTH, BAR_HEIGHT)
    fill_rect = pygame.Rect(x, y, fill, BAR_HEIGHT)
    pygame.draw.rect(surf, RED, fill_rect)
    pygame.draw.rect(surf, WHITE, outline_rect, 2)

reset = False
def button(text,x,y,width,height,active,inactive,action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    global paused, reset, running

    if x+width > mouse[0] > x and y+height > mouse[1] > y:
        pygame.draw.rect(screen, active, (x, y, width, height))
        if click[0] == 1 and action != None:
            if action == "play":
                while game_loop():
                    reset = False
            elif action == "continue":
                paused = False
            elif action =="quit":
                pygame.quit()
                quit()
            elif action == "reset":
                reset = True
    else:
        pygame.draw.rect(screen, inactive, (x, y, width, height))

    draw_text(screen, text, 32, 400, y + 5)

# FUNC END

# INTRO
def game_intro():

    intro = True

    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        screen.blit(bg, (0,0))
        draw_text(screen, ("MENU"), 80, WIDTH / 2, HEIGHT - 650)

        button("START", 275, 300, 250, 50, LIGHTER_GREY, GREY, "play")
        button("QUIT", 275, 400, 250, 50, LIGHTER_GREY, GREY, "quit")

        pygame.display.update()
        clock.tick(FPS)

# END OF GAME INTO FUNC

# INTRO
paused = False

def game_paused():
    global paused
    paused = True
    while paused:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        screen.blit(bg, (0,0))
        draw_text(screen, ("MENU"), 80, WIDTH / 2, HEIGHT - 650)
        button("CONTINUE", 275, 300, 250, 50, LIGHTER_GREY, GREY, "continue")
        button("RESET", 275, 380, 250, 50, LIGHTER_GREY, GREY, "reset")
        button("QUIT", 275, 460, 250, 50, LIGHTER_GREY, GREY, "quit")

        pygame.display.update()
        clock.tick(FPS)
        if reset:
            paused = False
            return True
    return False

# END OF GAME INTO FUNC

# GAME FUNC
def game_loop():
    global bg_y, running
    #GAME OVER
    game_over = False
    victory = False
    # SCORE
    score = 0

        # CLASSES

    # Creating a Player class
    class Player(pygame.sprite.Sprite):
        def __init__(self):
            pygame.sprite.Sprite.__init__(self)
            self.image = player_img
            self.rect = self.image.get_rect()
            self.rect.centerx = WIDTH / 2
            self.rect.bottom = HEIGHT - 10
            self.speedx = 0
            self.speedy = 0
            self.hp = 3
            self.blinking = False
            self.blink_counter = 0
            self.sprite_counter = 0

        def update(self):
            self.speedx = 0

            # Movement while pressing keys + ANIMATION(imgs)!
            keystate = pygame.key.get_pressed()
            if keystate[pygame.K_LEFT]:
                self.speedx = -8
                self.image = player_img2
            if keystate[pygame.K_RIGHT]:
                self.speedx = 8
                self.image = player_img2
            self.rect.x += self.speedx
            if self.speedx == 0:
                self.image = player_img

            # Img while shooting

            # If player isnt moving
            elif (keystate[pygame.K_UP] and self.speedx == 0):
                self.image = player_img3

            # If moving
            elif (keystate[pygame.K_UP] and self.speedx != 0):
                self.image = player_img4

            # Bg borders for player
            if self.rect.right > WIDTH:
                self.rect.right = WIDTH
            if self.rect.left < 0:
                self.rect.left = 0

            if self.blinking:
                self.image = player_blinking_sprites[self.sprite_counter]
                self.sprite_counter += 1
                if self.sprite_counter >= len(player_blinking_sprites):
                    self.sprite_counter = 0
                    self.blink_counter += 1

                if self.blink_counter > 3:
                    self.blink_counter = 0
                    self.blinking = False

            if game_over:
                self.rect.bottom = HEIGHT + 100

        # Spawning bullets
        def shoot(self):
            player_shoot_sound.play()
            pl_bullet = PlayerBull(self.rect.centerx, self.rect.top)
            all_sprites.add(pl_bullet)
            pl_bullets.add(pl_bullet)

    # Player bullets class
    class PlayerBull(pygame.sprite.Sprite):
        def __init__(self, x, y):
            pygame.sprite.Sprite.__init__(self)
            self.image = player_bullet
            self.rect = self.image.get_rect()
            self.rect.bottom = y
            self.rect.centerx = x
            self.speedy = -10

        def update(self):
            self.rect.y += self.speedy
            # kill if it moves off the top of the screen
            if self.rect.bottom < 0:
                self.kill()

    # Creating enemy class
    class Enemy(pygame.sprite.Sprite):
        def __init__(self):
            pygame.sprite.Sprite.__init__(self)
            self.image = enemy_img
            self.rect = self.image.get_rect()
            self.rect.x = random.randrange(WIDTH - self.rect.width)
            self.rect.y = random.randrange(-100, -40)
            self.bspeedy = random.randrange(1, 3)
            self.speedy = self.bspeedy
            self.frame = 0
            self.last_update = pygame.time.get_ticks()
            self.frame_rate = 30

        def update(self):
            self.rect.y += self.speedy
            if self.rect.top > HEIGHT + 10:
                self.rect.x = random.randrange(WIDTH - self.rect.width)
                self.rect.y = random.randrange(-100, -40)
                self.speedy = random.randrange(1, 3)

            if self.frame % self.frame_rate == 0:
                self.frame = 0
                self.image = enemy_img
                #self.speedy = self.bspeedy
            elif self.frame > self.frame_rate/2:
                self.image = enemy_img2
                #self.speedy = self.bspeedy+1
            self.frame += 1

            if score == 5000:
                self.kill()

    # Enemy bullets class
    class EnemyBull(pygame.sprite.Sprite):
        def __init__(self, x, y, speedy=3):
            pygame.sprite.Sprite.__init__(self)
            self.image = enemy_bullet
            self.rect = self.image.get_rect()
            self.rect.bottom = y
            self.rect.centerx = x
            self.speedy = speedy

        def update(self):
            self.rect.y += self.speedy
            # kill if it moves off the top of the screen
            if self.rect.bottom > HEIGHT:
                self.kill()

            elif score == 5000:
                self.kill()

    class Explosion(pygame.sprite.Sprite):
        def __init__(self, center):
            pygame.sprite.Sprite.__init__(self)
            self.image = explosion_anim[0]
            self.rect = self.image.get_rect()
            self.rect.center = center
            self.frame = 0
            self.last_update = pygame.time.get_ticks()
            self.frame_rate = 100

        def update(self):
            now = pygame.time.get_ticks()
            if now - self.last_update > self.frame_rate:
                self.last_update = now
                self.frame += 1
                if self.frame == len(explosion_anim):
                    self.kill()
                else:
                    center = self.rect.center
                    self.image = explosion_anim[self.frame]
                    self.rect = self.image.get_rect()
                    self.rect.center = center

    class Powerup(pygame.sprite.Sprite):
        def __init__(self, center):
            pygame.sprite.Sprite.__init__(self)
            self.image = hp_powerup
            self.rect = self.image.get_rect()
            self.rect.center = center
            self.speedy = 2

        def update(self):
            self.rect.y += self.speedy

            if self.rect.top > HEIGHT:
                self.kill()

            elif score == 5000:
                self.kill()

    # FIRST BOSS!!
    class Boss_v001(pygame.sprite.Sprite):
        def __init__(self):
            pygame.sprite.Sprite.__init__(self)
            self.image = boss_v1_img
            self.rect = self.image.get_rect()
            self.rect.centerx = WIDTH / 2
            self.rect.top = -300
            self.speedy = 10
            self.killed = False

        def update(self):
            self.rect.y += self.speedy
            if self.rect.bottom > HEIGHT / 2:
                self.speedy = 0

            if score > 5000:
                self.image = boss_v1_death
                self.speedy = -10
                if self.rect.bottom == HEIGHT - 800:
                    self.kill()

    # MEOW BOSS
    class SuperBoss(pygame.sprite.Sprite):
        def __init__(self):
            pygame.sprite.Sprite.__init__(self)
            self.image = cat_boss[0]
            self.rect = self.image.get_rect()
            self.rect.centerx = WIDTH / 2
            self.rect.top = -300
            self.speedy = 3
            self.speedx = 0
            self.shot_animation = False
            self.shot_delay = 3
            self.shot_sprite = 1
            self.shot_timer = 0
            self.hp = 100
            self.killed = False

        def update(self):
            self.rect.y += self.speedy
            self.rect.x += self.speedx
            if self.rect.bottom > HEIGHT / 2:
                self.speedy = 0

            if self.rect.right == 800 and self.speedx > 0:
                self.speedx *= -1
            elif self.rect.left == 0 and self.speedx < 0:
                self.speedx *= -1

            if self.shot_animation:
                self.shot_timer += 1
                if self.shot_timer == self.shot_delay:
                    self.shot_timer = 0
                    self.shot_sprite += 1
                    if self.shot_sprite >= len(cat_boss):
                        self.shot_sprite = 0
                        self.shot_timer = 0
                        self.shot_animation = False
                    else:
                        self.image = cat_boss[self.shot_sprite]

            if self.hp <= 0:
                self.kill()
                self.killed = True

        def start_shoot(self):
            self.shot_sprite = 0
            self.shot_timer = 0
            self.shot_animation = True

        def move(self):
            if self.speedx == 0 and self.speedy == 0:
                self.speedx = 5 # *random.choice([1 ,-1])
            if self.rect.right > 760 and self.speedx > 0:
                self.speedx *= -1
            elif self.rect.left < 40 and self.speedx < 0:
                self.speedx *= -1

    class BossBull(pygame.sprite.Sprite):
        def __init__(self, x, y, speedy):
            pygame.sprite.Sprite.__init__(self)
            self.image = boss_bullet
            self.rect = self.image.get_rect()
            self.rect.bottom = y
            self.rect.centerx = x
            self.speedy = speedy

        def update(self):
            self.rect.y -= self.speedy
            # kill if it moves off the top of the screen
            if self.rect.top > HEIGHT:
                self.kill()

    # CLASSES END

    # Groupping all sprites
    all_sprites = pygame.sprite.LayeredUpdates()
    enemies = pygame.sprite.Group()
    pl_bullets = pygame.sprite.Group()
    e_bullets = pygame.sprite.Group()
    powerups = pygame.sprite.Group()

    # Creating new object as instance of player class
    player = Player()

    # Adding player sprites to all sprites
    all_sprites.add(player)

    # Spawning enemies
    for i in range(3):
        e = Enemy()
        all_sprites.add(e)
        enemies.add(e)
    enemy = Enemy()

    boss = Boss_v001()
    bossv1 = pygame.sprite.Group()

    cat = SuperBoss()
    catsg = pygame.sprite.Group()

    while running:
        for event in pygame.event.get():
            # Closing the window!
            if event.type == pygame.QUIT:
                running = False
                pygame.quit()

            # IF up arrow - shoot
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    player.shoot()
                elif event.key == pygame.K_ESCAPE:
                    stop = game_paused()
                    if stop:
                        return stop
                elif event.key == pygame.K_q:
                    pygame.quit()

        # Making ENEMIES shoot bullets:

         # The timer is the time in seconds until the enemy shoots.
        enemy_timer = random.uniform(0, 1)  # Random float 0 and 1
        dt = 0

        # clock.tick returns the time that has passed since the last frame.
        dt = clock.tick(FPS) / 1000  # / 1000 to convert it to seconds.

        clock.tick(FPS)

        # Decrease the timer by the delta time.
        enemy_timer -= dt
        if enemy_timer <= 0 and score < 5000 and game_over == False:  # Ready to fire.
            # Pick a random enemy to get the x and y coords.
            random_enemy = random.choice(enemies.sprites())
            enemy_x = random_enemy.rect.centerx
            enemy_y = random_enemy.rect.bottom
            # Create a bullet and add it to the sprite groups.
            bullet = EnemyBull(enemy_x, enemy_y, random_enemy.speedy+2)
            all_sprites.add(bullet)
            e_bullets.add(bullet)
            timer = random.uniform(0, 1)  # Reset the timer.

        # Update
        all_sprites.update()

        # Check if enemy hits player
        hits = pygame.sprite.spritecollide(player, enemies, False)
        for hit in hits:
            player.hp -= 1
            player.blinking = True
            hit.kill()
            e = Enemy()
            all_sprites.add(e)
            enemies.add(e)
            expl = Explosion(hit.rect.center)
            all_sprites.add(expl)

            expl = Explosion(player.rect.center)
            all_sprites.add(expl)
            if player.hp == 0:
                game_over = True  # game over!!

        # Check if bullet hits an enemy
        hits = pygame.sprite.groupcollide(enemies, pl_bullets, True, True)
        for hit in hits:
            score += 50
            e = Enemy()
            all_sprites.add(e)
            enemies.add(e)
            expl = Explosion(hit.rect.center)
            all_sprites.add(expl)

            if random.random() > 0.85:
                powup = Powerup(hit.rect.center)
                all_sprites.add(powup)
                powerups.add(powup)

        # Check if powerup hits a player
        hits = pygame.sprite.spritecollide(player, powerups, True)
        for hit in hits:
            if player.hp != 3:
                player.hp += 1

        # Check if bullet hits a player
        hits = pygame.sprite.spritecollide(player, e_bullets, False)
        for hit in hits:
            player.hp -= 1
            player.blinking = True
            hit.kill()
            expl = Explosion(player.rect.center)
            all_sprites.add(expl)

            if player.hp == 0:
                game_over = True  # game over!!

        # BACKGROUND START

        # Drawing scrolling BG
        if game_over == False:
            rel_y = bg_y % bg.get_rect().height
            screen.blit(bg, (0, rel_y - bg.get_rect().height))
            if rel_y < HEIGHT:
                screen.blit(bg, (0, rel_y))
            bg_y -= 1

            # Drawing sprites
            all_sprites.draw(screen)

        elif game_over:
            screen.blit(bg, (0,0))
            draw_text(screen, ("GAME OVER"), 48, WIDTH / 2, HEIGHT - 600)
            draw_text(screen, ("PRESS ESC TO ENTER THE MENU"), 20, WIDTH / 2, HEIGHT - 530)
            draw_text(screen, ("YOU SCORED " + (str(score))), 20, WIDTH / 2, HEIGHT - 500)

        if victory:
            screen.blit(bg, (0,0))
            draw_text(screen, ("VICTORY!!"), 54, WIDTH / 2, HEIGHT - 600)
            draw_text(screen, ("YOU SCORED " + (str(score))), 20, WIDTH / 2, HEIGHT - 500)
            draw_text(screen, ("MADE WITH LOVE BY ANASTASIYA OVINNIKOVA"), 20, WIDTH / 2, HEIGHT - 450)
            draw_text(screen, ("PRESS ESC TO ENTER THE MENU"), 20, WIDTH / 2, HEIGHT - 400)

        # BACKGROUND END

        # DRAWING SPRITES

        # FIRST BOSS

        if score >= 5000:
            bossv1.add(boss)
            all_sprites.add(boss)

            if boss.speedy == 0:
                hits = pygame.sprite.groupcollide(pl_bullets, bossv1, True, False)
                for hit in hits:
                    score += 1
                    expl = Explosion(boss.rect.center)
                    all_sprites.add(expl, layer=100)
                    all_sprites.move_to_front(expl)
                    boss.killed = True

                    if score == 5002:
                        score -= 1

        # CAT BOSS

        if boss.killed:
            if game_over == False and cat.killed == False:
                draw_boss_hp(screen, WIDTH - 600, 50, cat.hp)

            catsg.add(cat)
            all_sprites.add(cat)

            if cat.speedy == 0 and cat.killed == False:  # Ready to fire.

                if cat.speedx != 0:
                    hits = pygame.sprite.groupcollide(pl_bullets, catsg, True, False)
                    for hit in hits:
                        cat_dmg_sound.play()
                        score += 15
                        cat.hp -= 5
                        expl = Explosion(cat.rect.center)
                        all_sprites.add(expl, layer=100)
                        #all_sprites.move_to_front(expl)
                timer = random.uniform(0, 0.3)
                timer -= dt
                if timer <= 0:
                    boss_x = cat.rect.centerx
                    boss_y = cat.rect.bottom
                    bullet = BossBull(boss_x, boss_y, boss.speedy)
                    all_sprites.add(bullet)
                    e_bullets.add(bullet)

                    cat.start_shoot()

                    timer = random.uniform(0, 0.3)
                    cat.move()

        if cat.killed:
            player.rect.y += player.speedy
            player.speedy = -4
            if player.rect.top < 0:
                player.kill()
                victory = True

        if game_over == False and cat.killed == False:
            # Drawing score and player hp
            draw_text(screen, ("SCORE: " + (str(score))), 18, WIDTH / 2, 10)
            draw_hp(screen, WIDTH - 140, 5, player.hp, player_hp_img)
            # *after* drawing everything, flip the display
        pygame.display.flip()

    # END OF GAME FUNC

try:
    game_intro()
except:
    pygame.error
pygame.quit()


from bs4 import BeautifulSoup
from urllib import urlopen
import re

class Player:
    def __init__(self, row):
        self.stat_names = ['MIN','FGM','FGA','FTM','FTA','3PM','3PA','PTS','OFFR','DEFR','REB','AST','TO','STL','BLK']
        self.name = row.a.string.encode('utf-8')
        row_cells = row.find_all('td')
        stat_line = [float(cell.string) for cell in row_cells[1:15]]
        self.season_stats = dict(zip(self.stat_names, stat_line))
        self.game_stats = dict(zip(self.stat_names, [0.0] * 15))

class Team:
    def __init__(self, source_soup):
        team_stats_html = urlopen('http://espn.go.com'+source_soup.find(class_ = 'mod-page-tabs').ul.find_all('li')[2].a.get('href').encode('utf-8'))
        source_soup = BeautifulSoup(team_stats_html)
        team_stats_html.close()
        self.name = source_soup.h3.string.encode('utf-8')
        season_stats_table = source_soup.find_all('table')[1]
        rows = season_stats_table.find_all(class_ = re.compile(r'player'))
        self.roster = {}
        for row in rows:
            self.create_player(row)
    def create_player(self, row):
        self.roster[row.a.string.encode('utf-8')] = Player(row)

class Game:
    def __init__(self, source_url):
        source_html = urlopen(source_url)
        source_soup = BeautifulSoup(source_html.read())
        source_html.close()
        self.title = source_soup.title.string.encode('utf-8')
        home_team_html = urlopen(source_soup.find(class_ = 'team home').a.get('href'))
        home_team_soup = BeautifulSoup(home_team_html)
        home_team_html.close()
        self.home_team = Team(home_team_soup)
        away_team_html = urlopen(source_soup.find(class_ = 'team away').a.get('href').encode('utf-8'))
        away_team_soup = BeautifulSoup(away_team_html)
        away_team_html.close()
        self.away_team = Team(away_team_soup)
        self.players = {}
        self.players.update(self.home_team.roster)
        self.players.update(self.away_team.roster)

        def clean_pbp_rows(pbp_rows):
            pbp_rows.remove(pbp_rows[-1])
            first_half_rows = []
            second_half_rows = []
            while pbp_rows[-1].th == None:
                second_half_rows.append(pbp_rows.pop())
            pbp_rows = pbp_rows[:-3]
            second_half_rows = second_half_rows[::-1]
            while pbp_rows[-1].th == None:
                first_half_rows.append(pbp_rows.pop())
            first_half_rows = first_half_rows[::-1]

            def clean_pbp_cells(row, timeout_expression):
                cells = row.find_all('td')
                if timeout_expression.match(cells[1].string.encode('utf-8')) == None:
                    cells = cells[0:2]+[cells[3]]
                for cell in cells:
                    if (cell.string.encode('utf-8')) == '\xc2\xa0':
                        cells.remove(cell)
                cells[0] = cells[0].string.encode('utf-8')
                cells[1] = cells[1].string.encode('utf-8')
                row = [cells[0], cells[1]]
                return row

            timeout_expression = re.compile('.*Timeout')
            first_half_rows = [clean_pbp_cells(row, timeout_expression) for row in first_half_rows]
            second_half_rows = [clean_pbp_cells(row, timeout_expression) for row in second_half_rows]
            return (first_half_rows, second_half_rows)

        pbp_rows = source_soup.find(class_ = 'mod-data mod-pbp').find_all('tr')
        (self.first_half_rows, self.second_half_rows) = clean_pbp_rows(pbp_rows)

if __name__ == '__main__':
    game = Game(r'http://espn.go.com/ncb/playbyplay?gameId=400598837')
    print(game.title)
    print(game.home_team.name)
    print(game.home_team.roster.keys())
    print(game.away_team.name)
    print(game.away_team.roster.keys())
    for row in game.first_half_rows:
        print(row)
    for row in game.second_half_rows:
        print(row)


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

import click
import numpy as np
from scipy import stats

def ttest(gene_region,nongene_region):
    np.random.seed(12345678)
    p=stats.ttest_rel(gene_region, nongene_region)[1]
    gene_aver=np.mean(gene_region)
    nongene_aver=np.mean(nongene_region)
    aver=np.mean(gene_region)-np.mean(nongene_region)
    return p,gene_aver,nongene_aver,aver

@click.command()
@click.option('-i','--input')
@click.option('-o','--out')
#f1 file format
#CHR_CNV	Mis	Mis-ok	Mis-ok-list	Mis-no	Mis-no-list	Mis-ok/Mis	Norm	Norm-ok	Norm-ok-list	Norm-no	Norm-no-list	Norm-ok/Norm	Ambiguous  Ambiguous-list	Introgression/CNV	A1_gene	A1_nongene	A2_gene	A2_nongene	A3_gene	A3_nongene

def main(input,out):
    print("Runing!")
    np.random.seed(12345678)
    with open(input,'r') as f1:
        region_dict={}
        for line in f1:
            if line[0] == "#":
                headline = line.strip().split('\t')
            else:
                line=line.strip().split("\t")
                region_dict[line[0]] = line
    f2=open(out,'w')
    f2.write("chr_start_end\tgene/nongene\tpvalue\taverage\tcpnu_midain_List\n")
    for k,v in region_dict.items():
        gene_region=[]
        nongene_region=[]
        if v[5] != "":
            miss_sample=v[5]
            miss_sample=miss_sample.replace(',','\t')
            miss_sample=miss_sample.strip().split('\t')
            for i in range(16,len(headline)):
                if headline[i].split('_')[0] in miss_sample:
                    if i % 2 == 0:
                        gene_region.append(float(v[i]))
                    else:
                        nongene_region.append(float(v[i]))
            pvalue,gene_aver,nongene_aver,aver = ttest(gene_region,nongene_region)
            gene_region = [str(i) for i in gene_region]
            nongene_region = [str(i) for i in nongene_region]
            f2.write('{}\t{}\t{}\t{}\t{}\t{}\t{}\n'.format(k,str(pvalue), str(gene_aver), str(nongene_aver),str(aver) ,','.join(gene_region),','.join(nongene_region)))
        else:
            f2.write(k+'\t\t\t\t\t\t\n')
    print("Done.")

if __name__ == '__main__':
    main()


import time
import allure
from allure_commons.types import AttachmentType
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from conduit_methods import *

class TestConduitApp(object):

    def setup(self):
        browser_options = Options()
        browser_options.headless = True
        self.browser = webdriver.Chrome(ChromeDriverManager().install(), options=browser_options)
        self.browser.get("http://localhost:1667/")

    def teardown(self):
        self.browser.quit()

# TC10 Delete blogpost (precondition: sign in, create comment)

    def test_delete_comment(self):
        conduit_login(self.browser)
        time.sleep(4)
        user_name = self.browser.find_element_by_xpath('//a[@class="nav-link"][contains(.,"Próba Pista")]').text
        create_comment(self.browser)
        time.sleep(2)
        allure.attach(self.browser.get_screenshot_as_png(), name="Created_comment", attachment_type=AttachmentType.PNG)

        comment = self.browser.find_element_by_xpath(f'//div[@class="card-footer"][contains(.,"{user_name}")]//parent::div[@class="card"]//p[@class="card-text"]')
        assert comment.text == 'Test comment.'
        delete_comment_btn = self.browser.find_element_by_xpath('//i[@class="ion-trash-a"]')
        delete_comment_btn.click()
        time.sleep(2)
        comment_block = self.browser.find_elements_by_xpath('//div[@class="card-block"]/p')
        assert len(comment_block) == 0


from Controller.Main import *
from sys import *

def menu():

    return print('''
        /////////  MENU  /////////
        1 - Para listar Rotas
        2 - Para add nova rota
        3 - Para consultar rota
        0 - Para sair
        //////////////////////////
        ''')

def printRotas(rotas):

    for rota in rotas:
        print('Origem: {}'.format(rota.origem.id))
        print('Destino: {}'.format(rota.destino.id))
        print('Custo: {}'.format(rota.peso))
        print('\n')

def adicionarRota(grafo):
    print('Informe a nova rota no formato: Origem,Destino,Custo')
    origem, destino, custo = input().split(',')
    grafo.atualizaGrafo(origem, destino, custo)
    if grafo.arquivo.writeArquivo(origem, destino, custo):
        print('Rota adiconada com sucesso')
    else:
        print('Ocorreu algum erro ao atualiza o arquivo')

def obterViagem(grafo):
    print('Informe qual a viagem no formato: Origem-Destino: ')
    origem, destino = input().split('-')
    origem = Vertice(origem)
    destino = Vertice(destino)

    rota, custo = grafo.obterRotaDijkstra(origem.getId(), destino.getId())

    print('Rota: {} \n'. format(rota))
    print('Custo: {} \n'. format(custo))

if __name__ == '__main__':

    args = []

    for param in sys.argv:
        args.append(param)
    # nome_ficheiro = args[1]
    nome_ficheiro = 'input-fileCp.csv'
    controller_api = Main(nome_ficheiro)
    controller_api.tratar_dados_de_entrada()
    controller_api.monta_grafo()
    option = 999
    while option != 0:
        menu()
        option = int(input('Informe qual operação deseja realizar: '))

        if option == 1:
            printRotas(controller_api.grafo.lista_Arestas)
        elif option == 2:
            adicionarRota(controller_api)
        elif option == 3:
            obterViagem(controller_api)
        elif option == 0:
            print('Bye')
        else :
            print('Opção Invalida')


from typing import Any, Callable, Dict, List, Literal, Set, Type, _GenericAlias

from cattr import Converter

def make_literal_structure_fn(
    converter: Converter, *classes: Type,
) -> Callable[[Dict, Type], Any]:
    """
    Given a sequence of attrs classes, each of which has a different Literal
    field, generate a union structuring function.
    """
    # First, find the field with the literal.
    literal_fields: List[Set[str]] = []

    for cls in classes:
        lits = set()
        for attr in cls.__attrs_attrs__:
            t = attr.type
            if isinstance(t, _GenericAlias) and t.__origin__ is Literal:
                lits.add(attr.name)
        literal_fields.append(lits)
    field = next(iter(set.intersection(*literal_fields)))

    cls_mapping = {
        getattr(cls.__attrs_attrs__, field).type.__args__[0]: cls
        for cls in classes
    }

    def union_literal_hook(obj: Dict, _: Type):
        val = obj[field]
        try:
            cls = cls_mapping[val]
        except KeyError:
            raise Exception(f"No class matches literal {val}.")
        return converter.structure(obj, cls)

    return union_literal_hook


# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import print_function
import six
import shlex
import fuzzyfinder
from prompt_toolkit.completion import Completion

class TextUtils(object):
    """Utilities for parsing and matching text.

    Attributes:
        * None.
    """

    def shlex_split(self, text):
        """Wrapper for shlex, because it does not seem to handle unicode in 2.6.

        Args:
            * text: A string to split.

        Returns:
            A list that contains words for each split element of text.
        """
        if six.PY2:
            text = text.encode('utf-8')
        return shlex.split(text)

    def find_collection_matches(self, word, collection, fuzzy):
        """Yields all matching names in list.

        Args:
            * word: A string representing the word before
                the cursor.
            * collection: A collection of words to match.
            * fuzzy: A boolean that specifies whether to use fuzzy matching.

        Yields:
            A generator of prompt_toolkit's Completions.
        """
        if fuzzy:
            for suggestion in fuzzyfinder.fuzzyfinder(word, collection):
                yield Completion(suggestion, -len(word))
        else:
            for name in sorted(collection):
                if name.startswith(word) or not word:
                    yield Completion(name, -len(word))

    def find_matches(self, word, collection, fuzzy):
        """Finds all matches in collection for word.

        Args:
            * word: A string representing the word before
                the cursor.
            * collection: A collection of words to match.
            * fuzzy: A boolean that specifies whether to use fuzzy matching.

        Yields:
            A generator of prompt_toolkit's Completions.
        """
        word = self.last_token(word).lower()
        for suggestion in self.find_collection_matches(
                word, collection, fuzzy):
            yield suggestion

    def get_tokens(self, text):
        """Parses out all tokens.

        Args:
            * text: A string to split into tokens.

        Returns:
            A list of strings for each word in the text.
        """
        if text is not None:
            text = text.strip()
            words = self.safe_split(text)
            return words
        return []

    def last_token(self, text):
        """Finds the last word in text.

        Args:
            * text: A string to parse and obtain the last word.

        Returns:
            A string representing the last word in the text.
        """
        if text is not None:
            text = text.strip()
            if len(text) > 0:
                word = self.safe_split(text)[-1]
                word = word.strip()
                return word
        return ''

    def safe_split(self, text):
        """Safely splits the input text.

        Shlex can't always split. For example, "\" crashes the completer.

        Args:
            * text: A string to split.

        Returns:
            A list that contains words for each split element of text.

        """
        try:
            words = self.shlex_split(text)
            return words
        except:
            return text


import manga109api
import argparse
import os
import glob
from PIL import Image

def args_parser():
    """
    :return: This function returns the manual input of book, annotation_type, and page count.
    """
    parser = argparse.ArgumentParser()
    parser.add_argument('--book', type=str, help='Name of book to annotate from.')
    parser.add_argument('--annotation', type=str, help='Type of annotation: "body", "face", "frame", "text".')
    parser.add_argument('--pages', type=int, default=1, help='Number of pages to annotate.')
    parser.add_argument('--preprocess', action='store_true', help='Preprocess the extracted images to have a uniform size.')
    parser.add_argument('--size', type=int, default=128, help='The uniform size if using preprocessing.')
    args = parser.parse_args()
    return args

if __name__ == "__main__":
    ap = args_parser()
    manga109_root_dir = "manga109extracted"
    if not os.path.exists(manga109_root_dir):
        os.makedirs(manga109_root_dir)
    book = ap.book
    page_count = ap.pages
    file_count = [glob.glob(os.path.join(manga109_root_dir, '**', '*.*'), recursive=True)]
    count = len(file_count[0])

    for page_index in range(page_count):
        tracker = 0
        p = manga109api.Parser(root_dir="Manga109s_data")
        annotation = p.get_annotation(book=book)
        img = Image.open(p.img_path(book=book, index=page_index))
        for annotation_type in [ap.annotation]:
            rois = annotation["page"][page_index][annotation_type]
            for roi in rois:
                cropped = img.crop((roi["@xmin"], roi["@ymin"], roi["@xmax"], roi["@ymax"]))
                image_x_dim, image_y_dim = cropped.size
                if ap.preprocess:
                    cropped = cropped.resize((ap.size, ap.size), Image.ANTIALIAS)
                if image_x_dim >= (ap.size / 2) and image_y_dim >= (ap.size / 2):
                    cropped.save("manga109extracted/%s_%d.jpg" % (ap.book, count))
                    count += 1
                    tracker += 1
        print("Extracted %d %s images from page %d of %s's book." % (tracker, ap.annotation, page_index + 1, ap.book))


from talon import Context, Module, actions, app, fs
import pathlib
import logging
import pprint

mod = Module()
mod.tag("ssh", desc="tag for ssh functionality")
mod.list("ssh_hosts", desc="List for storing speakable ssh hosts")

ctx = Context()
ctx.matches = r"""
tag: user.ssh
"""
ctx.lists["user.ssh_hosts"] = {}

def on_ready():
    """Trigger one list update soon is talon is ready"""
    ssh_config_change(None, None)

def ssh_config_change(path, flags):
    """Update the ssh host list based off of a change of ssh config"""
    host_list = []
    global ssh_configs
    for file in ssh_configs:
        with open(file) as f:
            for line in f:
                if line.startswith("Host "):
                    host_list.extend(line.split()[1:])
    ctx.lists["user.ssh_hosts"] = actions.user.create_spoken_forms_from_list(host_list)

ssh_configs = []
ssh_config = pathlib.Path.home() / ".ssh/config"
if ssh_config.exists():
    ssh_configs.append(ssh_config)
    fs.watch(ssh_config, ssh_config_change)
ssh_config_dir = pathlib.Path.home() / ".ssh/config.d"
if ssh_config_dir.exists():
    ssh_configs.extend(ssh_config_dir.glob("*"))
    for path in ssh_config_dir.glob("*"):
        fs.watch(path, ssh_config_change)
# print(f"SSH Configs: {ssh_configs}")
app.register("ready", on_ready)

@mod.action_class
class Actions:
    def ssh_dump_hosts_list():
        """Dump add a pretty version of the file completions to the log"""
        logging.info("SSH Host Completions:")
        logging.info(pprint.pformat(ctx.lists["user.ssh_hosts"]))


from aBuild import msg
from aBuild.utility import chdir, _get_reporoot

class dataset:

    def __init__(self,dset,systemSpecies,root=None,calculator = None,lFormat = 'mtpselect'):
        from os import path,makedirs
        from aBuild.database.crystal import Crystal
        from aBuild.calculators.vasp import VASP

        if isinstance(dset,list):

            if isinstance(dset[0], dict):
                self.init_enum(dset,systemSpecies)
            elif isinstance(dset[0], Crystal):
                self.crystals = dset
                self.nCrystals = len(dset)
            elif isinstance(dset[0], VASP):
                self.calcs = dset
                self.nCalcs = len(dset)
            elif isinstance(dset[0],str):
                self.init_paths(dset,systemSpecies)
        elif isinstance(dset, str):
           self.init_file(dset,systemSpecies,lFormat)

        self.species = systemSpecies
        self.root = root
        self.calculator = calculator

    # Used to be called 'buildFoldersFromEnum
    def init_enum(self,enumdicts,systemSpecies,runGetKpoints = True):
        from aBuild.enumeration import Enumerate
        from aBuild.calculators.vasp import VASP
        from aBuild.database.crystal import Crystal
        from aBuild.jobs import Job
        from random import randrange
        from aBuild.utility import chdir

        #        from crystal import Crystal
        from os import path
        import os

        #    if not path.isdir(self.root):
        #    os.mkdir(self.root)
        print("Building database from enumerations")
        self.crystals = []
        #        configIndex = startPoint = self._starting_point
        for eDict in enumdicts:
            enumController = Enumerate(eDict)
            if enumController.nEnumStructs == 0:
                msg.warn('There are no enumerated structures for lattice type {}.  Not building any VASP folders for them.'.format(self.enumDicts[index]["lattice"]))
                enumController.buildInputFile()

                enumController.enumerate()

            # Loop to generate random structures for a given lattice type
            for i in range(eDict["nconfigs"]):
                rStruct = randrange(1,enumController.nEnumStructs)
                print('Adding {} structure # {} to database'.format(eDict["lattice"],rStruct) )
                with open('structNums','a+') as f:
                    f.write(eDict["lattice"] + ' ' + str(rStruct) + '\n')
                    #print("Building VASP folder for {} structure #: {}".format(eDict["lattice"],rStruct))
                enumController.generatePOSCAR(rStruct)

                poscarpath = path.join(enumController.root,"poscar.{}.{}".format(eDict["lattice"],rStruct))
                thisCrystal = Crystal(poscarpath, systemSpecies = systemSpecies) #title = ' '.join([self.enumDicts[index]["lattice"]," str #: {}"]).format(rStruct)
                self.crystals.append(thisCrystal)

    # Sometimes an entire dataset is stored in one file.  I'd like to extract each crystal from the file to
    # create a list of crystal objects
    def init_file(self,datafile,systemSpecies,linesformat):
        from aBuild.database.crystal import Crystal
        possibleFiles = {'new_training.cfg':'mlpadd','train.cfg': 'mlptrain','structures.in':'ce'}
        #selectedFile = path.join(self.root,'new_training.cfg')

        with open(datafile,'r') as f:
            lines = f.readlines()

        self.crystals = []
        nCrystals = 0
        for index,line in enumerate(lines):

            if line == 'BEGIN_CFG\n':
                #                nCrystals += 1
                #if numOfStructs is not 'all' and (nCrystals < start or nCrystals > start + numOfStructs):
                #    continue
                nAtoms = int(lines[index+2].split()[0])
                structlines = lines[index:index + 11 + nAtoms]
                thisCrystal = Crystal(structlines,systemSpecies,lFormat = linesformat)
                self.crystals.append(thisCrystal)

    def init_paths(self,paths,systemSpecies):
        from aBuild.database.crystal import Crystal
        from aBuild.calculators.vasp import VASP
        self.crystals = []
        for dirpath in paths:
            calc = VASP(dirpath,systemSpecies)
            calc.read_results()
            if calc.crystal.results is not None:
                self.crystals.append(calc.crystal)

    def starting_point(self,folderpath):
        from os import path
        from glob import glob
        from aBuild.utility import chdir

        with chdir(folderpath):
            dirs = glob('E.*')
        prevCalcs = [int(x.split('.')[1])  for x in dirs]
        prevCalcs.sort()
        if prevCalcs != []:
            return prevCalcs[-1] + 1
        else:
            return 1

        #    def write(self):

    def buildFolders(self,buildpath,calculator,runGetKpoints = True,foldername = 'E'):
        from os import path
        from aBuild.calculators.vasp import VASP
        from aBuild.jobs import Job

        import os
        print("Building folders in {}".format(buildpath))
        if not path.isdir(buildpath):
                os.mkdir(buildpath)
                print('Made path:',buildpath)
        configIndex = startPoint = self.starting_point(buildpath)
        for crystal in self.crystals:
            vaspspecs = {"incar":calculator["incar"],"kpoints":calculator["kpoints"], 'potcar':calculator["potcars"],"crystal":crystal}
            thisVASP = VASP(vaspspecs,self.species)

            runpath = path.join(buildpath,foldername + ".{}".format(configIndex) )
            if not path.isdir(runpath):
                os.mkdir(runpath)
            else:
                msg.fatal("I'm gonna write over top of a current directory. ({})  I think I'll stop instead.".format(runpath))
            print("Building folder for structure: {}".format(crystal.title) )
            with chdir(runpath):
                thisVASP.buildFolder(runGetKPoints = runGetKpoints)
            configIndex += 1

        exdir = path.join(buildpath,'E.')
        mljob = Job(calculator["execution"],exdir,calculator["execution"]["exec_path"], arrayStart = startPoint,arrayEnd = configIndex - 1)
        with chdir(buildpath):
            print('Building job file')
            mljob.write_jobfile()

    def build_relax_select_input(self):
        from os import remove,path
        from aBuild.enumeration import Enumerate
        from aBuild.database.crystal import Crystal
        from aBuild.fitting.mtp import MTP
        from aBuild.utility import unpackProtos,getAllPerms
        from glob import glob
        fittingRoot = path.join(self.root,'fitting','mtp')

        for ilat  in range(self.nEnums):
            lat = self.enumDicts[ilat]["lattice"]
            enumLattice = Enumerate(self.enumDicts[ilat])

            if lat == 'protos':
                structures = getProtoPaths()
                for struct in structures:
                    scrambleOrder = getAllPerms(self.knary,justCyclic = 'uniqueUnaries' in struct)
                    for scramble in scrambleOrder:
                        thisCrystal = Crystal(struct,species = self.species)
                        thisCrystal.scrambleAtoms(scramble)
                        thisMTP = MTP(fittingRoot,dataSet = [thisCrystal],forRelax=True)
                        with open(path.join(fittingRoot,'to-relax.cfg'),'a+') as f:
                            f.writelines(thisMTP.lines)

            else:
                for struct in range(1,enumLattice.nEnumStructs+1):
                    enumLattice.generatePOSCAR(struct)
                    thisCrystal = Crystal.fromPOSCAR(enumLattice.root, self.species,
                                                     filename = "poscar.{}.{}".format(lat,struct),
                                                     title = ' '.join([lat," str #: {}"]).format(struct))
                    thisMTP = MTP(fittingRoot,dataSet = [thisCrystal],forRelax=True)
                    with open(path.join(fittingRoot,'to-relax.cfg'),'a+') as f:
                        f.writelines(thisMTP.lines)

                    delpath = path.join(enumLattice.root,"poscar.{}.{}".format(lat,struct))
                    remove(delpath)

        thisMTP.write_relaxin()


class Solution:
    def majorityElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        length = len(nums)
        majnum = 0  #众数个数
        num = 0     #众数
        l = {}
        for i in range(length):
            if nums[i] in l:
                l[nums[i]] += 1
            else:
                l[nums[i]] = 1
        # 用于求众数个数
        # for j in l:
        #     majnum = max(majnum,l[j])
        # return majnum
        for j in l:
            if l[j] > majnum:
                majnum = l[j]
                num = j
        return num
#2018.12.07
#-------------------main function-------------------
if __name__ == '__main__':
    # l = [3,2,3]
    # l = [2,2,1,1,1,2,2]
    l = [1,2,3,4,5,6,5]
    s = Solution()
    print(s.majorityElement(l))


"""
Type annotations for outposts service literal definitions.

[Open documentation](https://vemel.github.io/boto3_stubs_docs/mypy_boto3_outposts/literals.html)

Usage::

    ```python
    from mypy_boto3_outposts.literals import AddressTypeType

    data: AddressTypeType = "OPERATING_ADDRESS"
    ```
"""
import sys

if sys.version_info >= (3, 8):
    from typing import Literal
else:
    from typing_extensions import Literal

__all__ = (
    "AddressTypeType",
    "AssetStateType",
    "AssetTypeType",
    "CatalogItemClassType",
    "CatalogItemStatusType",
    "ComputeAssetStateType",
    "FiberOpticCableTypeType",
    "LineItemStatusType",
    "MaximumSupportedWeightLbsType",
    "OpticalStandardType",
    "OrderStatusType",
    "OrderTypeType",
    "PaymentOptionType",
    "PaymentTermType",
    "PowerConnectorType",
    "PowerDrawKvaType",
    "PowerFeedDropType",
    "PowerPhaseType",
    "ShipmentCarrierType",
    "SupportedHardwareTypeType",
    "SupportedStorageEnumType",
    "UplinkCountType",
    "UplinkGbpsType",
)

AddressTypeType = Literal["OPERATING_ADDRESS", "SHIPPING_ADDRESS"]
AssetStateType = Literal["ACTIVE", "RETIRING"]
AssetTypeType = Literal["COMPUTE"]
CatalogItemClassType = Literal["RACK", "SERVER"]
CatalogItemStatusType = Literal["AVAILABLE", "DISCONTINUED"]
ComputeAssetStateType = Literal["ACTIVE", "ISOLATED", "RETIRING"]
FiberOpticCableTypeType = Literal["MULTI_MODE", "SINGLE_MODE"]
LineItemStatusType = Literal[
    "BUILDING", "CANCELLED", "DELIVERED", "ERROR", "INSTALLED", "INSTALLING", "PREPARING", "SHIPPED"
]
MaximumSupportedWeightLbsType = Literal[
    "MAX_1400_LBS", "MAX_1600_LBS", "MAX_1800_LBS", "MAX_2000_LBS", "NO_LIMIT"
]
OpticalStandardType = Literal[
    "OPTIC_1000BASE_LX",
    "OPTIC_1000BASE_SX",
    "OPTIC_100GBASE_CWDM4",
    "OPTIC_100GBASE_LR4",
    "OPTIC_100GBASE_SR4",
    "OPTIC_100G_PSM4_MSA",
    "OPTIC_10GBASE_IR",
    "OPTIC_10GBASE_LR",
    "OPTIC_10GBASE_SR",
    "OPTIC_40GBASE_ESR",
    "OPTIC_40GBASE_IR4_LR4L",
    "OPTIC_40GBASE_LR4",
    "OPTIC_40GBASE_SR",
]
OrderStatusType = Literal[
    "CANCELLED",
    "COMPLETED",
    "ERROR",
    "FULFILLED",
    "INSTALLING",
    "IN_PROGRESS",
    "PENDING",
    "PREPARING",
    "PROCESSING",
    "RECEIVED",
]
OrderTypeType = Literal["OUTPOST", "REPLACEMENT"]
PaymentOptionType = Literal["ALL_UPFRONT", "NO_UPFRONT", "PARTIAL_UPFRONT"]
PaymentTermType = Literal["ONE_YEAR", "THREE_YEARS"]
PowerConnectorType = Literal["AH530P7W", "AH532P6W", "IEC309", "L6_30P"]
PowerDrawKvaType = Literal["POWER_10_KVA", "POWER_15_KVA", "POWER_5_KVA"]
PowerFeedDropType = Literal["ABOVE_RACK", "BELOW_RACK"]
PowerPhaseType = Literal["SINGLE_PHASE", "THREE_PHASE"]
ShipmentCarrierType = Literal["DBS", "DHL", "FEDEX", "UPS"]
SupportedHardwareTypeType = Literal["RACK", "SERVER"]
SupportedStorageEnumType = Literal["EBS", "S3"]
UplinkCountType = Literal[
    "UPLINK_COUNT_1",
    "UPLINK_COUNT_12",
    "UPLINK_COUNT_16",
    "UPLINK_COUNT_2",
    "UPLINK_COUNT_3",
    "UPLINK_COUNT_4",
    "UPLINK_COUNT_5",
    "UPLINK_COUNT_6",
    "UPLINK_COUNT_7",
    "UPLINK_COUNT_8",
]
UplinkGbpsType = Literal["UPLINK_100G", "UPLINK_10G", "UPLINK_1G", "UPLINK_40G"]