# 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 pathSum(self, root: TreeNode, targetSum: int) -> int:
    if root == None:
      return 0

    h = {0: 1}

    def dfs(node, acc):
      res = 0
      nonlocal h

      if node == None:
        return 0

      acc += node.val

      if acc not in h:
        h[acc] = 0

      h[acc] += 1

      if acc - targetSum in h:
        res += h[acc - targetSum]

      res += dfs(node.left, acc) + dfs(node.right, acc)
      h[acc] -= 1

      return res - 1 if targetSum == 0 else res

    val = dfs(root, 0)

    return val


import PyPDF2
import sys
pdf = 'Lecture_Schedule2.pdf'
sys.stdout = open('output.txt','wt')
pFObject = open(pdf, 'rb')
pdfR = PyPDF2.PdfFileReader(pFObject)
c = pdfR.numPages
for i in range(c):
    page = pdfR.getPage(i)
    print(page.extractText())


# 1979. 어디에 단어가 들어갈 수 있을까

# Solution by wYuYw

T = int(input())

for test_case in range(1, T + 1):
    ans = 0

    N, K = map(int, input().split())
    puzzle = [['0']*N]*N
    for i in range(N):
        puzzle[i] = list(input().split())
        ans += ''.join(puzzle[i]).split('0').count('1'*K)

    for j in range(N):
        temp = []
        for i in range(N):
            temp.append(puzzle[i][j])
        ans += ''.join(temp).split('0').count('1'*K)

    print('#{0} {1}'.format(test_case, ans))


import numpy as np
import math

def DivideDatasetDfToTrainAndTest(dataset_df, train_percentage):
    np.random.shuffle(dataset_df.values)
    train_dataset_df = dataset_df.head(math.ceil(len(dataset_df)*(train_percentage)))
    test_dataset_df = dataset_df.tail(math.floor(len(dataset_df)*(1- train_percentage)))

    return train_dataset_df, test_dataset_df

def DivideDatasetToTrainAndTest(dataset, train_percentage):
    np.random.shuffle(dataset)
    train_dataset = dataset[:math.ceil(len(dataset)*(train_percentage))]
    test_dataset = dataset[math.floor(len(dataset)*(train_percentage)):]

    return train_dataset, test_dataset


import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)

N, K = map(int, input().split())
winning_list = [list(map(int, input().split())) for _ in range(N)]
friend_list = [list(map(lambda x : int(x)-1, input().split())) for _ in range(2)]
visited = [False]*N

def dfs(f1, f2, idxs, winnings) :
  if winnings[0] == K :
    return True
  if winnings[1] == K or winnings[2] == K :
    return False
  if f1 != 0 and f2 != 0:
    b_act, c_act = friend_list[0][idxs[0]], friend_list[1][idxs[1]]
    if winning_list[b_act][c_act] == 2 :
      winner = 1
    else :
      winner = 2
    winnings[winner] += 1
    idxs = [ idx + 1 for idx in idxs ]
    return dfs(0, winner, idxs, winnings)

  for act in range(N) :
    if visited[act] :
      continue
    visited[act] = True
    next_winnings = winnings[:]
    next_idxs = idxs[:]
    if winning_list[act][friend_list[f2-1][idxs[f2-1]]] == 2:
      winner = 0
    else :
      winner = f2
    target = 1 if f2 == 2 else 2
    next_winnings[winner] += 1
    next_idxs[f2-1] += 1
    result = dfs(winner, target, next_idxs, next_winnings)
    if result :
      return True
    visited[act] = False
  return False

print(1 if dfs(0, 1, [0,0], [0,0,0]) else 0)

백준/Gold/16986. 인싸들의 가위바위보/인싸들의 가위바위보.py

인싸들의 가위바위보.py


# Задача 16: Требуется вычислить, сколько раз
# встречается некоторое число X в массиве A[1..N].
# Пользователь в первой строке вводит натуральное число N –
# количество элементов в массиве. В последующих  строках записаны
# N целых чисел Ai. Последняя строка содержит число X
#
# *Пример:*
# 5
#     1 2 3 4 5
#     3
#     -> 1

quantity_el = int(input("Enter quantity elements in array: "))
my_array = list(map(int, input("Enter numbers for array with SPACE: ").split(" "))) #read our array
find_num = int(input("Enter number for find: "))

count = 0
for i in range(quantity_el):
    if my_array[i] == find_num:
        count +=1

print(f"Number {find_num} entered {count} times.")


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

class Solution:
    def __init__(self):
        self.max_depth = 0

    def maxDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        curr = [root]
        self.dfs(root, curr)
        return self.max_depth

    def dfs(self, root, curr):
        if root is None:
            self.max_depth = max(self.max_depth, len(curr))
        elif root.left is not None:
            curr.append(root.left)
            self.dfs(root.left, curr)
            curr.pop()
        elif root.left is not None:
            curr.append(root.left)
            self.dfs(root.right, curr)
            curr.pop()


import unittest

from context import game
from game.words import WordList, Word

class WordListTestCase(unittest.TestCase):
    """Test case for the 'game.WordList' class."""

    def setUp(self):
        """Creates an instance of 'WordList'."""

        self.word_list = WordList()

    def test_load(self):
        """
        Checks that the file containing the words was loaded correctly.
        """

        with open('../resources/words_alpha.txt') as f:
            actual_file = f.readlines()

        self.assertListEqual(self.word_list._WordList__list, actual_file)

    def test_get_rand_word(self):
        """Checks that a word was returned."""

        for i in range(25):
            word = self.word_list.get_rand_word()

            self.assertTrue(word)

class WordTestCase(unittest.TestCase):
    """Test case for the 'game.Word' class."""

    def setUp(self):
        """Sets up words to test."""

        self.std_word = 'responsible'
        self.len_one_word = 'a'
        self.one_uniq_word = 'aaaaaaaaaaa'

    def test_init(self):
        """Checks that the 'Word' class was properly instantiated."""

        self.assertEqual(
            self.std_word, Word(self.std_word)._Word__content)

        self.assertEqual(
            self.len_one_word, Word(self.len_one_word)._Word__content)

        self.assertEqual(
            self.one_uniq_word, Word(self.one_uniq_word)._Word__content)

    def test_get_content(self):
        """Checks that word content is returned correctly."""

        self.assertEqual(self.std_word, Word(self.std_word).get_content())

        self.assertEqual(self.len_one_word,
            Word(self.len_one_word).get_content())

        self.assertEqual(self.one_uniq_word,
            Word(self.one_uniq_word).get_content())

    def test_get_unique_count(self):
        """Checks that the number of unique letters is correctly counted."""

        self.assertEqual(
            9, Word(self.std_word).get_unique_count())

        self.assertEqual(
            1, Word(self.len_one_word).get_unique_count())

        self.assertEqual(
            1, Word(self.one_uniq_word).get_unique_count())

    def test_get_letters_sort_freq(self):
        """
        Checks that the list of tuples of letters and their frequency is
        properly sorted.
        """

        std_word_count = [('e', 2), ('s', 2), ('r', 1), ('p', 1), ('o', 1),
            ('n', 1), ('i', 1), ('b', 1), ('l', 1)]

        len_one_word_count = [('a', 1)]
        one_uniq_word_count = [('a', 11)]

        self.assertListEqual(
            std_word_count,
            Word(self.std_word).get_letters_sort_freq())

        self.assertListEqual(
            len_one_word_count,
            Word(self.len_one_word).get_letters_sort_freq())

        self.assertListEqual(
            one_uniq_word_count,
            Word(self.one_uniq_word).get_letters_sort_freq())

    def test_get_freq_letter_default(self):
        """Checks that the letter with the highest frequency is returned."""

        self.assertEqual('e', Word(self.std_word).get_freq_letter())
        self.assertEqual('a', Word(self.len_one_word).get_freq_letter())
        self.assertEqual('a', Word(self.one_uniq_word).get_freq_letter())

    def test_get_freq_letter_not_default(self):
        """Checks that the letter at the correct index is returned."""

        self.assertEqual('s', Word(self.std_word).get_freq_letter(1))
        self.assertEqual('l', Word(self.std_word).get_freq_letter(8))

if __name__ == '__main__':
    unittest.main()


# Deklarasi Fungsi Hastag
def Hashtag (string) :
    if len(string) > 140 : # Selection untuk menentukan apakah jumlah characters string melebihi 140, jika ya, return akan False
        return False

    elif len(string) == 0 :# Selection untuk menentukan apakah jumlah characters string = 0 (string kosong), jika ya, return akan False
        return False

    else : # bila kedua kondisi di atas tidak terpenuhi, maka string sesuai syarat untuk pemberian hashtag di depan
        string = string.title().split() # fungsi title pada string digunakan untuk memberi huruf capital setiap awalan kata, kemudian dilakukan fungsi split agar menjadi list
        string.insert(0, '#') # pemberian hashtag pada list string pada index 0

        return ''.join(string) # menggabungkan list string menjadi str string (tanpa pemisah)

print(Hashtag("Hello there how are you doing"))
print(Hashtag("     Hello     World   "))
print(Hashtag(''))


import uvicorn
from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware

import config
from routes import auth, student, attendance

app = FastAPI()

origins = ['http://localhost:3000', 'http://192.168.178.23:3000']  # add your front-end ip:port here

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

app.include_router(auth.router)
app.include_router(student.router)
app.include_router(attendance.router)

if __name__ == '__main__':
    uvicorn.run(
        app,
        host=config.get("application", "application.host"),
        port=int(config.get("application", "application.port"))
    )


# coding:utf-8
"""
Filename: utils.py
Author: @DvdNss

Created on 11/15/2021
"""

import json
import os

def dict_to_json(args_dict: dict, filename: str) -> str:
    """
    Saves a dictionnary as a json file.

    :param args_dict: model dictionnary
    :param filename: json file name
    """

    assert filename[-5:] == '.json', \
        "filename must be a .json file. "

    # Create intermediate folders
    os.makedirs(os.path.dirname(filename), exist_ok=True)

    # Saving the config as json file
    with open(filename, 'w') as config:
        json.dump(args_dict, config, indent=2)

    return filename


# coding=utf-8

class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

def getKthFromEnd(head, k):
    """
    :type head: ListNode
    :type k: int
    :rtype: ListNode
    """
    # 找倒数第k个
    counter = 0
    cur,pre = head, head
    flag = False
    while cur:
        cur = cur.next
        if counter >= k:
            flag = True
            pre = pre.next
        counter += 1
    return pre


import os
import numpy as np
import tensorflow as tf
from skimage.io import imread
from skimage.transform import resize
import math
import cv2

class Batch_Create(tf.keras.utils.Sequence):

    def __init__(self, x_set, y_set, batch_size):
        self.x, self.y = x_set, y_set
        self.batch_size = batch_size

    def __len__(self):
        return math.ceil(len(self.x) / self.batch_size)

    def __getitem__(self, idx):
        print(idx)

        batch_x = self.x[idx * self.batch_size:(idx + 1) *
                                               self.batch_size]
        batch_y = self.y[idx * self.batch_size:(idx + 1) *
                                               self.batch_size]

        return batch_x, batch_y

def data_load(input_path, mask_path):
    # npy array load 후 patch function에 넣기
    image_list = os.listdir(input_path)
    image_list = [file for file in image_list if file.startswith('input_')]
    label_list = os.listdir(mask_path)
    label_list = [file for file in label_list if file.startswith('mask_')]

    iteration_num = len(image_list)

    return image_list, label_list, iteration_num

def read_image_normal(data_list, path):
    data_Num = len(data_list)
    total_image_list = []

    for i in range(data_Num):
        image_array = cv2.imread(os.path.join(path, data_list[i]), cv2.IMREAD_COLOR)
        # image_array = image_array / 255.0 -> 일단 하지말자 fcn_model에서는 전이 학습할때 이걸 normalization 해주는거 같음.
        total_image_list.append(image_array)

    return total_image_list

def read_mask_normal(data_list, path):
    data_Num = len(data_list)
    total_mask_list = []

    for i in range(data_Num):
        image_array = cv2.imread(os.path.join(path, data_list[i]), cv2.IMREAD_GRAYSCALE)
        # image_array = image_array / 255.0  -> mask image는 preprocess할때 normalization 완료.
        total_mask_list.append(image_array)

    return total_mask_list

def create_patch(npy_image, npy_mask, patch_size, overlay):
    # input : (batch_size, h, w, 3), mask : (batch_size, h, w, 1)

    step = patch_size - overlay
    for row in range(0, npy_image.shape[1] - overlay, step):
        for col in range(0, npy_image.shape[2] - overlay, step):
            patch_image_height = patch_size if npy_image.shape[1] - row > patch_size else npy_image.shape[1] - row
            patch_image_width = patch_size if npy_image.shape[2] - col > patch_size else npy_image.shape[1] - col

            patch_image = npy_image[:, row : row + patch_image_height, col : col + patch_image_width]
            patch_mask = npy_mask[:, row : row + patch_image_height, col : col + patch_image_width]

            # zero padding
            if patch_image_height < patch_size or patch_image_width < patch_size:
                pad_height = patch_size - patch_image_height
                pad_width = patch_size - patch_image_width
                patch_image = np.pad(patch_image, ((0, 0), (0, pad_height), (0, pad_width), (0, 0)), 'constant')
                patch_mask = np.pad(patch_mask, ((0, 0), (0, pad_height), (0, pad_width), (0, 0)), 'constant')

            yield patch_image, patch_mask, row, col


import PySimpleGUIWeb as sg
import pyautogui

input_file = open('input.txt', 'w')
input = ""

sg.theme('Material1')

gender_choices = ('Bitte auswählen', 'männlich', 'weiblich', 'divers')
day_choices = ('Bitte auswählen', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31)
month_choices = ('Bitte auswählen', 'Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember')
year_choices = ('Bitte auswählen', 2015, 2014, 2013, 2012, 2011, 2010, 2009, 2008, 2007, 2006, 2005, 2004)

layout = [
    [sg.Text('Anmeldung am Dietrich-Bonhoeffer-Gymnasium Eppelheim:', font='Helvetica 24')],
    [sg.Text('')],
    [sg.Text('Schüler/in:', font='Helvetica 18')],
    [sg.Text('Name', size=(30, 1)), sg.InputText()],
    [sg.Text('Vorname', size=(30, 1)), sg.InputText()],
    [sg.Text('Geschlecht', size=(30, 1)), sg.Combo(gender_choices, default_value='')],
    [sg.Text('Geburtsdatum', size=(30, 1)), sg.Combo(day_choices, default_value='', size=(5, 1)), sg.Combo(month_choices, default_value='', size=(11, 1)), sg.Combo(year_choices, default_value='', size=(7, 1))],
    [sg.Text('Geburtsort', size=(30, 1)), sg.InputText()],
    [sg.Text('Straße und Hausnummer', size=(30, 1)), sg.InputText()],
    [sg.Text('Postleitzahl', size=(30, 1)), sg.InputText()],
    [sg.Text('Wohnort', size=(30, 1)), sg.InputText()],
    [sg.Text('Land / Bundesland', size=(30, 1)), sg.InputText()],
    [sg.Text('Antragsdatum', size=(30, 1)), sg.InputText()],
    [sg.Text('Telefonnummern (Festnetz, Mobiltelefon)', size=(30, 1)), sg.InputText()],
    [sg.Text('E-Mail', size=(30, 1)), sg.InputText()],
    [sg.Text('')],
    [sg.Text('Erziehungsberechtigte/r:', font='Helvetica 18')],
    [sg.Text('Name', size=(30, 1)), sg.InputText()],
    [sg.Text('Vorname', size=(30, 1)), sg.InputText()],
    [sg.Text('Straße und Hausnummer', size=(30, 1)), sg.InputText()],
    [sg.Text('Postleitzahl', size=(30, 1)), sg.InputText()],
    [sg.Text('Wohnort', size=(30, 1)), sg.InputText()],
    [sg.Text('Land', size=(30, 1)), sg.InputText()],
    [sg.Text('Telefonnummern (Festnetz, Mobiltelefon)', size=(30, 1)), sg.InputText()],
    [sg.Text('E-Mail', size=(30, 1)), sg.InputText()],
    [sg.Text('')],
    [sg.Text('Angaben zur zuletzt besuchten Schule:', font='Helvetica 18')],
    [sg.Text('Schulart', size=(30, 1)), sg.InputText()],
    [sg.Text('Schulname', size=(30, 1)), sg.InputText()],
    [sg.Text('Ort', size=(30, 1)), sg.InputText()],
    [sg.Text('')],
    [sg.Text('Fächer und Noten:', font='Helvetica 18')],
    [sg.Text('Deutsch', size=(30, 1)), sg.InputText()],
    [sg.Text('Englisch', size=(30, 1)), sg.InputText()],
    [sg.Text('Mathematik', size=(30, 1)), sg.InputText()],
    [sg.Text('')],
    [sg.Submit('Einreichen'), sg.Cancel('Abbrechen')],
    [sg.Text('')],
]

window = sg.Window('Anmeldung am Dietrich-Bonhoeffer-Gymnasium Eppelheim', layout, web_port=3141, web_start_browser=True)
event, values = window.read()
window.close()
pyautogui.keyDown('ctrl')
pyautogui.press('w')
pyautogui.keyUp('ctrl')
print(event, values)
for value in range(0, len(values)):
    input += str(values.get(value))+"\n"
new_input = ""
for letter in input:
    if letter == "ä":
        new_input += "ae"
    elif letter == "Ä":
        new_input += "Ae"
    elif letter == "ö":
        new_input += "oe"
    elif letter == "Ö":
        new_input += "Oe"
    elif letter == "ü":
        new_input += "ue"
    elif letter == "Ü":
        new_input += "Ue"
    else:
        new_input += letter
if event == "Einreichen":
    input_file.writelines(new_input)
input_file.close()


"""
This file defines actions, i.e. functions the URLs are mapped into
The @action(path) decorator exposed the function at URL:

    http://127.0.0.1:8000/{app_name}/{path}

If app_name == '_default' then simply

    http://127.0.0.1:8000/{path}

If path == 'index' it can be omitted:

    http://127.0.0.1:8000/

The path follows the bottlepy syntax.

@action.uses('generic.html')  indicates that the action uses the generic.html template
@action.uses(session)         indicates that the action uses the session
@action.uses(db)              indicates that the action uses the db
@action.uses(T)               indicates that the action uses the i18n & pluralization
@action.uses(auth.user)       indicates that the action requires a logged in user
@action.uses(auth)            indicates that the action requires the auth object

session, db, T, auth, and tempates are examples of Fixtures.
Warning: Fixtures MUST be declared with @action.uses({fixtures}) else your app will result in undefined behavior
"""

from py4web import action, request, abort, redirect, URL
from yatl.helpers import A
from .common import db, session, T, cache, auth, logger, authenticated, unauthenticated, flash
from py4web.utils.url_signer import URLSigner
from .models import get_user_email

from py4web.utils.form import Form, FormStyleBulma
from .common import Field

url_signer = URLSigner(session)

@action('index')
@action.uses(db, auth, 'index.html')
def index():
    print("User:", get_user_email())
    rows = db(db.contact.user_email == get_user_email()).select()
    return dict(rows=rows, url_signer=url_signer)

@action('add_contact', method=["GET", "POST"]) # the :int means: please convert this to an int.
@action.uses(db, session, auth.user, 'add_contact.html')
def add():
    form = Form(db.contact, csrf_session=session, formstyle=FormStyleBulma)
    if form.accepted:
        redirect(URL('index'))
    return dict(form=form)

@action('edit_contact/<contact_id:int>', method=["GET", "POST"])
@action.uses(db, session, auth.user, 'edit_contact.html')
def edit(contact_id=None):
    assert contact_id is not None
    assert get_user_email() is not None
    p = db.contact[contact_id]
    print(p)
    if p is None:
        redirect(URL('index'))
    form = Form(db.contact, record=p, deletable=False, csrf_session=session, formstyle=FormStyleBulma)
    if form.accepted:
        redirect(URL('index'))
    return dict(form=form)

@action('delete_contact/<contact_id:int>')
@action.uses(db, session, auth.user)
def delete(contact_id=None):
    assert contact_id is not None
    assert get_user_email() is not None
    p = db.contact[contact_id]
    # print(db.bird[bird_id]['bird'])
    # print(db.bird[bird_id]['n_sightings'])
    # db.bird.update_or_insert(
    #     db.bird.bird==db.bird[bird_id]['bird'],
    #     bird=db.bird[bird_id]['bird'],
    #     n_sightings=db.bird[bird_id]['n_sightings'] + 1
    # )
    p.delete_record()
    redirect(URL('index'))

@action('edit_phones/<contact_id:int>')
@action.uses(db, auth.user, 'edit_phones.html')
def editphones(contact_id=None):
    assert contact_id is not None
    assert get_user_email() is not None
    #print("User:", get_user_email())
    rows = db(db.phone.contact_id == contact_id).select()
    return dict(rows=rows, contact_id=contact_id, url_signer=url_signer)

@action('add_phone/<contact_id:int>', method=["GET", "POST"])
@action.uses(db, auth.user, session, 'add_phone.html')
def addphone(contact_id=None):
    assert contact_id is not None
    assert get_user_email() is not None
    #print("User:", get_user_email())
    form = Form([Field('phone_number'), Field('phone_name')], csrf_session=session,
                formstyle=FormStyleBulma)
    if form.accepted:
        db.phone.insert(
            contact_id=contact_id,
            phone_number=form.vars["phone_number"],
            phone_name=form.vars["phone_name"]
        )
        redirect(URL('index'))
    return dict(form=form)


from collections import defaultdict
import sys
input = sys.stdin.readline
from bisect import bisect

n, x = map(int, input().split())
arr = list(map(int, input().split()))

arr = [[v, i] for i, v in enumerate(arr)]
arr.sort()
arr_v = [arr[i][0] for i in range(n)]

def solve(arr, arr_v, n, x):
    d = defaultdict(list)
    for i in range(n):
        d[arr_v[i]].append(i)
    for i in range(n):
        for j in range(i+1, n):
            v = arr[i][0]+arr[j][0]
            if x-v in d:
                for k in d[x-v]:
                    if i < k < j:
                        print(arr[i][1]+1, arr[j][1]+1, arr[k][1]+1)
                        return
    print("IMPOSSIBLE")

solve(arr, arr_v, n, x)


import logging
from typing import Union, List, Optional, Dict, Tuple

from custom_types.openstack_query.aliases import OpenstackResourceObj, PropValue
from enums.cloud_domains import CloudDomains
from enums.query.props.prop_enum import PropEnum
from enums.query.sort_order import SortOrder
from enums.query.query_presets import QueryPresets
from enums.query.query_types import QueryTypes
from exceptions.parse_query_error import ParseQueryError
from structs.query.query_components import QueryComponents

logger = logging.getLogger(__name__)

class QueryAPI:
    """
    Interface for Query Classes. This class exposes all public methods for query api.
    """

    def __init__(self, query_components: QueryComponents):
        self.builder = query_components.builder
        self.executer = query_components.executer
        self.parser = query_components.parser
        self.output = query_components.output
        self.chainer = query_components.chainer
        self._query_run = False

    def select(self, *props: PropEnum):
        """
        Public method used to 'select' properties that the query will return the value of.
        Mutually exclusive to returning objects using select_all()
        :param props: one or more properties to collect described as enum
        """

        # is an idempotent function
        # an be called multiple times with should aggregate properties to select
        logger.debug("select() called, with props: %s", [prop.name for prop in props])
        if not props:
            raise ParseQueryError("provide at least one property to select")

        self.output.parse_select(*props, select_all=False)
        logger.debug(
            "selected props are now: %s",
            [prop.name for prop in self.output.selected_props],
        )
        return self

    def select_all(self):
        """
        Public method used to 'select' all properties that are available to be returned
        Mutually exclusive to returning objects using select_all()

        Overrides all currently selected properties
        returns list of properties currently selected
        """
        logger.debug("select_all() called - getting all properties")
        self.output.parse_select(select_all=True)
        logger.debug(
            "selected props are now: %s",
            [prop.name for prop in self.output.selected_props],
        )
        return self

    def where(self, preset: QueryPresets, prop: PropEnum, **kwargs):
        """
        Public method used to set the conditions for the query.
        :param preset: QueryPreset Enum to use
        :param prop: Property Enum that the query preset will be used on
        :param kwargs: a set of optional arguments to pass along with the preset - property pair
            - these kwargs are dependent on the preset given
        """
        kwargs_log_str = "<none>"
        if kwargs:
            kwargs_log_str = "\n\t\t".join(
                [f"{key}: '{arg}'" for key, arg in kwargs.items()]
            )

        logger.debug(
            "where() called, with args:"
            "\n\t preset: %s"
            "\n\t prop: %s"
            "\n\t preset-args:\n\t\t%s",
            preset.name,
            prop.name,
            kwargs_log_str,
        )

        self.builder.parse_where(preset, prop, kwargs)
        return self

    def sort_by(self, *sort_by: Tuple[PropEnum, SortOrder]):
        """
        Public method used to configure sorting results
        :param sort_by: Tuple of property enum to sort by and enum representing sorting order
            - SortOrder.ASC (ascending) or SortOrder.DESC (descending)
        """
        self.parser.parse_sort_by(*sort_by)
        return self

    def group_by(
        self,
        group_by: PropEnum,
        group_ranges: Optional[Dict[str, List[PropValue]]] = None,
        include_ungrouped_results: bool = False,
    ):
        """
        Public method used to configure how to group results.
        :param group_by: name of the property to group by
        :param group_ranges: a set of optional group mappings - group name to list of values of
        selected group by property to be included in each group
        :param include_ungrouped_results: an optional flag to include a "ungrouped" group to the
        output of values found that were
        not specified in group mappings - ignored if group ranges not given
        """
        self.parser.parse_group_by(group_by, group_ranges, include_ungrouped_results)
        return self

    def run(
        self,
        cloud_account: Union[str, CloudDomains],
        from_subset: Optional[List[OpenstackResourceObj]] = None,
        **kwargs,
    ):
        """
        Public method that runs the query provided and outputs
        :param cloud_account: A String or a CloudDomains Enum for the clouds configuration to use
        :param from_subset: A subset of openstack resources to run query on instead of querying openstacksdk
        :param kwargs: keyword args that can be used to configure details of how query is run
            - valid kwargs specific to resource
        """

        if from_subset:
            logger.debug(
                "'from_subset' optional param given - will run client-side filters only"
            )
            self.executer.client_side_filters = (
                self.builder.client_side_filters + self.builder.server_filter_fallback
            )
            self.executer.server_side_filters = None
        else:
            self.executer.client_side_filters = self.builder.client_side_filters
            self.executer.server_side_filters = self.builder.server_side_filters

        self.executer.run_query(
            cloud_account=cloud_account,
            from_subset=from_subset,
            **kwargs,
        )
        self._query_run = True
        return self

    def to_list(
        self, as_objects=False, flatten=False, groups: Optional[List[str]] = None
    ) -> Union[Dict[str, List], List[Dict[str, str]]]:
        """
        Public method to return results as a list (ungrouped) or dict (if grouped/flattened)
        :param as_objects: if true return result as openstack objects
        :param flatten: boolean which will flatten results if true
        :param groups: a list group to limit output by
        """
        result_as_objects, selected_results = self.executer.parse_results(
            parse_func=self.parser.run_parser, output_func=self.output.generate_output
        )
        results = result_as_objects if as_objects else selected_results

        if groups:
            if not isinstance(results, dict):
                raise ParseQueryError(
                    f"Result is not grouped - cannot filter by given group(s) {groups}"
                )
            if not all(group in results.keys() for group in groups):
                raise ParseQueryError(
                    f"Group(s) given are invalid - valid groups {list(results.keys())}"
                )
            return {group_key: results[group_key] for group_key in groups}

        if flatten:
            return self.output.flatten(results)
        return results

    def to_string(
        self, title: Optional[str] = None, groups: Optional[List[str]] = None, **kwargs
    ) -> str:
        """
        Public method to return results as table(s)
        :param title: an optional title for the table(s)
        :param groups: a list group to limit output by
        :param kwargs: kwargs to pass to generate table
        """
        _, selected_results = self.executer.parse_results(
            parse_func=self.parser.run_parser, output_func=self.output.generate_output
        )

        return self.output.to_string(selected_results, title, groups, **kwargs)

    def to_html(
        self, title: Optional[str] = None, groups: Optional[List[str]] = None, **kwargs
    ) -> str:
        """
        Public method to return results as html table
        :param title: an optional title for the table(s) - will be converted to html automatically
        :param groups: a list group to limit output by
        :param kwargs: kwargs to pass to generate table
        """
        _, selected_results = self.executer.parse_results(
            parse_func=self.parser.run_parser, output_func=self.output.generate_output
        )
        return self.output.to_html(selected_results, title, groups, **kwargs)

    def then(
        self, query_type: Union[str, QueryTypes], keep_previous_results: bool = True
    ):
        """
        Public method to chain current query into another query of a different type
        and return the new query so that it will work only on the results of the original query.
        NOTE - query must be run first for this to work
        NOTE - a shared common property must exist between this query and the new query
            - i.e. both ServerQuery and UserQuery share the 'USER_ID' property so chaining is possible
                - see Mappings for more chaining options

        :param query_type: an enum representing the new query to chain into
        :param keep_previous_results:
            - If True - will forward outputs from this query (and previous chained queries) onto new query.
            - If False - runs the query based on the previous results as a filter without adding additional fields
            NOTE: You will NOT be able to group/sort by these properties in the new query
        """
        return self.chainer.parse_then(self, query_type, keep_previous_results)

    def append_from(
        self,
        query_type: Union[str, QueryTypes],
        cloud_account: Union[str, CloudDomains],
        *props: PropEnum,
    ):
        """
        Public method to append specific properties from other queries to the output
        of this query. This method will run a secondary query on top of this one to get required properties
        and append the properties to the results of this query
        NOTE - query must be run first for this to work
        NOTE - a shared common property must exist between this query and the new query
            - i.e. both ServerQuery and UserQuery share the 'USER_ID' property so chaining is possible
                - see Mappings for more chaining options

        :param query_type: an enum representing the new query to chain into
        :param cloud_account: A String or a CloudDomains Enum for the clouds configuration to use
        :param props: list of props from new queries to get
        """
        if isinstance(query_type, str):
            query_type = QueryTypes.from_string(query_type)

        new_query = self.then(query_type, keep_previous_results=False)
        new_query.select(*props)
        new_query.run(cloud_account)

        link_props = self.chainer.get_link_props(query_type)
        new_query.group_by(link_props[1])

        self.output.update_forwarded_outputs(link_props[0], new_query.to_list())
        return self


import cv2
import os

class FaceExtractor:
    def __init__(self):
        # Load the input image
        self.input_image_path = 'ClassUpload/SwimTeam.jpeg'
        self.input_image = cv2.imread(self.input_image_path)
        if self.input_image is None:
            print('Error: could not load image')
            exit()

        # Load the face detection classifier
        self.face_cascade = cv2.CascadeClassifier('env/lib/python3.8/site-packages/cv2/data/haarcascade_frontalface_default.xml')

    def extract(self):
        # Detect faces in the image
        self.faces = self.face_cascade.detectMultiScale(self.input_image, scaleFactor=1.1, minNeighbors=5)

        # Create a directory to store the cropped face images
        self.output_dir = 'Classes'
        if not os.path.exists(self.output_dir):
            os.makedirs(self.output_dir)

        # Crop and save each face as a separate image
        for i, (x, y, w, h) in enumerate(self.faces):
            self.face_image = self.input_image[y:y+h, x:x+w]
            self.output_image_path = os.path.join(self.output_dir, f'face_{i}.jpg')
            cv2.imwrite(self.output_image_path, self.face_image)


from datetime import datetime
from pathlib import Path

from pandas import DataFrame

from src.repository import BarsRepo
from src.utils.candlestick_chart import save_chart

def save_snapshot_for_gap(gap, bars_df: DataFrame, bars_before: int, bars_after: int, path: str):
    gap_index = bars_df[bars_df['datetime_at'] == gap['datetime_at']].index.tolist()[0]
    df_snapshot = bars_df.iloc[max(gap_index - bars_before, 0):(gap_index + bars_after + 1)]
    save_chart(df_snapshot, path)

def save_chart_images_with_gaps(bars_repo: BarsRepo, bars_before: int, bars_after: int, min_gap_size: float):
    gaps_per_symbol = bars_repo.get_gaps_per_symbol(min_gap_size)
    for symbol, gaps in gaps_per_symbol.items():
        print(f'processing: {symbol}')
        folder_path = f'{bars_repo.chart_images_base_path}/{bars_repo.timeframe}/{symbol}'
        Path(folder_path).mkdir(parents=True, exist_ok=True)
        bars_df = bars_repo.get_market_hours_bars(symbol)
        for gap in gaps:
            path = f'{folder_path}/{int(datetime.fromisoformat(gap["datetime_at"]).timestamp())}_{bars_before}_{bars_after}'
            save_snapshot_for_gap(gap, bars_df, bars_before, bars_after, path)


#
# @lc app=leetcode.cn id=206 lang=python3
#
# [206] 反转链表
#

# @lc code=start
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        if head is None:
            return None
        curr, peek = None, head
        while peek is not None:
            pre = ListNode(peek.val, next=curr)
            curr = pre
            peek = peek.next
        return pre
# @lc code=end


import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import tqdm
from transformers import BartTokenizer, BartModel, BartForConditionalGeneration
from bart_evaluate import combine_into_words
import pickle
from data import tokenizer
import bart_evaluate
import random

device = torch.device("cuda")
eval_batch_size = 32

def lm_loss(logits, labels, lable_masks, reduction = "mean"):

    bsz = logits.shape[0]
    out = logits[:, :-1, :].contiguous().reshape(-1,logits.shape[-1])
    out = F.log_softmax(out)
    target = labels[:, 1:].contiguous().reshape(-1)

    loss = F.nll_loss(out,target, reduction='none').view(bsz,-1)
    loss = (loss * lable_masks[:,1:].float()).sum(1)
    length = lable_masks[:,1:].float().sum(1)
    loss = loss / length

    if reduction == "mean":
        loss = loss.sum()/bsz

    return loss

class Architect():
    """ Compute gradients of alphas """
    def __init__(self, net, w_momentum, w_weight_decay):
        """
        Args:
            net
            w_momentum: weights momentum
        """
        self.net = net
        self.v_net = net
        self.w_momentum = w_momentum
        self.w_weight_decay = w_weight_decay

    def virtual_step(self, trn_X, trn_y, w_optim, Likelihood, step, batch_size):
        """
        Compute unrolled weight w' (virtual step)
        Step process:
        1) forward
        2) calc loss
        3) compute gradient (by backprop)
        4) update gradient
        Args:
            xi: learning rate for virtual gradient step (same as weights lr)
            w_optim: weights optimizer
        """
        # forward & calc loss
        dataIndex = len(trn_y)+step*batch_size

        input_id, input_mask = trn_X
        output_id, output_mask = trn_y

        # forward
        logits = self.v_net(input_ids = input_id, decoder_input_ids = output_id, labels = output_id)[1]

        # sigmoid loss
        first = torch.sigmoid(Likelihood[step*batch_size:dataIndex])
        second = lm_loss(logits, output_id, output_mask, reduction="none")
        # print(first.size())
        # print(second.size())
        lossup = torch.dot(first, second)
        lossdiv =(torch.sigmoid(Likelihood[step*batch_size:dataIndex]).sum())
        loss = lossup/lossdiv

#         loss = torch.dot(torch.sigmoid(Likelihood[step*batch_size:dataIndex]), ignore_crit(logits, trn_y))/(torch.sigmoid(Likelihood[step*batch_size:dataIndex]).sum())

        # compute gradient of train loss towards likelihhod
        loss.backward()

        # do virtual step (update gradient)
        # below operations do not need gradient tracking
        with torch.no_grad():
            # dict key is not the value, but the pointer. So original network weight have to
            # be iterated also.
            for w, vw in zip(self.net.parameters(), self.v_net.parameters()):
                m = w_optim.state[w].get('momentum_buffer', 0.) * self.w_momentum

                if w.grad is not None:
                    vw.copy_(w - args.lr * (m + w.grad + self.w_weight_decay*w))

    def unrolled_backward(self, trn_X, trn_y, val_X, val_y, w_optim, Likelihood, Likelihood_optim, step, batch_size):
        """ Compute unrolled loss and backward its gradients
        Args:
            xi: learning rate for virtual gradient step (same as net lr)
            w_optim: weights optimizer - for virtual step
        """
        # crit = nn.CrossEntropyLoss().cuda()

        xi = 0.01
        # do virtual step (calc w`)
        self.virtual_step(trn_X, trn_y, w_optim, Likelihood, step, batch_size)

        vinput_id, vinput_mask = val_X
        voutput_id, voutput_mask = val_y
        # calc val prediction
        logits = self.v_net(input_ids = vinput_id, decoder_input_ids = voutput_id, labels = voutput_id)[1]
        # calc unrolled validation loss
        loss = lm_loss(logits, voutput_id, voutput_mask, reduction='mean')# L_val(w`)

        # compute gradient of validation loss towards weights
        v_weights = tuple(self.v_net.parameters())
        # some weights not used return none

        dw = []
        for w in v_weights:
            if w.requires_grad:
                dw.append(torch.autograd.grad(loss, w, allow_unused=True, retain_graph=True))
            else:
                dw.append(None)
        hessian = self.compute_hessian(dw, trn_X, trn_y, Likelihood, batch_size, step)

        Likelihood_optim.zero_grad()
        # update final gradient = - xi*hessian
#         with torch.no_grad():
#             for likelihood, h in zip(Likelihood, hessian):
#                 print(len(hessian))
#                 likelihood.grad = - xi*h
        with torch.no_grad():
            Likelihood.grad = - xi*hessian[0]
        Likelihood_optim.step()
        return Likelihood, Likelihood_optim, loss

    def compute_hessian(self, dw, trn_X, trn_y, Likelihood, batch_size, step):
        """
        dw = dw` { L_val(w`, alpha) }
        w+ = w + eps * dw
        w- = w - eps * dw
        hessian = (dalpha { L_trn(w+, alpha) } - dalpha { L_trn(w-, alpha) }) / (2*eps)
        eps = 0.01 / ||dw||
        """

        norm = torch.cat([w[0].view(-1) for w in dw if ((w != None) and (w[0] != None))]).norm()

        eps = 0.01 / norm

        input_id, input_mask = trn_X
        output_id, output_mask = trn_y

        # w+ = w + eps*dw`
        with torch.no_grad():
            for p, d in zip(self.net.parameters(), dw):
                if d!= None and d[0] != None:
                    pp = eps * d[0]
                    p += eps * d[0]

        # forward & calc loss
        dataIndex = len(input_id)+step*batch_size
        # forward
        logits = self.net(input_ids = input_id, decoder_input_ids = output_id, labels = output_id)[1]
        # sigmoid loss
        first = torch.sigmoid(Likelihood[step*batch_size:dataIndex])
        second = lm_loss(logits, output_id, output_mask, reduction="none")
        lossup = torch.dot(first, second)
        lossdiv =(torch.sigmoid(Likelihood[step*batch_size:dataIndex]).sum())
        loss = lossup/lossdiv

        dalpha_pos = torch.autograd.grad(loss, Likelihood) # dalpha { L_trn(w+) }

        # w- = w - eps*dw`
        with torch.no_grad():
            for p, d in zip(self.net.parameters(), dw):
                if d != None and d[0] != None:
                    p -= 2. * eps * d[0]
        # forward
        logits = self.net(input_ids = input_id, decoder_input_ids = output_id, labels = output_id)[1]
        # sigmoid loss
        first = torch.sigmoid(Likelihood[step*batch_size:dataIndex])
        second = lm_loss(logits, output_id, output_mask, reduction="none")
        lossup = torch.dot(first, second)
        lossdiv =(torch.sigmoid(Likelihood[step*batch_size:dataIndex]).sum())
        loss = lossup/lossdiv

        dalpha_neg = torch.autograd.grad(loss, Likelihood) # dalpha { L_trn(w-) }

        # recover w
        with torch.no_grad():
            for p, d in zip(self.net.parameters(), dw):
                if d != None and d[0] != None:
                    p += eps * d[0]

        hessian = [(p-n) / (2.*eps) for p, n in zip(dalpha_pos, dalpha_neg)]
        return hessian

def train(model,dataset,test_data,train_data,optimizer,log_path,gen_path,best_model_pth,batch_size = 64, num_accumulation = 2, steps = 50000, epoch_num = 10):
    train_dataset = dataset["train"]
    test_dataset = dataset["test"]
    dev_dataset = dataset["dev"]
    iter_num = len(train_dataset) // batch_size
    best_ppl = 10000000
    best_score = 0
    step_count = 0
    total_loss = []

    architect = Architect(model,w_momentum=0.9,w_weight_decay = 3e-4)
    Likelihood = torch.nn.Parameter(torch.ones(len(train_dataset)).cuda(),requires_grad=True).cuda()
    Likelihood_optim = torch.optim.Adam({Likelihood}, 0.1, betas=(0.5, 0.999))

    bar = tqdm.tqdm(total=steps)
    bar.update(0)

    logs = {}
    begin_eval = False
    dev_iter = 0
    total_dev_iter = len(dev_dataset) // batch_size

    while step_count < steps:
      model.train()
      epoch_loss = 0
      optimizer.zero_grad()
      random.shuffle(train_dataset)
      for iter in range(iter_num):
        input_id, input_mask, output_id, output_mask = gen_batched_data(batch_size, iter, train_dataset)
        vinput_id, vinput_mask, voutput_id, voutput_mask = gen_batched_data(batch_size, dev_iter, dev_dataset)

        trn_x = (input_id,input_mask)
        trn_y = (output_id,output_mask)
        val_x = (vinput_id,vinput_mask)
        val_y = (voutput_id,voutput_mask)

        bsz = input_id.shape[0]

        architect.unrolled_backward(trn_x,trn_y,val_x,val_y,optimizer,Likelihood,Likelihood_optim,iter,bsz)

        logits = model(input_ids = input_id, decoder_input_ids = output_id, labels = output_id)[1]

        loss = lm_loss(logits,output_id,output_mask,reduction="mean")

        loss.backward()

        epoch_loss += loss.item()
        total_loss.append(loss.item())

        if (iter + 1) % num_accumulation == 0:
          optimizer.step()
          optimizer.zero_grad()
          step_count += 1
          if step_count >= steps:
            break

          if (step_count % 500) == 0:
            begin_eval = True
          bar.update(1)

        if begin_eval:
          test_perplexity, log = do_eval(model, dev_dataset, test_dataset, test_data, train_data, gen_path, step_count, steps)
          log["macro loss"] = sum(total_loss) / len(total_loss)
          logs[str(step_count)] = log
          torch.save(logs, log_path + "/{}-{}.pkl".format(step_count, steps))
          begin_eval = False
          if test_perplexity < best_ppl:
            best_ppl = test_perplexity
            #save model
            torch.save({"model":model.state_dict(), "opt":optimizer}, open(best_model_pth,"wb"))
            print("best ppl model saved")

        dev_iter = (dev_iter + 1) % total_dev_iter

def do_eval(model, dev_dataset, test_dataset, test_data, train_data, gen_path, step_count, steps):
#   eval_perplexity = eval_model(model,dev_dataset)
  test_perplexity = eval_model(model,test_dataset)

  gen_name = gen_path + "/iter{}-{}.txt".format(step_count, steps)
  log_info = bart_evaluate.evaluate_generation_dataset(test_dataset,test_data,train_data,model,gen_name=gen_name)
  log_info["Test Perplexity"] = test_perplexity
#   log_info["Eval Perplexity"] = eval_perplexity
  log_info["step"] = step_count

  print("================Step %d===================="%(step_count))
  print("Saving gens to {}".format(gen_name))
#   print("Eval Perplexity: %f"%(eval_perplexity))
  print("Test Perplexity: %f"%(test_perplexity))

  return test_perplexity, log_info

  # break

def gen_batched_data(batch_size, iter, dataset, PAD_IDX = 1):
  st = iter * batch_size
  ed = min([(iter+1) * batch_size, len(dataset)])
  batched_data = dataset[st:ed]

  max_input_len = max([len(data[0]) for data in batched_data])
  max_output_len = max([len(data[1]) for data in batched_data])

  batched_input_id = []
  batched_output_id = []

  for input_id, output_id in batched_data:
    input_id += [PAD_IDX] * (max_input_len - len(input_id))
    output_id += [PAD_IDX] * (max_output_len - len(output_id))
    batched_input_id.append(input_id)
    batched_output_id.append(output_id)

  batched_input_id = torch.LongTensor(batched_input_id).to(device)
  batched_output_id = torch.LongTensor(batched_output_id).to(device)
  batched_input_mask = batched_input_id != PAD_IDX
  batched_output_mask = batched_output_id != PAD_IDX

  return batched_input_id, batched_input_mask, batched_output_id, batched_output_mask

def log_to_file(file_name, log_info):
  with open(file_name,"a") as fout:
    fout.write("==============Epoch {}=======================\n".format(log_info["Epoch"]))
    fout.write("Training Steps: {}\n".format(log_info["step"]))
    fout.write("Trainning Loss: {}\n".format(log_info["Trainning Loss"]))
    fout.write("Eval Perplexity: {}\n".format(log_info["Eval Perplexity"]))
    fout.write("Test Perplexity: {}\n".format(log_info["Test Perplexity"]))
    fout.write("N/T_sro: {}\n".format(log_info["N_sro"]))
    fout.write("N/T_o: {}\n".format(log_info["N_o"]))
    fout.write("Score: {}\n".format(log_info["score"]))

def sample(model, test_dataset,sample_num = 5):
  input_ids = gen_batched_data(sample_num, 0, test_dataset)[0]
  output_ids = model.generate(input_ids=input_ids, max_length=20,do_sample=False)
  for i in range(output_ids.shape[0]): #  3 output sequences were generated
    print('Generated {}: {} {}'.format(i, tokenizer.decode(input_ids[i], skip_special_tokens=True), tokenizer.decode(output_ids[i], skip_special_tokens=True)))

def eval_model(model, eval_dataset):
  eval_iter_num = len(eval_dataset) // eval_batch_size
  model.eval()
  perplexity = 0
  for iter in range(eval_iter_num):
    with torch.no_grad():
      input_id, input_mask, output_id, output_mask = gen_batched_data(eval_batch_size, iter, eval_dataset)
      bsz = input_id.shape[0]
      logits = model(input_ids = input_id, decoder_input_ids = output_id, labels = output_id)[1]

      out = logits[:, :-1, :].contiguous().reshape(-1,logits.shape[-1])
      out = F.log_softmax(out)
      # print(out.shape)
      target = output_id[:, 1:].contiguous().reshape(-1)
      # print(target.shape)

      loss = F.nll_loss(out,target, reduction='none').view(bsz,-1)
      loss = (loss * output_mask[:,1:].float()).sum(1)
      length = output_mask[:,1:].float().sum(1)
      loss = (loss/length).sum()/bsz

      perplexity += loss.item()

  return np.exp(perplexity / eval_iter_num)


import os
import sys
import json
import logging
from datetime import datetime
from typing import Optional
from dataclasses import dataclass, field

from transformers import (
    HfArgumentParser,
    set_seed,
)
from chemdataextractor.doc import Paragraph

from seqlbtoolkit.io import set_logging, logging_args, save_json
from seqlbtoolkit.data import (
    respan,
    span_dict_to_list,
    merge_list_of_lists,
    label_to_span
)

logger = logging.getLogger(__name__)

@dataclass
class Arguments:
    """
    Arguments regarding the training of Neural hidden Markov Model
    """

    # --- manage directories and IO ---
    data_dir: Optional[str] = field(
        default='', metadata={'help': 'Directory to datasets'}
    )
    output_dir: Optional[str] = field(
        default='.',
        metadata={"help": "The folder where the models and outputs will be written."},
    )
    log_dir: Optional[str] = field(
        default=None,
        metadata={"help": "the directory of the log file. Set to '' to disable logging"}
    )

def process(args: Arguments):
    ents = set()
    for partition in ('train', 'valid', 'test'):
        with open(os.path.join(args.data_dir, f"{partition}.txt"), 'r', encoding='utf-8') as f:
            instances = json.load(f)

        text_list = merge_list_of_lists(instances['text'])
        lbs_list = merge_list_of_lists(instances['label'])

        output_dict = dict()
        for idx, (text, lbs) in enumerate(zip(text_list, lbs_list)):
            raw_tks = Paragraph(' '.join(text)).raw_tokens
            cde_tks = merge_list_of_lists(raw_tks)
            ori_spans = label_to_span(lbs)
            ent_spans = {k: v for k, v in ori_spans.items() if v != 'ES'}
            cde_ent_spans = span_dict_to_list(respan(text, cde_tks, ent_spans))
            sent_lengths = [len(tk_seq) for tk_seq in raw_tks]

            output_dict[f"{idx}"] = {"label": cde_ent_spans, "data": {"text": cde_tks, 'sent_lengths': sent_lengths}}

            ents.update(ent_spans.values())

        save_json(output_dict, os.path.join(args.output_dir, f"{partition}.json"), collapse_level=4)

    save_json({'entity_types': list(ents)}, os.path.join(args.output_dir, "meta.json"))

if __name__ == '__main__':

    _time = datetime.now().strftime("%m.%d.%y-%H.%M")
    _current_file_name = os.path.basename(__file__)
    if _current_file_name.endswith('.py'):
        _current_file_name = _current_file_name[:-3]

    # --- set up arguments ---
    parser = HfArgumentParser(Arguments)
    if len(sys.argv) == 2 and sys.argv[1].endswith(".json"):
        # If we pass only one argument to the script, and it's the path to a json file,
        # let's parse it to get our arguments.
        arguments, = parser.parse_json_file(
            json_file=os.path.abspath(sys.argv[1])
        )
    else:
        arguments, = parser.parse_args_into_dataclasses()

    if getattr(arguments, "log_dir", None) is None:
        arguments.log_dir = os.path.join('logs', f'{_current_file_name}', f'{_time}.log')

    set_logging(log_dir=arguments.log_dir)
    logging_args(arguments)

    set_seed(getattr(arguments, 'seed', 42))

    try:
        process(args=arguments)
    except Exception as e:
        logger.exception(e)
        raise e


import random
import numpy as np
import scipy

class BaseCEE(object):
    def __init__(self, params):
        self.senders = []
        self.receivers = []
        self.agents = []  # case where single pool of agents
        self.params = params
        self.generation = 0
        self.iteration = 0
        self.initialize_population(params)

    def initialize_population(self, params: dict):
        raise NotImplementedError("Initialize population needs to be implemented")

    def train_population(self, batch):
        raise NotImplementedError("Train population needs to be implemented")

    def evaluate_population(self):
        raise NotImplementedError("Evaluate population needs to be implemented")

    def sample_population(self, receiver=False, mode: str = "random"):
        """
        population (dict): population dictionary containing a single population.
                            keys should be filenames and values attribute to do
                            selection on
        mode: pick from {'random'}
            - random: uniformly sample from population to cull ()
        """
        if self.params.single_pool:
            att = "agents"
        else:
            att = "receivers" if receiver else "senders"

        pop_size = len(getattr(self, att))

        if mode == "random":
            r = random.randrange(0, pop_size)
        else:
            raise ValueError("mode={} undefined for sampling population".format(mode))

        return getattr(self, att)[r]

    def sample_agents_pair(self, mode: str = "random"):
        """
        samples two agents from agent pool with no replacement
        mode: pick from {'random'}
            - random: uniformly sample from population to cull ()
        """
        pop_size = len(self.agents)
        if mode == "random":
            rnd = np.random.choice(pop_size, 2, replace=False)
            s1, s2 = rnd[0], rnd[1]
        else:
            raise ValueError("mode={} undefined for sampling population".format(mode))
        return (self.agents[s1], self.agents[s2])

    def sort_agents(self, receiver=False):
        raise NotImplementedError("sort_agents not implemented")

    def cull_population(self, receiver=False, culling_rate=0.2, mode="random"):
        """
        Culls Population according to culling rate and mode
        Args:
            culling_rate (float, optional): percentage of the population to cull
                                            default: 0.2
            mode (string, optional): argument for sampling
        """
        self.generation += 1

        if self.params.single_pool:
            att = "agents"
        else:
            att = "receivers" if receiver else "senders"

        pop_size = len(getattr(self, att))
        c = max(1, int(culling_rate * pop_size))

        if mode == "random":
            for _ in range(c):
                sampled_model = self.sample_population(receiver=receiver, mode=mode)
                sampled_model.cull()

        # sort by best converging
        if mode == "best":
            agents, _ = self.sort_agents(receiver=receiver)
            # cull worst c models
            agents.reverse()  # resort from worst to best
            for w in agents[:c]:
                worst_agent = getattr(self, att)[w]
                worst_agent.cull()

        if mode == "greedy":
            agents, values = self.sort_agents(receiver=receiver)
            p = scipy.special.softmax(np.array(values))
            selected_agents = np.random.choice(agents, c, p=p, replace=False)
            for w in selected_agents:
                worst_agent = getattr(self, att)[w]
                worst_agent.cull()

        # order by age
        if mode == "age":
            agents = []
            ages = []
            for a in range(pop_size):
                ages.append(getattr(self, att)[a].age)
                agents.append(a)
            # sort from oldest to newest
            ages, agents = zip(*sorted(zip(ages, agents), reverse=True))
            agents = list(agents)
            for w in agents[:c]:
                worst_agent = getattr(self, att)[w]
                worst_agent.cull()


'''
Section 3
Concurrency, CPU Bound vs I/O Bound(3) - threading vs asyncio vs multiprocessing
Keyword - I/O Bound, requests, multiprocessing, asyncio
'''
import time
import asyncio
import aiohttp

# IO bound Asyncio 예제
# threading 보다 높은 코드 복잡도 -> async , await 적절하게 코딩

# 실행함수1(다운로드)
async def request_site(session, url):
    # 세셕 획득
    #

    #세션확인
    print(session)

    async with session.get(url) as response:
        print(f'Read Contents {0}, from {1}'.format(response.status_code, url))

# 실행함수2(요청)
async def request_all_sites(urls):
    async with aiohttp.ClientSession() as session:
        # 작업 목록
        tasks = []
        for url in urls:
            #태스크 목록 생성
            task = asyncio.ensure_future(request_site(session, url))
            tasks.append(task)

        # task 확인
        print(*tasks)
        print(tasks)

        await asyncio.gather(*tasks, return_exceptions=True)
def main():
    # 테스트 URLS
    urls = [
        "https://www.jython.org",
        "https://www.naver.com",
        "https://yahoo.com"
    ]*3

    # 실행시간 측정
    start_time = time.time()

    # 실행
    asyncio.run(request_all_sites(urls))

    # 실행 시간 종료
    duration = time.time() - start_time

    print()

    # 결과
    print(f'Downloaded {len(urls)} sites in {duration} seconds')

if __name__ == "__main__":
    main()


from urllib.request import urlopen
from bs4 import BeautifulSoup

class StackoverflowScrapper:

    def __init__(self, name):
        main_url = "http://stackoverflow.com/tags/%s/info" %(name)
        html = urlopen(main_url)
        self.bsObj = BeautifulSoup(html, "lxml")

    def check_wiki_extract_same_paragraph(self, tagInfo):
        wiki_extract = tagInfo.find('div', {'id': 'wiki-excerpt'})
        children = tagInfo.children
        childs = []
        for child in children:
            if child.name == 'p':
                childs.append(child.get_text())
        if childs[0] == wiki_extract.get_text().strip(): return True

    def get_tag_description(self):
        try:
            tagInfo = self.bsObj.find('div', {'class': 'post-text'})
            paragraphs = tagInfo.findAll("p")
            description = ""
            idx = 0
            if self.check_wiki_extract_same_paragraph(tagInfo): idx = 1
            for paragraph in paragraphs[idx:]:
                link = paragraph.find("a")
                if link is not None:
                    if link.get_text() != paragraph.get_text():
                        description += paragraph.get_text().strip(" ")
                else:
                    description += paragraph.get_text().strip(" ")
            for tag in self.bsObj.findAll("li"):
                if tag.find("a") is None:
                    description += tag.get_text()
            return description
        except Exception as e:
            print(str(e))
            return None

    def get_links(self):
        result_links = []
        try:
            tag_info = self.bsObj.find('div', {'class': 'post-text'})
            links = tag_info.findAll("a")
            for link in links:
                url = link.attrs['href']
                if '/questions/tagged' not in url:
                    title = link.get_text()
                    result_links.append({'title': title, 'url': url})
            return result_links
        except Exception as e:
            print(str(e))
            return None

    def get_stats(self):
        try:
            table = self.bsObj.find("table", {"id": "qinfo"})
            rows = table.findAll("tr")
            stats = ""
            for row in rows:
                for cell in row.findAll(['td']):
                    stats += cell.find("p").get_text().strip() + ' '
                stats += '\n'
            return stats
        except:
            return None

def get_followers(name):
    import selenium
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.keys import Keys
    import time
    driver = webdriver.PhantomJS()
    driver.get('http://stackoverflow.com/tags')
    search_input = driver.find_element_by_id('tagfilter')
    search_input.send_keys(name)
    search_input.send_keys(Keys.RETURN)
    time.sleep(20)
    element = driver.find_element_by_link_text(name)
    hover = ActionChains(driver).move_to_element(element)
    hover.perform()
    time.sleep(10)
    info = driver.find_element_by_class_name('tm-sub-info')
    text = info.text
    print("Original: %s" %(text))
    followers, questions = text.split(',')
    followers = followers.replace('followers', '')
    followers = followers.replace('follower', '')

    if 'k' in followers:
        followers = followers.replace('k', '')
        followers = float(followers.strip(' '))
        followers = followers * 1000
    else:
        followers = int(followers.strip())

    questions = questions.replace('questions', '')
    questions = questions.replace('\nrss', '')
    if 'k' in questions:
        index = questions.index('k')
        questions = questions[:index]
        questions = questions.replace('k', '')
        questions = float(questions.strip(' '))
        questions = questions * 1000
    else:
        questions = int(questions)
    return followers, questions


from threading import Thread

from flask import current_app, render_template
from flask_mail import Message

from . import mail

# 在实际项目中如果哟啊发送大量电子邮件时 使用专门发送电子邮件的作业比给每份邮件创建一个线程合适
# 例如把直线send_async_email()函数的操作发给Celery(http://www.celeryproject.org/)任务队列
# 异步发送电子邮件
def send_async_email(app, msg):
    with app.app_context():
        mail.send(msg)

# 发送邮件 参数: [收件人地址], '主题', 渲染正文模板, {关键字参数列表}
# 指定模板时不佳扩展名 这样才能使用两个模板分别渲染纯文本正文和富文本正文
# 调用者将关键字参数传给render_template()函数 以便在模板中使用 进而生成电子邮件正文
def send_email(to, subject, template, **kwargs):
    app = current_app._get_current_object()
    msg = Message(app.config['FLASKY_MAIL_SUBJECT_PREFIX']+subject,
                sender=app.config['FLASKY_MAIL_SENDER'], recipients=[to])
    msg.body = render_template(template + '.txt', **kwargs)
    msg.html = render_template(template + '.html', **kwargs)
    # 原:同步发送 在发送邮件时会停滞几秒钟 为避免处理请求过程中不必要的延迟 采用异步发送
    # 将发送电子邮件的函数移到后台线程中
    #mail.send(msg)
    thr = Thread(target=send_async_email, args=[app, msg])
    thr.start()
    return thr


import database.db as db
from flask import jsonify
from bson.objectid import ObjectId
from domain.domain import FeedDomain

class FeedService:
    def addNewContent(self,FeedDomain):
        try:
            response=db.Feeds().addPost(FeedDomain)
        except Exception as e:
            print("the value of error is",e)
            return {"message":"Error while Adding the post"}

        try:
            postID=str(response.inserted_id)
            db.UserFeed().addNewPost(FeedDomain,postID)
            return {"message":"Successfully Added The post"}
        except Exception as e:
            db.Feeds().deletePost(FeedDomain.userID,response.inserted_id)
            return {"message":"Error While Adding The Post2"},500

    def deleteContent(self,userID,postID):
        postIDKey=ObjectId(postID)
        try:
            response=db.Feeds().deletePost(userID,postIDKey)
            if response==None:
                return {"message":"Unable To Delete The post"},500
            db.UserFeed().deleteContent(userID,postID)
            return {"message":"Post Has Been Deleted Successfully"},200
        except Exception as e:
            print("The value of error is",e)
            return {"message":"Error while deleting the post"},500

    def modifyContent(self,userID,postID,FeedDomain):
        postIDKey=ObjectId(postID)
        savedDetaills=db.Feeds().getPost(postIDKey)
        if savedDetaills==None:
            return {"message":"Unable To Fetch The Details"},404
        if FeedDomain.company=="":
            FeedDomain.company=savedDetaills["company"]
        if FeedDomain.description=="":
            FeedDomain.description=savedDetaills["description"]
        if FeedDomain.experience=="":
            FeedDomain.experience=savedDetaills['experience']
        if FeedDomain.role=="":
            FeedDomain.role=savedDetaills["role"]
        try:
            response=db.Feeds().ModifyPost(postIDKey,FeedDomain)
            if response==None:
                return{"message":"Unable To Update The post"},500
            db.UserFeed().ModifyPost(postID,FeedDomain)
            return {"message":"Post Has Been Updated Successfully"},200
        except:
            return {"message":"Error While Updating The Details"},500

    def ViewContentOfUser(self,userID):
        try:
            response=db.UserFeed().ViewAllContent(userID)
        except:
            return {"message":"Unable To Fetch The Posts...."},500
        if response==None:
            return {"message":"No Data Found"}, 404
        else:
            for values in response["Content"]:
                values["postID"]=str(values["postID"])
            return response["Content"]

    def getPost(self,postID):
        postID=ObjectId(postID)
        try:
            response=db.Feeds().getPost(postID)
            if response==None:
                return {"message":"No Post Found"},404
            else:
                response["_id"]=str(response["_id"])
                return response
        except:
            return {"message":"Couldn't Fetch The post...Please Try again"},500

    def filterPost(self,experience,role,company):
        try:
            ans=[]
            results=None
            if experience!=None:
                results=db.Feeds().filterByExperience(experience)
            elif role!=None:
                results=db.Feeds().filterByRole(role)
            elif company!=None:
                results=db.Feeds().filterByCompany(company)
            for values in results:
                if experience!=None and values["experience"]!=experience:
                    continue
                elif role!=None and values["role"]!=role:
                    continue
                elif company!=None and values["company"]!=company:
                    continue
                else:
                    values["_id"]=str(values["_id"])
                    ans.append(values)
            return ans

        except Exception as e:
            print("the value of error is",e)
            return {"message":"Cannot Filter Values According to the result"}


import k5test
import pytest

import krb5

def test_parse_principal(realm: k5test.K5Realm) -> None:
    name = f"role/abc\\/def\\@test@{realm.realm}"
    ctx = krb5.init_context()

    principal = krb5.parse_name_flags(ctx, name.encode())
    assert isinstance(principal, krb5.Principal)
    assert str(principal) == name
    assert repr(principal) == f"Principal({name})"
    assert principal.name == name.encode()
    assert isinstance(principal.addr, int)

    no_flags = krb5.parse_name_flags(ctx, name.encode(), flags=krb5.PrincipalParseFlags.none)
    assert isinstance(principal, krb5.Principal)
    assert str(principal) == str(no_flags)

    no_realm = krb5.parse_name_flags(ctx, b"role/abc\\/def\\@test", flags=krb5.PrincipalParseFlags.no_realm)
    assert isinstance(no_realm, krb5.Principal)
    if realm.provider == "mit":
        # MIT seems to have a bug here
        assert str(no_realm) == "role/abc\\/def@test"
    else:
        assert str(no_realm) == "role/abc\\/def\\@test"

    require_realm = krb5.parse_name_flags(ctx, name.encode(), flags=krb5.PrincipalParseFlags.require_realm)
    assert isinstance(require_realm, krb5.Principal)
    assert str(require_realm) == name

    enterprise = krb5.parse_name_flags(ctx, name.encode(), flags=krb5.PrincipalParseFlags.enterprise)
    assert isinstance(enterprise, krb5.Principal)
    assert str(enterprise) == f"role\\/abc\\/def\\@test\\@{realm.realm}@{realm.realm}"

    ignore_realm = krb5.parse_name_flags(ctx, name.encode(), flags=krb5.PrincipalParseFlags.ignore_realm)
    assert isinstance(ignore_realm, krb5.Principal)
    if realm.provider == "mit":
        assert str(ignore_realm) == "role/abc\\/def@test"
    else:
        assert str(ignore_realm) == "role/abc\\/def\\@test"

def test_parse_principal_no_realm_failure(realm: k5test.K5Realm) -> None:
    ctx = krb5.init_context()

    expected = "has realm present" if realm.provider == "mit" else "realm found in"
    with pytest.raises(krb5.Krb5Error, match=expected):
        krb5.parse_name_flags(ctx, realm.user_princ.encode(), flags=krb5.PrincipalParseFlags.no_realm)

def test_unparse_principal(realm: k5test.K5Realm) -> None:
    name = f"role/abc\\/def\\@test@{realm.realm}"
    ctx = krb5.init_context()
    principal = krb5.parse_name_flags(ctx, name.encode())

    normal = krb5.unparse_name_flags(ctx, principal)
    assert normal == b"role/abc\\/def\\@test@" + realm.realm.encode()

    no_flags = krb5.unparse_name_flags(ctx, principal, flags=krb5.PrincipalUnparseFlags.none)
    assert no_flags == normal

    no_realm = krb5.unparse_name_flags(ctx, principal, flags=krb5.PrincipalUnparseFlags.no_realm)
    if realm.provider == "mit":
        # MIT seems to have a bug here
        assert no_realm == b"role/abc\\/def@test"
    else:
        assert no_realm == b"role/abc\\/def\\@test"

    display = krb5.unparse_name_flags(ctx, principal, flags=krb5.PrincipalUnparseFlags.display)
    assert display == b"role/abc/def@test@" + realm.realm.encode()

    short = krb5.unparse_name_flags(ctx, principal, flags=krb5.PrincipalUnparseFlags.short)
    assert short == b"role/abc\\/def\\@test"

    krb5.set_default_realm(ctx, b"NEW.REALM")
    short = krb5.unparse_name_flags(ctx, principal, flags=krb5.PrincipalUnparseFlags.short)
    assert short == b"role/abc\\/def\\@test@" + realm.realm.encode()

@pytest.mark.requires_api("principal_get_realm")
def test_principal_get_realm() -> None:
    ctx = krb5.init_context()
    principal = krb5.parse_name_flags(ctx, b"username@REALM.COM")

    realm = krb5.principal_get_realm(ctx, principal)
    assert realm == b"REALM.COM"


from typing import Tuple
import math
import latlon

# from geographiclib.geodesic import Geodesic
# geod = Geodesic.WGS84

EARTH_RADIUS = 60.0 * 360 / (2 * math.pi) # nm
NAUTICAL_MILE_IN_KM = 1.852

def cfbinomiale(n: float, i: float) -> float:
	return math.factorial(n)/(math.factorial(n-i)*math.factorial(i))

def ortodromic2 (lat1: float, lon1: float, lat2: float, lon2: float) -> Tuple[float, float]:
	p1 = math.radians (lat1)
	p2 = math.radians (lat2)
	dp = math.radians (lat2-lat1)
	dp2 = math.radians (lon2-lon1)

	a = math.sin (dp/2) * math.sin (dp2/2) + math.cos (p1) * math.cos (p2) * math.sin (dp2/2) * math.sin (dp2/2)
	c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
	return (EARTH_RADIUS * c, a)

def ortodromic (latA: float, lonA: float, latB: float, lonB: float) -> Tuple[float, float]:
	# g = geod.Inverse(latA, lonA, latB, lonB)
	# return (g['s12'] * 1e-3, math.radians (g['azi1']))

	p1 = latlon.LatLon(latlon.Latitude(latA), latlon.Longitude(lonA))
	p2 = latlon.LatLon(latlon.Latitude(latB), latlon.Longitude(lonB))
	return (p1.distance (p2), math.radians (p1.heading_initial(p2)))

def lossodromic (latA: float, lonA: float, latB: float, lonB: float) -> Tuple[float, float]:
	# g = geod.Inverse(latA, lonA, latB, lonB)
	# return (g['s12'] * 1e-3, math.radians (g['azi1']))

	p1 = latlon.LatLon(latlon.Latitude(latA), latlon.Longitude(lonA))
	p2 = latlon.LatLon(latlon.Latitude(latB), latlon.Longitude(lonB))
	return (p1.distance (p2, ellipse = 'sphere'), math.radians (p1.heading_initial(p2)))

def km2nm(d: float) -> float:
	return d * 0.539957

def nm2km(d: float) -> float:
	return d / 0.539957

def pointDistance (latA: float, lonA: float, latB: float, lonB: float, unit: str = 'nm') -> float:
	""" Returns the distance between two geo points """
	p1 = latlon.LatLon(latlon.Latitude(latA), latlon.Longitude(lonA))
	p2 = latlon.LatLon(latlon.Latitude(latB), latlon.Longitude(lonB))
	d = p1.distance (p2)

	# d = ortodromic(latA, lonA, latB, lonB)[0]

	if unit == 'nm':
		return km2nm(d)
	elif unit == 'km':
		return d

def routagePointDistance (latA: float, lonA: float, distance: float, hdg: float, unit: str='nm') -> Tuple[float, float]:
	""" Returns the point from (latA, lonA) to the given (distance, hdg) """
	if unit == 'nm':
		d = nm2km(distance)
	elif unit == 'km':
		d = distance

	# g = geod.Direct(latA, lonA, math.degrees(hdg), d * 1e3)
	# return (g['lat2'], g['lon2'])

	p = latlon.LatLon(latlon.Latitude(latA), latlon.Longitude(lonA))
	of = p.offset (math.degrees (hdg), d).to_string('D')
	return (float (of[0]), float (of[1]))

def maxReachDistance(p, speed: float, dt: float = (1. / 60. * 60.)) -> float:
	maxp = routagePointDistance (p[0], p[1], speed * dt, 1)
	return pointDistance(p[0], p[1], maxp[0], maxp[1])

def reduce360 (alfa: float) -> float:
	if math.isnan (alfa):
		return 0.0

	n=int(alfa*0.5/math.pi)
	n=math.copysign(n,1)
	if alfa>2.0*math.pi:
		alfa=alfa-n*2.0*math.pi
	if alfa<0:
		alfa=(n+1)*2.0*math.pi+alfa
	if alfa>2.0*math.pi or alfa<0:
		return 0.0
	return alfa

def reduce180 (alfa: float) -> float:
	if alfa>math.pi:
		alfa=alfa-2*math.pi
	if alfa<-math.pi:
		alfa=2*math.pi+alfa
	if alfa>math.pi or alfa<-math.pi:
		return 0.0
	return alfa

def pathAsGeojson(path) -> object:
	feats = []
	route = []

	for order, wayp in enumerate(path):
		feat = {
			"type": "Feature",
			"id": order,
			"geometry": {
				"type": "Point",
				"coordinates": [ # longitude, latitude
					wayp.pos[1],
					wayp.pos[0]
				]
			},
			"properties": {
				"timestamp": str(wayp.time),
				"twd": math.degrees(wayp.twd),
				"tws": wayp.tws,
				"knots": wayp.speed,
				"heading": wayp.brg
			}
		}
		feats.append(feat)
		route.append([wayp.pos[1], wayp.pos[0]]) # longitude, latitude

	feats.append({
		"type": "Feature",
		"id": 999,
		"geometry": {
			"type": "LineString",
			"coordinates": route
		},
		"properties": {
			"start-timestamp": str(path[0].time),
			"end-timestamp": str(path[-1].time)
		}
	})

	return {
		"type": "FeatureCollection",
		"features": feats
	}


"""Vehicles Routing Problem (VRP) by google.

https://developers.google.com/optimization/routing/vrp"""

from ortools.constraint_solver import routing_enums_pb2
from ortools.constraint_solver import pywrapcp
import flora
import network_operations as net_ops
import input_output_operations as io_ops
import pandas as pd
import pdb
import optops

def create_data_model():
    data = {}
    data['distance_matrix'] = []
    data['num_vehicles'] = 4
    data['depot'] = 0
    return data

def convert_list_data_to_ints(a_list):
    """ Method to get a list of floats and to return them as ints.

    Args:
        a_list (list): list of floats
    return:
        list of ints
    """
    int_list = [[int(float(j)) for j in i] for i in a_list]
    return int_list

def print_results_to_map(or_result, data_csvfile):
    """Method to read VRP result and original data file and to print
    the result to map

    Args:
        or_result (str): text with VRP result
        data_csvfile (str): filepath of original data position in disk
    """
    # read data from disk
    supermarkets = pd.read_csv(data_csvfile, delimiter='\t')
    result = io_ops.get_route_node_ids_from_textfile(or_result)
    # update dataframes with results
    pdb.set_trace()
    add_route_to_df(supermarkets, result)

def add_route_to_df(df, routes, new_col='routes'):
    """Method to add a new column (route) to dataframe and
    the corresponding nodes of the route.

    Args:
        df (dataframe): Dataframe to work on (add new column)
        routes (dict): dictionary of <route_id: nodes_list> pairs
        new_col (str, optional): Name of the new column. Defaults to 'routes'.
    """
    # add new column to dataframe
    df[new_col] = -1
    # populate new column with data
    for route in routes:
        _populate_df_with_route(routes, route, df)

def _populate_df_with_route(routes_dict, route_id, df):
    """Method to populate a dataframe with routes

    Args:
        routes_dict ([type]): [description]
        route_id ([type]): [description]
        df ([type]): [description]
    """
    pass

def print_solution(data, manager, routing, solution):
    """Prints solution on console."""
    max_route_distance = 0
    written_result = ''
    for vehicle_id in range(data['num_vehicles']):
        index = routing.Start(vehicle_id)
        plan_output = 'Route for vehicle {}:\n'.format(vehicle_id)
        route_distance = 0
        while not routing.IsEnd(index):
            plan_output += ' {} -> '.format(manager.IndexToNode(index))
            previous_index = index
            index = solution.Value(routing.NextVar(index))
            route_distance += routing.GetArcCostForVehicle(
                previous_index, index, vehicle_id)
        written_result += plan_output
        plan_output += '{}\n'.format(manager.IndexToNode(index))
        plan_output += 'Distance of the route: {}m\n'.format(route_distance)
        print(plan_output)
        max_route_distance = max(route_distance, max_route_distance)
    print('Maximum of the route distances: {}m'.format(max_route_distance))
    return written_result

def main():
    """Solve the CVRP problem."""
    # Instantiate the data problem.
    #data = flora.flora('results/graphs/attica-graph.graphml', 'data/supermarkets.csv', '') #create_data_model()
    data_csvfile = 'data/super_sample_tabs.csv'
    data = create_data_model()
    od_result = net_ops.compute_distance_matrix(data_csvfile)
    data['distance_matrix'] = od_result[0].values.tolist()
    data['distance_matrix'] = convert_list_data_to_ints(data['distance_matrix'])
    #pdb.set_trace()
    # following line is for kasselouris od matrix
    #io_ops.write_od_to_csv(data['distance_matrix'], 'data/kassel_results.csv')

    # Create the routing index manager.
    manager = pywrapcp.RoutingIndexManager(len(data['distance_matrix']),
                                           data['num_vehicles'], data['depot'])

    # Create Routing Model.
    routing = pywrapcp.RoutingModel(manager)

    # Create and register a transit callback.
    def distance_callback(from_index, to_index):
        """Returns the distance between the two nodes."""
        # Convert from routing variable Index to distance matrix NodeIndex.
        from_node = manager.IndexToNode(from_index)
        to_node = manager.IndexToNode(to_index)
        return data['distance_matrix'][from_node][to_node]

    transit_callback_index = routing.RegisterTransitCallback(distance_callback)

    # Define cost of each arc.
    routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)

    # Add Distance constraint.
    dimension_name = 'Distance'
    routing.AddDimension(
        transit_callback_index,
        0,  # no slack
        20000,  # vehicle maximum travel distance
        True,  # start cumul to zero
        dimension_name)
    distance_dimension = routing.GetDimensionOrDie(dimension_name)
    distance_dimension.SetGlobalSpanCostCoefficient(100000)

    # Setting first solution heuristic.
    search_parameters = pywrapcp.DefaultRoutingSearchParameters()
    search_parameters.first_solution_strategy = (
        routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)

    # Solve the problem.
    solution = routing.SolveWithParameters(search_parameters)

    # Print solution on console.
    written_solution = ''
    or_result = 'results/or_results.txt'
    solution_paths = optops.get_all_routes(data, manager, routing, solution)
    if solution:
        written_solution = print_solution(data, manager, routing, solution)
        with open(or_result, 'w') as f:
            f.write(written_solution)
    pdb.set_trace()
    # process the results and print them to map
    print_results_to_map(or_result, data_csvfile)

if __name__ == '__main__':
    main()


# this is the hook for the trainer.
import typing as t
from contextlib import contextmanager

from loguru import logger
from torch import nn

# _Base = Trainer
# else:
#     _Base = object
from contrastyou.hooks import TrainerHook

if t.TYPE_CHECKING:
    from contrastyou.trainer.base import Trainer

    _Base = Trainer
else:
    _Base = object

class HookMixin(_Base):
    def __init__(self, **kwargs) -> None:
        super().__init__(**kwargs)
        self._hooks = nn.ModuleList()

    @contextmanager
    def register_hook(self, *hook: "TrainerHook"):
        if self._initialized:
            raise RuntimeError("`register_hook must be called before `init()``")
        for h in hook:
            self._hooks.append(h)
            h.to(self.device)  # put the hook into device.
            h.register_trainer(self)

        logger.trace("bind TrainerHooks")

        for h in self._hooks:
            h.after_initialize()
        yield
        for h in hook:
            h.close()
        logger.trace("close TrainerHooks")


class Solution:
    def reverseVowels(self, s: str) -> str:
        vowels = {"a", "e", "i", "o", "u", "A", "E", "I", "O", "U"}

        left, right = 0, len(s) - 1
        charArray = list()
        for c in s:
            charArray.append(c)

        while left < right:
            while left < right and s[left] not in vowels:
                left += 1
            while left < right and s[right] not in vowels:
                right -= 1
            charArray[left], charArray[right] = charArray[right], charArray[left]
            left += 1
            right -= 1
        return "".join(charArray)

345.Reverse vowels of a string/reverse_vowels.py


import sys

log_file = sys.argv[1]
file_prev = ""
tags = tuple(sys.argv[2].split(","))

def str_diff(str1:str,str2:str) -> str:
    return str1[len(str2):]

while 1:
    try:
        new_log = ""
        with open("logs/" + log_file,"r") as f:
            file_current = f.read()
        if file_prev != file_current:
            for log in str_diff(file_current,file_prev).split("\n"):
                for tag in tags:
                    if log.startswith("[{}]".format(tag)):
                            new_log += log + "\n"
        print(new_log, end="")
        file_prev = file_current
    except KeyboardInterrupt:
        print("Interrupt detected, quitting!")
        exit(0)
        break


from typing import Tuple

import torch

from .base_trainer import BaseTrainer
from .utils import get_batch_tensors, get_dict

class StandardTrainer(BaseTrainer):

    def __init__(self, model, optimizer, loss, metrics=None,
                 x_device=None, x_type=torch.float, y_device=None, y_type=torch.long):
        """
        :param model: torch module
        :param optimizer: torch optimizer
        :param loss: loss function with signature function(preds, trues)
        :param metrics: list of metric functions with signature function(preds, trues)
        :param x_device: device to put inputs
        :param x_type: type to cast inputs
        :param y_device: device to put labels
        :param y_type: type to cast labels
        """
        super().__init__([model], [optimizer], loss, metrics)

        self.x_device = x_device
        self.x_type = x_type
        self.y_device = y_device
        self.y_type = y_type

        self.training = True

    def _extract_data(self, batch_data):
        return get_batch_tensors(batch_data, self.x_type, self.x_device, self.y_type, self.y_device)

    def _train_one_batch(self, inputs, targets) -> Tuple[dict, object]:
        if isinstance(inputs, dict):
            preds = self.models[0](**inputs)
        elif isinstance(inputs, list):
            preds = self.models[0](*inputs)
        else:
            preds = self.models[0](inputs)

        loss = self.loss(preds, targets)

        if isinstance(loss, dict):
            loss_dict = {k: float(loss[k]) for k in loss}
            loss = loss["Loss"]
        else:
            loss_dict = {"Loss": float(loss)}

        loss.backward()
        self.optimizers[0].step()

        return loss_dict, preds

    def _get_train_measures(self, inputs, targets, loss_dict, cache) -> dict:
        preds = cache

        measures = loss_dict
        for m in self.metrics:
            m_value = m(preds, targets)
            m_dict = get_dict(m_value, name=m._get_name())
            measures.update(m_dict)

        return measures

    def _get_eval_cache(self, inputs, targets):
        if isinstance(inputs, dict):
            preds = self.models[0](**inputs)
        elif isinstance(inputs, list):
            preds = self.models[0](*inputs)
        else:
            preds = self.models[0](inputs)

        return preds, targets

    def _get_eval_logs(self, eval_caches):
        preds, trues = eval_caches

        loss = self.loss(preds, trues)
        loss_dict = get_dict(loss, prefix="Val_", name="Loss")

        measures = loss_dict
        for m in self.metrics:
            m_value = m(preds, trues)
            m_dict = get_dict(m_value, prefix="val ", name=m._get_name())
            measures.update(m_dict)

        return measures

    def extra_repr(self):
        return f"x_device={self.x_device}, x_type={self.x_type}, " \
               f"y_device={self.y_device}, y_type={self.y_type}"


#!/usr/bin/python3
# coding=utf-8
import sys
sys.path.append("../../")
import tensorflow as tf
import time
from input_data import Load_data
from model import QueryModel
from model import Decoder
from model import Hidden_init
from metric import *
from sklearn.model_selection import LeaveOneOut, KFold

import sys
import random

cities = [
'osaka',
#'glas',
#'edin',
#'toro',
#'melb',
#'weeplaces_poi_25_length_3-15-numtraj_765',
#'weeplaces_poi_50_length_3-15-numtraj_2134',
#'weeplaces_poi_100_length_3-15-numtraj_4497',
#'weeplaces_poi_200_length_3-15-numtraj_7790',
#'weeplaces_poi_400_length_3-15-numtraj_12288',
]
if len(sys.argv) > 1:
    id = int(sys.argv[1]) % len(cities)
else:
    id = random.randint(0, len(cities) - 1)
city = cities[id]
# city = 'Glas'
# city = 'Edin'
# city = 'Toro'
tf.keras.backend.set_floatx('float64')# float desgin

pretrain_batch_size = 32
batch_size = 32
k = 256
dec_units = 256

data = Load_data(city)
poi_embedding, poi_size = data.self_embedding()
data_size = data.data_size()

query = QueryModel(poi_embedding, k)
decoder = Decoder(poi_embedding, poi_size, dec_units)
h_state = Hidden_init(dec_units)

loss_object = tf.keras.losses.SparseCategoricalCrossentropy(
    from_logits=True, reduction='none')

def loss_function(real, pred, real2, pred2):
    mask = tf.math.logical_not(tf.math.equal(real, 0))
    loss_ = loss_object(real, pred)
    mask = tf.cast(mask, dtype=loss_.dtype)
    loss_ *= mask

    loss = loss_object(real2, pred2)

    return tf.reduce_mean(loss_) + 1 * tf.reduce_mean(loss)

def pre_loss_function(sim):
    real = np.eye(pretrain_batch_size)
    loss = tf.keras.losses.categorical_crossentropy(real, sim)

    return tf.reduce_mean(loss)

# --------------------------- pre-train_step ------------------------------
def pre_train_step(pre_que, sample1, sample2, lr=0.0005):
    pre_loss = 0
    optimizer = tf.keras.optimizers.Adam(lr=lr)

    with tf.GradientTape() as tape:
        query_out = query(pre_que)
        dec_input1 = sample1[:, 0]
        dec_input2 = sample2[:, 0]
        dec_hidden1 = h_state(query_out)
        dec_hidden2 = h_state(query_out)
        for t in range(1, sample1.shape[1]):
            output1, dec_hidden1 = decoder.pre_train(dec_input1, query_out, dec_hidden1)
            output2, dec_hidden2 = decoder.pre_train(dec_input2, query_out, dec_hidden2)
            dec_input1 = sample1[:, t]
            dec_input2 = sample2[:, t]
        sim = tf.matmul(output1, tf.transpose(output2))
        sim = tf.math.softmax(sim)
        pre_loss += pre_loss_function(sim)

    batch_loss = pre_loss
    variables = decoder.trainable_variables + query.trainable_variables + h_state.trainable_variables
    gradients = tape.gradient(pre_loss, variables)
    optimizer.apply_gradients(zip(gradients, variables))
    return batch_loss

exps = tf.keras.optimizers.schedules.ExponentialDecay(
                                                    0.1,
                                                    decay_steps=5,
                                                    decay_rate=0.9,
                                                    staircase=False)

# --------------------------- train_step ---------------------------------
def train_step(que, traj,lr = 0.1):
    loss = 0
    optimizer = tf.keras.optimizers.Adam(lr=lr)

    with tf.GradientTape() as tape:
        query_out = query(que)
        dec_input = traj[:, 0]
        dec_hidden=h_state(query_out)

        for t in range(1, traj.shape[1]):
            predictions, predictions2, dec_hidden = decoder(dec_input, query_out, dec_hidden)
            loss += loss_function(traj[:, t], predictions, que[:, 2], predictions2)
            dec_input = tf.argmax(tf.nn.softmax(predictions), 1)

    batch_loss = loss
    variables = query.trainable_variables + decoder.trainable_variables+h_state.trainable_variables
    gradients = tape.gradient(loss, variables)
    optimizer.apply_gradients(zip(gradients, variables))
    return batch_loss

# ------------------------------- evaluate ------------------------------
def evaluate(que, traj):
    predict_traj = []
    realnum_poi = 0
    query_out = query(que)
    dec_input = traj[:, 0]

    for poi in tf.squeeze(traj):
        if (poi == 0):
            break
        realnum_poi += 1
    realnum_poi = realnum_poi - 2

    start_poi = traj[:, 0]
    start_poi = tf.cast(start_poi,dtype=tf.int32)
    end_poi = traj[:, realnum_poi + 1]
    predict_traj.append(start_poi)
    dec_hidden = h_state(query_out)
    table = np.ones([poi_size],dtype=np.float64)

    table[start_poi.numpy()] = 0.
    table[end_poi.numpy()] = 0.
    table[0] = 0.

    for t in range(realnum_poi):
        decoder.set_dropout()
        predictions, _, dec_hidden = decoder(dec_input, query_out,dec_hidden)
        mask = tf.expand_dims(table,axis=0)
        dec_input = tf.argmax(tf.nn.softmax(predictions * mask), 1)
        predict_traj.append(dec_input)

    predict_traj.append(end_poi)
    real_traj = tf.squeeze(traj)[0:realnum_poi + 1].numpy()
    real_traj = np.append(real_traj, end_poi)
    predict_traj = [i.numpy().tolist() for i in predict_traj]
    predict_traj = [i[0] for i in predict_traj]

    return real_traj, predict_traj

if __name__ == '__main__':
    import pandas as pd
    import datetime
    from BERT_Trip.util import evaluate_results, save_results
    total_test_f1 = []
    total_test_pairs_f1 = []

    max_f1 = []
    max_pf1 = []
    fold = 0
    df = pd.read_csv('./train_data/'+city+'-query.csv', header = None, sep= ' ', names = ['startPOI', 'startTime', 'endPOI', 'endTime'])
    seed = int(datetime.datetime.now().timestamp())
    random.seed(seed)
    np.random.seed(seed)
    loo = KFold(n_splits = 5, shuffle = True, random_state = seed)
    for train_index, test_index in loo.split(df):
        decoder.reset_variable()
        query.reset_variable()
        h_state.reset_variable()

        pre_dataset_train, _, pre_steps_train, _ = data.load_dataset_kfold(train_index, test_index, pretrain_batch_size)
        # -------------------------- pre-train ----------------------------
        PRE_EPOCHES = 5
        EPOCHS = 20
        avg = []
        for epoch in range(PRE_EPOCHES):
            start = time.time()
            pre_loss = 0
            #print("pretrain epoch", epoch)
            for (batch, (que, traj)) in enumerate(pre_dataset_train.take(pre_steps_train)):
                total_batch_loss = 0
                pre_que, sample1, sample2 = data.load_pretrain_dataset(que, traj)
                pre_batch_loss = pre_train_step(pre_que, sample1, sample2)
                pre_loss += pre_batch_loss
            #print('Epoch {} Loss {:.4f}'.format(epoch + 1, pre_loss / pre_steps_train))
            avg.append(time.time() - start)
            print(f'{city}: Pretain Time taken for 1 epoch {avg[-1]:.3f} sec avg: {np.mean(avg):.3f}')

        # ------------------------------ train -----------------------------------
        dataset_train, dataset_val, steps_train, steps_val = data.load_dataset_kfold(train_index, test_index, pretrain_batch_size)
        start1 = time.time()
        best_test_f1 = -1
        best_result_list = []
        res = {}
        avg = []
        for epoch in range(EPOCHS):
            start = time.time()
            total_loss = 0
            lr = 0.1
            #print(f"train epoch: {epoch}")
            for (batch, (que, traj)) in enumerate(dataset_train.take(steps_train)):
                batch_loss = train_step(que, traj, lr)
                total_loss += batch_loss

            avg.append(time.time() - start)
            print(f'{city}: Time taken for 1 epoch {avg[-1]:.3f} sec avg: {np.mean(avg):.3f}')
            result_list = []
            for (batch, (que, traj)) in enumerate(dataset_val.take(steps_val)):
                for i in range(len(traj)):
                    traj1 = traj[i].numpy()
                    indexs = np.where(traj1 == 0)
                    traj1 = np.delete(traj1,indexs)
                    traj1 = tf.convert_to_tensor(traj1)
                    que1 = tf.expand_dims(que[i], 0)
                    traj1 = tf.expand_dims(traj1, 0)
                    real_traj, predict_traj = evaluate(que1, traj1)

                    result_list.append({'expected': real_traj.tolist(), 'predict': predict_traj})
            e = evaluate_results(result_list)['f1']
            if e > best_test_f1:
                best_test_f1 = e
                best_result_list = result_list

        save_results(
            dataset = city,
            method = 'SelfTrip',
            train_size = len(train_index),
            test_size =  len(test_index),
            fold = fold,
            seed = seed,
            results = result_list,
        )
        print("best f1: ", best_test_f1)
        print('\n--' * 2 + 'Time take {} sec'.format(time.time() - start1))
        print('---- finish' + ' index = ' + str(fold) + '-' * 5 + '\n')
        fold = fold + 1


import sys
n = int(input())

arr = list(map(int, sys.stdin.readline().split()))

dp = [1 for _ in range(len(arr))]

for i in range(1, len(arr)):

    if i==1:
        if arr[i]>arr[i-1]:
            dp[i]=2
        continue

    cur = arr[i]
    l, r = 0, i-1

    while l<=r:

        mid = (l+r)//2

        if mid<cur:
            dp[i]= max(dp[i], dp[mid])


print('汉诺塔（Hanoi）')
n = input('请输入盘子数：')
A = input('请输入第一个柱子名称：')
B = input('请输入第二个柱子名称：')
C = input('请输入第三个柱子名称：')
X = []
def move(n, a, b, c):
    if n == 1:
        step = a + str(n) + '-->' + c + str(n)
        X.append(step)
        return
    move(n-1, a, c, b)
    step = a + str(n) + '-->' + c + str(n)
    X.append(step)
    #move(1, a, b, c)
    move(n-1, b, a, c)

move(int(n), str(A), str(B), str(C))
print('共需操作'+str(len(X))+'次\n操作过程为\n',X)


from sklearn.datasets import load_iris
iris=load_iris()
'''
print (type(iris))
#print (iris.data)
print(iris.feature_names)
print(iris.target)
print(iris.target_names)
print(iris.data.shape)
print (iris.target.shape)
'''

X=iris.data
y=iris.target
#print(X.shape)
#print(y.shape)

##########################################

from sklearn.neighbors import KNeighborsClassifier
knn=KNeighborsClassifier(n_neighbors=1)

knn.fit(X,y)

X_new=[[3,5,4,2],[5,4,3,2]]
Fl = knn.predict(X_new)
print ('Pridicted Flower type are :- ')
print (iris['target_names'][Fl])

##########################################

from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(iris['data'],iris['target'],test_size=0.3,random_state=0)

knn=KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train,y_train)
y_pred=knn.predict(X_test)

print (y_pred)
print (y_test)


king_init, stone_init, n = input().split()

king_row = 8 - int(king_init[1]) + 1
king_column = int(ord(king_init[0]) - ord('A')) + 1
stone_row = 8 - int(stone_init[1]) + 1
stone_column = int(ord(stone_init[0]) - ord('A')) + 1
n = int(n)

# 명령에 따른 (row, column) 변화
dp = [(0, 1), (0, -1), (1, 0), (-1, 0), (-1, 1), (-1, -1), (1, 1), (1, -1)]
order_list = ['R', 'L', 'B', 'T', 'RT', 'LT', 'RB', 'LB']

for i in range(n):
    next_king_row, next_king_column, next_stone_row, next_stone_column = king_row, king_column, stone_row, stone_column
    order = input()
    for j in range(8):
        if order == order_list[j]:
            next_king_row = king_row + dp[j][0]
            next_king_column = king_column + dp[j][1]
        if (stone_row == next_king_row) and (stone_column == next_king_column):
            next_stone_row = stone_row + dp[j][0]
            next_stone_column = stone_column + dp[j][1]
        if (next_king_row<1 or next_king_column<1 or next_stone_row<1 or next_stone_column<1 or
            next_king_row>8 or next_king_column>8 or next_stone_row>8 or next_stone_column>8):
            next_king_row = king_row
            next_king_column = king_column
            next_stone_row = stone_row
            next_stone_column = stone_column
        king_row = next_king_row
        king_column = next_king_column
        stone_row = next_stone_row
        stone_column = next_stone_column

print(chr(king_column - 1 + ord('A')) + str(9 - king_row))
print(chr(stone_column - 1 + ord('A')) + str(9 - stone_row))

# Solution with functions

def get_pos(chess_pos):
    x = ord(chess_pos[0]) - ord('A')
    y = 8 - int(chess_pos[1])
    return x, y

def get_chess_pos(x_pos, y_pos):
    x = chr(x_pos + ord('A'))
    y = str(8 - y_pos)
    return x + y

king_pos, stone_pos, N = input().split()
kingx, kingy = get_pos(king_pos)
stonex, stoney = get_pos(stone_pos)

move_dir = {'R': (1, 0), 'L': (-1, 0), 'B': (0, 1), 'T': (0, -1), 'RT': (1, -1),
            'LT': (-1, -1), 'RB': (1, 1), 'LB': (-1, 1)}

for _ in range(int(N)):
    cmd = input()
    dx, dy = move_dir[cmd]
    king_nx, king_ny = kingx + dx, kingy + dy
    if 0 <= king_nx < 8 and 0 <= king_ny < 8:
        if stonex == king_nx and stoney == king_ny:
            stone_nx, stone_ny = stonex + dx, stoney + dy
            if 0 <= stone_nx < 8 and 0 <= stone_ny < 8:
                stonex, stoney = stone_nx, stone_ny
                kingx, kingy = king_nx, king_ny
        else:
            kingx, kingy = king_nx, king_ny

print(get_chess_pos(kingx, kingy))
print(get_chess_pos(stonex, stoney))