#!/usr/bin/env python3
# code by Jon May [jonmay@isi.edu]

import re
import string
import unicodedata as ud

DICT = set()
ANTIDICT = set()

def setdict(thedict):
    global DICT
    DICT = thedict

def setantidict(theantidict):
    global ANTIDICT
    ANTIDICT = theantidict

def haspunc(tok, v=1):
    ''' returns true any character is punc or symbol '''
    if v < 1: return False
    solveset = set(['S', 'P'])
    for x in tok:
        if ud.category(x)[0] in solveset and x != "'":
            return True
    return False

def hasdigit(tok, v=1):
    ''' returns true if any character is a digit '''
    if v < 1: return False
    for x in tok:
        if x.isdigit():
            return True
    return False

def hasnonlatin(tok, v=3):  # v3
    ''' returns true if any character is a letter but not A-Za-z '''
    if v < 3: return False
    az = set(string.ascii_letters)
    for x in tok:
        if ud.category(x)[0] == "L" and x not in az:
            return True
    return False

def laughter(tok, v=3):  # v3
    ''' returns true if it looks like laughing '''
    if v < 3: return False
    return re.match("h[ha]+y?$", tok, flags=re.I) is not None

def dictmatches(tok, v=2):
    ''' returns true if in english dict but not in il6 dict '''
    if v < 2: return False
    return (tok.lower() in DICT and tok.lower() not in ANTIDICT)

def rt(tok, v=1):
    return re.match("rt$", tok, flags=re.I) is not None

def ethiopia(tok, v=3):
    if v < 3: return False
    return re.match("ethiopia", tok, flags=re.I)

ORGLIST = ["ABO", "TPLF", "WBO", "OPDO", "QBO", "FXG", "FDG", "OMN"]

def orgs(tok, v=5):
    if v < 5:
        return False, tok
    for org in ORGLIST:
        if tok.startswith(org):
            return True, org
    return False, tok

def skip(token, v=3):
    ''' heuristics to skip replacement '''
    ret, token = orgs(token, v)
    ret = ret or token.startswith("@") or \
          token.startswith("#") or \
          "http" in token or \
          rt(token, v) or \
          ethiopia(token, v) or \
          haspunc(token, v) or \
          hasdigit(token, v) or \
          dictmatches(token, v) or \
          hasnonlatin(token, v) or \
          laughter(token, v)
    return ret, token


import aiohttp,asyncio
from typing import List
from threading import Thread

class HttpWrapper:
    def __init__(self) -> None:
        pass

    @staticmethod
    async def singleRequestPost(url:str, params:dict=None, timeout:int=10) -> dict:
        sessionTimeout = aiohttp.ClientTimeout(total=timeout)
        async with aiohttp.ClientSession(timeout=sessionTimeout) as session:
            async with session.post(url,json=params) as resp:
                return await resp.json()

    @staticmethod
    async def singleRequestGet(url:str, timeout:int=10) -> dict:
        sessionTimeout = aiohttp.ClientTimeout(total=timeout)
        async with aiohttp.ClientSession(timeout=sessionTimeout) as session:
            async with session.get(url) as resp:
                return await resp.json()

    @staticmethod
    async def singleRequestGetWithMaxTimeout(url:str, maxCountTimeout:int=5, timeout:int=10) -> dict:
        for _ in range(maxCountTimeout):
            try:
                return await HttpWrapper.singleRequestGet(url,timeout)
            except Exception as e:
                print("Request Timeout. Retrying...")
                pass
        return {'err':  "Request timeout"}

    @staticmethod
    async def singleRequestPostWithMaxTimeout(url:str, maxCountTimeout:int=5, params:dict=None, timeout:int=10) -> dict:
        for _ in range(maxCountTimeout):
            try:
                return await HttpWrapper.singleRequestPost(url,params,timeout)
            except Exception as e:
                print("Request Timeout. Retrying...")
                pass
        return {'err':  "Request timeout"}

    @staticmethod
    async def solveRet(res, finished, url, params, timeout, idx):
        try:
            res[idx] = await HttpWrapper.singleRequestPost(url, params, timeout)
            finished[idx] = True
            print(f"Request to {url} finished")
        except Exception as e:
            print("exception =",e)

    @staticmethod
    async def multipleRequestPostWithMaxTimeout(urlList:List[str], paramsList:List[dict], maxCountTimeout:int=5, timeout:int=10) -> dict:

        finished = [False for i in range(len(urlList))]
        ret = [None for i in range(len(urlList))]
        res = [None for i in range(len(urlList))]
        for _ in range(maxCountTimeout):
            for i in range(len(urlList)):
                if not finished[i]:
                    print(f"Request to {urlList[i]} started")
                    ret[i] = Thread(target=asyncio.run,args=[HttpWrapper.solveRet(res, finished, urlList[i], paramsList[i], timeout, i)])
                    ret[i].start()

            for i in range(len(urlList)):
                if not finished[i]:
                    ret[i].join()
        for i in range(len(urlList)):
            if(not finished[i]):
                res[i] = {'err':  "Request timeout"}

            # try:
            #     res = await asyncio.gather(*ret)
            #     print("Successful",res)
            #     return {'result':  res}
            # except Exception as e:
            #     print(f'Exception: {repr(e)}')
            #     for i in range(len(urlList)):
            #         if(not finished[i]):
            #             res[i] = {'err':  "Request timeout"}

        return {'result':  res}


# Write your code here
import random

print("H A N G M A N")

word_choices = ("python", "java", "kotlin", "javascript")
word = random.choice(word_choices)
guess = input("Guess the word: ")

if guess == word:
    print("You survived!")
else:
    print("You are hanged!")


class Solution:
    def getTriggerTime(self, increase: List[List[int]], requirements: List[List[int]]) -> List[int]:
        ans = []
        n = len(increase)
        c,r,h = [0],[0],[0] # 记录每天的c\r\h值
        for ic,ir,ih in increase:
            c.append(c[-1] + ic)
            r.append(r[-1] + ir)
            h.append(h[-1] + ih)
        for rc,rr,rh in requirements:
            posc = bisect.bisect_left(c,rc)
            posr = bisect.bisect_left(r,rr)
            posh = bisect.bisect_left(h,rh)
            pos = max(posc,posr,posh)
            if pos == n + 1:
                ans.append(-1)
            else:
                ans.append(pos)
        return ans


# -*- coding: utf-8 -*-
"""
Aula 4 - grid com knn
"""
import pickle
from sklearn.model_selection import GridSearchCV, train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import classification_report

# Train Test Split
trainX, testX, trainY, testY = train_test_split(dataset, label, test_size=0.20,random_state=42)

#List Hyperparameters that we want to tune.
n_neighbors = list(range(1,30))

grid = GridSearchCV(KNeighborsClassifier(), n_neighbors=n_neighbors , cv=5, refit = True, n_jobs=-1, verbose = 2)

# fitting the model for grid search
grid.fit(trainX, trainY)

# print how our model looks after hyper-parameter tuning
print(grid.best_estimator_)

model = KNeighborsClassifier(leaf_size=1, n_neighbors=11, p=1)
model.fit(trainX, trainY)

# save the model to disk
filename = 'model_knn.sav'
pickle.dump(model, open(filename, 'wb'))

predictions = model.predict(testX)
print(classification_report(testY, predictions))


import pandas as pd
from pandas import DataFrame
import json
import numpy as np
import re
"""
FEATURES :
    TO FIND :
        STRINGS     :
        INTS        :
        DATES       :
        ARRAYS      : INVESTORS, INVESTMENTS, FUNDING,
        FUNCTIONS   : GROWTH

    FOUND :
        STRINGS     : STATUS, COMPANY TYPE , NAMES, IPO STATUS, ACCOUNTS OVERDUE, IND SICs
        INTS        : OFFICERS, RESIGNATIONS
        DATES       : INCORPORATED, DISSOLVED, FOUNDED,YEAR END, FILING DATES
        ARRAYS      : PREV_NAMES,INDUSTRIES, ACCOUNT TYPES, ACCOUNTS
        FUNCTIONS   :
"""

def retrieve_series_values(series):
    temp_list = []
    for temp_array in series:
        if isinstance(temp_array, list):
            for this_value in temp_array:
                if isinstance(this_value, dict):
                    for key in this_value.keys():
                        temp_list.append(key)
                else:
                    temp_list.append(this_value.strip())
    temp_list = list(set(temp_list))
    temp_list.sort()
    return temp_list

def add_col_to_df(df,new_cols,prefix):
    for col in new_cols:
        new_col = prefix+col
        new_col_series = []
        for df_array in df:
            flag = 0
            if isinstance(df_array, list):
                for df_entry in df_array:
                    if isinstance(df_entry, dict):
                        if col in df_entry.keys():
                            flag = 1
                            break
                    else:
                        if df_entry.strip()==col:
                            flag = 1
                            break
            new_col_series.append(flag)
        df_features[new_col] =new_col_series

def normalize_section(s,num,df):
    temp_df = pd.json_normalize(s)
    temp_df = pd.DataFrame(np.diag(temp_df),index=[temp_df.columns][0]).transpose()
    temp_df['index'] = num
    df = pd.concat([df,temp_df])
    return df

def section_sub_table(table):
    df_temp_table = pd.DataFrame()
    temp_rows = table[0]['rows']
    for temp_row in temp_rows:
        df_temp_table= df_temp_table.append(temp_row)
    df_temp_table.reset_index(drop=True, inplace=True)
    temp_columns = table[0]['columnNames']
    for old_col in df_temp_table.columns:
        new_col = temp_columns[old_col]
        df_temp_table.rename(columns={old_col:new_col},  inplace=True)
    return df_temp_table

def rename_accs_table_cols(titles):
    new_titles = list(titles[0:2])
    new_titles.append('notes')
    table_width = len(titles)
    if table_width == 4:
        new_titles.append('current_r')
    elif table_width == 7:
        new_titles.extend(['current_l','current_r','previous_l','previous_r'])
    elif table_width ==5:
        last_col = titles[4]
        if last_col[-1:] =='+':
            new_titles.extend(['current_l','current_r'])
        else:
            new_titles.extend(['current_r','previous_r'])
    return new_titles

def rename_duplicate_headers(headers):
    distinct_headers = set(headers)
    if len(headers) != len(distinct_headers):
        new_headers = []
        for header in headers:
            if header not in new_headers:
                new_headers.append(header)
            else:
                new_header = header+"+"
                new_headers.append(new_header)
        headers = new_headers
    return(headers)

def combine_same_year_cols(df):
    for i in df.columns:
        df[i][df[i].apply(lambda i: True if re.search('^\s*$', str(i)) else False)]=None
    how_many_cols = len(df.columns)
    if how_many_cols== 7:
        cy_r = df['current_r']
        cy_l = df['current_l']
        df['current_r'] = df['current_r'].fillna(df['current_l'])
        df['previous_r'] = df['previous_r'].fillna(df['previous_l'])
        df=df.drop(columns=['current_l','previous_l'])
    elif how_many_cols ==5 and 'current_l'in df.columns:
        df['current_r'] = df['current_r'].fillna(df['current_l'])
        df=df.drop(columns=['current_l'])
    return df

print("\n** ** ** ** ** ** ** ** ** ** ** **")
# reading in the updated company file
with open('my_files/crunchbase_info_tidy.json') as json_file:
    data = json.load(json_file)

df = pd.json_normalize(data)
FIELDS = ["index","name","ni_company_house_match","company_number","company_house_name","company_house_overview.previous_company_names","company_house_overview.Company type","company_house_overview.Company status","company_house_overview.Incorporated on","company_house_people.num_officers","company_house_people.num_resignation","company_house_overview.SIC","company_house_overview.Accounts overdue","company_house_overview.Dissolved on"]
#,"company_house_overview.Dissolved on"

df_features = df[FIELDS]
df_features.rename(columns = {
    "company_house_overview.previous_company_names":"ch_prev_names",
    "company_house_overview.Company type":"ch_type",
    "company_house_overview.Company status":"ch_status",
    "company_house_overview.Incorporated on":"ch_incorporation_date",
    "company_house_overview.Dissolved on":"ch_dissolve_date",
    "company_house_people.num_officers":"ch_tot_officers",
    "company_house_people.num_resignation":"ch_tot_resignations",
    "company_house_overview.SIC":"ch_sic",
    "company_house_overview.Accounts overdue":"ch_overdue",
    #"":"",
    },inplace = True)

#LOAD THE SUBSET OF DATA INTO DATAFRAME
#subsets required from crunchbase data['Overview','Investors'{table},'Funding Rounds'{table}] ]
overview_df = pd.DataFrame()
funding_df = pd.DataFrame()
investment_df = pd.DataFrame()
investors_df = pd.DataFrame()
funding_round_df = pd.DataFrame()
filed_accs_df = pd.DataFrame()
investor_list = []
funding_type_list = []
for company in data:
    #print("\n****   "+ company['name'] + "   *******\n")
    cb_sections = company['sections']
    for cb_section in cb_sections:
        if cb_section['name'] == "Overview":
            overview_df = normalize_section(cb_section['data'],company['index'],overview_df)
        elif cb_section['name'] == "Funding Rounds":
            funding_df = normalize_section(cb_section['fields'],company['index'],funding_df)
        elif cb_section['name']=="Investors":
            investment_df = normalize_section(cb_section['fields'],company['index'],investment_df)

            investor_table = section_sub_table(cb_section['table'])
            inv_list = list(set(list(investor_table['Investor Name'])))
            inv_list = ['invr_'+inv for inv in inv_list]
            inv_flag = [1 for inv in inv_list]
            temp_investor_df = pd.DataFrame(data=inv_flag)
            temp_investor_df = temp_investor_df.transpose()
            temp_investor_df.columns=inv_list
            temp_investor_df['index'] = company['index']
            investors_df = pd.concat([investors_df,temp_investor_df])

            temp_funding_round_list = list(investor_table['Funding Round'])
            funding_round_list= []
            for funding in temp_funding_round_list:
                fund_round = funding.split(" - ")[0].strip()
                if fund_round not in funding_round_list:
                    funding_round_list.append(fund_round)
            funding_round_list = ['fr_'+fr for fr in funding_round_list]
            funding_round_flag = [1 for fr in funding_round_list]
            temp_funding_round_df = pd.DataFrame(data=funding_round_flag)
            temp_funding_round_df = temp_funding_round_df.transpose()
            temp_funding_round_df.columns=funding_round_list
            temp_funding_round_df['index'] = company['index']
            funding_round_df = pd.concat([funding_round_df,temp_funding_round_df])

    ###setting up the filed accounts df
    if 'filing_history' in company:
        all_filing_history = pd.json_normalize(company['filing_history'])
        accs_filed = all_filing_history[(all_filing_history.doc_code=="AA")]
        accs_filed = accs_filed.drop(columns = 'doc_code')
        accs_filed = accs_filed.iloc[::-1].reset_index(drop=True)

        ixbrl_df = pd.json_normalize(company['ixbrl_info'])
        if not(ixbrl_df.empty):
            accs_filed = pd.merge(accs_filed,ixbrl_df,how='outer',left_on='date_filed',right_on='accs_date_filed')
            if 'accs_table.data' in accs_filed:
                accs_filed = accs_filed[['date_filed','filing_desc','made_up_to','accs_table.data']]
            else:
                accs_filed = accs_filed[['date_filed','filing_desc','made_up_to','accs_table']]

            #accs_filed.rename(columns = {'accs_table.data':'table_data'},inplace=True)
        accs_filed = accs_filed.transpose()
        accs_filed = accs_filed.unstack().to_frame().sort_index(level=1).T
        accs_filed[(0,'acc_index')] = company['index']
        filed_accs_df = pd.concat([filed_accs_df,accs_filed])

new_cols=[]
for col in filed_accs_df.columns:
    new_name = "y"+str(col[0])+"_"+col[1]
    new_cols.append(new_name)
filed_accs_df.columns = new_cols

section_df = pd.merge(overview_df,funding_df, how='outer',left_on='index', right_on='index')
section_df = pd.merge(section_df,investment_df, how='outer',left_on='index', right_on='index')

print("\n^^^^ ^^ ^^^^ ^^ ^^^^ ^^ ^^^^\n")

overview_df = section_df.reset_index(drop=True)
SUBFIELDS = ["Also Known As ","Company Type ","Industries ","Founded Date ","Founders ","IPO Status ", "Number of Funding Rounds ","Total Funding Amount ","Number of Investors ","index"]
df_subfeatures = overview_df[SUBFIELDS]

df_features = pd.merge(df_features,df_subfeatures, how='outer',left_on='index', right_on='index')
df_features = pd.merge(df_features,investors_df, how='outer',left_on='index', right_on='index')
df_features = pd.merge(df_features,funding_round_df, how='outer',left_on='index', right_on='index')
df_features = pd.merge(df_features,filed_accs_df, how='outer',left_on='index', right_on='y0_acc_index')

##FIND ALL COLUMNS IN DF THAT HAVE 'accs_table.data' IN THEIR NAME
acc_table_cols =[]
for col in df_features.columns:
    if re.search("accs_table.data",col):
        acc_table_cols.append(col)

#FOR EACH OF THOSE COLUMNS RETRIEVE THE ACCOUNTS TABLE DATA
all_company_accs_df = pd.DataFrame()
for company in data:
    comp_index = company['index']
    comp_name = company['name']
    this_comp_index = data.index(company)
    print(" ><><>< "+str(comp_index) +" ><><><><   "+comp_name+"   ><><><><")
    this_company_accs_df  = pd.DataFrame(data=[comp_index],columns=['comp_index'])
    for acc_col in acc_table_cols:
        #print(">>>>>>>>>>>>>>>>>>>>>>>>")
        this_year = acc_col.split("_")[0]
        acc_col_list = list(df_features[acc_col])
        ##JUST RETURN THE VALUE IN THE SERIES
        acc_table = (acc_col_list[this_comp_index])
        if isinstance(acc_table,list):
            acc_layout = pd.json_normalize(acc_table)
            current_titles = acc_layout.columns
            acc_layout.columns = rename_accs_table_cols(current_titles)
            acc_layout = combine_same_year_cols(acc_layout)
            acc_layout = acc_layout.drop(columns=['index','notes']).dropna(axis=0).reset_index(drop=True)
            acc_layout['Title'] = acc_layout['Title'].str.upper()

            ##LETS TRANSPOSE & UNSTACK
            acc_headers = list(acc_layout['Title'])
            acc_headers = [this_year+"_title_"+header for header in acc_headers]
            #CHECK FOR DUPLIACTE HEADERS
            acc_headers = rename_duplicate_headers(acc_headers)
            acc_values = (acc_layout['current_r'])
            acc_table_df = acc_values.to_frame().T

            acc_table_df.columns = acc_headers
            for acc_header in acc_headers:
                if 'ORDINARY' in acc_header:
                    acc_table_df = acc_table_df.drop(columns=[acc_header])

            acc_table_df['comp_index'] = company['index']
            this_company_accs_df = pd.merge(this_company_accs_df,acc_table_df, how='outer',left_on='comp_index', right_on='comp_index')
        #print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")

    #print(this_company_accs_df)
    #print(all_company_accs_df.info())
    all_company_accs_df = pd.concat([all_company_accs_df,this_company_accs_df])
print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
#reorder all accs_table columns
all_company_accs_df = all_company_accs_df.reindex(sorted(all_company_accs_df.columns), axis=1)
##print(all_company_accs_df.info())
df_features = pd.merge(df_features,all_company_accs_df,how='outer',left_on='index', right_on='comp_index')
#Get a list of column names, check if there is a value, insert result beside column
feature_columns = df_features.columns
for (col_index,feature) in enumerate(feature_columns):
    placing = (col_index*2)+1
    new_col = "present_"+feature
    df_features.insert(placing,new_col,df_features[feature].notnull())

#create sorted list of all SICs
#create a list of all values - then add those values as column names
#adding a prefix to make the df columns easier to search
to_seperate = [('ch_sic','sic_'),('Industries ','ind_')]
for sep in to_seperate:
    col = sep[0]
    prefix = sep[1]
    new_col_list = retrieve_series_values(df_features[col])
    add_col_to_df(df_features[col],new_col_list,prefix)
    df_features = df_features.drop(columns=[col])

##cols to drop
drop_cols = ['present_index','present_comp_index','present_name','present_ni_company_house_match','present_company_house_name','present_ch_sic']
for drop_col in drop_cols:
    df_features=df_features.drop(columns=[drop_col])

df_features.to_excel(r'my_files/features2.xlsx',index = False)


from sys import stdin
# 部分和問題
N, X = map(int, stdin.readline().split())
ab = [list(map(int, stdin.readline().split())) for _ in range(N)]

dp = [[0]*N, [0],N]
print(dp)

def recursive_search(i, sum, target):
    # 終了条件
    if i == N:
        if sum == target:
            return True
        else:
            return False

    # aを選ぶ選択をした場合
    if recursive_search(i+1, sum + ab[i][0], target):
        return True

    # bを選ぶ選択をした場合
    if recursive_search(i+1, sum + ab[i][1], target):
        return True

    return False

if recursive_search(0, 0, X):
    print("Yes")
else:
    print("No")


import sqlalchemy
import pandas as pd
import geopy
import unicodedata

engine = sqlalchemy.create_engine(
    'mysql+pymysql://admin:N6zmVKVW@jobs-intelligence-slovakia.'
    'cluster-c0rbbiliflyo.eu-central-1.rds.amazonaws.com:9906/General_Intelligence_CZ')

conn = engine.connect()

if __name__ == '__main__':
    cities_add = pd.read_csv(r'C:\Users\labus.INTERCONNECTION\Desktop\WebScrapingProject\Geolocation_CZ\cities_cz.csv', sep=',')

    for column in cities_add.columns:
        cities_add[column] = cities_add[column].apply(
            lambda val: unicodedata.normalize('NFKD', str(val)).encode('ascii', 'ignore').decode())

    #import cities_add into Cities_Processed table
    cities_add.to_sql(name='Cities_Processed', con=engine, if_exists='append', index=False)

    print(cities_add.head(10))


#!/usr/bin/env python3
# p3_190503_2031.py
# SQLite exercise following a youTube lesson
# Complete Overview - Creating a Database, Table, and Running Queries
# https://www.youtube.com/watch?v=pd-0G0MigUA

import sqlite3

class Employee:
    """Sample Employee class"""

    def __init__(self, first, last, pay):
        self.first = first
        self.last = last
        self.pay = pay
    @property
    def email(self):
        return '{}.{}@gmail.com'.format(self.first, self.last)
    @property
    def fullname(self):
        return '{} {}'.format(self.first, self.last)
    def __repr__(self):
        return "Employee('{}', '{}', '{})".format(
            self.first, self.last, self.pay
        )

def class_formatting():
    # c.execute("INSERT INTO employees VALUES ('{}', '{}', '{}')".format(
    #    emp1.first, emp1.last, emp1.pay))
    c.execute(
        "INSERT INTO employees VALUES (?, ?, ?)",
        (emp1.first, emp1.last, emp1.pay),
    )
    c.execute(
        "INSERT INTO employees VALUE (:first, :last, :pay)",
        {'first': emp2.first, 'last': emp2.last, 'pay': emp2.pay},
    )
    conn.commit()
    c.execute("SELECT * FROM employees WHERE last=?", ('Schafer',))
    print(c.fetchall())
    c.execute(
        "SELECT * FROM employees WHERE last=:last", {'last': 'Doe'}
    )
    print(c.fetchall())
    conn.commit()

def stream_formatting():
    c.execute(
        "INSERT INTO employees VALUES ('Annie', 'Schafer', 60000)"
    )
    conn.commit()
    c.execute("SELECT * FROM employees WHERE last='Schafer'")
    # c.fetchone()
    # c.fetchmany(5)
    print(c.fetchall())
    conn.commit()

def insert_emp(emp):
    with conn:
        c.execute(
            "INSERT INTO employees VALUES (:first, :last, :pay)",
            {'first': emp.first, 'last': emp.last, 'pay': emp.pay},
        )

def get_emps_by_name(lastname):
    c.execute(
        "SELECT * FROM employees WHERE last=:last", {'last': lastname}
    )
    return c.fetchall()

def update_pay(emp, pay):
    with conn:
        c.execute(
            """UPDATE employees SET pay = :pay
                  WHERE first = :first and last = :last""",
            {'first': emp.first, 'last': emp.last, 'pay': pay},
        )

def remove_emp(emp):
    with conn:
        c.execute(
            "DELETE from employees WHERE first = :first AND last = :last",
            {'first': emp.first, 'last': emp.last},
        )

def main():
    pass

if __name__ == '__main__':
    emp1 = Employee('John', 'Doe', 80000)
    emp2 = Employee('Jane', 'Doe', 90000)
    # conn = sqlite3.connect('files/p3_190503_2031.db')
    conn = sqlite3.connect(':memory:')
    c = conn.cursor()
    c.execute(
        """CREATE TABLE employees (
        first text,
        last text,
        pay integer
    )"""
    )

    # stream_formatting()
    # class_formatting()
    insert_emp(emp1)
    insert_emp(emp2)
    emps = get_emps_by_name('Doe')
    print(emps)
    update_pay(emp2, 95000)
    remove_emp(emp1)
    emps = get_emps_by_name('Doe')
    print(emps)

    conn.close()


from paddlespeech.server.bin.paddlespeech_server import ServerExecutor
import threading
import os

streaming_asr_server = ServerExecutor()
streaming_punc_server = ServerExecutor()

asr_yaml = "conf/ws_conformer_wenetspeech_application.yaml"
punc_yaml = "conf/punc_application.yaml"

if os.getenv('GPU') != None:
    asr_yaml = "conf/ws_conformer_wenetspeech_application_gpu.yaml"
    punc_yaml = "conf/punc_application_gpu.yaml"

asr = threading.Thread(target=streaming_asr_server,
                       kwargs={
                           "config_file": asr_yaml,
                           "log_file": "log/paddlespeech_asr.log"
                       })

punc = threading.Thread(target=streaming_punc_server,
                        kwargs={
                            "config_file": punc_yaml,
                            "log_file": "log/paddlespeech_punc.log"
                        })

if __name__ == "__main__":
    asr.start()
    print("启动ASR服务")
    punc.start()
    print("启动PUNC服务")
    asr.join()
    punc.join()


class Solution:
    def numSquares(self, n: int) -> int:
        squares = set()
        queue = []
        count = 0
        for i in range(0,n+1):
            if int(math.sqrt(i)) * int(math.sqrt(i)) == i:
                squares.add(i)
        squares.pop()
        if n in squares:
            return 1
        for ele in squares:
            if n-ele in squares:
                return count+2
            queue.append(n-ele)
        count = count + 1

        while(len(queue) > 0):
            print(queue)
            for i in range(0, len(queue)):
                ele1 = queue.pop(0)
                for num in squares:
                    if ele1-num > 0:
                        if ele1-num in squares:
                            count = count + 1
                            return count + 1
                        queue.append(ele1-num)
            count = count + 1
        return 0


import numpy as np
import math as mt  # importing libraries
from scipy.linalg import eigh
import cmath as cm
from sympy import symbols, Eq, solve
from math import *
from sympy import *
import sympy as sy
from tkinter.ttk import Button
from tkinter import *
import tkinter as tk
from tkinter import ttk

w = Tk()
tabcontrol = ttk.Notebook(w)

directtab = ttk.Frame(tabcontrol)
rayleightab = ttk.Frame(tabcontrol)
duhameltab = ttk.Frame(tabcontrol)

tabcontrol.add(directtab, text="direct method")
tabcontrol.add(rayleightab, text="rayleigh damping")
tabcontrol.add(duhameltab, text="Duhamel")
tabcontrol.pack(expand=1, fill="both")

def frame1(a, b):
    frame = Frame(directtab, width="480", height="590", background="gray35", bd=2, relief="raised").place(x=a, y=b)

frame = frame1(10, 55)  # for gui(design)

frame3 = Frame(duhameltab, width="900", height="590", background="gray35", bd=2, relief="raised").place(x=55, y=70)

# calculating needed coeficients that are used to find structural response
# A, B, C, D
def coe_C(p0, k, beta, ksi, force_type):
    bet1 = (1 - (beta ** 2))
    bet2 = (2 * ksi * beta)
    pk = (p0 / k)
    if force_type == "sin":
        C = -pk * ((bet2) / ((bet1 ** 2) + (bet2 ** 2)))
    elif force_type == "cosin":
        C = pk * ((bet1) / ((bet1 ** 2) + (bet2 ** 2)))
    else:
        print("  ")
    return C

def coe_D(p0, k, beta, ksi, force_type):
    bet1 = (1 - (beta ** 2))
    bet2 = (2 * ksi * beta)
    pk = (p0 / k)
    if force_type == "sin":
        D = pk * ((bet1) / ((bet1 ** 2) + (bet2 ** 2)))
    elif force_type == "cosin":
        D = -pk * ((bet2) / ((bet1 ** 2) + (bet2 ** 2)))
    else:
        print("  ")
    return D

def coe_A(C, x0):
    A = -C + x0
    return A

def coe_B(xdot0, ksi, wn, A, w, D, wbar):
    B = (xdot0 + (ksi * wn * A) - (w * D)) / wbar
    return B

# forced vibration response
def xt(ksi, wn, t, A, wbar, B, C, D, w):
    e = mt.e
    dt = (e ** (-ksi * wn * t)) * ((A * cm.cos(wbar * t)) + (B * cm.sin(wbar * t))) + (
            (C * cm.cos(w * t)) + (D * cm.sin(w * t)))
    return dt

# now functions for free vibration ...
# B coe for free vibration after turning the engine off

def B_free(xd0, ksi, wn, x0, wbar):
    B = (xd0 + (ksi * wn * x0)) / wbar
    return B

def XT_free(ksi, wn, t, A, wbar, B):
    e = mt.e
    dt = (e ** (-ksi * wn * t)) * ((A * cm.cos(wbar * t)) + (B * cm.sin(wbar * t)))
    return dt

# derivative of forced vibration relation
def XdotT(ksi, wn, T, A, wbar, B, D, C, w):
    e = mt.e
    t = symbols("t")
    dt = (e ** (-ksi * wn * t)) * ((A * sy.cos(wbar * t)) + (B * sy.sin(wbar * t))) + (
            (C * sy.cos(w * t)) + (D * sy.sin(w * t)))
    dt_diff = lambdify(t, dt)
    xdot = dt_diff(T)
    return xdot

def Atriangle(p, w, dt, tfinal, ksi, wn, wbar):
    force_type = forcetype.get()
    f = 0
    if force_type == "sin":
        steps = tfinal / dt
        steps = int(steps)
        for i in range(steps + 1):
            f += (p * sin(w * ((i) * dt))) * (mt.e ** (ksi * wn * dt * (i))) * cos(wbar * dt * (i))
    elif force_type == "cosin":
        steps = tfinal / dt
        steps = int(steps)
        for i in range(steps + 1):
            f += (p * cos(w * ((i) * dt))) * (mt.e ** (ksi * wn * dt * (i))) * cos(wbar * dt * (i))

    AT = f * dt
    return AT

def Btriangle(p, w, dt, tfinal, ksi, wn, wbar):
    force_type = forcetype.get()
    f = 0
    if force_type == "sin":
        steps = tfinal / dt
        steps = int(steps)
        for i in range(steps + 1):
            f += p * mt.sin(w * ((i) * dt)) * (mt.e ** (ksi * wn * dt * (i))) * sin(wbar * dt * (i))
    elif force_type == "cosin":
        steps = tfinal / dt
        steps = int(steps)
        for i in range(steps + 1):
            f += p * mt.cos(w * ((i) * dt)) * (mt.e ** (ksi * wn * dt * (i))) * sin(wbar * dt * (i))

    BT = f * dt
    return BT

def duhammel(ksi, wn, t, m, wbar, AT, BT):
    wn = float(wn)
    ksi = float(ksi)
    t = float(t)
    XT = ((mt.e ** (-ksi * wn * t)) / (m * wbar))*(AT * sin(wbar * t) - BT * cos(wbar * t))
    return XT

m1 = tk.StringVar()
m2 = tk.StringVar()
k1 = tk.StringVar()
k2 = tk.StringVar()
ksi1 = tk.StringVar()
ksi2 = tk.StringVar()
omega1 = tk.StringVar()
omega2 = tk.StringVar()
p01 = tk.StringVar()
p02 = tk.StringVar()
turnof = tk.StringVar()
cutt = tk.StringVar()
x01 = tk.StringVar()
xd01 = tk.StringVar()
x02 = tk.StringVar()
xd02 = tk.StringVar()
forcetype = tk.StringVar()
forcetype.set("cosin")
delta_t = tk.StringVar()

# here inputs or datas will be taken from user

M1 = Entry(directtab, textvariable=m1).place(x="80", y="65")
M2 = Entry(directtab, textvariable=m2).place(x="80", y="90")
K1 = Entry(directtab, textvariable=k1).place(x="80", y="140")
K2 = Entry(directtab, textvariable=k2).place(x="80", y="165")
KSI1 = Entry(directtab, textvariable=ksi1).place(x="80", y="215")
KSI2 = Entry(directtab, textvariable=ksi2).place(x="80", y="240")
OMEGA1 = Entry(directtab, textvariable=omega1).place(x="80", y="290")
OMEGA2 = Entry(directtab, textvariable=omega2).place(x="80", y="315")
P01 = Entry(directtab, textvariable=p01).place(x="270", y="65")
P02 = Entry(directtab, textvariable=p02).place(x="270", y="90")
TURNOF = Entry(directtab, textvariable=turnof).place(x="80", y="365")
CUTT = Entry(directtab, textvariable=cutt, state="disabled", background="white", disabledbackground="gray70")
X01 = Entry(directtab, textvariable=x01).place(x="270", y="215")
XD01 = Entry(directtab, textvariable=xd01).place(x="270", y="240")
X02 = Entry(directtab, textvariable=x02).place(x="270", y="290")
XD02 = Entry(directtab, textvariable=xd02).place(x="270", y="315")
force_type = OptionMenu(directtab, forcetype, "cosin", "sin").place(x=300, y="135")
CUTT.place(x="80", y="390")
X02 = Entry(duhameltab, textvariable=delta_t).place(x="120", y="110")

def activate_entry():
    CUTT.configure(state="normal")

# '''the main function is the code below starts after (def) command and its for structural __
# response by dirext and exact method'''

def dynamic_response():
    M1 = float(m1.get())
    M2 = float(m2.get())
    K1 = float(k1.get())
    K2 = float(k2.get())
    KSI1 = float(ksi1.get())
    KSI2 = float(ksi2.get())
    OMEGA1 = float(omega1.get())
    OMEGA2 = float(omega2.get())
    P01 = float(p01.get())
    P02 = float(p02.get())
    TURNOF = float(turnof.get())
    CUTT = cutt.get()
    X01 = float(x01.get())
    XD01 = float(xd01.get())
    X02 = float(x02.get())
    XD02 = float(xd02.get())

    force_type = forcetype.get()

    # boundary conditions
    BCx0 = [X01, X02]  # distance BC
    BCxd0 = [XD01, XD02]  # speed    BC
    t = TURNOF
    Pt = [P01 * mt.cos(OMEGA1 * t), P02 * mt.cos(OMEGA2 * t)]

    M = np.array([[M1, 0], [0, M2]])
    K = np.array([[K1 + K2, -K2], [-K2, K2]])
    # calculating natural frequencies and mode shape
    V, D = eigh(K, M)
    Wnn = V
    Wn = np.sqrt(V)
    Wn1 = mt.sqrt(Wnn[0])
    Wn2 = mt.sqrt(Wnn[1])

    # making the first row ("1") this is node shape

    D[1] = D[1] * (1 / D[0])
    D[0] = np.ones_like(D[0])

    fi = D
    fit = fi.T

    dot1 = np.dot(fit, M)
    dot2 = np.dot(dot1, fi)
    Mhat = dot2
    Mhat[0][1], Mhat[1][0] = 0, 0

    dot3 = np.dot(fit, K)
    dot4 = np.dot(dot3, fi)
    Khat = dot4
    Khat[0][1], Khat[1][0] = 0, 0
    print("mhat", Mhat)
    print("khat", Khat)
    Chat = np.array([[2 * KSI1 * Wn1 * Mhat[0][0], 0], [0, 2 * KSI2 * Wn2 * Mhat[1][1]]])

    Phat = np.dot(fit, Pt)

    # decoupled equation solve ....
    wn1 = mt.sqrt((Khat[0][0]) / (Mhat[0][0]))
    print("wn1", wn1)
    wn2 = mt.sqrt((Khat[1][1]) / (Mhat[1][1]))
    print("wn2", wn2)
    wbar1 = wn1 * (cm.sqrt(1 - (KSI1 ** 2)))
    print("wbar1", wbar1)
    wbar2 = wn2 * (cm.sqrt(1 - (KSI2 ** 2)))
    print("wbar2", wbar2)
    fiinv = np.linalg.inv(fi)
    BCq0 = np.dot(BCx0, fiinv)
    BCqd0 = np.dot(BCxd0, fiinv)
    phat011 = P01
    phat012 = (fit[0][1]) * P02
    phat021 = P01
    phat022 = (fit[1][1]) * P02
    # the general format of the forces in decoupled equations ... =                             <<<<<<<<<<<
    # p01cos (w1*t)+ phat012 cos(w2*t)   and the next line will be like this....               <<<<<<<<<<<
    # for the first part >>>> forced vibration ...
    C11 = coe_C(p0=phat011, k=Khat[0][0], beta=(OMEGA1 / wn1), ksi=KSI1, force_type=force_type)
    print("C11", C11)
    A11 = coe_A(C=C11, x0=BCq0[0])
    print("A11", A11)
    D11 = coe_D(p0=phat011, k=Khat[0][0], beta=(OMEGA1 / wn1), ksi=KSI1, force_type=force_type)
    print("D11", D11)
    B11 = coe_B(xdot0=BCqd0[0], ksi=KSI1, wn=wn1, A=A11, w=OMEGA1, D=D11, wbar=wbar1)
    print("B11", B11)
    e = mt.e
    q11 = xt(ksi=KSI1, wn=wn1, t=TURNOF, A=A11, wbar=wbar1, B=B11, C=C11, D=D11, w=OMEGA1)
    print("q11", q11)
    # this was q1 acording to the first force in decoupled equations

    C12 = coe_C(p0=phat012, k=Khat[0][0], beta=(OMEGA2 / wn1), ksi=KSI1, force_type=force_type)
    A12 = coe_A(C=C12, x0=BCq0[0])
    D12 = coe_D(p0=phat012, k=Khat[0][0], beta=(OMEGA2 / wn1), ksi=KSI1, force_type=force_type)
    B12 = coe_B(xdot0=BCqd0[0], ksi=KSI1, wn=wn1, A=A12, w=OMEGA2, D=D12, wbar=wbar1)
    e = mt.e
    q12 = xt(ksi=KSI1, wn=wn1, t=TURNOF, A=A12, wbar=wbar1, B=B12, C=C12, D=D12, w=OMEGA2)
    print("q12", q12)
    # and this was for first decoupled equation according to second phat force

    q1 = (q11 + q12)
    print("q1", q1)
    C21 = coe_C(p0=phat011, k=Khat[1][1], beta=(OMEGA1 / wn2), ksi=KSI2, force_type=force_type)
    print("C21", C21)
    A21 = coe_A(C=C21, x0=BCq0[1])
    print("A21", A21)
    D21 = coe_D(p0=phat011, k=Khat[1][1], beta=(OMEGA1 / wn2), ksi=KSI2, force_type=force_type)
    print("D21", D21)
    B21 = coe_B(xdot0=BCqd0[1], ksi=KSI2, wn=wn2, A=A21, w=OMEGA1, D=D21, wbar=wbar2)
    print("B21", B21)
    e = mt.e
    q21 = xt(ksi=KSI2, wn=wn2, t=TURNOF, A=A21, wbar=wbar2, B=B21, C=C21, D=D21, w=OMEGA1)
    print("q21", q21)
    # this was q2 acording to the first force in decoupled equations

    C22 = coe_C(p0=phat022, k=Khat[1][1], beta=(OMEGA2 / wn2), ksi=KSI2, force_type=force_type)
    A22 = coe_A(C=C22, x0=BCq0[1])
    D22 = coe_D(p0=phat022, k=Khat[1][1], beta=(OMEGA2 / wn2), ksi=KSI2, force_type=force_type)
    B22 = coe_B(xdot0=BCqd0[1], ksi=KSI2, wn=wn2, A=A22, w=OMEGA2, D=D22, wbar=wbar2)
    e = mt.e
    q22 = xt(ksi=KSI2, wn=wn2, t=TURNOF, A=A22, wbar=wbar2, B=B22, C=C22, D=D22, w=OMEGA2)

    q2 = q21 + q22
    print("q2", q2)
    q = [q1, q2]
    print("q", q)
    XT = np.dot(fi, q)  # this is XT in forced vibration time....
    print("XT", XT)
    if CUTT != '':
        CUTT = float(CUTT)
        XdotT11 = XdotT(ksi=KSI1, wn=wn1, T=TURNOF, A=A11, wbar=wbar1, B=B11, C=C11, D=D11, w=OMEGA1)
        XdotT12 = XdotT(ksi=KSI1, wn=wn1, T=TURNOF, A=A12, wbar=wbar1, B=B12, C=C12, D=D12, w=OMEGA2)
        XdotT1 = XdotT11 + XdotT12
        print("xdot1",XdotT1 )
        XdotT21 = XdotT(ksi=KSI2, wn=wn2, T=TURNOF, A=A21, wbar=wbar2, B=B21, C=C21, D=D21, w=OMEGA1)
        XdotT22 = XdotT(ksi=KSI2, wn=wn2, T=TURNOF, A=A22, wbar=wbar2, B=B22, C=C22, D=D22, w=OMEGA2)
        XdotT2 = XdotT21 + XdotT22
        print("xdot2", XdotT2)
        BF11 = B_free(xd0=XdotT1, ksi=KSI1, wn=wn1, x0=q1, wbar=wbar1)
        BF22 = B_free(xd0=XdotT2, ksi=KSI2, wn=wn2, x0=q2, wbar=wbar2)
        print("BF1", BF11)
        print("BF2", BF22)
        AF11 = q1
        AF22 = q2
        print("a1", AF11)
        print("a2", AF22)
        qf1 = XT_free(ksi=KSI1, wn=wn1, t=(CUTT - TURNOF), A=AF11, wbar=wbar1, B=BF11)
        qf2 = XT_free(ksi=KSI2, wn=wn2, t=(CUTT - TURNOF), A=AF22, wbar=wbar2, B=BF22)
        print("qf1", qf1)
        print("qf2", qf2)
        QF = [qf1, qf2]
        print("qf", QF)

        XF = np.dot(fi, QF)
        print("XF", XF)
    else:
        XF = ['', '']

    return XT, XF

def solveduhamel():
    M1 = float(m1.get())
    M2 = float(m2.get())
    K1 = float(k1.get())
    K2 = float(k2.get())
    KSI1 = float(ksi1.get())
    KSI2 = float(ksi2.get())
    OMEGA1 = float(omega1.get())
    OMEGA2 = float(omega2.get())
    P01 = float(p01.get())
    P02 = float(p02.get())
    TURNOF = float(turnof.get())
    CUTT = cutt.get()
    X01 = float(x01.get())
    XD01 = float(xd01.get())
    X02 = float(x02.get())
    XD02 = float(xd02.get())
    Delta_t = float(delta_t.get())

    force_type = forcetype.get()

    # boundary conditions
    BCx0 = [X01, X02]  # distance BC
    BCxd0 = [XD01, XD02]  # speed    BC
    t = TURNOF
    Pt = [P01 * mt.cos(OMEGA1 * t), P02 * mt.cos(OMEGA2 * t)]

    M = np.array([[M1, 0], [0, M2]])
    K = np.array([[K1 + K2, -K2], [-K2, K2]])
    # calculating natural frequencies and mode shape
    V, D = eigh(K, M)
    Wnn = V
    Wn = np.sqrt(V)
    Wn1 = mt.sqrt(Wnn[0])
    Wn2 = mt.sqrt(Wnn[1])

    # making the first row ("1") this is node shape

    D[1] = D[1] * (1 / D[0])
    D[0] = np.ones_like(D[0])

    fi = D
    fit = fi.T

    dot1 = np.dot(fit, M)
    dot2 = np.dot(dot1, fi)
    Mhat = dot2
    Mhat[0][1], Mhat[1][0] = 0, 0

    dot3 = np.dot(fit, K)
    dot4 = np.dot(dot3, fi)
    Khat = dot4
    Khat[0][1], Khat[1][0] = 0, 0

    Chat = np.array([[2 * KSI1 * Wn1 * Mhat[0][0], 0], [0, 2 * KSI2 * Wn2 * Mhat[1][1]]])

    Phat = np.dot(fit, Pt)

    # decoupled equation solve ....
    wn1 = mt.sqrt((Khat[0][0]) / (Mhat[0][0]))
    wn2 = mt.sqrt((Khat[1][1]) / (Mhat[1][1]))
    wbar1 = wn1 * (cm.sqrt(1 - (KSI1 ** 2)))
    wbar2 = wn2 * (cm.sqrt(1 - (KSI2 ** 2)))
    fiinv = np.linalg.inv(fi)
    BCq0 = np.dot(BCx0, fiinv)
    BCqd0 = np.dot(BCxd0, fiinv)
    phat011 = P01
    phat012 = (fit[0][1]) * P02
    phat021 = P01
    phat022 = (fit[1][1]) * P02
    A11 = Atriangle(p=phat011, w=OMEGA1, dt=Delta_t, tfinal=TURNOF, ksi=KSI1, wn=wn1, wbar=wbar1)
    B11 = Btriangle(p=phat011, w=OMEGA1, dt=Delta_t, tfinal=TURNOF, ksi=KSI1, wn=wn1, wbar=wbar1)
    q11 = duhammel(ksi=KSI1, wn=wn1, t=TURNOF, m=Mhat[0][0], wbar=wbar1, AT=A11, BT=B11)

    A12 = Atriangle(p=phat012, w=OMEGA2, dt=Delta_t, tfinal=TURNOF, ksi=KSI1, wn=wn1, wbar=wbar1)
    B12 = Btriangle(p=phat012, w=OMEGA2, dt=Delta_t, tfinal=TURNOF, ksi=KSI1, wn=wn1, wbar=wbar1)
    q12 = duhammel(ksi=KSI1, wn=wn1, t=TURNOF, m=Mhat[0][0], wbar=wbar1, AT=A12, BT=B12)

    q1 = q11 + q12

    A21 = Atriangle(p=phat011, w=OMEGA1, dt=Delta_t, tfinal=TURNOF, ksi=KSI2, wn=wn2, wbar=wbar2)
    B21 = Btriangle(p=phat011, w=OMEGA1, dt=Delta_t, tfinal=TURNOF, ksi=KSI2, wn=wn2, wbar=wbar2)
    q21 = duhammel(ksi=KSI2, wn=wn2, t=TURNOF, m=Mhat[1][1], wbar=wbar2, AT=A21, BT=B21)

    A22 = Atriangle(p=phat022, w=OMEGA2, dt=Delta_t, tfinal=TURNOF, ksi=KSI2, wn=wn2, wbar=wbar2)
    B22 = Btriangle(p=phat022, w=OMEGA2, dt=Delta_t, tfinal=TURNOF, ksi=KSI2, wn=wn2, wbar=wbar2)
    q22 = duhammel(ksi=KSI2, wn=wn2, t=TURNOF, m=Mhat[1][1], wbar=wbar2, AT=A22, BT=B22)
    q2 = q21 + q22
    q = [q1, q2]
    XT = np.dot(fi, q)

    # if CUTT != '':
    #     qf11 = duhammel(ksi=KSI1, wn=wn1, t=CUTT, m=Mhat[0][0], wbar=wbar1, AT=A11, BT=B11)
    #     qf12 = duhammel(ksi=KSI1, wn=wn1, t=CUTT, m=Mhat[0][0], wbar=wbar1, AT=A12, BT=B12)
    #     qf1 = qf11 + qf12
    #
    #     qf21 = duhammel(ksi=KSI2, wn=wn2, t=CUTT, m=Mhat[1][1], wbar=wbar2, AT=A21, BT=B21)
    #     qf22 = duhammel(ksi=KSI2, wn=wn2, t=CUTT, m=Mhat[1][1], wbar=wbar2, AT=A22, BT=B22)
    #     qf2 = qf21 + qf22
    #     qf = [qf1, qf2]
    #     XTF = np.dot(fi, qf)
    #
    # else:
    #     XTF = ["", ""]

    return XT

def outputDu():
    x = solveduhamel()
    TURNOF = float(turnof.get())
    ex1 = x[0]
    ex2 = x[1]
    story1 = Label(duhameltab, text=f"first story displacement : \n  {x} in time :{TURNOF} s", bg="gray25",
                   fg="ghost white",
                   font="arvo 10 bold italic", relief="sunken").place(x="200", y="200")
    story2 = Label(duhameltab, text=f"second story displacement : \n  {x} in time :{TURNOF} s", bg="gray25",
                   fg="ghost white",
                   font="arvo 10 bold italic", relief="sunken").place(x="200", y="300")

# the function is for rayleigh damping that can be used to get the information such as alpha, beta, C and modal C

def rayleigh_damping():
    M1 = float(m1.get())
    M2 = float(m2.get())
    K1 = float(k1.get())
    K2 = float(k2.get())
    KSI1 = float(ksi1.get())
    KSI2 = float(ksi2.get())
    M = np.array([[M1, 0], [0, M2]])
    K = np.array([[K1 + K2, -K2], [-K2, K2]])
    # calculating natural frequencies and mode shape
    V, D = eigh(K, M)
    Wnn = V
    Wn = np.sqrt(V)
    Wn1 = mt.sqrt(Wnn[0])
    Wn2 = mt.sqrt(Wnn[1])

    D[1] = D[1] * (1 / D[0])
    D[0] = np.ones_like(D[0])

    fi = D
    fit = fi.T

    alpha, beta = symbols("alpha, beta")
    eq1 = Eq((alpha + (beta * (Wn1 ** 2))), (2 * KSI1 * Wn1))
    eq2 = Eq((alpha + (beta * (Wn2 ** 2))), (2 * KSI2 * Wn2))
    alphabeta = solve((eq1, eq2), (alpha, beta))
    alpha = float(alphabeta[alpha])
    beta = float(alphabeta[beta])
    CR = np.dot(alpha, M) + np.dot(beta, K)
    dot1 = np.dot(fit, CR)
    dot2 = np.dot(dot1, fi)
    CHR = dot2
    CHR[0][1] = 0
    CHR[1][0] = 0
    l = Label(rayleightab,
              text=f"alpha ={alpha}\n\nbeta ={beta}\n\n\ndamping matrix = \n{CR}\n\n\nmodal damping matrix =\n{CHR}\n\n\nC = (alpha x M)+(beta x K)",
              background="gray35", bd=2, relief="raised", font="arvo 16 bold italic", fg="white", justify="left")
    l.place(x=10, y=150)

def data1():
    M1 = float(m1.get())
    M2 = float(m2.get())
    K1 = float(k1.get())
    K2 = float(k2.get())
    M = np.array([[M1, 0], [0, M2]])
    K = np.array([[K1 + K2, -K2], [-K2, K2]])
    # calculating natural frequencies and mode shape
    V, D = eigh(K, M)
    Wnn = V
    Wn = np.sqrt(V)
    Wn1 = mt.sqrt(Wnn[0])
    Wn2 = mt.sqrt(Wnn[1])
    D[1] = D[1] * (1 / D[0])
    D[0] = np.ones_like(D[0])
    fi = D
    l = Label(directtab, text=f"natural frequency (Wn1) : \n  {Wn1} ", bg="gray25",
              fg="ghost white",
              font="arvo 10 bold italic", relief="sunken").place(x="520", y="300")
    l2 = Label(directtab, text=f"natural frequency (Wn2) : \n  {Wn2} ", bg="gray25",
               fg="ghost white",
               font="arvo 10 bold italic", relief="sunken").place(x="520", y="350")
    l3 = Label(directtab, text=f" modal shape matrix : \n  {fi} ", bg="gray25",
               fg="ghost white",
               font="arvo 10 bold italic", relief="sunken").place(x="520", y="400")

# this function is for outputs
def output1():
    TURNOF = float(turnof.get())
    x = dynamic_response()
    ex1 = x[0][0]
    ex2 = x[0][1]
    exf1 = x[1][0]
    exf2 = x[1][1]
    story1 = Label(directtab, text=f"first story displacement : \n  {ex1} in time :{TURNOF} s", bg="gray25",
                   fg="ghost white",
                   font="arvo 10 bold italic", relief="sunken").place(x="520", y="200")
    story2 = Label(directtab, text=f"second story displacement : \n {ex2} in time :{TURNOF} s", bg="gray25",
                   fg="ghost white",
                   font="arvo 10 bold italic", relief="sunken").place(x="520", y="250")
    if exf1 != '':
        story1f = Label(directtab, text=f"first story displacement :\n  {exf1} in cut time (free vibration)",
                        bg="gray25",
                        fg="ghost white",
                        font="arvo 10 bold italic", relief="sunken").place(x="520", y="70")
        story2f = Label(directtab, text=f"second story displacement : \n    {exf2} in cut time (free vibration) ",
                        bg="gray25",
                        fg="ghost white",
                        font="arvo 10 bold italic", relief="sunken").place(x="520", y="120")
    else:
        aaa = None

# codes below are for window design

w.geometry("1000x720")
w.title("dynamics of structures/ hossein bagheri ")
l = Label(directtab, text="dynamic response of 2dof---force type:p0sin(wt)\ngithub: hosseinbagheri0110 ",
          fg="ghost white",
          bg="gray20"
          , font="arvo 13 bold italic", bd=3, relief="raised", width="99").place(x=2, y=7)
lr = Label(rayleightab, text="dynamic response of 2dof---force type:p0sin(wt)\ngithub: hosseinbagheri0110 ",
           fg="ghost white",
           bg="gray20"
           , font="arvo 13 bold italic", bd=3, relief="raised", width="99").place(x=2, y=7)
lD = Label(duhameltab, text="dynamic response of 2dof---force type:p0sin(wt)\ngithub: hosseinbagheri0110 ",
           fg="ghost white",
           bg="gray20"
           , font="arvo 13 bold italic", bd=3, relief="raised", width="99").place(x=2, y=7)

l2 = Label(directtab, text="email : hosseinbagheri0110@gmail.com", fg="ghost white",
           bg="gray20"
           , font="comic 12 bold italic", bd=3, relief="raised", width="30").place(x=670, y=660)

w.configure(background="gray45")
w.resizable(width=False, height=False)
# button1 = tk.Button(text="submit", width=25, activeforeground="white", activebackground="black", bg="gray25",
#                     fg="ghost white", font="arvo 16 bold italic", command=w.destroy).place(x=75, y=650)

lablem1 = Label(directtab, text="M1    ", background="gray27", fg="white", font="arvo 10 bold italic", bd=2,
                relief="sunken").place(x="25", y="65")
lablem2 = Label(directtab, text="M2    ", fg="white", font="arvo 10 bold italic", bd=2, relief="sunken",
                background="gray27").place(x="25", y="90")
lablek1 = Label(directtab, text="k1    ", fg="white", font="arvo 10 bold italic", bd=2, relief="sunken",
                background="gray27").place(x="25", y="140")
lablek2 = Label(directtab, text="k2    ", fg="white", font="arvo 10 bold italic", bd=2, relief="sunken",
                background="gray27").place(x="25", y="165")
lableksi1 = Label(directtab, text="ksi 1 ", fg="white", font="arvo 10 bold italic", bd=2, relief="sunken",
                  background="gray27").place(x="25", y="215")
lableksi2 = Label(directtab, text="ksi 2", fg="white", font="arvo 10 bold italic", bd=2, relief="sunken",
                  background="gray27").place(x="25", y="240")
lablew1 = Label(directtab, text="omega1 ", fg="white", font="arvo 10 bold italic", bd=2, relief="sunken",
                background="gray27").place(x="19", y="290")
lablew2 = Label(directtab, text="omega2 ", fg="white", font="arvo 10 bold italic", bd=2, relief="sunken",
                background="gray27").place(x="19", y="315")
lablep01 = Label(directtab, text="p(0)1 ", fg="white", font="arvo 10 bold italic", bd=2, relief="sunken",
                 background="gray27").place(x="230", y="65")
lablep02 = Label(directtab, text="p(0)2 ", fg="white", font="arvo 10 bold italic", bd=2, relief="sunken",
                 background="gray27").place(x="230", y="90")
lableofft = Label(directtab, text="Time 1", fg="white", font="arvo 10 bold italic", bd=2, relief="sunken",
                  background="gray27").place(x="15", y="365")
lablecutt = Label(directtab, text="Time 2", fg="white", font="arvo 10 bold italic", bd=2, relief="sunken",
                  background="gray27").place(x="15", y="390")
lablex01 = Label(directtab, text="x01", fg="white", font="arvo 10 bold italic", bd=2, relief="sunken",
                 background="gray27").place(x="230", y="215")
lablexd01 = Label(directtab, text="x.01", fg="white", font="arvo 10 bold italic", bd=2, relief="sunken",
                  background="gray27").place(x="230", y="240")
labletype = Label(directtab, text="force type", fg="white", font="arvo 10 bold italic", bd=2, relief="sunken",
                  background="gray27").place(x="230", y="140")
lablex02 = Label(directtab, text="x.02", fg="white", font="arvo 10 bold italic", bd=2, relief="sunken",
                 background="gray27").place(x="230", y="290")
lablexd02 = Label(directtab, text="x.02", fg="white", font="arvo 10 bold italic", bd=2, relief="sunken",
                  background="gray27").place(x="230", y="315")
buttonof = Button(directtab, text="engine has turned off and\nthere is free vibration", fg="white",
                  font="arvo 8 bold italic", bd=2, relief="raised",
                  background="gray27", cursor="hand2", command=activate_entry).place(x="220", y="368")

labledelta = Label(duhameltab, text="delta t", fg="white", font="arvo 10 bold italic", bd=2, relief="sunken",
                  background="gray27").place(x="70", y="110")

frame2 = frame1(510, 55)
button1 = tk.Button(directtab, text="submit", width=25, activeforeground="white", activebackground="black", bg="gray25",
                    fg="ghost white", font="arvo 16 bold italic", cursor="hand2",
                    command=lambda: [output1(), data1()]).place(x=75, y=650)

button2 = tk.Button(rayleightab, text="calculate Rayleigh Damping according to direct method tab data", width=60,
                    activeforeground="white", activebackground="black", bg="gray25",
                    fg="ghost white", font="arvo 16 bold italic", cursor="hand2", command=rayleigh_damping).place(x=75,
                                                                                y=60)
button3 = tk.Button(duhameltab, text="calculate X(t) with duhamel", width=60,
                    activeforeground="white", activebackground="black", bg="gray25",
                    fg="ghost white", font="arvo 16 bold italic", cursor="hand2", command=outputDu).place(x=75,
                                                                                y=60)
w.mainloop()


'''
Usage:
    mRNA_pipe_v1.py [--project_dir=<project_dir>]
                    (--project_name=<project_name>)
                    (--fq_dir=<fq_dir>)
                    (--sample_inf=<sample_inf>)
                    (--transcript=<transcript_fa_file>)
                    (--gene2tr=<gene_transcript_id_map>)
                    (--goseq_anno=<goseq_annotation>)
                    (--topgo_anno <togo_annotation>)
                    (--gene_length=<gene_length_file>)
                    (--kegg_abbr=<species_kegg_abbr>)
                    (--kegg_blast=<kegg_blast_annotation>)

Options:
    --help -h                               show this information
    --project_dir=<project_dir>             project directory
    --project_name=<project_name>           project name
    --fq_dir=<fq_dir>                       fastq directory
    --sample_inf=<sample_inf>               sample information: group<tab>sample
    --transcript=<transcript_fa_file>       transcript fasta file
    --gene2tr=<gene_transcript_id_map>      file map transcript id to gene id
    --goseq_anno=<goseq_annotation>         gene go annotation:(format)gene_id,go_id
    --topgo_anno=<topgo_annotation>         gene go annotation:(format)go_id<tab>gene_id<tab>gene_id ...
    --gene_length=<gene_length_file>        gene length file
    --kegg_abbr=<species_kegg_abbr>         species kegg abbr
    --kegg_blast=<kegg_blast_annotation>    kegg blast out
'''

import ConfigParser
from python_tools import circ_mkdir_unix
from docopt import docopt
import time
from os import system
from os import path
from os import listdir
import subprocess
from collections import OrderedDict

ConfigFile = '/home/public/scripts/RNAseq/python/configure.ini'
conf = ConfigParser.ConfigParser()
conf.read(ConfigFile)
fastqc_pipe = conf.get('server34', 'fastqc_pipe')
rseqc_pipe = conf.get('server34', 'rseqc_pipe')
quant_pipe = conf.get('server34', 'quant_pipe')
enrich_pipe = conf.get('server34', 'enrich_pipe')

PIPE_THREAD = 2

def run_cmd(cmd):
    p = subprocess.Popen(cmd, shell=False, universal_newlines=True, stdout=subprocess.PIPE)
    ret_code = p.wait()
    output = p.communicate()[0]
    return output

def add_pipe_message(monitor_dir, message = 'pipe_begin'):
    system('touch {0}/{1}_{2}'.format(monitor_dir, message, time.strftime("%Y%m%d_%H%M%S", time.localtime())))
    return 0

def cp_dir_with_ignore(from_dir, to_dir):
    ignore_file = path.join(from_dir, '.ignore')
    system("cp -r {0} {1}".format(from_dir, to_dir))
    with open(ignore_file) as ignore_file_inf:
        for eachline in ignore_file_inf:
            eachline = eachline.strip()
            each_new_ignore_file = path.join(to_dir, eachline)
            system("rm -rf {}".format(each_new_ignore_file))

class Workflow:
    def __init__(self):
        self.work_schedule_dict = {'fastqc':self.run_fastqc, 'quant':self.run_quant, 'enrich':self.run_enrich}
        self.work_schedule_list = ['fastqc', 'quant', 'enrich']
        self.work_status = []
        self.work_dir = []
        self.project_dir = None
        self.project_name = None
        self.monitor_dir = None
        self.fq_dir = None
        self.sample_inf = None
        self.bam_dir = None
        self.bed_file = None
        self.transcript = None
        self.gene2tr = None
        self.goseq_anno = None
        self.topgo_anno = None
        self.gene_length = None
        self.kegg_abbr = None
        self.kegg_blast = None

    def run_fastqc(self):
        add_pipe_message(self.monitor_dir, 'fastqc_start')
        fastqc_dir = path.join(self.project_dir, 'fastqc')

        run_cmd(['python',
        fastqc_pipe,
        'fastqc_collection',
        '--SampleInf',
        self.sample_inf,
        '--OutDir',
        fastqc_dir,
        '--CleanDir',
        self.fq_dir,
        '--workers',
        '{}'.format(PIPE_THREAD)])

        return fastqc_dir

    def run_quant(self):
        add_pipe_message(self.monitor_dir, 'quant_start')
        quant_dir = path.join(self.project_dir, 'quantification')

        run_cmd(['python',
        quant_pipe,
        'quant_collection',
        '--SampleInf',
        self.sample_inf,
        '--CleanDir',
        self.fq_dir,
        '--Transcript',
        self.transcript,
        '--Gene2Tr',
        self.gene2tr,
        '--OutDir',
        quant_dir,
        '--workers',
        '{}'.format(PIPE_THREAD)])

        return quant_dir

    def run_enrich(self):
        add_pipe_message(self.monitor_dir, 'enrich_start')
        enrich_dir = path.join(self.project_dir, 'enrichment')

        run_cmd(['python',
        enrich_pipe,
        'enrichment_collection',
        '--QuantDir',
        '{}/quantification'.format(self.project_dir),
        '--GoseqAnno',
        self.goseq_anno,
        '--TopgoAnno',
        self.topgo_anno,
        '--GeneLen',
        self.gene_length,
        '--KEGGAbbr',
        self.kegg_abbr,
        '--KEGGBlast',
        self.kegg_blast,
        '--OutDir',
        enrich_dir,
        '--workers',
        '{}'.format(PIPE_THREAD)])

        return enrich_dir

    def run_result(self):
        add_pipe_message(self.monitor_dir, 'generate_result_start')
        result_dir = path.join(self.project_dir, self.project_name)
        if path.exists(result_dir):
            system('rm -rf {}'.format(result_dir))
        circ_mkdir_unix(result_dir)
        for each_dir in self.work_dir:
            each_dir_ignore = path.join(each_dir, '.ignore')
            each_dir_name = path.basename(each_dir)
            each_dir_result = path.join(result_dir, each_dir_name)
            cp_dir_with_ignore(each_dir, each_dir_result)

    def run_pipe(self):
        for each_module in self.work_schedule_list:
            if each_module not in self.work_status:
                self.work_dir.append(self.work_schedule_dict[each_module]())
        self.run_result()
        add_pipe_message(self.monitor_dir, 'project_finished')

if __name__ == '__main__':
    ## read arguments
    arguments = docopt(__doc__, version = 'mRNA pipeline v1')
    if not arguments['--project_dir']:
        project_dir = path.abspath('.')
    else:
        project_dir = arguments['--project_dir']
    monitor_dir = path.join(project_dir, 'monitor')
    my_workflow = Workflow()
    my_workflow.project_dir = project_dir
    my_workflow.project_name = arguments['--project_name']
    my_workflow.monitor_dir = monitor_dir
    my_workflow.sample_inf = arguments['--sample_inf']
    my_workflow.fq_dir = arguments['--fq_dir']
    my_workflow.transcript = arguments['--transcript']
    my_workflow.gene2tr = arguments['--gene2tr']
    my_workflow.goseq_anno = arguments['--goseq_anno']
    my_workflow.topgo_anno = arguments['--topgo_anno']
    my_workflow.gene_length = arguments['--gene_length']
    my_workflow.kegg_abbr = arguments['--kegg_abbr']
    my_workflow.kegg_blast = arguments['--kegg_blast']
    map(circ_mkdir_unix, [project_dir, monitor_dir])

    ## running pipeline
    my_workflow.run_pipe()


#dictionary sorting example- sort by a ranking (by key or value)

myDict={'Jack':5,'Bill':14,'Kat':3,'Jess':33,'Alex':4}
myDict2={'Jack':5,'Bill':14,'Kat':3,'Jess':33,'Alex':4}

def sortByKey():
    sortedByKeyDict=sorted(myDict.items(),key=lambda t: t[0])
    print (sortedByKeyDict)    #note array order is displayed

def sortByValue():
    sortedByValueDict=sorted(myDict.items(),key=lambda t: t[1])
    print (sortedByValueDict)  #note array order is displayed

sortByKey();

sortByValue();


from . import S3
from .s3_bucket_error import NumberRandomCharsException, NumberCharsBucketNameException
from botocore.client import ClientError
import uuid
import os

class S3Object(S3):

    @staticmethod
    def create_name(object_suffix, num_random_chars=6):
        if num_random_chars < NumberRandomCharsException.min_num_chars or \
           num_random_chars > NumberRandomCharsException.max_num_chars:

            raise NumberRandomCharsException

        return ''.join([str(uuid.uuid4()).replace('-', '')[:num_random_chars], '-', object_suffix])

    @staticmethod
    def get_object_key(file_name, with_random_prefix=True, num_random_chars=6):
        if with_random_prefix:
            return S3Object.create_name(file_name, num_random_chars)
        else:
            return file_name

    @classmethod
    def upload(cls, bucket_name, file_dict, with_random_prefix=True, num_random_chars=6):
        if 'file_path' in file_dict:
            _, file_name = os.path.split(file_dict['file_path'])
        else:
            file_name = file_dict['file_name']

        object_key = cls.get_object_key(file_name, with_random_prefix, num_random_chars)

        if 'file_path' in file_dict:
            cls.resource.Object(bucket_name, object_key).upload_file(file_dict['file_path'])

        elif 'file_bytes' in file_dict:
            cls.resource.meta.client.put_object(Bucket=bucket_name, Key=object_key, Body=file_dict['file_bytes'])

        elif 'file_obj' in file_dict:
            cls.resource.meta.client.upload_fileobj(file_dict['file_obj'], bucket_name, object_key)

        else:
            raise NotImplementedError('The "file_dict" parameters should include file_obj or file_bytes or file_path')

        return object_key

    @classmethod
    def download(cls, bucket_name, object_key, file_dict=dict()):
        if 'file_path' in file_dict:
            return cls.resource.Object(bucket_name, object_key).download_file(file_dict['file_path'])

        elif 'file_obj' in file_dict:
            return cls.resource.meta.client.download_fileobj(bucket_name, object_key, file_dict['file_obj'])

        elif len(file_dict) == 0:
            # Get the file bytes
            return cls.resource.meta.client.get_object(Bucket=bucket_name, Key=object_key)['Body'].read()

        else:
            raise NotImplementedError('The "file_dict" parameters should include file_obj, file_path, or empty dict to return the file bytes')

    @classmethod
    def delete(cls, bucket_name, object_key):
        return cls.resource.meta.client.delete_object(Bucket=bucket_name, Key=object_key)

    @classmethod
    def list_all(cls, bucket_name, as_generator):
        bucket = cls.resource.Bucket(bucket_name)
        objects = bucket.objects.all()

        if as_generator:
            return objects

        return list(objects)

    @classmethod
    def list_all_keys(cls, bucket_name, as_generator):
        objects = cls.list_all(bucket_name, as_generator=True)
        objects_keys = (obj.key for obj in objects)

        if as_generator:
            return objects_keys

        return list(objects_keys)

    @classmethod
    def is_available(cls, bucket_name, object_key):
        try:
            cls.resource.Object(bucket_name, object_key).load()

        except ClientError as e:
            if e.response['Error']['Code'] == '404':
                return False

        else:
            return True

    @classmethod
    def num_objects(cls, bucket_name):
        return len(cls.list_all(bucket_name, False))