import pygame
import sys

class UI:

    def __init__(self, UIdeck):
        self.drawMap = {0:self.drawPlayer1, 1:self.drawPlayer2, 2:self.drawPlayer3, 3:self.drawPlayer4}
        self.discardMap = {0:self.discardCardP1, 1:self.discardCardP2, 2:self.discardCardP3, 3:self.discardCardP4}
        pygame.init()
        width = 800
        height = 600
        gameOver = False

        ## makes the screen
        self.screen = pygame.display.set_mode((width,height))
        pygame.display.set_caption('Uno')

        black = (0,0,0)
        white = (255,255,255)
        self.transparent = (0, 0, 0, 0)

        self.cards= ['images/black_+4.png','images/black_+4.png','images/black_+4.png','images/black_+4.png','images/black_wildcard.png','images/black_wildcard.png','images/black_wildcard.png','images/black_wildcard.png',
                 'images/blue_+2.png','images/blue_0.png', 'images/blue_1.png', 'images/blue_2.png', 'images/blue_3.png', 'images/blue_4.png', 'images/blue_5.png', 'images/blue_6.png', 'images/blue_7.png', 'images/blue_8.png', 'images/blue_9.png', 'images/blue_reverse.png', 'images/blue_skip.png',
                 'images/green_+2.png','images/green_0.png', 'images/green_1.png', 'images/green_2.png', 'images/green_3.png', 'images/green_4.png', 'images/green_5.png', 'images/green_6.png', 'images/green_7.png', 'images/green_8.png', 'images/green_9.png', 'images/green_reverse.png', 'images/green_skip.png',
                 'images/red_+2.png','images/red_0.png', 'images/red_1.png', 'images/red_2.png', 'images/red_3.png', 'images/red_4.png', 'images/red_5.png', 'images/red_6.png', 'images/red_7.png', 'images/red_8.png', 'images/red_9.png', 'images/red_reverse.png', 'images/red_skip.png',
                 'images/yellow_+2.png','images/yellow_0.png', 'images/yellow_1.png', 'images/yellow_2.png', 'images/yellow_3.png', 'images/yellow_4.png', 'images/yellow_5.png', 'images/yellow_6.png', 'images/yellow_7.png', 'images/yellow_8.png', 'images/yellow_9.png', 'images/yellow_reverse.png', 'images/yellow_skip.png',]

        for card in UIdeck:
            if card.color == 'wild':
                card.color = 'black'
            if card.value == 'basic':
                card.value = 'wildcard'
        # print(UIdeck)

        self.cardMap = {card:'images/' + card.color + '_' + str(card.value) + '.png' for card in UIdeck}
        self.reverseMap = {'images/' + card.color + '_' + str(card.value) + '.png':card for card in UIdeck}
        self.deck = []
        self.discard = []
        self.player1Hand = []
        self.player2Hand = []
        self.player3Hand = []
        self.player4Hand = []
        self.player1XPoints = []
        self.player2XPoints = []
        self.player3XPoints = []
        self.player4XPoints = []
        self.screen.fill(black)
        self.turnCondition = True
        self.dealDeck(card)

    ## deals all the cards to player 1 and loads them onto the screen, keeping track of what cards are in player 1's hand and where they are on the screen
    def dealPlayer1(self, hand):
        x = 0
        y = 10
        for card in hand:
            image = pygame.image.load(self.cardMap[card])
            self.player1Hand.append(self.cardMap[card])
            self.screen.blit(image, (x,y))
            self.player1XPoints.append(x)
            x += 50

    ## deals all the cards to player 2 and loads them onto the screen, keeping track of what cards are in player 2's hand and where they are on the screen
    def dealPlayer2(self, hand):
        x =  10
        y = 135
        for card in hand:
            image = pygame.image.load(self.cardMap[card])
            self.player2Hand.append(self.cardMap[card])
            self.screen.blit(image, (x,y))
            self.player2XPoints.append(x)
            x += 25

    ## deals all the cards to player 3 and loads them onto the screen, keeping track of what cards are in player 3's hand and where they are on the screen
    def dealPlayer3(self, hand):
        x =  10
        y = 260
        for card in hand:
            image = pygame.image.load(self.cardMap[card])
            self.player3Hand.append(self.cardMap[card])
            self.screen.blit(image, (x,y))
            self.player3XPoints.append(x)
            x += 25

    ## deals all the cards to player 4 and loads them onto the screen, keeping track of what cards are in player 4's hand and where they are on the screen
    def dealPlayer4(self, hand):
        x =  10
        y = 385
        for card in hand:
            image = pygame.image.load(self.cardMap[card])
            self.player4Hand.append(self.cardMap[card])
            self.screen.blit(image, (x,y))
            self.player4XPoints.append(x)
            x += 25

## after cards are dealt to each player, the remaining cards are displayed as a stack in the center of the screen
## and flips the first card over to start the game
    def dealDeck(self, card = None):
        x =  540
        y = 260
        image = pygame.image.load('images/back.png')
        self.screen.blit(image, (x,y))
        # image = pygame.image.load(self.cardMap[card])
        # self.screen.blit(image, (440,260))

    def draw(self, player, card):
        self.drawMap[player](card)

    def drawPlayer1(self, card):
        self.player1Hand.append(self.cardMap[card])
        size1X = len(self.player1XPoints)
        nextPoint = self.player1XPoints[size1X-1] + 25
        self.player1XPoints.append(nextPoint)
        image = pygame.image.load(self.cardMap[card])
        self.screen.blit(image, (nextPoint,10))
        pygame.display.update()

    ## when called, will draw a card from the draw pile and add it to player 2's hand
    def drawPlayer2(self, card):
        self.player2Hand.append(self.cardMap[card])
        size2X = len(self.player2XPoints)
        nextPoint = self.player2XPoints[size2X-1] + 25
        self.player2XPoints.append(nextPoint)
        image = pygame.image.load(self.cardMap[card])
        self.screen.blit(image, (nextPoint,135))
        pygame.display.update()

    ## when called, will draw a card from the draw pile and add it to player 3's hand
    def drawPlayer3(self, card):
        self.player3Hand.append(self.cardMap[card])
        size3X = len(self.player3XPoints)
        nextPoint = self.player3XPoints[size3X-1] + 25
        self.player3XPoints.append(nextPoint)
        image = pygame.image.load(self.cardMap[card])
        self.screen.blit(image, (nextPoint,260))
        pygame.display.update()

    ## when called, will draw a card from the draw pile and add it to player 4's hand
    def drawPlayer4(self, card):
        self.player4Hand.append(self.cardMap[card])
        size4X = len(self.player4XPoints)
        nextPoint = self.player4XPoints[size4X-1] + 25
        self.player4XPoints.append(nextPoint)
        image = pygame.image.load(self.cardMap[card])
        self.screen.blit(image, (nextPoint,385))
        pygame.display.update()

    def discardfoo(self, player, card):
        # print(self.player1Hand)
        # print(self.player2Hand)
        # print(self.player3Hand)
        # print(self.player4Hand)
        # print(card)
        # print(self.cardMap[card])
        print('p'+str(player+1), 'discarded', card)
        self.discardMap[player](card)

    def discardCardP1(self, card):
        self.discard.append(self.cardMap[card])
        image = pygame.image.load(self.cardMap[card])
        self.screen.blit(image, (440,260))
        pygame.display.update()
        self.player1Hand.remove(self.cardMap[card])
        lastI = len(self.player1XPoints)-1
        self.player1XPoints.remove(self.player1XPoints[lastI])
        pygame.draw.rect(self.screen, self.transparent,(0,10,600,125))
        pygame.display.update()

        for x_point in self.player1XPoints:
            index = self.player1XPoints.index(x_point)
            card = self.player1Hand[index]
            image = pygame.image.load(card)
            self.screen.blit(image, (x_point,10))
            pygame.display.update()

    def discardCardP2(self, card):
        self.discard.append(self.cardMap[card])
        image = pygame.image.load(self.cardMap[card])
        self.screen.blit(image, (440,260))
        pygame.display.update()

        self.player2Hand.remove(self.cardMap[card])
        lastI = len(self.player2XPoints)-1
        self.player2XPoints.remove(self.player2XPoints[lastI])
        pygame.draw.rect(self.screen, self.transparent,(10,135,600,125))
        pygame.display.update()

        for x_point in self.player2XPoints:
            index = self.player2XPoints.index(x_point)
            card = self.player2Hand[index]
            image = pygame.image.load(card)
            self.screen.blit(image, (x_point,135))
            pygame.display.update()

    def discardCardP3(self, card):
        # print(card)
        # print(self.cardMap[card])
        self.discard.append(self.cardMap[card])
        image = pygame.image.load(self.cardMap[card])
        self.screen.blit(image, (440,260))
        pygame.display.update()

        self.player3Hand.remove(self.cardMap[card])
        lastI = len(self.player3XPoints)-1
        self.player3XPoints.remove(self.player3XPoints[lastI])
        pygame.draw.rect(self.screen, self.transparent, (10,260,400,125))
        pygame.display.update()

        for x_point in self.player3XPoints:
            index = self.player3XPoints.index(x_point)
            card = self.player3Hand[index]
            image = pygame.image.load(card)
            self.screen.blit(image, (x_point,260))
            pygame.display.update()

    def discardCardP4(self, card):
        self.discard.append(self.cardMap[card])
        image = pygame.image.load(self.cardMap[card])
        self.screen.blit(image, (440,260))
        pygame.display.update()

        self.player4Hand.remove(self.cardMap[card])
        lastI = len(self.player4XPoints)-1
        self.player4XPoints.remove(self.player4XPoints[lastI])
        pygame.draw.rect(self.screen, self.transparent,(10,385,600,125))
        pygame.display.update()

        for x_point in self.player4XPoints:
            index = self.player4XPoints.index(x_point)
            card = self.player4Hand[index]
            image = pygame.image.load(card)
            self.screen.blit(image, (x_point,385))
            pygame.display.update()

    def humanTurn(self, drawnCard = None):
        print('v')
        events = []
        drawn = False
        while True:
            # print('y')
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    print('quitted')
                    sys.exit()
                if event.type == pygame.MOUSEBUTTONDOWN: ## if there is a mouse click
                    if not drawn:
                        print('sdf')
                        if event.pos[0] > 540 and event.pos[0] < 600:
                            if event.pos[1] > 265 and event.pos[1] < 365: ## if the click is on the draw pile -> player 1 gets that card in it's hand
                                if self.turnCondition: return 'draw a card'
                                drawn = True
                                self.player1Hand.append(self.cardMap[drawnCard])
                                size1X = len(self.player1XPoints)
                                nextPoint = self.player1XPoints[size1X-1] + 50
                                self.player1XPoints.append(nextPoint)
                                print(self.cardMap[drawnCard])
                                image = pygame.image.load(self.cardMap[drawnCard])
                                self.screen.blit(image, (nextPoint,10))
                                pygame.display.update()

                    if event.pos[1] > 10 and event.pos[1] < 110:
                        for xPos in self.player1XPoints:
                            if event.pos[0] > xPos and event.pos[0] < xPos + 50:
                                index = self.player1XPoints.index(xPos)
                                selectedCard = self.player1Hand[index]
                                self.discard.append(selectedCard)
                                image = pygame.image.load(selectedCard)
                                self.screen.blit(image, (440,260))
                                pygame.display.update()
                                self.player1Hand.remove(selectedCard)
                                lastI = len(self.player1XPoints)-1
                                self.player1XPoints.remove(self.player1XPoints[lastI])
                                pygame.draw.rect(self.screen, self.transparent,(0,10,600,125))
                                pygame.display.update()

                                for x_point in self.player1XPoints:
                                    index = self.player1XPoints.index(x_point)
                                    card = self.player1Hand[index]
                                    image = pygame.image.load(card)
                                    self.screen.blit(image, (x_point,10))
                                    pygame.display.update()

                                return ('discard', self.reverseMap[selectedCard])
# deal()
# chooseCardP2(random.choice(player2Hand))
# chooseCardP3(random.choice(player3Hand))
# chooseCardP4(random.choice(player4Hand))
# ##drawPlayer3()
# ##drawPlayer4()

# pygame.display.update()
# while not gameOver:
#     for event in pygame.event.get():
#         if event.type == pygame.QUIT:
#             sys.exit()
#         if event.type == pygame.MOUSEBUTTONDOWN: ## if there is a mouse click
#             if event.pos[0] > 540 and event.pos[0] < 600:
#                 if event.pos[1] > 265 and event.pos[1] < 365: ## if the click is on the draw pile -> player 1 gets that card in it's hand
#                     card = deck.pop()
#                     player1Hand.append(card)
#                     size1X = len(player1XPoints)
#                     nextPoint = player1XPoints[size1X-1] + 50
#                     player1XPoints.append(nextPoint)
#                     image = pygame.image.load(card)
#                     screen.blit(image, (nextPoint,10))
#                     pygame.display.update()
#
#             if event.pos[1] > 10 and event.pos[1] < 110:
#                 for xPos in player1XPoints:
#                     if event.pos[0] > xPos and event.pos[0] < xPos + 50:
#                         index = player1XPoints.index(xPos)
#                         selectedCard = player1Hand[index]
#
#                         discard.append(selectedCard)
#                         image = pygame.image.load(selectedCard)
#                         screen.blit(image, (440,260))
#                         pygame.display.update()
#                         player1Hand.remove(selectedCard)
#                         lastI = len(player1XPoints)-1
#                         player1XPoints.remove(player1XPoints[lastI])
#                         pygame.draw.rect(screen, self.transparent,(0,10,600,125))
#                         pygame.display.update()
#
#                         for x_point in player1XPoints:
#                             index = player1XPoints.index(x_point)
#                             card = player1Hand[index]
#                             image = pygame.image.load(card)
#                             screen.blit(image, (x_point,10))
#                             pygame.display.update()
#
#                         break
#
#
# pygame.display.update()

##pygame.quit()
##quit()


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

import requests
from bs4 import BeautifulSoup as bs

def get_historical_data(name, number_of_days):
    data = []
    url = "https://finance.yahoo.com/quote/" + name + "/history/"
    content = requests.get(url).content
    rows = bs(content, 'html.parser').findAll('table')[0].tbody.findAll('tr')

    for each_row in rows:
        divs = each_row.findAll('td')
        if divs[1].span.text != 'Dividend':
            data.append({'Date': divs[0].span.text, 'Open': float(divs[1].span.text.replace(',', ''))})

    return data[:number_of_days]

if __name__ == '__main__':
    get_historical_data('AAPL', 10)


from urllib.request import Request, urlopen
from fake_useragent import UserAgent
import json
import csv

headers = {"user-agent": UserAgent().chrome, "referer": "https://finance.daum.net/"}
path = "./RPAbasic/crawl/download/"
data = []
try:
    url = "https://finance.daum.net/api/search/ranks?limit=10"
    res = urlopen(Request(url, headers=headers)).read().decode("UTF-8")
    # print(res) # JSON 형태로 반환

    # JSON To Dictionary
    rank_json = json.loads(res)["data"]

    for item in rank_json:
        print(
            "순위 {}, 금액 {}, 회사명 {}".format(
                item["rank"], item["tradePrice"], item["name"]
            )
        )
        data.append(item)

    # 크롤링 데이터 저장 ( txt 파일 / csv 파일 )
    with open(path + "finance.txt", "a", encoding="UTF-8") as txt:
        with open(path + "finance.csv", "a", encoding="UTF-8", newline="") as csvfile:
            # 텍스트 저장
            txt.write(
                "순위 {}, 금액 {}, 회사명 {}\n".format(
                    item["rank"], item["tradePrice"], item["name"]
                )
            )

            # csv 저장
            output = csv.writer(csvfile)
            # Column 타이틀 (Header)
            output.writerow(data[0].keys())
            for row in data:
                output.writerow(row.values())  # value 저장

except Exception as e:
    print(e)

# 비동기식 데이터 ( Ajax ) 는 값을 가져오지 못하고 있음

# 브라우저 설정 Network 탭 ( F12 )
# DOC : 실제 브라우저 페이지에 대한 요청 정보
# Fetch/XHR : 비동기식 데이터 접근을 위한 요청 정보
#   일부 사이트는 보안상의 문제로 AJax URL 접근을 제한 (Security ...) >> 403 Error 발생
#   데이터 확인 법 ( preview 탭에서 주고받은 데이터 확인 가능 )
#   ( headers 탭 ) referer: https://finance.daum.net/ ( 정상적으로 동작하기 위해서는 : https://finance.daum.net 에서 해당 데이터를 요청해야 함 )
#       referer : 데이터 요청이 들어온 페이지 ( 크롤링 할 때, fake 주소를 지정해서 Ajax 데이터를 조회해야 한다 )


import torch
import pytorch_ssim

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

nc = 3
imsize = 256

def ssim(img, ref,weight_ssim):
    img = img.reshape(1, nc, imsize, imsize)

    img.requires_grad_()

    ssim_value = pytorch_ssim.ssim(ref, img)
    ssim_loss = pytorch_ssim.SSIM()
    ssim_out = -weight_ssim * ssim_loss(ref, img)
    ssim_out.backward()

    #    del ref
    #    torch.cuda.empty_cache()
    return ssim_value, img.grad.flatten()

def ssim_opt(m0, temp, ref,weight_ssim):
    temp = temp.reshape(1, nc, imsize, imsize)

    # _, nc, imsize, imsize = temp.shape
    temp.requires_grad_()

    ssim_value = pytorch_ssim.ssim(ref, temp)
    ssim_loss = pytorch_ssim.SSIM()
    ssim_out = -weight_ssim * ssim_loss(ref, temp)
    comp = ((-weight_ssim * m0) - ssim_out) ** 2
    comp.backward()

    return comp, temp.grad


from datetime import timedelta
from random import randint, choices

import faker_commerce
from django.core.management.base import BaseCommand
from django_seed import Seed
from faker import Faker

from catalog.models import Category, Discount, Producer, Promocode, Product

class Command(BaseCommand):
    help = "Filling database"
    __DEFAULT_COUNT_PRODUCERS = 200
    __DEFAULT_COUNT_CATEGORIES = 75
    __DEFAULT_COUNT_PRODUCTS = 2000
    __DEFAULT_COUNT_PROMOCODES = 250
    __DEFAULT_COUNT_DISCOUNTS = 20

    def __init__(self, *args, **kwargs):
        self.seeder = Seed.seeder()
        self.fake = Faker()
        self.fake.add_provider(faker_commerce.Provider)
        super().__init__(*args, **kwargs)

    def handle(self, *args, **options):
        self.__create_producers()
        self.__create_categories()
        self.__create_discounts()
        self.__create_promocodes()
        self.__create_products()
        return "Fake data is created"

    def __create_producers(self):
        self.seeder.add_entity(
            model=Producer,
            number=self.__DEFAULT_COUNT_PRODUCERS,
            customFieldFormatters={
                "name": lambda x: self.fake.company(),
                "description": lambda x: self.fake.paragraph(nb_sentences=3),
                "country": lambda x: self.fake.country(),
            },
        )
        self.seeder.execute()

    def __create_categories(self):
        self.seeder.add_entity(
            model=Category,
            number=self.__DEFAULT_COUNT_CATEGORIES,
            customFieldFormatters={
                "name": lambda x: self.fake.ecommerce_category(),
                "description": lambda x: self.fake.paragraph(nb_sentences=2),
            },
        )
        self.seeder.execute()

    def __create_discounts(self):
        self.seeder.add_entity(
            model=Discount,
            number=self.__DEFAULT_COUNT_DISCOUNTS,
            customFieldFormatters={
                "percent": lambda x: randint(1, 50),
                "name": lambda x: self.fake.sentence(nb_words=1)[:-1],
                "date_start": lambda x: self.fake.date_this_month(),
                "date_end": lambda x: self.fake.date_this_month()
                + timedelta(days=randint(5, 100)),
            },
        )
        self.seeder.execute()

    def __create_promocodes(self):
        self.seeder.add_entity(
            model=Promocode,
            number=self.__DEFAULT_COUNT_PROMOCODES,
            customFieldFormatters={
                "name": lambda x: self.fake.swift(length=8),
                "percent": lambda x: randint(1, 50),
                "date_start": lambda x: self.fake.date_this_month(),
                "date_end": lambda x: self.fake.date_this_month()
                + timedelta(days=randint(20, 200)),
                "is_cumulative": lambda x: self.fake.pybool(),
            },
        )
        self.seeder.execute()

    def __create_products(self):
        discounts = Discount.objects.all()
        categories = Category.objects.all()
        producers = Producer.objects.all()
        self.seeder.add_entity(
            model=Product,
            number=self.__DEFAULT_COUNT_PRODUCTS,
            customFieldFormatters={
                "name": lambda x: self.fake.ecommerce_name(),
                "price": lambda x: self.fake.ecommerce_price(),
                "count_on_stock": lambda x: randint(100, 10000),
                "articul": lambda x: self.fake.isbn10(),
                "description": lambda x: self.fake.paragraph(nb_sentences=2),
                "discount": lambda x: choices(discounts)[0],
                "category": lambda x: choices(categories)[0],
                "producer": lambda x: choices(producers)[0],
            },
        )
        self.seeder.execute()


#defino librerias
import csv
import datetime as ti
#constantes y variables
opciones="""
Bienvenido
__________________________
seleccione una opcion
1.Ingresar datos

2.salir

"""

runTime=True
datetime = ti.date.today()
#funciones
"""Recive el nombre del archivo atraves de un input de usuario y si el archivo existe convierte los datos a valores dentro
de un diccionario donde definimos las claves.
"""
def readFile(urlfile):
    cheques=[]
    file=open(urlfile+".csv","r")
    csvfile=csv.reader(file)
    for row in csvfile:
        if row != []:
            data = {"Nrocheque":row[0],"codigoBanco":row[1],
            "codigoSucursal":row[2],"ctaOrigen":row[3],"ctaDestino":row[4],
            "valor":row[5],"fechaOrigen":row[6],"fechaPago":row[7],"DNI":row[8],"tipo":row[9],"estado":row[10],}
            cheques.append(data)
    file.close
    return cheques

"""Funcion hecha para devolver los datos requeridos por el usuario, que son filtrados por el dni y el tipo de cheque
solicitado a traves de un input que son verificados en torno a las claves y valores del diccionario realizado en la funcion anterior"""
def buscarDni(dni, tipo):
    busqueda=[]
    cantidad = 0
    cheques = readFile(urlfile)
    for cheque in cheques:
        if cheque["DNI"]==dni and cheque["tipo"]==tipo:
            cantidad += 1
            print("cheque encontrado\n")
            busqueda.append(cheque)
    repetidos = []
    for i in busqueda:

        repetidos = map(lambda x:x["Nrocheque"], busqueda)
        if i["Nrocheque"] in busqueda:
            repetidos.append(i["Nrocheque"])
    if repetidos !=[]:
        print("Error cheques duplicados")
    else:
        print(f"se encontraron {cantidad} cheques")
        print(busqueda)

def CSVDES(dni, busqueda):
    file = open(dni+"__"+datetime+"csv","w")
    csvfile=csv.writer(file)
    for row in busqueda:
        csvfile.writerow([row["Nrocheque"],row["codigoBanco"],row[
            "codigoSucursal"],row["ctaOrigen"],row["ctaDestino"],row[
            "valor"],row["fechaOrigen"],row["fechaPago"],row["DNI"],row["tipo"],row["estado"]])
    file.close
    print("csv grabado")

#metodo principal

if __name__=="__main__":
    while runTime:
        print(opciones)
        op = input()
        if op == "1":
            urlfile = input("ingrese el nombre del archivo: \n")
            dni = input("ingrese el DNI del usuario a consultar: \n")
            tipo = input("tipo de cheque a buscar EMITIDO o DEPOSITADO: \n")
            salida = input("desea la salida por pantalla o csv: \n")
            print(urlfile,dni,tipo,salida)
            try:
                resultado = buscarDni(dni, tipo)
                if salida =="PANTALLA":
                    print(resultado)
                elif salida == "CSV":
                    CSVDES(resultado)
                else:
                    print("invalido")
            except:
                print("invalido")

        elif op =="2":
            runTime = False
        else:
            runTime = False


import requests
import re

# Manual PDF: https://www.correios.com.br/a-a-z/pdf/rastreamento-de-objetos/manual_rastreamentoobjetosws.pdf
class Objeto(object):
    def __init__(self, *args, **kwargs):
        self.cepDestino = ""
        self.dataPostagem = ""
        self.eventos = list()
        self.numero = kwargs.get('numero', '')
        self.categoria = kwargs.get('categoria', '')
        self.sigla = kwargs.get('sigla', '')
        self.nome = kwargs.get('nome', '')
        self.json = ""

        if 'evento' in kwargs and len(kwargs.get('evento', list())) > 0:
            evento = kwargs.get('evento')[0]
            self.cepDestino = evento.get('cepDestino', '')
            self.dataPostagem = evento.get('dataPostagem', '')

            for evento in kwargs.get('evento', list()):
                self.eventos.append(Evento(**evento))

class Evento(object):
    def __init__(self, *args, **kwargs):
        self.tipo = kwargs.get('tipo', '')
        self.data = kwargs.get('data', '')
        self.hora = kwargs.get('hora', '')
        self.criacao = kwargs.get('criacao', '')
        self.prazoGuarda = kwargs.get('prazoGuarda', '')
        self.diasUteis = kwargs.get('diasUteis', '')
        self.descricao = kwargs.get('descricao', '')
        self.detalhe = kwargs.get('detalhe', '')
        self.status = kwargs.get('status', '')

        if 'unidade' in kwargs:
            self.unidade = Unidade(**kwargs.get('unidade', dict()))

        if 'destino' in kwargs and len(kwargs.get('destino', list())) > 0:
            self.destino = Destino(**kwargs.get('destino')[0])

        if 'detalheOEC' in kwargs:
            self.detalheOEC = OEC(**kwargs.get('detalheOEC', dict()))

class Unidade(object):
    def __init__(self, *args, **kwargs):
        self.tipounidade = kwargs.get('tipounidade', '')
        self.local = kwargs.get('local', '')
        self.sto = kwargs.get('sto', '')
        self.codigo = kwargs.get('codigo', '')
        self.uf = kwargs.get('uf', '')
        self.cidade = kwargs.get('cidade', '')

        if 'endereco' in kwargs:
            self.endereco = Endereco(**kwargs.get('endereco', dict()))

class Endereco(object):
    def __init__(self, *args, **kwargs):
        self.numero = kwargs.get('numero', '')
        self.cep = kwargs.get('cep', '')
        self.localidade = kwargs.get('localidade', '')
        self.bairro = kwargs.get('bairro', '')
        self.codigo = kwargs.get('codigo', '')
        self.logradouro = kwargs.get('logradouro', '')
        self.uf = kwargs.get('uf', '')
        self.latitude = kwargs.get('latitude', '')
        self.longitude = kwargs.get('longitude', '')

class Destino(object):
    def __init__(self, *args, **kwargs):
        self.bairro = kwargs.get('bairro', '')
        self.local = kwargs.get('local', '')
        self.cidade = kwargs.get('cidade', '')
        self.uf = kwargs.get('uf', '')
        self.codigo = kwargs.get('codigo', '')

        if 'endereco' in kwargs:
            self.endereco = Endereco(**kwargs.get('endereco', dict()))

class OEC(object):
    def __init__(self, *args, **kwargs):
        self.lista = kwargs.get('lista', '')
        self.longitude = kwargs.get('longitude', '')
        self.latitude = kwargs.get('latitude', '')
        self.carteiro = kwargs.get('carteiro', '')
        self.distrito = kwargs.get('distrito', '')
        self.unidade = kwargs.get('unidade', '')

        if 'endereco' in kwargs:
            self.endereco = Endereco(**kwargs.get('endereco', dict()))

def generate_valid_code(cod, with_cep=False):
    cod = cod.strip()

    if len(cod) < 12 or 13 < len(cod):
        return ""

    prefix = cod[0:2]
    number = cod[2:10]
    suffix = cod[-2:]
    multipliers = [8, 6, 4, 2, 3, 5, 9, 7]

    if len(number) < 8 and len(cod) == 12:
        diff = 8 - len(number)
        zeros = "0" * diff
        number = zeros + number

    sum_ = sum(int(number[i]) * multipliers[i] for i in range(8))
    rest = sum_ % 11

    if rest == 0:
        verifying_digit = "5"
    elif rest == 1:
        verifying_digit = "0"
    else:
        verifying_digit = str(11 - int(rest))

    valid_code = prefix + number + verifying_digit + suffix

    if with_cep:
        obj = track(valid_code)
        return valid_code, str(obj.cepDestino)

    return valid_code

def track(cod):
    if not is_valid(cod):
        return None

    body = '''
   <rastroObjeto>
       <usuario>MobileXect</usuario>
       <senha>DRW0#9F$@0</senha>
       <tipo>L</tipo>
       <resultado>T</resultado>
       <objetos>{obj}</objetos>
       <lingua>101</lingua>
       <token>QTXFMvu_Z-6XYezP3VbDsKBgSeljSqIysM9x</token>
   </rastroObjeto>
   '''

    r = requests.post('http://webservice.correios.com.br/service/rest/rastro/rastroMobile',
                      data=body.format(obj=cod),
                      headers={'Content-Type': 'application/xml'})

    if r.status_code == 200:
        result = r.json().get('objeto', list())
        if result:
            obj = Objeto(**result[0])
            obj.json = r.json()
            return obj

    return None

def is_cod(cod):
    if re.match('^[a-zA-Z]{2}[0-9]{9}[a-zA-Z]{2}$', cod):
        return True
    return False

def is_valid(cod):
    return re.match('^[a-zA-Z]{2}[0-9]{9}[a-zA-Z]{2}$', cod)


import sqlite3 as sql
import json
import os
from datetime import datetime

db = sql.connect('MainScripts/JIRATable.sqlite')
c = db.cursor()
#
# c.execute('drop table if exists JIRAData')

c.execute('''create table if not exists JIRAData (Key text, Status text, Summary text,Reporter text,IssueCreatedOn text, IssueUpdatedOn text, RefreshDate text)''' )
#
ins = 'insert into JIRAData (Key,Status,Summary,Reporter,IssueCreatedOn,IssueUpdatedOn,RefreshDate) values (?,?,?,?,?,?,?)'
#
#
with open('MainScripts/JIRAData.json','r') as f:
    JData = json.load(f)
#
i = 0
#

for r in JData:
    RefreshDate = JData[i]['RefreshDate']
    val=(JData[i]['Key'],JData[i]['Status'],JData[i]['Summary'],JData[i]['Reporter'],JData[i]['IssueCreatedDate'],JData[i]['UpdatedDate'],RefreshDate)
    i = i+1
    c.execute(ins,val)
    print(c.fetchall())

db.commit()

# c.execute('select count(*) From JIRAData')
# res = c.fetchall()

# for row in res:
#     print(row)

# db.close()

# os.remove('JIRATable.sqlite')

# print(JData[0]['Key'])
# print(JData[1]['Key'])


import stanza
import json,re
# # stanza.download('en')       # This downloads the English models for the neural pipeline
nlp = stanza.Pipeline('en') # This sets up a default neural pipeline in English
field = "yugioh"

f = open(field+"_entitys.json")
field_entitys = json.load(f)
f.close()

max_num = 5000

def exist(text,entitys):
    flag = False
    label_title = None
    match_type = None
    if text in entitys:#exact match
        flag = True
        label_title = text
        match_type = "exact match"
        return flag,label_title,match_type
    return flag,label_title,match_type

src = open("./data/zeshel/documents/"+field+".json")
out_f = open("exact_"+field+"_tmp.jsonl",'w')
cnt = 0
succ_cnt = 0
line = src.readline()
finish_flag = False
while line:
    if finish_flag:
        break
    cnt += 1
    if cnt%100==0:
        print(cnt)
    text = json.loads(line)["text"]
    from_doc = json.loads(line)["title"]
    try:
        doc = nlp(text)
    except:
        line = src.readline()
        continue
    mention_list = []
    entity_list = []
    doc_flag = False
    for sentence in doc.sentences:
        if doc_flag:
            break
        for ent in sentence.ents:
            if doc_flag:
                break
            if ent.text in mention_list:
                continue
            if ent.type == "DATE" or ent.type == "CARDINAL" or ent.type == "ORDINAL":#不要日期
                continue
            flag, label_title,match_type = exist(ent.text.lower(),field_entitys)
            if flag:
                if from_doc.lower() == label_title:
                    continue
                start,end = ent.start_char,ent.end_char
                sample = {}
                sample["context_left"] = text[:start]
                if len(sample["context_left"].split())<64:
                    continue
                sample["context_left"] = " ".join(sample["context_left"].split()[-128:])
                sample["mention"] = ent.text
                sample["context_right"] = text[end:]
                sample["context_right"] = " ".join(sample["context_right"].split()[:128])
                sample["label_title"] = label_title
                sample["world"] = field
                sample["from_doc"] = from_doc
                sample["match_type"] = match_type
                out_f.write(json.dumps(sample)+"\n")
                succ_cnt += 1
                if succ_cnt>=max_num:
                    finish_flag = True
                entity_list.append(label_title)
                mention_list.append(ent.text)
                doc_flag = True
    line = src.readline()
src.close()
out_f.close()

f = open(field+"_entity_index_by_title")
entity_index_by_title = json.load(f)
f.close()

out_f = open("exact_"+field+".jsonl",'w')
f = open("exact_"+field+"_tmp.jsonl")
line = f.readline()
while line:
    line = json.loads(line)
    new_line = {}
    new_line["context_left"] = line["context_left"]
    new_line["context_right"] = line["context_right"]
    label_title = line["label_title"]
    new_line["label"] = entity_index_by_title[label_title][0]
    new_line["label_title"] = label_title
    new_line["label_id"] = entity_index_by_title[label_title][1]
    new_line["mention"] = line["mention"]
    new_line["from_doc"] = line["from_doc"]
    new_line["world"] = line["world"]
    new_line["match_type"] = line["match_type"]
    out_f.write(json.dumps(new_line)+"\n")
    line = f.readline()
f.close()
out_f.close()


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

import numpy as np
import librosa
from pathlib import Path

from microphone import record_audio

import matplotlib.mlab as mlab

from os import listdir
from os.path import isfile, join

import song_titles_artists as sta

def load_song_from_path(path: str):
    """
    Loads a song from a path.

    Returns the samples, sampling rate, and duration.

    Parameters
    ------------
    path: str
          The path for the specified audio file

    Returns
    -------
    Tuple[np.ndarray, int, float]
        The list of samples, the sampling rate and the duration of the song
    """
    song_path = Path(path)
    samples, sampling_rate = librosa.load(song_path, sr=None)
    duration = librosa.get_duration(y=samples, sr=sampling_rate)
    samples.astype("float32")
    samples /= np.max(samples)
    samples *= 2**15
    return samples, sampling_rate, duration

def load_music_files(directory: str):
    """
    Loads the files in a folder.

    Returns the samples from each file.

    Parameters
    ------------
    directory: str
          The path for the specified folder

    Returns
    -------
    List
        Names of all of the songs
    np.ndarray size: (M,N)
        The array of samples for each song
    """
    files = sorted([f for f in listdir(directory) if isfile(join(directory, f))])
    music_list = np.array([load_song_from_path(directory + paths)[0] for paths in files])
    id_dict = sta.song_name_to_ID(files)
    ids = [id_dict[title] for title in files]
    return ids, music_list

def return_specgram(samples: np.ndarray, sampling_rate: int):
    """
    Creates the spectrogram for a specific set of samples

    Returns the spectrogram, the frequencies, and the times

    Parameters
    ------------
    samples: np.ndarray
          The array of samples
    sampling_rate: int
          The sampling rate
    Returns
    -------
    Tuple[np.narray, np.narray, np.narray]
        The spectrogram 2d array, the frequencies and the timestamps
    """
    S, freqs, times = mlab.specgram(
        samples,
        NFFT=4096,
        Fs=sampling_rate,
        window=mlab.window_hanning,
        noverlap=int(4096 / 2),
        mode='magnitude'
    )
    return S, freqs, times

def record_sound(time: float):
    """
    Records sound for a specific duration of time

    Returns the frames and the sample rate

    Parameters
    ------------
    time: float
          The duration of the recording in seconds

    Returns
    -------
    Tuple[np.ndarray, int]
        The frames and the sample rate
    """
    listen_time = time  # seconds
    frames, sample_rate = record_audio(listen_time)
    samples = np.empty(0)
    for i in frames:
        samples = np.append(samples, np.frombuffer(i, np.int16))
    samples = samples.astype("float32")
    samples /= np.max(samples)
    samples *= 2 ** 15
    return samples, sample_rate


"""A Flask webapp.

Application factory.

From the ``Flask`` docs:

    Instead of creating a ``Flask`` instance globally, we will create
    it inside a function. This function is known as the application
    factory. Any configuration, registration, and other set up the
    application needs will happen inside the function, then the
    application will be returned.

The app will be passed through to each ``init_app`` declaration, unique
to its respective module. These functions will configure the app
designed in this package.

Registers:

    * This app's commandline interface
    * Config object from config module
    * Exceptions for handling error pages
    * This app's registered ``Flask`` extensions
    * Loggers from logging
    * Blueprints containing routing logic
    * This app's interactive shell
"""
from flask import Flask

from app import cli, config, exceptions, extensions, fs, log, shell, views
from app.version import __version__

def create_app() -> Flask:
    """Create ``Flask`` web application object.

    Accessible through wsgi.py.

    :return: Web application initialized and processed through factory
        pattern.
    """
    app = Flask(__name__)
    config.init_app(app)
    log.init_app(app)
    extensions.init_app(app)
    views.init_app(app)
    exceptions.init_app(app)
    shell.init_app(app)
    cli.init_app(app)
    fs.init_app(app)
    return app

__all__ = ["__version__", "create_app"]


"""Handles different kind of browser streams."""

import copy
import logging
from typing import Dict

import cv2
import imutils
import tornado.ioloop
import tornado.web
from tornado.queues import Queue

from viseron.config.config_camera import MJPEG_STREAM_SCHEMA
from viseron.const import TOPIC_FRAME_PROCESSED, TOPIC_STATIC_MJPEG_STREAMS
from viseron.data_stream import DataStream
from viseron.helpers import draw_contours, draw_mask, draw_objects, draw_zones
from viseron.nvr import FFMPEGNVR

LOGGER = logging.getLogger(__name__)

class StreamHandler(tornado.web.RequestHandler):
    """Represents a stream."""

    async def process_frame(self, nvr: FFMPEGNVR, frame, mjpeg_stream_config):
        """Return JPG with drawn objects, zones etc."""
        if mjpeg_stream_config["width"] and mjpeg_stream_config["height"]:
            resolution = mjpeg_stream_config["width"], mjpeg_stream_config["height"]
            frame.resize(
                "tornado", mjpeg_stream_config["width"], mjpeg_stream_config["height"]
            )
            # TODO move this to a preprocess
            processed_frame = frame.get_preprocessed_frame(
                "tornado"
            ).get()  # Convert to Mat
        else:
            resolution = nvr.camera.resolution
            processed_frame = frame.decoded_frame_mat_rgb

        if mjpeg_stream_config["draw_motion_mask"] and nvr.config.motion_detection.mask:
            draw_mask(
                processed_frame,
                nvr.config.motion_detection.mask,
            )

        if mjpeg_stream_config["draw_motion"] and frame.motion_contours:
            draw_contours(
                processed_frame,
                frame.motion_contours,
                resolution,
                nvr.config.motion_detection.area,
            )

        if mjpeg_stream_config["draw_zones"]:
            draw_zones(processed_frame, nvr.zones)

        if mjpeg_stream_config["draw_objects"]:
            draw_objects(
                processed_frame,
                frame.objects,
                resolution,
            )

        if mjpeg_stream_config["rotate"]:
            processed_frame = imutils.rotate_bound(
                processed_frame, mjpeg_stream_config["rotate"]
            )

        if mjpeg_stream_config["mirror"]:
            processed_frame = cv2.flip(processed_frame, 1)

        # Write a low quality image to save bandwidth
        ret, jpg = cv2.imencode(
            ".jpg", processed_frame, [int(cv2.IMWRITE_JPEG_QUALITY), 100]
        )

        return ret, jpg

class DynamicStreamHandler(StreamHandler):
    """Represents a dynamic stream using query parameters."""

    async def get(self, camera):
        """Handle a GET request."""
        request_arguments = {k: self.get_argument(k) for k in self.request.arguments}
        LOGGER.debug(request_arguments)
        mjpeg_stream_config = MJPEG_STREAM_SCHEMA(request_arguments)
        LOGGER.debug(mjpeg_stream_config)
        nvr = FFMPEGNVR.nvr_list.get(camera, None)

        if not nvr:
            self.set_status(404)
            self.write(f"Camera {camera} not found.")
            self.finish()
            return

        self.set_header(
            "Content-Type", "multipart/x-mixed-replace;boundary=--jpgboundary"
        )
        self.set_header("Connection", "close")

        frame_queue = Queue(maxsize=10)
        frame_topic = f"{nvr.config.camera.name_slug}/{TOPIC_FRAME_PROCESSED}/*"
        unique_id = DataStream.subscribe_data(frame_topic, frame_queue)
        while True:
            try:
                item = await frame_queue.get()
                frame = copy.copy(item.frame)
                ret, jpg = await self.process_frame(nvr, frame, mjpeg_stream_config)

                if ret:
                    self.write("--jpgboundary")
                    self.write("Content-type: image/jpeg\r\n")
                    self.write("Content-length: %s\r\n\r\n" % len(jpg))
                    self.write(jpg.tobytes())
                    await self.flush()
            except tornado.iostream.StreamClosedError:
                DataStream.unsubscribe_data(frame_topic, unique_id)
                LOGGER.debug(f"Stream closed for camera {nvr.config.camera.name_slug}")
                break

class StaticStreamHandler(StreamHandler):
    """Represents a static stream defined in config.yaml."""

    active_streams: Dict[str, object] = {}

    @tornado.gen.coroutine
    def stream(self, nvr, mjpeg_stream, mjpeg_stream_config, publish_frame_topic):
        """Subscribe to frames, draw on them, then publish processed frame."""
        frame_queue = Queue(maxsize=10)
        subscribe_frame_topic = (
            f"{nvr.config.camera.name_slug}/{TOPIC_FRAME_PROCESSED}/*"
        )
        unique_id = DataStream.subscribe_data(subscribe_frame_topic, frame_queue)

        while self.active_streams[mjpeg_stream]:
            item = yield frame_queue.get()
            frame = copy.copy(item.frame)
            ret, jpg = yield self.process_frame(nvr, frame, mjpeg_stream_config)

            if ret:
                DataStream.publish_data(publish_frame_topic, jpg)

        DataStream.unsubscribe_data(subscribe_frame_topic, unique_id)
        LOGGER.debug(f"Closing stream {mjpeg_stream}")

    async def get(self, camera, mjpeg_stream):
        """Handle GET request."""
        nvr = FFMPEGNVR.nvr_list.get(camera, None)
        if not nvr:
            self.set_status(404)
            self.write(f"Camera {camera} not found.")
            self.finish()
            return

        mjpeg_stream_config = nvr.config.camera.static_mjpeg_streams.get(
            mjpeg_stream, None
        )
        if not mjpeg_stream_config:
            self.set_status(404)
            self.write(f"Stream {mjpeg_stream} not defined.")
            self.finish()
            return

        frame_queue = Queue(maxsize=10)
        frame_topic = (
            f"{TOPIC_STATIC_MJPEG_STREAMS}/{nvr.config.camera.name_slug}/{mjpeg_stream}"
        )
        unique_id = DataStream.subscribe_data(frame_topic, frame_queue)

        if self.active_streams.get(mjpeg_stream, False):
            self.active_streams[mjpeg_stream] += 1
            LOGGER.debug(
                "Stream {mjpeg_stream} already active, number of streams: "
                f"{self.active_streams[mjpeg_stream]}"
            )
        else:
            LOGGER.debug(f"Stream {mjpeg_stream} is not active, starting")
            self.active_streams[mjpeg_stream] = 1
            tornado.ioloop.IOLoop.current().spawn_callback(
                lambda: self.stream(
                    nvr, mjpeg_stream, mjpeg_stream_config, frame_topic
                ),
            )

        self.set_header(
            "Content-Type", "multipart/x-mixed-replace;boundary=--jpgboundary"
        )
        self.set_header("Connection", "close")
        while True:
            try:
                jpg = await frame_queue.get()
                self.write("--jpgboundary")
                self.write("Content-type: image/jpeg\r\n")
                self.write("Content-length: %s\r\n\r\n" % len(jpg))
                self.write(jpg.tobytes())
                await self.flush()
            except tornado.iostream.StreamClosedError:
                DataStream.unsubscribe_data(frame_topic, unique_id)
                LOGGER.debug(
                    f"Stream {mjpeg_stream} closed for camera "
                    f"{nvr.config.camera.name_slug}"
                )
                break
        self.active_streams[mjpeg_stream] -= 1


import random
import time
from typing import Set

import schedule

from timmy import core
from timmy.data.war_state import WarState
from timmy.data.word_war import WordWar
from timmy.db_access import word_war_db

class WarTicker:
    def __init__(self):
        self.loaded_wars: Set[WordWar] = set()
        self.active_wars: Set[WordWar] = set()

    def reset_timer(self) -> None:
        schedule.clear('warticker')
        self.__init__()
        self.load_wars()

    def load_wars(self) -> None:
        self.loaded_wars = word_war_db.load_wars()
        schedule.every(1).seconds.do(war_update_loop).tag('warticker')

    def begin_war(self, war: WordWar) -> None:
        current_epoch = time.time()
        late_start = int(current_epoch - war.start_epoch)

        message = "{}: Starting now!".format(war.get_name())

        if late_start >= 5:
            message += " ({:d} seconds late. Sorry!)".format(late_start)

        core.bot_instance.connection.privmsg(war.channel, message)

        self.notify_war_members(war, message)

        war.begin_war()

    def end_war(self, war: WordWar) -> None:
        current_epoch = time.time()
        late_end = int(current_epoch - war.end_epoch)

        message = "{}: Ending now!".format(war.get_name())

        if late_end >= 5:
            message += " ({:d} seconds late. Sorry!)".format(late_end)

        core.bot_instance.connection.privmsg(war.channel, message)

        self.notify_war_members(war, message)

        if war.channel in core.bot_instance.channels:
            core.bot_instance.channels[war.channel].last_war_id = war.get_id()

        if war.current_chain >= war.total_chains:
            war.end_war()
            self.active_wars.remove(war)

            if war.channel in core.bot_instance.channels \
                    and core.bot_instance.channels[war.channel].newest_war_id == war.get_id():
                core.bot_instance.channels[war.channel].newest_war_id = ""
        else:
            war.current_chain += 1

            if war.randomness:
                war.start_epoch = war.end_epoch + war.base_break + (war.base_break * (random.randrange(20) - 10)) / 100
                war.end_epoch = war.start_epoch + war.base_duration + (
                        war.base_duration * (random.randrange(20) - 10)) / 100
            else:
                war.start_epoch = war.end_epoch + war.base_break
                war.end_epoch = war.start_epoch + war.base_duration

            war.start_break()
            self.war_start_count(war)

            if war.channel in core.bot_instance.channels:
                core.bot_instance.channels[war.channel].newest_war_id = war.get_id()

    @staticmethod
    def war_start_count(war: WordWar) -> None:
        time_to_start = int(war.start_epoch - time.time())

        if time_to_start < 60:
            message = "{}: Starting in {:d} {}.".format(
                    war.get_name(), time_to_start, "seconds" if time_to_start > 1 else "second"
            )
        else:
            minutes = time_to_start / 60
            if time_to_start % 60 == 0:
                message = "{}: Starting in {:d} {}.".format(
                        war.get_name(include_duration = True), int(minutes), "minutes" if minutes > 1 else "minute"
                )
            else:
                message = "{}: Starting in {:.1f} minutes.".format(war.get_name(include_duration = True), minutes)

        core.bot_instance.connection.privmsg(war.channel, message)

    @staticmethod
    def war_end_count(war: WordWar) -> None:
        time_to_end = int(war.end_epoch - time.time())

        if time_to_end < 60:
            message = "{}: {:d} {} remaining!".format(
                    war.get_name(), time_to_end, "seconds" if time_to_end > 1 else "second"
            )
        else:
            minutes = time_to_end // 60
            message = "{}: {:d} {} remaining.".format(
                    war.get_name(), minutes, "minutes" if minutes > 1 else "minute"
            )

        core.bot_instance.connection.privmsg(war.channel, message)

    @staticmethod
    def notify_war_members(war: WordWar, message: str) -> None:
        db_id = war.get_id()
        message += f" [ID {db_id}]"

        for nick in war.war_members:
            core.bot_instance.connection.privmsg(nick, message)

def war_update_loop() -> None:
    try:
        from timmy.core import bot_instance
        loaded_wars = core.war_ticker.loaded_wars.copy()

        for war in loaded_wars:
            if war.channel in bot_instance.channels.keys():
                core.war_ticker.active_wars.add(war)
                core.war_ticker.loaded_wars.remove(war)

                bot_instance.channels[war.channel].newest_war_id = war.get_id()

        wars = core.war_ticker.active_wars.copy()
        if wars is None or len(wars) <= 0:
            return

        current_epoch = time.time()

        for war in wars:
            if war.start_epoch >= current_epoch:
                time_difference = int(war.start_epoch - current_epoch)

                if time_difference in [600, 300, 60, 30, 5, 4, 3, 2, 1]:
                    core.war_ticker.war_start_count(war)
                elif time_difference == 0:
                    core.war_ticker.begin_war(war)
                elif time_difference >= 3600:
                    if time_difference % 3600 == 0:
                        core.war_ticker.war_start_count(war)
                elif time_difference >= 1800:
                    if time_difference % 1800 == 0:
                        core.war_ticker.war_start_count(war)
            else:
                if war.end_epoch >= current_epoch:
                    if war.state == WarState.PENDING:
                        core.war_ticker.begin_war(war)
                    else:
                        time_difference = int(war.end_epoch - current_epoch)

                        if time_difference in [600, 300, 60, 5, 4, 3, 2, 1]:
                            core.war_ticker.war_end_count(war)
                        elif time_difference == 0:
                            core.war_ticker.end_war(war)
                        elif time_difference >= 3600:
                            if time_difference % 3600 == 0:
                                core.war_ticker.war_end_count(war)
                        elif time_difference >= 1800:
                            if time_difference % 1800 == 0:
                                core.war_ticker.war_end_count(war)
                else:
                    core.war_ticker.end_war(war)
    except Exception:
        from timmy.utilities import irc_logger
        irc_logger.log_traceback()


import os
import sys
import numpy as np
from collections import OrderedDict

def groupSinglets(comp_name):
    d = { "sm": "SM",
          "lin": "L ",
          "quad": "Q ",
          "lin_mixed": "M ",
          "sm_lin_quad": "SM+L+Q ",
          "quad_mixed": "Q+Q+M ",
          "sm_lin_quad_mixed": "SM+L+L+Q+Q+M ",
          "DATA": "DATA"
        }

    type_ = comp_name.split("_c")[0]
    if type_ in d:
        newName = d[type_]
    else:
        return comp_name

    if type_ != "sm" and type_!= "DATA": #need to account for operators here
        ops = comp_name.split(type_ + "_")[1]
        if len(ops.split("_")) == 2:
            ops = ops.split("_")
            newName += ops[0] + " " + ops[1]
        else:
            newName += ops

    return newName

def isBSM(sample):
    if any(i in sample for i in ["sm_", "quad_", "lin_"]):
        return True
    else:
        return False

def makeStructure(h_dict, model, outdir, isMkDC = True):

    for sample in h_dict:
        if isMkDC:
            first_var = h_dict[sample].keys()[0]
            structure = h_dict[sample][first_var].keys()
        else:
            structure = h_dict[sample]

        file_name = outdir + "/structure_" + sample + "_" + model + ".py"
        f = open(file_name, 'w')

        f.write("#-----------------------------------\n")
        f.write("#     Automatically generated       # \n")
        f.write("#        by mkDCInputs.py           # \n")
        f.write("#-----------------------------------\n")
        f.write("\n\n\n")

        for key in structure:

            isData = 0
            if key == "DATA": isData = 1

            f.write('structure["{}"]  = {} \n'.format(key, "{"))
            f.write("        'isSignal' : 0, \n")
            f.write("        'isData' : {}, \n".format(isData))
            f.write("{}\n".format("}"))
            f.write("\n\n")

        f.close()

# THIS FUNCTION IS TERRIBLE, HOPEFULLY SOMEONE STRONG OF HEART WILL FIX IT
# anyway we do not need the Latinos plot card redineed in order to generate the datacards
def makePlot(h_dict, model, config, outdir, isMkDC = True):

    colors = config.getlist("d_plot", "colors")
    c_types = [i.split(":")[0] for i in colors]
    c_vals = [[int(j) for j in i.split(":")[1:]] for i in colors]

    user_colors = {}
    for i,j in zip(c_types, c_vals):
        user_colors[i] = j

    group = []
    g_colors = []

    if config.has_option("d_plot", "isSignal"):
        isSignal = config.getlist("d_plot", "isSignal")
        comp = [i.split(":")[0] for i in isSignal]
        val = [i.split(":")[1] for i in isSignal]
        isSignal = dict((c,v) for c,v in zip(comp, val))

    else:
        isSignal = {}

    if config.has_option("d_plot", "group"): group = config.getlist("d_plot", "group")
    if config.has_option("d_plot", "g_colors"): g_colors = [int(col) for col in config.getlist("d_plot", "g_colors")]

    for sample in h_dict:
        if isMkDC:
            first_var = h_dict[sample].keys()[0]
            structure = h_dict[sample][first_var].keys()
        else:
            structure = h_dict[sample]

        ops = sample.split("_")[1:]

        cd = {}

        for key in user_colors.keys():
            if key != "sm" and key not in config.getlist("variables", "makeDummy"):
                if len(user_colors[key]) == len(ops):
                    for j,op in enumerate(ops):
                        cd[key + "_" + op] = user_colors[key][j]

                if len(user_colors[key]) < len(ops):
                    if key + "_" + "_".join(op for op in ops) in structure:
                        cd[key + "_" + "_".join(op for op in ops)] = user_colors[key][0]
                    else:
                        cd[key + "_" + "_".join(op for op in ops[::-1])] = user_colors[key][0]

                if len(user_colors[key]) > len(ops):
                    for j,op in enumerate(ops):
                        cd[key + "_" + op] = user_colors[key][j]
            else:
                cd[key] = user_colors[key][0]

        for key in config.getlist("variables", "makeDummy"):
            if key not in user_colors.keys():
                cd[key] = 1

        file_name = outdir + "/plot_" + sample + "_" + model + ".py"
        f = open(file_name, 'w')

        f.write("#-----------------------------------\n")
        f.write("#     Automatically generated       # \n")
        f.write("#        by mkDCInputs.py           # \n")
        f.write("#-----------------------------------\n")
        f.write("\n\n\n")

        for idx,g_ in enumerate(group):

            group_these = {}

            if  g_.split(":")[1] == "BSM":

                g_name = g_.split(":")[0]
                if g_name == "model": g_name = model

                group_these[g_name] = {}

                if g_name in isSignal.keys(): sig_val = isSignal[g_name]
                else: sig_val = 2

                group_these[g_name]['nameHR'] = "'{}'".format(g_name)
                group_these[g_name]['isSignal'] = sig_val
                group_these[g_name]['color'] = g_colors[idx]
                group_these[g_name]['samples'] = []
                components = h_dict[sample][h_dict[sample].keys()[0]].keys() #they are equal forall variables

                for comp in components:
                    if isBSM(comp): group_these[g_name]['samples'].append(comp)

            elif g_.split(":")[1] == "all":
                for i,key in enumerate(structure):
                    if key != "DATA":
                        leg_name = groupSinglets(key)
                        group_these[key] = {}

                        if key in isSignal.keys(): sig_val = isSignal[key]
                        else: sig_val = 2

                        # thisif else are ridicolous now
                        if key not in cd.keys(): cd[key] = 1

                        group_these[key]['nameHR'] = "'{}'".format(leg_name)
                        group_these[key]['isSignal'] = sig_val
                        group_these[key]['color'] = cd[key]
                        group_these[key]['samples'] = [key]

            else:
                g_name = g_.split(":")[0]
                g_list = [str(i) for i in (g_.split(":")[1])[1:-1].split(" ")]

                group_these[g_name] = {}

                if g_name in isSignal.keys(): sig_val = isSignal[g_name]
                else: sig_val = 2

                group_these[g_name]['nameHR'] = "'{}'".format(g_.split(":")[0])
                group_these[g_name]['isSignal'] = sig_val
                group_these[g_name]['color'] = g_colors[idx]
                group_these[g_name]['samples'] = []

                components = h_dict[sample][h_dict[sample].keys()[0]].keys() #they are equal forall variables

                for comp in g_list:
                    if comp not in components:
                        sys.exit("[ERROR] The sample {} specified for grouping into {} does not exists ...".format(comp, g_name))

                    group_these[g_name]['samples'].append(comp)

            if len(group_these.keys()) != 0:

                #sort the dict to allow right plotting
                group_these = OrderedDict(sorted(group_these.items(), key=lambda t: t[1]["isSignal"], reverse=True))

                for key in group_these.keys():
                    f.write('groupPlot["{}"]  = {} \n'.format(key, "{"))
                    for subkey in group_these[key]:
                        if subkey != 'samples':
                            f.write("        '{}' : {}, \n".format(subkey, group_these[key][subkey]))
                        else:
                            write_list = "["
                            for s in group_these[key][subkey]:
                                write_list += "'{}'".format(s) + ","

                            write_list = write_list[:-1] + "]"
                            f.write("        '{}' : {}, \n".format(subkey, write_list))

                    f.write("{}\n".format("}"))
                    f.write("\n\n")

        for i,key in enumerate(structure):

            if key in isSignal.keys(): sig_val = isSignal[key]
            else: sig_val = 2

            isData = 0
            if key == "DATA":
                isData = 1

            color = 1
            if key in cd:
                color = cd[key]

            if i > len(cd.keys()): sys.exit("[ERROR]: Colors not sufficient, add more...")

            f.write('plot["{}"]  = {} \n'.format(key, "{"))
            f.write("        'color' : {}, \n".format(cd[key]))
            f.write("        'isSignal' : {}, \n".format(sig_val))
            f.write("        'isData' : {}, \n".format(isData))
            f.write("        'scale' : 1, \n")

            if key == "DATA":
                f.write("        'isBlind' : 1, \n") #default blinding on data

            f.write("{}\n".format("}"))
            f.write("\n\n")

        f.close()

def makeVariables(h_dict, model, config, outdir):

    xaxis_ = config.getlist("d_variables", "xaxis")
    name_ = config.getlist("d_variables", "name")
    range_ = config.getlist("d_variables", "range")
    fold_ = config.getlist("d_variables", "fold")

    for sample in h_dict:
        vars_ = h_dict[sample].keys()

        bl = len(vars_)
        #if not all(len(lst) == bl for lst in [xaxis_, name_, range_, fold_]):

        if xaxis_[0] == "auto": xaxis_ = dict((i,j) for i,j in zip(vars_, vars_))
        elif len(xaxis_) == len(vars_):
            tn = config.getlist("variables", "treenames")
            xaxis_ = dict((i,j) for i,j in zip(tn, xaxis_))
        else:
            sys.exit("[ERROR] xaxis name do not match variables, check inputs in cfg ...")

        if name_[0] == "auto": name_ = dict((i,j) for i,j in zip(vars_, vars_))
        elif len(name_) == len(vars_):
            tn = config.getlist("variables", "treenames")
            name_ = dict((i,j) for i,j in zip(tn, name_))
        else:
            sys.exit("[ERROR] names do not match variables, check inputs in cfg ...")

        if fold_[0] == "auto": fold_ = dict((i,0) for i in vars_)
        elif len(fold_) == len(vars_):
            tn = config.getlist("variables", "treenames")
            fold_ = dict((i,j) for i,j in zip(tn, fold_))
        else:
            sys.exit("[ERROR] folds do not match variables, check inputs in cfg ...")

        if range_[0] == "auto":
            tn = config.getlist("variables", "treenames")
            range_ = dict.fromkeys(tn)
            bins = [int(i) for i in config.getlist("variables", "bins")]
            ranges = [i[1:-1].split(":") for i in config.getlist("variables", "xrange")]
            ranges = [list(map(float, sublist)) for sublist in ranges]

            for k,b,r in zip(range_.keys(), bins, ranges):
                range_[k] = {'bins': b, 'range': [r[0], r[1]]}

        elif len(range_) == len(vars_):
            tn = config.getlist("variables", "treenames")
            range_ = dict((i,j) for i,j in zip(tn, range_))
        else:
            sys.exit("[ERROR] ranges do not match variables, check inputs in cfg ...")

        file_name = outdir + "/variables_" + sample + "_" + model + ".py"
        f = open(file_name, 'w')

        f.write("#-----------------------------------\n")
        f.write("#     Automatically generated       # \n")
        f.write("#        by mkDCInputs.py           # \n")
        f.write("#-----------------------------------\n")
        f.write("\n\n\n")

        for var, xa, name, ra, fold in zip(vars_, xaxis_, name_, range_, fold_):

            f.write('variables["{}"]  = {} \n'.format(var, "{"))
            f.write("        'name' : '{}', \n".format(name_[var]))
            f.write("        'range' : ({},{},{}), \n".format(range_[var]['bins'], range_[var]['range'][0], range_[var]['range'][1]))
            f.write("        'xaxis' : {}, \n".format(xaxis_[var]))
            f.write("        'fold' : {}, \n".format(fold_[var]))
            f.write("{}\n".format("}"))
            f.write("\n\n")

        f.close()

def makeSamples(h_dict, model, config, outdir, isMkDC = True):

    for sample in h_dict:
        if isMkDC:
            first_var = h_dict[sample].keys()[0]
            structure = h_dict[sample][first_var].keys()
        else:
            structure = h_dict[sample]

        file_name = outdir + "/samples_" + sample + "_" + model + ".py"
        f = open(file_name, 'w')

        f.write("#-----------------------------------\n")
        f.write("#     Automatically generated       # \n")
        f.write("#        by mkDCInputs.py           # \n")
        f.write("#-----------------------------------\n")
        f.write("\n\n\n")

        f.write("import os \n")
        f.write("import inspect \n")
        f.write("configurations = os.path.realpath(inspect.getfile(inspect.currentframe())) # this file \n")
        f.write("configurations = os.path.dirname(configurations) \n\n")
        f.write("from LatinoAnalysis.Tools.commonTools import getSampleFiles, getBaseW, addSampleWeight\n\n")

        #Samples declaration
        f.write("# samples\n\n")
        f.write("try:\n")
        f.write("   len(samples)\n")
        f.write("except NameError:\n")
        f.write("   import collections\n")
        f.write("   samples = collections.OrderedDict()")
        f.write("\n\n")

        names = config.getlist("d_samples", "name")
        w = config.getlist("d_samples", "weight")
        ws = config.getlist("d_samples", "weights")
        fxj = config.getlist("d_samples", "filesperjob")

        if len(names) == len(w) == len(ws) == len(fxj) == 1:
            names = names*len(structure)
            w = w*len(structure)
            ws = ws*len(structure)
            fxj = fxj*len(structure)

        elif len(names) == len(w) == len(ws) == len(fxj) != len(structure):
            sys.exit("[ERROR] While making sample, provide a list of parameters = to number of EFT component \
                        or only one value (repeated). Nothing inbetween ...")
        else:
            sys.exit("[ERROR] While making sample, provide a list of parameters = to number of EFT component \
                        or only one value (repeated). Nothing inbetween ...")

        for i,key in enumerate(structure):

            f.write('samples["{}"]  = {} \n'.format(key, "{"))
            f.write("        'name' : {}, \n".format(names[i]))
            f.write("        'weight' : {}, \n".format(w[i]))
            f.write("        'weights' : {}, \n".format(ws[i]))
            f.write("        'isData' : 0, \n")
            f.write("        'FilesPerJob' : {}, \n".format(fxj[i]))
            f.write("{}\n".format("}"))
            f.write("\n\n")

        f.close()

def makeConfiguration(h_dict, model, config, outdir):

    for sample in h_dict:

        file_name = outdir + "/configuration_" + sample + "_" + model + ".py"

        write_out = {}

        write_out["tag"] = config.get("d_configuration", "tag")

        aliasesFile = config.get("d_configuration", "aliasesFile")
        if aliasesFile == "auto":
            aliasesFile = "aliases_" + sample + "_" + model + ".py"
        write_out["aliasesFile"] = aliasesFile

        variablesFile = config.get("d_configuration", "variablesFile")
        if variablesFile == "auto":
            variablesFile = "variables_" + sample + "_" + model + ".py"
        write_out["variablesFile"] = variablesFile

        cutsFile = config.get("d_configuration", "cutsFile")
        if cutsFile == "auto":
            cutsFile = "cuts_" + sample + "_" + model + ".py"
        write_out["cutsFile"] = cutsFile

        samplesFile = config.get("d_configuration", "samplesFile")
        if samplesFile == "auto":
            samplesFile = "samples_" + sample + "_" + model + ".py"
        write_out["samplesFile"] = samplesFile

        plotFile = config.get("d_configuration", "plotFile")
        if plotFile == "auto":
            plotFile = "plot_" + sample + "_" + model + ".py"
        write_out["plotFile"] = plotFile

        structureFile = config.get("d_configuration", "structureFile")
        if structureFile == "auto":
            structureFile = "structure_" + sample + "_" + model + ".py"
        write_out["structureFile"] = structureFile

        nuisancesFile = config.get("d_configuration", "nuisancesFile")
        if nuisancesFile == "auto":
            nuisancesFile = "nuisances_" + sample + "_" + model + ".py"
        write_out["nuisancesFile"] = nuisancesFile

        write_out["lumi"] = config.get("d_configuration", "lumi")
        write_out["outputDirPlots"] = config.get("d_configuration", "outputDirPlots")
        write_out["outputDirDatacard"] = config.get("d_configuration", "outputDirDatacard")

        f = open(file_name, 'w')

        f.write("#-----------------------------------\n")
        f.write("#     Automatically generated       # \n")
        f.write("#        by mkDCInputs.py           # \n")
        f.write("#-----------------------------------\n")
        f.write("\n\n\n")

        for key, value in write_out.items():
            if type(value) == str:
                f.write("{} = '{}' \n\n".format(key, value))
            else:
                f.write("{} = {} \n\n".format(key, value))

        f.close()

def makeAliases(h_dict, model, outdir):

    for sample in h_dict:

        file_name = outdir + "/aliases_" + sample + "_" + model + ".py"
        f = open(file_name, 'w')

        f.write("#-----------------------------------\n")
        f.write("#     Automatically generated       # \n")
        f.write("#        by mkDCInputs.py           # \n")
        f.write("#-----------------------------------\n")
        f.write("\n\n\n")

        f.write('aliases["inclusive"] = {} \n'.format("{"))
        f.write("      'expr': 0 == 0'\n".format("{"))
        f.write("{}\n".format("}"))
        f.write("\n\n")

        f.close()

def makeCuts(h_dict, model, outdir):

    for sample in h_dict:

        file_name = outdir + "/cuts_" + sample + "_" + model + ".py"
        f = open(file_name, 'w')

        f.write("#-----------------------------------\n")
        f.write("#     Automatically generated       # \n")
        f.write("#        by mkDCInputs.py           # \n")
        f.write("#-----------------------------------\n")
        f.write("\n\n\n")

        f.write("cuts['{}'] = {} \n".format(sample, "{"))
        f.write("  'expr': 'inclusive', \n")
        f.write("{}\n".format("}"))
        f.write("\n\n")

        f.close()

def whatNuis(comp):

    test = np.array(["sm", "lin", "quad", "mixed"])
    base = np.array(comp.split("_")) #sm #lin #quad #

    if base.size == 1: return base #only identical sm has 1 len

    mask = np.isin(base, test)
    c = base[mask]
    ops = np.setdiff1d(base, c)

    #so finally this component
    #receives nuis contributions from these

    final = [mod + "_" + op  for mod in c if mod != "sm" for op in ops]
    if "sm" in c:
        final.append("sm")

    return final

def propagateNuis(h_dict, nuis_dict):

    var = h_dict.keys()[0]
    s_int = h_dict[var].keys()

    for key_name in nuis_dict.keys():
        samples_dict = nuis_dict[key_name]['samples']

        for sam in samples_dict.keys():
            sam_nuis_prop = 0
            c = whatNuis(sam)
            sample_nuis = samples_dict[sam] - 1

            #propagation
            if sam in s_int:
                #comp_yield = float('%.4f'%h_dict[var][sam].Integral())
                comp_yield = h_dict[var][sam].Integral()
            else: continue

            for basic_component in c:
                if basic_component in s_int and basic_component in samples_dict.keys():
                    #yield_ = float('%.4f'%h_dict[var][basic_component].Integral())
                    yield_ = h_dict[var][basic_component].Integral()

                    sam_nuis_prop += (yield_ * samples_dict[basic_component]) / comp_yield

            nuis_dict[key_name]['samples'][sam] = sam_nuis_prop

    return nuis_dict

# def switchNuis(comp_1, nuis_comp_1, comp_2):
#     #this stands also if the component is not sm
#     #print("sigma_{} = ({}-1) * {}/{} + 1 = {}".format("2", nuis_comp_1, comp_1, comp_2, (nuis_comp_1 - 1) * float(comp_1)/comp_2 + 1))
#     return (nuis_comp_1 - 1) * float(comp_1)/comp_2 + 1

# def propagateNuis(h_dict, nuis_dict):

#     #only lnN nuisances can be propagated
#     #checks are made

#     for nuis_name in nuis_dict.keys():
#         if nuis_dict[nuis_name]['type'] == "lnN":
#             if len(nuis_dict[nuis_name]['samples'].keys()) > 1:
#                 sys.exit("[ERROR] Cannot propagate more than one nuisance, there is \
#                 ambiguity... Please insert only one component for each nuisance and it will be propagated")

#             for sample in nuis_dict[nuis_name]['samples'].keys():

#                 #cerco questo oggetto nella dict degli histo
#                 comp_yield = 0
#                 comp_nuis = nuis_dict[nuis_name]['samples'][sample]
#                 for var in h_dict.keys():
#                     compnames = h_dict[var].keys()
#                     for j in compnames:
#                         if j == sample:
#                             #We do this because mkDatacards saves only the first 4 decimal places
#                             #in the rate. Without this the models are distorted... Not nice but still..
#                             #card.write(''.join(('%-.4f' % yieldsSig[name]) line 240
#                             comp_yield = float('%.4f'%h_dict[var][j].Integral())
#                     # propagate to other components having
#                     # the nuis name in their name

#                     for cn in compnames:
#                         if (sample in cn) and sample != cn:
#                             comp2_yield = float('%.4f'%h_dict[var][cn].Integral())
#                             #print(cn, comp2_yield)
#                             comp2_nuis = switchNuis(comp_yield, comp_nuis, comp2_yield)
#                             #print(comp2_nuis)

#                             nuis_dict[nuis_name]['samples'][cn] = comp2_nuis

#     #print(nuis_dict)
#     return nuis_dict

def check_Nuisances(nuis_dict, h_dict):

    for nuis_name in nuis_dict.keys():
        for sample in nuis_dict[nuis_name]['samples'].keys():

            for sam in h_dict.keys():
                for var in h_dict[sam].keys():
                    compnames = h_dict[sam][var].keys()
                    #check if the nuis same is present in at least one component
                    #for each variable
                    if not any([sample in i for i in compnames]):
                        return False
    return True

def makeNuisDict(config, d_name_, name_, type_, samples_, components):

    n_d = {}

    if len(samples_) == 1 and samples_[0][0].split(":")[0] == "all":
        val = float(samples_[0][0].split(":")[1])
        t = type_[0]
        dn = d_name_[0]
        n = name_[0]
        n_d[dn] = {}
        n_d[dn]['name'] = n
        n_d[dn]['type'] = t
        n_d[dn]['samples'] = {}
        for comp in components:
            if comp not in config.getlist("variables", "makeDummy"):
                n_d[dn]['samples'][comp] = val

        return n_d

    for dn, n, t, s in zip(d_name_, name_, type_, samples_):
        n_d[dn] = {}
        n_d[dn]['name'] = n
        n_d[dn]['type'] = t
        n_d[dn]['samples'] = {}

        for kv in s:
            comp = kv.split(":")[0]
            val = float(kv.split(":")[1])
            n_d[dn]['samples'][comp] = val

    return n_d

def makeNuisances(h_dict, model, config, outdir, isMkDC = True):

    #THIS PART IS NOT PERFECT
    #CAN WORK IF THE NUISANCE IS ONLY ON SM
    #DID NOT CHECK FOR OTHER SCENARIOS

    defname = config.getlist("d_nuisances", "defname") #the name in dict key
    name = config.getlist("d_nuisances", "name") # the 'name' field
    samples = [i[1:-1].split("|") for i in config.getlist("d_nuisances", "samples")]
    samples = [list(map(str, sublist)) for sublist in samples]
    types = config.getlist("d_nuisances", "types")

    for sample in h_dict:

        if isMkDC: components = h_dict[sample][h_dict[sample].keys()[0]].keys()
        else: components = h_dict[sample]

        nd = makeNuisDict(config, defname, name, types, samples, components)

        if isMkDC:
            if not check_Nuisances(nd, h_dict):
                sys.exit("[ERROR] Nuisances specified in cfg file are not present in components dict ... Check inputs")

        if config.get("d_nuisances", "propagate") == "True":
            #
            # HORRIBLE FIX FOR 1D NUIS
            #

            nd = propagateNuis(h_dict[sample], nd)

        file_name = outdir + "/nuisances_" + sample + "_" + model + ".py"
        f = open(file_name, 'w')

        f.write("#-----------------------------------\n")
        f.write("#     Automatically generated       # \n")
        f.write("#        by mkDCInputs.py           # \n")
        f.write("#-----------------------------------\n")
        f.write("\n\n\n")

        for key in nd.keys():
            f.write("nuisances['{}'] = {} \n".format(key, "{"))
            f.write("        'name' : '{}', \n".format(nd[key]['name']))
            f.write("        'type' : '{}', \n".format(nd[key]['type']))
            f.write("        'samples': {} \n".format("{"))

            for sample in nd[key]['samples']:
                f.write("            '{}' : '{}', \n".format(sample, nd[key]['samples'][sample]))
            f.write("        {} \n".format("}"))

            f.write("{}\n".format("}"))
            f.write("\n\n")

        f.close()


import jittor as jt
from jittor import init
import math
from os.path import join as pjoin
from collections import OrderedDict
from jittor import nn

def np2th(weights, conv=False):
    'Possibly convert HWIO to OIHW.'
    if conv:
        weights = weights.transpose([3, 2, 0, 1])

    return jt.float32(weights)

class StdConv2d(nn.Conv):

    def execute(self, x):
        w = self.weight
        # (v, m) = torch.var_mean(w, dim=[1, 2, 3], keepdim=True, unbiased=False)
        m = jt.mean(w, dims=(1, 2, 3), keepdims=True)
        v = jt.mean((w - m) ** 2, dims=(1,2,3), keepdims=True)
        w = ((w - m) / jt.sqrt((v + 1e-05)))
        return nn.conv2d(x, w, self.bias, self.stride, self.padding, self.dilation, self.groups)

def conv3x3(cin, cout, stride=1, groups=1, bias=False):
    return StdConv2d(cin, cout, kernel_size=3, stride=stride, padding=1, bias=bias, groups=groups)

def conv1x1(cin, cout, stride=1, bias=False):
    return StdConv2d(cin, cout, kernel_size=1, stride=stride, padding=0, bias=bias)

class PreActBottleneck(nn.Module):
    'Pre-activation (v2) bottleneck block.\n    '

    def __init__(self, cin, cout=None, cmid=None, stride=1):
        super().__init__()
        cout = (cout or cin)
        cmid = (cmid or (cout // 4))
        self.gn1 = nn.GroupNorm(32, cmid, eps=1e-06, affine=None)
        self.conv1 = conv1x1(cin, cmid, bias=False)
        self.gn2 = nn.GroupNorm(32, cmid, eps=1e-06, affine=None)
        self.conv2 = conv3x3(cmid, cmid, stride, bias=False)
        self.gn3 = nn.GroupNorm(32, cout, eps=1e-06, affine=None)
        self.conv3 = conv1x1(cmid, cout, bias=False)
        self.relu = nn.ReLU()
        if ((stride != 1) or (cin != cout)):
            self.downsample = conv1x1(cin, cout, stride, bias=False)
            self.gn_proj = nn.GroupNorm(cout, cout, affine=None)

    def execute(self, x):
        residual = x
        if hasattr(self, 'downsample'):
            residual = self.downsample(x)
            residual = self.gn_proj(residual)
        y = nn.relu(self.gn1(self.conv1(x)))
        y = nn.relu(self.gn2(self.conv2(y)))
        y = self.gn3(self.conv3(y))
        y = nn.relu((residual + y))
        return y

    def load_from(self, weights, n_block, n_unit):
        conv1_weight = np2th(weights[pjoin(n_block, n_unit, 'conv1/kernel')], conv=True)
        conv2_weight = np2th(weights[pjoin(n_block, n_unit, 'conv2/kernel')], conv=True)
        conv3_weight = np2th(weights[pjoin(n_block, n_unit, 'conv3/kernel')], conv=True)
        gn1_weight = np2th(weights[pjoin(n_block, n_unit, 'gn1/scale')])
        gn1_bias = np2th(weights[pjoin(n_block, n_unit, 'gn1/bias')])
        gn2_weight = np2th(weights[pjoin(n_block, n_unit, 'gn2/scale')])
        gn2_bias = np2th(weights[pjoin(n_block, n_unit, 'gn2/bias')])
        gn3_weight = np2th(weights[pjoin(n_block, n_unit, 'gn3/scale')])
        gn3_bias = np2th(weights[pjoin(n_block, n_unit, 'gn3/bias')])
        # self.conv1.weight.copy_(conv1_weight)
        # self.conv2.weight.copy_(conv2_weight)
        # self.conv3.weight.copy_(conv3_weight)
        # self.gn1.weight.copy_(gn1_weight.view((- 1)))
        # self.gn1.bias.copy_(gn1_bias.view((- 1)))
        # self.gn2.weight.copy_(gn2_weight.view((- 1)))
        # self.gn2.bias.copy_(gn2_bias.view((- 1)))
        # self.gn3.weight.copy_(gn3_weight.view((- 1)))
        # self.gn3.bias.copy_(gn3_bias.view((- 1)))
        self.conv1.weight = (conv1_weight)
        self.conv2.weight = (conv2_weight)
        self.conv3.weight = (conv3_weight)
        self.gn1.weight = (gn1_weight.view((- 1)))
        self.gn1.bias = (gn1_bias.view((- 1)))
        self.gn2.weight = (gn2_weight.view((- 1)))
        self.gn2.bias = (gn2_bias.view((- 1)))
        self.gn3.weight = (gn3_weight.view((- 1)))
        self.gn3.bias = (gn3_bias.view((- 1)))
        if hasattr(self, 'downsample'):
            proj_conv_weight = np2th(weights[pjoin(n_block, n_unit, 'conv_proj/kernel')], conv=True)
            proj_gn_weight = np2th(weights[pjoin(n_block, n_unit, 'gn_proj/scale')])
            proj_gn_bias = np2th(weights[pjoin(n_block, n_unit, 'gn_proj/bias')])
            # self.downsample.weight.copy_(proj_conv_weight)
            # self.gn_proj.weight.copy_(proj_gn_weight.view((- 1)))
            # self.gn_proj.bias.copy_(proj_gn_bias.view((- 1)))
            self.downsample.weight = (proj_conv_weight)
            self.gn_proj.weight = (proj_gn_weight.view((- 1)))
            self.gn_proj.bias = (proj_gn_bias.view((- 1)))

class ResNetV2(nn.Module):
    'Implementation of Pre-activation (v2) ResNet mode.'

    def __init__(self, block_units, width_factor):
        super().__init__()
        width = int((64 * width_factor))
        self.width = width
        self.root = nn.Sequential(OrderedDict([('conv', StdConv2d(3, width, kernel_size=7, stride=2, bias=False, padding=3)), ('gn', nn.GroupNorm(32, width, eps=1e-06, affine=None)), ('relu', nn.ReLU())]))
        self.body = nn.Sequential(OrderedDict([('block1', nn.Sequential(OrderedDict(([('unit1', PreActBottleneck(cin=width, cout=(width * 4), cmid=width))] + [(f'unit{i:d}', PreActBottleneck(cin=(width * 4), cout=(width * 4), cmid=width)) for i in range(2, (block_units[0] + 1))])))), ('block2', nn.Sequential(OrderedDict(([('unit1', PreActBottleneck(cin=(width * 4), cout=(width * 8), cmid=(width * 2), stride=2))] + [(f'unit{i:d}', PreActBottleneck(cin=(width * 8), cout=(width * 8), cmid=(width * 2))) for i in range(2, (block_units[1] + 1))])))), ('block3', nn.Sequential(OrderedDict(([('unit1', PreActBottleneck(cin=(width * 8), cout=(width * 16), cmid=(width * 4), stride=2))] + [(f'unit{i:d}', PreActBottleneck(cin=(width * 16), cout=(width * 16), cmid=(width * 4))) for i in range(2, (block_units[2] + 1))]))))]))

    def execute(self, x):
        features = []
        (b, c, in_size, _) = x.shape
        x = self.root(x)
        features.append(x)
        x = nn.Pool(3, stride=2, padding=0, op='maximum')(x)
        for i in range((len(self.body) - 1)):
            x = self.body[i](x)
            right_size = int(((in_size / 4) / (i + 1)))
            if (x.shape[2] != right_size):
                pad = (right_size - x.shape[2])
                assert ((pad < 3) and (pad > 0)), 'x {} should {}'.format(x.shape, right_size)
                # feat = jt.zeros((b, x.shape[1], right_size, right_size), device=x.device)
                feat = jt.zeros((b, x.shape[1], right_size, right_size))
                feat[:, :, 0:x.shape[2], 0:x.shape[3]] = x[:]
            else:
                feat = x
            features.append(feat)
        x = self.body[(- 1)](x)
        return (x, features[::(- 1)])


from django.core.management.base import BaseCommand
from django.utils.crypto import get_random_string
from django.utils import timezone
from user_paste.models import User, Post
import datetime
import string

class Command(BaseCommand):
    help = 'Generates fake data for a local sqlite database'

    def add_arguments(self, parser):
        parser.add_argument('--num_users', type=int, required=True)
        parser.add_argument('--num_user_posts', type=int, required=True)

    def handle(self, *args, **options):
        created_date = timezone.now() - datetime.timedelta(weeks=100)
        for i in range(options['num_users']):
            user_name = get_random_string(20, string.ascii_letters + string.digits)
            user = User.objects.create(user_name=user_name)
            for i in range(options['num_user_posts']):
                created_date += datetime.timedelta(days=1)
                post_content = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
                                  Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
                                  Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
                                  Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'''
                Post(post_title = user.user_name + f'post{i}',
                        post_author = user,
                        post_content = post_content,
                        post_description = 'Lorem ipsum dolor sit amet, consectetur adipiscing',
                        post_category = 'Plain Text',
                        post_type = 'Notes',
                        post_created_date = created_date).save()


import pytesseract
import os
import sys
from PIL import Image
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfgen import canvas
from reportlab.lib.units import cm
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.pagesizes import A4
import fitz
import shutil

from PyQt5.QtGui     import *
from PyQt5.QtCore    import *
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import QMessageBox

class Form(QMainWindow):
    def __init__(self,parent=None):
        super().__init__(parent)

        getFileNameButton = QPushButton("Выбрать файл")
        getFileNameButton.clicked.connect(self.getFileName)
        getFileNameButton.setFixedSize(160,160)

        layoutV = QVBoxLayout()
        layoutV.addWidget(getFileNameButton)

        layoutH = QHBoxLayout()
        layoutH.addLayout(layoutV)

        centerWidget = QWidget()
        centerWidget.setLayout(layoutH)
        self.setCentralWidget(centerWidget)

        self.resize(200,200)
        self.setWindowTitle("PdfReader")

    def getFileName(self):
        filename, filetype = QFileDialog.getOpenFileName(self,
                             "Выбрать файл",
                             ".",
                             "PDF Files(*.pdf)")

        pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

        pdf_document = fitz.open(filename)

        try:
            os.mkdir("imagepdf")
        except OSError:
            None
        os.chdir("imagepdf")

        for current_page in range(len(pdf_document)):
            for image in pdf_document.getPageImageList(current_page):
                xref = image[0]
                pix = fitz.Pixmap(pdf_document, xref)
                if pix.n < 5:        # this is GRAY or RGB
                    pix.writePNG("%s.png" % (current_page))
                else:                # CMYK: convert to RGB first
                    pix1 = fitz.Pixmap(fitz.csRGB, pix)
                    pix1.writePNG("%s.png" % (current_page))
                    pix1 = None
                pix = None
        os.chdir('..')

        c = canvas.Canvas("Результат.pdf", pagesize=A4)

        for k in range(len(pdf_document)):
            os.chdir("imagepdf")
            img = Image.open(str(k) + '.png')
            os.chdir('..')

            custom_config = r'--oem 3 --psm 6'

            text = pytesseract.image_to_string(img, lang='rus', config= custom_config)

            with open('Text.txt', 'w', encoding="cp1251") as text_file:
                text_file.write(text)

            sumtext = sum(1 for line in open('Text.txt'))
            l = []
            pdfmetrics.registerFont(TTFont('FreeSans', 'FreeSans.ttf'))
            c.setFont('FreeSans', 14)
            i = 29.0
            with open('Text.txt', 'r+') as sumtext:
                for l in sumtext:
                    i = i - 0.5
                    c.drawString(0.5 * cm, i * cm, l.rstrip())
            if os.path.isfile('Text.txt'):
                os.remove('Text.txt')
            else: print("File doesn't exists!")
            c.showPage()
            m = k + 1

            if m == len(pdf_document):
                QMessageBox.about(self, "Выполнено", "Выполнено")
                c.save()

        shutil.rmtree("imagepdf") #удалить директорию с фото

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Form()
    ex.show()
    sys.exit(app.exec_())


# coding:utf-8
"""
@file: .py
@author: dannyXSC
@ide: PyCharm
@createTime: 2022年05月04日 21点47分
@Function: 请描述这个py文件的作用
"""
from Modal.Affiliation import Affiliation
from py2neo import Graph, NodeMatcher, Node

class AffiliationRepo:
    label = "Affiliation"

    def __init__(self):
        pass

    @staticmethod
    def create_affiliation_check(graph: Graph, affiliation: Affiliation):
        node_matcher = NodeMatcher(graph)
        node = node_matcher.match(AffiliationRepo.label, name=affiliation.name).first()
        if node is None:
            node = AffiliationRepo.create_affiliation(graph, affiliation)
        return node

    @staticmethod
    def create_affiliation(graph: Graph, affiliation: Affiliation):
        node = Node(AffiliationRepo.label, name=affiliation.name)
        graph.create(node)
        graph.push(node)
        return node

    @staticmethod
    def get_affiliation_by_name(graph: Graph, name: str) -> Node:
        node_matcher = NodeMatcher(graph)
        return node_matcher.match(AffiliationRepo.label, name=name).first()

    @staticmethod
    def get_all_affiliation_dict(graph: Graph) -> dict:
        cql = "match (n:Affiliation) return (n);"
        nodes = [x["n"] for x in graph.run(cql).data()]
        return {x["name"]: x for x in nodes}

    @staticmethod
    def get_all_affiliation_name(graph: Graph) -> set:
        cql = "match (n:Affiliation) return (n.name);"
        return set(x["(n.name)"] for x in graph.run(cql).data())

    @staticmethod
    def to_neo4j_node(affiliation: Affiliation):
        return Node(AffiliationRepo.label, **affiliation.to_dict())


from projects.models import *
from communities.models import *
from users.models import MossaicUser
from risk_models.models import *

from django import forms

from django.forms.models import inlineformset_factory
from django.forms.models import modelformset_factory
from django.forms.models import BaseInlineFormSet
from django.forms import ModelForm, Textarea
from django.forms.widgets import HiddenInput

class NewModelForm(forms.Form):
	project_name = forms.CharField()

class NewMMLForm(forms.Form):
	new_links = forms.CharField(widget=forms.TextInput(attrs={'class':'tokenized ajaxurl-ajax-metrics'}))

class MetricForm(ModelForm):
	class Meta:
		model = Metric
		widgets = {
			'project': HiddenInput,
			# 'metricType': RadioSelect,
		}

class MCScoreForm(ModelForm):
	class Meta:
		model = MCScore
		widgets = {
			'option': HiddenInput,
			'modelMetricLink': HiddenInput,
		}

MCScoreFormSet = inlineformset_factory(ModelMetricLink,MCScore,form=MCScoreForm,extra=0,can_order=False,can_delete=False)

class ModelElementForm(ModelForm):
	class Meta:
		model = ModelMetricLink
		widgets = {
			'metric': HiddenInput
		}

	def save(self, *args, **kwargs):
		super(ModelElementForm, self).save(*args, **kwargs)
		if hasattr(self,'nested'):
			self.nested.save()

	def is_valid(self,*args, **kwargs):
		if hasattr(self,'nested'):
			return super(ModelElementForm, self).is_valid(*args, **kwargs) and self.nested.is_valid()
		else:
			return super(ModelElementForm, self).is_valid(*args, **kwargs)

	def has_changed(self, *args, **kwargs):
		has_changed = super(ModelElementForm, self).has_changed(*args, **kwargs)
		if hasattr(self,'nested'):
			for form in self.nested.forms:
				has_changed = has_changed or form.has_changed
		return has_changed

	def __init__(self, data=None, *args, **kwargs):
		super(ModelElementForm, self).__init__(data=data, *args, **kwargs)
		if self.instance.metric.metricType == 'M':
			self.nested = MCScoreFormSet(instance=self.instance,prefix="C%s" % self.instance.pk, data=data)

ChoiceFormSet = inlineformset_factory(Metric,MCOption,can_order=True,can_delete=True)

RiskModelFormset = inlineformset_factory(RiskModel, ModelMetricLink, form=ModelElementForm,  extra=0,can_delete=True)