import torch
from torch import nn
import pdb
from torch.autograd import Variable
from torch.nn.functional import cosine_similarity, softmax, normalize
from torch.nn.parameter import Parameter
import math

debug = False
detach_previous_timestep=True
reset_memory= False

def test_simplex_bound(tensor, dim=1):
    # it's impossible to deal with dimensions
    # we will default to test dim 1 of 2-dim (x, y),
    # so that for every x, y is simplex bound

    if dim != 1:
        raise DeprecationWarning("no longer accepts dim other othan one")
        raise NotImplementedError
    t = tensor.contiguous()
    if (t.sum(1) - 1 > 1e-6).any() or (t.sum(1) < -1e-6).any() or (t < 0).any() or (t > 1).any():
        raise ValueError("test simplex bound failed")
    if (t != t).any():
        raise ValueError('test simple bound failed due to NA')
    return True

class Original(nn.Module):
    def __init__(self,
                 x=47782,
                 h=128,
                 L=16,
                 v_t=3656,
                 W=32,
                 R=16,
                 N=64,
                 bs=1):
        super(Original, self).__init__()

        '''PARAMETERS'''
        self.x = x
        self.h = h
        self.L = L
        self.v_t = v_t
        self.W = W
        self.R = R
        self.N = N
        self.bs = bs
        self.E_t = W * R + 3 * W + 5 * R + 3

        """CONTROLLER"""
        self.RNN_list=nn.ModuleList()
        for _ in range(self.L):
            self.RNN_list.append(RNN_Unit(self.x, self.R, self.W, self.h, self.bs))

        self.hidden_previous_timestep=Variable(torch.Tensor(self.bs,self.L,self.h).zero_().cuda())
        self.W_y=nn.Linear(self.L*self.h,self.v_t)
        self.W_E=nn.Linear(self.L*self.h,self.E_t)

        '''memory'''
        '''these should not receive any gradient'''
        # u_0
        self.usage_vector=Variable(torch.Tensor(self.bs,self.N).zero_()).cuda()
        # p, (N), should be simplex bound
        self.precedence_weighting=Variable(torch.Tensor(self.bs,self.N).zero_()).cuda()
        # (N,N)
        self.temporal_memory_linkage=Variable(torch.Tensor(self.bs,self.N, self.N).zero_()).cuda()
        # (N,W)
        self.memory=Variable(torch.Tensor(self.N,self.W).zero_()).cuda()
        # (N, R). Does this require gradient?
        self.last_read_weightings=Variable(torch.Tensor(self.bs, self.N, self.R).fill_(1.0/self.N)).cuda()

        '''computer'''
        self.last_read_vector = Variable(torch.Tensor(self.bs, self.W, self.R).zero_().cuda())
        self.W_r = nn.Linear(self.W * self.R, self.v_t, bias=False)

    def forward(self, input):
        input_x_t = torch.cat((input, self.last_read_vector.view(self.bs, -1)), dim=1)

        '''Controller'''
        hidden_previous_layer=Variable(torch.Tensor(self.bs,self.h).zero_().cuda())
        hidden_this_timestep=Variable(torch.Tensor(self.bs,self.L,self.h).cuda())
        for i in range(self.L):
            hidden_output=self.RNN_list[i](input_x_t, self.hidden_previous_timestep[:,i,:],
                                           hidden_previous_layer)
            hidden_this_timestep[:,i,:]=hidden_output
            hidden_previous_layer=hidden_output

        flat_hidden=hidden_this_timestep.view((self.bs,self.L*self.h))
        output=self.W_y(flat_hidden)
        interface_input =self.W_E(flat_hidden)
        if detach_previous_timestep:
            # I'm detaching every passed on variables. Not sure whether this should be done?
            self.hidden_previous_timestep=hidden_this_timestep.detach()

        '''interface'''
        last_index = self.W * self.R

        # Read keys, each W dimensions, [W*R] in total
        # no processing needed
        # this is the address keys, not the contents
        read_keys = interface_input[:, 0:last_index].contiguous().view(self.bs, self.W, self.R)

        # Read strengths, [R]
        # 1 to infinity
        # slightly different equation from the paper, should be okay
        read_strengths = interface_input[:, last_index:last_index + self.R]
        last_index = last_index + self.R
        read_strengths = 1 - nn.functional.logsigmoid(read_strengths)

        # Write key, [W]
        write_key = interface_input[:, last_index:last_index + self.W]
        last_index = last_index + self.W

        # write strength beta, [1]
        write_strength = interface_input[:, last_index:last_index + 1]
        last_index = last_index + 1
        write_strength = 1 - nn.functional.logsigmoid(write_strength)

        # erase strength, [W]
        erase_vector = interface_input[:, last_index:last_index + self.W]
        last_index = last_index + self.W
        erase_vector = torch.sigmoid(erase_vector)

        # write vector, [W]
        write_vector = interface_input[:, last_index:last_index + self.W]
        last_index = last_index + self.W

        # R free gates? [R] TODO what is this?
        free_gates = interface_input[:, last_index:last_index + self.R]

        last_index = last_index + self.R
        free_gates = torch.sigmoid(free_gates)

        # allocation gate [1]
        allocation_gate = interface_input[:, last_index:last_index + 1]
        last_index = last_index + 1
        allocation_gate = torch.sigmoid(allocation_gate)

        # write gate [1]
        write_gate = interface_input[:, last_index:last_index + 1]
        last_index = last_index + 1
        write_gate = torch.sigmoid(write_gate)

        # read modes [R,3]
        read_modes = interface_input[:, last_index:last_index + self.R * 3]
        # TODO
        read_modes = read_modes.contiguous().view(self.bs, self.R, 3)
        read_modes = nn.functional.softmax(read_modes,dim=2)

        '''memory'''
        # then write
        allocation_weighting=self.allocation_weighting()
        write_weighting=self.write_weighting(write_key,write_strength,
                                             allocation_gate,write_gate,allocation_weighting)
        self.write_to_memory(write_weighting,erase_vector,write_vector)
        # update some
        memory_retention = self.memory_retention(free_gates)
        self.update_usage_vector(write_weighting, memory_retention)
        self.update_temporal_linkage_matrix(write_weighting)
        self.update_precedence_weighting(write_weighting)

        forward_weighting=self_weighting()
        backward_weighting=self.backward_weighting()

        read_weightings=self.read_weightings(forward_weighting, backward_weighting, read_keys, read_strengths,
                                             read_modes)
        # read from memory last, a new modification.
        read_vectors=self.read_memory(read_weightings)

        '''computer'''
        if detach_previous_timestep:
            # DO WE NEED TO DETACH THIS?
            self.last_read_vector = read_vectors.detach()
        output = output + self.W_r(self.last_read_vector.view(self.bs, self.W * self.R))
        return output

    def reset_parameters(self):
        for module in self.RNN_list:
            # this should iterate over RNN_Units only
            module.reset_parameters()
        self.W_y.reset_parameters()
        self.W_E.reset_parameters()
        self.W_r.reset_parameters()
    def new_sequence_reset(self):
        ''''''

        '''COMPUTER'''
        self.last_read_vector = Variable(torch.Tensor(self.bs, self.W, self.R).zero_().cuda())
        self.W_r.weight.detach()

        '''CONTROLLER'''
        for RNN in self.RNN_list:
            RNN.new_sequence_reset()

        self.hidden_previous_timestep = Variable(torch.Tensor(self.bs,self.L,self.h).zero_()).cuda()
        self.W_y.weight.detach()
        self.W_y.bias.detach()
        self.W_E.weight.detach()
        self.W_E.bias.detach()

        '''interface has none'''
        '''memory'''
        # memory is the only value that is not reset after new sequence
        self.usage_vector=Variable(torch.Tensor(self.bs, self.N).zero_().cuda())
        self.precedence_weighting= Variable(torch.Tensor(self.bs, self.N).zero_().cuda())
        self.temporal_memory_linkage = Variable(torch.Tensor(self.bs, self.N, self.N).zero_().cuda())
        self.last_read_weightings=Variable(torch.Tensor(self.bs, self.N, self.R).fill_(1.0/self.N).cuda())
        if reset_memory:
            self.memory=Variable(torch.Tensor(self.N,self.W).zero_()).cuda()

    def write_content_weighting(self, write_key, key_strength, eps=1e-8):
        '''

        :param memory: M, (N, W)
        :param write_key: k, (W), R, desired content
        :param key_strength: \beta, (1) [1, \infty)
        :param index: i, lookup on memory[i]
        :return: most similar weighted: C(M,k,\beta), (N), (0,1)
        '''

        # memory will be (N,W)
        # write_key will be (bs, W)
        # I expect a return of (N,bs), which marks the similiarity of each W with each mem loc

        # (self.bs, self.N)
        innerprod=torch.matmul(write_key,self.memory.t())
        # (parm.N)
        memnorm=torch.norm(self.memory,2,1)
        # (self.bs)
        writenorm=torch.norm(write_key,2,1)
        # (self.N, self.bs)
        normalizer=torch.ger(memnorm,writenorm)
        similarties=innerprod/normalizer.t().clamp(min=eps)
        similarties=similarties*key_strength.expand(-1,self.N)
        normalized= softmax(similarties,dim=1)
        return normalized

    def read_content_weighting(self, read_keys, key_strengths, eps=1e-8):
        '''

        :param memory: M, (N, W)
        :param read_keys: k^r_t, (W,R), R, desired content
        :param key_strength: \beta, (R) [1, \infty)
        :param index: i, lookup on memory[i]
        :return: most similar weighted: C(M,k,\beta), (N, R), (0,1)
        '''

        '''
            torch definition
            def cosine_similarity(x1, x2, dim=1, eps=1e-8):
                w12 = torch.sum(x1 * x2, dim)
                w1 = torch.norm(x1, 2, dim)
                w2 = torch.norm(x2, 2, dim)
                return w12 / (w1 * w2).clamp(min=eps)
        '''

        innerprod=torch.matmul(self.memory.unsqueeze(0),read_keys)
        # this is confusing. matrix[n] access nth row, not column
        # this is very counter-intuitive, since columns have meaning,
        # because they represent vectors
        mem_norm=torch.norm(self.memory,p=2,dim=1)
        read_norm=torch.norm(read_keys,p=2,dim=1)
        mem_norm=mem_norm.unsqueeze(1)
        read_norm=read_norm.unsqueeze(1)
        # (batch_size, locations, read_heads)
        normalizer=torch.matmul(mem_norm,read_norm)

        # if transposed then similiarities[0] refers to the first read key
        similarties= innerprod/normalizer.clamp(min=eps)
        weighted=similarties*key_strengths.unsqueeze(1).expand(-1,self.N,-1)
        ret= softmax(weighted,dim=1)
        return ret

    # the highest freed will be retained? What does it mean?
    def memory_retention(self,free_gate):
        '''

        :param free_gate: f, (R), [0,1], from interface vector
        :param read_weighting: w^r_t, (N, R), simplex bounded,
               note it's from previous timestep.
        :return: \psi, (N), [0,1]
        '''

        # a free gate belongs to a read head.
        # a single read head weighting is a (N) dimensional simplex bounded value

        # (N, R)
        inside_bracket = 1 - self.last_read_weightings * free_gate.unsqueeze(1).expand(-1,self.N,-1)
        ret= torch.prod(inside_bracket, 2)
        return ret

    def update_usage_vector(self, write_wighting, memory_retention):
        '''

        :param previous_usage: u_{t-1}, (N), [0,1]
        :param write_wighting: w^w_{t-1}, (N), simplex bound
        :param memory_retention: \psi_t, (N), simplex bound
        :return: u_t, (N), [0,1], the next usage,
        '''

        ret= (self.usage_vector+write_wighting-self.usage_vector*write_wighting)*memory_retention

        self.usage_vector=ret
        return ret

    def allocation_weighting(self):
        '''
        Sorts the memory by usages first.
        Then perform calculation depending on the sort order.

        The alloation_weighting of the third least used memory is calculated as follows:
        Find the least used and second least used. Multiply their usages.
        Multiply the product with (1-usage of the third least), return.

        Do not confuse the sort order and the memory's natural location.
        Verify backprop.

        :param usage_vector: u_t, (N), [0,1]
        :return: allocation_wighting: a_t, (N), simplex bound
        '''
        sorted, indices= self.usage_vector.sort(dim=1)
        cum_prod=torch.cumprod(sorted,1)
        # notice the index on the product
        cum_prod=torch.cat([Variable(torch.ones(self.bs,1).cuda()),cum_prod],1)[:,:-1]
        sorted_inv=1-sorted
        allocation_weighting=sorted_inv*cum_prod
        # to shuffle back in place
        ret=torch.gather(allocation_weighting,1,indices)
        return ret

    def write_weighting(self, write_key, write_strength, allocation_gate, write_gate, allocation_weighting):
        '''
        calculates the weighting on each memory cell when writing a new value in

        :param memory: M, (N, W), memory block
        :param write_key: k^w_t, (W), R, the key that is to be written
        :param write_strength: \beta, (1) bigger it is, stronger it concentrates the content weighting
        :param allocation_gate: g^a_t, (1), balances between write by content and write by allocation gate
        :param write_gate: g^w_t, (1), overall strength of the write signal
        :param allocation_weighting: see above.
        :return: write_weighting: (N), simplex bound
        '''
        # measures content similarity
        content_weighting=self.write_content_weighting(write_key,write_strength)
        write_weighting=write_gate*(allocation_gate*allocation_weighting+(1-allocation_gate)*content_weighting)
        test_simplex_bound(write_weighting,1)
        return write_weighting

    def update_precedence_weighting(self,write_weighting):
        '''

        :param write_weighting: (N)
        :return: self.precedence_weighting: (N), simplex bound
        '''
        # this is the bug. I called the python default sum() instead of torch.sum()
        # Took me 3 hours.
        # sum_ww=sum(write_weighting,1)
        sum_ww=torch.sum(write_weighting,dim=1)
        self.precedence_weighting=(1-sum_ww).unsqueeze(1)*self.precedence_weighting+write_weighting
        test_simplex_bound(self.precedence_weighting,1)
        return self.precedence_weighting

    def update_temporal_linkage_matrix(self,write_weighting):
        '''

        :param write_weighting: (N)
        :param precedence_weighting: (N), simplex bound
        :return: updated_temporal_linkage_matrix
        '''

        ww_j=write_weighting.unsqueeze(1).expand(-1,self.N,-1)
        ww_i=write_weighting.unsqueeze(2).expand(-1,-1,self.N)
        p_j=self.precedence_weighting.unsqueeze(1).expand(-1,self.N,-1)
        batch_temporal_memory_linkage=self.temporal_memory_linkage.expand(self.bs,-1,-1)
        self.temporal_memory_linkage= (1 - ww_j - ww_i) * batch_temporal_memory_linkage + ww_i * p_j
        test_simplex_bound(self.temporal_memory_linkage,1)
        test_simplex_bound(torch.transpose(self.temporal_memory_linkage,1,2),1)
        return self.temporal_memory_linkage

    def backward_weighting(self):
        '''

        :return: backward_weighting: b^i_t, (N,R)
        '''
        ret= torch.matmul(self.temporal_memory_linkage, self.last_read_weightings)
        test_simplex_bound(ret,1)
        return ret

    def forward_weighting(self):
        '''

        :return: forward_weighting: f^i_t, (N,R)
        '''
        ret= torch.matmul(self.temporal_memory_linkage.transpose(1,2), self.last_read_weightings)
        test_simplex_bound(ret,1)
        return ret
    # TODO sparse update, skipped because it's for performance improvement.

    def read_weightings(self, forward_weighting, backward_weighting, read_keys,
                        read_strengths, read_modes):
        '''

        :param forward_weighting: (bs,N,R)
        :param backward_weighting: (bs,N,R)
        ****** content_weighting: C, (bs,N,R), (0,1)
        :param read_keys: k^w_t, (bs,W,R)
        :param read_key_strengths: (bs,R)
        :param read_modes: /pi_t^i, (bs,R,3)
        :return: read_weightings: w^r_t, (bs,N,R)

        '''

        content_weighting=self.read_content_weighting(read_keys,read_strengths)
        test_simplex_bound(content_weighting,1)
        test_simplex_bound(backward_weighting,1)
        test_simplex_bound(forward_weighting,1)
        # has dimension (bs,3,N,R)
        all_weightings=torch.stack([backward_weighting,content_weighting,forward_weighting],dim=1)
        # permute to dimension (bs,R,N,3)
        all_weightings=all_weightings.permute(0,3,2,1)
        # this is becuase torch.matmul is designed to iterate all dimension excluding the last two
        # dimension (bs,R,3,1)
        read_modes=read_modes.unsqueeze(3)
        # dimension (bs,N,R)
        read_weightings = torch.matmul(all_weightings, read_modes).squeeze(3).transpose(1,2)
        self.last_read_weightings=read_weightings
        # last read weightings
        test_simplex_bound(self.last_read_weightings,1)
        return read_weightings

    def read_memory(self,read_weightings):
        '''

        memory: (N,W)
        read weightings: (N,R)

        :return: read_vectors: [r^i_R], (W,R)
        '''

        return torch.matmul(self.memory.t(),read_weightings)

    def write_to_memory(self,write_weighting,erase_vector,write_vector):
        '''

        :param write_weighting: the strength of writing
        :param erase_vector: e_t, (W), [0,1]
        :param write_vector: w^w_t, (W),
        interfere with each other
        :return:
        '''
        term1_2=torch.matmul(write_weighting.unsqueeze(2),erase_vector.unsqueeze(1))
        term1=self.memory.unsqueeze(0)*(Variable(torch.ones((self.bs,self.N,self.W)).cuda())-term1_2)
        term2=torch.matmul(write_weighting.unsqueeze(2),write_vector.unsqueeze(1))
        self.memory=torch.mean(term1+term2, dim=0)

class RNN_Unit(nn.Module):
    """
    A single unit of deep RNN
    """

    def __init__(self, x, R, W, h, bs):
        super(RNN_Unit, self).__init__()

        self.x = x
        self.R = R
        self.W = W
        self.h = h
        self.bs = bs

        self.W_input=nn.Linear(self.x+self.R*self.W+2*self.h,self.h)
        self.W_forget=nn.Linear(self.x+self.R*self.W+2*self.h,self.h)
        self.W_output=nn.Linear(self.x+self.R*self.W+2*self.h,self.h)
        self.W_state=nn.Linear(self.x+self.R*self.W+2*self.h,self.h)

        self.old_state=Variable(torch.Tensor(self.bs,self.h).zero_().cuda())

    def reset_parameters(self):
        for module in self.children():
            module.reset_parameters()

    def forward(self,input_x,previous_time,previous_layer):
        # a hidden unit outputs a hidden output new_hidden.
        # state also changes, but it's hidden inside a hidden unit.

        semicolon_input=torch.cat([input_x,previous_time,previous_layer],dim=1)

        # 5 equations
        input_gate=torch.sigmoid(self.W_input(semicolon_input))
        forget_gate=torch.sigmoid(self.W_forget(semicolon_input))
        new_state=forget_gate * self.old_state + input_gate * \
                  torch.tanh(self.W_state(semicolon_input))
        output_gate=torch.sigmoid(self.W_output(semicolon_input))
        new_hidden=output_gate*torch.tanh(new_state)
        self.old_state=new_state

        return new_hidden

    def new_sequence_reset(self):
        self.W_input.weight.detach()
        self.W_input.bias.detach()
        self.W_output.weight.detach()
        self.W_output.bias.detach()
        self.W_forget.weight.detach()
        self.W_forget.bias.detach()
        self.W_state.weight.detach()
        self.W_state.bias.detach()

        self.old_state=Variable(torch.Tensor(self.bs,self.h).zero_().cuda())


# Copyright 2010-2011 OpenStack Foundation
# Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import os
import subprocess

os_log_cfg_file = {'ceilometer': 'openstack-ceilometer',
                   'cinder': 'openstack-cinder',
                   'httpd': 'openstack-dashboard',
                   'glance': 'openstack-glance',
                   'keystone': 'openstack-keystone',
                   'neutron': 'openstack-neutron',
                   'nova': 'openstack-nova',
                   'swift': 'openstack-swift',
                   'rabbitmq': 'rabbitmq-server',
                   'mariadb': 'mariadb',
                   'mongodb': 'mongodb',
                   'heat': 'openstack-heat'}

os_service_name = ['nova', 'cinder', 'httpd', 'glance', 'keystone',
                   'neutron', 'ceilometer', 'swift', 'rabbitmq',
                   'mariadb', 'mongodb', 'heat']

logrotaion_dir = "/etc/logrotate.d/"

class LogRotateCheck(object):

    def find_openstack_service(self):
        output = subprocess.check_output('systemctl | grep -i  -e "mongo" \
                                        -e "maria" -e "httpd" -e "rabbit" \
                                        -e "openstack" | tr -s " " \
                                        | cut -d" " -f1', shell=True)
        running_service = output.split('\n')
        running_service.append('keystone')
        service_list = []
        for service_name in os_service_name:
            r = [s for s in running_service if service_name in s]
            if r:
                service_list.append(service_name)
        return service_list

    def log_rotate_check(self):
        try:
            result = []
            final_result = {}
            tmp = {}
            overall_status = True
            service_list = self.find_openstack_service()
            for service_name in service_list:
                cfg_file = os_log_cfg_file[service_name]
                if os.path.exists(logrotaion_dir + cfg_file):
                    cfg_lines = open(logrotaion_dir + cfg_file, "r").read().\
                        splitlines()
                    case_name = 'Log Rotate Check for ' + cfg_file
                    for line in cfg_lines:
                        if "/var/log" in line and not line.startswith('#'):
                            tmp[cfg_file] = "Config Exists"
                    if cfg_file not in tmp:
                        overall_status = False
                        res = {'status': 'Fail'}
                        res.update(
                            {'test_case_name': case_name})
                        res.update({'message': 'No Log Rotation Config Found'})
                        result.append(res)
                    else:
                        res = {'status': 'Pass'}
                        res.update(
                            {'test_case_name': case_name})
                        res.update({'message': 'Log Rotation Config Found'})
                        result.append(res)
                else:
                    # tmp[cfg_file] = "Log file doesn't exists"
                    overall_status = False
                    res = {'status': 'Fail'}
                    res.update(
                        {'test_case_name': case_name})
                    res.update({'message': "Log file doesnot exists"})
                    result.append(res)
            final_result.update(
                {'OverallStatus': overall_status})
            final_result.update({'result': result})
            print(final_result)
            return
        except Exception:
            final_result.update(
                {'OverallStatus': False})
            result = {}
            result.update({'test_case_name': 'Log Rotate Check'})
            result.update({'status': 'Fail'})
            result.update(
                {'message': 'Exception in log rotate check'})
            final_result.update({'result': [result]})
            print(final_result)
            return

if __name__ == '__main__':
    lrc = LogRotateCheck()
    lrc.log_rotate_check()


import re
import sys
import json

regex = re.compile('([a-z0-9]+)="([^"]+)"', re.I)
dup_id = re.compile("stackoverflow.com/questions/(\d+)/")

fp_all = open("all_questions", "w")
fp_dup = open("dup_questions", "w")
with open(sys.argv[1], "r") as fp:
    for line in fp:
        try:
            line = line.strip()
            post = dict(re.findall(regex, line))
            if "Id" not in post:
                continue

            if "has already been answered" in post["Body"] or ("Title" in post and "[duplicate]" in post["Title"]):
                post["dups"] = [int(num) for num in re.findall(dup_id, post["Body"])]
                fp_dup.write(post["Id"] + "wcyz666SQL" + json.dumps(post) + "\n")

            fp_all.write(post["Id"] + "wcyz666SQL" + json.dumps(post) + "\n")
        except:
            pass


#!/usr/bin/python
# -*- coding: utf-8 -*-
import re

from django.db.models import Q
from django.utils.safestring import mark_safe
from rest_framework import serializers

from .relations import ArticleCategoryField, ArticleAuthorField, ArticleTagField
from .models import Article, Category, Tag
from .utils import truncate_content, CommonMarkdown

__author__ = "xuzhao"
__email__ = "contact@xuzhao.xin"
__file__ = "serializers.py"
__description__ = ""
__created_time__ = "2018/10/11 13:56"

class ArticleSerializer(serializers.ModelSerializer):
    body = serializers.SerializerMethodField(read_only=True)
    category = ArticleCategoryField(read_only=True)
    author = ArticleAuthorField(read_only=True)
    tags = ArticleTagField(read_only=True, many=True)

    @staticmethod
    def get_body(obj):
        body = obj.body
        reg = re.compile('<[^>]*>')
        return truncate_content(mark_safe(reg.sub('', CommonMarkdown.get_markdown(obj.body))), length=100)

    class Meta:
        model = Article
        exclude = ('last_updated_by', 'created_time', 'last_updated_time')

class ArticleCreateSerializer(serializers.ModelSerializer):

    class Meta:
        model = Article
        exclude = ('last_updated_by', 'created_time', 'last_updated_time')

class ArticleUpdateSerializer(serializers.ModelSerializer):

    class Meta:
        model = Article
        exclude = ('last_updated_by', 'created_time', 'last_updated_time', 'author', 'slug')

class CategorySerializer3(serializers.ModelSerializer):

    author_display = ArticleAuthorField(read_only=True, source='author')

    class Meta:
        model = Category
        exclude = ('last_updated_by', 'created_time', 'last_updated_time')

class CategorySerializer2(serializers.ModelSerializer):

    author_display = ArticleAuthorField(read_only=True, source='author')
    sub_categories = serializers.SerializerMethodField(read_only=True)

    def get_sub_categories(self, obj):
        request = self.context['request']
        if request.user.is_superuser:
            queryset = Category.objects.filter(parent_category_id=obj.id)
        else:
            queryset = Category.objects.filter(Q(is_private=False) | Q(owner_id=request.user.id), is_active=True)
        serializer = CategorySerializer3(queryset, many=True, read_only=True,
                                         context={'request': request})
        return serializer.data

    class Meta:
        model = Category
        exclude = ('last_updated_by', 'created_time', 'last_updated_time')

class CategorySerializer(serializers.ModelSerializer):

    author_display = ArticleAuthorField(read_only=True, source='author')
    sub_categories = serializers.SerializerMethodField(read_only=True)

    def get_sub_categories(self, obj):
        request = self.context['request']
        if request.user.is_superuser:
            queryset = Category.objects.filter(parent_category_id=obj.id)
        else:
            queryset = Category.objects.filter(Q(is_private=False) | Q(owner_id=request.user.id), is_active=True)
        serializer = CategorySerializer2(queryset, many=True, read_only=True,
                                         context={'request': request})
        return serializer.data

    class Meta:
        model = Category
        exclude = ('last_updated_by', 'created_time', 'last_updated_time')

class TagSerializer(serializers.ModelSerializer):

    class Meta:
        model = Tag
        exclude = ('last_updated_by', 'created_time', 'last_updated_time')

class CommonSerializer(serializers.ModelSerializer):

    def __init__(self, *args, **kwargs):
        self.Meta.model = kwargs['model']
        kwargs['context'] = {'request': kwargs['request']}
        del kwargs['model']
        del kwargs['request']
        super(serializers.ModelSerializer, self).__init__(*args, **kwargs)

    class Meta:
        exclude = ('last_updated_by', 'created_time', 'last_updated_time')


from custom_hooks.dcm_base_hook import  DcmBaseHook
from airflow.models import BaseOperator
from airflow.utils.decorators import apply_defaults
from airflow.version import version

class DcmListProjectOperator(BaseOperator):
	"""
	list the projects accociated with a DCM profileId

	Used to test the connection to the API is valid and the hook works.

	:param profileId: The profile id accociated with the DFA/DCM account
	:type profileId: STRING

	"""
	@apply_defaults
	def __init__(self, profileId, dcm_conn_id = 'datateam_dcm_default', delegate_to=None, *args, **kwargs):

		super(DcmListProjectOperator, self).__init__(*args, **kwargs)

		self.profileId = profileId

		self.dcm_conn_id = dcm_conn_id
		self.delegate_to = delegate_to

	def execute(self, context):

		hook = DcmBaseHook(dcm_conn_id=self.dcm_conn_id, delegate_to=self.delegate_to)

		hook.list_projects(profileId=self.profileId)

class DcmCreateReportOperator(BaseOperator):
	"""
	creates the report listed in a valid report.json

	:param profileId: The profile id accociated with the DFA/DCM account
	:type profileId: STRING

	:param reports: forms the request body to be sent to the DFA reporting api
					please see https://developers.google.com/doubleclick-advertisers/v3.3/reports/insert
	:type REPORT RESOURCE: can be a list of report resource or single resource
	A valid exmaple is

	[{
    "name": "prospecting_report",
    "type": "STANDARD",
    }]

	"""
	@apply_defaults
	def __init__(self, profileId, reports, dcm_conn_id='datateam_dcm_default', delegate_to=None, *args, **kwargs):

		super(DcmCreateReportOperator, self).__init__(*args, **kwargs)

		self.profileId = profileId
		self.reports = reports

		self.dcm_conn_id = dcm_conn_id
		self.delegate_to = delegate_to

	def execute(self, context):

		hook = DcmBaseHook(dcm_conn_id=self.dcm_conn_id, delegate_to=self.delegate_to)
		# so reportId returned is auto pushed to xcom
		return hook.create_reports(profileId=self.profileId, reports=self.reports)

class DcmRunReportOperator(BaseOperator):

	"""
	Runs a report created by DcmCreateReportOperator or by valid report id

	:param profileId: The profile id accociated with the DFA/DCM account
	:type profileId: STRING

	:param reportId (templated) a list of report ids from created reports. If using template then set useXcom to True
	:type reportId: LIST

	valid example is ['12123123', '123456789']

	:param useXcom: If using DcmCreateReportOperator downstream then set this to True. If passing in report Id manually then set to False
	:type useXcom: BOOL

	"""

	template_fields = ['reportId']
	@apply_defaults
	def __init__(self, profileId, reportId, useXcom=True, dcm_conn_id = 'datateam_dcm_default', delegate_to=None, *args, **kwargs):

		super(DcmRunReportOperator, self).__init__(*args, **kwargs)

		self.profileId = profileId
		self.reportId = reportId
		self.useXcom = useXcom

		self.dcm_conn_id = dcm_conn_id
		self.delegate_to = delegate_to

	def execute(self, context):

		hook = DcmBaseHook(dcm_conn_id=self.dcm_conn_id, delegate_to=self.delegate_to)

		return hook.run_report(profileId=self.profileId, reportId=self.reportId, useXcom=self.useXcom)

class DcmDownloadReportOperator(BaseOperator):

	"""
	First checks to see if report is available for download then uploads report to GCS bucket. Requries a valid fieldId dict
	e.g {"reportId":xxxxx, "fileId":xxxx}

	:param profileId: The profile id accociated with the DFA/DCM account
	:type profileId: STRING

	:param fileId_dict: A valid dictionary containing a report id and file id e.g {"reportId":xxxxx, "fileId":xxxx}
	:type fileId_dict: DICT

	:param filename: The name of the file created in GCS containing the report data e.g example_file.csv
	:type filename: STRING

	:param bucket: The name of the bucket to store the report file in. Should not include the gs:// prefix
	:type bucket: STRING

	:param useXcom: required when relying on Xcom value of previous task to pass into the templated field of this operator.
					this is becuase Xcom values are pickled and need to be reformated before they can be used.
					an example would be passing in the fileId_dict paramater of this operator as
					fileId_dict = {{task_instance.xcom_pull(task_id='<task_id>')}}. This sould be set to false if you are going to
					pass in the fileId_dict manually.
	:type useXcom: BOOL default = True
	"""

	template_fields = ['fileId_dict']
	@apply_defaults
	def __init__(self, profileId, fileId_dict, filename, bucket, useXcom=True, dcm_conn_id = 'datateam_dcm_default', delegate_to = None, *args, **kwargs):

		super(DcmDownloadReportOperator, self).__init__(*args, **kwargs)

		self.profileId = profileId
		self.fileId_dict = fileId_dict
		self.useXcom = useXcom
		self.filename = filename
		self.bucket = bucket

		self.dcm_conn_id = dcm_conn_id
		self.delegate_to = delegate_to

	def execute(self, context):

		hook = DcmBaseHook(dcm_conn_id=self.dcm_conn_id, delegate_to=self.delegate_to)

		return hook.download_report_file(profileId=self.profileId, fileId_dict=self.fileId_dict, useXcom=self.useXcom, filename=self.filename, bucket=self.bucket)


with open("text_5.txt", "a+", encoding="utf-8") as my_file:
    while True:
        input_numbers = input('Введите числа через пробел ')
        try:
            num_check  = input_numbers.split()
            for i in num_check:
                i = int(i)
            break
        except ValueError:
            print('Только числа!')
    my_file.write(input_numbers)
    my_file.seek(0)
    numbers = ((my_file.readline()).split())
    num_sum = 0
    for el in numbers:
        num_sum += int(el)
    print(f'Сумма элементов в файле - {num_sum}')


'''
and 　并且 所有条件都满足，总体上才是条件满足
or　　或者　任意一个条件满足，总体上就是条件满足
not　非
'''

car = 3
home = 2
love = 0 # 0 表示不爱,１ 表示爱

# 只要不爱她闺女，就可以结婚
if not love == 1:
    print("可以结婚")
else:
    print("不能结婚")

# 丈母娘要求，至少有３辆车并且有２套房，或者要爱她闺女，才能结婚
# if (car >= 3 and home >= 2) or love == 1:
#     print("可以结婚")
# else:
#     print("我的女儿呀，不能嫁给你")

# 丈母娘要求，至少有３辆车，或者有２套房，或者要爱她闺女，才能结婚
# if car >= 3 or home >= 2 or love == 1:
#     print("可以结婚")
# else:
#     print("我的女儿呀，不能嫁给你")

# 丈母娘要求，至少有３辆车，并且有２套房，并且要爱她闺女，才能结婚
# if car >= 3 and home >=2 and love == 1:
#     print("可以结婚")
# else:
#     print("我的女儿呀，不能嫁给你")

print("程序结束")


# =============================================================================
# Basics packages
# =============================================================================
import numpy as np
import copy
from astropy.io import fits
from scipy.interpolate import NearestNDInterpolator
# =============================================================================
# Astropy and associated packages
# =============================================================================

# =============================================================================
# KOALA packages
# =============================================================================
# Modular
from koala.corrections.correction import CorrectionBase
from koala.rss import RSS

class Throughput(CorrectionBase):
    """
    Throughput correction class.

    This class accounts for the relative flux loss due to differences on the fibre efficiencies.

    Attributes
    ----------
    - name
    -
    """
    name = "ThroughputCorrection"
    throughput = None
    verbose = False

    def __init__(self, **kwargs):
        super().__init__()
        self.throughput = kwargs.get('throughput', None)
        self.throughput_path = kwargs.get('throughput_path', None)
        if self.throughput_path is not None:
            self.load_throughput(self.throughput_path)

    @staticmethod
    def create_throughput_from_flat(rss_set, clear_nan=True,
                                    statistic='median',
                                    smooth=False):
        """Compute the throughput function from a set of flat exposures.

        Given a set of flat exposures, this method will estimate the average
        efficiency of each fibre.

        Parameters
        ----------
        - rss_set: (list) List of RSS data.
        - clean_nan: (bool, optional, default=True) If True, nan values will be replaced
        by a nearest neighbour interpolation.
        - statistic: (str, optional, default='median') Set to 'median' or 'mean'
        to compute the throughput function.
        - smooth: (bool, optional, default=False) Apply a smoothing gaussian function
        to the throughput solution.
        """
        if statistic == 'median':
            stat_func = np.nanmedian
        elif statistic == 'mean':
            stat_func = np.nanmean

        fluxes = []
        for rss in rss_set:
            f = rss.intensity_corrected / rss.info['exptime']
            fluxes.append(f)
        # Combine
        combined_throughput = stat_func(fluxes, axis=0)

        # Normalize flat
        throughput = combined_throughput / stat_func(
            combined_throughput, axis=0)[np.newaxis, :]
        if clear_nan:
            x, y = np.meshgrid(np.arange(0, throughput.shape[1]),
                               np.arange(0, throughput.shape[0]))
            nan_mask = np.isfinite(throughput)
            interpolator = NearestNDInterpolator(list(zip(x[nan_mask], y[nan_mask])),
                                                 throughput[nan_mask])
            throughput = interpolator(x, y)
        if smooth:
            raise NotImplementedError("Smoothing not implemented!")
        return throughput, None

    def load_throughput(self, path, extension=1):
        """Load a throughput map from a FITS file."""
        self.throughput_path = path
        self.corr_print("Loading throughput from: ", self.throughput_path)
        with fits.open(self.throughput_path) as f:
            self.throughput = f[extension].data.copy()

    def apply(self, rss, throughput=None, plot=True):
        """Apply a 2D throughput model to a RSS.

        Parameters
        ----------
        - throughput
        - rss: (RSS)
        - plot: (bool, optional, default=True)
        """

        if throughput is None and self.throughput is not None:
            throughput = self.throughput
        else:
            raise RuntimeError("Throughput not provided!")

        if type(rss) is not RSS:
            raise ValueError("Throughput can only be applied to RSS data:\n input {}"
                             .format(type(rss)))
        # =============================================================================
        # Copy input RSS for storage the changes implemented in the task
        # =============================================================================
        rss_out = copy.deepcopy(rss)

        rss_out.intensity_corrected = rss_out.intensity_corrected / throughput
        rss_out.variance_corrected = rss_out.variance_corrected / throughput ** 2
        self.log_correction(rss, status='applied')
        return rss_out


#
#  Created by jon finerty - 2013
#

import sublime
import sublime_plugin

# GLOBAL SETTINGS
SNAKE_ON = False
SNAKE_DIRECTION = 'right'
SNAKE_SCORE = 0
SNAKE_STARTING_LENGTH = 10
SNAKE_STARTING_SPEED = 100
SNAKE_SPEED_INCREASE_RATE = 0.99
SNAKE_X_BOUNDARY = 0
SNAKE_Y_BOUNDARY = 0

# OVERWRITE ARROW KEYS - but pass through to old commands
class set_snake_rightCommand(sublime_plugin.TextCommand):

    def run(self, edit):
        global SNAKE_DIRECTION
        if SNAKE_DIRECTION != 'left':
            SNAKE_DIRECTION = 'right'
        if SNAKE_ON == False:
            self.view.run_command("move", {
                                    "by": "characters",
                                    "forward": True
                                 })

class set_snake_leftCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        global SNAKE_DIRECTION
        if SNAKE_DIRECTION != 'right':
            SNAKE_DIRECTION = 'left'
        if SNAKE_ON == False:
            self.view.run_command("move", {
                                    "by": "characters",
                                    "forward": False
                                 })

class set_snake_upCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        global SNAKE_DIRECTION
        if SNAKE_DIRECTION != 'down':
            SNAKE_DIRECTION = 'up'
        if SNAKE_ON == False:
            self.view.run_command("move", {
                                    "by": "lines",
                                    "forward": False
                                 })

class set_snake_downCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        global SNAKE_DIRECTION
        if SNAKE_DIRECTION != 'up':
            SNAKE_DIRECTION = 'down'
        if SNAKE_ON == False:
            self.view.run_command("move", {
                                    "by": "lines",
                                    "forward": True
                                 })

class SnakeCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        global SNAKE_ON, SNAKE_SCORE, SNAKE_X_BOUNDARY, SNAKE_Y_BOUNDARY, SNAKE_DIRECTION

        # reset stuff
        SNAKE_SCORE = 0
        SNAKE_DIRECTION = "right"

        if SNAKE_ON == False:

            SNAKE_ON = True

            templateView = self.view
            window = templateView.window()

            # Set up copy of current window, so work is not lost.
            entireFileRegion = sublime.Region(0, templateView.size())
            fileText = templateView.substr(entireFileRegion)
            syntax = templateView.settings().get('syntax')
            pos = templateView.sel()

            # copy across and set syntax
            snakeView = window.new_file()
            snakeView.set_scratch(True)
            snakeView.set_name("SNAKE")
            window.focus_view(snakeView)
            edit = snakeView.begin_edit()
            snakeView.insert(edit, 0, fileText + "\n")
            snakeView.end_edit(edit)
            snakeView.set_syntax_file(syntax)

            # replace tabs with spaces
            edit = snakeView.begin_edit()
            snakeStartingX, snakeStartingY = snakeView.rowcol(pos[0].a)
            # expand tabs tends to break stuff, check for tabs first?
            tabs = snakeView.find("\t", 0)
            if tabs != None:
                snakeView.run_command("expand_tabs", {"set_translate_tabs": True})
            snakeView.end_edit(edit)

            # find longest line
            entireSnakeViewRegion = sublime.Region(0, snakeView.size())
            lines = snakeView.split_by_newlines(entireSnakeViewRegion)
            maxLineLength = 0
            for line in lines:
                if line.size() > maxLineLength:
                    maxLineLength = line.size()

            # add on nessacary space to end of lines
            edit = snakeView.begin_edit()
            totalPaddingOffset = 0
            for line in lines:
                paddingSize = (maxLineLength - line.size()) + 1
                paddingString = (" " * (paddingSize - 1)) + "|"
                snakeView.insert(edit,
                                line.b + totalPaddingOffset,
                                paddingString)
                totalPaddingOffset = totalPaddingOffset + paddingSize
            snakeView.end_edit(edit)

            # set word wrap to maximum line length, otherwise pain
            snakeView.settings().set("wrap_width", 0)
            snakeView.settings().set("word_wrap", False)

            # Add border to bottom of code so its more obvious
            edit = snakeView.begin_edit()
            SNAKE_Y_BOUNDARY = maxLineLength
            SNAKE_X_BOUNDARY = len(lines)
            bottomBorder = ("_" * maxLineLength) + "|\n"
            snakeView.insert(edit, snakeView.size(), bottomBorder)
            snakeView.end_edit(edit)

            # Create default snake -
            # consists of a set of positions (stored as text_points)
            snakeStartingPoint = snakeView.text_point(
                                    snakeStartingX,
                                    0)
            snakeEndingPoint = snakeStartingPoint + SNAKE_STARTING_LENGTH + 1
            snake = range(snakeStartingPoint, snakeEndingPoint)
            snakeHeadIndex = SNAKE_STARTING_LENGTH
            updateSpeed = SNAKE_STARTING_SPEED

            # draw initial snake
            edit = snakeView.begin_edit()
            for segment in snake:
                segmentRegion = sublime.Region(segment, segment + 1)
                snakeView.replace(edit, segmentRegion, u"\u2588")
            snakeView.end_edit(edit)
            snakeView.show_at_center(snakeStartingPoint)

            # start snake update timeout loop
            sublime.set_timeout(lambda: renderSnake(snakeView,
                                                    snake,
                                                    snakeHeadIndex,
                                                    updateSpeed), updateSpeed)
        else:
            SNAKE_ON = False

def renderSnake(snakeView, snake, snakeHeadIndex, updateSpeed):
    global SNAKE_DIRECTION, SNAKE_ON, SNAKE_SCORE, SNAKE_X_BOUNDARY, SNAKE_X_BOUNDARY

    if SNAKE_ON:
        SNAKE_SCORE = SNAKE_SCORE + 1

        # 'Render' new snake position
        edit = snakeView.begin_edit()

        # Get position of cell to be eaten
        newPosX, newPosY = snakeView.rowcol(snake[snakeHeadIndex])
        if SNAKE_DIRECTION == "right":
            newPosY = newPosY + 1
        elif SNAKE_DIRECTION == "left":
            newPosY = newPosY - 1
        elif SNAKE_DIRECTION == "up":
            newPosX = newPosX - 1
        else:
            newPosX = newPosX + 1

        # check boundary lose conditions
        if (newPosX >= SNAKE_X_BOUNDARY or newPosY >= SNAKE_Y_BOUNDARY):
            gameOver()
        newPoint = snakeView.text_point(newPosX, newPosY)

        snakeView.show_at_center(newPoint)
        eatenChar = snakeView.substr(newPoint)

        # draw head
        newPointRegion = sublime.Region(newPoint, newPoint + 1)
        snakeView.replace(edit, newPointRegion, u"\u2588")

        # see if lost by eating oneself
        for segment in snake:
            if newPoint == segment:
                gameOver()

        if eatenChar == " ":  # don't grow
            tailIndex = snakeHeadIndex + 1
            if tailIndex >= len(snake):
                tailIndex = 0
            tailPos = snake[tailIndex]
            tailRegion = sublime.Region(tailPos, tailPos + 1)
            snakeView.replace(edit, tailRegion, " ")
            snakeHeadIndex = tailIndex
            snake[snakeHeadIndex] = newPoint
        elif eatenChar == "\n":  # eaten a wall, die
            gameOver()
        else:  # grow snake
            SNAKE_SCORE = SNAKE_SCORE + 1
            snakeHeadIndex = snakeHeadIndex + 1
            snake.insert(snakeHeadIndex, newPoint)
            if (updateSpeed > 1):
                updateSpeed = updateSpeed * SNAKE_SPEED_INCREASE_RATE

        snakeView.end_edit(edit)

        sublime.status_message("SNAKE_SCORE: " + str(SNAKE_SCORE))
        sublime.set_timeout(lambda: renderSnake(snakeView,
                                                snake,
                                                snakeHeadIndex,
                                                updateSpeed), int(updateSpeed))
    else:
        # reset arrow key functionality
        SNAKE_ON = False
        SNAKE_DIRECTION = "right"

def gameOver():
    global SNAKE_ON
    sublime.error_message("Game Over!\nYour SNAKE_SCORE was: " +
                          str(SNAKE_SCORE))
    SNAKE_ON = False


#
# @lc app=leetcode.cn id=107 lang=python3
#
# [107] 二叉树的层次遍历 II
#

# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
        if not root:
            return []
        queue = [root]
        res = []
        while queue:
            cur = []
            nextqueue = []
            for i in queue:
                cur.append(i.val)
                if i.left:
                    nextqueue.append(i.left)
                if i.right:
                    nextqueue.append(i.right)
            queue = nextqueue
            res.append(cur)
        return res[::-1]

# @lc code=end


"""
Author: Hugo Castalan
Email: hugoc@mit.edu

File description: Sample code format to keep your code organized. Feel free to delete the "Help: ..." when not needed
"""

#########################################
# IMPORTS
#########################################

import pandas as pd

#########################################
# FUNCTIONS
#########################################

# Help: Put any functions that are essential to your analysis, Tjis should be largest part of your code

def load_csv(file_path):
    """
    Loading csv of given path and returning df (press " three times under a function and hit enter to get this)
    :param file_path: the path of the file
    :return: df
    """
    df = pd.read_csv(file_path)
    return df

def calc_returns(df, horizon):
    """
    raise with not implemented error
    :param df: price df
    :param horizon: return period
    :return:
    """
    raise NotImplementedError

#########################################
# MAIN
#########################################

# Help: Main should contain functions that are code and wrap most functions above together into a quick analysis
# Help: You pass in some key variables and call a few functions, keep it short

def run_return_analysis():

    file_path = "......folder/px_whatever.csv"
    horizon = 20

    px_df = load_csv(file_path=file_path)
    ret_df = calc_returns(df=px_df, horizon=horizon)

    save_path = "...../folder2/ret_whatever.csv"
    ret_df.to_csv(save_path)

#########################################
# EXECUTION GUARD
#########################################

# Help: Notice that only this portion under the guard will be call when hit run

if __name__ == '__main__':
    run_return_analysis()


# %%
import tensorflow as tf

physical_devices = tf.config.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(physical_devices[0], True)

import gym
import tensorflow as tf
import numpy as np
import random
from collections import deque
from tensorflow.keras.models import Sequential
from tensorflow.keras import layers
from tensorflow.keras.optimizers import Adam

REPLAY_SIZE = 10000
BATCH_SIZE = 32
ENV_NAME = 'CartPole-v0'
EPISODE = 10000
STEP = 300

class DqnAgent():
    def __init__(self, env):
        self.replay_buffer = deque()
        self.time_step = 0
        self.gamma = 0.7
        self.learning_rate = 0.01
        self.epsilon = 1
        self.max_epsilon = 1
        self.min_epsilon = 0.001
        self.epsilon_time_decay = 0.9
        self.n_states = env.observation_space.shape[0]
        self.n_actions = env.action_space.n
        self.create_Q_network()

    def create_Q_network(self):
        print(f'n states: {self.n_states}')
        model = Sequential([
            layers.Dense(32, input_dim=self.n_states, activation='relu', kernel_initializer='he_uniform',
                         bias_initializer='zeros'),  # , kernel_initializer='he_uniform'),
            layers.Dense(32, activation='relu', kernel_initializer='he_uniform', bias_initializer='zeros'),
            # , kernel_initializer='he_uniform'),
            layers.Dense(self.n_actions, activation='linear', kernel_initializer='he_uniform', bias_initializer='zeros')
            ##, name='q_value')
        ])
        model.compile(optimizer=Adam(learning_rate=0.01),
                      loss='mse')
        self.model = model

    def one_hot_encode(self, action):
        one_hot_action = np.zeros(self.n_actions)
        one_hot_action[action] = 1
        return one_hot_action

    def learn(self, state, action, reward, next_state, done):
        # one_hot_action = self.one_hot_encode(action)
        self.replay_buffer.append((state, action, reward, next_state, done))
        if len(self.replay_buffer) > REPLAY_SIZE:
            self.replay_buffer.popleft()
        if len(self.replay_buffer) > BATCH_SIZE:
            self.train()

    def train(self):
        self.time_step += 1
        mini_batch = random.sample(self.replay_buffer, BATCH_SIZE)
        for data in mini_batch:
            state, action, reward, next_state, done = data
            y_reward = reward
            if not done:
                y_reward = reward + self.gamma * np.max(self.model.predict(next_state)[0])
            # state_input = np.reshape(state,[4,-1])
            y = self.model.predict(state)
            y[0][action] = y_reward
            self.model.fit(state, y, epochs=1, verbose=0)

        if self.epsilon > self.min_epsilon:
            self.epsilon *= self.epsilon_time_decay
        #
        # state_batch = [data[0] for data in mini_batch]
        # action_batch = [data[1] for data in mini_batch]
        # reward_batch = [data[2] for data in mini_batch]
        #
        # # calc y
        # y_batch = []
        # Q_value_batch = self.model.predict(state)

    def act(self, state):
        # state_input = np.reshape(state, [4,-1])
        print(state)
        next_values = self.model.predict(state)
        return np.argmax(next_values[0])

    def act_epsilon_gready(self, state):
        if np.random.rand() <= self.epsilon:
            return self.env.action_space.sample()
        else:
            # state_input = np.reshape(state, [4,-1])
            q_values = self.model.predict(state)
            return np.argmax(q_values[0])

def test_dqn():
    env = gym.make('CartPole-v0')
    agent = DqnAgent(env)
    model = agent.model
    a = np.array([1, 2, 3, 4])
    b = np.reshape(a, [1, 4])
    # model.fit(np.random.random([10,4]),np.random.random([10,2]))
    p = model.predict(b)
    print(f'input dim: {agent.n_states} ')
    print(p)

def main():
    env = gym.make(ENV_NAME)
    agent = DqnAgent(env)

    for episode in range(EPISODE):
        state = env.reset()
        state = np.reshape(state, [1, 4])
        for step in range(STEP):
            action = agent.act_epsilon_gready(state)
            next_state, reward, done, info = env.step(action)
            next_state = np.reshape(next_state, [1, 4])

            reward_agent = -1 if done else 0.1
            agent.learn(state, action, reward, next_state, done)
            state = next_state
            if done:
                break

if __name__ == '__main__':
    main()
    # agent = DqnAgent()
    # test_dqn()


from time import sleep
from math import *
titulo = 'CRIADOR DE FATORIAL'
print('#' * len(titulo))
print(titulo)
print('#' * len(titulo))

num = int(input('\nDigite um número para descobrir seu fatorial: '))
c = num
f = 1
result = factorial(num)
print('Calculando fatorial {}!...'.format(num))
while c > 0:
    print('{}'.format(c), end='')
    print(' X ' if c > 1 else ' = {}'.format(result), end='')
    f *= c
    c -= 1


# -*- coding: utf-8 -*-
"""
Utility functions to convert the html body in e-mail friendly html
Actually it would be better to convert our html to an intermediate
structure (json) and template it. We'll do that later
"""
from bs4 import BeautifulSoup

from django.template import TemplateDoesNotExist
from django.template.loader import render_to_string

class HtmlConvert(object):
    def __init__(self, template_root):
        self.template_root = template_root

    def _clean(self, soup):
        empty_paras = soup.findAll(
            lambda tag: tag.name == 'p'
            and tag.find(True) is None
            and (tag.string is None or tag.string.strip() == '')
        )
        [empty_para.extract() for empty_para in empty_paras]

        # remove <br />
        # brs = soup.find_all('br')
        # [br.extract() for br in brs]

    def to_mail(self, body):
        soup = BeautifulSoup(body)
        self._clean(soup)
        return self._convert_html(soup).replace('\n', '')

    def _convert_html(self, soup_in):
        stack = ""
        try:
            if len(soup_in.find_all()):
                for child in soup_in.children:
                    stack += self._convert_html(child)
            return self._template(soup_in, stack)

        except AttributeError:
            return self._template(soup_in, stack)

    def _template(self, tag, stack):

        if not tag.name:
            return unicode(tag)

        string = stack if stack else tag.string
        try:
            context = {
                'tag': tag,
                'href': tag.get('href'),
                'title': tag.get('title', ''),
                'class': " ".join(tag.get('class', '')),
                'string': string,
            }
            template_name = "%s/%s.html" % (self.template_root, tag.name)
            return render_to_string(template_name, context).rstrip()

        except TemplateDoesNotExist:
            return unicode(string)


from datetime import datetime
import random
import importlib
from init import *
import pytz

def welcome(user, location, bot):
    hour = datetime.now(pytz.timezone('Europe/Moscow')).hour
    if hour > 20 or hour < 8:
        bot.send_message(user["chat_id"], "Администрация закрыта, приходите завтра. Вы перемещаетесь на улицу.")

        location = change_location_by_id(user, "street")
        try:
            location_module = importlib.import_module(location['file'])
            location_module.welcome(user, location, bot)
        except Exception as e:
            print(e)
    elif "dirty" in user['states']:
        bot.send_message(user["chat_id"], "Вы испачкались, в таком виде вас не пускают. Вы перемещаетесь на улицу.")

        location = change_location_by_id(user, "street")
        try:
            location_module = importlib.import_module(location['file'])
            location_module.welcome(user, location, bot)
        except Exception as e:
            print(e)
    else:
        bot.send_message(user['chat_id'], "👩💼 Вы в здании администрации.\n\n"
                                          "* /icecream - купить мороженое.")

def message(msg, user, location, neighbors, bot):
    hour = datetime.now(pytz.timezone('Europe/Moscow')).hour

    if "/icecream" in msg.text:
        if 'coin' in user['inventory']:
            bot.send_message(user["chat_id"], '🍦 В инвентарь добавлено одно мороженое.')
            user['inventory'].remove('coin')
            user['inventory'].append('icecream')
        else:
            bot.send_message(user["chat_id"], '👀 У вас нет монет, монет...')
    else:
        for neighbor in neighbors:
            if neighbor["chat_id"] != user["chat_id"]:
                bot.send_message(neighbor["chat_id"], "{}: {}".format(user["name"], msg.text))

def event(users, location, bot):
    hour = datetime.now(pytz.timezone('Europe/Moscow')).hour

    if hour > 20 or hour < 8:
        for user in users:
            bot.send_message(user['chat_id'], "🕜 Администрация закрывается, вас попросили выйти на улицу.")

            location = change_location_by_id(user, "street")
            try:
                location_module = importlib.import_module(location['file'])
                location_module.welcome(user, location, bot)
            except Exception as e:
                print(e)


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

class Node(object):

    def __init__(self, key, value, next_=None, prev=None):
        self.key = key
        self.value = value
        self.next = next_
        self.prev = prev

    def __repr__(self):
        return f'Node({self.key!r}, {self.value!r})'

class LRUCache:

    def __init__(self, capacity):
        """
        :type capacity: int
        """
        self.capacity = capacity
        self.head = Node('head', 'head')
        self.recorder = {}

    def get(self, key):
        """
        :type key: int
        :rtype: int
        """
        if key not in self.recorder:
            return -1

        node = self.recorder[key]
        node.prev.next = node.next
        if node.next is not None:
            node.next.prev = node.prev
        node.next = self.head.next
        if self.head.next is not None:
            self.head.next.prev = node
        self.head.next = node
        node.prev = self.head
        return node.value

    def put(self, key, value):
        """
        :type key: int
        :type value: int
        :rtype: void
        """
        if key in self.recorder:
            node = self.recorder[key]
            node.prev.next = node.next
            if node.next is not None:
                node.next.prev = node.prev
            del self.recorder[key]
        else:
            if self.capacity > 0:
                self.capacity -= 1
            else:
                move = self.head.next
                while move.next is not None:
                    move = move.next
                move.prev.next = None
                move.prev = None
                del self.recorder[move.key]

        node = Node(key, value, self.head.next, prev=self.head)
        if self.head.next is not None:
            self.head.next.prev = node
        self.head.next = node
        self.recorder[key] = node

    def __repr__(self):
        move = self.head.next
        result = ''
        while move is not None:
            result += str(move.key)
            move = move.next
        return result

def test():
    cache = LRUCache(2)
    cache.put(1, 1)
    cache.put(2, 2)
    assert cache.get(1) == 1
    cache.put(3, 3)
    assert cache.get(2) == -1
    cache.put(4, 4)
    assert cache.get(1) == -1
    assert cache.get(3) == 3
    assert cache.get(4) == 4

if __name__ == '__main__':
    test()


from contextlib import contextmanager

from alembic import op
from sqlalchemy.orm.session import Session

__all__ = ("session",)

@contextmanager
def session():
    bind = op.get_bind()
    session = Session(bind)
    try:
        yield session
    except Exception:
        session.rollback()
        raise
    else:
        session.commit()


import os
import audio_converter as aconv

foldername_data   = 'recorded_data'

dir_root         = os.path.dirname(os.path.realpath(__file__))
dir_unprocessed  = dir_root + '/' +foldername_data
dir_processed    = dir_unprocessed + '_processed'

list_unprocessed = os.listdir(dir_unprocessed)
list_processed   = os.listdir(dir_processed)

for data_unprocessed in list_unprocessed :
    if data_unprocessed not in list_processed :

        convert('C:/Users/junhy/Documents/2019/vsCodeTest/DenoisingModule/recorded_data/', 'voice_013')
        aconv.convert(dir_root, dir_unprocessed, dir_processed, data_unprocessed)


import warnings

from toga.handlers import wrapped_handler
from toga.sources import TreeSource
from toga.sources.accessors import build_accessors

from .base import Widget

class Tree(Widget):
    """Tree Widget.

    :param headings: The list of headings for the interface.
    :param id:  An identifier for this widget.
    :param style: An optional style object. If no style is provided then a new
        one will be created for the widget.
    :param data: The data to display in the widget. Can be an instance of
        :class:`~toga.sources.TreeSource`, a list, dict or tuple with data to
        display in the tree widget, or a class instance which implements the
        interface of :class:`~toga.sources.TreeSource`. Entries can be:

          - any Python object ``value`` with a string representation. This
            string will be shown in the widget. If ``value`` has an attribute
            ``icon``, instance of (:class:`~toga.Icon`), the icon will be
            shown in front of the text.

          - a tuple ``(icon, value)`` where again the string representation of
            ``value`` will be used as text.

    :param accessors: Optional; a list of attributes to access the value in the
        columns. If not given, the headings will be taken.
    :param multiple_select: Boolean; if ``True``, allows for the selection of
        multiple rows. Defaults to ``False``.
    :param on_select: A handler to be invoked when the user selects one or
        multiple rows.
    :param on_double_click: A handler to be invoked when the user double clicks
        a row.
    :param missing_value: value for replacing a missing value in the data
            source. (Default: None). When 'None', a warning message will be
            shown.
    """

    MIN_WIDTH = 100
    MIN_HEIGHT = 100

    def __init__(
        self,
        headings,
        id=None,
        style=None,
        data=None,
        accessors=None,
        multiple_select=False,
        on_select=None,
        on_double_click=None,
        missing_value=None,
        factory=None,  # DEPRECATED!
    ):
        super().__init__(id=id, style=style)
        ######################################################################
        # 2022-09: Backwards compatibility
        ######################################################################
        # factory no longer used
        if factory:
            warnings.warn("The factory argument is no longer used.", DeprecationWarning)
        ######################################################################
        # End backwards compatibility.
        ######################################################################

        self.headings = headings
        self._accessors = build_accessors(headings, accessors)
        self._multiple_select = multiple_select
        self._data = None
        if missing_value is None:
            print(
                "WARNING: Using empty string for missing value in data. "
                "Define a 'missing_value' on the table to silence this message"
            )
        self._missing_value = missing_value or ""

        self._on_select = None
        self._on_double_click = None

        self._impl = self.factory.Tree(interface=self)
        self.data = data

        self.on_select = on_select
        self.on_double_click = on_double_click

    @property
    def data(self):
        """
        :returns: The data source of the tree
        """
        return self._data

    @data.setter
    def data(self, data):
        """Set the data source of the data.

        :param data: Data source
        :type  data: ``dict`` or ``class``
        """
        if data is None:
            self._data = TreeSource(accessors=self._accessors, data=[])
        elif isinstance(data, (list, tuple, dict)):
            self._data = TreeSource(accessors=self._accessors, data=data)
        else:
            self._data = data

        self._data.add_listener(self._impl)
        self._impl.change_source(source=self._data)

    @property
    def multiple_select(self):
        """Does the table allow multiple rows to be selected?"""
        return self._multiple_select

    @property
    def selection(self):
        """The current selection of the table.

        A value of None indicates no selection. If the tree allows multiple selection,
        returns a list of selected data nodes. Otherwise, returns a single data node.
        """
        return self._impl.get_selection()

    @property
    def missing_value(self):
        return self._missing_value

    @property
    def on_select(self):
        """The callable function for when a node on the Tree is selected. The
        provided callback function has to accept two arguments tree
        (:obj:`Tree`) and node (``Node`` or ``None``).

        :rtype: ``callable``
        """
        return self._on_select

    @on_select.setter
    def on_select(self, handler):
        """Set the function to be executed on node select.

        :param handler:     callback function
        :type handler:      ``callable``
        """
        self._on_select = wrapped_handler(self, handler)
        self._impl.set_on_select(self._on_select)

    @property
    def on_double_click(self):
        """The callable function for when a node on the Tree is selected. The
        provided callback function has to accept two arguments tree
        (:obj:`Tree`) and node (``Node`` or ``None``).

        :rtype: ``callable``
        """
        return self._on_double_click

    @on_double_click.setter
    def on_double_click(self, handler):
        """Set the function to be executed on node double click.

        :param handler:     callback function
        :type handler:      ``callable``
        """
        self._on_double_click = wrapped_handler(self, handler)
        self._impl.set_on_double_click(self._on_double_click)


"""
给定一个整数数组，判断是否存在重复元素。

如果任何值在数组中出现至少两次，函数返回 true。如果数组中每个元素都不相同，则返回 false。

示例 1:

输入: [1,2,3,1]
输出: true
示例 2:

输入: [1,2,3,4]
输出: false
示例 3:

输入: [1,1,1,3,3,4,3,2,4,2]
输出: true

来源：力扣（LeetCode）
链接：https://leetcode-cn.com/problems/contains-duplicate
著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
"""

class Solution(object):
    def containsDuplicate(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        # 1 比较长度,有重复的去重后list长度一定小于原list长度
        # return len(nums) != len(set(nums))
        # 2 排序，排序后存在前后相同的元素，肯定重复
        # data = self.bubble_sort(nums)
        # for num in range(len(data) - 1):
        #     if data[num] == data[num + 1]:
        #         return True
        # return False
        # 3 用dict,很容易想到，类似于hash,可惜超时了。。。
        dict = {}
        for i in nums:
            if i in dict.keys():
                if dict[i] >= 2:
                    return True
                else:
                    dict[i] += 1
            else:
                dict[i] = 1

    # 写个简单的插入排序吧 QAQ
    def bubble_sort(self, nums):
        for i in range(len(nums) - 1):
            for j in range(i+1, len(nums)):
                if nums[i] > nums[j]:
                    nums[i], nums[j] = nums[j], nums[i]
        return nums

if __name__ == '__main__':
    nums = [1, 2, 3, 1, 45, 4656, 1, 34, 454, ]

    # print(list(set(nums)))
    solution = Solution()
    print(solution.containsDuplicate(nums))


#!python

from __future__ import division, print_function  # Python 2 and 3 compatibility

class Tuplegram(list):
	"""
		Tuplegram is a histogram implemented as a subclass of the list type.
	"""

	def __init__(self, word_list=None):
		"""
			Initialize this histogram as a new list and count given words.
		"""
		super(Tuplegram, self).__init__()  # Initialize this as a new list
		# Add properties to track useful word counts for this histogram
		self.types = 0  # Count of distinct word types in this histogram
		self.tokens = 0  # Total count of all word tokens in this histogram
		# Count words in given list, if any
		print("SELF:")
		print(self)
		print(word_list)
		if word_list is not None:
			for word in word_list:
				self.add_count(word)

	def add_count(self, word, count=1):
		"""
			Increase frequency count of given word by given count amount.
		"""
		# TODO: Increase word frequency by count
		self.tokens += count
		index = 0
		for item in self:
			index += 1
			if item[0] == word:
				newItem = tuple([item[0], item[1]+count])
				self.remove(item)
				self.append(newItem)
				break
		else:
			self.types += 1
			self.append(tuple([word,count]))

	def frequency(self, word):
		"""
			Return frequency count of given word, or 0 if word is not found.
		"""
		# Retrieve word frequency count
		for pairing in self:
			if pairing[0] == word:
				return pairing[1]
		else:
			return 0

	def __contains__(self, word):
		"""
			Return boolean indicating if given word is in this histogram.
		"""
		# Check if word is in this histogram
		for pairing in self:
			if pairing[0] == word:
				return True
		else:
			return False

	def _index(self, word):
		"""
			Return the index of entry containing given word if found
			in this histogram, or None if given word is not found.
		"""
		# Implement linear search to find index of entry with given word
		index = 0
		for pairing in self:
			if pairing[0] == word:
				return index
			else:
				index += 1
		else:
			return None

def print_histogram(word_list):
	print('word list: {}'.format(word_list))
	# Create a tuplegram and display its contents
	histogram = Tuplegram(word_list)
	print('tuplegram: {}'.format(histogram))
	print('{} tokens, {} types'.format(histogram.tokens, histogram.types))
	for array in histogram:
		word = array[0]
		freq = array[1]
		print('{!r} occurs {} times'.format(word, freq))
	print()

def main():
	import sys
	arguments = sys.argv[1:]  # Exclude script name in first argument
	if len(arguments) >= 1:
		# Test histogram on given arguments
		print_histogram(arguments)
	else:
		# Test histogram on letters in a word
		word = 'abracadabra'
		print_histogram(list(word))
		# Test histogram on words in a classic book title
		fish_text = 'one fish two fish red fish blue fish'
		print_histogram(fish_text.split())
		# Test histogram on words in a long repetitive sentence
		woodchuck_text = ('how much wood would a wood chuck chuck\nif a wood chuck could chuck wood')
		print_histogram(woodchuck_text.split())

if __name__ == '__main__':
	main()


""""
///////////////////////////////////////////////////////////////////////
//                                                                   //
//                             Vslam        v1                       //
//                        Luis  Lujano, 13-10775                     //
//                       Jaime Villegas, 13-11493                    //
///////////////////////////////////////////////////////////////////////
"""

import cv2
import numpy as np
import matplotlib.pyplot as plt
plt.ion()

def thresholdTrans(img, thresholdy, w, h): # verifica si el punto se encuentra en una ventana dada
    imgy = img[:,1]
    # print(w)
    # print(h)
    # print("sd")

    valid = (imgy <(h-thresholdy)) &(imgy > thresholdy) # se asumen traslaciones en y
    index = np.where(valid <= 0)
    imgPoints = np.delete(img, index, 0)

    return imgPoints

def thresholdRot(img, thresholdx, w, h): # verifica si el punto se encuentra en una ventana dada

    imgx = img[:,0]
    # print(w)
    # print(h)
    # print("sd")

    valid = (imgx <(w-thresholdx)) & (imgx > thresholdx) # se asumen rotaciones en direccion x
    index = np.where(valid <= 0)
    imgPoints = np.delete(img, index, 0)

    return imgPoints

#Stanford deep learning, berkley
fIdx = 500
dataset = '../../../Datasets/00/00.txt.d/camera_left.image_raw_'
# dataset = '../ressources/kitti/odometry/02/image_2'  # OR use that database

import odotools as odotools
odotools.read_poses('../../../Datasets/00/', "00_gt", fIdx)
gt_poses = np.zeros((0, 2), dtype=np.float)  # (x, y) relative camera coordinates

poses = np.zeros((0, 2), dtype=np.float)  # (x, y) relative camera coordinates
R_p, t_p = None, None

while True:
    im0_path = '%s%08d.pgm' % (dataset, fIdx)
    im1_path = '%s%08d.pgm' % (dataset, fIdx+1)
    im1 = cv2.imread(im0_path)
    im2 = cv2.imread(im1_path)
    if fIdx == 500:
        plt.waitforbuttonpress()
    if im1 is None:
        raise Exception("File %s does not exist" % im0_path)
    if im2 is None:
        raise Exception("File %s does not exist" % im1_path)

    # Convert im1 and im2 to GRAY.
    im1GRAY = cv2.cvtColor(im1, cv2.COLOR_BGR2GRAY)
    im2GRAY = cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY)

    # Detect features with:
    fast = cv2.FastFeatureDetector_create(threshold=50, nonmaxSuppression=True) # crear detector de features con fast
    im1KPts = fast.detect(im1GRAY, None)
    #im1KPts = thresholdTrans(im1KPts, 20)

    # Draw keypoints
    im1KPtsOut = im1.copy() #
    cv2.drawKeypoints(im1GRAY, im1KPts, outImage=im1KPtsOut, color=(0, 0, 255))

    im1KPts_means = cv2.KeyPoint_convert(im1KPts) # puntos en formato (x, y) en la imagen 1

    #    plt.scatter(im1KPts_means[:, 0], 1KPts_means[:, 1], marker='+', s=100)

    w1, h1 = im1.shape[1], im1.shape[0]

    im1KPts_means = thresholdRot(im1KPts_means, 40, w1, h1)
    im1KPts_means = thresholdTrans(im1KPts_means, 50, w1, h1)

    # Track the keypoints in other frame
    criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 40, 0.001)## 40, 0.001
    im2KPts_means, im2KPts_status, im2KPts_err = cv2.calcOpticalFlowPyrLK(im1, im2, im1KPts_means, None, winSize=(21, 21), maxLevel=3, criteria=criteria, minEigThreshold=0.001)

    # Keep only valid points
    im2KPts_status = im2KPts_status.astype(np.bool).ravel()

    # im2valid = thresholdTrans(im2KPts_means, 20)
    # im2KPts_status = im2valid & im2KPts_status

    im2KPts_ok = im2KPts_means[im2KPts_status]
    im1KPts_ok = im1KPts_means[im2KPts_status]

    colRands = np.random.randint(0,255, (100, 3)) # dibujar match entre puntos img1, img2
    im1KltOut = im1.copy()
    for i, (new, old) in enumerate(zip(im2KPts_ok, im1KPts_ok)):
        a, b = new.ravel()
        c, d = old.ravel()
        im1KltOut = cv2.line(im1KltOut, (a, b), (c, d), (0, 0, 255), 1)
        im1KltOut = cv2.circle(im1KltOut, (a, b), 5, colRands[i%len(colRands)].tolist(), -1)
    # img = cv2.add(frame, mask)

    # Use the calibration matrix and keypoints matching to compute the essential matrix
    camMatrix = np.array([[7.188560000000e+02, 0.000000000000e+00, 6.071928000000e+02, 4.538225000000e+01],
                          [0.000000000000e+00, 7.188560000000e+02, 1.852157000000e+02, 1.130887000000e-01],
                          [0.000000000000e+00, 0.000000000000e+00, 1.000000000000e+00, 3.779761000000e-03]])

    fx = camMatrix[0][0]
    fy = camMatrix[1][1]
    focal= fx # TODO: Focal length in pixel
    x = camMatrix[0][2]
    y = camMatrix[1][2]

    pp = [x,y]  # TODO: Set the center point [x, y]

    E, mask = cv2.findEssentialMat(im2KPts_ok, im1KPts_ok, focal = focal, pp=(pp[0], pp[1]), method=cv2.RANSAC, prob=0.999, threshold=1.0)
    points, R, t, mask = cv2.recoverPose(E, im1KPts_ok, im2KPts_ok)
    print("Matrix R = {}".format(R))
    print("Matrix t = {}".format(t))
    print("Puntos detectados ={}".format(np.size(im1KPts)))
    print("puntos bajo analisis ={}".format(np.size(im1KPts_means)/2))
    print("Puntos emperejados ={}".format(np.size(im1KPts_ok)/2))
    t = abs(t)

    # Display the figures
    plt.figure(1)
    plt.clf()
    plt.subplot(3, 1, 1)
    plt.imshow(im1KPtsOut[..., ::-1])
    plt.subplot(3, 1, 2)
    plt.imshow(im1KltOut[..., ::-1])
    plt.subplot(3, 1, 3)
    plt.imshow(im2[..., ::-1])

    # Compute R, t from absolute scale and essential matrix
    scale = odotools.getAbsoluteScale(len(poses)+1)

    if t_p is None:
        t_p = np.zeros_like(t)
        R_p = np.asmatrix(np.identity(3))
        R2 = R_p

    t_p = t_p + scale * (R_p * t)
    #
    # R2 = R_p
    R_p = R * R_p
    #
    # senod = R2[0,1]
    # senod2 = R_p[0,1]

    #print(R_p)
    #print(R2)

    # if np.sign(senod) != np.sign(senod2):
    #     t_p = abs(t_p)
    #     print("hola")

    # Stack up the poses

    poses = np.vstack([poses, [t_p[0, 0], t_p[2, 0]]])  # x, y
    gt_poses = odotools.gt_poses[1:len(poses)+1, 0:2]

    # Compute errors
    # odo_dist = np.sqrt(np.sum(np.diff(poses, axis=0)**2))
    t_error = np.mean(np.sqrt(np.sum((gt_poses - poses)**2, axis=1)))

    # Plot trajectory and GT
    plt.figure(2)
    plt.clf()
    plt.plot(poses[:, 0], poses[:, 1], marker='o', c='b', label="Odometry")
    plt.plot(gt_poses[:, 0], gt_poses[:, 1], marker='s', c='g', label="GroundTruth")
    plt.axis('equal')
    plt.title("Frame %d\nCum. err. %.2fm" % (fIdx, t_error))
    plt.legend()

    plt.draw()
    plt.waitforbuttonpress(0.02)

    fIdx += 1


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

from cmyui import log, Ansi
from quart import Blueprint, request, jsonify

from objects import glob
from objects.utils import convert_mode_int, get_safe_name

__all__ = ()

api = Blueprint('api', __name__)

""" valid modes, mods, sorts """
valid_modes = frozenset({'std', 'taiko', 'catch', 'mania'})
valid_mods = frozenset({'vn', 'rx', 'ap'})
valid_sorts = frozenset({'tscore', 'rscore', 'pp', 'plays',
                        'playtime', 'acc', 'maxcombo'})

""" /get_leaderboard """
@api.route('/get_leaderboard') # GET
async def get_leaderboard():
    mode = request.args.get('mode', default='std', type=str)
    mods = request.args.get('mods', default='vn', type=str)
    sort_by = request.args.get('sort', default='pp', type=str)
    country = request.args.get('country', default=None, type=str)
    page = request.args.get('page', default=0, type=int)

    if mode not in valid_modes:
        return b'invalid mode! (std, taiko, catch, mania)'

    if mods not in valid_mods:
        return b'invalid mods! (vn, rx, ap)'

    if country is not None and len(country) != 2:
        return b'invalid country!'

    if sort_by not in valid_sorts:
        return b'invalid sort param!'

    q = ['SELECT u.id user_id, u.name username, '
        'u.country, tscore_{0}_{1} tscore, '
        'rscore_{0}_{1} rscore, pp_{0}_{1} pp, '
        'plays_{0}_{1} plays, playtime_{0}_{1} playtime, '
        'acc_{0}_{1} acc, maxcombo_{0}_{1} maxcombo FROM stats '
        'JOIN users u ON stats.id = u.id '
        'WHERE pp_{0}_{1} > 0 AND u.priv >= 3'.format(mods, mode)]

    args = []

    if country is not None:
        q.append('AND u.country = %s')
        args.append(country)

    # TODO: maybe cache total num of scores in the db to get a
    # rough estimate on what is a ridiculous page for a request?
    q.append(f'ORDER BY {sort_by}_{mods}_{mode} DESC '
            'LIMIT 50 OFFSET %s')
    args.append(page * 50)

    if glob.config.debug:
        log(' '.join(q), Ansi.LGREEN)
    res = await glob.db.fetchall(' '.join(q), args)
    return jsonify(res) if res else b'{}'

""" /get_user """
@api.route('/get_user') # GET
async def get_user():
    # get request args
    id = request.args.get('id', type=int)
    name = request.args.get('name', type=str)

    # check if required parameters are met
    if not name and not id:
        return b'missing parameters! (id or name)'

    # fetch user info and stats
    # user info
    q = ['SELECT u.id user_id, u.name username, u.safe_name username_safe, u.country, u.priv privileges, '
        'u.silence_end, u.donor_end, u.creation_time, u.latest_activity, u.clan_id, u.clan_rank, '

        # total score
        'tscore_vn_std, tscore_vn_taiko, tscore_vn_catch, tscore_vn_mania, '
        'tscore_rx_std, tscore_rx_taiko, tscore_rx_catch, '
        'tscore_ap_std, '

        # ranked score
        'rscore_vn_std, rscore_vn_taiko, rscore_vn_catch, rscore_vn_mania, '
        'rscore_rx_std, rscore_rx_taiko, rscore_rx_catch, '
        'rscore_ap_std, '

        # pp
        'pp_vn_std, pp_vn_taiko, pp_vn_catch, pp_vn_mania, '
        'pp_rx_std, pp_rx_taiko, pp_rx_catch, '
        'pp_ap_std, '

        # plays
        'plays_vn_std, plays_vn_taiko, plays_vn_catch, plays_vn_mania, '
        'plays_rx_std, plays_rx_taiko, plays_rx_catch, '
        'plays_ap_std, '

        # playtime
        'playtime_vn_std, playtime_vn_taiko, playtime_vn_catch, playtime_vn_mania, '
        'playtime_rx_std, playtime_rx_taiko, playtime_rx_catch, '
        'playtime_ap_std, '

        # accuracy
        'acc_vn_std, acc_vn_taiko, acc_vn_catch, acc_vn_mania, '
        'acc_rx_std, acc_rx_taiko, acc_rx_catch, '
        'acc_ap_std, '

        # maximum combo
        'maxcombo_vn_std, maxcombo_vn_taiko, maxcombo_vn_catch, maxcombo_vn_mania, '
        'maxcombo_rx_std, maxcombo_rx_taiko, maxcombo_rx_catch, '
        'maxcombo_ap_std '

        # join users
        'FROM stats JOIN users u ON stats.id = u.id']

    # achivement
    q2 = ['''
    SELECT userid, achid FROM user_achievements ua
        INNER JOIN users u ON u.id = ua.userid
    ''']

    # argumnts
    args = []

    # append request arguments (id or name)
    if id:
        q.append('WHERE u.id = %s')
        q2.append('WHERE u.id = %s')
        args.append(id)
    elif name:
        q.append('WHERE u.safe_name = %s')
        q2.append('WHERE u.safe_name = %s')
        args.append(get_safe_name(name))

    q2.append('ORDER BY ua.achid ASC')

    if glob.config.debug:
        log(' '.join(q), Ansi.LGREEN)
    res = await glob.db.fetchall(' '.join(q), args)
    res_ach = await glob.db.fetchall(' '.join(q2), args)
    return jsonify(udata=res,achivement=res_ach) if res else b'{}'

""" /get_scores """
@api.route('/get_scores') # GET
async def get_scores():
    # get request args
    id = request.args.get('id', type=int)
    mode = request.args.get('mode', type=str)
    mods = request.args.get('mods', type=str)
    sort = request.args.get('sort', type=str)
    limit = request.args.get('limit', type=int)

    # check if required parameters are met
    if not id:
        return b'missing parameters! (id)'

    if sort == 'recent':
        sort = 'id'
    elif sort == 'best':
        sort = 'pp'
    else:
        return b'invalid sort! (recent or best)'

    if mods not in valid_mods:
        return b'invalid mods! (vn, rx, ap)'

    if mode == 'std':
        mode = 0
    elif mode == 'taiko':
        mode = 1
    elif mode == 'catch':
        mode = 2
    elif mode == 'mania':
        mode = 3
    else:
        return b'wrong mode type! (std, taiko, catch, mania)'

    if not limit:
        limit = 50

    # fetch scores
    q = [f'SELECT scores_{mods}.*, maps.* '
        f'FROM scores_{mods} JOIN maps ON scores_{mods}.map_md5 = maps.md5']
    q2 = [f'SELECT COUNT(scores_{mods}.id) AS result '
        f'FROM scores_{mods} JOIN maps ON scores_{mods}.map_md5 = maps.md5']

    # argumnts
    args = []

    q.append(f'WHERE scores_{mods}.userid = %s '
            f'AND scores_{mods}.mode = {mode} '
            f'AND maps.status = 2')
    q2.append(f'WHERE scores_{mods}.userid = %s '
            f'AND scores_{mods}.mode = {mode}')
    if sort == 'pp':
        q.append(f'AND scores_{mods}.status = 2')
        q2.append(f'AND scores_{mods}.status = 2')
    q.append(f'ORDER BY scores_{mods}.{sort} DESC '
            f'LIMIT {limit}')
    args.append(id)

    if glob.config.debug:
        log(' '.join(q), Ansi.LGREEN)
        log(' '.join(q2), Ansi.LGREEN)
    res = await glob.db.fetchall(' '.join(q), args)
    limit = await glob.db.fetch(' '.join(q2), args)
    return jsonify(scores=res, limit=limit['result']) if res else jsonify(scores=[], limit=limit['result'])

""" /get_most_beatmaps """
@api.route('/get_most_beatmaps') # GET
async def get_most_beatmaps():
    # get request args
    id = request.args.get('id', type=int)
    mode = request.args.get('mode', type=str)
    mods = request.args.get('mods', type=str)
    limit = request.args.get('limit', type=int)

    # check if required parameters are met
    if not id:
        return b'missing parameters! (id)'

    if mods not in valid_mods:
        return b'invalid mods! (vn, rx, ap)'

    if mode == 'std':
        mode = 0
    elif mode == 'taiko':
        mode = 1
    elif mode == 'catch':
        mode = 2
    elif mode == 'mania':
        mode = 3
    else:
        return b'wrong mode type! (std, taiko, catch, mania)'

    if not limit:
        limit = 50

    # fetch scores
    q = [f'SELECT scores_{mods}.mode, scores_{mods}.map_md5, maps.artist, maps.title, maps.set_id, maps.creator, COUNT(*) AS `count` '
        f'FROM scores_{mods} JOIN maps ON scores_{mods}.map_md5 = maps.md5']

    # argumnts
    args = []

    q.append(f'WHERE userid = %s AND scores_{mods}.mode = {mode} GROUP BY map_md5')
    q.append(f'ORDER BY COUNT DESC '
            f'LIMIT {limit}')
    args.append(id)

    if glob.config.debug:
        log(' '.join(q), Ansi.LGREEN)
    res = await glob.db.fetchall(' '.join(q), args)
    return jsonify(maps=res) if res else jsonify(maps=[])

""" /get_replay """
@api.route('/get_replay') # GET
async def get_replay():
    id = request.args.get('id', type=int)
    mods = request.args.get('mods', type=str)

    # check if required parameters are met
    if not id:
        return b'missing parameters! (id)'

    if mods not in valid_mods:
        return b'invalid mods! (vn, rx, ap)'

    # fetch scores
    q = ['SELECT scores_{0}.*, maps.*, users.name FROM scores_{0}'.format(mods)]

    args = []

    q.append(f'JOIN maps ON scores_{mods}.map_md5 = maps.md5')
    q.append(f'JOIN users ON scores_{mods}.userid = users.id')
    q.append(f'WHERE scores_{mods}.id = %s')
    args.append(id)

    if glob.config.debug:
        log(' '.join(q), Ansi.LGREEN)
    res = await glob.db.fetch(' '.join(q), args)
    return jsonify(res) if res else b'{}'

""" /get_online """
@api.route('/get_online') # GET
async def get_online():
    # TODO: fetch from gulag
    NotImplemented

    return b'{"online": 0}'


"""
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

"""

import os

#Used in subsequent imports from params
from resource_management.libraries.script.script import Script
from resource_management.libraries.functions.default import default
from resource_management.libraries.functions.format import format
from install_params import exclude_packages
from status_params import *

config = Script.get_config()
hadoop_conf_dir = None
hbase_conf_dir = None
hadoop_home = None
try:
  hadoop_conf_dir = os.environ["HADOOP_CONF_DIR"]
  hbase_conf_dir = os.environ["HBASE_CONF_DIR"]
  hadoop_home = os.environ["HADOOP_HOME"]
except:
  pass
#directories & files
dfs_name_dir = config['configurations']['hdfs-site']['dfs.namenode.name.dir']
fs_checkpoint_dir = config['configurations']['hdfs-site']['dfs.namenode.checkpoint.dir']
dfs_data_dir = config['configurations']['hdfs-site']['dfs.datanode.data.dir']
#decomission
hdfs_exclude_file = default("/clusterHostInfo/decom_dn_hosts", [])
exclude_file_path = config['configurations']['hdfs-site']['dfs.hosts.exclude']
include_file_path = default("/configurations/hdfs-site/dfs.hosts", None)
hdfs_include_file = None
manage_include_files = default("/configurations/hdfs-site/manage.include.files", False)
if include_file_path and manage_include_files:
  slave_hosts = default("/clusterHostInfo/slave_hosts", [])
  hdfs_include_file = slave_hosts
update_files_only = default("/commandParams/update_files_only",False)
# HDFS High Availability properties
dfs_ha_enabled = False
dfs_ha_nameservices = default("/configurations/hdfs-site/dfs.internal.nameservices", None)
dfs_ha_namenode_ids = default(format("/configurations/hdfs-site/dfs.ha.namenodes.{dfs_ha_nameservices}"), None)

namenode_id = None
namenode_rpc = None
hostname = config['agentLevelParams']['hostname']
if dfs_ha_namenode_ids:
  dfs_ha_namemodes_ids_list = dfs_ha_namenode_ids.split(",")
  dfs_ha_namenode_ids_array_len = len(dfs_ha_namemodes_ids_list)
  if dfs_ha_namenode_ids_array_len > 1:
    dfs_ha_enabled = True
if dfs_ha_enabled:
  for nn_id in dfs_ha_namemodes_ids_list:
    nn_host = config['configurations']['hdfs-site'][format('dfs.namenode.rpc-address.{dfs_ha_nameservices}.{nn_id}')]
    if hostname.lower() in nn_host.lower():
      namenode_id = nn_id
      namenode_rpc = nn_host

hadoop_user = config["configurations"]["cluster-env"]["hadoop.user.name"]
hdfs_user = hadoop_user

grep_exe = "findstr"

name_node_params = default("/commandParams/namenode", None)

service_map = {
  "datanode" : datanode_win_service_name,
  "journalnode" : journalnode_win_service_name,
  "namenode" : namenode_win_service_name,
  "secondarynamenode" : snamenode_win_service_name,
  "zkfc_slave": zkfc_win_service_name
}


import warnings
import theano
import numpy

import theano.tensor as T

from layer import Layer
from utils.model.layer_utils import setupDefaultLayerOptions

__all__ = ["BatchStandardizeLayer"]

#-------------------------------------------------------------------------------
# Begin BatchNormLayer
class BatchStandardizeLayer(Layer):
    def __init__(self):
        super(BatchStandardizeLayer, self).__init__()
        self.layerType='BStandardLayer'

    def constructLayer(self, inputShape, initParams, name,
                       **layerSpecs):
        self.layerName = name

        self.inputShape = inputShape
        self.outputShape = inputShape

    def fprop(self, x, isTest=False):

        ret = x
        if not isTest:
            norm_axis = (1,)+tuple(range(2,len(self.inputShape)))
            x_avg = T.mean(x, axis=norm_axis, keepdims=True)
            x_std = T.std(x, axis=norm_axis, keepdims=True)
            ret = (x-x_avg)/(x_std+1e-4)

        return ret

# End BatchNormLayer
#-------------------------------------------------------------------------------


from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='acr-home'),
    path('about/', views.about, name='about-us'),
    path('research/', views.research, name='research'),
    path('team/', views.team, name='team'),
    path('partnership/', views.partnership, name='partnership'),
    path('csi-jump/', views.csi_jump, name='csi-jump'),
    path('lifelinesnets-es/', views.lln_es, name='lln-es'),
    path('lifelinesnets-tr/', views.lln_tr, name='lln-tr'),
    path('smart-grid/', views.smart_grid, name='smart-grid'),
    path('iec-standards-conformity/',
         views.iec_stds_conformity, name='iec-stds-conform'),
    path('acfta-iec-standards-ecosystem',
         views.acfta_iec_stds_eco, name='acfta-iec-stds-eco'),
    path('integrated-lifelines', views.illie_dp, name='illie-dp'),
]


def threeSum(nums):
    def twoSum(l, k):
        l2 = []
        L = 0
        R = len(l)-1
        while L < R:
            s = l[L] + l[R]
            if s == k:
                l2.append( [ l[L], l[R] ] )
                while l[L] + l[R] == k and L < R:
                    L += 1
            elif s < k:
                L += 1
            else:
                R -= 1
        return l2

    nums = sorted(nums)
    l3 = []
    for i in range(len(nums) - 2):
        if i != 0 and nums[i] == nums[i-1]:
            continue
        a = nums[i]
        l2 = twoSum(nums[i+1:], -a)
        for pair in l2:
            l3.append([a, *pair])

    sl3 = [ tuple( sorted(x) ) for x in l3 ]
    l3Set = set(sl3)
    l3 = list(l3Set)
    l3 = [list(x) for x in l3]

    return l3


import re

max_val = -1
max_len = -1
longest_chain = ''

def solve24():
    components_list = open('inputs/day24.txt').read().split('\n')
    components = [(int(c.split('/')[0]), int(c.split('/')[1])) for c in components_list]
    build('0', components)
    print('Task 1:', max_val)
    print('Task 2:', sum([int(v) for v in re.findall('[0-9]+', longest_chain)]))

def build(chain, components):
    last = int(chain.split('/')[-1])
    for i in range(len(components)):
        component = components[i]
        if component.__contains__(last):
            if component[1] == last:
                component = (component[1], component[0])
            build(chain+'--'+to_component(component), components[:i]+components[i+1:])
    update_max(chain)
    update_longest(chain)

def to_component(c):
    return str(c[0])+'/'+str(c[1])

def update_max(chain):
    global max_val
    val = sum([int(v) for v in re.findall('[0-9]+', chain)])
    max_val = max(max_val, val)

def update_longest(chain):
    global max_len
    global longest_chain
    if chain.count('--') > max_len:
        max_len = chain.count('--')
        longest_chain = chain
    elif chain.count('--') == max_len:
        if sum([int(v) for v in re.findall('[0-9]+', chain)]) > sum([int(v) for v in re.findall('[0-9]+', longest_chain)]):
            longest_chain = chain


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

# Create a new window and set the dimensions
window = tk.Tk()
window.geometry("600x400")

# Set the background color of the window
window.config(bg="#065569")

window.resizable(width=False,height=False)

# Window's Title
window.title('Number Guessing Game')

#Create random variable
TARGET = random.randint(0, 1000)
RETRIES = 0

def update_result(text):
    result.configure(text=text)

# Creating a game
def new_game():
    guess_button.config(state='normal')
    global TARGET, RETRIES
    TARGET = random.randint(0, 1000)
    RETRIES = 0
    update_result(text="Guess a number between\n 1 and 1000")

def play_game():
    global RETRIES

    choice = int(number_form.get())

    if choice != TARGET:
        RETRIES += 1

        result = "Wrong Guess!! Try Again"
        if TARGET < choice:
            hint = "The required number lies between 0 and {}".format(result)
        else:
            hint = "The required number lies between {} and 1000".format(choice)
        result += "\n\nHINT :\n" + hint

    else:
        result = "You guessed the correct number after {} retries".format(RETRIES)
        guess_button.configure(state='disabled')
        result += "\n" + "Click on Play to start a new game"

    update_result(result)

title = tk.Label(window,text="Guessing Game",font=("Arial",24),fg="#fffcbd",bg="#065569")

result = tk.Label(window, text="Click on Play to start a new game", font=("Arial", 12, "normal", "italic"),fg = "White", bg="#065569", justify=tk.LEFT)

play_button = tk.Button(window, text="Play Game", font=("Arial", 14, "bold"), fg = "Black", bg="#29c70a", command=new_game)

guess_button = tk.Button(window,text="Guess",font=("Arial",13), state='disabled', fg="#13d675",bg="Black", command=play_game)

# Exit Button
exit_button = tk.Button(window,text="Exit Game",font=("Arial",14), fg="White", bg="#b82741", command=exit)

# Entry Fields
guessed_number = tk.StringVar()
number_form = tk.Entry(window,font=("Arial",11),textvariable=guessed_number)

# Place the labels

title.place(x=170, y=50)
result.place(x=180, y=210)

# Place the buttons
exit_button.place(x=300,y=320)
guess_button.place(x=350, y=147)
play_button.place(x=170, y=320)

# Place the entry field
number_form.place(x=180, y=150)

# Start the window
window.mainloop()