import collections
from itertools import combinations
import requests
import time
from tqdm import tqdm

BLACK = '0'
YELLOW = '1'
GREEN = '2'

def get_all_possible_words():
    word_list = []
    with open('./data/words.txt') as f:
        word_list.extend([word.strip() for word in f.readlines()])

    return word_list

def build_word_matches_naive():
    words = get_all_possible_words()
    # start = time.time()
    out = {}

    combos = list(combinations(words, 2))
    with tqdm(combos, desc='Warming up', mininterval=0.3) as combos_progress:
        for (w1 ,w2) in combos_progress:
            match = check_wordle(w1,w2)
            if not out.get(w1): out[w1] = {}
            out[w1][w2] = match
        # end = time.time()
        # print(end - start, len(out))
    return out

def get_match_for_words(word1, word2, matches):
    try:
        if matches.get(word1) and matches[word1].get(word2):
            return matches[word1][word2]
        elif matches.get(word2) and matches[word2].get(word1):
            return matches[word2][word1]
    except KeyError:
        print(word1, word2)
        raise Exception(f'cant find match for words {word1}, {word2}')

def check_wordle(word, target_word):
    out = [BLACK] * 5
    count = collections.Counter(target_word)

    for i in range(5):
        char = word[i]
        if char == target_word[i]:
            count[char] -= 1
            out[i] = GREEN

    for i in range(5):
        char = word[i]
        if out[i] == BLACK and count[char] > 0 and char in target_word:
                count[char] -= 1
                out[i] = YELLOW
    return ''.join(map(str, out))

def get_matches_for_guesses(words, guesses):
    guesses = iter(guesses)
    for guess in guesses:
        match = next(guesses)
        words = [word for word in words if(does_guess_match_word(word, guess, match))]
    return words

def get_matches_for_guess(words, guess, match):
    return [word for word in words if(does_guess_match_word(word, guess, match))]

def does_guess_match_word(word, guess, match):
    count = collections.Counter(word)
    for position, char in enumerate(match):
        if char == GREEN:
            if word[position] != guess[position]:
                return False
            count[guess[position]] -= 1
            continue

    for position, char in enumerate(match):
        if char == YELLOW:
            if word[position] == guess[position] or (word[position] != guess[position] and count[guess[position]] == 0):
                return False

            count[guess[position]] -= 1
            continue

        if char == BLACK and count[guess[position]] > 0:
            return False
    return True

def get_next_best_word_naive(words, matches):
    # start = time.time()
    out = []
    for x in words:
        lengths = []
        for y in words:
            if x == y: continue
            match = get_match_for_words(x, y, matches)
            lengths.append(len(get_matches_for_guess(words, x, match)))
        mean = sum(lengths)/len(lengths)
        # print(x, mean)
        out.append((x, mean))

    out.sort(reverse=True, key=lambda tup: tup[1])
    # end = time.time()
    # print('naive', end - start)
    return out[0:10]

def get_next_best_word_log(words, matches):
    start = time.time()
    lengths = {}
    for x, y in combinations(words, 2):
        match = get_match_for_words(x, y, matches)
        length = len(get_matches_for_guess(words, x, match))
        if not lengths.get(x): lengths[x] = []
        if not lengths.get(y): lengths[y] = []
        lengths[x].append(length)
        lengths[y].append(length)

    out = [ ( x, sum(lengths[x])/len(lengths[x]))   for x in lengths]
    out.sort(reverse=True, key=lambda tup: tup[1])
    # end = time.time()
    # print('log', end - start)

    return out[0:10]

def get_next_best_word_log_memoed(words, matches, limit):
    # start = time.time()
    lengths = {}
    colours = {}

    combos = list(combinations(words, 2))
    with tqdm(combos, desc=f'Getting {limit} next best guesses', mininterval=0.3) as combos_progress:
        for x, y in combos_progress:
            match = get_match_for_words(x, y, matches)
            length = len(get_memoed_matches_for_guesses(words, x, match, colours))
            if not lengths.get(x): lengths[x] = []
            if not lengths.get(y): lengths[y] = []
            lengths[x].append(length)
            lengths[y].append(length)

    out = [ ( x, sum(lengths[x])/len(lengths[x]))   for x in lengths]
    out.sort(reverse=True, key=lambda tup: tup[1])
    # end = time.time()
    # print('log', end - start)

    return out[0:limit]

def get_memoed_matches_for_guesses(words, x, match, memo):
    if not memo.get(x): memo[x] = {}
    if not memo[x].get(match):
        memo[x][match] = get_matches_for_guess(words, x, match)

    return memo[x][match]


from fastapi.testclient import TestClient
from fastapi import status

from src.gerenciador import TASKS, Task, task_manager_app

def test_quando_listar_tarefas_devo_ter_como_retorno_codigo_de_status_200():
    client = TestClient(task_manager_app)

    response = client.get('/task')
    assert response.status_code == status.HTTP_200_OK

def test_quando_listar_tarefas_formato_de_retorno_deve_ser_json():
    client = TestClient(task_manager_app)

    response = client.get('/task')
    assert response.headers['Content-Type'] == 'application/json'

def test_quando_listar_tarefas_retorno_deve_ser_uma_lista():
    client = TestClient(task_manager_app)

    response = client.get('/task')
    assert isinstance(response.json(), list)

def test_quando_listar_tarefas_a_tarefa_retornada_deve_possuir_ID():
    client = TestClient(task_manager_app)
    TASKS.append({'id': 1})

    response = client.get('/task')
    assert 'id' in response.json().pop()

    TASKS.clear()

def test_quanto_listar_tarefas_a_tarefa_deve_possuir_titulo():
    client = TestClient(task_manager_app)
    TASKS.append({'title': 'Exemplo de Título'})

    response = client.get('/task')
    assert 'title' in response.json().pop()

    TASKS.clear()

def test_quando_listar_tarefas_a_tarefa_deve_possuir_descrição():
    client = TestClient(task_manager_app)
    TASKS.append({"description": 'Exemplo de descrição'})

    response = client.get('/task')
    assert "description" in response.json().pop()

    TASKS.clear()

def test_quando_listar_tarefas_a_tarefa_deve_possuir_estado():
    TASKS.append({'state': 'Exemplo de estado'})
    client = TestClient(task_manager_app)

    response = client.get('/task')
    assert 'state' in response.json().pop()

    TASKS.clear()

def test_recurso_tarefas_deve_aceitar_o_verbo_POST():
    client = TestClient(task_manager_app)

    response = client.post('/task')
    assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY

def test_quando_uma_tarefa_e_submetida_deve_possuir_um_titulo():
    client = TestClient(task_manager_app)

    response = client.post('/task', json={})
    assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY

def test_titulo_da_tarefa_deve_conter_entre_3_e_50_caracteres():
    client = TestClient(task_manager_app)

    response = client.post('/task', json={'title': '*' * 2})
    assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY

    response = client.post('/task', json={'title': '*' * 51})
    assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY

def test_quando_uma_tarefa_e_submetida_deve_possuir_uma_descricao():
    client = TestClient(task_manager_app)

    response = client.post('/task', json={'title': 'Título'})
    assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY

def test_descrição_da_tarefa_pode_conter_no_maximo_200_caracteres():
    client = TestClient(task_manager_app)

    response = client.post('/task',
                           json={
                               'title': 'Título',
                               "description": '*' * 201
                           })
    assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY

def test_quando_criar_uma_tarefa_a_mesma_deve_ser_retornada():
    client = TestClient(task_manager_app)
    task = {
        'title': 'Título de exemplo',
        "description": "Descrição de exemplo.",
        'state': 'não finalizado'
    }

    response = client.post('/task', json=task)
    assert response.json()['title'] == task['title'] and \
           response.json()['description'] == task['description'] and \
           response.json()['state'] == task['state']

def test_quando_criar_uma_tarefa_seu_id_deve_ser_unico():
    client = TestClient(task_manager_app)
    task_1 = {
        'title': 'Exemplo de Título',
        "description": 'Exemplo de descrição',
    }
    task_2 = {
        'title': 'Exemplo de Título',
        "description": 'Exemplo de descrição',
    }

    response_1 = client.post('/task', json=task_1)
    response_2 = client.post('/task', json=task_2)

    assert response_1.json()['id'] != response_2.json()['id']

def test_quando_criar_uma_tarefa_seu_estado_padrao_deve_ser_nao_finalizado():
    client = TestClient(task_manager_app)
    task = {
        'title': 'Exemplo de Título',
        "description": 'Exemplo de descrição'
    }

    response = client.post('/task', json=task)
    assert response.json()['state'] == 'não finalizado'

def test_quando_criar_uma_tarefa_codigo_de_status_retornado_deve_ser_201():
    client = TestClient(task_manager_app)
    task = {
        'title': 'Exemplo de Título',
        "description": 'Exemplo de descrição.'
    }

    response = client.post('/task', json=task)

    assert response.status_code == status.HTTP_201_CREATED

def test_quando_criar_uma_nova_tarefa_a_mesma_deve_ser_persistida():
    client = TestClient(task_manager_app)

    task = {
        'title': 'Exemplo de Título',
        'description': 'Exemplo de descrição'
    }
    response = client.post('/task', json=task)

    print(response.json())

    assert response.status_code == status.HTTP_201_CREATED and len(TASKS) == 6

    TASKS.clear()


# 6. В одномерном массиве найти сумму элементов, находящихся между минимальным и максимальным элементами.
# Сами минимальный и максимальный элементы в сумму не включать.
import random

def main():
    lst = [random.randint(0, 80) for _ in range(0, 12)]
    print(lst)
    local_max = {'idx': 0, 'value': lst[0]}
    local_min = {'idx': 0, 'value': lst[0]}
    local_sum = 0
    for i, item in enumerate(lst):
        if item > local_max['value']:
            local_max['idx'] = i
            local_max['value'] = item
        if item < local_min['value']:
            local_min['idx'] = i
            local_min['value'] = item
    if local_max['idx'] > local_min['idx']:
        max_idx = local_max['idx']
        min_idx = local_min['idx'] + 1
    else:
        max_idx = local_min['idx']
        min_idx = local_max['idx'] + 1

    for i in range(min_idx, max_idx):
        local_sum += lst[i]
    print(f'Значение максимального элемента {local_max["value"]}, а его индекс {local_max["idx"]}')
    print(f'Значение минимального элемента {local_min["value"]}, а его индекс {local_min["idx"]}')
    print(f'Сумма элементов между минимальным и максимальным элментом {local_sum}')


import random
import wsnsimpy.wsnsimpy_tk as wsp

# SOURCE = [9,49,99,94,90]
SOURCE = [99]
ACTIVE= [1,0,0,0,0,0]
SINK = 0

hole = []
received = []

###########################################################
class MyNode(wsp.Node):
    tx_range = 100

    ##################
    def init(self):
        super().init()
        self.recv = False
        #test initial energy
        self.energy = 100

    ##################
    def run(self):
        # print("node id : "+ str(self.id) + "time now: "+str(sim.now))
        if self.id in SOURCE:
            self.scene.nodecolor(self.id,0,0,0)
            self.recv = True
            yield self.timeout(2)
            self.start_process(self.start_send())
        if self.id == SINK:
            self.scene.nodecolor(self.id,0,1,0)
        else:
            self.scene.nodecolor(self.id,.7,.7,.7)

    ##################
    def on_receive(self, sender, **kwargs):
        self.energy -=2
        self.log(f"Receive message from {sender}")
        if self.id == SINK:
            self.log(f"SINK Received from {sender}")
            received.append(sender)
            print (received)
            return
        self.log(f"New message; prepare to forward")
        self.scene.nodecolor(self.id,1,0,0)
        yield self.timeout(random.uniform(0.5,1.0))
        seq = kwargs['seq']
        self.forward(self.id,seq)

    def forward(self,src,seq):
        self.energy -=5
        self.next = self.get_sgp()
        self.log(f"Forwarded to SINK via {self.next}")
        self.send(self.next, msg='data', src=src, seq=seq)

    def get_sgp(self):
        distances = list()
        for neighbor in self.neighbors:
            if neighbor.id == SINK:
                return SINK
            for distance in neighbor.neighbor_distance_list:
                if distance[1].id == SINK:
                    instances = (distance[0],neighbor)
                    distances.append(instances)
        return min(distances)[1].id

    def start_send(self):
        print(self.id)
        self.scene.clearlinks()
        seq = 0
        while True:
            yield self.timeout(1)
            self.log(f"start send from source with seq {seq}")
            self.forward(self.id, seq)
            seq += 1
###########################################################
sim = wsp.Simulator(
        until=100,
        timescale=1,
        visual=True,
        terrain_size=(700,700),
        title="Flooding Demo")
for x in range(10):
    for y in range(10):
        px = 50 + x*60 + random.uniform(-20,20)
        py = 50 + y*60 + random.uniform(-20,20)
        node = sim.add_node(MyNode, (px,py))
        node.tx_range = 100
        node.logging = True

sim.run()


import numpy as np
import evaluate
import torch
import torch.nn as nn

def compute_metrics(model,tokenizer, dataloaderTest, loss_preds_fc, epoch, CFG):

    metrics = {}
    bleu = evaluate.load("bleu")
    metrics[f"BLEU_1"] = 0
    metrics[f"BLEU_2"] = 0
    metrics[f"BLEU_3"] = 0
    metrics[f"BLEU_4"] = 0
    metrics[f"LOSS"] = 0
    rouge = evaluate.load('rouge')
    metrics["ROUGE"] = 0

    preds = []
    targets = []

    example_index = np.random.randint(len(dataloaderTest))
    for j, (inputs, labels) in enumerate(dataloaderTest):

        out = model(**inputs.to(CFG.device)).logits

        loss = loss_preds_fc(
            nn.functional.log_softmax(out,dim=-1),
            inputs["labels"]) / inputs["input_ids"].size(0)

        metrics[f"LOSS"] += loss.detach().cpu().numpy()

        output_dict = model.generate(
            input_ids = inputs["input_ids"],
            attention_mask = inputs["attention_mask"],
            decoder_start_token_id = tokenizer.lang_code_to_id["de_DE"],
            num_beams=CFG.num_beams,
            length_penalty=CFG.length_penalty,
            max_length=100,
            repetition_penalty = CFG.repetition_penalty,
            early_stopping = CFG.early_stopping,
            do_sample=False,
            return_dict_in_generate=True)
        raw_preds = tokenizer.batch_decode(output_dict['sequences'], skip_special_tokens=True)

        for i in range(len(raw_preds)):
            targets.append(labels[i])
            if j == example_index:
                metrics[f"EXAMPLE"] = f"pred: {raw_preds[i]}, target: {labels[i]}"
            if raw_preds[i]:
                preds.append(raw_preds[i])
            else:
                preds.append("@")

    metrics[f"BLEU_1"] += bleu.compute(predictions = preds, references = [[target] for target in targets], max_order = 1).get("bleu")
    metrics[f"BLEU_2"] += bleu.compute(predictions = preds, references = [[target] for target in targets], max_order = 2).get("bleu")
    metrics[f"BLEU_3"] += bleu.compute(predictions = preds, references = [[target] for target in targets], max_order = 3).get("bleu")
    metrics[f"BLEU_4"] += bleu.compute(predictions = preds, references = [[target] for target in targets], max_order = 4).get("bleu")
    metrics[f"ROUGE"] += rouge.compute(predictions = preds, references = [[target] for target in targets]).get("rouge1")
    metrics[f"LOSS"] /= len(dataloaderTest)

    return metrics


def count(mask, words):
    cnt = 0
    for word in words:
        if (word & ((1 << 26) - 1 - mask)) == 0:
            cnt += 1
    return cnt

def go(index, k, mask, words):
    if k < 0:  # k 개의 단어를 모두 고르면 끝
        return 0
    if index == 26:  # 성공적으로 26개의 알파벳 순회가 끝나면 끝
        return count(mask, words)
    ans = 0
    t1 = go(index + 1, k - 1, mask | (1 << index), words)
    if ans < t1:  # ans < t1일 경우 t1을 ans로
        ans = t1
    if index not in [ord('a') - ord('a'), ord('n') - ord('a'), ord('t') - ord('a'), ord('i') - ord('a'), ord('c') - ord('a')]:
        t2 = go(index + 1, k, mask, words)
        if ans < t2:  # ans < t2일 경우 t2를 ans로
            ans = t2
    return ans

n, m = map(int, input().split())
words = [0] * n
for i in range(n):
    s = input()
    for x in s:
        words[i] |= (1 << (ord(x) - ord('a')))
print(go(0, m, 0, words))


hobit = []
for i in range(9):
    hobit.append(int(input()))
hobit.sort()
total = sum(hobit)
breaker = 1
for i in range(1, 9):
    for j in range(i):
        if (total - 100) == (hobit[i] + hobit[j]):
            hobit.remove(hobit[i])
            hobit.remove(hobit[j])
            breaker -= 1
            break
    if breaker == 0:
        break

for i in hobit:
    print(i)


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

class Solution:
    # @param {TreeNode} root
    # @return {string[]}

    def binaryTreePaths(self, root):

        def getPath(node, pathlist):
            if not node.left and not node.right:
                ans.append(pathlist)
                return
            if node.left:
                getPath(node.left, pathlist + '->' + str(node.left.val))
            if node.right:
                getPath(node.right, pathlist + '->' + str(node.right.val))

        if not root:
            return []
        ans = []
        getPath(root, str(root.val))
        return ans


from collections import namedtuple
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
import pdb

def to_scalar(var):
    # returns a python float
    return var.view(-1).data.tolist()[0]

def argmax(vec):
    # vec is only 1d vec
    # return the argmax as a python int
    _, idx = torch.max(vec, 0)
    return to_scalar(idx)

# the input is 2d dim tensor
# output 1d tensor
def argmax_m(mat):
    ret_v, ret_ind = [], []
    m, n = mat.size()
    for i in range(m):
        ind_ = argmax(mat[i])
        ret_ind.append(ind_)
        ret_v.append(mat[i][ind_])
    if type(ret_v[0]) == Variable or type(ret_v[0]) == torch.Tensor:
        return ret_ind, torch.cat(ret_v)
    else:
        return ret_ind, torch.Tensor(ret_v)

# Compute log sum exp in a numerically stable way for the forward algorithm
# vec is n * n, norm in row
def log_sum_exp_m(mat):
    row, column = mat.size()
    ret_l = []
    for i in range(row):
        vec = mat[i]
        max_score = vec[argmax(vec)]
        max_score_broadcast = max_score.view( -1).expand(1, vec.size()[0])
        ret_l.append( max_score + \
        torch.log(torch.sum(torch.exp(vec - max_score_broadcast))))
    return torch.cat(ret_l, 0)

def log_sum_exp(vec_list):
    tmp_mat = torch.stack(vec_list, 0)
    m,n = tmp_mat.size()
    # value may be nan because of gradient explosion
    try:
        max_score = torch.max(tmp_mat)
    except:
        pdb.set_trace()
    max_expand = max_score.expand(m, n)
    max_ex_v = max_score.expand(1, n)
    # sum along dim 0
    ret_val = max_ex_v + torch.log(torch.sum(torch.exp(tmp_mat - max_expand), 0))
    return ret_val

# vec1 and vec2 both 1d tensor
# return 1d tensor
def add_broad(vec1, vec2):
    s_ = vec1.size()[0]
    vec1 = vec1.expand(3, s_).transpose(0,1)
    vec2 = vec2.expand(s_, 3)
    new_vec = vec1 + vec2
    return new_vec.view(-1)

# transform a list to 1d vec
def to_1d(vec_list):
    ret_v = vec_list[0].clone()
    v_l = len(vec_list)
    for i in range(1, v_l):
        ret_v = add_broad(ret_v, vec_list[i])
    return ret_v

def to_ind(num, logit):
    ret_l = []
    for i in reversed(range(logit)):
        tmp = num / 3 ** i
        num = num - tmp * 3 ** i
        ret_l.append(tmp)
    return list(reversed(ret_l))

def create_empty_var(if_gpu):
    if if_gpu:
        loss = Variable(torch.Tensor([0]).cuda())
    else:
        loss = Variable(torch.Tensor([0]))
    return loss


import datetime
import socket

from six import text_type, string_types

from werkzeug import import_string

def get_task_history(last_task):
    """Append last task to task history."""
    if hasattr(last_task, 'branch') and last_task.branch:
        return
    elif hasattr(last_task, 'hide') and last_task.hide:
        return
    else:
        return get_func_info(last_task)

def get_func_info(func):
    """Retrieve a function's information."""
    name = func.__name__
    doc = func.__doc__ or ""
    try:
        nicename = func.description
    except AttributeError:
        if doc:
            nicename = doc.split('\n')[0]
            if len(nicename) > 80:
                nicename = name
        else:
            nicename = name
    parameters = []
    try:
        closure = func.func_closure
    except AttributeError:
        closure = func.__closure__
    try:
        varnames = func.func_code.co_freevars
    except AttributeError:
        varnames = func.__code__.co_freevars

    if closure:
        for index, arg in enumerate(closure):
            if not callable(arg.cell_contents):
                parameters.append((varnames[index],
                                   text_type(arg.cell_contents)))
    return ({
        "nicename": nicename,
        "doc": doc,
        "parameters": parameters,
        "name": name,
        "time": str(datetime.datetime.now()),
        "hostname": socket.gethostname(),
    })

def get_workflow_info(func_list):
    """Return function info, go through lists recursively."""
    funcs = []
    for item in func_list:
        if item is None:
            continue
        if isinstance(item, list):
            funcs.append(get_workflow_info(item))
        else:
            funcs.append(get_func_info(item))
    return funcs

def obj_or_import_string(value, default=None):
    """Import string or return object."""
    if isinstance(value, string_types):
        return import_string(value)
    elif value:
        return value
    return default


import pandas as pd
import numpy as np

import matplotlib.pyplot as plt
from matplotlib import gridspec
from pandas import Grouper, DataFrame
from pandas.plotting import lag_plot, autocorrelation_plot
import seaborn as sns
from sklearn.preprocessing import MinMaxScaler

def draw_centralTedency(dataframe):
    fig = plt.figure()
    gs = gridspec.GridSpec(8, 8)
    ax = plt.gca()
    mean = dataframe.mean().values[0]
    median = dataframe.median().values[0]
    mode = dataframe.mode().values[0]
    sns.distplot(dataframe, ax=ax)
    ax.axvline(mean, color='r', linestyle='--')
    ax.axvline(median, color='g', linestyle='-')
    ax.axvline(mode, color='b', linestyle='-')
    plt.legend({'Mean': mean, 'Median': median, 'Mode': mode})
    plt.show()

def barplot_features_with_date(dataset:DataFrame, columns):
    pos = np.arange(len(dataset['date']))

    for column in columns:
        if column == 'USD ISE':
            continue
        _, ax = plt.subplots()
        dataset.plot(kind='bar', x=dataset.columns.get_loc("date"), y=dataset.columns.get_loc(column), color='blue',
                     ax=ax)
        dataset.plot(kind='bar', x=dataset.columns.get_loc("date"), y=dataset.columns.get_loc('USD ISE'), color='red',
                     ax=ax)
        ticks = plt.xticks(pos[::15], rotation=90)
        plt.rcParams["figure.figsize"] = (15, 8)
        plt.show()

def timeseries_to_supervised(df, n_in, n_out):
    agg = pd.DataFrame()
    for i in range(n_in, 0, -1):
        df_shifted = df.shift(i).copy()
        df_shifted.rename(columns=lambda x: ('%s(t-%d)' % (x, i)), inplace=True)
        agg = pd.concat([agg, df_shifted], axis=1)

    for i in range(0, n_out):
        df_shifted = df.shift(-i).copy()
        if i == 0:
            df_shifted.rename(columns=lambda x: ('%s(t)' % (x)), inplace=True)
        else:
            df_shifted.rename(columns=lambda x: ('%s(t+%d)' % (x, i)), inplace=True)
        agg = pd.concat([agg, df_shifted], axis=1)
    agg.dropna(inplace=True)
    return agg

def preprocess(dataset: DataFrame, n_in: int, n_out: int, show_plots: bool):
    # plot the data to get insights
    dataset.reset_index(inplace=True, drop=True)
    dataset = dataset.iloc[0:530]
    # print(dataset['USD ISE'])
    columns = ['TL ISE', 'USD ISE', 'SP', 'DAX', 'FTSE', 'NIKEEI', 'BOVESPA', 'EU', 'EM']
    if show_plots == True:
        # years = DataFrame()
        # barplot_features_with_date(dataset, columns)
        #
        # for index, row in pd.concat([dataset['date'], dataset['USD ISE']], axis=1).iterrows():
        #     years.loc[index, row['date'].month] = row['USD ISE']
        #
        # print(years)
        dataset.boxplot()
        plt.show()
        plt.plot(dataset['USD ISE'])
        dataset.hist()
        plt.show()
        ax = plt.gca()
        dataset.plot(kind='line', x=0, y=3, color='blue', ax=ax)
        dataset.plot(kind='scatter', x=0, y=3, color='red', ax=ax)
        plt.show()

        for column in columns:
          #  figure(1, figsize=(6, 6))
            lag_plot(dataset[column].to_frame())
            #plt.savefig(column+'.png')
            plt.xlabel(column + ' (t)')
            plt.ylabel(column + ' (t + 1)')
            plt.show()

        for column in columns:
            autocorrelation_plot(dataset[column])
            plt.xlabel(column + ' Autocorrelation')
            plt.ylabel(column + ' Lag')
            plt.show()

    del dataset['date']
    # if show_plots == True:
    #     for column in columns:
    #         draw_centralTedency(dataset[column].to_frame())
    feature_scaler = MinMaxScaler(feature_range=(-1, 1))
    index_scaler = MinMaxScaler(feature_range=(-1, 1))

    # dataframe = scaler.fit_transform(np.reshape(dataset.values, (dataset.shape[0], dataset.shape[1])))
    # dataframe = pd.DataFrame(data=dataframe, columns=columns)
    print(dataset.shape)

    stock = dataset['USD ISE'].to_frame()
    scaled_stock = index_scaler.fit_transform(np.reshape(stock['USD ISE'].values, (stock.shape[0], stock.shape[1])))
    scaled_stock = pd.DataFrame(data=scaled_stock, columns=['USD ISE'])

    features = dataset.copy()
    scaled_features = feature_scaler.fit_transform(np.reshape(dataset.values, (dataset.shape[0], dataset.shape[1])))
    scaled_features = pd.DataFrame(data=scaled_features, columns=columns)
    del features['USD ISE']
    del features['TL ISE']

    superv_stock = timeseries_to_supervised(scaled_stock, n_in, n_out)
    superv_features = timeseries_to_supervised(scaled_features, n_in, n_out)

    # Dataset  transformed to supervised problem of series (samples) 30 composed of
    # 1 days is of size [samples = the rows of any of our dataframe, 1 (for each day) ,gitr
    # features = the columns of the features dataframe]:
    samples = superv_stock.shape[0]
    features = superv_features.shape[1]
    steps = 1
    return superv_stock, superv_features, [samples, steps, features], index_scaler, feature_scaler

file_name = 'data_akbilgic.xlsx'
data = pd.read_excel(file_name, header=None, skiprows=2)
data.dropna(inplace=True)
data.columns = ['date', 'TL ISE', 'USD ISE', 'SP', 'DAX', 'FTSE', 'NIKEEI', 'BOVESPA', 'EU', 'EM']
#preprocess(data, 1, 1, True)


import numpy as np
import cv2
import matplotlib.pyplot as plt
from mylibrary import count_pixels

def get_masked_img(img_gray):
    """
    Purpose:
        measure the weighted difference between the center point and its neighbors.
    Input:
        img_gray: matrix, value type can be real.
    Output:
        res_img: matrix, value type can be real.
    """
    mask = np.array([[-1,-1,-1],
                     [-1,8,-1],
                     [-1,-1,-1]])
    pad_img = np.pad(img_gray, ((1,1),(1,1)), 'constant')
    res_img = np.zeros(img_gray.shape)
    for i in range(1, pad_img.shape[0] - 1):
        for j in range(1, pad_img.shape[1] - 1):
            res_img[i-1,j-1] = np.sum(pad_img[i-1:i+2, j-1:j+2] * mask)
    return res_img

def detect_point(img_gray, T, white_only=False):
    """
    Purpose:
        Detect points in images
    Input:
        img_gray: matrix with value type real
        T: threshold, used to record pixels which value greater T.
        white_only: boolean, if False, detect black and white points. Otherwise, only detect white.
    Output:
        res_img: matrix with value type np.uint8. use to record points.
        stat: a dictionary, key is pixel value, value is number of given pixel value.
    """
    masked_img = get_masked_img(img_gray)
    if white_only == False:
        masked_img = np.abs(masked_img)

    stat = count_pixels(masked_img, include_border=False)
    res_img = np.zeros(img_gray.shape).astype(np.uint8)
    #ignore border
    for i in range(1, masked_img.shape[0] - 1):
        for j in range(1, masked_img.shape[1] - 1):
            if masked_img[i,j] > T:
                res_img[i,j] = 1
    return res_img, stat

def mark_img(img, binary_img):
    res = img.copy()
    for i in range(binary_img.shape[0]):
        for j in range(binary_img.shape[1]):
            if binary_img[i,j] == 1:
                res[i,j] = np.array([0,0,255]).astype(np.uint8)
    return res

if __name__ == "__main__":
    ## detect both black and white points
    img = cv2.imread("../task2a_img/point.jpg")
    gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    binary, stat = detect_point(gray_img, 110)
    res_img = mark_img(img, binary)
    key_val_arr = np.asarray([[key, val] for key,val in stat.items()])
    check_all = key_val_arr.T
    plt.bar(check_all[0],check_all[1],align='center') # A bar chart
    plt.title('task2_a all pixel (black white)')
    plt.xlabel('pixels value')
    plt.ylabel('numbers')
    plt.savefig("../task2a_img/task2a_hist_all_black_white")
    plt.close()

    check_greater = np.asarray([x for x in key_val_arr if x[0] > 100]).T
    plt.bar(check_greater[0],check_greater[1],align='center') # A bar chart
    plt.title('task2_a pixel val > 100 (black white)')
    plt.xlabel('pixels value')
    plt.ylabel('numbers')
    plt.savefig("../task2a_img/task2a_hist_100_black_white")
    plt.close()

    cv2.imwrite('../task2a_img/res_point.jpg', res_img)

    ## detect only white points
    gray_img = cv2.imread("../task2a_img/point.jpg", 0)
    binary, stat = detect_point(gray_img, 110, white_only=True)
    res_img = mark_img(img, binary)
    key_val_arr = np.asarray([[key, val] for key,val in stat.items()])
    check_all = key_val_arr.T
    plt.bar(check_all[0],check_all[1],align='center') # A bar chart
    plt.title('task2_a all pixel (white)')
    plt.xlabel('pixels value')
    plt.ylabel('numbers')
    plt.savefig("../task2a_img/task2a_hist_all_white")
    plt.close()

    check_greater = np.asarray([x for x in key_val_arr if x[0] > 100]).T
    plt.bar(check_greater[0],check_greater[1],align='center') # A bar chart
    plt.title('task2_a pixel val > 100 (white)')
    plt.xlabel('pixels value')
    plt.ylabel('numbers')
    plt.savefig("../task2a_img/task2a_hist_100_white")
    plt.close()

    cv2.imwrite('../task2a_img/res_point_white.jpg', res_img)


__author__ = 'Aaron'
import os
from PAD_Monster import PADMonster

class PADTeam:

    def __init__(self, team):
        """
        Initializes the PADTeam Class.
        :param team: an array containing 6 PADMonster Classes
        """
        # self.team = [PADMonster() for monster in range(6)] -> how the team should look
        self.team = team
        # below we initialize the variables that will be containing the team stats.
        self.hp = 0
        # for all atk arrays: [fire atk, water atk, wood atk, light atk, dark atk]
        self.atk = [0, 0, 0, 0, 0]
        # for all base atks, it's the sum of each value in the array
        self.base_atk = 0
        self.pronged_atk = [0, 0, 0, 0, 0]
        self.base_pronged_atk = 0
        self.rcv = 0

        # below we initialize the modified stats, the team's total stats after being
        # multiplied by the effects of the two leader skills
        self.hp_modified = 0
        self.atk_modified = [0, 0, 0, 0, 0]
        self.base_atk_modified = 0
        self.pronged_atk_modified = [0, 0, 0, 0, 0]
        self.base_pronged_atk_modified = 0
        self.rcv_modified = 0

        # a string that will contain all our the teams' awakenings
        self.awakenings = ''

        # the leader skills effects: [hp multiplied by, atk multiplied by, rcv multiplied by]
        self.leader1_effects = [1, 1, 1]
        self.leader2_effects = [1, 1, 1]
        # store how each monster's stats will be modified as in if the monster satisfies the
        # leader skill's conditions
        self.stats_modified_by = [[1, 1, 1] for monster in range(6)]

        # set all the variables according to the team input
        self.__set__team__hp()
        self.__set__team__rcv()
        self.__set__team__atk()
        self.__set__team__base__atk()
        self.__set__team__awakenings()
        self.__set__modified__stats__()

    def __set__team__hp(self):
        self.hp = 0
        for monster in range(6):
            self.hp += self.team[monster].hp
    def __set__team__rcv(self):
        self.rcv = 0
        for monster in range(6):
            self.rcv += self.team[monster].rcv
    def __set__team__awakenings(self):
        self.awakenings = ''
        for awakening in range(len(self.team[0].awakenings)):
            # count stores how many instances of a specific awakening are contained in the team
            count = 0
            for monster in range(6):
                if self.team[monster].awakenings[awakening][2] > 0:
                    count += self.team[monster].awakenings[awakening][2]
            if count > 0:
                # if the team has an awakening, save it to the string and add the count number
                self.awakenings += self.team[0].awakenings[awakening][0]+': '+str(count)+'\n'
    def __set__team__atk(self):
        self.atk = [0, 0, 0, 0, 0]
        self.pronged_atk = [0, 0, 0, 0, 0]
        for attr in range(5):
            for monster in self.team:
                self.atk[attr] += monster.atk[attr]
                self.pronged_atk[attr] += monster.pronged_atk[attr]
    def __set__team__base__atk(self):
        self.base_atk = 0
        self.base_pronged_atk = 0
        for monster in self.team:
            self.base_atk += monster.atk[monster.attr_main]
            self.base_pronged_atk += monster.pronged_atk[monster.attr_main]
    def __set__modified__stats__(self):

        self.stats_modified_by = [[1, 1, 1] for monster in range(6)]

        # the first and last team members of the team are considered the leaders and we use
        # their respective leader skills.
        for index in [0, 5]:
            # if the leader skill isn't ""
            if self.team[index].leader_skill_name:
                # the skill effect will look [hp modified by, atk modified by, rcv modified by]
                # an additional 4th index exists if there's a conditional which will look like:
                # [hp * by, atk * by, rcv * by, ['elem' or 'type', # associated with elem or type]]
                if len(self.team[index].leader_skill_effect) > 3:
                    # if fourth array exists, save whether the conditional asks for an element
                    # or type in attribute variable
                    # and save the # associated in the num variable
                    attribute = self.team[index].leader_skill_effect[3][0]
                    num = self.team[index].leader_skill_effect[3][1]

                    # check if each monster in the team satisfies the elem or type conditional
                    # if true, the stats modified index for that monster will be multiplied appropriately
                    if attribute == "elem":
                        for monster in range(6):
                            if self.team[monster].attr_main == num or self.team[monster].attr_sub == num:
                                self.stats_modified_by[monster][0] *= self.team[index].leader_skill_effect[0]
                                self.stats_modified_by[monster][1] *= self.team[index].leader_skill_effect[1]
                                self.stats_modified_by[monster][2] *= self.team[index].leader_skill_effect[2]

                    elif attribute == "type":
                        for monster in range(6):
                            if self.team[monster].type_main == num or self.team[monster].type_sub == num:
                                self.stats_modified_by[monster][0] *= self.team[index].leader_skill_effect[0]
                                self.stats_modified_by[monster][1] *= self.team[index].leader_skill_effect[1]
                                self.stats_modified_by[monster][2] *= self.team[index].leader_skill_effect[2]

                # if there isn't a 4th index conditional, just multiply all of the stats modified indexes
                # by the appropriate skill effect amounts
                else:
                    for monster in range(6):
                        self.stats_modified_by[monster][0] *= self.team[index].leader_skill_effect[0]
                        self.stats_modified_by[monster][1] *= self.team[index].leader_skill_effect[1]
                        self.stats_modified_by[monster][2] *= self.team[index].leader_skill_effect[2]

        hp = 0
        base_atk = 0
        atk = [0, 0, 0, 0, 0]
        base_pronged_attack = 0
        pronged_atk = [0, 0, 0, 0, 0]
        rcv = 0

        # modify each team stat according to the leader skills' effects and save them to their respective
        # variables.
        for monster in range(6):
            hp += self.team[monster].hp * self.stats_modified_by[monster][0]
            rcv += self.team[monster].rcv * self.stats_modified_by[monster][2]
            main_attr = self.team[monster].attr_main
            base_atk += self.team[monster].atk[main_attr] * self.stats_modified_by[monster][1]
            base_pronged_attack += self.team[monster].pronged_atk[main_attr] * self.stats_modified_by[monster][1]
            for attr in range(5):
                atk[attr] += self.team[monster].atk[attr] * self.stats_modified_by[monster][1]
                pronged_atk[attr] += self.team[monster].pronged_atk[attr] * self.stats_modified_by[monster][1]
        self.hp_modified = hp
        self.atk_modified = atk
        self.base_atk_modified = base_atk
        self.pronged_atk_modified = pronged_atk
        self.base_pronged_atk_modified = base_pronged_attack
        self.rcv_modified = rcv


def pesquisaBinaria(lista, item):
    alto = len(lista) - 1
    baixo = 0

    while baixo <= alto:
        meio = (baixo + alto) // 2
        chute = lista[meio]

        if chute == item:
            return chute
        if chute > item:
            alto = meio - 1
        else:
            baixo = meio + 1
    return None

minha_lista = [1, 2, 5, 7, 9]
print(pesquisaBinaria(minha_lista, 7))


#coding= utf-8

import re
import datetime
import sys
#sys.path.append('..')
import  requests
from lxml import etree
from scrapy_redis.spiders import RedisSpider
from sinaSpider2.weiboID import weiboID

from scrapy.selector import Selector
from scrapy.http import Request
sys.path.append('..')
from sinaSpider2.items import  TweetsItem
#import ssl
#ssl._create_default_https_context = ssl._create_unverified_context
class Spider(RedisSpider):

	name = 'tweetsSpider'
	host = 'http://weibo.cn'
	#start_urls = [
        #5235640836, 5676304901, 5871897095, 2139359753, 5579672076, 2517436943, 5778999829, 5780802073, 2159807003,
        #1756807885, 3378940452, 5762793904, 1885080105, 5778836010, 5722737202, 3105589817, 5882481217, 5831264835,
        #2717354573, 3637185102, 1934363217, 5336500817, 1431308884, 5818747476, 5073111647, 5398825573, 2501511785,
	#]
	redis_key = 'tweetsSpider:start_urls'
	start_urls = []
	for ID in weiboID:
		url =  "http://weibo.cn/%s/profile?filter=1&page=1" % ID
		start_urls.append(url)

	def start_requests(self):
		for url in self.start_urls:
			yield Request(url=url,callback=self.parse)

	def parse(self,response):
		'''抓取微博数据'''
		selector = Selector(response)
		ID = re.findall('weibo\.cn/(\d+)',response.url)[0]
		tweets = selector.xpath('body/div[@class="c" and @id]')
		print('tweets的数据是：')
		print(tweets)
		items =[]
		for tweet in tweets:
			tweetsItems = TweetsItem()
			id = tweet.xpath('@id').extract_first()  # 微博ID
			content = tweet.xpath('div/span[@class="ctt"]/text()').extract_first()  # 微博内容
			print('微博内容为：',content)
			#cooridinates = tweet.xpath('div/a/@href').extract_first()  # 定位坐标
			like = re.findall('赞\[(\d+)\]', tweet.extract())  # 点赞数
			transfer = re.findall('转发\[(\d+)\]', tweet.extract())  # 转载数
			comment = re.findall('评论\[(\d+)\]', tweet.extract())  # 评论数
			others = tweet.xpath('div/span[@class="ct"]/text()').extract_first()  # 求时间和使用工具（手机或平台）
			tweetsItems["ID"] = ID
			tweetsItems["_id"] = ID  + "-" + id
			if content:
				tweetsItems["Content"] = content.strip("[位置]")  # 去掉最后的"[位置]"
			#if cooridinates:
			#	cooridinates = re.findall('center=([\d|.|,]+)', cooridinates)
			#	if cooridinates:
			#		tweetsItems["Co_oridinates"] = cooridinates[0]
			if like:
				tweetsItems["Like"] = int(like[0])
			if transfer:
				tweetsItems["Transfer"] = int(transfer[0])
			if comment:
				tweetsItems["Comment"] = int(comment[0])
			if others:
				others = others.split("来自")
				tweetsItems["PubTime"] = others[0]
				if len(others) == 2:
					tweetsItems["Tools"] = others[1]
			print('tweetsitems的数据是：')
			print(tweetsItems)
			print(isinstance(tweetsItems,TweetsItem))
			yield tweetsItems
#			items.append(tweetsItems)
#		return items

			url_next = selector.xpath('//div[@class="pa" and @id="pagelist"]/form/div/a[text()="下页"]/@href').extract()
			if url_next:
				yield Request(url=self.host + url_next[0], callback=self.parse)
			else:
				urlFollows = "http://weibo.cn/%s/follow" % ID
				idFollows = self.getNextID(urlFollows,response.request.cookies)
				for ID in idFollows:
					url = "http://weibo.cn/%s/profile?filter=1&page=1" % ID
					yield Request(url=url,callback=self.parse)

	def getNextID(self,url,cookies):
			IDs = []
			r = requests.get(url=url,cookies = cookies)
			if r.status_code == 200:
				selector = etree.HTML(r.content)
				texts = selector.xpath('body/table/tr/td/a[text()="关注他"or text()="关注她"]/@href')
				IDs = re.findall('uid=(\d+)',';'.join(texts),re.S)
			return IDs


import random
import enum

MENU_TEXT = 'Type "play" to play the game, "score" to show the scoreboard, and "exit" to quit:'

GUESS_TEXT = 'Guess the word '
WIN_FAIL_TEXT = {True: 'You guessed the word {}!\nYou survived!',
                 False: 'You lost!'
                 }

SCOREBOARD_TEXT = 'You won: {:d} times.\nYou lost: {:d} times.'

END_TEXT = 'Thanks for playing!'
WRONG_LETTER_TEXT = "That letter doesn't appear in the word."
NON_LETTER_TEXT = 'Please, enter a lowercase letter from the English alphabet.'
WRONG_NUM_LETTERS = 'Please, input a single letter.'
NO_IMPROVEMENTS_TEXT = 'No improvements.'
ALREADY_TRIED_TEXT = "You've already guessed this letter"
BYE_TEXT = 'Bye!'
ASK_LETTER_TEXT = 'Input a letter:'

POSSIBLE_GOAL_WORDS = ('python', 'java', 'swift', 'javascript', 'pascal', 'golang', 'rust', 'haskell', 'basic',
                       'fortran', 'ada', 'cobol', 'php', 'erlang', 'forth', 'prolog', 'lisp')

NUM_ATTEMPTS = 8

class GameState(enum.Enum):
    idle = 0
    in_menu = 1
    playing = 2
    quit = 4

def find_all(a_str, sub):
    start = 0
    while True:
        start = a_str.find(sub, start)
        if start == -1:
            return
        yield start
        start += 1

class Hangman:
    def __init__(self, game_id):
        self.id = game_id
        self.state = GameState.idle
        self.num_of_wins = 0
        self.num_of_fails = 0
        self.reset()
        self.goal_word = random.choice(POSSIBLE_GOAL_WORDS)
        self.num_of_attempts = NUM_ATTEMPTS
        self.guess_word = '-' * len(self.goal_word)
        self.guess_history = ''
        self.win = False

    def reset(self):
        self.goal_word = random.choice(POSSIBLE_GOAL_WORDS)
        self.num_of_attempts = NUM_ATTEMPTS
        self.guess_word = '-' * len(self.goal_word)
        self.guess_history = ''
        self.win = False

    def try_letter(self, letter):
        success = 0
        if not len(letter) == 1:
            result = WRONG_NUM_LETTERS
        elif letter < 'a' or letter > 'z':
            result = NON_LETTER_TEXT
        elif letter in self.guess_history:
            result = ALREADY_TRIED_TEXT
        else:
            self.guess_history += letter
            if letter in self.goal_word:
                self.guess_word = list(self.guess_word)
                indxs = list(find_all(self.goal_word, letter))
                for indx in indxs:
                    self.guess_word[indx] = letter
                self.guess_word = ''.join(self.guess_word)
                success = 1
                result = self.guess_word + '\n' + ASK_LETTER_TEXT
            else:
                result = WRONG_LETTER_TEXT
                success = -1
        return success, result

    def proceed(self, user_input):
        answer = ''
        if self.state == GameState.idle:
            self.state = GameState.in_menu
            answer = MENU_TEXT
        elif self.state == GameState.in_menu:
            if user_input == 'play':
                self.state = GameState.playing
                answer = self.guess_word + '\n' + ASK_LETTER_TEXT
            elif user_input == 'score':
                self.state = GameState.in_menu
                answer = SCOREBOARD_TEXT.format(self.num_of_wins, self.num_of_fails)
            elif user_input == 'exit':
                self.state = GameState.quit
                answer = BYE_TEXT
            else:
                self.state = GameState.in_menu
                answer = MENU_TEXT
        elif self.state == GameState.quit:
            pass
        elif self.state == GameState.playing:
            if self.num_of_attempts > 1:
                result, answer = self.try_letter(user_input)
                if result < 0:
                    self.num_of_attempts -= 1
                else:
                    if self.goal_word == self.guess_word:
                        self.win = True
                        self.state = GameState.in_menu
                        answer = WIN_FAIL_TEXT[self.win].format(self.goal_word)
                        answer += '\n' + MENU_TEXT
                        self.num_of_wins += 1
                        self.reset()
            else:
                self.win = False
                self.state = GameState.in_menu
                answer = WIN_FAIL_TEXT[self.win]
                answer += '\n' + MENU_TEXT
                self.num_of_fails += 1
                self.reset()
        return answer


#######################
## 1. Importing modules
#######################

from datetime import datetime, timedelta
from airflow import DAG
from airflow.operators.bash import BashOperator

#######################
## 2. Default arguments
#######################

default_args = {
    'owner': 'jdpinedaj',
    'retries': 5,
    'retry_delay': timedelta(minutes=5)
}

#######################
## 3. Instantiate a DAG
#######################

dag = DAG(
    dag_id='dag_with_cron_expression_v03',
    default_args=default_args,
    start_date=datetime(2021, 1, 1),
    schedule_interval='23 0-20/2 * * *',
)

#######################
## 4. Tasks
#######################

t1 = BashOperator(
    task_id='first_task',
    bash_command='echo dag with cron expression!',
    dag=dag,
)

#######################
## 5. Setting up dependencies
#######################

# t1 >> [t2, t3]


#!/usr/bin/env python
# -*- coding: utf-8 -*-
from collections import defaultdict

def read_input():
    f = open("input")
    return [int(l.strip()) for l in f]

def process(inp, preamble=25):
    previous = []
    all_seen = defaultdict(int)
    for n in range(len(inp)):
        sums = []
        if n >= preamble:
            if inp[n] not in all_seen:
                return inp[n]
            old = previous.pop(0)

            for (_, psums) in previous:
                osum = psums.pop(0)
                all_seen[osum] -= 1
                if all_seen[osum] == 0:
                    del all_seen[osum]

        for (v, _) in previous:
            sums.append(v + inp[n])
            all_seen[v + inp[n]] += 1
        previous.append((inp[n], sums))

    return previous

def main():
    inp = read_input()
    print(process(inp))

if __name__ == '__main__':
    main()


#coding:utf-8
from annoying.decorators import render_to
from django.template import loader, RequestContext
from webmoney.models import Invoice
from zpayment.forms import PaymentRequestForm, get_zp_sign

@render_to('zp_sample/simple_payment.html')
def simple_payment(request):
    response = {}
    invoice = Invoice.objects.create(user=request.user)
    payment_no = invoice.payment_no
    description = loader.render_to_string('zp_sample/simple_payment_desc.txt',
        context_instance=RequestContext(request)
    )[:255]
    amount = '101.42'
    zp_sign = get_zp_sign(payment_no, amount)
    initial = {
        'LMI_PAYMENT_NO': invoice.payment_no,
        'LMI_PAYMENT_DESC': description,
        'LMI_PAYMENT_AMOUNT': amount,
        'CLIENT_MAIL': 'testmail@example.com',
        'ZP_SIGN': zp_sign
    }
    response['form'] = PaymentRequestForm(initial=initial)

    return response


from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from taggedwiki.models import *
from django.template import RequestContext
from django import forms
from django import template
from cgi import escape
from random import randint
import re
import taggedwiki.decorators as decorators

def viewListAllSpaces(request):
	return render_to_response('allSpaces.html',{'spaces':Space.objects.all(),},context_instance=RequestContext(request))

@decorators.loadSpaceOr404
def viewListSpace(request,space):
	return render_to_response('space.html',{'space':space,},context_instance=RequestContext(request))

@decorators.loadSpaceOr404
def viewListPagesWithTagAjax(request, space, tagname):
	try:
		tag = Tag.objects.get(Title=tagname)
	except Tag.DoesNotExist:
		raise Http404()
	pages = Page.objects.filter(Space=space,Tags=tag)
	return render_to_response('pageList.html',{'pages':pages,},context_instance=RequestContext(request))

@decorators.loadSpaceOr404
@decorators.loadPageOr404
def viewPage(request,space,page):
	outPages = []
	tags = Tag.objects.filter(page__Space=space).distinct()
	# get body, escape for html
	body = " "+escape(page.Body).replace("\n","
")+" " # extra spaces are so tags at start and end are matched
	# pass 1: for every tag we find in our body, store the first position it occurs (if it does) and add to outPages, counting links as we go
	for tag in tags:
		tag.LocationInBody = -1
		regex = escape(tag.Title).replace('\\','\\\\').replace('.','\\.').replace('^','\\^').replace('$','\\$').replace('*','\\*').replace('+','\\+').replace('{','\\{').replace('[','\\[').replace(']','\\]').replace('|','\\|').replace('(','\\(').replace(')','\\)') # escaping all special chars in a regular expression
		regexObj = re.compile('\W'+regex+'\W' , re.IGNORECASE)
		regexMatch = regexObj.search( body )
		if regexMatch:
			tag.CountInBody = len ( regexObj.findall(body) ) # TODO: This will undercount if tags are right next to each other with only one character seperating them.
			for outPage in Page.objects.filter(Space=space, Tags=tag):
				if not outPage == page:
					if not outPage in outPages:
						outPage.NumberOfLinks = tag.CountInBody
						outPages.append(outPage)
					else:
						outPageIndex = outPages.index(outPage)
						outPages[outPageIndex].NumberOfLinks = outPages[outPageIndex].NumberOfLinks + tag.CountInBody
			tag.LocationInBody = regexMatch.start()+1
	# pass 2: Now put in HTML for each tag
	tags = sorted([t for t in tags if t.LocationInBody > -1], key=lambda obj: obj.LocationInBody)
	offset = 0
	for tag in tags:
		html = ''
		body = body[0:tag.LocationInBody+offset]+html+body[tag.LocationInBody+offset:]
		offset = offset + len(html)
	# finally sort results before returning
	outPages = sorted([p for p in outPages], key=lambda obj: obj.NumberOfLinks, reverse=True)
	return render_to_response('viewPage.html',{'space':space,'page':page,'outPages':outPages,'body':body,'tags':tags},context_instance=RequestContext(request))

class EditPageForm(forms.Form):
	Title = forms.CharField(required=True)
	Body = forms.CharField(widget=forms.Textarea,required=True)
	AddANewTag = forms.CharField(widget=forms.Textarea,required=False)

@decorators.loadSpaceOr404
@decorators.loadPageOr404
def viewEditPage(request,space,page):
	if request.method == 'POST':
		form = EditPageForm(request.POST)
		if form.is_valid():
			cd = form.cleaned_data
			page.Title = cd['Title'].strip()
			page.Body = cd['Body']
			page.LastUpdateIP = request.META['REMOTE_ADDR']
			page.save()
			for newtag in cd['AddANewTag'].split("\n"):
				if newtag.strip():
					page.addTag(newtag.strip())
			for removetag in request.POST.getlist('Remove'):
				tag = Tag.objects.get(id=removetag)
				if tag in page.Tags.all():
					page.removeTag(tag)
			return HttpResponseRedirect(page.get_absolute_url())
	else:
		form = EditPageForm(  {  'Title':page.Title, 'Body':page.Body, }  )
	return render_to_response('editPage.html', {'space':space,'page':page,'form': form,},context_instance=RequestContext(request))

@decorators.loadSpaceOr404
def viewNewPage(request,space):
	if request.method == 'POST':
		form = EditPageForm(request.POST)
		if form.is_valid():
			cd = form.cleaned_data
			# todo: check name doesn't already exist!
			page = Page(Space=space, Title=cd['Title'], Body=cd['Body'], LastUpdateIP = request.META['REMOTE_ADDR'])
			page.save()
			for newtag in cd['AddANewTag'].split("\n"):
				if newtag.strip():
					page.addTag(newtag.strip())
			return HttpResponseRedirect(page.get_absolute_url())
	else:
		form = EditPageForm( )
	return render_to_response('editPage.html', {'space':space,'form': form,},context_instance=RequestContext(request))


import pandas as pd
from sheets.sheet import AbstractSheetAdapter

from telegram import ReplyKeyboardMarkup

from sheets.i18n import I18n
from sheets.settings import Settings

class KeyboardAdapterClass(AbstractSheetAdapter):
    def __init__(self) -> None:
        super().__init__('keyboard', 'keyboard', initialize_as_df=True)

    async def _pre_async_init(self):
        self.sheet_name = I18n.keyboard
        self.REGISTER_FUNCTION = I18n.register
        self.update_sleep_time = Settings.keyboard_update_time
        self.retry_sleep_time  = self.update_sleep_time // 2

    async def _get_df(self) -> pd.DataFrame:
        df = pd.DataFrame(await self.wks.get_all_records())
        df = df.drop(index = 0, axis = 0)
        df = df.loc[
            (df.key != "") &
            (df.is_active == I18n.yes)
        ]
        return df

    async def _process_df_update(self):
        self.keys = self.as_df.key.values.tolist()
        self.reply_keyboard = ReplyKeyboardMarkup([
            self.keys[idx:idx+2]
            for idx in range(0,len(self.keys),2)
        ] if len(self.keys) > 2 else [[x] for x in self.keys])
        self.registration_keyboard_row = self._get(self.as_df.function == self.REGISTER_FUNCTION)

    def get(self, key: str) -> pd.Series:
        return self._get(self.as_df.key == key)

Keyboard = KeyboardAdapterClass()


# A dictionary is a collection which is unordered, changable, and indexed. No duplicate members

# create a dictionary

person = {
    'first_name': 'Zachary',
    'last_name': 'Anstey',
    'age' : 25
}

# use a constructor
person2 = dict(first_name='John',last_name='doe')

# print(person2,type(person))
# print(person, type(person))

# Get value
# print(person['first_name'])
# print(person.get('last_name'))

# Add key/value

person['phone'] = '555-555-55555'
# print(person)

# Get dict keys
# print(person.keys())

# get dict items
# print(person.items())

# copy dirct
person2 = person.copy()
person2['city'] = 'Boston'
# print(person2)

# remove item
del(person['age'])
person.pop('phone')
# print(person)

# Clear
person.clear()
# print(person)

# Get length
print(len(person2))

# List of dictionaries
people = [
    {'name':'Martha','age':30},
    {'name': 'Kevin','age':25}
]
print(people[1])


import vk
import json

class api:
	def __init__(self):
		self.vk = vk.vk()
		self.v = vk.init.d['v']

	def getResult(self):
		res = json.loads(self.vk.response)
		if self.vk.status==200:
			r = res['response']
		else:
			r = res['error']
		return self.vk.status, r

	def getUserInfo(self, idUser):
		self.vk.checkToken()
		url = 'https://api.vk.com/method/users.get?user_id=%s&v=%s' % (idUser, self.v)
		self.vk.send(url)
		return self.getResult()


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:

        res = []

        def dfs(root):
            if not root:
                return

            res.append(root.val)
            dfs(root.left)
            dfs(root.right)
            return

        dfs(root)
        print(res)
        sortedL = sorted(res)
        print(sortedL)
        return sortedL[k-1]


import os
from glob import glob
from PIL import Image   # PIL는 이미지를 load 할 때 이용
import time
from IPython.display import clear_output

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

absolute_path = 'D:/Python/★★Python_POSTECH_AI/Postech_AI 9) Team Project/Image/'
os.listdir(absolute_path)

image_path = absolute_path + 'Train/apple/'
os.listdir(image_path)

image_path2 = absolute_path + 'Train/orange/'
os.listdir(image_path2)

# Image Load
imag1 = plt.imread(image_path + '0_100.jpg')
imag2_jpg = Image.open(image_path + '0_100.jpg')
imag2 = np.array(imag2_jpg)

# Image show
plt.imshow(imag1)

imag2_jpg
plt.imshow(imag2_jpg)
plt.imshow(imag2)

# 흑백으로 열기
imag2_kb_jpg = Image.open(image_path + '0_100.jpg').convert('L')
imag2_kb_jpg

imag2_kb = np.array(imag2_kb_jpg)
imag2_kb.shape
plt.imshow(imag2_kb_jpg, 'gray')

plt.imshow(imag2_kb_jpg, 'RdBu')    # 색깔지정하기
plt.imshow(imag2_kb_jpg, 'jet')    # gray반전
plt.colorbar()

# 두번째 이미지 열기
imag3_jpg = Image.open(image_path2 + '0_100.jpg')
imag3_jpg
imag3 = np.array(imag3_jpg)
imag3

# Image Resize
import cv2
imag3_shrink = cv2.resize(imag3, (50,50))
imag3_expand = cv2.resize(imag3, (200,200))
imag3_resize = cv2.resize(imag3, (100,50))
print(imag3_shrink.shape, imag3_expand.shape)

plt.imshow(imag3_shrink)
plt.imshow(imag3_expand)
plt.imshow(imag3_resize)

# Image Agmentation ---------------------------------------
from tensorflow.keras.preprocessing.image import ImageDataGenerator
Image_transform_comparison(inputs, image_gen)

# (주로 사용하는 기능들)
# - width_shift_range       # 가로방향 shift
# - height_shift_range      # 세로방향 shift
# - brightness_range
# - zoom_range              # 상하좌우로 늘림
# - horizontal_flip         # 세로방향 반전
# - vertical_flip           # 가로방향 반전
# - rescale                 #
# - preprocessing_function  # (Customizing) 어떤함수를 넣어서 적용가능

# - width_shift_range   : 가로방향 shift
gen2 = ImageDataGenerator(width_shift_range=0.3)
image_gen2 = next(iter(gen2.flow(inputs)))
Image_transform_comparison(inputs, image_gen2)

# - height_shift_range : 세로방향 shift
gen3 = ImageDataGenerator(height_shift_range=0.3)
image_gen3 = next(iter(gen3.flow(inputs)))
Image_transform_comparison(inputs, image_gen3)

# - brightness_range
gen4 = ImageDataGenerator(brightness_range=[1,2])
image_gen4 = next(iter(gen4.flow(inputs)))
Image_transform_comparison(inputs, image_gen4)

# - zoom_range  : 위아래 좌우 늘림
gen5 = ImageDataGenerator(zoom_range=0.3)
image_gen5 = next(iter(gen5.flow(inputs)))
Image_transform_comparison(inputs, image_gen5)

# - zoom_range
gen6 = ImageDataGenerator(rescale=1/255)
image_gen6 = next(iter(gen6.flow(inputs)))
Image_transform_comparison(inputs, image_gen6)

# Rescale시 주의 사항: Train_set에 해주면 Test_set에도 반드시 해주어야함
Train_data_gen = ImageDataGenerator(zoom_ragne=0.7, rescale=1/255)
Test_data_gen = ImageDataGenerator(rescale=1/255)


import random
import itertools
import numpy # Should be imported after random and itertools

def oneHot(size, key):
    return numpy.array([i == key for i in range(size)])

def DataGetter(trainSize, validateSize):
    ''' Get a random sample from train.csv each time
        @param trainSize, validateSize: batch size for training set and validation set correspondingly.
                                        `trainSize` means sampled size and `validateSize` means valid size
        @return : (training getter, validating getter) '''

    f = open('../data/train.csv')
    next(f) # The first line is header
    lines = map(lambda row: row.strip().split(','), f)
    dataset = list(map(lambda row: (numpy.array(tuple(map(float, row[1:]))), oneHot(10, int(row[0]))), lines))
    del f
    trainset = dataset[validateSize:]
    validateset = dataset[:validateSize]
    random.shuffle(trainset)

    def subGetter(dataset, batchSize): # batchSize = None means whole set
        if batchSize is None:
            batchSize = len(dataset)
        assert batchSize <= len(dataset)
        pos = 0
        while True:
            _pos = (pos + batchSize) % len(dataset)
            if pos < _pos:
                batch = itertools.islice(dataset, pos, _pos)
            elif pos > _pos:
                batch = itertools.chain(itertools.islice(dataset, pos, len(dataset)), itertools.islice(dataset, 0, _pos))
            else:
                batch = dataset
            pos = _pos

            yield list(map(numpy.array, zip(*batch)))

    return subGetter(trainset, trainSize), subGetter(validateset, None)

def TestGetter():
    f = open('../data/test.csv')
    next(f) # The first line is header
    lines = map(lambda row: row.strip().split(','), f)
    dataset = list(map(lambda row: numpy.array(tuple(map(float, row))), lines))
    del f

    for batch in [dataset[i * 500 : (i + 1) * 500] for i in range(len(dataset) // 500)]:
        yield numpy.array(batch)


import sys

sys.path.append("")
sys.path.append("../../..")

import streamlit as st
import pandas as pd
from sympy import lambdify, diff
import numpy as np
from plotly.subplots import make_subplots

from config import COLORS
from common.calculation.derivative.derivative_calculator import FirstDerivativeCalculator, SecondDerivativeCalculator
from common.models.line_segment import LineSegment
from common.models.point_generation import EquidistantPointGenerator
from tasks.utils.expression_parsing import custom_parse_expr
from tasks.utils.streamlit import (
    display_x_points,
    display_title,
    get_new_key,
    set_initial_key,
    input_sympy_function,
    add_line,
    display_whitespace,
)

LINE_START = "$\quad$"

def fill_beginning_and_end(input_list, value_to_fill=None):
    return [value_to_fill] + input_list + [value_to_fill]

def display_result_table(
    points,
    function_as_lambda,
    first_derivative_as_lambda,
    second_derivative_as_lambda,
    first_derivative_values,
    second_derivatives_values,
):
    no_value_text = "No value"
    data = {
        "x": points,
        "f(x)": [function_as_lambda(point) for point in points],
        "approx f'(x)": first_derivative_values,
        "|Δf'(x)|": [
            abs(first_derivative_as_lambda(point) - first_derivative_values[i]) for i, point in enumerate(points)
        ],
        "approx f''(x)": fill_beginning_and_end(second_derivatives_values),
        "|Δf''(x)|": fill_beginning_and_end(
            [
                abs(second_derivative_as_lambda(point) - second_derivatives_values[i])
                for i, point in enumerate(points[1:-1])
            ]
        ),
    }
    df = pd.DataFrame(data=data)
    idx = pd.IndexSlice
    df_styler = (
        df.style.format(
            formatter={
                "|Δf'(x)|": "{:e}",
                "approx f''(x)": lambda x: no_value_text if np.isnan(x) else f"{x:.6f}",
                "|Δf''(x)|": lambda x: no_value_text if np.isnan(x) else f"{x:e}",
            }
        )
        .set_properties(**{"background-color": "#fff2f7"}, subset=idx[::2, :])
        .set_properties(**{"background-color": "#f2faff"}, subset=idx[1::2, :])
    )
    st.dataframe(df_styler)

def add_line_with_points(
    fig, x_points, y_points, function, line_title, points_title, line_color, point_color, row, col
):
    max_point = max(list(x_points))
    min_point = min(list(x_points))
    additional_value = (max_point - min_point) * 0.1
    points_for_function = np.linspace(min_point - additional_value, max_point + additional_value, 150)
    add_line(fig, points_for_function, function, line_title, line_color, row=row, col=col)

    fig.add_scatter(
        x=x_points,
        y=y_points,
        mode="markers",
        hovertemplate="(%{x}, %{y})",
        name=points_title,
        marker=dict(color=point_color, size=7),
        row=row,
        col=col,
    )

def display_result(points, sympy_function, first_derivative_values, second_derivatives_values):
    display_title(st, "Результат", 2)

    first_derivative = diff(sympy_function)
    second_derivative = diff(first_derivative)
    first_derivative_as_lambda = lambdify("x", first_derivative)
    second_derivative_as_lambda = lambdify("x", second_derivative)
    function_as_lambda = lambdify("x", sympy_function)

    fig = make_subplots(rows=1, cols=2, subplot_titles=("Первая производная", "Вторая производная"))
    add_line_with_points(
        fig,
        points,
        first_derivative_values,
        function=first_derivative_as_lambda,
        line_title="f'",
        points_title="approx f'",
        line_color="gray",
        point_color=COLORS["theme_color"],
        row=1,
        col=1,
    )
    add_line_with_points(
        fig,
        points[1:-1],
        second_derivatives_values,
        function=second_derivative_as_lambda,
        line_title="f''",
        points_title="approx f''",
        line_color="black",
        point_color=COLORS["dark_blue"],
        row=1,
        col=2,
    )
    fig.update_layout(margin=dict(l=0, t=50, b=0, r=0))

    display_title(st, "Графики", 4)
    st.plotly_chart(fig, use_container_width=True)

    display_title(st, "Таблица", 4)
    display_result_table(
        points,
        function_as_lambda,
        first_derivative_as_lambda,
        second_derivative_as_lambda,
        first_derivative_values,
        second_derivatives_values,
    )

def main():
    set_initial_key(st)
    first_derivative_calculator = FirstDerivativeCalculator()
    second_derivative_calculator = SecondDerivativeCalculator()

    display_title(st, "Нахождение производных таблично-заданной функции по формулам численного дифференцирования")
    display_whitespace(st)
    sympy_function = input_sympy_function(st, "exp(3*x)", key=get_new_key(st))
    function = lambdify("x", custom_parse_expr(sympy_function))

    col_1, col_2, col_3 = st.columns((4, 4, 3))
    left_value = col_1.number_input("Введите значение начальной точки", step=0.1, value=0.0)
    inter_node_length = col_2.number_input("Введите расстояние между точками", format="%e", value=5e-2)
    number_of_points = col_3.number_input("Введите количество точек", value=10)

    if inter_node_length < 0:
        col_2.error("Расстояние должно быть > 0")
    elif number_of_points < 3:
        col_3.error("Необходимо минимум 3 точки")
    else:
        line_segment = LineSegment(left_value, left_value + inter_node_length * (number_of_points - 1))
        all_points = sorted(EquidistantPointGenerator().generate(line_segment, number_of_points))
        function_values = [function(point) for point in all_points]
        display_x_points(
            st,
            x=function_values,
            y=all_points,
            key=get_new_key(st),
            names=("f(x)", "x"),
            title="Значения функции в сгенерированных точках",
        )

        first_derivative_values = first_derivative_calculator.calculate(function_values, inter_node_length)
        second_derivative_values = second_derivative_calculator.calculate(function_values, inter_node_length)
        display_result(all_points, sympy_function, first_derivative_values, second_derivative_values)

if __name__ == "__main__":
    main()


# calculates the performance of a baseline method for calorie / nutrient prediction on our dataset
#
# two baseline methods are implemented:
#
# 1. mean baseline: always predicts the value that is the average value in the dataset
# 2. random-choice baseline: chose a random recipe from the dataset and use its values as the prediction
#    Since the dataset is large enough that hitting the specific correct recipe is very unlikely, this is basically the same as predicting a random value with the mean and variance of the dataset - which has worse performance than simply always predicting the mean.
#
from pathlib import Path
import json
import numpy as np
import argparse
import random

parser = argparse.ArgumentParser()
# parser.add_argument("--runname", help="name this experiment", required=True)
parser.add_argument(
    "--datadir",
    help="input data dir generated by data/split.py (contains e.g. train.json and train/",
    required=True,
)
args = parser.parse_args()

dir = Path(args.datadir)

with open(dir / "train.json") as f:
    train = json.load(f)["data"]

def criterion_rel_error(pred, truth):
    import numpy as torch

    # https://en.wikipedia.org/wiki/Approximation_error
    ret = torch.abs(1 - pred / truth)
    ret[torch.isnan(ret)] = 0  # if truth = 0 relative error is undefined
    return torch.mean(ret)

def get_mean_baseline_model(train_values):
    print("using MEAN baseline")
    mean = np.mean(train_values)

    def model():
        return mean

    return model

def get_random_choice_baseline_model(train_values):
    print("using RANDOM CHOICE baseline")

    def model():
        return random.choice(train_values)

    return model

for key in train[0].keys():
    if key not in ["kcal", "protein", "fat", "carbohydrates", "mass_per_portion"]:
        continue
    ground_truths = [e[key] for e in train]

    mean = np.mean(ground_truths)
    std = np.std(ground_truths)
    print(f"{key} mean: {mean:.1f}")
    print(f"{key} std: {std:.1f}")

    with open(dir / "val.json") as f:
        test = json.load(f)["data"]

    baseline_model = get_random_choice_baseline_model(ground_truths)

    # baseline_model = get_mean_baseline_model(ground_truths)

    l1_train = np.mean(
        np.abs([ground_truth - baseline_model() for ground_truth in ground_truths])
    )

    ground_truths_val = [e[key] for e in test]
    l1_val = np.mean(
        np.abs([ground_truth - baseline_model() for ground_truth in ground_truths_val])
    )
    rel_val = criterion_rel_error(
        np.array([baseline_model() for ground_truth in ground_truths_val]),
        ground_truths_val,
    )
    print(f"{key} l1_train: {l1_train:.3g}")
    print(f"{key} l1_val: {l1_val:.3g}")
    print(f"{key} rel_error_val {rel_val:.3g}")


import os

from flask_restplus import Resource, Namespace

from api.service import CourseService

course_api = Namespace(
    'courses', description='API endpoints for managing courses resourse')

current_api = Namespace(
    'current', description='API endpoints for getting current session and semester')

@course_api.route('/all')
class CourseList(Resource):
    @course_api.doc('Get All Courses')
    def get(self):
        ''' Get all courses '''
        response, code = CourseService.get_all_courses()
        return response, code

@course_api.route('/<string:department>/<string:level>')
@course_api.route('/<string:department>/<string:level>/<int:minimal>')
class Course(Resource):
    @course_api.doc('Get Courses via Department & Level')
    def get(self, department, level, minimal=0):
        ''' Get a course by department and level '''
        print(minimal)
        response, code = CourseService.get_courses_by_dept_level(
            department=department, level=level, minimal=minimal)
        return response, code

@current_api.route('/semester')
class CurrentSemester(Resource):
    @current_api.doc('Get current semester')
    def get(self):
        ''' Get the current semester '''
        response = {
            'success': True,
            'data': os.environ.get('CURRENT_SEMESTER') or 'unavailable'
        }
        return response, 200

@current_api.route('/session')
class CurrentSession(Resource):
    @current_api.doc('Get current session')
    def get(self):
        ''' Get the current session '''
        response = {
            'success': True,
            'data': os.environ.get('CURRENT_SESSION') or 'unavailable'
        }
        return response, 200


file1 = open('AdventOfCode/2020/Input/Input5.txt', 'r')
lines = file1.read().splitlines()

# %% Part-1
def convertColumnToNumber(line):
    letters = list(line)
    cLower = 0
    cUpper = 8
    for letter in letters:
        if(letter == "R"):
            cLower = cLower + (cUpper-cLower)/2
        else:
            cUpper = cUpper - (cUpper-cLower)/2
    column = cLower
    return column

def convertRowToNumber(line):
    letters = list(line)
    rLower = 0
    rUpper = 128
    for letter in letters:
        if(letter == "B"):
            rLower = rLower + (rUpper-rLower)/2
        else:
            rUpper = rUpper - (rUpper-rLower)/2
    row = rLower
    return row

# %% Part-2
def findMySeat(seatIds):
    prev = seatIds[0]
    for seatId in seatIds[1:]:
        if (seatId-1 != prev) and (seatId != prev):
            return seatId - 1
        prev = seatId

# %% Main
seatIds = []
for line in lines:
    row = convertRowToNumber(line[0:7])
    column = convertColumnToNumber(line[7:10])
    seatIds.append(row * 8 + column)

print("Part-1: Maximum ID :", max(seatIds))
seatIds.sort()
print("Part-2: My seat ID is :", findMySeat(seatIds))


import datetime

from google.appengine.ext import ndb

from components import utils
import gae_ts_mon

import config
import model

# Override default target fields for app-global metrics.
GLOBAL_TARGET_FIELDS = {
  'job_name': '',  # module name
  'hostname': '',  # version
  'task_num': 0,  # instance ID
}

_TAG_FIELD_NAMES = {
  'builder',
  'user_agent'
}
_ATTR_FIELD_NAMES = {
  'bucket',
  'status',
  'result',
  'failure_reason',
  'cancelation_reason'
}
_ALL_FIELD_NAMES = _TAG_FIELD_NAMES | _ATTR_FIELD_NAMES

BUCKETER_24_HR = gae_ts_mon.GeometricBucketer(growth_factor=10 ** 0.05)
BUCKETER_48_HR = gae_ts_mon.GeometricBucketer(growth_factor=10 ** 0.053)
BUCKETER_5_SEC = gae_ts_mon.GeometricBucketer(growth_factor=10 ** 0.0374)
BUCKETER_1K = gae_ts_mon.GeometricBucketer(growth_factor=10 ** 0.031)

def _fields_for(build, field_names):
  """Returns field values for a build"""
  if not build:
    return {f: '' for f in field_names}

  tags = None
  result = {}
  for f in field_names:
    if f in _ATTR_FIELD_NAMES:
      result[f] = str(getattr(build, f) or '')
    elif f in _TAG_FIELD_NAMES:
      if tags is None:
        tags = dict(t.split(':', 1) for t in build.tags)
      result[f] = tags.get(f, '')
    else:
      raise ValueError('invalid field %r' % f)
  return result

def _fields_for_fn(fields):
  assert all(isinstance(f, gae_ts_mon.StringField) for f in fields)
  assert all(f.name in _ALL_FIELD_NAMES for f in fields)
  field_names = [f.name for f in fields]
  return lambda b: _fields_for(b, field_names)   # pragma: no cover

def _string_fields(*names):
  return [gae_ts_mon.StringField(n) for n in names]

def _incrementer(metric):
  """Returns a function that increments the metric.

  Fields must be string and one of _ALL_FIELD_NAMES.

  The returned function accepts a build.
  """
  fields_for = _fields_for_fn(metric.field_spec)
  return lambda b: metric.increment(fields_for(b))   # pragma: no cover

def _adder(metric, value_fn):
  """Returns a function that adds a build value to the distribution metric.

  Fields must be string and one of _ALL_FIELD_NAMES.
  value_fn accepts a build.

  The returned function accepts a build.
  """
  fields_for = _fields_for_fn(metric.field_spec)
  return lambda b: metric.add(value_fn(b), fields_for(b))  # pragma: no cover

inc_created_builds = _incrementer(gae_ts_mon.CounterMetric(
    'buildbucket/builds/created',
    'Build creation',
    _string_fields('bucket', 'builder', 'user_agent')))
inc_started_builds = _incrementer(gae_ts_mon.CounterMetric(
    'buildbucket/builds/started',
    'Build start',
    _string_fields('bucket', 'builder')))
inc_completed_builds = _incrementer(gae_ts_mon.CounterMetric(
    'buildbucket/builds/completed',
    'Build completion, including success, failure and cancellation',
    _string_fields(
        'bucket', 'builder', 'result', 'failure_reason', 'cancelation_reason')))
inc_heartbeat_failures = _incrementer(gae_ts_mon.CounterMetric(
    'buildbucket/builds/heartbeats',
    'Failures to extend a build lease',
    _string_fields('bucket', 'builder', 'status')))
inc_lease_expirations = _incrementer(gae_ts_mon.CounterMetric(
    'buildbucket/builds/lease_expired',
    'Build lease expirations',
    _string_fields('bucket', 'builder', 'status')))
inc_leases = _incrementer(gae_ts_mon.CounterMetric(
    'buildbucket/builds/leases',
    'Successful build leases or lease extensions',
    _string_fields('bucket', 'builder')))

_BUILD_DURATION_FIELDS = _string_fields(
    'bucket', 'builder', 'result', 'failure_reason', 'cancelation_reason')

# requires the argument to have non-None create_time and complete_time.
add_build_cycle_duration = _adder(  # pragma: no branch
    gae_ts_mon.NonCumulativeDistributionMetric(
        'buildbucket/builds/cycle_durations',
        'Duration between build creation and completion',
        _BUILD_DURATION_FIELDS,
        bucketer=BUCKETER_48_HR,
        units=gae_ts_mon.MetricsDataUnits.SECONDS),
    lambda b: (b.complete_time - b.create_time).total_seconds())

# requires the argument to have non-None start_time and complete_time.
add_build_run_duration = _adder(  # pragma: no branch
    gae_ts_mon.NonCumulativeDistributionMetric(
        'buildbucket/builds/run_durations',
        'Duration between build start and completion',
        _BUILD_DURATION_FIELDS,
        bucketer=BUCKETER_48_HR,
        units=gae_ts_mon.MetricsDataUnits.SECONDS),
    lambda b: (b.complete_time - b.start_time).total_seconds())

CURRENTLY_PENDING = gae_ts_mon.GaugeMetric(
    'buildbucket/builds/pending',
    'Number of pending builds',
    _string_fields('bucket'))
CURRENTLY_RUNNING = gae_ts_mon.GaugeMetric(
    'buildbucket/builds/running',
    'Number of running builds',
    _string_fields('bucket'))
LEASE_LATENCY_SEC = gae_ts_mon.NonCumulativeDistributionMetric(
    'buildbucket/builds/never_leased_duration',
    'Duration between a build is created and it is leased for the first time',
    _string_fields('bucket'),
    bucketer=BUCKETER_24_HR,
    units=gae_ts_mon.MetricsDataUnits.SECONDS)
SCHEDULING_LATENCY_SEC = gae_ts_mon.NonCumulativeDistributionMetric(
    'buildbucket/builds/scheduling_duration',
    'Duration of a build remaining in SCHEDULED state',
    _string_fields('bucket'),
    bucketer=BUCKETER_48_HR,
    units=gae_ts_mon.MetricsDataUnits.SECONDS)
SEQUENCE_NUMBER_GEN_DURATION_MS = gae_ts_mon.CumulativeDistributionMetric(
    'buildbucket/sequence_number/gen_duration',
    'Duration of a sequence number generation in ms',
    _string_fields('sequence'),
    # Bucketer for 1ms..5s range
    bucketer=BUCKETER_5_SEC,
    units=gae_ts_mon.MetricsDataUnits.MILLISECONDS)
TAG_INDEX_INCONSISTENT_ENTRIES = gae_ts_mon.NonCumulativeDistributionMetric(
    'buildbucket/tag_index/inconsistent_entries',
    'Number of inconsistent entries encountered during build search',
    _string_fields('tag'),
    # We can't have more than 1000 entries in a tag index.
    bucketer=BUCKETER_1K)
TAG_INDEX_SEARCH_SKIPPED_BUILDS = gae_ts_mon.NonCumulativeDistributionMetric(
    'buildbucket/tag_index/skipped_builds',
    'Number of builds we fetched, but skipped',
    _string_fields('tag'),
    # We can't have more than 1000 entries in a tag index.
    bucketer=BUCKETER_1K)

@ndb.tasklet
def set_build_status_metric(metric, bucket, status):
  q = model.Build.query(
      model.Build.bucket == bucket,
      model.Build.status == status)
  value = yield q.count_async()
  metric.set(value, {'bucket': bucket}, target_fields=GLOBAL_TARGET_FIELDS)

@ndb.tasklet
def set_build_latency(metric_sec, bucket, must_be_never_leased):
  q = model.Build.query(
      model.Build.bucket == bucket,
      model.Build.status == model.BuildStatus.SCHEDULED,
  )
  if must_be_never_leased:
    q = q.filter(model.Build.never_leased == True)
  else:
    # Reuse the index that has never_leased
    q = q.filter(model.Build.never_leased.IN((True, False)))

  now = utils.utcnow()
  dist = gae_ts_mon.Distribution(gae_ts_mon.GeometricBucketer())
  for e in q.iter(projection=[model.Build.create_time]):
    latency = (now - e.create_time).total_seconds()
    dist.add(latency)
  if dist.count == 0:
    dist.add(0)
  metric_sec.set(dist, {'bucket': bucket}, target_fields=GLOBAL_TARGET_FIELDS)

# Metrics that are per-app rather than per-instance.
GLOBAL_METRICS = [
  CURRENTLY_PENDING,
  CURRENTLY_RUNNING,
  LEASE_LATENCY_SEC,
  SCHEDULING_LATENCY_SEC,
]

def update_global_metrics():
  """Updates the metrics in GLOBAL_METRICS."""
  futures = []
  for b in config.get_buckets_async().get_result():
    futures.extend([
      set_build_status_metric(
          CURRENTLY_PENDING, b.name, model.BuildStatus.SCHEDULED),
      set_build_status_metric(
          CURRENTLY_RUNNING, b.name, model.BuildStatus.STARTED),
      set_build_latency(LEASE_LATENCY_SEC, b.name, True),
      set_build_latency(SCHEDULING_LATENCY_SEC, b.name, False),
    ])
  for f in futures:
    f.check_success()


ventas = Tk()
ventas.title("LOCAL")
ventas.geometry("1000x1000")
ventas.resizable(False,False)
ventas.config(bg="black")

def saludar(nombre):
    print("Hola", nombre)

#creamos un boton centrado en la ventana
# boton = Button(ventas, text="Enviar", width=10, height=2, bg="white", fg="black", font=("Arial", 12), command=lambda: saludar("alex"))
# boton.place(x=200, y=200)

# cajatxt = Entry(ventas, width=30, font=("Arial", 12))
# cajatxt.place(x=150, y=150)

# boton1 = Button(ventas, text="b1",width=30)
# boton2 = Button(ventas, text="b2",width=30)
# boton3 = Button(ventas, text="b3",width=30)

# boton1.grid(row=0, column=0)
# boton2.grid(row=0, column=1)
# boton3.grid(row=0, column=2)

#creamos el loop de la ventana
ventas.mainloop()


__author__ = 'Jose Gabriel'

import os
import pprint

def read_block(f):
    s = ""
    line = f.readline()
    while line and not line.startswith("."):
        s += line
        line = f.readline()
    return s, line

def read_doc(f):
    doc = {"title": "", "authors": "", "content": ""}

    line = f.readline()
    while line and not line.startswith(".I"):
        if line.startswith(".T"):
            doc["title"], line = read_block(f)
        elif line.startswith(".A"):
            doc["authors"], line = read_block(f)
        elif line.startswith(".W"):
            doc["content"], line = read_block(f)
        else:
            _, line = read_block(f)
    return doc, line

def create_doc(data, out_folder, name):
    with open(out_folder + os.sep + name, 'w') as f:
        f.write(data["title"] + "\n")
        f.write(data["content"] + "\n")
        f.write(data["authors"])

def parse_all(s, out_folder):
    with open(s) as f:
        line = f.readline()  # .I
        while line:
            doc_name = "d%03d.txt" % (int(line.strip().split()[-1]))
            doc, line = read_doc(f)
            create_doc(doc, out_folder, doc_name)
            # print("**********************************")

if __name__ == '__main__':
    s = "adi" + os.sep + "ADI.ALL"
    out_folder = "test_index"

    try:  # averiguar como preguntar si una carpeta o fichero existe en python
        os.mkdir(out_folder)
    except FileExistsError:
        pass

    parse_all(s, out_folder)


""" player sprite. no relation to sprite.py """
from kivy.graphics.context_instructions import Color
from kivy.graphics.vertex_instructions  import Rectangle
from kivy.core.image 				   import Image

from settings 						  import *  		 # constant values
from textures						   import *
from collections 					   import deque
import os

class PlayerSprite:
	def __init__(self, game, path=PLAYER_SPRITE_PATH, colour=(1,1,1,1)):
		self.game 			 = game
		self.game_mode		 = self.game.gm
		self.pos_x, self.pos_y = 0,0
		self.accum_time 	   = 0.0
		self.animate_fire 	 = False
		self.do_once		   = True

		self.texture_gen = (image for image in get_image_sequence('player'))

		with self.game.canvas:
			self.sprt_colour = Color(colour)
			self.sprt_rect = Rectangle(texture = next(self.texture_gen))

	def re_init(self):
		self.pos_x, self.pos_y = scr_width() / 2, 0
		self.sprt_rect.size = controller_size()
		self.sprt_rect.pos = (self.pos_x - controller_size()[0] / 2, self.pos_y)

	def update(self, dt):
		self.accum_time += dt

		if self.accum_time > GUN_ANIMATION_DURATION:
			self.accum_time = 0

			if self.animate_fire:
				if self.do_once:
					self.game_mode.player_attack()
					self.do_once = False

				try:
					texture = next(self.texture_gen)
					self.sprt_rect.texture = texture

				except StopIteration:
					self.do_once = True
					self.animate_fire = True if self.game.hud.left_button.state == 'down' else False

					self.texture_gen = (image for image in get_image_sequence('player'))
					self.sprt_rect.texture = next(self.texture_gen)

	def attack(self):
		self.animate_fire = True


"""
Classe     : Idbs.py
Scopo      : Questa classe aggiunge la gestione dei file indice alla classe dbs
Versione   : 1.5
Autore     : Marco Salvati
Data       : 2019-07-05
Revisione   : 2019-01-21 - 2019-04-24 - 2019-11-25
"""
import os,json
from dbase.dbs import *

__author__ = 'Marco Salvati'
__copyright__ = 'Marco Salvati, Copyright (c) 2019'
__license__ = "GPL 3"
__version__ = "1.5.0"
__email__ = "salvatimarco61@gmail.com"

class Idbs:
    def __init__(self,table,filename):
        self.__table=table
        self.__dbs=table.dbs
        self.__filename=filename
        self.__keys=[[]]
        self.__fields=[]
        self.__pk=False  # flag per chiave parziale
        self.__next=0
        self.__next_key=""
        self.__next_key2=""
        self.__pkType=-1
    def index(self,*fields):
        self.__fields=fields
        cursore=self.__dbs.tell() # salva la posizione del cursore
        cycle=range(1,len(self.__dbs))
        for i in cycle: # cicla l'intero dbs
            k=""
            self.__dbs.seek(i)
            for f in fields:
                rf=f.field
                k+="{}-".format(self.__dbs[rf])
            k=k[:-1] # elimina l'ultimo '-'
            self.__keys.append(k) # scrivi la chiave
            self.__keys[0].append(i) # scrivi il record
        self.__dbs.seek(cursore)  # ripristina il cursore

    def reindex(self):
        self._keys=[[]]
        self.__index(self.__fields)

    def __contains__(self, key): # operatore in
        self.__pk=False
        self.__next=0
        self.__next_key=""
        self.__next_key2=""
        self.__pkType=-1
        if "*" in key: # chiave parziale
            self.__pk=True
            return self.__PKsearch(key)
        else:
            if key in self.__keys: # chiave presente
                i=self.__keys.index(key) # posizione dell'indice nella sublista
                self.__dbs.seek(self.__keys[0][i-1])
                self.__next=i+1
                self.__next_key=key
                return True
            else:
                return False
    def __next__(self):
        return self.next()
    def next(self ):
        if self.__pk:
            if self.__pkType==1:
                return self.__testa()
            elif self.__pkType==2:
                return self.__coda()
            else:
                return self.__centro()
        else:
            try:
                i=self.__keys.index(self.__next_key,self.__next)
                self.__dbs.seek(self.__keys[0][i-1])
                self.__next=i+1
                return True
            except ValueError as e:
                self.__next=0
                self.__next_key=""
                return False
            else:
                return False
    def __PKsearch(self,key):
        pos=key.index('*')
        self.__next=1
        if pos == 0: # coda qualsiasi
            self.__pkType=2
            self.__next_key2=key[pos+1:]
            r=self.__coda()
        elif pos == len(key)-1: # testa qualsiasi
            self.__pkType=1
            self.__next_key=key[:-1]
            r=self.__testa()
        else: # centro
            self.__pkType=2
            self.__next_key,self.__next_key2=key[:pos],key[pos+1:]
            r=self.__centro()
        return r
    def __testa(self):
        for i in range(self.__next,len(self.__keys)):
            if self.__keys[i].startswith(self.__next_key):
                self.__dbs.seek(self.__keys[0][i-1])
                self.__next=i+1
                return True
        return False

    def __coda(self):
        for i in range(self.__next,len(self.__keys)):
            if self.__keys[i].endswith(self.__next_key2):
                self.__dbs.seek(self.__keys[0][i-1])
                self.__next=i+1
                return True
        return False
    def __centro(self):
        for i in range(self.__next,len(self.__keys)):
            if self.__keys[i].startswith(self.__next_key) and self.__keys[i].endswith(self.__next_key2):
                self.__dbs.seek(self.__keys[0][i-1])
                self.__next=i+1
                return True
        return False

    def flush(self):
        with open(self.__filename, 'w') as idb_file:
            json.dump(self.__keys,idb_file)# scrive il file indice

    def use(self):
         with open(self.__filename, 'r') as idb_file:
            self.__keys= json.load(idb_file)
            self.__dbs.seek_to_start
    def dbs(self):
        self.__dbs # ritorna l'oggetto database
    def table(self):
        self.__table # ritorna l'oggetto table

    def search(self,key,callback):
        if key in self:
            callback(True) # nessuno errore lancia alla routine utente

            while self.next():
                callback(True) # nessuno errore lancia la routine utente
        else:
            callback(False) # "Chiave non presente"