import cv2 as cv
import numpy as np
import threading
import os
import time
import datetime
from upload import s3
import firebase_admin
from firebase_admin import credentials, db
import multiprocessing as mp
from multiprocessing import shared_memory
import sys

cred = credentials.Certificate("your firebase credit path")
firebase_admin.initialize_app(cred,
                              {'databaseURL' : 'your firebase url'})

datapath='video'
ref=db.reference(datapath)
fps= 30
dst='./test.mp4'    #저장 위치

event_flag=np.array([0])

save_second=3   #몇초 저장할건지

queue_size = save_second*30
queue = []
class Sdata:
    def __init__(self, cam_no):
        self.cam_no=cam_no
        self.event_type=0
        self.link_storage=''
        self.date=''
        self.time=''

def Vsave(sdata, filename):
    global event_flag
    ref=db.reference('flag')
    ref.set(str(event_flag[0]))
    s3.upload_file(filename,"sjmama1",filename)
    print('s3 upload!')
    sdata.link_storage='https://your_s3_storage_url/'+sdata.date+'%'+sdata.date[2:4]+sdata.time+'.mp4'
    sdata_dict=sdata.__dict__
    print(sdata_dict)
    ref=db.reference('video')#저장한 파일은 삭제
    ref.push(sdata_dict)
    print('firebase push!')
    os.remove(filename)

def showNsave(cam_no):
    global cap
    global event_flag
    ret, frame = cap.read()
    ref=db.reference('flag')
    ref.set(str('0'))
    print(str(frame.shape).replace(' ', ''))
    os.system('echo '+str(frame.shape).replace(' ', '').replace('(', '').replace(')','')+' > info.txt')
    sevent_flag=np.array([0])
    print(sevent_flag.nbytes)
    flagshm=shared_memory.SharedMemory(create=True, size=sevent_flag.nbytes)
    print('flag= '+flagshm.name)
    os.system('echo '+flagshm.name+' >> info.txt')
    event_flag = np.ndarray(sevent_flag.shape, dtype=sevent_flag.dtype, buffer=flagshm.buf)
    event_flag[:]=sevent_flag[:]
    sdata=Sdata(cam_no)
    innerflag=0
    print(frame.shape, frame.dtype)
    fourcc = cv.VideoWriter_fourcc(*'mp4v')
    frameshm=shared_memory.SharedMemory(create=True, size=frame.nbytes)
    os.system('echo '+frameshm.name+' >> info.txt')
    print('frame= '+frameshm.name)
    out = cv.VideoWriter(dst, fourcc, fps, (frame.shape[1], frame.shape[0]))
    while cap.isOpened():
        ret, frame = cap.read()
        sharedframe=np.ndarray(frame.shape, dtype=frame.dtype, buffer=frameshm.buf)
        sharedframe[:]=frame[:]
        # if frame is read correctly ret is True
        if not ret:
            print("Can't receive frame (stream end?). Exiting ...")
            break
        #cv.imshow('frame', frame)
        queue.append(frame)
        if len(queue) > queue_size:
            queue.pop(0)
        if innerflag==0 and event_flag[0]!=0:#여기는 위험 신호 감지해서 저장하게 해야하고
            print(event_flag[0])
            ref=db.reference('flag')
            if event_flag[0]==12:
                ref.set(str(2))
            else:
                ref.set(str(event_flag[0]))
            sdata.event_type=str(event_flag[0])
            innerflag=event_flag[0]
            now = datetime.datetime.now()
            sdata.date=now.strftime("%Y-%m-%d")
            sdata.time=now.strftime("%Hh%Mm%Ss")
            new_filename = f"{sdata.date}#{sdata.time}.mp4"
            for f in queue:
                out.write(f)
        elif innerflag!=0 and event_flag[0]!=0:
            out.write(frame)
            if (innerflag != event_flag[0]) :
                innerflag=event_flag[0]
                ref=db.reference('flag')
                if event_flag[0]==12:
                    ref.set(str(2))
                else:
                    ref.set(str(event_flag[0]))
        elif innerflag!=0 and event_flag[0]==0:
            print(event_flag[0])
            out.release()
            os.rename("test.mp4", new_filename)
            Vsaveth=threading.Thread(target=Vsave, args=(sdata, new_filename))
            Vsaveth.start()
            innerflag=event_flag[0]
            out = cv.VideoWriter(dst, fourcc, fps, (frame.shape[1], frame.shape[0]))

    out.release()
    cap.release()

# fctl=threading.Thread(target=flagcontrol, args=())
# fctl.daemon=True
def main():
    global cap
    cam_no='0000'
    cap = cv.VideoCapture('rtmp://stramserverip/stream app name/'+cam_no)
    S_S=threading.Thread(target=showNsave, args=(cam_no,))#show and save
    # fctl.start()
    if os.path.exists(dst):
        os.remove(dst)
    S_S.start()
    S_S.join()
    cv.destroyAllWindows()

if __name__ == "__main__":
    main()


#! /usr/bin/env python
import xml.etree.ElementTree as ET
from collections import defaultdict
from pathlib import Path

class FappXml:
    def __init__(self, path, region_name="kernel", cmg=0):
        self.region_name = region_name
        self.cmg = cmg
        self.fill_xmls(path)
        self.fill_event_dict()
        self.fill_cmg_tids()

    def fill_xmls(self, path):
        pa_dir = Path(path).expanduser()
        filenames = list(pa_dir.glob("pa*.xml"))  # need list here
        self.xmls = [None for _ in filenames]
        for file in filenames:
            idx = int(file.name[2:-4]) - 1
            self.xmls[idx] = ET.parse(file)
        assert len(self.xmls) == 17, "Didn't find all 17 paN.xml files"

    def fill_event_dict(self):
        self.event_dict = defaultdict(list)
        query = f"./information/region[@name='{self.region_name}']"
        query += "/spawn/process/thread/cpupa/event"
        for idx, xml in enumerate(self.xmls):
            for event in xml.getroot().findall(query):
                event_name = event.get("name")
                self.event_dict[event_name].append(idx)
        assert self.event_dict, (
            "No events found in the given region! "
            "Is the correct --roi specified? (see --help)"
        )

    def fill_cmg_tids(self):
        query = "./environment/spawn/process/thread"
        self.cmg_tids = []
        for thread in self.xmls[0].findall(query):
            if self.cmg == int(thread.find("cmg").attrib["id"]):
                self.cmg_tids.append(thread.attrib["id"])
        assert len(
            self.cmg_tids
        ), "No processes found for given CMG -- this shouldn't happen."

    def get_event(self, event_name, thread_id):
        assert 0 <= thread_id and thread_id < len(self.cmg_tids), "Wrong thread id"
        result = ""
        if event_name == "LABEL-FAPP-cpupa":
            query = f"./information/region[@name='{self.region_name}']"
            query += f"/spawn/process/thread[@id='{thread_id}']"

            if self.xmls[0].findall(f"{query}/cpupa"):
                result = "FAPP-cpupa"
            return result
        else:
            tid = self.cmg_tids[thread_id]
            idx = self.event_dict[event_name][0]
            query = f"./information/region[@name='{self.region_name}']"
            query += f"/spawn/process/thread[@id='{tid}']"
            query += f"/cpupa/event[@name='{event_name}']"
            element = self.xmls[idx].find(query)
            if element is not None:
                result = int(element.text)
        return result

    # Single values
    def get_measured_time(self):
        query = "./environment/measured_time"
        return self.xmls[0].find(query).text

    def get_node_name(self):
        query = "./environment/spawn/process/host"
        return self.xmls[0].find(query).get("name")

    def get_process_no(self):
        query = "./environment/spawn/process"
        return int(self.xmls[0].find(query).get("id"))

    def get_cmg_no(self):
        query = "./environment/spawn/process/thread/cmg"
        return int(self.xmls[0].find(query).get("id"))

    def get_measured_region(self):
        query = f"./information/region[@name='{self.region_name}']"
        elem = self.xmls[0].find(query)
        return [elem.get("name"), int(elem.get("id"))]

    def get_vector_length(self):
        query = "./environment/vector_length"
        return int(self.xmls[0].find(query).get("vlen"))

    def get_counter_timer_freq(self):
        query = "./environment/spawn/process/cntfrq"
        return int(self.xmls[0].find(query).text)


import tensorflow as tf
from models import encoder_decoder

class Transformer(tf.keras.Model):
    def __init__(self, *, num_layers, d_model, num_heads, dff,
                 input_vocab_size, target_vocab_size, dropout_rate=0.1):
        super().__init__()
        self.encoder = encoder_decoder.Encoder(num_layers=num_layers, d_model=d_model,
                                               num_heads=num_heads, dff=dff,
                                               vocab_size=input_vocab_size,
                                               dropout_rate=dropout_rate)

        self.decoder = encoder_decoder.Decoder(num_layers=num_layers, d_model=d_model,
                                               num_heads=num_heads, dff=dff,
                                               vocab_size=target_vocab_size,
                                               dropout_rate=dropout_rate)

        self.final_layer = tf.keras.layers.Dense(target_vocab_size)

    def call(self, inputs):
        # To use a Keras model with `.fit` you must pass all your inputs in the
        # first argument.
        context, x = inputs

        context = self.encoder(context)  # (batch_size, context_len, d_model)

        x = self.decoder(x, context)  # (batch_size, target_len, d_model)

        logits = self.final_layer(x)  # (batch_size, target_len, target_vocab_size)

        return logits

    def get_config(self):
        config = super().get_config().copy()
        config.update({
            'encoder': self.encoder,
            'decoder': self.decoder,
            'final_layer': self.final_layer
        })

        return config


#! /bin/env python
#

from argparse import ArgumentParser, FileType
from collections import defaultdict

HEADERS = ("operation", "key")

class LazyOpener(dict):

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        for stream in self.values():
            stream.close()
        self.clear()

    def __missing__(self, key):
        self[key] = open(key ,'w')
        # print(','.join(HEADERS), file=self[key])
        return self[key]

def partname(filename, index):
    try:
        filename, suffix = filename.rsplit('.', 1)
    except ValueError:
        return "%s-%i" % (filename, index)
    return "%s-%i.%s" % (filename, index, suffix)

def _main(source, count):
    files = {partname(source.name, i): 0 for i in range(count)}
    key2file = defaultdict(lambda : min(files, key=lambda x: files[x]))
    with LazyOpener() as streams:
        for line in source:
            operation, key, _ = line.split(',')
            _file = key2file[key]
            print(','.join((operation, key)), file=streams[_file])
            files[_file] += 1

def main(args):
    for stream in args.path:
        stream.readline()
        _main(stream, args.count)

def parser():
    _parser = ArgumentParser()
    _parser.add_argument("path", nargs='+', type=FileType('r'))
    _parser.add_argument("count", type=int)
    return _parser

if __name__ == "__main__":
    main(parser().parse_args())


# zad 8
krotka1 = (111002, "Anna", "Mikulak")
krotka2 = (111001, "Anna2", "Mikulak1")
krotka3 = (111003, "Anna3", "Mikulak2")
krotka4 = (111004, "Anna4", "Mikulak3")
lista = [krotka1, krotka2, krotka3, krotka4]

# zad 9
slownik = {}
slownik = dict({krotka1[0]:[krotka1[1], krotka1[2]],krotka2[0]:[krotka2[1], krotka2[2]],krotka3[0]:[krotka3[1], krotka3[2]],krotka4[0]:[krotka4[1], krotka4[2]], "151100": ["Anna", "Mikulak", "ann@bill.com", "1999", "Olsztyn"]})
print(slownik)


from pathlib import Path
from setuptools import setup, find_packages

HERE = Path(__file__).parent

README = (HERE / 'README.md').read_text()

VERSION = '2.0.0'

setup(
        name='txtv',
        version=VERSION,
        description='CLI for reading swedish text-tv',
        long_description=README,
        long_description_content_type='text/markdown',
        url='https://github.com/voidcase/txtv',
        download_url=f'https://github.com/voidcase/txtv/archive/v{VERSION}.tar.gz',
        author='Isak Lindhé',
        author_email='isak.e.lindhe@gmail.com',
        license='GPLv3+',
        py_modules=['txtv'],
        packages=find_packages(),
        python_requires='>=3.6',
        classifiers=[
            'Programming Language :: Python :: 3',
            'Programming Language :: Python :: 3.6',
            'Programming Language :: Python :: 3.7',
            'Environment :: Console',
            'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)',
            'Operating System :: POSIX',
            'Operating System :: MacOS',
            'Topic :: Internet :: WWW/HTTP :: Dynamic Content :: News/Diary',
            'Natural Language :: Swedish',
            ],
        install_requires=[
            'beautifulsoup4',
            'colorama',
            'requests',
            ],
        entry_points={
            'console_scripts': [
                'txtv=txtv.txtv:run',
            ],
        }
)


import pygame

from direction import Direction
from entity import Entity
from game_state import GameState
from model import Model
from world import World

BACKGROUND = (0, 0, 0)

class Renderer:
    display: pygame.display
    fontTitle: pygame.font
    font: pygame.font
    heartTexture: pygame.image

    def __init__(self, display: pygame.display):
        self.display = display
        self.font = pygame.font.Font(pygame.font.get_default_font(), 20)
        self.fontTitle = pygame.font.Font(pygame.font.get_default_font(), 40)
        self.heartTexture = pygame.image.load('img/heart.png')

    def renderModel(self, model: Model):
        x = model.position.x
        y = model.position.y
        ox = model.offset.x
        oy = model.offset.y
        startX = 30
        startY = 80
        offset = 16

        texture = model.texture.value
        direction = model.direction

        if direction is Direction.UP:
            texture = pygame.transform.rotate(texture, 90)
        elif direction == Direction.DOWN:
            texture = pygame.transform.rotate(texture, -90)
        elif direction == Direction.LEFT:
            texture = pygame.transform.flip(texture, True, False)

        position = (startX + x*32 - ox*offset, startY + y*32 - oy*offset)
        self.display.blit(texture, position)

    def renderInfo(self, world:World):
        title = self.fontTitle.render('PACMAN', False, (252, 186, 3))
        self.display.blit(title, (240, 20))
        startX = 40
        startY = 20
        for i in range(world.lives):
            position = (startX + 40 * i, startY)
            self.display.blit(self.heartTexture, position)
        points = self.font.render('Punkty:  ' + str(int(world.score)), False, (255, 255, 255))
        self.display.blit(points, (500, 30))

    def render(self, world: World):
        self.display.fill(BACKGROUND)
        if world.gameState is GameState.WON:
            end = self.fontTitle.render('YOU WON', False, (252, 186, 3))
            self.display.blit(end, (240, 350))
        elif world.gameState is GameState.LOST:
            end = self.fontTitle.render('YOU LOST', False, (252, 186, 3))
            self.display.blit(end, (240, 350))
        else:
            self.renderInfo(world)
            for row in world.grid:
                for list in row:
                    for entity in list:
                        if isinstance(entity, Entity):
                            model = entity.model()
                            self.renderModel(model)
        pygame.display.update()


from player import Player
from map import Map
from event import battle

class Game:
    def __init__(self, num_player):
        self.num_player = num_player
        self.players = []
        for player_id in range(num_player):
            self.players.append(Player(player_id))
        self.map = Map()
        self.global_time = 0

    def __str__(self):
        return "time : %d, target location : %d" % (self.global_time, self.map.target_location)

    def time_goes_by(self):
        self.global_time += 1

        for p in self.players:
            if p.status == "alive":
                p.run(self.map.target_location)
                self.map.safe_zone_effect(p, self.global_time)
            #p_in_safe_zone = self.map.is_in_safe_zone(p.location, self.global_time)

            p.status_update()
            print(p)
            #if p_in_safe_zone:
            #   print("\t is in safe zone.")

            for i in range(len(self.players)):
                if self.players[i].status == "alive":
                    players_to_fight = [self.players[i]]
                    for j in range(len(self.players)):
                        if i == j:
                            continue
                        if self.players[i].location == self.players[j].location:
                            if self.players[j].status == "alive":
                                players_to_fight.append(self.players[j])
                    if len(players_to_fight)>1:
                        battle(players_to_fight)

                    for p in players_to_fight:
                        p.status_update()
        print(self)
        print("=" * 40)

if __name__ == "__main__":
    g = Game(30)
    while True:
        g.time_goes_by()
        alive_players = [player for player in g.players if player.status == "alive"]
        if len(alive_players) <= 1:
            if len(alive_players) == 1:
                print("The Winner is Player " + str(alive_players[0].id))
            break

    #for t in range(4):
    #    g.time_goes_by()


import time
import DFA_config
from progress.bar import IncrementalBar
from progress.spinner import Spinner
from DFA_functionality import *

### Experiment 0: How many among random @DFA_config.max_count are synchro
"""
def test_0():
    generated_automata = 0
    non_synchro_count = 0
    bar = Spinner("While loops looking for non_synchro  ")
    while generated_automata < DFA_config.max_count:
        bar.next()
        generated_automata += find_non_synchro()
        non_synchro_count += 1

    bar.finish()
    print("Ex0. Among {} random DFA (with empty) synchro is {}".format(generated_automata, (DFA_config.max_count - non_synchro_count)))
"""

### Experiment 1: How many NON_EMPTY unique DFA among @DFA_config.max_count are synchro

### RESULT: number of unique (NON_EMPTY) automata raises very quickly, for example for (states, transitions, alphabet)
### (4 4 1) it's 256, (4 4 2) it's 4096, (4 4 3) it's 20736?, (5 5 1 3125), witn (n n 1) it's n^n
### (20 20 1) it's more than 170k
### Without NON_EMPTY restriction number of unique DFAs raises even more drastic
def test_1():
    print()
    print("Test 1")
    ### to avoid infinite loops if @DFA_config.max_count is bigger than unique possible
    failed_count_loop = 0
    failed_count_total = 0
    max_failed_count = DFA_config.max_count
    added_dfa = 0
    unique_dfa_arr = []
    synchro_unique = 0
    look_bar = IncrementalBar('looking for unique automata ', max = DFA_config.max_count)
    while added_dfa < DFA_config.max_count:
        if failed_count_loop >= DFA_config.max_count:
            print()
            print ("in experiment 1 @DFA_config.max_count is bigger than unique DFA possible")
            print("Found unique DFA number is ", len(unique_dfa_arr))
            break
        unique_dfa = generate_non_empty_DFA()
        unique_dfa = reorder_transitions(unique_dfa)
        if unique_dfa in unique_dfa_arr:
            failed_count_total += 1
            failed_count_loop += 1
        else:
            unique_dfa_arr.append(unique_dfa)
            added_dfa += 1
            look_bar.next()
            failed_count_loop = 0

    print()
    check_bar = IncrementalBar('checking unique automata', max = DFA_config.max_count)
    for dfa in unique_dfa_arr:
        check_bar.next()
        if check_synchro(dfa) == 1:
            synchro_unique += 1

    print()
    print("Among {} unique DFA (without empty) synchro is {}".format(added_dfa, synchro_unique))
    print("Meanwhile generated repeating DFA {}".format(failed_count_total))

### RESULT of comparing Experiment 0 and Experiment 1: almost no difference if we use unique automata or random
### because with number big enough numbers (let's say starting with 8) chance of collision is less than 1/8^8

### Experiment 2: how many random NON_EMPTY DFAs among @DFA_config.max_count are synchro
def test_2():
    print()
    print("Test 2")
    tested_dfa = 0
    synchro_random = 0
    bar2 = IncrementalBar("Searching ", max = DFA_config.max_count)
    empty_generated = 0
    while tested_dfa < DFA_config.max_count:
        random_dfa = generate_non_empty_DFA()
        if check_synchro(random_dfa) == 1:
            synchro_random += 1
        bar2.next()
        tested_dfa += 1
    print()
    print('''Among {} random DFA (without empty) synchro is {}
            '''.format(tested_dfa, synchro_random))
    return synchro_random

### Experiment 3: how many random DFAs (with empty DFA inclusive) among @DFA_config.max_count are synchro
def test_3():
    print()
    print("Test 3")
    tested_dfa = 0
    synchro_random = 0
    bar3 = IncrementalBar('Searching', max = DFA_config.max_count)
    while tested_dfa < DFA_config.max_count:
        random_dfa = generate_DFA()
        if check_synchro(random_dfa) == 1:
            synchro_random += 1

        bar3.next()
        tested_dfa += 1

    print()
    print("Among {} random DFA (with empty) synchro is {}".format(tested_dfa, synchro_random))
    return synchro_random

"""
    ALERT:
    Experiment 0 and 3 have identic aim, but different approaches.
    Looks like values are different. To be tested
"""

### Experiment 4: how many DFA with at least one empty state among @DFA_config.max_count are synchro

### Important: if @DFA_config.max_transition_number == @DFA_config.max_states_number * @DFA_config.max_alphabet_number, empty DFA doesn't exist
### So be sure to pick up number suitable for this test
def test_4():
    print()
    print("Test 4")
    empty_tested = 0
    synchro_empty = 0
    non_empty_generated = 0
    bar4 = IncrementalBar("Searching ", max = DFA_config.max_count)
    while empty_tested < DFA_config.max_count:
        failed_count = 0
        bar4.next()
        empty_dfa = generate_DFA()
        while not check_if_empty(empty_dfa):
            if failed_count > DFA_config.max_count * 100:
                print()
                print("a lot of non empty generated. No more empties found. Exit with data obtained so long")
                print("Among {} empty DFA synchro is {}". format(empty_tested, synchro_empty))
                print("Meanwhile generated non empty DFA {}".format(non_empty_generated))
                return synchro_empty
            failed_count += 1
            non_empty_generated += 1
            empty_dfa = generate_DFA()
        if (check_synchro(empty_dfa) == 1):
            synchro_empty += 1
        empty_tested += 1
    print()
    print("Among {} empty DFA synchro is {}". format(empty_tested, synchro_empty))
    print("Meanwhile generated non empty DFA {}".format(non_empty_generated))
    return synchro_empty

### Experiment 4: how many DFA with at least one empty state among @DFA_config.max_count are synchro

### Important: if @DFA_config.max_transition_number == @DFA_config.max_states_number * @DFA_config.max_alphabet_number, empty DFA doesn't exist
### So be sure to pick up number suitable for this test
def test_5():
    print()
    print("Test 5")
    empty_tested = 0
    synchro_empty = 0
    non_empty_generated = 0
    bar4 = IncrementalBar("Searching ", max = DFA_config.max_count)
    while empty_tested < DFA_config.max_count:
        failed_count = 0
        bar4.next()
        empty_dfa = generate_DFA()
        while not check_if_one_empty(empty_dfa):
            if failed_count > DFA_config.max_count * 150:
                print()
                print("a lot of non empty generated. No more empties found. Exit with data obtained so long")
                print("Among {} empty DFA synchro is {}". format(empty_tested, synchro_empty))
                print("Meanwhile generated non empty DFA {}".format(non_empty_generated))
                return synchro_empty
            failed_count += 1
            non_empty_generated += 1
            empty_dfa = generate_DFA()

        if check_synchro(empty_dfa) == 1:
            synchro_empty += 1
        empty_tested += 1
    print()
    print("Among {} empty DFA with ONE empty state synchro is {}". format(empty_tested, synchro_empty))
    print("Meanwhile generated non empty DFA {}".format(non_empty_generated))
    return synchro_empty

### Experiment 5: find a not fully connected DFA with doubled states, but still synchro
### assuming it should be connected to some strongly connected component of the DFA
def test_6():
    """TODO"""

### Experiment 6: find fully connected DFA,  but non synchro
def test_7():
    """TODO"""

### Experiment 7: how many tries to generate fully connected
def test_8():
    """TODO"""

def take_median(func):
    loops_total = 10
    synchro_found = 0
    for i in range(loops_total):
        synchro_found += func()
    return (synchro_found /(DFA_config.max_count * loops_total)) * 100
if __name__ == "__main__":

    start_time = time.time()

    ### checking the algorithm with 100% synchronizable automaton. 1 is expected
    # dfa_synchro = {0: {1: 1, 0: 1}, 1: {0: 2, 1: 1}, 2: {0: 3, 1: 2}, 3: {0: 0, 1: 3}}
    # print("IT MUST BE 1! Result", check_synchro(dfa_synchro))

    if DFA_config.max_transition_number < DFA_config.max_states_number:
        print("Unable to generate strongly connected component")
        print("Please increase @DFA_config.max_transition_number to be at least @DFA_config.max_states_number")
        exit()

    if DFA_config.max_transition_number > DFA_config.max_states_number * DFA_config.max_alphabet_number:
        print("too many transitions, infinite loop while generating automaton")
        exit()

    print("Parameters are:")
    print("Max number of suitable tested DFA ", DFA_config.max_count)
    print("DFA_config.max_states_number ", DFA_config.max_states_number)
    print("DFA_config.max_transition_number ", DFA_config.max_transition_number)
    print("DFA_config.max_alphabet_number ", DFA_config.max_alphabet_number)
    print("Let's get the party started")
    print()
    """
    test_1()
    test_2()
    test_3()
    test_4()
    """

    while (DFA_config.max_transition_number <= DFA_config.max_states_number * DFA_config.max_alphabet_number):
        print()
        print("----New iteration----")
        print("max transition number is ", DFA_config.max_transition_number)
        test_4()
        test_5()
        DFA_config.max_transition_number += 1
    print()
    print("--- Execution took {} seconds ---".format(time.time() - start_time))


# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd

import re
from tqdm import tqdm

def count_language_words(text):
    uk_words = ['biscuit', 'lorry', 'torch', 'chips', 'lift', 'queue', 'rubbish',
                'postcode', 'flat', 'bonnet', 'boot', 'mobile', 'trousers', 'braces', 'brolly',
                'chuffed', 'cheeky', 'knackered', 'loo', 'mum', 'plaster', 'pram', 'telly', 'trolley',
                'windscreen', 'banger', 'bobby', 'cuppa', 'crisps', 'holiday', 'jam', 'lift',
                'scone', 'tarmac', 'till', 'toilet', 'whinge', 'zebra crossing', 'zed', 'diversion',
                'draught', 'dummy', 'chemist', 'dustbin', 'garden', 'nappy', 'pavement', 'petrol',
                'pub', 'shop', 'shopkeeper', 'trainer']
    us_words = ['cookie', 'truck', 'flashlight', 'fries', 'elevator', 'line', 'garbage',
                'zip code', 'apartment', 'hood', 'cellphone', 'pants', 'suspenders', 'umbrella',
                'delighted', 'sassy', 'exhausted', 'bathroom', 'mom', 'band-aid', 'stroller', 'TV',
                'shopping cart', 'windshield', 'hot dog', 'police officer', 'cup of coffee',
                'potato chips', 'vacation', 'jelly', 'elevator', 'biscuit', 'asphalt', 'cash register',
                'restroom', 'whine', 'crosswalk', 'zee', 'detour', 'draft', 'pacifier', 'pharmacy',
                'garbage can', 'yard', 'diaper', 'sidewalk', 'gas', 'bar',  'store', 'storekeeper', 'sneaker']
    au_words = ['biscuit',    'ute',    'torch',    'chips',    'lift',    'queue',
                'rubbish',    'postcode',    'flat',    'bonnet',    'boot',    'mobile',    'trousers',
                'braces',    'brolly',    'chuffed',    'cheeky',    'knackered',    'loo',    'mum',
                'plaster',    'pram',    'telly',    'trolley',    'windscreen',    'barbie',    'cop',
                'brekkie',    'esky',    'maccas',    'servo',    'thongs',    'arvo',    'ute',    'keen',
                'trackie dacks',    'pokies',    'espresso',    'snag',    'ta',    'bloke',    'dunny',
                'lollies',    'footy',    'cuppa',    'chook',    'fair dinkum',    'fossick',    'heaps',
                'mug',    'no worries',    'schooner',    'stubbie',    'tinnie',    'woop woop']

    sa_words = ['braai',    'robot',    'bakkie',    'chop-chop',    'dagga',    'doek',
                'bokkie',    'kiff',    'larny',    'naartjie',    'ou',    'sarmie',    'howzit',
                'ja',    'ubuntu',    'voetsek',    'veld',    'lekker',    'babelaas',    'boerewors',
                'kombi',    'muti',    'pap',    'mealie',    'tsotsi',    'shebeen',    'tsonga',
                'kasi',    'madiba',    'bliksem',    'snoek',    'tik',    'dagga',    'donga',
                'eina',    'mielie',    'robot',    'kloof',    'koppie',    'bra',    'dop',
                'jol',    'kief',    'skollie',    'sosatie',    'veldskoen',    'ouma',    'oupa']
    nz_words = ['bach',    'jandals',    'chilly bin',    'togs',    'biscuit',    'brekkie',
                'dairy',    'haka',    'kiwi',    'puku',    'tiki',    'wop-wops',    'judder bar',    'chur',
                'sweet as',    'welly',    'sheep station',    'suss',    'tramping',    'wahine',    'hongi',
                'pakeha',    'whanau',    'hobbit',    'rugby',    'coromandel',    'hokey pokey',    'bach',
                'kiwifruit',    'wharfie',    'pounamu',    'kiore',    'kia ora',    'taonga',    'tiki',
                'jandal',    'puku',    'haka',    'wop-wops',    'pom',    'ute',    'kiwi',    'jafa',
                'tiki tour',    'chocka',    'tramping',    'pakeha',    'whanau',    'wai',    'iwi',
                'whare',    'mate',    'fush and chups']
    ca_words = ['toque',    'loonie',    'toonie',    'poutine',    'double-double',    'tuque',
                'keener',    'chesterfield',    'runners',    'serviette',    'hydro',    'mickey',    'kerfuffle',
                'loonie',    'backbacon',    'chesterfield',    'two-four',    'washroom',    'touque',    'all-dressed',
                'hydro',    'serviette',    'loonie',    'gongshow',    'mickey',    'kerfuffle',    'runners',
                'tuque',    'double-double',    'poutine',    'beaver tail',    'keener',    'washroom',    'two-four',
                'backbacon',    'zed',    'runners',    'tuque',    'kerfuffle',    'toque',    'loonie',    'keener',
                'hydro',    'mickey',    'homo milk',    'chesterfield',    'double-double',    'pogie',    'gonger',
                'serviette',    'zing',    'kerfuffle',    'loonie',    'toonie',    'runners',    'tuque',
                'backbacon',    'hydro',    'mickey',    'poutine',    'chesterfield',    'two-four',    'washroom',
                'zed',    'keener',    'beaver tail',    'zing',    'double-double',    'homo milk',    'kerfuffle',
                'loonie',    'gongshow',    'tuque',    'runners',    'backbacon',    'pogie',    'serviette',    'mickey',
                'two-four',    'beaver tail',    'zing',    'washroom',    'double-double',    'chesterfield',    'toonie']

    word_count = {
        'UK English': 0,
        'American English': 0,
        'Australian English': 0,
        'South African English': 0,
        'New Zealand English': 0,
        'Canadian English': 0
    }

    # Split the text into words
    words = text.lower().split()

    # Count the occurrences of each type of word
    for word in words:
        if word in uk_words:
            word_count['UK English'] += 1
        if word in us_words:
            word_count['American English'] += 1
        if word in au_words:
            word_count['Australian English'] += 1
        if word in sa_words:
            word_count['South African English'] += 1
        if word in nz_words:
            word_count['New Zealand English'] += 1
        if word in ca_words:
            word_count['Canadian English'] += 1

    return word_count


#!/usr/bin/env python3

from lib import common, githubapi

ORGANIZATION_CONFIG_KEY = "ORGANIZATION"

def organization_repository_webhooks_list(
    auth_token: str, organization_name: str, repository_type: str
) -> list[tuple[str, list[str]]]:
    repository_list: list[tuple[str, list[str]]] = []

    try:
        for repository_item in githubapi.organization_repository_list(
            auth_token, organization_name, repository_type
        ):
            # split repository into owner/repository parts
            owner, repository = repository_item["full_name"].split("/")
            webhook_list: list[str] = []

            try:
                for item in githubapi.repository_webhook_list(
                    auth_token, owner, repository
                ):
                    webhook_list.append(item["config"]["url"])
            except githubapi.APIRequestError as err:
                common.github_api_exit_error(
                    f"Unable to fetch webhook list for repository {owner}/{repository}.",
                    err,
                )

            if webhook_list:
                repository_list.append((repository_item["git_url"], webhook_list))

    except githubapi.APIRequestError as err:
        common.github_api_exit_error(
            f"Unable to fetch repository list for organization {organization_name}, type {repository_type}.",
            err,
        )

    return repository_list

def main():
    # load config from file
    config_data = common.load_config(config_key_addition_set={ORGANIZATION_CONFIG_KEY})
    config_auth_token = config_data["AUTH_TOKEN"]

    # fetch repositories of the specified type and their defined webhooks
    print("Building repository list including webhooks:")
    repository_list = organization_repository_webhooks_list(
        config_auth_token,
        config_data[ORGANIZATION_CONFIG_KEY],
        config_data["REPOSITORY_TYPE"],
    )

    # output list, repository URL with webhooks defined
    for repository_url, webhook_list in repository_list:
        print(f"{repository_url}:")
        for hook_item in webhook_list:
            print(f"\t{hook_item}")

if __name__ == "__main__":
    main()


import tkinter
w=tkinter.Tk()
w.geometry('500x200')
w.title('练习')
w.update()
width=w.winfo_width()
height=w.winfo_height()
print("当前窗口的宽度为",width)
print("当前窗口的高度为",height)
init_EntryValue=tkinter.StringVar()
init_EntryValue.set('zhizhe214@163.com')
e=tkinter.Entry(w,bg='#4f5555',fg='white',text=init_EntryValue,relief='sunken',justify='right')
# e=tkinter.Entry(w,bg='black',fg='white',text=init_EntryValue).place(x=width/2,y=height/2)
# e.pack(expand='yes',fill='x')
e.pack(side='right')
e.pack(side=tkinter.RIGHT)
w.mainloop()


import os, sys, glob, numpy as np, matplotlib.pyplot as mpl

m = 51
n = 1
T = 50
Hc = 100

tofile = 1
x0 = 0.1
x1 = 0.2
y0 = 0.1
y1 = 0.2
result_path = 'blasdf'
filename = 'asfd'
dt = 0.001

os.system('./main_walk %d %f %f %f %f %d %d %d %s %s %f %g'%(tofile,x0,x1,y0,y1,m,n,T,result_path,filename,Hc,dt))
# exit()
mpl.ion()
for step in sorted(glob.glob('spine_n*.txt')):
	tmp = np.loadtxt(step)
	if len(np.shape(tmp))==1:
		break
	for i in xrange(len(tmp)):
		mpl.plot(tmp[i,0],tmp[i,1],'r-x')
	mpl.draw()
a = raw_input('press return>')


from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
import io
from PIL import Image
from datetime import datetime

start=datetime.now()
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.set_window_size(1920,1080)
print(datetime.now()-start)

start=datetime.now()
# driver = webdriver.Chrome(executable_path = './chromedriver.exe')
url = "http://127.0.0.1:8080/"
driver.get(url)
# driver.save_screenshot('screenie.png')
img = driver.find_element(By.ID, "container").screenshot_as_png
imageStream = io.BytesIO(img)
im = Image.open(imageStream)
im.save("aaaaa.png")
print(datetime.now()-start)

start=datetime.now()
url = "http://127.0.0.1:8080/"
driver.get(url)
# driver.save_screenshot('screenie.png')
img = driver.find_element(By.ID, "container").screenshot_as_png
imageStream = io.BytesIO(img)
im = Image.open(imageStream)
im.save("aaaaa2.png")
print(datetime.now()-start)

start=datetime.now()
url = "http://127.0.0.1:8080/"
driver.get(url)
# driver.save_screenshot('screenie.png')
img = driver.find_element(By.ID, "container").screenshot_as_png
imageStream = io.BytesIO(img)
im = Image.open(imageStream)
im.save("aaaaa3.png")
print(datetime.now()-start)
# screenshot = Image.open('screenie.png')
# w, h = screenshot.size
# print(w)
# print(h)
# im1 = screenshot.crop((10, 0, w-15, 300))
# im1.save('screenie.png')


import random
import PySimpleGUI as sg

class SimuladorDeDado:
    def __init__(self):
        self.valor_minimo = 1
        self.valor_maximo = 6
        self.layout = [
        [sg.Text("JOGAR O DADO? ")],
        [sg.Button("SIM"),sg.Button("NÃO")]
        ]

    def Iniciar(self):

        self.janela = sg.Window("SIMULADOR DE DADO",layout = self.layout)
        self.eventos, self.valores = self.janela.Read()

        try:
            if self.eventos == 'SIM':
                self.GerarValorDoDado()
            elif self.eventos == 'NÃO':
                print("AGRADECEMOS SUA PARTICIPAÇÃO")
            else:
                print("FAVOR DIGITAR APENAS SIM OU NÃO!!")
        except:
            print("OCORREU UM ERRO AO RECEBER SUA RESPOSTA")

    def GerarValorDoDado(self):
        print(random.randint(self.valor_minimo,self.valor_maximo))

simulador = SimuladorDeDado()
simulador.Iniciar()


# Importing phase and polar from cmath
# Complex form a+bj, Polar coordinates (r,phi)
from cmath import phase, polar
# Reading input String
given_input = input()
# Converting given string into complex number i.e. a+bj format
in_complex_format = complex(given_input)
#calc_r = abs(in_complex_format) # abs gives the r of polar coordinates
#calc_teta = phase(in_complex_format) # phase gives the phi of polar coordinates
# We can also use polar which will give the r and phi directly as a tuple.
(calc_r_using_polar, calc_teta_using_polar) = polar(in_complex_format)
#print(calc_r)
#print(calc_teta)
print(calc_r_using_polar)
print(calc_teta_using_polar)


#!/usr/bin/env python3

''' Given key, finding minimum number of elements in array equal to key or greater
than key '''

def slidingWindow1(arr, num):
    total  = 0
    found = 0
    elem = 0
    sum = 0
    for i in range(len(arr)):
       sum = sum + arr[i]
       total = total + 1
       print(sum, total)
       if sum >= num:
           if found  == 0:
              found = 1
              elem = total
           else:
              if total < elem:
                 elem = total
           total =  0
    return elem

if __name__ == '__main__':

   arr = [10, 20, 30, -40, 50, 6, 7, 4, 44, 99, 12, 123, 145]
   key = 7799999999
   r = slidingWindow1(arr, key)
   print(arr)
   print("Number of arrary elements are  ", r)


# encoding=utf-8
"""in response to https://notepad-plus-plus.org/community/topic/17550"""
from Npp import *

console.show()
console.clear()

keyword = notepad.prompt('Enter keyword to search for:', '', 'permit')
find_what = notepad.prompt('Enter text to find:', '', 'log')

if find_what != None and len(find_what) > 0 and keyword != None and len(keyword)>0:
    replace_with = notepad.prompt('Enter replacement text:', '', '')

    if replace_with != None:

        editor.beginUndoAction()
        nLines = editor.getLineCount()

        for line_nbr in range(0, nLines):
            start_pos = editor.positionFromLine(line_nbr)
            str = editor.getLine(line_nbr)
            end_pos = start_pos + len(str) - 1
            if keyword in str:
                editor.rereplace(find_what, replace_with, 0, start_pos, end_pos)

        editor.endUndoAction()

"""
# Alan-Kilborn's original code
bookmark_mask = 1 << 24

if editor.markerNext(0, bookmark_mask) == -1:

    notepad.messageBox('To use this, need to have at least one bookmarked line; you have zero', '')

else:

    find_what = notepad.prompt('Enter text to find:', '', '')

    if find_what != None and len(find_what) > 0:

        replace_with = notepad.prompt('Enter replacement text:', '', '')

        if replace_with != None:

            editor.beginUndoAction()

            line_nbr = editor.markerNext(0, bookmark_mask)
            while line_nbr != -1:
                start_pos = editor.positionFromLine(line_nbr)
                end_pos = start_pos + len(editor.getLine(line_nbr)) - 1
                editor.rereplace(find_what, replace_with, 0, start_pos, end_pos)
                line_nbr = editor.markerNext(line_nbr + 1, bookmark_mask)

            editor.endUndoAction()
"""

"""
@Alan-Kilborn said:
> here’s a Pythonscript that fulfills the title of this thread;

Nice.

I'm not sure that "run a regex to bookmark, then run a PythonScript to edit" is any less confusing to the typical user than "run these 2-3 regex in sequence"... but that's still a good alternative.

Really, with one more prompt at the beginning of your script ("Enter the keyword, such as 'permit': "), and using PythonScript to set the bookmarks rather than making the user manually bookmark, PythonScript could be made to handle it.  (Or, skipping bookmarks, just wrap the `editor.rereplace` inside of the `while` loop with pseudocode `if line contains KEYWORD then editor.rereplace`.)
"""


import os
import sys
from pathlib import Path

import click
from tabulate import tabulate

from knext import rest
from knext.core.reasoner import Reasoner

@click.option("--file", help="Path of DSL file.")
@click.option("--dsl", help="DSL string enclosed in double quotes.")
def run_dsl(file, dsl):
    """
    Query dsl by providing a string or file.
    """
    client = Reasoner()
    dsl_content = ""
    if file and not dsl:
        with open(file, "r") as f:
            dsl_content = f.read()
    elif not file and dsl:
        dsl_content = dsl
    else:
        click.secho("ERROR: Please choose either --file or --dsl.", fg="bright_red")

    result = client.run_dsl(dsl_content)
    table = tabulate(result.cells, result.columns, tablefmt="github")
    click.echo(table)

@click.option("--file", help="Path of DSL file.")
@click.option("--dsl", help="DSL string enclosed in double quotes.")
def submit_reasoner_job(file, dsl):
    """
    Submit asynchronous reasoner jobs to server by providing DSL file or string.
    """
    client = Reasoner()
    if file and not dsl:
        with open(file, "r") as f:
            dsl_content = f.read()
    elif not file and dsl:
        dsl_content = dsl
    else:
        click.secho("ERROR: Please choose either --file or --dsl.", fg="bright_red")
        sys.exit()
    inst = client.submit(dsl_content)

    click.secho(
        f"ReasonerJob [\n{dsl_content}] has been successfully submitted."
        f" Use ` knext reasoner get --id {inst.reasoner_job_inst_id} ` to check job status.",
        fg="bright_green",
    )

@click.option("--id", help="Unique id of submitted reasoner job.")
def query_reasoner_job(id):
    """
    Query submitted reasoner job status.
    """
    try:
        job_inst_id = int(id)
    except:
        click.secho("ERROR: Option [--id] must be a integer.", fg="bright_red")
        sys.exit()

    client = Reasoner()
    res = client.query(job_inst_id=job_inst_id)
    if res and len(res) > 0:
        data = {k: [v if v else "-"] for k, v in res[0].to_dict().items()}
        table = tabulate(data, headers="keys", tablefmt="github")
        click.echo(table)
    else:
        click.secho(
            f"ERROR: Cannot find ReasonerJob instance. "
            f"Please check if the [--id {id}] is correct.",
            fg="bright_red",
        )
        sys.exit()

    if (
        res[0].status in ["SUCCESS", "FAILURE"]
        and res[0].result
        and res[0].result.table_name
    ):
        confirm = click.style(
            f"ReasonerJob instance with id [{res[0].job_inst_id}] execution completed. "
            f"Do you need to download result_file?",
            fg="bright_green",
        )
        if click.confirm(confirm):
            client = rest.TableStoreApi()
            table_name = res[0].result.table_name
            content = client.table_store_download_get(file_name=table_name)

            file_path = os.path.join(
                os.environ["KNEXT_ROOT_PATH"],
                os.environ["KNEXT_REASONER_RESULT_DIR"],
                table_name.split("/")[-1],
            )
            if not Path(file_path).parent.exists():
                os.mkdir(Path(file_path).parent.resolve())
            with open(file_path, "w") as file:
                file.write(content)
            click.secho(
                f"Download successful. The file path is [{file_path}].",
                fg="bright_green",
            )
        else:
            sys.exit()


from django.urls import path
from . import views

urlpatterns = [

    path('shows/', views.home_shows, name='home-shows'),

    path('show/<str:pk>/', views.shows, name='show'),
    path('post-show/', views.post_show, name="post-show"),
    path('update-show/<str:pk>/', views.update_show, name="update-show"),
    path('delete-show/<str:pk>/', views.delete_show, name="delete-show"),

    path('profile/<str:pk>/', views.user_profile, name='user-profile'),

]


import os
import torch
from torch import nn
from torch import optim
import torchvision.models as models
from torchvision import datasets
from torchvision import transforms
from torch.utils.data import Dataset, DataLoader

import torch.multiprocessing as mp
import torch.distributed as dist
from torch.nn.parallel import DistributedDataParallel as DDP
from torch.utils.data.distributed import DistributedSampler

from tqdm import tqdm
import argparse
import pandas as pd
import numpy as np
import random
import math

def adjust_learning_rate(args, optimizer, loader, step):
    max_steps = args.epochs * len(loader)
    warmup_steps = 10 * len(loader)
    base_lr = args.bs / 256
    if step < warmup_steps:
        lr = base_lr * step / warmup_steps
    else:
        step -= warmup_steps
        max_steps -= warmup_steps
        q = 0.5 * (1 + math.cos(math.pi * step / max_steps))
        end_lr = base_lr * 0.001
        lr = base_lr * q + end_lr * (1 - q)
    optimizer.param_groups[0]['lr'] = lr * args.learning_rate_weights
    optimizer.param_groups[1]['lr'] = lr * args.learning_rate_biases

# LARS Optimizer
class LARS(optim.Optimizer):
    def __init__(self, params, lr, weight_decay=5e-4, momentum=0.9, eta=0.001,
                 weight_decay_filter=False, lars_adaptation_filter=False):
        defaults = dict(lr=lr, weight_decay=weight_decay, momentum=momentum,
                        eta=eta, weight_decay_filter=weight_decay_filter,
                        lars_adaptation_filter=lars_adaptation_filter)
        super().__init__(params, defaults)

    def exclude_bias_and_norm(self, p):
        return p.ndim == 1

    @torch.no_grad()
    def step(self):
        for g in self.param_groups:
            for p in g['params']:
                dp = p.grad

                if dp is None:
                    continue

                if not g['weight_decay_filter'] or not self.exclude_bias_and_norm(p):
                    dp = dp.add(p, alpha=g['weight_decay'])

                if not g['lars_adaptation_filter'] or not self.exclude_bias_and_norm(p):
                    param_norm = torch.norm(p)
                    update_norm = torch.norm(dp)
                    one = torch.ones_like(param_norm)
                    q = torch.where(param_norm > 0.,
                                    torch.where(update_norm > 0,
                                                (g['eta'] * param_norm / update_norm), one), one)
                    dp = dp.mul(q)

                param_state = self.state[p]
                if 'mu' not in param_state:
                    param_state['mu'] = torch.zeros_like(p)
                mu = param_state['mu']
                mu.mul_(g['momentum']).add_(dp)

                p.add_(mu, alpha=-g['lr'])

def main(args: argparse):
    args.ngpus_per_node = torch.cuda.device_count()
    args.rank = 0
    args.dist_url = f'tcp://localhost:{args.port}'
    args.world_size = args.ngpus_per_node
    mp.spawn(main_worker, (args,), args.ngpus_per_node)

def main_worker(gpu, args):
    args.rank += gpu

    dist.init_process_group(
        backend='nccl', init_method=args.dist_url,
        world_size=args.world_size, rank=args.rank)

    if args.rank == 0:
        log = {
            "train_loss" : [],
            "train_acc" : [],
            "test_loss" : [],
            "test_acc" : []
        }

        log_path = os.getcwd() + f"/{args.bs}_{args.lr}_{args.warm}.parquet"

    torch.cuda.set_device(gpu)
    torch.backends.cudnn.benchmark = True

    # Model
    model = models.resnet18(num_classes = 10).cuda(gpu)
    param_weights = []
    param_biases = []
    for param in model.parameters():
        if param.ndim == 1:
            param_biases.append(param)
        else:
            param_weights.append(param)
    parameters = [{'params': param_weights}, {'params': param_biases}]
    model = DDP(model, device_ids=[gpu])

    # Optimizer
    optimizer = LARS(
        params=parameters, weight_decay=args.wd, lr=args.lr, weight_decay_filter=True, lars_adaptation_filter=True
    )

    # Scheduler
    if not args.warm:
        scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=args.epochs)

    # Dataset
    train_dataset = datasets.CIFAR10(
        root="~/data/",
        train=True,
        transform=transforms.Compose([
            transforms.RandomCrop(32, padding=4),
            transforms.RandomHorizontalFlip(),
            transforms.RandomRotation(15),
            transforms.ToTensor(),
            transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
        ]),
        download=True
    )
    test_dataset = datasets.CIFAR10(
        root="~/data/",
        train=False,
        transform=transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
        ]),
        download=True
    )
    assert args.bs % args.world_size == 0
    train_sampler = DistributedSampler(train_dataset)
    test_sampler = DistributedSampler(test_dataset)
    per_device_batch_size = args.bs // args.world_size

    # Data Loader
    train_loader = DataLoader(
        dataset=train_dataset, batch_size=per_device_batch_size, num_workers=args.workers, pin_memory=True, sampler=train_sampler
    )
    test_loader = DataLoader(
        dataset=test_dataset, batch_size=per_device_batch_size, num_workers=args.workers, pin_memory=True, sampler=test_sampler
    )

    # Loss Function
    criterion = nn.CrossEntropyLoss()

    for epoch in range(args.epochs):
        train_sampler.set_epoch(epoch)
        train_loss = 0
        correct = 0
        total = 0
        batch_count = 0
        for step, (train_img, train_label) in tqdm(enumerate(train_loader, start=epoch * len(train_loader))):
            if args.warm:
                adjust_learning_rate(args, optimizer, train_loader, step)
            else:
                scheduler.step()
            batch_count = step
            train_img = train_img.cuda(gpu, non_blocking=True)
            train_label = train_label.cuda(gpu, non_blocking=True)
            logits = model(train_img)
            loss = criterion(logits, train_label)

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            if args.rank == 0:
                train_loss += loss.item()
                _, predicted = logits.max(1)
                total += train_label.size(0)
                correct += predicted.eq(train_label).sum().item()

        if args.rank == 0:
            log["train_loss"].append(train_loss/(batch_count+1))
            log["train_acc"].append(100.*correct/total)

        if args.rank == 0:
            test_sampler.set_epoch(epoch)
            with torch.no_grad():
                test_loss = 0
                correct = 0
                total = 0
                batch_count = 0
                for step, (val_img, val_label) in tqdm(enumerate(test_loader)):
                    batch_count = step
                    val_img = val_img.cuda(gpu, non_blocking=True)
                    val_label = val_label.cuda(gpu, non_blocking=True)
                    logits = model(val_img)
                    loss = criterion(logits, val_label)

                    test_loss += loss.item()
                    _, predicted = logits.max(1)
                    total += val_label.size(0)
                    correct += predicted.eq(val_label).sum().item()

                log["test_loss"].append(test_loss/(batch_count+1))
                log["test_acc"].append(100.*correct/total)

            print(f"Epoch: {epoch} - " + " - ".join([f"{key}: {log[key][epoch]}" for key in log]))

    if args.rank == 0:
        log_df = pd.DataFrame(log)
        log_df.to_parquet(log_path)

    dist.destroy_process_group()

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
                    prog='Torch Multi-GPU Baseline',
                    description='This project conduct the benchmark of among batch sizes in multi-gpu',
                    epilog='ENJOY!!!')

    parser.add_argument('--bs', type = int, default=32,
                    help='batch size')
    parser.add_argument('--workers', type = int, default=4,
                    help='Number of processor used in data loader')
    parser.add_argument('--epochs', type = int, default=1,
                    help='# Epochs used in training')
    parser.add_argument('--lr', type=float, default=0.01,
                    metavar='LR', help='initial learning rate', dest='lr')
    parser.add_argument('--seed', default=None, type=int,
                    help='seed for initializing training. ')
    parser.add_argument('--port', type=int, default=8080, help='Multi-GPU Training Port.')
    parser.add_argument('--warm', action='store_true',
                        help='Toggle to use warm up strategy')
    parser.add_argument('--learning-rate-weights', default=0.2, type=float, metavar='LR',
                    help='base learning rate for weights')
    parser.add_argument('--learning-rate-biases', default=0.0048, type=float, metavar='LR',
                    help='base learning rate for biases and batch norm parameters')
    parser.add_argument('--wd', default=5e-4, type=float, metavar='W',
                    help='weight decay')

    args = parser.parse_args()

    if args.seed is not None:
        random.seed(args.seed)
        np.random.seed(args.seed)
        torch.manual_seed(args.seed)

    main(args=args)


#!/usr/bin/env python

import os
import sys
import yaml
import pickle
import argparse
sys.path.append("../")

import numpy as np
import pandas as pd
from pandas import DataFrame as df
import matplotlib.pyplot as plt
from matplotlib import rc
from scipy.stats import poisson

import prepare_plots as plots

parser = argparse.ArgumentParser('prepare_measurements.py')
add_arg = parser.add_argument
add_arg('config', nargs='?', default='prepare_measurements.yaml')
args = parser.parse_args()
with open(args.config) as f:
    config = yaml.load(f, yaml.FullLoader)

model    = config['model']
strategy = config['strategy']

if (model == 'IN'):
    from models.IN.graph import Graph, load_graph
elif ('EC' in model):
    from models.EC1.graph import Graph, load_graph

n_events   = config['n_events']
pt_cuts    = config['pt_cuts']
graph_dir  = config['graph_dir']
truth_file = config['truth_file']

print("analyzing\n  - {0}\n  - {1}_{2}\n  - {3}"
      .format(pt_cuts, model, strategy, truth_file))

# configure matplotlib
font = {'family' : 'sans-serif',
        'weight' : 'bold',
        'size'   : 12}
rc('font', **font)
rc('text', usetex=True)

def get_shape(g):
    """ graph shape = (n_nodes, n_edges)
    """
    n_nodes = g[1].X.shape[0]
    n_edges = g[1].Ri.shape[1]
    return n_nodes, n_edges

def get_segment_efficiency(g):
    """ segment efficiency = sum(y)/len(y)
    """
    return np.sum(g[1].y)/(g[1].y).shape[0]

def get_truth_efficiency(i, g, tr):
    """ truth efficiency = sum(y)/n_total_true
    """
    evt_id = g[0]
    n_segs = tr[evt_id][0]
    pt_cuts = tr['pt_cuts'][i]
    truth_eff = np.sum(g[1].y)/n_segs[i]
    return truth_eff

def read_truth(file_name):
    with open(file_name, 'rb') as f:
        truth_table = pickle.load(f)
    return truth_table

truth_table = read_truth(truth_file)
data_dirs  = ["{0}/{1}_{2}_{3}/".format(graph_dir, model, strategy, pt)
              for pt in pt_cuts]

to_hist = df({'seg_eff'   : [], 'seg_eff_er'   : [],
              'truth_eff' : [], 'truth_eff_er' : [],
              'part_eff'  : [], 'part_eff_er'  : [],
              'n_segs'    : [], 'n_segs_er'    : [],
              'n_nodes'   : [], 'n_nodes_er'   : [],
              'size'      : [], 'size_er'      : []})

# analyze efficiencies per pt cut
for i in range(len(pt_cuts)):
    graph_files = os.listdir(data_dirs[i])
    seg_effs, truth_effs = [], []
    node_counts, edge_counts = [], []
    sizes = []

    # must loop over graphs due to memory limitations
    for graph_file in graph_files:
        graph_path = data_dirs[i] + graph_file
        graph_data = (graph_file.split('_')[0],
                      load_graph(graph_path),
                      os.path.getsize(graph_path)/10**6)

        sizes.append((sys.getsizeof(graph_data[1].X) +
                     sys.getsizeof(graph_data[1].Ri) +
                     sys.getsizeof(graph_data[1].Ro) +
                     sys.getsizeof(graph_data[1].y) +
                     sys.getsizeof(graph_data[1].a))/10.**6)

        n_nodes, n_edges = get_shape(graph_data)
        node_counts.append(n_nodes)
        edge_counts.append(n_edges)

        truth_eff = get_truth_efficiency(i, graph_data, truth_table)
        seg_eff = get_segment_efficiency(graph_data)
        truth_effs.append(truth_eff)
        seg_effs.append(seg_eff)

    avg_seg_eff   = [np.mean(seg_effs), np.sqrt(np.var(seg_effs))]
    avg_truth_eff = [np.mean(truth_effs), np.sqrt(np.var(truth_effs))]

    avg_nodes     = [np.mean(node_counts), np.sqrt(np.var(node_counts))]
    avg_edges     = [np.mean(edge_counts), np.sqrt(np.var(edge_counts))]
    avg_size      = [np.mean(sizes), np.sqrt(np.var(sizes))]

    # print out a brief report of the measurements
    data_tag = " ***** pt=" + pt_cuts[i] + " data ***** "
    print("{0}\n \t seg_eff: {1} +/- {2} \n \t truth_eff: {3} +/- {4}"
          .format(data_tag, np.round(avg_seg_eff[0], decimals=3),
                  np.round(avg_seg_eff[1], decimals=3),
                  np.round(avg_truth_eff[0], decimals=3),
                  np.round(avg_truth_eff[1], decimals=3)))
    print("\t nodes: {0} +/- {1} \n \t edges: {2} +/- {3}"
          .format(np.round(avg_nodes[0], decimals=3),
                  np.round(avg_nodes[1], decimals=3),
                  np.round(avg_edges[0], decimals=3),
                  np.round(avg_edges[1], decimals=3)))
    print("\t size: {0} +/- {1}"
          .format(np.round(avg_size[0], decimals=3),
                  np.round(avg_size[1], decimals=3)))

    to_hist = to_hist.append({'seg_eff'      : avg_seg_eff[0],
                              'seg_eff_er'   : avg_seg_eff[1],
                              'truth_eff'    : avg_truth_eff[0],
                              'truth_eff_er' : avg_truth_eff[1],
                              'n_segs'       : avg_edges[0],
                              'n_segs_er'    : avg_edges[1],
                              'n_nodes'      : avg_nodes[0],
                              'n_nodes_er'   : avg_nodes[1],
                              'size'         : avg_size[0],
                              'size_er'      : avg_size[1]},
                             ignore_index=True)

pt_map = {'0p5':0.5, '0p6':0.6, '0p75':0.75, '1':1.0,
          '1p5':1.5, '2':2, '2p5':2.5, '3':3, '4':4, '5':5}
pt_cuts = np.array([pt_map[pt_cut] for pt_cut in pt_cuts])

# plot segment efficiencies
plots.plotXY(pt_cuts, np.array(to_hist['seg_eff']), '$p_{T}$ Cut [GeV]',
             'Segment Efficiency', yerr=np.array(to_hist['seg_eff_er']),
             color='mediumseagreen', title=graph_dir+'/plots/seg_eff.png',
             save_fig=True)

# plot truth efficiencies
plots.plotXY(pt_cuts, np.array(to_hist['truth_eff']), '$p_{T}$ Cut [GeV]',
             'Truth Efficiency', yerr=np.array(to_hist['truth_eff_er']),
             color='mediumseagreen', title=graph_dir+'/plots/truth_eff.png',
             save_fig=True)

# plot sizes
plots.plotXY(pt_cuts, np.array(to_hist['size']), '$p_{T}$ Cut [GeV]',
             'Size [MB]', yerr=np.array(to_hist['size_er']), color='indigo',
             title=graph_dir+'/plots/size.png', save_fig=True)

# plot n_segments
plots.plotXY(pt_cuts, np.array(to_hist['n_segs']), '$p_{T}$ Cut [GeV]',
             'Segments per Graph', yerr=np.array(to_hist['n_segs_er']), color='seagreen',
             title=graph_dir+'/plots/n_segments.png', save_fig=True)

# plot n_nodes
plots.plotXY(pt_cuts, np.array(to_hist['n_nodes']), '$p_{T}$ Cut [GeV]',
             'Nodes per Graph', yerr=np.array(to_hist['n_nodes_er']), color='seagreen',
             title=graph_dir+'/plots/n_nodes.png', save_fig=True)


'''
This is the main code of the ECAPATDNN project, to define the parameters and build the construction
'''

import argparse, glob, os, torch, warnings, time
from utils.tools import *
from utils.dataLoader import train_loader, eval_loader,my_collate_fn
from utils.ECAPAModel import ECAPAModel

parser = argparse.ArgumentParser(description = "ECAPA_trainer")
parser.add_argument('--device',      type=str,   default='cuda:0',       help='Device training on ')
## Training Settings
parser.add_argument('--num_frames', type=int,   default=300,     help='Duration of the input segments, eg: 200 for 2 second')
parser.add_argument('--max_epoch',  type=int,   default=80,      help='Maximum number of epochs')
parser.add_argument('--batch_size', type=int,   default=128,     help='Batch size')
parser.add_argument('--n_cpu',      type=int,   default=4,       help='Number of loader threads')
parser.add_argument('--test_step',  type=int,   default=1,       help='Test and save every [test_step] epochs')
parser.add_argument('--lr',         type=float, default=0.001,   help='Learning rate')
parser.add_argument("--lr_decay",   type=float, default=0.97,    help='Learning rate decay every [test_step] epochs')

## Training and evaluation path/lists, save path
parser.add_argument('--train_list', type=str,   default="./data/train_df",     help='The path of the training list, eg:"/data08/VoxCeleb2/train_list.txt" in my case')
parser.add_argument('--eval_list',  type=str,   default="./data/valid_df",              help='The path of the evaluation list, eg:"/data08/VoxCeleb1/veri_test2.txt" in my case')
parser.add_argument('--eval_max_length', type=int,  default=120000,  help='the max length of evaluate audio')
parser.add_argument('--save_path',  type=str,   default="./exps",                                     help='Path to save the score.txt and models')
parser.add_argument('--initial_model',  type=str,   default="",                                          help='Path of the initial_model')

## Model and Loss settings
parser.add_argument('--C',       type=int,   default=1024,   help='Channel size for the dialect encoder')
parser.add_argument('--m',       type=float, default=0.2,    help='Loss margin in AAM softmax')
parser.add_argument('--s',       type=float, default=30,     help='Loss scale in AAM softmax')
parser.add_argument('--n_class', type=int,   default=9,   help='Number of dialects')
parser.add_argument('--loss' ,   type=str, default="PairDistanceLoss", help='Target and loss function')
## Command
parser.add_argument('--eval',    dest='eval', action='store_true', help='Only do evaluation')

## Initialization
warnings.simplefilter("ignore")
torch.multiprocessing.set_sharing_strategy('file_system')
args = parser.parse_args()
args = init_args(args)

## Define the data loader
train_data = train_loader(**vars(args))
trainLoader = torch.utils.data.DataLoader(train_data, batch_size = args.batch_size, shuffle = True, num_workers = args.n_cpu, drop_last = True)

def my_collate_fn(data, max_length=args.eval_max_length):

    lens = [x[0].shape[0] for x in data]
    max_len = max(lens)
    max_length = min(max_len, max_length)

    features = torch.zeros(len(data), max_length)
    for i, length in enumerate(lens):
        features[i,:length] = data[i][0][:max_length]
    labels = torch.tensor([x[1] for x in data])

    return features, labels

eval_data = eval_loader(**vars(args))
evalLoader = torch.utils.data.DataLoader(eval_data, batch_size = args.batch_size*2, shuffle = True, num_workers = args.n_cpu, drop_last = True, collate_fn=my_collate_fn)

## Search for the exist models
#modelfiles = glob.glob('%s/model_0*.model'%args.model_save_path)
#modelfiles.sort()
modelfiles = []

## Only do evaluation, the initial_model is necessary
if args.eval == True:
    s = ECAPAModel(**vars(args))
    print("Model %s loaded from previous state!"%args.initial_model)
    s.load_parameters(args.initial_model)
    loss = s.eval_network(loader=evalLoader)
    print("EvalLoss %2.2f"%(loss))
    quit()

## If initial_model is exist, system will train from the initial_model
if args.initial_model != "":
    print("Model %s loaded from previous state!"%args.initial_model)
    s = ECAPAModel(**vars(args))
    s.load_parameters(args.initial_model)
    epoch = 1

## Otherwise, system will try to start from the saved model&epoch
elif len(modelfiles) >= 1:
    print("Model %s loaded from previous state!"%modelfiles[-1])
    epoch = 1
    s = ECAPAModel(**vars(args))
    s.load_parameters(modelfiles[-1])
    ## Otherwise, system will train from scratch
else:
    epoch = 1
    s = ECAPAModel(**vars(args))

eval_losses = []
score_file = open(args.score_save_path, "a+")

while(1):
    ## Training for one epoch
    loss, lr = s.train_network(epoch = epoch, loader = trainLoader)

    ## Evaluation every [test_step] epochs
    if epoch % args.test_step == 0:
        s.save_parameters(args.model_save_path + "/model_%04d.model"%epoch)
        eval_losses.append(s.eval_network(loader=evalLoader))
        print(time.strftime("%Y-%m-%d %H:%M:%S"), "%d epoch, EvalLoss %2.2f, minEvalLoss %2.2f"%(epoch,eval_losses[-1], min(eval_losses)))
        score_file.write("%d epoch, LR %f, LOSS %f, EvalLoss %2.2f, minEvalLoss %2.2f\n"%(epoch, lr, loss, eval_losses[-1], min(eval_losses)))
        score_file.flush()

    if epoch >= args.max_epoch:
        quit()

    epoch += 1


import math
import os
import random
import re
import sys

def rotation(a,k,queries):
    k = k%len(a)
    a = a[len(a)-k:]+a[:len(a)-k]
    for _ in range(len(queries)):
        print(a[queries[_]])

if __name__ == "__main__":
    nkq = input().split()
    n = int(nkq[0])
    k = int(nkq[1])
    q = int(nkq[2])
    #takes , input , remove space , split and convert
    #to integer and make a list and put them in a ]
    a = list(map(int,input().rstrip().split()))

    queries = []
    for _ in range(q):
        queries_item = int(input())
        queries.append(queries_item)
    rotation(a,k,queries)


import dash_bootstrap_components as dbc
from dash import dcc
from dash import html
from dash import dash_table

def Forecast(pdb):

  pm = pdb.current_portfolio
  tickers = [ticker.ticker for ticker in pm.tickers]
  cg = pdb.current_content

  fig_portfolio_prediction = cg.forecast_portfolio
  portfolio_perfomance_df = cg.forecast_perfomance_df

  body = dbc.Container(
    [
      dbc.Row(
        [
          dbc.Col(
            [
              dcc.Graph( id='graph-portfolio_forecast',figure=fig_portfolio_prediction ),
              html.P( "Montecarlo simulations predict the following scenario for the portfolio:"),
              html.Center([dash_table.DataTable(id='table-portfolio_perfomance', data=portfolio_perfomance_df.to_dict('records'),
                                  columns=[{"name": i, "id": i} for i in portfolio_perfomance_df.columns],
                                  fill_width=False
                          )])
            ], width=6, style={'marginLeft': 'auto', 'marginRight': 'auto'},
          ),
          dbc.Col(
            [
              dcc.Dropdown(
                id='dropdown-forecast_asset',
                options=[
                  {'label':i, 'value':i} for i in tickers
                ],
                value=tickers[0],
                placeholder="Select an asset"
              ),
              dcc.Graph( id='graph-asset_forecast' ),
              html.Center([html.Div(id='table-asset_perfomance')]),
            ], width=6,
          ),
        ], justify="center", style={'marginLeft': 'auto', 'marginRight': 'auto'}
      )
    ],
  className="mt-4",
  )

  layout = html.Div([
    body
  ])

  return layout


from typing import Dict, List, Optional, Tuple

import numpy as np
import tensorflow as tf
from tensorflow.python.framework import tensor_util

from simple_graph import SimpleGraph, SimpleNode

# [Basic graph utils]
def get_node_name_parts_from_input(input_name: str) -> Tuple[str, str, str]:
    input_parts = input_name.split(":")
    if len(input_parts) < 2:
        suffix = ""
    else:
        suffix = ":" + input_parts[1]
    node_name = input_parts[0]
    if node_name[0] == "^":
        prefix = "^"
        node_name = node_name[1:]
    else:
        prefix = ""
    return prefix, node_name, suffix

def get_node_name_from_input(input_name: Optional[str]) -> str:
    if input_name is None:
        return ""
    _, node_name, _ = get_node_name_parts_from_input(input_name)
    return node_name

def get_canonical_input_name(input_name: str) -> str:
    prefix, node_name, suffix = get_node_name_parts_from_input(input_name)
    suffix = ":0" if suffix == "" else suffix
    return prefix + node_name + suffix

def get_const_value(node: tf.NodeDef) -> np.ndarray:
    # Alternatively
    # tf.contrib.util.constant_value(tensor) will get a tensor"s constant value
    return tensor_util.MakeNdarray(node.attr["value"].tensor)

def get_const_value_by_name(
    graph_def: tf.GraphDef, name: str, simple_graph: Optional[SimpleGraph] = None
) -> np.ndarray:
    if simple_graph:
        node = get_node_by_name(graph_def, simple_graph, name)
        return get_const_value(node)
    else:
        node_name = get_node_name_from_input(name)
        founds = [nd for nd in graph_def.node if nd.name == node_name]
        if len(founds) == 0:
            error_msg = "Unknown node name: {}".format(node_name)
            raise Exception(error_msg)
        return get_const_value(founds[0])

# deprecated: use SimpleGraph.get_node_by_name instead
def get_node_by_name(
    graph_def: Optional[tf.GraphDef], simple_graph: SimpleGraph, name: str
) -> tf.NodeDef:
    return simple_graph.get_node_by_name(name)

# deprecated: use simple_graph.get_simple_node_by_name instead
def get_simple_node_by_name(simple_graph: SimpleGraph, name: str) -> SimpleNode:
    return simple_graph.get_simple_node_by_name(name)

# [Pattern matching]
def check_inputs(  # noqa: C901
    simple_graph: SimpleGraph,
    current_node: SimpleNode,
    pattern_nodes: Dict[str, SimpleNode],
    first_node: SimpleNode,
    matched_name_map: Dict[str, str],
) -> bool:
    # check op type
    if first_node.op != "*":
        matched_ops = [op.strip() for op in first_node.op.split("|")]
        if current_node.op not in matched_ops:
            return False
    # check node name
    if first_node.name in matched_name_map:
        if matched_name_map[first_node.name] != current_node.name:
            return False
    # check inputs
    if (len(first_node.inputs) == 1) and (first_node.inputs[0] == "*"):
        matched_name_map[first_node.name] = current_node.name
        return True
    # if inputs contains both unknown inputs and known inputs
    if (len(first_node.inputs) > 1) and ("*" in first_node.inputs):
        known_inputs = [name for name in first_node.inputs if name != "*"]
        start_idx = 0
        for key_name in known_inputs:
            matched = False
            if key_name.isdigit():
                matched = True
                continue
            for i in range(start_idx, len(current_node.inputs)):
                input_name = current_node.inputs[i]
                cur_input_node = simple_graph.get_simple_node_by_name(input_name)
                expected_input_op_str = (pattern_nodes[key_name].op).strip()
                if "|" in expected_input_op_str:
                    expected_input_ops = expected_input_op_str.split("|")
                else:
                    expected_input_ops = list([expected_input_op_str])
                if (cur_input_node.op in expected_input_ops) and (
                    check_inputs(
                        simple_graph,
                        cur_input_node,
                        pattern_nodes,
                        pattern_nodes[key_name],
                        matched_name_map,
                    )
                ):
                    matched = True
                    start_idx = i
            if not matched:
                return False
    # if all listed inputs are known inputs
    else:
        if len(current_node.inputs) != len(first_node.inputs):
            return False
        for i, input_name in enumerate(current_node.inputs):
            cur_input_node = simple_graph.get_simple_node_by_name(input_name)
            if first_node.inputs[i].isdigit():
                continue
            tmp_input_node = pattern_nodes[first_node.inputs[i]]
            if not check_inputs(
                simple_graph,
                cur_input_node,
                pattern_nodes,
                tmp_input_node,
                matched_name_map,
            ):
                return False
    matched_name_map[first_node.name] = current_node.name
    return True

def get_matched_pattern(
    simple_graph: SimpleGraph,
    pattern_nodes: Dict[str, SimpleNode],
    first_node_key: str,
    init_name_map: Optional[Dict[str, str]] = None,
) -> List[Dict[str, str]]:
    matched_name_maps = list()
    for i, node in enumerate(simple_graph.nodes):
        simple_node = get_simple_node_by_name(simple_graph, node.name)
        tmp_name_map = init_name_map.copy() if init_name_map else dict()
        if check_inputs(
            simple_graph,
            simple_node,
            pattern_nodes,
            pattern_nodes[first_node_key],
            tmp_name_map,
        ):
            matched_name_maps.append(tmp_name_map)
    return matched_name_maps

def get_input_target_op_name(
    simple_graph: SimpleGraph,
    node_name: str,
    input_index: int,
    target_op: str,
    op_map: Dict[str, List[int]],
) -> Optional[str]:
    node = get_simple_node_by_name(simple_graph, node_name)
    input_node_name = get_node_name_from_input(node.inputs[input_index])
    input_node = get_simple_node_by_name(simple_graph, input_node_name)
    if input_node.op == target_op:
        return input_node_name
    if input_node.op in op_map:
        for tmp_index in op_map[input_node.op]:
            target_name = get_input_target_op_name(
                simple_graph, input_node_name, tmp_index, target_op, op_map
            )
            if target_name is not None:
                break
        return target_name
    else:
        return None

def remove_underscore_class_attr(graph_def: tf.GraphDef):
    for i, node in enumerate(graph_def.node):
        if '_class' in node.attr.keys():
            node.attr.pop('_class')


from datetime import datetime
import urllib.parse

# uri = f"mongodb+srv://{urllib.parse.quote('edwardly1002')}:{urllib.parse.quote('Ltb123!@#')}@cluster0.lqbbu.mongodb.net/myFirstDatabase?retryWrites=true&w=majority"
uri = f"mongodb+srv://{urllib.parse.quote('fbfighter')}:{urllib.parse.quote('fbfighter')}@fb-topic.ixbkp2u.mongodb.net/?retryWrites=true&w=majority"
DB_NAME = 'fakenews'
COLL_POST = 'testpost'
COLL_CMT = 'testcmt'

COLL_POST_VAL = {
    '$jsonSchema': {
        'bsonType': 'object',
        'additionalProperties': True,
        'required': ['info', 'post_id', 'page_id'],
        'properties': {
            'info': {
                'bsonType': 'object',
                'properties': {
                    'time': {'bsonType': 'date'},
                    'reaction_count': {'bsonType': 'int'},
                    'comments': {'bsonType': 'int'},
                    'shares': {'bsonType': 'int'},
                    'reactors': {'bsonType': 'array'},
                    'reactions': {
                        'bsonType': 'object',
                        'properties': {
                            'Thích': {'bsonType': 'int'},
                            'Yêu thích': {'bsonType': 'int'},
                            'Thương thương':{'bsonType': 'int'},
                            'Haha':{'bsonType': 'int'},
                            'Wow': {'bsonType': 'int'},
                            'Buồn': {'bsonType': 'int'},
                            'Phẫn nộ':{'bsonType': 'int'},
                        }
                    },
                }
            },
            'comments_full': {'bsonType': 'array'},
            'post_id': {'bsonType': 'string'},
            'page_id': {'bsonType': 'string'},
            'post_url': {'bsonType': 'string'},
            'text': {'bsonType': 'string'},
            'images': {'bsonType': 'array'},
            'medical_label': {'bsonType': 'bool'},
            'username': {'bsonType': 'string'},
            'fetched_time': {'bsonType': 'date'},
            'user_id': {'bsonType': 'string'}
        }
    }
}

COLL_CMT_VAL = {
    '$jsonSchema': {
        'bsonType': 'object',
        'additionalProperties': True,
        'required': ['info', 'text', 'comment_id', 'post_id', 'page_id', 'cookie'],
        'properties': {
            'info': {
                'bsonType': 'object',
                'required': ['reaction_count', 'comments'],
                'properties': {
                    'time': {'bsonType': 'date'},
                    'reaction_count': {'bsonType': 'int'},
                    'comments': {'bsonType': 'int'},
                    'reactors': {'bsonType': 'array'},
                    'reactions': {
                        'bsonType': 'object',
                        'properties': {
                            'Thích': {'bsonType': 'int'},
                            'Yêu thích': {'bsonType': 'int'},
                            'Thương thương':{'bsonType': 'int'},
                            'Haha':{'bsonType': 'int'},
                            'Wow': {'bsonType': 'int'},
                            'Buồn': {'bsonType': 'int'},
                            'Phẫn nộ':{'bsonType': 'int'},
                        }
                    },
                }
            },
            'text': {'bsonType': 'string'},
            'comment_url': {'bsonType': 'string'},
            'page_id': {'bsonType': 'string'},
            'post_id': {'bsonType': 'string'},
            'comment_id': {'bsonType': 'string'},
            'comments_full': {'bsonType': 'array'},
            'username': {'bsonType': 'string'},
            'user_id': {'bsonType': 'string'},
            'cookie': {'bsonType': 'string'}
        }
    }
}

POST_OBJECT = {
    'info': {
        'time': None,
        'reaction_count': 0,
        'reactions':
        {
            'Thích': None,
            'Yêu thích': None,
            'Thương thương':None,
            'Haha':None,
            'Wow': None,
            'Buồn': None,
            'Phẫn nộ':None,
        },
        'comments': 0,
        'shares': 0,
        'reactors': [],
        'complete_crawl_comment': False,
    },
    'comments_full': [],
    'post_id': None,
    'page_id': None,
    'post_url': None,
    'text': '',
    'images': None,
    'medical_label': None,
    'username': None,
    'fetched_time': None,
    'user_id': None,
}

COMMENT_OBJ = {
    'info': {
        'time': None,
        'reaction_count': 0,
        'comments': 0,
        'reactions': None,
        'reactors': None,
        'complete_crawl_comment': False,
    },
    'text': '',
    'comment_id': None,
    'post_id': None,
    'page_id': None,
    'comments_full': [],
    'comment_url': None,
    'username': None,
    'user_id': None,
}

REACTION_MAP = {
    'Thích': 'Like',
    'Yêu thích': 'Love',
    'Thương thương': 'Care',
    'Haha': 'Haha',
    'Wow': 'Wow',
    'Buồn': 'Sad',
    'Phẫn nộ': 'Angry',
}

MON_TO_NUM = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

STD_RULES = {}
STD_RULES.update(REACTION_MAP)

DB_DEBUG=True


from secml.testing import CUnitTest

from secml.array import CArray
from secml.optim.function import CFunction
from secml.data.loader import CDLRandom

class CClassifierGradientMixinTestCases(CUnitTest):
    """Unittests interface for CClassifierGradientMixin.

    Attributes
    ----------
    clf_grads_class : CClassifierGradientTest
        Test class implementing gradient test methods for specific clf.

    """
    clf_grads_class = None

    @classmethod
    def setUpClass(cls):

        CUnitTest.setUpClass()

        cls.seed = 2

        cls.ds = CDLRandom(n_features=2, n_redundant=0,
                           n_informative=2, n_clusters_per_class=1,
                           random_state=cls.seed).load()
        cls.ds_sparse = cls.ds.tosparse()

    @staticmethod
    def _grad_tr_fun(params, x0, y0, clf_grads, clf):
        """Change classifier parameters and recompute objective function.

        Parameters
        ----------
        params : CArray
            New value of the parameters. Vector array.
        x0 : CArray
            Point on which to compute the objective function.
        y0 : CArray
            Label of the point on which to compute the objective function.
        clf_grads : CClassifierGradientTest
            Test class implementing gradient test methods.
        clf : CClassifier
            Target classifier.

        """
        clf = clf_grads.change_params(params, clf)
        return clf_grads.train_obj(x0, y0, clf)

    def _test_grad_tr_params(self, clf):
        """Compare `grad_tr_params` output with numerical gradient.

        Parameters
        ----------
        clf : CClassifier

        """
        i = self.ds.X.randsample(
            CArray.arange(self.ds.num_samples), 1, random_state=self.seed)
        x, y = self.ds.X[i, :], self.ds.Y[i]
        self.logger.info("idx {:}: x {:}, y {:}".format(i.item(), x, y))

        params = self.clf_grads_class.params(clf)

        # Compare the analytical grad with the numerical grad
        gradient = clf.grad_tr_params(x, y).ravel()
        num_gradient = CFunction(self._grad_tr_fun).approx_fprime(
            params, epsilon=1e-6,
            x0=x, y0=y, clf_grads=self.clf_grads_class, clf=clf)

        error = (gradient - num_gradient).norm()
        self.logger.info("Analytical gradient:\n{:}".format(gradient))
        self.logger.info("Numerical gradient:\n{:}".format(num_gradient))
        self.logger.info("norm(grad - grad_num): {:}".format(error))
        self.assertLess(error, 1e-2)

        self.assertTrue(gradient.is_vector_like)
        self.assertEqual(params.size, gradient.size)
        self.assertEqual(params.issparse, gradient.issparse)
        self.assertIsSubDtype(gradient.dtype, float)


from pyiga.codegen import cython as codegen
from pyiga import vform
import numpy as np

def my_stiffness_vf(dim):
    # same as stiffness_vf(), but slower implementation
    from pyiga.vform import VForm, inner, grad, dx
    V = VForm(dim)
    u, v = V.basisfuns()
    V.add(inner(grad(u), grad(v)) * dx)
    return V

def vector_laplace_vf(dim):
    from pyiga.vform import VForm, inner, grad, dx
    V = VForm(dim)
    u, v = V.basisfuns(components=(dim,dim))
    V.add(inner(grad(u), grad(v)) * dx)
    return V

def vector_L2functional_vf(dim):
    from pyiga.vform import VForm, inner, dx
    V = VForm(dim, arity=1)
    u = V.basisfuns(components=(dim,))
    f = V.input('f', shape=(dim,))
    V.add(inner(u, f) * dx)
    return V

def test_codegen_poisson2d():
    code = codegen.CodeGen()
    vf = vform.stiffness_vf(2)
    assert (not vf.vec) and vf.arity == 2
    codegen.AsmGenerator(vf, 'TestAsm', code).generate()
    code = codegen.preamble() + '\n' + code.result()

def test_codegen_poisson3d():
    code = codegen.CodeGen()
    vf = my_stiffness_vf(3)
    assert (not vf.vec) and vf.arity == 2
    codegen.AsmGenerator(vf, 'TestAsm', code).generate()
    code = codegen.preamble() + '\n' + code.result()

def test_codegen_vectorlaplace2d():
    code = codegen.CodeGen()
    vf = vector_laplace_vf(2)
    assert vf.vec == 2*2 and vf.arity == 2
    codegen.AsmGenerator(vf, 'TestAsm', code).generate()
    code = codegen.preamble() + '\n' + code.result()

def test_codegen_functional():
    code = codegen.CodeGen()
    vf = vform.L2functional_vf(3, updatable=True)
    assert (not vf.vec) and vf.arity == 1
    codegen.AsmGenerator(vf, 'TestAsm', code).generate()
    code = codegen.preamble() + '\n' + code.result()

def test_codegen_vecfunctional():
    code = codegen.CodeGen()
    vf = vector_L2functional_vf(3)
    assert vf.vec == 3 and vf.arity == 1
    codegen.AsmGenerator(vf, 'TestAsm', code).generate()
    code = codegen.preamble() + '\n' + code.result()

def test_codegen_parameter():
    from pyiga.vform import VForm, inner, grad, dx, norm
    code = codegen.CodeGen()
    dim = 2
    vf = VForm(dim, arity=1)
    u = vf.basisfuns()
    a = vf.parameter('a')
    b = vf.parameter('b', shape=(dim,))
    vf.add(norm(a * b) * inner(grad(u), b / norm(a * b)) * dx)
    codegen.AsmGenerator(vf, 'TestAsm', code).generate()
    code = codegen.preamble() + '\n' + code.result()

def test_codegen_wave_st2d():
    code = codegen.CodeGen()
    vf = vform.wave_st_vf(2)
    assert (not vf.vec) and vf.arity == 2 and vf.spacetime
    codegen.AsmGenerator(vf, 'TestAsm', code).generate()
    code = codegen.preamble() + '\n' + code.result()


'''

This file can be executed from a python prompt to run the demo.  A few things to note:

-- Fatsheet runs faster than the regular spreadsheet compiler only when the dataset is very large.  With the dataset included in the demo, significant acceleration may not be observed during some runs.  With enormous datasets, the speedup can top 1000%.  Essentially, the advantage of processing a dataset on the GPU has to completely offset the cost of writing the dataset to the GPU across PCIe.  This bottleneck is compounded when a spreadsheet formula has multiple different functions, each requiring a different dataset to be sent to the GPU.

-- The output of Fatsheet may be inaccurate compared to CPU results depending on the GPU used to run the demo.  Many GPUs only support 32-bit precision calculations, while most CPUs can support up to 64-bit precision.  On the GeForce 9600M GT used to develop this demo, the error ends up around 0.0000000004194103 for the correlation example.

-- The speedtest should actually be run multiple times to establish an average acceleration; GPU and CPU processing times are highly variable.

'''

from xmlparse import odsSheet
from topology import Tree
import numpy as np
from time import time

# reads spreadsheet XML into pythonic data container
odsheet = odsSheet('data/correl_test.xml')

# Compiles spreadsheet data into dependency tree
t = Tree(odsheet)

# run correlation using fatsheet (executes on GPU)
sGPU = time()
t.run()
GPU_correl = t.getResults()[t.schedule[0][0]]
eGPU = time()

# GPU test results
GPU_time = eGPU - sGPU
print("GPU time: %s" % (GPU_time))
print("GPU correlation: %.16f" % (GPU_correl))

# run correlation using numpy built-in function (executes on CPU)
sCPU = time()
CPU_correl = np.corrcoef(t.sheetobj.data[0,:,0], t.sheetobj.data[0,:,1])[0][1]
eCPU = time()

# CPU test results
CPU_time = eCPU - sCPU
print("CPU time: %s" % (CPU_time))
print("CPU correlation: %.16f" % (CPU_correl))

# error between GPU / CPU calculations
print("GPU / CPU error: %.16f" % (GPU_correl - CPU_correl))
print("GPU Speedup: %.16f" % (CPU_time / GPU_time))


from django.http import HttpResponse
from django.template import Template, Context, loader
from Familiares.models import *
from datetime import datetime
from pytz import timezone

FORMATO = "%Y-%m-%d %H:%M:%S"

def saludo(request):
  return HttpResponse('Hola Django - Coder')

def familiares(self):

  BUENOS_AIRES_TZ = datetime.now(timezone('America/Argentina/Buenos_Aires'))

  diccionario = {
    'FechaActual': BUENOS_AIRES_TZ,
    'Familiares': [
      {
      'Nombre': 'Silvina',
      'Apellido': 'Aguirre',
      'Parentesco': 'madre',
      'Edad': 51,
      'Nacimiento': {'anio': 1970, 'mes': '09', 'dia': '28' }
      },
      {
      'Nombre': 'Andres',
      'Apellido': 'Galeano',
      'Parentesco': 'padre',
      'Edad': 55,
      'Nacimiento': {'anio': 1967, 'mes': '06', 'dia': '27' }
      },
      {
      'Nombre': 'Ludmila',
      'Apellido': 'Galeano Aguirre',
      'Parentesco': 'hermana',
      'Edad': 25,
      'Nacimiento': {'anio': 1997, 'mes': '03', 'dia': '24' }
      }
    ]
  }

  plantilla = loader.get_template('template1.html')
  documento = plantilla.render(diccionario)

  for item in diccionario['Familiares']:
    Nacimiento = f"{item['Nacimiento']['anio']}-{item['Nacimiento']['mes']}-{item['Nacimiento']['dia']}"
    familiar = Familiares(nombre=item['Nombre'], apellido=item['Apellido'], parentesco=item['Parentesco'], edad=item['Edad'], nacimiento=Nacimiento)
    familiar.save()

  return HttpResponse(documento)


import boto3
from botocore.exceptions import ClientError
import json
import os
import time
from datetime import datetime, timezone
from dateutil import tz

from antiope.aws_account import *
from common import *

import logging
logger = logging.getLogger()
logger.setLevel(getattr(logging, os.getenv('LOG_LEVEL', default='INFO')))
logging.getLogger('botocore').setLevel(logging.WARNING)
logging.getLogger('boto3').setLevel(logging.WARNING)
logging.getLogger('urllib3').setLevel(logging.WARNING)

RESOURCE_PATH = "accessanalyzer/analyzer"
RESOURCE_TYPE = "AWS::AccessAnalyzer::Analyzer"

def lambda_handler(event, context):
    logger.debug("Received event: " + json.dumps(event, sort_keys=True))
    message = json.loads(event['Records'][0]['Sns']['Message'])
    logger.info("Received message: " + json.dumps(message, sort_keys=True))

    try:

        account_id = message['account_id']
        target_account = AWSAccount(message['account_id'])

        regions = target_account.get_regions()
        if 'region' in message:
            regions = [message['region']]

        # describe ec2 instances
        for r in regions:
            client = target_account.get_client('accessanalyzer', region=r)
            analyzer = get_analyzer(target_account, client, r)

    except AntiopeAssumeRoleError as e:
        logger.error("Unable to assume role into account {}({})".format(target_account.account_name, target_account.account_id))
        return()
    except AntiopeAssumeRoleError as e:
        logger.error("Unable to assume role into account {}({})".format(target_account.account_name, target_account.account_id))
        return()
    except ClientError as e:
        if e.response['Error']['Code'] == 'AccessDeniedException' or e.response['Error']['Code'] == 'UnauthorizedOperation':
            logger.error(f"AccessDeniedException/UnauthorizedOperation for access-analyzer in {target_account.account_name}({target_account.account_id}): {e}")
            return()
        else:
            logger.critical("AWS Error getting info for {}: {}".format(account_id, e))
            capture_error(message, context, e, "ClientError for {}: {}".format(account_id, e))
            raise
    except Exception as e:
        logger.critical("{}\nMessage: {}\nContext: {}".format(e, message, vars(context)))
        capture_error(message, context, e, "General Exception for {}: {}".format(account_id, e))
        raise

def get_analyzer(target_account, client, region):
    analyzers = []
    response = client.list_analyzers()
    while 'nextToken' in response:
        analyzers += response['analyzers']
        response = client.list_analyzers(nextToken=response['nextToken'])
    analyzers += response['analyzers']

    # There may be no analyzers in this region, so we return none
    if len(analyzers) == 0:
        return(None)

    for a in analyzers:
        resource_item = {}
        resource_item['awsAccountId']                   = target_account.account_id
        resource_item['awsAccountName']                 = target_account.account_name
        resource_item['resourceType']                   = RESOURCE_TYPE
        resource_item['source']                         = "Antiope"
        resource_item['configurationItemCaptureTime']   = str(datetime.datetime.now())
        resource_item['awsRegion']                      = region
        resource_item['configuration']                  = a
        resource_item['supplementaryConfiguration']     = {}
        resource_item['resourceId']                     = f"{target_account.account_id}-{region}-{a['name']}"
        resource_item['ARN']                            = a['arn']
        resource_item['errors']                         = {}
        save_resource_to_s3(RESOURCE_PATH, resource_item['resourceId'], resource_item)

    # There can currently only be one analyzer. When that changes this part needs to be fixed
    return(analyzers[0]['arn'])  # Arn is needed for getting findings