import os
import librosa
from data_tools import audioToNP
from data_tools import mixRandomNoise, npToSpectrogram
import numpy as np
import soundfile as sf

# creating randomly blended noisy inputs

def create_data(noise_dir, voice_dir, path_save_spectrogram, sample_rate,
                min_duration, frame_length, hop_length_frame, hop_length_frame_noise, nb_samples, n_fft, hop_length_fft):

    list_noise_files = os.listdir(noise_dir)
    list_voice_files = os.listdir(voice_dir)

    # Extracting noise and voice from folder and convert to numpy
    noise = audioToNP(noise_dir, list_noise_files, sample_rate,
                      frame_length, hop_length_frame_noise, min_duration)
    print("NOISE DONE")
    voice = audioToNP(voice_dir, list_voice_files,
                      sample_rate, frame_length, hop_length_frame, min_duration)
    print("VOICE DONE")

    # Blend some clean voices with random selected noises (and a random level of noise)
    prod_voice, prod_noise, prod_noisy_voice = mixRandomNoise(
        voice, noise, nb_samples, frame_length)

    # Squared spectrogram dimensions
    dim_square_spec = int(n_fft / 2) + 1

    # Create Amplitude and phase of the sounds
    m_amp_db_voice,  m_pha_voice = npToSpectrogram(
        prod_voice, dim_square_spec, n_fft, hop_length_fft)
    m_amp_db_noisy_voice,  m_pha_noisy_voice = npToSpectrogram(
        prod_noisy_voice, dim_square_spec, n_fft, hop_length_fft)

    # Save timeseries, spectrogram useful for training process
    np.save(path_save_spectrogram + 'voice_amp_db', m_amp_db_voice)
    np.save(path_save_spectrogram + 'noisy_voice_amp_db', m_amp_db_noisy_voice)


#multiple if statements
#in if-elif-else if first condition is true,we execute that and skip remaining conditions
#But what if we need to check multiple conditions even if previous conditions were true
#here we use multiple if statements
####<<<PROGRAM>>>>####
'''
Pizza order exercise:
Input size(S,M,L),if they want pepperoni and if they want extra cheese
Work out the final bill based on following prices:
Small pizza:$15
Medium Pizza:$20
Large pizza:$25
pepperoni for small pizza: +$2
Pepperoni for medium or large pizza: +$3
extra cheese for any size pizza: +$1
'''
print("welcome to Python Pizza Deliveries!")
size=input("What size pizza do you want?")
add_pep=input("Do you want pepperoni?Y or N")
extra_cheese=input("Do you want extra cheese?Y or N")

bill=0;
#for size we use if-elif because if one condition is true,we don't need to check remaining conditions
if(size=="S"):
    bill+=15
elif(size=='M'):
    bill+=20
else:
    bill+=25
#but here for pepperoni and extra cheese,we use if because we always want to ensure we check these conditions
if add_pep =="Y":
    if(size=="S"):
        bill+=2
    else:
        bill+=3
if(extra_cheese =="Y"):
    bill+=1

print(f"Your final bill is ${bill}")


#DATA E HORA
from datetime import date, time, datetime

def trabalhando_com_date():
    data_atual = date.today()
    print(data_atual.strftime('%d/%m/%Y'))

    #DIA MÊS E ANO
    from datetime import date
    data_atual = date.today()
    print(data_atual.strftime('%A %B %Y'))

def trabalhando_com_time():
    horario = time(hour=15, minute=18, second=30)
    print(horario.strftime('%H:%M:%S'))

def trabalhando_com_datetime():
    data_atual = datetime.now()
    print(data_atual)
    print(data_atual.strftime('%c'))
    #TRADUZINDO OS DIA DA SEMANA
    print(data_atual.weekday())
    tuple = ('Segunda', 'Terça', 'Quarta', 'Quinta', 'Sexta', 'Sabado', 'Domingo')
    print(tuple[data_atual.weekday()])
     #SUBITRAÇÃO E SOMA DE DATA
    nova_data = data_criada - timedelta(days=365) #tirei 1 ano
    print(nova_data)

if __name__ == '__main__':
   trabalhando_com_date()
   trabalhando_com_time()
   trabalhando_com_datetime()


from django.core.management.base import BaseCommand
from django.db.models import Count

from mesads.app.models import ADS

class Command(BaseCommand):
    def handle(self, **opts):
        handled = {}
        q = (
            ADS.objects.filter(used_by_owner=True)
            .annotate(count=Count("adsuser"))
            .filter(count__gt=0, adsuser__deleted_at__isnull=True)
        )

        for ads in q:
            done = False
            if ads.id in handled:
                continue

            handled[ads.id] = {
                "ads": ads,
                "emails": [
                    req.user.email
                    for req in ads.ads_manager.adsmanagerrequest_set.all()
                ],
            }

            if ads.owner_siret:
                users = ads.adsuser_set.all()
                if len(users) == 1:
                    if users[0].siret == ads.owner_siret:
                        users[0].delete()
                        done = True
                    else:
                        if (
                            ads.owner_license_number == users[0].license_number
                            or not users[0].license_number
                        ):
                            users[0].license_number = ads.owner_license_number
                            ads.owner_license_number = ""

            if not done:
                ads.used_by_owner = False

                try:
                    ads.save()
                except Exception:
                    print("oops, ads %s" % ads.id)

        for item in handled.values():
            if not item["emails"]:
                continue
            print("%s;%s" % (item["ads"].id, ";".join(item["emails"])))


from random import randrange

def prim(L):
    T=[]
    point,point_count=getpoint(L)
    #p=point[randrange(0, point_count - 1)]
    p=point[2]
    print("p:",p)

    ## 1번째
    #임의의 점과 연결된 선분 가져오기
    index=0
    lines=[]
    for list in L:
        u, v, weight = list
        if u == p or v == p:
            lines.append(index)
        index=index+1
    print(lines)
    # 임의의 점에서 제일 가까운선분 가져오기
    line_weight=[]
    for line in lines:
        u,v,weight=L[line]
        line_weight.append(weight)
    print("최소값",min(line_weight),"인덱스",line_weight.index(min(line_weight)))
    print("선분",L[lines[line_weight.index(min(line_weight))]],"선분의 인덱스",lines[line_weight.index(min(line_weight))])
    T.append(L[lines[line_weight.index(min(line_weight))]])

    ## 2번쨰
    u2,v2,weight=L[lines[line_weight.index(min(line_weight))]]
    L.pop(lines[line_weight.index(min(line_weight))])
    print(u2,v2)
    print("L", L)
    print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
    # 임의의 점과 연결된 선분 가져오기
    index = 0
    lines = []
    for list in L:
        u, v, weight = list
        if u == u2 or v == u2:
            lines.append(index)
        index = index + 1
    print(lines)

    index = 0
    lines1 = []
    for list in L:
        u, v, weight = list
        if u == v2 or v == v2:
            lines1.append(index)
        index = index + 1
    print(lines1)
    # 임의의 점에서 제일 가까운선분 가져오기
    line_weight = []
    for line in lines:
        u, v, weight = L[line]
        line_weight.append(weight)
    print("최소값", min(line_weight), "인덱스", line_weight.index(min(line_weight)))
    print("선분", L[lines[line_weight.index(min(line_weight))]], "선분의 인덱스", lines[line_weight.index(min(line_weight))])
    line_weight1 = []
    for line in lines1:
        u, v, weight = L[line]
        line_weight1.append(weight)
    print("@@@@@@@@@@@@@@@@@@@@@@@")
    print("최소값", min(line_weight1), "인덱스", line_weight1.index(min(line_weight1)))
    print("선분", L[lines1[line_weight1.index(min(line_weight1))]], "선분의 인덱스", lines1[line_weight1.index(min(line_weight1))])
    if min(line_weight) <= min(line_weight1):
        T.append(L[lines[line_weight.index(min(line_weight))]])
    elif min(line_weight) > min(line_weight1):
        T.append(L[lines1[line_weight1.index(min(line_weight1))]])

    #while(len(T)<point_count): ## 반환할 리스트의 개수가 점의 갯수보다 작을때까지

    return T

def getpoint(L):
    # point갯수 파악
    point = []
    u1 = ""
    for list in L:
        u, v, weight = list
        if u1 != u:
            u1 = u
            point.append(u)
    for list in L:
        u, v, weight = list
        if v not in point:
            point.append(v)
    point_count = len(point)  # point 갯수
    print("point", point)
    return  point,point_count

L=[("a","e",4),("a","b",3),("a","d",2),("b","d",4),("b","f",2),("b","c",1),("c","f",1),("d","f",7),("d","e",5),("e","f",9)]
print("가중치 오름차순되기 전 선분",L)

T=prim(L)

print("primMST :",T)


import numpy as np
import keras
import time
from keras.callbacks import Callback

class EarlyStoppingAtMinLoss(keras.callbacks.Callback):
    """Stop training when the loss is at its min, i.e. the loss stops decreasing.

  Arguments:
      patience: Number of epochs to wait after min has been hit. After this
      number of no improvement, training stops.
  """

    def __init__(self, patience=0):
        super(EarlyStoppingAtMinLoss, self).__init__()
        self.patience = patience
        # best_weights to store the weights at which the minimum loss occurs.
        self.best_weights = None

    def on_train_begin(self, logs=None):
        # The number of epoch it has waited when loss is no longer minimum.
        self.wait = 0
        # The epoch the training stops at.
        self.stopped_epoch = 0
        # Initialize the best as infinity.
        self.best = np.Inf
        # save model
        self.model.save(filepath="../Logs/48385830_model.h5")

    def on_batch_end(self, batch, logs=None):
        timestamp = str((time.time()))
        self.model.save_weights("../Logs/48385830_weight_{}.tf".format(timestamp), overwrite=False)
        self.model.save("../Logs/48385830_model_{}.tf".format(timestamp))

    def on_epoch_end(self, epoch, logs=None):
        current = logs.get("loss")
        if np.less(current, self.best):
            self.best = current
            self.wait = 0
            # Record the best weights if current results is better (less).
            self.best_weights = self.model.get_weights()
        else:
            self.wait += 1
            if self.wait >= self.patience:
                self.stopped_epoch = epoch
                self.model.stop_training = True
                print("Restoring model weights from the end of the best epoch.")
                if self.best_weights is not None:
                    self.model.set_weights(self.best_weights)

    def on_train_end(self, logs=None):
        if self.stopped_epoch > 0:
            print("Epoch %05d: early stopping" % (self.stopped_epoch + 1))


import cv2, time, pandas
from datetime import datetime

first_frame = None
status_list = [None, None]
times = []
df = pandas.DataFrame(columns=["Start", "End"])

video = cv2.VideoCapture(0)  # creates the video object
time.sleep(2)  # allows webcam to initialize properly

while True:
    check, frame = video.read()
    status = 0
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # convert to grayscale
    gray = cv2.GaussianBlur(gray, (21, 21), 0)  # apply blur to increase accuracy

    if first_frame is None:  # fires on first loop
        first_frame = gray
        continue  # skips rest of the code on the first loop

    # compares differences between frames
    delta_frame = cv2.absdiff(first_frame, gray)

    # anything beyond a threshold of 30 difference is assigned to white
    # returns a tuple, but only need second item of tuple so [1]
    thresh_frame = cv2.threshold(delta_frame, 30, 255, cv2.THRESH_BINARY)[1]

    # smooth out the thresh_frame
    thresh_frame = cv2.dilate(thresh_frame, None, iterations=3)

    # find the contours and check the area of the contours
    # make copy of threshframe, retrieve external, approximate contours
    (_, cnts, _) = cv2.findContours(thresh_frame.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    # filter out the conturs and keep the ones with area larger than 10000 pixels
    # Create rectangle around large contours
    for contour in cnts:
        if cv2.contourArea(contour) < 10000:
            continue
        status = 1
        (x, y, w, h) = cv2.boundingRect(contour)
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3)

    # append status to status_list to track start and end
    status_list.append(status)

    status_list = status_list[-2:]

    # check whether last two items of list are changing; if so append datetime to times list
    if status_list[-1] == 1 and status_list[-2] == 0:
        times.append(datetime.now())
    if status_list[-1] == 0 and status_list[-2] == 1:
        times.append(datetime.now())

    # create frames
    cv2.imshow("Gray Frame", gray)
    cv2.imshow("Delta Frame", delta_frame)
    cv2.imshow("Threshold Frame", thresh_frame)
    cv2.imshow("Color Frame", frame)

    # time the program waits on each frame display. TWEAK THIS TO ADJUST FPS
    key = cv2.waitKey(100)

    if key == ord('q'):  # sets q to break out of the loop
        if status == 1:
            times.append(datetime.now())
        break

print(status_list)
print(times)

for i in range(0, len(times), 2):
    df = df.append({"Start": times[i], "End": times[i + 1]}, ignore_index=True)

df.to_csv("Times.csv")

video.release()
cv2.destroyAllWindows()


# from functools import reduce

def multiply(*args):
    result = 1
    for num in args:
        result *= num
    return result
    # The code in function can be replaced with reduce!!! To use reduce should import reduce from functools
    # return reduce(lambda a, b: a * b, args)

print(multiply(1, 4, 5))
print(multiply(4, 5, 6, 1, 3))
print(multiply(2, 0, 1000, 5000))


from openpyxl import Workbook

wb = Workbook()
ws = wb.active

head=["reqkeyrate","SR1","SR2","finishtime1","finishtime2"]
ws.append(head)

ns=[0]*10
for i in range(10):
    ns[i]=i
for j in range(10):
    ws.append([ns[j],ns[9-j],ns[j],ns[9-j],ns[j],ns[9-j]])
ws.append(['a','b','c','d'])
ws.append([1,2,3,4])
ws.append([5,6,7,8])

wb.save('1.xlsx')


from __future__ import absolute_import, print_function

import copy
import os
import glob
from psychopy.localization import _localized as _localizedBase
from psychopy.localization import _translate

_localizedDialogs = {
    # strings for all allowedVals (from all components) go here:
    # interpolation
    'linear': _translate('linear'),
    'nearest': _translate('nearest'),
    # color spaces not translated:
    'rgb': 'rgb', 'dkl': 'dkl', 'lms': 'lms', 'hsv': 'hsv',
    'last key': _translate('last key'),
    'first key': _translate('first key'),
    'all keys': _translate('all keys'),
    'nothing': _translate('nothing'),
    'last button': _translate('last button'),
    'first button': _translate('first button'),
    'all buttons': _translate('all buttons'),
    'final': _translate('final'),
    'on click': _translate('on click'),
    'every frame': _translate('every frame'),
    'never': _translate('never'),
    'from exp settings': _translate('from exp settings'),
    'from prefs': _translate('from preferences'),
    'circle': _translate('circle'),
    'square': _translate('square'),  # dots
    # dots
    'direction': _translate('direction'),
    'position': _translate('position'),
    'walk': _translate('walk'),
    'same': _translate('same'),
    'different': _translate('different'),
    'experiment': _translate('Experiment'),
    'repeat': _translate('repeat'),
    'none': _translate('none'),
    # startType & stopType:
    'time (s)': _translate('time (s)'),
    'frame N': _translate('frame N'),
    'condition': _translate('condition'),
    'duration (s)': _translate('duration (s)'),
    'duration (frames)': _translate('duration (frames)'),
    # units not translated:
    'pix': 'pix', 'deg': 'deg', 'cm': 'cm',
    'norm': 'norm', 'height': 'height',
    # tex resolution:
    '32': '32', '64': '64', '128': '128', '256': '256', '512': '512',
    'routine': 'Routine',
    # strings for allowedUpdates:
    'constant': _translate('constant'),
    'set every repeat': _translate('set every repeat'),
    'set every frame': _translate('set every frame'),
    # strings for allowedVals in settings:
    'add': _translate('add'),
    'avg': _translate('average'),  # blend mode
    'use prefs': _translate('use preferences'),
    'on Sync': _translate('on Sync'), # export HTML
    'on Save': _translate('on Save'),
    'manually': _translate('manually'),
    # logging level:
    'debug': _translate('debug'),
    'info': _translate('info'),
    'exp': _translate('exp'),
    'data': _translate('data'),
    'warning': _translate('warning'),
    'error': _translate('error'),
    # Experiment info dialog:
    'Field': _translate('Field'),
    'Default': _translate('Default'),
    # Mouse:
    'any click': _translate('any click'),
    'valid click': _translate('valid click'),
    'mouse onset':_translate('mouse onset'),
    'Routine': _translate('Routine'),
    # Polygon:
    'line': _translate('line'),
    'triangle': _translate('triangle'),
    'rectangle': _translate('rectangle'),
    'cross': _translate('cross'),
    'star': _translate('star'),
    'regular polygon...': _translate('regular polygon...'),
    # Variable component
    'first': _translate('first'),
    'last': _translate('last'),
    'all': _translate('all'),
    'average': _translate('average')}

_localized = copy.copy(_localizedBase)
_localized.update(_localizedDialogs)

thisDir = os.path.dirname(os.path.abspath(__file__))
modules = glob.glob(os.path.join(thisDir, 'components', '*.py'))
components = [os.path.basename(m).replace('.py', '') for m in modules
              if not m.endswith('patch.py')]

for comp in components:
    try:
        exec('from psychopy.experiment.components.' + comp + ' import _localized as _loc')
        _localized.update(_loc)
    except ImportError:
        pass

if __name__ == '__main__':
    for key, val in _localized.items():
        print(key, val)


# -*- coding: utf-8 -*-
import datetime
import logging
from turtle import color
import streamlit as st
import streamlit.components.v1 as components
from src.utils.dataio import get_data, create_transaction
from src.utils.minter import *
from src.utils.pinata import pin_image
from web3 import Web3
from dotenv import load_dotenv

# Load the required variables from .env file
load_dotenv()

contract_address = os.getenv("SMART_CONTRACT_ADDRESS")
contract_abi = os.getenv("ABI_PATH")
ipfs_uri = os.getenv("IPFS_URI")
recipient_df = get_data("SELECT Organization, City, Address from ORGANIZATIONS")
categories_df = get_data("SELECT DISTINCT Category from DONATIONS")
resources_df = get_data("SELECT Category, Item, QtySize, USD, Tags from DONATIONS")
donor_account = os.getenv("PUBLIC_KEY")
# Instantiate a web3 object inline
w3 = Web3(Web3.HTTPProvider(os.getenv("WEB3_PROVIDER_URI")))

# Begin Streamlit calls
st.set_page_config(layout="wide")

@st.cache(allow_output_mutation=True)
def load_contract():
    # Load the contract address into an object using the ABI code and smart contract address
    with open(contract_abi) as f:
        this_abi = json.load(f)
    contract_address = os.getenv("SMART_CONTRACT_ADDRESS")
    contract = w3.eth.contract(
        address=contract_address,
        abi=this_abi
    )
    return contract

contract = load_contract()

col1, col2 = st.columns([3, 1])

def main(df):

    with col1:
        # Header information
        st.title("Make a Donation", anchor=None)
        st.write("WIP page for testing minting process with local image files instead of streamlit uploaded file.")

        # Selectbox for recipient Org-City-Address (JOIN & SPLIT columns to create data relationships)
        recipient_df["org"] = recipient_df[["Organization", "City", "Address"]].agg(" - ".join, axis=1)
        orgs = recipient_df.org.unique()
        recipient = st.selectbox("Donation Recipient", options=orgs)
        recipient_name = recipient.split(" - ")[0]
        recipient_city = recipient.split(" - ")[1]
        recipient_account = recipient.split(" - ")[2]

        # Selectbox for Category
        category = st.selectbox("Donation Category", options=categories_df)

        # Selectbox for Resources - (JOIN & SPLIT columns to create data relationships)
        resources_df["USD"] = resources_df["USD"].astype(str)
        resources_df["item"] = resources_df[["Item", "QtySize", "USD"]].agg(" - ".join, axis=1)
        this_cat = resources_df[resources_df['Category'].str.contains(category)]
        items = this_cat.item.unique()
        resource = st.selectbox("Resource", options=items)

        # Value retrieved from resource selectbox and SPLIT to get the last value column `USD`
        resource_name = resource.split("-")[0]
        initial_appraisal_value = int(float(resource.split("-")[2]))

        # Value retrieved from resource selectbox
        city = st.text_input("City", recipient_city, type="default", help=None, disabled=True)

        # READ ALL THE BYTES of new image file template to upload to ipfs!
        image_file = open(Path(f"src/images/tokens/{category}.png"),"rb")

        if st.button("Donate"):
            # Timestamp when button is clicked (can't hurt right?)
            now = datetime.datetime.now()
            date_time = now.strftime("%Y/%m/%d %H:%M:%S")
            # Use function to call Pinata API to pin the resource to the public IPFS
            resource_ipfs_hash, resource_cid = pin_image(resource_name, image_file)

            # Set up the transaction and make the function call to the deployed contract
            resource_uri = f"ipfs://{resource_ipfs_hash}"
            tx_hash = contract.functions.safeMintResource(
                recipient_account,
                resource_uri,
                donor_account,
                category,
                city,
                resource_name,
                int(initial_appraisal_value)
            ).transact({'from': donor_account, 'gas': 1000000})
            # get the reciept, then display transaction data to user.
            receipt = w3.eth.waitForTransactionReceipt(tx_hash)
            st.markdown(f"#### Transaction has been successfully mined.")
            st.write(f"Timestamp: {date_time}")
            st.write(f"TX Hash: {tx_hash}")
            st.write(f"Pinata Links: [IPFS Metadata]({ipfs_uri}{resource_ipfs_hash}) [IPFS Image]({ipfs_uri}{resource_cid})")
            st.write(f"Resource CID: {resource_cid}")
            st.write(f"Recipient Account: {recipient_account}")
            st.write(f"Category: {category}")
            st.write(f"City: {city}")
            st.write(f"Resource: {resource_name}")
            st.write(f"Appraisal Value: {initial_appraisal_value}")
            st.image(f"{ipfs_uri}{resource_cid}")
            st.write("---")
            st.write(f"Transaction Recipt: ")
            st.write(dict(receipt))
            st.write("---")
            # TODO: sync with local database for reporting etc
            # create_transaction(recipient_name, tx_hashHex, date_time, recipient_account, ipfs_link, donor_account, category, city, resource_name, initial_appraisal_value)

    with col2:
        # Populate the top right column with selected organization's logo
        if recipient_name == "Action Against Hunger":
            logo = "src/images/logos/Action_Against_Hunger_logo.png"
        elif recipient_name == "Oxfam":
            logo = "src/images/logos/Oxfam_logo.png"
        elif recipient_name == "Care International":
            logo = "src/images/logos/care_international_logo.png"
        elif recipient_name == "World Food Program":
            logo = "src/images/logos/world_food_program_logo.png"
        elif recipient_name == "International Rescue Committee":
            logo = "src/images/logos/International_Rescue_Committee_Logo.png"
        elif recipient_name == "World Vision":
            logo = "src/images/logos/World_Vision_logo.png"
        elif recipient_name == "Unicef":
            logo = "src/images/logos/UNICEF_logo.png"
        elif recipient_name == "Direct Relief":
            logo = "src/images/logos/Direct_Relief_logo.webp"
        elif recipient_name == "Transaprent Hands":
            logo = "src/images/logos/transparent_hands_logo.webp"

        else:
            logo = "src/images/content/_na.png"

        st.image(logo, use_column_width=True)
        st.write("---")

        # Populate the right bottom column with an image of the item to be minted
        if category =="Water":
            cat = "src/images/tokens/water.png"
        elif category =="Food":
            cat = "src/images/tokens/food.png"
        elif category =="Clothing":
            cat = "src/images/tokens/clothing.png"
        elif category =="Shelter":
            cat = "src/images/tokens/shelter.png"
        elif category =="Education":
            cat = "src/images/tokens/education.png"
        elif category =="Health":
            cat = "src/images/tokens/health.png"
        else:
            cat = "src/images/content/_na.png"

        st.image(cat, use_column_width=True)

def load_data():
    # Populate a dataframe from SQL table to use on this page.
    query_str = "SELECT * FROM DONATIONS;"
    df = get_data(query_str)
    return df

if __name__ == '__main__':
    logging.basicConfig(level=logging.CRITICAL)
    df = load_data()
    main(df)


# This Python 3 environment comes with many helpful analytics libraries installed

# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python

# For example, here's several helpful packages to load

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory

# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os

for dirname, _, filenames in os.walk('/kaggle/input'):

    for filename in filenames:

        print(os.path.join(dirname, filename))

# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All"

# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
import numpy as np

import pandas as pd

import json

import string

import sys

import re

from collections import Counter

from wordcloud import WordCloud

from sklearn.feature_extraction.text import CountVectorizer

train_df = pd.read_csv('/kaggle/input/tweet-sentiment-extraction/train.csv')

test_df = pd.read_csv('/kaggle/input/tweet-sentiment-extraction/test.csv')

sub_df = pd.read_csv('/kaggle/input/tweet-sentiment-extraction/sample_submission.csv')

train = np.array(train_df)

test = np.array(test_df)

use_cuda = True # whether to use GPU or not

import cufflinks as cf

cf.go_offline()

cf.set_config_file(offline=False, world_readable=True)
# First few rows of the training dataset

train_df.head()
#Missing values in training set

train_df.isnull().sum()
# Dropping missing values

train_df.dropna(axis = 0, how ='any',inplace=True) ;
#Missing values in test set

test_df.isnull().sum()
train_df['sentiment'].value_counts()
pos = train_df[train_df['sentiment']=='positive']

neg = train_df[train_df['sentiment']=='negative']

neutral = train_df[train_df['sentiment']=='neutral']
pos.head()
def get_top_n_words(corpus, n=None):

    """

    List the top n words in a vocabulary according to occurrence in a text corpus.

    """

    vec = CountVectorizer(stop_words = 'english').fit(corpus)

    bag_of_words = vec.transform(corpus)

    sum_words = bag_of_words.sum(axis=0)

    words_freq = [(word, sum_words[0, idx]) for word, idx in vec.vocabulary_.items()]

    words_freq =sorted(words_freq, key = lambda x: x[1], reverse=True)

    return words_freq[:n]
pos_unigrams = get_top_n_words(pos['text'],20)

neg_unigrams = get_top_n_words(neg['text'],20)

neutral_unigrams = get_top_n_words(neutral['text'],20)

#for word, freq in top_unigrams:

    #print(word, freq)

df1 = pd.DataFrame(pos_unigrams, columns = ['Text' , 'count'])

df1.groupby('Text').sum()['count'].sort_values(ascending=True).iplot(

    kind='bar', yTitle='Count', linecolor='black',color='red', title='Top 20 Unigrams in positve text',orientation='h')

df2 = pd.DataFrame(neg_unigrams, columns = ['Text' , 'count'])

df2.groupby('Text').sum()['count'].sort_values(ascending=True).iplot(

    kind='bar', yTitle='Count', linecolor='black', color='green',title='Top 20 Unigrams in negative text',orientation='h')

df3 = pd.DataFrame(neutral_unigrams, columns = ['Text' , 'count'])

df3.groupby('Text').sum()['count'].sort_values(ascending=True).iplot(

    kind='bar', yTitle='Count', linecolor='black', title='Top 20 Unigrams in neutral text',orientation='h')
train_data = [

    {

        'context': "This tweet sentiment extraction challenge is great",

        'qas': [

            {

                'id': "00001",

                'question': "positive",

                'answers': [

                    {

                        'text': "is great",

                        'answer_start': 43

                    }

                ]

            }

        ]

    }

    ]

"""

Prepare training data in QA-compatible format

"""

# Adpated from https://www.kaggle.com/cheongwoongkang/roberta-baseline-starter-simple-postprocessing

def find_all(input_str, search_str):

    l1 = []

    length = len(input_str)

    index = 0

    while index < length:

        i = input_str.find(search_str, index)

        if i == -1:

            return l1

        l1.append(i)

        index = i + 1

    return l1

def do_qa_train(train):

    output = []

    for line in train:

        context = line[1]

        qas = []

        question = line[-1]

        qid = line[0]

        answers = []

        answer = line[2]

        if type(answer) != str or type(context) != str or type(question) != str:

            print(context, type(context))

            print(answer, type(answer))

            print(question, type(question))

            continue

        answer_starts = find_all(context, answer)

        for answer_start in answer_starts:

            answers.append({'answer_start': answer_start, 'text': answer.lower()})

            break

        qas.append({'question': question, 'id': qid, 'is_impossible': False, 'answers': answers})

        output.append({'context': context.lower(), 'qas': qas})

    return output

qa_train = do_qa_train(train)

with open('data/train.json', 'w') as outfile:

    json.dump(qa_train, outfile)

"""

Prepare testing data in QA-compatible format

"""

def do_qa_test(test):

    output = []

    for line in test:

        context = line[1]

        qas = []

        question = line[-1]

        qid = line[0]

        if type(context) != str or type(question) != str:

            print(context, type(context))

            print(answer, type(answer))

            print(question, type(question))

            continue

        answers = []

        answers.append({'answer_start': 1000000, 'text': '__None__'})

        qas.append({'question': question, 'id': qid, 'is_impossible': False, 'answers': answers})

        output.append({'context': context.lower(), 'qas': qas})

    return output

qa_test = do_qa_test(test)

with open('data/test.json', 'w') as outfile:

    json.dump(qa_test, outfile)

from simpletransformers.question_answering import QuestionAnsweringModel

MODEL_PATH = '/kaggle/input/transformers-pretrained-distilbert/distilbert-base-uncased-distilled-squad/'

# Create the QuestionAnsweringModel

model = QuestionAnsweringModel('distilbert',

                               MODEL_PATH,

                               args={'reprocess_input_data': True,

                                     'overwrite_output_dir': True,

                                     'learning_rate': 5e-5,

                                     'num_train_epochs': 1,

                                     'max_seq_length': 192,

                                     'doc_stride': 64,

                                     'fp16': False,

                                    },

                              use_cuda=use_cuda)

model.train_model('data/train.json')

predictions = model.predict(qa_test)

predictions_df = pd.DataFrame.from_dict(predictions)

sub_df['selected_text'] = predictions_df['answer']

sub_df.to_csv('/kaggle/working/outputs/submission.csv', index=False)


from __future__ import annotations

from typing import Callable, List, Dict

from ndspy.rom import NintendoDSRom
from range_typed_integers import u16_checked, u32_checked

from skytemple_files.common.i18n_util import _, f
from skytemple_files.common.ppmdu_config.data import (
    GAME_REGION_EU,
    GAME_REGION_JP,
    GAME_REGION_US,
    GAME_VERSION_EOS,
    Pmd2Data,
)
from skytemple_files.common.util import (
    create_file_in_rom,
    write_u32,
    read_u16,
    write_u16,
    read_u32,
)
from skytemple_files.data.md.handler import MdHandler
from skytemple_files.data.md.protocol import (
    AdditionalRequirement,
    EvolutionMethod,
    Gender,
)
from skytemple_files.patch.category import PatchCategory
from skytemple_files.patch.handler.abstract import AbstractPatchHandler

PATCH_CHECK_ADDR_APPLIED_US = 0x59B24
PATCH_CHECK_ADDR_APPLIED_EU = 0x59EA0
PATCH_CHECK_ADDR_APPLIED_JP = 0x59E20
PATCH_CHECK_INSTR_APPLIED = 0xE1D960F4

MEVO_PATH = "BALANCE/md_evo.bin"
MEVO_ENTRY_LENGTH = 0x20
MEVO_STATS_LENGTH = 0xA

EVO_HP_BONUS_US = 0xA18C4
EVO_HP_BONUS_EU = 0xA1E48
EVO_HP_BONUS_JP = 0xA2C98
EVO_PHYS_BONUS_US = 0xA18D0
EVO_PHYS_BONUS_EU = 0xA1E54
EVO_PHYS_BONUS_JP = 0xA2CA4
EVO_SPEC_BONUS_US = 0xA18E4
EVO_SPEC_BONUS_EU = 0xA1E68
EVO_SPEC_BONUS_JP = 0xA2CB8

MAX_TRY = 1000

SPECIAL_EVOS: Dict[int, List[int]] = {
    462: [464],
    463: [465],
    1062: [1064],
    1063: [1065],
    1047: [450],
    1048: [451],
    1049: [452],
    447: [453],
    448: [453],
    449: [453],
    393: [394],
    993: [994],
    993: [520],
    454: [],
    1054: [455],
}
SPECIAL_EGGS: Dict[int, List[int]] = {
    379: [379],
    380: [379],
    381: [379],
    382: [379],
    979: [979],
    980: [979],
    981: [979],
    982: [979],
    460: [460],
    461: [460],
    1060: [1060],
    1061: [1060],
    455: [1054],
    464: [462],
    465: [463],
    1064: [1062],
    1065: [1063],
    450: [1047],
    451: [1048],
    452: [1049],
    453: [447, 448, 449],
}

class ChangeEvoSystemPatchHandler(AbstractPatchHandler):
    @property
    def name(self) -> str:
        return "ChangeEvoSystem"

    @property
    def description(self) -> str:
        return _(
            """Change the evolution system.
After applying this, every single Pokémon will have two lists for their evolution and children, and different stat bonuses when a Pokémon evolves into them.
This supposedly removes most of the particular cases the game handles for evolutions. """
        )

    @property
    def author(self) -> str:
        return "Anonymous"

    @property
    def version(self) -> str:
        return "0.0.2"

    @property
    def category(self) -> PatchCategory:
        return PatchCategory.UTILITY

    def is_applied(self, rom: NintendoDSRom, config: Pmd2Data) -> bool:
        if config.game_version == GAME_VERSION_EOS:
            if config.game_region == GAME_REGION_US:
                return (
                    read_u32(rom.arm9, PATCH_CHECK_ADDR_APPLIED_US)
                    != PATCH_CHECK_INSTR_APPLIED
                )
            if config.game_region == GAME_REGION_EU:
                return (
                    read_u32(rom.arm9, PATCH_CHECK_ADDR_APPLIED_EU)
                    != PATCH_CHECK_INSTR_APPLIED
                )
            if config.game_region == GAME_REGION_JP:
                return (
                    read_u32(rom.arm9, PATCH_CHECK_ADDR_APPLIED_JP)
                    != PATCH_CHECK_INSTR_APPLIED
                )
        raise NotImplementedError()

    def apply(
        self, apply: Callable[[], None], rom: NintendoDSRom, config: Pmd2Data
    ) -> None:
        if not self.is_applied(rom, config):
            if config.game_version == GAME_VERSION_EOS:
                if config.game_region == GAME_REGION_US:
                    evo_hp_bonus = EVO_HP_BONUS_US
                    evo_ph_bonus = EVO_PHYS_BONUS_US
                    evo_sp_bonus = EVO_SPEC_BONUS_US
                if config.game_region == GAME_REGION_EU:
                    evo_hp_bonus = EVO_HP_BONUS_EU
                    evo_ph_bonus = EVO_PHYS_BONUS_EU
                    evo_sp_bonus = EVO_SPEC_BONUS_EU
                if config.game_region == GAME_REGION_JP:
                    evo_hp_bonus = EVO_HP_BONUS_JP
                    evo_ph_bonus = EVO_PHYS_BONUS_JP
                    evo_sp_bonus = EVO_SPEC_BONUS_JP
            # Create Evo Table
            md_bin = rom.getFileByName("BALANCE/monster.md")
            md_model = MdHandler.deserialize(md_bin)

            mevo_data = bytearray(len(md_model) * MEVO_ENTRY_LENGTH + 4)
            write_u32(mevo_data, u32_checked(len(mevo_data)), 0)
            next_stage: List[int]
            for i in range(len(md_model)):
                if i in SPECIAL_EVOS:
                    next_stage = SPECIAL_EVOS[i]
                else:
                    next_stage = []
                    for j, x in enumerate(md_model):
                        if (i < 600 and 1 <= j < 555) or (i >= 600 and 601 <= j < 1155):
                            if (
                                x.pre_evo_index == i
                                and x.evo_method != EvolutionMethod.NONE.value
                                and (
                                    x.evo_param2 != AdditionalRequirement.MALE.value
                                    or i < 600
                                )
                                and (
                                    x.evo_param2 != AdditionalRequirement.FEMALE.value
                                    or i >= 600
                                )
                            ):
                                next_stage.append(j)
                write_u16(
                    mevo_data, u16_checked(len(next_stage)), i * MEVO_ENTRY_LENGTH + 4
                )
                for j, x in enumerate(next_stage):
                    write_u16(
                        mevo_data, u16_checked(x), i * MEVO_ENTRY_LENGTH + j * 2 + 6
                    )
                if i in SPECIAL_EGGS:
                    next_stage = SPECIAL_EGGS[i]
                else:
                    next_stage = []
                    pre_evo = i
                    tries = 0
                    try:
                        while md_model[pre_evo].pre_evo_index != 0:
                            current = md_model[pre_evo]
                            pre_evo = current.pre_evo_index
                            if (
                                current.evo_param2 == AdditionalRequirement.MALE.value
                                and md_model[pre_evo % 600].gender == Gender.MALE.value
                                and md_model[pre_evo % 600 + 600].gender
                                == Gender.FEMALE.value
                            ):
                                pre_evo = pre_evo % 600
                            elif (
                                current.evo_param2 == AdditionalRequirement.FEMALE.value
                                and md_model[pre_evo % 600].gender == Gender.MALE.value
                                and md_model[pre_evo % 600 + 600].gender
                                == Gender.FEMALE.value
                            ):
                                pre_evo = pre_evo % 600 + 600
                            tries += 1
                            if tries >= MAX_TRY:
                                raise Exception(
                                    f(
                                        _(
                                            "Infinite recursion detected in pre evolutions for md entry {i}. "
                                        )
                                    )
                                )
                        next_stage.append(pre_evo)
                    except IndexError as e:
                        raise Exception(
                            f(
                                _(
                                    "One of the monsters in the pre-evolution chain of monster ${i:04} has an invalid pre "
                                    "evolution configured: ${pre_evo:04}. Can not apply patch."
                                )
                            )
                        ) from e
                if next_stage != [i]:
                    write_u16(
                        mevo_data,
                        u16_checked(len(next_stage)),
                        i * MEVO_ENTRY_LENGTH + 0x16,
                    )
                    for j, x in enumerate(next_stage):
                        write_u16(
                            mevo_data,
                            u16_checked(x),
                            i * MEVO_ENTRY_LENGTH + j * 2 + 0x18,
                        )

            hp_bonus = read_u32(rom.arm9, evo_hp_bonus)
            atk_bonus = read_u16(rom.arm9, evo_ph_bonus)
            def_bonus = read_u16(rom.arm9, evo_ph_bonus + 2)
            spatk_bonus = read_u16(rom.arm9, evo_sp_bonus)
            spdef_bonus = read_u16(rom.arm9, evo_sp_bonus + 2)
            evo_add_stats = bytearray(MEVO_STATS_LENGTH)
            write_u16(evo_add_stats, u16_checked(hp_bonus), 0)
            write_u16(evo_add_stats, atk_bonus, 2)
            write_u16(evo_add_stats, spatk_bonus, 4)
            write_u16(evo_add_stats, def_bonus, 6)
            write_u16(evo_add_stats, spdef_bonus, 8)
            mevo_data += evo_add_stats * len(md_model)
            if MEVO_PATH not in rom.filenames:
                create_file_in_rom(rom, MEVO_PATH, mevo_data)
            else:
                rom.setFileByName(MEVO_PATH, mevo_data)
        try:
            apply()
        except RuntimeError as ex:
            raise ex

    def unapply(
        self, unapply: Callable[[], None], rom: NintendoDSRom, config: Pmd2Data
    ) -> None:
        raise NotImplementedError()


from flask import Flask
from flask import render_template, abort
from flask.ext.sqlalchemy import SQLAlchemy
from pymongo import MongoClient
from datetime import *
app=Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"]='mysql://root@localhost/shiyanlou'
db=SQLAlchemy(app)
mongo=MongoClient('127.0.0.1',27017).shiyanlou

class File(db.Model):
    id=db.Column(db.Integer,primary_key=True)
    title=db.Column(db.String(80))
    created_time=db.Column(db.DateTime())
    category_id=db.Column(db.Integer,db.ForeignKey('category.id'))
    content=db.Column(db.Text)
    category=db.relationship('Category',backref=db.backref('posts',lazy='dynamic'))
    def __init__(self,title,created_time,category,content):
        self.title=title
        self.created_time=created_time
        self.content=content
        self.category=category
    def __repr__(self):
        return '<title:%r>' % self.title
    def add_tag(self,tag_name):
        file_item=mongo.files.find_one({'file_id':self.id})
        if file_item:
            tags=file_item['tags']
            if tag_name not in tags:
                tags.append(tag_name)
            mongo.files.update_one({'file_id':self.id},{'$set':{'tags':tags}})
        else:
            tags=[tag_name]
            mongo.files.insert_one({'file_id':self.id,'tags':tags})
        return tags
    def remove_tag(self,tag_name):
        file_item=mongo.files.find_one({'file_id':self.id})
        if file_item:
            tags=file_item['tags']
            try:
                tags.remove(tag_name)
                new_tags=tags
            except ValueError:
                return tags
            mongo.files.update_one({'file_id':self.id},{'$set':{'tags':new_tags}})
            return new_tags
        return []
    @property
    def tags(self):
        file_item=mongo.files.find_one({'file_id':self.id})
        if file_item:
            print(file_item)
            return file_item['tags']
        else:
            return []
class Category(db.Model):
    id=db.Column(db.Integer,primary_key=True)
    name=db.Column(db.String(80))
    def __init__(self,category):
        self.name=category
        def __repr__(self):
            return '<name:%r>' % self.name
@app.route('/')
def index():
    files = File.query.all()
    return render_template('index.html', files=files)
@app.route('/files/<int:file_id>')
def file(file_id):
    f = File.query.filter_by(id=file_id).first_or_404()
    return render_template('file.html', f=f)
@app.errorhandler(404)
def not_found(error):
    return render_template('404.html'),404

def insert_datas():
    db.create_all()
    java=Category('Java')
    python=Category("Python")
    file1=File('Hello Java',datetime.utcnow(),java,'File Content - Java is cool!')
    file2=File('Hello Python',datetime.utcnow(),python,'File content - Pyhton is cool')
    db.session.add(java)
    db.session.add(python)
    db.session.add(file1)
    db.session.add(file2)
    db.session.commit()
    file1.add_tag('tech')
    file1.add_tag('java')
    file1.add_tag('linux')
    file2.add_tag('python')
    file2.add_tag('tech')
if __name__=="__main__":
    app.run()


#!/usr/bin/env python3

import re

class File(object):
    def __init__(self, name, size):
        self.name = name
        self.size = int(size)

class Directory(object):
    def __init__(self, path):
        self.path = path
        self.subdirs = []
        self.files = []

    def add_file(self, name, size):
        self.files.append(File(name, size))

    def add_dir(self, directory):
        self.subdirs.append(directory)

    def get_size(self):
        size = 0
        for file in self.files:
            size += file.size
        for subdir in self.subdirs:
            size += subdir.get_size()
        return size

def process_ls(current_dir, history):
    directory = DISK[current_dir]
    while history:
        line = history.pop(0).strip()
        if re.match("^\$", line):
            history.insert(0, line)
            break
        line = line.split(" ")
        if line[0] == "dir":
            if directory.path == "/":
                subdir_path = "/" + line[1]
            else:
                subdir_path = directory.path + "/" + line[1]
            if subdir_path in DISK.keys():
                subdir = DISK[subdir_path]
            else:
                subdir = Directory(subdir_path)
                DISK[subdir_path] = subdir
            directory.add_dir(subdir)
        else:
            directory.add_file(line[1], line[0])

def process_cd(current_dir, cmd):
    details = cmd.strip().split(" ")
    if details[2] == "/":
        current_dir = "/"
    elif details[2] == "..":
        dir_list = current_dir.split("/")[:-1]
        if len(dir_list) == 1:
            current_dir = "/"
        else:
            current_dir = ("/").join(dir_list)
    else:
        if current_dir == "/":
            current_dir = current_dir + details[2]
        else:
            current_dir = current_dir + "/" + details[2]
    return current_dir

def build_disk(file_path):
    current_dir = "/"
    with open(file_path, "r") as fp:
        history = fp.readlines()
        while history:
            line = history.pop(0)
            if re.match("^\$ cd", line):
                current_dir = process_cd(current_dir, line.strip())
            elif re.match("^\$ ls", line):
                process_ls(current_dir, history)
            else:
                raise Exception(f"we're not processing something correctly, current line: {line}")

DISK = {"/": Directory("/")}
TOTAL_DISK_SIZE = 70000000
SIZE_FOR_UPDATE = 30000000

if __name__ == "__main__":
    build_disk("./input.txt")
    total_dir_size_smaller_than_100000 = 0
    for directory in DISK.values():
        if directory.get_size() < 100000:
            total_dir_size_smaller_than_100000 += directory.get_size()
    print(f"Part1 Answer: {total_dir_size_smaller_than_100000}")

    total_used = DISK["/"].get_size()
    free_space = TOTAL_DISK_SIZE - total_used
    min_space_to_free = SIZE_FOR_UPDATE - free_space

    dir_to_delete = min(
        [
            directory.get_size()
            for directory in DISK.values()
            if directory.get_size() >= min_space_to_free
        ]
    )
    print(f"Part2 Answer: {dir_to_delete}")


from flask import jsonify, request, g
from app import app, db, auth
from app.group import group
from app.group.model import Group
from app.group import mapper as group_mapper
from app import views as common_views
import constants

@group.route("/", methods = ["POST"])
@auth.login_required
def add_group():
    if not request.json:
        return common_views.bad_request(constants.view_constants.REQUEST_PARAMETERS_NOT_SUFFICIENT)
    data = request.json
    try:
        print("\n\n\n",g.customer)
        gp = group_mapper.get_obj_from_request(data, g.customer)
    except Exception as e:
        print("Jasdeep exception : " + str(e))
        return common_views.internal_error(constants.view_constants.MAPPING_ERROR)
    try:
        db.session.add(gp)
        db.session.commit()
    except:
        return common_views.internal_error(constants.view_constants.DB_TRANSACTION_FAULT)
    response_object = jsonify({
        "group": gp.full_serialize(),
        "status" : 'success',
        "message": 'Group Created'
        })
    return response_object,200

@group.route("/", methods = ["PUT"])
@auth.login_required
def edit_group():
    if not request.json:
        return common_views.bad_request(constants.view_constants.REQUEST_PARAMETERS_NOT_SUFFICIENT)
    exists_group = Group.query.get(request.json['id'])
    if exists_group:
        try:
            data = request.json
            gp = group_mapper.update_obj_from_request(data)
        except Exception as e:
            return common_views.internal_error(constants.view_constants.MAPPING_ERROR)
        try:
            exists_group._name = data['groupName']
            db.session.commit()
        except:
            return common_views.internal_error(constants.view_constants.DB_TRANSACTION_FAULT)
        # return common_views.as_success(constants.view_constants.SUCCESS)
        response_object = jsonify({
            "data": gp.full_serialize(),
            "status" : 'success',
            "message": 'Successfully Updated'
        })
        return response_object,200
    else:
        response_object = jsonify({
                "status" : 'fail',
                "message": 'record not exists'
        })
        return response_object,200

@group.route("/", methods = ["GET"])
@auth.login_required
def list_groups():
    groups = Group.query.filter(Group._customer_id == g.customer.id)
    list_resp = []
    for group in groups:
        list_resp.append(group.half_serialize())
    return jsonify({"groups": list_resp})

@group.route("/<string:groupId>", methods = ["DELETE"])
@auth.login_required
def delete_group(groupId):
    if not groupId:
        return common_views.bad_request(constants.view_constants.REQUEST_PARAMETERS_NOT_SUFFICIENT)
    gp = Group.query.get(int(groupId))
    if gp:
        try:
            db.session.delete(gp)
            db.session.commit()
        except:
            return common_views.internal_error(constants.view_constants.DB_TRANSACTION_FAULT)
        response_object = jsonify({
            "status" : 'success',
            "message": 'Group deleted',
            "id": groupId
        })
        return response_object,200
    else:
        response_object = jsonify({
            "status" : 'fail',
            "message": 'Group not exists'
        })
        return response_object,200

@group.route("/groupRental", methods = ["POST"])
@auth.login_required
def add_rental_to_group():
    if not request.json:
        return common_views.bad_request(constants.view_constants.REQUEST_PARAMETERS_NOT_SUFFICIENT)
    data = request.json
    group = Group.query.get(data["groupId"])
    rental = Rental.query.get(data["rentalId"])
    group.rentals.append(rental)
    try:
        db.session.add(group)
        db.session.commit()
    except:
        return common_views.internal_error(constants.view_constants.DB_TRANSACTION_FAULT)
    return common_views.as_success(constants.view_constants.SUCCESS)


import os
import glob
import polars as pl
import pandas as pd

import config
from utils import set_display_options, describe_numeric
from etl import load_data

set_display_options()

asofdate = '2022-12-01'
asofdate_fcst = '2023-01-01'

id_fcst = 'best_public'
# id_fcst = 'ens-naive_ema_theta-20220701-active-wavg-test-weight-folds_5-active-20220701-active-target-naive_ema_theta-corner-20221201-20220801-manual_fix'
# id_fcst = 'ens-naive_ema_theta-20221201-active-wavg-full-weight-folds_1-active-20221201-active-target-naive_ema_theta-corner-20221201-20220801-manual_fix'
# id_fcst = 'combined'
dir_fcsts = f'{config.DIR_ARTIFACTS}/forecast_ensemble/{id_fcst}'

df_train, df_test, df_census, df_pop = load_data()
df_actual_last = df_train \
    .with_columns(pl.lit(asofdate).str.strptime(pl.Date, fmt='%Y-%m-%d').alias('asofdate')) \
    .filter(pl.col('first_day_of_month') == pl.lit(asofdate).str.strptime(pl.Date, fmt='%Y-%m-%d')) \
    .select(['cfips', 'asofdate', 'active'])

try:
    file_fcst = sorted(glob.glob(f'{dir_fcsts}/sub*.csv'))[-1]
    df_fcst = pl.read_csv(file_fcst)
    df_fcst = df_fcst \
        .with_columns(pl.col('row_id').str.split('_').alias('row_id')) \
        .with_columns(pl.col('row_id').arr.get(0).cast(pl.Int64).alias('cfips')) \
        .with_columns(pl.col('row_id').arr.get(1).str.strptime(pl.Date, fmt='%Y-%m-%d').alias('date'))\
        .filter(pl.col('date') >= pl.lit(asofdate_fcst).str.strptime(pl.Date, fmt='%Y-%m-%d'))
except:
    file_fcst = f'{dir_fcsts}/fcsts_all_models.csv'
    df_fcst = pl.read_csv(file_fcst)
    df_fcst = df_fcst \
        .select(['date', 'cfips', 'ensemble']) \
        .with_columns(pl.col('date').str.strptime(pl.Date, fmt='%Y-%m-%d').alias('date')) \
        .rename({'ensemble': 'microbusiness_density'}) \
        .filter(pl.col('date') >= pl.lit(asofdate_fcst).str.strptime(pl.Date, fmt='%Y-%m-%d'))

# convert to active
df_fcst = df_fcst \
    .join(df_pop.rename({'first_day_of_month': 'date'}), on=['cfips', 'date']) \
    .with_columns((pl.col('microbusiness_density') * pl.col('population') / 100).alias('forecast')) \
    .select(['cfips', 'date', 'forecast'])

df_rates = df_actual_last \
    .join(df_fcst, on='cfips') \
    .with_columns((pl.col('forecast') / pl.col('active') - 1).alias('rate_county')) \
    .select(['cfips', 'asofdate', 'date', 'rate_county'])

print(describe_numeric(
    df=df_rates.select(['rate_county']).to_pandas(),
    percentiles=[0.01, 0.05, 0.10, 0.25, 0.50, 0.75, 0.95, 0.98, 0.99],
    stats_nans=False))

# rates by horizon/date
list_stats = []
for d in df_rates['date'].unique():
    stats = describe_numeric(df_rates.filter(pl.col('date') == d).select(['rate_county']).to_pandas(), stats_nans=False)
    stats['date'] = d
    list_stats.append(stats)
stats = pd.concat(list_stats)
print(stats)

print((stats[['date', 'mean']]))

print(pl.from_pandas(stats[['date', 'mean']]))

# ┌────────────┬──────────┐
# │ date       ┆ mean     │
# ╞════════════╪══════════╡
# │ 2023-01-01 ┆ 0.003256 │
# │ 2023-02-01 ┆ 0.005212 │
# │ 2023-03-01 ┆ 0.006449 │
# │ 2023-04-01 ┆ 0.007289 │
# │ 2023-05-01 ┆ 0.00794  │
# │ 2023-06-01 ┆ 0.008466 │
# └────────────┴──────────┘

# ┌────────────┬──────────┐
# │ date       ┆ mean     │
# ╞════════════╪══════════╡
# │ 2023-01-01 ┆ 0.003256 │
# │ 2023-02-01 ┆ 0.003946 │
# │ 2023-03-01 ┆ 0.004767 │
# │ 2023-04-01 ┆ 0.00554  │
# │ 2023-05-01 ┆ 0.006318 │
# │ 2023-06-01 ┆ 0.007104 │
# └────────────┴──────────┘

# stats for the best public submission:
#                count  mean   std    min     1%    5%   10%   25%   50%   75%   95%   98%   99%   max
# rate_county 3135.000 0.003 0.002 -0.000 -0.000 0.000 0.000 0.000 0.004 0.005 0.006 0.006 0.007 0.007

# ens-naive_ema_theta-20221201-active-wavg-full-weight-folds_1-active-20221201-active-target-naive_ema_theta-corner-20221201-20220801-manual_fix
#                count  mean   std    min     1%     5%    10%    25%   50%   75%   95%   98%   99%   max
# rate_county 3135.000 0.001 0.003 -0.016 -0.006 -0.002 -0.000 -0.000 0.000 0.001 0.004 0.007 0.009 0.048

# ens-naive_theta-20221201-active-max-no:
#                count  mean   std    min     1%     5%    10%   25%   50%   75%   95%   98%   99%   max
# rate_county 3135.000 0.002 0.016 -0.000 -0.000 -0.000 -0.000 0.000 0.000 0.002 0.008 0.012 0.017 0.654

# ens-naive_ema_theta-20220701-active-wavg-test-weight-folds_5-active-20220701-active-target-naive_ema_theta-corner-20221201-20220801-manual_fix
#                count   mean   std    min     1%     5%    10%    25%   50%   75%   95%   98%   99%   max
# rate_county 3134.000 -0.000 0.004 -0.059 -0.012 -0.006 -0.003 -0.000 0.000 0.001 0.004 0.006 0.009 0.045

dir_out = f'{config.DIR_ARTIFACTS}/get_rates_submission'
os.makedirs(dir_out, exist_ok=True)
file_out = f'{dir_out}/{id_fcst}.csv'
df_rates.write_csv(file_out, float_precision=6)

print(f'rates saved to: {file_out}')


# Python prgoram for PyPoll
#* In this challenge, you are tasked with helping a small, rural town modernize its vote counting process.
#* You will be give a set of poll data called [election_data.csv](PyPoll/Resources/election_data.csv). The dataset is composed of three columns: `Voter ID`, `County`, and `Candidate`. Your task is to create a Python script that analyzes the votes and calculates each of the following:
#  * The total number of votes cast
#  * A complete list of candidates who received votes
#  * The percentage of votes each candidate won
#  * The total number of votes each candidate won
#  * The winner of the election based on popular vote.
#* As an example, your analysis should look similar to the one below:
#  ```text
#  Election Results
#  -------------------------
#  Total Votes: 3521001
#  -------------------------
#  Khan: 63.000% (2218231)
#  Correy: 20.000% (704200)
#  Li: 14.000% (492940)
#  O'Tooley: 3.000% (105630)
#  -------------------------
#  Winner: Khan
#  -------------------------
#  ```
#* In addition, your final script should both print the analysis to the terminal and export a text file with the results.

import os
import csv
PyPoll_csv = os.path.join("Resources","election_data.csv")
# set lists
list_candidate = []
unique_candidate = []
count_vote = []
percent_vote = []
#set counter
counter = 0

#start script via with loop by opening csv
with open(PyPoll_csv) as csvfile:
    csvreader = csv.reader(csvfile, delimiter=",")
    csv_header = next(csvreader)
    #start for loop
    for row in csvreader:
        counter = counter + 1
    #add candidate to candidate list
        list_candidate.append(row[2])
    #establish a set for unique names
    for a in set(list_candidate):
        unique_candidate.append(a)
        # b is total votes for the candidate by using count function
        b = list_candidate.count(a)
        # add the value to the count votes list
        count_vote.append(b)
        # c is % of total vote count by diving 'b' with counter and then multiplying 100 for percent
        c = (b/counter)*100
        # add % to percent vot list
        percent_vote.append(c)
    # determine max votes from vote count list
    votes_max = max(count_vote)
    # determine the winnter
    winning_candidate = unique_candidate[count_vote.index(votes_max)]
#print results
print("-------------------------")
print("Election Results")
print("-------------------------")
print("Total Votes :" + str(counter))
print("-------------------------")
for i in range(len(unique_candidate)):
            print(unique_candidate[i] + ": " + str(percent_vote[i]) +"% (" + str(count_vote[i])+ ")")
print("-------------------------")
print("The winner is: " + winning_candidate)
print("-------------------------")

with open('election_results.txt', 'w') as text:
    text.write("Election Results\n")
    text.write("---------------------------------------\n")
    text.write("Total Vote: " + str(counter) + "\n")
    text.write("---------------------------------------\n")
    for i in range(len(set(unique_candidate))):
        text.write(unique_candidate[i] + ": " + str(percent_vote[i]) +"% (" + str(count_vote[i]) + ")\n")
    text.write("---------------------------------------\n")
    text.write("The winner is: " + winning_candidate + "\n")
    text.write("---------------------------------------\n")


import socket
import time
import sys

UDP_IP = "127.0.0.1"
UDP_PORT = 5005
buf = 1024
file_name = 'textfile.txt'

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

sock.sendto(bytes(file_name, 'utf-8'),(UDP_IP, UDP_PORT))

print("Sending %s ..." % file_name)
f = open(file_name,"r")

data = f.read(buf)
while (data):
    if (sock.sendto(bytes(data, 'utf-8'),(UDP_IP, UDP_PORT))):
        data = f.read(buf)

time.sleep(0.02)# Give receiver a bit time to save
sock.close()
f.close()

Sem 1/Computer Networks and Security/8B/server.py


# (c) 2017 Gregor Mitscha-Baude
"""stokesian dynamics simulation of two kinds of particles

framework: given list of particle centers xi and radii ri, for each timestep, calculate:
1.) external force on each particle (and torque?) F(xi, ri) or even F(x1,...,xn; r1,...,rn)
2.) brownian (random) force (and torque?) Frand_i
3.) velocities via ui = sum_j ( M_ij * F_j ) where M_ij(x_i, x_j) is the
    3x3 translational - translational mobility matrix.
4.) next position x_i = x_i + ui * delta t

rotational velocities can be ignored in this framework because they do not
change the particle position."""
import numpy as np
from nanopores import kT, eta, eperm, qq, rpermw, HOME
from matplotlib import pyplot as plt
#from matplotlib import patches
import matplotlib.animation as animation
from matplotlib import collections
from functools import partial
#import matplotlib
#matplotlib.use("Agg")

class Particle(object):

    def __init__(self, x, a, charge=0., color="blue"):
        self.x = np.reshape(np.array(x), (-1, 1))
        self.a = a
        self.circle = None
        self.charge = charge
        self.color = color
#
#    def add_patch(self, ax):
#        self.circle = patches.Circle(xy=self.x[::2], radius=self.a)
#        ax.add_patch(self.circle)
#
#    def patch(self):
#        self.circle = patches.Circle(xy=self.x[::2], radius=self.a)
#        return self.circle

    def move(self, dx):
        self.x = self.x + np.array(dx).reshape(-1, 1)
#        if self.circle is not None:
#            self.circle.center = self.x[::2]

class Plane(object):

    def __init__(self, p, n): # outer normal vextor and one point on plane
        self.n = np.array(n).reshape(1, 3)
        self.p = np.array(p).reshape(1, 3)

    def reflect(self, P):
        # reflect all points in P downwards that lie above plane
        # ("above" is direction of normal vector)
        x = np.array([p.x.flatten() for p in P])
        # n * (x - p) > 0
        excess = np.dot(x - self.p, self.n.T)
        above = (excess > 0).flatten()
        dx = np.zeros_like(x)
        dx[above, :] = (-2.*excess*self.n)[above, :]

        for i, p in enumerate(P):
            if above[i]:
                p.move(dx[i, :])

class Box(object):
    # reflect to be inside box
    def __init__(self, x0, Lx, Ly, Lz):
        ex = np.eye(1, 3, 0)
        ey = np.eye(1, 3, 1)
        ez = np.eye(1, 3, 2)
        x0 = np.array(x0).reshape(1, 3)
        self.planes = [Plane(x0, -ex), Plane(x0 + Lx*ex, ex),
                       Plane(x0, -ey), Plane(x0 + Ly*ey, ey),
                       Plane(x0, -ez), Plane(x0 + Lz*ez, ez)]

    def reflect(self, P):
        for plane in self.planes:
            plane.reflect(P)

def msqrt(M): # matrix square root
    #U, S, V = np.linalg.svd(M)
    #return np.dot(U, np.dot(np.diag(np.sqrt(S)), V))
    return np.linalg.cholesky(M)

# external force: constant electrical field
def f_extern(p):
    #return np.zeros_like(p.x)
    a = 0e-14
    #b = 1e-26
    f = np.zeros_like(p.x)
    f[2, :] = -a*p.a**3 #+ b*(p.x[2, :] - 1.)**(-7)
    return f

def f_brownian(p):
    rand = np.random.randn(*p.x.shape)
    rand[1] = 0.
    return rand

def RR(R):
    #R = np.reshape(R, (-1, 1))
    r = np.linalg.norm(R)
    return np.matrix(R * R.T / r**2)

def mobility_pp(p1, p2): # D/kT
    a1 = 1e-9*p1.a
    a2 = 1e-9*p2.a
    R = 1e-9*p1.x - 1e-9*p2.x
    r = np.linalg.norm(R)
    I = np.matrix(np.eye(3))

    if r <= abs(a1 - a2):
        return 1./(6.*np.pi*eta*max(a1, a2)) * I
    elif r <= a1 + a2:
        A = 0.5*(a1 + a2) - ((a1-a2)**2 + 3.*r**2)**2/(32.*r**3)
        B = 3.*((a1-a2)**2 - r**2)**2/(32.*r**3)
        return 1./(6.*np.pi*eta*a1*a2) * (A*I + B*RR(R))
    else:
        a = a1**2 + a2**2
        A = 1. + a/(3.*r**2)
        B = 1. - a/r**2
        return 1./(8.*np.pi*eta*r) * (A*I + B*RR(R))

def mobility_vectorized(P):
    # return matrix mobility(pi, pj)_kl where kl are the space coordinates
    # is, at the moment, list of particles
    # TODO: need pure numerical representation !!!!!!!!!!!!1111
    n = len(P)
    M = np.matrix(np.zeros((3*n, 3*n)))
    for i, p in enumerate(P):
        for j, q in enumerate(P):
            M[i::n, j::n] = mobility_pp(p, q)
    return M

def mobility(P):
    # nice: M is of the form Mijkl = Aij * Ikl + Bij * Rijkl
    # where Ikl, Rijkl are easily formed
    # obtain vector representations of positions, radii
    n = len(P)
    a = 1e-9*np.array([p.a for p in P])
    x = 1e-9*np.array([p.x.flatten() for p in P])
    rr = np.sum((x[:, None, :] - x[None, :, :])**2, 2) + 1e-100
    r = np.sqrt(rr)

    A = np.zeros_like(r)
    B = np.zeros_like(r)

    ama = a[:, None] - a[None, :]
    apa = a[:, None] + a[None, :]
    asq = a[:, None]**2 + a[None, :]**2

    case1 = r <= np.abs(ama)
    A[case1] = (1./(6.*np.pi*eta*np.maximum(a[:,None], a[None,:])))[case1]
    B[case1] = 0.

    case2 = ~case1 & (r <= apa)
    C = 1./(6.*np.pi*eta*a[:, None]*a[None, :])
    A[case2] = (C*(0.5*apa - (ama**2 + 3.*r**2)**2/(32.*r**3)))[case2]
    B[case2] = (C*(3.*(ama**2 - r**2)**2/(32.*r**3)))[case2]

    case3 = ~(case1 | case2) # else
    C = 1./(8.*np.pi*eta*r)
    A[case3] = (C*(1. + asq/(3.*r**2)))[case3]
    B[case3] = (C*(1. - asq/r**2))[case3]

    I = np.eye(3)
    R = (x[:, None, :] - x[None, :, :])
    RR = (R[:, :, :, None] * R[:, :, None, :]).transpose(2, 0, 3, 1)
    RR = RR / (rr[None, :, None, :])

    M = (A[None, :, None, :] * I[:, None, :, None]).reshape(3*n, 3*n) + \
        (B[None, :, None, :] * RR).reshape(3*n, 3*n)
    return np.matrix(M)

def f_vectorized(P, f):
    n = len(P)
    F = np.zeros((3*n, 1))
    for i, p in enumerate(P):
        F[i::n] = f(p)
    return F

def f_electric(P):
    n = len(P)
    a = 1e-9*np.array([p.a for p in P])
    apa = a[:, None] + a[None, :]

    x = 1e-9*np.array([p.x.flatten() for p in P])

    R = x[:, None, :] - x[None, :, :]
    r = np.sqrt(np.sum(R**2, 2) + 1e-100)
    R0 = R / (r**3)[:, :, None]

    q = np.array([float(p.charge) for p in P])
    const = qq**2 / (4.*np.pi*eperm*rpermw)

    QQ = q[:, None] * q[None, :]
    F = const * QQ[:, :, None] * R0
    #F[np.diag_indices_from(r)] = 0.
    tooclose = r <= apa
    R0i = R / (np.maximum(a[:, None], a[None, :])**3)[:, :, None]
    F[tooclose] = (const * QQ[:, :, None] * R0i)[tooclose]

    f = np.sum(F, 1).T.reshape(3*n, 1)

    return f

def f_shortrange(P):
    n = len(P)
    a = 1e-9*np.array([p.a for p in P])
    apa = a[:, None] + a[None, :]

    x = 1e-9*np.array([p.x.flatten() for p in P])
    R = x[:, None, :] - x[None, :, :]
    r = np.sqrt(np.sum(R**2, 2)) + 1e-100

    #E0 = apa*1e9*10.*kT # total energy required for r = apa*1.1 --> r = 0
    #E = E0*((r/apa/1.1 - 1.)**2)
    E0 = 1e9*1e1*kT
    cutoff = 1.05
    f = 2./cutoff*E0*np.maximum(1. - r/apa/cutoff, 0.)

    R0 = R / r[:, :, None]
    F = f[:, :, None] * R0
    ff = np.sum(F, 1).T.reshape(3*n, 1)
    return ff

def simulation(P, T=100., dt=1.):
    t = 0.
    while t < T:
        # calculate forces
        force = f_electric(P) + f_shortrange(P)
        brownian = f_vectorized(P, f_brownian)
        M = mobility(P)
        sqM = np.matrix(msqrt(M))

        # determine resulting velocities
        U = M*force + np.sqrt(2.*kT/dt*1e9)*sqM*brownian

        n = len(P)
        # move particles
        for i, p in enumerate(P):
            u = U[i::n]
            p.move(dt*u)

        yield t
        t += dt

# static particles: no forces act on them, are not affected by
#from time import time

def move(P, dt=1., boundaries=()):
    # calculate forces
    force = f_electric(P) + f_shortrange(P)
    brownian = f_vectorized(P, f_brownian)
    #t = time()
    M = mobility(P)
    #print "forming mobility", time() - t
    #t = time()
    sqM = np.matrix(msqrt(M))
    #print "matrix square root", time() - t

    # determine resulting velocities
    Udet = M*force
    Ubro = np.sqrt(2.*kT/dt*1e9)*sqM*brownian
    #U = M*force + np.sqrt(2.*kT/dt*1e9)*sqM*brownian
    U = Udet + Ubro
    #print "P0:", Udet[0], Ubro[0]
    #print "P1:", Udet[1], Ubro[1]
    #print

    n = len(P)
    # move particles
    for i, p in enumerate(P):
        u = U[i::n]
        p.move(dt*u)

    for b in boundaries:
        b.reflect(P)

class ParticleCollection(object):
    def __init__(self, particles=None):
        if particles is None:
            particles = []
        self.P = particles

    def add(self, generate, N):
        i = 0
        while i < N:
            p0 = generate()
            if all(np.linalg.norm(q.x - p0.x) > p0.a + q.a for q in self.P):
                self.P.append(p0)
                i += 1

def ellipse_collection(ax, P):
    xy = np.array([p.x[::2].flatten() for p in P])
    sizes = np.array([p.a for p in P])
    coll = collections.EllipseCollection(sizes, sizes, np.zeros_like(sizes),
               offsets=xy, units='x', facecolors=[p.color for p in P],
               transOffset=ax.transData, alpha=0.7)
    return coll

def panimate(ax, dt, pcoll, patches, boundaries, **kwargs):
    particles = pcoll.P
    coll = ellipse_collection(ax, particles)
    def init():
        return ()
        #ax.clear()
        #ax.add_patch(rect)
        #return (rect, )

    def animate(i):
        if i == 0:
            for patch in patches:
                ax.add_patch(patch)
            ax.add_collection(coll)
        else:
            move(particles, dt, boundaries)
            xy = np.array([p.x[::2].flatten() for p in particles])
            coll.set_offsets(xy)
        return tuple(patches + [coll])

    kwargs = dict(dict(frames=1800, interval=10, blit=True), **kwargs)
    ani = animation.FuncAnimation(ax.figure, animate, init_func=init, **kwargs)
    return ani

def maybe_save(do_save, ani, name):
    if do_save:
        ani.save(HOME + "/presentations/anaday17/" + name, fps=30, dpi=200,
                 writer="ffmpeg_file",
                 savefig_kwargs={"bbox_inches":0, "pad_inches":0},
                 extra_args=['-vcodec', 'libx264'])
    else:
        plt.show()

def random_particle(L, *args, **kwargs):
    x = [(2*np.random.rand() - 1.)*L, 0., (2*np.random.rand() - 1.)*L]
    return Particle(x, *args, **kwargs)

def setup_box(L):
    # ax, patches, boundaries = setup_box(L)
    fig = plt.figure()
    fig.set_size_inches(6, 6)

    ax = plt.axes([0,0,1,1], autoscale_on=False,
                     xlim=(-L, L), ylim=(-L, L))
    ax.set_axis_off()
    rect = plt.Rectangle([-L, -L], 2*L, 2*L, ec='k', lw=3, fc='none')
    box = Box([-L, -L, -L], 2*L, 2*L, 2*L)
    return ax, [rect], [box]


#comparing with if, elif and else
#Python program that checks the value of the boolean
#variable disaster and prints an appropriate comment:

print("hans")
disaster = True
if disaster:
    print('woe')
else:
    print('whee')

#Tests withing tests
#this program checks the true condition and prints out the statement
furry = True
small = True
if furry:
    if small:
        print('its a cat')
    else:
        print('its a bear')
else:
    if small:
        print('its a skink')
    else:
        print('its a human. Or a hariless bear')

#Using elif if there are more than two possibilites
color = "blue"
if color == "red":
     print("its a tomato")
elif color == "green":
     print("its a green pepper")
elif color == "purple":
     print("the color is nice")
else:
     print("no color dey")

##Checking the true value of a list
#this code checks for empty data structures

some_list = []
if some_list:
    print('not empty')
else:
    print('this is empty')

#LOOPS
#while
#simple loop that prints out numbers from 1 to 5
    count = 1
    while count <=5:
        print (count)
        count +=1

#cancelling a loop with break
#we break out of the loop when the character "q" is typed
while True:
    stuff = input("String to capitalize [type q to quit]: ")
    if stuff == "q":
        break
    print(stuff.capitalize())

#skipping ahead with continue
#reading an integer and printing the square if its odd and skips if its even

while True:
    value = input('Integer, please[q to quit]:')
    if value == 'q': # quits the program
        break
    number = int(value)
    if number %2 ==0:
        continue
    print(number, 'squared is', number*number)

#checking break use with else
#checks a list of  numbers to find even.

numbers = [1,3,5,4]
position = 0
while position < len(numbers):
    number = numbers[position]
    if number % 2 == 0:
        print('found even number', number)
        break
    position +=1
else:
    print('no even number found')

#Printing the letters in a word(cat)
word = 'cat'
for letters in word:
    print(letters)

#iterating throughh the keys of a dictionary and printing out the keys

hans = {'name':'kyei','age':'90','school':'ucc','country':'ghana'}
for value in hans.values():
    print( value)

#using the else in a for loop to verify that the for loop ran to completion

cheeses =  ['hans']
for cheese in cheeses:
    print ('THis shop  has some lovely',cheese)
    break
else:
    print('this is not much of cheese, is it?')

#comprehensions
#list comprehension
number_list = [number for number in range(1,6)]
print(number_list)

# A list of only odd numbers between 1 and 5 using comprehensions
the_list = [number for number in range(1,6) if number %2 == 1]
print(the_list)

#alternative method
odd_list = []
for number in range(1,6):
    if number % 2 ==1:
        odd_list.append(number)
print(odd_list)

#funtions
def do_nothing():
    return True

#this function returns statement to send the value of anuthing back to its caller twice
def echo(anything):
    return anything + ' ' + anything

# this function takes the a color and returns a string that matches with the condition
def message(color):
    if color == 'red':
        return "its a tomato"
    elif color == "green":
        return "green pepper"
    elif color == "purple":
        return "nice color"
    else:
        return "ive never heard of a color" + " " + color + "."

#A function that prints whether a statement is none:
def is_none(thing):
    if thing is None:
        print('its None')
    elif thing:
        print('its true')
    else:
        print('its false')

#positional arguments
#returns the key and value
def menu(wine,entree,dessert):
    return {'wine':wine, 'entree':entree, 'dessert':dessert}

#a function that is expected to run each time with afreshh empty results , add
#argument to it and print a single item list.

def buggy(arg,result=[]):
    result.append(arg)
    print(result)

#gathering positional arguments with *
def print_more(required1,required2,*args):
    print ('need this one:',required1)
    print ('need thisone too:',required2)
    print('all the rest:',args)

#funtions with arguments
    #the function takes two numbers , adds them and prints them out
def add_args(arg1, arg2):
    print(arg1 + arg2)

#this funtion takes the add_args function and excutes with the values passed
def run_something_with_args(func,arg1,arg2):
    func(arg1,arg2)

#combining the previous fucntion with the *args and **kwargs

def sum_args(*args):
    return sum (args)
def run_with_positional_args(func,*args):
    return func(*args)

#functions inside other functions
def outer_func(a,b):
    def inner_func(c,d):
        return c + d
    return inner_func(a,b)

#the use of the lambda functions
#the function capitalizes the list of words and appends an exclamation point

def edit_story(words,func):
    for word in words:
        print(func(word))
stairs = ['thud','meow','thud','hiss']
def enliven(word):
    return word.capitalize() + "!"
def upper(word):
    return word.upper() + "@"

#using the lambda in place of the enliven function
# edit_story(stairs, lambda word: word.capitalize() + '!')

#DECORATORS
def document_it(func):
    def new_func(*args,**kwargs):
        print('Runnning function:',func._name_)
        print('positional arguments:',args)
        print('keyword arguments:',kwargs)
        result = func(*args, **args)
        print('result:',result)
        return result

#catching errors using try and except
short_list = [1, 2, 3]
while True:
    value = input('Position [q to quit]? ')
    if value == 'q':
        break
    try:
        position = int(value)
        print(short_list[position])
    except IndexError as err:
        print('Bad index:', position)
    except Exception as other:
        print('Something else broke:', other)


from tkinter import *

root = Tk()

canvas = Canvas(root, width='300', height='300')
canvas.pack()
def draw_square(row,coloumn,color):
    square = canvas.create_rectangle(row,coloumn,row+60,coloumn+60,fill = color)
    return square
# fill the canvas with a checkerboard pattern.

numbers = [0,60,120,180,240]

for i in range(0,5):
    for j in range(0,5):
        if (i%2 == 0) and (j%2 == 0):
            draw_square(numbers[i],numbers[j],"black")
        elif (i%2 != 0) and (j%2 != 0):
            draw_square(numbers[i],numbers[j],"black")

# draw_square(0,0,"white")
# draw_square(60,0,"white")
# draw_square(120,0,"white")
# draw_square(180,0,"white")
# draw_square(240,0,"white")

root.mainloop()


info = {'Seoul': 0, 'Yeongdeungpo': 9.1, 'Anyang': 23.9, 'Suwon': 41.5, 'Osan': 56.5, 'Seojeongri': 66.5, 'Pyeongtaek': 75
      , 'Seonghwan': 84.4, 'Cheonan': 96.6, 'Sojeongni': 107.4, 'Jeonui': 114.9, 'Jochiwon': 129.3, 'Bugang': 139.8, 'Sintanjin': 151.9
      , 'Daejeon': 166.3, 'Okcheon': 182.5, 'Iwon': 190.8, 'Jitan': 196.4, 'Simcheon': 200.8, 'Gakgye': 204.6, 'Yeongdong': 211.6, 'Hwanggan': 226.2
      , 'Chupungnyeong': 234.7, 'Gimcheon': 253.8, 'Gumi': 276.7, 'Sagok': 281.3, 'Yangmok': 289.5, 'Waegwan': 296, 'Sindong': 305.9, 'Daegu': 323.1
      , 'Dongdaegu': 326.3, 'Gyeongsan': 338.6, 'Namseonghyeon': 353.1, 'Cheongdo': 361.8, 'Sangdong': 372.2, 'Miryang': 381.6, 'Samnangjin': 394.1
      , 'Wondong': 403.2, 'Mulgeum': 412.4, 'Hwamyeong': 421.8, 'Gupo': 425.2, 'Sasang': 430.3, 'Busan': 441.7}

[N, Q] = list(map(int, input().split()))

def timeToHour(time):
    if time == '-:-':
        return 0;
    [hour, minute] = list(map(int, time.split(':')))
    return (hour * 60) + minute

schedule = {}
for _ in range(N):
    [station, arrival, departure] = input().split()
    schedule[station] = [timeToHour(arrival), timeToHour(departure)]

sections = [input().split() for _ in range(Q)]

for [start, end] in sections:
    distance = abs(info[end] - info[start])
    duration = ((1440 + schedule[end][0] - schedule[start][1]) % 1440) / 60

    print(distance / duration)


from unittest import mock

import pytest

from paasng.engine.controller.cluster import RegionClusterService

pytestmark = pytest.mark.django_db

@pytest.fixture(autouse=True)
def helper(bk_app):
    # Make a mocked controller client
    mocked_client = mock.Mock()
    mocked_client.list_region_clusters.return_value = [
        {
            "name": "default",
            "is_default": True,
            "bcs_cluster_id": "BCS-K8S-10000",
            "support_bcs_metrics": False,
            "ingress_config": {
                "app_root_domains": [{"name": "local-bkapps-t.example.com"}],
            },
        },
        {
            "name": "extra-1",
            "is_default": False,
            "bcs_cluster_id": "BCS-K8S-10001",
            "support_bcs_metrics": False,
            "ingress_config": {
                "app_root_domains": [{"name": "local-bkapps-extra.example.com"}],
            },
        },
    ]
    return RegionClusterService(bk_app, client=mocked_client)

class TestGetEngineAppCluster:
    def test_empty_cluster_field(self, bk_stag_env, helper):
        helper.client.retrieve_app_config.return_value = {'cluster': ''}

        engine_app = bk_stag_env.engine_app
        assert helper.get_engine_app_cluster(engine_app.name).name == 'default'

    def test_valid_cluster_field(self, bk_stag_env, helper):
        helper.client.retrieve_app_config.return_value = {'cluster': 'extra-1'}

        engine_app = bk_stag_env.engine_app
        assert helper.get_engine_app_cluster(engine_app.name).name == 'extra-1'

    def test_invalid_cluster_field(self, bk_stag_env, helper):
        helper.client.retrieve_app_config.return_value = {'cluster': 'invalid-cluster-name'}

        engine_app = bk_stag_env.engine_app
        with pytest.raises(ValueError):
            helper.get_engine_app_cluster(engine_app.name)

def test_get_cluster(helper):
    assert helper.get_cluster('extra-1').name == 'extra-1'


n=int(input())

flag=True
for i in range(n):
    ans=i
    s=str(i)
    for j in s:
        ans+=int(j)
    if ans==n: print(i); flag=False;break;
if flag: print(0)


from openmdao.api import Problem, ScipyOptimizeDriver, ExecComp, IndepVarComp

# We'll use the component that was defined in the last tutorial
from openmdao.test_suite.components.paraboloid import Paraboloid

# build the model
prob = Problem()
indeps = prob.model.add_subsystem('indeps', IndepVarComp())
indeps.add_output('x', 3.0)
indeps.add_output('y', -4.0)

prob.model.add_subsystem('parab', Paraboloid())

# define the component whose output will be constrained
prob.model.add_subsystem('const', ExecComp('g = x + y'))

prob.model.connect('indeps.x', ['parab.x', 'const.x'])
prob.model.connect('indeps.y', ['parab.y', 'const.y'])

# setup the optimization
prob.driver = ScipyOptimizeDriver()
prob.driver.options['optimizer'] = 'COBYLA'

prob.model.add_design_var('indeps.x', lower=-50, upper=50)
prob.model.add_design_var('indeps.y', lower=-50, upper=50)
prob.model.add_objective('parab.f_xy')

# to add the constraint to the model
prob.model.add_constraint('const.g', lower=0, upper=10.)
# prob.model.add_constraint('const.g', equals=0.)

prob.setup()
prob.run_driver()

# minimum value
print(prob['parab.f_xy'])

# location of the minimum
print(prob['indeps.x'])
print(prob['indeps.y'])


import tornado.ioloop
import tornado.web
from tornado import gen
from time import sleep
from datetime import datetime
from tornado.ioloop import IOLoop, PeriodicCallback

loop = IOLoop.current()

class MainHandler(tornado.web.RequestHandler):
    async def get(self):
        await gen.sleep(1)
        self.write('Hello, world')

def background_task():
    print(datetime.now())

if __name__ == '__main__':
    application = tornado.web.Application(
        [
            (r'/', MainHandler),
        ]
    )
    application.listen(8888)
    task = PeriodicCallback(background_task, 1000)
    task.start()
    loop.start()


from enum import Enum

# noinspection PyArgumentList
TYPE = Enum('TYPE', 'stimulus measurement scan_config analysis emka', module=__name__)

FILE_TYPES = {
    TYPE.stimulus: ('.mat', [('matlab', '.mat'), ('json', '.json'), ('python', '.pkl')]),
    TYPE.measurement: ('.csv', [('table', '.csv'), ('matlab', '.mat'), ('python', '.pkl')]),
    TYPE.scan_config: ('.cfg', [('scanImage', '.cfg')]),
    TYPE.analysis: ('.csv', [('data frame', '.pandas'), ('table', '.csv'), ('python', '.pkl')]),
    TYPE.emka: ('.raw', [('raw file', '.raw'), ('raw file txt', '.txt')])}


'''
Given an array of n integers nums and an integer target, find the number of index triplets i, j, k with
0 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target.

Follow up: Could you solve it in O(n2) runtime?

Example 1:
Input: nums = [-2,0,1,3], target = 2
Output: 2
Explanation: Because there are two triplets which sums are less than 2:
[-2,0,1]
[-2,0,3]

Example 2:
Input: nums = [], target = 0
Output: 0

Example 3:
Input: nums = [0], target = 0
Output: 0
'''
'''
Method below uses 2 pointers.
Time:O(n**2)
Space:O(1)
'''
class Solution(object):
    def threeSumSmaller(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        nums.sort()
        res = 0
        for n in range(len(nums)):
            i, j = n+1, len(nums)-1
            while i<j:
                if nums[i]+nums[j]+nums[n] >= target:
                    j -= 1
                else:
                    res += j-i
                    i += 1
        return res


"""
말할 숫자들을 하나의 string으로 쭉 늘어놓고,
자기 순서가 될 때, 즉, idx % m == p - 1 일 때만 결과 ret에 붙여주는 식으로 구현
여기서 말할 숫자의 개수를 설정할 때 조금 애를 먹었는데, 문제에서 제시되는 충분히 큰 값인 t^2 값을 설정함으로 해결.
"""

def convert(n, base):
    T = "0123456789ABCDEF"
    q, r = divmod(n, base)
    if q == 0:
        return T[r]
    else:
        return convert(q, base) + T[r]

def solution(n, t, m, p):
    num_str = ""
    ret = ""
    for i in range(t**2):
        num_str += convert(i, n)

    for idx, j in enumerate(num_str):
        if t == len(ret):
            return ret
        if idx % m == p - 1:
            ret += j
    return ret


import imgaug as ia
from imgaug import augmenters as iaa

def get():
    def sometimes(aug): return iaa.Sometimes(0.5, aug)

    return iaa.Sequential(
        [
            # apply the following augmenters to most images
            iaa.Fliplr(0.5),  # horizontally flip 50% of all images
            iaa.Flipud(0.2),  # vertically flip 20% of all images
            # crop images by -5% to 10% of their height/width
            sometimes(iaa.CropAndPad(
                percent=(-0.05, 0.1),
                pad_mode=ia.ALL,
                pad_cval=(0, 255)
            )),
            sometimes(iaa.Affine(
                # scale images to 80-120% of their size, individually per axis
                scale={'x': (0.8, 1.2), 'y': (0.8, 1.2)},
                # translate by -20 to +20 percent (per axis)
                translate_percent={'x': (-0.2, 0.2), 'y': (-0.2, 0.2)},
                rotate=(-45, 45),  # rotate by -45 to +45 degrees
                shear=(-16, 16),  # shear by -16 to +16 degrees
                # use nearest neighbour or bilinear interpolation (fast)
                order=[0, 1],
                # if mode is constant, use a cval between 0 and 255
                cval=(0, 255),
                # use any of scikit-image's warping modes (see 2nd image from the top for examples)
                mode=ia.ALL
            )),
            # execute 0 to 5 of the following (less important) augmenters per image
            # don't execute all of them, as that would often be way too strong
            iaa.SomeOf((0, 5),
                       [
                # convert images into their superpixel representation
                sometimes(iaa.Superpixels(
                    p_replace=(0, 1.0), n_segments=(20, 200))),
                iaa.OneOf([
                    # blur images with a sigma between 0 and 3.0
                    iaa.GaussianBlur((0, 3.0)),
                    # blur image using local means with kernel sizes between 2 and 7
                    iaa.AverageBlur(k=(2, 7)),
                    # blur image using local medians with kernel sizes between 2 and 7
                    iaa.MedianBlur(k=(3, 11)),
                ]),
                iaa.Sharpen(alpha=(0, 1.0), lightness=(
                    0.75, 1.5)),  # sharpen images
                iaa.Emboss(alpha=(0, 1.0), strength=(
                    0, 2.0)),  # emboss images
                # search either for all edges or for directed edges,
                # blend the result with the original image using a blobby mask
                iaa.SimplexNoiseAlpha(iaa.OneOf([
                    iaa.EdgeDetect(alpha=(0.5, 1.0)),
                    iaa.DirectedEdgeDetect(
                        alpha=(0.5, 1.0), direction=(0.0, 1.0)),
                ])),
                # add gaussian noise to images
                iaa.AdditiveGaussianNoise(loc=0, scale=(
                    0.0, 0.05*255), per_channel=0.5),
                iaa.OneOf([
                    # randomly remove up to 10% of the pixels
                    iaa.Dropout((0.01, 0.1), per_channel=0.5),
                    iaa.CoarseDropout((0.03, 0.15), size_percent=(
                        0.02, 0.05), per_channel=0.2),
                ]),
                # invert color channels
                iaa.Invert(0.05, per_channel=True),
                # change brightness of images (by -10 to 10 of original value)
                iaa.Add((-10, 10), per_channel=0.5),
                # change hue and saturation
                iaa.AddToHueAndSaturation((-20, 20)),
                # either change the brightness of the whole image (sometimes
                # per channel) or change the brightness of subareas
                iaa.OneOf([
                    iaa.Multiply((0.5, 1.5), per_channel=0.5),
                    iaa.FrequencyNoiseAlpha(
                        exponent=(-4, 0),
                        first=iaa.Multiply((0.5, 1.5), per_channel=True),
                        second=iaa.ContrastNormalization((0.5, 2.0))
                    )
                ]),
                # improve or worsen the contrast
                iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5),
                iaa.Grayscale(alpha=(0.0, 1.0)),
                # move pixels locally around (with random strengths)
                sometimes(iaa.ElasticTransformation(
                    alpha=(0.5, 3.5), sigma=0.25)),
                # sometimes move parts of the image around
                sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))),
                sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1)))
            ],
                random_order=True
            )
        ],
        random_order=True
    )


"""
Python script to scrape books from `Project Gutenberg` eBook library
website: https://www.gutenberg.org/
"""

# =====================================================================
# Import
# =====================================================================

# import internal modules
import re
import requests
import time
import random

# import 3rd-party modules
from bs4 import BeautifulSoup as bs
import pandas as pd

# import local modules

# =====================================================================
# Functions
# =====================================================================

def download_page(url):
    """
    Function to
    - download page
    - and parse it with BeautifulSoup
    """
    # download page
    response = requests.get(url)
    print(url, response.status_code)

    # parse page
    soup = bs(response.content, features="lxml")

    return soup

def get_book_links(soup) -> pd.DataFrame:
    """
    Function to get links of the first 25 books
    from search page
    """
    # create empty dataframe
    cols = ["title", "author", "link", "book_id"]
    books_df = pd.DataFrame(columns=cols)

    # define base url for links
    base_url = "https://www.gutenberg.org"

    # scrape info
    for rank, element in enumerate(soup.find_all("li", attrs={"class": "booklink"}), start=1):
        title = element.find("span", attrs={"class": "title"}).text
        author = element.find("span", attrs={"class": "subtitle"}).text
        link = base_url + element.find("a", attrs={"class": "link"}).get("href")
        # cover_img_link = base_url + element.find("img", attrs={"class": "cover-thumb"}).get("src")
        book_id = re.findall(r"\d+", link)[0]
        # utf_8_txt_link = f"{base_url}/files/{book_id}/{book_id}-0.txt"
        books_df.loc[rank] = [title, author, link, book_id]

    return books_df

def get_book_text(link):
    """
    Function to get book text in html
    """
    # download book page
    soup = download_page(link)

    # define base url for links
    base_url = "https://www.gutenberg.org"

    # scrape book html link
    book_html_link = base_url + soup.find("a", attrs={"class": "link"}, href=re.compile(r".htm")).get("href")

    # slow down requests frequency to avoid IP ban
    time.sleep(random.uniform(2.0, 3.0))

    # download book page
    response = requests.get(book_html_link)
    print(book_html_link, response.status_code)

    # scrape
    soup = bs(response.text, 'html.parser')
    tags = soup.find_all(['h4', 'p'])
    text = [tag.text for tag in tags]
    book = ' '.join(text)

    # return book text
    return book

def search_books(search) -> pd.DataFrame:
    """
    Function to search for books
    """

    # prepare search query url in required format
    search_book_url = "https://www.gutenberg.org/ebooks/search/?query="
    search_book_url += "+".join(search.split(" "))

    # download page
    soup = download_page(search_book_url)

    return get_book_links(soup)

def get_most_popular_books() -> pd.DataFrame:
    """
    Function to get links of the 25 most popular books
    (most downloaded from `Project Gutenberg`)
    """
    # download page of most popular books
    url='https://www.gutenberg.org/ebooks/search/?sort_order=downloads'
    soup = download_page(url)

    return get_book_links(soup)

# ============================================================
# Main Function
# ============================================================

def main():
    """
    Main function
    """
    # input search query
    # search = input("search for books, authors, genre, ...")

    popular_books = get_most_popular_books()
    print(popular_books)
    book_link = popular_books.loc[1, "link"]
    print(book_link)
    book_text = get_book_text(book_link)
    # book_text = get_book_text("https://www.gutenberg.org/ebooks/31547")

    # find index position where metadata from website ends
    # metadata_end_idx = book_text.rfind("***",0,1000)
    # print(book_text[metadata_end_idx: 1000])

# =====================================================================
# Run
# =====================================================================

# execute main() if you directly run this program
if __name__ == '__main__':
    main()


# Create a program that asks the user to enter their name and their age. Print
# out a message addressed to them that tells them the year that they will turn
# 100 years old.

name = input("Type your name: ").capitalize()
age = int(input("Type your age: "))
birthday = int(input("Type your birthday: "))

yearNow = 2017
ageNow = (yearNow - birthday)
age100 = ((yearNow - age) + 100)
print("You have %d years old." % ageNow)
print("%s you will be 100 year old in %d." % (name, age100))


import sys
import unittest

import soundfile

from porcupine import Porcupine
from util import *

class PorcupineTestCase(unittest.TestCase):
    @staticmethod
    def __append_language(s, language):
        if language == 'en':
            return s
        return f'{s}_{language}'

    @classmethod
    def __pv_model_path_by_language(cls, relative, language):
        model_path_subdir = cls.__append_language('lib/common/porcupine_params', language)
        return os.path.join(os.path.dirname(__file__), relative, f'{model_path_subdir}.pv')

    @classmethod
    def __pv_keyword_paths_by_language(cls, relative, language):
        keyword_files_root = cls.__append_language('resources/keyword_files', language)
        keyword_files_dir = \
            os.path.join(os.path.dirname(__file__), relative, keyword_files_root, pv_keyword_files_subdir())

        res = dict()
        for x in os.listdir(keyword_files_dir):
            res[x.rsplit('_')[0]] = os.path.join(keyword_files_dir, x)

        return res

    def run_porcupine(self, language, keywords, ground_truth, audio_file_name=None):
        if audio_file_name is None:
            _audio_file_name_prefix = self.__append_language('multiple_keywords', language)
            audio_file_name = f'{_audio_file_name_prefix}.wav'
        keyword_paths = list()
        for x in keywords:
            keyword_paths.append(self.__pv_keyword_paths_by_language('../..', language)[x])

        porcupine = Porcupine(
            access_key=sys.argv[1],
            library_path=pv_library_path('../..'),
            model_path=self.__pv_model_path_by_language('../..', language),
            keyword_paths=keyword_paths,
            sensitivities=[0.5] * len(keyword_paths))

        audio, sample_rate = soundfile.read(
            os.path.join(os.path.dirname(__file__), '../../resources/audio_samples/', audio_file_name),
            dtype='int16')
        assert sample_rate == porcupine.sample_rate

        num_frames = len(audio) // porcupine.frame_length
        results = []
        for i in range(num_frames):
            frame = audio[i * porcupine.frame_length:(i + 1) * porcupine.frame_length]
            result = porcupine.process(frame)
            if result >= 0:
                results.append(result)

        porcupine.delete()

        self.assertEqual(results, ground_truth)

    def test_single_keyword(self):
        self.run_porcupine(
            language='en',
            keywords=['porcupine'],
            ground_truth=[0],
            audio_file_name='porcupine.wav')

    def test_multiple_keywords(self):
        keywords = [
            'americano', 'blueberry', 'bumblebee', 'grapefruit',
            'grasshopper', 'picovoice', 'porcupine', 'terminator'
        ]
        self.run_porcupine(
            language='en',
            keywords=keywords,
            ground_truth=[6, 0, 1, 2, 3, 4, 5, 6, 7])

    def test_single_keyword_es(self):
        self.run_porcupine(
            language='es',
            keywords=['manzana'],
            ground_truth=[0],
            audio_file_name='manzana.wav')

    def test_multiple_keywords_es(self):
        self.run_porcupine(
            language='es',
            keywords=['emparedado', 'leopardo', 'manzana'],
            ground_truth=[0, 1, 2])

    def test_single_keyword_de(self):
        self.run_porcupine(
            language='de',
            keywords=['heuschrecke'],
            ground_truth=[0],
            audio_file_name='heuschrecke.wav')

    def test_multiple_keywords_de(self):
        self.run_porcupine(
            language='de',
            keywords=['ananas', 'heuschrecke', 'leguan', 'stachelschwein'],
            ground_truth=[0, 1, 2, 3])

    def test_single_keyword_fr(self):
        self.run_porcupine(
            language='fr',
            keywords=['mon chouchou'],
            ground_truth=[0],
            audio_file_name='mon_chouchou.wav')

    def test_multiple_keywords_fr(self):
        self.run_porcupine(
            language='fr',
            keywords=['framboise', 'mon chouchou', 'parapluie'],
            ground_truth=[0, 1, 0, 2])

if __name__ == '__main__':
    if len(sys.argv) != 2:
        print("usage: test_porcupine.py ${AccessKey}")
        exit(1)

    unittest.main(argv=sys.argv[:1])