from __future__ import print_function
import argparse
import os
import random
import numpy as np
import torch
import torch.nn.parallel
import torch.optim as optim
import torch.utils.data
from dataset.dataset import RIODatasetSceneGraph, collate_fn_vaegan_points
from model.VAE import VAE
from model.atlasnet import AE_AtlasNet
from model.discriminators import BoxDiscriminator, ShapeAuxillary
from model.losses import bce_loss
from helpers.util import bool_flag

from model.losses import calculate_model_losses

import torch.nn.functional as F
import json

from tensorboardX import SummaryWriter

parser = argparse.ArgumentParser()
# standard hyperparameters, batch size, learning rate, etc
parser.add_argument('--batchSize', type=int, default=8, help='input batch size')
parser.add_argument('--lr', type=float, help='learning rate', default=0.0001)
parser.add_argument('--nepoch', type=int, default=101, help='number of epochs to train for')

# paths and filenames
parser.add_argument('--outf', type=str, default='checkpoint', help='output folder')
parser.add_argument('--model', type=str, default='', help='model path')
parser.add_argument('--dataset', required=False, type=str, default="./GT", help="dataset path")
parser.add_argument('--dataset_3RScan', type=str, default='', help="dataset path of 3RScan")
parser.add_argument('--label_file', required=False, type=str, default='labels.instances.align.annotated.ply', help="label file name")
parser.add_argument('--logf', default='logs', help='folder to save tensorboard logs')
parser.add_argument('--exp', default='./experiments/layout_test', help='experiment name')
parser.add_argument('--path2atlas', default="./experiments/model_36.pth", type=str)

# GCN parameters
parser.add_argument('--residual', type=bool_flag, default=False, help="residual in GCN")
parser.add_argument('--pooling', type=str, default='avg', help="pooling method in GCN")

# dataset related
parser.add_argument('--large', default=True, type=bool_flag, help='large set of shape class labels')
parser.add_argument('--use_splits', default=True, type=bool_flag, help='Set to true if you want to use splitted training data')
parser.add_argument('--use_scene_rels', type=bool_flag, default=True, help="connect all nodes to a root scene node")
parser.add_argument('--with_points', type=bool_flag, default=False, help="if false and with_feats is false, only predicts layout."
                                                                         "If true always also loads pointsets. Notice that this is much "
                                                                         "slower than only loading the save features. Therefore, "
                                                                         "the recommended setting is with_points=False and with_feats=True.")
parser.add_argument('--with_feats', type=bool_flag, default=True, help="if true reads latent point features instead of pointsets."
                                                                       "If not existing, they get generated at the beginning.")
parser.add_argument('--shuffle_objs', type=bool_flag, default=True, help="shuffle objs of a scene")
parser.add_argument('--num_points', type=int, default=1024, help='number of points for each object')
parser.add_argument('--rio27', default=False, type=bool_flag)
parser.add_argument('--use_canonical', default=True, type=bool_flag)
parser.add_argument('--with_angles', default=True, type=bool_flag)
parser.add_argument('--num_box_params', default=6, type=int)
parser.add_argument('--crop_floor', default=False, type=bool_flag)

# training and architecture related
parser.add_argument('--workers', type=int, help='number of data loading workers', default=4)
parser.add_argument('--overfiting_debug', type=bool_flag, default=False)
parser.add_argument('--weight_D_box', default=0.1, type=float, help="Box Discriminator")
parser.add_argument('--with_changes', default=True, type=bool_flag)
parser.add_argument('--with_shape_disc', default=True, type=bool_flag)
parser.add_argument('--with_manipulator', default=True, type=bool_flag)

parser.add_argument('--replace_latent', default=True, type=bool_flag)
parser.add_argument('--network_type', default='shared', choices=['dis', 'sln', 'mlp', 'shared'], type=str)

args = parser.parse_args()
print(args)

def train():
    """ Train the network based on the provided argparse parameters
    """
    args.manualSeed = random.randint(1, 10000)  # optionally fix seed 7494
    print("Random Seed: ", args.manualSeed)

    print(torch.__version__)

    random.seed(args.manualSeed)
    torch.manual_seed(args.manualSeed)

    # prepare pretrained AtlasNet model, later used to convert pointsets to a shape feature
    saved_atlasnet_model = torch.load(args.path2atlas)
    point_ae = AE_AtlasNet(num_points=1024, bottleneck_size=128, nb_primitives=25)
    point_ae.load_state_dict(saved_atlasnet_model, strict=True)
    if torch.cuda.is_available():
        point_ae = point_ae.cuda()
    point_ae.eval()

    # instantiate scene graph dataset for training
    dataset = RIODatasetSceneGraph(
            root=args.dataset,
            root_3rscan=args.dataset_3RScan,
            label_file=args.label_file,
            npoints=args.num_points,
            path2atlas=args.path2atlas,
            split='train_scans',
            shuffle_objs=(args.shuffle_objs and not args.overfiting_debug),
            use_points=args.with_points,
            use_scene_rels=args.use_scene_rels,
            with_changes=args.with_changes,
            vae_baseline=args.network_type == 'sln',
            with_feats=args.with_feats,
            large=args.large,
            atlas=point_ae,
            seed=False,
            use_splits=args.use_splits,
            use_rio27=args.rio27,
            use_canonical=args.use_canonical,
            crop_floor=args.crop_floor,
            center_scene_to_floor=args.crop_floor,
            recompute_feats=False)

    collate_fn = collate_fn_vaegan_points
    # instantiate data loader from dataset
    dataloader = torch.utils.data.DataLoader(
            dataset,
            batch_size=args.batchSize,
            collate_fn=collate_fn,
            shuffle=(not args.overfiting_debug),
            num_workers=int(args.workers))

    # number of object classes and relationship classes
    num_classes = len(dataset.classes)
    num_relationships = len(dataset.relationships)

    try:
        os.makedirs(args.outf)
    except OSError:
        pass
    # instantiate the model
    model = VAE(type=args.network_type, vocab=dataset.vocab, replace_latent=args.replace_latent,
                with_changes=args.with_changes, residual=args.residual, gconv_pooling=args.pooling,
                with_angles=args.with_angles, num_box_params=args.num_box_params)
    if torch.cuda.is_available():
        model = model.cuda()
    # instantiate a relationship discriminator that considers the boxes and the semantic labels
    # if the loss weight is larger than zero
    # also create an optimizer for it
    if args.weight_D_box > 0:
        boxD = BoxDiscriminator(6, num_relationships, num_classes)
        optimizerDbox = optim.Adam(filter(lambda p: p.requires_grad, boxD.parameters()), lr=args.lr, betas=(0.9, 0.999))
        boxD.cuda()
        boxD = boxD.train()

    # instantiate auxiliary discriminator for shape and a respective optimizer
    shapeClassifier = ShapeAuxillary(128, len(dataset.cat))
    shapeClassifier = shapeClassifier.cuda()
    shapeClassifier.train()
    optimizerShapeAux = optim.Adam(filter(lambda p: p.requires_grad, shapeClassifier.parameters()), lr=args.lr, betas=(0.9, 0.999))

    # initialize tensorboard writer
    writer = SummaryWriter(args.exp + "/" + args.logf)

    # optimizer for model
    params = filter(lambda p: p.requires_grad,list(model.parameters()) )
    optimizer = optim.Adam(params, lr=args.lr)

    print("---- Model and Dataset built ----")

    if not os.path.exists(args.exp + "/" + args.outf):
        os.makedirs(args.exp + "/" + args.outf)

    # save parameters so that we can read them later on evaluation
    with open(os.path.join(args.exp, 'args.json'), 'w') as f:
        json.dump(args.__dict__, f, indent=2)
    print("Saving all parameters under:")
    print(os.path.join(args.exp, 'args.json'))

    optimizer.step()
    torch.autograd.set_detect_anomaly(True)
    counter = 0

    print("---- Starting training loop! ----")
    for epoch in range(0, args.nepoch):
        print('Epoch: {}/{}'.format(epoch, args.nepoch))

        for i, data in enumerate(dataloader, 0):
            # skip invalid data
            if data == -1:
                continue

            try:
                enc_objs, enc_triples, enc_tight_boxes, enc_objs_to_scene, enc_triples_to_scene = data['encoder']['objs'],\
                            data['encoder']['tripltes'], data['encoder']['boxes'], data['encoder']['obj_to_scene'], data['encoder']['tiple_to_scene']

                if args.with_feats:
                    encoded_enc_points = data['encoder']['feats']
                    encoded_enc_points = encoded_enc_points.cuda()
                elif args.with_points:
                    enc_points = data['encoder']['points']
                    enc_points = enc_points.cuda()

                dec_objs, dec_triples, dec_tight_boxes, dec_objs_to_scene, dec_triples_to_scene = data['decoder']['objs'],\
                            data['decoder']['tripltes'], data['decoder']['boxes'], data['decoder']['obj_to_scene'], data['decoder']['tiple_to_scene']

                if 'feats' in data['decoder']:
                    encoded_dec_points = data['decoder']['feats']
                    encoded_dec_points = encoded_dec_points.cuda()
                else:
                    if 'points' in data['decoder']:
                        dec_points = data['decoder']['points']
                        dec_points = dec_points.cuda()

                # changed nodes
                missing_nodes = data['missing_nodes']
                manipulated_nodes = data['manipulated_nodes']

            except Exception as e:
                print('Exception', str(e))
                continue

            enc_objs, enc_triples, enc_tight_boxes = enc_objs.cuda(), enc_triples.cuda(), enc_tight_boxes.cuda()
            dec_objs, dec_triples, dec_tight_boxes = dec_objs.cuda(), dec_triples.cuda(), dec_tight_boxes.cuda()

            if args.with_points:
                enc_points, dec_points = enc_points.cuda(), dec_points.cuda()

            # avoid batches with insufficient number of instances with valid shape classes
            mask = [ob in dataset.point_classes_idx for ob in dec_objs]
            if sum(mask) <= 1:
                continue

            optimizer.zero_grad()
            optimizerShapeAux.zero_grad()

            model = model.train()

            if args.weight_D_box > 0:
                optimizerDbox.zero_grad()

            # if we are reading pointsets and not precomputed shape features, convert them to features
            # otherwise do nothing, as we already have the features
            if not args.with_feats and args.with_points:
                with torch.no_grad():
                    encoded_enc_points = point_ae.encoder(enc_points.transpose(2,1).contiguous())
                    encoded_dec_points = point_ae.encoder(dec_points.transpose(2,1).contiguous())

            # set all scene (dummy) nodes point encodings to zero
            enc_scene_nodes = enc_objs == 0
            dec_scene_nodes = dec_objs == 0
            encoded_enc_points[enc_scene_nodes] = torch.zeros([torch.sum(enc_scene_nodes), encoded_enc_points.shape[1]]).float().cuda()
            encoded_dec_points[dec_scene_nodes] = torch.zeros([torch.sum(dec_scene_nodes), encoded_dec_points.shape[1]]).float().cuda()

            if args.num_box_params == 7:
                # all parameters, including angle, procesed by the box_net
                enc_boxes = enc_tight_boxes
                dec_boxes = dec_tight_boxes
            elif args.num_box_params == 6:
                # no angle. this will be learned separately if with_angle is true
                enc_boxes = enc_tight_boxes[:, :6]
                dec_boxes = dec_tight_boxes[:, :6]
            elif args.num_box_params == 4:
                # height, centroid. assuming we want the other sizes to be estimated from the shape aspect ratio
                enc_boxes = enc_tight_boxes[:, 2:6]
                dec_boxes = dec_tight_boxes[:, 2:6]
            else:
                raise NotImplementedError

            # limit the angle bin range from 0 to 24
            enc_angles = enc_tight_boxes[:, 6].long() - 1
            enc_angles = torch.where(enc_angles > 0, enc_angles, torch.zeros_like(enc_angles))
            enc_angles = torch.where(enc_angles < 24, enc_angles, torch.zeros_like(enc_angles))
            dec_angles = dec_tight_boxes[:, 6].long() - 1
            dec_angles = torch.where(dec_angles > 0, dec_angles, torch.zeros_like(dec_angles))
            dec_angles = torch.where(dec_angles < 24, dec_angles, torch.zeros_like(dec_angles))

            attributes = None

            boxGloss = 0
            loss_genShape = 0
            loss_genShapeFake = 0
            loss_shape_fake_g = 0

            if args.with_manipulator:
                model_out = model.forward_mani(enc_objs, enc_triples, enc_boxes, enc_angles, encoded_enc_points, attributes, enc_objs_to_scene,
                                                   dec_objs, dec_triples, dec_boxes, dec_angles, encoded_dec_points, attributes, dec_objs_to_scene,
                                                   missing_nodes, manipulated_nodes)

                mu_box, logvar_box, mu_shape, logvar_shape, orig_gt_box, orig_gt_angle, orig_gt_shape, orig_box, orig_angle, orig_shape, \
                dec_man_enc_box_pred, dec_man_enc_angle_pred, dec_man_enc_shape_pred, keep = model_out
            else:
                model_out = model.forward_no_mani(dec_objs, dec_triples, dec_boxes, encoded_dec_points, angles=dec_angles,
                                      attributes=attributes)

                mu_box, logvar_box, mu_shape, logvar_shape, dec_man_enc_box_pred, dec_man_encd_angles_pred, \
                  dec_man_enc_shape_pred = model_out

                orig_gt_box = dec_boxes
                orig_box = dec_man_enc_box_pred

                orig_gt_shape = encoded_dec_points
                orig_shape = dec_man_enc_shape_pred

                orig_angle = dec_man_encd_angles_pred
                orig_gt_angle = dec_angles

                keep = []
                for i in range(len(dec_man_enc_box_pred)):
                    keep.append(1)
                keep = torch.from_numpy(np.asarray(keep).reshape(-1, 1)).float().cuda()

            if args.with_manipulator and args.with_shape_disc and dec_man_enc_shape_pred is not None:
                shape_logits_fake_d, probs_fake_d = shapeClassifier(dec_man_enc_shape_pred[mask].detach())
                shape_logits_fake_g, probs_fake_g = shapeClassifier(dec_man_enc_shape_pred[mask])
                shape_logits_real, probs_real = shapeClassifier(encoded_dec_points[mask].detach())

                # auxiliary loss. can the discriminator predict the correct class for the generated shape?
                loss_shape_real = torch.nn.functional.cross_entropy(shape_logits_real, dec_objs[mask])
                loss_shape_fake_d = torch.nn.functional.cross_entropy(shape_logits_fake_d, dec_objs[mask])
                loss_shape_fake_g = torch.nn.functional.cross_entropy(shape_logits_fake_g, dec_objs[mask])
                # standard discriminator loss
                loss_genShapeFake = bce_loss(probs_fake_g, torch.ones_like(probs_fake_g))
                loss_dShapereal = bce_loss(probs_real, torch.ones_like(probs_real))
                loss_dShapefake = bce_loss(probs_fake_d, torch.zeros_like(probs_fake_d))

                loss_dShape = loss_dShapefake + loss_dShapereal + loss_shape_real + loss_shape_fake_d
                loss_genShape = loss_genShapeFake + loss_shape_fake_g
                loss_dShape.backward()
                optimizerShapeAux.step()

            vae_loss_box, vae_losses_box = calculate_model_losses(args,
                                                                    orig_gt_box,
                                                                    orig_box,
                                                                    name='box', withangles=args.with_angles, angles_pred=orig_angle,
                                                                    mu=mu_box, logvar=logvar_box, angles=orig_gt_angle,
                                                                    KL_weight=0.1, writer=writer, counter=counter)
            if dec_man_enc_shape_pred is not None:
                vae_loss_shape, vae_losses_shape = calculate_model_losses(args,
                                                                        orig_gt_shape,
                                                                        orig_shape,
                                                                        name='shape', withangles=False,
                                                                        mu=mu_shape, logvar=logvar_shape,
                                                                        KL_weight=0.1, writer=writer, counter=counter)
            else:
                # set shape loss to 0 if we are only predicting layout
                vae_loss_shape, vae_losses_shape = 0, 0

            if args.with_manipulator and args.with_changes:
                oriented_gt_boxes = torch.cat([dec_boxes], dim=1)
                boxes_pred_in = keep * oriented_gt_boxes + (1-keep) * dec_man_enc_box_pred

                if args.weight_D_box == 0:
                    # Generator loss
                    boxGloss = 0
                    # Discriminator loss
                    gamma = 0.1
                    boxDloss_real = 0
                    boxDloss_fake = 0
                    reg_loss = 0
                else:
                    logits, _ = boxD(dec_objs, dec_triples, boxes_pred_in, keep)
                    logits_fake, reg_fake = boxD(dec_objs, dec_triples, boxes_pred_in.detach(), keep, with_grad=True,
                                               is_real=False)
                    logits_real, reg_real = boxD(dec_objs, dec_triples, oriented_gt_boxes, with_grad=True, is_real=True)
                    # Generator loss
                    boxGloss = bce_loss(logits, torch.ones_like(logits))
                    # Discriminator loss
                    gamma = 0.1
                    boxDloss_real = bce_loss(logits_real, torch.ones_like(logits_real))
                    boxDloss_fake = bce_loss(logits_fake, torch.zeros_like(logits_fake))
                    # Regularization by gradient penalty
                    reg_loss = torch.mean(reg_real + reg_fake)

                # gradient penalty
                # disc_reg = discriminator_regularizer(logits_real, in_real, logits_fake, in_fake)
                boxDloss = boxDloss_fake + boxDloss_real + (gamma/2.0) * reg_loss
                optimizerDbox.zero_grad()
                boxDloss.backward()
                # gradient clip
                # torch.nn.utils.clip_grad_norm_(boxD.parameters(), 5.0)
                optimizerDbox.step()

            loss = vae_loss_box + vae_loss_shape + 0.1 * loss_genShape
            if args.with_changes:
                   loss = loss + args.weight_D_box * boxGloss #+ b_loss

            # optimize
            loss.backward()

            # Cap the occasional super mutant gradient spikes
            # Do now a gradient step and plot the losses
            torch.nn.utils.clip_grad_norm_(model.parameters(), 5.0)

            for group in optimizer.param_groups:
                for p in group['params']:
                    if p.grad is not None and p.requires_grad and torch.isnan(p.grad).any():
                        print('NaN grad in step {}.'.format(counter))
                        p.grad[torch.isnan(p.grad)] = 0
            optimizer.step()
            counter += 1

            if counter % 100 == 0:
                print("loss at {}: box {:.4f}\tshape {:.4f}\tdiscr RealFake {:.4f}\t discr Classifcation "
                      "{:.4f}".format(counter, vae_loss_box, vae_loss_shape, loss_genShapeFake,
                                                              loss_shape_fake_g))
            writer.add_scalar('Train Loss BBox', vae_loss_box, counter)
            writer.add_scalar('Train Loss Shape', vae_loss_shape, counter)
            writer.add_scalar('Train Loss loss_genShapeFake', loss_genShapeFake, counter)
            writer.add_scalar('Train Loss loss_shape_fake_g', loss_shape_fake_g, counter)

        if epoch % 5 == 0:
            model.save(args.exp, args.outf, epoch)

    writer.close()

def main():
    train()

if __name__ == "__main__": main()


#!/usr/bin/python

import youtube_dl

class Downloader:

    #This method checks a list to download each item
    def download_list(self,list_to_download):
        list_errors = []
        for item in list_to_download:
            if not self.download(item):
                list_errors.append(item)
        return list_errors

    #This method downloads an item calling Youtube_DL
    def download(self,item):
        try:
            ydl_opts = {
                'format': 'bestaudio/best',
                'postprocessors': [{
                    'key': 'FFmpegExtractAudio',
                    'preferredcodec': 'mp3',
                    'preferredquality': '192',
                }],
            }
            with youtube_dl.YoutubeDL(ydl_opts) as ydl:
                ydl.download(["http://www.youtube.com/watch?v=" + item])
            print(item + " was downloaded")
            return True
        except:
            return False


standard_values = [5,8.5,300,500,120,300,500,75,30,250,45,150,5]
weights = [0.3723,.2190,0.371,0.0037,.0155,0.0062,.0037,0.025,0.061,0.0074,.0412,0.01236,0.3723]

standards = {
		'ph_val':[8.5,0.2190],
		'dissolved_oxygen':[5,0.3723],
		'biological_oxygen_demand':[5,0.3723],
		# 'colour':[5,]
		'electrical_conductivity':[300,0.371],
		'total_dissolved_solids':[500,0.0037],
		'total_alkalinity':[120,0.0155],
		'total_hardness':[300,0.0062],
		'total_suspended_solids':[500,0.0037],

        'magnesium':[30,0.061],
		'calcium':[75,0.025],
		'nitrate':[45,0.0412],
		'chlorides':[250,0.0074],
		'sulphate':[150,0.01236],

}

# def calcQn(vn,i):
# 	if i >1:
# 		q = (vn)/(standard_values[i])
# 		q = 100 * q
# 		print(q)
# 		return abs(q)

# 	elif i == 0:
# 		q = (vn - 14.6)/(standard_values[i] - 14.6)
# 		q = q * 100
# 		print(q)
# 		return abs(q)/1.4
# 	elif i == 1:
# 		q = (vn - 7)/(standard_values[i] - 7)
# 		q = 100 * q
# 		print(q)
# 		return abs(q)

# def calculateIndex(params):
# 	qn = []

# 	for i in range(0,13):
# 		qn.append(calcQn(params[i],i))
# 	print(qn)
# 	qn[12] = qn[12]/4.21052
# 	wn = 0
# 	for w in weights:
# 		wn = wn + w
# 	numerator = 0
# 	for i in range(0,13):
# 		numerator = numerator + (qn[i] * weights[i])
# 	print(numerator)
# 	index = numerator/wn
# 	print(index)
# 	return index

def calcQn(vn,key):
	if key == 'ph_val':
		q = (vn - 7)/(standards[key][0] - 7)
		q = 100 * q
		return abs(q)
	elif key == 'dissolved_oxygen':
		q = (vn - 14.6)/(standards[key][0] - 14.6)
		q = 100 * q
		return abs(q)
	elif key == 'biological_oxygen_demand':
		q = (vn)/(standards[key][0])
		q = (100 * q)/4.21052
		return abs(q)

	else :
		q = (vn)/(standards[key][0])
		q = 100 * q
		print(q)
		return abs(q)

def calculateIndex(dict_of_params):
	qn = {}
	for key in dict_of_params:
		qn[key] = calcQn(dict_of_params[key],key)
	print(qn)
	wn = 0
	numerator = 0
	for key in dict_of_params:
		wn = wn + standards[key][1]

		numerator =numerator +( standards[key][1] * qn[key] )

	index = numerator/wn
	print(index)
	return index


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

class Solution:
    def rotateDigit(self, d):
        if d == "0":
            return "0"
        elif d == "1":
            return "1"
        elif d == "2":
            return "5"
        elif d == "5":
            return "2"
        elif d == "6":
            return "9"
        elif d == "8":
            return "8"
        elif d == "9":
            return "6"

    def rotatedDigits(self, N):
        result = 0

        for i in range(1, N + 1):
            rotated_digits = [self.rotateDigit(digit) for digit in list(str(i))]
            if None in rotated_digits:
                continue
            elif int("".join(rotated_digits)) != i:
                result += 1

        return result

if __name__ == "__main__":
    solution = Solution()

    assert 4 == solution.rotatedDigits(10)
    assert 247 == solution.rotatedDigits(857)


from DNA import DNA
import random
import string

class Spawn:

    def __init__(self, target, genes=None, mutation_rate=10):

        self.target = target
        if genes is None:
            self.dna = DNA(size=len(target))
        else:
            self.dna = DNA(genes=genes)
        self.fitness = 0
        self.mutation_rate = mutation_rate

    def speak(self):
        return "".join(self.dna.genes)

    def calculate_fitness(self):
        matches = 0
        for x in range(len(self.dna.genes)):
            if self.dna.genes[x] == self.target[x]:
                matches += 1
        self.fitness = int(matches/len(self.target)*100)
        return matches

    def give_birth(self, other):
        new_genes =  []
        for x in range(len(self.dna.genes)):
            new_genes.append(random.choice([self.dna.genes[x], other.dna.genes[x]]))
        child_spawn = Spawn(self.target, genes=new_genes)
        child_spawn.mutate()
        return child_spawn

    def mutate(self):
        for index in range(len(self.dna.genes)):
            mutation_happened = random.randint(0, 10000) < self.mutation_rate
            if mutation_happened:
                self.dna.genes[index] = random.choice(string.ascii_letters + " ")


#!/usr/bin/env python3
import sys
import datetime

from setuptools import setup

if sys.version_info < (2, 5):
    sys.exit("Python 3.5 or greater is required.")

with open("README.md", "r", encoding="utf-8") as f:
    readme = f.read()

VERSION = datetime.datetime.now().strftime("%Y.%m.%d.%H%M%S")
LICENSE = "apache"

setup(
    name="reqe",
    version=VERSION,
    description=(
        "A python network request library that will not throw exceptions"
    ),
    long_description=readme,
    long_description_content_type="text/markdown",
    author="ophlr",
    author_email="niushuaibing@foxmail.com",
    maintainer="Eric",
    maintainer_email="niushuaibing@foxmail.com",
    license=LICENSE,
    packages=[
        "reqe"
    ],
    platforms=["all"],
    url="https://github.com/ophlr/reqe",
    install_requires=[
        "requests",
    ],
    classifiers=[
        'Development Status :: 5 - Production/Stable',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: Apache Software License',
        'Programming Language :: Python :: 3'
    ],
)


from common import aoc_22_common as aoc_22
import functools

def compare_lists(left, right):

        for i in range(0, len(left)):

            if (i > len(right) -1):
                return False

            if (type(left[i]) == type(right[i])):
                if (type(left[i]) is list):
                    result = compare_lists(left[i], right[i])
                    print(result)
                    if (result != None):
                        return result

                if (type(left[i]) is int):
                    if (left[i] < right[i]):
                        return True
                    elif (left[i] > right[i]):
                        return False
                    # If equal we continue checking

            if (type(left[i]) != type(right[i])):
                if (type(left[i]) is int):
                    return compare_lists([left[i]], right[i])
                else:
                    return compare_lists(left[i]    , [right[i]])

        # We have exhausted left, but have we also exhausted right?
        print("Exhausted left")
        if (len(left) < len(right)):
            return True

        # Inconclusive - inputs match exactly
        return None

class PacketPair:
    def __init__(self, packets_comprising_pair):
        self.left = eval(packets_comprising_pair[0])
        self.right = eval(packets_comprising_pair[1])

    def __str__(self):
        return str(self.left) + "|" + str(self.right)

    def inputs_in_right_order(self):
        left = self.left
        right = self.right

        return compare_lists(self.left, self.right)

packet_pairs = [PacketPair(packets) for packets in aoc_22.group_by_delimiter(aoc_22.load_file("day13/input"),"")]
pair = packet_pairs[1]
print (pair.inputs_in_right_order())

total_index_of_true = 0
for i in range(0, len(packet_pairs)):
    if packet_pairs[i].inputs_in_right_order():
        total_index_of_true += (i + 1)
print("Total " + str(total_index_of_true))

input_unsorted = []
for pair in packet_pairs:
    input_unsorted.append( pair.left)
    input_unsorted.append(pair.right)

DIVIDER_1 = [[2]]
DIVIDER_2 = [[6]]

input_unsorted.append(DIVIDER_1)
input_unsorted.append(DIVIDER_2)

def compare_packets(packet1, packet2):
    result = compare_lists(packet1, packet2)
    if (result):
        return - 1
    elif (not result):
        return 1
    else:
        return 0

sorted_packets = (sorted(input_unsorted, key=functools.cmp_to_key( compare_packets)))
# print (sorted(input_unsorted, key=functools.cmp_to_key( compare_packets)))
divider_1_index = 0
divider_2_index = 0

for i in range(0, len(sorted_packets)):
    print (sorted_packets[i])
    if (sorted_packets[i]) == DIVIDER_1:
        divider_1_index = i
    if (sorted_packets[i] == DIVIDER_2):
        divider_2_index = i

# The above indices are zero based but we want 1-based so add 1
print ((divider_1_index + 1) * (divider_2_index + 1))


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models

def forward(apps, schema_editor):
    ''' Copies any existing group this contact may have into groups'''
    Contact = apps.get_model("contacts", "Contact")
    for contact in Contact.objects.exclude(group=None):
        contact.groups.add(contact.group)

def backward(apps, schema_editor):
    ''' Takes the first group of contact's several groups
        and adds to the newly reproduced field 'group'.
        For development purposes, this seems resonable. '''
    Contact = apps.get_model("contacts", "Contact")
    for contact in Contact.objects.all():
        first_group = contact.groups.first()
        if first_group:
            contact.group = first_group
            contact.save()

class Migration(migrations.Migration):

    dependencies = [
        ('contacts', '0015_auto_20170307_1338'),
    ]

    operations = [
        migrations.RunPython(forward, backward)
    ]


import os
import numpy as np
import matplotlib.pyplot as plt
plt.rcdefaults()
from matplotlib.lines import Line2D
from matplotlib.patches import Rectangle
from matplotlib.collections import PatchCollection

NumConvMaxbig = 10
NumConvMaxsmall = 24
NumConvMaxtiny = 50
SmallOnFrom = 3
TinyOnFrom = 4
NumFcMax = 20
White = 1.
Light = 0.7
Medium = 0.5
Dark = 0.3
Black = 0.

def add_layer(patches, colors, sizex=24, sizey=24, num=5,
              top_left=[0, 0],
              loc_diff=[3, -3],
              ):
    # add a rectangle
    top_left = np.array(top_left)
    loc_diff = np.array(loc_diff)
    loc_start = top_left - np.array([0, sizex])
    for ind in range(num):
        patches.append(Rectangle(loc_start + ind * loc_diff, sizex, sizey))
        if ind % 2:
            colors.append(Medium)
        else:
            colors.append(Light)

def add_mapping(patches, colors, start_ratio, patch_sizex, patch_sizey, ind_bgn,
                top_left_list, loc_diff_list, num_show_list, size_list):

    start_loc = top_left_list[ind_bgn] \
        + (num_show_list[ind_bgn] - 1) * np.array(loc_diff_list[ind_bgn]) \
        + np.array([start_ratio[0] * size_list[ind_bgn],
                    -start_ratio[1] * size_list[ind_bgn]])

    end_loc = top_left_list[ind_bgn + 1] \
        + (num_show_list[ind_bgn + 1] - 1) \
        * np.array(loc_diff_list[ind_bgn + 1]) \
        + np.array([(start_ratio[0] + .5 * patch_sizex / size_list[ind_bgn]) *
                    size_list[ind_bgn + 1],
                    -(start_ratio[1] - .5 * patch_sizex / size_list[ind_bgn]) *
                    size_list[ind_bgn + 1]])

    patches.append(Rectangle(start_loc, patch_sizex, patch_sizey))
    colors.append(Dark)
    patches.append(Line2D([start_loc[0], end_loc[0]],
                          [start_loc[1], end_loc[1]]))
    colors.append(Black)
    patches.append(Line2D([start_loc[0] + patch_sizex, end_loc[0]],
                          [start_loc[1], end_loc[1]]))
    colors.append(Black)
    patches.append(Line2D([start_loc[0], end_loc[0]],
                          [start_loc[1] + patch_sizey, end_loc[1]]))
    colors.append(Black)
    patches.append(Line2D([start_loc[0] + patch_sizex, end_loc[0]],
                          [start_loc[1] + patch_sizey, end_loc[1]]))
    colors.append(Black)

def label(xy, text, xy_off=[0, 17]):
    plt.text(xy[0] + xy_off[0], xy[1] + xy_off[1], text,
             family='sans-serif', size=8)

if __name__ == '__main__':

    fc_unit_size = 2
    layer_width = 34

    patches = []
    colors = []

    fig, ax = plt.subplots()

    ############################
    # conv layers
    sizex_list = [30, 14, 8, 2, 1]
    sizey_list = [45, 14, 8, 2, 1]
    num_list = [8, 32, 64, 64, 256]
    x_diff_list = [0, 54, layer_width, layer_width, layer_width]
    text_list = ['Convolutional\ninputs'] + ['Feature\nmaps'] * (len(sizex_list) - 1)
    loc_diff_list = [[3, -3]] * SmallOnFrom + [[2, -2]] * (TinyOnFrom-SmallOnFrom) + [[1,-1]] * (len(sizex_list)-TinyOnFrom)

    bigs = num_list[:SmallOnFrom]
    smalls = num_list[SmallOnFrom:TinyOnFrom]
    tinys = num_list[TinyOnFrom:]
    num_show_list1 = list(map(min, bigs, [NumConvMaxbig] * len(bigs)))
    num_show_list2 = list(map(min, smalls, [NumConvMaxsmall] * len(smalls)))
    num_show_list3 = list(map(min, smalls, [NumConvMaxtiny] * len(tinys)))
    num_show_list = num_show_list1 + num_show_list2 +  num_show_list3

    top_left_list = np.c_[np.cumsum(x_diff_list), np.zeros(len(x_diff_list))]

    for ind in range(len(sizex_list)):
        add_layer(patches, colors, sizex=sizex_list[ind], sizey=sizey_list[ind],
                  num=num_show_list[ind],
                  top_left=top_left_list[ind], loc_diff=loc_diff_list[ind])
        label(top_left_list[ind], text_list[ind] + '\n{}@{}x{}'.format(
            num_list[ind], sizex_list[ind], sizey_list[ind]))

    ############################
    # in between layers
    start_ratio_list = [[0.4, 0.5], [0.4, 0.8], [0.4, 0.5], [0, 1]]
    patch_sizex_list = [4, 4, 3, 4]
    patch_sizey_list = [6, 4, 3, 4]
    stridex_list = [2, 2, 2, 2]
    stridey_list = [3, 2, 2, 2  ]
    ind_bgn_list = range(len(patch_sizex_list))
    text_list = ['Convolution', 'Convolution', 'Convolution', 'Convolution']

    for ind in range(len(patch_sizex_list)):
        add_mapping(patches, colors, start_ratio_list[ind],
                    patch_sizex_list[ind], patch_sizey_list[ind], ind,
                    top_left_list, loc_diff_list, num_show_list, sizex_list)
        label(top_left_list[ind], text_list[ind] + '\n{}x{} kernel\nstride ({},{})'.format(
            patch_sizex_list[ind], patch_sizey_list[ind], stridex_list[ind], stridey_list[ind]), xy_off=[26, -70])

    ############################
    # fully connected layers
    size_list = [fc_unit_size]
    num_list = [256]
    num_show_list = [20]
    x_diff_list = [sum(x_diff_list) + layer_width, layer_width, layer_width]
    top_left_list = np.c_[np.cumsum(x_diff_list), np.zeros(len(x_diff_list))]
    loc_diff_list = [[fc_unit_size, -fc_unit_size]] * len(top_left_list)
    text_list = ['Hidden\nunits'] #* (len(size_list) - 1) + ['Outputs']

    for ind in range(len(size_list)):
        add_layer(patches, colors, sizex=size_list[ind], sizey=size_list[ind], num=num_show_list[ind],
                  top_left=top_left_list[ind], loc_diff=loc_diff_list[ind])
        label(top_left_list[ind], text_list[ind] + '\n{}'.format(
            num_list[ind]))

    text_list = ['Flatten\n', 'Fully\nconnected', 'Fully\nconnected']

    for ind in range(len(size_list)):
        label(top_left_list[ind], text_list[ind], xy_off=[-10, -65])

    ############################
    # fully connected layers NUMBER TWO
    size_list = [fc_unit_size, fc_unit_size]
    num_list = [128, 64]
    num_show_list = [18, 10]
    x_diff_list = [4*layer_width+25, layer_width, layer_width]
    top_left_list = np.c_[np.cumsum(x_diff_list), np.zeros(len(x_diff_list))-94]
    loc_diff_list = [[fc_unit_size, -fc_unit_size]] * len(top_left_list)
    text_list = ['Flat inputs', 'Hidden units'] #* (len(size_list) - 1) + ['Outputs']

    for ind in range(len(size_list)):
        add_layer(patches, colors, sizex=size_list[ind], sizey=size_list[ind], num=num_show_list[ind],
                  top_left=top_left_list[ind], loc_diff=loc_diff_list[ind])
        label(top_left_list[ind], text_list[ind] + '\n{}'.format(
            num_list[ind]), xy_off=[0,5])

    text_list = ['', 'Fully\nconnected', 'Fully\nconnected']

    for ind in range(len(size_list)):
        label(top_left_list[ind], text_list[ind], xy_off=[-10, -55])

    ############################
    # fully connected layers CONCAT
    size_list = [fc_unit_size, fc_unit_size]
    num_list = [384, 256]
    num_show_list = [22, 20]
    x_diff_list = [7.1*layer_width,layer_width,layer_width]
    top_left_list = np.c_[np.cumsum(x_diff_list), np.zeros(len(x_diff_list))-40]
    loc_diff_list = [[fc_unit_size, -fc_unit_size]] * len(top_left_list)
    text_list = ['Hidden units', 'Hidden units'] #* (len(size_list) - 1) + ['Outputs']

    for ind in range(len(size_list)):
        add_layer(patches, colors, sizex=size_list[ind], sizey=size_list[ind], num=num_show_list[ind],
                  top_left=top_left_list[ind], loc_diff=loc_diff_list[ind])
        label(top_left_list[ind], text_list[ind] + '\n{}'.format(
            num_list[ind]), xy_off=[0,5])

    text_list = ['Concat', 'Fully\nconnected', 'Split']

    label(top_left_list[0], text_list[0], xy_off=[0,-35])
    label(top_left_list[1], text_list[1], xy_off=[-10, -55])

    ############################
    # fully connected layers SPLIT 1
    size_list = [fc_unit_size, fc_unit_size]
    num_list = [128, 21]
    num_show_list = [11, 7]
    x_diff_list = [9.4*layer_width, layer_width*.8]
    top_left_list = np.c_[np.cumsum(x_diff_list), np.zeros(len(x_diff_list))-20]
    loc_diff_list = [[fc_unit_size, -fc_unit_size]] * len(top_left_list)
    text_list = ['Advantage', ''] #* (len(size_list) - 1) + ['Outputs']

    for ind in range(len(size_list)):
        add_layer(patches, colors, sizex=size_list[ind], sizey=size_list[ind], num=num_show_list[ind],
                  top_left=top_left_list[ind], loc_diff=loc_diff_list[ind])
        label(top_left_list[ind], text_list[ind] + '\n{}'.format(
            num_list[ind]), xy_off=[0,5])

    text_list = ['Fully\nConnected', '', '']

    for ind in range(len(size_list)):
        label(top_left_list[ind], text_list[ind], xy_off=[10, -35])

    ############################
    # fully connected layers SPLIT 2
    size_list = [fc_unit_size, fc_unit_size]
    num_list = [128, 1]
    num_show_list = [11, 1]
    x_diff_list = [9.4*layer_width, layer_width*.8]
    top_left_list = np.c_[np.cumsum(x_diff_list), np.zeros(len(x_diff_list))-70]
    loc_diff_list = [[fc_unit_size, -fc_unit_size]] * len(top_left_list)
    text_list = ['Value', ''] #* (len(size_list) - 1) + ['Outputs']

    for ind in range(len(size_list)):
        add_layer(patches, colors, sizex=size_list[ind], sizey=size_list[ind], num=num_show_list[ind],
                  top_left=top_left_list[ind], loc_diff=loc_diff_list[ind])
        label(top_left_list[ind], text_list[ind] + '\n{}'.format(
            num_list[ind]), xy_off=[0,5])

    text_list = ['Fully\nConnected', '', '']

    for ind in range(len(size_list)):
        label(top_left_list[ind], text_list[ind], xy_off=[10, -35])

    ############################
    # fully connected layers FINAL CONCAT
    size_list = [fc_unit_size]
    num_list = [21]
    num_show_list = [7]
    x_diff_list = [11*layer_width,layer_width]
    top_left_list = np.c_[np.cumsum(x_diff_list), np.zeros(len(x_diff_list))-40]
    loc_diff_list = [[fc_unit_size, -fc_unit_size]] * len(top_left_list)
    text_list = ['Output'] #* (len(size_list) - 1) + ['Outputs']

    for ind in range(len(size_list)):
        add_layer(patches, colors, sizex=size_list[ind], sizey=size_list[ind], num=num_show_list[ind],
                  top_left=top_left_list[ind], loc_diff=loc_diff_list[ind])
        label(top_left_list[ind], text_list[ind] + '\n{}'.format(
            num_list[ind]), xy_off=[0,5])

    text_list = ['Matmul']

    for ind in range(len(size_list)):
        label(top_left_list[ind], text_list[ind], xy_off=[-10, -19])

    ############################
    colors += [0, 1]
    collection = PatchCollection(patches, cmap=plt.cm.gray)
    collection.set_array(np.array(colors))
    ax.add_collection(collection)
    plt.tight_layout()
    plt.axis('equal')
    plt.axis('off')
    plt.show()
    fig.set_size_inches(8, 2.5)

    fig_dir = './'
    fig_ext = '.png'
    fig.savefig(os.path.join(fig_dir, 'convnet_fig' + fig_ext),
                bbox_inches='tight', pad_inches=0)


# -*- coding: utf-8 -*-
from __future__ import division
import random, math
from django.core.urlresolvers import resolve
from django.shortcuts import render, get_object_or_404
from django.db.models import Prefetch, Count
from django.conf import settings
from web import models, utils
from web.constants import *

def globalContext(request, nav=NAV):
    rainbow = utils.getRainbowFromSize(len(nav))
    return {
        'current': resolve(request.path_info).url_name,
        'static_url': (settings.STATIC_FULL_URL + settings.STATIC_URL).replace('//static', '/static'),
        'site_name': 'db0.company',
        'site_description': 'Deby Lepage Official Website',
        'debug': settings.DEBUG,
        'static_files_version': '3',
        'rainbow': [(
            (index * (100/len(RAINBOW))),
            ((index + 1) * (100 / (len(RAINBOW)))),
            color,
        ) for index, color in enumerate(RAINBOW)],
        'nav': [tuple(list(info) + [rainbow[index]]) for index, info in enumerate(nav)],
    }

def generic(request):
    context = globalContext(request)
    return render(request, context['current'] + u'.html', context)

def index(request):
    context = globalContext(request, nav=NAV[1:])
    context['hide_nav'] = True
    return render(request, 'index.html', context)

def projects(request):
    context = globalContext(request)
    context['projects'] = models.Project.objects.all().order_by('-current', '-end', '-beginning')
    if 'tags' in request.GET:
        context['hide_nav'] = True
        context['back'] = '/tags/' if 'project' not in request.GET else request.GET['project']
        context['tags'] = []
        for tag in request.GET['tags'].split(','):
            context['tags'].append(tag)
            context['projects'] = context['projects'].filter(tags__name=tag)
        if not context['projects']:
            context['tags'] = []
        context['tags'] = ' - '.join(context['tags'])
    return render(request, 'projects.html', context)

def project(request, id):
    context = globalContext(request)
    context['hide_nav'] = True
    context['back'] = '/projects/'
    context['project'] = get_object_or_404(models.Project.objects.all().prefetch_related(Prefetch('stats', to_attr='all_stats'), Prefetch('tags', to_attr='all_tags')), id=id)
    total = len(context['project'].all_stats)
    context['percent_stats'] = 100 / int(total / int(math.ceil(total / 4))) if total else 0
    return render(request, 'project.html', context)

def social(request):
    context = globalContext(request)
    context['socials'] = models.SocialLink.objects.all().order_by('small', '-importance', 'name')
    context['show_tags'] = 'show_tags' in request.GET
    if context['show_tags']:
        context['socials'] = context['socials'].prefetch_related(Prefetch('tags', to_attr='all_tags'))
    if 'tags' in request.GET:
        context['hide_nav'] = True
        context['back'] = '/socialtags/'
        context['tags'] = []
        for tag in request.GET['tags'].split(','):
            context['tags'].append(tag)
            context['socials'] = context['socials'].filter(tags__name=tag)
        if not context['socials']:
            context['tags'] = []
        context['tags'] = ' - '.join(context['tags'])
    for (link, color) in zip(context['socials'], utils.getRainbowFromSize(len(context['socials']))):
        link.color = color
        if context['show_tags']:
            link.tags_string = u', '.join([tag.name for tag in link.all_tags])
    return render(request, 'social.html', context)

def faq(request):
    context = globalContext(request)
    context['questions'] = models.FAQ.objects.all().order_by('-importance')
    for (question, color) in zip(context['questions'], utils.getRainbowFromSize(len(context['questions']))):
        question.color = color
    return render(request, 'faq.html', context)

def tags(request):
    context = globalContext(request)
    context['hide_nav'] = True
    context['back'] = '/projects/'
    context['filter_url'] = '/projects/'
    context['tags'] = models.Tag.objects.all().order_by('?')
    return render(request, 'tags.html', context)

def socialtags(request):
    context = globalContext(request)
    context['hide_nav'] = True
    context['back'] = '/social/'
    context['filter_url'] = '/social/'
    context['tags'] = models.SocialTag.objects.all().order_by('?')
    return render(request, 'tags.html', context)

def public(request):
    context = globalContext(request)
    context['publics'] = models.Public.objects.all().order_by('-modification')
    context['show_tags'] = 'show_tags' in request.GET
    context['ajax'] = 'ajax' in request.GET
    context['page'] = int(request.GET.get('page', 0))
    if not request.user.is_superuser:
        context['publics'] = context['publics'].filter(private=False)
    if context['show_tags']:
        context['publics'] = context['publics'].prefetch_related(Prefetch('tags', to_attr='all_tags'))
    if 'tags' in request.GET:
        context['hide_nav'] = True
        context['back'] = '/publictags/'
        context['tags'] = []
        for tag in request.GET['tags'].split(','):
            context['tags'].append(tag)
            context['publics'] = context['publics'].filter(tags__name=tag)
        if not context['publics']:
            context['tags'] = []
        context['tags'] = ' - '.join(context['tags'])
    if 'search' in request.GET:
        context['hide_nav'] = True
        context['back'] = '/publictags/'
        context['search'] = request.GET['search']
        context['publics'] = context['publics'].filter(name__icontains=context['search'])
    context['next_page'] = context['page'] + 20
    context['publics'] = context['publics'][context['page']:context['next_page']]
    for (public, color) in zip(context['publics'], utils.getRainbowFromSize(len(context['publics']))):
        public.color = color
        if context['show_tags']:
            public.tags_string = u', '.join([tag.name for tag in public.all_tags])
    context['extends'] = 'ajax.html' if context['ajax'] else 'base.html'
    return render(request, 'public.html', context)

def publictags(request):
    context = globalContext(request)
    context['hide_nav'] = True
    context['back'] = '/public/'
    context['filter_url'] = '/public/'
    context['tags'] = models.PublicTag.objects.all().order_by('?')
    context['allow_search'] = True
    return render(request, 'tags.html', context)


# Создайте файл. Запишите в него N первых элементов последовательности Фибоначчи.
# 7 –> 0 1 1 2 3 5 8

n = int(input("Введите число: "))
a = 0
b = 1
data = open('Fibonacci.txt', 'w')
data.writelines(str(n) + ' -> ' +  str(a) + (' ') + str(b))
for i in range(3, n+1):
    i = a+b
    data.writelines((' ') + str(i))
    a = b
    b = i
data.close()


from telebot import types
from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton

import config
import telebot

from bd import add_to_airtable

bot = telebot.TeleBot(config.TOKEN)
print(config.TOKEN)

name = ""
username = ""
link = ""
city = ""
theme = ""
status = ""

@bot.message_handler(commands=['start', 'help'])
def start(message):
    print(message.from_user.first_name)
    if message.text == '/start':
        # TODO: Проверка на повторный ввод /start
        bot.send_message(message.chat.id,
                         'Привет!👋\nЯ Matching бот в Санкт-Петербурге 🤖 \n\nКаждую неделю я буду '
                         'предлагать вам для встречи интересного человека, случайно выбранного среди других '
                         'участников сообщества.\n\nДля старта ответьте на несколько вопросов.\n\n👉 Поехали 🚀')
        bot.send_message(message.chat.id, '☕ Напишите Имя и Фамилию')
        bot.register_next_step_handler(message, get_login)
    else:
        bot.send_message(message.from_user.id, 'Жми /start чтобы начать',
                         reply_markup=telebot.types.ReplyKeyboardRemove())

def get_login(message):
    # TODO: Проверка верно введенного Имя Фамилия
    global name
    global username
    name = message.text
    username = message.from_user.username
    choose_country(message)  # Next step

def choose_country(message):
    markup = types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=False)
    item1 = types.KeyboardButton("Санкт-Петербург")
    item2 = types.KeyboardButton("Москва")
    item3 = types.KeyboardButton("Другой город")
    markup.add(item1, item2, item3)
    bot.send_message(message.chat.id,
                     '🌎 Выберите свой город.\n\nТак мы определим, где вы сможете встретиться с партнёром 🤝',
                     reply_markup=markup)

@bot.message_handler(content_types=['text'])
def get_link(message):
    global theme
    if message.text == "Дизайн" or message.text == "Разработка" or message.text == "Стартапы":
        theme = message.text
        choose(message)

    global city
    if message.text == "Санкт-Петербург":
        city = message.text
        bot.send_message(message.from_user.id,
                         '🤳 Пришлите ссылку на свой профиль в любой соц. сети,где есть наиболее подробная информация о '
                         'вас.\n\nТак вы сможете лучше узнать друг о друге до встречи. Можно поставить прочерк, '
                         'если не хотите ничего указывать.,',
                         reply_markup=telebot.types.ReplyKeyboardRemove())
        bot.register_next_step_handler(message, save_link)
        print('op srabotalo')
    elif message.text == "Москва" or message.text == "Другой":
        bot.send_message(message.from_user.id, "Этот город пока не подключен",
                         reply_markup=telebot.types.ReplyKeyboardRemove())
        bot.register_next_step_handler(message, choose_country)

def save_link(message):
    print('9999999999999999')
    global link
    link = message.text
    choose_theme(message)

def choose_theme(message):
    print('0000000000000000000000000000000000')
    markup = types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=False)
    item1 = types.KeyboardButton("Дизайн")
    item2 = types.KeyboardButton("Разработка")
    item3 = types.KeyboardButton("Стартапы")
    markup.add(item1, item2, item3)
    bot.send_message(message.chat.id,
                     'Выберите свою предпочтительную дисциплину.',
                     reply_markup=markup)

def choose(message):
    print(add_to_airtable(name, username, link, city, theme, "Ready"))
    bot.send_message(message.chat.id,
                     'Получилось! 🙌\n\nТеперь вы — участник встреч 💪\n\nЧто дальше?\n\n1)  Каждый '
                     'понедельник мы будем присылать вашу пару для встречи в этот чат.\nНапишите партнеру в Telegram, '
                     'чтобы договориться о совместном походе на занятие.\nЕсли ваши предпочтения не совпали, '
                     'то вы можете просто выбраться на чашечку кофе.\nВремя и место вы выбираете сами.',
                     reply_markup=telebot.types.ReplyKeyboardRemove())

if __name__ == '__main__':
    bot.infinity_polling()


import os
import shutil
import sys
import time

import cv2
import numpy as np

def get_images(input_path):
    files = []
    exts = ['jpg', 'png', 'jpeg', 'JPG']

    for parent, dirnames, filenames in os.walk(input_path):

        for filename in filenames:
            for ext in exts:
                if filename.endswith(ext):
                    files.append(os.path.join(parent, filename))
                    break
    print('Find {} images'.format(len(files)))
    return files

def resize_image(img):
    img_size = img.shape
    img_size_min = np.min(img_size[0:2])
    img_size_max = np.max(img_size[0:2])

    img_scale = float(2400) / float(img_size_min)
    print("scale factor = "+str(img_scale))
    if np.round(img_scale * img_size_max) > 3600:
        img_scale = float(2400) / float(img_size_max)
    new_h = int(img_size[0] * img_scale)
    new_w = int(img_size[1] * img_scale)

    new_h = new_h if new_h // 16 == 0 else (new_h // 16 + 1) * 16
    new_w = new_w if new_w // 16 == 0 else (new_w // 16 + 1) * 16
    re_im = cv2.resize(img, (new_w, new_h), interpolation=cv2.cv2.INTER_CUBIC)
    return re_im, (new_h / img_size[0], new_w / img_size[1])

def remove_border(input_path, output_path):
    """remove P&ID drawing broder
    """
    if os.path.exists(output_path):
        shutil.rmtree(output_path)
    os.makedirs(output_path)
    img_fn_list = get_images(input_path)
    epsilon = 0.0001
    for img_fn in img_fn_list:
        print('===============')
        print(img_fn)
        start = time.time()
        try:
            img_gray = cv2.imread(img_fn,cv2.IMREAD_GRAYSCALE)
        except:
            print("Error reading image {}!".format(img_fn))
            continue
        h, w = img_gray.shape[:2]
        img_blank =  np.ones(shape=[h, w], dtype=np.uint8)*255
        img_binary = cv2.threshold(img_gray, 128, 255, cv2.THRESH_BINARY)[1]
        _, contours, _ = cv2.findContours(img_binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
        area = []
        for cnt in contours:
           approx = cv2.approxPolyDP(cnt,epsilon*cv2.arcLength(cnt,True),True)
           area .append(cv2.contourArea(cnt))
        # sort by contour area
        top_cnt_area = np.argsort(-1*np.array(area))
        # drawing has not been pre-processed
        # select the thrid largest contour which fit the drawing broader
        ind = top_cnt_area[2]
        approx = cv2.approxPolyDP(contours[ind],epsilon*cv2.arcLength(contours[ind],True),True)
        cv2.drawContours(img_blank, [approx], 0, (0), thickness = -1, lineType=8)
        # combine image with masks
        img_gray = cv2.bitwise_or(img_blank, img_gray)
        cv2.imwrite(os.path.join(output_path, os.path.basename(img_fn)), img_gray)

def thinning (input_path, output_path):
    """
     Morphological operation that is used to remove selected foreground pixels from binary images
    """
    if os.path.exists(output_path):
        shutil.rmtree(output_path)
    os.makedirs(output_path)
    img_fn_list = get_images(input_path)
    epsilon = 0.0001
    for img_fn in img_fn_list:
        print('===============')
        print(img_fn)
        start = time.time()
        try:
            img_gray = cv2.imread(img_fn,cv2.IMREAD_GRAYSCALE)
        except:
            print("Error reading image {}!".format(img_fn))
            continue
        # swap the color from black white to white black
        img= cv2.subtract(255, img_gray)

        img1 = img.copy()
        # Structuring Element
        kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
        # Create an empty output image to hold values
        thin = np.zeros(img.shape,dtype='uint8')

        # Loop until erosion leads to an empty set
        while (cv2.countNonZero(img1)!=0):
            # Erosion
            erode = cv2.erode(img1,kernel)
            # Opening on eroded image
            opening = cv2.morphologyEx(erode,cv2.MORPH_OPEN,kernel)
            # Subtract these two
            subset = erode - opening
            # Union of all previous sets
            thin = cv2.bitwise_or(subset,thin)
            # Set the eroded image for next iteration
            img1 = erode.copy()
        cv2.imwrite(os.path.join(output_path, os.path.basename(img_fn)), thin)

def skeleton(input_path, output_path):
    """
     Skeletonization is a process for reducing foreground regions in a binary
     image to a skeletal remnant that largely preserves the extent and connectivity
     of the original region while throwing away most of the original foreground pixels.
    """
    if os.path.exists(output_path):
        shutil.rmtree(output_path)
    os.makedirs(output_path)
    img_fn_list = get_images(input_path)
    epsilon = 0.0001
    for img_fn in img_fn_list:
        print('===============')
        print(img_fn)
        start = time.time()
        try:
            img_gray = cv2.imread(img_fn,cv2.IMREAD_GRAYSCALE)
        except:
            print("Error reading image {}!".format(img_fn))
            continue
        # swap the color from black white to white black
        img= cv2.subtract(255, img_gray)

        # Step 1: Create an empty skeleton
        size = np.size(img)
        skel = np.zeros(img.shape, np.uint8)

        # Get a Cross Shaped Kernel
        element = cv2.getStructuringElement(cv2.MORPH_CROSS, (3,3))
        # Repeat steps 2-4
        while True:
            #Step 2: Open the image
            open = cv2.morphologyEx(img, cv2.MORPH_OPEN, element)
            #Step 3: Substract open from the original image
            temp = cv2.subtract(img, open)
            #Step 4: Erode the original image and refine the skeleton
            eroded = cv2.erode(img, element)
            skel = cv2.bitwise_or(skel,temp)
            img = eroded.copy()
            # Step 5: If there are no white pixels left ie.. the image has been completely eroded, quit the loop
            if cv2.countNonZero(img)==0:
                break
        cv2.imwrite(os.path.join(output_path, os.path.basename(img_fn)), skel)


from project.formula_teams.mercedes_team import MercedesTeam
from project.formula_teams.red_bull_team import RedBullTeam

class F1SeasonApp:
	def __init__(self):
		self.red_bull_team = None
		self.mercedes_team = None

	@property
	def valid_teams(self):
		return ["Mercedes", "Red Bull"]

	def register_team_for_season(self, team_name: str, budget: int):
		if team_name not in self.valid_teams:
			raise ValueError("Invalid team name!")

		if team_name == 'Mercedes':
			self.mercedes_team = MercedesTeam(budget)
		elif team_name == "Red Bull":
			self.red_bull_team = RedBullTeam(budget)

		return f"{team_name} has joined the new F1 season."

	def get_race_results(self, race_name: str, red_bull_pos: int, mercedes_pos: int):
		team_ahead = "Red Bull" if red_bull_pos < mercedes_pos else "Mercedes"

		mercedes_revenue = self.mercedes_team.calculate_revenue_after_race(mercedes_pos)
		red_bull_revenue = self.red_bull_team.calculate_revenue(red_bull_pos)

		return f"Red Bull: {red_bull_revenue}. " \
		       f"Mercedes: {mercedes_revenue}. " \
		       f"{team_ahead} is ahead at the {race_name} race."

	def new_race_results(self, race_name: str, red_bull_pos: int, mercedes_pos: int):

		if self.red_bull_team is None or self.mercedes_team is None:
			raise Exception("Not all teams have registered for the season.")

		return self.get_race_results(race_name, red_bull_pos, mercedes_pos)


import sys
import re
import collections

# Can be on of Test, Normal or Verbose
mode = 'Normal'

orientation = ('N', 'S', 'E', 'W')
command = ('F', 'L', 'R')

turnLeftSwitcher = {
    'N': 'W',
    'S': 'E',
    'E': 'N',
    'W': 'S'
}

turnRightSwitcher = {
    'N': 'E',
    'S': 'W',
    'E': 'S',
    'W': 'N'
}

class Scent:
    'Definition of a scent'

    def __init__(self, xCoord, yCoord, orientation):
        self.xCoord = xCoord
        self.yCoord = yCoord
        self.orientation = orientation

    def compareScent(self, xCoord, yCoord, orientation):
        return (self.xCoord == xCoord) and (self.yCoord == yCoord) and (self.orientation == orientation)

class Map:
    'Definition of the map'

    __listScent = []

    def __init__(self, xSize, ySize):
        self.xSize = xSize
        self.ySize = ySize

    def addScent(self, Scent):
        self.__listScent.append(Scent)

    def checkScent(self, Scent):
        for i in range(len(self.__listScent)):
            if self.__listScent[i].compareScent(Scent.xCoord, Scent.yCoord, Scent.orientation):
                return True
        return False

    def printScent(self):
        for i in range(len(self.__listScent)):
            print(self.__listScent[i].xCoord, self.__listScent[i].yCoord)

class Robot:
    'Robot that will land in the planet landscape'

    isLost = False

    def __init__(self, origX, origY, orientation):
        self.xPos = origX
        self.yPos = origY
        self.orientation = orientation

    def move(self, command, Map):

        if self.isLost:
            return

        tempScent = Scent(self.xPos, self.yPos, self.orientation)

        if Map.checkScent(tempScent) and command == 'F':
            del tempScent
            return

        if command == 'F':  # Move Formard
            if self.orientation == 'N':
                self.yPos += 1
            elif self.orientation == 'S':
                self.yPos -= 1
            elif self.orientation == 'E':
                self.xPos += 1
            elif self.orientation == 'W':
                self.xPos -= 1
        elif command == 'L':  # Turn Left
            self.orientation = turnLeftSwitcher.get(self.orientation)
        elif command == 'R':  # Turn Right
            self.orientation = turnRightSwitcher.get(self.orientation)

        if self.xPos > Map.xSize or self.yPos > Map.ySize or self.xPos < 0 or self.yPos < 0:
            self.isLost = True
            self.xPos = tempScent.xCoord
            self.yPos = tempScent.yCoord
            Map.addScent(tempScent)

        return True

if mode == 'Verbose':
    inputString = raw_input("Please, input the planet size in this format: 23 32 (Max 50x50)\n")
    valid = bool(re.match("^(([0-9]|[0-4][0-9]|50)\s([0-9]|[0-4][0-9]|50))$", inputString))
    if valid != True:
        print("Incorrect input, the first line format is x y to define the size of the planet landscape. X and Y must to be between 1 and 50.")
        sys.exit(-1)

    listInput = inputString.split(' ')
    xSize = int(listInput[0])
    ySize = int(listInput[1])

    print("Planet size: {}x{}\n".format(xSize, ySize))

    listRobots = collections.OrderedDict()

    moreInput = True
    while moreInput:
        inputString = raw_input("Input the initial position of the robot in this format: 1 1 W\n")
        valid = bool(re.match("^(([0-9]|[0-4][0-9]|50)\s([0-9]|[0-4][0-9]|50)\s(W|E|N|S))$", inputString))
        if valid is not True:
            print("Incorrect input, the format must to be: X Y (one of W, N, S, E).")
            del inputString
            del valid
            del moreInput
            sys.exit(-1)
        listInput = inputString.split(' ')
        if int(listInput[0]) > xSize or int(listInput[1]) > ySize:
            print("Incorrect input, the coordinates for the robot need to be within the planet size.")
            del inputString
            del valid
            del moreInput
            del listInput
            sys.exit(-1)
        tempRobot = Robot(int(listInput[0]), int(listInput[1]), str(listInput[2]))
        inputString = raw_input("Input the list of commands for this robot. Commands valid are F, R or L. Max 100 commands.\n")
        valid = bool(re.match("^(R|F|L){1,100}$", inputString))
        if valid is not True:
            print("Incorrect input, wrong command found or too many commands.")
            del inputString
            del valid
            del moreInput
            del listInput
            del tempRobot
            sys.exit(-1)
        listRobots.update({tempRobot: inputString})
        inputString = raw_input("Do you want to add another robot? (y/n): \n")
        if inputString[0] == 'n' or inputString[0] == 'N':
            moreInput = False

    mars = Map(xSize, ySize)
    for key, value in listRobots.items():
        for i in value:
            key.move(i, mars)
        print("{} {} {} {}".format(key.xPos, key.yPos, key.orientation, 'LOST' if key.isLost else ''))
    del listRobots
    del inputString
    del valid
    del moreInput
    del listInput
    del tempRobot
    del mars
elif mode == 'Normal':
    inputString = raw_input()
    if not inputString:
        sys.exit(-1)
    valid = bool(re.match("^(([0-9]|[0-4][0-9]|50)\s([0-9]|[0-4][0-9]|50))$", inputString))
    if valid != True:
        sys.exit(-1)

    listInput = inputString.split(' ')
    xSize = int(listInput[0])
    ySize = int(listInput[1])

    listRobots = {}

    moreInput = True
    while moreInput:
        inputString = raw_input()
        if not inputString:
            break
        valid = bool(re.match("^(([0-9]|[0-4][0-9]|50)\s([0-9]|[0-4][0-9]|50)\s(W|E|N|S))$", inputString))
        if valid is not True:
            del inputString
            del valid
            del moreInput
            sys.exit(-1)
        listInput = inputString.split(' ')
        if int(listInput[0]) > xSize or int(listInput[1]) > ySize:
            del inputString
            del valid
            del moreInput
            del listInput
            sys.exit(-1)
        tempRobot = Robot(int(listInput[0]), int(listInput[1]), str(listInput[2]))
        inputString = raw_input()
        valid = bool(re.match("^(R|F|L){1,100}$", inputString))
        if valid is not True:
            del inputString
            del valid
            del moreInput
            del listInput
            del tempRobot
            sys.exit(-1)
        listRobots.update({tempRobot: inputString})
        inputString = raw_input()
        if inputString:
            inputString = raw_input()
            if inputString:
                moreInput = False

    mars = Map(xSize, ySize)
    for key, value in listRobots.items():
        for i in value:
            key.move(i, mars)
        print("{} {} {} {}".format(key.xPos, key.yPos, key.orientation, 'LOST' if key.isLost else ''))
    del listRobots
    del inputString
    del valid
    del moreInput
    del listInput
    del tempRobot
    del mars
else:
    print("---Test Mode---")
    # We initiliaze the planet landscape
    xSize = 5
    ySize = 3
    mars = Map(xSize, ySize)
    r1 = Robot(1, 1, 'E')
    r1.move('R', mars)
    r1.move('F', mars)
    r1.move('R', mars)
    r1.move('F', mars)
    r1.move('R', mars)
    r1.move('F', mars)
    r1.move('R', mars)
    r1.move('F', mars)
    print("{} {} {} {}".format(r1.xPos, r1.yPos, r1.orientation, 'LOST' if r1.isLost else ''))
    del r1

    r2 = Robot(3, 2, 'N')
    r2.move('F', mars)
    r2.move('R', mars)
    r2.move('R', mars)
    r2.move('F', mars)
    r2.move('L', mars)
    r2.move('L', mars)
    r2.move('F', mars)
    r2.move('F', mars)
    r2.move('R', mars)
    r2.move('R', mars)
    r2.move('F', mars)
    r2.move('L', mars)
    r2.move('L', mars)
    print("{} {} {} {}".format(r2.xPos, r2.yPos, r2.orientation, 'LOST' if r2.isLost else ''))
    del r2

    r3 = Robot(0, 3, 'W')
    r3.move('L', mars)
    r3.move('L', mars)
    r3.move('F', mars)
    r3.move('F', mars)
    r3.move('F', mars)
    r3.move('L', mars)
    r3.move('F', mars)
    r3.move('L', mars)
    r3.move('F', mars)
    r3.move('L', mars)
    print("{} {} {} {}".format(r3.xPos, r3.yPos, r3.orientation, 'LOST' if r3.isLost else ''))
    del r3

    del mars


"""
    Purpose: Use SIFT to extract features from a template image. This template image represents the object we are searching for. We can then
    use those features to check for the object in a larger, more uncontrolled scene.

    For further detail: David G. Lowe's paper on Distinctive Image Features from Scale-Invariant Keypoints

    Note: Query image --> template and training image --> search space
"""
#Libraries required (details added for Windows users)
import cv2 #pip install opencv-python
import numpy as np #pip install numpy
import matplotlib.pyplot as plt #pip install matplotlib
import os
import glob

#Detector basics
MIN_MATCH_COUNT=10 #Used to control how many matches are required before an object is "detected"
detector = cv2.SIFT_create() #SIFT object constructor

#Setting up Flann-based feature/description matcher to be used later (a bit faster than brute force for large datasets)
#to find k best matches for each descriptor from our query set
FLANN_INDEX_KDTREE=0
flannParam = dict(algorithm=FLANN_INDEX_KDTREE, tree=5) #builds dictionary (https://python-reference.readthedocs.io/en/latest/docs/functions/dict.html)
search_params = dict(checks=50)
flann = cv2.FlannBasedMatcher(indexParams=flannParam, searchParams=search_params) #will train flann on descriptor collection (of dicts)

templateImg = cv2.imread(filename="images/template_images/stop_sign_1.png", flags=0) #read the image that is used to train the classifier (aka a template to match on other images)
#Detection section (template image aka query image)
templateKP, templateDesc = detector.detectAndCompute(image=templateImg, mask=None) #used to detect keypoints (templateKP) and get the descriptors (templateDesc) in a single step
templateWithKP = cv2.drawKeypoints(image=templateImg, keypoints=templateKP, outImage=None, color=(255, 0, 0), flags=4) #draws keypoints previously computed
plt.imshow(X=templateWithKP) #X is an array-like or PIL image
plt.show() #shows plot with keypoints (for template image) only ONCE

#Image(s) that need to be searched
folder = "images/test_images" #folder with images that need checking

#iterate through test images and check all of them for stop signs
for filename in os.listdir(folder):
    unknownImg = cv2.imread(os.path.join(folder, filename)) #gets an image in the folder (not necessarily sorted)

    #error checking (wrong directory could cause problems)
    if unknownImg is not None:
        unknownImg = cv2.resize(src=unknownImg, dsize=(700, 330)) #resize the image (helps quite a bit with consistency)

        #Use this to see the directory image every time (unedited):
        # cv2.imshow(winname="Directory image", mat=unknownImg)
        # cv2.waitKey(delay=0)

        #Detection section (unknown image(s) aka training image(s))

        #used to detect keypoints (unknownKP) and get the descriptors (unknownDesc) in a single step
        unknownKP, unknownDesc = detector.detectAndCompute(image=unknownImg, mask=None)
        matches = flann.knnMatch(queryDescriptors=templateDesc, trainDescriptors=unknownDesc, k=2) #finds best 2 matches for each unknownDesc

        #Actual object/feature matching section
        reasonableMatch=[] #array that will be used to hold the best matches
        for m, n in matches:
            #if the Euclidean distance is reasonably close (aka L2 metric or L2 norm) for the template image and the image we are checking
            if(m.distance < (0.75*n.distance)):
                reasonableMatch.append(m) #add it

        #If there are more reasonable matches than the minimum that was previously set (aka a stop sign has been found)
        if(len(reasonableMatch) > MIN_MATCH_COUNT):
            temppt=[]
            unkpt=[]

            for m in reasonableMatch:
                temppt.append(templateKP[m.queryIdx].pt) #template points (source points)
                unkpt.append(unknownKP[m.trainIdx].pt) #points from image we are checking (destination points)
            temppt, unkpt = np.float32((temppt, unkpt)) #convert all to float32 data type

            #Must be 3D and don't know first dimension (not consistent)
            temppt = temppt.reshape(-1, 1, 2)
            unkpt = unkpt.reshape(-1, 1, 2)

            #Object/feature detection visualization calculations (using above calculations)
            #Finds a perspective transformation between two planes (aka maps the points in one point to the corresponding point in another image)
            M, mask = cv2.findHomography(srcPoints=temppt, dstPoints=unkpt, method=cv2.RANSAC, ransacReprojThreshold=3.0)
            matchesMask = mask.ravel().tolist()

            h, w = templateImg.shape #returns the dimensions of the template image (height = h, width = w)
            points = np.float32([[[0, 0], [0, h-1], [w-1, h-1], [w-1, 0]]]).reshape(-1, 1, 2) #convert all to float32 data type
            destination = cv2.perspectiveTransform(src=points, m=M) #used to align image detection

            #Bounding box options (all can coincide to compare, but ideally just choose one)
            print("Stop sign has been found in " + filename)

            #Rotated rectangle option
            # rot_rect = cv2.minAreaRect(destination)
            # box = cv2.boxPoints(rot_rect)
            # box = np.int0(box)
            # cv2.drawContours(unknownImg, [box], 0, (0,0,255), 2)
            # cv2.imshow(winname="Stop sign detection", mat=unknownImg)
            # cv2.waitKey(delay=0)

            #Straight rectangle option
            x,y,w,h = cv2.boundingRect(array=destination)
            cv2.rectangle(img=unknownImg, pt1=(x,y), pt2=(x+w, y+h), color=(0,255,0), thickness=2)
            cv2.imshow(winname="Stop sign found", mat=unknownImg)
            cv2.waitKey(delay=0)

            #Matched image visualization
            draw_params = dict(matchColor=(0,255,0), singlePointColor=None, matchesMask=matchesMask, flags=2) #specifying custom parameters (flag = 2 means only matched keypoints shown)
            matchedImg = cv2.drawMatches(templateImg, templateKP, unknownImg, unknownKP, reasonableMatch, None, **draw_params) #draw keypoints and connectors
            plt.imshow(X=matchedImg, cmap='gray')
            plt.show()

        #if not enough matches (aka a stop sign has not been found)
        else:
            print("No stop signs have been found in " + filename)
            matchesMask=None
            cv2.imshow(winname="No match found", mat=unknownImg)
            cv2.waitKey(delay=0)

cv2.destroyAllWindows() #will close any windows left open if the program is forcibly stopped


from tools import colors
from database.dbClass import DB
import threading

def test(corres:str):
    db = DB(corres)
    try:
        # Create threads to run multiple tests
        threads = [threading.Thread(target=db.test1()) for i in range(1, 1000)]

        # Start threads
        for thread in threads:
            thread.start()

        for thread in threads:
            thread.join()

        # Wait for the threads to finish
        # Thread1.join()
        # Thread2.join()

        print(colors.bcolors.OKGREEN + "Tests OK !" + colors.bcolors.ENDC)
    except:
        raise Exception(colors.bcolors.FAIL + "Error during tests" + colors.bcolors.ENDC)


import time
from cereal import car
from common.numpy_fast import clip, interp
from common.realtime import DT_CTRL
from selfdrive.controls.lib.drive_helpers import CONTROL_N, apply_deadzone
from selfdrive.controls.lib.pid import PIDController
from selfdrive.modeld.constants import T_IDXS

LongCtrlState = car.CarControl.Actuators.LongControlState

LINEAL = 1
TRAPEZOIDAL = 5
REACTIVO = 9

def long_control_state_trans(CP, active, long_control_state, v_ego, v_target,
                            v_target_1sec, brake_pressed, cruise_standstill):
  # Ignore cruise standstill if car has a gas interceptor
  cruise_standstill = cruise_standstill and not CP.enableGasInterceptor
  accelerating = v_target_1sec > v_target
  planned_stop = (v_target < CP.vEgoStopping and
                  v_target_1sec < CP.vEgoStopping and
                  not accelerating)
  stay_stopped = (v_ego < CP.vEgoStopping and
                  (brake_pressed or cruise_standstill))
  stopping_condition = False  # planned_stop or stay_stopped

  starting_condition = (v_target_1sec > CP.vEgoStarting and
                        accelerating and
                        not cruise_standstill and
                        not brake_pressed)
  started_condition = v_ego > CP.vEgoStarting

  if not active:
    long_control_state = LongCtrlState.off

  else:
    if long_control_state in (LongCtrlState.off, LongCtrlState.pid):
      long_control_state = LongCtrlState.pid
      if stopping_condition:
        long_control_state = LongCtrlState.stopping

    elif long_control_state == LongCtrlState.stopping:
      if starting_condition and CP.startingState:
        long_control_state = LongCtrlState.starting
      elif starting_condition:
        long_control_state = LongCtrlState.pid

    elif long_control_state == LongCtrlState.starting:
      if stopping_condition:
        long_control_state = LongCtrlState.stopping
      elif started_condition:
        long_control_state = LongCtrlState.pid

  return long_control_state

class planner:
  # vel_vector: vector de velocidades deseadas
  # time_vector: vector de tiempos de cambio deseados
  # tipo: tipo de suavizado y transición entre velocidades
  def __init__(self, tipo):
    # --- Variables
    self.perfil = 0
    self.fase = 0
    self.tipo = tipo
    self.vel_vector = []
    self.time_vector = []
    self.separator = ','
    if self.tipo == 5:
      self.archivo = '../controls/tests/perfil_trapezoidal.csv'
      with open(self.archivo, 'r') as _archivo:
        for linea in _archivo:
          linea = linea.rstrip()
          lista = linea.split(self.separator)
          self.time_vector.append(float(lista[0]))
          self.vel_vector.append(float(lista[1]))
    # Velocidades y aceleraciones
    self.enable = False
    self.t_a = 10000.0
    self.acc_max = 0.5
    self.stage = 0
    self.cont = 1
    self.change_stage = False
    self.t_ini = (time.time_ns()-self.t_ini)/1000000000.0
    self.v_target = 5.5  # m/s
    self.v_ini = 0.0
    self.t_1 = 0.0
    self.t_2 = 0.0
    self.tau = self.v_target/self.acc_max

  # time: tiempo actual de simulación en segundos
  def update(self, t, CS):
    if CS.rightBlinker and self.perfil == 0:
      self.enable = True
      self.perfil = 1
      self.fase = 1
      self.t_1 = t
    if CS.rightBlinker and self.fase == 0:
      self.enable = True
      self.fase = -1
      self.t_2 = t
    if CS.leftBlinker and self.perfil == 0:
      self.enable = False
    if self.tipo == TRAPEZOIDAL and self.enable:
      vel = self.read_trapezoidal(t-self.t_ini)
    elif self.tipo == REACTIVO and self.enable:
      vel = self.set_speed(t-self.t_ini, CS)
    else:
      vel = 0.0
    return vel, self.enable

  # time: tiempo actual de simulación en segundos
  def read_trapezoidal(self, t):
    longitud = len(self.time_vector)
    if t > self.time_vector[self.cont] and t < self.time_vector[longitud-1]:
      self.cont += 1
    return self.vel_vector[self.cont]

  def set_speed(self, t, CS):
    vel = CS.vEgo
    if self.perfil == 1:
      if self.fase == 1:
        if (t-self.t_1 <= self.tau):
          vel = self.acc_max*(t-self.t_1)
        else:
          self.fase = 0
      if self.fase == -1:
        if (t-self.t_2 <= self.tau):
          vel = self.v_target-self.acc_max*(t-self.t_2)
    if vel > self.v_target:
      vel = self.v_target
    elif vel < self.v_ini:
      vel = self.v_ini
    self.cont += 1
    return vel

class LongControl:
  def __init__(self, CP):
    self.CP = CP
    self.long_control_state = LongCtrlState.off  # initialized to off
    self.pid = PIDController((CP.longitudinalTuning.kpBP, CP.longitudinalTuning.kpV),
                              (CP.longitudinalTuning.kiBP,
                              CP.longitudinalTuning.kiV),
                              k_f=CP.longitudinalTuning.kf, rate=1 / DT_CTRL)
    self.v_pid = 0.0
    self.last_output_accel = 0.0
    self.vpid_flag = False
    self.t_ini = time.time_ns()
    self.vel_profile = planner(REACTIVO)

  def reset(self, v_pid):
    """Reset PID controller and change setpoint"""
    self.pid.reset()
    self.v_pid = v_pid
    self.vpid_flag = False

  def update(self, active, CS, long_plan, accel_limits, t_since_plan):
    """Update longitudinal control. This updates the state machine and runs a PID loop"""
    # Interp control trajectory
    speeds = long_plan.speeds
    if len(speeds) == CONTROL_N:
      v_target_now = interp(t_since_plan, T_IDXS[:CONTROL_N], speeds)
      a_target_now = interp(
          t_since_plan, T_IDXS[:CONTROL_N], long_plan.accels)

      v_target_lower = interp(
          self.CP.longitudinalActuatorDelayLowerBound + t_since_plan, T_IDXS[:CONTROL_N], speeds)
      a_target_lower = 2 * (v_target_lower - v_target_now) / \
          self.CP.longitudinalActuatorDelayLowerBound - a_target_now

      v_target_upper = interp(
          self.CP.longitudinalActuatorDelayUpperBound + t_since_plan, T_IDXS[:CONTROL_N], speeds)
      a_target_upper = 2 * (v_target_upper - v_target_now) / \
          self.CP.longitudinalActuatorDelayUpperBound - a_target_now

      v_target = min(v_target_lower, v_target_upper)
      a_target = min(a_target_lower, a_target_upper)

      v_target_1sec = interp(self.CP.longitudinalActuatorDelayUpperBound +
                              t_since_plan + 1.0, T_IDXS[:CONTROL_N], speeds)
    else:
      v_target = 0.0
      v_target_now = 0.0
      v_target_1sec = 0.0
      a_target = 0.0

    self.pid.neg_limit = accel_limits[0]
    self.pid.pos_limit = accel_limits[1]

    output_accel = self.last_output_accel
    # Diferencial de tiempo en segundos
    dt_ini = (time.time_ns()-self.t_ini)/1000000000.0

    self.long_control_state = long_control_state_trans(self.CP, active, self.long_control_state, CS.vEgo,
                                                        v_target, v_target_1sec, CS.brakePressed,
                                                        CS.cruiseState.standstill)

    if self.long_control_state == LongCtrlState.off:
      self.reset(CS.vEgo)
      output_accel = 0.

    elif self.long_control_state == LongCtrlState.stopping:
      if output_accel > self.CP.stopAccel:
        output_accel = min(output_accel, 0.0)
        output_accel -= self.CP.stoppingDecelRate * DT_CTRL
      self.reset(CS.vEgo)

    elif self.long_control_state == LongCtrlState.starting:
      output_accel = self.CP.startAccel
      self.reset(CS.vEgo)

    elif self.long_control_state == LongCtrlState.pid:
      v_pid, self.vpid_flag = self.vel_profile.update(dt_ini, CS)
      if self.vpid_flag:
        self.v_pid = v_pid
      else:
        self.v_pid = v_target_now

      # Toyota starts braking more when it thinks you want to stop
      # Freeze the integrator so we don't accelerate to compensate, and don't allow positive acceleration
      # TODO too complex, needs to be simplified and tested on toyotas
      prevent_overshoot = not self.CP.stoppingControl and CS.vEgo < 1.5 and v_target_1sec < 0.7 and v_target_1sec < self.v_pid
      deadzone = interp(CS.vEgo, self.CP.longitudinalTuning.deadzoneBP,
                        self.CP.longitudinalTuning.deadzoneV)
      freeze_integrator = prevent_overshoot

      error = self.v_pid - CS.vEgo
      error_deadzone = apply_deadzone(error, deadzone)
      output_accel = self.pid.update(error_deadzone, speed=CS.vEgo,
                                      feedforward=a_target,
                                      freeze_integrator=freeze_integrator)

    self.last_output_accel = clip(
        output_accel, accel_limits[0], accel_limits[1])

    return self.last_output_accel


#DESAFIO1: Crie um programa que leia o nome de uma pessoa e mostre uma mensagem de boas-vindas
#de acordo com o valor digitado
nome = input('Digite seu nome: ')
print('Seja bem vindo(a)', nome, '! Prazer em te conhecer!')

#DESAFIO2: Crie um programa que leia o dia, o mês e o ano de nascimento de uma pessoa e mostre
#uma mensagem com a data formatada.
dia = input('Digite seu dia de nascimento: ')
mes = input('Digite seu mes de nascimento: ')
ano = input('Digite seu ano de nascimento: ')
print('Você nasceu dia', dia, 'de', mes, 'de', ano)

#DESAFIO3: Crie um programa que leia dois numeros e tente mostra a soma entre eles.
num1 = input('Digite um primeiro número: ')
num2 = input('Digite um segundo número: ')
#soma = int(num1) + int(num2)
print('A soma de', num1, '+', num2, 'é igual a', (int(num1) + int(num2)))


#OPEN THE FILE

filename = input('enter filename: ')

file = open(filename, encoding="UTF-8")

#TOTAL WORD COUNT IS LOWER THAN EXPECTED. SOLUTIONS:
#1. USE ANOTHER TYPE OF ENCODING
#2. OR OPEN 98-0 FILE USING ANOTHER FILE EXTENSION

print(file)

#CREATE A DICTIONARY TO HOLD THE CONTENTS OF THE FILE

dict1={}

#OPEN YOUR EXCLUSION LIST
stopword = set(word.strip() for word in open("stopwords.txt"))

#print(stopword)

#CREATE A LOOP TO RAED THE DATA

for word in file.read().lower().split():

	#CLEAN DATA

	word = word.replace(",", "")
	word = word.replace("“", "")
	word = word.replace("\"", "")
	word = word.replace(".", "")

	if word not in stopword:

		#ADD TO THE DICT

		if word not in dict1:

			dict1[word] = 1

		else:

			dict1[word] +=1

'''
#CHECK FOR WHATS IN THE DICTIONARY
for i,j in dict1.items():
	print(i, ",", j)
'''

#CREATE A LIST TO HOLD THE WORDS IN THE DICTIONARY. USING THE LIST, THE ITEMS CAN BE SORTED AND COUNTED

list1=[]

#TRANSFER THE WORDS TO THE LIST, ONE WORD AT A TIME

for i,j in dict1.items():

	list1.append(i)

'''
#CHECK WHATS IN THE LIST
for i in list1:
	print(i)
'''

'''
#IMPORT THE COLLECTIONS MODULE

#import collections

#ACCESS THE COUNTER METHOD IN THE COLLECTIONS MODULE
#PASS THE DICTIONARY INTO THE METHOD AND ASSIGN IT TO A VARIABLE

#tally = collections.Counter(dict1)

#COUNT AND PRINT OUT THE 10 MOST FREQUENTLY OCCURING ITEMS IN THE DICT

for word, count in (tally.most_common(10)):
	print(word, ",", count)

'''

'''
#THIS IS TO CHECK IF THE TALLY OPERATION BELOW ACHIEVES THE DESIRED OBJ
#IMPORT THE COLLECTIONS MODULE

import collections

#ACCESS THE COUNTER METHOD IN THE COLLECTIONS MODULE
#ASSIGN THE COUNTER METHOD TO A TALLY VARIABLE

tally = collections.Counter()

#LOOP THROUGH THE LIST AND COUNT THE FREQUENCY OF EACH ITEM

for i in list1:

	tally[i] += 1

	print(i, tally[i])

print(tally)
'''

#IMPORT THE COUNTER MODULE

from collections import Counter

#COUNT AND PRINT OUT THE 10 MOST FREQUENTLY OCCURING ITEMS IN THE LIST

for word, count in (Counter(dict1).most_common(10)):
	print(word, ",", count)


import re
import matplotlib.pyplot as plt
import matplotlib.widgets as widg

book = open('journal-1900.txt', encoding='utf-8')
book_string = book.read()
book.close()

# some wired punctuation characters that you cannot type, you have to copy them
book_string_nopunc = re.sub('[.!?“”,‘’:;]', '', book_string)

# two types of dashes (oh god)
book_string_nopunc = re.sub('[—-]', ' ', book_string_nopunc)

# seems to work
# print(book_string_nopunc)

# now we need to remove all uppercase characters
book_string_nofrills = book_string_nopunc.lower()
# print(book_string_nofrills)

# now we need a list of words from a paragraph
words = book_string_nofrills.split()
words.sort()
# print(words)

words_and_count = {}

for word in words:
    if word in words_and_count:
        words_and_count[word] += 1
    else:
        words_and_count[word] = 1

# graph words with more than threshold
words_and_count_hicount = {}

thresh = 25
for word in words_and_count:
    if words_and_count[word] > 25:
        words_and_count_hicount[word] = words_and_count[word]

print(words_and_count_hicount)

# getting words_x and count_y from words_and_count_hicount
words_x = [word for word in words_and_count_hicount]
count_y = [words_and_count_hicount[word] for word in words_and_count_hicount]

# bar graph time!

# number of words
words_number = len(words_x)
indexes = list(range(words_number))

fig, ax = plt.subplots()

plt.bar(indexes, count_y)
plt.xticks(indexes, words_x)

# make a bar graph that shows 20 words at a time
# and has a slider which allows horizontal navigation

# [left, bottom, width, height]
slider_ax = plt.axes([0.15, 0.9, 0.5, 0.1])
slider = widg.Slider(slider_ax, 'sector', 0, 1, valinit=0)

ax.set_xlim(-0.5, 20.5)

def sector(val):
    left_edge = (words_number - 21) * slider.val - 0.5
    ax.set_xlim(left_edge, left_edge + 21)

slider.on_changed(sector)

plt.show()


from os import environ

import pandas as pd
import numpy as np
import pymongo

from logger import logger
from app.models import MISSING_MESSAGE

class HSBCModel:
    def __init__(self, document):
        self.df = pd.read_excel(document)
        self.__client = pymongo.MongoClient(environ.get("MONGO_URI"))
        self.__db = self.__client.robin_hood
        self.collection = self.__db[environ.get("HSBC_COLLECTION")]
        self.statments_collection = self.__db[
            environ.get("ACCOUNT_STATMENT_COLLECTION")
        ]
        self.mandatory_columns = [
            "Nombre de la cuenta",
            "Número de cuenta",
            "Nombre del banco",
            "Divisa",
            "País",
            "Estatus de la cuenta",
            "Tipo de cuenta",
            "Referencia del Banco",
            "Narrativa adicional",
            "Referencia del cliente",
            "Tipo TRN",
            "Importe del abono",
            "Importe del cargo",
            "Saldo",
            "Fecha posterior",
        ]

    def __verified_accounts(self):
        is_valid = True
        missing_columns = []
        columns = list(self.df.columns)
        for column in columns:
            if column not in self.mandatory_columns:
                is_valid = False
                missing_columns.append(column)

        return is_valid, missing_columns

    def __clean_data(self):
        self.df["Importe del cargo"] = self.df["Importe del cargo"].replace(np.nan, 0)
        self.df["Importe del abono"] = self.df["Importe del abono"].replace(np.nan, 0)
        self.df["Fecha posterior"] = self.df["Fecha posterior"].apply(
            lambda row: str(row) if row is not None else ""
        )
        self.df = self.df.rename(
            columns={"País": "Pais", "Número de cuenta": "Numero de cuenta"}
        )

    def __remove_trash_key(self, item: dict):
        if "index" in item:
            item.pop("index")
        elif "level_0" in item:
            item.pop("level_0")
        return item

    def __change_key_name(self, item: dict):
        item["divisa"] = item.pop("Divisa")
        item["estatus"] = item.pop("Estatus de la cuenta")
        item["fecha"] = item.pop("Fecha posterior")
        item["abono"] = str(item.pop("Importe del abono"))
        item["cargo"] = str(item.pop("Importe del cargo"))
        item["descripcion"] = item.pop("Narrativa adicional")
        item["nombre"] = item.pop("Nombre de la cuenta")
        item["banco"] = item.pop("Nombre del banco")
        item["cuenta"] = str(item.pop("Numero de cuenta"))
        item["pais"] = item.pop("Pais")
        item["referencia_banco"] = str(item.pop("Referencia del Banco"))
        item["referencia_cliente"] = item.pop("Referencia del cliente")
        item["saldo"] = str(item.pop("Saldo"))
        item["tipo_trn"] = item.pop("Tipo TRN")
        item["tipo_cuenta"] = item.pop("Tipo de cuenta")
        return item

    def __get_account_statments(self, item: dict):
        account = {}
        account["cliente"] = str(item["Referencia del cliente"])
        account["empresa"] = ""
        account["banco"] = item["Nombre del banco"]
        account["cuenta"] = str(item["Numero de cuenta"])
        account["descripcion"] = item["Narrativa adicional"]
        account["referencia"] = str(item["Referencia del Banco"])
        account["fecha"] = item["Fecha posterior"]
        account["deposito"] = str(item["Importe del abono"])
        account["retiro"] = str(item["Importe del cargo"])
        account["saldo"] = str(item["Saldo"])
        return account

    def get_account_statment_dict(self):
        data = self.df
        data.reset_index(inplace=True)
        statment = data.to_dict("records")
        statment = list(map(self.__remove_trash_key, statment))
        statment = list(map(self.__change_key_name, statment))
        return statment

    def get_statment_dict(self, bank, client, company):
        data = self.df
        data.reset_index(inplace=True)
        statments = data.to_dict("records")
        statments = list(map(self.__get_account_statments, statments))
        return statments

    def save_statments(self, bank, client, company):
        is_valid, missing_columns = self.__verified_accounts()
        if not is_valid:
            raise Exception(MISSING_MESSAGE.format(str(missing_columns)))
        self.__clean_data()
        try:
            self.collection.insert_many(self.get_account_statment_dict())
            self.statments_collection.insert_many(
                self.get_statment_dict(bank, client, company)
            )
            return True
        except Exception as e:
            logger.error(e)
            return False


from flask import Flask, render_template, json, request
from flaskext.mysql import MySQL
from mymoodanalyzer import setup, checkin
from requests_oauthlib import OAuth1

app = Flask(__name__)

labels = ['POS', 'NEG', 'OTHER']
colors = ["#32CD32", "#DC143C", "#D3D3D3"]

@app.route('/')
def main():
	return render_template('index.html')

@app.route('/result', methods=['POST'])
def handle_data():
	url = 'https://api.twitter.com/1.1/account/verify_credentials.json'
	auth = OAuth1(your_key, your_api_secret, your_access_token, your_access_token_secret)
	setup(url, auth)
	name = request.form['handle']
	output = checkin(name,auth)
	values = [output["pos_per"], output["neg_per"], 100-output["pos_per"]-output["neg_per"]]
	return render_template('result.html', output=output, set=zip(values, labels, colors))

if __name__ == "__main__":
    app.run()


from django.shortcuts import render, redirect
from django.views.decorators.http import require_http_methods
from Inventory.utils import get_all, delete_inventory_sheet, delete_invetory_item
from Inventory.models import Warehouse, InventorySheet, InventoryDiscription
from Inventory.forms import InventoryDiscriptionForm

# Create your views here.

def Home_view(request):
    context = {}
    if request.POST and "ID" in request.POST:
        ID = request.POST.get("ID")
        return redirect("edit", pk=ID)

    if request.POST and "DID" in request.POST:
        ID = request.POST.get("DID")
        delete_inventory_sheet(ID)
        context["Inventories"] = get_all(InventorySheet)
        return render(request, "home.html", context)

    if request.POST and "CID" in request.POST:
        return redirect("create")

    else:
        context["Inventories"] = get_all(InventorySheet)
        return render(request, "home.html", context)

def Edit_view(request, pk):

    if request.POST and "AID" in request.POST:
        return redirect("item", pk=pk)

    if request.POST and "DID" in request.POST:
        delete_invetory_item(request.POST.get("DID"))
        return redirect("edit", pk=pk)

    if request.POST and "Save" in request.POST:
        obj = InventorySheet.objects.get(id=pk)
        if request.POST.get("WID") == "None":
            obj.Assigned_Warehouse = None

        else:
            obj.Assigned_Warehouse = Warehouse.objects.get(
                WareHouseId=request.POST.get("WID")
            )
        obj.save()
        return redirect("edit", pk=pk)
    context = {}
    context["ItemList"] = get_all(InventoryDiscription, ID=pk)
    context["WareHouses"] = get_all(Warehouse)
    obj = InventorySheet.objects.get(id=pk)

    context["current_W"] = obj.Assigned_Warehouse

    context["id"] = pk
    return render(request, "Inventory.html", context)

def Create_view(request):
    if request.POST:
        date = request.POST.get("date")
        WID = request.POST.get("WID")
        if WID != "None":
            W = Warehouse.objects.get(WareHouseId=WID)
            obj = InventorySheet(Date=date, Assigned_Warehouse=W)
        else:
            obj = InventorySheet(Date=date)
        obj.save()

        return redirect("edit", pk=obj.id)
    context = {}

    context["Warehouse"] = get_all(Warehouse)
    return render(request, "create.html", context)

def item_view(request, pk):
    if request.POST:
        InventoryDiscriptionForm(request.POST.dict(), pk)

        return redirect("edit", pk=pk)

    context = {}
    context["id"] = pk

    return render(request, "item.html", context)

def Warehouse_view(request):
    if request.POST:
        data = request.POST.dict()
        obj = Warehouse(
            WareHouseId=data["WareHouseId"], Name=data["Name"], Address=data["Address"]
        )
        obj.save()
        return redirect("home")
    return render(request, "ware.html")


from turtle import Screen
from time import sleep
from snake import Snake
from food import Food
from score import Scoreboard
from wall import Wall

def off():
    global game_is_on
    game_is_on = False

AXIS = 280

# Creating and setting up screen
screen = Screen()
screen.tracer(0)
screen.bgcolor("black")
screen.title("The Snake Game")
screen.setup(width=610, height=610)

# creating wall
wall = Wall()
wall.create_wall()

# creating wall
snake = Snake()

# creating food
food = Food()

# creating scoreboard
scoreboard = Scoreboard()
screen.listen()

# setting up key press
screen.onkey(fun=snake.up, key="Up")
screen.onkey(fun=snake.down, key="Down")
screen.onkey(fun=snake.right, key="Left")
screen.onkey(fun=snake.left, key="Right")

# starting game
game_is_on = True
while game_is_on:
    screen.update()
    sleep(.1)
    snake.move()

    # setting conduction for snake to eat food
    if snake.head.distance(food) < 15:
        food.move()
        snake.grow()
        scoreboard.eat()

    # setting conduction for snake to byte tail
    for segment in snake.segments[1:]:
        if snake.head.distance(segment) < 10:
            scoreboard.final()
            snake.reset()

    # setting conduction for snake to hit wall
    if snake.head.xcor() > AXIS or snake.head.xcor() < -AXIS or snake.head.ycor() > AXIS or snake.head.ycor() < -AXIS:
        scoreboard.final()
        snake.reset()

    # setting key to stop the game
    screen.onkey(key="s", fun=off)

screen.exitonclick()

Day 24/Program Day 24/Sanke Game Updated/main.py