from utils_py.tree import *

class Solution:
    def hasPathSum(self, root: TreeNode, sum: int) -> bool:
        if not root:
            return False
        if not root.left and not root.right:
            return sum == root.val

        remain = sum - root.val
        return self.hasPathSum(root.left, remain) or self.hasPathSum(root.right, remain)

def test(test_name, root, sum, expected):
    res = Solution().hasPathSum(root, sum)
    if res == expected:
        print(test_name + ' success.')
    else:
        print(test_name + ' failed.')

if __name__ == "__main__":
    #        5
    #       / \
    #      4   8
    #     /   / \
    #    11  13  4
    #   /  \      \
    #  7    2      1
    root1 = TreeNode(5);
    root1.left = TreeNode(4);
    root1.right = TreeNode(8);
    root1.left.left = TreeNode(11);
    root1.left.left.left = TreeNode(7);
    root1.left.left.right = TreeNode(2);
    root1.right.left = TreeNode(13);
    root1.right.right = TreeNode(4);
    root1.right.right.right = TreeNode(1);
    sum1 = 22;
    expected1 = True;
    test("test1", root1, sum1, expected1);


import torch
import torch.nn as nn

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.lstm1 = nn.LSTMCell(2, 256)
        self.lstm2 = nn.LSTMCell(256, 256)
        self.linear = nn.Linear(256, 2)

    def forward(self, inputs, future = 0):
        """
        inputs: (B, T, 2)
        """
        h_t = torch.zeros(inputs.size(0), 256, dtype=torch.float)
        c_t = torch.zeros(inputs.size(0), 256, dtype=torch.float)
        h_t2 = torch.zeros(inputs.size(0), 256, dtype=torch.float)
        c_t2 = torch.zeros(inputs.size(0), 256, dtype=torch.float)

        inputs = inputs.permute(1,0,2)
        outputs = [torch.zeros_like(inputs[0])] # 処理の都合上のダミーを入れておく
        for i, input_t in enumerate(inputs):
            lstm_input_t = input_t.clone()
            lstm_input_t[input_t == 0] = outputs[i][input_t == 0]

            h_t, c_t = self.lstm1(input_t, (h_t, c_t))
            h_t2, c_t2 = self.lstm2(h_t, (h_t2, c_t2))
            output = self.linear(h_t2)
            outputs += [output]

        outputs = torch.stack(outputs[1:], 1).squeeze(2)

        return outputs


"""
    Refer to https://github.com/nelson-liu/contextual-repr-analysis/blob/master/contexteval/contextualizers/precomputed_contextualizer.py
"""
import json
import logging
from typing import List, Optional

import h5py
import torch

from src.contextualizers import Contextualizer

logger = logging.getLogger(__name__)  # pylint: disable=invalid-name

class PrecomputedContextualizer(Contextualizer):
    """
    This "contextualizer" simply reads representations from one or multiple
    hdf5 files provided upon construction.

    Each hdf5 has the following two groups of data:
        sentence_to_index: dict that maps each sentence (string) to a index
        indexed representaion: representation (words x dim) matrix for each
            indexed sentence

    If multiple hdf5 files, they must be built based on the same set of
    sentencces. The returned are concatenated features.

    Parameters
    ----------
    representations_paths: list of paths
        A list of Paths to multiple HDF5 files with the representations.
    """
    def __init__(self,
                 representations_paths: List[str],
                 ) -> None:
        super(PrecomputedContextualizer, self).__init__()
        # if `file_path` is a URL, redirect to the cache
        self.paths = representations_paths
        # Read the HDF5 file.
        self._representations = [h5py.File(_, 'r') for _ in self.paths]
        # Get the sentences to index mapping
        self._sentence_to_index = json.loads(
                self._representations[0].get('sentence_to_index')[0])
        # some sanity checks
        for i in range(1, len(self._representations)):
            this_sentence_to_index = json.loads(
                    self._representations[i].get('sentence_to_index')[0])
            assert this_sentence_to_index == self._sentence_to_index
        self.dim = self.get_dim()

    def forward(self, sentences: List[List[str]]) -> torch.FloatTensor:
        """
        Parameters
        ----------
        sentences: List[List[str]]
            A batch of sentences. len(sentences) is the batch size, and each sentence
            itself is a list of strings (the constituent words). If the batch is padded,
            the expected padding token in the Python ``None``.

        Returns
        -------
        representations: List[FloatTensor]
            A list with the contextualized representations of all words in an input sentence.
            Each inner FloatTensor is of shape (seq_len, repr_dim), and an outer List
            is used to store the representations for each input sentence.
        """
        batch_representations = []

        for sentence in sentences:
            this_sentence = " ".join([x for x in sentence if x is not None])
            this_id = self._sentence_to_index[this_sentence]
            # read multiple representations (words x dim) into a list
            representation = [torch.FloatTensor(reptn[this_id])
                    for reptn in self._representations]
            representation = torch.hstack(representation)
            batch_representations.append(representation)
        return batch_representations

    def get_dim(self):
        """
            Get dimension of features
        """
        dims = [rptn['1'].shape[1] for rptn in self._representations]
        return sum(dims)


#! /usr/bin/python

import matplotlib
matplotlib.use('Qt4Agg')
import pylab
import numpy
import sys

aux = sys.argv[0]
pref = aux[:aux.find('show_mesh_points.py')]

rawd = numpy.loadtxt(pref+'mesh_generating_points.txt')
pylab.plot(rawd[:,0], rawd[:,1],'x')

rawd2 = numpy.loadtxt(pref+'edges.txt')
for i in range(len(rawd2[:,0])):
    pylab.plot((rawd2[i,0],rawd2[i,2]),(rawd2[i,1],rawd2[i,3]),'r')
pylab.axis('equal')
pylab.xlim((-0.05, 1.05))
pylab.ylim((-0.05, 1.05))
pylab.show()


"""
Given a string, determine if it consists of all unique characters.

For example, the string 'abcde' has all unique characters and should return True.
The string 'aabcde' contains duplicate characters and should return False.
"""

# def uniqueCharacters(str):
#     return len(set(str)) == len(str)

# test_str = "aabcde"

# test_str_positive = "abcde"
# test_str_negative = "aabcde"
#
# print(test_str_positive)
# print(uniqueCharacters(test_str_positive))
#
# print(test_str_negative)
# print(uniqueCharacters(test_str_negative))

# O(n) --> n represents the number of characters
def uniqueCharacters(str):
    if len(str) == 1:
        return True

    chars = set()
    for char in str:
        if char in chars:
            return False
        else:
            chars.add(char)
    return True

test_str_positive = "abcde"
test_str_negative = "aabcde"

print(test_str_positive)
print(uniqueCharacters(test_str_positive))

print(test_str_negative)
print(uniqueCharacters(test_str_negative))


"""router topology example

chain of 3 routers between two hosts; simpler than the general version

   h1---r1---r2---r3---h2

Subnets are 10.0.0 to 10.0.3
Last byte (host byte) is 2 on the left and 1 on the right
   10.0.0.2-r1-10.0.1.1
   10.0.1.2-r2-10.0.2.1
   10.0.2.2-r3-10.0.3.1
ri-eth0 is on the left and ri-eth1 is on the right

"""

# mn --custom router.py --topo rtopo

from mininet.net import Mininet
from mininet.node import Node, OVSKernelSwitch, Controller, RemoteController
#from mininet.node import Node, Host, OVSSwitch, OVSKernelSwitch, Controller, RemoteController, DefaultController
from mininet.cli import CLI
from mininet.link import TCLink
from mininet.topo import Topo
from mininet.log import setLogLevel, info

ENABLE_LEFT_TO_RIGHT_ROUTING = True		# tell all routers how to get to h2

class RTopo(Topo):

    def build(self, **_opts):     # special names?
        h1 = self.addHost( 'h1', ip='10.0.0.10/24', defaultRoute='via 10.0.0.2' )
        h2 = self.addHost( 'h2', ip='10.0.3.10/24', defaultRoute='via 10.0.3.1' )
        r1 = self.addHost( 'r1' )
        r2 = self.addHost( 'r2' )
        r3 = self.addHost( 'r3' )

        self.addLink( h1, r1, intfName1 = 'h1-eth0', intfName2 = 'r1-eth0')
        self.addLink( r1, r2, inftname1 = 'r1-eth1', inftname2 = 'r2-eth0')
        self.addLink( r2, r3, inftname1 = 'r2-eth1', inftname2 = 'r3-eth0')
        self.addLink( r3, h2, intfName1 = 'r3-eth1', intfName2 = 'h2-eth0')

def run():
    rtopo = RTopo()
    net = Mininet(topo = rtopo, link=TCLink, autoSetMacs = True)
    net.start()

    r1 = net['r1']
    r2 = net['r2']
    r3 = net['r3']
    h1 = net['h1']
    h2 = net['h2']

    r1.cmd('ifconfig r1-eth0 10.0.0.2/24')
    r1.cmd('ifconfig r1-eth1 10.0.1.1/24')
    r1.cmd('sysctl net.ipv4.ip_forward=1')
    rp_disable(r1)

    r2.cmd('ifconfig r2-eth0 10.0.1.2/24')
    r2.cmd('ifconfig r2-eth1 10.0.2.1/24')
    r2.cmd('sysctl net.ipv4.ip_forward=1')
    rp_disable(r2)

    r3.cmd('ifconfig r3-eth0 10.0.2.2/24')
    r3.cmd('ifconfig r3-eth1 10.0.3.1/24')
    r3.cmd('sysctl net.ipv4.ip_forward=1')
    rp_disable(r3)

    # add one-way routes to 10.0.3.0/24:
    if ENABLE_LEFT_TO_RIGHT_ROUTING:
        r1.cmd('route add -net 10.0.3.0/24 gw 10.0.1.2')
        r2.cmd('route add -net 10.0.3.0/24 gw 10.0.2.2')

    for h in [h1, r1, r2, r3, h2]:  h.cmd('/usr/sbin/sshd')

    CLI( net)
    net.stop()

# in the following, ip(4,2) returns 10.0.4.2
def ip(subnet,host,prefix=None):
    addr = '10.0.'+str(subnet)+'.' + str(host)
    if prefix != None: addr = addr + '/' + str(prefix)
    return addr

# For some examples we need to disable the default blocking of forwarding of packets with no reverse path
def rp_disable(host):
    ifaces = host.cmd('ls /proc/sys/net/ipv4/conf')
    ifacelist = ifaces.split()    # default is to split on whitespace
    for iface in ifacelist:
       if iface != 'lo': host.cmd('sysctl net.ipv4.conf.' + iface + '.rp_filter=0')
    #print 'host', host, 'iface list:',  ifacelist

setLogLevel('info')
run()

"""
Manual routing commands:

r1: ip route add to 10.0.3.0/24 via 10.0.1.2 dev r1-eth1
r2: ip route add to 10.0.3.0/24 via 10.0.2.2 dev r2-eth2

r1: route add -net 10.0.3.0/24 gw 10.0.1.2
r2: route add -net 10.0.3.0/24 gw 10.0.2.2

r3: ip route add to 10.0.0.0/24 via 10.0.2.1 dev r3-eth0
r2: ip route add to 10.0.0.0/24 via 10.0.1.1 dev r2-eth0

r3: route add -net 10.0.0.0/24 gw 10.0.2.1
r2: route add -net 10.0.0.0/24 gw 10.0.1.1

"""


# -*- coding: utf-8 -*-
"""
Created on Tue Apr 11 15:08:53 2017

@author: nickl
"""

#read from one file and write to another
input_file = open("replace.txt", "r") #read from this file
output_file = open("readit.txt", "w")   #write to this file
createDict = {}
for line in input_file: #one line at a time
    #createDict = {}
    value, key = line.split()
    createDict[key] = value
print(createDict)
#line = line.strip() #remove the carriage return
    #new_str = "" #empty string
    #for char in line: #one character at a time
    #    new_str = char + new_str #concatenating backwards
    #print(new_str, file = output_file)
#print(key, value, file = output_file)

input_file.close()
output_file.close()


from netCDF4 import Dataset
import numpy as np
from datetime import datetime, timedelta
from progressbar import ProgressBar
import settings
import utils
import glob

class parcels_to_timeslicing:
    def __init__(self, file_dict: dict):
        self.parallel_step = settings.PARALLEL_STEP
        self.file_dict = file_dict
        self.temp_direc, self.output_direc = get_directories(scenario_name=settings.SCENARIO_NAME)
        self.reference_time = datetime(2010, 1, 1, 12, 0)
        self.time_difference_list = set_time_difference_list(ref_time=self.reference_time, parallel_step=self.parallel_step)
        self.variable_list = ['lon', 'lat', 'z', 'beach', 'size_class', 'time']

    def run(self):
        if self.parallel_step == 1:
            year, month, run, restart = settings.STARTYEAR, settings.STARTMONTH, settings.RUN, settings.RESTART
            utils.print_statement('Splitting {}-{}, run {} restart {}'.format(year, month, run, restart), to_print=True)
            # Loading the data
            parcels_dataset = load_parcels_post_output(file_dict=self.file_dict)
            full_dict = {}
            for key in self.variable_list:
                if key in parcels_dataset.variables.keys():
                    full_dict[key] = parcels_dataset.variables[key][:, :-1]
            # Looping through the dates
            for time_value in self.time_difference_list:
                date = (self.reference_time + timedelta(seconds=time_value)).strftime("%Y-%m-%d-%H-%M-%S")
                prefix = 'timeslices_{}'.format(date)
                output_name = get_file_names(file_dict=self.file_dict, prefix=prefix, directory=self.temp_direc,
                                             final=False)
                if not utils.check_file_exist(output_name, without_pkl=True):
                    time_selection = full_dict['time'] == time_value
                    # Setting the output name
                    if np.nansum(time_selection) > 0:
                        time_dict = {}
                        for variable in self.variable_list[:-1]:
                            if variable in full_dict.keys():
                                time_dict[variable] = full_dict[variable][time_selection]
                        utils.save_obj(filename=output_name, item=time_dict)
        elif self.parallel_step == 2:
            pbar = ProgressBar()
            for time_value in pbar(self.time_difference_list):
                date = (self.reference_time + timedelta(seconds=time_value)).strftime("%Y-%m-%d-%H-%M-%S")
                prefix = 'timeslices_{}'.format(date)
                # Setting the output name
                output_name = get_file_names(file_dict=self.file_dict, directory=self.output_direc, final=True,
                                             prefix=prefix)
                # Loading an example input file to know which variables are stored in it:
                parcels_dataset = load_parcels_post_output(file_dict=self.file_dict)
                # Creating an output dict
                output_dict = {}
                for key in self.variable_list[:-1]:
                    if key in parcels_dataset.variables.keys():
                        output_dict[key] = np.array([], dtype=float)
                # Getting all the timeslice files for this date, and looping through them
                str_format = settings.INIT_SIZE, settings.INIT_DENSITY
                file_list = glob.glob(self.temp_direc + prefix + '*{:.1E}*{}*'.format(*str_format))
                for file_name in file_list:
                    time_file = utils.load_obj(file_name)
                    for key in output_dict.keys():
                        output_dict[key] = np.append(output_dict[key], time_file[key])
                    utils.remove_file(file_name)
                    utils.print_statement("At {} we have {} particles".format(date, output_dict['lon'].size), to_print=True)
                    utils.save_obj(output_name, output_dict)
        else:
            ValueError('settings.PARALLEL_STEP can not have a value of {}'.format(self.parallel_step))

########################################################################################################################
"""
These following functions are used across all scenarios
"""

def get_directories(scenario_name):
    # temp_direc = utils.get_output_directory(server=settings.SERVER) + 'timeslices/{}/temporary/'.format(scenario_name)
    temp_direc = settings.SCRATCH_DIREC
    output_direc = settings.DATA_OUTPUT_DIREC + 'timeslices/{}/'.format(scenario_name)
    utils.check_direc_exist(temp_direc)
    utils.check_direc_exist(output_direc)
    return temp_direc, output_direc

def load_parcels_post_output(file_dict, year=settings.STARTYEAR, month=settings.STARTMONTH,
                             run=settings.RUN, restart=settings.RESTART, with_post=True):
    if settings.SCENARIO_NAME in ['FragmentationKaandorpPartial']:
        parcels_dataset = Dataset(file_dict['parcels'][year][month][run][restart])
    else:
        parcels_dataset = Dataset(file_dict[run][restart])
    return parcels_dataset

def get_file_names(file_dict, directory, final, prefix, year=settings.STARTYEAR, month=settings.STARTMONTH,
                   run=settings.RUN, restart=settings.RESTART):
    split = {True: None, False: '.nc'}[final]
    if settings.SCENARIO_NAME in ['FragmentationKaandorpPartial']:
        output_name = directory + utils.analysis_save_file_name(input_file=file_dict['postprocess'][year][month][run][restart],
        prefix=prefix, split=split)
    else:
        output_name = directory + utils.analysis_save_file_name(input_file=file_dict[run][restart], prefix=prefix, split=split)
    return output_name

def set_time_difference_list(ref_time, parallel_step):
    if parallel_step == 1:
        current_time, end_time = datetime(2010 + settings.RESTART, 1, 1, 0), datetime(2010 + settings.RESTART + 1, 1, 1, 0)
    elif parallel_step == 2:
        current_time, end_time = datetime(2010, 1, 1, 0), datetime(2010 + settings.SIM_LENGTH, 1, 1, 0)
    time_step, time_list = timedelta(hours=24), []
    while current_time < end_time:
        time_list.append((current_time - ref_time).total_seconds())
        current_time += time_step
    return time_list


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

from collections import OrderedDict
import csv

def prepare_vocal_multiple_option(path="data/questions_vocal_single_option.csv"):
    """
    Load questions and answer formats for single-option vocal questions
    :param path: csv file to load from
    :return: ordered dictionary where keys are questions and values are answer format that the subject must say out loud
    """
    question_templates = OrderedDict()

    with open(path, encoding='utf-8') as out:
        reader = csv.reader(out)

        headers = next(reader, [])

        if len(headers) != 2 or headers[0] != "question" or headers[1] != "answer_format":
            raise Exception("prepare_vocal_single_options: Bad csv format")

        for row in reader:
            if len(row) != 5:
                raise Exception("prepare_vocal_single_options: Bad csv format")

            question_templates[row[0]] = [row[1],row[2],row[3],row[4]]

    return question_templates

def assoc_array_to_list_multiple(sv_od):
    return [item for kvp in list(sv_od.items()) for item in kvp]


DAge={'<=30':{'yes':2/5, 'no':3/5},
       '30-40':{'yes':1,'no':0},
       '>40':{'yes':3/5, 'no':2/5}}
DIncome={
    'low':{'yes':3/4,'no':1/4},
    'medium':{'yes':4/6,'no':2/6},
    'high':{'yes':2/4,'no':2/4}
}
DStudent={
    'yes':{'yes':6/7,'no':1/7},
    'no':{'yes':3/7,'no':4/7}
}
DCredit={
    'fair':{'yes':6/8,'no':2/8},
    'excellent':{'yes':3/6,'no':3/6}
}

PYes=9/14
PNo=5/14

age=input('Enter Age from <=30, 30-40, >40 : ')
income=input('Enter income from low, medium, high : ')
student=input('Enter student from yes, no : ')
cr=input('Enter credit rating from fair, excellent : ')

VYes=PYes*(DAge[age]['yes'])*DIncome[income]['yes']*DCredit[cr]['yes']*DStudent[student]['yes'];

VNo=PNo*(DAge[age]['no'])*DIncome[income]['no']*DCredit[cr]['no']*DStudent[student]['no'];
print()
print(VYes/(VYes+VNo));
print(VNo/(VYes+VNo));
print()

if(VYes>VNo):
  print('Buys Computer = Yes')
else:
  print('Buys Computer = No')


from model import *
from utils import *

def train_96(img_size,
          reg_stage,
          ext_stage,
          train_set_name,
          val_set_name,
          test_set_name,
          batch_size,
          num_epochs,
          learning_rate,
          model_name,
          reg_loss_name,
          mask_smooth_loss_func,
          lamda_mask,
          gamma,
          save_every_epoch,
          dice_label,
          if_train_aug,
          fixed_set_name,
          save_start_epoch):

    device = torch.device("cuda:0" if (torch.cuda.is_available()) else "cpu")
    model = model_name.to(device)
    mask_smooth = mask_smooth_loss_func.to(device)

    optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

    if reg_loss_name=="NCC":
        reg_loss_func=NCC().loss
    elif reg_loss_name=="GCC":
        reg_loss_func=GCC()
    elif reg_loss_name=="MSE":
        reg_loss_func=nn.MSELoss().to(device)

    cur_path = os.getcwd()
    result_path=cur_path+'/result'

    loss_log_path=result_path+'/loss_log'
    create_folder(result_path,'loss_log')

    sample_img_path=result_path+'/sample_img'
    create_folder(result_path,'sample_img')

    model_save_path=result_path+'/model'
    create_folder(result_path,'model')

    model_str=str(model)[0:str(model).find("(")]
    lamda_mask_str=str(lamda_mask)
    gamma_str=str(gamma)
    lr_str=str(learning_rate)
    dataset_str=train_set_name[0:str(train_set_name).find(".")]

    ext_stage_str=str(ext_stage)
    reg_stage_str=str(reg_stage)

    if if_train_aug==True:
        modal_name=model_str+"_"+ext_stage_str+"_"+reg_stage_str+"_"+lamda_mask_str+"_"+gamma_str+"_"+lr_str+"_"+dataset_str+"_trainAug_true"
    else:
        modal_name=model_str+"_"+ext_stage_str+"_"+reg_stage_str+"_"+lamda_mask_str+"_"+gamma_str+"_"+lr_str+"_"+dataset_str

    modal_path=sample_img_path+"/"+modal_name
    create_folder(sample_img_path,modal_name)

    sample_o_path=modal_path+"/"+"o"
    sample_t_path=modal_path+"/"+"t"
    create_folder(modal_path,"o")
    create_folder(modal_path,"t")

    for i in range(int(ext_stage)):
        idx=i+1
        s_name="s_"+str(idx)
        s_mask_name="s_"+str(idx)+"_mask"
        sample_s_path=modal_path+"/"+s_name
        sample_s_mask_path=modal_path+"/"+s_mask_name

        create_folder(modal_path,s_name)
        create_folder(modal_path,s_mask_name)

    for q in range(int(reg_stage)):
        qdx=q+1
        r_name="r_"+str(qdx)
        r_grid_name="r_"+str(qdx)+"_grid"
        sample_r_path=modal_path+"/"+r_name
        sample_r_grid_path=modal_path+"/"+r_grid_name

        create_folder(modal_path,r_name)
        create_folder(modal_path,r_grid_name)
    if if_train_aug==True:
        modal_info="Model: {}    ext_stage: {}    reg_stage: {}    λ_mask: {}    γ: {}    lr: {}    dataset: {}    train_Agu: {}".format(model_str,
                                                                                ext_stage_str,
                                                                                reg_stage_str,
                                                                                lamda_mask_str,
                                                                                gamma_str,
                                                                                lr_str,dataset_str,"true")
    else:
        modal_info="Model: {}    ext_stage: {}    reg_stage: {}    λ_mask: {}    γ: {}    lr: {}    dataset: {}".format(model_str,
                                                                                ext_stage_str,
                                                                                reg_stage_str,
                                                                                lamda_mask_str,
                                                                                gamma_str,
                                                                                lr_str,dataset_str)

    create_log(modal_info,loss_log_path,modal_name)

    print (modal_info)

    train_loader=load_data_no_fix(train_set_name,batch_size)
    val_loader=load_data_no_fix(val_set_name,batch_size)
    test_loader=load_data_no_fix(test_set_name,batch_size)

    for epoch in range(num_epochs):

        total_loss_train=[]
        total_sim_loss_train=[]

        start=time.time()

        fixed_data=torch.from_numpy(np.load('./dataset/'+fixed_set_name+"_fixed_96.npy")).to(device).view(-1,1,img_size,img_size,img_size).float()
        fixed_label_am=torch.from_numpy(np.load('./dataset/'+fixed_set_name+"_fixed_amlabel_96.npy")).to(device).view(-1,1,img_size,img_size,img_size).float()
        fixed_data_val=torch.from_numpy(np.load('./dataset/'+fixed_set_name+"_fixed_96.npy")).to(device).view(-1,1,img_size,img_size,img_size).float()
        fixed_label_am_val=torch.from_numpy(np.load('./dataset/'+fixed_set_name+"_fixed_amlabel_96.npy")).to(device).view(-1,1,img_size,img_size,img_size).float()

        for i, x in enumerate(train_loader):

            moving_data,moving_label_bm,moving_label_am=x

            moving_data=moving_data.to(device).view(-1,1,img_size,img_size,img_size).float()
            moving_label=moving_label_bm.to(device).view(-1,1,img_size,img_size,img_size).float()

            if if_train_aug == True:

                degree=5
                voxel=5
                scale_min=0.98
                scale_max=1.02

                """
                degree=2
                voxel=2
                scale_min=0.98
                scale_max=1.02
                """

                moving_data,moving_label=train_aug(moving_data,moving_label,img_size,degree,voxel,scale_min,scale_max)

            optimizer.zero_grad()

            striped_list,mask_list,warped_list,grid_list= model(fixed_data,moving_data,if_train=True)

            sim_loss_train=reg_loss_func(warped_list[-1],fixed_data)

            mask_smooth_loss=lamda_mask*sum([mask_smooth(i) for i in mask_list])

            loss_train = sim_loss_train + mask_smooth_loss
            loss_train.backward()
            optimizer.step()

            total_loss_train.append(loss_train.item())
            total_sim_loss_train.append(sim_loss_train.item())

        ave_loss_train,std_loss_train=get_ave_std(total_loss_train)
        ave_sim_loss_train,std_sim_loss_train=get_ave_std(total_sim_loss_train)

        if epoch % save_every_epoch ==0:
            model.eval()
            with torch.no_grad():

                total_loss_val_sim_af=[]

                total_ext_val=[]
                total_dice_val=[]

                for j, y in enumerate(val_loader):

                    moving_data_val,moving_label_bm_val,moving_label_am_val=y

                    moving_data_val=moving_data_val.to(device).view(-1,1,img_size,img_size,img_size).float()
                    moving_label_bm_val=moving_label_bm_val.to(device).view(-1,1,img_size,img_size,img_size).float()
                    moving_label_am_val=moving_label_am_val.to(device).view(-1,1,img_size,img_size,img_size).float()

                    striped_list_val,mask_list_val,warped_list_val,grid_list_val= model(fixed_data_val,moving_data_val,if_train=False)

                    sim_loss_val=reg_loss_func(warped_list_val[-1],fixed_data_val)

                    total_loss_val_sim_af.append(sim_loss_val.item())

                    merged_mask_list_val=np.prod(mask_list_val)
                    ext_val=f1_dice_loss(merged_mask_list_val,moving_label_bm_val)
                    total_ext_val.append(ext_val)

                    moving_label_am_skulled_val=merged_mask_list_val*moving_label_am_val
                    warped_label_am_skulled_val=F.grid_sample(moving_label_am_skulled_val, grid_list_val[-1],
                                                              mode="nearest",align_corners=True,padding_mode="zeros")

                    dice_val=compute_label_dice(fixed_label_am_val,warped_label_am_skulled_val,dice_label)

                    total_dice_val.append(dice_val)

                ave_loss_val_sim,std_loss_val_sim=get_ave_std(total_loss_val_sim_af)

                ave_ext_val,std_ext_val=get_ave_std(total_ext_val)
                ave_dice_val,std_dice_val=get_ave_std(total_dice_val)

                loss_info="Epoch[{}/{}], All Training loss: {:.4f}/{:.4f} , Reg Training loss: {:.4f}/{:.4f} , Reg val loss: {:.4f}/{:.4f} , Dice_ext_val: {:.4f}/{:.4f}  ,  Dice_reg_val: {:.4f}/{:.4f}".format(epoch+1,num_epochs,
                ave_loss_train,std_loss_train,
                ave_sim_loss_train,std_sim_loss_train,
                ave_loss_val_sim,std_loss_val_sim,
                ave_ext_val,std_ext_val,
                ave_dice_val,std_dice_val
                )

                print (loss_info)
                append_log(loss_info,loss_log_path,modal_name)

                if epoch>save_start_epoch:

                    save_sample_any(epoch,"o",fixed_data_val,sample_o_path)
                    #save_nii_any(epoch,"o",fixed_data_val,sample_o_path)

                    save_sample_any(epoch,"t",moving_data_val,sample_t_path)
                    #save_nii_any(epoch,"t",moving_data_val,sample_t_path)

                    for t in range(int(ext_stage)):
                        tdx=t+1
                        s_name="s_"+str(tdx)
                        s_mask_name="s_"+str(tdx)+"_mask"

                        sample_s_path=modal_path+"/"+s_name
                        sample_s_mask_path=modal_path+"/"+s_mask_name

                        save_sample_any(epoch,s_name,striped_list_val[t],sample_s_path)
                        #save_nii_any(epoch,s_name,striped_list_val[t],sample_s_path)

                        save_sample_any(epoch,s_mask_name,mask_list_val[t],sample_s_mask_path)

                    for y in range(int(reg_stage)):
                        ydx=y+1
                        r_name="r_"+str(ydx)
                        sample_r_path=modal_path+"/"+r_name

                        save_sample_any(epoch,r_name,warped_list_val[y],sample_r_path)
                        #save_nii_any(epoch,r_name,warped_list_val[y],sample_r_path)

                    torch.save(model.state_dict(), os.path.join(model_save_path,modal_name+"_"+str(epoch)+".pth"))

    return


import json
from datetime import datetime
import asyncio as aio

from api import NiceHashAPI
from config import dict_to_object
from miners import miner_types

class Worker:
    def __init__(self, gpu, conf, loop):
        self.loop = loop
        self.gpu = dict_to_object(gpu)
        self.cwd = conf.cwd
        self.config = conf.config
        self.algos = conf.algos
        self.session = conf.session
        self.is_benchmark = conf.benchmark
        min_wait = self.config.nicehash.min_wait
        self.nicehash = NiceHashAPI(self.gpu.address, min_wait, loop)
        self.miner = None

    def read_heuristics(self):
        h = self.cwd / ("gpu_heuristics_%d.json" % self.gpu.gpu_id)
        if not h.exists():
            return
        with open(h) as f:
            self.heurisitcs = json.load(f)
        return self.heurisitcs

    async def start(self):
        if not self.is_benchmark:
            assert self.read_heuristics()
        if self.is_benchmark:
            await self.benchmark()
        else:
            await self.run()

    async def benchmark(self):
        for algo, algo_conf in await self.filter_algos():
            await self.exec(algo, algo_conf)
            await aio.sleep(self.gpu.min_time)
            await self.miner.stop()

    async def filter_algos(self):
        prices = await self.nicehash.get_prices()
        algos = []
        for algo in prices:
            algo_conf = getattr(self.algos, algo['name'], None)
            if algo_conf:
                algos.append((algo, algo_conf))
        return algos

    async def exec(self, algo, algo_conf):
        now_d = datetime.now().strftime("%Y-%m-%d")
        now_t = datetime.now().strftime("%H-%M-%S")
        log_p = self.cwd / 'logs' / now_d / ('gpu-%s-%s-%s.log' %
                                             (self.gpu.gpu_id, now_t,
                                              algo['name']))
        log_p.parent.mkdir(parents=True, exist_ok=True)
        if type(algo_conf) is tuple:
            miner_type = miner_types[algo_conf[0]]
            self.miner = miner_type(
                *algo_conf[1:],
                algo=algo,
                gpu=self.gpu,
                log_p=log_p,
                session=self.session,
                loop=self.loop)
        else:
            miner_type = miner_types[algo_conf]
            self.miner = miner_type(algo, self.gpu, log_p, self.session,
                                    self.loop)
        await self.miner.run()


from Python_Rec import Customer

customers = []

def new_customers():
    number_of_customers = int(input("Введите число новых посетителей: "))
    for i in range(number_of_customers):
        nc = Customer(input("Введите имя клиента: "),
                      input("Введите баланс клиента: "))
        customers.append(nc)

def show_customers():
    for customer in customers:
        print(f'Клиент: {customer.name}. Баланс: {customer.balance}')

new_customers()
show_customers()


import argparse
import os
import glob
import random
import fnmatch

def parse_directory(path, rgb_prefix='img_', flow_x_prefix='flow_x_', flow_y_prefix='flow_y_'):
    """
    Parse directories holding extracted frames from standard benchmarks
    """
    print('parse frames under folder {}'.format(path))
    frame_folders = glob.glob(os.path.join(path, '*'))

    def count_files(directory, prefix_list):
        lst = os.listdir(directory)
        cnt_list = [len(fnmatch.filter(lst, x+'*')) for x in prefix_list]
        return cnt_list

    rgb_counts = {}
    flow_counts = {}
    for i,f in enumerate(frame_folders):
        all_cnt = count_files(f, (rgb_prefix, flow_x_prefix, flow_y_prefix))
        k = f.split('/')[-1]
        rgb_counts[k] = all_cnt[0]
        x_cnt = all_cnt[1]
        y_cnt = all_cnt[2]
        if x_cnt != y_cnt:
            raise ValueError('x and y direction have different number of flow images. video: '+f)
        flow_counts[k] = x_cnt
        if i % 200 == 0:
            print('{} videos parsed'.format(i))

    print('frame folder analysis done')
    return rgb_counts, flow_counts

def build_split_list(split_tuple, frame_info, split_idx, shuffle=False):
    split = split_tuple[split_idx]

    def build_set_list(set_list):
        rgb_list, flow_list = list(), list()
        for item in set_list:
            rgb_cnt = frame_info[0][item[0]]
            flow_cnt = frame_info[1][item[0]]
            rgb_list.append('{} {} {}\n'.format(item[0], rgb_cnt, item[1]))
            flow_list.append('{} {} {}\n'.format(item[0], flow_cnt, item[1]))
        if shuffle:
            random.shuffle(rgb_list)
            random.shuffle(flow_list)
        return rgb_list, flow_list
    print('here')
    train_rgb_list, train_flow_list = build_set_list(split[0])
    test_rgb_list, test_flow_list = build_set_list(split[1])
    return (train_rgb_list, test_rgb_list), (train_flow_list, test_flow_list)

def parse_dataset_splits(dataset = 'ucf101_splits'):
    print(dataset)
    class_ind = [x.strip().split() for x in open(dataset+'/classInd.txt')]
    print(class_ind)
    class_mapping = {x[1]:int(x[0])-1 for x in class_ind}

    def line2rec(line):
        items = line.strip().split('/')
        label = class_mapping[items[0]]
        vid = items[1].split('.')[0]
        return vid, label

    splits = []
    for i in xrange(1, 4):
        train_list = [line2rec(x) for x in open(dataset+'/trainlist{:02d}.txt'.format(i))]
        test_list = [line2rec(x) for x in open(dataset+'/testlist{:02d}.txt'.format(i))]
        splits.append((train_list, test_list))
    return splits

def create_files_HMDB51(path_file='hmdb51_splits'):
    """
        Create train and test files standard
        author : Darwin TC
        date: 03/05/2018
    """
    def line2rec(line, splits):
        detail_line = line.split(' ')
        number_split = splits[-1:]
        return (detail_line[0],detail_line[1], int(number_split))

    def create_splits():
        """
            Read the initial traing and test files of the HMDB51 dataset to afterward create
            just 3 files for test and train, sorting of a standard way similar to UCF101 dataset
        """
        # load split file
        class_files = glob.glob(path_file+'/*split*.txt')
        class_list = set([x[x.find('/')+1:x.find('_test')] for x in class_files])
        class_dict = {x: str(i+1) for i,x in enumerate(class_list)}
        splits_train = [[],[],[]]
        splits_test = [[],[],[]]
        for name in class_files:
            part_name = name.split('/')[-1][:-4].split('_')
            test_list = [line2rec(x, part_name[2] if len(part_name)==3 else part_name[3]) for x in open(name)]
            for line in test_list:
                name_class = part_name[0] if len(part_name)==3 else (part_name[0]+'_'+part_name[1])
                name_clip = line[0][:line[0].find('.avi')]
                number_split = line[2]
                type_data = line[1] # is a data train or test(1,2)
                new_data = name_class+'/'+name_clip+'\n'
                if type_data == '1':
                    splits_train[number_split-1].append(new_data)
                elif type_data == '2':
                    splits_test[number_split-1].append(new_data)
        return class_dict, splits_train, splits_test

    def create_files(class_names, train, test, out_path ='.'):
        """
           method to create the initial files of class names, train and test
        """
        list_label = ['{} {}\n'.format(v,k) for k,v in class_names.items()]
        open(os.path.join(out_path, 'classInd.txt'), 'w').writelines(list_label)
        for i in range(1,4):
            open(os.path.join(out_path, 'trainlist{:02d}.txt'.format(i)), 'w').writelines(train[i-1])
            open(os.path.join(out_path, 'testlist{:02d}.txt'.format(i)), 'w').writelines(test[i-1])

    #create list of class, train and test
    class_names, train, test = create_splits()
    for i in range(3):
        train[i]=sorted(train[i])
        test[i]=sorted(test[i])
    create_files(class_names,train, test)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--dataset', type=str, default='ucf101', choices=['ucf101', 'hmdb51'])
    parser.add_argument('--frame_path', type=str, default='./ucf101_frames',
                        help="root directory holding the frames")
    parser.add_argument('--out_list_path', type=str, default='./settings')

    parser.add_argument('--rgb_prefix', type=str, default='img_',
                        help="prefix of RGB frames")
    parser.add_argument('--flow_x_prefix', type=str, default='flow_x',
                        help="prefix of x direction flow images")
    parser.add_argument('--flow_y_prefix', type=str, default='flow_y',
                        help="prefix of y direction flow images", )

    parser.add_argument('--num_split', type=int, default=3,
                        help="number of split building file list")
    parser.add_argument('--shuffle', action='store_true', default=False)

    args = parser.parse_args()

    dataset = args.dataset
    frame_path = args.frame_path
    rgb_p = args.rgb_prefix
    flow_x_p = args.flow_x_prefix
    flow_y_p = args.flow_y_prefix
    num_split = args.num_split
    out_path = args.out_list_path
    shuffle = args.shuffle

    out_path = os.path.join(out_path,dataset)
    if not os.path.isdir(out_path):
        print("creating folder: "+out_path)
        os.makedirs(out_path)

    # operation
    print('processing dataset {}'.format(dataset))
    split_tp = parse_dataset_splits(dataset+'_splits')

    f_info = parse_directory(frame_path, rgb_p, flow_x_p, flow_y_p)

    print('writing list files for training/testing')
    for i in xrange(max(num_split, len(split_tp))):
        lists = build_split_list(split_tp, f_info, i, shuffle)
        open(os.path.join(out_path, 'train_rgb_split{}.txt'.format(i + 1)), 'w').writelines(lists[0][0])
        open(os.path.join(out_path, 'val_rgb_split{}.txt'.format(i + 1)), 'w').writelines(lists[0][1])
        open(os.path.join(out_path, 'train_flow_split{}.txt'.format(i + 1)), 'w').writelines(lists[1][0])
        open(os.path.join(out_path, 'val_flow_split{}.txt'.format(i + 1)), 'w').writelines(lists[1][1])


def pig_it(text):
    #your code here
    words = text.split(" ")
    new_sent = []
    for word in words:
        if str.isalpha(word):
            new_word = word[1:]+word[0]+"ay"
            new_sent.append(new_word)
        else:
            new_sent.append(word)

    return (" ").join(new_sent)

print(pig_it('Pig latin is cool'))
print(pig_it('Hello world !'))


from flask import Flask, render_template, request, send_file
import pandas as pd
from openpyxl import Workbook
from openpyxl.styles import numbers
from openpyxl.utils import get_column_letter
import os

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/', methods=['POST'])
def generate_id():
    file = request.files['file']
    if file:
        file.save('data/ean.xlsx')

        tabela_ean = pd.read_excel('data/ean.xlsx')
        tabela_id = pd.read_excel('data/id.xlsx')

        rezultujuca_tabela = pd.merge(tabela_ean, tabela_id, on='EAN', how='left')
        rezultujuca_tabela['ID'].fillna('Nemam ID', inplace=True)
        rezultujuca_tabela.loc[rezultujuca_tabela['EAN'].isnull(), 'ID'] = ''

        book = Workbook()
        sheet = book.active

        header = list(rezultujuca_tabela.columns)
        sheet.append(header)
        for _, row in rezultujuca_tabela.iterrows():
            sheet.append(list(row))

        for cell in sheet['A'][1:]:
            cell.number_format = numbers.FORMAT_NUMBER

        for column in sheet.columns:
            max_length = 0
            column_letter = get_column_letter(column[0].column)
            for cell in column:
                try:
                    if len(str(cell.value)) > max_length:
                        max_length = len(str(cell.value))
                except:
                    pass
            adjusted_width = (max_length + 2) * 1.2
            sheet.column_dimensions[column_letter].width = adjusted_width

        book.save('data/rezultat.xlsx')
        return render_template('download.html')
    else:
        return "Nije otpremljen fajl."

@app.route('/download')
def download():
    return send_file('data/rezultat.xlsx', as_attachment=True)

if __name__ == '__main__':
    app.run()


import torch
import torch.nn as nn
from torch.autograd import *
import torch.optim as optim
from model import LSTM
from dataset import create_datalist, create_dataset
from torch.utils.data import DataLoader
import math
import os
import pandas as pd
from math import log10

def ToVariable(x):
    tmp = torch.FloatTensor(x)
    return Variable(tmp)

use_gpu = torch.cuda.is_available()
# print(use_gpu)
input_size = 900
output_size = 900
hidden_dim = 2000
num_layer = 4
model = LSTM(input_size, hidden_dim, num_layer, output_size)
loss_function = nn.MSELoss()
# optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
optimizer = optim.RMSprop(model.parameters(), lr=0.001, alpha=0.9)

if use_gpu:
    model = model.cuda()

root_path = "dataset"
data_num = 100
time_step = 5
datalist = create_datalist(root_path)
train_data, test_data = create_dataset(data_num, datalist, time_step)
# print(len(train_data)) #17*80
# print(len(test_data))  #17*20

def train(epoch):
    for step, input_data in enumerate(train_data, 1):
        seq = ToVariable(input_data[0])
        outs = ToVariable(input_data[1])
        if use_gpu:
            seq = seq.cuda()
            outs = outs.cuda()
        optimizer.zero_grad()
        model.hidden = model.init_hidden()
        modout = model(seq)
        loss = loss_function(modout, outs)
        loss.backward()
        optimizer.step()
        print("step{}/epoch{}: Loss: {:.4f}".format(step, epoch, loss.data[0]))

        # if step%17 == 0:
        #     # print(modout)
        #     for i in range(len(modout[0])):
        #         if modout[0][i] < 0:
        #             modout[0][i] = 0
        #         if modout[0][i]%1 >0.3:
        #             modout[0][i] = math.ceil(modout[0][i])
        #         else:
        #             modout[0][i] = math.floor(modout[0][i])
        #     # print(modout)
        #     loss_int = loss_function(modout, outs)
        #     print(loss_int.data[0])

def test(epoch):
    avg_psnr = 0
    for batch in test_data:
        input, target = ToVariable(batch[0]), ToVariable(batch[1])
        if use_gpu:
            input = input.cuda()
            target = target.cuda()

        prediction = model(input)
        for i in range(len(prediction[0])):
            if prediction[0][i] < 0:
                prediction[0][i] = 0
            if prediction[0][i]%1 >0.3:
                prediction[0][i] = math.ceil(prediction[0][i])
            else:
                prediction[0][i] = math.floor(prediction[0][i])
                # print(modout)
        mse = loss_function(prediction, target)
        psnr = 10 * log10(1 / mse.data[0])
        avg_psnr += psnr
    print("===> Avg. PSNR: {:.4f} dB".format(avg_psnr / len(test_data)))

def checkpoint(epoch):
    if not os.path.exists("checkpoints"):
        os.mkdir("checkpoints")
    if not os.path.exists(os.path.join("checkpoints", "try")):
        os.mkdir(os.path.join("checkpoints", "try"))
    model_out_path = "checkpoints/try/model_epoch_{}.pth".format(epoch)
    torch.save(model, model_out_path)
    print("Checkpoint saved to {}".format("checkpoints" + "try"))

nEpochs = 1
for epoch in range(1, nEpochs+1):
    train(epoch)
    # if epoch % 5 == 0:
    #     checkpoint(epoch)

predDat = []
model = model.eval()
for step, data in enumerate(test_data, 1):
    seq = ToVariable(data[0])
    trueVal = ToVariable(data[1])
    if use_gpu:
        seq = seq.cuda()
        trueVal = trueVal.cuda()
    predDat = model(seq)
    for i in range(len(predDat[0])):
        if predDat[0][i] < 0:
            predDat[0][i] = 0
        if predDat[0][i] % 1 > 0.3:
            predDat[0][i] = math.ceil(predDat[0][i])
        else:
            predDat[0][i] = math.floor(predDat[0][i])
    loss_int = loss_function(predDat, trueVal)
    predDat = predDat[-1].data.cpu().numpy()
    print(predDat)
    print(loss_int.data[0])
    dataframe = pd.DataFrame(predDat)
    num_excel = math.ceil(step//17)
    time = step % 17 + 5
    if not os.path.exists("result"):
        os.mkdir("result")
    if not os.path.exists(os.path.join("result", "excel{}".format(num_excel))):
        os.mkdir(os.path.join("result", "excel{}".format(num_excel)))
    dataframe.to_csv("result/excel{}/prediction_900_time{}.csv".format(num_excel, time), index=False, sep=',')


print('у нас есть квадратное уравнение a*x**2+b*x+c=0')
a=int(input('a= '))
b=int(input('b= '))
c=int(input('c= '))
discr=b**2 - 4*a*c
if a==0:
    x=(-c)/b
    print('if a=0',x)
else:
    if discr>0:
        print('2 вещественных корня')
        x1=(-b+discr**(1/2))/(2*a)
        x2=(-b-discr**(1/2))/(2*a)
        print(x1,'    ',x2)
    elif discr==0:
        print('1 вещ корень')
        x=(-b)/(2*a)
        print(x)
    elif discr<0:
        k=complex(discr)
        print(k)
        print('два комплексных корня')
        x1=(-b+k**(1/2))/(2*a)
        x2=(-b-k**(1/2))/(2*a)
        print(x1,'    ',x2)


'Tests for ps4a'
from ps4a import getWordScore, getFreqDict, updateHand, isValidWord
from ps4a import loadWords

#
# Test code
# You don't need to understand how this test code works
# (but feel free to look it over!)

# To run these tests, simply run this file (open up in your IDE,
# then run the file as normal)

def test_get_word_score():
    """
    Unit test for get_word_score
    """
    failure = False
    # dictionary of words and scores
    words = {("", 7): 0, ("it", 7): 4, ("was", 7): 18, ("scored", 7): 54,
             ("waybill", 7): 155, ("outgnaw", 7): 127, ("fork", 7): 44,
             ("fork", 4): 94}
    for key in words:
        (word, num) = key
        score = getWordScore(word, num)
        if score != words[(word, num)]:
            print("FAILURE: test_get_word_score()")
            print("\tExpected", words[(word, num)], "points but got '"
                  + str(score) + "' for word '" + word + "', n=" + str(num))
            failure = True
    if not failure:
        print("SUCCESS: test_get_word_score()")

# end of test_get_word_score

def test_update_hand():
    """
    Unit test for update_hand
    """
    # test 1
    hand_orig = {'a': 1, 'q': 1, 'l': 2, 'm': 1, 'u': 1, 'i': 1}
    hand_copy = hand_orig.copy()
    word = "quail"

    hand2 = updateHand(hand_copy, word)
    expected_hand1 = {'l': 1, 'm': 1}
    expected_hand2 = {'a': 0, 'q': 0, 'l': 1, 'm': 1, 'u': 0, 'i': 0}
    if hand2 not in (expected_hand1, expected_hand2):
        print("FAILURE: test_update_hand('" + word + "', "
              + str(hand_orig) + ")")
        print("\tReturned: ", hand2, "\n\t-- but expected:", expected_hand1,
              "or", expected_hand2)

        return  # exit function
    if hand_copy != hand_orig:
        print("FAILURE: test_update_hand('" + word + "', "
              + str(hand_orig) + ")")
        print("\tOriginal hand was", hand_orig)
        print("\tbut implementation of update_hand mutated the original hand!")
        print("\tNow the hand looks like this:", hand_copy)

        return  # exit function

    # test 2
    hand_orig = {'e': 1, 'v': 2, 'n': 1, 'i': 1, 'l': 2}
    hand_copy = hand_orig.copy()
    word = "evil"

    hand2 = updateHand(hand_copy, word)
    expected_hand1 = {'v': 1, 'n': 1, 'l': 1}
    expected_hand2 = {'e': 0, 'v': 1, 'n': 1, 'i': 0, 'l': 1}
    if hand2 not in (expected_hand1, expected_hand2):
        print("FAILURE: test_update_hand('" + word + "', "
              + str(hand_orig) + ")")
        print("\tReturned: ", hand2, "\n\t-- but expected:",
              expected_hand1, "or", expected_hand2)

        return  # exit function

    if hand_copy != hand_orig:
        print("FAILURE: test_update_hand('" + word + "', "
              + str(hand_orig) + ")")
        print("\tOriginal hand was", hand_orig)
        print("\tbut implementation of update_hand mutated the original hand!")
        print("\tNow the hand looks like this:", hand_copy)

        return  # exit function

    # test 3
    hand_orig = {'h': 1, 'e': 1, 'l': 2, 'o': 1}
    hand_copy = hand_orig.copy()
    word = "hello"

    hand2 = updateHand(hand_copy, word)
    expected_hand1 = {}
    expected_hand2 = {'h': 0, 'e': 0, 'l': 0, 'o': 0}
    if hand2 not in (expected_hand1, expected_hand2):
        print("FAILURE: test_update_hand('" + word + "', "
              + str(hand_orig) + ")")
        print("\tReturned: ", hand2, "\n\t-- but expected:", expected_hand1,
              "or", expected_hand2)

        return  # exit function

    if hand_copy != hand_orig:
        print("FAILURE: test_update_hand('" + word + "', "
              + str(hand_orig) + ")")
        print("\tOriginal hand was", hand_orig)
        print("\tbut implementation of update_hand mutated the original hand!")
        print("\tNow the hand looks like this:", hand_copy)

        return  # exit function

    print("SUCCESS: test_update_hand()")

# end of test_update_hand

def test_is_valid_word(word_list):
    """
    Unit test for is_valid_word
    """
    failure = False
    # test 1
    word = "hello"
    hand_orig = getFreqDict(word)
    hand_copy = hand_orig.copy()

    if not isValidWord(word, hand_copy, word_list):
        print("FAILURE: test_is_valid_word()")
        print("\tExpected True, but got False for word: '" + word
              + "' and hand:", hand_orig)

        failure = True

    # Test a second time to see if word_list or hand has been modified
    if not isValidWord(word, hand_copy, word_list):
        print("FAILURE: test_is_valid_word()")

        if hand_copy != hand_orig:
            print("\tTesting word", word,
                  "for a second time - be sure you're not modifying hand.")
            print("\tAt this point, hand ought to be", hand_orig,
                  "but it is", hand_copy)

        else:
            print("\tTesting word", word,
                  "for a second time - have you modified word_list?")
            word_in_wl = word in word_list
            print("The word", word, "should be in word_list - is it?",
                  word_in_wl)

        print("\tExpected True, but got False for word: '"
              + word + "' and hand:", hand_copy)

        failure = True

    # test 2
    hand = {'r': 1, 'a': 3, 'p': 2, 'e': 1, 't': 1, 'u': 1}
    word = "rapture"

    if isValidWord(word, hand, word_list):
        print("FAILURE: test_is_valid_word()")
        print("\tExpected False, but got True for word: '"
              + word + "' and hand:", hand)

        failure = True

    # test 3
    hand = {'n': 1, 'h': 1, 'o': 1, 'y': 1, 'd': 1, 'w': 1, 'e': 2}
    word = "honey"

    if not isValidWord(word, hand, word_list):
        print("FAILURE: test_is_valid_word()")
        print("\tExpected True, but got False for word: '"
              + word + "' and hand:", hand)

        failure = True

    # test 4
    hand = {'r': 1, 'a': 3, 'p': 2, 't': 1, 'u': 2}
    word = "honey"

    if isValidWord(word, hand, word_list):
        print("FAILURE: test_is_valid_word()")
        print("\tExpected False, but got True for word: '"
              + word + "' and hand:", hand)

        failure = True

    # test 5
    hand = {'e': 1, 'v': 2, 'n': 1, 'i': 1, 'l': 2}
    word = "evil"

    if not isValidWord(word, hand, word_list):
        print("FAILURE: test_is_valid_word()")
        print("\tExpected True, but got False for word: '"
              + word + "' and hand:", hand)

        failure = True

    # test 6
    word = "even"

    if isValidWord(word, hand, word_list):
        print("FAILURE: test_is_valid_word()")
        print("\tExpected False, but got True for word: '"
              + word + "' and hand:", hand)
        print("\t(If this is the only failure, make sure is_valid_word()"
              + " isn't mutating its inputs)")

        failure = True

    if not failure:
        print("SUCCESS: test_is_valid_word()")

WORD_LIST = loadWords()
print("----------------------------------------------------------------------")
print("Testing get_word_score...")
test_get_word_score()
print("----------------------------------------------------------------------")
print("Testing update_hand...")
test_update_hand()
print("----------------------------------------------------------------------")
print("Testing is_valid_word...")
test_is_valid_word(WORD_LIST)
print("----------------------------------------------------------------------")
print("All done!")


import asyncio

class AsyncContextManager:
    def __init__(self):
        self.conn = None

    async def do_something(self):
        # 异步操作数据库
        return 666

    async def __aenter__(self):
        print(123)
        # 异步链接数据库
        self.conn = await asyncio.sleep(1)
        return self

    async def __aexit__(self, exc_type, exc, tb):
        print(456)
        # 异步关闭数据库链接
        await asyncio.sleep(1)

async def func():
    async with AsyncContextManager() as f:
        result = await f.do_something()
        print(result)

if __name__ == '__main__':
    asyncio.run(func())


from app.models import db, BaseModel

class RoleModel(BaseModel):
    """User's role"""
    __tablename__ = 'core_role'
    __bind_key__ = 'default'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), unique=False, nullable=False)


'''
Merge Two Binary Trees by doing Node Sum (Recursive and Iterative)
2.5
Given two binary trees. We need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then
sum node values up as the new value of the merged node. Otherwise, the non-null node will be used as the node of new tree.

Example:

Input:
     Tree 1            Tree 2
       2                 3
      / \               / \
     1   4             6   1
    /                   \   \
   5                     2   7

Output: Merged tree:
         5
        / \
       7   5
      / \   \
     5   2   7
Note: The merging process must start from the root nodes of both trees.
'''

import CdataS.BT.BianrySearchTree as tree

def mergetree(root1, root2):
    if root1 == None and root2 == None:
        return
    if root1 and root2 == None:
        out = tree.Node(root1.value)
        out.left_child = mergetree(root1.left_child, None)
        out.right_child = mergetree(root1.right_child, None)
    if root2 and root1 == None:
        out = tree.Node(root2.value)
        out.left_child = mergetree(None, root2.left_child)
        out.right_child = mergetree(None, root2.right_child)
    if root1 and root2:
        out = tree.Node(root1.value + root2.value)
        out.left_child = mergetree(root1.left_child, root2.left_child)
        out.right_child = mergetree(root1.right_child, root2.right_child)
    return out

bst1 = tree.BST()
bst2 = tree.BST()
bst1.insert(4)
bst1.insert(2)
bst1.insert(1)
bst1.insert(5)

bst2.insert(3)
bst2.insert(1)
bst2.insert(2)
bst2.insert(6)
bst2.insert(7)
mergetree(bst1.root, bst2.root)


N = int(input("ENTER THE NUMBER : "))
l = []
for i in range(N):
        s = list(input().split())
        if s[0]=='insert':
            l.insert(int(s[1]),int(s[2]))
        if s[0]=='remove':
            l.remove(int(s[1]))
        if s[0]=='append':
            l.append(int(s[1]))
        if s[0]=='sort':
            l.sort()
        if s[0]=='pop':
            l.pop()
        if s[0]=='reverse':
            l.reverse()
        if s[0]=='print':
            print(l)


'''
map(), filter, and reduce() in Python. These functions are most commonly used with Lambda function.
"Lambda functions are functions that do have any name".
'''

## SYNTAX:
## map(function, iterable)

# items = [1, 2, 3, 4, 5]
# even_number=list(map((lambda x: x**2), items))
# print(even_number)

'''
A filter function in Python tests a specific user-defined condition for a function and returns an iterable for
the elements and values that satisfy the condition or, in other words, return true.
'''

## SYNTAX:
## filter(function, iterable)

# a = [1,2,3,4,5,6]
# b = [2,5,0,7,3]
# c= list(filter(lambda x: x in a, b))
# print(c)

# Reduce function works same as MAP and FILTER but we have to import it first
# *IMP* Reduce function works on two input at a time
from functools import reduce
a = [4, 5, 6]
sum_numbers = reduce(lambda x, y: x + y, a)
print(sum_numbers)


'''
    1. Don't delete anything which is already there in code.
    2. you can create your helper functions to solve the task and call them.
    3. Don't change the name of already existing functions.
    4. Don't change the argument of any function.
    5. Don't import any other python modules.
    6. Find in-line function comments.

'''

import gym
import numpy as np
import math
import time
import argparse
import matplotlib.pyplot as plt

class sarsaAgent():
    '''
    - constructor: graded
    - Don't change the argument of constructor.
    - You need to initialize epsilon_T1, epsilon_T2, learning_rate_T1, learning_rate_T2 and weight_T1, weights_T2 for task-1 and task-2 respectively.
    - Use constant values for epsilon_T1, epsilon_T2, learning_rate_T1, learning_rate_T2.
    - You can add more instance variable if you feel like.
    - upper bound and lower bound are for the state (position, velocity).
    - Don't change the number of training and testing episodes.
    '''

    def __init__(self):
        self.env = gym.make('MountainCar-v0')
         # Custom attributes
        self.d_granularity = 8
        self.v_granularity = 8
        self.d_granule = (self.env.observation_space.high[0] - self.env.observation_space.low[0])/self.d_granularity
        self.v_granule = (self.env.observation_space.high[1] - self.env.observation_space.low[1])/self.v_granularity

        self.num_tilings = 8
        self.d_tile_width = self.d_granule
        self.v_tile_width = self.v_granule
        self.d_tiles = self.d_granularity + 1
        self.v_tiles = self.v_granularity + 1
        self.offset = 1/self.num_tilings

        # Predefined attributes
        self.epsilon_T1 = 0.02
        self.epsilon_T2 = 0.03
        self.learning_rate_T1 = 1e-2
        self.learning_rate_T2 = 1e-2
        self.weights_T1 = np.zeros(((self.d_granularity + 1)*(self.v_granularity + 1), 3))
        self.weights_T2 = np.zeros((self.num_tilings*self.d_tiles*self.v_tiles, 3))
        self.discount = 1.0
        self.train_num_episodes = 10000
        self.test_num_episodes = 100
        self.upper_bounds = [self.env.observation_space.high[0], self.env.observation_space.high[1]]
        self.lower_bounds = [self.env.observation_space.low[0], self.env.observation_space.low[1]]

    '''
    - get_table_features: Graded
    - Use this function to solve the Task-1
    - It should return representation of state.
    '''
    def get_table_features(self, obs):
        # Find the discretized state value
        a = int((obs[0] - self.lower_bounds[0])/self.d_granule)
        b = int((obs[1] - self.lower_bounds[1])/self.v_granule)
        # Set the corresponding entry to 1
        entry = np.zeros(((self.d_granularity + 1)*(self.v_granularity + 1), 1))
        entry[a*(self.d_granularity + 1) + b] = 1
        return entry

    '''
    - get_better_features: Graded
    - Use this function to solve the Task-2
    - It should return representation of state.
    '''
    def get_better_features(self, obs):
        # State has dimension of total number of tiles
        state = np.zeros((self.num_tilings*self.d_tiles*self.v_tiles, 1))
        # Find the tiles overlapping with the input
        d_array = np.array([range(self.num_tilings)])*self.offset*self.d_tile_width
        v_array = np.array([range(self.num_tilings)])*self.offset*self.v_tile_width
        a = (((obs[0] - self.lower_bounds[0] - d_array)/self.d_tile_width).astype('int') + 1)*self.d_tiles
        b = ((obs[1] - self.lower_bounds[1] - v_array)/self.v_tile_width).astype('int') + 1
        c = np.array([range(self.num_tilings)])*self.d_tiles*self.v_tiles
        # Set the corresponding tiles in the state to 1
        state[a + b + c] = 1
        return state

    '''
    - choose_action: Graded.
    - Implement this function in such a way that it will be common for both task-1 and task-2.
    - This function should return a valid action.
    - state representation, weights, epsilon are set according to the task. you need not worry about that.
    '''
    def choose_action(self, state, weights, epsilon):
        # Get action using epsilon greedy
        if np.random.rand() < epsilon:
            return np.random.choice(range(3))
        # Get max Q(s, a)
        best_action = np.argmax(np.matmul(weights.T, state))
        return best_action

    '''
    - sarsa_update: Graded.
    - Implement this function in such a way that it will be common for both task-1 and task-2.
    - This function will return the updated weights.
    - use sarsa(0) update as taught in class.
    - state representation, new state representation, weights, learning rate are set according to the task i.e. task-1 or task-2.
    '''
    def sarsa_update(self, state, action, reward, new_state, new_action, learning_rate, weights):
        # Get new and old Q values
        new_q = np.matmul(weights[:, new_action:new_action + 1].T, new_state)
        old_q = np.matmul(weights[:, action:action + 1].T, state)
        # Find the target based on SARSA rule
        target = reward + self.discount*new_q
        # Update the corresponding weights
        weights[:, action:action  + 1] += learning_rate*float(target - old_q)*state
        return weights

    '''
    - train: Ungraded.
    - Don't change anything in this function.

    '''

    def train(self, task='T1'):
        if (task == 'T1'):
            get_features = self.get_table_features
            weights = self.weights_T1
            epsilon = self.epsilon_T1
            learning_rate = self.learning_rate_T1
        else:
            get_features = self.get_better_features
            weights = self.weights_T2
            epsilon = self.epsilon_T2
            learning_rate = self.learning_rate_T2
        reward_list = []
        plt.clf()
        plt.cla()
        for e in range(self.train_num_episodes):
            current_state = get_features(self.env.reset())
            done = False
            t = 0
            new_action = self.choose_action(current_state, weights, epsilon)
            while not done:
                action = new_action
                obs, reward, done, _ = self.env.step(action)
                new_state = get_features(obs)
                new_action = self.choose_action(new_state, weights, epsilon)
                weights = self.sarsa_update(current_state, action, reward, new_state, new_action, learning_rate,
                                            weights)
                current_state = new_state
                if done:
                    reward_list.append(-t)
                    break
                t += 1
            # print(f"Epoch: {e:05}, Reward = {-t}", end = '\r')
        self.save_data(task)
        reward_list=[np.mean(reward_list[i-100:i]) for i in range(100,len(reward_list))]
        plt.plot(reward_list)
        plt.savefig(task + '.jpg')

    '''
       - load_data: Ungraded.
       - Don't change anything in this function.
    '''

    def load_data(self, task):
        return np.load(task + '.npy')

    '''
       - save_data: Ungraded.
       - Don't change anything in this function.
    '''

    def save_data(self, task):
        if (task == 'T1'):
            with open(task + '.npy', 'wb') as f:
                np.save(f, self.weights_T1)
            f.close()
        else:
            with open(task + '.npy', 'wb') as f:
                np.save(f, self.weights_T2)
            f.close()

    '''
    - test: Ungraded.
    - Don't change anything in this function.
    '''

    def test(self, task='T1'):
        if (task == 'T1'):
            get_features = self.get_table_features
        else:
            get_features = self.get_better_features
        weights = self.load_data(task)
        reward_list = []
        for e in range(self.test_num_episodes):
            current_state = get_features(self.env.reset())
            done = False
            t = 0
            while not done:
                action = self.choose_action(current_state, weights, 0)
                obs, reward, done, _ = self.env.step(action)
                new_state = get_features(obs)
                current_state = new_state
                if done:
                    reward_list.append(-1.0 * t)
                    break
                t += 1
        return float(np.mean(reward_list))

if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("--task", required=True,
       help="first operand", choices={"T1", "T2"})
    ap.add_argument("--train", required=True,
       help="second operand", choices={"0", "1"})
    args = vars(ap.parse_args())
    task=args['task']
    train=int(args['train'])
    agent = sarsaAgent()
    agent.env.seed(0)
    np.random.seed(0)
    agent.env.action_space.seed(0)
    if(train):
        agent.train(task)
    else:
        print(agent.test(task))


from openerp import models, fields, api, _
from openerp.tools.translate import _
from openerp.exceptions import Warning
from openerp import models, api
import time

import logging
_logger = logging.getLogger(__name__)

class account_invoice_ext_api(models.Model):
    _inherit = 'account.transfer'
    _order = 'date desc'

    @api.depends('source_journal_id', 'target_journal_id')
    def _compute_misma_ue(self):
        for record in self:
            ue_source = False
            ue_target = False
            if record.source_journal_id.operating_unit_id:
                ue_source = record.source_journal_id.operating_unit_id.id
            if record.target_journal_id.operating_unit_id:
                ue_target = record.target_journal_id.operating_unit_id.id
            if ue_source and ue_target and (ue_source == ue_target):
                record.misma_ue = True
            else:
                record.misma_ue = False

    move_id = fields.Many2one(
        'account.move',
        'Asiento',
    )

    misma_ue = fields.Boolean(
        string=u'Misma UE',
        compute='_compute_misma_ue',
    )

    # TODO: SPRING 4 GAP 247
    entry_date = fields.Date(
        string=u'Fecha', default=lambda *a: fields.Date.today(),
        readonly=True,
        states={'draft': [('readonly', False)]},
    )

    affectation_number = fields.Char(
        size=15)

    @api.multi
    def action_show_field(self):
        self.ensure_one()
        # OPEN POPUP
        wizard_obj = self.env['account.move.affectation.number.transfer']
        wizard_id = wizard_obj.create({
            'affectation_number': self.affectation_number,
            'transfer_id': self.id,
            'prev_number': self.affectation_number
        }).id
        action_dict = \
        self.env.ref('grp_tesoreria.change_affectation_number_action_transfer').read()[0]
        action_dict['res_id'] = wizard_id
        return action_dict

    # TODO: SPRING 4 GAP 247
    @api.onchange('entry_date')
    def _onchange_entry_date(self):
        for record in self:
            if record.entry_date:
                record.date = record.entry_date

    @api.multi
    def action_confirm(self):
        self.ensure_one()

        if self.amount <= 0.0:
            raise Warning(_('Amount must be greater than 0!'))

        # set date if not configured
        if not self.date:
            self.date = fields.Date.context_today(self)

        # set period if not configured
        period = self.period_id
        if not self.period_id:
            period = period.with_context(
                company_id=self.company_id.id).find(
                self.date)[:1]
            if not period:
                raise Warning(_('Not period found for current date'))
            self.period_id = period.id

        ue_source = False
        ue_target = False
        if self.source_journal_id.operating_unit_id:
            ue_source = self.source_journal_id.operating_unit_id.id
        if self.target_journal_id.operating_unit_id:
            ue_target = self.target_journal_id.operating_unit_id.id

        if ue_source and ue_target and (ue_source == ue_target):
            # create move
            move = self.source_move_id.create(self.get_move_vals('ue'))

            self.write({
                'move_id': move.id,
                'state': 'confirmed',
                })
        else:
            # create source move
            source_move = self.source_move_id.create(self.get_move_vals('source'))

            # create target move
            target_move = self.target_move_id.create(self.get_move_vals('target'))

            self.write({
                'target_move_id': target_move.id,
                'source_move_id': source_move.id,
                'state': 'confirmed',
                })

    @api.multi
    def get_move_vals(self, move_type):
        self.ensure_one()
        company_currency = self.company_id.currency_id

        if move_type not in ['ue']:
            transfer_account = self.company_id.transfer_account_id
            if not transfer_account:
                raise Warning(_(
                    'No transfer account configured con company %s!') % (
                    self.source_journal_id.company_id.name))

        if move_type == 'source':
        # TODO: C INCIDENCIA
            ref = _('%s (Origen)' % self.ref)
            journal = self.source_journal_id
            first_account = journal.default_debit_account_id
            second_account = transfer_account
            operating_unit_id = self.source_journal_id.operating_unit_id.id
        if move_type == 'target':
       # TODO: C INCIDENCIA
            ref = _('%s (Destino)' % self.ref)
            journal = self.target_journal_id
            first_account = transfer_account
            second_account = journal.default_credit_account_id
            operating_unit_id = self.target_journal_id.operating_unit_id.id

        # Se agrega el caso en que los diarios origen y destino tienen la misma ue
        if move_type == 'ue':
            ref = _(u'%s (Asiento único)' % self.ref)
            first_account = self.source_journal_id.default_credit_account_id
            second_account = self.target_journal_id.default_debit_account_id
            journal = self.source_journal_id

        name = journal.sequence_id.with_context(fiscalyear_id=self.period_id.fiscalyear_id.id)._next()
        move_vals = {
            'ref': ref,
            'name': name,
            'nro_afectacion_siif':self.affectation_number,
            'period_id': self.period_id.id,
            'date': self.date,
            'journal_id': journal.id,
            'company_id': self.company_id.id,
            }

        source_currency = self.source_journal_id.currency or company_currency
        diff_currency = source_currency != company_currency

        company_currency_amount = diff_currency and source_currency.with_context(date=self.date).compute(self.amount, company_currency) or self.amount

        first_line_vals = {
            'name': name,
            'debit': 0.0,
            'credit': company_currency_amount,
            'account_id': first_account.id,
            'currency_id': (diff_currency and first_account.currency_id and first_account.currency_id.id == self.source_journal_id.currency.id) and source_currency.id or False,
            'amount_currency': (diff_currency and first_account.currency_id and first_account.currency_id.id == self.source_journal_id.currency.id) and -1 * self.amount or False,
        }
        currency = second_account.currency_id or company_currency
        second_line_vals = {
            'name': name,
            'debit': company_currency_amount,
            'credit': 0.0,
            'account_id': second_account.id,
            'currency_id': (currency and currency.id != company_currency.id) and currency.id or False,
            'amount_currency': (currency and currency.id != company_currency.id) and source_currency.with_context(date=self.date).compute(self.amount, currency) or False,
        }
        if move_type == 'ue':
            move_vals.update({'operating_unit_id': self.source_journal_id.operating_unit_id.id})
        elif move_type in ['source', 'target']:
            move_vals.update({'operating_unit_id': operating_unit_id})
        move_vals['line_id'] = [
            (0, _, first_line_vals), (0, _, second_line_vals)]
        return move_vals

    # TODO: SPRING 10 GAP 493 C
    @api.one
    def action_cancel(self):
        if self.state == 'confirmed':
            # TODO: L Generando extornos
            period_id = self.env['account.period'].find(fields.Date.today()).id
            if self.source_move_id and self.target_move_id:
                self.source_move_id.create_reversals(
                    fields.Date.today(),
                    reversal_period_id=period_id,
                )
                self.target_move_id.create_reversals(
                    fields.Date.today(),
                    reversal_period_id=period_id,
                )
            elif self.move_id:
                self.move_id.create_reversals(
                    fields.Date.today(),
                    reversal_period_id=period_id,
                )
            self.write({'state': 'cancel', 'source_move_id': False, 'target_move_id': False, 'move_id': False})
        else:
            return super(account_invoice_ext_api, self).action_cancel()


import pandas as pd
import re
import datetime
import random

# display the full contents of a column
pd.set_option('display.max_colwidth', None)

# load CSV data
df = pd.read_csv('jeopardy.csv')

# remove hashtag to investigate data contents
#print(df.info())
#print(df.head())

# Data Cleaning

# clean up column names: add underscores and lowercase to allow dot notation
df.rename(columns={
  'Show Number': 'show_number',
  ' Air Date': 'air_date',
  ' Round': 'round',
  ' Category': 'category',
  ' Value': 'value',
  ' Question': 'question',
  ' Answer': 'answer'},
  inplace=True)

# fix value column to be straight floats with no $'s commas or "None"
df.value = df.value.apply(lambda x: float(x.strip('$').replace(',', '')) if x != 'None' else 0)

# change air_date column in datetime format instead of string
df.air_date = df.air_date.apply(lambda x: pd.to_datetime(x))

# remove hypertext reference from question column
df.question = df.question.apply(lambda x: re.sub(re.compile('<.*?>'), '', x))

# change category and answer columns in lowercase for further easy search
df.category = df.category.apply(lambda x: x.lower())
df.answer = df.answer.apply(lambda x: str(x).lower())

# Data Analyzing

# filter dataset to only include rows with questions that contain all words in the user inputted list
def word_filter(dataset, words):
    filter = lambda x: all(word.lower() in x.lower() for word in words)
    return dataset.loc[dataset.question.apply(filter)]

# remove hashtags to test word_filter function
#words1 = ['King', 'England']
#print(word_filter(df, words1))

# find the average difficulty of rounds based on value column
mean_difficulty = df.groupby('round').value.mean().reset_index()
# remove hashtags to observe mean_difficulty
#print(mean_difficulty)

# calculate average value of filtered dataset
def average_value(dataset, words):
  filtered_dataset = word_filter(dataset, words)
  return filtered_dataset.value.mean()

# remove hashtags to test average_value function
#words2 = ['King']
#print(average_value(df, words2))

# calculate unique answers counts in filtered dataset
def unique_answers(dataset, words):
  filtered_dataset = word_filter(dataset, words)
  return filtered_dataset.answer.value_counts().reset_index()

# remove hashtags to test unique_answers function
#words3 = ['King']
#print(unique_answers(df, words3))

# compare computer use in 90s vs 2000s
filtered_by_computer = word_filter(df, ['Computer'])
filtered_by_computer_90s = filtered_by_computer[(filtered_by_computer.air_date > datetime.datetime(1990, 1, 1)) & (filtered_by_computer.air_date < datetime.datetime(1999, 12, 31))]
filtered_by_computer_00s = filtered_by_computer[(filtered_by_computer.air_date > datetime.datetime(2000, 1, 1)) & (filtered_by_computer.air_date < datetime.datetime (2009, 12, 31))]

# remove hashtags to see how computer use has changed
#print('Computer use has changed from {} in 90s to {} in 00s.'.format(str(len(filtered_by_computer_90s)), str(len(filtered_by_computer_00s))))

# Jeopardy Game!

def play_game(dataset):
  yes_or_no = "yes"
  correct = 0
  incorrect = 0

  while (yes_or_no == "yes"):
    random_index = random.randint(0, 216930)
    question = dataset.question.iloc[random_index]
    answer = dataset.answer.iloc[random_index]
    print(question)
    user_input = input('And your answer is: ')

    if user_input.lower() == answer.lower():
      print('Good job! You got it right!')
      correct += 1
      score = "Correct: " + str(correct) + ' vs Incorrect: ' + str(incorrect)
      print('Current Score: ' + score)
      yes_or_no = input('Want to continue? (yes/no): ' )
    else:
      print('Oops. You missed! The correct answer was: ' + answer)
      incorrect += 1
      score = "Correct: " + str(correct) + ' vs Incorrect: ' + str(incorrect)
      print('Current Score: ' + score)
      yes_or_no = input('Want to continue? (yes/no): ' )
  return 'Final Score: ' + score

# Let's play Jeopardy!
play_game(df)


#!/usr/bin/env python3
import time
import os
import sys
import configparser
import subprocess
import filecmp
import datetime

mainpy = os.path.abspath(os.path.join(
    os.path.dirname(os.path.abspath(__file__)), "../../main.py"))

def compare(sample, ref):
    return filecmp.cmp(sample, ref)

def fast_parser_test(ref_ir, ref_w, cfg=None, log=None, bk_ref_ir=None):
    if cfg is None:
        cfg = "g.cfg"
    if log is None:
        log = sys.stdout
    else:
        log = open(log, "w")
        log.write(datetime.datetime.now().strftime("%Y_%m_%d-%H_%M_%S")+"\n")
    config = configparser.ConfigParser()
    config.read(cfg)
    outdir = config["Common"]["output_dir"]
    if "model_type" in config["Common"]:
        framework = config["Common"]["model_type"]
    else:
        framework = "tf"
    model_name = config["Common"]["model_name"]
    name = framework + '_' + model_name
    out_ir = os.path.join(outdir, model_name+".txt")
    out_w = os.path.join(outdir, model_name+".bin")
    cmd = ["python3", mainpy, "-c", cfg]
    print("testing ", name)
    pid = subprocess.Popen(cmd, stdout=log, stderr=log)
    retcode = pid.wait()
    if retcode != 0:
        print("Parser error!! %s IR generate failed" % name)
        sys.exit(-1)
    wok = compare(out_w, ref_w)
    ok = compare(out_ir, ref_ir)
    if not ok and bk_ref_ir is not None and os.path.exists(bk_ref_ir):
        print("%s using backup ref!" % name)
        ok = compare(out_ir, bk_ref_ir)
    if not ok:
        print("Parser error!! %s IR changed!" % name)
        sys.exit(-1)
    if not wok:
        if model_name != "transformer_mini":
            print("Parser error!! %s Weight file changed!" % name)
            sys.exit(-1)
    sys.exit(0)

if __name__ == "__main__":
    fast_parser_test(sys.argv[1], sys.argv[2],
                     sys.argv[3], sys.argv[4], sys.argv[5])


import rospy
from geometry_msgs.msg import PoseStamped, TwistStamped
from nav_msgs.msg import Odometry
from mavros_msgs.srv import CommandBool, SetMode
from mavros_msgs.msg import OverrideRCIn
import numpy as np
import time

# NOTE: this only works for one particular angle convention
def euler_to_quaternion(a):
    x = np.cos(a[0]/2.)*np.cos(a[1]/2.)*np.cos(a[2]/2.) + np.sin(a[0]/2.)*np.sin(a[1]/2.)*np.sin(a[2]/2.)
    y = np.sin(a[0]/2.)*np.cos(a[1]/2.)*np.cos(a[2]/2.) - np.cos(a[0]/2.)*np.sin(a[1]/2.)*np.sin(a[2]/2.)
    z = np.cos(a[0]/2.)*np.sin(a[1]/2.)*np.cos(a[2]/2.) + np.sin(a[0]/2.)*np.cos(a[1]/2.)*np.sin(a[2]/2.)
    w = np.cos(a[0]/2.)*np.cos(a[1]/2.)*np.sin(a[2]/2.) - np.sin(a[0]/2.)*np.sin(a[1]/2.)*np.cos(a[2]/2.)

    return [x,y,z,w]

def yaw_to_quaternion(yaw):
    x = np.cos(yaw/2.)
    y = 0
    z = 0
    w = np.sin(yaw/2.)

    return [x,y,z,w]

class Quadcopter(object):

    def __init__(self, disable_signals=False, control_style='position',
                 do_arming=False):
        self.control_style = control_style

        self.position = [0,0,0]
        self.vel = [0,0,0]

        self.orientation = [0,0,0,0]
        self.ang_vel = [0,0,0]

        rospy.init_node('quadcopter', anonymous=True,
                        disable_signals=disable_signals)

        # Only arm stuff if the flag is set. Otherwise assume it has already
        # been taken care of elsewhere and is good to go
        if do_arming:
            rospy.wait_for_service('/mavros/cmd/arming')
            rospy.wait_for_service('/mavros/set_mode')

            self.arm_service = rospy.ServiceProxy('/mavros/cmd/arming', CommandBool)
            self.mode_service = rospy.ServiceProxy('/mavros/set_mode', SetMode)

        if self.control_style == 'position':
            if do_arming:
                # Set the mode to GUIDED
                self.mode_service(0, 'GUIDED')

            self.waypoint_msg = PoseStamped()

            self.pub_waypoint = rospy.Publisher('/mavros/setpoint_position/local',
                                                PoseStamped,
                                                queue_size=10
                                               )
        elif self.control_style == 'velocity':
            if do_arming:
                # Set the mode to STABILIZE
                #self.mode_service(0, 'STABILIZE')
                self.mode_service(0, 'GUIDED')

            self.velocity_msg = TwistStamped()

            self.pub_velocity = rospy.Publisher('/mavros/setpoint_velocity/cmd_vel',
                                                TwistStamped,
                                                queue_size=10
                                               )
        elif self.control_style == 'alt_hold':
            if do_arming:
                # Set the mode to ALT_HOLD
                self.mode_service(0, 'ALT_HOLD')

            self.rc_msg = OverrideRCIn()
            # Initialize all control channels to the centered position
            self.rc_msg.channels[0] = 1500
            self.rc_msg.channels[1] = 1500
            self.rc_msg.channels[2] = 1500
            # Initialize all unused channels to 'ignore and use previous value'
            self.rc_msg.channels[3] = 65535
            self.rc_msg.channels[4] = 65535
            self.rc_msg.channels[5] = 65535
            self.rc_msg.channels[6] = 65535
            self.rc_msg.channels[7] = 65535

            self.pub_rc = rospy.Publisher('/mavros/rc/override',
                                          OverrideRCIn,
                                          queue_size=10
                                         )

        self.sub_odometry = rospy.Subscriber('/mavros/local_position/odom',
                                             Odometry,
                                             self.odo_callback
                                            )

        if do_arming:
            # Arm the quadcopter
            self.arm_service(True)

            if self.control_style == 'alt_hold':
                # publish an initial message to prevent the motors from automatically disarming
                self.rc_msg.channels[3] = 1600 # Make it move up a bit to prevent disarm
                self.pub_rc.publish(self.rc_msg)
                time.sleep(.5) # space the messages out slightly
                self.rc_msg.channels[3] = 1600 # Level it out again
                self.pub_rc.publish(self.rc_msg)

        self.step = 0

    def odo_callback(self, data):

        self.position[0] = data.pose.pose.position.x
        self.position[1] = data.pose.pose.position.y
        self.position[2] = data.pose.pose.position.z
        self.vel = data.twist.twist.linear

        self.orientation = data.pose.pose.orientation
        self.ang_vel = data.twist.twist.angular

    def disarm(self):
        """
        Make sure the quadcopter gets disarmed. Might be other things that
        need to happen here in the future in case calling the service fails
        """

        self.arm_service(False)

    def __call__(self, t, x):
        if self.step >= 100:
            if self.control_style == 'position':
                self.waypoint_msg.pose.position.x = x[0]
                self.waypoint_msg.pose.position.y = x[1]
                self.waypoint_msg.pose.position.z = x[2]
                if len(x) > 3:
                    # Set the yaw using quaternions
                    self.waypoint_msg.pose.orientation.x = np.cos(x[3]/2.)
                    self.waypoint_msg.pose.orientation.w = np.sin(x[3]/2.)

                self.pub_waypoint.publish(self.waypoint_msg)
            elif self.control_style == 'velocity':
                self.velocity_msg.twist.linear.x = x[0]
                self.velocity_msg.twist.linear.y = x[1]
                self.velocity_msg.twist.linear.z = x[2]
                if len(x) > 3:
                    self.velocity_msg.twist.angular.z = x[3]

                self.pub_velocity.publish(self.velocity_msg)
            elif self.control_style == 'alt_hold':
                # Scale an input between -1 and 1 to 1000, 2000
                # Make sure the input does not exceed those bounds
                self.rc_msg.channels[0] = max(1000,min(x[0]*500+1500,2000))
                self.rc_msg.channels[1] = max(1000,min(x[1]*500+1500,2000))
                self.rc_msg.channels[2] = max(1000,min(x[2]*500+1500,2000))

                self.pub_rc.publish(self.rc_msg)

            # Put the kill switch as the 5th element for now
            if len(x) > 4:
                if x[4] > .2:
                    self.disarm()

            self.step = 0
        self.step += 1

        return self.position
        #return [self.position[0], self.position[1], self.position[2]]


from flask import Flask, render_template, redirect, url_for, request, send_from_directory, flash, session, send_file
from flask_bootstrap import Bootstrap
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, SelectField, validators, BooleanField, SelectMultipleField, widgets
from wtforms.validators import DataRequired

#Home page start button, allows user to begin quiz
class BeginQuiz(FlaskForm):
    submit = SubmitField('Start Quiz')

class Question1(FlaskForm):
    genre1 = SelectField(u'Genre', choices = "", validators = [validators.required()])
    next1 = SubmitField('Next')

class Question2(FlaskForm):
    artist1 = StringField('Artist 1')
    artist2 = StringField('Artist 2')
    back2 = SubmitField('Back')
    next2 = SubmitField('Next')

    def validate(self):

        rv = FlaskForm.validate(self)

        if not rv:
            return False

        if len(self.artist1.data) == 0 and len(self.artist2.data) == 0 and self.next2.data:
            self.artist1.errors.append('Please add an artists')
            self.artist2.errors.append('Please add an artists')
            return False

        elif len(self.artist1.data) == 0 and self.next2.data:
            self.artist1.errors.append('Please add an artist')
            return False

        elif len(self.artist2.data) == 0 and self.next2.data:
            self.artist1.errors.append('Please add an artist')
            return False

        return True

class MultiCheckboxField(SelectMultipleField):
    widget = widgets.ListWidget(prefix_label=False)
    option_widget = widgets.CheckboxInput()

class Question3(FlaskForm):

    track_options = MultiCheckboxField('', choices=[], coerce=int, render_kw={'style': 'height: fit-content; list-style: none;'})
    back3 = SubmitField('Back')
    next3 = SubmitField('Next')

    def validate(self):

        rv = FlaskForm.validate(self)

        if not rv:
            return False

        if len(self.track_options.data) != 2 and self.next3.data:
            self.track_options.errors.append('Please select 2 items')
            return False

        return True


from xml.dom import minidom
import urllib.request as urllib2
from bs4 import BeautifulSoup
from tqdm import tqdm
import requests
from nltk import ngrams
import from_ngram_to_vector

"""
scarica le pagine, calcola gli ngrammi, e salva i risultati in questo modo:
"nome pagine	vector "
"""

if __name__ == '__main__':
    file = open("page_vector.txt", "w+")
    PATH_SITE_MAP = 'sitemap.xml'
    xmldoc = minidom.parse(PATH_SITE_MAP)
    itemlist = xmldoc.getElementsByTagName('loc')
    STOP = 0
    for page in tqdm(itemlist):
        page_name = page.childNodes[0].nodeValue
        if not (page_name.endswith("doc") or page_name.endswith(".docx")):
            try:
                page = requests.get(page_name)
                data = page.text
                soup = BeautifulSoup(data, 'html.parser')
                stringa = ""
                tags = (soup.findAll())
                for i in tags:
                    stringa = stringa + " " + (i.name)
                stringa = stringa[1:]
                n_grammi = 10
                decagram = ngrams(stringa.split(), n_grammi)

                grams_list = []
                for grams in decagram:
                    grams_list.append(grams)
                vector = from_ngram_to_vector.from_decagram_to_vector(grams_list)

                file.write(page_name)
                file.write("\t" + str(vector) + "\n")
                STOP = STOP + 1

                if STOP == 2000:
                    break
            except requests.exceptions.RequestException as e:
                print(e)