import connection
import os

class Curso:
    con = connection.conectar()

    def menu_cur(self):
        os.system('cls')
        print('=====Curso=====')
        print('1 - Listar')
        print('2 - Inserir')
        print('3 - Deletar')
        print('4 - Atualizar')
        opt = input()
        if int(opt) == 1:
            self.listagem_cur()
        elif int(opt) == 2:
            self.inserir_cur(titulo=input('Titulo:'),
                             descricao=input('Descrição:'),
                             keyword=input('Palavra-Chave:'),
                             duracao=input('Duração:'),
                             pathPNG=input('Caminho PNG: '),
                             fk1=input('Foreign Key Categoria'),
                             fk2=input('Foreign Key Instrutor'))
        elif int(opt) == 3:
            self.delete_cur(id=input('ID:'))
        elif int(opt) == 4:
            self.update_cur(coluna=input('Coluna:'), valor=input('Valor:'), id=input('ID:'))
        else:
            print('Valor Inválido')

    def listagem_cur(self):
        with self.con.cursor() as cursor:
            sql = 'select * from tbcurso'
            cursor.execute(sql)
            dados = cursor.fetchall()
            self.con.commit()
            cursor.close()
            for linhas in dados:
                print(linhas)
        os.system('PAUSE')

    def inserir_cur(self, **kwargs):

        with self.con.cursor() as cursor:
            sql = 'insert into tbcurso(titulo,descricao,palavrachave,duracao,caminhoIMG,' \
                  'fk_tbcategoria_id,fk_tbinstrutor_id) values(%s,%s,%s,%s,%s,%s,%s)'
            cursor.execute(sql, (kwargs['titulo'], kwargs['titulo'], kwargs['titulo'], kwargs['titulo'], kwargs['titulo'],
                                 kwargs['titulo'], kwargs['titulo']))

            self.con.commit()
            cursor.close()

    def delete_cur(self, **kwargs):

        with self.con.cursor() as cursor:
            sql = 'delete from tbcurso where id = %s'
            print(kwargs)
            cursor.execute(sql, (kwargs['id']))
            self.con.commit()
            cursor.close()

    def update_cur(self, **kwargs):

        with self.con.cursor() as cursor:
            sql = "update tbcurso set {} = %s where id = %s".format(kwargs['coluna'])
            cursor.execute(sql, (kwargs['valor'], kwargs['id']))
            self.con.commit()
            cursor.close()


# Problem Statement: https://leetcode.com/problems/n-ary-tree-preorder-traversal/

"""
# Definition for a Node.
class Node(object):
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children
"""

class Solution(object):
    def preorder(self, root):
        """
        :type root: Node
        :rtype: List[int]
        """
        if root is None:
            return []

        stack = [root]
        output = []

        # Till there is element in stack the loop runs.
        while stack:

            #pop the last element from the stack and store it into temp.
            temp = stack.pop()

            # append. the value of temp to output
            output.append(temp.val)

            #add the children of the temp into the stack in reverse order.
            # children of 1 = [3,2,4], if not reveresed then 4 will be popped out first from the stack.
            # if reversed then stack = [4,2,3]. Here 3 will pop out first.
            # This continues till the stack is empty.
            stack.extend(temp.children[::-1])

        #return the output
        return output

Easy/589. N-ary Tree Preorder Traversal.py


import time

import pandas as pd
import requests
import json
import logging

def get_price_from_daum(ticker, datacount=2000, adj_flag=True, interval="days"):
    """
    unadj is working at only "days" case
    otherwise, always do adjustment

    :param ticker:
    :param datacount:
    :param adj_flag:
    :param interval:
    :return:
    """
    url = "https://finance.daum.net/api/charts/A{}/{}?limit=200&adjusted={}".format(str(ticker), interval,
                                                                                "true" if adj_flag else "false")
    headers = {
        "referer": "https://finance.daum.net/chart/A{}".format(str(ticker)),
        "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36"
    }
    if adj_flag:
        params = {
            "limit": str(datacount),
            "adjusted": "true"
        }
    else:
        params = {
            "limit": str(datacount),
            "adjusted": "false"
        }

    try:
        resp = requests.get(url, headers=headers, params=params)
        data = resp.json()
        result = pd.DataFrame.from_dict(data["data"])
        result["symbolCode"] = result["symbolCode"].map(lambda x: x[1:])
        result["candleTime"] = result["candleTime"].map(lambda x: x[:10])
        result["ADJ"] = "ADJ" if adj_flag else "UNADJ"
        result = result.rename(columns={"date": "DATE", "candleTime": "STARTDATE", "tradePrice": "CLOSE",
                                        "highPrice": "HIGH", "lowPrice": "LOW", "openingPrice": "OPEN",
                                        "candleAccTradeVolume": "VOLUME", "symbolCode": "TICKER"})
        result = result[["DATE", "STARTDATE", "OPEN", "HIGH", "LOW", "CLOSE", "VOLUME", "TICKER", "ADJ"]]
    except:
        result = None
    return result

def test_get_price_from_daum():
    test_ticker = "035720"
    test_datacount = 900
    test_interval = "days"  # days
    test_adjust = True

    logging.info("daum history")
    actual_adj = get_price_from_daum(test_ticker, test_datacount, test_adjust, test_interval)
    logging.info("date min, max")
    logging.info((actual_adj.DATE.min(), actual_adj.DATE.max()))
    test_adjust = False
    actual_unadj = get_price_from_daum(test_ticker, test_datacount, test_adjust, test_interval)
    actual_comb = pd.merge(actual_adj[["TICKER", "DATE", "CLOSE", "ADJ"]].rename(columns={"CLOSE": "ADJCLOSE"}),
                           actual_unadj[["TICKER", "DATE", "CLOSE", "ADJ"]].rename(columns={"ADJ": "UNADJ_ADJ",
                                                                                "CLOSE": "UNADJCLOSE"}),
                           on=["TICKER", "DATE"], how="inner")
    logging.info(actual_comb.dtypes)
    logging.info(actual_comb.head(100))

    day_unadj = get_price_from_daum(test_ticker, 30, False, "weeks")
    logging.info(day_unadj.shape)
    logging.info(day_unadj[day_unadj["DATE"].map(lambda x: pd.to_datetime(x, format="%Y-%m-%d").month in [2, 3, 4])])

def get_adj_unadj_day_price(tickers, datacount, sleep_time_every_100=10):
    failed = []
    prices = []
    for i, t in enumerate(tickers):
        try:
            adj = get_price_from_daum(t, datacount, True, "days").drop(columns=["ADJ"])
            unadj = get_price_from_daum(t, datacount, False, "days")
            both = pd.merge(adj,
                            unadj[["TICKER", "DATE", "CLOSE"]].rename(columns={"CLOSE": "UNADJCLOSE"}),
                            on=["TICKER", "DATE"], how="inner")
            prices.append(both)
            logging.info("{}, {}% ticker = {} history download - adj, unadj both".format(str(i), str(round(
                i / len(tickers) * 100, 2)), t))
            if i % 100 == 99:
                time.sleep(sleep_time_every_100)
                logging.info("every 100 tickers, sleep during {} seconds".format(str(sleep_time_every_100)))
        except Exception as e:
            logging.info("{} skipped because of {}".format(t, str(e)))
            failed.append(t)
    return pd.concat(prices, ignore_index=True), failed

def test_get_adj_unadj_day_price():
    test_tickers = ["005930", "035720"]
    test_datacount = 2000
    actual, failed = get_adj_unadj_day_price(test_tickers, test_datacount)
    logging.info(actual.TICKER.unique())
    logging.info("date min, max")
    logging.info((actual.DATE.min(), actual.DATE.max()))
    logging.info(actual.head())
    logging.info(actual[actual["DATE"].map(
        lambda x: pd.to_datetime(x, format="%Y-%m-%d").year * 100 + pd.to_datetime(x, format="%Y-%m-%d").month in [
            202103, 201803, 201804])])
    logging.info(actual.dtypes)


### Immutable ###
a = 1 # 전역변수
def vartest(a): # 지역변수
    a = a+1
vartest(a)
print(a)

### mutable ###
b = [1,2,3]
def vartest2(b):
    b=b.append(4)
vartest2(b)
print(b)


#!/usr/bin/env python3

descr = """
This script will analyse integers occurring at the end of each line of a text
file. It will output their sum and the largest and smallest integers.
"""

import argparse
import itertools
import math
import re

# Pattern of an integer occurring at the end of a line (preceded by whitespace)
pattern = re.compile("\s[0-9]+$")

def line_integer(line):
    """Get trailing integer from a line."""
    match = pattern.search(line)
    if match:
        return int(match.group())
    else:
        return float('nan')

def process_file(file):
    """Print the sum, minimum and maximum of trailing integers in a file."""

    total = 0
    min_i = float('inf')
    max_i = -float('inf')

    with open(file, "r") as f:
        for i in itertools.filterfalse(math.isnan, map(line_integer, f)):
            total = total + i
            min_i = min(min_i, i)
            max_i = max(max_i, i)

    print("{file}: sum {total}".format(**locals()))
    print("{file}: min {min_i}".format(**locals()))
    print("{file}: max {max_i}".format(**locals()))

# Define and parse the script arguments
parser = argparse.ArgumentParser(description=descr)
parser.add_argument("file", nargs='+')
args = parser.parse_args()

# Run the actual job
for file in args.file:
    process_file(file)


import sys, os, stat
from optparse import OptionParser

# # Parse Command Line
# usage = """
# Description: taxonomy comparsion tool

# """

# parser = OptionParser(usage=usage)
# parser.add_option("-i", "--input", dest="dir_input",
# 	default=None, metavar="FILE",
# 	help="input directory (required)")
# parser.add_option("-o", "--output", dest="dir_out",
# 	default=None, metavar="FILE",
# 	help="Output filename (required)")

# options,args = parser.parse_args()

# if not options.dir_input:
# 	sys.exit("Missing input fasta file, -i dir")
# if not os.path.isfile(options.dir_input):
# 	sys.exit("Missing input fasta file, %r" % options.dir_input)
# if not options.dir_out:
# 	sys.exit("Missing output directory, -o xxxxx")
# if not options.node:
# 	sys.exit("Missing number of node")
# list_input_file = ["visualize_taxonomy/GOEL01S1L001.level_7.converted.txt", "visualize_taxonomy/GOEL02S2L001.level_7.converted.txt" ]
list_input_file = sys.argv[1:]
short_name = []
sum_species = []
for file_name in list_input_file:
	file_name = file_name.split("/")[-1].split(".")[0]
	short_name.append(file_name)

list_taxa = {}
for i in range(len(list_input_file)):
	input_file = os.path.abspath(list_input_file[i])
	num_species = 0
	file = open(input_file, 'r')
	for line in file:
		num = int(line[:line.find('\t')])
		taxa = line[line.find('\t')+1:-1]
		if taxa not in list_taxa.keys():
			list_taxa[taxa]=[]
			for j in range(len(list_input_file)):
				list_taxa[taxa].append(0)
			list_taxa[taxa][i] = num
			num_species+=num
		else:
			list_taxa[taxa][i] = num
			num_species+=num
	sum_species.append(num_species)

for name in short_name:
	sys.stdout.write(name + "\t")
sys.stdout.write("taxonomic\n")

for taxa, num in list_taxa.items():
	for num_taxa in num:
		sys.stdout.write(str(num_taxa) + "\t")
	sys.stdout.write(taxa.replace("\t","__") + "\n")

for num_species in sum_species:
	sys.stdout.write(str(num_species) + "\t")
sys.stdout.write("\n")


import requests
import multiprocessing as mp
cookie = {'session':''}
def post(url):
    return requests.post(url+'free',cookies=cookie)
urls = ["http://tcp.olymp.hackforces.com:8090/" for i in range(80)]
if __name__ == '__main__':
    with mp.Pool(10) as pool:
        res = pool.map(post, urls)
        print(res)


# coding=utf8
import time
from psutil import net_io_counters
import tkinter as tk
import threading

KB = 1024
MB = KB * 1024
GB = MB * 1024

def format_speed_unit(speed: int):
    if speed < KB:
        return f"{speed}B/s"
    elif speed < MB:
        return f"{speed / KB:.2f}KB/s"
    elif speed < GB:
        return f"{speed / MB:.2f}MB/s"
    else:
        return f"{speed / GB:.2f}GB/s"

current_net_speed = f"↑{format_speed_unit(0)} ↓{format_speed_unit(0)}"

def get_net_speed():
    global current_net_speed
    while True:
        sent_before = net_io_counters().bytes_sent
        recv_before = net_io_counters().bytes_recv
        time.sleep(1)
        sent_now = net_io_counters().bytes_sent
        recv_now = net_io_counters().bytes_recv
        upload_speed = (sent_now - sent_before)
        download_speed = (recv_now - recv_before)
        current_net_speed = f"↑{format_speed_unit(upload_speed)} ↓{format_speed_unit(download_speed)}"

def update_speed():
    global current_net_speed
    label.config(text=current_net_speed)
    root.after(1000, update_speed)

if __name__ == '__main__':
    thread_a = threading.Thread(target=get_net_speed)
    thread_a.start()

    root = tk.Tk()
    label = tk.Label(root)
    label.pack()
    update_speed()
    root.mainloop()


# leetcode 38. 报数

class Solution:
    def countAndSay(self, n):
        """
        :type n: int
        :rtype: str
        """
        if n==1:#类似于斐波拉契数，后面的数跟前面的数有关
            return '1'
        if n==2:
            return '11'
        pre='11'
        for i in range(3,n+1):
            res=''#结果，每次报数都要初始化
            cnt=1#计数
            length=len(pre)
            for j in range(1,length):
                if pre[j-1]==pre[j]:#相等则加一
                    cnt+=1
                else:
                    res+=str(cnt)+pre[j-1]
                    cnt=1#重置为1
            res+=str(cnt)+pre[j]
            pre=res#保存上一次的结果
        return res


from pupa.celery import app
from django.core.mail import send_mail
from .models import Order

@app.task
def order_created(order_id):
    # отправка уведомления по электронной почте при успешном создании заказа
    # send email notification when order is successfully created
    order = Order.objects.get(id=order_id)
    subject = 'Заказ №. {}'.format(order_id)
    message = 'Привет {0} (◕‿◕),\n\n\
    Рад сообщить тебе, что твой заказ под номером {1}\n\
    был успешно оформлен! ٩(◕‿◕)۶\n\n\
    Спасибо тебе (◕‿◕)♡,\n\
    С уважением yamitoys!'.format(order.first_name, order.id)
    mail_sent = send_mail(subject,
                          message,
                          'yamitoys@gmail.com',
                          [order.email])
    return mail_sent


import numpy as np

from .. import impurities, metrics
from ..ensemble.meta_algorithms import Bagging
from ..base.models import Model
from ..regression.models import LinearRegression
from ..utils.data_utils import permutate
from ..utils.decorator_utils import fit_method, predict_method
from ..utils.operation_utils import sign
from .optimizers import DecisionStumpSolver, CART, RandomTree

class DecisionStump(Model):
    model_type = "binary-classifier"

    def __init__(self, metrics=[metrics.Accuracy()]):
        self.feature_ = None
        self.threshold_ = None
        self.sign_ = 0

        self.compile(optimizer = DecisionStumpSolver(), metrics=metrics)

    @fit_method
    def fit(self, X, y):
        self.sign_, self.feature_, self.threshold_ = self.optimizer.execute(X, y)
        return self

    @predict_method
    def predict(self, X):
        x = X[:, self.feature_]
        theta = self.threshold_
        s = self.sign_
        return s * sign(x - theta)

class DecisionTree(Model):
    model_type = ["binary-classifier", "regressor"]

    def __init__(
        self,
        criterion = impurities.GiniIndex(),
        max_height = np.inf,
        optimizer = CART(),
        metrics = [metrics.Accuracy()]
    ):
        self.model_type = criterion.problem_type
        self.tree_ = None

        self.compile(criterion = criterion,
                     optimizer = optimizer,
                     max_height = max_height,
                     metrics = metrics)

    @fit_method
    def fit(self, X, y):
        self.tree_ = self.optimizer.execute(X, y)
        return self

    @predict_method
    def predict(self, X):
        return self.tree_.predict(X)

class RandomForest(Model):
    model_type = ["binary-classifier", "regressor"]

    meta_algorithm = Bagging(DecisionTree())

    def __init__(
        self,
        n_estimators = 5,
        optimizer = RandomTree(tree=CART(), criterion=impurities.GiniIndex(), random_subspace=(0.5, )),
        metrics = [metrics.Accuracy()],
        **kwargs
    ):
        self.model_type = optimizer.criterion.problem_type
        self.compile(
            n_estimators = n_estimators,
            optimizer = optimizer,
            metrics = metrics,
            **kwargs
        )

    @fit_method
    def fit(self, X, y, raw_importance=False):
        self.meta_algorithm = self.meta_algorithm.fit(X, y)
        self.feature_importances_ = self.eval_feature_importance(X, y, raw_importance)
        return self

    @predict_method
    def predict(self, X):
        return self.meta_algorithm.predict(X)

    def eval_feature_importance(self, X, y, raw=False):
        """Evaluate raw feature importance

        The instructive annotation comes from the referance of random forest
        by Leo Breiman and Adele Cutler.
        See: https://www.stat.berkeley.edu/~breiman/RandomForests/cc_home.htm#varimp

        Parameters
        ----------
        X: 2-D array  (n_samples, n_features)
            The feature part of the training data

        y: 1-D array like   (n_samples, )
            The value or label from the training data

        raw: bool, default=False
            If `raw==True`, use the votes cast for right
            class as the evaluating metric in classification
            problem. If `raw==False`, use accuracy.

        Return
        ------
        importances: 1-D array (n_features, )
            Feature importance evaluated based on out-of-bag data.
        """
        bag = self.meta_algorithm
        oob_to_model = self.meta_algorithm.data_oob_status_
        correct_votes = np.empty(self.n_estimators)
        permuted_correct_votes = np.empty((X.shape[1], self.n_estimators))

        if self.model_type == "binary-classifier":
            if raw:
                metric = lambda y_pred, y_true: sum(y_pred == y_true)
            else:
                metric = metrics.Accuracy()
        elif self.model_type == "regressor":
            metric = metrics.R2()
        else:
            raise ValueError("Invalid model_type")

        # (1) In every tree grown in the forest, put down the oob cases
        # and evaluate the metrics (accuracy for classification and r^2 for
        # regression).
        # The author use votes for the correct as the metric (raw == True),
        # so we use "correct_votes" the variable name to match the instruction.
        for i in range(self.n_estimators):
            X_oob = X[oob_to_model[:, i], :]
            y_oob = y[oob_to_model[:, i]]
            y_pred = bag.base_models[i].predict(X_oob)
            correct_votes[i] = metric(y_oob, y_pred)

            # (2) Now randomly permute the values of variable m
            # in the oob cases and put these cases down the tree.
            for m in range(X.shape[1]):
                X_oob_m_permuted = permutate(X_oob, feature_idx=m)
                y_pred_m_permuted = bag.base_models[i].predict(X_oob_m_permuted)
                permuted_correct_votes[m, i] = metric(y_oob, y_pred_m_permuted)

        # (3) Subtract the metric evaluated in the variable-m-permuted
        #  oob data from the metric evaluated in the untouched oob data.
        performance_in_trees = correct_votes - permuted_correct_votes

        # (4) The average of this number over all trees in the forest
        # is the importance score for variable m.
        importances = np.mean(performance_in_trees, axis=1)

        return importances

class GradientBoostedDecisionTree(Model):
    model_type = "regressor"

    def __init__(
        self,
        n_estimators = 5,
        max_height = 5,
        metrics = [metrics.MeanSquaredError()]
    ):
        self.model_weight_ = np.empty(0)
        self.base_regression_trees_ = [DecisionTree(criterion = impurities.MeanSquaredError(),
                                                   max_height = max_height,
                                                   metrics = metrics) for _ in range(n_estimators)]
        self.compile(
            n_estimators = n_estimators,
            max_height = max_height,
            metrics = metrics
        )

    @fit_method
    def fit(self, X, y):

        residuals_i = y
        for i in range(self.n_estimators):
            # (1) obtain base-model_i by A({(xn, yn − sn)}) where `A`
            #     is regression tree algorithm, `sn` are predicting
            #     values evaluated by current boosting model. Therefore,
            #     `yn - sn` are the residuals.
            model_i = self.base_regression_trees_[i].fit(X, residuals_i)
            y_pred_i = model_i.predict(X)[:, None]

            # (2) compute the model weight for model_i by regressing residuals
            #     `yn − sn` on the predicting values of current model `gt(xn)`.
            #     Note that it is an "single variable linear regression".
            coef_i = LinearRegression().fit(y_pred_i, residuals_i).weight_[1]
            self.model_weight_ = np.append(self.model_weight_, coef_i)

            # (3) After the update of model weight has been done, compute the
            #     prediction based on the updated boosting model, and then
            #     substract the prediction from y to set new residuals for
            #     the next round of training.
            residuals_i = y - self.predict(X)

        return self

    @predict_method
    def predict(self, X):

        n_estimators = len(self.model_weight_)
        polls = np.empty((X.shape[0], n_estimators))

        for i in range(n_estimators):
            polls[:, i] = self.base_regression_trees_[i].predict(X)

        return polls @ self.model_weight_


"""
This is a script to convert curve info in other formats to NRML, v0.5.

It is being modified on a needs basis.

"""

import csv
import numpy

import xlrd
from hazimp.misc import csv2dict

FLOOD_HOUSE_FABRIC = 'structural_domestic_flood_2012'
FLOOD_HOUSE_CONTENTS = 'contents_domestic_flood_2012'
LOSS_CAT_FABRIC = 'structural_loss_ratio'
LOSS_CAT_CONTENTS = 'contents_loss_ratio'
FLOOD_IMT = 'water depth above ground floor (m)'

def xml_write_variable(xml_h, name, value):
    """
    Add a variable name and value to an xml file.

    :param xml_h: A handle to the xml file.
    :param name: The name of the variable.
    :param value: The value of the variable.
    """
    xml_h.write('%s="' % name)
    try:
        xml_h.write(value)
    except TypeError:
        if numpy.isnan(value):
            # This is what we need for blank string values.
            # Probably not universal though.
            xml_h.write('')
        else:
            # to rethrow error
            xml_h.write(value)
    xml_h.write('" ')

def write_nrml_top(xml_h, vulnerability_set_id, asset_category, loss_category):
    """
    Write the top section of an nrml file.

    :param xml_h: A handle to the xml file.
    :param vulnerability_set_id: String name of the vulnerability set.
    :param asset_category: String name of the assert category.
    :param loss_category: String name of the loss category.
    :param imt: String name of the intensity measure type.
    :param imls: 1D vector of the intensity measure values (x-axis) of the
                 vuln curve.
    """

    intro = """<?xml version='1.0' encoding='utf-8'?>
<nrml xmlns="http://openquake.org/xmlns/nrml/0.5"
      xmlns:gml="http://www.opengis.net/gml">

    <vulnerabilityModel """
    xml_h.write(intro)
    xml_write_variable(xml_h, "id", vulnerability_set_id)
    xml_write_variable(xml_h, "assetCategory", asset_category)
    xml_write_variable(xml_h, "lossCategory", loss_category)
    xml_h.write('>\n')

def write_nrml_curve(xml_h, vulnerability_function_id, imls: list, imt: str,
                     loss_ratio, coef_var):
    """
    Write the curve info of an nrml file.

    :param xml_h: A handle to the xml file.
    :param vulnerability_function_id: String name of the vuln function.
    :param imls: 1D vector of the intensity measure values (x-axis) of the
                 vuln curve.
    :param imt: intensity measure type
    :param loss_ratio: 1D vector of the loss ratio values (y-axis) of the
                 vuln curve.
    :param coef_var: 1D vector of the coefficient of variation values (y-axis)
                     of the vuln curve.
    """
    xml_h.write("<vulnerabilityFunction ")
    xml_write_variable(xml_h, "id",
                       vulnerability_function_id)
    xml_h.write('')
    xml_h.write('dist="LN">\n  <imls ')
    xml_write_variable(xml_h, "imt", imt)
    xml_h.write('>')
    for iml in imls:
        if numpy.isnan(iml):
            continue
        xml_h.write(str(iml) + ' ')
    xml_h.write('</imls>\n')
    xml_h.write('<meanLRs>')
    xml_h.write(loss_ratio)
    xml_h.write('</meanLRs>\n')
    xml_h.write('<covLRs>')
    xml_h.write(coef_var)
    xml_h.write('</covLRs>\n')
    xml_h.write('</vulnerabilityFunction>\n\n')

def write_nrml_close(xml_h):
    """
    Write the final section of an nrml file and close it.

    :param xml_h: A handle to the xml file.
    """
    xml_h.write('</vulnerabilityModel>\n')
    xml_h.write('</nrml>\n')

def csv_curve2nrml(csv_filename, xml_filename):
    """
    Read in a csv hazard curve file and convert it to an NRML file.

    :param csv_filename: The csv file to be read.
    :param xml_filename: The NRML file to be written.
    """
    # Read the file twice.
    # Once for the non-per-curve info and then
    # for the per curve info.

    csv_dict = csv2dict(csv_filename)
    vulnerability_set_id = csv_dict['vulnerabilitySetID'][0]
    try:
        asset_category = csv_dict['assetCategory'][0]
    except IndexError:
        # Assume asset_category is empty
        asset_category = ''
    loss_category = csv_dict['lossCategory'][0]
    imls = [v for v in csv_dict['IML'] if not v == '']

    # open the csv file to read the rows
    reader = csv.DictReader(open(csv_filename, 'r'))
    with open(xml_filename, 'w') as xml_h:
        write_nrml_top(xml_h, vulnerability_set_id, asset_category,
                       loss_category)

        # Loop over the csv file info
        for row in reader:
            row = {k.strip(): v.strip() for k, v in list(row.items())}
            if row['Alpha'] == 'N/A':
                # This row has no model
                continue
            coef_var = ''
            loss_ratio = ''
            for iml in imls:
                if numpy.isnan(iml):
                    continue
                loss_ratio += str(row[str(int(iml))]) + ' '
                coef_var += str((float(row['Alpha'])/float(row['Beta']))) + ' '
            write_nrml_curve(xml_h, row['vulnerabilityFunctionID'],
                             imls, csv_dict['IMT'][0],
                             loss_ratio, coef_var)

        write_nrml_close(xml_h)

def validate_excel_curve_data(excel_file):
    """
    Check that the titles and the water depths do not change
    from sheet to sheet.
    The first 2 rows are titles.
    The first coulmn is the water depth.

    :param excel_file: The excel file to validate.
    """

    default = None
    valid = True
    titles = {}
    wb = xlrd.open_workbook(excel_file)
    for s in wb.sheets():
        title = []
        # The first 3 rows should be titles that are the same,
        # except for the 2nd value on the 1st row.
        for row in [0, 1, 2]:
            values = []
            for col in range(s.ncols):
                val = s.cell(row, col).value

                # This is just for visualising.
                try:
                    val = str(val)
                except TypeError:
                    pass

                values.append(val)
            title.append(values)
        # Remove the  2nd value on the 1st row.
        del title[0][1]
        titles[s.name] = title
        default = title

    if default is None:
        valid = False
    else:
        # Check that all sheets have the same title info
        for title in list(titles.values()):
            if not title == default:
                print(("title", title))
                print(("default", default))
                valid = False
                break

    return valid and check_identical_depths(wb)

def check_identical_depths(wb):
    """
    Check that the depth values are the same for all workbooks.
    Check that the first colum, starting at the 4th row, is identical.

    :param wb: The excel workbook xlrd object.
    """

    valid = True
    default = None
    depths = {}
    for s in wb.sheets():
        values = []
        for row in range(3, s.nrows):
            col = 0
            val = s.cell(row, col).value
            values.append(val)
        depths[s.name] = values
        default = values

    if default is None:
        valid = False
    else:
        # Check that all sheets have the same title info
        for depth in list(depths.values()):
            if not depth == default:
                print(("depth", depth))
                print(("default", default))
                valid = False
                break

    return valid

def read_excel_curve_data(excel_file):
    """
    Read in the excel file info.  Specific, undocumented format.

    :param excel_file: The excel workbook.
    """
    wb = xlrd.open_workbook(excel_file)
    a_sheet = wb.sheets()[0]

    # Get a list of the depths
    depths = []
    for row in range(3, a_sheet.nrows):
        col = 0
        val = a_sheet.cell(row, col).value
        depths.append(val)
    fabric_vuln_curves, contents_vuln_curves = read_excel_worksheet(wb)

    return depths, fabric_vuln_curves, contents_vuln_curves

def read_excel_worksheet(wb):
    """
    Read an excel worksheet

    :param wb: The excel workbook xlrd object.
    """
    fabric_vuln_curves = {}  # the keys are curve names.
    contents_vuln_curves = {}  # the keys are curve names.

    for s in wb.sheets():
        di_block = []
        for row in range(3, s.nrows):
            values = []
            for col in range(s.ncols):
                values.append(s.cell(row, col).value)
            di_block.append(values)
        # Get individual curves from the curve block.
        # Convert the curves into an array
        di_array = numpy.asarray(di_block)
        insure = {"INSURED": 0, "UNINSURED": 4}
        for key in insure:
            # Read in the structure type
            # The 2nd value on the 1st row.
            curve_id_base = s.cell(0, 1).value.split()[0] + '_' + key
            fabric_vuln_curves[curve_id_base] = di_array[:, 1 + insure[key]]
            tag_offset = {'_SAVE': 2, '_NOACTION': 3, '_EXPOSE': 4}
            for tag in tag_offset:
                curve_id = curve_id_base + tag
                contents_vuln_curves[curve_id] = di_array[:, tag_offset[tag]
                                                          + insure[key]]
    return fabric_vuln_curves, contents_vuln_curves

def excel_curve2nrml(contents_filename, fabric_filename, xls_filename):
    """
    Read in an excel flood curve file and convert it to an NRML file.

    The excel file format is specific and best understood by looking
    at the file flood_2012_test.xlsx.

    :param contents_filename: The contents NRML file to be created.
    :param fabric_filename: The fabric NRML file to be created.
    :param xls_filename: The excel file that is the basis of the NRML files.
    """

    validate_excel_curve_data(xls_filename)

    depths, fabric_vuln_curves, contents_vuln_curves = read_excel_curve_data(
        xls_filename)
    curve_info = [{'curves': fabric_vuln_curves,
                   'set_id': FLOOD_HOUSE_FABRIC,
                   'asset': '',
                   'loss_category': LOSS_CAT_FABRIC,
                   'file_name': fabric_filename},
                  {'curves': contents_vuln_curves,
                   'set_id': FLOOD_HOUSE_CONTENTS,
                   'asset': '',
                   'loss_category': LOSS_CAT_CONTENTS,
                   'file_name': contents_filename}]

    for set_id in curve_info:

        with open(set_id['file_name'], 'w') as xml_h:
            write_nrml_top(
                xml_h,
                set_id['set_id'],
                set_id['asset'],
                set_id['loss_category']
            )

            # Loop over the csv file info
            for curve_dic_key in set_id['curves']:
                curve_values = set_id['curves'][curve_dic_key]
                coef_var = ''
                loss_ratio = ''
                # creating the coef_var vector
                for iml in curve_values:
                    loss_ratio += str(iml) + ' '
                    coef_var += '0 '
                write_nrml_curve(xml_h, curve_dic_key, depths, FLOOD_IMT,
                                 loss_ratio, coef_var)

            write_nrml_close(xml_h)

# -----------------------------------------------------------
if __name__ == "__main__":

    import os
    import argparse
    parser = argparse.ArgumentParser(
        description="Convert curve data to NRML format"
        )

    parser.add_argument("-i", "--input", required=True,
                help="Input curve file (either Excel or csv)")
    parser.add_argument("-o", "--output", help="Output file name")
    parser.add_argument("-f", "--format", choices=['csv', 'xlsx'],
                help="File format (inferred from input file if not given)")

    args = parser.parse_args()

    input_file = args.input
    base, ext = os.path.splitext(input_file)

    if args.output:
        output_file = args.output
    else:
        output_file = f"{base}.xml"

    if args.format:
        informat = args.format
    else:
        if ext=='.csv':
            informat = 'csv'
        elif ext.strip('.') in ['xls', 'xlsx']:
            informat = 'xlsx'
        else:
            print("Not sure what the file format is")
            print("Use the -f option to specify")

    if informat == 'csv':
        csv_curve2nrml(input_file, output_file)
    elif informat == 'xlsx':
        output_contents_file = f"{base}_contents.xml"
        output_fabric_file = f"{base}_fabric.xml"
        excel_curve2nrml(output_contents_file, output_fabric_file, input_file)


from typing import List

class Solution:
    def finalPrices(self, prices: List[int]) -> List[int]:
        length = len(prices)
        answer = []

        for i in range(0, length - 1):
            price = prices[i]
            discount = 0

            for j in range(i + 1, length):
                if prices[j] <= prices[i]:
                    discount = prices[j]
                    break

            answer.append(price - discount)

        answer.append(prices[-1])
        return answer

print(Solution().finalPrices([8, 4, 6, 2, 3]))


try:
    from sortedcontainers import *
    from math import gcd, lcm
except ImportError:
    pass
from bisect import *
from collections import *
from functools import *
from heapq import *
import itertools
from string import whitespace, ascii_lowercase, ascii_uppercase, ascii_letters, digits, hexdigits, octdigits, punctuation, printable

from util import *

"""
echo "=== sample ===" ; python3 day23.py < sample.in
echo "=== real ===" ; python3 day23.py < day23.in
echo "=== sample ===" ; python3 day23.py < sample.in ; echo "=== real ===" ; python3 day23.py < day23.in
"""

A = sys.stdin.read().rstrip('\n').splitlines()
N = len(A)

grid = set()

look_order = [
    (0, -1),    # north
    (0, 1),     # south
    (-1, 0),    # west
    (1, 0),     # east
]

adj = [
    [(0, -1), (1, -1), (-1, -1)],
    [(0, 1), (1, 1), (-1, 1)],
    [(-1, 0), (-1, -1), (-1, 1)],
    [(1, 0), (1, -1), (1, 1)],
]

for i, line in enumerate(A):
    for j, ch in enumerate(line):
        if ch == '#':
            grid.add((j, i))    # (x, y)

start_idx = 0

ROUNDS = 10

def print_grid(grid):
    min_x = min(x for x, y in grid)
    max_x = max(x for x, y in grid)
    min_y = min(y for x, y in grid)
    max_y = max(y for x, y in grid)
    for y in range(min_y, max_y + 1):
        for x in range(min_x, max_x + 1):
            print('#' if (x, y) in grid else '.', end='')
        print()
    print()

ri = 0
while True:
    ri += 1
    ctr = Counter()
    proposal = []
    for x, y in grid:
        something_adj = False
        for nx, ny in n8(x, y):
            if (nx, ny) in grid:
                something_adj = True
                break
        if not something_adj:
            continue
        for i in range(4):
            idx = (start_idx + i) % 4
            something_adj = False
            for dx, dy in adj[idx]:
                nx, ny = x + dx, y + dy
                if (nx, ny) in grid:
                    something_adj = True
                    break
            if not something_adj:
                dx, dy = look_order[idx]
                proposal.append((x, y, x + dx, y + dy))
                ctr[(x + dx, y + dy)] += 1
                break
    moved = False
    for x, y, nx, ny in proposal:
        if ctr[(nx, ny)] == 1:
            moved = True
            grid.remove((x, y))
            grid.add((nx, ny))
    if not moved:
        print(ri)
        exit(0)
    start_idx = (start_idx + 1) % 4

min_x = min(x for x, y in grid)
max_x = max(x for x, y in grid)
min_y = min(y for x, y in grid)
max_y = max(y for x, y in grid)

res = 0
for y in range(min_y, max_y + 1):
    for x in range(min_x, max_x + 1):
        if (x, y) not in grid:
            res += 1
print(res)


from flask import Flask, render_template, request, session, redirect, url_for, flash
from flask_wtf import FlaskForm
from wtforms import (
    StringField,
    BooleanField,
    DateTimeField,
    RadioField,
    SelectField,
    TextField,
    TextAreaField,
    SubmitField,
)
from wtforms.validators import DataRequired

# create the app
app = Flask(__name__)
# Set the secret key
app.config["SECRET_KEY"] = "superkey"

# Default Route
@app.route("/")  # 127.0.0.1:5000
def index():
    return render_template("home.html")  # render the basic template

##########################
### Course management

# Custom parameters route
@app.route("/course/<input_name>")
def course_name(input_name):
    name = input_name
    return render_template("flask.html", name=name)

# List of courses
@app.route("/course/")
def course_list():
    user_logged_in = True
    course_list = ["flask", "pygame", "data science", "machine learning"]
    return render_template(
        "courses.html", log_ok=user_logged_in, courses_iter_list=course_list
    )

###############
# New Courses

# Sign up form to compile
@app.route("/course/new/")
def new_course():
    return render_template("course_new.html")

# Thank you page after form compiled
@app.route("/course/created")
def course_created():
    # grab first name variable from signup html page
    course_name = request.args.get("course-name")
    course_subject = request.args.get("course-subject")

    # save to database

    return render_template(
        "course_created.html", course_name=course_name, course_subject=course_subject
    )

###### ERROR HANDLER
## Page 404 error
@app.errorhandler(404)
def page_not_found(error):
    return render_template("404.html"), 404

################################################
#### ADVANCE FORMS WITH wtforms and flask_wtf
class InfoForm(FlaskForm):  # Inherit from FlaskForm
    name = StringField("What's the name of the course? ")
    submit = SubmitField("Submit")

# Route for the advance form
@app.route("/course/create/simple/", methods=["GET", "POST"])
def advance_form():
    name = False  # placeholder iniziale
    form = InfoForm()  # Oggetto InfoForm

    # Logic of the form
    if form.validate_on_submit():  # if you press: submit, this is what happend
        name = form.name.data  # get the data
        form.name.data = ""  # reset
    return render_template("course_advance.html", advance_form=form, course_name=name)

################################################
#### SUPER ADVANCE FORMS with validators, session, url for, ...
class CourseAdvanceForm(FlaskForm):
    """Definition of the form

    Args:
        FlaskForm (object): FlaskForm default main class
    """

    course_name = StringField(
        "What's the name of the course", validators=[DataRequired()]
    )
    course_active = BooleanField("The course is active?")
    difficulty = RadioField(
        "Please the difficulty of the course:",
        choices=[("easy", "Easy"), ("medium", "Medium"), ("hard", "Hard")],
    )
    platform = SelectField(
        u"Pick the platform you want to do the course:",
        choices=[("zoom", "Zoom"), ("skype", "Skype"), ("teams", "Teams")],
    )
    # the u before the string is the casting to unicode
    note = TextAreaField()
    submit = SubmitField("Submit")

@app.route("/course/create/advance", methods=["GET", "POST"])
def super_form():

    # Actions for the complicate form
    course_form = CourseAdvanceForm()

    # if the form is compiled
    if course_form.validate_on_submit():

        # save form information into session user cookie
        session["course_name"] = course_form.course_name.data
        session["course_active"] = course_form.course_active.data
        session["difficulty"] = course_form.difficulty.data
        session["platform"] = course_form.platform.data
        session["note"] = course_form.note.data

        # reset the form
        course_form.course_name.data = ""
        course_form.course_active.data = ""
        course_form.difficulty.data = ""
        course_form.platform.data = ""
        course_form.note.data = ""

        # go to the thankyou template page (thankyou function in python file)
        return redirect(url_for("course_result"))

    return render_template("course_advance_super.html", form=course_form)

@app.route("/course/create/advance/result")
def course_result():
    flash("Form compiled succesfully!")  # flash an alert message to the user
    return render_template("course_result.html")

## Main function (app run)
if __name__ == "__main__":
    app.run(debug=True)


#http://codeforces.com/contest/1091/problem/B

import sys
import math
from collections import defaultdict

try:
    sys.stdin = open('input.txt', 'r')
    sys.stdout = open('output.txt', 'w')

except:
    pass

n=int(input())
coords=[]
params=[]
for i in range(n):
	c=[int(x) for x in input().split()]
	coords.append(c)
for i in range(n):
	p=[int(x) for x in input().split()]
	params.append(p)

init=[coords[0][0],coords[0][1]]
answers=defaultdict(lambda:0)

for c in coords:
	c1,c2=c[0],c[1]
	for p in params:
		p1,p2=p[0],p[1]
		a=(c1+p1,c2+p2)
		answers[a]+=1

for a in answers:
	if(answers[a]==n):
		p=a

print(p[0],p[1])

B - New Year and the Treasure Geolocation.py


#!/usr/bin/python3
import sys, time, datetime
sys.path.extend(['..', '.'])
from collections import *
from runner import run, run_samples, get_commands
from utils import *
def get_day(): return 21
def get_year(): return 2021

def roll(dice, i):
    su = 0
    for _ in range(3):
        su += dice[i]
        i += 1
        i %= len(dice)
    return su, i

def parse(v):
    lines = get_lines(v)
    chunks = v.split('\n\n')
    pos1 = int(lines[0].split()[-1])
    pos2 = int(lines[1].split()[-1])
    return [pos1, pos2]

def p1(v):
    pos = parse(v)
    dice = [i for i in range(1, 101)]
    points = [0, 0]
    i = 0
    no = 0
    who = 0
    while max(points) < 1000:
        no += 3
        add, i = roll(dice, i)
        pos[who] = (pos[who] + add - 1)%10 + 1
        points[who] += pos[who]
        who ^= 1

    return no*min(points)

def p2(v):
    pos = parse(v)
    DP = {}
    rolls = [1, 2, 3]
    C = Counter()
    for a in rolls:
        for b in rolls:
            for c in rolls:
                C[a + b + c] += 1

    def solve(who, pos1, pos2, p1, p2):
        T = who, pos1, pos2, p1, p2
        if T in DP:
            return DP[T]
        if p1 >= 21:
            return (1, 0)
        if p2 >= 21:
            return (0, 1)
        V = [0, 0]
        for k, no in C.items():
            if who == 0:
                np = (pos1 + k - 1)%10 + 1
                w1, w2 = solve(1, np, pos2, p1 + np, p2)
            else:
                np = (pos2 + k - 1)%10 + 1
                w1, w2 = solve(0, pos1, np, p1, p2 + np)
            V[0] += w1*no
            V[1] += w2*no
        DP[T] = V
        return V
    V = solve(0, pos[0], pos[1], 0, 0)
    return max(V)

if __name__ == '__main__':
    """
    cmds = [
        'run1', 'run2',
        #'print_stats',
        #'submit1',
        #'submit2']
    """
    cmds = get_commands()
    print('Commands:', cmds)
    if 'run_samples' in cmds or 'samples_only' in cmds:
        run_samples(p1, p2, cmds, __file__)
    if 'samples_only' not in cmds:
        run(get_year(), get_day(), p1, p2, cmds, FILE=__file__)


import pytz

timezones = {1: {"zone": "Great Britain", "timezone": 'GB'},
             2: {"zone": "Greece", "timezone": 'Europe/Athens'},
             3: {"zone": "Spain", "timezone": 'Europe/Madrid'},
             4: {"zone": "France", "timezone": 'Europe/Paris'},
             5: {"zone": "Italy", "timezone": 'Europe/Rome'},
             6: {"zone": "Croatia", "timezone": 'Europe/Zagreb'},
             7: {"zone": "Sweden", "timezone": 'Europe/Stockholm'},
             8: {"zone": "Finland", "timezone": 'Europe/Helsinki'},
             9: {"zone": "Belgium", "timezone": 'Europe/Brussels'},
             }

for location in sorted(timezones):
    print("{}: {}".format(location, timezones[location]["zone"]))

print()
user = None

while user != 0:
    try:
        print("You can anytime press 0 to quit.")
        user = int(input("Please enter a timezone from 1-9: "))

        if user in timezones.keys():
            tzToDisplay = pytz.timezone(timezones[user]["timezone"])
            print("Here you go: ")
            print()
            print(f"Your local time is: {pytz.datetime.datetime.now().strftime('%A %x %X')}")
            print(f"Your UTC local time is: {pytz.datetime.datetime.utcnow().strftime('%A %x %X')}")
            print("Your chosen zone: {}\t The time: {}".format(timezones[user]["zone"],
                                                               pytz.datetime.datetime.now(tzToDisplay).strftime(
                                                                   '%A %x %X')))
            print("=" * 50)
            continue

        elif user == 0:
            print("Alright, you chose to quit")
            break

        else:
            print("Sorry, that is invalid.")
            print()
            continue

    except (ValueError, TypeError):
        print("Sorry, but you have to enter an integer from 1-9, or 0, not anything else!")
        print()
        continue


import pandas as pd
import glob
import os

file_list = glob.glob("*.json")
for i in range(0, len(file_list)):
    file_list[i] = './' + file_list[i]

def read_file(path, file_number, count_files):
    result = pd.DataFrame(
        columns=['Item', 'SalePriceBeforePromo', 'SalePriceTimePromo', 'DatePriceBeforePromo', 'ObjCode',
                 'DiscountType', 'DiscountValue', 'DateBegin', 'DateEnd', 'PWCcode', 'Value', 'FirstValue',
                 'LessOrEqual', 'File'])
    json_data = pd.read_json(path)
    File = os.path.basename(path)
    if 'Information' not in json_data.keys() or \
            not isinstance(json_data['Information'], pd.Series) or \
            'QtyGoodsLists' not in json_data['Information'].keys() or \
            'GoodsLists' not in json_data['Information'].keys() or \
            'GeneralInfo' not in json_data.keys() \
            or not isinstance(json_data["GeneralInfo"], pd.Series):
        return
    QtyGoodsLists = json_data["Information"]["QtyGoodsLists"] if isinstance(json_data["Information"]["QtyGoodsLists"],
                                                                            int) else 0
    GoodsLists = json_data["Information"]["GoodsLists"] if isinstance(json_data["Information"]["GoodsLists"],
                                                                      list) else []
    if len(GoodsLists) != QtyGoodsLists:
        print('Warning: Len GoodsLists not equal QtyGoodsLists in file ' + File)
        if len(GoodsLists) < QtyGoodsLists:
            QtyGoodsLists = len(GoodsLists)
    print("Found product lists: " + str(QtyGoodsLists) + " in file № " + str(file_number) + " / " + str(
        count_files))
    GeneralInfo = json_data["GeneralInfo"]
    DateBegin = GeneralInfo["DateBegin"] if 'DateBegin' in GeneralInfo.keys() else None
    DateEnd = GeneralInfo["DateEnd"] if 'DateEnd' in GeneralInfo.keys() else None
    PWCcode = GeneralInfo["PWCcode"] if 'PWCcode' in GeneralInfo.keys() else None
    for IndexGoodsList in range(0, QtyGoodsLists):
        print("\nChecking the list of goods № " + str(IndexGoodsList + 1) + " / " + str(
            QtyGoodsLists) + " in file № " + str(file_number) + " / " + str(count_files))
        GoodsListItem = GoodsLists[IndexGoodsList]
        GoodsListItemIsDict = isinstance(GoodsListItem, dict)
        QtyPrices = GoodsListItem["QtyPrices"] if GoodsListItemIsDict and 'QtyPrices' in GoodsListItem.keys() else 0
        Prices = GoodsListItem["Prices"] if GoodsListItemIsDict and 'Prices' in GoodsListItem.keys() and isinstance(
            GoodsListItem["Prices"], list) else []
        DiscountType = GoodsListItem[
            "DiscountType"] if GoodsListItemIsDict and 'DiscountType' in GoodsListItem.keys() else None
        DiscountValue = GoodsListItem[
            "DiscountValue"] if GoodsListItemIsDict and 'DiscountValue' in GoodsListItem.keys() else None
        PriceOptionsList = GoodsListItem[
            "PriceOptions"] if GoodsListItemIsDict and 'PriceOptions' in GoodsListItem.keys() else None
        PriceOptions = PriceOptionsList[0] if isinstance(PriceOptionsList, list) and len(PriceOptionsList) > 0 else {}
        PriceOptionsIsDict = isinstance(PriceOptions, dict)
        LessOrEqual = PriceOptions["Operator"] if PriceOptionsIsDict and 'Operator' in PriceOptions.keys() else None
        FirstValue = PriceOptions["FirstValue"] if PriceOptionsIsDict and 'FirstValue' in PriceOptions.keys() else None
        Value = PriceOptions["Value"] if PriceOptionsIsDict and 'Value' in PriceOptions.keys() else None
        if QtyPrices != len(Prices):
            print(
                'Warning: Len Prices not equal QtyPrices in GoodsLists[' + str(IndexGoodsList) + '] in file ' +
                File
            )
            if len(Prices) < QtyPrices:
                QtyPrices = len(Prices)
        for PriceIndex in range(0, QtyPrices):
            PriceItem = Prices[PriceIndex]
            PriceItemIsDict = isinstance(Prices[PriceIndex], dict)
            QtyGoods = PriceItem["QtyGoods"] if PriceItemIsDict and 'QtyGoods' in PriceItem.keys() else 0
            Columns = PriceItem["ColumnsName"] if PriceItemIsDict and 'ColumnsName' in PriceItem.keys() and \
                                                  isinstance(PriceItem["ColumnsName"], list) else []
            ItemColumnIndex = Columns.index("Item")
            SalePriceBeforePromoColumnIndex = Columns.index("SalePriceBeforePromo")
            SalePriceTimePromoColumnIndex = Columns.index("SalePriceTimePromo")
            DatePriceBeforePromoColumnIndex = Columns.index("DatePriceBeforePromo")
            ObjCode = PriceItem["StoreCode"] if 'StoreCode' in PriceItem.keys() else None
            Data = PriceItem["Data"] if 'Data' in PriceItem.keys() and isinstance(PriceItem["Data"], list) else []
            if QtyGoods != len(Data):
                print(
                    'Warning: Len Prices not equal QtyPrices in GoodsLists[' + str(IndexGoodsList) + '][Prices][' +
                    str(PriceIndex) + '] in file ' + File
                )
                if len(Data) < QtyGoods:
                    QtyGoods = len(Data)
            ManyGoods = False
            if QtyGoods > 1000:
                print("There are a lot of goods")
                ManyGoods = True
            for DataIndex in range(0, QtyGoods):
                if ManyGoods and DataIndex % 100 == 0:
                    print("Checking good № " + str(DataIndex + 1) + " / " + str(QtyGoods))
                DataItem = Data[DataIndex]
                Item = DataItem[ItemColumnIndex] if 0 <= ItemColumnIndex < len(DataItem) else None
                SalePriceBeforePromo = DataItem[
                    SalePriceBeforePromoColumnIndex] if 0 <= SalePriceBeforePromoColumnIndex < len(
                    DataItem) else None
                SalePriceTimePromo = DataItem[
                    SalePriceTimePromoColumnIndex] if 0 <= SalePriceTimePromoColumnIndex < len(DataItem) else None
                DatePriceBeforePromo = DataItem[
                    DatePriceBeforePromoColumnIndex] if 0 <= DatePriceBeforePromoColumnIndex < len(
                    DataItem) else None
                ResultRow = {'Item': Item,
                             'SalePriceBeforePromo': SalePriceBeforePromo,
                             'SalePriceTimePromo': SalePriceTimePromo,
                             'DatePriceBeforePromo': DatePriceBeforePromo,
                             'ObjCode': ObjCode,
                             'DiscountType': DiscountType,
                             'DiscountValue': DiscountValue,
                             'DateBegin': DateBegin,
                             'DateEnd': DateEnd,
                             'PWCcode': PWCcode,
                             'Value': Value,
                             'FirstValue': FirstValue,
                             'LessOrEqual': LessOrEqual,
                             'File': File}
                result = result.append(ResultRow, ignore_index=True)
    return result

datas = pd.DataFrame()
count_files = len(file_list)
# запись в файл
for file in file_list:
    file_number = file_list.index(file) + 1
    print("File parsing started " + file + " № " + str(file_number) + " / " + str(count_files))
    datas = datas.append(read_file(file, file_number, count_files))
    print("File parsing  № " + str(file_number) + " / " + str(count_files) + " finished! ")
datas.to_excel('./result.xlsx', index=False, na_rep='None')

# ObjCode = json_data["Information"]["GoodsLists"][0]["Prices"][0]["StoreCode"]
# DiscountType = json_data["Information"]["GoodsLists"][0]["DiscountType"]
# DiscountValue = json_data["Information"]["GoodsLists"][0]["DiscountValue"]
# Value = None
# FirstValue = None
# LessOrEqual = json_data["Information"]["GoodsLists"][0]["PriceOptions"][0]["Operator"]
# DateBegin = json_data["GeneralInfo"]["DateBegin"]
# DateEnd = json_data["GeneralInfo"]["DateEnd"]
# PWCcode = json_data["GeneralInfo"]["PWCcode"]
# File = path


from flask import Flask, request, redirect
from twilio.twiml.messaging_response import Message, MessagingResponse

app = Flask(__name__)

@app.route("/sms", methods=['GET', 'POST'])

def sms_reply():
    # Get message sent
    message_body = request.form['Body'].upper()

    if message_body.find('CONFIRM') >= 0:
        msg_reply = "Thanks for the confirmation!"

    elif message_body.find('RESCHEDULE') >= 0:
        msg_reply = "It appears you need to cancel or change your appointment, we will contact you."

    else:
        msg_reply = f"Unknown response: '{message_body}' - Please respond with CONFIRM or RESCHEDULE."

    # TwiML response
    resp = MessagingResponse()
    resp.message(msg_reply)

    return str(resp)

if __name__ == "__main__":
    app.run(debug=True)


from __future__ import absolute_import, division, print_function

import os
import glob

try:
    import pydicom
except ImportError:
    import dicom as pydicom
import numpy as np

from glue.logger import logger
from glue.core.data import Data
from glue.config import data_factory

__all__ = ['is_dicom', 'dicom_reader']

def is_dicom_file(filename):
    """
    This function is used to check whether a file is in the DICOM format. We
    do this by checking that bytes 129 to 132 are DICM. See

    http://stackoverflow.com/questions/4792727/validating-dicom-file

    for more details
    """
    try:
        with open(filename, 'rb') as f:
            f.read(128)
            if f.read(4) == b'DICM':
                return True
            else:
                return False
    except Exception:
        return False

def dicom_label(filename):
    """
    This function just returns the name of the file without the .dcm extension
    if present. We don't strip off any other extensions in case they are part
    of the name and not actually an extension.
    """
    label = os.path.basename(os.path.normpath(filename))
    if label.endswith('.dcm'):
        label = label[:-4]
    return label

def is_dicom(source):
    """
    Determine if the source is either a DICOM file or a directory that
    contains at least one DICOM file.
    """
    if os.path.isdir(source):
        for filename in glob.glob(os.path.join(source, '*')):
            if is_dicom_file(filename):
                return True
        return False
    else:
        return is_dicom_file(source)

@data_factory(
    label='DICOM file or directory',
    identifier=is_dicom,
    priority=100,
)
def dicom_reader(source):
    """
    Read a DICOM file or a directory with DICOM files
    """

    if os.path.isdir(source):

        # We are dealing with a directory which should contain DICOM files. At
        # this point, we need to check whether the directory contains zero,
        # one, or more DICOM datasets.

        arrays = {}
        for filename in glob.glob(os.path.join(source, '*')):
            if is_dicom_file(filename):
                logger.info("Reading DICOM data from {0}".format(filename))
                ds = pydicom.read_file(filename)
                arrays[dicom_label(filename)] = ds.pixel_array
            else:
                logger.info("Not a DICOM file: {0}".format(filename))

        # If there are no DICOM files, we raise an error, and if there is one
        # then we are done!

        if len(arrays) == 0:
            raise Exception("No DICOM files found in directory: {0}".format(source))
        elif len(arrays) == 1:
            label = list(arrays.keys())[0]
            return [Data(array=arrays[label], label=label)]

        # We now check whether all the shapes of the DICOM files are the same,
        # and if so, we merge them into a single file.

        labels = sorted(arrays)
        ref_shape = arrays[labels[0]].shape

        for label in labels[1:]:

            if arrays[label].shape != ref_shape:
                break

        else:

            # Since we are here, the shapes of all the DICOM files match, so
            # we can construct a higher-dimensional array.

            # Make sure arrays are sorted while constructing array
            array = np.array([arrays[label] for label in labels])

            # We flip the array here on that in most cases we expect that the
            # scan will start at the top of e.g. the body and move downwards.
            array = array[::-1]

            return [Data(array=array, label=dicom_label(source))]

        # If we are here, the shapes of the DICOM files didn't match, so we
        # simply return one Data object per DICOM file.
        return [Data(array=arrays[label], label=label) for label in labels]

    else:

        ds = pydicom.read_file(source)
        data = [Data(array=ds.pixel_array, label=dicom_label(source))]

    return data


from shapely import affinity
from shapely.geometry import LineString, Point, MultiPoint, GeometryCollection, MultiLineString
from shapely.ops import cascaded_union

from copter.sensors.Sensor import Sensor
from geometry.Circle import Circle
from geometry.PointVector import PointVector

__author__ = 'Martin'

class SensorMock(Sensor):
    def __init__(self, sensor_range, obstacles, other_copters):
        self.sensor_range = sensor_range
        self.sweep_resolution = 25
        self.obstacles = obstacles
        self.other_copters = other_copters

    def collect_obstacles_closest_points(self, copter_position):
        obstacle_entities = list(map(
            lambda obstacle: obstacle.geometric_entity.get_lib_conversion(),
            self.obstacles))
        return self.get_closest_points(copter_position, obstacle_entities)

    def collect_other_copters_closest_points(self, position):
        other_copters_circles = list(map(
            lambda copter: Circle(PointVector(copter.position.x, copter.position.y), copter.size).get_lib_conversion(),
            self.other_copters))
        return self.get_closest_points(position, other_copters_circles)

    # inspired by http://gis.stackexchange.com/q/81613
    def get_closest_points(self, copter_position, entities):
        position = copter_position.get_lib_conversion()
        all_entities = cascaded_union(entities)

        starting_sweep_line = LineString(
            [position, (position.x, position.y - self.sensor_range)])
        sweep_lines = [affinity.rotate(starting_sweep_line, i, position)
                       for i in range(0, 360, self.sweep_resolution)]
        radial_sweep = cascaded_union(sweep_lines)

        perimeter = []
        for radial_line in radial_sweep:
            intersection = radial_line.intersection(all_entities)

            if isinstance(intersection, Point):
                perimeter.append(intersection)
            elif isinstance(intersection.type, MultiPoint):
                intersection_dict = {}
                for intersection_point in intersection:
                    intersection_dict[position.distance(intersection_point)] = intersection_point
                # add closest intersection point
                perimeter.append(intersection_dict[min(intersection_dict.keys())])
            elif isinstance(intersection, LineString):
                closest_point = self.get_line_closest_point(position, intersection)
                perimeter.append(closest_point)
            elif isinstance(intersection, MultiLineString):
                for line in intersection:
                    closest_point = self.get_line_closest_point(position, line)
                    perimeter.append(closest_point)
            elif isinstance(intersection, GeometryCollection):
                pass
            else:
                raise ValueError("Unknown type {}".format(intersection.__class__.__name__))

        return list(map(lambda p: PointVector(p.x, p.y), perimeter))

    @staticmethod
    def get_line_closest_point(position, line):
        p1 = Point(line.coords[0][0], line.coords[0][1])
        p2 = Point(line.coords[1][0], line.coords[1][1])
        if position.distance(p1) < position.distance(p2):
            return p1
        else:
            return p2


import logging
import os
import socket

from contextlib import contextmanager
from threading import Lock

try:
    from odoo.tools import classproperty
except ImportError:
    from openerp.tools import classproperty

from werkzeug.local import Local

_logger = logging.getLogger(__name__)

try:
    import paramiko
except ImportError:
    _logger.warning('Cannot import paramiko.')

class SSHEnvironments(set):
    """ It provides a common object for all environments for a request """

class SSHEnvironment(object):
    """ It provides an environment for thread-safe management of SSH sessions

    Attributes:
        RETRY_EXCEPTIONS: (list) Exceptions that will cause an SSH
            reconnection attempt.
        RETRY_MAX: (int) Maximum amount of consecutive SSH connection
            reconnection attempts before failure.
        client: (paramiko.Client|None) Raw Paramiko SSH connection. None if
            not connected.
        host: (str) Node Hostname/IP that is used for connection.
        port: (int) Port number used for connection.
        username: (str) Username used for authentication.
        identify_file: (str) Path to the SSH identity file

    TODO:
        * Create a transport registry
        * Add locking at transport level
        * Allow host key policy configuration
    """

    RETRY_EXCEPTIONS = [
        paramiko.ssh_exception.SSHException,
        paramiko.ssh_exception.ChannelException,
        socket.error,
    ]
    RETRY_MAX = 5
    _local = Local()

    @classproperty
    def envs(cls):
        """ It returns the current, or creates new, Environments """
        try:
            return cls._local.environments
        except AttributeError:
            cls.reset()
            return cls._local.environments

    @classmethod
    def reset(cls):
        """ It creates a new set of Environments and scrubs the old """
        _logger.debug('Resetting SSHEnvironment')
        try:
            for env in cls._local.environments:
                env._cleanup()
        except AttributeError:
            pass
        cls._local.environments = SSHEnvironments()

    @contextmanager
    def get_channel(self, retry=True):
        """ It provides context manager yielding a new Paramiko channel """
        try:
            transport = self.client.get_transport()
            with transport.open_session() as channel:
                yield channel
        except tuple(self.RETRY_EXCEPTIONS + [AttributeError]):
            if retry:
                self._connect()
                with self.get_channel(False) as channel:
                    yield channel
            else:
                raise

    def _cleanup(self):
        """ It provides a handler to close existing resources """
        if self.client:
            try:
                self.client.close()
            except:  # pragma: no cover
                _logger.info('Client close failed for %s', self.client)
        self.client = None

    def _connect(self):
        """ It creates an SSH connection to remote node """
        self._cleanup()
        self.client = paramiko.SSHClient()
        self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        try:
            self.client.connect(
                self.host,
                port=self.port,
                username=self.username,
                key_filename=os.path.expanduser(self.identity_file),
            )
            self._retry_left = self.RETRY_MAX
        except tuple(self.RETRY_EXCEPTIONS):
            if self._retry_left:
                self._retry_left -= 1
                self._connect()
            else:
                self.client = None
                raise

    def __wrap_method(self, method):
        """ It injects a lock and a reconnect onto the method """
        locked = self.__inject_lock(method)
        return self.__inject_reconnect(locked)

    def __inject_lock(self, method):
        """ It injects a thread lock onto the method """

        def __call_method(*args, **kwargs):
            self._lock.acquire(True)
            try:
                return method(*args, **kwargs)
            finally:
                self._lock.release()

        return __call_method

    def __inject_reconnect(self, method):
        """ It injects a reconnector onto the method """

        def __call_method(*args, **kwargs):
            try:
                return method(*args, **kwargs)
            except tuple(self.RETRY_EXCEPTIONS):
                self._connect()
                return method(*args, **kwargs)

        return __call_method

    def __new__(cls, host, port=22, username=None, identity_file=None,
                connect=True, *args, **kwargs
                ):
        """ It returns a cached SSHEnvironment, or creates a new one.

        The bulk of this method is only called once when the SSHEnvironment
        is instantiated for the first time. Subsequent instantiations will
        return a cached SSHEnvironment & skip the initiatizations.

        Params:
            host: (str) Host or IP of remote node.
            post: (int) Remote SSH port.
            username: (str|None) Username for connection. None for shell
                default.
            identity_file: (str|None) Path to the SSH identity file
            connect: (bool) If a connection should be initiated automatically
        """

        eval_args = (host, port, username, identity_file)

        for env in cls.envs:
            if env._eval_args == eval_args:
                return env

        self = object.__new__(cls, *args, **kwargs)
        cls.envs.add(self)

        self.host = host
        self.port = port
        self.username = username
        self.identity_file = identity_file
        self.client = None
        self._retry_left = self.RETRY_MAX
        self._args = args
        self._kwargs = kwargs
        self._eval_args = eval_args
        self._lock = Lock()

        if connect:
            self._connect()

        return self

    def __init__(self, *args, **kwargs):
        """ It initializes a new SSHEnvironment.

        Note that ``__init__`` will be run the first time an Environment is
        used in a session, regardless of whether it was cached. This method
        currently does nothing, but could be useful for environment prep

        Params:
            host: (str) Host or IP of remote node.
            post: (int) Remote SSH port.
            username: (str|None) Username for connection. None for shell
                default.
            identity_file: (str|None) Path to the SSH identity file
            connect: (bool) If a connection should be initiated automatically
        """
        pass

    def __getattr__(self, key):
        """ Provide passthrough to paramiko Client while locking the conn """
        try:
            return super(SSHEnvironment, self).__getattr__(key)
        except AttributeError:
            pass
        method = getattr(self.client, key)
        if not callable(method):
            return method
        return self.__wrap_method(method)

    def __str__(self):
        """ Allow object rebuilds """
        return '%(class)s(*%(args)r, **%(kwargs)r)' % {
            'class': self.__class__.__name__,
            'args': self._eval_args + self._args,
            'kwargs': self._kwargs,
        }

    def __repr__(self):
        return self.__str__()


#
# @lc app=leetcode.cn id=7 lang=python3
#
# [7] 整数反转
#

# @lc code=start
class Solution:
    def reverse(self, x: int) -> int:
        ele_list = []
        if x>=0:
            t = x
        else:
            t = -x
        while t>0:
            yushu = t%10
            t = t//10
            ele_list.append(yushu)
        res = 0
        for ele in ele_list:
            res=res*10+ele
        if res>pow(2,31)-1 or res<-pow(2,31):
            return 0
        if x>=0:
            return res
        else:
            return -res

# @lc code=end


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pdb

def sigmoid(z):
    z = z.astype(np.float64)
    return 1 / (1 + np.exp(-z))

def costFunction(X, y, weights):
    m = np.size(X,0)
    return (1 / m) * sum(-y * np.log(sigmoid(X.dot(weights))) - (1 - y) * np.log(1 - sigmoid(X.dot(weights))))

def costFunctionDerivative(X, y, weights, alpha):
    #pdb.set_trace()
    m = np.size(X,0)
    activation = sigmoid(X.dot(weights)) - y #Activation function g(z)
    activation = activation.astype(np.float64) #Turn it into float64
    temp_weights = np.copy(weights)

    for j in range(0, np.size(weights)):
        temp_weights[j] = weights[j] - alpha * (1/m) * np.sum((sigmoid(X.dot(weights)) - y) * X[:, j:j+1])
    return temp_weights

def gradientDescent(X, y, weights, alpha, iterations):
    print("Initial cost function: {0}".format(costFunction(X, y, weights)))
    for i in range(0, iterations):
        weights = costFunctionDerivative(X, y, weights, alpha)
    print("Final cost function: {0}".format(costFunction(X,y,weights)))
    return weights

def plotData(data, xAxis, yAxis):
    approved = data[data['approved'].isin([1])] #Students that passed
    ded = data[data['approved'].isin([0])] #Slept with the fishes

    fig, ax = plt.subplots(figsize=(16,9))
    ax.scatter(approved[xAxis], approved[yAxis], s=30, c='b', marker='o', label='approved')
    ax.scatter(ded[xAxis],ded[yAxis], s=30, c='r', marker='x', label='Ded')
    ax.legend()
    ax.set_xlabel(xAxis)
    ax.set_ylabel(yAxis)
    plt.show()

def main():
    #Load data
    headers=['exam1','exam2','approved']
    data = pd.read_csv('ex2data1.txt', names=headers, skipinitialspace=True, quotechar='"')

    #x = passenger class, Fare
    X = pd.DataFrame(data[['exam1','exam2']])
    #X = (X - np.mean(X)) / np.std(X) #Normalizing data
    X.insert(loc=0, column='Bias', value=1) #Adding column of 1's for the bias unit!
    #y = slept with the fishes
    y = data['approved']

    #Initializing weights at 0!
    weights = np.zeros((np.size(X,1),1))

    iterations = 1500
    alpha = 0.001

    #plotData(data, 'exam1', 'exam2')

    #Calculate cost function
    X = np.array(X.values) #Turn X into an ndarray for ease of usage
    y = np.array(y.values) #Turn y into an ndarray for ease of usage
    y = np.reshape(y, [np.size(y),1]) #Reshape it into (size,1) nd array instead of (size,)
    optimized_weights = gradientDescent(X, y, weights, alpha, iterations)

    #Load test data
    test_data = pd.read_csv('ex2data1.txt', names=headers, skipinitialspace=True, quotechar='"')
    test_y = pd.DataFrame(test_data['approved'])
    #x = passenger class, Fare
    X_OPT = pd.DataFrame(test_data[['exam1','exam2']])
    X_OPT.insert(loc=0, column='Bias', value=1) #Adding column of 1's for the bias unit!
    predictions = sigmoid(X_OPT.dot(optimized_weights))
    test_y.insert(loc=0, column='Predictions', value=predictions)
    print(test_y)
    #pdb.set_trace()

    index = 0
    for i in test_y['Predictions']:
        count = 0
        if(i > 0.5 and test_y['approved'][index] == 1):
            count += 1
        elif(i <= 0.5 and test_y['approved'][index] == 0):
            count += 1
        index += 1
    print("Accuracy rate: {0}%".format((count * 100)/np.size(test_data,0)))

    test_features = np.array([[1, 45, 85]], np.float64)
    print("Probability for 45 and 85: {0}%".format(sigmoid(test_features.dot(optimized_weights))))

if __name__ == '__main__':
    main()


#p.176

import pandas as pd
fish = pd.read_csv('https://bit.ly/fish_csv_data')

## fish파일의 첫 5개의 행 출력
#print(fish.head())

## Species의 열의 고유값들 추출
#print(pd.unique(fish['Species']))

## Species열을 제외한 나머니 열은 입력 데이터로 변환 -> 데이터프레임에서 5개의 열을 리스트로 변환
fish_input = fish[['Weight', 'Length', 'Diagonal', 'Height', 'Width']].to_numpy()

## fish_input에 5개의 특성이 잘 저장되어 있는지 5개 열 추출
#print(fish_input[:5])

## fish_target 추출
fish_target = fish['Species'].to_numpy()

## 데이터 세트 2개 필요
from sklearn.model_selection import train_test_split
train_input, test_input, train_target, test_target = train_test_split(fish_input, fish_target, random_state=42)

## 데이터 세트에 대해 표준화 전처리
from sklearn.preprocessing import StandardScaler
ss = StandardScaler()
ss.fit(train_input)
train_scaled = ss.transform(train_input)
test_scaled = ss.transform(test_input)

## k-최근접 이웃 분류기의 확률 예측 + 점수 확인
from sklearn.neighbors import KNeighborsClassifier
kn = KNeighborsClassifier(n_neighbors=3)
kn.fit(train_scaled, train_target)
#print(kn.score(train_scaled, train_target))
#print(kn.score(test_scaled, test_target))

## k-최근접 이웃 분류기에서 정렬된 출력값
#print(kn.classes_) ['Bream' 'Parkki' 'Perch' 'Pike' 'Roach' 'Smelt' 'Whitefish']

## k-최근접 이웃 분류기에서 예측된 5개의 출력값
#print(kn.predict(test_scaled[:5]))

## k-최근접 이웃 분류기에서 예측된 5개의 출력값의 확률(round, decimals이용) -> 출력순서는 classes와 같다.
import numpy as np
proba = kn.predict_proba(test_scaled[:5])
#print(np.round(proba, decimals=4))

## proba의 네번째 샘플의 확률을 확인(distances, indexes 이용) -> 위의(k-최근접 이웃 분류기에서 예측된 5개의 출력값의 확률)과 같음
distances, indexes = kn.kneighbors(test_scaled[3:4])
#print(train_target[indexes])[['Roach' 'Perch' 'Perch']]

04-1.로지스틱 회귀를 위한 데이터 준비(logistic regression).py


import logging
import re
from datetime import datetime
from typing import Optional

import pandas as pd
from bs4 import BeautifulSoup, Tag

from scraping_netkeiba import url

class Horse:
    def __init__(self, horse_id: str, html: str):
        self.__horse_id: str = horse_id
        self.__soup: BeautifulSoup = BeautifulSoup(html, "html.parser")
        self.__horse_title: Tag = self.__soup.select_one("div.horse_title")
        self.__profile_table: Tag = self.__soup.select_one("table.db_prof_table")
        self.validate()

    def name(self) -> str:
        return self.__horse_title.select_one("h1").text

    def eng_name(self) -> str:
        return self.__horse_title.select_one("p.eng_name").text

    def gender(self) -> str:
        horse_title_sentence: str = self.__horse_title.select_one("p.txt_01").text
        return re.match(r"^.*([牡牝セ]).*$", horse_title_sentence).group(1)

    def birth_date(self) -> datetime.date:
        birth_date_tag: Tag = self.__profile_table.find(
            "th", string="生年月日"
        ).parent.find("td")
        return datetime.strptime(birth_date_tag.text, "%Y年%m月%d日").date()

    def trainer_id(self) -> str:
        trainer_tag: Tag = (
            self.__profile_table.find("th", string="調教師").parent.find("td").find("a")
        )
        return url.trainer_pattern().match(trainer_tag.get("href")).group(1)

    def owner_id(self) -> str:
        owner_tag: Tag = (
            self.__profile_table.find("th", string="馬主").parent.find("td").find("a")
        )
        return url.owner_pattern().match(owner_tag.get("href")).group(1)

    def breeder_id(self) -> str:
        breeder_tag: Tag = (
            self.__profile_table.find("th", string="生産者").parent.find("td").find("a")
        )
        return url.breeder_pattern().match(breeder_tag.get("href")).group(1)

    def as_dataframe(self) -> pd.DataFrame:
        try:
            return pd.DataFrame.from_dict(
                {
                    "horse_id": [self.__horse_id],
                    "name": [self.name()],
                    "eng_name": [self.eng_name()],
                    "gender": [self.gender()],
                    "birth_date": [self.birth_date()],
                    "trainer_id": [self.trainer_id()],
                    "owner_id": [self.owner_id()],
                    "breeder_id": [self.breeder_id()],
                }
            )
        except Exception as e:
            logging.warning(f"An error occurred while scraping Horse: {e}")

    def validate(self):
        top: Optional[Tag] = self.__soup.find(
            "a", href=url.horse_pattern().match, attrs={"class": "active"}
        )
        if top is None:
            raise Exception("Active horse url is not found")

        top_href: str = top.get("href")
        horse_id: str = url.horse_pattern().match(top_href).group(1)
        if self.__horse_id != horse_id:
            raise Exception(
                f'Invalid horse id: expected "{self.__horse_id}", got "{horse_id}"'
            )


"""
Create permission groups
Create permissions (read only) to models for a set of groups
"""
import logging

from django.contrib.contenttypes.models import ContentType
from django.core.management.base import BaseCommand
from django.contrib.auth.models import Group
from django.contrib.auth.models import Permission

grouppermdict= {
    'Validated User': [
         'auth.view_permission',
         'caserooms.add_caseroom',
         'caserooms.add_caseroomentry',
         'caserooms.change_caseroom',
         'caserooms.change_caseroomentry',
         'caserooms.delete_caseroom',
         'caserooms.delete_caseroomentry',
         'caserooms.view_caseroom',
         'caserooms.view_caseroomentry',
         'oauth2_provider.add_accesstoken',
         'oauth2_provider.delete_accesstoken',
         'oauth2_provider.view_accesstoken',
         'splineapp.add_spinesplinemodel',
         'splineapp.change_spinesplinemodel',
         'splineapp.delete_spinesplinemodel',
         'splineapp.view_spinesplinemodel',
         'users.add_consent',
         'users.add_contribution',
         'users.add_cost',
         'users.change_consent',
         'users.change_contribution',
         'users.change_cost',
         'users.change_user',
         'users.delete_consent',
         'users.delete_contribution',
         'users.delete_cost',
         'users.delete_user',
         'users.view_consent',
         'users.view_contribution',
         'users.view_cost',
         'users.view_user'
    ],
    'Non validated User': [
        'auth.view_permission',
        'oauth2_provider.add_accesstoken',
        'oauth2_provider.delete_accesstoken',
        'oauth2_provider.view_accesstoken',
        'users.add_consent',
        'users.change_consent',
        'users.change_user',
        'users.delete_consent',
        'users.delete_user',
        'users.view_consent',
        'users.view_user'
    ],
    'Staff': [
         'auth.add_permission',
         'auth.change_permission',
         'auth.delete_permission',
         'auth.view_permission',
         'caserooms.add_caseroom',
         'caserooms.add_caseroomentry',
         'caserooms.change_caseroom',
         'caserooms.change_caseroomentry',
         'caserooms.delete_caseroom',
         'caserooms.delete_caseroomentry',
         'caserooms.view_caseroom',
         'caserooms.view_caseroomentry',
         'contenttypes.add_contenttype',
         'contenttypes.change_contenttype',
         'contenttypes.delete_contenttype',
         'contenttypes.view_contenttype',
         'guardian.add_groupobjectpermission',
         'guardian.add_userobjectpermission',
         'guardian.change_groupobjectpermission',
         'guardian.change_userobjectpermission',
         'guardian.delete_groupobjectpermission',
         'guardian.delete_userobjectpermission',
         'guardian.view_groupobjectpermission',
         'guardian.view_userobjectpermission',
         'oauth2_provider.add_accesstoken',
         'oauth2_provider.change_accesstoken',
         'oauth2_provider.delete_accesstoken',
         'oauth2_provider.view_accesstoken',
         'splineapp.add_spinesplinemodel',
         'splineapp.change_spinesplinemodel',
         'splineapp.delete_spinesplinemodel',
         'splineapp.view_spinesplinemodel',
         'users.add_consent',
         'users.add_consentcontent',
         'users.add_contribution',
         'users.add_cost',
         'users.add_user',
         'users.add_userproof',
         'users.change_consent',
         'users.change_consentcontent',
         'users.change_contribution',
         'users.change_cost',
         'users.change_user',
         'users.change_userproof',
         'users.delete_consent',
         'users.delete_consentcontent',
         'users.delete_contribution',
         'users.delete_cost',
         'users.delete_user',
         'users.delete_userproof',
         'users.view_consent',
         'users.view_consentcontent',
         'users.view_contribution',
         'users.view_cost',
         'users.view_user',
         'users.view_userproof'
    ]

}

class Command(BaseCommand):
    help = 'Creates read only default permission groups for users'

    def handle(self, *args, **options):
        for k,v in grouppermdict.items():
            new_group, created = Group.objects.get_or_create(name=k)
            for permission in v:
                app_label=permission.split('.')[0]
                model_name=permission.rsplit('_')[-1]
                permstring=permission.split('.')[1]
                print('Getting Content Type for app {} and model {} with permstring {}'.format(app_label,model_name,permstring))
                content_type = ContentType.objects.get(app_label=app_label, model=model_name)
                print("Creating {}".format(permission))
                try:
                    model_add_perm = Permission.objects.get(codename=permstring, content_type=content_type)
                except Permission.DoesNotExist:
                    logging.warning("**** Permission not found with name '{}'.".format(permission))
                    continue

                new_group.permissions.add(model_add_perm)

        print("Created default group and permissions.")


import pytest

from .. import *

def test_scratch_slot():
    slot = ScratchSlot()
    assert slot == slot
    assert slot.__hash__() == slot.__hash__()
    assert slot != ScratchSlot()

    assert slot.store().__teal__()[0] == ScratchStackStore(slot).__teal__()[0]
    assert slot.store(Int(1)).__teal__()[0] == ScratchStore(slot, Int(1)).__teal__()[0]

    assert slot.load().type_of() == TealType.anytype
    assert slot.load(TealType.uint64).type_of() == TealType.uint64
    assert slot.load().__teal__()[0] == ScratchLoad(slot).__teal__()[0]

def test_scratch_load_default():
    slot = ScratchSlot()
    expr = ScratchLoad(slot)
    assert expr.type_of() == TealType.anytype

    expected = TealSimpleBlock([
        TealOp(Op.load, slot)
    ])

    actual, _ = expr.__teal__()

    assert actual == expected

def test_scratch_load_type():
    for type in (TealType.uint64, TealType.bytes, TealType.anytype):
        slot = ScratchSlot()
        expr = ScratchLoad(slot, type)
        assert expr.type_of() == type

        expected = TealSimpleBlock([
            TealOp(Op.load, slot)
        ])

        actual, _ = expr.__teal__()

        assert actual == expected

def test_scratch_store():
    for value in (Int(1), Bytes("test"), App.globalGet(Bytes("key")), If(Int(1), Int(2), Int(3))):
        slot = ScratchSlot()
        expr = ScratchStore(slot, value)
        assert expr.type_of() == TealType.none

        expected, valueEnd = value.__teal__()
        storeBlock = TealSimpleBlock([
            TealOp(Op.store, slot)
        ])
        valueEnd.setNextBlock(storeBlock)

        actual, _ = expr.__teal__()

        assert actual == expected

def test_scratch_stack_store():
    slot = ScratchSlot()
    expr = ScratchStackStore(slot)
    assert expr.type_of() == TealType.none

    expected = TealSimpleBlock([
        TealOp(Op.store, slot)
    ])

    actual, _ = expr.__teal__()

    assert actual == expected


import subprocess
import os
from subprocess import Popen
import threading
import sys
from pydub import AudioSegment
from pydub.playback import play

HOME_DIR = os.environ['HOME']
MP3_1_DIR = HOME_DIR + '/mp3/build1.mp3'
MP3_2_DIR = HOME_DIR + '/mp3/build2.mp3'
MP3_3_DIR = HOME_DIR + '/mp3/build3.mp3'

class Ridocker:

    def __init__(self):
        self.stop_event = threading.Event()  # 停止させるかのフラグ

        # スレッドの作成と開始
        self.thread_1 = threading.Thread(target=self.exec)
        self.thread_2 = threading.Thread(
            target=self.run_mp3, args=(MP3_1_DIR,))
        self.thread_3 = threading.Thread(target=self.retry)

    def run_mp3(self, path):
        sound = AudioSegment.from_file(path, 'mp3')
        play(sound)

    def exec(self):
        cmd = "docker-compose build"
        popen = Popen(cmd, shell=True)
        popen.wait()
        self.stop()

    def retry(self):
        sound = AudioSegment.from_file(MP3_2_DIR, 'mp3')
        while not self.stop_event.is_set():
            play(sound)

    def stop(self):
        self.stop_event.set()
        self.thread_3.join()  # スレッドが停止するのを待つ
        self.run_mp3(MP3_3_DIR)

if __name__ == '__main__':
    h = Ridocker()

    h.thread_2.start()
    h.thread_2.join()
    h.thread_3.start()
    h.thread_1.start()


skates_list = []
people_list = []
coincidence = 0
skates_count = int(input('Кол-во коньков: '))
for i_skates in range(skates_count):
    skates = int(input(f'Размер {i_skates+1} пары:'))
    skates_list.append(skates)

people_count = int(input('Кол-во людей: '))
for i_people in range(people_count):
    people = int(input(f'Размер ноги {i_people+1} человека:'))
    people_list.append(people)
print(skates_list, people_list)

for _ in range(skates_count):
    min_skates = min(skates_list)
    min_peoples = min(people_list)
    if min_skates >= min_peoples:
        coincidence += 1
        skates_list.remove(min_skates)
        people_list.remove(min_peoples)
    else:
        skates_list.remove(min_skates)

print('Наибольшее кол-во людей, которые могут взять ролики:', coincidence)


from ..core import Object
from PyQt5.QtCore import QObject
from PyQt5.QtWidgets import QWidget
from janus.widgets.ui.scan_ui import Ui_Scan
from ..actions.gridscanaction import GridScanAction

class ScanControls(QObject, Object):

    def __init__(self, parent=None, grid_controller=None, grid_axis_controller=None, raw_tango_devices=None):
        Object.__init__(self)
        QObject.__init__(self)
        self.parent = parent
        self.grid_controller = grid_controller
        self.grid_axis_controller = grid_axis_controller
        self.raw_tango_devices = raw_tango_devices
        self.setup_ui()
        self.connect_signals()

    def start_scan(self):
        if "grid_scan" not in self.janus.actions or self.janus.actions["grid_scan"] is None:
            action = GridScanAction(self.grid_controller, self.grid_axis_controller, self.raw_tango_devices)
            action.progress_signal.connect(self.action_progress)
            action.done_signal.connect(self.action_done)
            self.janus.actions["grid_scan"] = action
            action.start()

    def action_progress(self, progress_data):
        print("progress: {}".format(progress_data["progress"]))

    def action_done(self):
        self.janus.actions["grid_scan"] = None

    def connect_signals(self):
        self.ui.startScan.clicked.connect(self.start_scan)

    def setup_ui(self):
        self.widget = QWidget(self.parent)
        self.ui = Ui_Scan()
        self.ui.setupUi(self.widget)


"""
Scatter plot
============

Produce a scatter plot of the pressure in cell (i,j) vs. r(i,j) = distance
from origin.

"""

#--------------------------
def setplot(plotdata):
#--------------------------

    """
    Specify what is to be plotted at each frame.
    Input:  plotdata, an instance of pyclaw.plotters.data.ClawPlotData.
    Output: a modified version of plotdata.

    """

    from pyclaw.plotters import colormaps

    plotdata.clearfigures()  # clear any old figures,axes,items data

    plotfigure = plotdata.new_plotfigure(name='scatter', figno=1)

    # Set up for axes in this figure:
    plotaxes = plotfigure.new_plotaxes()
    plotaxes.xlimits = 'auto'
    plotaxes.ylimits = 'auto'
    plotaxes.title = 'Scatter plot'

    # Set up for item on these axes: scatter of 2d data
    plotitem = plotaxes.new_plotitem(plot_type='1d_from_2d_data')

    def p_vs_r(current_data):
        # Return radius of each grid cell and p value in the cell
        from pylab import sqrt
        x = current_data.x
        y = current_data.y
        r = sqrt(x**2 + y**2)
        q = current_data.q
        p = q[:,:,0]
        return r,p

    plotitem.map_2d_to_1d = p_vs_r
    plotitem.plot_var = 0
    plotitem.plotstyle = 'o'
    plotitem.color = 'b'
    plotitem.show = True       # show on plot?

    # Parameters used only when creating html and/or latex hardcopy
    # e.g., via pyclaw.plotters.frametools.printframes:

    plotdata.printfigs = True                # print figures
    plotdata.print_format = 'png'            # file format
    plotdata.print_framenos = 'all'          # list of frames to print
    plotdata.print_fignos = 'all'            # list of figures to print
    plotdata.html = True                     # create html files of plots?
    plotdata.html_homelink = '../README.html'   # pointer for top of index
    plotdata.latex = True                    # create latex file of plots?
    plotdata.latex_figsperline = 1           # layout of plots
    plotdata.latex_framesperline = 2         # layout of plots
    plotdata.latex_makepdf = True            # also run pdflatex?

    return plotdata


"""Evaluation metrics to measure log probabilities for language modeling."""

from __future__ import annotations

from dataclasses import dataclass

import numpy as np

from explainaboard.metrics.metric import (
    Metric,
    MetricConfig,
    MetricStats,
    SimpleMetricStats,
)
from explainaboard.serialization import common_registry
from explainaboard.utils.typing_utils import narrow

@dataclass
@common_registry.register("LogProbConfig")
class LogProbConfig(MetricConfig):
    """Configuration for LogProb metrics.

    Args:
        ppl: Whether to exponente the log prob into perplexity.
    """

    ppl: bool = False

    def to_metric(self) -> Metric:
        """See MetricConfig.to_metric."""
        return LogProb(self)

class LogProb(Metric):
    """Calculate the log probability or perplexity."""

    def is_simple_average(self, stats: MetricStats):
        """See Metric.is_simple_average."""
        return stats.num_statistics() == 1

    def calc_stats_from_data(self, true_data: list, pred_data: list) -> MetricStats:
        """See Metric.calc_stats_from_data.

        Takes in a list of floats (token-level), or list of lists of floats (sentence
        level) and either one float for each or float+length rows
        """
        if len(pred_data) == 0 or isinstance(pred_data[0], float):
            return SimpleMetricStats(np.array(pred_data))
        elif isinstance(pred_data[0], list):
            return SimpleMetricStats(np.array([[sum(x), len(x)] for x in pred_data]))
        else:
            t = type(pred_data[0])
            raise ValueError(f"Invalid type of pred_data for calc_stats_from_data {t}")

    def _calc_metric_from_aggregate(self, agg_stats: np.ndarray) -> np.ndarray:
        """See Metric.calc_metric_from_aggregate."""
        is_batched = agg_stats.ndim != 1
        if not is_batched:
            agg_stats = agg_stats.reshape((1, agg_stats.shape[0]))
        config = narrow(LogProbConfig, self.config)
        val = (
            agg_stats[:, 0]
            if agg_stats.size == 1
            else agg_stats[:, 0] / agg_stats[:, 1]
        )
        if config.ppl:
            val = np.exp(-val)
        if not is_batched:
            val = val[0]
        return val


import pygame
import sys
import obd

# Initialize Pygame
pygame.init()

# Screen dimensions
WIDTH, HEIGHT = 800, 600

# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)

# Create the screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Race Car Dashboard")

# Font setup
font = pygame.font.Font(None, 40)

# OBD-II connection setup
connection = obd.OBD()  # This initializes the OBD-II connection using default settings

# Function to read OBD-II data
def get_obd_data():
    try:
        rpm_response = connection.query(obd.commands.RPM)
        coolant_temp_response = connection.query(obd.commands.COOLANT_TEMP)
        dtc_response = connection.query(obd.commands.GET_DTC)

        rpm = rpm_response.value.magnitude if rpm_response.value else 0
        coolant_temp = coolant_temp_response.value.magnitude if coolant_temp_response.value else 0

        if dtc_response and not dtc_response.is_null():
            dtc_codes = [dtc.code for dtc in dtc_response]
        else:
            dtc_codes = []

        return rpm, coolant_temp, dtc_codes
    except Exception as e:
        print("Error reading OBD-II data:", e)
        return 0, 0, []

# Function to clear DTC
def clear_dtc():
    try:
        connection.query(obd.commands.CLEAR_DTC)
    except Exception as e:
        print("Error clearing DTC:", e)

def draw_text(text, x, y, color=WHITE):
    text_surface = font.render(text, True, color)
    screen.blit(text_surface, (x, y))

def main():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_x, mouse_y = pygame.mouse.get_pos()
                read_dtc_button = pygame.Rect(WIDTH - 200, 50, 150, 50)
                clear_dtc_button = pygame.Rect(WIDTH - 200, HEIGHT - 100, 150, 50)

                if read_dtc_button.collidepoint(mouse_x, mouse_y):
                    # Code to read DTC here
                    rpm, coolant_temp, dtc_codes = get_obd_data()
                    print("Read DTC:", dtc_codes)
                elif clear_dtc_button.collidepoint(mouse_x, mouse_y):
                    # Code to clear DTC here
                    clear_dtc()
                    print("DTCs cleared")

        # Clear the screen
        screen.fill(BLACK)

        # Draw race car elements
        pygame.draw.rect(screen, RED, (10, 10, WIDTH - 20, HEIGHT - 20), 5)
        pygame.draw.rect(screen, RED, (30, 30, WIDTH - 60, HEIGHT - 60), 5)
        pygame.draw.line(screen, YELLOW, (WIDTH // 2, 30), (WIDTH // 2, HEIGHT - 30), 5)

        # Display car data
        rpm, coolant_temp, dtc_codes = get_obd_data()  # Get OBD-II data

        draw_text(f"RPM: {rpm}", 50, 50)
        draw_text(f"Coolant Temp: {coolant_temp}°C", 50, HEIGHT - 100)

        dtc_x, dtc_y = WIDTH // 2 + 50, 50
        for code in dtc_codes:
            draw_text(f"DTC: {code}", dtc_x, dtc_y)
            dtc_y += 40

        # Draw buttons
        pygame.draw.rect(screen, YELLOW, (WIDTH - 200, 50, 150, 50))
        pygame.draw.rect(screen, YELLOW, (WIDTH - 200, HEIGHT - 100, 150, 50))

        draw_text("Read DTC", WIDTH - 190, 60, BLACK)
        draw_text("Clear DTC", WIDTH - 190, HEIGHT - 90, BLACK)

        # Update the display
        pygame.display.flip()

if __name__ == "__main__":
    main()


from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserCreationForm
from .models import SiteUser

class SiteUserAdmin(UserAdmin):
  model = SiteUser

  def get_fieldsets(self, request, obj=None):
    if obj:
      current_fieldsets = []
      for item in UserAdmin.fieldsets:
        field_options = dict(item[1])

        if item[0] == None:
          fields = list(item[1]["fields"])
          fields.append("profile_image")
          field_options.update({"fields": tuple(fields)})

        current_fieldsets.append((item[0], field_options,))
    else:
      current_fieldsets = self.add_fieldsets + (
        ("Other", {"fields": ["profile_image"]}),
      )

    return current_fieldsets

admin.site.register(SiteUser, SiteUserAdmin)