'''
Alice and Bob have an undirected graph of n nodes and three types of edges:

Type 1: Can be traversed by Alice only.
Type 2: Can be traversed by Bob only.
Type 3: Can be traversed by both Alice and Bob.
Given an array edges where edges[i] = [typei, ui, vi] represents a bidirectional edge of type typei between nodes ui and vi, find the maximum number of edges you can remove so that after removing the edges, the graph can still be fully traversed by both Alice and Bob. The graph is fully traversed by Alice and Bob if starting from any node, they can reach all other nodes.

Return the maximum number of edges you can remove, or return -1 if Alice and Bob cannot fully traverse the graph.

Example 1:

Input: n = 4, edges = [[3,1,2],[3,2,3],[1,1,3],[1,2,4],[1,1,2],[2,3,4]]
Output: 2
Explanation: If we remove the 2 edges [1,1,2] and [1,1,3]. The graph will still be fully traversable by Alice and Bob. Removing any additional edge will not make it so. So the maximum number of edges we can remove is 2.
Example 2:

Input: n = 4, edges = [[3,1,2],[3,2,3],[1,1,4],[2,1,4]]
Output: 0
Explanation: Notice that removing any edge will not make the graph fully traversable by Alice and Bob.
Example 3:

Input: n = 4, edges = [[3,2,3],[1,1,2],[2,3,4]]
Output: -1
Explanation: In the current graph, Alice cannot reach node 4 from the other nodes. Likewise, Bob cannot reach 1. Therefore it's impossible to make the graph fully traversable.

Constraints:

1 <= n <= 105
1 <= edges.length <= min(105, 3 * n * (n - 1) / 2)
edges[i].length == 3
1 <= typei <= 3
1 <= ui < vi <= n
All tuples (typei, ui, vi) are distinct.
'''
class Solution:

    def dfs(self,vis,li,com,i):
        if(vis[i]):
            return 0
        vis[i]=com
        ans=1
        for j in li[i]:
            ans+=self.dfs(vis,li,com,j)
        return ans

    def maxNumEdgesToRemove(self, n: int, e: List[List[int]]) -> int:
        ans=0
        li=[[] for i in range(n)]
        for l in e:
            if(l[0]==3):
                li[l[1]-1].append(l[2]-1)
                li[l[2]-1].append(l[1]-1)
        vis=[0 for i in range(n)]
        com=0
        for i in range(n):
            if(vis[i]):
                continue
            else:
                com+=1
                temp=self.dfs(vis,li,com,i)
                ans+=(temp-1)
        lia=[[] for i in range(com)]
        lib=[[] for i in range(com)]
        for l in e:
            if(l[0]==1):
                a=vis[l[1]-1]-1
                b=vis[l[2]-1]-1
                if(a==b):
                    continue
                lia[a].append(b)
                lia[b].append(a)
            elif(l[0]==2):
                a=vis[l[1]-1]-1
                b=vis[l[2]-1]-1
                if(a==b):
                    continue
                lib[a].append(b)
                lib[b].append(a)
        visa=[0 for i in range(com)]
        coma=0
        comb=0
        for i in range(com):
            if(visa[i]):
                continue
            else:
                coma+=1
                if(coma>1):
                    return -1
                self.dfs(visa,lia,coma,i)

        visb=[0 for i in range(com)]
        for i in range(com):
            if(visb[i]):
                continue
            else:
                comb+=1
                if(comb>1):
                    return -1
                self.dfs(visb,lib,comb,i)
        if(coma>1 or comb>1):
            return -1
        ans+=(com-1)*2
        return len(e)-ans

daily questions/1579. Remove Max Number of Edges to Keep Graph Fully Traversable.py


import requests
from bs4 import BeautifulSoup
import random
# columbus salami: 073007107034

def lookup(barcode):

    # list of user agents to not raise alert
    user_agent_list = [
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
        'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)',
        'Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1',
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134'
    ]

    # pick a random user agent
    user_agent = random.choice(user_agent_list)

    # initialize header with user agent
    headers = {
        "User-Agent": user_agent
    }

    # create URL with entered barcode
    URL = "https://us.openfoodfacts.org/product/"+str(barcode)

    # grab html content and apply beautifulsoup
    page = requests.get(URL, headers=headers)
    soup = BeautifulSoup(page.content, "html.parser")

    # search for name of product
    raw_name = soup.find('title')
    name = raw_name.text.strip()
    print(f"Product: {name}")

    # list of ids for nutritional types lookup
    type_list = [
        ['nutriment_fat_tr', 'fat'],
        ['nutriment_cholesterol_tr', 'cholesterol'],
        ['nutriment_salt_tr', 'salt'],
        ['nutriment_sodium_tr', 'sodium'],
        ['nutriment_carbohydrates_tr', 'carbohydrates'],
        ['nutriment_fiber_tr', 'fiber'],
        ['nutriment_sugars_tr', 'sugars'],
        ['nutriment_proteins_tr', 'proteins']
    ]

    # iterate over all given contents
    for item in type_list:
        # search for given content
        result = soup.find(id=item[0])

        # search for values within content
        inner_result = result.find_all(class_="nutriment_value")

        # iterate over all values and insert into list initalized with type in 0 place.
        # format [type, 100g value, serving value, extra info...]
        nutrition_list = []
        nutrition_list.append(item[1])
        for nutriment_value in inner_result:
            nutrition_list.append(nutriment_value.text.strip())

        # see everything returned
        #print(nutrition_list)

        # grab necessary values
        nutri_type = nutrition_list[0]
        nutri_100g = nutrition_list[1]
        nutri_ss = nutrition_list[2]

        print(f"Type: {nutri_type}")
        print(f"\tServing Size: {nutri_ss}")
        print(f"\t100g: {nutri_100g}")


from vcover_algorithms import *
import networkx as nx

def test_binary_algorithms():
    nodes = [0,1,2,3]
    edges = [(0,2),(0,3),(2,3),(1,2),(1,3)]
    n = 4
    cover = [False for _ in range(n)]
    k = 3

    assert len(BinaryVCoverAlgorithm().find_vcovers_binary(nodes, nodes, edges, edges, cover, n, 0, k, lambda _ : None, lambda _ : None)) == 4

def test_find_vcovers_brute():
    assert len(VCoverBruteForce([0,1,2,3], [(0,2),(0,3),(2,3),(1,2),(1,3)], 4, 3, lambda _ : None, lambda _ : None).find_vcovers()) == 4
    assert len(VCoverBruteForce([0,1,2,3], [(0,2),(0,3),(2,3),(1,2),(1,3)], 4, 2, lambda _ : None, lambda _ : None).find_vcovers()) == 1
    assert len(VCoverBruteForce([0,1,2,3], [(0,2),(0,3),(2,3),(1,2),(1,3)], 4, 1, lambda _ : None, lambda _ : None).find_vcovers()) == 0

def test_find_vcovers_enhanced():
    vcovers = VCoverEnhanced([0,1,2,3,4,5,6],[(0,2),(0,3),(0,6),(0,1),(1,2),(1,3),(1,6),(1,4),(1,5),(2,3),(2,6),(3,6),(4,6),(5,6)],
        4, lambda _ : None, lambda _ : None).find_vcovers()
    assert vcovers[0] == [0,1,2,6]

    vcovers = VCoverEnhanced([0,1,2,3,4,5,6],[(0,2),(0,3),(0,6),(0,1),(1,2),(1,3),(1,6),(1,4),(1,5),(2,3),(2,6),(3,6),(4,6),(5,6)],
        4, lambda _ : None, lambda _ : None).find_vcovers()
    assert vcovers[0] == [0,1,2,6]

def test_find_vcovers_take2():
    vcovers = VCoverTakeTwo([(0,2),(0,3),(1,3),(1,2),(4,0)]).find_vcovers()
    assert len(vcovers) == 1
    assert sorted(vcovers[0]) == [0,1,2,3]

def test_find_vcovers_greedy():
    vcovers = VCoverGreedy([(0,2),(0,3),(1,3),(1,2),(4,0)]).find_vcovers()
    assert len(vcovers) == 1
    assert sorted(vcovers[0]) == [0, 1]

    vcovers = VCoverGreedy([(0,2),(0,3),(0,6),(0,1),(1,2),(1,3),(1,6),(1,4),(1,5),(2,3),(2,6),(3,6),(4,6),(5,6)]).find_vcovers()
    assert len(vcovers) == 1
    assert sorted(vcovers[0]) == [0,1,2,6]


import sys
import os
import numpy as np
import pickle

from .gen_graphs import gen_graphs

def spladder_core(options):

    genes_loaded = False

    ### check if result file exists and start gen graph step if necessary
    if not os.path.exists(options.out_fname):
        print('Augmenting splice graphs.', file=sys.stdout)
        print('=========================', file=sys.stdout)
        if not hasattr(options, 'genes'):
            genes = pickle.load(open(options.annotation, 'rb'), encoding='latin1')
        else:
            genes = options.genes

        genes = gen_graphs(genes, options)

        print('Saving genes to %s' % (options.out_fname))
        pickle.dump(genes, open(options.out_fname, 'wb'), -1)

        genes_loaded = True
    else:
        print('Augmenting splice graphs already completed.')

    ### prune splice graph if necessary
    if options.do_prune:
        load_fn = options.out_fname
        options.out_fname = re.sub(r'.pickle$', '_pruned.pickle', options.out_fname)
        if not os.path.exists(options.out_fname):
            ### load genes if not present yet
            if not genes_loaded:
                genes = pickle.load(open(load_fn), 'rb')

            ### make splice graphs unique
            genes = uniquify_splicegraph(genes)

            ### prune graphs
            num_paths_before = count_all_paths(genes)
            genes = prune_graph(genes, bam_fnames)
            num_paths_after = count_all_paths(genes)

            ### save pruned genes
            print('saving genes to %s' % (options.out_fname))
            pickle.dump(genes, open(options.out_fname, 'wb'), -1)

            genes_loaded = True;
        else:
            print('Pruning of splice graphs already done')
    else:
        print('No pruning requested!')

    ### generate isoforms if necessary
    if options.do_gen_isoforms:
        load_fn = options.out_fname
        options.out_fame = re.sub(r'.pickle$', '_with_isoforms.pickle', options.out_fname);
        if not os.path.exists(options.out_fname):
            ### load genes if not present yet
            if not genes_loaded:
                genes = pickle.load(open(load_fn, 'rb'))

            ### generate isoforms
            print('Generating all isoforms')
            genes = generate_isoforms(genes, PAR['conf'])

            ### re-constitute splicing graph
            print('\tRe-constituting simplified splice graph from generated transcripts')
            genes_unsimplified = genes
            genes = splice_graph(genes, options.infer_sg)

            ### save splicing graph with isoforms
            print('\tSaving genes to %s' % options.out_fname)
            pickle.dump((genes, genes_unsimplified), open(options.out_fname, 'wb'), -1)
        else:
            print('Generating all isoforms already done')
    else:
        print('Generating all isoforms not requested')


from nltk.tokenize import sent_tokenize, word_tokenize
import Stopwords
import string
import re

class tweet:

    def __init__(self, dataline, wordMap, uniWordMap):
        self.tweetwords = []
        self.dataline = dataline
        self.wordMap = wordMap
        self.uniWordMap = uniWordMap
        self.time = 0

    def tweet(self):
        number = len(self.wordMap)
        inline = self.dataline
        words = []
        tokens = self.tokenize(inline)

        for token in tokens:
            tmpToken = token.lower()
            if ((not Stopwords.isStopword(tmpToken)) and (not self.isNoisy(tmpToken))):
                if (not(tmpToken in self.wordMap)):
                    words.append(number)
                    self.wordMap[tmpToken] = number
                    number += 1
                    self.uniWordMap.append(tmpToken)
                else:
                    words.append(self.wordMap.get(tmpToken))

        for w in words:
            self.tweetwords.append(w)

        words.clear()
        tokens.clear()

    def isNoisy(self, token):
        if(("#pb#" in token.lower()) or ("http:" in token.lower())):
            return True

        if("@" in token.lower()):
            return True

        if(token in string.punctuation):
            return True

        return False

    def tokenize(self, line):
        tokens = word_tokenize(line)
        return tokens


from xml.dom.minidom import Document
from validate_docbr import CPF, CNPJ

class documento:
    @staticmethod
    def cria_documento(documento):
        if len(documento) == 11:
            return Cpf(documento)
        elif len(documento) == 14:
            return Cnpj(documento)
        else:
            raise ValueError ("Numero de caracteres incorreto!")

class Cpf:
    def __init__(self, documento):
        documento = str(documento)
        if self.validar_cpf(documento):
            self.cpf = documento
        else:
            raise ValueError("CPF inválido")

    def validar_cpf(self, cpf):
            validador = CPF()
            return validador.validate(cpf)

    def __str__(self):
        cpf = CPF()
        mascara = cpf.mask(self.cpf)
        return ("O CPF é {}".format(mascara))

class Cnpj:
    def __init__(self, documento):
        documento = str(documento)
        if self.valida_cnpj(documento):
            self.cnpj = documento
        else:
            raise ValueError("CNPJ inválido!")

    def valida_cnpj(self, cnpj):
            validador = CNPJ()
            return validador.validate(cnpj)

    def __str__(self):
        cnpj = CNPJ()
        mascara= cnpj.mask(self.cnpj)
        return ("O CNPJ é {}".format(mascara))

Eduardo = documento.cria_documento("04082987070")
print(Eduardo)

StarBit_enterprise = documento.cria_documento("02828446000134")
print(StarBit_enterprise)


from app.classes.workers.workerRoot import WorkerRoot
from app.classes.migrate.migrate import MigrateDB
import json

class SvcMigrate(WorkerRoot):
    """
        SvcAggregate

        Service for Aggregation Computations
    """

    def __init__(self, is_tmp: bool = None):
        # call parent __init__
        super(SvcMigrate, self).__init__(
            self,
            str(SvcMigrate),
            MigrateDB(),
            120,
            ['migrate'],
            True)

    def GetWorkerRoot(self):
        return super(SvcMigrate, self)

    @staticmethod
    def GetInstance(myClass: object = None):
        return WorkerRoot.GetInstance(SvcMigrate)

    @staticmethod
    def runMe(params: json = {}):
        svc_agg_instance = SvcMigrate.GetInstance()
        if svc_agg_instance.IsRunning() is False:
            svc_agg_instance.Start()
        svc_agg_instance.RunMe(params)


import factory

from .models import User

class UserFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = User

    tel = factory.Sequence(lambda n: f"{n:08d}")
    name = factory.Faker("name")
    is_active = True


from turtle import Turtle
counter = 0
class Score(Turtle):
    def __init__(self):
        super().__init__()
        self.penup()
        self.color("white")
        self.goto(0,270)
        self.hideturtle()
        self.score =0
        self.write(f"Score:{self.score}", font=("Calibri", 25, "normal"))
    def increase_score(self):
        self.clear()
        self.score+=1
        self.write(f"Score:{self.score}", font=("Calibri", 25, "normal"))


#!/usr/bin/env python
# encoding: utf-8
"""
@author: HuRuiFeng
@file: 184_team-up-by-ability.py
@time: 2023/8/27 1:28
@project: huawei-od-python
@desc: 184 能力组队
"""

def solve_method(nums, min_power):
    # 过滤出小于最低能力值的人
    arr = sorted(filter(lambda x: x < min_power, nums))
    # 大于等于最低能力值的人，都可以自成一队
    count = len(nums) - len(arr)
    # 双指针遍历
    i, j = 0, len(arr) - 1
    while i < j:
        if arr[i] + arr[j] >= min_power:
            # 如果最小值和最大值加起来小于最低能力值，可以组成一队
            count += 1
            i += 1
            j -= 1
        else:
            # 否则左指针向右移
            i += 1
    return count

if __name__ == '__main__':
    assert solve_method([3, 1, 5, 7, 9], 8) == 3
    assert solve_method([3, 1, 5, 7, 9, 2, 6], 8) == 4
    assert solve_method([3, 1, 5, 6, 9], 8) == 2


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def hasCycle(self, head: Optional[ListNode]) -> bool:
        # set solution
        # checked = set()
        # while head:
        #     if head in checked:
        #         return True
        #     checked.add(head)
        #     head = head.next
        # return False

        slowPointer, fastPointer = head, head
        # slow moves one step each time
        # fast moves two steps each time

        while fastPointer and fastPointer.next:
            # due to fast need two steps if fast.next.next is null
            # while means fast reach the end of the list, then return False
            slowPointer = slowPointer.next
            fastPointer = fastPointer.next.next

            if slowPointer == fastPointer:
                return True

        return False


import pandas as pd
import json
import math as m

df = pd.read_excel('data/BD_Docentes.xlsx', header=0)

members = []

def getAreaReference(arg):
    switcher = {
        'SOCIO-HUMANÍSTICA': {
            'label': arg,
            'reference':'academic-areas/socio-humanistica'
        },
        'ADMINISTRATIVA': {
            'label': arg,
            'reference':'academic-areas/administrativa'
        },
        'BIOLÓGICA Y BIOMÉDICA': {
            'label': arg,
            'reference':'academic-areas/biologica-biomedica'
        },
        'TÉCNICA': {
            'label': arg,
            'reference':'academic-areas/tecnica'
        }
    }
    return switcher.get(arg, 'Invalid area')

def getDepartmentReference(arg):
    switcher = {
        'ARQUITECTURA Y ARTES':{
            'label': arg,
            'reference':'departments/arquitectura-artes'
        },
        'CIENCIAS BIOLÓGICAS':{
            'label': arg,
            'reference':'departments/ciencias-biologicas'
        },
        'CIENCIAS DE LA SALUD':{
            'label': arg,
            'reference':'departments/ciencias-salud'
        },
        'CIENCIAS EMPRESARIALES':{
            'label': arg,
            'reference':'departments/ciencias-empresariales'
        },
        'CIENCIAS DE LA COMPUTACIÓN Y ELECTRÓNICA':{
            'label': arg,
            'reference':'departments/ciencias-computacion-electronica'
        },
        'CIENCIAS DE LA COMUNICACIÓN':{
            'label': arg,
            'reference':'departments/ciencias-comunicacion'
        },
        'PSICOLOGÍA':{
            'label': arg,
            'reference':'departments/psicologia'
        },
        'QUÍMICA Y CIENCIAS EXACTAS':{
            'label': arg,
            'reference':'departments/quimica-ciencias-exactas'
        },
        'GEOLOGÍA Y MINAS E INGENIERÍA CIVIL':{
            'label': arg,
            'reference':'departments/geologia-minas-ingenieria-civil'
        },
        'CIENCIAS JURÍDICAS':{
            'label': arg,
            'reference':'departments/ciencias-juridicas'
        },
        'ECONOMÍA':{
            'label': arg,
            'reference':'departments/economia'
        },
        'CIENCIAS DE LA EDUCACIÓN':{
            'label': arg,
            'reference':'departments/ciencias-educacion'
        }
    }
    return switcher.get(arg, 'Invalid dept')

def getSectionReference(arg):
    switcher = {
        'FILOSOFÍA Y TEOLOGÍA':{
            'label': arg,
            'reference':'sections/filosofia-teologia'
        },
        'LENGUAS CONTEMPORÁNEAS':{
            'label': arg,
            'reference':'sections/lenguas-contemporaneas'
        },
        'LENGUAS HISPÁNICAS Y LITERATURA':{
            'label': arg,
            'reference':'sections/lenguas-hispanicas-literatura'
        },
        'PEDAGOGÍA DE LAS CIENCIAS EXPERIMENTALES':{
            'label': arg,
            'reference':'sections/pedagogia-ciencias-experimentales'
        },
        'PSICOPEDAGOGÍA':{
            'label': arg,
            'reference':'sections/psicopedagogia'
        },
        'ARQUITECTURA Y URBANISMO':{
            'label': arg,
            'reference':'sections/arquitectura-urbanismo'
        },
        'ARTE, TEORÍA Y CONSERVACIÓN DEL PATRIMONIO':{
            'label': arg,
            'reference':'sections/arte-teoria-conservacion-patrimonio'
        },
        'BIOLOGÍA Y GENÉTICA':{
            'label': arg,
            'reference':'sections/biologia-genetica'
        },
        'BIOTECNOLOGIA Y PRODUCCION':{
            'label': arg,
            'reference':'sections/biotecnologia-produccion'
        },
        'CIENCIAS DE LA EDUCACIÓN BÁSICA':{
            'label': arg,
            'reference':'sections/ciencias-educacion-basica'
        },
        'CLÍNICO QUIRÚRGICA':{
            'label': arg,
            'reference':'sections/clinico-quirurgica'
        },
        'COMUNICACIÓN ORGANIZACIONAL':{
            'label': arg,
            'reference':'sections/comunicacion-organizacional'
        },
        'COMUNICACIÓN TECNOLOGIAS':{
            'label': arg,
            'reference':'sections/comunicacion-tecnologias'
        },
        'CONTABILIDAD Y AUDITORIA':{
            'label': arg,
            'reference':'sections/contabilidad-auditoria'
        },
        'DERECHO PRIVADO':{
            'label': arg,
            'reference':'sections/derecho-privado'
        },
        'DERECHO PÚBLICO':{
            'label': arg,
            'reference':'sections/derecho-publico'
        },
        'DESARROLLO ECONÓMICO':{
            'label': arg,
            'reference':'sections/desarrollo-economico'
        },
        'DISEÑO Y EXPRESIÓN ARTÍSTICA':{
            'label': arg,
            'reference':'sections/diseno-expresion-artistica'
        },
        'ECOLOGÍA Y SISTEMÁTICA':{
            'label': arg,
            'reference':'sections/ecologia-sistematica'
        },
        "ELECTRONICA Y TELECOMUNICACIONES": {
            'label': arg,
            "reference": "sections/electronica-telecomunicaciones",
        },
        "ESTRUCTURAS, TRANSPORTE Y CONSTRUCCION": {
            'label': arg,
            "reference": "sections/estructuras-transporte-construccion",
        },
        "EVALUACIÓN Y DIAGNÓSTICO PSICOLÓGICO": {
            'label': arg,
            "reference": "sections/evaluacion-diagnostico-psicologico",
        },
        "FILOSOFÍA Y TEOLOGÍA":{
            'label': arg,
            "reference": "sections/filosofia-teologia",
        },
        "FINANZAS Y GESTIÓN BANCARIA":{
            "label": arg,
            "reference": "sections/finanzas-gestion-bancaria"
        },
        "FISICOQUIMÍCA Y MATEMÁTICAS":{
            "label": arg,
            "reference": "sections/fisicoquimica-matematicas"
        },
        "GENÉTICA HUMANA, MICROBIOLOGÍA Y BIOQUÍMICA CLÍNICA":{
            "label": arg,
            "reference": "sections/genetica-humana-microbiologia-bioquimica-clinica"
        },
        "GEODINÁMICA, MINERÍA Y METALURGIA":{
            "label": arg,
            "reference": "sections/geodinamica-mineria-metalurgia"
        },
        "HOTELERÍA Y TURISMO":{
            "label": arg,
            "reference": "sections/hoteleria-turismo",
        },
        "INGENIERÍA AMBIENTAL":{
            "label": arg,
            "reference": "sections/ingenieria-ambiental",
        },
        "INGENIERÍA DE PROCESOS":{
            "label": arg,
            "reference": "sections/ingenieria-procesos"
        },
        "INGENIERÍA DE SOFTWARE Y GTI": {
            "label": arg,
            "reference": "sections/ingenieria-software-gti",
        },
        "INGENIERÍA ARTIFICIAL":{
            "reference": "sections/inteligencia-artificial",
            "label": arg,
        },
        "INTERVENCIÓN PSICOLÓGICA":{
            "reference": "sections/intervencion-psicologica",
            "label": arg,
        },
        "MANEJO Y GESTIÓN DE RECURSOS NATURALES":{
            "reference": "sections/manejo-gestion-recursos-naturales",
            "label": arg
        },
        "MÉTODOS CUANTITATIVOS":{
            "reference": "sections/metodos-cuantitativos",
            "label": arg
        },
        "NARRATIVAS AUDIOVISUALES":{
            "reference": "sections/narrativas-audiovisuales",
            "label": arg
        },
        "NUEVAS TENDENCIAS DEL DERECHO":{
            "reference": "sections/nuevas-tendencias-derecho",
            "label": arg
        },
        "ORGANIZACIÓN Y GESTIÓN EMPRESARIAL":{
            "reference": "sections/organizacion-gestion-empresarial",
            "label": arg
        },
        "PEDAGOGÍA DE LAS CIENCIAS EXPERIMENTALES":{
            "reference": "sections/pedagogia-las-ciencias-experimentales",
            "label": arg
        },
        "POLÍTICAS PÚBLICAS":{
            "reference": "sections/politicas-publicas",
            "label": arg
        },
        "PRECLÍNICA":{
            "reference": "sections/preclinica",
            "label": arg
        },
        "PSICOLOGÍA BÁSICA":{
            "reference": "sections/psicologia-basica",
            "label": arg
        },
        "PSICOLOGÍA EVOLUTIVA":{
            "reference": "sections/psicologia-evolutiva",
            "label": arg
        },
        "PSICOPEDAGOGÍA":{
            "reference": "sections/psicopedagogia",
            "label": arg
        },
        "QUÍMICA BÁSICA Y APLICADA": {
            "reference": "sections/quimica-basica-aplicada",
            "label": arg
        },
        "RECURSOS HÍDRICOS":{
            "reference": "sections/recursos-hidricos",
            "label": arg
        },
        "SOCIOHUMANÍSTICA, SALUD PÚBLICA Y GESTIÓN EN SALUD":{
            "reference": "sections/sociohumanistica-salud-publica-gestion-en-salud",
            "label": arg
        },
        "TECNOLOGIAS AVANZADAS DE LA WEB Y SBC":{
            "reference": "sections/tecnologias-avanzadas-web-sbc",
            "label": arg
        },
        "TEORÍA ECONÓMICA":{
            "reference": "sections/teoria-economica",
            "label": arg
        }
    }
    return switcher.get(arg, 'Invalid section')

for index, row in df.iterrows():
    if(type(row['mail']) is str):
        level_fourth = [row['level_fourth']]
        level_third = [row['level_third']]

        temp = {
            "id": row['mail'],
            "ci":row['ci'],
            "mail":row['mail'],
            "area": getAreaReference(row['area']),
            "department":getDepartmentReference(row['department']),
            "displayName":row['displayName'],
            "level_fourth": level_fourth,
            "level_third": level_third,
            "modality": row['modality'],
            "section":getSectionReference(row['section'])
        }
        members.append(temp)

with open('users.json', 'w',encoding='utf8') as outfile:
    json.dump(members, outfile, ensure_ascii=False, indent=4)


import time
import gi
gi.require_version('Notify', '0.7')
from gi.repository import Notify

Notify.init('Nag')

def show(msg):
	print(msg)
	notification = Notify.Notification.new(msg)
	notification.show()
	time.sleep(10)
	notification.close()

def show_all(msgs):
	show('\r\n'.join(msgs))


from typing import List

input = """office, country, telephone
chicago, USA, +1 312 373 1000
Beijing, China, +86 4008 900 505
Bangalore, India, +91 80 4064 9570
"""

def acquireData(input):
    lines: List[str] = input.split("\n")
    result = []
    loopItems = lines[1:].filter
    for line in loopItems:
        record = line.split(',')
        if record[1].strip() == "India":
            result.append({
                'city': record[0].strip(),
                'phone': record[2].strip()
            })
        return result


"""

Download and convert COVID Trackin Project COVID-19 data in a VR Ulysses file.

Author: Rory Barnes
Date: 8 Apr 2020

"""

import numpy as np
import string as str
import subprocess as subp
import csv
import subprocess as subp
import re
import requests
import pandas as pd

sDirCovidTracking="/Users/rory/DataViz/Ulysses/covid19/covid-tracking-data/data"
#sSource="/Users/rory/DataViz/Ulysses/covid19/covid-tracking-data/data/states_daily_4pm_et.csv"
sSource="daily.csv"

iDaysJan=9
iDaysFeb=29
iDaysMarch=31
iDaysApril=30
iDaysMay=31
iDaysJune=30
iDaysJuly=31
iDaysAug=31
iDaysSep=30
iDaysOct=31
iDaysNov=30
iDaysDec=31

# Return number of days, and date
def fnDays(sDate):
    iYear=int(sDate[0:4])
    iMonth=int(sDate[4:6])
    iDay=int(sDate[-2:])
    # Sometime I'll make this more elegant
    if iMonth == 1:
        iNumDays = iDay - 22
    if iMonth == 2:
        iNumDays = iDay + iDaysJan
    if iMonth == 3:
        iNumDays = iDay + iDaysJan + iDaysFeb
    if iMonth == 4:
        iNumDays = iDay + iDaysJan + iDaysFeb + iDaysMarch
    if iMonth == 5:
        iNumDays = iDay + iDaysApril + iDaysMarch + iDaysJan + iDaysFeb
    if iMonth == 6:
        iNumDays = iDay + iDaysApril + iDaysMay + iDaysMarch + iDaysJan + iDaysFeb
    if iMonth == 7:
        iNumDays = iDay + iDaysApril + iDaysMay + iDaysJune + iDaysMarch + iDaysJan + iDaysFeb
    if iMonth == 8:
        iNumDays = iDay + iDaysApril + iDaysMay + iDaysJune + iDaysJuly + iDaysMarch + iDaysJan + iDaysFeb
    if iMonth == 9:
        iNumDays = iDay + iDaysApril + iDaysMay + iDaysJune + iDaysJuly + iDaysAug + iDaysMarch + iDaysJan + iDaysFeb
    if iMonth == 10:
        iNumDays = iDay + iDaysApril + iDaysMay + iDaysJune + iDaysJuly + iDaysAug + iDaysMarch + iDaysJan + iDaysFeb + iDaysSep
    if iMonth == 11:
        iNumDays = iDay + iDaysApril + iDaysMay + iDaysJune + iDaysJuly + iDaysAug + iDaysMarch + iDaysJan + iDaysFeb + iDaysSep + iDaysOct
    if iMonth == 12:
        iNumDays = iDay + iDaysApril + iDaysMay + iDaysJune + iDaysJuly + iDaysAug + iDaysMarch + iDaysJan + iDaysFeb + iDaysSep + iDaysOct + iDaysNov

    # First day still counts
    #print(repr(iDay),repr(iMonth),sDate)
    iNumDays += 1
    sDate=repr(iDay)+' '
    if iMonth == 1:
        sDate += 'Jan'
    if iMonth == 2:
        sDate += 'Feb'
    if iMonth == 3:
        sDate += 'Mar'
    if iMonth == 4:
        sDate += 'Apr'
    if iMonth == 5:
        sDate += 'May'
    if iMonth == 6:
        sDate += 'Jun'
    if iMonth == 7:
        sDate += 'Jul'
    if iMonth == 8:
        sDate += 'Aug'
    if iMonth == 9:
        sDate += "Sep"
    if iMonth == 10:
        sDate += "Oct"
    if iMonth == 11:
        sDate += "Nov"
    if iMonth == 12:
        sDate += "Dec"

    sDate += ' 2020'
    #print (sDate)
    return iNumDays,sDate

# Return iState,sState,iPopulation (from Wikipedia)
def fnState(sAbbrev):
    if sAbbrev == "AK":
        return 0,"Alaska",0.732
    if sAbbrev == "AL":
        return 1,"Alabama",4.903
    if sAbbrev == "AR":
        return 2,"Arkansas",3.018
    if sAbbrev == "AS":
        return 3,"American Samoa",0.056
    if sAbbrev == "AZ":
        return 4,"Arizona",7.279
    if sAbbrev == "CA":
        return 5,"California",39.512
    if sAbbrev == "CO":
        return 6,"Colorado",5.729
    if sAbbrev == "CT":
        return 7,"Connecticut",3.565
    if sAbbrev == "DC":
        return 8,"District of Columbia",0.706
    if sAbbrev == "DE":
        return 9,"Delaware",0.974
    if sAbbrev == "FL":
        return 10,"Florida",21.478
    if sAbbrev == "GA":
        return 11,"Georgia",10.617
    if sAbbrev == "GU":
        return 12,"Guam",0.166
    if sAbbrev == "HI":
        return 13,"Hawaii",1.416
    if sAbbrev == "IA":
        return 14,"Iowa",3.155
    if sAbbrev == "ID":
        return 15,"Idaho",1.787
    if sAbbrev == "IL":
        return 16,"Illinois",12.671
    if sAbbrev == "IN":
        return 17,"Indiana",6.732
    if sAbbrev == "KS":
        return 18,"Kansas",2.913
    if sAbbrev == "KY":
        return 19,"Kentucky",4.468
    if sAbbrev == "LA":
        return 20,"Louisiana",4.649
    if sAbbrev == "MA":
        return 21,"Massachusetts",6.950
    if sAbbrev == "MD":
        return 22,"Maryland",6.046
    if sAbbrev == "ME":
        return 23,"Maine",1.344
    if sAbbrev == "MI":
        return 24,"Michigan",9.987
    if sAbbrev == "MN":
        return 25,"Minnesota",5.640
    if sAbbrev == "MO":
        return 26,"Missourri",6.137
    if sAbbrev == "MP":
        return 27,"Northern Mariana Islands",0.055
    if sAbbrev == "MS":
        return 28,"Mississippi",2.976
    if sAbbrev == "MT":
        return 29,"Montana",1.069
    if sAbbrev == "NC":
        return 30,"North Carolina",10.488
    if sAbbrev == "ND":
        return 31,"North Dakota",0.762
    if sAbbrev == "NE":
        return 32,"Nebraska",1.934
    if sAbbrev == "NH":
        return 33,"New Hampshire",1.360
    if sAbbrev == "NJ":
        return 34,"New Jersey",8.882
    if sAbbrev == "NM":
        return 35,"New Mexico",2.097
    if sAbbrev == "NV":
        return 36,"Nevada",3.080
    if sAbbrev == "NY":
        return 37,"New York",19.454
    if sAbbrev == "OH":
        return 38,"Ohio",11.689
    if sAbbrev == "OK":
        return 39,"Oklahoma",3.957
    if sAbbrev == "OR":
        return 40,"Oregon",4.218
    if sAbbrev == "PA":
        return 41,"Pennsylvania",12.802
    if sAbbrev == "PR":
        return 42,"Puerto Rico",3.194
    if sAbbrev == "RI":
        return 43,"Rhode Island",1.059
    if sAbbrev == "SC":
        return 44,"South Carolina",5.149
    if sAbbrev == "SD":
        return 45,"South Dakota",0.885
    if sAbbrev == "TN":
        return 46,"Tennessee",6.833
    if sAbbrev == "TX":
        return 47,"Texas",28.996
    if sAbbrev == "UT":
        return 48,"Utah",3.206
    if sAbbrev == "VA":
        return 49,"Virginia",8.536
    if sAbbrev == "VI":
        return 50,"Virgin Islands",0.105
    if sAbbrev == "VT":
        return 51,"Vermont",0.624
    if sAbbrev == "WA":
        return 52,"Washington",7.615
    if sAbbrev == "WI":
        return 53,"Wisconsin",5.822
    if sAbbrev == "WV":
        return 54,"West Virginia",1.792
    if sAbbrev == "WY":
        return 55,"Wyoming",0.579

iNumStates = 56

# Get latest data
#sCmd = 'cd '+sDirCovidTracking+'; git pull origin master >& gitlog'
#sCmd = 'cd '+sDirCovidTracking

# Cut for debugging
url = 'https://covidtracking.com/api/v1/states/daily.csv'
r = requests.get(url, allow_redirects=True)
open('daily.csv','wb').write(r.content)

#sCmd = `mv daily.csv `+sDirCovidTracking
#subp.call(sCmd, shell=True)

#exit()
print('Latest data set downloaded.')

"""
To initialize columns, must get total number of days in latest data set. The
code reads in the first 2 lines of the file and computes the number of days
since 04 Mar 2020, the first date in the data set. After initializing the
matrices, the file stream is reset, the header read in again, and then the loop
to read in the numeric data is executed.
"""

"""
Column header of the COVID-19 Tracking data

date,0
state,1
positive,2
negative,3
pending,4
hospitalizedCurrently,5
hospitalizedCumulative,6
inIcuCurrently,7
inIcuCumulative,8
onVentilatorCurrently,9
onVentilatorCumulative,10
recovered,11
DataQuality,12
lasUpdate,13
dateModified,14
checkTime,15
death,16
hospitalized,17
dateChecked,18
fips,19
positiveIncrease,20
negativeIncrease,21
totalTestResultsIncrease,22
posNeg,23
deathIncrease,24
hospitalizedIncrease,25
hash,26
commericalScore,27
negRegularScore,28
negativeScore,29
positiveScore,30
score,31
grade,32
"""

# Read in data as Pandas Dataframe
data = pd.read_csv(sSource)
# Fill in missing values with 0
data.fillna(0, inplace=True)
# Convert relevant columns to ints
data['death'] = data['death'].astype(int)
data['hospitalizedCumulative'] = data['hospitalizedCumulative'].astype(int)
data['hospitalizedCurrently'] = data['hospitalizedCurrently'].astype(int)
data['inIcuCumulative'] = data['inIcuCumulative'].astype(int)
data['inIcuCurrently'] = data['inIcuCurrently'].astype(int)
data['negative'] = data['negative'].astype(int)
data['onVentilatorCumulative'] = data['onVentilatorCumulative'].astype(int)
data['onVentilatorCurrently'] = data['onVentilatorCurrently'].astype(int)
data['pending'] = data['pending'].astype(int)
data['positive'] = data['positive'].astype(int)
data['recovered'] = data['recovered'].astype(int)

iaDate = data['date'].values
iaDeathsTotal = data['death'].values
iaHospitalizedCumulative = data['hospitalizedCumulative'].values
iaHospitalizedCurrently = data['hospitalizedCurrently'].values
iaInIcuCumulative = data['inIcuCumulative'].values
iaInIcuCurrently = data['inIcuCurrently'].values
iaNegativeCumulative = data['negative'].values
iaOnVentilatorCumulative = data['onVentilatorCumulative'].values
iaOnVentilatorCurrently = data['onVentilatorCurrently'].values
iaPending = data['pending'].values
iaPositiveCumulative = data['positive'].values
iaRecoveredCumulative = data['recovered'].values

saDataQuality = data['dataQualityGrade'].values
saFIPS = data['fips'].values
saHash = data['hash'].values
saLastUpdate = data['lastUpdateEt'].values
saState = data['state'].values

iRows = len (data['date'].values)

iNumDays,sFoo = fnDays(repr(iaDate[0]))

print('Found '+repr(iNumDays)+' days of data.')

iaPositive = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaNegative = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaPend = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaHospCur = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaHospCum = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaICUCur = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaICUCum = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaVentCum = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaVentCur = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaDeaths = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaTests = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaRecovered = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaDeathsNew = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaHospNew = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaNegNew = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaPosNew = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaTestsNew = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
daPercentPos = [[0.0 for i in range(iNumDays)] for j in range(iNumStates)]
iaDeathsMillion = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaPosMillion = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaTestsMillion = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaTestsCumulative = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
saAbbrev = ["" for i in range(iNumStates)]
iaPop = [0 for i in range(iNumStates)]
saDate = ["" for i in range(iNumDays)]

for iRow in range(iRows):
    #sAbbrev=saLine[1]
    # Must convert to days since 22 Jan
    iDaysSince22Jan,sDateNew = fnDays(repr(iaDate[iRow]))
    #print(sDate+' '+repr(iDaysSince04Mar))
    saDate[iDaysSince22Jan-1] = sDateNew
    #print(repr(iDaysSince22Jan))
    #print(saState[iRow])
    iState,sState,iPopulation = fnState(saState[iRow])
    saAbbrev[iState] = sState
    iaPop[iState] = iPopulation

    iaPositive[iState][iDaysSince22Jan-1] = iaPositiveCumulative[iRow]
    iaNegative[iState][iDaysSince22Jan-1] = iaNegativeCumulative[iRow]
    iaPend[iState][iDaysSince22Jan-1] = iaPending[iRow]
    iaHospCur[iState][iDaysSince22Jan-1] = iaHospitalizedCurrently[iRow]
    iaHospCum[iState][iDaysSince22Jan-1] = iaHospitalizedCumulative[iRow]
    iaICUCur[iState][iDaysSince22Jan-1] = iaInIcuCurrently[iRow]
    iaICUCum[iState][iDaysSince22Jan-1] = iaInIcuCumulative[iRow]
    iaVentCur[iState][iDaysSince22Jan-1] = iaOnVentilatorCurrently[iRow]
    iaVentCum[iState][iDaysSince22Jan-1] = iaOnVentilatorCumulative[iRow]
    iaRecovered[iState][iDaysSince22Jan-1] = iaRecoveredCumulative[iRow]
    iaDeaths[iState][iDaysSince22Jan-1] = iaDeathsTotal[iRow]

for iState in range(iNumStates):
    for iDay in range(iNumDays):
        if iDay > 0:
            iaDeathsNew[iState][iDay] = iaDeaths[iState][iDay] - iaDeaths[iState][iDay-1]
            iaHospNew[iState][iDay] = iaHospCum[iState][iDay] - iaHospCur[iState][iDay]
            iaPosNew[iState][iDay] = iaPositive[iState][iDay] - iaPositive[iState][iDay-1]
            iaTestsNew[iState][iDay] = iaTestsCumulative[iState][iDay] - iaTestsCumulative[iState][iDay-1]
        else:
            iaDeathsNew[iState][iDay] = 0
            iaHospNew[iState][iDay] = 0
            iaPosNew[iState][iDay] = 0
            iaTestsCumulative[iState][iDay] = iaPositive[iState][iDay] + iaNegative[iState][iDay] + iaPend[iState][iDay]
        iaDeathsMillion[iState][iDay] = iaDeaths[iState][iDay]/iaPop[iState]
        iaPosMillion[iState][iDay] = iaPositive[iState][iDay]/iaPop[iState]
        iaTestsMillion[iState][iDay] = iaTests[iState][iDay]/iaPop[iState]
        if iaTestsNew[iState][iDaysSince22Jan-1] > 0:
            #print("% Pos > 0")
            daPercentPos[iState][iDay] = float(iaPosNew[iState][iDay])/iaTestsNew[iState][iDay]
            #print(repr(iaPosNew[iState][iDaysSince22Jan-1]), repr(iaTestsNew[iState][iDaysSince22Jan-1]), repr(daPercentPos[iState][iDaysSince22Jan-1]))
        else:
            daPercentPos[iState][iDaysSince22Jan-1] = 0.0

print ('Last date for data: '+saDate[-1])

"""
csvData = csv.reader(open(sSource,"r"))
saHeader = next(csvData)

saLine = next(csvData)
sDate=saLine[0]
sAbbrev=saLine[1]
# Must convert to days since March 4th
iNumDays,sFoo = fnDays(sDate)
print('Found '+repr(iNumDays)+' days of data.')

iaPositive = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaNegative = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaPending = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaHospCur = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaHospCum = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaICUCur = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaICUCum = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaVentCum = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaVentCur = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaDeaths = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaTests = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaRecovered = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaDeathsNew = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaHospNew = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaNegNew = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaPosNew = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaTestsNew = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
daPercentPos = [[0.0 for i in range(iNumDays)] for j in range(iNumStates)]
iaDeathsMillion = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaPosMillion = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
iaTestsMillion = [[0 for i in range(iNumDays)] for j in range(iNumStates)]
saState = ["" for i in range(iNumStates)]
iaPop = [0 for i in range(iNumStates)]
saDate = ["" for i in range(iNumDays)]

# Rest csv file and read in header
csvData = csv.reader(open(sSource,"r"))
saHeader = next(csvData)

for saLine in csvData:
    sDate=saLine[0]
    sAbbrev=saLine[1]
    # Must convert to days since March 4th
    iDaysSince22Jan,sDateNew = fnDays(sDate)
    #print(sDate+' '+repr(iDaysSince04Mar))
    saDate[iDaysSince22Jan-1] = sDateNew
    #print(repr(iDaysSince22Jan))
    iState,sState,iPopulation = fnState(sAbbrev)
    saState[iState] = sState
    iaPop[iState] = iPopulation
    #print(sState,repr(iState),repr(iDaysSince22Jan))
    if saLine[2] != '':
        iaPositive[iState][iDaysSince22Jan-1] = int(saLine[2])
    if saLine[3] != '':
        iaNegative[iState][iDaysSince22Jan-1] = int(saLine[3])
    if saLine[4] != '':
        iaPending[iState][iDaysSince22Jan-1] = int(saLine[4])
    if saLine[5] != '':
        iaHospCur[iState][iDaysSince22Jan-1] = int(saLine[5])
    if saLine[6] != '':
        iaHospCum[iState][iDaysSince22Jan-1] = int(saLine[6])
    if saLine[7] != '':
        iaICUCur[iState][iDaysSince22Jan-1] = int(saLine[7])
    if saLine[8] != '':
        iaICUCum[iState][iDaysSince22Jan-1] = int(saLine[8])
    if saLine[9] != '':
        iaVentCur[iState][iDaysSince22Jan-1] = int(saLine[9])
    if saLine[10] != '':
        iaVentCum[iState][iDaysSince22Jan-1] = int(saLine[10])
    if saLine[11] != '':
        iaRecovered[iState][iDaysSince22Jan-1] = int(saLine[11])
    if saLine[16] != '':
        iaDeaths[iState][iDaysSince22Jan-1] = int(saLine[16])
    if saLine[19] != '':
        iaTests[iState][iDaysSince22Jan-1] = int(saLine[19])
    if saLine[22] != '':
        iaDeathsNew[iState][iDaysSince22Jan-1] = int(saLine[22])
    if saLine[23] != '':
        iaHospNew[iState][iDaysSince22Jan-1] = int(saLine[23])
    if saLine[24] != '':
        iaNegNew[iState][iDaysSince22Jan-1] = int(saLine[24])
    if saLine[25] != '':
        iaPosNew[iState][iDaysSince22Jan-1] = int(saLine[25])
    if saLine[26] != '':
        iaTestsNew[iState][iDaysSince22Jan-1] = int(saLine[26])
"""

# Read in with Pandas data frame

"""
    if iaTestsNew[iState][iDaysSince22Jan-1] > 0:
        #print("% Pos > 0")
        daPercentPos[iState][iDaysSince22Jan-1] = float(iaPosNew[iState][iDaysSince22Jan-1])/iaTestsNew[iState][iDaysSince22Jan-1]
        #print(repr(iaPosNew[iState][iDaysSince22Jan-1]), repr(iaTestsNew[iState][iDaysSince22Jan-1]), repr(daPercentPos[iState][iDaysSince22Jan-1]))
    else:
        daPercentPos[iState][iDaysSince22Jan-1] = 0.0

    #print(repr(daPercentPos[iState][iDaysSince22Jan-1]))
    iaDeathsMillion[iState][iDaysSince22Jan-1] = iaDeaths[iState][iDaysSince22Jan-1]/iaPop[iState]
    iaPosMillion[iState][iDaysSince22Jan-1] = iaPositive[iState][iDaysSince22Jan-1]/iaPop[iState]
    iaTestsMillion[iState][iDaysSince22Jan-1] = iaTests[iState][iDaysSince22Jan-1]/iaPop[iState]
"""

# Write data!
sOut='covid19-US-'+saDate[-1].replace(" ", "") +'.csv'
#print ('Last date for data: '+saDate[-1])
OutFile=open(sOut,'w')

sOutLine=',State ID,Days Since Jan 22,'
sOutLine += 'Cumulative Deaths,New Deaths,'
sOutLine += 'Cumulative Positive,New Positive,% Positive,'
sOutLine += 'Cumulative Tests,New Tests,'
sOutLine += 'Cumulative Hospitalized,Current Hospitalized,New Hospitalized,'
sOutLine += 'Cumulative in ICU,Currently in ICU,'
sOutLine += 'Cumulative on Vent.,Currently on Vent.,'
sOutLine += 'Deaths/Million,Pos./Million,Tests/Million,'
sOutLine += 'Population,'
sOutLine += '#State,#Date\n'

OutFile.write(sOutLine)

iID = 1
#iLines =3000
iDaysReport = 45
for iState in range(iNumStates):
#    for iDayIndex in range(iLines):
#        iDay = iID + iDayIndex - iLines
#    for iDay in range(iNumDays):
    for iDayIndex in range(iDaysReport):
        iDay = iNumDays + iDayIndex - iDaysReport
        sOutLine = repr(iID)+','+repr(iState)+','+repr(iDay)+','
        sOutLine += repr(iaDeaths[iState][iDay])+','+repr(iaDeathsNew[iState][iDay])+','
        sOutLine += repr(iaPositive[iState][iDay])+','+repr(iaPosNew[iState][iDay])+','+repr(daPercentPos[iState][iDay])+','
        sOutLine += repr(iaTests[iState][iDay])+','+repr(iaTestsNew[iState][iDay])+','
        sOutLine += repr(iaHospCum[iState][iDay])+','+repr(iaHospCur[iState][iDay])+','+repr(iaHospNew[iState][iDay])+','
        sOutLine += repr(iaICUCum[iState][iDay])+','+repr(iaICUCur[iState][iDay])+','
        sOutLine += repr(iaVentCum[iState][iDay])+','+repr(iaVentCur[iState][iDay])+','
        sOutLine += repr(iaDeathsMillion[iState][iDay])+','+repr(iaPosMillion[iState][iDay])+','+repr(iaTestsMillion[iState][iDay])+','
        sOutLine += repr(iaPop[iState])+','
        sOutLine += saState[iState]+','+saDate[iDay]+'\n'
        OutFile.write(sOutLine)
        iID += 1

# Copy file into US.csv
OutFile.close()
cmd = "cp "+sOut+" US.csv"
subp.call(cmd,shell=True)
exit(0)


from datetime import datetime

dia1 = input()
horario1 = input()
full_date1 = datetime.strptime(dia1+' - '+horario1, 'Dia %d - %H : %M : %S')
dia2 = input()
horario2 = input()
full_date2 = datetime.strptime(dia2+' - '+horario2, 'Dia %d - %H : %M : %S')
result = full_date2-full_date1
hours = result.seconds//3600
minute = (result.seconds//60) % 60
seconds = result.seconds - hours*3600 - minute*60
print(f'{result.days} dia(s)')
print(f'{hours} hora(s)')
print(f'{minute} minuto(s)')
print(f'{seconds} segundo(s)')


import ast
from sklearn_crfsuite import CRF
from sklearn.model_selection import train_test_split
from inspect import Attribute
import warnings
from sklearn_crfsuite import CRF
from sklearn.metrics import f1_score
from unidecode import unidecode
from sklearn.metrics import classification_report, recall_score, precision_score, accuracy_score

encoding = "utf-8"

def get_otomi_corpus():
    try:
        sentences = []
        with open("corpus_otomi.txt", "r", encoding=encoding) as file:
            for line in file:
                try:
                    # Evaluar la línea como una expresión de Python y guardarla en una variable
                    sentences.append(ast.literal_eval(line.strip()))
                except Exception as e:
                    print(f"Error al evaluar la línea: {line}")
                    print(e)
        return sentences
    except FileNotFoundError:
        print("El archivo 'corpus_otomi.txt' no se encontró en la carpeta actual.")
    except Exception as e:
        print("Ocurrió un error al abrir el archivo:")
        print(e)

def format_sentence(sent: list):
    data = []
    for word in sent:
        _word = {}
        _word["tag"] = word[-1]
        _word["word"] = "".join(map(lambda s: s[0],word[:-1]))
        _word["morphemes"] = word[:-1]
        data.append(_word)
    return data

def map_tag(tag: str, tags_map : list) -> str:
    return tags_map.get(tag.lower(), "N/F")

def word_to_features(sent, i):
    word = sent[i]
    stem = list(filter(lambda x: x[1] == "stem", word['morphemes']))
    # para las features tomé las que nos dió la profesora como sugerencia
    features = {
        'word.lower()': word["word"].lower(),
        'word[:3]': word["word"][:3],
        'word[:2]': word["word"][:2],
        'word.isdigit()': word["word"].isdigit(),
        'length': len(word["word"]),
        'position': i
    }

    if len(stem) > 0: features.update({'stem': stem[0][0]})

    return features

# Extract features and labels
def sent_to_features(sent):
    return [word_to_features(sent, i) for i in range(len(sent))]

def sent_to_labels(sent):
    return [label for token, label in sent]

corpus = get_otomi_corpus()
data = [format_sentence(s) for s in corpus]

# Prepare data for CRF
X = [[word_to_features(sent, i) for i in range(len(sent))] for sent in data]
y = [[unidecode(word["tag"]) for word in sent] for sent in data]

# split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# para tener una salida limpia y que no se vean los warnings
with warnings.catch_warnings():
    warnings.simplefilter("ignore")

    # Initialize and train the CRF tagger: https://sklearn-crfsuite.readthedocs.io/en/latest/api.html
    crf = CRF(algorithm='lbfgs', c1=0.1, c2=0.1, max_iterations=100, all_possible_transitions=True, verbose=True)

    try:
        crf.fit(X_train, y_train)
    except AttributeError as e:
        print(e)

    y_pred = crf.predict(X_test)

    # Flatten the true and predicted labels
    y_test_flat = [label for sent_labels in y_test for label in sent_labels]
    y_pred_flat = [label for sent_labels in y_pred for label in sent_labels]

    # Evaluate the model
    report = classification_report(y_true=y_test_flat, y_pred=y_pred_flat)
    print("\n\n******* Evaluación del modelo *******")
    print(report)
    print("******* ACCURACY *******")
    print(accuracy_score(y_pred_flat, y_test_flat))
    print("******* PRESICION *******")
    print(precision_score(y_pred_flat, y_test_flat, average="macro"))
    print("******* RECALL *******")
    print(recall_score(y_pred_flat, y_test_flat, average="macro"))
    print("******* F1_SCORE *******")
    print(f1_score(y_pred_flat, y_test_flat, average="macro"))
    print("\n\n\n******* Ejemplo de oración etiquetada *******")
    sentence = X_test[0]
    string = " ".join([w['word.lower()'] for w in sentence])
    print(f"\nOración: {string}")
    prediction = crf.predict([sentence])
    print(f"\nEtiquetado obtenido: {', '.join(prediction[0])}")
    print(f"\nEtiquetado correcto: {', '.join(y_test[0])}")


from pathlib import Path
from typing import Iterable, Iterator, Optional

from pipeline.es_doc import EsDoc
from pipeline.global_infra import GlobalInfra
from util.print import eprint

class Group:
    def __init__(
        self,
        name: str,
        logs: Iterable[EsDoc] = [],
        global_infra: Optional[GlobalInfra] = None,
        generation: Optional[int] = None,  # an optional integer to use in the safe name's suffix
    ):
        self.logs = logs
        self.name = name
        self.global_infra = global_infra
        self._generation = generation

    def __str__(self) -> str:
        """Logging-friendly representation"""
        optional_fields = []
        url = self.job_url()
        if url:
            optional_fields.append(f"url={url}")
        return f"<Group name={self.name} {' '.join(optional_fields)}>"

    def infer_global_infra(self) -> None:
        eprint(f"Inferring global infra for {str(self)}... (Log stream will be fully loaded into memory)")
        self.logs = list(self.logs)
        self.global_infra = GlobalInfra.fromLogs(self.logs)
        eprint(f"Done inferring global infra for {str(self)}.")

    @classmethod
    def group_name_to_pot_name(Self, gname: str) -> str:
        return gname.split("__")[-1].split("-")[0]

    def pot_name(self) -> str:
        # Example group ID: "boundary_nodes_pre_master__boundary_nodes_pot-2784039865"
        # Corresponding pot name: "boundary_nodes_pot"
        return self.group_name_to_pot_name(self.name)

    @staticmethod
    def is_group_name_local(jid: str) -> bool:
        return sum([1 for c in jid if c == "-"]) > 1

    def job_id(self) -> Optional[int]:
        # Example group IDs:
        # "boundary_nodes_pre_master__boundary_nodes_pot-2784039865"
        #   ==> test ran in CI
        # "boundary_nodes_pre_master__boundary_nodes_pot-username-zh1-spm99_zh7_dfinity_network-2784039865"
        #   ==> test ran locally (no CI job exists; None will be returned)
        # "boundary_nodes_pre_master__boundary_nodes_pot-2784039865--pseudo"
        #   ==> test ran in CI; replaying it via local log; CI job should exist
        # Corresponding job ID: 2784039865
        # Note:
        # - A group name might not have a CI job ID associated with it if it has been created for mainnet logs
        #   In that case, this method will return None.
        original_name = self.name.replace("--pseudo", "")
        if self.is_group_name_local(original_name):
            return None
        suffix = original_name.split("-")[-1]
        if suffix.isdigit():
            return int(suffix)
        return None

    def job_url(self) -> Optional[str]:
        id = self.job_id()
        if not id:
            return None
        else:
            return f"https://gitlab.com/dfinity-lab/public/ic/-/jobs/{id}"

    def safe_name(self) -> str:
        """Similar to self.name but avoids overriding existing files"""
        if self._generation is None:
            return self.name
        return f"{self.name}--{self._generation}"

    @staticmethod
    def _safe_log_stream(file: Path) -> Iterator[EsDoc]:
        with open(file, "r", encoding="utf-8") as fh:
            while True:
                line = fh.readline()
                if not line:
                    return
                obj_str = line.strip().strip("[],")
                if not obj_str:
                    continue
                repr = eval(obj_str)
                yield EsDoc(repr)

    @classmethod
    def _StreamFromFile(Self, log_file: Path) -> "Group":
        """Create group and set up a logs stream"""
        gname = Path(log_file).stem.split(".")[0] + "--pseudo"
        return Group(gname, logs=Self._safe_log_stream(log_file))

    @classmethod
    def _LoadFromFile(Self, log_file: Path) -> "Group":
        """Create group and load logs from file"""
        eprint(f"Loading logs from {log_file} ...")
        with open(log_file, "r") as input_file:
            all_doc_reprs = eval(input_file.read())
            logs = [EsDoc(repr) for repr in all_doc_reprs]
        eprint(f"Done loading logs from {log_file}.")
        # Example file name: "boundary_nodes_pre_master__boundary_nodes_pot-2784039865--0.raw.log"
        components = log_file.stem.split("--")
        return Group(name=components[0], logs=logs, generation=len(components) - 1)

    @classmethod
    def fromFile(Self, log_file: Path, as_stream=True) -> "Group":
        if as_stream:
            return Self._StreamFromFile(log_file)
        else:
            return Self._LoadFromFile(log_file)


from pathlib import Path
import rasterio
import numpy as np

def get_project_path():
    return Path(__file__).parent.parent.absolute()

project_path = get_project_path()
fb_path = project_path / 'data/humdata/population_nga_2018-10-01.tif'
grid_path = project_path / 'data/grid3/NGA - population - v1.2 - mastergrid.tif'

fb_raster = rasterio.open(fb_path)
grid_raster = rasterio.open(grid_path)

data_eye = np.eye(90, dtype=np.uint8)
data_zeros = np.zeros(90*90, dtype=np.uint8).reshape(90,90)
data_ones = np.ones(90*90, dtype=np.uint8).reshape(90,90)
data_left_edge = np.copy(data_zeros)
data_left_edge[:,0] = 1
data_top_edge = np.copy(data_zeros)
data_top_edge[0,:] = 1
data_right_edge = np.copy(data_zeros)
data_right_edge[:,-1] = 1
data_bottom_edge = np.copy(data_zeros)
data_bottom_edge[-1,:] = 1

files = {
    "eye": data_eye,
    "zeros": data_zeros,
    "ones": data_ones,
    "left_edge": data_left_edge,
    "top_edge": data_top_edge,
    "right_edge": data_right_edge,
    "bottom_edge": data_bottom_edge
}

for name,data in files.items():

    with rasterio.open( \
            project_path / f'test/data/pipeline/{name}.tif', \
            'w', \
            driver='GTiff', \
            width=data.shape[1], \
            height=data.shape[0], \
            count=1, \
            dtype=data.dtype, \
            crs=fb_raster.crs, \
            transform=fb_raster.transform \
            ) as dst:
        dst.write(data, indexes=1)

# with this window, not all pixels in the test raster will have corresponding pixel here
grid_data = grid_raster.read(1, window=((0,30),(0,30)))
with rasterio.open( \
        project_path / 'test/data/pipeline/grid3-30.tif', \
        'w', \
        driver='GTiff', \
        width=grid_data.shape[1], \
        height=grid_data.shape[0], \
        count=1, \
        dtype=grid_data.dtype, \
        crs=grid_raster.crs, \
        transform=grid_raster.transform \
        ) as dst:
    dst.write(grid_data, indexes=1)

# with this window, all pixels in the test raster will have corresponding pixel here
grid_data = grid_raster.read(1, window=((0,60),(0,60)))
with rasterio.open( \
        project_path / 'test/data/pipeline/grid3-60.tif', \
        'w', \
        driver='GTiff', \
        width=grid_data.shape[1], \
        height=grid_data.shape[0], \
        count=1, \
        dtype=grid_data.dtype, \
        crs=grid_raster.crs, \
        transform=grid_raster.transform \
        ) as dst:
    dst.write(grid_data, indexes=1)


from django.contrib import admin
from django.urls import path,include
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',views.welcome,name='welcome'),
    path('load_form',views.load_form,name='load_form'),
    path('add',views.add,name='add'),
    path('show',views.show,name='show'),
    path('edit/<int:id>',views.edit),
    path('update/<int:id>',views.update),
    path('delete/<int:id>',views.delete),
    path('search',views.search),
    path('register',views.register),
    path('addd',views.addd),
    path('watch',views.watch),
    path('editt/<int:id>',views.editt),
    path('Gro',views.Gro),
    path('details',views.details),
    path('Adddd',views.Adddd),
    path('regi', views.regi),
    path('Dele/<int:id>',views.Dele),
    path('sea',views.sea),

]


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Tests for the corpus module."""

import logging
from unittest import TestCase

from dphon.corpus import PlaintextCorpusLoader

class TestPlaintextCorpusLoader(TestCase):
    """Test the PlaintextCorpusLoader."""

    def setUp(self) -> None:
        """Create a loader for testing."""
        self.load = PlaintextCorpusLoader()

    def test_no_files(self) -> None:
        """should error if no valid files were found"""
        # pass an empty list of paths; should error since no files found
        with self.assertRaises(SystemExit):
            with self.assertLogs(level="ERROR"):
                for _ in self.load([]):
                    pass

    def test_non_file(self) -> None:
        """should warn if passed something that isn't plaintext"""
        # Pass a python file and a text file; should warn
        # re-enable logging so we can check it in the test
        logging.disable(logging.INFO)
        with self.assertLogs(level="WARNING"):
            for _ in self.load(["tests/unit/test_corpus.py",
                                "tests/fixtures/laozi/laozi.txt"]):
                pass

    def test_single_file(self) -> None:
        """should load contents of a single file"""
        # Use a tiny file with no whitespace, etc. to strip
        path = "tests/fixtures/laozi/tiny.txt"
        docs = list(self.load([path]))
        self.assertEqual(docs[0][1], {"id": "tiny"})
        with open(path, encoding="utf8") as file:
            self.assertEqual(docs[0][0], file.read())

    def test_glob(self) -> None:
        """should load all text files in a directory via glob"""
        docs = list(self.load(["tests/fixtures/laozi/*.txt"]))
        self.assertEqual(len(docs), 4)
        doc_ids = [doc[1]["id"] for doc in docs]
        self.assertIn("gd_laozi", doc_ids)
        self.assertIn("tiny", doc_ids)
        self.assertIn("mwd_laozi", doc_ids)
        self.assertIn("laozi", doc_ids)

    def test_file_and_glob(self) -> None:
        """should allow combination of files and globs"""
        docs = list(self.load(["tests/fixtures/laozi/*laozi.txt",
                               "tests/fixtures/laozi/tiny.txt"]))
        self.assertEqual(len(docs), 4)
        doc_ids = [doc[1]["id"] for doc in docs]
        self.assertIn("gd_laozi", doc_ids)
        self.assertIn("tiny", doc_ids)
        self.assertIn("mwd_laozi", doc_ids)
        self.assertIn("laozi", doc_ids)


from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.chrome.service import Service
import pandas as pd
import time
import json
import os
import datetime
from urllib.parse import urlparse
import csv
import re

# Obtener la fecha actual
fecha_actual = datetime.datetime.now()
# Formatear la fecha en AAAAMMDD
fecha_formateada = fecha_actual.strftime("%Y%m%d")
# Formatear la hora en HHMM
hora_formateada = fecha_actual.strftime("%H%M")
# Combinar la fecha y la hora con _
fecha_resultante = fecha_formateada + "_" + hora_formateada
# fecha_resultante = '20230723_2026'
# Obtener la ruta absoluta del directorio actual
directorio_actual = os.path.abspath(os.path.dirname(__file__))
# Nombre del directorio donde deseas guardar el archivo
directory_schedule = 'schedule'
# Crear la ruta completa del directorio
ruta_schedule = os.path.join(directorio_actual, directory_schedule)
# Comprobar si el directorio existe, si no, crearlo
if not os.path.exists(ruta_schedule):
    os.makedirs(ruta_schedule)
# Nombre del archivo que deseas guardar
nombre_archivo_schedule = fecha_resultante +'.txt'
nombre_archivo_schedule_only_link = fecha_resultante +'_link.txt'
# Combinar la ruta del directorio actual con el nombre del archivo
ruta_archivo_schedule = os.path.join(ruta_schedule, nombre_archivo_schedule)
ruta_archivo_schedule_only_link = os.path.join(ruta_schedule, nombre_archivo_schedule_only_link)

# ubicacion = "C:/Users/yfigueroa/Documents/GitHub/yfsDataBase/NBA2023/chromedriver"
# driver = webdriver.Chrome(ubicacion)
# Especifica la ruta al ejecutable de ChromeDriver
# para actualizar el chromedriver https://googlechromelabs.github.io/chrome-for-testing/#stable
ruta_chrome_driver = 'C:/Users/yfigueroa/Documents/GitHub/yfsDataBase/chromedriver.exe'  # Sustituye por tu ruta real
# Crea un objeto Service con la ruta del ejecutable
servicio_chrome = Service(ruta_chrome_driver)
# Crea una instancia de Chrome WebDriver utilizando el objeto Service
driver = webdriver.Chrome(service=servicio_chrome)

listGameTemp = []
listGameLink = []
listGameLinkOnly = []

# url de la data
schedule_link = "https://www.baseball-reference.com/leagues/majors/2023-schedule.shtml"
driver.get(schedule_link)
page = BeautifulSoup(driver.page_source,'html.parser')
#   div_4649142714
#   div_2554848825
#   div_4800524807
# div_1998533194
# div_9550324858
# div_3799355433
# div_1762894190 postseason
# div_2906366340 postseason

div_schedule = page.find('div', id='div_2906366340')
divs = div_schedule.find_all('div')
for div in divs:
    div_h3 = div.find('h3')
    listGameLink.append([div_h3.text])

    div_ps = div.find_all('p')
    # print(div_ps)
    # Bandera para saltar el último ciclo
    skip_last = False

    for i,div_p in enumerate(div_ps):
        # Verificamos si estamos en la penúltima iteración
        if i == len(div_ps) - 1:
            skip_last = True

        # Si la bandera es True, continuamos al siguiente ciclo sin hacer nada
        if skip_last:
            continue

        # print(div_p)
        p_em_a = div_p.find('em').find('a')
        listGameLink.append([p_em_a.text,p_em_a['href']])
        listGameLinkOnly.append(p_em_a['href'])

time.sleep(5)

with open(ruta_archivo_schedule, 'w') as archivo:
    for fila in listGameLink:
       result = ''
       for element in fila:
           result += str(element) +';'
       result = result[:-1] + '\n'
       archivo.write(result)
with open(ruta_archivo_schedule_only_link, 'w') as archivo:
    for fila in listGameLinkOnly:
       archivo.write("'" + str(fila) + "'," + "\n")


def isPalindrome(s):
   temp = [c for c in s]
   temp.reverse()
   return ''.join(temp) == s

def twodigit():
   return reversed(range(100, 999))

q = reversed(sorted([a * b for a in twodigit() for b in twodigit()]))

found = False

for s in q:
   if isPalindrome(str(s)):
      print(s)
      found = True
      break

if not found:
   print("guh")


import discord
from imdb_manager import IMDbMovieData, IMDbActorData

class GlobalDiscordMethods:
    latest_movie_query = None

    @classmethod
    async def display_actor_in_chat(cls, name, ctx):
        actor = IMDbActorData(name)

        embedded_msg = discord.Embed(title=str(actor.actor_obj), description="%s" % str(actor.biography),
                                     color=0x00ff00)

        name.replace(" ", "_")
        embedded_msg.add_field(name="IMDb page", value=f"{actor.url}\n\n"
                                                       f"View more entries for this query at:"
                                                       f"\nhttps://www.imdb.com/find?q={name.replace(' ', '_')}", inline=False)

        embedded_msg.set_thumbnail(url=actor.thumbnail)
        embedded_msg.set_image(url=actor.cover)

        await ctx.send(embed=embedded_msg)

    @classmethod
    async def display_movie_in_chat(cls, title, ctx):
        imdb = IMDbMovieData(title)
        GlobalDiscordMethods.latest_movie_query = imdb
        print("latest_movie_query set: " + GlobalDiscordMethods.latest_movie_query.title)

        embedded_msg = discord.Embed(title=imdb.title, description=imdb.plot, color=0x00ff00)
        embedded_msg.set_thumbnail(url=imdb.image)
        embedded_msg.add_field(name="Director", value=imdb.director, inline=False)
        embedded_msg.add_field(name="Cast", value=imdb.stars, inline=False)
        embedded_msg.add_field(name="Genres", value=imdb.genre, inline=False)
        embedded_msg.add_field(name="IMDb rating", value=imdb.rating, inline=False)
        embedded_msg.add_field(name="IMDb link", value=imdb.url, inline=False)
        embedded_msg.add_field(name="Release year", value=imdb.year, inline=False)
        embedded_msg.add_field(name="Runtime", value=f"{imdb.runtime}"
                                                     f"\n\nView more entries for this query at:"
                                                     f"\nhttps://www.imdb.com/find?q={imdb.title.replace(' ', '_')}"
                                                     f"\n\nWant to add this title to your watchlist? Type '!temflix save'", inline=False)

        await ctx.send(embed=embedded_msg)


# Provides terminal-based chat interface for RWKV model.
# Usage: python chat_with_bot.py C:\rwkv.cpp-169M.bin
# Prompts and code adapted from https://github.com/BlinkDL/ChatRWKV/blob/9ca4cdba90efaee25cfec21a0bae72cbd48d8acd/chat.py

import os
import argparse
import pathlib
import copy
import torch
import sampling
import rwkv_cpp_model
import rwkv_cpp_shared_library
from rwkv_tokenizer import get_tokenizer
import json
from typing import List, Dict, Optional
import time

# ======================================== Script settings ========================================

# English, Chinese, Japanese
LANGUAGE: str = 'English'
# QA: Question and Answer prompt to talk to an AI assistant.
# Chat: chat prompt (need a large model for adequate quality, 7B+).
PROMPT_TYPE: str = 'QA'

MAX_GENERATION_LENGTH: int = 250

# Sampling temperature. It could be a good idea to increase temperature when top_p is low.
TEMPERATURE: float = 0.8
# For better Q&A accuracy and less diversity, reduce top_p (to 0.5, 0.2, 0.1 etc.)
TOP_P: float = 0.5
# Penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.
PRESENCE_PENALTY: float = 0.2
# Penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.
FREQUENCY_PENALTY: float = 0.2

END_OF_LINE_TOKEN: int = 187
DOUBLE_END_OF_LINE_TOKEN: int = 535
END_OF_TEXT_TOKEN: int = 0

# =================================================================================================

parser = argparse.ArgumentParser(description='Provide terminal-based chat interface for RWKV model')
parser.add_argument('model_path', help='Path to RWKV model in ggml format')
parser.add_argument('tokenizer', help='Tokenizer to use; supported tokenizers: 20B, world', nargs='?', type=str, default='20B')
args = parser.parse_args()

script_dir: pathlib.Path = pathlib.Path(os.path.abspath(__file__)).parent

with open(script_dir / 'prompt' / f'{LANGUAGE}-{PROMPT_TYPE}.json', 'r', encoding='utf8') as json_file:
    prompt_data = json.load(json_file)

    user, bot, separator, init_prompt = prompt_data['user'], prompt_data['bot'], prompt_data['separator'], prompt_data['prompt']

assert init_prompt != '', 'Prompt must not be empty'

tokenizer_decode, tokenizer_encode = get_tokenizer(args.tokenizer)

library = rwkv_cpp_shared_library.load_rwkv_shared_library()
print(f'System info: {library.rwkv_get_system_info_string()}')

print('Loading RWKV model')
model = rwkv_cpp_model.RWKVModel(library, args.model_path)

# =================================================================================================

processed_tokens: List[int] = []
logits: Optional[torch.Tensor] = None
state: Optional[torch.Tensor] = None

def process_tokens(_tokens: List[int], new_line_logit_bias: float = 0.0) -> None:
    global processed_tokens, logits, state

    processed_tokens += _tokens

    for _token in _tokens:
        logits, state = model.eval(_token, state, state, logits)

    logits[END_OF_LINE_TOKEN] += new_line_logit_bias

state_by_thread: Dict[str, Dict] = {}

def save_thread_state(_thread: str) -> None:
    state_by_thread[_thread] = {
        'tokens': copy.deepcopy(processed_tokens),
        'logits': copy.deepcopy(logits),
        'state': copy.deepcopy(state)
    }

def load_thread_state(_thread: str) -> None:
    global processed_tokens, logits, state

    thread_state = state_by_thread[_thread]

    processed_tokens = copy.deepcopy(thread_state['tokens'])
    logits = copy.deepcopy(thread_state['logits'])
    state = copy.deepcopy(thread_state['state'])

# Model only saw '\n\n' as [187, 187] before, but the tokenizer outputs [535] for it at the end.
# See https://github.com/BlinkDL/ChatRWKV/pull/110/files
def split_last_end_of_line(tokens):
    if len(tokens) > 0 and tokens[-1] == DOUBLE_END_OF_LINE_TOKEN:
        tokens = tokens[:-1] + [END_OF_LINE_TOKEN, END_OF_LINE_TOKEN]

    return tokens

# =================================================================================================

processing_start = time.time()

prompt_tokens = tokenizer_encode(init_prompt)
prompt_token_count = len(prompt_tokens)
print(f'Processing {prompt_token_count} prompt tokens, may take a while')

process_tokens(split_last_end_of_line(prompt_tokens))

processing_duration = time.time() - processing_start

print(f'Processed in {int(processing_duration)} s, {int(processing_duration / prompt_token_count * 1000)} ms per token')

save_thread_state('chat_init')
save_thread_state('chat')

print(f'\nChat initialized! Your name is {user}. Write something and press Enter. Use \\n to add line breaks to your message.')

while True:
    # Read user input
    user_input = input(f'> {user}{separator} ')
    msg = user_input.replace('\\n', '\n').strip()

    temperature = TEMPERATURE
    top_p = TOP_P

    if '-temp=' in msg:
        temperature = float(msg.split('-temp=')[1].split(' ')[0])

        msg = msg.replace('-temp='+f'{temperature:g}', '')

        if temperature <= 0.2:
            temperature = 0.2

        if temperature >= 5:
            temperature = 5

    if '-top_p=' in msg:
        top_p = float(msg.split('-top_p=')[1].split(' ')[0])

        msg = msg.replace('-top_p='+f'{top_p:g}', '')

        if top_p <= 0:
            top_p = 0

    msg = msg.strip()

    # + reset --> reset chat
    if msg == '+reset':
        load_thread_state('chat_init')
        save_thread_state('chat')
        print(f'{bot}{separator} Chat reset.\n')
        continue
    elif msg[:5].lower() == '+gen ' or msg[:3].lower() == '+i ' or msg[:4].lower() == '+qa ' or msg[:4].lower() == '+qq ' or msg.lower() == '+++' or msg.lower() == '++':

        # +gen YOUR PROMPT --> free single-round generation with any prompt. Requires Novel model.
        if msg[:5].lower() == '+gen ':
            new = '\n' + msg[5:].strip()
            state = None
            processed_tokens = []
            process_tokens(tokenizer_encode(new))
            save_thread_state('gen_0')

        # +i YOUR INSTRUCT --> free single-round generation with any instruct. Requires Raven model.
        elif msg[:3].lower() == '+i ':
            new = f'''
Below is an instruction that describes a task. Write a response that appropriately completes the request.

# Instruction:
{msg[3:].strip()}

# Response:
'''
            state = None
            processed_tokens = []
            process_tokens(tokenizer_encode(new))
            save_thread_state('gen_0')

        # +qq YOUR QUESTION --> answer an independent question with more creativity (regardless of context).
        elif msg[:4].lower() == '+qq ':
            new = '\nQ: ' + msg[4:].strip() + '\nA:'
            state = None
            processed_tokens = []
            process_tokens(tokenizer_encode(new))
            save_thread_state('gen_0')

        # +qa YOUR QUESTION --> answer an independent question (regardless of context).
        elif msg[:4].lower() == '+qa ':
            load_thread_state('chat_init')

            real_msg = msg[4:].strip()
            new = f'{user}{separator} {real_msg}\n\n{bot}{separator}'

            process_tokens(tokenizer_encode(new))
            save_thread_state('gen_0')

        # +++ --> continue last free generation (only for +gen / +i)
        elif msg.lower() == '+++':
            try:
                load_thread_state('gen_1')
                save_thread_state('gen_0')
            except Exception as e:
                print(e)
                continue

        # ++ --> retry last free generation (only for +gen / +i)
        elif msg.lower() == '++':
            try:
                load_thread_state('gen_0')
            except Exception as e:
                print(e)
                continue
        thread = 'gen_1'

    else:
        # + --> alternate chat reply
        if msg.lower() == '+':
            try:
                load_thread_state('chat_pre')
            except Exception as e:
                print(e)
                continue
        # chat with bot
        else:
            load_thread_state('chat')
            new = f'{user}{separator} {msg}\n\n{bot}{separator}'
            process_tokens(tokenizer_encode(new), new_line_logit_bias=-999999999)
            save_thread_state('chat_pre')

        thread = 'chat'

        # Print bot response
        print(f'> {bot}{separator}', end='')

    start_index: int = len(processed_tokens)
    accumulated_tokens: List[int] = []
    token_counts: Dict[int, int] = {}

    for i in range(MAX_GENERATION_LENGTH):
        for n in token_counts:
            logits[n] -= PRESENCE_PENALTY + token_counts[n] * FREQUENCY_PENALTY

        token: int = sampling.sample_logits(logits, temperature, top_p)

        if token == END_OF_TEXT_TOKEN:
            print()
            break

        if token not in token_counts:
            token_counts[token] = 1
        else:
            token_counts[token] += 1

        process_tokens([token])

        # Avoid UTF-8 display issues
        accumulated_tokens += [token]

        decoded: str = tokenizer_decode(accumulated_tokens)

        if '\uFFFD' not in decoded:
            print(decoded, end='', flush=True)

            accumulated_tokens = []

        if thread == 'chat':
            if '\n\n' in tokenizer_decode(processed_tokens[start_index:]):
                break

        if i == MAX_GENERATION_LENGTH - 1:
            print()

    save_thread_state(thread)


from picosdk.ps2000 import ps2000 as ps
unit_location_dict = {
    "left_bottom": b"JY214/1608",
    "right_bottom": b"JY214/1781",
}
def get_units():
    '''finds units and sorts them into a dict for ease of initilization and matching with location'''
    units = ps.list_units()
    print("{} units found".format(len(units)))
    #match unit with location
    for unit in units:
        serial = unit.serial
        for k,v in unit_location_dict.items():
            if v == serial: break
        if v == serial: unit_location_dict[k] = unit
        else: print("unit {} not matched".format(serial))
    return unit_location_dict


import unittest

class Calculator():
    def calcHelper(operation, num1, num2):
        if (num1.isnumeric() and num2.isnumeric()):
            if operation == "ADD":
                Calculator.calcAdd(int(num1), int(num2));
            elif operation == "SUB":
                Calculator.calcSubtract(int(num1), int(num2));
            elif operation == "MULT":
                Calculator.calcMultiply(int(num1), int(num2));
            elif operation == "DIV":
                Calculator.calcDivide(int(num1), int(num2))
            elif operation == "MOD":
                Calculator.calcModulus(int(num1), int(num2))
            elif operation == "ESC":
                return;
            else:
                print("Bad selecttion.")
        else:
            print("Bad numbers entered.")

    def calcAdd(a, b):
        answer = a + b
        print(str(a),"+",str(b),"=", str(answer), "\n")
        return answer;
    def calcSubtract(a, b):
        answer = a - b
        print(str(a),"-",str(b),"=", str(answer), "\n")
        return answer;
    def calcMultiply(a, b):
        answer = a * b
        print(str(a),"*",str(b),"=", str(answer), "\n")
        return answer;
    def calcDivide(a, b):
        answer = a / b;
        print(str(a),"/",str(b),"=", str(answer), "\n")
        return answer;
    def calcModulus(a, b):
        answer = a % b;
        print(str(a),"%",str(b),"=", str(answer), "\n")
        return answer;

class TestProgram(unittest.TestCase):
    def test_case_1(self):
        expected = 11
        actual = Calculator.calcAdd(5,6)
        print(actual)
        self.assertEqual(actual, expected)
    def test_case_2(self):
        expected = 3
        actual = Calculator.calcSubtract(10,7)
        self.assertEqual(actual, expected)
    def test_case_3(self):
        expected = 12
        actual = Calculator.calcMultiply(3,4)
        self.assertEqual(actual, expected)
    def test_case_4(self):
        expected = 20
        actual = Calculator.calcDivide(100, 5)
        self.assertEqual(actual, expected)
    def test_case_5(self):
        expected = 1
        actual = Calculator.calcModulus(10,3)
        self.assertEqual(actual, expected)

def main() -> None:
    math = input("Would you like to ADD, SUB, MULT, DIV, or MOD?")
    firstNumber = input("Enter the first number:")
    secondNumber = input("Enter the second number:")
    Calculator.calcHelper(math, firstNumber, secondNumber)
    TestProgram.test_case_1();

if __name__ == "__main__":
    main();


import json, ast
from optparse import OptionParser
from datetime import datetime
from elasticsearch import Elasticsearch

def get_options():
    parser=OptionParser()
    parser.add_option('-s', '--es-host', dest='es_host', help='ES host name')
    parser.add_option('-o', dest='es_output', help='ES Output file name')
    (options, args) = parser.parse_args()

    for arg in required_args():
        if getattr(options, arg) is None:
            parser.error('Missing required argument %s' %arg)

    return options

def required_args():
    return ['es_host']

if __name__ == '__main__':
    start_time = datetime.now()
    options = get_options()


from rest_framework import serializers

from subscriptions.models import SubscriptionPlan

class SubscriptionPlanSerializer(serializers.ModelSerializer):
    apple_product_id = serializers.SerializerMethodField()
    price = serializers.SerializerMethodField()

    def get_apple_product_id(self, plan):
        if 'request' not in self.context:
            return plan.apple_product_id

        user = self.context['request'].user
        if user.is_anonymous:
            return plan.apple_product_id
        return plan.apple_product_id_notrial

    def get_price(self, plan):
        return str(plan.get_price_card().price)

    class Meta:
        model = SubscriptionPlan
        fields = (
            'id',
            'name',
            'price',
            'period',
            'trial_days',
            'apple_product_id',
            'google_product_id',
        )


import pandas as pd
import seaborn as sns
import numpy as np
import os
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.dates as mdates
from datetime import date, timedelta
from simtools.Analysis.BaseAnalyzers import BaseAnalyzer

mpl.rcParams['pdf.fonttype'] = 42

class InsetAnalyzer(BaseAnalyzer):
    def __init__(self, exp_name, working_dir='.', sweep_variables=None):  # sweep_variables=None,

        super(InsetAnalyzer, self).__init__(working_dir=working_dir,
                                            filenames=["output/InsetChart.json"]
                                            )
        self.sweep_variables = sweep_variables or ["scenario", "ITN_coverage"]
        self.inset_channels = ['Infected', 'New Infections', 'Adult Vectors', 'Daily EIR']
        #self.sweep_variables = sweep_variables or ["start_days", 'Run_Number', 'krate_scale_factor_s']
        self.exp_name = exp_name
        # self.inset_channels = ['Infected', 'New Infections', 'Adult Vectors', 'Daily EIR']

    # def filter(self, simulation):
    #    return simulation.tags["Run_Number"] == 0

    def select_simulation_data(self, data, simulation):
        """
        Extract data from output data and accumulate in same bins as reference.
        """

        # Load data from simulation
        simdata = pd.DataFrame( { x : data[self.filenames[0]]['Channels'][x]['Data'] for x in self.inset_channels})
        simdata['day'] = simdata.index

        for sweep_var in self.sweep_variables :
            simdata[sweep_var] = simulation.tags[sweep_var]

        return simdata

    def finalize(self, all_data):

        selected = [data for sim, data in all_data.items()]
        if len(selected) == 0:
            print("No data have been returned... Exiting...")
            return

        df = pd.concat(selected, sort=False).reset_index(drop=True)

       # df['krate'] = df['krate_scale_factor_s'].apply(lambda x : 'old' if x == 1 else 'new') # comment out

        sns.set_style('whitegrid', {'axes.linewidth': 0.5})

        fig = plt.figure(figsize=(12, 8))
        formatter = mdates.DateFormatter("%m-%Y")
        axes = [fig.add_subplot(2,2, x+1) for x in range(len(self.inset_channels))]
        fig.autofmt_xdate()
        palette = sns.color_palette('Set1')

        df['date'] = df['day'].apply(lambda x : date(2013, 1, 1) + timedelta(days=x))

        for ia, (a, adf) in enumerate(df.groupby('scenario')): # comment out block

            for ax, channel in zip(axes, self.inset_channels) :
                gdf = adf.groupby('date')[channel].agg([np.min, np.max, np.mean]).reset_index()
                ax.plot(gdf['date'], gdf['mean'], '-', label=a, color=palette[ia])
                #ax.fill_between(gdf['date'], gdf['amin'], gdf['amax'], linewidth=0, color=palette[ia], alpha=0.3)
                ax.set_ylabel(channel)
                ax.legend()
                ax.xaxis.set_major_formatter(formatter)
                ax.xaxis.set_major_locator(mdates.MonthLocator(interval=3))
        plt.show()

if __name__ == "__main__":

    from simtools.Analysis.AnalyzeManager import AnalyzeManager
    from simtools.SetupParser import SetupParser

    SetupParser.default_block = 'HPC'
    SetupParser.init()

    analyzer = InsetAnalyzer(exp_name='Spatial IR Rotation ITNs')

    am = AnalyzeManager('eef6b34b-f4d2-ec11-a9f8-b88303911bc1', analyzers=analyzer)
    am.analyze()


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

import requests
from bs4 import BeautifulSoup
import time
import random

link = "http://www.santostang.com/"

def scrap(link):
    headers = {'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
    r = requests.get(link, headers= headers)
    html = r.text
    soup = BeautifulSoup(html, "lxml")
    return soup

soup = scrap(link)
title_list = soup.find_all("h1", class_="post-title")
for eachone in title_list:
    url = eachone.a['href']
    print ('开始爬取这篇博客: ', url)
    soup_article = scrap(url)
    title = soup_article.find("h1", class_="view-title").text.strip()
    print ('这篇博客的标题为: ', title)
    sleep_time = random.randint(0,2) + random.random()
    print ('开始休息: ', sleep_time, '秒')
    time.sleep(sleep_time)

'''
scrap_times = 0
for eachone in title_list:
    url = eachone.a['href']
    print ('开始爬取这篇博客: ', url)
    soup_article = scrap(url)
    title = soup_article.find("h1", class_="view-title").text.strip()
    print ('这篇博客的标题为: ', title)

    scrap_times += 1
    if scrap_times % 5 == 0:
        sleep_time = 10 + random.random()
    else:
        sleep_time = random.randint(0,2) + random.random()
    time.sleep(sleep_time)
    print ('开始休息: ', sleep_time, '秒')
'''