import numpy as np
from scipy import interpolate
from utils import HandledValueError

def spline(xpoints, ypoints, steps, order):
    if order > 5 or order >= len(xpoints):
        raise HandledValueError('Spline order must be smaller than number of points and not greater than 5')
    tck = interpolate.splrep(xpoints, ypoints, k=order, per=True)
    x = np.linspace(0, 1, steps)
    y = interpolate.splev(x, tck)
    return x, y

def linear(xpoints, ypoints, steps):
    f = interpolate.interp1d([xpoints[-2]-1, xpoints[-1]-1] + xpoints, [ypoints[-2], ypoints[-1]] + ypoints)
    x = np.linspace(0, 1, steps)
    y = f(x)
    return x, y

def points(kind, xpoints, ypoints, steps, order=None):
    x, y = [], []
    for i in range(0, len(xpoints) - 1):
        x0 = xpoints[i]
        y0 = ypoints[i]
        if kind == 'polynomial':
            xs, ys = func(kind, x0, xpoints[i + 1], y0, ypoints[i + 1], steps, order)
        else:
            xs, ys = func(kind, x0, xpoints[i + 1], y0, ypoints[i + 1], steps)
        x.extend(xs)
        y.extend(ys)
    return x, y

def func(kind, *args, **kwargs):
    if kind == 'harmonic':
        return harmonic(*args, **kwargs)
    elif kind == 'cycloidal':
        return cycloidal(*args, **kwargs)
    elif kind == 'parabolic':
        return parabolic(*args, **kwargs)
    elif kind == 'polynomial':
        return polynomial(*args, **kwargs)
    else:
        raise HandledValueError

def harmonic(x0, x1, y0, y1, steps):
    x = np.linspace(x0, x1, steps)
    y = y0 + (y1-y0)/2*(1-np.cos(np.divide(np.pi*(x+(-x0)), x1-x0)))
    return x, y

def cycloidal(x0, x1, y0, y1, steps):
    x = np.linspace(x0, x1, steps)
    r = np.divide(x+(-x0), x1-x0)
    y = y0 + (y1-y0)/np.pi*(np.pi*r-np.sin(2*np.pi*r)/2)
    return x, y

def parabolic(x0, x1, y0, y1, steps):
    x = np.linspace(x0, (x0+x1)/2, steps)
    r = np.divide(x + (-x0), x1 - x0)
    y = y0 + 2*(y1-y0)*r**2
    x2 = np.linspace((x0+x1)/2, x1, steps)
    r2 = np.divide(x2 + (-x0), x1 - x0)
    y2 = y0 + (y1-y0)*(1-2*(1-r2)**2)
    return np.concatenate((x, x2)), np.concatenate((y, y2))

def polynomial(x0, x1, y0, y1, steps, order):
    # Vandermonde matrix
    A = np.zeros((2+order*2, 2+order*2))

    B = np.zeros(2+order*2)

    B[0] = 0
    B[1] = y1-y0

    for col in range(0, 2+order*2):
        A[0, col] = 0 ** col
        A[1, col] = (x1-x0) ** col

    for deriv in range(1, order + 1):
        B[deriv*2] = 0
        B[1+deriv*2] = 0
        for col in range(0, 2+order*2):
            A[deriv*2, col] = der(0, col, deriv)
            A[1+deriv*2, col] = der((x1-x0), col, deriv)

    c = np.linalg.solve(A, B)

    x = np.linspace(0, x1-x0, steps)
    y = np.polyval(c[::-1], x)

    return x+x0, y+y0

# k-th derivative of x^n
def der(x, n, k):
    if k > n:
        return 0
    return np.math.factorial(n)/np.math.factorial(n-k)*x**(n-k)


import os
from itertools import islice
import numpy as np
import scipy.signal
import python_speech_features as psf
from ..features import spectral_features

class LmfIterator:
    def __init__(self, spectrograms, transform_which=None,
                sample_rate=10000, num_filters=40, fft_type='one_sided',
                diff_features=False):
        ''' Iterator over log mel-frequency features, given an iterator over STFT
        features.

        Args:
            spectrograms (iterable): should yield an iterable
              (e.g. a tuple) of STFT features, each one of shape
              (time x frequency [x ...])
            transform_which (tuple or None): if not None, which of the STFT
              features yielded from spectrograms should be transformed?
              For example, if spectrograms yields a 3-tuple of STFT features,
              `transform_which=(0,)` means that only the first member of that
              tuple is transformed
            sample_rate (float): sample rate of the signals represented in
              spectrograms
            num_filters (int): number of filters to generate; determines
              output dimension of log mel-frequency features
            fft_type (str): 'one_sided' or 'one_sided_plus_one'
                if 'one_sided', the filterbank size will be based on the
                assumption that the spectrogram frequency bin count is NFFT//2;
                if 'one_sided_plus_one', the freq bin count should be
                NFFT//2 + 1
                'two_sided' will raise a NotImplementedError
            diff_features (bool): If true, generate first- and second-order
              difference features in time

        Yields:
            tuple, same shape as the elements of `spectrograms`, with one or more
              feature sets transformed into leg mel-frequency filterbank
              features
        '''
        self.sample_rate = sample_rate
        self.num_fft = None
        self.num_filters = num_filters
        self.spectrograms = spectrograms
        self.transform_which = transform_which
        self.diff_features = diff_features

    def __next__(self):
        spectrograms = next(self.spectrograms)
        # Initialize number of bins
        if self.num_fft is None:
            if self.fft_type == 'one_sided_plus_one':
                self.num_fft = 2*(spectrograms[0].shape[1]-1)
            elif self.fft_type == 'one_sided':
                self.num_fft = 2*spectrograms[0].shape[1]
            elif self.fft_type == 'two_sided':
                raise NotImplementedError
            else:
                raise ValueError("Unrecognized FFT type")
        transformed_spectrograms = []
        for i, spectrogram in enumerate(spectrograms):
            if self.transform_which is None or i in self.transform_which:
                transformed_spectrograms.append(self._transform(spectrogram))
            else:
                transformed_spectrograms.append(spectrogram)
        return tuple(transformed_spectrograms)

    def __iter__(self):
        return self

    def _transform(self, spectrogram):
        '''Transform STFT features into log mel-frequency filterbank features'''
        # handle multiple spectrograms at once
        spec_dim = len(spectrogram.shape)
        # Move time and frequency to the end of dim list,
        # if there are other dimensions
        if spec_dim > 2:
            new_shape = [*range(2,spec_dim), 0, 1]
            spectrogram = np.transpose(spectrogram, new_shape)
        # freely adapted from python_speech_features logfbank
        fb = psf.get_filterbanks(self.num_filters,
                                self.num_fft,
                                self.sample_rate,0,
                                self.sample_rate/2)
        mag_spec = np.absolute(spectrogram)
        pow_spec = 1.0/self.num_fft * np.square(mag_spec)
        energies = np.dot(pow_spec,fb.T) # compute the filterbank energies
        energies = np.where(energies == 0,np.finfo(float).eps,energies)
        log_energies = np.log(energies)
        # Move time and frequency back to the start of the dim list
        if spec_dim > 2:
            old_shape = [spec_dim-2, spec_dim-1, *list(range(spec_dim-2))]
            print(old_shape)
            log_energies = np.transpose(log_energies, old_shape)

        # get diff features
        if self.diff_features:
            first_diff = np.diff(log_energies, 1, axis=0)
            # Zero-pad beginning
            first_diff = np.concatenate((np.zeros((1,*first_diff.shape[1:]),
                                                 dtype=log_energies.dtype),
                                                 first_diff), axis=0)
            second_diff = np.diff(log_energies, 2, axis=0)
            second_diff = np.concatenate((np.zeros((2,*second_diff.shape[1:]),
                                                 dtype=log_energies.dtype),
                                                 second_diff), axis=0)
            # Concatenate in frequency
            # This is a weird thing to do, not sure if would be better to
            # have on its own dimension
            log_energies = np.concatenate((log_energies, first_diff, second_diff),
                                          axis=1)
        return log_energies

def lmf_stft_iterator(wavs, fs = 1.0, stft_len=1024, stft_step=512, nfft=512,
    nfilters=40, use_diffs=True, mode='complex', **kwargs):
    """
    get signals from iterator wavs, transform to freq domn,
    get difference features, and yield
    Warning: stft length should make sense in reln. to length of signals yielded by wavs

    Args:

      **kwargs - passed to stft

    Yields:
        tuple of NDArrays - (features, truth) where
            features is time x freq and
            truth is num_srcs + 1 x time x freq, with the mixed signal's STFT
            in the last slot on the first axis

    """

    while True:
        truth_sigs, mixed_sig = next(wavs)
        # Stack signals onto each other for transformation
        all_sigs = (truth_sigs, mixed_sig)
        all_sigs = np.concatenate(all_sigs, axis=0)
        # transform each truth signal into logmel features
        lmfs = []
        stfts = []

        for sig in all_sigs:
            stft = scipy.signal.spectrogram(sig, fs=fs, nperseg=stft_len,
                                            noverlap=stft_len-stft_step,
                                            mode=mode, scaling='density', **kwargs)[2].T
            stfts.append(stft)

        stft = np.stack(stfts, 0)

        lmf = psf.logfbank(mixed_sig, samplerate=fs,
                nfft=nfft, nfilt=nfilters,
                winlen=stft_len, winstep=stft_step)

        if use_diffs:
            # take 1st- and 2nd-order differences of LMF in time
            diff1 = np.diff(lmf, axis=0)
            diff2 = np.diff(diff1, axis=0)
            # zero-pad diff features
            diff1 = np.concatenate((np.zeros_like(diff1[:1]), diff1))
            diff2 = np.concatenate((np.zeros_like(diff2[:2]), diff2))
            # concatenate difference features in "frequency" TODO: use another dimension??
            lmf = np.concatenate((lmf, diff1, diff2), axis=1)

        yield lmf, stft


from rich import box
from rich.columns import Columns
from rich.console import Console
from rich.panel import Panel
from rich.tree import Tree

console = Console(record=True, width=100)

about = """I'm a freelance Data Scientist / Machine Learning Engineer with 2x M.Sc. degrees. I worked primarily on
Computer Vision problems in the past. Currently, I'm more interested in Natural Language Processing and Time-Series applications.

You can find me on [bold link=https://twitter.com/bertan_gunyel]Twitter[/] and [bold link=https://www.linkedin.com/in/bertan-gunyel/]Linkedin"""

panel = Panel.fit(
    about, box=box.DOUBLE, border_style="blue", title="[b]Hello everyone", width=60
)

tree = Tree("[link=https://www.linkedin.com/in/bertan-gunyel/]Bertan Günyel", guide_style="bold")
projects_tree = tree.add(":open_file_folder: Projects", guide_style="bold")
tech_stack_tree = tree.add(":computer: Tech Stack", guide_style="bold")

projects_nlp_tree = projects_tree.add(':memo: NLP')
projects_time_series_tree = projects_tree.add(':chart_with_upwards_trend: Time-series')

projects_nlp_tree.add("⭐ [link=https://github.com/bgunyel/topic-modeling]Topic Modeling")
projects_nlp_tree.add("⭐ [link=https://github.com/bgunyel/kaggle_commonlit-readibility]Kaggle CommonLit Readability Prize")

projects_time_series_tree.add("⭐ [link=https://github.com/bgunyel/electricity-demand-forecasting]Electricity Demand Forecasting")
projects_time_series_tree.add("⭐ [link=https://github.com/bgunyel/strava-runners]Strava Runners")

tech_stack_nlp_tree = tech_stack_tree.add(':memo: NLP')
tech_stack_ml_tree = tech_stack_tree.add(':rocket: Machine Learning')
tech_stack_ds_tree = tech_stack_tree.add(':dart: Data Science')
tech_stack_vis_tree = tech_stack_tree.add(':art: Visualization')
tech_stack_mlops_tree = tech_stack_tree.add(':hourglass: ML Ops')
tech_stack_cloud_tree = tech_stack_tree.add(':crystal_ball: Cloud Computing')

tech_stack_nlp_tree.add('⭐ Hugging Face')
tech_stack_nlp_tree.add('⭐ NLTK')

tech_stack_ml_tree.add('⭐ PyTorch')
tech_stack_ml_tree.add('⭐ Scikit-learn')

tech_stack_ds_tree.add('⭐ Pandas')
tech_stack_ds_tree.add('⭐ Numpy')
tech_stack_ds_tree.add('⭐ Stats-models')

tech_stack_vis_tree.add('⭐ Matplotlib')
tech_stack_vis_tree.add('⭐ Seaborn')
tech_stack_vis_tree.add('⭐ Plotly')

tech_stack_mlops_tree.add('⭐ Domino Data Lab')

tech_stack_cloud_tree.add('⭐ AWS')
tech_stack_cloud_tree.add('⭐ GCP')
tech_stack_cloud_tree.add('⭐ Azure')

panel2 = Panel.fit(tree, box=box.DOUBLE, border_style="blue", title="[b]Technical Breakdown", width=80)

console.print(Columns([panel, panel2]))

CONSOLE_HTML_FORMAT = """\

{code}

"""

console.save_html("README.md", inline_styles=True, code_format=CONSOLE_HTML_FORMAT)


from typing import Optional

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def getMinimumDifference(root: Optional[TreeNode]) -> int:
    xs = []
    def traverse(node):
        if not node:
            return
        traverse(node.left)
        xs.append(node.val)
        traverse(node.right)

    traverse(root)
    min_diff = float("infinity")
    for a, b in zip(xs, xs[1:]):
        min_diff = min(min_diff, abs(a - b))

    return min_diff


from multiprocessing import shared_memory, Condition

import numpy as np
from lib.exceptions import InvalidInputNameException
from lib.utils import create_ndbuffer

def create_readonly_wire(name):
    try:
        shm = shared_memory.SharedMemory(name, create=False)
    except FileNotFoundError:
        shm = None
    return shm

class Inputs:

    ENABLE_NAME = "Enable"

    def __init__(self, input_data) -> None:
        self.inputs = input_data

    def _init_enabled(self) -> None:
        self._enable_data = self.inputs[Inputs.ENABLE_NAME] if Inputs.ENABLE_NAME in self.inputs else None
        self._enable_condition = Condition(self.inputs[Inputs.ENABLE_NAME]["lock"]) if self.enable_wire else None

    def read(self, name):
        if self.inputs.get(name) is None:
            raise InvalidInputNameException(f"{name} is not declared in inputs")

        if self.inputs[name].get("created", False):
            # Do this if read wire has been created
            # Read data from the different buffers of the SHM Objects
            dim = create_ndbuffer((1,), np.int64, self.inputs[name]["dim"].buf)[:][0]
            shape = create_ndbuffer((dim,), np.int64, self.inputs[name]["shape"].buf)
            type = create_ndbuffer((1,), '<U6', self.inputs[name]["type"].buf)[:][0]
            data = create_ndbuffer(shape, type, self.inputs[name]["data"].buf)
        else:
            # Do this if read wire has not been created
            # Create required wires(SHM Objects) to read data from the outputs
            wire_name = self.inputs[name]["wire"]
            data_wire = create_readonly_wire(wire_name)
            dim_wire = create_readonly_wire(wire_name + "_dim")
            shape_wire = create_readonly_wire(wire_name + "_shape")
            type_wire = create_readonly_wire(wire_name + "_type")

            if data_wire is None or shape_wire is None or dim_wire is None or type_wire is None:
                return None

            # Store SHM Object in "dim"
            self.inputs[name]["dim"] = dim_wire
            # Read dimension data from the object's buffer
            dim = create_ndbuffer((1,), np.int64, dim_wire.buf)[:][0]
            # Store SHM Object in "type"
            self.inputs[name]["type"] = type_wire
            # Read type data from the object's buffer
            type = create_ndbuffer((1,), '<U6', type_wire.buf)[:][0]
            # In case type isn't defined return None
            if not type:
                return None

            # Store SHM Object in "shape"
            self.inputs[name]["shape"] = shape_wire
            # Read shape data from the object's buffer
            shape = create_ndbuffer((dim,), np.int64, shape_wire.buf)

            # Store SHM Object in "data"
            self.inputs[name]["data"] = data_wire
            # Read data from the object's buffer
            data = create_ndbuffer(shape, type[:], data_wire.buf)

            # Mark wire as created
            self.inputs[name]["created"] = True

        return data

    def _read_npy_matrix(self, name, dtype):
        if self.inputs[name].get("created", False):
            dim = create_ndbuffer((1,), np.int64, self.inputs[name]["dim"].buf)[:][0]
            shape = create_ndbuffer((dim,), np.int64, self.inputs[name]["shape"].buf)
            data = create_ndbuffer(shape, dtype, self.inputs[name]["data"].buf)
        else:
            wire_name = self.inputs[name]["wire"]
            data_wire = create_readonly_wire(wire_name)
            shape_wire = create_readonly_wire(wire_name + "_shape")
            dim_wire = create_readonly_wire(wire_name + "_dim")
            if data_wire is None or shape_wire is None or dim_wire is None:
                return None

            self.inputs[name]["dim"] = dim_wire
            dim = create_ndbuffer((1,), np.int64, dim_wire.buf)[:][0]
            self.inputs[name]["shape"] = shape_wire
            shape = create_ndbuffer((dim,), np.int64, shape_wire.buf)
            self.inputs[name]["data"] = data_wire
            data = create_ndbuffer(shape, dtype, data_wire.buf)
            self.inputs[name]["created"] = True

        return data

    def read_image(self, name):
        if self.inputs.get(name) is None:
            raise InvalidInputNameException(f"{name} is not declared in inputs")

        data = self._read_npy_matrix(name, np.uint8)
        return data

    def read_number(self, name):
        if self.inputs.get(name) is None:
            raise InvalidInputNameException(f"{name} is not declared in inputs")

        number = None
        if self.inputs[name].get("created", False):
            number = create_ndbuffer((1,), np.float64, self.inputs[name]["data"].buf)
        else:
            wire_name = self.inputs[name]["wire"]
            data_wire = create_readonly_wire(wire_name)
            if data_wire is None:
                return None
            self.inputs[name]["data"] = data_wire
            number = create_ndbuffer((1,), np.float64, data_wire.buf)
            self.inputs[name]["created"] = True

        return number

    def read_string(self, name):
        if self.inputs.get(name) is None:
            raise InvalidInputNameException(f"{name} is not declared in inputs")

        string = None
        if self.inputs[name].get("created", False):
            string = create_ndbuffer((1,), '<U64', self.inputs[name]["data"].buf)
        else:
            wire_name = self.inputs[name]["wire"]
            data_wire = create_readonly_wire(wire_name)
            if data_wire is None:
                return None
            self.inputs[name]["data"] = data_wire
            string = create_ndbuffer((1,), '<U64', data_wire.buf)
            self.inputs[name]["created"] = True

        return string

    def read_array(self, name):
        if self.inputs.get(name) is None:
            raise InvalidInputNameException(f"{name} is not declared in inputs")

        data = self._read_npy_matrix(name, np.float64)
        return data

    @property
    def enabled(self) -> bool:
        # No enable wire used, so enabled by default
        if self._enable_data is None:
            return True

        _enabled = self.read_number(Inputs.ENABLE_NAME)

        # If enable wire is present but, not initialized, return False
        if _enabled is None:
            return False

        return np.isclose(_enabled, np.array([1.0]))

    @enabled.setter
    def enabled(self, _enabled: bool):
        if self._enable_data is None:
            return

        self._enable_data["lock"].acquire()

        if self._enable_data.get("created", False):
            self._enable_data["data"][0] = 1 if _enabled else 0
        else:
            # Wire doesn't exist yet, ideally has to be created and set
            # TODO: Is this case necessary?
            pass

        self._enable_data["lock"].release()


# -*- coding: utf-8 -*-
#导入其他模块的符号集，Urllib 模块提供了读取web页面数据的接口，
#使我们可以像读取本地文件一样读取www和ftp上的数据
import urllib.request
import re
import html.parser as h
import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')
#定义函数，urllib.urlopne()打开一个url地址，read()方法读取url上的数据，
#返回网页，即将页面下载下来
def getHtml(url):
    page = urllib.request.urlopen(url)
    #print(page.info())
    html = page.read()
    return html

#输出该网页内容
#print (html)

#获取图片
def getImg(html):
    reg = r''
    imgre = re.compile(reg)
    #html=html.encode('utf-8')
    imglist = re.findall(imgre,html)
    print("the informations of images")
    print(imglist)
    x=0
    for imgre in imglist:
        urllib.request.urlretrieve(imgre,'%s.jpg'%x)
        x=x+1

def getAuthor(html):
	#作者姓名
    reau=r'
(.*?)
'
    #author=re.compile(reau)
    author_list=re.findall(reau,html,re.S|re.M)
    print('the name of author:')
    for content in author_list:
        print(content)

    #作者信息
    reau1=r'
(.*?)
'
    author_list=re.findall(reau1,html,re.S|re.M)
    #print("the author_list is:",author_list)
    print('the informations of author:')
    for content in author_list:
        if "href" not in content:
    	    print(content)
        else:
    	    print('the research direction of author:')
    	    reau3=r'(.*?)'
    	    author_direction=re.findall(reau3,content,re.S|re.M)
    	    print(author_direction)
    	    #if content in author_direction:
    	        #print(content)
    	    #print(content)
    #print(author_list)
    reau2=r'
(.*?)
'
    author_list=re.findall(reau2,html,re.S|re.M)
    print('the email of author:')
    for content in author_list:
    	print(content)

    #发表过的论文，论文信息存在html的table标签内,tr行td列
    res_tr=r'
(.*?)
'#第一行
    m_tr=re.findall(res_tr,html,re.S|re.M)
    for line in m_tr:
        res_td=r'(.*?)	'#第一列
        m_td=re.findall(res_td,line,re.S|re.M)
        for mm in m_td:
            if "href" in mm:#第一列内a标签
                restr = r'(.*?)'
                h = re.findall(restr,mm,re.S|re.M)
                print("The name of the paper:")
                print(h),
            if "class" in mm:#第一列内div标签
                restr=r'
(.*?)
'
                h2=re.findall(restr,mm,re.S|re.M)
                print("author names,Magazine name and date of publication:")
                print(h2)

    #合作过的学者信息,信息在html的ul标签内
    res_ul=r'
(.*?)
'
    m_ul=re.findall(res_ul,html,re.S|re.M)
    for line in m_ul:
        if "href" in line:
            #print(line)
            restr=r'(.*?)'
            h3=re.findall(restr,line,re.S|re.M)
            print("the name of cooperative scholars:")
            #h3=unicode(h3).encode("utf-8")
            print(h3)

    #页面内的url
    res_url = r"(?<=href=\").+?(?=\")|(?<=href=\').+?(?=\')"
    link = re.findall(res_url, html, re.I|re.S|re.M)
    print('the url list:')
    for url in link:
        print (url)

url=["https://scholar.google.com/citations?user=hNTyptAAAAAJ&hl=en","https://scholar.google.com/citations?user=Jj00ksMAAAAJ&hl=en","https://scholar.google.com/citations?user=09kJn28AAAAJ&hl=en","https://scholar.google.com/citations?user=q92q8SMAAAAJ&hl=en"]
y=0
for u in url:
    #print(url[0])
    #print(url[1])
    html = getHtml(url[y])
    #print(html)
    html=html.decode('ISO-8859-1')#ISO-8859-1
    getAuthor(html)
    #getImg(html)
    y=y+1


# Alex Johnson
# CS 4300 - Computer Graphics
# Assignment 06 - 3D Animation
# Part I : Scripting

# Initial source code from '3D_Scripting_Mac/CS4800/simple_animation.py'
# These scripts have been included in the .zip file, and can be
# found in the '/examples folder'.

# Maya's Python commands can be found online at:
# http://download.autodesk.com/us/maya/2010help/CommandsPython/index_all.html

import maya.cmds as cmds
import random

# Start scene with a "clean slate"
# -> erase any artifacts on stage
newfile = cmds.file(f=True, new=True)

#*************************************
#       Scene Configuration
#*************************************

# Set playback options
cmds.playbackOptions( loop='continuous' )
cmds.playbackOptions( minTime='0sec', maxTime='5sec', ast=0, ps=1.8)

# set initial perspective position
cmds.setAttr('persp.translateX', -15.000)
cmds.setAttr('persp.translateY', 10.000)
cmds.setAttr('persp.translateZ', 20.000)

# set initial perspective angle
cmds.setAttr('persp.rotateX', -15.000)
cmds.setAttr('persp.rotateY', -35.000)
cmds.setAttr('persp.rotateZ', 0.000)

#*************************************
#      Object Configuration
#*************************************

# Create a sphere, with 10 subdivisions in the X direction,
# and 15 subdivisions in the Y direction,
# the radius of the sphere is 20.
# http://download.autodesk.com/us/maya/2010help/CommandsPython/polySphere.html
cmds.polySphere(n='sphere01', sx=15, sy=20, r=1)

# Query the radius of the new sphere
r = cmds.polySphere('sphere01', q=True, sx=True )

# Set sphere's pivot point to the bottom.
# -> Makes scaling the sphere upon collision easier
cmds.move(0, -1, "sphere01.scalePivot","sphere01.rotatePivot", absolute=True)

# Create polygon cube (Ground)
# result is a 20 units height rectangular box
# with 10 subdivisions along X, 15 along Y and 20 along Z.
c = cmds.polyCube(sx=10, sy=15, sz=5, h=.25, w=5, d=5)
cmds.setKeyframe(c, v=-0.3, at='translateY', itt='linear', ott='linear', t = 0)

#*************************************
#     Perspective Movement
#*************************************

## Pull Back perspective over 200 frames.
# start perspective change
cmds.setKeyframe( 'persp', v=-20,      at='translateX', itt='linear', ott='linear', t = 0)
cmds.setKeyframe( 'persp', v=15,       at='translateY', itt='linear', ott='linear', t = 0)
cmds.setKeyframe( 'persp', v=25,       at='translateZ', itt='linear', ott='linear', t = 0)

# finish perspective change
cmds.setKeyframe( 'persp', v=-15,    at='translateX', itt='linear', ott='linear', t = 200)
cmds.setKeyframe( 'persp', v=10,     at='translateY', itt='linear', ott='linear', t = 200)
cmds.setKeyframe( 'persp', v=20,     at='translateZ', itt='linear', ott='linear', t = 200)

#*************************************
#      Camera Configuration
#*************************************

## Create camera
cameraName = cmds.camera()
cameraShape = cameraName[1]

#*************************************
#      Camera Movement
#*************************************

## Move camera through scene
# Camera Start Position
cmds.setKeyframe(cameraName, v=6.8,       at='translateX', itt='spline', ott='spline', t = 0)
cmds.setKeyframe(cameraName, v=9.5,       at='translateY', itt='spline', ott='spline', t = 0)
cmds.setKeyframe(cameraName, v=15.8,      at='translateZ', itt='spline', ott='spline', t = 0)

cmds.setKeyframe(cameraName, v=-24.3,     at='rotateX', itt='spline', ott='spline', t = 0)
cmds.setKeyframe(cameraName, v=22.7,      at='rotateY', itt='spline', ott='spline', t = 0)
cmds.setKeyframe(cameraName, v=0,         at='rotateZ', itt='spline', ott='spline', t = 0)

# Camera End Position
cmds.setKeyframe(cameraName, v=-3.3,      at='translateX', itt='spline', ott='spline', t = 150)
cmds.setKeyframe(cameraName, v=4.0,       at='translateY', itt='spline', ott='spline', t = 150)
cmds.setKeyframe(cameraName, v=5.6,       at='translateZ', itt='spline', ott='spline', t = 150)

cmds.setKeyframe(cameraName, v=-18.3,     at='rotateX', itt='spline', ott='spline', t = 150)
cmds.setKeyframe(cameraName, v=-34.1,     at='rotateY', itt='spline', ott='spline', t = 150)
cmds.setKeyframe(cameraName, v=0.0,       at='rotateZ', itt='spline', ott='spline', t = 150)

#*************************************
#    Sphere Animation - Translate
#*************************************

# Animate a sphere as it bounces vertically.
# Translate manages it's vertical position

cmds.setKeyframe( 'sphere01', v=6.0,     at='translateY', itt='spline', ott='spline', t = 60)
cmds.setKeyframe( 'sphere01', v=1.0,     at='translateY', itt='spline', ott='spline', t = 90)
cmds.setKeyframe( 'sphere01', v=1.0,     at='translateY', itt='spline', ott='spline', t = 110)
cmds.setKeyframe( 'sphere01', v=5.5,     at='translateY', itt='spline', ott='spline', t = 160)

#*************************************
#    Sphere Animation - Scale
#*************************************

# Animate a sphere as it bounces vertically.
# Scale manages the shape deformation/compressions

# Initial keyframe
cmds.setKeyframe( 'sphere01', v=1.0,     at='scaleY', itt='linear', ott='linear', t = 60)

# Start Compression
cmds.setKeyframe( 'sphere01', v=1.0,     at='scaleX', itt='linear', ott='linear', t = 90)
cmds.setKeyframe( 'sphere01', v=1.0,     at='scaleY', itt='linear', ott='linear', t = 90)
cmds.setKeyframe( 'sphere01', v=1.0,     at='scaleZ', itt='linear', ott='linear', t = 90)

# Max Compression
cmds.setKeyframe( 'sphere01', v=1.3,     at='scaleX', itt='spline', ott='spline', t = 100)
cmds.setKeyframe( 'sphere01', v=0.6,     at='scaleY', itt='spline', ott='spline', t = 100)
cmds.setKeyframe( 'sphere01', v=1.3,     at='scaleZ', itt='spline', ott='spline', t = 100)

# End Compression
cmds.setKeyframe( 'sphere01', v=1.0,     at='scaleX', itt='linear', ott='linear', t = 110)
cmds.setKeyframe( 'sphere01', v=1.0,     at='scaleY', itt='linear', ott='linear', t = 110)
cmds.setKeyframe( 'sphere01', v=1.0,     at='scaleZ', itt='linear', ott='linear', t = 110)

# End rebound compressions
cmds.setKeyframe( 'sphere01', v=1.2,     at='scaleY', itt='linear', ott='linear', t = 130)

# Final keyframe
cmds.setKeyframe( 'sphere01', v=1.0,     at='scaleY', itt='linear', ott='linear', t = 150)


"""
This script includes the Demo class to perform the object instance
segmentation demo.
"""

import os
import cv2
import pycocotools.mask as pycocomask

from detectron2.data.datasets import register_coco_instances
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog, DatasetCatalog
from detectron2.utils.visualizer import ColorMode

class Demo:
    """
    The Demo class provides functions to demonstrate object instance
    segmentation on a COCOpen-generated datset.
    ...

    Attributes
    ----------
    parameters : dict
        contains all parameters used to generate the demo dataset

    Methods
    -------
    make_new_dirs():
        Make new directories where the demo files are saved
    demo():
        Runs the demo to visualize object instance segmentation
    """

    # Constructor
    def __init__(
        self,
        parameters: dict,
    ) -> None:
        # Initializing parameters
        self.parameters = parameters

        # Initializing root and destination directory
        self.root_dir = self.parameters["directory"]["root_dir"]
        self.data_dir_name = self.parameters["directory"]["dataset_dir_name"]

        # Saving all directory names
        self.dataset_dir = self.root_dir + f"/datasets/{self.data_dir_name}"
        self.demo_dir = self.root_dir + "/demo"
        self.demo_dataset_dir = self.demo_dir + f"/{self.data_dir_name}"
        self.vis_dir = self.demo_dataset_dir + "/visualization"
        self.mask_dir = self.demo_dataset_dir + "/masks"

    def make_new_dirs(self):
        """
        Make new directories where the demo files are saved
        """
        try:
            os.mkdir(self.demo_dir)
        except FileExistsError:
            print("demo directory already exists!")
        try:
            os.mkdir(self.demo_dataset_dir)
        except FileExistsError:
            print("demo dataset directory already exists!")
        try:
            os.mkdir(self.vis_dir)
        except FileExistsError:
            print("visualization directory already exists!")
        try:
            os.mkdir(self.mask_dir)
        except FileExistsError:
            print("masks directory already exists!")

    def demo(self):
        """
        Runs the demo to visualize object instance segmentation
        """
        data = self.parameters["dataset_verification"]["which_dataset"]
        register_coco_instances(
            data,
            {},
            f"{self.dataset_dir}/{data}/{data}.json",
            f"{self.dataset_dir}/{data}/",
        )
        dicts = DatasetCatalog.get(f"{data}")
        metadata = MetadataCatalog.get(f"{data}")

        count = self.parameters["dataset_verification"]["number_of_images"]
        for i, d in enumerate(dicts[:count]):
            img = cv2.imread(d["file_name"])
            annos = d["annotations"]
            mask_list = []
            for anno in annos:
                encoded = anno["segmentation"]
                instance_annotation = pycocomask.decode(encoded) * 255
                instance_img = cv2.cvtColor(instance_annotation, cv2.COLOR_GRAY2BGR)
                x, y, w, h = anno["bbox"]
                mask = cv2.rectangle(
                    instance_img, (x, y), (x + w, y + h), (255, 255, 255), 2
                )
                mask_list.append(mask)

            # horizontally concatenate visualization of object instance
            # segmentation masks
            cat_masks = cv2.hconcat(mask_list)

            # visualize object instance segmentation on cocopen-generated data
            visualizer = Visualizer(
                img, metadata, scale=0.5, instance_mode=ColorMode.IMAGE
            )
            out = visualizer.draw_dataset_dict(d)

            # save masks and visualizations
            cv2.imwrite(f"{self.vis_dir}/{str(i)}.png", out.get_image())
            cv2.imwrite(f"{self.mask_dir}/{str(i)}.png", cat_masks)


from flask import Flask
from flask import render_template, url_for, request, flash, redirect
import sqlite3
import transferserver
import chatserver
import _thread
import threading
import multiprocessing

app = Flask(__name__)
app.secret_key = "supersecretkey"

all_processes = []

def createTableData():

    items = []

    conn = sqlite3.connect('serversdatabase.db')
    cur = conn.cursor()

    try:
        cur.execute('SELECT * FROM servers')
        conn.commit()
    except:
        cur.execute("CREATE TABLE servers(name TEXT, type TEXT, port TEXT, status TEXT)")
        conn.commit()

    liste = cur.fetchall()

    for i in liste:
        templist = ['name', 'type', 'port', 'status']
        tempdict = {}

        cpt = 0
        for j in i:
            tempdict[templist[cpt]] = j
            cpt += 1

        items.append(tempdict)

    cur.close()
    conn.close()

    return items

def bootAllServers():

    global all_processes

    items = []

    conn = sqlite3.connect('serversdatabase.db')
    cur = conn.cursor()

    cur.execute('SELECT * FROM servers')
    conn.commit()

    liste = cur.fetchall()

    for i in liste:
        templist = []
        cpt = 0
        for j in i:
            templist.append(j)
            cpt += 1
        items.append(templist)

    cur.close()
    conn.close()

    for row in items:

        if row[3] == "Up":

            process = multiprocessing.Process(target=startOneServ, args=(row[0], row[1], row[2]))
            process.start()
            all_processes.append([process, row[0]])

@app.route("/backend/addServer", methods=['POST'])
def addServer():

    namevar = request.form['name']
    typevar = request.form['type']
    portvar = request.form['port']
    statusvar = "Down"

    conn = sqlite3.connect('serversdatabase.db')
    cur = conn.cursor()

    data = (namevar, typevar, portvar, statusvar)

    cur.execute("INSERT INTO servers(name, type, port, status) VALUES(?, ?, ?, ?)", data)

    conn.commit()

    cur.close()
    conn.close()

    flash("Server added.")

    headers = ["Server's name", "Server's type", 'Port', 'Status']

    return redirect(url_for('index'))
    return render_template('index.html', headers=headers, objects=createTableData())

@app.route("/backend/deleteServer", methods=['POST'])
def deleteServer():

    namevar = request.form['name']
    typevar = request.form['type']
    portvar = request.form['port']

    conn = sqlite3.connect('serversdatabase.db')
    cur = conn.cursor()

    data = (namevar, typevar, portvar)

    cur.execute('SELECT * FROM servers WHERE name = ? AND type = ? AND port = ?', data)
    conn.commit()

    liste = cur.fetchall()

    if len(liste) > 0:

        cur.execute('DELETE FROM servers WHERE name = ? AND type = ? AND port = ?', data)
        flash("Server deleted.")
        conn.commit()

    else:
        flash("Error. Please check and retry server's information.")

    headers = ["Server's name", "Server's type", 'Port', 'Status']

    return redirect(url_for('index'))
    return render_template('index.html', headers=headers, objects=createTableData())

def startOneServ(name, type, port):

    if type == "File transfer":

        x = name
        vars()[x] = name

        name = transferserver.Server(int(port))
        print(x + " started.")
        name.start()

        statusvar = "Up"

        conn = sqlite3.connect('serversdatabase.db')
        cur = conn.cursor()

        data = (statusvar, x, type, port)

        cur.execute('UPDATE servers SET status = ? WHERE name = ? AND type = ? AND port = ?', data)
        conn.commit()

    elif type == "Chat":

        x = name
        vars()[x] = name

        name = chatserver.listening_serv(int(port))

        print(x + " started.")

        name.run()

        statusvar = "Up"

        conn = sqlite3.connect('serversdatabase.db')
        cur = conn.cursor()

        data = (statusvar, x, type, port)

        cur.execute('UPDATE servers SET status = ? WHERE name = ? AND type = ? AND port = ?', data)
        conn.commit()

@app.route("/backend/startServer", methods=['POST'])
def startServer():

    global all_processes

    namevar = request.form['name']
    typevar = request.form['type']
    portvar = request.form['port']

    statusvar = "Up"

    conn = sqlite3.connect('serversdatabase.db')
    cur = conn.cursor()

    data = (statusvar, namevar, typevar, portvar)

    cur.execute('UPDATE servers SET status = ? WHERE name = ? AND type = ? AND port = ?', data)
    conn.commit()

    process = multiprocessing.Process(target=startOneServ, args=(namevar, typevar, portvar))
    process.start()
    all_processes.append([process, namevar])

    print(namevar + " started.")

    headers = ["Server's name", "Server's type", 'Port', 'Status']

    return redirect(url_for('index'))
    return render_template('index.html', headers=headers, objects=createTableData())

@app.route("/backend/stopServer", methods=['POST'])
def stopServer():

    global all_processes

    namevar = request.form['name']
    typevar = request.form['type']
    portvar = request.form['port']

    statusvar = "Down"

    conn = sqlite3.connect('serversdatabase.db')
    cur = conn.cursor()

    data = (statusvar, namevar, typevar, portvar)

    cur.execute('UPDATE servers SET status = ? WHERE name = ? AND type = ? AND port = ?', data)
    conn.commit()

    for proc in range(0, len(all_processes)):
        if all_processes[proc][1] == namevar:
            all_processes[proc][0].terminate()
            all_processes.pop(proc)

    headers = ["Server's name", "Server's type", 'Port', 'Status']

    return redirect(url_for('index'))
    return render_template('index.html', headers=headers, objects=createTableData())

@app.route("/backend/stopAllServers", methods=['POST'])
def stopAllServers():

    global all_processes

    statusvar = "Down"

    conn = sqlite3.connect('serversdatabase.db')
    cur = conn.cursor()

    data = (statusvar,)

    cur.execute('UPDATE servers SET status = ?', data)
    conn.commit()

    for proc in range(0, len(all_processes)):
        all_processes[proc][0].terminate()
        all_processes.pop(proc)

    headers = ["Server's name", "Server's type", 'Port', 'Status']

    return redirect(url_for('index'))
    return render_template('index.html', headers=headers, objects=createTableData())

@app.route('/')
def index():

    headers = ["Server's name", "Server's type", 'Port', 'Status']

    return render_template('index.html', headers=headers, objects=createTableData())

if __name__ == '__main__':

    boot = threading.Thread(target=bootAllServers)
    boot.start()

    app.run(debug=True)


import os
import pickle
import numpy as np
import matplotlib.pyplot as plt
from sklearn import preprocessing
from tensorflow.keras.layers import Input, Dense, Conv1D, AveragePooling1D, Concatenate, Flatten, Dropout
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import SGD

from ELCA import lc_fitter, transit

def pn_rates(model,X,y):
    y_pred = np.round( model.predict(X) )

    pos_idx = y==1
    neg_idx = y==0

    tp = np.sum(y_pred[pos_idx]==1)/y_pred.shape[0]
    fn = np.sum(y_pred[pos_idx]==0)/y_pred.shape[0]

    tn = np.sum(y_pred[neg_idx]==0)/y_pred.shape[0]
    fp = np.sum(y_pred[neg_idx]==1)/y_pred.shape[0]

    return fn,fp

def make_cnn(maxlen):

    input_local = Input(shape=(maxlen,1))
    x = Conv1D(16, 5, strides=1)(input_local)
    #x = Conv1D(16, 5, strides=1)(x)
    x = AveragePooling1D(pool_size=5, strides=2)(x)
    x = Conv1D(8, 5, strides=1)(x)
    #x = Conv1D(8, 5, strides=1)(x)
    x = AveragePooling1D(pool_size=5, strides=2)(x)

    xf = Flatten()(x)
    z = Dense(64, activation='relu')(xf)
    #z = Dropout(0.1)(z)
    z = Dense(32, activation='relu')(z)
    z = Dense(8, activation='relu')(z)

    output = Dense(1, activation='sigmoid', name='main_output')(z)
    model = Model(inputs=input_local, outputs=output)

    SGDsolver = SGD(lr=0.1, momentum=0.25, decay=0.0001, nesterov=True)
    model.compile(loss='binary_crossentropy',
                optimizer=SGDsolver,
                metrics=['accuracy'])
    return model

def generate_negatives(residuals, N=10000):
    negative = []

    # generate some negative examples
    for i in range(10000):

        # choose random sample of time, ensure we have enough data points ``
        length = 0
        while (length != 180):
            di = np.random.choice( np.arange(len(times)) )

            # make sure we get enough data for NN
            ti = np.random.choice( np.arange(len(times[di])) )
            start_t = times[di][ti]
            end_t = times[di][ti] + 2*180/(24*60)
            tmask = (times[di] > start_t) & (times[di] < end_t)
            length = tmask.sum()

        negative.append(residuals[di][tmask])

    return negative

def generate_positives(residuals, pars, N=10000, minsnr=1.5, snrscale=2):
    positive = []
    snrs = []

    # generate some positive examples
    for i in range(10000):

        # choose random sample of time, ensure we have enough data points ``
        length = 0
        while (length != 180):
            di = np.random.choice( np.arange(len(times)) )

            # make sure we get enough data for NN
            ti = np.random.choice( np.arange(len(times[di])) )
            start_t = times[di][ti]
            end_t = times[di][ti] + 2*180/(24*60)
            tmask = (times[di] > start_t) & (times[di] < end_t)
            length = tmask.sum()

        tlength = 0
        # make sure the transit length is sufficient
        while (tlength < 15):
            std = np.std( residuals[di][tmask] )
            snr = minsnr+np.random.uniform()*snrscale
            snrs.append( snr )

            # create parameters for injected data set
            pars['ar'] = np.random.uniform()*7 + 7
            pars['per'] = np.random.uniform()*4 + 2
            pars['tm'] = np.median( times[di][tmask] ) + np.random.random()/24/60
            pars['rp'] = ( np.abs(snr)*std)**0.5

            tmodel = transit(time=times[di][tmask], values=pars)

            if (snr<0):
                tmodel *= -1
                tmodel += 2

            tlength = (tmodel!=1).sum()

        # create data and assess transit probability
        data = residuals[di][tmask] * tmodel
        pdata = preprocessing.scale( data )
        positive.append(pdata)

    return snrs, positive

if __name__ == "__main__":

    data_fp = "TESS"

    try:
        # package results from Pearson 2019

        # loop through TESS data to get residuals
        FILENAME = 'TESS/TESSdata.pkl'
        tess_data = pickle.load(open(FILENAME,'rb'))

        # alloc data arrays
        times = []
        residuals = []; fluxs = []
        tics = []

        # loop through existing TESS data for precomputed
        for index, row in tess_data.iterrows():

            if os.path.exists( row.lcdata ):
                data = pickle.load(open(row.lcdata,'rb'))
                if not 'r' in data: # check to see if residuals have been calculated
                    continue

                tics.append(row.tic_id)
                times.append(data['t'])
                residuals.append(data['r'])
                fluxs.append(data['f'])

        pickle.dump({'tic':tics,'time':times,'residual':residuals, 'fluxs':data['f']}, open('TESS_residual.pkl','wb'))

    except:
        print('loading data from pickle file')
        sv = pickle.load( open('TESS_residual.pkl','rb') )
        residuals = sv['residual']
        times = sv['time']

    # create some random data yo
    pars = {'ar': 15.9187, 'u1': 0.4242, 'u2': 0.1400, 'inc': 89.325, 'ecc': 0, 'ome': 0, 'a0': 1, 'a1': 0, 'a2': 0}

    # create training data
    #negative = generate_negatives(data, residuals, 10000)
    snrs, positive = generate_positives(residuals, pars, 20000, minsnr=1, snrscale=2)

    # create some low snr transits to train on as negative, shouldn't detect a transit beneath the noise
    snrs2, negative  = generate_positives(residuals, pars, 20000, minsnr=-1, snrscale=1.25)

    X = np.vstack([positive, negative])
    X = X.reshape((X.shape[0], X.shape[1], 1))
    y = np.hstack([np.ones(len(positive)), np.zeros(len(negative))])

    nb_epoch = 25
    batch_size = 32
    cnn = make_cnn(180)
    history = cnn.fit(X, y, batch_size=batch_size, epochs=nb_epoch,
              verbose=1, validation_split=0.0, validation_data=None)

    cnn.save_weights('tess_cnn1d.h5')

    score = cnn.evaluate(X, y, verbose=1)
    fn,fp = pn_rates(cnn,X,y)
    print('\nTrain loss:', score[0])
    print('Train accuracy:', score[1])
    print('Train FP:',fp)
    print('Train FN:',fn)

    # test the network
    negative = generate_negatives( residuals, 10000)
    snrs, positive = generate_positives( residuals, pars, 10000, minsnr=0.05, snrscale=2)
    snrs2, negative  = generate_positives( residuals, pars, 10000, minsnr=-1, snrscale=1)
    Xt = np.vstack([positive,negative])
    Xt = Xt.reshape((Xt.shape[0], Xt.shape[1], 1))
    yt = np.hstack([np.ones(len(positive)), np.zeros(len(negative))])

    score = cnn.evaluate(Xt, yt, verbose=1)
    fn,fp = pn_rates(cnn,Xt,yt)
    print('\nTest loss:', score[0])
    print('Test accuracy:', score[1])
    print('Test FP:',fp)
    print('Test FN:',fn)

    y_pos = cnn.predict(Xt[:10000])
    y_neg = cnn.predict(Xt[10000:])

    plt.plot( snrs, y_pos[:,0],'g.',label='Test Positive',alpha=0.5)
    plt.plot( snrs2, y_neg[:,0],'r.',label='Test Negative',alpha=0.5)
    plt.fill_between([1,2],0,1, facecolor='green',label='Training Positive', alpha=0.25)
    plt.fill_between([-1,0.25],0,1, facecolor='purple',label='Training Negative', alpha=0.25)

    plt.xlabel('Transit SNR')
    plt.ylabel('Transit Probability')
    plt.title('CNN Transit Recovery using TESS Noise')

    bsnrs = np.linspace(min(snrs),max(snrs),21)
    bsnr = np.zeros(20)
    bavg = np.zeros(20)
    bstd = np.zeros(20)
    for i in range(bsnr.shape[0]):
        smask = (snrs > bsnrs[i] ) & (snrs<bsnrs[i+1])
        bavg[i] = np.mean( np.array(y_pos)[smask] )
        bsnr[i] = np.mean( np.array(snrs)[smask] )
        bstd[i] = np.std( np.array(y_pos)[smask] )

    plt.errorbar(bsnr,bavg,yerr=bstd,ls='none',marker='.',label='Average',color='black',alpha=0.75)
    #plt.fill_between(bsnr,bavg-bstd,bavg+bstd,alpha=0.75,color='black',label='Average')

    bsnrs = np.linspace(min(snrs2),max(snrs2),13)
    bsnr = np.zeros(12)
    bavg = np.zeros(12)
    bstd = np.zeros(12)
    for i in range(bsnr.shape[0]):
        smask = (snrs2 > bsnrs[i] ) & (snrs2<bsnrs[i+1])
        bavg[i] = np.mean( np.array(y_neg)[smask] )
        bsnr[i] = np.mean( np.array(snrs2)[smask] )
        bstd[i] = np.std( np.array(y_neg)[smask] )

    plt.errorbar(bsnr,bavg,yerr=bstd,ls='none',marker='.',color='black',alpha=0.75)

    #plt.fill_between(bsnr,bavg-bstd,bavg+bstd,alpha=0.75,color='black')
    plt.grid(True,ls='--')
    plt.ylim([0,1])
    plt.xlim([-1,2])
    plt.legend(loc='best')
    plt.show()


from django.urls import path
from . import views

urlpatterns = [
    path('', views.catalog, name='catalog'),
    path('cart/', views.cart, name='cart'),
    path('cart/remove/', views.removeItemFromCart),
    path('cart/checkout/', views.checkout),
    path('cart/checkout/complete/', views.submitOrder),
    path('admin-login/', views.admin_login, name='admin_login'),
    path('admin-dashboard/', views.admin_dashboard, name='admin'),

]


import sys
import gc
import logging
import uos as os
from utime import sleep_us
from ujson import dumps

__version__ = "1.7.2"
ENOENT = 2
_MAX_CLASS_LEVEL = 2  # Max class nesting
# deal with ESP32 firmware specific implementations.
try:
    from machine import resetWDT  # type: ignore  - LoBo specific function
except ImportError:
    # machine.WDT.feed()
    def resetWDT():
        pass

class Stubber:
    "Generate stubs for modules in firmware"

    def __init__(self, path: str = None, firmware_id: str = None):
        try:
            if os.uname().release == "1.13.0" and os.uname().version < "v1.13-103":
                raise NotImplementedError("MicroPython 1.13.0 cannot be stubbed")
        except AttributeError:
            pass

        self._log = logging.getLogger("stubber")
        self._report = []
        self.info = _info()
        gc.collect()
        if firmware_id:
            self._fwid = str(firmware_id).lower()
        else:
            self._fwid = "{family}-{ver}-{port}".format(**self.info).lower()
        self._start_free = gc.mem_free()  # type: ignore

        if path:
            if path.endswith("/"):
                path = path[:-1]
        else:
            path = get_root()

        self.path = "{}/stubs/{}".format(path, self.flat_fwid).replace("//", "/")
        self._log.debug(self.path)
        try:
            ensure_folder(path + "/")
        except OSError:
            self._log.error("error creating stub folder {}".format(path))
        self.problematic = [
            "upip",
            "upysh",
            "webrepl_setup",
            "http_client",
            "http_client_ssl",
            "http_server",
            "http_server_ssl",
        ]
        self.excluded = [
            "webrepl",
            "_webrepl",
            "port_diag",
            "example_sub_led.py",
            "example_pub_button.py",
        ]
        # there is no option to discover modules from micropython, list is read from an external file.
        self.modules = []

    def get_obj_attributes(self, item_instance: object):
        "extract information of the objects members and attributes"
        _result = []
        _errors = []
        self._log.debug("get attributes {} {}".format(repr(item_instance), item_instance))
        for name in sorted(dir(item_instance)):
            try:
                val = getattr(item_instance, name)
                # name , item_repr(value) , type as text, item_instance
                _result.append((name, repr(val), repr(type(val)), val))
            except AttributeError as e:
                _errors.append("Couldn't get attribute '{}' from object '{}', Err: {}".format(name, item_instance, e))
        # remove internal __
        _result = [i for i in _result if not (i[0].startswith("_"))]
        gc.collect()
        return _result, _errors

    def add_modules(self, modules: list):
        "Add additional modules to be exported"
        self.modules = sorted(set(self.modules) | set(modules))

    def rename_package_modules(self, path=None):
        if path is None:
            path = self.path

        for name in os.listdir(path):
            if name.endswith(".py"):
                file_path = path + "/" + name
                pack_path = file_path[:-3]
                try:
                    os.stat(pack_path)
                    os.rename(file_path, pack_path + "/__init__.pyi")
                except Exception:
                    os.rename(file_path, file_path.replace(".py", ".pyi"))
            elif "." not in name: # dir
                self.rename_package_modules(path + "/" + name)

    def create_all_stubs(self):
        "Create stubs for all configured modules"
        self._log.info("Start micropython-stubber v{} on {}".format(__version__, self._fwid))
        gc.collect()
        for module_name in self.modules:
            self.create_one_stub(module_name)
        self._log.info("Finally done")

    def create_one_stub(self, module_name):
        if module_name in self.problematic:
            self._log.warning("Skip module: {:<20}        : Known problematic".format(module_name))
            return False
        if module_name in self.excluded:
            self._log.warning("Skip module: {:<20}        : Excluded".format(module_name))
            return False

        file_name = "{}/{}.py".format(self.path, module_name.replace(".", "/"))
        gc.collect()
        m1 = gc.mem_free()  # type: ignore
        self._log.info("Stub module: {:<20} to file: {:<55} mem:{:>5}".format(module_name, file_name, m1))
        result = False
        try:
            result = self.create_module_stub(module_name, file_name)
        except OSError:
            return False
        gc.collect()
        self._log.debug("Memory     : {:>20} {:>6X}".format(m1, m1 - gc.mem_free()))  # type: ignore
        return result

    def create_module_stub(self, module_name: str, file_name: str = None) -> bool:
        """Create a Stub of a single python module

        Args:
        - module_name (str): name of the module to document. This module will be imported.
        - file_name (Optional[str]): the 'path/filename.py' to write to. If omitted will be created based on the module name.
        """
        if module_name in self.problematic:
            self._log.warning("SKIPPING problematic module:{}".format(module_name))
            return False

        if file_name is None:
            file_name = self.path + "/" + module_name.replace(".", "_") + ".py"

        if "/" in module_name:
            # for nested modules
            module_name = module_name.replace("/", ".")

        # import the module (as new_module) to examine it
        new_module = None
        try:
            new_module = __import__(module_name, None, None, ("*"))
        except ImportError:
            # move one line up to overwrite
            self._log.warning("{}Skip module: {:<20} to file: {:<55}".format("\u001b[1A", module_name, "Failed to import"))
            return False

        # Start a new file
        ensure_folder(file_name)
        with open(file_name, "w") as fp:
            # todo: improve header
            s = '"""\nModule: \'{0}\' on {1}\n"""\n# MCU: {2}\n# Stubber: {3}\n'.format(module_name, self._fwid, self.info, __version__)
            fp.write(s)
            fp.write("from typing import Any\n\n")
            self.write_object_stub(fp, new_module, module_name, "")

        self._report.append({"module": module_name, "file": file_name})

        if not module_name in ["os", "sys", "logging", "gc"]:
            # try to unload the module unless we use it
            try:
                del new_module
            except (OSError, KeyError):  # lgtm [py/unreachable-statement]
                self._log.warning("could not del new_module")
            try:
                del sys.modules[module_name]
            except KeyError:
                self._log.debug("could not del sys.modules[{}]".format(module_name))
        gc.collect()
        return True

    def write_object_stub(self, fp, object_expr: object, obj_name: str, indent: str, in_class: int = 0):
        "Write a module/object stub to an open file. Can be called recursive."
        print(obj_name, object_expr, "------------------------------------")
        gc.collect()
        if object_expr in self.problematic:
            self._log.warning("SKIPPING problematic module:{}".format(object_expr))
            return

        # self._log.debug("DUMP    : {}".format(object_expr))
        items, errors = self.get_obj_attributes(object_expr)
        if errors:
            self._log.error(errors)

        singletons = set()

        if obj_name == "hub":
            import hub
            items.append(("_Button", "", "<class 'type'>", hub.button.center))
            items.append(("_Device", "", "<class 'type'>", hub.port.A.device))
            items.append(("_Motor", "", "<class 'type'>", hub.port.A.motor))
            items.append(("_MotorPair", "", "<class 'type'>", hub.port.A.motor.pair(hub.port.B.motor)))
            hub.port.E.mode(hub.port.MODE_GPIO) # for getting more atts
            import time
            time.sleep(0.5)
            items.append(("_Pin", "", "<class 'type'>", hub.port.E.p5))
            items.append(("_Port", "", "<class 'type'>", hub.port.E))

            items.append(("_battery", "", "<class 'type'>", hub.battery))
            items.append(("_bluetooth", "", "<class 'type'>", hub.bluetooth))
            items.append(("_button", "", "<class 'type'>", hub.button))
            items.append(("_display", "", "<class 'type'>", hub.display))
            items.append(("_motion", "", "<class 'type'>", hub.motion))
            items.append(("_port", "", "<class 'type'>", hub.port))
            items.append(("_sound", "", "<class 'type'>", hub.sound))
            items.append(("_supervision", "", "<class 'type'>", hub.supervision))

        for item_name, item_repr, item_type_txt, item_instance in items:
            # name_, repr_(value), type as text, item_instance
            # do not create stubs for these primitives
            if item_name in ["classmethod", "staticmethod", "BaseException", "Exception"]:
                continue

            # allow the scheduler to run on LoBo based FW
            resetWDT()
            sleep_us(1)

            # Class expansion only on first 3 levels (bit of a hack)
            if item_type_txt == "<class 'type'>" and len(indent) <= _MAX_CLASS_LEVEL * 4:
                self._log.debug("{0}class {1}:".format(indent, item_name))
                superclass = ""
                is_exception = (
                    item_name.endswith("Exception")
                    or item_name.endswith("Error")
                    or item_name
                    in [
                        "KeyboardInterrupt",
                        "StopIteration",
                        "SystemExit",
                    ]
                )
                if is_exception:
                    superclass = "Exception"
                s = "\n{}class {}({}):\n".format(indent, item_name, superclass)
                # s += indent + "    ''\n"
                if is_exception:
                    s += indent + '    """"""\n'
                else:
                    # Add __init__
                    s += indent + "    def __init__(self, *argv, **kwargs) -> None:\n"
                    s += indent + '        """"""\n\n'
                fp.write(s)
                # self._log.debug("\n" + s)

                self._log.debug("# recursion over class {0}".format(item_name))
                self.write_object_stub(
                    fp,
                    item_instance,
                    "{0}.{1}".format(obj_name, item_name),
                    indent + "    ",
                    in_class + 1,
                )
            # Class Methods and functions
            elif "method" in item_type_txt or "function" in item_type_txt:
                self._log.debug("# def {1} function or method, type = '{0}'".format(item_type_txt, item_name))
                # module Function or class method
                # will accept any number of params
                # return type Any
                ret = "Any"
                first = ""
                # Self parameter only on class methods/functions
                if in_class > 0:
                    first = "self, "
                # class method - add function decoration
                if "bound_method" in item_type_txt or "bound_method" in item_repr:
                    s = "{}@classmethod\n".format(indent)
                    s += "{}def {}(cls, *args, **kwargs) -> {}:\n".format(indent, item_name, ret)
                else:
                    s = "{}def {}({}*args, **kwargs) -> {}:\n".format(indent, item_name, first, ret)
                # s += indent + "    ''\n" # EMPTY DOCSTRING
                s += indent + '    """"""\n\n'
                fp.write(s)
                self._log.debug("\n" + s)
            # constants of known types & values
            elif item_type_txt == "<class 'module'>":
                # Skip imported modules
                # fp.write("# import {}\n".format(item_name))
                pass

            elif item_type_txt.startswith("<class '"):

                t = item_type_txt[8:-2]
                s = ""

                if t in ["str", "int", "float", "bool", "bytearray", "bytes"]:
                    # known type: use actual value
                    s = "{0}{1} = {2} # type: {3}\n".format(indent, item_name, item_repr, t)
                elif t in ["dict", "list", "tuple"]:
                    # dict, list , tuple: use empty value
                    ev = {"dict": "{}", "list": "[]", "tuple": "()"}
                    s = "{0}{1} = {2} # type: {3}\n".format(indent, item_name, ev[t], t)
                else:
                    # something else
                    if not t in ["object", "set", "frozenset"]:
                        # Possibly default others to item_instance object ?
                        # https://docs.python.org/3/tutorial/classes.html#item_instance-objects
                        t = "Any"
                    # Requires Python 3.6 syntax, which is OK for the stubs/pyi
                    s = "{0}{1} : {2} ## {3} = {4}\n".format(indent, item_name, t, item_type_txt, item_repr)
                fp.write(s)
                self._log.debug("\n" + s)
            else:
                # keep only the name
                self._log.debug("# all other, type = '{0}'".format(item_type_txt))
                fp.write("# all other, type = '{0}'\n".format(item_type_txt))

                fp.write(indent + item_name + " # type: Any\n")

        del items
        del errors
        try:
            del item_name, item_repr, item_type_txt, item_instance  # pylint: disable=undefined-loop-variable
        except (OSError, KeyError, NameError):  # lgtm [py/unreachable-statement]
            pass

    @property
    def flat_fwid(self):
        "Turn _fwid from 'v1.2.3' into '1_2_3' to be used in filename"
        s = self._fwid
        # path name restrictions
        chars = " .()/\\:$"
        for c in chars:
            s = s.replace(c, "_")
        return s

    def clean(self, path: str = None):
        "Remove all files from the stub folder"
        if path is None:
            path = self.path
        self._log.info("Clean/remove files in folder: {}".format(path))
        try:
            items = os.listdir(path)
        except (OSError, AttributeError):  # lgtm [py/unreachable-statement]
            # os.listdir fails on unix
            return
        for fn in items:
            item = "{}/{}".format(path, fn)
            try:
                os.remove(item)
            except OSError:
                try:  # folder
                    self.clean(item)
                    os.rmdir(item)
                except OSError:
                    pass

    def report(self, filename: str = "modules.json"):
        "create json with list of exported modules"
        self._log.info("Created stubs for {} modules on board {}\nPath: {}".format(len(self._report), self._fwid, self.path))
        f_name = "{}/{}".format(self.path, filename)
        gc.collect()
        try:
            # write json by node to reduce memory requirements
            with open(f_name, "w") as f:
                f.write("{")
                f.write(dumps({"firmware": self.info})[1:-1])
                f.write(",\n")
                f.write(dumps({"stubber": {"version": __version__}, "stubtype": "firmware"})[1:-1])
                f.write(",\n")
                f.write('"modules" :[\n')
                start = True
                for n in self._report:
                    if start:
                        start = False
                    else:
                        f.write(",\n")
                    f.write(dumps(n))
                f.write("\n]}")
            used = self._start_free - gc.mem_free()  # type: ignore
            self._log.info("Memory used: {0} Kb".format(used // 1024))
        except OSError:
            self._log.error("Failed to create the report.")

def ensure_folder(path: str):
    "Create nested folders if needed"
    i = start = 0
    while i != -1:
        i = path.find("/", start)
        if i != -1:
            if i == 0:
                p = path[0]
            else:
                p = path[0:i]
            # p = partial folder
            try:
                _ = os.stat(p)
            except OSError as e:
                # folder does not exist
                if e.args[0] == ENOENT:
                    try:
                        os.mkdir(p)
                    except OSError as e2:
                        # self._log.error("failed to create folder {}".format(p))
                        raise e2
                else:
                    # self._log.error("failed to create folder {}".format(p))
                    raise e
        # next level deep
        start = i + 1

def _info():
    "collect base information on this runtime"
    _n = sys.implementation.name  # type: ignore
    _p = sys.platform if not sys.platform.startswith("pyb") else "stm32"
    info = {
        "name": _n,  # - micropython
        "release": "0.0.0",  # mpy semver from sys.implementation or os.uname()release
        "version": "0.0.0",  # major.minor.0
        "build": "",  # parsed from version
        "sysname": "unknown",  # esp32
        "nodename": "unknown",  # ! not on all builds
        "machine": "unknown",  # ! not on all builds
        "family": _n,  # fw families, micropython , pycopy , lobo , pycom
        "platform": _p,  # port: esp32 / win32 / linux
        "port": _p,  # port: esp32 / win32 / linux
        "ver": "",  # short version
    }
    try:
        info["release"] = ".".join([str(n) for n in sys.implementation.version])
        info["version"] = info["release"]
        info["name"] = sys.implementation.name
        info["mpy"] = sys.implementation.mpy  # type: ignore
    except AttributeError:
        pass

    if sys.platform not in ("unix", "win32"):
        try:
            u = os.uname()
            info["sysname"] = u.sysname
            info["nodename"] = u.nodename
            info["release"] = u.release
            info["machine"] = u.machine
            # parse micropython build info
            if " on " in u.version:
                s = u.version.split(" on ")[0]
                if info["sysname"] == "esp8266":
                    # esp8266 has no usable info on the release
                    if "-" in s:
                        v = s.split("-")[0]
                    else:
                        v = s
                    info["version"] = info["release"] = v.lstrip("v")
                try:
                    info["build"] = s.split("-")[1]
                except IndexError:
                    pass
        except (IndexError, AttributeError, TypeError):
            pass

    try:  # families
        from pycopy import const as _t  # type: ignore

        info["family"] = "pycopy"
        del _t
    except (ImportError, KeyError):
        pass
    try:  # families
        from pycom import FAT as _t  # type: ignore

        info["family"] = "pycom"
        del _t
    except (ImportError, KeyError):
        pass
    if info["platform"] == "esp32_LoBo":
        info["family"] = "loboris"
        info["port"] = "esp32"
    elif info["sysname"] == "ev3":
        # ev3 pybricks
        info["family"] = "ev3-pybricks"
        info["release"] = "1.0.0"
        try:
            # Version 2.0 introduces the EV3Brick() class.
            from pybricks.hubs import EV3Brick  # type: ignore

            info["release"] = "2.0.0"
        except ImportError:
            pass

    # version info
    if info["release"]:
        info["ver"] = "v" + info["release"].lstrip("v")
    if info["family"] == "micropython":
        if info["release"] and info["release"] >= "1.10.0" and info["release"].endswith(".0"):
            # drop the .0 for newer releases
            info["ver"] = info["release"][:-2]
        else:
            info["ver"] = info["release"]
        # add the build nr, but avoid a git commit-id
        if info["build"] != "" and len(info["build"]) < 4:
            info["ver"] += "-" + info["build"]
    if info["ver"][0] != "v":
        info["ver"] = "v" + info["ver"]
    # spell-checker: disable
    if "mpy" in info:  # mpy on some v1.11+ builds
        sys_mpy = int(info["mpy"])
        arch = [
            None,
            "x86",
            "x64",
            "armv6",
            "armv6m",
            "armv7m",
            "armv7em",
            "armv7emsp",
            "armv7emdp",
            "xtensa",
            "xtensawin",
        ][sys_mpy >> 10]
        if arch:
            info["arch"] = arch
    return info
    # spell-checker: enable

def get_root() -> str:
    "Determine the root folder of the device"
    try:
        c = os.getcwd()
    except (OSError, AttributeError):
        # unix port
        c = "."
    r = c
    for r in [c, "/sd", "/flash", "/", "."]:
        try:
            _ = os.stat(r)
            break
        except OSError as e:
            continue
    return r

def show_help():
    print("-p, --path   path to store the stubs in, defaults to '.'")
    sys.exit(1)

def read_path() -> str:
    "get --path from cmdline. [unix/win]"
    path = ""
    if len(sys.argv) == 3:
        cmd = (sys.argv[1]).lower()
        if cmd in ("--path", "-p"):
            path = sys.argv[2]
        else:
            show_help()
    elif len(sys.argv) == 2:
        show_help()
    return path

def isMicroPython() -> bool:
    "runtime test to determine full or micropython"
    # pylint: disable=unused-variable,eval-used
    try:
        # either test should fail on micropython
        # a) https://docs.micropython.org/en/latest/genrst/syntax.html#spaces
        # Micropython : SyntaxError
        # a = eval("1and 0")  # lgtm [py/unused-local-variable]
        # Eval blocks some minification aspects

        # b) https://docs.micropython.org/en/latest/genrst/builtin_types.html#bytes-with-keywords-not-implemented
        # Micropython: NotImplementedError
        b = bytes("abc", encoding="utf8")  # lgtm [py/unused-local-variable]

        # c) https://docs.micropython.org/en/latest/genrst/core_language.html#function-objects-do-not-have-the-module-attribute
        # Micropython: AttributeError
        c = isMicroPython.__module__  # lgtm [py/unused-local-variable]
        return False
    except (NotImplementedError, AttributeError):
        return True

def main():

    stubber = Stubber(path=read_path())
    # stubber = Stubber(path="/sd")
    # Option: Specify a firmware name & version
    # stubber = Stubber(firmware_id='HoverBot v1.2.1')
    stubber.clean()
    # there is no option to discover modules from micropython, need to hardcode
    # below contains combined modules from  Micropython ESP8622, ESP32, Loboris, Pycom and ulab , lvgl
    # spell-checker: disable
    # modules to stub : 131

    extras = [
        "util",
        "util.adjust_motor_offset.lpf2",
        "util.adjust_motor_offset.lpf2.lpf2_logging",
        "util.adjust_motor_offset.lpf2.lpf2_motor_adjust",
        "util.adjust_motor_offset.lpf2.lpf2_motor_eeprom",
        "util.adjust_motor_offset.lpf2.lpf2_packet",
        "util.adjust_motor_offset.lpf2.lpf2_process",
        "util.adjust_motor_offset.AbsoluteMotorAdjust",
        "util.animations",
        "util.color",
        "util.constants",
        "util.error_handler",
        "util.ext_sensor_data",
        "util.log",
        "util.motion",
        "util.motor",
        "util.movement",
        "util.print_override",
        "util.resetter",
        "util.schedule",
        "util.scratch",
        "util.sensors",
        "util.storage",
        "util.time",
        "system",
        "system.",
        "system.callbacks",
        "system.callbacks.customcallbacks",
        "system.abstractwrapper",
        "system.display",
        "system.motors",
        "system.motorwrapper",
        "system.move",
        "system.movewrapper",
        "system.simplemotorwrapper",
        "system.simplemovewrapper",
        "system.sound",
        "version",
        #"ui",
        #"ui.hubui",
    ]

    stubber.modules = [
        "hub",
        "_api",
        "_api.app",
        "_api.button",
        "_api.colorsensor",
        "_api.control",
        "_api.distancesensor",
        "_api.forcesensor",
        "_api.large_technic_hub",
        "_api.lightmatrix",
        "_api.motionsensor",
        "_api.motor",
        "_api.motorpair",
        "_api.operator",
        "_api.speaker",
        "_api.statuslight",
        "_api.util",
        "mindstorms",
        "mindstorms.control",
        "mindstorms.operator",
        "mindstorms.util",
        "spike",
        "spike.control",
        "spike.operator",
        "spike.util",
    ]  # spell-checker: enable

    gc.collect()

    stubber.create_all_stubs()
    stubber.rename_package_modules()
    #stubber.report()

if __name__ == "__main__" or isMicroPython():
    try:
        _log = logging.getLogger("stubber")
        logging.basicConfig(level=logging.INFO)
        # logging.basicConfig(level=logging.DEBUG)
    except NameError:
        pass
    main()


import xml.sax

class MyClass(xml.sax.ContentHandler):
    def __init__(self):
        self.currentElement = ""
        self.name = ""
        self.year = ""
        self.country = ""

    def startElement(self, tag, attributes):
        self.currentElement = tag
        if tag == "singsong":
            self.name = attributes["name"]
            self.year = attributes["year"]
            self.country = attributes["country"]

    def endElement(self, tag):
        if tag == "singsong":
            print("* "*30)
            print(self.name.ljust(30), self.year.rjust(10), self.country.center(20))

#run code
if __name__ == "__main__":
    sax_parser = xml.sax.make_parser()
    handlerObj = MyClass()
    sax_parser.setContentHandler(handlerObj)

    print("Name".ljust(30), "Year".rjust(10), "Country".center(20))
    sax_parser.parse("./data/sing_song_01.xml")


from tqdm import tqdm
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from utils.datasets import getTestDataset
from models.model_tfc import jsccEnd2End_tfc

dataset_root = '/workspace/dataset/cifar-10-batches-py'
test_ds, shape = getTestDataset(dataset_root)

BW = 6
train_SNRs = [1, 4, 7, 13, 19]
plot_color = ['#0072bd', '#d95319', '#edb120', '#7e2f8e', '#77ac30']
plot_marker = ['-*', '-|', '-o', '-s', '-d']
test_SNRs = [1, 4, 7, 10, 13, 16, 19 ,22, 25]

for i, train_SNR in enumerate(train_SNRs):
    model_dir = f'./logs/BW{BW}_{train_SNR}dB/weights'
    avg_PSNR = []

    for test_SNR in tqdm(test_SNRs):
        test_model = jsccEnd2End_tfc(shape, 1/BW, test_SNR)
        test_model.load_weights(model_dir)
        PSNRs = []
        for image in test_ds:
            output = test_model(image)
            PSNRs.append(tf.image.psnr(image, output, max_val=255))
        avg_PSNR.append(np.mean(PSNRs))
    plt.plot(test_SNRs, avg_PSNR, plot_marker[i], color=plot_color[i], label=f'deepJSCC (SNR_train = {train_SNR}dB)')

plt.title(f'AWGN Channel (k/n = 1/{BW}) [self made]')
plt.xlabel('SNR_test (dB)')
plt.ylabel('PSNR (dB)')
plt.axis((0, 25, 20, 36))
plt.legend()
plt.show()


from typing import TYPE_CHECKING, Any, Final

from aiogram import BaseMiddleware

from bot.database.repository.repository import Repository

if TYPE_CHECKING:
    from collections.abc import Awaitable, Callable

    from aiogram.types import TelegramObject
    from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker

class RepositoryMiddleware(BaseMiddleware):
    """Мидлварь для добавления класса-репозитория в аргументы обработчиков телеграма."""

    SESSION_KEY: Final[str] = "session"
    REPOSITORY_KEY: Final[str] = "repo"

    def __init__(
        self,
        session_maker: "async_sessionmaker[AsyncSession]",
        session_key: str = SESSION_KEY,
        repository_key: str = REPOSITORY_KEY,
    ) -> None:
        self.session_maker = session_maker
        self.session_key = session_key
        self.repository_key = repository_key

    async def __call__(
        self,
        handler: "Callable[[TelegramObject, dict[str, Any]], Awaitable[Any]]",
        event: "TelegramObject",
        data: dict[str, Any],
    ) -> Any:
        async with self.session_maker.begin() as session:
            data[self.session_key] = session
            data[self.repository_key] = Repository(session)
            return await handler(event, data)


import argparse
import json
import logging
import random
from pathlib import Path
from typing import (Any, )

import jsonlines
import torch
import numpy as np
from tqdm import tqdm
# from tqdm.contrib.logging import tqdm_logging_redirect
from data_loader import MyDataset

from validator import get_completeness
from validator import get_soundness

# Fix random seeds for reproducibility
SEED = 42
torch.manual_seed(SEED)  # pytorch random seed
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(SEED)  # numpy random seed
random.seed(SEED)  # python random seed

def main(config: dict[str, Any]):

    solution_prefix = Path(config['solution_prefix'])

    # Set variables related to `validate_labeling_config`
    validate_labeling_config = config['validate_labeling']
    logging.info(validate_labeling_config)
    labeled_path = validate_labeling_config['labeled_data']
    testcases_path = validate_labeling_config['testcase']
    output_path = validate_labeling_config['output']
    get_soundness_args = validate_labeling_config['get_soundness']['args']
    get_completeness_args = (
        validate_labeling_config['get_completeness']['args'])

    testcases_dictionary: dict[list[str]] = {}
    with jsonlines.open(testcases_path, 'r') as dataset:
        for data in tqdm(dataset, desc='Loading testcases'):
            name = data['name']
            testcases = data['public_tests']['input']
            # testcases.extend(data['private_tests']['input'])
            # testcases.extend(data['generated_tests']['input'])
            testcases_dictionary[name] = testcases

    soundness = []
    completeness = []

    sound_and_complete_dataset = []
    labeled_dataset = jsonlines.open(labeled_path, 'r')
    for labeled_data in tqdm(labeled_dataset, desc=f'Testing {labeled_path}'):
        grammar = labeled_data['grammar']
        name = labeled_data['name']
        description = labeled_data['description']
        specification = MyDataset.get_specification(description)
        testcases = testcases_dictionary[name]
        solution_dir = solution_prefix / name

        is_sound = get_soundness(
            grammar,
            solution_dir,
            solution_sampling_seed=42,
            name=name,
            specification=specification,
            **get_soundness_args
        )
        is_complete = get_completeness(
            grammar, testcases,
            name=name,
            specification=specification,
            **get_completeness_args
        )
        if is_sound and is_complete:
            sound_and_complete_dataset.append(labeled_data)

        soundness.append(is_sound)
        completeness.append(is_complete)
    correctness = list(map(lambda e: all(e), zip(completeness, soundness)))

    labeled_dataset.close()
    average_soundness = sum(soundness) / len(soundness)
    average_completeness = sum(completeness) / len(completeness)
    average_correctness = sum(correctness) / len(correctness)

    with jsonlines.open(output_path, 'w') as output:
        output.write_all(sound_and_complete_dataset)

    print("Soundness & Completeness & Sound. \\& Compl. \\\\")
    print(f"{average_soundness * 100:.2f} & ", end='')
    print(f"{average_completeness * 100:.2f} & ", end='')
    print(f"{average_correctness * 100:.2f} \\\\")

if __name__ == "__main__":
    logger = logging.getLogger('validator')
    logger.setLevel(logging.ERROR)
    # logger.setLevel(logging.INFO)
    logger.addHandler(logging.FileHandler('validate_labeling.log'))

    parser = argparse.ArgumentParser()
    parser.add_argument('--labeled-data')
    parser.add_argument('--testcase')
    parser.add_argument('--test', action='store_true')
    parser.add_argument('--output', default='/dev/null')
    args = parser.parse_args()

    with open('./config.json') as fp:
        config = json.load(fp)

    data_dir = Path(config['data_dir'])
    unlabeled_test_data = data_dir / config['unlabeled_test_data']
    unlabeled_valid_data = data_dir / config['unlabeled_valid_data']
    testcases_path = (
        unlabeled_test_data if args.test else unlabeled_valid_data)
    trainer_config = config['trainer']
    model_dir = Path(trainer_config['save_dir'])
    labeled_data_base = (
        'model_labeled_test_data.jsonl' if args.test
        else 'model_labeled_valid_data.jsonl'
    )
    labeled_data = model_dir / labeled_data_base

    defaults = {
        'labeled_data': labeled_data,
        'testcase': testcases_path,
        'output': args.output,
    }

    task = 'validate_labeling'
    task_config = config.setdefault(task, {})
    for k in defaults.keys():
        if getattr(args, k, None) is not None:
            task_config[k] = getattr(args, k)
        task_config.setdefault(k, defaults[k])

    main(config)


import logging
from app.session import Session
from isdquantum.utils import misc

logger = logging.getLogger(__name__)

def go():
    ses = Session()
    logger.info("H was\n{0}".format(ses.h))
    logger.info("Syndrome was\n{0}".format(ses.syndrome))
    logger.info("With {} accuracy error is \n{}".format(
        ses.accuracy, ses.error))
    if ses.args.export_qasm_file is not None:
        misc.export_circuit_to_qasm(ses.qc, ses.args.export_qasm_file)
    if ses.args.draw_circuit:
        misc.draw_circuit(ses.qc, ses.args.img_dir)
    if ses.args.draw_dag:
        misc.draw_dag(ses.qc, ses.args.img_dir)


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

import discord
from discord.ext import commands, tasks
import asyncio
from dateutil.relativedelta import relativedelta as rdelta
import traceback
import m10s_util as ut
import textwrap

import config
import aiohttp

from discord import app_commands

class m10s_direct_msg(commands.Cog):

    def __init__(self, bot):
        self.bot: commands.Bot = bot

    @commands.command()
    @commands.is_owner()
    @ut.runnable_check()
    async def send_dm(self, ctx, target_id:int, *, content:str):
        try:
            user = await self.bot.fetch_user(target_id)
        except discord.NotFound:
            await ctx.author.send("該当ユーザーが見つかりませんでした。")
        except discord.HTTPException:
            await ctx.author.send("ユーザーの取得に失敗しました。")
        else:
            m  = await ctx.author.send(f"`{str(user)}`に```\n{content}```と送信してもよろしいですか？")
            await m.add_reaction("⭕")
            await m.add_reaction("❌")
            try:
                r,u = await self.bot.wait_for("reaction_add", timeout=30, check=lambda r,u:u.id == ctx.author.id and str(r.emoji) in ["⭕","❌"])
            except asyncio.TimeoutError:
                await m.edit("タイムアウトしました。再度操作を行ってください。")
            if str(r.emoji) == "⭕":
                try:
                    await user.send(content)
                except:
                    await ctx.author.send("送信に失敗しました。")
                else:
                    await ctx.author.send("送信しました。")
            else:
                await ctx.author.send("キャンセルしました。")

async def setup(bot):
    await bot.add_cog(m10s_direct_msg(bot))


"""
@author: LowinLi
字典处理
"""

import jieba_fast
from tqdm import tqdm
import pandas as pd
import re

jieba_fast.enable_parallel()

def main(file="../data/0609/merge_data_plus_shuffle_lm_512.txt"):
    token_dict = {}
    with open(file, "r") as f:
        lines = f.readlines()
    for line in tqdm(lines):
        tokens = jieba_fast.lcut(line)
        for token in tokens:
            if token in token_dict:
                token_dict[token] += 1
            else:
                token_dict[token] = 1
    df = pd.DataFrame({"word": token_dict.keys(), "count": token_dict.values()})
    pat = re.compile(
        "[\u4e00-\u9fa5]|[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b]"
    )  # 所有中文和中文标点
    df["chinese"] = df["word"].apply(lambda x: bool(pat.search(x)))
    df = df[df["chinese"]]
    df = df.sort_values("count", ascending=False)
    with open("special_tokens.txt", "r") as f:
        vocabs = f.read()

    df["length"] = df["word"].apply(lambda x: len(x))
    # 5000个字和标点
    for word in (df["word"][df["length"] == 1].iloc[:5000]).tolist():
        vocabs += word + "\n"
    # 15000-106个词
    for word in (df["word"][df["length"] > 1].iloc[: 15000 - 106]).tolist():
        vocabs += word + "\n"
    with open("../data/0609/vocab.txt", "w") as f:
        f.write(vocabs)

if __name__ == "__main__":
    main()


from django.conf.urls import url
from django.contrib.auth import views as auth_views
from django.urls import path
from . import views

urlpatterns = [
    url(r'change_password', views.change_password, name='change_password'),
    url('index', views.index, name='index'),
    url('dashboard/', views.dashboard, name='dashboard'),
    url('show_user/', views.show_user, name='show_user'),
    url('show_role/', views.show_role, name='show_role'),
    url('show_permission/', views.show_permission, name='show_permission'),
    url('show_role_to_user/', views.show_role_to_user, name='show_role_to_user'),
    url('show_permission_to_role', views.show_permission_to_role, name='show_permission_to_role'),
    url('add_user/', views.add_user, name='add_user'),
    url('add_role/', views.add_role, name='add_role'),
    url('add_permission/', views.add_permission, name='add_permission'),
    url('add_role_to_user/', views.add_role_to_user, name='add_role_to_user'),
    url('add_permission_to_role', views.add_permission_to_role, name='add_permission_to_role'),
    path('<int:id>/update_user', views.update_user, name='update_user'),
    #path('update_user', views.update_user, name='update_user'),
    path(r'<int:id>/update_role/', views.update_role, name='update_role'),
    path('<int:id>/update_permission/', views.update_permission, name='update_permission'),
    path('<int:id>/update_role_to_user/', views.update_role_to_user, name='update_role_to_user'),
    path('<int:id>/update_permission_to_role', views.update_permission_to_role, name='update_permission_to_role'),
    path('<int:id>/delete_user/', views.delete_user, name='delete_user'),
    path('delete_user/', views.delete_user, name='delete_user'),
    path('<int:id>/delete_role/', views.delete_role, name='delete_role'),
    path('<int:id>delete_permission/', views.delete_permission, name='delete_permission'),
    path('<int:id>/delete_role_to_user/', views.delete_role_to_user, name='delete_role_to_user'),
    path('<int:id>/delete_permission_to_role', views.delete_permission_to_role, name='delete_permission_to_role'),
    url(r'logout/', auth_views.LogoutView.as_view(template_name='myapp/logout.html'), name='logout'),
    url(r'', auth_views.LoginView.as_view(template_name='myapp/login.html'), name='login'),

   #path('hello',views.Hello.as_view()),
]


import requests
import datetime
from database import engine, SessionLocal
from sqlalchemy.orm import Session
import models
import pandas as pd
from sqlalchemy import create_engine, desc

try:
    db = SessionLocal()
    db: Session
finally:
    db.close()

now1 = datetime.datetime.now()
coinList = db.query(models.coinList).filter(models.coinList.delflag == 0).all()

for coin in coinList:
    try:
        now = datetime.datetime.now()

        # csv 파일 읽어오기
        try:
            current_time = now.strftime("%d%H%M")
            df = pd.read_csv(f"./csv/{coin.coin_name}{current_time}.csv")
        except:
            now = now - datetime.timedelta(minutes=1)
            current_time = now.strftime("%d%H%M")
            df = pd.read_csv(f"./csv/{coin.coin_name}{current_time}.csv")

        # 기간 지정
        end_date = pd.to_datetime(current_time)

        # 12일 EMA 계산
        ema12 = df['close'].ewm(span=12).mean()
        # 26일 EMA 계산
        ema26 = df['close'].ewm(span=26).mean()
        # MACD 계산
        macd = ema12 - ema26

        # Signal line 계산
        signal_line = macd.ewm(span=9).mean()

        # MACD histogram 계산
        macd_histogram = macd - signal_line

        disparityMa = df["close"].rolling(window=12).mean()
        disparity = float(disparityMa) / float(df["close"][-1]) * 100

        print(disparityMa)

    except Exception as e:
        print(e, coin.coin_name)

now2 = datetime.datetime.now()
print(now2-now1)


import time
from mtablica import MonitorowanaTablica
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

N = 50  # liczba elementów, można zmieniać
FPS = 60  # klatki na sekundę do parametru interval

tablica = MonitorowanaTablica(0, 1000, N, "T") # zbadaj też opcje: "S", "A", "T"

###############################################
############ Przykład: Quick Sort #########

def quicksort(tablica, start, end):
    if end - start > 1:
        p = partition(tablica, start, end)
        quicksort(tablica, start, p)
        quicksort(tablica, p + 1, end)

def partition(tablica, start, end):
    pivot = tablica[start]
    i = start + 1
    j = end - 1

    while True:
        while (i <= j and tablica[i] <= pivot):
            i = i + 1
        while (i <= j and tablica[j] >= pivot):
            j = j - 1

        if i <= j:
            tablica[i], tablica[j] = tablica[j], tablica[i]
        else:
            tablica[start], tablica[j] = tablica[j], tablica[start]
            return j

algorytm = "QuickSort"
t0 = time.perf_counter()

quicksort(tablica, 0, len(tablica))

delta_t = time.perf_counter() - t0
###############################################
###############################################
print(f"Sortowanie: {algorytm}")
print(f"Tablica posortowana w czasie {delta_t*1000:.1f} ms. Liczba operacji: {len(tablica.pelne_kopie):.0f}.")
###############################################

# konfiguracja wyświetlania histogramu
plt.rcParams["font.size"] = 16
fig, ax = plt.subplots(figsize=(16, 8))
container = ax.bar(np.arange(0, len(tablica), 1), tablica.pelne_kopie[0], align="edge", width=0.8)
fig.suptitle(f"Sortowanie: {algorytm}")
ax.set(xlabel="Indeks", ylabel="Wartość")
ax.set_xlim([0, N])
txt = ax.text(0.01, 0.99, "", ha="left", va="top", transform=ax.transAxes)

# funkcja aktualizująca stan poszczególnych klatek do wyświetlenia
def update(frame):
    txt.set_text(f"Liczba operacji = {frame}")
    for rectangle, height in zip(container.patches, tablica.pelne_kopie[frame]):
        rectangle.set_height(height)
        rectangle.set_color("darkblue")

    idx, op = tablica.aktywnosc(frame)
    if op == "get":
        container.patches[idx].set_color("green")
    elif op == "set":
        container.patches[idx].set_color("red")

    return (txt, *container)

# tu akumulowana jest animacja, wyświetlna komendą show
ani = FuncAnimation(fig, update, frames=range(len(tablica.pelne_kopie)), blit=True, interval=1000./FPS, repeat=False)
plt.show()


from extras.blackjack import logo
import random as r
import subprocess as s

#! My_attempt(failed)

    # cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
    # user_deck = [r.choice(cards), r.choice(cards)]
    # computer_deck = [r.choice(cards), r.choice(cards)]
    # result_of_a_game = ""
    # game_is_not_end = True
    # s.call("clear")
    # ask_for_game = input("Do you want to play some Blackjack? y/n: ").lower()

    # if ask_for_game != "y":
    #     exit("So why you are here?")
    # s.call("clear")
    # print(logo)

    # def blackjack(deck):
    #     result = False
    #     if sum(deck) == 21:
    #         result = True
    #     return result

    # def over_21(deck):
    #     result = False
    #     if sum(deck) > 21:
    #         if 11 in deck:
    #             if sum(deck) - 10 < 21:
    #                 return result
    #             else:
    #                 result = True
    #                 return result
    #         else:
    #             result = True
    #             return result
    #     else:
    #         return result

    # while game_is_not_end:
    #     user_deck = [r.choice(cards), r.choice(cards)]
    #     computer_deck = [r.choice(cards), r.choice(cards)]
    #     if blackjack(computer_deck) == True:
    #         result_of_a_game = "lose"
    #         game_is_not_end = False
    #         print("Computer has blackjack:", computer_deck, "You are", result_of_a_game)

    #     elif blackjack(user_deck) == True:
    #         result_of_a_game = "win"
    #         game_is_not_end = False
    #         print("You have blackjack:", user_deck, "You are", result_of_a_game)

    #     elif over_21(user_deck) == True:
    #         result_of_a_game = "lose"
    #         game_is_not_end = False
    #         print("Computer deck is:", computer_deck, "\nUser deck is:", user_deck, "You are", result_of_a_game)

    #     else:
    #         print("sup")
    #         print("Computer deck is:", computer_deck, "\nUser deck is:", user_deck)
    #         game_is_not_end = False

s.call("clear")

def deal_card():
    """Returns a random card from the deck"""
    cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
    card = r.choice(cards)
    return card

def calculate_score(cards):
    """Take a list of cards an return the score calculated from the cards"""
    if sum(cards) ==21 and len(cards) == 2:
        return 0
    if 11 in cards and sum(cards) > 21:
        cards.remove(11)
        cards.append(1)
    return sum(cards)

def compare(user_score, computer_score):
    if user_score == computer_score:
        return "Draw"
    elif computer_score == 0:
        return "Lose, opponent has Blackjack!"
    elif user_score == 0:
        return "You win with a Blackjack!"
    elif user_score > 21:
        return "You went over. You lose!"
    elif computer_score > 21:
        return "Opponent went over. You win!"
    elif user_score > computer_score:
        return "You win!"
    else:
        return "You lose!"

def play_game():

    user_cards = []
    computer_cards = []
    is_game_over = False
    user_score = 99
    computer_score = 99

    print(logo)
    for _ in range(2):
        user_cards.append(deal_card())
        computer_cards.append(deal_card())

    while not is_game_over:
        user_score = calculate_score(user_cards)
        computer_score = calculate_score(computer_cards)
        print(f"Your cards: {user_cards} current score: {user_score}")
        print(f"Computer's first card: {computer_cards[0]}")

        if user_score == 0 and computer_score == 0 or user_score > 21:
            is_game_over = True
        else:
            user_should_deal = input("Type 'y' to get another card, type 'n' to pass:\t")
            if user_should_deal == "y":
                user_cards.append(deal_card())
            else:
                is_game_over = True

    while computer_score != 0 and computer_score < 17:
        computer_cards.append(deal_card())
        computer_score = calculate_score(computer_cards)

    print(f"    Your final hand: {user_cards}, final_score: {user_score}")
    print(f"    Computer's final hand : {computer_cards}, final_score: {computer_score}")
    print(compare(user_score, computer_score))

while input("Do you want to play the game of Blackjack? Type 'y' or 'n'\t") == 'y':
    s.call("clear")
    play_game()


from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import csv
import os.path

def abstracts_sims(pairsSet, fileName):
    if not os.path.isfile(fileName):

        # Import node information
        with open("./node_information.csv", "r") as f:
            reader = csv.reader(f)
            node_info = list(reader)
        IDs = [element[0] for element in node_info]

        print("Cosine similarities for the abstracts are calculated.")

        corpus = [element[5] for element in node_info]
        vectorizer = TfidfVectorizer(stop_words="english")
        # each row is a node in the order of node_info
        features_TFIDF = vectorizer.fit_transform(corpus)  # compute TFIDF vector of each paper's abstract
        abstractsSimilarities = cosine_similarity(features_TFIDF)

        sims = []

        for i in range(len(pairsSet)):
            source = pairsSet[i][0]  # an ID of edges
            target = pairsSet[i][1]  # an ID of edges

            ## find an index maching to the source and target ID
            index_source = IDs.index(source)
            index_target = IDs.index(target)

            sims.append(abstractsSimilarities[index_source, index_target])

        with open(fileName, 'w') as f:
            csv_out = csv.writer(f)
            for row in sims:
                csv_out.writerow([row])


from django.core.management.base import BaseCommand, CommandError
from waverly.models import Podcast, User, Event
import time

class Command(BaseCommand):
    help = 'Processes a url to get article content and details, and converting text to speech'
    # self.stdout.write("ðŸ‘¾  executing Command (process.py)")
    print ("ðŸ’»  executing Command (process.py)")

    # def add_arguments(self, parser):
        # parser.add_argument('poll_id', nargs='+', type=int)

    def handle(self, *args, **options):
        # for poll_id in options['poll_id']:
        #     try:
        #         poll = Poll.objects.get(pk=poll_id)
        #     except Poll.DoesNotExist:
        #         raise CommandError('Poll "%s" does not exist' % poll_id)
        #
        #     poll.opened = False
        #     poll.save()
        print ('ðŸŒˆ  processing podcast 1/3 [this process should have happened in the background]')
        time.sleep(2)
        print ('ðŸŒˆ  processing podcast 2/3 [this process should have happened in the background]')
        time.sleep(2)
        print ('ðŸŒˆ  processing podcast 3/3 [this process should have happened in the background]')

        self.stdout.write(self.style.SUCCESS('ðŸŒˆ  ðŸŒˆ  ðŸŒˆ  Successfully executed command'))