import os
import pickle
import shutil
import numpy as np
from tqdm import tqdm
import time

class ModelManager:
    '''
    Model manager is designed to load and save all models
    No matter what dataset name.
    '''
    path_name = './checkpoints/'
    @classmethod
    def __init__(cls, cfg):
        if not cfg.MODEL.TRAINING and cfg.PATH.MODEL_PATH is not None:
            cls.path_name = cfg.PATH.MODEL_PATH
        elif cfg.MODEL.TRAINING and cfg.MODEL.MODEL_NAME:
            cls.path_name += cfg.MODEL.MODEL_NAME+"-"+ time.strftime("%Y_%m_%d__%H_%M_%S", time.localtime()) +'/'
            cfg.PATH.MODEL_PATH = cls.path_name
        else:
            raise Exception('Model path initialization error, please check your config.py')
    def save_model(self, model, model_name):
        '''
        Save model to model/ dir
        :param model: model to be saved
        :param model_name: model name
        :return: None
        '''
        if 'pkl' not in model_name:
            model_name += '.pkl'
        if not os.path.exists('checkpoints'):
            os.makedirs('checkpoints')
        if not os.path.exists( self.path_name):
            os.makedirs(self.path_name)
        pickle.dump(model,open(self.path_name+model_name,'wb'))

    def save_config(self,cfg):
        '''
        Save config to model/ dir as yaml file
        :param cfg: config
        :return: None
        '''
        if not os.path.exists(self.path_name):
            os.makedirs(self.path_name)
        cfg.PATH.CONFIG_PATH = self.path_name+'config.yaml'
        with open(self.path_name+'config.yaml','w') as f:
            f.write(cfg.dump())

    def load_model(self, model_name):
        '''
        load model from model/ dir
        :param model_name: model name
        :return: model
        '''

        if 'pkl' not in model_name:
            model_name += '.pkl'
        if not os.path.exists(self.path_name+model_name):
            raise Exception('Model not found %s'%(self.path_name+model_name))
        return pickle.load(open(self.path_name+model_name,'rb'))

    def save_test_result(self,test_result):
        '''
        Save test result to model/ dir
        :param test_result: test result, as txt file
        :return: None
        '''
        if not os.path.exists(self.path_name):
            os.makedirs(self.path_name)
        with open(self.path_name+'test_result.txt','w') as f:
            for item in test_result:
                f.write(str(item)+'\n')

    @staticmethod
    def clean_workspace():
        '''
        clean model/ dir
        :return: None
        '''
        if os.path.exists('checkpoints'):
            shutil.rmtree('checkpoints')

def get_time_cost(begin_time, end_time):
    '''
    get the time cost
    :param begin_time: the start time
    :param end_time: the end time
    :return: the time cost
    '''
    time_cost = end_time - begin_time
    return "%d day %d hour %d minute %.2f second"%(time_cost // 86400, time_cost % 86400 // 3600, time_cost % 3600 // 60, time_cost % 60)

def k_neighbors(sim_vector, k):
    '''
    input the  similarity matrix, the index of the user, and the k
    return the k nearest neighbor of the user
    :param sim_vector: the similarity matrix
    :param k: the k
    :return: the k nearest neighbor of the user and the similarity between the user and the neighbor
    '''
    # get the similarity matrix
    sim_vector = sim_vector
    # get the k
    k = k
    # get the k nearest neighbor of the user
    neighbor = np.argsort(sim_vector)[-k-1:-1]
    neighbor_sim = np.sort(sim_vector)[-k-1:-1] # do not include the user itself
    return neighbor, neighbor_sim

def get_score_matrix(train_rating,user_map,movie_map):
    '''
        get the score  matrix
        @param: train_rating, the train rating
        @param: user_map, the user map
        @param: movie_map, the movie map
        @return: score_matrix, the movie popularity, the movie count
    '''
    print("<<<< begin to conduct the score matrix")
    score_matrix = np.zeros((len(user_map.keys()),len(movie_map.keys())))
    movie_popular = np.zeros(len(movie_map.keys()))
    movie_count = len(movie_map.keys())
    tqdm_process = tqdm(total=train_rating.shape[0])
    for row in train_rating.itertuples(index=True,name="Pandas"):
        user = user_map[getattr(row,'userId')]
        movie = movie_map[getattr(row,'movieId')]
        rate = getattr(row,'rating')
        score_matrix[user][movie] = rate
        movie_popular[movie] += 1
        tqdm_process.update(1)
    tqdm_process.close()
    print(">>>> end to conduct the score matrix")
    print("@ score matrix shape:",score_matrix.shape)
    print('movie_popular shape:',movie_popular.shape)
    print('movie_count:',movie_count)
    return score_matrix, movie_popular, movie_count

def calculate_movie_similarity(train_set,pre_sim_calcul = False):
    '''
    calculate the tfidf of the movies
    :param train_set: the train set, a tuple of (trainset,user_map,movie_map,movie_type_features)
    :return: score_matrix, movie_popular, movie_sim, movie_count
    '''
    # get the train set
    train_rating, user_map, movie_map, movie_type_features = train_set

    score_matrix, movie_popular, movie_count = get_score_matrix(train_rating,user_map,movie_map)

    movie_sim= np.zeros((movie_count, movie_count))

    if pre_sim_calcul:
        print("<<<< begin to conduct the movie similarity matrix")
        begin_time = time.time()  # record the start time
        for i in tqdm(range(movie_count)):
            movie_sim[i][i] = 1
            for j in range(i+1,movie_count):
                movie_sim[i][j] = cosine_similarity(movie_type_features[i],movie_type_features[j])
                movie_sim[j][i] = movie_sim[i][j]
        end_time = time.time()  # record the end time

        print(">>>> end to conduct the movie similarity matrix")
        print("@ time cost: %s"%get_time_cost(begin_time,end_time))
    else:
        print("post calculate  the similarity during prediction!")
    return score_matrix, movie_popular, movie_sim, movie_count,user_map,movie_map,movie_type_features

def cosine_similarity(list1,list2):
    '''
    calculate the cosine_similarity of list1 and list2

    :param list1: the first list
    :param list2: the second list
    :return: the cosine_similarity
    '''
    # get the number of common items
    assert(len(list1) == len(list2))
    n = len(list1)
    assert(n > 0)
    # calculate the sum of the two lists
    sum1 = sum(list1*list2)
    # calculate the square of the two lists
    den = np.sqrt(sum(list1**2)) * np.sqrt(sum(list2**2))
    # calculate the cosine similarity
    if den == 0:
        return 0
    else:
        return sum1/den

def calculate_user_sim_matrix(train_set,pre_sim_calcul = True):
    '''
    calculate the similarity matrix between users
    :param train_set: the train set, a tuple of (trainset,user_map,movie_map,movie_type_features)
        """
    :return: the score_matrix, the similarity matrix, movie_popular, movie_count
    '''
    # conduct the score matrix
    print("<<<<<< begin to caculate the similarity matrix, the movie popularity and the movie count")

    train_rating, user_map, movie_map, movie_type_features  = train_set

    score_matrix, movie_popular, movie_count = get_score_matrix(train_rating,user_map,movie_map)

    # get the similarity matrix between users
    user_sim_matrix = np.zeros((score_matrix.shape[0],score_matrix.shape[0]))
    if pre_sim_calcul:
        user_sim_matrix = get_user_sim_matrix(score_matrix)
    else:
        print("post calculate  the similarity during prediction!")
    print(">>>> end to caculate the similarity matrix.")
    print('user_sim_matrix shape:',user_sim_matrix.shape)
    return score_matrix,user_sim_matrix, movie_popular, movie_count,user_map,movie_map,movie_type_features

def get_user_sim_matrix(input_matrix):
    '''
    get the similarity matrix between users with pearson similarity
    :param input_matrix: the input matrix with shape (n_users, n_items)
    :return: the similarity matrix
    '''
    # get the shape of the input matrix
    begin_time = time.time()  # record the start time
    print("<<<< begin to get the similarity matrix")
    input_matrix = np.array(input_matrix) # convert to numpy array
    print('input score matrix shape:',input_matrix.shape)
    # get the number of users
    n_users = input_matrix.shape[0]
    # calculate the similarity matrix between users with person similarity
    user_sim_matrix = np.zeros((n_users, n_users))
    print('user_sim_matrix shape:',user_sim_matrix.shape)

    for i in tqdm(range(n_users)):
        user_sim_matrix[i][i] = 1
        for j in range(i+1,n_users):
            user_sim_matrix[i][j] = pearson_sim(input_matrix[i],input_matrix[j])
            user_sim_matrix[j][i] = user_sim_matrix[i][j]
    print(">>>> end to get the similarity matrix")
    end_time = time.time()  # record the end time
    print('@ time cost: '+get_time_cost(begin_time, end_time))
    return user_sim_matrix

def pearson_sim(list1,list2):
    '''
    calculate the pearson similarity between two lists
    :param list1: the first list
    :param list2: the second list
    :return: the pearson similarity
    '''
    # get the number of common items
    assert len(list1) == len(list2)
    n = len(list1)
    assert n > 0
    # calculate the sum of the two lists
    avg1 = sum(list1)/n
    avg2 = sum(list2)/n
    norm1 = list1 - avg1
    norm2 = list2 - avg2
    # calculate the sum of the two lists
    sum1 = sum(norm1*norm2)
    # calculate the square of the two lists
    den = np.sqrt(sum(norm1**2)) * np.sqrt(sum(norm2**2))
    # calculate the pearson similarity
    if den == 0:
        return 0.0
    else:
        return sum1/den

def SSE_error(prediction,real_rating):
    '''
    calculate the SSE error
    :param prediction: the prediction of the user
    :param real_rating: the real rating of the user
    :return: the SSE error
    '''
    # get the prediction and the real rating
    prediction = np.array(prediction)
    real_rating = np.array(real_rating)
    # calculate the SSE error
    SSE = sum((prediction - real_rating)**2)
    return SSE

if __name__ == '__main__':
    # test the similarity matrix
    from dataset import Dataset
    from config import cfg
    dataset = Dataset(cfg)
    train_set = dataset.get_trainset()

    a = pearson_sim(np.array([1,2,3,4,5]),np.array([1,2,3,4,5]))
    b = pearson_sim(np.array([1,2,3,4,5]),np.array([5,4,3,2,1]))
    print(a,b)
    score_matrix,user_sim_matrix, movie_popular, movie_count,user_map,movie_map = calculate_user_sim_matrix(train_set,pre_sim_calcul = False)
    pickle.dump(user_sim_matrix,open('user_map.pkl','wb'))
    pickle.dump(movie_map,open('movie_map.pkl','wb'))
    # print(user_sim_matrix, movie_popular, movie_count)

    # pickle.dump(train_set, open('./checkpoints\CF-2022_04_11__11_32_40/trainset.pkl', 'wb'))
    # pickle.dump(score_matrix,open('checkpoints\CF-2022_04_11__11_32_40\score_matrix.pkl','wb'))

    # test the model_manager
    # model_manager = ModelManager(cfg)
    # model_manager.clean_workspace()
    # model_manager.save_model(user_sim_matrix, 'user_sim_matrix')
    # model_manager.save_model(movie_popular, 'movie_popular')
    # model_manager.save_model(movie_count, 'movie_count')
    # d = model_manager.load_model('score_matrix')
    # a = model_manager.load_model('user_sim_mat')
    # b = model_manager.load_model('movie_popular')
    # c = model_manager.load_model('movie_count')
    # print(a[0:3],b,c,d[0:3])

    # test the time cost
    # begin_time = time.time()  # record the start time
    # time.sleep(3)
    # end_time = time.time()  # record the end time

    # # print the time cost
    # print('@ time cost:',get_time_cost(begin_time,end_time))


import os
import logging
from pathlib import Path

from llama_index import (
    GPTSimpleVectorIndex,
    GPTSimpleKeywordTableIndex,
    SimpleDirectoryReader
)
from llama_index.indices.composability import ComposableGraph

# Initialise Logger
logging.basicConfig(level=logging.INFO, format="[{asctime}] - {funcName} - {message}", style='{')
logger = logging.getLogger("BUILD_INDEX")

openai_api_key = os.environ.get('OPENAI_API_KEY')

# Load Documents
cv_root_directory = Path()/'data'

for directory_index in range(1,4):
    document = SimpleDirectoryReader(cv_root_directory/f'cv{directory_index}').load_data()
    index = GPTSimpleVectorIndex.from_documents(document)

    index_file = Path()/'data'/f'cv_{directory_index}_index.json'
    # save index to disk
    index.save_to_disk(index_file)

# Select one index to prove need for composability
# load index from disk
index = GPTSimpleVectorIndex.load_from_disk(cv_root_directory/'cv_1_index.json')
# Query index
spock_address = index.query("Where does Spock Sarek Live ?")
logger.info(spock_address)
uhura_address = index.query("Where does Uhura Live ?")
logger.info(uhura_address)

# Compose indices for query
# Generate indices from files
index_cv_1 = GPTSimpleVectorIndex.load_from_disk(cv_root_directory/'cv_1_index.json')
index_cv_2 = GPTSimpleVectorIndex.load_from_disk(cv_root_directory/'cv_2_index.json')
index_cv_3 = GPTSimpleVectorIndex.load_from_disk(cv_root_directory/'cv_3_index.json')

# Write up summaries
cv_1_summary="Curriculum Vitae of Nyota Uhura"
cv_2_summary="Curriculum Vitae of Spock Sarek"
cv_3_summary="Curriculum Vitae of James T. Kirk"

# set query config
query_configs = [
    {
        "index_struct_type": "simple_dict",
        "query_mode": "default",
        "query_kwargs": {
            "similarity_top_k": 1
        }
    },
    {
        "index_struct_type": "keyword_table",
        "query_mode": "simple",
        "query_kwargs": {}
    },
]

index_all_cvs = ComposableGraph.from_indices(
    GPTSimpleKeywordTableIndex,
    [index_cv_1, index_cv_2, index_cv_3],
    index_summaries=[cv_1_summary, cv_2_summary, cv_3_summary],
    max_keywords_per_chunk=50
)

# Query again across indices
spock_address = index_all_cvs.query("Where does Spock Sarek Live ?")
uhura_actress = index_all_cvs.query("Who played Nyota Uhura ?")
kirk_players = index_all_cvs.query("Where has James Kirk been portrayed ?")
logger.info(spock_address)
logger.info(uhura_actress)
logger.info(kirk_players)


from osv import fields, osv

class account_journal_simulation(osv.osv):
    _name = "account.journal.simulation"
    _description = "Simulation level"
    _columns = {
        'name': fields.char('Simulation name', size=32, required=True),
        'code': fields.char('Simulation code', size=8, required=True),
    }
    _sql_constraints = [
        ('code_uniq', 'unique (code)', 'The code of the simulation must be unique !')
    ]
    _order = "name"
account_journal_simulation()

def _state_simul_get(self, cr, uid, context={}):
    obj = self.pool.get('account.journal.simulation')
    ids = obj.search(cr, uid, [])
    res = obj.read(cr, uid, ids, ['code', 'name'], context)
    return [('valid','Base')]+ [(r['code'], r['name']) for r in res]

class account_journal(osv.osv):
    _inherit = "account.journal"
    _columns = {
        'state': fields.selection(_state_simul_get, 'Status', required=True),
        'parent_ids': fields.many2many('account.journal', 'account_journal_simulation_rel', 'journal_src_id', 'journal_dest_id', 'Child journals'),
        'child_ids': fields.many2many('account.journal', 'account_journal_simulation_rel', 'journal_dest_id', 'journal_src_id', 'Parent journal'),
    }
    _defaults = {
        'state': lambda self,cr,uid,context: 'valid'
    }
account_journal()

class account_move_line(osv.osv):
    _inherit = "account.move.line"
    def search_not_run(self, cr, uid, crit, offset=0, limit=None, order=None, context={}):
        if not 'fiscalyear' in context:
            context['fiscalyear'] = self.pool.get('account.fiscalyear').find(cr, uid)
        ok = True
        for c in crit:
            if c[0]=='journal_id':
                ok = False
                break
        if 'journal_id' in context:
            ok=False
        if ok:
            plus = ''
            for state in context.get('journal_state', []):
                plus+=",'"+state+"'"
            cr.execute("select id from account_journal where state in ('valid'"+plus+")")
            crit.append(('journal_id', 'in', map(lambda x: x[0], cr.fetchall())))
        res = super(account_move_line, self).search(cr, uid, crit, offset, limit, order, context)
        return res

    def _query_get(self, cr, uid, obj='l', context={}):
        res = super(account_move_line, self)._query_get(cr, uid, obj, context)
        if context.get('journal_state', []):
            plus = " and ("+obj+".journal_id in (select id from account_journal where state in ('valid', "+','.join(map(lambda x: "'"+x+"'", context['journal_state']))+")))"
        else:
            plus = " and ("+obj+".journal_id in (select id from account_journal where state='valid'))"
        return res+plus
account_move_line()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:


#!/usr/bin/env python3

"""
From a set of zone transits representing trips between stops, work out
the effective trip time for a passenger arriving at the the origin every
minute from the departure time of the first bus to the departure time of
the last one
"""

import collections
import datetime
import logging
import sys
import csv

import isodate

zones = ['milton_pandr_south', 'milton_pandr_north']

logger = logging.getLogger('__name__')

header = [
    'Passenger_Arrival',
    'Passenger_Wait',
    'Bus_Departure',
    'Bus_Arrival',
    'Bus_Duration',
    'Bus_Interval',
    'Passenger_Duration',
]

def process_zones():

    for zone in zones:

        logger.debug('Processing %s', zone)

        # Read in...

        in_filename = 'transits-{}.csv'.format(zone)
        logger.info('Reading %s', in_filename)

        with open(in_filename, 'r', newline='') as in_file:
            input = csv.reader(in_file, dialect='excel', quoting=csv.QUOTE_ALL)
            next(input)   # Skip headers

            previous_depart = None
            trip_table = collections.OrderedDict()
            for row in input:

                trip = {}
                raw_arrive, raw_duration, raw_distance = row
                trip['arrive'] = isodate.parse_datetime(raw_arrive)
                trip['duration'] = datetime.timedelta(seconds=float(raw_duration))
                trip['depart'] = trip['arrive'] - trip['duration']
                day = trip['depart'].date()
                trip['distance'] = float(raw_distance)
                trip['interval'] = (trip['depart'] - previous_depart).total_seconds() if previous_depart else None

                if day not in trip_table:
                    trip_table[day] = []
                trip_table[day].append(trip)

                previous_depart = trip['depart']

        # ... write out

        step = datetime.timedelta(minutes=1)

        out_filename = 'trips-{}.csv'.format(zone)
        logger.info('writing %s', out_filename)
        with open(out_filename, 'w', newline='') as out_file:
            output = csv.writer(out_file, dialect='excel', quoting=csv.QUOTE_ALL)
            output.writerow(header)

            for day in trip_table:
                logger.info('Processing %s %s', zone, day)
                todays_trips = trip_table[day]

                # Find the minute before the first bus of the day
                start = todays_trips[0]['depart'].replace(second=0)
                # And the last departure of the day
                end = todays_trips[-1]['depart']

                logger.debug("Start %s, end %s, step %s", start, end, step)

                # Step through the day from 'start' to 'end' in steps of 'step'
                # Find the next bus to depart after 'start'
                while start < end:
                    # Find first departure after 'start'
                    for row in todays_trips:
                        logger.debug("row depart: %s, start: %s", row['depart'], start)
                        if row['depart'] > start:
                            wait = int((row['depart'] - start).total_seconds())
                            traveling = int((row['duration']).total_seconds())
                            trip_duration = wait + traveling
                            output.writerow([
                                start,
                                wait,
                                row['depart'],
                                row['arrive'],
                                traveling,
                                row['interval'],
                                trip_duration,
                            ])
                            break
                    else:
                        logger.error("No bus for a departure at %s", start)

                    start = start + step

def main():

    logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)

    logger.info('Start')

    process_zones()

    logger.info('Stop')

if __name__ == "__main__":
    main()


from django.shortcuts import render
from django.http import HttpResponse
from myapp.models import City,Country,Person
from myapp.forms import PersonForm
from django.shortcuts import redirect

# Create your views here.

def index(request):
    country=Country.objects.all()
    context={
        'country':country,
    }
    #return HttpResponse("hey%s"%slug)
    return render(request, 'myapp/home.html', context)

def add_person(request):
    if request.method=="POST":
        form=PersonForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect("/")
    else:
        form=PersonForm()
        context={
            'form':form,
        }
        return render(request, 'myapp/person.html', context)
def get_city(request,id):
    opt2_html = ""
    try:
        country=Country.objects.get(pk = id)
        city = City.objects.filter(country_id = country.id)
        # make_models  = company.makemodel_set.all()
        for c in city:
            opt2_html += "<option value='"+str(c.id)+"'>"+c.name+"</option>"
            print(opt2_html)
            context={
            'country':country,
            'city':city,
            }

    except:
        write_exception("Error in fetching options 2")
    return HttpResponse(opt2_html)
    # return render(request, 'myapp/home.html', context)


from stat_arb.src.data_loader.dao.dataframe.RawPostgresSampledDataLoader import RawPostgresSampledDataLoader
from stat_arb.src.data_loader.dao.dataframe.ClickhouseTradesDataLoader import ClickhouseTradesDataLoader
from stat_arb.src.data_loader.database import database_config
from datetime import datetime
from stat_arb.src.data_loader.general.Interval import Interval
from stat_arb.src.data_loader.general.SamplingSchemas import SamplingSchemas
from static_data import PATH

# queries = [
#     {'source': 'MOEX_DIRECT', 'instrument': 'USD/RUB_T+1', 'size': 1_000_000},
#     {'source': 'MOEX_DIRECT', 'instrument': 'EUR/USD_T+1', 'size': 1_000_000},
#     {'source': 'MOEX_DIRECT', 'instrument': 'CNH/RUB_T+1', 'size': 1_000_000},
#     {'source': 'RBI',         'instrument': 'EUR/USD_T+2', 'size': 1_000_000},
#     {'source': 'RBI',         'instrument': 'USD/CNH_T+2', 'size': 1_000_000},
# ]

queries = [
    {'source': 'MOEX', 'instrument': 'USD/RUB_T+1', 'size': 3_000_000}
]

interval = Interval(datetime(2021, 1, 1), datetime(2021, 12, 31))

def load_data(query: dict, interval: Interval):
    print('loading:\n', query, '\n', interval, '\n')
    with database_config.sql_engine_fxet_db1.connect() as connection:
        loader = RawPostgresSampledDataLoader(connection.connection.connection)
        vwap = loader.load_vwap_for_interval(query['source'],
                                             query['instrument'],
                                             interval,
                                             SamplingSchemas.FIRST_PRICE_PREDICTION_SCHEMA,
                                             query['size'])
    return vwap

if __name__ == '__main__':
    for q in queries:
        source = q['source'].split('_')[0].lower()
        instrument = q['instrument'].split('_')[0].replace('/', '').upper()
        spot_data = load_data(q, interval)
        spot_data.to_csv(f'{PATH}/{source}/{instrument}.csv')


# Реализуйте RLE алгоритм: реализуйте модуль сжатия и восстановления данных.
# Входные и выходные данные хранятся в отдельных текстовых файлах.

def get_coding(text):
    with open(text, 'r') as data:
        txt = data.readline()
    count = 1
    res = ''
    for i in range(len(txt)-1):
        if txt[i] == txt[i+1]:
            count += 1
        else:
            res = res + str(count) + txt[i]
            count = 1
    if count > 1 or (txt[len(txt)-2] != txt[-1]):
        res = res + str(count) + txt[-1]
        with open('coding.txt', 'w') as data:
            data.write(res)

def get_decoding(text):
    with open(text, 'r') as data:
        txt = data.readline()
    number = ''
    res = ''
    for i in range(len(txt)):
        if not txt[i].isalpha():
            number += txt[i]
        else:
            res = res + txt[i] * int(number)
            number = ''
    with open('decoding.txt', 'w') as data:
        data.write(res)
    return res

get_coding('text.txt')
get_decoding('coding.txt')


import argparse

from subcommands.setup.parser   import parser as setup_parser
from subcommands.export.parser  import parser as export_parser
from subcommands.info.parser    import parser as info_parser
from subcommands.process.parser import parser as process_parser
from subcommands.prune.parser   import parser as prune_parser
from subcommands.version.parser   import parser as version_parser

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description='Runs data processing live for incoming data'
    )
    subparsers = parser.add_subparsers()

    subparsers.add_parser(
        name='setup',
        help='Generate config files for setting up Hotspur',
        parents=[setup_parser]
    )
    subparsers.add_parser(
        name='process',
        help='Automatically find and process EM data',
        parents=[process_parser]
    )
    subparsers.add_parser(
        name='info',
        help='Retrieve info about projects and sessions',
        parents=[info_parser]
    )
    subparsers.add_parser(
        name='export',
        help='Export data alongside Relion metadata star files',
        parents=[export_parser]
    )
    subparsers.add_parser(
        name='prune',
        help='Remove processed data and databases for projects or sessions',
        parents=[prune_parser]
    )
    subparsers.add_parser(
        name='version',
        help='Print the current version',
        parents=[version_parser]
    )

    args = parser.parse_args()

    if 'config' in args:
        from utils.config import load_config
        load_config(args.config)

    if 'func' in args:
        args.func(args)
    else:
        parser.print_help()


#!/usr/bin/env python3
import argparse
import configparser
from pathlib import Path

from rich import console

import sys
sys.path.append("/home/vermin/IdeaProjects/summalarva")
from summalarva.openai_client import OpenAIClient
from summalarva.orgnoter import OrgNoter

console = console.Console()
config = configparser.ConfigParser()
argparser = argparse.ArgumentParser()
argparser.add_argument("input", type=str, help="Input file")
argparser.add_argument("--config", type=str, help="Config file")
args = argparser.parse_args()
input_path = Path(args.input).expanduser()

if args.config:
    config.read(args.config)
else:
    config.read(Path("~/.config/summalarva.ini").expanduser())

openai_api_key = config["openai"]["api_key"]
if config["openai"]["host"]:
    openai_host = config["openai"]["host"]
    openai_client = OpenAIClient(openai_api_key, openai_host)
else:
    openai_client = OpenAIClient(openai_api_key)

console.print("Start processing file", args.input)
summarises = openai_client.summarize_document(args.input)
try:
    org_noter = OrgNoter(args.input)
    for page_num,summary in summarises.items():
        org_noter.page_summarize_model_append(page_num, summary)
    console.print("Start create org noter")
    org_noter.create_note()
except Exception as e:
    raise e

summary_text = ""
for page_num, summary in summarises.items():
    summary_text += f"Page {page_num}\n\n{summary}\n\n"

with open("summary.txt", "w") as f:
    f.write(summary_text)


import pygame
import sys
from moviepy.editor import VideoFileClip
from PIL import Image

pygame.init()

music_background = pygame.mixer.music.load("assets/LostCompanionTomboFry.mp3")
pygame.mixer.music.play()
pygame.mixer.music.set_volume(0.2)

lar = 550
hut = 700

screen = pygame.display.set_mode((lar, hut))
pygame.display.set_caption("Menu")

gif_path = "assets/bg.gif"
clip = VideoFileClip(gif_path)
fps = clip.fps

frames = []
for t in range(0, int(clip.duration * fps)):
    frame = clip.get_frame(t / fps)
    pil_image = Image.fromarray((frame * 255).astype('uint8'))
    pil_image = pil_image.resize((lar, hut))
    pygame_image = pygame.image.fromstring(pil_image.tobytes(), pil_image.size, pil_image.mode)
    frames.append(pygame_image)

# Carregar recursos do menu antecipadamente
fonte = pygame.font.Font(None, 30)
texto_play = fonte.render("Play", True, (0, 0, 0))
texto_quit = fonte.render("Quit", True, (0, 0, 0))
Title = fonte.render("Pythongoras-Game", True, (255, 255, 255))

def mostrar_menu():
    frame_index = 0
    clock = pygame.time.Clock()

    while True:
        for evento in pygame.event.get():
            if evento.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif evento.type == pygame.MOUSEBUTTONDOWN:
                if batom_play.collidepoint(evento.pos):
                    pygame.time.delay(100)
                    iniciar_jogo()
                elif batom_quit.collidepoint(evento.pos):
                    pygame.quit()
                    sys.exit()

        screen.blit(frames[frame_index], (0, 0))

        batom_Title = pygame.Rect(190, 100 + 50, 150, 50)
        pos_text_Title = Title.get_rect(center=batom_Title.center)
        screen.blit(Title, pos_text_Title)

        batom_play = pygame.Rect(lar/2 - 75, hut/2 + 50, 150, 50)
        pygame.draw.rect(screen, (255, 255, 255), batom_play)
        pos_text_play = texto_play.get_rect(center=batom_play.center)
        screen.blit(texto_play, pos_text_play)

        if batom_play.collidepoint(pygame.mouse.get_pos()):
            pygame.draw.rect(screen, (200, 200, 200), batom_play)

        batom_quit = pygame.Rect(lar/2 - 75, hut/2 + 140, 150, 50)
        pygame.draw.rect(screen, (255, 255, 255), batom_quit)
        pos_text_quit = texto_quit.get_rect(center=batom_quit.center)
        screen.blit(texto_quit, pos_text_quit)

        if batom_quit.collidepoint(pygame.mouse.get_pos()):
            pygame.draw.rect(screen, (200, 200, 200), batom_quit)

        pygame.display.flip()

        frame_index = (frame_index + 1) % len(frames)

        clock.tick(fps)

def iniciar_jogo():
    print("O jogo começou!")
    import Chose

mostrar_menu()


import jittor as jt
from jittor import Module
from jittor import nn
import pygmtools as pygm
import numpy as np
import parameter

class AlexNet(Module):
    def __init__(self, *args, **kw) -> None:
        super().__init__(*args, **kw)
        self.padsize = parameter.parameters().pad
        self.kernel_size = parameter.parameters().kernel_size
        self.side_len = parameter.parameters().side_len

        self.CNNoutSize = int(self.side_len / 2 + self.padsize - (self.kernel_size - 1) / 2)
        self.layer1 = nn.Sequential(
            nn.Conv2d(in_channels=3, out_channels=16, kernel_size=self.kernel_size, stride=1, padding=self.padsize),
        )
        self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
        self.layer2 = nn.Sequential(
            nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, stride=1, padding=1),
        )
        self.fc = nn.Sequential(
            nn.Linear(in_features=self.CNNoutSize*self.CNNoutSize*32, out_features=128),
            nn.Relu(),
            nn.Linear(in_features=128, out_features=16)
        )

    def execute(self, x) -> None:
        x = self.layer1(x)
        x = self.pool(x)
        x = self.layer2(x)
        x = x.view(-1, self.CNNoutSize*self.CNNoutSize*32)
        x = self.fc(x)
        return x

class Net(Module):
    def __init__(self, sinkhorn_norm) -> None:
        pygm.BACKEND = 'jittor'
        self.slice = parameter.parameters().slice**2
        self.side_len = parameter.parameters().side_len

        if (sinkhorn_norm):
            self.execute = self.execute_sinkhorn
        else:
            self.execute = self.execute_sigmoid

        self.AlexNet = AlexNet()
        self.fc = nn.Sequential(
            nn.Linear(in_features=self.slice*16, out_features=64),
            nn.ReLU(),
            nn.Linear(in_features=64, out_features=self.slice ** 2),
        )

    def execute_sinkhorn(self, input) -> None:
        x = input
        x = jt.reshape(x, (-1, 3, self.side_len, self.side_len))
        x = self.AlexNet(x)
        x = jt.reshape(x, (-1, self.slice*16))
        x = self.fc(x)
        x = jt.reshape(x, (-1, self.slice, self.slice))
        x = pygm.sinkhorn(x)
        return x

    def execute_sigmoid(self, input) -> None:
        x = input
        x = jt.reshape(x, (-1, 3, self.side_len, self.side_len))
        x = self.AlexNet(x)
        x = jt.reshape(x, (-1, self.slice*16))
        x = self.fc(x)
        x = jt.nn.Sigmoid()(x)
        x = jt.reshape(x, (-1, self.slice, self.slice))
        return x


import copy
import functools
import os
import random
import torch
import torch.nn.functional as F
import blobfile as bf
import torchvision.utils as vutils
import numpy as np
import torch as th
import torch.distributed as dist
from torch.nn.parallel.distributed import DistributedDataParallel as DDP
from torch.optim import Adam
from ..models.unet.fp16_util import zero_grad
from tqdm  import tqdm
from ..utils import dist_util
import matplotlib.pyplot as plt
from .plotters import ImPlotter
from .config_getters import get_model

class IPFStepBase(th.nn.Module):
    def __init__(
        self,
        model,
        forward_diffusion,
        backward_diffusion,
        data_loader,
        prior_loader,
        cache_data_loader = None,
        args = None,
        forward_model = None,
        cache_loader = False,
        resume_checkpoint = 0,
        checkpoint_directory = './',
        plot_directory = './',
    ):

        super().__init__()

        self.set_seed(dist.get_rank()+0)
        ema_rate = args.ema_rate
        save_interval=args.save_interval
        lr_anneal_steps = 0
        self.args = args
        self.model = model
        self.forward_diffusion = forward_diffusion
        self.backward_diffusion = backward_diffusion

        self.forward_model = forward_model
        self.prior_loader = prior_loader
        self.data_loader = data_loader
        self.cache_data_loader = cache_data_loader

        self.num_steps = self.args.nit
        self.num_iter = self.args.num_iter
        self.lr_anneal_steps = lr_anneal_steps
        self.batch_size = self.args.batch_size
        self.cache_loader = cache_loader
        self.cache_refresh = self.args.cache_refresh_stride
        self.lr = self.args.lr
        self.classes = self.args.num_data_classes > 0
        self.weight_decay = self.args.weight_decay
        self.ema_rate = (
            [ema_rate]
            if isinstance(ema_rate, float)
            else [float(x) for x in ema_rate.split(",")]
        )
        self.save_interval = save_interval
        self.checkpoint_dir = checkpoint_directory
        self.plot_dir = plot_directory
        self.plotter = ImPlotter(im_dir=self.plot_dir, plot_level=1)

        self.step = 0
        self.resume_step = resume_checkpoint
        self.resume_checkpoint = resume_checkpoint
        self.global_batch = self.batch_size * dist.get_world_size()

        self.model_params = list(self.model.parameters())
        self.master_params = self.model_params
        self.sync_cuda = th.cuda.is_available()

        self._load_and_sync_parameters()

        # Optimizers
        self.opt = Adam(self.master_params, lr=self.lr)
        if self.resume_step:
            self._load_optimizer_state()
            # Model was resumed, either due to a restart or a checkpoint
            # being specified at the command line.
            self.ema_params = [
                self._load_ema_parameters(rate) for rate in self.ema_rate
            ]
        else:
            self.ema_params = [
                copy.deepcopy(self.master_params) for _ in range(len(self.ema_rate))
            ]

        if th.cuda.is_available():
            self.use_ddp = True
            self.ddp_model = DDP(
                self.model,
                device_ids=[dist_util.dev()],
                output_device=dist_util.dev(),
                broadcast_buffers=False,
                bucket_cap_mb=128,
                find_unused_parameters=False,
            )
        else:
            if dist.get_world_size() > 1:
                self.use_ddp = False
                self.ddp_model = self.model

    def _load_and_sync_parameters(self):
        resume_checkpoint = find_resume_checkpoint() or self.resume_checkpoint

        if resume_checkpoint:
            self.resume_step = parse_resume_step_from_filename(resume_checkpoint)
            if dist.get_rank() == 0:
                self.model.load_state_dict(
                    dist_util.load_state_dict(
                        resume_checkpoint, map_location=dist_util.dev()
                    )
                )

        dist_util.sync_params(self.model.parameters())

    def _load_ema_parameters(self, rate):
        ema_params = copy.deepcopy(self.master_params)

        main_checkpoint = find_resume_checkpoint() or self.resume_checkpoint
        ema_checkpoint = find_ema_checkpoint(main_checkpoint, self.resume_step, rate)
        if ema_checkpoint:
            if dist.get_rank() == 0:
                state_dict = dist_util.load_state_dict(
                    ema_checkpoint, map_location=dist_util.dev()
                )
                ema_params = self._state_dict_to_master_params(state_dict)

        dist_util.sync_params(ema_params)
        return ema_params

    def _load_optimizer_state(self):
        main_checkpoint = find_resume_checkpoint() or self.resume_checkpoint
        opt_checkpoint = bf.join(
            bf.dirname(main_checkpoint), f"opt{self.resume_step:06}.pt"
        )
        if bf.exists(opt_checkpoint):
            state_dict = dist_util.load_state_dict(
                opt_checkpoint, map_location=dist_util.dev()
            )
            self.opt.load_state_dict(state_dict)

    def optimize_step(self):
        #self._anneal_lr()
        # if self.args.grad_clipping:
        #     clipping_param = self.args.grad_clip
        #     total_norm = torch.nn.utils.clip_grad_norm_(self.model.parameters(), clipping_param)
        self.opt.step()
        for rate, params in zip(self.ema_rate, self.ema_params):
            update_ema(params, self.master_params, rate=rate)

    def _anneal_lr(self):
        if not self.lr_anneal_steps:
            return
        frac_done = (self.step + self.resume_step) / self.lr_anneal_steps
        lr = self.lr * (1 - frac_done)
        for param_group in self.opt.param_groups:
            param_group["lr"] = lr

    def save(self):
        if dist.get_rank() == 0:
            self.set_seed(0)
            init_samples, labels = next(self.prior_loader)
            init_samples = init_samples.to(dist_util.dev())
            labels = labels.to(dist_util.dev()) if labels is not None else None

            sample_model = get_model(self.args)
            for rate, params in zip(self.ema_rate, self.ema_params):
                state_dict = self._master_params_to_state_dict(params)
                sample_model.load_state_dict(state_dict)
                sample_model = sample_model.to(dist_util.dev())
                x_tot_plot = self.backward_diffusion.sample(init_samples, labels, t_batch=None, net=sample_model)
                filename = 'ema{0}_step{1}.png'.format(rate, self.step)
                self.plotter.plot(init_samples, x_tot_plot, filename)
            sample_model = None
            torch.cuda.empty_cache()

            # init_samples, labels = next(self.data_loader)
            # init_samples = init_samples.to(dist_util.dev())
            # labels = labels.to(dist_util.dev()) if labels is not None else None
            # x_tot_plot = self.forward_diffusion.sample(init_samples, labels, t_batch=None, net=self.forward_model)
            # filename = 'sample{0}_step{1}.png'.format(rate, self.step)
            # self.plotter.plot(init_samples, x_tot_plot, filename)

        def save_checkpoint(rate, params):

            state_dict = self._master_params_to_state_dict(params)
            if dist.get_rank() == 0:
                if not rate:
                    filename = f"model{(self.step+self.resume_step):06d}.pt"
                else:
                    filename = f"ema_{rate}_{(self.step+self.resume_step):06d}.pt"
                with bf.BlobFile(bf.join(self.checkpoint_dir, filename), "wb") as f:
                    th.save(state_dict, f)

        save_checkpoint(0, self.master_params)
        for rate, params in zip(self.ema_rate, self.ema_params):
            save_checkpoint(rate, params)

        if dist.get_rank() == 0:
            with bf.BlobFile(
                bf.join(self.checkpoint_dir, f"opt{(self.step+self.resume_step):06d}.pt"),
                "wb",
            ) as f:
                th.save(self.opt.state_dict(), f)

        dist.barrier()

    def _master_params_to_state_dict(self, master_params):
        state_dict = self.model.state_dict()
        for i, (name, _value) in enumerate(self.model.named_parameters()):
            assert name in state_dict
            state_dict[name] = master_params[i]
        return state_dict

    def _state_dict_to_master_params(self, state_dict):
        params = [state_dict[name] for name, _ in self.model.named_parameters()]
        return params

    def log_step(self):
        return

    def get_blob_logdir(self):
        return self.plot_dir

    def set_seed(self, seed=0):
        torch.manual_seed(seed)
        random.seed(seed)
        np.random.seed(seed)
        torch.cuda.manual_seed_all(seed)

def parse_resume_step_from_filename(filename):
    """
    Parse filenames of the form path/to/modelNNNNNN.pt, where NNNNNN is the
    checkpoint's number of steps.
    """
    split = filename.split("model")
    if len(split) < 2:
        return 0
    split1 = split[-1].split(".")[0]
    try:
        return int(split1)
    except ValueError:
        return 0

def find_resume_checkpoint():
    # On your infrastructure, you may want to override this to automatically
    # discover the latest checkpoint on your blob storage, etc.
    return None

def find_ema_checkpoint(main_checkpoint, step, rate):
    if main_checkpoint is None:
        return None
    filename = f"ema_{rate}_{(step):06d}.pt"
    path = bf.join(bf.dirname(main_checkpoint), filename)
    if bf.exists(path):
        return path
    return None

def update_ema(target_params, source_params, rate=0.99):
    """
    Update target parameters to be closer to those of source parameters using
    an exponential moving average.

    :param target_params: the target parameter sequence.
    :param source_params: the source parameter sequence.
    :param rate: the EMA rate (closer to 1 means slower).
    """
    for targ, src in zip(target_params, source_params):
        targ.detach().mul_(rate).add_(src, alpha=1 - rate)


import datetime,time,os,sys

if(sys.platform.lower().startswith('linux')):
    OS_TYPE = 'linux'
elif(sys.platform.lower().startswith('mac')):
    OS_TYPE = 'macintosh'
elif(sys.platform.lower().startswith('win')):
    OS_TYPE = 'windows'
else:
    OS_TYPE = 'invalid'

# Get our current directory
OUTPUT_FILE_DIRECTORY = os.getcwd()

def find_all(a_str, sub):
    """
    Returns the indexes of {sub} where they were found in {a_str}.  The values
    returned from this function should be made into a list() before they can
    be easily used.
    Last Update: 03/01/2017
    By: LB023593
    """

    start = 0
    while True:
        start = a_str.find(sub, start)
        if start == -1: return
        yield start
        start += 1

# Create variables for all the paths
if((OS_TYPE == 'windows')):
    # Clear Screen Windows
    os.system('cls')
    directories = list(find_all(OUTPUT_FILE_DIRECTORY,'\\'))
    OUTPUTS_DIR = OUTPUT_FILE_DIRECTORY[:directories[-1]] + '\\outputs\\'
    INPUTS_DIR = OUTPUT_FILE_DIRECTORY[:directories[-1]] + '\\inputs\\'
    SCRIPTS_DIR = OUTPUT_FILE_DIRECTORY[:directories[-1]] + '\\scripts\\'
    MODULES_DIR = OUTPUT_FILE_DIRECTORY[:directories[-1]] + '\\modules\\'
    MODULES_GITHUB_DIR = OUTPUT_FILE_DIRECTORY[:directories[-1]] + '\\modules\\github\\'
    CLASSES_DIR = OUTPUT_FILE_DIRECTORY[:directories[-1]] + '\\classes\\'
elif((OS_TYPE == 'linux') or (OS_TYPE == 'macintosh')):
    # Clear Screen Linux / Mac
    os.system('clear')
    directories = list(find_all(OUTPUT_FILE_DIRECTORY,'/'))
    OUTPUTS_DIR = OUTPUT_FILE_DIRECTORY[:directories[-1]] + '/outputs/'
    INPUTS_DIR = OUTPUT_FILE_DIRECTORY[:directories[-1]] + '/inputs/'
    SCRIPTS_DIR = OUTPUT_FILE_DIRECTORY[:directories[-1]] + '/scripts/'
    MODULES_DIR = OUTPUT_FILE_DIRECTORY[:directories[-1]] + '/modules/'
    MODULES_GITHUB_DIR = OUTPUT_FILE_DIRECTORY[:directories[-1]] + '/modules/github/'
    CLASSES_DIR = OUTPUT_FILE_DIRECTORY[:directories[-1]] + '/classes/'

# OS Compatibility for importing Class Files
if((OS_TYPE == 'linux') or (OS_TYPE == 'macintosh')):
    sys.path.insert(0,'../classes/')
    sys.path.insert(0,MODULES_DIR)
elif((OS_TYPE == 'windows')):
    sys.path.insert(0,'..\\classes\\')
    sys.path.insert(0,MODULES_DIR)

# < --- Begin Custom Classes Import --- >
# Custom Colors for printing to the screen
from custom_colors import *

from benchmark import *

from crypto_pairs import *

from command_line_arguments import *

from pseudothreading import *

from tracking import *

from pretty_formatting import *
# < ---  End  Custom Classes Import --- >

# Time all the things!
runtime = Benchmark()

# Text Coloration
cc = ColoredText(['exchange'],['38;5;214m'])

# Get parameters from commandline
parameters = Parse()

# Define what we're expecting to be passed in
parameters.add_expectation('-crypto-main', 'string', True, False)
parameters.add_expectation('-crypto-alt', 'string', True, False)

# Assign passed in values
parameters.parse_commandline()

# Check expectations were met
parameters.validate_requirements()

# World Reserve Crypto
main = parameters.get_parameter('-crypto-main').value
# Poor wanna be Crypto
alt = parameters.get_parameter('-crypto-alt').value

master = Metrics(main, alt)
selling, buying = master.call_order_book('binance')

# keys
# ['symbol', 'timestamp', 'datetime', 'high', 'low', 'bid', 'bidVolume', 'ask', 'askVolume', 'vwap', 'open', 'close', 'last', 'previousClose', 'change', 'percentage', 'average', 'baseVolume', 'quoteVolume', 'info']
ticker = master.call_fetch_ticker('binance')

print("\n bid:\t" + format(ticker['DOGE/BTC']['bid'], '.8f'))
print(" bidVolume:\t" + str(ticker['DOGE/BTC']['bidVolume']))
print(" ask:\t" + format(ticker['DOGE/BTC']['ask'], '.8f'))
print(" askVolume:\t" + str(ticker['DOGE/BTC']['askVolume']))
print("")
print(buying)
print("")
#print(buying[ticker['DOGE/BTC']['bid']])
print(str(buying[0][1]) + " @ " + format(buying[0][0],'.8f'))
print(buying[-1])

print("Buying:")
for counter in range(5,-1,-1):
    print(str(buying[counter][1]) + " @ " + format(buying[counter][0],'.8f'))

print("")
print("Selling:")
for counter in range(0,5):
    print(str(selling[counter][1]) + " @ " + format(selling[counter][0],'.8f'))

#https://www.binance.com/en/trade/DOGE_BTC


from abc import ABC, abstractmethod
import threading
import boto3
import botocore
import sys
import logging
import logging.config
from enum import Enum
from itertools import cycle
from botocore.config import Config
from botocore.endpoint import MAX_POOL_CONNECTIONS
from collections.abc import Iterable

class AWS_SVC_BASE(ABC):
    ''' Represent an AWS service that contain multiple resources(workers) '''
    aws_config = Config(
        retries=dict(
            total_max_attempts=25,
            mode='adaptive'
        ),
        max_pool_connections=MAX_POOL_CONNECTIONS,
    )

    def __init__(self, svc_type, session, svc_config):
        if not isinstance(session, boto3.Session):
            logging.error('session must be of type boto3.Session')
            raise(ValueError)
        if not isinstance(svc_type, AWS_SVC_TYPE):
            logging.error('svc_type must be of type AWS_SVC_TYPE')
            raise(ValueError)
        if not isinstance(svc_config, dict):
            logging.error('svc_config must be of type AWS_SVC_TYPE')
            raise(ValueError)
        self.session = session
        self.account_id = 0
        self.service_type = svc_type
        self.svc_config = svc_config
        self.rsc_prefix = svc_config['resource_prefix']
        self._key_lock = threading.Lock()
        self.worker_cycle = cycle(list())
        super().__init__()

    @abstractmethod
    def get_existing_workers(self):
        ''' Query the existing workers based on the rsc_prefix '''
        # pass

    @abstractmethod
    def create_workers(self):
        ''' Create workers/resources of this service '''
        # pass

    @abstractmethod
    def delete_workers(self):
        ''' Delete the workers created by create_workers() function '''
        # pass

    @abstractmethod
    def _check_existing_identity(self, identiy_arn):
        ''' Check if identiy_arn exists in AWS '''
        # pass

    def check_existing_user(self, aws_id, target_user, aws_partition = 'aws'):
        ''' Check if the target_user exists in AWS account aws_id '''
        user_arn = 'arn:{}:iam::{}:user/{}'.format(aws_partition, aws_id, target_user)
        return self._check_existing_identity(user_arn)

    def check_existing_role(self, aws_id, target_role, aws_partition = 'aws'):
        ''' Check if the target_role exists in AWS account aws_id '''
        role_arn = 'arn:{}:iam::{}:role/{}'.format(aws_partition, aws_id, target_role)
        return self._check_existing_identity(role_arn)

    def precheck(self):
        ''' Check if there is at least one resrouce to perform the test '''
        # If no object is in the cycle, the default value None will be returned
        if next(self.worker_cycle, None) is None:
            return False
        return True

    def _get_next_worker(self):
        with self._key_lock:
            try:
                return next(self.worker_cycle)
            except StopIteration:
                logging.error('Empty worker cycle')
                return None

    def _set_worker_cycle(self, iterable_obj):
        if not isinstance(iterable_obj, Iterable):
            logging.error('set_worker_cycle function expects an Iterable input')
            return
        self.worker_cycle = cycle(iterable_obj)

    def _check_boto3_response(self, resp):
        return 'ResponseMetadata' in resp and resp['ResponseMetadata']['HTTPStatusCode'] >= 200 and resp['ResponseMetadata']['HTTPStatusCode'] < 300

    def _enable_logging(self):
        logging.config.dictConfig({
            'version': 1,
            'disable_existing_loggers': True,
        })
        logging.basicConfig(level=logging.DEBUG, format='%(module)s: %(message)s')

class AWS_SVC_TYPE(Enum):
    IAM = 'iam'
    S3 = 's3'
    KMS = 'kms'
    SQS = 'sqs'


# 给定一个二叉搜索树，编写一个函数 kthSmallest 来查找其中第 k 个最小的元素。

# 说明：
# 你可以假设 k 总是有效的，1 ≤ k ≤ 二叉搜索树元素个数。

# 示例 1:

# 输入: root = [3,1,4,null,2], k = 1
#    3
#   / \
#  1   4
#   \
#    2
# 输出: 1
# 示例 2:

# 输入: root = [5,3,6,2,4,null,null,1], k = 3
#        5
#       / \
#      3   6
#     / \
#    2   4
#   /
#  1
# 输出: 3

# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

# Simple idea of combining dfs and quicksort.
# High time and memory cost.
class Solution0:
    def kthSmallest(self, root: TreeNode, k: int) -> int:
        def dfs(root, res):
            res.append(root.val)
            if root.left != None:
                dfs(root.left, res)
            if root.right != None:
                dfs(root.right, res)
        res = []
        dfs(root, res)

        def quicksort(seq, low, high):
            i = low
            j = high

            if low < high:
                base = seq[low]
                while i < j:
                    while seq[j] > base and j > i:
                        j -= 1
                    if j > i:
                        seq[i] = seq[j]
                        i += 1

                    while seq[i] < base and i < j:
                        i += 1
                    if i < j:
                        seq[j] = seq[i]
                        j -= 1

                seq[i] = base

                quicksort(seq, low, i-1)
                quicksort(seq, i+1, high)

        quicksort(res, 0, len(res)-1)
        return res[k-1]

# Inorder traversal.
class Solution1:
    def kthSmallest(self, root: TreeNode, k: int) -> int:
        def inorder(root, res):
            if root.left != None:
                inorder(root.left, res)
            res.append(root.val)
            if root.right != None:
                inorder(root.right, res)

        res = []
        inorder(root, res)
        return res[k-1]


from collections import deque

with open('day6.txt') as day6:
  lines = day6.readlines()

target_size = 14
current = 0

buffer = deque([''] * target_size)

for line in lines:
  for char in line:
    current = current + 1
    buffer.popleft()
    buffer.append(char)
    if current > target_size and len(set(buffer)) == target_size:
      print(current)
      break


from pages.investment_proposal.predefined_plan.predefined_plan import PredefinedPlanPage
from pages.investment_proposal.customized_plan.customized_plan import CustomizedPlanPage
from pages.investment_proposal.investment_proposal import InvestmentProposalPage
from pages.investment_proposal.investment_proposal_config import PREDEFINED_PLAN

import time

# Define the test case
def test_investment_proposal_page(driver):
    predefined_plan = PREDEFINED_PLAN
    predefined_plan_page = PredefinedPlanPage(driver)

    if predefined_plan:
        time.sleep(1)
        predefined_plan_page.click_investment_plan()
        predefined_plan_page.click_continue_button()
        time.sleep(1)
    else:
        predefined_plan_page.click_customized_plan()

        customized_plan_page = CustomizedPlanPage(driver)
        customized_plan_page.fill_customized_plan()

    investment_proposal_page = InvestmentProposalPage(driver)
    investment_proposal_page.click_continue_button()


# -*- coding: utf-8 -*-
"""
Created on Sat Nov 10 17:30:55 2018

@author: Wioletta
"""
import cv2
from localbinarypatterns import LocalBinaryPatterns

img = cv2.imread('yaleB01_P00A+000E+00.pgm')

cv2.imshow('Image',img)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

desc = LocalBinaryPatterns(24, 8)

hist = desc.describe(gray)

cv2.imshow('Histogram', hist)


import os

scriptPath = os.path.dirname(os.path.abspath(__file__))
projRootPath = os.path.abspath(
    os.path.join(scriptPath ,
                os.path.join('..', '..')))

import numpy as np

# matplotlib for displaying the output
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

from scipy import signal
from scipy.io import wavfile

# and IPython.display for audio output
import IPython.display

# Librosa for audio
import librosa

# And the display module for visualization
import librosa.display

#### Path to data
# Get data files
two_up =  os.path.abspath(os.path.join('.' ,"../.."))
print("Project root path is: ", two_up)

dataDirName = "data"
rawDataDirName = "converted_wav"
className = "violin"
# className = "guitar"
data_path = os.path.join(projRootPath, dataDirName, rawDataDirName, className)

print(data_path)
root_paths = []

# Get all files from data_path
# r=root, d=directories, f = files
(_, d, allFiles) = next(os.walk(data_path))
wavFiles = [f for f in allFiles if f.endswith(".wav")]

file = wavFiles[1]
sample_rate, samples = wavfile.read(os.path.join(data_path, file))
frequencies, times, spectrogram = signal.spectrogram(samples, sample_rate)

# all spectrogram
plt.pcolormesh(times, frequencies, spectrogram)
plt.imshow(spectrogram)
plt.ylabel('Frequency')
plt.gca().invert_yaxis()
plt.xlabel('Time')
plt.show()


def isACoveredByB(a, b):
    return a[0] >= b[0] and a[1] <= b[1]

class Solution:
    def removeCoveredIntervals(self, intervals: List[List[int]]) -> int:
        cntIntervals = len(intervals)
        cntToRemove = 0
        for i in range(cntIntervals):
            isCovered = 0
            for j in range(cntIntervals):
                if i == j:
                    continue
                if isACoveredByB(intervals[i], intervals[j]):
                    isCovered = 1
                    break
            cntToRemove += isCovered
        return cntIntervals - cntToRemove


from tkinter import *
from threading import Thread
from unpacker import *
from lookupData import *

from telemetryModule import *

import math

root = Tk()

root.title("F1 2021 Telemetry App")
root.geometry("{}x{}".format(1200, 800))
root.configure(background="#212026")

telemetry_modules = []

telemetry_data = [None] * 12

def update():
    for telemetry_module in telemetry_modules:
        telemetry_module.frame.after(1, telemetry_module.updateSize)

    root.geometry("{}x{}".format(round_to_multiple(root.winfo_width(), 12), round_to_multiple(root.winfo_height(), 8)))

    root.after(10, update)

def retrieve_packet_task():
    while True:
        packet = retrieve_packet()
        telemetry_data[packet.packetHeader.packetID] = packet

def round_to_multiple(x, base):
    return base * round(x / base)

def create_telemetry_module(name, column, row, x_span, y_span, colour):
    new_module = TelemetryModule(root, name, column, row, x_span, y_span, colour)
    telemetry_modules.append(new_module)

create_telemetry_module("Timing Tower", 0, 0, 3, 6, "gray")
create_telemetry_module("Pace Tower", 3, 0, 2, 6, "gray")
create_telemetry_module("Pace Graph", 5, 0, 7, 3, "gray")
create_telemetry_module("Predicted Finish Graph", 5, 3, 7, 3, "gray")
create_telemetry_module("Pit Monitor", 0, 6, 2, 2, "gray")
create_telemetry_module("Fuel Monitor", 2, 6, 2, 2, "gray")
create_telemetry_module("Weather Forecast", 4, 6, 8, 1, "gray")
create_telemetry_module("Pit Strategy", 4, 7, 8, 1, "gray")

thread = Thread(target = retrieve_packet_task)
thread.start()

root.after(1, update)
root.mainloop()


from redirect import config, cryptoDecrypt, datetime,  GenericException,jwt, logger, messages, timezone

def getClientServerTimeDiff(auth):
    try:
        token = auth.split(' ')[-1]
        decrypted = cryptoDecrypt(token)
        client_timestamp = float(decrypted)/1000
        dt = datetime.datetime.now(timezone.utc)
        utc_time = dt.replace(tzinfo=timezone.utc)
        server_timestamp = utc_time.timestamp()
        diff = server_timestamp - client_timestamp
        return diff
    except:
        raiseGenericException('errToken')

def validateTokenAndGetPayload(auth):
    try:
        token = auth.split(' ')[-1]  # get last word
        secret = config.get('authentication').get('jwt').get('secret')
        algorithm = config.get('authentication').get('jwt').get('algorithm')
        payload = jwt.decode(token, secret, algorithm)
        print(payload)
        return payload

    except jwt.ExpiredSignatureError as error1:
        logger.error(error1)
        raiseGenericException('errTokenExpired')
    except (Exception) as error:
        logger.error(error)
        raiseGenericException('errInvalidToken')

def raiseGenericException(errName):
    raise GenericException(
        code=messages[errName][0], name=errName, message=messages[errName][1])

def raiseGenericExceptionFn(errName, mess):
    raise GenericException(
            code= messages[errName](mess)[0], name = errName, message= messages[errName](mess)[1]
        )


# MIT 6.001 pset 1c

total_cost = 1000000.0
portion_down_payment = 0.25
total_down_payment = total_cost * portion_down_payment
current_savings = 0.0
r = 0.04
base_annual_sallary = 0.0
semi_annual_raise = 0.07
best_saving_rate = 0.0
money_range = 100.0
months = 36
init_upper = 10000
upper_bound = init_upper
lower_bound = 0
portion_saved = (upper_bound + lower_bound) / 2
steps = 0

base_annual_sallary = float(input("Whats your annual sallary? "))

while abs(current_savings - total_down_payment) > money_range:
    steps += 1
    current_savings = 0.0
    annual_sallary = base_annual_sallary
    monthly_salary = annual_sallary / 12
    monthly_deposit = monthly_salary * (portion_saved / 10000)

    for month in range(1, months + 1):
        current_savings += current_savings * (r/12)
        current_savings += monthly_deposit

        if month % 6 == 0:
            annual_sallary += annual_sallary * semi_annual_raise
            monthly_salary = annual_sallary / 12
            monthly_deposit = monthly_salary * (portion_saved / 10000)

    prev_portion_saved = portion_saved

    if current_savings > total_down_payment:
        upper_bound = portion_saved
    else:
        lower_bound = portion_saved

    portion_saved = int(round((upper_bound + lower_bound) / 2))

    if prev_portion_saved == portion_saved:
        break

if prev_portion_saved == portion_saved and portion_saved == init_upper:
    print("it is not possible to pay the house in three years")
else:
    print("Best savings rate is", portion_saved / 10000)
    print("Steps in bisection search:", steps)


import requests
import ast
import sys
import getopt

class XkcdClient():

    def api_call(self, url):
        self.urls = url

        r = requests.get(url = self.urls)
        byte_str = r.content
        dict_str = byte_str.decode("UTF-8")
        my_data = ast.literal_eval(dict_str)
        return my_data

    def get_image(self,img_url):
        self.img_name = img_url.split('/')[-1]
        img_data = requests.get(img_url).content
        with open(self.img_name, 'wb') as handler:
            handler.write(img_data)

# client = XkcdClient()
# response = client.api_call('https://xkcd.com/info.0.json')
# print(response)

if __name__ == '__main__':

    cmd_line_args = sys.argv[1:]
    unix_args = 'hn:os'
    gnu_args = ['help','comicnum=','print','save-image']

    oplist, args = getopt.getopt(cmd_line_args,unix_args,gnu_args)

    print(args) #Extra arguments that are not part of the uni_args or gnu_args
    print(oplist) #oplist is a list of tuples
    comic_num = ''
    client = XkcdClient()
    url_latest = 'https://xkcd.com/info.0.json'
    for opt, arg in oplist:
        print(opt)
        print(arg)

        if opt == '-h' or opt == '--help':
            print('help message')
            print('Use -n or --comicnum to specify the comic number you want use 0 as argument for latest comic')
            print('Use -o or --print to get info in text/json format')
            print('Use -s or --save-image to download image in this directory')
        elif opt == '-n' or opt == '--comicnum':
            comic_num = arg
            if comic_num is '0': #default get the latest comic
                print('Get the comic number ' + str(arg))
                response = client.api_call(url_latest)
                print(response)
            else:
                url_specific = 'http://xkcd.com/'+arg+'/info.0.json'
                response = client.api_call(url_specific)
        elif opt == '-o' or opt == '--print':
            if comic_num:
                if comic_num is '0':
                    print('print output in format json/text')
                    print(response)
                else:
                    print('The output in json/text is')
                    print(response)
            else:
                print('Set the -n parameter first')
        elif opt == '-s' or opt == '--save-image':
            if comic_num:
                img_url = response['img']
                client.get_image(img_url)
            else:
                print('Set the -n parameter first')


groups_number = int(input())
total_people = 0
musala_people = 0
mont_blanc_people = 0
kilimanjaro_people = 0
k2_people = 0
everest_people = 0
percent_musala = 0
percent_mont_blanc = 0
percent_kilimanjaro = 0
percent_k2 = 0
percent_everest = 0
for group in range(groups_number):
    current_people = int(input())
    if current_people <= 5:
        musala_people += current_people
    elif current_people <= 12:
        mont_blanc_people += current_people
    elif current_people <= 25:
        kilimanjaro_people += current_people
    elif current_people <= 40:
        k2_people += current_people
    elif current_people > 40:
        everest_people += current_people
total_people = musala_people + mont_blanc_people + kilimanjaro_people\
    + k2_people + everest_people
percent_musala = musala_people / total_people * 100
percent_mont_blanc = mont_blanc_people / total_people * 100
percent_kilimanjaro = kilimanjaro_people / total_people * 100
percent_k2 = k2_people / total_people * 100
percent_everest = everest_people / total_people * 100
print(f"{percent_musala:.2f}%")
print(f"{percent_mont_blanc:.2f}%")
print(f"{percent_kilimanjaro:.2f}%")
print(f"{percent_k2:.2f}%")
print(f"{percent_everest:.2f}%")