from braces.views import GroupRequiredMixin
from authentications.utils import get_correlation_id_from_username, check_permissions_by_user
from web_admin import setup_logger
from django.contrib import messages
from web_admin.api_settings import AGENT_SMARTCARD_PATH, ADD_AGENT_SMART_CARD_PATH
from web_admin.get_header_mixins import GetHeaderMixin
from web_admin.restful_client import RestFulClient
from django.shortcuts import render, redirect
from django.views.generic.base import TemplateView
from web_admin.api_logger import API_Logger
from django.conf import settings
import logging

logger = logging.getLogger(__name__)
logging.captureWarnings(True)

class SmartCardView(GroupRequiredMixin, TemplateView, GetHeaderMixin):
    group_required = "CAN_VIEW_AGENT_SMARTCARD"
    login_url = 'web:permission_denied'
    raise_exception = False

    def check_membership(self, permission):
        self.logger.info(
            "Checking permission for [{}] username with [{}] permission".format(self.request.user, permission))
        return check_permissions_by_user(self.request.user, permission[0])

    template_name = 'agents/smart_card.html'
    logger = logger

    def dispatch(self, request, *args, **kwargs):
        correlation_id = get_correlation_id_from_username(self.request.user)
        self.logger = setup_logger(self.request, logger, correlation_id)
        return super(SmartCardView, self).dispatch(request, *args, **kwargs)

    def get(self, request, *args, **kwargs):
        self.logger.info('========== Start getting list agent smart card ==========')
        context = super(SmartCardView, self).get_context_data(**kwargs)
        agent_id = context['agent_id']
        agent_smartcard = self._get_agent_smartcard(agent_id)

        permissions = {
            'is_perm_add_smart_card': check_permissions_by_user(self.request.user, "CAN_ADD_AGENT_SMARTCARD"),
            'is_perm_del_smart_card': check_permissions_by_user(self.request.user, "CAN_DELETE_AGENT_SMARTCARD"),
        }

        context = {
            "smartcards": agent_smartcard,
            'permissions': permissions,
            'agent_id': agent_id
        }
        self.logger.info('========== Finished getting agent smartcard ==========')
        return render(request, self.template_name, context)

    def post(self, request, *args, **kwargs):
        if not check_permissions_by_user(request.user, 'CAN_ADD_AGENT_SMARTCARD'):
            return render(request, 'web/permission-denied.html')

        self.logger.info('========== Start adding agent smart card ==========')
        context = super(SmartCardView, self).get_context_data(**kwargs)
        agent_id = context['agent_id']
        params = {
            "card_number": request.POST.get('number'),
            "card_serial": request.POST.get('serial')
        }
        is_success = self._add_agent_smart_card(agent_id, params)
        self.logger.info('========== Finished adding agent smartcard ==========')
        if is_success:
            return redirect(request.META['HTTP_REFERER'])
        else:
            agent_smartcard = self._get_agent_smartcard(agent_id)
            permissions = {
                'is_perm_add_smart_card': check_permissions_by_user(self.request.user, "CAN_ADD_AGENT_SMARTCARD"),
                'is_perm_del_smart_card': check_permissions_by_user(self.request.user, "CAN_DELETE_AGENT_SMARTCARD"),
            }
            context = {
                "smartcards": agent_smartcard,
                'permissions': permissions,
                "agent_id": agent_id
            }
            return render(request, self.template_name, context)

    def _get_agent_smartcard(self, agent_id):
        params = {"agent_id": agent_id}
        is_success, status_code, status_message, data = RestFulClient.post(url=AGENT_SMARTCARD_PATH,
                                                                           headers=self._get_headers(),
                                                                           loggers=self.logger,
                                                                           params=params,
                                                                           timeout=settings.GLOBAL_TIMEOUT)

        API_Logger.post_logging(loggers=self.logger, params=params, response=data,
                                status_code=status_code, is_getting_list=True)
        data = self.filter_deleted(data)
        if not is_success:
            messages.add_message(
                self.request,
                messages.ERROR,
                status_message
            )
            data = []
        return data

    def _add_agent_smart_card(self, agent_id, params):
        url = ADD_AGENT_SMART_CARD_PATH.format(agent_id)
        is_success, status_code, status_message, data = RestFulClient.post(url=url,
                                                                           headers=self._get_headers(),
                                                                           loggers=self.logger,
                                                                           params=params,
                                                                           timeout=settings.GLOBAL_TIMEOUT)

        API_Logger.post_logging(loggers=self.logger, params=params, response=data,
                                status_code=status_code)

        if is_success:
            messages.add_message(
                self.request,
                messages.SUCCESS,
                'Add agent smartcard successfully'
            )
        elif status_message == 'timeout':
            messages.add_message(
                self.request,
                messages.ERROR,
                "Can not add SmartCard for this user, please try again or contact technical support"
            )
        elif status_message == "Invalid agent smartcard":
            messages.add_message(
                self.request,
                messages.ERROR,
                status_message
            )

        return is_success

    def filter_deleted(self, data):
        return [ i for i in data if i['is_deleted']== False]


class Solution:
    def maxProfit(self, prices: list[int]) -> int:
        max_pro = 0
        min_price = prices[0]
        for i in range(1, len(prices)):
            min_price = min(min_price, prices[i])
            max_pro = max(max_pro, prices[i]-min_price)
        return max_pro

sol = Solution()
print(sol.maxProfit([7, 1, 5, 3, 6, 4]))


from attractors import attractors

def lay_solver(bdd, g):
    for min_prio in range(g.d, -1, -1):
        i = min_prio % 2
        w = lay_ep(bdd, g, min_prio)
        if not w == bdd.false:
            x = attractors.attractor(bdd, g, i, w)
            ind_game = g.induced_game(bdd, ~x)
            (z_0, z_1) = lay_solver(bdd, ind_game)
            if i == 0:
                return z_0 | x, z_1
            else:
                return z_0, z_1 | x
    return bdd.false, bdd.false

def lay_ep(bdd, g, min_prio):
    if min_prio % 2 == 0:
        i = 0
        f_old = g.sup_prio_expr_even(bdd, min_prio)
    else:
        i = 1
        f_old = g.sup_prio_expr_odd(bdd, min_prio)
    while True:
        lay_attr_f = lay_attr(bdd, g, i, min_prio, f_old)
        f = f_old & lay_attr_f
        if f == f_old:
            break
        f_old = f
    return f

def lay_attr(bdd, g, i, min_prio, u):
    if i == 0:
        if g.d % 2 == 0:
            init_prio = g.d + 2
        else:
            init_prio = g.d + 1
    else:
        if g.d % 2 == 0:
            init_prio = g.d + 1
        else:
            init_prio = g.d + 2

    b = bdd.false
    for curr_prio in range(init_prio, min_prio - 1, - 2):
        u_p = u & g.sup_prio_expr(bdd, curr_prio)
        if i == 0:
            u_p_bis = g.sup_prio_expr_odd(bdd, curr_prio + 1)
        else:
            u_p_bis = g.sup_prio_expr_even(bdd, curr_prio + 1)

        p_safe_attr = attractors.p_safe_attractor(bdd, g, i, u_p | b, u_p_bis & ~b)
        b = b | p_safe_attr
    return b


"""Tests `phydmslib.models.GammaDistributedBetaModel` class.

Written by Jesse Bloom and Sarah Hilton.
"""

import random
import unittest
import numpy
import scipy.optimize
from phydmslib.constants import N_AA, N_NT, AA_TO_INDEX
import phydmslib.models

class test_GammaBeta_ExpCM(unittest.TestCase):
    """Test gamma distributed beta for `ExpCM`."""

    # use approach here to run multiple tests:
    # http://stackoverflow.com/questions/17260469/instantiate-python-unittest-testcase-with-arguments
    BASEMODEL = phydmslib.models.ExpCM

    def test_GammaDistributedBeta(self):
        """Initialize, test values, update, test again."""
        random.seed(1)
        numpy.random.seed(1)
        nsites = 10

        # create preference set
        prefs = []
        minpref = 0.01
        for _r in range(nsites):
            rprefs = numpy.random.dirichlet([0.5] * N_AA)
            rprefs[rprefs < minpref] = minpref
            rprefs /= rprefs.sum()
            prefs.append(dict(zip(sorted(AA_TO_INDEX.keys()), rprefs)))

        if self.BASEMODEL == phydmslib.models.ExpCM:
            paramvalues = {"eta": numpy.random.dirichlet([5] * (N_NT - 1)),
                           "omega": 0.7,
                           "kappa": 2.5,
                           "beta": 1.2,
                           "mu": 0.5}
            basemodel = self.BASEMODEL(prefs)
            assert set(paramvalues.keys()) == set(
                basemodel.freeparams
            ), "{0} vs {1}".format(set(paramvalues.keys()),
                                   set(basemodel.freeparams))
            basemodel.updateParams(paramvalues)
        elif self.BASEMODEL == phydmslib.models.ExpCM_empirical_phi:
            g = numpy.random.dirichlet([3] * N_NT)
            paramvalues = {"omega": 0.7,
                           "kappa": 2.5,
                           "beta": 1.2,
                           "mu": 0.5}
            basemodel = self.BASEMODEL(prefs, g)
            assert set(paramvalues.keys()) == set(
                basemodel.freeparams
            ), "{0} vs {1}".format(set(paramvalues.keys()),
                                   set(basemodel.freeparams))
            basemodel.updateParams(paramvalues)
        else:
            raise ValueError("Invalid BASEMODEL: {0}".format(self.BASEMODEL))
            rprefs /= rprefs.sum()
            prefs.append(dict(zip(sorted(AA_TO_INDEX.keys()), rprefs)))

        ncats = 4
        gammamodel = phydmslib.models.GammaDistributedBetaModel(basemodel,
                                                                ncats)
        self.assertTrue(
            numpy.allclose(
                numpy.array([m.beta for m in gammamodel._models]),
                phydmslib.models.DiscreteGamma(
                    gammamodel.alpha_lambda,
                    gammamodel.beta_lambda,
                    gammamodel.ncats)))
        for (param, pvalue) in paramvalues.items():
            if param != gammamodel.distributedparam:
                self.assertTrue(numpy.allclose(getattr(gammamodel, param),
                                               pvalue))

        # try some updates and make sure everything remains OK
        for i in range(3):
            newvalues = {}
            for param in gammamodel.freeparams:
                (low, high) = gammamodel.PARAMLIMITS[param]
                if gammamodel.PARAMTYPES[param] == float:
                    newvalues[param] = random.uniform(low, high)
                else:
                    paramlength = gammamodel.PARAMTYPES[param][1]
                    newvalues[param] = numpy.random.uniform(low, high,
                                                            paramlength)
            gammamodel.updateParams(newvalues)
            self.assertTrue(
                numpy.allclose(
                    numpy.array([m.beta for m in gammamodel._models]),
                    phydmslib.models.DiscreteGamma(
                        gammamodel.alpha_lambda,
                        gammamodel.beta_lambda,
                        gammamodel.ncats)))
            for (param, pvalue) in newvalues.items():
                if param != gammamodel.distributedparam:
                    self.assertTrue(numpy.allclose(pvalue,
                                                   getattr(gammamodel, param)))
                    if param not in gammamodel.distributionparams:
                        self.assertTrue(
                            all((numpy.allclose(pvalue, getattr(m, param))
                                 for m in gammamodel._models)))

            # This is the opposite test of gammaomega
            self.assertTrue(
                gammamodel._models[0].branchScale
                > gammamodel.branchScale
                > gammamodel._models[-1].branchScale)

            t = 0.15
            for k in range(gammamodel.ncats):
                M = gammamodel.M(k, t)
                self.assertTrue(numpy.allclose(gammamodel._models[k].M(t), M))
                for param in gammamodel.freeparams:
                    if param not in gammamodel.distributionparams:
                        dM = gammamodel.dM(k, t, param, M)
                        self.assertTrue(
                            numpy.allclose(
                                dM,
                                gammamodel._models[k].dM(t, param, Mt=None)))

            # Check derivatives with respect to distribution params
            d_distparams = gammamodel.d_distributionparams
            self.assertTrue((d_distparams["alpha_lambda"] > 0).all())
            self.assertTrue((d_distparams["beta_lambda"] < 0).all())
            for param in gammamodel.distributionparams:
                diffs = []
                for k in range(gammamodel.ncats):
                    pvalue = getattr(gammamodel, param)

                    def func(x):
                        gammamodel.updateParams({param: x[0]})
                        return getattr(gammamodel._models[k],
                                       gammamodel.distributedparam)

                    def dfunc(x):
                        gammamodel.updateParams({param: x[0]})
                        return gammamodel.d_distributionparams[param][k]

                    diff = scipy.optimize.check_grad(func, dfunc,
                                                     numpy.array([pvalue]))
                    gammamodel.updateParams({param: pvalue})
                    diffs.append(diff)
                diffs = numpy.array(diffs)
                self.assertTrue(
                    (diffs < 1e-5).all(),
                    ("Excessive diff for d_distributionparams[{0}] when "
                     "distributionparams = {1}:\n{2}".format(
                            param, gammamodel.distributionparams, diffs)))

            # Check the stationary state & deriviative of the stationary state
            # Stationary states should be different for each `k`
            self.assertFalse(
                all((numpy.allclose(gammamodel.stationarystate(i),
                                    gammamodel.stationarystate(j))
                    for i in range(gammamodel.ncats)
                    for j in range(i + 1, gammamodel.ncats))))
            # The derviative of the stationary states should be different with
            # respect to beta for each `k`
            self.assertFalse(
                all(
                    (numpy.allclose(gammamodel.dstationarystate(i, "beta"),
                                    gammamodel.dstationarystate(j, "beta"))
                     for i in range(gammamodel.ncats)
                     for j in range(i + 1, gammamodel.ncats))))

class test_GammaBeta_ExpCM_empirical_phi(test_GammaBeta_ExpCM):
    """Test gamma distributed beta for `ExpCM_empirical_phi`."""

    # use approach here to run multiple tests:
    # http://stackoverflow.com/questions/17260469/instantiate-python-unittest-testcase-with-arguments
    BASEMODEL = phydmslib.models.ExpCM_empirical_phi

if __name__ == "__main__":
    runner = unittest.TextTestRunner()
    unittest.main(testRunner=runner)


data = []
count = 0
with open('reviews.txt', 'r') as f:
	for line in f:
		data.append(line.strip())
		count += 1
		if count % 100000 ==0 :
			print(len(data))

print('檔案讀取完了, 總共有', len(data), '筆資料')

# #以下把有提到nice的留言收集起來

# NICE = []
# # for message in data:
# # 	if 'nice' in message:
# # 		NICE.append(message)
# # print('總共有', len(NICE), '則留言提到 nice')
# # print(NICE[0])

# # 上方可以改寫成下面快寫法
# NICE = [message for message in data if 'nice' in message]
# print('總共有', len(NICE), '則留言提到 nice')
# print(NICE[0])

wc = {}
for d in data:
	words = d.split()	# split 預設是空白建，還不會切出多餘的空白
	for word in words:
		if word in wc:
			wc[word] += 1
		else:
			wc[word] = 1  # 新增新的詞進字典

# for key in wc:
# 	if wc[key] > 100:
# 		print(key, wc[key])

while True:
	word = input("請輸入你想查找的字:")
	if word == 'q':
		break
	elif word not in wc:
		print("沒有出現過此字")
	else:
		print(word, "在這群留言中出現過 ", wc[word], "次")


# calculate the weight of source based on its location and window counts
import os
import numpy as np
from pprint import pprint
from spaceweight import SpherePoint, SphereDistRel
from pytomo3d.utils.io import dump_json

def assign_source_to_points(sources):
    points = []
    for event, cat in sources.iteritems():
        origin = cat[0].preferred_origin()
        point = SpherePoint(origin.latitude, origin.longitude, tag=event,
                            weight=1.0)
        points.append(point)

    assert len(points) == len(sources)
    return points

def normalize_source_weights(points, wcounts):
    wsum = 0.0
    wcounts_sum = 0
    for p in points:
        wsum += p.weight * wcounts[p.tag]
        wcounts_sum += wcounts[p.tag]

    print("The summation of window counts: %d" % wcounts_sum)
    print("The iniital summation(weight * window_counts): %f" % wsum)
    factor = 1.0 / wsum

    weights = {}
    for p in points:
        weights[p.tag] = p.weight * factor

    # validate
    wsum = 0.0
    for event in weights:
        wsum += wcounts[event] * weights[event]
    if not np.isclose(wsum, 1.0):
        raise ValueError("Error normalize source weights: %f" % wsum)
    print("The normalized sum is: %f" % wsum)
    print("Final weights: %s" % weights)
    return weights

def calculate_source_weights_on_location(
        points, search_ratio, plot_flag, outputdir):
    """
    :param outputdir: output directory for figures
    """
    # set a fake center point
    center = SpherePoint(0, 180.0, tag="Center")
    weightobj = SphereDistRel(points, center=center)

    if plot_flag:
        scan_figname = os.path.join(
            outputdir, "source_weights.smart_scan.png")
    else:
        scan_figname = None

    ref_distance, cond_number = weightobj.smart_scan(
        max_ratio=search_ratio, start=0.1, gap=0.2,
        drop_ratio=0.95, plot=plot_flag,
        figname=scan_figname)

    print("Reference distance and condition number: %f, %f"
          % (ref_distance, cond_number))

    if plot_flag:
        map_figname = os.path.join(
            outputdir, "source_weights.global_map.pdf")
        weightobj.plot_global_map(figname=map_figname, lon0=180.0)

    return ref_distance, cond_number

def dump_weights_to_txt(weights, outputfile):
    events = weights.keys()
    events.sort()

    with open(outputfile, 'w') as fh:
        for e in events:
            fh.write("%-16s %.10e\n" % (e, weights[e]))

def calculate_source_weights(info, param, output_file, _verbose=False):
    """
    program which calculates the source weightings for weighting
    strategy I, in which case the source weightings needs to be
    calculated separately.
    """
    print("=" * 10 + " Param " + "=" * 10)
    pprint(param)
    sources = {k: v["source"] for k, v in info.iteritems()}
    wcounts = {k: v["window_counts"] for k, v in info.iteritems()}

    outputdir = os.path.dirname(output_file)
    if not os.path.exists(outputdir):
        os.makedirs(outputdir)

    ref_distance = -1.0
    cond_num = -1.0

    points = assign_source_to_points(sources)
    if param["flag"]:
        print("=" * 10 + " Weight source on location " + "=" * 10)
        ref_distance, cond_num = calculate_source_weights_on_location(
            points, param["search_ratio"], param["flag"], outputdir)
    print("=" * 10 + " Normalize weights " + "=" * 10)
    weights = normalize_source_weights(points, wcounts)

    # write weights to txt(for summing kernels)
    print("=" * 10 + " Write weights " + "=" * 10)
    print("Output weight file: %s" % output_file)
    dump_weights_to_txt(weights, output_file)

    # generate log file
    log_content = {"weights": weights, "reference_distance": ref_distance,
                   "cond_num": cond_num, "weight_flag": param["flag"],
                   "serach_ratio": param["search_ratio"]}
    outputfn = os.path.join(outputdir, "source_weights.log.json")
    print("Output log file: %s" % outputfn)
    dump_json(log_content, outputfn)


from . import development, test, production

def set_app_config_keys(app, settings):
    all_config_vars = dict(
        list(settings.FLASK_VARS.items()) +
        list(settings.FLASK_JWT_VARS.items()) +
        list(settings.DB_CONNECTION.items())
    )

    for key, value in all_config_vars.items():
        app.config[key] = value

def development_config(app):
    set_app_config_keys(app, development)

def testing_config(app):
    set_app_config_keys(app, test)

def production_config(app):
    set_app_config_keys(app, production)

config = {
    'development': development_config,
    'testing': testing_config,
    'production': production_config,
    'default': development_config,
}


import glob
import os
import random
import h5py
from os.path import join

import numpy as np
import torch

#from fastmri_utils.data import transforms
from tqdm import tqdm

from operators import rotate_real, to_complex
from extra_utils import cut_to_01

from PIL import Image
# ----- Dataset creation, saving, and loading -----

def create_iterable_dataset(
    n, set_params, iterator, iter_params,
):
    """ Creates training, validation, and test data sets.

    Samples data signals from a data generator and stores them.

    Parameters
    ----------
    n : int
        Dimension of signals x.
    set_params : dictionary
        Must contain values for the following keys:
        path : str
            Directory path for storing the data sets.
    generator : callable
        Generator function to create signal samples x. Will be called with
        the signature generator(n, **gen_params).
    gen_params : dictionary
        Additional keyword arguments passed on to the signal generator.
    """

    dataset_train = iterator(mode='train', **iter_params)
    dataset_val = iterator(mode='val', **iter_params)
    dataset_test = iterator(mode='test', **iter_params)
    iter_train = iter(dataset_train)
    iter_val = iter(dataset_val)
    iter_test = iter(dataset_test)
    os.makedirs(os.path.join(set_params["path"], "train"), exist_ok=True)
    os.makedirs(os.path.join(set_params["path"], "val"), exist_ok=True)
    os.makedirs(os.path.join(set_params["path"], "test"), exist_ok=True)

    for idx in tqdm(range(len(iter_train)), desc="generating training signals"):
        torch.save(
            next(iter_train),
            os.path.join(
                set_params["path"], "train", "sample_{:05d}.pt".format(idx)
            ),
        )

    for idx in tqdm(range(len(iter_val)), desc="generating validation signals"):
        torch.save(
            next(iter_val),
            os.path.join(
                set_params["path"], "val", "sample_{:05d}.pt".format(idx)
            ),
        )

    for idx in tqdm(range(len(iter_test)), desc="generating test signals"):
        torch.save(
            next(iter_test),
            os.path.join(
                set_params["path"], "test", "sample_{:05d}.pt".format(idx)
            ),
        )

class Load_fluorescence_microscopy_dataset:
    """ Loads a dataset of fluorescence microscopy images.

    Parameters
    ----------
    mode : str
        One of 'train', 'test', 'val'. Decides which dataset to create.
    path_train : str
        Path to training images.
    path_val : str
        Path to validation images.
    path_test : str
        Path to testing images.
    """
    def __init__(self, mode, path_train = None, path_val = None, path_test = None):

        self.mode = mode

        self.path_train = path_train
        self.path_val = path_val
        self.path_test = path_test

        if self.mode.lower() == 'train':
            self.path = path_train
        elif self.mode.lower() == 'val':
            self.path = path_val
        elif self.mode.lower() == 'test':
            self.path = path_val
        else:
            self.path = None
        if self.path is not None:
            self.filenames = glob.glob(join(self.path, '*.tif'))
        else:
            self.filenames = [];

        self.data_size = len(self.filenames)

    def __len__(self):
        return self.data_size

    def __iter__(self):
        self.count = 0
        return self

    def __next__(self):
        if self.path is None:
            raise StopIteration
        if self.count < self.data_size:
            fname = self.filenames[self.count]
            self.count += 1
            im = np.array(Image.open(fname)).astype(np.float64)
            im /= (256*256-1)
            return torch.tensor(im, dtype=torch.float)
        else:
            raise StopIteration

class Load_fastMRI_dataset_complex(Load_fastMRI_dataset):

    def __init__(self, mode, path_train = None, path_val = None, path_test = None):
        super().__init__(mode, path_train, path_val, path_test)

    def __next__(self):
        if self.path is None:
            raise StopIteration
        if self.count < self.data_size:
            fname = self.filenames[self.count]
            self.count += 1
            with h5py.File(fname, 'r') as hf:
                rec = np.array(hf['reconstruction_rss'])
                rec = rec[int(rec.shape[0]/2), ...]
                amax = np.amax(rec)
                rec *= (255/amax)
                im = Image.fromarray(np.uint8(rec))
                im = im.resize((256,256))
                np_im = np.array(im).astype(np.float32)/255;
                X = np.zeros([2, 256,256]);
                X[0,:,:] = np_im;
                return torch.tensor(X, dtype=torch.float)
        else:
            raise StopIteration

class IPDataset(torch.utils.data.Dataset):
    """ Datasets for imaging inverse problems.

    Loads image signals created by `create_iterable_dataset` from a directory.

    Implements the map-style dataset in `torch`.

    Attributed
    ----------
    subset : str
        One of "train", "val", "test".
    path : str
        The directory path. Should contain the subdirectories "train", "val",
        "test" containing the training, validation, and test data respectively.
    """

    def __init__(self, subset, path, transform=None, device=None):
        self.path = path
        self.files = glob.glob(os.path.join(path, subset, "*.pt"))
        self.transform = transform
        self.device = device

    def __len__(self):
        return len(self.files)

    def __getitem__(self, idx):
        # load image and add channel dimension
        out = torch.load(self.files[idx])
        if out.dim() == 2:
            out = out.unsqueeze(0)
        if self.device is not None:
            out.to(self.device)
        out = (out,)
        return self.transform(out) if self.transform is not None else out

# ----- data transforms -----

class SimulateMeasurements(object):
    """ Forward operator on target samples.

    Computes measurements and returns (measurement, target) pair.

    Parameters
    ----------
    operator : callable
        The measurement operation to use.

    """

    def __init__(self, operator):
        self.operator = operator

    def __call__(self, target):
        (target,) = target
        meas = self.operator(target)
        return meas, target

class Jitter(object):
    """ Adds random pertubations to the input of (input, target) pairs.
    """

    def __init__(self, eta, scale_lo, scale_hi, n_seed=None, t_seed=None):
        self.eta = eta
        self.scale_lo = scale_lo
        self.scale_hi = scale_hi
        self.rng = np.random.RandomState(n_seed)
        self.trng = torch.Generator()
        if t_seed is not None:
            self.trng.manual_seed(t_seed)

    def __call__(self, inputs):
        meas, target = inputs
        m = meas.shape[-1]  # number of sampled measurements
        scale = (
            self.scale_lo + (self.scale_hi - self.scale_lo) * self.rng.rand()
        )
        noise = torch.randn(meas.shape, generator=self.trng).to(meas.device)
        meas_noisy = meas + self.eta / np.sqrt(m) * noise * scale
        return meas_noisy, target

# ---- run data generation -----
if __name__ == "__main__":
    import config

    np.random.seed(config.numpy_seed)
    torch.manual_seed(config.torch_seed)
    create_iterable_dataset(
        config.n, config.set_params, config.data_gen, config.data_params,
    )


"""Base class for diff comment resources."""

from django.core.exceptions import ObjectDoesNotExist
from django.db.models import Q
from django.template.defaultfilters import timesince
from djblets.util.decorators import augment_method_from
from djblets.webapi.decorators import webapi_request_fields
from djblets.webapi.fields import (IntFieldType,
                                   ResourceFieldType,
                                   StringFieldType)

from reviewboard.reviews.models import Comment
from reviewboard.webapi.base import WebAPIResource
from reviewboard.webapi.decorators import webapi_check_local_site
from reviewboard.webapi.resources import resources
from reviewboard.webapi.resources.base_comment import BaseCommentResource

class BaseDiffCommentResource(BaseCommentResource):
    """Base class for diff comment resources.

    Provides common fields and functionality for all diff comment resources.
    The list of comments cannot be modified from this resource.
    """

    model = Comment
    name = 'diff_comment'
    fields = dict({
        'first_line': {
            'type': IntFieldType,
            'description': 'The line number that the comment starts at.',
        },
        'num_lines': {
            'type': IntFieldType,
            'description': 'The number of lines the comment spans.',
        },
        'filediff': {
            'type': ResourceFieldType,
            'resource': 'reviewboard.webapi.resources.filediff.'
                        'FileDiffResource',
            'description': 'The per-file diff that the comment was made on.',
        },
        'interfilediff': {
            'type': ResourceFieldType,
            'resource': 'reviewboard.webapi.resources.filediff.'
                        'FileDiffResource',
            'description': "The second per-file diff in an interdiff that "
                           "the comment was made on. This will be ``null`` if "
                           "the comment wasn't made on an interdiff.",
        },
    }, **BaseCommentResource.fields)

    uri_object_key = 'comment_id'

    allowed_methods = ('GET',)

    def get_queryset(self,
                     request,
                     review_request_id=None,
                     review_id=None,
                     is_list=False,
                     *args, **kwargs):
        """Return a queryset for Comment models.

        If the queryset is being used for a list of comment resources,
        then this can be further filtered by passing ``?interdiff-revision=``
        on the URL to match the given interdiff revision, and
        ``?line=`` to match comments on the given line number.

        Args:
            request (django.http.HttpRequest):
                The HTTP request from the client.

            review_request_id (int, optional):
                The review request ID used to filter the results. If set,
                only comments from the given review request that are public
                or owned by the requesting user will be included.

            review_id (int, optional):
                The review ID used to filter the results. If set,
                only comments from the given review that are public
                or owned by the requesting user will be included.

            is_list (bool, optional):
                Whether the incoming HTTP request is for list resources.

            *args (tuple):
                Additional positional arguments.

            **kwargs (dict):
                Additional keyword arguments.

        Returns:
            django.db.models.query.QuerySet:
            A queryset for Comment models.
        """
        q = Q()

        if review_request_id is not None:
            try:
                review_request = resources.review_request.get_object(
                    request, review_request_id=review_request_id,
                    *args, **kwargs)
            except ObjectDoesNotExist:
                raise self.model.DoesNotExist

            q &= Q(review__isnull=False)
            q &= Q(
                Q(filediff__diffset__history__review_request=review_request) |
                Q(filediff__diffset__review_request_draft__review_request=review_request))

        if is_list:
            if review_id:
                q &= Q(review=review_id)

            if 'interdiff-revision' in request.GET:
                interdiff_revision = int(request.GET['interdiff-revision'])
                q &= Q(interfilediff__diffset__revision=interdiff_revision)

            if 'line' in request.GET:
                q &= Q(first_line=int(request.GET['line']))

        queryset = self.model.objects.filter(q)
        order_by = kwargs.get('order-by', None)

        if order_by:
            queryset = queryset.order_by(*[
                field
                for field in order_by.split(',')
                if '__' not in field  # Don't allow joins
            ])

        return queryset

    def serialize_public_field(self, obj, **kwargs):
        return obj.review.get().public

    def serialize_timesince_field(self, obj, **kwargs):
        return timesince(obj.timestamp)

    def serialize_user_field(self, obj, **kwargs):
        return obj.review.get().user

    @webapi_request_fields(
        optional={
            'interdiff-revision': {
                'type': IntFieldType,
                'description': 'The second revision in an interdiff revision '
                               'range. The comments will be limited to this '
                               'range.',
            },
            'line': {
                'type': IntFieldType,
                'description': 'The line number that each comment must '
                               'start on.',
            },
            'order-by': {
                'type': StringFieldType,
                'description': 'Comma-separated list of fields to order by.',
                'added_in': '1.7.10',
            },
        },
        allow_unknown=True
    )
    @augment_method_from(BaseCommentResource)
    def get_list(self, request, review_id=None, *args, **kwargs):
        pass

    @augment_method_from(WebAPIResource)
    def get(self, *args, **kwargs):
        """Returns information on the comment."""
        pass


import time
from sound_keyboard.queue import get_queue
from sound_keyboard.face_gesture_detector.face_detection import inference
from sound_keyboard.face_gesture_detector.eye_blink_detection import get_eye_state
from sound_keyboard.face_gesture_detector.enums import (
    EyeDirection,
    EyeState,
    MouthState,
    Gestures
)
import copy
import cv2
import dlib
import numpy as np
from math import hypot
from sound_keyboard.face_gesture_detector.gaze_tracking import GazeTracking

class FaceGestureDetector:
    def __init__(self, queue):
        self.cap = cv2.VideoCapture(0)
        self.queue = queue
        self.detector = dlib.get_frontal_face_detector()
        self.predictor = dlib.shape_predictor("./sound_keyboard/face_gesture_detector/shape_predictor_68_face_landmarks.dat")
        # self.debug = len(sys.argv) >= 2 and sys.argv[1] == 'DEBUG'
        self.debug = True

        self.previous = None

        self.gaze = GazeTracking()

    def get_gaze_state(self, x):
        if x <= 0.54:
            return EyeDirection.LEFT
        if x >= 0.57:
            return EyeDirection.RIGHT
        return EyeDirection.CENTER

    def get_mouth_state(self, mouth_ratio):
        if mouth_ratio > 6.8:
            return MouthState.CLOSE
        else:
            return MouthState.OPEN

    #ランドマークの重心
    def midpoint(self, p1, p2):
        return int((p1.x + p2.x) / 2), int((p1.y + p2.y) / 2)

    #口開閉検知
    def get_mouth_ratio(self, eye_points, facial_landmarks):
        mouth_region = np.array([
            (
                facial_landmarks.part(eye_points[0]).x,
                facial_landmarks.part(eye_points[0]).y
            ),
            (
                facial_landmarks.part(eye_points[1]).x,
                facial_landmarks.part(eye_points[1]).y
            ),
            (
                facial_landmarks.part(eye_points[2]).x,
                facial_landmarks.part(eye_points[2]).y
            ),
            (
                facial_landmarks.part(eye_points[3]).x,
                facial_landmarks.part(eye_points[3]).y
            ),
            (
                facial_landmarks.part(eye_points[4]).x,
                facial_landmarks.part(eye_points[4]).y
            ),
            (
                facial_landmarks.part(eye_points[5]).x,
                facial_landmarks.part(eye_points[5]).y
            )], np.int32)
        left_point = (facial_landmarks.part(eye_points[0]).x, facial_landmarks.part(eye_points[0]).y)
        right_point = (facial_landmarks.part(eye_points[3]).x, facial_landmarks.part(eye_points[3]).y)
        center_top = self.midpoint(facial_landmarks.part(eye_points[1]), facial_landmarks.part(eye_points[2]))
        center_bottom = self.midpoint(facial_landmarks.part(eye_points[5]), facial_landmarks.part(eye_points[4]))

        # hor_line = cv2.line(frame, left_point, right_point, (0, 255, 0), 2)
        # ver_line = cv2.line(frame, center_top, center_bottom, (0, 255, 0), 2)

        hor_line_length = hypot((left_point[0] - right_point[0]), (left_point[1] - right_point[1]))
        ver_line_length = hypot((center_top[0] - center_bottom[0]), (center_top[1] - center_bottom[1]))

        if ver_line_length == 0:
            ver_line_length += 0.001

        ratio = hor_line_length / ver_line_length
        return ratio, mouth_region

    # 視線検知
    def get_gaze_right_level(self, eye_points, facial_landmarks, frame, gray):
        left_eye_region = np.array([
            (
                facial_landmarks.part(eye_points[0]).x,
                facial_landmarks.part(eye_points[0]).y
            ),
            (
                facial_landmarks.part(eye_points[1]).x,
                facial_landmarks.part(eye_points[1]).y
            ),
            (
                facial_landmarks.part(eye_points[2]).x,
                facial_landmarks.part(eye_points[2]).y
            ),
            (
                facial_landmarks.part(eye_points[3]).x,
                facial_landmarks.part(eye_points[3]).y
            ),
            (
                facial_landmarks.part(eye_points[4]).x,
                facial_landmarks.part(eye_points[4]).y
            ),
            (
                facial_landmarks.part(eye_points[5]).x,
                facial_landmarks.part(eye_points[5]).y
            )], np.int32)

        height, width, _ = frame.shape
        mask = np.zeros((height, width), np.uint8)
        cv2.polylines(mask, [left_eye_region], True, 255, 2)
        cv2.fillPoly(mask, [left_eye_region], 255)
        eye = cv2.bitwise_and(gray, gray, mask=mask)

        min_x = np.min(left_eye_region[:, 0])
        max_x = np.max(left_eye_region[:, 0])
        min_y = np.min(left_eye_region[:, 1])
        max_y = np.max(left_eye_region[:, 1])

        gray_eye = eye[min_y: max_y, min_x: max_x]
        _, threshold_eye = cv2.threshold(gray_eye, 70, 255, cv2.THRESH_BINARY)
        height, width = threshold_eye.shape
        left_side_threshold = threshold_eye[0: height, int(width / 2): width]
        left_side_white = cv2.countNonZero(left_side_threshold)

        right_side_threshold = threshold_eye[0: height, 0: int(width / 2)]
        right_side_white = cv2.countNonZero(right_side_threshold)

        under_side_threshold = threshold_eye[int(height/2):height, 0:width]
        under_side_white = cv2.countNonZero(under_side_threshold)

        if left_side_white == 0:
            gaze_right_level = 0
        elif right_side_white == 0:
            gaze_right_level = 1
        else:
            gaze_right_level = left_side_white / (right_side_white+left_side_white)

        return gaze_right_level, under_side_white, left_eye_region

    def gaze_preprocess(self, frame, face):

        #ランドマーク
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        landmarks = self.predictor(gray, face)
        return landmarks, gray

    def get_eye_blink_state(self, frame, landmarks, facial_landmarks):
        x = [0 for i in range(len(facial_landmarks))]
        y = [0 for i in range(len(facial_landmarks))]

        for i, facial_landmark in enumerate(facial_landmarks):
            x[i] = landmarks.part(facial_landmark).x
            y[i] = landmarks.part(facial_landmark).y

        trim_val = 2
        frame_trim = frame[y[1]-trim_val:y[3]+trim_val,x[0]:x[2]]
        height, width = frame_trim.shape[0],frame_trim.shape[1]
        frame_trim_resize = cv2.resize(frame_trim , (int(width*7.0), int(height*7.0)))
        # gray scale
        frame_gray = cv2.cvtColor(frame_trim_resize, cv2.COLOR_BGR2GRAY)
        # 平滑化
        frame_gray = cv2.GaussianBlur(frame_gray,(7,7),0)
        # 二値化
        thresh = 80
        maxval = 255
        e_th, frame_black_white = cv2.threshold(frame_gray,thresh,maxval,cv2.THRESH_BINARY_INV)
        eye_contours, _ = cv2.findContours(frame_black_white, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        if len(eye_contours) == 0:
            return EyeState.CLOSE
        # (rx, ry, rw, rh) = cv2.boundingRect(eye_contours[0])
        # cv2.circle(frame_trim_resize, (int(rx+rw/2), int(ry+rh/2)), int((rw+rh)/4), (255, 0, 0), 2) #円で表示
        # cv2.circle(frame, (int(x[0]+(rx+rw)/10), int(y[1]-3+(ry+rh)/10)), int((rw+rh)/20), (0, 255, 0), 1)    #元画像に表示
        return EyeState.OPEN

    def run(self):
        # この関数のwhile True:下でcapから画像を取得して、ジェスチャーを判別、
        # queueに(Gestureオブジェクト、time.time())の形でジェスチャーを入れていってください。
        while True:
            _, frame = self.cap.read()
            self.gaze.refresh(frame)
            frame = self.gaze.annotated_frame()
            face = inference(frame)
            start, end = face
            if start == -1 and end == -1:
                cv2.imshow('frame', frame)
                cv2.waitKey(1)
                continue

            (left, top), (right, bottom) = start, end
            if self.debug:
                cv2.rectangle(frame, start, end, (255, 0, 0), 2)
            dlib_face = dlib.rectangle(left=left, top=top, right=right, bottom=bottom)
            landmarks, gray = self.gaze_preprocess(frame, dlib_face)
            if landmarks == -1 and gray == -1:
                continue

            # まばたきの計測
            left_eye_state, right_eye_state, left_eye_position, right_eye_position = get_eye_state(frame, landmarks)

            #口の開閉度測定
            mouth_landmarks = [60, 61, 63, 64, 65, 67]
            mouth_ratio, mouth_region = self.get_mouth_ratio(mouth_landmarks, landmarks)

            eye_direction = EyeDirection.CENTER
            if self.gaze.is_right():
                eye_direction = EyeDirection.RIGHT
            elif self.gaze.is_left():
                eye_direction = EyeDirection.LEFT
            else:
                eye_direction = EyeDirection.CENTER

            if self.debug:
                def draw_eye(position, state):
                    x, y, x1, y1 = position
                    cv2.rectangle(frame, (x, y), (x1, y1), (255, 0, 0), 2)

                    text = ""
                    if state == EyeState.OPEN:
                        text = "open"
                    else:
                        text = "close"

                    cv2.putText(frame, text, (x, y1), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 5)

                draw_eye(left_eye_position, left_eye_state)
                draw_eye(right_eye_position, right_eye_state)
                cv2.polylines(frame, pts=[mouth_region], isClosed=True, color=(0, 255, 0), thickness=2)
                cv2.imshow("frame", frame)
                key = cv2.waitKey(1)
                if key ==27:
                    break

            if self.queue.full():
                self.queue.queue.clear()

            gestures = Gestures(
                eye_direction=eye_direction,
                left_eye_state=left_eye_state,
                right_eye_state=right_eye_state,
                mouth_state=self.get_mouth_state(mouth_ratio),
            )

            if (
                    gestures.eye_direction != EyeDirection.CENTER or
                    gestures.left_eye_state != gestures.right_eye_state or
                    ((self.previous == None or self.previous.mouth_state == MouthState.CLOSE) and gestures.mouth_state == MouthState.OPEN)
                ):
                self.queue.put((gestures, time.time()))

            self.previous = copy.deepcopy(gestures)

if __name__ == '__main__':
    queue = get_queue()
    FaceGestureDetector(queue).run()


import json
import logging
import os
import uuid

logger = logging.getLogger(__name__)

ALLOWED_EXTENSIONS = ['.wav', '.mp3', '.m4a']

class KnownRequestParseError(Exception):
    """
    Occurs when encountering an HTTP request that does not conform to the API interface.
    """
    pass

class UploadedFile(object):
    """
    Represents a file extracted from a multipart/form-data HTTP request and saved to disk.
    """
    def __init__(self, original_filename, stored_filename, mime_type, metadata=None):
        self.original_filename = original_filename
        self.stored_filename = stored_filename
        self.mime_type = mime_type
        self.metadata = metadata or {}

def extract_file_from_http_request(headers, body, upload_dir, unique_id=None):
    """
    Parse the raw HTTP POST request and extract the audio file from it.

    Parameters
    ----------
    headers : dict
        Request headers.
    body : bytes
        Raw request body.
    upload_dir: str
        Path to the folder for storing extracted files.
    unique_id : str (optional)
        A string uniquely identifying this HTTP request.

    Returns
    -------
    UploadedFile
    """
    headers = {
        header.lower(): value
        for header, value in headers.items()
    }
    if 'content-type' not in headers or 'boundary=' not in headers['content-type']:
        msg = 'Expected a multipart/form-data request with content type defined'
        raise KnownRequestParseError(msg)

    unique_id = unique_id or str(uuid.uuid4())
    content_type = headers['content-type']

    # Separate the file part from multipart form data.
    newline = b'\r\n'
    boundary = content_type.split('boundary=')[-1].replace('"', '').encode('utf-8')
    initial_delimiter = b'--' + boundary + newline
    final_delimiter = newline + b'--' + boundary + b'--'
    try:
        file_part = body.split(initial_delimiter)[1].split(final_delimiter)[0]
    except Exception:
        logger.warning('Failed to parse the raw multipart body', exc_info=True)
        raise KnownRequestParseError('Expected a non-empty multipart/form-data body')

    # Separate Content-Disposition, Content-Type (optional), and file content.
    file_part_lines = [line for line in file_part.split(newline) if line]
    cdisp_line = None
    ctype_line = None
    file_content = None

    for i, line in enumerate(file_part_lines):
        if line.startswith(b'Content-Disposition'):
            cdisp_line = line.decode('utf-8')
        elif line.startswith(b'Content-Type'):
            ctype_line = line.decode('utf-8')
        else:
            file_content = newline.join([fpl for fpl in file_part_lines[i:]])
            break

    # Parse Content-Disposition to extract parameter name and original filename.
    cdisp_line = cdisp_line.replace('; name=', '"name": ')
    cdisp_line = cdisp_line.replace('; filename=', ', "filename": ')
    cdisp_line = '{' + cdisp_line.replace('Content-Disposition: form-data', '') + '}'
    try:
        content_disposition = json.loads(cdisp_line)
    except Exception:
        logger.warning(f'Failed to parse Content-Disposition line: {cdisp_line}', exc_info=True)
        raise KnownRequestParseError('Malformed request body')

    # Check if the request form is valid.
    if content_disposition['name'] != 'audio-file':
        raise KnownRequestParseError('Expected a file with key "audio-file" in the request')

    original_filename = content_disposition['filename']
    original_extension = os.path.splitext(original_filename)[1].lower()
    mime_type = ctype_line.replace('Content-Type: ', '') if ctype_line else 'text/plain'
    logger.info(f'Original filename: "{original_filename}". Inferred MIME type: {mime_type}')

    if original_extension not in ALLOWED_EXTENSIONS:
        msg = 'Only the following file extensions are supported: ' + ', '.join(ALLOWED_EXTENSIONS)
        raise KnownRequestParseError(msg)

    # Save the file to disk.
    storage_path = os.path.join(upload_dir, f'{unique_id}{original_extension}')
    logger.info('Saving uploaded file ({} bytes) to: "{}"'.format(len(file_content), storage_path))
    with open(storage_path, 'wb') as f:
        f.write(file_content)

    return UploadedFile(
        original_filename=original_filename,
        stored_filename=storage_path,
        mime_type=mime_type,
    )


from PyQt5 import QtGui
from PyQt5.QtWidgets import *
import requests
from bs4 import BeautifulSoup

URL = "https://kitsunekko.net/dirlist.php?dir=subtitles%2Fjapanese%2F"
r = requests.get(URL)
soup = BeautifulSoup(r.content, 'html.parser')

animeLinks = []
animeNames = []
table = soup.find('table', attrs = {'id':'flisttable'})
for row in table.findAll('td', attrs = {'class':''}):
    animeURL = row.a['href']
    anime = row.find("strong").text
    animeNames.append(anime)
    animeLinks.append('https://kitsunekko.net/' + animeURL)

class MainWindow(QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        self.setWindowTitle("Kitsunekko Crawler")
        self.setWindowIcon(QtGui.QIcon('logo.png'))

        self.animeIndex = 0

        # 0 = all, 1 = SRT, 2 = ASS, 3 = ZIP
        self.downloadIndicator = 0
        self.fileName = "subs.srt"
        self.subtitleLinks = []

        layout = QVBoxLayout()
        animeNameLabel = QLabel("Select Anime: ")
        downloadAllbutton = QPushButton("Download All Files")
        downloadAllbutton.clicked.connect(self.downloadAll)
        downloadSRTbutton = QPushButton("Download SRT Files")
        downloadSRTbutton.clicked.connect(self.setSRT)
        downloadSRTbutton.clicked.connect(self.downloadAll)
        downloadASSbutton = QPushButton("Download ASS Files")
        downloadASSbutton.clicked.connect(self.setASS)
        downloadASSbutton.clicked.connect(self.downloadAll)
        downloadZIPbutton = QPushButton("Download ZIP and RAR Files")
        downloadZIPbutton.clicked.connect(self.setZIP)
        downloadZIPbutton.clicked.connect(self.downloadAll)

        comboBoxAnimeNames = QComboBox()
        comboBoxAnimeNames.addItems(animeNames)
        comboBoxAnimeNames.currentIndexChanged.connect(self.index_changed)
        layout.addWidget(animeNameLabel)
        layout.addWidget(comboBoxAnimeNames)
        layout.addWidget(downloadAllbutton)
        layout.addWidget(downloadSRTbutton)
        layout.addWidget(downloadASSbutton)
        layout.addWidget(downloadZIPbutton)

        comboBoxAnimeNames = QWidget()
        comboBoxAnimeNames.setLayout(layout)

        self.setCentralWidget(comboBoxAnimeNames)

    def index_changed(self, i):  # i is an int
        self.animeIndex = i

    def downloadAll(self):
        URL = animeLinks[self.animeIndex]
        r = requests.get(URL)
        soup = BeautifulSoup(r.content, 'html.parser')

        self.subtitleLinks = []
        table = soup.find('table', attrs={'id': 'flisttable'})
        for row in table.findAll('td', attrs={'class': ''}):
            subtitle = row.a['href']
            self.subtitleLinks.append('https://kitsunekko.net/' + subtitle)

        for i in range(len(self.subtitleLinks)):
            if self.downloadIndicator == 1 or self.downloadIndicator == 0:
                if self.subtitleLinks[i].rsplit('.', 1)[-1] == 'srt':
                    self.downloadSRT(i)
            if self.downloadIndicator == 2 or self.downloadIndicator == 0:
                if self.subtitleLinks[i].rsplit('.', 1)[-1] == 'ass':
                    self.downloadASS(i)
            if self.downloadIndicator == 3 or self.downloadIndicator == 0:
                if self.subtitleLinks[i].rsplit('.', 1)[-1] == 'zip' or self.subtitleLinks[i].rsplit('.', 1)[-1] == 'rar' or self.subtitleLinks[i].rsplit('.', 1)[-1] == '7z':
                    self.downloadZIP(i)

    def downloadSRT(self, i):
        fileName = self.subtitleLinks[i].rsplit('/', 1)[-1]
        r = requests.get(self.subtitleLinks[i], stream=True)
        with open(fileName, "wb") as srt:
            srt.write(r.content)

    def downloadASS(self, i):
        fileName = self.subtitleLinks[i].rsplit('/', 1)[-1]
        r = requests.get(self.subtitleLinks[i], stream=True)
        with open(fileName, "wb") as ass:
            ass.write(r.content)

    def downloadZIP(self, i):
        fileName = self.subtitleLinks[i].rsplit('/', 1)[-1]
        r = requests.get(self.subtitleLinks[i], stream=True)
        with open(fileName, "wb") as code:
            code.write(r.content)

    def setSRT(self):
        self.downloadIndicator = 1

    def setASS(self):
        self.downloadIndicator = 2

    def setZIP(self):
        self.downloadIndicator = 3

app = QApplication([])

window = MainWindow()
window.show()

app.exec_()


from datetime import datetime
import re
import urllib.request
import json
import time
import csv
import paramiko
import pods
import getfiles
import sys
import threading
from multiprocessing import Process
from multiprocessing.pool import ThreadPool
import subprocess
import os
import signal
import usbmeter

url_pods = "http://192.168.101.243:30000/api/v1/query?query=kubelet_running_pods{kubernetes_io_hostname='server06'}"
values_pods_t=json.loads(urllib.request.urlopen(url_pods).read())["data"]["result"][0]['value'][1]
DEFAULT_PODS=int(values_pods_t)

PI_IP = "192.168.101.70"
TEST_MODE = False

finished = False
timestamps={}
jobs_status = {
    "warm_done" : False,
    "cold_done" : False,
    "delete_done": False}

def start_master(command:str):

    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect('192.168.101.243', username='fil', password='1')
    print(command)
    stdin, stdout, stderr = client.exec_command(command)

    for line in stdout:
        print (line.strip('\n'))

    client.close()

def get_prom_pod_time(values:dict, target_pods:int, repetition: int):
    success = False
    url_time = "http://192.168.101.243:30000/api/v1/query?query=kube_pod_status_phase{namespace='capture',job='Kubernetes'}"
    raw = urllib.request.urlopen(url_time).read()
    output = json.loads(raw)

    for pod in output["data"]["result"]:
        try:
            pod_name = pod['metric']['pod']
            if pod_name in values.keys():
                phase = pod['metric']['phase']
                status = pod['value'][1]
                if(str(values[pod_name][0]) == "0" and phase == "Pending" and status == "1"):
                    values[pod_name][0] = pod['value'][0]
                    print("Pending!")
                elif(str(values[pod_name][1]) == "0" and phase == "Running" and status == "1"):
                    values[pod_name][1] = pod['value'][0]
                    values[pod_name][2] = datetime.utcfromtimestamp(float( values[pod_name][1]) - float(values[pod_name][0])).strftime('%H:%M:%S:%f')
                    print("Running!")
                else:
                    dummy = 0
            else:
                # initialize the values
                values[pod_name] = ["0", "0", "0"]
        except Exception as e:
            print(e)
            continue

    if len(values) == int(target_pods) and (not any ("0" in x for x in list(values.values()))):
        writer = csv.writer(open("/home/luongtrann/hanoi/code/data/pod_start_time_{}_{}.csv".format(str(target_pods), str(repetition)), 'w'))
        for key, value in values.items():
            writer.writerow([key, value])
        success = True
    # else:
    #     #print("Condition is not satisfied.")
    #     print(len(values))
    #     print("require: " + str(target_pods))
    #     print(values)
    return success, values
def get_data_from_api(query:str, instance:str):
    url_data = "http://192.168.101.243:30000/api/v1/query?query=" + query
    try:
        contents = urllib.request.urlopen(url_data).read().decode('utf-8')
        values=json.loads(contents)["data"]["result"][0]['value']
    except:
        values = -1
    return values

def get_prometheus_values(target_pods:int, job:str, repetition: int):

    #CPU Usage
    url_cpu = "http://192.168.101.243:30000/api/v1/query?query=100-(avg%20by%20(instance,%20job)%20(irate(node_cpu_seconds_total{mode=%22idle%22,%20job=%22node_exporter_metrics%22,%20instance=%22192.168.101.72:9100%22}[30s])*100))"
    try:
        contents_cpu = urllib.request.urlopen(url_cpu).read().decode('utf-8')
        values_cpu=json.loads(contents_cpu)["data"]["result"][0]['value']
    except:
        values_cpu = "-1"
    #number of pods.
    url_pods = "http://192.168.101.243:30000/api/v1/query?query=kubelet_running_pods{kubernetes_io_hostname='server06'}"
    try:
        contents_pods = urllib.request.urlopen(url_pods).read()
        values_pods=json.loads(contents_pods)["data"]["result"][0]['value']
    except:
        values_pods="-1"
    #memory status
    url_memory = "http://192.168.101.243:30000/api/v1/query?query=(node_memory_MemTotal_bytes{job='node_exporter_metrics',instance='192.168.101.72:9100'}-node_memory_MemAvailable_bytes{job='node_exporter_metrics',instance='192.168.101.72:9100'})/(1024*1024)"
    try:
        contents_memory = urllib.request.urlopen(url_memory).read()
        values_memory=json.loads(contents_memory)["data"]["result"][0]['value']
    except:
        values_memory = "-1"

    #write values to file
    try:
        writer = csv.writer(open("/home/luongtrann/hanoi/code/data/server/data-prometheus_{}_{}_server.csv".format(str(target_pods), str(repetition)), 'a'))
        writer.writerow([datetime.utcfromtimestamp(values_pods[0]).strftime('%Y-%m-%d %H:%M:%S'), values_pods[1], values_cpu[1], values_memory[1], job])
    except:
        print("Error")
    #+2 on target pods for the default pods
    curr_pods = int(values_pods[1])
    if TEST_MODE: print("Current pods: %s, target: %d" % (curr_pods, (int(target_pods)+DEFAULT_PODS)))
    if(curr_pods>=(int(target_pods)+DEFAULT_PODS)):
       jobs_status["cold_done"] = True
       jobs_status["delete_done"] = False

    elif(curr_pods==DEFAULT_PODS):
        jobs_status["delete_done"] = True

def start_measurement_prometheus_time(target_pods: int, repetition: int):
    pod_start_status = False # variable for Kien function
    values = {} # variable for Kien function
    if(int(target_pods) < 5):
        return
    while pod_start_status != True:
        pod_start_status, values = get_prom_pod_time(values, target_pods, repetition)
        time.sleep(0.2)
    return

def start_measurement_prometheus(minutes: int, target_pods: int, repetition: int):
    '''
    minutes: defines the time to measure the warm scenario
    '''
    print("Scenario: COLD - Started...")
    timestamps["cold_start"]= time.time()
    while not jobs_status["cold_done"]:
        get_prometheus_values(target_pods, "cold", repetition)
        time.sleep(1)
    timestamps["cold_end"]= time.time()

    time.sleep(15)

    print("Scenario: WARM - Started...")
    timestamps["warm_start"]= time.time()
    warm_endtime = time.time()+(60*int(minutes)) #5min x 60
    while warm_endtime-time.time() > 0:
        get_prometheus_values(target_pods, "warm", repetition)
        time.sleep(1)
    jobs_status["warm_done"] = True
    timestamps["warm_end"]= time.time()

    #time.sleep(30) # there is no point to sleep here

    print("Scenario: DELETE - Started...")
    timestamps["delete_start"]= time.time()
    pods.delete_pods()
    while not jobs_status["delete_done"]:
        get_prometheus_values(target_pods, "delete", repetition)
        time.sleep(1)
        if jobs_status["delete_done"]:
            timestamps["pods_deleted"]= time.time()
            for i in range(0, 5, 1):
                get_prometheus_values(target_pods, "delete_after", repetition)
                time.sleep(1)
            timestamps["pods_deleted_after"]= time.time()

    #time.sleep(30) # there is no point to sleep here

    print("Measurement finished.")
    print("Saving timestamps..")
    timestamps_to_file(target_pods, repetition)
    print("Done")
    global finished
    finished = True
    return

def start_ummeter(target_pods: int, rep: int, time:int):
    cmd = '/usr/bin/python3 /home/luongtrann/usbmeter/usbmeter --addr 00:16:A5:00:0F:65 --out /home/luongtrann/hanoi/code/data_ummeter/data_ummeter_{}_{} --time {}'.format(target_pods, rep, time)
    s = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE)

def timestamps_to_file(target_pods:int, repetition:int):
    with open('/home/luongtrann/hanoi/code/data/server/timestamps_{}_{}_server.csv'.format(str(target_pods), str(repetition)), 'w') as f:
        for key in timestamps.keys():
            f.write("%s,%s\n"%(key,timestamps[key]))

if __name__ == "__main__":

    """
    call: python3 main.py [COMMAND] [TARGET_PODS] [MINUTES_WARM]
    """
    target_pods = sys.argv[2]
    minutes = sys.argv[3]
    rep = sys.argv[4]
    if sys.argv[1] == "master":
        #Update replicas
        cmd = '/usr/bin/python3 /home/fil/filip/hanoi/code/main.py changevalue {} {} {}'.format(str(target_pods), str(minutes), str(rep))
        start_master(cmd)
        #p3=Process(target=start_measurement_prometheus_time, args=(target_pods, rep, ), daemon = True)
        p2=Process(target=start_measurement_prometheus, args=(minutes, target_pods, rep, ), daemon = True)
        p1=Process(target=usbmeter.main, args=(target_pods, rep, minutes, ), daemon = True)
        #p3.start()
        p1.start()
        p2.start()
        p1.join()
        p2.join()
        #p3.join()
        print("Every process is done.")
    elif sys.argv[1] == "changevalue":
        usbmeter.main(target_pods, rep, minutes, )

    else: print("Not recognized command")


import webob
import webob.dec

from keystone.common import logging
from keystone.common import wsgi
from keystone import config
from keystone.openstack.common import timeutils

CONF = config.CONF
LOG = logging.getLogger('access')
APACHE_TIME_FORMAT = '%d/%b/%Y:%H:%M:%S'
APACHE_LOG_FORMAT = (
    '%(remote_addr)s - %(remote_user)s [%(datetime)s] "%(method)s %(url)s '
    '%(http_version)s" %(status)s %(content_length)s')

class AccessLogMiddleware(wsgi.Middleware):
    """Writes an access log to INFO."""

    @webob.dec.wsgify
    def __call__(self, request):
        data = {
            'remote_addr': request.remote_addr,
            'remote_user': request.remote_user or '-',
            'method': request.method,
            'url': request.url,
            'http_version': request.http_version,
            'status': 500,
            'content_length': '-'}

        try:
            response = request.get_response(self.application)
            data['status'] = response.status_int
            data['content_length'] = len(response.body) or '-'
        finally:
            # must be calculated *after* the application has been called
            now = timeutils.utcnow()

            # timeutils may not return UTC, so we can't hardcode +0000
            data['datetime'] = '%s %s' % (now.strftime(APACHE_TIME_FORMAT),
                                          now.strftime('%z') or '+0000')

            LOG.info(APACHE_LOG_FORMAT % data)
        return response


import unittest

from hummingbot.connector.exchange.bitmex.bitmex_order_status import BitmexOrderStatus

class BitmexOrderStatusUnitTests(unittest.TestCase):
    def test_ge(self):
        status_1 = BitmexOrderStatus.New
        status_2 = BitmexOrderStatus.PartiallyFilled

        self.assertTrue(status_2 >= status_1)
        self.assertEqual(status_2.__ge__(1), NotImplemented)

    def test_gt(self):
        status_1 = BitmexOrderStatus.Canceled
        status_2 = BitmexOrderStatus.FAILURE

        self.assertTrue(status_2 > status_1)
        self.assertEqual(status_2.__gt__(1), NotImplemented)

    def test_le(self):
        status_1 = BitmexOrderStatus.New
        status_2 = BitmexOrderStatus.Canceled

        self.assertTrue(status_1 <= status_2)
        self.assertEqual(status_2.__le__(1), NotImplemented)

    def test_lt(self):
        status_1 = BitmexOrderStatus.PartiallyFilled
        status_2 = BitmexOrderStatus.FAILURE

        self.assertTrue(status_1 < status_2)
        self.assertEqual(status_2.__lt__(1), NotImplemented)


import sys
from array import array

from lvtools import chunk, util, graphics, palette, level_info
from lvtools.compression import decompress_data
import png
import dump_tileset as tileset

def parse_metatile_info(data):
    metatiles = []
    for i in xrange(0, len(data), 2 * 4):
        # Read 4 values from data
        tile = (util.read_word(data, i+offs) for offs in xrange(0, 8, 2))
        # Extract attributes
        tile = [(x >> 6, x & 0x10, x & 0x20) for x in tile]
        metatiles.append(tile)
    return metatiles

def main(level_header, output_file, tiles_per_row):
    output_pal = palette.assemble_palette(level_header.load_list.palettes)
    tiles = tileset.extract_tiles(level_header)

    metatile_data = decompress_data(chunk.read_chunk(level_header.metatile_chunk))
    meta_info = parse_metatile_info(metatile_data)
    del metatile_data

    def assemble_metatile(info):
        flipped_tiles = [graphics.flip_image(tiles[i], 8, hf, vf) for i, hf, vf in info]
        return graphics.layout_tiles(flipped_tiles, 8, 8, [[0, 1], [2, 3]])[0]

    meta_imgs = [assemble_metatile(info) for info in meta_info]

    output_data, output_w, output_h = graphics.layout_tiles(meta_imgs, 16, 16, tileset.rows_of_tiles(len(meta_imgs), tiles_per_row))

    with open(output_file, 'wb') as f:
        w = png.Writer(output_w, output_h, palette=output_pal)
        w.write_array(f, output_data)

if __name__ == '__main__':
    level_id = int(sys.argv[1], 0)
    level_header = level_info.load_level_header(level_id=level_id)
    main(level_header, sys.argv[2], int(sys.argv[3]))


import pandas as pd
import generic_topic_detector

filepath = "C:\\Users\\rupachak\\Desktop\\Kaggle Data\\Ethereum Developer Interviews\\interview.csv"
interview_frame = pd.read_csv(filepath)
text_list = interview_frame['Who are you and what are you working on?'].values
text_list = list(map(lambda x:str(x),text_list))

lda_html = generic_topic_detector.get_formatted_html_data(text_list)
with open('eth_topics.html','w') as lda_topic:
    lda_topic.write(lda_html)


import asyncio

import pandas as pd
from stockpredict.fetcher.api_client import AbstractApi

from stockpredict.settings import BALANCE_CASH_INCOME_DROP, CASH_ADDITIONAL_DROP, BALANCE_CASH_INCOME_JOIN_KEYS, \
    INFERENCE_DROP, DTYPE
from stockpredict.util.helpers import async_resolver

class DataFetcher:
    def __init__(self, client: AbstractApi):
        self.client = client

    def get_data(self, marker: str, period: str = "annual") -> pd.DataFrame:
        task = (asyncio.gather(self.client.get_balance_sheet(marker=marker, period=period, limit=1),
                               self.client.get_cash_flow(marker=marker, period=period, limit=1),
                               self.client.get_income_statement(marker=marker, period=period, limit=1)
                               ))
        balance, cash, income = async_resolver(task)
        balance_df = pd.DataFrame.from_dict(balance)
        cash_flow_df = pd.DataFrame.from_dict(cash)
        income_df = pd.DataFrame.from_dict(income)

        return self.preprocess(balance_df, cash_flow_df, income_df)

    def preprocess(self, balance_sheet: pd.DataFrame, cash_flow: pd.DataFrame, income: pd.DataFrame) -> pd.DataFrame:
        balance_sheet.drop(columns=BALANCE_CASH_INCOME_DROP, inplace=True)
        cash_flow.drop(columns=[*BALANCE_CASH_INCOME_DROP, *CASH_ADDITIONAL_DROP], inplace=True)
        income.drop(columns=BALANCE_CASH_INCOME_DROP, inplace=True)

        balance_sheet.set_index(BALANCE_CASH_INCOME_JOIN_KEYS, inplace=True)
        cash_flow.set_index(BALANCE_CASH_INCOME_JOIN_KEYS, inplace=True)
        income.set_index(BALANCE_CASH_INCOME_JOIN_KEYS, inplace=True)

        df = balance_sheet.join(cash_flow).join(income)
        df.reset_index(inplace=True)
        df.drop(columns=INFERENCE_DROP, inplace=True)
        df.fillna(value=0, inplace=True)
        df = df.astype(DTYPE)

        return df


import math

def distance(origin, destination):
    """
    Calculate the Haversine distance.

    Parameters
    ----------
    origin : tuple of float
        (lat, long)
    destination : tuple of float
        (lat, long)

    Returns
    -------
    distance_in_km : float

    Examples
    --------
    >>> origin = (48.1372, 11.5756)  # Munich
    >>> destination = (52.5186, 13.4083)  # Berlin
    >>> round(distance(origin, destination), 1)
    504.2
    """
    lat1, lon1 = origin['lat'], origin['long']
    lat2, lon2 = destination['lat'], destination['long']
    radius = 6371  # km

    dlat = math.radians(lat2 - lat1)
    dlon = math.radians(lon2 - lon1)
    a = (math.sin(dlat / 2) * math.sin(dlat / 2) +
         math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) *
         math.sin(dlon / 2) * math.sin(dlon / 2))
    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
    d = radius * c

    return d

# Testing

# print(distance({"long": -79.919225, "lat":43.260879}, {"long": -79.390331772, "lat":43.656997372}))


import boto3
import simplejson as json
from boto3.dynamodb.conditions import Key, Attr
from botocore.exceptions import ClientError
from boto3.dynamodb.types import TypeDeserializer

dynamodb = boto3.client('dynamodb')
table_name = 'Project'

def lambda_handler(event, context):
    try:
        event = json.loads(event['body'])

        project_name = event.get('project_name')
        status = event.get('status')
        description = event.get('description')
        email = event.get('email')
        deadline = event.get('deadline')
        risk_eval = event.get('risk_eval')
        project_id = event.get('project_id')
        team = event.get('team')
        start_date = event.get('start_date')

        # byte_id = (f"{project_name}{email}").encode('utf-8')
        # project_id = (bcrypt.hashpw(byte_id, salt)).decode('utf-8')

        dynamodb.put_item(
            TableName=table_name,
            Item={
                'project_id': {'S': project_id},
                'status': {'N': str(status)},
                'deadline': {'S': deadline},
                'project_name': {'S': project_name},
                'risk_eval': {'N': str(risk_eval)},
                'email': {'S': email},
                'description': {'S': description},
                'team': {'L': [{'S': x} for x in team] },
                'start_date': {'S': start_date}
            }
        )

        return {"code": 200, "message": "Project added successfully"}
    except dynamodb.exceptions.ConditionalCheckFailedException:
        return {"code": 400, "message": f"Project with same name already exists"}
    except Exception as e:
        return {"code": 500, "message": f"An unexpected error occurred while adding project: {e}"}


import torchvision
from torchvision.models.detection import FasterRCNN
from torchvision.models.detection.rpn import AnchorGenerator

def fasterRCNN(backbone, class_num):
    anchor_generator = AnchorGenerator(sizes=((32, 64, 128, 256, 512),),
                                    aspect_ratios=((0.5, 1.0, 2.0),))

    roi_pooler = torchvision.ops.MultiScaleRoIAlign(featmap_names=['0'],
                                                    output_size=7,
                                                    sampling_ratio=2)

    # put the pieces together inside a FasterRCNN model
    model = FasterRCNN(backbone,
                    num_classes=class_num,
                    rpn_anchor_generator=anchor_generator,
                    box_roi_pool=roi_pooler)
    return model


from pyramid_sqlalchemy import Session
from sqlalchemy import engine_from_config, event, DDL
from sqlalchemy.orm import configure_mappers

# import or define all models here to ensure they are attached to the
# Base.metadata prior to any initialization routines
from .mymodel import Product, City, User, Price # flake8: noqa

# run configure_mappers after defining all of the models to ensure
# all relationships can be setup
configure_mappers()

def get_engine(settings, prefix='sqlalchemy.'):
    return engine_from_config(settings, prefix)

def includeme(config):
    """
    Initialize the model for a Pyramid app.

    Activate this setup using ``config.include('multitenant_rls.models')``.

    """
    settings = config.get_settings()
    engine = engine_from_config(settings)
    Session.configure(bind=engine)

    # use pyramid_tm to hook the transaction lifecycle to the request
    config.include('pyramid_tm')

enable_rls_str = "alter table %(table)s enable row level security"
"""Activate RLS for the given table"""

tenancy_policy_str = "create policy %(table)s_tenancy_policy on %(table)s for all " \
                     "using (city_id=current_setting('my.city_id')::integer) " \
                     "with check (city_id=current_setting('my.city_id')::integer)"
"""Create the city constraint policy for the given table"""

event.listen(Price.__table__, 'after_create', DDL(enable_rls_str))
event.listen(Price.__table__, 'after_create', DDL(tenancy_policy_str))
"""In case you want to apply the policy to more than one table, the keyword Table can be used instead naming each table
separately."""


import itertools
import sys
import time
from collections import defaultdict
from collections.abc import Sequence
from typing import Any, TypedDict

import gymnasium as gym
import numpy as np
import numpy.typing as npt
from gymnasium import spaces
from solvers.base import Solver
from utils import Seed

from .graph import SamplingMethod, SATGraph

class G2SATObservation(TypedDict):
    graph: spaces.GraphInstance
    valid_actions: list[tuple[int, int]]
    total_valid_actions: int

class G2SATEnv(gym.Env[dict, npt.NDArray[np.integer]]):
    def __init__(
        self,
        num_vars: int,
        num_clauses: int,
        solver: Solver,
        reward_metric: str,
        num_sampled_pairs: int = 2_000,
        compress_observations: bool = True,
        intermediate_rewards: bool = False,
        intermediate_rewards_coeff: float = 0.1,
        sampling_method: SamplingMethod = "g2sat",
        allow_overlaps: bool = False,
        fixed_templates: Sequence[npt.NDArray[np.int64] | list[list[int]]]
        | None = None,
        solve_repetitions: int = 1,
        solve_agg: str = "mean",
        reference_instances: list[list[list[int]]] | None = None,
        normalize_by_reference: bool = False,
    ) -> None:
        self.num_vars = num_vars
        self.num_clauses = num_clauses
        self.solver = solver
        self.reward_metric = reward_metric
        self.num_sampled_pairs = num_sampled_pairs
        self.compress_observations = compress_observations
        self.intermediate_rewards = intermediate_rewards
        self.intermediate_rewards_coeff = intermediate_rewards_coeff
        self.sampling_method: SamplingMethod = sampling_method
        self.allow_overlaps = allow_overlaps
        self.solve_repetitions = solve_repetitions
        self.solve_agg = solve_agg
        self.reference_instances = reference_instances
        self.normalize_by_reference = normalize_by_reference

        self.fixed_templates = fixed_templates
        if fixed_templates is None:
            self.fixed_templates_iter = None
        else:
            self.fixed_templates_iter = iter(itertools.cycle(fixed_templates))

        max_num_nodes = self._compute_max_node_count()
        self.observation_space = spaces.Dict(
            {
                "graph": spaces.Graph(
                    node_space=spaces.Discrete(2),
                    edge_space=None,
                ),
                "valid_pairs": spaces.Sequence(
                    spaces.MultiDiscrete([max_num_nodes, max_num_nodes]),
                ),
            }
        )
        self.action_space = spaces.MultiDiscrete([max_num_nodes, max_num_nodes])

    def _compute_max_node_count(self) -> int:
        max_num_nodes = self.num_vars * 2 + self.num_clauses * 3
        if self.fixed_templates is None:
            return max_num_nodes

        for template in self.fixed_templates:
            if isinstance(template, list):
                num_vars = max(
                    abs(literal) for clause in template for literal in clause
                )
                num_literals = 2 * num_vars
                num_clauses = sum(len(clause) for clause in template)
            else:
                num_literals = len(template)
                num_clauses = template.sum()
            num_nodes = num_literals + num_clauses
            max_num_nodes = max(max_num_nodes, num_nodes)
        return max_num_nodes

    def step(
        self,
        action: npt.NDArray[np.integer] | tuple[int, int],
    ) -> tuple[G2SATObservation, float, bool, bool, dict[str, Any]]:
        t0 = time.monotonic()
        clause1, clause2 = action

        if tuple(action) not in self.valid_actions:
            msg = f"Invalid action: {action}"
            raise ValueError(msg)

        self.graph.merge(clause1, clause2)

        obs, sample_time = self.get_obs()
        self.valid_actions = obs["valid_actions"]

        is_3sat = self.graph.is_3sat()
        terminated = is_3sat or len(obs["valid_actions"]) == 0
        truncated = False

        if terminated or self.intermediate_rewards:
            metrics = self._get_metrics()
            reward = metrics[self.reward_metric]
            if not terminated:
                reward *= self.intermediate_rewards_coeff
        else:
            metrics = {}
            reward = 0

        t1 = time.monotonic()
        info = {
            "is_3sat": is_3sat,
            "metrics": metrics,
            "timing": {
                "step": t1 - t0,
                "sample_pairs": sample_time,
            },
        }

        return obs, reward, terminated, truncated, info

    def _solve_instance(self, instance: list[list[int]]) -> dict[str, Any]:
        return self.solver.solve_instance_agg(
            instance,
            repetitions=self.solve_repetitions,
            agg_fn=self.solve_agg,
        )

    def _get_metrics(self) -> dict[str, Any]:
        metrics = self._solve_instance(self.graph.to_clauses())
        if self.reference_instances:
            ref_lists = defaultdict(list)
            for instance in self.reference_instances:
                ref = self._solve_instance(instance)
                for k, v_ref in ref.items():
                    ref_lists[k].append(v_ref)
            fn = getattr(np, self.solve_agg)
            for k, v_refs in ref_lists.items():
                metrics[f"ref/{k}"] = fn(v_refs)
                if self.normalize_by_reference:
                    # Add epsilon to avoid divide-by-zero warnings
                    metrics[k] /= metrics[f"ref/{k}"] + sys.float_info.epsilon
        return metrics

    def reset(
        self,
        template: npt.NDArray[np.int64] | list[list[int]] | None = None,
        seed: Seed = None,
    ) -> tuple[G2SATObservation, dict[str, Any]]:
        rng = np.random.default_rng(seed)
        if template is not None:
            # ok, already have template
            pass
        elif self.fixed_templates_iter is not None:
            # get next fixed template
            template = next(self.fixed_templates_iter)
        else:
            # sample a new template
            template = SATGraph.sample_template(
                self.num_vars, self.num_clauses * 3, seed=rng
            )

        if isinstance(template, list):
            self.graph = SATGraph.from_clauses(
                template,
                sampling_method=self.sampling_method,
                allow_overlaps=self.allow_overlaps,
                seed=rng,
            )
        else:
            self.graph = SATGraph.from_template(
                template,
                sampling_method=self.sampling_method,
                allow_overlaps=self.allow_overlaps,
                seed=rng,
            )
        obs, sample_time = self.get_obs()
        self.valid_actions = obs["valid_actions"]

        info = {
            "template": template,
            "timing": {
                "sample_pairs": sample_time,
            },
        }
        return obs, info

    def render(self) -> None:
        self.graph.plot_nx()

    def get_obs(self) -> tuple[G2SATObservation, float]:
        t0 = time.monotonic()
        valid, count = self.graph.sample_valid_merges_with_count(self.num_sampled_pairs)
        t1 = time.monotonic()
        return {
            "graph": self.graph.to_graph_instance(self.compress_observations),
            "valid_actions": valid,
            "total_valid_actions": count,
        }, t1 - t0


"""
    helo
    ~~~~
"""

# flake8: noqa: F401

from .db import (
    binding,
    unbinding,
    execute,
    select_db,
    isbound,
    state,
    FetchResult,
    ExecResult,
    Binder,
    EnvKey,
)
from .types import (
    Tinyint,
    Smallint,
    Int,
    Bigint,
    Bool,
    Auto,
    BigAuto,
    UUID,
    Float,
    Double,
    Decimal,
    Text,
    Char,
    VarChar,
    IP,
    Email,
    URL,
    Date,
    Time,
    DateTime,
    Timestamp,
    K,
    UK,
    F,
    ENGINE,
    ENCODING,
    SQL,
    ON_CREATE,
    ON_UPDATE,
)
from .model import Model, JOINTYPE, ROWTYPE
from .util import (
    adict,
    adictformatter,
    asyncinit,
    singleton,
    singleton_asyncinit,
    argschecker,
    and_,
    or_,
    In,
)
from .g import G

__version__ = '0.0.6'
__license__ = 'MIT'


# Method 1
def Fibonacci(n):
    if n<=0:
        print("Incorrect input")
    elif n ==1:
        return(0)
    elif n == 2:
        return(1)
    else:
        return(Fibonacci(n-1)+Fibonacci(n-2))

num = int(input("Enter the Nth term : "))
print(Fibonacci(num))

# Method 2
def fibonacci(n):
    a = 0
    b = 1
    if n<= 0:
        print("Incorrect input")
    elif n==1:
        return a
    elif n == 2:
        return b
    else:
        for i in range(2,n):
            c = a+b
            a = b
            b = c
        return b

numb = int(input("Enter the Nth term : "))
print(fibonacci(numb))


import numpy as np
import cv2
import cvui
import random

WINDOW1_NAME = 'Window 1'
WINDOW2_NAME = 'Windows 2'

def main():
	# We have one mat for each window.
	frame1 = np.zeros((600, 800, 3), np.uint8)
	frame2 = np.zeros((600, 800, 3), np.uint8)

	# Create variables used by some components
	window1_values = []
	window2_values = []
	window1_checked = [False]
	window1_checked2 = [False]
	window2_checked = [False]
	window2_checked2 = [False]
	window1_value = [1.0]
	window1_value2 = [1.0]
	window1_value3 = [1.0]
	window2_value = [1.0]
	window2_value2 = [1.0]
	window2_value3 = [1.0]

	img = cv2.imread('lena-face.jpg', cv2.IMREAD_COLOR)
	imgRed = cv2.imread('lena-face-red.jpg', cv2.IMREAD_COLOR)
	imgGray = cv2.imread('lena-face-gray.jpg', cv2.IMREAD_COLOR)

	padding = 10

	# Fill the vector with a few random values
	for i in range(0, 20):
		window1_values.append(random.uniform(0., 300.0))
		window2_values.append(random.uniform(0., 300.0))

	# Start two OpenCV windows
	cv2.namedWindow(WINDOW1_NAME)
	cv2.namedWindow(WINDOW2_NAME)

	# Init cvui and inform it to use the first window as the default one.
	# cvui.init() will automatically watch the informed window.
	cvui.init(WINDOW1_NAME)

	# Tell cvui to keep track of mouse events in window2 as well.
	cvui.watch(WINDOW2_NAME)

	while (True):
		# Inform cvui that all subsequent component calls and events are related to window 1.
		cvui.context(WINDOW1_NAME)

		# Fill the frame with a nice color
		frame1[:] = (49, 52, 49)

		cvui.beginRow(frame1, 10, 20, 100, 50)
		cvui.text('This is ')
		cvui.printf('a row')
		cvui.checkbox('checkbox', window1_checked)
		cvui.window(80, 80, 'window')
		cvui.rect(50, 50, 0x00ff00, 0xff0000)
		cvui.sparkline(window1_values, 50, 50)
		cvui.counter(window1_value)
		cvui.button(100, 30, 'Fixed')
		cvui.image(img)
		cvui.button(img, imgGray, imgRed)
		cvui.endRow()

		padding = 50
		cvui.beginRow(frame1, 10, 150, 100, 50, padding)
		cvui.text('This is ')
		cvui.printf('another row')
		cvui.checkbox('checkbox', window1_checked2)
		cvui.window(80, 80, 'window')
		cvui.button(100, 30, 'Fixed')
		cvui.printf('with 50px paddin7hg.')
		cvui.endRow()

		cvui.beginRow(frame1, 10, 250, 100, 50)
		cvui.text('This is ')
		cvui.printf('another row with a trackbar ')
		cvui.trackbar(150, window1_value2, 0., 5.)
		cvui.printf(' and a button ')
		cvui.button(100, 30, 'button')
		cvui.endRow()

		cvui.beginColumn(frame1, 50, 330, 100, 200)
		cvui.text('Column 1 (no padding)')
		cvui.button('button1')
		cvui.button('button2')
		cvui.text('End of column 1')
		cvui.endColumn()

		padding = 10
		cvui.beginColumn(frame1, 300, 330, 100, 200, padding)
		cvui.text('Column 2 (padding = 10)')
		cvui.button('button1')
		cvui.button('button2')
		cvui.trackbar(150, window1_value3, 0., 5., 1, '%3.2Lf', cvui.TRACKBAR_DISCRETE, 0.25)
		cvui.text('End of column 2')
		cvui.endColumn()

		cvui.beginColumn(frame1, 550, 330, 100, 200)
		cvui.text('Column 3 (use space)')
		cvui.space(5)
		cvui.button('button1 5px below')
		cvui.space(50)
		cvui.text('Text 50px below')
		cvui.space(20)
		cvui.button('Button 20px below')
		cvui.space(40)
		cvui.text('End of column 2 (40px below)')
		cvui.endColumn()

		# Update all components of window1, e.g. mouse clicks, and show it.
		cvui.update(WINDOW1_NAME)
		cv2.imshow(WINDOW1_NAME, frame1)

		# From this point on, we are going to render the second window. We need to inform cvui
		# that all updates and components from now on are connected to window 2.
		# We do that by calling cvui.context().
		cvui.context(WINDOW2_NAME)
		frame2[:] = (49, 52, 49)

		cvui.beginRow(frame2, 10, 20, 100, 50)
		cvui.text('This is ')
		cvui.printf('a row')
		cvui.checkbox('checkbox', window2_checked)
		cvui.window(80, 80, 'window')
		cvui.rect(50, 50, 0x00ff00, 0xff0000)
		cvui.sparkline(window2_values, 50, 50)
		cvui.counter(window2_value)
		cvui.button(100, 30, 'Fixed')
		cvui.image(img)
		cvui.button(img, imgGray, imgRed)
		cvui.endRow()

		padding = 50
		cvui.beginRow(frame2, 10, 150, 100, 50, padding)
		cvui.text('This is ')
		cvui.printf('another row')
		cvui.checkbox('checkbox', window2_checked2)
		cvui.window(80, 80, 'window')
		cvui.button(100, 30, 'Fixed')
		cvui.printf('with 50px paddin7hg.')
		cvui.endRow()

		# Another row mixing several components
		cvui.beginRow(frame2, 10, 250, 100, 50)
		cvui.text('This is ')
		cvui.printf('another row with a trackbar ')
		cvui.trackbar(150, window2_value2, 0., 5.)
		cvui.printf(' and a button ')
		cvui.button(100, 30, 'button')
		cvui.endRow()

		cvui.beginColumn(frame2, 50, 330, 100, 200)
		cvui.text('Column 1 (no padding)')
		cvui.button('button1')
		cvui.button('button2')
		cvui.text('End of column 1')
		cvui.endColumn()

		padding = 10
		cvui.beginColumn(frame2, 300, 330, 100, 200, padding)
		cvui.text('Column 2 (padding = 10)')
		cvui.button('button1')
		cvui.button('button2')
		cvui.trackbar(150, window2_value3, 0., 5., 1, '%3.2Lf', cvui.TRACKBAR_DISCRETE, 0.25)
		cvui.text('End of column 2')
		cvui.endColumn()

		cvui.beginColumn(frame2, 550, 330, 100, 200)
		cvui.text('Column 3 (use space)')
		cvui.space(5)
		cvui.button('button1 5px below')
		cvui.space(50)
		cvui.text('Text 50px below')
		cvui.space(20)
		cvui.button('Button 20px below')
		cvui.space(40)
		cvui.text('End of column 2 (40px below)')
		cvui.endColumn()

		# Update all components of window2, e.g. mouse clicks, and show it.
		cvui.update(WINDOW2_NAME)
		cv2.imshow(WINDOW2_NAME, frame2)

		# Check if ESC key was pressed
		if cv2.waitKey(20) == 27:
			break

if __name__ == '__main__':
	main()


import pandas as pd
import numpy as np

df = pd.read_csv('dataset/ISEAR.csv')
# df['split'] = np.random.randn(df.shape[0], 1)

msk = np.random.rand(len(df)) <= 0.7
train = df[msk]
test = df[~msk]

train.to_csv('dataset/train_before.csv')
test.to_csv('dataset/test_before.csv')


#
# @lc app=leetcode id=19 lang=python3
#
# [19] Remove Nth Node From End of List
#

# @lc code=start
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeNthFromEnd(self, head , n ):
        # Step 1: Create two pointers, slow and fast, and initialize them to the head of the linked list.
        slow = fast = head

        # Step 2: Move the fast pointer n steps ahead in the linked list.
        for i in range(n):
            if  fast  is None:
                # If the fast pointer reaches the end before moving n steps, it means n is greater than the length of the linked list.
                # In this case, we can return the original head as no node needs to be removed.
                return head
            fast = fast.next

        # Step 3: Move both the slow and fast pointers simultaneously until the fast pointer reaches the end of the linked list.
        while fast.next:
            fast = fast.next
            slow = slow.next

        # Step 4: At this point, the slow pointer is pointing to the node just before the node to be removed.
        # We update the next pointer of the slow node to skip the next node, effectively removing it from the linked list.
        slow.next = slow.next.next

        # Step 5: Return the head of the modified linked list.
        return head

# @lc code=end


def canJump1(nums):
    start_points = dict()

    def helper(start):
        if nums[start] >= len(nums)-start-1:
            return True
        if start > len(nums)-1:
            return False
        if start not in start_points:
            start_points[start] = any([helper(start+i+1) for i in range(0, nums[start])])
        return start_points[start]

    return helper(0)

def canJump2(nums):
    last_pos = len(nums)-1
    for i in range(len(nums)-1, -1, -1):
        if i+nums[i] >= last_pos:
            last_pos = i
    return last_pos == 0


from django.contrib import admin

from .models import Menu, Price, Category, Group, Image

class ImageInline(admin.TabularInline):
    model = Image
    extra = 3

class PriceInline(admin.TabularInline):
    model = Price
    extra = 1
    exclude = ('extra',)

class MenuAdmin(admin.ModelAdmin):
    inlines = [PriceInline]

    def created_at_f(self, obj):
        return obj.created_at.strftime("%d.%m.%Y")

    created_at_f.admin_order_field = 'created_at'
    created_at_f.short_description = 'Созданно'

    def prices(self, obj):
        return obj.prices.all()

    list_display = ('name', 'created_at_f', 'is_active')
    list_filter = ['is_active']
    #list_editable = ['is_special']
    search_fields = ['name']
    exclude = ('extra',)
    date_hierarchy = 'created_at'

class CategoryAdmin(admin.ModelAdmin):
    list_display = ('name', 'group')
    search_fields = ['name']
    exclude = ('template', 'extra')

class GroupAdmin(admin.ModelAdmin):
    list_display = ('name', 'code')
    search_fields = ['name']

admin.site.register(Menu, MenuAdmin)
admin.site.register(Category, CategoryAdmin)


import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
INF = 10**30

def keta(n):
    return len(str(n))

def make(n, l, r):
    ans = 0
    sn = str(n)
    for i in range(5):
        if keta(n) > 2+i:
            ans += 10 ** i
        elif keta(n) == 2 + i:
            if sn[0] > str(l):
                ans += 10 ** i
            elif sn[0] == str(l):
                if sn[-1] < r:
                    ans += int(sn[1:-1])
                else:
                    if sn[1:-1] == '':
                        ans += 1
                    else:
                        ans += int(sn[1:-1]) + 1
    if l == r:
        ans += 1
    return ans

def main():
    n = int(input().strip())
    ans = 0
    for i in range(1, n+1):
        p = make(n, str(i)[-1], str(i)[0])
        if i % 10 == 0:
            continue
        # if i < 10:
        #     p += 1
        # print("i ,p: ", i, p)
        ans += p
    print(ans)
if __name__ == '__main__':
    main()


import time
from mingus.midi import fluidsynth
from mingus.containers import Note

# the soundfont arugment is required and does not have a default
# the second argument "driver" is the driver and could be "pulseaudio" but is autodetected properly.
fluidsynth.init(sf2="/usr/share/sounds/sf2/FluidR3_GM.sf2")
# C is actually C-4
scale = ["C", "D", "E", "F", "G", "A", "B", "C-5"]
for note in scale:
    n = Note(note, 4)
    fluidsynth.play_Note(n)
    time.sleep(1)
    # you have to stop the note otherwise it will stay sustained
    fluidsynth.stop_Note(n)