import numpy as np
import time
import torch
import torch.nn as nn
import torch.optim as optim

from .expt_template import Base
from .utilities import MaxPool1dFixed

device = "cuda" if torch.cuda.is_available() else "cpu"

class MLP_1_hidden(nn.Module):
    def __init__(self, input_size = 1024, hidden_size = 512):
        super().__init__()
        self.input_size = input_size
        self.hidden_size  = hidden_size
        self.fc1 = torch.nn.Linear(self.input_size, self.hidden_size)
        self.out = torch.nn.Linear(self.hidden_size, self.hidden_size)
        self.logit_scale = nn.Parameter(torch.ones([], device=device))

    def forward(self, input):
        output = self.out(self.fc1(input))
        return output

class MLPExpt(Base):
    def __init__(self,variant="MLP_1_hidden", n_shot=5, lr=0.003):

        super().__init__()

        if variant == "MLP_1_hidden":
            self.model = MLP_1_hidden().to(device)
        self.optimizer = torch.optim.SGD(self.model.parameters(), lr=lr, weight_decay=1e-5)
        self.criterion = nn.CrossEntropyLoss(reduction="sum")  # to make calculation of loss and accuracy comparable
        self.scheduler = optim.lr_scheduler.MultiStepLR(
            self.optimizer, milestones=[150, 250, 350], gamma=0.1
        )
        self.cossim = torch.nn.CosineSimilarity()
        self.softmax = torch.nn.Softmax(dim=1)
        self.max_pool = MaxPool1dFixed(n_shot)

    def calc_loss(self, prototypes, support_labels, queries, query_labels, reduction="max"):
        """
        prototypes: NWAY*NSHOT, 512
        queries: NWAY*NQueries, 512
        query_labels: NWAY*NQueries
        """
        logit_scale = self.model.logit_scale.exp()
        logits_per_image = logit_scale * queries @ prototypes.t() # NWAY*NQueries, NWAY*NSHOT
        probabilities = self.softmax(logits_per_image)

        #Assuming that relative sorting of both support labels are grouped by label, hence maxpooling makes sense
        assert all(torch.eq(support_labels,torch.sort(support_labels)[0]))

        probabilities_collapsed = self.max_pool(probabilities)

        return self.criterion(probabilities_collapsed, query_labels)

    def num_correct_preds(self, prototypes, support_labels, queries, query_labels, reduction="max"):
        probabilities = queries @ prototypes.t()
        probabilities_collapsed = self.softmax(self.max_pool(probabilities))
        return torch.sum(probabilities_collapsed.argmax(dim=1) == query_labels).item()

    def train_loop(self, train_loader, epoch, class_prototype_calculation="max"):

        """

        class_prototype_calculation: Defines method by which we will be calculating loss/accuracy
            "max": take argmax of all prototypes and predict for corresponding label
            "mean": takes mean of all the prototypes

        Doesn't use support labels because they have already been used in Task Sampler to generate the appended img text embedding
        """

        train_loss = 0.0
        total = 0
        correct = 0

        start_time = time.time()
        for i, (
            support_inputs, # N_WAY x N_SHOT , 1024 - tensor
            support_labels, # N_WAY x N_SHOT , 1 - tensor
            query_inputs, # N_WAY x N_QUERY , 1024 - tensor
            query_labels, # N_WAY x N_QUERY , 1 - tensor
            true_class_ids, # N_WAY  - list
        ) in enumerate(train_loader):

            support_inputs = support_inputs.to(device)
            support_labels = support_labels.to(device)
            query_inputs = query_inputs.to(device)
            query_labels = query_labels.to(device)

            support_labels_sorted, indices = torch.sort(support_labels)
            support_inputs_sorted = support_inputs[indices]

            self.model.train()
            loss= 0

            self.optimizer.zero_grad()

            class_prototype_predictions = self.model(support_inputs_sorted) # N_WAY x N_SHOT, 512

            query_image_embs = query_inputs[:,:512]
            loss = self.calc_loss(class_prototype_predictions, support_labels_sorted, query_image_embs, query_labels.cuda())
            loss.backward()
            self.optimizer.step()
            train_loss += loss.item()
            total += query_labels.size(0)

            corr = self.num_correct_preds(class_prototype_predictions, support_labels_sorted, query_image_embs, query_labels.cuda())
            correct += corr

            print(i, loss.item()/query_labels.size(0), corr/query_labels.size(0))

        self.scheduler.step()
        epoch_loss = train_loss/total
        epoch_accuracy = correct*100/total
        return (epoch_loss, epoch_accuracy)

    def evaluate(self, test_loader):

        for i, (
            support_inputs, # N_WAY x N_SHOT , 1024 - tensor
            support_labels, # N_WAY x N_SHOT , 1 - tensor
            query_inputs, # N_WAY x N_QUERY , 1024 - tensor
            query_labels, # N_WAY x N_QUERY , 1 - tensor
            true_class_ids, # N_WAY  - list
        ) in enumerate(test_loader):

            with torch.no_grad():

                # train_labels_sorted, indices = torch.sort(train_labels)
                # train_inputs_sorted = train_embeddings[indices]

                support_labels_sorted, indices = torch.sort(support_labels)
                support_inputs_sorted = support_inputs[indices]

                self.model.eval()
                loss= 0

                class_prototype_predictions = self.model(support_inputs_sorted)  # N_WAY x N_SHOT, 512

                test_image_embs = query_inputs[:,:512]
                loss = self.calc_loss(class_prototype_predictions, support_labels_sorted, test_image_embs, query_labels.cuda())
                test_loss = loss.item()
                total = query_labels.size(0)

                corr = self.num_correct_preds(class_prototype_predictions, support_labels_sorted, test_image_embs, query_labels.cuda())

                epoch_loss = test_loss/total
                epoch_accuracy = corr*100/total
                print(
                    f"Accuracy: {epoch_accuracy:7.3f}%"
                )
                return (epoch_loss, epoch_accuracy)


from typing import Tuple
import click
from pipelime.pipes.piper import Piper, PiperCommand

@click.command("smart_converter", help="Converts Subfolders to Underfolder")
@click.option("-i", "--input_folder", required=True, type=str, help="Input H5 Filename")
@click.option(
    "-o", "--output_folder", required=True, type=str, help="Output Underfolder"
)
@click.option(
    "-e",
    "--extensions_map",
    required=True,
    type=(str, str),
    multiple=True,
    help="Image extensions map pairs {item_name:extension} as multiple tuples",
)
@Piper.command(
    inputs=["input_folder"],
    outputs=["output_folder"],
)
def smart_converter(
    input_folder: str,
    output_folder: str,
    extensions_map: str,
):

    import rich

    from pipelime.converters.smartconverter import SmartConverter

    extensions_map = {a: b for a, b in extensions_map}
    converter = SmartConverter(
        folder=input_folder,
        extensions_map=extensions_map,
    )
    converter.convert(output_folder)
    rich.print("Underfolder Writer output to:", output_folder)


import sys
_module = sys.modules[__name__]
del sys
SRPN = _module
axis = _module
data_otb = _module
download = _module
test_otb = _module
train = _module
video2pic = _module

from _paritybench_helpers import _mock_config, patch_functional
from unittest.mock import mock_open, MagicMock
from torch.autograd import Function
from torch.nn import Module
import abc, collections, copy, enum, functools, inspect, itertools, logging, math, matplotlib, numbers, numpy, pandas, queue, random, re, scipy, sklearn, string, tensorflow, time, torch, torchaudio, torchtext, torchvision, types, typing, uuid, warnings
import numpy as np
from torch import Tensor
patch_functional()
open = mock_open()
yaml = logging = sys = argparse = MagicMock()
ArgumentParser = argparse.ArgumentParser
_global_config = args = argv = cfg = config = params = _mock_config()
argparse.ArgumentParser.return_value.parse_args.return_value = _global_config
yaml.load.return_value = _global_config
sys.argv = _global_config
__version__ = '1.0.0'
xrange = range
wraps = functools.wraps

import torch.nn as nn

import torch.utils.model_zoo as model_zoo

import torch

from torch.nn import Module

from torch.nn import functional as F

import math

from torchvision.transforms import functional as F2

import numpy as np

from torch.utils.data import Dataset

from torch.utils.data import DataLoader

from torchvision.transforms import functional as F

import random

from torch.autograd import Variable as V

from torch import nn

import torch.optim as optim

from torch.optim import lr_scheduler

model_urls = {'alexnet': 'https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth'}

class SiameseRPN(nn.Module):

    def __init__(self):
        super(SiameseRPN, self).__init__()
        self.features = nn.Sequential(nn.Conv2d(3, 64, kernel_size=11, stride=2), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=3, stride=2), nn.Conv2d(64, 192, kernel_size=5), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=3, stride=2), nn.Conv2d(192, 384, kernel_size=3), nn.ReLU(inplace=True), nn.Conv2d(384, 256, kernel_size=3), nn.ReLU(inplace=True), nn.Conv2d(256, 256, kernel_size=3))
        self.k = 5
        self.conv1 = nn.Conv2d(256, 2 * self.k * 256, kernel_size=3)
        self.relu1 = nn.ReLU(inplace=True)
        self.conv2 = nn.Conv2d(256, 4 * self.k * 256, kernel_size=3)
        self.relu2 = nn.ReLU(inplace=True)
        self.conv3 = nn.Conv2d(256, 256, kernel_size=3)
        self.relu3 = nn.ReLU(inplace=True)
        self.conv4 = nn.Conv2d(256, 256, kernel_size=3)
        self.relu4 = nn.ReLU(inplace=True)
        self.cconv = nn.Conv2d(256, 2 * self.k, kernel_size=4, bias=False)
        self.rconv = nn.Conv2d(256, 4 * self.k, kernel_size=4, bias=False)
        self.reset_params()

    def reset_params(self):
        pretrained_dict = model_zoo.load_url(model_urls['alexnet'])
        model_dict = self.state_dict()
        pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
        model_dict.update(pretrained_dict)
        self.load_state_dict(model_dict)

    def forward(self, template, detection):
        template = self.features(template)
        detection = self.features(detection)
        ckernal = self.conv1(template)
        ckernal = ckernal.view(2 * self.k, 256, 4, 4)
        self.cconv.weight = nn.Parameter(ckernal)
        cinput = self.conv3(detection)
        coutput = self.cconv(cinput)
        rkernal = self.conv2(template)
        rkernal = rkernal.view(4 * self.k, 256, 4, 4)
        self.rconv.weight = nn.Parameter(rkernal)
        rinput = self.conv4(detection)
        routput = self.rconv(rinput)
        return coutput, routput

class SmoothL1Loss(Module):

    def __init__(self, use_gpu):
        super(SmoothL1Loss, self).__init__()
        self.use_gpu = use_gpu
        return

    def forward(self, clabel, target, routput, rlabel):
        rloss = F.smooth_l1_loss(routput, rlabel, size_average=False, reduce=False)
        e = torch.eq(clabel.float(), target)
        e = e.squeeze()
        e0, e1, e2, e3, e4 = e[0].unsqueeze(0), e[1].unsqueeze(0), e[2].unsqueeze(0), e[3].unsqueeze(0), e[4].unsqueeze(0)
        eq = torch.cat([e0, e0, e0, e0, e1, e1, e1, e1, e2, e2, e2, e2, e3, e3, e3, e3, e4, e4, e4, e4], dim=0).float()
        rloss = rloss.squeeze()
        rloss = torch.mul(eq, rloss)
        rloss = torch.sum(rloss)
        rloss = torch.div(rloss, eq.nonzero().shape[0] + 0.0001)
        return rloss

class Myloss(Module):

    def __init__(self):
        super(Myloss, self).__init__()
        return

    def forward(self, coutput, clabel, target, routput, rlabel, lmbda):
        closs = F.cross_entropy(coutput, clabel)
        rloss = F.smooth_l1_loss(routput, rlabel, size_average=False, reduce=False)
        e = torch.eq(clabel.float(), target)
        e = e.squeeze()
        e0, e1, e2, e3, e4 = e[0].unsqueeze(0), e[1].unsqueeze(0), e[2].unsqueeze(0), e[3].unsqueeze(0), e[4].unsqueeze(0)
        eq = torch.cat([e0, e0, e0, e0, e1, e1, e1, e1, e2, e2, e2, e2, e3, e3, e3, e3, e4, e4, e4, e4], dim=0).float()
        rloss = rloss.squeeze()
        rloss = torch.mul(eq, rloss)
        rloss = torch.sum(rloss)
        rloss = torch.div(rloss, eq.nonzero().shape[0] + 0.0001)
        loss = torch.add(closs, lmbda, rloss)
        return loss


#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import rootpath
rootpath.append()

from backend.data_preparation.dumper.dumperbase import DumperBase

class MoisTempDumper(DumperBase):
    HIS_TEMP_MODE = 0
    HIS_MOIS_MODE = 1
    REC_TEMP_MODE = 2
    REC_MOIS_MODE = 3

    def insert_one(self, data_type, conn, p_lat, p_long, p_value, p_time=0, p_start=0, p_end=0):
        # insert one record into database
        # recording insert count number to self.inserted_count

        # filter for geolocation in America
        if p_lat > 49 or p_lat < 25 or p_long < 230 or p_long > 290:
            return

        cur = conn.cursor()
        try:
            if data_type == MoisTempDumper.REC_TEMP_MODE:
                cur.execute(
                    "INSERT INTO recent_temperature(lat,long,temperature,starttime,endtime) values (%s, %s, %s, %s, %s)",
                    (p_lat, p_long, p_value, p_start, p_end))
            if data_type == MoisTempDumper.REC_MOIS_MODE:
                cur.execute("INSERT INTO recent_moisture(lat,long,moisture,starttime, endtime) values (%s, %s, %s, %s, %s)",
                            (p_lat, p_long, p_value, p_start, p_end))
            if data_type == MoisTempDumper.HIS_TEMP_MODE:
                cur.execute("INSERT INTO historical_temperature(lat,long,temperature,datetime) values (%s, %s, %s, %s)",
                            (p_lat, p_long, p_value, p_time))

            if data_type == MoisTempDumper.HIS_MOIS_MODE:
                cur.execute("INSERT INTO historical_moisture(lat,long,moisture,datetime) values (%s, %s, %s, %s)",
                            (p_lat, p_long, p_value, p_time))
        except Exception as err:
            print("error", err)

        conn.commit()
        cur.close()
        self.inserted_count += 1
        print('number of inserted records: ', self.inserted_count)

    def insert_batch(*args, **kwargs):
        # insert a batch of records into database
        # recording insert count number to self.inserted_count
        pass


import corpus2
from npsemrel.carrot import resource
import npsemrel.ml.features.f23elems as fi

class FstPredFeature(fi.F23ElemsFeature):
	'''
	Ekstraktor cech typu:

	,,czy element jest predykatem pierwszego typu''

	dla elementow relacji semantycznej. W kostruktorze przekazywane sa
	zaladowane zasoby (npsemrel.carrot.resources) - dla tej cechy wymagane
	jest zaladowanie predykatow typu pierwszego
	'''

	def __init__(self, fst_type_pred_file, tagset_str = 'nkjp'):
		super(FstPredFeature, self).__init__(
				self.__get_fst_pred_for_token,
				"_fst_pred")
		self.tagset = corpus2.get_named_tagset(tagset_str)
		self.resources = resource.Resource(self.tagset)
		self.resources.load_fst_type_predicate_dictionary(fst_type_pred_file)

	def __get_fst_pred_for_token(self, ud, tok_pos):
		'''
		Zwraca:
			True - jezeli element jest predykatem typu pierwszego
			False - jezeli element nie jest predykatem typu pierwszego

		ud - decyzja uzytkownika
		tok_pos - pozycja tokenu wzgledem poczatku frazy
		'''
		lexeme = ud.syntax_relation.np_adjp_phrase().annotated_sentence().tokens()[
					ud.syntax_relation.np_adjp_phrase().segments()[tok_pos]
				].get_preferred_lexeme(self.tagset)

		return self.resources.exists_as_first_predicate(lexeme)


# coding: utf-8

import re

import pyrange.config

class Field(object):
    def __init__(self, function):
        self.function = function

    def __call__(self, value, field):
        return field if self.function(value) else None

class RangeField(object):
    def __init__(self, start, end=None, cond=pyrange.config.AND):
        self.start = start
        self.end = end
        self.cond = cond

    def __call__(self, value, field):
        start_groups = re.search(pyrange.config.OPERATOR_PATTERN,
                                 self.start).groups()

        start_operator_function = pyrange.config.OPERATOR_DICT.get(start_groups[
            pyrange.config.FUNCTION])

        start_num = float(start_groups[pyrange.config.VALUE])

        if not self.end:
            if start_operator_function(float(value), start_num):
                return field
            else:
                return

        end_groups = re.search(pyrange.config.OPERATOR_PATTERN,
                               self.end).groups()

        end_operator_function = pyrange.config.OPERATOR_DICT.get(end_groups[
            pyrange.config.FUNCTION])

        end_num = float(end_groups[pyrange.config.VALUE])
        if self.cond(start_operator_function(float(value), start_num),
                     end_operator_function(float(value), end_num)):
            return field

    def __repr__(self):
        return 'RangeField ({}, {}, {})'.format(self.start, self.end, self.cond)

class RangeOrField(RangeField):
    def __init__(self, start, end=None):
        super(RangeOrField, self).__init__(start, end, cond=pyrange.config.OR)

class RangeList(object):
    def __init__(self, range_list):
        self.range_list = range_list

    def __call__(self, value, field):
        for range_field in self.range_list:
            if range_field(value, field):
                return field


import pandas as pd
from car import Car
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import *
from utils import only_numbers
from xpath import Xpath

def main():
    xpath=Xpath()
    webd_path = 'chromedriver.exe'

    driver = webdriver.Chrome(webd_path)

    driver.get('https://cars.skoda-auto.com/456/pl-pl/carSearch')

    driver.implicitly_wait(30)

    accept_button = driver.find_element(By.ID, 'onetrust-accept-btn-handler')
    accept_button.click()
    del accept_button

    cars=[]
    number_of_car=0
    # repair the link
    for a in range(1,3):
        for n in range(6):
            car_element = driver.find_element(By.CSS_SELECTOR,(f"#car-search-item-{number_of_car}"))
            number_of_car+=1

            # getting all info
            car_element.click()

            car=Car()
            # gettin all of the car info
            car.link = driver.current_url
            car.model=driver.find_element(By.XPATH, xpath.carmodel).text
            car.engine=driver.find_element(By.XPATH,xpath.carengine).text
            car.price=only_numbers(driver.find_element(By.XPATH,xpath.carprice).text)
            car.installment=only_numbers(driver.find_element(By.XPATH,xpath.carinstallment).text)
            car.prod_year=driver.find_element(By.XPATH,xpath.carprod_year).text
            car.vin=driver.find_element(By.XPATH,xpath.carvin).text
            car.color=driver.find_element(By.XPATH,xpath.carcolor).text
            cars.append(car)

            driver.get('https://cars.skoda-auto.com/456/pl-pl/carSearch')
            driver.implicitly_wait(10)
            if a%6>0:
                for more_page_results in range(a%6):
                    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
                    driver.find_element(By.ID,"result-page-more-results-btn").click()
                    driver.implicitly_wait(10)
                    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

    # sleep (11)
    carlinks=[]
    carmodels=[]
    carengines=[]
    carprices=[]
    carinstallments=[]
    carprod_years=[]
    carvins=[]
    carcolors=[]
    inf=[carlinks,carmodels,carengines,carprices,carinstallments,carprod_years,carvins,carcolors]

    for car in cars:
        inf=car.car_info(inf)

    cardic={"link":inf[0],"model":inf[1],"engine":inf[2],"price":inf[3],"installment":inf[4],"prod_year":inf[5],"vin":inf[6],"color":inf[7]}
    cardf=pd.DataFrame(cardic)
    print(cardf)

    cardf.to_excel("car_excel.xlsx")

    # try:
    #     SCROLL_PAUSA_TIME = 3
    #     driver.get(subcategory_url)
    #     last_height = driver.execute_script("return document.body.scrollHeight")
    #     while True:
    #         driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    #         time.sleep(SCROLL_PAUSA_TIME)
    #         new_height = driver.execute_script("return document.body.scrollHeight")
    #         if new_height == last_height:
    #             time.sleep(3)
    #             break
    #         last_height = new_height

 # .get_attribute('href')

    # driver.close()

if __name__ == '__main__':
    main()


def fun(n):
    c = 0
    for i in range(1,n+1):
        if n%i == 0:
            c+=i
    return c
n = input()
a = n.split(',')
l = [int(i) for i in a]
nl = []
for i in l:
    a = fun(i)
    if a in l:
        nl.append(i)
nl = sorted(nl)
if len(nl) == 0:
    print(-1)
else:
    print(*nl)


import numpy as np
from sklearn import metrics
from ogb.nodeproppred import Evaluator as Evaluator_n
from ogb.linkproppred import Evaluator as Evaluator_l

import warnings
from sklearn.exceptions import UndefinedMetricWarning
warnings.filterwarnings(action='ignore', category=UndefinedMetricWarning)

METRICS = {
    'f1'            : ['f1mic', 'f1mac'],
    'accuracy'      : ['accuracy'],
    'accuracy_ogb'  : ['accuracy'],
    'hits20'        : ['hits20'], # 'hits50', 'hits100'],
    'hits50'        : ['hits50'], # 'hits20', 'hits100'],
    'hits100'       : ['hits100'] # 'hits50', 'hits20']}
}

class Metrics:
    full_graph_name = {
        'arxiv'      : 'ogbn-arxiv',
        'products'   : 'ogbn-products',
        'papers100M' : 'ogbn-papers100M',
        'collab'     : 'ogbl-collab',
        'ppa'        : 'ogbl-ppa'
    }
    def __init__(self, name_data: str, is_sigmoid: bool, metric: str, metric_win_size: int):
        self.window_size = metric_win_size
        self.name_data = name_data
        self.is_sigmoid = is_sigmoid
        self.name = metric
        if metric == 'f1':
            self.calc = self._calc_f1
            self.is_better = self._is_better_f1
            self.metric_term = ('f1mic', 'max')      # if terminate by multiple metrics, then [('f1mic', 'max'), ('f1mac', 'max')]
        elif metric == 'accuracy':
            self.calc = self._calc_accuracy
            self.is_better = self._is_better_accuracy
            self.metric_term = ('accuracy', 'max')
        elif metric == 'accuracy_ogb':
            self.evaluator = Evaluator_n(name=self.full_graph_name[name_data])
            self.calc = self._calc_accuracy_ogb
            self.is_better = self._is_better_accuracy
            self.metric_term = ('accuracy', 'max')
        elif metric.startswith('hits'):
            self.evaluator = Evaluator_l(name=self.full_graph_name[name_data])
            self.calc = self._calc_hits
            self.is_better = eval(f"self._is_better_{metric}")
            self.metric_term = (metric, 'max')
        else:
            raise NotImplementedError

    def _calc_f1(self, y_true, y_pred):
        """
        Compute F1-score (micro- and macro averaged for multiple classes).

        NOTE: for the case of each node having a single label (e.g., ogbn-arxiv),
            F1-micro score is equivalent to accuracy.
        """
        if not self.is_sigmoid:
            y_true = np.argmax(y_true, axis=1)
            y_pred = np.argmax(y_pred, axis=1)
        else:
            y_pred[y_pred > 0.5] = 1
            y_pred[y_pred <= 0.5] = 0
        return {
            'f1mic' : metrics.f1_score(y_true, y_pred, average="micro"),
            'f1mac' : metrics.f1_score(y_true, y_pred, average="macro")
        }

    def _calc_accuracy(self, y_true, y_pred):
        y_true = np.argmax(y_true, axis=1)
        y_pred = np.argmax(y_pred, axis=1)
        # if each node has only 1 ground truth label, accuracy is equivalent to f1-micro
        return {
            'accuracy' : metrics.f1_score(y_true, y_pred, average="micro")
        }

    def _calc_accuracy_ogb(self, y_true, y_pred):
        """
        This function is equivalent to _calc_accuracy. We just do this to conform to the leaderboard requirement
        """
        y_true = np.argmax(y_true, axis=1)[:, np.newaxis]
        y_pred = np.argmax(y_pred, axis=1)[:, np.newaxis]
        acc = self.evaluator.eval({'y_true': y_true, 'y_pred': y_pred})['acc']
        return {
            'accuracy' : acc
        }

    def _calc_hits(self, y_true, y_pred):
        pos_pred = y_pred[y_true==1]
        neg_pred = y_pred[y_true==0]
        ret = {}
        for K in [50]:
            hits_val = self.evaluator.eval(
                {'y_pred_pos': pos_pred, 'y_pred_neg': neg_pred}
            )[f'hits@{K}']
            ret[f'hits{K}'] = hits_val
        return ret

    def _is_better_accuracy(self, loss_all, loss_min_hist, accuracy_all, accuracy_max_hist):
        assert len(loss_all) == len(accuracy_all)
        window_acc = accuracy_all[-self.window_size : ]
        window_loss = loss_all[-self.window_size : ]
        acc_avg = sum(window_acc) / len(window_acc)
        loss_avg = sum(window_loss) / len(window_loss)
        if acc_avg > accuracy_max_hist:
            return True, loss_avg, acc_avg
        else:
            return False, loss_min_hist, accuracy_max_hist

    def _is_better_f1(self, loss_all, loss_min_hist, f1mic_all, f1mic_max_hist, f1mac_all, f1mac_max_hist):
        assert len(loss_all) == len(f1mic_all) == len(f1mac_all)
        window_mic = f1mic_all[-self.window_size : ]
        window_mac = f1mac_all[-self.window_size : ]
        window_loss = loss_all[-self.window_size : ]
        mic_avg = sum(window_mic) / len(window_mic)
        mac_avg = sum(window_mac) / len(window_mac)
        loss_avg = sum(window_loss) / len(window_loss)
        if mic_avg > f1mic_max_hist:
            return True, loss_avg, mic_avg, mac_avg
        else:
            return False, loss_min_hist, f1mic_max_hist, f1mac_max_hist

    def __is_better_hits(self, loss_all, loss_min_hist, hits_all, hits_max_hist):
        assert len(loss_all) == len(hits_all)
        window_hits = hits_all[-self.window_size : ]
        window_loss = loss_all[-self.window_size : ]
        hits_avg = sum(window_hits) / len(window_hits)
        loss_avg = sum(window_loss) / len(window_loss)
        if hits_avg > hits_max_hist:
            return True, loss_avg, hits_avg
        else:
            return False, loss_min_hist, hits_max_hist

    def _is_better_hits20(self, loss_all, loss_min_hist, hits20_all, hits20_max_hist):
        return self.__is_better_hits(loss_all, loss_min_hist, hits20_all, hits20_max_hist)

    def _is_better_hits50(self, loss_all, loss_min_hist, hits50_all, hits50_max_hist):
        return self.__is_better_hits(loss_all, loss_min_hist, hits50_all, hits50_max_hist)

    def _is_better_hits100(self, loss_all, loss_min_hist, hits100_all, hits100_max_hist):
        return self.__is_better_hits(loss_all, loss_min_hist, hits100_all, hits100_max_hist)


# coding: utf-8

import os
import sys
import re
import glob
import fnmatch
from subprocess import Popen, PIPE

thisdir = os.path.dirname(os.path.abspath(__file__))

dcache_mount = "/pnfs/desy.de/cms/tier2/store/user/{user}/hbt_resonant_run2/HHNtuples"

sample_kinds = {
    "background": {"user": "tokramer", "campaign_cre": re.compile(r"^RunII[^\/]+$")},
    "signal": {"user": "mrieger", "campaign_cre": re.compile(r"^RunII[^\/]+$")},
    "data": {"user": "mrieger", "campaign_cre": re.compile(r"^Run201[^\/]+$")},
}

def print_err(*msg):
    sys.stderr.write(" ".join(map(str, msg)) + "\n")
    sys.stderr.flush()

def get_sample_info(sample_name, sample_kind, tag, campaign_pattern=None):
    user = sample_kinds[sample_kind]["user"]
    sample_dir = os.path.join(dcache_mount.format(user=user), tag, sample_name)

    # get campaign names (the original ones, plus extensions)
    campaign_names = []
    for elem in os.listdir(sample_dir):
        if sample_kinds[sample_kind]["campaign_cre"].match(elem):
            campaign_names.append(elem)
    if not campaign_names:
        raise Exception("could not determine campaign_names for sample '{}'".format(sample_name))

    # apply an additional campaign name filtering
    if campaign_pattern:
        campaign_names = [c for c in campaign_names if fnmatch.fnmatch(c, campaign_pattern)]
        if not campaign_names:
            raise Exception("no campaign found after applying pattern '{}'".format(campaign_pattern))

    if sample_kind == "data":
        # simply sort
        campaign_names.sort()
    else:
        # sort such that the leading one is not a dataset extension (unless there are only extensions)
        campaign_names.sort(key=lambda c: bool(re.match(r"^(.+)_ext\d+-v.+$", c)))

    return sample_dir, campaign_names

def get_sample_files(sample_name, sample_kind, tag, campaign_pattern=None, dry=False):
    sample_dir, campaign_names = get_sample_info(sample_name, sample_kind, tag, campaign_pattern=campaign_pattern)

    # complain when more than one campaign is found
    if len(campaign_names) > 1:
        msg = "found more than one campaign for sample {}: {}".format(sample_name, campaign_names)
        if sample_kind == "data":
            raise Exception(msg)
        print_err(msg)

    # get timestamps
    timestamps = {}
    for campaign_name in campaign_names:
        campaign_dir = os.path.join(sample_dir, campaign_name)

        # get the correct timestamp
        _timestamps = [ts for ts in os.listdir(campaign_dir) if re.match(r"^\d+_\d+$", ts)]
        if not _timestamps:
            raise Exception("found no timestamp in campaign directory {}".format(campaign_dir))
        if len(_timestamps) > 1:
            print_err("found more than one timestamp in campaign directory {}, using latest one".format(campaign_dir))

        # store it
        timestamps[campaign_name] = sorted(_timestamps)[-1]

    # create a list of file names
    sample_files = []
    xrd_door = ""  # "root://dcache-cms-xrootd.desy.de:1094/"
    for campaign_name in campaign_names:
        ts_dir = os.path.join(sample_dir, campaign_name, timestamps[campaign_name])
        for file_name in glob.glob(os.path.join(ts_dir, "00??", "*.root")):
            sample_files.append((xrd_door or "") + file_name)

    return sample_files

def print_sample_files(*args, **kwargs):
    print("\n".join(get_sample_files(*args, **kwargs)))

def write_sample_files(sample_name, sample_kind, tag, campaign_pattern=None, output_dir=None, dry=False):
    """
    For legacy support.
    """
    # default output dir
    if output_dir is None:
        output_dir = os.path.normpath(os.path.join(thisdir, "..", "inputFiles", "UL17_" + sample_kind))

    # define the output file
    campaign_names = get_sample_info(sample_name, sample_kind, tag, campaign_pattern=campaign_pattern)[1]
    output_file = os.path.join(output_dir, "{}__{}.txt".format(sample_name, campaign_names[0]))

    if not dry:
        # get sample files
        sample_files = get_sample_files(sample_name, sample_kind, tag, campaign_pattern=campaign_pattern)

        # write them
        if not os.path.exists(output_dir):
            os.makedirs(output_dir)
        with open(output_file, "w") as f:
            f.write("\n".join(sample_files) + "\n")

        print_err("found {} files for sample {}, campaigns {}, written to {}".format(
            len(sample_files), sample_name, campaign_names, output_file))

    # finally, print the output file
    print(output_file)

if __name__ == "__main__":
    from argparse import ArgumentParser

    parser = ArgumentParser(description="prints input file lists for small ntuple production")
    parser.add_argument("--sample", help="the sample to list files for")
    parser.add_argument("--kind", choices=list(sample_kinds), help="the sample kind")
    parser.add_argument("--campaign", default=None, help="an additional pattern for filtering campaigns, mandatory for data")
    parser.add_argument("--tag", default="uhh_2017_v4", help="the big ntuple production tag")
    parser.add_argument("--write", action="store_true", help="write the files names to file")
    parser.add_argument("--dry", action="store_true", help="dry run")
    args = parser.parse_args()

    # get the function to call and prepare kwargs
    func = print_sample_files
    kwargs = {
        "sample_name": args.sample, "sample_kind": args.kind, "campaign_pattern": args.campaign,
        "tag": args.tag, "dry": args.dry,
    }
    if args.write:
        func = write_sample_files

    # call it
    func(**kwargs)


import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.gridspec as gridspec

def setup_axes(m=4, n=5, colorbar=True, title='', figsize=(15, 7)):

    fig = plt.figure(figsize=figsize)

    gs = gridspec.GridSpec(m, n, left=0.05, bottom=0.1, top=0.95, right=0.98,
            figure=fig, wspace=0.0, hspace=0.0)

    axes = []
    for bi in range(19):
        i = int(bi // n)
        j = int(bi - i * n)
        ax = fig.add_subplot(gs[i,j])

        if i != m-1:
            ax.set_xticklabels([])
        if j != 0:
            ax.set_yticklabels([])

        axes.append(ax)

    if colorbar:
        if m == 5:
            cax = fig.add_axes([0.85, 0.17, 0.19, 0.02])
        else:
            cax = fig.add_axes([0.84/float(m) * (m - 1) + 0.16, 0.14, 0.19, 0.02])
        axes.append(cax)
    else:
        if m == 5:
            cax = fig.add_axes([0.75, 0.17, 0.19, 0.02])
        else:
            cax = fig.add_axes([0.84/float(m) * (m - 1) + 0.14, 0.14, 0.19, 0.02])
        #cax.get_xaxis().set_visible(False)
        #cax.get_yaxis().set_visible(False)
        cax.set_axis_off()
        cax.set_title(title)

    return fig, axes


from typing import List, Optional, Tuple, Iterable
from dataclasses import dataclass

__pdoc__ = {
    "EntitieErrorInfo.__init__": False,
    "GetEntitiesError.__init__": False,
}

@dataclass(init=False)
class EntityErrorInfo:
    """
    Represents a failed entity retrieval.
    """

    __slots__ = ("name", "error_message")

    name: str
    error_message: str

    @property
    def is_error(self) -> bool:
        """
        Always returns True since this instance always indicates an error.
        """
        return True

    def __init__(self, name: str, error_message: str):
        self.name = name
        """The name of the entity."""

        self.error_message = error_message
        """Contains the error message."""

@dataclass(init=False)
class GetEntitiesError(Exception):
    entities: List[EntityErrorInfo]
    message: str

    def __init__(self, entities: List[EntityErrorInfo]):
        self.entities = entities
        """entities"""

        self.message = "failed to retrieve:\n" + (
            "\n".join("\t" + x.name + " error_message: " + x.error_message for x in self.entities)
        )
        """message"""

        super().__init__(self.message)

    @classmethod
    def _raise_if(cls, entities: Iterable[Tuple[str, Optional[str]]]) -> None:
        entities_list = [EntityErrorInfo(x, y) for x, y in entities if y]
        if entities_list:
            raise GetEntitiesError(entities_list)


import math as yojiykfsd
import random as befgetsl
import string as kskiskfs
def fredsaf(xrertfrsds):
    if xrertfrsds == 0:
        return int(yojiykfsd.sin(yojiykfsd.pi / int(round(yojiykfsd.e)-1)))
    else:
        return xrertfrsds * fredsaf(xrertfrsds - int(yojiykfsd.sin(yojiykfsd.pi / 2)))
cinone = ''
artyude, brewqis = input().split(chr(ord(str(False)[0]) - 2*(fredsaf(3) + int(yojiykfsd.sin(yojiykfsd.pi / 2))) - fredsaf(4)))
artyude = int(str(int(artyude)) + cinone.replace(kskiskfs.digits, (cinone.replace(kskiskfs.octdigits,''))))
brewqis = int(brewqis) + int(yojiykfsd.hypot(yojiykfsd.tan(yojiykfsd.pi), yojiykfsd.log(1)))+ round((4242/3*yojiykfsd.cos(0)-14)%100)
introfu = befgetsl.randint(1,100)
while introfu > 0 - round((4242/3*yojiykfsd.cos(0)-14)%10):
    introfu = introfu - befgetsl.randint(1,10)
    cinone = artyude + brewqis + int(yojiykfsd.hypot(yojiykfsd.tan(yojiykfsd.pi), yojiykfsd.log(1)))
if round(yojiykfsd.sqrt(int(befgetsl.randint(0,30)))) == int(yojiykfsd.gamma(43%3)):
    print('Congratulations! You are a lucker! ', cinone)
else:
    print(cinone)


from django import template
from django.db.models import Max

register = template.Library()

@register.simple_tag
def get_primary_item(order):
    """
    Find the most expensive item in an order for
    displaying in user account order history
    """
    highest_price = order.lineitems.all().aggregate(Max("lineitem_total")).get(
        "lineitem_total_max"
    )
    return order.lineitems.filter(lineitem_total=highest_price)

@register.simple_tag
def get_primary_item_image(order):
    """
    Find the most expensive item in an order for
    displaying in user account order history
    """
    highest_price = order.lineitems.aggregate(Max("lineitem_total")).get(
        "lineitem_total__max"
    )
    lineitem = order.lineitems.get(lineitem_total=highest_price)
    image = lineitem.product.image.url
    return image


import requests
import pandas as pd
from bs4 import BeautifulSoup
import openpyxl
from datetime import datetime

## 크롤링 할 페이지
req = requests.get('https://finance.naver.com/sise/sise_market_sum.nhn?&page=1')

## HTML 코드 저장
html = req.text

## BeautifulSoup으로 html소스를 python 객체로 변환
## 첫 인자 : html소스코드
## 두 번째 인자 : parser 종류 명시
## Python 내장 html.parser를 이용
soup = BeautifulSoup(html, 'html.parser')

## 데이터 가공을 위한 dataframe 활용
contents = soup.find_all(attrs={'class': 'type_2'})
dfContent = []
allDfContents = []

for content in contents:
    trs = content.find_all("tr")
    for tr in trs:
        ths = tr.find_all("th")
        for th in ths:
            dfContent.append(th.text)
        if dfContent:
            allDfContents.append(dfContent)
            dfContent = []

        tds = tr.find_all("td")
        for td in tds:
            dfContent.append(td.text)
        if dfContent:
            allDfContents.append(dfContent)
            dfContent = []

## pandas를 이용한 dataframe 생성
dfContents = pd.DataFrame(allDfContents)
## 공백 제거 : None 값 제거
dfContents = dfContents[dfContents[1].notnull()]
## 엑셀에 데이터 저장
excelPath = 'D:/study/study-python/stock/stockData_' + str(datetime.today().strftime("%Y%m%d")) + '.xlsx'
dfContents.to_excel(excel_writer=excelPath)

print('End')


import os
import glob

# pretty much just removes everything from the folders, including ignoreMe.txt
def clear():
    path = os.getcwd()

    dts_input_path = path + "/DatavyuToSuperCoder/Input/"
    os.chdir(dts_input_path)
    dts_input_files = glob.glob(os.path.join(dts_input_path, "*"))
    for file in dts_input_files:
        os.remove(file)

    dts_output_path = path + "/DatavyuToSuperCoder/Output/"
    os.chdir(dts_output_path)
    dts_output_files = glob.glob(os.path.join(dts_output_path, "*"))
    for file in dts_output_files:
        os.remove(file)

    ft_path = path + "/combining/Input/"
    os.chdir(ft_path)
    ft_files = glob.glob(os.path.join(ft_path, "*"))
    for file in ft_files:
        os.remove(file)

    output_path = path + "/OUTPUT/"
    os.chdir(output_path)
    output_files = glob.glob(os.path.join(output_path, "*"))
    for file in output_files:
        os.remove(file)

    input_path = path + "/INPUT/"
    os.chdir(input_path)
    # only removes ignoreMe.txt from the INPUT folder (the .csv inputs stay)
    input_ignore = glob.glob(os.path.join(input_path, "*txt"))
    if input_ignore:
        os.remove(input_ignore[0])

clear()


import socket  # 套接字
import struct  # 生成用于网络传输的而二进制数据
import select
import timeit

def createIcmp(sn=16):
    TYPE = 8
    CODE = 0
    ID = 1
    DATA = 'hi:)'
    header = struct.pack('BBHHH', TYPE, CODE, 0, ID, sn)    # 构造数据报
    data = bytearray()
    data.extend(map(ord, DATA))
    cs = get_checksum(header + data)
    new_header = struct.pack('BBHHH', TYPE, CODE, cs, ID, sn)
    return new_header + data

def get_checksum(data):
    count_to = len(data)
    counter = 0
    ch_sum = 0
    while counter < count_to:
        if 8 <= counter <= 7:
            ch_sum += (data[counter + 1] * 256 + data[counter])
        else:
            ch_sum += (data[counter] * 256 + data[counter + 1])
        counter += 2
    carry = int(ch_sum / 256 / 256)
    ch_sum = (ch_sum & 0xffff) + carry
    carry = int(ch_sum / 256 / 256)
    ch_sum = (ch_sum & 0xffff) + carry
    ch_sum ^= 0xffff

    ch_sum1 = int(ch_sum / 256)
    ch_sum2 = ch_sum & 0x00ff
    ch_sum = ch_sum2 * 256 + ch_sum1
    return ch_sum

def ping(name, address, quantity=4):
    shutdowncom = []
    opencom = []
    ping_result = []
    my_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname('icmp'), None)
    for i in range(quantity):
        sn = 0
        sequence_number = 1234 + i
        icmp_frame = createIcmp(sequence_number)
        my_socket.sendto(icmp_frame, (address, 1))
        start = timeit.default_timer()

        block = select.select([my_socket], [], [], 3)
        if block[0]:
            ip_frame = my_socket.recv(1024)
            stop = int(1000 * (timeit.default_timer() - start))
            received_icmp_frame = ip_frame[20:28]
            type, code, cs, id, sn = struct.unpack('bbHHh', received_icmp_frame)
        else:
            stop = 2999

        while timeit.default_timer() - start < 1:
            continue
        if (stop >= 1200):
            status = ('状态：已关机      时间 = %(stop)sms' % locals())
            ping_result.append(status)
            shutdowncom.append(name)

        else:
            status = ('状态：已开机      时间 = %(stop)sms' % locals())
            ping_result.append(status)
            opencom.append(name)

        return ping_result

    my_socket.close()
    return opencom

if __name__ == '__main__':
    name = " "
    url = str(input("请输入URL："))
    address = url.replace("https://", '').replace("http://", '').replace("/", '')
    try:
        result = str(ping(name, address, quantity=1))
        print(result.replace("[", '').replace("]", '').replace("'", ''))
    except:
        print('状态：检测失败      时间 = 未知\n' % locals())


suits = ["club", "spade", "heart", "diamond"]
values = ["nine", "ten", "jack", "queen", "king", "ace"]
card_weights = {"nine": 9, "ten": 10, "jack": 11, "queen": 12, "king": 13, "ace": 14}

deck = []
trump_list = []

class Card(object):
	def __init__(self, suit, value):
		self.suit = suit
		self.value = value

	def __str__(self):
		return self.value + " of " + self.suit

	def __repr__(self):
		return str(self)

	def __cmp__(self, other):
		return self.suit == other.suit and self.value == other.value

	def __eq__(self, other):
		return self.suit == other.suit and self.value == other.value

for i in suits:
	for j in values:
		deck.append(Card(i, j))

hand = [Card("club", "jack"), Card("spade", "jack"), Card("spade", "ace"), Card("spade", "king"), Card("spade", "queen")]

def getOppositeTrumpSuit(trump_suit):
	if trump_suit == "club":
		return "spade"
	if trump_suit == "spade":
		return "club"
	if trump_suit == "heart":
		return "diamond"
	if trump_suit == "diamond":
		return "heart"

def getTrumpCardsList(trump_suit):
	# return list of trump in game scoring order
	if not trump_list:
		for v in values[0:2]:
			trump_list.append(Card(trump_suit, v))
		for v in values[3:5]:
			trump_list.append(Card(trump_suit, v))
		trump_list.append(Card(getOppositeTrumpSuit(trump_suit), "jack"))
		trump_list.append(Card(trump_suit, "jack"))

	return trump_list

def generateLegalMoves(hand, trump_suit, lead_suit):
	legal = []
	if lead_suit == None:
		return hand

	if trump_suit == lead_suit:
		trump_list = getTrumpCardsList(trump_suit)
		for card in hand:
			if card in trump_list:
				legal.append(card)

	else:
		for card in hand:
			if card.value == "jack" and card.suit == getOppositeTrumpSuit(trump_suit):
				continue
			if card.suit == lead_suit:
				legal.append(card)

	if len(legal) == 0:
		return hand

	return legal

def compareCards(card1, card2):
	# return the "higher" card, used for non trump
	if card1.suit != card2.suit:
		return None
	else:
		if card_weights[card1.value] > card_weights[card2.value]:
			return card1
		else:
			return card2

def scoreTrick(line, trump_suit):
	# score a trick and determine the winner.
	# line is a list starting with the first player and advancing through each play sequentially
	played_trump = []
	trump_list = getTrumpCardsList(trump_suit)

	for c in line:
		if c in trump_list:
			played_trump.append(c)

	if len(played_trump) == 1:
		return played_trump[0]

	elif len(played_trump) > 1:
		highest_trump = played_trump[0]
		for p in played_trump[1:]:
			if trump_list.index(p) > trump_list.index(highest_trump):
				highest_trump = p
		return highest_trump
	else:
		max_played = line[0]
		for c in line[1:]:
			if c.suit != max_played.suit:
				continue
			else:
				if card_weights[c.value] > card_weights[max_played.value]:
					max_played = c
		return max_played

played_hand = [Card("heart", "jack"), Card("heart", "king"), Card("diamond", "jack"), Card("diamond", "ace")]

print(scoreTrick(played_hand, "diamond"))


import os

if __name__ == "__main__":
    filename = '测试1.txt'
    path = 'd:\data'
    if not os.path.exists(path):
        os.makedirs(path)
    text = 'cd {} && type nul> {}'.format(path,filename)
    os.system(str(text))


from django.db import models
from backend.coremodels.article import Article
from backend.coremodels.order import Order
from backend.operations.enumerator import OrderedUnitOperator

class OrderedArticle(models.Model):
    '''Order.'''
    id = models.AutoField(primary_key=True)
    quantity = models.PositiveIntegerField(default=None)
    article = models.ForeignKey(Article, on_delete=models.CASCADE)
    order = models.ForeignKey(Order, on_delete=models.CASCADE)
    unit = models.CharField(max_length=10,
                            choices=OrderedUnitOperator.choices, default=OrderedUnitOperator.INPUT)

    def __str__(self):
        return (str(self.id)
                + ": " + str(self.article)
                + " " + str(self.order)
                + " " + str(self.id))


# System libraries
import copy

# PyPi libraries
import numpy as np

# Local libraries
from libraries import common
from libraries import day4

if __name__ == '__main__':
    draws_fn = 'day4/draws'
    draws_file_handler = common.FileHandler(draws_fn)
    draws = draws_file_handler.read_csv()[0]

    boards_fn = 'day4/boards'
    boards = day4.Boards(np.loadtxt(boards_fn, dtype=int))
    board_count = int(boards.get_boards_count(5))
    for i in range(1, board_count + 1):
        board = day4.Board(boards.get_board_data(i))
        boards.add_board(board)

    have_winner = False
    for draw in draws:
        if not have_winner:
            for board in boards.boards:
                board.mark_match(int(draw))
                check_won = board.check_win()
                if check_won:
                    have_winner = True
                    unmarked_sum = board.get_unmarked_sum()
                    last_draw = int(draw)
                    break

    print(unmarked_sum*last_draw)


import numpy as np
from collections import defaultdict

class RecommenderEvaluator(object):
    def __init__(self, rating_data_test, predicted_ratings, dataset):
        self._prepare_results_data(dataset, rating_data_test, predicted_ratings)

    def _prepare_results_data(self, dataset, rating_data_test, predicted_ratings):
        if dataset == "ml-100k" or dataset == "ml-1m":
            self.results = rating_data_test[["userid", "itemid", "rating", "timestamp"]].copy()
        else:
            self.results = rating_data_test[["userId", "movieId", "rating", "timestamp"]].copy()
        self.results["predicted_rating_round"] = np.round(predicted_ratings[0])
        self.results["predicted_rating_decimal"] = predicted_ratings[0]

    def get_results_data(self):
        return self.results

    def rmse(self):
        return np.sqrt(np.mean(np.power(self.results["rating"] - self.results["predicted_rating_decimal"], 2)))

    def mae(self):
        return np.mean(np.abs(self.results["rating"] - self.results["predicted_rating_decimal"]))

    def precision_recall_at_k(self, k=20):
        user_est_true = defaultdict(list)
        for index, row in self.results.iterrows():
            user_est_true[row['userid']].append((row['predicted_rating_decimal'], row['rating']))

        mean_test = {}
        for uid, user_ratings in user_est_true.items():
            count = 0
            sum_ratings = 0
            for pred_real in user_ratings:
                count = count + 1
                sum_ratings = sum_ratings + pred_real[1]
            mean_test[uid] = sum_ratings / count

        precisions = dict()
        recalls = dict()
        for uid, user_ratings in user_est_true.items():
            if len(user_ratings) >= k:
                user_ratings.sort(key=lambda x: x[0], reverse=True)
                n_rel = sum((true_r >= mean_test[uid]) for (_, true_r) in user_ratings)
                n_rec_k = sum((est >= mean_test[uid]) for (est, _) in user_ratings[:k])
                n_rel_and_rec_k = sum(((true_r >= mean_test[uid]) and (est >= mean_test[uid]))
                                      for (est, true_r) in user_ratings[:k])
                precisions[uid] = n_rel_and_rec_k / n_rec_k if n_rec_k != 0 else 1
                recalls[uid] = n_rel_and_rec_k / n_rel if n_rel != 0 else 1

        return precisions, recalls

    def f1(self, precision, recall):
        return 2 * precision * recall / (precision + recall)


import sys

line = [l[:-1] for l in sys.stdin.readlines()][0]

for i in range(4, len(line)):
    s = line[i-4:i]
    if len(set(s)) == len(s):
        print(i)
        break

for i in range(14, len(line)):
    s = line[i-14:i]
    if len(set(s)) == len(s):
        print(i)
        break


class Stack:
    def __init__(self):
        self.data = []

    def empty(self):
        if self.data == []:
            return True
        return False

    def top(self):
        flag = self.empty()
        if flag == True:
            return None
        else:
            return self.data[-1]

    def pop(self):
        flag = self.empty()
        if flag == True:
            return None
        else:
            return self.data.pop(len(self.data)-1)

    def push(self, data):
        self.data.append(data)

    def __repr__(self):
        return f'{self.data}'

    # box = []
    # def __init__(self, data2):
    #     self.data2 = data2

    # @classmethod
    # def empty(cls):
    #     if cls.box:
    #         return True
    #     else:
    #         return False

    # @classmethod
    # def top(cls):
    #     if cls.box:
    #         return cls.box[len(Stack.box)-1]
    #     else:
    #         return None

    # @classmethod
    # def pop(cls):
    #     if cls.box:
    #         poppop = cls.box[len(Stack.box)-1]
    #         cls.box.pop()
    #         return poppop
    #     else:
    #         return None

    # def push(self):
    #     Stack.box.append(self.data2)
    #     print(Stack.box)
    #     print(len(Stack.box))

    # def __repr__(self):
    #     return f'{self.data2}'

if __name__ == '__main__':
    stack = Stack()

    print(stack.empty())

    stack.push(1)
    stack.push(2)
    stack.push(3)
    print(stack)
    print(stack.top())

    stack.push(4)
    print(stack.pop())
    print(stack.top())

    # print(Stack.empty())
    # a = Stack(1).push()
    # b = Stack(2).push()
    # c = Stack(3).push()
    # print(Stack.pop())
    # print(Stack.top())


from typing import BinaryIO, List, Optional, Tuple, Union
from pathlib import Path
from PIL import Image
import cv2
IMAGE_FILE_EXTENSIONS = {".jpg", ".png"}

def parse_path(path: str) -> Tuple[str, List[int]]:
    """Parse data path which is a path to a .jpg/.png file

      Args:
          path (str): the data path to parse

      Returns:
          file_path (str): the file path
    """

    if Path(path).suffix in IMAGE_FILE_EXTENSIONS:
        return path
    else:
        raise Exception("Unknown image suffix")

def compute_ratio_and_resize(img,width,height,model_height):
    '''
    Calculate ratio and resize correctly for both horizontal text
    and vertical case
    '''
    ratio = width/height
    # if ratio<1.0:
    #     ratio = calculate_ratio(width,height)
    #     img = cv2.resize(img,(model_height,int(model_height*ratio)), interpolation=Image.ANTIALIAS)
    # else:
    img = cv2.resize(img,(int(model_height*ratio),model_height),interpolation=Image.ANTIALIAS)
    return img,ratio


def my_gen(): # Generator Function
    try:
        print('creating context and yielding object')
        lst = [1, 2, 3, 4, 5]
        yield lst
    finally:
        print('exiting context and cleaning up')

gen = my_gen()  # create generator

lst = next(gen)  # enter context and get "as" object
print(lst)
# next(gen)  # exit context
#
#
# exiting context and cleaning up
#
# ---------------------------------------------------------------------------
# StopIteration                             Traceback (most recent call last)
# <ipython-input-5-ad32aeec95bc> in <module>()
# ----> 1 next(gen)  # exit context
#
# StopIteration:

# catch the StopIteration exception

gen = my_gen()
lst = next(gen)
print(lst)
try:
    next(gen)
except StopIteration:
    pass

class GenCtxManager:
    def __init__(self, gen_func, *args, **kwargs):
        self._gen = gen_func(*args, **kwargs)

    def __enter__(self):
        return next(self._gen)

    def __exit__(self, exc_type, exc_value, exc_tb):
        try:
            next(self._gen)
        except StopIteration:
            pass
        return False

def open_file(fname, mode):
    try:
        print('opening file...')
        f = open(fname, mode)
        yield f
    finally:
        print('closing file...')
        f.close()

with GenCtxManager(open_file, 'test.txt', 'w') as f:
    print('writing to file...')
    f.write('testing...')

with open('test.txt') as f:
    print(next(f))

Programming Languages/Python/Basics/Part -2 - Iteration & Generators/05 - Context Managers/09_ctx_generator.py


import pytest

from ephios.core.forms.users import MANAGEMENT_PERMISSIONS
from ephios.extra.permissions import get_groups_with_perms

def test_querying_get_groups_with_perms(groups, event):
    managers, planners, volunteers = groups

    groups_with_any_perm = get_groups_with_perms(
        event,
        only_with_perms_in=["view_event", "change_event"],
        must_have_all_perms=False,
    )
    assert set(groups) == set(groups_with_any_perm)

    groups_with_all_perms = get_groups_with_perms(
        event, only_with_perms_in=["view_event", "change_event"], must_have_all_perms=True
    )
    assert set(groups_with_all_perms) == {managers, planners}

    management_groups_by_list = get_groups_with_perms(
        only_with_perms_in=MANAGEMENT_PERMISSIONS,
        must_have_all_perms=True,
    )
    assert set(management_groups_by_list) == {managers}

    management_groups_by_list = get_groups_with_perms(
        only_with_perms_in=["core.view_event", "core.change_event"],
        must_have_all_perms=False,
    )
    assert set(management_groups_by_list) == {managers}

    with pytest.raises(ValueError):
        # view_event is not a specific enough to identify a permission
        get_groups_with_perms(
            only_with_perms_in=["view_event"],
        )


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

import json
import datetime

import pytz

__version__ = '0.0.1'
__all__ = [
    'dumps', 'loads',
]

__author__ = 'KAWASAKI Yasukazu <kawasaki@dev.kawa1128.jp>'

class _NNE4FineJsonEncoder(json.JSONEncoder):
  def default(self, obj):
    if isinstance(obj, datetime.datetime):
      if obj.tzinfo is None:
        obj = obj.replace(tzinfo=pytz.utc)
      else:
        obj = obj.astimezone(pytz.utc)
      obj.replace(microsecond=0)
      return {'__datetime.datetime__': obj.isoformat()}
    return json.JSONEncoder.default(self, obj)

def _NNE4FineJsonDecoder_hook(dct):
  if '__datetime.datetime__' in dct:
    tm_str = dct['__datetime.datetime__']
    if tm_str.endswith('+00:00'):
      # 2015-01-01T00:00:00+00:00
      tm = datetime.datetime.strptime(tm_str, '%Y-%m-%dT%H:%M:%S+00:00')
      tm = tm.replace(tzinfo=pytz.utc)
      return tm
  return dct

def dumps(obj):
  return json.dumps(obj, cls=_NNE4FineJsonEncoder)

def loads(s):
  if not isinstance(s, str):
    raise TypeError('ConfigFile Object is not str, not {!r}'.format(s.__class__.__name__))
  return json.loads(s, object_hook=_NNE4FineJsonDecoder_hook)


from socket import *

serverName = '40.124.98.131'
serverPort = 12000
message = "Paquete UDP"
clientSocket = socket(AF_INET, SOCK_DGRAM)
clientSocket.sendto(bytes(message, 'UTF-8'), (serverName, serverPort))
modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
print(modifiedMessage)
clientSocket.close()


import numpy as np

from deeplearning.layers import *
from deeplearning.layer_utils import *

class TwoLayerNet(object):
    """
    A two-layer fully-connected neural network with ReLU nonlinearity and
    softmax loss that uses a modular layer design. We assume an input dimension
    of D, a hidden dimension of H, and perform classification over C classes.

    The architecure should be affine - relu - affine - softmax.

    Note that this class does not implement gradient descent; instead, it
    will interact with a separate Solver object that is responsible for running
    optimization.

    The learnable parameters of the model are stored in the dictionary
    self.params that maps parameter names to numpy arrays.
    """

    def __init__(self, input_dim=3 * 32 * 32, hidden_dim=100, num_classes=10,
                 weight_scale=1e-3, reg=0.0):
        """
        Initialize a new network.

        Inputs:
        - input_dim: An integer giving the size of the input
        - hidden_dim: An integer giving the size of the hidden layer
        - num_classes: An integer giving the number of classes to classify
        - dropout: Scalar between 0 and 1 giving dropout strength.
        - weight_scale: Scalar giving the standard deviation for random
          initialization of the weights.
        - reg: Scalar giving L2 regularization strength.
        """
        self.params = {}
        self.reg = reg
        self.params['W1'] = np.random.normal(0, weight_scale, (input_dim, hidden_dim))
        self.params['b1'] = np.zeros(hidden_dim)
        self.params['W2'] = np.random.normal(0, weight_scale, (hidden_dim, num_classes))
        self.params['b2'] = np.zeros(num_classes)

    def loss(self, X, y=None):
        """
        Compute loss and gradient for a minibatch of data.

        Inputs:
        - X: Array of input data of shape (N, d_1, ..., d_k)
        - y: Array of labels, of shape (N,). y[i] gives the label for X[i].

        Returns:
        If y is None, then run a test-time forward pass of the model and return:
        - scores: Array of shape (N, C) giving classification scores, where
          scores[i, c] is the classification score for X[i] and class c.

        If y is not None, then run a training-time forward and backward pass and
        return a tuple of:
        - loss: Scalar value giving the loss
        - grads: Dictionary with the same keys as self.params, mapping parameter
          names to gradients of the loss with respect to those parameters.
        """
        scores = None
        out1, cache1 = affine_relu_forward(X, self.params['W1'], self.params['b1'])
        scores, cache2 = affine_forward(out1, self.params['W2'], self.params['b2'])

        # If y is None then we are in test mode so just return scores
        if y is None:
            return scores

        loss, grads = 0, {}
        loss, grads_sm = softmax_loss(scores, y)
        l2_loss = 0.5 * self.reg * (np.sum(self.params['W1']*self.params['W1']) + np.sum(self.params['W2']*self.params['W2']))
        loss = loss + l2_loss

        #backpropagate to get gradients
        grads_out1, grads['W2'], grads['b2'] = affine_backward(grads_sm, cache2)
        #add regularization
        grads['W2'] += self.reg * self.params['W2']
        grads_X, grads['W1'], grads['b1'] = affine_relu_backward(grads_out1, cache1)
        grads['W1'] += self.reg * self.params['W1']

        return loss, grads

class FullyConnectedNet(object):
  """
  A fully-connected neural network with an arbitrary number of hidden layers,
  nonlinearities, and a softmax loss function. The nonlinearities can be
  chosen from sigmoid, tanh, ReLU and LeakyReLU. This will also implement
  dropout and batch normalization as options. For a network with L layers,
  the architecture will be
  {affine - [batch norm] - nonlinearity - [dropout]} x (L - 1) - affine - softmax
  where batch normalization and dropout are optional, and the {...} block is
  repeated L - 1 times.
  Similar to the TwoLayerNet above, learnable parameters are stored in the
  self.params dictionary and will be learned using the Solver class.
  """

  def __init__(self, hidden_dims, input_dim=3*32*32, num_classes=10, nonlinearity={'type':'ReLU'},
               dropout=0, use_batchnorm=False, reg=0.0,
               weight_scale=1e-2, dtype=np.float32, seed=None):
    """
    Initialize a new FullyConnectedNet.
    Inputs:
    - hidden_dims: A list of integers giving the size of each hidden layer.
    - input_dim: An integer giving the size of the input.
    - num_classes: An integer giving the number of classes to classify.
    - nonlinearity: A dictionary containing the type of nonlinearity to use
      and its hyperparameters. It must contain the key 'type' and the
      corresponding value is the name of the nonlinearity.
    - dropout: Scalar between 0 and 1 giving dropout strength. If dropout=0 then
      the network should not use dropout at all.
    - use_batchnorm: Whether or not the network should use batch normalization.
    - reg: Scalar giving L2 regularization strength.
    - weight_scale: Scalar giving the standard deviation for random
      initialization of the weights.
    - dtype: A numpy datatype object; all computations will be performed using
      this datatype. float32 is faster but less accurate, so you should use
      float64 for numeric gradient checking.
    - seed: If not None, then pass this random seed to the dropout layers. This
      will make the dropout layers deteriminstic so we can gradient check the
      model.
    """
    self.nonlinearity = nonlinearity['type']
    if self.nonlinearity == 'LeakyReLU':
      self.leak = nonlinearity['leak']
    else:
      self.leak = None
    self.use_batchnorm = use_batchnorm
    self.use_dropout = dropout > 0
    self.reg = reg
    self.num_layers = 1 + len(hidden_dims)
    self.dtype = dtype
    self.params = {}

    # L-1 layers weight init.  First layer has different shapes due to inputs
    # since range has 0 indexing, add one to get 1 indexing
    for layer in range(self.num_layers - 1):
        # first layer uses input_dim and rest of the layers uses last hidden dim
        dim_input = input_dim if layer == 0 else hidden_dims[layer-1]

        self.params['W'+str(layer+1)] = np.random.randn(dim_input, hidden_dims[layer]) * weight_scale
        self.params['b'+str(layer+1)] = np.zeros(hidden_dims[layer])

        if self.use_batchnorm:
            self.params['gamma'+str(layer+1)] = np.ones(hidden_dims[layer])
            self.params['beta'+str(layer+1)] = np.zeros(hidden_dims[layer])

    self.params['W'+str(self.num_layers)] = np.random.randn(hidden_dims[self.num_layers-2], num_classes) * weight_scale
    self.params['b'+str(self.num_layers)] = np.zeros(num_classes)

    # When using dropout we need to pass a dropout_param dictionary to each
    # dropout layer so that the layer knows the dropout probability and the mode
    # (train / test). You can pass the same dropout_param to each dropout layer.
    self.dropout_param = {}
    if self.use_dropout:
      self.dropout_param = {'mode': 'train', 'p': dropout}
      if seed is not None:
        self.dropout_param['seed'] = seed

    # With batch normalization we need to keep track of running means and
    # variances, so we need to pass a special bn_param object to each batch
    # normalization layer. You should pass self.bn_params[0] to the forward pass
    # of the first batch normalization layer, self.bn_params[1] to the forward
    # pass of the second batch normalization layer, etc.
    self.bn_params = []
    if self.use_batchnorm:
      # change python2 xrange to python3 range.
      self.bn_params = [{'mode': 'train'} for i in range(self.num_layers - 1)]
    # Cast all parameters to the correct datatype
    for k, v in self.params.items():
      self.params[k] = v.astype(dtype)

  def loss(self, X, y=None):
    """
    Compute loss and gradient for the fully-connected net.
    Input / output: Same as TwoLayerNet above.
    """
    X = X.astype(self.dtype)
    mode = 'test' if y is None else 'train'

    # Set train/test mode for batchnorm params and dropout param since they
    # behave differently during training and testing.
    if self.dropout_param is not None:
      self.dropout_param['mode'] = mode
    if self.use_batchnorm:
      for bn_param in self.bn_params:
        bn_param[mode] = mode

    scores = None
    caches = {}
    outs = {}
    dropout_caches = {}

    for layer in range(1, self.num_layers):
        input_matrix = X if layer == 1 else outs['Out_l'+str(layer-1)]
        #Depending on whether or not we use batchnorm and dropout, we pass appropriate parameters to forward function
        if self.use_dropout and self.use_batchnorm:
          out, cache = layer_forward(input_matrix, self.params['W'+str(layer)], self.params['b'+str(layer)], self.nonlinearity,
                                    self.leak, self.use_batchnorm, self.params['gamma'+str(layer)],
                                    self.params['beta'+str(layer)], self.bn_params[layer-1], self.use_dropout, self.dropout_param)
        elif self.use_batchnorm:
          out, cache = layer_forward(input_matrix, self.params['W'+str(layer)], self.params['b'+str(layer)], self.nonlinearity,
                                    self.leak, self.use_batchnorm, self.params['gamma'+str(layer)],
                                    self.params['beta'+str(layer)], self.bn_params[layer-1], self.use_dropout)
        elif self.use_dropout:
          out, cache = layer_forward(input_matrix, self.params['W'+str(layer)], self.params['b'+str(layer)], self.nonlinearity, self.leak,
                                    self.use_batchnorm, use_dropout=self.use_dropout, dropout_param=self.dropout_param)
        else:
          out, cache = layer_forward(input_matrix, self.params['W'+str(layer)], self.params['b'+str(layer)], self.nonlinearity, self.leak,
                                    use_batchnorm=self.use_batchnorm, use_dropout=self.use_dropout)
        outs.update({'Out_l'+str(layer): out})
        caches.update({'C'+str(layer): cache})

    # last layer
    scores, cache_out = affine_forward(outs['Out_l'+str(self.num_layers-1)],
                                       self.params['W'+str(self.num_layers)],
                                       self.params['b'+str(self.num_layers)])

    outs.update({'Out_l'+str(self.num_layers): scores})
    caches.update({'C'+str(self.num_layers): cache_out})

    # If test mode return early
    if mode == 'test':
      return scores

    loss, grads = 0.0, {}
    # FC scores -> softmax. Calculate loss and gradients
    loss, sm_grads = softmax_loss(scores, y)

    # add L2 reg to loss
    w_squared = 0
    for layer in range(self.num_layers):
        w_squared += np.sum(self.params['W'+str(layer+1)] * \
                            self.params['W'+str(layer+1)])
    reg_loss = 0.5 * self.reg * w_squared
    loss += reg_loss

    # backprop
    dxs = {}
    for layer in range(self.num_layers, 0, -1):
        if layer == self.num_layers:
            # last layer, backprop the softmax loss gradient
            dx, dw, db = affine_backward(sm_grads, caches['C'+str(layer)])
        else:
            if self.use_batchnorm:
                dx, dw, db, dgamma, dbeta = layer_backward(dx, caches['C'+str(layer)], self.use_batchnorm, self.use_dropout)
                grads.update({'gamma'+str(layer): dgamma})
                grads.update({'beta'+str(layer): dbeta})
            else:
                dx, dw, db = layer_backward(dx, caches['C'+str(layer)], self.use_batchnorm, self.use_dropout)

        # store the gradients and dxs
        grads['W'+str(layer)] = dw
        grads['b'+str(layer)] = db
        dxs['dx_l'+str(layer)] = dx

        # add regularization gradient contribution
        grads['W'+str(layer)] += self.reg * self.params['W'+str(layer)]

    return loss, grads


import os
import sys
import numpy as np
import tensorflow as tf
from tqdm import tqdm
from utils import get_batch_data

from config import cfg
from CapsNet import CapsNet

tf.logging.set_verbosity(tf.logging.INFO)

def save_to():
    if not os.path.exists(cfg.results):
        os.mkdir(cfg.results)
    if cfg.is_training:
        loss = cfg.results + '/loss.csv'
        train_acc = cfg.results + '/train_acc.csv'
        val_acc = cfg.results + '/val_acc.csv'

        if os.path.exists(val_acc):
            os.remove(val_acc)
        if os.path.exists(loss):
            os.remove(loss)
        if os.path.exists(train_acc):
            os.remove(train_acc)

        fd_train_acc = open(train_acc, 'w')
        fd_train_acc.write('step,train_acc\n')
        fd_loss = open(loss, 'w')
        fd_loss.write('step,loss\n')
        fd_val_acc = open(val_acc, 'w')
        fd_val_acc.write('step,val_acc\n')
        return fd_train_acc, fd_loss, fd_val_acc
    else:
        test_acc = cfg.results + '/test_acc.csv'
        if os.path.exists(test_acc):
            os.remove(test_acc)
        fd_test_acc = open(test_acc, 'w')
        fd_test_acc.write('test_acc\n')
        return fd_test_acc

def train(model, supervisor):
    fd_train_acc, fd_loss, fd_val_acc = save_to()
    config = tf.ConfigProto()  # 用于创建session的时候对session进行参数配置
    config.gpu_options.allow_growth = True  # 刚开始配置少量GPU内存，然后按需慢慢增加（不会释放内存，会导致碎片）
    # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.95)
    with supervisor.managed_session(config=config) as sess:
        print("\nNote: all of results will be saved to directory: " + cfg.results)
        for epoch in range(cfg.epoch):
            print('Training for epoch ' + str(epoch) + '/' + str(cfg.epoch) + ':')
            if supervisor.should_stop():
                print('supervisor stoped!')
                break
            for step in tqdm(range(cfg.num_tr_batch), total=cfg.num_tr_batch, ncols=70, leave=False, unit='b'):
                global global_step
                global_step = epoch * cfg.num_tr_batch + step

                if global_step % cfg.train_sum_freq == 0:  # 每1个mini-batch进行一次total_loss、accuracy、train_summary的记录
                    _, loss, train_acc, summary_str = sess.run(
                        [model.train_op, model.margin_loss, model.accuracy, model.train_summary])
                    assert not np.isnan(loss), 'Something wrong! loss is nan...'
                    supervisor.summary_writer.add_summary(summary_str, global_step)

                    fd_loss.write(str(global_step) + ',' + str(loss) + "\n")
                    fd_loss.flush()  # 刷新缓冲区
                    fd_train_acc.write(str(global_step) + ',' + str(train_acc / cfg.batch_size) + "\n")
                    fd_train_acc.flush()
                else:
                    sess.run(model.train_op)  # 每个mini-batch进行一次模型的优化

                if cfg.val_sum_freq != 0 and (global_step) % cfg.val_sum_freq == 0:  # 每2个mini-batch进行一次验证
                    val_acc = 0
                    for i in range(cfg.num_val_batch):
                        img, label = sess.run([model.val_img, model.val_label])
                        acc = sess.run(model.accuracy, {model.X: img,
                                                        model.labels: label})  # feed_dict用来临时替换掉一个op的输出结果
                        val_acc += acc
                    val_acc = val_acc / (cfg.batch_size * cfg.num_val_batch)
                    fd_val_acc.write(str(global_step) + ',' + str(val_acc) + '\n')
                    fd_val_acc.flush()

            if (epoch + 1) % cfg.save_freq == 0:
                supervisor.saver.save(sess, cfg.logdir + '/model_epoch_%04d_step_%02d' % (epoch, global_step))
            # 如果没有saver，模型不会自动保存

        fd_val_acc.close()
        fd_train_acc.close()
        fd_loss.close()

def evaluation(model, supervisor):
    fd_test_acc = save_to()
    with supervisor.managed_session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
        supervisor.saver.restore(sess, tf.train.latest_checkpoint(cfg.logdir))  # 重新导入模型
        tf.logging.info('Model restored!')

        test_acc = 0
        for i in tqdm(range(cfg.num_te_batch), total=cfg.num_te_batch, ncols=70, leave=False, unit='b'):
            img, label = sess.run([model.te_img, model.te_label])
            acc = sess.run(model.accuracy, {model.X: img, model.labels: label})
            test_acc += acc
        test_acc = test_acc / (cfg.batch_size * cfg.num_te_batch)
        fd_test_acc.write(str(test_acc))
        fd_test_acc.close()
        print('Test accuracy has been saved to ' + cfg.results + '/test_accuracy.txt')

def main(_):
    tf.logging.info('Loading Graph...')
    model = CapsNet()
    tf.logging.info('Graph loaded!')
    sv = tf.train.Supervisor(graph=model.graph, logdir=cfg.logdir, save_model_secs=0)
    if cfg.is_training:
        tf.logging.info('Start Training...')
        train(model, sv)
        tf.logging.info('Train done!')
    else:
        evaluation(model, sv)
        tf.logging.info('Test done!')
    print("Main programming finished!")

if __name__ == "__main__":
    tf.app.run()


import dash
from dash import html
from dash import dcc
from dash.dependencies import Input, Output
import plotly.graph_objects as go
import plotly.express as px
import pandas as pd
from pathlib import Path

app = dash.Dash(__name__, external_stylesheets=["https://codepen.io/chriddyp/pen/bWLwgP.css"])
server = app.server

path_data = Path(Path(__file__).parent, 'games.csv')

df = pd.read_csv(path_data)
df.dropna(inplace=True)
df = df[df['Year_of_Release'] >= 2000]
df = df.loc[df['User_Score'] != 'tbd']

df['Year_of_Release'] = df['Year_of_Release'].astype(int)
df['Critic_Score'] = df['Critic_Score'].astype(int)
df['User_Score'] = df['User_Score'].astype(float)

years_filter = dcc.RangeSlider(
                    id='crossfilter-year--slider',
                    min=df['Year_of_Release'].min(),
                    max=df['Year_of_Release'].max(),
                    step=1,
                    pushable=False,
                    value=[df['Year_of_Release'].min(), df['Year_of_Release'].max()],
                    marks={str(year): str(year) for year in df['Year_of_Release'].unique()}
                    )

genres_filter = dcc.Dropdown(
                    id='crossfilter-genres',
                    options=dict(zip(df['Genre'].unique(), df['Genre'].unique())),
                    value=df['Genre'].unique(),
                    multi=True
                    )

ratings_filter = dcc.Dropdown(
                    id='crossfilter-ratings',
                    options=dict(zip(df['Rating'].unique(), df['Rating'].unique())),
                    value=df['Rating'].unique(),
                    multi=True
                     )

graf1 = dcc.Graph(id='stacked-area1')
graf2 = dcc.Graph(id='scatter-plot2')

app.layout = html.Div([
    html.H2(children="Интерактивный дашборд. Статистика по играм.",
            style={'padding': '10px 5px 5px 5px', "fontSize": "32px"}),

    html.Div("Предназначен для обзора статистики по играм, выпущенным с 2000 по 2016 год. "
            "Фильтры по жанрам и рейтингам поддерживают множественный выбор, "
            "фильтр по годам выпуска (внизу страницы) интервальный.",
             style={'padding': '10px 5px 5px 5px',"fontSize": "24px" }), #инструкция

    html.Div([
        html.Div("Фильтр жанров",style={'width': '49%', 'display': 'inline-block',"fontSize": "20px"}),
        html.Div("Фильтр рейтингов", style={'width': '49%', 'float': 'right', 'display': 'inline-block',"fontSize": "20px"})

   ], style={'padding': '5px 5px 5px 5px'}),

    html.Div([

        html.Div(genres_filter, style={'width': '49%', 'display': 'inline-block'}),
        html.Div(ratings_filter, style={'width': '49%', 'float': 'right', 'display': 'inline-block'})

    ], style={
        'padding': '10px 5px'
    }),

    html.H3(children=f'всего игр: {df["Name"].nunique()}', id='interactive-text1', style={'width': '49%', 'padding': '10px 10px 10px 10px'}),

    html.Div([
        html.Div(graf1, style={'width': '49%', 'display': 'inline-block', 'hight': '30%', 'padding': '0 20'}),
        html.Div(graf2, style={'display': 'inline-block', 'width': '49%', 'hight': '30%', 'padding': '0 20'}),
    ]),# style={'hight': '30%', 'padding': '0 20'}),

    html.Div(years_filter, style={'width': '600px', 'padding': '0px 20px 20px 20px'})
])

@app.callback(
    [
        Output('stacked-area1', 'figure'),
        Output('scatter-plot2', 'figure'),
        Output('interactive-text1', 'children')
    ],
    [
        Input('crossfilter-genres', 'value'),
        Input('crossfilter-ratings', 'value'),
        Input('crossfilter-year--slider', 'value')
    ])
def update_data(genre_values,
                rating_values,
                year_values):
    list_years = [i for i in range(year_values[0], year_values[1] + 1)]
    dff = df.query('Year_of_Release in @list_years and Rating in @rating_values and Genre in @genre_values')

    fig2 = go.Figure(px.scatter(dff.sort_values(by='User_Score'), x='User_Score', y='Critic_Score', color='Genre', hover_name='Name'))

    fig2.update_layout(
        autosize=True,
        title= f'Оценка критиков/оценка игроков с разбивкой по жанрам (цвет).',
        title_font={'size':20},
        height=500,
        yaxis=dict(title_text="Critic score"),
        xaxis=dict(title_text="User score")
    )

    data = dff.groupby(by=['Platform', 'Year_of_Release'],  as_index=False)\
              .agg({'Name': 'count'})\
              .rename(columns={'Name': 'num'})

    fig1 = go.Figure()

    for cur_plat in list(data.Platform.unique()):

        fig1.add_trace(go.Scatter(
            x=data.query('Platform == @cur_plat')['Year_of_Release'].values,
            y=data.query('Platform == @cur_plat')['num'].values,
            mode='lines',
            line=dict(width=0.5),
            name=cur_plat,
            text=data['Platform'],
            stackgroup='one' # define stack group
        ))
    fig1.update_layout(
        autosize=True,
        title= 'Количество выпущенных игр по годам и платформам',
        title_font={'size':20},
        margin_t=60,
        height=500,
        legend_title=dict(text='Platform'),
        yaxis=dict(
            title_text="Count games"),

    )
    text = f'всего игр: {dff["Name"].nunique()}'

    return fig1, fig2, text

# if __name__ == '__main__':
#     app.run_server(debug=True)


#!/usr/bin/env python3

import argparse
import os
import queue
import sounddevice as sd
import vosk
import sys
#import socket
#import ssl

q = queue.Queue()

def int_or_str(text):
    """Helper function for argument parsing."""
    try:
        return int(text)
    except ValueError:
        return text

def callback(indata, frames, time, status):
    """This is called (from a separate thread) for each audio block."""
    if status:
        print(status, file=sys.stderr)
    q.put(bytes(indata))

parser = argparse.ArgumentParser(add_help=False)
parser.add_argument(
    '-l', '--list-devices', action='store_true',
    help='show list of audio devices and exit')
args, remaining = parser.parse_known_args()
if args.list_devices:
    print(sd.query_devices())
    parser.exit(0)
parser = argparse.ArgumentParser(
    description=__doc__,
    formatter_class=argparse.RawDescriptionHelpFormatter,
    parents=[parser])
parser.add_argument(
    '-m', '--model', type=str, metavar='MODEL_PATH',
    help='Path to the model')
parser.add_argument(
    '-d', '--device', type=int_or_str,
    help='input device (numeric ID or substring)')
parser.add_argument(
    '-r', '--samplerate', type=int, help='sampling rate')
args = parser.parse_args(remaining)

try:
    if args.model is None:
        args.model = "model"
    if not os.path.exists(args.model):
        print ("Please download a model for your language from https://alphacephei.com/vosk/models")
        print ("and unpack as 'model' in the current folder.")
        parser.exit(0)
    if args.samplerate is None:
        device_info = sd.query_devices(args.device, 'input')
        # soundfile expects an int, sounddevice provides a float:
        args.samplerate = int(device_info['default_samplerate'])

    model = vosk.Model(args.model)

    with sd.RawInputStream(samplerate=args.samplerate, blocksize = 8000, device=args.device, dtype='int16',
                            channels=1, callback=callback):
            print('#' * 80)
            print('Press Ctrl+C to stop the recording')
            print('#' * 80)

            rec = vosk.KaldiRecognizer(model, args.samplerate)
            while True:
                data = q.get()
                if rec.AcceptWaveform(data):
                    # print(rec.Result()
                    str = rec.Result();
#                    os.system( "java PVA '"+ str.replace("'","")  +"'");
                    os.system( "echo '"+ str.replace("'","") +"' | openssl s_client -connect 127.0.0.1:39999 -nbio 1>/dev/null 2>/dev/null");
# in case anyone is interessted in a python solo solution, this is the way to go, if you want to not have a reliable ssl channel
#                   context = ssl.create_default_context()
#                   with socket.create_connection(("127.0.0.1", 39999)) as sock:
#                   	context.check_hostname = False;
#                   	context.verify_mode = False;
#                   	with context.wrap_socket(sock, server_side=False, server_hostname=None) as ssock:
#                   		print(ssock.version())
#                   		ssock.send(str.encode('utf-8'))
#                   		ssock.close()
#                   	sock.close()
#
# because this is what happens:
# working - Nicht für mich gedacht:test test
# not working - Thu Jul 14 13:30:43 CEST 2022: Server: javax.net.ssl.SSLException: Connection reset
# not working - Thu Jul 14 13:30:46 CEST 2022: Server: javax.net.ssl.SSLException: Connection reset
# not working - Thu Jul 14 13:30:53 CEST 2022: Server: javax.net.ssl.SSLException: Connection reset
# 3 / 4 of all TLS connects to the java server fail with python.

except KeyboardInterrupt:
    print('\nDone')
    parser.exit(0)
except Exception as e:
    parser.exit(type(e).__name__ + ': ' + str(e))


from bs4 import BeautifulSoup
from elasticsearch import Elasticsearch
from pdfminer.converter import PDFPageAggregator
from pdfminer.layout import LAParams, LTTextBox, LTTextLine
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfparser import PDFParser, PDFDocument
import datetime
import requests

def get_document_urls():
    r = requests.get('http://www.roanokeva.gov/agendacenter')
    soup = BeautifulSoup(r.text)
    docs = []
    for tag in soup.find_all('td', attrs={'class': 'minutes'}):
        anchor = tag.find('a')
        if not anchor:
            continue
        rel = anchor['href']
        if rel[0] == '/':
            rel = 'http://www.roanokeva.gov' + rel
        docs.append(rel)
    return docs

def convert_pdf_to_text(path):
    fp = open(path, 'rb')
    parser = PDFParser(fp)
    doc = PDFDocument()
    parser.set_document(doc)
    doc.set_parser(parser)
    doc.initialize('')
    rsrcmgr = PDFResourceManager()
    laparams = LAParams()
    device = PDFPageAggregator(rsrcmgr, laparams=laparams)
    interpreter = PDFPageInterpreter(rsrcmgr, device)
    pdfText = []
    # Process each page contained in the document.
    for page in doc.get_pages():
        interpreter.process_page(page)
        layout = device.get_result()
        for lt_obj in layout:
            if isinstance(lt_obj, LTTextBox) or isinstance(lt_obj, LTTextLine):
                pdfText.append(lt_obj.get_text())
    return pdfText

def handle():
    print('Beginning doc loop')
    es = Elasticsearch("192.168.1.71")
    #Replace phrase with userdata to be searched
    phrase="Test String To Be Replaced"
    lineNumCount=0
    phraseFinal=""
    lineNum=[]
    lineCounter=0

    es.indices.delete(index='meeting_minutes')
    es.indices.create(index='meeting_minutes')
    es.indices.put_mapping(index='meeting_minutes', doc_type='meeting_minute', body={
        "properties" : {
          "full_text" : {
            "type" : "string"
          },
          "import_date" : {
            "type" : "date",
            "format" : "strict_date_optional_time||epoch_millis"
          },
          "meeting_date" : {
            "type" : "string"
          },
          "meeting_time" : {
            "type" : "string"
          },
          "organization" : {
            "type" : "string"
          },
          "separated_text" : {
            "type" : "string"
          },
          "url" : {
            "type" : "string"
          },
          "votes" : {
            "type": "nested",
            "properties": {
              "AYES" : {
                "type" : "string"
              },
              "Motion" : {
                "type" : "string"
              },
              "NAYS" : {
                "type" : "string"
              }
            }
          }
        }
    })

    #es.delete(index="meeting_minutes"])

    for document in get_document_urls():
        print('Parse document', document)
        with open('/tmp/file.pdf', 'wb') as outf:
            r = requests.get(document, stream=True)
            for chunk in r.iter_content():
                if not chunk:
                    continue
                outf.write(chunk)
        try:
            text = convert_pdf_to_text('/tmp/file.pdf')
        except:
            print('Failed to parse')
            continue
        if len(text) < 20:
            print('Not enough text blocks, probably an image based PDF')
            print('will not parse')
            continue
        full_text = ""

        previous_line = ""
        two_lines_ago = ""
        three_lines_ago = ""
        votes = []
        for line in text:
            lineCounter=lineCounter+1
            if not line.strip().isdigit():
                full_text += line.strip() + '\n'
                #testCount=testCount+1
                #print(testCount)
            if phrase in line:
                lineNum.append(lineCounter)
            if line.startswith('NAYS:'):
                ayes = previous_line.replace('A YES: Council Members ', '') \
                            .replace('AYES: Council Members ', '') \
                            .replace(' and ', ', ') \
                            .replace("\n", ' ') \
                            .split(',')
                ayes = filter(lambda x: len(x) > 2, map(lambda x: x.strip(), ayes))
                ayes = list(ayes)
                nays = line.replace(' and ', ', ') \
                            .replace("\n", ' ') \
                            .split(',')
                if len(nays) > 0 and '0' in nays[0]:
                    nays = []
                votes.append({
                    'Motion': two_lines_ago,
                    'AYES': list(ayes),
                    'NAYS': nays
                })
            three_lines_ago = two_lines_ago
            two_lines_ago = previous_line
            previous_line = line

        doc = {
            'organization': text[1],
            'meeting_date': text[2],
            'meeting_time': text[3],
            'paragraph': lineNum,
            'url': document,
            'separated_text': text,
            'full_text': full_text,
            'import_date': datetime.datetime.now(),
            'votes': votes
        }
        res = es.index(index="meeting_minutes", doc_type='meeting_minute', body=doc)

handle()


"""
Марченко Андрій 141гр.
Розділ 2
10. Описати функцію IsSquare (K) логічного типу, яка повертає True, якщо цілий
параметр K (> 0) є квадратом деякого цілого числа, і False в іншому випадку. З її
допомогою знайти кількість квадратів в наборі з 10 цілих позитивних чисел.
"""

from math import sqrt

def IsSquare(K: int):
    if sqrt(K) == int(sqrt(K)):
        return True
    return False

def IsSquareForTenNumbers(list):
    for i in list:
        print(IsSquare(i))
    return "Finish"

i = 1
List = []
while i <= 10:
    n = input("Enter a number: ")
    try:
        n = int(n)
        if n <= 0:
            continue
        List.append(n)
    except ValueError:
        print("Wrong value")
        continue
    i += 1
    n = None
print("List: ", List)
print("Is square: ", IsSquareForTenNumbers(List))


# -*- coding:utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import copy

def rgb_to_ycbcr(im_rgb):
    im_ycbcr = copy.deepcopy(im_rgb)

    R = im_rgb[:, :, 0]
    G = im_rgb[:, :, 1]
    B = im_rgb[:, :, 2]

    im_ycbcr[:, :, 0] = 0.299 * R + 0.587 * G + 0.114 * B
    im_ycbcr[:, :, 1] = -0.1687 * R - 0.3313 * G + 0.5 * B + 128
    im_ycbcr[:, :, 2] = 0.5 * R - 0.4187 * G - 0.0813 * B + 128

    return np.uint8(im_ycbcr)

def ycbcr_to_rgb(im_ycbcr):

    im_rgb = copy.deepcopy(im_ycbcr)

    im_rgb = im_rgb.astype(np.float)

    Y = im_rgb[:, :, 0]
    Cb = im_rgb[:, :, 1] - 128
    Cr = im_rgb[:, :, 2] - 128

    im_result = copy.deepcopy(im_rgb)
    im_result[:, :, 0] = Y + 1.402*Cr
    im_result[:, :, 1] = Y - 0.34414*Cb - 0.71414*Cr
    im_result[:, :, 2] = Y + 1.772*Cb

    return np.uint8(im_result)

def main():
    im = Image.open('pic.jpg')
    im = np.array(im)

    plt.subplot(2, 2, 1)
    plt.title('origin image')
    plt.imshow(im)

    im_ycbcr = rgb_to_ycbcr(im)

    # 输出Y分量，使Cb,Cr分量为128
    im_ycbcr_y = copy.deepcopy(im_ycbcr)
    im_ycbcr_y[:,:,1] = 128
    im_ycbcr_y[:,:,2] = 128

    im_rgb_y = ycbcr_to_rgb(im_ycbcr_y)
    plt.subplot(2, 2, 2)
    plt.title('YCbCr Y')
    plt.imshow(im_rgb_y)
    plt.imsave("pic_ycbcr_y.jpg", im_rgb_y)

    # 输出Cb分量，使Y=0,Cr=0
    im_ycbcr_cb = copy.deepcopy(im_ycbcr)
    im_ycbcr_cb[:, :, 0] = 0
    im_ycbcr_cb[:, :, 2] = 0
    im_rgb_cb = ycbcr_to_rgb(im_ycbcr_cb)
    plt.subplot(2, 2, 3)
    plt.title('YCbCr Cb')
    plt.imshow(im_rgb_cb)
    plt.imsave("pic_ycbcr_cb.jpg", im_rgb_cb)

    # 输出Cr分量，使Y=0,Cb=0
    im_ycbcr_cr = copy.deepcopy(im_ycbcr)
    im_ycbcr_cr[:, :, 0] = 0
    im_ycbcr_cr[:, :, 1] = 0
    im_rgb_cr = ycbcr_to_rgb(im_ycbcr_cr)
    plt.subplot(2, 2, 4)
    plt.title('YCbCr Cr')
    plt.imshow(im_rgb_cr)
    plt.imsave("pic_ycbcr_cr.jpg", im_rgb_cr)

    plt.show()

if __name__ == '__main__':
    main()


from TAScheduler.models import Course, User, UserType
from typing import Optional, Union, Type, Iterable
import more_itertools

class CourseAPI:

    @staticmethod
    def create_course(code: str, name: str) -> Union[int, TypeError]:
        """
        Creates a course by taking a code and name with admin log, raises TypeError if argument issue
        """
        if code == '' or name == '':
            raise TypeError('Course code or Course name can\'t be empty.')

        new_course = Course.objects.create(code=code, name=name)
        return new_course.id

    @staticmethod
    def get_course_by_course_code(code: str) -> Optional[Course]:
        """
        Gets a course by its course_code, if it exists or returns None
        """
        try:
            course = Course.objects.get(code=code)
            return course
        except Course.DoesNotExist:
            return None

    @staticmethod
    def get_course_by_course_id(id: int) -> Optional[Course]:
        """
        Gets a course by its course_id, if it exists
        """
        try:
            course = Course.objects.get(id=id)
            return course
        except Course.DoesNotExist:
            return None

    @staticmethod
    def get_all_courses() -> Optional[Iterable[Course]]:
        """
        Gets all courses from the database, if they exist
        """
        set = Course.objects.all()
        return set if more_itertools.ilen(set) > 0 else None

    @staticmethod
    def edit_course(id: int) -> bool:
        pass

    @staticmethod
    def delete_course(id: int) -> bool:
        """
        Deletes course, if it exists, using a course_id value, returns boolean to confirm
        """
        try:
            course = Course.objects.get(id=id)
            course.delete()
            return True
        except Course.DoesNotExist:
            return False


from acf.wrappers.base import BaseResultContainer

class HttpResultContainer(BaseResultContainer):

    def __init__(
        self, is_successful=True, result=None, raw_result=None
    ):
        super(HttpResultContainer, self).__init__(
            result=result, raw_result=raw_result
        )
        self.is_successful = is_successful

    @property
    def response(self):
        return self.raw_result