import click
from datetime import timedelta
from pathlib import Path
from typing import Iterable

# this library does not come with type stubs, so we tell mypy to ignore it
import pytimeparse  # type: ignore

from urbasys import log_utils
from urbasys.urbackup import retention

BACKUP_ROOT_DIR = "/urbackup/mirror"

@click.group()
@click.option("-v", "--verbose", count=True)
@click.option("-q", "--quiet", count=True)
def main(verbose, quiet):
    log_utils.setup_logging(verbose, quiet)

@main.command(name="retain-monthlies", help="Keeps one backup snapshot per month.")
@click.argument(
    "backups-root-dirs",
    nargs=-1,
    type=click.Path(path_type=Path, dir_okay=True, exists=True),
)
@click.option("-n", "--dry-run", is_flag=True)
@click.option(
    "--keep-latest",
    default=30,
    type=int,
    help="The number of latest snapshots to keep. "
    "After that only the oldest snapshot of the month will be kept.",
)
def retain_monthlies(
    backups_root_dirs: Iterable[Path], dry_run: bool, keep_latest: int
) -> None:
    for backup_root_dir in backups_root_dirs:
        retention.retain_monthlies(backup_root_dir, dry_run, keep_latest)

@main.command(
    name="delete-old",
    help="Deletes backup folders older than a certain amount of time.",
)
@click.argument(
    "backups-root-dirs",
    nargs=-1,
    type=click.Path(path_type=Path, dir_okay=True, exists=True),
)
@click.option("-n", "--dry-run", is_flag=True)
@click.option(
    "--max-age",
    required=True,
    type=lambda arg: timedelta(seconds=pytimeparse.parse(arg)),
    help="Snapshots older than this will be deleted.",
)
@click.option(
    "--min-keep",
    default=5,
    type=int,
    help="The minimum number of snapshots to keep (even if they are older than `--max-age`).",
)
def delete_old(
    backups_root_dirs: Iterable[Path], dry_run: bool, max_age: timedelta, min_keep: int
) -> None:
    for backup_root_dir in backups_root_dirs:
        retention.delete_old(backup_root_dir, dry_run, max_age, min_keep)


from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.db.models import CharField, Value, Q
from itertools import chain

from authentication.models import User
from website.models import Review, UserFollows, Ticket
from website import forms

@login_required
def flux(request):
    """
    Gérer le flux pour afficher les tickets et les avis de l'utilisateur et de ses abonnés
    """

    # Récupérer les identifiants des utilisateurs suivis par l'utilisateur connecté
    users_followed_ids = UserFollows.objects.filter(user=request.user).values_list('followed_user', flat=True)

    # Récupérer les avis associés aux tickets pour l'utilisateur connecté et les utilisateurs suivis
    reviews_followed = Review.objects.filter(
        Q(user__in=users_followed_ids) | Q(ticket__user__in=users_followed_ids)
    ).select_related('user', 'ticket').annotate(content_type=Value('REVIEW', CharField()))

    # Récupérer les tickets de l'utilisateur connecté et des utilisateurs suivis
    tickets_user_and_followed = Ticket.objects.filter(
        Q(user=request.user) | Q(user__in=users_followed_ids)
    ).annotate(content_type=Value('TICKET', CharField()))

    # Combiner tous les objets (avis et tickets) et les trier par date de création
    posts = sorted(chain(reviews_followed, tickets_user_and_followed),
                   key=lambda post: post.time_created, reverse=True)

    # Récupérer les tickets déjà répondu par des avis
    tickets_already_answer = tickets_user_and_followed.filter(review__in=reviews_followed)

    return render(request, 'website/flux.html', context={'posts': posts, 'tickets_already_answer': tickets_already_answer})

@login_required
def create_ticket(request):
    """
    Permit to create a new ticket
    """
    ticket_form = forms.TicketForm()
    if request.method == "POST":
        ticket_form = forms.TicketForm(request.POST, request.FILES)
        if ticket_form.is_valid():
            ticket = ticket_form.save(commit=False)
            ticket.user = request.user
            ticket.save()
            return redirect('flux')
    context = {"ticket_form": ticket_form}
    return render(request, 'website/create_ticket.html', context=context)

@login_required
def create_review(request):
    """
    Permet de créer une nouvelle review avec un ticket associé
    """
    if request.method == "POST":
        ticket_form = forms.TicketForm(request.POST, request.FILES)
        review_form = forms.ReviewForm(request.POST)
        if ticket_form.is_valid() and review_form.is_valid():
            ticket = ticket_form.save(commit=False)
            ticket.user = request.user
            ticket.save()

            review = review_form.save(commit=False)
            review.user = request.user
            review.ticket = ticket
            review.save()

            return redirect('flux')
    else:
        ticket_form = forms.TicketForm()
        review_form = forms.ReviewForm()

    context = {"review_form": review_form, "ticket_form": ticket_form}
    return render(request, 'website/create_review.html', context=context)

@login_required
def create_review_from_ticket(request, ticket_id):
    """
    Permet de créer une review en réponse à un ticket
    """
    ticket = None
    review_form = forms.ReviewForm()

    try:
        ticket = Ticket.objects.get(pk=ticket_id)
        if request.method == "POST":
            review_form = forms.ReviewForm(request.POST)
            if review_form.is_valid():
                review = review_form.save(commit=False)
                review.user = request.user
                review.ticket = ticket
                review.save()
                return redirect('flux')
    except UnboundLocalError:
        return redirect('flux')
    except Ticket.DoesNotExist:
        return redirect('flux')

    return render(request, 'website/create_review_from_ticket.html', context={"ticket": ticket, "review_form": review_form})

@login_required
def display_posts(request):
    """
    Display all ticket et review from user connected
    """
    tickets = Ticket.objects.filter(user=request.user)
    tickets = tickets.annotate(content_type=Value('TICKET', CharField()))
    reviews = Review.objects.filter(user=request.user)
    reviews = reviews.annotate(content_type=Value('REVIEW', CharField()))
    posts = sorted(chain(reviews, tickets),
                   key=lambda post: post.time_created, reverse=True)
    return render(request, 'website/posts.html', context={"posts": posts})

@login_required
def follow_users(request):
    """
    Manage the subscription system
    """
    users_follow_you = [user_follow.user.username for user_follow in
                        UserFollows.objects.filter(followed_user=request.user.id)]

    users_followed = UserFollows.objects.filter(user_id=request.user)
    users_to_exclude = [user_followed.followed_user.username for user_followed in users_followed]
    users_to_exclude.append(request.user.username)
    users_to_follow = User.objects.exclude(username__in=users_to_exclude)
    if request.method == "POST":
        to_follow = User.objects.get(pk=request.POST["to_follow"])
        if to_follow in users_to_follow:
            UserFollows(user=request.user, followed_user=to_follow).save()
    users_followed = UserFollows.objects.filter(user_id=request.user)
    return render(request, 'website/follow_users.html', context={"users_followed": users_followed,
                                                                 "users_to_follow": users_to_follow,
                                                                 "users_follow_you": users_follow_you})

@login_required
def delete_follow_user(request, user_id):
    """
        Delete a follow user
    """
    try:
        user_follow = UserFollows.objects.get(user=request.user, followed_user=user_id)
    except UserFollows.DoesNotExist:
        return redirect('follow_users')
    if request.method == "POST":
        user_follow.delete()
        return redirect('follow_users')

@login_required
def delete_review(request, review_id):
    """
        Delete a review
    """
    try:
        review = Review.objects.get(pk=review_id)
        if review.user != request.user:
            return redirect('posts')
    except UserFollows.DoesNotExist:
        return redirect('posts')
    if request.method == "POST":
        review.delete()
        return redirect('posts')

@login_required
def update_review(request, review_id):
    """
        Modifie a review
    """
    try:
        review = Review.objects.get(pk=review_id)
        if review.user != request.user:
            return redirect('posts')
    except Review.DoesNotExist:
        return redirect('posts')

    review_form = forms.ReviewForm(instance=review)
    if request.method == "POST":
        update_form = forms.ReviewForm(request.POST)
        if update_form.is_valid():
            review_updated = update_form.save()
            return redirect('posts')
    context = {"review_form": review_form}
    return render(request, 'website/update_review.html', context=context)

@login_required
def delete_ticket(request, ticket_id):
    """
        Delete a review
    """
    try:
        ticket = Ticket.objects.get(pk=ticket_id)
        if ticket.user != request.user:
            return redirect('posts')
    except UserFollows.DoesNotExist:
        pass
    if request.method == "POST":
        ticket.delete()
        return redirect('posts')

@login_required
def update_ticket(request, ticket_id):
    """
        Modifie a ticket
    """
    try:
        ticket = Ticket.objects.get(pk=ticket_id)
        if ticket.user != request.user:
            return redirect('posts')
    except Ticket.DoesNotExist:
        return redirect('posts')
    ticket_form = forms.TicketForm(instance=ticket)
    if request.method == "POST":
        update_form = forms.TicketForm(request.POST, request.FILES)
        if update_form.is_valid():
            ticket_updated = update_form.save()
            return redirect('posts')
    context = {"ticket_form": ticket_form}
    return render(request, 'website/update_ticket.html', context=context)


# -*- utf-8 -*-
########################################
# PSF license aggrement for logmsg.py
# Developed by Ivan Rybko
# Logmsg
########################################

from singleton import Singleton
import logging

@Singleton
class Logmsg:
    def __init__(self, name):
        if isinstance(name, str):
            self.lname = name
            self.lpath = self.lname + ".log"

    def logmsg(self, message):
        # create file handler which logs even debug messages
        logger = logging.getLogger(self.lname)
        logger.setLevel(logging.DEBUG)
        # create file handler which logs even debug info messages
        loghandler = logging.FileHandler(self.lpath)
        loghandler.setLevel(logging.DEBUG)
        # add format
        frmt = logging.Formatter('{asctime} {name} {levelname:8s} {message}',style='{')
        loghandler.setFormatter(frmt)
        logger.addHandler(loghandler)
        # add message
        logger.debug(message)
        logging.info(message)

    def __call__(self, message):
        self.logmsg(message)

def uselog(*args):
    step1 = args[0]
    step2 = args[1]
    log = Logmsg(step1)
    log(step2)

logs = {
    "static":     Logmsg("staticserver"),
    "dynamic":    Logmsg("dynamicserver"),
    "httpserver": Logmsg("httpserver"),
    "smtpserver": Logmsg("smtpserver"),
    "wsgiserver": Logmsg("wsgiserver"),
    "wsgiapp":    Logmsg("wsgiapp"),
    "imapclient": Logmsg("imapclient"),
    "smtpclient": Logmsg("smtpclient"),
    "httpclient": Logmsg("httpclient"),
    "dbrequest":  Logmsg("dbrequest")
    }


#!/usr/bin/env python

'''
./converter.py \
	--model model.h5 \
	--desc "Predicts either a phone is in the hands or in a pocket" \
	--input_desc "Sensor samples (acc, gyro, mag, 50Hz)" \
	--output_desc "1 - phone in the hands, 0 - phone in a pocket" \
	--author "Danylo Kostyshyn" \
	--license="MIT"
'''

from __future__ import absolute_import
import os
import argparse
import coremltools
import numpy as np
from keras.models import load_model

parser = argparse.ArgumentParser(description='Converts Keras .h5 model to CoreML .mlmodel')
parser.add_argument('--model', dest='model_name', help='Input model name')
parser.add_argument('--desc', dest='desc', help='Short description')
parser.add_argument('--input_desc', dest='input_desc', help='Input description')
parser.add_argument('--output_desc', dest='output_desc', help='Oouput description')
parser.add_argument('--author', dest='author', help='Author')
parser.add_argument('--license', dest='license', help='License')

args = parser.parse_args()

def main():
	model_name = args.model_name
	keras_model = load_model(model_name)

	coreml_model = coremltools.converters.keras.convert(keras_model, \
		input_names='input', output_names='output')

	coreml_model.input_description['input'] = args.input_desc
	coreml_model.output_description['output'] = args.output_desc

	coreml_model.short_description = args.desc
	coreml_model.author = args.author
	coreml_model.license = args.license

	f_name, f_ext = os.path.splitext(model_name)
	coreml_model.save(os.path.join(f_name + '.mlmodel'))

	print("Success!")

if __name__ == '__main__':
	main()


import os
import numpy as np
import matplotlib.pyplot as plt

from config import *
from model import get_model
from data import get_single_test

def evaluate(test_num):
    model = get_model(train=TRAIN_MODEL)
    print("got model")
    test = get_single_test(test_num)
    print("got test")

    num_clips = test.shape[0] - CLIP_LEN
    clips = np.zeros((num_clips, *DIM, N_CHANNELS))

    # apply sliding window technique to get the clips
    for i in range(num_clips):
        clip = np.zeros((*DIM, N_CHANNELS))
        for j in range(CLIP_LEN):
            clip[j] = test[i+j, :, :, :]
        clips[i] = clip

    # get reconstruction cost of all the clips
    reconstructed_clips = model.predict(clips, batch_size=BATCH_SIZE)
    cost = np.array([
        np.linalg.norm( clips[i] - reconstructed_clips[i] )
        for i in range(num_clips)
    ])
    # arregularity score
    sa = (cost - np.min(cost)) / np.max(cost)
    # regularity score
    sr = 1.0 - sa

    # plot scores
    plt.plot(sr)
    plt.ylabel('regularity score Sr(t)')
    plt.xlabel('frame t')
    if not os.path.exists(FIG_PATH):
        os.makedirs(FIG_PATH)
    plt.savefig(f'{FIG_PATH}/Figure_{(test_num or TEST_NUM):03d}.png')
    # plt.show()
    plt.clf()

if __name__=="__main__":
    for i in range(4, 16):
        evaluate(i)


from typing import List, Union

from openai.types.chat import ChatCompletionMessageParam, ChatCompletionContentPartParam

from imported_code_prompts import (
    IMPORTED_CODE_BOOTSTRAP_SYSTEM_PROMPT,
    IMPORTED_CODE_IONIC_TAILWIND_SYSTEM_PROMPT,
    IMPORTED_CODE_REACT_TAILWIND_SYSTEM_PROMPT,
    IMPORTED_CODE_TAILWIND_SYSTEM_PROMPT,
    IMPORTED_CODE_SVG_SYSTEM_PROMPT,
)
from screenshot_system_prompts import (
    BOOTSTRAP_SYSTEM_PROMPT,
    IONIC_TAILWIND_SYSTEM_PROMPT,
    REACT_TAILWIND_SYSTEM_PROMPT,
    TAILWIND_SYSTEM_PROMPT,
    SVG_SYSTEM_PROMPT,
)

USER_PROMPT = """
Generate code for a web page that looks exactly like this.
"""

SVG_USER_PROMPT = """
Generate code for a SVG that looks exactly like this.
"""

def assemble_imported_code_prompt(
    code: str, stack: str, result_image_data_url: Union[str, None] = None
) -> List[ChatCompletionMessageParam]:
    system_content = IMPORTED_CODE_TAILWIND_SYSTEM_PROMPT
    if stack == "html_tailwind":
        system_content = IMPORTED_CODE_TAILWIND_SYSTEM_PROMPT
    elif stack == "react_tailwind":
        system_content = IMPORTED_CODE_REACT_TAILWIND_SYSTEM_PROMPT
    elif stack == "bootstrap":
        system_content = IMPORTED_CODE_BOOTSTRAP_SYSTEM_PROMPT
    elif stack == "ionic_tailwind":
        system_content = IMPORTED_CODE_IONIC_TAILWIND_SYSTEM_PROMPT
    elif stack == "svg":
        system_content = IMPORTED_CODE_SVG_SYSTEM_PROMPT
    else:
        raise Exception("Code config is not one of available options")

    user_content = (
        "Here is the code of the app: " + code
        if stack != "svg"
        else "Here is the code of the SVG: " + code
    )
    return [
        {
            "role": "system",
            "content": system_content,
        },
        {
            "role": "user",
            "content": user_content,
        },
    ]
    # TODO: Use result_image_data_url

def assemble_prompt(
    image_data_url: str,
    generated_code_config: str,
    result_image_data_url: Union[str, None] = None,
) -> List[ChatCompletionMessageParam]:
    # Set the system prompt based on the output settings
    system_content = TAILWIND_SYSTEM_PROMPT
    if generated_code_config == "html_tailwind":
        system_content = TAILWIND_SYSTEM_PROMPT
    elif generated_code_config == "react_tailwind":
        system_content = REACT_TAILWIND_SYSTEM_PROMPT
    elif generated_code_config == "bootstrap":
        system_content = BOOTSTRAP_SYSTEM_PROMPT
    elif generated_code_config == "ionic_tailwind":
        system_content = IONIC_TAILWIND_SYSTEM_PROMPT
    elif generated_code_config == "svg":
        system_content = SVG_SYSTEM_PROMPT
    else:
        raise Exception("Code config is not one of available options")

    user_prompt = USER_PROMPT if generated_code_config != "svg" else SVG_USER_PROMPT

    user_content: List[ChatCompletionContentPartParam] = [
        {
            "type": "image_url",
            "image_url": {"url": image_data_url, "detail": "high"},
        },
        {
            "type": "text",
            "text": user_prompt,
        },
    ]

    # Include the result image if it exists
    if result_image_data_url:
        user_content.insert(
            1,
            {
                "type": "image_url",
                "image_url": {"url": result_image_data_url, "detail": "high"},
            },
        )
    return [
        {
            "role": "system",
            "content": system_content,
        },
        {
            "role": "user",
            "content": user_content,
        },
    ]


#MORFOLOJİK OPERASYONLAR
import cv2
import matplotlib.pyplot as plt
import numpy as np

#
img = cv2.imread("img.png",0)

plt.figure(),plt.imshow(img , cmap = "gray"),plt.axis("off")

#EROZYON -ön plandaki nesnenin sınırlarını aşındırır
kernel = np.ones((3,3),dtype = np.uint8)
result = cv2.erode(img, kernel, iterations = 1)
plt.figure(),plt.imshow(result , cmap = "gray"),plt.axis("off")

#GENİŞLEME - ön plandaki nesnenin sınırlarını genişletir - erozyonun tam tersidir
result2 = cv2.dilate(img , kernel , iterations = 1)
plt.figure(),plt.imshow(result2 , cmap = "gray"),plt.axis("off")

#AÇMA - erozyon + genişleme - gürültünün giderilmesinde faydalıdır
whiteNoise = np.random.randint(0,2,size = img.shape[:2])
whiteNoise = whiteNoise * 255
plt.figure(),plt.imshow(whiteNoise , cmap = "gray"),plt.axis("off")

noise_img = whiteNoise + img
plt.figure(),plt.imshow(noise_img , cmap = "gray"),plt.axis("off")

#açılma
opening = cv2.morphologyEx(noise_img.astype(np.float32),cv2.MORPH_OPEN,kernel)
plt.figure(),plt.imshow(opening , cmap = "gray"),plt.axis("off"),plt.title("Açilma")

#KAPATMA - genişleme+erozyon - ön plandaki nesnelerin içindeki küçük delikleri veya nesne üzerindeki küçük siyah noktaları kapatmak için kullanışlıdır
blackNoise = np.random.randint(0,2,size = img.shape[:2])
blackNoise = whiteNoise  * -255
black_noise_img = img+blackNoise
black_noise_img[black_noise_img<=-245] =0
plt.figure(),plt.imshow(black_noise_img , cmap = "gray"),plt.axis("off"),plt.title("black noise")

closing = cv2.morphologyEx(black_noise_img.astype(np.float32),cv2.MORPH_CLOSE,kernel)
plt.figure(),plt.imshow(closing , cmap = "gray"),plt.axis("off"),plt.title("Kapanma")

#MORFOLOJİK GRADYAN - GENİŞLEME - EROZYON - GRADIENT
gradient = cv2.morphologyEx(img,cv2.MORPH_GRADIENT,kernel)
plt.figure(),plt.imshow(gradient , cmap = "gray"),plt.axis("off"),plt.title("GRADIENT")
plt.show()


from django.db import models
from mptt.models import MPTTModel, TreeForeignKey

from accounts.models import User

class Category(MPTTModel):
    name = models.CharField(max_length=100, verbose_name='Имя категории')
    slug = models.SlugField(max_length=100, verbose_name='Ссылка')
    parent = TreeForeignKey('self',
                            verbose_name='Родитель',
                            related_name='children',
                            on_delete=models.SET_NULL,
                            blank=True,
                            null=True
                            )

    class MPTTMeta:
        level_attr = 'mptt_level'
        order_insertion_by = ['name']

    class Meta:
        verbose_name = 'Категория'
        verbose_name_plural = 'Категории'

    def __str__(self):
        return self.name

class Ingredients(models.Model):
    name = models.CharField(max_length=100, verbose_name='Ингредиент')
    slug = models.SlugField(max_length=100, verbose_name='Ссылка')

    class Meta:
        verbose_name = 'Ингредиент'
        verbose_name_plural = 'Ингредиенты'

    def __str__(self) -> str:
        return self.name

class Dish(models.Model):
    name = models.CharField(max_length=50, verbose_name='Название блюда')
    slug = models.SlugField(max_length=50)
    ingredients = models.ForeignKey(Ingredients,
                                    related_name='ingredients',
                                    verbose_name='Ингредиенты',
                                    on_delete=models.CASCADE)

    class Meta:
        verbose_name = 'Блюда'
        verbose_name_plural = 'Блюда'

    def __str__(self) -> str:
        return self.name

class DeyWeek(models.Model):
    dey_name = models.CharField(max_length=50, verbose_name='День недели')

    class Meta:
        verbose_name = 'День'
        verbose_name_plural = 'Дни'

    def __str__(self) -> str:
        return self.dey_name

class DishWeek(models.Model):
    dish = models.ForeignKey(Dish,
                             related_name='dishweek',
                             verbose_name='Название блюда',
                             on_delete=models.CASCADE,
                             )
    slug = models.SlugField(max_length=50)
    ingredients = models.ForeignKey(Ingredients,
                                    related_name='ingredient',
                                    verbose_name='Ингредиенты',
                                    on_delete=models.CASCADE)
    dey_week = models.ForeignKey(DeyWeek,
                                    related_name='deyweek',
                                    verbose_name='Дни',
                                    on_delete=models.CASCADE)

    class Meta:
        verbose_name = 'Блюдо на неделю'
        verbose_name_plural = 'Блюда на неделю '

    def __str__(self) -> str:
        return f"Блюдо {self.dish}"

class Menu(models.Model):
    author = models.ForeignKey(
        User,
        verbose_name='Автор',
        related_name='Menu',
        on_delete=models.CASCADE)
    title = models.CharField(max_length=200, verbose_name='Заглавление')
    category = models.ForeignKey(Category,
                                 verbose_name='Категория',
                                 related_name='Menu',
                                 on_delete=models.SET_NULL,
                                 null=True)
    created_at = models.DateTimeField(
        auto_now_add=True, verbose_name='Дата создания')
    Ingredients = models.ForeignKey(
        Ingredients,
        verbose_name='Ингредиенты',
        related_name='Ингредиент',
        on_delete=models.CASCADE)
    breakfast = models.ForeignKey(
        Dish,
        verbose_name='Завтрак',
        related_name='Завтрак',
        on_delete=models.CASCADE)
    lunch = models.ForeignKey(
        Dish,
        verbose_name='Обед',
        related_name='Обед',
        on_delete=models.CASCADE)
    dinner = models.ForeignKey(
        Dish,
        verbose_name='Ужин',
        related_name='Ужин',
        on_delete=models.CASCADE)
    create_at = models.DateTimeField(
        null=True, blank=True, verbose_name='Дата создания')

    class Meta:
        verbose_name = 'Меню'
        verbose_name_plural = 'Меню'

    def __str__(self):
        return f"Владелец:{self.author} Завтрак: {self.breakfast} Обед: {self.lunch} Ужин:{self.dinner} Дата Создания записи {self.create_at}"

class Recipe(models.Model):
    name = models.CharField(max_length=100, verbose_name='Название')
    serves = models.CharField(max_length=50, verbose_name='Служит')
    prep_time = models.DurationField(
        default=0, verbose_name='Время подготовки')
    cook_time = models.DurationField(
        default=0, verbose_name='Время приготовления')
    ingredients = models.ForeignKey(
        Ingredients,
        verbose_name='Ингредиенты',
        related_name='Ингредиенты',
        on_delete=models.CASCADE)
    directions = models.TextField(verbose_name='Направление')
    menu = models.ForeignKey(Menu,
                             verbose_name='Меню',
                             related_name='recipe',
                             on_delete=models.SET_NULL,
                             null=True,
                             blank=True
                             )

    class Meta:
        verbose_name = 'Рецепт'
        verbose_name_plural = 'Рецепты'

    def __str__(self) -> str:
        return self.name

class Comment(models.Model):
    title = models.CharField(max_length=50, verbose_name='Заглавление')
    email = models.CharField(max_length=100, verbose_name='Email')
    message = models.TextField(max_length=500, verbose_name='Сообщение')
    menu = models.ForeignKey(Menu,
                             verbose_name='Меню',
                             related_name='comment',
                             on_delete=models.CASCADE,
                             )
    author = models.ForeignKey(User,
                               verbose_name='Пользователь',
                               on_delete=models.SET_NULL,
                               null=True)

    class Meta:
        verbose_name = 'Коментарий'
        verbose_name_plural = 'Коментарии'

    def __str__(self) -> str:
        return self.title


# set FLASK_APP=app
# set FLASK_ENV=development
# flask run

from flask import Flask, render_template, request
import semsim_funcs
import semtag_funcs
import semnull_funcs
import semcluster_funcs
import textCoder_funcs

app = Flask(__name__)

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

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

@app.route('/semsim_results', methods=['GET', 'POST'])
def semsim_results():
    target_words = ''.join(request.form['target_words'].lower().split()).split(',')
    contrast_word = request.form['contrast_word']
    pos_words = ''.join(request.form['pos_words'].lower().split()).split(',')
    neg_words = ''.join(request.form['neg_words'].lower().split()).split(',')
    relative_sims, target_words, pos_words, neg_words, error_message = semsim_funcs.get_sims(target_words, pos_words, neg_words, contrast_word=contrast_word)
    print(relative_sims)
    targets_with_sims = []
    for z in zip(target_words, relative_sims):
        this_str = ' '.join([z[0], str(round(z[1], 3))])
        targets_with_sims.append(this_str)
    return render_template('semsim_results.html', error_message=error_message, targets_with_sims=targets_with_sims, target_words=target_words, pos_words=pos_words, neg_words=neg_words, relative_sims=relative_sims, contrast_word=contrast_word)

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

@app.route('/semtag_results', methods=['GET', 'POST'])
def semtag_results():
    text = request.form['text']
    tags = request.form['tags']
    tags_in_vocab, output_all, output_sim_all, output_per_paragraph, output_sim_per_paragraph, output_paragraphs = semtag_funcs.get_semtags(text, tags)
    tags_in_vocab_str = ','.join(tags_in_vocab)
    output_per_paragraph_str = ','.join(output_per_paragraph)
    output_sim_per_paragraph_str = ','.join([str(round(a, 3)) for a in output_sim_per_paragraph])
    text = text.split('\n')
    output_tag_with_sim_per_paragraph = []
    for z in zip(output_per_paragraph, output_sim_per_paragraph):
        this_str = ' '.join([z[0], str(round(z[1], 3))])
        output_tag_with_sim_per_paragraph.append(this_str)
    output_tag_with_sim_all = []
    for z in zip(output_all, output_sim_all):
        this_str = ' '.join([z[0], str(round(z[1], 3))])
        output_tag_with_sim_all.append(this_str)
    nested_info_per_paragraph = []
    for z in zip(output_per_paragraph, output_sim_per_paragraph, output_paragraphs):
        this_row = [z[0], round(z[1], 3), z[2]]
        nested_info_per_paragraph.append(this_row)
    return render_template('semtag_results.html', nested_info_per_paragraph=nested_info_per_paragraph,output_tag_with_sim_per_paragraph=output_tag_with_sim_per_paragraph, output_tag_with_sim_all=output_tag_with_sim_all,tags_in_vocab_str=tags_in_vocab_str, text=text, output_all=output_all, output_sim_all=output_sim_all, output_per_paragraph_str=output_per_paragraph_str, output_sim_per_paragraph_str=output_sim_per_paragraph_str)

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

@app.route('/semnull_results', methods=['GET', 'POST'])
def semnull_results():
    target_words = ''.join(request.form['target_words'].lower().split()).split(',')
    contrast_word = request.form['contrast_word']
    pos_words = ''.join(request.form['pos_words'].lower().split()).split(',')
    neg_words = ''.join(request.form['neg_words'].lower().split()).split(',')
    scores_to_test = request.form['scores_to_test']
    template_sentence = request.form['template_sentence']
    template_pos = request.form['template_pos']
    print(target_words)
    print(contrast_word)
    null_distr_scores = semnull_funcs.get_semnull(pos_words, neg_words, template_sentence, template_pos)
    N_random_words_found = len(null_distr_scores)
    print(N_random_words_found)
    p_values, target_words_scores, target_words_output, scores_to_test_output, error_message = semnull_funcs.get_p(target_words, pos_words, neg_words, scores_to_test, null_distr_scores, contrast_word=contrast_word)
    p_values_nested = []
    target_words_output.extend(scores_to_test_output)
    print(p_values)
    for z in zip(target_words_output, target_words_scores, p_values):
        p_values_nested.append([z[0], round(z[1], 3), z[2]])
    print(p_values_nested)
    return render_template('semnull_results.html', error_message=error_message,null_distr_scores=null_distr_scores, N_random_words_found=N_random_words_found, p_values_nested=p_values_nested)

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

@app.route('/textCoder_results', methods=['GET', 'POST'])
def textCoder_results():
    text = request.form['text']
    print(text)
    knowledge_list = textCoder_funcs.get_textCoder(text)
    topics_str = ','.join([k[0] for k in knowledge_list])
    attributes_str = ', '.join([a[0] for k in knowledge_list for a in k[1]])
    text_in_paragraphs = text.split("\n")
    nested_info_per_paragraph = []
    nested_info_per_paragraph_count = []
    nested_info_per_paragraph_parindex = []
    topics_str_list = [k[0] for k in knowledge_list]
    attr_str_list = [', '.join([a[0] for a in k[1]]) for k in knowledge_list]
    for z in zip(topics_str_list, attr_str_list):
        nested_info_per_paragraph.append([z[0], z[1]])
    attr_str_count_list = [', '.join([a[0] + " (" + str(a[1]) + ")" for a in k[1]]) for k in knowledge_list]
    for z in zip(topics_str_list, attr_str_count_list):
        nested_info_per_paragraph_count.append([z[0], z[1]])
    attr_str_parindex_list = [', '.join([a[0] + " (" + str(a[2]) + ")" for a in k[1]]) for k in knowledge_list]
    for z in zip(topics_str_list, attr_str_parindex_list):
        nested_info_per_paragraph_parindex.append([z[0], z[1]])
    return render_template('textCoder_results.html', topics_str=topics_str, attributes_str=attributes_str, nested_info_per_paragraph=nested_info_per_paragraph, text_in_paragraphs=text_in_paragraphs,nested_info_per_paragraph_count=nested_info_per_paragraph_count,nested_info_per_paragraph_parindex=nested_info_per_paragraph_parindex)

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

@app.route('/semcluster_results', methods=['GET', 'POST'])
def semcluster_results():
    words = request.form['words']
    cluster_labels, cluster_items = semcluster_funcs.get_clusters(words)
    cluster_info = []
    for z in zip(cluster_labels, cluster_items):
        cluster_info.append(['Cluster label: ' + z[0], 'Items: ' + ', '.join(z[1])])
    return render_template('semcluster_results.html', words=words, cluster_info=cluster_info)


import itertools
import math
from collections import defaultdict

import brownie
import pytest

from scripts.utils import pack_values

ZERO_ADDRESS = "0x0000000000000000000000000000000000000000"

@pytest.fixture(scope="module", autouse=True)
def registry(
    Registry,
    provider,
    gauge_controller,
    alice,
    lending_swap,
    lp_token,
    n_coins,
    is_v1,
    rate_method_id,
    underlying_decimals,
    wrapped_decimals,
    chain,
):
    registry = Registry.deploy(provider, gauge_controller, {"from": alice})
    registry.add_pool(
        lending_swap,
        n_coins,
        lp_token,
        rate_method_id,
        pack_values(wrapped_decimals),
        pack_values(underlying_decimals),
        hasattr(lending_swap, "initial_A"),
        is_v1,
        "",
        {"from": alice},
    )
    chain.sleep(10)
    registry.remove_pool(lending_swap, {"from": alice})
    yield registry

@pytest.mark.itercoins("send", "recv")
def test_find_pool(registry, wrapped_coins, underlying_coins, send, recv):
    assert registry.find_pool_for_coins(wrapped_coins[send], wrapped_coins[recv]) == ZERO_ADDRESS
    assert (
        registry.find_pool_for_coins(underlying_coins[send], underlying_coins[recv]) == ZERO_ADDRESS
    )

def test_get_n_coins(registry, lending_swap):
    assert registry.get_n_coins(lending_swap) == [0, 0]

def test_get_coins(registry, lending_swap):
    assert registry.get_coins(lending_swap) == [ZERO_ADDRESS] * 8
    assert registry.get_underlying_coins(lending_swap) == [ZERO_ADDRESS] * 8

def test_get_decimals(registry, lending_swap):
    assert registry.get_decimals(lending_swap) == [0] * 8
    assert registry.get_underlying_decimals(lending_swap) == [0] * 8

def test_get_rates(registry, lending_swap):
    assert registry.get_rates(lending_swap) == [0] * 8

@pytest.mark.itercoins("send", "recv")
def test_get_coin_indices(registry, lending_swap, underlying_coins, wrapped_coins, send, recv):
    with brownie.reverts("No available market"):
        registry.get_coin_indices(lending_swap, wrapped_coins[send], wrapped_coins[recv])
    with brownie.reverts("No available market"):
        registry.get_coin_indices(lending_swap, underlying_coins[send], underlying_coins[recv])

@pytest.mark.once
def test_get_balances(registry, lending_swap):
    with brownie.reverts():
        registry.get_balances(lending_swap)

@pytest.mark.once
def test_get_underlying_balances(registry, lending_swap):
    with brownie.reverts():
        registry.get_underlying_balances(lending_swap)

@pytest.mark.once
def test_get_admin_balances(registry, lending_swap):
    with brownie.reverts():
        registry.get_admin_balances(lending_swap)

@pytest.mark.once
def test_get_virtual_price_from_lp_token(alice, registry, lp_token):
    with brownie.reverts():
        registry.get_virtual_price_from_lp_token(lp_token)

@pytest.mark.once
def test_get_pool_from_lp_token(registry, lp_token):
    assert registry.get_pool_from_lp_token(lp_token) == ZERO_ADDRESS

@pytest.mark.once
def test_get_lp_token(registry, lending_swap):
    assert registry.get_lp_token(lending_swap) == ZERO_ADDRESS

def test_coin_count_is_correct(registry):

    assert registry.coin_count() == 0

def test_get_all_swappable_coins(registry, wrapped_coins, underlying_coins):
    coin_set = set(map(str, itertools.chain(wrapped_coins, underlying_coins)))
    coin_count = len(coin_set)

    coins = set(registry.get_coin(i) for i in range(coin_count))

    assert coins == {ZERO_ADDRESS}

@pytest.mark.once
def test_last_updated_getter(registry, history):
    registry_txs = history.filter(receiver=registry.address)
    assert math.isclose(registry_txs[-1].timestamp, registry.last_updated())

def test_coin_swap_count(registry, wrapped_coins, underlying_coins):
    coins = set(map(str, itertools.chain(wrapped_coins, underlying_coins)))

    for coin in coins:
        assert registry.get_coin_swap_count(coin) == 0

def test_swap_coin_for(registry, wrapped_coins, underlying_coins):
    wrapped_coins = list(map(str, wrapped_coins))
    underlying_coins = list(map(str, underlying_coins))
    pairings = defaultdict(set)

    wrapped_pairs = itertools.combinations(wrapped_coins, 2)
    underlying_pairs = itertools.combinations(underlying_coins, 2)

    for coin_a, coin_b in itertools.chain(wrapped_pairs, underlying_pairs):
        pairings[coin_a].add(coin_b)
        pairings[coin_b].add(coin_a)

    for coin in pairings.keys():
        coin_swap_count = len(pairings[coin])
        available_swaps = {
            registry.get_coin_swap_complement(coin, i) for i in range(coin_swap_count)
        }

        assert available_swaps == {ZERO_ADDRESS}


import os,sys,re
import slide_window
import vcf_operating
import read_group
import read_dict
from multiprocessing import Pool
#https://github.com/mfumagalli/ngsPopGen/blob/9ee3a6d5e733c1e248e81bfc21514b0527da967b/scripts/getDxy.pl
def help():
    print("python3 xx.py vcf two_group_list fasta_fai outfile window step")
    print("two_group_list: classA\tHH1\nclassB\tHH2\nclassB\tHH3")
    sys.exit()
def maf_1_0(list_):
    count_0=sum([x.count('0') for x in list_])
    count_1=sum([x.count('1') for x in list_])
    all_count=count_0+count_1
    if all_count==0:
        return [0,0]
    maf_1=count_1/all_count
    maf_0=count_0/all_count
    return [maf_0,maf_1]

def each_maf(line):
    types1=[line[x][:3] for x in loci1]
    types2=[line[x][:3] for x in loci2]
    all_maf=maf_1_0(types1)+maf_1_0(types2)
    return all_maf

def each_dxy(line_):
    line=line_.split()
    chr_,pos=line[0],int(line[1])
    all_maf=each_maf(line)
    dxy=all_maf[0]*all_maf[3]+all_maf[1]*all_maf[2]
    return [chr_,pos,dxy]

def every_dxy(vcfs):
    head=next(vcfs).split()
    global loci1,loci2
    loci1=[head.index(x) for x in group_dict[group1]]
    loci2=[head.index(x) for x in group_dict[group2]]
    po1=Pool(theads)
    all_dxy=po1.map(each_dxy,vcfs)
    po1.close()
    dxy_dict={}
    for list_ in all_dxy:
        if list_[2]==0:
            continue
        dxy_dict.setdefault(list_[0],{}).setdefault(list_[1],list_[2])
    return dxy_dict

def window_dxy(cal_window):
    chr_=cal_window[0]
    if chr_ not in dxy_snp_pos_dict:
        return
    chr_snp_pos=dxy_snp_pos_dict[chr_]
    N_SNP,ALL_DXY=0,0
    for pos in chr_snp_pos:
        if pos<cal_window[1]:
            continue
        elif cal_window[1]<=pos<=cal_window[2]:
            N_SNP+=1
            ALL_DXY+=dxy_dict[chr_][pos]
        elif pos >cal_window[2]:
            break
    if ALL_DXY==0 or N_SNP==0:
        return
    ALL_DXY=ALL_DXY/(cal_window[2]-cal_window[1]+1)
    return [cal_window[0],str(cal_window[1]),str(cal_window[2]),str(N_SNP),str(ALL_DXY)]

def write_result(all_window_dxy,out_file):
    all_window_dxy.sort(key=lambda x:(x[0],int(x[1])))
    with open(out_file,'w') as fp:
        print('CHROM\tSTART\tEND\tN_SNP\tDXY',file=fp)
        for win in all_window_dxy:
            print('\t'.join(win),file=fp)

def main(vcf_file,group_file,fasta_fai,out_file,window,step):
    global group_dict,group1,group2
    group_dict=read_group.Read_group1(group_file)
    [group1,group2]=list(group_dict.keys())
    vcfs=For_vcf.vcf_read2(vcf_file)
    global dxy_dict,dxy_snp_pos_dict
    dxy_dict=every_dxy(vcfs)
    dxy_snp_pos_dict={}
    for chr_ in dxy_dict:
        dxy_snp_pos_dict[chr_]=sorted(dxy_dict[chr_].keys())
    chr_len_dict=read_dict.read_fai(fasta_fai)
    all_windows=[]
    #[[chr,start,end]]
    for chr_ in chr_len_dict:
        all_windows+=slide_window.slide_window2(chr_,window,step,1,chr_len_dict[chr_])
    po2=Pool(theads)
    all_window_dxy=po2.map(window_dxy,all_windows)
    po2.close()
    #all_window_dxy=list(filter(None,all_window_dxy))
    all_window_dxy=[x for x in all_window_dxy if x !=None]
    write_result(all_window_dxy,out_file)

if __name__=="__main__":
    try:
        [vcf_file,group_file,fasta_fai,out_file]=sys.argv[1:5]
        window=int(sys.argv[5])
        step=int(sys.argv[6])
    except:
        help()
    theads=30
    For_vcf=vcf_operating.for_vcf()
    main(vcf_file,group_file,fasta_fai,out_file,window,step)


import maya.cmds as cmds
from functools import partial

def rename_sl(*args):
    new_name = cmds.textField('Rename', q = True, text =True)
    items = cmds.ls(sl=True)
    for item in items:
        cmds.rename(item , new_name)
def replace_sl(*args):
    name_to_replace = cmds.textField('RePlace_before', q = True, text =True)
    replace_name = cmds.textField('RePlace_after', q = True, text =True)
    items = cmds.ls(sl=True)
    for item in items:
        new_name = item.replace(name_to_replace, replace_name)
        cmds.rename(item , new_name)

def rename_sl_ui():
    '''
    main rondom copy function
    '''
    cmds.window()
    cmds.columnLayout()
    cmds.rowLayout(nc=3)
    cmds.text(label='input')
    rename_input = cmds.textField('Rename', text='new_name', ed=True)
    cmds.button(c=rename_sl, label='Rename')
    cmds.setParent('..')
    cmds.rowLayout(nc=5)
    cmds.text(label='before')
    cmds.textField('RePlace_before', text='name', ed=True)
    cmds.text(label='after')
    cmds.textField('RePlace_after', text='new_name', ed=True)
    cmds.button(c=replace_sl, label='Replace')
    cmds.setParent('..')
    #name_button = cmds.textFieldButtonGrp('Rename',label='input', text='name',ed = True, buttonLabel='Rename', bc=rename_sl)
    #name_button = cmds.textFieldButtonGrp('Replace',label='input', text='name',ed = True, buttonLabel='Replace', bc=rename_sl)
    cmds.showWindow()

rename_sl_ui()


import time
start_time = time.time()

from global_var import *
from demandSimulate import *
from oneStepOptimization_poly import *
from sklearn import preprocessing
from sklearn.linear_model import LinearRegression

from scipy.stats import qmc

def microgrid_poly2d():

    # stock
    alpha = 0.5; K0 = 0; sigma = 2; processType = "Regular";

    # backward simulation

    sampler = qmc.Sobol(d=2, scramble=False)
    # 512 samples
    W = sampler.random_base2(m=9)
    X0 = -10 + 20*W[:,0]; # X0 in [-10,10]
    ICord = 10*W[:,1];    # Ic in[0,Imax = 10]
    ICord_rep = np.repeat(ICord,batch);
    X0_rep = np.repeat(X0,batch);

    demandMatrix = demandSimulate(alpha, K0, sigma, 1, nsim, dt, X0_rep);
    # I0 is 5
    finalCost = -200 * ((ICord_rep-5))*(ICord_rep<5)
    # 20 rows, 512 columns,create 20x512 matrix, filled by column wise with order = "F" (default fill row),
    # take row sum mean  to get final cost for each starting site
    finalCost = np.mean(finalCost.reshape((batch,sites),order = "F"),axis = 0)

    costNext = np.zeros((sites,no_regime));

    for r in range(no_regime):
        costNext[:,r] = finalCost;

    modelIndx = 0; Model = [None] * (nstep)
    polyMdl = [None]*no_regime
    for iStep in range(nstep,0,-1):
        for r in range(no_regime):
            polyMdl[r] = LinearRegression()

            X_train = np.column_stack((X0, ICord))

            poly = preprocessing.PolynomialFeatures(degree=3)
            X_poly = poly.fit_transform(X_train)

            y_train = costNext[:, r]
            polyMdl[r].fit(X_poly, y_train)
        print(iStep)
        print(costNext[:10,0])
        print("\n")
        # generate X_t
        demandMatrix = demandSimulate(alpha, K0, sigma, 1, nsim, dt, X0_rep);

        # optimize basedd on X_t-1,I_t
        cost, _, control, nextInventory, nextRegime, imbalance, batteryOutput =oneStepOptimization_microgrid(
            demandMatrix[:, 1], ICord_rep, polyMdl)

        for r in range(no_regime):
            costNext[:, r] = np.mean(cost[:, r].reshape((batch, sites),order = "F"), axis=0)

        Model[modelIndx] = polyMdl.copy();
        modelIndx+=1;

    # forward simulations
    np.random.seed(10)
    simOutSample = 2000;

    X0 = np.zeros(simOutSample);
    I0 = 5;
    demandMatrix = demandSimulate(alpha, K0, sigma, nstep, simOutSample, maturity, X0);

    inventoryForward = np.zeros((simOutSample,nstep+1));
    inventoryForward[:, 0] = I0

    regimeForward = np.zeros((simOutSample, nstep + 1))
    regimeForward[:, 0] = 0

    Bt = np.zeros((simOutSample, nstep))
    St = np.zeros((simOutSample, nstep))
    dieselPower = np.zeros((simOutSample, nstep))
    trueCost = np.zeros((simOutSample, nstep))
    artificialCost = np.zeros((simOutSample, nstep))

    costForward = np.zeros((simOutSample, nstep + 1))

    for iStep in range(nstep):
        _, _, control, nextInventory, nextRegime, imbalance, batteryOutput = \
        oneStepOptimization_microgrid(demandMatrix[:, iStep], inventoryForward[:, iStep], Model[nstep - iStep-1])
        row_idx = tuple(np.arange(0,simOutSample))
        col_idx = tuple(np.int64(regimeForward[:,iStep]))
        inventoryForward[:, iStep + 1] = nextInventory[row_idx,col_idx]
        regimeForward[:, iStep + 1] = nextRegime[row_idx,col_idx]
        Bt[:, iStep] = batteryOutput[row_idx,col_idx]
        St[:, iStep] = imbalance[row_idx,col_idx]
        dieselPower[:, iStep] = control[row_idx,col_idx]

        trueCost[:, iStep] = k[0] * (dieselPower[:, iStep] ** 0.9) * dt + switchCost * (regimeForward[:, iStep + 1] > regimeForward[:, iStep])
        artificialCost[:, iStep] = k[1] * St[:, iStep] * (St[:, iStep] > 0) * dt - k[2] * St[:, iStep] * (St[:, iStep] < 0) * dt

    penalty = -200 * (inventoryForward[:, -1] - I0) * (inventoryForward[:, -1] < I0)

    costForward = trueCost + artificialCost
    pathWiseCost = np.sum(costForward, axis=1) + penalty
    totalCost = np.mean(pathWiseCost)
    stdDeviation = np.std(pathWiseCost) / np.sqrt(simOutSample)
    totalTrueCost = np.mean(np.sum(trueCost, axis=1))
    totalArtificialCost = np.mean(np.sum(artificialCost, axis=1))

    print(f'nsim: {sites} x {batch}, regime=1, totalCost= {totalCost}, totalTrueCost= {totalTrueCost}, totalArtificialCost= {totalArtificialCost}')

if __name__ == "__main__":
    microgrid_poly2d()
    print(f'Running time: {time.time()- start_time} s')


"""
    NOMBRE: Maria del Carmen Hernandez Diaz
    ACCOUNT: 1718110389
    GROUP: TIC 51
    DATE: 30-05-2020
    DESCRIPTION: Creación de cookies con nombre, número de visitas, fecha, y hora del visitante.
    En caso de que no haya nombre como respuesta del usuario se marcara como 'Anónimo'.
"""

import web # Librería de web.py
import datetime # Librería para manipulación de fecha y tiempo.

class Visitas:
    def GET(self, name):
      try:
        # Administración Cookies
        cookie = web.cookies()
        visitas = "0"
        print(cookie)

        # Creación de variables para manejo accesible del tiempo.
        date = datetime.datetime.now()
        dateV = date.strftime('%a, %d-%m-%Y') # Estructura. (Dia de la semana, dia en numero-mes-año)
        hourV = date.strftime('%H:%M:%S') # Estructura. (Hora:Mes:Segundos)

        # Date - Visitor.
        if dateV:
          web.setcookie("dateV", dateV, expires="", domain=None)
        else:
          web.setcookie("dateV", dateV, expires ="", domain=None)

        # Hour - Visitor.
        if hourV:
          web.setcookie("hourV", hourV, expires="", domain=None)
        else:
          web.setcookie("hourV", hourV, expires ="", domain=None)

        # Name - Visitor.
        if name:
          web.setcookie("nombre", name, expires="", domain=None)
        else:
          name="Anónimo" # Asignacion en caso de que no haya un nombre introducido por un usuario.
          web.setcookie("nombre", name, expires ="", domain=None)

        # No. Visitas
        if cookie.get("visitas"):
          visitas = int(cookie.get("visitas"))
          visitas += 1 # Contador de visitas
          web.setcookie("visitas", str(visitas), expires ="", domain=None)

        else:
          web.setcookie("visitas", str(1), expires ="", domain=None)
          visitas = "1"
          # Retorno de variables en cadena (str), con saltos de linea para mejor visualizacion.
        return "Bienvenido." + "\n" + "\n" + "Nombre: " + str(name) + "\n" + "No. Visitas: " + str(visitas) + "\n" + "Fecha visita: " + str(dateV) + "\n" + "Hora visita: " + str(hourV)

      except Exception as e:
        return "Error" + str(e.args) # En caso de error.


import os
import torch
import numpy as np
from torch.utils.data import Dataset
import h5py

class DataSet(Dataset):
    def __init__(self, transform, split='train'):
        self.image_list = []
        self.transform = transform
        file_list_path = os.path.join('dataset', split + '.list')
        with open(file_list_path, 'r') as f:
            for line in f:
                self.image_list.append(os.path.join('dataset', os.path.join(line.strip(), 'mri_norm2.h5')))

    def __len__(self):
        return len(self.image_list)

    def __getitem__(self, index):
        content = h5py.File(self.image_list[index], 'r')
        image = content['image'][:]
        label = content['label'][:]
        sample = {'image': image, 'label': label}
        if self.transform:
            sample = self.transform(sample)
        return sample

class RandomCrop(object):
    def __init__(self, output_size):
        self.output_size = output_size

    def __call__(self, sample):
        image, label = sample['image'], sample['label']
        w, h, d = image.shape
        if w <= self.output_size[0] or h <= self.output_size[1] or d <= self.output_size[2]:
            pw = (self.output_size[0] - w) // 2 + 3
            ph = (self.output_size[1] - h) // 2 + 3
            pd = (self.output_size[2] - d) // 2 + 3
            image = np.pad(image, ((pw, pw), (ph, ph), (pd, pd)), 'constant', constant_values=0)
            label = np.pad(label, ((pw, pw), (ph, ph), (pd, pd)), 'constant', constant_values=0)
        w, h, d = image.shape
        w1 = np.random.randint(0, w - self.output_size[0])
        h1 = np.random.randint(0, h - self.output_size[1])
        d1 = np.random.randint(0, d - self.output_size[2])
        image = image[w1:w1 + self.output_size[0], h1:h1 + self.output_size[1], d1:d1 + self.output_size[2]]
        label = label[w1:w1 + self.output_size[0], h1:h1 + self.output_size[1], d1:d1 + self.output_size[2]]
        return {'image': image, 'label': label}

class RandomRotFlip(object):
    def __call__(self, sample):
        image, label = sample['image'], sample['label']
        k = np.random.randint(0, 4)
        image = np.rot90(image, k)
        label = np.rot90(label, k)
        axis = np.random.randint(0, 2)
        image = np.flip(image, axis).copy()
        label = np.flip(label, axis).copy()
        return {'image': image, 'label': label}

class ToTensor(object):
    def __call__(self, sample):
        image = sample['image']
        image = image.reshape(1, image.shape[0], image.shape[1], image.shape[2]).astype(np.float32)
        image = torch.from_numpy(image)
        label = torch.from_numpy(sample['label']).long()
        return {'image': image, 'label': label}


#coding=utf-8
import json
import queue
import random
import re
import time
import traceback
import urllib
import urllib.request
import uuid

import requests
import wipe_off_html_tag
from bs4 import BeautifulSoup

import data_storager

class ParentChild(object):
    def __init__(self):
        self._parent_id = None
        self._parent_title = None
        self._parent_url = None
        self._parent_layer = None
        self._child_id = None
        self._child_title = None
        self._child_url = None
        self._child_layer = None

class Spider(object):
    """
    layer：第一层，首页， 科学百科
           第二层，航空航天
           第三层，3·8马来西亚航班失踪事件
           第四层，马来西亚航空公司
           第h层
    """
    def __init__(self):
        self._post_lemmas_url = 'https://baike.baidu.com/wikitag/api/getlemmas'
        self._get_zhixin_url = 'https://baike.baidu.com/wikiui/api/zhixinmap?lemmaId='
        self._get_guess_like_url = 'https://baike.baidu.com/api/wikiui/guesslike?url='
        self._parent_childs = queue.Queue()  # relationship队列
        self._crawl_layers = 4-1  # 需要爬取的最后一层的上一层

    def execute(self):
        # 录入首页，科学百科
        root_url = 'https://baike.baidu.com/science'
        root_title = r'科学百科'
        root_id = str(uuid.uuid3(uuid.NAMESPACE_DNS, str(root_url)))
        # data_storager.insert_webpage(root_id, root_title, root_url, None)
        # print("wxm entity:", root_title, root_url)

        # 录入首页下二级页面
        first_page_html = self.get_firstpage_content("https://baike.baidu.com/science")
        first_page_urls = self.get_firstpage_url(first_page_html)  # 10条二级页面信息
        tag_ids = self.get_firstpage_tagid(first_page_urls)  # 9条二级页面信息
        first_page_urls_slice = first_page_urls[1:2]  # 获取前半部分URL
        tag_ids_slice = tag_ids[1:2]  # 获取前半部分Tag

        for page_id in range(0, 61):
            try:
                for i in range(0, len(tag_ids_slice)):
                    # 处理每一个二级页面，如航空航天，天文学，环境生态
                    print("page_id:" + str(page_id) + ", tagids：" + str(tag_ids_slice[i]))
                    time.sleep(random.choice(range(1, 3)))
                    title = first_page_urls_slice[i].get_text()
                    url = first_page_urls_slice[i].get('href')
                    id = str(uuid.uuid3(uuid.NAMESPACE_DNS, str(url)))  # 为当前网页生成UUID
                    data_storager.insert_relationship(root_id, root_title, id, title)  # 插入首页与二级页面的relationship，如<科学百科_id, 航空航天_id>
                    data_storager.insert_webpage(id, title, url, None)  # 插入二级页面webpage信息,页面内容为空，如航空航天
                    print("wxm entity:",title, url)
                    # 录入所有的<二级页面，三级页面>配对信息到缓存中,如航空航天下面的当前page_id下的所有条目,如马航,911事件
                    self.get_second_page_url(id, title, url, tag_ids_slice[i], page_id)
                while (self._parent_childs):  # 从第三层（马航,911事件，位于缓存对象的child_xx中）开始，处理队列每一个页面
                    pc = self._parent_childs.get(0)
                    self.deal_with_child_webpage(pc)  # 用layer控制爬虫爬取深度，调用deal_with_child_webpage方法即插入数据库
            except:
                print(traceback.format_exc())
                print("wxmException，page_id：" + str(page_id))
            print("wxmmmmmmmmmmmmmm， tag_ids_slice:" + str(tag_ids_slice[i]))
        print("wxmmmmmmmmmmmmmm， page_id：" + str(page_id))

    def deal_with_child_webpage(self, pc):
        """ 处理当前缓存队列中的对象中的child对象，该方法始终以child为中心处理，parent在前面处理了；从第三层开始的 """
        try:
            htmL_content = self.get_sub_page_content(pc._child_url)  # 获取当前孩子页面的html内容
            if htmL_content == None:
                return
            real_content = self.get_web_real_content(htmL_content) # 获取网页的真正需要入库的content
            if len(real_content)>100 and real_content!=None:
                real_content=real_content[:100]
            data_storager.insert_relationship(pc._parent_id, pc._parent_title, pc._child_id, pc._child_title)  # 插入二级页面与三级页面的relationship，如<航空航天_id, 马航_id>
            data_storager.insert_webpage(pc._child_id, pc._child_title, pc._child_url, real_content)  # 插入当前孩子页面信息，如马航 todo, 解析出htmlcontent中汉字
            print("wxm entity:",pc._child_title, pc._child_url)
            self.batch_insert_webpage_attributes(pc._child_id, pc._child_title, htmL_content)  # 插入当前页面的所有属性信息，如马航下的中文名 3·8马来西亚航班失踪事件
            self._crawl_layers = random.choice(range(3, 7))
            if pc._child_layer <= self._crawl_layers:  # 3<=4,仅从第三层爬到第四层
                self.get_sub_urls(pc._child_id, pc._child_title, pc._child_url, pc._child_layer, htmL_content)  # 获取当前网页下所有连接，传入第三层的id，title，url，获取第四层信息，加入到缓存队列中
        except:
            print(traceback.format_exc())
            print("wxmException, ", pc._child_id, pc._child_title, pc._child_url)

    def batch_insert_webpage_attributes(self, id, title, htmL_content):
        soup_findattribute = BeautifulSoup(htmL_content, 'html.parser')
        data_findattribute = soup_findattribute.find_all('div', {'class': 'basic-info cmn-clearfix'})
        for attributes in data_findattribute:
            attribute_names = attributes.find_all('dt', {'class': 'basicInfo-item name'})
            names_list = []
            for a in attribute_names:
                attribute_name = a.get_text()
                names_list.append(attribute_name)
            attribute_values = attributes.find_all('dd', {'class': 'basicInfo-item value'})
            values_list = []
            for b in attribute_values:
                attributes_value = b.get_text()
                values_list.append(attributes_value)
            nvs = zip(names_list, values_list)
            nvDict = dict((str(names_list), str(values_list)) for names_list, values_list in nvs)
            for names_lists, values_lists in nvDict.items():
                names_lists_insert = names_lists
                value_lists_insert = values_lists.strip("\n")
                data_storager.insert_attributes(id, title, names_lists_insert, value_lists_insert)

    def get_web_real_content(self, htmL_content):
        soup = BeautifulSoup(htmL_content, 'html.parser')
        main_content = soup.find('div', {'class': 'main-content'})
        return wipe_off_html_tag.filter_tags(str(main_content)).strip(r'收藏\n查看我的收藏\n').\
            strip(r'有用+1\n已投票\n').strip(r'编辑\n锁定\n').strip(r'审核\n。')

    def get_sub_urls(self, id, title, url, layer, content):
        soup = BeautifulSoup(content, 'html.parser')
        body = soup.body  # 获取body部分

        # 爬取para中连接词条
        try:
            link_list1 = {}
            link_body1 = body.find_all('div', {'class': 'para', 'label-module': "para"})
            for link_body in link_body1:
                result_list = link_body.find_all('a', {'target': '_blank'})
                if result_list:
                    for link in result_list:
                        fourth_page_url_postfix = link.get('href')
                        pattern = re.compile(r'/pic')
                        match = pattern.match(fourth_page_url_postfix)
                        if not match:
                            sub_title = link.get_text()
                            sub_url = "http://baike.baidu.com" + fourth_page_url_postfix
                            link_list1[sub_title] = sub_url
        except:
            print(traceback.format_exc())
            print("wxmException, 爬取para中连接词条 todo print 出错更详细信息:" + id + ", " + title + ", " + url)

        # 爬取相关信息词条
        try:
            link_list2 = {}
            link_body2 = body.find('div', {'class': 'zhixin-box'})
            newlemmaid = link_body2['data-newlemmaid']
            headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36'
            }
            data = {"lemmaId": newlemmaid}
            request_url = self._get_zhixin_url + str(newlemmaid)
            content = requests.get(request_url, params=data, headers=headers).content
            response = content.decode('unicode-escape').replace("\/", "/")
            js = json.loads(response)
            for i in range(len(js)):
                title_urls = js[i]['data']
                for j in range(len(title_urls)):
                    sub_title = js[i]['data'][j]['title']
                    sub_url = js[i]['data'][j]['url']
                    link_list2[sub_title] = sub_url
        except:
            print(traceback.format_exc())
            print("wxmException, 爬取相关信息词条 todo print 出错更详细信息:" + id + ", " + title + ", " + url)

        # 爬取猜你喜欢词条
        try:
            link_list3 = {}
            headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36'
            }
            data = {
                "url": sub_url,
                "lemmaTitle": sub_title,
                "eid": 202
            }
            request_url = self._get_guess_like_url + str(sub_url) + "&lemmaTitle=" + sub_title + "&eid=202"
            content = requests.get(request_url, params=data, headers=headers).content
            try:
                jso1 = json.loads(content)
                ad_place_data = jso1["ad_place_list"][0]["ad_place_data"]
                ad_place_data2 = ad_place_data.replace("\/", "/")
                jso2 = json.loads(ad_place_data2)
                ads = jso2["ads"]
                for ad in ads:
                    sub_title = ad['title']
                    sub_url = ad['url']
                    link_list3[sub_title] = sub_url
            except:
                print(traceback.format_exc())
                print("wxmException, 爬取猜你喜欢词条 json 解析失败")
        except:
            print(traceback.format_exc())
            print("wxmException, 爬取猜你喜欢词条 todo print 出错更详细信息:" + id + ", " + title + ", " + url)
        try:
            dictMerged2 = {}
            dictMerged1 = dict(link_list1, **link_list2)
            dictMerged2 = dict(dictMerged1, **link_list3)
        except:
            print(traceback.format_exc())
            print("wxmException, 合并dict失败")

        for sub_title, sub_url in dictMerged2.items():
            pc = ParentChild()
            pc._parent_id = id
            pc._parent_title = title
            pc._parent_url = url
            pc._parent_layer = layer
            child_id = str(uuid.uuid3(uuid.NAMESPACE_DNS, str(sub_url)))  # 为当前孩子页面生成UUID
            pc._child_id = child_id
            pc._child_title = sub_title
            pc._child_url = sub_url
            pc._child_layer = pc._parent_layer + 1
            self._parent_childs.put(pc)

    def get_sub_page_content(self, url3, data=None):
        header = {
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Accept-Encoding': 'gzip,deflate,sdch',
            'Accept-Language': 'zh-CN,zh;q=0.8',
            'Connection': 'keep-alive',
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36'
        }
        timeout = 180
        try:
            result2 = requests.get(url3, headers=header, timeout=timeout)
            result2.encoding = 'utf-8'
            return result2.text
        except urllib.request.HTTPError as e:
            print(traceback.format_exc())
            print('wxmException,  1:', e.code)
        except urllib.request.URLError as e:
            print(traceback.format_exc())
            print('wxmException,  2:', e.reason)
        except:
            print(traceback.format_exc())
            print('wxmException,  0')
        return None

    def get_second_page_url(self, id, title, url, tagId, page):
        """
            父亲id是传入的，孩子id是生成的
        """
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36'
        }
        data = {
            "limit": "24",
            "timeout": 3000,
            "filterTags": [],
            "tagId": tagId,
            "fromLemma": False,
            "contentLength": 40,
            "page": page
        }
        try:
            content = requests.post(self._post_lemmas_url, data=data, headers=headers).content
            response = content.decode('unicode-escape').replace("\/", "/")
            js = json.loads(response)
            for i in range(len(js['lemmaList'])):
                sub_title = (js['lemmaList'][i]['lemmaTitle'])
                sub_url = (js['lemmaList'][i]['lemmaUrl'])
                pc = ParentChild()
                pc._parent_id = id
                pc._parent_title = title
                pc._parent_url = url
                pc._parent_layer = 2
                child_id = str(uuid.uuid3(uuid.NAMESPACE_DNS, str(sub_url)))  # 为当前孩子页面生成UUID
                pc._child_id = child_id
                pc._child_title = sub_title
                pc._child_url = sub_url
                pc._child_layer = 3
                self._parent_childs.put(pc)
        except:
            print(traceback.format_exc())
            print("wxmException, ", id, title, url, tagId, page)  # todo 描述

    def get_firstpage_content(self, url, data=None):
        header = {
            'Accept': 'text/css,*/*;q=0.1',
            'Accept-Encoding': 'gzip,deflate,sdch',
            'Accept-Language': 'zh-CN,zh;q=0.8',
            'Connection': 'keep-alive',
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36'
        }
        timeout = random.choice(range(80, 180))
        while True:
            try:
                rep = requests.get(url, headers=header, timeout=timeout)
                rep.encoding = 'utf-8'
                req = urllib.request.Request(url, data, header)
                response = urllib.request.urlopen(req, timeout=timeout)
                response.close()
                break
            except urllib.request.HTTPError as e:
                print(traceback.format_exc())
                print('wxmException,  1:', e)
                time.sleep(random.choice(range(1,3)))
            except urllib.request.URLError as e:
                print(traceback.format_exc())
                print('wxmException,  2:', e)
                time.sleep(random.choice(range(1,3)))
            except:
                print(traceback.format_exc())
                print('wxmException 0')
        return rep.text

    def get_firstpage_url(self, html):
        bs = BeautifulSoup(html, "html.parser")
        body = bs.body  # 获取body部分
        data = body.find_all('div', {'class': 'category-info'})  # 找到id为7d的div
        urls_list = []
        for a in data:
            ul = a.find('h5')
            url = ul.find('a')
            urls_list.append(url)
        return urls_list

    def get_firstpage_tagid(self, first_page_urls):
        links_list = []
        for i in first_page_urls:
            links = i.get('href')
            links_list.append(links)
        tagid_list = []
        for b in links_list:
            tagid = b[46:51]
            tagid_list.append(tagid)
        # 这里可以del 多个first_page_urls，将其分成多部分，供两台机器一起跑
        # 可以用切片，将tagid_list选择出对应的部分来
        del first_page_urls[0]
        del tagid_list[0]
        return tagid_list  # 检测出返回值最好跟你要输出的内容保持一直，出错率比较低

if __name__ == '__main__':
    spider = Spider()
    spider.execute()


#!C:\Users\Bhavya Mulpuri\AppData\Local\Programs\Python\Python36-32\python.exe
import requests
from bs4 import BeautifulSoup
import cgi, cgitb
import csv
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

print("Content-Type:text/html\r\n\r\n")
cgitb.enable();
form = cgi.FieldStorage();

state = "S29";""" form["s1"].value"""
constituency =  "82" ;"""form["s2"].value"""

url = "http://eciresults.nic.in/Constituencywise" + state + constituency + ".htm?ac=" + constituency;
print(url);

page = requests.get(url);
soup = BeautifulSoup(page.content, 'html.parser');
table = soup.find('table', border="1");
t_rows = table.find_all('tr', style="font-size:12px;");

result = [];

with open('data.csv', 'w') as f:
    for row in t_rows:
        result = row.find_all('td');
        # print("<p>%s%s%s</p>" %(result[0].text.strip(),result[1].text.strip(),result[2].text.strip()));
        data_writer = csv.writer(f, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL);
        data_writer.writerow(
            ['%s' % (result[0].text.strip()), '%s' % (result[1].text.strip()), '%s' % (result[2].text.strip())]);

var = [];
x_axis = [];
y_axis = [];
z_axis = [];

with open('data.csv', 'r') as f:
    var = csv.reader(f, delimiter=",");
    for i in var:
        if len(i) is not 0:
            x_axis.append(i[0]);
            y_axis.append(i[1]);
            z_axis.append(int(i[2]));

fig = plt.figure()
plt.bar(x_axis, z_axis, width=0.8, color=['red', 'green', 'pink', 'blue', 'black'])
for i,v in enumerate(z_axis):
    plt.text(x_axis[i] ,v ,str(v),horizontalalignment = 'center',fontsize =10);
plt.xlabel('candidates');
plt.ylabel('number of votes');
plt.title('Constituencywise result Data Visualization');
num=np.arange(len(x_axis));
plt.xticks(num,x_axis,fontsize=6,rotation=30);
plt.show();
plt.savefig('C:/xampp/htdocs/plot.pdf');


# 금광 / p375 / DP
# # 내가 푼 방법
from collections import deque

T = int(input())

dx = [-1,0,1] # 왼위, 왼, 왼아래
dy = [-1,-1,-1]

for i in range(T):
    queue = deque()
    n,m = map(int,input().split())
    data = [[] for i in range(n)]
    str = list(map(int,input().split()))
    for i in range(n):
        for j in range(m):
            data[i].append(str[(i*m) + j])

    answer = [[0 for _ in range(m)] for _ in range(n)]

    for i in range(n):
        answer[i][m - 1] = data[i][m - 1]
        queue.append((i,m-1))
    while queue:
        #print(answer)
        x,y = queue.popleft()
        for i in range(3):
            nx = x + dx[i]
            ny = y + dy[i]
            if 0<=nx<n and 0<=ny<m and answer[nx][ny] < answer[x][y] + data[nx][ny]:
                answer[nx][ny] = answer[x][y]+data[nx][ny]
                queue.append((nx,ny))

    max = 0
    for i in range(n):
        if max < answer[i][0]:
            max = answer[i][0]
    print(max)

# 책에서 푼 방법
# 테스트 케이스(Test Case) 입력
# for tc in range(int(input())):
#     n,m = map(int,input().split())
#     array = list(map(int,input().split()))
#
#     # 다이나믹 프로그래밍을 위한 2차원 DP 테이블 초기화
#     dp = []
#     index = 0
#     for i in range(n):
#         dp.append(array[index:index+m])
#         index += m
#
#     # 다이나믹 프로그래밍 진행
#     for j in range(1,m):
#         for i in range(n):
#             print(dp)
#             # 왼쪽 위에서 오는 경우
#             if i == 0:
#                 left_up = 0
#             else:
#                 left_up = dp[i-1][j-1]
#             # 왼쪽 아래에서 오는 경우
#             if i == n-1:
#                 left_down = 0
#             else:
#                 left_down = dp[i+1][j-1]
#             # 왼쪽에서 오는 경우
#             left = dp[i][j-1]
#             dp[i][j] = dp[i][j] + max(left_up, left_down, left)
#
#
#         result = 0
#         for i in range(n):
#             result = max(result,dp[i][m-1])
#
#     print(result)

# 2
# 3 4
# 1 3 3 2 2 1 4 1 0 6 4 7
# 4 4
# 1 3 1 5 2 2 4 1 5 0 2 3 0 6 1 2


from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from model import Todo

app = FastAPI()

from database import(
    fetchOneTodo,
    fetchAllTodos,
    createTodo,
    updateTodo,
    removeTodo,
)

origins = ['http://localhost:3000', 'http://localhost:4000']

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials= True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.get("/")
def read_root():
    return {"Hello":"Kiki"}

@app.get("/api/todo")
async def get_todo():
    response = await fetchAllTodos()
    return response

@app.get("/api/todo/{title}", response_model=Todo)
async def get_todo_by_id(title):
    response = await fetchOneTodo(title)
    if response:
        return response
    raise HTTPException(404,f"No Todo Item by that {title}")

# Create
@app.post("/api/todo/", response_model=Todo)
async def post_todo(todo:Todo):
    response = await createTodo(todo.dict())
    if response:
        return response
    raise HTTPException(400,"Something went wrong. Bad Request")

# Update
@app.put("/api/todo/{title}/", response_model=Todo)
async def put_todo(title:str, desc:str):
    response = await updateTodo(title, desc)
    if response:
        return response
    raise HTTPException(404, f"No Todo Item by that {title}")

# Delete
@app.delete("/api/todo/{title}")
async def delete_todo(title):
    response = await removeTodo(title)
    if response:
        return "Deleted todo item"
    raise HTTPException(404, f"There is no TODO item with title {title}")

'''
Can run the server with command below
uvicorn main:app --reload --port 4000
'''


"""Strava API key update.

:authors
    JP at 17/04/20
"""
from google.cloud import firestore
import logging
import requests
import time

db = firestore.Client()
collection = db.collection('strava')

class RefreshTokenBadRequest(Exception):
    """Expection for an invalid request to Strava to get a new token."""

    pass

def refresh_key(event, context):
    """Triggered from a message on a Cloud Pub/Sub topic. Update Strava keys stored in Firestore."""
    for doc in collection.stream():
        data = doc.to_dict()
        logging.info(f'got key {doc.id}')

        if time.time() < data['expires_at']:
            logging.info(f'expiry time {data["expires_at"]} not greater than current {time.time()}')
            continue
        else:
            logging.info(f'expiry time {data["expires_at"]} greater than current so getting a new access token')

        request_params = {'client_id': data['client_id'],
                          "client_secret": data['client_secret'],
                          "grant_type": "refresh_token",
                          "refresh_token": data['refresh_token']}

        response = requests.post("https://www.strava.com/api/v3/oauth/token", params=request_params)

        logging.info(f'made request to Strava and got response code {response.status_code}')

        if response.status_code != 200:
            logging.error(f'Bad request {response.status_code} content {response.text}')
            raise RefreshTokenBadRequest

        response_dict = response.json()

        update_doc = collection.document(doc.id)

        update_doc.update({'access_token': response_dict['access_token'],
                           'refresh_token': response_dict['refresh_token'],
                           'expires_at': response_dict['expires_at']})

        logging.info(f'updated {doc.id} with new expiry time {response_dict["expires_at"]}')


import sqlite3
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime

'''
This is a simple tool i made to create a plot graph with the data from the database. You can
manually enter the prefix's of the companys you want to look at in the prefix_list and it
will use all the data it can in the database corresponding to that prefix. This tool is used
to determine the sentiment score and volume over a chosen period of time.
'''
# Set the period of time you want to analyse - in days
time_period = 20

# Dates
date = datetime.datetime.today()
#date = date - datetime.timedelta(days=time_period)
past_date = date - datetime.timedelta(days=time_period)

conn = sqlite3.connect('Cobalt_Blue.db')
c = conn.cursor()

def select_scraped_data(prefix):
	c.execute("SELECT post_date, sentiment, price FROM Scraped_data WHERE company=?", (prefix,))
	data = c.fetchall()
	return data

# Set companys to graph by prefix eg COB
prefix_list = ['4DS']

# Loop through company prefix
graph = True
previous_item = [past_date]
for prefix in prefix_list:
	try:
		print("Analysing: " + prefix + " for past " + str(time_period) + " days")
		data = select_scraped_data(prefix)

		# Declaring x,y coord variables for graph
		x = []
		y = []
		x2 = []
		y2 = []

		# Converting to datestamp to sort by date
		date_data = []
		clean_data = []
		for item in data:
			item = list(item)
			item[0] = datetime.datetime.strptime(item[0], "%d/%m/%y")
			date_data.append(item)

		# Sorting dates and saving wanted dates in new list.
		date_data.sort()
		for item in date_data:
			if item[0] > past_date:
				clean_data.append(item)

		# Getting volume and sentiment
		volume, sentiment = 0, 0
		buy, sell, hold, none = 0, 0, 0, 0
		for item in clean_data:
			print(item)
			volume += 1
			if item[1] == 'Buy':
				buy += 1
				sentiment += 1
			elif item[1] == 'Sell':
				sell += 1
				sentiment -= 1
			elif item[1] == 'Hold':
				hold += 1
				if sentiment > 1:
					sentiment -= 1
				elif sentiment < -1:
					sentiment += 1
			elif item[1] == 'None':
				none += 1
		# Sorting dates to plot only end of day figure and decaying sentiment for time without posts.
			if item[0] > previous_item[0]:
				#print(item)
				# Finding days between posts and decaying sentiment at 0.25/day if no post.
				sent_decay = item[0] - previous_item[0]
				sent_decay = str(sent_decay)
				sent_decay = sent_decay[0:2]
				sent_decay = int(sent_decay)
				sent_decay = sent_decay/4
				print(sent_decay)
				if sentiment > 1 and sent_decay > 0.25:
					if sentiment >= sent_decay:
						sentiment -= sent_decay
					else:
						sentiment = 0

		# Converting date format for graphing and appending to coords.
				new_date = item[0].strftime('%d-%m-%Y')
				x.append(new_date)
				x2.append(new_date)
				y.append(sentiment)
				y2.append(item[2])
				previous_item = item

		# Buy/sell ratio
		ratio = ((buy - sell) / (buy + sell + hold + none))*100
		ratio = round(ratio, 2)
		# Volume ratio
		volume_ratio = round(volume / time_period, 1)

		print("Volume: " + str(volume))
		print("Buy: " + str(buy))
		print("Sell: " + str(sell))
		print("Hold: " + str(hold))
		print("None: " + str(none))
		print("Buy strength: " + str(ratio) + "%")
		print("Volume Strength: " + str(volume_ratio) + " posts per day")
		print("Sentiment Score: " + str(sentiment))
		ratio = 0
		volume_ratio = 0

	except Exception as e:
		print("Error - No data in that time period!")
		print(e)
		graph = False
print()

if graph:
	# Setting graph format - change between months/days and set fmt to %m or %d
	#months = mdates.MonthLocator()
	days = mdates.DayLocator()
	fmt = mdates.DateFormatter('%d')

	# Converting back to datetime to graph
	x = [datetime.datetime.strptime(d,'%d-%m-%Y').date() for d in x]
	x2 = [datetime.datetime.strptime(d,'%d-%m-%Y').date() for d in x2]

	# Plotting coords
	fig, ax = plt.subplots()
	ax2 = ax.twinx()
	ax.plot(x, y, label='Sentiment', color='r')
	ax.set_xlabel('Time (days)')
	ax.set_ylabel('Sentiment Score')
	ax2.plot(x2, y2, label='Price', color='b')
	ax2.set_ylabel("Price at Post")

	# Getting legends for graph
	handles, labels = ax.get_legend_handles_labels()
	ax.legend(handles, labels, loc='center left')
	# Setting x axis format
	ax.xaxis.set_major_locator(days)
	ax.xaxis.set_major_formatter(fmt)

	# Graphing info
	plt.title(str(prefix) + ' Sentiment')
	plt.legend()
	plt.show()

# Closing database connections
c.close()
conn.close()


from pynput import keyboard

# Defines a function for the keylogger

def keyPressed(key):
    print(str(key))
    with open("keyfile.text",'a') as logKey:
        try:
            char = key.char
            logKey.write()
        except:
            print("Error getting char")

if __name__ == "__main__":
    listener = keyboard.Listener(on_press=keyPressed)
    listener.start()
    input()


import errno
import sqlite3
import os

from mdde_stats.service.data import Benchmark

class LocalDataManager:

    def __init__(self, data_dir: str):
        """
        Constructor
        :param data_dir: path to directory where database should be stored
        """
        if data_dir is None:
            raise TypeError('data_dir path was not supplied')

        self._db_name = 'mdde_stats.db'
        self._data_dir = os.path.abspath(data_dir)
        try:
            # Create date folder if not exists
            os.makedirs(self._data_dir)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise
        self._full_path = os.path.join(self._data_dir, self._db_name)
        self._connection = None

    def initialize_db(self, db_file: str = None):
        """
        Initialize the local db
        :param db_file: (optional) Name of the database file. If not specified, default name is used
        :return:
        """
        if db_file is not None:
            if db_file.isspace():
                raise ValueError("db_file must be a valid file name")
            self._db_name = db_file
            self._full_path = os.path.join(self._data_dir, self._db_name)
        self._make_schema()

    @property
    def db_location(self) -> str:
        """
        Get location of the database file
        :return: String with full path to the database
        """
        return self._full_path

    TABLE_LOGS = 'logs'

    def _make_schema(self):
        """
        Create schema if needed
        """
        conn = sqlite3.connect(self.db_location)
        try:
            with conn:
                c = conn.cursor()
                logs_create_q = 'CREATE TABLE IF NOT EXISTS logs (' \
                                'run_id TEXT NOT NULL, ' \
                                'node_id TEXT, ' \
                                'tuple_id TEXT, ' \
                                'source TEXT, ' \
                                'operation INTEGER NOT NULL, ' \
                                'epoch DATETIME ' \
                                ');'
                logs_run_idx_create_q = 'CREATE INDEX IF NOT EXISTS run_idx ON logs (run_id);'
                logs_run_node_create_q = 'CREATE INDEX IF NOT EXISTS run_node ON logs (node_id);'
                c.execute(logs_create_q)
                c.execute(logs_run_idx_create_q)
                c.execute(logs_run_node_create_q)
        finally:
            conn.close()

    def flush(self):
        """
        Flush all records from the database leaving schema intact
        :return:
        """
        conn = sqlite3.connect(self.db_location)
        try:
            with conn:
                c = conn.cursor()
                c.execute('DELETE FROM {};'.format(self.TABLE_LOGS))
        finally:
            conn.close()

    def __enter__(self) -> Benchmark:
        if self._connection is not None:
            raise RuntimeError("There is already an open connection")
        self._connection = sqlite3.connect(self.db_location)
        return Benchmark(self._connection)

    def __exit__(self, exc_type, exc_value, traceback):
        if exc_type is not None:
            self._connection.rollback()
        else:
            self._connection.commit()
        self._connection.close()
        self._connection = None


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

"""
Pour éviter de surcharger de commandes le drone.
"""

from vector import Vector

class Queue (object):
    """
    Permet de moduler le flux de commandes à donner au drone, en moyennant les vecteurs déplacement successifs sur plusieurs frames.
    """

    def __init__(self, drone, cmd_max):
        self.queue = []
        self.processed = []
        self.drone = drone

        self.opposed = {self.drone.forward : self.drone.backward,
                self.drone.backward : self.drone.forward,
                self.drone.left : self.drone.right,
                self.drone.right : self.drone.left,
                self.drone.down : self.drone.up,
                self.drone.up : self.drone.down,
                self.drone.rLeft : self.drone.rRight,
                self.drone.rRight : self.drone.rLeft}
        self.count = 0
        self.cmd_max = cmd_max

    def flush(self):
        """
        Envoie les commandes intelligemment : somme les vecteurs déplacement pour améliorer la précision des déplacements.
        """
        #Vecteur représentant le pointage moyen sur plusieurs frames
        sum_vector = Vector(0,0,0)
        for vector in self.queue:
            sum_vector += vector

        sum_vector /= self.count
        self.drone.processVector(sum_vector)
        self.empty()

    def empty(self):
        self.queue = []
        self.count = 0

    def add(self, offset):
        self.queue.append(Vector(offset[0], offset[1], offset[2]))
        self.count+=1

    def check(self):
        if self.count >= self.cmd_max:
            self.flush()


from django.db import models
# from address.models import AddressField
from django import forms
# from django_google_maps import fields as map_fields
from Location.models import Address
from django.utils.html import mark_safe
from django.conf import settings
from django.db.models import Avg
from decimal import *

class ImageUploadForm(forms.Form):
    """Image upload form."""
    image = forms.ImageField()

class Restaurant(models.Model):
    name = models.CharField(max_length=42)
    newfields = models.CharField(max_length=42, null=True, blank=True)
    restaurant_address = models.CharField("Address", max_length=42, null=True, blank=True)
    email = models.EmailField(max_length=75, blank=True)
    website = models.URLField(max_length=200, null=True, blank=True)
    content = models.TextField()
    created_on = models.DateTimeField(auto_now_add=True)
    # banner_image = models.ImageField(upload_to='restaurant/images/', default='restaurant/images/None/no-img.jpg')
    # logo = models.ImageField(upload_to='restaurant/images/', default='restaurant/images/None/no-img.jpg')
    restaurant_banner_image = models.ImageField("Banner Image", upload_to='restaurant/images/',blank=True)
    restaurant_logo = models.ImageField("Logo", upload_to='restaurant/images/',blank=True)
    # owner = models.ForeignKey('auth.User', related_name='restaurant_owner', editable=False, on_delete=models.CASCADE)
    owner = models.ForeignKey('auth.User', related_name='restaurant_owner', on_delete=models.CASCADE)
    address = models.OneToOneField(Address, null=True, related_name='restaurants', on_delete=models.CASCADE)
    # address = map_fields.AddressField(max_length=200, null=True, blank=True)
    # geolocation = map_fields.GeoLocationField(max_length=100, null=True, blank=True)
    # from django_google_maps import fields as map_fields
    # follower = models.ManyToManyField('auth.User', blank=True)
    # address1 = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'id': "autocomplete"}))
    # address2 = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'id': "autocomplete"}))
    # address1 = AddressField(blank=True, null=True)
    # address2 = AddressField(related_name='+', blank=True, null=True)
    # highlighted = models.TextField()

    def restaurant_logo_tag(self):
        return mark_safe('<img class="user-image img-circle" src="%s%s" width="80" height="80" />' % (settings.MEDIA_URL , self.restaurant_logo))
    restaurant_logo_tag.short_description = 'Restaurant Logo'

    # def average_rating(self):
    #     # breakpoint()
    #     avg =  self.reviews.aggregate(Avg('rating'))
    #     breakpoint()
    #     return Decimal(avg)

    def __str__(self):
        return self.name

    class Meta:
        ordering = ['created_on']
        # permissions = (
        #     ('read_item','Can read item'),
        # )