from settings import MicrophoneStream, listen_print_loop
from google.cloud import speech_v1p1beta1 as speech
import ply.lex as lex

RATE = 16000
CHUNK = int(RATE / 10)

def main():
    temp = ''
    language_code = "pl-PL"

    with open('data.txt', 'r') as file:
        phrases = []
        for line in file:
            phrases.append(line.rstrip())

    client = speech.SpeechClient()
    config = speech.RecognitionConfig(
        encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
        sample_rate_hertz=RATE,
        language_code=language_code,
        speech_contexts = [{
        'phrases': phrases,
        'boost': 20
        }]
    )

    streaming_config = speech.StreamingRecognitionConfig(
        config=config, interim_results=True
    )

    with MicrophoneStream(RATE, CHUNK) as stream:
        audio_generator = stream.generator()
        requests = (
            speech.StreamingRecognizeRequest(audio_content=content)
            for content in audio_generator
        )

        responses = client.streaming_recognize(streaming_config, requests)
        temp = listen_print_loop(responses)
    return temp

text = ''
text = main().replace('stop', '').lower()

TABLES = [r'pacjenci', r'wizyty', r'pacjenta', r'lekarze']
COLUMNS = [r'id\spacjenta', r'nazwisko', r'imie', r'pesel', r'data\surodzenia', r'lekarz', r'pacjent', r'koszt', r'data\swizyty', r'id\slekarza', r'specjalnosc', r'nip']

def create_regex(table):
    str = r''
    for item in table:
        str += f'|({item})'
    return str[1:]

TABLES_STR = create_regex(TABLES)
COLUMNS_STR = create_regex(COLUMNS)

tokens = (
'SELECT',
'ALL',
'DISTINCT',
'TOP',
'PERCENT',
'PROPER_NAME',
'DOT',
'MIN',
'MAX',
'COUNT',
'AVG',
'SUM',
'FROM',
'WHERE',
'AND',
'OR',
'NOT',
'IS',
'EQUAL',
'GREATER_THAN',
'LESS_THAN',
'GREATER_THAN_OR_EQUAL',
'LESS_THAN_OR_EQUAL',
'NOT_EQUAL',
'BEGINE',
'END',
'ORDER_BY',
'ASC',
'DESC',
'TABLE',
'COLUMN',
'HAVING',
'GROUP_BY',
'JOIN',
'ON',
'INNER_JOIN',
'LEFT_JOIN',
'RIGHT_JOIN',
'FULL_OUTER_JOIN',
'NULL',
'IN',
'NUMBER'
)

t_SELECT = r'(wybierz)|(zaznacz)|(podaj)|(zwróć)'
t_ALL = r'(wszystkie)|(wszystko)'
t_DISTINCT = r'(unikalne)|(wyjątkowe)'
t_TOP = r'(pierwsze)|(górne)|(początkowe)'
t_PERCENT = r'procent'

t_DOT = r'(kropka)|(\.)'
t_MIN = r'minimum(\sz)?'
t_MAX = r'maksimum(\sz)?'
t_COUNT = r'(policz)|(ilość)|(oblicz\silość)'
t_AVG = r'(średnia)|(oblicz\sśrednią)'
t_SUM = r'(suma)|(zsumuj)|(oblicz\ssumę)'

t_FROM = r'(z\stabeli)|(z\sbazy\sdanych)'

t_WHERE = r'(gdzie)|(tam\sgdzie)'
t_AND = r'oraz'
t_OR = r'(lub)|(albo)'
t_NOT = r'(nieprawda\sże)|(nie\sjest)'
t_IS = r'jest'

t_EQUAL	= r'(jest\s)?równ(a|e|y)'
t_GREATER_THAN = r'(jest\s)?większ(a|e)\s(niż)?'
t_LESS_THAN	= r'(jest\s)?mniejsz(a|e)\s(niż)?'
t_GREATER_THAN_OR_EQUAL = r'(jest\s)?większ(a|e)\s(albo)|(lub)|(bądź)\s(jest\s)?równ(a|e)\s(niż)?'
t_LESS_THAN_OR_EQUAL = r'(jest\s)?mniejsz(a|e)\s(albo)|(lub)|(bądź)\s(jest\s)?równ(a|e)\s(niż)?'
t_NOT_EQUAL = '(jest\s)?różne\s(od)?'

t_BEGINE = r'((rozpocznij)|(zacznij)|(stwórz))\spodzapytanie'
t_END = r'((zakończ)|(zamknij))\spodzapytanie'

t_ORDER_BY = r'(posortuj(\spo)?)|(uporządkuj(\spo)?)|(ułóż\sw\skolejności)'
t_ASC = r'rosnąco'
t_DESC = r'malejąco'

t_TABLE = create_regex(TABLES)
t_COLUMN = create_regex(COLUMNS)

t_HAVING = r'(mając(e|y)?)|(posiadają(c|e)?)'

t_GROUP_BY = r'pogrupuj(\spo)?'

t_JOIN = r'(połącz(enie)?)|(złącz(enie)?)'
t_ON = r'na'
t_INNER_JOIN = r'((połącz(enie)?)|(złącz(enie)?))\s(wewnętrzne)'
t_LEFT_JOIN = r'((połącz(enie)?)|(złącz(enie)?))\s(lewostronn(i)?e)'
t_RIGHT_JOIN = r'((połącz(enie)?)|(złącz(enie)?))\s(prawostronne)'
t_FULL_OUTER_JOIN = r'((połącz(enie)?)|(złącz(enie)?))\s(całkowite)'

t_NULL = r'((bez)|(brak)|(nie\sma))\swartości'
t_IN = r'w'

def t_NUMBER(t):
    r'\d+(,\d+)?'
    if ',' in t.value:
        t.value = int(t.value)
    else:
        t.value = int(t.value)
    return t

def t_PROPER_NAME(t):
    r'nazwa\swłasna\s\w+'
    t.value = t.value[13:]
    return t

t_ignore  = ' \t\n'

def t_error(t):
     print("Illegal character '%s'" % t.value[0])
     t.lexer.skip(1)

lexer = lex.lex()

lexer.input(text)

#EXAMPLES
"""
lexer.input('''Zaznacz suma koszt
                Z tabeli wizyty Gdzie pacjent jest równy
                stwórz podzapytanie zaznacz id pacjenta
                Z tabeli pacjenci gdzie
                Nazwisko jest równe nazwa własna Gumowska
                oraz imie jest równe nazwa własna anna
                zakończ podzapytanie'''.lower())

lexer.input('''Wybierz nazwisko z tabeli pacjenci
                połącz lewostronnie wizyty na pacjenci
                kropka id pacjenta jest równe wizyty
                kropka pacjent Gdzie wizyty
                kropka pacjent jest bez wartości'''.lower())

lexer.input('''zaznacz unikalne lekarze kropka nazwisko
                lekarze kropka specjalnosc
                z tabeli lekarze
                Połącz  wizyty
                Na lekarze kropka id lekarza równe wizyty kropka lekarz
                Złącz pacjenci
                Na pacjenci kropka id pacjenta równe wizyty kropka pacjent
                Gdzie pacjent kropka nazwisko równe nazwa własna Witkowski'''.lower())

lexer.input('''Zaznacz nazwisko specjalnosc
                Z tabeli lekarze
                Gdzie specjalnosc jest równa rozpocznij podzapytanie wybierz specjalnosc z tabeli lekarze gdzie nazwisko jest równe nazwa własna Stefanowicz zakończ podzapytanie
                Oraz nazwisko jest różne od  nazwa własna Stefanowicz'''.lower())
"""
lista = []
while True:
    tok = lexer.token()
    if not tok:
        break
    print(tok)
    lista.append(tok)

lista.append(None)

breaks = [
'FROM',
'JOIN',
'INNER_JOIN',
'LEFT_JOIN',
'RIGHT_JOIN',
'FULL_OUTER_JOIN',
'WHERE',
'GROUP_BY',
'HAVING',
'SELECT',
'OREDR_BY',
'BEGINE',
]

if lista[0] != None:
    it = iter(lista)
    item = next(it)
    line = []
    commands = []
    before = ' '

    while item != None:
        if item.type == 'END':
            commands.append(line)
            line = [item.type]
            commands.append(line)
            line =[]
            item = next(it)
        elif item.type in breaks:
            commands.append(line)
            line = [(item.type).replace('_', ' ')]
            item = next(it)
        elif item.type == 'TABLE':
            before = item.value
            item = next(it)
            if item == None:
                line.append(before.replace(' ', '_'))
                break
            if item.type == 'DOT':
                item = next(it)
                if item == None:
                    line.append(before.replace(' ', '_'))
                    break
                if item.type == 'COLUMN':
                    line.append(f'{before}.{(item.value)}'.replace(' ', '_'))
                    before = ' '
                    item = next(it)
                else:
                    line.append('ERR' + f'{before}.{(item.value)}'.replace(' ', '_'))
            else:
                line.append(before.replace(' ', '_'))
                before = ' '
        elif item.type == 'COLUMN':
            line.append((item.value).replace(' ', '_'))
            item = next(it)
        elif item.type == 'NUMBER':
            line.append(item.value)
            item = next(it)
        elif item.type == 'PROPER_NAME':
            line.append(('\'' + item.value + '\'').replace(' ', '_'))
            item = next(it)
        elif item.type == 'EQUAL':
            line.append('=')
            item = next(it)
        elif item.type == 'GREATER_THAN':
            line.append('>')
            item = next(it)
        elif item.type == 'LESS_THAN':
            line.append('<')
            item = next(it)
        elif item.type == 'GREATER_THAN_OR_EQUAL':
            line.append('>=')
            item = next(it)
        elif item.type == 'LESS_THAN_OR_EQUAL':
            line.append('<=')
            item = next(it)
        elif item.type == 'NOT_EQUAL':
            line.append('!=')
            item = next(it)
        elif item.type == 'ALL':
            line.append('*')
            item = next(it)
        elif item.type == 'PERCENT':
            line.append('PERCENT')
            item = next(it)
        elif item.type in ['MIN', 'MAX', 'COUNT', 'AVG', 'SUM']:
            func = item.type
            item = next(it)
            if item == None:
                break
            if item.type == 'COLUMN':
                line.append(f'{func}({item.value})')
                item = next(it)
        else:
            line.append(item.type)
            item = next(it)
    commands.append(line)
print(*commands, sep='\n')

final = []
tab = []
for item in commands[1:]:
    if item:
        if item[0] == 'BEGINE':
            final.append(tab + [[' ( ']])
            tab = []
        elif item[0] == 'END':
            final.append(tab + [[' ) ']])
            tab = []
        else:
            tab.append(item)
final.append(tab)
for item in final:
    print(*item,sep='\n',end='\n\n')

querry = []
for line in final:
    for item in line:
        temp = ''
        if item[0] == 'SELECT' or item[0] == 'FROM':
            for it in item:
                if isinstance(item, str):
                    temp += ' ' + it + ','
                else:
                    temp += ' ' + str(it)

            if 'TOP' in temp:
                temp = temp.replace(',', '', 1)
            if 'DISTINCT' in temp:
                querry.append(temp[0:].replace(',', '', 2).replace(' ', '', 1))
            else:
                querry.append(temp[0:].replace(',', '', 1).replace(' ', '', 1))
        else:
            for it in item:
                temp += ' ' + it
            querry.append(temp.replace(' ', '', 1))
querry[-1] += ';'
print(*querry,sep='\n')


#!/usr/bin/python
import socket, subprocess
import time
import sys

HOST = ''
PORT = remote_port

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind((HOST, PORT))
    while 1:
        result, (rip, rport) = s.recvfrom(1024)
        sys.stdout.write('%s@%d:%s' %(rip, rport, result))
except KeyboardInterrupt:
    s.close


"""
Problem 1.3.10

"Using the computer and language of your choice write a program to estimate the machine epsilon (of your machine)"
Machine epsilon is a number such that any number epsilon where:
epsilon + 1.0 = 1.0

Note: this program is not an original. Copied as notes from Dr. Randall Cone
"""
import math

# Strategy?
# Let's iterate through 2-adic fractions as they get smaller,
# and use a while loop to do so.

# Variable Setup
e = 1.0 # Epsilon  starting with it equaling a non-trivial 2-adic fractional number e = 1/(2^n)
n = 1.0 # track where in the iteration we are

# we need to keep the loop going until e + 1 = 1
while(not (1==1+e)):
    # in order to not loop forever these numbers need to get smaller
    e = (2**(-n))
    # print the result of the iteration
    print('n = {},\te = {}'.format(n,e))
    # and then increase our counter
    n=n+1

# finally print out the resulting answer
print('\nSmallest value epsilon: {:24.24f}'.format(e))

# proof that this is our true epsilon value
print('e + 1 = {:24.24f}'.format(e+1))


import logging
from threading import Event
from typing import TYPE_CHECKING

from app.types_.dofus.scripts.com.ankamagames.dofus.network.messages.game.inventory.exchanges.ExchangeBidHouseTypeMessage import (
    ExchangeBidHouseTypeMessage,
)

from app.database.models import get_engine
from app.gui.signals import AppSignals
from app.modules.sale_hotel import SaleHotel
from app.network.utils import send_parsed_msg
from app.types_.dicts.common import EventValueChangeWithCallback

if TYPE_CHECKING:
    from app.types_.models.common import CommonInfo

logger = logging.getLogger(__name__)

class ScrappingSaleHotel(SaleHotel):
    def __init__(
            self,
            types: list[int],
            is_playing_event: Event,
            app_signals: AppSignals,
            common_info: 'CommonInfo',
    ) -> None:
        super().__init__(common_info, app_signals)
        self.engine = get_engine()
        self.is_playing_event = is_playing_event

        self.selected_type: int | None = None

        self.types_left = self.get_accepted_types(types)
        self.objects_left_in_type: list[dict] = []

        if self.is_playing_event.is_set():
            self.on_start(True)
        else:
            self.on_stop(True)

    def on_start(self, is_first: bool = False):
        if not is_first:
            self.process()

        self.check_event_change_thread(
            [
                EventValueChangeWithCallback(
                    target_value=False,
                    event=self.is_playing_event,
                    callback=self.on_stop,
                )
            ]
        )

    def on_stop(self, is_first: bool = False):
        if not is_first:
            if self.selected_object is not None:
                self.close_selected_object()
            if self.selected_type is not None:
                self.close_type()

        self.check_event_change_thread(
            [
                EventValueChangeWithCallback(
                    target_value=True,
                    event=self.is_playing_event,
                    callback=self.on_start,
                )
            ]
        )

    def clear(self):
        super().clear()
        self.is_playing_event.clear()
        self.app_signals.on_new_buying_hdv_playing_value.emit()

    def update_progression(self):
        self.app_signals.on_new_scraping_current_state.emit(
            {
                "category_remaining": len(self.types_left),
                "object_remaining": len(self.objects_left_in_type),
            }
        )

    def process(self):
        if self.selected_object is not None:
            self.close_selected_object()
        elif len(self.objects_left_in_type) > 0:
            _object = self.objects_left_in_type.pop()
            self.place_object(_object["object_gid"])
        elif len(self.types_left) > 0:
            if self.selected_type is not None:
                self.close_type()
            else:
                self.place_type()
        else:
            logger.info("no object or type left to check prices")
            self.clear()

        self.update_progression()

    def place_type(self):
        assert len(self.types_left) > 0
        _type = self.types_left.pop()
        send_parsed_msg(
            self.common_info.message_to_send_queue,
            ExchangeBidHouseTypeMessage(
                follow=True,
                type=_type,
            ),
        )
        self.selected_type = _type
        logger.info(f"Sending check type {_type}")

    def close_type(self):
        assert self.selected_type is not None
        logger.info(f"Sending check type {self.selected_type} to close")
        send_parsed_msg(
            self.common_info.message_to_send_queue,
            ExchangeBidHouseTypeMessage(
                follow=False,
                type=self.selected_type,
            ),
        )
        self.selected_type = None
        if self.is_playing_event.is_set():
            self.process()


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

"""
项目名：SingalDemo - the name of the current project.
文件名：singal - the name of the new file which you specify in the New File dialog box during the file creation.
电脑：Administrator - baiyang
时间：2018/6/10 23:17
IDE:PyCharm - the name of the IDE in which the file will be created.
"""

from django.dispatch import Signal, receiver
from django.db.models.signals import post_save, pre_save
from .models import Student

signal_bai = Signal(providing_args="bai")

@receiver(signal_bai)
def signal_callback(sender, **kwargs):
    print(sender, sender, kwargs)
    print(signal_callback)

@receiver(post_save, sender=Student)
def pre_save_callback(sender, **kwargs):
    print(sender, kwargs)
    print("pre_save_callback")


# Increase the Weight by 1000

import pandas
from sklearn import linear_model

df = pandas.read_csv("Python\Machine Learning Modules - W3H Schools\data.csv")      # VS Code Syntax
# df = pandas.read_csv("data.csv")                                                  # IntelliJ IDEA Syntax

X = df[['Weight', 'Volume']]
y = df['CO2']

reger = linear_model.LinearRegression()
reger.fit(X, y)

estimateCO2 = reger.predict([[3300, 1300]])

print(estimateCO2)

# Expected Print Statement [114.75968007]

Python/Machine Learning Modules - W3H Schools/multiRegressTestFile1000.py


import sys
si = sys.stdin.readline

n = int(si())
nums = list(map(int, si().split()))

dp = [0] * n
dp[0] = nums[0]

for i in range(1, n):
    dp[i] = max(nums[i], dp[i-1] + nums[i])

print(max(dp))


import numpy as np
import pylab as plt
import Neurons
import Networking
import Simulation
import Recorder
from scipy.optimize import curve_fit
import scipy.io as sio

t = 700
res = 1
stim_start = 100
stim_end = 450

def guss(x, a, b, c):
    return a * np.exp(-(x - b ** 2) / (2 * c ** 2))

base = np.arange(0, t, res)

net = Networking.network()
fr = Recorder.Voltage()

pos = 1

Nrns = np.empty((100, 100), dtype=type(net))
Nrns[0, 0] = net.create('FEF')
for i in range(np.size(Nrns, 0)):
    for j in range(np.size(Nrns, 1)):
        Nrns[i, j] = net.create('FEF')
        input = np.zeros(len(base))
        if pos == 1:
            pos_coef = guss(np.sqrt(i ** 2 + (np.size(Nrns, 1) - j) ** 2), 1, 0, 1) * 1
        elif pos == 2:
            pos_coef = guss(np.sqrt((np.size(Nrns, 0)/2 - i) ** 2 + j ** 2), 1, 0, 1) * 1
        elif pos == 3:
            pos_coef = guss(np.sqrt(i ** 2 + j ** 2), 1, 0, 1) * 1
        elif pos == 4:
            pos_coef = guss(np.sqrt(i ** 2 + (np.size(Nrns, 1) / 2 - j) ** 2), 1, 0, 1) * 1
        elif pos == 5:
            pos_coef = guss(np.sqrt(i ** 2 + (np.size(Nrns, 1) - j) ** 2), 1, 0, 1) * 1
        elif pos == 6:
            pos_coef = guss(np.sqrt((np.size(Nrns, 0)/2 - i) ** 2 + (np.size(Nrns, 1)/2 - j) ** 2), 1, 0, 1) * 1
        elif pos == 7:
            pos_coef = guss(np.sqrt((np.size(Nrns, 0) - i) ** 2 + (np.size(Nrns, 1) - j) ** 2), 1, 0, 1) * 1
        elif pos == 8:
            pos_coef = guss(np.sqrt(i ** 2 + (np.size(Nrns, 1) / 2 - j) ** 2), 1, 0, 1) * 1

        input[int(np.where(base == stim_start)[0]):int(np.where(base == stim_end)[0])] = np.hanning(
            len(input[int(np.where(base == stim_start)[0]):int(np.where(base == stim_end)[0])])) * pos_coef
        Nrns[i, j].set_input_current(np.copy(input))
Nrns = np.asmatrix(Nrns)

n_conn = 0
d = []
for i in range(np.size(Nrns, 0)):
    for j in range(np.size(Nrns, 1)):
        d.append(np.sqrt(i ** 2 + j ** 2))

d_list = np.unique(d)
n_trials = 1
distances = np.zeros((n_trials, len(d_list)))
weits = np.zeros((n_trials, len(d_list)))

# for x in range(n_trials):
#     distance = []
#     for i in range(np.size(Nrns, 0)):
#         print(i)
#         for j in range(np.size(Nrns, 1)):
#
#             for k in range(np.size(Nrns, 0)):
#                 for l in range(np.size(Nrns, 1)):
#                     if i != k or j != l:
#                         dist = np.sqrt((i - k) ** 2 + (j - l) ** 2)
#                         prob = dist / np.sqrt(np.size(Nrns, 0) ** 2 + np.size(Nrns, 1) ** 2)
#                         rand = np.random.normal()
#                         if rand >= prob and rand >= 0:
#                             w = (np.random.normal() + 0.75) * 0.35 * (1 - prob)
#                             net.connect(Nrns[i, j], Nrns[k, l], w)
#                             distances[x, np.where(d_list == dist)[0]] += 1
#                             weits[x, np.where(d_list == dist)[0]] = w

    # distances.append(distance)

indexes = sio.loadmat('indexes.mat')
i_indx = indexes['i']
j_indx = indexes['j']

for x in range(n_trials):
    distance = []
    for i in range(np.size(Nrns, 0)):
        print(i)
        for j in range(np.size(Nrns, 1)):

            for l in range(min(len(i_indx[i][j][0]), len(j_indx[i][j][0]))):

                net.connect(Nrns[i, j], Nrns[i_indx[i][j][0][l], j_indx[i][j][0][l]], 1)

m_distances = np.mean(distances, 0)
m_weits = np.mean(weits, 0)
params, pcov = curve_fit(guss, d_list[1:], m_distances[1:])

params_w, p_w = curve_fit(guss, d_list[1:], m_weits[1:])

fr.record_from(Nrns)

simul = Simulation.simulation()
simul.set_resolution(res)
simul.run(net, t, [fr])

v, t_1 = fr.get_result()
plt.figure()

plt.plot(t_1, v[0, 0], label='Neuron')# + str(i+1))
plt.plot(t_1, v[-1, -1], label='Neuron')

plt.xlim([0, t])
plt.legend()
plt.xlabel('time (ms)')
plt.ylabel('Firing rate (Hz)')

plt.show()


import streamlit as st

title = "Conclusion"
sidebar_name = "Conclusion"

def run():

    st.title(title)

    st.markdown(
        """

        ---

        > **"En Formule 1, chance et malchance n'existent pas.**
        > **Cette dernière n'est autre que la somme d'éléments ou de situations que nous n'avons pas su ou pu prévoir",** _Enzo Ferrari_

        ---

        ## Objectif du projet

        Le but du projet était de prédire le gagnant et le top 3 des courses de Formule 1, de parier et voir si nos pronostics étaient gagnants.
        Nous avons même poussé l'exercice jusqu'à prédire l'intégralité du classement sur une saison complète.

        ---

        ## Résumé

        #### Vainqueur de la course

        - 5 modèles : Régression Logistique, Forêt aléatoire, Arbre de décision, SVC, KNN
        - f1-score : de 0.31 (KNN) à 0.55 (régression logistique)
        - ROI des paris associés : -10,5 % (forêt aléatoire) à 16 % (régression logistique)
        - _Rappel : le ROI moyen pour des paris en France : -17%_
        - _Les parieurs "pro" annoncent un ROI de 10%_

        ---
        #### Top 3

        - 3 modèles : Régression Logistique, Forêt aléatoire, Arbre de décision
        - f1-score : de 0.62 (forêt aléatoire & régression logistique) à 0.69 (arbre de décision)
        - ROI des paris associés :  -10 % (régression logistique), 48 % (forêt aléatoire) et **72 % (arbre de décision)**
        - _Rappel : le ROI moyen pour des paris en France : -17%_
        - _Les parieurs "pro" annoncent un ROI de 10%_

        ---

        La différence entre deux modèles se fait surtout sur les prévisions des outsiders. Les deux premiers étant souvent correctement prédits.

        ---

        #### Classement complet sur une saison

        Cette partie relève plus du test qu'autre chose, en France on ne peut pas parier sur un classement final complet. On peut uniquement parier sur
        le vainqueur ou des "faces à faces" (est-ce que le pilote 1 finira devant le pilote 2?) et nous n'avons pas pu obtenir les côtes associées
        à ces paris.

        - 4 modèles : Régression Logistique, Forêt aléatoire, Arbre de décision, Linear SVC
        - Ecart de points moyen : 10.5 (Arbre de décision) à 88 (LinearSVC)
        - Ecart de classement moyen :  0.4 (Arbre de décision) à 4 (LinearSVC)

        ---

        ## Perspectives et évolutions possibles

        L'utilisation de SHAP nous a montré que certaines features n'étaient pas pertinentes.
        - Supprimer les données météo sauf l'info "pluie".
        - La classe du circuit n'a pas d'impact en l'état.

        Compléter le jeu de données :
        - Echanger avec un professionnel du métier.
        - Utiliser la librairie fastf1 pour l'enrichir (vitesse max, vitesse moyenne dans les virages, régularité du pilote, etc.).
          - La classe du circuit aura peut-être alors un intérêt.
        - Créer un indicateur "forme du moment" ou "efficacité de la stratégie".
        - Budget de l'écurie ou le salaire du pilote.
        - Différencier pilote principal d'une écurie et deuxième pilote.
        - Probabilité d'apparitions de la Safety Car sur le circuit.
        - ...

        Concernant les modèles :
        - Essayer d'autres modèles évidemment.
        - Utiliser une combinaison de plusieurs modèles.
        - Utiliser un réseau de neurones.
        - ...

        Ce projet semble montrer que l'utilisation de machine learning est une piste intéressante pour battre les bookmakers dans le domaine de la F1.

        """, unsafe_allow_html=True
    )


from spyre import server

import pandas

class Tables(server.App):
    title = "VHI index"

    inputs = [{ "type" : 'dropdown',
                "label" : 'index',
                "options" : [{"label": 'VHI', "value": 'VHI'},
                            {"label" : 'SMN', "value": 'SMN'},
                            {"label" : 'SNT', "value": 'SNT'}],
                "key" : 'index',
                "action_id" : "update_data"},

                { "type" : 'dropdown',
                "label" : 'color',
                "options" : [{"label": 'DarkGreen', "value": 'DarkGreen'},
                            {"label" : 'DarkBlue', "value": 'DarkBlue'},],
                "key" : 'color',
                "action_id" : "update_data"},

              { "type" : 'dropdown',
                "label" : 'region',
                "options" : [{"label": 'Kyiv', "value": '1KYV'},
                            {"label" : 'Lviv', "value": '2LV'},
                            {"label" : 'Odesa', "value": '3ODS'}],
                "key" : 'region',
                "action_id" : "update_data"},

              ###{ "type":'text',
              #  "label": 'year(from 1990-2010)',
              #  "value" : '2000',
              #  "key": 'year',
              #  "action_id" : "update_data"},'''

              { "type":'text',
                "label": 'first weak(from 1-40)',
                "value" : '1',
                "key": 'weak1',
                "action_id" : "update_data"},

              { "type":'text',
                "label": 'last weak(from 1-40)',
                "value" : '40',
                "key": 'weak2',
                "action_id" : "update_data",},

              { "type": 'slider',
                "label":'year',
                "key":"year" ,
                "min": "1990",
                "max": "2010",
                "action_id": "update_data"}]

    controls = [{   "type" : "hidden",
                    "id" : "update_data"}]

    tabs = ["Plot"]

    outputs = [{"type": "plot",
                "id": "plot",
                "tab": "Plot",
                "control_id": 'update_data'}]

    def getData(self, params):
        region = params['region']
        print(region[0])
        path = "vhi_id_"+region[0]+"_2017-02-26.csv"
        df = pandas.read_csv(path,index_col=False,header=0)
        return df

    def getPlot(self, params):
        df = self.getData(params)
        df = df[df['year'] == int(params['year'])]
        df = df[(df['weak'] <= int(params['weak2']))&(df['weak'] >= int(params['weak1']))]

        plt_obj = df.plot(x='weak',y=params['index'],color=params['color'])
        plt_obj.set_ylabel(params['index'])
        plt_obj.set_xlabel('weak')
        fig = plt_obj.get_figure()
        return fig

app = Tables()
app.launch()


import datetime
import hashlib

from localstack.utils.strings import to_bytes

from .events import Event, EventHandler, EventMetadata, EventPayload
from .metadata import get_session_id

def get_hash(value) -> str:
    max_length = 10
    digest = hashlib.sha1()
    digest.update(to_bytes(str(value)))
    result = digest.hexdigest()
    return result[:max_length]

class EventLogger:
    """
    High-level interface over analytics event abstraction. Expose specific event types as
    concrete functions to call in the code.
    """

    def __init__(self, handler: EventHandler, session_id: str = None):
        self.handler = handler
        self.session_id = session_id or get_session_id()

    @staticmethod
    def hash(value):
        return get_hash(value)

    def event(self, event: str, payload: EventPayload = None, **kwargs):
        if kwargs:
            if payload is None:
                payload = kwargs
            else:
                raise ValueError("either use payload or set kwargs, not both")

        self._log(event, payload=payload)

    def _log(self, event: str, payload: EventPayload = None):
        self.handler.handle(Event(name=event, metadata=self._metadata(), payload=payload))

    def _metadata(self) -> EventMetadata:
        return EventMetadata(
            session_id=self.session_id,
            client_time=str(datetime.datetime.now()),
        )


def prob_at_least_N(k: int, N: int):
    # All AaBb-XXXX matings always has a 25% probability of AaBb offspring (Punnett square).
    # This is a binomial experiment;
    # We are looking for the probability of at least N 'successes' (AaBb) with 2^k trials and success rate of 25%.
    # p(N ≥ AaBb)
    # This is equal to 1 minus the probability of less than N AaBb with 2^k and success rate of 25%
    # p(N ≥ AaBb) = 1 - p(N < AaBb)
    # p(N ≥ AaBb) = 1 - p(N - 1 ≤ AaBb)   [Note that N is a natural number]
    # The right hand side can be nicely expressed by the binomial cumulative distribution function (bcdf)
    # p(N ≥ AaBb) = 1 - bcdf(N - 1, 2^k, p)

    p_AaBb = 0.25

    from scipy.stats import binom
    return 1 - binom.cdf(N - 1, 2**k, p_AaBb)

sample_input = '5 9'
if __name__=='__main__':
    k, N = (int(i) for i in sample_input.split(' '))
    print(prob_at_least_N(k, N))


import json
import random
import requests
import time
import sys
import signal
import argparse

def make_request(url, useragents):
    try:
        if not url.startswith('http'):
            url = 'https://' + url

        headers = {
            'User-Agent': random.choice(useragents)
        }

        response = requests.get(url, timeout=1, headers=headers)
        response.raise_for_status()
        print(f'{url} - {response.status_code}')

    except requests.exceptions.RequestException as e:
        if e.response is not None:
            print(f'{url} - connection error: {e.response.status_code}')
        else:
            print(f'{url} - unknown connection error')

    return url

def signal_handle(sig, frame):
    print('Ctrl+C or system exit catched, stopping...')
    sys.exit(0)

def main(args):
    with open(args.websites, 'r') as w:
        websites = json.load(w)

    with open(args.useragents, 'r') as u:
        useragents = json.load(u)

    signal.signal(signal.SIGINT, signal_handle)
    signal.signal(signal.SIGTERM, signal_handle)

    if args.invisible:
        print('notice: invisible mode enabled, requests will be sent with a large interval')

    while True:
        url = random.choice(websites)
        make_request(url, useragents)

        def_int = random.uniform(0.5, 3.0)
        inv_int = random.uniform(3.7, 12.0)

        if args.invisible:
            time.sleep(inv_int)
        else:
            time.sleep(def_int)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='sitesounds is a simple Python script that make a web traffic noise')
    parser.add_argument('-w', '--websites', metavar='file', nargs='?', default='websites.json', help='the name of the JSON file containing website URLs')
    parser.add_argument('-u', '--useragents', metavar='file', nargs='?', default='useragents.json', help='the name of the JSON file containing useragents')
    parser.add_argument('-i', '--invisible', action='store_true', help='enable less suspicious mode by increasing requests interval')
    args = parser.parse_args()

    main(args)


import os, sys

# if len(sys.argv) != 8:
#     print
#     print "CFG     = str(sys.argv[1])"
#     print "ANAFILE = str(sys.argv[2])"
#     print "PGRFILE = str(sys.argv[3])"
#     print "INFILE1 = str(sys.argv[4])"
#     print "INFILE2 = str(sys.argv[5])"
#     print "OUTDIR  = str(sys.argv[6])"
#     print
#     sys.exit(1)

BASE_PATH = os.path.realpath(__file__)
BASE_PATH = os.path.dirname(BASE_PATH)
sys.path.insert(0,BASE_PATH)

# CFG     = str(sys.argv[1])
# ANAFILE = str(sys.argv[2])
# PGRFILE = str(sys.argv[3])
# INFILE1 = str(sys.argv[4])
# INFILE2 = str(sys.argv[5])
# OUTDIR  = str(sys.argv[6])

if True:
    # hack for dev
    CFG = "cfg/for_pressnet.cfg"
    ANAFILE = "test_ana.root"
    PGRFILE = "test_pgraph.root"
    INFILE1 = "../../../testdata/mcc9v12_intrinsicoverlay/supera-Run004955-SubRun000079.root"
    INFILE2 = "../../../testdata/mcc9v12_intrinsicoverlay/taggeroutv2-larcv-Run004955-SubRun000079.root"
    OUTDIR  = "."

from larcv import larcv
from ublarcvapp import ublarcvapp
import ROOT
from ROOT import std

proc = larcv.ProcessDriver('ProcessDriver')
proc.configure(CFG)
flist=ROOT.std.vector('std::string')()
flist.push_back(ROOT.std.string(INFILE1))
flist.push_back(ROOT.std.string(INFILE2))
proc.override_input_file(flist)

proc.override_ana_file(os.path.join(OUTDIR,ANAFILE))
proc.override_output_file(os.path.join(OUTDIR,PGRFILE))
proc.initialize()
proc.batch_process()
proc.finalize()

sys.exit(0)


#
# @lc app=leetcode id=83 lang=python3
#
# [83] Remove Duplicates from Sorted List
#
# https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/
#
# algorithms
# Easy (42.14%)
# Total Accepted:    315.6K
# Total Submissions: 747.1K
# Testcase Example:  '[1,1,2]'
#
# Given a sorted linked list, delete all duplicates such that each element
# appear only once.
#
# Example 1:
#
#
# Input: 1->1->2
# Output: 1->2
#
#
# Example 2:
#
#
# Input: 1->1->2->3->3
# Output: 1->2->3
#
#
#
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # def deleteDuplicates(self, head: ListNode) -> ListNode:
    #     current = head
    #     while current:
    #         while current.next and current.next.val == current.val:
    #             current.next = current.next.next
    #         current = current.next
    #     return head

    def deleteDuplicates(self, head: ListNode) -> ListNode:
        current = head
        while current and current.next:
            if current.next.val == current.val:
                current.next = current.next.next
            else:
                current = current.next
        return head


from os import getenv

from benchbuild.project import Project
from benchbuild.environments import container
from benchbuild.source import Git
from benchbuild.utils.cmd import make

class LevelDB(Project):
    NAME: str = 'leveldb'
    DOMAIN: str = 'database'
    GROUP: str = 'benchbuild'
    SOURCE = [
        Git(remote='https://github.com/google/leveldb',
            local='leveldb.src',
            limit=5,
            refspec='HEAD')
    ]
    CONTAINER = container.Buildah().from_('debian:buster-slim')

    def compile(self):
        leveldb_repo = local.path(self.source_of('leveldb.src'))

        clang = compiler.cc(self)
        clang_cxx = compiler.cxx(self)

        with local.cwd(leveldb_repo):
            with local.env(CXX=str(clang_cxx), CC=str(clang)):
                make_ = run.watch(make)
                make_("clean")
                make_("all", "-i")

    def run_tests(self):
        """
        Execute LevelDB's runtime configuration.

        Args:
            experiment: The experiment's run function.
        """
        leveldb_repo = local.path(self.source_of('leveldb.src'))

        leveldb = wrapping.wrap(leveldb_repo / "out-static" / "db_bench", self)
        leveldb = run.watch(leveldb)
        with local.env(LD_LIBRARY_PATH="{}:{}".format(
                leveldb_repo / "out-shared", getenv("LD_LIBRARY_PATH", ""))):
            leveldb()


from enum import Enum
from os.path import abspath, join
from json import load

# ENUMERATIONS FOR OBJECT ATTRIBUTES
Size = Enum("Size", "small large")
Color = Enum("Color", "gray blue brown yellow red green purple cyan")
Material = Enum("Material", "rubber metal")
Shape = Enum("Shape", "cube sphere cylinder")

# OBJECT WRAPPER
class CLEVRObject:
    def __init__(self, jsonRep):
        # pull out spatial attributes from jsonRep
        self.real_coordinates = tuple(jsonRep["3d_coords"])
        self.pixel_coordinates = tuple(jsonRep["pixel_coords"])
        self.rotation = jsonRep["rotation"]

        # otherwise, stick all the attributes in a dict
        self.attributes = {
            "size" : Size[jsonRep["size"]],
            "color" : Color[jsonRep["color"]],
            "material" : Material[jsonRep["material"]],
            "shape" : Shape[jsonRep["shape"]]
        }

    def __str__(self):
        size = self.attributes["size"].name
        color = self.attributes["color"].name
        material = self.attributes["material"].name
        shape = self.attributes["shape"].name
        return f"{size} {color} {material} {shape}"

# ENUMERATIONS FOR SCENE ATTRIBUTES
Split = Enum("Split", "train test val")

# UTILITIES FOR SCENE CONSTRUCTION
def assocListToRelation(assoc):
    for src, dests in enumerate(assoc):
        for dest in dests:
            yield (src, dest)

# SCENE WRAPPER
class Scene:
    def __init__(self, jsonRep):
        # pulling some attributes out is straightforward
        self.split = Split[jsonRep["split"]]
        self.index = jsonRep["image_index"]
        self.filename = jsonRep["image_filename"]
        # not sure what these are right now, but ok
        # self.directions = {
        #     k : tuple(jsonRep["directions"][k])
        #         for k in ("left", "right", "front", "behind", "below", "above")
        # }
        # the objects
        self.objects = [
            CLEVRObject(objRep) for objRep in jsonRep["objects"]
        ]
        # the relations, storing references to the objects
        self.relations = {
            k : [
                (self.objects[src], self.objects[dest])
                    for src, dest in assocListToRelation(jsonRep["relationships"][k])
            ]
            for k in ("left", "right", "front", "behind")
        }

# DATABASE CONNECTION
class Database:
    def __init__(self, rootPath):
        self._root = abspath(rootPath)
        # load the scenes
        with open(join(self._root, "scenes", "CLEVR_train_scenes.json")) as f:
            self.train = [Scene(s) for s in load(f)["scenes"]]
        with open(join(self._root, "scenes", "CLEVR_val_scenes.json")) as f:
            self.validation = [Scene(s) for s in load(f)["scenes"]]


from django.http import HttpResponse
from django.shortcuts import render, reverse
from datetime import datetime
from os import listdir

def home_view(request):
    template_name = 'app/home.html'
    pages = {
        'Главная страница': reverse('home'),
        'Показать текущее время': reverse('time'),
        'Показать содержимое рабочей директории': reverse('workdir')
    }
    context = {
        'pages': pages
    }
    return render(request, template_name, context)

def time_view(request):
    current_time = datetime.now()
    msg = f'Текущее время: {current_time}'
    return HttpResponse(f"
Текущее время: {msg}

Home")

def workdir_view(request):
    path = f'./'
    files = listdir(path=path)
    return HttpResponse(f"
Список файлов в директории:

{files}
Home")


import typing
"""
Problem1:

Directed Graph, detected if there is a cycle
#https://leetcode.com/problems/course-schedule/

use dfs : keep track of cur_vertex, visited, recursion stack, if cur_vertex already in recursion_stack , then there is a cycle

"""

"""
ifhascycle(cur_vertex, visited, recursion_stack): return if could detect a cycle starting dfs from "cur_vertex"
peudo-code as follows:
"""
numOfVertices = 10
graph = {i:[] for i in range(numOfVertices)}
def ifhascycle(cur_vertex, visited, recursion_stack):
    if cur_vertex not in recursion_stack and not visited[cur_vertex]:
        recursion_stack.add(cur_vertex)
    elif cur_vertex in recursion_stack:
        return True
    else:  # elif i already visited, then return False
        return False

    for neighbor in graph[cur_vertex]:
        if ifhascycle(neighbor, visited, recursion_stack):
            return True
    recursion_stack.remove(cur_vertex)  #####this step is important!!!!Has to remove the cur_vertex from recursion_stack
    visited[cur_vertex] = True
    return False

def graphHasCycle(graph):
    recursion_stack = set()
    visited = [False for _ in range(numOfVertices)]
    for idx in range(numOfVertices):
        if not visited[idx] and ifhascycle(idx, visited, recursion_stack):
            return False
    return True

"""
problem 2: output all the courses in a correct order
topological sort (BFS)
https://leetcode.com/problems/course-schedule-ii/submissions/

"""
"""
topological sort (Kahn's algorithm: BFS)
step 1: put all the courses that do not have any required courses into the queue, visited.add()
step 2: do the BFS(Note: only if all the prerequisite courses have been added to outputResult,
        then we can add this course to output).
"""
def findOrder(numCourses: int, prerequisites: List[List[int]]) -> List[int]:
    requires = [[] for _ in range(numCourses)] ##key: prerequisite
    requiredBy = [set() for _ in range(numCourses)] ##key: courses that have prerequisite
    for course, prereq in prerequisites:
        requires[prereq].append(course)  #[2,3] must take 3 before taking 2  require[3] = [2]
        requiredBy[course].add(prereq)   #requiredBy[2] = [3]

    toProcess = deque()
    for course, prereqs in enumerate(requiredBy):
        if not prereqs:
            toProcess.append(course)

    outputOrder = []
    while toProcess:
        prereq = toProcess.popleft()
        outputOrder.append(prereq)
        for course in requires[prereq]:
            requiredBy[course].remove(prereq)
            if not requiredBy[course]:
                """
                only if all the prerequisite courses have been added to outputOrder, then we can add this course to process
                """
                toProcess.append(course)

    return outputOrder if len(outputOrder) == numCourses else []

"""
topological sort (DFS)
"""
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
    descendants = [[] for _ in range(numCourses)]
    for course, prereq in prerequisites:
        descendants[course].append(prereq)

    orderList = []
    def visit(visited, recursion_stack, cur_node):
        nonlocal orderList
        if visited[cur_node]:
            return True

        if cur_node in recursion_stack:
            return False

        recursion_stack.add(cur_node)  ###mark cur_node as a temporary mark

        for des in descendants[cur_node]:
            if not visit(visited, recursion_stack, des):
                return False

        recursion_stack.remove(cur_node)  ###remove temporary mark
        visited[cur_node] = True
        orderList.append(cur_node)
        return orderList

    visited = [False for _ in range(numCourses)]
    for idx in range(numCourses):
        if not visited[idx]:
            if not visit(visited, set(), idx):
                return []
    return orderList


import math
from tensorflow.keras import backend as K

from training.training_config import TrainingSetup

class LrTweaker:
    """Component for adjusting the learning rate of a model."""

    def __init__(
        self,
        training_config: TrainingSetup,
        patience: int,
        decrease_multiplier: float,
        min_lr: float,
    ):
        self.training_config: TrainingSetup = training_config
        self.patience: int = patience
        self.decrease_multiplier: float = decrease_multiplier
        self.min_lr: float = min_lr

        self.best: float = math.inf
        self.curr_wait: int = 0

    def on_epoch_end(self, loss: float) -> None:
        if loss < self.best:
            self.best = loss
            self.curr_wait = 0
        else:
            self.curr_wait += 1

        if self.curr_wait >= self.patience:
            self.curr_wait = 0
            old_lr = self.training_config.p.learning_rate
            new_lr = max(old_lr * self.decrease_multiplier, self.min_lr)

            self.training_config.p.learning_rate = new_lr
            K.set_value(self.training_config.model.optimizer.learning_rate, new_lr)


oldlist = [23,45,45,67,89,71,23,34,45]

newlist = []

for val in oldlist:
    if val not in newlist:
        newlist.append(val)
print(newlist)

newdictionary = {}

for val in oldlist:
    newdictionary[val] =0

for key,value in newdictionary:
    print(key,end=" ")
    print(value,end=" ")
print()

# {23:0,45:0,67:0,89:0,71:0,34:0}


import BinaryTree as Tree
import pickle

class ListMenu():
    def __init__(self):
        self.BTree = Tree.DSASearchTree()

    def unPickle(self):
        exitLoop = False

        while exitLoop == False:
            fileName = input("Exit (x) or Enter a filename: ")
            if fileName == "x":
                exitLoop = True
            else:
                try:
                    with open(fileName,"rb") as dataFile:
                        inObject = pickle.load(dataFile)
                    inObject.postorder()
                    print("\n")
                    for items in inObject.linkedL:
                        self.BTree.insert(items, items)
                    exitLoop = True
                except:
                    print("Error: Object file does not exist")

    def pickle(self, fileName):
        try:
            with open(fileName, "wb") as dataFile:
                pickle.dump(self.BTree,dataFile)
        except:
            print("Error: problem pickling object")

    def csvRead(self):
        fileName = input("Enter Filename: \n")
        try:
            dataFile = open(fileName, "r")
            for item in dataFile.readline().split(","):
                self.BTree.insert(item, item)
        except Exception as e:
            print("Error: Object File does not exist ", e)

    def csvWrite(self, fileName):
        distype = ""
        while distype != "pr" and distype != "po" and distype != "in":
            distype = input("preorder (pr), postorder (po) or inorder (in)?\n")

        if distype == "pr":
            self.BTree.preorder()
        elif distype == "po":
            self.BTree.postorder()
        elif distype == "in":
            self.BTree.inorder()

        #for i in self.BTree.linkedL:
        #    print(i)

        try:
            with open(fileName,"w") as dataFile:
                for i in self.BTree.linkedL:
                  dataFile.write(i + ",")
        except Exception as e:
            print("Error writing to file", e)

        self.BTree.cleanLinkedList()

    def dispayList(self):
        distype = input("preorder (pr), postorder (po) or inorder (in)?\n")
        print("Displaying List... \n")
        if distype == "pr":
            self.BTree.preorder()
            self.BTree.cleanLinkedList()
        elif distype == "po":
            self.BTree.postorder()
            self.BTree.cleanLinkedList()
        elif distype == "in":
            self.BTree.inorder()
            self.BTree.cleanLinkedList()
        print("\n ...Display Complete \n")

    def addValue(self, i):
        self.BTree.insert(i, i)

    def removeValue(self, i):
        self.BTree.delete(i)

    def getBalance(self):
        print(self.BTree.balance(), "\n\n")

    def getHeight(self):
        print(self.BTree.height(), "\n\n")

    def interactiveMenu(self):
        exitLoop = False
        while exitLoop == False:
            usrInput = ""
            while usrInput != "r" and usrInput != "w" and usrInput != "d" and usrInput != "a" and usrInput != "rm" and usrInput != "b" and usrInput != "h" and usrInput != "x":
                usrInput = input("#####\nRead File (r)\nWrite File (w) \nDisplay (d)\nadd value(a)\nRmove Value (rm)\nBalance (b)\nHeight (h)\nExit Program(x)\n#####\n")
            if usrInput == "x":
                exitLoop = True
            elif usrInput == "w":
                filetype = input("Csv file or Serialised file? (c/s)")
                fileName = input("Enter filename: ")
                if filetype == "s":
                    self.pickle(fileName)
                elif filetype == "c":
                    self.csvWrite(fileName)
            elif usrInput == "r":
                filetype = input("Csv file or Serialised file? (c/s)")
                if filetype == "s":
                    self.unPickle()
                elif filetype == "c":
                    self.csvRead()
            elif usrInput == "d":
                self.dispayList()
            elif usrInput == "rm":
                value = input("enter a value: \n")
                self.removeValue(value)
            elif usrInput == "a":
                value = input("enter a value \n")
                self.addValue(value)
            elif usrInput == "b":
                self.getBalance()
            elif usrInput == "h":
                self.getHeight()

menu = ListMenu()
menu.interactiveMenu()


import numpy as np
import matplotlib.pyplot as plt
import gym
from arp.arp import ARProcess
import time

class SquareEnvironment(gym.core.Env):
    def __init__(self,
                 size=10,
                 target_size = 0.5,
                 dt=0.1,
                 n_steps=10000,
                 visualize=False,
                 velocity_lim = 1.0):
        plt.ion()
        self.size = size
        self.n_steps = n_steps
        self.target_size = target_size
        self.velocity = np.zeros(2)
        self.pos = np.zeros(2)
        self.dt = dt
        self.visualize = visualize
        self.time = 0
        self.velocity_lim = velocity_lim
        self.trajectory = []
        self.env = self
        self.current_steps = 0
        from gym.spaces import Box as GymBox
        Box = GymBox
        self._observation_space = Box(
        low= -np.ones(6),
        high=np.ones(6)
        )
        self._action_space = Box(low=-np.ones(2), high=np.ones(2))
        self.fig = None
        if self.visualize:
            plt.ion()
            self.fig = plt.figure(figsize=(6,6))
            self.ax = self.fig.add_subplot(111)
            self.hl_target, = self.ax.plot([], [], markersize=25, marker="o", color='r')
            self.hl_agent, = self.ax.plot([], [], markersize=10, marker="o", color='b')
            self.hl, = self.ax.plot([], [])
            self.ax.set_xticks([])
            self.ax.set_yticks([])
            self.ax.set_title("Agent Trajectory")

    def step(self, action):
        self.current_steps += 1
        self.velocity = np.clip(action, - self.velocity_lim, self.velocity_lim).flatten()
        self.pos += self.velocity * self.dt
        clipped_pos = np.clip(self.pos, -self.size/2, self.size/2)
        self.velocity[clipped_pos!=self.pos] = 0
        self.pos = clipped_pos
        reward = np.linalg.norm(self.pos - self.target_pos) < self.target_size
        done = reward > 0 or self.current_steps >= self.n_steps
        reward -= self.dt
        self.time += self.dt
        if self.visualize:
            self.trajectory.append(self.pos)
            self.hl_target.set_xdata(self.target_pos[0])
            self.hl_target.set_ydata(self.target_pos[1])
            self.hl_agent.set_xdata(self.pos[0])
            self.hl_agent.set_ydata(self.pos[1])
            self.hl.set_xdata(np.array(self.trajectory)[:, 0])
            self.hl.set_ydata(np.array(self.trajectory)[:, 1])
            self.ax.set_ylim([-self.size/2, self.size/2])
            self.ax.set_xlim([-self.size/2, self.size/2])
            time.sleep(0.02)
            self.fig.canvas.draw()
            self.fig.canvas.flush_events()
        new_ob = np.hstack([self.pos * 2/self.size, self.velocity, (self.target_pos - self.pos) * 2/self.size])
        return new_ob, reward, done, {}

    def reset(self):
        self.velocity = np.zeros(2)
        self.pos = np.zeros(2)
        self.current_steps = 0
        self.time = 0
        self.trajectory = []
        self.target_pos = 2 * np.random.random(size = (2,)) - 1
        self.target_pos /= np.linalg.norm(self.target_pos)
        self.target_pos *= self.size/4
        new_ob = np.hstack([self.pos, self.velocity, self.target_pos])
        return new_ob

    @property
    def observation_space(self):
        return self._observation_space

    @property
    def action_space(self):
        return self._action_space

if __name__ == "__main__":
    plt.ion()
    env = SquareEnvironment(visualize=True)
    ob = env.reset()
    p = 3
    alpha = 0.8
    ar = ARProcess(p=p, alpha=alpha, size=env.action_space.shape[-1])
    ar.reset()
    steps = 0
    while steps < 10000:
        x, _ = ar.step()
        ob, reward, done, _ = env.step(x)
        if done:
            env.reset()
            ar.reset()


from typing import Any, Dict, List, Optional, Tuple
import copy
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader

#from data import MNISTDataset, FederatedSampler
#from models.models import CNN, MLP
from models.models import CNN, MLP
from utils import arg_parser, average_weights, Logger
from data.mnist import MNISTDataset
from data.sampler import FederatedSampler
from core.sampler_builder import get_sampler
from core.client_selection import ClientSelector

class FedAvg:
    """Implementation of FedAvg
    http://proceedings.mlr.press/v54/mcmahan17a/mcmahan17a.pdf
    """

    def __init__(self, args: Dict[str, Any]):
        self.args = args
        self.device = torch.device(
            f"cuda:{args.device}" if torch.cuda.is_available() else "cpu"
        )
        self.logger = Logger(args)

        self.train_loader, self.test_loader = self._get_data(
            root=self.args.data_root,
            n_clients=self.args.n_clients,
            n_shards=self.args.n_shards,
            non_iid=self.args.non_iid,
            sample_type=self.args.sample_type
        )

        if self.args.model_name == "mlp":
            self.root_model = MLP(input_size=784, hidden_size=128, n_classes=10).to(
                self.device
            )
            self.target_acc = 0.97
        elif self.args.model_name == "cnn":
            self.root_model = CNN(n_channels=1, n_classes=10).to(self.device)
            self.target_acc = 0.99
        else:
            raise ValueError(f"Invalid model name, {self.args.model_name}")

        self.reached_target_at = None  # type: int

    def _get_data(self,
            root: str,
            n_clients: int,
            n_shards: int,
            non_iid: int,
            sample_type: str
    ) -> Tuple[DataLoader, DataLoader]:
        """
        Args:
            root (str): path to the dataset.
            n_clients (int): number of clients.
            n_shards (int): number of shards.
            non_iid (int): 0: IID, 1: Non-IID
            sample_type (int): federated, uniform, group, responsive

        Returns:
            Tuple[DataLoader, DataLoader]: train_loader, test_loader
        """
        global sampler, train_loader

        train_set = MNISTDataset(root=root, train=True)
        test_set = MNISTDataset(root=root, train=False)

        sampler = FederatedSampler(
                train_set,
                non_iid=non_iid,
                n_clients=n_clients,
                n_shards=n_shards
        )

        # if sample_type == 'federated':
        #     sampler = FederatedSampler(
        #         train_set,
        #         non_iid=non_iid,
        #         n_clients=n_clients,
        #         n_shards=n_shards
        #     )
        #     train_loader = DataLoader(train_set, batch_size=128, sampler=sampler)
        # elif sample_type == 'uniform':
        #     sampler = get_sampler(
        #         sample_strategy=sample_type,
        #         client_num=n_clients,
        #     )
        #     train_loader = DataLoader(train_set,
        #                               batch_size=128,
        #                               sampler=sampler.sample(size=n_clients))
        # elif sample_type == 'group':
        #     sampler = get_sampler(
        #         sample_strategy=sample_type,
        #         client_num=n_clients,
        #     )
        #     train_loader = DataLoader(train_set,
        #                               batch_size=128,
        #                               sampler=sampler.sample(size=n_clients, shuffle=True))
        # elif sample_type == 'responsiveness':
        #     sampler = get_sampler(
        #         sample_strategy=sample_type,
        #         client_num=n_clients,
        #     )
        #     train_loader = DataLoader(train_set, batch_size=128, sampler=sampler.sample(size=n_clients))

        # train_loader = DataLoader(train_set, batch_size=128, sampler=sampler)
        train_loader = DataLoader(train_set, batch_size=128, sampler=sampler)
        test_loader = DataLoader(test_set, batch_size=128)

        return train_loader, test_loader

    def _train_client(
        self, root_model: nn.Module, train_loader: DataLoader, client_idx: int
    ) -> Tuple[nn.Module, float]:
        """Train a client model.

        Args:
            root_model (nn.Module): server model.
            train_loader (DataLoader): client data loader.
            client_idx (int): client index.

        Returns:
            Tuple[nn.Module, float]: client model, average client loss.
        """
        model = copy.deepcopy(root_model)
        model.train()
        optimizer = torch.optim.SGD(
            model.parameters(), lr=self.args.lr, momentum=self.args.momentum
        )

        for epoch in range(self.args.n_client_epochs):
            epoch_loss = 0.0
            epoch_correct = 0
            epoch_samples = 0

            for idx, (data, target) in enumerate(train_loader):
                data, target = data.to(self.device), target.to(self.device)
                optimizer.zero_grad()

                logits = model(data)
                loss = F.nll_loss(logits, target)
                loss.backward()
                optimizer.step()

                epoch_loss += loss.item()
                epoch_correct += (logits.argmax(dim=1) == target).sum().item()
                epoch_samples += data.size(0)

            # Calculate average accuracy and loss
            epoch_loss /= idx
            epoch_acc = epoch_correct / epoch_samples

            print(
                f"Client #{client_idx} | Epoch: {epoch}/{self.args.n_client_epochs} | Loss: {epoch_loss} | Acc: {epoch_acc}",
                end="\r",
            )

        return model, epoch_loss / self.args.n_client_epochs

    def train(self) -> None:
        """Train a server model."""
        train_losses = []
        global idx_clients

        for epoch in range(self.args.n_epochs):
            clients_models = []
            clients_losses = []

            # Randomly select clients
            m = max(int(self.args.frac * self.args.n_clients), 1)
            if self.args.sample_type == 'random':
                # idx_clients = np.random.choice(range(self.args.n_clients), m, replace=False)
                selector = ClientSelector(self.args.n_clients)
                idx_clients = selector.random_selection(m)
            elif self.args.sample_type == 'replacement':
                selector = ClientSelector(self.args.n_clients)
                idx_clients = selector.random_selection_with_replacement(m)
            elif self.args.sample_type == 'stratified':
                labels = np.random.randint(1, 10, self.args.n_clients)
                selector = ClientSelector(self.args.n_clients)
                idx_clients = selector.stratified_sampling(m, labels)
            elif self.args.sample_type == 'active-learning':
                uncertainty_scores = np.random.rand(self.args.n_clients)
                selector = ClientSelector(self.args.n_clients)
                idx_clients = selector.active_learning(m, uncertainty_scores)
            elif self.args.sample_type == 'cohort':
                cohort_labels = np.random.choice(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'],
                                                 self.args.n_clients)
                selector = ClientSelector(self.args.n_clients)
                idx_clients = selector.cohort_selection(m, cohort_labels)
            elif self.args.sample_type == 'rank':
                client_features = np.random.rand(self.args.n_clients)
                client_performance = np.random.rand(self.args.n_clients)
                selector = ClientSelector(self.args.n_clients)
                idx_clients = selector.learning_to_rank_selection(m, client_features, client_performance)
            elif self.args.sample_type == 'budget':
                budget = 100  # Budget for communication or computation
                client_costs = np.random.randint(1, 10, self.args.n_clients)
                selector = ClientSelector(self.args.n_clients)
                idx_clients = selector.budget_constrained_selection(budget, client_costs)
            elif self.args.sample_type == 'reputation':
                reputation_scores = np.random.rand(self.args.n_clients)
                selector = ClientSelector(self.args.n_clients)
                idx_clients = selector.reputation_selection(m, reputation_scores)
            elif self.args.sample_type == 'priority':
                priority_scores = np.random.rand(self.args.n_clients)
                selector = ClientSelector(self.args.n_clients)
                idx_clients = selector.priority_selection(m, priority_scores)
            elif self.args.sample_type == 'weighted':
                # Generate random client weights (for example, based on data size or data quality)
                client_weights = np.random.uniform(0.1, 1.0, self.args.n_clients)
                selector = ClientSelector(self.args.n_clients)
                idx_clients = selector.weighted_sampling(m, client_weights)
            elif self.args.sample_type == 'bias-correction':
                # Generate random client data sizes (e.g., based on the number of data samples each client has)
                client_data_sizes = np.random.randint(100, 1000, self.args.n_clients)
                selector = ClientSelector(self.args.n_clients, client_data_sizes)
                idx_clients = selector.bias_correction_selection(m, client_data_sizes)

            # Train clients
            self.root_model.train()

            for client_idx in idx_clients:
                # Set client in the sampler
                self.train_loader.sampler.set_client(client_idx)

                # Train client
                client_model, client_loss = self._train_client(
                    root_model=self.root_model,
                    train_loader=self.train_loader,
                    client_idx=client_idx,
                )
                clients_models.append(client_model.state_dict())
                clients_losses.append(client_loss)

            # Update server model based on clients models
            updated_weights = average_weights(clients_models)
            self.root_model.load_state_dict(updated_weights)

            # Update average loss of this round
            avg_loss = sum(clients_losses) / len(clients_losses)
            train_losses.append(avg_loss)

            if (epoch + 1) % self.args.log_every == 0:
                # Test server model
                total_loss, total_acc = self.test()
                avg_train_loss = sum(train_losses) / len(train_losses)

                # Log results
                logs = {
                    "train/loss": avg_train_loss,
                    "test/loss": total_loss,
                    "test/acc": total_acc,
                    "round": epoch,
                }
                if total_acc >= self.target_acc and self.reached_target_at is None:
                    self.reached_target_at = epoch
                    logs["reached_target_at"] = self.reached_target_at
                    print(
                        f"\n -----> Target accuracy {self.target_acc} reached at round {epoch}! <----- \n"
                    )

                self.logger.log(logs)

                # Print results to CLI
                print(f"\n\nResults after {epoch + 1} rounds of training:")
                print(f"---> Avg Training Loss: {avg_train_loss}")
                print(
                    f"---> Avg Test Loss: {total_loss} | Avg Test Accuracy: {total_acc}\n"
                )

                # Early stopping
                if self.args.early_stopping and self.reached_target_at is not None:
                    print(f"\nEarly stopping at round #{epoch}...")
                    break

    def test(self) -> Tuple[float, float]:
        """Test the server model.

        Returns:
            Tuple[float, float]: average loss, average accuracy.
        """
        self.root_model.eval()

        total_loss = 0.0
        total_correct = 0.0
        total_samples = 0

        for idx, (data, target) in enumerate(self.test_loader):
            data, target = data.to(self.device), target.to(self.device)

            logits = self.root_model(data)
            loss = F.nll_loss(logits, target)

            total_loss += loss.item()
            total_correct += (logits.argmax(dim=1) == target).sum().item()
            total_samples += data.size(0)

        # calculate average accuracy and loss
        total_loss /= idx
        total_acc = total_correct / total_samples

        return total_loss, total_acc

if __name__ == "__main__":
    args = arg_parser()
    fed_avg = FedAvg(args)
    fed_avg.train()


#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2017
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program.  If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram VideoNote."""

from telegram import PhotoSize, TelegramObject

class VideoNote(TelegramObject):
    """This object represents a video message (available in Telegram apps as of v.4.0).

    Attributes:
        file_id (:obj:`str`): Unique identifier for this file.
        length (:obj:`int`): Video width and height as defined by sender.
        duration (:obj:`int`): Duration of the video in seconds as defined by sender.
        thumb (:class:`telegram.PhotoSize`): Optional. Video thumbnail.
        file_size (:obj:`int`): Optional. File size.

    Args:
        file_id (:obj:`str`): Unique identifier for this file.
        length (:obj:`int`): Video width and height as defined by sender.
        duration (:obj:`int`): Duration of the video in seconds as defined by sender.
        thumb (:class:`telegram.PhotoSize`, optional): Video thumbnail.
        file_size (:obj:`int`, optional): File size.
        **kwargs (:obj:`dict`): Arbitrary keyword arguments.

    """

    def __init__(self, file_id, length, duration, thumb=None, file_size=None, **kwargs):
        # Required
        self.file_id = str(file_id)
        self.length = int(length)
        self.duration = int(duration)
        # Optionals
        self.thumb = thumb
        self.file_size = file_size

        self._id_attrs = (self.file_id,)

    @classmethod
    def de_json(cls, data, bot):
        if not data:
            return None

        data = super(VideoNote, cls).de_json(data, bot)

        data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot)

        return cls(**data)


import re

from django.shortcuts import render
from apps.front.models import Document
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from pure_pagination import PageNotAnInteger, Paginator
from django.http import HttpResponse
from django.db.models import Q, F
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator

from apps.utils.pyecharts_restful import JsonResponse
import db_tools.data.show_data as s_data

import json
from rest_framework.views import APIView
from pyecharts.charts import Bar, Line, Pie, WordCloud, Graph
from pyecharts import options as opts
from pyecharts.commons.utils import JsCode
from collections import Counter
from apps.utils import handle_memcache

# Create your views here.

def index(request):
    return render(request, 'front/index.html')

def search(request):
    search_cat_id = request.GET.get('searchtype') # 获取前端查询类型参数
    words = request.GET.get('words') # 获取前端查询关键词参数
    page = request.GET.get('page', 1)  # 获取页数

    # 根据查询条件和关键词内容，在数据库查找相关的内容
    if search_cat_id == 'title':
        if words:
            documents = Document.objects.filter(title__contains=words).all()
        else:
            documents = Document.objects.all()
    elif search_cat_id == 'author':
        if words:
            documents = Document.objects.filter(author__contains=words).all()
        else:
            documents = Document.objects.all()
    elif search_cat_id == 'source':
        if words:
            documents = Document.objects.filter(acticle_source__contains=words).all()
        else:
            documents = Document.objects.all()
    elif search_cat_id == 'keywords':
        if words:
            documents = Document.objects.filter(key_words__contains=words).all()
        else:
            documents = Document.objects.all()
    elif search_cat_id == 'summary':
        if words:
            documents = Document.objects.filter(summary__contains=words).all()
        else:
            documents = Document.objects.all()
    else:
        documents = Document.objects.all()

    # 搜索结果可视化核心代码
    author_list = []
    source_list = []
    year_list = []
    orginize_list = []
    key_words = []
    for document in documents:
        author_list += hander_author(document.author)
        source_list.append(document.acticle_source.split(",")[0])
        year_list.append(get_year(document.acticle_source.split(",")[1]))
        orginize_list.append(document.author_location.split(",")[0])
        key_words += handle_words(document.key_words)
    orginize_dict = Counter(orginize_list)
    author_dict = Counter(reversed(author_list))
    source_dict = Counter(source_list)
    year_dict = Counter(reversed(year_list))
    words_dict = Counter(key_words)

    words_list = sorted(words_dict.items(),key=lambda item:item[1])
    print(words_list)

    new_year = sorted(year_dict.items(),key=lambda item: item[0])
    new_year_name = []
    new_year_nums = []
    for item in new_year:
        new_year_name.append(item[0])
        new_year_nums.append(item[1])
    # memcache 存储词云数据
    handle_memcache.set_key("words",words_list)

    # memcached 存储作者信息
    handle_memcache.set_key("author_name",list(author_dict.keys())[:20])
    handle_memcache.set_key("author_nums",list(author_dict.values())[:20])

    # memcached 存储期刊信息
    handle_memcache.set_key("qikan_name",list(source_dict.keys())[:20])
    handle_memcache.set_key("qikan_nums",list(source_dict.values())[:20])

    # memcached 存储年份信息
    handle_memcache.set_key("year_name",new_year_name)
    handle_memcache.set_key("year_nums",new_year_nums)

    # memcached 存储机构信息
    handle_memcache.set_key("org_name",list(orginize_dict.keys()))
    handle_memcache.set_key("org_nums",list(orginize_dict.values()))

    p = Paginator(documents, 10, request=request)
    s_documents = p.page(page)
    # 查询内容返回前端模板，前端可以通过{{ documents }} 的方式获取数据
    context = {
        'documents': s_documents,
    }
    # 返回模板文件与数据
    return render(request, 'front/search.html', context=context)

def get_year(value):
    if re.search("\d+",value):
        return re.search("\d+",value).group()
    return ""

def handle_words(value):
    return value.split(",")

def hander_author(value):
    return [i.strip() for i in value.split(",")]

def document_list(request):
    all_documents = Document.objects.all()
    page = request.GET.get('page', 1)

    p = Paginator(all_documents, 20, request=request)
    documents = p.page(page)
    context = {
        'documents': documents,

    }
    return render(request, 'front/list.html', context=context)

#  可视化图表作者柱状图参数配置
def author_bar() -> Bar:
    x = handle_memcache.get_value("author_name")
    y = handle_memcache.get_value("author_nums")
    c = (
        Bar()
            .add_xaxis(x)
            .add_yaxis("作者", y)
            .set_global_opts(
            title_opts=opts.TitleOpts(title="作者数量", subtitle="相关文章数量"), # 设置标题
            xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)), # 横轴设置字体倾斜45°，可显示全部作者名称
        )
            .dump_options_with_quotes() # 返回Json 格式的数据，前端解析调用
    )
    return c

def orginize_bar() -> Bar:
    o_x = handle_memcache.get_value("org_name")
    o_y = handle_memcache.get_value("org_nums")
    c = (
        Bar()
            .add_xaxis(o_x)
            .add_yaxis("机构", o_y, category_gap="60%")
            .set_series_opts(
            itemstyle_opts={
                "normal": {
                    "color": JsCode(
                        """new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                    offset: 0,
                    color: 'rgba(0, 122, 255, 1)'
                }, {
                    offset: 1,
                    color: 'rgba(0, 77, 167, 1)'
                }], false)"""
                    ),
                    "barBorderRadius": [30, 30, 30, 30],
                    "shadowColor": "rgb(0, 160, 221)",
                }
            }
        )
            .set_global_opts(
            title_opts=opts.TitleOpts(title="机构"),
            xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)),
        )
            .dump_options_with_quotes()
    )
    return c

def year_line():
    x = handle_memcache.get_value("year_name")
    y = handle_memcache.get_value("year_nums")

    line2 = (
        Line()
            .set_global_opts(
            tooltip_opts=opts.TooltipOpts(is_show=False),
            xaxis_opts=opts.AxisOpts(type_="category"),
            yaxis_opts=opts.AxisOpts(
                type_="value",
                axistick_opts=opts.AxisTickOpts(is_show=True),
                splitline_opts=opts.SplitLineOpts(is_show=True),
            ),
        )
            .add_xaxis(xaxis_data=x)
            .add_yaxis(
            series_name="",
            y_axis=y,
            symbol="emptyCircle",
            is_symbol_show=True,
            label_opts=opts.LabelOpts(is_show=False),
        )
            # 设置 boundary_gap 的时候一定要放在最后一个配置项里, 不然会被覆盖
            .dump_options_with_quotes()
    )
    return line2

def xueke_pie():
    data1 = handle_memcache.get_value("qikan_name")
    data2 = handle_memcache.get_value("qikan_nums")
    c = (
        Pie()
            .add(
            "",
            [
                list(z)
                for z in zip(
                data1,
                data2,
            )
            ],
            center=["40%", "50%"],
        )
            .set_global_opts(
            title_opts=opts.TitleOpts(title="期刊占比"),
            legend_opts=opts.LegendOpts(type_="scroll", pos_left="80%", orient="vertical"),
        )
            .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
            .dump_options_with_quotes()
    )
    return c

def qikan_bar() -> Bar:
    x = handle_memcache.get_value("qikan_name")
    y = handle_memcache.get_value("qikan_nums")
    c = (
        Bar()
            .add_xaxis(x)
            .add_yaxis("期刊", y)
            .reversal_axis()
            .set_series_opts(label_opts=opts.LabelOpts(position="right"))
            .set_global_opts(title_opts=opts.TitleOpts(title="期刊数量展示"))
            .dump_options_with_quotes()
    )
    return c

def cat_pie():
    data1 = s_data.cat
    data2 = s_data.cat_nums
    c = (
        Pie()
            .add(
            "",
            [list(z) for z in zip(data1, data2)],
            radius=["40%", "55%"],
            label_opts=opts.LabelOpts(
                position="outside",
                formatter="{a|{a}}{abg|}\n{hr|}\n {b|{b}: }{c}  {per|{d}%}  ",
                background_color="#eee",
                border_color="#aaa",
                border_width=1,
                border_radius=4,
                rich={
                    "a": {"color": "#999", "lineHeight": 22, "align": "center"},
                    "abg": {
                        "backgroundColor": "#e3e3e3",
                        "width": "100%",
                        "align": "right",
                        "height": 22,
                        "borderRadius": [4, 4, 0, 0],
                    },
                    "hr": {
                        "borderColor": "#aaa",
                        "width": "100%",
                        "borderWidth": 0.5,
                        "height": 0,
                    },
                    "b": {"fontSize": 16, "lineHeight": 33},
                    "per": {
                        "color": "#eee",
                        "backgroundColor": "#334455",
                        "padding": [2, 4],
                        "borderRadius": 2,
                    },
                },
            ),
        )
            .set_global_opts(title_opts=opts.TitleOpts(title="资源类型展示"))
            .dump_options_with_quotes()
    )
    return c

def wordshow():
    words_list = handle_memcache.get_value('words')[::-1][1:50]
    c = (
        WordCloud()
            .add(series_name="热点分析", data_pair=words_list, word_size_range=[6, 66],shape="star")
            .set_global_opts(
            title_opts=opts.TitleOpts(
                title="热点分析", title_textstyle_opts=opts.TextStyleOpts(font_size=23)
            ),
            tooltip_opts=opts.TooltipOpts(is_show=True),
        )
            .dump_options_with_quotes()
    )
    return c

def relation():
    nodes = [
        {"name": "邢蓓蓓", "symbolSize": 10},
        {"name": "林剑", "symbolSize": 20},
        {"name": "武敬平", "symbolSize": 30},
        {"name": "陶锋", "symbolSize": 40},
        {"name": "宋振峰", "symbolSize": 50},
        {"name": "胡伟", "symbolSize": 40},
        {"name": "吴小龙", "symbolSize": 30},
        {"name": "喻小勇", "symbolSize": 20},
    ]
    links = []
    for i in nodes:
        for j in nodes:
            links.append({"source": i.get("name"), "target": j.get("name")})
    c = (
        Graph()
            .add("", nodes, links, repulsion=8000)
            .set_global_opts(title_opts=opts.TitleOpts(title="关系图"))
            .dump_options_with_quotes()
    )
    return c

# 作者柱状图类视图函数，继承DRF的APIView ，返回Json格式的数据
class AuthorChartView(APIView):
    def get(self, request, *args, **kwargs):
        return JsonResponse(json.loads(author_bar()))

class OrginiseChartView(APIView):
    def get(self, request, *args, **kwargs):
        return JsonResponse(json.loads(orginize_bar()))

class YearChartView(APIView):
    def get(self, request, *args, **kwargs):
        return JsonResponse(json.loads(year_line()))

class XuekeChartView(APIView):
    def get(self, request, *args, **kwargs):
        return JsonResponse(json.loads(xueke_pie()))

class QikanChartView(APIView):
    def get(self, request, *args, **kwargs):
        return JsonResponse(json.loads(qikan_bar()))

class CatChartView(APIView):
    def get(self, request, *args, **kwargs):
        return JsonResponse(json.loads(cat_pie()))

class WordChartView(APIView):
    def get(self, request, *args, **kwargs):
        return JsonResponse(json.loads(wordshow()))

class RelationChartView(APIView):
    def get(self, request, *args, **kwargs):
        return JsonResponse(json.loads(relation()))

@method_decorator([login_required(login_url='/user/login/'),],name='dispatch') # 类视图的装饰器使用方法，同样需要登录才能执行
class ShowView(APIView):
    def get(self, request, *args, **kwargs):

        # 返回可视化模板文件，使用utf-8编码，可以正常显示中文
        return HttpResponse(content=open("./templates/front/show.html", encoding="utf-8").read())

@login_required(login_url='/user/login/')  # 添加装饰器，此视图函数需要登录之后才能执行
def upper_search(request):
    page = request.GET.get('page',1)
    searchtype1 = request.GET.get('searchtype1') # 获取搜索类型参数1

    upper_words1 = request.GET.get('upper_words1') # 获取前端查询条件第一个关键词
    searchtype_logic = request.GET.get('searchtype_logic') # 获取前端逻辑类型“与”，“或”
    searchtype2 = request.GET.get('searchtype2')  # 获取搜索类型参数2
    upper_words2 = request.GET.get('upper_words2')# 获取前端查询条件第二个关键词
    #
    # print(searchtype1)
    # print(upper_words1)
    # print(searchtype2)
    # print(upper_words2)

    if upper_words1 and upper_words2:
        # 如果前端查询逻辑关系是 “与” ，执行下面的操作
        if searchtype_logic == 'and':

            # 判断关键词的类型，执行相应的数据库查询操作
            if searchtype1 == 'author' and searchtype2 == 'title':
                documents = Document.objects.filter(author__icontains=upper_words1, title__icontains=upper_words2).all()
            elif searchtype1 == 'author' and searchtype2 == 'keywords':
                documents = Document.objects.filter(author__icontains=upper_words1,
                                                    key_words__icontains=upper_words2).all()
            elif searchtype1 == 'author' and searchtype2 == 'summary':
                documents = Document.objects.filter(author__icontains=upper_words1,
                                                    summary__icontains=upper_words2).all()
            elif searchtype1 == 'title' and searchtype2 == 'keywords':
                documents = Document.objects.filter(title__icontains=upper_words1,
                                                    key_words__icontains=upper_words2).all()
            elif searchtype1 == 'keywords' and searchtype2 == 'summary':
                documents = Document.objects.filter(key_words__icontains=upper_words1,
                                                    summary__icontains=upper_words2).all()
            elif searchtype1 == 'title' and searchtype2 == 'summary':
                documents = Document.objects.filter(key_words__icontains=upper_words1,
                                                    summary__icontains=upper_words2).all()
            elif searchtype1 == 'author' and searchtype2 == 'author':
                documents = Document.objects.filter(author__icontains=[upper_words1,upper_words2]).all()
            else:
                documents = Document.objects.all()

        # 如果前端查询逻辑关系是 “或” ，执行下面的操作
        else:
            print('or')
            if searchtype1 == 'author' and searchtype2 == 'title':
                documents = Document.objects.filter(
                    Q(author__icontains=upper_words1) | Q(title__icontains=upper_words2)).all()
            elif searchtype1 == 'author' and searchtype2 == 'keywords':
                documents = Document.objects.filter(
                    Q(author__icontains=upper_words1) | Q(key_words__icontains=upper_words2)).all()
            elif searchtype1 == 'author' and searchtype2 == 'summary':
                documents = Document.objects.filter(
                    Q(author__icontains=upper_words1) | Q(summary__icontains=upper_words2)).all()
            elif searchtype1 == 'title' and searchtype2 == 'keywords':
                documents = Document.objects.filter(
                    Q(title__icontains=upper_words1) | Q(key_words__icontains=upper_words2)).all()
            elif searchtype1 == 'keywords' and searchtype2 == 'summary':
                documents = Document.objects.filter(
                    Q(key_words__icontains=upper_words1) | Q(summary__icontains=upper_words2)).all()
            elif searchtype1 == 'keywords' and searchtype2 == 'author':
                documents = Document.objects.filter(
                    Q(key_words__icontains=upper_words1) | Q(author__icontains=upper_words2)).all()
            elif searchtype1 == 'title' and searchtype2 == 'summary':
                documents = Document.objects.filter(
                    Q(title__icontains=upper_words1) | Q(summary__icontains=upper_words2)).all()
            elif searchtype1 == 'title' and searchtype2 == 'author':
                documents = Document.objects.filter(
                    Q(title__icontains=upper_words1) | Q(author__icontains=upper_words2)).all()
            elif searchtype1 == 'author' and searchtype2 == 'author':
                print("两个作者")
                documents = Document.objects.filter(
                    Q(author__icontains=upper_words1) | Q(author__icontains=upper_words2)).all()
            else:
                documents = Document.objects.all()
        # 搜索结果可视化核心代码
        # 搜索结果可视化核心代码
        author_list = []
        source_list = []
        year_list = []
        orginize_list = []
        key_words = []
        for document in documents:
            author_list += hander_author(document.author)
            source_list.append(document.acticle_source.split(",")[0])
            year_list.append(get_year(document.acticle_source.split(",")[1]))
            orginize_list.append(document.author_location.split(",")[0])
            key_words += handle_words(document.key_words)
        orginize_dict = Counter(orginize_list)
        author_dict = Counter(reversed(author_list))
        source_dict = Counter(source_list)
        year_dict = Counter(reversed(year_list))
        words_dict = Counter(key_words)

        words_list = sorted(words_dict.items(), key=lambda item: item[1])

        new_year = sorted(year_dict.items(), key=lambda item: item[0])
        new_year_name = []
        new_year_nums = []
        for item in new_year:
            new_year_name.append(item[0])
            new_year_nums.append(item[1])
        # memcache 存储词云数据
        handle_memcache.set_key("words", words_list)

        # memcached 存储作者信息
        handle_memcache.set_key("author_name", list(author_dict.keys())[:20])
        handle_memcache.set_key("author_nums", list(author_dict.values())[:20])

        # memcached 存储期刊信息
        handle_memcache.set_key("qikan_name", list(source_dict.keys())[:20])
        handle_memcache.set_key("qikan_nums", list(source_dict.values())[:20])

        # memcached 存储年份信息
        handle_memcache.set_key("year_name", new_year_name)
        handle_memcache.set_key("year_nums", new_year_nums)

        # memcached 存储机构信息
        handle_memcache.set_key("org_name", list(orginize_dict.keys()))
        handle_memcache.set_key("org_nums", list(orginize_dict.values()))

        p = Paginator(documents, 8, request=request)
        s_documents = p.page(page)

        context = {
            'documents': s_documents,
        }
        return render(request, 'front/search.html', context=context)

    else:
        documents = Document.objects.all()
        # 搜索结果可视化核心代码
        author_list = []
        source_list = []
        year_list = []
        orginize_list = []
        key_words = []
        for document in documents:
            author_list += hander_author(document.author)
            source_list.append(document.acticle_source.split(",")[0])
            year_list.append(get_year(document.acticle_source.split(",")[1]))
            orginize_list.append(document.author_location.split(",")[0])
            key_words += handle_words(document.key_words)
        orginize_dict = Counter(orginize_list)
        author_dict = Counter(reversed(author_list))
        source_dict = Counter(source_list)
        year_dict = Counter(reversed(year_list))
        words_dict = Counter(key_words)

        words_list = sorted(words_dict.items(), key=lambda item: item[1])
        print(words_list)

        new_year = sorted(year_dict.items(), key=lambda item: item[0])
        new_year_name = []
        new_year_nums = []
        for item in new_year:
            new_year_name.append(item[0])
            new_year_nums.append(item[1])
        # memcache 存储词云数据
        handle_memcache.set_key("words", words_list)

        # memcached 存储作者信息
        handle_memcache.set_key("author_name", list(author_dict.keys())[:20])
        handle_memcache.set_key("author_nums", list(author_dict.values())[:20])

        # memcached 存储期刊信息
        handle_memcache.set_key("qikan_name", list(source_dict.keys())[:20])
        handle_memcache.set_key("qikan_nums", list(source_dict.values())[:20])

        # memcached 存储年份信息
        handle_memcache.set_key("year_name", new_year_name)
        handle_memcache.set_key("year_nums", new_year_nums)

        # memcached 存储机构信息
        handle_memcache.set_key("org_name", list(orginize_dict.keys()))
        handle_memcache.set_key("org_nums", list(orginize_dict.values()))

        p = Paginator(documents, 8, request=request)
        s_documents = p.page(page)

        context = {
            'documents': s_documents,
        }
        return render(request, 'front/search.html', context=context)

def test(request):
    upper_words1 = request.GET.get('upper_words1')
    upper_words2 = request.GET.get('upper_words2')

    documents = Document.objects.filter(Q(title__contains=upper_words1) | Q(key_words__contains=upper_words2)).all()

    for d in documents:
        print(d.title)
        print(d.key_words)

    context = {
        "documents":documents
    }
    return render(request,'front/search.html',context=context)


from tkinter import *
from pytube import YouTube

tk = Tk()
tk.geometry("350x500")
tk.title("Download Video from Youtube")
def unduh():
    try:
        var.set("Sedang Mendownload...")
        tk.update()
        YouTube(link.get()).streams.first().download()
        link.set("Video Sukses diDownload")
    except Exception as e:
        var.set("Link Tidak Ditemukan")
        tk.update()
        link.set("Masukkan Link")

Label(tk, text= "Silakan Download Video Anda", font= "Consolan 13 bold").pack()
var = StringVar()
var.set("Masukkan Link di Bawah")
Entry(tk, textvariable=var, width = 50).pack()
link = StringVar()
Entry(tk, textvariable=link, width = 50).pack()
Button(tk, text="Download", command = unduh).pack()
tk.mainloop()


import pandas as pd
import json
import matplotlib.pyplot as plt

from football_viz import plot_pitch_skc, plot_ff
from helper import transform_to_sck
from settings import PROCESSED_FOLDER, df_output_name, pattern_match, ff_output_name

# Read Previous Processed data (Example 2)
if not('event_data' in locals()):
    event_data = pd.read_csv(f"{PROCESSED_FOLDER}{df_output_name}.csv")

    # Create Id - to match it Freeze Frames
    event_data['id'] = event_data[['match_id', 'event_id']].apply(lambda x: pattern_match.format(x['match_id'], x['event_id']),axis=1)

    # Read Processed Freeze Frames
    with open(f"{PROCESSED_FOLDER}/{ff_output_name}.json", 'r') as f:
        ff_dict = json.load(f)

# Find Possession Chain With Goal
selected_chain = event_data[event_data['goal'] == 1].iloc[13]
selected = event_data[(event_data['chain_id'] == selected_chain['chain_id']) & (event_data['match_id']== selected_chain['match_id'])]

# fig, axe = plt.subplots(nrows=len(selected), ncols=1, figsize=(5, len(selected)*1))
# axes = axe.flatten()

# Visualize all events Leading to goal
index = 0
for idx, event in selected.iterrows():

    # Get Opta Event
    start_x = event['x']
    start_y = event['y']
    end_x = event['x2']
    end_y = event['y2']

    # Get Freeze Frame if Exists
    ff = ff_dict.get(event['id'], None)

    # Pitch
    fig, ax = plot_pitch_skc(field_color='w',figsize=(10, 7))

    # Plot Freeze Frame
    if ff is not None:
        plot_ff(ff, ax)

    # Arrow - Pass
    start_loc = transform_to_sck(start_x, start_y)

    if end_x == end_x: # Not None
        end_loc = transform_to_sck(end_x, end_y)
        ax.annotate('', xytext=(start_loc[0], start_loc[1]), ha='center', va='center', xy=(end_loc[0], end_loc[1]), arrowprops=dict(arrowstyle="->", color='k'), size=30, zorder=5)

    # Plot Ball
    # Plot Ball Location if available
    b_loc = transform_to_sck(start_x, start_y)
    ax.plot(b_loc[0],
            b_loc[1],
            'o',
            color='k',
            markersize=12,
            alpha=1.0,
            zorder=5
            )
    index +=1

plt.show()


"""Utilities for running I/O-bound operations asyncronously."""

import asyncio
import dataclasses
import functools
import sys
from typing import Any, Awaitable, Dict, Optional, Sequence, Tuple, TypeVar, Union

import six

if sys.version_info >= (3, 8):
    from typing import Protocol
else:
    from typing_extensions import Protocol

def run(*args, **kwargs):
    """Run an awaitable to completion, dispatch based on python version."""
    if sys.version_info < (3, 8):
        if sys.platform in ("win32", "cygwin"):
            asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
    return asyncio.run(*args, **kwargs)

# A type variable to indicate that the keys of the dict will not change.
K = TypeVar("K")
# A type variable to indicate that the async task is called with K, V tuples.
V = TypeVar("V")

class MapTask(Protocol):
    def __call__(self, key: K, value: V) -> Awaitable[Tuple[K, Any]]:
        """An async function that runs on each key, value pair in a map."""

async def limited_concurrency(*args, f: MapTask, sem: asyncio.Semaphore, **kwargs):
    """Run f but limit the number of processes that can run at once."""
    async with sem:
        return await f(*args, **kwargs)

async def run_map(
    mapping: Dict[K, V],
    func: MapTask,
    max_concurrency: int = -1,
) -> Dict[K, Any]:
    """Run async function on K, V pairs, return map with result as new value."""
    if max_concurrency > 0:
        sem = asyncio.Semaphore(max_concurrency)
        func = functools.partial(limited_concurrency, f=func, sem=sem)
    return dict(await asyncio.gather(*(func(k, v) for k, v in mapping.items())))

@dataclasses.dataclass
class CompletedAsyncProcess:
    """Results from a finished async subprocess run."""

    args: Union[Sequence[str], str]
    returncode: Optional[int]
    stdout: Optional[bytes] = None
    stderr: Optional[bytes] = None

async def subprocess_run(
    command: Union[Sequence[str], str],
    input: Optional[Union[str, bytes]] = None,
    capture_output: bool = False,
) -> CompletedAsyncProcess:
    """Run a subprocess with async. Tries to mirror the subprocess.run API."""
    if not isinstance(command, str):
        shell_command = " ".join(command)
    else:
        shell_command = command
    proc = await asyncio.create_subprocess_shell(
        shell_command,
        stdin=asyncio.subprocess.PIPE,
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE,
    )
    if input is not None:
        stdout, stderr = await proc.communicate(input=six.ensure_binary(input))
    else:
        stdout, stderr = await proc.communicate()
    return CompletedAsyncProcess(
        command,
        proc.returncode,
        stdout if capture_output else None,
        stderr if capture_output else None,
    )


import hashlib
class Hash_Function():
    def __init__(self,valor):
        self.valor = valor

    def Hashing(self,valor):
        hash_object = hashlib.md5(valor.encode())
        hex_dig = hash_object.hexdigest()
        return hex_dig

    #Insertar un nuevo elemento
    def insertar(self, valor):
        hash = self.hash_function(valor)
        if self.tabla[hash] is None:
            return None
        else:
            return hex(id(self.tabla[hash]))

    #Eliminar un elemento
    def eliminar(self, valor):
        hash = self.hash_function(valor)
        if self.tabla[hash] is None:
            print("No hay elementos con ese valor", valor)
        else:
            print("Elemento con valor", valor, "eliminado")
            self.tabala[hash] is None;


N = int(input())
count = 0
for i in range(1, N+1):
    st = str(i)
    so = oct(i)
    if "7" in st:
        continue
    elif "7" in so:
        continue
    else:
        count += 1
print(count)