from itertools import combinations
from scipy.optimize import fsolve
from copy import copy
from pdb import set_trace
INFT = float(10**10)
class Bound(object):
    def __init__(self,x,y,r):
        self.x , self.y , self.r = x , y , r
    def fit(self,another_bound):
        if another_bound.x == INFT :
            return self.x + self.r <= 1.0
        elif another_bound.x == - INFT :
            return self.x - self.r >= 0.0
        elif another_bound.y == INFT :
            return self.y + self.r <= 1.0
        elif another_bound.y == - INFT :
            return self.y - self.r >= 0.0
        else:
            return (self.r + another_bound.r)**2 <=  (self.x - another_bound.x)**2 + (self.y - another_bound.y)**2
        # return (self.r + another_bound.r)**2 <=  (self.x - another_bound.x)**2 + (self.y - another_bound.y)**2
    def fit_all(self,bounds):
        for i in bounds:
            if not self.fit(i):
                return False
        return True
# bound( x , y , r )
bound_set0 = [
    Bound( -INFT , 0.0 , INFT ),
    Bound( INFT , 0.0 , INFT ),
    Bound(  0.0, -INFT, INFT ),
    Bound(  0.0, INFT, INFT  ),
    Bound( 0.5, 0.5, 0)
    ]

def find(bound_set):
    new_bound_set = copy(bound_set)
    max_r = 0
    for selected_3_bound in list(combinations(bound_set, 3)):
        new_bound = Bound(solve(selected_3_bound)[0],solve(selected_3_bound)[1],solve(selected_3_bound)[2])
        # set_trace()
        if new_bound.fit_all(new_bound_set) and new_bound.r > max_r:
            max_r = new_bound.r
            max_bound = new_bound
    new_bound_set.append(max_bound)
    return new_bound_set
def solve(three_bounds):
    def fi(solution,bound):
        if bound.x == INFT :
            return solution[0] + solution[2] - 1.0
        elif bound.x == - INFT :
            return solution[0] - solution[2] - 0.0
        elif bound.y == INFT :
            return solution[1] + solution[2] - 1.0
        elif bound.y == - INFT :
            return solution[1] - solution[2] - 0.0
        else:
            return -(solution[2] + bound.r)**2 + (solution[0] - bound.x)**2 + (solution[1] - bound.y)**2
        # return -(solution[2] + bound.r)**2 + (solution[0] - bound.x)**2 + (solution[1] - bound.y)**2
    f = lambda x :[
            fi(x,three_bounds[0]),
            fi(x,three_bounds[1]),
            fi(x,three_bounds[2])
        ]
    return fsolve(f,[0.5,0.5,0.0])
# test:
for x in find(find(find(find(find(find(bound_set0))))))[len(bound_set0):]:
    print(x.x)
    print(x.y)
    print(x.r)
    print('---')


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 29 02:35:41 2017

@author: samara
"""

from tkinter import *
import InterfaceEstacionamentoV
import Estacionamento

class InterfaceA():
    def __init__(self):
        self.interface = InterfaceEstacionamentoV.InterfaceV()
        self.interface.janela.title("Alterar Dados do Estacionamento")

        self.interface.lblNome["text"]= "Nome: (Não pode ser alterado)"
        self.nomeAnterior = self.interface.txtNome.get()

        self.bntAlt = Button(self.interface.cont5, text="Alterar", bg="navy", command=self.alterarDadosEstacionamento).pack(side=LEFT, padx=20)
        self.lblmsg = Label(self.interface.cont6, text="", bg="navy", fg="white")
        self.lblmsg.pack()

        self.bntSair = Button(self.cont5, text="Sair", bg="navy", command=self.sairDaInterface).pack(side=RIGHT)

    def alterarDadosEstacionamento(self):
        qtdeVagasDispCarro = self.interface.txtQtdeVagasDispCarro.get()
        qtdeVagasDispMoto = self.interface.txtQtdeVagasDispMoto.get()

        taxa15minC = self.interface.txtTaxa15minC.get()
        taxaDiariaC = self.interface.txtTaxaDiariaC.get()
        taxaMensalC = self.interface.txtTaxaMensalC.get()

        taxa15minM = self.interface.txtTaxa15minM.get()
        taxaDiariaM = self.interface.txtTaxaDiariaM.get()
        taxaMensalM = self.interface.txtTaxaMensalM.get()

        estacionamento = Estacionamento.Estacionamento(self.nomeAnterior, qtdeVagasDispCarro, qtdeVagasDispMoto)
        retorno = estacionamento.alterarDadosEstacionamento()
        estacionamento.alterarTaxasEstacionamento(self.nomeAnterior, taxa15minC, taxaDiariaC, taxaMensalC, taxa15minM, taxaDiariaM, taxaMensalM)

        self.lblmsg["text"] = retorno

    def sairDaInterface(self):
        self.janela.destroy()


import cvxopt
import cvxopt.solvers

from cvxopt.solvers import lp
from numpy import array

cvxopt.solvers.options['show_progress'] = False  # disable cvxopt output

try:
    import cvxopt.glpk
    GLPK_IF_AVAILABLE = 'glpk'
    # GLPK is the fastest LP solver I could find so far:
    # <https://scaron.info/blog/linear-programming-in-python-with-cvxopt.html>
    # ... however, it's verbose by default, so tell it to STFU:
    cvxopt.solvers.options['glpk'] = {'msg_lev': 'GLP_MSG_OFF'}  # cvxopt 1.1.8
    cvxopt.solvers.options['msg_lev'] = 'GLP_MSG_OFF'  # cvxopt 1.1.7
    cvxopt.solvers.options['LPX_K_MSGLEV'] = 0  # previous versions
except ImportError:
    # issue a warning as GLPK is the best LP solver in practice
    print("CVXOPT import: GLPK solver not found")
    GLPK_IF_AVAILABLE = None

def cvxopt_matrix(M):
    if isinstance(M, cvxopt.matrix):
        return M
    return cvxopt.matrix(M)

def cvxopt_solve_lp(c, G, h, A=None, b=None, solver=GLPK_IF_AVAILABLE):
    """
    Solve a linear program defined by:

        minimize
            c.T * x

        subject to
            G * x <= h
            A * x == b

    using the LP solver from `CVXOPT <http://cvxopt.org/>`_.

    Parameters
    ----------
    c : array, shape=(n,)
        Linear-cost vector.
    G : array, shape=(m, n)
        Linear inequality constraint matrix.
    h : array, shape=(m,)
        Linear inequality constraint vector.
    A : array, shape=(meq, n), optional
        Linear equality constraint matrix.
    b : array, shape=(meq,), optional
        Linear equality constraint vector.
    solver : string, optional
        Solver to use, default is GLPK if available

    Returns
    -------
    x : array, shape=(n,)
        Optimal (primal) solution of the LP, if one exists.

    Raises
    ------
    ValueError
        If the LP is not feasible.
    """
    args = [cvxopt_matrix(c), cvxopt_matrix(G), cvxopt_matrix(h)]
    if A is not None:
        args.extend([cvxopt_matrix(A), cvxopt_matrix(b)])
    sol = lp(*args, solver=solver)
    if 'optimal' not in sol['status']:
        raise ValueError("LP optimum not found: %s" % sol['status'])
    return array(sol['x']).reshape((array(c).shape[0],))


from django.urls import path
from . import views

app_name = "core"
urlpatterns = [
    path('author/', views.AuthorList.as_view(), name='list-author'),
    path('author/<int:pk>/', views.AuthorDetail.as_view(), name='detail-author'),
    path('book/', views.BookList.as_view(), name='list-book'),
    path('book/<int:pk>/', views.BookDetail.as_view(), name='detail-book'),
]


import pandas as pd

def build(gps, game_id):
    players = []

    for i in gps.PlayerID.unique():
        counter = 0
        prev_a = 0.0

        for j in range(1, 3):
            for k in gps[(gps.PlayerID == i) & (gps.Half == j)].FrameID.values:  # first half second half
                ax = list(gps[(gps.PlayerID == i) & (gps.FrameID == k) & (gps.Half == j)].AccelX)[0]
                ay = list(gps[(gps.PlayerID == i) & (gps.FrameID == k) & (gps.Half == j)].AccelY)[0]
                az = list(gps[(gps.PlayerID == i) & (gps.FrameID == k) & (gps.Half == j)].AccelZ)[0]
                a = ax ** 2 + ay ** 2 + az ** 2

                if (prev_a > 5.0 and a < 5.0) or (prev_a < 5.0 and a > 5.0):
                    counter += 1
                prev_a = a

        players.append({'GameID': game_id, 'PlayerID': i, 'IntenseEvents': counter})

    return players

if __name__ == '__main__':
    df = pd.read_csv('gps.csv')
    for i in df.GameID.unique():
        if i > 3:
            print('game{0}.csv'.format(i))
            game_df = df[df.GameID == i]
            count_df = pd.DataFrame(build(game_df, i))
            count_df.to_csv('game{0}.csv'.format(i))


from PIL import Image
import numpy as np
import cv2

img = Image.open('back_img.jpg')

size = img.size

x_length = size[0]
print('x_length:', x_length)

y_length = size[1]
print('y_length:', y_length)

im_num = np.array(img)

img_blur = cv2.GaussianBlur(im_num, (5, 5), 0)

img_gray = cv2.cvtColor(img_blur, cv2.COLOR_BGR2GRAY)

img_canny = cv2.Canny(img_gray, 100, 200)

cv2.imwrite('back_img_func.jpg', img_canny)

cv2.imshow('image', img_canny)
cv2.waitKey(0)


"""First migration

Revision ID: 1e99703f8998
Revises:
Create Date: 2022-03-30 17:34:52.872031

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '1e99703f8998'
down_revision = None
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('resources',
    sa.Column('resource_uuid', sa.String(), nullable=False),
    sa.Column('name', sa.String(), nullable=True),
    sa.Column('description', sa.String(), nullable=True),
    sa.Column('created', sa.DateTime(), nullable=True),
    sa.Column('updated', sa.DateTime(), nullable=True),
    sa.Column('state', sa.String(), nullable=True),
    sa.PrimaryKeyConstraint('resource_uuid')
    )
    op.create_index(op.f('ix_resources_resource_uuid'), 'resources', ['resource_uuid'], unique=False)
    op.create_table('entries',
    sa.Column('resource_uuid', sa.String(), nullable=True),
    sa.Column('entry_uuid', sa.String(), nullable=False),
    sa.Column('private_body', sa.String(), nullable=True),
    sa.Column('public_body', sa.String(), nullable=True),
    sa.Column('created', sa.DateTime(), nullable=True),
    sa.Column('updated', sa.DateTime(), nullable=True),
    sa.Column('state', sa.String(), nullable=True),
    sa.ForeignKeyConstraint(['resource_uuid'], ['resources.resource_uuid'], ),
    sa.PrimaryKeyConstraint('entry_uuid')
    )
    op.create_index(op.f('ix_entries_entry_uuid'), 'entries', ['entry_uuid'], unique=False)
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_entries_entry_uuid'), table_name='entries')
    op.drop_table('entries')
    op.drop_index(op.f('ix_resources_resource_uuid'), table_name='resources')
    op.drop_table('resources')
    # ### end Alembic commands ###


import time
from threading import Timer
import xmlrpc.client
from .edit import Edit
from .utils import firmwareWarning
import json
import os
import base64

class Session(object):
    """
    Session object
    """

    def __init__(self, sessionURL, mainAPI, autoHeartbeat=True, autoHeartbeatInterval=10):
        self.url = sessionURL
        self.mainAPI = mainAPI
        self.defaultAutoHeartbeatInterval = autoHeartbeatInterval
        self.rpc = xmlrpc.client.ServerProxy(self.url)
        self.connected = True
        self._edit = None
        if autoHeartbeat:
            self.rpc.heartbeat(autoHeartbeatInterval)
            self.autoHeartbeatInterval = autoHeartbeatInterval
            self.autoHeartbeatTimer = Timer(autoHeartbeatInterval - 1, self.doAutoHeartbeat)
            self.autoHeartbeatTimer.start()
        else:
            self.rpc.heartbeat(300)

    def __del__(self):
        self.cancelSession()

    @property
    def OperatingMode(self):
        """
        Get the current operation mode for the session.

        :return: (int)
                 0: run mode
                 1: edit mode
        """
        result = int(self.mainAPI.getParameter("OperatingMode"))
        return result

    @property
    def edit(self) -> Edit:
        """
        Requesting an Edit object with this property. If the edit mode is False at the moment,
        the edit mode will be activated with this request with the function setOperationMode(1).

        :return: Edit object
        """
        if not self.OperatingMode:
            return self.setOperatingMode(mode=1)
        else:
            self._edit = Edit(editURL=self.url + 'edit/',
                              sessionAPI=self,
                              mainAPI=self.mainAPI)
            return self._edit

    def startEdit(self) -> Edit:
        """
        Starting the edit mode and requesting an Edit object.

        :return:
        """
        self.rpc.setOperatingMode(1)
        self._edit = Edit(editURL=self.url + 'edit/',
                          sessionAPI=self,
                          mainAPI=self.mainAPI)
        return self._edit

    def stopEdit(self) -> None:
        """
        Stopping the edit mode.

        :return: None
        """
        self.rpc.setOperatingMode(0)
        self._edit = None

    def heartbeat(self, heartbeatInterval: int) -> int:
        """
        Extend the live time of edit-session If the given value is outside the range of "SessionTimeout",
        the saved default timeout will be used.

        :param heartbeatInterval: (int) requested timeout-interval till next heartbeat, in seconds
        :return: (int) the used timeout-interval, in seconds
        """
        result = self.rpc.heartbeat(heartbeatInterval)
        return result

    def doAutoHeartbeat(self) -> None:
        """
        Auto Heartbeat Timer for automatic extending the live time of edit-session.
        If the given value is outside the range of "SessionTimeout", the saved default timeout will be used.

        :return: None
        """
        newHeartbeatInterval = self.heartbeat(self.autoHeartbeatInterval)
        self.autoHeartbeatInterval = newHeartbeatInterval
        # schedule event a little ahead of time
        self.autoHeartbeatTimer = Timer(self.autoHeartbeatInterval - 1, self.doAutoHeartbeat)
        self.autoHeartbeatTimer.start()

    def cancelSession(self) -> None:
        """
        Explicit stopping this session If an application is still in edit-mode, it will implicit do the same
        as "stopEditingApplication". If an import or export is still being processed, the session is kept alive
        until the import/export has finished, although the method returns immediately.

        :return: None
        """
        if self.autoHeartbeatTimer:
            self.autoHeartbeatTimer.cancel()
            self.autoHeartbeatTimer.join()
            self.autoHeartbeatTimer = None
        if self.connected:
            self.rpc.cancelSession()
            self.connected = False

    def exportConfig(self) -> bytearray:
        """
        Exports the whole configuration of the sensor-device and stores it at the desired path.

        :return: (bytearray) configuration as one data-blob :binary/base64
        """
        # increase heartbeat interval which will prevent a closed session after the "long" export progress
        self.heartbeat(heartbeatInterval=30)
        config = self.rpc.exportConfig()
        config_bytes = bytearray()
        config_bytes.extend(map(ord, str(config)))
        while self.getExportProgress() < 1.0:
            time.sleep(1)
        self.cleanupExport()
        self.mainAPI.waitForConfigurationDone()
        return config_bytes

    def importConfig(self, config: str, global_settings=True, network_settings=False, applications=True) -> None:
        """
        Import whole configuration, with the option to skip specific parts.

        :param config:              (str) The config file (*.o2d5xxcfg) as a Binary/base64 data
        :param global_settings:     (bool) Include Globale-Configuration (Name, Description, Location, ...)
        :param network_settings:    (bool) Include Network-Configuration (IP, DHCP, ...)
        :param applications:        (bool) Include All Application-Configurations
        :return:                    None
        """
        # This is required due to the long import progress which may take longer than 10 seconds (default)
        self.heartbeat(heartbeatInterval=30)
        if global_settings:
            self.rpc.importConfig(config, 0x0001)
        if network_settings:
            self.rpc.importConfig(config, 0x0002)
        if applications:
            self.rpc.importConfig(config, 0x0010)
        while self.getImportProgress() < 1.0:
            time.sleep(1)
        self.mainAPI.waitForConfigurationDone()

    def exportApplication(self, applicationIndex: int) -> bytearray:
        """
        Exports one application-config.

        :param applicationIndex: (int) application index
        :return: None
        """
        config = self.rpc.exportApplication(applicationIndex)
        application_bytes = bytearray()
        application_bytes.extend(map(ord, str(config)))
        while self.getExportProgress() < 1.0:
            time.sleep(1)
        else:
            self.cleanupExport()
        return application_bytes

    def importApplication(self, application: str) -> int:
        """
        Imports an application-config and creates a new application with it.

        :param application: (str) application-config as one-data-blob: binary/base64
        :return: (int) index of new application in list
        """
        if not self.OperatingMode:
            self.setOperatingMode(mode=1)
            index = int(self.rpc.importApplication(application))
            while self.getImportProgress() < 1.0:
                time.sleep(1)
            self.setOperatingMode(mode=0)
        else:
            index = int(self.rpc.importApplication(application))
            while self.getImportProgress() < 1.0:
                time.sleep(1)
        self.mainAPI.waitForConfigurationDone()
        return index

    def getImportProgress(self) -> float:
        """
        Get the progress of the asynchronous configuration import (yields 1.0 when the last import has finished).
        Returns xmlrpc errors occurring during import.

        :return: (float) progress (0.0 to 1.0)
        """
        try:
            result = self.rpc.getImportProgress()
            return result
        except xmlrpc.client.Fault as fault:
            if fault.faultCode == 101107:
                return 1.0

    def getExportProgress(self) -> float:
        """
        Returns the progress of the ongoing export (configuration or application). After the export is done
        this method returns 1.0 until the cleanupExport() is called.

        :return: (float) progress (0.0 to 1.0)
        """
        try:
            result = self.rpc.getExportProgress()
            return result
        except xmlrpc.client.Fault as fault:
            if fault.faultCode == 101110:
                return 1.0
        finally:
            self.cleanupExport()

    def cleanupExport(self) -> None:
        """
        Removes the exported configuration/application binary archive file from the device tmpfs.
        Shall be called after the file is fully downloaded by the user with HTTP GET request.

        :return: None
        """
        self.rpc.cleanupExport()

    def getApplicationDetails(self, applicationIndex: [int, str]) -> dict:
        """
        The method returns details about the application line ApplicationType,
        TemplateInfo and Models with Type and Name.

        :param applicationIndex: (int) application Index
        :return: (dict) json-string containing application parameters, models and image settings
        """
        result = json.loads(self.rpc.getApplicationDetails(applicationIndex))
        return result

    def resetStatistics(self) -> None:
        """
        Resets the statistic data of current active application.

        :return: None
        """
        self.rpc.resetStatistics()
        self.mainAPI.waitForConfigurationDone()

    @staticmethod
    def writeApplicationConfigFile(applicationName: str, data: bytearray) -> None:
        """
        Stores the application data as an o2d5xxapp-file in the desired path.

        :param applicationName: (str) application name as str
        :param data: (bytearray) application data
        :return: None
        """
        extension = ".o2d5xxapp"
        filename, file_extension = os.path.splitext(applicationName)
        if not file_extension == extension:
            applicationName = filename + extension
        with open(applicationName, "wb") as f:
            f.write(data)

    @staticmethod
    def writeConfigFile(configName: str, data: bytearray) -> None:
        """
        Stores the config data as an o2d5xxcfg-file in the desired path.

        :param configName: (str) application file path as str
        :param data: (bytearray) application data
        :return: None
        """
        extension = ".o2d5xxcfg"
        filename, file_extension = os.path.splitext(configName)
        if not file_extension == extension:
            configName = filename + extension
        with open(configName, "wb") as f:
            f.write(data)

    def readApplicationConfigFile(self, applicationFile: str) -> str:
        """
        Read and decode an application-config file.

        :param applicationFile: (str) application config file path
        :return: (str) application data
        """
        result = self.readConfigFile(configFile=applicationFile)
        return result

    @firmwareWarning
    def readConfigFile(self, configFile: str) -> str:
        """
        Read and decode a device-config file.

        :param configFile: (str) config file path
        :return: (str) config data
        """
        if isinstance(configFile, str):
            if os.path.exists(os.path.dirname(configFile)):
                with open(configFile, "rb") as f:
                    encodedZip = base64.b64encode(f.read())
                    decoded = encodedZip.decode()
                    return decoded
            else:
                raise FileExistsError("File {} does not exist!".format(configFile))

    def setOperatingMode(self, mode) -> [None, Edit]:
        """
        Changes the operation mode of the device. Setting this to "edit" will enable the "EditMode"-object on RPC.

        :param mode: 1 digit
                     0: run mode
                     1: edit mode
                     2: simulation mode (Not implemented!)
        :return: None or Edit object
        """
        if mode == 0:  # stop edit mode
            self.stopEdit()
        elif mode == 1:  # start edit mode
            return self.startEdit()
        else:
            raise ValueError("Invalid operating mode")

    def __getattr__(self, name):
        # Forward otherwise undefined method calls to XMLRPC proxy
        return getattr(self.rpc, name)


pkgname = "tig"
pkgver = "2.5.8"
pkgrel = 1
build_style = "gnu_configure"
make_cmd = "gmake"
make_dir = "."
make_install_args = ["install-doc-man"]
hostmakedepends = ["gmake", "automake", "asciidoc", "xmlto", "pkgconf"]
makedepends = ["ncurses-devel"]
depends = ["git"]
pkgdesc = "Text-mode interface for git"
maintainer = "Wesley Moore <wes@wezm.net>"
license = "GPL-2.0-or-later"
url = "https://github.com/jonas/tig"
source = f"{url}/releases/download/{pkgname}-{pkgver}/{pkgname}-{pkgver}.tar.gz"
sha256 = "b70e0a42aed74a4a3990ccfe35262305917175e3164330c0889bd70580406391"
# test suite tries to access /dev/tty which fails
options = ["!check"]

def post_install(self):
    self.install_completion("contrib/tig-completion.bash", "bash")
    self.install_completion("contrib/tig-completion.zsh", "zsh")


#! /Users/tianranmao/Projects/so1.0/venv/bin/python
import requests
from bs4 import BeautifulSoup
import datetime
import pytz
import time
import re
import os

# --------------------------------------------------------------------
#                          Main Function
# --------------------------------------------------------------------

def scrape_web(url):
    try:
        source = requests.get(url, timeout=20).text
    except Exception as e:
        print(e)
        return None

    soup = BeautifulSoup(source, 'lxml')

    paragraph = soup.find('p').text

    print(paragraph)
    print()
    return paragraph

if __name__ == "__main__":

    week_day_dict = {
    0 : 'Monday',
    1 : 'Tuesday',
    2 : 'Wednesday',
    3 : 'Thursday',
    4 : 'Friday',
    5 : 'Saturday',
    6 : 'Sunday'
  }

    #url_510300_jan = "http://yunhq.sse.com.cn:32041//v1/sho/list/tstyle/510300_01?callback=jQuery112402078220234177265_1577088059316&select=contractid%2Clast%2Cchg_rate%2Cpresetpx%2Cexepx&order=contractid%2Cexepx%2Case&_=1577088059323"

    #url_510300_feb = "http://yunhq.sse.com.cn:32041//v1/sho/list/tstyle/510300_02?callback=jQuery112402078220234177265_1577088059316&select=contractid%2Clast%2Cchg_rate%2Cpresetpx%2Cexepx&order=contractid%2Cexepx%2Case&_=1577088059351"

    url_510300_mar = "http://yunhq.sse.com.cn:32041//v1/sho/list/tstyle/510300_03?callback=jQuery112402078220234177265_1577088059318&select=contractid%2Clast%2Cchg_rate%2Cpresetpx%2Cexepx&order=contractid%2Cexepx%2Case&_=1577088059356"

    url_510300_apr = "http://yunhq.sse.com.cn:32041//v1/sho/list/tstyle/510300_04?callback=jQuery112409417454011549969_1582766597079&select=contractid%2Clast%2Cchg_rate%2Cpresetpx%2Cexepx&order=contractid%2Cexepx%2Case&_=1582766597086"

    url_510300_jun = "http://yunhq.sse.com.cn:32041//v1/sho/list/tstyle/510300_06?callback=jQuery112402078220234177265_1577088059336&select=contractid%2Clast%2Cchg_rate%2Cpresetpx%2Cexepx&order=contractid%2Cexepx%2Case&_=1577088059360"

    url_510300_sep = "http://yunhq.sse.com.cn:32041//v1/sho/list/tstyle/510300_09?callback=jQuery11240028350739831281335_1579742947846&select=contractid%2Clast%2Cchg_rate%2Cpresetpx%2Cexepx&order=contractid%2Cexepx%2Case&_=1579742947854"

    url_510300 = "http://yunhq.sse.com.cn:32041//v1/sh1/line/510300?callback=jQuery1124083017185515941_1577089469213&begin=0&end=-1&select=time%2Cprice%2Cvolume&_=1577089469215"

    #url_510050_jan = "http://yunhq.sse.com.cn:32041/v1/sho/list/tstyle/510050_01?callback=jQuery112408090383939976182_1574904018122&select=contractid%2Clast%2Cchg_rate%2Cpresetpx%2Cexepx&_=1574904018127"

    #url_510050_feb = "http://yunhq.sse.com.cn:32041//v1/sho/list/tstyle/510050_02?callback=jQuery112407089919710187241_1577321533000&select=contractid%2Clast%2Cchg_rate%2Cpresetpx%2Cexepx&order=contractid%2Cexepx%2Case&_=1577321533005"

    url_510050_mar = "http://yunhq.sse.com.cn:32041/v1/sho/list/tstyle/510050_03?callback=jQuery111206287606767948288_1564018683263&select=contractid%2Clast%2Cchg_rate%2Cpresetpx%2Cexepx&_=1564018683268"

    url_510050_apr = "http://yunhq.sse.com.cn:32041//v1/sho/list/tstyle/510050_04?callback=jQuery112409417454011549969_1582766597079&select=contractid%2Clast%2Cchg_rate%2Cpresetpx%2Cexepx&order=contractid%2Cexepx%2Case&_=1582766597082"

    url_510050_jun = "http://yunhq.sse.com.cn:32041/v1/sho/list/tstyle/510050_06?callback=jQuery111209494863322515489_1571879875297&select=contractid%2Clast%2Cchg_rate%2Cpresetpx%2Cexepx&_=1571879875304"

    url_510050_sep = "http://yunhq.sse.com.cn:32041//v1/sho/list/tstyle/510050_09?callback=jQuery11240028350739831281335_1579742947844&select=contractid%2Clast%2Cchg_rate%2Cpresetpx%2Cexepx&order=contractid%2Cexepx%2Case&_=1579742947849"

    url_510050 = "http://yunhq.sse.com.cn:32041/v1/sh1/line/510050?callback=jQuery111208396578891098054_1563195335181&begin=0&end=-1&select=time%2Cprice%2Cvolume & _ =1563195335188"

    url_list = [url_510300, url_510300_mar, url_510300_apr, url_510300_jun, url_510300_sep, url_510050, url_510050_mar, url_510050_apr, url_510050_jun, url_510050_sep]

    while True:

        now_shanghai = datetime.datetime.now(tz=pytz.timezone('Asia/Shanghai'))
        file_name = f"./txt/{now_shanghai.strftime('%Y-%m-%d')}.txt"
        if not os.path.exists(file_name):
            with open(file_name, 'w') as f:
                pass

        for url in url_list:
            paragraph = scrape_web(url)

            if paragraph!=None:

                pattern_date = re.compile('"date":(\d+),')
                match_date = re.search(pattern_date, paragraph)
                webdate = int(match_date.group(1))
                realdate = int(now_shanghai.strftime('%Y%m%d'))
                # print("web date is: {}".format(webdate))
                # print("real date is: {}".format(realdate))

                pattern_time = re.compile('"time":(\d+),')
                match_time = re.search(pattern_time, paragraph)
                webtime = int(match_time.group(1))
                realTimeString = now_shanghai.strftime('%H%M%S')
                realTime = int(realTimeString)
                # print("web time is: {}".format(webtime))
                # print("real time is: {}".format(realTime))

                weekday = now_shanghai.weekday()
                workday = weekday != 5 and weekday != 6 and webdate==realdate

                time_start = 93000
                time_break = 113000
                time_restart = 130000
                time_stop = 150000
                time_near = 91500

                market_open = workday and ((webtime >=  time_start and realTime <  time_break) or (webtime >=  time_restart and realTime <=  time_stop))
                nearly_open = workday and ((time_break <= realTime and webtime <  time_restart) or (time_near < webtime < time_start))

                if market_open:
                    with open(file_name, 'a') as f:
                        try:
                            f.write(paragraph)
                            f.write('\n')
                            print('writing to file...')
                        except Exception as e:
                            print(e)

        if market_open:
            print('{} {}{}:{}{}:{}{} markets open'.format(week_day_dict[weekday], realTimeString[0],realTimeString[1],
                                                                                realTimeString[2],realTimeString[3],
                                                                                realTimeString[4],realTimeString[5]))
            #print('waiting for 5 seconds')
            #time.sleep(5)
        elif nearly_open:
            print('{} {}{}:{}{}:{}{} markets opening soon'.format(week_day_dict[weekday], realTimeString[0],realTimeString[1],
                                                                                realTimeString[2],realTimeString[3],
                                                                                realTimeString[4],realTimeString[5]))
            print('waiting for 10 seconds')
            time.sleep(10)
        else:
            print('{} {}{}:{}{}:{}{} markets closed'.format(week_day_dict[weekday], realTimeString[0],realTimeString[1],
                                                                                realTimeString[2],realTimeString[3],
                                                                                realTimeString[4],realTimeString[5]))
            print('waiting for 10 minutes')
            time.sleep(600)


from django.urls import path, include
from . import views
from django.contrib.auth.views import auth_login

urlpatterns = [
    path('', views.index, name='main_home'),
    path('login', views.index, name='main_login'),
    path('account/', views.account, name='main_account'),
    path('feed/', views.feed, name='main_feed'),
    path('search/', views.search, name='main_search'),
    path('message/', views.message, name='main_message'),
    path('master/', views.master, name='main_master'),
    path('organization/', views.organization, name='main_organization'),
]


# any all function practice

def all_sum(*args):
    total = 0
    for i in args:
        total += i
    return total

# print(all_sum(1,2,3,4))    # correct input
# print(all_sum(1,2,3,4, "salman", ["salman"]))    # wrong input

## here we solve the problem of wrong input using all function
def all_add(*args):
    if all([(type(arg) == int or type(arg) == float) for arg in args]):
        total = 0
        for i in args:
            total += i
        return total
    else:
        return "wrong input"

print(all_add(1,2,3,4))
print(all_add(1,2,3,4, "salman", ["salman"]))


import pandas as pd
import streamlit as st
import fitz
from PIL import Image
from dataExtractor import DataExtractor
from image2 import Canvas
from firebase import FirebaseDB
import json
from st_keyup import st_keyup

json_data = {'Tear Down': ['cable', 'bomba', 'intake'],
             'Production': ['simulacion', 'equipo'],
             'Artificial Lift': ['cable', 'bomba', 'intake', 'motor', 'sensor', 'protector'],
             'Efficiency': ['general', 'bomba', 'motor']}

st.write(f"Define a new extraction {st.session_state.user['nombre']}")

def read_pdf(uploaded):
    file = fitz.open(stream=uploaded.read())
    return file

def create_image_list_from_pdf(file):
    images = []
    for page_number in range(len(file)):
        page = file.load_page(page_number)
        pix = page.get_pixmap(matrix=fitz.Matrix(2, 2))
        image = Image.frombytes("RGB", (pix.width, pix.height), pix.samples)
        w, h = 700, 500
        resized_image = image.resize((w, h))
        images.append(resized_image)
    return images

def replace_image_in_canvas(canvas, image, key):
    new_image = image  # Get the new image
    new_key = key  # Get the new key
    canvas.reset_canvas(new_image, new_key)  # Call the reset_canvas method of the canvas object

def load_canvas(image, page_number, draw_mode, update):
    canvas = Canvas(image, draw_mode, update_streamlit=update)
    canvas.create_canvas(page_number)
    canvas.process_drawing()
    return canvas

def store_scaled_coordinates(page_number, coordinates, delete_columns):
    if coordinates is not None:
        # Fill the 'page' column with the page_number value
        coordinates['page'] = page_number

        # Drop the specified columns
        coordinates = coordinates.drop(delete_columns, axis=1)

        return pd.DataFrame(coordinates)

def present_dataframe(dataframe, markdown):
    if isinstance(dataframe, pd.DataFrame):
        st.subheader(markdown)
        st.dataframe(dataframe)
    else:
        st.write("No DataFrame was provided.")

def first_page():
    st.subheader("PDF Document")
    st.write("Upload and define attributes.")
    file = st.file_uploader("Upload PDF", type=['pdf'])
    if file is not None:
        if "file" not in st.session_state:
            st.session_state["file"] = file

        if "regex" not in st.session_state:
            st.session_state["regex"] = pd.DataFrame()

        if "subject" not in st.session_state:
            st.session_state["subject"] = None

def get_report_main_topics(report):
    st.session_state.atributos_reporte = json_data[report]

def add_coordinates_to_firebase(dataframe, collection_name, subject):
    firebase_db = FirebaseDB()
    return firebase_db.add_coordinates(dataframe, collection_name, subject)

def selection():
    # Usage example
    subject_name = "testmail/test2"
    #To upload a report
    st.subheader("PDF Document Extraction")
    uploaded_file = st.file_uploader("Upload PDF sample", type=['pdf'])
    realtime_update = st.checkbox("Update in realtime", True)
    st.write("Select between defining the area of the table (rect),"
             "or modify a predefined area (transform)")
    drawing_mode = st.selectbox("Drawing tool:", ["rect", "transform"])

    if uploaded_file is not None:
        if "compiled_scales" not in st.session_state:
            st.session_state["compiled_scales"] = pd.DataFrame()

        if "page_number" not in st.session_state:
            st.session_state["page_number"] = 0

        pdf = read_pdf(uploaded_file)
        image_list = create_image_list_from_pdf(pdf)

        canvas_obj = load_canvas(image_list[st.session_state["page_number"]],
                                 st.session_state["page_number"],
                                 drawing_mode, realtime_update)

        st.caption("Note: This canvas version could define columns or cells with None values,"
                   " consider to select a table or area of it in order that the table extraction preview"
                   " contains the elements you want.")

        present_dataframe(st.session_state["compiled_scales"], "All Scaled Coordinates")

        objects_df = canvas_obj.get_objects_dataframe()
        all_scaled_coordinates = None
        if objects_df is not None and 'type' in objects_df.columns:
            table_objects = objects_df.loc[objects_df['type'] == 'rect']

            if len(table_objects) > 0:
                difference = (len(st.session_state["atributos_reporte"])-len(st.session_state["compiled_scales"]))
                #st.write(difference)
                data = st.session_state["atributos_reporte"][-difference:] if difference > 0 else []
                #st.write(data)
                all_scaled_coordinates = canvas_obj.process_tables(table_objects,
                                                                   pdf.load_page(st.session_state["page_number"]),
                                                                   data)
                if all_scaled_coordinates is not None:
                    st.markdown("### Scaled Page Coordinates")
                    st.table(all_scaled_coordinates)

                    st.markdown("### Extracted Page Tables")

                    table_count = 0

                    for _, row in all_scaled_coordinates.iterrows():
                        top = row['Top']
                        left = row['Left']
                        height = row['Final height']
                        width = row['Final width']
                        titles = row['Title']

                        data_extractor = DataExtractor(uploaded_file, st.session_state["page_number"] + 1, top, left,
                                                       width, height)
                        tables, title = data_extractor.extract_tables(titles)

                        if tables:
                            st.subheader(f"Table {titles}")
                            table_count += 1
                            for i in range(len(tables)):
                                st.dataframe(tables[i])
                        else:
                            st.write("No tables were extracted.")

                else:
                    st.write("No rectangle selections found on the canvas.")

        else:
            st.write("No rectangle selections found on the canvas.")

        canvas_element = st.empty()  # Create an empty element to display the canvas

        if "disabled" not in st.session_state:
            st.session_state["disabled"] = False

        next_button = st.button("Next", disabled=st.session_state["disabled"])
        save_button = st.button("Save", disabled=not st.session_state["disabled"])

        if next_button:
            canvas_element.empty()  # Clear the canvas element
            st.session_state["page_number"] += 1
            new_scaled_coordinates = store_scaled_coordinates(st.session_state["page_number"], all_scaled_coordinates,
                                                              ["scaleX", "scaleY", "Width", "Height"])
            if new_scaled_coordinates is not None:
                st.session_state["compiled_scales"] = pd.concat([st.session_state["compiled_scales"],
                                                                 new_scaled_coordinates], ignore_index=True)
            if st.session_state["page_number"] >= len(image_list) - 1:
                # st.session_state["page_number"] = 0
                st.session_state["disabled"] = True

            canvas_obj.reset_canvas(image_list[st.session_state["page_number"]], st.session_state["page_number"])

        if st.session_state["disabled"]:
            st.write("Before you save, define the mail subject for extraction (this implies how will be the subject"
                     " text when an email arrives to your inbox):")
            subject_value = st_keyup("", value="Report_sample", key="subject")
            st.write(f"Subject: {subject_value}")
            subject = st.session_state.user['email'] + "/" + subject_value
            subject_name = subject.replace(" ", "_")
            st.write(f"Final parameters: {subject}")

        if save_button:
            st.session_state["page_number"] += 1
            new_scaled_coordinates = store_scaled_coordinates(st.session_state["page_number"], all_scaled_coordinates,
                                                              ["scaleX", "scaleY", "Width", "Height"])
            if new_scaled_coordinates is not None:
                st.session_state["compiled_scales"] = pd.concat([st.session_state["compiled_scales"],
                                                                 new_scaled_coordinates], ignore_index=True)
            present_dataframe(st.session_state["compiled_scales"], "Final Scaled Coordinates")

            id_num = add_coordinates_to_firebase(st.session_state["compiled_scales"], "db_coord", subject_name)
            st.markdown("Data saved with id " + str(id_num))
            st.button("Finish", on_click= lambda : reset_all(uploaded_file))

        canvas_obj.display_canvas()
    else:
        st.session_state["page_number"] = 0
        st.session_state["disabled"] = False
        st.session_state["compiled_scales"] = pd.DataFrame()

# Función para mostrar la pantalla dependiendo del botón seleccionado
def mostrar_pantalla():
    # Inicializar el session state
    if 'boton_seleccionado' not in st.session_state:
        st.session_state.boton_seleccionado = None

    if 'input_text' not in st.session_state:
        st.session_state.input_text = False

    if not st.session_state.user['imap']:
        st.header("Additional process")
        st.subheader("As this app works with email (IMAP), it is important to get access to your email account.")
        input_text = st.text_input("Input you mail password", key='input_text_value')
        if st.button("Save"):
            firebasedb = FirebaseDB()
            firebasedb.set_user_data(st.session_state.user['uid'], 'ek', input_text)
            # Cambia el valor a True para mostrar los botones
            st.session_state.user['imap'] = True
        st.caption(":red[Gmail:] _For Gmail accounts, it is important to enable IMAP and input an app password, "
                   "for this you can look at the next link:_ https://support.google.com/mail/answer/185833?hl=es-419")
    else:
        # Mostrar el header dependiendo del botón seleccionado
        if st.session_state.boton_seleccionado is not None:
            if 'atributos_reporte' not in st.session_state:
                st.session_state.atributos_reporte = []

            st.header(f"Report type: {st.session_state.boton_seleccionado}")
            print(st.session_state.boton_seleccionado)
            get_report_main_topics(st.session_state.boton_seleccionado)
            print(st.session_state.atributos_reporte)
            st.write(st.session_state.atributos_reporte)
            selection()

        # Botones para seleccionar
        if st.session_state.boton_seleccionado is None:
            if st.button('Tear Down', key='button1',
                         on_click=lambda: st.session_state.update(boton_seleccionado="Tear Down")):
                pass
            if st.button('Production', key='button2',
                         on_click=lambda: st.session_state.update(boton_seleccionado="Production")):
                pass
            if st.button('Artificial Lift', key='button3',
                         on_click=lambda: st.session_state.update(boton_seleccionado="Artificial Lift")):
                pass
            if st.button('Efficiency', key='button4',
                         on_click=lambda: st.session_state.update(boton_seleccionado="Efficiency")):
                pass

        if st.session_state.boton_seleccionado is None:
            st.write("Please, select a report type")

# Mostrar la pantalla
mostrar_pantalla()

def reset_all(file):
    st.session_state.boton_seleccionado = None
    st.session_state["page_number"] = 0
    st.session_state["disabled"] = False
    st.session_state["compiled_scales"] = pd.DataFrame()
    file = None


import argparse
import socket
import sys
import json
import urllib.request
import redis
import base64
import re
import boto3
import os
import subprocess
from faker import Faker
import logging

logging.basicConfig(level=logging.DEBUG)
fake = Faker('en_US')
Faker.seed(1337)

kms_client = boto3.client('kms')
kms_key_id = os.environ.get('KMS_KEY_ID')

r = redis.Redis(unix_socket_path='/run/redis.sock')
for i in range(1,100):
	name = fake.name()
	r.set(name, 'bar{}'.format(i))

# Running server you have pass port the server  will listen to. For Example:
# $ python3 /app/server.py server 5005
class VsockListener:
	# Server
	def __init__(self, conn_backlog=128):
		self.conn_backlog = conn_backlog

	def bind(self, port):
		# Bind and listen for connections on the specified port
		self.sock = socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM)
		self.sock.bind((socket.VMADDR_CID_ANY, port))
		self.sock.listen(self.conn_backlog)

	def recv_data(self):
		# Receive data from a remote endpoint
		while True:
			try:
				logging.info("Let's accept stuff")
				(from_client, (remote_cid, remote_port)) = self.sock.accept()
				logging.info("Connection from " + str(from_client) + str(remote_cid) + str(remote_port))

				query = json.loads(base64.b64decode(from_client.recv(4096).decode()).decode())
				logging.info("Message received: {}".format(query))
				query_type = list(query.keys())[0]
				query = query[query_type]

				logging.info("{} {}".format(query_type, query))
				if query_type == 'get':
					response = query_redis(query)
				elif query_type == 'set':
					response = put_in_redis(query)
				else:
					response = "Bad query type\n"

				# Send back the response
				from_client.send(str(response).encode())

				from_client.close()
				logging.info("Client call closed")
			except Exception as ex:
				logging.info(ex)

KMS_PROXY_PORT="8000"

def get_plaintext(credentials):
		"""
		prepare inputs and invoke decrypt function
		"""

		# take all data from client
		access = credentials['access_key_id']
		secret = credentials['secret_access_key']
		token = credentials['token']
		ciphertext= credentials['ciphertext']
		region = credentials['region']

		logging.info('ciphertext: {}'.format(ciphertext))
		creds = decrypt_cipher(access, secret, token, ciphertext, region)
		return creds

def decrypt_cipher(access, secret, token, ciphertext, region):
		"""
		use KMS Tool Enclave Cli to decrypt cipher text
		"""
		logging.info('in decrypt_cypher')
		proc_params = [
				"/app/kmstool_enclave_cli",
				"decrypt",
				"--region", region,
				"--proxy-port", KMS_PROXY_PORT,
				"--aws-access-key-id", access,
				"--aws-secret-access-key", secret,
				"--aws-session-token", token,
				"--ciphertext", ciphertext,
		]
		logging.debug('proc_params: {}'.format(proc_params))
		proc = subprocess.Popen(
			proc_params,
			stdout=subprocess.PIPE,
			stderr=subprocess.PIPE
		)

		ret = proc.communicate()

		logging.debug('proc: {}'.format(proc.communicate()))

		if ret[0]:
				logging.info('No KMS error')
				logging.debug('ret[0]: {}'.format(ret[0]))
				b64text = proc.communicate()[0].decode().split()[1]
				logging.debug('b64text: {}'.format(b64text))
				plaintext = base64.b64decode(b64text).decode()
				return (0, plaintext)
		else:
				logging.info('KMS error')
				return (1, "KMS Error. Decryption Failed.\n")

def server_handler(args):
	server = VsockListener()
	server.bind(args.port)
	logging.info("Started listening to port : {}".format(args.port))
	server.recv_data()

def put_in_redis(query):
	status, query = get_plaintext(query)
	if status:
		logging.info(query)
		return query
	try:
		query = json.loads(query)
	except ValueError:
		return 'Failed to put in data: Mot valid JSON\n'
	for key in query.keys():
		r.set(key, query[key])
	return "Put the data in\n"

# Get list of current ip ranges for the S3 service for a region.
# Learn more here: https://docs.aws.amazon.com/general/latest/gr/aws-ip-ranges.html#aws-ip-download
def query_redis(query):
	status, value = get_plaintext(query)
	if status:
		logging.info(value)
		return value
	value = r.get(value)
	if value != None:
		logging.info("Key exists")
		return "The key exists\n"
	elif value == None:
		logging.info("Key doesn't exist")
		return "They key does not exist\n"
	else:
		logging.info("In Else")
		return "Somehow here with value: {}\n".format(value)

def main():
	parser = argparse.ArgumentParser(prog='vsock-sample')
	parser.add_argument("--version", action="version",
						help="Prints version information.",
						version='%(prog)s 0.1.0')
	subparsers = parser.add_subparsers(title="options")

	server_parser = subparsers.add_parser("server", description="Server",
											help="Listen on a given port.")
	server_parser.add_argument("port", type=int, help="The local port to listen on.")
	server_parser.set_defaults(func=server_handler)

	if len(sys.argv) < 2:
		parser.print_usage()
		sys.exit(1)

	args = parser.parse_args()
	args.func(args)

if __name__ == "__main__":
	main()


import os
import config
from dotenv import load_dotenv

import neuronet
import markups as nav
import actions
import constants
import paths
import user_settings as settings
from utils import set_default_commands

import markovify
import logging
from gtts import gTTS

import asyncio
from aiogram import Bot, types, Dispatcher, executor

"""ENV"""

dotenv_path = os.path.join(os.path.dirname(__file__), ".env")

bot_token = ''

if os.path.exists(dotenv_path):
    load_dotenv(dotenv_path)
    bot_token = os.getenv("API_TOKEN")

if bot_token == '': bot_token = config.API_TOKEN

"""Log level"""

logging.basicConfig(format = "%(asctime)s - %(levelname)s - %(message)s", level = logging.INFO)
logger = logging.getLogger(__name__)

"""Bot init"""

bot = Bot(token = bot_token)
dp = Dispatcher(bot)

"""Startup function"""

async def on_startup(dp):
    await set_default_commands(dp)

"""Voice answer generation"""

def generate(text, out_file):

    tts = gTTS(text, lang = "ru")
    tts.save(out_file)

"""Get text model"""

def get_model(filename):

    with open(filename, encoding = "utf-8") as f: text = f.read()
    return markovify.Text(text)

"""Get compliment"""

async def get_compliment():

    generator = get_model(paths.PATH_FEMALE_TEXT_MODEL_ANSWER)
    statement = True

    while statement:

        text = generator.make_sentence()
        if text is not None: statement = False

    return text

"""Start function"""

@dp.message_handler(commands = ["start", "hi", "hello"])
async def start(message: types.Message, commands = "start"):

    await bot.send_chat_action(message.from_user.id, types.chat.ChatActions.TYPING)
    await asyncio.sleep(1)
    await message.answer(f"{actions.ANSWER_HI} {message.from_user.full_name}!",
                        reply_markup = nav.greet_markup)

"""Error function"""

@dp.errors_handler()
async def error(self):
    await logger.warning('update "%s" casused error "%s"', self.exception_name, self.exception_message)

"""On photo"""

@dp.message_handler(content_types = ["photo"])
async def photo(message: types.Message):

    filename = "settings_" + str(message.from_user.id) + ".txt"
    settings_path = paths.PATH_USER_DATA + filename

    is_text = await settings.get_user_settings_text(settings_path)

    tmp_pic_file = paths.PATH_USER_DATA + str(message.from_user.id) + ".jpg"
    await message.photo[-1].download(destination_file=tmp_pic_file)

    result = neuronet.resolve(tmp_pic_file)
    os.remove(tmp_pic_file)

    if is_text == False:
        tmp_audio_file = paths.PATH_USER_DATA + str(message.from_user.id) + ".mp3"

    if len(result[0]) == 0:

        text = actions.ANSWER_UNDEFINED

        if is_text == False:
            generate(text, tmp_audio_file)

        await bot.send_chat_action(message.chat.id, types.chat.ChatActions.TYPING)
        await asyncio.sleep(1)

        if is_text == False:
            await message.answer_audio(audio = open(tmp_audio_file, "rb"))
            os.remove(tmp_audio_file)
            return
        else:
            await message.answer(text)
            return

    text = result[1][0] + ", на мой скромный взгляд."

    if result[0][0] == constants.IS_FEMALE: text = f'{actions.ANSWER_FEMALE} {text}'
    elif result[0][0] == constants.IS_MALE: text = f'{actions.ANSWER_MALE} {text}'

    print(text)

    await bot.send_chat_action(message.from_user.id, types.chat.ChatActions.TYPING)
    await asyncio.sleep(1)

    if is_text == False:
        generate(text, tmp_audio_file)
        await message.answer_audio(audio = open(tmp_audio_file, "rb"))
        os.remove(tmp_audio_file)
    else: await message.answer(text)

    text = ""

    if result[0][0] == constants.IS_FEMALE: text = await get_compliment()
    elif result[0][0] == constants.IS_MALE: text = actions.ANSWER_MALE_WITHOUT_MODEL

    print(text)

    await bot.send_chat_action(message.from_user.id, types.chat.ChatActions.TYPING)
    await asyncio.sleep(1)

    if is_text == False:
        generate(text, tmp_audio_file)
        await message.answer_audio(audio = open(tmp_audio_file, "rb"))
        os.remove(tmp_audio_file)
    else:
        await message.answer(text)

@dp.message_handler()
async def answers(message: types.Message):

    filename = "settings_" + str(message.from_user.id) + ".txt"
    settings_path = paths.PATH_USER_DATA + filename

    if message.text == actions.QUERY_GREETING:
        await message.answer(actions.ANSWER_GREETING, reply_markup = nav.main_markup)

    elif message.text == actions.QUERY_SETTINGS:
        await message.answer(actions.ANSWER_SETTINGS, reply_markup = nav.settings_markup)

    elif message.text == actions.QUERY_TEXT_ANSWER:

        is_text = True
        await settings.set_user_settings_text(settings_path, is_text)

        await message.answer(actions.ANSWER_TEXT_ANSWER)

    elif message.text == actions.QUERY_VOICE_ANSWER:

        is_text = False
        await settings.set_user_settings_text(settings_path, is_text)

        await message.answer(actions.ANSWER_VOICE_ANSWER)

    elif message.text == actions.QUERY_MAIN_MENU:
        await message.answer(actions.ANSWER_MAIN_MENU, reply_markup = nav.main_markup)

    elif message.text == actions.QUERY_GET_COMPLIMENT:

        is_text = await settings.get_user_settings_text(settings_path)

        if is_text:

            text = await get_compliment()

            print(text)

            await bot.send_chat_action(message.from_user.id, types.chat.ChatActions.TYPING)
            await asyncio.sleep(1)
            await message.answer(text)

        else:

            tmp_audio_file = paths.PATH_USER_DATA + str(message.from_user.id) + ".mp3"

            text = await get_compliment()

            print(text)

            await bot.send_chat_action(message.from_user.id, types.chat.ChatActions.TYPING)
            await asyncio.sleep(1)
            generate(text, tmp_audio_file)
            await message.answer_audio(audio = open(tmp_audio_file, "rb"))
            os.remove(tmp_audio_file)

    elif message.text == actions.QUERY_START_AUTO_COMPLIMENTS:

        is_run = True
        await settings.set_user_settings_text(settings_path, is_run)

        await asyncio.sleep(1)
        await message.answer(actions.ANSWER_START_AUTO_COMPLIMENTS,
                                reply_markup = nav.auto_compliments_markup)

        while is_run == True:

            is_run = await settings.get_user_settings_text(settings_path)
            text = await get_compliment()
            print(text)

            await bot.send_chat_action(message.from_user.id, types.chat.ChatActions.TYPING)
            await asyncio.sleep(3)
            await message.answer(text)

    elif message.text == actions.QUERY_STOP_AUTO_COMPLIMENTS:

        is_run = False
        await settings.set_user_settings_text(settings_path, is_run)

        await bot.send_chat_action(message.from_user.id, types.chat.ChatActions.TYPING)
        await asyncio.sleep(1)
        await message.answer(actions.ANSWER_STOP_AUTO_COMPLIMENTS,
                                reply_markup = nav.main_markup)

"""Exit function"""

@dp.message_handler(commands = ["exit", "cancel", "bye"])
async def exit(message: types.Message, commands = "exit"):

    await bot.send_chat_action(message.from_user.id, types.chat.ChatActions.TYPING)
    await asyncio.sleep(1)
    await message.answer(f"{actions.ANSWER_BYE} {message.from_user.full_name}!")

"""Run long-polling"""

def main():
    executor.start_polling(dp, on_startup=on_startup, skip_updates = True)

if __name__ == "__main__": main()


def load_dataset(key_values):

    if key_values['dataset'] == 'cora':
        from .preprocessing_cora import clean_cora
        table, pairs = clean_cora()

    elif key_values['dataset'] == 'restaurant':
        from .preprocessing_restaurant import clean_restaurant
        table, pairs = clean_restaurant()

    elif key_values['dataset'] == 'abt_buy':
        from .preprocessing_abt_buy import clean_abt_buy
        table, pairs = clean_abt_buy()

    elif key_values['dataset'] == 'amzn_gp':
        from .preprocessing_amzn_gp import clean_amzn_gp
        table, pairs = clean_amzn_gp()

    elif key_values['dataset'] == 'census':
        from .preprocessing_census import clean_census
        table, pairs = clean_census()

    elif key_values['dataset'] == 'dblp_acm':
        from .preprocessing_dblp_acm import clean_dblp_acm
        table, pairs = clean_dblp_acm()

    elif key_values['dataset'] == 'febrl_dirty':
        from .preprocessing_febrl_dirty import clean_febrl_dirty
        table, pairs = clean_febrl_dirty()

    if key_values['verbose'] > 0:
        print("#####################################################################")
        print("CURRENT dataset:        "+key_values['dataset'])
        print("CURRENT cluster_method: "+key_values['cluster_method'])
        print("CURRENT embedding_type: "+key_values['embedding_type'])
        print("#####################################################################")
    return key_values['dataset'], table, pairs


# Code you have previously used to load data
import pandas as pd
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor

# Path of the file to read
file_path = './home-data-for-ml-course/train.csv'

data = pd.read_csv(file_path)
# Create target object and call it y
y = data.SalePrice
# Create X
features = ['LotArea', 'YearBuilt', '1stFlrSF', '2ndFlrSF', 'FullBath', 'BedroomAbvGr', 'TotRmsAbvGrd']
X = data[features]

# Split into validation and training data
# train_X, val_X, train_y, val_y = train_test_split(X, y, random_state=1)

# Specify Model
model = RandomForestRegressor(random_state = 1)
# Fit Model
model.fit(X, y)

# Make validation predictions and calculate mean absolute error
val_predictions = model.predict(X)
val_mae = mean_absolute_error(val_predictions, y)
print("Validation MAE: {:,.0f}".format(val_mae))

# print(len(val_predictions))
# print(val_y.columns)
# print("******\n", val_X.columns)
# print(type(val_y))

# # Appying Test Datas
test_data_path = "./home-data-for-ml-course/test.csv"
test_data = pd.read_csv(test_data_path)

test_X = test_data[features]

val_test_predictions = model.predict(test_X)
# val_test_mae = mean_absolute_error(val_test_predictions, test_y)
# print("Validation MAE: {:,.0f}".format(val_test_mae))
# # Run the code to save predictions in the format used for competition scoring

output = pd.DataFrame({'Id': test_data.Id, 'SalePrice': val_test_predictions})
output.to_csv('submission.csv', index=False)


MPG = 20
def find_ample_city(gallons, distances):
    curr_gas, total_gas, start_city, remaining_gas = 0, 0, 0, 0
    for i in range(len(gallons)):
        curr_gas = gallons[i]*MPG - distances[i]
        if remaining_gas >= 0:
            remaining_gas += curr_gas
        else:
            remaining_gas = curr_gas
            start_city = i

        total_gas += curr_gas

    return start_city if total_gas >= 0 else -1

# gallons = (20, 15, 15, 15, 35, 25, 30, 15, 65, 45, 10, 45, 25)
# distances = (15, 20, 50, 15, 15, 30, 20, 55, 20, 50, 10, 15, 15)

gallons = [50,20, 5, 30, 25, 10, 10]
distances = [900, 600, 200, 400, 600, 200, 100]
assert find_ample_city(gallons, distances) == 3


import subprocess
from pathlib import Path
from typing import List

RESOURCE_PATH = Path("tests/resources")

def call_main(args: List[str]) -> List[str]:

    root_path = Path("./")
    filename = root_path / "rmsd/calculate_rmsd.py"

    cmd = ["python", f"{filename}", *args]

    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    stdout, stderr = proc.communicate()

    if stderr is not None:
        print(stderr.decode())

    return stdout.decode().strip().split("\n")


import sys
from heapq import heappop, heappush, heapify

class edge():
  def __init__(self, src, nbr, weigh):
    self.src = src
    self.nbr = nbr
    self.weigh = weigh

v = int(input())
e = int(input())

graph = {}

for i in range(v):
  graph[i] = []

for i in range(e):
  a, b, c = map(int, input().split())
  graph[a].append(edge(a, b, c))
  graph[b].append(edge(b, a, c))

s = int(input())
d = int(input())

w = int(input())
k = int(input())

visited = [0]*v

mxm = -1*sys.maxsize
smxm = ""
mim = sys.maxsize
smim = ""

cmax = sys.maxsize
cpath = ""
fmin = -1*sys.maxsize
fpath = ""

t = []
heapify(t)

def multisolver(graph, s, d, w, k, wsf, psf):
  global mxm, smxm, mim, smim, cmax, cpath, fmin, fpath, t
  if(s==d):

    if(wsf > mxm):
      mxm = wsf
      smxm = psf

    if(wsf < mim):
      mim = wsf
      smim = psf

    if(wsf > w and wsf < cmax):
      cmax = wsf
      cpath = psf

    if(wsf < w and wsf > fmin):
      fmin = wsf
      fpath = psf

    heappush(t, [wsf, psf])
    if(len(t)>k):
      heappop(t)

    return

  visited[s] = 1

  for i in graph[s]:
    if(visited[i.nbr] ==0):

      npsf = psf + str(i.nbr)
      nwsf = wsf + i.weigh

      multisolver(graph, i.nbr, d, w, k, nwsf, npsf)

  visited[s] = 0

  return

multisolver(graph, s, d, w, k,0,"0")

print("Smallest Path = ", smim,"@", mim, sep ="")
print("Largest Path = ",smxm,"@", mxm, sep ="")
print("Just Larger Path than ", w," = ", cpath, "@",cmax, sep ="")
print("Just Smaller Path than ",w," = ", fpath, "@",fmin, sep ="")

print(k, "th largest path = ", t[0][1],"@", t[0][0],sep ="")


def animal_cracker(string):
    """
    A function that takes two-word string and returns
    True if both words begin with the same letter
    """

    mystring = string.lower().split(' ')

    if mystring[0][0] == mystring[1][0]:
        print(f'{mystring} both have the same beginning letter')
    else:
        print(f'{mystring} does not have the same beginning letter')

animal_cracker('Levelhead llama')


# numbers 리스트로 만들 수 있는 모든 합의 경우의 수
def solution(numbers):
    answer = []
    for i in range(len(numbers)):
        for j in range(i+1, len(numbers)):
            answer.append(numbers[i] + numbers[j])
    answer = sorted(list(set(answer)))
    return answer

print(solution([2,1,3,4,1]))


"""!@namespace httpproxy Transport Layer fuer XMLRPClib"""
import xmlrpclib
import urllib2

class Urllib2Transport(xmlrpclib.Transport):
    """!Transport-Layer fuer das XMLRPC-Modul unter Verwendung von urllib2"""
    def __init__(self, opener=None, https=False, use_datetime=0):
        xmlrpclib.Transport.__init__(self, use_datetime)
        self.opener = opener or urllib2.build_opener()
        self.https = https
        self.verbose = 0

    def request(self, host, handler, request_body, verbose=0):
        """!HTTP-Request"""
        proto = ('http', 'https')[bool(self.https)]
        req = urllib2.Request('%s://%s%s' % (proto, host, handler), request_body)
        req.add_header('User-agent', self.user_agent)
        self.verbose = verbose
        return self.parse_response(self.opener.open(req))

class HTTPProxyTransport(Urllib2Transport):
    """!HTTP-Proxy fuer das XMLRPC-Modul"""
    def __init__(self, proxies, use_datetime=0):
        self._proxies = proxies
        opener = urllib2.build_opener(urllib2.ProxyHandler(proxies))
        Urllib2Transport.__init__(self, opener, use_datetime)

    def get_proxy_name(self):
        """!Liefert die Connect-Parameter des Proxies zurueck
        @return Dictionary mit den Proxy-Parametern
        """
        return self._proxies


"""The filtersets tests module."""
import pytest
from django.db.models.query import QuerySet
from django.http.request import HttpRequest

from communication.filtersets import (_get_interlocutors, _get_recipients,
                                      _get_reviews, _get_senders)

pytestmark = pytest.mark.django_db

def test_get_reviews(reviews: QuerySet):
    """Should return the filtered list of reviews."""
    assert not _get_reviews(None).count()
    request = HttpRequest()
    user = reviews[0].professional.user
    request.user = user
    result = _get_reviews(request)
    assert result.count() == 1
    assert result[0].professional.user == reviews[0].professional.user

def test_get_recipients(messages: QuerySet):
    """Should return recipients."""
    assert not _get_recipients(None).count()
    request = HttpRequest()
    user = messages[0].sender
    request.user = user
    result = _get_recipients(request)
    assert result.count() == 1
    assert result[0] == messages[0].recipient

def test_get_senders(messages: QuerySet):
    """Should return senders."""
    assert not _get_senders(None).count()
    request = HttpRequest()
    user = messages[0].recipient
    request.user = user
    result = _get_senders(request)
    assert result.count() == 1
    assert result[0] == messages[0].sender

def test_get_interlocutors(messages: QuerySet):
    """Should return interlocutors."""
    assert not _get_interlocutors(None).count()
    request = HttpRequest()
    user = messages[0].recipient
    request.user = user
    result = _get_interlocutors(request)
    assert result.count() == 1
    assert result[0] == messages[0].sender


from django.core.exceptions import ValidationError
from django.db import models
from django.urls import reverse
from django.utils.text import slugify
from django_resized import ResizedImageField

from .base import BaseModel
from .images import Images

def file_size(value):
    limit = 6 * 1024 * 1024
    if value.size > limit:
        raise ValidationError("Plik który chcesz wrzucić jest większy niż 6MB.")

class Articles(BaseModel):
    id = models.AutoField(primary_key=True)
    category = models.ForeignKey(
        "category", on_delete=models.CASCADE, verbose_name="Kategoria artykułu"
    )
    title = models.CharField(verbose_name="Tytyuł artykułu", max_length=256)
    slug = models.SlugField(verbose_name="Slug", blank=True, null=True, max_length=256)
    body = models.TextField(verbose_name="Treść artukułu")
    image = ResizedImageField(
        verbose_name="Zdjęcie główne",
        size=[1280, 960],
        upload_to="images/articles/",
        validators=[file_size],
        null=True,
        blank=True,
    )
    image_alt = models.CharField(
        verbose_name="Alternatywny text dla obrazka",
        max_length=125,
        blank=True,
        null=True,
    )
    image_title = models.CharField(
        verbose_name="Title dla obrazka", blank=True, null=True, max_length=70
    )
    meta_description = models.CharField(
        verbose_name="Meta description dla artykułu", blank=True, null=True, max_length=160
    )
    meta_title = models.CharField(
        verbose_name="Meta title dla artykułu", blank=True, null=True, max_length=60
    )
    def save(self, *args, **kwargs):
        self.slug = slugify(self.title)
        super(Articles, self).save()

    def get_absolute_url(self):
        return reverse(
            "article_details",
            kwargs={
                "category": self.category.slug,
                "title": self.slug,
                "pk": self.id,
            },
        )

    class Meta:
        ordering = ("-created_time",)
        verbose_name_plural = "Artykuły"

    def images(self):
        return Images.objects.filter(article_id=self)

    def __str__(self):
        return self.category.name + ", " + self.title


import numpy as np
from scipy.integrate import solve_ivp

class VanDerPolOscillator:
    def __init__(self, epsilon):
        self.epsilon = epsilon

    def coupledEquation(self, t, x):
        x1 = x[0]
        x2 = x[1]
        fx1 = x2
        fx2 = -x1 - (self.epsilon * ((x1 ** 2) - 1) * x2)
        return np.array([fx1, fx2], float)

    def methodRK45(self, initialState, t0=0):
        tSpan = [t0, (8 * np.pi)]
        points = solve_ivp(self.coupledEquation, tSpan, initialState, method='RK45', first_step=0.01, max_step=0.01)
        tPoints = points['t']
        fx1Points = points['y'][0]
        fx2Points = points['y'][1]
        return tPoints, fx1Points, fx2Points


# -*- coding: utf-8 -*-
"""
Created on Tue Dec  5 21:43:03 2017

@author: ly
"""
import numpy as np
import pandas as pd
import os
import seaborn as sns # data visualization library
import matplotlib.pyplot as plt
import xgboost as xgb
import math

from sklearn import metrics
from sklearn.model_selection import KFold
from xgboost.sklearn import XGBClassifier
from sklearn.linear_model import Lasso
from sklearn.metrics import confusion_matrix

def prepare_data(filepath):

    filepath=r"E:\workspace\Dementia\Q_DLB_nonDLB_after_removing_education_normalizion_0_to_1.csv"
    dataset=pd.read_csv(filepath, index_col=None)
    temp = dataset.copy()
    DLB_count = 0
    nonDLB_count = 0
    for i in range(len(temp)):
        if temp.loc[i,'Diagnosis'] == 'DLB':
            temp.loc[i,'Diagnosis'] = 1
            DLB_count+=1
        else:
            temp.loc[i,'Diagnosis'] = 0
            nonDLB_count+=1
    return temp,DLB_count,nonDLB_count

#filepath =r"E:\workspace\Dementia\Q_DLB_nonDLB_after_removing_education_normalizion_0_to_1.csv"
#temp,DLB_count,nonDLB_count = prepare_data(filepath)
#raw_target = list(temp.loc[:,'Diagnosis'] )
#Label_Array = temp.columns[:-1]
#import itertools
#combination = list(itertools.combinations(Label_Array,2))

def statistic(true,predict):
    TP=0 #TP：正确的正例
    TN=0 #TN：正确的负例
    FP=0 #FP：错误的正例
    FN=0 #FN：错误的负例
    for i in range(len(true)):
        if(true[i]==1):
            if(predict[i]==1):
                TP+=1 #真实为1，预测也为1
            else :
                FP+=1 #真实为1，预测为0

        elif(predict[i]==1):
            FN+=1 #真实为0，预测为1
        else :
            TN+=1 #真实为0，预测为0

    return [TP,FP,TN,FN]

#统计准确率衡量的5个指标：Sn,Sp,Avc,Acc,Mcc
def assess(TP,FP,TN,FN):
    Sn=Sp=Acc=Avc=Mcc=0 #评价分类器所用指标

    if(TP+FN!=0):
        Sn=TP*1.0/(TP+FN) #预测为1（正）的正确率

    if(TN+FP!=0):
        Sp=TN*1.0/(TN+FP) #预测为0（负）的正确率

    Avc=(Sn+Sp)*1.0/2 #正负平均准确率
    Acc=(TP+TN)*1.0/(TP+FP+TN+FN) #总体预测准确率

    if((TP+FN)*(TP+FP)*(TN+FP)*(TN+FN)!=0):
        Mcc=(TP*TN-FP*FN)*1.0/math.sqrt((TP+FN)*(TP+FP)*(TN+FP)*(TN+FN))

    return [Sn,Sp,Acc,Avc,Mcc]

def kFoldTest(clf, raw_data, raw_target):
    '''
    十折交叉检验，clf是分类器，返回预测集
    '''
    predict=[]
    kf = KFold(n_splits=10)
    for train_index, test_index in kf.split(raw_data):
        #print("TRAIN:", train_index, "TEST:", test_index)#查看如何分割数据
        X_train, X_test = raw_data[[train_index]], raw_data[[test_index]]
        #Y_test在这里没作用，为了数据变量对齐0.0
        Y_train, Y_test = raw_target[:test_index[0]]+raw_target[test_index[-1]+1:], raw_target[test_index[0]:test_index[-1]+1]
        clf.fit(X_train,Y_train)
        test_target_temp=clf.predict(X_test)
        predict.append(test_target_temp)
    test_target = [i for temp in predict for i in temp]#将10次测试集展平
    return test_target

def common_classier(raw_data, raw_target):
    '''
    使用常见的分类器进行分类
    '''
    from sklearn import neighbors
    from sklearn.svm import SVC
    from sklearn.tree import DecisionTreeClassifier
    from sklearn.ensemble import RandomForestClassifier
    from sklearn.ensemble import AdaBoostClassifier

    df=pd.DataFrame(index=['Sn','Sp','ACC','AVC','MCC'])#该dataframe为了将正确率写入Excel

    clf = SVC(kernel='linear', C=1)
    value = statistic(raw_target,kFoldTest(clf, raw_data, raw_target))
    df['SVM']=assess(value[0],value[1],value[2],value[3])

    #用KNN
    clf=neighbors.KNeighborsClassifier(n_neighbors = 3 )
    value=statistic(raw_target,kFoldTest(clf, raw_data, raw_target))
    df['KNN']=assess(value[0],value[1],value[2],value[3])

#    #NB,pca的时候不能用
#    clf=MultinomialNB(alpha=0.01)
#    ACC,DLB,nonDLB=statistics(raw_target,kFoldTest(clf, ra

    #Dtree
    clf = DecisionTreeClassifier(random_state=0)
    value=statistic(raw_target,kFoldTest(clf, raw_data, raw_target))
    df['Dtree']=assess(value[0],value[1],value[2],value[3])

    #随机森林
    clf = RandomForestClassifier(n_estimators= 30, max_depth=13, min_samples_split=110,
                                 min_samples_leaf=20,max_features='sqrt' ,oob_score=True,random_state=10)
    value=statistic(raw_target,kFoldTest(clf, raw_data, raw_target))
    df['RF']=assess(value[0],value[1],value[2],value[3])

    #boosting
    clf = AdaBoostClassifier(n_estimators=100)
    value=statistic(raw_target,kFoldTest(clf, raw_data, raw_target))
    df['adaboost']=assess(value[0],value[1],value[2],value[3])

    return df

'''
filepath = r"E:\workspace\Dementia\Q_DLB_nonDLB_after_removing_education_normalizion_0_to_1.csv"
temp,DLB_count,nonDLB_count = prepare_data(filepath)
raw_data = temp.drop('Diagnosis',1).as_matrix(columns=None)
raw_label = list(temp.loc[:,'Diagnosis'])
df = common_classier(raw_data,raw_label)

temp_acc = max(list(df.loc['ACC',:] ))
Label_Array = temp.columns[:-1]
import itertools
combination = list(itertools.combinations(Label_Array,2))
writer = pd.ExcelWriter(r'E:\workspace\Dementia\acc.xlsx')
i = 0
max_acc = 0

for label_index in combination:
    sheet = "combination" + str(i)
    i += 1
    raw_data = temp.loc[:,label_index].as_matrix(columns=None)
    temp_df = common_classier(raw_data, raw_label)
    temp_acc = max(list(temp_df.loc['ACC',:] ))
    if temp_acc >= max_acc :
        temp_df.to_excel(writer,sheet_name=sheet,index=True)
        max_acc = temp_acc
writer.save()
'''
def Combination(temp, DLB_count, nonDLB_count, num):
    '''
    对数据集temp特征进行组合再进行分类
    '''
    raw_target = list(temp.loc[:,'Diagnosis'] )
    Label_Array = temp.columns[:-1]
    import itertools
    combination = list(itertools.combinations(Label_Array,num))
    writer = pd.ExcelWriter(r'E:\workspace\Dementia\acc.xlsx')
    i = 0
    max_acc = 0
    for label_index in combination:
        sheet = "combination" + str(i)
        i += 1
        raw_data = temp.loc[:,label_index].as_matrix(columns=None)
        temp_df = common_classier(raw_data, raw_target)
        temp_acc = max(list(temp_df.loc['ACC',:] ))
        if temp_acc >= max_acc :
            temp_df.to_excel(writer,sheet_name=sheet,index=True)
            max_acc = temp_acc
    writer.save()

if __name__ == '__main__':

    filepath = r"E:\workspace\Dementia\Q_DLB_nonDLB_after_removing_education_normalizion_0_to_1.csv"
    temp,DLB_count,nonDLB_count = prepare_data(filepath)
    raw_data =  temp.drop('Diagnosis',1).as_matrix(columns=None)
    raw_label = list(temp.loc[:,'Diagnosis'])
    Combination(raw_data, DLB_count, nonDLB_count, 2)


from typing import Dict

from src.property.PropertyFactory import PropertyFactory
from src.storage.common.entity.Entity import Entity
from src.template.entity.EntityTemplate import EntityTemplate

class EntityFactory:

    def __init__(self, entity_template: EntityTemplate):
        self.entity_template = entity_template

    def create(self, key: str, props_values: Dict[str, str]) -> Entity:
        return Entity(
            key,
            PropertyFactory.create_from_template_and_dict(
                self.entity_template.properties_templates,
                props_values,
                lambda prop_template_id: 'Property ' + prop_template_id + ' not found for entity ' + key
            )
        )


tc = int(input())

for _ in range(tc):
    # 금광 행렬 정보 입력 받음
    n, m = map(int, input().split())

    # 매장된 금의 개수 정보 입력 받음
    data = list(map(int, input().split()))

    # matrix, arr 초기화 => tc for문 2번째 돌 때 초기화 상태여야함
    dp = []
    arr = []

    # 매장된 금의 개수 정보를 matrix로 표현
    for i in range(n*m):
        arr.append(data[i])
        if (i+1) % m == 0:
            dp.append(arr)
        arr = []

    # 행의 개수 = n
    # 이동방법 RU, R, RD 총 세 개
    # 단, 범위를 벗어나지 못하므로 맨위, 맨아래에선 오른쪽 이동만 가능
    # 모든 방향의 이동에 대해서 최종 금광 개수를 저장
    # 모든 저장 값들 중 최댓값 산출

     # 세 방향에서 왔을 때 최댓값 담을 2차원 배열
    find_max = []

    for j in range(1, m):
        for i in range(n):
            if i == 0:
                dp[i][j] += max(dp[i][j-1], dp[i+1][j-1])
            if i == n-1:
                dp[i][j] += max(dp[i][j-1], dp[i-1][j-1])
            else:
                dp[i][j] += max(dp[i][j-1], dp[i-1][j-1], dp[i+1][j-1])
        if j == m-1:
            for k in range(n):
                find_max.append(dp[k][j])

    result = max(find_max)
    print(result)

이것이 취업을 위한 코딩 테스트다 with 파이썬/다이나믹프로그래밍/금광 문제.py


import pathlib
import sys
import typing

import flash
import flash.image
import pytorch_lightning
import torch
import torchmetrics
import torchvision

import enpheeph
import enpheeph.injections.plugins.indexing.indexingplugin

CURRENT_DIR = pathlib.Path(__file__).absolute().parent
RESULTS_DIRECTORY = CURRENT_DIR / "results" / "alexnet-cifar10"
WEIGHTS_FILE = RESULTS_DIRECTORY / "weights" / "alexnet-cifar10.pt"
LOG_DIRECTORY = RESULTS_DIRECTORY / "injection_results"

WEIGHTS_FILE.parent.mkdir(parents=True, exist_ok=True)
LOG_DIRECTORY.mkdir(parents=True, exist_ok=True)

CIFAR_DIRECTORY = pathlib.Path("/shared/ml/datasets/vision/") / "CIFAR10"

class AlexNetLightningModule(pytorch_lightning.LightningModule):
    def __init__(self, pretrained: bool = True, num_classes: int = 1000) -> None:
        super().__init__()

        self.num_classes = num_classes
        self.pretrained = pretrained

        self.model = torchvision.models.AlexNet(num_classes=num_classes)
        if self.pretrained:
            # must be accessed with sys.modules otherwise it uses the function
            # which is imported from the sub-module
            # we use type: ignore as mypy cannot check torchvision typings
            # we have to split it otherwise black creates problems
            mod = sys.modules["torchvision.models.alexnet"]
            state_dict = torch.hub.load_state_dict_from_url(
                mod.model_urls["alexnet"],  # type: ignore[attr-defined]
                progress=True,
            )
            # we must filter the mismatching keys in the state dict
            # we generate the current model state dict
            model_state_dict = self.model.state_dict()
            filtered_state_dict = {
                k: v_new
                # we select the new value if the dimension is the same as with the old
                # one
                if v_new.size() == v_old.size()
                # otherwise we use the initialized one from the model
                else v_old
                for (k, v_old), v_new in zip(
                    model_state_dict.items(),
                    state_dict.values(),
                )
            }

            self.model.load_state_dict(filtered_state_dict, strict=False)

        self.normalizer_fn = torch.nn.Softmax(dim=-1)
        self.accuracy_fn = torchmetrics.Accuracy()
        self.loss_fn = torch.nn.CrossEntropyLoss()

        self.save_hyperparameters()

        # we initialize the weights
        self.init_weights()

    def init_weights(self) -> None:
        # this initialization is similar to the ResNet one
        # taken from https://github.com/Lornatang/AlexNet-PyTorch/
        # @ alexnet_pytorch/model.py#L63
        for m in self.modules():
            if isinstance(m, torch.nn.Conv2d):
                torch.nn.init.kaiming_normal_(
                    m.weight, mode="fan_out", nonlinearity="relu"
                )
                if m.bias is not None:
                    torch.nn.init.constant_(m.bias, 0)
            elif isinstance(m, torch.nn.BatchNorm2d):
                torch.nn.init.constant_(m.weight, 1)
                if m.bias is not None:
                    torch.nn.init.constant_(m.bias, 0)
            elif isinstance(m, torch.nn.Linear):
                torch.nn.init.normal_(m.weight, 0, 0.01)
                if m.bias is not None:
                    torch.nn.init.constant_(m.bias, 0)

    def forward(self, inpt: torch.Tensor) -> torch.Tensor:
        return self.model(inpt)

    def configure_optimizers(self) -> torch.optim.Optimizer:
        optimizer = torch.optim.SGD(self.parameters(), lr=1e-2)
        return optimizer

    def inference(
        self,
        batch: typing.Union[
            torch.Tensor,
            typing.Dict[flash.core.data.data_source.DefaultDataKeys, torch.Tensor],
        ],
        batch_idx: int,
    ) -> typing.Dict[str, torch.Tensor]:
        # we need to check for the batch to be a flash batch or to be a standard tuple
        # as otherwise it may not be compatible
        if isinstance(batch, dict):
            x = batch.get(flash.core.data.data_source.DefaultDataKeys.INPUT, None)
            y = batch.get(flash.core.data.data_source.DefaultDataKeys.TARGET, None)
            if x is None or y is None:
                raise ValueError("Incompatible input for the batch")
        else:
            x, y = batch

        output = self.forward(x)
        return {
            "loss": self.loss_fn(output, y),
            "accuracy": self.accuracy_fn(self.normalizer_fn(output), y),
        }

    def training_step(
        self,
        batch: typing.Union[
            torch.Tensor,
            typing.Dict[flash.core.data.data_source.DefaultDataKeys, torch.Tensor],
        ],
        batch_idx: int,
    ) -> torch.Tensor:
        res = self.inference(batch, batch_idx)
        self.log_dict(
            {"train_loss": res["loss"], "train_accuracy": res["accuracy"]},
            prog_bar=True,
            on_step=True,
            on_epoch=True,
            logger=True,
        )
        return res["loss"]

    def validation_step(
        self,
        batch: typing.Union[
            torch.Tensor,
            typing.Dict[flash.core.data.data_source.DefaultDataKeys, torch.Tensor],
        ],
        batch_idx: int,
    ) -> None:
        res = self.inference(batch, batch_idx)
        self.log_dict(
            {"val_loss": res["loss"], "val_accuracy": res["accuracy"]},
            prog_bar=True,
            on_step=True,
            on_epoch=True,
            logger=True,
        )

    def test_step(
        self,
        batch: typing.Union[
            torch.Tensor,
            typing.Dict[flash.core.data.data_source.DefaultDataKeys, torch.Tensor],
        ],
        batch_idx: int,
    ) -> None:
        res = self.inference(batch, batch_idx)
        self.log_dict(
            {"test_loss": res["loss"], "test_accuracy": res["accuracy"]},
            prog_bar=True,
            on_step=True,
            on_epoch=True,
            logger=True,
        )

pytorch_lightning.seed_everything(seed=41, workers=True)

storage_plugin = enpheeph.injections.plugins.storage.SQLiteStoragePlugin(
    db_url="sqlite:///" + str(LOG_DIRECTORY / "database.sqlite")
)
pytorch_mask_plugin = enpheeph.injections.plugins.NumPyPyTorchMaskPlugin()
pytorch_handler_plugin = enpheeph.handlers.plugins.PyTorchHandlerPlugin()

monitor_1 = enpheeph.injections.OutputPyTorchMonitor(
    location=enpheeph.utils.data_classes.MonitorLocation(
        module_name="model.features.0",
        parameter_type=enpheeph.utils.enums.ParameterType.Activation,
        dimension_index={
            enpheeph.utils.enums.DimensionType.Tensor: ...,
            enpheeph.utils.enums.DimensionType.Batch: ...,
        },
        bit_index=None,
    ),
    enabled_metrics=enpheeph.utils.enums.MonitorMetric.StandardDeviation,
    storage_plugin=storage_plugin,
    move_to_first=False,
    indexing_plugin=enpheeph.injections.plugins.indexing.indexingplugin.IndexingPlugin(
        dimension_dict=enpheeph.utils.constants.PYTORCH_DIMENSION_DICT,
    ),
)
fault_1 = enpheeph.injections.OutputPyTorchFault(
    location=enpheeph.utils.data_classes.FaultLocation(
        module_name="model.features.0",
        parameter_type=enpheeph.utils.enums.ParameterType.Weight,
        parameter_name="weight",
        dimension_index={
            enpheeph.utils.enums.DimensionType.Tensor: (
                ...,
                0,
                0,
            ),
            enpheeph.utils.enums.DimensionType.Batch: ...,
        },
        bit_index=[10, 16, 31],
        bit_fault_value=enpheeph.utils.enums.BitFaultValue.StuckAtOne,
    ),
    low_level_torch_plugin=pytorch_mask_plugin,
    indexing_plugin=enpheeph.injections.plugins.indexing.indexingplugin.IndexingPlugin(
        dimension_dict=enpheeph.utils.constants.PYTORCH_DIMENSION_DICT,
    ),
)
monitor_2 = enpheeph.injections.OutputPyTorchMonitor(
    location=enpheeph.utils.data_classes.MonitorLocation(
        module_name="model.features.0",
        parameter_type=enpheeph.utils.enums.ParameterType.Activation,
        dimension_index={
            enpheeph.utils.enums.DimensionType.Tensor: ...,
            enpheeph.utils.enums.DimensionType.Batch: ...,
        },
        bit_index=None,
    ),
    enabled_metrics=enpheeph.utils.enums.MonitorMetric.StandardDeviation,
    storage_plugin=storage_plugin,
    move_to_first=False,
    indexing_plugin=enpheeph.injections.plugins.indexing.indexingplugin.IndexingPlugin(
        dimension_dict=enpheeph.utils.constants.PYTORCH_DIMENSION_DICT,
    ),
)
monitor_3 = enpheeph.injections.OutputPyTorchMonitor(
    location=enpheeph.utils.data_classes.MonitorLocation(
        module_name="model.classifier.1",
        parameter_type=enpheeph.utils.enums.ParameterType.Activation,
        dimension_index={
            enpheeph.utils.enums.DimensionType.Tensor: (slice(10, 100),),
            enpheeph.utils.enums.DimensionType.Batch: ...,
        },
        bit_index=None,
    ),
    enabled_metrics=enpheeph.utils.enums.MonitorMetric.StandardDeviation,
    storage_plugin=storage_plugin,
    move_to_first=False,
    indexing_plugin=enpheeph.injections.plugins.indexing.indexingplugin.IndexingPlugin(
        dimension_dict=enpheeph.utils.constants.PYTORCH_DIMENSION_DICT,
    ),
)
fault_2 = enpheeph.injections.OutputPyTorchFault(
    location=enpheeph.utils.data_classes.FaultLocation(
        module_name="model.classifier.1",
        parameter_type=enpheeph.utils.enums.ParameterType.Activation,
        dimension_index={
            enpheeph.utils.enums.DimensionType.Tensor: (slice(10, 100),),
            enpheeph.utils.enums.DimensionType.Batch: ...,
        },
        bit_index=...,
        bit_fault_value=enpheeph.utils.enums.BitFaultValue.StuckAtOne,
    ),
    low_level_torch_plugin=pytorch_mask_plugin,
    indexing_plugin=enpheeph.injections.plugins.indexing.indexingplugin.IndexingPlugin(
        dimension_dict=enpheeph.utils.constants.PYTORCH_DIMENSION_DICT,
    ),
)
monitor_4 = enpheeph.injections.OutputPyTorchMonitor(
    location=enpheeph.utils.data_classes.MonitorLocation(
        module_name="model.classifier.1",
        parameter_type=enpheeph.utils.enums.ParameterType.Activation,
        dimension_index={
            enpheeph.utils.enums.DimensionType.Tensor: (slice(10, 100),),
            enpheeph.utils.enums.DimensionType.Batch: ...,
        },
        bit_index=None,
    ),
    enabled_metrics=enpheeph.utils.enums.MonitorMetric.StandardDeviation,
    storage_plugin=storage_plugin,
    move_to_first=False,
    indexing_plugin=enpheeph.injections.plugins.indexing.indexingplugin.IndexingPlugin(
        dimension_dict=enpheeph.utils.constants.PYTORCH_DIMENSION_DICT,
    ),
)

injection_handler = enpheeph.handlers.InjectionHandler(
    injections=[monitor_1, fault_1, monitor_2, monitor_3, fault_2, monitor_4],
    library_handler_plugin=pytorch_handler_plugin,
)

callback = enpheeph.integrations.pytorchlightning.InjectionCallback(
    injection_handler=injection_handler,
    storage_plugin=storage_plugin,
)

trainer = pytorch_lightning.Trainer(
    callbacks=[callback],
    deterministic=True,
    enable_checkpointing=False,
    max_epochs=10,
    # one can use gpu but some functions will not be deterministic, so deterministic
    # must be set to False
    accelerator="cpu",
    devices=1,
    # if one uses spawn or dp it will fail as sqlite connector is not picklable
    # strategy="ddp",
)

model = AlexNetLightningModule(num_classes=10, pretrained=False)

# transform = torchvision.transforms.Compose(
#     [
#         #torchvision.transforms.ToTensor(),
#         torchvision.transforms.Normalize(
#             (0.5, 0.5, 0.5),
#             (0.5, 0.5, 0.5),
#         ),
#         torchvision.transforms.RandomHorizontalFlip(),
#     ]
# )
cifar_train = torchvision.datasets.CIFAR10(
    str(CIFAR_DIRECTORY),
    train=True,
    download=True,
)
cifar_test = torchvision.datasets.CIFAR10(
    str(CIFAR_DIRECTORY),
    train=False,
    download=True,
)
datamodule = flash.image.ImageClassificationData.from_datasets(
    train_dataset=cifar_train,
    test_dataset=cifar_test,
    val_split=0.2,
    num_workers=64,
    batch_size=32,
)

if not WEIGHTS_FILE.exists():
    trainer.fit(
        model,
        train_dataloaders=datamodule.train_dataloader(),
        val_dataloaders=datamodule.val_dataloader(),
    )

    trainer.save_checkpoint(str(WEIGHTS_FILE))

model = model.load_from_checkpoint(str(WEIGHTS_FILE))

# no injections/monitors
print("\n\nBaseline, no injection or monitors\n")
trainer.test(
    model,
    dataloaders=datamodule.test_dataloader(),
)

# we enable only the monitors
# we use this as baseline, no injections
callback.injection_handler.activate([monitor_1, monitor_2, monitor_3, monitor_4])
print("\n\nBaseline, no injection, only monitors\n")
trainer.test(
    model,
    dataloaders=datamodule.test_dataloader(),
)

# we enable the faults
callback.injection_handler.activate([fault_1, fault_2])
print("\n\nWeight + activation injection\n")
trainer.test(
    model,
    dataloaders=datamodule.test_dataloader(),
)

# we disable the faults
callback.injection_handler.deactivate([fault_1, fault_2])
print("\n\nBaseline again, no injection, only monitors\n")
# we test again to reach same results as before injection
trainer.test(
    model,
    dataloaders=datamodule.test_dataloader(),
)