# import the necessary packages
import tensorflow.keras.backend as K
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.pyplot import figure

def make_pairs(images, labels):
    # initialize two empty lists to hold the (image, image) pairs and
    # labels to indicate if a pair is positive or negative
    pairImages = []
    pairLabels = []

    # calculate the total number of classes present in the dataset
    # and then build a list of indexes for each class label that
    # provides the indexes for all examples with a given label
    numClasses = len(np.unique(labels))
    idx = [np.where(labels == i)[0] for i in range(0, numClasses)]

    # loop over all images
    for idxA in range(len(images)):
        # grab the current image and label belonging to the current
        # iteration
        currentImage = images[idxA]
        label = labels[idxA]

        # randomly pick an image that belongs to the *same* class
        # label
        idxB = np.random.choice(idx[label])
        posImage = images[idxB]

        # prepare a positive pair and update the images and labels
        # lists, respectively
        pairImages.append([currentImage, posImage])
        pairLabels.append([1])

        # grab the indices for each of the class labels *not* equal to
        # the current label and randomly pick an image corresponding
        # to a label *not* equal to the current label
        negIdx = np.where(labels != label)[0]
        negImage = images[np.random.choice(negIdx)]

        # prepare a negative pair of images and update our lists
        pairImages.append([currentImage, negImage])
        pairLabels.append([0])

    # return a 2-tuple of our image pairs and labels
    return (np.array(pairImages), np.array(pairLabels))

def euclidean_distance(vectors):
    # unpack the vectors into separate lists
    (featsA, featsB) = vectors

    # compute the sum of squared distances between the vectors
    sumSquared = K.sum(K.square(featsA - featsB), axis=1,
        keepdims=True)

    # return the euclidean distance between the vectors
    return K.sqrt(K.maximum(sumSquared, K.epsilon()))

def plot_training(H, plotPath):
    # construct a plot that plots and saves the training history
    plt.style.use("ggplot")
    plt.figure()
    plt.plot(H.history["loss"], label="train_loss")
    plt.plot(H.history["val_loss"], label="val_loss")
    plt.title("Training Loss")
    plt.xlabel("Epoch #")
    plt.ylabel("Loss")
    plt.legend(loc="lower left")
    plt.savefig(plotPath)

def plot_confusion_matrix(cm,
						  plotPath,
                          target_names,
                          title='Confusion matrix',
                          cmap=None,
                          normalize=True):
    """
    A prettier confusion matrix
    """
    import matplotlib.pyplot as plt
    import numpy as np
    import itertools

    cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
    accuracy = np.trace(cm) / np.sum(cm).astype('float')
    misclass = 1 - accuracy

    if cmap is None:
        cmap = plt.get_cmap('Blues')

    plt.figure(figsize=(16, 14))
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()

    if target_names is not None:
        tick_marks = np.arange(len(target_names))
        plt.xticks(tick_marks, target_names, rotation=45)
        plt.yticks(tick_marks, target_names)

    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]

    thresh = cm.max() / 1.5 if normalize else cm.max() / 2
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        if normalize:
            plt.text(j, i, "{:0.4f}".format(cm[i, j]),
                     horizontalalignment="center",
                     color="white" if cm[i, j] > thresh else "black")
        else:
            plt.text(j, i, "{:,}".format(cm[i, j]),
                     horizontalalignment="center",
                     color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label\naccuracy={:0.4f}; misclass={:0.4f}'.format(accuracy, misclass))
    plt.savefig(plotPath)


import numpy as np

__author__ = 'roeiherz'

"""
Write an algorithm to print all ways of arranging 8 queens on 8x8 chess board so that none of them share the same
row, column or diagonal. In this case, "diagonal" means all diagonals, not just the two that bisect the board.
"""

N = 5

def is_legal(mat, i, j):

    if i >= N or j >= N:
        return False

    # Row or column are not allowed
    if mat[i, :].sum() >= 1 or mat[:, j].sum() >= 1:
        return False

    # Diagonal is not allowed
    for ii in range(N):
        if i - ii >= 0 and j - ii >= 0 and mat[i - ii, j - ii] == 1:
            return False
        if i + ii < N and j + ii < N and mat[i + ii, j + ii] == 1:
            return False
        if i - ii >= 0 and j + ii < N and mat[i - ii, j + ii] == 1:
            return False
        if i + ii < N and j - ii >= 0 and mat[i + ii, j - ii] == 1:
            return False

    return True

def chess(mat, col=0):
    # Stop case
    if col >= N:
        return True

    for row in range(N):
        if is_legal(mat, row, col):
            mat[row, col] = 1
            if chess(mat, col + 1):
                return True
            # If placing queen in mat doesn't lead to a solution then remove queen
            mat[row, col] = 0

    return False

if __name__ == '__main__':
    mat = np.zeros((N, N))
    chess(mat, col=0)
    print(mat)


"""
https://leetcode.com/problems/jump-game-iii/

Given an array of non-negative integers arr, you are initially positioned at start index of the array. When you are at index i, you can jump to i + arr[i] or i - arr[i], check if you can reach to any index with value 0.

Notice that you can not jump outside of the array at any time.

EXAMPLE:
    Input: arr = [4,2,3,0,3,1,2], start = 5
    Output: true
    Explanation:
    All possible ways to reach at index 3 with value 0 are:
    index 5 -> index 4 -> index 1 -> index 3
    index 5 -> index 6 -> index 4 -> index 1 -> index 3
"""

class Solution:
    def canReach(self, arr: List[int], start: int) -> bool:

        visited = set()
        n = len(arr)
        queue = deque()
        queue += [start]
        while queue:
            index = queue.popleft()
            if index not in visited and index >= 0 and index < n:
                visited.add(index)
                if arr[index] == 0:
                    return True
                queue += [index - arr[index], index + arr[index]]
        return False

        #DFS Approach
        """
        self.visited = set()
        def jump(index, n):
            if index in self.visited or index < 0 or index >= n:
                return False
            self.visited.add(index)
            if arr[index] == 0:
                return True
            print(index, index - arr[index], index + arr[index])
            return jump(index - arr[index], n) or jump(index + arr[index], n)

        return jump(start, len(arr))
        """


from scapy.all import *

def spoof_pkt(pkt):
    if ICMP in pkt and pkt[ICMP].type == 8:
        print("Original:")
        print("Source=", pkt[IP].src)
        print("Destination=", pkt[IP].dst)
        a = IP()
        a.src = pkt[IP].dst
        a.dst = pkt[IP].src
        a.ihl = pkt[IP].ihl
        a.ttl = 15

        b = ICMP(type=0,id=pkt[ICMP].id,seq =pkt[ICMP].seq)

        if pkt.haslayer(Raw):
            data = pkt[Raw].load
            spoof = a/b/data
        else:
            spoof = a/b
        print(" Spoofing.. :) ")
        print("NewSrc=" ,a.src)
        print("NewDst=",a.dst)
        send(spoof,verbose=0)

print("~~ Sniffing.. ~~ ")
f = 'icmp and src host 10.0.2.15'
sniff(filter= f,prn=spoof_pkt)


# coding=utf-8

"""Project level settings."""
from .project import *  # noqa
import os

if 'SENTRY_KEY' in os.environ:
    SENTRY_KEY = os.environ['SENTRY_KEY']
    import sentry_sdk
    from sentry_sdk.integrations.django import DjangoIntegration
    sentry_sdk.init(
        dsn=SENTRY_KEY,
        integrations=[DjangoIntegration()],
        send_default_pii=True
    )

# Hosts/domain names that are valid for this site; required if DEBUG is False
# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
# Localhost:9000 for vagrant
# Changes for live site
# ['*'] for testing but not for production

ALLOWED_HOSTS = [
    'localhost:8080',
    'ford3.kartoza.com']

# Pipeline - for production we want to compress resources
# PIPELINE['JS_COMPRESSOR'] = 'pipeline.compressors.yuglify.YuglifyCompressor'
# PIPELINE['CSS_COMPRESSOR'] = 'pipeline.compressors.yuglify.YuglifyCompressor'
PIPELINE['PIPELINE_ENABLED'] = False

# Comment if you are not running behind proxy
USE_X_FORWARDED_HOST = True

# Set debug to false for production
DEBUG = TEMPLATE_DEBUG = False

SERVER_EMAIL = 'no_reply@openedu.org.za'

DEFAULT_FROM_EMAIL = 'no_reply@openedu.org.za'


import json

with open('data.json', 'r',encoding='utf-8') as f:
  data_str = f.read()
data = json.loads(data_str)

def get_participle(subject):
  participle = None
  if "We" in subject or "You" in subject or "They" in subject:
    participle = "are"
  elif "He" in subject or "She" in subject:
    participle = "is"
  elif subject == "I":
    participle = "am"
  return participle

def valid_sentences():
  sentences = []
  for subject in data ["subjects_english"]:
    for verb in data["verbs"]:
      # past
      english = f"{subject} {verb['english_past']}"
      māori = f"I {verb['māori']} {data['subjects_dict'][str.lower(subject)]}"
      sentences.append(f"{english}, {māori}")

      # present
      english = f"{subject} {get_participle(subject)} {verb['english_present']}"
      māori = f"Kei te {verb['māori']} {data['subjects_dict'][str.lower(subject)]}"
      sentences.append(f"{english}, {māori}")

      # future
      english = f"{subject} {verb['english_future']}"
      māori = f"Ka {verb['māori']} {data['subjects_dict'][str.lower(subject)]}"
      sentences.append(f"{english}, {māori}")
  return sentences

def main():
  for sentence in valid_sentences():
    print(sentence)

if __name__ == "__main__":
  main()


from django.test import Client, TestCase
from ..models import Disaster, Continent, Region, Type

class DisasterModelTest(TestCase):

    fixtures = ['disaster.json']

    # 1
    def test_disaster(self):
        disaster = Disaster.objects.get(disasterNo="2020-0055-WEF")
        self.assertEqual(disaster.year, 2020)
        disasters =Disaster.objects.all()
        self.assertEqual(disasters.count(), 1)

    # 2
    def test_continent(self):
        continent = Continent.objects.get(pk="Asia")
        self.assertEqual(continent.id, "Asia")
        continents =Continent.objects.all()
        self.assertEqual(continents.count(), 1)

    # 3
    def test_region(self):
        region = Region.objects.get(pk="SouthernAsia")
        self.assertEqual(region.continent.id, "Asia") # assert continent object
        regions = Region.objects.all()
        self.assertEqual(regions.count(), 1)

    # 4
    def test_type(self):
        disaster_type = Type.objects.get(pk="Flood")
        self.assertEqual(disaster_type.id, "Flood")
        types = Type.objects.all()
        self.assertEqual(types.count(), 1)


import optimizers
from metrics import Loss
class Sequential:
    def __init__(self):
        self.stack = []
        self.optimizer_type = None
        self.loss_type = []
    def add(self, layers):
        self.stack.append(layers)
    def predict(network, input):
        output = input
        for layer in network:
            output = layer.forward(output)
        return output
    def train(self,  X, Y, loss, epochs=20, lr=0.01):
        loss, epoch, history = [], [], []
        cost, cost_prime = Loss.mse, Loss.mse_prime
        if loss == "binary_cross_entropy":
            cost = Loss.binary_cross_entropy
            cost_prime = Loss.binary_cross_entropy_prime
        for i in range(epochs):
            error = 0
            for x, y in zip(X, Y):
                #forward
                output = Sequential.predict(self.stack, x)
                #print(output)
                #calculate the error
                error += cost(y, output)
                loss.append(error)
                #print(error)
                #calculate the backprop
                grad = cost_prime(y, output)
                for layer in reversed(self.stack):
                    grad = layer.backward(grad, lr)
            epoch.append(i+1)
            error /= len(X)
            print(f"{i + 1}/{epochs}, error={error}")
        history.append(epoch)
        history.append(loss)
        return history


# -*- coding: utf-8 -*-
#
# @lc app=leetcode.cn id=1 lang=python3
#

# [1]
#

# @lc code=start
class Solution:
    # @return a tuple, (index1, index2)
    def twoSum(self, num, target):
        dic = {}
        for i in range(len(num)):
            if num[i] not in dic:
                dic[target - num[i]] = i
            else:
                return dic[num[i]], i

        return -1, -1

# @lc code=end


#!env python

import io
import urllib
import smtplib
import argparse
import numpy as np
import pandas as pd
import yahoo_fin.stock_info as yfsi
import matplotlib.pyplot as plt

parser = argparse.ArgumentParser(usage='Inquire the price for stock, need the stock ID argument')
parser.add_argument('--sid', '-s', type = str, help = 'stock ID')
args = parser.parse_args()
sid = args.sid
ctx = ''
ndone = 0

sh = 'http://query.sse.com.cn/security/stock/downloadStockListFile.do?csrcCode=&stockCode=&areaName=&stockType=1'
sz = 'http://www.szse.cn/api/report/ShowReport?SHOWTYPE=xlsx&CATALOGID=1110&TABKEY=tab1&random=0.4178381198138461'

def parse1ashare(sid, p2s = False):
	global ndone
	if isinstance(sid, int) or isinstance(sid, np.int64):
		if sid < 10:
			sid = '00000' + str(sid) + '.SZ'
		elif sid < 100:
			sid = '0000' + str(sid) + '.SZ'
		elif sid < 1000:
			sid = '000' + str(sid) + '.SZ'
		elif sid < 10000:
			sid = '00' + str(sid) + '.SZ'
		elif sid >= 300000 and sid < 600000:
			sid = str(sid) + '.SZ'
		elif sid >= 600000:
			sid = str(sid) + '.SS'

	sdc = yfsi.get_data(sid, start_date='01/01/2012')[20:]
	if len(sdc) < 90:
		#print(f'{sid} too short history data')
		ndone += 1
		return
	del sdc['ticker']
	sdc = sdc.fillna(method = 'ffill')
	pprice = sdc['adjclose']
	t = sdc['open'] - sdc['close']
	pdiff = (t / t.max())
	pdiff[pdiff >= 0.45] = pprice.max()
	pdiff[pdiff < 0.45] = 0
	t = sdc['volume'] * ((sdc['close'] + sdc['open'] + sdc['high'] + sdc['low']) / 4)
	pvol = (t / t.max())
	pvol[pvol >= 0.45] = pprice.max() / 2
	pvol[pvol < 0.45] = 0

	tsd = pd.DataFrame(pprice)
	tsd['diff'] = pdiff
	tsd['vol'] = pvol
	sigdif = 0
	sigvol = 0
	if len(pdiff) > 0 and pdiff[-1] > pprice.max() / 2:
		sigdif = 1
	if len(pvol) > 0 and pvol[-1] > pprice.max() / 3:
		sigvol = 1
	if sigdif or sigvol:
		print(f'-----------\n{sid} signal diff={sigdif} vol={sigvol}')
		global ctx
		ctx += f'-----------\n{sid} signal diff={sigdif} vol={sigvol}\n'
	if p2s:
		tsd.plot()
		plt.title(f'{sid}')
		plt.show()
	ndone += 1

if sid and isinstance(sid, str) and len(sid) > 0:
	parse1ashare(sid, True)
else:
	shheader = {
		'X-Requested-With': 'XMLHttpRequest',
		'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
		'Referer': 'http://www.sse.com.cn/assortment/stock/list/share/'
	}
	req = urllib.request.Request(sh, headers = shheader)
	resp = urllib.request.urlopen(req)
	if not resp:
		print(f'Cannot open stock list on {sh}')
		exit(1)
	r = resp.read().decode('gb2312')
	if not r:
		print('Unknown char encoding on SH')
		exit(2)
	t = io.StringIO(r)
	a = pd.read_csv(t, sep = '\t')
	if a.empty or len(a.iloc[:, 0]) == 0 or a.iloc[0, 0] != 600000:
		print('Unexpected format, maybe SH website is changed')
		exit(3)
	sids = pd.Series(a.iloc[:, 0])

	urllib.request.urlretrieve(sz, 'sz.xlsx')
	b = pd.read_excel('sz.xlsx')
	if not r:
		print('Cannot open stock lsit file on SZ')
		exit(4)
	if b.empty or len(b.iloc[:, 4]) == 0 or b.iloc[0, 4] != 1:
		print('Unexpected format, maybe SZ website is changed')
		exit(5)

	sids = sids.append(pd.Series(b.iloc[:, 4]), ignore_index = True, verify_integrity = True)

	sids.apply(parse1ashare)
	print(len(sids))

	while (ndone < len(sids)):
		sleep(5)

	if len(ctx) > 0:
		print('sending email...')
		s = smtplib.SMTP(host = 'smtp.qq.com', port = 587)
		s.starttls()
		s.login('3168681165@qq.com', 'bsnidiijpefhdgbj')
		s.sendmail('3168681165@qq.com', ['3168681165@qq.com'], f'Subject: daily result\n\n{ctx}')
		s.quit()


# -*- coding: utf-8 -*-
"""
Created on Sat Nov 30 11:34:57 2019

Using user input, identifies headwords that match other headwords
as variants of a single headword.

Outputs dataframe containing solely entry ids and variant types indicating
relations.

@author: Mark
"""

import pandas as pd
import json
from os import path
from shutil import copyfile
from PandasExcel import add_bom, remove_bom
from ast import literal_eval

in_file  = 'headword_matches_unprocessed.csv'
out_file = 'headword_matches_processed.csv'

def main():
    write_backup(in_file)
    write_backup(out_file)

    global in_df, out_df
    in_df =  pd.read_csv(in_file, header=0)
    out_df = pd.read_csv(out_file, header=0)

    # read col 'matches' as native python types
    in_df.loc[:, 'matches'] = [literal_eval(row) for row in in_df['matches']]

    remove_bom(in_df)
    remove_bom(out_df)

    order_matches()

    print(in_df)
    print(out_df)

    write_result(in_df,     in_file)
    write_result(out_df,    out_file)

# iterates thru matches data
# asks user to decide how to structure entries
# deletes all entries that have been processed from in_df
def order_matches():
    copy = in_df.copy()
    for index, row in copy.iterrows():
        if row['entry_id'] not in list( in_df['entry_id'] ):
            continue
        if not row['matches']:
            continue
        ordered, ids = get_order(row)
        if not ordered:
            # 'esc' was passed as input
            # print dfs as they are
            return
        edit_dfs(ordered, ids)

# prints entry id and headword form of all entries referenced in row
# asks user to specify which form is the main entry, which are the variants
# and what variant types they are
def get_order(row):
    ids = [row['entry_id']] + [m[1] for m in row['matches']]
    headwords = [row['headword']] + [m[0] for m in row['matches']]
    json_dicts = [get_json_dict(i) for i in ids]
    glosses = [get_gloss(j) for j in json_dicts]
    bibs    = [get_bib(j)   for j in json_dicts]
    dates   = [j['date']  for j in json_dicts]
    for i, (eid, hdwrd, gls, bib, date) \
    in enumerate( zip(ids, headwords, glosses, bibs, dates) ):
        print(f"{i}: {hdwrd}- {gls}- {bib}- {date}")
    print("How would you like to organize these entries?")
    print("Please input a string of integers indicating your choice "+\
          "for each entry.")
    print("2 to set as headword, 1 to set as variant, 0 to set as unrelated.")
    print("Type 'esc' to leave the program and save progress.")
    print( ''.join( str(i) for i, _ in enumerate(ids) ) , end='' )
    result = input()
    result = validate_result(result, len(ids))
    if result == -1:
        return None, None
    return result, ids

# given result, a string describing hierarchy of entry ids in list ids
# edit in_df and out_df to this data heirarchy
def edit_dfs(result, ids):
    global in_df, out_df

    # delete main entry if ignored, but keep any instances where it appears as
    # a variant later
    if result[0] == '0':
        del_id(ids[0])
        result = result[1:]
        ids = ids[1:]

    # base case, no headword or variants
    # ignore all entries
    if '2' not in result:
        return

    entry_id = ''
    variants = {}
    # iterate thru rest of result, recording data for new entry_id and variants
    for i, res in zip(ids, result):
        # result val of 1 indicates id is variant of entry_id
        if res == '1':
            # ask user to input variant type
            variants[i] = input_var_type(i)
            del_id(i, True)

        # result val of 2 indicates id is entry_id
        elif res == '2':
            assert not entry_id
            entry_id = i
            del_id(i, True)

        else:
            assert res == '0'

    out_df.loc[ len(out_df) ] = {'entry_id':entry_id, 'variants':variants}

def input_var_type(var):
    print(f"What variant type would you like to give {var}?")
    print("Type '1' for Barbosa, '2' for SIL Dict, '3' for Martins, 4 for Weir.")
    print("Or type a custom field in.", end='')
    var_type = input()
    var_type = var_type.strip()

    if var_type == '1':
        return 'Barbosa 2005'
    elif var_type == '2':
        return 'SIL Dict 2011'
    elif var_type == '3':
        return 'Martins 2005'
    elif var_type == '4':
        return 'Weir'

    else:
        return var_type

# function shouldn't be needed, left as a comment in case that changes
"""def id_in_df(eid):
    match_ids = [m[1] for match in in_df['matches'] for m in match]
    result = eid in match_ids or eid in list(in_df['entry_id'])
    if not result:
        print(in_df['entry_id'])
        print(f'id not found {eid}')
    return result"""

# deletes row containing eid in 'entry_id' col
# if matched, delete eid for any instance it occurs matched to another
# entry (in the 'matches' col)
def del_id(eid, matched=False):
    global in_df
    index =  in_df.index[in_df['entry_id'] == eid].tolist()
    assert len(index) <= 1, index
    in_df = in_df.drop(index)

    if matched:
        for i, row in in_df.iterrows():
            matches = row['matches']
            index = [this_id for this_id, m in enumerate(matches) if m[1] == eid]
            if not index:
                continue
            assert len(index) == 1
            index = index[0]
            new_matches = matches.remove( matches[index] )
            if new_matches:
                in_df.loc[i, 'matches'] = new_matches
            else:
                in_df = in_df.drop(i)

# checks if string result fits acceptance conditions
# prompts user for correction until conditions are met
# returns string
def validate_result(result, size):
    if result == 'esc':
        return -1
    while True:
        try:
            assert len(result) == size
            for char in result:
                assert char in ('0', '1', '2')
            assert result.count('2') <= 1
            # can't have variants if no headword is identified
            if '2' not in result:
                assert '1' not in result
            int(result)
            return result
        except (ValueError, AssertionError):
            print("Please type only ints in the range 0-2 in a string of len",\
                  size)
            result = input()

def get_json_dict(eid):
    filehead = eid
    filehead = rep_all(filehead, '/ ', '_')
    filehead = rep_all(filehead, '()[]? ', '')
    filehead = filehead.replace('_=', '=')
    filename = 'entries/' + filehead + '.json'
    while True:
        try:
            with open(filename, 'r', encoding='utf8') as f:
                lemma = json.load(f)
            return lemma
        except FileNotFoundError:
            print(f'Could not find {filename}. Please enter the correctly'+\
                  ' typed json file below:')
            filename = input()

def get_gloss(lemma):
    for sense in lemma['sense']:
        # return gloss if gloss has nonzero value
        if sense['gloss']:
            # gloss is stored as a dictionary w/ language as keys
            # which language is returned is unimportant, so
            # return a random value
            # somehow it's always portuguese
            # lmão
            return sense['gloss'].popitem()[1]
        # return definition if def has nonzero value
        elif sense['def']:
            return sense['def'].popitem()[1]
    # tough luck, no data on lexeme meaning
    else:
        return None

def get_bib(lemma):
    if lemma['note']:
        for note in lemma['note']:
            if note[1] == 'bibliography':
                return note[0]
    return None

def rep_all(s, chars, tgt):
    for c in chars:
        s = s.replace(c, tgt)
    return s

# checks that backup file does not currently exist
# write backup as copy of main
def write_backup(f):
    f_backup = f.split(sep='.')
    f_backup[0] += 'BACKUP'
    f_backup = '.'.join(f_backup)

    assert not path.exists(f_backup), f"Make sure {f} is satisfactory "\
    +f"and then delete {f_backup}"

    copyfile(f, f_backup)

# check that files are not open
# wait for user input if so, then write df
def write_result(df, file):
    while True:
        try:
            add_bom(df)
            df.to_csv(file, encoding='utf8', index=False)
            break
        except:
            input(f"{file} is currently in use by another program. Please "\
                  +"close it so that the results may be saved.")

if __name__ == '__main__':
    main()


import matplotlib.pyplot as plt
import numpy as np
import umap
import torch
from collections import OrderedDict
from torch import nn

def get_invariant_feats(model_feats,model_da, device, test_loader,use_ssl=1,only_ssl=1,no_da=0):
    'no_da: no domain adaptation'
    model_feats.eval()
    model_da.eval()
    test_loader
    alpha = 1
    pred_array = []
    y_lab_array = []
    params = OrderedDict(model_feats.named_parameters())
    #params = params.to(device)
    pred_array = np.asarray(pred_array)
    y_lab_array = np.asarray(y_lab_array)
    with torch.no_grad():
        for data in test_loader:
            x_src = data['x_src'].to(device).to(device)
            y_src = data['y_src'].to(device).to(device)
            if use_ssl:

                x_src_feats = model_feats(x_src, params)
                #x_src_feats = x_src[:,:,:]
                #reps = model(x,params).detach().cpu().numpy().squeeze(0)

                #x_src_feats = model_feats(z, params)
                #x_src_feats = x_src_feats.reshape(x_src.shape[0],-1,x_src_feats.shape[-1])
            else:
                x_src_feats = x_src
            if no_da:
                params_da =  OrderedDict(model_feats.named_parameters())
                invrt_feats = model_da(x_src_feats,params_da )
            elif only_ssl:
                invrt_feats = x_src_feats
            else:
                invrt_feats = model_da.get_invariant_feats(x_src_feats)

    return invrt_feats,y_src
def visualize_reps(model_feats,model_da,device,test_loader,rob_c_idx,rob_b_idx,use_ssl=1,only_ssl=1,src_type ='B',trgt_type='C',no_da=0):
    reducer = umap.UMAP()
    #reducer = TSNE(n_components=2)
    if only_ssl:
        assert use_ssl == 1, "use ssl should be 1 if only ssl is 1"
    reps,y_src = get_invariant_feats(model_feats = model_feats, model_da=model_da,device=device,test_loader=test_loader,
                                     use_ssl=use_ssl,only_ssl=only_ssl)
    #reps = reps[:,:,0:32]
    if use_ssl == 1:
        ssl_sv_str = 'with_ssl'
    else:
        ssl_sv_str = 'without_ssl'
    if only_ssl == 1:
        only_ssl_str = 'with_only_ssl'
    else:
        only_ssl_str = 'without_ssl'

    y_src = y_src.cpu().numpy()
    rob_lbl = np.zeros(y_src.shape)
    rob_lbl[rob_b_idx,:,:] = 1
    y_src2= np.copy(y_src)
    y_src2[rob_b_idx,:,:] = y_src2[rob_b_idx,:,:] + 10
    reps = reps.cpu().numpy().reshape(-1,reps.shape[-1])
    rob_lbl = rob_lbl.reshape(-1,rob_lbl.shape[-1])
    y_src2 = y_src2.reshape(-1,y_src2.shape[-1])
    y_src = y_src.reshape(-1, y_src.shape[-1])
    transformed = reducer.fit_transform(reps)
    save_path_rob = f'figures/DA/rob_lab_{ssl_sv_str}_{only_ssl_str}_src_{src_type}_trgt_{trgt_type}.pdf'
    save_path_terr = f'figures/DA/rob_ter_{ssl_sv_str}_{only_ssl_str}_src_{src_type}_trgt_{trgt_type}.pdf'
    fig1, axs1 = plt.subplots(1, 3, figsize=(15, 5))
    fig2, axs2 = plt.subplots(1, 3, figsize=(15, 5))
    axs1[0].scatter(transformed[:, 0], transformed[:, 1], c=rob_lbl,alpha=0.1)

    axs1[1].scatter(transformed[(rob_lbl == 0).reshape(-1,), 0], transformed[(rob_lbl == 0).reshape(-1,), 1], c='tab:blue', alpha=0.1,label='Robot c')
    axs1[1].legend()
    axs1[2].scatter(transformed[(rob_lbl == 1).reshape(-1,), 0], transformed[(rob_lbl == 1).reshape(-1,), 1], c='tab:orange', alpha=0.1,label='Robot b')
    axs1[2].legend()
    axs2[0].scatter(transformed[:, 0], transformed[:, 1], c=y_src2,
                    label=['b0', 'b1', 'b2', 'b3', 'b4', 'C0', 'C1', 'C2', 'C3', 'C4'], alpha=0.2)
    clr_list = ['tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:brown']
    label_list = ['Stair Down', 'flat', 'Stair up', 'Slope down', 'Slope Up']
    for j in range(0, 5):
        j_idx = np.where(((rob_lbl == 0) & (y_src == j)))[0]
        axs2[1].scatter(transformed[j_idx, 0], transformed[j_idx, 1], c=clr_list[j], label=label_list[j],alpha=0.2)
    for j in range(0, 5):
        j_idx = np.where(((rob_lbl == 1)  &(y_src == j)))[0]
        axs2[2].scatter(transformed[j_idx, 0], transformed[j_idx, 1], c=clr_list[j], label=label_list[j],alpha=0.2)
    axs2[1].set_title("Robot C")
    axs2[2].set_title("Robot B")
    axs2[0].set_title("Combined Robots")
    axs2[2].legend()
    '''
    for i in range(0,reps.shape[0]):
        temp_reps = reps[i, :, :]
        if (i not in rob_c_idx) and ( i not in rob_b_idx) :
            continue
        else:
            transformed = reducer.fit_transform(temp_reps)
        if i in list(rob_b_idx):
            lbl = 'rob_b'
            clr ='b'
            clr_list = ['tab:blue','tab:orange','tab:green','tab:red','tab:brown']
            label_list = ['B1','B2','B3','B4','B5']
        elif i in list(rob_c_idx):
            lbl = 'rob_c'
            clr = 'r'
            clr_list = ['tab:pink' , 'tab:gray', 'tab:olive','tab:cyan','k']
            label_list = ['C1', 'C2', 'C3', 'C4','C5']
        for j in range(0,5):
            j_idx = np.where(y_src[i,:].reshape(-1,) == j)
            axs[0].scatter(transformed[j_idx,0],transformed[j_idx,1],c= clr_list[j],label = label_list[j])
        for k in range(10,15):
            j_idx = np.where(y_src[i, :].reshape(-1, ) == j)
            axs[0].scatter(transformed[j_idx, 0], transformed[j_idx, 1], c=clr_list[j], label=label_list[j])
        axs[1].scatter(transformed[:,0],transformed[:,1],c = clr,label = lbl)
        '''
    fig1.suptitle(f'Robot Type - Src {src_type} Trgt {trgt_type}')
    fig2.suptitle(f'Terrai Type - Src {src_type} Trgt {trgt_type}')
    fig1.savefig(save_path_rob)
    print(save_path_terr)
    return fig2
    #fig2.savefig(save_path_terr)

def visualize_reps_with_labels(reps,y_unlab, y_src,y_prop=None):
    #reps = reps.cpu().numpy().reshape(-1, reps.shape[-1])
    reducer = umap.UMAP()
    y_src = y_src.reshape(-1, )
    transformed = reducer.fit_transform(reps)
    fig1, axs1 = plt.subplots(1, 3, figsize=(15, 5))

    clr_list = ['tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:brown']
    label_list = ['Stair Down', 'flat', 'Stair up', 'Slope down', 'Slope Up']
    for j in range(0, 5):
        j_idx = np.where( y_src == j)[0]
        axs1[1].scatter(transformed[j_idx, 0], transformed[j_idx, 1], c=clr_list[j], label=label_list[j], alpha=0.2)
    for j in range(0, 5):
        j_idx = np.where((y_prop == j))[0]
        axs1[2].scatter(transformed[j_idx, 0], transformed[j_idx, 1], c=clr_list[j], label=label_list[j], alpha=0.2)

    clr_list = ['k','tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:brown',
            ]
    label_list = ['unlabelled','Stair Down', 'flat', 'Stair up', 'Slope down', 'Slope Up',]

    for j in range(-1, 5):
        j_idx = np.where((y_unlab == j))[0]
        axs1[0].scatter(transformed[j_idx, 0], transformed[j_idx, 1], c=clr_list[j+1], label=label_list[j+1], alpha=0.2)
    plt.savefig(f'figures/DA/rob_lab_propagated_vel_b_01')
def plot_ts_reps(model_feats,model_clfr, device, x,y,title='figure',window=50):
    #fig1, axs1 = plt.subplots(4, 1, figsize=(15, 5))
    params = OrderedDict(model_feats.named_parameters())
    model_feats.to(device)
    x = torch.from_numpy(x)[:,:].to(device).unsqueeze(0)

    if window == -1:
        #src_lbl_clfr = model_clfr(x_src_feats)
        reps = model_feats(x,params) #.detach().cpu().numpy().squeeze(0)
        src_lbl_clfr = model_clfr(reps)
        reps = reps.detach().cpu().numpy().squeeze(0)
        src_lbl_clfr_reshaped = src_lbl_clfr.reshape(-1, src_lbl_clfr.shape[-1])
        probs = torch.nn.functional.softmax(src_lbl_clfr_reshaped, dim=1).detach()
        entropy = torch.distributions.Categorical(probs=probs).entropy().cpu().numpy()
        preds = torch.argmax(probs,dim=1).cpu().numpy()
        #reps = x.cpu().numpy().squeeze(0)
    else:
        z = x.squeeze(0).unfold(0, window, window)
        z = z.swapaxes(1, 2)
        reps = model(z, params).detach().cpu()
        reps = reps.reshape(-1,reps.shape[-1])

    fig, ax = plt.subplots(5, 1, sharex=True)
    ax[0].plot(x[0,:,:].detach().cpu().numpy())
    ax[0].title.set_text("Input data")
    ax[1].plot(reps)
    ax[1].title.set_text("Learned Reps")
    ax[2].plot(entropy)
    ax[2].title.set_text("Entropy (soft max)")
    ax[3].plot(preds)
    ax[3].title.set_text("Predicted class")

    ax[4].plot(y)
    ax[4].title.set_text("True class")
    #plt.show()
    plt.tight_layout()
    fig_path = f'figures/DA/rob_lab_1{title}_{str(window)}.pdf'
    #fig.suptitle(title)
    #plt.show()
    return fig
    #fig.savefig(fig_path)


class Node:
    def __init__(self, value):
        self.value = value
        self.next = None

class Queue:
    # 初始化空队列
    def __init__(self):
        head = Node(0)
        self.front = head
        self.rear = head

    # 判断队列是否为空
    def is_empty(self):
        return self.front == self.rear

    # 入队操作
    def enqueue(self, value):
        node = Node(value)
        self.rear.next = node
        self.rear = node

    # 出队操作
    def dequeue(self):
        if self.is_empty():
            raise Exception('Queue is empty')
        else:
            node = self.front.next
            self.front.next = node.next
            if self.rear == node:
                self.rear = self.front
            value = node.value
            del node
            return value

    # 获取队头元素
    def front_value(self):
        if self.is_empty():
            raise Exception('Queue is empty')
        else:
            return self.front.next.value

    # 获取队尾元素
    def rear_value(self):
        if self.is_empty():
            raise Exception('Queue is empty')
        else:
            return self.rear.value

queue = Queue()

queue.enqueue(1)
print(queue.front_value())
print(queue.rear_value())
queue.dequeue()
queue.enqueue(2)
queue.enqueue(3)
queue.dequeue()
print(queue.front_value())
print(queue.rear_value())
queue.dequeue()
print(queue.front_value())
print(queue.rear_value())


import re

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

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html

# from scrapy.exceptions import DropItem

nums = [str(n) for n in range(10)]
pattern = r'^\d{1,5}.?\d{0,2}$'

def strip_whitespace(d):
    return {
        key: (value.strip() if value is not None else value) for key, value in d.items()
    }

def strip_currency(value):
    clean = ''
    for i in value:
        if i in nums or i == ',':
            clean += i
    clean = clean.replace(',', '.')
    return clean

def strip_deposit(value):
    reverse = value[::-1]
    clean = ''
    separator = True
    for i in reverse:
        if i in nums:
            clean += i
        elif i == ',' or i == '.' and separator:
            separator = False
            clean += i
    clean = clean.replace(',', '.')
    return clean[::-1]

class ImmoscoutPipeline:
    def process_item(self, item, spider):

        temp_dict = strip_whitespace(item)

        if item.get('cold_rent'):
            temp_dict['cold_rent'] = strip_currency(item['cold_rent'])

        if item.get('warm_rent'):
            temp_dict['warm_rent'] = strip_currency(item['warm_rent'])

        if item.get('square_meter'):
            temp_dict['square_meter'] = strip_currency(item['square_meter'])

        if item.get('year_built'):
            if 'unbekannt' in item['year_built']:
                temp_dict['year_built'] = None

        if item.get('parking_rent'):
            temp_dict['parking_rent'] = strip_currency(item['parking_rent'])

        if item.get('floor'):
            if 'von' in item['floor']:
                temp_dict['floor'] = temp_dict['floor'][0]

        if item.get('rooms'):
            temp_dict['rooms'] = temp_dict['rooms'].replace(',', '.')

        if item.get('deposit'):
            deposit = strip_deposit(item['deposit'])
            match = re.match(pattern, deposit)
            if match and (len(item['deposit']) < 25) and ('-' not in item['deposit']):
                if len(deposit) <= 3:
                    temp_dict['deposit'] = str(
                        round(
                            (float(deposit) * float(temp_dict['cold_rent'])),
                            2
                        )
                    )
                else:
                    temp_dict['deposit'] = deposit
            else:
                temp_dict['deposit'] = None

        return temp_dict


# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

#counting valley

s = 'DDUUDDUDUUUD'

k = 0
valley = 0
for i in range(len(s)):
    if (s[i] == 'U'):
        k = k + 1
        g = 1
    if (s[i] == 'D'):
        k = k -1
        g = 0
    print(k,g)
    if ( (k == 0) & (g == 1)):
        valley = valley + 1
valley

s= 'au'

longest_char = []
for i in range(len(s)-1):
    current_char = list(s[i])
    print(i)
    print(current_char)
    for j in range(i+1,len(s)):
        if (s[j] in current_char):
            if (len(current_char) > len(longest_char)):
                longest_char = current_char
            break
        if (s[j] not in current_char):
            current_char.append(s[j])
            print(j,current_char)
        if (j == len(s)-1):
            if (len(current_char) > len(longest_char)):
                longest_char = current_char
            break

longest_char

arr_count = int(input())
arr = list(map(int, input().rstrip().split()))

reverse = a*1
n =len(a)
for i in range(len(a)):
    print(a)
    reverse[n-i-1] = a[i]
    print(i)
    print(a)
    print(reverse)

print(reverse)


##for i in range[5]
##   nome = input("Nome: ")
##    t    = float(input("Tempo: "))
##
##   nadadoras.append(nome)
##   tempo.append(t)

#índices --> 0        1            2           3        4
nadadoras = ["Luisa", "Alexandra", "Victória", "Karol", "Beatriz"]
tempo     = [17.3   , 15         , 13        , 19     , 16]

#Informar o nome da melhor e da pior nadadora
#Sintaxe: nome_lista(elemento_procurado)

indice_melhor_tempo = tempo.index(min(tempo))
indice_pior_tempo   = tempo.index(max(tempo))

print("Melhor nadadora é {}".format(nadadoras[indice_melhor_tempo]))
print("Pior nadadora é {}".format(nadadoras[indice_pior_tempo]))


from gierka.hero import Hero
from gierka.monster import Monster
from random import randrange

## the class to manage the battle between the hero and the monster
class Battle:
    ## the constructor
    # @param hero the hero of the game
    # @param the moster that the hero is fighting right now
    def __init__(self, hero, monster):
        self.hero = hero
        self.monster = monster

    ## method taht defines the turn of the monster in the battle. Makes random choices and calls aproptiate functions.
    def monster_turn(self):
        move = randrange(0,2)
        if move == 1:
            self.monster_attack()
        else:
            self.monster_heal()

    ## the method to define the attack of the monster
    def monster_attack(self):
        defence = self.hero.get_defence()
        damage = 40
        if defence > 1:
            damage = damage / (defence / 2)
        self.hero.lower_hp(damage)
        print("The {} attacked!".format(self.monster.type))

    ## the method to define the attack of the hero
    def hero_attack(self):
        self.monster.lower_hp(self.hero.get_attack())
        self.hero.lower_mana(20)

    ## the method that defines the rest option for the hero in the battle, allowing them to regain  mana
    def hero_rest(self):
        self.hero.up_mana(10)

    ## the method that allows the monster to regain health
    def monster_heal(self):
        self.monster.up_hp(5)
        print("The {} healed itself".format(self.monster.type))

    ## method that defines the turn of the hero in the battle, interpretes the input from the user and calls appropriate funstions
    def hero_turn(self):
        print("{} hp:{}, mp:{}".format(self.hero.get_name(), self.hero.hp, self.hero.mana))
        print("{} hp:{}".format(self.monster.type, self.monster.hp))
        print("What do you want to do?")
        print("1. attack (20 mana)")
        print("2. rest (recover 10 mana)")
        command = None
        while command not in ("1", "2"):
            command = input(">>")
            if command not in ("1", "2"):
                print("Invalid command")
        if command == "1":
            if self.hero.mana[0] >= 20:
                self.hero_attack()
            else:
                print("You don't have enough mana...")
        elif command == "2":
            self.hero_rest()

    ## the mian method of the class calls the functions describing the turns of the hero and the monster until the winer is decided and return the result of the battle
    def battle(self):
        while self.monster.hp > 0 and self.hero.hp[0] > 0:
            self.hero_turn()
            if self.monster.hp > 0:
                self.monster_turn()
        return self.monster.hp <= 0

if __name__ == "__main__":
    h = Hero("test")
    m = Monster(1,2)
    b = Battle(h,m)
    b.battle()


from pycp2k.inputsection import InputSection
from ._wall1 import _wall1

class _metavar1(InputSection):
    def __init__(self):
        InputSection.__init__(self)
        self.Lambda = None
        self.Mass = None
        self.Gamma = None
        self.Scale = None
        self.Colvar = None
        self.WALL_list = []
        self._name = "METAVAR"
        self._keywords = {'Gamma': 'GAMMA', 'Scale': 'SCALE', 'Colvar': 'COLVAR', 'Lambda': 'LAMBDA', 'Mass': 'MASS'}
        self._repeated_subsections = {'WALL': '_wall1'}
        self._aliases = {'Width': 'Scale'}
        self._attributes = ['WALL_list']

    def WALL_add(self, section_parameters=None):
        new_section = _wall1()
        if section_parameters is not None:
            if hasattr(new_section, 'Section_parameters'):
                new_section.Section_parameters = section_parameters
        self.WALL_list.append(new_section)
        return new_section

    @property
    def Width(self):
        """
        See documentation for Scale
        """
        return self.Scale

    @Width.setter
    def Width(self, value):
        self.Scale = value


import spacy
import json
from csv import DictWriter
from spacy.matcher import Matcher

# Load English tokenizer, tagger, parser, NER and word vectors
nlp = spacy.load("en_core_web_sm")

def is_ambiguous(sent: str):
    doc = nlp(sent.lower())
    matches = matcher(doc)
    return len(matches) > 0

# Load the dataset
with open('data/requirements.json') as file:
    dataset = json.load(file)

# Load the patterns
with open('data/patterns.json') as file:
    patterns = json.load(file)

matcher = Matcher(nlp.vocab)
matcher.add("AMBIGUOUS_PATTERNS", patterns)

# Result dictionary
results = []

# Evaluate the algorithm
correct_predictions = 0
for requirement in dataset:
    prediction = is_ambiguous(requirement['requirement'])
    # Add result
    is_correct = prediction == requirement['is_ambiguous']
    results.append({"requirement": requirement["requirement"], "prediction": prediction,
                    "is_ambiguous": requirement['is_ambiguous'],
                    "is_correct": is_correct})
    if is_correct:
        correct_predictions += 1

# Save results to CSV
with open('results.csv', 'w', newline='') as file:
    writer = DictWriter(file, fieldnames=['requirement', 'prediction', 'is_ambiguous', 'is_correct'])
    writer.writeheader()
    writer.writerows(results)

accuracy = correct_predictions / len(dataset)
print(f"Accuracy: {accuracy:.2f}")


import numpy as np
import matplotlib.pyplot as plt
import sympy as sy
from sympy.utilities.lambdify import lambdify

def f(x):
    x = sy.symbols('x')
    return sy.sin(x)/x

def g(x):
    return sy.exp(x)

def taylor_expansion(f, a=1, n=3):
    x = sy.symbols('x')
    func = f(x)
    result = func.subs(x, a)
    for i in range(1, n):
        result += sy.diff(func, x, i).subs(x, a) * ((x - a)**i)/(sy.factorial(i))
    sy.pretty_print(result)
    return lambdify(x, result)

x = np.linspace(-9.4, 9.4, 200)

func_to_plot = taylor_expansion(f, n=10)

plt.plot(x, np.sin(x), 'k', label='Analytic')
plt.plot(x, [func_to_plot(i) for i in x])
plt.legend()
plt.show()

taylor_expansion(g, n=6)


def main():
    V, A, B, C = map(int, input().split())
    V = V % (A + B + C)
    a = V - A
    b = a - B
    c = b - C

    if a < 0:
        print("F")
    elif b < 0:
        print("M")
    else:
        print("T")

if __name__ == '__main__':
    main()


import sys

#readline
datas = []
while True:
  try:
    line = input()
    if line == "":
        break
    datas.append(line)
  except:
    break

#print(datas)

#a , b = map(int, input().split())

#풀이
#1. b 부터 시작해서 a 배수까지 늘려간다음에, 최소 공배수를 찾는다.
#2. a 에서 배려가면서, a 와 b 를 모두 나눌수 있는, 최대 공약수를 찾는다.

#구현
def printResult (a , b):
    result = []

    for i in range(1, a+1):
        if (b*i) % a ==0:
            #print ("b*i", b*i)
            result.append(b*i)
            break

    for i in range(a, 0, -1):
        if a % i ==0 and b % i ==0:
            #print ("i", i)
            result.append(i)
            break

    print(result[0],result[1])

for data in datas:
    try:
        a , b = map(int, data.split(" "))
        printResult(a, b)
    except:
       pass


t = {

	'comment' : u'回复',
	'nocomments_desc' : u'还没有恢复.',
	'editing_comment' : u'编辑回复',
	'view_comment' : u'查看回复',

	# form fields
	'name' : u'Name',
	'name_explain' : u'作者',
	'name_missing' : u'请输入作者',

	'email' : u'邮件地址',
	'email_explain' : u'作者的电子邮件地址',
	'email_missing' : u'请输入一个有效的电子邮件地址', # frontend message (appears on your site!)

	'content' : u'回复',
	'content_explain' : u'',
	'content_missing' : u'请输入回复内容', # frontend message (appears on your site!)

	'status' : u'统计',
	'status_explain' : u'',

	# messages
	'created' : u'您的回复已添加', # frontend message (appears on your site!)
	'updated' : u'您的回复已更新',
	'deleted' : u'您的回复已删除',

	# email notification
	'notify_subject' : u'新回复已添加',
	'nofity_heading' : u'有新回复来了.'

}


from guacho_utils import *
import matplotlib as mpl
from mpl_toolkits.axes_grid1 import ImageGrid
from matplotlib.colors import LogNorm
from matplotlib import rc
import matplotlib.pyplot as plt
#import yt
plt.rc('font', family='serif')
plt.rc('font',**{'family':'serif','serif':['{Palatino}']})

rc('text', usetex=True)

mpl.rc('xtick',labelsize=10)
mpl.rc('ytick',labelsize=10)
mpl.rc('text',usetex=True)

firstrun= True
plt.ion()

path='/datos_diable/matias/Guacho_Working/Guacho-master/A1-5GP-GC/BIN/'
nout=45
lx=11.472
ly=5.7359
extent=(-lx,lx,-lx,lx)

circ1=plt.Circle((0.,0.),radius=1.,color='k',fill=False,ls='dashed')
#circ2=plt.Circle((0.,0.),radius=0.035,color='k',fill=False,ls='dashed')

rhosc = get_scalings(nout=0, path=path, verbose=True )[2]
tempsc = get_scalings(nout=0, path=path, verbose=True )[2]
nxtot, nytot, nztot = get_boxsize(nout=0, path=path, verbose=True )
rho = readbin3d_all(nout=nout,neq=0,path=path,verbose=False,mhd=True)
vx = readbin3d_all(nout=nout,neq=1,path=path,verbose=False,mhd=True)
vy = readbin3d_all(nout=nout,neq=2,path=path,verbose=False,mhd=True)
vz = readbin3d_all(nout=nout,neq=3,path=path,verbose=False,mhd=True)
pgas = readbin3d_all(nout=nout,neq=4,path=path,verbose=False,mhd=True)
bx = readbin3d_all(nout=nout,neq=5,path=path,verbose=False,mhd=True)
by = readbin3d_all(nout=nout,neq=6,path=path,verbose=False,mhd=True)
bz = readbin3d_all(nout=nout,neq=7,path=path,verbose=False,mhd=True)
rho_n = readbin3d_all(nout=nout,neq=8,path=path,verbose=False,mhd=True)*rhosc

v=np.sqrt(vx*vx+vy*vy+vz*vz)
b=np.sqrt(bx*bx+by*by+bz*bz)
t=pgas*1./(8.3145e7*(2*rho-rho_n))
yhp=1.-rho_n/rho
#Phi=Phi+1e-30
#Psi= (Phi*rho_n)*2.1789e-11

#------------------------------------------------------
fig= plt.figure(1)
fig.clf()

im = plt.imshow(rho[:,nytot/2,:], norm=LogNorm(), origin='lower', cmap='Blues',
           extent=extent )

cont= plt.contour(yhp[:,nytot/2,:], levels=[.9, .99, .999], colors='k',extent=extent )

plt.xlabel(r'$x~[\mathrm{R_*}]$')
plt.ylabel(r'$z~[\mathrm{R_*}]$')
plt.title(r'Density')
#plt.text(-0.13, 0.13,'A1 @ t='+str(nout*0.025).format('f')+' days')

ax= fig.add_subplot(1,1,1)
ax.add_patch(circ1)
#ax.add_patch(circ2)

cb= plt.colorbar(im,label='', pad=0.01, extend='max')
cb.set_label(r'$\rho_\mathrm{H}~[\mathrm{g cm^{-3}}]$')

plt.savefig('./PDF/A1b-f1.pdf',dpi=300,transparent=True,bbox_inches='tight')

#-------------------------------------------------------
fig= plt.figure(2)
fig.clf()

im = plt.imshow(t[:,nytot/2,:], origin='lower', cmap='gist_heat',
     extent=extent, vmin= 1e4, vmax = 2e6, norm= LogNorm())

plt.xlabel(r'$x~[\mathrm{R_*}]$')
plt.ylabel(r'$z~[\mathrm{R_*}]$')

plt.title(r'Temperature')

cb= plt.colorbar(im,label='', pad=0.01, extend='max')
cb.set_label(r'$T~[\mathrm{K}]$')
#plt.text(-0.13, 0.13,'P4c @ t='+str(nout*0.025).format('f')+' days')

plt.savefig('./PDF/A1b-f2.pdf',dpi=300,transparent=True,bbox_inches='tight')
#-------------------------------------------------------
fig= plt.figure(3)
fig.clf()

im = plt.imshow(b[:,nytot/2,:], origin='lower', cmap='Spectral', #bds_highcontrast',
     extent=extent, norm= LogNorm())#, vmin=1e-18 , vmax= 1e-12 )
plt.xlabel(r'$x~[\mathrm{R_*}]$')
plt.ylabel(r'$z~[\mathrm{R_*}]$')
plt.title(r'Magnetic field')
#plt.text(-0.13, 0.13,'P4c @ t='+str(nout*0.025).format('f')+' days')

cb= plt.colorbar(im,label='', pad=0.01, extend='max')
#cb.set_label(r'$\psi~[\mathrm{erg~cm^{-3}~s^{-1}}]$')
cb.set_label(r'$B~[G]$')

plt.savefig('./PDF/A1b-f3.pdf',dpi=300,transparent=True,bbox_inches='tight')

#--------------------------------------------------------------------------
fig= plt.figure(4)
fig.clf()

im = plt.imshow(v[:,nytot/2,:]/1e5,origin='lower', cmap='rainbow', extent=extent )

plt.xlabel(r'$x~[\mathrm{R_*}]$')
plt.ylabel(r'$z~[\mathrm{R_*}]$')
plt.title(r'Velocity')
#plt.text(-0.13, 0.13,'P4c @ t='+str(nout*0.025).format('f')+' days')

cb= plt.colorbar(im,label='', pad=0.01, extend='max')
cb.set_label(r'$v~[\mathrm{km s^{-1}}]$')

plt.savefig('./PDF/A1b-f4.pdf',dpi=300,transparent=True,bbox_inches='tight')

'''
    data = dict(density = (denT, "g/cm**3"), temperature=(temp, "K"),ionization_fraction= (yhp, "K") )
    bbox = np.array([[-.15, .15], [-.0375, .0375] ,[-.15, .15]])
    ds = yt.load_uniform_grid(data, denT.shape, length_unit="au", bbox=bbox, nprocs=1)

  slc = yt.SlicePlot(ds, "y", ["density"], width=((.3,'au'),(0.3,'au')) )

  slc.set_cmap("density", "Blues_r")
  slc.set_cmap("temperature", "gist_heat")
  slc.set_xlabel('x[AU]')
  slc.set_ylabel('z[AU]')
  slc.set_colorbar_label("density","$n_\mathrm{H}~[\mathrm{cm^{-3}}]$")

  slc.set_zlim("density",zmax=1e6,zmin=1e2)

  slc.annotate_sphere([0.,0.,0.,],(0.00511, 'au'),circle_args={'ls': 'solid'})
  slc.annotate_sphere([0.,0.,0.,],(0.035, 'au'),circle_args={'ls': 'dashed'})
  slc.annotate_contour('ionization_fraction',ncont=1,clim=(0.5,0.5))
  slc.annotate_contour('ionization_fraction',ncont=1,clim=(0.99,0.99))
  slc.annotate_contour('ionization_fraction',ncont=1,clim=(0.999,0.999),plot_args={"colors": 'black',"ls":'.'})

  slc.save('dens.png')

'''


from pycp2k.inputsection import InputSection
from ._print68 import _print68
from ._ae_basis1 import _ae_basis1
from ._pp_basis1 import _pp_basis1
from ._method1 import _method1
from ._optimization2 import _optimization2
from ._potential4 import _potential4
from ._powell1 import _powell1

class _atom1(InputSection):
    def __init__(self):
        InputSection.__init__(self)
        self.Atomic_number = None
        self.Element = None
        self.Run_type = None
        self.Coulomb_integrals = None
        self.Exchange_integrals = None
        self.Core = None
        self.Electron_configuration = []
        self.Max_angular_momentum = None
        self.Calculate_states = None
        self.PRINT = _print68()
        self.AE_BASIS = _ae_basis1()
        self.PP_BASIS = _pp_basis1()
        self.METHOD_list = []
        self.OPTIMIZATION = _optimization2()
        self.POTENTIAL = _potential4()
        self.POWELL = _powell1()
        self._name = "ATOM"
        self._keywords = {'Core': 'CORE', 'Max_angular_momentum': 'MAX_ANGULAR_MOMENTUM', 'Element': 'ELEMENT', 'Coulomb_integrals': 'COULOMB_INTEGRALS', 'Exchange_integrals': 'EXCHANGE_INTEGRALS', 'Run_type': 'RUN_TYPE', 'Calculate_states': 'CALCULATE_STATES', 'Atomic_number': 'ATOMIC_NUMBER'}
        self._repeated_keywords = {'Electron_configuration': 'ELECTRON_CONFIGURATION'}
        self._subsections = {'POWELL': 'POWELL', 'AE_BASIS': 'AE_BASIS', 'PRINT': 'PRINT', 'PP_BASIS': 'PP_BASIS', 'POTENTIAL': 'POTENTIAL', 'OPTIMIZATION': 'OPTIMIZATION'}
        self._repeated_subsections = {'METHOD': '_method1'}
        self._attributes = ['METHOD_list']

    def METHOD_add(self, section_parameters=None):
        new_section = _method1()
        if section_parameters is not None:
            if hasattr(new_section, 'Section_parameters'):
                new_section.Section_parameters = section_parameters
        self.METHOD_list.append(new_section)
        return new_section


rows, columns = input().split(', ')
matrix = []
for row in range(int(rows)):
    data = input().split()
    matrix.append([int(n) for n in data])
sum_column = 0
for col in range(int(columns)):
    for row in range(len(matrix)):
        sum_column += matrix[row][col]
    print(sum_column)
    sum_column = 0


import numpy as np
import pandas as pd
import os
import re
from sklearn import model_selection

def create_folds(data, num_splits):
    # we create a new column called kfold and fill it with -1
    data["kfold"] = -1

    num_bins = int(np.floor(1 + np.log2(len(data))))

    # bin targets
    data.loc[:, "bins"] = pd.cut(
        data["score"], bins=num_bins, labels=False
    )

    # the next step is to randomize the rows of the data
    data = data.sample(frac=1).reset_index(drop=True)

    # initiate the kfold class from model_selection module
    kf = model_selection.StratifiedGroupKFold(n_splits=num_splits, shuffle=True, random_state=0xFACED)

    # fill the new kfold column
    # note that, instead of targets, we use bins!
    for f, (t_, v_) in enumerate(kf.split(X=data, y=data['bins'].values, groups=data['anchor'].values)):
        data.loc[v_, 'kfold'] = f

    data = data.drop("bins", axis=1)
    # return dataframe with folds
    return data

def get_cpc_texts():
    contexts = []
    pattern = '[A-Z]\d+'
    for file_name in os.listdir('/home/toefl/K/ptp/dataset/context/CPCSchemeXML202105'):
        result = re.findall(pattern, file_name)
        if result:
            contexts.append(result)
    contexts = sorted(set(sum(contexts, [])))
    results = {}
    for cpc in ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'Y']:
        with open(f'/home/toefl/K/ptp/dataset/context/CPCTitleList202202/cpc-section-{cpc}_20220201.txt') as f:
            s = f.read()
        pattern = f'{cpc}\t\t.+'
        result = re.findall(pattern, s)
        cpc_result = result[0].lstrip(pattern)
        for context in [c for c in contexts if c[0] == cpc]:
            pattern = f'{context}\t\t.+'
            result = re.findall(pattern, s)
            results[context] = cpc_result + ". " + result[0].lstrip(pattern)
    return results

cpc_texts = get_cpc_texts()

# read training data
df = pd.read_csv("/home/toefl/K/ptp/dataset/train.csv")
df['context_text'] = df['context'].map(cpc_texts)

# create folds
df = create_folds(df, num_splits=5)

print(df.kfold.value_counts())
print(df.head())

df.to_csv("train_folds.csv", index=False)


from network import PortForwardServer
import parallel
import console

if __name__ == '__main__':
    server = PortForwardServer()

    def command_status(args):
        num_free, num_busy, num_all = parallel.daemon.status()
        print('ALL: {0}'.format(num_all))
        print('FREE: {0}'.format(num_free))
        print('BUSY: {0}'.format(num_busy))
    def command_add(args):
        listen, dst = args.split('>')
        listen = listen.split(':')
        if len(listen) == 2:
            listen_ip, listen_port = listen
            listen_port = int(listen_port)
        elif len(listen) == 1:
            listen_ip = '0.0.0.0'
            listen_port = int(listen[0])
        else:
            print('unknown input:', listen)
        dst_ip, dst_port = dst.split(':')
        dst_port = int(dst_port)
        print('dst_ip: {0} [fmt={1}]'.format(dst_ip, type(dst_ip)))
        print('dst_port: {0} [fmt={1}]'.format(dst_port, type(dst_port)))
        print('listen_ip: {0} [fmt={1}]'.format(listen_ip, type(listen_ip)))
        print('listen_port: {0} [fmt={1}]'.format(listen_port, type(listen_port)))
        server.add(dst_ip, dst_port, listen_ip, listen_port)
    def command_ls(args):
        lists = server.get_rules()
        for i, l in enumerate(lists):
            print('[{0}] {1}'.format(i+1, l))
    def command_delete(args):
        index = None
        try:
            index = int(args)
        except:
            print('Error: delete command should be followed by a int value.')
        if index > len(server.get_rules()):
            print('Error: index too large for only {0} rules.'.format(len(server.get_rules())))
        elif index <= 0:
            print('Error: Invalid index {0}'.format(index))
        else:
            server.delete(index-1)

    con = console.console('PortForward')
    con.regist('status', action=command_status, help_info='show parallel daemon status.')
    con.regist('add', action=command_add, help_info='Add new port forward, format:\n\t[listen_ip:]listen_port>dst_ip:dst_port')
    con.regist('ls', action=command_ls, help_info='Show current port forward rules.', alias=['show'])
    con.regist('delete', action=command_delete, help_info='delete a rule.', alias=['del'])
    con.interactive()


import asyncio
from pyppeteer import launch
async def intercept_network_response(response):
    # In this example, we only care about HTML responses!
    if "text/html" in response.headers.get("content-type", ""):
        # Print some info about the responses
        print("URL:", response.url)
        print("Method:", response.request.method)
        print("Response headers:", response.headers)
        print("Request Headers:", response.request.headers)
        print("Response status:", response.status)
        # Print the content of the response
        print("Content: ", await response.json())
        # NOTE: Use await response.json() if you want to get the JSON directly
async def main():
    browser = await launch()
    page = await browser.newPage()
    await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36')
    await page.goto('https://www.woolworths.com.au')

    page.on('response', lambda response: asyncio.ensure_future(intercept_network_response(response)))

    await page.goto('https://www.woolworths.com.au/shop/productdetails/339939')
    await browser.close()
asyncio.get_event_loop().run_until_complete(main())


from selenium import webdriver
import unittest
import HtmlTestRunner

class GoogleSearch(unittest.TestCase):

	@classmethod
	def setUpClass(self):
		self.driver = webdriver.Chrome(executable_path='../../ChromeDriver/chromedriver.exe')
		self.driver.implicitly_wait(10)
		self.driver.maximize_window()

	def test_search_automation(self):
		self.driver.get('https://google.com')
		self.driver.find_element_by_name('q').send_keys('Automation Testing')
		self.driver.find_element_by_name('btnK').click()
		assert 'Automation Testing' in self.driver.page_source

	def test_search_python_for_new_bie(self):
		self.driver.get('https://google.com')
		self.driver.find_element_by_name('q').send_keys('Python for beginner')
		self.driver.find_element_by_name('btnK').click()
		assert 'Python for beginner' in self.driver.page_source

	@classmethod
	def tearDownClass(self):
		self.driver.close()
		self.driver.quit()
		print("Test Completed")

if __name__ == '__main__':
	unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='C:/Phu/Python/Selenium/Report'))


import torch
from torch import optim
import time
import numpy as np

from quantumdraw.sampler.metropolis import Metropolis
from quantumdraw.solver.neural_solver import NeuralSolver
from quantumdraw.solver.user_solver import UserSolver
from quantumdraw.wavefunction.neural_wave_function import NeuralWaveFunction
from quantumdraw.wavefunction.user_wave_function import UserWaveFunction

domain = {'xmin': -5., 'xmax': 5.}

def get_user_score(user_guess, current_pot):
    uwf = UserWaveFunction(current_pot, domain, xpts=list(map(lambda g: g[0], user_guess)), ypts=list(map(lambda g: g[1], user_guess)))

    # sampler
    sampler = Metropolis(nwalkers=100, nstep=100,
                         step_size=0.5, domain=domain)

    usolver = UserSolver(wf=uwf, sampler=sampler)
    data = usolver.feedback()
    points = list(zip(data['x'],data['y']))

    return points, usolver.get_score()

def get_solution(current_pot):
    uwf = UserWaveFunction(current_pot, domain, xpts=[], ypts=[])

    # sampler
    sampler = Metropolis(nwalkers=100, nstep=100,
                         step_size=0.5, domain=domain)

    usolver = UserSolver(wf=uwf, sampler=sampler)
    data = usolver.get_solution()
    data['y'] /= np.max(data['y'])
    points = list(zip(data['x'].tolist(),data['y'].tolist()))

    return points

def get_ai_score(current_pot, max_iterations=100, duration=30):
    sampler = Metropolis(nwalkers=500, nstep=2000,
                         step_size=0.5, domain=domain)

    # wavefunction
    wf = NeuralWaveFunction(current_pot, domain, 11, fcinit='random', sigma=0.5)

    # optimizer
    opt = optim.Adam(wf.parameters(), lr=0.05)

    # scheduler
    scheduler = optim.lr_scheduler.StepLR(opt, step_size=100, gamma=0.75)

    # define solver
    solver = NeuralSolver(wf=wf, sampler=sampler, optimizer=opt, scheduler=scheduler)
    # solver = NeuralSolver(wf=wf, sampler=sampler, optimizer=None, scheduler=None)

    pos = solver.run(1)
    solver.sampler.nstep = solver.resample.resample

    end_time = time.time() + duration
    while time.time() < end_time:
        pos = solver.run(1, pos=pos, with_tqdm=False)
        num_samples = 50
        low_x = -5
        high_x = 5
        x_points = [low_x + sample_num * (high_x - low_x) / num_samples for sample_num in range(0, num_samples)]
        x = torch.tensor(x_points)
        y = solver.wf(x)
        y /= y.max()
        y_points = y.detach().numpy().T[0].tolist()
        points = list(zip(x_points, y_points))
        yield points, solver.get_score()


from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import scipy.cluster.hierarchy as sch
from sklearn.cluster import AgglomerativeClustering

def k_means_clustering(X):
    print ("Using the elbow method to perform K means clustering..")
    wcss = []
    for i in range(1,11):
        kmeans = KMeans(n_clusters=i, init='k-means++', max_iter=300, n_init=10, random_state=0)
        kmeans.fit(X)
        wcss.append(kmeans.inertia_)
    max_ratio = 0
    optimum_clusters = 0
    for i in range(1,9):
        if (wcss[i] - wcss[i - 1]) / (wcss[i + 1] - wcss[i - 1]) > max_ratio:
            max_ratio = (wcss[i] - wcss[i - 1]) / (wcss[i + 1] - wcss[i - 1])
            optimum_clusters = i + 1
    kmeans = KMeans(n_clusters=optimum_clusters, init='k-means++', max_iter=300, n_init=10, random_state=0)
    y_kmeans = kmeans.fit_predict(X)
    return y_kmeans

def hierarchical_clustering(X):
    print('Plotting Dendrogram,.')
    dendrogram = sch.dendrogram(sch.linkage(X, method='ward'))
    plt.title('Dendrogram')
    plt.ylabel('Euclidean Distances')
    plt.show()
    optimum_clusters = input('Input the optimum cluster...')
    print('Performing Hierarchical Clustering..')
    hc = AgglomerativeClustering(n_clusters=optimum_clusters, affinity='euclidean', linkage='ward')
    y_hc = hc.fit_predict(X)
    return y_hc


import io
import zipfile
import pandas as pd

def extract_zip(content):
    with zipfile.ZipFile(io.BytesIO(content)) as thezip:
        for zipinfo in thezip.infolist():
            with thezip.open(zipinfo) as thefile:
                yield zipinfo.filename, thefile

def extract_features_from_bureau(bureau_df, bureau_balances_df):
    bureau_df['AMT_CREDIT_SUM_DEBT'] = bureau_df['AMT_CREDIT_SUM_DEBT'].fillna(value=0)
    bureau_df['AMT_CREDIT_DEBT_RATIO'] =  bureau_df['AMT_CREDIT_SUM_DEBT']/data['bureau']['AMT_CREDIT_SUM']
    bureau_df['AMT_CREDIT_DEBT_RATIO'] = bureau_df['AMT_CREDIT_DEBT_RATIO'].fillna(value=0)

    bureau_df = bureau_df.set_index('SK_ID_BUREAU')

    DPD_STATUS_MAP = {
        'C': 0,
        'X': 0,
        '0': 0,
        '1': 1,
        '2': 2,
        '3': 3,
        '4': 4,
        '5': 5,
    }
    def sum_of_dpd(x):
        return x['STATUS'].map(DPD_STATUS_MAP).sum()

    dpd_counts_df = bureau_balances_df.groupby(['SK_ID_BUREAU']).apply(sum_of_dpd)

    dpd_counts_df = pd.DataFrame(dpd_counts_df, columns=['DPD_COUNTS']);
    bureau_with_dpds = pd.concat([bureau_df, dpd_counts_df], axis=1)

    bureau_with_dpds = bureau_with_dpds.set_index('SK_ID_CURR')

    return bureau_with_dpds[:][['AMT_CREDIT_DEBT_RATIO', 'CREDIT_DAY_OVERDUE', 'DPD_COUNTS']]


import os,sys
import numpy as np

#sys.path.insert(0, '../utils')
#sys.path.insert(1, os.path.join(sys.path[0], '..'))
sys.path.append("..")

################################
#####  External functions  #####
################################

# def __SMA ( df['Close'], 21 )
from util.sma   import __SMA

####################################
#####  PANDAS BOLLINGER BANDS  #####
####################################
#def calculate_bollinger_bands(data, window=20):
#    rolling_mean = data['Close'].rolling(window=window).mean()
#    rolling_std = data['Close'].rolling(window=window).std()
#    upper_band = rolling_mean + 2 * rolling_std
#    lower_band = rolling_mean - 2 * rolling_std
#    return rolling_mean, upper_band, lower_band

def __BB (data, window=20, cl='Close'):
    std = data[cl].rolling(window).std()
    data = __SMA ( data, window, cl )
    data['BB_upper']   = data["SMA_20"] + std * 2
    data['BB_lower']   = data["SMA_20"] - std * 2
    data['BB_middle']  = data["SMA_20"]

    # Calculate the cross over and cross under values
    #data['BB_Cross_Over']  = np.where ( data['Close'] > data['BB_upper'], 1, 0 )
    #data['BB_Cross_Under'] = np.where ( data['Close'] < data['BB_lower'], 1, 0 )

    ## 2 = LONG, -2 = SHORT
    #data['BB_Signal'] = np.select(
    #    [ ( data['Close'] < data['BB_lower'] ) & ( data['Close'].shift(1) > data['BB_lower'].shift(1)),
    #      ( data['Close'] > data['BB_upper'])  & ( data['Close'].shift(1) < data['BB_upper'].shift(1))],
    #    [2, -2])

    return data


from __future__ import division

import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import beta

class BernoulliBandit(object):
    """伯努利多臂老虎机,输入K表示拉杆个数."""

    def __init__(self, num_bandit, probs=None):
        assert probs is None or len(probs) == num_bandit

        if probs is None:
            # 随机生成K个0～1的数,作为拉动每根拉杆的获奖概率
            self.probs = np.random.uniform(size=num_bandit)
        else:
            self.probs = probs

        self.best_idx = np.argmax(self.probs)  # 获奖概率最大的拉杆
        self.best_prob = self.probs[self.best_idx]  # 最大的获奖概率
        self.num_bandit = num_bandit

    def step(self, k):
        # 当玩家选择了k号拉杆后,根据拉动该老虎机的k号拉杆获得奖励的概率返回1（获奖）或0（未获奖）
        if np.random.rand() < self.probs[k]:
            reward = 1
        else:
            reward = 0
        return reward

class Solver(object):
    """多臂老虎机算法基本框架."""

    def __init__(self, bandit: BernoulliBandit):
        """bandit (Bandit): the target bandit to solve."""
        self.bandit = bandit
        self.counts = np.zeros(self.bandit.num_bandit)  # 每根拉杆的尝试次数
        self.regret = 0.  # 当前步的累积懊悔
        self.actions = []  # 维护一个列表,记录每一步的动作
        self.regrets = []  # 维护一个列表,记录每一步的累积懊悔

    def update_regret(self, k):
        # 计算累积懊悔并保存,k为本次动作选择的拉杆的编号
        curr_regret = self.bandit.best_prob - self.bandit.probs[k]
        self.regret += curr_regret
        self.regrets.append(self.regret)

    @property
    def estimated_probs(self):
        raise NotImplementedError

    def run_one_step(self):
        # 返回当前动作选择哪一根拉杆,由每个具体的策略实现
        raise NotImplementedError

    def run(self, num_steps):
        # 运行一定次数,num_steps为总运行次数
        for _ in range(num_steps):
            k = self.run_one_step()
            self.counts[k] += 1
            self.actions.append(k)
            self.update_regret(k)

class EpsilonGreedy(Solver):
    """epsilon贪婪算法,继承Solver类."""

    def __init__(self, bandit: BernoulliBandit, epsilon=0.01, init_prob=1.0):
        super(EpsilonGreedy, self).__init__(bandit)
        """
        eps (float): the probability to explore at each time step.
        init_prob (float): default to be 1.0; optimistic initialization
        """
        assert 0. <= epsilon <= 1.0
        self.epsilon = epsilon
        # 初始化拉动所有拉杆的期望奖励估值
        self.estimates = np.array([init_prob] * self.bandit.num_bandit)

    @property
    def estimated_probs(self):
        return self.estimates

    def run_one_step(self):
        if np.random.random() < self.epsilon:
            k = np.random.randint(0, self.bandit.num_bandit)  # 随机选择一根拉杆
        else:
            k = np.argmax(self.estimates)  # 选择期望奖励估值最大的拉杆
        reward = self.bandit.step(k)  # 得到本次动作的奖励
        self.estimates[k] += 1. / (self.counts[k] + 1) * (
            reward - self.estimates[k])
        return k

class DecayingEpsilonGreedy(Solver):
    """epsilon值随时间衰减的epsilon-贪婪算法,继承Solver类."""

    def __init__(self, bandit: BernoulliBandit, epsilon=1.0, init_prob=1.0):
        super(DecayingEpsilonGreedy, self).__init__(bandit)
        self.epsilon = epsilon
        # 初始化拉动所有拉杆的期望奖励估值
        self.estimates = np.array([init_prob] * self.bandit.num_bandit)
        self.total_count = 0

    @property
    def estimated_probs(self):
        return self.estimates

    def run_one_step(self):
        self.total_count += 1
        self.epsilon = 1.0 / self.total_count
        if np.random.random() < self.epsilon:  # epsilon值随时间衰减
            k = np.random.randint(0, self.bandit.num_bandit)
        else:
            k = np.argmax(self.estimates)

        reward = self.bandit.step(k)
        self.estimates[k] += 1. / (self.counts[k] + 1) * (
            reward - self.estimates[k])

        return k

class UCB(Solver):
    """UCB算法,继承Solver类."""

    def __init__(self, bandit: BernoulliBandit, coef, init_prob=1.0):
        super(UCB, self).__init__(bandit)
        self.total_count = 0
        self.estimates = np.array([init_prob] * self.bandit.num_bandit)
        self.coef = coef

    @property
    def estimated_probs(self):
        return self.estimates

    def run_one_step(self):
        self.total_count += 1
        ucb = self.estimates + self.coef * np.sqrt(
            np.log(self.total_count) / (2 * (self.counts + 1)))
        # 计算上置信界
        k = np.argmax(ucb)  # 选出上置信界最大的拉杆
        r = self.bandit.step(k)
        self.estimates[k] += 1. / (self.counts[k] + 1) * (
            r - self.estimates[k])
        return k

class BayesianUCB(Solver):
    """Assuming Beta prior."""

    def __init__(self, bandit: BernoulliBandit, coef=3, init_a=1, init_b=1):
        super(BayesianUCB, self).__init__(bandit)
        self.coef = coef
        self._a = np.array([init_a] *
                           self.bandit.num_bandit)  # 列表,表示每根拉杆奖励为1的次数
        self._b = np.array([init_b] *
                           self.bandit.num_bandit)  # 列表,表示每根拉杆奖励为0的次数

    @property
    def estimated_probs(self):
        return self._a / (self._a + self._b)

    def run_one_step(self):
        ucb = self._a / (self._a + self._b) + beta.std(self._a,
                                                       self._b) * self.coef
        # 计算上置信界
        k = np.argmax(ucb)  # 选出上置信界最大的拉杆
        r = self.bandit.step(k)
        self._a[k] += r  # 更新Beta分布的第一个参数
        self._b[k] += (1 - r)  # 更新Beta分布的第二个参数
        return k

class ThompsonSampling(Solver):
    """汤普森采样算法,继承Solver类."""

    def __init__(self, bandit: BernoulliBandit, init_a=1, init_b=1):
        super(ThompsonSampling, self).__init__(bandit)
        """
        init_a (int): initial value of a in Beta(a, b).
        init_b (int): initial value of b in Beta(a, b).
        """
        self._a = np.array([init_a] *
                           self.bandit.num_bandit)  # 列表,表示每根拉杆奖励为1的次数
        self._b = np.array([init_b] *
                           self.bandit.num_bandit)  # 列表,表示每根拉杆奖励为0的次数

    @property
    def estimated_probs(self):
        return self._a / (self._a + self._b)

    def run_one_step(self):
        samples = np.random.beta(self._a, self._b)  # 按照Beta分布采样一组奖励样本
        k = np.argmax(samples)  # 选出采样奖励最大的拉杆
        r = self.bandit.step(k)

        self._a[k] += r  # 更新Beta分布的第一个参数
        self._b[k] += (1 - r)  # 更新Beta分布的第二个参数
        return k

def plot_results(solvers, solver_names, figname):
    """生成累积懊悔随时间变化的图像。输入solvers是一个列表,列表中的每个元素是一种特定的策略。
    而solver_names也是一个列表,存储每个策略的名称.

    Plot the results by multi-armed bandit solvers.
    Args:
        solvers (list<Solver>): All of them should have been fitted.
        solver_names (list<str)
        figname (str)
    """
    assert len(solvers) == len(solver_names)
    assert all(map(lambda s: isinstance(s, Solver), solvers))
    assert all(map(lambda s: len(s.regrets) > 0, solvers))

    bandit = solvers[0].bandit

    fig = plt.figure(figsize=(18, 5))
    fig.subplots_adjust(bottom=0.3, wspace=0.3)

    ax1 = fig.add_subplot(131)
    ax2 = fig.add_subplot(132)
    ax3 = fig.add_subplot(133)

    # Sub.fig. 1: Regrets in time.
    for i, s in enumerate(solvers):
        ax1.plot(range(len(s.regrets)), s.regrets, label=solver_names[i])

    ax1.set_xlabel('Time step')
    ax1.set_ylabel('Cumulative regret')
    ax1.legend(loc=9, bbox_to_anchor=(1.82, -0.25), ncol=5)
    ax1.grid('k', ls='--', alpha=0.3)

    # Sub.fig. 2: Probabilities estimated by solvers.
    sorted_indices = sorted(
        range(bandit.num_bandit), key=lambda x: bandit.probs[x])
    ax2.plot(
        range(bandit.num_bandit), [bandit.probs[x] for x in sorted_indices],
        'k--',
        markersize=12)
    for s in solvers:
        ax2.plot(
            range(bandit.num_bandit),
            [s.estimated_probs[x] for x in sorted_indices],
            'x',
            markeredgewidth=2)
    ax2.set_xlabel('Actions sorted by ' + r'θ')
    ax2.set_ylabel('Estimated')
    ax2.grid('k', ls='--', alpha=0.3)

    # Sub.fig. 3: Action counts
    for s in solvers:
        ax3.plot(
            range(bandit.num_bandit),
            np.array(s.counts) / float(len(solvers[0].regrets)),
            ls='solid',
            lw=2)
    ax3.set_xlabel('Actions')
    ax3.set_ylabel('Frac. # trials')
    ax3.grid('k', ls='--', alpha=0.3)

    plt.savefig(figname)

def plot_results_(solvers, solver_names):
    """生成累积懊悔随时间变化的图像。输入solvers是一个列表,列表中的每个元素是一种特定的策略。
    而solver_names也是一个列表,存储每个策略的名称."""

    for idx, solver in enumerate(solvers):
        time_list = range(len(solver.regrets))
        plt.plot(time_list, solver.regrets, label=solver_names[idx])
    plt.xlabel('Time steps')
    plt.ylabel('Cumulative regrets')
    plt.title('%d-armed bandit' % solvers[0].bandit.num_bandit)
    plt.legend()
    plt.show()

def experiment(K, N):
    """Run a small experiment on solving a Bernoulli bandit with K slot
    machines, each with a randomly initialized reward probability.

    Args:
        K (int): number of slot machiens.
        N (int): number of time steps to try.
    """

    bandit = BernoulliBandit(K)
    print('Randomly generated Bernoulli bandit has reward probabilities:\n',
          bandit.probs)
    print('The best machine has index: {} and proba: {}'.format(
        max(range(K), key=lambda i: bandit.probs[i]), max(bandit.probs)))

    test_solvers = [
        # EpsilonGreedy(bandit, 0),
        # EpsilonGreedy(bandit, 1),
        EpsilonGreedy(bandit, 0.01),
        UCB(bandit, coef=1.0),
        BayesianUCB(bandit, 3, 1, 1),
        ThompsonSampling(bandit, 1, 1)
    ]
    names = [
        # 'Full-exploitation', 'Full-exploration',
        r'ϵ' + '-Greedy',
        'UCB1',
        'Bayesian UCB',
        'Thompson Sampling'
    ]

    for s in test_solvers:
        s.run(N)

    plot_results(test_solvers, names, 'results_K{}_N{}.png'.format(K, N))

def plot_single():

    np.random.seed(1)  # 设定随机种子,使实验具有可重复性
    K = 10  # 10
    bandit_10_arm = BernoulliBandit(K)
    print('随机生成了一个%d臂伯努利老虎机' % K)
    print('获奖概率最大的拉杆为%d号,其获奖概率为%.4f' %
          (bandit_10_arm.best_idx, bandit_10_arm.best_prob))

    np.random.seed(1)
    epsilon_greedy_solver = EpsilonGreedy(bandit_10_arm, epsilon=0.01)
    epsilon_greedy_solver.run(50000)
    print('epsilon-贪婪算法的累积懊悔为：', epsilon_greedy_solver.regret)
    plot_results_([epsilon_greedy_solver], ['EpsilonGreedy'])

    np.random.seed(0)
    epsilons = [1e-4, 0.01, 0.1, 0.25, 0.5, 1.0]
    epsilon_greedy_solver_list = [
        EpsilonGreedy(bandit_10_arm, epsilon=e) for e in epsilons
    ]
    epsilon_greedy_solver_names = ['epsilon={}'.format(e) for e in epsilons]
    for solver in epsilon_greedy_solver_list:
        solver.run(50000)

    plot_results_(epsilon_greedy_solver_list, epsilon_greedy_solver_names)

    np.random.seed(1)
    decaying_epsilon_greedy_solver = DecayingEpsilonGreedy(bandit_10_arm)
    decaying_epsilon_greedy_solver.run(50000)
    print('epsilon值衰减的贪婪算法的累积懊悔为：', decaying_epsilon_greedy_solver.regret)
    plot_results_([decaying_epsilon_greedy_solver], ['DecayingEpsilonGreedy'])

    np.random.seed(1)
    coef = 1  # 控制不确定性比重的系数
    UCB_solver = UCB(bandit_10_arm, coef)
    UCB_solver.run(50000)
    print('上置信界算法的累积懊悔为：', UCB_solver.regret)
    plot_results_([UCB_solver], ['UCB'])

    np.random.seed(1)
    thompson_sampling_solver = ThompsonSampling(bandit_10_arm)
    thompson_sampling_solver.run(50000)
    print('汤普森采样算法的累积懊悔为：', thompson_sampling_solver.regret)
    plot_results_([thompson_sampling_solver], ['ThompsonSampling'])

if __name__ == '__main__':
    experiment(10, 5000)


from pandas import read_csv, get_dummies, DataFrame
df = read_csv("dataset/train.csv")
val_df = read_csv("dataset/test.csv")

features = df.columns.drop(["Id","SalePrice"])
target = ["SalePrice"]

X, y = df[features], df[target]
X= X.fillna(X.mean())
X = get_dummies(X)

vX= val_df[features]
vX= vX.fillna(vX.mean())
vX = get_dummies(vX)

X, vX = X.align(vX, join='inner', axis=1)

from sklearn.model_selection import train_test_split
train_X, test_X, train_y, test_y= train_test_split(X, y, test_size = 0.1)

from sklearn.ensemble import RandomForestRegressor
model = RandomForestRegressor()
model.fit(train_X,train_y)

pred_y = model.predict(test_X)
from sklearn.metrics import mean_squared_error
import numpy as np
rmse = mean_squared_error(np.log(test_y), np.log(pred_y))**0.5

val_y = model.predict(vX)
result = DataFrame({"Id" : list(range(1461,1461+len(val_y))), "SalePrice" : val_y})
result.set_index("Id",inplace=True)
result.to_csv("v0.csv")


# coding: utf-8

"""执行多个游戏客户端
540 × 960 分辨率
"""
import win32gui
from base import Ops, dm

class Base(Ops):
    def real_init(self):
        hds = []
        check_list = ['雷电模拟器-1-2-3', '雷电模拟器-1-2', '雷电模拟器-1', '雷电模拟器']
        for ch in check_list:
            hds_ = dm.EnumWindow(0, ch, 'LDPlayerMainFrame', 3)
            if hds_:
                hds_ = map(int, hds_.split(','))
                for hd in hds_:
                    if hd not in hds:
                        hds.insert(0, int(hd))
        hds = [int(dm.EnumWindow(hd, 'TheRender', 'RenderWindow', 3)) for hd in hds]
        self.orders = hds
        self.bind(self.orders[0])

    def check_no_mental(self):
        self.sleep_a_while(1)
        is_used = self.get_color(801,396) == '731010' and self.get_color(424,393) == '0b0b0b'
        if is_used:
            hd = self.hd
            self.switch()
            self.remove(hd)
            return True
        return False

    def bind(self, hd):
        self.hd = hd
        dm.BindWindow(hd, 'normal', 'windows', 'windows', 0)
        win32gui.SetForegroundWindow(hd)

    def switch(self):
        index = self.orders.index(self.hd)
        if index != len(self.orders) - 1:
            next_index = index + 1
        else:
            next_index = 0
        self.bind(self.orders[next_index])

    def main(self, click_pos, round_times, name=''):
        time.sleep(0.5)
        print(name)
        click = click_pos
        for i in range(round_times):
            self.setFront()
            self.sleep_a_while(0.5)
            # 1181,638 and 1182,656 是『开始行动』按钮的两个点
            need_click = dm.GetColor(832, 480) == '0093d5' and dm.GetColor(857, 490) == 'ffffff'
            if need_click:
                # 1214,38 是编队界面右上角的垃圾箱图标
                while self.get_color(913, 33) != '7d0000':
                    self.click(832, 480)
                    if self.check_no_mental():
                        print("{} :理智用完".format(name))
                        return True
                    self.sleep_a_while(1)
                    self.switch()
                    yield
            # 1210,55 是战斗界面右上角的暂停图标
            # 1126,468 是编队界面的『开始行动』图标
            while self.get_color(893, 36) != 'ffffff':
                self.click(822, 369)
                self.sleep_a_while(1)
                self.switch()
                yield

                # 现在应该进入战斗
            while not (dm.GetColor(832, 480) == '0093d5' and dm.GetColor(857, 490) == 'ffffff'):
                print(name)
                if self.check_no_mental():
                    print("{} :理智用完".format(name))
                    return True
                self.click(*click)
                self.sleep_a_while(0.5)
                self.switch()
                yield
                self.sleep_a_while(0.5)
        yield True

    def remove(self, hd):
        self.orders.remove(hd)

    def run(self, gs):

        assert len(gs) == len(self.orders)
        gss = [False for g in gs]
        while True:
            if all(gss):
                break
            for i, g in enumerate(gs):
                if gss[i]:
                    continue
                try:
                    next(g)
                except StopIteration:
                    print('{} stop'.format(str(g)))
                    gss[i] = True

CE5 = (696,131)
M1_7 = (438,168)
M4_4 = (406,250)
M4_9 = (416,257)
JIAO_MIE = (582,409)
XIN_PAIN2 = (627,195)

if __name__ == '__main__':
    import time
    time.sleep(3)
    b = Base(enable_front=True)
    gs = [
        b.main(M1_7, 100, "官服号"),
        b.main(CE5, 100, "b服凛寒"),
        b.main(XIN_PAIN2, 100, "b服柠檬sweet")
    ]
    b.run(gs)


"""

mlsim

Author
------
J. Brusey

Date
----
4-March-2021

MLSim class that packages up a machine learnt model and scaler.

"""

import numpy as np

from .partial_scaler import PartialScaler

class MLSim:
    """Discrete time simulator derived from machine-learnt model.

    The idea of this class is to wrap up the ML simulator and deal
    with scaling, lagged state and actions, and differing intervals to
    make the simulator easier to call. The initial state and prior
    actions are provided to the constructor and then the simulator can
    be run by successive calls to ~step~.

    """

    def __init__(
        self,
        scaler_and_model,
        initial_state,
        xlag,
        ulag,
        xlen,
        ulen,
        interval=1,
        initial_clock=0,
        prior_actions=None,
        ut_min=None,
        ut_max=None,
    ):
        """construct a simulator object

        Parameters
        ----------
        scaler_and_model : tuple

          tuple containing scaler and model. The scaler transforms the
          combined x, u vector, while model provides a single step
          simulator for scaled values.

        initial_state : array-like

          vector containing initial state. Can also be a 2D array of
          xlag vectors for more complete initialisation. If only a
          single vector is supplied, that vector is replicated xlag
          times to make up the initial state.

        xlag : integer

          number of lagged state values. This must match the
          parameters used to develop the model

        ulag : integer

          number of lagged control values. This must match the
          parameters used to develop the model

        xlen : integer

          length of the state vector

        ulen : integer

          length of the control vector

        interval : integer

          time interval that the simulator was developed using, in
          seconds

        initial_clock : integer

          initial clock value so that first step will have time
          initial_clock + interval

        prior_actions : array

          ulag - 1 actions

        ut_min : array

          action array minimum values (used for clipping)

        ut_max : array

          action array maximum values (used for clipping)

        """
        scaler, self.model = scaler_and_model
        self.xt = initial_state
        assert initial_state.shape == (xlag, xlen)
        self.xlag = xlag
        self.ulag = ulag
        self.xlen = xlen
        self.ulen = ulen
        self.interval = interval
        self.clock = initial_clock
        self.prior_actions = prior_actions
        self.first = True
        self.x_scaler = PartialScaler(scaler, 0, xlen, xlen + ulen)
        self.u_scaler = PartialScaler(scaler, xlen, xlen + ulen, xlen + ulen)
        self.xt = self.x_scaler.transform(self.xt)
        self.ut_min = ut_min
        self.ut_max = ut_max
        assert (ut_min is None) == (ut_max is None)

    def step(self, ut):
        """simulate a single time step

        Given the current state history given by ~xt~ and current
        control history ~ut~

        Parameters
        ----------

        ut : array-like

          control input for current time step

        Returns
        -------

        (time, state vector at end of time step)

        """
        ut = np.array(ut, dtype=np.float32).reshape(1, -1)
        if self.ut_min is not None and self.ut_max is not None:
            ut = np.clip(ut, self.ut_min, self.ut_max)
        ut = self.u_scaler.transform(ut)
        assert self.xt.shape[0] == self.xlag
        assert self.xt.shape[1] == self.xlen
        if self.first:
            # for first step, assume that the same control inputs were
            # used for all previous steps
            if self.prior_actions is not None:
                self.ut = np.vstack([self.u_scaler.transform(self.prior_actions), ut])
            else:
                self.ut = np.vstack([ut] * (self.ulag))
            self.first = False
        else:
            self.ut = np.append(self.ut[1:], ut, axis=0)
        assert self.ut.shape == (self.ulag, self.ulen)

        x = np.hstack((self.xt.reshape(1, -1), self.ut.reshape(1, -1)))
        new_xt = self.model.predict(x)
        self.xt = np.append(self.xt[1:], new_xt.reshape(1, -1), axis=0)
        self.clock += self.interval
        return (self.clock, self.x_scaler.inverse_transform(new_xt)[0])


from math import *

class TreeNode:
    def __init__(self):
        self.val = set()
        self.child = [None for i in range(26)]

def dfs(root, ans):
    if len(root.val) <= 1:
        return

    for i in range(26):
        if root.child[i] != None:
            dfs(root.child[i], ans)

    diff = root.val - ans
    if len(diff) <= 1:
        return

    ans.add(diff.pop())
    ans.add(diff.pop())

t = int(input())
for T in range(1, t + 1):
    n = int(input())
    ans = set()
    root = TreeNode()

    for i in range(n):
        cur = root
        word = input()
        for j in range(len(word) - 1, -1, -1):
            index = ord(word[j]) - 65
            if cur.child[index] == None:

                cur.child[index] = TreeNode()

            cur.child[index].val.add(word)
            cur = cur.child[index]
    # print(root.child)

    for i in range(26):
        if root.child[i] != None:
            # print(i)
            dfs(root.child[i], ans)

    print("Case #{}: {}".format(T, len(ans)))


import logging
from logging.handlers import RotatingFileHandler
import datetime

#Logger
log = logging.getLogger('root')
log.setLevel(logging.DEBUG)

fileHandler = RotatingFileHandler('GlitchFilterLog.log', maxBytes=100000)
fileHandler.setLevel(logging.INFO)

log.addHandler(fileHandler)

log.info('******************** {} - Session ********************'.format(datetime.datetime.now()))

formatter = logging.Formatter('%(asctime)s) - %(levelname)s: %(message)s')
fileHandler.setFormatter(formatter)


class Solution:
    def subsets(self, nums: List[int]) -> List[List[int]]:
        l=[[]]
        x=[]
        temp=[]
        n=len(nums)
        for i in range(1,2**n):
            x.append(bin(i)[2:])
        for i in x:
            temp=[]
            y=i[::-1]
            for j in range(len(i)):
                if int(y[j]):
                    temp.append(nums[j])
            l.append(temp)
        return l

    #faster solution
#     class Solution:
#     def subsets(self, nums: List[int]) -> List[List[int]]:
#         res = []
#         def backtrack(lst, start):
#             res.append(list(lst))

#             for i in range(start, len(nums)):
#                 lst.append(nums[i])

#                 backtrack(lst, i+1)
#                 lst.pop()

#         backtrack([],  0)
#         return res


import logging

from ibm_cloud_sdk_core import ApiException

from ..base_client import BaseClient
from ..consts import VPC_DEFAULT_PAGINATION_LIMIT
from ..exceptions import IBMInvalidRequestError

LOGGER = logging.getLogger(__name__)

class ImagesClient(BaseClient):
    """
    Client for images related APIs
    """

    def __init__(self, cloud_id, region):
        super(ImagesClient, self).__init__(cloud_id=cloud_id, region=region)

    def list_images(self, visibility=None, name=None, resource_group_id=None,
                    limit=VPC_DEFAULT_PAGINATION_LIMIT):
        """
        :param visibility:
        :param name:
        :param resource_group_id:
        :param limit: Number of Resources Per Page
        :return:
        """
        if visibility and visibility not in ["private", "public"]:
            raise IBMInvalidRequestError("Parameter 'visibility' should be one of ['private', 'public']")

        params = {
            "visibility": visibility,
            "name": name,
            "resource_group_id": resource_group_id,
            "limit": limit
        }

        response = self.service.list_images(**params).get_result()
        return response.get("images", [])

    def create_image(self, image_json):
        """
        :param image_json:
        :return:
        """
        if not isinstance(image_json, dict):
            raise IBMInvalidRequestError("Parameter 'image_json' should be a dictionary")

        # TODO: Create schema for input and validate it

        return self.service.create_image(image_prototype=image_json).get_result()

    def delete_image(self, image_id):
        """
        :param image_id:
        :return:
        """
        return self.service.delete_image(id=image_id)

    def get_image(self, image_id):
        """
        :param image_id:
        :return:
        """
        return self.service.get_image(id=image_id).get_result()

    def update_image(self, image_id, image_json):
        """
        :param image_id:
        :param image_json:
        :return:
        """
        response = {}
        try:
            response = self.service.update_image(id=image_id, image_patch=image_json).get_result()
        except ApiException as e:
            LOGGER.info("Update Image failed with status code " + str(e.code) + ": " + e.message)

        return response

    def list_operating_systems(self, limit=VPC_DEFAULT_PAGINATION_LIMIT):
        """
        :param limit: Number of Resources Per Page
        :return:
        """
        params = {
            "limit": limit
        }

        response = {}
        try:
            response = self.service.list_operating_systems(**params).get_result()
        except ApiException as e:
            LOGGER.info("List Operating Systems failed with status code " + str(e.code) + ": " + e.message)

        return response.get("operating_systems", [])

    def get_operating_system(self, operating_system_name):
        """
        :param operating_system_name:
        :return:
        """

        response = {}
        try:
            response = self.service.get_operating_system(name=operating_system_name).get_result()
        except ApiException as e:
            LOGGER.info("Get Operating System failed with status code " + str(e.code) + ": " + e.message)

        return response


from typing import Any, List, Optional
from lib.parsers.data_parsers.data_parser import DataParser
from lib.parsers.id_parsers.id_parser import IdParser
from types_.category_id import CategoryId
from lib.db import update_supplier

class ParsingController:
    def __init__(self, id_parsers: List[IdParser]=None, data_parsers: List[DataParser]=None):
        self.fsm: Any = None
        self.cat_id: str = ''
        self.id_parsers = id_parsers or []
        self.data_parsers = data_parsers or []
        self.inns = []
        self.suppliers = {}

    def set_fsm(self, fsm):
        self.fsm = fsm

    def set_cat_id(self, cat_id: CategoryId):
        self.cat_id = cat_id

    def trigger(self, event: str):
        if (self.fsm):
            self.fsm.trigger(event)

    async def parse_inns(self):
        self.inns = []
        self.suppliers = {}
        for id_parser in self.id_parsers:
            for inn in await id_parser.parse(self.cat_id):
                self.inns.append(inn)
        self.trigger('fetching_ids_finish')
        return self.inns

    async def parse_supplier(self, inn):
        result = {}
        for data_parser in self.data_parsers:
            data = await data_parser.parse(inn)
            if data_parser.key_name is None:
                for (k, v) in data.items():
                    result[k] = v
            else:
                result[data_parser.key_name] = data
        self.suppliers[inn] = result
        return result

    async def parse_suppliers(self):
        for inn in self.inns:
            print('fetching supplier', inn)
            await self.parse_supplier(inn)
        self.trigger('fetching_data_finish')

    async def save_data(self):
        for inn in self.inns:
            supplier = self.suppliers[inn]
            await update_supplier(inn, supplier)
        self.trigger('uploading_data_finish')