from __future__ import print_function
try:
    from openpyxl import load_workbook
    from openpyxl.utils import get_column_letter
except:
    print("Openpyxl module cannot be imported because it's not installed. pip install openpyxl to solve the issue")
    exit('Simulation terminated')
from re import compile, escape
from scipy import interpolate
from fipy import numerix
from collections import OrderedDict

# User Setup
simulation_control_workbook = 'sim_settings.xlsx'
simulation_control_workbook_sheet = 'Sim_Control'

################################# Import the simuatlion settings #######################################################
cell_properties  = load_workbook(simulation_control_workbook)       # Load workbook containing cell property data
sim_settings_sheet = cell_properties.get_sheet_by_name(simulation_control_workbook_sheet)  # Load the sheet corresponding to desired cell
Uocp_neg_sheet = 'Uocp_neg'
Uocp_pos_sheet = 'Uocp_pos'
kappa_sheet = 'Kappa'

control_chars = ''.join(map(unichr, range(0,32) + range(127,160)))           # Deal with non-printable characters in cell property names
control_char_re = compile('[%s]' % escape(control_chars))
def remove_control_chars(s):
    return control_char_re.sub('', s.strip())

for sheet in cell_properties:
    if sheet.title == sim_settings_sheet.title:
        namecol = sim_settings_sheet.min_column  # This column no. contains the name of the cell parameters
        minrow = sim_settings_sheet.min_row  # This column no. contains the name of the cell parameters
        valuecol = namecol + 1  # This column no. contains the numerical/string value of the cell-parameters
        # settings_dict = {}  # Create an object to store the parameters
        settings_dict = OrderedDict()
        for row_number in range(minrow, sim_settings_sheet.max_row+1):                             # Import cell properties sim_settings_sheet
            name_location  = get_column_letter(namecol)  + str(row_number)          # Obtain the alphabetical column string of the name column
            value_location = get_column_letter(valuecol) + str(row_number)          # Obtain the alphabetical column string of the value column
            if sim_settings_sheet[name_location].value != None:                                    # Skip processing if blank row
                sim_setting_name = remove_control_chars(sim_settings_sheet[name_location].value)   # Get the contents of the 'name-column' & strip any non-printable characters
                sim_setting_name = sim_settings_sheet[name_location].value
                sim_setting_value = sim_settings_sheet[value_location].value                       # Force to type 'float' if user entered a cell parameter without decimal point
                if not isinstance(sim_settings_sheet[value_location].value, float):
                    if isinstance(sim_settings_sheet[value_location].value, long or int):
                        sim_setting_value = float(sim_setting_value)
                    else:
                        sim_setting_value = str(sim_setting_value)             # Convert unicode Uocp functions to strings
                settings_dict[sim_setting_name] = sim_setting_value

    elif sheet.title == Uocp_neg_sheet:
        namecol = sheet.min_column  # This column no. contains the timestamp
        valuecol = namecol + 1  # This column no. contains the current value
        minrow = sheet.min_row
        maxrow = sheet.max_row
        data_col_length = maxrow - minrow + 1
        theta_table_vals_neg = numerix.empty(data_col_length)
        Uocp_table_vals_neg = numerix.empty(data_col_length)
        for row_number in range(minrow, maxrow + 1):
            name_location = get_column_letter(namecol) + str(row_number)  # Obtain the alphabetical column string of the timestamp column
            value_location = get_column_letter(valuecol) + str(row_number)  # Obtain the alphabetical column string of the current_value column
            if sheet[name_location].value != None:  # Check for blank row
                theta_table_vals_neg[row_number - minrow] = float(sheet[name_location].value)
                Uocp_table_vals_neg[row_number - minrow] = float(sheet[value_location].value)  # Need to add in ability to process formulae in spreadsheet cells

    elif sheet.title == Uocp_pos_sheet:
        namecol = sheet.min_column  # This column no. contains the timestamp
        valuecol = namecol + 1  # This column no. contains the current value
        minrow = sheet.min_row
        maxrow = sheet.max_row
        data_col_length = maxrow - minrow + 1
        theta_table_vals_pos = numerix.empty(data_col_length)
        Uocp_table_vals_pos = numerix.empty(data_col_length)
        for row_number in range(minrow, maxrow + 1):
            name_location = get_column_letter(namecol) + str(row_number)  # Obtain the alphabetical column string of the timestamp column
            value_location = get_column_letter(valuecol) + str(row_number)  # Obtain the alphabetical column string of the current_value column
            if sheet[name_location].value != None:  # Check for blank row
                theta_table_vals_pos[row_number - minrow] = float(sheet[name_location].value)
                Uocp_table_vals_pos[row_number - minrow] = float(sheet[value_location].value)  # Need to add in ability to process formulae in spreadsheet cells

    elif sheet.title == kappa_sheet:
        namecol = sheet.min_column  # This column no. contains the timestamp
        valuecol = namecol + 1  # This column no. contains the current value
        minrow = sheet.min_row
        maxrow = sheet.max_row
        data_col_length = maxrow - minrow + 1
        kappa_ce = numerix.empty(data_col_length)
        kappa_value = numerix.empty(data_col_length)
        for row_number in range(minrow, maxrow + 1):
            name_location = get_column_letter(namecol) + str(row_number)  # Obtain the alphabetical column string of the timestamp column
            value_location = get_column_letter(valuecol) + str(row_number)  # Obtain the alphabetical column string of the current_value column
            if sheet[name_location].value != None:  # Check for blank row
                kappa_ce[row_number - minrow] = float(sheet[name_location].value)
                kappa_value[row_number - minrow] = float(sheet[value_location].value)  # Need to add in ability to process formulae in spreadsheet cells
        interpolated_kappa_fn = interpolate.interp1d(kappa_ce, kappa_value, kind='slinear')

#################################################### Cleanse names #####################################################
# General
initial_soc_percent, cell_temperature = settings_dict['initial_soc_percent'], settings_dict['cell_temperature']

# Timing
simEndtime, temporal_resolution = settings_dict['simEndtime'], settings_dict['temporal_resolution']

# Domain sizing
L_neg_normalised, L_pos_normalised = settings_dict['L_neg_normalised'], settings_dict['L_pos_normalised']
L_sep_normalised = settings_dict['L_sep_normalised']
Rs_neg_normalised, Rs_pos_normalised = settings_dict['Rs_neg_normalised'], settings_dict['Rs_pos_normalised']

# Domain meshing
nx_neg, nx_pos, nx_sep = int(settings_dict['nx_neg']), int(settings_dict['nx_pos']), int(settings_dict['nx_sep'])
nr_neg, nr_pos = int(settings_dict['nr_neg']), int(settings_dict['nr_pos'])
non_uniform_axial_meshing, non_uniform_radial_meshing = settings_dict['non_uniform_axial_meshing'], settings_dict['non_uniform_radial_meshing']

# Solution variable tolerancing
tolerance_phi_s_neg_res, tolerance_phi_s_pos_res = settings_dict['tolerance_phi_s_neg_res'], settings_dict['tolerance_phi_s_pos_res']
tolerance_Cs_neg_res, tolerance_Cs_pos_res = settings_dict['tolerance_Cs_neg_res'], settings_dict['tolerance_Cs_pos_res']
tolerance_phi_e_sim_cell_res, tolerance_Ce_sim_cell_res = settings_dict['tolerance_phi_e_sim_cell_res'], settings_dict['tolerance_Ce_sim_cell_res']

# Under-relaxation factors
under_relax_factor_phi_s_neg = settings_dict['under_relax_factor_phi_s_neg']
under_relax_factor_phi_s_pos = settings_dict['under_relax_factor_phi_s_pos']
under_relax_factor_Ce = settings_dict['under_relax_factor_Ce']
under_relax_factor_phi_e = settings_dict['under_relax_factor_phi_e']
under_relax_factor_Cs_neg = settings_dict['under_relax_factor_Cs_neg']
under_relax_factor_Cs_pos = settings_dict['under_relax_factor_Cs_pos']

# Current profile import
cprofile_workbook, cprofile_sheet, repeat_current_profile = simulation_control_workbook, settings_dict['cprofile_sheet'], settings_dict['repeat_current_profile']

# Cell properties import
cell_properties_workbook, cell_properties_sheet = simulation_control_workbook, settings_dict['cell_properties_sheet']

# Data logging & Debugging
datalogging_switch = settings_dict['datalogging_switch']
if datalogging_switch == 'on':
    datalogging_switch == 'On'
record_period, datalog_filename, output_location = settings_dict['record_period'], settings_dict['datalog_filename'], settings_dict['output_location']
det_residual_live_plot, det_residual_saving = settings_dict['det_residual_live_plot'], settings_dict['det_residual_saving']
mem_leak_analysis, det_concentrations_live_plot = settings_dict['mem_leak_analysis'], settings_dict['Cs_live_plot']
if det_residual_live_plot == 'on':
    det_residual_live_plot == 'On'
if det_residual_saving == 'on':
    det_residual_live_plot == 'On'
if mem_leak_analysis == 'on':
    mem_leak_analysis == 'On'
if det_concentrations_live_plot == 'on':
    det_concentrations_live_plot == 'On'

# Analysis
auto_process = settings_dict['auto_process']
if auto_process == 'on':
    auto_process == 'On'
if auto_process == 'On' and datalogging_switch != 'On':
    print('auto_process data is switched On, but datalogging is switched off. Either switch datalogging to On or switch auto_process to Off')
    exit('Simulation terminated')

########################################### Pre-process domain sizes ###################################################
nx_tot = nx_neg + nx_sep + nx_pos
L_tot_normalised = L_neg_normalised + L_pos_normalised + L_sep_normalised  # Total normalised length (by default set to 3)

########################### Current Profile Import #####################################################################
cprofile_book = load_workbook(cprofile_workbook)                                   # Load workbook containing current profile
cprofile_data = cprofile_book.get_sheet_by_name(cprofile_sheet)                             # Load the sheet corresponding to desired profile

cprofile_namecol = cprofile_data.min_column                                                 # This column no. contains the timestamp
cprofile_valuecol = cprofile_namecol + 1                                                    # This column no. contains the current value
# Improve the below logic to handle column headers, keep moving down until you hit the first float
cprofile_minrow = cprofile_data.min_row
cprofile_maxrow = cprofile_data.max_row
cprofile_length = cprofile_maxrow - cprofile_minrow + 1
cprofile_timestamps = numerix.empty(cprofile_length)
cprofile_currents = numerix.empty(cprofile_length)

for row_number in range(cprofile_minrow,cprofile_maxrow + 1):
    cprofile_timestamp_location = get_column_letter(cprofile_namecol) + str(row_number)    # Obtain the alphabetical column string of the timestamp column
    cprofile_value_location = get_column_letter(cprofile_valuecol) + str(row_number)       # Obtain the alphabetical column string of the current_value column
    if cprofile_data[cprofile_timestamp_location].value != None:                            # Check for blank row
        cprofile_timestamps[row_number - cprofile_minrow] = float(cprofile_data[cprofile_timestamp_location].value)
        cprofile_currents[row_number - cprofile_minrow] = float(cprofile_data[cprofile_value_location].value) # Need to add in ability to process formulae in spreadsheet cells

interpolated_current_fn = interpolate.interp1d(cprofile_timestamps, cprofile_currents, kind = 'slinear')

################################# Import the cell properties ###########################################################
cell_properties  = load_workbook(cell_properties_workbook)          # Load workbook containing cell property data
data             = cell_properties.get_sheet_by_name(cell_properties_sheet)  # Load the sheet corresponding to desired cell
namecol          = data.min_column                                           # This column no. contains the name of the cell parameters
minrow           = data.min_row                                              # This column no. contains the name of the cell parameters
valuecol         = namecol + 1                                               # This column no. contains the numerical/string value of the cell-parameters
params_dict = {}                                                             # Create an object to store the parameters

for row_number in range(minrow, data.max_row+1):                             # Import cell properties data
    name_location  = get_column_letter(namecol)  + str(row_number)          # Obtain the alphabetical column string of the name column
    value_location = get_column_letter(valuecol) + str(row_number)          # Obtain the alphabetical column string of the value column
    if data[name_location].value != None:                                    # Skip processing if blank row
        cell_parameter_name = remove_control_chars(data[name_location].value)# Get the contents of the 'name-column' & strip any non-printable characters
        cell_parameter_name = data[name_location].value
        cell_parameter_value = data[value_location].value                    # Force to type 'float' if user entered a cell parameter without decimal point
        if not isinstance(data[value_location].value, float):
            if isinstance(data[value_location].value, long or int):
                cell_parameter_value = float(cell_parameter_value)
            else:
                cell_parameter_value = str(cell_parameter_value)             # Convert unicode Uocp functions to strings
        params_dict[cell_parameter_name] = cell_parameter_value


import os

from testsuite_support.builder_and_runner import BuilderAndRunner, GPRINSTALL

bnr = BuilderAndRunner()
output = "output.txt"

prefix_switch = "--prefix=" + os.getcwd() + "/inst"
status = bnr.run([GPRINSTALL, "-d", "-p", prefix_switch, "prj.gpr"],
                 output=output).status
file = open(output, "r")
lines = file.readlines()
file.close()

for line in lines:
    if line.startswith("cp ") or line.startswith('ln'):
        print(line)


import bpy
import bmesh
import os
from pathlib import Path

"""
This script is made for  Blender 2.92 and use the MeshSlicer (https://github.com/TomekSc/MeshSlicer).
To install the add-on:
    1- Downlowd MeshSlicer.py from the github
    2- On blender 2.92, go to Edit/Preference/Add-ons
    3- Click on Install add-on and select MeshSlicer.py
    4- Activate the add on by ticking his box on the add-ons list

To use it, put your files to preprocess on a folder and attribute it's path to FILES_FOLDER.
The sliced results will be save in SAVE_FOLDER/name_mesh/

Writed by Leopold Le Roux, 21/10/2021
"""

FILE_PATH = ""
FILES_FOLDER = Path("WHERE ARE THE STL FILES")
SAVE_FOLDER = Path("WHERE TO STORE THE STL FILES")
Z_MAX = 0
Z_MIN = 0
SLICE_Zs = []
SLIDE_HEIGHT = 2

def slice_a_file(name_file, input_path, output_path):
    "Will load the stl from the FILE_PATH and slice it, the slided mesh are save in SAVE_FOLDER"

    # if the results folder do not exist, create it
    if not os.path.exists(str(output_path)):
        os.makedirs(output_path)
    # Clean the results folder
    for file in output_path.glob("*.stl"):
        file.unlink()

    # Clear workspace
    bpy.ops.object.select_all(action='SELECT')
    bpy.ops.object.delete()

    # Found the Z size
    bpy.ops.import_mesh.stl(filepath=input_path)
    obj = bpy.context.active_object
    Z_MAX = max([v[-1] for v in obj.bound_box])
    Z_MIN = min([v[-1] for v in obj.bound_box])

    # Compute the Z height of the slides
    NUMBER_OF_SLICE = int((Z_MAX - Z_MIN) / SLIDE_HEIGHT)
    SLICE_Zs = []
    z = Z_MIN
    while z < Z_MAX:
        SLICE_Zs.append(z)
        z += SLIDE_HEIGHT
    SLICE_Zs.append(Z_MAX)

    for slide_number in range(NUMBER_OF_SLICE):

        # Clear workspace
        bpy.ops.object.select_all(action='SELECT')
        bpy.ops.object.delete()

        # Ask for 1 plan to slice
        bpy.data.scenes["Scene"].my_tool.slicerSize = 60
        bpy.ops.generate.operator(variant=0)

        # Add a second plan
        bpy.ops.object.select_all(action='DESELECT')
        bpy.data.objects['Slicer'].select_set(True)

        # Create a // plan
        bpy.ops.object.duplicate_move()
        bpy.data.objects['Slicer.001'].location = [0,0, SLICE_Zs[slide_number]]
        bpy.data.objects['Slicer'].location = [0,0, SLICE_Zs[slide_number+1]]

        # Import mesh
        bpy.ops.object.select_all(action='DESELECT')
        #bpy.ops.mesh.primitive_cube_add(size=30.0,location=(0,0,0))
        bpy.ops.import_mesh.stl(filepath=input_path)
        objects = bpy.context.selected_objects
        name_mesh = objects[0].name

        # Slice
        bpy.ops.object.select_all(action='DESELECT')

        bpy.data.objects['Slicer.001'].select_set(True)
        bpy.data.objects['Slicer'].select_set(True)

        bpy.data.objects[name_mesh].select_set(True)
        bpy.ops.slice.operator()

        # Extract the good layer
        bpy.ops.object.select_all(action='DESELECT')
        if slide_number == 0:
            bpy.data.objects["Bool result 0"].select_set(True)
        else:
            bpy.data.objects["Bool result 1"].select_set(True) # Select the part between the 2 plan

        bpy.ops.export_mesh.stl(filepath=str(output_path / f"{name_file}_slide_{slide_number}.stl"), use_selection=True)

# List file to process
raw_files = list(FILES_FOLDER.glob("*.stl"))
for file in raw_files:
    output_path =  SAVE_FOLDER / file.stem
    slice_a_file(file.stem, str(file), output_path)


# !/usr/bin/env python3
# -*- coding: utf-8 -*-
import time

def get_timestamp_sec():
    return int(time.time())

def get_timestamp_mills():
    return int(time.time() * 1000)

import urllib.request
import urllib.parse
import json
import ssl
ssl._create_default_https_context = ssl._create_unverified_context

def http_post_json(url, data):
    try:
        headers = {'Content-Type': 'application/json'}
        data = json.dumps(data)
        dataEncode = data.encode('utf-8')
        dataEncode = data.encode('utf-8')
        request = urllib.request.Request(url, dataEncode, headers)
        response = urllib.request.urlopen(request, timeout=30)
        respData = response.read().decode('utf-8')
        respJson = json.loads(respData)
        return respJson
    except urllib.error.URLError as e:
        print('err:', e)
        return {'success':False}
    except Exception as e:
        print('ex:', e)
        return {'success':False}

def http_get_json(url):
    try:
        request = urllib.request.Request(url)
        response = urllib.request.urlopen(request)
        respData = response.read().decode('utf-8')
        respJson = json.loads(respData)
        return respJson
    except urllib.error.URLError as e:
        print('err:', e)
        return {'success':False}
    except Exception as e:
        print('ex:', e)
        return {'success':False}

import base64

def img_to_base64(imgPath):
    with open(imgPath,'rb') as f:
        b64 = base64.b64encode(f.read())
        return b64.decode()

def base64_to_Img(base64Str, targetPath):
    with open(targetPath, 'wb') as f:
        imgBytes = base64.b64decode(base64Str.encode())
        f.write(imgBytes)
    f.close()

import uuid

def get_mac_addr():
    node = uuid.getnode()
    mac = uuid.UUID(int = node).hex[-12:]
    return mac

# def get_mac_addr():
#     import sys
#     import os
#     mac = None
#     print(sys.platform)
#     if sys.platform == "win32":
#         start = 0
#         for line in os.popen("ipconfig/all"):
#             if line.lstrip().startswith("网卡名字"):
#                 start = 1
#             if start == 1 and line.lstrip().startswith("Physical Address"):
#                 mac = line.split(":")[1].strip().replace("-", ":")
#                 break
#     else:
#         for line in os.popen("/sbin/ifconfig"):
#             if 'Ether' in line:
#                 mac = line.split()[1]
#                 break
#     return mac


import torch

from predictor.utils import load_model

def evaluate(model, valid_iter, path):
    load_model(path, model)
    model.eval()
    total = 0
    correct = 0
    with torch.no_grad():
        for seqs, labels, lengths in valid_iter:
            outputs = model(seqs, lengths)
            outputs = (outputs >= 0.5).float()
            total += labels.size(0)
            correct += (labels == outputs).sum().item()
    return round(correct / total * 100, 2)


# 给定一个字符串 s，找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。
#
# 示例 1：
#
# 输入: "babad"
# 输出: "bab"
# 注意: "aba" 也是一个有效答案。
#
#
# 示例 2：
#
# 输入: "cbbd"
# 输出: "bb"
#
# Related Topics 字符串 动态规划

# leetcode submit region begin(Prohibit modification and deletion)
class Solution:
    def longestPalindrome(self, s: str) -> str:
        n = len(s)
        dp = [[0] * n for _ in range(n)]
        res = ""
        for i in range(n - 1, -1, -1):
            for j in range(i, n):
                dp[i][j] = s[i] == s[j] and (j - i < 3 or dp[i + 1][j - 1])
                if dp[i][j] and j - i + 1 > len(res):
                    res = s[i:j + 1]
        return res

# leetcode submit region end(Prohibit modification and deletion)
print(Solution().longestPalindrome("cbbd"))


from typing import Callable
import dataclasses
from datetime import datetime

from src.utils.metrics import metrics

import pandas as pd
from sklearn.pipeline import Pipeline
from sklearn.metrics import classification_report
from sklearn.base import BaseEstimator

@dataclasses.dataclass
class Model:
    model_name: str = dataclasses.field()
    model_parameter_grid: dict[list] = dataclasses.field()
    model: BaseEstimator = dataclasses.field()

@dataclasses.dataclass
class ModelPredictorConfig:
    k_features: int = dataclasses.field()
    use_gabor: int = dataclasses.field()
    feature_selection_score_function: Callable = dataclasses.field()
    id_column: str = dataclasses.field()
    target_column: str = dataclasses.field()

class ModelPredictor:
    def __init__(self, config: ModelPredictorConfig):
        self.config = config

    def predict(self,
                model: Pipeline,
                dataset: pd.DataFrame,
                augmented_column: str,
                gabor_column: str) -> dict:
        non_feature_columns = [self.config.id_column,
                               self.config.target_column,
                               augmented_column, gabor_column]
        feature_columns = list(filter(lambda col:
                                      col not in non_feature_columns, dataset))
        dataset = dataset[dataset[augmented_column] == 0]

        start_time = datetime.now()
        prediction = model.predict(dataset[feature_columns])
        end_time = datetime.now()

        TP, TN, FP, FN = metrics.calculate_results(prediction,
                                                   dataset[self.config.target_column].tolist())
        classif_report = classification_report(dataset[self.config.target_column].tolist(),
                                               prediction, output_dict=True)
        test_macro_f1_score = classif_report['macro avg']['f1-score']
        test_weighted_f1_score = classif_report['weighted avg']['f1-score']
        test_macro_recall_score = classif_report['macro avg']['recall']
        test_weighted_recall_score = classif_report['weighted avg']['recall']
        test_macro_precision_score = classif_report['macro avg']['precision']
        test_weighted_precision_score = classif_report['weighted avg']['precision']
        test_accuracy_score = classif_report['accuracy']
        total_samples = metrics.calculate_total_samples(TP, TN, FP, FN)
        total_time = end_time - start_time

        saved_params = {'gabor': self.config.use_gabor}
        results = {'model_name': model.__class__.__name__,
                   'params': saved_params,
                   'accuracy': test_accuracy_score,
                   'f1_macro': test_macro_f1_score,
                   'f1_weighted': test_weighted_f1_score,
                   'recall_macro': test_macro_recall_score,
                   'recall_weighted': test_weighted_recall_score,
                   'precision_macro': test_macro_precision_score,
                   'precision_weighted': test_weighted_precision_score,
                   'TP': TP, 'TN': TN, 'FP': FP, 'FN': FN,
                   'total_samples': total_samples,
                   'total_time': total_time}

        return results


import re
import requests
from rdflib import Graph

class NameSpaces():
    def __init__(self, url, debug=False):
        self.stats = {}
        self.json = {}
        self.stats = {}
        self.namespaces = {}
        self.DEBUG = debug
        self.content = requests.get(url)
        self.url = url
        self.processor(self.content)
        self.graph = False
        return

    def load_graph(self, url):
        self.graph = Graph()
        self.graph.parse(url, format='ttl')
        return self.graph

    def get_namespace(self, spacestring):
        namespacecheck = re.search(r"\@prefix\s+(\S+)\:\s+<(\S+)>", spacestring)
        if namespacecheck:
            self.namespaces[namespacecheck.group(2)] = namespacecheck.group(1)
        return self.namespaces

    def processor(self, content):
        data = content.text.split('\n')
        success = False
        for line in data:
            if '@prefix' in line:
                self.get_namespace(line)
                success = True
        return success

    def counter(self, concept):
        for namespace in self.namespaces:
            if namespace in concept:
                if self.DEBUG:
                    print(self.namespaces[namespace])
                if self.namespaces[namespace] in self.stats:
                    self.stats[self.namespaces[namespace]] = self.stats[self.namespaces[namespace]] + 1
                else:
                    self.stats[self.namespaces[namespace]] = 1
        return self.stats

    def getstats(self):
        return self.stats

    def getnamespaces(self):
        return self.namespaces

    def getstatements(self):
        lines = 0
        for s, p, o in self.load_graph(self.url):
            self.counter("%s" % s)
            self.counter("%s" % p)
            self.counter("%s" % o)
            lines = lines + 1
        return lines


import socket
import select
sockL = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
host = ""
port = 60003
sockL.bind((host, port))
sockL.listen(1)

ListOfSockets = [sockL]
print("Listening on port {}".format(port))

while True:
    tup = select.select(ListOfSockets, [], [])
    sock = tup[0][0]
    if sock == sockL:
        client_host, client_port = sockL.accept()
        ListOfSockets.append(client_host)
        for i in range(1,len(ListOfSockets)):
            if ListOfSockets[i] != sock:
                ListOfSockets[i].sendall(bytearray("[{}] (Connected)".format(client_port),"ascii"))
            else:
                print("Unable to connect {}".format(client_port))
    else:
        data = sock.recv(2048).decode()
        if not data:
            for i in range(1, len(ListOfSockets)):
                if ListOfSockets[i] != sock:
                    ListOfSockets[i].sendall(bytearray("[{}] (Diconnected)".format(client_port),"ascii"))
            sock.close()
            for i in range(1, len(ListOfSockets)):
                if ListOfSockets[i] == sock:
                    ListOfSockets.pop(i)
                    break

        else:
            for i in range(1,len(ListOfSockets)):
                ListOfSockets[i].sendall(bytearray(data,"ascii"))


import vtk
from GemoBase import *
import ClipperAdaptor
import math
import Polyline
import Segment

from pyclipper import *
from GemoAlgo import *

class SweepLine:
    def __init__(self):
        self.segs = []  # 属性segs，Segment类型列表

    def intersect(self, y):  # 定义segs和y线的求交列表
        ips = []  # 交点列表
        yLine = Line(Point3D(0, y, self.segs[0].A.z), Vector3D(1, 0, 0))  # 创建和X轴平行的y线
        for seg in self.segs:
            if seg.A.y == y:
                ips.append(seg.A.clone())
            elif seg.B.y == y:
                ips.append(seg.B.clone())
            else:
                ip = intersect(yLine, seg)
                if ip is not None:
                    ips.append(ip)
        ips.sort(key=lambda p: p.x)  # 对交点按x值从小到大排列
        i = len(ips) - 1
        while i > 0:  # 移除交点中重合的点
            if ips[i].distanceSquare(ips[i - 1]) == 0:
                del ips[i]
                del ips[i - 1]
                i = i - 2
            else:
                i = i - 1
        return ips  # 返回处理后的交点列表

def genClipHatches(polygons, interval, angle):
    xMin, xMax = float('inf'), float('-inf')
    yMin, yMax = float('inf'), float('-inf')
    z = polygons[0].points[0].z
    for poly in polygons:
        for pt in poly.points:
            xMin, xMax = min(xMin, pt.x), max(xMax, pt.x)
            yMin, yMax = min(xMin, pt.y), max(yMax, pt.y)
    v = Vector3D(math.cos(angle), math.sin(angle))  # 填充方向向量
    n = Vector3D(math.cos(angle + math.pi / 2), math.sin(angle + math.pi / 2))  # 填充方向法向量
    O = Point3D((xMin + xMax) / 2, (yMin + yMax) / 2, z)  # 多边形包络矩形的圆中心
    R = math.sqrt((xMax - xMin) ** 2 + (yMax - yMin) ** 2) / 2  # 外接圆半径
    P1 = O - n.amplified(R)
    parallels = []  # 保存生成的平行线段
    for i in range(0, int(2 * R / interval) + 1, 1):
        Q = P1 + n.amplified(interval * i)
        seg = Polyline.Polyline()
        seg.addPoint(Q - v.amplified(R))
        seg.addPoint(Q + v.amplified(R))
        parallels.append(seg)
    hatchSegs = []
    ca = ClipperAdaptor.ClipperAdaptor()
    clipper = Pyclipper()
    clipper.AddPaths(ca.PolyToPaths(polygons), PT_CLIP, True)
    clipper.AddPaths(ca.PolyToPaths(parallels), PT_SUBJECT, False)
    sln = clipper.Execute2(CT_INTERSECTION)
    for child in sln.Childs:
        if len(child.Contour) > 0:
            poly = ca.PathToPoly(child.Contour, z, False)
            seg = Segment(poly.startPoint(), poly.endPoint())
            hatchSegs.append(seg)
    return hatchSegs

def calcHatchPoints(polygons, ys):  # 输入多边形和扫描线高度ys
    segs = []  # 存放多边形的线段列表
    for poly in polygons:
        for i in range(poly.count() - 1):
            seg = Segment(poly.point(i), poly.point(i + 1))
            seg.yMin, seg.yMax = min(seg.A.y, seg.B.y), max(seg.A.y, seg.B.y)
            segs.append(seg)
    segs.sort(key=lambda p: p.yMin)
    k = 0
    sweep = SweepLine()
    ipses = []
    for y in ys:
        for i in range(len(sweep.segs) - 1, -1, -1):  # 移除不再和扫描线相交的线段
            if sweep.segs[i].yMax < y:
                del sweep.segs[i]
        for i in range(k, len(segs)):  # 从segs添加和扫描线相交的线段
            if segs[i].yMin < y and segs[i].yMax >= y:
                sweep.segs.append(segs[i])
            elif segs[i].yMin >= y:
                k = i
                break
        if len(sweep.segs) > 0:
            ips = sweep.intersect(y)
            ipses.append(ips)
    return ipses

def genSweepHatches(polygons, interval, angle):
    mt = Matrix3D.createRotateMatrix('Z', -angle)  # 旋转矩阵，取
    mb = Matrix3D.createRotateMatrix('Z', angle)  # 旋转矩阵，回
    rotPolys = []  # 存储旋转后的多边形
    for poly in polygons:
        rotPolys.append(poly.multiplied(mt))
    yMin, yMax = float('inf'), float('-inf')
    for poly in rotPolys:
        for pt in poly.points:
            yMin = min(yMin, pt.y)
            yMax = max(yMax, pt.y)
    ys = []
    y = yMin + interval
    while y < yMax:
        ys.append(y)
        y += interval
    segs = genHatches(rotPolys, ys)
    for seg in segs:
        seg.multiply(mb)
    return segs

def genHatches(polygons, ys):
    segs = []
    ipses = calcHatchPoints(polygons, ys)
    for ips in ipses:
        if len(ips) < 2:
            continue
        for i in range(0, len(ips), 2):
            seg = Segment(ips[i], ips[i + 1])
            segs.append(seg)
    return segs


#!/usr/bin/python3
"""List all Git projects and run gitleaks all
   Before running
        create the "repositories" and "leaks_reports" folders
        change GIT_ENPOINT
        change GIT_PRIVATE_TOKEN
"""
import subprocess
import os
from datetime import date
from multiprocessing.dummy import Pool as ThreadPool
import json
import gitlab

pwned_commands = []
clean_commands = []
today = date.today()
today_frmt = today.strftime("%b-%d-%Y")

def call_cmd(command_str):
    """Executes a command on the host machine."""
    try:
        subprocess.check_output(command_str, shell=True)
        if "gitleaks" in command_str:
            clean_commands.append(command_str)
    except BaseException:
        if "gitleaks" in command_str:
            pwned_commands.append(command_str)

def clone(ssh_url_to_repo, name):
    """Clones repository to the machine."""
    cmd = f"git clone {ssh_url_to_repo} repositories/{name}"
    call_cmd(cmd)

def gitleak_check(name, http_link):
    """Executes gitleak on a repository."""
    cmd = f"./gitleaks detect -s repositories/{name} --report-path leaks_reports/secrets_{name}.json"
    call_cmd(cmd)
    append_link(name, http_link)

def append_link(name, http_link):
    """Adds repository link to json report."""
    web = '{"RepoUrl": "'+  http_link + '\"},'
    try:
        with open(f"leaks_reports/secrets_{name}.json", "r+") as jsonFile:
            data_lines = jsonFile.read()
            if len(data_lines) <= 3:
                os.remove(f"leaks_reports/{name}.json")
            else:
                jsonFile.seek(0,0)
                a ='[\n '+ web+ '\n' + data_lines[2:]
                jsonFile.write(a)
    except:
        pass

def remove_repo(name):
    """Deletes Repository from the machine."""
    cmd = f"rm -rf repositories/{name}"
    call_cmd(cmd)

def organize(repos):
    """Schedules everything."""
    urls = repos.split("_")
    name = urls[0].split("/")
    ssh_url_to_repo = urls[0]
    repo_name_ssh = name[-1]
    repo_name_web = urls[1]
    clone(ssh_url_to_repo, repo_name_ssh)
    gitleak_check(repo_name_ssh, repo_name_web)
    remove_repo(repo_name_ssh)

if __name__ == "__main__":

    GIT_ENPOINT = "https://" # replace gitlab instance link
    GIT_PRIVATE_TOKEN = ""  # replace token

    gl = gitlab.Gitlab(url=GIT_ENPOINT, private_token=GIT_PRIVATE_TOKEN)
    gl.auth()
    git_projects = gl.projects.list(get_all=True)  # return all projects as a list, warning it will take time
    projects = []
    for project in git_projects:
        concatenated = project.ssh_url_to_repo + "_" + project.web_url
        projects.append(concatenated)

    pool = ThreadPool(os.cpu_count())  # only use the number of cores
    organizer = pool.map(organize, projects)
    pool.close()  # Close the pool
    pool.join()  # Wait for the work to finish

    with open(
        f"leaks_reports/{today_frmt}_pwned_commands.txt", "w", encoding="utf-8"
    ) as fp:
        for pwned_command in pwned_commands:
            fp.write(f"{pwned_command}\n")
    with open(
        f"leaks_reports/{today_frmt}_clean_commands.txt", "w", encoding="utf-8"
    ) as fp:
        for successful_command in clean_commands:
            fp.write(f"{successful_command}\n")

    print("Scan Done!")

    reports = os.listdir("leaks_reports/")
    for report in reports:
        if ".json" in report:
            try:
                fp = open(f"leaks_reports/{report}", "r", encoding="utf-8")
                file_content = json.load(fp)
                fp.close()
                if len(file_content) == 0:
                    os.remove(f"leaks_reports/{report}")
            except BaseException:
                pass


#! /usr/bin/env python3

import sys

def valueerror():
    try:
        num = int('foo')
    except ValueError:
        print ("invalid syntax")

def division_by_zero():
    try:
        num = 5/0
    except ValueError:
        print ("Value Error")
    except ZeroDivisionError:
        print ("division by zero")
    else:
        print ("good")

def unknow_error():
    try:
        num = 5/0
    except:
        print(f"{sys.exc_info()}")
    else:
        print("good")

def main():
    valueerror()
    division_by_zero()
    unknow_error()

if __name__ == "__main__":main()


from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from walkysite import settings
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
	#url(r'^$', 'walkysite.views.home', name='home'),
	url(r'^$', 'walkysite.views.index'),
	url(r'^jsdev/', include('jsdev.urls', namespace="jsdev")),
	#url(r'^jsdev/$', include('jsdev.urls', namespace="jsdev")),
	url(r'^maps_test/$', 'route.views.maps_test'),
	url(r'^maps/$', 'route.views.maps'),
	#url(r'^mapsdraw/$', 'route.views.mapsdraw'),
	#url(r'^updt/$', 'route.views.save_data'),
	url(r'^updt/$', 'route.views.update'),
	url(r'^cleardb/$', 'route.views.clearDB'),
	url(r'^all/$', include('route.urls')),
	#url(r'^map_from_db/$', 'route.views.show_map'),
	url(r'^map_from_db/$', 'route.views.get_points'),
	url(r'^places/$', 'route.views.places'),
	url(r'^routeList/$', 'route.views.routeList'),
    # url(r'^$', 'walkysite.views.home', name='home'),
    # url(r'^walkysite/', include('walkysite.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(admin.site.urls)),
)#+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += staticfiles_urlpatterns()

#MEdia path for map_images
urlpatterns += patterns('',
        url(r'^media/(?P<path>.*)$',
            'django.views.static.serve',
            {'document_root': settings.MEDIA_ROOT, }),
    )


from typing import List

import jsonlines
from torch.utils.data import Dataset

def load_data(name: str, path: str, supervise: bool) -> List:
    """根据名字加载不同的数据集."""

    def load_snli_data(path, supervise):
        with jsonlines.open(path, "r") as f:
            return [
                (line["origin"], line["entailment"], line["contradiction"])
                if supervise
                else line["origin"]
                for line in f
            ]

    def load_lqcmc_data(path):
        with open(path, "r", encoding="utf8") as f:
            return [line.strip().split("\t")[0] for line in f]

    def load_sts_data(path):
        with open(path, "r", encoding="utf8") as f:
            return [(line.split("||")[1], line.split("||")[2], line.split("||")[3]) for line in f]

    assert name in ["snli", "lqcmc", "sts"]
    if name == "snli":
        return load_snli_data(path, supervise)
    elif name == "lqcmc":
        return load_lqcmc_data(path)
    elif name == "sts":
        return load_sts_data(path)

class SimcseDataset(Dataset):
    """训练数据集, 重写__getitem__和__len__方法."""

    def __init__(self, data: List, split, supervise, tokenizer, max_length):
        self.data = data
        self.split = split
        self.supervise = supervise
        self.tokenizer = tokenizer
        self.max_length = max_length

    def __len__(self):
        return len(self.data)

    def text_2_id(self, text: str):
        if self.split == "train":
            if self.supervise is False:
                return self.tokenizer(
                    [text, text],
                    max_length=self.max_length,
                    truncation=True,
                    padding="max_length",
                    return_tensors="pt",
                )
            else:
                return self.tokenizer(
                    [text[0], text[1], text[2]],
                    max_length=self.max_length,
                    truncation=True,
                    padding="max_length",
                    return_tensors="pt",
                )
        elif self.split == "test":
            return self.tokenizer(
                text,
                max_length=self.max_length,
                truncation=True,
                padding="max_length",
                return_tensors="pt",
            )

    def __getitem__(self, index: int):
        if self.split == "train":
            return self.text_2_id(self.data[index])
        elif self.split == "test":
            line = self.data[index]
            return self.text_2_id([line[0]]), self.text_2_id([line[1]]), int(line[2])


"""Word Finder: finds random words from a dictionary."""
import random

class WordFinder:
    def __init__(self, file_path):
        self.word_list = self.get_list(file_path)
        print(f"{len(self.word_list)} words read")

    def get_list(self, file_path):
        length = random.randint(3, 10)
        word_list = []
        file = open(file_path)
        words = file.read().split()

        i = 0
        while i < length:
            word_list.append(random.choice(words))
            i += 1
        file.close()
        return list(set(word_list))

    def random(self):
        return self.word_list[random.randint(0, len(self.word_list) - 1)]

wf = WordFinder('words.txt')

print(wf.random())
print(wf.random())
print(wf.random())

class RandomWordFinder(WordFinder):
    def __init__(self, file_path):
        self.word_list = self.get_list(file_path)
        print(f"{len(self.word_list)} words read")

    def get_list(self, file_path):
        words = list(set(super().get_list(file_path)))
        for word in words:
            if word == "" or word.startswith("#"):
                words.remove(word)
        return words

rwf = RandomWordFinder("more_words.txt")

print(rwf.random())
print(rwf.random())
print(rwf.random())


#상하좌우
DIR = [[-1,0],[1,0],[0,-1],[0,1]]
BLOCK = {1:[[1,0],[0,1],[-1,0],[0,-1]], 2:[[0,1],[-1,0],[1,0],[0,-1]], 3:[[0,-1],[-1,0],[0,1],[1,0]], 4:[[1,0], [0,-1],[0,1],[-1,0]], 5:[[1,0],[-1,0],[0,1],[0,-1]]}
# T = int(input())
T = 1
for _ in range(T):
    # N = int(input())
    N = 10
    # MAP = [list(map(int,input().split())) for i in range(N)]
    MAP = [[0, 1, 0, 3, 0, 0, 0, 0, 7, 0], [0, 0, 0, 0, -1, 0, 5, 0, 0, 0], [0, 4, 0, 0, 0, 3, 0, 0, 2, 2], [1, 0, 0, 0, 1, 0, 0, 3, 0, 0], [0, 0, 3, 0, 0, 0, 0, 0, 6, 0], [3, 0, 0, 0, 2, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 4, 0], [0, 5, 0, 4, 1, 0, 7, 0, 0, 5], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [2, 0, 6, 0, 0, 4, 0, 0, 0, 4]]
    HOLE, MAX = dict(), float('-inf')
    for col in range(N):
        for row in range(N):
            if MAP[col][row] > 5: HOLE[MAP[col][row]] = HOLE.get(MAP[col][row], [])+[[col,row]]
    print(HOLE)
    POINT = [2,3]
    direction = 3
    # POINT = [6,3]
    init_y, init_x = 2,3
    # direction = 2
    for col in range(N):
        for row in range(N):
            if MAP[col][row] == 0:
                print(">>", col, row)
                for direction in range(4):
                    POINT = [col,row]
                    init_y, init_x = col,row
                    answer = 0
                    while True:
                        new_y, new_x = POINT[0]+DIR[direction][0], POINT[1]+DIR[direction][1]
                        if new_y<0 or new_y >=N or new_x<0 or new_x>=N:
                            answer += 1
                            direction  = direction + 1 if direction %2 == 0 else direction -1
                        elif 1<=MAP[new_y][new_x]<=5:
                            # print("??",  direction, DIR.index(BLOCK[MAP[new_y][new_x]][direction]), BLOCK[MAP[new_y][new_x]][direction])
                            answer += 1
                            direction = DIR.index(BLOCK[MAP[new_y][new_x]][direction])
                        elif 6<=MAP[new_y][new_x]<=10:
                            idx = 1- HOLE[MAP[new_y][new_x]].index([new_y,new_x])
                            new_y, new_x = HOLE[MAP[new_y][new_x]][idx]
                        if new_y == init_y and new_x == init_x: break
                        # print(new_y, new_x)
                        if 0<=new_y<N and 0<=new_x<N and MAP[new_y][new_x] == -1: break
                        POINT = [new_y,new_x]
                    # print(answer)
                    if MAX<answer: MAX = answer
    print(MAX)
"""
DIR = [[-1,0],[1,0],[0,-1],[0,1]]
first = [[1,0],[0,1],[-1,0],[0,-1]]
second = [[0,1],[-1,0],[1,0],[0,-1]]
third = [[0,-1],[-1,0],[0,1],[1,0]]
fourth = [[1,0], [0,-1], [0,1],[-1,0]]
fifth = [[1,0],[-1,0],[0,1],[0,-1]]
SHAPE = [[],first, second, third, fourth, fifth]
# T = int(input())
T = 1
for _ in range(T):
    # N = int(input())
    # MAP = [list(map(int,input().split())) for i in range(N)]
    N = 4
    MAP = [[0,0,0,3],[0,0,0,0],[1,0,0,4],[0,0,0,0]]
    MAX = 0

    BLOCK, HOLE, START = [], [], []
    BLOCK_MAP, HOLE_MAP, BLACK_HOLE_MAP = [[False] * N for i in range(N)], [[False] * N for i in range(N)], [[False] * N for i in range(N)]
    WARM_HOLE_DICT ={}

    for col in range(N):
        for row in range(N):
            if 6<=MAP[col][row]<=10: WARM_HOLE_DICT[MAP[col][row]] = WARM_HOLE_DICT.get(MAP[col][row],[]) + [[col,row]]

    for start_y in range(N):
        for start_x in range(N):
            if MAP[start_y][start_x] == 0 and start_y==0 and start_x==1:

                for idx in range(4):
                    y,x = start_y, start_x
                    count = 0
                    print("idx===============", idx, y, x)
                    while True:
                        y,x = y+DIR[idx][0], x+DIR[idx][1]
                        print(">>>", y,x,idx,count)
                        if y == start_y and x== start_x: break
                        elif 0<=x<N and 0<=y<N and MAP[y][x] == -1:break
                        elif y < 0 or y >=N or x < 0 or x >= N:
                            idx = idx-1 if idx%2==1 else idx + 1
                            count += 1
                        elif 1<=MAP[y][x]<=5:
                            idx = DIR.index(SHAPE[MAP[y][x]][idx])
                            count += 1
                        elif 6<=MAP[y][x]<=10:
                            if y == WARM_HOLE_DICT[MAP[y][x]][0][0] and x == WARM_HOLE_DICT[MAP[y][x]][0][1]:
                                y, x = WARM_HOLE_DICT[MAP[y][x]][1][0], WARM_HOLE_DICT[MAP[y][x]][1][1]
                            else: y, x = WARM_HOLE_DICT[MAP[y][x]][0][0], WARM_HOLE_DICT[MAP[y][x]][0][1]

                    if MAX < count:
                        print(start_y, start_x,idx, count)
                        MAX = count
    print("#{} {}".format(_+1,MAX))

"""

'''
    # print(WARM_HOLE_DICT)
    # for item in MAP: print(item)
    for start_x, start_y in START:
        y,x = start_y, start_x
        for idx in range(4):
            count = 0
            while True:
                y,x = y+DIR[idx][0], x+DIR[idx][1]

                if y == start_y and x== start_x: break
                elif 0<=x<N and 0<=y<N and MAP[y][x] == -1:break
                elif y < 0 or y >=N or x < 0 or x >= N:
                    idx = idx-1 if idx%2==1 else idx + 1
                    count += 1
                elif 1<=MAP[y][x]<=5:
                    idx = DIR.index(SHAPE[MAP[y][x]][idx])
                    count += 1
                elif 6<=MAP[y][x]<=10:
                    if y == WARM_HOLE_DICT[MAP[y][x]][0][0] and x == WARM_HOLE_DICT[MAP[y][x]][0][1]:
                        y, x = WARM_HOLE_DICT[MAP[y][x]][1][0], WARM_HOLE_DICT[MAP[y][x]][1][1]
                    else: y, x = WARM_HOLE_DICT[MAP[y][x]][0][0], WARM_HOLE_DICT[MAP[y][x]][0][1]

            if MAX < count: MAX = count
        # break
    print(MAX)
    '''


def ePrimo(numero):
    for num in range(2,numero):
        if numero % num == 0:
            return False

    return True

def n_primos(n):
    cont = 0
    while n >= 2:
        primo = ePrimo(n)
        if primo:
            cont += 1
        n-= 1
    return cont

Coursera/Introdução à Ciência da Computação com Python Parte 1/Semana 7 - EX - opcional/main.py


import os
import sys
import multiprocessing
import traceback
import time
import warnings
multiprocessing.freeze_support()

# Find module path
MODULE_PATH = os.path.dirname(os.path.realpath(__file__))
# Find ROOT folder
ROOT = os.path.split(MODULE_PATH)[0]

# Add Utilities dir to path
try:
    from .utilities import JLog
except Exception:
    # Reverse compatibility step - Add utilities folder to path directly
    PYTHON_SCRIPTS_FOLDER = os.path.join(ROOT, 'Python Scripts')
    TEST = os.path.exists(PYTHON_SCRIPTS_FOLDER)
    if TEST:
        UTILITIES_FOLDER = os.path.join(PYTHON_SCRIPTS_FOLDER, 'utilities')
        sys.path.append(UTILITIES_FOLDER)
    else:
        ARC_FOLDER = os.path.join(ROOT, 'arc')
        UTILITIES_FOLDER = os.path.join(ARC_FOLDER, 'utilities')
        sys.path.append(UTILITIES_FOLDER)
    import JLog

class Minion(multiprocessing.Process):
    """Multiprocessing worker class"""
    def __init__(self, task_queue, result_queue):
        multiprocessing.Process.__init__(self)
        self.task_queue = task_queue
        self.result_queue = result_queue
        self.proc_name = None
        self.log = None
    def start_log(self):
        """Instantiates Logging class"""
        # Create PrintLog
        self.log = JLog.PrintLog()
        self.proc_name = self.name
    def run(self):
        warnings.filterwarnings("ignore")
#        f = open(os.devnull, 'w')
#        sys.stderr = f
        num_jobs = 0
        max_jobs = 100  # Max jobs before terminating
        sleep_time = 1
        while True:
            try:
                next_task = self.task_queue.get()
                sleep_time = 1
            except Exception:
                time.sleep(sleep_time)
                if sleep_time < 10:
                    sleep_time += .5
            if next_task is None:
                # Poison pill means shutdown
                break
            try:
                result = next_task()
            except Exception:
#                sys.stderr = sys.__stderr__
                self.start_log()
                self.log.Wrap('------------------------------------------')
                self.log.Wrap("EXCEPTION:")
                self.log.Wrap(traceback.format_exc())
                self.log.Wrap('------------------------------------------')
            self.result_queue.put(result)
            num_jobs += 1
            if num_jobs > max_jobs:
                self.result_queue.put("Maxed")
                break
        return
# End of Minion


#!/usr/bin/env python

import plotly.graph_objs as go
from plotly.offline import plot

#predict_isoelectric_point_ProMoST code from
#http://isoelectric.ovh.org/
#http://biologydirect.biomedcentral.com/articles/10.1186/s13062-016-0159-9
#IPC - Isoelectric Point Calculator
#Lukasz P. Kozlowski
#Biology Direct 2016
#DOI: 10.1186/s13062-016-0159-9

# N-teminus, middle, C-terminus
promost = {
    'K': [10.00, 9.80, 10.30],
    'R': [11.50, 12.50, 11.50],
    'H': [4.89, 6.08, 6.89],
    'D': [3.57, 4.07, 4.57],
    'E': [4.15, 4.45, 4.75],
    'C': [8.00, 8.28, 9.00],
    'Y': [9.34, 9.84, 10.34],
    'U': [5.20, 5.43, 5.60],  # ref (http://onlinelibrary.wiley.com/doi/10.1002/bip.21581/pdf)
}

promost_mid = {
    "G": [7.50, 3.70],
    "A": [7.58, 3.75],
    "S": [6.86, 3.61],
    "P": [8.36, 3.40],
    "V": [7.44, 3.69],
    "T": [7.02, 3.57],
    "C": [8.12, 3.10],
    "I": [7.48, 3.72],
    "L": [7.46, 3.73],
    "J": [7.46, 3.73],
    "N": [7.22, 3.64],
    "D": [7.70, 3.50],
    "Q": [6.73, 3.57],
    "K": [6.67, 3.40],
    "E": [7.19, 3.50],
    "M": [6.98, 3.68],
    "H": [7.18, 3.17],
    "F": [6.96, 3.98],
    "R": [6.76, 3.41],
    "Y": [6.83, 3.60],
    "W": [7.11, 3.78],
    "X": [7.26, 3.57],  # avg
    "Z": [6.96, 3.535],  # ("E"+"Q")/2
    'B': [7.46, 3.57],  # ("N"+"D")/2
    'U': [5.20, 5.60],
    'O': [7.00, 3.50],
}

import sys
def predict_isoelectric_point_ProMoST(seq):
    '''Calculate isoelectric point using ProMoST model'''
    NQ = 0.0
    pH = 6.51  # starting po pI = 6.5 - theoretically it should be 7, but average protein pI is 6.5 so we increase the probability of finding the solution
    pHprev = 0.0
    pHnext = 14.0
    E = 0.01  # epsilon means precision [pI = pH +- E]
    temp = 0.01
    while 1:
        if seq[0] in promost:
            QN1 = -1.0 / (1.0 + pow(10, (promost[seq[0]][2] - pH)))
        else:
            QN1 = -1.0 / (1.0 + pow(10, (promost_mid[seq[0]][1] - pH)))
        # print
        if seq[-1] in promost:
            QP2 = 1.0 / (1.0 + pow(10, (pH - promost[seq[-1]][0])))
        elif seq[-1] in promost_mid:
            QP2 = 1.0 / (1.0 + pow(10, (pH - promost_mid[seq[-1]][0])))
        else:
            print(seq[-1] + " not found!")

            sys.exit(1)

        QN2 = -seq.count('D') / (1.0 + pow(10, (promost['D'][1] - pH)))
        QN3 = -seq.count('E') / (1.0 + pow(10, (promost['E'][1] - pH)))
        QN4 = -seq.count('C') / (1.0 + pow(10, (promost['C'][1] - pH)))
        QN5 = -seq.count('Y') / (1.0 + pow(10, (promost['Y'][1] - pH)))
        QP1 = seq.count('H') / (1.0 + pow(10, (pH - promost['H'][1])))
        QP3 = seq.count('K') / (1.0 + pow(10, (pH - promost['K'][1])))
        QP4 = seq.count('R') / (1.0 + pow(10, (pH - promost['R'][1])))

        NQ = QN1 + QN2 + QN3 + QN4 + QN5 + QP1 + QP2 + QP3 + QP4
        # %%%%%%%%%%%%%%%%%%%%%%%%%   BISECTION   %%%%%%%%%%%%%%%%%%%%%%%%
        if NQ < 0.0:  # we are out of range, thus the new pH value must be smaller
            temp = pH
            pH = pH - ((pH - pHprev) / 2.0)
            pHnext = temp
            # print "pH: ", pH, ", \tpHnext: ",pHnext
        else:
            temp = pH
            pH = pH + ((pHnext - pH) / 2.0)
            pHprev = temp
            # print "pH: ", pH, ",\tpHprev: ", pHprev

        if (pH - pHprev < E) and (pHnext - pH < E):  # terminal condition, finding pI with given precision
            return round(pH,2)

mass_aa = { #monoisotopic 	average #Kyte-Doolittle?
"A"	: 71.0788,
"B" : 114.6686,   # Asx   Aspartic acid or Asparagine
"C"	: 103.1388,
"D"	: 115.0886,
"E"	: 129.1155,
"F"	: 147.1766,
"G"	: 57.0519,
"H"	: 137.1411,
"I"	: 113.1594,
"K"	: 128.1741,
"L"	: 113.1594,
"M"	: 131.1926,
"N"	: 114.1038,
"O"	: 237.3018,
"P"	: 97.1167,
"Q"	: 128.1307,
"R"	: 156.1875,
"S"	: 87.0782,
"T"	: 101.1051,
"U"	: 150.0388,
"V"	: 99.1326,
"W"	: 186.2132,
"X" : 111.1138, # Xaa   Any amino acid
"Y"	: 163.176,
"Z" : 128.7531    #Glx   Glutamine or Glutamic acid
}

# Kyte-Doolittle Hydropathy Scores
hydro_scores = {
"A" : 1.8,
"R" : -4.5,
"N" : -3.5,
"D" : -3.5,
"C" : 2.5,
"Q" : -3.5,
"E" : -3.5,
"G" : -0.4,
"H" : -3.2,
"I" : 4.5,
"L" : 3.8,
"K" : -3.9,
"M" : 1.9,
"F" : 2.8,
"P" : -1.6,
"S" : -0.8,
"T" : -0.7,
"W" : -0.9,
"Y" : -1.3,
"V" : 4.2

 }

def calculate_MW(seq): #calculate based on average mass
    mw = 0.0
    for c in seq:
        if c in mass_aa: mw += mass_aa[c]
    mw += 18.01524 #avg for water molecule
    return round(mw,2)

def calculate_hydro(seq):
    hydro = 0.0
    for c in seq:
        if c in hydro_scores: hydro += hydro_scores[c]
    return round(hydro,2)

from skbio.diversity import alpha as skbio_alpha
from skbio.diversity import beta as skbio_beta
import itertools

def mercat_compute_alpha_beta_diversity(counts,bif):

    abm = dict()

    abm['shannon'] = skbio_alpha.shannon(counts)
    abm['simpson'] = skbio_alpha.simpson(counts)
    abm['simpson_e'] = skbio_alpha.simpson_e(counts)
    abm['goods_coverage'] = skbio_alpha.goods_coverage(counts)
    abm['fisher_alpha'] = skbio_alpha.fisher_alpha(counts)
    abm['dominance'] = skbio_alpha.dominance(counts)
    abm['chao1'] = skbio_alpha.chao1(counts)
    abm['chao1_ci'] = skbio_alpha.chao1_ci(counts)
    abm['ace'] = skbio_alpha.ace(counts)

    with open(bif + "_diversity_metrics.txt", 'w') as dmptr:
        for abmetric in abm:
            dmptr.write(abmetric + " = " + str(abm[abmetric]) + "\n")

def mercat_scatter_plots(bif,xlab,res_df,kmerstring):

    axis_title_font_size = 20
    axis_tick_label_size = 18
    legend_font_size = 14
    marker_size = 10

    save_img = 0

    trace1 = go.Scatter(
        x=res_df[xlab].values,
        y=res_df.index.values,
        mode='markers'
    )

    data = go.Data([trace1])
    layout = go.Layout(
        legend=dict(
            font=dict(
                # family='sans-serif',
                size=legend_font_size,
                color='black'
            ),
            # borderwidth=2
        ),
        autosize=True,
        height=632,
        # title='Baseline',
        width=1274,
        xaxis=dict(
            # autorange=True,
            fixedrange=False,
            title=xlab,
            # type='linear',
            # showgrid=False,
            ticks='inside',
            ticklen=8,
            tickwidth=2,
            tickcolor='#000',
            #tickvals=list(reversed(cores)),
            titlefont=dict(
                # family='Courier New, monospace',
                size=axis_title_font_size,
                color='black'
            ),
            tickfont=dict(
                # family='Old Standard TT, serif',
                size=axis_tick_label_size,
                color='black'
            ),
            showgrid=True,
            showline=True,
            mirror=True,
            zeroline=False,
            # gridcolor='black',
            # linecolor='black',
            gridwidth=2
        ),
        yaxis=dict(
            #type='log',
            autorange=True,
            fixedrange=False,
            ticks='inside',
            ticklen=8,
            tickwidth=2,
            tickcolor='#000',
            titlefont=dict(
                # family='Courier New, monospace',
                size=axis_title_font_size,
                color='black'
                # color='#7f7f7f'
            ),
            rangemode='normal',
            tickmode='linear',
            # tickwidth=4,
            tickfont=dict(
                # family='Old Standard TT, serif',
                size=axis_tick_label_size,
                color='black'
            ),
            showgrid=True,
            showline=True,
            mirror=True,
            zeroline=False,
            # gridcolor='black',
            # linecolor='black',
            gridwidth=2,
            title=kmerstring
        )
    )

    fig = go.Figure(data=data, layout=layout)
    plot(fig, filename=bif + "_"+xlab+".html", auto_open=False)

def mercat_stackedbar_plots(inp_folder,top10_all_samples,xlab,kmerstring):
    axis_title_font_size = 20
    axis_tick_label_size = 18
    legend_font_size = 14
    marker_size = 10

    topk10 = 10
    all_samples = list(top10_all_samples.keys())
    btraces = []
    kmer_percent = dict()

    kmernames = dict()

    for bif in top10_all_samples:
        res_df,total_freq_count = top10_all_samples[bif]
        index_vals = res_df.index.values
        kmernames[bif]=index_vals
        #topk10 = min(len(index_vals), 10)
        #If a small sample has less than 10kmers, ignore it
        if len(index_vals) < 10: continue

        kmer_percent[bif] = []
        for i in range(0, topk10):
            fr = res_df.loc[index_vals[i], 'Count']
            fr = round(float(fr) * 100.0 / total_freq_count,1)
            kmer_percent[bif].append(fr)

    ylist = dict()
    all_kmer_percents = (list(kmer_percent.values()))
    ylist[0] = list(itertools.repeat(0,len(all_samples)))
    for i in range(1,topk10+1):
        ylist[i] = [x[i-1] for x in all_kmer_percents]

    all_annotations = []

    for s in ylist:
        if s==0: continue
        trace1 = go.Bar(
            x=all_samples,
            y=ylist[s],
            name="kmer" + str(s)
        )
        btraces.append(trace1)

        annotations = []
        for index, (xi, yi) in enumerate(zip(all_samples, ylist[s])):
            prevy = 0
            for z in range(0, s):
                prevy += ylist[z][index]

            annotations.append(
                 dict(x=xi, y=yi+prevy,
                 text=str(kmernames[xi][s-1]+": " + str(yi)+"%"),
                 xanchor='center',
                 yanchor='bottom',
                 showarrow=False,
                 )
            )

        all_annotations.append(annotations)

    data = go.Data(btraces)
    layout = go.Layout(
        barmode='stack',
        annotations=list(itertools.chain.from_iterable(all_annotations)),
        legend=dict(
            font=dict(
                # family='sans-serif',
                size=legend_font_size,
                color='black'
            ),
            # borderwidth=2
        ),
        autosize=True,
        height=632,
        # title='Baseline',
        width=1274,
        xaxis=dict(
            # autorange=True,
            fixedrange=False,
            title="Samples",
            #type='linear',
            # showgrid=False,
            ticks='inside',
            ticklen=8,
            tickwidth=2,
            tickcolor='#000',
            #tickvals=list(reversed(cores)),
            titlefont=dict(
                # family='Courier New, monospace',
                size=axis_title_font_size,
                color='black'
            ),
            tickfont=dict(
                # family='Old Standard TT, serif',
                size=axis_tick_label_size,
                color='black'
            ),
            showgrid=True,
            showline=True,
            mirror=True,
            zeroline=False,
            # gridcolor='black',
            # linecolor='black',
            gridwidth=2
        ),
        yaxis=dict(
            #type='linear',
            autorange=True,
            fixedrange=False,
            ticks='inside',
            ticklen=8,
            tickwidth=2,
            tickcolor='#000',
            titlefont=dict(
                # family='Courier New, monospace',
                size=axis_title_font_size,
                color='black'
                # color='#7f7f7f'
            ),
            rangemode='normal',
            tickmode='linear',
            # tickwidth=4,
            tickfont=dict(
                # family='Old Standard TT, serif',
                size=axis_tick_label_size,
                color='black'
            ),
            showgrid=True,
            showline=True,
            mirror=True,
            zeroline=False,
            # gridcolor='black',
            # linecolor='black',
            gridwidth=2,
            title=kmerstring
        )
    )

    fig = go.Figure(data=data, layout=layout)
    plot(fig, filename=inp_folder + "_barchart_"+xlab+".html", auto_open=False)


"""QMK CLI Subcommands

We list each subcommand here explicitly because all the reliable ways of searching for modules are slow and delay startup.
"""
import os
import shlex
import sys
from importlib.util import find_spec
from pathlib import Path
from subprocess import run

from milc import cli, __VERSION__
from milc.questions import yesno

import_names = {
    # A mapping of package name to importable name
    'pep8-naming': 'pep8ext_naming',
    'pyserial': 'serial',
    'pyusb': 'usb.core',
    'qmk-dotty-dict': 'dotty_dict',
    'pillow': 'PIL'
}

safe_commands = [
    # A list of subcommands we always run, even when the module imports fail
    'clone',
    'config',
    'doctor',
    'env',
    'setup',
]

subcommands = [
    'qmk.cli.ci.validate_aliases',
    'qmk.cli.bux',
    'qmk.cli.c2json',
    'qmk.cli.cd',
    'qmk.cli.chibios.confmigrate',
    'qmk.cli.clean',
    'qmk.cli.compile',
    'qmk.cli.docs',
    'qmk.cli.doctor',
    'qmk.cli.find',
    'qmk.cli.flash',
    'qmk.cli.format.c',
    'qmk.cli.format.json',
    'qmk.cli.format.python',
    'qmk.cli.format.text',
    'qmk.cli.generate.api',
    'qmk.cli.generate.autocorrect_data',
    'qmk.cli.generate.compilation_database',
    'qmk.cli.generate.config_h',
    'qmk.cli.generate.develop_pr_list',
    'qmk.cli.generate.dfu_header',
    'qmk.cli.generate.docs',
    'qmk.cli.generate.info_json',
    'qmk.cli.generate.keyboard_c',
    'qmk.cli.generate.keyboard_h',
    'qmk.cli.generate.keycodes',
    'qmk.cli.generate.keycodes_tests',
    'qmk.cli.generate.make_dependencies',
    'qmk.cli.generate.rgb_breathe_table',
    'qmk.cli.generate.rules_mk',
    'qmk.cli.generate.version_h',
    'qmk.cli.git.submodule',
    'qmk.cli.hello',
    'qmk.cli.import.kbfirmware',
    'qmk.cli.import.keyboard',
    'qmk.cli.import.keymap',
    'qmk.cli.info',
    'qmk.cli.json2c',
    'qmk.cli.lint',
    'qmk.cli.kle2json',
    'qmk.cli.list.keyboards',
    'qmk.cli.list.keymaps',
    'qmk.cli.list.layouts',
    'qmk.cli.mass_compile',
    'qmk.cli.migrate',
    'qmk.cli.new.keyboard',
    'qmk.cli.new.keymap',
    'qmk.cli.painter',
    'qmk.cli.pytest',
    'qmk.cli.via2json',
]

def _install_deps(requirements):
    """Perform the installation of missing requirements.

    If we detect that we are running in a virtualenv we can't write into we'll use sudo to perform the pip install.
    """
    command = [sys.executable, '-m', 'pip', 'install']

    if sys.prefix != sys.base_prefix:
        # We are in a virtualenv, check to see if we need to use sudo to write to it
        if not os.access(sys.prefix, os.W_OK):
            print('Notice: Using sudo to install modules to location owned by root:', sys.prefix)
            command.insert(0, 'sudo')

    elif not os.access(sys.prefix, os.W_OK):
        # We can't write to sys.prefix, attempt to install locally
        command.append('--user')

    return _run_cmd(*command, '-r', requirements)

def _run_cmd(*command):
    """Run a command in a subshell.
    """
    if 'windows' in cli.platform.lower():
        safecmd = map(shlex.quote, command)
        safecmd = ' '.join(safecmd)
        command = [os.environ['SHELL'], '-c', safecmd]

    return run(command)

def _find_broken_requirements(requirements):
    """ Check if the modules in the given requirements.txt are available.

    Args:

        requirements
            The path to a requirements.txt file

    Returns a list of modules that couldn't be imported
    """
    with Path(requirements).open() as fd:
        broken_modules = []

        for line in fd.readlines():
            line = line.strip().replace('<', '=').replace('>', '=')

            if len(line) == 0 or line[0] == '#' or line.startswith('-r'):
                continue

            if '#' in line:
                line = line.split('#')[0]

            module_name = line.split('=')[0] if '=' in line else line
            module_import = module_name.replace('-', '_')

            # Not every module is importable by its own name.
            if module_name in import_names:
                module_import = import_names[module_name]

            if not find_spec(module_import):
                broken_modules.append(module_name)

        return broken_modules

def _broken_module_imports(requirements):
    """Make sure we can import all the python modules.
    """
    broken_modules = _find_broken_requirements(requirements)

    for module in broken_modules:
        print('Could not find module %s!' % module)

    if broken_modules:
        return True

    return False

def _yesno(*args):
    """Wrapper to only prompt if interactive
    """
    return sys.stdout.isatty() and yesno(*args)

def _eprint(errmsg):
    """Wrapper to print to stderr
    """
    print(errmsg, file=sys.stderr)

# Make sure our python is new enough
#
# Supported version information
#
# Based on the OSes we support these are the minimum python version available by default.
# Last update: 2021 Jan 02
#
# Arch: 3.9
# Debian: 3.7
# Fedora 31: 3.7
# Fedora 32: 3.8
# Fedora 33: 3.9
# FreeBSD: 3.7
# Gentoo: 3.7
# macOS: 3.9 (from homebrew)
# msys2: 3.8
# Slackware: 3.7
# solus: 3.7
# void: 3.9

if sys.version_info[0] != 3 or sys.version_info[1] < 7:
    _eprint('Error: Your Python is too old! Please upgrade to Python 3.7 or later.')
    exit(127)

milc_version = __VERSION__.split('.')

if int(milc_version[0]) < 2 and int(milc_version[1]) < 4:
    requirements = Path('requirements.txt').resolve()

    _eprint(f'Your MILC library is too old! Please upgrade: python3 -m pip install -U -r {str(requirements)}')
    exit(127)

# Make sure we can run binaries in the same directory as our Python interpreter
python_dir = os.path.dirname(sys.executable)

if python_dir not in os.environ['PATH'].split(':'):
    os.environ['PATH'] = ":".join((python_dir, os.environ['PATH']))

# Check to make sure we have all our dependencies
msg_install = f'\nPlease run `{sys.executable} -m pip install -r %s` to install required python dependencies.'
args = sys.argv[1:]
while args and args[0][0] == '-':
    del args[0]

safe_command = args and args[0] in safe_commands

if not safe_command:
    if _broken_module_imports('requirements.txt'):
        if _yesno('Would you like to install the required Python modules?'):
            _install_deps('requirements.txt')
        else:
            _eprint(msg_install % (str(Path('requirements.txt').resolve()),))
            exit(1)

    if cli.config.user.developer and _broken_module_imports('requirements-dev.txt'):
        if _yesno('Would you like to install the required developer Python modules?'):
            _install_deps('requirements-dev.txt')
        elif _yesno('Would you like to disable developer mode?'):
            _run_cmd(sys.argv[0], 'config', 'user.developer=None')
        else:
            _eprint(msg_install % (str(Path('requirements-dev.txt').resolve()),))
            _eprint('You can also turn off developer mode: qmk config user.developer=None')
            exit(1)

# Import our subcommands
for subcommand in subcommands:
    try:
        __import__(subcommand)

    except (ImportError, ModuleNotFoundError) as e:
        if safe_command:
            _eprint(f'Warning: Could not import {subcommand}: {e.__class__.__name__}, {e}')
        else:
            raise


import bot
import psutil
import config
import random
import aiohttp
import discord  # removed "from discord import embeds", doesn't do anything
import requests
import time
import asyncio
import concurrent.futures
import os
import importlib
import sys
import shutil
from bs4 import BeautifulSoup
from discord.ext import commands

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

"""
requests and urllib are blocking. Do not use these libraries within your asynchronous code.
(http://discordpy.readthedocs.io/en/latest/faq.html#what-does-blocking-mean)

discord.py uses aiohttp, so it should already be installed.
"""

async def getdata(url):  # switch from requests module to aiohttp (see above for reason)
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            r = await response.text()
    return r

class Fun(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    async def add(self, ctx, *integers): # creates a list of input (I haven't typecasted to (int) due to multitude of reasons)
        """Adds multiple numbers together."""
        if len(integers) <= 1:
            return await ctx.send("Provide at least two or more numbers!")

        new_list = [] # initializing new list
        for number in integers: # iterating over the original list of numbers
            try:
               new_number = float(number) # conver the output to integer
               new_list.append([new_number, str(number)])
               continue # append the converted string along with the string as a list
            except (TypeError, ValueError):
               continue # if a string is passed, pass it
        equation = " + ".join([num[1] for num in new_list]) #iterate over our new_list to get the string part of numbers and join them
        total = sum([num[0] for num in new_list]) # iterate over the new_list and add all the appended float numbers together
        em = discord.Embed(title = f"**__Input:__**\n```py\n{equation}\n```\n**__Output:__**\n```py\n{total}\n```", color = discord.Color.blue()) # send both the input and output
        await ctx.send(embed = em)

    @commands.command(description='For when you wanna settle the score some other way')
    async def choose(self, ctx, *choices: str):
        """Chooses between multiple choices.""" # removed unnecessary lines of code
        if "@everyone" in choices or "@here" in choices:
            em = discord.Embed(title = "Nice try, sadly that won't work here.", color = discord.Color.red())
            return await ctx.send(embed = em)
        em = discord.Embed(title = random.choice(choices), color = discord.Color.blue())
        await ctx.send(embed = em)

    @commands.command(description='#emotes')
    async def emote(self, ctx, emote : discord.Emoji = None):
        """emote command"""
        if emote is None:
            em = discord.Embed(title="No emote given", description = f"Please use `{config.prefix}emote <emote>`.", color = discord.Color.red())
            await ctx.send(embed=em)
        try:
            em = discord.Embed(timestamp=emote.created_at, color = discord.Color.blue())
            em.set_author(name=emote.name, icon_url=emote.url)
            em.set_thumbnail(url=emote.url)
            em.set_footer(text="Created on")
            em.add_field(name="ID", value=emote.id)
            em.add_field(name="Usage", value=f"`{emote}`")
            em.add_field(name="URL", value=f"[click here]({emote.url})") # masked links instead of actually sending the full link
            await ctx.send(embed=em)
        except IndexError:
            em = discord.Embed(title="Error", description="There was an error fetching the emote. The most likely cause is that it's from a server the bot isn't in.", color = discord.Color.red())

    @commands.command()
    async def f(self, ctx, *, message2):
        em = discord.Embed(title = f"F in the chat to: **{message2}**", color=discord.Color.blue())
        msg = await ctx.send(embed = em)
        await msg.add_reaction('ðŸ‡«')
        def check(reaction, user):
            return msg == reaction.message
        usersreacted = []
        donotaccept = self.bot.user.name
        while True:
            try:
                reaction, user = await self.bot.wait_for('reaction_add', check=check, timeout=30)
            except asyncio.TimeoutError:
                new_msg = await ctx.fetch_message(msg.id)
                number = len([x for x in await new_msg.reactions[0].users().flatten() if not x.bot])
                em3 = discord.Embed(title = f"F in the chat to: **{message2}**", color=discord.Color.blue())
                multipleusers = f"{', '.join(usersreacted)}"
                em3.add_field(name="Users who paid respects", value=f"{multipleusers}\n**A total of {number} people paid their respects.**")
                return await msg.edit(embed=em3)
                #return await ctx.send(f"A total of {number} people paid their respects to **{message2}**.")
            else:
                #try:
                #    for user in usersreacted:
                #        emoji = self.bot.emoji
                #        await msg.remove_reaction("ðŸ‡«", user.id)
                #except discord.Forbidden:
                #    pass
                if str(reaction.emoji) == "ðŸ‡«":
                    if user.name in usersreacted:
                        continue
                    if user.name == donotaccept:
                        continue
                    usersreacted.append(user.name)
                    #await ctx.send(f"**{user.name}** has paid their respects.")
                    em2 = discord.Embed(title = f"F in the chat to: **{message2}**", color=discord.Color.blue())
                    multipleusers = f"{', '.join(usersreacted)}"
                    em2.add_field(name="Users who paid respects", value=f"{multipleusers}")
                    await msg.edit(embed=em2)

    @commands.command()
    async def image(self, ctx, *, query):
        query = query.replace(" ", "+")
        if not os.path.exists('cache'):
            os.makedirs('cache')
        if os.path.isfile(f'cache/{query}.py'):
            sys.path.insert(0, './cache')
            #print("Using cache file:\n")
            importedquery = importlib.import_module(f"{str(query)}")
            images = importedquery.cache
        else:
            images = []
            htmldata = await getdata(f'https://searx.prvcy.eu/search?q={query}&categories=images')
            #print(f"https://www.bing.com/images/search?q={str(newquery)}")
            soup = BeautifulSoup(htmldata, 'html.parser')
            for item in soup.find_all('img'):
                if "explicit" not in str(item):
                    try:
                        replace1 = item['src'].replace("%3A", ":")
                        replace2 = replace1.replace("%2F", "/")
                        if "gstatic" in replace2:
                            replace3 = replace2.replace("%3F", "?")
                            replace4 = replace3.replace("%3D", "=")
                            replace5 = replace4.replace("%26", "?")
                            lefttext = replace5.split("?usqp")[0]
                            replace6 = lefttext.replace("?usqp", "")
                            righttext = replace6.split("/image_proxy?url=")[1]
                            #print(righttext)
                            images.append(righttext)
                        else:
                            replace3 = replace2.replace("%3F", "/")
                            replace4 = replace3.replace("%3D", "/")
                            replace5 = replace4.replace("%26", "?")
                            righttext = replace5.split("/image_proxy?url=")[1]
                            #print(righttext)
                            images.append(righttext)
                        f = open(f"cache/{query}.py", "w")
                        f.write(f"cache = {images}")
                        f.close()
                    except Exception as e:
                        #print(e)
                        pass
        printimage = random.choice(images)
        await ctx.send(str(printimage))

    @commands.command()
    async def listcache(self, ctx):
        try:
            em = discord.Embed(title="Image Cache Files", description=f"`{', '.join(os.listdir('./cache/'))}`", color=discord.Color.blue())
            await ctx.send(embed=em)
        except OSError:
            em = discord.Embed(title="Error", description="No cache folder could be found.", color=discord.Color.red())
            await ctx.send(embed=em)

    @commands.command()
    async def clearcache(self, ctx, clear = None):
        try:
            clear = clear.replace(" ", "+")
        except AttributeError:
            pass
        if clear is None:
            shutil.rmtree("./cache")
            em = discord.Embed(title="Image Cache Directory Cleared", description="The `./cache` directory has been cleared. Images will take a few seconds longer to fetch as they recache.", color=discord.Color.green())
            await ctx.send(embed=em)
        else:
            if os.path.isfile(f'cache/{clear}.py'):
                os.remove(f"./cache/{clear}.py")
                em = discord.Embed(title="Image Cache Directory Cleared", description=f"The `./cache/{clear}.py` file has been deleted. Images will take a few seconds longer to fetch as they recache.", color=discord.Color.green())
                await ctx.send(embed=em)
            else:
                em = discord.Embed(title="No cache file found.", description=f"There was no cache file found at `./cogs/{clear}.py`.", color=discord.Color.red())
                await ctx.send(embed=em)

    @commands.command(aliases=['rd'])
    @commands.cooldown(2,5,commands.BucketType.user)
    async def reddit(self, ctx, *, name):
        posts = []
        subreddit = f"{name}"
        async with aiohttp.ClientSession() as session:
            async with session.get(f"https://www.reddit.com/r/{subreddit}/.json") as r:
                response = await r.json()
                try:
                 for i in response['data']['children']:
                    posts.append(i['data'])
                except KeyError:
                    return await ctx.send("The subreddit you provided doesn't exist!")
                try:
                 post = random.choice([p for p in posts if not p['stickied'] or p['is_self']])
                except IndexError:
                    return await ctx.send("The subreddit you provided doesn't exist!")
                if post['over_18'] is True and ctx.channel.nsfw is False:
                    return await ctx.send("Failed to get a post from that subreddit, try again in an NSFW channel.")
                title = str(post['title'])
        embed=discord.Embed(title=f'{title}', colour=0xaf85ff, url=f"https://reddit.com/{post['permalink']}")
        embed.set_footer(text=f"{post['upvote_ratio'] * 100:,}% Upvotes | Posted to r/{post['subreddit']}")
        embed.set_image(url=post['url'])
        await ctx.send(embed=embed)

def setup(bot):
    bot.add_cog(Fun(bot))


import subprocess
import multiprocessing
import os
import argparse

# Parameters
nbRepeats = 20
nbPerJob = 5

Fig3Seed = 600000
Fig5Seed = 300000

# Paths
dirPath = os.path.dirname(os.path.realpath(__file__))

ExecPath = os.path.join(dirPath, 'src', 'AstroSim')

Fig3Dir = 'Fig3Data'
Fig5Dir = 'Fig5Data'

Fig3ParamPath = os.path.join(dirPath, 'parameters', 'Fig3Params')
Fig5ParamPath = os.path.join(dirPath, 'parameters', 'Fig5Params')

Fig3DirPath = os.path.join(dirPath, 'data', Fig3Dir)
Fig5DirPath = os.path.join(dirPath, 'data', Fig5Dir)

# Generate the job commands associated with a given figure
def GenerateJobCommands(paramPath, dirName, addParams = [], seed = 12345):
	# Load default simulation arguments
	params = [ExecPath]
	with open(paramPath) as f:
		for line in f:
			params += line.split()

	# Modify some arguments
	params += ['-Path', dirPath, '-SubDir', dirName, '-repeat', str(nbRepeats), '-SplitGrid', str(nbPerJob)]
	params += addParams

	# Prepare simulations
	print('Preparing simulations for ' + dirName + '...')
	subprocess.run(params, stdout = subprocess.DEVNULL, stderr = subprocess.DEVNULL)
	print('Done.')

	# Generate job commands
	launch = []
	with open(os.path.join(dirPath, 'data', dirName, 'LaunchGrids.job')) as f:
		for i, line in enumerate(f):
			launch.append([ExecPath, '-simLoad'] + line.split() +
				['-modelLoad', '-SubDir', dirName, '-Path', dirPath, '-Sim' , '-seed', str(seed + i)])
	return launch

if __name__ == '__main__':
	# Argument parsing
	parser = argparse.ArgumentParser()
	parser.add_argument('-n', '--nbCores', type = int, default=os.cpu_count(),
		help = 'Number of cores to be used')
	parser.add_argument('-f', '--figures', type = int, nargs = '+',
		choices = [3, 5, 6], help = 'Simulations to be run (figure number)')
	args = parser.parse_args()

	allJobs = []
	# Figure 3 and 6
	if (args.figures is None or 3 in args.figures or 6 in args.figures):
		allJobs += GenerateJobCommands(Fig3ParamPath, Fig3Dir, seed = Fig3Seed)

	# Figure 5
	if (args.figures is None or 5 in args.figures):
		allJobs += GenerateJobCommands(Fig5ParamPath, Fig5Dir, seed = Fig5Seed)

	# Simulations
	pool = multiprocessing.Pool(args.nbCores)
	print('Running simulations ({0} jobs on {1} cores)...'.format(len(allJobs), args.nbCores))
	for i, _ in enumerate(pool.imap_unordered(subprocess.run, allJobs)):
		print('Job {0} done. ({1:.2%})'.format(i,(i+1)/len(allJobs)))


from django.conf.urls import patterns, url

urlpatterns = patterns(
    'datensparsam.apps.pdfbuilder.views',
    # Examples:
    # url(r'^$', 'datensparsam.views.home', name='home'),
    # url(r'^datensparsam/', include('datensparsam.foo.urls')),
    url(r'^$', 'index', {}, name='index'),
)


import pathlib
from xml.etree.ElementTree import Element
from typing import Optional

from ..exceptions import ElementPathRuntimeError

try:
    import xmlschema
except ImportError:  # pragma: no cover
    from ..exceptions import xpath_error

    def validate_analyzed_string(root: Element) -> None:
        raise ElementPathRuntimeError('not schema-aware')

    def validate_json_to_xml(root: Element) -> None:
        raise xpath_error('FOJS0004')

else:
    from ..namespaces import XPATH_FUNCTIONS_NAMESPACE

    analyzed_string_schema: Optional[xmlschema.XMLSchemaBase] = None
    json_to_xml_schema: Optional[xmlschema.XMLSchemaBase] = None

    __all__ = ['validate_analyzed_string', 'validate_json_to_xml']

    def validate_analyzed_string(root: Element) -> None:
        global analyzed_string_schema

        if analyzed_string_schema is None:
            xsd_file = pathlib.Path(__file__).parent.joinpath('analyze-string.xsd')
            analyzed_string_schema = xmlschema.XMLSchema(xsd_file)

        analyzed_string_schema.validate(root)

    def validate_json_to_xml(root: Element) -> None:
        global json_to_xml_schema

        if json_to_xml_schema is None:
            xsd_file = pathlib.Path(__file__).parent.joinpath('schema-for-json.xsd')
            json_to_xml_schema = xmlschema.XMLSchema(xsd_file)

        json_to_xml_schema.validate(root, namespaces={'j': XPATH_FUNCTIONS_NAMESPACE})


#! /usr/bin/python2
# vim: set fileencoding=utf-8
"""Plot dataset in (reduced) dimension 2 or 3."""
import sys
try:
    # old version of matplotlib on some computers
    del sys.path[sys.path.index('/usr/lib/pymodules/python2.7')]
except ValueError:
    pass
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import prettyplotlib as ppl
import matplotlib as mpl
from sklearn import manifold, decomposition  # , datasets
import calc_tsne
import scipy.io as sio

def plot_embedding(figure, index, method, run_time, data, classes, dimension):
    """Scatter subplot `data` with colors corresponding to `classes` on
    `figure` at position `index` in `dimension`D. Title is made of `method`
    and `run_time`."""
    common = dict(c=classes, cmap=mpl.cm.Spectral, alpha=0.8, s=45)
    if dimension == 2:
        axe = figure.add_subplot(1, 1, 1 + index)
        ppl.scatter(data[:, 0], data[:, 1], **common)
    elif dimension == 3:
        axe = figure.add_subplot(5, 3, 1 + index, projection="3d")
        ppl.scatter(data[:, 0], data[:, 1], data[:, 2], **common)
    else:
        raise ValueError(dimension)
    plt.title("{} ({:.2g} sec)".format(method, run_time))
    axe.xaxis.set_major_formatter(mpl.ticker.NullFormatter())
    axe.yaxis.set_major_formatter(mpl.ticker.NullFormatter())
    if dimension == 3:
        axe.zaxis.set_major_formatter(mpl.ticker.NullFormatter())
    plt.axis('tight')

def choose_decomposition_method(method, n_components):
    """Return the decomposition corresponding to `method`."""
    if method == 'PCA':
        return decomposition.PCA(n_components)
    elif method == 'Randomized PCA':
        return decomposition.RandomizedPCA(n_components)
    elif method == 'Kernel PCA':
        return decomposition.KernelPCA(n_components, kernel='rbf')
    elif method == 'Sparse PCA':
        return decomposition.SparsePCA(n_components, n_jobs=1)
    elif method == 'SVD':
        return decomposition.TruncatedSVD(n_components)
    elif method == 'Factor Analysis':
        return decomposition.FactorAnalysis(n_components)
    elif method == 'ICA':
        return decomposition.FastICA(n_components)
    raise ValueError('{} is not a known method'.format(method))

def choose_manifold_method(method, n_components, n_neighbors):
    """Return the manifold corresponding to `method`."""
    method = method.lower()
    if method in ['standard', 'ltsa', 'hessian', 'modified']:
        # solver = 'auto' if method != 'standard' else 'dense'
        return manifold.LocallyLinearEmbedding(n_neighbors, n_components,
                                               eigen_solver='dense',
                                               method=method)
    elif method == 'isomap':
        return manifold.Isomap(n_neighbors, n_components)
    elif method == 'mds':
        return manifold.MDS(n_components, max_iter=200, n_init=1)
    elif method == 'spectral':
        return manifold.SpectralEmbedding(n_components=n_components,
                                          n_neighbors=n_neighbors)
    elif method == 't-sne':
        return calc_tsne.tSNE(n_components, perplexity=25, theta=0.3)
    raise ValueError('{} is not a known method'.format(method))

def compute_embedding(high, method, n_components=2, n_neighbors=None):
    """Reduce dimension of `high` to `n_components` using `method`
    (parametrized by `n_neighbors`)"""
    n_neighbors = n_neighbors or (n_components * (n_components + 3) / 2) + 4
    try:
        projector = choose_manifold_method(method, n_components, n_neighbors)
    except ValueError:
        projector = choose_decomposition_method(method, n_components)
    start = time()
    lower = projector.fit_transform(high)
    return lower, time() - start

def join_cities(cities):
    """Concatenate matrix for all `cities` but keep track from which city each
    point come from."""
    features = None
    for idx, city in enumerate(cities):
        mat = load_matrix(city)['v']
        coming_from = idx*np.ones((1, mat.shape[0])).ravel()
        if features is not None:
            features = np.vstack([features, mat])
            origin = np.hstack([origin, coming_from]).ravel()
        else:
            features = mat
            origin = coming_from
    return features, origin

def split_cities(cities, reduced, origin, features):
    """Save a 2d representation (given by `reduced`) from each city of
    `cities`."""
    for idx, city in enumerate(cities):
        autochthons = origin == idx
        original = sio.loadmat(city+'_fv')
        coords = reduced[autochthons, :]
        cats = features[autochthons, 5] * 8e5
        newv = np.hstack([coords, coords, cats.reshape((coords.shape[0], 1))])
        sio.savemat(city+'_tsne', {'i': original['i'],
                                   'c': original['c'],
                                   'stat': original['stat'],
                                   'v': newv})

if __name__ == '__main__':
    from timeit import default_timer as time
    from ClosestNeighbor import load_matrix
    import numpy as np
    # pylint: disable=C0103
    city = sys.argv[1].strip().lower()
    nb_dim = 2 if len(sys.argv) <= 2 else int(sys.argv[2])
    features = None
    origin = None
    cities = ['stockholm', 'prague', 'paris', 'barcelona', 'rome', 'berlin',
              'london', 'helsinki', 'amsterdam', 'moscow']
    cities = ['amsterdam', 'atlanta', 'barcelona', 'berlin', 'chicago',
              'helsinki', 'houston', 'indianapolis', 'london', 'losangeles',
              'moscow', 'newyork', 'paris', 'prague', 'rome', 'sanfrancisco',
              'seattle', 'stlouis', 'stockholm', 'washington']
    cities = ['paris', 'barcelona', 'rome', 'berlin', 'barcelona',
              'sanfrancisco', 'washington', 'newyork']

    # cities = ['helsinki']
    if len(cities) > 1:
        features, origin = join_cities(cities)
    else:
        features = load_matrix(city)['v']
        origin = features.shape[0] * [0, ]
    # sio.savemat('tmp', {'A': features}, do_compression=True)
    # sys.exit()
    features[:, 5] = features[:, 5] / 8e5
    # to_keep = set(range(6, 15))
    # to_keep = set(range(18, 24)+range(25, 31))
    # to_keep = set(range(0, 5))
    cats = (8*features[:, 5]).astype(int)
    features[:, 5] = 0
    # to_delete = set(range(features.shape[1])).difference(to_keep)
    # features = np.delete(features, list(to_delete), axis=1)
    # print(features.shape)
    # features[:, 5] *= 0.0
    # print(np.sum(features[:, 5]))
    Axes3D
    # n_points = 300
    # X, color = datasets.samples_generator.make_s_curve(n_points,
    # features, cats = datasets.samples_generator.make_swiss_roll(n_points,
    #                                                             noise=0.1,
    #                                                           random_state=0)
    fig = plt.figure(figsize=(25, 18))
    # title = "{} venues of {} projected to {} dimensions"
    # title = title.format(features.shape[0], city.title(), nb_dim)
    # print(title)
    # plt.suptitle(title, fontsize=14)
    # ax = fig.add_subplot(241, projection='3d')
    # plt.scatter(X[:, 0], X[:, 1], X[:, 2], c=color, cmap=mpl.cm.Spectral)

    methods = ['Standard', 'LTSA', 'Hessian', 'Modified', 'Isomap', 'MDS',
               'Spectral', 't-SNE', 'PCA', 'Randomized PCA', 'Kernel PCA',
               'Sparse PCA', 'SVD', 'Factor Analysis', 'ICA']
    methods = ['t-SNE']

    # for i, method in enumerate(methods):
    #     reduced, how_long = compute_embedding(features, method, nb_dim)
    #     sio.savemat('tsne_'+city, {'cl': cats, 'data': reduced})
    #     plot_embedding(fig, i, method, how_long, reduced, cats, nb_dim)
    #     print("{}: {:.3f} sec".format(method, how_long))
    # outfile = '{}_DR_{}.png'.format(city, nb_dim)
    # plt.savefig(outfile, frameon=False, bbox_inches='tight',
    #             pad_inches=0.05)
    reduced, how_long = compute_embedding(features, 't-SNE', 2)
    split_cities(cities, reduced, origin, features)
    # city_name = np.array(map(lambda x: cities[int(x)], origin))
    # to_export = np.hstack([reduced, cats.reshape((reduced.shape[0], 1)),
    #                        city_name.reshape((reduced.shape[0], 1))])
    # np.savetxt('allCities.tsv', to_export, comments='', delimiter='\t', fmt='%s',
    #            header='posx posy cat city'.replace(' ', '\t'))


n,m = map(int,input().split())

see = set()
for i in range(n) :
    see.add(input())

hear = set()
for i in range(m) :
    hear.add(input())

res = sorted(list(see&hear))

print(len(res))

for i in res :
    print(i)


# 给定一个字符串 (s) 和一个字符模式 (p) ，实现一个支持 '?' 和 '*' 的通配符匹配。

# '?' 可以匹配任何单个字符。
# '*' 可以匹配任意字符串（包括空字符串）。
# 两个字符串完全匹配才算匹配成功。

# 说明:

# s 可能为空，且只包含从 a-z 的小写字母。
# p 可能为空，且只包含从 a-z 的小写字母，以及字符 ? 和 *。

class Solution:
    def isMatch(self, s: str, p: str) -> bool:
        # 递归 递归式 + 递归出口 递归处理好当前 不管后面
        if not p:    #p为空 且 s 为空 则返回true
            return not s
        if not s:
            if not p:
                return True
            if set(p) == {'*'}:
                return True
            return False
        if p[0] == '?' or p[0] == s[0]:
            return self.isMatch(s[1:], p[1:])
        if p[0] == '*':
            return self.isMatch(s[1:], p) or self.isMatch(s, p[1:])
        return False

# 思路
# 双指针 回溯法
# 优先看是不是一对一匹配 直接跳 i j
# 之后看j 对应的是不是* 是直接跳 * 这里先考虑* 匹配零个字符的情况
# 之后继续 如果不匹配了 回溯 j 回到* 之后 i 回到之前的后一个位置 表示用*匹配了一个字符
# 再继续 不匹配 就用*匹配两个字符
class Solution2:
    def isMatch(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: bool
        """
        i = 0
        j = 0
        start = -1
        match = 0
        while i < len(s):
            # 一对一匹配,匹配成功一起移
            if j < len(p) and (s[i] == p[j] or p[j] == "?"):
                i += 1
                j += 1
            # 记录p的"*"的位置,还有s的位置
            elif j < len(p) and p[j] == "*":
                start = j
                match = i
                # 这里没有用*匹配任何字符
                j += 1
            # j 回到 记录的下一个位置
            # match 更新下一个位置
            # 这代表用*匹配一个字符
            elif start != -1:
                j = start + 1
                match += 1
                i = match
            else:
                return False
         # 将多余的 * 直接匹配空串
        return all(x == "*" for x in p[j:])

s = Solution2()
print(s.isMatch("acdcb","a*cb"))


#!/usr/bin/python

"""
Custom Ansible module - returns list of keys for a specific endpoint.
"""

import urllib3
from fortiosapi import FortiOSAPI
from ansible.module_utils.basic import AnsibleModule

DOCUMENTATION = """
---
module: fortios_fw_state
short_description: GETs current state of FW.
description:
    - Lorum Ipsum
version_added: "0.0.5"
author:
    - Gustav Larsson
notes:
    - Lorum Ipsum

requirements:
    - ansible>=2.9
options:
    host:
        description:
            - Host to GET state from.
        type: str
        required: true
    token:
        description:
            - API token for device.
        type: str
        required: true
    differ_api_path:
        description:
            - API path to GET state from.
        type: str
        required: true
    differ_api_name:
        description:
            - API endpoint to GET state from.
        type: str
        required: true
    ssl_verify:
        description:
            - Switch whether to check SSL-cert or not.
        type: str
        required: true
"""

module = AnsibleModule(argument_spec={
    "host":{"type":"str", "required":True},                     # hostname
    "token":{"type":"str", "required":True, "no_log":True},     # token
    "differ_api_path":{"type":"str", "required":True},          # path to endpoint eg. /xxx/...
    "differ_api_name":{"type":"str", "required":True},          # name of endpoint eg. /.../xxx
    "ssl_verify":{"type":"bool", "required":True}},             # True/False
    supports_check_mode=True)

if module.params["ssl_verify"] is False:
    urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

fgt = FortiOSAPI()
fgt.tokenlogin(module.params["host"], module.params["token"], verify=module.params["ssl_verify"])

def main(differ_api_path, differ_api_name):
    """
    returns list of entries, keyed by name
    """

    return_list = []
    result = fgt.get(differ_api_path, differ_api_name)['results']

    if differ_api_path == "router" and differ_api_name == "static":                 # /router/static/ is keyed by seq-num, not name
        search_key = "seq-num"
    elif differ_api_path == "firewall" and differ_api_name == "policy":             # /firewall/policy/ is keyed by policyid, not name
        search_key = "policyid"
    elif differ_api_path == "firewall" and differ_api_name == "central-snat-map":   # /firewall/central-snat-map/ is keyed by policyid, not name
        search_key = "policyid"
    elif differ_api_path == "system.snmp" and differ_api_name == "community":       # /firewall/central-snat-map/ is keyed by policyid, not name
        search_key = "id"
    else:
        search_key = "name"

    for entry in result:
        if differ_api_path == "system" and differ_api_name == "interface":
            if entry["type"] == "tunnel":         # /system/interface/ dynamic tunnels are skipped
                continue
        return_list.append(entry[search_key])

    module.exit_json(return_list=return_list)

if __name__ == "__main__":
    main(module.params["differ_api_path"], module.params["differ_api_name"])