# 1. Дороги
# trails_sum = 0
# n = int(input())
# for x in range(n):
#     str_matrix = input()
#     str_list_int = list(map(int, str_matrix.split()))
#     trails_sum += sum(str_list_int[x+1:])
#
# print(trails_sum)

# 2. Шамбала
# def draw_x(matrix, start_point, side):
#     finish_point = start_point + side
#     for i in range(start_point, finish_point):
#         for j in range(start_point, finish_point):
#             if i == finish_point-1 or i == start_point or j == finish_point-1 or j == start_point:
#                 matrix[i][j] = 'X'
#
#
# n = int(input())
# n = 2 * n - 1
# start_point = 0
# matrix = [[' '] * n for x in range(n)]
# for side in range(n, 0, -4):
#     draw_x(matrix, start_point, side)
#     start_point += 2
#
# for x in matrix:
#     print(''.join(map(str, x)))

# 3. Ход конём
# pos = input()
# x1 = ord(pos[0])-64
# y1 = int(pos[1])
# x2 = ord(pos[3])-64
# y2 = int(pos[4])
# if (abs(x2-x1) == 2 and abs(y2-y1) == 1) or (abs(x2-x1) == 1 and abs(y2-y1) == 2):
#     print('YES')
# else:
#     print('NO')

# 4. Равнобедренные треугольники

def createstring(symbols):
    str_res = ' ' * symbols
    str_res = '*' + str_res[1:-1] + ('*' if len(str_res)>1 else '')
    return str_res

n = int(input())
numbers_strings = 2 * n - 1
for x in range(0, n):
    print(createstring(x+1))
for x in range(n-1, 0, -1):
    print(createstring(x))

2.Разминка/2.2 Матрица.py

2.2 Матрица.py


import yt_dlp
import urllib
import json

def yt_dlp_version():
    return yt_dlp.version.__version__

def query_video(page_url):
    '''
    query_video: queries metadata for the video in page_url using yt_dlp
    Returns 'None' on error
    '''
    ydl_opts = {'nocheckcertificate' : True,
                'quiet': True,
                'restrictfilenames': True,
                'format': 'bestaudio/best'
               }
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        try:
            info_dict = ydl.extract_info(urllib.parse.unquote(page_url), download=False)
            return json.dumps(info_dict, indent=4)
        except:
            return None

if __name__ == '__main__':
    r = query_video("https://www.youtube.com/watch?v=ye2CLllRO8I")
    print(r)


import sys
import os
from PIL import Image, ImageEnhance, ImageDraw, ImageFilter
import numpy as np
from shapes import Punto, Triangulo
from delaunay import triangular

USAGE = "error: Espicifique un archivo imagen para realizar triangulación de delaunay"
MIN_PUNTO_DISTANCIA = 5
GRIS_CLARO = 20

def main():
    if len(sys.argv) <= 1:
        print(USAGE)
        return

    imagen_path = sys.argv[1]

    try:
        imagen_original = Image.open(imagen_path)
    except:
        print(f"No se podía encontrar la imagen '{imagen_path}'")
        return

    imagen_modificada = modificar_imagen(imagen_original)

    puntos = encontrar_puntos(imagen_modificada)
    triangulos = triangular(puntos)

    new_path = os.path.splitext(imagen_path)[0] + "-delaunay.png"
    draw_triangulacion(imagen_original, triangulos, new_path)

def modificar_imagen(imagen: Image) -> Image:
    imagen_modificada = imagen.convert('L') # greyscale
    imagen_modificada = imagen_modificada.filter(ImageFilter.FIND_EDGES) # encontrar bordes, la imangen sale muy oscura
    imagen_modificada = ImageEnhance.Brightness(imagen_modificada).enhance(10.0) # agrega brillo a la imagen
    imagen_modificada = imagen_modificada.filter(ImageFilter.GaussianBlur(1)) # blur

    return imagen_modificada

def draw_triangulacion(imagen: Image, triangulos: set[Triangulo], path: str):
    draw = ImageDraw.Draw(imagen)

    for tri in triangulos:
        for edge in tri.edges:
            draw.line([(edge.p1.x, edge.p1.y), (edge.p2.x, edge.p2.y)], fill="black", width=1)

    imagen.save(path)

def encontrar_puntos(imagen: Image) -> list[Punto]:
    pixeles = np.array(imagen)
    imagen_largo = len(pixeles)
    puntos: list[Punto] = []

    for y in range(0, imagen_largo, MIN_PUNTO_DISTANCIA):
        imagen_ancho = len(pixeles[y])
        for x in range(0, imagen_ancho, MIN_PUNTO_DISTANCIA):
            color_valor = pixeles[y][x]
            if color_valor >= GRIS_CLARO:
                puntos.append(Punto(x, y))

    return puntos

if __name__ == '__main__':
    main()


# -*- coding:utf-8 -*-
import sys
import numpy as np
import pandas as pd
import seaborn as sns
from hmmlearn.hmm import GaussianHMM
from matplotlib import pyplot as plt
import tushare as tu
import seaborn as sns

n = 6  # 六个隐藏状态
beginTime = '2015-1-1'
endTime = '2018-12-1'
data = tu.get_hist_data('sh', beginTime, endTime,'D')
high = data['high']
low = data['low']

volume = data['volume']
close = data['close'][5:]
Date = pd.to_datetime(data.index[5:])
print(Date)
logDel = (np.log(np.array(high)) - np.log(np.array(low)))[5:]
logRet1 = np.array(np.diff(np.log(close)))[5:]
logRet5 = np.log(np.array(close[5:])) - np.log(np.array(close[:-5]))  # 指數對數收益差
logVol5 = np.log(np.array(volume[5:])) - np.log(np.array(volume[:-5]))  # 指數對數交易量差
plt.hist(logVol5,200,normed=1,facecolor='green',alpha=0.75)
# plt.show()
# print(logDel)
A = np.column_stack([logDel[:100], logRet5[:100], logVol5[:100]])#1D-2D
print(A)
model = GaussianHMM(n_components=6, covariance_type='diag', n_iter=2000).fit(A)
hidden_states = model.predict(A)
print(hidden_states)
plt.figure(figsize=(10, 5))
sns.set_style('white')
for i in range(model.n_components):
        pos = (hidden_states==i)
        plt.plot(Date[i], close[i], 'o', label='hidden state %d' % i, lw=360)
        plt.legend()
        plt.grid(10)
plt.show()


import argparse
import logging
import os
import shutil
import sys
import tempfile
import traceback

import yaml

from cros.factory.test.env import paths
from cros.factory.utils import file_utils
from cros.factory.utils.process_utils import Spawn

DESCRIPTION = '''
Audits source hashes logged during system finalization
to verify that no unauthorized changes have been made to
a factory bundle.  Hashes are compared against a "golden"
(known to be correct) set of hashes.

Each value for GOLDEN or SAMPLE may be any of the following:

- the 'py' directory of a factory source tree
- a factory log bundle (with name ending in '.tar.xz')
- a factory toolkit (with name ending in '.run')
- an event log file (in which case the last 'source_hashes' event
  logged is examined)

For example, to compare multiple factory logs against a "golden"
toolkit:

  audit_source_hashes --golden install_factory_toolkit.run logs/*.tar.xz
'''

class AuditException(Exception):
  pass

def GetHashes(path):
  """Gets a dictionary of hashes for the given path.

  Args:
    path: A path to any of the accepted file types (see DESCRIPTION).

  Returns:
    A dictionary of paths to their hashes.
  """
  if os.path.isdir(path):
    if os.path.basename(path) != 'py':
      raise AuditException(
          '%s is a directory, but not a path to a "py" source directory' % path)
    return file_utils.HashSourceTree(path)['hashes']

  if path.endswith('.run'):
    tmpdir = tempfile.mkdtemp(prefix='toolkit.')
    try:
      # Extract and hash toolkit
      Spawn([path, '--tar', '-axf', '-C', tmpdir, './usr/local/factory/py'],
            check_call=True, log=True)
      return file_utils.HashSourceTree(
          os.path.join(tmpdir, 'usr', 'local', 'factory', 'py'))['hashes']
    finally:
      shutil.rmtree(tmpdir)

  # None of the above: it's either an event log file, or a report
  # containing an event log file.
  if path.endswith('.tar.xz'):
    # Looks like a report.  Extract the event log data.
    proc = Spawn(['tar', '-Oaxf', path, 'events'], read_stdout=True)
    if proc.returncode:
      raise AuditException(
          'Unable to read events from report %s (tar returned %d)' % (
              path, proc.returncode))
    data = proc.stdout_data
  else:
    # Assume it's an event log.  Look for the event specifically
    # to avoid a bunch of unnecessary YAML parsing.
    data = file_utils.ReadFile(path)

  events = data.split('\n---\n')
  for e in reversed(events):
    if not e.startswith('EVENT: source_hashes'):
      continue
    data = yaml.load(e)

    hash_function = data.get('hash_function')
    if hash_function != file_utils.SOURCE_HASH_FUNCTION_NAME:
      raise ValueError(
          'Expected hash function %r but got %r' % (
              file_utils.SOURCE_HASH_FUNCTION_NAME,
              hash_function))
    return data['hashes']
  raise AuditException(
      'No source_hashes event in event log %s' % path)

def FindMismatches(golden_hashes, sample_hashes, sample, out):
  error_count = [0]

  def ReportLine(path, msg):
    if error_count[0] == 0:
      out.write('In sample %s:\n' % sample)
    out.write('- %s: %s\n' % (path, msg))
    error_count[0] += 1

  all_keys = sorted(set(sample_hashes.keys()) |
                    set(golden_hashes.keys()))

  for k in all_keys:
    sample_value = sample_hashes.get(k)
    golden_value = golden_hashes.get(k)
    if golden_value is None and sample_value is not None:
      ReportLine(k, 'unexpected file encountered in sample')
    elif sample_value is None and golden_value is not None:
      ReportLine(k, 'missing from sample')
    elif sample_value != golden_value:
      ReportLine(k, 'hash mismatch (expected %s, found %s)' % (
          golden_value, sample_value))

  return error_count[0]

def AuditHashes(golden, samples, out):
  """Audits source hashes (see DESCRIPTION).

  Args:
    golden: Path to golden used for analysis.
    samples: A list of samples to compare against.

  Returns:
    True if all values are correct, False otherwise.
  """
  golden_hashes = GetHashes(golden)

  total_bad_samples = 0
  total_mismatched_hashes = 0
  total_exceptions = 0
  for s in samples:
    try:
      sample_hashes = GetHashes(s)
      mismatched_hashes = FindMismatches(golden_hashes,
                                         sample_hashes, s, out)
    except Exception:
      out.write('Error processing sample %s\n' % s)
      traceback.print_exc(file=out)
      total_exceptions += 1
      total_bad_samples += 1
    else:
      total_mismatched_hashes += mismatched_hashes
      if mismatched_hashes:
        total_bad_samples += 1

  if total_bad_samples:
    out.write('\n'
              'Found %d mismatched hashes and %d exceptions.\n'
              'FAILED (%d/%d samples passed).\n' % (
                  total_mismatched_hashes,
                  total_exceptions,
                  len(samples) - total_bad_samples,
                  len(samples)))
    return False
  out.write('PASSED (%d/%d samples passed).\n' % (
      len(samples), len(samples)))
  return True

def main(argv=None, out=sys.stderr):
  parser = argparse.ArgumentParser(
      description=DESCRIPTION,
      formatter_class=argparse.RawDescriptionHelpFormatter)
  parser.add_argument(
      '--golden', '-g', metavar='GOLDEN',
      help='Source of "golden" (correct) hashes; defaults to this source tree')
  parser.add_argument(
      'samples', metavar='SAMPLE', nargs='+',
      help='Samples to check against the golden')
  args = parser.parse_args(sys.argv[1:] if argv is None else argv)
  logging.basicConfig(level=logging.WARNING)

  if args.golden is None:
    args.golden = os.path.join(paths.FACTORY_DIR, 'py')

  sys.exit(0 if AuditHashes(args.golden, args.samples, out) else 1)

if __name__ == '__main__':
  main()


#!/usr/bin/env python
# Need to install jsk-visualisation library with
#sudo apt-get install -y ros-melodic-jsk-visualization
# Please run rviz by rosrun rviz rviz -d `rospack find jsk_rviz_plugins`/config/pictogram.rviz

# code copied from https://app.theconstructsim.com/#/Desktop
import rospy
import math
from jsk_rviz_plugins.msg import Pictogram, PictogramArray
from random import random, choice
from geometry_msgs.msg import Point
from nav_msgs.msg import Odometry

def goal_callback(data):
    global cur_location, destination
    if data:
        destination = data#the point we are telling the robot to go to.
        cur_pose = rospy.wait_for_message("/thorvald_001/base_link", Odometry)#current pose of the robots odomertry
        cur_location = cur_pose.pose.pose.position# gets current position of the robot from odometry

rospy.init_node("pictogram_object_demo_node")

cur_location = destination = Point()

p = rospy.Publisher("/pictogram_array", PictogramArray,  queue_size=1)
goal_sub = rospy.Subscriber("/robot_goal", Point, goal_callback)#goal is going to be at the start of grapes

r = rospy.Rate(1)
pictograms = ["fa-building","location"]

while not rospy.is_shutdown():

    arr = PictogramArray()
    arr.header.frame_id = "thorvald_001/base_link"#this would be odom but the odom seems to be at a different location than the robot itself.
    arr.header.stamp = rospy.Time.now()
    for index, character in enumerate(pictograms):
        msg = Pictogram()
        msg.header.frame_id = "thorvald_001/base_link"
        msg.action = Pictogram.JUMP
        if index == 0:
            msg.header.stamp = rospy.Time.now()
            msg.pose.position = cur_location
        else:
            msg.header.stamp = rospy.Time.now()
            msg.pose.position = destination

        # It has to be like this to have them vertically orient the icons.
        msg.pose.orientation.w = 0.7
        msg.pose.orientation.x = 0
        msg.pose.orientation.y = -0.7
        msg.pose.orientation.z = 0
        msg.size = 1
        msg.color.r = 25 / 255.0
        msg.color.g = 255 / 255.0
        msg.color.b = 240 / 255.0
        msg.color.a = 1.0
        msg.character = character
        arr.pictograms.append(msg)

    p.publish(arr)
    r.sleep()


#!/usr/bin/env python
import sqlite3
import os
import database as gemini_db
import gemini_utils as util

def append_variant_info(main_curr, chunk_db):
    """
    Append the variant and variant_info data from a chunk_db
    to the main database.
    """

    cmd = "attach ? as toMerge"
    main_curr.execute(cmd, (chunk_db, ))

    main_curr.execute("BEGIN TRANSACTION")
    cmd = "INSERT INTO variants SELECT * FROM toMerge.variants"
    main_curr.execute(cmd)

    cmd = \
        "INSERT INTO variant_impacts SELECT * FROM toMerge.variant_impacts"
    main_curr.execute(cmd)
    main_curr.execute("END TRANSACTION")

    cmd = "detach toMerge"
    main_curr.execute(cmd)

def append_sample_genotype_counts(main_curr, chunk_db):
    """
    Append the sample_genotype_counts from a chunk_db
    to the main database.
    """
    cmd = "attach ? as toMerge"
    main_curr.execute(cmd, (chunk_db, ))

    cmd = "INSERT INTO sample_genotype_counts \
           SELECT * FROM toMerge.sample_genotype_counts"
    main_curr.execute(cmd)

    cmd = "detach toMerge"
    main_curr.execute(cmd)

def append_sample_info(main_curr, chunk_db):
    """
    Append the sample info from a chunk_db
    to the main database.
    """
    cmd = "attach ? as toMerge"
    main_curr.execute(cmd, (chunk_db, ))

    cmd = "create table samples as select * from toMerge.samples where 1=0"
    main_curr.execute(cmd)

    cmd = "INSERT INTO samples SELECT * FROM toMerge.samples"
    main_curr.execute(cmd)

    cmd = "detach toMerge"
    main_curr.execute(cmd)

def append_resource_info(main_curr, chunk_db):
    """
    Append the resource info from a chunk_db
    to the main database.
    """
    cmd = "attach ? as toMerge"
    main_curr.execute(cmd, (chunk_db, ))

    cmd = "INSERT INTO resources SELECT * FROM toMerge.resources"
    main_curr.execute(cmd)

    cmd = "detach toMerge"
    main_curr.execute(cmd)

def append_version_info(main_curr, chunk_db):
    """
    Append the version info from a chunk_db
    to the main database.
    """
    cmd = "attach ? as toMerge"
    main_curr.execute(cmd, (chunk_db, ))

    cmd = "INSERT INTO version SELECT * FROM toMerge.version"
    main_curr.execute(cmd)

    cmd = "detach toMerge"
    main_curr.execute(cmd)

def append_gene_summary(main_curr, chunk_db):
    """
    Append the gene_summary from a chunk_db
    to the main database.
    """
    cmd = "attach ? as toMerge"
    main_curr.execute(cmd, (chunk_db, ))

    cmd = "INSERT INTO gene_summary SELECT * FROM toMerge.gene_summary"
    main_curr.execute(cmd)

    cmd = "detach toMerge"
    main_curr.execute(cmd)

def append_gene_detailed(main_curr, chunk_db):
    """
    Append the gene_detailed from a chunk_db
    to the main database.
    """
    cmd = "attach ? as toMerge"
    main_curr.execute(cmd, (chunk_db, ))

    cmd = "INSERT INTO gene_detailed SELECT * FROM toMerge.gene_detailed"
    main_curr.execute(cmd)

    cmd = "detach toMerge"
    main_curr.execute(cmd)

def update_sample_genotype_counts(main_curr, chunk_db):
    """
    Update the main sample_genotype_counts table with the
    counts observed in one of the chunked databases (chunk_db)
    """
    curr_db_conn = sqlite3.connect(chunk_db)
    curr_db_conn.isolation_level = None
    curr_db_conn.row_factory = sqlite3.Row
    curr_db_curr = curr_db_conn.cursor()

    cmd = "SELECT sample_id, num_hom_ref, \
                  num_het, num_hom_alt, \
                  num_unknown FROM sample_genotype_counts"
    curr_db_curr.execute(cmd)
    for row in curr_db_curr:
        main_curr.execute("""UPDATE sample_genotype_counts
                          SET num_hom_ref = num_hom_ref + ?,
                              num_het = num_het + ?,
                              num_hom_alt = num_hom_alt + ?,
                              num_unknown = num_unknown + ?
                          WHERE sample_id= ? """,
                          (row['num_hom_ref'],
                           row['num_het'],
                           row['num_hom_alt'],
                           row['num_unknown'],
                           row['sample_id']))
    curr_db_curr.close()

def merge_db_chunks(args):

    # open up a new database
    if os.path.exists(args.db):
        os.remove(args.db)

    main_conn = sqlite3.connect(args.db)
    main_conn.isolation_level = None
    main_curr = main_conn.cursor()
    main_curr.execute('PRAGMA synchronous = OFF')
    main_curr.execute('PRAGMA journal_mode=MEMORY')
    # create the gemini database tables for the new DB
    gemini_db.create_tables(main_curr)

    databases = []
    for database in args.chunkdbs:
        databases.append(database)

    for idx, database in enumerate(databases):

        db = database[0]

        append_variant_info(main_curr, db)

        # we only need to add these tables from one of the chunks.
        if idx == 0:
            append_sample_genotype_counts(main_curr, db)
            append_sample_info(main_curr, db)
            append_resource_info(main_curr, db)
            append_version_info(main_curr, db)
            append_gene_summary(main_curr, db)
            append_gene_detailed(main_curr, db)
        else:
            update_sample_genotype_counts(main_curr, db)

    main_conn.commit()
    main_curr.close()

def merge_chunks(parser, args):
    merge_db_chunks(args)


from flask import Flask, jsonify, render_template, request
import os
import training
import chatbot

app = Flask(__name__)

@app.route("/")
def home():
    return render_template("index.html")

@app.route("/get")
def get_bot_response():
    userText = request.args.get('msg')
    ints = chatbot.predict_class(userText)
    return chatbot.get_response(ints, chatbot.intents)

if __name__ == '__main__':
    app.run(debug=True, port=os.getenv("PORT", default=5000))


import pygame
import random
import math

# Initialize the game engine
pygame.init()

BLACK = [0, 0, 0]
WHITE = [255, 255, 255]

WIDTH = 500
HEIGHT = 500
# Set the height and width of the screen
SIZE = [WIDTH, HEIGHT]

screen = pygame.display.set_mode(SIZE)
pygame.display.set_caption("Ball Animation")

BLACK = (  0,   0,   0)
WHITE = (255, 255, 255)
RED   = (255,   0,   0)
# Create an empty array
ball_list = pygame.sprite.Group()

def sign(n):
    if n > 0:
        return -1
    else:
        return 1

def random_color():
    return (random.randrange(0, 244), random.randrange(0, 244), random.randrange(0, 244))

class Ball(pygame.sprite.Sprite):

    def __init__(self, size, x, y, velocity, theta, elasticity, forces, friction_coefficient):

        super().__init__()

        self.size = size
        self.friction_coefficient = friction_coefficient
        self.elasticity = elasticity

        self.image = pygame.Surface([self.size, self.size])
        self.image.fill(WHITE)
        self.image.set_colorkey(WHITE)

        self.rect = self.image.get_rect()

        self.rect.x = x
        self.rect.y = HEIGHT - y

        self.v_x = velocity * math.cos(theta)
        self.v_y = -1 * velocity * math.sin(theta)

        self.a_x = 0
        self.a_y = 0

        self.forces = forces

        # Draw the ellipse
        pygame.draw.ellipse(self.image, (random.randrange(0, 244), random.randrange(0, 244), random.randrange(0, 244)),[0, 0, self.size, self.size])

    def apply_force(self, force):
        self.a_x += force[0]
        self.a_y += -1 * force[1]

    def update(self):

        for force in self.forces:
            self.apply_force(force)

        #bogus friction (messes things up when v is too high or too low)
        friction_x = (self.v_x ** 2)*sign(self.v_x)*self.friction_coefficient
        friction_y = (self.v_y ** 2)*sign(self.v_y)*self.friction_coefficient*-1

        #friction_y is multiplied by -1 because y increases as the ball goes down

        self.apply_force([friction_x, friction_y])

        self.v_x += self.a_x
        self.v_y += self.a_y

        self.rect.x += self.v_x
        self.rect.y += self.v_y

        self.a_x = 0
        self.a_y = 0

        self.collision()

    def collision(self):
        if self.rect.x > WIDTH-self.size or self.rect.x < self.size:
            # Reset it just above the top
            self.v_x = -1 * self.v_x * self.elasticity

            if(self.rect.x < self.size):
                self.rect.x = self.size

            if(self.rect.y > WIDTH-self.size):
                self.rect.y = WIDTH-self.size

        if self.rect.y < 0 or self.rect.y > HEIGHT - self.size:

            if(self.rect.y < 0):
                self.rect.y = 0

            if(self.rect.y > HEIGHT - self.size):
                self.rect.y = HEIGHT - self.size

            self.v_y = -1 * self.v_y * self.elasticity

clock = pygame.time.Clock()

all_sprite_list = pygame.sprite.Group()

all_sprite_list.add(Ball(x=50,
            y=50,
            size=10,
            velocity=50,
            theta=math.pi/2.5,
            forces=[[0, -0.9]],
            elasticity=.9,
            friction_coefficient=0.01
            ))

# Loop until the user clicks the close button.
done = False
while not done:

    for event in pygame.event.get():   # User did something
        if event.type == pygame.QUIT:  # If user clicked close
            done = True   # Flag that we are done so we exit this loop

    all_sprite_list.draw(screen)
    all_sprite_list.update()

    pygame.display.flip()
    clock.tick(100)

pygame.quit()


import pghistory
import model_utils.fields
import mptt.fields
import django.utils.timezone
from django.contrib.auth.models import Group
from django.db import models
from model_utils.models import SoftDeletableModel, TimeStampedModel
from mptt.models import MPTTModel, TreeForeignKey

from attendees.whereabouts.models import Organization

class Menu(MPTTModel, TimeStampedModel, SoftDeletableModel):
    """
    Todo: Raise ValueError if the instance.get_level() > 1 due to Boostrap 4 dropdown-submenu limit,
    or smartmenus will be needed.

    UniqueConstraint(fields=['organization', 'category', 'html_type', 'url_name'], condition=models.Q(is_removed=False))
    cannot be used since some menu repatitive for different user groups

    """

    ATTENDEE_CREATE_VIEW = "attendee_create_view"
    ATTENDEE_UPDATE_VIEW = "attendee_update_view"
    ATTENDEE_UPDATE_SELF = "attendee_update_self"

    id = models.BigAutoField(
        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
    )

    organization = models.ForeignKey(
        Organization,
        null=False,
        blank=False,
        default=0,
        on_delete=models.SET(0),
        help_text="Organization of the menu",
    )

    category = models.CharField(
        max_length=32,
        null=False,
        blank=False,
        db_index=True,
        default="main",
        help_text="Type of menu, such as 'main', 'side', etc",
    )

    parent = TreeForeignKey(
        "self", on_delete=models.SET(-1), null=True, blank=True, related_name="children"
    )

    html_type = models.CharField(
        max_length=50,
        blank=True,
        null=False,
        help_text="HTML tags such as div or a. For API it can be blank",
    )

    urn = models.CharField(
        max_length=255,
        blank=True,
        null=True,
        help_text="use relative path (including leading & ending slash '/') such as /app/division/assembly/page-name",
    )

    url_name = models.SlugField(
        max_length=255,
        blank=False,
        null=False,
        db_index=True,
        help_text="view name of the path, such as 'assembly_attendances', 'divider between index and register links', "
        "etc. For API it's class name",
    )

    display_name = models.CharField(
        max_length=50,
        blank=False,
        null=False,
        help_text="description of the path, such as 'Character index page', 'divider between index and register "
        "links', etc",
    )

    display_order = models.SmallIntegerField(default=0, blank=False, null=False)

    infos = models.JSONField(
        null=True,
        blank=True,
        default=dict,
        help_text="HTML attributes & more such as {'class': 'dropdown-item'}. Please keep {} here even no data.",
    )

    auth_groups = models.ManyToManyField(
        Group, through="MenuAuthGroup", related_name="auth_groups"
    )

    class MPTTMeta:
        order_insertion_by = ["display_order", "display_name"]

    class Meta:
        db_table = "users_menus"

    def __str__(self):
        return "%s %s %s URN: ...%s" % (
            self.organization_slug,
            self.category.upper(),
            self.display_name,
            self.urn[-40:] if self.urn else "",
        )

    @property
    def organization_slug(self):
        return self.organization.slug if self.organization else ""

    @staticmethod
    def user_can_create_attendee(user, url_name=ATTENDEE_CREATE_VIEW):
        return Menu.objects.filter(
            auth_groups__in=user.groups.all(),
            url_name=url_name,
            menuauthgroup__write=True,
            is_removed=False,
        ).exists()

class MenusHistory(pghistory.get_event_model(
    Menu,
    pghistory.Snapshot('menu.snapshot'),
    pghistory.BeforeDelete('menu.before_delete'),
    name='MenusHistory',
    related_name='history',
)):
    pgh_id = models.BigAutoField(primary_key=True, serialize=False)
    pgh_created_at = models.DateTimeField(auto_now_add=True)
    pgh_label = models.TextField(help_text='The event label.')
    pgh_obj = models.ForeignKey(db_constraint=False, on_delete=models.deletion.DO_NOTHING, related_name='history', to='users.menu')
    id = models.IntegerField(db_index=True)
    created = model_utils.fields.AutoCreatedField(default=django.utils.timezone.now, editable=False, verbose_name='created')
    modified = model_utils.fields.AutoLastModifiedField(default=django.utils.timezone.now, editable=False, verbose_name='modified')
    organization = models.ForeignKey(db_constraint=False, default=0, help_text='Organization of the menu', on_delete=models.deletion.DO_NOTHING, related_name='+', related_query_name='+', to='whereabouts.organization')
    is_removed = models.BooleanField(default=False)
    tree_id = models.PositiveIntegerField(editable=False)
    level = models.PositiveIntegerField(editable=False)
    lft = models.PositiveIntegerField(editable=False)
    rght = models.PositiveIntegerField(editable=False)
    display_order = models.SmallIntegerField(default=0)
    category = models.CharField(default='main', help_text="Type of menu, such as 'main', 'side', etc", max_length=32)
    url_name = models.SlugField(db_index=False, help_text="view name of the path, such as 'assembly_attendances', 'divider between index and register links', etc. For API it's class name", max_length=255)
    display_name = models.CharField(help_text="description of the path, such as 'Character index page', 'divider between index and register links', etc", max_length=50)
    infos = models.JSONField(blank=True, default=dict, help_text="HTML attributes & more such as {'class': 'dropdown-item'}. Please keep {} here even no data.", null=True)
    parent = mptt.fields.TreeForeignKey(blank=True, db_constraint=False, null=True, on_delete=models.deletion.DO_NOTHING, related_name='+', related_query_name='+', to='users.menu')
    urn = models.CharField(blank=True, help_text="use relative path (including leading & ending slash '/') such as /app/division/assembly/page-name", max_length=255, null=True)
    html_type = models.CharField(blank=True, help_text='HTML tags such as div or a. For API it can be blank', max_length=50)
    pgh_context = models.ForeignKey(db_constraint=False, null=True, on_delete=models.deletion.DO_NOTHING, related_name='+', to='pghistory.context')

    class Meta:
        db_table = "users_menushistory"


import json
import os

EXCEPTIONS_KYUJITAI = {
    '缺缺': '欠缺',
    '共倭國': '共和國',
}

EXCEPTIONS_SHINJITAI = {
    '欠欠': '欠缺',
}

class BasicConverter(object):
    """
    Basic converter, only converting Shinjitai to Kyujitai, and vice versa (WITHOUT kakikae)
    """

    def __init__(self):

        # Determine Shinjitai/Kyujitai database path
        current_path = os.path.abspath(os.path.dirname(__file__))
        kyujitai_db_path = os.path.join(current_path, 'kyujitai.json')

        # Parse Kyujitai database
        kyujitai_db_file = open(kyujitai_db_path, 'r', encoding="utf-8")
        self.kyujitai_data = json.load(kyujitai_db_file)
        kyujitai_db_file.close()

        # Build Shinjitai to Kyujitai conversion databases
        self.shinjitai_to_kyujitai_database = {}
        self.kyujitai_to_shinjitai_database = {}

        # create Shinjitai/Kyujitai dictionaries
        for entry in self.kyujitai_data:
            shinjitai = entry[0]
            kyujitai = entry[1]
            self.shinjitai_to_kyujitai_database[shinjitai] = kyujitai
            self.kyujitai_to_shinjitai_database[kyujitai] = shinjitai

    def shinjitai_to_kyujitai(self, input_string):

        # convert individual characters
        for char in self.shinjitai_to_kyujitai_database:
            input_string = input_string.replace(char, self.shinjitai_to_kyujitai_database[char])

        # process conversion exceptions
        for word in EXCEPTIONS_KYUJITAI:
            input_string = input_string.replace(word, EXCEPTIONS_KYUJITAI[word])

        return input_string

    def kyujitai_to_shinjitai(self, input_string):

        # convert individual characters
        for char in self.kyujitai_to_shinjitai_database:
            input_string = input_string.replace(char, self.kyujitai_to_shinjitai_database[char])

        # process conversion exceptions
        for word in EXCEPTIONS_SHINJITAI:
            input_string = input_string.replace(word, EXCEPTIONS_SHINJITAI[word])

        return input_string

class KyujitaiConverter(object):
    """
    Full converter, converting Shinjitai to Kyujitai, and vice versa (WITH kakikae)
    """

    def __init__(self):

        # Use BasicConverter to convert individual Shinjitai/Kyujitai characters
        self.basic_converter = BasicConverter()

        # Determine Kakikae databases paths
        current_path = os.path.abspath(os.path.dirname(__file__))
        kakikae_simplified_db_path = os.path.join(current_path, 'kakikae_simplified.json')
        kakikae_variants_db_path = os.path.join(current_path, 'kakikae_variants.json')

        # Parse Kakikae database
        kakikae_simplified_db_file = open(kakikae_simplified_db_path, 'r', encoding="utf-8")
        self.kakikae_database_simplified = json.load(kakikae_simplified_db_file)
        kakikae_simplified_db_file.close()
        kakikae_variants_db_file = open(kakikae_variants_db_path, 'r', encoding="utf-8")
        self.kakikae_database_variants = json.load(kakikae_variants_db_file)
        kakikae_variants_db_file.close()

        # Build Kakikae conversion databases
        self.kakikae_encode_database = {}
        self.kakikae_decode_database = {}

        # First step: index of all potential words (build database keys)
        for entry in self.kakikae_database_simplified:
            new_char = entry['new']
            # Encode (Shinjitai to Kyujitai) database keys
            for word in entry.get('words'):
                # add word if not already in database
                if word not in self.kakikae_encode_database:
                    self.kakikae_encode_database[word] = word
            # Decode (Kyujitai to Shinjitai) database keys
            for old_char in entry['old']:
                for word in entry.get('words'):
                    word = word.replace(new_char, old_char)
                    if word not in self.kakikae_decode_database:
                        self.kakikae_decode_database[word] = word
        # Variants database only used for decoding (Kyujitai to Shinjitai conversion)
        for entry in self.kakikae_database_variants:
            new_char = entry['new']
            for old_char in entry['old']:
                for word in entry.get('words'):
                    word = word.replace(new_char, old_char)
                    if word not in self.kakikae_decode_database:
                        self.kakikae_decode_database[word] = word

        # Second step: replace Kakikae characters (build database values)
        for entry in self.kakikae_database_simplified:
            new_char = entry['new']
            base_old_char = entry['old'][0]
            # Encode (Shinjitai to Kyujitai) database values
            for word in entry.get('words'):
                self.kakikae_encode_database[word] = self.kakikae_encode_database[word].replace(new_char, base_old_char)
            # Decode (Kyujitai to Shinjitai) database values
            for old_char in entry['old']:
                for word in entry.get('words'):
                    word = word.replace(new_char, old_char)
                    self.kakikae_decode_database[word] = self.kakikae_decode_database[word].replace(old_char, new_char)
        # Variants database only used for decoding (Kyujitai to Shinjitai conversion)
        for entry in self.kakikae_database_variants:
            new_char = entry['new']
            for old_char in entry['old']:
                for word in entry.get('words'):
                    word = word.replace(new_char, old_char)
                    self.kakikae_decode_database[word] = self.kakikae_decode_database[word].replace(old_char, new_char)

    def shinjitai_to_kyujitai(self, input_string):

        # revert douon no kanji ni yoru kakikae
        for word in self.kakikae_encode_database:
            input_string = input_string.replace(word, self.kakikae_encode_database[word])

        # convert remaining individual characters
        input_string = self.basic_converter.shinjitai_to_kyujitai(input_string)

        return input_string

    def kyujitai_to_shinjitai(self, input_string):

        # convert individual characters first
        input_string = self.basic_converter.kyujitai_to_shinjitai(input_string)

        # apply douon no kanji ni yoru kakikae
        for word in self.kakikae_decode_database:
            input_string = input_string.replace(word, self.kakikae_decode_database[word])

        return input_string


#!/usr/bin/env python

import csv
import os
import re
import sys
import yaml
import copy

signs = {
    r'\(f\)': "female",
    r'\(m\)': "male",
    r'\(v\)': "verb",
    r'\(adj\.\)': "adjective",
    r'\(adj\.': "adjective",
    r'\(adv\.\)': "adverb",
    r'\(adv\)': "adverb",
    r'\(prep\.\)': "preposition",
    r'\([pP]ron\.\)': "pronoun",
}

accents = {
    'á': 'a',
    'í': 'i',
    'é': 'e',
    'ó': 'o',
    'ú': 'u',
    'ü': 'u',
}
words_regex = f"^[ a-zA-Z{''.join(sorted(accents.keys()))}.-]+(\.\.\.|!|\?)?$"

def remove_accent(ladino):
    ladino = re.sub(r'á', 'a', ladino)
    ladino = re.sub(r'í', 'i', ladino)
    ladino = re.sub(r'é', 'e', ladino)
    ladino = re.sub(r'ó', 'o', ladino)
    ladino = re.sub(r'ú', 'u', ladino)

    return ladino

def get_grammar_types():
    types = {
        "female": False,
        "verb": False,
        "adjective": False,
        "adverb": False,
        "preposition": False,
        "pronoun": False,
    }
    for sign, grammar_type in signs.items():
        regex = r'^(.*?)\s+' + sign + r'\s*$'
        match = re.search(regex, ladino)
        if match:
            types[grammar_type] = True
            ladino = match.group(1)

def _err(msg):
    print(f"ERROR {msg}", file=sys.stderr)

def save_words(all_words):
    with open("words.txt", "w") as fh:
        for word in all_words:
            print(word, file=fh)

def save_librelingo_format(dictionary):
    data = {
        'Two-way-dictionary': dictionary,
    }
    filename = "ladinokomunita.yaml"
    #print(data)
    with open(filename, 'w') as fh:
        fh.write("# This is a generated file. Do not edit manually!\n\n")
        fh.write("Skill:\n")
        fh.write("  Name: Ladinokomunita\n")
        fh.write("  Id: 10000\n\n")
        fh.write("New words: []\n")
        fh.write("Phrases: []\n\n")
        yaml.dump(data, fh, Dumper=yaml.Dumper, allow_unicode=True, indent=4)

def save_yaml_format(full):
    filename = "dictionary.yaml"
    data = {
        'words': full
    }
    with open(filename, 'w') as fh:
        yaml.dump(full, fh, Dumper=yaml.Dumper, allow_unicode=True, indent=4)

def _extract_words(word):
    if ',' in word:
        return re.split(r'\s*,\s*', word)
    else:
        return word

def main():
    #filename = "diksionario_biervos_excel_corrected.csv"
    filename = "biervos.csv"
    dictionary = []
    full = []
    all_words = set()
    with open(filename) as fh:
        # Palavra,Eshemplos,Espanyol,English,Turkish,Origen,id,Portuguese,French
        rd = csv.DictReader(fh, delimiter=',')

        count = 50
        for row in rd:
            ladino = row['Palavra']
            eshemplos = row.get('Eshemplos')
            examples = []
            if eshemplos is not None and eshemplos != '':
                examples = [{
                    'ladino': eshemplos,
                }]
            entry = {
                'origen'     : row['Origen'],
                'id'         : row['id'],
                'examples'   : examples,
                'versions': [{
                    'translations': {
                        'english'    : _extract_words(row['English']),
                        'spanish'    : _extract_words(row['Espanyol']),
                        'turkish'    : _extract_words(row['Turkish']),
                        'portuguese' : _extract_words(row['Portuguese']),
                        'french'     : _extract_words(row['French']),
                    },
                }]
            }
            #print(ladino)
            #print(english)
            if ladino is None:
                _err(f"Ladino is None in {row}")
                continue

            match = re.search(r'^(?P<words>[^()]+?)\s*(\((?P<grammar>.*)\))?\s*$', ladino)
            if match is None:
                _err(f"Ladino does not match it is '{ladino}'")
                continue
            grammar = match.group('grammar')
            words_str = match.group('words')
            #print(words_str)
            words = re.split(r'\s*[/,]\s*', words_str)
            word = words[0]
            match = re.search(words_regex, word)
            if not match:
                _err(f"Word '{word}' does not match our rules from Ladino {ladino}")
                continue
            #print(word)
            if entry['versions'][0]['translations']['english'].__class__.__name__ == 'list':
                translated_words = entry['versions'][0]['translations']['english']
            else:
                translated_words = [entry['versions'][0]['translations']['english']]
            for translated_word in translated_words:
                dictionary.append({translated_word: word})
            plain_word = remove_accent(word)
            all_words.add(plain_word)
            #if plain_word in full:
            #    prev = copy.deepcopy(full[plain_word])
            #    #prev_accented = prev.pop('accented')
            #    _err(f"Ladino word '{plain_word}' already exists. (original '{word}' row: {row})")
            #    print(prev, file=sys.stderr)
            #    print(entry, file=sys.stderr)
            #    print('-----', file=sys.stderr)
            #    continue
            #full[plain_word] = copy.deepcopy(entry)
            data = copy.deepcopy(entry)
            data['versions'][0]['accented'] = word
            data['versions'][0]['ladino'] = plain_word
            #grammar_types = get_grammar_types(grammar)
            print(grammar)
            if grammar == 'v':
                grammar = 'verb'
                data['conjugations'] = {}
            elif grammar == 'f':
                grammar = None
                data['versions'][0]['gender'] = 'feminine'
                data['versions'][0]['number'] = 'singular'
            elif grammar == 'm':
                grammar = None
                data['versions'][0]['gender'] = 'masculine'
                data['versions'][0]['number'] = 'singular'
            elif grammar is None:
                pass
            elif grammar in ['adv.', 'adv']:
                grammar = 'adverb'
            elif grammar == 'adj.':
                grammar = 'adjective'
            elif grammar == 'prep.':
                grammar = 'preposition'
            elif grammar in ['pron.', 'Pron.']:
                grammar = 'pronoun'
            else: #grammar in ['adj.+ m/f', 'm+pron.', 'n+adj.', 'm/p', 'm/f']: #, 'm), arabá (f', 'distribuir', 'la', 'en teatro']:
                data['comments'] = [f'grammar: {grammar}']
                grammar = None
            data['grammar'] = grammar

            if len(words) > 1:
                data['versions'][0]['alternative-spelling'] = []
                for alt_word in words[1:]:
                    data['versions'][0]['alternative-spelling'].append({
                        'ladino': remove_accent(alt_word),
                        'accented': alt_word,
                    })

            if len(sys.argv) == 2 and sys.argv[1] == 'all':
                full.append(data)
            else:
                # Normally just save one:
                word_file = f"words/{data['versions'][0]['ladino'].lower()}.yaml"
                if os.path.exists(word_file):
                    exit(f"File {word_file} already exists")
                print(f"{word_file}\nword: {data['versions'][0]['ladino'].lower()}\nid: {data['id']}")
                with open(word_file, 'w') as fh:
                    yaml.dump(data, fh, Dumper=yaml.Dumper, allow_unicode=True, indent=4)
                count -= 1
                if count <= 0:
                    exit()

    #save_librelingo_format(dictionary)
    save_yaml_format(sorted(full, key=lambda this: this['versions'][0]['ladino']))
    #save_words(all_words)

            #if grammar is None:
            #    print(ladino)

            #ladino_words = [ladino]
            #if re.search(r'/', ladino):
            #    ladino_words = ladino.split('/')
            #elif re.search(r',', ladino):
            #    ladino_words = ladino.split(',')
            #elif re.search(r';', ladino):
            #    ladino_words = ladino.split(';')

            #err = False
            #for ladino in ladino_words:
            #    if not re.search(r'^[A-Za-z ]+(\s?\.\.\.)?$', ladino):
            #        #print(f"ERROR ladino word '{ladino}' has unhandled characer in this row: {row}")
            #        err = True
            #        continue
            #if err:
            #    continue

            #for ladino in ladino_words:
            #    ladino = ladino.strip(' ')
            #    #if re.search('^la\s+[a-zA-Z]+$', ladino):
            #    print(ladino)

            #print(row)
            #exit()

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-
"""
Created on Wed Mar  2 21:09:10 2022

@author: rsibaja
"""

p1 = int(input("Digite el valor de su inversion"))
p2 = int(input("Digite el valor de su inversion"))
p3 = int(input("Digite el valor de su inversion"))
total = p1 + p2 + p3
porcentajep1 = p1 / total * 100
porcentajep2 = p2 / total * 100
porcentajep3 = p3 / total * 100
print(f' el porcentaje de la inversion de la primera persona es:  {porcentajep1:,}')
print(f' el porcentaje de la inversion de la segunda persona es:  {porcentajep2:,}')
print(f' el porcentaje de la inversion de la tercera persona es:  {porcentajep3:,}')


"""Replica of icanhazepoch.com - HTTP Style"""

from time import time
from wsgiref.simple_server import BaseHTTPRequestHandler, HTTPServer
# Dirty import, but works both on Python 2 and Python 3

class EpochHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/plain; charset=utf-8')
        self.end_headers()
        self.wfile.write('{0:.0f}'.format(time()).encode('UTF-8'))

if __name__ == '__main__':
    HTTPServer(('0.0.0.0', 8000), EpochHandler).serve_forever()


from typing import List

from fastapi import APIRouter, Depends, status, HTTPException
from sqlalchemy.orm import Session

from app.database.models import Deliveryman as DeliverymanModel
from app.database.connection import get_db
from app.schemas.deliveryman import DeliverymanResponse
from app.schemas.deliveryman import DeliverymanBase as DeliverymanRequest

router = APIRouter()

@router.get("", response_model=List[DeliverymanResponse])
def get_all_deliverymans(
    db: Session = Depends(get_db),
):
    return db.query(DeliverymanModel).all()

@router.get(
    "/{id_deliveryman}",
    response_model=DeliverymanResponse,
)
def get_deliveryman_by_id(
    id_deliveryman: int,
    db: Session = Depends(get_db),
):

    deliveryman_in_db = db.query(DeliverymanModel).get(id_deliveryman)
    if not deliveryman_in_db:
        raise HTTPException(status_code=404)

    return deliveryman_in_db

@router.post(
    "",
    response_model=DeliverymanResponse,
    status_code=status.HTTP_201_CREATED,
)
def create_deliveryman(
    deliveryman: DeliverymanRequest,
    db: Session = Depends(get_db),
):
    db_deliveryman = DeliverymanModel(
        name=deliveryman.name,
        vehicle=deliveryman.vehicle,
    )
    db.add(db_deliveryman)
    db.commit()

    return db_deliveryman

@router.put(
    "/{id_deliveryman}",
    response_model=DeliverymanResponse,
)
def update_deliveryman(
    id_deliveryman: int,
    deliveryman: DeliverymanRequest,
    db: Session = Depends(get_db),
):
    deliveryman_in_db = db.query(DeliverymanModel).get(id_deliveryman)
    if not deliveryman_in_db:
        raise HTTPException(status_code=404)

    deliveryman_in_db.name = deliveryman.name
    deliveryman_in_db.vehicle = deliveryman.vehicle
    db.commit()

    return deliveryman_in_db


from datetime import datetime, time, timedelta
from typing import Tuple

from wakeapp.utils.regex_utils import check_string

class TimeController:
    def __init__(self, arrival:datetime=None, depature:datetime=None) -> None:
        if arrival is None:
            self.arrival = datetime.now()
        if depature is None:
            self.depature = datetime.now()

    def calculate_travel_time(self) -> Tuple[float,float,float]:
        """Return a Tuple of hour, minute and second calculated by the internal arrival and depature time"""
        time_diff = self.arrival - self.depature
        return map(float, (str(time_diff).split(":")))

    def get_wakeup_datetime(self, get_ready: str) -> datetime:
        hours, minutes, seconds = self.calculate_travel_time()
        travel_time = timedelta(seconds=seconds,minutes=minutes, hours=hours)
        get_ready_hour, get_ready_minutes = map(float,get_ready.split(':'))
        get_ready_timedelta = timedelta(hours=get_ready_hour,minutes=get_ready_minutes)
        print(travel_time)
        print(get_ready_timedelta)
        print(self.depature)
        return (self.depature - get_ready_timedelta)

    def convert_string_to_time_object(time: str) -> time | None:
        if (check_string(time, r"^[012]\d:[0-5]\d$")):
            return None
        (hour, minutes) = time.split(":")
        return time(hour=hour, minute=minutes)


from __future__ import print_function
from time import time
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
from sklearn.decomposition import NMF, LatentDirichletAllocation
from sklearn.datasets import fetch_20newsgroups
import pickle
import collections
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.model_selection import StratifiedShuffleSplit
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import f1_score
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
import sklearn.svm
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from nltk import word_tokenize
from nltk.stem import WordNetLemmatizer
from time import time

def print_top_words(model, feature_names, n_top_words):
    for topic_idx, topic in enumerate(model.components_):
        message = "Topic #%d: " % topic_idx
        message += " ".join([feature_names[i]
                             for i in topic.argsort()[:-n_top_words - 1:-1]])
        print(message)
    print()

#__________________________________

def loadDataset(pklPath):
    with open(pklPath, "rb") as pklFile:
        return np.array(pickle.load(pklFile, encoding="utf-8"))

def splitData(startString, dataSet, divisions=100):
    newString = startString
    for i in dataSet:
        newString= newString + str(i)
    #return newString
    newList = []
    div = len(newString)//100
    temp = ""
    for j in range(len(newString)):
        temp = temp + newString[j]
        if j%div == 0:
            newList.append(temp)
            temp = ""
    newList.append(temp)
    return newList

c_data = splitData("", loadDataset("consPapersNew.pkl"))
d_data = splitData("", loadDataset("deonPapersNew.pkl"))
final_data_set = c_data  + d_data

y = []
for i in c_data:
    y.append('cons')
for i in d_data:
    y.append('deon')

class LemmaTokenizer(object):
    def __init__(self):
        self.wnl = WordNetLemmatizer()
    def __call__(self, doc):
        return [self.wnl.lemmatize(t) for t in word_tokenize(doc)]

stop_words = ['xe2', 'xe', 'fetus', 'sv', 'ac', 'sydney', 'x80', 'user', 'abortion', 'xxxviii', 'kagan', 'parfit', 'oxford', 'new york university', 'midwest', '``', '[', '\'\'', '\\\\xe2', '&', 'user\\\\non', '0812', '2018', ']', '\\\\xe2\\\\x80\\\\x94', 'york', r'user\\\\non', 'user\\non', r'user\\non', r'\\xe2\\x80\\x94', r'\\\\xe2\\\\x80\\\\x94', 'id',
    '\\xe2\\x80\\x94', 'by new university', 'new university march', 'university march', '( ).',
    'donaldson', '<', ': )', 'jones', 'nora', 'university', 'march'  ]
for i in range(0, 3000):
    stop_words.append(str(i))

vectorizer = TfidfVectorizer(ngram_range=(1, 3),token_pattern=r'\b\w+\b', tokenizer=LemmaTokenizer(), stop_words=stop_words, strip_accents='ascii', max_df=.7, )

X = vectorizer.fit_transform(final_data_set)

nComponents = 20

lda = LatentDirichletAllocation( max_iter=5,
                                learning_method='online',
                                learning_offset=50,
                                random_state=0)

lda.fit(X)

n_top_words = 10
print("\nTopics in LDA model:")
tf_feature_names = vectorizer.get_feature_names()
print_top_words(lda, tf_feature_names, n_top_words)


import base64
import io
import json

import numpy as np
import pandas as pd
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization, hashes
from cryptography.hazmat.primitives.asymmetric import padding
from django.contrib.auth.models import User
from django.db import transaction
from rest_framework_simplejwt.tokens import AccessToken
from uniprotparser.betaparser import UniprotParser

def get_user_from_token(request):
    if 'HTTP_AUTHORIZATION' in request.META:
        authorization = request.META['HTTP_AUTHORIZATION'].replace("Bearer ", "")
        access_token = AccessToken(authorization)
        user = User.objects.filter(pk=access_token["user_id"]).first()
        if user:
            return user
    return

def is_user_staff(request):
    user = request.user
    if not user:
        user = get_user_from_token(request)
    if user:
        if user.is_staff:
            return True
    return False

def delete_file_related_objects(file):
    for c in file.comparisons.all():
        with transaction.atomic():
            for column in c.differential_sample_columns.all():
                column.delete()
            for da in c.differential_analysis_datas.all():
                da.delete()
    with transaction.atomic():
        for r in file.raw_datas.all():
            r.delete()
    for rc in file.raw_sample_columns.all():
        with transaction.atomic():
            rc.delete()

def calculate_boxplot_parameters(values):
    q1, med, q3 = np.percentile(values, [25, 50, 75])

    iqr = q3 - q1

    low = q1 - 1.5 * iqr
    high = q3 + 1.5 * iqr

    wisker_high = np.compress(values <= high, values)
    wisker_low = np.compress(values >= low, values)

    real_high_val = np.max(wisker_high)
    real_low_val = np.min(wisker_low)

    return {"low": real_low_val, "q1": q1, "med": med, "q3": q3, "high": real_high_val}

def check_nan_return_none(value):
    if pd.notnull(value):
        return value
    return None

def get_uniprot_data(df, column_name):
    primary_id = df[column_name].str.split(";")
    primary_id = primary_id.explode().unique()
    parser = UniprotParser()
    uni_df = []
    for p in parser.parse(primary_id):
        uniprot_df = pd.read_csv(io.StringIO(p), sep="\t")
        uni_df.append(uniprot_df)
    if len(uni_df) == 1:
        uni_df = uni_df[0]
    elif len(uni_df) > 1:
        uni_df = pd.concat(uni_df, ignore_index=True)
    else:
        return pd.DataFrame()
    uni_df.set_index("From", inplace=True)
    return uni_df

def encrypt_data(public_key, data: bytes):
    loaded_key = serialization.load_pem_public_key(
        public_key,
        backend=default_backend()
    )
    encrypted = base64.b64encode(loaded_key.encrypt(
        data,
        padding.OAEP(
            mgf=padding.MGF1(algorithm=hashes.SHA256()),
            algorithm=hashes.SHA256(),
            label=None
        )
    ))
    return encrypted


"""
Asset class. Part of the StoryTechnologies project.

July 19, 2016
Brett Alistair Kromkamp (brett.kromkamp@gmail.com)
"""

from storyengine.core.coreerror import CoreError

class Asset:

    def __init__(self, instance_of, reference='', data=None):

        if instance_of not in {'image', 'video', 'sound', 'scene', 'html', 'text', 'augmented-reality'}:
            raise CoreError("Unrecognized 'instance of' parameter")
        self.reference = reference
        self.instance_of = instance_of
        self.data = data


#!/usr/bin/python

import time
import subprocess
import concurrent.futures
import numpy as np
from decimal import *
from threading import Event
from sense_hat import SenseHat
from MotorDriver import *
from ConnectionSenseHat import *
from modules.PCA9685 import PCA9685
from multiprocessing import Value, Array, Process
from gpiozero import RotaryEncoder, Button
from gpiozero.pins.pigpio import PiGPIOFactory
#from WitMotionSensorConnection import JY901S

U_MAX = 100
U_MIN = 0
SPEED = 0
OFFSET = 1
FWD = 1
BCK = 0

# ロータリーエンコーダのピン設定
PIN_ROTAR_A1 = 17
PIN_ROTAR_A2 = 27
PIN_ROTAR_D1 = 22
PIN_ROTAR_D2 = 23

# ロータリーエンコーダ初期化
rotation_a = FWD
rotation_d = FWD
def rotated_clockwise_a():
    global rotation_a
    rotation_a = BCK

def rotated_clockwise_d():
    global rotation_d
    rotation_d = BCK

def rotated_counter_clockwise_a():
    global rotation_a
    rotation_a = FWD

def rotated_counter_clockwise_d():
    global rotation_d
    rotation_d = FWD

# Wit Motion
'''
obj = None
device = None
# WitMotion角度センサ初期化
devicePath = "/dev/ttyUSB0"
if os.path.exists("/dev/ttyUSB1"):
    devicePath = "/dev/ttyUSB1"
obj = JY901S()
device = obj.getDevice()
device.serialConfig.portName = devicePath   # Set serial port
device.serialConfig.baud = 9600                     # Set baud rate
device.openDevice()                                 # Open serial port
# Read configuration information
obj.readConfig(device)
# Data update event
device.dataProcessor.onVarChanged.append(obj.onUpdate)
'''
'''
# sense hatを初期化
sense = SenseHat()
angle = []
# sense hat指示値読み替え用配列定義
for i in range(360):
    if i <= 180:
        angle.append(i)
    else:
        angle.append(i - 360)
'''
# sense hatの指示値読み取りをスレッド化
def read_sense_hat_angle(th_angle_y):
    while True:
        angle_y = subprocess.run("cat /opt/angle_y", encoding='utf-8', stdout=subprocess.PIPE, shell=True)
        angle_y = str(angle_y.stdout)
        if angle_y == '' or angle_y == None:
            continue
        else:
            th_angle_y.value = int(angle_y)

getcontext().prec = 28
def request_controler(th_angle_y):
    factory = PiGPIOFactory()
    rotary_encoder_a = RotaryEncoder(
            PIN_ROTAR_A1, PIN_ROTAR_A2, wrap=True, max_steps=180, pin_factory=factory
    )
    rotary_encoder_a.steps = 0
    rotary_encoder_a.when_rotated_clockwise = rotated_clockwise_a
    rotary_encoder_a.when_rotated_counter_clockwise = rotated_counter_clockwise_a

    rotary_encoder_d = RotaryEncoder(
            PIN_ROTAR_D1, PIN_ROTAR_D2, wrap=True, max_steps=180, pin_factory=factory
    )
    rotary_encoder_d.steps = 0
    rotary_encoder_d.when_rotated_clockwise = rotated_clockwise_d
    rotary_encoder_d.when_rotated_counter_clockwise = rotated_counter_clockwise_d

    # 積算開始
    start = time.time()

    before_count_a = 0
    before_count_d = 0
    rt_a_counter = 0
    rt_d_counter = 0
    prev_angle_y = 0

    direction = [
        'forward',
        'backward',
    ]

    # PWM制御
    pwm = PCA9685(0x40, debug=False)
    pwm.setPWMFreq(50)
    motor_driver = MotorDriver()
    pwm.setPWMFreq(50000000.0)

    # 制御パラメータ設定
    k1 = Decimal(str(363.41817119))
    k2 = Decimal(str(43.69756113))
    k3 = Decimal(str(7.46787171))
    k4 = Decimal(str(6.50978926))

    # モータ初期化
    direction_a = 0
    direction_d = 0
    motor_driver.MotorRun(pwm, 0, direction[direction_a], SPEED)
    motor_driver.MotorRun(pwm, 1, direction[direction_d], SPEED)

    getcontext().prec = 28
    while True:
        # 振子の回転角度を取得
        angle_y = Decimal(str(th_angle_y.value))
        delta_angle_y = Decimal(angle_y - prev_angle_y)
        rt_a_counter = rotary_encoder_a.steps
        rt_d_counter = rotary_encoder_d.steps
        # １ステップ前からのモータ回転角度
        delta_theta_a = Decimal(abs(abs(rt_a_counter) - abs(before_count_a)))
        delta_theta_d = Decimal(abs(abs(rt_d_counter) - abs(before_count_d)))
        if rotation_a == BCK:
            delta_theta_a = -delta_theta_a
        if rotation_d == BCK:
            delta_theta_d = -delta_theta_d
        # motorの振子に対する角度
        motor_a_angle = delta_theta_a - delta_angle_y
        motor_d_angle = delta_theta_d - delta_angle_y
        '''
        if np.sign(delta_theta_a) == np.sign(delta_angle_y):
            motor_a_angle = delta_theta_a - delta_angle_y
        else:
            motor_a_angle = delta_angle_y - delta_theta_a
        if np.sign(delta_theta_d) == np.sign(delta_angle_y):
            motor_d_angle = delta_theta_d - delta_angle_y
        else:
            motor_d_angle = delta_angle_y - delta_theta_d
        '''
        delta_time = Decimal(time.time() - start)
        start = time.time()
        # 振子の角速度
        if delta_angle_y == 0:
            angular_velocity_y = Decimal(0)
        else:
            angular_velocity_y = Decimal(delta_angle_y/delta_time)
        # モータの角速度
        velocity_a = Decimal(delta_theta_a/delta_time)
        velocity_d = Decimal(delta_theta_d/delta_time)

        # U(t)を導出
        ua = ((angle_y * k1) + (angular_velocity_y * k2) + (motor_a_angle * k3) + (velocity_a * k4))
        ud = ((angle_y * k1) + (angular_velocity_y * k2) + (motor_d_angle * k3) + (velocity_d * k4))

        # 回転方向を設定
        if ua < 0:
            direction_a = BCK
        else:
            direction_a = FWD

        if ud < 0:
            direction_d = BCK
        else:
            direction_d = FWD
        ua = abs(ua)
        ud = abs(ud)

        # 系に対してPWMデューティ比として与える制御入力をスケーリング
        if ua != U_MIN:
            ua = round(Decimal(0.02) * ua)
        if ud != U_MIN:
            ud = round(Decimal(0.02) * ud)

        # デューティ比の最大値を超過する場合は足切り
        if ua > U_MAX:
            ua = U_MAX
        if ud > U_MAX:
            ud = U_MAX

        print("angle_y  : " + str(angle_y)
                    + "\tangular_velocity_y  : " + str(angular_velocity_y)
                    + "\t motor angle : " + str(motor_a_angle)
                    + "\t motor velocity : " + str(velocity_a)
                    + "\t motor delta_theta : " + str(delta_theta_a)
                    + "\t direction : " + direction[direction_a]
                    + "\t u(t)  : " + str(ua)
                    + "\t delta : " + str(delta_time))

        # deleta theata計測用
        before_count_a = rt_a_counter
        before_count_d = rt_d_counter
        prev_angle_y = angle_y

        # モータへデューティ比を印加
        motor_driver.MotorRun(pwm, 0, direction[direction_a], ua)
        motor_driver.MotorRun(pwm, 1, direction[direction_d], ua)
        # ハードウェア側の応答を見て待ち時間を設定
        #time.sleep(0.002)

if __name__ == "__main__":
    th_angle_y = Value('i', 0)
    p1 = Process(target=request_controler, args=[th_angle_y])
    p2 = Process(target=read_sense_hat_angle, args=[th_angle_y])
    p1.start()
    p2.start()


import math
import os
import tempfile
from copy import deepcopy
from unittest import TestCase

import torch
from mmcv.runner import load_checkpoint, save_checkpoint

from mmcls.models.backbones import DistilledVisionTransformer
from .utils import timm_resize_pos_embed

class TestDeiT(TestCase):

    def setUp(self):
        self.cfg = dict(
            arch='deit-base', img_size=224, patch_size=16, drop_rate=0.1)

    def test_init_weights(self):
        # test weight init cfg
        cfg = deepcopy(self.cfg)
        cfg['init_cfg'] = [
            dict(
                type='Kaiming',
                layer='Conv2d',
                mode='fan_in',
                nonlinearity='linear')
        ]
        model = DistilledVisionTransformer(**cfg)
        ori_weight = model.patch_embed.projection.weight.clone().detach()
        # The pos_embed is all zero before initialize
        self.assertTrue(torch.allclose(model.dist_token, torch.tensor(0.)))

        model.init_weights()
        initialized_weight = model.patch_embed.projection.weight
        self.assertFalse(torch.allclose(ori_weight, initialized_weight))
        self.assertFalse(torch.allclose(model.dist_token, torch.tensor(0.)))

        # test load checkpoint
        pretrain_pos_embed = model.pos_embed.clone().detach()
        tmpdir = tempfile.gettempdir()
        checkpoint = os.path.join(tmpdir, 'test.pth')
        save_checkpoint(model, checkpoint)
        cfg = deepcopy(self.cfg)
        model = DistilledVisionTransformer(**cfg)
        load_checkpoint(model, checkpoint, strict=True)
        self.assertTrue(torch.allclose(model.pos_embed, pretrain_pos_embed))

        # test load checkpoint with different img_size
        cfg = deepcopy(self.cfg)
        cfg['img_size'] = 384
        model = DistilledVisionTransformer(**cfg)
        load_checkpoint(model, checkpoint, strict=True)
        resized_pos_embed = timm_resize_pos_embed(
            pretrain_pos_embed, model.pos_embed, num_tokens=2)
        self.assertTrue(torch.allclose(model.pos_embed, resized_pos_embed))

        os.remove(checkpoint)

    def test_forward(self):
        imgs = torch.randn(3, 3, 224, 224)

        # test with_cls_token=False
        cfg = deepcopy(self.cfg)
        cfg['with_cls_token'] = False
        cfg['output_cls_token'] = True
        with self.assertRaisesRegex(AssertionError, 'but got False'):
            DistilledVisionTransformer(**cfg)

        cfg = deepcopy(self.cfg)
        cfg['with_cls_token'] = False
        cfg['output_cls_token'] = False
        model = DistilledVisionTransformer(**cfg)
        outs = model(imgs)
        self.assertIsInstance(outs, tuple)
        self.assertEqual(len(outs), 1)
        patch_token = outs[-1]
        self.assertEqual(patch_token.shape, (3, 768, 14, 14))

        # test with output_cls_token
        cfg = deepcopy(self.cfg)
        model = DistilledVisionTransformer(**cfg)
        outs = model(imgs)
        self.assertIsInstance(outs, tuple)
        self.assertEqual(len(outs), 1)
        patch_token, cls_token, dist_token = outs[-1]
        self.assertEqual(patch_token.shape, (3, 768, 14, 14))
        self.assertEqual(cls_token.shape, (3, 768))
        self.assertEqual(dist_token.shape, (3, 768))

        # test without output_cls_token
        cfg = deepcopy(self.cfg)
        cfg['output_cls_token'] = False
        model = DistilledVisionTransformer(**cfg)
        outs = model(imgs)
        self.assertIsInstance(outs, tuple)
        self.assertEqual(len(outs), 1)
        patch_token = outs[-1]
        self.assertEqual(patch_token.shape, (3, 768, 14, 14))

        # Test forward with multi out indices
        cfg = deepcopy(self.cfg)
        cfg['out_indices'] = [-3, -2, -1]
        model = DistilledVisionTransformer(**cfg)
        outs = model(imgs)
        self.assertIsInstance(outs, tuple)
        self.assertEqual(len(outs), 3)
        for out in outs:
            patch_token, cls_token, dist_token = out
            self.assertEqual(patch_token.shape, (3, 768, 14, 14))
            self.assertEqual(cls_token.shape, (3, 768))
            self.assertEqual(dist_token.shape, (3, 768))

        # Test forward with dynamic input size
        imgs1 = torch.randn(3, 3, 224, 224)
        imgs2 = torch.randn(3, 3, 256, 256)
        imgs3 = torch.randn(3, 3, 256, 309)
        cfg = deepcopy(self.cfg)
        model = DistilledVisionTransformer(**cfg)
        for imgs in [imgs1, imgs2, imgs3]:
            outs = model(imgs)
            self.assertIsInstance(outs, tuple)
            self.assertEqual(len(outs), 1)
            patch_token, cls_token, dist_token = outs[-1]
            expect_feat_shape = (math.ceil(imgs.shape[2] / 16),
                                 math.ceil(imgs.shape[3] / 16))
            self.assertEqual(patch_token.shape, (3, 768, *expect_feat_shape))
            self.assertEqual(cls_token.shape, (3, 768))
            self.assertEqual(dist_token.shape, (3, 768))


from turtle import Turtle

class Separator(Turtle):

    def __init__(self):
        super().__init__()
        self.color("White")
        self.hideturtle()
        self.penup()
        self.goto(0, -300)
        self.setheading(90)
        self.dashed_line()

    def dashed_line(self):
        while self.ycor() < 350:
            self.pendown()
            self.fd(10)
            self.penup()
            self.fd(10)


#!/usr/bin/env python3

import os

from vndk_definition_tool import (ELF, ELFLinker, PT_SYSTEM, PT_VENDOR)

class GraphBuilder(object):
    _PARTITION_NAMES = {
        PT_SYSTEM: 'system',
        PT_VENDOR: 'vendor',
    }

    _LIB_DIRS = {
        ELF.ELFCLASS32: 'lib',
        ELF.ELFCLASS64: 'lib64',
    }

    def __init__(self):
        self.graph = ELFLinker()

    def add_lib(self, partition, klass, name, dt_needed=tuple(),
                exported_symbols=tuple(), imported_symbols=tuple(),
                extra_dir=None):
        """Create and add a shared library to ELFLinker."""

        lib_dir = os.path.join('/', self._PARTITION_NAMES[partition],
                               self._LIB_DIRS[klass])
        if extra_dir:
            lib_dir = os.path.join(lib_dir, extra_dir)

        path = os.path.join(lib_dir, name + '.so')

        elf = ELF(klass, ELF.ELFDATA2LSB, dt_needed=dt_needed,
                  exported_symbols=set(exported_symbols),
                  imported_symbols=set(imported_symbols))

        lib = self.graph.add_lib(partition, path, elf)
        setattr(self, name + '_' + elf.elf_class_name, lib)
        return lib

    def add_lib32(self, partition, name, dt_needed=tuple(),
                  exported_symbols=tuple(), imported_symbols=tuple(),
                  extra_dir=None):
        return self.add_lib(partition, ELF.ELFCLASS32, name, dt_needed,
                            exported_symbols, imported_symbols, extra_dir)

    def add_lib64(self, partition, name, dt_needed=tuple(),
                  exported_symbols=tuple(), imported_symbols=tuple(),
                  extra_dir=None):
        return self.add_lib(partition, ELF.ELFCLASS64, name, dt_needed,
                            exported_symbols, imported_symbols, extra_dir)

    def add_multilib(self, partition, name, dt_needed=tuple(),
                     exported_symbols=tuple(), imported_symbols=tuple(),
                     extra_dir=None):
        """Add 32-bit / 64-bit shared libraries to ELFLinker."""
        return (
            self.add_lib(partition, ELF.ELFCLASS32, name, dt_needed,
                         exported_symbols, imported_symbols, extra_dir),
            self.add_lib(partition, ELF.ELFCLASS64, name, dt_needed,
                         exported_symbols, imported_symbols, extra_dir)
        )

    def resolve(self, vndk_lib_dirs=None, ro_vndk_version=None):
        if vndk_lib_dirs is not None:
            self.graph.vndk_lib_dirs = vndk_lib_dirs
        if ro_vndk_version is not None:
            self.graph.ro_vndk_version = ro_vndk_version
        self.graph.resolve_deps()


# http://brettbeauregard.com/blog/2011/04/improving-the-beginner’s-pid-reset-windup/

class PID:
    def __init__(self):
        self.setpoint: float = 0
        self._last_input: float = 0
        self.output: float = 0
        self._i_term: float = 0
        self._kp: float = 0
        self._kd: float = 0
        self._ki: float = 0

        self.out_min: float = 0
        self.out_max: float = 0

    def compute(self, input: float):
        error = self.setpoint - input
        self._i_term += self._ki * error
        if self._i_term > self.out_max:
            self._i_term = self.out_max
        elif self._i_term < self.out_min:
            self._i_term = self.out_min

        d_input = input - self._last_input

        self.output = self._kp * error + self._i_term - self._kd * d_input
        if self.output > self.out_max:
            self.output = self.out_max
        elif self.output < self.out_min:
            self.output = self.out_min

        self._last_input = input

    def set_tuning(self, kp: float = None, ki: float = None, kd: float = None, sample_time: float = None):
        self._kp = kp
        if ki is not None and sample_time:
            self._ki = ki * sample_time
        if kd is not None and sample_time:
            self._kd = kd / sample_time

# Reference velocity calculation
def calc_ref_v(headway: float, self_x: float, leader_a: float, leader_v: float, leader_x: float, age: float) -> float:
    # extrapolate the old information
    leader_x += leader_a / 2 * age ** 2 + leader_v * age
    leader_v += leader_a * age

    dist = leader_x - self_x
    return leader_v + dist - headway


'''
05 - leia um vetor de 10 posições contar e escrever quantos pares ele possui.
'''

vetor = list(range(11))

cont = 0
for num in vetor:
    if num % 2 == 0:
        print(num, end=' ')
        cont += 1
print()
print(f'O vetor possui {cont-1} pares')


"""
Example for XIAO BLE. Blinks the built-in REG LED.
Marcelo Rovai @Aug22
"""

import time
import board
import digitalio

ledR = digitalio.DigitalInOut(board.LED_RED)
ledR.direction = digitalio.Direction.OUTPUT

ledG = digitalio.DigitalInOut(board.LED_GREEN)
ledG.direction = digitalio.Direction.OUTPUT

ledB = digitalio.DigitalInOut(board.LED_BLUE)
ledB.direction = digitalio.Direction.OUTPUT

def led_off():
    ledR.value = True
    ledG.value = True
    ledB.value = True

led_off()
while True:
    ledR.value = False
    print("Red On")
    time.sleep(1.0)
    led_off()
    time.sleep(1.0)
    ledG.value = False
    print("Green On")
    time.sleep(1.0)
    led_off()
    time.sleep(1.0)
    ledB.value = False
    print("Blue On")
    time.sleep(1.0)
    led_off()
    time.sleep(1.0)


import os
import sys
import locale

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
sys.path.append(BASE_DIR)
sys.path.append(os.path.dirname(BASE_DIR))
sys.path.insert(0, os.path.join(BASE_DIR, "apps"))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '=41#sape7s5bot&j+m-7s+dh88(bs#jw@!a1k$=$7a#=@+e$uu'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'widget_tweaks',
    'compressor',
    'solo',
    'website',
)

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

ROOT_URLCONF = 'rigipstrophy.urls'

WSGI_APPLICATION = 'rigipstrophy.wsgi.application'

locale.setlocale(locale.LC_ALL, "pl_PL.UTF-8")

LANGUAGE_CODE = 'pl'
TIME_ZONE = 'Europe/Warsaw'
USE_I18N = True
USE_L10N = False
USE_TZ = False

DATE_FORMAT = "d.m.Y"
DATETIME_FORMAT = "d.m.Y, H:i"

DATE_INPUT_FORMATS = (
    '%d.%m.%Y', '%d.%m.%y',  # '25.10.2006', '25.10.06'
    '%d-%m-%Y', '%d/%m/%Y', '%d/%m/%y',  # '25-10-2006', '25/10/2006', '25/10/06'
    '%d %b %Y',  # '25 Oct 2006',
    '%d %B %Y',  # '25 October 2006',
)

DATETIME_INPUT_FORMATS = (
    '%d.%m.%Y %H:%M:%S',     # '2006.10.25 14:30:59'
    '%d.%m.%Y %H:%M',     # '2006.10.25 14:30'
    '%Y-%m-%d %H:%M:%S',     # '2006-10-25 14:30:59'
    '%Y-%m-%d %H:%M:%S.%f',  # '2006-10-25 14:30:59.000200'
    '%Y-%m-%d %H:%M',        # '2006-10-25 14:30'
    '%Y-%m-%d',              # '2006-10-25'
    '%m/%d/%Y %H:%M:%S',     # '10/25/2006 14:30:59'
    '%m/%d/%Y %H:%M:%S.%f',  # '10/25/2006 14:30:59.000200'
    '%m/%d/%Y %H:%M',        # '10/25/2006 14:30'
    '%m/%d/%Y',              # '10/25/2006'
    '%m/%d/%y %H:%M:%S',     # '10/25/06 14:30:59'
    '%m/%d/%y %H:%M:%S.%f',  # '10/25/06 14:30:59.000200'
    '%m/%d/%y %H:%M',        # '10/25/06 14:30'
    '%m/%d/%y',              # '10/25/06'
)

MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media")
MEDIA_URL = '/media/'

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")
STATIC_URL = '/static/'


"""

Retrieve images from the web

"""

import shutil
import requests
from tools.system_tools import img_dir
from PIL import Image, ImageTk, ImageGrab

#####################################
#                                   #
#   Saved URLs for updating plots   #
#                                   #
#####################################

# Pivotal Weather daily CONUS weather forecast images
ndfd_loc = "https://x-hv1.pivotalweather.com/maps/ndfd/latest/"
pw_conus_maxtemp_url = ndfd_loc + "ndfd_sfctmax.conus.png"
pw_conus_mintemp_url = ndfd_loc + "ndfd_sfctmin.conus.png"
pw_conus_24hrqpf_url = ndfd_loc + "ndfd_24hqpf.conus.png"
pw_conus_24hsnow_url = ndfd_loc + "ndfd_24hsnow.conus.png"
pw_conus_24hgust_url = ndfd_loc + "ndfd_24hgust.conus.png"
# Alicia Bentley maps (http://www.atmos.albany.edu/student/abentley/realtime.html)
#  CONUS Bentley maps
ab_con_loc = "http://www.atmos.albany.edu/student/abentley/realtime/images/northamer/"
ab_con_6hrprecip_url = ab_con_loc + "6hprecip/6hprecip_57.png"
ab_con_850thetae_url = ab_con_loc + "850_thetae/850_thetae_57.png"
ab_con_capeshear_url = ab_con_loc + "cape_shear/cape_shear_57.png"
ab_con_mslp_jets_url = ab_con_loc + "mslp_jet/mslp_jet_57.png"
#  Pacific Bentley maps
ab_pac_loc = "http://www.atmos.albany.edu/student/abentley/realtime/images/pacific/"
ab_pac_mslp_anom_url = ab_pac_loc + "mslp_anom/mslp_anom_57.png"
ab_pac_6hrprecip_url = ab_pac_loc + "6hprecip/6hprecip_57.png"
ab_pac_850thetae_url = ab_pac_loc + "850_thetae/850_thetae_57.png"
ab_pac_700wnd_pw_url = ab_pac_loc + "700wind_pw/700wind_pw_57.png"
# Coastal Data Information Program swell map
cdip_swell_url = "http://cdip.ucsd.edu/recent/model_images/monterey.png"
# Surfline coastal data
surfline_sst_url = "https://slcharts01.cdn-surfline.com/charts/cencal/monterey/nearshoresst/monterey_large_1_F.png"
surfline_wnd_url = "https://slcharts01.cdn-surfline.com/charts/cencal/monterey/nearshorewinds/monterey_large_1.png"
# NWS fire maps
nws_fire_outlook_url = "https://www.spc.noaa.gov/products/fire_wx/day1otlk_fire.gif"
nifc_fire_month_url  = "https://www.predictiveservices.nifc.gov/outlooks/month1_outlook.png"

# Local directory for downloaded images
# img_dir = config.locs.image_dir
# Each image data (source url, local dir+filename)
pw_conus_maxtemp = ( pw_conus_maxtemp_url, img_dir+"pw_conus_maxtemp.png" )
pw_conus_mintemp = ( pw_conus_mintemp_url, img_dir+"pw_conus_mintemp.png" )
pw_conus_24hrqpf = ( pw_conus_24hrqpf_url, img_dir+"pw_conus_24hrqpf.png" )
pw_conus_24hsnow = ( pw_conus_24hsnow_url, img_dir+"pw_conus_24hsnow.png" )
pw_conus_24hgust = ( pw_conus_24hgust_url, img_dir+"pw_conus_24hgust.png" )
ab_con_6hrprecip = ( ab_con_6hrprecip_url, img_dir+"ab_con_6hrprecip.png" )
ab_con_850thetae = ( ab_con_850thetae_url, img_dir+"ab_con_850thetae.png" )
ab_con_capeshear = ( ab_con_capeshear_url, img_dir+"ab_con_capeshear.png" )
ab_con_mslp_jets = ( ab_con_mslp_jets_url, img_dir+"ab_con_mslp_jets.png" )
ab_pac_mslp_anom = ( ab_pac_mslp_anom_url, img_dir+"ab_pac_mslp_anom.png" )
ab_pac_6hrprecip = ( ab_pac_6hrprecip_url, img_dir+"ab_pac_6hrprecip.png" )
ab_pac_850thetae = ( ab_pac_850thetae_url, img_dir+"ab_pac_850thetae.png" )
ab_pac_700wnd_pw = ( ab_pac_700wnd_pw_url, img_dir+"ab_pac_700wnd_pw.png" )
cdip_swell       = ( cdip_swell_url,       img_dir+"cdip_swell.png"       )
surfline_sst     = ( surfline_sst_url,     img_dir+"surfline_sst.png"     )
surfline_wnd     = ( surfline_wnd_url,     img_dir+"surfline_wnd.png"     )
nws_fire_outlook = ( nws_fire_outlook_url, img_dir+"nws_fire_outlook.png" )
nifc_fire_month  = ( nifc_fire_month_url,  img_dir+"nifc_fire_month.png"  )

# All image data
images = ( pw_conus_maxtemp, pw_conus_mintemp, pw_conus_24hrqpf,
           pw_conus_24hgust, ab_con_6hrprecip, ab_con_850thetae,
           ab_con_capeshear, ab_con_mslp_jets, ab_pac_mslp_anom,
           ab_pac_6hrprecip, ab_pac_850thetae, ab_pac_700wnd_pw,
           cdip_swell, surfline_sst, surfline_wnd, nws_fire_outlook,
           nifc_fire_month )

def get_img( url, filename ):
    try:
        r = requests.get( url, stream = True )
    except:
        return
    # Check if the image was retrieved successfully
    if r.status_code == 200:
        # Set decode_content value to True, otherwise the downloaded image file's size will be zero.
        r.raw.decode_content = True
        with open(filename,'wb') as f:
            shutil.copyfileobj(r.raw, f)
    else:
        print(url)
        raise RuntimeWarning("Couldn't download image!")

def crop_bentleys( path, debug=False ):
    # Opens a image in RGB mode
    im = Image.open(path)
    # Crop specs for removing unnecessary vars from plot
    im1 = im.crop((100, 0, 1060, 791))
    # Debug mode for checking crop specs
    if debug:
        # Shows the image in image viewer
        im1.show()
    else:
        # Overwrite previous image
        im1.save(path)

def get_imgs( ):
    # Retrieves all listed images
    for img in images:
        get_img( img[0], img[1] )
    for img in images[4:12]:
        crop_bentleys( img[1] )

def get_pws( ):
    for img in images[:4]:
        get_img( img[0], img[1] )

def get_bentleys( ):
    for img in images[4:12]:
        get_img( img[0], img[1] )
        crop_bentleys( img[1] )

def get_marine( ):
    for img in images[12:15]:
        get_img( img[0], img[1] )

if __name__ == "__main__":
    #get_img( pw_conus_maxtemp[0], img_dir+"test.png" )
    get_imgs()


import re
import pathlib
from setuptools import setup, find_packages
from setuptools.command.test import test as TestCommand

BASE_PATH = pathlib.Path(__file__).parent
try:
    version = re.findall(r"""^__version__ = "([^']+)"\r?$""",
                         (BASE_PATH / "aioftp" / "__init__.py").read_text(),
                         re.M)[0]
except IndexError:
    raise RuntimeError("Unable to determine version.")

class NoseTestCommand(TestCommand):

    def finalize_options(self):
        TestCommand.finalize_options(self)
        self.test_args = []
        self.test_suite = True

    def run_tests(self):
        import pathlib
        import shutil
        import nose

        for path in pathlib.Path("tests").glob("*"):
            if path.is_dir():
                shutil.rmtree(str(path))
        cov = pathlib.Path(".coverage")
        if cov.exists():
            cov.unlink()
        nose.main(argv=["nosetests"])

setup(
    name="aioftp",
    version=version,
    description=("ftp client/server for asyncio"),
    long_description=(BASE_PATH / "README.rst").read_text(),
    classifiers=[
        "Development Status :: 5 - Production/Stable",
        "Programming Language :: Python",
        "Programming Language :: Python :: 3",
        "Topic :: Internet :: File Transfer Protocol (FTP)",
    ],
    author="pohmelie",
    author_email="multisosnooley@gmail.com",
    url="https://github.com/aio-libs/aioftp",
    license="Apache 2",
    packages=find_packages(),
    python_requires=" >= 3.5.3",
    install_requires=[],
    tests_require=["nose", "coverage", "trustme"],
    cmdclass={"test": NoseTestCommand},
    include_package_data=True
)