import json
from pathlib import Path

import numpy as np
import argparse
from typing import Text, Optional, List, Dict, Any, Set, BinaryIO

class Codebook:

    def __init__(self, directoryname: Text):
        codebook_filename = Path(directoryname) / "codebook.bin"
        print(f"Reading codebook from {codebook_filename}")
        with open(codebook_filename, "br") as codebook:
            self.format = int(np.fromfile(codebook, dtype=np.uint8, count=1)[0])
            assert self.format == 0
            self.height = int(np.fromfile(codebook, dtype=np.uint64, count=1)[0])
            self.width = int(np.fromfile(codebook, dtype=np.uint64, count=1)[0])
            self.vocab_size = int(np.fromfile(codebook, dtype=np.uint64, count=1)[0])
            self._codebook = np.reshape(
                np.fromfile(codebook, dtype=np.float32, count=-1),
                (self.height, self.width, self.vocab_size)
            )
        readme_filename = Path(directoryname) / "README.md"
        print(f"Reading README from {readme_filename}")
        self.readme = ""
        with open(readme_filename, "r") as readme:
            for line in readme:
                self.readme += line
                if line.startswith("Local topology:"):
                    self.local_topology = line[len("Local topology:"):].strip()
                elif line.startswith("Global topology:"):
                    self.global_topology = line[len("Global topology:"):].strip()

    def __str__(self) -> str:
        return f"Codebook({np.shape(self._codebook)})"

    def fingerprint(self, i: int, size: int) -> List[int]:
        assert 0 <= i < self.vocab_size
        assert 0 < size < self.vocab_size
        weights = np.reshape(
            self._codebook[:, :, i],
            (-1, )
        )
        # Return the `size` largest values
        return np.argsort(weights)[:size].tolist()

def run(
    codebook_filename: Text,
    vocabulary_filename: Text,
    output_filename: Text,
    density: Optional[int] = None,
) -> None:
    codebook = Codebook(codebook_filename)
    vocab: List[Text] = []
    with open(vocabulary_filename, "r", encoding="utf-8") as file:
        for word in file:
            vocab.append(word.strip())
    print("Extracting semantic map...")
    semantic_map = dict()
    if density:
        size = max(int(density * codebook.width * codebook.height), 1)
    else:
        size = max(codebook.width * codebook.height // 50, 5)
    for i, word in enumerate(vocab):
        semantic_map[word] = codebook.fingerprint(i, size)
    print(f"Exporting to {output_filename}")
    with open(output_filename, "w+") as file:
        json.dump(
            {
                "Note": "",
                "Height": codebook.height,
                "Width": codebook.width,
                "AssumeLowerCase": all([word.islower() for word in vocab]),
                "GlobalTopology": codebook.global_topology,
                "LocalTopology": codebook.local_topology,
                "CreationReadme": codebook.readme,
                "CreationOptions": {"MaxActiveCells": size, "Executor": "PythonScript"},
                "Embeddings": semantic_map,
            },
            file
        )

def float_between_0_and_1_or_None(x) -> Optional[float]:
    if x is None:
        return None
    try:
        x = float(x)
    except ValueError:
        raise argparse.ArgumentTypeError("%r not a floating-point literal" % (x,))

    if x < 0.0 or x > 1.0:
        raise argparse.ArgumentTypeError("%r not in range [0.0, 1.0]"%(x,))
    return x

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Convert semantic map binary codebook into json"
    )
    parser.add_argument(
        "--codebook",
        type=str,
        help="Path to codebook binary",
    )
    parser.add_argument(
        "--vocabulary",
        type=str,
        help="Path to vocabulary list",
    )
    parser.add_argument(
        "--out",
        type=str,
        help="Name of the json file to create",
    )
    parser.add_argument(
        "--density",
        type=float_between_0_and_1_or_None,
        help="(Optional) Approximate density of the map",
    )

    args = parser.parse_args()

    run(
        args.codebook,
        args.vocabulary,
        args.out,
        args.density,
    )


import numpy as np
import matplotlib.pyplot as plt

def f(x):
    if x < 0 or x > 1:
        return 0
    else:
        return 1 - x

def g(x):
    if x < 0 or x > 2:
        return 0
    else:
        return 1 - x/2

def riemann(f, g, bounds = [-3, 4], delta_x = 0.001):
    s = 0
    for x in np.arange(bounds[0], bounds[1], delta_x):
        s += f(x) * g(x) * delta_x
    return s

def convo(f, g, delta_u = 0.001):
    h = []
    for u in np.arange(-5, 5, delta_u):
        print(u)
        h.append(riemann(f, lambda y: g(u - y)))

    return h

h = convo(f, g)

plt.plot(np.arange(-5, 5, 0.001), h)
plt.show()


from functools import reduce
from operator import concat

# ===================================================
# utils
# ===================================================

def select_keys(d: dict, ks: list) -> dict:
    return {k: d[k] for k in ks if k in d}

# ===================================================
# API
# ===================================================

def produce_api_calls(intervals: list) -> list:
    intervals = [select_keys(x, ['vessel', 'start', 'end']) for x in intervals]
    intervals = sorted(intervals, key=lambda x: (x['vessel'], x['start']))
    v_to_intervals = {}
    for interval in intervals:
        vessel, start2, end2 = interval.get('vessel'), interval.get('start'), interval.get('end')
        cur_intervals = v_to_intervals.get(vessel, [])
        if not cur_intervals or cur_intervals[-1].get('end') < start2:
            # empty or no_overlap (end1<start2): add interval
            cur_intervals.append(interval)
            v_to_intervals[vessel] = cur_intervals
        else:
            # overlaps (start2 <= end1): update cur interval with [start1, max(end1, end2)]
            v_to_intervals[vessel][-1]['end'] = max(cur_intervals[-1].get('end'), end2)
    return list(reduce(concat, v_to_intervals.values()))


import numpy as np
import random
import math
import copy
import matplotlib.pyplot as plt
import matplotlib.animation as animation

class RED_FOXES:
    def __init__(self, maxx, minx, wymiar, fitness, alfa):
        self.wymiar = wymiar
        self.position = np.empty(wymiar)
        self.mi = random.uniform(0, 1)
        self.fitness = fitness
        self.alfa = alfa
        for j in range(int(wymiar)):
            if j == 0:
                self.position[j] = random.uniform(minx[0], maxx[0])
            else:
                self.position[j] = random.uniform(minx[j], maxx[j])

    def __repr__(self):
        return '{' +str(self.fitness) + ', ' + str(self.position) + ', ' + str(self.mi) + ', '+ str(self.alfa) +'}'

def fitness_function(x, nazwa_funckji_celu): #  x to lista, ale nie musibyc w zaleznosci od funkcji lista napewno to bedzie w przypadku powella i brown
    lista_pomocnicza1 = np.empty(lisy[1].wymiar)
    lista_pomocnicza2 = np.empty(lisy[1].wymiar)
    zmienna_pomocnicza1 = 0
    zmienna_pomocnicza2 = 0
    result = 0
    a = 0
    b = 0
    if nazwa_funckji_celu == "brown":
        for j in range(lisy[1].wymiar-1):
            zmienna_pomocnicza1 = math.pow(x[0], 2)
            zmienna_pomocnicza2 = math.pow(x[1], 2) + 1
            lista_pomocnicza1[j] = math.pow(zmienna_pomocnicza1, zmienna_pomocnicza2)
        a = math.pow(x[0], 2) + 1
        b = math.pow(x[1], 2)
        result = (np.sum(lista_pomocnicza1)) + (math.pow((b), (a)))
    elif nazwa_funckji_celu == "csendes":
        for j in range(lisy[1].wymiar):
            lista_pomocnicza1[j] = math.pow(x[j], 6)*(2+np.sin(1/x[j]))
        result = np.sum(lista_pomocnicza1)
    elif nazwa_funckji_celu == "griewank":
        for j in range(lisy[1].wymiar):
            lista_pomocnicza1[j] = ((math.pow(x[j], 2)) / 4000)
            lista_pomocnicza2[j] = np.cos((x[j] / np.sqrt(j+1)))
        lista_pomocnicza1[0] = np.sum(lista_pomocnicza1)
        lista_pomocnicza2[0] = np.prod(lista_pomocnicza2)
        result = 1+lista_pomocnicza1[0] - lista_pomocnicza2[0]
    elif nazwa_funckji_celu == "powell":
        for j in range(lisy[1].wymiar/4):
            lista_pomocnicza1[j] = ((math.pow((x[(4*j) -3]+10*(x[(4*j)-2])), 2))+(5*math.pow((x[(4*j)-1]-x[4*j]),2))+(math.pow((x[(4*j)-2]-2*x[(4*j)-1]),4))+(10*math.pow((x[(4*j)-3]-x[4*j]),2)))
        result = np.sum(lista_pomocnicza1)
    elif nazwa_funckji_celu == "powell sum":
        for j in range(lisy[1].wymiar):
            lista_pomocnicza1[j] = math.pow(np.abs(x[j]),j+1)
        result = np.sum(lista_pomocnicza1)
    elif nazwa_funckji_celu == "rastragin":
        for j in range(lisy[1].wymiar):
            lista_pomocnicza1[j] = (math.pow(x[j], 2))-(10*np.cos(2*math.pi*x[j]))
        result = (10*lisy[1].wymiar) + np.sum(lista_pomocnicza1)
    elif nazwa_funckji_celu == "rotated hyper-ellipsoid":
        for l in range(lisy[1].wymiar):
            for j in range(lisy[1].wymiar):
                lista_pomocnicza1[j] = math.pow(x[j], 2)
            lista_pomocnicza2[l] = np.sum(lista_pomocnicza1)
        result = np.sum(lista_pomocnicza2)
    elif nazwa_funckji_celu == "mishra":
        for j in range(lisy[1].wymiar):
            lista_pomocnicza1[j] = np.abs(x[j])
        result = math.pow(((1/lisy[1].wymiar) * (np.sum(lista_pomocnicza1)))-(math.pow(np.abs(x[j]), 1/lisy[1].wymiar)), 2)
    elif nazwa_funckji_celu == "salomon":
        for j in range(lisy[1].wymiar):
            lista_pomocnicza1[j] = math.pow(x[j], 2)
            lista_pomocnicza2[j] = math.pow(x[j], 2)
        result = 1 - (np.cos(np.sum(lista_pomocnicza1))) + (0.1*np.sqrt(np.sum(lista_pomocnicza2)))
    else:
        print("zla nazwa funkcji")
        result = 0
    # t1 = np.sum(x ** 2) / 4000
    # t2 = np.prod([np.cos(x[idx] / np.sqrt(idx + 1)) for idx in range(0, 2)])
    return result

def obser_angle(observation_radius, fi, weather):
    if fi != 0:
        return observation_radius*(np.sin(fi)/fi)
    if fi == 0:
        return weather

def odleglosc_euklidesowa(v, u):
   return np.linalg.norm(v - u)

weather = random.uniform(0,1)
wymiar = 2
minima = [-10, -10]
maxima = [10, 10]
ilosc_liskow = 50
#generowanie liskow w roznych zakresach
lisy = []
lisy1 = []
y = 0
liczba_powtorzen_calego_algorytmu = 20
while y < ilosc_liskow:
    lisy.append(RED_FOXES(maxima, minima, wymiar, 0, 0))
    y = y+1

print(lisy)
#liczba iteracji
T = 80

fitness_prawdziwe = np.empty(ilosc_liskow)

#przelicza przez funkcje celu
for i in range(int(ilosc_liskow)):
    lisy[i].fitness = fitness_function(lisy[i].position, "griewank")

#posortowana przeliczona funkcja celu

#for l in range(int(ilosc_liskow)):
    #print(str(l) + ". " + str(lisy[l].position) + " / " + str(lisy[l].fitness))

#print(len(lisy))
lisy1 = copy.deepcopy(lisy)
#for l in range(int(ilosc_liskow)):
    #print(str(l) + ". " + str(lisy1[l].position) + " / " + str(lisy1[l].fitness))
#print(len(lisy1))
lista_obliczenia = np.empty(liczba_powtorzen_calego_algorytmu)
lista_wykres = np.empty((T*2, ilosc_liskow))
#glowna petla while

for z in range(liczba_powtorzen_calego_algorytmu):
    wykr = 0
    t=0
    lisy.clear()
    lisy=copy.deepcopy(lisy1)
    while t < T:
        zmienna = 0
        kappa = random.uniform(0, 1)
        fi_obser = np.empty(len(lisy)+1)
        fi_obser[0] = random.uniform(0, 2 * np.pi)
        lisy.sort(key=lambda x: np.abs(x.fitness))
        x_best = lisy[0].position
        a = random.uniform(0, 0.2)

        for o in range(len(lisy)):
            lisy[o].alfa = random.uniform(0, odleglosc_euklidesowa(lisy[o].position, x_best))
            fi_obser[o+1] = random.uniform(0, 2 * np.pi)
            x_z_kreska = lisy[o].position
            x_z_kreska = x_z_kreska + lisy[o].alfa*np.sign(x_best - x_z_kreska)
            if np.abs(lisy[o].fitness) > np.abs(fitness_function(x_z_kreska, "griewank")):
                lisy[o].position = x_z_kreska
                lisy[o].fitness = fitness_function(x_z_kreska, "griewank")

            noticing = lisy[o].mi
            if noticing > 0.75:
                r = obser_angle(a, fi_obser[0], weather)
                ar_cos = 0
                for h in range(0, wymiar):
                    ar_cos = ar_cos + a * r * np.cos(fi_obser[o+1])
                    lisy[o].position[h] = ar_cos + lisy[o].position[h]
                lisy[o].fitness = fitness_function(lisy[o].position, "griewank")

        lisy.sort(key=lambda x: np.abs(x.fitness))
        old_lisy_len = len(lisy)
        for g in range(0, math.ceil(0.05 * len(lisy))):
            lisy.pop(len(lisy)-1 - g)
        new_lisy_len = len(lisy)
        habitat_center = (lisy[0].position + lisy[1].position)/2
        habitat_diameter = np.sqrt(np.linalg.norm(lisy[0].position-lisy[1].position))
        for c in range(0,old_lisy_len - new_lisy_len):
            if kappa >= 0.45:
                nowe_minx = [habitat_center[0]+habitat_diameter, habitat_center[0]+habitat_diameter]
                lisy.append(RED_FOXES(maxima, nowe_minx, wymiar, 0, 0))
                lisy[-1].fitness = fitness_function(lisy[-1].position, "griewank")
            else:
                new_from_alfa_foxes_position = kappa*habitat_center
                max_min_new_from_alfa_foxes_position = [new_from_alfa_foxes_position[0], new_from_alfa_foxes_position[1]]
                lisy.append(RED_FOXES(max_min_new_from_alfa_foxes_position, max_min_new_from_alfa_foxes_position, wymiar, 0 ,0))
                lisy[-1].fitness = fitness_function(lisy[-1].position, "griewank")
        t = t + 1
        for h in range(len(lisy)):
            lista_wykres[wykr][h] = lisy[h].position[0]
            lista_wykres[wykr+1][h] = lisy[h].position[1]
        #plt.figure(t)
        wykr += 2
        print("generacja: "+str(t)+", najlepsze: "+str(lisy[0]))
        #plt.plot(lista_wykres[0], lista_wykres[1], 'ro')
    lisy.sort(key=lambda x: np.abs(x.fitness))
    print("generacja:"+str(t)+"najlepsze: "+str(lisy[0]))
    lista_obliczenia[z] = lisy[0].fitness
print("odchylenie standardowe: "+str(np.std(lista_obliczenia[0:45]))+", srednia: "+str(np.average(lista_obliczenia[0:45])))

fig, ax = plt.subplots()
print(lista_wykres[2])

def animate(i):
    x_axes = lista_wykres[i]
    y_axes = lista_wykres[i+1]

    ax.clear()
    ax.set_xlim(-10, 10)
    ax.set_ylim(-10, 10)
    ax.autoscale(enable=False, axis='both', tight=None)
    i+=2
    return ax.plot(x_axes, y_axes, 'o')

ani = animation.FuncAnimation(fig, animate, frames=T, interval=100, repeat=False)

plt.show()

#ani.save('location.gif', writer=animation.ImageMagickFileWriter()) odkomentować jeśli ma zapisać animacje


import os
import subprocess
import tkinter as tk
from tkinter import messagebox, filedialog

def scan_drive():
    # Define the path to scan
    path = "C:\\"

    # Run the built-in Windows command to check for file system errors
    result = subprocess.run(["chkdsk", path], stdout=subprocess.PIPE)

    # Check the output for any errors
    output = result.stdout.decode("utf-8")
    if "errors" in output:
        messagebox.showwarning("Scan Results", "There are file system errors on drive C.")
    else:
        messagebox.showinfo("Scan Results", "No errors were found on drive C.")

    # Write the results to a text file
    desktop_path = os.path.join(os.path.join(os.environ["USERPROFILE"]), "Desktop")
    filename = filedialog.asksaveasfilename(initialdir=desktop_path, title="Save Scan Results", filetypes=(("Text Files", "*.txt"),))
    if filename:
        with open(filename, "w") as f:
            f.write(output)

    # Update the status bar
    status_bar.config(text="Scan complete.")

# Create the main window
root = tk.Tk()
root.title("Simple Shitty Drive Scanner")
root.geometry("300x150")

# Create the button to scan the drive
scan_button = tk.Button(root, text="Scan Drive", command=scan_drive)
scan_button.pack(pady=20)

# Create the button to open the GitHub page
def open_github():
    os.system("start https://github.com/gravymix")
github_button = tk.Button(root, text="My GitHub", command=open_github)
github_button.pack()

# Create the status bar
status_bar = tk.Label(root, text="Ready.")
status_bar.pack(side=tk.BOTTOM, fill=tk.X)

# Start the main event loop
root.mainloop()


import sqlite3
# from flask import jsonify
# from flask import request

conn = sqlite3.connect('cards.db')
c = conn.cursor()

# c.execute("""
#     CREATE TABLE DominionGames (
#     game_id integer PRIMARY KEY,
#     Player1Name text NOT NULL,
#     Player1Score text NOT NULL,
#     Player2Name text NOT NULL,
#     Player2Score text NOT NULL,
#     Player3Name text,
#     Player3Score text,
#     Player4Name text,
#     Player4Score text)""")
# print("Table made succsessfully")

#c.execute("INSERT INTO DominionGames (game_id, Player1Name, Player1Score, Player2Name, Player2Score) VALUES ('1', 'Josh', '10', 'John', '8')")

c.execute("SELECT * FROM DominionGames")
print(c.fetchall())


import jax
import tensorflow as tf
import numpy as np
from transformers import GPT2TokenizerFast
import itertools

class TFRecordLoader:
    def __init__(self, index_fname, batch_size, parse_fn, map_fn=None, restore_state=None):
        if restore_state is not None:
            self.file_idx = restore_state["file_idx"]
            self.file_idx_init = False
            self.used = restore_state["used"]
        else:
            self.file_idx = 0
            self.file_idx_init = True
            self.used = []

        self.index = open(index_fname).read().splitlines()
        self.clean_index = list(filter(lambda x: x not in self.used, self.index))
        self.bs = batch_size
        # self.seq = sample_size
        self.parse_fn = parse_fn

        if map_fn:
            self.map_fn = map_fn
        else:
            self.map_fn = lambda x: x

        self.sample_fn = self.sample_once()

    def reset(self):
        self.file_idx = 0
        self.file_idx_init = True
        self.used = []

        self.clean_index = list(filter(lambda x: x not in self.used, self.index))
        self.sample_fn = self.sample_once()

    def sample_once(self):
        for i in self.clean_index:
            compression = "ZLIB" if "zstd" in i else ""

            file = tf.data.TFRecordDataset(i, compression_type=compression).map(self.parse_fn, num_parallel_calls=tf.data.AUTOTUNE)
            file = file.apply(tf.data.experimental.dense_to_ragged_batch(np.prod(self.bs), drop_remainder=True))
            file = file.prefetch(10)

            for file_idx, data in enumerate(file):
                data = jax.tree_map(lambda x: x.numpy(), data)
                data = self.map_fn(data)

                if not self.file_idx_init and file_idx <= self.file_idx:
                    if file_idx % 1000 == 0:
                        print(f"skipping to batch {self.file_idx}, currently at {file_idx}")
                    continue
                self.file_idx_init = True
                self.file_idx = file_idx
                yield jax.tree_map(lambda x: x.reshape(self.bs + x.shape[1:]), data)
            self.used.append(i)
            self.file_idx = 0

    # this loops infinitely, use .sample_once to get an iterator for validation
    def get_samples(self):
        try:
            return next(self.sample_fn)
        except StopIteration:
            self.reset()
            return self.get_samples()

    def get_state(self):
        return {
            "used": self.used,
            "file_idx": self.file_idx
        }

class TFRecordNewInputs(TFRecordLoader):
    def __init__(self, index_fname, batch_size, sample_size, restore_state=None):
        def tf_parse(example_proto):
            features = {
                "text": tf.io.VarLenFeature(tf.int64)
            }
            parsed_features = tf.io.parse_single_example(example_proto, features)

            return tf.cast(tf.sparse.to_dense(tf.sparse.reorder(parsed_features["text"])), tf.uint32)

        super().__init__(index_fname, batch_size, tf_parse, restore_state=restore_state)

def _parse_function(example_proto): # https://zhuanlan.zhihu.com/p/552951305  # XD
    feature_desc = {"input_ids": tf.io.VarLenFeature(tf.int64)}
    example = tf.io.parse_single_example(example_proto, feature_desc)
    for name in list(example.keys()):
        t = example[name]
        if t.dtype == tf.int64: t = tf.cast(t, dtype=tf.int32)
        example[name] = tf.sparse.to_dense(t, default_value=0)
        # example[name] = tf.sparse.to_dense(tf.sparse.reorder(t)) # mesh-transformer-jax
    return example

def shard(data, batch_size=None):  # XD
    return jax.tree_map(lambda x: x.numpy().reshape(batch_size + x.shape[1:]), data)  # mtj

def load_tfrecord_dataset(index_fname, batch_size, seq_len, restore_state=None):  # XD
    # adapted from gpt-neo
    fnames = [index_fname] if index_fname.endswith('.tfrecords') else open(index_fname).read().splitlines()
    ds = tf.data.Dataset.from_tensor_slices(fnames)#.repeat()
    ds = ds.apply(tf.data.TFRecordDataset)
    # fp = index_fname; ds = tf.data.TFRecordDataset(fp)
    # # ds = ds.shuffle(buffer_size=min(1000, len(sequences))) # flaxmodels, https://zhuanlan.zhihu.com/p/552951305
    ds = ds.map(_parse_function, num_parallel_calls=tf.data.AUTOTUNE)
    # gradient_accumulation_steps = 8
    # mp_size, dp_size = 8, 1
    # train_mbs_per_replica = 2 # train_micro_batch_size_per_gpu in deepspeed
    # train_batch_size = (gradient_accumulation_steps, train_mbs_per_replica * dp_size)
    # seq_len = 80  # max(len(s) for s in sequences) == 78
    # ds = ds.apply(tf.data.experimental.dense_to_ragged_batch(np.prod(self.bs), drop_remainder=True)) # mtj
    ds = ds.padded_batch(batch_size=np.prod(batch_size), padded_shapes={'input_ids': [seq_len]},
                        padding_values={'input_ids': 0}, drop_remainder=True)
    ds = ds.prefetch(10)  # mesh-transformer-jax
    # ds = ds.repeat()  # gpt-neo/inputs.py
    # map shard directly over ds won't work, getting AttributeError: 'Tensor' object has no attribute 'numpy'
    # because inside tf.function?, see e.g.:
    # 1) https://stackoverflow.com/questions/34097281/convert-a-tensor-to-numpy-array-in-tensorflow
    # 2) https://github.com/tensorflow/tensorflow/issues/27519
    # ds = ds.map(partial(shard, batch_size=batch_size), num_parallel_calls=tf.data.AUTOTUNE)
    # matthias-wright/flaxmodels/training/stylegan2/data_pipeline.py
    return map(lambda x: shard(x, batch_size=batch_size), iter(ds))

class TFRecordWIT(TFRecordLoader):
    def __init__(self, index_fname, batch_size, restore_state=None, text_tokens=256):
        self.tokenizer = GPT2TokenizerFast.from_pretrained("gpt2")
        self.tokenizer.pad_token = "<|endoftext|>"
        self.tokenizer.add_special_tokens({'sep_token': '<|sep|>', 'pad_token': '<|pad|>'})

        def map_fn(example):
            tokenizer = self.tokenizer

            def decode(x):
                return tokenizer(["<|endoftext|>" + i.decode() for i in x])["input_ids"]

            texts = [
                decode(example["context_page_description"]),
                decode(example["context_section_description"]),
                decode(example["caption_reference_description"]),
                decode(example["caption_alt_text_description"]),
                decode(example["caption_attribution_description"]),
            ]

            output = []

            for text, dalle in zip(zip(*texts), example["dalle"]):
                all_text = list(itertools.chain(*text))[-text_tokens+1:]

                all_text += [tokenizer.pad_token_id] * ((text_tokens - 1) - len(all_text))

                assert len(all_text) == text_tokens - 1

                all_tokens = all_text + [tokenizer.sep_token_id] + list(dalle + tokenizer.vocab_size + 1)
                output.append(all_tokens)

            return np.array(output)

        def tf_parse(example_proto):
            features = {
                "page_title": tf.io.FixedLenFeature([], tf.string),
                "section_title": tf.io.FixedLenFeature([], tf.string),
                "hierarchical_section_title": tf.io.FixedLenFeature([], tf.string),
                "caption_reference_description": tf.io.FixedLenFeature([], tf.string),
                "caption_attribution_description": tf.io.FixedLenFeature([], tf.string),
                "caption_alt_text_description": tf.io.FixedLenFeature([], tf.string),
                "mime_type": tf.io.FixedLenFeature([], tf.string),
                "context_page_description": tf.io.FixedLenFeature([], tf.string),
                "context_section_description": tf.io.FixedLenFeature([], tf.string),

                "dalle": tf.io.FixedLenFeature([1024], tf.int64),
            }

            parsed_features = tf.io.parse_single_example(example_proto, features)

            return parsed_features

        super().__init__(index_fname, batch_size, tf_parse, map_fn, restore_state=restore_state)

if __name__ == "__main__":
    # d = TFRecordNewInputs("data/pile.val.index", (8, 32), 2048)
    # for idx, i in enumerate(d.sample_once()):
    #     print(i)
    #     break

    d = TFRecordWIT("data/wit_dalle.train.index", (8, 32))
    for idx, i in enumerate(d.sample_once()):
        print(i)
        break

    print()


from ..helper import config, get_class_from_dot_string
from ..logging import logger

default_storage = config('DEFAULT_FILE_STORAGE')

class DefautStorageBackend:
    default_backend = None
    instance = None

    def __init__(self):
        if not default_storage:
            raise Exception('Default storage backend not configured')

        self.default_backend = get_class_from_dot_string(default_storage)()

    @property
    def backend(self):
        return self.default_backend

    @staticmethod
    def getInstance():
        if not DefautStorageBackend.instance:
            DefautStorageBackend.instance = (DefautStorageBackend()).backend
        return DefautStorageBackend.instance

try:
    default_backend = DefautStorageBackend.getInstance()
except Exception as e:
    logger.exception(e)
    default_backend = None

__all__ = [
    'default_backend'
]


import json
import unittest
from copy import deepcopy
from http import HTTPStatus
from typing import Union

from flask import Response

from team_picker.constants import (TEAMS_URL, TEAM_BY_ID_URL,
                                   RESULT_ONE_ROLE, RESULT_LIST_TEAMS,
                                   RESULT_CREATED_COUNT, RESULT_ONE_TEAM,
                                   RESULT_DELETED_COUNT, RESULT_UPDATED_COUNT,
                                   POST_TEAM_PERMISSION, GET_TEAM_PERMISSION,
                                   )
from team_picker.models import M_ID, M_NAME, Team

from base_test import BaseTestCase
from misc import make_url, MatchParam, UserType, Expect
from test_data import EqualDataMixin, TeamData, UNASSIGNED_TEAM

TEAM_1 = '1'
TEAM_2 = '2'
TEAM_3 = '3'
TEAM_4 = '4'
TEAM_5 = '5'
TEAMS_KEY_LIST = [TEAM_1, TEAM_2, TEAM_3, TEAM_4, TEAM_5]
TEAMS = {
    k: TeamData(0, f"Team {k}")
    for k in TEAMS_KEY_LIST
}

class TeamsTestCase(BaseTestCase):
    """This class represents the test case for teams."""

    teams = {}

    def setUp(self):
        """
        Method called to prepare the test fixture.
        This is called immediately before calling the test method.
        """
        super().setUp()

        self.teams = TeamsTestCase.setup_test_teams(self)

    @staticmethod
    def setup_test_teams(test_case: BaseTestCase) -> dict:
        """
        Setup teams for tests.
        :param test_case:   BaseTestCase instance
        :return
            teams {     # key is '{team number}'
                '1': {<team dict>},
                ...
            }
        """
        teams = {
            UNASSIGNED_TEAM.name:
                TeamData(UNASSIGNED_TEAM.id, UNASSIGNED_TEAM.name).to_dict()
        }
        with test_case.app.app_context():
            # Add required teams.
            app_db = test_case.get_db()
            for k, team_data in TEAMS.items():
                team = team_data.to_model(ignore=[M_ID])
                app_db.session.add(team)
                app_db.session.flush()
                team_data.id = team.id

                teams[k] = team_data.to_dict()

            app_db.session.commit()

        return teams

    def tearDown(self):
        """
        Method called immediately after the test method has been called
        and the result recorded.
        """
        super().tearDown()

    def assert_team(self, expected: Union[EqualDataMixin, dict], actual: dict,
                    msg: str = None):
        """
        Assert teams are equal.
        """
        if msg is None:
            msg = ""
        if isinstance(expected, EqualDataMixin):
            self.assert_equal_data(expected, actual, ignore=None)
        else:
            self.assert_equal_dict(expected, actual, ignore=None)

    def assert_teams_list(self, expected: list[EqualDataMixin], actual: list,
                          msg: str = None):
        """
        Assert teams lists are equal.
        """
        if msg is None:
            msg = ""
        self.assertEqual(len(expected), len(actual),
                         f'Length mismatch for {msg}')
        index = 0
        for actual_team in actual:
            with self.subTest(actual_team=actual_team):
                self.assert_team(expected[index], actual_team,
                                 msg=f'team {index} {msg}')
                index = index + 1

    def assert_teams_list_resp(self, expect: Expect, resp: Response,
                               expected: list[EqualDataMixin],
                               http_status: Union[
                                   HTTPStatus, range] = HTTPStatus.OK,
                               msg: str = None):
        """
        Assert teams list responses are equal.
        :param expect:      Expected result; one of Expect.SUCCESS or
                            Expect.FAILURE
        :param resp:        response body
        :param expected:    expected list
        :param http_status: expected HTTPStatus or range of status codes
        :param msg:         assert error message
        """
        if msg is None:
            msg = ""

        self.assertTrue(resp.is_json)
        resp_body = resp.get_json()

        if expect == Expect.SUCCESS:
            self.assert_ok(resp.status_code)

            self.assert_success_response(resp_body)
            self.assert_body_entry(resp_body, RESULT_LIST_TEAMS,
                                   MatchParam.IGNORE)
            resp_teams = resp_body[RESULT_LIST_TEAMS]

            self.assert_teams_list(expected, resp_teams, msg=msg)
        else:
            if isinstance(http_status, HTTPStatus):
                self.assert_response_status_code(http_status,
                                                 resp.status_code, msg=msg)

            self.assertTrue(resp.is_json)
            self.assert_error_response(resp_body, http_status, '',
                                       MatchParam.IGNORE, msg=msg)

    def assert_team_resp(self, expect: Expect, resp: Response,
                         expected: EqualDataMixin,
                         http_status: Union[
                             HTTPStatus, range] = HTTPStatus.OK,
                         msg: str = None):
        """
        Assert team response is equal.
        :param expect:      Expected result; one of Expect.SUCCESS or
                            Expect.FAILURE
        :param resp:        response body
        :param expected:    expected entity
        :param http_status: expected HTTPStatus or range of status codes
        :param msg:         assert error message
        """
        if msg is None:
            msg = ""

        self.assertTrue(resp.is_json)
        resp_body = resp.get_json()

        if expect == Expect.SUCCESS:
            self.assert_ok(resp.status_code)
            self.assert_success_response(resp_body)
            self.assert_body_entry(resp_body, RESULT_ONE_TEAM, MatchParam.EQUAL,
                                   value=expected)
        else:
            if isinstance(http_status, HTTPStatus):
                self.assert_response_status_code(http_status,
                                                 resp.status_code, msg=msg)

            self.assertTrue(resp.is_json)
            self.assert_error_response(resp_body, http_status, '',
                                       MatchParam.IGNORE, msg=msg)

    def test_get_all_teams(self):
        """
        Test get all teams to ensure valid users can access and others cannot.
        """
        with self.client as client:
            for user in UserType:
                permissions = self.set_permissions(user)

                with self.subTest(user=user):
                    resp = client.get(
                        make_url(TEAMS_URL)
                    )

                    has = BaseTestCase.has_permission(
                        permissions, GET_TEAM_PERMISSION)
                    expect = Expect.SUCCESS if has else Expect.FAILURE
                    if has:
                        http_status = HTTPStatus.OK
                    else:
                        http_status = HTTPStatus.UNAUTHORIZED

                    self.assert_teams_list_resp(
                        expect, resp, [v for v in self.teams.values()],
                        http_status=http_status, msg=f"{user}")

    def test_get_team_by_id(self):
        """
        Test get teams by id.
        """
        with self.client as client:
            for user in UserType:
                permissions = self.set_permissions(user)

                with self.subTest(user=user):
                    for k, v in self.teams.items():
                        with self.subTest(team=v):
                            resp = client.get(
                                make_url(TEAM_BY_ID_URL, team_id=v[M_ID])
                            )

                            has = BaseTestCase.has_permission(
                                permissions, GET_TEAM_PERMISSION)
                            expect = Expect.SUCCESS if has else Expect.FAILURE
                            if has:
                                http_status = HTTPStatus.OK
                            else:
                                http_status = HTTPStatus.UNAUTHORIZED

                            expected = v.to_dict() \
                                if isinstance(v, EqualDataMixin) else v
                            self.assert_team_resp(
                                expect, resp, expected,
                                http_status=http_status)

    def test_get_team_by_invalid_id(self):
        """
        Test get team by an invalid id.
        """
        with self.client as client:
            for user in UserType:
                permissions = self.set_permissions(user)

                with self.subTest(user=user):
                    resp = client.get(
                        make_url(TEAM_BY_ID_URL, team_id=1000)
                    )

                    has = BaseTestCase.has_permission(
                        permissions, GET_TEAM_PERMISSION)
                    if has:
                        http_status = HTTPStatus.NOT_FOUND
                    else:
                        http_status = HTTPStatus.UNAUTHORIZED

                    self.assert_response_status_code(http_status,
                                                     resp.status_code)

                    self.assertTrue(resp.is_json)
                    self.assert_error_response(resp.get_json(),
                                               http_status.value,
                                               http_status.phrase,
                                               MatchParam.CASE_INSENSITIVE,
                                               MatchParam.IN)

    def _create_team(self, expect: Expect, new_team: TeamData,
                     http_status: Union[HTTPStatus, range] = HTTPStatus.OK,
                     tag: str = None):
        """
        Create a team
        :param expect:   Expected result; one of Expect.SUCCESS or
                         Expect.FAILURE
        :param new_team: team to create
        """
        result = None
        new_team_dict = new_team.to_dict(ignore=[M_ID])
        msg = f'{new_team} {tag if tag is not None else ""}'
        with self.client as client:
            resp = client.post(TEAMS_URL, json=new_team_dict)

            resp_body = json.loads(resp.data)
            if expect == Expect.SUCCESS:
                self.assert_created(resp.status_code, msg=msg)
                self.assert_success_response(resp_body, msg=msg)
                self.assert_body_entry(resp_body, RESULT_CREATED_COUNT,
                                       MatchParam.EQUAL, value=1, msg=msg)
                self.assertTrue(RESULT_ONE_TEAM in resp_body.keys(), msg=msg)
                self.assert_equal_dict(
                    new_team_dict, resp_body[RESULT_ONE_TEAM], ignore=[M_ID])
                result = resp_body[RESULT_ONE_TEAM]
            else:
                if isinstance(http_status, HTTPStatus):
                    self.assert_response_status_code(http_status,
                                                     resp.status_code, msg=msg)

                self.assertTrue(resp.is_json)
                self.assert_error_response(resp_body, http_status, '',
                                           MatchParam.IGNORE, msg=msg)
        return result

    def test_create_team(self):
        """ Test create a team """
        for user in UserType:
            with self.subTest(user=user):
                permissions = self.set_permissions(user)

                new_team = TeamData(0, f"Bueno {user} FC")

                has = BaseTestCase.has_permission(
                    permissions, POST_TEAM_PERMISSION)
                expect = Expect.SUCCESS if has else Expect.FAILURE
                if has:
                    http_status = HTTPStatus.OK
                else:
                    http_status = HTTPStatus.UNAUTHORIZED

                self._create_team(expect, new_team, http_status=http_status)

    def test_create_invalid_team(self):
        """ Test creating an invalid team """
        # Only managers can create teams.
        self.set_permissions(UserType.MANAGER)

        bad_team = TeamData(0, "")

        for index in range(1):
            with self.subTest(index=index):
                if index == 0:
                    bad_team.name = "   "
                    info = "empty name"
                else:
                    self.fail(f"Unexpected index {index}")

                self._create_team(Expect.FAILURE, bad_team,
                                  http_status=range(400, 500),
                                  tag=f'index {index} {info}')

    def test_create_duplicate_team(self):
        """ Test creating a duplicate team """
        # Only managers can create teams.
        self.set_permissions(UserType.MANAGER)

        new_team = TeamData(0, "The one and only")
        self._create_team(Expect.SUCCESS, new_team, http_status=HTTPStatus.OK)
        self._create_team(Expect.FAILURE, new_team, http_status=range(400, 500))

    def _delete_team(self, expect: Expect, team_id: int,
                     http_status: Union[HTTPStatus, range] = HTTPStatus.OK):
        """
        Create a team
        :param expect:   Expected result; one of Expect.SUCCESS or
                         Expect.FAILURE
        """
        msg = f'team_id {team_id}'
        with self.client as client:
            resp = client.delete(
                make_url(TEAM_BY_ID_URL, team_id=team_id)
            )

            resp_body = json.loads(resp.data)
            if expect == Expect.SUCCESS:
                self.assert_ok(resp.status_code, msg=msg)
                self.assert_success_response(resp_body, msg=msg)
                self.assert_body_entry(resp_body, RESULT_DELETED_COUNT,
                                       MatchParam.EQUAL, value=1, msg=msg)
            else:
                if isinstance(http_status, HTTPStatus):
                    self.assert_response_status_code(http_status,
                                                     resp.status_code, msg=msg)

                self.assertTrue(resp.is_json)
                self.assert_error_response(resp_body, http_status, '',
                                           MatchParam.IGNORE, msg=msg)

    def test_delete_team(self):
        """ Test deleting a team """
        # Only managers can create teams.
        self.set_permissions(UserType.MANAGER)

        new_team = TeamData(0, "Short lived")
        created_team = self._create_team(Expect.SUCCESS, new_team,
                                         http_status=HTTPStatus.OK)

        for user in UserType:
            with self.subTest(user=user):
                permissions = self.set_permissions(user)

                has = BaseTestCase.has_permission(
                    permissions, POST_TEAM_PERMISSION)
                expect = Expect.SUCCESS if has else Expect.FAILURE
                if has:
                    http_status = HTTPStatus.OK
                else:
                    http_status = HTTPStatus.UNAUTHORIZED

                self._delete_team(expect, created_team[M_ID],
                                  http_status=http_status)

    def test_delete_invalid_team(self):
        """ Test deleting an invalid team """
        # Only managers can delete teams.
        self.set_permissions(UserType.MANAGER)

        self._delete_team(Expect.FAILURE, 1000,
                          http_status=HTTPStatus.NOT_FOUND)

    def _update_team(self, expect: Expect, team_id: int, old_team: dict,
                     new_team: dict, tag: str,
                     http_status: Union[HTTPStatus, range] = HTTPStatus.OK):
        """
        Create a team
        :param expect:   Expected result; one of Expect.SUCCESS or
                         Expect.FAILURE
        """
        updates = {}
        for k, v in old_team.items():
            new_v = new_team[k]
            if new_v != v:
                updates[k] = new_v

        msg = f'team_id {team_id} {old_team} -> {new_team} = {updates} ' \
              f'{tag if tag is not None else ""}'
        with self.client as client:
            resp = client.patch(
                make_url(TEAM_BY_ID_URL, team_id=team_id), json=updates
            )

            resp_body = json.loads(resp.data)
            if expect == Expect.SUCCESS:
                self.assert_ok(resp.status_code, msg=msg)
                self.assert_success_response(resp_body, msg=msg)
                self.assert_body_entry(resp_body, RESULT_UPDATED_COUNT,
                                       MatchParam.EQUAL, value=1, msg=msg)
                self.assert_body_entry(resp_body, RESULT_ONE_TEAM,
                                       MatchParam.EQUAL, value=new_team,
                                       msg=msg)
            else:
                if isinstance(http_status, HTTPStatus):
                    self.assert_response_status_code(http_status,
                                                     resp.status_code, msg=msg)

                self.assertTrue(resp.is_json)
                self.assert_error_response(resp_body, http_status, '',
                                           MatchParam.IGNORE, msg=msg)

    def test_update_team(self):
        """ Test updating a team """
        # Only managers can create teams.
        self.set_permissions(UserType.MANAGER)

        new_team = TeamData(0, "Change can be swift")
        created_team = self._create_team(Expect.SUCCESS, new_team,
                                         http_status=HTTPStatus.OK)

        for index in range(1):
            with self.subTest(index=index):
                if index == 0:
                    # Change name
                    old_team = created_team
                    new_team = deepcopy(old_team)
                    new_team[M_NAME] = f"{new_team[M_NAME]} and sudden"
                    tag = "name"
                else:
                    self.fail(f"Unexpected index {index}")

                for user in UserType:
                    if index > 0 and user != UserType.MANAGER:
                        # Only manager's can update, so only need to test
                        # others once.
                        continue

                    with self.subTest(user=user):
                        permissions = self.set_permissions(user)

                        has = BaseTestCase.has_permission(
                            permissions, POST_TEAM_PERMISSION)
                        expect = Expect.SUCCESS if has else Expect.FAILURE
                        if has:
                            http_status = HTTPStatus.OK
                        else:
                            http_status = HTTPStatus.UNAUTHORIZED

                        self._update_team(expect, created_team[M_ID],
                                          old_team, new_team, tag,
                                          http_status=http_status)

    def test_update_team_invalid(self):
        """ Test updating a team """
        # Only managers can create and update teams.
        self.set_permissions(UserType.MANAGER)

        new_team = TeamData(0, "Change can be hard")
        created_team = self._create_team(Expect.SUCCESS, new_team,
                                         http_status=HTTPStatus.OK)

        for index in range(1):
            with self.subTest(index=index):
                if index == 0:
                    # Empty name
                    old_team = created_team
                    new_team = deepcopy(old_team)
                    new_team[M_NAME] = "  "
                    tag = "empty name"
                else:
                    self.fail(f"Unexpected index {index}")

                self._update_team(Expect.FAILURE, created_team[M_ID],
                                  old_team, new_team, tag,
                                  http_status=range(400, 500))

# Make the tests conveniently executable
if __name__ == "__main__":
    unittest.main()


from fastapi import APIRouter, Response, HTTPException, status

from models import ProductSchema
from controllers import read_products, read_category, write_product, read_product, purge_product

from datetime import date

router = APIRouter()

@router.get('/', response_model=list[ProductSchema])
async def get_products(read_all: bool, sorting_by_price_from_exp_to_cheap: bool = None,
                       select_by_date: date = None, select_by_category: str = None):
    try:
        return await read_products(read_all=read_all, sorting_by_price_from_exp_to_cheap=sorting_by_price_from_exp_to_cheap,
                                   select_by_date=select_by_date, select_by_category=select_by_category)
    except BaseException:
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)

@router.post('/', response_model=ProductSchema)
async def add_product(name: str, price: float, category_name: str):
    check_category = await read_category(name=category_name)
    if check_category:
        return await write_product(name=name, price=price, category=check_category)
    raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail='Category does not exist')

@router.delete('/')
async def delete_product(product_id: int):
    check_product = await read_product(product_id=product_id)
    if check_product:
        await purge_product(product_id=product_id)
        return Response(status_code=status.HTTP_200_OK)
    raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail='Product does not exist')


#스택/큐
#올바른 괄호
from collections import deque
def solution(s):
    answer = 0
    que = deque(s)
    while que:
        k = que.popleft()
        if answer == -1:
            return False
        if k == '(':
            answer += 1
        else:
            answer -= 1

    if answer == 0:
        return True
    else:
        return False

프로그래머스/파이썬/레벨2/올바른 괄호.py


import logging
import time
from datetime import timedelta

import numpy as np
from tqdm import tqdm

import src.cfg as cfg
import src.data as data
import src.models as models
import src.processing as processing
import src.filters as fltr

"""
parameters = dict(years_train=list(range(2010, 2014)),
                  years_test=[2014, 2015],
                  X_vars=['ice_conc', 'icethic_cea', 'tair', 'vosaline', 'radlw'],
                  y_var='thick_cr2smos',
                  bounds=[0, 400, 0, 400],
                  step=[20, 20]
                  )
"""
parameters = dict(years_train=list(range(2010, 2012)),
                  years_test=[2014],
                  X_vars=['icethic_cea', 'vosaline', 'radlw'],
                  y_var='thick_cr2smos',
                  bounds=[0, 400, 0, 400],
                  step=[20, 20]
                  )

reg_params = dict(model='lasso',
                  dx=5,
                  dy=5,
                  dt=5
                  )
filters = dict(partial_pca=5)

logging.basicConfig(format='%(asctime)s - %(message)s',
                    level=logging.INFO)
logging.getLogger('suds').setLevel(logging.INFO)  # set INFO level for all modules

logging.info('Loading test and train data...')
y_arr_train, X_arr_train = data.load_features(parameters['y_var'],
                                              parameters['X_vars'],
                                              parameters['years_train'])

y_arr_test, X_arr_test = data.load_features(parameters['y_var'],
                                            parameters['X_vars'],
                                            parameters['years_test'])
mask = np.load(cfg.mask_path, allow_pickle=True)
logging.info('Data is loaded')

bounds = parameters['bounds']
step = parameters['step']
indices = processing.gen_indices(bounds=bounds, step=step, mask=mask)
start = time.time()
logging.info('{} points'.format(len(indices)))
logging.info('Starting iterating')

ft = fltr.FeatureTable(dx=reg_params['dx'],
                       dy=reg_params['dy'],
                       dt=reg_params['dt'])

res = []
for idx, point in tqdm(enumerate(indices), total=len(indices)):

    y_train = y_arr_train[:, point[0], point[1]]
    y_test = y_arr_test[:, point[0], point[1]]
    X_train = ft.gen_matrix(data=X_arr_train, x=point[0], y=point[1], filters=filters)
    X_test = ft.gen_matrix(data=X_arr_test, x=point[0], y=point[1], filters=filters)
    if idx == 0:
        lstm = models.MyLSTM()
        lstm.fit(X_train, y_train)
    res.append(lstm.predict(X_test))

template = np.empty_like(y_arr_test)
template[:] = np.nan
out = processing.restore_array(template=template, array_in=res, indices=indices)

for idx, val in enumerate(indices):
    (i, j) = indices[idx]
elapsed = (time.time() - start)

logging.info('Processed {} points in {} ({} points/sec)'.format(len(indices),
                                                                str(timedelta(seconds=elapsed)),
                                                                round(len(indices) / elapsed), 5))


import json
import logging

from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework import status
from rest_framework.response import Response

from .tasks import fetch_fastq

logger = logging.getLogger(__name__)

@csrf_exempt
def test(request):
    return HttpResponse("Data Collection API Test Page.")

@csrf_exempt
def handle_request(request):
    logger.info('Received request.')

    if request.method != 'POST':
        logger.info('Received invalid request.')
        return JsonResponse({'task_id': '', 'error': 'Request must be a POST request.'})

    request_body = json.loads(request.body)

    run_id = request_body.get('run_id', None)

    if run_id is None:
        print('Invalid run ID.')
        logger.error(f'No run id found!')
        return JsonResponse({'task_id': '', 'error': 'No run id found!'})

    logger.info(f'Run id is {run_id}')
    print(f'Run ID: {run_id}')

    try:
        worker = fetch_fastq.delay(run_id)
    except Exception as e:
        print(str(e))
        logger.error(str(e))
        return JsonResponse({'task_id': '', 'error': 'Error in fetching the fastq files.'})

    res = JsonResponse({'task_id': worker.task_id})

    res.headers['Access-Control-Allow-Origin'] = '*'

    return res


from flask import *
import requests, subprocess, json, re

app = Flask(__name__)

@app.route("/")
async def start():
    return """
    <form action="/get">
        <input name="username" id="username">
        <label for="username">Username</label>
    </form>
    """

@app.route("/get")
async def getRender():
    id = request.args.get("username") or abort(400)
    video_list = requests.get(f"https://recoverplays.tv/GetUserInfo.php?username={id}&sort=date").json()
    video_ids = []
    for video in video_list[1]:
        #rawdll = requests.get(f"https://web.archive.org/web/20191210043532/https://plays.tv/video/{id}").text
        #matched_lines = [line for line in rawdll.split('\n') if "720.mp4" in line]
        #assert len(matched_lines) == 1
        #p = Popen(["grep", "-Po", 'src="\k[^"]+/720\.mp4(?=")'], stdin=PIPE, stdout=PIPE)
        #dll = p.communicate(input=matched_lines[0])[0]
        video_ids.append({'id': video['id'], 'title': f"{video['name']}, {video['date']}"})
    return render_template("render.html", ids=video_ids, len=len)

@app.route("/get/<videoid>")
async def get(videoid):
    rawdll = requests.get(f"http://web.archive.org/web/20191210043532id_/https://plays.tv/video/{videoid}").text
    matched_lines = [line for line in rawdll.split('\n') if "720.mp4" in line]
    assert len(matched_lines) == 1
    dll = re.sub(
           "^//",
           "https://",
           re.search('src=\"([^\"]+/720\.mp4)(?=\")', matched_lines[0]).group(1)
    )
    return {"lien": dll, "isOk": True}


# -*- coding: utf-8 -*-
"""
Created on Mon Nov  2 23:15:08 2020

@author: mfb
"""
# Tour de Hanoï

# Création d'une pile sans taille définie
def CreaStack():
    P = []                                # Création de la Pile en mémoire
    return(P)

# Empilage par la méthode '.append()', sans contrôle de taille de pile
def Empiler(Pile,Val):
    Pile.append(Val)

def PileVide(Pile):
    return(Pile == [])

def Depiler(Pile):
    return(Pile.pop())

#                 Pile 1 Pile 3  Pile 2
def TourDeHanoi(n,depart,arrivee,inter):
    global cmpt
    if(n != 0):
        TourDeHanoi(n-1,depart,inter,arrivee)
        Empiler(arrivee,Depiler(depart))
        TourDeHanoi(n-1,inter,arrivee,depart)
        cmpt = cmpt +1
    return

# Création des 3 piles vides
P1 = CreaStack()
P2 = CreaStack()
P3 = CreaStack()

NbDisques = int(input("Saisir le nombre de disques de la tour de départ : "))

# Initialisation de la pile de départ P1, soit le nombre de disques au départ
for i in range(NbDisques,0,-1):
    Empiler(P1,i)
cmpt = 0
print(P1,P2,P3)

TourDeHanoi(NbDisques,P1,P3,P2)
print(P1,P2,P3)
print(cmpt)


r"""Preprocess code.org data into a form that we can handle."""

import os
import pickle
import getpass
import numpy as np

USER = getpass.getuser()
DATA_ROOT = '/mnt/fs5/{}/generative-grading/data/real/education/'.format(USER)
ZIPF_CLASS = {'head': 0, 'body': 1, 'tail': 2}

def load_data(problem_name):
    problem_id = int(problem_name.split('_')[0].replace('codeorg', ''))
    root = '/mnt/fs5/{}/datasets/code_org_public_dataset_v1/data/p{}'.format(USER, problem_id)
    programs_file = os.path.join(root, 'programs-{}.pickle'.format(problem_id))
    counts_file = os.path.join(root, 'countMap-{}.pickle'.format(problem_id))
    annotations_file = os.path.join(root, 'annotations-{}.pickle'.format(problem_id))

    with open(programs_file, 'rb') as fp:
        programs = pickle.load(fp)

    with open(counts_file, 'rb') as fp:
        counts = pickle.load(fp)

    program2count = {}
    for _id in programs.keys():
        program2count[programs[_id]] = counts[_id]

    ranks = build_rank_map_from_count_map(program2count)
    zipfs = build_zipf_map(program2count, ranks)

    with open(annotations_file, 'rb') as fp:
        annotations = pickle.load(fp)

    countMap = {}
    labelMap = {}
    zipfMap = {}  # program to head/body/tail

    for _id, labels in annotations.items():
        program = programs[_id]
        countMap[program] = counts[_id]
        labelMap[program] = annotations[_id]
        zipfMap[program] = zipfs[program]

    return countMap, labelMap, zipfMap

def build_rank_map_from_count_map(count_map):
    programs = np.array(list(count_map.keys()))
    counts = np.array(list(count_map.values()))
    # NOTE: need to reverse b/c most common = rank 0
    ranks = np.argsort(counts)[::-1]
    programs = np.array(programs)
    programs = programs[ranks]
    return dict(zip(programs, np.arange(len(programs))))

def build_zipf_map(count_map, rank_map):
    programs = np.array(list(count_map.keys()))
    counts = np.array(list(count_map.values()))
    ranks = np.array(list(rank_map.values()))

    # by default, everything is a body
    zipf_classes = np.ones(len(programs)) * ZIPF_CLASS['body']
    # every with less than 3 counts is in the tail
    zipf_classes[counts < 3] = ZIPF_CLASS['tail']
    # every in top 20 is in the head
    zipf_classes[ranks <= 20] = ZIPF_CLASS['head']

    return dict(zip(programs, zipf_classes))

def save_data(out_dir, counts, labels, zipfs):
    with open(os.path.join(out_dir, 'counts.pkl'), 'wb') as fp:
        pickle.dump(counts, fp)

    with open(os.path.join(out_dir, 'labels.pkl'), 'wb') as fp:
        pickle.dump(labels, fp)

    with open(os.path.join(out_dir, 'zipfs.pkl'), 'wb') as fp:
        pickle.dump(zipfs, fp)

if __name__ == '__main__':
    import argparse
    arg_parser = argparse.ArgumentParser()
    arg_parser.add_argument(
        'out_dir',
        type=str,
        help='where to dump raw data')
    arg_parser.add_argument(
        'problem',
        type=str,
        help='codeorg1|codeorg9')
    args = arg_parser.parse_args()

    if not os.path.isdir(args.out_dir):
        os.makedirs(args.out_dir)

    counts, labels, zipfs = load_data(args.problem)
    save_data(args.out_dir, counts, labels, zipfs)


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

import base64
import json

from odoo import fields, models, api

from ..utils.utils import make_signature

class PaymentAcquirer(models.Model):
    _inherit = 'payment.acquirer'

    provider = fields.Selection(selection_add=[('liqpay', 'Liqpay')])
    liqpay_public_key = fields.Char(
        'Liqpay Public Key', required_if_provider='liqpay')
    liqpay_private_key = fields.Char(
        'Liqpay Private Key', required_if_provider='liqpay')
    liqpay_base_url = fields.Char(
        'Base callback url', required_if_provider='liqpay')
    liqpay_server_side =  fields.Char(
        default='https://www.liqpay.ua/api/request',
        help="If you don't sure don't change it",
        string='Server Side', required_if_provider='liqpay')
    liqpay_client_side =  fields.Char(
        default='https://www.liqpay.ua/api/3/checkout',
        help="If you don't sure don't change it",
        string='Client Side', required_if_provider='liqpay')

    def liqpay_form_generate_values(self, values):
        """
            Method to generate values for liqpay acquirer payment
        :param values: values to generate payment from
        :return: updated values dict with:
                liqpay_data - request data in json encoded to base64
                liqpay_signature - {private_key}{data_in_base64}{private_key}
                signature for sending to liqpay for transactions verification
        """

        self.ensure_one()

        base_url = self.liqpay_base_url
        return_url = '{}/shop/confirmation'.format(base_url)
        callback_url = '{}/liqpay/callback'.format(base_url)
        partner_lang = 'ru'

        # Fill in request data for liqpay transaction
        request = {
          'version': '3',
          'public_key': self.liqpay_public_key,
          'action': 'pay',
          'amount': values['amount'],
          'currency': values['currency'] and values['currency'].name or 'UAH',
          'description': 'Order payment: {}'.format(values['reference']),
          'order_id': values['reference'],
          'language': partner_lang,
          'sandbox': '1' if self.environment == 'test' else '',
          'server_url': callback_url,
          'result_url': return_url,
          'sender_first_name': values['billing_partner_name'] or '',
          'sender_city': values['billing_partner_city'] or '',
          'sender_address': values['billing_partner_address'] or '',
          'sender_postal_code': values['billing_partner_zip'] or '',
        }

        # Dump request data to json and encode it to base64
        data = base64.b64encode(json.dumps(request))

        # Make signature for current transaction
        signature = make_signature(
            self.liqpay_private_key, data, self.liqpay_private_key)

        values.update({
            'liqpay_data': data,
            'liqpay_signature': signature,
        })
        return values

    def liqpay_get_form_action_url(self):
        """
        :return: LiqPay checkout API method route
        """
        return self.liqpay_client_side

    @api.multi
    def show_liqpay_journal(self):
        """
        :return: Window action to open liqpay_journal view
        """
        tree_view_id = self.env.ref(
            'liqpay_acquirer.liqpay_journal_tree_view').id
        form_view_id = self.env.ref(
            'liqpay_acquirer.liqpay_journal_form_view').id

        return {
            'name': 'LiqPay journal',
            'view_type': 'form',
            'view_mode': 'tree, form',
            'views': [(tree_view_id, 'tree'), (form_view_id, 'form')],
            'res_model': 'liqpay.journal',
            'type': 'ir.actions.act_window',
            'context': self.env.context,
        }


from typing import Dict
from tqdm import tqdm

from towhee.utils.log import engine_log
from towhee.runtime import ops, AcceleratorConf
from towhee.operator import NNOperator
from towhee.serve.triton import constant
from towhee.serve.triton.model_to_triton import ModelToTriton
from towhee.serve.triton.pipe_to_triton import PipeToTriton

class Builder:
    """
    Build triton models from towhee pipeline.
    """

    def __init__(self, dag_repr: 'DAGRepr', model_root: str, conf: Dict):
        self.dag_repr = dag_repr
        self._server_conf = conf
        self._model_root = model_root

    @staticmethod
    def _load_op(op_name: str, init_args: tuple, init_kws: Dict) -> 'Operator':
        """
        load operator
        """
        name = op_name.split('/')
        if len(name) == 2:
            hub_id, name = name[0], name[1]
        else:
            hub_id, name = 'towhee', name[0]
        try:
            hub = getattr(ops, hub_id)
            init_args = () if init_args is None else init_args
            init_kws = {} if init_kws is None else init_kws
            return getattr(hub, name)(*init_args, **init_kws).get_op()
        except Exception as e:  # pylint: disable=broad-except
            engine_log.error('Load operator: %s by init args: [%s] and kws: [%s] failed, error: %s', op_name, init_args, init_kws, e)
            return None

    def _model_convert(self, node_repr: 'NodeRepr'):
        op_name = node_repr.op_info.operator
        op_init_args = node_repr.op_info.init_args
        op_init_kws = node_repr.op_info.init_kws
        op = self._load_op(op_name, op_init_args, op_init_kws)
        if op is None:
            return False

        if isinstance(op, NNOperator):
            if not hasattr(op, 'supported_formats'):
                return True
            model_name = node_repr.name.replace('/', '.')
            converter = ModelToTriton(self._model_root, op, model_name, node_repr.config, self._server_conf)
            status = converter.to_triton()
            if status == -1:
                return False
            elif status == 0:
                return True
            inputs, outputs = converter.get_model_in_out()
            acc_conf = {'model_name': model_name, 'inputs': inputs, 'outputs': outputs}
            node_repr.config.acc_info = AcceleratorConf(type='triton', params=acc_conf)
        return True

    def build(self) -> bool:
        progress_bar = tqdm(total=len(self.dag_repr.nodes)+1, desc='Building model and pipeline')
        for node_id, node in self.dag_repr.nodes.items():
            if node_id in ['_input', '_output'] or node.op_info.type != 'hub':
                progress_bar.update(1)
                continue
            if not self._model_convert(node):
                return False
            progress_bar.update(1)
        status = PipeToTriton(self.dag_repr, self._model_root, constant.PIPELINE_NAME, self._server_conf).process()
        progress_bar.update(1)
        return status

# pylint: disable=import-outside-toplevel
def main():
    from towhee.utils.thirdparty.dill_util import dill as pickle
    import sys
    import json
    if len(sys.argv) != 4:
        engine_log.error('The args is invalid, it must has three parameters in [dag_file_path, mode_root_dir, config_file_path]')
        sys.exit(-1)

    dag_file, model_root, config_file = sys.argv[1], sys.argv[2], sys.argv[3]
    with open(dag_file, 'rb') as f_dag, open(config_file, 'rb') as f_conf:
        dag_repr = pickle.load(f_dag)
        config = json.load(f_conf)
        if not Builder(dag_repr, model_root, config).build():
            sys.exit(-1)
    sys.exit(0)

if __name__ == '__main__':
    main()


#!/usr/bin/env python3
import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)

ports = (31, 32, 35, 36)

for port in ports:
    GPIO.setup(port, GPIO.OUT)
    GPIO.output(port, GPIO.LOW)
    time.sleep(0.2)

GPIO.cleanup()


import csv
import os
import re
import sys

def path_select():
    pth = sys.argv[1]
    return pth
    # argv로 인자 받기, class에선 이 방법을 사용할 수 없다

def search_files(pth):
    sep_path = []
    path_group = {}
    for (root_dirs, _, file_dirs) in os.walk(pth):
        for fil in file_dirs:
            # file_dirs는 root_dirs 안에 있는 모든 파일들, fil은 그 개별의 파일이다
            full_path = os.path.join(root_dirs, fil)
            # root_dirs와 fil을 합치면 fil의 절대 경로가 나온다
            if root_dirs not in path_group:
                path_group[root_dirs] = []
                sep_path.append(root_dirs)
                # root_dir이 앞에서 선언한 path_group 딕셔너리에 없다면 root_dir을 딕셔너리 key 값으로 생성한다
                # 모든 파일을 체크하지만 root_dir이 같다면 딕셔너리에 추가 되지 않는다
                # sep_path에 파일 경로를 따로 저장한다
            path_group[root_dirs].append(full_path)
            # path_group[root_dirs]의 value 값으로 full_path를 넣는다
            # 바로 직전에 root_dirs를 생성하지 않았어도 이미 이전에 생성했던 같은 경로의 key가 있으니 그 안에 full_path를 넣으면 된다
    return path_group, sep_path

def write_files(path_group, sep_path):
    csv_path = os.path.expanduser('~/final_test.csv')
    # csv 파일이 저장될 경로 설정, os의 홈 폴더에 생성하게 한다
    with open(csv_path, 'w') as f:
        i = 0
        writer = csv.writer(f)
        writer.writerow(
            ["#", "Project Name", "Sequence Name", "Shot Name", "Version", "Start Frame", "Final Frame", "Total Frames",
             "Path"])
        for pth in sep_path:
            i += 1
            # i는 인덱싱을 하기 위한 숫자다
            sep = pth.split(os.sep)
            # 각 파일이 들어있는 경로인 key를 리스트로 분리해준다
            frame_grp = []
            for n in path_group[pth]:
                frame_num = re.search('[0-9]{4}[.]', n)
                frame_num = n[frame_num.span()[0]:frame_num.span()[1] - 1]
                frame_grp.append(frame_num)
            frame_grp = sorted(frame_grp)
            first_frame = frame_grp[0]
            final_frame = frame_grp[-1]
            # 각 key 값의 value 리스트 마다 프레임 넘버를 뽑아 새로운 리스트에 넣어주고, 그 리스트를 정렬해 스타트/엔드 프레임을 찾는다
            fileseq = path_group[pth][0]
            # value값은 리스트 안에 여러개 있지만, 그 값은 프레임 넘버 빼고 전부 동일하니까 아무거나 가져와도 된다
            numm = re.search('[0-9]{4}[.]', fileseq)
            numm = fileseq[numm.span()[0]:numm.span()[1] - 1]
            fileseq = fileseq.replace(numm, '####')
            pathname = pth + fileseq
            # 가져온 value 값에서 프레임 넘버를 뽑아서 ####로 변경해준다, 그 뒤 key값과 합쳐서 풀 패스 경로를 만든다
            writer.writerow([i, sep[4], sep[6], sep[7], sep[9], first_frame, final_frame, len(frame_grp), pathname])

def main():
    pth = path_select()
    path_group, sep_path = search_files(pth)
    write_files(path_group, sep_path)

if __name__ == "__main__":
    main()

def search_files_old(pth):
    sep_path = []
    for (rots, _, fils) in os.walk(pth):
        for fil in fils:
            full_path = os.path.join(rots, fil)
            exact_path = full_path
            full_path = full_path[len(pth):-len(fil) - 1]
            numm = re.search('[0-9]{4}[.]', fil)
            full_path = full_path + os.sep + fil[numm.span()[0]:numm.span()[1] - 1]
            mid_path = full_path.split(os.sep)
            mid_path.append(exact_path)

            sep_path.append(mid_path)
    print(sep_path)
    return sep_path


import os
import glob
import re
from datetime import datetime, timedelta, time

import fitz
from openpyxl import Workbook

from dataclasses import dataclass

# ------ CHANGE ME ---------
folder_path = "C:\\Users\\majona\\GitHub\\iko-tools\\Python\\antwerpTimeConverter\\input"
save_path = f'C:\\Users\\majona\\GitHub\\iko-tools\\Python\\antwerpTimeConverter\\{datetime.now().strftime("%d-%m-%Y_%H-%M-%S")}.xlsx'
year = '2023'

@dataclass
class person:
    include: bool = False
    laborcode: str = "blank"

# add more Nummer to personid codes as necessary
people = {
    "21592": person(True, "BEANJBRU"),
    "21616": person(True, "BEANDCAR"),
    "21612": person(True, "BEANDHOS"),
    "21545": person(True, "BEANPJOR"),
    "21263": person(True, "BEANBMAE"),
    "21368": person(True, "BEANPMAE"),
    "21457": person(True, "BEANAMAR"),
    "21547": person(True, "BEANNPAT"),
    "20746": person(True, "BEANDROO"),
    "21328": person(True, "BEANPVHA"),
    "20889": person(True, "BEANBVRI"),
    "21193": person(True, "BEANBWIN"),
}

converted_wb = Workbook()
converted_ws = converted_wb.create_sheet("Sheet1", 0)
converted_ws.append(
    [
        "Empl#",
        "Employee Name",
        "Day",
        "Time In",
        "Time Out",
        "Hours",
        "LL1",
        "LL2",
        "LL3",
        "LL4",
        "LL5",
        "LL6",
        "LL7",
        "Reg",
        "O/T",
    ]
)

date_regex = r"^\d{2}/\d{2}/\d{4}"
time_regex = r"\b\d{1,2}:\d{2}\b"

for filepath in glob.glob(os.path.join(folder_path, "*.pdf")):
    doc = fitz.open(filepath)
    print(f'Reading: {filepath}')
    employee = person()
    for page in doc:
        # the blocks option seems to give best result where lines are kept together
        text = page.get_text('blocks')
        # print(text)
        for row in text:
            # see if employee is defined if the row is a employee definition
            if (row[4].find('Nummer') > 0):
                person_num = row[4][row[4].find('Nummer') + 7:row[4].find('Badge')]
                # print(person_num)
                employee = people.get(person_num.strip())
                if (not(employee.include)):
                    print(f'Not Found: {person_num.strip()}')
                continue
            if (employee.include):
                match = re.findall(date_regex, row[4])
                if (match):
                    # print(match)
                    time_matches = re.findall(time_regex, row[4])
                    hours = row[4][row[4].find(time_matches[1])+len(time_matches[1]):].split('\n')
                    # print(hours)
                    start_time = datetime.strptime(f'{match[0]} {time_matches[0]}', "%d/%m/%Y %H:%M")
                    end_time = start_time + timedelta(hours=float(hours[1].replace(',','.')))
                    converted_ws.append(
                        [
                            person_num.strip(),
                            employee.laborcode,
                            start_time.strftime('%A').upper(),
                            start_time.strftime('%Y-%m-%d %H:%M'),
                            end_time.strftime('%Y-%m-%d %H:%M'),
                            hours[1].replace(',','.'),
                            None,
                            None,
                            None,
                            None,
                            None,
                            None,
                            None,
                            hours[1].replace(',','.'),
                            0,
                        ]
                    )
                else:
                    print(f'Skipping row: {row[4]}')
                    continue

converted_wb.save(save_path)


# two compartments per rucksack
# points for shared items

import string

def priority(letter: str) -> int:
    return (
        (26 if letter.isupper() else 0)
        + string.ascii_lowercase.index(letter.lower())
        + 1
    )

score = 0

with open("input.txt") as f:

    lines, i = [l.rstrip("\n") for l in f.readlines()], 0

    while i < len(lines):
        a, b, c = [set(j) for j in lines[i : i + 3]]

        common = a.intersection(b).intersection(c)
        score += priority(list(common)[0])
        i += 3

print(score)


import random
import time

class Attack:
    @staticmethod
    def attack_target(person, target):
        hit = random.choice(person.atk_chance)
        if hit:
            target.hp -= person.dmg
            print(f'{person.name} hit the {target.name} and deal {person.dmg}, now {target.name} has {target.hp} hp')
        else:
            print(f'{person.name} has missed')

        time.sleep(1)

    @staticmethod
    def attack_chance(percent):
        att = [False for i in range(10)]
        p = percent // 10
        for i in range(p):
            att[i] = True
        return att

    @staticmethod
    def fight(*persons):
        while 1:
            if persons[0].hp <= 0:
                print(f'{persons[1].name} win!')
                break
            Attack.attack_target(persons[0], persons[1])
            if persons[1].hp <= 0:
                print(f'{persons[0].name} win!')
                break
            Attack.attack_target(persons[1], persons[0])

class Warrior:
    def __init__(self, name):
        self.hp = 100
        self.dmg = 20
        self.name = name
        self.atk_chance = Attack.attack_chance(80)

bob = Warrior('Bob')
joe = Warrior('Joe')
Attack.fight(bob, joe)


import numpy as np

import src.main.Downlink as Dl
import src.main.Analyzer as An
import src.main.IO as IO

from pandas import DataFrame

points = 10
min_power = -1
max_power = 2

dn = 0.5

b_line = 0.0
t_line = (-0.36, 0.37)
q_line = 0.0
s_line = (-0.5, 0.0, 0.5)

opt = True

def set_power(total_points=points, power_min=min_power, power_max=max_power) -> bool:
    """setting global parameters"""
    global points, min_power, max_power
    try:
        points = total_points
        min_power = power_min
        max_power = power_max
        return True
    except TypeError:
        RuntimeError('set_power failed: unable to change parameters!')
        return False

def get_power() -> tuple:
    """getting power parameters"""
    return points, min_power, max_power

def get_lines() -> tuple:
    """getting boundary parameters"""
    return b_line, t_line, q_line, s_line

def set_distance(distance=dn) -> bool:
    """setting normalized distance"""
    global dn
    try:
        dn = distance
        return True
    except TypeError:
        RuntimeError('set_distance failed: unable to change parameters!')
        return False

def toggle_mode():
    """toggling operating mode"""
    global opt
    opt = not opt
    return

class DataGenerator:

    def __init__(self, antenna_count=128, user_count=10, length=4, factor=0, block_test=100000, block_train=100):
        """initializing data generator for downlink channel"""
        self.__antenna_count = antenna_count
        self.__user_count = user_count
        self.__length = length
        self.__factor = factor
        self.__block_test = block_test
        self.__block_train = block_train
        self.__model_train = Dl.DownlinkGenerator(antenna_count, user_count, length, block_train)
        self.__model_test = Dl.DownlinkGenerator(antenna_count, user_count, length, block_test)
        return

    def feature_test(self):
        """testing channels features"""
        data_points = self.__block_train * self.__user_count
        dl = Dl.DownlinkModule(self.__antenna_count, self.__user_count, self.__length, self.__block_train, None)
        decision_line = 0.0
        symbols = dl.get_symbol(mode='binary', line=decision_line)
        symbols = np.reshape(symbols, data_points)
        print()
        print('balanced symbols with line = ' + str(decision_line))
        print('--------------------------------------')
        print('symbols mean: ', end='')
        print(np.mean(symbols))
        print('symbols variance: ', end='')
        print(np.var(symbols))
        print('+1 symbol probability: ', end='')
        print(np.sum(symbols == +1) / data_points)
        print('0 symbol probability: ', end='')
        print(np.sum(symbols == 0) / data_points)
        print('--------------------------------------')
        decision_line = 0.4
        symbols = dl.get_symbol(mode='binary', line=decision_line)
        symbols = np.reshape(symbols, data_points)
        print()
        print('binary symbols with line = ' + str(decision_line))
        print('--------------------------------------')
        print('symbols mean: ', end='')
        print(np.mean(symbols))
        print('symbols variance: ', end='')
        print(np.var(symbols))
        print('0 symbol probability: ', end='')
        print(np.sum(symbols == 0) / data_points)
        print('1 symbol probability: ', end='')
        print(np.sum(symbols == 1) / data_points)
        decision_line = (-0.5, 0.5)
        symbols = dl.get_symbol(mode='ternary', line=decision_line)
        print('--------------------------------------')
        symbols = np.reshape(symbols, data_points)
        print()
        print('ternary symbols with line = ' + str(decision_line))
        print('--------------------------------------')
        print('symbols mean: ', end='')
        print(np.mean(symbols))
        print('symbols variance: ', end='')
        print(np.var(symbols))
        print('-1 symbol probability: ', end='')
        print(np.sum(symbols == -1) / data_points)
        print('0 symbol probability: ', end='')
        print(np.sum(symbols == 0) / data_points)
        print('+1 symbol probability: ', end='')
        print(np.sum(symbols == +1) / data_points)
        print('--------------------------------------')
        decision_line = 0.0
        symbols = dl.get_symbol(mode='quaternary', line=decision_line)
        print()
        print('quaternary symbols with line = ' + str(decision_line))
        print('--------------------------------------')
        print('symbols mean: ', end='')
        print(np.mean(symbols))
        print('symbols variance: ', end='')
        print(np.var(symbols))
        print('00 symbol probability: ', end='')
        print(np.sum((symbols[0] == 0) & (symbols[1] == 0)) / data_points)
        print('01 symbol probability: ', end='')
        print(np.sum((symbols[0] == 0) & (symbols[1] == 1)) / data_points)
        print('10 symbol probability: ', end='')
        print(np.sum((symbols[0] == 1) & (symbols[1] == 0)) / data_points)
        print('11 symbol probability: ', end='')
        print(np.sum((symbols[0] == 1) & (symbols[1] == 1)) / data_points)
        print('--------------------------------------')
        decision_line = (-0.5, 0.0, 0.5)
        symbols = dl.get_symbol(mode='sixteen', line=decision_line)
        print()
        print('sixteen symbols with line = ' + str(decision_line))
        print('--------------------------------------')
        print('symbols mean: ', end='')
        print(np.mean(symbols))
        print('symbols variance: ', end='')
        print(np.var(symbols))
        print('-3&-3 symbol probability: ', end='')
        print(np.sum((symbols[0] == -3) & (symbols[1] == -3)) / data_points)
        print('-3&-1 symbol probability: ', end='')
        print(np.sum((symbols[0] == -3) & (symbols[1] == -1)) / data_points)
        print('-3&+1 symbol probability: ', end='')
        print(np.sum((symbols[0] == -3) & (symbols[1] == +1)) / data_points)
        print('-3&+3 symbol probability: ', end='')
        print(np.sum((symbols[0] == -3) & (symbols[1] == +3)) / data_points)
        print('-1&-3 symbol probability: ', end='')
        print(np.sum((symbols[0] == -1) & (symbols[1] == -3)) / data_points)
        print('-1&-1 symbol probability: ', end='')
        print(np.sum((symbols[0] == -1) & (symbols[1] == -1)) / data_points)
        print('-1&+1 symbol probability: ', end='')
        print(np.sum((symbols[0] == -1) & (symbols[1] == +1)) / data_points)
        print('-1&+3 symbol probability: ', end='')
        print(np.sum((symbols[0] == -1) & (symbols[1] == +3)) / data_points)
        print('+1&-3 symbol probability: ', end='')
        print(np.sum((symbols[0] == +1) & (symbols[1] == -3)) / data_points)
        print('+1&-1 symbol probability: ', end='')
        print(np.sum((symbols[0] == +1) & (symbols[1] == -1)) / data_points)
        print('+1&+1 symbol probability: ', end='')
        print(np.sum((symbols[0] == +1) & (symbols[1] == +1)) / data_points)
        print('+1&+3 symbol probability: ', end='')
        print(np.sum((symbols[0] == +1) & (symbols[1] == +3)) / data_points)
        print('+3&-3 symbol probability: ', end='')
        print(np.sum((symbols[0] == +3) & (symbols[1] == -3)) / data_points)
        print('+3&-1 symbol probability: ', end='')
        print(np.sum((symbols[0] == +3) & (symbols[1] == -1)) / data_points)
        print('+3&+1 symbol probability: ', end='')
        print(np.sum((symbols[0] == +3) & (symbols[1] == +1)) / data_points)
        print('+3&+3 symbol probability: ', end='')
        print(np.sum((symbols[0] == +3) & (symbols[1] == +3)) / data_points)
        print('--------------------------------------')
        return

    def __get_comment_train(self, power: float) -> str:
        """generating comment for train data set"""
        text0 = 'Downlink'
        text1 = '\nMatched Filter'
        text2 = '\ncorrelation = NONE'
        text3 = '\nnumber of antennas = ' + str(self.__antenna_count)
        text4 = '\nnumber of users = ' + str(self.__user_count)
        text5 = '\nchannel length = ' + str(self.__length)
        text6 = '\ntransmission block length = ' + str(self.__block_train)
        text7 = '\n1 / (Noise Power) = ' + str(power)
        comment = text0 + text1 + text2 + text3 + text4 + text5 + text6 + text7
        return comment

    def __get_comment_test(self, power: float) -> str:
        """generating comment for test data set"""
        text0 = 'Downlink'
        text1 = '\nMatched Filter'
        text2 = '\ncorrelation = NONE'
        text3 = '\nnumber of antennas = ' + str(self.__antenna_count)
        text4 = '\nnumber of users = ' + str(self.__user_count)
        text5 = '\nchannel length = ' + str(self.__length)
        text6 = '\ntransmission block length = ' + str(self.__block_test)
        text7 = '\n1 / (Noise Power) = ' + str(power)
        comment = text0 + text1 + text2 + text3 + text4 + text5 + text6 + text7
        return comment

    def __create_train_data(self, power: float, mode: str):
        """creating data set to train models"""
        writer = IO.DataWriter()
        gen = self.__model_train
        if mode == 'binary':
            data_set = gen.record_data_set_binary(power=power, factors=self.__factor, line=b_line)
        elif mode == 'ternary':
            data_set = gen.record_data_set_ternary(power=power, factors=self.__factor, line=t_line)
        elif mode == 'quaternary':
            data_set = gen.record_data_set_quadrature(power=power, factors=self.__factor, line=q_line)
        else:
            data_set = gen.record_data_set_sixteen(power=power, factors=self.__factor, line=s_line)
        name = 'train_set'
        # comment = self.__get_comment_train(power=power)
        writer.frame_to_csv(data_set, name)
        # writer.comment_csv(comment, name)
        return

    def __create_test_data(self, powers, mode: str):
        """creating data set to test models"""
        writer = IO.DataWriter()
        gen = self.__model_test
        for power in powers:
            if mode == 'binary':
                data_set = gen.record_data_set_binary(power=power, factors=self.__factor, line=b_line)
            elif mode == 'ternary':
                data_set = gen.record_data_set_ternary(power=power, factors=self.__factor, line=t_line)
            elif mode == 'quaternary':
                data_set = gen.record_data_set_quadrature(power=power, factors=self.__factor, line=q_line)
            else:
                data_set = gen.record_data_set_sixteen(power=power, factors=self.__factor, line=s_line)
            name = 'test_' + ('%.2f' % power)
            # comment = self.__get_comment_test(power=power)
            writer.frame_to_csv(data_set, name)
            # writer.comment_csv(comment, name)
        return

    def run_channel_test(self, power: float):
        """recording channel runs"""
        gen = Dl.DownlinkGenerator(self.__antenna_count, self.__user_count, self.__length, self.__block_train)
        gen.data_run_test(power=power, factors=self.__factor)
        return

    def generate_train_data(self, mode: str, power=1.00):
        """running the channel model to obtain train data set"""
        self.__create_train_data(power=power, mode=mode)
        return

    def generate_test_data(self, mode: str):
        """running channel model to obtain test data set"""
        powers = np.logspace(min_power, max_power, points)
        self.__create_test_data(powers=powers, mode=mode)
        return

class TestLearner:

    def __init__(self, block_train=100):
        """initializing testing class"""
        self.__reader = IO.DataReader()
        self.__block_train = block_train
        return

    def __read_file(self, file: str, rows=None) -> DataFrame:
        """reading file"""
        data_set = self.__reader.read_data(data_name=file, rows=rows)
        return data_set

    def __get_data(self, file: str, labels=1, rows=None) -> tuple:
        """reading data set"""
        data_set = self.__read_file(file=file, rows=rows)
        if labels == 1:
            x_set = data_set.loc[:, data_set.columns == 'out']
            y_set = data_set.loc[:, data_set.columns == 'bit']
            y_set = y_set.values.flatten()
        else:
            x_set = data_set.loc[:, (data_set.columns != 'bit #1') & (data_set.columns != 'bit #2')]
            y_1 = data_set.loc[:, 'bit #1']
            y_2 = data_set.loc[:, 'bit #2']
            y_set = np.stack((y_1, y_2), axis=1)
        x_set = np.asarray(x_set)
        y_set = np.asarray(y_set)
        return x_set, y_set

    def __get_classifier(self, learner: str, file: str, bits: int, rows=None) -> An:
        """generating a linear learner"""
        if learner == 'ml':
            if bits == 2:
                analyzer = An.BinaryPredictor(line=b_line)
            elif bits == 3:
                x_set, y_set = self.__get_data(file=file, labels=1)
                gain = np.var(x_set) / np.var(y_set)
                analyzer = An.TernaryPredictor(line=t_line, gain=gain)
            elif bits == 4:
                analyzer = An.QuadraturePredictor(line=q_line)
            else:
                x_set, y_set = self.__get_data(file=file, labels=2)
                gain = np.mean(np.var(x_set, axis=0)) / np.mean(np.var(y_set, axis=0))
                analyzer = An.SixteenPredictor(line=s_line, gain=gain)
            return analyzer
        if learner == 'map':
            x_set, y_set = self.__get_data(file=file, labels=2)
            print('in __predictor_define -- ', end='')
            print('shape of y_set: ' + str(y_set.shape))
            total = y_set.shape[0]
            pos = float(np.sum(y_set > 0)) / total
            neg = float(np.sum(y_set < 0)) / total
            line = float(pos) / float(neg)
            analyzer = An.BinaryPredictor(line=line)
            return analyzer
        if learner == 'kmm':
            if bits == 2:
                analyzer = An.MeansClusterClassifier()
            elif bits == 4:
                analyzer = An.ParallelMeansClusterClassifier()
            else:
                raise Exception('learner=' + learner + ' does not accept ' + str(bits) + ' bits!')
            return analyzer
        if learner == 'acm':
            if bits == 2:
                analyzer = An.AgglomerativeClusterClassifier()
            elif bits == 4:
                analyzer = An.ParallelAgglomerativeClusterClassifier()
            else:
                raise Exception('learner=' + learner + ' does not accept ' + str(bits) + ' bits!')
            return analyzer
        if learner == 'gmm':
            if bits == 2:
                analyzer = An.GaussianMixtureClassifier()
            elif bits == 4:
                analyzer = An.ParallelGaussianMixtureClassifier()
            else:
                raise Exception('learner=' + learner + ' does not accept ' + str(bits) + ' bits!')
            return analyzer
        if bits == 4 or bits == 16:
            labels = 2
            if learner == 'bayes':
                analyzer = An.ParallelBayesClassifier()
            elif learner == 'logistic':
                analyzer = An.ParallelLogisticClassifier()
            elif learner == 'tree':
                analyzer = An.ParallelTreeClassifier()
            elif learner == 'forest':
                analyzer = An.ParallelForestClassifier()
            elif learner == 'vector':
                analyzer = An.ParallelVectorClassifier()
            elif learner == 'nearest':
                analyzer = An.ParallelNearestClassifier()
            else:
                raise Exception('learner=' + learner + ' is not defined!')
        else:
            labels = 1
            if learner == 'bayes':
                analyzer = An.BayesClassifier()
            elif learner == 'logistic':
                analyzer = An.LogisticClassifier()
            elif learner == 'tree':
                analyzer = An.TreeClassifier()
            elif learner == 'forest':
                analyzer = An.ForestClassifier()
            elif learner == 'vector':
                analyzer = An.VectorClassifier()
            elif learner == 'nearest':
                analyzer = An.NearestClassifier()
            else:
                raise Exception('learner=' + learner + ' is not defined!')
        x_set, y_set = self.__get_data(file=file, labels=labels, rows=rows)
        analyzer.fit(x_set, y_set)
        return analyzer

    def __test_classifier(self, analyzer: An, file: str, labels: int) -> tuple:
        """testing a blind predictor"""
        x_set, y_set = self.__get_data(file=file, labels=labels)
        score = analyzer.accuracy(x_set, y_set)
        return score, (1 - score)

    def get_error(self, learner: str, file: str, bits: int, labels: int, run: bool) -> np.ndarray:
        """getting error on learner"""
        if run:
            name = self.__get_classifier_identifier(learner=learner)
        else:
            name = None
        if learner == 'ml' or learner == 'map':
            return self.__error_blind(learner, file, bits, labels, name)
        if learner == 'gmm' or learner == 'kmm' or learner == 'acm':
            return self.__error_unsupervised(learner, file, bits, labels, name)
        return self.__error_supervised(learner, file, bits, labels, name)

    def __error_blind(self, learner: str, file: str, bits: int, labels: int, name: str) -> np.ndarray:
        """getting error on blind learner"""
        powers = np.logspace(min_power, max_power, points)
        error = np.ones(len(powers))
        for index in range(0, len(powers)):
            power = powers[index]
            test = file + 'test_' + ('%.2f' % power)
            model = self.__get_classifier(learner=learner, file=test, bits=bits)
            err = self.__test_classifier(analyzer=model, file=test, labels=labels)
            if name is not None:
                self.__print_info(name=name, power=power, error=err[1])
            error[index] = err[1]
        return error

    def __error_supervised(self, learner: str, file: str, bits: int, labels: int, name: str) -> np.ndarray:
        """getting error on supervised learner"""
        if opt:
            return self.__error__supervised(learner, file, bits, labels, name)
        powers = np.logspace(min_power, max_power, points)
        train = file + 'test_100.00'
        model = self.__get_classifier(learner=learner, file=train, bits=bits, rows=self.__block_train)
        error = np.ones(len(powers))
        for index in range(0, len(powers)):
            power = powers[index]
            test = file + 'test_' + ('%.2f' % power)
            err = self.__test_classifier(analyzer=model, file=test, labels=labels)
            if name is not None:
                self.__print_info(name=name, power=power, error=err[1])
            error[index] = err[1]
        return error

    def __error__supervised(self, learner: str, file: str, bits: int, labels: int, name: str) -> np.ndarray:
        """getting optimum error on supervised learner"""
        powers = np.logspace(min_power, max_power, points)
        error = np.ones(len(powers))
        for index in range(0, len(powers)):
            power = powers[index]
            test = file + 'test_' + ('%.2f' % power)
            model = self.__get_classifier(learner=learner, file=test, bits=bits, rows=self.__block_train)
            err = self.__test_classifier(analyzer=model, file=test, labels=labels)
            if name is not None:
                self.__print_info(name=name, power=power, error=err[1])
            error[index] = err[1]
        return error

    def __error_unsupervised(self, learner: str, file: str, bits: int, labels: int, name: str) -> np.ndarray:
        """getting error on unsupervised learner"""
        powers = np.logspace(min_power, max_power, points)
        error = np.ones(len(powers))
        for index in range(0, len(powers)):
            power = powers[index]
            test = file + 'test_' + ('%.2f' % power)
            model = self.__get_classifier(learner=learner, file=test, bits=bits, rows=self.__block_train)
            err = self.__test_classifier(analyzer=model, file=test, labels=labels)
            if name is not None:
                self.__print_info(name=name, power=power, error=err[1])
            error[index] = err[1]
        return error

    @staticmethod
    def __get_classifier_identifier(learner: str) -> str:
        """getting classifier's identifier"""
        if learner == 'ml':
            name = 'Maximum Likelihood Predictor'
        elif learner == 'map':
            name = 'Maximum A Posterior Predictor'
        elif learner == 'bayes':
            name = 'Naive Bayes Classifier'
        elif learner == 'logistic':
            name = 'Logistic Regression Classifier'
        elif learner == 'tree':
            name = 'Decision Tree Classifier'
        elif learner == 'forest':
            name = 'Random Forests Classifier'
        elif learner == 'vector':
            name = 'Support Vector Machines Classifier'
        elif learner == 'nearest':
            name = 'Nearest Neighbors Classifier'
        elif learner == 'gmm':
            name = 'Gaussian Mixtures Classifier'
        elif learner == 'acm':
            name = 'Agglomerative Clustering Classifier'
        else:
            name = 'Means Clustering Classifier'
        return name

    @staticmethod
    def __print_info(name: str, power: float, error: float):
        """printing run down of results"""
        print('with 1 / En = ' + ('%.2f' % power), end=': ')
        print(name + ' Error = ' + str(error))
        return


import os, time
import numpy as np
import tensorflow as tf # version 1.14
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from tensorflow.keras.datasets import mnist
from tensorflow.keras.callbacks import TensorBoard

(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = np.expand_dims(X_train, 3)
X_test = np.expand_dims(X_test, 3)

optimizers = [
    'Adagrad',
    'Adam',
    'RMSprop',
    'SGD'
]
for optimizer in optimizers:
    model = Sequential()
    model.add(Conv2D(16, (3,3), activation='relu', input_shape=X_train[0].shape))
    model.add(MaxPooling2D())
    model.add(Conv2D(16, (3,3), activation='relu'))
    model.add(MaxPooling2D())
    model.add(Flatten())
    model.add(Dense(10, activation='softmax'))
    model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy'])
    tensorboard = tf.keras.callbacks.TensorBoard(os.path.join('log_test1',f'{optimizer}_{time.time()}'))
    model.fit(X_train, y_train, batch_size=32, epochs=5, callbacks=[tensorboard])


print("Введите размерность улитки: ")
dimension = input()

key = {True: 'x', False: ' '}
olist = list(range(int(dimension) // 2))

for ykey in (1, -1):
    for y in olist[::ykey]:
        slist = []
        for xkey in (1, -1):
            for x in olist[::xkey]:
                d = -2 if (xkey, ykey) == (-1, 1) else 0
                slist.append(key[x >= y+d and not y % 2 or x < y+d and not x % 2])

        print(' '.join(slist))


class Employee:

    def __init__(self, first_name: str, department: str, email: str):
        self.first_name = first_name
        self.department = department
        self.email = email

class Task:

    def __init__(self, name: str, description: str, duration: int):
        self.name = name
        self.description = description
        self.__duration = duration

    def get_duration(self, emp: Employee) -> int:
        if emp.department == "Koordination":
            return self.__duration

t1 = Task("Kaffee kochen", "...", 1)

emp1 = Employee("Max", "IT", "max@company.de")
emp2 = Employee("Petra", "Koordination", "petra@company.de")

print(
    f"{emp1.first_name} versucht die Dauer von {t1.name} zu lesen:",
    t1.get_duration(emp1)
)

print(
    f"{emp2.first_name} versucht die Dauer von {t1.name} zu lesen:",
    t1.get_duration(emp2)
)