#与えられた文字列の各文字を，以下の仕様で変換する関数cipherを実装せよ．
#英小文字ならば(219 - 文字コード)の文字に置換
#その他の文字はそのまま出力
#この関数を用い，英語のメッセージを暗号化・復号化せよ．

def cipher(text):
    text = [chr(219 - ord(w)) if 97 <= ord(w) <= 122 else w for w in text]
    print(text)
    return ''.join(text)

text = 'this is a message.'
ans = cipher(text)
print(ans)
ans = cipher(ans)
print(ans)


def test(adn):
    def esMutante(secuencia):
        # secuencia[i:i+4] es como un "slice"
        # A * 4 = AAAA
        return any(secuencia[i:i+4] == secuencia[i]*4 for i in range(len(secuencia)-3))

    # Filas
    if any(esMutante(fila) for fila in adn):
        return True

    # Columnas
    if any(esMutante(''.join(adn[i][j] for i in range(len(adn)))) for j in range(len(adn[0]))):
        return True

    # Diagonales
    if any(esMutante(''.join(adn[i+k][j+k] for k in range(4))) for i in range(len(adn)-3) for j in range(len(adn[0])-3)):
        return True

    if any(esMutante(''.join(adn[i-k][j+k] for k in range(4))) for i in range(3, len(adn)) for j in range(len(adn[0])-3)):
        return True

    return False

# adn = ["ATGCGA", "CDGTGC", "TTATST", "AGAAGG", "CCCDTA", "TCACTG"]

adn = []
letras = 'ACGT'

while(len(adn) < 6) :
    print('Ingrese el subadn')
    aux = input()
    if len(aux) == 6 and any(aux[j] in letras for j in range(1,len(aux))) :
        adn.append(aux)
    else :
        print('El subadn debe tener 6 caracteres y solo puede contener las letras A,C,G y T por favor ingreselo de nuevo')

if test(adn):
    print("La persona es mutante.")
else:
    print("La persona no es mutante.")


list = [3, 2, 5, 7, 4]

def inseration_sort(list):
    for i in range(1, len(list)):
        print(f"Elementul curent din lista Nesortata ce va fi introdus in lista Sortata este: {list[i]}")
        print(f"Noua lista nesortata este: {list[i:]}")
        for j in range( i-1, -1, -1):
            print(f"Comparam elementul nou introdus, si anume: {list[j+1]} cu elementul deja aflat in lista sortata, si anume: {list[j]} ")
            if list[j] > list[j+1]:
                list[j], list[j+1] = list[j+1], list[j]
            else:
                break

if __name__ == "__main__":
    lista = [3, 2, 5, 7, 4]

    inseration_sort(lista)


import pygame

def make_grid(width, height):
	output = []

	while width > 0:
		width -= 1
		output.append([None] * height)
	return output

def coerce_range(value, lower, upper):
	if value < lower: return lower
	if value > upper: return upper
	return value

def range255(value):
	if value < 0: return 0
	if value > 255: return 255
	return int(value)

_alpha_rects = {}

def draw_alpha_rectangle(screen, x, y, width, height, r, g, b, alpha):
	key = str(width) + ':' + str(height)
	img = _alpha_rects.get(key, None)
	if img == None:
		img = pygame.Surface((width, height)).convert()
	img.fill((r, g, b))
	img.set_alpha(alpha)

	screen.blit(img, (x, y))


#!/usr/bin/env python3
# https://www.yuque.com/qw5nze/ga14hc/bqd045

from html.parser import HTMLParser
from urllib.request import urlopen
import sys
import json

class TypeParser(HTMLParser):

    def __init__(self, url, begin_row=0, begin_col=0):
        super().__init__()
        self.result = {}
        self._begin_row = begin_row
        self._begin_col = begin_col
        self._row = -1
        self._col = -1
        self._in_td = False
        self.feed(urlopen(url).read().decode('utf-8'))

    def handle_starttag(self, tag, attrs):
        self._in_td = tag == 'td'
        if self._in_td:
            self._col += 1
        elif tag == 'tr':
            self._row += 1
            self._col = -1

    def handle_endtag(self, tag):
        self._in_td = False

    def handle_data(self, data):
        if self._in_td and self._row >= self._begin_row:
            if self._col == self._begin_col:
                self._value = data.strip()
            elif self._col == self._begin_col + 1:
                self.result[data.strip()] = self._value

    def merge_aliases(self, aliases):
        for k, v in self.result.items():
            if v in aliases:
                self.result[k] = aliases[v]
                del aliases[v]

    def hack_aliases(self, hack, aliases):
        for k, vs in hack.items():
            for v in vs:
                if v in aliases:
                    alias = aliases[v]
                    del aliases[v]
                else:
                    alias = v
                if k in self.result:
                    self.result[k] += ',' + alias
                else:
                    self.result[k] = alias

# Parse type name
#parser = TypeParser('https://doc-bot.tmall.com/docs/doc.htm?articleId=108271&docType=1')
#parser = TypeParser('https://www.aligenie.com/doc/357554/eq19cg', 1)
parser = TypeParser('https://www.aligenie.com/doc/357554/gxhx67', 1, 1)
aliases = json.load(urlopen('https://open.bot.tmall.com/oauth/api/aliaslist'))['data']
places = json.load(urlopen('https://open.bot.tmall.com/oauth/api/placelist'))['data']

aliases = {i['key']: ','.join(i['value'] if i['key'] in i['value'] else [i['key']] + i['value']) for i in aliases}
parser.merge_aliases(aliases)
# parser.hack_aliases({'airpurifier': ['净化器']}, aliases)

# Generate
if len(sys.argv) > 1 and sys.argv[1] == 'md':
    print('区域名称：' + ','.join(places) + '\n')
    print('设备名称：' + ';'.join([v for k, v in parser.result.items()]) + '\n')
    print('未知类型：' + ';'.join([v for k, v in aliases.items()]) + '\n')
else:
    print('ZONE_PLACES = ' + str(places) + '\n')
    print('TYPE_NAMES = ' + str(parser.result).replace('{', '{\n    ').replace('}', '\n}').replace(', ', ',\n    ') + '\n')
    #print('VOID_NAMES = ' + str([v for k, v in aliases.items()]).replace('[', '[\n    ').replace(']', '\n]').replace(', ', ', \n    ') + '\n')


from util.postgres.conexao import ConfiguracoesConexaoPostgresSQL
from ibge.extract.download import get_dados_populacionais_municipios, get_dados_populacionais_ufs
from sqlalchemy import create_engine
import sys

def criar_esquema(config, child_db):

    conexao = config.get_conexao()

    # Criação de um cursor da conexão tipo "psycopg2" referenciado à variável "cursor"
    cursor = conexao.cursor()
    # Inicializa o schema denominado "ibge" no banco de dados mãe "DB_NAME"
    cursor.execute(f'CREATE SCHEMA IF NOT EXISTS {child_db};')
    conexao.commit()

    cursor.execute(f'''/*Tabela de população por municipios*/
                        CREATE TABLE IF NOT EXISTS {child_db}.populacao_municipio("ID"                  VARCHAR(6),
                                                                                "MUNNOME"	            VARCHAR(66),
                                                                                "MUNCODDV"            VARCHAR(7),
                                                                                "POPULACAO"           INTEGER);
        ''')

    cursor.execute(f'''/*Tabela de população por UFs*/
                        CREATE TABLE IF NOT EXISTS {child_db}.populacao_uf("ID"                  VARCHAR(6),
                                                                           "ESTADO"	             VARCHAR(66),
                                                                           "POPULACAO"           INTEGER);
            ''')

    conexao.commit()
    # Encerra o cursor
    cursor.close()
    # Encerra a conexão
    conexao.close()

def inserir_dados(config, child_db):
    inserir_dados_municipios(child_db, config)
    inserir_dados_ufs(child_db, config)

def inserir_dados_ufs(child_db, config):
    # Recupera o dataframe que contém as populações de cada município
    df = get_dados_populacionais_ufs()
    df.columns = ['ID', 'ESTADO', 'POPULACAO']
    # URI do banco de dados mãe do SGBD PostgreSQL
    DATABASE_URI = config.get_string_conexao()
    # Cria um "engine" para o banco de dados mãe "DB_NAME" usando a função "create_engine" do SQLAlchemy
    engine = create_engine(DATABASE_URI)
    # Remove em particular a última linha, que possui ID textual.
    df = df[df.ID.apply(lambda x: x.isnumeric())]
    df.to_sql('populacao_uf', con=engine, schema=child_db, if_exists='append', index=False, index_label='ID')

def inserir_dados_municipios(child_db, config):
    # Recupera o dataframe que contém as populações de cada município
    df = get_dados_populacionais_municipios()
    df.columns = ['ID', 'MUNNOME', 'POPULACAO']
    # Recupera o último dígito (verificador)
    df['MUNCODDV'] = df['ID']
    # Exclui o último dígito numérico das colunas identificadas, o qual corresponde ao dígito de controle do código...
    # do município. Foi detectado que para alguns municípios o cálculo do dígito de controle não é válido
    if len(df.loc[0, 'ID']) == 7:
        df['ID'].replace(regex='.$', value='', inplace=True)
    # URI do banco de dados mãe do SGBD PostgreSQL
    DATABASE_URI = config.get_string_conexao()
    # Cria um "engine" para o banco de dados mãe "DB_NAME" usando a função "create_engine" do SQLAlchemy
    engine = create_engine(DATABASE_URI)
    # Remove em particular a última linha, que possui ID textual.
    df = df[df.ID.apply(lambda x: x.isnumeric())]
    df.to_sql('populacao_municipio', con=engine, schema=child_db, if_exists='append', index=False, index_label='ID')

if __name__ == '__main__':
    # Conecta ao banco de dados mãe "DB_NAME" do SGBD PostgreSQL usando o módulo python "psycopg2"
    arquivo_configuracao = sys.argv[1]
    config = ConfiguracoesConexaoPostgresSQL(arquivo_configuracao)

    child_db = 'ibge'
    criar_esquema(config, child_db)
    inserir_dados(config, child_db)


from simcse import SimCSE
import pandas as pd

DATABASE_FILE = "corpus_sample/filtered_corpus_after.csv"

def load_body_texts():
    df = pd.read_csv(DATABASE_FILE, low_memory=False)
    result = []
    for email_id in range(len(df)):
        if type(df.iloc[email_id]['body_text']) == str:
            result.append(df.iloc[email_id]['body_text'] + f"#{email_id}")
        else:
            result.append("")
    return result

all_texts = load_body_texts()

def find_identical_body(email_id):
    identicals = set()
    for i in range(len(all_texts)):
        if all_texts[i] == all_texts[email_id]:
            identicals.add(i)
    return identicals

def nonempty_rows():
    df = pd.read_csv(DATABASE_FILE)
    result = []
    for email_id in range(len(df)):
        if type(df.iloc[email_id]['body_text']) == str:
            result.append(email_id)
    return result

#body_texts = load_body_texts()

model = SimCSE("princeton-nlp/sup-simcse-bert-base-uncased")
model.build_index(all_texts)

def common_neighbors(sents):
    neighbor_sents = model.search(sents)
    return neighbor_sents

def nearest_neighbors(sents):
    neighbor_sents = model.search(sents)
    nearest = []
    for neighborhood in neighbor_sents:
        if len(neighborhood) <= 1:
            nearest.append(None)
        else:
            nearest.append(neighbor_sents[1])

def very_similar(sents, threshold=0.98):
    neighbor_sents = model.search(sents, threshold=threshold, top_k=10)
    all_buddies = set()
    for neighborhood in neighbor_sents:
        index = 0
        buddies = set()
        while index < len(neighborhood) and neighborhood[index][1] >= threshold:
            neighbor = neighborhood[index][0]
            neighbor_id = neighbor.split("#")[-1]
            buddies.add(neighbor_id)
            index += 1
        buddies = tuple(sorted(buddies))
        if len(buddies) > 1:
            all_buddies.add(buddies)
    return all_buddies


from app import app
from flask import request
import json
import traceback

_print = print

def print(s):
	return s

@app.route('/mark', methods=['POST'])
def mark():
	if request:
		data = request.json
		code = data['code']
		_print(code)
		try:
			code = eval(code)
			_print('> {}'.format(code))
		except:
			code = traceback.format_exc()

		return str(code)
	return 'false'


import os
import re
def jp_manage(domain):

    # command = 'whois ' + domain
    command = 'whois ureru.co.jp'
    result = os.popen(command)
    data_result = result.read()

    domain_info = {}
    pattern = re.compile(r'(\[Name\].*|\[Phone\].*|\[Email\].*|\[Administrative Contact\].*)')
    match = pattern.findall(data_result)
    # print match[0].split(']')
    # print match[0].split('')[2:]
    count = len(match)
    # print data_result
    for i in range(count):

        if match[i].split(' ')[0].strip()=='[Name]':
            domain_info['reg_name'] = match[i][6:].strip()
        elif match[i].split(' ')[0].strip()=='[Phone]':
            domain_info['reg_phone'] = match[i][7:].strip()
        elif match[i].split(' ')[0].strip()=='[Email]':
            domain_info['reg_email'] = match[i][7:].strip()
        elif match[i].split(']')[0].strip()=='[Administrative Contact':
            domain_info['reg_name'] = match[i][24:].strip()
    # domain_info['detail'] = str(data_result)
    # print domain_info

    return domain_info

jp_manage(domain = 'ureru.co.jp')


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

from __future__ import absolute_import, unicode_literals

import json
from builtins import dict

from future import standard_library
from pyload.core import datatype
from pyload.core.datatype import BaseObject, ExceptionObject
from pyload.utils import convert

standard_library.install_aliases()

# compact json separator
separators = (',', ':')

# json encoder that accepts api objects
class BaseEncoder(json.JSONEncoder):

    def default(self, o):
        if isinstance(o, BaseObject) or isinstance(o, ExceptionObject):
            ret = {'@class': o.__class__.__name__}
            for att in o.__slots__:
                ret[att] = getattr(o, att)
            return ret

        return json.JSONEncoder.default(self, o)

# more compact representation, only clients with information of the
# classes can handle it
class BaseEncoderCompact(json.JSONEncoder):

    def default(self, o):
        if isinstance(o, BaseObject) or isinstance(o, ExceptionObject):
            ret = {'@compact': [o.__class__.__name__]}
            ret['@compact'].extend(getattr(o, attr) for attr in o.__slots__)
            return ret

        return json.JSONEncoder.default(self, o)

def convert_obj(dct):
    if '@class' in dct:
        cls = getattr(datatype, dct['@class'])
        del dct['@class']
        # convert keywords to str, <=2.6 does not accept unicode
        return cls(**dict((convert.to_str(x, x), y)
                          for x, y in dct.items()))
    elif '@compact' in dct:
        cls = getattr(datatype, dct['@compact'][0])
        return cls(*dct['@compact'][1:])

    return dct

def dumps(*args, **kwargs):
    if 'compact' in kwargs and kwargs['compact']:
        kwargs['cls'] = BaseEncoderCompact
        del kwargs['compact']
    else:
        kwargs['cls'] = BaseEncoder

    kwargs['separators'] = separators
    return json.dumps(*args, **kwargs)

def dump(*args, **kwargs):
    if 'compact' in kwargs and kwargs['compact']:
        kwargs['cls'] = BaseEncoderCompact
        del kwargs['compact']
    else:
        kwargs['cls'] = BaseEncoder

    kwargs['separators'] = separators
    return json.dump(*args, **kwargs)

def loads(*args, **kwargs):
    kwargs['object_hook'] = convert_obj
    return json.loads(*args, **kwargs)


import numpy as np
import soundfile as sf
from pathlib import Path
from scipy.signal import blackman, blackmanharris
import argparse
import numpy as np
import soundfile as sf
from pathlib import Path
import onnxruntime as ort
import torch
import scipy
import scipy.signal

def calcFeat(Spec, cfg):
    """compute spectral features"""

    if cfg['feattype'] == "MagSpec":
        inpFeat = np.abs(Spec)

    elif cfg['feattype'] == "LogPow":
        pmin = 10**(-12)
        powSpec = np.abs(Spec)**2
        inpFeat = np.log10(np.maximum(powSpec, pmin))

    else:
        ValueError('Feature not implemented.')

    return inpFeat

def calcSpec(y, params, channel=None):
    """compute complex spectrum from audio file"""

    fs = int(params["fs"])

    if channel is not None and (len(y.shape)>1):
        # use only single channel
        sig = sig[:,channel]

    # STFT parameters
    N_win = int(float(params["winlen"])*fs)
    if 'nfft' in params:
        N_fft = int(params['nfft'])
    else:
        N_fft = int(float(params['winlen'])*fs)
    N_hop = int(N_win * float(params["hopfrac"]))
    win = np.sqrt(np.hanning(N_win))

    Y = stft(y, N_fft, win, N_hop)

    return Y

def spec2sig(Spec, params):
    """convert spectrum to time signal"""

    # STFT parameters
    fs = int(params["fs"])
    N_win = int(float(params["winlen"])*fs)
    if 'nfft' in params:
        N_fft = int(params['nfft'])
    else:
        N_fft = int(float(params['winlen'])*fs)
    N_hop = int(N_win * float(params["hopfrac"]))
    win = np.sqrt(np.hanning(N_win))

    x = istft(Spec, N_fft, win, N_hop)

    return x

# STFT
def stft(x, N_fft, win, N_hop, nodelay=True):
	"""
	short-time Fourier transform
		x 			time domain signal [samples x channels]
		N_fft 		FFT size (samples)
		win 		window,  len(win) <= N_fft
		N_hop 		hop size (samples)
		nodelay 	[True,False]: do not introduce delay (visible windowing effects in first frames)
	"""
	# get lengths
	if x.ndim == 1:
		x = x[:,np.newaxis]
	Nx = x.shape[0]
	M = x.shape[1]
	specsize = int(N_fft/2+1)
	N_win = len(win)
	N_frames = int(np.ceil( (Nx+N_win-N_hop)/N_hop ))
	Nx = N_frames*N_hop # padded length
	x = np.vstack([x, np.zeros((Nx-len(x),M))])

	# init
	X_spec = np.zeros((specsize,N_frames,M), dtype=complex)
	win_M = np.outer(win,np.ones((1,M)))
	x_frame = np.zeros((N_win,M))
	for nn in range(0,N_frames):
		idx = int(nn*N_hop)
		x_frame = np.vstack((x_frame[N_hop:,:], x[idx:idx+N_hop,:]))
		x_win = win_M * x_frame
		X = np.fft.rfft(x_win, N_fft, axis=0)
		X_spec[:,nn,:] = X

	if nodelay:
		delay = int(N_win/N_hop - 1)
		X_spec = X_spec[:,delay:,:]

	if M==1:
		X_spec = np.squeeze(X_spec)

	return X_spec

def istft(X, N_fft, win, N_hop):
	# get lengths
	specsize = X.shape[0]
	N_frames = X.shape[1]
	if X.ndim < 3:
		X = X[:,:,np.newaxis]
	M = X.shape[2]
	N_win = len(win)

	# init
	Nx = N_hop*(N_frames-1) + N_win
	win_M = np.outer(win,np.ones((1,M)))
	x = np.zeros((Nx,M))

	for nn in range(0,N_frames):
		X_frame = np.squeeze(X[:,nn,:])
		x_win = np.fft.irfft(X_frame, N_fft, axis=0)
		x_win = x_win.reshape(N_fft,M)
		x_win = win_M * x_win[0:N_win,:]
		idx1 = int(nn*N_hop); idx2 = int(idx1+N_win)
		x[idx1:idx2,:] = x_win + x[idx1:idx2,:]

	if M==1:
		x = np.squeeze(x)

	return x

class NSnet2Enhancer(object):
    """NSnet2 enhancer class."""

    def __init__(self, modelfile, cfg=None):
        """Instantiate NSnet2 given a trained model path."""
        self.cfg = cfg
        self.frameShift = float(self.cfg['winlen'])* float(self.cfg["hopfrac"])
        self.fs = int(self.cfg['fs'])
        self.mingain = 10**(self.cfg['mingain']/20)
        self.N_win = int(float(self.cfg['winlen']) * self.fs)
        if 'nfft' in cfg:
            self.N_fft = int(self.cfg['nfft'])
        else:
            self.N_fft = self.N_win
        self.N_hop = int(self.N_fft * float(cfg["hopfrac"]))

        """load onnx model"""
        self.ort = ort.InferenceSession(modelfile)
        self.dtype = np.float32
        self.win = np.sqrt(scipy.signal.windows.hann(self.N_win, sym=False))
        self.win_buf = torch.from_numpy(self.win).float()
        L = len(self.win)
        awin = np.zeros_like(self.win)
        for k in range(0, self.N_hop):
            idx = range(k, L, self.N_hop)
            H = self.win[idx]
            awin[idx] = np.linalg.pinv(H[:,np.newaxis])
        self.awin = torch.from_numpy(awin).float()

    def enhance(self, x):
        """Obtain the estimated filter"""
        onnx_inputs = {self.ort.get_inputs()[0].name: x.astype(self.dtype)}
        out = self.ort.run(None, onnx_inputs)[0][0]
        return out

    def enhance_48khz(self, x):
        """Run model on single sequence"""
        if len(x.shape) < 2:
            x = torch.from_numpy(np.expand_dims(x, 0)).float()
        else:
            x = x.transpose()
        # x: [channels, samples]

        sig_framed = torch.nn.functional.conv1d(x.unsqueeze(1), weight=torch.diag(self.win_buf).unsqueeze(1), stride=self.N_hop).permute(0,2,1).contiguous()
        spec = torch.fft.rfft(sig_framed, n=self.N_fft)
        spec = torch.stack((spec.real, spec.imag), dim=-1)
        feat = torch.log10(torch.sum(spec**2, dim=-1).clamp_min(1e-12))
        onnx_inputs = {self.ort.get_inputs()[0].name: feat.numpy().astype(self.dtype)}
        out = self.ort.run(None, onnx_inputs)[0]
        out = torch.from_numpy(out).float()
        out_spec = out.unsqueeze(3) * spec
        out_spec_restacked = torch.complex(out_spec[:,:,:,0], out_spec[:,:,:,1]).contiguous()
        x_framed = torch.fft.irfft(out_spec_restacked, n=self.N_fft)[:,:,0:self.N_win]
        # overlapp-add using conv_transpose
        sig = torch.nn.functional.conv_transpose1d(x_framed.permute(0,2,1), weight=torch.diag(self.awin).unsqueeze(1), stride=self.N_hop).squeeze(1).contiguous()
        return sig[0]

    def __call__(self, sigIn, inFs):
        """Enhance a single Audio signal."""
        assert inFs in (16000, 48000), "Inconsistent sampling rate!"

        if inFs == 48000:
            return self.enhance_48khz(sigIn)

        inputSpec = calcSpec(sigIn, self.cfg)
        inputFeature = calcFeat(inputSpec, self.cfg)
        # shape: [batch x time x freq]
        inputFeature = np.expand_dims(np.transpose(inputFeature), axis=0)

        # Obtain network output
        out = self.enhance(inputFeature)

        # limit suppression gain
        Gain = np.transpose(out)
        Gain = np.clip(Gain, a_min=self.mingain, a_max=1.0)
        outSpec = inputSpec * Gain

        # go back to time domain
        sigOut = spec2sig(outSpec, self.cfg)

        return sigOut

#!/usr/bin/env python3
import argparse
import numpy as np
import soundfile as sf
from pathlib import Path
#import NSnet2Enhancer

"""
    Inference script for NSnet2 baseline.
"""

def main(model,input,output,fs):
    # check input path
    inPath = Path(input).resolve()
    assert inPath.exists()

    cfg = {
            'winlen'   : 0.02,
            'hopfrac'  : 0.5,
            'fs'       : fs,
            'mingain'  : -80,
            'feattype' : 'LogPow',
            'nfft'     : 320
        }

    if fs == 48000:
        cfg['nfft'] = 1024

    # Create the enhancer
    enhancer = NSnet2Enhancer(modelfile=model, cfg=cfg)

    # get modelname
    modelname = Path(model).stem

    if inPath.is_file() and inPath.suffix == '.wav':
        # input is single .wav file
        sigIn, fs = sf.read(str(inPath))
        if len(sigIn.shape) > 1:
            sigIn = sigIn[:,0]

        outSig = enhancer(sigIn, fs)

        outname = './{:s}_{:s}.wav'.format(inPath.stem, modelname)
        if output:
            # write in given dir
            outdir = Path(output)
            outdir.mkdir(exist_ok=True)
            outpath = outdir.joinpath(outname)
        else:
            # write in current work dir
            outpath = Path(outname)

        print('Writing output to:', str(outpath))
        sf.write(outpath.resolve(), outSig, fs)

    elif inPath.is_dir():
        # input is directory
        if output:
            # full provided path
            outdir = Path(output).resolve()
        else:
            outdir = inPath.parent.joinpath(modelname).resolve()

        outdir.mkdir(parents=True, exist_ok=True)
        print('Writing output to:', str(outdir))

        fpaths = list(inPath.glob('*.wav'))

        for ii, path in enumerate(fpaths):
            print(f"Processing file [{ii+1}/{len(fpaths)}]")
            print(path)

            sigIn, fs = sf.read(str(path))

            if len(sigIn.shape) > 1:
                sigIn = sigIn[:,0]

            outSig = enhancer(sigIn, fs)
            outSig1 = np.convolve(sigIn, outSig)
            outpath = outdir / path.name
            outpath = str(outpath)
            sf.write(outpath, outSig1, fs)

    else:
        raise ValueError("Invalid input path.")

model = '$CHECKPOINT DIRECTORY$'
input = '$NOISY .WAV FILE$'
output = '$OUTPUT DIRECTORY$'

fs = 16000
main(model,input,output,fs)


from machine import Pin
from time import sleep

step_pin = Pin(17, Pin.OUT)
dir_pin = Pin(19, Pin.OUT)
Canon = Pin(25, Pin.OUT)

def angle_to_steps(angle):
    if angle < 0 or angle > 360:
        raise ValueError("Angle must be between 0 and 360 degrees")
    return int(angle * 200 / 360)

def move_motor(steps, direction):
    dir_pin.value(direction)
    for i in range(steps):
        step_pin.value(1)
        sleep(0.001)
        step_pin.value(0)
        sleep(0.001)

def canon():
    if Canon.value() == 0:
        Canon.value(1)
    else:
        Canon.value(0)


import django
from django.conf import settings
from tracker.models import parse_query, Tracker
from tracker.models import Statistic, make_daily_report
from django.test import TestCase

import datetime

class UnitTestCase(TestCase):

    def test_query_string(self):

        query = "label1"
        self.assertEqual(parse_query(query), [{'lbl': 'label1'}])
        query = "label1|label2"
        self.assertEqual(parse_query(query), [{'lbl': 'label1'},
            {'lbl': 'label2'}])

        query = "label1:cat|label2"
        self.assertEqual(parse_query(query), [{'lbl': 'label1', 'cat': 'cat'},
            {'lbl': 'label2'}])

        query = "label1:cat:ele1|label2"
        self.assertEqual(parse_query(query),
            [{'lbl': 'label1', 'cat': 'cat', 'domid': 'ele1'},
            {'lbl': 'label2'}])

    def test_traker(self):

        tracker = Tracker()

        tracker.incr_labels('label1')
        self.assertEqual(tracker.labels, {'label1': {'lbl': 'label1'}})
        tracker.incr_labels('label1')
        self.assertEqual(tracker.flush_label('label1'), 2)
        self.assertEqual(tracker.flush_label('label1'), 0)
        tracker.save()

        tracker = Tracker()
        self.assertEqual(tracker.labels, {'label1': {'lbl': 'label1'}})
        tracker.reset_cache()

    def test_report(self):

        tracker = Tracker()
        tracker.incr_labels('label1:cat1:dom1')
        tracker.incr_labels('label1:cat1:dom1')

        self.assertEqual(Statistic.objects.count(), 0)

        make_daily_report()

        self.assertEqual(tracker.flush_label('label1'), 0)

        self.assertEqual(Statistic.objects.count(), 1)

        stat = Statistic.objects.all()[0]
        self.assertEqual(stat.counter, 2)
        self.assertEqual(stat.category, 'cat1')
        self.assertEqual(stat.dom_id, 'dom1')

        tracker.incr_labels('label1:cat1:dom1')
        tracker.incr_labels('label1:cat1:dom1')

        stat = Statistic.objects.all()[0]
        self.assertEqual(stat.counter, 2)

        make_daily_report()

        stat = Statistic.objects.all()[0]
        self.assertEqual(stat.counter, 4)

        tracker.reset_cache()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#########################################################################################################
#  \file mainSU2.py                                                                                #
#  \Python script for creating a .su2 structured mesh of a 4-digit NACA airfoil with circular domain.   #
#  \author Rafael Medina Nogueron                                                                       #
#  \Instituto Politecnico Nacional                                                                      #
#  \rafaelmn@ipn.mx                                                                                #
#  \version 1.0.                                                                                #
#########################################################################################################
import numpy as np

#N = 31
#Ny = 600
def tetraNodos(Ny, N):
    """
    structured mesh
    Ny = Number of coordinates
    N = Number of radial nodes
    """
    nodSU = np.zeros([Ny-N+1, 6])
    cont = 0
    for i in range(0, Ny-N+1):
        nodSU[i,0] = 9
        nodSU[i,1] = i
        nodSU[i,2] = i + 1
        nodSU[i,3] = i + N
        nodSU[i,4] = i + N - 1
        cont += 1
        if cont == N - 1:
          nodSU[i,1] = i
          nodSU[i,2] = i - N + 2
          nodSU[i,3] = i + 1
          nodSU[i,4] = i + N - 1
          cont = 0
        nodSU[i,5] = i # Columna para enumerar las celdas de la malla
    return nodSU


# -*- coding: utf-8 -*-
"""Setup file for qlp
"""
__author__ = "None"
__version__ = "0.1.0"

from os import path

from setuptools import setup, find_packages

CWD = path.abspath(path.dirname(__file__))

with open(path.join(CWD, "README.md"), encoding="utf-8") as inp:
    LONG_DESCRIPTION = inp.read()

with open(path.join(CWD, "requirements.txt"), encoding="utf-8") as inp:
    REQUIREMENTS = [el.strip() for el in inp.read().split(",")]

setup(
    name="qlp",
    version=__version__,
    description=None,
    long_description=LONG_DESCRIPTION,
    long_description_content_type="text/markdown",
    url=None,
    author=__author__,
    author_email=None,
    keywords=[],
    packages=find_packages(exclude=["docs", "tests"]),
    install_requires=REQUIREMENTS,
)


import io

class DicionarioTags:
    def __init__(self):
        self.__arquivo = io.open("tags.txt", "r", encoding='utf8')
        self.__arquivo_a = io.open("tags.txt", "a", encoding="utf8")

    def mostra_todas_as_tags(self):
        with self.__arquivo as arquivo:
            for linha in arquivo:
                print(linha)

    def adiciona_tag(self, tag):
        funcao = str(input("Digite a função desta tag: "))
        with self.__arquivo_a as arquivo:
            arquivo.write("\n")
            arquivo.write(f"tag: {tag} --->>> Função: {funcao}")
            print(f"Tag: '{tag}' adicionada com sucesso!!")

    def busca_tag(self, tag):
        with self.__arquivo as arquivo:
            for linha in arquivo:
                if tag in linha.split(" --->>>")[0]:
                    busca = linha
                    print(busca)

    def busca_funcao(self, palavra_chave):
        palavra_chave = str(palavra_chave).lower()
        with self.__arquivo as arquivo:
            for linha in arquivo:
                if palavra_chave in linha:
                    busca_funcao = linha
                    print(busca_funcao)


import numpy as np
import time
import gym
from ppo import PPO
env = gym.make("CartPole-v1", render_mode="human")
ppo = PPO(input_shape=4,
            output_shape=2,
            hidden_shape=(64, 64,),
            actor_learning_rate=3e-4,
            critic_learning_rate=1e-3,
            epochs=10,
            batch_size=100,
            name=f"PPO-{int(time.time())}")

ppo.load_weights("weights/PPO-1658512915/PPO-1658512915-1658514231-actor.hdf5", None)
episodes = 10
for _ in range(episodes):
    state, done, result = env.reset(seed=42), False, 0
    while not done:

        action, _, _ = ppo.policy(state)

        next_state, _, done, _ = env.step(action)

        state = next_state

env.close()


# select students with 2nd high score

def second_highest(students):
    if len(students) < 2:
        print("Need at least 2 students!")
    else:
        sorted_score = sorted([score[1] for score in students], reverse = True)
        second_score = sorted_score[1]
        for student in students:
            if student[1] == second_score:
                print(student[0])

list1 = [['Jerry', 100], ['Justin', 84], ['Tom', 90], ['Allen', 92], ['Harsh', 90]]
second_highest(list1)


if __name__== '__main__':

    # un año es bisiesto si y solo si es divisible por 4
    # ademas debe ser divisible por 100 y por 400!
    # el primer año que se establecio fue el 1582 por el Papa GregorioXII

    def bisiesto(a):

        bisiesto = False

        if a > 1581 :
            if a % 4 == 0 :
                if a % 100 != 0: bisiesto = True
                else:
                    if a % 400 == 0: bisiesto = True
        else:
            print(" el primer año que se establecio como bisiesto fue el 1582 por el Papa GregorioXII ")

        return bisiesto

    while True:
        a = int(input("Introduzca el año que desea verificar mayor a 1582:  "))
        check = bisiesto(a)

        if check == True :
            print("El año: [" + str(a) + "] Es bisiesto")
        else:
            if a >= 1582: print("El año: [" + str(a) + "] NO es bisiesto")


import os
import pickle
from typing import Optional

import optuna
from tqdm import tqdm
from stable_baselines3.common.callbacks import BaseCallback, EvalCallback
from stable_baselines3.common.vec_env import VecEnv

class TqdmCallback(BaseCallback):
    """ Callback for utilizing tqdm
    ref: https://github.com/hill-a/stable-baselines/issues/297#issuecomment-877789711
    """
    def __init__(self):
        super().__init__()
        self.progress_bar = None

    def _on_training_start(self):
        self.progress_bar = tqdm(total=self.locals['total_timesteps'])

    def _on_step(self):
        self.progress_bar.update(1)
        return True

    def _on_training_end(self):
        self.progress_bar.close()
        self.progress_bar = None

class TrialEvalCallback(EvalCallback):
    """
    Callback used for evaluating and reporting a trial.
    """

    def __init__(
        self,
        eval_env: VecEnv,
        trial: optuna.Trial,
        n_eval_episodes: int = 5,
        eval_freq: int = 10000,
        deterministic: bool = True,
        verbose: int = 0,
        best_model_save_path: Optional[str] = None,
        log_path: Optional[str] = None,
    ):

        super().__init__(
            eval_env=eval_env,
            n_eval_episodes=n_eval_episodes,
            eval_freq=eval_freq,
            deterministic=deterministic,
            verbose=verbose,
            best_model_save_path=best_model_save_path,
            log_path=log_path,
        )
        self.trial = trial
        self.eval_idx = 0
        self.is_pruned = False

    def _on_step(self) -> bool:
        if self.eval_freq > 0 and self.n_calls % self.eval_freq == 0:
            super()._on_step()
            self.eval_idx += 1
            # report best or report current ?
            # report num_timesteps or elasped time ?
            self.trial.report(self.last_mean_reward, self.eval_idx)
            # Prune trial if need
            if self.trial.should_prune():
                self.is_pruned = True
                return False
        return True

class LickingTrackerCallback(BaseCallback):
    """ Callback for tracking a licking behavior """
    def __init__(self, env, save_path):
        super().__init__()
        self.env = env
        self.save_path = save_path

    def _on_step(self):
        return True

    def _on_training_end(self):
        with open(os.path.join(self.save_path, "lick_timing.pkl"), "wb") as f1:
            pickle.dump(self.env.get_attr("lick_timing")[0], f1)

        with open(os.path.join(self.save_path, "spout_timing.pkl"), "wb") as f2:
            pickle.dump(self.env.get_attr("spout_timing")[0], f2)

        with open(os.path.join(self.save_path, "actions.pkl"), "wb") as f3:
            pickle.dump(self.env.get_attr("actions")[0], f3)

        with open(os.path.join(self.save_path, "lick_pos.pkl"), "wb") as f4:
            pickle.dump(self.env.get_attr("lick_pos")[0], f4)

        with open(os.path.join(self.save_path, "move_and_lick_pos.pkl"), "wb") as f5:
            pickle.dump(self.env.get_attr("move_and_lick_pos")[0], f5)

        with open(os.path.join(self.save_path, "trial_start_timing.pkl"), "wb") as f6:
            pickle.dump(self.env.get_attr("trial_start_timing")[0], f6)

        with open(os.path.join(self.save_path, "shockzone_start_timing.pkl"), "wb") as f7:
            pickle.dump(self.env.get_attr("shockzone_start_timing")[0], f7)

        with open(os.path.join(self.save_path, "shockzone_end_timing.pkl"), "wb") as f8:
            pickle.dump(self.env.get_attr("shockzone_end_timing")[0], f8)

        with open(os.path.join(self.save_path, "shock_timing.pkl"), "wb") as f9:
            pickle.dump(self.env.get_attr("shock_timing")[0], f9)

        with open(os.path.join(self.save_path, "us_type.pkl"), "wb") as f10:
            pickle.dump(self.env.get_attr("us_type")[0], f10)

        with open(os.path.join(self.save_path, "time_step.pkl"), "wb") as f11:
            pickle.dump(self.env.get_attr("time_step")[0], f11)


"""
"""

import operator as op
import collections as coll

from pymotifs import core
from pymotifs import models as mod
from pymotifs.utils import row2dict

from pymotifs.nr import representatives as reps

class Reporter(core.Reporter):
    allow_no_data = True
    headers = [
        'Handle',
        'Version',
        'Release',
        'Release Index',
        'Method',
        'Representative',
        'Change Count'
    ]

    def release_index(self, release_id):
        with self.session() as session:
            release = session.query(mod.NrReleases).get(release_id)
            if not release:
                raise core.InvalidState("Unknown release %s" % release_id)
            return release.index

    def as_release_name(self, nr_release):
        return 'v' + nr_release

    def release_order(self, start, stop):
        start_index = self.release_index(start)
        stop_index = self.release_index(stop)
        with self.session() as session:
            query = session.query(mod.NrReleases.nr_release_id).\
                filter(mod.NrReleases.index >= start_index).\
                filter(mod.NrReleases.index <= stop_index).\
                order_by(mod.NrReleases.index)
            return [self.as_release_name(r.nr_release_id) for r in query]

    def possible_classes(self, start, stop, resolution):
        start_index = self.release_index(start)
        stop_index = self.release_index(stop)
        rel = mod.NrReleases
        with self.session() as session:
            query = session.query(mod.NrClasses.handle,
                                  mod.NrClasses.nr_release_id,
                                  mod.NrClasses.nr_class_id,
                                  ).\
                join(rel,
                     mod.NrClasses.nr_release_id == rel.nr_release_id).\
                filter(rel.index >= start_index).\
                filter(rel.index <= stop_index).\
                filter(mod.NrClasses.resolution == resolution).\
                order_by(mod.NrClasses.nr_release_id)
            return [row2dict(r) for r in query]

    def common_classes(self, start, stop, resolution):
        releases = self.release_order(start, stop)
        releases = set(r.replace('v', '') for r in releases)
        possible = self.possible_classes(start, stop, resolution)
        found = coll.defaultdict(set)
        for entry in possible:
            found[entry['handle']].add(entry['nr_release_id'])

        common = set()
        for handle, present_in in found.items():
            if present_in == releases:
                common.add(handle)

        return sorted(e['nr_class_id'] for e in possible if e['handle'] in common)

    def chains(self, class_id):
        with self.session() as session:
            chains = mod.NrChains
            ife = mod.IfeInfo
            pdbs = mod.PdbInfo
            classes = mod.NrClasses
            query = session.query(chains.nr_release_id,
                                  classes.name,
                                  classes.handle,
                                  classes.version,
                                  classes.version,
                                  ife.ife_id.label('id'),
                                  ife.bp_count.label('bp'),
                                  ife.length,
                                  pdbs.resolution,
                                  pdbs.experimental_technique.label('method'),
                                  ).\
                join(ife, ife.ife_id == chains.ife_id).\
                join(classes, classes.nr_class_id == chains.nr_class_id).\
                join(pdbs, pdbs.pdb_id == ife.pdb_id).\
                filter(classes.nr_class_id == class_id)

            return [row2dict(r) for r in query]

    def methods(self, methods='*', **kwargs):
        if methods == '*':
            return reps.known()
        selected = []
        methods = set(methods)
        for name, klass in reps.known():
            if name in methods:
                selected.append((name, klass))
        return selected

    def get_representatives(self, grouped, class_id, **kwargs):
        chains = self.chains(class_id)
        for method, klass in self.methods(**kwargs):
            finder = klass(self.config, self.session)
            rep = finder(chains)
            rel_name = self.as_release_name(rep['nr_release_id'])
            handle = rep['handle']
            version = rep['version']
            key = (handle, method)
            grouped[key][rel_name] = (version, rep['id'])

    def finalize(self, ordering, grouped):
        data = []
        for (handle, method), releases in grouped.items():
            last_entry = None
            entries = []
            for index, release in enumerate(ordering):
                count = 0
                if release not in releases:
                    self.logger.error("Missing release %s for %s",
                                      release, handle)
                    entries = []
                    break
                version, curr = releases[release]
                if index:
                    count = last_entry['Change Count']
                    _, prev = releases[ordering[index - 1]]
                    if prev != curr:
                        count += 1

                last_entry = {
                    'Handle': handle,
                    'Version': version,
                    'Method': method,
                    'Release': release,
                    'Release Index': index,
                    'Representative': curr,
                    'Change Count': count,
                }
                entries.append(last_entry)
            data.extend(entries)
        key = op.itemgetter('Release Index', 'Handle', 'Method')
        return sorted(data, key=key)

    def data(self, entry, **kwargs):
        start, stop, resolution = entry
        grouped = coll.defaultdict(dict)
        ordering = self.release_order(start, stop)
        for class_id in self.common_classes(start, stop, resolution):
            self.get_representatives(grouped, class_id)
        return self.finalize(ordering, grouped)


from multiprocessing import Process
import os

import pandas as pd
from PIL import Image
from tqdm import tqdm

from hpa.data import load_channels

class DataTypeChanger:
    def __init__(self, data_dir, pid):
        self.data_dir = data_dir
        self.pid = pid

    def __call__(self, ids):
        bad_ids = []
        if self.pid == 0:
            generator = tqdm(ids, desc='Process 0')
        else:
            generator = ids
        for img_id in generator:
            try:
                channels = load_channels(img_id, self.data_dir)
                for color, channel in channels.items():
                    img = Image.fromarray(channel)
                    output_path = os.path.join(self.data_dir, f'{img_id}_{color}.png')
                    img.save(output_path)
            except:
                bad_ids.append(img_id)
        with open(f'/home/mchobanyan/bad_ids_{self.pid}.txt', 'w') as file:
            for i in bad_ids:
                file.write(str(i) + ',')

if __name__ == '__main__':

    NUM_JOBS = 2
    DATA_INDEX = '/home/mchobanyan/data/kaggle/hpa-single-cell/full-train-index.csv'
    DATA_DIR = '/home/mchobanyan/data/kaggle/hpa-single-cell/misc/public-hpa/data2/'

    data_idx = pd.read_csv(DATA_INDEX)
    data_idx = data_idx.loc[data_idx['Source'] == 'external']
    data_idx = data_idx.reset_index(drop=True)

    chunk_size = int(len(data_idx) / NUM_JOBS) + 1
    chunks = [data_idx['ID'].iloc[i:i + chunk_size].values.tolist() for i in range(0, len(data_idx), chunk_size)]

    processes = []
    for pid, chunk in enumerate(chunks):
        p = Process(target=DataTypeChanger(DATA_DIR, pid), args=(chunk,))
        processes.append(p)
        p.start()

    for p in processes:
        p.join()


from flask import Flask, request, jsonify
from flask_cors import CORS, cross_origin
import base64
import json
from flask_sqlalchemy import SQLAlchemy
import math
import numpy as np
from env_variables import *

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://%s:%s@%s:%s/%s' % (
  MYSQL_USER,
  MYSQL_PASSWORD,
  MYSQL_HOST,
  MYSQL_PORT,
  MYSQL_DATABSASE
)

db = SQLAlchemy(app)

CORS(app, resources={r'/api/*': {'origins': '*'}})

@app.route('/num_identifier', methods=['POST'])
@cross_origin()
def num_identifier():
  if request.method == 'POST':
    data = request.get_json()
    image_data = json.loads(data['image'])
    y = data['value']

    if y is not None:
      add_training_data(image_data, y)

    image_data = modify_training_data(image_data)
    result = determine_number(image_data)
    std = np.std(result)
    for i in range(0, 10):
      print(i, result[i] / std)
      i += 1

    print('RESULT', result.index(max(result)))

    return jsonify({'prediction': result.index(max(result))})

def add_training_data(image_data, value):
  db.engine.execute(
    'INSERT INTO Number_Training_Data (image, value) \
     VALUES ("%(image_data)s", %(value)d)'
    % {'image_data': image_data, 'value': value}
  )

#  Returns formatted training data and associated Y values
def get_training_data():
  training_data_json = db.engine.execute('SELECT image, value FROM Number_Training_Data')
  training_data_json = training_data_json.fetchall()
  training_data = []
  y = []
  for x in range(0, len(training_data_json)):
    img_data = modify_training_data(json.loads(training_data_json[x]['image']))

    # training_data.append([1] + json.loads(training_data_json[x]['image']))
    training_data.append(img_data)
    y.append([training_data_json[x]['value']])

  return {
    'training_data': np.mat(training_data),
    'y': np.mat(y)
  }

# Add new features to existing pixel data
def modify_training_data(array):
  return [1] + new_features(array) # + array

def expand_stroke(array):
  for pixel in array:
    row = pixel / 20
    col = pixel % 20

    if (array[pixel] is 1):
      if (row is not 0):
        array[pixel - 1] = 2 if array[pixel - 1] is 0 else array[pixel - 1]
      if (row is not 19):
        array[pixel + 1] = 2 if array[pixel + 1] is 0 else array[pixel + 1]
      if (col is not 0):
        array[pixel - 20] = 2 if array[pixel - 20] is 0 else array[pixel - 20]
      if (col is not 19):
        array[pixel + 20] = 2 if array[pixel + 20] is 0 else array[pixel + 20]

  for pixel in array:
    array[pixel] = 1 if pixel > 0 else 0

  return array

# Generates various calculated features including in an array:
#   Total pixels: # pixels colored in the image
#   Row count: # rows that have at least one colored pixel
#   Column count: # columns that have at least one colored pixel
#   Row sums (one for each row): # of colored pixels in each row
#   Column sums (one for each column): # of colored pixels in each column
#   Top distribution: % pixels in top half of number pixel boundaries
#   Bottom distribution: % pixels in bottom half of number pixel boundaries
#   Left distribution: % pixels in left half of number pixel boundaries
#   Right distribution: % pixels in right half of number pixel boundaries
#   Top left  distribution: % pixels in top left quarter of number pixel boundaries
#   Top right distribution: % pixels in top right quarter of number pixel boundaries
#   Bottom left distribution: % pixels in bottom left quarter of number pixel boundaries
#   Bottom right distribution: % pixels in bottom right quarter of number pixel boundaries

def new_features(img_array):
  # represent the number density (sum of 1s) in each row/col
  row_sums = [0] * 20
  col_sums = [0] * 20

  # represent the count of rows/columns with at least one marked pixel
  row_count = 0
  col_count = 0

  total_pixels = 0

  for pixel in range(0, len(img_array)):
    row = pixel / 20
    col = pixel % 20
    row_sums[row] += img_array[pixel]
    col_sums[col] += img_array[pixel]
    total_pixels += img_array[pixel]

  total_pixels = total_pixels if total_pixels > 0 else 1

  for i in range (0, 19):
    row_count += 1 if row_sums[i] > 0 else 0
    col_count += 1 if col_sums[i] > 0 else 0

  left_bound = None
  right_bound = None
  top_bound = None
  bottom_bound = None

  # Get boundaries of number image based on colored pixels
  for i in range(0, 20):
    if row_sums[i] > 0:
      if left_bound is None:
        left_bound = i
      right_bound = i

    if col_sums[i] > 0:
      if top_bound is None:
        top_bound = i
      bottom_bound = i

  left_bound = left_bound if left_bound else 0
  right_bound = right_bound if right_bound else 0
  top_bound = top_bound if top_bound else 0
  bottom_bound = bottom_bound if bottom_bound else 0
  horizontal_mid_index = (left_bound + right_bound) / 2
  vertical_mid_index = (top_bound + bottom_bound) / 2

  top_left_count = 0
  top_right_count = 0
  bottom_left_count = 0
  bottom_right_count = 0

  left_count = 0
  right_count = 0
  top_count = 0
  bottom_count = 0

  # Determine pixel distribution based on number image dimesions
  for pixel in range(0, len(img_array)):
    row = pixel / 20
    col = pixel % 20

    if row < horizontal_mid_index and col < vertical_mid_index:
      top_left_count += img_array[pixel]
    elif row < horizontal_mid_index and col > vertical_mid_index:
      top_right_count += img_array[pixel]
    elif row > horizontal_mid_index and col < vertical_mid_index:
      bottom_left_count += img_array[pixel]
    elif row > horizontal_mid_index and col > vertical_mid_index:
      bottom_right_count += img_array[pixel]

    if row < horizontal_mid_index:
      top_count += img_array[pixel]
    elif row > horizontal_mid_index:
      bottom_count += img_array[pixel]
    if col < vertical_mid_index:
      left_count += img_array[pixel]
    elif col > vertical_mid_index:
      right_count += img_array[pixel]

  pixel_distribution = [
    top_count / float(total_pixels),
    bottom_count / float(total_pixels),
    left_count / float(total_pixels),
    right_count / float(total_pixels),
    top_left_count / float(total_pixels),
    top_right_count / float(total_pixels),
    bottom_left_count / float(total_pixels),
    bottom_right_count / float(total_pixels),
    top_count / float(bottom_count if bottom_count else 1),
    left_count / float(right_count if right_count else 1)
  ]

  return [total_pixels, row_count, col_count] + row_sums + col_sums + pixel_distribution

# Return theta values for each Y value
def get_thetas():
  output = []

  for num in range(0, 10):
    thetas = db.engine.execute('SELECT coefficient FROM Number_Thetas WHERE value = %d ORDER BY feature ASC' % num)
    thetas = thetas.fetchall()
    output.append(np.mat(thetas))

  return output

# X: training example inputs
# y: training example outputs
# theta: regression coefficients
def logistic_cost(X, y, theta, lam = 0):
  # m represents the number of training examples
  m = y.shape[0]
  square = np.vectorize(lambda x: x*x)
  sigmoid = np.vectorize(lambda z: 1 / (1 + math.exp(-1 * z)))

  h = sigmoid((X * theta))

  cost = np.sum(np.multiply(-y, (np.log(h))) - np.multiply((1 - y), np.log(1 - h))) / m
  regularization_term = lam * np.sum(np.square(theta))

  return cost + regularization_term

# X: training example inputs
# y: training example outputs
# theta: regression coefficients
# alpha: learning rate
# num_iters: number if iterations of GD to run
def gradient_descent(num, X, y, theta, alpha = 0.01, num_iters = 400):
  # m represents the number of training examples
  m = y.shape[0]
  sigmoid = np.vectorize(lambda z: 1 / (1 + math.exp(-1 * z)))
  for iter in range(0, num_iters):
    # Run gradient descent to optimize thetas for regression
    h = sigmoid(X * theta)

    theta = theta - sum(np.multiply((h-y), X), 0).T * (alpha / m)
    print(num, iter, logistic_cost(X, y, theta))

  for feature in range(0, theta.shape[0]):
    db.engine.execute(
      'REPLACE INTO Number_Thetas (feature, coefficient, value) \
      VALUES (%(feature)d, %(coefficient)f, %(value)d)' %
      {'feature': feature, 'coefficient': theta[feature], 'value': num})

# Sigmoid returns a value between 0 and 1
# When z = 0, should return 0.5
# def sigmoid(z):
#   return 1 / (1 + math.exp(-1 * z))

# Returns inputs and outputs for training and cross validation data
def get_training_and_cv_data(use_3rd_party_training_data = 0):
  data = get_training_data()

  # Use 3rd party data
  if use_3rd_party_training_data:
    # If we use third party data for training, we use all of our personal data for cross validation
    personal_data_training_size = 0
    convert_grayscale_to_binary = np.vectorize(lambda y: 1 if y >= 0.5 else 0)
    # 3rd party data uses Y label of 10 for zero since it plays nicely in Octave
    # This function simply changes the label to 0 to make it more consistent
    replace_10_with_0 = np.vectorize(lambda y: 0 if y == 10 else y)
    data_3rd_party = sio.loadmat('data.mat')
    training_X = convert_grayscale_to_binary(data_3rd_party['X'])
    training_y = replace_10_with_0(data_3rd_party['y'])
  # Use personal data
  else:
    personal_data_training_size = len(data['training_data']) * 0.8
    training_X = data['training_data'][0: personal_data_training_size]
    training_y = data['y'][0: personal_data_training_size]

  cross_validation_X = data['training_data'][personal_data_training_size: len(data['training_data'])]
  cross_validation_y = data['y'][personal_data_training_size: len(data['training_data'])]

  return {
    'training_X': training_X,
    'training_y': training_y,
    'cross_validation_X': cross_validation_X,
    'cross_validation_y': cross_validation_y
  }

@app.route('/run_regression', methods=['GET'])
@cross_origin()
def run_regression():
  data = get_training_and_cv_data(0)
  theta = [[0]] * data['training_X'].shape[1]
  theta = np.mat(theta)
  y_convert_function = np.vectorize(lambda y, num: 1 if y == num else 0)

  for num in range(0, 10):
    gradient_descent(num, data['training_X'], y_convert_function(data['training_y'], num), theta, 0.01, 800)
  check_results(data['cross_validation_X'], data['cross_validation_y'])
  return 'success'

sigmoid = np.vectorize(lambda z: 1 / (1 + math.exp(-1 * z)))

def check_results(cv_X, cv_y):
  # data = get_training_data()
  theta = get_thetas()

  output = []
  for cv_data_row in range(0, cv_X.shape[0]):
    X_data = cv_X[cv_data_row]
    cur_results =  []
    for i in range(0, len(theta)):
      cur_results.append(sigmoid(X_data * theta[i]).A1[0])
    output.append([cv_y[cv_data_row].A1[0], cur_results])

  correct = 0
  total = 0
  for i in range(0, len(output)):
    total += 1
    if (output[i][0] == output[i][1].index(max(output[i][1]))):
      correct += 1

  print('correct', correct)
  print('total', total)
  print('accurracy %f' % (correct/float(total)))

def determine_number(X):
  theta = get_thetas()

  X = np.mat(X)
  results = []
  for i in range(0, len(theta)):
    results.append(sigmoid(X * theta[i]).A1[0])

  return results

# check_results()

if __name__ == '__main__':
  if PYTHON_ENV == 'development':
    app.run(debug=True)
  else:
    app.run(host= '0.0.0.0')


import itertools

import gensim
import nltk
import numpy as np

class Word2Vec(gensim.models.word2vec.Word2Vec):
    def __init__(self, *args, **kwargs):
        kwargs['null_word'] = True
        super().__init__(*args, **kwargs)
        self.null_word = '\0' # from internal gensim model

    # We normalize based on word frequencies
    def normalize(self):
        # null_word does not have freq counts, so set one before normalization
        self.vocab[self.null_word].count = self.min_count
        self.init_sims(replace=True)
        counts = np.zeros(len(self.vocab))
        for word in self.vocab.values():
            counts[word.index] = word.count
        counts = counts.reshape((-1,1))
        # log because softmax will recover the frequency proportions
        self.syn0 *= np.log(counts)

    def one_hot(self, words, sent_length=None):
        if sent_length == None:
            sent_length = len(words)
        ret = np.zeros((sent_length, self.syn0.shape[0]))
        for i, word in enumerate(words):
            if word in self.vocab:
                ret[i][self.vocab[word].index] = 1
            else:
                ret[i][self.vocab[self.null_word].index] = 1
        return ret

    def words_to_indices(self, words, sent_length=None):
        if sent_length == None:
            sent_length = len(words)
        ret = np.zeros(sent_length)
        for i, word in enumerate(words):
            if word in self.vocab:
                ret[i] = self.vocab[word].index
            else:
                ret[i] = self.vocab[self.null_word].index
        return ret

    def embedding_matrix(self):
        return self.syn0

    def index_to_word_map(self):
        out_dict = {}
        for word in self.vocab.keys():
            out_dict[self.vocab[word].index] = word
        return out_dict

    # Calculate loss given word vectors and target words.
    # vectors is an array or tensor with size (n,d) of n word vectors.
    # words is a list of n words to compute the loss against.
    # Returns a tensor with size (n,).
    """
    def loss(self, vectors, words):
        dist = tf.matmul(self.syn0, vectors, transpose_b=True)
        dist = tf.transpose(dist)
        words = [self.vocab[word].index
                if word in self.vocab
                else self.vocab[self.null_word].index
                for word in words]
        return tf.nn.sparse_softmax_cross_entropy_with_logits(dist, words)
    """


"""
extract, prune and process variables
"""

import json
import logging
import re
from subprocess import check_output, STDOUT
from tempfile import TemporaryDirectory
from pathlib import Path

from lxml import etree

from baleen.utils import derive_path

log = logging.getLogger(__name__)

PRUNE_OPTIONS = '--unique'
RESUME_EXTRACT = False
RESUME_OFFSET = False
RESUME_PREP = False
RESUME_PRUNE = False

def extract_vars(extract_vars_exec, trees_dir, vars_dir, resume=RESUME_EXTRACT):
    """
    Extract variables in change/increase/decrease events
    """
    resume_option = '--resume' if resume else ''
    cmd = "{} {} {} {}".format(extract_vars_exec, resume_option, trees_dir,
                               vars_dir)
    log.info("\n" + cmd)
    # universal_newlines=True is passed so the return value will be a string
    # rather than bytes
    ret = check_output(cmd, shell=True, stderr=STDOUT, universal_newlines=True)
    log.info('\n{}'.format(ret))

def preproc_vars(trans_exec, trans_fname, in_vars_dir, out_vars_dir,
                 tmp_dir=None, resume=RESUME_PREP):
    """
    Preprocess variables

    Deletes determiners (DT), personal/possessive pronouns (PRP or PRP$) and
    list item markers (LS or LST).
    """
    # TODO 3: resume only works if tmp_dir is given
    if not tmp_dir:
        tmp = TemporaryDirectory()
        tmp_dir = tmp.name

    parts = [trans_exec, '--tag "#prep"']
    if resume:
        parts.append('--resume')
    parts += [in_vars_dir, tmp_dir, trans_fname]
    cmd = ' '.join(parts)
    log.info('\n' + cmd)
    # universal_newlines=True is passed so the return value will be a string
    # rather than bytes
    ret = check_output(cmd, shell=True, universal_newlines=True)
    log.info('\n{}'.format(ret))

    Path(out_vars_dir).mkdir(parents=True, exist_ok=True)

    for in_vars_fname in Path(tmp_dir).glob('*.json'):
        out_vars_fname = derive_path(in_vars_fname, new_dir=out_vars_dir)

        if resume and out_vars_fname.exists():
            log.info('skipping existing preprocessed file {}'.format(out_vars_fname))
            continue

        records = json.load(in_vars_fname.open())
        # Remove any var that has descendents
        # (i.e. from which a node was deleted)
        # Also remove empty vars or "NP" vars
        out_vars_records = [rec for rec in records
                            if rec['subStr'] not in ['', 'NP'] and
                            'descendants' not in rec]
        if out_vars_records:
            log.info('writing to preprocessed variable file {}'.format(out_vars_fname))
            json.dump(out_vars_records, out_vars_fname.open('w'), indent=0)
        else:
            log.info('skipping empty preprocessed variable file {}'.format(out_vars_fname))

def prune_vars(prune_vars_exec, in_vars_dir, out_vars_dir, resume=False,
               options=PRUNE_OPTIONS):
    """
    Prune variables in change/increase/decrease events
    """
    parts = [prune_vars_exec]

    if resume:
        parts.append('--resume')

    if options:
        parts.append(options)

    parts += [in_vars_dir, out_vars_dir]

    cmd = ' '.join(parts)
    # universal_newlines=True is passed so the return value will be a string
    # rather than bytes
    ret = check_output(cmd, shell=True, stderr=STDOUT, universal_newlines=True)
    log.info('\n{}'.format(ret))

def add_offsets(vars_dir, scnlp_dir, resume=RESUME_OFFSET):
    """
    Add character offsets to extracted variables

    Adds charOffsetBegin and charOffesetEnd fields to JSON records for
    variables. Offsets are absolute w.r.t. to the input text (abstract) to
    SCNLP.

    Parameters
    ----------
    vars_dir : str
        directory of files with extracted variables in json format
    scnlp_dir : str or Path
        directory containing scnlp output in xml format
    resume: bool
       resume process, skipping files that already have offsets
    """
    scnlp_dir = Path(scnlp_dir)

    for var_fname in Path(vars_dir).glob('*.json'):
        records = json.load(var_fname.open())

        try:
            rec = records[0]
        except IndexError:
            log.info('skipping file without extracted variables: {}'.format(var_fname))
            continue

        if (resume and records and
                rec.get('charOffsetBegin') and
                rec.get('charOffsetEnd')):
            log.info('skipping file with existing offsets: {}'.format(var_fname))
            continue

        scnlp_fname = scnlp_dir / Path(rec['filename']).with_suffix('.xml')
        xml_tree = etree.parse(scnlp_fname.open())
        sentences_elem = xml_tree.find('.//sentences')
        tree_number = None
        node2indices = []

        for rec in records:
            if rec['treeNumber'] != tree_number:
                tree_number = rec['treeNumber']
                sent_elem = sentences_elem[int(tree_number) - 1]
                tokens_elem = sent_elem.find('tokens')
                parse = sent_elem.find('parse').text
                node2indices = parse_pstree(parse, tokens_elem)

            indices = node2indices[rec['nodeNumber']]
            rec['charOffsetBegin'], rec['charOffsetEnd'] = indices

        with var_fname.open('w') as outf:
            log.info('adding offsets to file: {}'.format(var_fname))
            json.dump(records, outf, indent=0)

def parse_pstree(parse, tokens_elem):
    """
    Traverse phrase structure tree in labeled bracket format, storing
    character offsets for each node in the node2offsets dict. The parse is a
    labeled brackets strings as under <parse>...</parse> in the XML output by
    Stanford CoreNLP. The corresponding tokens are the elements contained in
    <tokens>...</tokens>.
    """
    # Construct a regexp that will tokenize the string.
    open_b, close_b = '()'
    open_pattern, close_pattern = (re.escape(open_b), re.escape(close_b))
    node_pattern = '[^\s%s%s]+' % (open_pattern, close_pattern)
    # leaf_pattern = '[^\s%s%s]+' % (open_pattern, close_pattern)
    # Modified original leaf pattern from NLTK to accommodate case like
    # (NP (NP (DT the) (CD 8 1/2) (NN day) (NN period))
    # where the leaf "8 1/2" contains whitespace
    leaf_pattern = '[^\s%s%s]+[^%s%s]*' % (open_pattern, close_pattern,
                                           open_pattern, close_pattern)
    symbol_re = re.compile('%s\s*(%s)?|%s|(%s)' % (
        open_pattern, node_pattern, close_pattern, leaf_pattern), )

    # Build an element tree to conveniently propagate offsets from child to
    # parent nodes
    parent = etree.Element('__ROOT__')
    token_count = 0

    for match in symbol_re.finditer(parse):
        symbol = match.group()
        # Beginning of a tree/subtree
        if symbol[0] == open_b:
            # create non-terminal node
            child = etree.Element('node')
            parent.append(child)
            parent = child
        # End of a tree/subtree
        elif symbol == close_b:
            parent = parent.getparent()
            # Propagate character offsets upwards
            parent.set('start', parent[0].get('start'))
            parent.set('end', parent[-1].get('end'))
        # Leaf node
        else:
            # Copy offsets from corresponding token
            token = tokens_elem[token_count]
            token_count += 1
            start, end = token[2].text, token[3].text
            # Propagate character offsets upwards
            parent.set('start', start)
            parent.set('end', end)
            # create terminal node
            child = etree.Element('node', start=start, end=end)
            parent.append(child)

    # Finally map node numbers to character offsets
    return dict((n, (int(elem.get('start')), int(elem.get('end'))))
                for n, elem in enumerate(parent.iter()))


import json
import os
import socket
import logging
from typing import Optional
from confluent_kafka import Message, Consumer
from kafka_client import KafkaClient

class KafkaConsumer(KafkaClient):
    """
    The KafkaConsumer class is used to create consumers for Apache Kafka.

    Attributes:
        config_json (dict): Configuration parameters for the consumer, loaded from a JSON file.
        consumer (Consumer): The Kafka consumer object.
    """
    def __init__(self, config_path: str, group_id: str, topic: str):
        """
        The constructor for KafkaConsumer class.

        Args:
            config_path (str): The path to the JSON configuration file.
            group_id (str): The group ID of the consumer.
            topic (str): The topic to subscribe to.
        """
        self.config_json = self._get_config(config_path)
        self.consumer = self._create_consumer(self.config_json, group_id, topic)

    def _create_consumer(self, config_json: dict, group_id: str, topic: str) -> Consumer:
        """
        Create a Kafka consumer using the configuration parameters.

        Args:
            config_json (dict): The configuration parameters.
            group_id (str): The group ID of the consumer.
            topic (str): The topic to subscribe to.

        Returns:
            Consumer: The Kafka consumer object, or None if an error occurred.
        """
        try:
            consumer = Consumer({
              "bootstrap.servers": config_json["KAFKA_BROKER"],
              "group.id": group_id,
              "client.id": socket.gethostname(),
              "isolation.level": config_json["ISOLATION_LEVEL"],
              "default.topic.config":{
                        "auto.offset.reset": config_json["AUTO_OFFSET_RESET"],
                        "enable.auto.commit": False
                }

            })
            consumer.subscribe([topic])
        except Exception as e:
            logging.exception("Can't open consumer" + str(e))
            consumer = None

        return consumer

    def close(self) -> None:
        """
        Close the consumer.
        """
        self.consumer.close()

    def poll(self) -> Optional[Message]:
        """
        Poll the consumer for new messages.
        """
        return self.consumer.poll(timeout=1.0)


# Greg Attra
# 03/26/22

'''
Executable for running linear function approximation on the four rooms problem
'''

import math
import matplotlib.pyplot as plt
import numpy as np
from tqdm import trange

from lib.function_approximation.algorithms import semigrad_onestep_sarsa
from lib.function_approximation.features import xy_features, all_features
from lib.function_approximation.four_rooms import FourRooms, doors, four_rooms_arena, goal
from lib.function_approximation.policy import q_function

ALPHA: float = 0.01
GAMMA: float = 0.9
EPSILON: float = 0.1
N_EPISODES: int = 100
N_TRIALS: int = 10

def main():
    arena = four_rooms_arena()
    print(arena)
    env = FourRooms(arena)

    feature_sets = [
        (0, 35, lambda s: all_features(s, arena, doors(), goal()), (1., .0, .0)),
        (0.1, 35, lambda s: all_features(s, arena, doors(), goal()), (.0, .0, 1.))
    ]
    for alpha, n_feats, X, color in feature_sets:
        Q = q_function
        total_results = np.zeros((N_TRIALS, N_EPISODES))
        for t in trange(N_TRIALS, desc='Trial', leave=False):
            results = semigrad_onestep_sarsa(
                env,
                Q,
                X,
                n_feats,
                alpha,
                EPSILON,
                GAMMA,
                N_EPISODES)
            total_results[t, :] = results['episode_lengths']

        avg_ret = np.average(total_results, axis=0)
        plt.plot(avg_ret, color=color, label=f'{n_feats} Features | Alpha: {alpha} {"(inference only)" if alpha == 0 else ""}')

        stde_avg_ret = 1.96 * (np.std(total_results) / np.sqrt(N_EPISODES))
        y_neg = avg_ret - stde_avg_ret
        y_pos = avg_ret + stde_avg_ret
        plt.fill_between(
            range(N_EPISODES),
            y_neg,
            y_pos,
            alpha=0.2,
            color=color)

    plt.xlabel('Episode')
    plt.ylabel('Avg Episode Length')
    plt.title('Naiive Features Approximation')
    plt.legend()
    plt.show()

if __name__ == '__main__':
    main()


#
# Problema - Separar Strings de acordo com vários delimitadores
# Autor: Ed - Data: 20/05/2015
# Observacoes: PytCbk - PB 2.1 - P. 56
#
linha = 'Edkallenn Silva de Lima; Casado; Professor, Servidor Público Federal,    Ed'
print("A linha é: \n", linha)
import re
split = re.split(r'[;,\s]\s*', linha)
print("E depois de separada: \n", split)


import pytorch_lightning as pl
import torch.nn as nn
from loguru import logger
from torch.optim import Adam
from torchnlp.datasets import imdb_dataset  # type: ignore

from slp.data.collators import SequenceClassificationCollator
from slp.modules.classifier import RNNTokenSequenceClassifier
from slp.plbind.dm import PLDataModuleFromCorpus
from slp.plbind.helpers import FromLogits
from slp.plbind.module import RnnPLModule
from slp.plbind.trainer import make_trainer, watch_model
from slp.util.log import configure_logging

MAX_LENGTH = 1024
collate_fn = SequenceClassificationCollator(device="cpu", max_length=MAX_LENGTH)
# collate_fn = SequenceClassificationCollator(device="cpu")

if __name__ == "__main__":
    pl.utilities.seed.seed_everything(seed=42)
    EXPERIMENT_NAME = "imdb-words-sentiment-classification"

    configure_logging(f"logs/{EXPERIMENT_NAME}")

    train, test = imdb_dataset(directory="./data/", train=True, test=True)

    raw_train = [d["text"] for d in train]
    labels_train = [d["sentiment"] for d in train]

    raw_test = [d["text"] for d in test]
    labels_test = [d["sentiment"] for d in test]

    ldm = PLDataModuleFromCorpus(
        raw_train,
        labels_train,
        test=raw_test,
        test_labels=labels_test,
        batch_size=64,
        batch_size_eval=32,
        collate_fn=collate_fn,
        pin_memory=True,
        num_workers=1,
        tokens="words",
        embeddings_file="./cache/glove.6B.50d.txt",
        embeddings_dim=50,
        lower=True,
        max_length=MAX_LENGTH,
        limit_vocab_size=-1,
        lang="en_core_web_md",
    )
    ldm.setup()

    model = RNNTokenSequenceClassifier(
        3,
        embeddings=ldm.embeddings,
        bidirectional=True,
        merge_bi="sum",
        finetune_embeddings=True,
        attention=True,
        nystrom=True,
        num_landmarks=32,
        num_heads=2,
    )

    optimizer = Adam([p for p in model.parameters() if p.requires_grad], lr=1e-3)
    criterion = nn.CrossEntropyLoss()

    lm = RnnPLModule(
        model,
        optimizer,
        criterion,
        metrics={"acc": FromLogits(pl.metrics.classification.Accuracy())},
    )

    trainer = make_trainer(
        EXPERIMENT_NAME,
        max_epochs=100,
        gpus=1,
        save_top_k=1,
    )
    watch_model(trainer, model)

    trainer.fit(lm, datamodule=ldm)

    trainer.test(ckpt_path="best", test_dataloaders=ldm.test_dataloader())


from django.conf import settings
from .models import Task, TaskList, Purpose
from django import forms

class TaskForm(forms.ModelForm):
    class Meta:
        model = Task
        fields = ['name', 'time']
        widgets = {
            'name': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Введите'
            }),
            'time': forms.DateTimeInput(attrs={
                'id': "timepkr",
                'style': "width:100px;float:left;",
                'class': "form-control",
                'placeholder': "HH:MM",
            })
        }

class ListAdd(forms.ModelForm):
    data = forms.DateField(widget=forms.DateInput(format='%d/%m/%Y'), input_formats=settings.DATE_INPUT_FORMATS)
    class Meta:
        model = TaskList
        task = forms.ModelChoiceField(queryset=Task.objects.all(), to_field_name="id")
        fields = ['data', 'task']
        widgets = {
            'data': forms.DateInput(attrs={'placeholder': 'DD-MM-YYYY', 'required': 'required'})
        }

class PurposeForm(forms.ModelForm):
    planed = forms.DateField(widget=forms.DateInput(format='%d/%m/%Y %H:%M'), input_formats=settings.DATE_INPUT_FORMATS)
    class Meta:
        model = Purpose
        task = forms.ModelChoiceField(queryset=Task.objects.all(), to_field_name="id")
        fields = ['name', 'task', 'planed']
        widgets = {
            'name': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Введите'
            }),
            'plan': forms.DateTimeInput(attrs={
                'id': "timepkr",
                'style': "width:100px;float:left;",
                'class': "form-control",
                'placeholder': "DD-MM-YYYY HH:MM",
            })
        }


import requests

if __name__ == "__main__":

    for index in range(100):
        response = requests.get('https://en.wikipedia.org/api/rest_v1/page/random/summary').json()
        text = response['extract'].strip()
        if len(text) > 32:
            with open('texts/%d.txt' % index, 'wt') as f:
                f.write(text)


class Solution:
    def freqAlphabets(self, s: str) -> str:
	    res = []
	    i = 0

	    while i < len(s):
		    if i + 2 < len(s) and s[i + 2] == '#':
			    val = int(s[i: i + 2])
			    res.append(chr(val + 96)) # chr(65 - 90) is 'A' to 'Z', chr(97 - 122) is 'a' to 'z'
			    i += 3
		    else:
			    res.append(chr(int(s[i]) + 96))
			    i += 1

	    return ''.join(res)

# Starting from i=0, check if s[i+2]=='#'
# If yes, then s[i: i+2] is a character
# Else, s[i] is a character


"""Diagonal-plus-low-rank preconditioner."""

from __future__ import annotations

from typing import Union

from probnum import backend, linops
from probnum.linalg import solvers
from probnum.typing import LinearOperatorLike

import itergp

class DiagonalPlusLowRank(linops.LinearOperator):
    r"""Diagonal-plus-low-rank preconditioner.

    Preconditioner consisting of a diagonal component
    :math:`\Lambda \in \mathbb{R}^{n \times n}` and a low-rank component of rank
    :math:`k`, given by :math:`P = \Lambda + U U^\top`, where
    :math:`U \in \mathbb{R}^{n \times k}`.

    Parameters
    ----------
    diagonal
        Diagonal component.
    low_rank_factor
        Factor :math:`U` of the low-rank component :math:`UU^\top`.
    """

    def __init__(
        self,
        diagonal: Union[backend.Scalar, backend.Array, linops.Scaling],
        low_rank_factor: LinearOperatorLike,
    ) -> None:

        # Components
        if not isinstance(diagonal, linops.Scaling):
            self._diagonal = linops.Scaling(
                factors=diagonal,
                shape=(low_rank_factor.shape[0], low_rank_factor.shape[0]),
            )
        else:
            self._diagonal = diagonal

        self._low_rank = itergp.linops.LowRankMatrix(U=low_rank_factor)

        sum_linop = self._diagonal + self._low_rank

        # Inverse via matrix inversion lemma
        small_matrix = linops.Identity(
            shape=(low_rank_factor.shape[1], low_rank_factor.shape[1]),
            dtype=sum_linop.dtype,
        ) + low_rank_factor.T @ (self.diag_comp.inv() @ low_rank_factor)
        small_matrix_cholfac = backend.linalg.cholesky(
            small_matrix.todense(), upper=False
        )

        def inv_matmul(X):
            diag_inv_X = self.diag_comp.inv() @ X
            return self.diag_comp.inv() @ X - self.diag_comp.inv() @ (
                low_rank_factor
                @ backend.linalg.solve_cholesky(
                    small_matrix_cholfac,
                    low_rank_factor.T @ diag_inv_X,
                    lower=True,
                )
            )

        diag_plus_low_rank_inverse = linops.LinearOperator(
            shape=self._diagonal.shape, dtype=self._diagonal.dtype, matmul=inv_matmul
        )

        super().__init__(
            shape=self._diagonal.shape,
            dtype=sum_linop.dtype,
            matmul=sum_linop.__matmul__,
            rmatmul=sum_linop.__rmatmul__,
            todense=sum_linop.todense,
            transpose=lambda: self,
            inverse=lambda: diag_plus_low_rank_inverse,
            rank=lambda: self._diagonal.shape[0]
            if self._diagonal.is_positive_definite
            else None,
            trace=sum_linop.trace,
        )

        # Matrix properties
        self.is_symmetric = True
        if self._diagonal.is_positive_definite:
            self.is_positive_definite = True

    @property
    def diag_comp(self) -> linops.Scaling:
        """Diagonal component."""
        return self._diagonal

    @property
    def low_rank_comp(self) -> itergp.linops.LowRankMatrix:
        """Low-rank component."""
        return self._low_rank

    @classmethod
    def from_kernel_matrix_linear_solve(
        cls,
        solver_state: solvers.LinearSolverState,
    ):
        kernel_linop = solver_state.problem.A._summands[0]
        noise_linop = solver_state.problem.A._summands[1]

        diagonal = backend.diagonal(noise_linop.todense())
        low_rank_factor = kernel_linop @ solver_state.belief.Ainv._summands[1].U

        return cls(diagonal=diagonal, low_rank_factor=low_rank_factor)


from __future__ import absolute_import
import logging

from dxlbootstrap.util import MessageUtils
from dxlclient.callbacks import RequestCallback
from dxlclient.message import ErrorResponse, Response

# Configure local logger
logger = logging.getLogger(__name__)

class MispServiceRequestCallback(RequestCallback):
    """
    Constructor parameters:

    :param dxlmispservice.app.MispService app: The Misp service application
    :param api_method: Method or function to invoke when a request is received.
    """
    def __init__(self, app, api_method):
        super(MispServiceRequestCallback, self).__init__()
        self._app = app
        self._api_method = api_method

    def on_request(self, request):
        """
        Callback invoked when a request is received.

        :param dxlclient.message.Request request: The request
        """
        logger.info("Request received on topic '%s'",
                    request.destination_topic)
        logger.debug("Payload for topic %s: %s", request.destination_topic,
                     request.payload)

        try:
            request_dict = MessageUtils.json_payload_to_dict(request) \
                if request.payload else {}
            if "event" in request_dict and \
                    type(request_dict["event"]).__name__ in ("str", "unicode") and \
                    request_dict["event"].isdigit():
                request_dict["event"] = int(request_dict["event"])

            response_data = self._api_method(**request_dict)
            if isinstance(response_data, dict) and \
                    response_data.get("errors", None):
                res = ErrorResponse(
                    request, error_message=str(response_data["errors"][0]))
            else:
                res = Response(request)
            MessageUtils.dict_to_json_payload(res, response_data)
        except Exception as ex:
            error_str = str(ex)
            logger.exception("Error handling request: %s", error_str)
            res = ErrorResponse(request,
                                error_message=MessageUtils.encode(error_str))

        self._app.client.send_response(res)


import random
import json
import urllib
import urllib2
from bs4 import BeautifulSoup

from django.conf import settings

# setup classes/functions

class Browser(object):

    def __init__(self, system, user_agent):
        self.system = system
        self.user_agent = user_agent

browsers = [
    Browser(system="Chrome 31.0 Win7 64-bit",
            user_agent=("Mozilla/5.0 (Windows NT 6.1; WOW64) "
                        "AppleWebKit/537.36 (KHTML, like Gecko) "
                        "Chrome/31.0.1650.57 Safari/537.36")),
    Browser(system="Safari 7.0 MacOSX",
            user_agent=("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9) "
                        "AppleWebKit/537.71 (KHTML, like Gecko) "
                        "Version/7.0 Safari/537.71")),
    Browser(system="Firefox 25.0 Win7 64-bit",
            user_agent=("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) "
                        "Gecko/20100101 Firefox/25.0")),
    Browser(system="Chrome 31.0 MacOSX",
            user_agent=("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) "
                        "AppleWebKit/537.36 (KHTML, like Gecko) "
                        "Chrome/31.0.1650.57 Safari/537.36")),
] # from http://techblog.willshouse.com/2012/01/03/most-common-user-agents/

def url_to_soup(url):
    header = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
              'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
              'Accept-Encoding': 'none',
              'Accept-Language': 'en-US,en;q=0.8',
              'Connection': 'keep-alive'}
    header['User-Agent'] = random.choice(browsers).user_agent
    request = urllib2.Request(url, headers=header)
    page = urllib2.urlopen(request).read()
    return BeautifulSoup(page, 'html.parser')

# scrapers

def eight_hundred_notes(q):
    querystring = "%s-%s-%s site:800notes.com" % (q[:3], q[3:6], q[6:])
    query = urllib.urlencode({
        'key': settings.GOOGLE_BROWSER_KEY,
        'cx': settings.EIGHT_HUNDRED_NOTES_CSE,
        'q': querystring
    })
    url = 'https://www.googleapis.com/customsearch/v1?%s' % query
    response = urllib2.urlopen(url).read()
    data = json.loads(response)
    num_results = int(data['searchInformation']['totalResults'])
    result = dict(name='800notes.com',
                  url="http://800notes.com/Phone.aspx/1-%s-%s-%s" \
                      % (q[:3], q[3:6], q[6:]))
    result['verdict'] = bool(num_results)
    return result

def who_called_us(q):
    url = "http://whocalled.us/lookup/%s" % q
    soup = url_to_soup(url)
    result = dict(name='whocalled.us', url=url)
    # look for an element anywhere on the page with id "calls"
    if soup.find(class_='note'):
        result['verdict'] = True
    else:
        result['verdict'] = False
    return result

def why_call_me(q):
    url = "http://www.whycall.me/%s-%s-%s.html" % (q[:3], q[3:6], q[6:])
    soup = url_to_soup(url)
    result = dict(name='whycall.me', url=url)
    # look for an element anywhere on the page with id "complaint"
    if soup.find(class_='description'):
        result['verdict'] = True
    else:
        result['verdict'] = False
    return result

# aggregation

# scrapers = [why_call_me,]
scrapers = [eight_hundred_notes, who_called_us, why_call_me]

def run(q):
    results = []
    for scraper in scrapers:
        results.append(scraper(q))
    return results


import pygame

class City():
    def __init__(self, screen):
        self.image = pygame.image.load("Graphics/cityImage.png").convert_alpha()

    def enterCity(self, screen, heal, upgrade, leave, player, background):
        width = screen.get_width()
        height = screen.get_height()
        screen.blit(pygame.image.load("Graphics/PlayerInFight.png").convert_alpha(), (width * 0.45, height * 0.55))
        font = pygame.font.Font("Fonts/zx_spectrum-7_bold.ttf", 30)
        playerCurrentHp = font.render("[1] Zycie: " + str(player.hp) + "/" + str(player.maxhp), True, "black")
        screen.blit(playerCurrentHp, (width * 0.1, height * 0.75))
        playerCurrentArmour = font.render("[1] Pancerz: " + str(player.armor), True, "black")
        screen.blit(playerCurrentArmour, (width * 0.4, height * 0.75))
        playerCurrentGold = font.render("Zloto: " + str(player.gold), True, "black")
        screen.blit(playerCurrentGold, (width * 0.75, height * 0.75))
        background = pygame.image.load("Graphics/city.png").convert_alpha()
        self.image = pygame.image.load("Graphics/blank.png").convert_alpha()
        background = pygame.transform.scale(background, (width, height))
        occupied = True
        screen.blit(pygame.image.load("Graphics/heal.png").convert(), (width * 0.1, height * 0.8))
        screen.blit(pygame.image.load("Graphics/upgrade.png").convert(), (width * 0.4, height * 0.8))
        screen.blit(pygame.image.load("Graphics/leave.png").convert(), (width * 0.75, height * 0.8))
        if heal:
            if player.gold <= 0:
                player.gold = 0

            elif player.hp < player.maxhp:
                player.hp += 1
                player.gold -= 1

        elif upgrade:
            if player.gold <= 0:
                player.gold = 0

            else:
                player.armor += 1
                player.gold -= 1


import logging
import os
import pathlib
import shutil

from ruamel.yaml import YAML

from appimagebuilder.modules.analisys.app_runtime_analyser import AppRuntimeAnalyser
from appimagebuilder.modules.generate.bundle_info_gatherer import BundleInfoGatherer
from appimagebuilder.modules.generate.bundle_info_gatherer_cli import (
    BundleInfoGathererCLI,
)
from appimagebuilder.modules.generate.desktop_entry_parser import DesktopEntryParser
from appimagebuilder.modules.generate.package_managers import apt, pacman
from appimagebuilder.modules.generate.recipe_generator import RecipeGenerator
from appimagebuilder.modules.generate import recipe_sections
from appimagebuilder.modules.generate.recipe_sections.files_section_generator import (
    FilesSectionGenerator,
)

class GenerateMethodError(RuntimeError):
    pass

class CommandGenerate:
    def __init__(self):
        self.logger = logging.getLogger("Generator")

        self.logger.info("Searching AppDir")
        self.app_dir = self._locate_app_dir()

        # configure Recipe Generator
        package_manager_section_generators = []
        if shutil.which("apt-get"):
            apt_file_package_resolver = apt.FilePackageResolver()
            apt_package_repository_resolver = apt.PackageRepositoryResolver()
            apt_section_generator = recipe_sections.AptSectionGenerator(
                apt_file_package_resolver, apt_package_repository_resolver
            )
            package_manager_section_generators.append(apt_section_generator)
        if shutil.which("pacman"):
            pacman_file_package_resolver = pacman.FilePackageResolver()
            pacman_section_generator = recipe_sections.PacmanSectionGenerator(
                pacman_file_package_resolver
            )
            package_manager_section_generators.append(pacman_section_generator)

        # append files section generator at last as it will catch all the dependencies
        package_manager_section_generators.append(FilesSectionGenerator())

        bundle_info_gatherer_ui = BundleInfoGathererCLI()
        desktop_entry_parser = DesktopEntryParser()
        bundle_info_gatherer = BundleInfoGatherer(
            bundle_info_gatherer_ui, desktop_entry_parser
        )

        runtime_analyser = AppRuntimeAnalyser()
        self.generator = RecipeGenerator(
            package_manager_section_generators, bundle_info_gatherer, runtime_analyser
        )

    def generate(self):
        recipe = self.generator.generate(self.app_dir)
        self._write_recipe_file(recipe)

        self.logger.info("Recipe generation completed.")

    def _write_recipe_file(self, recipe):
        yaml = YAML()
        yaml.default_flow_style = False

        recipe_header = "# appimage-builder recipe see https://appimage-builder.readthedocs.io for details\n"
        with open("AppImageBuilder.yml", "w") as f:
            f.write(recipe_header)
            yaml.dump(recipe, f)

    @staticmethod
    def _locate_app_dir():
        for file_name in os.listdir(os.path.curdir):
            if os.path.isdir(file_name) and file_name.lower() == "appdir":
                return pathlib.Path(file_name).absolute()

        raise GenerateMethodError(
            "Unable to find an AppDir, this is required to create a recipe."
        )


"""
Read or write data from or to local memory.

Though not very valuable in a production setup, these store adapters are great
for testing purposes.
"""

from oauth2.store import AccessTokenStore, AuthCodeStore, ClientStore
from oauth2.error import AccessTokenNotFound, AuthCodeNotFound, \
                         ClientNotFoundError
from oauth2.datatype import Client

class ClientStore(ClientStore):
    """
    Stores clients in memory.
    """
    def __init__(self):
        self.clients = {}

    def add_client(self, client_id, client_secret, redirect_uris,
                   authorized_grants=None, authorized_response_types=None):
        """
        Add a client app.

        :param client_id: Identifier of the client app.
        :param client_secret: Secret the client app uses for authentication
                              against the OAuth 2.0 provider.
        :param redirect_uris: A ``list`` of URIs to redirect to.

        """
        self.clients[client_id] = Client(
            identifier=client_id,
            secret=client_secret,
            redirect_uris=redirect_uris,
            authorized_grants=authorized_grants,
            authorized_response_types=authorized_response_types)

        return True

    def fetch_by_client_id(self, client_id):
        """
        Retrieve a client by its identifier.

        :param client_id: Identifier of a client app.
        :return: An instance of :class:`oauth2.Client`.
        :raises: ClientNotFoundError

        """
        if client_id not in self.clients:
            raise ClientNotFoundError

        return self.clients[client_id]

class TokenStore(AccessTokenStore, AuthCodeStore):
    """
    Stores tokens in memory.

    Useful for testing purposes or APIs with a very limited set of clients.
    Use memcache or redis as storage to be able to scale.
    """
    def __init__(self):
        self.access_tokens = {}
        self.auth_codes = {}
        self.refresh_tokens = {}
        self.unique_token_identifier = {}

    def fetch_by_code(self, code):
        """
        Returns an AuthorizationCode.

        :param code: The authorization code.
        :return: An instance of :class:`oauth2.datatype.AuthorizationCode`.
        :raises: :class:`AuthCodeNotFound` if no data could be retrieved for
                 given code.

        """
        if code not in self.auth_codes:
            raise AuthCodeNotFound

        return self.auth_codes[code]

    def save_code(self, authorization_code):
        """
        Stores the data belonging to an authorization code token.

        :param authorization_code: An instance of
                                   :class:`oauth2.datatype.AuthorizationCode`.

        """
        self.auth_codes[authorization_code.code] = authorization_code

        return True

    def save_token(self, access_token):
        """
        Stores an access token and additional data in memory.

        :param access_token: An instance of :class:`oauth2.datatype.AccessToken`.
        """
        self.access_tokens[access_token.token] = access_token

        unique_token_key = self._unique_token_key(access_token.client_id,
                                                  access_token.grant_type,
                                                  access_token.user_id)

        self.unique_token_identifier[unique_token_key] = access_token.token

        if access_token.refresh_token is not None:
            self.refresh_tokens[access_token.refresh_token] = access_token

        return True

    def delete_code(self, code):
        """
        Deletes an authorization code after use
        :param code: The authorization code.
        """
        if code in self.auth_codes:
            del self.auth_codes[code]

    def delete_refresh_token(self, refresh_token):
        """
        Deletes a refresh token after use
        :param refresh_token: The refresh_token.
        """
        if refresh_token in self.refresh_tokens:
            del self.refresh_tokens[refresh_token]

    def fetch_by_refresh_token(self, refresh_token):
        """
        Find an access token by its refresh token.

        :param refresh_token: The refresh token that was assigned to an
                              ``AccessToken``.
        :return: The :class:`oauth2.datatype.AccessToken`.
        :raises: :class:`oauth2.error.AccessTokenNotFound`
        """
        if refresh_token not in self.refresh_tokens:
            raise AccessTokenNotFound

        return self.refresh_tokens[refresh_token]

    def fetch_by_token(self, token):
        """
        Returns data associated with an access token or ``None`` if no data
        was found.

        Useful for cases like validation where the access token needs to be
        read again.

        :param token: A access token code.
        :return: An instance of :class:`oauth2.datatype.AccessToken`.
        """
        if token not in self.access_tokens:
            raise AccessTokenNotFound

        return self.access_tokens[token]

    def fetch_existing_token_of_user(self, client_id, grant_type, user_id):
        try:
            key = self._unique_token_key(client_id, grant_type, user_id)
            token = self.unique_token_identifier[key]
        except KeyError:
            raise AccessTokenNotFound

        return self.fetch_by_token(token)

    def _unique_token_key(self, client_id, grant_type, user_id):
        return "{0}_{1}_{2}".format(client_id, grant_type, user_id)