import copy
import json
import math
import struct
from functools import partial
from pathlib import Path, PosixPath

import numpy as np

# isort: off
import torch
import tensorrt as trt
# isort: on

# numpy doesn't know bfloat16, define abstract binary type instead
np_bfloat16 = np.dtype('V2', metadata={"dtype": "bfloat16"})

def torch_to_numpy(x: torch.Tensor):
    assert isinstance(x, torch.Tensor), \
        f'x must be a torch.Tensor object, but got {type(x)}.'
    if x.dtype != torch.bfloat16:
        return x.detach().cpu().numpy()
    return x.view(torch.int16).detach().cpu().numpy().view(np_bfloat16)

def numpy_to_torch(x):
    if x.dtype != np_bfloat16:
        return torch.tensor(x)
    return torch.tensor(x.view(np.int16)).view(torch.bfloat16)

def numpy_to_dtype(x, dtype: str):
    if x.dtype == np_bfloat16:
        # BF16 --> non-BF16 or BF16
        if dtype != 'bfloat16':
            torch_to_numpy(numpy_to_torch(x).to(str_dtype_to_torch(dtype)))
        else:
            return x
    else:
        # non-BF16 types --> non-BF16 or BF16
        if dtype != 'bfloat16':
            return x.astype(str_dtype_to_np(dtype))
        else:
            return torch_to_numpy(torch.from_numpy(x).to(torch.bfloat16))

fp32_array = partial(np.array, dtype=np.float32)
fp16_array = partial(np.array, dtype=np.float16)
int32_array = partial(np.array, dtype=np.int32)

def bf16_array(x):
    x = torch.tensor(x, dtype=torch.bfloat16)
    x = torch_to_numpy(x)
    return x

def trt_version():
    return trt.__version__

def torch_version():
    return torch.__version__

_str_to_np_dict = dict(
    float16=np.float16,
    float32=np.float32,
    int32=np.int32,
    bfloat16=np_bfloat16,
)

def str_dtype_to_np(dtype):
    ret = _str_to_np_dict.get(dtype)
    assert ret is not None, f'Unsupported dtype: {dtype}'
    return ret

_str_to_torch_dtype_dict = dict(
    bfloat16=torch.bfloat16,
    float16=torch.float16,
    float32=torch.float32,
    int32=torch.int32,
    int8=torch.int8,
)

def str_dtype_to_torch(dtype):
    ret = _str_to_torch_dtype_dict.get(dtype)
    assert ret is not None, f'Unsupported dtype: {dtype}'
    return ret

_str_to_trt_dtype_dict = dict(float16=trt.float16,
                              float32=trt.float32,
                              int64=trt.int64,
                              int32=trt.int32,
                              int8=trt.int8,
                              bool=trt.bool,
                              bfloat16=trt.bfloat16,
                              fp8=trt.fp8)

def str_dtype_to_trt(dtype):
    ret = _str_to_trt_dtype_dict.get(dtype)
    assert ret is not None, f'Unsupported dtype: {dtype}'
    return ret

_np_to_trt_dtype_dict = {
    np.int8: trt.int8,
    np.int32: trt.int32,
    np.float16: trt.float16,
    np.float32: trt.float32,

    # hash of np.dtype('int32') != np.int32
    np.dtype('int8'): trt.int8,
    np.dtype('int32'): trt.int32,
    np.dtype('float16'): trt.float16,
    np.dtype('float32'): trt.float32,
    np_bfloat16: trt.bfloat16,
    np.bool_: trt.bool,
}

def np_dtype_to_trt(dtype):
    ret = _np_to_trt_dtype_dict.get(dtype)
    assert ret is not None, f'Unsupported dtype: {dtype}'
    return ret

_trt_to_np_dtype_dict = {
    trt.int8: np.int8,
    trt.int32: np.int32,
    trt.float16: np.float16,
    trt.float32: np.float32,
    trt.bool: np.bool_,
    trt.bfloat16: np_bfloat16,
}

def trt_dtype_to_np(dtype):
    ret = _trt_to_np_dtype_dict.get(dtype)
    assert ret is not None, f'Unsupported dtype: {dtype}'
    return ret

_torch_to_np_dtype_dict = {
    torch.float16: np.float16,
    torch.float32: np.float32,
}

def torch_dtype_to_np(dtype):
    ret = _torch_to_np_dtype_dict.get(dtype)
    assert ret is not None, f'Unsupported dtype: {dtype}'
    return ret

_trt_to_torch_dtype_dict = {
    trt.float16: torch.float16,
    trt.float32: torch.float32,
    trt.int32: torch.int32,
    trt.int8: torch.int8,
    trt.bfloat16: torch.bfloat16
}

def trt_dtype_to_torch(dtype):
    ret = _trt_to_torch_dtype_dict.get(dtype)
    assert ret is not None, f'Unsupported dtype: {dtype}'
    return ret

def dim_to_trt_axes(dim):
    """Converts torch dim, or tuple of dims to a tensorrt axes bitmask"""
    if not isinstance(dim, tuple):
        dim = (dim, )

    # create axes bitmask for reduce layer
    axes = 0
    for d in dim:
        axes |= 1 << d

    return axes

def dim_resolve_negative(dim, ndim):
    if not isinstance(dim, tuple):
        dim = (dim, )
    pos = []
    for d in dim:
        if d < 0:
            d = ndim + d
        pos.append(d)
    return tuple(pos)

def mpi_comm():
    from mpi4py import MPI
    return MPI.COMM_WORLD

def mpi_rank():
    return mpi_comm().Get_rank()

def mpi_world_size():
    return mpi_comm().Get_size()

def pad_vocab_size(vocab_size, tp_size):
    return int(math.ceil(vocab_size / tp_size) * tp_size)

def to_dict(obj):
    return copy.deepcopy(obj.__dict__)

def to_json_string(obj):
    if not isinstance(obj, dict):
        obj = to_dict(obj)
    return json.dumps(obj, indent=2, sort_keys=True) + "\n"

def to_json_file(obj, json_file_path):
    with open(json_file_path, "w", encoding="utf-8") as writer:
        writer.write(to_json_string(obj))

def numpy_fp32_to_bf16(src):
    # Numpy doesn't support bfloat16 type
    # Convert float32 to bfloat16 manually and assign with bf16 abstract type
    original_shape = src.shape
    src = src.flatten()
    src = np.ascontiguousarray(src)

    assert src.dtype == np.float32
    dst = np.empty_like(src, dtype=np.uint16)
    for i in range(len(dst)):
        bytes = struct.pack('<f', src[i])
        dst[i] = struct.unpack('<H', struct.pack('BB', bytes[2], bytes[3]))[0]
    return dst.reshape(original_shape).view(np_bfloat16)

def fromfile(dir_path, name, shape=None, dtype=None):
    dtype = np_dtype if dtype is None else dtype
    p = dir_path
    if not isinstance(p, PosixPath):
        p = Path(p)
    p = p / name

    if Path(p).exists():
        t = np.fromfile(p, dtype=dtype)
        if shape is not None:
            t = t.reshape(shape)
        return t
    return None


#

import os
import csv

csv_path = os.path.join('..', 'Resources', 'budget_data.csv')
# csv_path = 'D:\\UCIBootcamp\\Module3_challenge\\python_challenge\\PyBank\\Resources\\budget_data.csv'
#

total_month = 0
net_amount = 0
date = []
changes = []
profit_change_list = []
profit_change_list_2 =[]

# decrease =  []
#
with open(csv_path) as csvfile:
    csvreader = csv.reader(csvfile, delimiter=",")
    csv_header = next(csvreader)

    # find the total amount
    for row in csvreader:
        #calculate total row
        total_month += 1
        #calculate total change in budget
        net_amount = net_amount + int(row[1])
        date.append(row[0])

        changes.append(int(row[1]))
    for i in range(1, len(changes)):

        profit_change_list.append(changes[i]-changes[i-1])

        # calculate average change
        avg_profit = round(sum(profit_change_list)/len(profit_change_list),2)

        # point out max min in profit
        max_profit = max(profit_change_list)
        min_profit = min(profit_change_list)
        # return date have max/min profit
        great_profit_increase = profit_change_list.index(max(profit_change_list)) + 1
        great_profit_decrease = profit_change_list.index(min(profit_change_list)) + 1
print("Financial Analysis")
print("--------------------")
print("Total months: ", total_month)
print("Total: $",net_amount)
print(f'Average Change: ${avg_profit}')
print(f'Greatest Increase in Profit:  {date[great_profit_increase]} ${max_profit}')
print(f'Greatest Decrease in Profit:  {date[great_profit_decrease]} ${min_profit}')

output = os.path.join('..', 'analysis', 'analysis_summary.txt')
with open(output, 'w') as txtfile:
    txtfile.write("Financial Analysis")
    txtfile.write("\n")
    txtfile.write("-------------------")
    txtfile.write("\n")
    txtfile.write(f'Total months: {total_month}')
    txtfile.write("\n")
    txtfile.write(f'Total: ${net_amount}')
    txtfile.write("\n")
    txtfile.write(f'Average Change: ${avg_profit}')
    txtfile.write("\n")
    txtfile.write(f'Greatest Increase in Profit:  {great_profit_increase} ${max_profit}')
    txtfile.write("\n")
    txtfile.write(f'Greatest Decrease in Profit:  {great_profit_decrease} ${min_profit}')


import numpy

matrix = numpy.loadtxt("similarity.txt")
key_file = open("pair.txt", "r")
keys = {}
line = key_file.readline()
while (line!=""):
	words = line.split(':', 1)
	keys[int(words[0])] = words[1].replace("\n", "")
	line = key_file.readline()

def my_cmp(x,y):
	if x[1] < y[1]:
		return -1
	elif x[1] > y[1]:
		return 1
	else:
		return 0

output = open("output","w")

for i in range(0,341):

	list = []
	for j in range(0,341):
		pair = []
		pair.append(j)
		pair.append(matrix[i][j])
		list.append(pair)
	list.sort(my_cmp, reverse=True)

	for k in range(1,7):
		output.write(keys[i] + " " + keys[list[k][0]] + " " + "\n")


from binance.client import Client
import pandas as pd
import matplotlib.pyplot as plt
import ta

data = Client().get_historical_klines("BTCUSDT", Client.KLINE_INTERVAL_1DAY, "01 JANUARY 2018")
df = pd.DataFrame(data, columns = ['timestamp', 'open', 'high', 'low', 'close', 'volume', 'close_time', 'quote_av', 'trades', 'tb_base_av', 'tb_quote_av', 'ignore'])
somme_investi = 0
benef = 0

del df['ignore']
del df['close_time']
del df['quote_av']
del df['trades']
del df['tb_base_av']
del df['tb_quote_av']

df['close'] = pd.to_numeric(df['close'])
df['high'] = pd.to_numeric(df['high'])
df['low'] = pd.to_numeric(df['low'])
df['open'] = pd.to_numeric(df['open'])
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
df['SMA50'] = ta.trend.sma_indicator(df['close'], 50)
df['SMA200'] = ta.trend.sma_indicator(df['close'], 200)

for i in range(len(df['SMA200']) - 1):
    if df['SMA200'][i] > df['SMA50'][i] and df['SMA200'][i+1] < df['SMA50'][i+1]:
        plt.annotate('BUY',
        ha = 'center', va = 'bottom',
        xytext = (df['timestamp'][i+1], df['SMA200'][i+1] + 5000),xy = (df['timestamp'][i+1], df['SMA200'][i+1]),arrowprops = {'facecolor' : 'green'})
        benef -= df['open'][i+1]
        somme_investi += df['open'][i+1]
        print("ACHAT: " + str(df['open'][i+1]) + " USDT")
    elif df['SMA200'][i] < df['SMA50'][i] and df['SMA200'][i+1] > df['SMA50'][i+1]:
        plt.annotate('SELL',
        ha = 'center', va = 'bottom',
        xytext = (df['timestamp'][i+1], df['SMA200'][i+1] + 5000),xy = (df['timestamp'][i+1], df['SMA200'][i+1]),arrowprops = {'facecolor' : 'red'})
        benef += df['open'][i+1]
        print("VENTE: " + str(df['open'][i+1]) + " USDT")

print("SOMME INVESTIE: " + str(somme_investi - benef))
print("BENEFICE TOTAL: " + str(benef))
plt.plot(df['timestamp'], df['open'])
plt.plot(df['timestamp'], df['SMA50'], color='r')
plt.plot(df['timestamp'], df['SMA200'], color='g')
plt.show()


from django.shortcuts import render,redirect
from django.contrib import messages
from .models import Courses

def new(request):
    context = {
        'course': Courses.objects.all()
    }
    return render(request, 'new.html', context)

def create(request):
    errors = Courses.objects.basic_validator(request.POST)
    if len(errors) > 0:
        for key, value in errors.items():
            messages.error(request, value)
        return redirect('/')
    else:
        Courses.objects.create(
            name=request.POST['name'],
            description=request.POST['description'],
        )
    return redirect('/')

def destroy(request, course_id):
    one_course = Courses.objects.get(id=course_id)
    context = {
        'course': one_course
    }
    return render(request, 'destroy.html', context)

def delete(request, course_id):
    to_delete =Courses.objects.get(id=course_id)
    to_delete.delete()
    return redirect('/')


class Solution:
    def sortColors(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        n=len(nums)
        low=0
        mid=0
        high=n-1
        while mid<=high:
            if nums[mid]==0:
                nums[mid],nums[low]=nums[low],nums[mid]
                low+=1;mid+=1
            elif nums[mid]==1:
                mid+=1
            else:
                nums[mid],nums[high]=nums[high],nums[mid]
                high-=1


def getPriority(a):
    if ord(a) >= 97:
        return ord(a)-96
    else:
        return ord(a)-38

f = open("input.txt", "r")
lines = []
ans1 = 0
ans2 = 0
for line in f.readlines():
    lines.append(line[:len(line)-1])
    first = line[:len(line)//2]
    second = line[len(line)//2:]
    for i in first:
        if i in second:
            ans1 += getPriority(i)
            break
print(ans1)

g = 0
while g < len(lines):
    group = lines[g:g+3]
    for k in group[0]:
        if (k in group[1]) and (k in group[2]):
            ans2 += getPriority(k)
            break
    g += 3
print(ans2)


from lcp.core.interfaces.module import Module
from lcp.modules.camerafeed.camera_feed import CameraFeed
import cv2 as cv
import _thread

class FaceDetector(Module):
    __name = "Face Detector"
    __version = "1.0"
    __dependencies = [CameraFeed]

    def __init__(self, config):
        super().__init__(self.__name, self.__version, self.__dependencies)
        self.__face_classifier_file = config.get('face_classifier', fallback='classifier.xml')
        self.__face_classifier = []
        self.__absolute_face_size = 0
        self.__tracked_faces = []
        self.__frame_width = 0
        self.__frame_height = 0
        self.__camera_feed = []
        self.__detector_thread = []

    def install(self, modules):
        modules = super().install(modules)
        self.__camera_feed = modules['CameraFeed']
        self.__face_classifier = cv.CascadeClassifier('..\\modules\\facedetector\\classifiers\\' + self.__face_classifier_file)

    def start(self):
        self.__detector_thread = _thread.start_new_thread(self.__detect_faces, ())

    def get_detected_faces(self):
        return self.__tracked_faces

    def get_frame_dimensions(self):
        return self.__frame_width, self.__frame_height

    def __detect_faces(self):
        while True:
            frame = self.__camera_feed.get_frame()
            gray_frame = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
            gray_frame = cv.equalizeHist(gray_frame)
            self.__frame_height, self.__frame_width, _ = frame.shape

            if self.__absolute_face_size == 0:
                height, width = gray_frame.shape[:2]

                if float(height) * 0.2 > 0:
                    self.__absolute_face_size = int(height * 0.2)

            self.__tracked_faces = self.__face_classifier.detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=2, minSize=(self.__absolute_face_size, self.__absolute_face_size))


# Find out if a given number is an "Armstrong Number".

# An n-digit number that is the sum of the nth powers of its digits
# is called an n-Armstrong number. Examples :
# 371 = 33 + 73 + 13;
# 9474 = 94 + 44 + 74 + 44;
# 93084 = 95 + 35 + 05 + 85 + 45.

# Write a Python program that;
# takes a positive integer number from the user,
# checks the entered number if it is Armstrong,
# consider the negative, float and any entries
# other than numeric values then display a warning message to the user.

# Examples
# Sample inputs	Outputs
# 407       407 is an Armstrong number
# 5         5 is an Armstrong number
# -153	    It is an invalid entry. Don't use non-numeric, float, or negative values!
# 153.87    or 153,87	 It is an invalid entry. Don't use non-numeric, float, or negative values!
# one	    It is an invalid entry. Don't use non-numeric, float, or negative values!
# 121	    121 is not an Armstrong number
while True:
    number = input("Enter a number: ")
    if number.isdigit() and int(number) > 0: # or not(type(number) is int)):
        number_list = list(number)
        exp = len(number)
        sum_number = 0
        for i in number_list:
            sum_number += int(i) ** exp
        if sum_number == int(number):
            print(f"{number} is an Armstrong number.")
        else:
            print(f"{number} is not an Armstrong number.")
        new_check = input("If would you like to check an other number enter 'yes': ")
        new_check = new_check.lower()
        if new_check == "yes":
            continue
        else:
            print("See you next time, thank you")
            break
    else:
        print("It is an invalid entry. Don't use non-numeric, float, or negative values!")


from operator import itemgetter, add
from pathlib import Path

banks = list(map(int, Path("day6.txt").read_text().split()))
n, history = len(banks), {}

while tuple(banks) not in history:
  history[tuple(banks)] = len(history)
  i, mx = max(enumerate(banks), key = itemgetter(1))
  banks[i] = 0
  for i in range(i + 1, i + 1 + mx):
    banks[i % len(banks)] += 1 # mx ended up being small, so this is fine
  if False:
    div, rem = divmod(mx, n)
    banks[:] = map(add, map(add, banks, [div] * n), [0 if (i - (n - rem)) < j <= i or (i - (n - rem) + 1 < 0 and n - ((n - rem) - i) < j < n) else 1 for j in range(n)])

print(len(history), len(history) - history[tuple(banks)])


import xml.etree.ElementTree as ET

bd=ET.Element("base")
ventana=ET.SubElement(bd,"ventana", name="ventana-consultas")
ventana_hide=ET.SubElement(ventana,"ventana-hide",)
ventana_hide.set("option-hide","false")
ET.dump(bd)
tree = ET.ElementTree(bd)
tree.write("C:/Users/ricar/Desktop/pruebas v1/pruebasv1.xml")

estructura_xml = ET.parse("C:/Users/ricar/Desktop/pruebas v1/pruebasv1.xml")
# Obtiene el elemento raíz:
raiz = estructura_xml.getroot()

'''for ventana in raiz.findall('ventana'):
    print(ventana)
    print("espacio1")
    print(ventana.get("option-hide"))

print("nada")
'''
for ventana in raiz.iter('ventana'):
    print("get: "+str(ventana.get("option-hide")))
    ventana.set("option-hide","0")
    print(ventana.get("option-hide"))

estructura_xml.write("C:/Users/ricar/Desktop/pruebas v1/pruebasv1.xml")


# Cryptomath Module
# http://inventwithpython.com/hacking (BSD Licensed)

def gcd(a, b):
    # Return the GCD of a and b using Euclid's Algorithm
    while a != 0:
        a, b = b % a, a
    return b

def succesiveSquaring(base, exp, mod):
    if exp == 0:
        x = 1
    else:
        half = succesiveSquaring(base, exp // 2, mod)  # just / in Python 2
        x = half * half
        if exp % 2 == 1:
            x *= base
    return x % mod

def gcdr(a, b):
    # Return the GCD of a and b using Euclid's Algorithm
    k=0
    while a != 0:
        q = b // a
        r = b % a
        d = b
        a, b = b % a, a
        if k > 0:
            print('{} = {} x {} + {}'.format(d,q,b,r))
        k+=1
    return b

def findModInverse(a, m):
    # Returns the modular inverse of a % m, which is
    # the number x such that a*x % m = 1

    if gcdr(a, m) != 1:
        assert "Not relatively prime!"
        return None # no mod inverse if a & m aren't relatively prime

    # Calculate using the Extended Euclidean Algorithm:
    u1, u2, u3 = 1, 0, a
    v1, v2, v3 = 0, 1, m
    while v3 != 0:
        q = u3 // v3 # // is the integer division operator
        v1, v2, v3, u1, u2, u3 = (u1 - q * v1), (u2 - q * v2), (u3 - q * v3), v1, v2, v3
    return u1 % m

if __name__ == '__main__':
    print(findModInverse(1051,17))
    print(succesiveSquaring(2,1234,789))


"""
__title__ = ''
__author__ = 'Thompson'
__mtime__ = '2018/5/23'
# code is far away from bugs with the god animal protecting
    I love animals. They taste delicious.
              ┏┓      ┏┓
            ┏┛┻━━━┛┻┓
            ┃      ☃      ┃
            ┃  ┳┛  ┗┳  ┃
            ┃      ┻      ┃
            ┗━┓      ┏━┛
                ┃      ┗━━━┓
                ┃  神兽保佑    ┣┓
                ┃　永无BUG！   ┏┛
                ┗┓┓┏━┳┓┏┛
                  ┃┫┫  ┃┫┫
                  ┗┻┛  ┗┻┛
"""

import requests

# 根据协议类型，选择不同的代理
proxies = {
  "http": "http://118.190.95.35:9001",
}
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}

response = requests.get("http://www.baidu.com", proxies = proxies,headers=headers)
print(response.content.decode())


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

import numpy as np
import torch
from torch import nn

class DNN(nn.Module):
    def __init__(self,args):
        super().__init__()
        self.outDim = args.outDim
        self.seqLen = args.seqLen
        self.hiddenDim1 = args.hiddenDim1
        self.hiddenDim2 = args.hiddenDim2
        self.hiddenDim3 = args.hiddenDim3
        self.fc1 = nn.Linear(self.seqLen,self.hiddenDim1)
        self.bn1 = nn.BatchNorm1d(self.hiddenDim1)
        # self.relu = nn.RReLU()
        self.relu = nn.RReLU()
        self.fc2 = nn.Linear(self.hiddenDim1,self.hiddenDim2)
        self.bn2 = nn.BatchNorm1d(self.hiddenDim2)

        self.fc3 = nn.Linear(self.hiddenDim2,self.hiddenDim3)
        self.bn3 = nn.BatchNorm1d(self.hiddenDim3)

        self.out = nn.Linear(self.hiddenDim3,self.outDim)
        self.dnn = nn.Sequential(
            self.fc1,
            self.bn1,
            self.relu,
            self.fc2,
            self.bn2,
            self.relu,
            self.fc3,
            self.bn3,
            self.relu,
            self.out,
        )

    def forward(self,seq):
        #assure seq is 1 dim
        seq.view(-1)
        out = self.dnn(seq)
        return out


#When newlist = oldlist, both will get modified when one is edited !!!!!!!!!!!!!!!!!

nums = [1,2,3,4,5,6,7]
k = 3
length = len(nums)
temp = nums.copy()

def indexRet(index, length, k):
	newIndexVal = (i+k)%length
	return newIndexVal

for i in range(length):
	newIndex = indexRet(i, length, k)
	nums[newIndex] = temp[i]

print(nums)

Data Structures and Algorithms - Udemy/Arrays Exercises/5. shiftArrays.py


import sqlite3
import pandas as pd
import time
import sys

from drugbank.drugbank_index_query import drugbank_search
from hpo.hpo_index_query import hpo_search
from omim.omim_index_query import omim_search
from stitch.stitch_chemical_sources_index_query import stitch_chemical_sources_search
from stitch.stitch_br08303_index_query import stitch_br08303_search

from python_requests import *
from usefull_temp import *
from link import *

'''
GLOBAL LISTS :
disease_list = {disease_name : [occurrence, source]}
curing_drug_list = {drugname : [occurrence, description, indication, toxicity, sources]}
side_effects_from_drug_list = {drugname : [occurrence, description, indication, toxicity, sources]}
'''

## SEARCH FROM HPO
def correction_hpo_disease_label(label):
    if (len(label) > 0 and label[0]=='#'):
        label = label.split(" ", 1)[1]

    if (len(label) > 0 and ',' in label):
        label = label.split(",", 1)[0]

    if (len(label) > 0 and ';' in label):
        label = label.split(";", 1)[0]

    return label

def get_diseases_from_hpo(hpo_id):

    disease_list = []

    DATABASE = "../data/HPO/hpo_annotations.sqlite"
    conn = sqlite3.connect(DATABASE)
    curs = conn.cursor()

    hpo_id = hpo_id.replace('_', ':')

    req = f"SELECT disease_label FROM phenotype_annotation WHERE sign_id = \"{hpo_id}\""
    curs.execute(req)

    for disease_tuple in curs.fetchall():
        disease = disease_tuple[0]
        disease = disease.lower()
        disease = correction_hpo_disease_label(disease)
        disease_list.append(disease)

    conn.commit()
    curs.close()

    return disease_list

## SEARCH FROM SIDER

SIDER_FILE = "../data/MEDDRAS/meddra_all_se.csv"

def get_sider_id(symptom):
    content = []
    df = pd.read_csv(SIDER_FILE, sep=',')
    n = len(df)

    for k in range(n):
        if symptom in df['side_effect_name'][k].lower():
            id1 = df['stitch_compound_id1'][k]
            id2 = df['stitch_compound_id2'][k]
            content.append((id1, id2))

    return content

## SEARCH FROM DRUGBANK

## GLOBAL SEARCH FUNCTION

def search_disease_from_symptom(symptom, disease_list):

    ## get symptoms
    hpo_query = create_hpo_query(symptom)
    content_hpo = hpo_search(hpo_query)

    ## complete symptoms

    ## Count lost items
    Total_hpo_count = len(content_hpo)
    count = 0

    for elem in content_hpo:
        hpo_id = elem[0]

        disease_list_from_hpo = get_diseases_from_hpo(hpo_id)

        if disease_list_from_hpo == []:
            count += 1
        else:
            for disease in disease_list_from_hpo:

                if disease in disease_list:
                    disease_list[disease][0] += 1
                else:
                    disease_list[disease] = [1, "hpo"]

    disease_list = dict(sorted(disease_list.items(), key=lambda item: item[1], reverse=True))

    return disease_list

def search_side_effects_drug_from_content_sider_id(content_sider_id, side_effects_from_drug_list):

    ## link with stitch
    content_stitch_atc = []
    for elem in content_sider_id:
        id1 = elem[0]
        id2 = elem[1]
        content_stitch_atc += sider_to_stitch_compoundid1(id1, id2)

    if len(content_stitch_atc) > 500:
        content_stitch_atc = content_stitch_atc[:500]

    ## link with drugbank
    content_drugbank = []
    for atc_code in content_stitch_atc:
        content_drugbank += stitch_atc_code_to_drugbank(atc_code)

    for item in content_drugbank:
        name = item[0]

        if name in side_effects_from_drug_list:
            side_effects_from_drug_list[name][0] += 1
        else:
            description = item[1]
            indication = item[2]
            toxicity = item[3]
            bloc = [1, description, indication, toxicity, 'sider / stitch / drugbank']
            side_effects_from_drug_list[name] = bloc

    side_effects_from_drug_list = dict(sorted(side_effects_from_drug_list.items(), key=lambda item: item[1], reverse=True))

    return side_effects_from_drug_list

def search_side_effects_drug_from_drugbank(symptom, side_effects_from_drug_list):
    query = create_drugbank_query_side_effect(symptom)
    content_drugbank = drugbank_search(query)

    for item in content_drugbank:
        name = item[1]

        if name in side_effects_from_drug_list:
            side_effects_from_drug_list[name][0] +=1

        else:
            description = item[2]
            indication = item[3]
            toxicity = item[4]
            sources = 'sider / stitch / drugbank'
            bloc = [1, description, indication, toxicity, sources]
            side_effects_from_drug_list[name] = bloc

    side_effects_from_drug_list = dict(sorted(side_effects_from_drug_list.items(), key=lambda item: item[1], reverse=True))

    return side_effects_from_drug_list

def search_curing_drug_from_symtom(symptom, curing_drug_list):
    query = create_drugbank_query(symptom)
    content_drugbank = drugbank_search(query)

    for item in content_drugbank:
        name = item[1]

        if name in curing_drug_list:
            curing_drug_list[name][0] += 1
        else:
            description = item[2]
            indication = item[3]
            toxicity = item[4]
            sources = "drugbank"
            bloc = [1, description, indication, toxicity, sources]
            curing_drug_list[name] = bloc

    curing_drug_list = dict(sorted(curing_drug_list.items(), key=lambda item: item[1], reverse=True))

    return curing_drug_list

def main():

    symptom = "abdominal"

    # correction of the input
    symptom = symptom.lower()

    ## THINGS TO PRINT : {0: disease_list, 1: curing_drug_list, 2: side_effects_from_drug_list, 3: All}
    print_value = 3

    ## CHECK ARGS
    args = sys.argv
    if "-s" in args:
        pos = args.index("-s")
        symptom = args[pos+1]
    if "-p" in args:
        pos = args.index("-p")
        print_value = int(args[pos+1])

    # initiation of global lists
    disease_list = {}
    curing_drug_list = {}
    content_sider_id = []
    side_effects_from_drug_list = {}

    def print_function(print_value, disease_list, curing_drug_list, side_effects_from_drug_list):
        if print_value==0:
            disease_list = search_disease_from_symptom(symptom, disease_list)
            print(len(disease_list))
            printlist(disease_list)
        elif print_value==1:
            curing_drug_list = search_curing_drug_from_symtom(symptom, curing_drug_list)
            print(len(curing_drug_list))
            printlist(curing_drug_list)
        elif print_value==2:
            content_sider_id = get_sider_id(symptom)
            content_sider_id = content_sider_id[:5]
            side_effects_from_drug_list = search_side_effects_drug_from_content_sider_id(content_sider_id, side_effects_from_drug_list)
            print(len(side_effects_from_drug_list))
            printlist(side_effects_from_drug_list)

    start = time.time()

    if print_value in [0, 1, 2]:
        print_function(print_value, disease_list, curing_drug_list, side_effects_from_drug_list)

    if print_value == 3:
        print_function(0, disease_list, curing_drug_list, side_effects_from_drug_list)
        print_function(1, disease_list, curing_drug_list, side_effects_from_drug_list)
        print_function(2, disease_list, curing_drug_list, side_effects_from_drug_list)

    end = time.time()

    print("#####")
    print()
    print(f"time : {end - start}")

if __name__ == '__main__':
    main()


from unittest import mock

import bson
import pytest

from test.tools import anything, in_any_order
from slivka import JobStatus
from slivka.db.documents import JobRequest, ServiceState
from slivka.db.helpers import delete_many, insert_many, pull_many
from slivka.scheduler import Runner, Scheduler
from slivka.scheduler.runners import Job, RunnerID
from slivka.scheduler.scheduler import (
    ERROR,
    REJECTED,
    ExecutionDeferred,
    ExecutionFailed,
)

def new_runner(service, name, command=None, args=None, env=None):
    return Runner(
        RunnerID(service, name),
        command=command or [],
        args=args or [],
        outputs=[],
        env=env or {},
    )

@pytest.fixture()
def mock_batch_start():
    with mock.patch.object(Runner, "batch_start") as mock_method:
        yield mock_method

@pytest.fixture()
def mock_check_status():
    with mock.patch.object(Runner, "check_status") as mock_method:
        yield mock_method

@pytest.fixture()
def mock_submit():
    with mock.patch.object(Runner, "submit") as mock_method:
        yield mock_method

def test_group_requests(job_directory):
    scheduler = Scheduler(job_directory)
    runner1 = new_runner("example", "runner1")
    runner2 = new_runner("example", "runner2")
    scheduler.add_runner(runner1)
    scheduler.add_runner(runner2)
    scheduler.selectors["example"] = lambda inputs: inputs.get("use")

    requests = [
        JobRequest(service="example", inputs={"use": "runner1"}),
        JobRequest(service="example", inputs={"use": "runner2"}),
        JobRequest(service="example", inputs={"use": None}),
        JobRequest(service="example", inputs={"use": "runner1"}),
    ]
    grouped = scheduler.group_requests(requests)
    assert grouped == {
        runner1: in_any_order(requests[0], requests[3]),
        runner2: in_any_order(requests[1]),
        REJECTED: in_any_order(requests[2]),
    }

def test_group_requests_if_runner_does_not_exist(job_directory):
    scheduler = Scheduler(job_directory)
    runner1 = new_runner("example", "runner1")
    scheduler.add_runner(runner1)
    scheduler.selectors["example"] = lambda inputs: "runner2"

    requests = [JobRequest(service="example", inputs={})]
    grouped = scheduler.group_requests(requests)
    assert grouped == {ERROR: in_any_order(*requests)}

def create_requests(count=1, service="example"):
    return [
        JobRequest(
            _id=bson.ObjectId(), service=service, inputs={"input": "val%d" % i}
        )
        for i in range(count)
    ]

def test_start_requests_if_successful_start(job_directory, mock_batch_start):
    scheduler = Scheduler(job_directory)
    runner = new_runner("example", "example")
    requests = [
        JobRequest(
            _id=bson.ObjectId(), service="example", inputs={"input": "val"}
        ),
        JobRequest(
            _id=bson.ObjectId(), service="example", inputs={"input": "val2"}
        ),
    ]
    mock_batch_start.side_effect = lambda inputs, cwds: (
        [Job("%04x" % i, cwd) for i, cwd in enumerate(cwds)]
    )
    started = scheduler._start_requests(runner, requests)
    assert started == in_any_order(
        *((req, Job("%04x" % i, anything())) for i, req in enumerate(requests))
    )

def test_start_requests_deferred_execution_if_error_raised(
    job_directory, mock_batch_start
):
    scheduler = Scheduler(job_directory)
    runner = new_runner("example", "example")
    requests = create_requests(2)
    mock_batch_start.side_effect = OSError
    with pytest.raises(ExecutionDeferred):
        scheduler._start_requests(runner, requests)

def test_start_request_failed_execution_if_too_many_errors_raised(
    job_directory, mock_batch_start
):
    scheduler = Scheduler(job_directory)
    runner = new_runner("example", "example")
    requests = create_requests(3)
    scheduler.set_failure_limit(0)
    mock_batch_start.side_effect = OSError
    with pytest.raises(ExecutionFailed):
        scheduler._start_requests(runner, requests)

class TestJobStatusUpdates:
    @pytest.fixture()
    def requests(self, database):
        requests = create_requests(5)
        insert_many(database, requests)
        yield requests
        delete_many(database, requests)

    @pytest.fixture()
    def scheduler(self, job_directory):
        scheduler = Scheduler(job_directory)
        runner = new_runner("example", "example")
        scheduler.add_runner(runner)
        scheduler.selectors["example"] = lambda inputs: "example"
        return scheduler

    @pytest.mark.parametrize("status", list(JobStatus))
    def test_check_status_updates_requests(
        self,
        scheduler,
        requests,
        database,
        mock_batch_start,
        mock_check_status,
        status,
    ):
        # must start the job, before moving to status check stage
        mock_batch_start.side_effect = lambda inputs, cwds: (
            [Job("%04x" % i, cwd) for i, cwd in enumerate(cwds)]
        )
        mock_check_status.return_value = status
        scheduler.main_loop()
        pull_many(database, requests)
        assert all(req.state == status for req in requests)

    def test_submit_deferred_job_status_not_updated(
        self, scheduler, requests, database, mock_submit
    ):
        mock_submit.side_effect = OSError
        scheduler.main_loop()
        pull_many(database, requests)
        assert all(req.state == JobStatus.ACCEPTED for req in requests)

    def test_submit_failed_job_status_set_to_error(
        self, scheduler, requests, database, mock_submit
    ):
        mock_submit.side_effect = OSError
        scheduler.set_failure_limit(0)
        scheduler.main_loop()
        pull_many(database, requests)
        assert all(req.state == JobStatus.ERROR for req in requests)

class TestServiceStatusUpdates:
    @pytest.fixture(autouse=True)
    def requests(self, database):
        requests = create_requests(5)
        insert_many(database, requests)
        yield requests
        delete_many(database, requests)

    @pytest.fixture()
    def scheduler(self, job_directory):
        scheduler = Scheduler(job_directory)
        runner = new_runner("example", "default")
        scheduler.add_runner(runner)
        return scheduler

    def test_service_start_successful(
        self, database, scheduler, mock_submit, mock_check_status
    ):
        mock_submit.side_effect = lambda cmd: Job("0x00", cmd.cwd)
        mock_check_status.return_value = JobStatus.QUEUED
        scheduler.main_loop()
        state = ServiceState.find_one(
            database, service="example", runner="default"
        )
        assert state.state == ServiceState.OK

    def test_service_start_soft_fail(self, database, scheduler, mock_submit):
        mock_submit.side_effect = OSError
        scheduler.main_loop()
        state = ServiceState.find_one(
            database, service="example", runner="default"
        )
        assert state.state == ServiceState.WARNING

    def test_service_start_hard_fail(self, database, scheduler, mock_submit):
        scheduler.set_failure_limit(0)
        mock_submit.side_effect = OSError
        scheduler.main_loop()
        state = ServiceState.find_one(
            database, service="example", runner="default"
        )
        assert state.state == ServiceState.DOWN

    @pytest.mark.xfail(reason="service status should not rely on erroneous jobs")
    def test_service_check_status_returned_all_errors(
        self, database, scheduler, mock_submit, mock_check_status
    ):
        mock_submit.side_effect = lambda cmd: Job("0x00", cmd.cwd)
        mock_check_status.return_value = JobStatus.ERROR
        scheduler.main_loop()
        state = ServiceState.find_one(
            database, service="example", runner="default"
        )
        assert state.state == ServiceState.DOWN

    def test_service_check_status_throws_exception(
        self, database, scheduler, mock_submit, mock_check_status
    ):
        mock_submit.side_effect = lambda cmd: Job("0x00", cmd.cwd)
        mock_check_status.side_effect = Exception
        scheduler.main_loop()
        state = ServiceState.find_one(
            database, service="example", runner="default"
        )
        assert state.state == ServiceState.WARNING


#!/usr/bin/env python3

import os

import mod_resource
import mod_something

if __name__ == "__main__":
    print("Hello, world! -> something returns: {}".format(mod_something.something()))

    res_path = \
        os.path.join(os.path.dirname(mod_resource.__file__), 'resource.txt')
    with open(res_path) as f:
        text = f.read()
    print("From mod_resource:resource.txt -> {}".format(text))


import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.options
import settings
from handlers import *

def make_app():
    db = None
    handlers = [
    (r"/", MainHandler),
    (r"/covert", CovertHandler)
    ]
    config = {"template_path":settings.TEMPLATE_PATH, "static_path":settings.ASSETS_PATH, "cookie_secret":settings.COOKIE_SECRET, "debug":True}
    return tornado.web.Application(handlers, **config)

if __name__ == '__main__':
    tornado.options.parse_command_line()
    app = make_app()
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(settings.SERVER_PORT)
    tornado.ioloop.IOLoop.instance().start()


# -*- coding: utf-8 -*-
"""
Created on Sun Sep  8 16:46:19 2019

@author: sanjain6
"""

if __name__ == '__main__':
    s = "abcab"
    c = 'abc'
    p = len(s)
    q = 0
    for i in range(len(s) - len(c) + 1):
        if c in s[i: i + len(c)]:
            q +=1

    print(q)


import socket
import os
import sys

# 第三方库
from 网络 import 创建网络

if __name__ == '__main__':
    创建网络.本地服务器地址='127.0.0.1'
    创建网络.本地服务器端口=1082
    插座= 创建网络.申请插座()
    print("服务器的socket建立了")
    插座.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    创建网络.绑定插座(插座)
    print('服务器的socket绑定到%s:%d'%(创建网络.本地服务器地址,创建网络.本地服务器端口))
    创建网络.监听插座(插座,500)
    print('服务器开始监听端口%d，等待连接。'%创建网络.本地服务器端口)
    while True:
        连线,客户端地址 = 创建网络.插座接受的连接(插座)
        print("服务器sock连接到客户端地址：",客户端地址)
        try:
            创建网络.连线超时(连线, 5)
            接收缓冲区=创建网络.连线接收的内容(连线, 2048)
            print('客户端发来数据：%s'%接收缓冲区)
            # if headers["Method"]=="CONNECT":
            发送的内容=b'HTTP/1.1 200 Connection Established\r\n\r\n'
            创建网络.连线发送(连线, 发送的内容)
            print("向客户端发送了HTTP/1.1 200 Connection Established\r\n\r\n")
            接收缓冲区=创建网络.连线接收的内容(连线, 2048)
            print('客户端再次发来数据：%s'%接收缓冲区)
            #else:
                #print("error")
        except socket.timeout:
            print ('time out')
        创建网络.关闭连接(连线)
    创建网络.关闭插座(插座)


#!/bin/python3

import os
import sys

class Node(object):
    def __init__(self, n):
        self.n = n
        self.neighbors = {}
        self.feet = 0

    def copy(self):
        n = Node(self.n)
        n.neighbors = self.neighbors.copy()
        n.feet = self.feet
        return n

class Graph(object):
    def __init__(self, edges=[], t=None):
        self.nodes = {}
        self.t = t
        self.crabs = 0
        for edge in edges:
            self.insert_edge(edge)

    def walk(self, edge=None):
        vertex = edge[0] if edge else 1
        q = [vertex]
        visited = [edge]
        while q:
            node = q.pop()
            for neighbor in self.nodes[node].neighbors:
                if [neighbor, node] in visited or [node, neighbor] in visited:
                    continue
                q.append(neighbor)
                visited.append([node, neighbor])
                yield [node, neighbor]

    def insert(self, node):
        if node not in self.nodes:
            self.nodes[node] = Node(node)

    def insert_edge(self, edge):
        v0, v1 = edge
        self.insert(v0)
        self.insert(v1)
        self.nodes[v0].neighbors[v1] = self.nodes[v1]
        self.nodes[v1].neighbors[v0] = self.nodes[v0]

    def violates_rules(self, edge):
        v0, v1 = edge
        if (self.nodes[v0].feet <= self.t) and (self.nodes[v1].feet == 1):
            print("False")
            return False
        if (self.nodes[v1].feet <= self.t) and (self.nodes[v0].feet == 1):
            print("False")
            return False
        print("True")
        return True

    def turn_on(self, edge):
        v0, v1 = edge
        self.insert_edge(edge)
        self.nodes[v0].feet += 1
        self.nodes[v1].feet += 1
        self.crabs += self.nodes[v0].feet==1
        self.crabs += self.nodes[v1].feet==1

    def turn_off(self, edge):
        v0, v1 = edge
        self.nodes[v0].feet -= 1
        self.nodes[v1].feet -= 1
        self.crabs -= self.nodes[v0].feet==0
        self.crabs -= self.nodes[v1].feet==0

    def copy(self):
        g = Graph()
        g.nodes = {k: v.copy() for k,v in self.nodes.items()}
        g.t = self.t
        g.crabs = self.crabs
        return g

    def __str__(self):
        s = ""
        for node in self.nodes:
            s += "{}: {}, ".format(node, self.nodes[node].feet)
        return s
#
# Complete the crabGraphs function below.
#
def crabGraphs(n, t, graph):
    #
    # Write your code here.
    #

    # dp -
    # grow the graph edge by edge. for each edge keep the best graph with and without it.
    # for the next edge, check all neighboring visited edges, and take the best graph without it and the best graph with it (restriction - must connect to head)

    # try to grow the graph edge by edge,
    # for each edge:
        # check every other edge, if violates the rules turn of, else turn on
    # if the graph is better with the edge, keep it on, else turn off
    graph = Graph(graph)
    crab_graph = Graph(t=t)
    num_crabs = 0
    for edge in graph.walk():
        print(edge)
        tmp_graph = crab_graph.copy()
        tmp_graph.turn_on(edge)
        for edge_ in tmp_graph.walk(edge):
            if tmp_graph.violates_rules(edge_):
                tmp_graph.turn_off(edge_)
        if tmp_graph.crabs > num_crabs:
            num_crabs = tmp_graph.crabs
            crab_graph = tmp_graph
        print(crab_graph)
    return num_crabs

if __name__ == '__main__':

    c = int(input())

    for c_itr in range(c):
        ntm = input().split()

        n = int(ntm[0])

        t = int(ntm[1])

        m = int(ntm[2])

        graph = []

        for _ in range(m):
            graph.append(list(map(int, input().rstrip().split())))

        result = crabGraphs(n, t, graph)

        print(str(result) + '\n')


import asyncio
import json
import multiprocessing as mp
from importlib import import_module

from django import http
from django.conf import settings
from django.core.cache import caches
from django.core.handlers.asgi import ASGIRequest
from django.contrib import auth
from django.utils import timezone

from asgiref.sync import sync_to_async

from loguru import logger

from worlds.models import Job, StreamLog

def add_websocket(app):
    async def websocket_app(scope, receive, send):
        if scope["type"] == "websocket":
            await logging_socket(scope, receive, send)
            return

        await app(scope, receive, send)

    return websocket_app

class AsyncWarpzoneRequest(ASGIRequest):
    def __init__(self, scope, body_file):
        scope['method'] = 'GET'
        super().__init__(scope, body_file)
        self.WS = http.QueryDict(scope.get('query_string', b'').decode())

def init_request(request):
    engine = import_module(settings.SESSION_ENGINE)
    session_key = request.COOKIES.get(settings.SESSION_COOKIE_NAME)
    request.session = engine.SessionStore(session_key)
    request.user = auth.get_user(request)

def get_job(jid, obj=False):
    job = Job.objects.filter(id=jid).first()
    if job:
        if obj:
            return job

        return job.to_json()

    return {}

def get_log(job, pod, obj=False):
    return StreamLog.objects.filter(job=job, pod=pod).first()

async def watch_log_data(job, pod, send, log_queue):
    lines = 0
    wait = 0.1

    while 1:
        try:
            await asyncio.sleep(wait)
            wait = 3.0

            log = await sync_to_async(get_log, thread_sensitive=True)(job, pod)
            if log:
                if log.lines != lines:
                    lines_send = ''
                    line_array = []
                    for i in range(lines, log.lines):
                        line_array.append(f'{pod}-{i}')

                    if line_array:
                        line_dict = caches['default'].get_many(line_array)
                        msg_lines = ''
                        for l in line_array:
                            m = line_dict.get(l, None)
                            if m is not None:
                                msg_lines += m

                        if msg_lines:
                            msg = {'type': 'log', 'data': msg_lines}
                            await send({'type': 'websocket.send', 'text': json.dumps(msg)})

                    lines = log.lines

                if log.status in ['completed', 'failed']:
                    break

        except:
            import traceback
            traceback.print_exc()
            raise

        try:
            if log_queue.get_nowait():
                log_queue.task_done()
                caches['default'].set(f'shutdown-{pod}', 'shutdown', 60)
                return

        except asyncio.QueueEmpty:
            pass

async def watch_job_data(job, send, queue):
    jdata = await sync_to_async(get_job, thread_sensitive=True)(job)
    last = timezone.now()

    while 1:
        await asyncio.sleep(0.1)
        now = timezone.now()
        diff = now - last
        if diff.total_seconds() > 5:
            last = now
            new_data = await sync_to_async(get_job, thread_sensitive=True)(job)
            if new_data['modified'] != jdata['modified']:
                jdata = new_data
                msg = {'type': 'job', 'data': jdata}
                logger.info('Sending job update: {} {}', jdata['id'], jdata['status'])
                await send({'type': 'websocket.send', 'text': json.dumps(msg)})

        try:
            if queue.get_nowait():
                queue.task_done()
                return

        except asyncio.QueueEmpty:
            pass

async def logging_socket(scope, receive, send):
    request = AsyncWarpzoneRequest(scope, None)
    await sync_to_async(init_request, thread_sensitive=True)(request)
    task = None
    log_task = None
    log_queue = None
    connected = False

    while 1:
        event = await receive()
        job = request.WS.get('job')
        pod = request.WS.get('pod')

        if event['type'] == 'websocket.connect':
            logger.info('Websocket Connected')

            if not request.user.is_authenticated:
                logger.info('User not authenticated, Closing Socket')
                await send({'type': 'websocket.close'})
                return

            job_queue = asyncio.Queue()
            task = asyncio.create_task(watch_job_data(job, send, job_queue))

            if pod:
                log_queue = asyncio.Queue()
                log_task = asyncio.create_task(watch_log_data(job, pod, send, log_queue))

            await send({'type': 'websocket.accept'})
            connected = True

        if connected and event['type'] == 'websocket.disconnect':
            logger.info('Websocket Disconnected')
            await job_queue.put(True)
            await job_queue.join()
            task.cancel()
            await asyncio.gather(task, return_exceptions=True)

            if log_queue:
                await log_queue.put(True)
                await log_queue.join()
                log_task.cancel()
                await asyncio.gather(log_task, return_exceptions=True)

            return

        if connected and event['type'] == 'websocket.receive':
            logger.info('Received Message')


class Solution:
    def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
        top = 0
        bottom = len(matrix)
        left = 0
        right = len(matrix[0])
        arr = []
        while (top < bottom and left < right):
            for j in range(left,right):
                arr.append(matrix[top][j])
            top += 1
            for j in range(top,bottom):
                arr.append(matrix[j][right-1])
            right -= 1

            if (not (left < right and top < bottom)):
                break

            for k in range(right-1,left-1,-1):
                arr.append(matrix[bottom-1][k])
            bottom -= 1
            for l in range(bottom-1,top - 1,-1):
                arr.append(matrix[l][left])
            left += 1
        return arr


from abc import ABC, abstractmethod

def process_for_variable(var, table_info):
    for tableName in table_info.keys():
        splited = var.split(tableName)
        if len(splited)>1:
            pure_variable = splited[-1][1:]
            if pure_variable == '':
                sql = table_info[tableName]['subQuery'].export_sql()
                return f'({sql})'
            alias = table_info[tableName]['alias']
            if not alias:
                return f'{tableName}.{pure_variable}'
            return f'{alias}.{pure_variable}'
    return var

def divide_symbols_and_values(condition):
    symbols = ['<=', '>=', '>', '<','=', 'in ', 'IN ']
    lst = []
    for field, con in condition.items():
        for symbol in symbols:
            splited = con.split(symbol)
            if len(splited) > 1:
                value = splited[-1].strip()
                symbol = symbol.strip()
                expression = (field, symbol, value)
                lst.append(expression)
                break
    return lst

class Clause(ABC):

    def update_table(self, table):
        self.table = table

    def set_aliasState(self, aliasState):
        self.aliasState = aliasState

    @abstractmethod
    def export_sql(self, aliasState):
        pass

class Select(Clause):

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

    def export_sql(self, aliasState):
        self.set_aliasState(aliasState=aliasState)
        if self.fields:
            adjusted_fields = [process_for_variable(field, self.table) for field in self.fields]
            select_part = ', '.join(adjusted_fields)
        else:
            select_part = '*'
        return f'SELECT {select_part}'

class From(Clause):

    def __init__(self, tableName):
        self.tableName_list = tableName

    def check_inlineView(self, tableName):
        if not tableName in self.table:
            return False
        if self.table[tableName]['subQuery'] :
            return True
        return False

    def export_sql(self, aliasState):
        self.set_aliasState(aliasState=aliasState)
        table_list = []
        for tableName in self.tableName_list:
            if self.check_inlineView(tableName=tableName):
                inleview_clause = self.table[tableName]['subQuery'].export_sql(aliasState=aliasState)
                self.table[tableName]['alias'] = self.aliasState.get_alias()
                alias = self.table[tableName]['alias']
                table_list.append(f'({inleview_clause}) {alias}')
            else:
                if len(self.tableName_list)>1:
                    if tableName in self.table:
                        self.table[tableName]['alias'] = self.aliasState.get_alias()
                        alias = self.table[tableName]['alias']
                        table_list.append(f'{tableName} {alias}')
                    else:
                        table_list.append(f'{tableName}')
                else:
                    table_list.append(tableName)
        from_part = ', '.join(table_list)
        return f'FROM {from_part}'

class Join(Clause):

    def __init__(self, tableName, how):
        self.tableName=tableName
        self.how=how

    def check_inlineView(self, tableName):
        if self.table[tableName]['subQuery'] :
            return True
        return False

    def export_sql(self, aliasState):
        if not self.tableName in self.table:
            return f'{self.how} JOIN {self.tableName}'

        self.set_aliasState(aliasState=aliasState)
        if self.check_inlineView(self.tableName):
            inleview_clause = self.table[self.tableName]['subQuery'].export_sql(aliasState=aliasState)
            self.table[self.tableName]['alias'] = self.aliasState.get_alias()
            alias = self.table[self.tableName]['alias']
            return f'{self.how} JOIN ({inleview_clause}) {alias}'
        else:
            if self.tableName in self.table:
                alias = self.table[self.tableName]['alias']
                return f'{self.how} JOIN {self.tableName} {alias}'
            else:
                return f'{self.how} JOIN {self.tableName}'

class On(Clause):

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

    def export_sql(self, aliasState):
        self.set_aliasState(aliasState=aliasState)
        var_symbols_var_tuple_lst = divide_symbols_and_values(self.condition)
        processed = []
        for tuple_ in var_symbols_var_tuple_lst:
            processed.append([process_for_variable(i, self.table) for i in tuple_])
        condition_lst = [' '.join(tuple_) for tuple_ in processed]
        on_part = ' AND '.join(condition_lst)
        return f'ON {on_part}'

class Where(Clause):

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

    def export_sql(self, aliasState):
        self.set_aliasState(aliasState=aliasState)
        var_symbols_var_tuple_lst = divide_symbols_and_values(self.condition)
        processed = []
        for tuple_ in var_symbols_var_tuple_lst:
            processed.append([process_for_variable(i, self.table) for i in tuple_])
        condition_lst = [' '.join(tuple_) for tuple_ in processed]
        where_part = ' AND '.join(condition_lst)
        return f'WHERE {where_part}'

class GroupBy(Clause):

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

    def export_sql(self, aliasState):
        self.set_aliasState(aliasState=aliasState)
        adjusted_fields = [process_for_variable(field, self.table) for field in self.fields]
        groupBy_part = ', '.join(adjusted_fields)
        return f'GROUP BY {groupBy_part}'

class Calculation(Clause):

    def __init__(self, field, kind):
        self.field = field
        self.kind = kind
        self.sql_fuction = {'sum': 'SUM', 'min':'MIN', 'max':'MAX', 'avg':'AVG', 'count':'COUNT' ,'variance':'VARIANCE', 'stddev':'STDDEV', 'lag':'LAG'}

    def export_sql(self, aliasState):
        self.set_aliasState(aliasState=aliasState)
        adjusted_field = process_for_variable(self.field, self.table)
        sql_function = self.sql_fuction[self.kind]
        return f'{sql_function}({adjusted_field})'

class OrderBy(Clause):

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

    def export_sql(self, aliasState):
        self.set_aliasState(aliasState=aliasState)
        adjusted_fields = [process_for_variable(field, self.table) for field in self.fields]
        groupBy_part = ', '.join(adjusted_fields)
        return f'ORDER BY {groupBy_part}'

class Alias:

    def __init__(self) :
        self.alias_candidate = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']
        self.alias_num = 0

    def get_alias(self):
        alias = self.alias_candidate[self.alias_num]
        self.alias_num += 1
        return alias

class QuerySql:

    def __init__(self, tableName, subQuery=None):
        self.table = tableName
        self.table_info = dict()
        self.subQuery = dict()

        self.select_part = None
        self.from_part = None
        self.join_part = None
        self.on_part = None
        self.where_part = None
        self.groupBy_part = None
        self.calculation_part = []
        self.orderBy_part = None
        self.alias_state = None

        self.set_table(tableName=tableName, subQuery=subQuery)

    def set_table(self, tableName, subQuery=None):
        self.table_info[tableName] = {'subQuery':subQuery, 'alias': None}
        if self.select_part:
            self.select_part.update_table(table=self.table_info)
        if self.from_part:
            self.from_part.update_table(table=self.table_info)
        if self.join_part:
            self.join_part.update_table(table=self.table_info)
        if self.where_part:
            self.where_part.update_table(table=self.table_info)
        return self

    def select(self, *fields):
        self.select_part = Select(fields=fields)
        self.select_part.update_table(table=self.table_info)
        if self.table :
            self.from_(self.table)
        return self

    def from_(self, *tableName):
        self.from_part = From(tableName=tableName)
        self.from_part.update_table(table=self.table_info)
        return self

    def join(self, tableName, how='LEFT', **condition):
        self.join_part = Join(tableName=tableName, how=how.upper())
        self.join_part.update_table(table=self.table_info)

        self.on_part = On(condition=condition)
        self.on_part.update_table(table=self.table_info)
        return self

    def on(self, **condition):
        self.on_part = On(condition=condition)
        self.on_part.update_table(table=self.table_info)
        return self

    def where(self, **condition):
        self.where_part = Where(condition=condition)
        self.where_part.update_table(table=self.table_info)
        return self

    def group_by(self, *fields):
        self.groupBy_part = GroupBy(fields)
        self.groupBy_part.update_table(table=self.table_info)
        return self

    def sum(self, fields):
        self.calculation_part.append(Calculation(fields, kind='sum'))
        for part in self.calculation_part:
            part.update_table(table=self.table_info)
        return self

    def min(self, fields):
        self.calculation_part.append(Calculation(fields, kind='min'))
        for part in self.calculation_part:
            part.update_table(table=self.table_info)
        return self

    def max(self, fields):
        self.calculation_part.append(Calculation(fields, kind='max'))
        for part in self.calculation_part:
            part.update_table(table=self.table_info)
        return self

    def avg(self, fields):
        self.calculation_part.append(Calculation(fields, kind='avg'))
        for part in self.calculation_part:
            part.update_table(table=self.table_info)
        return self

    def variance(self, fields):
        self.calculation_part.append(Calculation(fields, kind='variance'))
        for part in self.calculation_part:
            part.update_table(table=self.table_info)
        return self

    def stddev(self, fields):
        self.calculation_part.append(Calculation(fields, kind='stddev'))
        for part in self.calculation_part:
            part.update_table(table=self.table_info)
        return self

    def count(self, fields):
        self.calculation_part.append(Calculation(fields, kind='count'))
        for part in self.calculation_part:
            part.update_table(table=self.table_info)
        return self

    def lag(self, fields):
        self.calculation_part.append(Calculation(fields, kind='lag'))
        for part in self.calculation_part:
            part.update_table(table=self.table_info)
        return self

    def order_by(self, *fields):
        self.orderBy_part = OrderBy(fields)
        self.orderBy_part.update_table(table=self.table_info)
        return self

    def export_sql(self, aliasState=None):
        if not aliasState:
            self.alias_state = Alias()
            aliasState = self.alias_state
        loop_order = {'from':self.from_part, 'join':self.join_part, 'on':self.on_part, 'groupBy':self.groupBy_part, 'select':self.select_part, 'calculation':self.calculation_part, 'where':self.where_part, 'orderBy': self.orderBy_part}
        clause_order = {'select':'', 'from':'', 'join':'', 'on':'', 'where':'', 'groupBy':'', 'orderBy':''}
        for clause, part in loop_order.items():
            if part:
                if clause=='calculation':
                    lst = [i.export_sql(aliasState) for i in part]
                    if self.select_part:
                        lst = [clause_order['select']] + lst
                        clause_order['select'] = ', '.join(lst)
                    else:
                        clause_order['select'] = 'SELECT ' + ', '.join(lst)
                else:
                    clause_order[clause] = part.export_sql(aliasState)
        part_list = list(v for v in clause_order.values() if v != '')
        return ' '.join(part_list)


loadModule('/Capella/Capella')
import sys

def capella_query(query_class, e_obj, cls = None):
    """Call a query from the semantic browser from the qualified class name of the query and the EObect to pass as parameter"""
    res = []
    for e in callQuery(query_class, e_obj.get_java_object()):
        e_object_class = getattr(sys.modules["__main__"], "EObject")
        specific_cls = e_object_class.get_class(e)
        if specific_cls is not None:
            res.append(specific_cls(e))
        elif cls is not None:
            res.append(cls(e))
    return res

def capella_query_by_name(e_obj, query_name, cls = None):
    """Call a query from the semantic browser from the query name and the EObect to pass as parameter"""
    res = []
    for e in getSBQuery(e_obj.get_java_object(), query_name):
        e_object_class = getattr(sys.modules["__main__"], "EObject")
        specific_cls = e_object_class.get_class(e)
        if specific_cls is not None:
            res.append(specific_cls(e))
        elif cls is not None:
            res.append(cls(e))
    return res

def available_query_names(e_obj):
    """List all available query names for the given EObject"""
    return getAvailableSBQueries(e_obj.get_java_object())

def capella_version():
    return getCapellaVersion()

def get_label(e_obj):
    return getLabel(e_obj.get_java_object())

def is_system(component):
    return isSystem(component)

def get_libraries(system_engineering):
    res = []

    if system_engineering is not None:
        lib_cls = getattr(sys.modules["__main__"], "CapellaLibrary")
        for value in getLibraries(system_engineering.get_java_object()):
            lib = lib_cls()
            lib.open(value)
            res.append(lib)

    return res


from os import *
from sys import *
from collections import *
from math import *

def findInMatrix(x, arr):

    # Write your code here
    ## We know that each column and each row is sorted.
    ## Let us begin from the Right Top most point
    row, column = 0, len(arr[0])-1
    while row<len(arr) and column>=0:
        extremity_value = arr[row][column]
        if extremity_value == x:
            return True
        elif extremity_value > x:
            column -=1
        else:
            row +=1
    return False

CodingNinjas/Search in a 2D matrix II.py


import pytest
from unittest.mock import AsyncMock, patch
from api.exceptions import InvalidParameterError
from crawler.default.instances.second_instance import SecondInstance

# Mock para a resposta do ClientSession
mock_response = AsyncMock()
mock_response.text.return_value = 'Sample Text'

@pytest.mark.asyncio
async def test_capturar_numero_processo_codigo_invalid():
    instance = SecondInstance("TJ", "http://example.com")

    with pytest.raises(InvalidParameterError):
        await instance._capturar_numero_processo_codigo("123456")

@pytest.mark.asyncio
@patch('crawler.default.instances.second_instance.ClientSession')
async def test_consultar_processo(mock_session):
    mock_session.return_value.__aenter__.return_value.get.return_value = mock_response

    instance = SecondInstance("TJ", "http://example.com")

    result = await instance._consultar_processo("789")

    assert result == "Sample Text"


import matplotlib.pyplot as plt
import numpy as np
import torch
from torch import nn

from RNN_torch.model import RNN

# Hyper parameters
BATCH_SIZE = 64
EPOCH = 1
TIME_STEP = 28  # 考虑多少个时间点的数据
INPUT_SIZE = 1  # 每个时间点给RNN多少个数据点
LR = 0.01

rnn = RNN(INPUT_SIZE)
print(rnn)

optimizer = torch.optim.Adam(rnn.parameters(), lr=LR)  # optimize all cnn parameters
loss_func = nn.MSELoss()

h_state = None

plt.figure(1, figsize=(12, 5))
plt.ion()

for step in range(50):
    start, end = step * np.pi, (step + 1) * np.pi
    # use sin pre cos
    steps = np.linspace(start, end, TIME_STEP, dtype=np.float32)
    x_np = np.sin(steps)
    y_np = np.cos(steps)

    x = torch.from_numpy(x_np[np.newaxis, :, np.newaxis])  # shape(batch, time_step, input_size)
    y = torch.from_numpy(y_np[np.newaxis, :, np.newaxis])

    prediction, h_state = rnn(x, h_state)
    h_state = h_state.data  # !!! this step is important

    loss = loss_func(prediction, y)
    optimizer.zero_grad()  # clear gradient for next train
    loss.backward()  # back propagation, compute gradient
    optimizer.step()

    # plot
    plt.plot(steps, y_np.flatten(), 'r-')
    plt.plot(steps, prediction.data.numpy().flatten(), 'b-')
    plt.draw()
    plt.pause(0.5)

plt.ioff()
plt.show()


'''Write a Python program to count the number of strings where
    the string length is 2 or more and the first and last character
    are samefrom a given list of strings.'''

def give_str(words):
    selected_words = []
    for word in words:
        if len(word) > 2 and word[0] == word[-1]:
            selected_words.append(word)
    return selected_words

words = ['abc','xyz','aba','1221']
print(give_str(words))
print("No of selected_words: "+str(len(give_str(words))))


import collections.abc
import copy
import typing

import enpheeph.injections.plugins.indexing.abc.indexingpluginabc
import enpheeph.utils.constants
import enpheeph.utils.dataclasses
import enpheeph.utils.enums
import enpheeph.utils.typings

class IndexingPlugin(
    enpheeph.injections.plugins.indexing.abc.indexingpluginabc.IndexingPluginABC
):
    # it is Optional so that we can use None
    active_dimension_index: typing.Optional[
        typing.List[enpheeph.utils.typings.ActiveDimensionIndexType]
    ]
    dimension_dict: enpheeph.utils.typings.DimensionDictType

    def __init__(
        self, dimension_dict: enpheeph.utils.typings.DimensionDictType
    ) -> None:
        self.dimension_dict = dimension_dict

        self.reset_active_dimensions()

    # to select a set of dimensions to be used as active when selecting tensor indices
    # by default no dimension is considered active
    def select_active_dimensions(
        self,
        dimensions: collections.abc.Container[enpheeph.utils.enums.DimensionType],
        # if True, we will move all the indices so that the first index is 0
        # and the last is -1
        autoshift_to_boundaries: bool = False,
        # if True we fill the empty indices with the filler
        # if False we will skip them
        fill_empty_index: bool = True,
        # the filler to use, defaults to : for a single dimension,
        # which is slice(None, None)
        filler: typing.Any = slice(None, None),
    ) -> typing.List[enpheeph.utils.typings.ActiveDimensionIndexType]:
        # we invert the dimension dict to easily look it up
        # as we will be using the indices to look it up instead of the names
        inverted_dimension_dict = {v: k for k, v in self.dimension_dict.items()}
        # we get the highest index for both the positive and the negative indices
        # in terms of absolute value
        # we filter the Ellipsis to avoid mypy errors
        # **NOTE**: improve the typing here
        no_ellipsis_dimension_dict_values: typing.List[int] = typing.cast(
            typing.List[int,],
            [x for x in self.dimension_dict.values() if x != Ellipsis],
        )
        longest_positive_range: int = max(
            (x for x in no_ellipsis_dimension_dict_values if x >= 0),
            # we use -1 default so that range(-1 + 1) = []
            default=-1,
        )
        longest_negative_range: int = min(
            (x for x in no_ellipsis_dimension_dict_values if x < 0),
            # we use the number right outside the range to get an empty list
            default=0,
        )
        # this list contains all the possible indices including Ellipsis
        total_indices: typing.List[enpheeph.utils.typings.DimensionIndexType] = list(
            # we cover all the indices to the maximum,
            # including the maximum itself,
            # hence the + 1
            range(longest_positive_range + 1),
        )
        # we need to split the list creation otherwise mypy complains of different types
        total_indices += [Ellipsis]
        total_indices += list(
            # we create the list going from the most negative index to 0
            # 0 is excluded
            range(
                longest_negative_range,
                0,
            ),
        )
        # we save the filling and the valid indices in the following list
        dimension_index: typing.List[
            enpheeph.utils.typings.ActiveDimensionIndexType,
        ] = []
        for index in total_indices:
            # the index is saved if it is present in the dimensions to be selected
            # here we still don't consider the autoshift
            if (
                index in inverted_dimension_dict
                and inverted_dimension_dict[index] in dimensions
            ):
                dimension_index.append(inverted_dimension_dict[index])
            # if the index is not included, we then check if we need to fill it
            # due to fill_empty_index
            elif fill_empty_index:
                dimension_index.append(filler)
        if autoshift_to_boundaries:
            # we remove all the elements at the beginning/end of the list
            # that are fillers
            i = 0
            # infinite loop, but there is a break
            # **NOTE**: probably it can be optimized further
            while 1:
                # we start from 0, and for each filler we match we remove it
                if dimension_index[i] == filler:
                    del dimension_index[i]
                # if the element is not a filler than the start is done and we check the
                # end using -1
                elif i == 0:
                    i = -1
                # if both the element is not a filler and the index is at the end, it
                # means we are done
                else:
                    break
        # we copy the dimensions and we return them
        self.active_dimension_index = copy.deepcopy(dimension_index)
        return copy.deepcopy(self.active_dimension_index)

    # to reset the active dimensions to the empty dimension dict
    def reset_active_dimensions(self) -> None:
        self.active_dimension_index = None

    # to join indices following the order provided by the active_dimension dict
    def join_indices(
        self,
        dimension_indices: enpheeph.utils.typings.DimensionLocationIndexType,
    ) -> enpheeph.utils.typings.AnyIndexType:
        if self.active_dimension_index is None:
            raise ValueError(
                "First select the active dimensions with select_active_dimensions"
            )

        index: typing.List[enpheeph.utils.typings.Index1DType] = []
        for i in self.active_dimension_index:
            # if we have an enum as index we check it from the given dimensions
            if isinstance(i, enpheeph.utils.enums.DimensionType):
                # to check if we have a sequence of sequence we want each element
                # to be a sequence and have no elements which are integers, as
                # the other allowed values represent sequences
                sequence_of_sequence = isinstance(
                    dimension_indices[i], collections.abc.Sequence
                ) and not any(
                    isinstance(j, int)
                    # we use typing.cast to avoid mypy complaining
                    for j in typing.cast(
                        typing.Sequence[typing.Any],
                        dimension_indices[i],
                    )
                )
                # if it is a sequence of sequences we extend the index with all the
                # sub-sequences, as it will cover multiple dimensions
                if sequence_of_sequence:
                    index.extend(
                        typing.cast(
                            typing.Tuple[enpheeph.utils.typings.Index1DType, ...],
                            dimension_indices[i],
                        ),
                    )
                # otherwise it covers only 1 dimension so we append the element directly
                else:
                    index.append(
                        typing.cast(
                            enpheeph.utils.typings.Index1DType,
                            dimension_indices[i],
                        ),
                    )
            # if the element is not an enum it will be a filler,
            # so we append it directly
            else:
                index.append(i)
        return copy.deepcopy(tuple(index))

    # to filter a size/shape array depending on the active dimension index
    # by selecting only the dimensions with the enum
    def filter_dimensions(
        self,
        # a normal size/shape array
        dimensions: typing.Sequence[int],
    ) -> typing.Tuple[int, ...]:
        if self.active_dimension_index is None:
            raise ValueError(
                "First select the active dimensions with select_active_dimensions"
            )

        enum_types = [
            e
            for e in self.active_dimension_index
            if isinstance(e, enpheeph.utils.enums.DimensionType)
        ]
        active_dimension_index: typing.List[
            enpheeph.utils.typings.ActiveDimensionIndexType
        ] = copy.deepcopy(self.active_dimension_index)
        for e in enum_types:
            if self.dimension_dict[e] == Ellipsis:
                while len(dimensions) > len(active_dimension_index):
                    active_dimension_index.insert(active_dimension_index.index(e), e)
        # this is executed if the loop exits normally
        else:
            if len(dimensions) != len(active_dimension_index):
                raise ValueError(
                    "dimensions must be the same length of active_dimension_index "
                    "if no Ellipsis are used"
                )

        return_dimensions = []
        for d, ind in zip(dimensions, active_dimension_index):
            if isinstance(ind, enpheeph.utils.enums.DimensionType):
                return_dimensions.append(d)
        return tuple(return_dimensions)


import pandas as pd
import requests

from datetime import datetime

DISCORD_URL = "https://discord.com/api/v9/invites/UQZpTQbCT4?with_counts=true"
STARTED_AT = datetime.now()

request = requests.get(DISCORD_URL)

data = request.json()

new_dataframe = pd.json_normalize(data, max_level=2)

new_dataframe["_started_at"] = STARTED_AT.strftime("%Y-%m-%dT%H:%M:%S.%fZ")

old_dataframe = pd.read_parquet("../data/discord.parquet")

current_dataframe = pd.concat([new_dataframe, old_dataframe])

current_dataframe.to_parquet("../data/discord.parquet", compression="gzip")