# run the script to put xml file into a bunch of csv files
# %run -i ‘apple-health-data-parser’ ‘export.xml’

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime
from collections import OrderedDict
import subprocess
import os
import streamlit as st

#subprocess.call(["python", "D:\\Git\\qs_ledger\\apple_health\\apple-health-data-parser.py", "D:\\Personal\\Programming\\Health\\export\\apple_health_export\\export.xml"])
# this only needs to be done if there is new data

# list of csv files from apple health
#next(os.walk("D:\\Personal\\Programming\\Health\\export\\apple_health_export\\"))[2]

strong_df = pd.read_csv("D:\\Personal\\Programming\\Health\\export\\strong.csv")
mass_df = pd.read_csv("D:\\Personal\\Programming\\Health\\export\\apple_health_export\\BodyMass.csv")

# save some columns from mass_df into their own variables
mass_df = mass_df[mass_df.sourceName == "Health"]
mass_start_datetime_string = mass_df.startDate
mass_start_datetime = [datetime.strptime(string, "%Y-%m-%d %H:%M:%S -0600") for string in mass_start_datetime_string]
mass_value = mass_df[mass_df.sourceName == "Health"].value
mass_df["datetime"] = mass_start_datetime
st.write(mass_df)

st.subheader("strong_df")
strong_df["datetime"] = [datetime.strptime(string, "%Y-%m-%d %H:%M:%S") for string in strong_df["Date"]]
strong_df["lbs*reps"] = strong_df["Weight"] * strong_df["Reps"]
strong_df["bw"] = np.nan
st.write(strong_df)

################################################################ THE WALL ############

#st.header("Workouts from Strong")

# add most recent bodyweight to every line in strong_df

# return the most recent datetime in pandas timeseries relative to date
def most_recent(timeseries, input_dt):
    most_recent_dt = np.nan # for input_dt earlier than all dt
    for dt in timeseries:
        #st.write(dt)
        if input_dt > dt:
            #st.write("input time is after than this date")
            #st.write("input: " + str(input_dt) + "<" + str(dt))
            most_recent_dt = dt
        else:
            #st.write("input time is on or before this date")
            #most_recent_dt = dt
            pass

    return most_recent_dt

# generalize this, eg def fill_with_most_recent(from_df, into_df)
for index, row in strong_df.iterrows():
    bw_datetime = most_recent(mass_df["datetime"], row["datetime"])
    try:
        mass_value = mass_df[mass_df["datetime"] == bw_datetime]["value"][0]
    except:
        mass_value = np.nan

    # st.write("the value I want to put in:")
    # st.write(mass_value)
    # st.write(type(mass_value))

    # st.write("index")
    # st.write(index)
    # st.write("column location")
    # st.write(strong_df.columns.get_loc("bw"))

    # slot = strong_df.iloc[index, strong_df.columns.get_loc("bw")]
    # st.write("the value that is currently there:")
    # st.write(slot)

    strong_df.iloc[index, strong_df.columns.get_loc("bw")] = mass_value

    # st.write("the value in there now:")
    # st.write(strong_df.iloc[index, strong_df.columns.get_loc("bw")])

    # st.write("###############################################")

# calculate true weights based on value for "treat_weight"

# strong_df["treat_weight"] = ""
# strong_df.loc[strong_df["Exercise Name"] == "AnCap Repeaters (Assisted)", "treat_weight"] = "sub_from_bw"
# strong_df["true_weight"] = ""
# strong_df.loc[strong_df["treat_weight"] == "sub_from_bw", "true_weight"] = strong_df.loc[strong_df["treat_weight"] == "sub_from_bw", "bw"] - strong_df.loc[strong_df["treat_weight"] == "sub_from_bw", "weight"]

# st.write(strong_df)
#
# st.subheader("Specific Exercise")
# exercise = st.text_input("Exercise")
# st.write(strong_df[strong_df["Exercise Name"] == exercise])
#
# st.subheader("Performance over time, grouped by day")
# metric = st.text_input("Metric")
# if metric == "":
#     metric = "lbs*reps"
# values = strong_df[strong_df["Exercise Name"] == exercise][metric]
# times = strong_df[strong_df["Exercise Name"] == exercise]["datetime"]
#
# spec_perf_df = pd.concat([values, times], axis=1).reset_index().drop("index", axis="columns")
# st.write(spec_perf_df)
#
# unique_times = times.unique()
# for unique_time in unique_times:
#     st.write(spec_perf_df[spec_perf_df["datetime"] == unique_time]["lbs*reps"].sum())

# # plot a vertical line every time I work out, color coded by workout name
#
# plt.figure(figsize=(15,5))
#
# plt.xlim((737293.8399186282, 737324.695081372))
# plt.plot(wake_hrv_datetime_list, wake_hrv_value_list)
# plt.scatter(wake_hrv_datetime_list, wake_hrv_value_list)
#
# for name in strong_df["Workout Name"].unique():
#     name_df = strong_df[strong_df["Workout Name"] == name]
#     color = np.random.rand(3,)
#     for datetime in name_df["datetime"]:
#         plt.axvline(x=datetime, c=color, label=name)
#
#
# handles, labels = plt.gca().get_legend_handles_labels()
# by_label = OrderedDict(zip(labels, handles))
# plt.legend(by_label.values(), by_label.keys())
#
#
# # In[ ]:
#
#
# # color code blocks of time for training blocks
#
# # loop until user says no
# date_input_string = input("datetime YYYY-MM-DD") + "00:00:00"
# block_start_datetime = datetime.strptime(date_input_string, "%Y-%m-%d %H:%M:%S")
# block_type = input("block type")
# block_stop_datetime_string = input("ending datetime YYYY-MM-DD")
# block_stop_datetime = datetime.strptime(block_stop_datetime_string, "%Y-%m-%d %H:%M:%S")
# # print(again? Y/N)
#
#
# # for datetime in start list, append datetime to datetime list
# # append "start" to event list
# # append block type to block type list
#
# #for datetime in stop list, append datetime to datetimelist
# # append "stop" to event list
# # append block type to block type list
#
# # build block_df from lists above
#
# # concat block df and hrv_wake_df
#
# # plot axhspan from block start to block stop, color coded by block type
# # add this plot to hrv/workout plot
#
#
# # In[ ]:


import datetime
import logging
import os
import traceback
import sys

import boto3
import botocore
import psycopg2
import pandas

from . import get_client
from .s3 import put_df

logger = logging.getLogger()

class RedshiftClient(object):

    def __init__(
                self,
                pg_connector,
                schema: str,
                s3_client: boto3.resources.base.ServiceResource = None,
                profile_name: str = 'default',
                **kwargs
                ):

        self.schema = schema
        self.profile_name = profile_name
        self.connector = pg_connector

        if s3_client is not None:
            if isinstance(s3_client, boto3.resources.base.ServiceResource):
                self.s3_client = s3_client
            else:
                raise TypeError("expected s3_client of type botocore.client.S3")
        else:
            self.s3_client = get_client('s3', profile_name=self.profile_name)

        logger.info("Connected to Redshift")

        self.cursor = self.connector.cursor()
        self._reserved_words = ['AES128', 'AES256', 'ALL', 'ALLOWOVERWRITE',
                                'ANALYSE', 'ANALYZE', 'AND', 'ANY', 'ARRAY',
                                'AS', 'ASC', 'AUTHORIZATION', 'BACKUP', 'BETWEEN',
                                'BINARY', 'BLANKSASNULL', 'BOTH', 'BYTEDICT', 'BZIP2',
                                'CASE', 'CAST', 'CHECK', 'COLLATE', 'COLUMN', 'CONSTRAINT',
                                'CREATE', 'CREDENTIALS', 'CROSS', 'CURRENT_DATE',
                                'CURRENT_TIME', 'CURRENT_TIMESTAMP', 'CURRENT_USER',
                                'CURRENT_USER_ID', 'DEFAULT', 'DEFERRABLE', 'DEFLATE',
                                'DEFRAG', 'DELTA', 'DELTA32K', 'DESC', 'DISABLE',
                                'DISTINCT', 'DO', 'ELSE', 'EMPTYASNULL', 'ENABLE',
                                'ENCODE', 'ENCRYPT', 'ENCRYPTION', 'END', 'EXCEPT',
                                'EXPLICIT', 'FALSE', 'FOR', 'FOREIGN', 'FREEZE', 'FROM',
                                'FULL', 'GLOBALDICT256', 'GLOBALDICT64K', 'GRANT', 'GROUP',
                                'GZIP', 'HAVING', 'IDENTITY', 'IGNORE', 'ILIKE', 'IN',
                                'INITIALLY', 'INNER', 'INTERSECT', 'INTO', 'IS', 'ISNULL',
                                'JOIN', 'LEADING', 'LEFT', 'LIKE', 'LIMIT', 'LOCALTIME',
                                'LOCALTIMESTAMP', 'LUN', 'LUNS', 'LZO', 'LZOP', 'MINUS',
                                'MOSTLY13', 'MOSTLY32', 'MOSTLY8', 'NATURAL', 'NEW', 'NOT',
                                'NOTNULL', 'NULL', 'NULLS', 'OFF', 'OFFLINE', 'OFFSET',
                                'OID', 'OLD', 'ON', 'ONLY', 'OPEN', 'OR', 'ORDER', 'OUTER',
                                'OVERLAPS', 'PARALLEL', 'PARTITION', 'PERCENT', 'PERMISSIONS',
                                'PLACING', 'PRIMARY', 'RAW', 'READRATIO', 'RECOVER',
                                'REFERENCES', 'RESPECT', 'REJECTLOG', 'RESORT', 'RESTORE',
                                'RIGHT', 'SELECT', 'SESSION_USER', 'SIMILAR', 'SNAPSHOT ',
                                'SOME', 'SYSDATE', 'SYSTEM', 'TABLE', 'TAG', 'TDES',
                                'TEXT255', 'TEXT32K', 'THEN', 'TIMESTAMP', 'TO', 'TOP',
                                'TRAILING', 'TRUE', 'TRUNCATECOLUMNS', 'UNION', 'UNIQUE',
                                'USER', 'USING', 'VERBOSE', 'WALLET', 'WHEN', 'WHERE',
                                'WITH', 'WITHOUT']

    def __enter__(self, **kwargs):
        return self

    def __exit__(self, *args):
        self.cursor.close()

    def add_reserved_words(self, words: list) -> None:
        """Adds a reserved word to the connector attribute for later use"""

        if isinstance(words, list):
                self._reserved_words = self._reserved_words + \
                [str(w) for w in set(words) if w not in self._reserved_words]
        else:
            raise TypeError(f'Invalid type passed to add_reserved_words(): {type(words)}, expected str of list ')

    def _validate_column_names(self, df: pandas.DataFrame) -> pandas.DataFrame:
        """Validate the column names to ensure no reserved words are used."""

        reserved_words = [r.strip().lower() for r in self._reserved_words]

        df.columns = [x.lower().replace(' ', '_') for x in df.columns]

        for col in df.columns:
            try:
                assert col not in reserved_words
            except AssertionError:
                raise ValueError(f'DataFrame column name {col} is a reserved word')
        return df

    @staticmethod
    def _to_redshift_types(dtype_: str) -> str:
        """Retrieves corresponding valid Redshift type given a type name"""
        if dtype_.startswith('int'):
            return 'INTEGER'
        elif dtype_.startswith('float'):
            return 'REAL'
        elif dtype_.startswith('datetime'):
            return 'TIMESTAMP'
        elif dtype_ == 'bool':
            return 'BOOLEAN'
        else:
            return 'VARCHAR(256)'

    def _get_column_data_types(self, df: pandas.DataFrame, index: bool = False) -> list:
        """Retrieves redshift compatible data types from a DataFrame"""
        column_data_types = [self._to_redshift_types(dtype_.name) for dtype_ in df.dtypes.values]
        if index:
            column_data_types.insert(
                0, self._to_redshift_types(df.index.dtype.name))
        return column_data_types

    def _s3_to_redshift(self,
                        redshift_table_name: str,
                        column_list: list,
                        s3_bucket_name: str,
                        s3_key: str,
                        delimiter: str = ',',
                        quotechar: str = '"',
                        dateformat: str = 'auto',
                        timeformat: str = 'auto',
                        region: str = '',
                        parameters: str = '',
                        aws_role: str = None,
                        aws_token: str = ''):
        """Executes a COPY command from Redshift to load data from S3"""

        # get authentication information
        s3_file_path = f's3://{s3_bucket_name}/{s3_key}'
        access_key_id = os.environ.get('AWS_ACCESS_KEY_ID')
        secret_access_key = os.environ.get('AWS_SECRET_ACCESS_KEY')

        # create query parts
        if aws_role:
            authorization = f"""
            IAM_ROLE '{aws_role}'
            """
        elif access_key_id and secret_access_key:
            authorization = f"""
            access_key_id '{access_key_id}'
            secret_access_key '{secret_access_key}'
            """
        else:
            logger.warning("no AWS authentification set")
            authorization = ""

        s3_to_sql = f"""
        COPY {redshift_table_name}({' ,'.join(column_list)})
        FROM '{s3_file_path}'
        DELIMETER '{delimiter}'
        ignoreheader 1
        GZIP csv quote as '{quotechar}'
        dateformat '{dateformat}'
        timeformat '{timeformat}'
        {authorization}
        {parameters}
        """

        if region:
            s3_to_sql = s3_to_sql + f"region '{region}'"

        if aws_token:
            s3_to_sql = s3_to_sql + f"\n\tsession_token '{aws_token}'"
        s3_to_sql = s3_to_sql + ';'

        # log the whole resulting request
        logger.debug(s3_to_sql)

        # send the file
        logger.info('FILLING THE TABLE IN REDSHIFT')
        try:
            self.cursor.execute(s3_to_sql)
            self.connector.commit()
        except Exception as e:
            logger.error(e)
            traceback.print_exc(file=sys.stdout)
            self.connector.rollback()
            raise

    def _create_redshift_table(self,
                               df: pandas.DataFrame,
                               redshift_table_name: str,
                               column_data_types: list = None,
                               column_constraints: list = None,
                               index: bool = False,
                               diststyle: str = 'even',
                               distkey: str = '',
                               sort_interleaved: bool = False,
                               sortkey: str = '',
                               include_date_insert: bool = True,
                               debug=False):
        """Create a Redshift table based on a schema build from a DataFrame object"""

        columns = list(df.columns)

        if index:
            if df.index.name:
                columns.insert(0, df.index.name)
            else:
                columns.insert(0, "index")

        if column_data_types is None:
            column_data_types = self._get_column_data_types(df, index)

        columns_and_data_type = ', '.join(
            [f'{x} {y}' for x, y in zip(columns, column_data_types)])
        if include_date_insert:
            columns_and_data_type += ', date_insert DATETIME DEFAULT GETDATE()'

        create_table_query = f'CREATE TABLE IF NOT EXISTS {redshift_table_name} \
                                ({columns_and_data_type}'

        if column_constraints is not None:
            create_table_query += " ," + ", ".join(column_constraints) + ")"
        else:
            create_table_query += ")"

        if not distkey:
            # Without a distkey, we can set a diststyle
            if diststyle not in ['even', 'all', 'auto']:
                raise ValueError("diststyle must be either 'even', 'all' or 'auto'")
            else:
                create_table_query += f' DISTSTYLE {diststyle}'
        else:
            create_table_query += ' DISTSTYLE key'
            create_table_query += f' DISTKEY({distkey})'
        if len(sortkey) > 0:
            if sort_interleaved:
                create_table_query += ' INTERLEAVED'
            create_table_query += f' SORTKEY({sortkey})'
        if debug:
            logger.debug('CREATING A TABLE IN REDSHIFT')
            logger.debug(create_table_query)

        self.cursor.execute(create_table_query)
        self.connector.commit()

    def _pandas_to_redshift(self,
                            df: pandas.DataFrame,
                            redshift_table_name: str,
                            s3_bucket_name: str,
                            s3_key_prefix: str,
                            column_data_types: list = None,
                            column_constraints: list = None,
                            index: bool = False,
                            diststyle: str = 'even',
                            distkey: str = '',
                            sort_interleaved: bool = False,
                            sortkey: str = '',
                            delimiter: str = ',',
                            quotechar: str = '"',
                            dateformat: str = 'auto',
                            timeformat: str = 'auto',
                            region: str = '',
                            parameters: str = '',
                            aws_role: str = '',
                            aws_token: str = '',
                            drop_table: bool = False,
                            debug: bool = False
                            ):
        """Private method to create and load data from a DataFrame to a Redshift table"""

        df = self._validate_column_names(df)

        d = datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
        s3_key = f"{s3_key_prefix}/{redshift_table_name.replace('.', '/')}/{d}.csv.gz"
        put_df(self.s3_client, df, s3_bucket_name, s3_key, format='csv', compression='gzip')

        if drop_table:
            logger.info(">>Droping table")
            self.cursor.execute(f'DROP TABLE IF EXISTS {redshift_table_name} CASCADE;')

        self._create_redshift_table(df,
                                    redshift_table_name,
                                    column_data_types=column_data_types,
                                    column_constraints=column_constraints,
                                    index=index,
                                    diststyle=diststyle,
                                    distkey=distkey,
                                    sort_interleaved=sort_interleaved,
                                    sortkey=sortkey,
                                    debug=debug
                                    )

        self._s3_to_redshift(redshift_table_name,
                             list(df.columns),
                             s3_bucket_name,
                             s3_key,
                             delimiter,
                             quotechar,
                             dateformat,
                             timeformat,
                             region,
                             parameters,
                             aws_role,
                             aws_token)

    def upload_to_redshift(self,
                           df: pandas.DataFrame,
                           redshift_table_name: str,
                           s3_bucket_name: str,
                           s3_key_prefix: str,
                           aws_role: str,
                           debug: bool = False,
                           **kwargs
                           ):
        """Public method to create and load data from a DataFrame to a Redshift table"""

        logger.info("Uploading to Redshift")
        try:
            self._pandas_to_redshift(
                df=df,
                redshift_table_name=f'{self.schema}.{redshift_table_name}',
                s3_bucket_name=s3_bucket_name,
                s3_key_prefix=s3_key_prefix,
                aws_role=aws_role,
                debug=debug,
                **kwargs
            )
        except TypeError:
            raise TypeError('upload_to_redshift() got an unexpected keyword argument when calling pandas_to_redshift()')

        logger.info("Data loaded to Redshift")

    def _create_temp_redshift_table_from_target(self, target_redshift_table_name):
        """Create a temporary table based on a target table exisitng in redshift"""

        self.cursor.execute(f'DROP TABLE IF EXISTS stage_{target_redshift_table_name}')
        create_table_query = f'CREATE TEMP TABLE stage_{target_redshift_table_name} (LIKE {target_redshift_table_name})'

        logger.info('CREATING A TABLE IN REDSHIFT')

        self.cursor.execute(create_table_query)
        self.cursor.execute(f'ALTER TABLE stage_{target_redshift_table_name} DROP COLUMN date_insert;')
        self.connector.commit()

    def _delete_target_redshift_table_line(self, origin_table_name: str, target_table_name: str, key: list):
        """Delete rows in a Redshift table based on another table content"""

        comparison = "WHERE " + " AND".join([f' {target_table_name}.{v} = {origin_table_name}.{v}' for v in key]) + ";"
        query = f"""
            BEGIN TRANSACTION;
            DELETE FROM {target_table_name}
            USING {origin_table_name}
            {comparison};
        """

        logger.debug(f'DELETE LINES IN REDSHIFT TABLE {target_table_name}')

        self.cursor.execute(query)
        self.connector.commit()

    def _insert_target_redshift_table_line(
            self,
            origin_table_name,
            target_table_name,
            drop_origin_table: bool = True
            ):
        """Insert rows in a Redshift table based on another table content"""

        query = f"""
            INSERT INTO {target_table_name}
            SELECT * FROM {origin_table_name};
        """
        if drop_origin_table:
            query += f"\n\tDROP TABLE {origin_table_name};"

        query += "\n\tEND TRANSACTION;"

        logger.debug(f'INSERT LINES IN REDSHIFT TABLE {target_table_name}')

        self.cursor.execute(query)
        self.connector.commit()

    def upsert_rows(
                    self,
                    update_df: pandas.DataFrame,
                    target_table_name: str,
                    s3_bucket_name: str,
                    s3_key_prefix: str,
                    comparison_key: list,
                    aws_role: str,
                    **kwargs) -> None:
        """Performs an upsert lines into a target Redshift table based on a DataFrame content"""

        d = datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
        update_df = self._validate_column_names(update_df)
        s3_key = f"{s3_key_prefix}/{self.schema}/{target_table_name}/{d}.csv.gz"
        put_df(self.s3_client, update_df, s3_bucket_name, s3_key, format='csv', compression='gzip')
        self._create_temp_redshift_table_from_target(target_table_name)
        self._s3_to_redshift(f'stage_{target_table_name}',
                             list(update_df.columns),
                             s3_bucket_name,
                             s3_key,
                             aws_role=aws_role
                             )
        # the following method also begins a transaction
        self._delete_target_redshift_table_line(f'stage_{target_table_name}',
                                                target_table_name,
                                                comparison_key
                                                )
        # the following method also ends the started transaction, and drop the staging table
        self._insert_target_redshift_table_line(f'stage_{target_table_name}',
                                                target_table_name
                                                )
    def get_df(
            self,
            query: str,
            columns_: list() = None,
            fetch_size: int = 1e6) -> pandas.DataFrame:

        logger.debug(f'Execution {query} on Redshift')
        try:
            self.cursor.execute(query)
            self.connector.commit()
        except Exception as e:
            logger.error(e)
            traceback.print_exc(file=sys.stdout)
            self.connector.rollback()
            raise
        df_l = list()
        columns = [c[0] for c in self.cursor.description]
        while True:
            r = self.cursor.fetchmany(fetch_size)
            if len(r) == 0:
                break
            else:
                df_l.append(pandas.DataFrame(r))
        try:
            df = pandas.concat(df_l, axis=0)
            df.columns = columns
            if columns_:
                df = df.rename(index=str, columns={k: v for k, v in columns_.items() if v})
            return df
        except ValueError as e:
            logger.warning('Retrieved dataframe is void')
            return pandas.DataFrame()


from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.firefox import GeckoDriverManager
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.edge.service import Service

browserName= 'firefox'
#'edge'
#firefox
#chrome

if browserName == 'edge':
    s=Service('E:\Install\drivers\msedgedriver.exe')
    driver=webdriver.Edge(service=s)
elif browserName == 'firefox':
    s=Service(GeckoDriverManager().install())
    driver=webdriver.Firefox(service=s)
elif browserName == 'chrome':
    s=Service( ChromeDriverManager().install())
    driver=webdriver.Chrome(service=s)
else:
    print(f'BrowserName: {browserName} is not support')
    raise Exception('driver is not support')

driver.maximize_window()
driver.get('http://supermarket-tws.somee.com')
WebDriverWait(driver, 20).until(EC.title_contains('Super Market an Ecommerce Online Shopping'))
print(driver.title)

driver.close()


#12761_돌다리
from collections import deque
a,b,n,m = map(int, input().split())

def bfs(node):
    q=deque()
    q.append(node)
    graph[node]=0
    while q:
        node = q.popleft()
        for i in [node-1, node+1, node+a, node-a, node+b, node-b, node*a, node*b]:
            if (0<=i<=100000) and graph[i]==-1:
                q.append(i)
                graph[i]=graph[node]+1
                if i == m:
                    return graph[m]

graph = [-1]*100001
print(bfs(n))


# -*- coding: utf-8 -*-
import time
import typing
import threading

from .msgbox import *
from ..misc.utils import wait_timeout
from ..misc.debug import ExceptionHandle
from ..core.threading import ThreadConditionWrap
from .mailbox import UiMailBox, MessageBoxMail, QuestionBoxMail, ProgressBarMail
__all__ = ['ExceptionHandleMsgBox', 'ProgressBarContextManager',
           'showMessageBoxFromThread', 'showQuestionBoxFromThread']

class ExceptionHandleMsgBox(ExceptionHandle):
    def __init__(self, mailbox: UiMailBox, **kwargs):
        super(ExceptionHandleMsgBox, self).__init__(
            param=mailbox, callback=lambda mbox, msg:
            mbox.send(MessageBoxMail(MB_TYPE_ERR, msg, f'{self.__class__.__name__}')), **kwargs
        )

class ProgressBarContextManager:
    def __init__(self, mailbox: UiMailBox,
                 content: str, title: str = '',
                 init: int = 1, increase: int = 1, interval: float = 1.0, timeout: int = 0xffffffff,
                 ignore_exceptions: bool = False, catch_exceptions: typing.Sequence[typing.Type[Exception]] = None):
        self.__init = init
        self.__title = title
        self.__mailbox = mailbox
        self.__content = content
        self.__increase = increase
        self.__interval = interval
        self.__max_timeout = timeout
        self.__operation_finish = threading.Event()
        self.__ignore_exceptions = ignore_exceptions
        self.__catch_exceptions = catch_exceptions or list()

    def __timeoutDetection(self):
        try:
            wait_timeout(self.__operation_finish.is_set, timeout=self.__max_timeout, desc=self.__content)
        except RuntimeError:
            self.set_closeable()
            self.__sendProgressBarMail(0)
            self.__mailbox.send(MessageBoxMail(MB_TYPE_ERR, '操作超时，请检查', f'{self.__title}超时'))

    def __sendProgressBarMail(self, progress: int, closeable: bool = False):
        self.__mailbox.send(ProgressBarMail(
            progress, self.__content, self.__title,
            closeable=closeable, increase=self.__increase, interval=self.__interval
        ))

    def set_closeable(self):
        self.__sendProgressBarMail(self.__mailbox.getProgressValue(), True)

    def update(self, progress: int):
        self.__sendProgressBarMail(progress)

    def __enter__(self):
        self.__sendProgressBarMail(self.__init)
        threading.Thread(target=self.__timeoutDetection, daemon=True).start()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.__operation_finish.set()
        self.__sendProgressBarMail(0)
        if exc_type in self.__catch_exceptions:
            self.__mailbox.send(MessageBoxMail(MB_TYPE_ERR, f'{exc_val}', f'{self.__title}失败'))
            return True

        return self.__ignore_exceptions

def showMessageBoxFromThread(mailbox: UiMailBox, msg_type: str, content: str, title: str = '') -> bool:
    mailbox.send(MessageBoxMail(msg_type, content, title))
    return True if msg_type == MB_TYPE_INFO else False

def showQuestionBoxFromThread(mailbox: UiMailBox, content: str, title: str = '') -> bool:
    condition = ThreadConditionWrap()
    mailbox.send(QuestionBoxMail(content, title, condition))
    return condition.wait()


import os
import argparse

import SimpleITK as sitk
import numpy as np

def check_subject_validity(subject_dir):
    """Checks if a subject folder is valid.

    Args:
        subject_dir (str): The subject folder.

    Returns:
        bool: True if the subject folder is valid, False otherwise.
    """
    subject_valid = True
    strings_to_check = [
        "_t1.nii.gz",
        "_t1ce.nii.gz",
        "_t2.nii.gz",
        "_flair.nii.gz",
        "_seg.nii.gz",
    ]

    for string in strings_to_check:
        if not os.path.isfile(
            os.path.join(subject_dir, os.path.basename(subject_dir) + string)
        ):
            subject_valid = False
            break

    return subject_valid

def sanity_check(data_path):
    """Runs a few checks to ensure data quality and integrity

    Args:
        data_path (str): The input data folder.
    """
    all_files = os.listdir(data_path)

    # define base size and spacing
    base_size = np.array([240, 240, 155])
    base_spacing = np.array([1.0, 1.0, 1.0])

    for folders in all_files:
        current_subject = os.path.join(data_path, folders)
        if os.path.isdir(current_subject):

            assert check_subject_validity(
                current_subject
            ), "Subject {} does not contain all modalities or segmentation".format(
                current_subject
            )

            for files in os.listdir(current_subject):
                current_file = os.path.join(current_subject, files)
                if os.path.isfile(current_file):
                    # perform BraTS space check for all nifti images
                    if current_file.endswith(".nii.gz"):
                        image = sitk.ReadImage(current_file)
                        size_array = np.array(image.GetSize())
                        spacing_array = np.array(image.GetSpacing())

                        assert (base_size == size_array).all(), (
                            "Image size is not [240,240,155] for " + current_file
                        )
                        assert (base_spacing == spacing_array).all(), (
                            "Image resolution is not [1,1,1] for " + current_file
                        )

if __name__ == "__main__":
    parser = argparse.ArgumentParser("BraTS Data Sanity Check")
    parser.add_argument(
        "--data_path",
        dest="data",
        type=str,
        help="directory containing the prepared data",
    )

    args = parser.parse_args()

    sanity_check(args.data)

    print("Finished")


from scapy.layers.tls.all import *
from scapy.all import *
import requests
from ping3 import ping as ping3_ip

class ether(object):
    def __init__(self, conf, doh):
        self.reqs = requests.session()
        self.clflr = []
        self.clflr_ip = {}
        self.conf = conf
        self.doh = doh

    def get_sni(self, data):
        data = TLS(data)
        tls_ext = data[TLS].msg[0][TLS_Ext_ServerName][0]
        return tls_ext[ServerName].servername.decode()

    def exists_header_clflr(self, host):
        resp = self.reqs.get(f"https://{host}")
        return resp.headers.get("Server") == "cloudflare"

    def check_work_ip(self, addr):
        ping_time = ping3_ip(addr)
        if math.ceil(ping_time) != 0:
            ping_time = format(ping_time, ".3g")
            return ping_time
        return False

    def get_proxy_info(self):
        proxy_host, proxy_prot = self.conf.get_proxy()
        self.reqs.proxies = {'http': f'http://{proxy_host}:{proxy_prot}'}

    def header_args(self):
        return {
            "accept": "text/html"
        }

    def check_work_ip_sni(self, addr, domain):
        print ("check_work_ip_sni")
        proxies = self.get_proxy_info()
        header_args = self.header_args()
        try:
            resp = self.reqs.get(f"https://{domain}", headers=header_args)
            return addr
        except Exception as err:
            return False

    def clflr_range_ping(self, domain):
        try:
            clflr_range = self.conf.clflr_range()
            for clflr_ip in clflr_range:
                clflr_ip = clflr_ip.split(".")[:-1]
                for num in range(1, 255):
                    clflr_tmp = []
                    clflr_tmp = copy.deepcopy(clflr_ip)
                    clflr_tmp.append(str(num))
                    clflr_tmp = ".".join(clflr_tmp)
                    self.clflr_ip[domain] = clflr_tmp
                    work_ip_sni = self.check_work_ip_sni(clflr_tmp, domain)
                    if work_ip_sni != False:
                        self.doh.cache[domain] = work_ip_sni
                        return work_ip_sni

        except Exception as err:
            print("** clflr range not ping")
            return self.conf.def_clflr_ip()

    def check_clflr(self, data):
        host = self.get_sni(data)
        if host in self.clflr:
            if host in self.clflr_ip:
                clflr_ip = self.clflr_ip.get(host)
            else:
                clflr_ip = self.clflr_range_ping(host)
        else:
            is_clflr = self.exists_header_clflr(host)
            if is_clflr:
                self.clflr.append(host)
                clflr_ip = self.doh.query(host)
                chip = self.check_work_ip(clflr_ip)
                if chip != False:
                    self.clflr_ip[host] = clflr_ip
                else:
                    clflr_ip = self.clflr_range_ping(host)

        return (host, 443)


# DiscordWebhook

import json
import requests

class WebhookClient(object):
    """
    A class for interacting with webhooks [sending messages and deleting the webhook].
    """
    def __init__(self, username, webhook_url, avatar_url):
        if len(username) < 2 or len(username) > 32:
            raise ValueError('Username must be a string and has a length between 2 and 32.')
        if not requests.get(webhook_url).ok:
            raise WebhookInvalid("Not a valid Discord webhook.")
        self.username = username
        self.webhook_url = webhook_url
        self.avatar_url = avatar_url
    def send(self, message, tts=False):
        """
        Sends a message through a WebhookClient with the specified URL.
        """
        payload = {
            "avatar_url": str(self.avatar_url),
            "username": str(self.username),
            "content": message,
            "tts": tts
        }
        headers = {
            'Content-Type': 'application/json',
        }
        response = requests.post(self.webhook_url, data=json.dumps(payload), headers=headers)
        if not response.ok:
            raise requests.HTTPError("An error was returned from the server.")
    def delete(self):
        """
        Deletes the Webhook in WebhookClient.wurl.
        """
        response = requests.delete(self.webhook_url)
        if not response.ok:
            raise requests.HTTPError("An error was returned from the server.")
        return 'Webhook deleted.'
    def save(self, filename):
        """
        Saves the current WebhookClient to a .json file.
        """
        json.dump(self.__dict__, open(filename, 'x'))
class WebhookInvalid(Exception):
    """
    A Exception for Invalid Webhook passed into WebhookClient.
    """
    pass

def clifromjson(configjson):
    """
    A function for creating a WebhookClient object from a config json file. Useful if you're making a Webhook app that needs a config.
    """
    config = json.load(open(configjson))
    return WebhookClient(
        config['username'],
        config['webhook_url'],
        config['avatar_url'])

def jsonfromcli(cli, filename):
    """
    A fucntion for creating a json config file from a WebhookClient object.
    """
    if type(cli) != WebhookClient:
        raise TypeError('cli must be a WebhookClient object.')
    json.dump(cli.__dict__, open(filename, 'x'))


from drqa import retriever
import random
from tqdm import tqdm
import jsonlines
import json
import click
import os
import time

def batch(iterable, n=1):
    l = len(iterable)
    for ndx in range(0, l, n):
        yield iterable[ndx:min(ndx + n, l)]

@click.command()
@click.option("--model", type=click.Path(exists=True), help="Path to .npz tfidf model")
@click.option("--fills", type=click.Path(exists=True), help="jsonl file containing fills")
@click.option("--clues", type=click.Path(exists=True), help="jsonl file containing clues")
@click.option("--out", type=click.Path(exists=True), help="directory to put output in")
@click.option("--k", type=int, default=100, help="number of closest docs to search")
@click.option("--len-filter/--no-len-filter", default=True, help="Don't use length filter with negative matching")
def main(model, fills, clues, out, len_filter, k):
    os.makedirs(model, exist_ok=True)
    model = os.path.join(model, os.listdir(model)[0])
    ranker = retriever.get_class('tfidf')(tfidf_path=model)
    clues_ = []
    fills_ = []
    with jsonlines.open(clues) as reader:
        for line in reader:
            clues_.append(line["text"])

    with jsonlines.open(fills) as reader:
        for line in reader:
            fills_.append(line["text"])

    pairs = list(zip(clues_, fills_))

    top_negative = []
    print("Finding negative examples")
    for b in tqdm(list(batch(pairs, n=1000))):
        clues, fills = list(zip(*b))
        try:
            batch_doc_names, batch_doc_scores = list(zip(*ranker.batch_closest_docs(clues, k)))
            j = 0
            for i in range(len(clues)):
                clue, fill = clues[i], fills[i]
                doc_ids = [int(doc_name[3:]) for doc_name in batch_doc_names[i]]
                found = False
                for i in doc_ids:
                    pair = pairs[i]
                    if pair[0] != clue and pair[1] != fill:
                        top_negative.append(pair)
                        found = True
                        break
                if not found:
                    while True:
                        cand = random.choice(pairs)
                        if not len_filter or len(cand[1]) == len(fill):
                            top_negative.append(cand)
                            break
                j +=1
        except Exception as e:
            for c, fill in zip(clues, fills):
                while True:
                    cand = random.choice(pairs)
                    if len(cand[1]) == len(fill):
                        top_negative.append(cand)
                        break
    print("Building json")
    filename = os.path.join(out, "train.json")
    build_json(pairs, top_negative, filename)

def build_json(pos_examples, neg_examples, filename):
    """ Takes in two equal length lists pos_examples and neg_examples, and builds the json for dpr.
    """
    assert len(pos_examples) == len(neg_examples)
    print(f"Building json... at {filename}")
    joined_ex = []
    for i in tqdm(range(len(pos_examples))):
        example = {}
        example["question"] = pos_examples[i][0]
        example["answers"] = [],
        example["positive_ctxs"] = [{
            "text": pos_examples[i][1],
            "title": ""}]
        example["hard_negative_ctxs"] = [{
            "text": neg_examples[i][1],
            "title": ""}]
        example["negative_ctxs"] = []
        joined_ex.append(example)
    with open(filename, "w") as f:
        joined_json = json.dumps(joined_ex, indent=4)
        f.write(joined_json)

if __name__ == "__main__":
    main()


from django.shortcuts import render, get_object_or_404, get_list_or_404, redirect
from django.contrib.auth.decorators import login_required
from django.db.models import Q
from .models import Instructor
from .forms import InstructorForm
# Create your views here.
@login_required
def instructorList(request):
	instructor_list=Instructor.objects.all()
	instructor_count = Instructor.objects.count()
	query = request.GET.get("q")
	if query:
		instructor_list = instructor_list.filter(
			Q(instructor__first_name__startswith=query) |
			Q(instructor__last_name__startswith=query) |
			Q(instructor__email__startswith=query) |
			Q(employment_status__status__startswith=query) |
			Q(contact__startswith=query)
			).distinct()
	context={
		"title": "instructor list",
		"instructorlist": instructor_list,
		"searchPlaceholder": "Search instructor",
		"instructorcount": instructor_count,
	}
	return render(request, "instructor/instructor_list.html", context)
@login_required
def instructorDetail(request, pk):
	instructor_instance=get_object_or_404(Instructor, pk=pk)
	context={
		"title":"instructor details",
		"instance": instructor_instance,
	}
	return render(request, "instructor/instructor_detail.html", context)
@login_required
def instructorAdd(request):
	form = InstructorForm(request.POST or None, request.FILES or None)
	if request.method == "POST":
		if form.is_valid():
			instance=form.save(commit=False)
			instance.user = request.user
			instance.save()
			return redirect("instructor:detail", pk=instance.id)
	context = {
		"title": "add instructor",
		"form": form,
	}
	return render(request, "instructor/instructor_add.html", context)


import io
import os
import csv
import rake
import pickle
import tensorflow
import numpy as np
import pandas as pd
from io import TextIOWrapper
from keras.layers import Embedding
from healthcheck import HealthCheck
from tensorflow.keras.models import load_model
from flask import Flask, make_response, request, jsonify, json, render_template
from keras.preprocessing.sequence import pad_sequences
from sklearn.feature_extraction.text import CountVectorizer
import logging

app = Flask(__name__)

with open('tokenizer.picklex', 'rb') as handle:
    vec = pickle.load(handle)

pl = CountVectorizer(decode_error="replace", vocabulary=pickle.load(open("feature.pkl", "rb")))

word2idx = vec.word_index
vocab_size = len(vec.word_index) + 1

word2vec = {}
with open(os.path.join("glove.6B.100d.txt")) as f:
    for line in f:
        values = line.split()
        word = values[0]
        arr = np.asarray(values[1:], dtype="float32")
        word2vec[word] = arr

num_words = min(vocab_size, len(word2idx)+1)
Embedding_dim = 100
embedding_matrix = np.zeros((num_words, Embedding_dim))
for word, i in word2idx.items():
    if i < num_words:
        embedding_vector = word2vec.get(word)
        if embedding_vector is not None:
            embedding_matrix[i] = embedding_vector

embedding_layer = Embedding(num_words, Embedding_dim, weights=[embedding_matrix], input_length=22, trainable=False)

model = load_model("without_oov.h5")

logging.basicConfig(filename="flask.log", level=logging.DEBUG, format="%(asctime)s %(levelname)s %(name)s %(threadName)s:%(message)s")
logging.info("All useful files loaded")

health = HealthCheck(app, "/hcheck")

def app_ab():
    return True, "i am good"

health.add_check(app_ab)

def transform(text):
    text_m = rake.rake(text)
    tok = []
    for k, j in text_m.items():
        tok.append(k)
    text_m1 = " ".join(tok)
    p_f1 = vec.texts_to_sequences([text_m1])
    pp_f1 = pad_sequences(p_f1, maxlen=22, padding='post')
    predictions = model.predict(pp_f1)
    out_class = predictions.argmax(axis=-1)
    key = [key for (key, value) in pl.vocabulary.items() if value == out_class[0]]
    return key[0]

@app.route('/')
def form():
    return """

Tagging the tickets..

 Insert the data CSV file and then download the Result
            <form action="/transform" method="post" enctype="multipart/form-data">
                <input type="file" name="data_file" class="btn btn-block"/>

                <button type="submit" class="btn btn-primary btn-block btn-large">Predict</button>
            </form>

"""

@app.route('/transform', methods=["POST"])
def transform_view():
    if request.method == 'POST':
        df = pd.read_csv(request.files.get('data_file'))
        df = df.dropna()
        pic = []
        for rec in range(len(df["Description"])):
            pic.append(transform(df["Description"][rec]))
        df["predicted_category"] = pic
        response = make_response(df.to_csv())
        response.headers["Content-Disposition"] = "attachment; filename=result.csv"
        response.headers["Content-Type"] = "text/csv"
        app.logger.info("Logged in")
        return response

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


#!/usr/bin/python3.5

from datetime import datetime as dt
import sys

print("Ежедневник, выберите действие:\n\n{0}{1}{2}{3}{4}{5}".format(
    "1. Вывести список задач\n", "2. Добавить задачу\n",
    "3. Отредактировать задачу\n", "4. Завершить задачу\n",
    "5. Начать задачу сначала\n", "6. Выход\n"
))
tasks = {}

accomplished_tasks = set()
not_accomplished_tasks = set()

while True:
    number = int(input("Введите число: "))
    if number == 1:
        date = input("Введите дату в таком порядке: год месяц день ").split()
        current_date = [dt.now().year, dt.now().month, dt.now().day]
        def list_of_tasks(date=current_date):
            for identificator in tasks:
                if identificator["date"] == date and \
                                identificator not in accomplished_tasks:
                    print(identificator)
        list_of_tasks(date)

    if number == 2:
        identificator = int(input("Введите идентификатор данной задачи "))
        name = input("Введите название задачи ")
        text = input("Введите текст задачи ")
        date = input("Введите дату, к которой задание должно быть выполнено,"
                     "в формате год месяц день ").split()
        time = input("Введите время, к которому задача должна быть выполнена, "
                     "в формате час минута секунда ").split()
        print('Статус задачи установлен на "Не выполнено"')
        not_accomplished_tasks.add(identificator)
        tasks[identificator] = {"name": name, "text": text, "date": date,
                                "time": time}

    if number == 3:
        identificator = int(input("Введите идентификатор задачи "
                                  "для редактирования "))
        if identificator not in tasks:
            raise NameError("Нету задачи с таким идентификатором!")
        if not isinstance(identificator, int):
            raise ValueError("Идентификатор должен быть целым числом!")
        name = input("Введите новое название задачи ")
        text = input("Введите новый текст задачи ")
        date = input("Введите новую дату, к которой задание должно быть "
                     "выполнено, в формате год месяц день ").split()
        time = input("Введите новое время, к которому задача должна быть "
                     "выполнена, в формате час минута секунда ").split()
        tasks[identificator]["name"] = name
        tasks[identificator]["text"] = text
        tasks[identificator]["date"] = date
        tasks[identificator]["time"] = time

    if number == 4:
        identificator = int(input("Введите идентификатор данной задачи "))
        if identificator not in tasks:
            raise NameError("Нету задачи с таким идентификатором!")
        if not isinstance(identificator, int):
            raise ValueError("Идентификатор должен быть целым числом!")
        accomplished_tasks.add(identificator)
        not_accomplished_tasks.remove(identificator)

    if number == 5:
        identificator = int(input("Введите идентификатор данной задачи "))
        if identificator not in tasks:
            raise NameError("Нету задачи с таким идентификатором!")
        if not isinstance(identificator, int):
            raise ValueError("Идентификатор должен быть целым числом!")
        not_accomplished_tasks.add(identificator)
        accomplished_tasks.remove(identificator)

    if number == 6:
        sys.exit()


from typing import List
class Solution:
    def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
        result = []
        for i in range(len(intervals)):
            if newInterval[1] < intervals[i][0]:
                result.append(newInterval)
                return result + intervals[i:]
            elif newInterval[0] > intervals[i][1]:
                result.append(intervals[i])
            else:
                newInterval = [min(newInterval[0], intervals[i][0]), max(newInterval[1], intervals[i][1])]
                # print(newInterval)
        result.append(newInterval)
        # print(result)
        return result

if __name__ == "__main__":
    intervals = [[1,3],[6,9]]
    newInterval = [2,5]
    exp = [[1,5],[6,9]]

    intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]]
    newInterval = [4,8]
    exp = [[1,2],[3,10],[12,16]]

    x = Solution()
    res = x.insert(intervals, newInterval)
    print(exp, res)


from django.urls import path, re_path
from apps.validations.views import ListView, SearchView, ValidateVehicle, procedureAPIList, ValidationForm , ProcedureRegister, ProcedureCreate, ContractCreate, authorizaitonCreate, ValidateCreate, YearAntiquity, ProcedureSearchAPIView, ProcedureView,authorizationCreate, substitutionCreate, ProcedureViewList, ValidateUpdate

urlpatterns = [
    path('', ListView.index, name='validate.index'),
    path('create/', ValidateCreate.as_view(), name='validate.create.proceeding'),
    path('update/<int:pk>', ValidateUpdate.as_view(), name='validate.update.proceeding'),
    path('search/', SearchView.index, name='validate.search'),
    path('view/<str:pk>', ProcedureViewList.as_view(), name='procedure.view'),
    path('select/<str:pk>', ValidateVehicle.index, name='validate.val'),
    path('procedure/<str:pk>', ValidationForm.as_view(), name='procedure.register'),
    path('authorization/create', authorizationCreate.as_view(), name='procedure.authorization.create'),
    path('api/list/vehicle/', procedureAPIList.as_view(), name='procedure.list'),
    path('api/route/create/', ProcedureCreate.as_view(), name='procedure.create'),
    path('api/contract/create/<str:plate>', ContractCreate.as_view(), name='procedure.contract.create'),
    path('api/authorization/create/', authorizaitonCreate.as_view(), name='procedure.api.authorization.create'),
    path('api/substitution/create/', substitutionCreate.as_view(), name='procedure.api.substitution.create'),
    path('api/control/year/', YearAntiquity.as_view(), name="year.antiquity"),
    path('api/search/', ProcedureSearchAPIView.as_view(), name='procedure.search')
]


import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression

train_x=pd.read_csv(r'C:\Users\huang\Downloads\train (1)_scilearn.csv',header=None)
train_y=pd.read_csv(r'C:\Users\huang\Downloads\trainLabels_scilearn.csv',header=None)
test_x=pd.read_csv(r'C:\Users\huang\Downloads\test (1)_scilearn.csv',header=None)

logreg=LogisticRegression()

logreg.fit(train_x,train_y)
predicted=logreg.predict(test_x)

predicted=pd.DataFrame(predicted,index=np.arange(1,len(predicted)+1),columns=['Solution'])
predicted['Id']=predicted.index.name()
predicted.index=predicted['Id']

predicted.to_csv(r'C:\Users\huang\Desktop\london.csv')


import tensorflow as tf
import os
import numpy as np
import cv2
import scipy.io as sio
import utils
import glob
import tensorflow.contrib.eager as tfe
tfe.enable_eager_execution()

class ResNet50:
    def __init__(self, dataset, class_num):
        self.class_num = class_num
        self.idx_tensor = [idx for idx in range(self.class_num)]
        self.idx_tensor = tfe.Variable(np.array(self.idx_tensor, dtype=np.float32))
        self.dataset = dataset
        self.model = self.__create_model()

    def __loss_angle(self, y_true, y_pred, alpha=0.5):
        # cross entropy loss
        bin_true = y_true[:,0]
        cont_true = y_true[:,1]
        onehot_labels = tf.keras.utils.to_categorical(bin_true, self.class_num)
        cls_loss = tf.losses.softmax_cross_entropy(onehot_labels=onehot_labels, logits=y_pred)
        # MSE loss
        pred_cont = tf.reduce_sum(tf.nn.softmax(y_pred) * self.idx_tensor, 1) * 3 - 99
        mse_loss = tf.losses.mean_squared_error(labels=cont_true, predictions=pred_cont)
        # Total loss
        total_loss = cls_loss + alpha * mse_loss
        return total_loss

    def __create_model(self):
        resnet = tf.keras.applications.resnet50.ResNet50(include_top=False, weights='imagenet',input_shape=(self.dataset.input_size,self.dataset.input_size,3))
        output = resnet.layers[-1].output
        output = tf.keras.layers.Flatten()(output)

        fc_yaw = tf.keras.layers.Dense(name='yaw', units=self.class_num)(output)
        fc_pitch = tf.keras.layers.Dense(name='pitch', units=self.class_num)(output)
        fc_roll = tf.keras.layers.Dense(name='roll', units=self.class_num)(output)

        model = tf.keras.Model(inputs=resnet.inputs, outputs=[fc_yaw, fc_pitch, fc_roll])

        losses = {
            'yaw':self.__loss_angle,
            'pitch':self.__loss_angle,
            'roll':self.__loss_angle,
        }

        model.compile(optimizer=tf.train.AdamOptimizer(),loss=losses)

        return model

    def train(self, model_path, max_epoches, load_weight, should_train):

        if load_weight:
            self.model.load_weights(model_path)

        if should_train:
            self.model.fit(self.dataset.train_generator(shuffle=True),epochs=max_epoches,
                            steps_per_epoch=self.dataset.train_num // self.dataset.batch_size
                            ,max_queue_size=10,workers=1,verbose=1)

            self.model.save(model_path)

    def test(self):
        result_file = os.path.join(self.dataset.result_dir, self.dataset.result_file)
        if os.path.exists(result_file):
            os.remove(result_file)

        images, names = self.dataset.test_generator()
        predictions = self.model.predict(images, batch_size=len(images), verbose=1)
        predictions = np.asarray(predictions)
        yaws = tf.reduce_sum(tf.nn.softmax(predictions[0,:,:]) * self.idx_tensor, 1) * 3 - 99
        pitches = tf.reduce_sum(tf.nn.softmax(predictions[1,:,:]) * self.idx_tensor, 1) * 3 - 99
        rolls = tf.reduce_sum(tf.nn.softmax(predictions[2,:,:]) * self.idx_tensor, 1) * 3 - 99

        with open(result_file,'w+') as rf:
            for name, yaw, pitch, roll in zip(names, yaws, pitches, rolls):
                self.dataset.save_result(name, yaw, pitch, roll)
                rx, ry, rz = utils.convert_ypr_to_rvec(yaw, pitch, roll)
                rf.write(f'{name}, {rx}, {ry}, {rz}\n')


import plotly
import plotly.graph_objs as go
import json
import numpy as np
import pandas as pd
import sensors

def create_plot(sensorType: str, sensorName: str):
    N = 40
    x = np.linspace(0, 1, N)
    y = []
    sensor = sensors.Sensor(sensorType)
    for i in range(0, N):
        y.append(sensor.getReading()['value'])

    df = pd.DataFrame({'x': x, 'y': y})  # creating a sample dataframe

    data = [
        go.Scatter(
            x=df['x'],  # assign x as the dataframe column 'x'
            y=df['y'],
        )
    ]

    graphJSON = json.dumps(data, cls=plotly.utils.PlotlyJSONEncoder)
    return graphJSON


"""PythonProject URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.conf.urls import include, url
from django.views.static import serve #需要导入
from django.conf import settings

urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^media/(?P<path>.*)$', serve, {'document_root':settings.MEDIA_ROOT}),#这部分很重要
    url(r'^UserInfo/', include('apps.UserInfo.urls', namespace='UserInfo')),  # 用户模块
    url(r'^ContestItem/', include('apps.ContestItem.urls', namespace='ContestItem')),  # 比赛项目模块
    url(r'^Contest/', include('apps.Contest.urls', namespace='Contest')),  # 比赛模块
    url(r'^SignUp/', include('apps.SignUp.urls', namespace='SignUp')),  # 学生报名模块
    url(r'^Court/', include('apps.Court.urls', namespace='Court')),  # 赛场安排模块
    url(r'^ContestPlace/', include('apps.ContestPlace.urls', namespace='ContestPlace')),  # 比赛场地模块
    url(r'^Jifen/', include('apps.Jifen.urls', namespace='Jifen')),  # 比赛积分模块
    url(r'^DoctorTeam/', include('apps.DoctorTeam.urls', namespace='DoctorTeam')),  # 医护团队模块
    url(r'^Leaveword/', include('apps.Leaveword.urls', namespace='Leaveword')),  # 留言模块
    url(r'^Notice/', include('apps.Notice.urls', namespace='Notice')),  # 新闻公告模块

    url(r'^', include("apps.Index.urls", namespace="Index")),  # 首页模块

    url(r'^tinymce/', include('tinymce.urls')),
]


# coding: utf-8
import cv2

from ..utils.generic_utils import handleKeyError, handleTypeError

OPENCV_MORPHOLOGY_OPERATIONS = {
    "opening"  : cv2.MORPH_OPEN,
    "closing"  : cv2.MORPH_CLOSE,
    "gradient" : cv2.MORPH_GRADIENT,
    "tophat"   : cv2.MORPH_TOPHAT,
    "blackhat" : cv2.MORPH_BLACKHAT,
    "hitmiss"  : cv2.MORPH_HITMISS,
}
OPENCV_MORPHOLOGY_KERNEL_SHAPES = {
    "cross"  : cv2.MORPH_CROSS,
    "dilate" : cv2.MORPH_DILATE,
    "erode"  : cv2.MORPH_ERODE,
    "open"   : cv2.MORPH_OPEN,
}

def morph_kernel_creator(shape="erode", ksize=(5,5)):
    """Returns a structuring element of the specified size and shape for morphological operations.

    Args:
        shape (str, int) : Element shape.
        ksize (tuple)    : Size of the structuring element.

    Examples:
        >>> from pycharmers.opencv import morph_kernel_creator
        >>> kernel = morph_kernel_creator(shape="erode", ksize=(5,5))
        >>> kernel.shape
        (5, 5)
        >>> kernel = morph_kernel_creator(shape="erode", ksize=(3,5))
        >>> kernel.shape
        >>> (5, 3)
    """
    handleTypeError(types=[str, int], shape=shape)
    if isinstance(shape, str):
        handleKeyError(lst=list(OPENCV_MORPHOLOGY_KERNEL_SHAPES.keys()), shape=shape)
        shape = OPENCV_MORPHOLOGY_KERNEL_SHAPES.get(shape)
    return cv2.getStructuringElement(shape=shape, ksize=tuple(ksize))

def morph_transformer_creator(op, kernel=None, shape="erode", ksize=(5,5)):
    """Returns a function which performs advanced morphological transformations.

    Args:
        op (str, int)    : Type of a morphological operation
        kernel (array)   : Structuring element. It can be created using ``morph_kernel_creator``
                           The same can be achieved by giving values in the parameters (``shape``, ``ksize``).
        shape (str, int) : Element shape.
        ksize (tuple)    : Size of the structuring element.

    Examples:
        >>> from pycharmers.opencv import (SAMPLE_LENA_IMG, morph_transformer_creator,
                                        hconcat_resize_min, cv2plot, cv2read_mpl)
        >>> img = cv2read_mpl(SAMPLE_LENA_IMG, 0)
        >>> transformer = morph_transformer_creator(op="opening", shape="open", ksize=(12,12))
        >>> img_opening = transformer(img)
        >>> ax = cv2plot(hconcat_resize_min(img, img_opening), cmap="gray")
    """
    handleTypeError(types=[str, int], op=op)
    if isinstance(shape, str):
        handleKeyError(lst=list(OPENCV_MORPHOLOGY_OPERATIONS.keys()), op=op)
        op = OPENCV_MORPHOLOGY_OPERATIONS.get(op)
    if kernel is None:
        kernel = morph_kernel_creator(shape=shape, ksize=ksize)
    transformer = lambda src: cv2.morphologyEx(src, op, kernel)
    return transformer


import pandas as pd
import numpy as np
import matplotlib
try:
    import matplotlib.pyplot as plt
except ImportError:
    matplotlib.use("agg")
    import matplotlib.pyplot as plt

from single_period import SinglePeriod

class MultiPeriod(SinglePeriod):
    """Solve the multi-period (dynamic) portfolio optimization problem.
    """
    def __init__(self, stocks=('AAPL', 'MSFT', 'AAL', 'WMT'), budget=1000,
                 bin_size=None, gamma=None, file_path=None,
                 dates=None, model_type='CQM', alpha=0.005, baseline='^GSPC',
                 sampler_args=None, t_cost=0.01, verbose=True):
        """Class constructor.

        Args:
            stocks (list of str): List of stocks.
            budget (int): Portfolio budget.
            bin_size (int): Maximum number of intervals for each stock.
            gamma (float or int or list or tuple): Budget constraint penalty coefficient(s).
                If gamma is a tuple/list and model is DQM, grid search will be done;
                otherwise, no grid search.
            file_path (str): Full path of CSV file containing stock data.
            dates (list of str): Pair of strings for start date and end date.
            model_type (str): CQM or DQM.
            alpha (float or int or list or tuple): Risk aversion coefficient.
                If alpha is a tuple/list and model is DQM, grid search will be done;
                otherwise, no grid search.
            baseline (str): Stock baseline for rebalancing model.
            sampler_args (dict): Sampler arguments.
            t_cost (float): transaction cost; percentage of transaction dollar value.
            verbose (bool): Flag to enable additional output.
        """
        super().__init__(stocks=stocks, budget=budget, t_cost=t_cost,
                 bin_size=bin_size, gamma=gamma, file_path=file_path,
                 dates=dates, model_type=model_type, alpha=alpha,
                 baseline=baseline, sampler_args=sampler_args, verbose=verbose)

    def run(self, max_risk=0, min_return=0, num=0, init_holdings=None):
        """Solve the rebalancing portfolio optimization problem.

        Args:
            max_risk (int): Maximum risk for the CQM risk bounding formulation.
            min_return (int): Minimum return for the CQM return bounding formulation.
        """
        if not self.dates:
            self.dates = ['2010-01-01', '2012-12-31']
        self.load_data()

        num_months = len(self.df_all)
        first_purchase = True
        result = {}
        baseline_result = {}
        self.baseline_values = [0]
        self.update_values = [0]
        months = []

        # Define dataframe to save output data
        headers = ['Date', 'Value'] + self.stocks + ['Variance', 'Returns']
        self.opt_results_df = pd.DataFrame(columns=headers)
        row = []

        self.price_df = pd.DataFrame(columns=self.stocks)

        # Initialize the plot
        plt.ylim(ymax = 1.5*self.budget, ymin = -1.5*self.budget)
        plt.xticks(list(range(0, num_months, 2)),
                self.df_baseline.index.strftime('%b')[::2], rotation='vertical')
        plt.locator_params(axis='x', nbins=num_months/2)
        plt.plot(list(range(0, num_months)), [0]*(num_months),
                color='red', label="Break-even", linewidth=0.5)

        for i in range(3, num_months):

            # Look at just the data up to the current month
            df = self.df_all.iloc[0:i+1,:].copy()
            baseline_df_current = self.df_baseline.iloc[0:i+1,:]
            print("\nDate:", df.last_valid_index())
            months.append(df.last_valid_index().date())

            if first_purchase:
                budget = self.budget
                initial_budget = self.budget
                baseline_shares = (budget / baseline_df_current.iloc[-1])
                baseline_result = {self.baseline[0]: baseline_shares}
            else:
                # Compute profit of current portfolio
                budget = sum([df.iloc[-1][s]*result['stocks'][s] for s in self.stocks])
                self.update_values.append(budget - initial_budget)

                # Compute profit of fund portfolio
                fund_value = sum([baseline_df_current.iloc[-1][s]*baseline_result[s]
                                 for s in self.baseline])
                self.baseline_values.append(fund_value - initial_budget)

                self.budget = budget

            self.load_data(df=df)

            self.price_df.loc[i-2] = list(self.price.values)

            # Output for user on command-line and plot
            update_values = np.array(self.update_values, dtype=object)
            baseline_values = np.array(self.baseline_values, dtype=object)
            plt.plot(range(3, i+1), update_values,
                    color='blue', label="Optimized portfolio")
            plt.plot(range(3, i+1), baseline_values,
                    color='gray', label="Fund portfolio", linewidth=0.5)

            if first_purchase:
                plt.legend(loc="lower left")
                plt.title("Start: {start}, End: {end}".format\
                        (start=self.df_all.first_valid_index().date(),
                         end=self.df_all.last_valid_index().date()))

            plt.savefig("portfolio.png")
            plt.pause(0.05)

            # Making solve run
            if self.model_type == 'DQM':
                print(f"\nMulti-Period DQM Run...")

                self.build_dqm()
                self.solution['DQM'] = self.solve_dqm()
                result = self.solution['DQM']
            else:
                print(f"\nMulti-Period CQM Run...")

                # Set budget to 0 to enforce that portfolio is self-financing
                if self.t_cost and not first_purchase:
                    self.budget = 0

                self.solution['CQM'] = self.solve_cqm(max_risk=max_risk,
                                                      min_return=min_return,
                                                      init_holdings=init_holdings)
                result = self.solution['CQM']
                init_holdings = result['stocks']

            # Print results to command-line
            value = sum([self.price[s]*result['stocks'][s] for s in self.stocks])
            returns = result['return']
            variance = result['risk']

            row = [months[-1].strftime('%Y-%m-%d'), value] + \
                [result['stocks'][s] for s in self.stocks] + \
                [variance, returns]
            self.opt_results_df.loc[i-2] = row

            first_purchase = False

        print(self.opt_results_df)
        print(f'\nRun completed.\n')

        plt.savefig("portfolio.png")
        plt.show(block=False)


from allennlp.predictors.predictor import Predictor
from pprint import pprint
predictor_fact = Predictor.from_path("https://s3-us-west-2.amazonaws.com/allennlp/models/decomposable-attention-elmo-2018.02.19.tar.gz")

predictor_ner = Predictor.from_path("https://s3-us-west-2.amazonaws.com/allennlp/models/ner-model-2018.12.18.tar.gz")

# predictor_pos = Predictor.from_path("https://s3-us-west-2.amazonaws.com/allennlp/models/biaffine-dependency-parser-ptb-2018.08.23.tar.gz")

#'words' 'tags'

print('predictor loaded...')

if __name__ == '__main__':
    predictor_ner.predict(
        sentence="Did Uriah honestly think he could beat The Legend of Zelda in under three hours?"
    )

    ans = predictor_ner.predict(
        sentence="Did Uriah honestly think he could beat The Legend of Zelda in under three hours?"
    )

    # ans = predictor_pos.predict(
    #     sentence="If I bring 10 dollars tomorrow, can you buy me lunch?"
    # )
    pprint(ans)


from typing import List, Optional

import httpx

from lnbits.core.services import create_invoice
from lnbits.core.views.api import api_payment
from lnbits.helpers import urlsafe_short_hash

# from lnbits.db import open_ext_db
from . import db
from .models import CreateConfiguration, StrikeConfiguration, StrikeForward

############### CONFIGURATIONS ##########################

async def create_configuration(
    data: CreateConfiguration, currency: str
) -> StrikeConfiguration:
    """Create a new Configuration"""
    id = urlsafe_short_hash()
    await db.execute(
        """
        INSERT INTO strike.configurations (
            id,
            lnbits_wallet,
            handle,
            description,
            api_key,
            currency
        )
        VALUES (?, ?, ?, ?, ?, ?)
        """,
        (id, data.lnbits_wallet, data.handle, data.description, data.api_key, currency),
    )

    config = await get_configuration(id)
    assert config, "Newly created configuration couldn't be retrieved"
    return config

async def get_configuration(id: str) -> StrikeConfiguration:
    row = await db.fetchone("SELECT * FROM strike.configurations WHERE id = ?", (id,))
    return StrikeConfiguration.from_row(row) if row else None

async def get_configuration_by_wallet(wallet: str) -> StrikeConfiguration:
    row = await db.fetchone(
        "SELECT * FROM strike.configurations WHERE lnbits_wallet = ?", (wallet,)
    )
    return StrikeConfiguration.from_row(row) if row else None

async def get_configurations(wallet_id: str) -> Optional[list]:
    """Return all StrikeConfiguration belonging assigned to the wallet_id"""
    rows = await db.fetchall(
        "SELECT * FROM strike.configurations WHERE lnbits_wallet = ?", (wallet_id,)
    )
    return [StrikeConfiguration(**row) for row in rows] if rows else None

async def delete_configuration(id: int) -> None:
    """Delete a StrikeConfiguration"""
    await db.execute("DELETE FROM strike.configurations WHERE id = ?", (id,))

############### FORWARDS ##########################

async def create_forward(data: StrikeForward) -> StrikeForward:
    """Create a new Forward"""
    id = urlsafe_short_hash()
    await db.execute(
        """
        INSERT INTO strike.forwards (
            id,
            lnbits_wallet,
            handle,
            message,
            currency,
            sats_original,
            msats_fee,
            amount
        )
        VALUES (?, ?, ?, ?, ?, ?, ?, ?)
        """,
        (
            id,
            data.lnbits_wallet,
            data.handle,
            data.message,
            data.currency,
            data.sats_original,
            data.msats_fee,
            data.amount,
        ),
    )

    config = await get_forward(id)
    assert config, "Newly created forward couldn't be retrieved"
    return config

async def update_forward(forward: StrikeForward) -> StrikeForward:
    id = forward.id
    params = forward.dict()
    del params["id"]
    q = ", ".join([f"{field[0]} = ?" for field in params.items()])
    await db.execute(
        f"UPDATE strike.forwards SET {q} WHERE id = ?", (*params.values(), id)
    )
    config = await get_forward(id)
    assert config, "Updated forward couldn't be retrieved"
    return config

async def get_forward(id: str) -> StrikeForward:
    row = await db.fetchone("SELECT * FROM strike.forwards WHERE id = ?", (id,))
    return StrikeForward.from_row(row) if row else None

async def get_forwards(wallet_id: str) -> Optional[list]:
    """Return all StrikeForwards belonging assigned to the wallet_id"""
    rows = await db.fetchall(
        "SELECT * FROM strike.forwards WHERE lnbits_wallet = ? ORDER BY timestamp DESC",
        (wallet_id,),
    )
    return [StrikeForward(**row) for row in rows] if rows else None


from multiprocessing import Queue, Process

from abc import ABCMeta

from dframe.pipeline.package import PackageProcessor

class Core(Process, PackageProcessor):
    """Processing unit.

    As Core is an abstract class, the actual logic to process the package must be implemented extending it and
    overriding the process_package method defined in dframe.pipeline.package.PackageProcessor, which Core extends from.
    The run method only deals with getting the package, delivering it to process_package and sending the result through
    the output pipe, and thus it should commonly remain untouched (not overrided)
    """

    __metaclass__ = ABCMeta

    def __init__(self, pipe_in, pipe_out):
        super(Core, self).__init__()
        self.queue = Queue()        # FIFO queue
        self.pipe_in = pipe_in      # The input channel. Package get into the core through this pipe
        self.pipe_out = pipe_out    # The output channel. The core send the result through this pipe

        # Child process that listens for incoming packages through pipe_in and adds them to the processing queue.
        # From the Core perspective, this is the producer of packages (the one that puts them in the processing queue)
        self.producer = PipeConsumer(self.pipe_in, self.queue)

    def start(self):
        # Start the producer process before starting this one
        self.producer.start()
        super(Core, self).start()

    def terminate(self):
        """Terminates the core's execution processes.

        This method calls the terminate method to the underlaying processes, which are not 'safe' and can lead to
        corrupted pipes and unprocessed packages. In order to safely stop the core, inject None (poison pill) through
        its input pipe. This will stop smoothly the core and will propagate the signal.
        """

        # Terminate the producer process and wait until it has completely finished
        self.producer.terminate()
        self.producer.join()
        # Terminate self process
        super(Core, self).terminate()

    def run(self):
        """Logic of the core is executed here in a different process.

        This is the consumer part of the architecture. Packages are caught from the processing queue, processed and
        sent to the next module through the output pipe.
        """

        while True:
            # Get the next package to process from the queue. Blocking if there is none
            package = self.queue.get()
            # If we receive None, propagate the signal through the pipe and break the infinite loop to stop
            # the process
            if package is None:
                self.pipe_out.send(package)
                break
            # Process the package
            self.process_package(package)
            # Send the result to the next block through the output pipe
            self.pipe_out.send(package)

class PipeConsumer(Process):
    """Process that is in charge of reading a pipe and putting the incoming packages in a queue"""

    def __init__(self, pipe, queue):
        super(PipeConsumer, self).__init__()
        self.pipe = pipe
        self.queue = queue

    def run(self):
        while True:
            try:
                # Wait for the other end of the pipe to send the package and add it to the processing queue
                package = self.pipe.recv()
                self.queue.put(package)
                # If we receive None, break the infinite loop so that the process dies
                if package is None:
                    break
            except EOFError:
                break


from pyspark.sql import SparkSession
from pyspark.sql.functions import explode
from pyspark.sql.functions import col
from pyspark.sql import SQLContext
from pyspark.sql.functions import size
import pyspark
import yaml

def load_yaml(path: str) -> dict:
    """
    Loading Yaml files from a giving path.
    :param path: A path to the Yaml file.
    :return:dict with Yaml's data.
    """
    with open(path, 'r') as f:
        yaml_file = yaml.load(f, Loader=yaml.FullLoader)
    return yaml_file

class SparkAlgoUtil:
    """This class define the configs and methods which required to execute the both Relation/Ways-base algorithm"""
    def __init__(self):
        try:
            self.spark = SparkSession.builder \
                .master('local') \
                .appName('myAppName') \
                .config("spark.sql.warehouse.dir", "file:///C:/temp") \
                .getOrCreate()
        except Exception as err:
            print("OS error: {0}".format(err))
        self.sc = self.spark.sparkContext
        self.sqlContext = SQLContext(self.sc)  # Using SQLContext to read parquet file
        self.sqlContext.setConf("spark.sql.parquet.binaryAsString", "true")  # Convert binary data in columns to Strings

    def nodes_df_creation(self, path: str) -> pyspark.sql.dataframe.DataFrame:
        """
        Generating a Pyspark-DF from a nodes-parquet file with only the nodes which tagged as amenity.
        :param path: Path to the parquet file.
        :return: Pyspark-DF which contains the identification number of all the nodes who are tagged only as amenity.
        """
        try:
            nodes_df = self.spark.read.parquet(path)
        except OSError:
            print('cannot open', path)
        nodes_df = nodes_df.select('id', 'tags').filter(size(col('tags')) > 0)
        nodes_df = nodes_df.select(col('id'), explode(col('tags')).name('exploded_tags'))\
            .filter(col('exploded_tags.key') == 'amenity')
        nodes_df = nodes_df.select("id", 'exploded_tags.value').withColumnRenamed('id', 'nodeId')\
            .withColumnRenamed('value', 'amenity_type')
        return nodes_df

    def relation_df_creation(self, path: str) -> pyspark.sql.dataframe.DataFrame:
        """
        Generating a Pyspark-DF from a relation-parquet file with only "street" relations.
        :param path: path to the parquet file.
        :return: Pyspark-DF which contains the streets name and ways Id.
        """
        try:
            relation_df = self.spark.read.parquet(path)
        except OSError:
            print('cannot open', path)
        relation_df = relation_df.withColumn("members_explode", explode(relation_df.members))\
            .filter(col('members_explode.role') == 'street')
        relation_df = relation_df.withColumn("street_name", explode(relation_df.tags))\
            .filter(col('street_name.key') == 'name').select('members_explode.id', 'street_name.value')\
            .withColumnRenamed('value', 'street_name').withColumnRenamed('id', 'way_id')
        return relation_df

    def ways_df_creation(self, path: str, algorithm_type: str) -> pyspark.sql.dataframe.DataFrame:
        """
        Generating a Pyspark-DF from a relation-parquet file with only "street" relations.
        :param algorithm_type:
        :param path: path to the parquet file.
        :return: Pyspark-DF which contains the streets name and ways Id.
        """
        try:
            ways_df = self.spark.read.parquet(path)
        except OSError:
            print('cannot open', path)
        if algorithm_type == 'relation':
            return ways_df.select('id', 'nodes').withColumnRenamed('id', 'way_id')
        else:  # Type of algorithm is 'way'
            ways_df = ways_df.select('tags', 'nodes').withColumn("tags_explode", explode(ways_df.tags))\
                .filter(col("tags_explode.key") == "name").withColumn("tags_explode_for_residential", explode(ways_df.tags))
            ways_df = ways_df.filter(col("tags_explode_for_residential.value") == "residential")\
                .select(col('tags_explode'), explode(col('nodes')).name('nodes_explode'))
            ways_df = ways_df.select(col('tags_explode.value'), col('nodes_explode.nodeId'))\
                .withColumnRenamed('value', 'street_name')
        return ways_df


import sys

import pygame

from . import screen, game, images, quit

class Status(object):
    """
    Status data and display
    """

    DIGITBG = pygame.Surface((16, 32))
    DIGITBG = DIGITBG.convert()
    DIGITBG.fill((0, 0, 0))

    Y = 432
    X_PARTS = 128
    X_KEYS = 256
    X_BULLETS = 384
    X_LEVEL = 512

    def __init__(self, level):
        self.level = level
        self.digits = game.images.get_digits()
        # Wczytujemy obrazki
        self.images = {
            'parts':   game.images.get_icon(images.S_PARTS),
            'keys':    game.images.get_icon(images.S_KEYS),
            'bullets': game.images.get_icon(images.S_BULLETS),
            'level': game.images.get_icon(images.S_LEVEL)
        }
        self.refresh()
        self.clear()

    def refresh(self):
        screen.blit(self.images['parts'], pygame.Rect(self.X_PARTS - 32, self.Y, 32, 32))
        screen.blit(self.images['keys'], pygame.Rect(self.X_KEYS - 32, self.Y, 32, 32))
        screen.blit(self.images['bullets'], pygame.Rect(self.X_BULLETS - 32, self.Y, 32, 32))
        screen.blit(self.images['level'], pygame.Rect(self.X_LEVEL - 36, self.Y, 32, 32))

    def clear(self):
        self.keys = 0
        self.parts = 0
        self.bullets = 0

    def printnum(self, num, pos, dig):
        for i in range(dig-1, -1, -1):
            n = num % 10
            num = num // 10
            rect = pygame.Rect(pos, (16, 32)).move(i*16, 0)
            screen.blit(self.DIGITBG, rect)
            screen.blit(self.digits[n], rect)

    def update(self):
        scrclip = screen.get_clip()
        screen.set_clip(screen.get_rect())
        self.printnum(self.parts, (self.X_PARTS, self.Y), 2)
        self.printnum(self.keys, (self.X_KEYS, self.Y), 2)
        self.printnum(self.bullets, (self.X_BULLETS, self.Y), 2)
        self.printnum(self.level+1, (self.X_LEVEL, self.Y), 2)
        screen.set_clip(scrclip)

    def select_level(self):
        """
        Manually select level.
        """
        screen.set_clip(screen.get_rect())
        rect = pygame.Rect((self.X_LEVEL, self.Y), (16, 32))
        screen.blit(self.DIGITBG, rect)
        screen.blit(self.DIGITBG, rect.move(16, 0))
        pygame.display.flip()

        digits = []

        # Event loop to enter time
        while True:
            event = pygame.event.wait()
            if event.type == pygame.QUIT:
                quit()
            elif event.type == pygame.KEYDOWN:
                if pygame.K_0 <= event.key <= pygame.K_9:
                    n = event.key - pygame.K_0
                    screen.blit(self.digits[n], rect.move(16 * len(digits), 0))
                    pygame.display.flip()
                    digits = [10*i for i in digits]
                    digits.append(n)
            pygame.event.pump()
            if len(digits) == 2:
                break

        return sum(digits) - 1


import os
import numpy as np
import ROOT
import pickle
from .sample import Sample
from tqdm import tqdm

class Fitter:
    def __init__(self, mode, ES_tool=None, shift='None',
                 save_table=False, redo_fit=True):
        self.mode = mode
        self.ES_tool = ES_tool
        self.shift = shift
        self.save_table = save_table
        self.redo_fit = redo_fit

        # load in the SVfit dependencies...
        if (mode == 'SVfit'):
            SV_dir = "TauAnalysis/ClassicSVfit/src/"
            SV_files = ["SVfitIntegratorMarkovChain","ClassicSVfitIntegrand",
                        "ClassicSVfit", "svFitAuxFunctions", "MeasuredTauLepton",
                        "svFitHistogramAdapter"]
            ROOT.gInterpreter.ProcessLine(".include .")
            for SV_file in SV_files:
                path = "{0}{1}.cc++".format(SV_dir, SV_file)
                ROOT.gInterpreter.ProcessLine(".L {0}".format(path))

        # ...or load in the FastMTT dependencies
        elif (mode == 'FastMTT'):
            for baseName in ['../SVFit/MeasuredTauLepton',
                             '../SVFit/svFitAuxFunctions'
                             ,'../SVFit/FastMTT'] :
                if os.path.isfile("{0:s}_cc.so".format(baseName)) :
                    ROOT.gInterpreter.ProcessLine(".L {0:s}_cc.so"
                                                  .format(baseName))
                else :
                    ROOT.gInterpreter.ProcessLine(".L {0:s}.cc++"
                                                  .format(baseName))
        # otherwise, throw error
        else:
            print("ERROR: initializing fitter with invalid mode '{0}'"
                  .format(mode))

    def fit(self, s):

        # grab event info
        run, evt, lumi = s.events.array('run'), s.events.array('evt'), s.events.array('lumi')

        # grab MET info
        met, metphi = s.events.array('met'), s.events.array('metphi')
        measuredMETx, measuredMETy = met*np.cos(metphi), met*np.sin(metphi)
        covMET_00, covMET_01 = s.events.array('metcov00'), s.events.array('metcov01')
        covMET_10, covMET_11 = s.events.array('metcov10'), s.events.array('metcov11')

        # grab the lepton arrays
        ele_mass, muo_mass = 0.511*10**-3, 0.105
        pt_1, pt_2   = s.events.array('pt_1'),  s.events.array('pt_2')
        eta_1, eta_2 = s.events.array('eta_1'), s.events.array('eta_2')
        phi_1, phi_2 = s.events.array('phi_1'), s.events.array('phi_2')

        # grab the tau arrays
        pt_3,  pt_4  = s.events.array('pt_3'),  s.events.array('pt_4')
        eta_3, eta_4 = s.events.array('eta_3'), s.events.array('eta_4')
        phi_3, phi_4 = s.events.array('phi_3'), s.events.array('phi_4')
        m_3,   m_4   = s.events.array('m_3'),   s.events.array('m_4')
        dm_3,  dm_4  = s.events.array('decayMode_3'),  s.events.array('decayMode_4')
        match_3, match_4 = s.events.array('gen_match_3'), s.events.array('gen_match_4')

        # grab original mass fit
        m_sv = s.events.array('m_sv')
        progress_bar = tqdm(np.arange(s.n_entries)[s.mask])
        for i in progress_bar:

            # attempt to find value in lookup table
            found_masses = False
            if (self.mode == "SVfit"):
                tag = str(run[i]) + str(evt[i]) + str(lumi[i])
                try:
                    masses = s.lookup_table[tag]
                    s.mtt_fit[i] = masses['mtt_fit']
                    s.mA[i] = masses['mA']
                    s.mA_c[i] = masses['mA_c']
                    found_masses = True
                except:
                    s.n_recalculated += 1

            if (s.n_recalculated % 2500 == 0 and
                s.n_recalculated >= 2500): s.write_lookup_table()

            # if FastMTT, em channel is good-to-go
            if (s.tt[i] == 'em' and self.mode == 'FastMTT'):
                s.mtt_fit[i] = m_sv[i]
                continue

            # grab the ll 4-vectors
            l1, l2 = ROOT.TLorentzVector(), ROOT.TLorentzVector()
            if (s.ll[i] == 'ee'):
                l1.SetPtEtaPhiM(pt_1[i], eta_1[i], phi_1[i], ele_mass)
                l2.SetPtEtaPhiM(pt_2[i], eta_2[i], phi_2[i], ele_mass)
            elif (s.ll[i] == 'mm'):
                l1.SetPtEtaPhiM(pt_1[i], eta_1[i], phi_1[i], muo_mass)
                l2.SetPtEtaPhiM(pt_2[i], eta_2[i], phi_2[i], muo_mass)

            # build tau 4-vectors
            t1, t2 = ROOT.TLorentzVector(), ROOT.TLorentzVector()
            t1.SetPtEtaPhiM(pt_3[i], eta_3[i], phi_3[i], m_3[i])
            t2.SetPtEtaPhiM(pt_4[i], eta_4[i], phi_4[i], m_4[i])

            # apply tau ES corrections
            if (s.tt[i] != 'em'):
                if (s.tt[i] == 'tt'):
                    t1 *= self.ES_tool.getTES(pt_3[i], dm_3[i], match_3[i])
                t2 *= self.ES_tool.getTES(pt_4[i], dm_4[i], match_4[i])

            # store raw 4l mass
            s.m4l[i]  = (l1 + l2 + t1 + t2).M()
            if ((l1+l2+t1+t2).M() < 100):
                print("eh!!", s.m4l[i], s.ll[i]+s.tt[i], l1, l2, t1, t2)

            # continue past the mass fit if we already found one
            if (found_masses): continue

            # build ROOT objects
            covMET = ROOT.TMatrixD(2,2)
            VectorOfTaus = ROOT.std.vector('MeasuredTauLepton')
            tau_pair = VectorOfTaus()
            covMET[0][0] = covMET_00[i]
            covMET[0][1] = covMET_01[i]
            covMET[1][0] = covMET_10[i]
            covMET[1][1] = covMET_11[i]
            ele_decay = ROOT.MeasuredTauLepton.kTauToElecDecay
            mu_decay  = ROOT.MeasuredTauLepton.kTauToMuDecay
            had_decay = ROOT.MeasuredTauLepton.kTauToHadDecay

            if (s.tt[i] == 'et' or s.tt[i] == 'em'):
                tau_pair.push_back(ROOT.MeasuredTauLepton(ele_decay, t1.Pt(),
                                                          t1.Eta(), t1.Phi(),
                                                          ele_mass))
            elif (s.tt[i] == 'mt'):
                tau_pair.push_back(ROOT.MeasuredTauLepton(mu_decay, t1.Pt(),
                                                          t1.Eta(), t1.Phi(),
                                                          muo_mass))
            elif (s.tt[i] == 'tt'):
                tau_pair.push_back(ROOT.MeasuredTauLepton(had_decay, t1.Pt(),
                                                          t1.Eta(), t1.Phi(),
                                                          t1.M()))
            if (s.tt[i] != 'em'):
                tau_pair.push_back(ROOT.MeasuredTauLepton(had_decay, t2.Pt(),
                                                          t2.Eta(), t2.Phi(),
                                                          t2.M()))
            elif (s.tt[i] == 'em'):
                tau_pair.push_back(ROOT.MeasuredTauLepton(mu_decay, t2.Pt(),
                                                          t2.Eta(), t2.Phi(),
                                                          muo_mass))

            # run SVfit algorithm
            if (self.mode == 'SVfit'):
                svFitAlgo = ROOT.ClassicSVfit(0)
                svFitAlgo.addLogM_fixed(True, 6.)
                svFitAlgo.integrate(tau_pair, measuredMETx[i], measuredMETy[i], covMET)
                mass = svFitAlgo.getHistogramAdapter().getMass()
                s.mtt_fit[i] = mass

                # calculate A mass
                tt = ROOT.TLorentzVector()
                tt.SetPtEtaPhiM(svFitAlgo.getHistogramAdapter().getPt(),
                                svFitAlgo.getHistogramAdapter().getEta(),
                                svFitAlgo.getHistogramAdapter().getPhi(),
                                svFitAlgo.getHistogramAdapter().getMass())
                s.mA[i]   = (l1 + l2 + tt).M()

                # perform a constrained di-tau mass fit
                massConstraint = 125
                svFitAlgo.setDiTauMassConstraint(massConstraint)
                svFitAlgo.integrate(tau_pair, measuredMETx[i], measuredMETy[i], covMET)
                mass_c = svFitAlgo.getHistogramAdapter().getMass()

                # calculate constrained A mass
                tt_c = ROOT.TLorentzVector()
                tt_c.SetPtEtaPhiM(svFitAlgo.getHistogramAdapter().getPt(),
                                  svFitAlgo.getHistogramAdapter().getEta(),
                                  svFitAlgo.getHistogramAdapter().getPhi(),
                                  svFitAlgo.getHistogramAdapter().getMass())
                s.mA_c[i] = (l1 + l2 + tt_c).M()

                tag = str(run[i]) + str(evt[i]) + str(lumi[i])
                s.lookup_table[tag] = {'mtt_fit':s.mtt_fit[i],
                                       'mA':s.mA[i], 'mA_c':s.mA_c[i]}

            # run FastMTT algorithm
            elif (self.mode == 'FastMTT'):
                FMTT = ROOT.FastMTT()
                FMTT.run(tau_pair, measuredMETx[i], measuredMETy[i], covMET)
                ttP4 = FMTT.getBestP4()
                s.mtt_fit[i] = ttP4.M()

        # in case we've added to the lookup table, write it out
        if (self.mode == 'SVfit'): s.write_lookup_table()


from django.urls import path

from . import views

urlpatterns = [

        path("<int:id>",views.index,name="index"),
        path("home",views.home,name="home"),
        path("list/<int:id>",views.list,name="list"),
        path("create",views.create,name="create"),
        path("edit/<int:id>",views.edit,name="edit"),
        path("view",views.view,name="view"),

]