import Pretrained_supporting_functions as sf
import supporting_models as sm
import tensorflow as tf
from tensorflow import keras
import wandb
from wandb.keras import WandbCallback
import numpy as np
import matplotlib.pyplot as plt
import time
import tracemalloc
import os

#/vol/research/NOBACKUP/CVSSP/scratch_4weeks/ad00878/DBs/
#DBs/

def main():

    @tf.function
    def train_step(imgs,labels):
        with tf.GradientTape() as tape:
            preds = model(imgs,training=True)
            loss = loss_func(labels,preds)
        grads = tape.gradient(loss,model.trainable_variables)
        optimizer.apply_gradients(zip(grads,model.trainable_variables))
        train_loss(loss)
        train_acc_metric(labels,preds)
        train_prec_metric(labels,preds)
        train_rec_metric(labels,preds)
        return

    wandb.init(project='k_diversity',entity='adamdowse')
    config= {
        'db_path' : "DBs/",
        'ds_name' : "cifar10",
        'train_percent' : 0.1,
        'test_percent' : 0.1,
        'group' : 't9_hyperparam_search',
        'model_name' : 'Simple_CNN',
        'learning_rate' : wandb.config.learning_rate,
        'optimizer' : wandb.config.optimizer,
        'momentum' : 0,
        'random_db' : 'True',
        'batch_size' : wandb.config.batch_size,
        'data_aug' : '0', #0 = no data aug, 1 = data aug, 2 = data aug + noise
        'max_its' : 30000,
        'early_stop' : 5000,
        'mod_type' : 'Random_Bucket_full',
        'subset_type' : 'All', #Random_Bucket, Hard_Mining, All
        'train_type' : wandb.config.run_type, #SubMod, Random
        'activations_delay' : 4, #cannot be 0
        'k_percent' : 1, #percent of data to use for RB and HM
        'activation_layer_name' : 'penultimate_layer',
    }

    #Setup
    test_ds,ds_info,conn_path, train_ds = sf.setup_db(config)

    num_classes = ds_info.features['label'].num_classes
    class_names = ds_info.features['label'].names
    img_size = ds_info.features['image'].shape
    num_train_imgs = ds_info.splits['train[:'+str(int(config['train_percent']*100))+'%]'].num_examples

    #Model
    tf.keras.backend.clear_session()
    model = sm.select_model(config['model_name'],num_classes,img_size)
    model.build(img_size+(1,))
    model.summary()

    #Load pretrained weights TODO: make this a function

    #Loss
    loss_func = tf.keras.losses.CategoricalCrossentropy(from_logits=False)

    #Metrics
    train_loss = tf.keras.metrics.Mean(name='train_loss')
    train_acc_metric = tf.keras.metrics.CategoricalAccuracy(name='train_accuracy')
    train_prec_metric = tf.keras.metrics.Precision(name='train_precision')
    train_rec_metric = tf.keras.metrics.Recall(name='train_recall')

    #Optimizer
    if config['optimizer'] == 'Adam':
        optimizer = tf.keras.optimizers.Adam(learning_rate=config['learning_rate'], beta_1=0.9, beta_2=0.999, epsilon=1e-07, amsgrad=False)
    elif config['optimizer'] == 'SGD':
        optimizer = tf.keras.optimizers.SGD(learning_rate=config['learning_rate'])
    elif config['optimizer'] == 'Momentum':
        optimizer = tf.keras.optimizers.SGD(learning_rate=config['learning_rate'],momentum=config['momentum'])
    else:
        print('Optimizer not recognised')

    #Data Generator
    train_DG = sf.SubModDataGen(conn_path,config)
    test_DG = sf.TestDataGen(test_ds, 50, num_classes)

    #Compile Model
    model.compile(optimizer=optimizer,loss=loss_func,metrics=['accuracy',tf.keras.metrics.Precision(),tf.keras.metrics.Recall()])

    #Wandb
    #sf.wandb_setup(config,disabled)
    logging_callback = WandbCallback(log_freq=1,save_model=False,save_code=False)

    #Training
    batch_num = 0
    early_stop_max = 0
    early_stop_count = 0
    while batch_num < config['max_its']:
        print('Batch Number: ',batch_num)

        #reset metrics
        train_loss.reset_states()
        train_acc_metric.reset_states()
        train_prec_metric.reset_states()
        train_rec_metric.reset_states()

        #Scores the training data and decides what to train on
        print('Getting Subset')
        train_DG.get_data_subset(model,train_ds)
        #wandb.log({'Train_loss_hist':wandb.Histogram(train_DG.losses)},step=batch_num)

        #Train on the data subset
        print('Training')
        for i in range(train_DG.num_batches):
            #get the activations for the next batch selection
            train_DG.get_activations(model,i)
            batch_data = train_DG.__getitem__(i)
            train_step(batch_data[0],batch_data[1])

        #Test on the test data
        print('Evaluating')
        test_metrics = model.evaluate(test_DG)

        #Log metrics
        wandb.log({'Train_loss':train_loss.result(),'Train_acc':train_acc_metric.result(),'Train_prec':train_prec_metric.result(),'Train_rec':train_rec_metric.result()},step=batch_num)
        wandb.log({'Test_loss':test_metrics[0],'Test_acc':test_metrics[1],'Test_prec':test_metrics[2],'Test_rec':test_metrics[3]},step=batch_num)
        batch_num += train_DG.num_batches

        if test_metrics[1] > early_stop_max:
            early_stop_max = test_metrics[1]
            early_stop_count = 0
        else:
            early_stop_count += train_DG.num_batches
            if early_stop_count > config['early_stop']:
                break

    wandb.log({'Images_used_hist':wandb.Histogram(train_DG.data_used)})
    #clear keras backend
    tf.keras.backend.clear_session()
    #Finish
    print('Finished')

if __name__ == "__main__":
    sweep_configuration = {
        'method': 'grid',
        'name': 'Test Acc vs Batch Size big batches 2',
        'metric': {
            'goal': 'maximize',
            'name': 'Test_acc'
            },
        'parameters': {
            'batch_size': {'values': [1024]},
            'learning_rate': {'values': [0.01,0.001,0.0001,0.00001]},
            'run_type': {'values': ['Random','SubMod']}, #,['Hard_Mining','SubMod'],['Hard_Mining','Random']
            'optimizer': {'values': ['SGD','Adam']},
            }
        }

    os.environ['WANDB_API_KEY'] = 'fc2ea89618ca0e1b85a71faee35950a78dd59744'
    wandb.login()
    sweep_id = wandb.sweep(sweep=sweep_configuration, project='k_diversity')
    wandb.agent(sweep_id, function=main)


import cv2
import sys

class FrameWrapper():
    """
    A standard interface for getting frames from images, videos, and the webcam.
    TODO: ZED camera interfacing

    Example usage:
        filenames = ['relative/path/img.png', './video.mp4', 'webcam']
        frames = FrameWrapper(filenames)

        # Shows img.png, then all frames in video.mp4, then frames from webcam forever
        for frame in frames:
            cv2.imshow('Next frame', frame)
    """

    # Keywords used to identify file types
    WEBCAM = ['webcam']
    VIDEO_EXTS = ['mp4', 'avi']
    IMG_EXTS = ['jpg', 'png']

    VIDEO_TRIES = 200
    WEBCAM_TRIES = 10

    def __init__(self, filenames, resize=1):
        self.filenames = filenames  # Get this list of relative paths to files from vis
        # There aren't any checks for resize==1 to improve speed b/c this expects resize != 1
        self.resize = resize

    def __iter__(self):
        self.index = -1
        self.next_data = ('', None)
        self.next_data_obj()
        return self

    def __next__(self):
        if not self.has_next:
            raise StopIteration

        while self.index < len(self.filenames):
            if self.next_data[0] == "v": # Video
                # Try to get a frame out at most VIDEO_TRIES times.
                # If it still fails, we're probably at the end of the video file
                for i in range(self.VIDEO_TRIES):
                    ret, frame = self.next_data[1].read()
                    if ret:
                        return cv2.resize(frame, None, fx=self.resize, fy=self.resize)
                    print("WARNING: Failed to get frame from video {}. Try {}." \
                            .format(self.filenames[self.index], i), file=sys.stderr)
                self.next_data_obj()
            elif self.next_data[0] == "i": # Image
                img = self.next_data[1]
                self.next_data_obj()
                if img is not None:
                    return cv2.resize(img, None, fx=self.resize, fy=self.resize)
                print("WARNING: Failed to get image {}." \
                        .format(self.filenames[self.index-1]), file=sys.stderr)
            else: # Webcam
                # Try to get a frame out at most WEBCAM_TRIES times.
                for i in range(self.WEBCAM_TRIES):
                    ret, frame = self.next_data[1].read()
                    if ret:
                        return cv2.resize(frame, None, fx=self.resize, fy=self.resize)
                    print("WARNING: Failed to get frame from webcam. Try {}." \
                            .format(i), file=sys.stderr)
                self.next_data_obj()

        raise StopIteration

    def next_data_obj(self):
        """
        Helper function for getting the next object (video, image, webcam) when
        the previous one is exhausted.
        """
        # Close the webcam if it was open and we don't want it anymore
        if self.next_data[0] in self.WEBCAM:
            self.next_data[1].release()

        # Stop if we don't have any more data
        if self.index >= len(self.filenames) - 1:
            self.index += 1
            self.has_next = False
            return

        # Prepare the next data object
        self.index += 1
        filename = self.filenames[self.index]
        extension = filename[filename.rindex('.') + 1:].lower() if '.' in filename else None

        if filename in self.WEBCAM:
            self.next_data = ('w', cv2.VideoCapture(0))
        elif extension in self.VIDEO_EXTS:
            self.next_data = ('v', cv2.VideoCapture(filename))
        elif extension in self.IMG_EXTS:
            self.next_data = ('i', cv2.imread(filename))
        else:
            print("Unknown file format:", extension)

        self.has_next = True


#
# @lc app=leetcode id=889 lang=python3
#
# [889] Construct Binary Tree from Preorder and Postorder Traversal
#

# @lc code=start
# Definition for a binary tree node.
from typing import List, Optional

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

        Attributes:
        - val: Value of the node.
        - left: Left child node.
        - right: Right child node.
        """
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def __init__(self) -> None:
        """
        Initialize the Solution object with a dictionary to keep track of
        the indices of values in the postorder traversal.
        """
        self.valToIndex = {}

    def constructFromPrePost(self, preorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
        """
        Construct a binary tree from its preorder and postorder traversal lists.

        Args:
        - preorder: List[int]: Preorder traversal of the binary tree.
        - postorder: List[int]: Postorder traversal of the binary tree.

        Returns:
        - The root node of the constructed binary tree.
        """

        # Populate the valToIndex dictionary with indices of values from the postorder traversal.
        for i in range(len(postorder)):
            self.valToIndex[postorder[i]] = i

        # Start the recursive tree construction using the entire length of both lists.
        return self.build(preorder, 0, len(preorder)-1,
                          postorder, 0, len(postorder)-1)

    def build(self, preorder: List[int], preStart: int, preEnd: int,
              postorder: List[int], postStart: int, postEnd: int) -> TreeNode:
        """
        Recursive helper function to construct the binary tree using parts of the preorder and postorder lists.

        Args:
        - preorder: List[int]: Preorder traversal of the current subtree.
        - preStart, preEnd: Define the segment of the preorder list to consider for the current subtree.
        - postorder: List[int]: Postorder traversal of the current subtree.
        - postStart, postEnd: Define the segment of the postorder list to consider for the current subtree.

        Returns:
        - The root node of the constructed subtree.
        """

        # Base case: if the current segment of the preorder list is empty, return None.
        if preStart > preEnd:
            return None

        # Base case: if only one element is left in the segment, create and return a node.
        if preStart == preEnd:
            return TreeNode(preorder[preStart])

        # The first element in the current preorder list is the root of the current subtree.
        rootVal = preorder[preStart]

        # The second element in the current preorder list is the root of the left subtree.
        leftRootVal = preorder[preStart+1]

        # Find the index of the root of the left subtree in the postorder list.
        index = self.valToIndex[leftRootVal]

        # Calculate the size of the left subtree using its end index in the postorder list.
        leftSize = index - postStart + 1

        # Create a new TreeNode with the root value.
        root = TreeNode(rootVal)

        # Recursively construct the left subtree.
        root.left = self.build(preorder, preStart+1, preStart+leftSize,
                               postorder, postStart, index)

        # Recursively construct the right subtree.
        root.right = self.build(preorder, preStart+leftSize+1, preEnd,
                                postorder, index+1, postEnd-1)

        return root

# @lc code=end


from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import math
from selenium.webdriver.support.ui import Select
import os

def calc(x):
  return str(math.log(abs(12*math.sin(int(x)))))

try:
    link = "http://suninjuly.github.io/redirect_accept.html"
    browser = webdriver.Chrome()
    browser.get(link)

    button = browser.find_element(By.CSS_SELECTOR, ".trollface.btn.btn-primary") # клацнуть на кнопку
    button.click()

    new_window = browser.window_handles[1]  # узнаем имя вкладки, через массив всех имен вкладок, просим вывести нужную по порядку (если знаем)
    browser.switch_to.window(new_window) #переход на новую вкладку, в скобках имя новой вкладки

    X = browser.find_element(By.CSS_SELECTOR, "[id='input_value']") # находим элемент x на странице
    x = X.text
    y = calc(x) # посчитать формулу
    print(y)

    Answer = browser.find_element(By.CSS_SELECTOR, "[id='answer']") # передать в поле значение
    Answer.send_keys(y)

    button2 = browser.find_element(By.CSS_SELECTOR, ".btn.btn-primary") # клацнуть на кнопку
    button2.click()

finally:
    # ожидание чтобы визуально оценить результаты прохождения скрипта
    time.sleep(10)
    # закрываем браузер после всех манипуляций
    browser.quit()
    # пустая строка

#ALERT
#alert('Hello!');

#согласиться с ошибкой в модальном окне, чтобы закрыть его
#переходим на окно и соглашаемся
#alert = browser.switch_to.alert
#alert.accept()
#confirm.dismiss() #отказаться в модальном окне

#Чтобы получить текст из alert, используйте свойство text объекта alert:
#alert = browser.switch_to.alert
#alert_text = alert.text

#чтобы ввести текст в модальное окно
#prompt = browser.switch_to.alert
#prompt.send_keys("My answer")
#prompt.accept()

#BROWSER_WINDOW
#browser.switch_to.window(window_name) #переход на новую вкладку, в скобках имя новой вкладки
#new_window = browser.window_handles[1] #узнаем имя вкладки, через массив всех имен вкладок, просим вывести нужную по порядку (если знаем)
#first_window = browser.window_handles[0] #имя текущей вкладки, для дальнейшей работы с ней

#    X = browser.find_element(By.CSS_SELECTOR, "[id='input_value']") # находим элемент x на странице
#    x = X.text
#    y = calc(x) # посчитать формулу
#    print(y)
#    browser.execute_script("window.scrollTo(0, 180)") # прокрутить страницу на заданное кол-во пикселей

#    FirstName = browser.find_element(By.CSS_SELECTOR, "[placeholder='Enter first name']") # передать в поле значение
#    FirstName.send_keys("Artem")

#вставить файл
#    current_dir = os.path.abspath(os.path.dirname(__file__))  # получаем путь к директории текущего исполняемого файла
#    file_name = "for_add_txt_file.txt"
#    file_path = os.path.join(current_dir, file_name)  # добавляем к этому пути имя файла
#    element = browser.find_element(By.CSS_SELECTOR, "[id='file']")
#paste_manual    element.send_keys("C:\\Users/Minchenko_KL\\PycharmProjects\\Stepik\\venv\\for_add_txt_file.txt")
#paste_auto    element.send_keys(file_path)

#    StepSix = browser.find_element(By.CSS_SELECTOR, "[for='robotCheckbox']") #клацнуть на чекбокс
#    StepSix.click()

#    StepSeven = browser.find_element(By.CSS_SELECTOR, "[for='robotsRule']")  # клацнуть на радиокнопку
#    StepSeven.click()

#    button = browser.find_element_by_tag_name("button")
#    browser.execute_script("return arguments[0].scrollIntoView(true);", button)
#    button.click()

#    select = Select(browser.find_element(By.TAG_NAME, "select"))
#    select.select_by_visible_text(str(x))


class Leg:
	def __init__(self, start_coords, end_coords, mode, service, additional_data=None):
		'''
		start_coords: [<longitude>, <latitude>]
		end_coords: [<longitude>, <latitude>]
		mode: (string) one of 'walking', 'bicycling', 'driving'
		'''
		self.start_coords = start_coords
		self.end_coords = end_coords
		self.mode = mode
		self.service = service
		self.cost = None
		self.duration = None
		self.additional_data = additional_data


import os
import sys

from PyQt5.QtWidgets import QPushButton, QProgressBar, QFileDialog, QHBoxLayout

# class responsible for choosing and loading file containing data used later
class FileLoader(QHBoxLayout):

    def __init__(self, btn_name, error_display_method, set_filepath_method):
        super().__init__()

        self.__error_display_method = error_display_method
        self.__set_filepath_method = set_filepath_method
        self.__create_all(btn_name)

    def __create_all(self, btn_name="Select file", parent=None):
        self.__file_loader_dialog_btn = self.__create_file_loader_dialog_btn(btn_name)
        self.__loader = QProgressBar(parent)

        self.addWidget(self.__file_loader_dialog_btn)
        self.addWidget(self.__loader)

    def __create_file_loader_dialog_btn(self, btn_name):
        loader_btn = QPushButton(btn_name)
        loader_btn.clicked.connect(self.__choose_and_read_file)

        return loader_btn

    # method responsible for choosing file and displaying loading progress
    def __choose_and_read_file(self):
        self.__loader.setValue(1)
        parent = None
        # sets current directory as default one
        current_dir = os.path.dirname(sys.argv[0])
        self.__loader.setValue(10)
        # changes default type of file explorator
        options = QFileDialog.DontUseNativeDialog
        self.__loader.setValue(20)
        # opens window in which user can choose file
        self.maybe_selected_file, _ = QFileDialog.getOpenFileName(parent, "Choose csv file",
                                                                  current_dir, "CSV (*.csv)", options=options)
        self.__loader.setValue(30)
        if self.maybe_selected_file:
            self.__loader.setValue(40)
            self.__loader.setValue(50)
            # returns selected filepath
            self.__set_filepath_method(self.maybe_selected_file)
            return

        else:
            self.__error_display_method("Error: file loading aborted")
            self.__loader.setValue(1)

    def set(self, val):
        self.__loader.setValue(val)


import os

path = "C:\\Users\\Benjamin\\Desktop\\BensFolder\\DEV\\Candco\\CPP\\Projects\\YouTubeCommentsGraph\\CPP\\"

os.chdir(path)

f = open('logs.txt')
lines = f.readlines()
f.close()

def endsWith(subject, pattern):
    return subject[-len(pattern):] == pattern

linesLen = len(lines)
for linesIndex in range(linesLen):
    line = lines[linesIndex]
    if line[-1] == '\n':
        line = line[:-1]
    if endsWith(line, " - 0"):
        print(line)


import cv2
import numpy as np
import pandas as pd
import os
import re

#同一品牌的两期图片链接数据判断，以图片链接为判断标准，分新上线和已下架

#获取文件名称
def get_filename(path):
    files = os.listdir(path)
    return files

#产品包装判断
def pic_sx_judge(filepath1,filepath2,name):
    df1 =  pd.read_excel(filepath1+name)
    df2 =  pd.read_excel(filepath2+name)
    df_same = pd.DataFrame(columns=df2.columns)
    df_diff = pd.DataFrame(columns=df2.columns)
    df_same['图片链接_old'] = np.NaN
    df_diff['图片链接_old'] = np.NaN
    df_same['页面网址_old'] = np.NaN
    df_diff['页面网址_old'] = np.NaN
    os.chdir(filepath4)

    for i in range(len(df2)):
        if re.search('.tmall',df2.loc[1,'页面网址']):

            url = re.search('id=(\d+)',df2.loc[i,'页面网址']).group(1)
            pic1 = df2.loc[i, '图片链接']
            for j in range(len(df1)):
                if url in df1.loc[j, '页面网址']:
                    pic2 = df1.loc[j, '图片链接']
                    try:
                        image1 = cv2.imread(pic1)
                        image2 = cv2.imread(pic2)
                        difference = cv2.subtract(image1, image2)
                        result = not np.any(difference)  # if difference is all zeros it will return False
                        if result is True:
                            df_same.loc[i, '图片链接_old'] = df1.loc[j, '图片链接']
                            df_same.loc[i, '页面网址_old'] = df1.loc[j, '页面网址']
                            df_same.loc[i, df2.columns] = df2.loc[i]

                        else:
                            df_diff.loc[i, '图片链接_old'] = df1.loc[j, '图片链接']
                            df_diff.loc[i, '页面网址_old'] = df1.loc[j, '页面网址']
                            df_diff.loc[i, df2.columns] = df2.loc[i]
                    except:
                        df_diff.loc[i, '图片链接_old'] = df1.loc[j, '图片链接']
                        df_diff.loc[i, '页面网址_old'] = df1.loc[j, '页面网址']
                        df_diff.loc[i, df2.columns] = df2.loc[i]
        else:
            url = df2.loc[i, '页面网址']
            pic1 = df2.loc[i, '图片链接']
            for j in range(len(df1)):
                if url in df1.loc[j, '页面网址']:
                    pic2 = df1.loc[j, '图片链接']
                    try:
                        image1 = cv2.imread(pic1)
                        image2 = cv2.imread(pic2)
                        difference = cv2.subtract(image1, image2)
                        result = not np.any(difference)  # if difference is all zeros it will return False
                        if result is True:
                            df_same.loc[i, '图片链接_old'] = df1.loc[j, '图片链接']
                            df_same.loc[i, '页面网址_old'] = df1.loc[j, '页面网址']
                            df_same.loc[i, df2.columns] = df2.loc[i]

                        else:
                            df_diff.loc[i, '图片链接_old'] = df1.loc[j, '图片链接']
                            df_diff.loc[i, '页面网址_old'] = df1.loc[j, '页面网址']
                            df_diff.loc[i, df2.columns] = df2.loc[i]
                    except:
                        df_diff.loc[i, '图片链接_old'] = df1.loc[j, '图片链接']
                        df_diff.loc[i, '页面网址_old'] = df1.loc[j, '页面网址']
                        df_diff.loc[i, df2.columns] = df2.loc[i]
    return (df_same,df_diff)

#商品上新判断
def new_judge(filepath1,filepath2):
    word = ['天猫/', '京东/']
    #word = ['京东/']
    for nm  in word:
        filename2 = get_filename(filepath2 + nm + '提取数据1/')
        print(filename2)
        for name in filename2:
            path1 = filepath1 + nm + '提取数据1/'
            path2 = filepath2 + nm + '提取数据1/'
            df = pic_sx_judge(path1, path2,name)
            if os.path.isfile(filepath2 + nm + '图片判断' + '/' + '相同图片/'+ name):
                os.remove(filepath2 + nm + '图片判断' + '/' + '相同图片/'+ name)
            if os.path.isfile(filepath2 + nm + '图片判断' + '/' + '不同图片/'+ name):
                os.remove(filepath2 + nm + '图片判断' + '/' + '不同图片/'+ name)
            df[0].to_excel(filepath2 + nm + '图片判断' + '/' + '相同图片/'+ name, index=False)
            df[1].to_excel(filepath2 + nm + '图片判断' + '/' + '不同图片/'+ name, index=False)
'''def match_pic_url(name1,name2,name3,num1=0, num2=1):
    df_right1 = pd.read_excel(name1)
    df_right2 = pd.read_excel(name2)
    df1 = pd.read_excel(name3,sheetname=num1)
    df2 = pd.read_excel(name3, sheetname=num2)
    writer = pd.ExcelWriter(name3)
    old_id = []
new_id = []
[old_id.append(re.search('id=(\d+)',df_right1.loc[i,'链接']).group(1))for i in range(len(df_right1))]
[new_id.append(re.search('id=(\d+)', df1.loc[i, '页面网址']).group(1)) for i in range(len(df1))]
    for i in range(len(df1)):
        url_id = re.search('id=(\d+)',df_right1.loc[i,'链接']).group(1)
        for j in range(len(df_right1)):
            if url_id in df_right1.loc[j,'链接']:
                df1.loc[i,'图片链接'] = df_right1.loc[j,'图片链接']
    for i in range(len(df2)):
        for j in range(len(df_right2)):
            if df2.loc[i,'页面网址'] == df_right2.loc[j,'链接']:
                df2.loc[i,'图片链接'] = df_right2.loc[j,'图片链接']
    if os.path.isfile(name3):
        os.remove(name3)
    df1.to_excel(writer, index=False, sheet_name='天猫')
    df2.to_excel(writer,index=False,sheet_name = '京东')
    writer.save()'''
def match_pic_url_single(name1,name3,name):
    df_right = pd.read_excel(name1)
    df = pd.read_excel(name3)
    writer = pd.ExcelWriter(name3)
    if len(df_right)>0:
        if re.search('tmall', df.loc[0, '页面网址']):
            for i in range(len(df)):
                print(df.loc[i, '页面网址'])
                id = re.search('id=(\d+)', df.loc[i, '页面网址']).group(1)
                for j in range(len(df_right)):
                    if id in df_right.loc[j,'链接'] :
                        df.loc[i, '图片链接'] = df_right.loc[j, '图片链接']
                        #print(df_right.loc[j, '图片链接'])
        else:
            for i in range(len(df)):
                for j in range(len(df_right)):
                    if df.loc[i, '页面网址'] == df_right.loc[j, '链接']:
                        df.loc[i, '图片链接'] = df_right.loc[j, '图片链接']
                        #print(df_right.loc[j, '图片链接'])
        if os.path.isfile(name3):
            os.remove(name3)
        df.to_excel(writer, index=False, sheet_name=name)
        writer.save()
def match_data(date):
    word = ['天猫/', '京东/']
    #word = ['京东/']
    for name  in word:
        filename1 = get_filename(filepath2+name+'提取数据1/')
        for name1 in filename1:
            print(name1)
            match_pic_url_single(filepath4 + date + name + name1,filepath2+ name + '提取数据1/' + name1,name.split('/')[0])
            #match_pic_url_single(filepath4 + '1.30/' + name + name1, filepath2 + name + '提取数据1/' + name1, name.split('/')[0])

# filepath1 = r'E:/01复硕正态/08项目/01沐浴露监测/02成品数据/旗舰店数据/2018-2-28/'
# filepath2 = r'E:/01复硕正态/08项目/01沐浴露监测/02成品数据/旗舰店数据/2018-3-31/'
# filepath4 = r'E:\A_judge_pic\2017-11-15/'

filepath1 = r'G:\01复硕正态\08项目\04中华项目监测\02成品数据\旗舰店数据\2018-3-31/'
filepath2 = r'G:\01复硕正态\08项目\04中华项目监测\02成品数据\旗舰店数据\2018-4-13/'
filepath4 = r'G:\A_judge_pic\toothpaste/'
if __name__ == '__main__':
    match_data('4.13/')
    #new_judge(filepath1, filepath2)


#----------------Simple Crptocurrency example------------------------
#   Everytime a miner mine the pending transactions (add them to a new block and passing proof of work)
#   his reward is added to newly created pending transaction array with his toAddress and reward amount
import hashlib
import datetime

class Transaction:
    def __init__(self,fromAddress,toAddress,Amount):
        self.fromAddress=fromAddress
        self.toAddress=toAddress
        self.Amount=Amount

class Block:
    def __init__(self,date_time,Transactions,previoushash):
        self.date_time=date_time
        self.Transactions=Transactions
        self.previoushash=previoushash
        self.count=0
        self.currenthash=self.calculatehash()
    def calculatehash(self):
        string=str(self.date_time)+str(self.Transactions)+str(self.previoushash)+str(self.count)
        return  hashlib.sha256(string.encode()).hexdigest()
    def mine(self,difficulty):
        x="0"*difficulty
        current_hash=self.currenthash
        while(current_hash[0:difficulty]!=x):
            self.count+=1
            self.current_hash=self.calculatehash()

class Blockchain:
    def __init__(self,GenesisTransaction,difficulty):
        date_time=datetime.datetime.now()
        self.chain=[Block(date_time,GenesisTransaction,-1)]
        self.difficulty=difficulty
        self.PendingTransactions=[]
        self.miningrewards=100
    def Getlatest(self):
         return self.chain[len(self.chain)-1]
#  mining the list of pending transactions and adding to the chain
#  passing the miners address to add his reward to the new pending transaction list (with his address and reward amount)
    def mineBlock(self,MinerAddress):
        date_time=datetime.datetime.now()
        lasthash=self.Getlatest().currenthash
        newblock=Block(date_time,self.PendingTransactions,lasthash)
        newblock.mine(self.difficulty)
        print("Congo !!! -- A new block mined--")
        print("YIPPEEEEEE!!! -------------Miner rewarded------------")
        print("...Miner's reward will be added once a new block containing his reward is mined")
        print()
        self.chain.append(newblock)
#  adding the miners reward to new pending transaction with his address and reward amount
        self.PendingTransactions=[Transaction(None,MinerAddress,self.miningrewards)]
    def CreateTransaction(self,transaction):
        self.PendingTransactions.append(transaction)
#   Balance of an address is checked by traversing the complete chain and cheching
#   every transaction object in the transaction array of each block
    def GetBalanceofAddress(self,address):
        balance=0
        for block in self.chain:
            for transaction in block.Transactions:
                if(transaction.fromAddress==address):
                    balance-=transaction.Amount
                elif(transaction.toAddress==address):
                    balance+=transaction.Amount
        return balance
    def isValid(self):
        for i in range(1,len(self.chain)):
            current=self.chain[i]
            previous=self.chain[i-1]
            x=current.count #changing the temp to 0 just to validate that currently hash is same as when the block was created because temp value is changed while mining
            current.count=0
            if(current.previoushash!=previous.currenthash):
                return False
            if(current.currenthash!=current.calculatehash()):
                return False
        current.count=x #changing back the temp to its mined value, if some one wants to know how many times loop ran(computation power)
        return True
#   Genesis block with new Genesis transaction
GenesisTransactions=[Transaction(None,"Genesis",2000)]
mychain=Blockchain(GenesisTransactions,4)

mychain.CreateTransaction(Transaction("one","two",1000))
mychain.CreateTransaction(Transaction("two","three",2000))
mychain.CreateTransaction(Transaction("three","four",3000))

mychain.mineBlock("sumit")
#  after mining a new block with above pending transactions miner("Sumit") got a reward
#  that is saved in a new pending transaction array
print("Balance of --Sumit-- >",mychain.GetBalanceofAddress("sumit"))
print("______________________________________________________________")
print()
#  the balance is 0 as the transactions array with his reward is not yet added to the chain
#  once the pending record with his reward is mined by someone his balance will increase.

mychain.CreateTransaction(Transaction("here","there",2400))
mychain.CreateTransaction(Transaction("here","there",6000))
mychain.mineBlock("sumit")
print("Balance of --Sumit-- >",mychain.GetBalanceofAddress("sumit"))
#  ----------chceck validity-------
print()
print("______________________________")
print()
print("valid : ",mychain.isValid())


"""Utility for sending signed transactions to an Account on Starknet."""

# Source: https://github.com/perama-v/GoL2/blob/main/tests/utils/Signer.py
# Commit Hash: a5c05c15be1569730da04fc4f1f2e89be38c69be
# License: MIT

from starkware.crypto.signature.signature import private_to_stark_key, sign
from starkware.starknet.public.abi import get_selector_from_name
from starkware.cairo.common.hash_state import compute_hash_on_elements

class Signer():
    """
    Utility for sending signed transactions to an Account on Starknet.
    Parameters
    ----------
    private_key : int
    Examples
    ---------
    Constructing a Singer object
    >>> signer = Signer(1234)
    Sending a transaction
    >>> await signer.send_transaction(account,
                                      account.contract_address,
                                      'set_public_key',
                                      [other.public_key]
                                     )
    """

    def __init__(self, private_key):
        self.private_key = private_key
        self.public_key = private_to_stark_key(private_key)

    def sign(self, message_hash):
        return sign(msg_hash=message_hash, priv_key=self.private_key)

    async def send_transaction(self, account, to, selector_name, calldata, nonce=None):
        if nonce is None:
            execution_info = await account.get_nonce().call()
            nonce, = execution_info.result

        selector = get_selector_from_name(selector_name)
        message_hash = hash_message(
            account.contract_address, to, selector, calldata, nonce)
        sig_r, sig_s = self.sign(message_hash)

        return await account.execute(to, selector, calldata, nonce).invoke(signature=[sig_r, sig_s])

def hash_message(sender, to, selector, calldata, nonce):
    message = [
        sender,
        to,
        selector,
        compute_hash_on_elements(calldata),
        nonce
    ]
    return compute_hash_on_elements(message)


from flask_main import app,db
from flask import render_template,url_for,redirect
from models import Task
from datetime import datetime
import forms

@app.route("/")
def index():
    return render_template('index.html', title='Home')

@app.route("/chatbot")
def chat():
    return render_template('chatbot.html', title='Home')

@app.route("/sampleform",methods=['get','post'])
def sampleform():
    form=forms.SampleForm()
    if form.validate_on_submit():
        t=Task(title=form.title.data,date=datetime.utcnow())
        db.session.add(t)
        db.session.commit()
        return redirect(url_for('sampleform_output'))
    return render_template("sampleform.html",form=form)

@app.route("/sampleform_out")
def sampleform_output():
    t=Task.query.all()
    return render_template('sampleform_out.html',tasks=t)

@app.route("/edit/<int:task_id>",methods=['get','post'])
def edit(task_id):
    task=Task.query.get(task_id)
    form=forms.SampleForm()
    if task:
        if form.validate_on_submit():
            task.title=form.title.data
            task.date=datetime.utcnow()
            db.session.commit()
            return redirect(url_for("sampleform_output"))
        form.title.data=task.title
        return render_template("edit.html",form=form,task_id=task_id)
    return redirect(url_for("sampleform_output"))

@app.route("/delete/<int:task_id>",methods=['get','post'])
def delete(task_id):
    task=Task.query.get(task_id)
    if task:
            db.session.delete(task)
            db.session.commit()
            return redirect(url_for("sampleform_output"))
    return redirect(url_for("sampleform_output"))


import os
from PIL import Image
import torch
from torchvision.transforms import functional as F

class UnlabeledDataset(torch.utils.data.Dataset):
    def __init__(self, root, transform):
        r"""
        Args:
            root: Location of the dataset folder, usually it is /unlabeled
            transform: the transform you want to applied to the images.
        """
        self.transform = transform
        self.image_dir = root
        self.filelist = os.listdir(self.image_dir)
        problem_img = ['15460.PNG','151438.PNG', '158432.PNG']
        for p in problem_img:
            if p in self.filelist:
                self.filelist.remove(p)
        self.num_images = len(self.filelist)

    def __len__(self):
        return self.num_images

    def __getitem__(self, idx):
        # the idx of unlabeled image is not consecutive
        with open(os.path.join(self.image_dir, self.filelist[idx]), 'rb') as f:
            try:
                img = Image.open(f).convert('RGB')
            except OSError:
                print(self.filelist[idx],'caused error')
        return F.to_tensor(img)

def main():
    #device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')

    unlabeled_data = UnlabeledDataset(root = '/unlabeled', transform = None)
    data_loader = torch.utils.data.DataLoader(unlabeled_data, batch_size=1024,  num_workers=4)

    mean = 0.
    std = 0.
    nb_samples = 0.
    for data in data_loader:
        #data: batch x 3 x 244 x 244// not using the collate fn by prof
        batch_samples = data.size(0)
        data = data.view(batch_samples, data.size(1), -1)
        mean += data.mean(2).sum(0)
        std += data.std(2).sum(0)
        nb_samples += batch_samples
        if nb_samples % 1000 == 0:
            print(nb_samples,'samples succeeded!')

    mean /= nb_samples
    std /= nb_samples
    print('mean: ', mean, 'std: ', std)

if __name__ == "__main__":
    main()


#%%  Imports
import os.path as osp
from skimage import io
from skimage.transform import resize
import numpy as np
import pandas as pd
from tqdm import tqdm
from PIL import Image, ImageDraw, ImageFont
from random import shuffle, seed

#%%  Calculate sizes and resolutions

path = r'c:\dev\a2_contact'
images_grid = ( 4, 8 )
seeds = ( 448, 444 )
im_dim = ( 420, 320 )
border_ratio = ( .8, .5 )

a4_mm = ( 210, 297 )
full_image_pixels = tuple( 10 * x for x in a4_mm )

im_diff = ( 40, 40 )
im_border = [0,0]

for i in [0,1]:
    im_border[i] = int( border_ratio[i] * (full_image_pixels[i] - ( im_dim[i] * images_grid[i] + im_diff[i] * (images_grid[i] - 1) ))  )

#%%  Some font issues calculations
chars = 1488 + np.arange( 27 )
char_length = ( 20, 50, 90, 50, 20, 100, 80, 20, 20, 100, 50, 50, 60, 10, 20, 100, 100, 20, 60, 50, 60, 60, 40, 20, 50, 10, 20 )
clm = dict( zip( chars, map( lambda x:(190 - x)/10, char_length )) )
chars = ord( '0' ) + np.arange( 10 )
clm.update( zip( chars, 10 * [ 13 ] ) )
clm[ ord( ' ' ) ] = 7
clm[ ord( '.' ) ] = 7
clm[ ord( '-' ) ] = 10

#%%  Read from excel
df = pd.ExcelFile( osp.join( path, r'contacts.xlsx' ) ).parse( 'Sheet2' )
contacts = []
contact_items = ( 'image', 'name', 'date', 'ima_name', 'ima_num', 'aba_name', 'aba_num' )
for i, row in df.iterrows():
    contacts.append( dict( zip( contact_items, [ row[ x ] for x in contact_items ] ) ) )

#%%  Reading contact images
for c in tqdm( contacts ):
    c[ 'im_data' ] = resize( io.imread( osp.join( path, c['image'] ) ), ( im_dim[1], im_dim[1] ) )

#%%  shuffle contacts
for s in seeds:
    seed( s )
    shuffle( contacts )

#%%  generating text image
def GetTxtAndMaxLength( c ):
    lines = [ c['date'] + ' ' + c['name'][-1::-1],
              c['ima_num'] + ' ' + c['ima_name'][-1::-1] ]
    if isinstance( c['aba_name'], str ):
        lines.append( c['aba_num'] + ' ' + c['aba_name'][-1::-1] )
    max_length = np.max( [ sum( [ clm[ord(x)] for x in txt ] ) for txt in lines ] )
    return '\n'.join( lines ), max_length

def EmbedTxtToImg( im, c ):
    pil_im = Image.fromarray( ( 255 * im ).astype( np.uint8 ) )
    fnt = ImageFont.truetype( font='david.ttf', size=30, index=0, encoding='unic', layout_engine=ImageFont.LAYOUT_RAQM )
    txt, max_length = GetTxtAndMaxLength( c )
    draw = ImageDraw.Draw( pil_im )
    draw.multiline_text( ( im.shape[1] - max_length - 4, 4 ), txt, fill=(255,255,0), font=fnt, spacing=4, align="right" )
    return np.array( pil_im ).astype( np.float ) / 255

#%% create the final image
full_im = resize( io.imread( osp.join( path, r'school-rocket-ship.jpeg' )), ( full_image_pixels[0], full_image_pixels[1] ) )
for (y,x), c in tqdm( zip( np.ndindex( images_grid ), contacts ) ):
    y_start = y * (im_dim[0] + im_diff[0]) + im_border[0]
    y_end = y_start + im_dim[1]
    x_start = x * (im_dim[1] + im_diff[1]) + im_border[1]
    x_end = x_start + im_dim[1]
    full_im[ y_start : y_end, x_start : x_end ] = contacts[ y * images_grid[1] + x ][ 'im_data' ]
    y_text_start = y_end
    y_text_end = y_text_start + im_dim[0] - im_dim[1]
    full_im[ y_text_start : y_text_end, x_start : x_end ] = EmbedTxtToImg( .4 * full_im[ y_text_start : y_text_end, x_start : x_end ], c )

#%% save to file
io.imsave( osp.join( path, 'contacts.png' ), full_im )


import random
import sys
from read import readInput
from write import writeOutput
import time
from host import GO

class Manual_Player():
    def __init__(self):
        self.type = 'random'

    def get_input(self):
        print("Entire action")
        x = input("x:")
        y = input("y:")
        action = (int(x),int(y))
        return action

if __name__ == "__main__":
    N = 5
    begin = time.time()
    player = Manual_Player()
    print("Entire action")
    x = input("x:")
    y = input("y:")
    action = (x,y)
    writeOutput(action)
    print("Time taken by rando = ", time.time() - begin)


def begin():
    print('igra krestiki - noliki')
    print()
    for j in m:
        print(*j)
    print()
    global czsel1, czsel2, player1, player2
    player1 = input('vvedi imya  ')
    input_cz()
    czsel1 = val
    player2 = input('vtoroi igrok vvedi imya ')
    if czsel1 == 'x':
        czsel2 = "o"
    else:
        czsel2 = "x"
    print("____________________________________________")
    print(f'pervi igrok "{player1}" vibral "{czsel1}"')
    print(f' vtoromu igroku "{player2}" dostalsa "{czsel2}"')

def input_cz(): # check input cross or zero only
    global val
    val = input('nazhmi krestik "x" ili nolik "o"  ')
    if val == 'x' or val == 'o':
        return val
    else:
        print('nevernii vibor')
        input_cz()

m = [  # base matrix
    [0, 1, 2, 3],
    [1, '_', '_', '_'],
    [2, '_', '_', '_'],
    [3, '_', '_', '_']
]
cross = [  # clear matrix for "cross"
    [0, 1, 2, 3],
    [1, '_', '_', '_'],
    [2, '_', '_', '_'],
    [3, '_', '_', '_']
]
zero = [  # clear matrix for "zero"
    [0, 1, 2, 3],
    [1, '_', '_', '_'],
    [2, '_', '_', '_'],
    [3, '_', '_', '_']
]
win1 = [  # win combination
    [0, 1, 2, 3],
    [1, 'w', '_', '_'],
    [2, 'w', '_', '_'],
    [3, 'w', '_', '_']
]
win2 = [
    [0, 1, 2, 3],
    [1, 'w', 'w', 'w'],
    [2, '_', '_', '_'],
    [3, '_', '_', '_']
]
win3 = [
    [0, 1, 2, 3],
    [1, 'w', '_', '_'],
    [2, '_', 'w', '_'],
    [3, '_', '_', 'w']
]
win4 = [
    [0, 1, 2, 3],
    [1, '_', 'w', '_'],
    [2, '_', 'w', '_'],
    [3, '_', 'w', '_']
]
win5 = [
    [0, 1, 2, 3],
    [1, '_', '_', 'w'],
    [2, '_', '_', 'w'],
    [3, '_', '_', 'w']
]
win6 = [
    [0, 1, 2, 3],
    [1, '_', '_', 'w'],
    [2, '_', 'w', '_'],
    [3, 'w', '_', '_']
]
win7 = [
    [0, 1, 2, 3],
    [1, '_', '_', '_'],
    [2, 'w', 'w', 'w'],
    [3, '_', '_', '_']
]
win8 = [
    [0, 1, 2, 3],
    [1, '_', '_', '_'],
    [2, '_', '_', '_'],
    [3, 'w', 'w', 'w']
]

def clear():  # clear all matrix
    global m, zero, cross
    m = [
        [0, 1, 2, 3],
        [1, '_', '_', '_'],
        [2, '_', '_', '_'],
        [3, '_', '_', '_']
    ]
    cross = [  # clear matrix for "cross"
        [0, 1, 2, 3],
        [1, '_', '_', '_'],
        [2, '_', '_', '_'],
        [3, '_', '_', '_']
    ]
    zero = [  # clear matrix for "zero"
        [0, 1, 2, 3],
        [1, '_', '_', '_'],
        [2, '_', '_', '_'],
        [3, '_', '_', '_']
    ]

def check_win():  # proverka viigrisha (check winner)
    mm = (win1, win2, win3, win4,  # create matrix in matrix
          win5, win6, win7, win8)
    for ff in mm:  # cheking!!!
        if cross == ff:  # who is winner!?
            cross_win()
        elif zero == ff:  # who is winner!?
            zero_win()

def zero_win():  # see who is winner? player1 or player2
    if czsel1 == 'o':
        print()
        print(f'igrok {player1} viigral')
        pobeda()
    elif czsel2 == 'o':
        print()
        print(f'igrok {player2} viigral')
        pobeda()

def cross_win():  # see who is winner? player1 or player2
    if czsel1 == 'x':
        print()
        print(f'igrok {player1} viigral')
        pobeda()
    elif czsel2 == 'x':
        print()
        print(f'igrok {player2} viigral')
        pobeda()

def nichya():  # when we stop game
    ze = cr = 0  # ze - zero, cr - cross
    for i in m: #see matrix "m" to cross and zero
        for j in i:
            if j == 'o':
                ze += 1  # summ of zero
            elif j == 'x':
                cr += 1  # summ of cross
    if ((ze == 5) and (cr == 4)) or ((ze == 4) and (cr == 5)):  # see what a bigger?
        print('Voznikla nichya, nachnite novuyou igru')
        clear()
        begin()
        base_game()
        # exit() # if need

def pobeda():  # deistvia pri pobede
    print()
    print("END")
    print()
    for j in m:
        print(*j)  # any do
    # clear() #any do
    exit()  # any do

def input_z():  # input coordinates
    global z
    z = int(input('vvedi nomer stolbca ot 1 do 3  '))
    if 1 <= z <= 3:
        return z
    else:
        print('bud vnimatelen! stolbcov vsego 3 ')
        input_z()

def input_y():  # input number of row
    global y
    y = int(input('vvedi nomer stroki  '))
    if 1 <= y <= 3:
        return y
    else:
        print('bud vnimatelen! strok vsego 3 ')
        input_y()

def base_game():
    global count
    count = 1
    while count <= 9:
        print()
        for j in m:  # print matrix of cross zero
            print(*j)
        print()
        print(f'ХОД {count}')
        print()
        input_z()  # vvod indeksa
        input_y()  # vvod indeksa
        input_cz()  # input cross or zero
        # proverka ocheredi (first player or second?)
        if (count + 2) % 2 == 1 and val == czsel1:
            game()
        elif (count + 2) % 2 == 0 and val == czsel2:
            game()
        else:
            print('dolzhen bit drugoi simvol, ili ochered ne ta')

def game():
    global count
    if cross[y][z] == "w" or zero[y][z] == 'w':  # check clear cell of matrix
        print()
        print("!!! AUCHTUNG !!! uzhe zanyto, viberi druguiu kletku")
    else:
        m[y][z] = val
        if m[y][z] == "x":  # create cross - zero matrix
            cross[y][z] = "w"
        elif m[y][z] == "o":
            zero[y][z] = "w"
        nichya()
        check_win()
        count += 1
        # base_game()

begin()
base_game()


# 상위 카테고리를 검색하는 기능
#
# @param		inputData		입력데이터(카테고리 정보)
# @param		categories		입력데이터(inputCategories[0]: 카테고리1, inputCategories[1]: 카테고리2)
# @return						상위 카테고리
def getTopCategory(inputData, categories):
    topCategory = ""
    ########################여기부터 구현 (1) ---------------->
    treePath = makeTreePath(inputData)
    firstCategoryPath = []
    secondCategoryPath = []
    for i in range(len(categories)):
        category = categories[i]
        if i == 0:
            for strList in treePath:
                if category in strList:
                    firstCategoryPath = strList
                    break
        else:
            for strList in treePath:
                if category in strList:
                    secondCategoryPath = strList
                    break

    firstIndex = firstCategoryPath.index(categories[0])
    secondIndex = secondCategoryPath.index(categories[1])
    tmpPath = []
    for i in range(firstIndex):
        for j in range(secondIndex):
            if firstCategoryPath[i] == secondCategoryPath[j]:
                tmpPath.append(firstCategoryPath[i])
    topCategory = tmpPath[len(tmpPath) - 1]
    ############################# <-------------- 여기까지 구현 (1)
    return topCategory

# 하위 카테고리의 개수를 계산하는 기능
#
# @param		inputData		입력데이터(카테고리 정보)
# @param		categoryStr		입력데이터(카테고리)
# @return						하위 카테고리의 개수
def getNumberOfSubcategories(inputData, categoryStr) :
    numberOfSubcategories = 0
    ########################여기부터 구현 (2) ---------------->
    treePath = makeTreePath(inputData)
    path = []
    for strList in treePath:
        if categoryStr in strList:
            path = strList
            break
    parentNode = path[path.index(categoryStr)-1]
    childNodes = []
    for strList in treePath:
        if parentNode in strList:
            index = strList.index(parentNode)+1
            for i in range(index, len(strList)):
                if strList[i] not in childNodes:
                    childNodes.append(strList[i])
    numberOfSubcategories = len(childNodes)
    ############################# <-------------- 여기까지 구현 (2)
    return numberOfSubcategories

# 트리를 만드는 기능(솔루션용 기능, 제공파일에 없음)
#
# @param		inputData		입력데이터(카테고리 정보)
# @return						트리 경로
def makeTreePath(inputData):
    mindMapPath = []
    parentToChildMindMap = {}
    allNodeList = []
    for strArr in inputData:
        key = strArr[0]
        value = strArr[1]
        if key not in allNodeList:
            allNodeList.append(key)
        if value not in allNodeList:
            allNodeList.append(value)
        lowerPath = []
        if key in parentToChildMindMap.keys():
            lowerPath = parentToChildMindMap.get(key)
            lowerPath.append(value)
        else:
            lowerPath.append(value)
        parentToChildMindMap[key] = lowerPath

    childToParentMindMap = {}
    for strArr in inputData:
        key = strArr[1]
        value = strArr[0]
        childToParentMindMap[key] = value

    rootNode = ""
    for node in allNodeList:
        if node not in childToParentMindMap.keys():
            rootNode = node
    allNodeList.append(rootNode)

    leafNode = []
    for node in allNodeList:
        if node not in parentToChildMindMap.keys():
            leafNode.append(node)

    for key in leafNode:
        tmpKey = key
        path = []
        while (True):
            path.append(tmpKey)
            tmpKey = childToParentMindMap.get(tmpKey)
            if tmpKey == rootNode:
                path.append(rootNode)
                path.reverse()
                mindMapPath.append(path)
                break
    return mindMapPath


from setuptools import find_packages, setup

base_dependencies = [
    "pandas>=1.0.0",
    "python-dateutil>=2",
]

additional_dependencies = {
    "dev": [
        "black>=21.9b0",
        "pre-commit>=2.15.0",
        "pytest>=6.2.1",
        "pylint>=2.7.4",
        "jupyterlab",
        "twine",
        "seaborn",
    ],
}

VERSION = "0.0.1"
DESCRIPTION = "R original utilities now in python"

with open("README.md", "r", encoding="utf8") as fh:
    LONG_DESCRIPTION = fh.read()

setup(
    name="rutil",
    packages=find_packages(where="rutil"),
    package_dir={"": "rutil"},
    description=DESCRIPTION,
    long_description=LONG_DESCRIPTION,
    long_description_content_type="text/markdown",
    author="Ismael Cabral",
    version="0.0.2",
    py_modules=["rutil"],
    keywords=["R language", "R", "str()", "pandas", "struture"],
    install_requires=base_dependencies,
    extras_require=additional_dependencies,
    python_requires=">=3.6",
)


from django.urls import path,re_path,include
# yazdigimiz functions imporr edirk
from .views import register,activate,login,logout

app_name="account"

urlpatterns = [
    path('register/', register, name='register'),
    re_path(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,33})/$',activate, name='activate'),
    path('login/',login , name='login'),
    path('logout/',logout , name='logout'),

]


import argparse
import time
import logging
import json
import csv
import typing
from datetime import datetime
from constant import tables, table_schema2, table_schema
import apache_beam as beam
from apache_beam.io import fileio
from apache_beam.options.pipeline_options import GoogleCloudOptions
from apache_beam.options.pipeline_options import PipelineOptions
from apache_beam.options.pipeline_options import StandardOptions
from apache_beam.options.pipeline_options import DebugOptions
from apache_beam.transforms.trigger import AfterWatermark, AfterCount, AfterProcessingTime, AccumulationMode
from apache_beam.transforms.combiners import CountCombineFn
from apache_beam.runners import DataflowRunner, DirectRunner
#from IPython import embed

# Table schema for BigQuery
"""table_schema = {
    'fields':[
        {
            "name": "name",
            "mode": "NULLABLE",
            "type": "STRING"
        },
        {
            "name": "age",
            "mode": "NULLABLE",
            "type": "INTEGER"
        },
        {
            "name": "height",
            "mode": "NULLABLE",
            "type": "FLOAT"
        }
    ]
}"""
# ### functions and classes

def getSchema(element):
    names=element["address"].split("/")
    if not names[-1] in tables:
        return tables["tabledata.csv"]
    return tables[names[-1]]

def parse_json(element):
    row = json.loads(element.decode('utf-8'))
    #embed()
    return row #CommonLog(**row)

def parse_csv(element, message):
    lines=element.split(",")
    #head=lines[0]
    #lines.pop(0)
    schema=tables["tabledata.csv"]["fields"]
    names=message["address"].split("/")
    if names[-1] in tables:
        schema= tables[names[-1]]["fields"]

    #schema=getSchema(message)["fields"]
    dict={}
    for col in range(len(schema)):
        dict[schema[col]["name"]]=lines[col]
    #embed()
    return dict

def getURI(element):
    return element["address"]

def getDest(element):
    return element["destination"]

def cross_join(left, rights):
    #embed()
    return [left, rights]

# ### main

def run():
    # Command line arguments
    parser = argparse.ArgumentParser(description='Load from Json from Pub/Sub into a dynamic BigQuery output')

    # Setting up the Beam pipeline options
    options = PipelineOptions( save_main_session=True, streaming=True)
    options.view_as(GoogleCloudOptions).project = 'smartlive'
    options.view_as(GoogleCloudOptions).region = 'europe-west1'
    options.view_as(GoogleCloudOptions).staging_location = 'gs://testinsertbigquery/staging'
    options.view_as(GoogleCloudOptions).temp_location = 'gs://testinsertbigquery/temp'
    options.view_as(GoogleCloudOptions).dataflow_service_options=["enable_prime"]
    options.view_as(DebugOptions).experiments=["use_runner_v2"]
    options.view_as(GoogleCloudOptions).job_name = '{0}{1}'.format('chargement-fichier-streaming-primerunnerv2',time.time_ns())
    options.view_as(StandardOptions).runner = 'DataflowRunner'

    input_topic = 'projects/smartlive/topics/my_topic2'

    dataset='EtienneResults'

    table_names = ['smartlive:EtienneResults.output1', 'smartlive:EtienneResults.output2', 'smartlive:EtienneResults.output3']

    def table_fn(row, element):
        #embed()
        destination=element["destination"]
        return options.view_as(GoogleCloudOptions).project + ":" + dataset + "."+ destination

    window_duration = 60
    ##allowed_lateness = opts.allowed_lateness
    ##dead_letter_bucket = opts.dead_letter_bucket

    p = beam.Pipeline(options=options)

    readData=(p
            | 'ReadFromPubSub' >> beam.io.ReadFromPubSub(input_topic)
            | 'ParseJson' >> beam.Map(parse_json))

    dest_view=beam.pvalue.AsSingleton(
        readData | "Window1 into Fixed Intervals" >> beam.WindowInto
        (beam.window.FixedWindows(1),
         trigger=AfterCount(1),
         accumulation_mode=AccumulationMode.DISCARDING))

    data_view=(readData
            | 'Get CSV address' >> beam.Map(getURI)
            | 'Read CSV files' >> beam.io.ReadAllFromText(skip_header_lines=1)
            #| beam.GroupBy(lambda s: 0)
            #| 'ParseCSV' >> beam.Map(parse_csv)
            | "Window2 into Fixed Intervals" >> beam.WindowInto
        (beam.window.FixedWindows(1),
         trigger=AfterCount(1),
         accumulation_mode=AccumulationMode.DISCARDING))

    (data_view
        | 'ParseCSV' >> beam.Map(parse_csv, message=dest_view)
        | 'WriteWithDynamicDestination' >> beam.io.WriteToBigQuery(
                table=lambda row, dest_view : table_fn(row,dest_view),
                schema=lambda row, dest_view : getSchema(dest_view),
                table_side_inputs=(dest_view, ),
                schema_side_inputs=(dest_view, ),
                #additional_bq_parameters={'ignoreUnknownValues': True},
                create_disposition=beam.io.BigQueryDisposition.CREATE_IF_NEEDED,
                write_disposition=beam.io.BigQueryDisposition.WRITE_APPEND
                ))

    logging.getLogger().setLevel(logging.INFO)
    logging.info("Building pipeline ...")

    p.run().wait_until_finish()

if __name__ == '__main__':
  run()


# Description: Given a string and a non-negative int n, return a larger string that is n copies of the original string.
# Author: John Royce Punay
# Date created: March 5, 2018 7:20 PM
# string_times('Hi', 2) → 'HiHi'
# string_times('Hi', 3) → 'HiHiHi'
# string_times('Hi', 1) → 'Hi'

# Solution 1
def string_times1(word, count):
    if count < 0:
        print("Oops! negative value is not accepted for duplacating the string.")
        return False
    else:
        return word.capitalize() * count

# Solution 2

def string_times2(str, n):
  result = ""
  for i in range(n):  # range(n) is [0, 1, 2, .... n-1]
    result = result + str  # could use += here
  return result


# Create your views here.
from rest_framework import mixins, status
from rest_framework import viewsets
from rest_framework import permissions
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework import mixins
from rest_framework.views import APIView

from account.models import Account
from account.schemas import account_list
from account.serializers import AccountSerializer, CustomTokenObtainPairSerializer, CustomRefreshTokenSerializer, \
    RegisterSerializer
from rest_framework_simplejwt import views
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer

from back import settings

class RegisterApiView(mixins.CreateModelMixin, viewsets.GenericViewSet):
    serializer_class = RegisterSerializer
    queryset = Account.objects.all()

    def create(self, request, *args, **kwargs):
        serializer = RegisterSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_409_CONFLICT)
    # def pe

class LogoutApiView(APIView):

    def post(self, request, *args, **kwargs):
        rt = request.COOKIES.get('refresh', None)
        if rt is None:
            return Response({"detail": "Refresh token not found"}, status=400)
        response = Response()
        response.delete_cookie('refresh')
        response.delete_cookie('access')
        return response

class AccountViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
    permission_classes = [permissions.IsAuthenticated]

    @account_list
    def list(self, request: Request, *args, **kwargs):
        user_id = request.query_params.get('user_id')
        queryset = Account.objects.get(id=user_id)
        serializer = AccountSerializer(queryset)
        return Response(serializer.data)

class JWTCookieMixin(APIView):
    def finalize_response(self, request: Request, response: Response, *args, **kwargs):
        if response.data.get('access'):
            response.set_cookie('access',
                                response.data['access'],
                                max_age=settings.SIMPLE_JWT["ACCESS_TOKEN_LIFETIME"],
                                httponly=True,
                                samesite='Strict')
            del response.data['access']

        if response.data.get('refresh'):
            response.set_cookie('refresh',
                                response.data['refresh'],
                                max_age=settings.SIMPLE_JWT["REFRESH_TOKEN_LIFETIME"],
                                httponly=True,
                                samesite='Strict')
            del response.data['refresh']

        return super().finalize_response(request, response, *args, **kwargs)

class JWTCookieTokenPairView(JWTCookieMixin, views.TokenObtainPairView):
    serializer_class = CustomTokenObtainPairSerializer

class JWTCookieRefreshView(JWTCookieMixin, views.TokenRefreshView):
    serializer_class = CustomRefreshTokenSerializer


import json
from utils import response
import auth

def lambda_handler(event, context):

    # read payload
    params = event['queryStringParameters'] if 'queryStringParameters' in event else {}
    body = json.loads(event['body']) if 'body' in event else {}
    auth_body = body['auth'] if 'auth' in body else {}

    # auth
    authorized = auth.is_authorized(auth_body)
    if 'auth' in params and params['auth'] == 'checkAuth':
        return response({'authorized': authorized})
    elif 'auth' in params and params['auth'] == 'requestAuth':
        token = auth.request_authorization(auth_body)
        if token:
            return response({'authorized': True, 'authToken': token})
        else:
            return response({'authorized': False})
    elif not authorized:
        return response({'authorized': False}, status=401)

    return response(
        {
            'message': "I am going to steal the Krabby Patty formula!",
        }
    )


import os
import cv2
from glob import glob

def load_good_dices():
    normal_data_dir = os.path.join('.', 'assets', 'data', 'normal_dice', '*')
    normal_files_folders = glob(normal_data_dir)
    normal_files_paths = []
    labels = []
    for folder in normal_files_folders:
        files = glob(folder + '\\*.jpg')
        normal_files_paths += files
        labels += [int(os.path.basename(folder))] * len(files)

    print(f"{len(normal_files_paths)} normal files found")
    print(f"{len(labels)} normal files labels found")
    # for x in range(len(anomalous_files)):
    #    print(anomalous_files[x])

    return load_images(normal_files_paths), labels

def load_anomalous_dices():
    anomalous_data_dir = os.path.join('.', 'assets',  'data', 'anomalous_dice', '*.jpg')

    anomalous_files = glob(anomalous_data_dir)
    print(f"{len(anomalous_files)} anomalous files found")
    return load_images(anomalous_files), [11]*len(anomalous_files)

def load_images(images_path):
    img_size = 224
    images = []
    for img in images_path:
        try:
            img_arr = cv2.imread(img, 0)  # convert BGR to RGB format
            resized_arr = cv2.resize(img_arr, (img_size, img_size))  # Reshaping images to preferred size
            images.append(resized_arr)
        except Exception as e:
            print(e)
    return images


from unittest import TestCase
import datetime
from entities import patient, doctor
from services import service_gateway, entity_service, doctors_service, patients_service

# Tests for patients service API
class PatientServiceTest(TestCase):
    def test_patientAPI(self):
        es = entity_service.EntityService(entity_service.DictionaryBackingStore())
        service_gateway.Registry.registerService(es)
        ds = service_gateway.Registry.lookupService('DoctorsService')
        ps = service_gateway.Registry.lookupService('PatientsService')

        d = doctor.Doctor('Dolittle', datetime.date(1920, 5, 1), 'M', 'MD')
        p = patient.Patient('Buratino', datetime.date(2010, 1, 1), 'M', 60, 30, d.Id)
        p.Preconditions = ['Made of wood']
        p.Allergies = ['Water']
        p.Medications = ['Read a book twice a day']

        dctx = service_gateway.InvocationContext(d.Id)
        pctx = service_gateway.InvocationContext(p.Id)
        ds.createPatient(dctx, p)

        p1 = ds.findPatient(dctx, p.Id)
        p2 = ps.findPatient(pctx, p.Name)
        self.assertEqual(p1, p2)

        p2.Allergies.append('Milk')
        ps.updatePatient(pctx, p2)

        p3 = ps.findPatient(pctx, p.Name)
        self.assertIn('Milk', p3.Allergies)


"""
Correct an erogenous nucleotide in a reads

Author: Dominique Massau
Date: 27/09/2020
"""

# imports
from gc_content import parse_fasta
from overlap_graphs import is_similar_sequence
from point_mutation_identification import identify_mutations
from reverse_complement import get_reverse_complement
from sys import argv

# functions
def check_pairs(fastas: dict) -> tuple:
    """
    Check whether at least two pairs are present

    :param fastas: dict, contains header: sequence
    :return: tuple, contains two lists: (1) error reads and (2) all reads
    """

    pairs = {}

    for seq in fastas.values():
        reverse_comp = get_reverse_complement(seq).lower()
        if seq in pairs:
            pairs[seq].append(seq)
        elif reverse_comp in pairs:
            pairs[reverse_comp].append(seq)
        else:
            pairs[seq] = []

    correct_reads = [seq_1 for seq_1, seq_2 in pairs.items() if len(seq_2) > 0]
    error_reads = [seq_1 for seq_1, seq_2 in pairs.items() if seq_2 == []]

    return error_reads, correct_reads

def find_mate(error_reads: list, reads: list) -> dict:
    """
    Determine hamming distance for reads that can be mates

    The reads with an error are compared to all other reads as well as their
    reverse complements. For the pairs, the hamming distance is calculated
    and saved into a dictionary.

    :param error_reads: list, contains reads with an error
    :param reads: list, contains all reads
    :return: dict, contains error reads and other reads with hamming distance
    """

    scores = {error_read: {} for error_read in error_reads}
    for error_read in error_reads:
        for read in reads:

            if is_similar_sequence(error_read, read):
                continue
            else:
                reverse_complement = get_reverse_complement(read).lower()
                norm_hamming_dist = identify_mutations([error_read, read])
                revc_hamming_dist = identify_mutations([error_read, reverse_complement])

                scores[error_read].update({read: norm_hamming_dist})
                scores[error_read].update({reverse_complement: revc_hamming_dist})

    return scores

def report_fixes(scores: dict) -> None:
    """
    Writes a file with fixed reads

    :param scores: dict, contains dict with alternatives and hamming distance
    """

    writer = open("answer_file", 'w')

    for (error_read, fixed_reads) in scores.items():

        best_seen = False
        for k, v in fixed_reads.items():
            if v == 1:
                writer.write(f'{error_read.upper()}->{k.upper()}\n')
                best_seen = True

        if not best_seen:
            raise ValueError("A read contains multiple mistakes")

if __name__ == "__main__":
    fasta_dict = parse_fasta(argv[1])
    error_read_list, read_list = check_pairs(fasta_dict)
    score_dict = find_mate(error_read_list, read_list)
    report_fixes(score_dict)


import numpy as np
import sympy
from sympy.solvers import solve
from sympy import Symbol
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import seaborn as sns
import argparse

def plot_figure2(df,
                 num_init_pts,
                 imbal,
                 taus,
                 trial,
                 sel_rnd,
                 num_classes,
                 dataset_name,
                 fig_dir):

    fig, ax = plt.subplots()

    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)

    df = (df[(df['sel_rnd']==sel_rnd) &
             (df['num_init_pts']==num_init_pts) &
             (df['imbal']==imbal) &
             (df['trial']==trial)][['tau',
                                    'DMGT_rare_amnt',
                                    'DMGT_common_amnt']])

    n = Symbol('n')

    ideal_sizes = np.asarray(list(map(lambda tau: int((num_classes/2)*np.ceil(solve(sympy.sqrt(n+1) - sympy.sqrt(n) - tau, n)[0])), taus)))

    plot = sns.lineplot(data=df,
                        x='tau',
                        y='DMGT_common_amnt',
                        ci=95,
                        color=sns.color_palette('pastel')[0],
                        estimator='mean',
                        linewidth=5)

    sns.lineplot(data=df,
                 x='tau',
                 y='DMGT_rare_amnt',
                 ci=95,
                 color=sns.color_palette('pastel')[3],
                 estimator='mean',
                 linewidth=5)

    sns.lineplot(x=taus,
                 y=ideal_sizes,
                 color='darkgrey',
                 linestyle='--',
                 linewidth=5)

    plt.tight_layout(rect=[0, 0, 1, 0.8])
    plot.set_xlabel('Threshold', fontsize=14)
    plot.set_ylabel('Size of Selected Set', fontsize=14)
    plot.legend(labels=['common','rare', 'ideal'], bbox_to_anchor=(0.7,0.3), prop={'size':8})
    fig.savefig(fig_dir + f'{dataset_name}_tau_v_size.pdf', bbox_inches = "tight")


"""Module with main classes related to Interfaces."""
import json
import logging
from enum import IntEnum

from pyof.v0x01.common.phy_port import Port as PortNo01
from pyof.v0x01.common.phy_port import PortFeatures as PortFeatures01
from pyof.v0x04.common.port import PortFeatures as PortFeatures04
from pyof.v0x04.common.port import PortNo as PortNo04

from kytos.core.common import GenericEntity
from kytos.core.helpers import now

__all__ = ('Interface',)

LOG = logging.getLogger(__name__)

class TAGType(IntEnum):
    """Class that represents a TAG Type."""

    VLAN = 1
    VLAN_QINQ = 2
    MPLS = 3

class TAG:
    """Class that represents a TAG."""

    def __init__(self, tag_type, value):
        self.tag_type = TAGType(tag_type)
        self.value = value

    def __eq__(self, other):
        if not other:
            return False
        return self.tag_type == other.tag_type and self.value == other.value

    def as_dict(self):
        """Return a dictionary representating a tag object."""
        return {'tag_type': self.tag_type, 'value': self.value}

    @classmethod
    def from_dict(cls, tag_dict):
        """Return a TAG instance from python dictionary."""
        return cls(tag_dict.get('tag_type'), tag_dict.get('value'))

    @classmethod
    def from_json(cls, tag_json):
        """Return a TAG instance from json."""
        return cls.from_dict(json.loads(tag_json))

    def as_json(self):
        """Return a json representating a tag object."""
        return json.dumps(self.as_dict())

    def __repr__(self):
        return f"TAG({self.tag_type!r}, {self.value!r})"

class Interface(GenericEntity):  # pylint: disable=too-many-instance-attributes
    """Interface Class used to abstract the network interfaces."""

    # pylint: disable=too-many-arguments, too-many-public-methods
    def __init__(self, name, port_number, switch, address=None, state=None,
                 features=None, speed=None, config=None):
        """Assign the parameters to instance attributes.

        Args:
            name (string): name from this interface.
            port_number (int): port number from this interface.
            switch (:class:`~.core.switch.Switch`): Switch with this interface.
            address (|hw_address|): Port address from this interface.
            state (|port_stats|): Port Stat from interface. It will be
            deprecated.
            features (|port_features|): Port feature used to calculate link
                utilization from this interface. It will be deprecated.
            speed (int, float): Interface speed in bytes per second. Defaults
                to what is informed by the switch. Return ``None`` if not set
                and switch does not inform the speed.
            config(|port_config|): Port config used to indicate interface
                behavior. In general, the port config bits are set by the
                controller and are not changed by the switch. Options
                are: administratively down, ignore received packets, drop
                forwarded packets, and/or do not send packet-in messages.
        """
        self.name = name
        self.port_number = int(port_number)
        self.switch = switch
        self.address = address
        self.state = state
        self.features = features
        self.config = config
        self.nni = False
        self.endpoints = []
        self.stats = None
        self.link = None
        self.lldp = True
        self._custom_speed = speed
        self.set_available_tags(range(1, 4096))

        super().__init__()

    def __repr__(self):
        return f"Interface('{self.name}', {self.port_number}, {self.switch!r})"

    def __eq__(self, other):
        """Compare Interface class with another instance."""
        if isinstance(other, str):
            return self.address == other
        if isinstance(other, Interface):
            return self.port_number == other.port_number and \
                self.switch.dpid == other.switch.dpid
        return False

    @property
    def id(self):  # pylint: disable=invalid-name
        """Return id from Interface instance.

        Returns:
            string: Interface id.

        """
        return "{}:{}".format(self.switch.dpid, self.port_number)

    @property
    def uni(self):
        """Return if an interface is a user-to-network Interface."""
        return not self.nni

    def set_available_tags(self, iterable):
        """Set a range of VLAN tags to be used by this Interface.

        Args:
            iterable ([int]): range of VLANs.
        """
        self.available_tags = []

        for i in iterable:
            vlan = TAGType.VLAN
            tag = TAG(vlan, i)
            self.available_tags.append(tag)

    def enable(self):
        """Enable this interface instance.

        Also enable the switch instance this interface is attached to.
        """
        self.switch.enable()
        self._enabled = True

    def use_tag(self, tag):
        """Remove a specific tag from available_tags if it is there.

        Return False in case the tag is already removed.
        """
        try:
            self.available_tags.remove(tag)
        except ValueError:
            return False
        return True

    def is_tag_available(self, tag):
        """Check if a tag is available."""
        return tag in self.available_tags

    def get_next_available_tag(self):
        """Get the next available tag from the interface.

        Return the next available tag if exists and remove from the
        available tags.
        If no tag is available return False.
        """
        try:
            return self.available_tags.pop()
        except IndexError:
            return False

    def make_tag_available(self, tag):
        """Add a specific tag in available_tags."""
        if not self.is_tag_available(tag):
            self.available_tags.append(tag)
            return True
        return False

    def get_endpoint(self, endpoint):
        """Return a tuple with existent endpoint, None otherwise.

        Args:
            endpoint(|hw_address|, :class:`.Interface`): endpoint instance.

        Returns:
            tuple: A tuple with endpoint and time of last update.

        """
        for item in self.endpoints:
            if endpoint == item[0]:
                return item
        return None

    def add_endpoint(self, endpoint):
        """Create a new endpoint to Interface instance.

        Args:
            endpoint(|hw_address|, :class:`.Interface`): A target endpoint.
        """
        exists = self.get_endpoint(endpoint)
        if not exists:
            self.endpoints.append((endpoint, now()))

    def delete_endpoint(self, endpoint):
        """Delete a existent endpoint in Interface instance.

        Args:
            endpoint (|hw_address|, :class:`.Interface`): A target endpoint.
        """
        exists = self.get_endpoint(endpoint)
        if exists:
            self.endpoints.remove(exists)

    def update_endpoint(self, endpoint):
        """Update or create new endpoint to Interface instance.

        Args:
            endpoint(|hw_address|, :class:`.Interface`): A target endpoint.
        """
        exists = self.get_endpoint(endpoint)
        if exists:
            self.delete_endpoint(endpoint)
        self.add_endpoint(endpoint)

    def update_link(self, link):
        """Update link for this interface in a consistent way.

        Verify of the other endpoint of the link has the same Link information
        attached to it, and change it if necessary.

        Warning: This method can potentially change information of other
        Interface instances. Use it with caution.
        """
        if self not in (link.endpoint_a, link.endpoint_b):
            return False

        if self.link is None or self.link != link:
            self.link = link

        if link.endpoint_a == self:
            endpoint = link.endpoint_b
        else:
            endpoint = link.endpoint_a

        if endpoint.link is None or endpoint.link != link:
            endpoint.link = link

        return True

    @property
    def speed(self):
        """Return the link speed in bytes per second, None otherwise.

        If the switch was disconnected, we have :attr:`features` and speed is
        still returned for common values between v0x01 and v0x04. For specific
        v0x04 values (40 Gbps, 100 Gbps and 1 Tbps), the connection must be
        active so we can make sure the protocol version is v0x04.

        Returns:
            int, None: Link speed in bytes per second or ``None``.

        """
        speed = self.get_of_features_speed()

        if speed is not None:
            return speed

        if self._custom_speed is not None:
            return self._custom_speed

        if self._is_v0x04() and self.port_number == PortNo04.OFPP_LOCAL:
            return 0

        if not self._is_v0x04() and self.port_number == PortNo01.OFPP_LOCAL:
            return 0

        # Warn unknown speed
        # Use shorter switch ID with its beginning and end
        if isinstance(self.switch.id, str) and len(self.switch.id) > 20:
            switch_id = self.switch.id[:3] + '...' + self.switch.id[-3:]
        else:
            switch_id = self.switch.id
        LOG.warning("Couldn't get port %s speed, sw %s, feats %s",
                    self.port_number, switch_id, self.features)

        return None

    def set_custom_speed(self, bytes_per_second):
        """Set a speed that overrides switch OpenFlow information.

        If ``None`` is given, :attr:`speed` becomes the one given by the
        switch.
        """
        self._custom_speed = bytes_per_second

    def get_custom_speed(self):
        """Return custom speed or ``None`` if not set."""
        return self._custom_speed

    def get_of_features_speed(self):
        """Return the link speed in bytes per second, None otherwise.

        If the switch was disconnected, we have :attr:`features` and speed is
        still returned for common values between v0x01 and v0x04. For specific
        v0x04 values (40 Gbps, 100 Gbps and 1 Tbps), the connection must be
        active so we can make sure the protocol version is v0x04.

        Returns:
            int, None: Link speed in bytes per second or ``None``.

        """
        speed = self._get_v0x01_v0x04_speed()
        # Don't use switch.is_connected() because we can have the protocol
        if speed is None and self._is_v0x04():
            speed = self._get_v0x04_speed()
        return speed

    def _is_v0x04(self):
        """Whether the switch is connected using OpenFlow 1.3."""
        return self.switch.is_connected() and \
            self.switch.connection.protocol.version == 0x04

    def _get_v0x01_v0x04_speed(self):
        """Check against all values of v0x01. They're part of v0x04."""
        fts = self.features
        pfts = PortFeatures01
        if fts and fts & pfts.OFPPF_10GB_FD:
            return 10 * 10**9 / 8
        if fts and fts & (pfts.OFPPF_1GB_HD | pfts.OFPPF_1GB_FD):
            return 10**9 / 8
        if fts and fts & (pfts.OFPPF_100MB_HD | pfts.OFPPF_100MB_FD):
            return 100 * 10**6 / 8
        if fts and fts & (pfts.OFPPF_10MB_HD | pfts.OFPPF_10MB_FD):
            return 10 * 10**6 / 8
        return None

    def _get_v0x04_speed(self):
        """Check against higher enums of v0x04.

        Must be called after :meth:`get_v0x01_speed` returns ``None``.
        """
        fts = self.features
        pfts = PortFeatures04
        if fts and fts & pfts.OFPPF_1TB_FD:
            return 10**12 / 8
        if fts and fts & pfts.OFPPF_100GB_FD:
            return 100 * 10**9 / 8
        if fts and fts & pfts.OFPPF_40GB_FD:
            return 40 * 10**9 / 8
        return None

    def get_hr_speed(self):
        """Return Human-Readable string for link speed.

        Returns:
            string: String with link speed. e.g: '350 Gbps' or '350 Mbps'.

        """
        speed = self.speed
        if speed is None:
            return ''
        speed *= 8
        if speed == 10**12:
            return '1 Tbps'
        if speed >= 10**9:
            return '{} Gbps'.format(round(speed / 10**9))
        return '{} Mbps'.format(round(speed / 10**6))

    def as_dict(self):
        """Return a dictionary with Interface attributes.

        Speed is in bytes/sec. Example of output (100 Gbps):

        .. code-block:: python3

            {'id': '00:00:00:00:00:00:00:01:2',
             'name': 'eth01',
             'port_number': 2,
             'mac': '00:7e:04:3b:c2:a6',
             'switch': '00:00:00:00:00:00:00:01',
             'type': 'interface',
             'nni': False,
             'uni': True,
             'speed': 12500000000,
             'metadata': {},
             'lldp': True,
             'active': True,
             'enabled': False,
             'link': ""
            }

        Returns:
            dict: Dictionary filled with interface attributes.

        """
        iface_dict = {'id': self.id,
                      'name': self.name,
                      'port_number': self.port_number,
                      'mac': self.address,
                      'switch': self.switch.dpid,
                      'type': 'interface',
                      'nni': self.nni,
                      'uni': self.uni,
                      'speed': self.speed,
                      'metadata': self.metadata,
                      'lldp': self.lldp,
                      'active': self.is_active(),
                      'enabled': self.is_enabled(),
                      'link': self.link.id if self.link else ""}
        if self.stats:
            iface_dict['stats'] = self.stats.as_dict()
        return iface_dict

    @classmethod
    def from_dict(cls, interface_dict):
        """Return a Interface instance from python dictionary."""
        return cls(interface_dict.get('name'),
                   interface_dict.get('port_number'),
                   interface_dict.get('switch'),
                   interface_dict.get('address'),
                   interface_dict.get('state'),
                   interface_dict.get('features'),
                   interface_dict.get('speed'))

    def as_json(self):
        """Return a json with Interfaces attributes.

        Example of output:

        .. code-block:: json

            {"mac": "00:7e:04:3b:c2:a6",
             "switch": "00:00:00:00:00:00:00:01",
             "type": "interface",
             "name": "eth01",
             "id": "00:00:00:00:00:00:00:01:2",
             "port_number": 2,
             "speed": "350 Mbps"}

        Returns:
            string: Json filled with interface attributes.

        """
        return json.dumps(self.as_dict())

class UNI:
    """Class that represents an User-to-Network Interface."""

    def __init__(self, interface, user_tag=None):
        self.user_tag = user_tag
        self.interface = interface

    def __eq__(self, other):
        """Override the default implementation."""
        return (self.user_tag == other.user_tag and
                self.interface == other.interface)

    def is_valid(self):
        """Check if TAG is possible for this interface TAG pool."""
        if self.user_tag:
            return self.interface.is_tag_available(self.user_tag)
        return True

    def as_dict(self):
        """Return a dict representating a UNI object."""
        return {
            'interface_id': self.interface.id,
            'tag': self.user_tag.as_dict() if self.user_tag else None
            }

    @classmethod
    def from_dict(cls, uni):
        """Return a Uni instance from python dictionary."""
        return cls(uni.get('interface'),
                   uni.get('user_tag'))

    def as_json(self):
        """Return a json representating a UNI object."""
        return json.dumps(self.as_dict())

class NNI:
    """Class that represents an Network-to-Network Interface."""

    def __init__(self, interface):
        self.interface = interface

class VNNI(NNI):
    """Class that represents an Virtual Network-to-Network Interface."""

    def __init__(self, service_tag, *args, **kwargs):
        self.service_tag = service_tag

        super().__init__(*args, **kwargs)


# Fungsi untuk mencetak matriks adjacency
def print_matrix(matrix):
    for row in matrix:
        print(row)

# Algoritma Warshall
def warshall(graph):
    n = len(graph)
    for k in range(n):
        for i in range(n):
            for j in range(n):
                graph[i][j] = graph[i][j] or (graph[i][k] and graph[k][j])
    return graph

# Algoritma Floyd
def floyd(graph):
    n = len(graph)
    for k in range(n):
        for i in range(n):
            for j in range(n):
                graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j])
    return graph

# Contoh penggunaan
graph = [
    [0, 1, 0, 0],
    [0, 0, 1, 0],
    [0, 0, 0, 1],
    [1, 0, 0, 0]
]

print("Matriks adjacency awal:")
print_matrix(graph)

print("Hasil algoritma Warshall:")
print_matrix(warshall(graph))

print("Hasil algoritma Floyd:")
print_matrix(floyd(graph))

Source Code_Warshall’s and Floyd’s Algorithms/Farraz Hadyan_Warshall’s and Floyd’s Algorithms.py


import logging
from datetime import datetime
from jsonrpcserver import method

from zilpool.common import utils, blockchain
from zilpool.pyzil import crypto, ethash
from zilpool.database import pow, zilnode
from zilpool.stratum.stratum_server import *

def init_apis(config):
    zil_config = config["api_server"]["zil"]

    def check_network_info(block_num, boundary, timeout):
        if not blockchain.Zilliqa.is_pow_window():
            logging.warning(f"The network is not in pow window")
            return False

        network_ds_block = blockchain.Zilliqa.cur_ds_block
        if block_num < network_ds_block:
            logging.warning(f"Got wrong block number: {block_num} < {network_ds_block}")
            return False
        if block_num > network_ds_block + 1:
            logging.warning(f"Got wrong block number: {block_num} > {network_ds_block} + 1")
            return False

        network_difficulty = [
            blockchain.Zilliqa.shard_difficulty
        ]
        if config.site_settings.allow_ds_pow:
            network_difficulty.append(blockchain.Zilliqa.ds_difficulty)

        # try divided difficulty
        difficulty = ethash.boundary_to_difficulty_divided(
            boundary,
            n_divided=config["zilliqa"]["POW_BOUNDARY_N_DIVIDED"],
            n_divided_start=config["zilliqa"]["POW_BOUNDARY_N_DIVIDED_START"]
        )
        if difficulty not in network_difficulty:
            # try original difficulty
            old_difficulty = ethash.boundary_to_difficulty(boundary)
            if old_difficulty not in network_difficulty:
                logging.warning(f"Got wrong difficulty {difficulty}")
                return False

        if timeout > config["zilliqa"]["POW_WINDOW_IN_SECONDS"]:
            logging.warning(f"Got wrong timeout {timeout}")
            return False

        return True

    @method
    @utils.args_to_lower
    async def zil_requestWork(request,
                              pub_key: str, header: str,
                              block_num: str, boundary: str,
                              timeout: str, signature: str) -> bool:
        assert (len(pub_key) == 68 and      # 33 bytes -> "0x" + 66 chars
                len(header) == 66 and       # 32 bytes -> "0x" + 64 chars
                len(block_num) == 18 and    # 8 bytes  -> "0x" + 16 chars
                len(boundary) == 66 and     # 32 bytes -> "0x" + 64 chars
                len(timeout) == 10 and      # 4 bytes  -> "0x" + 8 chars
                len(signature) == 130)      # 64 bytes -> "0x" + 128 chars

        str_block_num, str_timeout = block_num, timeout
        block_num = crypto.hex_str_to_int(block_num)
        timeout = crypto.hex_str_to_int(timeout)

        if config["zilliqa"]["enabled"]:
            # 0. check network info
            if not check_network_info(block_num, boundary, timeout):
                logging.warning(f"Invalid PoW request from {pub_key}")
                return False

        # verify signature
        if not verify_signature(pub_key, signature, pub_key, header,
                                str_block_num, boundary, str_timeout):
            # hotfix for Zilliqa v4.2.0
            # set timeout to 60 and try again
            str_timeout = crypto.int_to_hex_str_0x(60, n_bytes=4)
            if not verify_signature(pub_key, signature, pub_key, header,
                                    str_block_num, boundary, str_timeout):
                logging.warning(f"failed verify signature")
                return False

        node = zilnode.ZilNode.get_by_pub_key(pub_key=pub_key, authorized=True)
        if not (node and node.authorized):
            logging.warning(f"unauthorized public key: {pub_key}")
            return False

        count = pow.PowWork.count(pub_key=pub_key, block_num=block_num)
        if count >= 2:
            logging.warning(f"too many PoW requests from {block_num} {pub_key}")
            return False

        work = pow.PowWork.new_work(header, block_num, boundary,
                                    pub_key=pub_key, signature=signature,
                                    timeout=timeout, pow_fee=node.pow_fee)
        # update pow window
        pow.PoWWindow.update_pow_window(work)

        for stratumMiner in stratumMiners:
            min_fee = config.site_settings.min_fee
            max_dispatch = config.site_settings.max_dispatch
            inc_expire = config.site_settings.inc_expire

            dispatchWork = pow.PowWork.get_new_works(count=1, min_fee=min_fee,
                                         max_dispatch=max_dispatch)
            if dispatchWork is not None:
                if dispatchWork.increase_dispatched(max_dispatch, inc_seconds=inc_expire):
                    stratumMiner.notify_work(dispatchWork)

        logging.critical(f"PoW work {block_num} {header} requested from {pub_key}")

        return work is not None

    work_not_done = (False, "", "", "")

    @method
    @utils.args_to_lower
    async def zil_checkWorkStatus(request,
                                  pub_key: str, header: str,
                                  boundary: str, signature: str) -> [list, tuple]:
        assert (len(pub_key) == 68 and
                len(header) == 66 and
                len(boundary) == 66 and
                len(signature) == 130)    # 64 bytes -> 128 chars + "0x"

        # verify signature
        if not verify_signature(pub_key, signature,
                                pub_key, header, boundary):
            logging.warning(f"failed verify signature")
            return False

        pow_result = pow.PowResult.get_pow_result(header, boundary, pub_key=pub_key)

        if not pow_result:
            logging.info(f"result not found for pub_key: {pub_key}, "
                         f"header: {header}, boundary: {boundary}")
            return work_not_done

        logging.critical(f"PoW result found, header: {header}, boundary: {boundary}")

        return True, pow_result.nonce, pow_result.header, pow_result.mix_digest

    @method
    @utils.args_to_lower
    async def zil_verifyResult(request,
                               pub_key: str, verified: str,
                               header: str, boundary: str, signature: str) -> bool:
        assert (len(pub_key) == 68 and
                len(verified) == 4 and
                len(header) == 66 and
                len(boundary) == 66 and
                len(signature) == 130)    # 64 bytes -> "0x" + 128 chars

        # verify signature
        if not verify_signature(pub_key, signature,
                                pub_key, verified, header, boundary):
            logging.warning(f"failed verify signature")
            return False

        pow_result = pow.PowResult.get_pow_result(header, boundary, pub_key=pub_key)

        if not pow_result:
            logging.warning(f"result not found for pub_key: {pub_key}, "
                            f"header: {header}, boundary: {boundary}")
            return False

        verified = verified == "0x01"
        if pow_result.update(verified=verified,
                             verified_time=datetime.utcnow()):
            worker = pow_result.get_worker()
            if worker is None:
                logging.warning(f"worker not found, {pow_result.worker_name}"
                                f"@{pow_result.miner_wallet}")
            else:
                worker.update_stat(inc_verified=1)

            logging.critical(f"PoW result verified by pub_key: {pub_key}, "
                             f"header: {header}, boundary: {boundary}")

            return True

        logging.warning(f"Failed update pow result {pow_result}")
        return False

    def verify_signature(pub_key, signature, *parameters):
        if zil_config["verify_sign"] is False:
            return True

        key = crypto.ZilKey(str_public=pub_key)

        msg_to_verify = b""
        for param in parameters:
            if isinstance(param, bytes):
                b_param = param
            elif isinstance(param, str):
                b_param = crypto.hex_str_to_bytes(param)
            elif isinstance(param, bool):
                b_param = b"\x01" if param else b"\x00"
            elif isinstance(param, int):
                b_param = crypto.int_to_bytes(param, n_bytes=8)
            else:
                logging.warning(f"wrong data type")
                return False

            msg_to_verify += b_param

        return key.verify(signature, msg_to_verify)