import nltk

import spacy
import random

import numpy as np
import multiprocessing as mp

from collections import defaultdict
from itertools import islice
from operator import itemgetter
from pysem.utils.multiprocessing import plainmap, PoolContainer
from pysem.utils.vsa import convolve, deconvolve, normalize, HRR

tokenizer = nltk.load('tokenizers/punkt/english.pickle')
stopwords = nltk.corpus.stopwords.words('english')
nlp = spacy.load('en')

class RandomIndexing(object):
    """A base class for embedding models that make use of random indexing
    algorithms. These algorithms assign random unit vectors to all of the
    words in a vocabulary to be modelled. Then, operations on these unit
    vectors are performed while scanning through text to build up useful
    representations of the words in question.

    Specific random indexing models are subclasses of this class that specify
    a unique encoding algorithm to be used to train the model. Subclasses also
    specify model-specific methods for retrieving and manipulating learned
    embeddings in useful ways. The methods in the base class that implement
    training etc. make use of these specifications and thus only operate
    as intended when called via an instance of an appropriately defined
    subclass.

    Parameters:
    ----------
    corpus : datahandler object
        A text corpus to model. The provided object must implement a stream
        that yields corpus articles.
    vocab : list
        A list of strings that define the vocabulary of words to be modelled.

    Attributes:
    ----------
    corpus : datahandler object
        The text corpus to model using random indexing.
    vocab : list
        A list of strings that define the vocabulary of words to be modelled.
    vectors : numpy.ndarray
        An array whose rows correspond to the learned embeddings for each
        vocabulary item. Initialized as an array of zeros when the model is
        trained. The index of a word in the vocab is the same as the index of
        the row in the array containing the word's embedding.
    """
    def __init__(self, corpus, vocab):
        self.corpus = corpus
        self.vocab = vocab

    @property
    def vocab(self):
        return self._vocab

    @vocab.setter
    def vocab(self, wordset):
        randitem = random.choice(wordset)
        if isinstance(wordset, list) and isinstance(randitem, str):
            self._vocab = wordset
            self.word_to_idx = {word: idx for idx, word in enumerate(wordset)}
            self.idx_to_word = {idx: word for idx, word in enumerate(wordset)}
        else:
            raise TypeError('The vocabulary must be a list of strings')

    @staticmethod
    def encode():
        raise NotImplementedError('Random indexing models must implement a \
                                   staticmethod for encoding text corpora')

    @staticmethod
    def preprocess(article):
        '''Convert an article into a list of lowercase sentences'''
        sen_list = tokenizer.tokenize(article)
        sen_list = [s.replace('\n', ' ') for s in sen_list]
        sen_list = [s.translate(readonly.strip_num) for s in sen_list]
        sen_list = [s.translate(readonly.strip_pun) for s in sen_list]
        sen_list = [s.lower() for s in sen_list if len(s) > 5]
        sen_list = [nltk.word_tokenize(s) for s in sen_list]
        sen_list = [[w for w in s if w in readonly.vocab] for s in sen_list]
        return sen_list

    def run_pool(self, function, batch):
        '''Apply an embedding function to batch of articles in parallel'''
        with mp.Pool(processes=mp.cpu_count()) as pool:
            results = pool.map_async(function, batch)

            for encoding in results.get():
                for word, vector in encoding.items():
                    self.vectors[self.word_to_idx[word], :] += vector

    def train(self, dim, batchsize=100):
        '''Train a model with embeddings of the specified dimension'''
        self.dim = dim
        self.batchsize = batchsize
        self.vectors = np.zeros((len(self.vocab), dim))
        self._config_readonly()

        for batch in self._batches():
            sents = plainmap(self.preprocess, batch)
            self.run_pool(self.encode, sents)

        self.normalize()

    def normalize(self):
        '''Normalize the embeddings learned by the model to unit length'''
        norms = np.linalg.norm(self.vectors, axis=1)
        norms[norms == 0] = 1  # avoids divide by zero for words w/o embedding
        self.vectors = np.divide(self.vectors, norms[:, np.newaxis])

    def top_matches(self, probe, n, base_vectors=False):
        '''Return the top n embeddings matching a probe embedding'''
        if base_vectors:
            sims = np.dot(readonly.vectors, probe)
        else:
            sims = np.dot(self.vectors, probe)
        ranked = sorted(enumerate(sims), key=itemgetter(1), reverse=True)
        return [(self.idx_to_word[x[0]], x[1]) for x in ranked[:n]]

    def _config_readonly(self):
        '''Declares a PoolContainer as a global variable for shared
        memory used by all processes in a multiprocessing pool.'''
        global readonly
        readonly = PoolContainer(self.dim, self.vocab)

    def _batches(self):
        '''Use of islice requires that self.corpus.articles is a generator.'''
        while True:
            batch = list(islice(self.corpus.articles, self.batchsize))
            yield batch

            if len(batch) < self.batchsize:
                break

        self.corpus.reset_streams()

class ContextEmbedding(RandomIndexing):
    """A random indexing model that produces word embeddings on the basis of
    word co-occurences within sentences.
    """
    @staticmethod
    def encode(sen_list):
        '''Encode context info for each unique word in a list of sentences'''
        encodings = defaultdict(readonly.zeros)
        for sen in sen_list:
            sen_sum = sum([readonly[w].v for w in sen if w not in stopwords])
            for word in sen:
                word_sum = sen_sum - readonly[word].v
                encodings[word] += word_sum
        return encodings

    def get_nearest(self, word, n=5):
        '''Print the n nearest neighbors to a target word in context space'''
        probe = self.vectors[self.word_to_idx[word], :]
        top_n = self.top_matches(probe, n)
        for item in top_n:
            print(item[0], item[1])

class OrderEmbedding(RandomIndexing):
    """A random indexing model that produces word embeddings on the basis of
    positional word co-occurences within sentences.
    """
    @staticmethod
    def encode(sen_list):
        '''Encode order info for each unique word in a list of sentences'''
        encodings = defaultdict(readonly.zeros)
        win = readonly.winsize
        for sen in sen_list:
            for i in range(len(sen)):
                o_sum = HRR(readonly.zeros())
                for j in range(win):
                    if i+j+1 < len(sen):
                        w = readonly[sen[i+j+1]]
                        p = readonly.pos_idx[j]
                        o_sum += w * p
                    if i-j-1 >= 0:
                        w = readonly[sen[i-j-1]]
                        p = readonly.neg_idx[j]
                        o_sum += w * p
                encodings[sen[i]] += o_sum.v
        return encodings

    def get_completions(self, word, position, n=5):
        '''Print the n most likely words to occur in the specified position
        relative to the provided target word in order space'''
        embedding = self.vectors[self.word_to_idx[word], :]
        if position > 0:
            probe = deconvolve(readonly.pos_idx[position-1].v, embedding)
        else:
            probe = deconvolve(readonly.neg_idx[abs(position+1)].v, embedding)

        top_n = self.top_matches(probe, n, base_vectors=True)
        for item in top_n:
            print(item[0], item[1])

    def get_resonants(self, phrase, n=5):
        '''Print the n most likely words to occur in the specified position
        relative to the provided phrase in order space'''
        probe = self.get_vector_encoding(phrase)
        top_n = self.top_matches(probe, n)
        for item in top_n:
            print(item[0], item[1])

    def get_vector_encoding(self, phrase):
        '''Converts a phrase query into a vector encoding the linguistic
        context surrounding a target position'''
        words = phrase.split()
        index = words.index('__')
        probe = np.zeros(self.dim)
        for word in words:
            if word == '__':
                continue
            w = readonly[word].v
            if words.index(word) < index:
                p = readonly.neg_idx[index-words.index(word)-1].v
                probe += convolve(w, p)
            if words.index(word) > index:
                p = readonly.pos_idx[words.index(word)-index-1].v
                probe += convolve(w, p)

        return normalize(probe)

    def _config_readonly(self):
        '''Configures PoolContainer to include vectors that tag different
        positional contexts around a target word'''
        global readonly
        readonly = PoolContainer(self.dim, self.vocab)
        readonly.build_position_tags()

class SyntaxEmbedding(RandomIndexing):
    """A random indexing model that produces word embeddings on the basis of
    syntactic dependency relations within sentences.
    """
    @staticmethod
    def encode(article):
        doc = nlp(article)
        encodings = defaultdict(readonly.zeros)
        targpos = 'VERB'

        for sen in doc.sents:
            for token in sen:
                word = token.lower_
                if word in readonly.vocab and token.pos_ == targpos:
                    for c in token.children:
                        if c.dep_ in readonly.verb_deps:
                            role = readonly.verb_deps[c.dep_]
                            if c.lower_ in readonly.vocab:
                                filler = readonly[c.lower_].v
                                binding = convolve(role, filler)
                                encodings[word] += binding
        return encodings

    def _config_readonly(self):
        '''Configures PoolContainer to include vectors that tag different
        syntactic neighbors of a target word'''
        global readonly
        readonly = PoolContainer(self.dim, self.vocab)
        readonly.build_dependency_tags()

    def train(self, dim, batchsize=100):
        '''Overrides usual RandomIndexing train method to run multiprocessing
        pools on batches of articles instead of batches of sentences'''
        self.dim = dim
        self.batchsize = batchsize
        self.vectors = np.zeros((len(self.vocab), dim))
        self._config_readonly()

        for batch in self._batches():
            self.run_pool(self.encode, batch)

        self.normalize()

    def get_verb_neighbors(self, word, dep, n=5):
        '''Print the n most likely words to occupy the specified syntactic
        dependency relative to the provided target word in syntax space'''
        embedding = self.vectors[self.word_to_idx[word], :]
        probe = deconvolve(readonly.verb_deps[dep], embedding)
        top_n = self.top_matches(probe, n, base_vectors=True)
        for item in top_n:
            print(item[0], item[1])


# -*- coding: utf-8 -*-
"""
Created on Mon Mar  9 10:05:26 2020

@author: Seta
"""

from collections import defaultdict
import os
import sys
import random
sys.path.append(os.getcwd()+'\\v0_5')

import pandapower as pp
from pandapower import timeseries as ts
from pandapower import control
import pandas as pd
import numpy as np
import time

from v0_5.Prosumer import Prosumer
from v0_5.centralcpu import CPU
from Storage import BatterySimple, BatterySimple
from PVgen import PVgen
from utils.function_repo import parse_hours, timegrid

# ============================================================================
# Create NETWORK

def simple_net():
    net = pp.create_empty_network()
    # Create buses
    pp.create_bus(net, name='Bus ext grid', vn_kv=10., type='b')
    pp.create_bus(net, name='Bus LV0', vn_kv=0.4, type='n')
    for i in range(1, 6):
        pp.create_bus(net, name='Bus LV1.%s' % i, vn_kv=0.4, type='m')
    for i in range(1, 5):
        pp.create_bus(net, name='Bus LV2.%s' % i, vn_kv=0.4, type='m')
    pp.create_bus(net, name='Bus LV2.2.1', vn_kv=0.4, type='m')
    pp.create_bus(net, name='Bus LV2.2.2', vn_kv=0.4, type='m')
    # Create lines
    for i in range(1,6):
        pp.create_line(net, from_bus=i, to_bus=i+1, length_km=0.08, std_type='NAYY 4x120 SE')
    pp.create_line(net, from_bus=1, to_bus=7, length_km=0.12, std_type='NAYY 4x120 SE')
    pp.create_line(net, from_bus=7, to_bus=8, length_km=0.12, std_type='NAYY 4x120 SE')
    for i in range(8, 10):
        pp.create_line(net, from_bus=i, to_bus=i+1, length_km=0.12, std_type='15-AL1/3-ST1A 0.4')
    pp.create_line(net, from_bus=8, to_bus=11, length_km=0.12, std_type='15-AL1/3-ST1A 0.4')
    pp.create_line(net, from_bus=11, to_bus=12, length_km=0.12, std_type='15-AL1/3-ST1A 0.4')
    # Create external grid
    pp.create_ext_grid(net, bus=0, m_pu=1.03, va_degree=0, name='External grid',
                       s_sc_max_mva=10, rx_max=0.1, rx_min=0.1)
    # Create transformer
    pp.create_transformer_from_parameters(net, hv_bus=0, lv_bus=1, sn_mva=.4,
                                          vn_hv_kv=10, vn_lv_kv=0.4, vkr_percent=1.325,
                                          vk_percent=4, pfe_kw=0.95, i0_percent=0.2375,
                                          tap_side="hv", tap_neutral=0, tap_min=-2,
                                          tap_max=2, tap_step_percent=2.5, tp_pos=0,
                                          shift_degree=150, name='MV-LV-Trafo')
    for ind in range(1,13):
        pp.create_load(net, bus=ind,
                        p_mw=0.0035,
                        name='Prosumer %s' % ind)
    return net

def import_data():
    irr = pd.read_csv(
                      filepath_or_buffer = 'data/1minIntSolrad-07-2006.csv',
                      sep                = ';',
                      skiprows           = 25,
                      parse_dates        = [[0,1]],
                      index_col          = 0,
                      )
    # Import load_profile test data
    load_data = pd.read_csv(
                            filepath_or_buffer = 'data/1MinIntSumProfiles-Apparent-2workingpeople.csv',
                            sep                = ';',
                            usecols            = [1,2],
                            parse_dates        = [1],
                            index_col          = 0,
                            )
    # Convert 0..24:00 hours to 0..23:59
    parse_hours(irr)
    load_data.index = pd.to_datetime(load_data.index, dayfirst=True) + pd.Timedelta(minutes=1)
    load_demand     = load_data.iloc[:, 0]
    irrad_data      = irr.iloc[:, 3]
    irrad_data.index= pd.to_datetime(irrad_data.index) + pd.DateOffset(years=10)
    if any(',' in string for string in load_demand):
        load_demand = load_demand.str.replace(',', '.')
        load_demand = 30*pd.to_numeric(load_demand)

    return irrad_data, load_demand

def neighborhood(net):

    _, load_demand = import_data()
    neighborhood = {}
    for b in net.bus.index[1:]:

        # Randomly generate a variation of a prosumer pv peak within +/- 30 %
        ft = random.randint(1,30)/100
        ld = load_demand*(1-ft)
        # Install a PV power around the magnitude of the peak demand of Prosumer X
        pk = np.max(ld)
        # stantiate a Prosumer X
        pvgen   = PVgen(installed_pv = pk*0.7)
        battery = BatterySimple(battery_capacity = 3.5,
                                initial_SOC = 60,
                                min_max_SOC = (20,80))
        p = Prosumer(pvgen = pvgen, battery=battery)
        # Store Prosumer X in Neighborhood dictionary
        neighborhood['%s' % net.bus.name[b]] = p

    return neighborhood

# Initialize results storage
def create_output_writer(net, time_steps, output_dir):
    ow = ts.OutputWriter(net, time_steps, output_path=output_dir,
                         output_file_type=".xls", log_variables=list())
    # these variables are saved to the harddisk after / during the time series loop
    ow.log_variable('res_ext_grid', 'p_mw')
    # ow.log_variable('res_load', 'p_mw')
    ow.log_variable('res_bus', 'vm_pu')
    ow.log_variable('res_line', 'loading_percent')
    # ow.log_variable('res_line', 'i_ka')
    return ow

# ============================================================================
# RUN example
# Load data
irr, load = import_data()
# Extract timestep size
timestep = timegrid(load)
# create network
net = simple_net()
# create neighborhood
nh = neighborhood(net)
# create central CPU that monitors and commands prosumers
cpu = CPU()

now=time.time()
t           = []
th_overload = pd.DataFrame()
vm_pu       = pd.DataFrame()
slack_p     = pd.DataFrame()
# Run stepwise simulation extracting load and irradiation
for i, (ir, ld) in enumerate(zip(irr[:1230], load[:1230]*10)):
    # Instantiate a controller unit for each Prosumer's load
    for j, (key, val) in enumerate(nh.items()):
        d = pd.DataFrame(index=[0])
        # Randomly generate a variation of a prosumer load within +/- 30 %
        ld = ld*(1-random.randint(1,30)/1000)
        val.run_pflow(ir, ld, timestep, timestamp=irr[:1440].index[i])
        net.load.at[j, "p_mw"] = -val.recorder.meta['p_grid_flow'][-1]/1000
        # d[key] = -val.recorder.meta['p_grid_flow'][-1]/1000
        # ds = ts.DFData(d)
        # control.ConstControl(net, element='load',
        #                       element_index=[j],
        #                       variable='p_mw',  data_source=ds,
        #                       profile_name=[key])
        # print("net_load",net.load.loc[j,"p_mw"], "pros_SOC", val.recorder.meta['battery_SOC'][-1])
    # Run power flow calculation at every timestep iteration
    # ow = create_output_writer(net,[], "E:/Temp")
    # ts.run_timeseries(net, verbose=False)
    pp.runpp(net)
    cpu.control_prosumers(net, nh, bypass_control=False)
    # Store line overload, voltage at buses and slack power balance
    # res['Time'].append(irr.index[i])
    # res['load'].append(net.load.p_mw.tolist())
    # res['th_overload'].append(net.res_line.loading_percent.tolist())
    # res['vm_pu_bus'].append(net.res_bus.vm_pu.tolist())
    # res['slack_p'].append(net.res_ext_grid.p_mw.tolist())
    # print('Time since beginning of simulation: ', time.time() - now)
    t.append(irr.index[i])
    th_overload = th_overload.append(net.res_line.loading_percent.transpose(), ignore_index=True)
    vm_pu = vm_pu.append(net.res_bus.vm_pu.transpose(), ignore_index=True)
    slack_p = slack_p.append(net.res_ext_grid.p_mw, ignore_index=True)
    # print('Time since beginning of simulation: ', time.time() - now)
th_overload.index = t
vm_pu.index = t
slack_p.index = t
# results = pd.DataFrame(res)


import subprocess

# result = subprocess.run(['ls', '-l'], stdout=subprocess.PIPE)
# >>> result.stdout

for i in range(1,114+1):
    str_surat = "%03d" % i
    lst_str = subprocess.check_output(["ls", "-l", str_surat]).decode().split("\n")
    Nayat = len(lst_str) - 3
    print("%d : %d," % (i,Nayat))


import re
from library_class import library
book = library([], 0)
while True:
    choice=input('''
    다음 중에서 하실 일을 골라주세요 :
    1. 도서 추가\n
    2. 도서 정보 수정\n
    3. 도서 정보 삭제\n
    4. 도서 목록\n
    5. 나가기
    ''')

    if choice=="1":
        print("도서 정보 입력")
        book.add(input("제목을 입력해주세요 >> "),input("출판사를 입력해주세요 >> "),
        input("저자를 입력해주세요 >> "), input("책에 대해 간단한 소개를 해주세요 >> "),
        input("재고를 입력해주세요 >> "))
        print("새로운 도서가 추가되었습니다.")

    elif choice=="2":
        if len(book.booklist)==0:
            print("도서정보가 없습니다.")
            continue
        print("도서 정보 수정")
        target_id = book.check("수정")
        print(f"{book.booklist[target_id]} 정보 수정")
        while True:
            num = input("어떤 정보를 수정하시겠습니까?\n제목:0\n출판사:1\n저자:2\n소개:3\n재고:4\n취소:5\n")
            if num == '0':
                book.update_name(target_id,input("제목을 입력해주세요. >> "))
            elif num == '1':
                book.update_publisher(target_id,  input("출판사를 입력해주세요. >> "))
            elif num=='2':
                book.update_author(target_id,input("저자를 입력해주세요. >> ") )
            elif num=='3':
                book.update_summary(target_id,input("책소개를 입력해주세요. >> ") )
            elif num =='4':
                book.update_stock(target_id,input("재고를 입력해주세요. >> ") )
            else:
                break

    elif choice == '3':
        if len(book.booklist)==0:
            print("도서정보가 없습니다.")
            continue
        print("도서 정보 삭제")
        target_id = book.check("삭제")
        if input(f"{book.booklist[target_id]}을 삭제하시겠습니까? y/n >> ").lower() == 'y':
            book.delete(target_id)
        else:
            print("삭제를 취소하셨습니다.")

    elif choice == '4':
        print("도서 목록 조회")
        book.show_list()

    else:
        print("Bye~")
        break


#! usr/bin/env python3

## This script is modified from PECO splitting script
## NOTE that it is essential for every excel file to have values in sample_metadata

# %% load-in
# python package for handling excel
import openpyxl
import os
import csv
# this package has a 'here' command literally like r
from pyprojroot import here

def split2csv(wb):
    for sheetName in wb.sheetnames:
        # we don't need our reference sheets
        if sheetName.startswith(('abundance summary', 'metadata', 'codes')):
            pass
        else:
            sheet = wb[sheetName]
            logging.debug('eDNA ' + sheetName)
            # open csv, filename will be region_sheet.csv
            with open(here('./rawdata/2018calvert/seine/fabs_extract/' + version + '_' + sheetName + '.csv'), 'w') as out:
                writer = csv.writer(out)
                # write each row with iter_rows
                for row in sheet.iter_rows(values_only=True):
                    # make it stop when there are no more values
                    if row[0] == None:
                        logging.debug('endsheet')
                        break
                    else:
                        writer.writerow(row)

if __name__ == '__main__':
    version = '2021Nov_FABSMaster'
    path = here('./rawdata/2018calvert/seine/raw/FABSMasterData.xlsx')
    wb = openpyxl.load_workbook(filename=path)
    split2csv(wb)

    version = '2021Nov_FABSMaster2018'
    path = here('./rawdata/2018calvert/seine/raw/FABS_masterdata_2018.xlsx')
    wb = openpyxl.load_workbook(filename=path)
    split2csv(wb)


# -*- coding: utf-8 -*-
"""
-------------------------------------------------
    File Name:      CreditHebei.py
    Description:    双公示-信用河北-基类
    Author:         Jack Deng
    Date:           2018-01-02
    Version:        v.1.0

    Lastmodified:       2018-01-02  by  Jack Deng
    Lastmodified:       2018-01-08  by  Jack Deng
-------------------------------------------------
"""

import traceback
import uuid

from scrapy.http.request import Request

from hive_framework_milk.commons.utils.selector_util import clean_all_space
from hive_framework_milk.commons.utils.tools import gen_rowkey
from hive_framework_milk.scrapy_spiders.item.parsed_item import ParsedItem
from hive_framework_milk.scrapy_spiders.spiders.spider_seed import Spider_Seed
from .field_mapping import map_field

class CreditHebei(Spider_Seed):
    """
    双公示-信用河北-基类
    """

    name = "CreditHebei"
    seed_key = None
    search_url = None

    custom_settings = {
        'ITEM_PIPELINES': {
             'hive_framework_milk.scrapy_spiders.pipeline.result_pipeline.ResultPipeline': 300
        },
    }

    def custom_init(self, *args, **kwargs):
        """
        检查属性，添加种子队列
        :param args:
        :param kwargs:
        :return:
        """
        super().custom_init(*args, **kwargs)
        if not self.seed_key or not self.search_url:
            raise NotImplementedError("seed_key {} or search_url {} not implemented".format(
                self.seed_key, self.search_url
            ))
        self.seed_queue_names = [self.seed_key]

    def make_request_from_seed(self, data):
        """
        根据种子，产生请求
        :param data:
        :return:
        """
        if isinstance(data, bytes):
            data = data.decode()
        search_url = self.search_url.format(data)
        self.logger1.info("Start search by {}".format(data))
        return Request(
            url=search_url,
            dont_filter=True,
            callback=self.parse,
            errback=self.common_errback,
            meta={'key': data}
        )

    def parse(self, response):
        """
        解析搜索结果页面，yield详情页请求
        :param response:
        :return:
        """
        try:
            key = response.meta["key"]
            # 取一页所有的url
            hrefs = [href.strip() for href in response.xpath("//table//tr//td//a/@href").extract()]
            if not hrefs:
                self.logger1.warning("key {} search failed, please check".format(key))
                return
            for href in hrefs:
                detail_url = response.urljoin(href)
                yield Request(
                    url=detail_url,
                    dont_filter=True,
                    callback=self.parse_detail,
                    errback=self.common_errback,
                    meta={"key": key}
                )
                self.logger1.info("yield detail url , key {}".format(key))
            next_page_href = "".join(response.xpath('//a[@class="next-page"]/@href').extract())
            if next_page_href:# 有下一页就翻页
                next_url = response.urljoin(next_page_href)
                yield Request(
                    url=next_url,
                    dont_filter=True,
                    callback=self.parse,
                    errback=self.common_errback,
                    meta={"key": key}
                )
                self.logger1.warning("yield turn page, key {}".format(key))
            else:
                self.logger1.info("key {} only has one page".format(key))
        except:
            err_msg = traceback.format_exc()
            self.logger1.error("Exception on search {url}, error:{err_msg}".format(
                url=response.url, err_msg=err_msg))

    def parse_detail(self, response):
        """
        解析详情页
        :param response:
        :return:
        """
        try:
            key = response.meta["key"]
            titles_tds = response.xpath("//table//tr//td[1]")
            values_tds = response.xpath("//table//tr//td[last()]")
            titles = [clean_all_space(td.xpath("string(.)").extract()) for td in titles_tds]
            values = ["".join(td.xpath("string(.)").extract()).strip() for td in values_tds]
            if len(titles) != len(values):
                raise Exception("the length of titles and values are not equal, url {}".format(response.url))
            tmp_dict = dict(zip(titles, values))
            res_dict = map_field(tmp_dict)
            if "xzcf" in self.name:
                if "license_status" in res_dict.keys():
                    res_dict["punish_status"] = res_dict.pop("license_status", "")
            item = ParsedItem()
            self.common_item_assembler(response, item)
            item["_id"] = "{}_{}".format(key, uuid.uuid4())
            item["bbd_html"] = ""
            item["_parsed_data"] = res_dict
            item["rowkey"] = gen_rowkey(item, keys=('do_time', 'bbd_type'))
            yield item
            self.logger1.info("one data {} save to mongodb".format(key))
        except:
            err_msg = traceback.format_exc()
            self.logger1.error("Exception on detail {url}, error:{err_msg}".format(
                url=response.url, err_msg=err_msg))

    def common_errback(self, response):
        """
        请求失败时输出日志
        :param response:
        :return:
        """
        self.logger1.error('request {} failed'.format(response.request.url))


#!/usr/bin/env python3

import json
import pandas as pd
import numpy as np
import re
import os
import argparse

import subprocess

RESOURCES_FILE = "data/HOAResources.csv"
CROPLAND_FILE = "data/HOACropland.csv"
CYCLES_RUN_SCRIPT = "bin/cycles/run"
TMP_DIR = "tmp"
OUTPUT_FILE = "outputs/cycles_results.csv"
CROPS_FILE = "data/crops-horn-of-africa.crop"
SOIL_WEATHER_DIR = "data/soil_weather"

def run_cycles(params):
    crops = [
        'Maize',
        'Sorghum',
        'Cassava',
        'Millet',
        'SpringLentil',
        'Potato',
    ]

    cropland_df = pd.read_csv(CROPLAND_FILE)
    cropland_df.set_index(['country', 'admin1', 'admin2', 'admin3'], inplace=True)

    df = pd.read_csv(RESOURCES_FILE)
    df.set_index(['country', 'admin1', 'admin2', 'admin3'], inplace=True)
    country_soil_points = df.loc[params["country"]]

    # Run Cycles for all soil points in the country
    # - TODO: Should run in parallel
    first = True
    for index, point in country_soil_points.iterrows():

        # Get the crop fractional area for this region
        cropland_row = cropland_df.loc[params["country"], index[0], index[1], index[2]]

        crop_pd = cropland_row[params["crop_name"].lower()+"_pd"]
        crop_grain_yield = cropland_row[params["crop_name"].lower()+"_grain_yield"]
        crop_fractional_area = cropland_row[params["crop_name"].lower()+"_fractional_area"]

        # Calculate planting date
        planting_day = int(crop_pd) + int(params["start_planting_day"])
        planting_day = planting_day - 365 if planting_day > 365 else planting_day
        planting_day = planting_day + 365 if planting_day <= 0 else planting_day

        # Calculate reference and current food production
        reference_production = 0.0
        production = 0.0
        for c in crops:
            reference_production += cropland_row[c.lower()+"_grain_yield"] * cropland_row[c.lower()+"_fractional_area"]

            if c != params["crop_name"]:
                production += cropland_row[c.lower()+"_grain_yield"] * cropland_row[c.lower()+"_fractional_area"]

        # Get the input/output files
        inputfile = point["filename"]
        season_file = f"{TMP_DIR}/{inputfile}.season"
        summary_file = f"{TMP_DIR}/{inputfile}.summary"

        # ** Run Cycles **
        cmd = [CYCLES_RUN_SCRIPT,
               '-i1', f"{SOIL_WEATHER_DIR}/{inputfile}",
               '-i2', CROPS_FILE,
               '-o1', season_file,
               '-o2', summary_file,
               '-p1', params["start_year"],
               '-p2', params["end_year"],
               '-p3', params["crop_name"],
               '-p4', str(planting_day),
               '-p5', params["fertilizer_rate"],
               '-p6', params["weed_fraction"],
               '-p7', "False"
              ]
        print(cmd)
        try:
              output = subprocess.run(cmd)
        except subprocess.CalledProcessError as exc:
              print("Status : FAIL", exc.returncode, exc.output)
              continue

        # Load the output file
        exdf = get_dataframe_for_execution_result(season_file, index, params,
                                                ["grain_yield", "cum._n_stress", "actual_tr", "potential_tr"])

        # Filter/Modify/Add Columns
        exdf["crop_production"] = exdf["grain_yield"]*crop_fractional_area
        exdf["water_stress"] = 1 - exdf["actual_tr"]/exdf["potential_tr"]
        exdf["relative_yield"] = exdf["grain_yield"] / crop_grain_yield
        exdf["planting_day"] = planting_day
        exdf["impact"] = (production + exdf["crop_production"]) / reference_production - 1.0

        exdf = exdf.rename(columns={'cum._n_stress': 'nitrogen_stress'})
        exdf = exdf.drop(['actual_tr', 'potential_tr'], axis = 1)

        # Write output
        if first:
            exdf.to_csv(OUTPUT_FILE, index=False)
            first = False
        else:
            exdf.to_csv(OUTPUT_FILE, mode='a', header=False, index=False)

def load_execution_result(outputloc):
    df = pd.read_csv(outputloc, sep='\t', header=0, skiprows=[1], skipinitialspace=True)
    df = df.rename(columns=lambda x: x.strip().lower().replace(' ', '_'))
    df['date'] = pd.to_datetime(df['date'], format="%Y-%m-%d")
    return df

def get_dataframe_for_execution_result(season_file, index, params, result_fields):
    df = load_execution_result(season_file)

    # Fetch feature values
    ndf = df.filter(result_fields, axis=1)

    # Insert timestamp
    ndf.insert(0, "date", df['date']) #.values.astype(np.int64) // 10 ** 9)

    # Insert geospatial details
    ndf.insert(1, "country", params["country"])
    ndf.insert(2, "admin1", index[0] if index[0] else None)
    ndf.insert(3, "admin2", index[1] if index[1] else None)
    ndf.insert(4, "admin3", index[2] if index[2] else None)

    return ndf

def _main():
    parser = argparse.ArgumentParser(
        description="Cycles execution for a country"
    )
    parser.add_argument("--country", dest="country", default="Kenya", help="Country name")
    parser.add_argument("--crop-name", dest="crop_name", default="Maize", help="Crop name")
    parser.add_argument("--start-year", dest="start_year", default="2000", help="Simulation start year")
    parser.add_argument("--end-year", dest="end_year", default="2020", help="Simulation end year")
    parser.add_argument("--start-planting-day", dest="start_planting_day", default="103", help="Start planting date")
    parser.add_argument("--fertilizer-rate", dest="fertilizer_rate", default="50.00", help="Fertilizer rate")
    parser.add_argument("--weed-fraction", dest="weed_fraction", default="0.2", help="Weed fraction")
    args = parser.parse_args()
    run_cycles(vars(args))

if __name__ == "__main__":
    _main()


#to insert,search and traverse in a singly linked list
class Node:
    def __init__(self,value=None):
        self.value = value
        self.next = None

class SLinkedList:
    def __init__(self):
        self.head = None
        self.tail = None

    # to make the linked list iterable
    def __iter__(self):
        node = self.head
        while node:
           yield node
           node = node.next

    # to insert into the linked list
    def insertSLL(self,value,location):
        newNode = Node(value)
        if self.head is None:
            self.head = newNode
            self.tail = newNode
        else:
             # location = 0 means inserting the first element of SLL
            if location == 0:
                newNode.next = self.head
                self.head = newNode
            # location = 1 means inserting as the last element of SLL
            elif location == 1:
                newNode.next = None
                self.tail.next = newNode
                self.tail = newNode
            # location means inserting the element at location index of SLL
            else:
                tempNode = self.head
                index=1
                while index < location-1 :
                    tempNode = tempNode.next
                    index+=1
                nextNode = tempNode.next
                tempNode.next = newNode
                newNode.next = nextNode
                if tempNode == self.tail:
                    self.tail = newNode

    # to traverse the linked list
    def traverseSLL(self):
        if self.head is None:
            print("The singly linked list is empty")
        else:
            node = self.head
            while node:
                print(node.value,end=' ')
                node = node.next
            print()

    # to search the linked list
    def searchSLL(self,key):
        if self.head is None:
            print("Value doesn't exist in the linked list")
        else:
            node = self.head
            while node:
                if node.value == key:
                    print("Value exists ")
                    break
                node = node.next
            else:
                print("Value doesn't exist in the linked list")

    # to delete from a linked list
    def deleteNodeSLL(self,location):
        if self.head is None:
            print("The SLL doesn't have elements to delete")
        else:
            # location = 0 means deleting the first element of SLL
            if location == 0:
                if self.head == self.tail:
                    self.head = None
                    self.tail = None
                else:
                    self.head = self.head.next
            # location = 1 means deleting the last element of SLL
            elif location == 1:
                if self.head == self.tail:
                    self.head = None
                    self.tail = None
                else:
                    node = self.head
                    while node:
                        if node.next == self.tail:
                            break
                        node = node.next
                    if node:
                        node.next = None
                        self.tail = node
            # location means deleting the element at location index of SLL
            else:
                tempNode = self.head
                index = 1
                while index < location-1:
                    tempNode = tempNode.next
                    index+=1
                nextNode = tempNode.next
                tempNode.next = nextNode.next

    #delete entire SLL
    def deleteEntireSLL(self):
        if self.head is None:
            print("The SLL doesn't exist")
        else:
            self.head = None
            self.tail = None

lst = SLinkedList()
lst.insertSLL(1,1)
lst.insertSLL(2,1)
lst.insertSLL(3,1)
lst.insertSLL(4,1)
lst.insertSLL(0,0)
lst.insertSLL(0,5)
lst.insertSLL(8,7)
print("The SLL\n")
print([node.value for node in lst])

#to perform traversal
print("SLL Traversal\n")
lst.traverseSLL()

#to perform search
key = int(input("Enter no to search : "))
lst.searchSLL(key)

#to perform deletion-0,1,posn
lst.deleteNodeSLL(0)
print([node.value for node in lst])
lst.deleteNodeSLL(1)
print([node.value for node in lst])
lst.deleteNodeSLL(4)
print([node.value for node in lst])

#delete the entire SLL
print("Delete the entire SLL")
lst.deleteEntireSLL()
print([node.value for node in lst])


'''
Given a dictionary of words and a string made up of those words (no spaces), return the original sentence in a list.
If there is more than one possible reconstruction, return any of them. If there is no possible reconstruction, then return null.

For example, given the set of words 'quick', 'brown', 'the', 'fox', and the string "thequickbrownfox", you should return ['the', 'quick', 'brown', 'fox'].

Given the set of words 'bed', 'bath', 'bedbath', 'and', 'beyond', and the string "bedbathandbeyond",
return either ['bed', 'bath', 'and', 'beyond] or ['bedbath', 'and', 'beyond'].
'''

# Slow, but working implementation.

def getSentence(str, word_list):
    def rGetSentence(str, word_list, sentence):
        # Terminal condition for recursion.
        if len(str) == 0:
            return sentence

        for i in range(len(str), 0, -1):
            try:
                word = word_list.index(str[:i])
            except ValueError:
                # No match found.
                continue
            else:
                # Match found. Add matching word to the sentence and recur, then break the loop.
                sentence.append(word_list[word])
                rGetSentence(str[len(word_list[word]):], word_list, sentence)
                break
        else:
            # End of loop, no matching words found starting at the beginning of the substring.
            return sentence

        # When there is a valid sentence, the total length of the sentence word list will be the same as the initial string.
        sentence_len = 0
        for each_word in sentence:
            sentence_len += len(each_word)
        if sentence_len == len(str):
            return sentence
        else:
            return None

    return rGetSentence(str, word_list, [])

print(getSentence("thequickbrownfox", ['quick', 'brown', 'the', 'fox']))
print(getSentence("bedbathandbeyond", ['bed', 'bath', 'bedbath', 'and', 'beyond']))


import streamlit as st
import pickle

# Load the pickle file containing the trained model
with open('rain_prediction_model.pkl', 'rb') as file:
    model = pickle.load(file)

# Define the streamlit app
if __name__ == '__main__':
    st.title('Rain Prediction App')

    # Create an input form for user inputs
    st.header('Enter Input Features')
    st.sidebar.header('Country Labels')
    st.sidebar.text('''London : 0
Paris : 1
New York : 2
Tokyo : 3
Beijing : 4
Istanbul : 5
Moscow : 6
Berlin : 7
Bangkok : 8
Seoul : 9
Mumbai : 10
Rome : 11
Madrid : 12
Mexico City : 13
Shanghai : 14
Rio de Janeiro : 15
Los Angeles : 16
Buenos Aires : 17
Sydney : 18
Johannesburg : 19''')
    options = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
    Location = st.selectbox('Select a Location', options)
    Min_Temperature = st.slider('Min Temperature today (C)', -20.00, 40.00, 0.00)
    Max_Temperature = st.slider('Max Temperature today (C)', -20.00, 40.00, 0.00)
    Wind_Direction = st.slider('Wind Direction in Degrees (°) --> 0° - blowing from the north, 90° - blowing from the east, 180° - blowing from the south, 270° - blowing from the west', 0, 360, 180)
    Wind_Speed = st.slider('Wind Speed (m/s)', 0.00, 13.00, 0.00)
    Humidity = st.slider('Humidity (%)', 7, 100, 50)
    Pressure = st.slider('Pressure (hPa)', 1000, 1036, 1000)
    Cloud = st.slider('Cloud (%)', 0, 100, 50)
    Temperature = st.slider('Present Temperature (C)', -20.00, 40.00, 0.00)
    opt = [0,1]
    Rain = st.selectbox('Is it raining today (0 - No, 1 - Yes)', opt)

    # Use the loaded model to make predictions
    features = [[Location, Min_Temperature, Max_Temperature, Wind_Direction, Wind_Speed, Humidity, Pressure, Cloud, Temperature, Rain]]
    prediction = model.predict(features)

    # Display the prediction results to the user
    st.write('Prediction : ', prediction[0])
    if (prediction[0]==1):
        st.header("Yes, it'll rain tomorrow")
    else:
        st.header("No, it'll not rain tomorrow")


from django.shortcuts import render, redirect
from django.http import HttpResponse
from datetime import datetime

class Chat:
    def __init__(self):
        self.messages = []
    def add(self,message):
        self.messages.append(message)
    def get_last_100_messages(self):
        self.messages.sort(key=lambda x: x.timestamp, reverse=True)
        return self.messages[:99]
    def get_messages_of_page(self, n):
        self.messages.sort(key=lambda x: x.timestamp, reverse=True)
        return self.messages[5*n:5*(n+1)]

class Message:
    def __init__(self, body, author):
        self.body = body
        self.timestamp = datetime.now()
        self.author = author
chat = Chat()

# for i in range(120):
#     message1 = Message("blablabalbal", "pancho{}".format(i))
#     chat.add(message1)

# Create your views here.
def index(request):
    if request.method == "GET":
        if not request.session.get("alias"):
            # No Alias
            return render(request, "input_alias.html")
        else:
            return redirect('send_message')
    elif request.method == "POST":
        print(request.POST.get("alias",""))
        alias = request.POST.get("alias","")
        request.session["alias"] = alias
        return redirect('send_message')

def send_message(request):
    alias = request.session.get("alias")
    if not alias:
        return redirect("") # Back to home
    else:
        if request.method == "GET":
            data = {"alias": alias}
            return render(request, "send_message.html",data)

        elif request.method == "POST":
            body = request.POST.get("body","")
            author = alias
            message = Message(body, author)
            chat.add(message)
            return redirect("messages",0)

def messages(request, page):
    alias = request.session.get("alias")
    if not alias:
        return redirect("") # Back to home
    else:
        if request.method == "GET":
            if page < min(19,(len(chat.get_last_100_messages())-1)//5):
                next_page_num = page + 1
            else:
                next_page_num = min(19,(len(chat.get_last_100_messages())-1)//5)
            if page <= 0:
                prev_page_num = 0
            else:
                prev_page_num = page - 1

            data = {"alias": alias,
                    "messages": chat.get_messages_of_page(page),
                    "next_page_num": next_page_num,
                    "prev_page_num": prev_page_num,
                    }
                    ### Meter current page
            return render(request, "messages.html",data)


"""Support for LG WebOS TV notification service."""
import logging

from aiowebostv import WebOsTvPairError

from homeassistant.components.notify import ATTR_DATA, BaseNotificationService
from homeassistant.const import CONF_ICON, CONF_NAME

from .const import ATTR_CONFIG_ENTRY_ID, DATA_CONFIG_ENTRY, DOMAIN, WEBOSTV_EXCEPTIONS

_LOGGER = logging.getLogger(__name__)

async def async_get_service(hass, _config, discovery_info=None):
    """Return the notify service."""

    if discovery_info is None:
        return None

    name = discovery_info.get(CONF_NAME)
    client = hass.data[DOMAIN][DATA_CONFIG_ENTRY][
        discovery_info[ATTR_CONFIG_ENTRY_ID]
    ].client

    return LgWebOSNotificationService(client, name)

class LgWebOSNotificationService(BaseNotificationService):
    """Implement the notification service for LG WebOS TV."""

    def __init__(self, client, name):
        """Initialize the service."""
        self._name = name
        self._client = client

    async def async_send_message(self, message="", **kwargs):
        """Send a message to the tv."""
        try:
            if not self._client.is_connected():
                await self._client.connect()

            data = kwargs.get(ATTR_DATA)
            icon_path = data.get(CONF_ICON, "") if data else None
            await self._client.send_message(message, icon_path=icon_path)
        except WebOsTvPairError:
            _LOGGER.error("Pairing with TV failed")
        except FileNotFoundError:
            _LOGGER.error("Icon %s not found", icon_path)
        except WEBOSTV_EXCEPTIONS:
            _LOGGER.error("TV unreachable")


""" FileCatalogFactory class to create file catalog client objects according to the
    configuration description
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from DIRAC import gLogger, gConfig, S_OK, S_ERROR
from DIRAC.ConfigurationSystem.Client.Helpers.Resources import getCatalogPath
from DIRAC.Resources.Catalog.FileCatalogProxyClient import FileCatalogProxyClient
from DIRAC.Core.Utilities.ObjectLoader import ObjectLoader

__RCSID__ = "$Id$"

class FileCatalogFactory(object):
  """ Factory of file catalog objects. Only exposes createCatalog() method
  """

  def __init__(self):
    self.log = gLogger.getSubLogger('FileCatalogFactory')
    self.catalogPath = ''

  def createCatalog(self, catalogName, useProxy=False):
    """ Create a file catalog object from its name and CS description
    """
    catalogPath = getCatalogPath(catalogName)
    catalogType = gConfig.getValue(catalogPath + '/CatalogType', catalogName)
    catalogURL = gConfig.getValue(catalogPath + '/CatalogURL', "DataManagement/" + catalogType)
    optionsDict = {}
    result = gConfig.getOptionsDict(catalogPath)
    if result['OK']:
      optionsDict = result['Value']

    if useProxy:
      result = self.__getCatalogClass(catalogType)
      if not result['OK']:
        return result
      catalogClass = result['Value']
      methods = catalogClass.getInterfaceMethods()
      catalog = FileCatalogProxyClient(catalogName)
      catalog.setInterfaceMethods(methods)
      return S_OK(catalog)

    return self.__createCatalog(catalogName, catalogType, catalogURL, optionsDict)

  def __getCatalogClass(self, catalogType):
    result = ObjectLoader().loadObject('Resources.Catalog.%sClient' % catalogType)
    if not result['OK']:
      gLogger.error('Failed to load catalog object', '%s' % result['Message'])
    return result

  def __createCatalog(self, catalogName, catalogType, catalogURL, optionsDict):

    self.log.debug('Creating %s client of type %s' % (catalogName, catalogType))

    result = self.__getCatalogClass(catalogType)
    if not result['OK']:
      return result
    catalogClass = result['Value']

    try:
      optionsDict['url'] = catalogURL
      catalog = catalogClass(**optionsDict)
      self.log.debug('Loaded module %sClient' % catalogType)
      return S_OK(catalog)
    except Exception as x:
      errStr = "Failed to instantiate %s()" % (catalogType)
      gLogger.exception(errStr, lException=x)
      return S_ERROR(errStr)

    # Catalog module was not loaded
    return S_ERROR('No suitable client found for %s' % catalogName)


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

import click

from nettool.nettest import NetTest as ntest

class ArgumentValidation(object):
    @classmethod
    def _type_validation(cls, validation_type, value):
        try:
            getattr(ntest.validate, validation_type)(value, raise_exception=True)
        except ValueError as e:
            error = unicode(e)
            raise click.BadParameter(error)
        return value

    @classmethod
    def _format_error(cls, error, valid_options):
        if valid_options:
            valid = u'\n\t\t'.join(valid_options)
            message = u'{}\n\tThe valid options are:\n\t\t{}'.format(error, valid)
        else:
            message = u'{}. There are no valid options.'.format(error)
        return message

    @classmethod
    def _type_validation_with_suggestions(cls, validation_class, value):
        try:
            validation_class.valid(value, raise_exception=True)
        except ValueError as e:
            error = unicode(e)
            message = cls._format_error(error, validation_class.valid_entries)
            raise click.BadParameter(message)
        return value

    @classmethod
    def hostname(cls, ctx, param, value):
        return cls._type_validation('hostname', value)

    @classmethod
    def ip(cls, ctx, param, value):
        return cls._type_validation('ip', value)


import numpy as np
import pandas as pd
import os
import time
import random
import matplotlib.pyplot as plt

from glob import glob

import PIL
import cv2

def hog(image_name):
    hog = cv2.HOGDescriptor()
    hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
    #image_path = glob.glob('data/*.jpg')
    #print(image_path)

    #for image_name in image_path:

    img = cv2.imread(image_name)

    if img.shape[1] < 400:
        (height, width) = img.shape[:2]
        ratio = float(height)/float(width)
        img = cv2.resize(img,(400, 350))

    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    rects,weights = hog.detectMultiScale(img_gray, winStride =(2,2), padding=(10,10), scale = 1.02)

    for i, (x,y,w,h) in enumerate(rects):
        if weights[i] < 0.1:
            continue
        elif weights[i] > 0.1 and weights[i] < 0.3:
            cv2.rectangle(img, (x,y), (x+w, y+h), (0,0,255),2)

        elif weights[i] > 0.3 and weights[i] < 0.7:
            cv2.rectangle(img, (x,y), (x+w, y+h), (50,122,255),2)

        elif weights[i] > 0.7:
            cv2.rectangle(img, (x,y), (x+w, y+h), (0,255,0),2)

    cv2.imshow('Image', img)
    cv2.waitKey(0)


#########################################################################################
## Script name:     fillHolesInCorridors.py
## Command line:    fillCorridorHoles
## ArcGIS Version:  9.1
## Last modified:   29 January 2007
## Author:          Dan Majka (dan@corridordesign.org)
## Description:     Fills holes in corridor feature class
##
## Parameter List:
##                  Argv[1]    Corridor shapefile
##                  Argv[2]    Output Buffered shapefile
##
#########################################################################################
import sys, string, os, time
from gen_checkArcGisVersion import *
from gen_reportErrors import *
from gen_logFile import *
gp = checkArcGIS()
import gen_checkExtensions

#----------------------------------------------------------------------------------------
def fillHoles(inputCorridor, holeDistance, outputCorridor, workspace):
    try:
        bufferedCorridor = workspace + '/_bufferedCorridor.shp'
        bufferDistance = int(holeDistance)/2
        negBufferDistance = -(bufferDistance)
        gp.Buffer_analysis(inputCorridor, bufferedCorridor, bufferDistance, '', '', 'ALL', '')
        print(' - buffered corridor')
        gp.Buffer_analysis(bufferedCorridor, outputCorridor, negBufferDistance, '', '', 'ALL', '')
        print(' - negative buffer')
        gp.delete(bufferedCorridor)
        return outputCorridor
    except:
        addErrorMessages(gp)

# CALL FUNCTIONS -------------------------------------------------------------------------------
# Script arguments
try:
    # Script arguments ------------------------------#####
    inputCorridor = sys.argv[1]
    outputCorridor = sys.argv[2]
    holeDistance = sys.argv[3]

    toolName = 'FillCorridorHoles'
    inParameters = '%s %s %s' % (inputCorridor,
                                 outputCorridor,
                                 holeDistance
                                 )

    # Utility stuff ------------------------------#####
    gen_checkExtensions.checkSpatialAnalyst(gp)
    gp.OverwriteOutput = 1
    desc = gp.Describe
    userWorkspace = os.path.dirname(outputCorridor)
    try:
        workspace = desc(userWorkspace).CatalogPath.replace("\\","/")
    except:
        workspace = userWorkspace
    totalSteps = '1'
    logFile = createLogFile(workspace, toolName, inParameters)

    # Call functions ------------------------------#####
    gp.AddMessage('')
    gp.AddMessage('CorridorDesigner ----------------------------------------')
    gp.AddMessage('RUNNING: sliceCorridor')
    gp.AddMessage('')
    gp.AddMessage(' Step 1/'+totalSteps+') Filling all holes less than ' +holeDistance+ ' map units wide')
    fillHoles(inputCorridor, holeDistance, outputCorridor, workspace)
    gp.AddMessage(' Finished! ')
    gp.AddMessage('  ')
    closeLogFile(logFile)
    del gp
except:
    addErrorMessages(gp)
    closeLogFile(logFile)
    del gp


from health_company_data_api.services.patients import PatientsService

class TestPatientsService:
    def test_get_patients_with_first_name_filter_without_data(self):
        patients_filters = {"first_name": "Lucas"}

        assert PatientsService().get_patients(patients_filters) == []

    def test_get_patients_with_first_name_filter_with_data(self):
        expected_data = [
            {
                "uuid": "PATIENT0001",
                "first_name": "VITORIA",
                "last_name": "CARVALHO",
                "date_of_birth": "1990-01-01T00:00:00",
            },
            {
                "uuid": "PATIENT0002",
                "first_name": "VITORIA",
                "last_name": "SILVA",
                "date_of_birth": "1986-07-12T00:00:00",
            },
        ]

        patients_filters = {"first_name": "Vitoria"}

        assert PatientsService().get_patients(patients_filters) == expected_data


"""module: stonehenge game, with a class of game and a class of gamestate
"""
from typing import Any, List
from game import Game
from game_state import GameState

class StonehengeGame(Game):
    """a class representing a game called Stonehenge, a subclass of Game.

    Cannot write example since class needs input.

    state - the current state of the Stonehenge Game
    side_length - the side length og the Stonehenge grid

    """
    current_state: "StonehengeState"
    side_length: int

    def __init__(self, p1_starts: bool) -> None:
        """
        Initialize StonehengeGame, using p1_starts to find who the first
        player is.
        overrides Game.__init__.
        """
        self.side_length = int(input("Enter the side length of the board:"))
        self.current_state = StonehengeState(p1_starts, self.side_length)

    def get_instructions(self) -> str:
        """
        Return the instructions for Stonehenge Game.
        overrides Game.get_instruction.
        """
        instructions = "Welcome to the Stonehenge Game, you are going to claim"\
                       " the cell in the Stonehenge grid by turns, who claims "\
                       "at least a half ley-lines of the grid will be the "\
                       "winner!"
        return instructions

    def is_over(self, state: "StonehengeState") -> bool:
        """
        Return whether or not StonehengeGame is over at state.
        Overrides Game.is_over.
        """
        counting1 = state.current_line[0] / (3 * (self.side_length + 1))
        counting2 = state.current_line[1] / (3 * (self.side_length + 1))
        return counting2 >= 0.5 or counting1 >= 0.5

    def is_winner(self, player: str) -> bool:
        """
        Return whether player has won StonehengeGame.
        Overrides Game.is_winner.

        Precondition: player is 'p1' or 'p2'.
        """
        if self.is_over(self.current_state):
            if self.current_state.get_current_player_name() != player:
                return True
        return False

    def str_to_move(self, string: str) -> str:
        """
        Return the move that string represents. If string is not a move,
        return some invalid move.
        Overrides Game.str_to_move.
        """
        return str(string)

class StonehengeState(GameState):
    """a class representing a game state of the Stonehenge Game, a subclass
    of GameState.

    grid - the list of how stonehenge grid looks like with each specific
           elements.
    current_line - a list showed the number of two players claimed ley lines.
    """
    current_line: List[int]
    grid: List[str]

    def __init__(self, is_p1_turn: bool, grid_length: int) -> None:
        """
        Initialize StonehengeState state and set the current player based on
        is_p1_turn. Form the initial grid by given grid_length
        extends GameState.__init__
        >>> state = StonehengeState(True, 1)
        >>> state.grid
        [['@', '@'], ['@', 'A', 'B'], ['@', 'C', '@'], ['@']]
        >>> state.p1_turn
        True
        """
        GameState.__init__(self, is_p1_turn)
        self.current_line = [0, 0]
        self.grid = [['@', '@']]
        order = ord('A')
        first_num = 2
        for i in range(grid_length + 1):
            line = ['@']
            num = 0
            if i == grid_length:
                first_num -= 2
            while num < first_num:
                line.append(chr(order + num))
                num += 1
            order += num
            first_num += 1
            if i != grid_length - 1:
                line.append('@')
            self.grid.append(line)
        self.grid += [['@'] * grid_length]

    def __str__(self) -> str:
        """
        Return a string representation of the current state of the game.
        Overrides GameState.__str__
        """

        n = len(self.grid)
        g1 = 3
        g2 = ' - '
        g3 = ' / \\'
        g4 = ' \\ /'
        graph = (2 * n - 2) * ' ' + self.grid[0][0] + g1 * ' ' + \
                self.grid[0][1] + '\n'
        graph += (2 * n - 3) * ' ' + '/' + g1 * ' ' + '/' + '\n'
        for i in range(1, n - 3):
            graph += 2 * (n - 3 - i) * ' '
            for j in range(len(self.grid[i]) - 2):
                graph += self.grid[i][j] + g2
            graph += self.grid[i][len(self.grid[i]) - 2] + g1 * ' ' + \
                     self.grid[i][len(self.grid[i]) - 1] + '\n'
            graph += (2 * (n - 3 - i) + 2) * ' ' + g3 * (i + 1) + ' /' + '\n'
        for j in range(len(self.grid[n - 3]) - 1):
            graph += self.grid[n - 3][j] + g2
        graph += self.grid[n - 3][len(self.grid[n - 3]) - 1] + '\n'
        graph += 4 * ' ' + g4 * (n - 3) + ' \\' '\n' + 2 * ' '
        for j in range(len(self.grid[n - 2]) - 2):
            graph += self.grid[n - 2][j] + g2
        graph += self.grid[n - 2][len(self.grid[n - 2]) - 2] + g1 * ' ' + \
                 self.grid[n - 2][len(self.grid[n - 2]) - 1] + '\n'
        graph += 4 * ' ' + '   \\' * (n - 3) + '\n'
        graph += 5 * ' '
        for j in range(n - 3):
            graph += g1 * ' ' + self.grid[n - 1][j]
        graph += '\n'
        return graph

    def get_possible_moves(self) -> list:
        """
        Return all possible moves that can be applied to this state.
        Overrides GameState.get_possible_moves

        >>> state = StonehengeState(True, 1)
        >>> state.grid
        [['@', '@'], ['@', 'A', 'B'], ['@', 'C', '@'], ['@']]
        >>> state.get_possible_moves()
        ['A', 'B', 'C']
        """
        new_list = []
        counting1 = self.current_line[0] / (3 * (len(self.grid) - 2))
        counting2 = self.current_line[1] / (3 * (len(self.grid) - 2))
        for i in range(1, len(self.grid) - 1):
            for j in range(1, len(self.grid[i])-1):
                if self.grid[i][j].isalpha():
                    new_list.append(self.grid[i][j])
            if i == len(self.grid) - 3:
                if self.grid[i][len(self.grid[i]) - 1].isalpha():
                    new_list.append(self.grid[i][len(self.grid[i]) - 1])
        if counting1 >= 0.5 or counting2 >= 0.5:
            new_list = []
        return new_list

    def make_move(self, move: str) -> 'StonehengeState':
        """
        Return the StonehengeState that results from applying move to
        this StonehengeState.
        Overrides GameState.make_move

        >>> state = StonehengeState(True, 1)
        >>> new_state = state.make_move('A')
        >>> new_state.grid
        [['1', '@'], ['1', '1', 'B'], ['@', 'C', '@'], ['1']]
        >>> new_state.p1_turn
        False
        """
        new_state = StonehengeState(self.p1_turn, len(self.grid) - 2)
        new_state.current_line, new_state.grid = self.current_line[:], [
            [cell for cell in item] for item in self.grid]
        if not self.is_valid_move(move):
            return self
        my_pos, index, new_state.p1_turn, ley_line_dic = '1', 0, False, {}
        if not self.p1_turn:
            my_pos, index, new_state.p1_turn = '2', 1, True
        for i in range(len(new_state.grid)):
            for j in range(len(new_state.grid[i])):
                new_state.grid[
                    i][j] = my_pos if new_state.grid[
                        i][j] == move else new_state.grid[i][j]
        c1, c2, ley_line_dic['+0+0'], ley_line_dic['-2-1'] \
            = 0, 0, [], []
        ley_line_dic['+0+0'].append(new_state.grid[0][0])
        ley_line_dic['-2-1'].append(new_state.grid[-2][-1])
        for i in range(1, len(new_state.grid) - 2):
            ley_line_dic['+0+0'].append(new_state.grid[i][1])
            ley_line_dic['-2-1'].append(new_state.grid[i][i + 1])
            c1 = c1 + 1 if new_state.grid[i][1] == my_pos else c1
            c2 = c2 + 1 if new_state.grid[i][i + 1] == my_pos else c2
        if c1 >= (len(ley_line_dic['+0+0']) - 1) / 2 and new_state.grid[int(
                '+0+0'[0:2])][int('+0+0'[2:])] == '@':
            new_state.grid[int('+0+0'[0:2])][
                int('+0+0'[2:])], new_state.current_line[
                    index] = my_pos, new_state.current_line[index] + 1
        if c2 >= (len(ley_line_dic['-2-1']) - 1) / 2 and new_state.grid[int(
                '-2-1'[0:2])][int('-2-1'[2:])] == '@':
            new_state.grid[int('-2-1'[0:2])][
                int('-2-1'[2:])], new_state.current_line[
                    index] = my_pos, new_state.current_line[index] + 1
        name1, name2, c1, c2, ley_line_dic['+0+1'], ley_line_dic['-1-1'] \
            = '+0+1', '-1-1', 0, 0, [], []
        ley_line_dic['+0+1'].append(
            new_state.grid[0][1])
        ley_line_dic['-1-1'].append(
            new_state.grid[-1][-1])
        for j in range(2, len(new_state.grid) - 1):
            for i in range(j - 1, len(new_state.grid) - 2):
                ley_line_dic[name1].append(new_state.grid[i][j])
                ley_line_dic[name2].append(new_state.grid[i][i + 2 - j])
                c1 = c1 + 1 if new_state.grid[i][j] == my_pos else c1
                c2 = c2 + 1 if new_state.grid[i][i + 2 - j] == my_pos else c2
            ley_line_dic[name1].append(
                new_state.grid[len(new_state.grid) - 2][j - 1])
            ley_line_dic[name2].append(
                new_state.grid[len(new_state.grid) - 2][-j])
            c1 = c1 + 1 if new_state.grid[
                len(new_state.grid) - 2][j - 1] == my_pos else c1
            c2 = c2 + 1 if new_state.grid[
                len(new_state.grid) - 2][-j] == my_pos else c2
            if c1 >= (len(ley_line_dic[name1]) - 1) / 2 and new_state.grid[int(
                    name1[0:2])][int(name1[2:])] == '@':
                new_state.grid[
                    int(name1[0:2])][int(name1[2:])], new_state.current_line[
                        index] = my_pos, new_state.current_line[index] + 1
            if c2 >= (len(ley_line_dic[name2]) - 1) / 2 and new_state.grid[int(
                    name2[0:2])][int(name2[2:])] == '@':
                new_state.grid[
                    int(name2[0:2])][int(name2[2:])], new_state.current_line[
                        index] = my_pos, new_state.current_line[index] + 1
            if j + 1 <= len(new_state.grid) - 2:
                name1, name2 = '+' + str(j - 1) + '+' + str(j + 1), \
                               '-1' + str(-j)
                ley_line_dic[name1], ley_line_dic[name2], c1, c2 = \
                    [], [], 0, 0
                ley_line_dic[name1].append(new_state.grid[j - 1][j + 1])
                ley_line_dic[name2].append(new_state.grid[-1][-j])
        for i in range(1, len(new_state.grid) - 1):
            name3, c3 = '+' + str(i) + '+' + '0', 0
            ley_line_dic[name3] = []
            for j in range(len(new_state.grid[i]) - 1):
                ley_line_dic[name3].append(new_state.grid[i][j])
                c3 = c3 + 1 if new_state.grid[i][j] == my_pos else c3
            if i == len(new_state.grid) - 3:
                ley_line_dic[name3].append(
                    new_state.grid[i][len(new_state.grid[i]) - 1])
                c3 = c3 + 1 if new_state.grid[i][
                    len(new_state.grid[i]) - 1] == my_pos else c3
            if c3 >= (len(ley_line_dic[name3]) - 1) / 2 and new_state.grid[int(
                    name3[0:2])][int(name3[2:])] == '@':
                new_state.grid[int(name3[0:2])][int(
                    name3[2:])], new_state.current_line[
                        index] = my_pos, new_state.current_line[index] + 1
        return new_state

    def __repr__(self) -> Any:
        """
        Return a representation of this StonehengeState (which can be used for
        equality testing).
        Overrides GameState.__repr__
        """
        player = 'p2'
        if self.p1_turn:
            player = 'p1'
        return "Player {} is playing and result as below: {}".format(player,
                                                                     self.grid)

    def rough_outcome(self) -> float:
        """
        Return an estimate in interval [LOSE, WIN] of best outcome the current
        player can guarantee from state self.
        >>> state = StonehengeState(True, 1)
        >>> state = state.make_move('A')
        >>> state.rough_outcome()
        -1
        """
        move_list = self.get_possible_moves()
        if move_list == []:
            return self.LOSE
        for item in move_list:
            new_state1 = self.make_move(item)
            if new_state1.get_possible_moves() == []:
                return self.WIN
            for item2 in new_state1.get_possible_moves():
                new_state2 = new_state1.make_move(item2)
                if new_state2.get_possible_moves() == []:
                    return self.LOSE
        return self.DRAW

if __name__ == "__main__":
    from doctest import testmod
    testmod()
    from python_ta import check_all
    check_all(config="a2_pyta.txt")


# coding: utf-8

from typing import Dict

import pytest
from httpx import AsyncClient
from src.models.get_level_list_response import GetLevelListResponse  # noqa: F401
from src.models.get_level_response import GetLevelResponse  # noqa: F401
from src.models.level import Level  # noqa: F401

@pytest.mark.asyncio
async def test_add_level(client: AsyncClient) -> None:
    """Test case for add_level

    Add a level
    """
    level = {
        "title": "title",
        "titleEn": "titleEn",
        "author": "author",
        "authorEn": "authorEn",
        "artists": "artists",
        "artistsEn": "artistsEn",
        "description": "No description",
        "descriptionEn": "No description",
        "genre": "general",
        "engine": "a",
        "background": None,
        "skin": None,
        "particle": None,
        "effect": None,
        "cover": "before_hash",
        "bgm": "hash",
        "data": "sus_hash",
        "preview": "hash",
        "length": "0",
        "bpm": 555,
        "notes": 2000,
        "rating": 8,
    }

    headers = {
        "Authorization": "Bearer admin",
    }
    response = await client.request(
        "POST",
        "/levels",
        headers=headers,
        json=level,
    )

    assert response.status_code == 200

@pytest.mark.asyncio
async def test_delete_level(client: AsyncClient) -> None:
    """Test case for delete_level

    Delete a level
    """

    headers = {
        "Authorization": "Bearer admin",
    }
    response = await client.request(
        "DELETE",
        "/levels/{levelName}".format(levelName="botorushippunotabi"),
        headers=headers,
    )

    assert response.status_code == 200

@pytest.mark.asyncio
async def test_edit_level(client: AsyncClient) -> None:
    """Test case for edit_level

    Edit a level
    """
    level = {
        "title": "b",
        "titleEn": "b",
    }

    headers = {
        "Authorization": "Bearer admin",
    }
    response = await client.request(
        "PATCH",
        "/levels/{levelName}".format(levelName="botorushippunotabi"),
        headers=headers,
        json=level,
    )

    assert response.status_code == 200

@pytest.mark.asyncio
async def test_get_level(client: AsyncClient) -> None:
    """Test case for get_level

    Get a level
    """

    headers: Dict[str, str] = {}
    response = await client.request(
        "GET",
        "/levels/{levelName}".format(levelName="botorushippunotabi"),
        headers=headers,
    )

    assert response.status_code == 200

@pytest.mark.asyncio
async def test_get_level_list(client: AsyncClient) -> None:
    """Test case for get_level_list

    Get level list
    """
    params: Dict[str, str] = {
        "localization": "en",
        "page": "0",
        "keywords": "Chino",
        "sort": "0",
        "order": "0",
        "status": "0",
        "author": "any",
        "random": "0",
    }
    headers: Dict[str, str] = {}
    response = await client.request(
        "GET",
        "/levels/list",
        headers=headers,
        params=params,
    )

    assert response.status_code == 200


import unittest
from hydipy.Discrete import DiscreteNode
import pyAgrum as gum
import numpy as np

class Test_DiscreteNOde(unittest.TestCase):
    def test_no_parents(self):
        self.discnode = DiscreteNode(
            "a", [[0.7], [0.2], [0.1]], parents=[], states=["a1", "a2", "a3"]
        )
        self.assertEqual("a", self.discnode.id)
        self.assertIsNone(
            np.testing.assert_array_equal(
                np.array([[0.7], [0.2], [0.1]]), self.discnode.values
            )
        )
        self.assertListEqual([], self.discnode.parents)
        self.assertListEqual(["a1", "a2", "a3"], self.discnode.states)
        self.assertEqual(3, self.discnode.cardinality)
        self.assertIsNone(
            np.testing.assert_array_equal(
                np.array([0.7, 0.2, 0.1]), self.discnode.agrum_cpd()
            )
        )
        bn = gum.BayesNet("test_bn")
        var = self.discnode.agrum_var()
        bn.add(var)
        bn.cpt("a").fillWith(self.discnode.agrum_cpd())
        cpt = bn.cpt("a")
        self.assertTupleEqual(cpt.names, tuple("a"))
        self.assertListEqual(cpt.tolist(), [0.7, 0.2, 0.1])

    def test_one_parent(self):
        self.a = DiscreteNode(
            "a", [[0.7], [0.2], [0.1]], parents=[], states=["a1", "a2", "a3"]
        )
        self.b = DiscreteNode(
            "b", [[0.4, 0.2, 0.9], [0.6, 0.8, 0.1]], parents=["a"], states=["b1", "b2"]
        )
        self.assertEqual("b", self.b.id)
        self.assertIsNone(
            np.testing.assert_array_equal(
                np.array([[0.4, 0.2, 0.9], [0.6, 0.8, 0.1]]), self.b.values
            )
        )
        self.assertListEqual(["a"], self.b.parents)
        self.assertListEqual(["b1", "b2"], self.b.states)
        self.assertEqual(2, self.b.cardinality)
        self.assertListEqual([], self.a.agrum_edges())
        self.assertListEqual([("a", "b")], self.b.agrum_edges())
        self.assertIsNone(
            np.testing.assert_array_equal(np.array([0.7, 0.2, 0.1]), self.a.agrum_cpd())
        )
        self.assertIsNone(
            np.testing.assert_array_equal(
                np.array([0.4, 0.6, 0.2, 0.8, 0.9, 0.1]), self.b.agrum_cpd()
            )
        )
        bn = gum.BayesNet("test_bn")
        var_a = self.a.agrum_var()
        var_b = self.b.agrum_var()
        bn.add(var_a)
        bn.add(var_b)
        bn.addArcs(self.b.agrum_edges())
        bn.cpt("a").fillWith(self.a.agrum_cpd())
        bn.cpt("b").fillWith(self.b.agrum_cpd())
        self.assertListEqual(bn.cpt("a").tolist(), [0.7, 0.2, 0.1])
        self.assertListEqual(bn.cpt("b").tolist(), [[0.4, 0.6], [0.2, 0.8], [0.9, 0.1]])

    def test_two_parents(self):
        self.a = DiscreteNode(
            "a", [[0.7], [0.2], [0.1]], parents=[], states=["a1", "a2", "a3"]
        )
        self.b = DiscreteNode("b", [[0.2], [0.8]], parents=[], states=["b1", "b2"])
        self.c = DiscreteNode(
            "c",
            [[0.4, 0.8, 0.2, 0.9, 0.25, 0.99], [0.6, 0.2, 0.8, 0.1, 0.75, 0.01]],
            parents=["a", "b"],
            states=["c1", "c2"],
        )
        self.assertListEqual([("b", "c"), ("a", "c")], self.c.agrum_edges())
        self.assertIsNone(
            np.testing.assert_array_equal(np.array([0.7, 0.2, 0.1]), self.a.agrum_cpd())
        )
        self.assertIsNone(
            np.testing.assert_array_equal(np.array([0.2, 0.8]), self.b.agrum_cpd())
        )
        self.assertIsNone(
            np.testing.assert_array_equal(
                np.array(
                    [0.4, 0.6, 0.8, 0.2, 0.2, 0.8, 0.9, 0.1, 0.25, 0.75, 0.99, 0.01]
                ),
                self.c.agrum_cpd(),
            )
        )
        bn = gum.BayesNet("test_bn")
        var_a = self.a.agrum_var()
        var_b = self.b.agrum_var()
        var_c = self.c.agrum_var()
        bn.add(var_a)
        bn.add(var_b)
        bn.add(var_c)
        bn.addArcs(self.a.agrum_edges())
        bn.addArcs(self.b.agrum_edges())
        bn.addArcs(self.c.agrum_edges())
        bn.cpt("a").fillWith(self.a.agrum_cpd())
        bn.cpt("b").fillWith(self.b.agrum_cpd())
        bn.cpt("c").fillWith(self.c.agrum_cpd())
        cpt_c = bn.cpt("c")
        self.assertTupleEqual(cpt_c.names, ("c", "b", "a"))
        self.assertListEqual(
            cpt_c.tolist(),
            [
                [[0.4, 0.6], [0.8, 0.2]],
                [[0.2, 0.8], [0.9, 0.1]],
                [[0.25, 0.75], [0.99, 0.01]],
            ],
        )
        self.assertEqual(
            str(cpt_c),
            "\n             ||  c                |\nb     |a     ||c1       |c2       |\n------|------||---------|---------|\nb1    |a1    || 0.4000  | 0.6000  |\nb2    |a1    || 0.8000  | 0.2000  |\nb1    |a2    || 0.2000  | 0.8000  |\nb2    |a2    || 0.9000  | 0.1000  |\nb1    |a3    || 0.2500  | 0.7500  |\nb2    |a3    || 0.9900  | 0.0100  |\n",
        )


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import logging
import os
import json
import io
import typing
import copy

from typing import Any, List, Text

from rasa_core import utils
from rasa_core.policies.policy import Policy
from rasa_core.constants import FALLBACK_SCORE
from rasa_core.events import (
    UserUttered, ActionExecuted,
    Event, SlotSet, Restarted, ActionReverted, UserUtteranceReverted,
    BotUttered)

logger = logging.getLogger(__name__)

if typing.TYPE_CHECKING:
    from rasa_core.domain import Domain
    from rasa_core.trackers import DialogueStateTracker

class ConfirmationPolicy(Policy):
    """Policy which executes a confirmation action if NLU confidence is low
       Should be put before other prediction policies.
        :param float nlu_threshold:
          minimum threshold for NLU confidence.
          If intent prediction confidence is lower than this,
          predict confirmation action with confidence 1.0.

        :param Text confirmation_action_name:
          name of the action to execute as a confirmation.
    """

    @staticmethod
    def _standard_featurizer():
        return None

    def __init__(self,
                 nlu_threshold=0.1,  # type: float
                 confirmation_action_name="action_default_confirmation", # type: Text
                 confirmation_rephrase_name="action_default_rephrase",  # type: Text
                 affirm_intent_name="affirm",  # type: Text
                 deny_intent_name="deny"  # type: Text
                 ):
        # type: (...) -> None

        super(ConfirmationPolicy, self).__init__()

        self.nlu_threshold = nlu_threshold
        self.confirmation_rephrase_name = confirmation_rephrase_name
        self.confirmation_action_name = confirmation_action_name
        self.affirm_intent_name = affirm_intent_name
        self.deny_intent_name = deny_intent_name

        self.cache_message = UserUttered(None)
    def train(self,
              training_trackers,  # type: List[DialogueStateTracker]
              domain,  # type: Domain
              **kwargs  # type: Any
              ):
        # type: (...) -> None
        """Does nothing. This policy is deterministic."""

        pass

    def should_confirm(self,
                        nlu_confidence,  # type float
                        last_action_name  # type: Text
                        ):
        # type: (...) -> bool
        """It should predict confirmation action only if
        a. predicted NLU confidence is lower than ``nlu_threshold`` &&
        b. last action is NOT confirmation action
        """
        return (nlu_confidence < self.nlu_threshold and
                last_action_name != self.confirmation_action_name)

    def confirmation_scores(self, domain, confirmation_score=FALLBACK_SCORE):
        """Prediction scores used if a confirmation is necessary."""

        result = [0.0] * domain.num_actions
        idx = domain.index_for_action(self.confirmation_action_name)
        result[idx] = confirmation_score
        return result

    def predict_action_probabilities(self, tracker, domain):
        # type: (DialogueStateTracker, Domain) -> List[float]
        """Predicts a confirmation action if NLU confidence is low
        """

        nlu_data = tracker.latest_message.parse_data

        # if NLU interpreter does not provide confidence score,
        # it is set to 1.0 here in order
        # to not override standard behaviour
        nlu_confidence = nlu_data["intent"].get("confidence", 1.0)
        '''
        print("\033[92m Events\033[0m")
        for idx, event in enumerate(tracker.events):
            print(str(idx) + '. ' + str(event))
        print("\033[92m Applied Events\033[0m")
        for idx, event in enumerate(tracker.applied_events()):
            print(str(idx) + '. ' + str(event))
        print("\033[33mLatest action:{}\033[0m".format(tracker.latest_action_name))
        print("\033[33mLatest intent:{}\033[0m".format(nlu_data["intent"]["name"]))
        '''
        # know what bot just did before 'action_listen'
        action_history = [evt.action_name for evt in tracker.events if isinstance(evt, ActionExecuted)]
        proceeding_action = action_history[-2] if len(action_history) > 2 else 'action_listen'

        if proceeding_action == self.confirmation_action_name:
            # Last action is confirmation question
            if nlu_data["intent"]["name"] == self.affirm_intent_name:
                # TODO: The user affirms the intent in confirmation, thus,
                # predict next action based on previous intent
                # Revert current `confirm (action) <- affirm (intent)`
                # And retrieve cached message for other policy to predict

                from rasa_core.events import UserUtteranceReverted
                tracker.update(UserUtteranceReverted())
                tracker.update(ActionExecuted('action_listen'))
                tracker.update(self.cache_message)
                tracker.latest_message = self.cache_message
                result = [0.0] * domain.num_actions

            elif nlu_data["intent"]["name"] == self.deny_intent_name:
                # TODO: The user denies the intent in confirmation, thus,
                # predict next action as ask for rephrasing
                # Revert current `confirm (action) <- deny (intent)`.
                from rasa_core.events import UserUtteranceReverted
                result = [0.0] * domain.num_actions
                idx = domain.index_for_action(self.confirmation_rephrase_name)
                result[idx] = FALLBACK_SCORE

            else:
                # TODO: The user gives a rephrase, thus,
                # predict next action based on current intent
                # Just return all 0 and let prediction policy do.
                result = [0.0] * domain.num_actions

        elif proceeding_action == self.confirmation_rephrase_name:
            # TODO: The user shall give another intent after bot ask for rephrase.
            # So predict based on new intent.
            result = [0.0] * domain.num_actions

        elif self.should_confirm(nlu_confidence if nlu_confidence else 1, tracker.latest_action_name):
            logger.debug("NLU confidence {} is lower "
                         "than NLU threshold {}. "
                         "Predicting confirmation action: {}"
                         "".format(nlu_confidence, self.nlu_threshold,
                                   self.confirmation_action_name))
            # we set this to 1.1 to make sure confirmation overrides
            # the memoization policy
            # Meanwhile we need to cache the current message for later prediction,
            # then revert input intent and confirmation action.
            self.cache_message = copy.deepcopy(tracker.latest_message)
            self.cache_message.parse_data['intent']['confidence'] = 1.0
            result = self.confirmation_scores(domain)
        elif tracker.latest_action_name == self.confirmation_action_name:
            result = [0.0] * domain.num_actions
            idx = domain.index_for_action('action_listen')
            result[idx] = FALLBACK_SCORE
        else:
            # NLU confidence threshold is met, as well as not in a confirmation process,
            # so predict all 0, let other policies predict.
            result = [0.0] * domain.num_actions
        '''
        print("\033[92m Events\033[0m")
        for idx, event in enumerate(tracker.events):
            print(str(idx) + '. ' + str(event))
        print("\033[92m Applied Events\033[0m")
        for idx, event in enumerate(tracker.applied_events()):
            print(str(idx) + '. ' + str(event))
        '''
        return result

    def persist(self, path):
        # type: (Text) -> None
        """Persists the policy to storage."""
        config_file = os.path.join(path, 'confirmation_policy.json')
        meta = {
            "nlu_threshold": self.nlu_threshold,
            "confirmation_action_name": self.confirmation_action_name
        }
        utils.create_dir_for_file(config_file)
        utils.dump_obj_as_json_to_file(config_file, meta)

    @classmethod
    def load(cls, path):
        # type: (Text) -> ConfirmationPolicy
        meta = {}
        if os.path.exists(path):
            meta_path = os.path.join(path, "confirmation_policy.json")
            if os.path.isfile(meta_path):
                with io.open(meta_path) as f:
                    meta = json.loads(f.read())

        return cls(**meta)


import mx_layers as layers

def _convolution(**kwargs):
  defaults = {'kernel_shape' : (3, 3), 'stride' : (1, 1), 'pad' : (1, 1), 'no_bias' : True}
  defaults.update(kwargs)
  return layers.convolution(**defaults)

def _normalized_convolution(network, **kwargs):
  network = layers.batch_normalization(network, fix_gamma=False)
  network = layers.ReLU(network)
  network = _convolution(X=network, **kwargs)
  return network

def _module(network, n_filters, n_layers):
  for index in range(n_layers):
    identity = network
    network = _normalized_convolution(network, n_filters=n_filters)
    network = _normalized_convolution(network, n_filters=n_filters)
    network += identity

  return network

def _transit(network, n_filters):
  '''
  identity = \
      _convolution(X=network, n_filters=n_filters, kernel_shape=(1, 1), stride=(2, 2), pad=(0, 0))
  '''
  identity = layers.pooling(X=network, mode='maximum', kernel_shape=(2, 2), stride=(2, 2), pad=(0, 0))
  identity = _convolution(X=identity, n_filters=n_filters, kernel_shape=(1, 1), pad=(0, 0))

  network = _normalized_convolution(network, n_filters=n_filters, stride=(2, 2))
  network = _normalized_convolution(network, n_filters=n_filters)

  return identity + network

def build_network(n_layers):
  network = layers.variable('data')
  network = _convolution(X=network, n_filters=16)

  for n_filters in (16, 32):
    network = _module(network, n_filters, n_layers)
    network = _transit(network, n_filters * 2)

  network = _module(network, 64, n_layers)
  network = layers.batch_normalization(network, fix_gamma=False)
  network = layers.ReLU(network)

  network = layers.pooling(X=network, mode='average', kernel_shape=(8, 8), stride=(1, 1), pad=(0, 0))
  network = layers.flatten(network)
  network = layers.batch_normalization(network, fix_gamma=False)
  network = layers.fully_connected(X=network, n_hidden_units=10)
  network = layers.softmax_loss(prediction=network, normalization='batch', id='softmax')

  return network

if __name__ == '__main__':
  from argparse import ArgumentParser
  parser = ArgumentParser()
  parser.add_argument('--batch_size', type=int, default=128)
  parser.add_argument('--initial_lr', type=float, default=0.1)
  parser.add_argument('--n_layers', type=int, required=True)
  parser.add_argument('--postfix', type=str, default='')
  args = parser.parse_args()

  network = build_network(n_layers=args.n_layers)

  from lr_scheduler import AtIterationScheduler
  lr_table = {32000 : args.initial_lr * 0.1, 48000 : args.initial_lr * 0.01}
  lr_scheduler = AtIterationScheduler(args.initial_lr, lr_table)

  optimizer_settings = {
    'args'         : {'momentum' : 0.9},
    'initial_lr'   : args.initial_lr,
    'lr_scheduler' : lr_scheduler,
    'optimizer'    : 'SGD',
    'weight_decay' : 0.0001,
  }

  from mx_initializers import PReLUInitializer
  initializer = PReLUInitializer()

  from mx_solver import MXSolver
  solver = MXSolver(
    batch_size         = args.batch_size,
    devices            = (0, 1, 2, 3),
    epochs             = 150,
    initializer        = initializer,
    optimizer_settings = optimizer_settings,
    symbol             = network,
    verbose            = True,
  )

  from data_utilities import load_cifar10_record
  data = load_cifar10_record(args.batch_size)

  info = solver.train(data)

  postfix = '-' + args.postfix if args.postfix else ''
  identifier = 'residual-network-on-cifar-10-%d%s' % (args.n_layers, postfix)

  import cPickle as pickle
  pickle.dump(info, open('info/%s' % identifier, 'wb'))
  parameters = solver.export_parameters()
  pickle.dump(parameters, open('parameters/%s' % identifier, 'wb'))


#!/usr/bin/env python3

#/mnt/c/Users/romie/Desktop/Fac/Master_1_BCD/HMIN113M (ordi baraque)
#/mnt/c/Users/romie/Desktop/Fac/HMIN113 (ordi portabe)

import os, sys, re, numpy
file=sys.argv[1]
dicoChro={}
dicoNm={}
list1=[]
list2=[]
list3=[]
n=1
t=0
with open(file,"r") as vcf :
	for ligne in vcf :

		info1=re.search("^#(\w*)\s(\w*)\s(\w*)\s(\w*)\t(\w*)\s(\w*)\s(\w*)\s(\w*)\s",ligne)
		info2=re.search("(^#)*.",ligne)

		if info1 :
			Chro=str(info1.group(1))
			list2.append(Chro)

			Pos=str(info1.group(2))
			list2.append(Pos)

			Id=str(info1.group(3))
			list2.append(Id)

			Ref=str(info1.group(4))
			list2.append(Ref)

			Alt=str(info1.group(5))
			list2.append(Alt)

			Qual=str(info1.group(6))
			list2.append(Qual)

			Fil=str(info1.group(7))
			list2.append(Fil)

			Inf=str(info1.group(8))
			list2.append(Inf)

		if not info2.group(1) :

			list1=ligne.split("\t")

			if Chro in dicoChro:

				if list1[0] in dicoChro[Chro]:

					while n<7 :
						if list1[n] in dicoChro[Chro][list1[0]] :
							print("ca marche")

							#else :
								#dicoChro[Chro][list1[0]][list2[n]]=[]
							#n+=1

							#if list1[7] in dicoChro[Chro][list1[0]] :
								#print("ca marche")

						else :

							print(dicoChro[1].values())
							dicoChro[Chro][list1[0]][list2[n]]={}

						n+=1
				else :
					dicoChro[Chro][list1[0]]={}

			else :
				dicoChro[Chro]={}

print(list1[1])
print(dicoChro)


p = 773
q = 149

n = p * q

phi = (p-1) * (q-1)

e = 2

def gcd(a, b):
    while a != b:
        if(a > b):
            a -= b
        else:
            b -= a
    return a

while(e < phi):
    if(gcd(e, phi) == 1 and gcd(e, n) == 1):
        break
    e += 1

k = 1

while k * e % phi != 1: k += 1

def encrypt(message):
    return (message ** e) % n

c = encrypt(100)

print(c ** k % n)


import pandas as pd

import matplotlib.pyplot as plt

def load(source):
    csv = pd.read_csv(source)
    return csv

iris_csv = load("iris.csv")

print(iris_csv)

# primero

iris_csv.plot()

# segundo

iris_csv["SepalLengthCm"].plot()

# tercero

iris_csv.plot.scatter(x="SepalLengthCm", y="PetalLengthCm", alpha=0.5)

# cuarto

iris_csv.plot.box()

# Quinto

iris_csv.plot.area(figsize=(12, 4), subplots=True)

fig, axs = plt.subplots(figsize=(12, 4))
iris_csv.plot.area(ax=axs)
axs.set_ylabel("Concentración de tamaños")
fig.savefig("concentracion.png")
plt.show()


from typing import *

from PyQt5.QtWidgets import *
from core.Tag import Tag

from core.Utils import Utils

class PropertySheetPanel(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.m_env = self.width()
        self.m_Properties = None    #type:Dict
        self.m_Methods = None       #type:Dict
        self.m_Editors = []
        self.m_Labels = []  # type:List[QLabel]
        self.m_Views = []  # type:List[QWidget]

    def setTarget(self, target: object):
        if self.layout() is None:
            layout = QFormLayout()
            self.setLayout(layout)
        else:
            layout = self.layout()
        while layout.rowCount() > 0:
            layout.removeRow(0)
        self.m_Labels.clear()
        self.m_Views.clear()
        self.setVisible(False)
        self.m_NumEditable = 0
        self.m_Target = target
        # 忽略类型，直接固定的接口，接口包括返回所有属性name的List和所有方法name的List
        self.m_Properties = target.getAllProperties()       #type:Dict
        self.m_Methods = target.getAllMethods()         #type:Dict
        def setValue(name: str,attrType:type):
            if attrType is bool:
                def callSet(option:str):
                    if option.lower() == "true":
                        setattr(target,name,True)
                    else:
                        setattr(target,name,False)
            else:
                def callSet(option):
                    if option != "":
                        if attrType is int:
                            setattr(target, name, int(option))
                        elif attrType is float:
                            setattr(target,name,float(option))
            return callSet

        for name,value in self.m_Properties.items():
            val=None
            try:
                property = getattr(target, name)
                if value != "":
                    try:
                        val=getattr(target,value)
                    except AttributeError:
                        pass
            except AttributeError:
                continue

            # print("name:", name, "type:", type(property))
            label = QLabel(name)
            # func = setValue(name,type(property))
            if isinstance(property, bool):
                view = QComboBox()
                view.addItems(['False', 'True'])
                if property:
                    view.setCurrentIndex(1)
                else:
                    view.setCurrentIndex(0)
                # view.currentIndexChanged[str].connect(copy.deepcopy(func))
            elif val is not None:
                items=[]
                view=QComboBox()
                cur=i=0
                for item in val:
                    if isinstance(item,Tag):
                        items.append(item.getReadable())
                        if property == item.getID():
                            cur=i
                        i+=1
                view.addItems(items)
                view.setCurrentIndex(cur)
            else:
                view = QLineEdit()
                view.setText(str(value))
                # view.textChanged.connect(copy.deepcopy(func))
            layout.addRow(label, view)
            self.m_Labels.append(label)
            self.m_Views.append(view)
        self.setFixedHeight(40 * len(self.m_Properties))
        Utils.debugOut("layout row count:", layout.rowCount())
        self.repaint()
        self.show()

    def setLayout(self, a0: 'QLayout'):
        super().setLayout(a0)
        self.layoutMgr = a0

    def updateObject(self):
        for index in range(len(self.m_Labels)):
            labelName=self.m_Labels[index].text()
            setMethodName=self.m_Methods.get(labelName)
            if setMethodName is not None:
                try:
                    setMethod=getattr(self.m_Target,setMethodName)
                except AttributeError:
                    continue
                view=self.m_Views[index]
                if isinstance(view,QLineEdit):
                    setMethod(view.text())
                elif isinstance(view,QComboBox):
                    setMethod(view.currentIndex())

# Test
# from PyQt5.QtCore import *
# from PyQt5.QtWidgets import *
# import sys
#
#
# class A():
#     properties=['number','flag']
#     methods=['Func']
#     def __init__(self):
#         self.number=5
#         self.flag=True
#
#     def Func(self):
#         print("A")
#
#     def getAllProperties(self):
#         return self.properties
#
#     def getAllMethods(self):
#         return self.methods
#
#
# if __name__ == "__main__":
#     app = QApplication(sys.argv)
#     a=A()
#     win = PropertySheetPanel()
#     win.setTarget(a)
#     win.show()
#     sys.exit(app.exec_())