from ui.gameplay import Gameplay
from ui.results import Results
from services.functions import Functions
from repository.repository import Repository
import pygame
import sys
import os
from pygame.locals import *
pygame.font.init()

dirname = os.path.dirname(__file__)

class MainMenu:

    def __init__(self):
        pygame.init()
        self.g = Gameplay()
        self.r = Results()
        self.f = Functions()
        self.repo = Repository()
        self.screen = pygame.display.set_mode((600, 600), 0, 32)
        self.mainClock = pygame.time.Clock()
        self.menufont = pygame.font.SysFont("cambria", 100)
        self.menufont2 = pygame.font.SysFont("cambria", 102)
        self.menufont3 = pygame.font.SysFont("cambria", 40)
        self.menufont4 = pygame.font.SysFont("cambria", 15)
        self.to_game = pygame.Rect(200, 300, 200, 50)
        self.to_results = pygame.Rect(200, 400, 200, 50)
        self.erase_results = pygame.Rect(5, 5, 135, 30)
        self.erase_confirmation = pygame.Rect(200, 50, 200, 100)
        self.click = False
        self.mx, self.my = pygame.mouse.get_pos()
        self.easy = pygame.Rect(200, 210, 200, 50)
        self.normal = pygame.Rect(200, 270, 200, 50)
        self.hard = pygame.Rect(200, 330, 200, 50)

    def run_main_menu(self):
        self.click = False
        confirm_box = False
        self.repo.create_or_maintain_result_files()
        while True:
            self.screen.fill((255, 255, 255))

            self.mx, self.my = pygame.mouse.get_pos()

            self.f.draw_text("Tyhjennä tulostaulu", self.menufont4, (0, 0, 0), self.screen, 10, 10)
            self.f.draw_text("Sudoku", self.menufont, (0, 0, 0), self.screen, 135, 150)
            self.f.draw_text('Peliin', self.menufont3, (0, 0, 0), self.screen, 248, 298)
            self.f.draw_text('Tulokset', self.menufont3, (0, 0, 0), self.screen, 225, 400)

            if self.to_game.collidepoint((self.mx, self.my)):
                if self.click:
                    self.difficulty()
            elif self.to_results.collidepoint((self.mx, self.my)):
                if self.click:
                    self.r.show_results()
            elif self.erase_results.collidepoint((self.mx, self.my)):
                if self.click and confirm_box:
                    confirm_box = False
                elif self.click:
                    confirm_box = True

            pygame.draw.rect(self.screen, (0, 0, 0), self.to_game, 1)
            pygame.draw.rect(self.screen, (0, 0, 0), self.to_results, 1)
            pygame.draw.rect(self.screen, (0, 0, 0), self.erase_results, 1)

            if confirm_box:
                pygame.draw.rect(self.screen, (0, 0, 0), self.erase_confirmation, 1)
                self.f.draw_text("Haluatko varmasti", self.menufont4, (0, 0, 0), self.screen, 240, 60)
                self.f.draw_text("tyhjentää tulostaulun?", self.menufont4, (0, 0, 0), self.screen, 230, 80)
                self.f.draw_text("ENTER = Tyhjennä", self.menufont4, (255, 0, 0), self.screen, 240, 100)
                self.f.draw_text("ESC = Peruuta", self.menufont4, (255, 0, 0), self.screen, 252, 120)

            self.click = False
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == KEYDOWN:
                    if event.key == K_ESCAPE and not confirm_box:
                        pygame.quit()
                        sys.exit()
                    if confirm_box:
                        if event.key == K_RETURN:
                            self.repo.clear_results()
                            confirm_box = False
                        elif event.key == K_ESCAPE:
                            confirm_box = False
                if event.type == MOUSEBUTTONDOWN:
                    if event.button == 1:
                        self.click = True

            pygame.display.update()
            self.mainClock.tick(60)

    def difficulty(self):

        while True:

            self.screen.fill((255, 255, 255))

            self.f.draw_text('Helppo', self.menufont3, (0, 0, 0), self.screen, 236, 208)
            self.f.draw_text('Normaali', self.menufont3, (0, 0, 0), self.screen, 218, 268)
            self.f.draw_text('Vaikea', self.menufont3, (0, 0, 0), self.screen, 240, 328)

            self.mx, self.my = pygame.mouse.get_pos()

            pygame.draw.rect(self.screen, (0, 0, 0), self.easy, 1)
            pygame.draw.rect(self.screen, (0, 0, 0), self.normal, 1)
            pygame.draw.rect(self.screen, (0, 0, 0), self.hard, 1)

            self.click = False
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        self.run_main_menu()
                if event.type == MOUSEBUTTONDOWN:
                    if event.button == 1:
                        self.click = True
                if self.easy.collidepoint((self.mx, self.my)):
                    if self.click:
                        self.g.game_loop(0)
                elif self.normal.collidepoint((self.mx, self.my)):
                    if self.click:
                        self.g.game_loop(1)
                elif self.hard.collidepoint((self.mx, self.my)):
                    if self.click:
                        self.g.game_loop(2)

            pygame.display.update()
            self.mainClock.tick(60)


import json
import os
import zipfile
from tqdm import tqdm
import wget

from db import insert_company_data, create_table

# Обработка json
def handle_json(json_payload, file_path):
    try:
        # Цикл проходит по всем компаниям в json файле
        for company_info in json_payload:
            try:
                # В случае нахождения ОКВЭД 62, записывает в базу данных информацию по компании
                if company_info['data']['СвОКВЭД']['СвОКВЭДОсн']['КодОКВЭД'] == '62':
                    sv_address_legal = company_info['data']['СвАдресЮЛ']['АдресРФ']
                    # Формирования адреса в виде удобной строки для чтения
                    place_registration = f"{sv_address_legal['Индекс']}, {sv_address_legal['Регион']['НаимРегион']} " \
                                         f"{sv_address_legal['Регион']['ТипРегион']}, {sv_address_legal['Город']['ТипГород']}" \
                                         f"{sv_address_legal['Город']['НаимГород']}, {sv_address_legal['Улица']['ТипУлица']}" \
                                         f"{sv_address_legal['Улица']['НаимУлица']}, {sv_address_legal['Дом']}, " \
                                         f"{sv_address_legal['Кварт']}"
                    insert_company_data(company_info['name'], company_info['data']['СвОКВЭД']['СвОКВЭДОсн']['КодОКВЭД'],
                                        int(company_info['inn']), int(company_info['kpp']), place_registration)
            except KeyError:
                pass
    except UnicodeDecodeError:
        print(f'Не удалось десериализовать файл: {file_path}')

# Обработка всех файлов в zip архиве
def handle_zip_file(path):
    with zipfile.ZipFile(path, mode="r") as archive:
        for filename in tqdm(archive.namelist()):
            templates = archive.open(filename, 'r')
            json_payload = json.load(templates)
            handle_json(json_payload, filename)

# GitHub action test 4
if __name__ == '__main__':
    # Создает таблицу в базе, если её нет
    create_table()

    if os.path.exists('egrul.json.zip'):
        handle_zip_file('egrul.json.zip')
    else:
        print('Скачивания файла...')
        wget.download('https://ofdata.ru/open-data/download/egrul.json.zip', 'egrul.json.zip')

        handle_zip_file('egrul.json.zip')


import matplotlib.pyplot as plt
import scipy
import numpy as np
import networkx as nx
import random
import cv2 as cv
import scipy.io
import scipy.spatial.distance as sd
import os

def is_connected(adj,n):
    adjn=np.zeros((n,n))
    adji=adj.copy()
    for i in range(n):
        adjn+=adji
        adji=adji.dot(adj)
    return len(np.where(adjn == 0)[0])==0

def max_span_tree(adj):
    n=adj.shape[0]
    if not(is_connected(adj,n)):
        print('This graph is not connected. No spanning tree exists')
    else:
        tr=np.zeros((n,n))
        adj[adj==0]=-np.inf
        conn_nodes = [0]
        rem_nodes = [i+1 for i in range(n-1)]
        while len(rem_nodes)>0:
            L=np.zeros(n)
            L[conn_nodes]=1
            L=L.reshape(n,1)
            C=np.zeros(n)
            C[rem_nodes]=1
            C=C.reshape(1,n)
            B=L.dot(C)
            A=B*adj
            i=np.where(A==np.max(A))[0][0]
            j=np.where(A==np.max(A))[1][0]
            tr[i,j]=1
            tr[j,i]=1
            conn_nodes+=[j]
            rem_nodes.remove(j)
    return tr.astype(int)

def build_similarity_graph(X, var=1, eps=0, k=0):
  assert eps + k != 0, "Choose either epsilon graph or k-nn graph"
  dists = sd.squareform(sd.pdist(X, "sqeuclidean"))
  similarities = np.exp(-dists / var)
  if eps:
    similarities[similarities < eps] = 0
    return similarities
  if k:
    sort = np.argsort(similarities)[:, ::-1]  # descending
    mask = sort[:, k + 1:]  # indices to mask
    for i, row in enumerate(mask): similarities[i, row] = 0
    np.fill_diagonal(similarities, 0)  # remove self similarity
    return (similarities + similarities.T) / 2  # make the graph undirected
    return similarities

def build_laplacian(W, laplacian_normalization=""):
  degree = W.sum(1)
  if not laplacian_normalization or laplacian_normalization=="unn":
    return np.diag(degree) - W
  elif laplacian_normalization == "sym":
    aux = np.diag(1 / np.sqrt(degree))
    return np.eye(*W.shape) - aux.dot(W.dot(aux))
  elif laplacian_normalization == "rw":
    return np.eye(*W.shape) - np.diag(1 / degree).dot(W)
  else: raise ValueError

def hardHFS(graph, labels, laplacian):
  classes = np.unique(labels[labels != 0]).reshape((-1, 1))
  f = (labels == classes).astype(np.float).T
  ixmask = np.where(labels == 0)[0]
  ixlabl = np.where(labels != 0)[0]
  luu = laplacian[ixmask][:, ixmask]
  wul = graph[ixmask][:, ixlabl]
  f[ixmask] = np.linalg.pinv(luu).dot(wul.dot(f[ixlabl]))
  return f

def plot_edges_and_points(X,Y,W,title=''):
    colors=['go-','ro-','co-','ko-','yo-','mo-']
    n=len(X)
    G=nx.from_numpy_matrix(W)
    nx.draw_networkx_edges(G,X)
    for i in range(n):
        plt.plot(X[i,0],X[i,1],colors[int(Y[i])])
    plt.title(title)
    plt.axis('equal')

def plot_graph_matrix(X,Y,W):
    plt.figure()
    plt.clf()
    plt.subplot(1,2,1)
    plot_edges_and_points(X,Y,W)
    plt.subplot(1,2,2)
    plt.imshow(W, extent=[0, 1, 0, 1])
    plt.show()

def plot_clustering_result(X,Y,W,spectral_labels,kmeans_labels,normalized_switch=0):
    plt.figure()
    plt.clf()
    plt.subplot(1,3,1)
    plot_edges_and_points(X,Y,W,'ground truth')
    plt.subplot(1,3,2)
    if normalized_switch:
        plot_edges_and_points(X,spectral_labels,W,'unnormalized laplacian')
    else:
        plot_edges_and_points(X,spectral_labels,W,'spectral clustering')
    plt.subplot(1,3,3)
    if normalized_switch:
        plot_edges_and_points(X,kmeans_labels,W,'normalized laplacian')
    else:
        plot_edges_and_points(X,kmeans_labels,W,'k-means')
    plt.show()

def plot_the_bend(X, Y, W, spectral_labels, eigenvalues_sorted):
    plt.figure()
    plt.clf()
    plt.subplot(1,3,1)
    plot_edges_and_points(X,Y,W,'ground truth')
    plt.subplot(1,3,2)
    plot_edges_and_points(X,spectral_labels,W,'spectral clustering')
    plt.subplot(1,3,3)
    plt.plot(np.arange(0,len(eigenvalues_sorted),1),eigenvalues_sorted,'v:')
    plt.show()

def plot_classification(X, Y,labels,  var=1, eps=0, k=0):
    plt.figure()
    W = build_similarity_graph(X, var=var, eps=eps, k=k)
    plt.subplot(1, 2, 1)
    plot_edges_and_points(X, Y, W, 'ground truth')
    plt.subplot(1, 2, 2)
    plot_edges_and_points(X, labels, W, 'HFS')
    plt.show()

def label_noise(Y, alpha):
    ind=np.arange(len(Y))
    random.shuffle(ind)
    Y[ind[:alpha]] = 3-Y[ind[:alpha]]
    return Y

def plot_classification_comparison(X, Y,hard_labels, soft_labels,var=1, eps=0, k=0):
    plt.figure()
    W = build_similarity_graph(X, var=var, eps=eps, k=k)
    plt.subplot(1,3,1)
    plot_edges_and_points(X, Y, W, 'ground truth')
    plt.subplot(1,3,2)
    plot_edges_and_points(X, hard_labels, W, 'Hard-HFS')
    plt.subplot(1,3,3)
    plot_edges_and_points(X, soft_labels, W, 'Soft-HFS')
    plt.show()

Large Scale Graph Learning/code_material_python/helper.py


from django.shortcuts import render
from .models import Product, ProductForm, Question, Option
from datetime import datetime

# Create your views here.
def form_view(request):
    pf = ProductForm(request.POST or None)
    if request.method == 'POST':
        if pf.is_valid():
            product = pf.save(commit = False)
            product.timestamp = datetime.now()
            product.save()
            return render(request,'app1/product.html',{'product':product})
    return render(request,'app1/index.html',{'pf':pf})

def products_view(request):
    products = Product.objects.all()
    return render(request,'app1/allproducts.html',{'products':products})

def ques_view(request):
    if request.method == "POST":
        question = Question.objects.all()
        optionSelected = request.POST['question']
        print(optionSelected)
        option = Option.objects.filter(value=optionSelected)[0]
        print(option)
        option.number += 1
        option.save()
        options = Option.objects.all()
        return render(request,'app1/form.html',{'options':options})
    question = Question.objects.all()
    return render(request,'app1/form.html',{'question':question[0].question, 'options':question[0].options.all()})


# -*- coding: utf-8 -*-

from .internal._curry3 import _curry3

@_curry3
def mapAccumRight(fn, acc, list):
    idx = len(list) - 1
    result = [None] * len(list)
    tuple = [acc]
    while idx >= 0:
        tuple = fn(tuple[0], list[idx])
        result[idx] = tuple[1]
        idx -= 1
    return [tuple[0], result]


# TAREA
# 1 Escriba un programa que almacene en una Lista los cursos que has cursado o los que te gustaría cursar en Udemy. Luego muestre la lista por consola.
listaDeCursos = ['JavaScriot desde cero', 'React con TypeScript', 'Python desde cero']
for i in listaDeCursos:
  print(i)

# 2 Escriba un programa que almacene personas en una lista, luego que le muestre por pantalla el mensaje de ‘Su nombre es ‘nombre’
listaDePersonas = ['Analia', 'Carlos', 'Agustin', 'Andres']
for i in listaDePersonas:
  print(f'Su nombre es {i}')

# 3 Escribir un programa que guarde en una variable un diccionario con los siguientes valores {'Euro':'€', 'Dollar':'$', 'Yen':'¥'} Luego pregunte al usuario por una divisa y muestre en consola el símbolo o un mensaje de advertencia si esa divisa no se encuentra en el diccionario.
diccionarioDeMonedas =  {'Euro':'€', 'Dollar':'$', 'Yen':'¥'}
divisaElegida = input("Ingrese una divisa -en palabra, con la primer letra en mayuscula-: ")
if divisaElegida.title() in diccionarioDeMonedas:
    print(f'El simbolo  es: {diccionarioDeMonedas[divisaElegida.title()]}')
else:
  print("La divisa no se encuentra en el diccionario")

# 4  En una tupla coloque los siguientes valores: números enteros, decimales, String, colecciones. Luego muestre en consola.
tupla = (20, 14.5, 'Hello', [1, 2, 3, 4, 5])
print(tupla)


import pandas as pd
from datetime import datetime, timedelta
import yahoo_fin.stock_info as si
import yfinance as yf
import streamlit as st
import re

#define dataframe table hover style
def hover(hover_color="blue"):
        return dict(selector="td:hover",
                    props=[("background-color", "%s" % hover_color)])

#define table style function
def set_styles(results):
    table_styles = [
                hover(),
                dict(selector="th", props=[("visibility", "show")])
                ]
    return (
        results.style.set_table_styles(table_styles)
        .set_properties(**{"background-color": "black", "color": "white"
        })

    )

#function to clean the index text
@st.cache
def format_cols(df):
    loops = len(df) + 1
    list_index = []
    for i in df.index:
        list_index.append(re.sub(r"(\w)([A-Z])", r"\1 \2", i).title())
    df.index = list_index

@st.cache
def convert_df(df):
   return df.to_csv().encode('utf-8')

#define the run function
def run():

    st.markdown("""

Financials

    """, unsafe_allow_html=True)

    ticker_list = ['-'] + si.tickers_sp500()
    # Add selection box
    global ticker
    global select_period
    default_t = ticker_list.index("AAPL")
    ticker = st.sidebar.selectbox("Select a ticker", ticker_list, index = default_t)

    if ticker != '-':
        st.write('', unsafe_allow_html=True)
        fin_type = st.radio("", ("Income Statement", "Balance Sheet", "Cash Flow"))
        pulse = st.selectbox("", ("Quarterly", "Annual"))

        if fin_type == 'Income Statement' and pulse =="Quarterly":
            inc_stm = si.get_income_statement(ticker, yearly=False)
            format_cols(inc_stm)
            st.table(set_styles(inc_stm))
        elif fin_type == 'Income Statement' and pulse =="Annual":
                inc_stm = si.get_income_statement(ticker, yearly=True)
                format_cols(inc_stm)
                st.table(set_styles(inc_stm))

        elif fin_type == 'Balance Sheet' and pulse =="Quarterly":
            bal_sht = si.get_balance_sheet(ticker, yearly=False)
            format_cols(bal_sht)
            st.table(bal_sht)

        elif fin_type == 'Balance Sheet'  and pulse =="Annual":
            bal_sht = si.get_balance_sheet(ticker, yearly=True)
            format_cols(bal_sht)
            st.table(bal_sht)

        elif fin_type == 'Cash Flow' and pulse =="Quarterly":
            cash_flw = si.get_cash_flow(ticker, yearly=False)
            format_cols(cash_flw)
            st.table(cash_flw)

        elif fin_type == 'Cash Flow' and pulse =="Annual":
            cash_flw = si.get_cash_flow(ticker, yearly=True)
            format_cols(cash_flw)
            st.table(cash_flw)

    if fin_type == 'Income Statement':
        csv = convert_df(inc_stm)
    elif fin_type == 'Balance Sheet':
        csv = convert_df(bal_sht)
    elif fin_type == 'Cash Flow':
        csv = convert_df(cash_flw)

    st.download_button("Click to Download",csv,"data.csv","text/csv",key='download-csv')


# coding=utf-8
import requests

from jedis import jedis
from bs4 import BeautifulSoup

table_name = "China_it_top_100_company_info"
def get_it_top100():
    url = "http://www.sohu.com/a/162100864_608782"
    req = requests.Session()
    re = jedis.jedis()
    re.clear_list(table_name)
    res = req.get(url=url)
    content = res.content.decode("utf-8")
    parse_info(content, re)

def parse_info(content, re):
    soup = BeautifulSoup(content, "html5lib")
    company_list = soup.find_all('tr')
    for i in range(1, 100):
        infos = company_list[i].text.split("\n")
        company_name = infos[2].strip()
        short_name = infos[3].strip()
        product = infos[4].strip()
        re.save_dict(table_name, data=dict(company_name=company_name, short_name=[short_name], product=product))
    re.add_to_file_local(table_name)
    print("中国IT企业100强获取完成")

if __name__ == '__main__':
    get_it_top100()


# -*- coding: utf-8 -*-

"""
(Description)

Created on Apr 2, 2015
"""
from ce1sus.controllers.base import BaseController, ControllerException, ControllerNothingFoundException
from ce1sus.db.brokers.syncserverbroker import SyncServerBroker
from ce1sus.db.classes.common import ServerType
from ce1sus.db.common.broker import BrokerException, NothingFoundException

__author__ = 'Weber Jean-Paul'
__email__ = 'jean-paul.weber@govcert.etat.lu'
__copyright__ = 'Copyright 2013-2014, GOVCERT Luxembourg'
__license__ = 'GPL v3+'

class SyncServerController(BaseController):

  def __init__(self, config, session=None):
    BaseController.__init__(self, config, session)
    self.sync_server_broker = self.broker_factory(SyncServerBroker)

  def get_all_servers(self):
    try:
      return self.sync_server_broker.get_all()
    except BrokerException as error:
      raise ControllerException(error)

  def get_all_pull_servers(self):
    try:
      return self.sync_server_broker.get_all_pull_servers()
    except BrokerException as error:
      raise ControllerException(error)

  def get_all_push_servers(self):
    try:
      return self.sync_server_broker.get_all_push_servers()
    except BrokerException as error:
      raise ControllerException(error)

  def get_server_by_uuid(self, uuid):
    try:
      return self.sync_server_broker.get_by_uuid(uuid)
    except NothingFoundException as error:
      raise ControllerNothingFoundException(error)
    except BrokerException as error:
      raise ControllerException(error)

  def remove_server(self, server, user):
    try:
      self.sync_server_broker.remove_by_id(server.identifier)
    except BrokerException as error:
      raise ControllerException(error)

  def update_server(self, server, user):
    try:
      user = self.user_broker.get_by_id(user.identifier)
      self.set_extended_logging(server, user, user.group, False)
      self.sync_server_broker.update(server)

    except BrokerException as error:
      raise ControllerException(error)

  def insert_server(self, server, user):
    try:
      user = self.user_broker.get_by_id(user.identifier)
      self.set_extended_logging(server, user, user.group, True)
      self.sync_server_broker.insert(server)

    except BrokerException as error:
      raise ControllerException(error)

  def get_all_types(self):
    values = ServerType.get_dictionary()
    return values


import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "r3.settings")

from app.models import Prop

f = open('work1.txt', 'r')
contents = f.read()
lines = contents.split('\n')
for l in lines:
	shortname = l[2:7]
	address = l[7:37]
	city = l[37:52]
	state = l[52:54]
	zip = l[54:64]
	comment = l[124:164] + ' ' + l[164:204]
	enis_no = l[204:206]
	print(shortname, address, city, state, zip, comment, enis_no)
	p=Prop(
		short_name=shortname,
		address=address,
		city=city,
		state=state,
		zip=zip,
		comment=comment,
		enis_no=enis_no,
		mgmt_co='',
		mgmt_pct=0)
	p.save()


# -*- coding: utf-8 -*-
"""
Created on Tue Apr  9 13:46:06 2019

Figure 4. Interannual terrestrial and oceanic contributions to Australian precipitation in (a) relative terms
[%] and (b) absolute terms [mm].

@author: z3131380
"""

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter

#==============================================================================
# Definitions
#==============================================================================
dir_in = r'/srv/ccrc/data19/z3131380/PartB/Output/Australia/100parcels/TS10min/exp01/Processed/Rainfall_recycling/'
dir_out = r'/home/z3131380/hdrive/PhD/PartB/Reporting/Paper_1/Figures/'

region = 'Australia'
df = pd.read_csv(dir_in+'Yearly/'+region+'_annual_rainfall_recycling_1979-2013.csv')

fsize = 14

#==============================================================================
# Functions
#==============================================================================
def make_patch_spines_invisible(ax):
    ax.set_frame_on(True)
    ax.patch.set_visible(False)
    for sp in ax.spines.values():
        sp.set_visible(False)

#==============================================================================
# Figure 4. Interannual terrestrial and oceanic contributions to Australian precipitation in (a) relative terms
# [%] and (b) absolute terms [mm].
#==============================================================================

fig, ax2 = plt.subplots(figsize=(16,11.7))
ax2 = plt.subplot2grid((2, 1), (0, 0), rowspan=1)
plt.annotate('(a)',xy=(1980,0.4),xycoords='data',fontsize=fsize)
#p3, = ax2.plot(df['Year'],df['P_anom'],'-k^', label="Rainfall anomaly [%]",markersize=4,linewidth=1,alpha=0.7)
ax2.fill_between(df['Year'],df['P_anom'],0,alpha=0.5,edgecolor='None',facecolor='gray')
ax2.set_ylim(-0.5,0.5)
ax2.grid(axis='x')
ax2.tick_params(labelbottom=False,labeltop=True)
ax2.axhline(0,color='gray',linestyle=':')
ax2.set_yticklabels(ax2.get_yticks(),fontsize=fsize)
ax2.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
plt.annotate('Precipitation anomaly [%]',xy=(-0.05,0.7),xycoords='axes fraction',fontsize=fsize,rotation=90)
ax2a = ax2.twinx()
ax2b = ax2.twinx()
p2a, = ax2a.plot(df['Year'],df['outregion_ocean_pct'],'-bo', label="Ocean water vapour contribution [%]",markersize=4,markeredgecolor='b',markerfacecolor='b')#,alpha=0.5)
p2b, = ax2b.plot(df['Year'],df['RR'],'-gs', label="Land water vapour contribution [%]",markersize=4,markeredgecolor='g',markerfacecolor='g')#,alpha=0.5)
ax2a.spines["right"].set_position(("axes", 1.01))
ax2b.spines["right"].set_position(("axes", 1.07))
make_patch_spines_invisible(ax2a)
make_patch_spines_invisible(ax2b)
ax2a.spines["right"].set_visible(True)
ax2b.spines["right"].set_visible(True)
tkw = dict(size=4, width=1.5)
ax2a.tick_params(axis='y', colors=p2a.get_color(), **tkw)
ax2b.tick_params(axis='y', colors=p2b.get_color(), **tkw)
ax2a.set_yticklabels(ax2a.get_yticks(),fontsize=fsize)
ax2a.yaxis.set_major_formatter(FormatStrFormatter('%.0f'))
ax2b.set_yticklabels(ax2b.get_yticks(),fontsize=fsize)
ax2b.yaxis.set_major_formatter(FormatStrFormatter('%.0f'))
ax2a.set_ylim(70,95)
ax2b.set_ylim(5,25)
plt.annotate("Oceanic contribution [%]",xy=(1.04,0.7),xycoords = 'axes fraction',rotation=90,color='blue',fontsize=fsize)
plt.annotate("Terrestrialcontribution [%]",xy=(1.1,0.74),xycoords = 'axes fraction',rotation=90,color='green',fontsize=fsize)
ax2.set_xlim(1979,2014)
ax2.set_xticks(np.arange(min(df['Year']), max(df['Year'])+1, 1))
ax2.set_xticklabels(ax2.get_xticks(),rotation=90,fontsize=fsize)
ax2.xaxis.set_major_formatter(FormatStrFormatter('%.0f'))

ax1 = plt.subplot2grid((2, 1), (1, 0), sharex=ax2)
plt.annotate('(b)',xy=(1980,2700),xycoords='data',fontsize=fsize)
#p1, = ax1.plot(df['Year'],df['P_total']/1000,'-k^', label="Rainfall total [m]",markersize=4,linewidth=1,alpha=0.7)
ax1.fill_between(df['Year'],df['P_total']/1000,0,alpha=0.5,edgecolor='None',facecolor='gray')
#ax1.set_ylim(0,2000)
ax1.grid(axis='x')
#ax1.tick_params(labelbottom=False,labeltop=True)
ax1.set_xlim(1979,2014)
ax1.set_ylim(500,3000)
ax1.set_xticks(np.arange(min(df['Year']), max(df['Year'])+1, 1))
ax1.set_xticklabels(ax1.get_xticks(),rotation=90,fontsize=fsize)
ax1.xaxis.set_major_formatter(FormatStrFormatter('%.0f'))
ax1.set_yticklabels(ax1.get_yticks(),fontsize=fsize)
ax1.yaxis.set_major_formatter(FormatStrFormatter('%.0f'))
plt.annotate('Precipitation total [m]',xy=(-0.06,0.65),xycoords='axes fraction',fontsize=fsize,rotation=90)
ax1a = ax1.twinx()
ax1b = ax1.twinx()
p1a, = ax1a.plot(df['Year'],df['outregion_ocean_mm']/1000,'-bo', label="Ocean water vapour contribution [m]",markersize=4,markeredgecolor='b',markerfacecolor='b')
p1b, = ax1b.plot(df['Year'],df['region_land_mm']/1000,'-gs', label="Land water vapour contribution [m]",markersize=4,markeredgecolor='g',markerfacecolor='g')
ax1a.spines["right"].set_position(("axes", 1.01))
ax1b.spines["right"].set_position(("axes", 1.07))
make_patch_spines_invisible(ax1a)
make_patch_spines_invisible(ax1b)
ax1a.spines["right"].set_visible(True)
ax1b.spines["right"].set_visible(True)
tkw = dict(size=4, width=1.5)
ax1a.tick_params(axis='y', colors=p1a.get_color(), **tkw)
ax1b.tick_params(axis='y', colors=p1b.get_color(), **tkw)
ax1a.set_yticklabels(ax1a.get_yticks(),fontsize=fsize)
ax1a.yaxis.set_major_formatter(FormatStrFormatter('%.0f'))
ax1b.set_yticklabels(ax1b.get_yticks(),fontsize=fsize)
ax1b.yaxis.set_major_formatter(FormatStrFormatter('%.0f'))
ax1a.set_ylim(600,1800)
ax1b.set_ylim(100,400)
plt.annotate("Oceanic contribution [m]",xy=(1.05,0.7),xycoords = 'axes fraction',rotation=90,color='blue',fontsize=fsize)
plt.annotate("Terrestrial contribution [m]",xy=(1.105,0.75),xycoords = 'axes fraction',rotation=90,color='green',fontsize=fsize)

plt.tight_layout()
fig.savefig(dir_out+'Fig4.png',bbox_inches='tight')
plt.close()


n = 0
team = []
num = 0
star = 0

i = input('Number Of Players: ')
n = n + int(i)
for x in range(n):
    player = []
    num += 1
    p=input('Points: ')
    f=input('Fouls: ')
    sum = int(p)*5 - int(f)*3
    if sum <= 40:
        star = 0
    else:
        star = star + sum
    player.append(num)
    player.append(p)
    player.append(f)
    player.append(sum)
    team.append(player)

print(*team, sep='\n')

gold = n * 40
print(gold)
print(star)
if star > gold:
    print('Gold Team')
else:
    print('Not A Gold Team')


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 14 14:23:43 2023

@author: johnmorgan
"""

def knap_sack(profits, profits_length, weights, capacity):
    if capacity <= 0 or profits_length == 0:
        return 0
    lookup_table = [[0 for x in range(capacity + 1)] for x in range(profits_length + 1)]
    for i in range(profits_length + 1):
        for j in range(capacity + 1):
            print(i, j)
            if i == 0 or j == 0:
                lookup_table[i][j] = 0
            elif weights[i - 1] <= j:
                lookup_table[i][j] = max(profits[i - 1] + lookup_table[i - 1][j - weights[i - 1]],
                                         lookup_table[i - 1][j])
            else:
                lookup_table[i][j] = lookup_table[i - 1][j]
            print(lookup_table)
    return lookup_table[profits_length][capacity]

profits = [60, 100, 120]
profits_length = 3
weights = [10, 20, 30]
capacity = 50 # knapsack weight

print(knap_sack(profits, profits_length, weights, capacity))

profits = [60, 100, 120]
profits_length = 3
weights = [10, 20, 30]
capacity = 10 # knapsack weight

print(knap_sack(profits, profits_length, weights, capacity))


from __future__ import annotations

import math
from contextlib import suppress
from typing import Any, Callable, Union

import toolz
from typing_extensions import Annotated, get_args, get_origin

from ibis.common.dispatch import lazy_singledispatch
from ibis.common.exceptions import IbisTypeError
from ibis.common.typing import evaluate_typehint
from ibis.util import flatten_iterable, is_function, is_iterable

class Validator(Callable):
    """Abstract base class for defining argument validators."""

    __slots__ = ()

    @classmethod
    def from_annotation(cls, annot, module=None):
        # TODO(kszucs): cache the result of this function
        annot = evaluate_typehint(annot, module)
        origin_type = get_origin(annot)

        if origin_type is Union:
            inners = map(cls.from_annotation, get_args(annot))
            return any_of(tuple(inners))
        elif origin_type is list:
            (inner,) = map(cls.from_annotation, get_args(annot))
            return list_of(inner)
        elif origin_type is tuple:
            (inner,) = map(cls.from_annotation, get_args(annot))
            return tuple_of(inner)
        elif origin_type is dict:
            key_type, value_type = map(cls.from_annotation, get_args(annot))
            return dict_of(key_type, value_type)
        elif origin_type is Annotated:
            annot, *extras = get_args(annot)
            return all_of((instance_of(annot), *extras))
        elif annot is Any:
            return any_
        else:
            return instance_of(annot)

# TODO(kszucs): in order to cache valiadator instances we could subclass
# grounds.Singleton, but the imports would need to be reorganized
class Curried(toolz.curry, Validator):
    """Enable convenient validator definition by decorating plain functions."""

    def __repr__(self):
        return '{}({}{})'.format(
            self.func.__name__,
            repr(self.args)[1:-1],
            ', '.join(f'{k}={v!r}' for k, v in self.keywords.items()),
        )

validator = Curried

@validator
def ref(key, *, this):
    try:
        return this[key]
    except KeyError:
        raise IbisTypeError(f"Could not get `{key}` from {this}")

@validator
def any_(arg, **kwargs):
    return arg

@validator
def option(inner, arg, *, default=None, **kwargs):
    if arg is None:
        if default is None:
            return None
        elif is_function(default):
            arg = default()
        else:
            arg = default
    return inner(arg, **kwargs)

@validator
def instance_of(klasses, arg, **kwargs):
    """Require that a value has a particular Python type."""
    if not isinstance(arg, klasses):
        # TODO(kszucs): unify errors coming from various validators
        raise IbisTypeError(
            f"Given argument with type {type(arg)} is not an instance of {klasses}"
        )
    return arg

class lazy_instance_of(Validator):
    """A version of `instance_of` that accepts class qualnames instead of "
    "already imported classes.

    Useful for delaying imports.
    """

    def __init__(self, classes):
        classes = (classes,) if isinstance(classes, str) else tuple(classes)
        self._classes = classes
        self._check = lazy_singledispatch(lambda x: False)
        self._check.register(classes, lambda x: True)

    def __repr__(self):
        return f"lazy_instance_of(classes={self._classes!r})"

    def __call__(self, arg, **kwargs):
        if self._check(arg):
            return arg
        raise IbisTypeError(
            f"Given argument with type {type(arg)} is not an instance of "
            f"{self._classes}"
        )

@validator
def any_of(inners, arg, **kwargs):
    """At least one of the inner validators must pass."""
    for inner in inners:
        with suppress(IbisTypeError, ValueError):
            return inner(arg, **kwargs)

    raise IbisTypeError(
        "argument passes none of the following rules: "
        f"{', '.join(map(repr, inners))}"
    )

one_of = any_of

@validator
def all_of(inners, arg, **kwargs):
    """All of the inner validators must pass.

    The order of inner validators matters.

    Parameters
    ----------
    inners : List[validator]
      Functions are applied from right to left so allof([rule1, rule2], arg) is
      the same as rule1(rule2(arg)).
    arg : Any
      Value to be validated.

    Returns
    -------
    arg : Any
      Value maybe coerced by inner validators to the appropiate types
    """
    for inner in inners:
        arg = inner(arg, **kwargs)
    return arg

@validator
def isin(values, arg, **kwargs):
    if arg not in values:
        raise ValueError(f'Value with type {type(arg)} is not in {values!r}')
    if isinstance(values, dict):  # TODO check for mapping instead
        return values[arg]
    else:
        return arg

@validator
def map_to(mapping, variant, **kwargs):
    try:
        return mapping[variant]
    except KeyError:
        raise ValueError(f'Value with type {type(variant)} is not in {mapping!r}')

@validator
def container_of(inner, arg, *, type, min_length=0, flatten=False, **kwargs):
    if not is_iterable(arg):
        raise IbisTypeError('Argument must be a sequence')

    if len(arg) < min_length:
        raise IbisTypeError(f'Arg must have at least {min_length} number of elements')

    if flatten:
        arg = flatten_iterable(arg)

    return type(inner(item, **kwargs) for item in arg)

@validator
def mapping_of(key_inner, value_inner, arg, *, type, **kwargs):
    return type(
        (key_inner(k, **kwargs), value_inner(v, **kwargs)) for k, v in arg.items()
    )

@validator
def int_(arg, min=0, max=math.inf, **kwargs):
    if not isinstance(arg, int):
        raise IbisTypeError('Argument must be an integer')
    if arg < min:
        raise ValueError(f'Argument must be greater than {min}')
    if arg > max:
        raise ValueError(f'Argument must be less than {max}')
    return arg

@validator
def min_(min, arg, **kwargs):
    if arg < min:
        raise ValueError(f'Argument must be greater than {min}')
    return arg

str_ = instance_of(str)
bool_ = instance_of(bool)
none_ = instance_of(type(None))
dict_of = mapping_of(type=dict)
list_of = container_of(type=list)
tuple_of = container_of(type=tuple)


import tkinter as tk
from PIL import ImageTk, Image
from .ai import AI

class Screen():
    """
    creates GUI, receives user input and deliver it to game, and runs
    the game accordingly. along with user's moves, also manages ai moves if
    necessary.
    """
    def __init__(self, game):
        """
        initializes screen.
        """
        self.__game = game
        self.__init_root()
        self.__init_board()
        self.__init_start()

    def run(self):
        """
        after initialization, runs the game.
        """
        self.__root.mainloop()

    def __exit_game(self, event):
        """
        closes GUI.
        """
        self.__root.destroy()

    def __init_root(self):
        """
        initializes GUI.
        """
        self.__root = tk.Tk()
        self.__root.title("Game of Coins")
        self.__root.geometry("850x570")
        self.__root.resizable(0, 0)

    def __init_board(self):
        """
        draws a clean board on the screen, including a background and an exit
        button.
        """
        self.__bg_img = ImageTk.PhotoImage(Image.open("ex12/ex12.png"))
        self.__bg = tk.Label(self.__root, image=self.__bg_img)
        self.__bg.pack()

        self.__exit_img = ImageTk.PhotoImage(Image.open("ex12/exit.png"))
        self.__exit = tk.Button(self.__bg, image=self.__exit_img, bg="black")
        self.__exit.place(relx=0.79, rely=0.005)
        self.__exit.bind("<Button-1>", self.__exit_game)

        self.__board = tk.Label(self.__bg, bg="black")
        self.__coin = ImageTk.PhotoImage(Image.open("ex12/coin.png"))
        self.__rcoin = ImageTk.PhotoImage(Image.open("ex12/rcoin.png"))
        self.__bcoin = ImageTk.PhotoImage(Image.open("ex12/bcoin.png"))
        self.__clean_board()

    def __clean_board(self):
        """
        crates a clean board, and cleans the game's board record.
        """
        self.__spots = [[] for row in range(self.__game.get_data("column_length"))]
        for row in range(self.__game.get_data("column_length")):
            for column in range(self.__game.get_data("row_length")):
                spot = tk.Button(self.__board, image=self.__coin, bg="black")
                spot.grid(row=row, column=column)
                spot.bind("<Button-1>", self.__spot_pressed(row, column))
                self.__spots[row].append(spot)
        self.__game.clean_board()

    def __init_start(self):
        """
        draws start screen, including four options.
        """
        self.__robot1 = ImageTk.PhotoImage(Image.open("ex12/robot1.png"))
        self.__robot2 = ImageTk.PhotoImage(Image.open("ex12/robot2.png"))
        self.__humans = ImageTk.PhotoImage(Image.open("ex12/humans.png"))
        self.__robots = ImageTk.PhotoImage(Image.open("ex12/robots.png"))
        self.__button1 = tk.Button(self.__bg, image=self.__robot1, bg="black")
        self.__button2 = tk.Button(self.__bg, image=self.__robots, bg="black")
        self.__button3 = tk.Button(self.__bg, image=self.__robot2, bg="black")
        self.__button4 = tk.Button(self.__bg, image=self.__humans, bg="black")
        self.__button1.place(relx=0.5, rely=0.15)
        self.__button3.place(relx=0.5, rely=0.55)
        self.__button2.place(relx=0.09, rely=0.55)
        self.__button4.place(relx=0.09, rely=0.15)
        self.__button1.bind("<Button-1>", self.__ai_player)
        self.__button2.bind("<Button-1>", self.__two_ai)
        self.__button3.bind("<Button-1>", self.__player_ai)
        self.__button4.bind("<Button-1>", self.__two_players)

    def __start_game(self):
        """
        removes starting screen, and places the board on the screen.
        """
        self.__button1.destroy()
        self.__button2.destroy()
        self.__button3.destroy()
        self.__button4.destroy()
        self.__board.place(relx=0.5, rely=0.48, anchor="center")

    def __spot_pressed(self, i, j):
        """
        a function for board initializing. creates a matching function for
        a given cell, which is attached to the relevant button.
        """
        def re(event):
            """
            user input function. activated when a button is pressed.
            if move is valid, makes the move. if necessary, preforms
            the following ai move.
            """
            if self.__player != "both" and\
                    self.__player != self.__game.get_current_player():
                return  # not player's turn.

            else:
                try:
                    self.__game.make_move(j)  # player's move.
                    self.__add_coin(*self.__game.get_data("last_move"))

                    if (self.__player == 1 or self.__player == 2) and\
                            self.__game.get_winner() is None:  # play ai move.
                        self.__game.make_move(self.__ai.find_legal_move())
                        self.__add_coin(*self.__game.get_data("last_move"))
                except:
                    pass

            # check for winner
            self.__should_end()

        return re

    def __add_coin(self, i, j, player):
        """
        draws a coin on the board
        :param i: row
        :param j: column
        :param player: which player coin to draw.
        """
        if player == 1:
            color = self.__rcoin
        else:
            color = self.__bcoin
        self.__spots[i][j].configure(image=color)

    def __should_end(self):
        """
        checks if a game should end, shows end message if True.
        """
        winner = self.__game.get_winner()
        if winner is None:
            return

        if winner == 0:
            self.__end_img = ImageTk.PhotoImage(
                Image.open("ex12/tie.png"))
        elif winner == 1:
            self.__show_winner_coins()
            self.__end_img = ImageTk.PhotoImage(
                Image.open("ex12/player1win.png"))
        else:  # winner == 2
            self.__show_winner_coins()
            self.__end_img = ImageTk.PhotoImage(
                Image.open("ex12/player2win.png"))

        self.__end = tk.Button(self.__root, image=self.__end_img, bg="black",
                               command=self.__restart_game)
        self.__end.place(relx=0.32, rely=0.85)
        self.__game.game_on = False # prevents any more moves during end message

    def __restart_game(self):
        """
        after clicking on end message, restarts game.
        """
        self.__game.game_on = True
        self.__end.destroy()
        self.__bg.destroy()
        self.__init_board()
        self.__init_start()

    def __show_winner_coins(self):
        """
        shows the winner coins
        """
        winner = self.__game.get_winner()
        coin_list = self.__game.winner_coins
        if winner == 1:
            self.__win_img = ImageTk.PhotoImage(Image.open("ex12/rcoinwin.png"))
        else:
            self.__win_img = ImageTk.PhotoImage(Image.open("ex12/bcoinwin.png"))
        for winner_coin in coin_list:
            self.__spots[winner_coin[0]][winner_coin[1]].configure(
                image=self.__win_img)

    #  main buttons functions - running the game according to user's choice:
    def __two_players(self, event):
        """
        both player are humans.
        """
        self.__player = "both"
        self.__start_game()

    def __ai_player(self, event):
        """
        ai plays first.
        """
        self.__player = 2
        self.__ai = AI(self.__game, 1)
        self.__game.make_move(self.__ai.find_legal_move())
        self.__add_coin(*self.__game.get_data("last_move"))
        self.__start_game()

    def __player_ai(self, event):
        """
        human plays first.
        """
        self.__player = 1
        self.__ai = AI(self.__game, 2)
        self.__start_game()

    def __two_ai(self, event):
        """
        tow ai playing against each other.
        """
        self.__player = None
        self.__ai1 = AI(self.__game, 1)
        self.__ai2 = AI(self.__game, 2)
        self.__start_game()
        self.__play_two_ai()

    def __play_two_ai(self):
        """
        while game has not ended, preforms ai moves.
        """
        if self.__game.game_on:
            self.__ai1, self.__ai2 = self.__ai2, self.__ai1
            try:
                move = self.__ai2.find_legal_move()
                self.__game.make_move(move)
                self.__root.after(30, self.__play_two_ai)
                self.__add_coin(*self.__game.get_data("last_move"))
                self.__should_end()
            except:
                pass


from datetime import datetime, timedelta
from airflow import DAG
from airflow.operators.dummy_operator import DummyOperator
from airflow.operators.python_operator import PythonOperator

# Define the Python function to be executed
def print_hello():
    return 'Hello from the PythonOperator!'

# Set the default arguments for the DAG
default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'email_on_failure': False,
    'email_on_retry': False,
    'retries': 1,
    'retry_delay': timedelta(minutes=1),
    'start_date': datetime(2023, 1, 1),
}

# Define the DAG
dag = DAG(
    'hello_world_dag',
    default_args=default_args,
    description='A simple Hello World DAG',
    schedule_interval=timedelta(days=1),
    catchup=False,
)

# Define the tasks
start_task = DummyOperator(
    task_id='start_task',
    dag=dag,
)

hello_task = PythonOperator(
    task_id='hello_task',
    python_callable=print_hello,
    dag=dag,
)

end_task = DummyOperator(
    task_id='end_task',
    dag=dag,
)

# Set the task dependencies
start_task >> hello_task >> end_task


from bs4 import BeautifulSoup
from celery import Celery
from minio import Minio
import re
import requests
import io
import json
import logging
from conf import BUCKET_NAME, MINIO_URL, MINIO_ACCESS_KEY, MINIO_SECRET_KEY, RABBITMQ_URI, REDIS_URI
import dask

logging.basicConfig()
logging.getLogger().setLevel(logging.INFO)

app = Celery('tasks',
    backend=REDIS_URI,
    broker=RABBITMQ_URI
    )

client = Minio(
    MINIO_URL,
    access_key=MINIO_ACCESS_KEY,
    secret_key=MINIO_SECRET_KEY,
    secure=False,
    )

bucket_name = BUCKET_NAME

def getCrewData(url):
    crew_data = {
        "crew": []
    }
    r = requests.get(url=url)

    # Create a BeautifulSoup object
    soup = BeautifulSoup(r.text, 'html.parser')

    #page title
    title = soup.find('title')
    crew_data["title"] = title.string
    cast_list = soup.find("table", {"class" : "cast_list"})

    trows = cast_list.find_all('tr')

    for tr in trows:
        td = tr.find_all('td')
        if len(td)==4:
            row = [i.text for i in td]
            crew_data["crew"].append({
                "name":re.sub("[^a-zA-Z' ]+", '', row[1]).strip(),
                "character":re.sub("[^a-zA-Z' ]+", '', row[3]).strip()
            })
    return crew_data

def getAllGenres():
    genres = []
    url = f"https://www.imdb.com/search/title/"
    r = requests.get(url=url, stream=True)
    soup = BeautifulSoup(r.text, 'html.parser')
    res = soup.find_all("tbody")
    for r in res:
        r_td = r.find_all("td")
        for td in r_td:
            genre = td.find("input", {"name": "genres"})
            if genre:
                genres.append(genre.get("value"))
    return genres

def getMovieDetails(url, scrape_ts):
    logging.info(f"Getting details for {url}")
    data = {}
    r = requests.get(url=url)
    # Create a BeautifulSoup object
    soup = BeautifulSoup(r.text, 'html.parser')

    # scrape timestamp
    data["scrape_ts"] = scrape_ts

    # page title
    title = soup.title.string
    data["title"] = title
    # data["title"] = title.split(' - IMDb')[0]

    # title url
    data["url"] = url

    try:
        data["poster_url"] = soup.find("div", {"class": "poster"}).find('img')['src']
    except:
        data["poster_url"] = None
        logging.debug(f"Poster URL for {url} is None")

    # rating
    try:
        ratingValue = soup.find("span", {"itemprop" : "ratingValue"})
        data["ratingValue"] = ratingValue.string
    except:
        data["ratingValue"] = None
        logging.debug(f"Rating value is None for url {url}")

    # no of rating given
    try:
        ratingCount = soup.find("span", {"itemprop" : "ratingCount"})
        data["ratingCount"] = ratingCount.string
    except:
        data["ratingCount"] = None
        logging.debug(f"Rating count is None for url {url}")

    # name
    titleName = soup.find("div",{'class':'titleBar'}).find("h1")
    data["name"] = titleName.contents[0].strip()
    # data["name"] = titleName.contents[0].replace(u'\xa0', u'')

    # is_series
    # check if the string "Episodes" is present
    try:
        isSeries = soup.find("div", {"class": "article"}).find("h2")
        data["isSeries"] = True
    except:
        data["isSeries"] = False

    # duration
    try:
        data["duration"] = soup.find("time").string.strip()
    except:
        data["duration"] = None
        logging.debug(f"Duration is None for url {url}")

    # genre
    try:
        data["genres"] = []
        genre = soup.find("div", {"class": "subtext"}).find_all("a", href=True)[:-1]
        for g in genre:
            data["genres"].append(re.search(r">.*<\/a>", str(g)).group()[1:-4])
    except:
        data["genres"] = None
        logging.debug(f"Genres is None for url {url}")

    # release date
    try:
        release_date = soup.find("div", {"class": "subtext"}).find_all("a", href=True)[-1]
        data["release_date"] = release_date.string.strip()
    except:
        data["release_date"] = None
        logging.debug(f"Release date is None for url {url}")

    # movie rated
    # subtext = soup.find("div", {"class": "subtext"})
    # print(f"subtext: {subtext}")
    # print(f"subtext text: {subtext.text}")
    # movie_rated = subtext.text.split()[0]

    # print(movie_rated)
    # # print(movie_rated.text.split())

    # data["movie_rated"] = movie_rated

    # summary
    try:
        summary_text = soup.find("div",{'class':'summary_text'})
        data["summary_text"] = summary_text.text.strip()
    except:
        data["summary_text"] = None
        logging.debug(f"Summary text is None for url {url}")

    try:
        credit_summary_item = soup.find_all("div",{'class':'credit_summary_item'})
        data["credits"] = {}
        for i in credit_summary_item:
            item = i.find("h4")
            names = i.find_all("a")
            data["credits"][item.string] = []
            for i in names:
                data["credits"][item.string].append({
                    "link": i["href"],
                    "name": i.string
                })
    except:
        data["credits"] = None
        logging.debug(f"Credits is None for url {url}")

    return data

def put_json(bucket_name, object_name, d):
    """
    jsonify a dict and write it as object to the bucket
    """
    # prepare data and corresponding data stream
    data = json.dumps(d).encode("utf-8")
    data_stream = io.BytesIO(data)
    data_stream.seek(0)

    # put data as object into the bucket
    client.put_object(
        bucket_name=bucket_name,
        object_name=object_name,
        data=data_stream,
        length=len(data),
        content_type="application/json"
    )

@app.task
def writeToFile(chunk, object_name, scrape_ts):
    '''
    for each chunk (metadata of 5 titles in json format),
    dump them into one .json file and store that .json file
    in the folder which has the name of today's date
    '''
    title_list = []
    for link in chunk:  # loop thru all the 5 json data
        url = f"https://www.imdb.com{link}"
        title = getMovieDetails(url, scrape_ts)
        title_list.append(title)

    put_json(bucket_name, object_name, title_list)


"""Base Invertible Module Class"""

from typing import Tuple, Iterable
import tensorflow as tf

class InvertibleModule(tf.keras.layers.Layer):
    """
    Generic invertible layer for invertible
    neural network structures.

    Used to implement `Nice`, `Glow`, `RNVP` etc.
    """

    def __init__(self, dims_in: Tuple[int], dims_c: Iterable[Tuple[int]] = None):
        """
        Args:
            dims_in: a tuple specifying the shape of the input,
                     excluding the batch dimension, to this
                     operator: ``dims_in = (dim_0,..., channels)``
            dims_c:  a list of tuples specifying the shape
                     of the conditions to this operator,
                     excluding the batch dimension

        ** Note  to implementors:**

        - The shapes are in the standard TensorFlow 'channels_last' format.
        """
        super().__init__()
        if dims_c is None:
            dims_c = []
        self.dims_in = tuple(dims_in)
        self.dims_c = list(dims_c)

    def call(  # pylint: disable=W0221
        self,
        x_or_z: tf.Tensor,
        c: Iterable[tf.Tensor] = None,
        rev: bool = False,
        jac: bool = True,
    ):
        """
        Perform a forward (default, ``rev=False``) or
        backward pass (``rev=True``) through this layer.
        Args:
            x_or_z: input data (array-like of one or more tensors)
            c:      conditioning data (array-like of none or more tensors)
            rev:    perform backward pass
            jac:    return Jacobian associated to the direction
        """
        raise NotImplementedError(
            f"{self.__class__.__name__} does not provide call(...) method"
        )

    def get_config(self):
        "Needed within TensorFlow to serialize this layer"
        config = {"dims_in": self.dims_in, "dims_c": self.dims_c}
        base_config = super().get_config()
        return dict(list(base_config.items()) + list(config.items()))


import matplotlib.pyplot as plt

def plot_many(rows, cols, width, height, data=None, titles=None, xlim=None):
    fig, plots = plt.subplots(rows, cols)
    fig.set_figheight(height)
    fig.set_figwidth(width)

    if data is not None:
        for p, d in zip(plots, data):
            for c in d:
                p.plot(*c)
    if titles is not None:
        for p, c in zip(plots, titles):
            p.set_title(c)
    if xlim is not None:
        for p in plots:
            p.set_xlim(xlim)
    return plots


"""Constants for the AMASTech integration."""
from __future__ import annotations

from collections.abc import Callable
from dataclasses import dataclass
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
import json, logging, asyncio, hashlib
import aiohttp
import async_timeout
from datetime import timedelta, datetime
from typing import Any
from homeassistant.components.sensor import SensorEntityDescription, SensorDeviceClass
from homeassistant.components.time import TimeEntityDescription
from homeassistant.components.number import NumberEntityDescription
from homeassistant.core import HomeAssistant
from homeassistant.components.binary_sensor import (
    BinarySensorDeviceClass,
    BinarySensorEntityDescription,
)
from homeassistant.const import TEMP_CELSIUS, PERCENTAGE
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives import padding
import os
from binascii import a2b_base64, b2a_base64, hexlify
from json import loads, dumps

_LOGGER = logging.getLogger(__name__)

DOMAIN = 'amas'
DEFAULT_NAME = 'AMAS'
DATA_KEY_API = 'api'
DATA_KEY_COORDINATOR = 'coordinator'
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=1)

def decrypt(payload, token):
     enc = a2b_base64(payload)
     IV = enc[:16]
     message = enc[16:]
     hmm = Cipher(algorithms.AES(token), modes.CBC(IV))
     decryptor = hmm.decryptor()
     data = decryptor.update(message) + decryptor.finalize()
     data = bytes((x for x in data if x >= 0x20 and x < 127))
     data = data.decode()
     return data

def encrypt(data, token):
    padder = padding.PKCS7(128).padder()
    data = padder.update(data) + padder.finalize()
    IV = os.urandom(16)
    cipher = Cipher(algorithms.AES(token), modes.CBC(IV))
    encryptor = cipher.encryptor()
    ct = encryptor.update(data) + encryptor.finalize()
    enc = IV + ct
    enc = b2a_base64(enc).decode().strip()
    return enc

def calculate_mac(enc):
	cbcmac = hashlib.sha256(enc.encode())
	cbcmac = hexlify(cbcmac.digest()).decode().strip()
	return cbcmac

# Encrypt and Calculate MAC
def encryptAndMac(message, token, mactoken):
	base64enc = encrypt(message, token)
	cbcmac = calculate_mac(base64enc)
	base64mac = encrypt(cbcmac.encode(), mactoken)
	return dumps({'base64enc': base64enc, 'base64mac': base64mac})

# Verify and Decrypt
def decryptAndVerify(enc_and_mac, token, mactoken):
	cbcmac = decrypt(enc_and_mac['base64mac'], mactoken)
	if calculate_mac(enc_and_mac['base64enc']) == cbcmac:
		return decrypt(enc_and_mac['base64enc'], token)
	else:
		return False

class AMASHub:
    """AMASHub class to check authentication and get device info.

    """

    def __init__(self, host: str, hass: HomeAssistant, session: aiohttp.ClientSession) -> None:
        """Initialize."""
        self.host = host
        self.session = session
        self.hass = hass
        self.loop = hass.loop
        self.api_key = ''
        self.mactoken = ''
        self.device_info = {}
        self.last_update = 0
        self.stream_task = None

    async def authenticate(self, api_key: str, mactoken: str) -> bool:
        """Test if we can decrypt responses."""
        url = 'http://' + self.host + '/control'
        try:
            api_key = a2b_base64(api_key)
            mactoken = a2b_base64(mactoken)
            body=loads(encryptAndMac(dumps({'state': {'desired': {}}}).encode(), api_key, mactoken))
            # r = requests.get(url, headers=headers)
            async with async_timeout.timeout(20):
                response = await self.session.post(url, json=body)
            if response.status == 200:
                payload = await response.json()
                _LOGGER.debug("Reponse content: %s", dumps(payload))
                try:
                    device_info = loads(decryptAndVerify(payload, api_key, mactoken))
                    device_info = device_info['state']['reported']
                except: raise ConfigEntryAuthFailed
                self.api_key = api_key
                self.mactoken = mactoken
                self.device_info = device_info
                self.last_update = datetime.now().strftime('%s')
                return True
            elif response.status == 500:
                 raise ConfigEntryNotReady
        except Exception as e:
            _LOGGER.warning("Failed to connect: %s", str(e))
            raise ConfigEntryNotReady

    async def check_connection(self) -> bool:
        """ Test if we can reconnect """
        url = 'http://' + self.host + '/control'
        try:
            body=loads(encryptAndMac(dumps({'state': {'desired': {}}}).encode(), self.api_key, self.mactoken))
            async with async_timeout.timeout(20):
                response = await self.session.post(url, json=body)
            if response.status == 200:
                payload = await response.json()
                _LOGGER.debug("Reponse content: %s", dumps(payload))
                try:
                    device_info = loads(decryptAndVerify(payload, self.api_key, self.mactoken))
                    device_info = device_info['state']['reported']
                except: raise ConfigEntryAuthFailed
                self.device_info = device_info
                self.last_update = datetime.now().strftime('%s')
                return True
            elif response.status == 500:
                 raise ConfigEntryNotReady
        except Exception as e:
            _LOGGER.warning("Failed to connect: %s", str(e))
            raise ConfigEntryNotReady

    async def control_device(self, state: dict[str, Any]) -> None:
        """Control device."""
        url = 'http://' + self.host + '/control'
        body = {'state': {'desired': state}}
        payload = loads(encryptAndMac(dumps(body).encode(), self.api_key, self.mactoken).encode())
        try:
            # r = requests.post(url, headers=headers, body=body)
            async with async_timeout.timeout(20):
                response = await self.session.post(url, json=payload)
                _LOGGER.debug("Response content: %s", str(response.content))
            if response.status == 200:
                device_info = await response.json()
                _LOGGER.debug("Reponse content: %s", str(device_info))
                try:
                    device_info = loads(decryptAndVerify(device_info, self.api_key,self.mactoken))
                except: raise ConfigEntryAuthFailed
                device_info = device_info['state']['reported']
                self.device_info = device_info
                self.last_update = datetime.now().strftime('%s')
                _LOGGER.debug("Device info: %s", str(response.content))
            else:
                _LOGGER.critical("Status code: "+str(response.status))
                raise ConfigEntryNotReady
        except Exception as e:
            _LOGGER.warning("Failed to connect: %s", str(e))
            raise ConfigEntryNotReady

    async def stream_info(self) -> None:
        url = 'http://' + self.host + '/metrics'
        try:
            async with self.session.ws_connect(url) as ws:
                async for msg in ws:
                    _LOGGER.debug('WSMsgType: ' + str(msg.type))
                    if msg.type == aiohttp.WSMsgType.ERROR:
                        await ws.close()
                    elif msg.type == aiohttp.WSMsgType.BINARY:
                        try:
                            device_info = loads(decryptAndVerify(loads(msg.data.decode()), self.api_key, self.mactoken))
                            device_info = device_info['state']['reported']
                            self.device_info = device_info
                            self.last_update = datetime.now().strftime('%s')
                        except: await ws.close()
                    elif msg.type == aiohttp.WSMsgType.TEXT:
                        try:
                            device_info = loads(decryptAndVerify(loads(msg.data), self.api_key, self.mactoken))
                            device_info = device_info['state']['reported']
                            self.device_info = device_info
                            self.last_update = datetime.now().strftime('%s')
                        except: await ws.close()
        except: _LOGGER.error('Streaming failed!')

@dataclass
class AMASNumberEntityDescription(NumberEntityDescription):
    """Describes AMASTech number entity."""

    icon: str = "mdi:leaf"

NUMBER_TYPES: tuple[AMASNumberEntityDescription, ...] = (
    AMASNumberEntityDescription(
        key="pump_interval",
        name="Pump Interval",
        icon="mdi:water-pump-off",
        native_max_value=86399,
        native_min_value=600,
    ),
    AMASNumberEntityDescription(
        key="pump_runtime",
        name="Pump Runtime",
        icon="mdi:water-pump",
        native_max_value=86399,
        native_min_value=60,
    ),
)

@dataclass
class AMASTimeEntityDescription(TimeEntityDescription):
    """Describes AMASTech time entity."""

    icon: str = "mdi:leaf"

TIME_TYPES: tuple[AMASTimeEntityDescription, ...] = (
    AMASTimeEntityDescription(
        key="light_on",
        name="Light On",
        icon="mdi:lightbulb-on",
    ),
    AMASTimeEntityDescription(
        key="light_off",
        name="Light Off",
        icon="mdi:lightbulb-off-outline",
    ),
)

@dataclass
class AMASSensorEntityDescription(SensorEntityDescription):
    """Describes AMASTech sensor entity."""

    icon: str = "mdi:leaf"

SENSOR_TYPES: tuple[AMASSensorEntityDescription, ...] = (
    AMASSensorEntityDescription(
        key="ambient_temperature",
        name="Ambient Temperature",
        native_unit_of_measurement=TEMP_CELSIUS,
        icon="mdi:thermometer",
        device_class=SensorDeviceClass.TEMPERATURE,
    ),
    AMASSensorEntityDescription(
        key="relative_humidity",
        name="Relative Humidity",
        native_unit_of_measurement=PERCENTAGE,
        icon="mdi:water-percent",
        device_class=SensorDeviceClass.HUMIDITY,
    ),
    AMASSensorEntityDescription(
        key="water_level",
        name="Water Level",
        native_unit_of_measurement=PERCENTAGE,
        entity_registry_enabled_default=False,
        icon="mdi:waves-arrow-up",
    ),
)

@dataclass
class RequiredAMASBinaryDescription:
    """Represent the required attributes of the AMASTech binary description."""

    state_value: Callable[[AMASHub], bool]

@dataclass
class AMASBinarySensorEntityDescription(
    BinarySensorEntityDescription, RequiredAMASBinaryDescription
):
    """Describes AMASTech binary sensor entity."""

    extra_value: Callable[[AMASHub], dict[str, Any] | None] = lambda api: None

BINARY_SENSOR_TYPES: tuple[AMASBinarySensorEntityDescription, ...] = (
    AMASBinarySensorEntityDescription(
        key="light_status",
        name="Light Status",
        entity_registry_enabled_default=True,
        device_class=BinarySensorDeviceClass.LIGHT,
        state_value=lambda api: bool(api.device_info['light']['status']),
    ),
    AMASBinarySensorEntityDescription(
        key="pump_status",
        name="Pump Status",
        entity_registry_enabled_default=True,
        device_class=BinarySensorDeviceClass.RUNNING,
        state_value=lambda api: bool(api.device_info['pump']['status']),
    ),
    AMASBinarySensorEntityDescription(
        key="water_level_alert",
        name="Water Level Alert",
        entity_registry_enabled_default=True,
        device_class=BinarySensorDeviceClass.BATTERY,
        state_value=lambda api: api.device_info['alerts']['water_level_alert'] == 'Low',
    ),
    AMASBinarySensorEntityDescription(
        key="amb_temp_alert",
        name="Ambient Temperature Alert",
        entity_registry_enabled_default=False,
        device_class=BinarySensorDeviceClass.PROBLEM,
        state_value=lambda api: api.device_info['alerts']['temp_alert'] == 'Low' or api.device_info['alerts']['temp_alert'] == 'High',
    ),
    AMASBinarySensorEntityDescription(
        key="rel_humidity_alert",
        name="Relative Humidity Alert",
        entity_registry_enabled_default=False,
        device_class=BinarySensorDeviceClass.PROBLEM,
        state_value=lambda api: api.device_info['alerts']['humidity_alert'] == 'Low' or api.device_info['alerts']['humidity_alert'] == 'High',
    ),
)


import re as Regx

def Print(Board):
	for Row in Board:
		print(Row)

def CopyDictionary(Dictionary):
	CopiedDictionary = {}
	for UniquePiece in Dictionary: # PieceName
		EachPieceList = []
		for Piece in Dictionary[UniquePiece]: # Associated List, Piece is Dictionary
			Attributes = {}
			for PieceAttribute in Piece: # Keys in Piece
				if PieceAttribute != 'Label':
					Attributes[PieceAttribute] = Piece[PieceAttribute]
			EachPieceList.append(Attributes)
		CopiedDictionary[UniquePiece] = EachPieceList
	return CopiedDictionary

def IsDigit(Check):
	Digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
	IsNumber = True

	for Character in Check:
		if Character not in Digits:
			return not IsNumber
	return IsNumber

def RefineGameFile(OpenedGame):

	"""
		A) File Can Have Entries Other Than Chess Lines But Those Lines Should Start From '#'
		B) File Can Have Blank Lines
		C) Moves Should Start From Number
			All of the below specified Formats can be used to Enter Moves in .txt File.
			-> For Example: 1. e4 e5
						 	1) e4 e5 or 1] e4 e5 or 1} e4 e5
						 	1 e4 e5
						 	1: e4 e5
						 	1> e4 e5
	"""

	RemoveCharacters = ['.', ')', '}', ']', ':', '>']
	Game = {}

	WhiteMoves = []
	BlackMoves = []

	if OpenedGame == None:
		return [False]
	else:
		for Move in OpenedGame:
			if Move == '\n': # Ignore Empty Lines
				continue
			else:
				for i in range(0, len(Move)):
					if Move[i] in RemoveCharacters:
						Move = Move[:i] + Move[(i+1):]
						break
				Move = Move.split()

				if IsDigit(Move[0]):
					if len(Move) == 2:
						WhiteMoves.append((int(Move[0]), Move[1]))
					elif len(Move) == 3:
						WhiteMoves.append((int(Move[0]), Move[1]))
						BlackMoves.append((int(Move[0]), Move[2]))
				else:
					continue

		Game['WhiteMoves'] = WhiteMoves
		Game['BlackMoves'] = BlackMoves
		Game['NumberOfMoves'] = len(WhiteMoves) + len(BlackMoves)

		return Game

def GetMinorPiece(Side, Starting, MinorPieceDic):
	if Side == '1':
		ChessPieces = ['White' + MinorPieceDic[Starting]]
	elif Side == '2':
		ChessPieces = ['Black' + MinorPieceDic[Starting]]

	return ChessPieces

def DecodeMove(ChessMove, Side):
	MinorPiece = {
		'K': 'King',
		'B': 'Bishop',
		'N': 'Knight',
		'R': 'Rook',
		'Q': 'Queen'
	}

	Symbols = {
			"!": "Good Move",
			"!!": "Brilliant Move",
			"!?": "Interesting Move",
			"?!": "Dubious Move",
			"?": "A Weak Move",
			"??": "Blunder",
			"+-": "White has a Decisive Advantage",
			"=": "Equal",
			"-+": "Black has a Decisive Advantage",
			"+": "Check",
			"#": "CheckMate",
			"(W+)": "White has Clear Advantage",
			"(W-)": "White has Slight Advantage",
			"(B+)": "Black has Clear Advantage",
			"(B-)": "Black has Slight Advantage",
			"<B>": "Black Won",
			"<W>": "White Won",
			"X": "Resigned",
			"BR": "Black Resigned",
			"WR": "White Resigned",
			"C": "Continued",
			"S": "Stopped",
			"D": "Draw"
		}

	MiscCharacters = ['!', '?', '-', '+', '=', '#', '(', '<']

	Compliment = ""
	for i in range(0,len(ChessMove)):
		if ChessMove[i] in MiscCharacters:
			if ChessMove[i] == '=' and i != len(ChessMove)-1: # c6=Q
				continue
			if ChessMove[i] == '-' and ChessMove[i+1] == 'O':
				continue

			Compliment = Symbols[ChessMove[i:]]
			ChessMove = ChessMove[:i]
			break

	GameEnded = Regx.compile(r'[XCSD]|WR|BR')

	if GameEnded.search(ChessMove) != None:
		return [None, None, None, Symbols[ChessMove]]

	PawnMove = Regx.compile(r'^[abcdefgh]\d$') # b6
	PawnCapture = Regx.compile(r'^[abcdefgh]x[abcdefgh]\d$') # cxb6

	MinorPieceMove1 = Regx.compile(r'^[BKNRQ][abcdefgh]\d$') # Nd2
	MinorPieceMove2 = Regx.compile(r'^[BKNRQ][abcdefgh][abcdefgh]\d$') # Ned2
	MinorPieceMove3 = Regx.compile(r'^[BKNRQ]\d[abcdefgh]\d$') # N7d2

	MinorPieceCapture1 = Regx.compile(r'^[BKNRQ]x[abcdefgh]\d$') # Nxd2
	MinorPieceCapture2 = Regx.compile(r'^[BKNRQ][abcdefgh]x[abcdefgh]\d$') #Nbxd2
	MinorPieceCapture3 = Regx.compile(r'^[BKNRQ]\dx[abcdefgh]\d$') # N7xd2

	ShortCastling = Regx.compile(r'^O-O$')
	LongCastling = Regx.compile(r'^O-O-O$')

	QueenPromotion = Regx.compile(r'^[abcdefgh]\d=Q$') # c6=Q

	Pieces = None
	Action = None
	MovedSquare = None

	if PawnMove.search(ChessMove) != None: # b6
		Action = "Move"
		MovedSquare = [ChessMove]

		if Side == '1':
			Pieces = ['WhitePawn']
		elif Side == '2':
			Pieces = ['BlackPawn']

	elif PawnCapture.search(ChessMove) != None: # cxb6
		Action = "Capture"
		MovedSquare = [ChessMove[0], ChessMove[2:]]

		if Side == '1':
			Pieces = ['WhitePawn']
		elif Side == '2':
			Pieces = ['BlackPawn']

	elif MinorPieceMove1.search(ChessMove) != None: # Nd2
		Action = 'Move'
		MovedSquare = [ChessMove[1:]]
		Pieces = GetMinorPiece(Side, ChessMove[0], MinorPiece)

	elif MinorPieceMove2.search(ChessMove) != None: # Ned2
		Action = 'Move'
		MovedSquare = [ChessMove[1], ChessMove[2:]]
		Pieces = GetMinorPiece(Side, ChessMove[0], MinorPiece)

	elif MinorPieceMove3.search(ChessMove) != None: # N7d2
		Action = 'Move'
		MovedSquare = [ChessMove[1], ChessMove[2:]]
		Pieces = GetMinorPiece(Side, ChessMove[0], MinorPiece)

	elif MinorPieceCapture1.search(ChessMove) != None: # Nxd2
		Action = 'Capture'
		MovedSquare = [ChessMove[2:]]
		Pieces = GetMinorPiece(Side, ChessMove[0], MinorPiece)

	elif MinorPieceCapture2.search(ChessMove) != None: # Nbxd2
		Action = 'Capture'
		MovedSquare = [ChessMove[1], ChessMove[3:]]
		Pieces = GetMinorPiece(Side, ChessMove[0], MinorPiece)

	elif MinorPieceCapture3.search(ChessMove) != None: # N7xd2
		Action = 'Capture'
		MovedSquare = [ChessMove[1], ChessMove[3:]]
		Pieces = GetMinorPiece(Side, ChessMove[0], MinorPiece)

	elif QueenPromotion.search(ChessMove) != None:
		Action = "Promoted"
		MovedSquare = [ChessMove[:2]]
		Pieces = GetMinorPiece(Side, ChessMove[3], MinorPiece)

	elif ShortCastling.search(ChessMove) != None:
		Action = "Short-Castled"
		MovedSquare = ['e1', 'h1']

		if Side == '1':
			Pieces = ['WhiteKing', 'WhiteRook']
		elif Side == '2':
			Pieces = ['BlackKing', 'BlackRook']

	elif LongCastling.search(ChessMove) != None:
		Action = "Long-Castled"
		MovedSquare = ['e1', 'a1']

		if Side == '1':
			Pieces = ['WhiteKing', 'WhiteRook']
		elif Side == '2':
			Pieces = ['BlackKing', 'BlackRook']

	return [Pieces, Action, MovedSquare, Compliment]


def main():
    ref = ["Humpty","dumpty","broke","his","head."]
    hyp = ["Humpty","dumpty","broke","his","nose",""]#"on","the","sidewalk","by","the","church."]

    ref2 = ['is', "lockwood's", 'destination', 'the', 'same', 'as', 'the', "mcclusky's"]
    hyp2 = ['is', 'lockwoods', 'destination', 'the', 'same', 'as', 'the', 'macloskeys']

    hyp3 = ['list', 'confidences', 'threats.']
    ref3 = ['list', "confidence's", 'threats']

    print(cer_wer(ref2,hyp2))
    print(wer(ref,hyp))

MAXIMUM_WRONG_CHARACTERS = 2
MINMUM_CHARACTERS_FOR_CER = 5

def wer(ref,hyp):
    d = []
    #zeros
    for j in range(len(ref)+1):
        d.append([0.0]*(len(hyp)+1))
    #initialize
    for i in range(len(d)):
        for j in range(len(d[0])):
            if i == 0:
                d[0][j] = j
            elif j == 0:
                d[i][0] = i
    #calculate
    for i in range(1, len(d)):
        for j in range(1, len(d[0])):
            if ref[i-1].lower() == hyp[j-1].lower():
                d[i][j] = d[i-1][j-1]
            else:
                sub = d[i-1][j-1] + 1
                ins = d[i][j-1] + 1
                deletion = d[i-1][j] + 1
                d[i][j] = min(sub,ins,deletion)
    return float(d[len(ref)][len(hyp)])/float(len(ref))

def cer_wer(ref,hyp):
    d = []
    #zeros
    for j in range(len(ref)+1):
        d.append([0]*(len(hyp)+1))
    #initialize
    for i in range(len(d)):
        for j in range(len(d[0])):
            if i == 0:
                d[0][j] = j
            elif j == 0:
                d[i][0] = i
    #calculate
    for i in range(1, len(d)):
        for j in range(1, len(d[0])):
            if cer_compare(ref[i-1].lower(),hyp[j-1].lower()):
                d[i][j] = d[i-1][j-1]
            else:
                sub = d[i-1][j-1] + 1
                ins = d[i][j-1] + 1
                deletion = d[i-1][j] + 1
                d[i][j] = min(sub,ins,deletion)
    return float(d[len(ref)][len(hyp)])/float(len(ref))

def cer_compare(ref_tok,hyp_tok):
    """If the reference token has the minimum number of characters
        and the character error rate is below the threshold
        return True
        else return False or their comparison"""
    cer = wer(ref_tok,hyp_tok)
    if len(ref_tok) >= MINMUM_CHARACTERS_FOR_CER:
        if cer <= MAXIMUM_WRONG_CHARACTERS/len(ref_tok):
            return True
        return False
    return ref_tok == hyp_tok

if __name__ == "__main__":
    main()


class Solution(object):
    def groupAnagrams(self, strs):
        """
        :type strs: List[str]
        :rtype: List[List[str]]
        """
        temp = defaultdict(list)

        for i in strs:
            key = "".join(sorted(list(i)))
            temp[key].append(i)

        output = []
        for i in temp.values():
            output.append(i)

        return output


from threading import Thread, current_thread
from UseFul import Variable
from time import sleep

i = 0

variable = Variable([])

threads = []

@variable
def increament_i():
    global i
    i += 1
    increament_i.data.append((current_thread(), i))
    sleep(i/10)

for _ in range(10):
    threads.append(Thread(target=increament_i))

print(repr(variable))
with variable as data:
    print(f"{data}")

for thread in threads:
    thread.start()
    print(repr(variable), i)

print(repr(variable))
with variable as data:
    print(f"{data}")


import os
import shutil
from pathlib import Path

from src.problem import Problem

PATH_TMP_FOLDER = Path("temp")

class Generator(Problem):
    def __init__(self, pid: str):
        super().__init__(pid)

    @staticmethod
    def __ensure_and_clear_temp_folder():
        if not PATH_TMP_FOLDER.exists():
            PATH_TMP_FOLDER.mkdir(parents=True)
        for file in PATH_TMP_FOLDER.iterdir():
            os.remove(file)

    @staticmethod
    def __compile(path: Path):
        if os.system(f"g++ {path} -o {PATH_TMP_FOLDER / path.stem} -O2 -std=c++20 -Wl,--stack=268435456"):
            raise Exception(f"{path} 编译失败")

    def generate(self):
        self._logger.info("生成测试数据")

        self._logger.debug("清空临时文件夹")
        self.__ensure_and_clear_temp_folder()
        if not self._path_std.exists():
            raise Exception("标程不存在")

        self._logger.info("编译标程")
        self.__compile(self._path_std)

        self._logger.info("生成输入样例")
        if self._path_gen_cpp.exists():
            self._logger.info("编译生成器")
            self.__compile(self._path_gen_cpp)
            os.system(f"cd {PATH_TMP_FOLDER} && gen")
        elif self._path_gen_py.exists():
            shutil.copy(self._path_gen_py, PATH_TMP_FOLDER)
            os.system(f"cd {PATH_TMP_FOLDER} && python gen.py")
        else:
            raise Exception("生成器不存在")

        self._logger.info("生成输出样例")
        files_input = PATH_TMP_FOLDER.glob("*.in")
        for file in files_input:
            os.system(f"{PATH_TMP_FOLDER / 'std'} < {file} > {PATH_TMP_FOLDER / (file.stem + '.out')}")
            self._logger.info(f"输出样例 #{file.stem} 已生成")

        self._logger.info("打包测试数据")
        path_zip = self._path / "testcase.zip"
        path_zip.unlink(missing_ok=True)
        os.system(f"7z a -tzip {path_zip} {PATH_TMP_FOLDER / '*.in'} {PATH_TMP_FOLDER / '*.out'}")


import sqlite3

class Cliente:
    def __init__(self):
        None

    #ver habitaciones en el rol cliente
    def ver_habitaciones(self):
        conn = sqlite3.connect('olympo.db')
        cursor = conn.cursor()
        consulta = "SELECT * FROM tb_habitaciones"
        cursor.execute(consulta)

        habitaciones = cursor.fetchall()
        print("Lista de habitaciones:")
        for habitacion in habitaciones:
            print("ID:", habitacion[0], "| Tipo:", habitacion[1], "| Descripción:", habitacion[2], "| Precio:", habitacion[3])

    #ver servicios del hotel
    def ver_servicios(self):
        conn = sqlite3.connect('olympo.db')
        cursor = conn.cursor()

        consulta = "SELECT * FROM tb_servicios"
        cursor.execute(consulta)
        servicios = cursor.fetchall()
        print("Lista de servicios:")
        for servicio in servicios:
            print("ID:", servicio[0], "| Nombre:", servicio[1], "| Descripción:", servicio[2], "| Precio:", servicio[3])

    #realizar una reserva como cliente
    def Reservar(self, tipo_habitacion, cantidad_personas, fecha_inicio, fecha_fin):

        conn = sqlite3.connect('Olympo.db')
        cursor = conn.cursor()

        consulta = "SELECT * FROM tb_habitaciones WHERE tipo = ? AND capacidad >= ?"
        cursor.execute(consulta, (tipo_habitacion, cantidad_personas))
        habitaciones = cursor.fetchall()

        disponible = False
        for habitacion in habitaciones:
            consulta = "SELECT * FROM tb_reservas WHERE id_habitacion = ? AND ((fecha_inicio BETWEEN ? AND ?) OR (fecha_fin BETWEEN ? AND ?))"
            cursor.execute(consulta, (habitacion[0], fecha_inicio, fecha_fin, fecha_inicio, fecha_fin))
            reservas = cursor.fetchall()
            if not reservas:
                disponible = True
                habitacion_id = habitacion[0]
                break
        if disponible:
            consulta = "INSERT INTO tb_reservas (id_habitacion, cantidad_personas, fecha_inicio, fecha_fin) VALUES (?, ?, ?, ?)"
            cursor.execute(consulta, (habitacion_id, cantidad_personas, fecha_inicio, fecha_fin))
            conn.commit()
            print("¡Reserva realizada con éxito!")
        else:
            print("Lo siento, no hay habitaciones disponibles para las fechas seleccionadas.")

#MENU
cl=Cliente
c=int(input('ver habitaciones del hotel=1 \n  consultar servicios del hotel=2 \n realizar una reserva=3 \n '))
if c==1:
    cl.ver_habitaciones()
elif c==2:
    cl.ver_habitaciones()
elif c==3:
    tp=input('ingrese el tipo de habitacion')
    can=int(input('ingrese la cantidad de personas a hospedarse'))
    ini=input('ingrese la fecha de inicio de la reserva')
    fin=input('ingrese la fecha de fin de la reserva')
    cl.Reservar(tp,can,ini,fin)


import time
import requests
from slackclient import SlackClient
from lxml import html
from datetime import datetime
from random import randint, choice
from decouple import config
import json

# list of env vars that should be defined:

# BOT_ID: str
# SLACK_BOT_TOKEN: str
# BOT_NAME: str
# FEEDS: json with dict[str, str], where key is feed name and value is feed url
# FEEDS example: {"bunny": "http://dailybunny.org/feed/", "otter": "http://dailyotter.org/feed/"}

# bot's ID as an environment variable
BOT_ID = config('BOT_ID')

# bot name as env var
BOT_NAME = config('BOT_NAME')

# rss feed store
FEEDS = config('FEEDS', cast=lambda json_input: json.loads(json_input))

# constants
AT_BOT = '<@' + BOT_ID + '>:'

slack_client = SlackClient(config('SLACK_BOT_TOKEN'))

def post_latest_item():
    api_call = slack_client.api_call('channels.list')
    if api_call.get('ok'):

        # select random animal
        animal = choice(list(FEEDS.keys()))

        # use latest img from that animal's feed
        msg = latest_rss_item(FEEDS[animal])
        print('composed random {} msg'.format(animal))

        channels = api_call.get('channels')
        joined_channels = [ch for ch in channels if ch.get('is_member')]

        for chat in joined_channels:
            print('posting to {}'.format(chat.get('name')))
            slack_client.api_call('chat.postMessage', channel=chat.get('id'),
                                  text=msg, as_user=True)

def handle_command(command, channel):
    """
        Receives commands directed at the bot and determines if they
        are valid commands. If so, then acts on the commands. If not,
        returns back what it needs for clarification.
    """
    response = 'Not sure what you mean. Supported animals: {}.'.format(', '.join(FEEDS.keys()))
    command_found = False
    for key in FEEDS.keys():
        if key in command:
            command_found = True
            print('command ' + key)
            response = random_rss_item(FEEDS[key])
            slack_client.api_call('chat.postMessage', channel=channel, text=response, as_user=True)
    if not command_found:
        slack_client.api_call('chat.postMessage', channel=channel, text=response, as_user=True)

def parse_slack_output(slack_rtm_output):
    """
        The Slack Real Time Messaging API is an events firehose.
        this parsing function returns None unless a message is
        directed at the Bot, based on its ID.
    """
    output_list = slack_rtm_output
    if output_list and len(output_list) > 0:
        for output in output_list:
            if output and 'text' in output and AT_BOT in output['text']:
                # return text after the @ mention, whitespace removed
                return output['text'].split(AT_BOT)[1].strip().lower(), \
                       output['channel']
    return None, None

def feed(feed_url):
    xml_feed_page = requests.get(feed_url)
    xml_feed = xml_feed_page.content
    doc = html.fromstring(xml_feed)
    return doc

def msg_from_item(item):
    title = item.find('title').text

    # just find img?
    image_src = item.find('encoded').find('img').get('src')
    return '{} {}'.format(title, image_src)

def latest_rss_item(feed_url):
    doc = feed(feed_url)
    latest_item = doc.find('channel/item')
    return msg_from_item(latest_item)

def random_rss_item(feed_url):
    doc = feed(feed_url)
    items = doc.findall('channel/item')
    random_index = randint(0, len(items) - 1)
    random_item = items[random_index]
    return msg_from_item(random_item)

if __name__ == '__main__':
    READ_WEBSOCKET_DELAY = 1  # 1 second delay between reading from firehose
    if slack_client.rtm_connect():
        print('{} connected and running!'.format(BOT_NAME))
        posted_today = False
        post_time = (12, 1)
        reset_time = (11, 50)

        while True:
            # handle command
            command, channel = parse_slack_output(slack_client.rtm_read())
            if command and channel:
                handle_command(command, channel)

            # at noon each day post a bunny
            timestamp = datetime.now().time()

            if not posted_today and (timestamp.hour, timestamp.minute) == post_time:
                print('time for daily post!')
                post_latest_item()
                posted_today = True

            if posted_today and (timestamp.hour, timestamp.minute) == reset_time:
                posted_today = False

            time.sleep(READ_WEBSOCKET_DELAY)
    else:
        print('Connection failed. Invalid Slack token or bot ID?')


from sklearn.cluster import KMeans
from sklearn.preprocessing import MinMaxScaler
import pandas as pd

def knn_transform(data, column, k):
    """返回数据和质心"""
    data_copy = data.copy()
    data = MinMaxScaler().fit_transform(data)
    estimator = KMeans(n_clusters=k)
    estimator.fit(data)
    data_copy[f"{column}_category"] = pd.DataFrame(estimator.labels_)
    return data_copy

def kd_range(data):
    record = {}
    temp = None
    for i in range(len(data)):
        if temp is None:
            temp = data[i]
            record[temp] = []
            start = i
        elif temp != data[i]:
            # 结束上一次记录
            end = i - 1
            record[temp].append([start, end])
            # 开始下一次记录
            start = i
            temp = data[i]
            # 若没有记录则添加类别记录
            try:
                record[temp]
            except:
                record[temp] = []
            # 如果是最后一个，则直接加入记录
        elif i == len(data) - 1:
            record[temp].append([start, i])
    return {'name': 'record_discretization', 'value': record}


from sqlmodel import Session

from aot_quotes.common.db import Quotes, engine

def seed_data(filename):
    with Session(engine) as session:
        with open(filename, "r") as fp:
            for line in fp.readlines():
                quote_obj = Quotes(quote=line.strip())
                session.add(quote_obj)
            session.commit()

if __name__ == "__main__":
    seed_data("./seed/data.txt")


from __future__ import annotations

from typing import Any

import torch
from pymatgen.core import Structure
from torch_geometric.data import Data
from torchtyping import TensorType  # type: ignore
from typing_extensions import TypeAlias

from torch_m3gnet.data import MaterialGraphKey

class MaterialGraph(Data):
    """
    Args
    ----
    pos: (num_nodes, 3) torch.float
        Cartesian coordinates
    atom_types: (num_nodes, ) torch.long
        Atomic numbers
    num_triple_i: (num_nodes, ) torch.int
        Number of triplets containing each node

    edge_index: (2, num_edges) torch.long
    edge_cell_shift: (num_edges, 3) torch.int
        Unit-cell vector from source node to destination node
    num_triplet_ij: (num_edges, )
        Number of triplets containing nodes in each edge
    triplet_edge_index: (2, num_triplets) torch.long
        triplet_edge_index[:, f] = [e1, e2] form the `f`-th triplet.
        edge_index[:, e1] = [i, j], edge_index[:, e2] = [i, k]
        Then, [i, j, k] is nodes of the `f`-th triplet.

    lattice: (3, 3) torch.float
        Row-wise lattice matrix

    num_nodes: int
    num_edges: int
    num_triplets: int

    edge_weights: (num_edges, ) torch.float
        Distance of each edge
    triplet_cos_angles: (num_triplets, ) torch.float
        Cosine of angle between j-i-k

    x: (num_nodes, num_node_features) torch.float
        Node features
    edge_attr: (num_edges, num_edge_features) torch.float
        Edge features

    atomic_energy: (num_nodes, )
        eV/atom
    total_energy: (1, )
        eV
    forces: (num_nodes, 3)
        eV/AA
    stresses: (6, )
        eV/AA^3 in Voigt order
    """

    def __init__(
        self,
        pos: TensorType["num_nodes", 3] | None = None,  # type: ignore # noqa: F821
        atom_types: TensorType["num_nodes", torch.long] | None = None,  # type: ignore # noqa: F821
        num_triplet_i: TensorType["num_nodes", torch.int] | None = None,  # type: ignore # noqa: F821
        edge_index: TensorType[2, "num_edges", torch.long] | None = None,  # type: ignore # noqa: F821
        edge_cell_shift: TensorType["num_edges", 3, torch.int] | None = None,  # type: ignore # noqa: F821
        num_triplet_ij: TensorType["num_edges", torch.int] | None = None,  # type: ignore # noqa: F821
        triplet_edge_index: TensorType[2, "num_triplets", torch.long] | None = None,  # type: ignore # noqa: F821
        lattice: TensorType[3, 3] | None = None,
    ):
        num_nodes = pos.size(0) if pos is not None else 0
        num_edges = edge_index.size(1) if edge_index is not None else 0
        num_triplets = triplet_edge_index.size(1) if triplet_edge_index is not None else 0

        # TODO: add "state" key

        super().__init__(
            pos=pos,
            atom_types=atom_types,
            num_triplet_i=num_triplet_i,
            edge_index=edge_index,
            edge_cell_shift=edge_cell_shift,
            num_triplet_ij=num_triplet_ij,
            triplet_edge_index=triplet_edge_index,
            lattice=lattice,
            num_nodes=num_nodes,
            num_edges=num_edges,
            num_triplets=num_triplets,
            # Derived properties
            scaled_pos=None,
            scaled_lattice=None,
            edge_distances=None,
            triplet_angles=None,
            edge_weights=None,
            elemental_energies=None,
            # Features
            x=None,
            edge_attr=None,
            # Targets
            scaled_atomic_energies=None,
            scaled_total_energy=None,
            total_energy=None,
            forces=None,
            stresses=None,
        )

    def __cat_dim__(self, key: str, value: Any, *args: Any, **kwargs: Any):
        if key in [MaterialGraphKey.EDGE_INDEX, MaterialGraphKey.TRIPLET_EDGE_INDEX]:
            return 1
        elif key in [
            MaterialGraphKey.LATTICE,
            MaterialGraphKey.TOTAL_ENERGY,
            MaterialGraphKey.STRESSES,
        ]:
            # graph-level properties and so need a new batch dimension
            return None
        else:
            return 0  # cat along node/edge dimension

    def __inc__(self, key: str, value: Any, *args, **kwargs) -> Any:
        if key == MaterialGraphKey.BATCH:
            return int(value.max()) + 1
        elif key == MaterialGraphKey.EDGE_INDEX:
            return self.num_nodes
        elif key == MaterialGraphKey.TRIPLET_EDGE_INDEX:
            return self.num_edges
        else:
            return 0

    @classmethod
    def from_structure(
        cls,
        structure: Structure,
        cutoff: float,
        threebody_cutoff: float,
    ) -> MaterialGraph:
        lattice = torch.as_tensor(structure.lattice.matrix.copy(), dtype=torch.float)
        pos = torch.tensor(structure.cart_coords, dtype=torch.float)

        edge_index, edge_cell_shift, distances = get_all_neighbors_with_cell_shifts(
            structure, cutoff
        )

        # Initialize node features by 0-indexed atomic numbers
        atom_types = torch.as_tensor([site.specie.Z - 1 for site in structure], dtype=torch.long)

        if threebody_cutoff > cutoff:
            raise ValueError("Three body cutoff raidus should be smaller than two body.")
        num_nodes = len(structure)
        triplet_edge_index, num_triplet_i, num_triplet_ij = compute_threebody(
            num_nodes, edge_index, distances, threebody_cutoff
        )

        return cls(
            pos=pos,
            atom_types=atom_types,
            num_triplet_i=num_triplet_i,
            edge_index=edge_index,
            edge_cell_shift=edge_cell_shift,
            num_triplet_ij=num_triplet_ij,
            triplet_edge_index=triplet_edge_index,
            lattice=lattice,
        )

def get_all_neighbors_with_cell_shifts(
    structure: Structure,
    cutoff: float,
) -> tuple[  # type: ignore
    TensorType[2, "num_edges"],  # noqa: F821
    TensorType["num_edges", 3],  # noqa: F821
    TensorType["num_edges"],  # noqa: F821
]:
    """Construct full neighbor list from pymatgen's structure."""
    all_neighbors = structure.get_all_neighbors(r=cutoff)
    src_indices = []
    dst_indices = []
    edge_cell_shifts = []
    distances = []
    for i, neigbors_i in enumerate(all_neighbors):
        for neighbor in neigbors_i:
            src_indices.append(i)
            dst_indices.append(neighbor.index)
            edge_cell_shifts.append(neighbor.image)
            distances.append(neighbor.nn_distance)

    edge_index = torch.vstack([torch.LongTensor(src_indices), torch.LongTensor(dst_indices)])
    edge_cell_shift = torch.as_tensor(edge_cell_shifts).to(torch.int)
    distances = torch.as_tensor(distances, dtype=torch.float)

    return edge_index, edge_cell_shift, distances

def compute_threebody(
    num_nodes: int,
    edge_index: TensorType[2, "num_edges", torch.long],  # type: ignore # noqa: F821
    distances: TensorType["num_edges"],  # type: ignore # noqa: F821
    threebody_cutoff: float,
) -> tuple[  # type: ignore
    TensorType[2, "num_triplets", torch.long],  # noqa: F821
    TensorType["num_nodes", torch.int],  # noqa: F821
    TensorType["num_edges", torch.int],  # noqa: F821
]:
    """
    Returns
    -------
    triplet_edge_index: (2, num_triplets) torch.long
        triplet_edge_index[:, f] = [e1, e2] form the `f`-th triplet.
        edge_index[:, e1] = [i, j], edge_index[:, e2] = [i, k]
        Then, [i, j, k] is nodes of the `f`-th triplet.
    num_triplet_i: (num_nodes, ) torch.int
        Number of triplets containing each node
    num_triplet_ij: (num_edges, ) torch.int
        Number of triplets containing nodes in each edge

    Note
    ----
    m3gnet.graph._threebody_indices.pyx::compute_threebody
    """
    num_edges = edge_index.size(1)

    valid_edge_index_mask = distances <= threebody_cutoff
    valid_edge_index_mapping = torch.where(valid_edge_index_mask)[0]
    original_indices = torch.arange(num_edges)[valid_edge_index_mask]
    valid_edge_index = edge_index[:, valid_edge_index_mask]

    degrees: TensorType["num_nodes"] = torch.bincount(
        valid_edge_index[1], minlength=num_nodes
    )  # type: ignore # noqa: F821
    num_triplet_i = degrees * (degrees - 1)

    num_triplet = torch.sum(num_triplet_i)
    num_triplet_ij_ = torch.zeros(valid_edge_index.size(1), dtype=torch.int)
    triplet_edge_index = torch.empty((2, num_triplet), dtype=torch.long)
    offset = 0
    idx = 0
    for i in range(num_nodes):
        for j in range(degrees[i]):
            num_triplet_ij_[offset + j] = degrees[i] - 1
            for k in range(degrees[i]):
                if j == k:
                    continue
                triplet_edge_index[0, idx] = offset + j
                triplet_edge_index[1, idx] = offset + k
                idx += 1
        offset += degrees[i]
    triplet_edge_index = original_indices[triplet_edge_index]

    num_triplet_ij = torch.zeros(num_edges, dtype=torch.int)
    num_triplet_ij[valid_edge_index_mapping] = num_triplet_ij_

    return triplet_edge_index, num_triplet_i, num_triplet_ij

# Stab for Batch(_base_cls=MaterialGraph)
BatchMaterialGraph: TypeAlias = Any


from django.shortcuts import render
import logging
from django.http import HttpResponse
from django.template import loader

def exercise1(request):
 template = loader.get_template('exercise1.html')
 if request.method == 'GET':
   msg ="get함"
 else:
  msg="post함"
 context={'result': msg}
 logging.warning(msg+"나는로깅")
 return HttpResponse(template.render(context, request))

def exercise2(request):
 if request.method == 'POST':
  Writer = request.POST['name']
  opinion = request.POST['opinion']
  context = {'name': Writer, 'opinion': opinion}
 else:
  context = None
 return render(request, 'exercise2.html', context)


#Объем файлов
#Вам доступен архив workbook.zip, содержащий различные папки и файлы.
#Напишите программу, которая выводит суммарный объем файлов этого архива в сжатом и не сжатом видах в байтах, в следующем формате:

#Объем исходных файлов: <объем до сжатия> байт(а)
#Объем сжатых файлов: <объем после сжатия> байт(а)

from zipfile import ZipFile

with ZipFile('workbook.zip') as zip_file:
    info = zip_file.infolist()
    x=0
    y=0
    for i in info:
        x+=i.file_size
        y+=i.compress_size
    print(f'Объем исходных файлов: {x} байт(а)')
    print(f'Объем сжатых файлов: {y} байт(а)')