import pandas as pd
import numpy as np
import pickle as pkl
import math
import re
import glob
import datetime
import os
import matplotlib.pyplot as plt
import itertools
from sklearn.metrics import roc_curve
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import plot_confusion_matrix
from sklearn.decomposition import PCA
from sklearn.utils.multiclass import unique_labels
from sklearn.preprocessing import normalize
from sklearn.utils.random import sample_without_replacement
import joblib
from imblearn.over_sampling import RandomOverSampler, ADASYN, SMOTE
from sklearn.impute import KNNImputer
from werkzeug.utils import secure_filename
from werkzeug.datastructures import FileStorage
import json
if not os.path.exists('./model'):
    os.makedirs('./model')
if not os.path.exists('./ConfusionMatrix'):
    os.makedirs('./ConfusionMatrix')

def load_data():

    with open('./converted_csv.csv','rb') as f:
        #data = pkl.load(f)
        data = pd.read_csv(f, encoding = 'big5')
    data['性別'] = data['性別'].apply(lambda x: preprocessing.sex(x))
    data['年齡'] = data['年齡'].fillna(np.nan).apply(preprocessing.age)
    data['體重'] = data['體重'].astype(str).fillna(np.nan).apply(preprocessing.weight)
    data['身高'] = data['身高'].astype(str).fillna(np.nan).apply(preprocessing.height)
    data[['病史_DM','病史_HYPERLIPIDEMIA', '病史_HYPERTENSION', '病史_CVA', '病史_CARDIAC_DISEASE',
                '病史_COPD', '病史_ASTHMA', '病史_HEPATIC_DISEASE', '病史_RENAL_DISEASE',
                '病史_BLEEDING_DISORDER', '病史_MAJOR_OPERATIONS', '病史_SMOKING','病史_ALLERGY']] = data[['病史_DM','病史_HYPERLIPIDEMIA', '病史_HYPERTENSION', '病史_CVA', '病史_CARDIAC_DISEASE',
                '病史_COPD', '病史_ASTHMA', '病史_HEPATIC_DISEASE', '病史_RENAL_DISEASE',
                '病史_BLEEDING_DISORDER', '病史_MAJOR_OPERATIONS', '病史_SMOKING','病史_ALLERGY']].applymap(preprocessing.history)

    data[['LABDATA_HB', 'LABDATA_PLATELET', 'LABDATA_INR', 'LABDATA_PT',
            'LABDATA_APTT', 'LABDATA_CR', 'LABDATA_GOT', 'LABDATA_GPT',
            'LABDATA_SUGAR', 'LABDATA_NA', 'LABDATA_K']] = data[['LABDATA_HB',
                                                                'LABDATA_PLATELET', 'LABDATA_INR',
                                                                'LABDATA_PT', 'LABDATA_APTT',
                                                                'LABDATA_CR', 'LABDATA_GOT', 'LABDATA_GPT',
                                                                'LABDATA_SUGAR', 'LABDATA_NA', 'LABDATA_K']].astype(str).fillna(np.nan).applymap(preprocessing.lab)

    data['ASA_CLASS_E'] = data['ASA_CLASS_E'].astype(str).fillna(np.nan).apply(preprocessing.ASA_CLASS_E)
    data['BT'] = data['BT'].astype(str).apply(preprocessing.strQ2B).apply(preprocessing.BT)
    data['SPO2'] = data['SPO2'].astype(str).apply(preprocessing.strQ2B).apply(preprocessing.spo2)
    data['HR'] = data['HR'].astype(str).apply(preprocessing.strQ2B).fillna(np.nan).apply(preprocessing.HR)
    data['RR'] = data['RR'].astype(str).apply(preprocessing.strQ2B).fillna(np.nan).apply(preprocessing.RR)
    #data['BP'] = data['BP'].astype(str).apply(preprocessing.strQ2B).fillna(np.nan).apply(lambda x: preprocessing.BP(x))#, axis=1, result_type='expand')
    #data['SBP'] = data['BP'].apply(lambda x: preprocessing.a(x))
    #data['DBP'] = data['BP'].apply(lambda x: preprocessing.b(x))
    data['意識'] = data['意識'].astype(str).apply(preprocessing.strQ2B).fillna(np.nan).apply(preprocessing.consciousness)

    sex_bg = {'男': 1, '女': 0}
    consense = {'清楚': 1, 'nan':0}
    tmp = {'I':1,'II':2,'III':3,'IV':4,'V':5,'VI':6,'None':0}
    data['性別'] = data['性別'].replace(sex_bg)
    data['意識'] = data['意識'].apply(str).replace(consense).fillna(0)
    data['麻醉危險分級_ASA_CLASS'] = data['麻醉危險分級_ASA_CLASS'].replace(tmp)
    #data['麻醉危險分級(ASA Class)'] = data['麻醉危險分級(ASA Class)'].replace(tmp)

    with open('patient_data.pkl','wb') as f:
        pkl.dump(data,f)

class preprocessing():
    def sex(x):
        if x not in ['男','女']:
            return np.nan
        return x

    def age(x):
        try:
            return float(x)
        except:
            return x

    def weight(x):
        x = x.strip('\' +-=~<>約\/`？a*')
        try:
            return float(x)
        except:
            if '?' in x:
                return(np.nan)
            elif '-' in x:
                x = x.split('-')
                a, b = float(x[0]), float(x[1])
                return max([a, b])
            elif '~' in x:
                x = x.split('~')
                a, b = float(x[0]), float(x[1])
                return max([a, b])
            elif x in ['', 'None','不詳']:
                return(np.nan)
            elif 'g' in x:
                x = x.strip('gms')
                return(float(x)/1000)
            elif 'X' in x:
                x = x.strip('X')
                return(float(x*10))
            elif 'x' in x:
                x = x.strip('x')
                return(float(x*10))
            else:
                try:
                    return(float(x[0:2]))
                except:
                    print(x,'Wrong Format')

    def height(x):
        x = x.strip('\' +-=~<>約\/`？a*')
        try:
            return float(x)
        except:
            if '?' in x:
                return(np.nan)
            elif '-' in x:
                x = x.split('-')
                a, b = float(x[0]), float(x[1])
                return max([a, b])
            elif '~' in x:
                x = x.split('~')
                a, b = float(x[0]), float(x[1])
                return max([a, b])
            elif x in ['', 'None','不詳','未測']:
                return(np.nan)
            elif 'X' in x:
                x = x.strip('X')
                return(float(x*10))
            elif 'x' in x:
                x = x.strip('x')
                return(float(x*10))
            else:
                try:
                    return(float(x[0:2]))
                except:
                    print(x,'Wrong Format')

    def history(x):
        try:
            if x == None:
                return x
            elif 'yes' in x:
                return True
            elif 'denied' in x:
                return False
        except:
            print(x, 'Wrong Format')

    def lab(x):
        x = x.strip('\' +-=~<>約\/`？a*')
        try:
            return float(x)
        except:
            if x == 'None':
                return np.nan
            elif '?' in x:
                return np.nan
            elif '-' in x:
                x = x.split('-')
                a, b = float(x[0]), float(x[1])
                return max([a, b])
            elif '~' in x:
                x = x.split('~')
                a, b = float(x[0]), float(x[1])
                return max([a, b])
            elif x in ['', 'None','不詳','未測']:
                return np.nan
            elif 'X' in x:
                x = x.strip('X')
                return float(x*10)
            elif 'x' in x:
                x = x.strip('x')
                return float(x*10)
    #         else:
    #             try:
    #                 return float(x[0:2])
    #             except:
    #                 print(x,'Wrong Format')

    def ASA_CLASS_E(x):
        if 'E' in x:
            return True
        else:
            return False

    def strQ2B(ustring):
        """把字串全形轉半形"""
        rstring = ""
        for uchar in ustring:
            inside_code=ord(uchar)
            if inside_code==0x3000:
                inside_code=0x0020
            else:
                inside_code-=0xfee0
                if inside_code<0x0020 or inside_code>0x7e:   #轉完之後不是半形字元返回原來的字元
                    rstring += uchar
        return rstring
    def BT(x):
    #     x = x.strip('\' +-=~<>約\/`？a*.無牙DGQqRAMBULmbu/ˇˊ，。:%\’)Cc').replace(',','.').replace(';','').replace(' ','').replace('..','.') .strip('\=.ˇˊ')
        x = x.strip(' QqDG無牙-\=.ˇˊ`').replace('?','').replace(',','.').replace(';','').replace(' ','').replace('..','.')
    #     print(x,end='\r')
        try:
            return float(x)
        except:
            if x == 'None':
                return np.nan
            elif '-' in x:
                x = x.split('-')
                a, b = float(x[0]), float(x[1])
                return max([a, b])
            elif '~' in x:
                x = x.split('~')
                a, b = float(x[0]), float(x[1])
                return max([a, b])
            elif x in ['', 'none','不詳','未測', '測量不到', '-', '---', '。','’']:
                return np.nan
            elif 'X' in x:
                x = x.strip('X')
                return float(x*10)
            elif 'x' in x:
                x = x.strip('x')
                return float(x*10)
            elif x in ['3.6.3', '3.7.5', '3.6.7', '3.6.6']:
                return float(x.replace('.','',1))
            else:
                print(x,'Wrong Format')
                return np.nan

    # def BT2(x):
    #     m = re.search(r'(\d|\.)*/(\d|\.)*',x)
    #     m =  m.group()
    #     return float(m)

    def spo2(x):
        x = str(x)
        if x == 'None':
            return np.nan
        x = x.strip('-`+Lq%*/VE').replace(',','.').replace(';','').replace(' ','').replace('..','.').replace('AMBU','')
        x = x.replace('ambu','').replace('N/C','').replace('n/c','').replace('RA','').replace(':','').replace('()','').replace('3L','')
        x = x.replace('O2USE','').replace('masl6L','').replace('/MIN','')
        try:
            y = float(x)
            if 1500 > y >= 1000:
    #             print(x,y/10)
                return y/10
            elif 15000 > y >= 1500:
    #             print(x,y/100)
                return y/100
            elif 100000 > y >= 15000:
                return y/1000
            else:
                return float(x)
        except:
            if '?' in x:
                return np.nan

            elif '*' in x:
                return np.nan

            elif '+' in x:
                return np.nan

            elif '-' in x:
                x = x.split('-')
                a, b = float(x[0]), float(x[1])
                return max([a, b])

            elif '~' in x:
                x = x.split('~')
                a, b = float(x[0]), float(x[1])
                return max([a, b])

            elif x in ['', 'none','不詳','未測', '測量不到']:
                return np.nan

            elif 'X' in x:
                x = x.strip('X')
                return float(x*10)
            elif 'x' in x:
                x = x.strip('x')
                return float(x*10)

            else:
    #             print(x)
                return np.nan

    def HR(x):
        x = x.strip('*-/qAf`').replace(',','.').replace(';','').replace(' ','').replace('..','.')
    #     print(x+'         ',end='\r')
        try:
            return float(x)
        except:
            if x == 'None':
                return np.nan
            elif '?' in x:
                return np.nan
            elif '-' in x:
                x = x.split('-')
                a, b = float(x[0]), float(x[1])
                return max([a, b])
            elif '~' in x:
                x = x.split('~')
                a, b = float(x[0]), float(x[1])
                return max([a, b])
            elif x in ['', 'none','不詳','未測', '測量不到','-','=']:
                return np.nan
            elif 'X' in x:
                x = x.strip('X')
                return float(x*10)
            elif 'x' in x:
                x = x.strip('x')
                return float(x*10)
            elif '/' in x:
                return np.nan
            elif '==' in x:
                return np.nan
            else:
                print(x,'Wrong Format')
                return np.nan

    def RR(x):
        print(x+'         ',end='\r')
        x = x.strip('`+wmEhi呼吸器擠壓').replace('ventilator','').replace('Ventilator','').replace('bambu','').replace('AMBU','').replace('ambu','').replace('Ambu','').replace('amb','').replace('abu','')
        x = x.replace('o2','').replace('O2','').replace('3L','').replace('use','').replace('AR','').replace('MV/','')
        x = x.replace('/VT','').replace('/TV','').replace('tv','').replace('vt','').replace('V/','').replace('v/','').replace('/v','')
        x = x.replace('/V','').replace('/T','').replace('/t','').replace('VT','').replace('vt','').replace('V','')
        x = x.replace('N/C','').replace('n/c','').replace('/MV','').replace('MV','').replace('/mv','')
        x = x.replace('`','').replace(',','.').replace(';','').replace(' ','').replace('..','.').strip('AMCR/:-')

        try:
            return float(x)
        except:
            if x == 'None':
                return np.nan
            elif '?' in x:
                return np.nan
            elif 'L' in x:
                return np.nan
            elif '/' in x:
                return np.nan
            elif '-' in x:
                x = x.split('-')
                a, b = float(x[0]), float(x[1])
                return max([a, b])
            elif '~' in x:
                x = x.split('~')
                a, b = float(x[0]), float(x[1])
                return max([a, b])
            elif x in ['', 'none','不詳','未測', '測量不到', '呼吸器','endo','AC']:
                return np.nan
            elif 'x' in x:
                x = x.strip('x')
                return float(x*10)
            elif '-' in x:
                x = x.strip('%').split('-')

                #print(x[0],x[1])
                return max(float(x[0],float(x[1])))
            else:
                #print('\n')
                #print(x,'Wrong Format')
                return np.nan

    # data['RR'].unique()#.astype(str).apply(strQ2B).fillna(np.nan).apply(RR)

    def BP(x):
        x = x.replace('---', 'None').replace('=7', '').replace('//','/').replace('\\','/').strip('()*- `無法監量側未測不到左手左腳RHmeanilILoOMEANJsr')
        if x in['None','NIL','-/-','-', '--', '?','?/?', 'nil','Nil','未量','上述', '未側','未測量','測量不到','未測/未測','','NoneNone','NoneNone-','NoneNone--','/', 'no','NO','No','N/M','N/A','       /','--/--','None-','None--','無法監測','0','未','x']:
            return [np.nan, np.nan]
    #     if x in list1:
    #         return [float(x), float(x)]
    #     elif x in list2:
    #         x = x.split('/')
    #         return [float(x[0]), np.nan]
        if x in ['155/*77','130*71','125*/85']:
            x = x.replace('*','/').replace('//','/')
            x = x.split('/')
            return [float(x[0]), float(x[1])]
        elif '?' in x:
            return [np.nan, np.nan]
        elif x == '37.2':
            return [np.nan, np.nan]
        else:

            x = x.split('/')
            try:
                x = [abs(float(x[0])), abs(float(x[1]))]
            except:

                x = [abs(float(x[0][0:math.ceil(len(x[0])/2)])), abs(float(x[0][math.ceil(len(x[0])/2):6]))]

            return x

    def a(x):
        return x[0]
    def b(x):
        return x[1]

    def consciousness(x):
        if '清楚' in x or '活動' in x:
            return '清楚'
        else:
            return np.nan


import unittest
import excalibur
import datetime

class TestTimeConversion(unittest.TestCase):
    def test_convert_timestamp_between_timezones(self):
        dt_obj = datetime.datetime(2019, 5, 1, 18, 20, 28)
        dt_obj_converted = excalibur.convert_timestamp_between_timezones(
            dt_obj,
            excalibur.get_utc_timezone_name(),
            excalibur.get_ny_timezone_name()
        )
        expected_dt_obj = datetime.datetime(2019, 5, 1, 14, 20, 28)
        # expected_dt_obj = excalibur.set_datetime_obj_timezone(expected_dt_obj, excalibur.get_ny_timezone_name())
        self.assertEqual(dt_obj_converted.replace(tzinfo=None), expected_dt_obj)


if __name__ == '__main__':
    #创建文件
    f=open(r"F:\us.txt","w")
    mli=str([i for i in range(1,50)])
    print(f.writable())
    f.write("hukskldj ksd jfkjs f")
    f.writelines("555555555555")
    f.writelines(mli)

    f.close()


def primegenerator(num, val):
    # Write your code here
    l=[]
    for num in range(2,num+1):
        for i in range(2,num):
           if num%i == 0:
              break
        else:
            l.append(num)
    lt=len(l)
    if val:
        # yield l
        for i in range(lt):
            if i%2==0:
                yield l[i]
    else:
        for i in range(lt):
            if i%2!=0:
                yield l[i]
if __name__ == '__main__':

    num = int(input().strip())

    val = int(input().strip())

    for i in primegenerator(num, val):
        print(i,end=" ")


print("Programa Para determinar el porcentaje de inversión de tres personas")

def in1(x):
    a=(x*100)//t
    print("1° Inversionista:", a , "% del capital")
    return a
def in2(y):
    b=(y*100)//t
    print("2° Inversionista:", b , "% del capital")
    return b
def in3(z):
    c=(z*100)//t
    print("3° Inversionista:", c , "% del capital")
    return c

x=float(input("Primer inversionista:"))
y=float(input("Segundo inversionista:"))
z=float(input("Tercer inversionista:"))

t=x+y+z

print("Total de la inversion :" , t)
in1(x)
in2(y)
in3(z)


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

"""Test task logic functions."""
import os

from django.conf import settings
from django.contrib.auth import get_user_model
from django.shortcuts import reverse

from ontask import models, tests
from ontask.action import services
from ontask.dataops import pandas

class EmailActionTracking(tests.OnTaskTestCase):
    """Test Email tracking."""

    fixtures = ['simple_email_action']
    filename = os.path.join(
        settings.BASE_DIR(),
        'ontask',
        'fixtures',
        'simple_email_action.sql'
    )

    trck_tokens = [
        "eyJhY3Rpb24iOjIsInNlbmRlciI6Imluc3RydWN0b3IwMUBib2d1cy5jb20iLCJ0by"
        "I6InN0dWRlbnQwMUBib2d1cy5jb20iLCJjb2x1bW5fdG8iOiJlbWFpbCIsImNvbHVtbl"
        "9kc3QiOiJFbWFpbFJlYWRfMSJ9:1hCeQr:oax6nggj9kBkSdz1oFXfYVz8R4I",
        "eyJhY3Rpb24iOjIsInNlbmRlciI6Imluc3RydWN0b3IwMUBib2d1cy5jb20iLCJ0byI6I"
        "nN0dWRlbnQwMkBib2d1cy5jb20iLCJjb2x1bW5fdG8iOiJlbWFpbCIsImNvbHVtbl9kc3Q"
        "iOiJFbWFpbFJlYWRfMSJ9:1hCeQr:nLzLJRAGgiJhZWyJ-D6oGXlIY_E",
        "eyJhY3Rpb24iOjIsInNlbmRlciI6Imluc3RydWN0b3IwMUBib2d1cy5jb20iLCJ0byI6In"
        "N0dWRlbnQwM0Bib2d1cy5jb20iLCJjb2x1bW5fdG8iOiJlbWFpbCIsImNvbHVtbl9kc3Qi"
        "OiJFbWFpbFJlYWRfMSJ9:1hCeQr:5LuQISOvahDaiYuOYUufdfYRT_o"
    ]

    wflow_name = 'wflow1'
    wflow_desc = 'description text for workflow 1'
    wflow_empty = 'The workflow does not have data'

    def test_tracking(self):
        """Test that tracking hits are properly stored."""
        # Repeat the checks two times to test if they are accumulating
        for idx in range(1, 3):
            # Iterate over the tracking items
            for trck in self.trck_tokens:
                self.client.get(reverse('trck') + '?v=' + trck)

            # Get the workflow and the data frame
            workflow = models.Workflow.objects.get(name=self.wflow_name)
            data_frame = pandas.load_table(
                workflow.get_data_frame_table_name())

            # Check that the results have been updated in the DB (to 1)
            for uemail in [x[1] for x in tests.user_info
                           if x[1].startswith('student')]:
                self.assertEqual(
                    int(data_frame.loc[data_frame['email'] == uemail,
                               'EmailRead_1'].values[0]),
                    idx
                )

class ActionImport(tests.OnTaskTestCase):
    """Test action import."""

    fixtures = ['simple_email_action']
    filename = os.path.join(
        settings.BASE_DIR(),
        'ontask',
        'fixtures',
        'simple_email_action.sql'
    )

    wflow_name = 'wflow1'

    def test_do_import(self):
        """Test the do_import_action functionality."""
        user = get_user_model().objects.get(email='instructor01@bogus.com')
        wflow = models.Workflow.objects.get(name=self.wflow_name)

        with open(os.path.join(
            settings.BASE_DIR(),
            'ontask',
            'fixtures',
            'survey_to_import.gz'
        ), 'rb') as file_obj:
            services.do_import_action(user, workflow=wflow, file_item=file_obj)

        models.Action.objects.get(name='Initial survey')


from genius_invocation.card.character.base import Damage
from collections import defaultdict
from typing import Any, List, TYPE_CHECKING
from genius_invocation.utils import *
from loguru import logger
if TYPE_CHECKING:
    from genius_invocation.game.game import GeniusGame

class ListenerNode:
    '''监听者节点'''
    def __init__(self, action, before=None, next=None):
        self.action = action
        self.before = before
        self.next = next

    def remove(self):
        if self.before:
            self.before.next = self.next
        if self.next:
            self.next.before = self.before

    def __call__(self, game: 'GeniusGame') -> None:
        self.action(game)

class Event:
    def __init__(self) -> None:
        self.listeners: dict(ListenerList) = {}
        for zone_type in ZoneType:
            self.listeners[zone_type] = ListenerList([])

    def __call__(self, zone_type):
        return self.listeners[zone_type]

class ListenerList(object):
    def __init__(self, actions) -> None:
        self.head = ListenerNode(None)
        self.tail = ListenerNode(None)
        self.head.next = self.tail
        self.tail.before = self.head
        for action in actions:
            self.append_action(action)

    def append_action(self, action) -> ListenerNode:
        node = ListenerNode(action, self.tail.before, self.tail)
        self.tail.before.next = node
        self.tail.before = node
        return node

    def append(self, listener: ListenerNode) -> ListenerNode:
        listener.before = self.tail.before
        listener.next = self.tail
        self.tail.before.next = listener
        self.tail.before = listener
        return listener

    def __call__(self, game: 'GeniusGame') -> None:
        listener = self.head.next
        tail_before = self.tail.before
        while listener!=self.tail:
            listener(game)
            if listener == tail_before: break # DEBUG： Burning_Flame will not trigger when it is generated during summon_zone, end_phase.
            listener = listener.next

'''
事件种类
'''

class EventManager:
    def __init__(self) -> None:
        self.events = defaultdict(Event)

    def listen(self, event_type: EventType, zone_type: ZoneType, action) -> ListenerNode:
        '''
        监听事件
        event_type: 事件类型
        zone_type: zone类型
        action: 监听动作
        '''
        return self.events[event_type](zone_type).append_action(action)

    def invoke(self, event_type, game: 'GeniusGame') -> None:
        for zone_type in ZoneType:
            self.events[event_type](zone_type)(game)


# coding: utf-8

# In[50]:

def pair_sum(arr, k):
    l = []
    s = set()
    arr_length = len(arr)
    for i in range(arr_length-1):
        for j in range(i,(arr_length-1)):
            new_k = arr[i] + arr[j]
            if new_k == k:
                s.add((arr[i],arr[j]))
    t = list(s)
    print("\n".join(map(str,t)))

pair_sum([1, 5, 7, -1, 5],6)

# In[40]:

def pair_sum2(arr,k):
    seen = set()
    output = set()
    if len(arr) <= 1:
        return "array must contain 2 elements"
    for item in arr:
        target = item - k
        if target not in arr:
            seen.add(item)
        else:
            output.add((max(target,item),min(target,item)))
    print("\n".join(map(str,list(output))))

pair_sum2([1, 5, 7, -1, 5],6)

# In[ ]:

python handson/Python for data structure/array_pair_sum.py


import json
import sys

from pathlib import Path, PurePath

def read_schemas(working_dir):
    """Create tables for prepating upload of files from working_dir
    """
    print("Looking into %s" % working_dir)
    return [
        {"file_name": jf.stem, "fields": json.loads(jf.read_text())["fields"]}
        for jf in Path(working_dir).expanduser().glob("*_schema.json")
        if jf.stat().st_size > 0
    ]

def _new_record_type(name, fields):
    """Create place holder for composited field"""
    return {"name": name, "type": "RECORD", "mode": "NULLABLE", "fields": fields}

def concat_schemas(schemas):
    """Concat schemas into a new schema: each one of them is a column as a record type"""
    return [_new_record_type(s["file_name"], s["fields"]) for s in schemas]

def save(file_name, schema, name_modifier=None):
    """Save a BigQuery table's schema to a file

    Top field names can be modified if name_modifier has been assigned
    """
    if name_modifier:
        for field in schema:
            print(field["name"])
            field["name"] = name_modifier(field["name"])

    print(json.dumps(schema, indent=2))
    with open(file_name, "wt") as jf:
        json.dump(schema, jf)

if __name__ == "__main__":
    working_dir = "."
    if len(sys.argv) == 2:
        working_dir = sys.argv[1]

    # print(json.dumps(concat_schemas(read_schemas(working_dir)), indent=2))
    save(
        PurePath(working_dir, "combined_schema.json"),
        concat_schemas(read_schemas(working_dir)),
        lambda n: n.split("_")[0],
    )


import pathlib as p

cur_dir=p.Path.cwd()

print(cur_dir)
print(p.Path.home())
print(cur_dir.is_absolute())
print(list(cur_dir.parents))

path=p.Path.home() / 'test.sh'
for dir in path.parents:
  print(dir)
print(path.parent)  # return parent dir
print(path.anchor)  # return top dir in absolute path
print(path.name)    # cur file or dir name
print(path.stem)    # file-name only
print(path.suffix)  # file ext only

print(path.exists())  # True if exists
print(cur_dir.exists())

print(path.is_file())  # True if a file

print(path.is_dir())     # True if a directory
print(cur_dir.is_dir())

#---------------------

path = p.Path.cwd() / "python" / "car.py"
print(path)
print(path.exists())
print(path.name)
print(path.parent)
print(path.is_file())

#----------------------

file = p.Path.cwd() / "tmp" / "test.py"
print(file)
dirc = p.Path.cwd() / "tmp" / "tmp-dir"
file2 = p.Path.cwd() / "tmp" / "tmp-dir" / "test.txt"
print(dirc)

file.parent.mkdir(exist_ok=True, parents=True)
dirc.mkdir(exist_ok=True, parents=True)
file2.mkdir(exist_ok=True, parents=True)

print(file.is_file())
print(file.touch())
print(file.is_file())

for i in file.parent.iterdir():
  print(i)

for i in file.parent.glob('*'):
  print(i)

for i in file.parent.glob('*.py'):
  print(i)

for i in file.parent.glob('*.txt'):
  print(i)

new_dir = cur_dir / 'tmp'
paths = [
new_dir / "program1.py",
new_dir / "program2.py",
new_dir / "folder_a" / "program3.py",
new_dir / "folder_a" / "folder_b" / "image1.jpg",
new_dir / "folder_a" / "folder_b" / "image2.png",
]

for path in paths:
  path.parent.mkdir(exist_ok=True, parents=True)
  path.touch()

print(list(new_dir.glob("*.py")))
print(list(new_dir.glob("?older_?")))
print(list(new_dir.glob("*1.??")))
print(list(new_dir.glob("program[13].py")))

# limitation with glob and iterdir is - it will only search in the current directory at depth 0
# ** will solve this problem
print(list(new_dir.glob("**/*.txt")))

for f in list(new_dir.glob("**/*.py")):
  print(f)

print(list(new_dir.rglob("*.py")))         # rglob is same as glob with **

# Moving files - replace()

src = new_dir / 'test.py'
dst = new_dir / 'tmp-dir' / 'chk.py'

src.replace(dst)  # if destination file exist, it will overwrite without warning

# can use to rename directory or files

src = new_dir / 'tmp-dir'
dst = new_dir / 'temp-dir'

src.replace(dst)

# Deleting files/folders
# use unlink() method

file = new_dir / 'program1.py'
print(file.exists())
file.unlink(missing_ok=True)  # if file is missing, it's ok
print(file.exists())

# To delete a folder use rmdir() method but dir should be empty before

del_dir = new_dir / 'folder_a'
try:
  del_dir.rmdir()
except Exception as e:
  print(f"Dir Deletion Error - {e}")

for file in list(del_dir.rglob('*.*')):  # search all file with extension
  file.unlink(missing_ok=True)
for dir in  list(del_dir.rglob('*')):   # after deleting all files, deleting child directory
  dir.rmdir()
del_dir.rmdir()                         # deleting parent dir

# this all can be done easily via a library shutil
del_dir = new_dir / 'temp-dir'

import shutil
shutil.rmtree(del_dir)

#-------------

new_dir = cur_dir / 'tmp'
paths = [
new_dir / "img1.jpg",
new_dir / "img2.gif",
new_dir / "folder_a" / "img3.png",
new_dir / "folder_a" / "folder_b" / "img4.jpg",
]

# Creating all the files and folders
for path in paths:
  path.parent.mkdir(exist_ok=True, parents=True)
  path.touch()

img_dir = new_dir / 'img'
img_dir.mkdir(exist_ok=True, parents=True)

for file in new_dir.rglob('*.*'):
  file_name = file.name
  out_file = img_dir / file_name
  file.replace(out_file)

# Deleting the folder structure
shutil.rmtree(new_dir)


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
        if not lists:
            return None

        if len(lists) == 1:
            return lists[0]

        nums = []

        for node in lists:
            while node:
                nums.append(node.val)
                node = node.next

        nums.sort()

        ret = ListNode()
        current = ret
        for num in nums:
            current.next = ListNode(num)
            current = current.next

        return ret.next


import pandas as pd
import numpy as np
import streamlit as st
import pivottablejs as pt
st.set_page_config(page_title="Analisis Efectividad Essalud", page_icon=":bar_chart:", layout="wide")

# Supongamos que tienes un DataFrame llamado df que contiene tus datos
# Si aún no tienes un DataFrame, puedes cargar tus datos desde un archivo CSV o Excel.
df = pd.read_csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vTnluyDGAkS8o-IezNn9dAEK9KKJa8ZkdbsLM6ijhyhlnm6-b5FEiXdfVPMsEL1qg/pub?gid=2015743116&single=true&output=csv")

# Elimina todas las columnas que contienen "CIM" en su nombre
df_cant = df.copy()
columnas_cim = [col for col in df.columns if "CIM" in col]
df = df.drop(columns=columnas_cim)

# Lista de las palabras clave que deseas buscar en los nombres de las columnas
palabras_clave = ["Interpretación"]

# Filtra las columnas que contienen las palabras clave
columnas_seleccionadas = [col for col in df.columns if any(palabra in col for palabra in palabras_clave)]

# Crea un nuevo DataFrame con todas las otras columnas y las seleccionadas en formato vertical
df_vertical = pd.melt(df, id_vars=[col for col in df.columns if col not in columnas_seleccionadas],
                      value_vars=columnas_seleccionadas, var_name="Nombre de la Columna", value_name="Valor")

# Cambiar el tipo de datos de la columna "Fecha de nacimiento" a datetime
df_vertical['Fecha de nacimiento'] = pd.to_datetime(df_vertical['Fecha de nacimiento'], errors='coerce')

# Renombrar las columnas
df_vertical = df_vertical.rename(columns={'Nombre de la Columna': 'Medicamento_Antibiotico', 'Valor': 'Interpretacion_Origen'})

df_vertical['Interpretacion_Futuro'] = df_vertical['Interpretacion_Origen']

# Realizar las transformaciones
df_vertical['Interpretacion_Futuro'].replace({'S*': 'S', 'R*': 'R', 'I': 'I', 'IB': 'I', np.nan: '-'}, inplace=True)
df_vertical['Medicamento_Antibiotico'] = df_vertical['Medicamento_Antibiotico'].str.replace(' Interpretación', '')
# df_vertical.to_clipboard()
tabla_dinamica = pd.pivot_table(df_vertical,
                                 values='Fecha de muestra',  # Puedes seleccionar cualquier columna numérica
                                 index=['Origen','Microorganismo', 'Interpretacion_Futuro', 'Medicamento_Antibiotico'],
                                 aggfunc='count')

# Resetear el índice para que los índices se conviertan en columnas
tabla_dinamica = tabla_dinamica.reset_index()
tabla_dinamica = tabla_dinamica.rename(columns={'Fecha de muestra': 'Recuento'})
tabla_dinamica = tabla_dinamica[tabla_dinamica["Interpretacion_Futuro"]!="-"]
df_filtered_v = tabla_dinamica
# df_filtered_v.to_clipboard()
# st.sidebar.header("Aplique los filtros aqui:")
# origen = st.sidebar.multiselect(
#         "Seleccione el origen:",
#         options=df_filtered_v["Origen"].unique()
#     )
# medicamento = st.sidebar.multiselect(
#         "Seleccione el medicamento:",
#         options=df_filtered_v["Medicamento_Antibiotico"].unique()
#     )
# microorganismo = st.sidebar.multiselect(
#         "Seleccione el microorganismo:",
#         options=df_filtered_v["Microorganismo"].unique()
#     )
# interpretacion = st.sidebar.multiselect(
#         "Seleccione la interpretacion:",
#         options=df_filtered_v["Interpretacion_Futuro"].unique()
#     )

# if origen:
#         df = df[df["Origen"].isin(origen)]

with st.container():
    st.title("ESSALUD - MICROBIOLOGIA")

    # Crear un contenedor con dos columnas

    st.header("", divider="grey")
    st.title("Analisis por Tipo Origen - Bacterias mas Frecuentes")
    st.write("Clasificacion R: RESISTENTE - S: SENSIBLE")
    top3_activate = st.checkbox("¿Desea activar el TOP3 Bacterias mas frecuentes?")
    origen_fil = st.multiselect(
        "Seleccione el ORIGEN:",
        options=df_filtered_v["Origen"].unique(),key="Origen_Frecuente"
    )
    df_selection_fil = df_cant
    if origen_fil:
        df_selection_fil = df_selection_fil[df_selection_fil["Origen"].isin(origen_fil)]
        df_cant = df_cant[df_cant["Origen"].isin(origen_fil)]

    st.write(f"Cantidad de registros para el ORIGEN :                   **{df_cant.shape[0]}** muestras", unsafe_allow_html=True)
    microorganismo_fil = st.multiselect(
        "Seleccione el MICROORGANISMO:",
        options=df_filtered_v["Microorganismo"].unique(),key="Microorganismo_Frecuente"
    )
    if microorganismo_fil:
        df_selection_fil = df_selection_fil[df_selection_fil["Microorganismo"].isin(microorganismo_fil)]

#     # Checkbox para mostrar/ocultar el DataFrame general
#     # mostrar_dataframe = st.checkbox("Mostrar Tabla General de Datos", value=False)

#     # if mostrar_dataframe:

#     #     ordenar_por = st.selectbox("Ordenar por:", df_vertical.columns)
#     #     df_sorted = df_vertical.sort_values(by=[ordenar_por])

#     #     # Mostrar la tabla en Streamlit
#     #     st.dataframe(df_sorted)
#     # Si el checkbox está seleccionado, muestra el DataFrame
#     # filtro = st.text_input("Filtrar por Medicamento Antibiótico:", "")
#     # filtro_microorganismo = st.text_input("Filtrar por Microorganismo", "")
#     # filtro_interpretacion = st.text_input("Filtrar por Interpretacion", "")

#     # Aplicar filtros secuencialmente a la tabla dinámica

#     # if filtro:
#     #     df_filtered_v = df_filtered_v[df_filtered_v['Medicamento_Antibiotico'].str.contains(filtro, case=False)]

#     # if filtro_microorganismo:
#     #     df_filtered_v = df_filtered_v[df_filtered_v['Microorganismo'].str.contains(filtro_microorganismo, case=False)]

#     # if filtro_interpretacion:
#     #     df_filtered_v = df_filtered_v[df_filtered_v['Interpretacion_Futuro'].str.contains(filtro_interpretacion, case=False)]

#     # # Ordenar la tabla dinámica por el recuento en orden descendente
#     # df_filtered_v = df_filtered_v.sort_values(by=['Recuento'], ascending=False)

#     # # Mostrar la tabla dinámica filtrada y ordenada
#     # st.write("Tabla Dinámica de Recuento (Ordenada por Recuento Descendente)")
#     # st.dataframe(df_filtered_v)

#     df_selection = df_filtered_v
#     if origen:
#         df_selection = df_selection[df_selection["Origen"].isin(origen)]
#     if medicamento:
#         df_selection = df_selection[df_selection["Medicamento_Antibiotico"].isin(medicamento)]
#     if microorganismo:
#         df_selection = df_selection[df_selection["Microorganismo"].isin(microorganismo)]
#     if interpretacion:
#         df_selection = df_selection[df_selection["Interpretacion_Futuro"].isin(interpretacion)]

#     df_selection = df_selection.sort_values(by=['Recuento','Interpretacion_Futuro'], ascending=[False, False])
#     total_recuento = df_selection["Recuento"].sum()

# # Calcular el porcentaje y agregarlo como una nueva columna
#     df_selection["Porcentaje"] = ((df_selection["Recuento"] / total_recuento) * 100).apply(lambda x: f"{x:.2f}%")

#     st.dataframe(df_selection)
#     df_sens_resis = df_selection.copy()
#     df_sens_resis = df_sens_resis[(df_sens_resis["Interpretacion_Futuro"] == "R") | (df_sens_resis["Interpretacion_Futuro"] == "S")]
#     df_sensible = df_sens_resis[df_sens_resis["Interpretacion_Futuro"]=="S"]
#     df_resistente = df_sens_resis[df_sens_resis["Interpretacion_Futuro"]=="R"]
#     col1, col2 = st.columns(2)
#     try:
#         valor_mas_sensible=df_sensible.iloc[0,3]
#     except:
#         valor_mas_sensible = "No se tienen datos para medicamentos sensibles"

#     try:
#         valor_mas_resistente=df_resistente.iloc[0,3]
#     except:
#         valor_mas_resistente = "No se tienen datos para medicamentos resistentes"
#     # Columna 1
#     with col1:
#         st.header("MEDICAMENTO MAS SENSIBLE")
#         st.write(f"EL MEDICAMENTO MAS SENSIBLE ES: {valor_mas_sensible}")

#     # Columna 2
#     with col2:
#         st.header("MEDICAMENTO MAS RESISTENTE")
#         st.write(f"EL MEDICAMENTO MAS RESISTENTE ES: {valor_mas_resistente}")

    st.title("")

    tabla_conteo = df_cant.groupby(["Origen", "Microorganismo"]).size().reset_index(name="Conteo")
    tabla_conteo = tabla_conteo.sort_values(by="Conteo", ascending=False)
    total_filas = tabla_conteo["Conteo"].sum()
    tabla_conteo["Porcentaje"] = (tabla_conteo["Conteo"] / total_filas * 100).apply(lambda x: f'{x:.2f}%')
    top3 = tabla_conteo.nlargest(3, 'Conteo')
    if top3_activate:
        tabla_conteo = tabla_conteo[tabla_conteo['Conteo'].isin(top3['Conteo'])]
        lista_top3 = top3.values.tolist()
        total_filas = tabla_conteo["Conteo"].sum()
        tabla_conteo["Porcentaje"] = (tabla_conteo["Conteo"] / total_filas * 100).apply(lambda x: f'{x:.2f}%')
        top3 = tabla_conteo.nlargest(3, 'Conteo')
        total_conteo = tabla_conteo["Conteo"].sum()
        total_porcentaje = tabla_conteo["Porcentaje"].str.rstrip('%').astype(float).sum()

    # Crear un DataFrame para los totales
        totales = pd.DataFrame({'Conteo': [total_conteo], 'Porcentaje': [f'{total_porcentaje:.2f}%']})

    # Concatenar el DataFrame de totales al DataFrame original
        tabla_conteo = pd.concat([tabla_conteo, totales], ignore_index=True)

        st.dataframe(tabla_conteo) #modificar tabla

    else:
        total_filas = tabla_conteo["Conteo"].sum()
        tabla_conteo["Porcentaje"] = (tabla_conteo["Conteo"] / total_filas * 100).apply(lambda x: f'{x:.2f}%')
        top3 = tabla_conteo.nlargest(3, 'Conteo')
        total_conteo = tabla_conteo["Conteo"].sum()
        total_porcentaje = tabla_conteo["Porcentaje"].str.rstrip('%').astype(float).sum()

    # Crear un DataFrame para los totales
        totales = pd.DataFrame({'Conteo': [total_conteo], 'Porcentaje': [f'{total_porcentaje:.2f}%']})

    # Concatenar el DataFrame de totales al DataFrame original
        tabla_conteo = pd.concat([tabla_conteo, totales], ignore_index=True)

        st.dataframe(tabla_conteo)
    df_selection_fil = df_vertical
    if top3_activate:
        df_selection_fil = df_selection_fil[df_selection_fil["Microorganismo"].isin(tabla_conteo["Microorganismo"].to_list())]
    else:
        df_selection_fil = df_selection_fil

    # df_selection_fil.to_clipboard()

    filas = st.multiselect("Selecciona las filas:", ["Medicamento_Antibiotico","Fecha de muestra","Area","Muestra","Sexo"])
    df_selection_fil = df_selection_fil[df_selection_fil["Interpretacion_Futuro"].isin(["R","S"])]
    df_selection_fil["Recuento"] = 1
    if origen_fil:
        df_selection_fil = df_selection_fil[df_selection_fil["Origen"].isin(origen_fil)]
    if microorganismo_fil:
        df_selection_fil = df_selection_fil[df_selection_fil["Microorganismo"].isin(microorganismo_fil)]
    # columnas = st.multiselect("Selecciona las columnas:", df_selection_fil.columns)
    # valor = st.selectbox("Selecciona un valor:", df_selection_fil.columns)

    if 1:
                tabla_dinamica = pd.pivot_table(df_selection_fil, values="Recuento", index= filas, columns="Interpretacion_Futuro", aggfunc='count',fill_value=0)
                total_general = tabla_dinamica.sum().sum()

    # Calcular los porcentajes y agregarlos a la tabla dinámica
                tabla_porcentajes = tabla_dinamica.applymap(lambda x: f'{(x / total_general * 100):.2f}%' if x != 0 else '')
                nuevos_nombres_columnas = {
        'R': '% Resistente',
        'S': '% Sensible'
    }

                tabla_porcentajes = tabla_porcentajes.rename(columns=nuevos_nombres_columnas)

    # Concatenar la tabla de porcentajes a la tabla dinámica original
                tabla_dinamica = pd.concat([tabla_dinamica, tabla_porcentajes], axis=1)

                try:
                    tabla_dinamica = tabla_dinamica.sort_values(by='S', ascending=False)
                except:
                     pass
                def highlight_max(s):
                    is_max = s == s.max()
                    background_color = 'background-color: darkred'
                    text_color = 'color: white'
                    styles = [background_color if v else '' for v in is_max]
                    styles[0] += f'; {text_color}'  # Aplicar estilo de texto blanco al primer valor
                    return styles

# Aplicar el estilo condicional a las columnas donde deseas resaltar los valores más altos
                columnas_resaltar = ['% Resistente', '% Sensible']  # Aquí puedes especificar las columnas que deseas resaltar
                if ('% Resistente' in (tabla_dinamica.columns.tolist())) and ('% Sensible' in (tabla_dinamica.columns.tolist())):
                    st.write(tabla_dinamica.style.apply(highlight_max, subset=columnas_resaltar))
                elif "% Resistente" in (tabla_dinamica.columns.tolist()):
                    st.write(tabla_dinamica.style.apply(highlight_max, subset=["% Resistente"]))
                elif "% Sensible" in (tabla_dinamica.columns.tolist()):
                    st.write(tabla_dinamica.style.apply(highlight_max, subset=["% Sensible"]))

    # st.write("La tabla dinámica solicitada no funciono")

hide_st_style = """
            <style>
            #MainMenu {visibility: hidden;}
            footer {visibility: hidden;}
            header {visibility: hidden;}
            </style>
            """
st.markdown(hide_st_style, unsafe_allow_html=True)


# Day 22 - Pong
from turtle import Screen, Turtle
from paddle import Paddle, START_POSITION
from ball import Ball
from pong_score import Scoreboard
import time

# Build the playing field
screen = Screen()
screen.setup(width=800, height=600)
screen.bgcolor('black')
screen.title('Pong')
screen.tracer(0)

# Dash lines down the middle
dash = Turtle()
dash.sety(300)
dash.hideturtle()
dash.color('white')
dash.setheading(270)
dash.speed(0)
dash.pensize(3)
for i in range(15):
    dash.forward(20)
    dash.penup()
    dash.forward(20)
    dash.pendown()

# Paddles - controllable turtles on the sides
r_paddle = Paddle(START_POSITION[0])
l_paddle = Paddle(START_POSITION[1])
screen.listen()
screen.onkeypress(r_paddle.up, 'Up')
screen.onkeypress(r_paddle.down, 'Down')
screen.onkeypress(l_paddle.up, 'w')
screen.onkeypress(l_paddle.down, 's')

# Ball
ball = Ball()
ball_speed = 0.10

# Scoreboard
left_score = Scoreboard((-130, 250))
right_score = Scoreboard((100, 250))

game_on = True
while game_on:
    screen.update()
    time.sleep(ball_speed)
    ball.move(ball.xdir, ball.ydir)
    # Wall collision detection
    if ball.ycor() < -280 or ball.ycor() > 280:
        ball.ydir *= -1
    # Paddle collision detection
    if ball.distance(r_paddle) < 50 and ball.xcor() > 320 \
            or ball.distance(l_paddle) < 50 and ball.xcor() < -320:
        ball.xdir *= -1
        ball_speed *= 0.90
    # Detect when paddles miss
    if ball.xcor() > 370:
        left_score.increase_score()
        ball.home()
        ball_speed = 0.10
        ball.xdir = -1
    if ball.xcor() < -370:
        right_score.increase_score()
        ball.home()
        ball_speed = 0.10
        ball.xdir = 1

    # Game end
    if left_score.score == 3 or right_score.score == 3:
        game_on = False

screen.exitonclick()


#User function Template for python3

class Solution:
    def minPartition(self, n):
        # code here
        coins = [2000,500,200,100,50,20,10,5,2,1]
        ans=[]
        i=0
        while n and i<10:
            if n>=coins[i]:
                ans.append(coins[i])
                n=n-coins[i]
            else:
                i+=1
        return ans

#{
 # Driver Code Starts
#Initial Template for Python 3

if __name__ == '__main__':
    t = int(input())
    for _ in range(t):
        N = int(input())

        ob = Solution()
        arr = ob.minPartition(N)
        for i in range(len(arr)):
            print(arr[i],end=" ")
        print()
# } Driver Code Ends


import numpy as np
from math import ceil, floor
from typing import Optional, Tuple, List
from transformers import Temporal
import tensorflow as tf

class Batcher:
    def __init__(self, S: Tuple[np.ndarray, np.ndarray], method: str, batch_size: int,
                 transformer: Optional[Temporal] = None, training: bool = False,
                 seed: Optional[int] = None):
        X, T = S
        self.data = X.copy()
        self.info = T.copy()
        self.method = method
        self.batch_size = batch_size
        self.seed = seed
        self.n_wins = self.data.shape[0]
        self.length = self.data.shape[1]
        self.channels = self.data.shape[2]
        self.n_batches = floor(self.n_wins / self.batch_size)
        self.rng = np.random.default_rng(seed=seed)
        self.shuffled_data = None
        self.output_data = None
        self.transformer = transformer
        self.training = training

    def reset_data(self):
        index_list = np.arange(self.n_wins, dtype=int)
        self.rng.shuffle(index_list)
        self.shuffled_data = self.data[index_list]
        self.output_data = self.shuffled_data

    def __len__(self):
        return self.n_batches

    def get_shape(self):
        return self.transformer.get_shape()

    def __iter__(self):
        self.reset_data()

        def gen():
            for i in range(self.n_batches):
                if self.transformer:
                    batches = self.transformer(self.output_data[i * self.batch_size: (i + 1) * self.batch_size],
                                               training=self.training)
                else:
                    batches = self.output_data[i * self.batch_size: (i + 1) * self.batch_size]

                yield batches

        return gen()

class Zipper:
    def __init__(self, batched_datasets: List[Batcher], stack: bool = True, stack_axis: int = 0):
        self.datasets = batched_datasets
        self.stack = stack
        self.stack_axis = stack_axis
        self.n_batches = self.datasets[0].n_batches
        assert self.n_batches == self.datasets[1].n_batches
        self.batch_size = self.datasets[0].batch_size
        self.length = self.datasets[0].length
        self.channels = self.datasets[0].channels

    def __iter__(self):
        def gen():
            if self.stack:
                for zipped_batch in zip(*tuple(self.datasets)):
                    yield np.stack(zipped_batch, axis=self.stack_axis)

            else:
                for zipped_batch in zip(*tuple(self.datasets)):
                    yield zipped_batch

        return gen()

class Concatenator:
    def __init__(self, zipped_datasets: List[Zipper]):
        self.datasets = zipped_datasets
        self.n_dss = len(self.datasets)
        self.N_batches = 0
        for zipped_dataset in zipped_datasets:
            self.N_batches += zipped_dataset.n_batches
        self.batch_size = self.datasets[0].batch_size
        self.length = self.datasets[0].length
        self.channels = self.datasets[0].channels

    def get_shape(self):
        return (self.N_batches, 2, self.batch_size, self.length, self.channels)

    def __iter__(self):
        def gen():
            for dataset in self.datasets:
                for batch in dataset:
                    yield batch

        return gen()

def batch_concat(S: Tuple[np.ndarray, np.ndarray], method: str, batch_size: int,
                 dicts: List[dict], transformer: Optional[Temporal] = None,
                 same_ds: bool = True, same_sub: bool = True, same_act: bool = True,
                 training: bool = False, seed: Optional[int] = None) -> Concatenator:
    X, T = S
    ds_dict, act_dict = dicts
    X = X.copy()
    T = T.copy()

    groups = []
    if same_ds:
        for ds, ds_id in ds_dict.items():
            idx = np.argwhere(np.all(T[:, :, 1] == ds_id, axis=1)).squeeze()
            ds_X = X[idx]
            ds_T = T[idx]

            if same_sub:
                sub_arr = ds_T[:, :, 2]
                sub_ids = np.unique(sub_arr)

                for sub_id in sub_ids:
                    idx = np.argwhere(np.all(ds_T[:, :, 2] == sub_id, axis=1)).squeeze()
                    ds_sub_X = ds_X[idx]
                    ds_sub_T = ds_T[idx]

                    if same_act:
                        for act, act_id in act_dict.items():
                            idx = np.argwhere(np.all(ds_sub_T[:, :, 3] == act_id, axis=1)).squeeze()
                            ds_sub_act_X = ds_sub_X[idx]
                            ds_sub_act_T = ds_sub_T[idx]
                            groups.append((ds_sub_act_X, ds_sub_act_T))

                    else:
                        groups.append((ds_sub_X, ds_sub_T))

            else:
                groups.append((ds_X, ds_T))

    else:
        groups.append((X, T))

    gp_batches = []
    for group in groups:
        X, T = group
        anchor = (X[:, 0], T)
        target = (X[:, 1], T)

        batched_anchor = Batcher(anchor, method, batch_size, transformer, training, seed)
        batched_target = Batcher(target, method, batch_size, transformer, training, seed)
        batched_datasets = [batched_anchor, batched_target]

        gp_batches.append(Zipper(batched_datasets))

    batches = Concatenator(gp_batches)

    return batches

def to_generator(C: Concatenator, input_type: tf.dtypes.DType,
                 input_shape: np.ndarray, training: bool = False):
    def gen():
        for batch in C:
            anchor_batch, target_batch = batch
            yield anchor_batch, target_batch

    return tf.data.Dataset.from_generator(
        gen,
        output_types=(input_type, input_type),
        output_shapes=(input_shape, input_shape)
    )

def prefetch(data: tf.data.Dataset) -> tf.data.Dataset:
    return data.cache().repeat().prefetch(tf.data.AUTOTUNE)


class Solution(object):
    def sortArrayByParity(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        g,s=len(nums)-1,0

        while(g>s):
            if(nums[s]%2==0):
                s+=1
            elif(nums[g]%2!=0):
                g-=1
            else:
                nums[g],nums[s]=nums[s],nums[g]
                g-=1
                s+=1
        return(nums)


from collections import deque

with open('input.txt') as f:
    lines = f.read().split('\n')[:-2]

heights = []
end_x = 0
end_y = 0
starts = []

for y, line in enumerate(lines):
    heights.append([])

    for x, c in enumerate(line):
        if c == 'S':
            c = 'a'

        if c == 'E':
            end_x = x
            end_y = y
            c = 'z'

        if c == 'a':
            starts.append((x, y))

        heights[-1].append(ord(c) - ord('a'))

end = end_x, end_y
h = len(heights)
w = len(heights[0])

best_distance = 100000

for start in starts:
    q = deque([start])
    distance_to = {start: 0}

    while len(q):
        point = q.popleft()
        x, y = point

        for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
            new_x = x + dx
            new_y = y + dy

            if not (0 <= new_x < w and 0 <= new_y < h):
                continue

            new_point = new_x, new_y

            if new_point in distance_to:
                continue

            if heights[new_y][new_x] - heights[y][x] > 1:
                continue

            distance_to[new_point] = distance_to[point] + 1

            if new_point == end:
                q.clear()
                break

            q.append(new_point)

    if end in distance_to and distance_to[end] < best_distance:
        best_distance = distance_to[end]

print(best_distance)


from PyQt5.QtWidgets import *
from PyQt5.QtCore import pyqtSignal
import csv,os

class LogUI(QDialog):
    log_signal = pyqtSignal(bool,str)
    def __init__(self):
        super(LogUI,self).__init__()
        self.dialog_win = QDialog()
        self.dialog_win.setFixedSize(200,70)
        self.log_btn = QPushButton('Log',self.dialog_win)
        self.log_btn.clicked.connect(self.logging)
        self.cancel_btn = QPushButton('Cancel',self.dialog_win)
        self.cancel_btn.clicked.connect(self.cancel)
        self.file_name = QLineEdit()

    def showdialog(self):
        buttons = QHBoxLayout()
        buttons.addWidget(self.cancel_btn)
        buttons.addWidget(self.log_btn)
        form = QFormLayout()
        form.addRow('File name: ',self.file_name)
        form.addRow('',buttons)
        self.dialog_win.setLayout(form)
        self.dialog_win.setWindowTitle('Log')
        self.dialog_win.show()

    def logging(self):
        if self.file_name.text() == '':
            m = QMessageBox.information(self, 'Message', 'Please enter a file name', QMessageBox.Ok)
            if QMessageBox.Ok:
                pass
        else:
            self.log_signal.emit(True,self.file_name.text())
            self.dialog_win.close()

    def cancel(self):
        self.dialog_win.close()


from datetime import datetime
import logging
import traceback
from flask import request, make_response
import flask
from werkzeug.exceptions import HTTPException
from orchestrator.exceptions import BusinessRuleViolation
from orchestrator.tasks.exceptions import TaskExecutionException

logger = logging.getLogger(__name__)

def as_flask_error(error=None, message=None, details=None, traceback=None,
                   status=500, code='INTERNAL', timestamp=None):
    return flask.jsonify({
        'path': request.path,
        'url': request.url,
        'method': request.method,
        'message': message or str(error),
        'details': details,
        'traceback': traceback,
        'status': status,
        'code': code,
        'timestamp': timestamp or datetime.utcnow()
    }, status=status), status

def raise_error(message, details=None, traceback=None, status=500,
                code='INTERNAL'):
    """
    Creates error response by raising HTTPException

    :param output: dictionary wrapping exception parameters
    :param mimetype: MIMETYPE for the ouput.
    :param status: Http Status code.
    :param headers: Dictionary containing additional response headers
    :raises: HTTPException containing wrapped flask response.
    """
    resp = make_response(
        as_flask_error(
            message=message, details=details, traceback=traceback,
            status=status, code=code))
    raise HTTPException(response=resp)

def register(app, **kwargs):

    @app.errorhandler(404)
    def page_not_found(error):
        return as_flask_error(error, **{
            'message': 'The given resource:%s is not found on server'
                       % request.path,
            'code': 'NOT_FOUND',
            'status': 404
        })

    @app.errorhandler(TaskExecutionException)
    def task_error(error):
        return as_flask_error(error, **{
            'code': error.code,
            'message': error.message,
            'details': error.details,
            'traceback': error.traceback,
            'status': 500,
        })

    @app.errorhandler(BusinessRuleViolation)
    def business_rule_violation(error):
        return as_flask_error(error, **{
            'code': error.code,
            'message': error.message,
            'details': error.to_dict()['details'],
            'status': 422,
            })

    @app.errorhandler(Exception)
    @app.errorhandler(500)
    def internal(error):
        logger.exception('Unknown error happened while serving request.')
        trace = traceback.format_exc()
        try:
            details = error.to_dict()
        except AttributeError:
            details = None
        return as_flask_error(error, **{
            'code': 'INTERNAL',
            'details': details,
            'traceback': trace,
            'status': 500,
        })

    @app.errorhandler(406)
    def not_acceptable(error):
        return as_flask_error(error, **{
            'code': 'NOT_ACCEPTABLE',
            'status': 406
        })

    @app.errorhandler(415)
    def invalid_media_type(error):
        return as_flask_error(error, **{
            'code': 'INVALID_MEDIA_TYPE',
            'status': 415
        })


from seq_simul.models.qscore_generator import GeneratorQscoreGRU
from seq_simul.models.qscore_discriminator import DiscriminatorQscoreCNN
from seq_simul.utils.miscs import load_weights

def get_G_qscore(opt, device):
    r"""
        Returns Qscore Generator models

        INPUT:
            opt (:obj:`Namespace`):
                set of fixed parameters
            device (:obj:`str`):
                which devices to use (either cuda (with number) or cpu)

        OUTPUT:
            G (:obj:`Generator`):
                Qscore Generator Model
    """
    if opt.G_model == "GRU":
        G = GeneratorQscoreGRU(opt, device).to(device)
    else:
        raise ValueError("Check Generator model name!")

    # Load weights
    if opt.G_init_param_fname:
        G_weights = load_weights(opt.G_init_param_fname)
        G.load_state_dict(G_weights)

    return G

def get_D_qscore(opt, device):
    r"""
        Returns Qscore Discriminator models

        INPUT:
            opt (:obj:`Namespace`):
                set of fixed parameters
            device (:obj:`str`):
                which devices to use (either cuda (with number) or cpu)
        OUTPUT:
            D (:obj:`Discriminator`):
                Qscore Discriminator Model
    """
    if opt.D_model == "CNN":
        D = DiscriminatorQscoreCNN(opt).to(device)
    else:
        raise ValueError("Check Discriminator model name!")

    # Load weights
    if opt.D_init_param_fname:
        D_weights = load_weights(opt.D_init_param_fname)
        D.load_state_dict(D_weights)

    return D

def qscore_model(opt, device):
    r"""
        Returns Generator and Discriminator Qscore models

        INPUT:
            opt (:obj:`Namespace`):
                set of fixed parameters
            device (:obj:`str`):
                which devices to use (either cuda (with number) or cpu)

        OUTPUT:
            G (:obj:`Generator`):
                Qscore Generator Model
            D (:obj:`Discriminator`):
                Qscore Discriminator Model
    """
    G = get_G_qscore(opt, device)
    D = get_D_qscore(opt, device)

    return G, D


#!/usr/bin/env python3
#***********************************************************************************************************#
# Bare basic app to drive a 7 meter LED strip with a standard electrical light switches just like any       #
# ordinary light bulb in the house.  All leds on the strip just lighting up bright white or off.            #
#                                                                                #
# Led strips data line connected to pin 12 on the RPi (GPIO 18)                                             #
# Light switch 1 connected to pin 16 (GPIO 23) and pin 1 (3v3) to give it power through a 12kOhm resistor   #
# Light switch 2 connected to pin 18 (GPIO 24) and pin 17 (3v3) to give it power through a 12kOhm resistor  #
#***********************************************************************************************************#
from time import sleep
from RPi import GPIO
from rpi_ws281x import Color, PixelStrip, ws

# LED strip configuration:
#   215 leds on a single strip, connected to RPi GPIO 18 (pin 12)
LED_COUNT = 215
LED_PIN = 18
LED_FREQ_HZ = 800000  # LED signal frequency in hertz (usually 800khz)
LED_DMA = 10          # DMA channel to use for generating signal (try 10)
LED_BRIGHTNESS = 255  # Set to 0 for darkest and 255 for brightest
LED_INVERT = False    # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL = 0
LED_STRIP = ws.SK6812_STRIP_RGBW

# LED strip on or off:
LIGHT_STATUS = False

# Light switch 1 connected to pin 16 (GPIO 23) and pin 1 (3v3) to give it power through a 12kOhm resistor:
SWITCH_1_GPIO = 23
SWITCH_1_STATUS = False

# Light switch 2 connected to pin 18 (GPIO 24) and pin 17 (3v3) to give it power through a 12kOhm resistor:
SWITCH_2_GPIO = 24
SWITCH_2_STATUS = False

def lightsOn(strip):
  """Turn on the leds"""
  print("turn on lights")
  # Set the leds to white, full brightness:
  color = Color(0, 0, 0, 255)
  for i in range(strip.numPixels()):
    strip.setPixelColor(i, color)
  strip.show()

def lightsOff(strip):
  """Turn off the leds"""
  print("turn off lights")
  color = Color(0, 0, 0, 0)
  for i in range(strip.numPixels()):
    strip.setPixelColor(i, color)
  strip.show()

def toggleLight(strip):
  """Change the status of the light"""
  global LIGHT_STATUS, SWITCH_1_STATUS, SWITCH_2_STATUS
  _flag = LIGHT_STATUS
  _switch1 = GPIO.input(SWITCH_1_GPIO)
  if SWITCH_1_STATUS != _switch1:
    _flag = not _flag

  _switch2 = GPIO.input(SWITCH_2_GPIO)
  if SWITCH_2_STATUS != _switch2:
    _flag = not _flag

  if LIGHT_STATUS != _flag:
    SWITCH_1_STATUS = _switch1
    SWITCH_2_STATUS = _switch2
    if _flag:
      lightsOn(strip)
    else:
      lightsOff(strip)
    LIGHT_STATUS = _flag

# Main program:
if __name__ == '__main__':
  # Create PixelStrip object with appropriate configuration:
  global strip
  strip = PixelStrip(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL, LED_STRIP)
  # Intialize the library (must be called once before other functions):
  strip.begin()

  GPIO.setmode(GPIO.BCM)
  # Create GPIO button objects for both switches:
  GPIO.setup(SWITCH_1_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  GPIO.setup(SWITCH_2_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

  # Read the current status of the switches and set the light accordingly:
  toggleLight(strip)

  print('Press Ctrl-C to quit.')

  try:
    while True:
      # Infinite loop, checking the button status every so many milliseconds:
      toggleLight(strip)
      sleep(0.5)

  except KeyboardInterrupt:
    # Ctrl-C was hit.  Stop the app!
    lightsOff(strip)
    GPIO.cleanup()


from collections import defaultdict

from ..model.room import Room

class RoomService:
    def __init__(self):
        self._rooms = {}
        self._players = {}
        self._player_id_to_room_id = {}
        self._room_id_to_player_ids = defaultdict(set)

    def get_room_details_response(self, room_id):
        print("get_room_details_response", room_id, self._get_players_for_room_id(room_id))
        room = self._rooms[room_id]
        return dict(players=self._get_players_for_room_id(room_id), **self._rooms[room_id].to_dict(),)

    def get_all_rooms_response(self):
        return list(
            filter(
                lambda room: room["is_public"] and room["num_players"],
                [
                    dict(**room.to_dict(), num_players=len(self._get_players_for_room_id(room.room_id)),)
                    for room in self._rooms.values()
                ],
            )
        )

    def is_player_in_room(self, player_id, room_id):
        return self._player_id_to_room_id.get(player_id) == room_id

    def insert_player_in_room(self, player, room_id):
        self._add_player(player)
        old_room_id = self._set_player_room(player["id"], room_id)
        self._sanity_check()
        return old_room_id

    def delete_player(self, player_id):
        if not player_id in self._players:
            return None
        old_room_id = self._player_id_to_room_id[player_id]
        del self._player_id_to_room_id[player_id]
        del self._players[player_id]
        self._room_id_to_player_ids[old_room_id].remove(player_id)
        return old_room_id

    def update_player(self, player):
        if player["id"] not in self._players:
            raise KeyError(f"Player {player['id']} not recognized.")
        self._players[player["id"]] = player
        return self._player_id_to_room_id[player["id"]]

    def player_can_join_room(self, player_id, room_id):
        return True

    def _get_players_for_room_id(self, room_id):
        return [self._players[pid] for pid in self._room_id_to_player_ids[room_id]]

    def _add_player(self, player):
        self._players[player["id"]] = player

    def _set_player_room(self, player_id, room_id):
        if not room_id in self._rooms:
            self._create_room_for_player(room_id, player_id)

        old_room_id = self._player_id_to_room_id.get(player_id)
        if old_room_id:
            self._room_id_to_player_ids[old_room_id].remove(player_id)
        if old_room_id != room_id:
            self._room_id_to_player_ids[room_id].add(player_id)
            self._player_id_to_room_id[player_id] = room_id
            return old_room_id
        return None

    def _create_room_for_player(self, room_id, player_id):
        self._rooms[room_id] = Room(room_id, room_id != player_id)

    def _sanity_check(self):
        if not set(self._rooms) == set(self._room_id_to_player_ids):
            print("rooms", self._rooms)
            print("room id to player ids", self._room_id_to_player_ids)
            raise RuntimeError("Rooms does not match room ids to player ids")
        if not set(self._players) == set(self._player_id_to_room_id):
            print(self._players)
            print(self._player_id_to_room_id)
            raise RuntimeError("Player ids do not match player to room ids")
        for room_id, player_ids in self._room_id_to_player_ids.items():
            for player_id in player_ids:
                if not self._player_id_to_room_id.get(player_id) == room_id:
                    raise RuntimeError("Room id does not match")
        for player_id, room_id in self._player_id_to_room_id.items():
            if not player_id in self._room_id_to_player_ids.get(room_id, {}):
                raise RuntimeError("Room id does not match in player id -> room id")


def cpy():
    list1=[]
    list2=[]
    with open("story.txt","r")as f,open("demo1.txt","a")as f1:

        for i in f:
            list1=i.split()
            for j in list1:
                if len(j)>4:
                    f1.write(j)

cpy()


import logging
import os

from cros.factory.device import device_utils
from cros.factory.test import test_case
from cros.factory.utils.arg_utils import Arg
from cros.factory.utils import pygpt

class PartitionTableTest(test_case.TestCase):
  ARGS = [
      Arg('min_usage_pct', (int, float),
          'Percentage of the storage device that must be before the end of the '
          'stateful partition.  For example, if this is 95%, then the stateful '
          'partition must end at a sector that is >=95% of the total number of '
          'sectors on the device.',
          default=95),
      Arg('expand_stateful', bool,
          'Repair partition headers and tables and expand stateful partition '
          'to all available free space',
          default=True)
  ]

  def _ShowGPTTable(self, path):
    show_cmd = pygpt.GPTCommands.Show()
    show_cmd.ExecuteCommandLine(path)

  def setUp(self):
    self.dut = device_utils.CreateDUTInterface()
    self.gpt = None

  def runTest(self):
    self.assertTrue(self.dut.link.IsLocal(),
                    'This test only support local device')
    dev = self.dut.storage.GetMainStorageDevice()
    self.gpt = pygpt.GPT.LoadFromFile(dev)
    stateful_no = self.dut.partitions.STATEFUL.index
    stateful_part = self.gpt.GetPartition(stateful_no)
    minios_a_no = self.dut.partitions.MINIOS_A.index
    minios_a_part = self.gpt.GetPartition(minios_a_no)
    minios_b_no = self.dut.partitions.MINIOS_B.index
    start_sector = stateful_part.FirstLBA
    sector_count = stateful_part.blocks
    end_sector = start_sector + sector_count
    sector_size = self.gpt.block_size

    # Linux always considers sectors to be 512 bytes long independently of the
    # devices real block size.
    device_size = 512 * int(
        self.dut.ReadFile('/sys/class/block/%s/size' % os.path.basename(dev)))

    pct_used = end_sector * sector_size * 100 / device_size

    logging.info(
        'start_sector=%d, sector_count=%d, end_sector=%d, device_size=%d',
        start_sector, sector_count, end_sector, device_size)
    logging.info('Stateful partition extends to %.3f%% of storage',
                 pct_used)
    if pct_used < self.args.min_usage_pct:
      if not self.args.expand_stateful:
        self.FailTask('Stateful partition does not cover enough of storage '
                      'device')

      # Repair partition headers and tables
      self.gpt.Resize(pygpt.GPT.GetImageSize(dev))

      # In disk_layout_v3, minios_b is the last partition.
      # We have to remove it or we cannot expand the stateful partition.
      has_minios_b = self.gpt.IsLastPartition(minios_b_no)
      # Calculate the size of minios_a and reserve space when expanding
      # stateful partition.
      reserved_blocks = minios_a_part.blocks if has_minios_b else 0

      if has_minios_b:
        self.gpt.WriteToFile(dev)
        pygpt.RemovePartition(dev, minios_b_no)
        # Reload gpt table if we remove partition minios_b.
        self.gpt = pygpt.GPT.LoadFromFile(dev)

      _, new_blocks = self.gpt.ExpandPartition(stateful_no, reserved_blocks)
      # Write back GPT table.
      self.gpt.WriteToFile(dev)

      if not has_minios_b:
        self._ShowGPTTable(dev)
        return

      # Add back partition minios_b.
      add_cmd = pygpt.GPTCommands.Add()
      add_cmd.ExecuteCommandLine('-i', str(minios_b_no), '-t', 'minios', '-b',
                                 str(start_sector + new_blocks), '-s',
                                 str(reserved_blocks), '-l', 'MINIOS-B', dev)

      # Write the content of minios_a to minios_b_part.
      src = '%sp%d' % (dev, minios_a_no)
      dst = '%sp%d' % (dev, minios_b_no)
      self.dut.CheckCall([
          'dd', 'bs=1048576',
          'if=%s' % src,
          'of=%s' % dst, 'iflag=fullblock', 'oflag=dsync'
      ], log=True)
      self._ShowGPTTable(dev)


# n! = 1 * 2 * 3  * 4...*n

# n! = [1 * 2 * 3  * 4...n-1]*n

#n! = (n-1)! *n

# n = int(input("Enter Number For Factor: ")) -->Take Input from user
# n = 2
# product = 1
# for i in range(n):
#     product = product * (i+1)
# print(product)

def factorial_iter(n):
    product = 1
    for i in range(n):
        product = product * (i+1)
    return product

def factorial_recursive(n):
    if n == 1 or n == 0:
        return 1
    return n * factorial_iter(n-1)

# print(factorial_iter(4))
print(factorial_recursive(5))


import os, yaml, argparse, torch

from module.data import load_dataloader
from module.model import load_generator, load_discriminator
from module.pretrain import GenTrainer, DisTrainer
from module.train import Trainer
from module.test import Tester

from module.generate import generate

from transformers import set_seed, AutoTokenizer

class Config(object):
    def __init__(self, args):

        with open('config.yaml', 'r') as f:
            params = yaml.load(f, Loader=yaml.FullLoader)
            for group in params.keys():
                for key, val in params[group].items():
                    setattr(self, key, val)

        self.mode = args.mode
        self.search_method = args.search

        use_cuda = torch.cuda.is_available()
        self.device_type = 'cuda' \
                           if use_cuda and self.mode != 'inference' \
                           else 'cpu'
        self.device = torch.device(self.device_type)

        self.g_ckpt = 'ckpt/generator.pt'
        self.d_ckpt = 'ckpt/discriminator.pt'
        self.tokenizer_path = f'data/{self.task}/tokenizer.json'

    def print_attr(self):
        for attribute, value in self.__dict__.items():
            print(f"* {attribute}: {value}")

def load_tokenizer(config):
    return

def pretrain(config, g_model, d_model, tokenizer):

    ###PreTrain Generator with Character Dataset
    g_train_dataloader = load_dataloader(config, tokenizer, 'train')
    g_valid_dataloader = load_dataloader(config, tokenizer, 'valid')

    g_trainer = GenTrainer(config, g_model, g_train_dataloader, g_valid_dataloader)
    g_trainer.train()

    ###Generate Samples to PreTrain Discriminator
    generate(config, g_model, tokenizer)

    ###PreTrain Discriminator
    config.model_type = 'discriminator'
    d_train_dataloader = load_dataloader(config, tokenizer, 'train')
    d_valid_dataloader = load_dataloader(config, tokenizer, 'valid')

    d_trainer = DisTrainer(config, d_model, d_train_dataloader, d_valid_dataloader)
    d_trainer.train()

def train(config, g_model, d_model, tokenizer):
    train_dataloader = load_dataloader(config, tokenizer, 'train')
    valid_dataloader = load_dataloader(config, tokenizer, 'valid')

    trainer = Trainer(config, g_model, d_model, tokenizer,
                      train_dataloader, valid_dataloader)
    trainer.train()

def test(config, g_model, d_model, tokenizer):
    test_dataloader = load_dataloader(config, 'test')
    tester = Tester(config, g_model, d_model, tokenizer, test_dataloader)
    tester.test()

def inference(g_model, tokenizer):
    g_model.eval()
    print(f'--- Inference Process Started! ---')
    print('[ Type "quit" on user input to stop the Process ]')

    while True:
        input_seq = input('\nUser Input Sequence >> ').lower()

        #End Condition
        if input_seq == 'quit':
            print('\n--- Inference Process has terminated! ---')
            break

        #convert user input_seq into model input_ids
        input_ids = tokenizer(input_seq, return_tensors='pt')['input_ids']
        output_ids = g_model.generate(input_ids, max_new_tokens=128, use_cache=True)
        output_seq = tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0]

        #Search Output Sequence
        print(f"Model Out Sequence >> {output_seq}")

def main(args):
    set_seed(42)
    config = Config(args)
    tokenizer = load_tokenizer(config)

    g_model = load_generator(config)
    d_model = load_discriminator(config)

    if config.mode == 'pretrain':
        pretrain(config, g_model, d_model, tokenizer)
    elif config.mode == 'train':
        train(config, g_model, d_model, tokenizer)
    elif config.mode == 'test':
        test(config, g_model, d_model, tokenizer)
    elif config.mode == 'inference':
        inference(g_model, tokenizer)

if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument('-mode', required=True)

    args = parser.parse_args()
    assert args.mode.lower() in ['pretrain', 'train', 'test', 'inference']

    if args.mode != 'pretrain':
        assert os.path.exists('ckpt/generator.pt')
        assert os.path.exists('ckpt/discriminator.pt')

    main(args)


from brave.inputs.input import Input
from gi.repository import Gst
import brave.config as config
import brave.exceptions

class TcpClientInput(Input):
    '''
    Allows an an input by receiving from another server via TCP.
    Basically using the `tcpclientsrc` element.
    '''

    def permitted_props(self):
        return {
            **super().permitted_props(),
            'host': {
                'type': 'str',
                'required': True
            },
            'port': {
                'type': 'int',
                'required': True
            },
            'width': {
                'type': 'int'
            },
            'height': {
                'type': 'int'
            },
            'container': {
                'type': 'str',
                'default': 'mpeg',
                'permitted_values': {
                    'mpeg': 'MPEG',
                    'ogg': 'OGG'
                }
            }
        }

    def create_elements(self):
        '''
        Creates the pipeline with elements needed to accept a TCP connection.
        '''

        # We support ogg and mpeg containers; the right demuxer must be used:
        demux_element = 'oggdemux' if self.container == 'ogg' else 'tsdemux'

        # We start with tcpclientsrc, and immediately demux it into audio and video.
        pipeline_string = 'tcpclientsrc name=tcpclientsrc ! %s name=demux ' % demux_element

        if self.has_video():
            # We need to decode the video:
            pipeline_string += (' queue2 max-size-time=3000000000 name=demux_to_video_queue ! '
                                'decodebin name=video_decodebin ')
            # This then can be connected to the common bit of the pipeline:
            pipeline_string += self.default_video_pipeline_string_end()

        if self.has_audio():
            # The audio part also needs a decodebin:
            pipeline_string += (' queue2 max-size-time=3000000000 name=demux_to_audio_queue'
                                ' ! decodebin name=audio_decodebin')

            # We will then connect this decodebin to aan audio convert/resample
            pipeline_string += ' audioconvert name=audioconvert ! audioresample ! ' + \
                config.default_audio_caps() + \
                self.default_audio_pipeline_string_end()

        self.create_pipeline_from_string(pipeline_string)
        tcpclientsrc = self.pipeline.get_by_name('tcpclientsrc')
        tcpclientsrc.set_property('host', self.host)
        tcpclientsrc.set_property('port', self.port)

        if self.has_video():
            self.final_video_tee = self.pipeline.get_by_name('final_video_tee')
            self.demux_to_video_queue = self.pipeline.get_by_name('demux_to_video_queue')
            self.video_element_after_demux = self.pipeline.get_by_name('video_output_queue')
            video_decodebin = self.pipeline.get_by_name('video_decodebin')
            video_decodebin.connect('pad-added', self._on_decodebin_pad_added)

        if self.has_audio():
            self.final_audio_tee = self.pipeline.get_by_name('final_audio_tee')
            self.demux_to_audio_queue = self.pipeline.get_by_name('demux_to_audio_queue')
            self.audio_element_after_demux = self.pipeline.get_by_name('audioconvert')
            audio_decodebin = self.pipeline.get_by_name('audio_decodebin')
            audio_decodebin.connect('pad-added', self._on_decodebin_pad_added)

        demux = self.pipeline.get_by_name('demux')
        demux.connect('pad-added', self._on_demux_pad_added)

    def _on_demux_pad_added(self, _, pad):
        '''
        Demux creates new pads every time the stream starts.
        This handles the creation of a new pad by linking it to the relevant element.
        The pipeline is:
        - For video: demuxer --> demux_to_video_queue --> decodebin --> ...
        - For audio: demuxer --> demux_to_audio_queue --> decodebin --> ...
        '''
        if not pad.has_current_caps():
            return

        caps = pad.get_current_caps()
        structure = caps.get_structure(0)
        name = structure.get_name()
        element_to_connect_to = None
        if name.startswith('video'):
            if hasattr(self, 'demux_to_video_queue'):
                element_to_connect_to = self.demux_to_video_queue

        elif name.startswith('audio'):
            if hasattr(self, 'demux_to_audio_queue'):
                element_to_connect_to = self.demux_to_audio_queue

        else:
            raise brave.exceptions.PipelineFailure('TCP Client: Unexpected pad name %s' % name)

        if not element_to_connect_to:
            return

        pad_to_connect_to = element_to_connect_to.get_static_pad('sink')
        if pad.link(pad_to_connect_to) is not Gst.PadLinkReturn.OK:
            self.logger.warning('Unable to connect demuxer to queue')

    def _on_decodebin_pad_added(self, _, pad):
        '''
        Like demux, decodebin creates new pads every time the stream starts.
        This handles the creation of a new pad by linking it to the relevant element.
        The pipeline is:
        - For video: decodebin --> video_output_queue --> tee --> ...
        - For audio: decodebin --> audioconvert --> audioresample --> ...
        '''
        if not pad.has_current_caps():
            return

        caps = pad.get_current_caps()
        structure = caps.get_structure(0)
        name = structure.get_name()
        element_to_connect_to = None
        if name.startswith('video'):
            element_to_connect_to = self.video_element_after_demux
        elif name.startswith('audio'):
            element_to_connect_to = self.audio_element_after_demux
        else:
            raise brave.exceptions.PipelineFailure('TCP Client: Unexpected decodebin pad name %s' % name)

        pad_to_connect_to = element_to_connect_to.get_static_pad('sink')
        if pad.link(pad_to_connect_to) is not Gst.PadLinkReturn.OK:
            self.logger.warning('Unable to connect demuxer to queue')


# Import Modules
import os
import csv
import sys

#create variables
month_counter = 0
total_pl = 0
this_month_pl = 0
prior_month_pl = 0
pl_change = 0
change_listing = []
months = []

# Define csv file
budget_data = os.path.join("Resources", "budget_data.csv")
# Define average function
def average(numbers):
    length = len(numbers)
    total = 0.0
    for number in numbers:
        total += number
    return round(total / length,2)

# open csvfile:
with open(budget_data) as csvfile:
    csvreader = csv.reader(csvfile, delimiter=",")

# store the header row so it won't be included in calculations
    csv_header = next(csvreader)

    # Loop through the file
    i = -2
    priormonth = []
    for row in csvreader:
        # Add total months
        month_counter = month_counter + 1
        i=i+1
        months.append(row[0])
        # Calulate the total amount of profit/losses
        total_pl = total_pl + int(row[1])

        # Calculate the change in P/L and store as values in a list

        priormonth.append(row[1])
        prior_month_pl= int(priormonth[i])
        #print(f"Prior month PL {prior_month_pl}")

        this_month_pl = int(row[1])
        #print(f"This Month PL {this_month_pl}")

        pl_change = this_month_pl - prior_month_pl
        change_listing.append(pl_change)
        #print(f"Change listing {change_listing}")

    #Calculate Avg Change in P/L
    change_listing.pop(0)
    avg_change = average(change_listing)

    #Calculate Greatest Inc in P/L
    greatest_increase = change_listing.index(max(change_listing))+1
    greatest_increase_month = months[greatest_increase]
    greatest_increase_pl = change_listing[change_listing.index(max(change_listing))]

    #Calculate Greatest Decr in P/L
    greatest_decrease = change_listing.index(min(change_listing))+1
    greatest_decrease_month = months[greatest_decrease]
    greatest_decrease_pl = change_listing[change_listing.index(min(change_listing))]

# Print finacial data
print(f"Financial Analysis")
print("--------------------------------------------")
print(f"Total Months: {month_counter}")
print(f"Total P/L: ${total_pl}")
print(f"Average Change in P/L: ${avg_change}")
print(f"Greatest Increase in P/L {greatest_increase_month} ${greatest_increase_pl}")
print(f"Greatest Decrease in P/L {greatest_decrease_month} ${greatest_decrease_pl}")

# print to a text file
sys.stdout = open('log.txt', 'w')
print(f"Financial Analysis")
print("--------------------------------------------")
print(f"Total Months: {month_counter}")
print(f"Total P/L: ${total_pl}")
print(f"Average Change in P/L: ${avg_change}")
print(f"Greatest Increase in P/L {greatest_increase_month} ${greatest_increase_pl}")
print(f"Greatest Decrease in P/L {greatest_decrease_month} ${greatest_decrease_pl}")


from multiprocessing.connection import wait
import os
import csv
import time
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler

def firebase_to_csv(data_directory, csv_destination_directory):
    data_files = os.listdir(data_directory)

    columns = ['ind','Activity','Accel. X','Accel. Y','Accel. Z','Gyro. X','Gyro. Y','Gyro. Z']

    if not os.path.exists(csv_destination_directory):
        os.makedirs(csv_destination_directory)

    f_csv = open(f'{csv_destination_directory}/data.csv','w')
    writer = csv.writer(f_csv)
    writer.writerow(columns)
    i=1

    for aux_f in data_files:
        f = open(f'{data_directory}/{aux_f}', 'r')
        lines = f.readlines()
        activity = lines[0].split()[0] #split é para remover o \n

        for line in lines[1::]:
            row = []
            row.append(i)
            i+=1
            row.append(activity)
            aux_row = line.split(',')
            for n in aux_row:
                row.append(n.split()[0])

            writer.writerow(row)
        f.close()

    f_csv.close()

def preprocess_csvdata(data_dataframe):

    ###############################################################################################################
    # Now that the csv file win un-processed data has been created, we can read it as a Pandas Dataframe and make some changes to the data
    FRAME_SIZE = 200

    # Remove the index column
    data_dataframe = data_dataframe.drop('ind', axis=1, inplace=False)

    # Split data into X/Input y/output/Target sets
    X = data_dataframe.drop('Activity', axis=1, inplace=False)
    y = pd.DataFrame(data_dataframe['Activity'])

    # Encode Target labels ('corrida', 'andar', etc...)
    y = pd.get_dummies(y)

    # Scale input values (the axis values)

    scaler = MinMaxScaler(feature_range=(-1, 1))
    X = pd.DataFrame(scaler.fit_transform(X=X))

    '''
        By default, each frame has 1000 entries.
        We'll split each one in half, so that each frame/instance contains only 200 entries.
        This has to be done on both input and ouput data sets.
        Each frame will have 200 rows and 6 columns (6 sensor axis).
        Each frame will be associated with a vector of length 4 indicating its corresponding activity/label.
    '''
    print(f"Splitting X and y of shapes {X.shape} and {y.shape}")
    X, y = split_frames(X, y, frame_size=FRAME_SIZE, input_features=6, output_classes=4)

    return X, y

def split_frames(X, y, frame_size, input_features, output_classes):
    model_input = []
    model_output = []

    # Model input are (frame_size x input_features) matrices
    instances = X.shape[0]

    for index in range(0, instances, frame_size):

        # Get 'frame_size' entries of input data
        input_frame_data = X.iloc[index:index + frame_size].to_numpy(copy=True)
        model_input.append(input_frame_data.reshape(
            frame_size, input_features))

        # For each input frame, we have a label/activity to match.
        output_frame_label = y.iloc[[index]].to_numpy(copy=True)
        model_output.append(output_frame_label.reshape(output_classes))

    model_input = np.array(model_input)
    model_output = np.array(model_output)
    return model_input, model_output

def augment_running_data(dataframe: pd.DataFrame):

    original_dataframe = dataframe.copy(deep=True)

    # Get rows of activity 'running'
    running_rows = original_dataframe[original_dataframe['Activity'] == 'correr']

    # Apply symmetry transformation
    transformed_running_rows = running_rows
    transformed_running_rows['Accel. X'] = -transformed_running_rows['Accel. X']
    transformed_running_rows['Accel. Z'] = -transformed_running_rows['Accel. Z']
    transformed_running_rows['Gyro. X'] = -transformed_running_rows['Gyro. X']
    transformed_running_rows['Gyro. Z'] = -transformed_running_rows['Gyro. Z']

    # Insert these transformed rows ino the dataframe
    original_dataframe = pd.concat([original_dataframe, transformed_running_rows], ignore_index=True)
    return original_dataframe


from flask import request
import json

def insert(client):
    productName = request.form['productName']
    productBlob = request.form['productBlob']
    productPrice = request.form['productPrice']
    productDescription = request.form['productDescription']
    productCategory = request.form['productCategory']
    productCarbonFootprint = request.form['productCarbonFootprint']
    productEthicallyFarmed = request.form['productEthicallyFarmed']
    productMainEnergySource = request.form['productMainEnergySource']

    query = ('INSERT INTO `eth-348810.test_dataset.products` (productName, productBlob, productPrice, productDescription, productCategory, productCarbonFootprint, productEthicallyFarmed, productMainEnergySource) VALUES (\'' + productName + '\', \'' + productBlob + '\', \'' + productPrice + '\', \'' + productDescription + '\', \'' + productCategory + '\', \'' + productCarbonFootprint + '\', \'' + productEthicallyFarmed + '\', \'' + productMainEnergySource + '\')')

    query_job = client.query(query)
    rows = query_job.result()
    records = [dict(row) for row in query_job]
    json_obj = json.dumps(str(records))

    return json_obj