#!/usr/bin/env python3
from os import walk, getcwd, path
from pathlib import PurePath

# ignore all of directories, include root level
IGNORE_DIRS = ['.git','bin','lib','include']

# ignore all files mathes
IGNORE_FILES = ['pyvenv.cfg','.DS_Store','.gitignore']

rootDir = getcwd()

result = []
for root, dir, files in walk(rootDir):
    dir.sort()
    files.sort()
    match = path.basename(root)
    if match in IGNORE_DIRS:
        dir[:] = [] # ignore sub
        continue

    times = len(PurePath(root[len(rootDir)-1:]).parts)
    result.append('|\t' * (times-1) + '├── %s\n' %match)

    for fname in files:
        pre = '|\t' * times
        recordPos = pre
        if fname not in IGNORE_FILES:
            result.append(pre+'├── %s\n' %fname)
dirs = ''.join(result)

print(dirs)


"""
This function will be triggered by Cloudwatch Custom Widget lambda function. You can also trigger resource-controller function manually with below event.
To stop ECS and EKS resources: {'Action':'disable'}
To start ECS and EKS resources: {'Action':'enable'}
"""

import os
import boto3
import time

ecs_client = boto3.client('ecs')
eks_client = boto3.client('eks')

EKS_CLUSTER_NAME = os.environ['EKS_CLUSTER_NAME']
ECS_CLUSTER_ARNS = os.environ['ECS_CLUSTER_ARNS'].split(",")

def set_ecs_desired_task_count_to_zero(cluster):
    """Function to set ECS Desired Tasks counts to 0 for all ECS Cluster Services"""
    services = ecs_client.list_services(
        cluster = cluster
        )
    for service in services['serviceArns']:
        update_response = ecs_client.update_service(
            cluster = cluster,
            service = service,
            desiredCount = 0)
    return 0

def set_ecs_desired_task_count_to_normal(cluster):
    """Function to set ECS Desired Tasks counts to 2 for all ECS Cluster Services except trafficgeneratorservice"""
    services = ecs_client.list_services(
        cluster = cluster
        )
    for service in services['serviceArns']:
        desired_count = 2
        if 'trafficgeneratorserviceecsservice' in service:
            desired_count = 1
        update_response = ecs_client.update_service(
            cluster = cluster,
            service = service,
            desiredCount = desired_count)
    return 0

def manageECSTasks(status):
    """Enable/Disable ECS Tasks"""
    clusters = ecs_client.list_clusters()
    for cluster in clusters['clusterArns']:
        if cluster in ECS_CLUSTER_ARNS:
            if status == "enable":
                set_ecs_desired_task_count_to_normal(cluster)
            elif status == "disable":
                set_ecs_desired_task_count_to_zero(cluster)
    if status == "enable":
        return "ECS Tasks Count set back to Normal"
    else:
        return "ECS Tasks Count set to Zero"

def waitTillUpdateCompletes(update_response,nodegroup_name):
    """Waiter function to wait till EKS cluster accepts the update"""
    update_in_progress = True
    while update_in_progress:
        update_status = eks_client.describe_update(
            name = EKS_CLUSTER_NAME,
            updateId = update_response['update']['id'],
            nodegroupName = nodegroup_name
        )
        if update_status['update']['status'] == 'InProgress':
            time.sleep(5)
        else:
            update_in_progress = False

def setEKSNodeGroupCountToNormal(nodegroup_name):
    """Function to set EKS nodegroup node desired count to 2"""
    update_response = eks_client.update_nodegroup_config(
        clusterName=EKS_CLUSTER_NAME,
        nodegroupName=nodegroup_name,
        scalingConfig={
            'minSize': 2,
            'desiredSize': 2
        },
    )
    waitTillUpdateCompletes(update_response,nodegroup_name)

def setEKSNodeGroupCountToZero(nodegroup_name):
    """Function to set EKS nodegroup node desired count to 0"""
    update_response = eks_client.update_nodegroup_config(
        clusterName=EKS_CLUSTER_NAME,
        nodegroupName=nodegroup_name,
        scalingConfig={
            'minSize': 0,
            'desiredSize': 0
        },
    )
    waitTillUpdateCompletes(update_response,nodegroup_name)

def manageEKSNodes(status):
    """Enable/Disable the EKS nodegroup Count"""
    nodegroups = eks_client.list_nodegroups(
        clusterName=EKS_CLUSTER_NAME)
    for group in nodegroups['nodegroups']:
        if EKS_CLUSTER_NAME.lower()+'nodegroup' in group.lower():
            nodegroup_name = group
            break
    if status == "enable":
        setEKSNodeGroupCountToNormal(nodegroup_name)
        return "EKS Nodegroup Count set back to Normal"
    elif status == "disable":
        setEKSNodeGroupCountToZero(nodegroup_name)
        return "EKS Nodegroup Count set to Zero"

def lambda_handler(event, context):
    """Main function to control ECS and EKS resources"""
    output1 = manageECSTasks(event['Action'])
    output2 = manageEKSNodes(event['Action'])
    return output1 + ', '+ output2


## standard imports
import argparse
import numpy as np
import pulp

## argument parser
parser = argparse.ArgumentParser()

## algorithm solver class
class planner():
    ## init
    def __init__(self, mdppath, algorithm, policy = None) :
        self.mdp = self.extractmdp(mdppath)
        if policy == None:
            if algorithm == "vi":
                self.valuefunc, self.policy = self.valueiteration()
            elif algorithm == "hpi":
                self.valuefunc, self.policy = self.howardpolicyiteration()
            else:
                self.valuefunc, self.policy = self.linearprogramming()
        else:
            self.policy = self.extractpolicy(policy)
            self.valuefunc = self.evalpolicy(self.policy, self.mdp["noofstates"])
        self.printoutput()

    ## function to extract mdp from the text file
    def extractmdp(self, mdppath):
        mdp = dict()
        lines = open(mdppath).read().strip()
        lines = lines.split("\n")
        for line in lines:
            l = line.split()
            if l[0] == "transition":
                s1 = int(l[1])
                ac = int(l[2])
                s2 = int(l[3])
                r = float(l[4])
                p = float(l[5])
                mdp["transitionfunc"][s1][ac][s2] = p
                mdp["rewardfunc"][s1][ac][s2] = r
            elif l[0] == "numStates":
                mdp["noofstates"] = int(l[1])
            elif l[0] == "numActions":
                mdp["noofactions"] = int(l[1])
                ## initialising transition and reward funtion
                mdp["transitionfunc"] = np.zeros((mdp["noofstates"], mdp["noofactions"], mdp["noofstates"]))
                mdp["rewardfunc"] = np.zeros((mdp["noofstates"], mdp["noofactions"], mdp["noofstates"]))
            elif l[0] == "mdptype":
                mdp["mdptype"] = l[1]
            elif l[0] == "discount":
                mdp["discountfactor"] = float(l[1])
            elif l[0] == "end":
                pass
        return mdp

    ## function to extract policy
    def extractpolicy(self, policypath):
        policy = []
        lines = open(policypath).read().strip()
        lines = lines.split("\n")
        for line in lines:
            policy.append(int(line))
        return np.array(policy)

    ## function for value iteration
    def valueiteration(self,):
        noofstates = int(self.mdp["noofstates"])
        v = np.zeros(noofstates)        ## initial value function
        vdash = np.zeros(noofstates)    ## value function after applying the operator
        while(True):                    ## iterating until all are within a tolerance of 1e-8 since precision is only upto 6
            vdash = np.sum(self.mdp["transitionfunc"]*(self.mdp["rewardfunc"] + self.mdp["discountfactor"]*v), axis=2)
            vdash = np.max(vdash, axis = 1)
            if(np.sum(vdash - v < 1e-8) == noofstates ):
                break
            v = vdash
        policy = np.sum(self.mdp["transitionfunc"]*(self.mdp["rewardfunc"] + self.mdp["discountfactor"]*v), axis=2)
        policy = np.argmax(policy, axis = 1)
        return vdash, policy

    ## function for linear programming
    def linearprogramming(self,):
        lpsolver = pulp.LpProblem('linearprogformdp', pulp.LpMaximize)    ## the linear program solver
        noofstates = self.mdp["noofstates"]
        noofactions = self.mdp["noofactions"]
        vars = ["v" + str(i) for i in range(0, noofstates)]              ## creating the linear programs variables's names
        v = [pulp.LpVariable(i) for i in vars]                ## converting them into pulp variables
        v = np.array(v)                                     ## putting them into numpy array to make calculation easier later
        lpsolver += (-pulp.lpSum(v))                           ## the objective function

        for s in range(0, noofstates):
            for a in range(0, noofactions):
                lpsolver += v[s] >= pulp.lpSum(self.mdp["transitionfunc"][s][a]*(self.mdp["rewardfunc"][s][a] + self.mdp["discountfactor"]*v))

        lpsolver.solve(pulp.PULP_CBC_CMD(msg=False))        ## to make pulp print nothing on the terminal

        valfunc = []
        for i in range(0, noofstates):
            valfunc.append(pulp.value(v[i]))
        valfunc = np.array(valfunc)
        policy = np.sum(self.mdp["transitionfunc"]*(self.mdp["rewardfunc"] + self.mdp["discountfactor"] * valfunc), axis=2)
        policy = np.argmax(policy, axis = 1)
        return valfunc, policy

    ## function for howard policy iteration
    def howardpolicyiteration(self,):
        noofstates = self.mdp["noofstates"]
        noofactions = self.mdp["noofactions"]
        policy = np.random.randint(low=0, high=noofactions, size=noofstates)            ## random initialisation of actions for the states
        policydash = np.random.randint(low=0, high=noofactions, size=noofstates)
        valfunc = None
        while(True):
            valfunc = self.evalpolicy(policy, noofstates)       ## doing policy evaluation
            policydash = np.sum(self.mdp["transitionfunc"]*(self.mdp["rewardfunc"] + self.mdp["discountfactor"]*valfunc), axis=2)
            policydash = np.argmax(policydash, axis = 1)
            if np.array_equal(policydash, policy):
                break
            policy = policydash
        return valfunc, policydash

    ## function for evaluating given policy
    def evalpolicy(self, policy, noofstates):
        statearray = np.arange(noofstates)
        transitionfunc = self.mdp["transitionfunc"][statearray,policy]
        rewardfunc = self.mdp["rewardfunc"][statearray,policy]
        tr = np.sum(transitionfunc*rewardfunc, axis=1)
        tr = np.reshape(tr,(tr.shape[0],1))
        invmat = np.linalg.inv(np.identity(noofstates) - self.mdp["discountfactor"]*transitionfunc)
        v = np.matmul(invmat, tr)
        v = np.reshape(v,(v.shape[0]))
        return v

    ## function to print the output
    def printoutput(self,):
        for i in range(0,self.mdp["noofstates"]):
            print('{:.6f}'.format(round(self.valuefunc[i], 6))," ",self.policy[i])

if __name__ == "__main__":

    ## parsing args
    parser.add_argument("--mdp",type=str)
    parser.add_argument("--algorithm",type=str,default="vi")
    parser.add_argument("--policy",type=str)
    args = parser.parse_args()

    ## calling planner to solve
    planner(args.mdp, args.algorithm,args.policy)


import turtle

window = turtle.Screen()
window.setup(500, 500)

'''for checking'''
window.tracer(0)

Nhong1 = turtle.Turtle()

def re_center():

    Nhong1.penup()
    Nhong1.goto(0, 0)
    Nhong1.pendown()
    Nhong1.setheading(90)

def draw_circle():
    colors = ["blue", "pink", "orange"]

    Nhong1.pensize(5)
    Nhong1.color("purple")
    Nhong1.circle(150)

    Nhong1.penup()
    Nhong1.goto(0, 0)
    Nhong1.pendown()
    Nhong1.speed(50)
    Nhong1.pensize(1)

    """Bg"""
    for index in range(155):
        Nhong1.pencolor(colors[index % 3])
        Nhong1.forward(index)
        Nhong1.right(70)

def draw_line():

    Nhong1.penup()
    '''put turtle to right side of circle'''
    Nhong1.goto(130, 0)
    Nhong1.setheading(0)
    Nhong1.pensize(5)
    Nhong1.color("purple")
    Nhong1.speed(2)

    for index in range(0, 12):
        Nhong1.pendown()
        Nhong1.backward(20)
        Nhong1.penup()
        Nhong1.goto(0, 0)
        Nhong1.rt(30)
        Nhong1.forward(130)

    Nhong1.pensize(3)
    Nhong1.goto(130, 0)
    Nhong1.setheading(0)

    for index in range(0, 24):
        Nhong1.pendown()
        Nhong1.backward(10)
        Nhong1.penup()
        Nhong1.goto(0, 0)
        Nhong1.rt(15)
        Nhong1.forward(130)

def draw_time(h, m, s):

    m += s/60
    h = (m/60) + (h % 12)

    Nhong1.pensize(3)

    '''hour'''
    re_center()
    Nhong1.rt(h*30)
    Nhong1.forward(80)

    '''minute'''
    re_center()
    Nhong1.rt(m*6)
    Nhong1.forward(100)

    '''second'''
    Nhong1.color("red")
    re_center()
    Nhong1.rt(s*6)
    Nhong1.forward(100)

def draw_clock():

    """Set position of turtle"""
    Nhong1.penup()
    Nhong1.goto(0, -150)
    Nhong1.pendown()

    # draw_circle()
    draw_line()

    hour, minute, second = input("Enter time: ").split(":")
    hour, minute, second = int(hour), int(minute), int(second)

    draw_time(hour, minute, second)
    turtle.exitonclick()

draw_clock()


import os
import zipfile

"""
ZIP压缩包处理
zipfile 内置库
"""

# 递归删除目录
def delete_folder(folder_path: str):
    """
    folder_path: 要删除的目录名。
    """
    # check folder exist
    if not os.path.exists(folder_path):
        return

    # del items in folder
    for item in os.listdir(folder_path):
        item_path = os.path.join(folder_path, item)
        if os.path.isfile(item_path):
            # del file
            os.remove(item_path)
        else:
            # del folder
            delete_folder(item_path)

    # del empty folder
    try:
        os.rmdir(folder_path)
    except PermissionError as e:
        raise (e, "no access")

# 打包文件夹为zip压缩文件
def zip_folder(to_zip_f: str):
    # 检查
    if not os.path.exists(to_zip_f):  # check file exist
        raise FileExistsError(f"folder not exist: {to_zip_f}")
    if not os.access(to_zip_f, os.R_OK):  # check file access
        raise PermissionError(f"folder no read access: {to_zip_f}")
    if not os.access(to_zip_f, os.W_OK):  # check file access
        raise PermissionError(f"folder no write access: {to_zip_f}")

    # 取默认压缩文件名
    *_, zip_file_name = to_zip_f.split(os.sep)
    zip_file_name = f"{zip_file_name}.zip"

    # 执行压缩
    with zipfile.ZipFile(zip_file_name, 'w') as zip_file:
        for root, dirs, files in os.walk(to_zip_f):
            for file in files:
                file_path = os.path.join(root, file)
                zip_file.write(file_path)

    return os.path.abspath(zip_file_name)

# 解压缩zip包
def unzip_file(zip_path: str, unzip_path=None):
    """
    解压zip文件

    :param zip_path: 压缩文件路径
    :param unzip_path: 解压位置 默认压缩文件同目录下 同名目录
    :return: 解压后 全路径
    """

    # 检查
    if not os.path.exists(zip_path):  # check file exist
        raise FileExistsError(f"file not exist: {zip_path}")
    if not os.access(zip_path, os.R_OK):  # check file access
        raise PermissionError(f"file no read access: {zip_path}")

    # 压缩文件路径 自适应
    zip_full_path = zip_path
    if not os.path.isabs(zip_path):
        zip_full_path = os.path.abspath(zip_path)
    base_name = os.path.basename(zip_full_path)  # testcase.zip
    pre_path, extension = os.path.splitext(base_name)  # 路径(不包括扩展名) 文件类型

    # 解压路径 自适应
    unzip_full_path = unzip_path
    if not unzip_path:
        unzip_full_path = pre_path
    if os.path.exists(unzip_full_path):
        delete_folder(unzip_full_path)

    # 执行解压
    try:
        with zipfile.ZipFile(zip_path, 'r') as zip_ref:
            # 校验压缩包是否损坏 不能覆盖所有损坏情况
            # res = zip_ref.testzip()
            # if res:
            #     raise IOError(f"{zip_path} file is broken")

            for file_info in zip_ref.infolist():
                file_info.filename = file_info.filename.encode('cp437').decode('gbk')  # 修复中文乱码
                zip_ref.extract(file_info, unzip_full_path)
    except zipfile.BadZipFile as e:  # 文件损坏可能报错
        print(f"bad zip file: {zip_path} {e}")
    except OSError as e:  # 文件损坏可能报错
        print(f"os error: {e}")
        return ''

    return os.path.abspath(unzip_full_path)

if __name__ == '__main__':
    # zip_file_path = "xxx.zip"
    # print(unzip_file(zip_file_path))

    to_zip_folder = r"xxx"
    print(zip_folder(to_zip_folder))


import matplotlib.pyplot as plt
import sys

# call example:
# python3 plot.py plik.txt -a
# draws f(x) = time(a.length)

if len(sys.argv) != 3 or (sys.argv[2] != '-a' and sys.argv[2] != '-b'):
    print("Program should be called as:")
    print("<pyton executable> plot.py <filename> -<string to watch to>")
    print("String to watch to should be 'a' or 'b'.")
    quit()

filename = sys.argv[1]
variable_string = sys.argv[2][1]

with open(filename) as file:
    print("Reading file... " + filename)

    testcases = [[], [], []]
    for line in file:
        numbers = []
        i = 0
        for char in line.strip().split():
            testcases[i].append(float(char))
            i = i + 1

print("Sumaryczna liczba rozpatrzonych przypadków: " + str(len(testcases[1])))
print("Sumaryczny czas ich rozwiazywania: " + str(sum(testcases[2])))

variable_string_index = 0 if variable_string == 'a' else 1
plt.plot(testcases[variable_string_index], testcases[2])

plt.xlabel('Długość napisu ' + variable_string + ' [znaki]')
#plt.xlabel('Długość napisów a i b (a.length = b.length) [znaki]')
plt.ylabel('Czas rozwiązania przypadku [s]')

plt.show()


import numpy as np
from scipy.optimize import curve_fit

def svi_vol(k, a, b, rho, m, sigma):
    return np.sqrt(a + b * (rho * (k - m) + np.sqrt((k - m)**2 + sigma**2)))

class SVIFitter:

    def __init__(self, strikes, observed_vol, init_params=None):
        self.strikes = np.array(strikes)
        self.observed_vol = np.array(observed_vol)

        if init_params:
            self.init_params = init_params
            self.opt_params = init_params
        else:
            self.init_params = np.array([0.1, 0.1, 0.1, 100, 0.1])
            self.opt_params = None

    def fit_curve_fit(self, maxfev=20000, num_iter=2):
        log_strikes = np.log(self.strikes)
        params = self.init_params
        for _ in range(num_iter):
            result = curve_fit(svi_vol, log_strikes, self.observed_vol, p0=params, maxfev=maxfev)
            params = result[0]
        return params

    def fit(self, method='curve_fit'):
        if method == 'curve_fit':
            return self.fit_curve_fit()

    def eval(self, k):
        if self.opt_params is None:
            raise Exception("No Parameters")
        return svi_vol(k, *self.opt_params)


import datetime
import json
from typing import List, Optional, Union, Dict

from dateutil.parser import parse as datetime_parser
from maccabistats.models.player_game_events import GameEventTypes, GoalTypes
from maccabistats.models.team_in_game import TeamInGame

class GameData(object):
    def __init__(self, competition: str, fixture: str, date_as_hebrew_string: str, stadium: str, crowd: str,
                 referee: str, home_team: TeamInGame, away_team: TeamInGame,
                 season_string: str, half_parsed_events: List[Dict], date: Optional[datetime.datetime] = None,
                 technical_result: bool = False):
        """
        :param season_string: season description, such as : 2000-2001 or 2000-01
        :param half_parsed_events: Events we could not parse and we save them for manual manipulation
        :param date: the date the game was played.
        :param technical_result: Whether this game was finished by a technical result,
                                 If so check the winner by the teams score directly
        """
        self.competition = competition
        self.fixture = fixture

        # todo get this shit out of here
        self.date_as_hebrew_string = date_as_hebrew_string
        self._full_date = self.__get_date_as_datetime() if date is None else date
        # Remove any resolution lower than a day,
        # So we can check a if game is played before another game without checking the hour
        # (it might be added and we want to compare two Games object from different times in the maccabipedia time)
        self.date = self._full_date.replace(hour=0, minute=0, second=0, microsecond=0)

        self.stadium = stadium
        self.crowd = crowd
        self.referee = referee
        self.home_team = home_team
        self.away_team = away_team
        self.season = season_string
        self._half_parsed_events = half_parsed_events
        self.technical_result = technical_result

    def played_before(self, date: Union[datetime.datetime, datetime.date, str]) -> bool:
        if isinstance(date, str):
            date = datetime_parser(date)

        return date >= self.date

    def played_after(self, date: Union[datetime.datetime, datetime.date, str]) -> bool:
        if isinstance(date, str):
            date = datetime_parser(date)

        return date <= self.date

    def __get_date_as_datetime(self) -> datetime.datetime:
        date_args = self.date_as_hebrew_string.strip().split(" ")

        return datetime.datetime(year=int(date_args[2]),
                                 month=GameData.__get_month_num_from_hebrew(date_args[1]),
                                 day=int(date_args[0]))

    @staticmethod
    def __get_month_num_from_hebrew(month_name: str) -> int:
        months_in_hebrew_to_num = {"ינו": 1, "פבר": 2, "מרץ": 3, "אפר": 4, "מאי": 5, "יונ": 6,
                                   "יול": 7, "אוג": 8, "ספט": 9, "אוק": 10, "נוב": 11, "דצמ": 12}

        return months_in_hebrew_to_num[month_name]

    @property
    def league_fixture(self) -> Optional[int]:
        try:
            return int(self.fixture.replace("מחזור", ""))
        except ValueError:
            return None

    @property
    def maccabi_score(self) -> int:
        return self.maccabi_team.score

    @property
    def maccabi_score_diff(self) -> int:
        return self.maccabi_team.score - self.not_maccabi_team.score

    @property
    def is_maccabi_home_team(self) -> bool:
        # TODO: handle games which are not played on "home\away", radius and so on
        return self.home_team.name in ["מכבי תל אביב", "מכבי תא", 'מכבי ת"א']

    @property
    def maccabi_team(self) -> TeamInGame:
        return self.home_team if self.is_maccabi_home_team else self.away_team

    @property
    def not_maccabi_team(self) -> TeamInGame:
        return self.away_team if self.is_maccabi_home_team else self.home_team

    @property
    def is_maccabi_win(self) -> bool:
        return self.maccabi_score_diff > 0

    @property
    def events(self) -> List[Dict]:
        """
        Return all players events from maccabi_team in this game.
        :return: Each list entry contains:
                    normal_players dict, event.to_json, team_name.
                List is ordered by event_time asc.
        """

        # Maccabi team players events
        players_events = [dict(player.get_as_normal_player().__dict__,  # Players attributes, normal -> no events.
                               **event.json_dict(),
                               team=self.maccabi_team.name)
                          for player in self.maccabi_team.players
                          for event in player.events]

        # Not maccabi team players events
        players_events.extend([dict(player.get_as_normal_player().__dict__,  # Players attributes, normal -> no events.
                                    **event.json_dict(),
                                    team=self.not_maccabi_team.name)
                               for player in self.not_maccabi_team.players
                               for event in player.events])

        sorted_players_events = sorted(players_events, key=lambda p: p['time_occur'])  # Sort by event time.

        return sorted_players_events

    def goals(self) -> List[Dict]:
        """
        Return list of game events which their type is goal (ordered by time).
        Each event contains the results of the game as it was AFTER the goal was scored.
        """

        goals_events = [event for event in self.events if event['event_type'] == GameEventTypes.GOAL_SCORE.value]
        maccabi_score = not_maccabi_score = 0

        for goal in goals_events:
            if goal['team'] == "מכבי תל אביב":
                if goal['goal_type'] == GoalTypes.OWN_GOAL.value:
                    not_maccabi_score += 1
                else:
                    maccabi_score += 1
            else:
                if goal['goal_type'] == GoalTypes.OWN_GOAL.value:
                    maccabi_score += 1
                else:
                    not_maccabi_score += 1

            goal['maccabi_score'] = maccabi_score
            goal['not_maccabi_score'] = not_maccabi_score

        return goals_events

    def maccabi_goals(self) -> List[Dict]:
        """
        Wrapper for self.goals, returns just maccabi goals (including own goals scored by the opponent)
        """
        return [goal for goal in self.goals() if
                (goal['team'] != 'מכבי תל אביב' and goal['goal_type'] == 'Own goal') or
                goal['team'] == 'מכבי תל אביב']

    def json_dict(self) -> Dict:
        return dict(stadium=self.stadium,
                    date=self.date.isoformat(),
                    crowd=self.crowd,
                    referee=self.referee,
                    competition=self.competition,
                    fixture=self.fixture,
                    home_team=self.home_team.json_dict(),
                    away_team=self.away_team.json_dict())

    def to_json(self) -> str:
        return json.dumps(self.json_dict())

    @property
    def _maccabipedia_page_name(self):
        return "{maccabipedia}/{prefix}:{date} {home_team} נגד {away_team} - {competition}".format(
            maccabipedia="https://www.maccabipedia.co.il",
            prefix="משחק",
            date=self.date.strftime('%d-%m-%Y'),
            home_team=self.home_team.name,
            away_team=self.away_team.name,
            competition=self.competition)

    def __repr__(self) -> str:
        return f"{self.date.date()} {self.competition}: {self.home_team.name}({self.home_team.score}) - ({self.away_team.score}){self.away_team.name}"

    def full_description(self) -> str:
        return "Game between {self.home_team.name} (home) - {self.away_team.name} (away)\n" \
               "Results : {self.home_team.score} - {self.away_team.score}\n" \
               "Played on {self.stadium} at {self.date} with {self.crowd} viewers\n" \
               "As part of {self.competition}, round - {self.fixture}\n" \
               "Referee : {self.referee}\n" \
               "HomeTeam : {self.home_team}\n" \
               "AwayTeam : {self.away_team}\n\n".format(self=self)


from math import *
import time

def cp(n):
    if n%2==0:
        return 0
    else:
        for i in range(3,int(sqrt(n))+1,2):
            if n%i==0:
                return 0
    return 1

def weight(n):
    w=1
    ex=1
    if n%2==0:
        w=2
        ex=2
    for i in range(3,int(n**.5+1),2):
        if n%i==0:
            if cp(i):
                w*=i**ex
                ex+=1
    return w

s=0
t= time.time()
for i in range(2,1000000):
    s=s+weight(i)
print(s)

r=1000000007
t=time.time()-t
print(s%r,t)


import scrapy
from ..items import QuotestutorialItem
from scrapy.http import FormRequest

class QuoteSpider(scrapy.Spider):
    name = 'quotesbackup'
    start_urls = ['http://quotes.toscrape.com/login']

    def parse(self, response):
        token = response.css("form input::attr(value)").extract_first()
        # print("TOKEN :" + token)
        return FormRequest.from_response(response, formdata={
            'csrf_token': token,
            'username': 'hero',
            'pasword' : 'password'
        }, callback=self.start_scraping)

    def start_scraping(self, response):
        items = QuotestutorialItem()
        all_div_quotes = response.css("div.quote")

        for quote in all_div_quotes:
            title = quote.css(".text::text").extract()
            author = quote.css(".author::text").extract()
            tags = quote.css(".tag::text").extract()

            items['title'] = title
            items['author'] = author
            items['tags'] = tags

            yield items


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

## CPCantalapiedra - EEAD - CSIC - 2016

## TODO:
# Make use of cyvcf https://github.com/arq5x/cyvcf based on https://github.com/jamescasbon/PyVCF
# Tests

import os, sys, traceback
#from optparse import OptionParser
from vcf_util import *
from util import parse_samples_translation
from output import print_variants_genes
from CSVWriter import CSVWriter

GENES = 0
ISOFS = 1
NONE = -1

vcf_header = ""
variants_list = []
samples_filename = ""
samples_translation = ""
contigs_info = ""
genes_info = ""
output_format = "tabular"
het_to_miss = False
show_monomorph = True
biallelic = "mono"
numeric = True
cluster_samples = True

def parse_genes(query, sets_list, start, end,
                  max_missing, max_heteros, maf,
                  tmp_files_path):
    variants_dict = {}

    if query == "" or query == None:
        raise Exception("Query is empty.")

    if "." in query:
        query_list = [query]
        query_type = 1 # ISOF
    else:
        query_list = [query]
        query_type = 0 # GENE

    if sets_list == None or len(sets_list) == 0:
        raise Exception("Sets list is empty.")

    # all the sets in sets_list are from the same set_id and set_name
    # they only differ in the set_path
    set_id = sets_list[0][0]
    set_name = sets_list[0][1]

    sys.stderr.write("Set: "+str(set_id)+" - "+str(set_name)+"\n")

    vcf_filenames = [x[2] for x in sets_list]

    for vcf_filename in vcf_filenames:
        sys.stderr.write("Filename: "+str(vcf_filename)+"\n")

    #### Parse queries file
    ####
    # if query_file != "":
    #     sys.stderr.write("Processing queries list...\n")
    #     query_list = parse_queries_file(query_file)

    #### Variants to show
    ####
    # if variants_file != "":
    #     sys.stderr.write("Processing variants list...\n")
    #     variants_list = parse_queries_file(variants_file, keys=(1,2))
    variants_list = []

    #### Parse samples translation
    ####
    samples_translation = ""
    if (set_id=="ibsc2012"):
        samples_translation = "/home/cantalapiedra/SNP_panels/exome_sbcc/IBSC2012_as_ref/samples.translation"
    elif (set_id=="climbar"):
        samples_translation = ""
    else:
        raise Exception("Unknown set id: "+str(set_id))

    sys.stderr.write("Processing samples translation list...\n")
    samples_trans_dict = parse_samples_translation(samples_translation)

    #### Parse samples list
    ####
    #sys.stderr.write("Parsing samples list...\n")
    #samples_list = parse_samples_list(samples_filename)
    samples_list = []

    #### Parse headers file
    ####
    #header_found = parse_vcf_header_file(vcf_header, genotypes_dict, names_dict, \
    #                                     samples_filename, samples_list, samples_translation, samples_trans_dict)
    header_found = False

    genotypes_dict = {}
    names_dict = {}

    #### Parse VCF file
    ####
    total_records = 0
    total_variants = 0
    total_output = 0
    sys.stderr.write("Parsing VCF file...\n")
    for vcf_filename in vcf_filenames:
        sys.stderr.write("Processing "+vcf_filename+"\n")

        vcf_file = open(vcf_filename, 'r')
        for line in vcf_file:

            if line.startswith("##"): continue

            line_data = line.strip().split()

            # If a header is found, and if no header file was specified
            # record names of genotypes
            if header_found:
                if line.startswith("#"): continue
            elif not header_found:
                if line.startswith("#") and vcf_header == "":
                    parse_header(line_data, genotypes_dict, names_dict, \
                                 samples_filename, samples_list, samples_translation, samples_trans_dict)
                    header_found = True
                    continue

            if not header_found:
                raise Exception("No header found nor provided for VCF data.")

            total_records += 1

            contig = line_data[VCF_CONTIG_COL]
            #sys.stderr.write(str(line_data)+"\n")
            pos = long(line_data[VCF_POS_COL])
            if (start != 0 and pos < start): continue
            if (end != 0 and pos > end): break
            if len(variants_list) > 0 and not [contig, pos] in variants_list: continue

            variant_dict = {'var_id':-1, 'contig':contig, 'pos':pos, \
                            'ref':line_data[VCF_REF_COL], 'alt':line_data[VCF_ALT_COL], \
                            'alleles':{}, 'effs':{}, 'eff_x':[]}

            ok_variant = load_effects_genes(line_data, variant_dict, query_type, query_list)

            if not ok_variant: continue

            total_variants+=1
            var_id = total_variants
            variant_dict["var_id"] = var_id

            alleles = parse_alleles(line_data, genotypes_dict, biallelic)

            variant_dict['alleles'] = alleles

            ok_variant = preprocess_variant(alleles, max_heteros, max_missing, show_monomorph, maf, \
                                            biallelic, het_to_miss)
            if ok_variant:
                variants_dict[var_id] = variant_dict
                total_output += 1
                for allele in alleles:
                    for j in alleles[allele]:
                        genotypes_dict[j][var_id] = allele

    csv_file = CSVWriter().get_csv_file(tmp_files_path)

    csv_fileo = os.fdopen(csv_file[0], 'wb')

    #### Output
    ####
    sys.stderr.write("Generating output...\n")
    print_variants_genes(variants_dict, genotypes_dict, names_dict, samples_list, \
                         output_format, \
                         biallelic, numeric, cluster_samples, csv_fileo)

    csv_fileo.close()

    sys.stderr.write("Total records read: "+str(total_records)+"\n")
    sys.stderr.write("Total variants parsed: "+str(total_variants)+"\n")
    sys.stderr.write("Total variants output: "+str(total_output)+"\n")
    sys.stderr.write("Finished.\n")

    return csv_file

## END


"""
20. Valid Parentheses

Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

    Open brackets must be closed by the same type of brackets.
    Open brackets must be closed in the correct order.
    Every close bracket has a corresponding open bracket of the same type.

Example 1:

Input: s = "()"
Output: true

Example 2:

Input: s = "()[]{}"
Output: true

Example 3:

Input: s = "(]"
Output: false
"""

class Solution:
    def isValid(self, s: str) -> bool:
        temp =[]
        open1  = ['(','[','{']
        close1 = [')',']','}']

        temp.append(s[0])
        for char in range(1, len(s)) :
            #print(temp)

            if s[char] in open1 :
                temp.append(s[char])
                #print(temp)
            elif (temp) and (temp[-1] == open1[close1.index(s[char])]):
                del temp[-1]
                #print(temp)
            else :
                return False

        if not temp : return True
        else : return False


from tornado.httpclient import AsyncHTTPClient
from tornado.gen import coroutine, Return
from datetime import datetime, timedelta

import json
import copy

class ES(object):
    _last_document_query = {
      'query': {
        'bool': {
            'must': [
                {
                    "range": {
                        "@timestamp": {
                            "gte": 'now-1d',
                            "lte": "now"
                        }
                    }
                }
            ]
        }
      },
      'size': 1,
        'sort': [
            {
                '@timestamp': {
                    'order': 'desc',
                    'ignore_unmapped': True,
                }
            }
        ]
    }

    _terms_query = {
      'query': {
        'bool': {
            'must': []
        }
      },
      "size": 0,
      "aggs": {
        "2": {
          "terms": {
            "field": "",
            "size": 10,
            "order": {
              "_count": "desc"
            }
          }
        }
      },
    }

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

    def gen_and_phrase(self, **kwargs):
        q = []

        for k, v in kwargs.iteritems():
            q.append({ "match": { k: { "query": v, "type": "phrase" } } })

        return q

    def gen_last_document_query(self, **kwargs):
        q = copy.deepcopy(self._last_document_query)
        q['query']['bool']['must'] +=  self.gen_and_phrase(**kwargs)
        return q

    @coroutine
    def query_es(self, query, index='_all'):
        http_client = AsyncHTTPClient()
        res = yield http_client.fetch(
            "{}/{}/_search".format(self.eshost, index),
             method='GET',
             body=json.dumps(query),
             allow_nonstandard_methods=True
        )

        res = json.loads(res.body)

        if res['hits']['total'] == 0:
            raise RuntimeError('query returned zero results')

        raise Return(res)

    @coroutine
    def get_last_document(self, index='logstash', **kwargs):
        query = self.gen_last_document_query(**kwargs)
        res = yield self.query_es(query)
        raise Return(res['hits']['hits'][0])

    @coroutine
    def get_terms(self, field, time_back, **kwargs):
        query = copy.deepcopy(self._terms_query)
        query['aggs']['2']['terms']['field'] = field

        time_back = {
            "range": {
                "@timestamp": {
                    "gte": time_back,
                    "lte": "now"
                }
            }
        }

        query['query']['bool']['must'].append(time_back)
        query['query']['bool']['must'] += self.gen_and_phrase(**kwargs)

        res = yield self.query_es(query)
        raise Return(res['aggregations']['2']['buckets'])


#!/usr/bin/env python
"""
.d88b.           8
YPwww. Yb  dP .d88 8d8b. .d88b Yb  dP   88b. Yb  dP
    d8  YbdP  8  8 8P Y8 8.dP'  YbdP    8  8  YbdP
`Y88P'   dP   `Y88 8   8 `Y88P   dP   w 88P'   dP
        dP                      dP      8     dP
"""
import logging
import ConfigParser
from Sydney import frycook
from Sydney import waitress

# Lumberjack
logging.basicConfig(level=logging.DEBUG)

# Config
conf = ConfigParser.SafeConfigParser()
conf.read('./sydney.conf')
chef = frycook.Frycook(
        redis_use_socket=conf.get('redis', 'use_socket'),
        redis_socket=conf.get('redis', 'socket'))
waitress = waitress.Waitress(chef, bind=conf.get('waitress', 'bind'),
        port=conf.get('waitress', 'port'),
        api_prefix=conf.get('waitress', 'api_prefix'),
        debug=True)
        #debug=conf.get('waitress', 'debug'))


"""
序数：序数表示位置，如 1st 和 2nd。大多数序数都以 th 结尾，只有 1、2 和 3 例外。
1.在一个列表中存储数字 1~9。
2.遍历这个列表。
3.在循环中使用一个 if-elif-else 结构，以打印每个数字对应的序数。输出内容应为
1st、2nd、3rd、4th、5th、6th、7th、8th 和 9th，但每个序数都独占一行。
"""
numbers = list(range(1, 10))

for number in numbers:
    if str(number).endswith('1'):
        print("%dst" % number)
    elif str(number).endswith('2'):
        print("%dnd" % number)
    elif str(number).endswith('3'):
        print("%drd" % number)
    else:
        print("%dth" % number)


import requests
import datetime
from outros import get_coins_market
import csv

coins = get_coins_market.get_market_names('BTC')
print('# CryptoCoins:', len(coins))

data_file = []
coin_counter = 0
for coin in coins:
    url = 'https://bittrex.com/Api/v2.0/pub/market/GetTicks?marketName=' + coin + '&tickInterval=day'
    req = requests.get(url)
    if req.status_code == 200:
        result = req.json()
    else:
        print("Failed!! The server response was: ", req.status_code)
        break
    array_result = result['result']
    title_file = ['CryptoCoin', 'Date', 'Open Value', 'Close Value', 'Highest Value', 'Lowest Value',
                  'High Low Variation', 'Open Close Variation', '24h Volume', 'Base Volume']
    coin_counter += 1
    print('#', coin_counter, 'CryptoCoin')
    for i in array_result:
        data = datetime.datetime.strptime(i['T'], '%Y-%m-%dT%H:%M:%S')
        date_formatted = datetime.datetime.strftime(data, '%Y-%m-%d')
        open_value = (i['O'])
        close = (i['C'])
        high = (i['H'])
        low = (i['L'])
        volume = i['V']
        base_volume = i['BV']
        high_low_var = (((high/low)-1)*100)
        open_close_var = (((close/open_value)-1)*100)
        data_line = [coin, date_formatted, '{:.8f}'.format(open_value), '{:.8f}'.format(close), '{:.8f}'.format(high),
                     '{:.8f}'.format(low), '{:.4f}%'.format(high_low_var), '{:.4f}%'.format(open_close_var), volume,
                     base_volume]
        data_file.append(data_line)

        # print('In', date_formatted, 'the open value is', '{:1.8f}'.format(open_value), 'and close value',
        #       '{:1.8f}'.format(close), 'highest value', '{:1.8f}'.format(high), 'lowest value',
        #       '{:.8f}'.format(low), 'high low variation', '{:.4f}%'.format(high_low_var), 'open close variation',
        #       '{:.4f}%'.format(open_close_var), '24h volume', volume, 'base volume', base_volume)

def create_file():
    file = open("all_markets.csv", 'w')
    writer = csv.writer(file, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
    writer.writerow(title_file)  # Titulo escrito manualmente
    for row in data_file:
        writer.writerow(row)  # Linhas
    file.close()

def read_file():
    file = open("all_markets.csv", newline='')
    reader = csv.reader(file)
    for row in reader:
        print(row)

create_file()
# read_file()
"""
https://www.reddit.com/r/BitcoinMarkets/comments/6k75ue/unable_to_get_historical_bittrex_data/
So far I've found "oneMin", "fiveMin", "thirtyMin", "hour" and "day". Anyone know of any more?

It appears to be a UNIX timestamp, which is the number of milliseconds since 1/1/1970. For example, 1499127220008
happened on July 3rd. I was hoping this param lets you retrieve only historical data after the timestamp but
I can't get it to work.
https://bittrex.com/Api/v2.0/pub/market/GetTicks?marketName=BTC-btg&tickInterval=thirtyMin&_=1499127220008

{
    success : true,
    message : "",
    result : [ // Array of candle objects.
    {
        BV: 13.14752793,          // base volume
        C: 0.000121,              // close
        H: 0.00182154,            // high
        L: 0.0001009,             // low
        O: 0.00182154,            // open
        T: "2017-07-16T23:00:00", // timestamp
        V: 68949.3719684          // 24h volume
    },
    ...
    { ... }]
}

1
down vote
accepted
Volume is the amount traded in that altcoin over the past 24 hours.

In the case of BTC-DGB, this is the amount of DGB that has been traded in 24 hours.

BaseVolume is the total value traded in the base currency, for example Bitcoin.

Volume increases regardless if it is a buy or sell order.

To find the overall volume you can go to https://coinmarketcap.com.

"""


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

import os
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras import callbacks
import matplotlib.pyplot as plt
from feature_extract import save_feature
from utils import load_mnist

def check_feature(file_path, input_data, name):
    # check if the features file exists
    if not os.path.exists(file_path):
        save_feature(input_data, name)

def train_model(train_fea_file, validation_fea_file, epochs, callback):
    """
    train the model using features extracted by VGG16
    train_fea_file: features of training data
    validation_fea_file: features of validation data
    """

    check_feature(train_fea_file, train, 'train')
    check_feature(validation_fea_file, valid, 'valid')

    # load features of training data and validation data
    train_fea = np.load(train_fea_file)
    valid_fea = np.load(validation_fea_file)

    # Define the densely connected classifier followed by finally dense layer for the number of classes
    model = Sequential()
    model.add(Dense(512, activation='relu', input_dim=(1*1*512)))
    model.add(Dense(10, activation='softmax'))

    # Compile the model
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])

    # Train the model
    history = model.fit(
        train_fea,
        train[1],
        epochs=epochs,
        verbose=1,
        validation_data=(valid_fea, valid[1]),
        callbacks=callback)
    return history, model

def save_model(trained_model):
    trained_model.save('trained_model.h5')

def plot_loss_acc(history):
    acc = history.history['acc']
    val_acc = history.history['val_acc']
    loss = history.history['loss']
    val_loss = history.history['val_loss']
    epochs = range(1, len(acc) + 1)

    plt.title('Training and validation accuracy')
    plt.plot(epochs, acc, 'red', label='Training acc')
    plt.plot(epochs, val_acc, 'blue', label='Validation acc')
    plt.legend()

    plt.figure()
    plt.title('Training and validation loss')
    plt.plot(epochs, loss, 'red', label='Training loss')
    plt.plot(epochs, val_loss, 'blue', label='Validation loss')

    plt.legend()
    plt.show()

# Incorporating reduced learning and early stopping for callback
reduce_learning = callbacks.ReduceLROnPlateau(
    monitor='val_loss',
    factor=0.2,
    patience=2,
    verbose=1,
    mode='auto',
    epsilon=0.0001,
    cooldown=2,
    min_lr=0
)

early_stopping = callbacks.EarlyStopping(
    monitor='val_loss',
    min_delta=0,
    patience=7,
    verbose=1,
    mode='auto'
)
callbacks = [reduce_learning, early_stopping]

def main():

    train_fea_path = 'train_features.npy'
    val_fea_path = 'valid_features.npy'
    HISTORY, MODEL = train_model(train_fea_path, val_fea_path, 100, callbacks)
    save_model(MODEL)
    plot_loss_acc(HISTORY)

if __name__ == '__main__':
    train, valid, test = load_mnist(normalize=True)
    main()


"""
Script to cotaing function to calculate the performance of the regressors
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import iqr

from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.tree import DecisionTreeRegressor

import config
import utility
import dataset_generator
import regressors_lib
from utility import get_square_number_array
import statistic_utility

NAMES = ["Random forest", "Nearest Neighbors U", "Nearest Neighbors D", "Decision Tree"]
CLASSIFIERS = [
    RandomForestRegressor(),
    KNeighborsRegressor(config.N_NEIGHBOURS, weights='uniform'),
    KNeighborsRegressor(config.N_NEIGHBOURS, weights='distance'),
    DecisionTreeRegressor(random_state=0)
]

CLASSIFIERS_DICT = {
    "Random forest": RandomForestRegressor(),
    "Nearest Neighbors U": KNeighborsRegressor(config.N_NEIGHBOURS, weights='uniform'),
    "Nearest Neighbors D": KNeighborsRegressor(config.N_NEIGHBOURS, weights='distance'),
    "Decision Tree": DecisionTreeRegressor(random_state=0)
}

def performance_dataset(X, y):
    x_train, x_test, y_train, y_test = train_test_split(X, y, train_size=0.20)

    lox, loy = regressors_lib.get_optimal_points(y_test)

    errors = {}
    for name, clf in zip(NAMES, CLASSIFIERS):
        lpx, lpy = regressors_lib.get_regressor_predicted_points(clf, x_train, x_test, y_train)

        error_x = abs(np.subtract(lpx, lox))
        error_y = abs(np.subtract(lpy, loy))

        errors[f'{name}--IQR-X'] = iqr(error_x)
        errors[f'{name}--IQR-Y'] = iqr(error_y)
        errors[f'{name}--MEDIAN X'] = np.median(error_x)
        errors[f'{name}--MEDIAN Y'] = np.median(error_y)

    return errors

def get_number_of_good_point(x_train, x_test, y_train, y_test):
    lox, loy = regressors_lib.get_optimal_points(y_test)

    good_points = {}
    for name, clf in zip(NAMES, CLASSIFIERS):
        lpx, lpy = regressors_lib.get_regressor_predicted_points(clf, x_train, x_test, y_train)

        good_point = 0
        for i in range(len(lpx)):
            p_predicted = np.array(lpx[i], lpy[i])
            p_optimal = np.array(lox[i], loy[i])
            dist = np.linalg.norm(p_predicted - p_optimal)
            if dist < config.OPTIMAL_TH:
                good_point += 1

        good_points[name] = good_point

    return good_points

def plot_test_multi_regress(x_train, x_test, y_train, y_test, title_add="default"):
    lox, loy = regressors_lib.get_optimal_points(y_test)

    error_x_df = pd.DataFrame()
    error_y_df = pd.DataFrame()
    for name, clf in zip(NAMES, CLASSIFIERS):
        lpx, lpy = regressors_lib.get_regressor_predicted_points(clf, x_train, x_test, y_train)

        error_x = abs(np.subtract(lpx, lox))
        error_y = abs(np.subtract(lpy, loy))

        error_x_df.insert(len(error_x_df.columns), name, error_x)
        error_y_df.insert(len(error_y_df.columns), name, error_y)

        df = pd.DataFrame({
            'diff x': error_x,
            'diff y': error_y
        })

        df.plot.kde(title=f'{name} {title_add}')

    error_x_df.plot.box(title=f"Error x {title_add}")
    error_y_df.plot.box(title=f"Error y {title_add}")

def get_ecdf_regressor_dataset(x_train, x_test, y_train, y_test, regressor, regressor_name):
    optimal_points, predicted_points = regressors_lib.get_regressor_optimal_and_predicted_points(regressor, x_train,
                                                                                x_test, y_train,
                                                                                y_test)
    return statistic_utility.get_ecdf_euclidean_df_resolve_auto(optimal_points, predicted_points, regressor_name)

def get_ecdf_dataset_squares(x_train, x_test, y_train, y_test, regressors, regressor_name):
    lox, loy = regressors_lib.get_optimal_points(y_test)

    regressors.fit(x_train, y_train)
    Z = regressors.predict(x_test)

    lpr = Z[:, 0]
    lpp = Z[:, 1]
    lpx, lpy = utility.pol2cart(lpr, lpp)

    xo, yo = get_square_number_array(lox, loy)
    xp, yp = get_square_number_array(lpx, lpy)

    df = statistic_utility.get_ecdf_square_df(xo, yo, xp, yp, regressor_name)

    return df

def main_test_dataset_default_vs_dataset_mean_and_std():
    name_file_reader = ["dati3105run0r", "dati3105run1r", "dati3105run2r"]
    name_file_cam = ["Cal3105run0", "Cal3105run1", "Cal3105run2"]

    x_train, y_train = dataset_generator.generate_dataset_base("BLE2605r", "2605r0")

    x_test, y_test = dataset_generator.generate_dataset(name_file_reader, name_file_cam,
                                                        dataset_generator.generate_dataset_base)

    plot_test_multi_regress(x_train, x_test, y_train, y_test)

    x_train, y_train = dataset_generator.generate_dataset_with_mean_and_std("BLE2605r", "2605r0")

    x_test, y_test = dataset_generator.generate_dataset(name_file_reader, name_file_cam,
                                                        dataset_generator.generate_dataset_with_mean_and_std)

    plot_test_multi_regress(x_train, x_test, y_train, y_test, title_add="mean_std")

def compare_experiment_with_ecdf():
    """
    Compare the performance of a regressor with different runs by ecdf
    :return: void
    """
    name_files_reader = ["dati3105run0r", "dati3105run1r", "dati3105run2r"]
    name_files_cam = ["Cal3105run0", "Cal3105run1", "Cal3105run2"]

    x_train, y_train = dataset_generator.generate_dataset_base("BLE2605r", "2605r0")

    ecdf_total = pd.DataFrame()
    for name_file_reader, name_file_cam in zip(name_files_reader, name_files_cam):
        x_test, y_test = dataset_generator.generate_dataset([name_file_reader], [name_file_cam],
                                                            dataset_generator.generate_dataset_base)

        # regressor_dict = {
        #     f'kNN_{name_file_reader}': KNeighborsRegressor(config.N_NEIGHBOURS, weights='distance')
        # }
        #
        # ecdf_df = get_ecdf_dataset_back(x_train, x_test, y_train, y_test, regressor_dict)
        ecdf_df = get_ecdf_regressor_dataset(x_train, x_test, y_train, y_test,
                                             KNeighborsRegressor(config.N_NEIGHBOURS, weights='distance'),
                                             f'kNN_{name_file_reader}')
        ecdf_total = pd.concat([ecdf_total, ecdf_df], axis=1)

    ecdf_total = ecdf_total.interpolate(method='linear')
    ecdf_total.plot.line(
        title="ECDF per different Run",
        xlabel="(m)",
        ylabel="Empirical cumulative distribution function"
    )

def compare_experiment_with_ecdf_square():
    name_files_reader = ["dati3105run0r", "dati3105run1r", "dati3105run2r"]
    name_files_cam = ["Cal3105run0", "Cal3105run1", "Cal3105run2"]

    x_train, y_train = dataset_generator.generate_dataset_base("BLE2605r", "2605r0")

    ax = plt.axes(title="ECDF per different Run")
    for name_file_reader, name_file_cam in zip(name_files_reader, name_files_cam):
        x_test, y_test = dataset_generator.generate_dataset([name_file_reader], [name_file_cam],
                                                            dataset_generator.generate_dataset_base)

        ecdf_df = get_ecdf_dataset_squares(x_train, x_test, y_train, y_test,
                                           KNeighborsRegressor(config.N_NEIGHBOURS, weights='distance'),
                                           f'kNN_{name_file_reader}')
        index = ecdf_df.index.tolist()
        ax.step(np.array(index), ecdf_df[f'kNN_{name_file_reader}'], label=name_file_reader, where="post")

    ax.set_xlabel("squares")
    ax.set_ylabel("Empirical cumulative distribution function")
    plt.legend(loc='lower right')
    plt.show()

def compare_regressor_with_ecdf(train_dataset, test_dataset, name_file_reader, num_run, regressors=None, what_type_of_ecdf=0):
    """
    Compare the performance of different regressor with a run by ecdf
    :param name_file_reader: experiment where are  taken the data
    :param train_dataset: dataset where to train the regressors
    :param test_dataset: dataset to be used as testing
    :param regressors: regressors to compare
    :param what_type_of_ecdf:
        0: euclidean
        1: square
    :return: void
    """

    if regressors is None:
        regressors = CLASSIFIERS_DICT

    name_plot = ' '.join(name_file_reader)
    if what_type_of_ecdf == 0:
        ecdf_total = compare_regressor_with_ecdf_euclidean(train_dataset, test_dataset, regressors)

        ecdf_total.plot.line(
            title=f"CDF run{num_run}",
            xlabel="(m)",
            ylabel="Empirical cumulative distribution function"
        )

        plt.savefig(f'plots/ecdf_euclidean_{name_plot}.png')
    if what_type_of_ecdf == 1:
        fig, ax = plt.subplots()
        ax.set_title(f"CDF run{num_run}")
        ax = compare_regressor_with_ecdf_square(train_dataset, test_dataset, regressors, ax)

        plt.legend(loc='lower right')
        plt.show()

        plt.savefig(f'plots/ecdf_square_{name_plot}.png')

def compare_regressor_with_ecdf_euclidean(train_dataset, test_dataset, regressors):
    x_train = train_dataset[0]
    y_train = train_dataset[1]

    x_test = test_dataset[0]
    y_test = test_dataset[1]

    ecdf_total = pd.DataFrame()
    for regressor_name in regressors:
        ecdf_df = get_ecdf_regressor_dataset(x_train, x_test, y_train, y_test, regressors[regressor_name],
                                             regressor_name)
        max_error_at_percentage, std, mean = statistic_utility.get_numeric_values(ecdf_df, 90)
        print(regressor_name, max_error_at_percentage, std, mean)
        ecdf_total = pd.concat([ecdf_total, ecdf_df], axis=1)

    ecdf_total = ecdf_total.interpolate(method='linear')

    return ecdf_total

def compare_regressor_with_ecdf_square(train_dataset, test_dataset, regressors, ax):
    x_train = train_dataset[0]
    y_train = train_dataset[1]

    x_test = test_dataset[0]
    y_test = test_dataset[1]

    for regressor_name in regressors:
        print(regressor_name)
        ecdf_df = get_ecdf_dataset_squares(x_train, x_test, y_train, y_test, regressors[regressor_name], regressor_name)

        index = ecdf_df.index.tolist()
        ax.step(np.array(index), ecdf_df[regressor_name], label=regressor_name, where="post")

    ax.set_xlabel("squares")
    ax.set_ylabel("Empirical cumulative distribution function")

    return ax

def compare_k_NNs(max_k, stride, train_dataset, test_dataset, name_file_readers, weights='distance',
                  what_type_of_ecdf=0):
    kNN_dict = regressors_lib.get_kNN_dict(max_k, stride, weights)

    compare_regressor_with_ecdf(train_dataset, test_dataset, name_file_readers, regressors=kNN_dict,
                                what_type_of_ecdf=what_type_of_ecdf)


import sys
class Node:
    levelsList = []
    lineList = []
    indexList = []
    starList = []
    fileBuffer = []

    def __init__(self, val=None):
        self.value = val
        self.children = []

    def __repr__(self):
        return "{}".format(self.value)

    def readFile(self):
        #for line in sys.stdin:
        lines = "".join(line for line in sys.stdin if not line.isspace())
        linelist = lines.split("\n")
        for line in linelist:
            self.setLevel(line)
            self.lineList.append(line.replace(".", ""))

    def setLevel(self, line):
        if line.count("*") > 0:
           self.levelsList.append(0)
        elif line.count(".") > 0:
           self.levelsList.append(line.count("."))
        else:
           self.levelsList.append(self.levelsList[len(self.levelsList) - 1])

    def traverseLevels(self):
        for level, value in zip(self.levelsList, self.lineList):
            last = root
            for _ in range(level):
                last = last.children[-1]
            if value.startswith(" ") or value.startswith(".") or value.startswith("*"):
                last.children.append(Node(value))
            else:
                temp = str(last.children[len(last.children) - 1])
                temp += "\n" + "    " + value
                last.children[len(last.children) - 1] = Node(temp)

    def processLines(self, root, depth=0):
        for child in root.children:
            '''To handle main trunck indexes'''
            if child.value.count("*") == 1:
                # To set the trunks first index and its star count
                if not self.indexList:
                   self.indexList.append(1)
                   self.starList.append(1)
                else:
                    # To set the trunks next indexes using last trunk index value
                    for i in reversed(range(len(self.starList))):
                        if (self.starList[i] == 1):
                            self.starList.append(1)
                            self.indexList.append(self.indexList[i]+1)
                            break
            #To handle branch indexes
            elif child.value.count("*") > 1:
                for i in reversed(range(len(self.indexList))):
                    if self.indexList[i]:
                        if child.value.count("*") in self.starList:
                            if child.value.count("*") == self.starList[i]:
                                self.indexList.append(self.indexList[i] + 1)
                                self.starList.append(child.value.count("*"))
                                break
                            if child.value.count("*") > self.starList[i]:
                                self.indexList.append(10 * self.indexList[i] + 1)
                                self.starList.append(child.value.count("*"))
                                break
                        else:
                          self.indexList.append(10 * self.indexList[i] + 1)
                          self.starList.append(child.value.count("*"))
                          break
            else:
                self.starList.append(child.value.count("*"))
                self.indexList.append('')

            indexFormatted = ""
            if child.children.__len__() > 0:
                if child.value.count("*") >= 1:
                    child.value = child.value.replace("*", "")
                    index = str(self.indexList[len(self.indexList) - 1])
                    for i in range(len(index)):
                       indexFormatted = indexFormatted + index[i] +  "."
                    self.fileBuffer.append('  ' * depth +  str(indexFormatted) + '%r' % child)
                else:
                    self.fileBuffer.append('  ' * depth + '+' + str(self.indexList[len(self.indexList)-1]) + '%r' % child)
            else:
                self.fileBuffer.append('  ' * depth + '-' + str(self.indexList[len(self.indexList) - 1]) + '%r' % child)
            self.processLines(child, depth + 1)

if __name__ == '__main__':
   root = Node()
   root.readFile()
   root.traverseLevels()
   root.processLines(root)
   for lines in root.fileBuffer:
       print(lines)


import os
from bs4 import BeautifulSoup
import requests
from dotenv import load_dotenv
import spotipy
from spotipy.oauth2 import SpotifyOAuth

load_dotenv("../.env")
spotify_client_id = os.getenv("spotify_client_id")
spotify_client_secret = os.getenv("spotify_client_secret")

date_selected = input("Which year you do want to travel to? Enter the date in YYYY-MM-DD format: ")

response = requests.get(f"https://www.billboard.com/charts/hot-100/{date_selected}")
billboard_webpage = response.text
soup = BeautifulSoup(billboard_webpage, "html.parser")

billboard_100_list = soup.select(selector="div li ul li h3")

songs_list = [song.getText().strip() for song in billboard_100_list]

# print(songs_list)

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=spotify_client_id,
                                               client_secret=spotify_client_secret,
                                               redirect_uri="http://example.com",
                                               scope="user-library-read",
                                               show_dialog=True,
                                               cache_path='token.txt'))

spotify_song_uri_list = []
year = date_selected.split("-")[0]
user_id = sp.current_user()["id"]

for song in songs_list:
    result = sp.search(q=f"track:{song} year:{year}", type="track")
    try:
        uri = result["tracks"]["items"][0]["uri"]
        spotify_song_uri_list.append(uri)
    except IndexError:
        print(f"{song} doesn't exist in Spotify. Skipped.")

print(spotify_song_uri_list)

playlist = sp.user_playlist_create(user=user_id, name=f"{date_selected} Billboard 100", public=False)
print(playlist)

sp.playlist_add_items(playlist_id=playlist['id'], items=spotify_song_uri_list)


import os
import numpy as np
import glob
from pickle import load
import tensorflow as tf
from tensorflow.keras.optimizers import Adam

from image_quality.models.image_quality_model import phiq_net
from callbacks.callbacks import create_callbacks
from image_quality.train.plot_train import plot_history
from image_quality.train.resolution_group_generator import ResolutionGroupGenerator
from callbacks.evaluation_callback_generator import ModelEvaluationIQGenerator
from callbacks.warmup_cosine_decay_scheduler import WarmUpCosineDecayScheduler
from image_quality.model_evaluation.model_evaluation import ModelEvaluation

def identify_best_weights(result_folder, history, best_plcc):
    pos = np.where(history['plcc'] == best_plcc)[0][0]
    if len(pos) == 0:
        return None

    pos_loss = '{}_{:.4f}'.format(pos + 1, history['loss'][pos])
    all_weights_files = glob.glob(os.path.join(result_folder, '*.h5'))
    for all_weights_file in all_weights_files:
        weight_file = os.path.basename(all_weights_file)
        if weight_file.startswith(pos_loss):
            best_weights_file = all_weights_file
            return best_weights_file
    return None

def remove_non_best_weights(result_folder, best_weights_files):
    all_weights_files = glob.glob(os.path.join(result_folder, '*.h5'))
    for all_weights_file in all_weights_files:
        if all_weights_file not in best_weights_files:
            os.remove(all_weights_file)

def get_image_list_path(image_folder, image_score_list, n_quality_levels, do_normalization=False):
    images_scores = []
    for image_score in image_score_list:
        content = image_score.split(',')
        if len(content) > 2:
            if n_quality_levels > 1:
                scores_softmax = np.array([float(score) for score in content[1: 6]])
                score = [score_softmax / scores_softmax.sum() for score_softmax in scores_softmax]
            else:
                score = float(content[-1])
        else:
            if do_normalization:
                score = float(content[1]) / 25 + 1
            else:
                score = float(content[1])
        images_scores.append('{};{}'.format(os.path.join(image_folder, content[0]), score))
    return images_scores

def check_args(args):
    if 'result_folder' not in args:
        exit('Result folder must be specified')
    if 'images_scores_file' not in args:
        exit('Image_score file must be specified')
    if 'image_folder' not in args:
        args['image_folder'] = None
        print('WARN: check the image paths are specified in images_scores_file, otherwise image folder must be specified')

    if 'n_quality_levels' not in args:
        exit('Number of quality levels (1 or 5) must be specified')

    if 'epochs' not in args:
        args['epochs'] = 100
    if 'lr_base' not in args:
        args['lr_base'] = 1e-4 / 2
    if 'naive_backbone' not in args:
        args['naive_backbone'] = False
    if 'backbone' not in args:
        args['backbone'] = 'resnet50'
    if 'weights' not in args:
        args['weights'] = None
    if 'initial_epoch' not in args:
        args['initial_epoch'] = 0
    if 'feature_fusion' not in args:
        args['feature_fusion'] = True
    if 'attention_module' not in args:
        args['attention_module'] = True
    if 'freeze_backbone' not in args:
        args['freeze_backbone'] = False
    if 'lr_schedule' not in args:
        args['lr_schedule'] = True
    if 'batch_size' not in args:
        args['batch_size'] = 16
    if 'image_aug' not in args:
        args['image_aug'] = True
    if 'do_finetune' not in args:
        args['do_finetune'] = True
    if 'multi_gpu' not in args:
        args['multi_gpu'] = 0
    if 'gpu' not in args:
        args['gpu'] = 0

    return args

def train_main(args):
    args = check_args(args)

    if args['multi_gpu'] == 0:
        gpus = tf.config.experimental.list_physical_devices('GPU')
        tf.config.experimental.set_visible_devices(gpus[args['gpu']], 'GPU')

    result_folder = args['result_folder']
    model_name = args['backbone']

    # Define loss function according to prediction objective (score distribution or MOS)
    if args['n_quality_levels'] > 1:
        using_single_mos = False
        loss = 'categorical_crossentropy'
        metrics = None
        model_name += '_distribution'
    else:
        using_single_mos = True
        metrics = None
        loss = 'mse'
        model_name += '_mos'

    if args['naive_backbone']:
        model_name += '_naive'
    if args['weights'] is None:
        model_name += '_nopretrain'
    if not args['image_aug']:
        model_name += '_no_imageaug'

    # Create PHIQnet model
    optimizer = Adam(args['lr_base'])

    if args['multi_gpu'] > 0:
        strategy = tf.distribute.MirroredStrategy(cross_device_ops=tf.distribute.HierarchicalCopyAllReduce())
        print('Number of devices: {}'.format(strategy.num_replicas_in_sync))

        with strategy.scope():
            # Everything that creates variables should be under the strategy scope.
            # In general this is only model construction & `compile()`.
            model = phiq_net(n_quality_levels=args['n_quality_levels'],
                             naive_backbone=args['naive_backbone'],
                             backbone=args['backbone'],
                             feature_fusion=args['feature_fusion'],
                             attention_module=args['attention_module'])

            model.compile(loss=loss, optimizer=optimizer, metrics=[metrics])

    else:
        model = phiq_net(n_quality_levels=args['n_quality_levels'],
                         naive_backbone=args['naive_backbone'],
                         backbone=args['backbone'],
                         feature_fusion=args['feature_fusion'],
                         attention_module=args['attention_module'])
        model.compile(loss=loss, optimizer=optimizer, metrics=[metrics])

    # model.summary()
    # Load Imagenet pretrained weights or existing weights for fine-tune
    if args['weights'] is not None:
        print('Load weights: {}'.format(args['weights']))
        model.load_weights(args['weights'], by_name=True)

    # model.run_eagerly = True

    if args['weights'] is not None and ('resnet' in args['backbone'] or args['backbone'] == 'inception'):
        imagenet_pretrain = True
    else:
        imagenet_pretrain = False

    # Define train and validation data
    with open(args['images_scores_file'], 'rb') as f:
        train_images_scores, val_images_scores, test_images_scores = load(f)
    if args['image_folder']:
        train_images_scores = get_image_list_path(args['image_folder'], train_images_scores, args['n_quality_levels'], do_normalization=True)
        val_images_scores = get_image_list_path(args['image_folder'], val_images_scores, args['n_quality_levels'], do_normalization=True)
        test_images_scores = get_image_list_path(args['image_folder'], test_images_scores, args['n_quality_levels'], do_normalization=True)

    train_generator = ResolutionGroupGenerator(train_images_scores, batch_size=args['batch_size'], imagenet_pretrain=imagenet_pretrain)
    train_steps = train_generator.__len__()

    validation_generator = ResolutionGroupGenerator(val_images_scores, batch_size=2, imagenet_pretrain=imagenet_pretrain, image_aug=False)
    validation_steps = validation_generator.__len__()

    evaluation_callback = ModelEvaluationIQGenerator(validation_generator,
                                                     using_single_mos)

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

    # Create callbacks including evaluation and learning rate scheduler
    callbacks = create_callbacks(model_name,
                                 result_folder,
                                 evaluation_callback,
                                 checkpoint=True,
                                 early_stop=True,
                                 metrics=metrics)

    warmup_epochs = 10
    if args['lr_schedule']:
        total_train_steps = args['epochs'] * train_steps
        warmup_steps = warmup_epochs * train_steps
        warmup_lr = WarmUpCosineDecayScheduler(learning_rate_base=args['lr_base'],
                                               total_steps=total_train_steps,
                                               warmup_learning_rate=0.0,
                                               warmup_steps=warmup_steps,
                                               hold_base_rate_steps=30 * train_steps,
                                               verbose=1)
        callbacks.append(warmup_lr)

    # Define optimizer and train
    model_history = model.fit(x=train_generator,
                              epochs=args['epochs'],
                              steps_per_epoch=train_steps,
                              validation_data=validation_generator,
                              validation_steps=validation_steps,
                              verbose=1,
                              shuffle=False,
                              callbacks=callbacks,
                              initial_epoch=args['initial_epoch'],
                              )
    # model.save(os.path.join(result_folder, model_name + '.h5'))
    # plot_history(model_history, result_folder, model_name)

    best_weights_file = identify_best_weights(result_folder, model_history.history, callbacks[3].best)
    remove_non_best_weights(result_folder, [best_weights_file])

    evaluation_testset = ModelEvaluation(model, using_single_mos=using_single_mos, imagenet_pretrain=imagenet_pretrain)
    evaluation_testset.__evaluation__(best_weights_file, test_images_scores, evaluation_name='testset_basetrain', result_folder=result_folder, draw_scatter=True)

    # do fine-tuning
    if args['do_finetune'] and best_weights_file:
        print('Finetune...')
        del (callbacks[-1])
        model.load_weights(best_weights_file)
        finetune_lr = 1e-6
        if args['lr_schedule']:
            warmup_lr_finetune = WarmUpCosineDecayScheduler(learning_rate_base=finetune_lr,
                                                            total_steps=total_train_steps,
                                                            warmup_learning_rate=0.0,
                                                            warmup_steps=warmup_steps,
                                                            hold_base_rate_steps=10 * train_steps,
                                                            verbose=1)
            callbacks.append(warmup_lr_finetune)
        finetune_optimizer = Adam(finetune_lr)
        model.compile(loss=loss, optimizer=finetune_optimizer, metrics=[metrics])

        finetune_model_history = model.fit(x=train_generator,
                                  epochs=args['epochs'],
                                  steps_per_epoch=train_steps,
                                  validation_data=validation_generator,
                                  validation_steps=validation_steps,
                                  verbose=1,
                                  shuffle=False,
                                  callbacks=callbacks,
                                  initial_epoch=args['initial_epoch'],
                                  )

        best_weights_file_finetune = identify_best_weights(result_folder, finetune_model_history.history, callbacks[3].best)
        if best_weights_file_finetune:
            remove_non_best_weights(result_folder, [best_weights_file, best_weights_file_finetune])
            evaluation_testset.__evaluation__(best_weights_file_finetune, test_images_scores, evaluation_name='testset_finetune',
                                              result_folder=result_folder, draw_scatter=True)


import json, os
import xml.etree.ElementTree as et
from framelib.utils import write_json, strip_space
from framelib.classes import tParseAndBuild, Documentation

def main(docs: Documentation):
    """
    Creates tutorial information and stores it in a dictionary format.
    This information is displayed to the user in a umenu.
    """

    tutorial_index = (
        docs.package / "docs" / "tutorials" / "framelib-tut" / "00_fl_index.maxtut.xml"
    )
    docs.interfaces_dir.mkdir(exist_ok=True)

    obj_lookup = docs.interfaces_dir / "framelib-obj-tlookup.json"

    worker = tParseAndBuild()

    worker.extract_from_refpage(tutorial_index)

    write_json(obj_lookup, worker.d_skeleton)

if __name__ == "__main__":
    main(Documentation())


#!/usr/bin/python3
"""
Implements the HBNB console

The console obey the following conventions:

1. End of file on input exits the command interpreter. (It calls the method
   'do_EOF')
2. The command 'quit' exits the program. (It calls the method 'do_quit')
3. The command 'create' creates a new instance of BaseModel, saves it
   (to the JSON file) and prints the 'id'. (It calls the method 'do_create').
   Ex: `create BaseModel`
4. Typing an empty line does not execute anything. (Actually, it calls the
   method `emptyline')
5. The command 'show' prints the string representation of an instance based
   on the class name and 'id'. (It calls the method 'do_show').
   Ex: `show BaseModel 1234-1234-1234`
6. The command 'destroy' deletes an instance based on the class name and id
   (save the change into the JSON file). (It calls the method 'do_destroy').
   Ex: `destroy BaseModel 1234-1234-1234`
7. The command 'all' prints all string representation of all instances based
   or not on the class name. (It calls the method 'do_all').
   Ex: `all BaseModel` or `all`
8. The command 'update' updates an instance based on the class name and id by
   adding or updating attribute (save the change into the JSON file). (It
   calls the method 'do_update')
   Ex: `update BaseModel 1234-1234-1234 email "aibnb@mail.com"`
"""
import cmd
import re
from shlex import split
from models.base_model import BaseModel
from models.user import User
from models.state import State
from models.city import City
from models.amenity import Amenity
from models.place import Place
from models.review import Review
from models import storage

PROMPT = '(hbnb) '
CLASS_NAME_MISSING = '** class name missing **'
NO_CLASS = "** class doesn't exist **"
INST_ID_MISSING = '** instance id missing **'
INST_NOT_FOUND = '** no instance found **'
ATTR_NAME_MISSING = '** attribute name missing **'
ATTR_VALUE_MISSING = '** value missing **'
classes = {
    "BaseModel",
    "User",
    "State",
    "City",
    "Amenity",
    "Place",
    "Review"
}

class HBNBCommand(cmd.Cmd):
    """
    Command interpreter which inherits from cmd module
    """
    prompt = PROMPT

    def do_EOF(self, arg):
        """EOF signal to exit the program."""
        print()
        return True

    def do_quit(self, arg):
        """Quit command to exit the program."""
        return True

    def do_create(self, arg):
        """
        Create a new class instance

        Usage: create <class name>
        """
        args = custom_parser(arg)
        if len(args) == 0:
            print(CLASS_NAME_MISSING)
        elif args[0] not in classes:
            print(NO_CLASS)
        else:
            new_id = eval(args[0])().id
            print(f'{new_id}')
            storage.save()

    def do_show(self, arg):
        """
        Prints the string representation of an instance based on the
        class name and id

        Usage: show <class name> <id>
        """
        all_obj = storage.all()
        args = custom_parser(arg)
        if len(args) == 0:
            print(CLASS_NAME_MISSING)
        elif args[0] not in classes:
            print(NO_CLASS)
        elif len(args) < 2:
            print(INST_ID_MISSING)
        elif "{}.{}".format(args[0], args[1]) not in all_obj:
            print(INST_NOT_FOUND)
        else:
            print(all_obj["{}.{}".format(args[0], args[1])])

    def do_destroy(self, arg):
        """
        Deletes an instance based on the class name and id

        Usage: destroy <class name> <id>
        """
        all_obj = storage.all()
        args = custom_parser(arg)
        if len(args) == 0:
            print(CLASS_NAME_MISSING)
        elif args[0] not in classes:
            print(NO_CLASS)
        elif len(args) < 2:
            print(INST_ID_MISSING)
        elif "{}.{}".format(args[0], args[1]) not in all_obj.keys():
            print(INST_NOT_FOUND)
        else:
            del all_obj["{}.{}".format(args[0], args[1])]
            storage.save()

    def do_all(self, arg):
        """
        Prints all string representation of all instances based or
        not on the class name

        Usage: all <class name> or all
        """
        args = custom_parser(arg)
        if (len(args) > 0):
            class_name = args[0]
            if class_name not in classes:
                print(NO_CLASS)
                return
            object_list = [
                str(obj) for obj in storage.all().values()
                if obj.__class__.__name__ == class_name
            ]
        else:
            object_list = [str(obj) for obj in storage.all().values()]

        print(object_list)

    def do_update(self, arg):
        """
        Updates an instance based on the class name and id

        Usage: update <class name> <id> <attribute name> <attribute value>
        """
        all_obj = storage.all()
        args = custom_parser(arg)

        if len(args) == 0:
            print(CLASS_NAME_MISSING)
            return

        class_name = args[0]
        if class_name not in classes:
            print(NO_CLASS)
            return

        if len(args) == 1:
            print(INST_ID_MISSING)
            return

        instance_id = f"{class_name}.{args[1]}"
        if instance_id not in all_obj.keys():
            print(INST_NOT_FOUND)
            return

        if len(args) == 2:
            print(ATTR_NAME_MISSING)
            return

        if len(args) == 3:
            try:
                type(eval(args[2])) != dict
            except NameError:
                print(ATTR_VALUE_MISSING)
                return

        obj = all_obj[instance_id]
        attr_name = args[2]
        if len(args) == 4:
            attr_value = args[3]
            if attr_name in obj.__class__.__dict__.keys():
                val_type = type(obj.__class__.__dict__[attr_name])
                obj.__dict__[attr_name] = val_type(attr_value)
            else:
                obj.__dict__[attr_name] = attr_value
        else:
            for key, value in eval(attr_name).items():
                if (key in obj.__class__.__dict__.keys() and
                        type(obj.__class__.__dict__[key]) in
                        {str, int, float}):
                    val_type = type(obj.__class__.__dict__[key])
                    obj.__dict__[key] = val_type(value)
                else:
                    obj.__dict__[key] = value

        storage.save()

    def emptyline(self):
        """Does not execute anything"""
        pass

    def do_count(self, arg):
        """
        Retrieves the number of instances of a class

        Usage: <class name>.count()
        """
        args = custom_parser(arg)
        count = 0
        for obj in storage.all().values():
            if args[0] == obj.__class__.__name__:
                count += 1
        print(count)

    def default(self, arg):
        """
        Called when the command prefix is not recognized
        """

        command_mapping = {
            "all": self.do_all,
            "show": self.do_show,
            "destroy": self.do_destroy,
            "update": self.do_update,
            "count": self.do_count
        }

        args = arg.split('.', 1)
        if len(args) == 2:
            search = re.search(r"\((.*?)\)", args[1])
            if search:
                command = [args[1][:search.span()[0]], search.group()[1:-1]]
                if command[0] in command_mapping.keys():
                    call = f"{args[0]} {command[1]}"
                    return command_mapping[command[0]](call)

        print(f"*** Unknown syntax: {arg}")

def custom_parser(arg):
    """
    Parse command arguments to a list
    """
    curly_match = re.search(r"\{(.*?)\}", arg)
    bracket_match = re.search(r"\[(.*?)\]", arg)

    if curly_match is None:
        if bracket_match is None:
            return [item.strip(",") for item in split(arg)]
        else:
            tokens = split(arg[:bracket_match.span()[0]])
            result_list = [item.strip(",") for item in tokens]
            result_list.append(bracket_match.group())
            return result_list
    else:
        tokens = split(arg[:curly_match.span()[0]])
        result_list = [item.strip(",") for item in tokens]
        result_list.append(curly_match.group())
        return result_list

if __name__ == '__main__':
    HBNBCommand().cmdloop()


import os
from opg.util.xmlParseTool  import Xml_Parserfile
from inspect import ismethod
import time,functools
from opg.util.fileOper import walk_dir_test
from opg.util.lginfo import logger
def decorator(param,userType = None):
    def _decorator(fun):
        if not isinstance(fun, type):
           @functools.wraps(fun)
           def wrapper(*args, **kwargs):
               logger.info(msg="前置调用函数%s,类%s" % (fun.__name__, str(args[0])))
               start   = time.time()
               if userType is not None:
                  args[0].inputKV["userType"] = userType
               rsp = fun(*args, **kwargs)
               if userType is not None:
                  del args[0].inputKV["userType"]
               runtime = time.time() - start
               return rsp
           wrapper.__decorator__ = True
           wrapper.__param__     = param
        return wrapper
    return _decorator

def resultData(param):
    def _resultData(fun):
        if not isinstance(fun, type):
           @functools.wraps(fun)
           def wrapper(*args, **kwargs):
               logger.info(msg="前置调用函数%s,类%s" % (fun.__name__, str(args[0])))
               start   = time.time()
               rsp = fun(*args, **kwargs)
               runtime = time.time() - start
               return rsp
           wrapper.__resultData__ = True
           wrapper.__param__     = param
        return wrapper
    return _resultData
def loadStrFromFile(filepath = ""):
    load_str = ""
    if os.path.exists(filepath):
        with open(filepath, 'r',encoding="utf-8") as load_f:
             lines    = load_f.readlines()
             load_str = "".join(lines)
             load_str = load_str.replace("\n\t", "")
    return load_str
from opg.util.dbtools import Database
class UopService(object):
    fmtdict = None
    token = None
    def __init__(self,module       = ""  ,
                      filename     = ""  ,
                      sqlvaluedict = {}  ,
                      reqjsonfile  = None ,
                      dbName       = None):
        """
            :param module :
            :param filename :
            :param sqlvaluedict :
        """
        self.module   = module
        self.filename = filename
        self.inputKV  = sqlvaluedict
        self.sqldict  = {}
        self.ifacedict       = {}
        self.compareFuncDict = {}
        self.reqjsondata = ""
        self.rsp         = None
        self.lsser       = [self,]
        self.dbName      = dbName
        self.initDbOperator()
        UopService.initFmtDict()
        self.initReqJsonData( reqjsonfile = reqjsonfile ,
                              reqjsondata = self.inputKV )
        self.dbManager = Database()

    @classmethod
    def initFmtDict(cls):
        if cls.fmtdict is None:
           cls.fmtdict = {}
           a = walk_dir_test( dir = os.getcwd() , sign = "Req" , endstr = ".txt" )
           b = walk_dir_test( dir = os.getcwd() , sign = "Fmt" , endstr = ".json")
           for cs in a + b:
               cls.fmtdict[os.path.basename(cs).split(".")[0]] = cs

    def resetToken(self):
        pass

    def initReqJsonData(self,reqjsonfile = "",reqjsondata = None):
        self.reqjsondata = self.getReqJsonData(reqjsonfile = reqjsonfile,
                                               reqjsondata = reqjsondata)

    def getReqJsonData(self,reqjsonfile = "",
                            reqjsondata = None):
        jsondata = None
        if reqjsonfile is not None and reqjsondata != "":
           if reqjsonfile.endswith(".txt"):
              jsonpath = os.getcwd() + reqjsonfile
           else:
              jsonpath = UopService.fmtdict[reqjsonfile]
           reqDataFmt   = loadStrFromFile(filepath = jsonpath)
           reqdata     = reqDataFmt % reqjsondata
           try:
              jsondata = eval(reqdata)
           except SyntaxError as err:
              jsondata = reqdata
              print("SyntaxError:",err)
           except Exception as e:
              raise e
           return jsondata

    def initInterfaceDict(self):
        for name in dir(self):
            funObj = getattr(self, name)
            if ismethod(funObj):
                curdoc = getattr(funObj , "__doc__")
                if curdoc is not None:
                    #print("curdoc"+str(curdoc))
                   sign = curdoc.split("\n")[1].strip()
                   if sign.startswith("Sign"):
                      funSign = sign.split(":")[1]
                      self.ifacedict[funSign] = funObj

    def initInterfaceData(self,sign = None):
        for name in dir(self):
            funObj = getattr(self, name)
            #if ismethod(funObj) and (sign or (name != "sendHttpReq")):
            if ismethod(funObj):
               signDec  = getattr(funObj, "__decorator__",False)
               signName = getattr(funObj, "__param__", False)
               if signDec :
                  if isinstance(signName,str):
                     self.ifacedict[signName] = [sign,funObj]
                  else:
                     if isinstance(signName,list):
                        for name in signName:
                            self.ifacedict[name] = [sign,funObj]

    def initCompareResultFunData(self,sign = None):
        for name in dir(self):
            funObj = getattr(self, name)
            #if ismethod(funObj) and (sign or (name != "sendHttpReq")):
            if ismethod(funObj):
               signDec  = getattr(funObj, "__resultData__",False)
               signName = getattr(funObj, "__param__", False)
               if signDec :
                  if isinstance(signName,str):
                     self.compareFuncDict[signName] = funObj
                  else:
                     if isinstance(signName,list):
                        for name in signName:
                            self.compareFuncDict[name] = funObj

    def initInterfaceDataT(self):
        for serv in self.lsser:
            for name in dir(serv):
                funObj = getattr(serv, name)
                if ismethod(funObj):
                    signDec = getattr(funObj, "__decorator__",False)
                    signName = getattr(funObj, "__param__", False)
                    if signDec :
                        if isinstance(signName,str):
                           self.ifacedict[signName] = funObj
                        else:
                            if isinstance(signName,list):
                               for name in signName:
                                   self.ifacedict[name] = funObj

    def initDbOperator(self):
        if self.filename is not None and self.filename != "" :
           xmlsqlpath = os.path.join(os.getcwd(),
                                     'steamdb',
                                      self.module, self.filename)
           xmlsqlfile = Xml_Parserfile(filename = xmlsqlpath)
           itsql = xmlsqlfile.parserSql()
            #a = self.inputKV
           for cursql in itsql :
               self.sqldict[cursql[0]] = (cursql[1], cursql[2] , cursql[3])

    def getTownList(self,predata = []):
        """
            :param predata:
            :return:
        """
        prels = []
        if predata is not None:
            for pre in predata:
                prels.append(self.sqldict[pre][2])
        return prels

    def handlingDb(self,sqlls = ()):
        """
            :param sqlls:
            :return:
        """
        if self.dbName is not None:
           # self.initDbOperator()
           # dbManager = Database()
           operDict = {
                         "delete" : self.dbManager.deleteData,
                         "add":      self.dbManager.insertData,
                         "update":  self.dbManager.updateData
                      }
           for sqlSign in sqlls:
               sqlOperType = self.sqldict[sqlSign][0]
               sqlStr      = self.sqldict[sqlSign][1] % self.inputKV
               operDict[sqlOperType](sqlStr,self.dbName)

    def handlingOneDb(self,sqlstr = ""):
        """
            :param sqlls:
            :return:
        """
        if self.dbName is not None:
           # self.initDbOperator()
           # dbManager = Database()
           operDict = {
                         "delete" : self.dbManager.deleteData,
                         "add":      self.dbManager.insertData,
                         "update":  self.dbManager.updateData
                      }
           sqlOperType = self.sqldict[sqlstr][0]
           sqlStr      = self.sqldict[sqlstr][1] % self.inputKV
           operDict[sqlOperType](sqlStr,self.dbName)

    def handlingInterface(self , interacels = ()):
        f = lambda x:self.ifacedict[x][1]()
        rst = list(map(f,interacels))
        print(rst)

    def getDbManager(self):
        return self.dbManager

    def selectBySqlName(self,sqlname):
        sqlstr    = self.sqldict[sqlname][1] % self.inputKV
        qurResult = self.dbManager.queryAll(sqlstr)
        if qurResult is not None and len(qurResult)>0:
           return qurResult[0][0]
        return None

    def selectAllDataBySqlName(self,sqlname):
        sqlstr = self.sqldict[sqlname][1] % self.inputKV
        qurResult = self.dbManager.queryAll(sqlstr,self.dbName)
        return qurResult

    def deleteBySqlName(self,sqlname):
        sqlstr = self.sqldict[sqlname][1] % self.inputKV
        self.dbManager.deleteData(sql = sqlstr,dbName=self.dbName)

    def setInPutData(self):
        pass

    def sendInterfaceUrlReq(self):
        pass

if __name__ == '__main__':
  pt  = os.path.abspath(os.path.join(os.getcwd(), "../.."))
  print(pt)
  t = {"a":"b","c":"d"}
  for m in t:
      print(m)


from threading import local

from django.core.cache.backends.base import InvalidCacheBackendError
from django.core.cache.backends.memcached import BaseMemcachedCache

try:
    from . import client
except ImportError:
    raise InvalidCacheBackendError('Could not import pymemcache.')

class PyMemcacheCache(BaseMemcachedCache):
    """An implementation of a cache binding using pymemcache."""

    def __init__(self, server, params):
        self._local = local()
        super(PyMemcacheCache, self).__init__(
            server,
            params,
            library=client,
            value_not_found_exception=KeyError,
        )

    @property
    def _cache(self):
        client = getattr(self._local, 'client', None)
        if client:
            return client

        client = self._lib.Client(self._servers, **self._options)
        if self._options:
            client.behaviors = self._options

        self._local.client = client

        return client


from django.urls import path
from musico import views

app_name = 'musico'

urlpatterns = [
    path('listar/', views.MusicoListView.as_view(), name='read'),
    path('criar/', views.MusicoCreateView.as_view(), name='create'),
    path('editar/<int:pk>/', views.MusicoUpdateView.as_view(), name='update'),
    path('excluir/<int:pk>/', views.MusicoDeleteView.as_view(), name='delete'),
]


import sys
sys.stdin = open("07.minimum.txt")
import time
start_time = time.time()

def perm(n,k,q):
    global min
    if q>=min:return
    if n==k:
        if q<min:
            min=q
    else:
        for i in range(k,n):
            A[i],A[k]=A[k],A[i]
            perm(n,k+1,q+sheet[A[k]][k])
            A[i], A[k] = A[k], A[i]

T=int(input())

for i in range(T):
    N=int(input())
    sheet=[[int(x) for x in input().split()]for y in range(N)]
    A=[int(x) for x in range(N)]
    min=98765
    perm(N,0,0)
    print('#{} {}'.format(i+1,min))
print(time.time() - start_time, 'seconds')

# count = 0
# total = 0
# N = 10
# # 원소의 포함여부 저장 (0, 1)

# def powerset(n, y, sol):      # n: 원소의 갯수, k: 현재depth
#     global min
#
#     if n == y:          # Basis Part
#         if min>=sol:
#             min=sol
#     else:               # Inductive Part
#         for k in range(N):
#             if A[k] == 1:
#                 continue
#             A[k] = 1        # k번 요소 O
#             powerset(n, y + 1, sol+data[y][k])  # 다음 요소 포함 여부 결정
#             A[k] = 0       # k번 요소 X
#
# # print("호출회수 : {}".format(total) )
#
# T = int(input())
# for tc in range(1,T+1):
#     N = int(input())
#     A = [0 for _ in range(N)]
#     data = [list(map(int, input().split())) for _ in range(N)]
#     min=98765
#     powerset(N, 0, 0)
#     print(min)
#     print(time.time() - start_time, 'seconds')


import supybot.conf as conf
import supybot.registry as registry
try:
    from supybot.i18n import PluginInternationalization
    _ = PluginInternationalization('Ericpedia')
except:
    # Placeholder that allows to run the plugin on a bot
    # without the i18n module
    _ = lambda x: x

def configure(advanced):
    # This will be called by supybot to configure this module.  advanced is
    # a bool that specifies whether the user identified themself as an advanced
    # user or not.  You should effect your configuration by manipulating the
    # registry as appropriate.
    from supybot.questions import expect, anything, something, yn
    conf.registerPlugin('Ericpedia', True)

Ericpedia = conf.registerPlugin('Ericpedia')
# This is where your configuration variables (if any) should go.  For example:
conf.registerChannelValue(Ericpedia, 'replaceString',
    registry.String("$nick", _("""Replaces random part of the title with this string or $nick for the nick of the person issuing the command.""")))

conf.registerChannelValue(Ericpedia, 'wikipediaRandomPageURL',
    registry.String("https://en.wikipedia.org/wiki/Special:Random", _("""URL used to find a random Wikipedia page""")))

# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:


from confy import env
from django.conf import settings

# from ledger.payments.helpers import is_payment_admin

from licensing_template.settings import (
    KMI_SERVER_URL,
    template_group,
    template_title,
    BUILD_TAG,
)

def licensing_template_url(request):

    return {
        "KMI_SERVER_URL": KMI_SERVER_URL,
        "template_group": template_group,
        "template_title": template_title,
        "build_tag": BUILD_TAG,
    }


from django.urls import path

from .views import ProjectListView, ProjectCreateFormView, ProjectUpdateView, ProjectDeleteView

urlpatterns = [
    path('', ProjectListView.as_view(), name='projects'),
    path('create/', ProjectCreateFormView.as_view(), name='project_create'),
    path('<int:pk>/update/', ProjectUpdateView.as_view(), name='project_update'),
    path('<int:pk>/delete/', ProjectDeleteView.as_view(), name='project_delete'),
]