import torch
from torch import nn
import torch.nn.functional as fun
import numpy as np

class C51M(nn.Module):
    def __init__(self, scenario, n_actions, epsilon=1.0, v_min=10, v_max=150):
        super(C51M, self).__init__()
        self.scenario = scenario
        self.n_actions = n_actions
        self.epsilon = epsilon
        self.v_min = v_min
        self.v_max = v_max

        if self.scenario == 'basic':
            # 21 atom for basic scenario
            self.n_atoms = 21
            self.delta_z = (v_max - v_min) / (self.n_atoms - 1)
            self.atom_support = np.array([v_min + i * self.delta_z for i in range(self.n_atoms)], dtype=np.float32)
            self.conv1 = nn.Conv2d(1, 8, kernel_size=6, stride=3)
            self.conv2 = nn.Conv2d(8, 8, kernel_size=3, stride=2)
            self.dropout = nn.Dropout(0.5)

            self.lstm = nn.LSTM(192, 128, batch_first=True)
            self.value_layer = nn.Linear(128, self.n_atoms)
            self.advantages = nn.ModuleList()
            for _ in range(n_actions):
                self.advantages.append(nn.Linear(128, self.n_atoms))
        else:
            # 51 atom for basic scenario
            self.n_atoms = 51
            self.delta_z = (v_max - v_min) / (self.n_atoms - 1)
            self.atom_support = np.array([v_min + i * self.delta_z for i in range(self.n_atoms)], dtype=np.float32)
            self.conv1 = nn.Conv2d(3, 32, kernel_size=8, stride=4)
            self.conv2 = nn.Conv2d(32, 64, kernel_size=4, stride=2)
            self.conv3 = nn.Conv2d(64, 64, kernel_size=3)
            self.dropout = nn.Dropout(0.5)

            self.lstm = nn.LSTM(2560, 512, batch_first=True)
            self.value_layer = nn.Linear(512, self.n_atoms)
            self.advantages = nn.ModuleList()
            for _ in range(n_actions):
                self.advantages.append(nn.Linear(512, self.n_atoms))

    def forward(self, x_screens, lstm_state):
        batch, time = x_screens.shape[:2]
        chw = x_screens.shape[2:]
        x_screens = x_screens.contiguous().view(batch*time, *chw)
        x = fun.relu(self.conv1(x_screens))
        x = fun.relu(self.conv2(x))
        if self.scenario != 'basic':
            x = fun.relu(self.conv3(x))
        x = x.view(batch, time, -1)
        x = self.dropout(x)
        x, lstm_state = self.lstm(x, lstm_state)
        value = self.value_layer(x)
        # noinspection PyUnresolvedReferences
        advantages = torch.stack([adv(x) for adv in self.advantages], dim=-1)

        atom_logits = value.unsqueeze(-1) + advantages - advantages.mean(-1, keepdim=True)
        atom_probabilities = fun.softmax(atom_logits, dim=-2)  # soft max along all atoms

        return atom_probabilities, lstm_state

    # noinspection PyCallingNonCallable, PyUnresolvedReferences
    def sample_actions(self, device, screen, lstm_state):
        screen = torch.tensor(screen, dtype=torch.float32, device=device)
        z_probabilities, lstm_state = self.forward(screen, lstm_state)
        q_values = torch.matmul(torch.from_numpy(self.atom_support).to(device), z_probabilities)
        q_values = q_values.view(-1).detach().cpu().numpy()

        if np.random.rand() < self.epsilon:
            action = np.random.randint(0, self.n_actions, 1)[0]
        else:
            action = q_values.argmax(axis=-1)
        return [action], lstm_state

if __name__ == '__main__':
    net = C51M('basic', 8, 1.0)
    a, _ = net.sample_actions(
        'cpu',
        np.random.normal(size=[1, 1, 1, 30, 45]),
        None
    )
    print(a)


from robot.libraries.BuiltIn import BuiltIn
import requests

class APIKeywords:
    def get_weather(self, city, base_url, api_key):
        query_params = {
            "q": city,
            "appid": api_key,
            "units": "metric"
        }
        response = requests.get(base_url, params=query_params)
        return response


from controlador import *
from markups import *

import telebot
from telebot import types
import numpy as np
from random import randint
from dotenv import load_dotenv
import os
import re

# Telegram Bot Token from BotFather
# Obtener información de .env
load_dotenv(dotenv_path = '.env')
# Obtener host y puerto
TOKEN = os.getenv('TOKEN')

# Telebot Bot
bot = telebot.TeleBot(TOKEN)

# Controlador juego
controlador = Controlador()

# Bienvenida al bot
@bot.message_handler(commands=['start'])
def bienvenida(message):
	bot.send_photo(message.chat.id, photo="https://github.com/Carlosma7/TFM-GrandQuiz/blob/main/doc/img/game/saludo.jpg?raw=true", caption=f"¡Bienvenido {message.from_user.first_name} a GrandQuiz, el concurso donde todas las generaciones son bienvenidas! \n\nMi nombre es Tercetto y seré tu guía en el concurso.", parse_mode = 'Markdown')
	bot.send_message(message.chat.id, f"Para empezar vamos a registrarnos en el concurso, por favor dime tu edad de la forma: \n*/registro <edad>* \n\nPor ejemplo: \n*/registro 24*", parse_mode = 'Markdown')

# Registro en el sistema de GrandQuiz
@bot.message_handler(commands=['registro'])
def registro(message):
	# Crear un jugador con nombre de usuario, nombre y edad
	j = Jugador(message.from_user.username, message.from_user.first_name, int(message.text[10:]))
	try:
		# Crear jugador en controlador
		controlador.crear_jugador(j)
		# Se guarda mensaje de éxito
		respuesta = f"¡Enhorabuena! Ya eres un jugador oficial de GrandQuiz, es hora de la diversión."
	except Exception as error:
		# Se produce un error
		respuesta = str(error)

	# Informar al usuario
	bot.send_message(message.chat.id, respuesta)

# Obtener estadisticas de un jugador
@bot.message_handler(commands=['estadisticas'])
def estadisticas(message):
	try:
		# Obtener estadisticas
		est = controlador.obtener_estadisticas(message.from_user.username)
		# Se guarda mensaje de éxito
		respuesta = f"Las estadísticas de {message.from_user.first_name} son:\n\n\U0001f3c6 {est.get_num_victorias()} victorias.\n\n\U0001f3ae {est.get_num_partidas()} partidas."
	except Exception as error:
		# Se produce un error
		respuesta = str(error)

	# Informar al usuario
	bot.send_message(message.chat.id, respuesta)

# Crear partida de GrandQuiz
@bot.message_handler(commands=['nueva_partida'])
def nueva_partida(message):
	# Comprobar que la conversación es en un grupo
	if message.chat.type == 'group':
		# Crear una partida con el id del chat del grupo
		p = Partida(message.chat.id)
		try:
			# Crear partida en controlador
			controlador.crear_partida(p, message.from_user.username)
			# Se guarda mensaje de éxito
			respuesta = f"¡Allá vamos! {message.from_user.first_name} ha creado una partida. Para unirte indícamelo con */unirme*."
		except Exception as error:
			# Se produce un error
			respuesta = str(error)
	else:
		# No es un grupo
		respuesta = f"Para crear una partida tienes que estar en un grupo."

	# Informar al usuario
	bot.send_message(message.chat.id, respuesta, parse_mode = 'Markdown')

# Unirse a partida
@bot.message_handler(commands=['unirme'])
def unirse_partida(message):
	# Comprobar que la conversación es en un grupo
	if message.chat.type == 'group':
		try:
			# Unirse a partida
			controlador.add_jugador(message.chat.id, message.from_user.username)
			respuesta = f"¡Bien! {message.from_user.first_name} se ha unido a la partida."
		except Exception as error:
			# Se produce un error
			respuesta = str(error)
	else:
		# No es un grupo
		respuesta = f"Para unirte a una partida tienes que estar en un grupo."

	# Informar al usuario
	bot.send_message(message.chat.id, respuesta, parse_mode = 'Markdown')

# Listar jugadores de una partida
@bot.message_handler(commands=['lista'])
def listar_jugadores(message):
	# Comprobar que la conversación es en un grupo
	if message.chat.type == 'group':
		try:
			# Obtener lista de jugadores
			lista = controlador.listar_jugadores(message.chat.id)
			if len(lista) == 0:
				respuesta = f"Aún no hay jugadores en la partida. ¡Anímate en ser el primero!"
			else:
				respuesta = f"Jugadores en la partida:\n\n"
				for jug in lista:
					respuesta += f"\U0001f466\U0001f3fb  {jug.get_nombre()}\n"
				if len(lista) == 2:
					respuesta += f"\nYa hay suficientes jugadores. ¡Así que pon /jugar para que comience el juego!"
		except Exception as error:
			# Se produce un error
			respuesta = str(error)
	else:
		# No es un grupo
		respuesta = f"Para ver una partida tienes que estar en un grupo."

	# Informar al usuario
	bot.send_message(message.chat.id, respuesta, parse_mode = 'Markdown')

# Iniciar una partida
@bot.message_handler(commands=['jugar'])
def iniciar_partida(message):
	# Comprobar que la conversación es en un grupo
	if message.chat.type == 'group':
		try:
			# Iniciar partida
			jug_turno, pregunta = controlador.iniciar_partida(message.chat.id)
			# Informar al usuario
			bot.send_photo(message.chat.id, photo="https://github.com/Carlosma7/TFM-GrandQuiz/blob/main/doc/img/game/nueva_partida.jpg?raw=true", caption=f"¡Por fin es la hora de jugar a GrandQuiz!\n\nY el afortunado en responder primero es \U0001f389 {jug_turno} \U0001f389", parse_mode = 'Markdown')
			# Definir markup
			markup = markup_respuestas(pregunta)
			enunciado = f"Turno de: {jug_turno}.\n\n{pregunta.get_enunciado()}"
			bot.send_message(message.chat.id, enunciado, reply_markup=markup)
		except Exception as error:
			# Se produce un error
			respuesta = str(error)
			# Informar al usuario
			bot.send_message(message.chat.id, respuesta, parse_mode = 'Markdown')
	else:
		# No es un grupo
		respuesta = f"Para iniciar una partida tienes que estar en un grupo."
		# Informar al usuario
		bot.send_message(message.chat.id, respuesta, parse_mode = 'Markdown')

# Responder pregunta
@bot.callback_query_handler(func=lambda call: True)
def responder_pregunta(call):
	if bool(re.match("[1-4]", call.data)):
		try:
			# Responder pregunta
			resultado = controlador.responder_pregunta(call.message.chat.id, call.from_user.username, int(call.data))
			# Pregunta acertada
			if resultado:
				respuesta = f"\u2705 ¡SÍ, HAS ACERTADO! Efectivamente era:\n\n*{controlador.obtener_respuesta(call.message.chat.id)}*"
			# Pregunta fallada
			else:
				respuesta = f"\u274C Noooooo, esa no era la respuesta correcta, la respuesta correcta era:\n\n*{controlador.obtener_respuesta(call.message.chat.id)}*"

			# Editar texto con respuesta
			respuesta = f"{call.message.text}\n\n{respuesta}"
			bot.edit_message_text(respuesta, call.message.chat.id, call.message.id, parse_mode = 'Markdown')

			# Comprobar si ha ganado un jugador
			if not controlador.comprobar_victoria(call.message.chat.id):
				# Cambiar turno
				jug_turno, pregunta = controlador.cambiar_turno(call.message.chat.id)
				# Definir markup
				markup = markup_respuestas(pregunta)
				enunciado = f"Turno de: {jug_turno}.\n\n{pregunta.get_enunciado()}"
				bot.send_message(call.message.chat.id, enunciado, reply_markup=markup)
			else:
				# Si ha ganado un jugador
				ganador = controlador.terminar_partida(call.message.chat.id)
				# Informar al usuario
				bot.send_photo(call.message.chat.id, photo="https://github.com/Carlosma7/TFM-GrandQuiz/blob/main/doc/img/game/ganador.jpg?raw=true", caption=f"\u2B50\U0001f3c6 ¡ENHORABUENA! {ganador.upper()} ES EL CAMPEÓN DE GRANDQUIZ. \U0001f3c6\u2B50", parse_mode = 'Markdown')
		except Exception as error:
			# Se produce un error
			respuesta = f"{call.from_user.first_name}: {str(error)}"
			# Informar al usuario
			bot.send_message(call.message.chat.id, respuesta, parse_mode = 'Markdown')

# Launch bot
bot.polling()


import pyttsx3  # pip install pyttsx3
import speech_recognition as sr  # pip install speechRecognition
import datetime
import wikipedia  # pip install wikipedia
import webbrowser
import os
import smtplib
import pywhatkit
import pyjokes

engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
# print(voices[1].id)
engine.setProperty('voice', voices[0].id)

def speak(audio):
    engine.say(audio)
    engine.runAndWait()

def wishMe():
    hour = int(datetime.datetime.now().hour)
    if hour >= 0 and hour < 12:
        speak("Good Morning!")

    elif hour >= 12 and hour < 18:
        speak("Good Afternoon!")

    else:
        speak("Good Evening!")

    speak("I am your voice assistant how may I help you")

def takeCommand():
    # It takes microphone input from the user and returns string output

    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        r.pause_threshold = 1
        audio = r.listen(source)

    try:
        print("Recognizing...")
        text = r.recognize_google(audio)
        print(f"User said: {text}\n")

    except Exception as e:
        # print(e)
        speak("please repeat again...")
        return "None"
    return text

if __name__ == "__main__":
    wishMe()
    while True:
        # if 1:
        text = takeCommand().lower()

        # Logic for executing tasks based on query

        if 'open youtube' in text:
            webbrowser.open("youtube.com")

        elif 'open google' in text:
            webbrowser.open("google.com")

        elif 'who is' in text:
            person = text.replace('who is', '')
            info = wikipedia.summary(person, 1)
            print(info)
            speak(info)

        elif 'how are you' in text:
            speak("im good, thanks for asking")

        elif 'play' in text:
            song = text.replace('play', '')
            speak('playing ' + song)
            pywhatkit.playonyt(song)

        elif 'time' in text:
            time = datetime.datetime.now().strftime('%I:%M %p')
            speak('Current time is ' + time)

        elif 'joke' in text:
            speak(pyjokes.get_joke())

        elif 'name' in text:
            speak("prajwal")

        elif 'exit' in text:
            speak("thank you")
            quit()


import sqlite3

baglanti = sqlite3.connect('deneme.sqlite3')
cursor = baglanti.cursor()

def tablo_olustur():
    cursor.execute('create table if not exists ogrenci (isim TEXT, soyisim TEXT, no INT)')
    baglanti.commit()
    print('Tablo Oluşturuldu.')

def veri_ekle():
    cursor.execute("insert into ogrenci Values('Mervan','Koncuk',01)")
    cursor.execute("insert into ogrenci Values('umur','savut',02)")
    baglanti.commit()
    print('Veri Eklendi')

def veri_ekle2():
    isim = input("Bir isim giriniz: ")
    soyisim= input('Soyisim giriniz: ')
    no =int(input('Noyu giriniz'))
    cursor.execute("insert into ogrenci Values(?,?,?)",(isim,soyisim,no))
    baglanti.commit()
    print("Veriler Eklendi")

def veri_cek():
    cursor.execute("select * from ogrenci")
    liste = cursor.fetchall()
    for i in liste:
        print(i)

def veri_guncelleme(isim,no):
    cursor.execute("update ogrenci set isim=? where no=?",(isim,no))
    baglanti.commit()
    print("Veri Güncellendi")

def veri_sil(isim):
    cursor.execute("delete from ogrenci where isim=?",(isim,))
    baglanti.commit()
    print("Veri Silindi")

def tablo_sil():
    cursor.execute("drop table ogrenci")
    baglanti.commit()
    print("Tablo Silindi")

tablo_olustur()
veri_ekle()
veri_ekle2()


import calendar
import json
import re
import string

PYTHON_VERSION_COMPATIBILITY = "PY3"

DEPS = [
  'flavor',
  'recipe_engine/context',
  'recipe_engine/file',
  'recipe_engine/json',
  'recipe_engine/path',
  'recipe_engine/step',
  'recipe_engine/time',
  'recipe_engine/properties',
  'recipe_engine/python',
  'recipe_engine/raw_io',
  'run',
  'vars',
]

SEEK_TRACE_NAME = 'skottie::Animation::seek'
RENDER_TRACE_NAME = 'skottie::Animation::render'
EXPECTED_DM_FRAMES = 25

def perf_steps(api):
  """Run DM on lottie files with tracing turned on and then parse the output."""
  api.flavor.create_clean_device_dir(
        api.flavor.device_dirs.dm_dir)
  lotties_host = api.path['start_dir'].join('lotties_with_assets')
  lotties_device = api.path['start_dir'].join('lotties_with_assets')
  if 'Android' in api.vars.builder_cfg.get('extra_config'):
    # Due to http://b/72366966 and the fact that CIPD symlinks files in by default, we ran into
    # a strange "Function not implemented" error when trying to copy folders that contained
    # symlinked files. It is not easy to change the CIPD "InstallMode" from symlink to copy, so
    # we use shutil (file.copytree) to make a local copy of the files on the host, which removes
    # the symlinks and adb push --sync works as expected.
    lotties_device = api.flavor.device_path_join(api.flavor.device_dirs.tmp_dir, 'lotties_with_assets')
    api.flavor.create_clean_device_dir(lotties_device)

    # Make a temp directory and then copy to a *non-existing* subfolder (otherwise copytree crashes).
    lotties_no_symlinks = api.path.mkdtemp('lwa').join('nosymlinks')
    api.file.copytree('Copying files on host to remove symlinks', lotties_host, lotties_no_symlinks)
    lotties_host = lotties_no_symlinks
    api.flavor.copy_directory_contents_to_device(lotties_host, lotties_device)

  # We expect this to be a bunch of folders that contain a data.json and optionally
  # an images/ subfolder with image assets required.
  lottie_files = api.file.listdir(
      'list lottie files', lotties_host,
      test_data=['skottie_asset_000', 'skottie_asset_001', 'skottie_asset_002'])
  perf_results = {}
  # Run DM on each lottie file and parse the trace files.
  for idx, lottie_file in enumerate(lottie_files):
    lottie_name = api.path.basename(lottie_file)
    lottie_folder = api.flavor.device_path_join(lotties_device, lottie_name)

    trace_output_path = api.flavor.device_path_join(
        api.flavor.device_dirs.dm_dir, '%s.json' % (idx + 1))
    # See go/skottie-tracing for how these flags were selected.
    dm_args = [
      'dm',
      '--resourcePath', api.flavor.device_dirs.resource_dir,
      '--lotties', lottie_folder,
      '--src', 'lottie',
      '--nonativeFonts',
      '--verbose',
      '--traceMatch', 'skottie',  # recipe can OOM without this.
      '--trace', trace_output_path,
      '--match', get_trace_match(
          'data.json', 'Android' in api.properties['buildername']),
    ]
    if api.vars.builder_cfg.get('cpu_or_gpu') == 'GPU':
      dm_args.extend(['--config', 'gles', '--nocpu'])
    elif api.vars.builder_cfg.get('cpu_or_gpu') == 'CPU':
      dm_args.extend(['--config', '8888', '--nogpu'])
    api.run(api.flavor.step, 'dm', cmd=dm_args, abort_on_failure=False)

    trace_test_data = api.properties.get('trace_test_data', '{}')
    trace_file_content = api.flavor.read_file_on_device(trace_output_path)
    if not trace_file_content and trace_test_data:
      trace_file_content = trace_test_data

    key = 'gles'
    if api.vars.builder_cfg.get('cpu_or_gpu') == 'CPU':
        key = '8888'
    perf_results[lottie_name] = {
        key: parse_trace(trace_file_content, lottie_name, api),
    }
    api.flavor.remove_file_on_device(trace_output_path)

  # Construct contents of the output JSON.
  perf_json = {
      'gitHash': api.properties['revision'],
      'swarming_bot_id': api.vars.swarming_bot_id,
      'swarming_task_id': api.vars.swarming_task_id,
      'renderer': 'skottie',
      'key': {
        'bench_type': 'tracing',
        'source_type': 'skottie',
      },
      'results': perf_results,
  }
  if api.vars.is_trybot:
    perf_json['issue'] = api.vars.issue
    perf_json['patchset'] = api.vars.patchset
    perf_json['patch_storage'] = api.vars.patch_storage
  # Add tokens from the builder name to the key.
  reg = re.compile('Perf-(?P<os>[A-Za-z0-9_]+)-'
                   '(?P<compiler>[A-Za-z0-9_]+)-'
                   '(?P<model>[A-Za-z0-9_]+)-'
                   '(?P<cpu_or_gpu>[A-Z]+)-'
                   '(?P<cpu_or_gpu_value>[A-Za-z0-9_]+)-'
                   '(?P<arch>[A-Za-z0-9_]+)-'
                   '(?P<configuration>[A-Za-z0-9_]+)-'
                   'All(-(?P<extra_config>[A-Za-z0-9_]+)|)')
  m = reg.match(api.properties['buildername'])
  keys = ['os', 'compiler', 'model', 'cpu_or_gpu', 'cpu_or_gpu_value', 'arch',
          'configuration', 'extra_config']
  for k in keys:
    perf_json['key'][k] = m.group(k)

  # Create the output JSON file in perf_data_dir for the Upload task to upload.
  api.file.ensure_directory(
      'makedirs perf_dir',
      api.flavor.host_dirs.perf_data_dir)
  now = api.time.utcnow()
  ts = int(calendar.timegm(now.utctimetuple()))
  json_path = api.flavor.host_dirs.perf_data_dir.join(
      'perf_%s_%d.json' % (api.properties['revision'], ts))
  json_contents = json.dumps(
      perf_json, indent=4, sort_keys=True, separators=(',', ': '))
  api.file.write_text('write output JSON', json_path, json_contents)

def get_trace_match(lottie_filename, is_android):
  """Returns the DM regex to match the specified lottie file name."""
  trace_match = '^%s$' % lottie_filename
  if is_android and ' ' not in trace_match:
    # Punctuation characters confuse DM when shelled out over adb, so escape
    # them. Do not need to do this when there is a space in the match because
    # subprocess.list2cmdline automatically adds quotes in that case.
    for sp_char in string.punctuation:
      if sp_char == '\\':
        # No need to escape the escape char.
        continue
      trace_match = trace_match.replace(sp_char, '\%s' % sp_char)
  return trace_match

def parse_trace(trace_json, lottie_filename, api):
  """parse_trace parses the specified trace JSON.

  Parses the trace JSON and calculates the time of a single frame. Frame time is
  considered the same as seek time + render time.
  Note: The first seek is ignored because it is a constructor call.

  A dictionary is returned that has the following structure:
  {
    'frame_max_us': 100,
    'frame_min_us': 90,
    'frame_avg_us': 95,
  }
  """
  step_result = api.run(
      api.python.inline,
      'parse %s trace' % lottie_filename,
      program="""
  import json
  import sys

  trace_output = sys.argv[1]
  trace_json = json.loads(trace_output)
  lottie_filename = sys.argv[2]
  output_json_file = sys.argv[3]

  perf_results = {}
  frame_max = 0
  frame_min = 0
  frame_cumulative = 0
  current_frame_duration = 0
  total_frames = 0
  frame_start = False
  for trace in trace_json:
    if '%s' in trace['name']:
      if frame_start:
        raise Exception('We got consecutive Animation::seek without a ' +
                        'render. Something is wrong.')
      frame_start = True
      current_frame_duration = trace['dur']
    elif '%s' in trace['name']:
      if not frame_start:
        raise Exception('We got an Animation::render without a seek first. ' +
                        'Something is wrong.')

      current_frame_duration += trace['dur']
      frame_start = False
      total_frames += 1
      frame_max = max(frame_max, current_frame_duration)
      frame_min = (min(frame_min, current_frame_duration)
                   if frame_min else current_frame_duration)
      frame_cumulative += current_frame_duration

  expected_dm_frames = %d
  if total_frames != expected_dm_frames:
    raise Exception(
        'Got ' + str(total_frames) + ' frames instead of ' +
        str(expected_dm_frames))
  perf_results['frame_max_us'] = frame_max
  perf_results['frame_min_us'] = frame_min
  perf_results['frame_avg_us'] = frame_cumulative/total_frames

  # Write perf_results to the output json.
  with open(output_json_file, 'w') as f:
    f.write(json.dumps(perf_results))
  """ % (SEEK_TRACE_NAME, RENDER_TRACE_NAME, EXPECTED_DM_FRAMES),
  args=[trace_json, lottie_filename, api.json.output()])

  # Sanitize float outputs to 2 precision points.
  output = dict(step_result.json.output)
  output['frame_max_us'] = float("%.2f" % output['frame_max_us'])
  output['frame_min_us'] = float("%.2f" % output['frame_min_us'])
  output['frame_avg_us'] = float("%.2f" % output['frame_avg_us'])
  return output

def RunSteps(api):
  api.vars.setup()
  api.file.ensure_directory('makedirs tmp_dir', api.vars.tmp_dir)
  api.flavor.setup('dm')

  with api.context():
    try:
      api.flavor.install(resources=True, lotties=True)
      perf_steps(api)
    finally:
      api.flavor.cleanup_steps()
    api.run.check_failure()

def GenTests(api):
  trace_output = """
[{"ph":"X","name":"void skottie::Animation::seek(SkScalar)","ts":452,"dur":2.57,"tid":1,"pid":0},{"ph":"X","name":"void SkCanvas::drawPaint(const SkPaint &)","ts":473,"dur":2.67e+03,"tid":1,"pid":0},{"ph":"X","name":"void skottie::Animation::seek(SkScalar)","ts":3.15e+03,"dur":2.25,"tid":1,"pid":0},{"ph":"X","name":"void skottie::Animation::render(SkCanvas *, const SkRect *, RenderFlags) const","ts":3.15e+03,"dur":216,"tid":1,"pid":0},{"ph":"X","name":"void SkCanvas::drawPath(const SkPath &, const SkPaint &)","ts":3.35e+03,"dur":15.1,"tid":1,"pid":0},{"ph":"X","name":"void skottie::Animation::seek(SkScalar)","ts":3.37e+03,"dur":1.17,"tid":1,"pid":0},{"ph":"X","name":"void skottie::Animation::render(SkCanvas *, const SkRect *, RenderFlags) const","ts":3.37e+03,"dur":140,"tid":1,"pid":0}]
"""
  dm_json_test_data = """
{
  "gitHash": "bac53f089dbc473862bc5a2e328ba7600e0ed9c4",
  "swarming_bot_id": "skia-rpi-094",
  "swarming_task_id": "438f11c0e19eab11",
  "key": {
    "arch": "arm",
    "compiler": "Clang",
    "cpu_or_gpu": "GPU",
    "cpu_or_gpu_value": "Mali400MP2",
    "extra_config": "Android",
    "model": "AndroidOne",
    "os": "Android"
   },
   "results": {
   }
}
"""
  parse_trace_json = {
      'frame_avg_us': 179.71,
      'frame_min_us': 141.17,
      'frame_max_us': 218.25
  }
  android_buildername = ('Perf-Android-Clang-AndroidOne-GPU-Mali400MP2-arm-'
                         'Release-All-Android_SkottieTracing')
  gpu_buildername = ('Perf-Debian10-Clang-NUC7i5BNK-GPU-IntelIris640-x86_64-'
                     'Release-All-SkottieTracing')
  cpu_buildername = ('Perf-Debian10-Clang-GCE-CPU-AVX2-x86_64-Release-All-'
                     'SkottieTracing')
  yield (
      api.test(android_buildername) +
      api.properties(buildername=android_buildername,
                     repository='https://skia.googlesource.com/skia.git',
                     revision='abc123',
                     task_id='abc123',
                     trace_test_data=trace_output,
                     dm_json_test_data=dm_json_test_data,
                     path_config='kitchen',
                     swarm_out_dir='[SWARM_OUT_DIR]') +
      api.step_data('parse skottie_asset_000 trace',
                    api.json.output(parse_trace_json)) +
      api.step_data('parse skottie_asset_001 trace',
                    api.json.output(parse_trace_json)) +
      api.step_data('parse skottie_asset_002 trace',
                    api.json.output(parse_trace_json))
  )
  yield (
      api.test(gpu_buildername) +
      api.properties(buildername=gpu_buildername,
                     repository='https://skia.googlesource.com/skia.git',
                     revision='abc123',
                     task_id='abc123',
                     trace_test_data=trace_output,
                     dm_json_test_data=dm_json_test_data,
                     path_config='kitchen',
                     swarm_out_dir='[SWARM_OUT_DIR]') +
      api.step_data('parse skottie_asset_000 trace',
                    api.json.output(parse_trace_json)) +
      api.step_data('parse skottie_asset_001 trace',
                    api.json.output(parse_trace_json)) +
      api.step_data('parse skottie_asset_002 trace',
                    api.json.output(parse_trace_json))
  )
  yield (
      api.test(cpu_buildername) +
      api.properties(buildername=cpu_buildername,
                     repository='https://skia.googlesource.com/skia.git',
                     revision='abc123',
                     task_id='abc123',
                     trace_test_data=trace_output,
                     dm_json_test_data=dm_json_test_data,
                     path_config='kitchen',
                     swarm_out_dir='[SWARM_OUT_DIR]') +
      api.step_data('parse skottie_asset_000 trace',
                    api.json.output(parse_trace_json)) +
      api.step_data('parse skottie_asset_001 trace',
                    api.json.output(parse_trace_json)) +
      api.step_data('parse skottie_asset_002 trace',
                    api.json.output(parse_trace_json))
  )
  yield (
      api.test('skottietracing_parse_trace_error') +
      api.properties(buildername=android_buildername,
                     repository='https://skia.googlesource.com/skia.git',
                     revision='abc123',
                     task_id='abc123',
                     trace_test_data=trace_output,
                     dm_json_test_data=dm_json_test_data,
                     path_config='kitchen',
                     swarm_out_dir='[SWARM_OUT_DIR]') +
      api.step_data('parse skottie_asset_000 trace',
                    api.json.output(parse_trace_json), retcode=1)
  )
  yield (
      api.test('skottietracing_trybot') +
      api.properties(buildername=android_buildername,
                     repository='https://skia.googlesource.com/skia.git',
                     revision='abc123',
                     task_id='abc123',
                     trace_test_data=trace_output,
                     dm_json_test_data=dm_json_test_data,
                     path_config='kitchen',
                     swarm_out_dir='[SWARM_OUT_DIR]',
                     patch_ref='89/456789/12',
                     patch_repo='https://skia.googlesource.com/skia.git',
                     patch_storage='gerrit',
                     patch_set=7,
                     patch_issue=1234,
                     gerrit_project='skia',
                     gerrit_url='https://skia-review.googlesource.com/') +
      api.step_data('parse skottie_asset_000 trace',
                    api.json.output(parse_trace_json)) +
      api.step_data('parse skottie_asset_001 trace',
                    api.json.output(parse_trace_json)) +
      api.step_data('parse skottie_asset_002 trace',
                    api.json.output(parse_trace_json))
  )


#!/bin/env python
# -*- coding: utf-8 -*-

#
# Copyright (C) 2012 Alexander Labunets (cahbka@gmail.com).
# All rights reserved.
#

import logging
import M2Crypto
import urllib
import urllib2
import xml.etree.ElementTree as etree

class SPGW:
    ALG = 'SHA1RSA'

    def __init__(self, private_key, public_key, url, aid, tident):
        self.private_key = private_key
        self.public_key = public_key
        self.url = url
        self.aid = aid
        self.tident = tident

    def _sign(self, text):
        key = M2Crypto.EVP.load_key_string(self.private_key)
        key.reset_context(md='sha1')
        key.sign_init()
        key.sign_update(text)
        signature = key.sign_final()[::-1]              # compatibility with Microsoft Cryptographic API (CAPI)
        return signature.encode('base64')

    def _verify_sign(self, text, signature):
        signature = signature.decode('base64')[::-1]    # compatibility with Microsoft Cryptographic API (CAPI)
        bio = M2Crypto.BIO.MemoryBuffer(self.public_key)
        rsa = M2Crypto.RSA.load_pub_key_bio(bio)
        pubkey = M2Crypto.EVP.PKey()
        pubkey.assign_rsa(rsa)
        pubkey.reset_context(md='sha1')
        pubkey.verify_init()
        pubkey.verify_update(text)
        return pubkey.verify_final(signature)

    def _parse(self, response):
        root = etree.fromstring(response)
        self.c = int(root.find('C').text.encode('utf-8'))
        self.r = root.find('R').text.encode('utf-8')
        self.s = root.find('S').text.encode('utf-8')
        if self._verify_sign(self.r, self.s):
            logging.debug('The signature is authentic')
            return dict(x.split('=') for x in self.r.split('&'))
        else:
            logging.debug('The signature is not authentic')
            return None

    def _send(self, command, params):
        query = 'ot=%s' % command
        for k, v in params.items():
            query = '&'.join([query, '='.join([k, v])])
        signature = self._sign(query)
        request = '{0:>s}?alg={1:>s}&query={2:>s}&sign={3:>s}'.format(
            self.url,
            self.ALG,
            urllib2.quote(query.encode('cp1251')),
            urllib2.quote(signature)
        )
        logging.debug("Request: [{0:>s}]".format("".join(request.split())))
        response = urllib2.urlopen(request).read()
        logging.debug("Response: [{0:>s}]".format("".join(response.split())))
        return self._parse(response)

    def request(self, ocode, code_1, code_2, code_3, amount, full_amount):
        command = 'R'
        params = {'TIdent': str(self.tident),
                  'OCode' : str(ocode),
                  'Code1' : str(code_1),
                  'Code2' : str(code_2),
                  'Code3' : str(code_3),
                  'Amount' : str(int(amount * 100)),
                  'FullAmount' : '' if full_amount is None else str(int(full_amount * 100))}
        return self._send(command, params)

    def confirm(self, cid, receiptnum):
        command = 'C'
        params = {'TIdent': str(self.tident),
                  'Cid': str(cid),
                  'ReceiptNum': str(receiptnum)}
        return self._send(command, params)

    def balance(self):
        command = 'GB'
        params = {'AId': str(self.aid)}
        balance = None
        try:
            b = self._send(command, params).get("Balances", None)
            if b:
                root = etree.fromstring(urllib.unquote_plus(b))
                node = root.find('Balance').find('CurrentBalance')
                if node.text:
                    balance = node.text
        except:
            pass
        return balance

if __name__ == "__main__":
    # set logger
    channel = logging.StreamHandler()
    channel.setFormatter(logging.Formatter("[%(asctime)s] [%(process)d] [%(levelname)-8s] %(message)s"))
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)
    logger.addHandler(channel)

    url = 'https://test.delta-telecom.ru/dtpayspgw/'

    # agent params
    privkey = open("private.key").read()
    pubkey = open("pubkey.pem").read()
    aid = 5088
    tident = '00000005088.00001558'

    # payment params
    receiptnum = '123456789'
    ocode = 3
    code_1 = '9261234567'
    amount = 97.0
    full_amount = 100.0

    # start test
    spgw = SPGW(privkey, pubkey, url, aid, tident)
    balance = spgw.balance()
    if balance and spgw.c == 0:
        balance = float(balance)
        logging.info("Balance = {0:.2f}".format(balance))
        if balance > amount:
            response = spgw.request(ocode, code_1, '', '', amount, full_amount)
            if spgw.c == 0:
                result = int(response.get("Result", None))
                message = response.get("ResultMessage", None)
                logging.info("Result = {0:>d}, Message = {1:>s}".format(result, message))
                if result == 0 and spgw.c == 0:
                    cid = int(response["Cid"])
                    response = spgw.confirm(cid, receiptnum)
                    if spgw.c == 0:
                        result = int(response.get("Result", None))
                        logging.info("Result = {0:>d}, Cid = {1:>d}".format(result, cid))
                        if result == 0:
                            logging.info("Payment processed successfully")
                else:
                    logging.info("Payment error: {0:>s}".format(message))
            else:
                logging.error("Error: {0:>s}".format(spgw.r))
        else:
            logging.error("Error: balance too low")
    else:
        logging.error("Error: wrong balance ({0:>s})".format(spgw.r))


#! /usr/bin/python3

import numpy as np
from scipy.optimize import curve_fit

"""
Colección de funciones para calcular un vector desde
el modelo de trayectoria
"""

def fun_lineal(x, b, c):
    return x * b + c

def tangente(ti, t, e, n, z):
    """
    Genera un vector en el tiempo ti
    para un modelo de desplazamiento (n, e, z)
    con un tiempo t

    Input:
    ti: ubicación de la recta tangente
    t:  array con tiempo del modelo
    e:  array con desplazamiento este
    n:  array con desplazamiento norte
    z:  array con desplazamiento vertical
    """
    # evalua la velocidad en base a los dos puntos mas cercanos del modelo
    idx = (np.abs(t-ti)).argmin()

    # dependiendo de la posicion de idx, considera el punto mas cercano
    # a la izquierda o a la derecha
    if idx-1 > 0:
        vel_e = (e[idx] - e[idx-1]) / (t[idx]-t[idx-1])
        vel_n = (n[idx] - n[idx-1]) / (t[idx]-t[idx-1])
        vel_z = (z[idx] - z[idx-1]) / (t[idx]-t[idx-1])
        # y = mx + c
        # calculo de c
        c_e = vel_e*(-t[idx-1]) + e[idx-1]
        c_n = vel_n*(-t[idx-1]) + n[idx-1]
        c_z = vel_z*(-t[idx-1]) + z[idx-1]

    else:
        vel_e = (e[idx+1] - e[idx]) / (t[idx+1]-t[idx])
        vel_n = (n[idx+1] - n[idx]) / (t[idx+1]-t[idx])
        vel_z = (z[idx+1] - z[idx]) / (t[idx+1]-t[idx])
        # y = mx + c
        # calculo de c
        c_e = vel_e*(-t[idx]) + e[idx]
        c_n = vel_n*(-t[idx]) + n[idx]
        c_z = vel_z*(-t[idx]) + z[idx]

    return np.array([vel_e, vel_n, vel_z]), np.array([c_e, c_n, c_z])

def fit(intervalo, t, e, n, z):
    """
    Genera un vector realizando un ajuste lineal
    sobre una serie en un intervalo de tiempo
    """
    # obtiene datos de intervalo de tiempo
    id_t = (t >= intervalo[0]) & (t <= intervalo[1])
    t = t[id_t]
    e = e[id_t]
    n = n[id_t]
    z = z[id_t]
    if len(t) < 10:
        print("Estacion sin datos para calcular una vel media")
        return False

    # ajuste de datos
    pva_e, cov_e = curve_fit(fun_lineal, t, e)
    pva_n, cov_n = curve_fit(fun_lineal, t, n)
    pva_z, cov_z = curve_fit(fun_lineal, t, z)

    err_e = np.sqrt(np.diag(cov_e))
    err_n = np.sqrt(np.diag(cov_n))
    err_z = np.sqrt(np.diag(cov_z))

    return (np.array([pva_e[0], pva_n[0], pva_z[0]]),
            np.array([pva_e[1], pva_n[1], pva_z[1]]),
            np.array([err_e[0], err_n[0], err_z[0]]))


from pyrogram import Client, enums, filters
from pyrogram.types import Message
from . import *

@Client.on_message(filters.command("cانضم", [""]) & filters.user(DEVS) & ~filters.me)
@Ubot(["انضم"], "")
async def join(client: Client, message: Message):
    tex = message.command[1] if len(message.command) > 1 else message.chat.id
    g = await message.reply_text("`جاري...`")
    try:
        await client.join_chat(tex)
        await g.edit(f"**تم الانضمام بنجاح ايدي المجموعة* `{tex}`")
    except Exception as ex:
        await g.edit(f"**خطأ:** \n\n{str(ex)}")

@Ubot(["غادر"], "")
async def leave(client: Client, message: Message):
    xd = message.command[1] if len(message.command) > 1 else message.chat.id
    xv = await message.reply_text("`جاري...`")
    try:
        await xv.edit_text(f"{client.me.first_name} غادر القروب, بايي!!")
        await client.leave_chat(xd)
    except Exception as ex:
        await xv.edit_text(f"**خطأ:** \n\n{str(ex)}")

@Ubot(["اطلعر"], "")
async def kickmeall(client: Client, message: Message):
    tex = await message.reply_text("`جاري مغادرة كل المجموعات...`")
    er = 0
    done = 0
    async for dialog in client.get_dialogs():
        if dialog.chat.type in (enums.ChatType.GROUP, enums.ChatType.SUPERGROUP):
            chat = dialog.chat.id
            try:
                done += 1
                await client.leave_chat(chat)
            except BaseException:
                er += 1
    await tex.edit(
        f"**تمت المغادرة من{done} مجموعة, فشل من {er} مجموعة**"
    )

@Ubot(["اطلعق"], "")
async def kickmeallch(client: Client, message: Message):
    ok = await message.reply_text("`جاري مغادرة كل القنوات...`")
    er = 0
    done = 0
    async for dialog in client.get_dialogs():
        if dialog.chat.type in (enums.ChatType.CHANNEL):
            chat = dialog.chat.id
            try:
                done += 1
                await client.leave_chat(chat)
            except BaseException:
                er += 1
    await ok.edit(
        f"**تمت المغادرة من {done} قناة, فشل المغادرة من {er} قناة**"
    )

add_command_help(
    "المغادرة",
    [
        [f"اطلر", "يغادر القروبات."],
        [f"اطلعق", "يغادر القنوات."],
        [f"انضم [Username]", "يضيف المستخدم."],
        [f"غادر [Username]", "يطرد الشخص الي منشنته."],
    ],
)


import numpy as np
import pytest

from cgpm.utils.general import get_prng
from cgpm.utils.test import gen_data_table

from cgpm2.categorical import Categorical
from cgpm2.crp import CRP
from cgpm2.normal import Normal
from cgpm2.poisson import Poisson

from cgpm2.flexible_rowmix import FlexibleRowMixture
from cgpm2.product import Product

from cgpm2.walks import add_cgpm
from cgpm2.walks import remove_cgpm

from cgpm2.transition_views import get_cgpm_view_proposals_existing
from cgpm2.transition_views import get_cgpm_view_proposals_singleton
from cgpm2.transition_views import get_dataset

def get_crosscat(prng):
    view0 = FlexibleRowMixture(
        cgpm_row_divide=CRP([-1], [], rng=prng),
        cgpm_components_base=Product([
            Normal([0], [], rng=prng),
            Normal([1], [], rng=prng),
        ], rng=prng),
        rng=prng)
    view1 = FlexibleRowMixture(
        cgpm_row_divide=CRP([-2], [], rng=prng),
        cgpm_components_base=Product([
            Poisson([2], [], rng=prng),
            Normal([3], [], rng=prng),
            Normal([4], [], rng=prng),
        ], rng=prng),
        rng=prng)
    view2 = FlexibleRowMixture(
        cgpm_row_divide=CRP([-3], [], rng=prng),
        cgpm_components_base=Product([
            Categorical([5], [], distargs={'k':4}, rng=prng),
        ], rng=prng),
        rng=prng)
    return Product([view0, view1, view2], rng=prng)

def populate_crosscat(crosscat, prng):
    X, Zv, Zrv = gen_data_table(
        n_rows=10,
        view_weights=[.4, .6],
        cluster_weights=[[.3,.4,.3],[.5,.5]],
        cctypes=['normal','normal','poisson','normal','normal','categorical'],
        distargs=[None, None, None, None, None, {'k':4}],
        separation=[0.99]*6,
        rng=prng)
    X[0,1] = X[3,1] = float('nan')
    dataset = np.transpose(X)
    for rowid, row in enumerate(dataset):
        observation = {c:v for c,v in enumerate(row)}
        crosscat.observe(rowid, observation)
    return crosscat

def test_crosscat_add_remove():
    prng = get_prng(2)
    crosscat =  get_crosscat(prng)
    infinite_mixture4 = FlexibleRowMixture(
        cgpm_row_divide=CRP([-4], [], rng=prng),
        cgpm_components_base=Product([
            Categorical([6], [], distargs={'k':4}, rng=prng),
        ], rng=prng),
        rng=prng)
    crosscat = add_cgpm(crosscat, infinite_mixture4)
    assert crosscat.outputs == [-1, 0, 1, -2, 2, 3, 4, -3, 5, -4, 6]
    crosscat = remove_cgpm(crosscat, -1)
    assert crosscat.outputs == [-2, 2, 3, 4, -3, 5, -4, 6]
    crosscat = remove_cgpm(crosscat, 5)
    assert crosscat.outputs == [-2, 2, 3, 4, -4, 6]

def test_get_view_proposals():
    prng = get_prng(2)
    crosscat = get_crosscat(prng)
    # Get block proposals of outputs [0,1] into all three views.
    proposals = get_cgpm_view_proposals_existing(crosscat, [0,1])
    assert len(proposals) == 3
    assert proposals[0].outputs == crosscat.cgpms[0].outputs
    assert proposals[1].outputs == crosscat.cgpms[1].outputs + [0, 1]
    assert proposals[2].outputs == crosscat.cgpms[2].outputs + [0, 1]
    # Get proposals of outputs [0,1] into 2 singleton views.
    proposals = get_cgpm_view_proposals_singleton(crosscat, [0,1], 2)
    assert len(proposals) == 2
    assert proposals[0].outputs[1:] == [0,1]
    assert proposals[1].outputs[1:] == [0,1]
    # Fail to get proposals for outputs in different views.
    with pytest.raises(Exception):
        proposals = get_cgpm_view_proposals_existing(crosscat, [0,2])

def test_logpdf_basic():
    prng = get_prng(2)
    crosscat = get_crosscat(prng)
    crosscat = populate_crosscat(crosscat, prng)
    for _rowid, row in get_dataset(crosscat, 0):
        logp = crosscat.logpdf(None, row)
        if np.isnan(row.values()[0]):
            assert np.allclose(logp, 0)
        else:
            assert logp < 0


def new_pandas_column_GUI(df_info=None, show_text_col = False, **kwargs):
    """
    If passed no parameters this will look for all the dataframes in the user
    namespace and make them available for adding a new column to. Once a
    dataframe is chosen only the numerical columns from that dataframe will
    be available for inclusion in the new column expression.

    If you wish to allow only certain dataframes or have them show up as
    user friendly names in the menus provide that information in the first
    paramater df_info.

    To allow inclusion of text columns pass True for show_text_col.

    :param bool show_text_col: (default = False). When True columns
    containing text will be shown.

    :param list df_info: List of Lists [[object,globalname,
    userfriendly]],..]
      * object -- pandas.DataFrame
      * globalname -- string name of the object in the user global name space.
      * userfriendly -- string name to display for user selection.

    :keyword bool findframes: default = True. If set to false and dataframes
    are passed in dfs_info, will not search for dataframes in the user
    namespace.
    """

    from ipywidgets import Layout, Box, HBox, VBox, GridBox, Tab, \
        Dropdown, Label, Text, Textarea, Button, Checkbox, Output
    from ipywidgets import HTML as richLabel
    from IPython.display import display, HTML
    from IPython import get_ipython
    from JPSLUtils.utils import new_cell_immediately_below,\
        select_cell_immediately_below, move_cursor_in_current_cell, \
        insert_text_into_next_cell, insert_text_at_beginning_of_current_cell, \
        insert_newline_at_end_of_current_cell, select_containing_cell, \
        delete_selected_cell, replace_text_of_next_cell

    from .utils import find_pandas_dataframe_names, build_run_snip_widget
    from IPython import get_ipython
    global_dict = get_ipython().user_ns
    JPSLUtils = global_dict["JPSLUtils"]
    dfs_info = []
    if isinstance(df_info,list):
        for k in df_info:
            dfs_info.append(k)
    findframes = kwargs.pop('findframes',True)
    if findframes:
        for k in find_pandas_dataframe_names():
            dfs_info.append([global_dict[k],k,k])
    friendly_to_globalname = {k[2]:k[1] for k in dfs_info}
    friendly_to_object = {k[2]:k[0] for k in dfs_info}

    #### Define GUI Elements ####

    importstr = '# CODE BLOCK generated using new_pandas_column_GUI().\n' \
                '# See https://jupyterphysscilab.github.io/' \
                'jupyter_Pandas_GUI.\n' \
                '# Imports (no effect if already imported)\n' \
                'import numpy as np\n'
    allbutlastline = importstr
    lastline = ''

    def split_to_all_but_last_and_last(text):
        all_but_last = ''
        last = ''
        lines = text.split('\n')
        for k in range(len(lines)):
            if k < len(lines) - 1:
                all_but_last += lines[k] + '\n'
            else:
                last = lines[k]
        return all_but_last, last

    # DataFrame Choice (Step 1)
    step1instr = Label(value = 'Select the DataFrame to work with.')
    tempopts = []
    tempopts.append('Choose')
    for k in dfs_info:
        tempopts.append(k[2])
    whichframe = Dropdown(options=tempopts,
                                description='DataFrame: ',)

    def update_columns(change):
        df = friendly_to_object[change['new']]
        tempcols = df.columns.values
        tempopt = ['Choose column to insert.']
        for k in tempcols:
            if show_text_col:
                tempopt.append(k)
            else:
                if df[k].dtype != 'O':
                    tempopt.append(k)
        whichcolumn.options = tempopt
        pass
    whichframe.observe(update_columns, names='value')
    step1 = VBox(children=[step1instr, whichframe])

    # Step 2
    newname = Text(placeholder='Type name for new column.')
    step2instr = richLabel(
        value='Pick a name for the new column. The expression will be ' \
              'built in the cell (textbox) below. Click the "Insert" button ' \
              'when you are satisfied with the name.')
    insertname = Button(description="Insert")

    def do_insertname(change):
        framename = friendly_to_globalname[whichframe.value]
        codestr = framename + '[\'' + newname.value + '\'] = '
        if JPSLUtils.notebookenv == 'NBClassic':
            select_containing_cell('newcolGUI')
            select_cell_immediately_below()
            insert_newline_at_end_of_current_cell(codestr)
        else:
            allbutlastline, lastline = split_to_all_but_last_and_last(
                codearea.sniptext.value)
            if lastline == '' or lastline == '\n':
                codearea.sniptext.value = allbutlastline + '\n' + codestr
            else:
                if lastline.endswith('\n'):
                    codearea.sniptext.value = allbutlastline + lastline + codestr
                else:
                    codearea.sniptext.value = allbutlastline + lastline + '\n' + \
                                   codestr
        pass

    insertname.on_click(do_insertname)

    step2 = VBox(children=[step2instr, HBox(children=[newname,
                                           insertname])])

    # Step 3
    whichcolumn = Dropdown(options=['Choose column to insert.'],
                           description='Column: ',
                           )

    def column_insert(change):
        col = change['new']
        if col == 'Choose column to insert.':
            return
        framename = friendly_to_globalname[whichframe.value]
        text = framename + '[\'' + col + '\']'
        if JPSLUtils.notebookenv == 'NBClassic':
            select_containing_cell('newcolGUI')
            insert_text_into_next_cell(text)
        else:
            allbutlastline, lastline = split_to_all_but_last_and_last(
                codearea.sniptext.value)
            if lastline.endswith('()') or lastline.endswith('+)') or \
                lastline.endswith('-)') or lastline.endswith('*)') or \
                lastline.endswith('/)') or lastline.endswith(' )'):
                lastline = lastline[:-1] + text +')'
            else:
                lastline += text
            codearea.sniptext.value = allbutlastline+lastline
        whichcolumn.value = 'Choose column to insert.'
        pass

    whichcolumn.observe(column_insert, names='value')
    step3instr = richLabel(
        value='Add the calculation to the right hand side of the = using the '
              'menus to insert columns, math operations or functions. ' \
              'Your choices will be appended to the end of the last line ' \
              'or inserted within the last set of parentheses. You can also' \
              ' manually edit the expression.')
    oplst = ['Choose an operation to insert.', '+', '-', '*', '/', '**',
             'exp()', 'log10()', 'ln()', 'sqrt()', 'sin()', 'cos()',
             'tan()', 'cot()', 'asin()', 'acos()', 'atan()', 'acot()']
    whichop = Dropdown(options=oplst,
                       description='Operation: ')

    def op_insert(change):
        need_numpy = False
        np_list = ['exp()', 'log10()', 'ln()', 'sqrt()', 'sin()', 'cos()',
                   'tan()', 'cot()', 'asin()', 'acos()', 'atan()',
                   'acot()']
        op = change['new']
        if op == 'Choose an operation to insert.':
            return
        if op in np_list:
            need_numpy = True
            if op == 'ln()':
                op = 'log()'
            op = 'np.' + op
        else:
            op = ' ' + op + ' '
        if JPSLUtils.notebookenv == 'NBClassic':
            select_containing_cell('newcolGUI')
            insert_text_into_next_cell(op)
            if need_numpy:
                move_cursor_in_current_cell(-1)
        else:
            allbutlastline, lastline = split_to_all_but_last_and_last(
                codearea.sniptext.value)
            if lastline.endswith('()') or lastline.endswith('+)') or \
                lastline.endswith('-)') or lastline.endswith('*)') or \
                lastline.endswith('/)') or lastline.endswith(' )') or \
                lastline.endswith('])'):
                lastline = lastline[:-1] + op +')'
            else:
                lastline += op
            codearea.sniptext.value = allbutlastline+lastline
        whichop.value = 'Choose an operation to insert.'
        pass

    whichop.observe(op_insert, names='value')

    step3drops = HBox(children=[whichcolumn, whichop])
    step3 = VBox(children=[step3instr, step3drops])

    # Step 4
    step4instr = richLabel(
        value = 'Carefully check the expression for typos:' \
            '

Check that parentheses, brackets or braces are properly ' \
              'paired.
' \
            '
Check that all double and single quotes are also ' \
              'properly paired.
' \
            '
Check that all function calls are prefaced by ' \
              'an np..

' \
            'Uncheck "Display updated data set", if you do not wish to ' \
                'display a summary of the updated data set. ' \
            'Click \'OK\' to do final code updates. ' \
            'In the classic Jupyter notebook this button will also ' \
            'run the code and clear this GUI from the notebook.'
    )
    show_updated_df_box = Checkbox(description='Show updated data set.',
                                   value=True,
                                   layout=Layout(left='-90px'))
    gen_col_but = Button(description='      OK      ')

    def run_new_col_decl(change):
        from IPython.display import display, HTML
        from IPython.display import Javascript as JS
        # if show updated dataframe is checked append dataframe name as last line.
        if show_updated_df_box.value == True:
            text = '# Display summary of updated data set.\n'
            text += 'display('+friendly_to_globalname[whichframe.value]+')'
            if JPSLUtils.notebookenv == 'NBClassic':
                select_containing_cell('newcolGUI')
                select_cell_immediately_below()
                insert_newline_at_end_of_current_cell(text)
            else:
                allbutlastline, lastline = split_to_all_but_last_and_last(
                    codearea.sniptext.value)
                if lastline == '' or lastline == '\n':
                    codearea.sniptext.value = allbutlastline + '\n' + text
                else:
                    codearea.sniptext.value = allbutlastline + lastline + \
                                              '\n\n' + text

        # run composed operation
        if JPSLUtils.notebookenv == 'NBClassic':
            select_containing_cell('newcolGUI')
            select_cell_immediately_below()
            display(JS('Jupyter.notebook.get_selected_cell().execute()'))
            select_containing_cell('newcolGUI')
            delete_selected_cell()
        pass

    gen_col_but.on_click(run_new_col_decl)
    step4act = VBox(children=[show_updated_df_box, gen_col_but])
    step4 = HBox(children=[step4instr, step4act])

    steps = Tab(children=[step1, step2, step3, step4])
    steps.set_title(0, 'Step 1')
    steps.set_title(1, 'Step 2')
    steps.set_title(2, 'Step 3')
    steps.set_title(3, 'Step 4')

    output = Output()
    codearea = build_run_snip_widget(importstr, output)

    with output:
        display(HTML(
        "
Pandas New Calculated "
        "Column "
        "Composer
"))
        display(steps)
    if JPSLUtils.notebookenv == 'NBClassic':
        display(output)
        select_containing_cell('newcolGUI')
        new_cell_immediately_below()
        select_containing_cell('newcolGUI')
        replace_text_of_next_cell(importstr)
    else:
        with output:
            display(codearea)
        display(output)
    pass


"""sunBlog3 URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include, re_path
from django.views.static import serve
from django.conf import settings

from article import views
import notifications.urls

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.index, name='index'),
    path('about/', views.about, name='about'),
    path('download/', views.download, name='download'),
    path('article/', include('article.urls')),

    path('s/', views.article_search,name='search'),
    path('taglist/', views.article_taglist),
    path('ueditor/', include('DjangoUeditor.urls')),
    re_path('^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),
    path('comment/', include('comment.urls', namespace='comment')),
    path('account/', include('sskUser.urls', namespace='user')),
    path('accounts/', include('allauth.urls')),
    path('inbox/notifications/', include(notifications.urls, namespace='notifications')),
    path('notice/', include('notice.urls', namespace='notice')),
]


import os
from flask import Flask, request, abort, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
import random

from models import setup_db, Question, Category
from sqlalchemy.sql.expression import func

QUESTIONS_PER_PAGE = 10

def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__)
    db = setup_db(app)
    CORS(app)

    # after request
    # ----------------

    @app.after_request
    def after_request(response):
        response.headers.add(
            'Access-Control-Allow-Headers',
            'Content-Type,Authorization,true')
        response.headers.add(
            'Access-Control-Allow-Methods',
            'GET,PATCH,POST,DELETE,OPTIONS')
        # response.headers.add('Access-Control-Allow-Origin', '*')
        return response

    # get all categories
    # ---------------------------------------

    @app.route('/categories', methods=['GET'])
    def get_categories():
        all_categories = [category.format()
                          for category in Category.query.all()]

        return jsonify({
            'success': True,
            'categories': all_categories,
        })

    # get all question
    # ---------------------------------------

    @app.route('/questions', methods=['GET'])
    def get_questions():
        page = int(request.args.get('page', 1))
        start = (page - 1) * QUESTIONS_PER_PAGE
        end = start + QUESTIONS_PER_PAGE

        all_questions = Question.query.order_by('id').all()
        current_questions = [question.format()
                             for question in all_questions[start:end]]
        all_categories = [category.format()
                          for category in Category.query.all()]

        return jsonify(

            {
                "success": True,
                "total_questions": len(all_questions),
                "categories": all_categories,
                # current_category="" becuase this is all questions from all
                # categories
                "current_category": "",
                "questions": current_questions
            }
        ), 200

    # Delete Questions With id
    # ------------------------------

    @app.route('/questions/<int:id>', methods=['DELETE'])
    def delete_question(id):

        question = Question.query.get(id)
        if(question):
            question.delete()
        else:
            abort(404, "Question not found")

        return jsonify({
            "success": True,
            'message': "deleted successfully"
        }), 200

    # create new Question
    # -------------------------------

    @app.route('/questions', methods=['POST'])
    def create_questions():

        json_data = request.get_json()

        question = json_data.get("question", "")
        answer = json_data.get("answer", "")
        difficulty = json_data.get("difficulty", "")
        category = json_data.get("category", "")

        # raise 404 if any field  was empty  because we can't process the
        # requests
        if(not(category and answer and difficulty and question)):
            abort(422, "can't process the request")

        # raise 404 if category dosen't exists
        cat = Category.query.get(category)
        if(not cat):
            abort(404, "category not found")

        Question(question, answer, category, difficulty).save()

        return jsonify({
            "success": True,
            "message": "question created successfully"
        }), 201

    # search for question
    # -------------------------------

    @app.route('/questions/search', methods=['POST'])
    def search_question():

        search_term = request.get_json().get('searchTerm', "")
        if(not search_term):
            abort(422, "can't process the request")

        # case sensitive search for  questions
        questions = Question.query.filter(
            Question.question.ilike(f'%{search_term}%')).all()

        return jsonify({
            "questions": [question.format() for question in questions],
            "totalQuestions": len(questions),
            "currentCategory": "",
            'success': True
        })

    # get question by category
    # -------------------------------

    @app.route('/categories/<int:id>/questions', methods=['GET'])
    def get_category_questions(id):

        # get the category or raise 404 error if the category not found
        category = Category.query.get_or_404(id)

        questions = Question.query.filter(Question.category == id).all()
        questions = [question.format() for question in questions]

        return jsonify({
            "questions": questions,
            "totalQuestions": len(questions),
            "current_category": category.type,
            'success': True,
        })

    # get question for the quiz
    # -------------------------------

    @app.route('/quizzes', methods=['POST'])
    def get_quizzes():
        json_data = request.get_json()
        previous_questions = json_data.get('previous_questions', "")
        quiz_category = json_data.get('quiz_category').get("id", "")

        # if can't process the request raise 422 error
        if(not isinstance(previous_questions, list) or quiz_category == ""):
            abort(422, "can't process the request")

        # getting all questinos excluding the questions already exists in
        # previous_questions
        question = Question.query.filter(
            Question.id.notin_(previous_questions))

        # if category !=0 then request choose one category so we get the
        # question from that category
        if(quiz_category != 0):
            question = question.filter(Question.category == quiz_category)

        # ordering the question randomly to get new question each time and get
        # only the first one
        question = question.order_by(func.random()).first()

        quiz_category
        return jsonify({
            "success": True,
            "question": question.format() if question else None
        }), 200

    @app.errorhandler(404)
    def not_found(error):
        return jsonify({
            "success": False,
            "message": error.description if error.description else "Not Found"
        }), 404

    @app.errorhandler(405)
    def not_found(error):
        return jsonify({
            "success": False,
            "message": "method not allowed"
        }), 405

    @app.errorhandler(422)
    def not_processable(error):
        return jsonify({
            "success": False,
            "message": error.description if error.description else "Not processable"
        }), 422

    @app.errorhandler(500)
    def internal_error(error):
        return jsonify({
            "success": False,
            "message": error.description if error.description else "Internal server error"
        }), 500

    return app


from collections import defaultdict
from typing import Dict, List

import networkx as nx
import numpy as np

from grams.algorithm.data_graph import CellNode
from grams.algorithm.literal_match import TextParser
from grams.algorithm.semantic_graph import SGColumnNode
from grams.inputs.linked_table import LinkedTable
from kgdata.wikidata.models import QNode, WDProperty, WDQuantityPropertyStats

class TypeFeatureExtraction:
    Freq = "FrequencyOfType"
    FreqOverRow = "FreqOfTypeOverRow"

    def __init__(self,
                 table: LinkedTable, sg: nx.MultiDiGraph, dg: nx.MultiDiGraph,
                 qnodes: Dict[str, QNode], wdprops: Dict[str, WDProperty],
                 wd_num_prop_stats: Dict[str, WDQuantityPropertyStats]):
        self.table = table
        self.sg = sg
        self.dg = dg
        self.qnodes = qnodes
        self.wdprops = wdprops
        self.wd_num_prop_stats = wd_num_prop_stats
        self.text_parser = TextParser()

        # self.transitive_props = [p.id for p in self.wdprops.values() if p.is_transitive()]
        self.hierarchy_props = {"P131", "P276"}

    def extract_features(self):
        freq_type = {}
        freq_over_row = {}
        cell2qnodes = {}
        column2types = {}

        for uid, udata in self.sg.nodes(data=True):
            u: SGColumnNode = udata['data']
            if not u.is_column:
                continue

            # cells in this column
            cells: List[CellNode] = [self.dg.nodes[cid]['data'] for cid in u.nodes]
            covered_fractions = [
                sum(span.length for spans in cell.qnodes_span.values() for span in spans) / max(len(cell.value), 1)
                for cell in cells
                if len(cell.qnode_ids) > 0
            ]
            if len(covered_fractions) == 0:
                continue
            avg_covered_fractions = np.mean(covered_fractions)
            if avg_covered_fractions < 0.8:
                continue

            for cell in cells:
                self.add_merge_qnodes(cell, cell2qnodes)

            type2freq = defaultdict(int)
            for cell in cells:
                classes = set()
                for qnode in cell2qnodes[cell.id]:
                    for stmt in qnode.props.get("P31", []):
                        classes.add(stmt.value.as_qnode_id())
                for c in classes:
                    type2freq[c] += 1

            for c, freq in type2freq.items():
                freq_type[uid, c] = freq
                freq_over_row[uid, c] = freq / self.table.size()
            column2types[uid] = list(type2freq.keys())

        return {
            self.Freq: freq_type,
            self.FreqOverRow: freq_over_row,
            "_column_to_types": column2types
        }

    def add_merge_qnodes(self, cell: CellNode, cell2qnodes: Dict[str, List[QNode]]):
        # merge qnodes that are sub of each other
        # attempt to merge qnodes (spatial) if they are contained in each other
        # we should go even higher order
        if len(cell.qnode_ids) > 1:
            # attempt to merge qnodes (spatial) if they are contained in each other
            # we should go even higher order
            ignore_qnodes = set()
            for q0_id in cell.qnode_ids:
                q0 = self.qnodes[q0_id]
                vals = {
                    stmt.value.as_qnode_id()
                    for p in self.hierarchy_props
                    for stmt in q0.props.get(p, [])
                }
                for q1_id in cell.qnode_ids:
                    if q0_id == q1_id:
                        continue
                    if q1_id in vals:
                        # q0 is inside q1, ignore q1
                        ignore_qnodes.add(q1_id)
            cell2qnodes[cell.id] = [self.qnodes[q_id] for q_id in cell.qnode_ids if q_id not in ignore_qnodes]
        elif len(cell.qnode_ids) > 0:
            cell2qnodes[cell.id] = [self.qnodes[cell.qnode_ids[0]]]
        else:
            cell2qnodes[cell.id] = []


# Author:Fuhong Gao
#通过gevent实现单线程下的多socket并发
import socket,gevent
from gevent import monkey
monkey.patch_all()
def server(port):
    s = socket.socket()
    s.bind(('0.0.0.0',port))
    s.listen()
    while True:
        cli, addr = s.accept()
        gevent.spawn(handle_request,cli)
def handle_request(conn):
    try:
        while True:
            data = conn.recv(1024)
            print('Received:',data)
            conn.send(data)
            if not data:
                conn.shutdown(socket.SHUT_WR)
    except Exception as e:
        print(e)
    finally:
        conn.close()
if __name__ == '__main__':
    server(8001)


import datetime
def printTimeStamp(name):
    print('Овчарнеко Иван Кожушко Андрей: ' + name)
    print('Час компіляції: ' + str(datetime.datetime.now()))

day = int(input(" День: "))
month = input("Месяц: ")
if month == 'Декабрь':
	astro_sign = 'Стрелец' if (day < 22) else 'Козерог'
elif month == 'Январь':
	astro_sign = 'Козерог' if (day < 20) else 'Водолей'
elif month == 'Февраль':
	astro_sign = 'Водолей' if (day < 19) else 'Рыба'
elif month == 'Март':
	astro_sign = 'Рыба' if (day < 21) else 'Овен'
elif month == 'Апрель':
	astro_sign = 'Овен' if (day < 20) else 'Телец'
elif month == 'Май':
	astro_sign = 'Телец' if (day < 21) else 'Близнеци'
elif month == 'Июнь':
	astro_sign = 'Близнеци' if (day < 21) else 'Рак'
elif month == 'Июль':
	astro_sign = 'Рак' if (day < 23) else 'Лев'
elif month == 'Август':
	astro_sign = 'Лев' if (day < 16) else 'Дева'
elif month == 'Сентябрь':
	astro_sign = 'Дева' if (day < 24) else 'Весы'
elif month == 'Октябрь':
	astro_sign = 'Весы' if (day < 23) else 'Скорпион'
elif month == 'Ноябрь':
	astro_sign = 'Скорпион' if (day < 22) else 'Овен'
print("Знак зодиака :",astro_sign)


import torch
import os.path as osp
import torchvision.transforms as transforms
from PIL import Image
import numpy as np
import json

fine_height_E = 256 #!!!!
fine_width_E = 256
transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

MODE = ['train','test']  #test
for mode in MODE:
	img_names = []
	with open(mode+'.txt', 'r') as f:
		for line in f.readlines():
			img_names.append(line.strip())
	N = len(img_names)

	average_items = {'hat':torch.zeros(3,fine_height_E,fine_width_E),\
					 'top':torch.zeros(3,fine_height_E,fine_width_E),\
					 'bottom':torch.zeros(3,fine_height_E,fine_width_E),\
					 'shoes':torch.zeros(3,fine_height_E,fine_width_E)}
	N_items = {'hat':N, 'top':N, 'bottom':N, 'shoes':N}

	for img_name in img_names:
		im = Image.open(osp.join(mode, 'image', img_name))
		im = transforms.ToTensor()(im)

		parse_name = img_name.replace('.jpg','.png')
		im_parse = Image.open(osp.join(mode, 'image-parse', parse_name))
		parse_array = np.array(im_parse) # H*W

		parse_hat = (parse_array == 4).astype(np.float32)
		parse_top_cloth = (parse_array == 5).astype(np.float32)
		parse_bottom_cloth = (parse_array == 6).astype(np.float32)
		parse_shoes = (parse_array == 7).astype(np.float32)

		phat = torch.from_numpy(parse_hat)
		ptop = torch.from_numpy(parse_top_cloth)
		pbottom = torch.from_numpy(parse_bottom_cloth)
		pshoes = torch.from_numpy(parse_shoes)
		p_items = {'hat':phat, 'top':ptop, 'bottom':pbottom, 'shoes':pshoes}

		im_hat = im * phat + (1 - phat)		# [0,1] 3*H*W Tensor
		im_top = im * ptop + (1 - ptop)
		im_bottom = im * pbottom + (1 - pbottom)
		im_shoes = im * pshoes + (1 - pshoes)
		im_items = {'hat':im_hat, 'top':im_top, 'bottom':im_bottom, 'shoes':im_shoes}

		bbox_name = img_name.replace('.jpg','.json')
		with open(osp.join(mode, 'bbox_coordinate', bbox_name), 'r') as f:
			box = json.load(f)
		for item_name in ['hat','top','bottom','shoes']:
			try:
				coor = box[item_name]
				crop_item = im_items[item_name][:,coor['y_up']:coor['y_down'],coor['x_left']:coor['x_right']] #[-1,1]
				crop_item = transforms.ToPILImage()(crop_item)
				ratio = fine_height_E / max(crop_item.size)
				new_size = tuple([int(x*ratio) for x in crop_item.size])
				resized_item = crop_item.resize(new_size, Image.ANTIALIAS)
				new_item = Image.new("RGB",(fine_width_E,fine_height_E),color=(255,255,255))
				new_item.paste(resized_item, ((fine_width_E-new_size[0])//2,
												(fine_height_E-new_size[1])//2))  # W*H [0,255]
				assert new_item.size == (fine_width_E,fine_height_E)
				new_item = transform(new_item) # [-1,1] 3*128*128  3*H*W Tensor
				average_items[item_name] += new_item
			except TypeError:
				N_items[item_name] -= 1

	for item_name in ['hat','top','bottom','shoes']:
		average_items[item_name] /= N_items[item_name]
		np.save(osp.join(mode,"%s_%s_mean.npy" % (item_name,mode)),np.array(average_items[item_name]))
		average_show = transforms.ToPILImage()((average_items[item_name]*0.5)+0.5)
		average_show.save(osp.join(mode,"%s_%s_mean.jpg" % (item_name,mode)))
		# print(average_items[item_name])
		# print(N_items[item_name])
		# print(torch.from_numpy(np.load(osp.join(mode,"%s_%s_mean.npy" % (item_name,mode))))==average_items[item_name])

# from IPython import embed; embed()


from django.db import models

from applications.app_prestamos.models.prestamoMigrado import Plazo, Servicio

class FuenteFinanciamiento(models.Model):
    GENDER_TIPO = (
        ("1", "Recursos propios"),
        ("2", "Fideicomiso"),
        ("3", "Banca comercial"),
        ("4", "Otros"),
    )
    idFuenteFinanciamiento = models.IntegerField(verbose_name="ID", primary_key=True)
    nombre = models.CharField(
        max_length=55, verbose_name="Nombre", blank=True, null=True
    )
    tipo = models.CharField(
        max_length=1, choices=GENDER_TIPO, verbose_name="Tipo", blank=True, null=True
    )
    puntos = models.DecimalField(
        verbose_name="Puntos", blank=True, null=True, max_digits=6, decimal_places=2
    )

    def __str__(self):
        return self.nombre

class PrestamoLineaCredito(models.Model):
    lineaCredito = models.IntegerField(
        verbose_name="Linea de credito", blank=True, null=True
    )
    ultimaMinistracion = models.IntegerField(
        verbose_name="Ultima administracion", blank=True, null=True
    )
    monto = models.IntegerField(verbose_name="Monto", blank=True, null=True)
    numeroAbonos = models.IntegerField(
        verbose_name="Numero de Abonos", blank=True, null=True
    )
    plazo = models.ForeignKey(
        Plazo, on_delete=models.CASCADE, verbose_name="Plazo", blank=True, null=True
    )
    servicio = models.ForeignKey(
        Servicio,
        on_delete=models.CASCADE,
        verbose_name="Servicio",
        blank=True,
        null=True,
    )
    fuenteFinanciamiento = models.ForeignKey(  # primer
        FuenteFinanciamiento,
        on_delete=models.CASCADE,
        verbose_name="Fuente de financiamiento",
        blank=True,
        null=True,
    )


""" Price controller """
from logging import log, DEBUG
#from decimal import Decimal
from typing import List
from flask import Blueprint, request, render_template

try:
    import simplejson as json
except ImportError:
    import json
from gnucash_portfolio.bookaggregate import BookAggregate
from gnucash_portfolio.lib import datetimeutils
from gnucash_portfolio.lib.csv_parser import CsvPriceParser
from pricedb import PriceModel, SecuritySymbol
from gnucash_portfolio_webui.models.price_models import (
    PriceImportViewModel, PriceImportInputModel, PriceImportSearchViewModel)
from gnucash_portfolio_webui.models.generic_models import ValidationResult

price_controller = Blueprint(  # pylint: disable=invalid-name
    'price_controller', __name__, url_prefix='/price')

@price_controller.route('/')
def index():
    """ Index page for prices """
    return render_template('price.html')

@price_controller.route('/latest')
def latest_prices():
    """ Displays the latest available prices """
    # todo: read from pricedb
    from pricedb import PriceDbApplication, PriceModel

    app = PriceDbApplication()
    latest: List[PriceModel] = app.get_latest_prices()
    model = {
        "prices": latest
    }
    return render_template("price.latest.html", model=model)

@price_controller.route('/download')
def download_page():
    """ Page for downloading the latest prices """
    model = {}
    return render_template('price.download.html', model=model)

@price_controller.route('/downloadprices')
def download_action():
    """ Download the prices. Not active yet. """
    import sys
    from pricedb import PriceDbApplication

    app = PriceDbApplication()
    app.download_prices()
    #sys.stdout.flush()

@price_controller.route('/downloadfor/<path:symbol>')
def download_price_for(symbol):
    """ Downloads and displays the price for given symbol """
    from pricedb import PriceDbApplication

    s = SecuritySymbol("", "")
    s.parse(symbol)

    app = PriceDbApplication()
    app.download_prices(namespace=s.namespace, symbol=s.mnemonic)

    return latest_prices()

# @price_controller.route('/download/<path:symbol>')
# def download(symbol):
#     """ download the latest price for security """
#     model = {
#         "symbol": symbol
#     }
#     return render_template('price.download.html', model=model)

# @price_controller.route('/import')
# def import_prices(message: str = None):
#     """ Stock price import. Data input. """
#     model = {}
#     if message:
#         model["message"] = message

#     with BookAggregate() as svc:
#         ref = __load_search_reference_model(svc)
#         search = None

#         return render_template('price.import.html', model=model, search=search, ref=ref)

@price_controller.route('/review', methods=['POST'])
def import_post():
    """ Review the prices from the uploaded file (.csv) """
    input_model = __read_review_input_model()

    validation = __validate_review_input_model(input_model)
    if not validation.valid:
        return import_prices(validation.message)

    try:
        parser = CsvPriceParser(input_model.currency)
        prices = parser.parse_prices_from_file_stream(input_model.csv_file)
    except ValueError as value_error:
        return import_prices(value_error)

    # Display the prices for review.
    with BookAggregate() as svc:
        # View model
        ref = __load_search_reference_model(svc)

        model = PriceImportViewModel()
        model.filename = input_model.csv_file.filename
        model.prices = prices

        return render_template('price.import.confirm.html',
                               model=model, search=input_model, ref=ref)

@price_controller.route('/load', methods=['POST'])
def load_prices():
    """ Imports .csv prices into database. """
    # Read user input.
    input_model: PriceImportInputModel = __read_review_input_model()

    validation = __validate_review_input_model(input_model)
    if not validation.valid:
        return import_prices(validation.message)

    # Get prices from file.
    try:
        parser = CsvPriceParser(input_model.currency)
        prices = parser.parse_prices_from_file_stream(input_model.csv_file)
    except ValueError as value_error:
        return import_prices(value_error)

    # Import prices.
    with BookAggregate(for_writing=True) as svc:
        result = svc.prices.import_prices(prices)
        svc.save()

    with BookAggregate() as svc:
        model = PriceImportViewModel()
        model.filename = input_model.csv_file.filename
        model.prices = prices

        return render_template('price.import.result.html', model=model, result=result)

@price_controller.route('/for/<symbol>')
def prices_for_symbol(symbol):
    """ displays all prices for symbol """
    with BookAggregate() as svc:
        sec_agg = svc.securities.get_aggregate_for_symbol(symbol)
        prices = sec_agg.get_prices()
        model = {
            "symbol": symbol,
            "prices": prices
        }
        return render_template('security.prices.html', model=model)

###############################################
# API

@price_controller.route('/api/create', methods=['POST'])
def api_create():
    """ Create a new price if it does not exist, or update existing for the date """
    symbol = request.json.get("symbol")
    currency = request.json.get("currency")

    date_str = request.json.get("date")
    date_val = datetimeutils.parse_us_date(date_str[:10])

    price_str = request.json.get("price")

    model = PriceModel()
    model.symbol = SecuritySymbol()
    model.symbol.parse(symbol)
    model.currency = currency
    model.value = Decimal(price_str)
    model.rate_date = date_val

    with BookAggregate(for_writing=True) as svc:
        success = svc.prices.import_price(model)
        svc.save()

    result = {
        "success": success,
        "message": "check the success"
    }
    return json.dumps(result)

###############################################
# Private

def __read_review_input_model() -> PriceImportInputModel:
    """ Read input model on price load """
    result = PriceImportInputModel()

    result.currency = request.form.get("search.currency")
    result.csv_file = request.files['import_file']

    return result

def __validate_review_input_model(model: PriceImportInputModel):
    """ Validates user input """
    result = ValidationResult()
    result.valid = False

    file = model.csv_file
    if not file:
        result.message = "No file selected"
        return result

    if file.content_type != "application/octet-stream":
        result.message = "Wrong file type submitted"
        return result

    assert file.filename != ''

    # Validation passed.
    result.valid = True
    return result

def __load_search_reference_model(svc: BookAggregate) -> PriceImportSearchViewModel:
    """ Populates the reference data for the search form """
    model = PriceImportSearchViewModel()

    # cur_svc = CurrenciesAggregate(svc.book)
    model.currencies = svc.currencies.get_book_currencies()

    return model


__author__ = "Cody Wild"

"""
This Spark parser was used on the full dataset to pull a sample of 'sentences' - i.e. internally coherent units from within the _idata code section
of the .asm file. This 50% sample - stored in the form of a list of lists of tokens - was then used to train a Word2Vec model specific to the _data section
"""

import re
from pyspark import SparkContext
import random

def sentenceParse(fileStr, type="tup"):
    startTrigger = "_idata"
    triggered = False
    sentenceTokens = []
    listOfSentences = []

    if type == "tup":
        fileList = fileStr[1].split('\n')
    elif type == "list":
        fileList = fileStr
    elif type == "string":
        fileList = fileStr.split('\n')
    for line in fileList:
        if startTrigger in line:
            triggered = True
            continue
        if triggered:
            textDisqualify = re.match('\.idata:[0-9A-F]*\s*; \.text:[0-9A-F]*.*', line)
            xrefDisqualify = re.match('\.idata:[0-9A-F]*\s*;.* XREF', line)
            beginSentenceMatch = re.match('\.idata:[0-9A-F]*\s*;(.*)', line)
            endSentenceMatch = re.match('\.idata:[0-9A-F]*\s*(?:\?\?\s)+\s*(.*);.*', line)
            middleSentenceMatch = re.match('\.idata:[0-9A-F]*\s*(?:\?\?\s)+\s*(.*)(?!;)', line)
            stillInSectionMatch = re.match('\.idata:.*', line)

            if not stillInSectionMatch:
                continue
            if endSentenceMatch or textDisqualify or xrefDisqualify:
                if endSentenceMatch:
                    sentenceTokens.extend(endSentenceMatch.groups(1)[0].split())

                if '_idata' in sentenceTokens:
                    sentenceTokens.remove('_idata')
                if sentenceTokens:
                    listOfSentences.append(sentenceTokens)
                sentenceTokens = []
                continue
            if beginSentenceMatch:
                sentenceTokens.extend(beginSentenceMatch.groups(1)[0].split())
                continue
            if middleSentenceMatch:
                sentenceTokens.extend(middleSentenceMatch.groups(1)[0].split())
                continue

    return listOfSentences

if __name__ == "__main__":
    sc = SparkContext()
    sc.wholeTextFiles("s3n://malwaredata/train/*.asm", 4000)\
        .sample(False, 0.5)\
        .flatMap(sentenceParse)\
        .coalesce(100).saveAsTextFile("s3n://malwaredata/w2v/idata/fiftyPerc/idataSentences" + str(round(random.random(), 2)))


import requests
import json
from constant import *
from threading import Timer
from datetime import datetime

data = {}

def getBankId(bank):  # Get bank name by bank ID
    if bank == 'Vietcombank':
        return 'vcb'
    if bank == 'Vietinbank':
        return 'ctg'
    if bank == 'Techcombank':
        return 'tcb'
    if bank == 'BIDV':
        return 'bid'
    if bank == 'Sacombank':
        return 'stb'
    if bank == 'SBV':
        return 'sbv'

def getBankData(bank, token):  # Get bank data by bank
    # Resquest to get currency
    api_url = 'https://vapi.vnappmob.com/api/v2/exchange_rate/%s'
    api_url_bank = api_url % (str(getBankId(bank)))
    response = requests.get(
        api_url_bank, headers={'Authorization': 'Bearer ' + token})
    return json.loads(response.content)["results"]

def getData():
    global data
    # Resquest to get token for authorization
    api_key = requests.get(
        'https://vapi.vnappmob.com/api/request_api_key?scope=exchange_rate&fbclid=IwAR1LiVpNea58gxXeGZgPX6QWRRJQpkFfS_r41PJRsuy2z7-1uurKflwhGWo')
    token = json.loads(api_key.content)["results"]

    for item in BANK_LIST:
        data[item] = getBankData(item, token)

    return data

def updateData():
    global data
    data = getData()
    global DATETIME
    DATETIME = str(datetime.now().strftime("%d/%m/%Y %H:%M:%S"))
    Timer(1800, updateData).start()

def getCurrencyBank(bank, type_currency):
    list = ["0", "0", "0", "0", "0"]
    for item in data[bank]:
        if item["currency"] == type_currency:
            list[0] = bank
            list[1] = item["currency"]
            list[4] = item["sell"]
            for i in item:
                if list[2] == "0" and (i == "buy" or i == "buy_cash"):
                    list[2] = str(item[i])
                if list[3] == "0" and i == "buy_transfer":
                    list[3] = str(item[i])
    return list

def getCurrency(bank, type_currency):  # Get sell currency
    # Find currency
    list = []
    if bank == "All" and type_currency == "All":
        for i in BANK_LIST:
            for j in TYPE_CURRENCY_LIST:
                list_item = getCurrencyBank(i, j)
                list.append(list_item)
    elif bank == "All":
        for i in BANK_LIST:
            list_item = getCurrencyBank(i, type_currency)
            list.append(list_item)
    elif type_currency == "All":
        for j in TYPE_CURRENCY_LIST:
            list_item = getCurrencyBank(bank, j)
            list.append(list_item)
    else:
        list_item = getCurrencyBank(bank, type_currency)
        list.append(list_item)

    return list