# -*- coding: utf-8 -*-
import itertools
import multiprocessing
import numpy as np
from scipy.optimize import curve_fit
from scipy.special import erf

def _get_bounds():
    """Define the lower (lb) and upper (ub) boundaries of the fitting
    parameters, being the signal without diffusion weighting (S0), the mean
    diffusivity (MD), the isotropic variance (V_I) and the anisotropic variance
    (V_A).

    Returns
    -------
    lb : list of floats
        Lower boundaries of the fitting parameters.
    ub : list of floats
        Upper boundaries of the fitting parameters.
    """
    S0 = [0, 10]
    MD = [1e-12, 4e-9]
    V_I = [1e-24, 5e-18]
    V_A = [1e-24, 5e-18]
    lb = [S0[0], MD[0], V_I[0], V_A[0]]
    ub = [S0[1], MD[1], V_I[1], V_A[1]]
    return lb, ub

def _random_p0(signal, gtab_infos, lb, ub, weight, n_iter):
    """Produce a guess of initial parameters for the fit, by calculating the
    signals of a given number of random sets of parameters and keeping the one
    closest to the input signal.

    Parameters
    ----------
    signal : np.ndarray
        Diffusion data of a single voxel.
    gtab_infos : np.ndarray
        Contains information about the gtab, such as the unique bvals, the
        encoding types, the number of directions and the acquisition index.
        Obtained as output of the function
        `io.b_tensor.generate_btensor_input`.
    lb : np.ndarray of floats
        Lower boundaries of the fitting parameters.
    ub : np.ndarray of floats
        Upper boundaries of the fitting parameters.
    weight : np.ndarray
        Gives a different weight to each element of `signal`.
    n_iter : int
        Number of random sets of parameters tested.

    Returns
    -------
    guess : np.ndarray
        Array containing the guessed initial parameters.
    """
    guess = []
    thr = np.inf

    for i in range(n_iter):
        params_rand = lb + (ub - lb) * np.random.rand(len(lb))
        signal_rand = gamma_fit2data(gtab_infos, params_rand)
        residual_rand = np.sum(((signal - signal_rand) * weight)**2)

        if residual_rand < thr:
            thr = residual_rand
            guess = params_rand

    return guess

def gamma_data2fit(signal, gtab_infos, fit_iters=1, random_iters=50,
                   do_weight_bvals=False, do_weight_pa=False,
                   do_multiple_s0=False):
    """Fit the gamma model to data

    Parameters
    ----------
    signal : np.array
        Diffusion data of a single voxel.
    gtab_infos : np.ndarray
        Contains information about the gtab, such as the unique bvals, the
        encoding types, the number of directions and the acquisition index.
        Obtained as output of the function
        `io.b_tensor.generate_btensor_input`.
    fit_iters : int, optional
        Number of iterations in the gamma fit. Defaults to 1.
    random_iters : int, optional
        Number of random sets of parameters tested to find the initial
        parameters. Defaults to 50.
    do_weight_bvals : bool , optional
        If set, does a weighting on the bvalues in the gamma fit.
    do_weight_pa : bool, optional
        If set, does a powder averaging weighting in the gamma fit.
    do_multiple_s0 : bool, optional
        If set, takes into account multiple baseline signals.

    Returns
    -------
    best_params : np.array
        Array containing the parameters of the fit.
    """
    if np.sum(gtab_infos[3]) > 0 and do_multiple_s0 is True:
        ns = len(np.unique(gtab_infos[3])) - 1
    else:
        ns = 0

    unit_to_SI = np.array([np.max(signal), 1e-9, 1e-18, 1e-18])
    unit_to_SI = np.concatenate((unit_to_SI, np.ones(ns)))

    def weight_bvals(sthr, mdthr, wthr):
        """Compute an array weighting the different components of the signal
        array based on the bvalue.
        """
        bthr = -np.log(sthr) / mdthr
        weight = 0.5 * (1 - erf(wthr * (gtab_infos[0] - bthr) / bthr))
        return weight

    def weight_pa():
        """Compute an array weighting the different components of the signal
        array based on the number of directions.
        """
        weight = np.sqrt(gtab_infos[2] / np.max(gtab_infos[2]))
        return weight

    def my_gamma_fit2data(gtab_infos, *args):
        """Compute a signal from gtab infomations and fit parameters.
        """
        params_unit = args
        params_SI = params_unit * unit_to_SI
        signal = gamma_fit2data(gtab_infos, params_SI)
        return signal * weight

    lb_SI, ub_SI = _get_bounds()
    lb_SI = np.concatenate((lb_SI, 0.5 * np.ones(ns)))
    ub_SI = np.concatenate((ub_SI, 2.0 * np.ones(ns)))
    lb_SI[0] *= np.max(signal)
    ub_SI[0] *= np.max(signal)

    lb_unit = lb_SI / unit_to_SI
    ub_unit = ub_SI / unit_to_SI

    bounds_unit = ([lb_unit, ub_unit])

    res_thr = np.inf

    for i in range(fit_iters):
        weight = np.ones(len(signal))
        if do_weight_bvals:
            weight *= weight_bvals(0.07, 1e-9, 2)
        if do_weight_pa:
            weight *= weight_pa()

        p0_SI = _random_p0(signal, gtab_infos, lb_SI, ub_SI, weight,
                           random_iters)
        p0_unit = p0_SI / unit_to_SI
        params_unit, params_cov = curve_fit(my_gamma_fit2data, gtab_infos,
                                            signal * weight, p0=p0_unit,
                                            bounds=bounds_unit, method="trf",
                                            ftol=1e-8, xtol=1e-8, gtol=1e-8)

        if do_weight_bvals:
            weight = weight_bvals(0.07, params_unit[1] * unit_to_SI[1], 2)
            if do_weight_pa:
                weight *= weight_pa()

            params_unit, params_cov = curve_fit(my_gamma_fit2data, gtab_infos,
                                                signal * weight,
                                                p0=params_unit,
                                                bounds=bounds_unit,
                                                method="trf")

        signal_fit = gamma_fit2data(gtab_infos, params_unit * unit_to_SI)
        residual = np.sum(((signal - signal_fit) * weight) ** 2)
        if residual < res_thr:
            res_thr = residual
            params_best = params_unit
            # params_cov_best = params_cov

    params_best[0] = params_best[0] * unit_to_SI[0]
    return params_best[0:4]

def gamma_fit2data(gtab_infos, params):
    """Compute a signal from gtab infomations and fit parameters.

    Parameters
    ----------
    gtab_infos : np.ndarray
        Contains information about the gtab, such as the unique bvals, the
        encoding types, the number of directions and the acquisition index.
        Obtained as output of the function
        `io.b_tensor.generate_btensor_input`.
    params : np.array
        Array containing the parameters of the fit.

    Returns
    -------
    signal : np.array
        Array containing the signal produced by the gamma model.
    """
    S0 = params[0]
    MD = params[1]
    V_I = params[2]
    V_A = params[3]
    RS = params[4:]  # relative signal
    if len(RS) != 0:
        RS = np.concatenate(([1], RS))
        RS_tile = np.tile(RS, len(gtab_infos[0])).reshape((len(gtab_infos[0]),
                                                           len(RS)))
        RS_index = np.zeros((len(gtab_infos[0]), len(RS)))
        for i in range(len(gtab_infos[0])):
            j = gtab_infos[3][i]
            RS_index[i][int(j)] = 1
        RS_matrix = RS_tile * RS_index
        SW = S0 * np.sum(RS_matrix, axis=1)
    else:
        SW = S0

    V_D = V_I + V_A * (gtab_infos[1] ** 2)
    signal = SW * ((1 + gtab_infos[0] * V_D / MD) ** (-(MD ** 2) / V_D))

    return np.real(signal)

def gamma_fit2metrics(params):
    """Compute metrics from fit parameters. This is the only function that
    takes the full brain.

    Parameters
    ----------
    params : np.ndarray
        Array containing the parameters of the fit for the whole brain.

    Returns
    -------
    microFA : np.ndarray
        MicroFA values for the whole brain.
    MK_I : np.ndarray
        Isotropic mean kurtosis values for the whole brain.
    MK_A : np.ndarray
        Anisotropic mean kurtosis values for the whole brain.
    MK_T : np.ndarray
        Total mean kurtosis values for the whole brain.
    """
    # S0 = params[..., 0]
    MD = params[..., 1]
    V_I = params[..., 2]
    V_A = params[..., 3]
    V_T = V_I + V_A
    V_L = 5 / 2. * V_A

    MK_I = 3 * V_I / (MD ** 2)
    MK_A = 3 * V_A / (MD ** 2)
    MK_T = 3 * V_T / (MD ** 2)
    microFA2 = (3/2.) * (V_L / (V_I + V_L + (MD ** 2)))
    microFA = np.real(np.sqrt(microFA2))
    microFA[np.isnan(microFA)] = 0

    return microFA, MK_I, MK_A, MK_T

def _fit_gamma_parallel(args):
    data = args[0]
    gtab_infos = args[1]
    fit_iters = args[2]
    random_iters = args[3]
    do_weight_bvals = args[4]
    do_weight_pa = args[5]
    do_multiple_s0 = args[6]
    chunk_id = args[7]

    sub_fit_array = np.zeros((data.shape[0], 4))
    for i in range(data.shape[0]):
        if data[i].any():
            sub_fit_array[i] = gamma_data2fit(data[i], gtab_infos, fit_iters,
                                              random_iters, do_weight_bvals,
                                              do_weight_pa, do_multiple_s0)

    return chunk_id, sub_fit_array

def fit_gamma(data, gtab_infos, mask=None, fit_iters=1, random_iters=50,
              do_weight_bvals=False, do_weight_pa=False, do_multiple_s0=False,
              nbr_processes=None):
    """Fit the gamma model to data

    Parameters
    ----------
    data : np.ndarray (4d)
        Diffusion data, powder averaged. Obtained as output of the function
        `reconst.b_tensor_utils.generate_powder_averaged_data`.
    gtab_infos : np.ndarray
        Contains information about the gtab, such as the unique bvals, the
        encoding types, the number of directions and the acquisition index.
        Obtained as output of the function
        `reconst.b_tensor_utils.generate_powder_averaged_data`.
    mask : np.ndarray, optional
        If `mask` is provided, only the data inside the mask will be
        used for computations.
    fit_iters : int, optional
        Number of iterations in the gamma fit. Defaults to 1.
    random_iters : int, optional
        Number of random sets of parameters tested to find the initial
        parameters. Defaults to 50.
    do_weight_bvals : bool , optional
        If set, does a weighting on the bvalues in the gamma fit.
    do_weight_pa : bool, optional
        If set, does a powder averaging weighting in the gamma fit.
    do_multiple_s0 : bool, optional
        If set, takes into account multiple baseline signals.
    nbr_processes : int, optional
        The number of subprocesses to use.
        Default: multiprocessing.cpu_count()

    Returns
    -------
    fit_array : np.ndarray
        Array containing the fit
    """
    data_shape = data.shape
    if mask is None:
        mask = np.sum(data, axis=3).astype(bool)

    nbr_processes = multiprocessing.cpu_count() if nbr_processes is None \
        or nbr_processes <= 0 else nbr_processes

    # Ravel the first 3 dimensions while keeping the 4th intact, like a list of
    # 1D time series voxels. Then separate it in chunks of len(nbr_processes).
    data = data[mask].reshape((np.count_nonzero(mask), data_shape[3]))
    chunks = np.array_split(data, nbr_processes)

    chunk_len = np.cumsum([0] + [len(c) for c in chunks])
    pool = multiprocessing.Pool(nbr_processes)
    results = pool.map(_fit_gamma_parallel,
                       zip(chunks,
                           itertools.repeat(gtab_infos),
                           itertools.repeat(fit_iters),
                           itertools.repeat(random_iters),
                           itertools.repeat(do_weight_bvals),
                           itertools.repeat(do_weight_pa),
                           itertools.repeat(do_multiple_s0),
                           np.arange(len(chunks))))
    pool.close()
    pool.join()

    # Re-assemble the chunk together in the original shape.
    fit_array = np.zeros((data_shape[0:3])+(4,))
    tmp_fit_array = np.zeros((np.count_nonzero(mask), 4))
    for i, fit in results:
        tmp_fit_array[chunk_len[i]:chunk_len[i+1]] = fit

    fit_array[mask] = tmp_fit_array

    return fit_array


from flask import (render_template, request, redirect, url_for, Blueprint)
from app import (app,  models, db,login_manager)
from app.utils import nocache
from app.forms import loginform
import flask.ext.login as flask_login

loginView = Blueprint('loginView',__name__)

@login_manager.user_loader
def user_loader(user_id):
    try:
        return models.User.query.get(user_id)
    except:
        return None

@login_manager.unauthorized_handler
def unauthorized_handler():
    return redirect(url_for('loginView.login'));

@loginView.route('/login', methods=['GET', 'POST'])
@loginView.route('/index', methods=['GET', 'POST'])
@loginView.route('/', methods=['GET', 'POST'])
@nocache.nocache
def login():
    flask_login.current_user
    error=""
    form = loginform.LoginForm(request.form)
    if flask_login.current_user.is_authenticated:
        return redirect(url_for('dashboardView.dashboard'))

    if form.validate():
        user = models.User.query.filter_by(uname=form.username.data).first()
        if user and user.password == form.password.data:
            if flask_login.login_user(user,remember=form.remember_me.data):
                return redirect(url_for('dashboardView.dashboard'))

        error = "Login Failed"
    return render_template('login.html', form=form, error=error)

@loginView.route('/logout')
@flask_login.login_required
@nocache.nocache
def logout():
    flask_login.logout_user()
    return redirect(url_for('loginView.login'))


import bot_telegram
import pandas as pd

from constants import TELEGRAM_GROUP_OWID

# ------------------------------------- telegram functions -------------------------------------
def telegram_msg(msg='', markdown=0, conf=TELEGRAM_GROUP_OWID):
    sent = 0
    while sent == 0 and conf:
        try:
            if markdown == 0:
                bot_telegram.send(messages=[msg], timeout=15, conf_custom=conf)
            else:
                bot_telegram.send(messages=[msg], timeout=15, parse_mode='markdown', conf_custom=conf)
            sent = 1
        except Exception as e:
            print(e)
            sent = 0

def telegram_image(src=None, caption=None, conf=TELEGRAM_GROUP_OWID):
    sent = 0
    while sent == 0 and conf:
        try:
            with open(src, 'rb') as f:
                bot_telegram.send(images=[f], captions=[caption], timeout=15, conf_custom=conf)
            sent = 1
        except Exception as e:
            print(e)
            sent = 0

def telegram_file(src=None, caption=None, conf=TELEGRAM_GROUP_OWID):
    sent = 0
    while sent == 0 and conf:
        try:
            with open(src, 'rb') as f:
                bot_telegram.send(files=[f], captions=[caption], timeout=15, conf_custom=conf)
            sent = 1
        except Exception as e:
            print(e)
            sent = 0

# ------------------------------------- functions to create customised date x-axis -------------------------------------
def keep_date(tdate, i, type):
    if type == '1m':
        return tdate.strftime('%d-%b') if i % 7 == 0 else ''
    elif type == '6m':
        return tdate.strftime('%d\n%b') if tdate.day == 1 else ''
    elif type == 'all':
        if tdate.day == 1:
            return tdate.strftime('%b\n%y') if tdate.month == 1 else tdate.strftime('%b') if tdate.month in [3, 5, 7, 9, 11] else ''
        else:
            return ''

def gen_date_xticks(date_list, type):
    ticks_dates = [pd.to_datetime(x) for x in date_list]
    ticks_dates.reverse()
    ticks_dates = [keep_date(ticks_dates[i], i, type) for i in range(len(ticks_dates))]
    ticks_dates.reverse()
    return ticks_dates


import threading
import time
import tkinter as tk

# Практика: реализовать приложение с интерфейсом (таймер)
# pip install auto-py-to-exe
# auto-py-to-exe

def start():
    thread = threading.Thread(target=timer, args=(), kwargs={})
    thread.start()

def timer():
    global seconds
    global minutes
    global hours
    global is_play
    is_play = True

    while is_play:
        seconds += 1
        if seconds > 59:
            minutes += 1
            seconds = 0
        if minutes > 59:
            hours += 1
            minutes = 0
            seconds = 0

        time.sleep(0.1)
        label_result.config(text=str(f"{hours:02d}:{minutes:02d}:{seconds:02d}"))  # print(f"{hours:02d}:{minutes:02d}:{seconds:02d}")

def pause():
    global is_play
    is_play = False

def stop():
    global seconds
    global minutes
    global hours
    seconds = 0
    minutes = 0
    hours = 0
    pause()
    label_result.config(text=str(f"{hours:02d}:{minutes:02d}:{seconds:02d}"))

if __name__ == "__main__":
    seconds = 0
    minutes = 0
    hours = 0
    is_play = True

    window = tk.Tk()  # создаётся главный экземпляр объекта

    # настройки окна
    window.title("Таймер")
    window.geometry('640x480')

    # наполнение дизайна
    label_result_title = tk.Label(window, text="Результат:", font=("Arial Bold", 25))
    label_result_title.grid(column=0, row=0)

    label_result = tk.Label(window, text="-", font=("Arial Bold", 25))
    label_result.grid(column=0, row=1)

    button_start = tk.Button(window, text="старт", command=start, font=("Arial Bold", 25))
    button_start.grid(column=0, row=2)

    button_pause = tk.Button(window, text="пауза", command=pause, font=("Arial Bold", 25))
    button_pause.grid(column=1, row=2)

    button_stop = tk.Button(window, text="стоп", command=stop, font=("Arial Bold", 25))
    button_stop.grid(column=2, row=2)

    # цикл событий - отрисовка окна
    window.mainloop()


from rest_framework import serializers
from .models import Category, Product, Order, Product_Order, Receipt

class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = '__all__'

class ProductSerializer(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = '__all__'

class OrderSerializer(serializers.ModelSerializer):
    class Meta:
        model = Order
        fields = '__all__'

class Product_OrderSerializer(serializers.ModelSerializer):
    class Meta:
        #        depth = 1 #안의 값 참조할 때 저걸 사용해주면 된다.
        model = Product_Order
        fields = '__all__'

class ProductReceiptSerializer(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = ('product_name', 'product_detail', 'price', 'quantity')

class OrderReceiptSerializer(serializers.ModelSerializer):
    class Meta:
        model = Order
        fields = ('id', 'payment', 'is_takeout',
                  'total_price', 'products')

    def create(self, validated_data):
        products_data = validated_data.pop('products')
        order = Order.objects.create(**validated_data)

        for product_data in products_data:
            Product_Order.objects.create(order=order, **product_data)

        Receipt.objects.create(order=order)

        return order

class ReceiptSerializer(serializers.ModelSerializer):

    order = serializers.SerializerMethodField()
    products = serializers.SerializerMethodField()

    class Meta:
        model = Receipt
        fields = ('id', 'order', 'products')

    def get_order(self, obj):
        order = {
            "id": obj.order.id,
            "payment": obj.order.payment,
            "is_takeout": obj.order.is_takeout,
            "total_price": obj.order.total_price,
        }
        return order

    def get_products(self, obj):
        # 동일한 order id를 가진 product_order를 가져오게끔 필터링
        products_order = Product_Order.objects.filter(order=obj.order.id)
        products = [{
            "id": product_order.product.id,
            "product_name": product_order.product.product_name,
            "product_detail": product_order.product.product_detail,
            "price": product_order.product.price,
            "is_soldout": product_order.product.is_soldout,
            "category": product_order.product.category.id,
            "quantity": product_order.quantity,
        } for product_order in products_order]
        return products

# POST /api/product-order/

# 	{
# 	"products" : [
# 		{
# 			"id" : 1, #상품별 아이디
# 			"quantity" : 2 #상품주문갯수
# 		},
# 		{
# 			"id" : 2,
# 			"quantity" : 1
# 		},
# 		...
# 	],
# 	"payment": "카드",
#     "is_takeout": false,
#     "total_price": 5000,
# }


"""
Command-line tool for starting an explainerdashboard from a particular directory
"""
import os
import webbrowser
from pathlib import Path
from importlib import import_module
from copy import deepcopy
import pickle
import oyaml as yaml

import pandas as pd
import joblib
import click

from explainerdashboard import ClassifierExplainer, RegressionExplainer
from explainerdashboard.explainers import BaseExplainer
from explainerdashboard.dashboards import ExplainerDashboard

explainer_ascii = """

                 _       _                    _           _     _                         _
                | |     (_)                  | |         | |   | |                       | |
  _____  ___ __ | | __ _ _ _ __   ___ _ __ __| | __ _ ___| |__ | |__   ___   __ _ _ __ __| |
 / _ \ \/ | '_ \| |/ _` | | '_ \ / _ | '__/ _` |/ _` / __| '_ \| '_ \ / _ \ / _` | '__/ _` |
|  __/>  <| |_) | | (_| | | | | |  __| | | (_| | (_| \__ | | | | |_) | (_) | (_| | | | (_| |
 \___/_/\_| .__/|_|\__,_|_|_| |_|\___|_|  \__,_|\__,_|___|_| |_|_.__/ \___/ \__,_|_|  \__,_|
          | |
          |_|
"""

def build_explainer(explainer_config):
    if isinstance(explainer_config, (Path, str)) and str(explainer_config).endswith(".yaml"):
        config = yaml.safe_load(open(str(explainer_config), "r"))
    elif isinstance(explainer_config, dict):
        config = explainer_config
    assert 'explainer' in config, \
        "Please pass a proper explainer.yaml config file that starts with `explainer:`!"
    config = explainer_config['explainer']

    print(f"explainerdashboard ===> Loading model from {config['modelfile']}")
    model = pickle.load(open(config['modelfile'], "rb"))

    print(f"explainerdashboard ===> Loading data from {config['datafile']}")
    if str(config['datafile']).endswith('.csv'):
        df = pd.read_csv(config['datafile'])
    elif str(config['datafile']).endswith('.parquet'):
        df = pd.read_parquet(config['datafile'])
    else:
        raise ValueError("datafile should either be a .csv or .parquet!")

    print(f"explainerdashboard ===> Using column {config['data_target']} to generate X, y ")
    target_col = config['data_target']
    X = df.drop(target_col, axis=1)
    y = df[target_col]

    if config['data_index'] is not None:
        print(f"explainerdashboard ===> Generating index from column {config['data_index']}")
        assert config['data_index'] in X.columns, \
            (f"Cannot find data_index column ({config['data_index']})"
             f" in datafile ({config['datafile']})!"
              "Please set it to the proper index column name, or set it to null")
        X = X.set_index(config['data_index'])

    params = config['params']

    if config['explainer_type'] == "classifier":
        print(f"explainerdashboard ===> Generating ClassifierExplainer...")
        explainer = ClassifierExplainer(model, X, y, **params)
    elif config['explainer_type'] == "regression":
        print(f"explainerdashboard ===> Generating RegressionExplainer...")
        explainer = ClassifierExplainer(model, X, y, **params)
    return explainer

def build_and_dump_explainer(explainer_config, dashboard_config=None):
    explainer = build_explainer(explainer_config)

    click.echo(f"explainerdashboard ===> Calculating properties by building Dashboard...")
    if dashboard_config is not None:
        ExplainerDashboard.from_config(explainer, dashboard_config)
    elif Path(explainer_config['explainer']['dashboard_yaml']).exists():
        click.echo(f"explainerdashboard ===> Calculating properties by building Dashboard from {explainer_config['explainer']['dashboard_yaml']}...")
        dashboard_config = yaml.safe_load(open(str(explainer_config['explainer']['dashboard_yaml']), "r"))
        ExplainerDashboard.from_config(explainer, dashboard_config)
    else:
        click.echo(f"explainerdashboard ===> Calculating all properties")
        explainer.calculate_properties()

    click.echo(f"explainerdashboard ===> Saving explainer to {explainer_config['explainer']['explainerfile']}...")
    if (dashboard_config is not None and
        explainer_config['explainer']['explainerfile'] != dashboard_config['dashboard']['explainerfile']):
        click.echo(f"explainerdashboard ===> Warning explainerfile in explainer config and dashboard config do not match!")
    explainer.dump(explainer_config['explainer']['explainerfile'])
    return

def launch_dashboard_from_pkl(explainer_filepath, no_browser, port, no_dashboard=False):
    explainer = BaseExplainer.from_file(explainer_filepath)

    if port is None:
        click.echo(f"explainerdashboard ===> Setting port to 8050, override with e.g. --port 8051")
        port = 8050

    db = ExplainerDashboard(explainer, port=port)

    if not no_browser and not os.environ.get("WERKZEUG_RUN_MAIN"):
        webbrowser.open_new(f"http://127.0.0.1:{port}/")

    if not no_dashboard:
        db.run(port)
    return

def launch_dashboard_from_yaml(dashboard_config, no_browser, port, no_dashboard=False):
    if isinstance(dashboard_config, (Path, str)) and str(dashboard_config).endswith(".yaml"):
        config = yaml.safe_load(open(str(dashboard_config), "r"))
    elif isinstance(dashboard_config, dict):
        config = dashboard_config
    else:
        raise ValueError(f"dashboard_config should either be a .yaml filepath or a dict!")

    if not Path(config['dashboard']['explainerfile']).exists():
        click.echo(f"explainerdashboard ===> {config['dashboard']['explainerfile']} does not exist!")
        click.echo(f"explainerdashboard ===> first generate {config['dashboard']['explainerfile']} with explainerdashboard build")
        return

    click.echo(f"explainerdashboard ===> Building dashboard from {config['dashboard']['explainerfile']}")

    db = ExplainerDashboard.from_config(config)

    if port is None:
        port =  config['dashboard']['params']['port']
        if port is None:
            port = 8050
        click.echo(f"explainerdashboard ===> Setting port to {port}, override with e.g. --port 8051")

    if not no_browser and not os.environ.get("WERKZEUG_RUN_MAIN"):
        click.echo(f"explainerdashboard ===> launching browser at {f'http://localhost:{port}/'}")
        webbrowser.open_new(f"http://localhost:{port}/")

    click.echo(f"explainerdashboard ===> Starting dashboard:")
    if not no_dashboard:
        db.run(port)
    return

@click.group()
@click.pass_context
def explainerdashboard_cli(ctx):
    """
    explainerdashboard CLI tool. Used to launch an explainerdashboard from
    the commandline.

    \b
    explainerdashboard run
    ----------------------

    Run explainerdashboard and start browser directly from command line.

    \b
    Example use:
        explainerdashboard run explainer.joblib
        explainerdashboard run dashboard.yaml
        explainerdashboard run dashboard.yaml --no-browser --port 8051
        explainerdashboard run --help

    If you pass an explainer.joblib file, will launch the full default dashboard.
    Generate this file with explainer.dump("explainer.joblib")

    If you pass a dashboard.yaml file, will launch a fully configured
    explainerdashboard. Generate dashboard.yaml with
    ExplainerDashboard.to_yaml('dashboard.yaml')

    If no argument given, searches for either dashboard.yaml or
    explainer.joblib, in that order, so if you keep that naming convention
    you can simply start with:

    \b
        explainerdashboard run

    \b
    explainerdashboard build
    ------------------------

    Build and store an explainer object, based on explainer.yaml file, that indicates
    where to find stored model (e.g. model.pkl), stored datafile (e.g. data.csv),
    and other explainer parameters.

    \b
    Example use:
        explainerdashboard build explainer.yaml
        explainerdashboard build explainer.yaml dashboard.yaml
        explainerdashboard build --help

    If given a second dashboard.yaml argument, will use that dashboard
    configuration to calculate necessary properties for that specific dashboard
    configuration before storing to disk. Otherwise will use dashboard_yaml
    parameter in explainer.yaml to find configuration, or alternatively
    simply calculate all properties.

    explainer.yaml file can be generated with explainer.to_yaml("explainer.yaml")

    If no argument given, searches for explainer.yaml, so if you keep that
    naming convention you can simply start the build with:

    \b
        explainerdashboard build

    """

@explainerdashboard_cli.command(help="run dashboard and open browser")
@click.pass_context
@click.argument("explainer_filepath", nargs=1, required=False)
@click.option("--no-browser", "-nb", "no_browser", is_flag=True,
                 help="Launch a dashboard, but do not launch a browser.")
@click.option("--port", "-p", "port", default=None,
                help="port to run dashboard on defaults.")
def run(ctx, explainer_filepath, no_browser, port):
    click.echo(explainer_ascii)
    if explainer_filepath is None:
        if (Path().cwd() / "dashboard.yaml").exists():
            explainer_filepath = Path().cwd() / "dashboard.yaml"
        elif (Path().cwd() / "explainer.joblib").exists():
            explainer_filepath = Path().cwd() / "explainer.joblib"
        else:
            click.echo("No argument given and could find neither a "
                    "dashboard.yaml nor a explainer.joblib. Aborting.")
            return

    if (str(explainer_filepath).endswith(".joblib") or
        str(explainer_filepath).endswith(".pkl") or
        str(explainer_filepath).endswith(".pickle") or
        str(explainer_filepath).endswith(".dill")):
        launch_dashboard_from_pkl(explainer_filepath, no_browser, port)
        return
    elif str(explainer_filepath).endswith(".yaml"):
        launch_dashboard_from_yaml(explainer_filepath, no_browser, port)
    else:
        click.echo("Please pass a proper argument to explainerdashboard run"
                    "(i.e. either an explainer.joblib or a dashboard.yaml)")
    return

@explainerdashboard_cli.command(help="build and save explainer object")
@click.pass_context
@click.argument("explainer_filepath", nargs=1, required=False)
@click.argument("dashboard_filepath", nargs=1, required=False)
def build(ctx, explainer_filepath, dashboard_filepath):
    click.echo(explainer_ascii)
    if explainer_filepath is None:
        if (Path().cwd() / "explainer.yaml").exists():
            explainer_filepath = Path().cwd() / "explainer.yaml"
        else:
            click.echo("No argument given to explainerdashboard build and "
                        "could not find an explainer.yaml. Aborting.")
            return

    if str(explainer_filepath).endswith(".yaml"):
        explainer_config = yaml.safe_load(open(str(explainer_filepath), "r"))

        click.echo(f"explainerdashboard ===> Building {explainer_config['explainer']['explainerfile']}")

        if (dashboard_filepath is not None and
            str(dashboard_filepath).endswith(".yaml")
            and Path(dashboard_filepath).exists()):
            click.echo(f"explainerdashboard ===> Using {dashboard_filepath} to calculate explainer properties")
            dashboard_config = yaml.safe_load(open(str(dashboard_filepath), "r"))
        else:
            dashboard_config = None

        print(f"explainerdashboard ===> Building {explainer_config['explainer']['explainerfile']}")
        build_and_dump_explainer(explainer_config, dashboard_config)
        print(f"explainerdashboard ===> Build finished!")
        return

@explainerdashboard_cli.command(help="run without launching dashboard")
@click.pass_context
@click.argument("explainer_filepath", nargs=1, required=True)
@click.option("--port", "-p", "port", default=None,
                help="port to run dashboard on defaults.")
def test(ctx, explainer_filepath, port):
    if (str(explainer_filepath).endswith(".joblib") or
        str(explainer_filepath).endswith(".pkl") or
        str(explainer_filepath).endswith(".pickle") or
        str(explainer_filepath).endswith(".dill")):
        launch_dashboard_from_pkl(explainer_filepath,
            no_browser=True, port=port, no_dashboard=True)
        return

    elif str(explainer_filepath).endswith(".yaml"):
        launch_dashboard_from_yaml(explainer_filepath,
            no_browser=True, port=port, no_dashboard=True)
        return
    else:
        raise ValueError("Please pass a proper argument "
                    "(i.e. .joblib, .pkl, .dill or .yaml)!")

if __name__ =="__main__":
    explainerdashboard_cli()


from django.contrib.sites.models import Site
from django.shortcuts import redirect
from main.models import *
from main.utils import Access
from settings import POST_RATE_COEFFICIENT, BLOG_RATE_COEFFICIENT, COMMENT_RATE_COEFFICIENT, MENU_CACHE_TIME, SIDEBAR_CACHE_TIME, LANGUAGE_CODE, SITENAME, TIME_ZONE, KEYWORD_MIN_COUNT, API_KEY, FEED_URL
import random, time
from django.contrib.auth.decorators import login_required
from itertools import imap, islice

def get_objects():
    posts = Post.objects.order_by('-date').select_related('author').all()[0:][:20]
    comments = Comment.objects.select_related('post', 'author').order_by('-created').exclude(depth=1).all()[0:][:20]
    objects = [
        {
            'type': 'post',
            'date': post.date,
            'object': post
        }   for post in posts
    ]
    objects += [
        {
            'type': 'comment',
            'date': comment.created,
            'object': comment
        } for comment in comments
    ]
    objects.sort(key = lambda element: element['date'], reverse = True)
    return objects

def djbyte(request):
    """Get special variables into template"""
    rate = None
    timezone = TIME_ZONE
    lenta_events_count = 0
    if request.user.is_authenticated():
        try:
            profile = Profile.objects.get(user=request.user)
            profile.update_last_visit()
            rate = profile.get_rate()
            timezone = profile.timezone
        except Profile.DoesNotExist:
            pass
        try:
            lenta_events_count = LentaLastView.objects.get(user=request.user).get_unseen_count()
        except LentaLastView.DoesNotExist:
            pass

    try:
        profiles = imap(lambda profile: {
                'name': profile.user.username,
                'rate': profile.get_rate()
            },
            Profile.objects.select_related('user').extra(select={
                'fullrate': 'rate+%f*posts_rate+%f*blogs_rate+%f*comments_rate'
                    % (POST_RATE_COEFFICIENT, BLOG_RATE_COEFFICIENT, COMMENT_RATE_COEFFICIENT),
            },
            order_by=['-fullrate',])[0:][:10]
        )
    except Profile.DoesNotExist:
        profiles = []
    blogs = Blog.objects.order_by('-rate')[0:][:10]
    try:
        type = request.session['right_panel']
        right_panel_js = "fast_funcs['%s']()" % (type)
    except KeyError:
        right_panel_js = None
    online = LastVisit.get_online()
    last_users = User.objects.order_by('-id')[:10]
    return({
        'your_rate': rate,
        'top_post_comment': islice(get_objects(), 20),
        'top_profiles': profiles,
        'top_blogs': blogs,
        'blogs_count': Blog.objects.count(),
        'profiles_count': Profile.objects.count(),
        'TIMEZONE': timezone,
        'city_count': City.objects.count(),
        'MENU_CACHE_TIME': MENU_CACHE_TIME,
        'SIDEBAR_CACHE_TIME': SIDEBAR_CACHE_TIME,
        'LANGUAGE_CODE': LANGUAGE_CODE,
        'SITENAME': SITENAME,
        'RIGHT_PANEL_JS': right_panel_js,
        'API_KEY': API_KEY,
        'keywords': lambda: ', '.join(x.__unicode__() for x in Tag.objects.cloud_for_model(
            Post, min_count=KEYWORD_MIN_COUNT
        )[:10]),
        'FEED_URL': FEED_URL,
        'ONLINE': online,
        'LAST_USERS': last_users,
        'SITE_DOMAIN': Site.objects.get_current().domain,
        'lenta_events_count' : lenta_events_count,
    })

@login_required
def permission(request):
    profile = request.user.get_profile()
    return {
        'PERM_DELETE_POST': request.user.has_perm('main.delete_post'),
        'PERM_EDIT_POST': request.user.has_perm('main.change_post'),
        'PERM_CREATE_POST': profile.check_access(Access.new_post),
        'PERM_DELETE_COMMENT': request.user.has_perm('main.delete_comment'),
        'PERM_EDIT_COMMENT': request.user.has_perm('main.change_comment'),
        'PERM_CREATE_COMMENT': profile.check_access(Access.new_comment),
        'PERM_DELETE_BLOG': request.user.has_perm('main.delete_blog'),
        'PERM_EDIT_BLOG': request.user.has_perm('main.change_blog'),
        'PERM_CREATE_BLOG': profile.check_access(Access.new_blog),
    }


inlist = [ int(i) for i in open("INPUT.TXT").read().split()[1:] ];
stage = list();
outfile = open("OUTPUT.TXT", "w");

i = 0;
while i < len(inlist):
	action = inlist[i];
	if action == 1:
		stage.insert(0, inlist[i+1]);
		i += 1;
	elif action == 2:
		stage.append(inlist[i+1]);
		i += 1;
	elif action == 3:
		outfile.write( str(stage[0]) + ' ' );
		stage.pop(0);
	else:
		outfile.write( str(stage[-1]) + ' ' );
		stage.pop();
	i += 1;

acmp/arch/8.Структуры-данных/0946.Полка/main.py


def get_key(file_location, bot_name):
    """This function returns key values of discord bots assuming keys are inputed as Key: Value"""

    with open(str(file_location),'r') as f:
        for line in f:
            if bot_name in line:
                bot_info = line

    for x in bot_info.split(' '):
        if bot_name not in x:
            key = x

    f.close()

    return key


"""
Code illustration: 3.10.py

    1. tkinter versus ttk Themed Widgets
    2. new widgets introduced in ttk

Chapter 3 : Programmable Drum Machine
Tkinter GUI Application Development Blueprints
"""

from tkinter import Tk, Button, Label, Checkbutton, Entry, PanedWindow, \
            Radiobutton, Scale, VERTICAL, HORIZONTAL, W
from tkinter import ttk

root = Tk()

style = ttk.Style()

print(style.theme_names())

# style.theme_use('default')

root.title('Tkinter Versus ttk Themed Widgets')

ttk.Separator(root, orient=VERTICAL).grid(
    row=0, rowspan=8, column=1, sticky="wns")

Label(root, text='Tkinter    Versus').grid(row=0, columnspan=2, sticky='ew')
ttk.Label(root, text='ttk').grid(row=0, column=1)

Button(root, text='tk Button').grid(row=1, column=0)
ttk.Button(root, text='ttk Button').grid(row=1, column=1)

Checkbutton(root, text='tk CheckButton').grid(row=2, column=0)
ttk.Checkbutton(root, text='ttk CheckButton').grid(row=2, column=1)

Entry(root).grid(row=3, column=0)
ttk.Entry(root).grid(row=3, column=1)

PanedWindow(root).grid(row=4, column=0)
ttk.PanedWindow(root).grid(row=4, column=1)

Radiobutton(root, text='tk Radio').grid(row=5, column=0)
ttk.Radiobutton(root, text='ttk Radio').grid(row=5, column=1)

Scale(root, orient=HORIZONTAL).grid(row=6, column=0)
ttk.Scale(root).grid(row=6, column=1)

ttk.Separator(root, orient=HORIZONTAL).grid(row=7, columnspan=2, sticky="ew")
ttk.Label(root, text='NEW WIDGETS INTRODUCED IN ttk').grid(row=8, columnspan=2)
ttk.Separator(root, orient=HORIZONTAL).grid(row=9, columnspan=2, sticky="ew")

ttk.Combobox(root).grid(row=11, column=0)

my_notebook = ttk.Notebook(root)
my_notebook.grid(row=12, column=1)
frame_1 = ttk.Frame(my_notebook)
frame_2 = ttk.Frame(my_notebook)
my_notebook.add(frame_1, text='Tab One')
my_notebook.add(frame_2, text='Tab Two')

ttk.Progressbar(root, length=140, value=65).grid(row=13, column=0)

my_tree = ttk.Treeview(root, height=2, columns=2)
my_tree.grid(row=14, columnspan=2)
my_tree.heading('#0', text='Column A', anchor=W)
my_tree.heading(2, text='Column B', anchor=W)
my_tree.column(2, stretch=0, width=70)

root.mainloop()


import requests
from bs4 import BeautifulSoup
import os
import json

def make_soup(url):
    html_content = requests.get(url)
    html_content.raise_for_status()
    parser = BeautifulSoup(html_content.text, 'html.parser')
    return parser

def find_table(soup):
    table = soup.find("table", class_="picklist-dataTable")
    table_rows = table.find_all('tr')[1:]
    return table_rows

def check_key_value(value, array):
    for i, v in enumerate(array):
            if v["form_number"] == value:
                return True

def save_as_pdf(form_number, pdf, form_year):

        dir_name= str(form_number)
        if not os.path.exists(dir_name):
            os.makedirs(dir_name)

        res = requests.get(pdf)
        with open(f'{dir_name}/{form_number} - {form_year}.pdf', 'wb') as f:
            f.write(res.content)

def main():
    textfile_results = []

    MIN_RANGE = 2018
    MAX_RANGE = 2020

    with open('test.txt') as f:
        textfile_results = [line.rstrip() for line in f]

    for results in textfile_results:
        format = results.split()
        url = f'https://apps.irs.gov/app/picklist/list/priorFormPublication.html?resultsPerPage=200&sortColumn=sortOrder&indexOfFirstRow=0&criteria=formNumber&value={format[0]}+{format[1]}&isDescending=false'

         #retrieves html content and creates parser
        soup = make_soup(url)

        #retrieves rows within table data
        table_rows = find_table(soup)

        tax_forms=[]

        #FIRST UTILITY
        for row in table_rows:
            form_number = row.find('td', 'LeftCellSpacer').text.strip()
            if form_number == results:
                form_title = row.find('td', 'MiddleCellSpacer').text.strip()
                form_year = int(row.find('td', 'EndCellSpacer').text.strip())
                res = check_key_value(value=form_number, array = tax_forms) #Checks if respective form number value exists
                if (res):
                    if form_year < tax_forms[0]["min_year"]:
                        tax_forms[0]["min_year"] = form_year
                    elif form_year > tax_forms[0]["max_year"]:
                        tax_forms[0]["max_year"] = form_year
                else:
                    tax_forms.append({
                        "form_number":form_number,
                        "form_title":form_title,
                        "min_year": form_year,
                        "max_year": form_year
                        })

                if MIN_RANGE <= form_year <= MAX_RANGE:
                    for link in row.find_all('a'):
                            pdf = link.get('href')

                    #SECOND UTILITY
                    data = save_as_pdf(pdf=pdf,form_number=form_number,form_year=form_year)
        final_results = json.dumps(tax_forms)
        print(final_results)

if __name__ == '__main__':
    main()


import itertools
import re

from datetime import datetime

from aiogram.dispatcher.middlewares import BaseMiddleware
from aiogram.dispatcher.handler import CancelHandler
from aiogram import Bot, types

from database import DataBase, models
from modules import Config, tools
from data import settings, texts

PRIVILEGE = tools.get_privilege()
CHECK_COMMANDS = set(itertools.chain.from_iterable(
    _privilege.get('scope') for _privilege in PRIVILEGE.values()
))

db = DataBase(Config.DATABASE_URL)
bot = Bot.get_current()

class CheckPrivilege(BaseMiddleware):

    async def on_process_message(self, message: types.Message, *_) -> None:

        text = (message.text or message.caption or '')
        user_id = message.from_user.id
        chat_type = message.chat.type
        chat_id = message.chat.id

        match_command = re.search(r'(?<=\/)(\w*?)(?=@|$)', text, re.IGNORECASE)
        command = match_command.group() if match_command is not None else None

        if command in CHECK_COMMANDS and command not in settings.EXCLUDED_COMMANDS:
            if chat_type in ('group', 'supergroup'):
                await self._check_group_privilege(chat_id, user_id, command)
            elif chat_type == 'private':
                await self._check_user_privilege(user_id, command)

    async def _check_user_privilege(self, user_id: int, command: str) -> None:
        user = db.get_data(models.User, id=user_id)

        now = datetime.now()

        if user.expires_in <= now:
            await bot.send_message(user_id, texts.COMMAND_NOT_AVAILABLE)
            raise CancelHandler
        if not self._check_command_privilege(user.privilege, command):
            await bot.send_message(user_id, texts.COMMAND_NOT_AVAILABLE)
            raise CancelHandler

    async def _check_group_privilege(self, group_id: int, user_id: int, command: str) -> None:
        group = db.get_data(models.Group, id=group_id)
        user = db.get_data(models.User, id=user_id)

        now = datetime.now()

        if user is None:
            await bot.send_message(group_id, texts.START_BOT)
            raise CancelHandler
        if group.expires_in <= now and user.expires_in <= now:
            await bot.send_message(group_id, texts.COMMAND_NOT_AVAILABLE)
            raise CancelHandler
        if (
            not self._check_command_privilege(user.privilege, command) and
            not self._check_command_privilege(group.privilege, command)
        ):
            await bot.send_message(group_id, texts.COMMAND_NOT_AVAILABLE)
            raise CancelHandler

    def _check_command_privilege(self, privilege: str, command: str) -> bool:
        data = PRIVILEGE.get(privilege)

        if data and command in data['scope']:
            return True
        return False


from django import forms
from .models import Book, Member, Transaction

class BookForm(forms.ModelForm):
    class Meta:
        model = Book
        fields = '__all__'

class MemberForm(forms.ModelForm):
    class Meta:
        model = Member
        fields = '__all__'

class TransactionForm(forms.ModelForm):
    class Meta:
        model = Transaction
        fields = '__all__'

class BookImportForm(forms.Form):
    num_books = forms.IntegerField(
        label='Number of Books to Import',
        min_value=1,
        widget=forms.NumberInput(attrs={'class': 'form-control'})
    )
    title = forms.CharField(
        label='Title',
        required=False,
        widget=forms.TextInput(attrs={'class': 'form-control'})
    )

    isbn = forms.CharField(
        label='ISBN',
        required=False,
        widget=forms.TextInput(attrs={'class': 'form-control'})
    )

    publisher = forms.CharField(
        label='Publisher',
        required=False,
        widget=forms.TextInput(attrs={'class': 'form-control'})
    )

    authors = forms.CharField(
        label='Authors',
        required=False,
        widget=forms.TextInput(attrs={'class': 'form-control'})
    )
    page = forms.IntegerField(
        label='Page Count',
        required=False,
        widget=forms.NumberInput(attrs={'class': 'form-control'})
    )

class BookSearchForm(forms.Form):
    title = forms.CharField(
        label='Title',
        required=False,
        widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Search by Title'})
    )
    authors = forms.CharField(
        label='Authors',
        required=False,
        widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Search by Author'})
    )

class TransactionSearchForm(forms.Form):
    book_title = forms.CharField(
        max_length=100,
        required=False,
        label="Search by Book Title",
        widget=forms.TextInput(attrs={'placeholder': 'Search by Book Title'})
    )
    member_name = forms.CharField(
        max_length=100,
        required=False,
        label="Search by Member Name",
        widget=forms.TextInput(attrs={'placeholder': 'Search by Member Name'})
    )


"""
import cv2
import numpy as np
image=cv2.imread("data\\input\\2.png")
def empty(a):
    pass
#we need this for creating trackbar
#track bar fonksiyonu için ihtiyacımz var

cv2.namedWindow("TrackBars")
cv2.resizeWindow("TrackBars",1000,350)
cv2.createTrackbar("hue min","TrackBars",0,255,empty)
cv2.createTrackbar("hue max","TrackBars",255,255,empty)
cv2.createTrackbar("sat min","TrackBars",0,255,empty)
cv2.createTrackbar("sat max","TrackBars",255,255,empty)
cv2.createTrackbar("val min","TrackBars",0,255,empty)
cv2.createTrackbar("val max","TrackBars",255,255,empty)
#creating track bars
#track bar yapıyoruz 6 adet 3 min 3 max için
while True:

    imgHSV =cv2.cvtColor(image,cv2.COLOR_BGR2HSV)
    #resmi hsv ye çeviriyoruz
    #turning rgb to hsv
    h_min =cv2.getTrackbarPos("hue min","TrackBars")
    h_max =cv2.getTrackbarPos("hue max","TrackBars")
    s_min =cv2.getTrackbarPos("sat min","TrackBars")
    s_max =cv2.getTrackbarPos("sat max","TrackBars")
    v_min =cv2.getTrackbarPos("val min","TrackBars")
    v_max =cv2.getTrackbarPos("val max","TrackBars")
    #trackbardan değeri okyuoruz
    #getting value from trackbar
    print(h_min,h_max,s_min,s_max,v_min,v_max)

    lower = np.array([h_min,s_min,v_min])
    upper = np.array([h_max,s_max,v_max])
    mask=cv2.inRange(imgHSV,lower,upper)
    #creating mask
    #maske oluşturuyoruz
    result =cv2.bitwise_and(image,image,mask=mask)
    #resmin üzerine maskeyi eklliyoruz
    #puting mask on img
    mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)

    cv2.imshow("img",image)
    cv2.imshow("mask", mask)
    cv2.imshow("result", result)
    if cv2.waitKey(1) & 0xFF == ord("q"):
        break
    """
# bu kod kağan şenkeser hayratıdır
# trackbar kullanarak bulmak istediğimiz rengin hsv kodlarını aldık

import cv2
import numpy as np

point=cv2.imread("data\\input\\2.png")

lower_gray = np.array([0, 0, 127])
upper_gray = np.array([0, 0, 127])

hsv=cv2.cvtColor(point,cv2.COLOR_BGR2HSV)

mask1=cv2.inRange(hsv,lower_gray,upper_gray)# 4 adımlı bir mask işlemi uygulandı
cv2.imshow("mask",mask1)
blur=cv2.medianBlur(mask1,5)
blur=cv2.bitwise_not(blur)

contours,_=cv2.findContours(blur,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)#kontur bulma fonksiyonu

cv2.drawContours(point,contours,1,(0,0,255),3)
# momnets ile bulunan konturun merkezi hesaplanır
for c in contours:
    M=cv2.moments(c)

    cX=int(M["m10"]/ (M["m00"]+ 1e-7))
    cY=int(M["m01"]/(M["m00"]+ 1e-7))
    center=(cX,cY)
print(center)
cv2.circle(point,center,1,(255,0,0))

cv2.imshow("point",point)
cv2.waitKey(0)
cv2.destroyAllWindows()


# http://zetcode.com/python/peewee/
"""
Peewee mapping

A Model = database table
A Field = table column
An Instance = table row

Peewee uses MySQLDatabase for MySQL, PostgresqlDatabase for PostgreSQL, and SqliteDatabase for SQLite.
In this tutorial, we work with SQLite database.

"""
import peewee
import datetime

# Make a new DATABASE = test.db file in system gets created
db = peewee.SqliteDatabase('test.db')

# DB Model = Note
class Note(peewee.Model):

    text = peewee.CharField()
    created = peewee.DateField(default=datetime.date.today)

    class Meta:
        """
        we define the reference to the database and
        the database table name

        """
        database = db
        db_table = 'notes'

# table is created and saved
Note.create_table()
# create and save a multiple instance(ROW)
# We define the data in a list of dictionaries
# one bulk create operation

data = [
    { 'text': 'Tai chi in the morning', 'created': datetime.date(2018, 10, 20) },
    { 'text': 'Visited friend', 'created': datetime.date(2018, 10, 12) },
    { 'text': 'Went to cinema', 'created': datetime.date(2018, 10, 5) },
    { 'text': 'Listened to music', 'created': datetime.date(2018, 10, 28) },
    { 'text': 'Watched TV all day', 'created': datetime.date(2018, 10, 14) },
    { 'text': 'Worked in the garden', 'created': datetime.date(2018, 10, 22) },
    { 'text': 'Walked for a hour', 'created': datetime.date(2018, 10, 28) }
]

# The atomic() method puts the bulk operation in a transaction
with db.atomic():

    query = Note.insert_many(data)
    query.execute()


inp = input("\nWhich system do you have a measurement for?\n\nType C for Celcius, F for Fahrenheit: ")

if inp == "F" or inp == "f":
    print("\nYou have entered Fahrenheit\n")
    freading = input("Enter your Fahrenheit temperature reading: ")
    try:
        int(freading)
        fconvert = float(freading)
        cresult = (fconvert-32)*5/9
        print(f"The Temperature in Celcius is: {cresult}\n")
    except:
        print("Enter a valid number!")

elif inp == "C" or inp == "c":
    print("\nYou have entered Celcius\n")
    creading = input("Enter your Celcius temperature reading: ")
    try:
        int(creading)
        cconvert = float(creading)
        fresult = (cconvert*9/5)+32
        print(f"The Temperature in Farenheit is: {fresult}\n")
    except:
        print("Enter a valid number!")
else:
    print("Please type either C or F!")


import pygame
import pig.data as D
import moviepy.editor as E
import random
#random.seed(123)

white = (255, 255, 255)
black = (0, 0, 0)
size   = (720, 400)
middle = (720//3, 400//3)

def main():
    records = []
    data = D.PeppaPigIterableDataset(split='val', fragment_type='dialog', window=0,
                                     transform=None, duration=None, triplet=False)
    data._prepare_triplets(hard=True)
    pygame.init()
    screen = pygame.display.set_mode(size)
    font = pygame.font.SysFont("monospace", 56)
    screen.fill(white)
    pygame.display.flip()
    DIGIT_SIZE=(314//2, 512//2)
    ear = pygame.image.load('ear.png')
    ear = pygame.transform.scale(ear, (300, 300))

    one = pygame.image.load('one.png')
    one = pygame.transform.scale(one, DIGIT_SIZE)

    two = pygame.image.load('two.png')
    two = pygame.transform.scale(two, DIGIT_SIZE)

    que = pygame.image.load('que.png')
    que = pygame.transform.scale(que, DIGIT_SIZE)

    key = pygame.image.load('key.png')

    for i, item in enumerate(data.raw_triplets(shuffle=True)):
        if i > 5:
            break
        else:

            screen.fill(white)
            screen.blit(ear, (200, 50))
            pygame.display.flip()
            item.anchor.preview()
            pygame.time.wait(1000)

            #item.negative.audio = item.positive.audio
            clips = (item.positive, item.negative)
            index = random.randint(0, 1)

            screen.fill(white)
            screen.blit(one, (300, 50))
            pygame.display.flip()
            pygame.time.wait(1000)

            clips[index].resize(width=720).preview(audio=False)
            screen.fill(white)
            pygame.display.flip()
            pygame.time.wait(2000)

            #pygame.time.wait(2000)

            screen.fill(white)
            screen.blit(two, (300, 50))
            pygame.display.flip()
            pygame.time.wait(1000)
            clips[1-index].resize(width=720).preview(audio=False)
            screen.fill(white)
            pygame.display.flip()

            screen.fill(white)
            screen.blit(que, (300, 50))
            pygame.display.flip()
            choice = get_choice()
            records.append(dict(item=i, index=index, correct=index+1, choice=choice))
            screen.fill(white)
            screen.blit(key, (100, 50))
            pygame.display.flip()
            _ = get_choice()

    print(f"Correct {sum(x['correct'] == x['choice'] for x in records)} out of {len(records)}")
    for rec in records:
        print(rec)

def get_choice():
    pygame.event.clear()
    while True:
        event = pygame.event.wait()
        #print(event)
        if event.type == pygame.KEYDOWN and event.key == pygame.K_1:
            return 1
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_2:
            return 2
        elif event.type == pygame.KEYDOWN:
            return event.key


from torch import nn
from .encoder import Encoder
from .decoder import Decoder
from .feedforward import PostNet

class Tacotron(nn.Module):
    def __init__(self, num_chars, num_mels=80, embed_dim=512, prenet_dim=256,
                 attention_lstm_dim=1024, decoder_lstm_dim=1024, attention_dim=128,
                 encoder_layers=3, kernel_size=5, postnet_layers=3, postnet_channels=512,
                 attention_dropout=0.1, dropout=0.5, max_frames=870, threshold=0.5):
        super(Tacotron, self).__init__()

        self.encoder = Encoder(num_chars, embed_dim, encoder_layers,
                               kernel_size, dropout)

        self.decoder = Decoder(num_mels, prenet_dim, embed_dim,
                               attention_lstm_dim, decoder_lstm_dim,
                               attention_dim, attention_dropout, dropout,
                               max_frames, threshold)

        self.postnet = PostNet(postnet_layers, num_mels, postnet_channels,
                               kernel_size, dropout)

    def forward(self, chars, lengths, melspecs):
        # chars: (batch_size, char_length)
        # lengths: (batch_size, )
        # melspecs: (batch_size, frames_length, num_mels)

        encoder_outputs = self.encoder(chars, lengths)
        # encoder_outputs: (batch_size, char_length, embed_dim)

        decoder_melspecs, decoder_probs, attention = self.decoder(encoder_outputs, lengths, melspecs)
        # decoder_melspecs: (batch_size, frames_length, num_mels)
        # decoder_probs: (batch_size, frames_length)

        postnet_melspecs = decoder_melspecs + self.postnet(decoder_melspecs)
        # postnet_melspecs: (batch_size, frames_length, num_mels)

        return decoder_melspecs, postnet_melspecs, decoder_probs, attention

    def inference(self, chars, lengths):
        # chars: (batch_size, char_length)
        # lengths: (batch_size, )

        encoder_outputs = self.encoder(chars, lengths)
        # encoder_outputs: (batch_size, char_length, embed_dim)

        decoder_melspecs, decoder_probs, attention = self.decoder.inference(encoder_outputs, lengths)
        # decoder_melspecs: (batch_size, frames_length, num_mels)
        # decoder_probs: (batch_size, frames_length)

        postnet_melspecs = decoder_melspecs + self.postnet(decoder_melspecs)
        # postnet_melspecs: (batch_size, frames_length, num_mels)

        return postnet_melspecs, decoder_probs, attention

def tacotron(num_chars, params):
    return Tacotron(num_chars, params.num_mels, params.embed_dim, params.prenet_dim,
                    params.attention_lstm_dim, params.decoder_lstm_dim, params.attention_dim,
                    params.encoder_layers, params.kernel_size, params.postnet_layers, params.postnet_channels,
                    params.attention_dropout, params.dropout, params.max_frames, params.threshold)


def getRequired(object, path, pop=False):
    if pop:
        value = object.pop(path)
    else:
        value = object.get(path)
    if not value:
        raise Exception('{} was not found and it is required'.format(path))

    return value


from operator import and_
from sqlalchemy import create_engine
engine=create_engine("mysql+pymysql://root:root@localhost:3306/joget")
from sqlalchemy import Integer,String,Column,MetaData
metadata=MetaData()
from sqlalchemy.orm import declarative_base
Base=declarative_base()
Base.metadata.create_all(engine)
class Customer(Base):
    __tablename__ ="customers"
    id=Column(Integer,primary_key=True,autoincrement=True)
    name=Column(String(30))
    address=Column(String(30))
    email=Column(String(30))
    def __repr__(self):
        return f"Customer(name={self.name!r},address={self.address!r},email={self.email!r})"
Base.metadata.create_all(engine)
from sqlalchemy.orm import sessionmaker
Session=sessionmaker()
Session.configure(bind=engine)
session=Session()
c1=Customer(name='Ravi kumar',address='Station Road Nanded',email='Ravi@gmail.com')
session.add(c1)
session.add_all([Customer(name='Komal Pande',address='Koti Hyderabad',email='komal@gmail.com'),
                 Customer(name='Rajendar Nath',address='Sector 40 Gurgaon',email='nath@gmail'),
                 Customer(name='S.M.Krishna',address='Budhwar Peth Pune',email='smk@gmail.com')])
session.commit()

"""result=session.query(Customer).all()
for row in result:
    print("Name=",row.name,"  Address:",row.address,"  Email=",row.email)
x=session.query(Customer).get(2)
print("Name=",x.name,"Address=",x.address,"Email=",x.email)
x.address="Banjara Hills Secunderabad"
session.commit()
x=session.query(Customer).first()
print("Name=",x.name,"Address=",x.address,"Email=",x.email)
x.name='Ravi Shrivastava'
print("Name=",x.name,"Address=",x.address,"Email=",x.email)
session.commit()
session.rollback()
print("Name=",x.name,"Address=",x.address,"Email=",x.email)
session.query(Customer).filter(Customer.id!=2).update({Customer.name:"Mr"+Customer.name},synchronize_session=False)
result=session.query(Customer).filter(Customer.id>2)
for row in result:
    print("name=",row.name," address=",row.address,"email=",row.email)
result=session.query(Customer).one()
for row in result:
    print("name=",row.name," address=",row.address,"email=",row.email)
# equal
result=session.query(Customer).filter(Customer.id==2)
for row in result:
    print("Name:",row.name," address:",row.address," email:",row.email)

# Not equal#
result=session.query(Customer).filter(Customer.id!=2)
for row in result:
    print("ID:",row.id,"name: ",row.name,"address: ",row.address,"email:",row.email)

#Like
result=session.query(Customer).filter(Customer.name.like('Ra%'))
for row in result:
    print("ID:",row.id,"name:",row.name,"address:",row.address,"email:",row.email)

# IN
result=session.query(Customer).filter(Customer.id.in_([1,3]))
for row in result:
    print("ID:",row.id,"Name:",row.name,"address:",row.address,"email:",row.email)

# conjunction(multiple condition add by using this method)#
# And
result=session.query(Customer).filter(Customer.id,Customer.name.like('Ra%'))
for row in result:
    print("Id :",row.id,"name:",row.name,"address:",row.address,"email:",row.email)

# _and()
from sqlalchemy import and_
result=session.query(Customer).filter(and_(Customer.id>2,Customer.name.like('Ra%')))
for row in result:
    print("Id:",row.id,"Name:",row.name,"Address:",row.address,"email:",row.email)

# _OR
from sqlalchemy import or_
result=session.query(Customer).filter(or_(Customer.id>2,Customer.name.like('Ra%')))
for row in result:
    print("ID:",row.id,"name:",row.name,"address:",row.address,"email:",row.email)

## Textual SQL
from sqlalchemy import text
cust=session.query(Customer).filter(text("id= :value")).params(value=1).one()
print(cust)

for cust in session.query(Customer).filter(text("id>3")):
    print(cust.name)
from sqlalchemy import text
session.query(Customer).from_statement(text("select * from customers")).all()"""
#print(session.query(Customer).from_statement(text("select * from customers")).all())
from sqlalchemy import text
stmt=text("select name,id,address,email from customers")
stmt=stmt.columns(Customer.id,Customer.name)
session.query(Customer.id,Customer.name).from_statement(stmt).all()
# print(session.query(Customer.id,Customer.name).from_statement(stmt).all())


import datetime
import logging
import time

import torch
import torch.distributed as dist

from smoke.utils.metric_logger import MetricLogger
from smoke.utils.comm import get_world_size

def reduce_loss_dict(loss_dict):
    """
    Reduce the loss dictionary from all processes so that process with rank
    0 has the averaged results. Returns a dict with the same fields as
    loss_dict, after reduction.
    """
    world_size = get_world_size()
    if world_size < 2:
        return loss_dict
    with torch.no_grad():
        loss_names = []
        all_losses = []
        for k in sorted(loss_dict.keys()):
            loss_names.append(k)
            all_losses.append(loss_dict[k])
        all_losses = torch.stack(all_losses, dim=0)
        dist.reduce(all_losses, dst=0)
        if dist.get_rank() == 0:
            # only main process gets accumulated, so only divide by
            # world_size in this case
            all_losses /= world_size
        reduced_losses = {k: v for k, v in zip(loss_names, all_losses)}
    return reduced_losses

def do_train(
        cfg,
        distributed,
        model,
        data_loader,
        optimizer,
        scheduler,
        checkpointer,
        device,
        checkpoint_period,
        arguments,
):
    logger = logging.getLogger("smoke.trainer")
    logger.info("Start training")
    meters = MetricLogger(delimiter=" ")
    max_iter = cfg.SOLVER.MAX_ITERATION
    start_iter = arguments["iteration"]
    model.train()
    start_training_time = time.time()
    end = time.time()

    for data, iteration in zip(data_loader, range(start_iter, max_iter)):
        data_time = time.time() - end
        iteration += 1
        arguments["iteration"] = iteration

        images = data["images"].to(device)
        targets = [target.to(device) for target in data["targets"]]

        loss_dict = model(images, targets)

        losses = sum(loss for loss in loss_dict.values())

        # reduce losses over all GPUs for logging purposes
        loss_dict_reduced = reduce_loss_dict(loss_dict)
        losses_reduced = sum(loss for loss in loss_dict_reduced.values())
        meters.update(loss=losses_reduced, **loss_dict_reduced)

        optimizer.zero_grad()
        losses.backward()
        optimizer.step()
        scheduler.step()

        batch_time = time.time() - end
        end = time.time()
        meters.update(time=batch_time, data=data_time)

        eta_seconds = meters.time.global_avg * (max_iter - iteration)
        eta_string = str(datetime.timedelta(seconds=int(eta_seconds)))

        if iteration % 10 == 0 or iteration == max_iter:
            logger.info(
                meters.delimiter.join(
                    [
                        "eta: {eta}",
                        "iter: {iter}",
                        "{meters}",
                        "lr: {lr:.8f}",
                        "max men: {memory:.0f}",
                    ]
                ).format(
                    eta=eta_string,
                    iter=iteration,
                    meters=str(meters),
                    lr=optimizer.param_groups[0]["lr"],
                    memory=torch.cuda.max_memory_allocated() / 1024.0 / 1024.0
                )
            )
        # fixme: do we need checkpoint_period here
        if iteration in cfg.SOLVER.STEPS:
            checkpointer.save("model_{:07d}".format(iteration), **arguments)
        if iteration == max_iter:
            checkpointer.save("model_final", **arguments)
        # todo: add evaluations here
        # if iteration % evaluate_period == 0:
        # test_net.main()

    total_training_time = time.time() - start_training_time
    total_time_str = str(datetime.timedelta(seconds=total_training_time))
    logger.info(
        "Total training time: {} ({:.4f} s / it)".format(
            total_time_str, total_training_time / (max_iter)
        )
    )


import matplotlib.pyplot as plt
import numpy as np
import torch
from torch import cuda, tensor
from torch.utils.data import DataLoader

from Datasets.Dataset import AlgebraicDataset
from Neural.Network import MultiLayerNetwork

device = "cuda" if cuda.is_available() else "cpu"
print(f"Using {device} device")

target_fn = lambda x: 5 * x * x + 5

train_n_samples = 3000
test_n_samples = 100
predict_n_samples = 1
train_dataset = AlgebraicDataset(target_fn, (-10, 10), train_n_samples)
test_dataset = AlgebraicDataset(target_fn, (-10, 10), test_n_samples)
predict_dataset = AlgebraicDataset(target_fn, (-10, 10), predict_n_samples)

train_dataloader = DataLoader(train_dataset, train_n_samples, shuffle=True)
test_dataloader = DataLoader(test_dataset, test_n_samples, shuffle=True)
predict_dataloader = DataLoader(predict_dataset, predict_n_samples, shuffle=True)

def plot(f, model_net, interval=(-10, 10), n_samples=100, title='Após treino inicial'):
    fig, ax = plt.subplots()
    # fig.suptitle('Treinando Modelo - MultiCamadas')
    manager = plt.get_current_fig_manager()
    manager.set_window_title('Resultados - Redes Neurais')

    ax.grid(True, which='both')
    ax.set_title(title)
    ax.spines['left'].set_position('zero')
    ax.spines['bottom'].set_position('zero')

    samples = np.linspace(interval[0], interval[1], n_samples)
    model_net.eval()
    arr = []

    with torch.no_grad():
        pred = model_net(tensor(samples).unsqueeze(1).float().to(device))
        arr.append(pred)

    ax.plot(samples, list(map(f, samples)), '--', label='Valor esperado', color='red')
    ax.plot(samples, arr[0].cpu(), label='Valor obtido')

    plt.legend()
    return arr

model = MultiLayerNetwork([1, 30, 1]).to(device)
model.train_model(train_dataloader, epochs=50, learning_rate=0.0005)
plot(target_fn, model, title=f'Após treino inicial | {model.total_epochs} epochs')
for i in range(4):
    print('Treinando novamente... ')
    model.train_model(train_dataloader, epochs=700, learning_rate=0.0005)
    plot(target_fn, model, title=f'Após {i + 1} treinos | {model.total_epochs} epochs')

plt.show()


#!/usr/bin/env python3

import math
import random
import scipy.stats
import statistics

def inter(z):
    n = len(z) + 1
    m = statistics.mean(z)
    s2 = statistics.variance(z)
    t = scipy.stats.t.ppf(1 - eps / 2, n - 2)
    d = t * math.sqrt(s2) * math.sqrt(n / (n - 1))
    return m - d, m + d

def gen():
    return random.gauss(mu, sigma)

random.seed(12345)
eps = 0.05
mu = 0
sigma = 2
n = 100
z = []
z.append(gen())
z.append(gen())
correct = total = 0
for i in range(n - 2):
    a, b = inter(z)
    x = gen()
    if a <= x < b:
        correct += 1
    total += 1
    z.append(x)

print(correct / total)


import os

FILE_NAME='students.txt'
menumsg='''
====================学生信息管理系统====================
-----------------------功能菜单------------------------
    1.录入学生信息
    2.查找学生信息
    3.删除学生信息
    4.修改学生信息
    5.排序
    6.统计学生总人数
    7.显示所有学生信息
    0.退出系统
------------------------------------------------------
'''

def main():
    while True:
        try:
            menuId=int(menus())
        except:
            print('菜单选择错误')
            continue
        if menuId in [0,1,2,3,4,5,6,7]:
            if menuId==0:
                print('感谢您的使用，再见！')
                break
            elif menuId==1:
                insert()
            elif menuId==2:
                search()
            elif menuId==3:
                delete()
            elif menuId==4:
                modify()
            elif menuId==5:
                sort()
            elif menuId==6:
                total_count()
            elif menuId==7:
                show()
        else:
            print('菜单选择错误')
            continue

def menus():
    print(menumsg)
    menuId=input('请选择：')
    return menuId

def insert():
    student_list=[]
    while True:
        try:
            sid=int(input('请输入学生ID:'))
        except:
            print('学生ID必须为数字int')
            continue
        sname=input('请输入学生姓名:')
        if sname=='':
            print('未输入姓名')
            continue
        try:
            senglish_score=int(input('请输入学生英语成绩:'))
            schinese_score=int(input('请输入学生语文成绩:'))
            smath_score=int(input('请输入学生数学成绩:'))
        except:
            print('成绩必须为数字int')
            continue
        total_score=senglish_score+schinese_score+smath_score
        student={'id': sid, 'name': sname, 'english': senglish_score, 'chinese': schinese_score, 'math': smath_score, 'total': total_score}
        student_list.append(student)
        save(student_list)
        print('学生信息录入成功!')
        student_list=[]
        next=input('是否继续添加y/n：')
        if next!='y' and next!='Y':
            break

def save(list):
    try:
        file=open(FILE_NAME, 'a', encoding='utf-8')
    except:
        file=open(FILE_NAME, 'w', encoding='utf-8')
    for item in list:
        file.write(str(item)+'\n')
    file.close()

def search():
    while True:
        sortmethod=input('按ID查询请输入1，按姓名查询请输入2：')
        if sortmethod=='1':
            msg='请输入学生ID：'
        elif sortmethod=='2':
            msg='请输入学生姓名：'
        else:
            print('查询方式输入错误!')
            continue
        keyword=input(msg)
        checkFile()
        with open(FILE_NAME, 'r') as file:
            list=file.readlines()
            for student in list:
                s=dict(eval(student))
                if sortmethod=='1':
                    if str(s['id'])==keyword:
                        print('ID\t姓名\t英语\t语文\t数学\t总分')
                        print(show_student(student))
                        break
                else:
                    if s['name']==keyword:
                        print('ID\t姓名\t英语\t语文\t数学\t总分')
                        print(show_student(student))
                        break
            else:
                print('未找到该学生')
        next=input('是否继续查询？y/n：')
        if next!='y' and next!='Y':
            break

def delete():
    while True:
        try:
            deleteID=int(input('请输入被删除学生的ID：'))
        except:
            print('输入ID格式错误')
            continue
        if deleteID != '':
            checkFile()
            with open(FILE_NAME, 'r+', encoding='utf-8') as file:
                list=file.readlines()
                for student in list:
                    s=dict(eval(student))
                    if s['id']==deleteID:
                        print('ID为'+str(deleteID)+'的学生已删除！')
                        list.remove(student)
                        break
                else:
                    print('未找到ID为'+str(deleteID)+'学生！')
                file.seek(0)            # 将指针移到开头
                file.truncate()         # 从文章size处开始进行截断，无size表示从当前位置截断，截断后的内容将被删除
                file.writelines(list)
            show()
        else:
            print('删除ID输入为空')
        next=input('是否继续删除？y/n：')
        if next!='y' and next!='Y':
            break

def modify():
    while True:
        show()
        modifyID=input('请输入需要修改学生的ID：')
        checkFile()
        with open(FILE_NAME, 'r+') as file:
            list=file.readlines()
            count=0
            for student in list:
                s=dict(eval(student))
                if str(s['id'])==modifyID:
                    print('找到ID为'+modifyID+'的学生，可以修改他的相关信息了！')
                    try:
                        sid=int(input('请输入学生ID:'))
                    except:
                        print('学生ID必须为数字int')
                        break
                    sname=input('请输入学生姓名:')
                    if sname=='':
                        print('未输入姓名')
                        break
                    try:
                        senglish_score=int(input('请输入学生英语成绩:'))
                        schinese_score=int(input('请输入学生语文成绩:'))
                        smath_score=int(input('请输入学生数学成绩:'))
                    except:
                        print('成绩必须为数字int')
                        break

                    total_score=senglish_score+schinese_score+smath_score
                    student={'id': sid, 'name': sname, 'english': senglish_score, 'chinese': schinese_score, 'math': smath_score, 'total': total_score}
                    list[count]=str(student)
                    break
                count+=1
            else:
                print('未找到该学生！')
            file.seek(0)            # 将指针移到开头
            file.truncate()         # 从文章size处开始进行截断，无size表示从当前位置截断，截断后的内容将被删除
            file.writelines(list)
            file.flush()            # 立即将缓冲区内容写入文件
            show()
        next=input('是否继续修改？y/n：')
        if next!='y' and next!='Y':
            break

def sort():
    while True:
        sortby=input('学号排序请按1，英语排序请按2，语文排序请按3，数学排序请按4，总分排序请按5：')
        sort_key='id'
        if sortby=='1':
            sort_key='id'
        elif sortby=='2':
            sort_key='english'
        elif sortby=='3':
            sort_key='chinese'
        elif sortby=='4':
            sort_key='math'
        elif sortby=='5':
            sort_key='total'
        else:
            print('排序方式输入错误!')
            continue
        sorttype=input('升序请按1，降序请按2：')
        if sorttype not in ['1', '2']:
            print('升降序输入错误!')
            continue
        checkFile()
        with open(FILE_NAME, 'r') as file:
            list=file.readlines()
            i=0
            for item in list:
                student=dict(eval(item))
                minsortnum=int(student[sort_key])
                j=i+1
                for j in range(i+1,len(list)):
                    aitem=list[j]
                    astudent=dict(eval(aitem))
                    asortnum=int(astudent[sort_key])
                    if asortnum < minsortnum:
                        minsortnum=asortnum
                        tmp=list[i]
                        list[i]=aitem
                        list[j]=tmp
                    j+=1
                i+=1

            if sorttype=='2':
                list.reverse()
            print('ID\t姓名\t英语\t语文\t数学\t总分')
            for item in list:
                print(show_student(item))
        next=input('是否继续排序？y/n：')
        if next!='y' and next!='Y':
            break

def total_count():
    with open(FILE_NAME, 'r') as file:
        data=file.readlines()
        print('学生总人数为：'+str(len(data)))

def show():
    checkFile()
    with open(FILE_NAME, 'r') as file:
        data=file.readlines()
        if len(data)==0:
            print('暂无数据！')
            return
        print('ID\t姓名\t英语\t语文\t数学\t总分')
        for item in data:
            print(show_student(item))

def checkFile():
    if not os.path.exists(FILE_NAME) or not os.path.isfile(FILE_NAME):
        file=open(FILE_NAME, 'w', encoding='utf-8')

def show_student(s):
    stu=dict(eval(s))
    student_info=''
    key_list=list(stu.keys())
    count=0
    for keys in key_list:
        student_info+=str(stu[keys])
        count+=1
        if count!=len(key_list):
            student_info+='\t'
    return student_info

main()

# 打包使用PYInstaller 需要pip安装，使用命令PYInstaller -F xxx.py 记得注意PYInstaller大小写，否则会报错
# -F 打包单个文件，产生单个的可执行文件
# -D 打包多个文件，产生一个目录（包含多个文件）作为可执行程序
# -w 指定程序运行时不显示命令行窗口（仅对 Windows 有效）


import requests
import urllib.request
import time
from bs4 import BeautifulSoup
import os
if __name__ == '__main__':
    def dossier():
        os.chdir("C://Users//deeps//Desktop//test")

    dossier()

path = r"C://Users//deeps//Desktop//test"
CompteurParcours = 0

url = "https://scantrad.net/mangas/the-second-coming-of-gluttony/30"

def Navigate(url):
    if url != "Fin du Manga":
        response = requests.get(url)
        soup = BeautifulSoup(response.text, "html.parser")
        ListeLiens = RecupListeLiens(soup)
    return [soup, ListeLiens]

def RecupListeLiens(soup):
    Div = soup.findAll('div')
    L = []
    Img=soup.findall('img')
    for item in Img:
        if'data-src' in item.attrs:
            print(item['data-src'])
    for item in Div:
        if ClasseDownload(item):
            L.append(item)
    ListeTag = []
    for item in L:
        ListeTag.append(item.find('img'))
    ListeLiens = []
    for item in ListeTag:
        t = item['src']
        n = t.find('https')
        ListeLiens.append(item['src'][n:])
    return ListeLiens

def Next(soup):
    NextUrl = ""
    A = soup.findAll('a')
    L = []
    for a in A:
        if 'class' in a.attrs and a['class'] == ['btn','next_page']:
            NextUrl = a['href']
    if NextUrl == "":
        NextUrl = "Fin du Manga"
    print(NextUrl)
    return NextUrl


import torch
import cv2
import matplotlib.pyplot as plt
from loss.convex_polygon_loss import *
import numpy as np

total_iter_poly_hist = []
total_iter_l1_hist = []
total_iter_l2_hist = []
PRINT = True
learning_rate = 0.2
variance = 150
setting = "lr2e-2v{}".format(variance)

for idx in range(1):
    init_mid = [[200, 360],
                [108, 57],
                [117, 277]]
    init_mid_np = np.concatenate((np.random.normal(200, variance / 3, size=(1, 2)), np.random.normal(0, variance, size=(2, 2))), axis=0)
    init_mid = torch.Tensor(init_mid)
    init_mid2 = [[200, 360],
                 [108, 57],
                 [117, 277]]
    init_mid2 = torch.Tensor(init_mid2)
    init_mid3 = [[200, 360],
                 [108, 57],
                 [117, 277]]
    init_mid3 = torch.Tensor(init_mid3)
    target_mid = [[288, 300],
                  [120, 160],
                  [190, 100]]
    target_mid_np = np.concatenate((np.random.normal(200, variance / 3, size=(1, 2)), np.random.normal(0, variance, size=(2, 2))), axis=0)
    target_mid = torch.Tensor(target_mid)
    # print(mid_2_points(init_mid))

    poly_init = torch.autograd.Variable(init_mid, requires_grad=True)
    poly_init2 = torch.autograd.Variable(init_mid2, requires_grad=True)
    poly_init3 = torch.autograd.Variable(init_mid3, requires_grad=True)
    poly_target = mid_2_points(target_mid)

    opt = torch.optim.Adam([poly_init], lr=learning_rate)
    # scheduler = torch.optim.lr_scheduler.StepLR(opt, step_size=30, gamma=0.1)
    opt2 = torch.optim.Adam([poly_init2], lr=learning_rate)
    # scheduler2 = torch.optim.lr_scheduler.StepLR(opt2, step_size=30, gamma=0.1)
    opt3 = torch.optim.Adam([poly_init3], lr=learning_rate)
    # scheduler3 = torch.optim.lr_scheduler.StepLR(opt3, step_size=30, gamma=0.1)

    # piou = c_poly_loss(mid_2_points(poly_init), poly_target)
    # fig, axes = plt.subplots(nrows=1, ncols=2)
    # plt.ion()
    # plot1, = axes[0].plot(torch.cat((poly_target[:, 0], poly_target[0, 0].unsqueeze(0))), torch.cat((poly_target[:, 1], poly_target[0, 1].unsqueeze(0))))
    total_iter_poly = 0
    total_iter_l1 = 0
    total_iter_l2 = 0
    poly_hist = []
    l1_hist = []
    l2_hist = []
    for k in range(10000):
        stop_poly = False
        old_loss = 0
        if (total_iter_poly > 0 or stop_poly) and total_iter_l2 > 0 and total_iter_l1 > 0:
            break
        if total_iter_poly < 1 and not stop_poly:
            opt.zero_grad()
            poly_1 = mid_2_points(poly_init)
            # riou = raster_poly_IOU(poly_1, poly_target, scale=1000)
            piou = c_poly_loss(poly_1, poly_target)
            lp = (1.0 - piou)
            if lp < 0.01:
                total_iter_poly = k
            l2 = torch.pow((poly_1 - poly_target), 2).mean()
            loss = lp
            poly_hist.append(lp.detach().numpy())
            if abs(old_loss - loss) < 1e-4:
                stop_poly = True
            old_loss = loss
            loss.backward()
            if PRINT and k % 50 == 0:
                print("Polygon IOU: {}. lr:{}".format(piou.item(), opt.param_groups[0]['lr']))
                poly_1_detach = poly_1.detach()
                im = np.zeros([700, 500, 3]) + 255
                im = cv2.putText(im, "Polygon IOU: {}. lr:{}".format(piou.item(), opt.param_groups[0]['lr']), (10, 20),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 0), 1)
                for i in range(-1, len(poly_1_detach) - 1):
                    p1 = poly_1_detach[i].int()
                    p2 = poly_1_detach[i + 1].int()
                    im = cv2.line(im, (p1[0], p1[1]), (p2[0], p2[1]), (0, 0, 255), 1)
                for i in range(-1, len(poly_target) - 1):
                    p1 = poly_target[i].int()
                    p2 = poly_target[i + 1].int()
                    im = cv2.line(im, (p1[0], p1[1]), (p2[0], p2[1]), (255, 0, 255), 1)

            opt.step()
            # scheduler.step()
            # del loss

        if total_iter_l2 < 1:
            opt2.zero_grad()
            poly_2 = mid_2_points(poly_init2)
            if torch.abs((poly_2 - poly_target)).mean() < 1:
                total_iter_l2 = k
            l2_2 = torch.pow((poly_2 - poly_target), 2).mean().sqrt()
            piou = c_poly_loss(poly_2, poly_target)
            lp = (1.0 - piou)
            l2_hist.append(lp.detach().numpy())
            l2_2.backward()
            if PRINT and k % 50 == 0:
                print("l2 loss: {}".format(l2_2.item()))
                poly_2_detach = poly_2.detach()
                im2 = np.zeros([700, 500, 3]) + 255
                im2 = cv2.putText(im2, "L2 loss: {}. lr:{}".format(l2_2.item(), opt2.param_groups[0]['lr']), (10, 20),
                                  cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 0), 1)
                for i in range(-1, len(poly_2_detach) - 1):
                    p1 = poly_2_detach[i].int()
                    p2 = poly_2_detach[i + 1].int()
                    im2 = cv2.line(im2, (p1[0], p1[1]), (p2[0], p2[1]), (0, 0, 255), 1)
                for i in range(-1, len(poly_target) - 1):
                    p1 = poly_target[i].int()
                    p2 = poly_target[i + 1].int()
                    im2 = cv2.line(im2, (p1[0], p1[1]), (p2[0], p2[1]), (255, 0, 255), 1)

            opt2.step()
            # scheduler2.step()
            # del l2_2

        if total_iter_l1 < 1:
            opt3.zero_grad()
            poly_3 = mid_2_points(poly_init3)
            if torch.abs((poly_3 - poly_target)).mean() < 1:
                total_iter_l1 = k
            l1_3 = torch.abs((poly_3 - poly_target)).mean()
            piou = c_poly_loss(poly_3, poly_target)
            lp = (1.0 - piou)
            l1_hist.append(lp.detach().numpy())
            l1_3.backward()
            if PRINT and k % 50 == 0:
                print("l1 loss: {}".format(l1_3.item()))
                poly_3_detach = poly_3.detach()
                im3 = np.zeros([700, 500, 3]) + 255
                im3 = cv2.putText(im3, "L1 loss: {}. lr:{}".format(l1_3.item(), opt3.param_groups[0]['lr']), (10, 20),
                                  cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 0), 1)
                for i in range(-1, len(poly_3_detach) - 1):
                    p1 = poly_3_detach[i].int()
                    p2 = poly_3_detach[i + 1].int()
                    im3 = cv2.line(im3, (p1[0], p1[1]), (p2[0], p2[1]), (0, 0, 255), 1)
                for i in range(-1, len(poly_target) - 1):
                    p1 = poly_target[i].int()
                    p2 = poly_target[i + 1].int()
                    im3 = cv2.line(im3, (p1[0], p1[1]), (p2[0], p2[1]), (255, 0, 255), 1)

            opt3.step()
            # scheduler2.step()
            # del l2_2

        if PRINT and k % 50 == 0:
            Hori = np.concatenate((im, im2, im3), axis=1)
            cv2.imshow("im", Hori)
            cv2.waitKey(20)
        # if not PRINT and k > 10:
        #     cv2.destroyAllWindows()
    poly_hist = np.array(poly_hist)
    l1_hist = np.array(l1_hist)
    l2_hist = np.array(l2_hist)

    fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(12, 4))
    ax1.set_title("training with polygon iou loss")
    ax1.plot(poly_hist)
    ax2.set_title("training with l1 loss")
    ax2.plot(l1_hist)
    ax3.set_title("training with l2 loss")
    ax3.plot(l2_hist)
    plt.show()

    print("poly: {} | l1: {} | l2: {}".format(total_iter_poly, total_iter_l1, total_iter_l2))
    total_iter_poly_hist.append(total_iter_poly)
    total_iter_l1_hist.append(total_iter_l1)
    total_iter_l2_hist.append(total_iter_l2)
print("success")

total_iter_poly_hist = np.array(total_iter_poly_hist)
total_iter_l1_hist = np.array(total_iter_l1_hist)
total_iter_l2_hist = np.array(total_iter_l2_hist)
np.save("results/poly_hist_{}.npy".format(setting), total_iter_poly_hist)
np.save("results/l1_hist_{}.npy".format(setting), total_iter_l1_hist)
np.save("results/l2_hist_{}.npy".format(setting), total_iter_l2_hist)

print("poly_zeros: {}".format(np.count_nonzero(total_iter_poly_hist == 0)))
print("l1_zeros: {}".format(np.count_nonzero(total_iter_l1_hist == 0)))
print("l2_zeros: {}".format(np.count_nonzero(total_iter_l2_hist == 0)))

print("poly_mean: {}".format(total_iter_poly_hist[total_iter_poly_hist != 0].mean()))
print("l1_mean: {}".format(total_iter_l1_hist[total_iter_l1_hist != 0].mean()))
print("l2_mean: {}".format(total_iter_l2_hist[total_iter_l2_hist != 0].mean()))


import random
from model.contact import Contact

def test_edit_contact(app, db, check_ui):
    if len(db.get_contacts_list()) == 0:
        contact = Contact(firstname="firstname",
                          middlename="middlename",
                          lastname="lastname",
                          nickname="nickname",
                          title="title",
                          company="company",
                          address="address",
                          homenumber="homenumber",
                          mobilenumber="mobilenumber",
                          worknumber="worknumber",
                          faxnumber="faxnumber",
                          email="email",
                          email2="email2",
                          email3="email3",
                          homepage="homepage",
                          bday="1",
                          bmonth="January",
                          byear="1981",
                          aday="2",
                          amonth="January",
                          ayear="2000",
                          address2="address2",
                          phone2="phone2",
                          notes="notes")
        app.contact.create(contact)
    old_contacts = db.get_contacts_list()
    contact = random.choice(old_contacts)
    contact.firstname = "firstname2"
    contact.lastname = "lastname2"
    contact.address = "address2"
    app.contact.edit(contact=contact)
    assert len(old_contacts) == app.contact.count()
    new_contacts = db.get_contacts_list()
    assert sorted(old_contacts, key=Contact.id_or_max) == sorted(new_contacts, key=Contact.id_or_max)
    if check_ui:
        assert sorted(new_contacts, key=Contact.id_or_max) == sorted(app.contact.get_contacts_list(), key=Contact.id_or_max)


"""
Contains basic functions to list and set up a parser to each file format
"""

import os

# metadata extractor library

from me import settings
from me.utils import export
from me.metadata.pdf import PDF
from me.metadata.html import HTML

def pdf(folder, output):
    """
    """
    filenames = __list(folder)

    for filename in filenames:
        if filename.split('.')[1] == 'pdf':
            p = PDF(os.path.join(folder, filename))

            if settings.DEBUG:
                print(p)
                print("\n\n")

            export.csv(os.path.join(output, (filename + '.csv')), p)

def html(folder, output):
    filenames = __list(folder)

    for filename in filenames:
        if filename.split('.')[1] == 'html':
            h = HTML(os.path.join(folder, filename))
            print(h)

            export.csv(os.path.join(output, (filename + '.csv')), h)

def __list(folder):
    """
    """
    (_, _, filenames) = next(os.walk(folder))
    return filenames


import numpy as np
import serialIMU as simu
import time
import threading

# global variables and settings
dt = 0.02    						          # loop period
endObsThread = False		                  # flag to kill measurement thread
freshIMUData = False                          # flag to identify when new IMU data is available
obsUpdateLock = threading.Lock()              # measurement data update lock
latestObs = np.asmatrix(np.zeros((3,1)))      # latest observation from IMU

# observation update thread
# keeps main thread from having to block on IMU read
def obsThread():
	global totalCount
	global endObsThread
	global freshIMUData
	global obsUpdateLock

	# initialize serial connection to IMU
	imu = simu.serialIMU('COM4',115200)

	# update observations as fast as we can
	while( endObsThread != True ):

		# get current time
		tobs1 = time.clock()

		# update count only once we can get a lock
		rawIMUData = imu.getIMUData()
		lockStatus = obsUpdateLock.acquire(timeout=1)  # blocks until lock available
		if( lockStatus == False ):
			endObsThread = True
			assert( False ), ('Observation update thread failed to obtain lock')
		try:
			latestObs[0] = rawIMUData[3]*9.81            # a_x      [m/s^2]
			latestObs[1] = rawIMUData[4]*9.81            # a_y      [m/s^2]
			latestObs[2] = rawIMUData[2]*(np.pi/180)     # omega_z  [rad/s^2]
			freshIMUData = True
		finally:
			obsUpdateLock.release()

		# force update at 50 Hz
		while( time.clock() < (tobs1 + 0.001)):
			time.sleep(0.001) # pass doesn't work here for whatever reason

# initialize state vector to all zeros
xk_prev = np.asmatrix(np.zeros((6,1)))

# prepare state transition matrix
stateTrans = dt*np.asmatrix(np.eye(6))
stateTrans[2,2] = 1
stateTrans[5,5] = 1

# prepare state transition jacobian
F = dt*np.asmatrix(np.zeros((6,6)))
F[0,1] = dt
F[1,2] = dt
F[2,2] = 1
F[3,4] = dt
F[4,5] = dt
F[5,5] = 1

# state error mapping  TODO: FIX PLACEHOLDER
G = np.asmatrix(np.eye(6))

# state covariance matrix  TODO: FIX PLACEHOLDER
Q = np.asmatrix(np.zeros((6,6)))

# error covariance matrix  TODO: FIX PLACEHOLDER
Pk_prev = np.asmatrix(np.zeros((6,6)))

# catching KeyboardInterrupt to terminate with CTRL-C
try:
	# start observation update thread
	obsUpdater = threading.Thread(name='obsUpdater1', target=obsThread)
	obsUpdater.start()
	time.sleep(1)

	# repeat indefinitely
	while( True ):
		# reset timer
		timerOverflow = True
		t0 = time.clock()

		# wait if no new IMU data is available
		# hopefully this loop will never execute
		while( freshIMUData == False ):
			time.sleep(0.001) # pass doesn't work here for whatever reason

		# get latest observation
		lockStatus = obsUpdateLock.acquire(timeout=1)  # blocks until lock available
		if( lockStatus == False ):
			endObsThread = True
			assert( False ), ('Main loop failed to obtain observation update lock')
		try:
			zk = latestObs
			freshIMUData = False
		finally:
			obsUpdateLock.release()

		# define forward observation model and
		hk = np.matrix([[1,2],[3,4]])
		Hk = 0

		# predict step: propigate 'dynamics' forward in time
		xk_prior = stateTrans*xk_prev
		Pk_prior = F*Pk_prev*F.transpose() + G*Q*G.transpose()

		# correct step -- TODO: THESE ARE ALL PLACEHOLDERS!
		K = Pk_prior
		xk_posterior = xk_prior
		Pk_posterior = Pk_prior

		# prepare for next step
		# prepare for next step
		xk_prev = xk_posterior
		Pk_prev = Pk_posterior

		# enforce loop timing to period of length dt
		while( time.clock() < (t0 + dt)):
			timerOverflow = False

		# fail if loop timing cannot be enforced
		# note: won't detect a long call to serialIMU.getIMUData() until that function actually returns; could be hung for a while before we realize it
		if( timerOverflow == True ):
			endObsThread = True
			assert( False ), ("Code execution took too long!")

# exit on KeyboardInterrupt
except KeyboardInterrupt:
	endObsThread = True
	print('Terminating due to KeyboardInterrupt')
	exit()