#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jul  6 15:39:18 2020

@author: mike_ubuntu
"""

import numpy as np

import epde.globals as global_var
from epde.factor import Factor
from typing import Union

def constancy_hard_equality(tensor, epsilon = 1e-7):
    print(np.abs(np.max(tensor) - np.min(tensor)), epsilon, type(np.abs(np.max(tensor) - np.min(tensor))),  type(epsilon))
    return np.abs(np.max(tensor) - np.min(tensor)) < epsilon

class Evaluator(object):
    """
    Class for evaluator of token (factor of the term in the sought equation) values with arbitrary function

    Attributes
    ----------

    _evaluator : function
        a function, which returns the vector of token values, evaluated on the studied area;
    params : dict
        dictionary, containing parameters of the evaluator (like grid, on which the function is evaluated or matrices of pre-calculated function)

    Methods
    ----------

    set_params(**params)
        set the parameters of the evaluator, using keyword arguments

    apply(token, token_params)
        apply the defined evaluator to evaluate the token with specific parameters
    """
    def __init__(self, eval_function, eval_kwargs_keys):
        self._evaluator = eval_function
        self.eval_kwargs_keys = eval_kwargs_keys

    def apply(self, token, structural = False, **kwargs):
        """
        Apply the defined evaluator to evaluate the token with specific parameters.

        Parameters:
        ----------
        token_label : string
            symbolic label of the specific token, e.g. 'cos';

        token_params : dict
            dictionary with keys, naming the token parameters (such as frequency, axis and power for trigonometric function)
            and values - specific values of corresponding parameters.

        Raises:
        ----------
        TypeError
            If the evaluator could not be applied to the token.

        """
        assert list(kwargs.keys()) == self.eval_kwargs_keys
        return self._evaluator(token, structural, **kwargs)

class Token_family(object):
    """
    Class for the type (family) of tokens, from which the tokens are taken as factors in the terms of the equation

    Attributes:
    -----------
    type : string
        the symbolic name of the token family (e.g. 'logarithmic', 'trigonometric', etc.)

    status : dict
        dictionary, containing markers, describing the token properties. Key - property, value - bool variable:

        'mandatory' - if True, a token from the family must be present in every term;

        'unique_token_type' - if True, only one token of the family can be present in the term;

        'unique_for_right_part' - if True, the tokens, present in the "right part" of the equation, can not be present in the terms of the "left part".
        Recommended to select "True", if any token of the familiy can have 0 values on the majority of studied area;

        'unique_specific_token' - if True, a specific token can be present only once per term;

    _evaluator : Evaluator object
        Evaluator, which is used to get values of the tokens from that family;

    tokens : list of strings
        List of function names, describing all of the functions, belonging to the family. E.g. for 'trigonometric' token type, this list will be ['sin', 'cos']

    token_params : OrderedDict
        Available range for token parameters. Ordered dictionary with key - token parameter name, and value - tuple with 2 elements:
        (lower boundary, higher boundary), while type of boundaries describes the avalable token params:
        if int - the parameters will be integer, if float - float.

    Methods:
    -----------
    set_status(mandatory = False, unique_specific_token = False, unique_token_type = False, unique_for_right_part = False)
        Method to set the markers of the token status;

    set_params(tokens, token_params)
        Method to set the list of tokens, present in the family, and their parameters;

    set_evaluator(eval_function, **eval_params)
        Method to set the evaluator for the token family & its parameters;

    test_evaluator()
        Method to test, if the evaluator and tokens are set properly

    evaluate(token, token_params)
        Method, which uses the specific token evaluator to evaluate the passed token with its parameters

    """
    def __init__(self, token_type : str, family_of_derivs : bool = False):

        """
        Initialize the token family;

        Parameters:
        -----------
        token_type : string
            The name of the token family; must be unique among other families.
        """

        self.type = token_type
        self.family_of_derivs = family_of_derivs
        self.evaluator_set = False; self.params_set = False; self.cache_set = False

    def set_status(self, meaningful = False, s_and_d_merged = True,
                   unique_specific_token = False, unique_token_type = False,
                   unique_for_right_part = False, requires_grid = False):
        """
        Set the status of the elements of the token family;

        Parameters:
        -----------
        mandatory : Bool
            if True, a token from the family must be present in every term;

        unique_token_type : Bool
            if True, only one token of the family can be present in the term;

        unique_for_right_part : Bool
            if True, the tokens, present in the "right part" of the equation, can not be present in the terms of the "left part".
            Recommended to select "True", if any token of the familiy can have 0 values on the majority of studied area;

        unique_specific_token : Bool
            if True, a specific token can be present only once per term;
        """
        self.status = {}
        self.status['meaningful'] = meaningful
        self.status['structural_and_defalut_merged'] = s_and_d_merged
        self.status['unique_specific_token'] = unique_specific_token
        self.status['unique_token_type'] = unique_token_type
        self.status['unique_for_right_part'] = unique_for_right_part
        self.status['requires_grid'] = requires_grid

    def set_params(self, tokens, token_params, equality_ranges, derivs_solver_orders = None):
        """
        Define the token family with list of tokens and their parameters

        Parameters:
        -----------
        tokens : list of strings
            List of function names, describing all of the functions, belonging to the family. E.g. for 'trigonometric' token type,
            this list will be ['sin', 'cos']

        token_params : OrderedDict
            Available range for token parameters. Ordered dictionary with key - token parameter name, and value - tuple with 2 elements:
            (lower boundary, higher boundary), while type of boundaries describes the avalable token params:
            if int - the parameters will be integer, if float - float.

        Example:
        ----------
        >>> token_names_trig = ['sin', 'cos']
        >>> trig_token_params = OrderedDict([('power', (1, 1)), ('freq', (0.9, 1.1)), ('dim', (0, u_initial.ndim))])
        >>> trigonometric_tokens.set_params(token_names_trig, trig_token_params)

        """
        assert bool(derivs_solver_orders is not None) == bool(self.family_of_derivs), 'Solver form must be set for derivatives, and only for them.'

        self.tokens = tokens; self.token_params = token_params
        if self.family_of_derivs:
            self.derivs_ords = {token : derivs_solver_orders[idx] for idx, token in enumerate(tokens)}
        self.params_set = True
        self.equality_ranges = equality_ranges

        if self.evaluator_set:
            self.test_evaluator()

    def set_evaluator(self, eval_function, eval_kwargs_keys = [], suppress_eval_test = True):#, ): , **eval_params   #Test, if the evaluator works properly
        """
        Define the evaluator for the token family and its parameters

        Parameters:
        ------------
        eval_function : function or Evaluator object
            Function, used in the evaluator, or the evaluator

        **eval_params : keyword arguments
            The parameters for evaluator; must contain params_names (names of the token parameters) &
            param_equality (for each of the token parameters, range in which it considered as the same),

        Example:
        -----------
        >>> def trigonometric_evaluator(token, token_params, eval_params):
        >>>
        >>>     '''
        >>>
        >>>     Example of the evaluator of token values, appropriate for case of trigonometric functions to be calculated on grid, with results in forms of tensors
        >>>
        >>>     Parameters
        >>>     ----------
        >>>     token: {'sin', 'cos'}
        >>>         symbolic form of the function to be evaluated:
        >>>     token_params: dictionary: key - symbolic form of the parameter, value - parameter value
        >>>         names and values of the parameters for trigonometric functions: amplitude, frequency & dimension
        >>>     eval_params : dict
        >>>         Dictionary, containing parameters of the evaluator: in this example, it contains coordinates np.meshgrid with coordinates for points,
        >>>         names of the token parameters (frequency, axis and power). Additionally, the names of the token parameters must be included with specific key 'params_names',
        >>>         and parameters range, for which each of the tokens if consedered as "equal" to another, like sin(1.0001 x) can be assumed as equal to (0.9999 x)
        >>>
        >>>     Returns
        >>>     ----------
        >>>     value : numpy.ndarray
        >>>         Vector of the evaluation of the token values, that shall be used as target, or feature during the LASSO regression.
        >>>
        >>>     '''
        >>>
        >>>     assert 'grid' in eval_params
        >>>     trig_functions = {'sin' : np.sin, 'cos' : np.cos}
        >>>     function = trig_functions[token]
        >>>     grid_function = np.vectorize(lambda *args: function(token_params['freq']*args[token_params['dim']])**token_params['power'])
        >>>     value = grid_function(*eval_params['grid'])
        >>>     return value
        >>>
        >>> der_eval_params = {'token_matrices':simple_functions, 'params_names':['power'], 'params_equality':{'power' : 0}}
        >>> trig_eval_params = {'grid':grid, 'params_names':['power',  'freq', 'dim'], 'params_equality':{'power': 0, 'freq':0.05, 'dim':0}}
        >>> trigonometric_tokens.set_evaluator(trigonometric_evaluator, **trig_eval_params)

        """
        if isinstance(eval_function, Evaluator):
            self._evaluator = eval_function
        else:
            self._evaluator = Evaluator(eval_function, eval_kwargs_keys)
#        self._evaluator.set_params(**eval_params)
        self.evaluator_set = True
        if self.params_set and not suppress_eval_test :
            self.test_evaluator()

    def test_evaluator(self):
        """
        Method to test, if the evaluator and tokens are set properly

        Raises Exception, if the evaluator does not work properly.
        """
#        assert self.cache_set, 'Cache not passed into the token familiy before test of evaluator'
        _, self.test_token = self.create()
#        self.test_token.Set_parameters(random = True)
        # self.test_token.use_cache()
        if self.status['requires_grid']:
            self.test_token.use_grids_cache()
        print(self.test_token.grid_idx, self.test_token.params)
#        self.test_params = {}
#        for key in self.token_params.keys():
#            if self.token_params[key][0] == self.token_params[key][1]:
#                self.test_params[key] = self.token_params[key][0]
#            else:
#                if isinstance(self.token_params[key][0], float):
#                    self.test_params[key] = np.random.uniform(low = self.token_params[key][0], high = self.token_params[key][0])
#                else:
#                    self.test_params[key] = np.random.randint(self.token_params[key][0], self.token_params[key][1])
#        try:
        self.test_token.scaled = False
        self.test_evaluation = self._evaluator.apply(self.test_token)
        print('Test evaluation performed correctly')
#        except:
#            raise Exception('Something went wrong during the test evaluation')

    def chech_constancy(self, test_function, **tfkwargs):
        '''
        Method to check, if any single simple token in the studied domain is constant, or close to it. The constant token is to be displayed and deleted from tokens and cache.
        '''
        raise NotImplementedError('Check constancy method has been depricated due to its limited use.')
        # assert self.params_set
        # constant_tokens_labels = []
        # for label in self.tokens:
        #     print(type(global_var.tensor_cache.memory[label + ' power 1']))
        #     constancy = test_function(global_var.tensor_cache.memory[label + ' power 1'], **tfkwargs)
        #     if constancy:
        #         constant_tokens_labels.append(label)

        # for label in constant_tokens_labels:
        #     print('Function ', label, 'is assumed to be constant in the studied domain. Removed from the equaton search')
        #     self.tokens.remove(label)
        #     global_var.tensor_cache.delete_entry(label + ' power 1')

    def evaluate(self, token):    # Return tensor of values of applied evaluator
        raise NotImplementedError('Method has been moved to the Factor class')
        if self.evaluator_set:# and self.cache_set:
            return self._evaluator.apply(token)
        else:
            raise TypeError('Evaluator function or its parameters not set brfore evaluator application.')

    def create(self, label = None, occupied : list = [], def_term_tokens = [], **factor_params):
        if type(label) == type(None):
#            print('tokens in the term:', def_term_tokens)
            try:
                label = np.random.choice([token for token in self.tokens
                                          if not token in occupied and
                                          not def_term_tokens.count(token) >= self.token_params['power'][1]])
            except ValueError:
                print(f'An error while creating factor of {self.type} token family')
                print('occupied:', occupied, ' all:', self.tokens)
                for token in self.tokens:
                    if not token in occupied: print(f'{token} is free')
                    if def_term_tokens.count(token) >= self.token_params['power'][1]: print(f"max power {self.token_params['power'][1]} not reached")
                raise ValueError("'a' cannot be empty unless no samples are taken")

#        factor_deriv_code = self.derivs_ords[label] if self.family_of_derivs else None
        if self.family_of_derivs:
            factor_deriv_code = self.derivs_ords[label]
        else:
            factor_deriv_code = None

        new_factor = Factor(token_name = label, deriv_code=factor_deriv_code,
                            status = self.status, family_type = self.type)

        if self.status['unique_token_type']:
            occupied_by_factor = self.tokens
        elif self.status['unique_specific_token']:
            occupied_by_factor = [label,]
        else:
            occupied_by_factor = []
        if len(factor_params) == 0:
            new_factor.Set_parameters(params_description = self.token_params,
                                      equality_ranges = self.equality_ranges,
                                      random = True)
        else:
            new_factor.Set_parameters(params_description = self.token_params,
                                      equality_ranges = self.equality_ranges,
                                      random = False,
                                      **factor_params)
        new_factor.Set_evaluator(self._evaluator)
        return occupied_by_factor, new_factor

#    def change_variables(self, prev_operator):
#        assert 'token_matrices' in self.eval_params
#        for key, value in self.set_params['token_matrices'].items():
#            self.set_params['token_matrices'][key] = value - prev_operator # С индексом?

    def cardinality(self, occupied : list = []):
#        print('type', self.type)
        return len([token for token in self.tokens if not token in occupied])

#class Derivatives_like_tokens(Token_family):
#    def __init__(self, token_type, data_tensor, coord_tensors = None,
#                 deriv_tensors = None, smooth_field = True, sigma = 5, max_order = 1,
#                 memory_for_cache = 5):
#        derivs_names = Define_Derivatives(var_name = token_type, dimensionality = data_tensor.ndim,
#                                          max_order = max_order)
#
#        derivs_data_processor = Input_data_entry(data_tensor = data_tensor,
#                                                 coord_tensors = coord_tensors)
#
#        derivs_data_processor.set_derivatives(deriv_tensors, None, None, smooth_field,
#                                              sigma, max_order)
#
#        set_grids = coord_tensors is not None and not all([str(ax) in global_var.grid_cache for ax in range(data_tensor.ndim)])
#        coord_tensors_passed = coord_tensors is not None
#
#        derivs_data_processor.use_global_cache(grids_as_tokens = coord_tensors_passed,
#                                               set_grids = set_grids,
#                                               memory_for_cache = memory_for_cache)
#
#        super().__init__(token_type = token_type)
#        self.set_status(unique_specific_token=False, unique_token_type=False, s_and_d_merged = False,
#                        meaningful = True, unique_for_right_part = False)
#        self.set_params(token_names, OrderedDict([('power', (1, 1))]), {'power' : 0})
#        self.set_evaluator(simple_function_evaluator, [])
#
class TF_Pool(object):
    '''

    '''
    def __init__(self, families):
        self.families = families

    @property
    def pool_tokens(self):
        raise NotImplementedError

    @property
    def families_meaningful(self):
        return [family for family in self.families if family.status['meaningful']]

    @property
    def families_supplementary(self):
        return [family for family in self.families if not family.status['meaningful']]

    def families_cardinality(self, meaningful_only : bool = False, occupied : list = []):
        if meaningful_only:
            return np.array([family.cardinality(occupied) for family in self.families_meaningful])
        else:
            return np.array([family.cardinality(occupied) for family in self.families])

    def create(self, label = None, create_meaningful : bool = False,
                      occupied : list = [], def_term_tokens = [], **kwargs) -> (str, Factor):
#        print('in create method are occupied: ', occupied)
        if label is None:
            if create_meaningful:
    #            print('a', self.families, 'p', self.families_cardinality(True, occupied))
                if np.sum(self.families_cardinality(True, occupied)) == 0:
                    print('occupied', occupied)
                    raise ValueError('Tring to create a term from an empty pool')
                return np.random.choice(a = self.families_meaningful,
                                        p = self.families_cardinality(True, occupied) / np.sum(self.families_cardinality(True, occupied))).create(label = None,
                                                                     occupied = occupied,
                                                                     def_term_tokens = def_term_tokens,
                                                                     **kwargs)
            else:
                return np.random.choice(a = self.families,
                                        p = self.families_cardinality(False, occupied)  / np.sum(self.families_cardinality(False, occupied))).create(label = None,
                                                                     occupied = occupied,
                                                                     def_term_tokens = def_term_tokens,
                                                                     **kwargs)
        else:
            token_families = [family for family in self.families if label in family.tokens]
            if len(token_families) > 1:
                print([family.tokens for family in token_families])
                raise Exception('More than one family contains token with desired label.')
            elif len(token_families) == 0:
                raise Exception('Desired label does not match tokens in any family.')
            else:
                return token_families[0].create(label = None, occupied = occupied,
                                                def_term_tokens = def_term_tokens,
                                                **kwargs)

    def __add__(self, other):
        return TF_Pool(families = self.families + other.families)


__author__ = 'mdu'
from datetime import *
try:
    from lxml import etree
except ImportError:
    print("lxml import error")
    raise

def getDateTime(dateTimeStr):
    year=int(dateTimeStr[0:4])
    month=int(dateTimeStr[4:6])
    day=int(dateTimeStr[6:8])
    hour=int(dateTimeStr[8:10])
    minute=int(dateTimeStr[10:12])
    second=int(dateTimeStr[12:14])
    return datetime(year, month, day, hour, minute, second)
def getDateStr(dateTimeStr):
    year=dateTimeStr[0:4]
    month=dateTimeStr[4:6]
    day=dateTimeStr[6:8]
    return day+"."+month+"."+year
def getTimeStr(dateTimeStr):
    hour=dateTimeStr[8:10]
    minute=dateTimeStr[10:12]
    second=dateTimeStr[12:14]
    return hour+":"+minute+":"+second
def getDateTimeStr(dateTimeStr):
    return getDateStr(dateTimeStr)+" "+getTimeStr(dateTimeStr)
def getTimeDelta(startDateTime,stopDateTime):
    delta=stopDateTime - startDateTime
    return int(delta.total_seconds())
def norm(inputStr):
    return inputStr.replace("\t"," ").replace("\n"," ").strip()

with open("xmltv_channels.csv",encoding="utf8", mode="w") as f:
    f.write("%s\t%s\t%s\n" %("channel_id","display_name","icon"))
    for action, element in etree.iterparse("xmltv.xml"):
        if element.tag=="channel":
            channel_id=norm(element.attrib["id"])
            display_name=""
            icon=""
            for childElement in list(element):
                if childElement.tag=="display-name":
                    display_name=norm(childElement.text)
                if childElement.tag=="icon":
                    icon=childElement.attrib["src"]
            f.write("%s\t%s\t%s\n" %(channel_id,display_name,icon))

with open("xmltv_schedule.csv",encoding="utf8", mode="w") as f:
    f.write("%s\t%s\t%s\t%s\t%s\t%s\t%s\n" %("channel_id","start","stop","delta","title","desc","category"))
    for action, element in etree.iterparse("xmltv.xml"):
        if element.tag=="programme":
            start=""
            stop=""
            channel=""
            startStr=norm(str.split(element.attrib["start"])[0])
            stopStr=norm(str.split(element.attrib["stop"])[0])
            start=getDateTimeStr(startStr)
            stop=getDateTimeStr(stopStr)
            delta=getTimeDelta(getDateTime(startStr),getDateTime(stopStr))
            channel_id=norm(str.split(element.attrib["channel"])[0])
            #print("%s, %s" % (start,stop))
            #print("%s, %s" % (str.split(element.attrib["start"])[0],str.split(element.attrib["stop"])[0]))
            title=""
            desc=""
            category=""
            for childElement in list(element):
                if childElement.tag=="title":
                    title=norm(childElement.text)
                if childElement.tag=="desc":
                    desc=norm(childElement.text)
                if childElement.tag=="category":
                    category=norm(childElement.text)
            f.write("%s\t%s\t%s\t%s\t%s\t%s\t%s\n" %(channel_id,start,stop,delta,title,desc,category))


import json

import luigi

from servicecatalog_puppet import aws, constants
from servicecatalog_puppet.workflow.launch import do_terminate_product_task

class TerminateProductDryRunTask(do_terminate_product_task.DoTerminateProductTask):
    launch_name = luigi.Parameter()

    region = luigi.Parameter()
    account_id = luigi.Parameter()

    portfolio = luigi.Parameter()
    product = luigi.Parameter()
    version = luigi.Parameter()

    ssm_param_inputs = luigi.ListParameter(default=[], significant=False)

    launch_parameters = luigi.DictParameter(default={}, significant=False)
    manifest_parameters = luigi.DictParameter(default={}, significant=False)
    account_parameters = luigi.DictParameter(default={}, significant=False)

    retry_count = luigi.IntParameter(default=1, significant=False)

    worker_timeout = luigi.IntParameter(default=0, significant=False)
    ssm_param_outputs = luigi.ListParameter(default=[], significant=False)
    requested_priority = luigi.IntParameter(significant=False, default=0)

    execution = luigi.Parameter()

    try_count = 1

    def write_result(self, current_version, new_version, effect, notes=""):
        with self.output().open("w") as f:
            f.write(
                json.dumps(
                    {
                        "current_version": current_version,
                        "new_version": new_version,
                        "effect": effect,
                        "notes": notes,
                        "params": self.param_kwargs,
                    },
                    indent=4,
                    default=str,
                )
            )

    def run(self):
        details = self.load_from_input("details")
        product_id = details.get("product_details").get("ProductId")

        self.info(
            f"starting dry run terminate try {self.try_count} of {self.retry_count}"
        )

        with self.spoke_regional_client("servicecatalog") as service_catalog:
            self.info(
                f"[{self.launch_name}] {self.account_id}:{self.region} :: looking for previous failures"
            )
            r = aws.get_provisioned_product_details(
                product_id, self.launch_name, service_catalog
            )

            if r is None:
                self.write_result(
                    "-", "-", constants.NO_CHANGE, notes="There is nothing to terminate"
                )
            else:
                provisioned_product_name = (
                    service_catalog.describe_provisioning_artifact(
                        ProvisioningArtifactId=r.get("ProvisioningArtifactId"),
                        ProductId=product_id,
                    )
                    .get("ProvisioningArtifactDetail")
                    .get("Name")
                )

                if r.get("Status") != "TERMINATED":
                    self.write_result(
                        provisioned_product_name,
                        "-",
                        constants.CHANGE,
                        notes="The product would be terminated",
                    )
                else:
                    self.write_result(
                        "-",
                        "-",
                        constants.CHANGE,
                        notes="The product is already terminated",
                    )


"""This file defines a K-Nearest Neighbors Classifier."""
import numpy as np
from sortedcontainers import SortedList

class KNN(object):
    def __init__(self, k: int):
        self.k = k

    def fit(self, X: np.ndarray, y: np.ndarray) -> None:
        self.X = X
        self.y = y

    def predict(self, X: np.ndarray) -> np.ndarray:
        y = np.zeros(len(X))
        for i, x in enumerate(X):
            sl = SortedList()
            for j, xt in enumerate(self.X):
                dist = np.linalg.norm(x - xt)
                if len(sl) < self.k:
                    sl.add((dist, self.y[j]))
                else:
                    if dist < sl[-1][0]:
                        del sl[-1]
                        sl.add((dist, self.y[j]))

            votes = {}
            for _, v in sl:
                votes[v] = votes.get(v, 0) + 1

            max_votes = 0
            max_votes_class = -1
            for v, count in votes.items():
                if count > max_votes:
                    max_votes = count
                    max_votes_class = v

            y[i] = max_votes_class

        return y

    def score(self, X: np.ndarray, y: np.ndarray) -> float:
        p = self.predict(X)
        return np.mean(p == y)

if __name__ == "__main__":
    pass


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

from django import forms
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
from index.models import CartridgeItemName
from docs.models  import SCDoc
from common.helpers import del_leding_zero
from index.forms.base_add_form import BaseAddForm

class AddItems(BaseAddForm):
    manualNumber = forms.CharField(max_length = 256, required=False)
    cartName = forms.ModelChoiceField(queryset=CartridgeItemName.objects.all(),
                                      error_messages={'required': _('Required field.')},
                                      empty_label=' ',
                                      required=True,
                                      )

    doc = forms.ModelChoiceField(queryset=SCDoc.objects.filter(), required=False)

    cartCount = forms.CharField(max_length = 4,
                                widget=forms.TextInput(attrs={'class': 'pm_counter', 'readonly': 'readonly'}),
                                error_messages={'required': _('Required field.')},
                                required=True
                                )

    required_css_class = 'required'

    def clean_cartName(self):
        """Проверят на пустоту введенные данные.
        """
        if not self.cleaned_data.get('cartName', ''):
            raise ValidationError(_('Required field.'))
        return self.cleaned_data.get('cartName', '')

    def clean_cartCount(self):
        """
        """
        temp_count = self.cleaned_data.get('cartCount', '')
        try:
            temp_count = int(temp_count)
        except ValueError:
            raise ValidationError(_('You have entered the wrong data.'))

        if temp_count <= 0:
            raise ValidationError(_('The value must be greater than zero.'))

        if not temp_count:
            raise ValidationError(_('Required field.'))

        return self.cleaned_data.get('cartCount', '')

    def clean_doc(self):
        """
        """
        if not self.cleaned_data.get('doc', 0):
            return 0

        doc_id = self.cleaned_data.get('doc')
        return doc_id.pk


import tensorflow as tf
import numpy as np
import dlib
from tensorflow.python.platform import gfile
# from contextlib import contextmanager
import argparse
import time
from pathlib import Path
import threading
# OpenCV
import cv2
import queue

def get_args():
    parser = argparse.ArgumentParser(description="This script detects faces from web cam input, "
                                                 "and estimates age and gender for the detected faces.",
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("--weight_file", type=str, default=None,
                        help="path to weight file (e.g. weights.28-3.73.hdf5)")
    parser.add_argument("--depth", type=int, default=16,
                        help="depth of network")
    parser.add_argument("--width", type=int, default=8,
                        help="width of network")
    parser.add_argument("--margin", type=float, default=0.6,
                        help="margin around detected face for age-gender estimation")
    parser.add_argument("--image_dir", type=str, default=None,
                        help="target image directory; if set, images in image_dir are used instead of webcam")

    args = parser.parse_args()
    return args

def draw_label(image, point, label, font=cv2.FONT_HERSHEY_SIMPLEX,
               font_scale=0.8, thickness=1):
    size = cv2.getTextSize(label, font, font_scale, thickness)[0]
    x, y = point
    cv2.rectangle(image, (x, y - size[1]), (x + size[0], y), (255, 0, 0), cv2.FILLED)
    cv2.putText(image, label, point, font, font_scale, (255, 255, 255), thickness, lineType=cv2.LINE_AA)

# @contextmanager
def video_capture(*args, **kwargs):
    cap = cv2.VideoCapture(*args, **kwargs)
    try:
        yield cap
    finally:
        cap.release()

def yield_images():
    cap = cv2.VideoCapture(0)
    # cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1080)
    # cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

    while cap.isOpened():
        # get video frame
        ret, img = cap.read()

        if not ret:
            raise RuntimeError("Failed to capture image")

        yield img

def yield_images_from_dir(image_dir):
    image_dir = Path(image_dir)

    for image_path in image_dir.glob("*.*"):
        img = cv2.imread(str(image_path), 1)
        print("img path",str(image_path))
        #cv2.imshow("result", img)
        #cv2.waitKey(0)

        if img is not None:
            h, w, _ = img.shape
            r = 640 / max(w, h)
            yield cv2.resize(img, (int(w * r), int(h * r)))

def tracker_new():

    tracker=cv2.TrackerKCF_create()
    return tracker

def main():
    args = get_args()
    depth = args.depth
    k = args.width
    weight_file = args.weight_file
    margin = args.margin
    image_dir = args.image_dir

    #videoCapture = cv2.VideoCapture('oto.avi')

    #fourcc = cv2.VideoWriter_fourcc(*'XVID')
    #videoWriter = cv2.VideoWriter('out.mp4',fourcc, 10, (640,480))
    que = queue.Queue()
    detector = dlib.get_frontal_face_detector()
    #image_generator=yield_images()
    rd_img =threading.Thread(target=lambda q,arg1:q.put(yield_images_from_dir(image_dir) if image_dir else yield_images()),args=(que,'img_d'))
    #image_generator=yield_images_from_dir(image_dir) if image_dir else yield_images()
    rd_img.start()
    image_generator=que.get()
    # tracker=tracker_new()

    detect_e=tf.Graph()
    with detect_e.as_default():
        e_graph_def=tf.GraphDef()
        with tf.gfile.GFile(weight_file,'rb') as f:
            serialized_graph = f.read()
            e_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(e_graph_def,name='')

    e_seesion=tf.Session(graph=detect_e)
    img_size = 224
    pred_emo= detect_e.get_tensor_by_name("pred_emo/Softmax:0")

    count=0
    dlib_thread=threading.Thread()
    for img in image_generator:
            input_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            img_h, img_w, _ = np.shape(input_img)

            detected = detector(input_img, 1)
            #print (detected)
            faces = np.empty((len(detected), img_size, img_size, 3))
            if len(detected) > 0:
                for i, d in enumerate(detected):
                    x1, y1, x2, y2, w, h = d.left(), d.top(), d.right() + 1, d.bottom() + 1, d.width(), d.height()
                    xw1 = max(int(x1 - margin * w), 0)
                    yw1 = max(int(y1 - margin * h), 0)
                    xw2 = min(int(x2 + margin * w), img_w - 1)
                    yw2 = min(int(y2 + margin * h), img_h - 1)
                    cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
                    faces[i, :, :, :] = cv2.resize(img[yw1:yw2 + 1, xw1:xw2 + 1, :], (img_size, img_size))
                    # cv2.rectangle(img, (xw1, yw1), (xw2, yw2), (255, 0, 0), 2)

                print("len(detect)",len(detected))
                start = time.time()
                res_emo = e_seesion.run(pred_emo, feed_dict={'input_1:0': faces})

                #print(res_age)
                #res_gen = sess.run(pred_gen, feed_dict={'input_1:0': faces})

                end = time.time()

                for i, d in enumerate(detected):
                    if max(res_emo[i][0],res_emo[i][1],res_emo[i][2])==res_emo[i][0]:
                        label ="happy"
                    elif max(res_emo[i][0],res_emo[i][1],res_emo[i][2])==res_emo[i][1]:
                        label = "nertual"
                    else :
                        label="others"
                    #label = "{}".format("happy"if max(res_emo[i][0],res_emo[i][1],res_emo[i][2])==res_emo[i][0] else "others")
                    #draw_label(img, (d.left(), d.top()), label)
                    print("emo label",label)
                    #draw_label(img, (d.left(), d.top()), label)

                    draw_label(img, (d.left(), d.top()), label)
                cv2.imshow("result", img)
            #key = cv2.waitKey(30)
                if cv2.waitKey(0) & 0xFF == ord('q'):
                    break

if __name__ == '__main__':
    main()


from datetime import datetime, timedelta
from icalendar import Event, Calendar
from .scraper import get_free_games

ONE_WEEK = timedelta(weeks=1)

def custom_attributes_to_dict(custom_attributes: list) -> dict:
	return dict(
		map(
			lambda x: (x["key"], x["value"]),
			custom_attributes
		)
	)

def create_event(game: dict):
	event = Event()

	custom_attributes = custom_attributes_to_dict(game["customAttributes"])
	if "com.epicgames.app.productSlug" in custom_attributes:
		url_slug = custom_attributes["com.epicgames.app.productSlug"]
	else:
		url_slug = game['urlSlug']

	uid = url_slug

	promotions = game["promotions"]
	offers = promotions["upcomingPromotionalOffers"] or promotions["promotionalOffers"]
	offer = offers[0]["promotionalOffers"][0]
	start_at = datetime.fromisoformat(offer["startDate"][:-1])
	end_at = datetime.fromisoformat(offer["endDate"][:-1])

	event.add('dtstart', start_at)
	event.add('dtend', end_at)
	event.add(
		'location',
		f"https://www.epicgames.com/store/en-US/p/{url_slug}"
	)
	event.add('summary', game["title"])
	event.add('description', game["description"])
	event.add('uid', uid)

	return event

def get_calendar():
	games = get_free_games()

	cal = Calendar()
	cal.add('prodid', '-//KAWCCO (@supersonichub1)/EN')
	cal.add('version', '2.0')
	cal.add('name', f"Free Epic Games")
	cal.add("method", "PUBLISH")

	events = map(create_event, filter(lambda x: x["promotions"], games))
	for event in events:
		cal.add_component(event)

	return cal


#create a list of alphabets from a-z
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

#define a function that will take three parameters as input(original_text, shift_amount_direction to encode or decode)
def caesar(cipher_direction,start_text, shift_amount):
    end_text = ''  #assign a empty string
    if cipher_direction == 'decode':   #to supress the code as make it less space consuming doing this
        shift_amount *= -1   #when we want to decode the word than we will have to go that much step back.

    for char in start_text:
        #if that character is available in alphabet list:
        if char in alphabet:
            #take out its position from alphabet list
            position = alphabet.index(char)
            #now new position will be current position added top shift amount
            new_position = position + shift_amount
            #add those characters to end_text variable which are present at new position index of alphabet
            end_text += alphabet[new_position]
        else:   #if that character is not in alphabet list[]
            end_text += char  #add those char/symbol/space as it is to end_text variable:
    #print the encoded or decoded word
    print(f"Your {cipher_direction}d word is: '{end_text}'")

#from art module import logo
from art import logo
print(logo)

#assign a variable play_again to True(bool)
play_again = True
#till the condition of play_again will be true, it will keep going through while loop:
while play_again:
    #taking user's input
    direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
    text = input("Type your message:\n").lower()
    shift = int(input("Type the shift number:\n"))

    shift = shift % 26 #if someone gives shift more than the index available at alphabet list

    #after taking the inputs call the function caesar
    caesar(cipher_direction=direction, start_text=text, shift_amount=shift)

    #ask again to user if they want to go again:
    result = input("Type 'yes' if you want to go again. Otherwise type 'no'.\n")

    if result == 'no':  #if the result is no
        play_again = False   #change the play_again variable value to False from True, which will end the while loop as the while condition will become false
        print("Good Bye!")


from django.contrib import messages
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
# For the warnings ^
from django.conf import settings
from django.shortcuts import render, get_object_or_404, redirect
from django.views.generic import ListView, DetailView, View
from django.utils import timezone
from .forms import CheckoutForm
from .models import Item, OrderItem, Order, BillingAddress, Payment

# Create your views here.

import stripe
stripe.api_key = settings.STRIPE_SECRET_KEY

""" def HomeView(request):
    context = {
        'items': Item.objects.all()
    }
    return render(request, "home-page.html", context) """

""" def ProductView(request):
    context = {
        'items': Item.objects.all()
    }
    return render(request, "product-page.html", context) """

def AboutView(request):
    return render(request, "about-page.html", {})

# either primary key or slug
class ProductView(DetailView):
    model = Item
    template_name = "product-page.html"

class HomeView(ListView):
    model = Item
    # How many items per page
    paginate_by = 10
    template_name = "home-page.html"

# View as we do not want to inherit a slug or pk
class OrderSummary(LoginRequiredMixin, View):
    def get(self, *args, **kwards):
        try:
            order = Order.objects.get(user=self.request.user, ordered=False)
            context = {
                'object': order
            }
            return render(self.request, 'order_summary.html', context)
        except ObjectDoesNotExist:
            messages.error(
                self.request, "You do not have any items in the cart")
            return redirect("/")

# def CheckoutView(request):
#     return render(request, "checkout-page.html", {})

class CheckoutView(View):
    def get(self, *args, **kwargs):
        # The form
        order = Order.objects.get(user=self.request.user, ordered=False)
        form = CheckoutForm()
        context = {
            'order': order,
            'form': form,
        }
        return render(self.request, "checkout-page.html", context)

    def post(self, *args, **kwargs):
        form = CheckoutForm(self.request.POST or None)
        try:
            order = Order.objects.get(user=self.request.user, ordered=False)
            if form.is_valid():
                # print(form.cleaned_data)
                street_address = form.cleaned_data.get('street_address')
                apartment_address = form.cleaned_data.get('apartment_address')
                country = form.cleaned_data.get('country')
                zip_code = form.cleaned_data.get('zip_code')
                # TODO
                # same_shipping_address = form.cleaned_data.get('same_shipping_address')
                # save_info = form.cleaned_data.get('save_info')
                payment_option = form.cleaned_data.get('payment_option')
                billing_address = BillingAddress(
                    user=self.request.user,
                    street_address=street_address,
                    apartment_address=apartment_address,
                    country=country,
                    zip_code=zip_code,
                )
                billing_address.save()
                order.billing_address = billing_address
                order.save()
                # TODO: redirect payment

                if payment_option == 'S':
                    return redirect('core:payment', payment_option='stripe')
                elif payment_option == 'P':
                    return redirect('core:payment', payment_option='paypal')
                else:
                    messages.warning(self.request, "Invalid payment option")
                    return redirect('core:checkout')

                return redirect('core:checkout')
            messages.warning(self.request, "Failed checkout")
            return redirect('core:checkout')
        except ObjectDoesNotExist:
            messages.error(
                self.request, "You do not have any items in the cart")
            return redirect("core:order-summary")
        # print(self.request.POST)

class PaymentView(View):
    def get(self, *args, **kwargs):
        # order
        order = Order.objects.get(user=self.request.user, ordered=False)
        context = {
            'order': order,
        }
        return render(self.request, "payment.html", context)

    def post(self, *args, **kwargs):
        # `source` is obtained with Stripe.js; see https://stripe.com/docs/payments/accept-a-payment-charges#web-create-token
        order = Order.objects.get(user=self.request.user, ordered=False)
        token = self.request.POST.get('stripeToken')
        amount = int(order.get_total() * 100)

        try:
            # Use Stripe's library to make requests...
            charge = stripe.Charge.create(
                amount=amount,  # in cents
                currency="usd",
                source=token,
                # description="My First Test Charge (created for API docs)",
            )

            order.ordered = True
            # create payment
            payment = Payment()
            payment.stripe_charge_id = charge['id']
            payment.user = self.request.user
            payment.amount = order.get_total()
            payment.save()

            # assign payment to order

            order.ordered = True
            order.payment = payment
            order.save()
            # error handling
            messages.success(self.request, "Your order was successful!")
            return redirect("/")

        except stripe.error.CardError as e:
            # Since it's a decline, stripe.error.CardError will be caught
            body = e.json_body
            err = body.get('error', {})
            messages.error(self.request, f"{err.get('message')}")
            return redirect("/")
        except stripe.error.RateLimitError as e:
            # Too many requests made to the API too quickly
            messages.error(self.request, "Rate limit error")
            return redirect("/")
        except stripe.error.InvalidRequestError as e:
            # Invalid parameters were supplied to Stripe's API
            messages.error(self.request, "Invalid parameters")
            return redirect("/")
        except stripe.error.AuthenticationError as e:
            # Authentication with Stripe's API failed
            # (maybe you changed API keys recently)
            messages.error(self.request, "Not authenticated")
            return redirect("/")
        except stripe.error.APIConnectionError as e:
            # Network communication with Stripe failed
            messages.error(self.request, "Network error")
            return redirect("/")
        except stripe.error.StripeError as e:
            # Display a very generic error to the user, and maybe send
            # yourself an email
            messages.error(self.request, "Something went wrong. You were not charged. Please try again")
            return redirect("/")
        except Exception as e:
            # Something else happened, completely unrelated to Stripe
            messages.error(self.request, "A serious error has occurred")
            return redirect("/")
            # Usually Code error

@login_required
def add_to_cart(request, slug):
    item = get_object_or_404(Item, slug=slug)
    # avoid recreating the item to declutter log
    order_item, created = OrderItem.objects.get_or_create(
        item=item, user=request.user, ordered=False)
    # Only take the uncompleted order
    order_qs = Order.objects.filter(user=request.user, ordered=False)
    if order_qs.exists():
        order = order_qs[0]
        # see if the order item is in the order
        if order.items.filter(item__slug=item.slug).exists():
            order_item.quantity += 1
            order_item.save()
            messages.info(request, "The item quantity has been updated.")
            return redirect("core:order-summary")
        else:
            # Adding different tokens
            messages.info(request, "This item was added to your cart.")
            order.items.add(order_item)
            return redirect("core:order-summary")
    else:
        # New Order
        ordered_date = timezone.now()
        order = Order.objects.create(
            user=request.user, ordered_date=ordered_date)
        order.items.add(order_item)
        messages.info(request, "This item was added to your cart.")
        return redirect("core:order-summary")

@login_required
def remove_single_from_cart(request, slug):
    item = get_object_or_404(Item, slug=slug)
    order_qs = Order.objects.filter(
        user=request.user, ordered=False)
    if order_qs.exists():
        order = order_qs[0]
        # see if the order item is in the order
        if order.items.filter(item__slug=item.slug).exists():
            order_item = OrderItem.objects.filter(
                item=item, user=request.user, ordered=False)[0]
            if order_item.quantity > 1:
                order_item.quantity -= 1
            else:
                order.items.remove(order_item)
                order_item.delete()
            order_item.save()

            messages.info(request, "The item quantity has been updated.")
            return redirect("core:order-summary")
        else:
            # User does not contain does item
            messages.info(request, "This item is not in your cart.")
            return redirect("core:Product", slug=slug)
    else:
        # User has no orders
        messages.info(request, "There are no items in your cart.")
        return redirect("core:Product", slug=slug)

@login_required
def remove_from_cart(request, slug):
    item = get_object_or_404(Item, slug=slug)
    order_qs = Order.objects.filter(
        user=request.user, ordered=False)
    if order_qs.exists():
        order = order_qs[0]
        # see if the order item is in the order
        if order.items.filter(item__slug=item.slug).exists():
            order_item = OrderItem.objects.filter(
                item=item, user=request.user, ordered=False)[0]
            order.items.remove(order_item)
            # Remove the object - or else it will think it has 1
            order_item.delete()
            messages.info(request, "This item was removed from your cart.")
            return redirect("core:order-summary")
        else:
            # User does not contain does item
            messages.info(request, "This item is not in your cart.")
            return redirect("core:Product", slug=slug)
    else:
        # User has no orders
        messages.info(request, "There are no items in your cart.")
        return redirect("core:Product", slug=slug)

""" from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Item, OrderItem, Order

# Create your views here.

class HomeView(ListView):
    model = Item
    paginate_by = 10
    template_name = "home-page.html"

def products(request):
    context = {
        'items': Item.objects.all()
    }
    return render(request, "product-page.html", context)

class ItemView(DetailView):
    model = Item
    template_name = "product-page.html"
"""


import typing as t
from dataclasses import dataclass, field

@dataclass
class Report:
    row_count: int = 0
    forward_primers_trimmed: t.List[int] = field(
        default_factory=list, repr=False, hash=False
    )
    reverse_primers_trimmed: t.List[int] = field(
        default_factory=list, repr=False, hash=False
    )
    scanning_summary: t.List[str] = field(
        default_factory=list, repr=False, hash=False, init=True
    )

    @property
    def both_trimmed(self) -> t.List[int]:
        both = list(
            set(self.forward_primers_trimmed) & set(self.reverse_primers_trimmed)
        )
        both.sort()
        return both

    def add_row(
        self,
        row_id: int,
        has_trimmed_forward_primer: bool,
        has_trimmed_reverse_primer: bool,
    ):
        self.row_count += 1
        if has_trimmed_forward_primer:
            self.forward_primers_trimmed.append(row_id)
        if has_trimmed_reverse_primer:
            self.reverse_primers_trimmed.append(row_id)
        return

    def add_scanning_summary(self, scanning_summary: t.List[str]):
        self.scanning_summary = scanning_summary
        return

    def add_null_data_summary(self, null_data_summary: str):
        self.scanning_summary.append(null_data_summary)
        return

    def summary(self) -> str:
        summary = self.scanning_summary.copy()
        total = self.row_count
        self.forward_primers_trimmed.sort()
        self.reverse_primers_trimmed.sort()
        summary.append(
            f"Forward primer trimmed in {len(self.forward_primers_trimmed)} of {total} sequences."
        )
        summary.append(
            f"Reverse primer trimmed in {len(self.reverse_primers_trimmed)} of {total} sequences."
        )
        summary.append(
            f"Forward + reverse primer trimmed in {len(self.both_trimmed)} out of {total} sequences."
        )
        return "\n".join(summary)


import math
#Lista de Exercícios 01  - Questão 17
#Comando: Faça um Programa para uma loja de tintas. O programa deverá pedir o tamanho em metros quadrados da área a ser pintada. Considere que a cobertura da tinta é de 1 litro para cada 6 metros quadrados e que a tinta é vendida em latas de 18 litros, que custam R$ 80,00 ou em galões de 3,6 litros, que custam R$ 25,00.
mq1 = float(input("Insira o tamanho (m²) da área a ser pintada: "))
galoes = (mq1/21.6)
latas = (mq1/108)
if mq1 <= 21.6:
  print("Você precisará de 1 galão de tinta, e pagará R$ 25.")
if (mq1 > 21.6) and (mq1 <= 64.8):
  galoes = round(galoes+0.5)
  pagar = galoes * 25
  print("Você precisará de {} galões de tinta e pagará R$ {}.".format(galoes, pagar))
elif mq1 > 64.8:
  mq2 = mq1 * 1.1
  l = mq2/6
  latas1 = l/18
  latas2 = round(latas1, 0)
  precolata2 = latas2 * 80
  faltatinta = latas1 - latas2
  litro = round(faltatinta, 3) * 18
  galoes1 = litro/3.6
  galoes2 = math.ceil(galoes1)
  precogaloes2 = galoes2 *25
  print("Você precisará de {} lata(s) e {} galão (ões).".format(latas2, galoes2))
  print("Total a pagar = R${}".format(precolata2 + precogaloes2))


import discord
from discord.ext import commands
import wolframalpha

class Calculate(commands.Cog):

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

    # Commands
    @commands.command()
    async def 계산(self, ctx):
        reply = ctx.message.content.split(" ")
        if len(reply) > 1:
            for i in range(2, len(reply)):
                reply[1] = reply[1] + " " + reply[i]
        app_id = "VPR9G7-54PV53JYTK"
        client = wolframalpha.Client(app_id)
        res = client.query(reply[1])
        answer = next(res.results).text
        embed = discord.Embed(title="Result", description=answer, color=0xffffff)
        embed.set_footer(text="Offered by NACL - Shio", icon_url="https://raw.githubusercontent.com/Shio7/EZ-Bot/master/images/Shio.png")
        await ctx.send(embed=embed)

def setup(client):
    client.add_cog(Calculate(client))


def bigger_price_my(limit: int, data: list) -> list:
    bigger_prices = []
    for _ in range(limit):
        max_temp = max(data, key=lambda x: x["price"] )
        data.remove(max_temp)
        bigger_prices.append(max_temp)

    return bigger_prices

bigger_price = lambda limit, data: sorted(data, key=lambda e: e["price"], reverse=True)[:limit]

if __name__ == '__main__':
    from pprint import pprint
    print('Example:')
    pprint(bigger_price(2, [
        {"name": "bread", "price": 100},
        {"name": "wine", "price": 138},
        {"name": "meat", "price": 15},
        {"name": "water", "price": 1}
    ]))

    # These "asserts" using for self-checking and not for auto-testing
    assert bigger_price(2, [
        {"name": "bread", "price": 100},
        {"name": "wine", "price": 138},
        {"name": "meat", "price": 15},
        {"name": "water", "price": 1}
    ]) == [
        {"name": "wine", "price": 138},
        {"name": "bread", "price": 100}
    ], "First"

    assert bigger_price(1, [
        {"name": "pen", "price": 5},
        {"name": "whiteboard", "price": 170}
    ]) == [{"name": "whiteboard", "price": 170}], "Second"

    print('Done! Looks like it is fine. Go and check it')


from __future__ import annotations

from functools import lru_cache

from bs4 import BeautifulSoup

from ..core.session import session
from ..core.website import Website

class CourrierInternational(Website):

    base_url = "https://www.courrierinternational.com/"
    login_url = base_url + "login?destination=<front>"
    alias = ["courrier"]

    article_node = ("div", {"class": "article-text"})

    clean_attributes = ["h3"]
    clean_nodes = ["div", ("span", {"class": "empty-author-name-short"})]

    header_entries = [
        "title",
        "author",
        "date",
        "url",
        "original_url",
        "description",
    ]

    @property
    def login_dict(self):
        credentials = self.credentials
        assert credentials is not None

        c = session.get(self.login_url)
        c.raise_for_status()

        e = BeautifulSoup(c.content, features="lxml")
        attrs = dict(name="form_build_id")
        form_id = e.find("input", attrs=attrs).attrs["value"]

        return {
            "remember_me": "1",
            "form_build_id": form_id,
            "form_id": "user_login_block",
            "op": "Se+connecter",
            "ci_promo_code_code": "",
            "name": credentials["username"],
            "pass": credentials["password"],
        }

    @lru_cache()
    def latest_issue_url(self):

        c = session.get(self.base_url)
        c.raise_for_status()

        e = BeautifulSoup(c.content, features="lxml")
        section = e.find("section", attrs={"class": "hebdo-section"})
        page_url = section.find("a").attrs["href"]

        c = session.get(page_url)
        c.raise_for_status()

        e = BeautifulSoup(c.content, features="lxml")
        magazine = e.find("div", attrs={"class": "magazine-tools"})
        return magazine.find("a", attrs={"data-icon": "file-pdf"}).attrs["href"]

    def author(self, url: str):
        author = super().author(url)
        if author is not None:
            return author

        e = self.bs4(url)

        author_node = e.find("div", {"class", "author-name-short"})
        if author_node is not None:
            return author_node.text

        author_node = e.find("p", {"class": "author-name"})
        if author_node is not None:
            return author_node.text

        return ""

    def original_url(self, url: str) -> str | None:
        e = self.bs4(url)
        div = e.find("div", {"class", "article-url-origin"})
        if div is None:
            return None
        link = div.find("a")
        if link is None:
            return None
        return link.attrs["href"]


# Universal pickling between numpy@CPython and numpypy (numpy@PyPy).
# Written by Sander Roobol. Public domain.

try:
	import cPickle as pickle
except ImportError:
	import pickle

try:
	from cStringIO import StringIO
except ImportError:
	from StringIO import StringIO

import sys
import platform
import inspect

__all__ = ['dump', 'dumps', 'load', 'loads', 'HIGHEST_PROTOCOL', 'PickleError']

dump = pickle.dump
dumps = pickle.dumps
HIGHEST_PROTOCOL = pickle.HIGHEST_PROTOCOL
PickleError = pickle.PickleError

if platform.python_implementation() == 'PyPy':
	# numpy -> numpypy
	def _translate(module, name):
		parts = module.split('.')
		if parts[0] == 'numpy':
			parts[0] = 'numpypy'
		module = '.'.join(parts)
		if module == 'numpypy.core.multiarray':
			module = '_numpypy.multiarray'
		return module, name

else:
	# numpypy -> numpy
	def _translate(module, name):
		parts = module.split('.')
		if parts[0] == 'numpypy' or parts[0] == '_numpypy':
			parts[0] = 'numpy'
		module = '.'.join(parts)
		if module == 'numpy.multiarray':
			module = 'numpy.core.multiarray'
		return module, name

if inspect.isbuiltin(pickle.Unpickler):
	# real cPickle: cannot subclass
	def _find_global(module, name):
		module, name = _translate(module, name)
		__import__(module)
		return getattr(sys.modules[module], name)

	def load(fileobj):
		unpickler = pickle.Unpickler(fileobj)
		unpickler.find_global = _find_global
		return unpickler.load()

else:
	# pure python implementation: ordinary pickle or pypy's cPickle
	class _Unpickler(pickle.Unpickler):
		def find_class(self, module, name):
			module, name = _translate(module, name)
			return pickle.Unpickler.find_class(self, module, name)

	def load(fileobj):
		unpickler = _Unpickler(fileobj)
		return unpickler.load()

def loads(str):
	return load(StringIO(str))


# import necessary libraries
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.metrics import AUC
from scikeras.wrappers import KerasClassifier
from tensorflow.keras.optimizers import Adam
from sklearn.model_selection import GridSearchCV
from backwardselection import importances
import pandas as pd
import numpy as np
import argparse
from accuracy import confusionMat, roc, accuracy
from storePerformance import storeIt
from modelObject import store
import os
import json
import pdb

# creating command line parser for flags to run script from the command line in a customized manner
parser = argparse.ArgumentParser(description="train and evaluate a feed forward fully connected neural network")

# input folder path flag created
parser.add_argument('-i', '--input', help='input folder with data (should include scaled/normalized x_train, x_test, y_test, y_train CSVs', required=True)

# output file name flag created
parser.add_argument('-o', '--output', help='output file for model object; must be pkl', required=False, default='./output.pkl')

# results.csv path flag created
parser.add_argument('-r', '--results', help='path to results.csv (must have Name, Description, Metric, Path)', required=True)

# matrix path flag created
parser.add_argument('-m', '--matrix', help='path to matrix file; must be png', required=False, default='./outputCM.png')

# list of options of hyperparameters for tuning
parser.add_argument('-t', '--tuninghyperparameters', help='json file with lists of hyperparameters for each option (update with types that can be played around with)', required=False, default=None)

# option to chage the number of folds to fit against
parser.add_argument('-f', '--folds', help='how many folds for crossfold validation selection', required=False, default=5)

# options for roc file storage
parser.add_argument('-c', '--curve', help='file path to store ROC curve', required=False, default='./outputROC.png')

def createIt(inputSize=10, numHiddenLayers=2, numHiddenNodes='None', activationHidden='relu', lossType="binary_crossentropy", rate=0.01):
    '''
    Creates model layers and assorts them together
    '''

    if numHiddenNodes == 'None':
        numHiddenNodes = inputSize+2

    i = Input(shape=(inputSize,))
    listLayers = []
    listLayers.append(i)
    for index in range(numHiddenLayers):
        l = Dense(numHiddenNodes, activation=activationHidden)(listLayers[-1])
        listLayers.append(l)
    o = Dense(1, activation='sigmoid')(listLayers[-1])

    model = Model(inputs=i, outputs=o)

    # returns overview of model shell
    model.summary()

    model.compile(optimizer=Adam(learning_rate=rate), loss=lossType)

    return model

def train(model, x_train, y_train, x_test, y_test, epochs=1000, batch_size=20):
    '''
    Trains model on training data that is stored in the generator
    '''
    model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size, validation_data=(x_test, y_test))

def hyperparameterTuning(function, x_train, y_train, param_grid, inputSize, cv, n_jobs=6, scoring='f1'):
    '''
    Selects best model from a park of models with a varied combindation of hyperparameters as specified by the user
    '''
    model = KerasClassifier(model=function, inputSize=inputSize, numHiddenLayers=2, numHiddenNodes='None', activationHidden='relu', optimizer='adam', lossType="binary_crossentropy", rate=0.01)
    g = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=n_jobs, cv=cv, scoring=scoring, refit=True, verbose=3)
    results = g.fit(x_train, y_train)

    return results.best_estimator_, results.best_params_, results.best_score_

def evaluate(model, x_test, y_test, resultsFile, storepath, cmfile, rocfile, crossfoldScore=None, description="simple feed forward model"):
    '''
    Evaluates trained model on test data set and stores results
    '''

    area, threshold = roc(model, x_test, y_test, rocfile, threshold=0.5)

    confusionMat(model, x_test, y_test, cmfile, threshold)

    store(model, storepath)

    f1, acc = accuracy(model, x_test, y_test, threshold)

    varaibleImportances = importances(model, x_test, y_test).to_dict()

    storeIt('DLModel', description, {'f1score': f1, 'regularAccuracy': acc, 'AUC': area, 'crossfoldScore': crossfoldScore}, storepath, resultsFile, varaibleImportances)

if __name__ == '__main__':

    # create args object for command line operability
    args = parser.parse_args()

    # save folder name inputed for test data
    folder = args.input

    # open all data from folder and storing as a dataframe
    x_train = pd.read_csv(os.path.join(folder, 'x_train.csv'))
    x_test = pd.read_csv(os.path.join(folder, 'x_test.csv'))
    y_train = pd.read_csv(os.path.join(folder, 'y_train.csv'))
    y_test = pd.read_csv(os.path.join(folder, 'y_test.csv'))

    # number of inputs into the model stored for later use
    iSize = len(x_train.columns)

    # train and quickly evaluate model
    if args.tuninghyperparameters != None:
        with open(args.tuninghyperparameters) as file:
            d = json.load(file)
        model, params, score = hyperparameterTuning(createIt, x_train, y_train, d, iSize, int(args.folds))
        evaluate(model, x_test, y_test, args.results, args.output, args.matrix, args.curve, crossfoldScore=score, description=params)

    else:
        # create a model shell
        model = createIt(iSize)
        train(model, x_train, y_train, x_test, y_test)
        evaluate(model, x_test, y_test, args.results, args.output, args.matrix, args.curve)


#Escreva um script Python que declara um vetor de dez elementos do tipo int. Em seguida, use um laço for para solicitar ao usuário que insira dez valores inteiros e armazene tais valores no vetor. Por fim, use novamente um laço for para varrer o arranjo e imprimir os valores múltiplos de dois na tela.

list = []
for i in range (10):
    n = int(input(f"Posição {i}: "))
    list.append(n)

print("Dos números citados acimaestes estes são multiplos de 2:")
for i in range (len(list)):
    if list[i] %2 == 0:
        print(list[i])

05.06.21 - Fazer lista e mostrar apenas multiplos de 2.py


N,T_prime = input().split()
N = int(N)

candidates = []
#一文字ずつ確認していき、長さが違えば一方だけ進める
for id in range(N):
  s = input()
  if abs(len(s) - len(T_prime)) > 1:
    continue
  i = 0
  j = 0
  m = 0
  while i < len(s) and j < len(T_prime):
    if s[i] == T_prime[j]:
      i += 1
      j += 1
      m += 1
    elif len(s) > len(T_prime):
      i += 1
    elif len(s) < len(T_prime):
      j += 1
    else:
      i += 1
      j += 1
  if m >= max(len(s), len(T_prime))-1:
      candidates.append(id+1)
print(len(candidates))
print(*candidates)


import re
import simplejson

from analyzers.ats_analyzers import ATSAnalyzer
from utils.page_fetcher import PageFetcher

class SmartRecruiterAnalyzer(ATSAnalyzer):

    @staticmethod
    def get_ats_name():
        return "SmartRecruiters"

    def find_posting_links(self, soup):
        fetcher = PageFetcher()
        offset = 0
        total = 1
        links = []
        while offset < total:
            content = fetcher.fetch_page("{url}?offset={offset}".format(url=self.url, offset=offset))
            content_json = simplejson.loads(content)
            for posting in content_json['content']:
                links.append(posting['ref'])
            total = content_json['totalFound']
            offset += 100
        return links

    def get_direct_regexp(self):
        return re.compile("https?://careers.smartrecruiters.com/\\w+/?")

    def search_for_direct_ats_page_link(self, soup):
        link = soup.find("a", href=re.compile("https?://careers.smartrecruiters.com/\\w+/"))
        if link:
            pattern = re.compile(re.compile("https?://careers.smartrecruiters.com/(\\w+)/?"))
            m = pattern.match(link['href'])
            company = m.group(1)
            return "api.smartrecruiters.com/v1/companies/{domain}/postings".format(domain=company)

    def search_for_job_links(self, soup):
        link = soup.find("a", href=re.compile("https://careers.smartrecruiters.com/\\w+/\\S+"))
        if link:
            pattern = re.compile(re.compile("https://careers?.smartrecruiters.com/(\\w+)"))
            m = pattern.match(link['href'])
            company = m.group(1)
            return "api.smartrecruiters.com/v1/companies/{domain}/postings".format(domain=company)

    def guess_ats_page_url(self):
        return "https://api.smartrecruiters.com/v1/companies/{domain}/postings".format(domain=self.get_domain().capitalize())


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

@version: 0.1
@author:  quantpy
@file:    apply_parallel.py
@time:    2018/4/11 12:41
"""
import os
import warnings
import traceback

from functools import partial
import multiprocessing

import numpy as np
import pandas as pd
from pandas.core.groupby import GroupBy

# import dill
from tqdm import tqdm

from .timeout import TimeoutJob

def get_worker_count(n_workers=None):
    if n_workers is None:
        cpu_count = os.cpu_count() or 1
        n_workers = cpu_count - 1 or 1
    return max(min(n_workers, os.cpu_count()), 1)

def items_apply_parallel(iteritems, func, *args, n_workers, progress=False, **kwargs):
    n_workers = get_worker_count(n_workers)
    if n_workers < 2:
        apply = TimeoutJob
    else:
        pool = multiprocessing.Pool(n_workers)
        apply = pool.apply_async
    to_do = {key: apply(func, (df, ) + args, kwargs) for key, df in iteritems}
    if n_workers >= 2:
        pool.close()

    res_dict, err_dict = {}, {}
    with tqdm(to_do.items(), ascii=True, disable=not progress) as iterable:
        for key, res in iterable:
            iterable.set_description(str(key)[:10])
            # res_dict[key] = res.get(0xffff)
            try:
                res_dict[key] = res.get(0xffff)
            except KeyboardInterrupt:
                print('Job canceled, only part of the result will returned!')
                break
            except Exception as e:
                err_dict[key] = traceback.format_exc()
                warnings.warn(repr(e))

    return res_dict, err_dict

def df_group_apply_parallel(df_group: GroupBy, func, *args, n_workers=None, progress=False, **kwargs):
    if get_worker_count(n_workers) < 2:
        return df_group.apply(func, *args, **kwargs)
    names = df_group.grouper.names
    res_dict, err_dict = items_apply_parallel(df_group, func, *args, n_workers=n_workers, progress=progress, **kwargs)

    if not res_dict:
        return
    k, v = res_dict.popitem()
    res_dict[k] = v
    if isinstance(v, (pd.DataFrame, pd.Series)):
        res = pd.concat(res_dict)
    else:
        res = pd.Series(res_dict)
    if res.index.nlevels > 1:
        res.index.set_names(names, level=range(len(names)), inplace=True)
    else:
        res.index.names = names

    return res

def func_group(df, func, args, **kwargs):
    return df.apply(func, axis=1, args=args, **kwargs)

def df_apply_parallel(df: pd.DataFrame, func, *args, axis=0,
                      n_workers=None, group_size=None, group_count=None,
                      progress=False, **kwargs):

    # if axis in (1, 'columns'):
    #     df = df.T
    #
    # iteritems = df.iteritems()
    # n_items = df.shape[1]
    # names = list(df.columns.names)
    #
    # return _apply_parallel(iteritems, names, n_items, func, *args, n_workers=n_workers, progress=progress, **kwargs)

    n_workers = get_worker_count(n_workers)

    # if n_workers == 1:
    #     return df.apply(func, axis=axis, args=args, **kwargs)

    # transform = False
    if axis in (0, 'index'):
        df = df.T
        # transform = True

    job_count = df.shape[0]

    if group_size is None:
        if group_count is None:
            group_count = n_workers
        group_size = int(np.ceil(job_count / group_count))

    grouper = pd.Series(np.arange(job_count) // group_size, index=df.index)
    df_group = df.groupby(grouper)
    # f = lambda d: d.apply(func, axis=1, args=args, **kwargs)
    f = partial(func_group, func=func, args=args, **kwargs)

    ret = df_group.apply_parallel(f, n_workers=n_workers, progress=progress)
    ret.index = ret.index.droplevel(-1)
    # if transform:
    #     ret = ret.T

    return ret

pd.core.groupby.DataFrameGroupBy.apply_parallel = df_group_apply_parallel
pd.core.groupby.SeriesGroupBy.apply_parallel = df_group_apply_parallel
pd.DataFrame.apply_parallel = df_apply_parallel
pd.Series.apply_parallel = df_apply_parallel