import shutil
import os
import re
from pathlib import Path

from .log import logger, is_debug
from .dwhrepo import repo
from .snowflake import sf
from .release import release
from .sql import print_sql, sql_meta, get_diff_sql, statement_cleanup
from .utils import get_file_contents
from .config import config

class Model():
    """Handles model .sql definitions."""

    MODEL_DIR    = config.read_config('model_dir', default='model')
    EXTENSIONS   = re.compile(r'.*(\.sql)|(\.vw)|(\.tbl)$', re.I)
    DIFF_DIR     = ".diff"

    def __init__(self):
        self.sf_safe_branch  = repo.get_sf_safe_branch()

    def prepare_release_candidate(self, force):
        """Prepares a release candidate file (if missing)."""
        repo.assert_repo()
        commit_hash = release.get_base_commit(self.sf_safe_branch)
        release.check_release_dir_clean(base_commit=commit_hash)

        files = repo.get_changed_files(index=commit_hash,
                prefix=Model.MODEL_DIR)

        if len(files) == 0:
            logger.info(f"No changes in {Model.MODEL_DIR} dir to prepare release candidate "
                    "file. Preparing empty file.")

        if force:
            logger.info("Not checking if release candidate file was changed due to --force.")
        else:
            release.check_release_candidate()

        branch = repo.get_branch()

        release_candidate_sql = f"--.Release candidate file, branch: {branch}\n\n"
        for change in files.values():
            release_candidate_sql += Model._change_into_release_entry(change, commit_hash)

        release.save_release_candidate_file(release_candidate_sql, branch)
        logger.debug("Release candidate file contents:\n" + release_candidate_sql)

    def deploy_release(self, dry_run=True):
        release_sql = release.prepare_release_file()
        deploy_sql = release.release_file_to_sql(release_sql)

        if is_debug():
            print_sql(deploy_sql)
        if dry_run:
            if not is_debug():
                print_sql(deploy_sql)
            logger.info("Skipping SQL execution due to --dry-run.")
            return

        sf.perform_release(deploy_sql, self.sf_safe_branch)
        release.save_release(release_sql)

        pass

    def clone_production(self, force):
        """Clones production database into new db named after branch name."""
        sf.clone_production(self.sf_safe_branch, force)

    def compare_sf_git(self, branch=None):
        """Compares Snowflake and current branch DDLs."""

        if branch is None:
            branch = self.sf_safe_branch

        git_ddls = self.get_all_ddls()
        sf_ddls = sf.get_all_objects(branch)

        logger.info("| {:_^39.39} | {:_^11.11} | {:_^30.30} | {:_^16.16} |".format(
           'object name', 'type', 'GIT file name', 'last change on SF'))

        for name in sorted(git_ddls.keys() | sf_ddls.keys()):
            o_name = git_ddls.get(name, sf_ddls.get(name))[0]
            o_type = git_ddls.get(name, sf_ddls.get(name))[1]
            o_file = git_ddls.get(name, (None, None, '! MISSING !'))[2][-30:]
            o_date = sf_ddls.get(name, (None, None, None))[2]
            o_date = f"{o_date:%Y-%m-%d %H:%M}" if o_date else '! MISSING !'

            logger.info(f"| {o_name:<39.39} | {o_type:<11.11} | {o_file:^30.30} | {o_date:^16.16} |")

    def compare_single_file(self, filename, branch=None):
        if branch is None:
            branch = self.sf_safe_branch
        if not self.EXTENSIONS.search(filename):
            logger.info(f"{filename} is not a SQL file. Running global comparison.")
            self.compare_sf_git(branch=branch)
            return

        o_name, o_type, easy_ddl, git_ddl = sql_meta(filename)
        sf_ddl = sf.get_ddl(branch, o_type, o_name)

        if not os.path.exists(self.DIFF_DIR):
            os.makedirs(self.DIFF_DIR)
        sf_filename = os.path.join(self.DIFF_DIR, os.path.basename(filename))
        with open(sf_filename, 'w') as sf_file:
            sf_file.write("-- Snowflake definition --\n")
            sf_file.write(statement_cleanup(sf_ddl))

        print_sql(git_ddl)
        print_sql(sf_ddl)

    def get_all_ddls(self):
        ddls = {}
        for f in Path(self.MODEL_DIR).rglob('*'):
            if self.EXTENSIONS.search(str(f)):
                o_name, o_type, easy_ddl, sql = sql_meta(str(f))
                ddls[(o_type + '#' + o_name).upper()] = (o_name, o_type, str(f))
        return ddls

    @staticmethod
    def _change_into_release_entry(change, commit_hash):
        """Converts a git.change of a file into a release candidate entry (string)."""
        change_type = change.change_type
        filename = change.a_path if change_type == 'D' else change.b_path
        if change_type != 'D':
            o_name, o_type, easy_ddl, sql = sql_meta(filename)

        if change_type == 'D':
            # dropped file/object
            return (f"--.File was removed and *will not* be included in the release.\n"
                    f"-- [{change_type}] NOT_INCLUDED:{filename}\n\n"
                    f"   You have to include a DROP statement to properly sync database state:\n"
                    f"   {release.HERE_STMT}\n\n")
        elif change_type == 'R':
            if easy_ddl:
                return (f"--.File with {o_type} definition was renamed and will be included in the release.\n"
                        f"-- [{change_type}] INCLUDED:{filename}\n\n"
                        f"   If you renamed the object, remember to drop the old one:\n"
                        f"   DROP {o_type} <<OLD_NAME>>; {release.HERE_STMT}\n\n")
            # rename table or stream
            return (f"--.File with {o_type} definition was renamed and *will not* be included in the release.\n"
                    f"-- [{change_type}] NOT_INCLUDED:{filename}\n\n"
                    f"   You may need to include an ALTER {o_type} RENAME statement to properly sync database state:\n"
                    f"   ALTER {o_type} <<OLD_NAME>> RENAME {o_name}; {release.HERE_STMT}\n\n")
        elif change_type == 'A':
            # add new object
            return Model._new_file_into_release_entry(change_type, easy_ddl, filename, o_name, o_type, sql)
        elif change_type == 'M':
            if easy_ddl:
                # modification / non table/stream
                return (f"--.File was changed and will be included in the release.\n"
                        f"--.You can change the order of the INCLUDE: statement below.\n"
                        f"-- [{change_type}] INCLUDED:{filename}\n\n")
            # modification / table/stream
            sql_diff = repo.get_file_diff(commit_hash, filename)
            return (f"--.File was changed but *will not be* included in the release.\n"
                    f"-- [{change_type}] NOT_INCLUDED:{filename}\n"
                    f"{sql_diff}\n"
                    f"   You may need to include an ALTER {o_type} statement to properly sync database state:\n"
                    +(f"   SELECT 1/IFF(COUNT(*)>0, 0, 1) FROM {o_name}; -- this will fail if stream is non empty\n" if o_type == 'STREAM' else '')
                    + f"   ALTER {o_type} {o_name} {release.HERE_STMT};\n\n")
        else:
            return (f"--.I don't understand this change and it it *will not* be included in the release.\n"
                    f"-- [{change_type}] NOT_INCLUDED:{filename}\n\n"
                    f"   You have to manually apply changes made in file listed above.\n"
                    f"   {release.HERE_STMT}\n\n")
    @staticmethod
    def _new_file_into_release_entry(change_type, easy_ddl, filename, o_name, o_type, sql):
        if easy_ddl:
            return (f"--.File was added and will be included in the release.\n"
                    f"--.You can change the order of the INCLUDE: statements below.\n"
                    f"-- [{change_type}] INCLUDED:{filename}\n\n")

        try:
            ddl = sf.get_ddl(repo.get_branch(), o_type, o_name)
        except RuntimeError as e:
            if 'does not exist' not in str(e):
                raise
            return (f"--.File with {o_type} definition was added and {o_name} does not exist on the server.\n"
                    f"--.It will be included in the release.\n"
                    f"--.You can change the order of the INCLUDE: statements below.\n"
                    f"-- [{change_type}] INCLUDED:{filename}\n\n")

        logger.warning(f"New CREATE {o_type} definition found in {filename}, but {o_name} already exists on the server.")
        sql_diff = get_diff_sql(ddl, sql, f"{o_type} {o_name} definition on the server",
                f"{o_type} {o_name} from {filename}")

        return (f"--.File with {o_type} definition was added but {o_name} *exists* on the server.\n"
                f"-- [{change_type}] NOT_INCLUDED:{filename}\n"
                f"{sql_diff}\n"
                f"   Double check if {o_type} definition on the server matches file contents. {release.HERE_STMT}\n"
                f"   SELECT '{o_type} {o_name} already exists on the server';\n\n")

model = Model()


from util import Queue, print_circuit
from circuit_components import Edge
from circuit_components import Resistor

class Circuit:

    graph: set[Edge]

    def __init__(self, graph: list[Edge]):
        self.graph = set()
        for d in graph:
            if isinstance(d, Edge):
                self.graph.add(d)

    def nodes(self) -> tuple:
        nodes = set()
        for edge in self.graph:
            nodes.add(edge.a)
            nodes.add(edge.b)
        return nodes

    def find_edges_with_node(self, node: str) -> list[str]:
        edges = []
        for edge in self.graph:
            if edge.a == node or edge.b == node:
                edges.append(edge)
        return edges

    def find_edges(self, a:str, b:str) -> list[Edge]:
        edges = []
        for e in self.graph:
            if e.hasNode(a) and e.hasNode(b):
                edges.append(e)
        return edges

    def _has_path(self,nodes:dict[str, bool], prev:dict[str, bool], a:str, b:str, deep=''):
        prev[a] = False
        for e in self.find_edges_with_node(a):
            n = e[a]
            if n == b:
                nodes[n] = True
                return True
            elif n not in prev:
                if self._has_path(nodes, prev.copy(), n, b, deep+' '*4):
                    nodes[a] = nodes[n] = True
        return a in nodes

    def subgraph(self, a:str, b:str) -> list[Edge]:
        nodes = dict()
        nodes[a] = self._has_path(nodes, dict(), a, b)
        for n in self.nodes():
            if n not in nodes:
                nodes[n]=False
        return Circuit(filter(lambda e: nodes[e.a] and nodes[e.b], self.graph))

    def _remove_parallel(self) -> 'Circuit':
        record = []
        unique_edges = []
        for e in self.graph:
            in_unique_list = False
            for e2 in unique_edges:
                if Edge.same_nodes(e, e2):
                    in_unique_list = True
                    break
            if not in_unique_list:
                unique_edges.append(e)
        for e in unique_edges:
            edges = self.find_edges(e.a, e.b)
            if len(edges) > 1:
                s = 0
                for e1 in edges:
                    s += 1/e1.r
                    self.graph.remove(e1)
                req = Resistor(e.a, e.b, 1/s)
                self.graph.add(req)
                record.append((req, edges))
        return record

    def _remove_series(self, fixA:str, fixB:str) -> 'Circuit':
        record = []
        for n in self.nodes():
            if n == fixA or n == fixB:
                continue
            edges = self.find_edges_with_node(n)
            if len(edges) == 2:
                a = edges[0][n]
                b = edges[1][n]
                self.graph.remove(edges[0])
                self.graph.remove(edges[1])
                req = Resistor(a, b, edges[0].r+edges[1].r)
                self.graph.add(req)
                record.append((req, edges))
        return record

    def _find_deltas(self):
        potential = dict()
        for edge in self.graph:
            e = self.find_edges_with_node(edge.a)
            if len(e) > 2:
                if edge.a in potential:
                    potential[edge.a].union([e0[edge.a] for e0 in e])
                else:
                    potential[edge.a] = set([e0[edge.a] for e0 in e])
            e = self.find_edges_with_node(edge.b)
            if len(e) > 2:
                if edge.b in potential:
                    potential[edge.b].union([e0[edge.b] for e0 in e])
                else:
                    potential[edge.b] = set([e0[edge.b] for e0 in e])
        pnodes = list(potential.keys())
        deltas = []
        while len(pnodes) > 0:
            p0 = pnodes.pop()
            p1 = None
            for i in range(len(pnodes)):
                p1 = pnodes[i]
                for j in range(i+1, len(pnodes)):
                    p2 = pnodes[j]
                    if p1 in potential[p0] and \
                       p2 in potential[p0] and \
                       p2 in potential[p1]:
                       deltas.append([p0,p1,p2])
        return deltas

    def _delta2Y(self, a:str, b:str, c:str):
        ab = self.find_edges(a, b)[0]
        ac = self.find_edges(a, c)[0]
        bc = self.find_edges(b, c)[0]
        y = str(len(self.graph))
        r_sum = ab.r+ac.r+bc.r
        ra = Resistor(a,y,(ab.r*ac.r)/r_sum,y+'a')
        rb = Resistor(b,y,(ab.r*bc.r)/r_sum,y+'b')
        rc = Resistor(c,y,(ac.r*bc.r)/r_sum,y+'c')
        self.graph.remove(ab)
        self.graph.remove(ac)
        self.graph.remove(bc)
        self.graph.add(ra)
        self.graph.add(rb)
        self.graph.add(rc)
        return (ra,rb,rc),(ab,ac,bc)

    def _remove_delta(self):
        deltas = self._find_deltas()
        if len(deltas) > 0:
            return [self._delta2Y(*deltas[0])]
        return []

    def req(self, a:str, b:str) -> float:
        c = self.subgraph(a, b)
        while len(c.graph) > 1:
            s = c._remove_series(a, b)
            p = c._remove_parallel()
            if len(s) == 0 and len(p) == 0:
                d = c._remove_delta()
                if len(d) == 0 and len(c.graph) > 1:
                    return None
        return c.graph.pop().r

    def _rsolver(self, record_stack:list[dict[str,Edge,Edge]], solved_info:dict[Edge,tuple], req:Edge) -> None:
        if len(record_stack) > 0:
            rec = record_stack.pop()
            V, R, i = solved_info[req]
            for e in rec['rmv']:
                t = rec['type']
                if t == 's':
                    solved_info[e]=(e.r*i, e.r, i)
                if t == 'p':
                    solved_info[e]=(V, e.r, V/e.r)
                if t == 'd':
                    solved_info[e]=(e.r*i, e.r, V/e.r)
                self._rsolver(record_stack, solved_info, e)

    def solve(self, a:str, va:float, b:str, vb:float) -> float:
        record_stack = []
        c = self.subgraph(a, b)
        while len(c.graph) > 1:
            s = c._remove_series(a, b)
            for req, rmv in s:
                record_stack.append({'type': 's', 'req':req, 'rmv': rmv})
            p = c._remove_parallel()
            for req, rmv in p:
                record_stack.append({'type': 'p', 'req':req, 'rmv': rmv})
            if len(s) == 0 and len(p) == 0:
                d = c._remove_delta()
                for req, rmv in d:
                  record_stack.append({'type': 'd', 'req':req, 'rmv': rmv})
                if len(d) == 0 and len(c.graph) > 1:
                    return None
        req = c.graph.pop();
        V = va-vb
        R = req.r
        i = V/R
        solved_info={req:(V,R,i)}
        c._rsolver(record_stack, solved_info, req)
        for e in self.graph:
            if e not in solved_info:
                solved_info[e] = (0,0,0)
        return solved_info

    def __str__(self):
        return 'Circuit: {}\n\t{}'.format(self.nodes(), '\n\t'.join(map(str,self.graph)))

    def __repr__(self):
        return 'Circuit ({}, {})'.format(self.nodes(), '\n\t'.join(self.graph))

if __name__=='__main__':

    R = Resistor
    c1= Circuit([
        R('A','B',2,'r1'),
        R('B','C',6,'r2'),
        R('B','C',3,'r3'),
        R('C','D',4,'r4'),
        R('A','D',2,'r5'),
    ])
    c = Circuit([
        R('A','C',70,'r1'),
        R('C','D',20,'r2'),
        R('C','E',50,'r3'),
        R('E','D',30,'r4'),
        R('E','F',10,'r5'),
        R('D','F',40,'r6'),
        R('B','F',60,'r7'),
    ])

    def flow(c,a,b,prev):
        prev.add(a)
        r_sum = 0
        r_mul=1
        for e in c.find_edges_with_node(a):
            n = e[a]
            if n not in prev:
                r_sum+=e.r
                r_mul*=e.r
        res = 0
        for e in c.find_edges_with_node(a):
            print(e,r_sum)
            n = e[a]
            if n == b:
                res += (e.r)/r_sum
            elif n not in prev:
                res += (e.r)/r_sum + flow(c,n,b,prev.copy())
        return res

    print(flow(c1,'A','D',set()))


import datetime

from wtforms import StringField, PasswordField, FileField
from wtforms import BooleanField, DateTimeField, SubmitField
from wtforms.validators import DataRequired, Email, MacAddress, URL
from bluecat.wtform_extensions import GatewayForm
from bluecat.wtform_fields import Configuration, CustomStringField, IP4Address, CustomSubmitField
from . import cmdb_config

class GenericFormTemplate(GatewayForm):
    """
    Generic form Template

    Note:
        When updating the form, remember to make the corresponding changes to the workflow pages
    """
    workflow_name = 'cmdb_configuration'
    workflow_permission = 'cmdb_configuration_page'
    servicenow_url = CustomStringField(
        label='ServiceNow URL',
        default=cmdb_config.servicenow_url,
        is_disabled_on_start=False,
        is_disabled_on_error=False,
        required=True,
    )

    servicenow_username = CustomStringField(
        label='ServiceNow User',
        default=cmdb_config.servicenow_username,
        is_disabled_on_start=False,
        is_disabled_on_error=False,
        required=True
    )

    servicenow_secret_file = CustomStringField(
        label='ServiceNow Secret file and path',
        default=cmdb_config.servicenow_secret_file,
        is_disabled_on_start=False,
        is_disabled_on_error=False,
        required=True
    )

    servicenow_max_query_results = CustomStringField(
        label='ServiceNow Max Query Results',
        default=cmdb_config.servicenow_username,
        is_disabled_on_start=False,
        is_disabled_on_error=False,
        required=True
    )

    submit = CustomSubmitField(
        label='Save',
        is_disabled_on_start=False,
        is_disabled_on_error=False
    )


from augmentation import SegmentationGenerator
# from model_checkpoint_parallel import ModelCheckpoint
import unet
import segnet

from keras.callbacks import TensorBoard, ModelCheckpoint
from keras.optimizers import SGD, Adam
from keras.utils.training_utils import multi_gpu_model
import keras.backend as K

import cv2
from skimage.io import *

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

from os.path import join, basename, exists
from os import listdir, makedirs

SIZE = (256, 256)
N_GPUS = 1
BATCH_SIZE = 16*N_GPUS

SMOOTH = 1.

def dice(y_true, y_pred):
    y_true_f = K.flatten(y_true)
    y_pred_f = K.flatten(y_pred)
    intersection = K.sum(y_true_f * y_pred_f)
    return (2. * intersection + SMOOTH) / (K.sum(y_true_f) + K.sum(y_pred_f) + SMOOTH)

def dice_coef_loss(y_true, y_pred):
    return -dice(y_true, y_pred)

DATA_PATHS = {
    'TRAIN': {
        'IMAGES': 'data/train/images',
        'MASKS': 'data/train/masks',
    },
    'TEST': {
        'IMAGES': 'data/test/images',
        'MASKS': 'data/test/masks',
    }
}

train_datagen = SegmentationGenerator(
    horizontal_flip=True,
    vertical_flip=True,
    rotation_range=90.,
    width_shift_range=0.3,
    height_shift_range=0.3,
    zoom_range=0.2,
    shear_range=0.3,
    fill_mode='reflect'
)

train_iter = train_datagen.flow_from_directory(
    DATA_PATHS['TRAIN']['IMAGES'],
    DATA_PATHS['TRAIN']['MASKS'],
    target_size=SIZE, color_mode='grayscale',
    batch_size=BATCH_SIZE, shuffle=True
)

valid_datagen = SegmentationGenerator()

valid_iter = valid_datagen.flow_from_directory(
    DATA_PATHS['TEST']['IMAGES'],
    DATA_PATHS['TEST']['MASKS'],
    target_size=SIZE, color_mode='grayscale',
    batch_size=BATCH_SIZE, shuffle=False
)

model = segnet.build_model(*SIZE, n_channels=1)
if N_GPUS >= 2:
    model = multi_gpu_model(model, N_GPUS)

optimizer = SGD(lr=0.001, momentum=0.9, decay=0.0005, nesterov=False)
model.compile(loss=dice_coef_loss, optimizer=optimizer, metrics=[dice])

model_idx = 0

experiment_dir = 'segnet_{}x{}'.format(*SIZE)
models_dir = join(experiment_dir, 'models')
log_dir = join(experiment_dir, 'log')
if not exists(models_dir):
    makedirs(models_dir)
if not exists(log_dir):
    makedirs(log_dir)
modelname = 'segnet' + str(model_idx)
modelpath = join(models_dir, modelname)
tb_log_dir = join(log_dir, modelname)

model.fit_generator(
    train_iter,
    epochs=200,
    callbacks=[
        ModelCheckpoint(filepath=modelpath, monitor='val_dice', period=1),
        TensorBoard(log_dir=tb_log_dir, batch_size=BATCH_SIZE)
    ],
    validation_data=valid_iter,
    use_multiprocessing=False
)


from socket import *
port = int(input("Port No: "))
address = ("localhost", port)
print(address)
BUFSIZE = 1024
s = socket(AF_INET,SOCK_STREAM)
s.connect(address)
while True:
    msg = input("Message to send: ")
    try:
        temp = s.send(msg.encode())
    except:
        print('connection closed')
        break
    else:
        print("{} bytes send".format(temp))

    try:
        data = s.recv(BUFSIZE) #receive message from server
    except:
        print('connection closed')
    else:
        if not data:
            break
    print("Received message: %s" %data.decode())

s.close()


# SI 206 Final Project- Covid API
# Name: Sarah Whitman

import json
import unittest
import os
import requests
import matplotlib
import sqlite3

#functions
def get_countries_from_api():
    url = "https://covid-api.mmediagroup.fr/v1/vaccines"
    r = requests.get(url)
    data = r.text
    dict_list = json.loads(data)

    country_list = list(dict_list.keys())
    #print(country_list)
    #print(len(country_list))
    return country_list

#get continent
def get_continents_from_api():
    url = "https://covid-api.mmediagroup.fr/v1/vaccines"
    r = requests.get(url)
    data = r.text
    dict_list = json.loads(data)

    continents_list = []
    for country_key in dict_list:
        try:
            continent = dict_list[country_key]["All"]["continent"]
            continents_list.append(continent)
        except KeyError:
            continents_list.append("N/A")
    #print(continents_list)
    #print(len(continents_list))
    return continents_list

def get_ppl_vax_from_api():
    url = "https://covid-api.mmediagroup.fr/v1/vaccines"
    r = requests.get(url)
    data = r.text
    dict_list = json.loads(data)
    vax_list = []
    for country_key in dict_list:
        vaxxed = dict_list[country_key]["All"]["people_vaccinated"]
        vax_list.append(vaxxed)
    #print(vax_list)
    #print(len(vax_list))
    return vax_list

def get_life_ex_from_api():
    url = "https://covid-api.mmediagroup.fr/v1/vaccines"
    r = requests.get(url)
    data = r.text
    dict_list = json.loads(data)
    life_ex_list = []
    for country_key in dict_list:
        try:
            cur_life_ex = dict_list[country_key]["All"]["life_expectancy"]
            life_ex_list.append(cur_life_ex)
        except KeyError:
            life_ex_list.append("N/A")
    #print(life_ex_list)
    #print(len(life_ex_list)
    return life_ex_list

def create_full_dictionary():
    country_list = get_countries_from_api()
    continents_list = get_continents_from_api()
    vaxxed_list = get_ppl_vax_from_api()
    life_exp_list = get_life_ex_from_api()
    tuples_list = []
    for i in range(len(country_list)):
        tup = (country_list[i], vaxxed_list[i], life_exp_list[i], continents_list[i])
        tuples_list.append(tup)
    #print(tuples_list)

    data_dictionary = {}
    sub_dict_list = []
    country_id = 0
    for tup in tuples_list:
        sub_dict = {}
        sub_dict["people_vaccinated"] = tup[1]
        sub_dict["life_expectancy"] = tup[2]
        sub_dict["continent"] = tup[3]
        sub_dict["country_id"] = country_id
        sub_dict_list.append(sub_dict)
        country_id +=1
    #print(sub_dict_list)

    for i in range(len(country_list)):
        country = tuples_list[i][0]
        data_dictionary[country] = sub_dict_list[i]
    print(data_dictionary)
    return data_dictionary

#set up the database
def setUpDatabase(db_name):
    path = os.path.dirname(os.path.abspath(__file__))
    conn = sqlite3.connect(path+'/'+db_name)
    cur = conn.cursor()
    return cur, conn

#create the table for continents and ids
def create_continents_table(cur, conn, data_dictionary):
    continent_list = []
    for country in data_dictionary.keys():
        continent = data_dictionary[country]["continent"]
        if continent not in continent_list:
            continent_list.append(continent)

    #cur.execute("DROP TABLE IF EXISTS Continents")
    cur.execute("CREATE TABLE IF NOT EXISTS Continents (id INTEGER PRIMARY KEY, continent TEXT)")
    count = 0

    for i in range(len(continent_list)):
        cur.execute("INSERT OR IGNORE INTO Continents (id, continent) VALUES (?,?)",(i,continent_list[i]))

    conn.commit()

#create the table for country, cases, deaths, continent_id
def create_covid_info_table(cur, conn, data_dictionary):
    #cur.execute("DROP TABLE IF EXISTS CovidInfo")
    cur.execute("CREATE TABLE IF NOT EXISTS CovidInfo (country_id INTEGER PRIMARY KEY, country TEXT UNIQUE, people_vaccinated INTEGER, life_expectancy INTEGER, continent_id INTEGER)")
    count = 0
    for country in data_dictionary:
        name = country
        vaxxed = data_dictionary[country]["people_vaccinated"]
        life_exp = data_dictionary[country]["life_expectancy"]
        continent_name = data_dictionary[country]["continent"]
        country_id = data_dictionary[country]["country_id"]

        cur.execute("SELECT id FROM Continents WHERE continent = ?", (continent_name,))
        continent_ids = cur.fetchall()
        for cont in continent_ids:
            continent_id = cont[0]
        cur.execute("INSERT OR IGNORE INTO CovidInfo (country_id, country, people_vaccinated, life_expectancy, continent_id) VALUES (?,?,?,?,?)", (country_id, name, vaxxed, life_exp, continent_id))
        if cur.rowcount == 1:
            count += 1
            if count == 25:
                break
    #cur.execute("SELECT * FROM CovidInfo")

    conn.commit()

class TestCases(unittest.TestCase):
    def test_get_countries_from_api(self):
        country_list = get_countries_from_api()
        self.assertEqual(len(country_list), 193)

    def test_get_continents_from_api(self):
        continent_list = get_continents_from_api()
        self.assertEqual(len(continent_list), 193)

    def test_get_ppl_vax_from_api(self):
        vax_list = get_ppl_vax_from_api()
        self.assertEqual(len(vax_list), 193)

    def test_get_life_ex_from_api(self):
        life_ex_list = get_life_ex_from_api()
        self.assertEqual(len(life_ex_list), 193)

    def test_create_full_dictionary(self):
        data_dictionary = create_full_dictionary()
        self.assertEqual(len(data_dictionary), 193)
        self.assertEqual(list(data_dictionary.keys())[2], "Algeria")
        self.assertEqual(data_dictionary["Cambodia"]["continent"], "Asia")

def main():
    #print("This is main")
    get_countries_from_api()
    get_continents_from_api()
    get_ppl_vax_from_api()
    get_life_ex_from_api()

    data_dictionary = create_full_dictionary()

    cur, conn = setUpDatabase("CovidInfo.db")
    create_continents_table(cur, conn, data_dictionary)
    create_covid_info_table(cur, conn, data_dictionary)
    conn.close()

if __name__ == '__main__':
    main()
    unittest.main(verbosity=2)


# import discord
from discord.ext import commands
from discord.ext.commands import Context
import coc

from config import Config
from commands.war import create_war_commands
from coc_events.war import create_war_events

config = Config()
bot = commands.Bot(command_prefix='!')
coc_client = coc.login(
  config.coc_dev_email,
  config.coc_dev_password,
  key_names=config.coc_key_names,
  client=coc.EventsClient
)

@bot.event
async def on_ready():
  print(f'We have logged in as {bot.user}')

# Ping pong test command
@bot.command(name='ping')
@commands.guild_only()
@commands.has_permissions(administrator=True)
async def ping(ctx:Context):
  await ctx.send('Pong')

# Commands
create_war_commands(config, bot, coc_client)

# CoC Events
create_war_events(config, bot, coc_client)

bot.run(config.bot_token)


import sys

from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
from django.core.management.base import CommandError
from django.core.management.base import CommandParser

from django_sftp.compat import get_username_field
from django_sftp.models import SFTPUserAccount
from django_sftp.models import SFTPUserGroup

class Command(BaseCommand):
    """Django Command to creare SFTP account.

    Runs as ./manage.py createsftpuseraccount.
    """

    help = "Create SFTP user account"

    def add_arguments(self, parser: CommandParser) -> None:
        """Parse django command arguments.

        Args:
            parser (CommandParser): CommandParser instance
        """
        parser.add_argument("username")
        parser.add_argument("group")
        parser.add_argument("home_dir", nargs="?")

    def handle(self, *args, **options) -> None:
        """Django command handler."""
        username = options.get("username")
        group_name = options.get("group")
        home_dir = options.get("home_dir")

        if SFTPUserAccount.objects.filter(user__username=username).exists():
            raise CommandError(f'FTP user account "{username}" is already exists.')

        user_model = get_user_model()
        try:
            user = user_model.objects.get(**{get_username_field(): username})
        except user_model.DoesNotExist:
            raise CommandError(f'User "{username}" is not exists.')

        try:
            group = SFTPUserGroup.objects.get(name=group_name)
        except SFTPUserGroup.DoesNotExist:
            raise CommandError(f'FTP user group "{group_name}" is not exists.')

        account = SFTPUserAccount.objects.create(
            user=user, group=group, home_dir=home_dir
        )

        sys.stdout.write(
            f'FTP user account pk={account.pk}, "{username}" was created.\n',
        )


from flask import Blueprint, request, make_response
from flask_login import login_required, current_user
from app.models import db, Bookmark, Bookmarks_List

bookmarks_routes = Blueprint("bookmarks", __name__)

@bookmarks_routes.route("")
def get_users_bookmarks():
    """ Get all of users bookmarks """
    user = current_user.to_dict()
    lists = Bookmarks_List.query.filter(Bookmarks_List.user_id == user["id"]).all()
    bookmarks = []
    for list in lists:
       for bookmark in list.to_dict_bookmark_tabs()["bookmarks"]:
           bookmarks.append(bookmark)
    return bookmarks

@bookmarks_routes.route("/<int:bookmark_id>")
def get_bookmark_by_id(bookmark_id):
    """" Get single bookmark by id """
    user = current_user.to_dict()

    bookmark = Bookmark.query.get(bookmark_id)
    #------------ validation -------------#
    if not bookmark:
        error = make_response("Bookmark does not exist")
        error.status_code = 404
        return error

    list = Bookmarks_List.query.get(bookmark.bookmarks_list_id)
    if list.user_id != user["id"]:
        error = make_response("Only the creator can view their Bookmark")
        error.status_code = 401
        return error
    #--------------------------------------#
    return bookmark.to_dict()

@bookmarks_routes.route("", methods=["PUT"])
def update_bookmark_status():
    """ update a bookmark's completion status """
    user = current_user.to_dict()

    bookmark_id = request.get_json()
    bookmark = Bookmark.query.get(bookmark_id)

    #------------ validation -------------#
    if not bookmark:
        error = make_response("Bookmark does not exist")
        error.status_code = 404
        return error
    #--------------------------------------#
    bookmark.completed = not bookmark.completed
    db.session.commit()
    return bookmark.to_dict()

@bookmarks_routes.route("", methods=["DELETE"])
def delete_bookmark():
    """ Delete a single bookmark """
    user = current_user.to_dict()
    data = request.get_json()

    #------------ validation -------------#
    bookmark = Bookmark.query.get(data["bookmarkId"])
    if not bookmark:
        error = make_response("Bookmark does not exist")
        error.status_code = 404
        return error

    list = Bookmarks_List.query.get(bookmark.bookmarks_list_id)
    if list.user_id != user["id"]:
        error = make_response("Only the creator of a Bookmark can delete a bookmark")
        error.status_code = 401
        return error
    # #--------------------------------------#

    db.session.delete(bookmark)
    db.session.commit()
    res = make_response({"message": "Successfully deleted"})
    res.status_code = 200
    return res


def preberi(ime_datoteke):
    datoteka = open(ime_datoteke,"r")
    sosednje = {}
    for i,vrstica in enumerate(datoteka):
        s = []
        for izvoz in vrstica.strip().split():
            s.append(int(izvoz))
        s *= 2
        for a,stev in enumerate(s):
            if stev == min(s):
                s = s[a:a+(len(s)//2)]
                break
        sosednje[i+1] = s
    return sosednje

def mozna_pot(pot, zemljevid):
    ne_vmes_ven = len(pot) - 2 == sum([True for krozisce in pot if len(zemljevid[krozisce]) != 1])
    if len(zemljevid[pot[0]]) == 1 and len(zemljevid[pot[-1]]) == 1:
        if ne_vmes_ven:
            for premik1,premik2 in zip(pot,pot[1:]):
                if premik2 in zemljevid[premik1] and premik1 != premik2:
                    continue
                else:
                    return False
            return True
    return False

def hamiltonova(pot, zemljevid):
    s = []
    for p in pot:
        if p not in s:
            s.append(p)
        else:
            return False
    vsa_krozisca = {krozisce for krozisce in zemljevid.keys() if len(zemljevid[krozisce]) > 1}
    pot1 = {a for a in pot}
    return vsa_krozisca <= pot1 and mozna_pot(pot,zemljevid)

def navodila(pot, zemljevid):
    odcepi = []
    for premik1,premik2,premik3 in zip(pot,pot[1:],pot[2:]):
        urejeno = zemljevid[premik2] *2
        for i,b in enumerate(urejeno):
            if b == premik1:
                urejeno = urejeno[i:len(urejeno)//2 + i]
                break

        for i,a in enumerate(urejeno):
            if a == premik3:
                if i:
                    odcepi.append(i)
                    break
    if odcepi:
        return odcepi
    return [0]

def prevozi(zacetek, navodila, zemljevid):
    krozisca = [zacetek,zemljevid[zacetek][0]]
    trenutno = zemljevid[zacetek][0]
    stevec = 0
    for navodilo in navodila:
        od = zemljevid[trenutno] * 2
        for i,b in enumerate(od):
            if b == krozisca[stevec]:
                od = od[i:len(od)//2 + i]
                stevec += 1
                break
        for i,odcep in enumerate(od):
            if i == navodilo:
                krozisca.append(odcep)
                trenutno = odcep
                break
    return krozisca

def sosedi(doslej, zemljevid):
    povezane = set()
    for krozisce in doslej:
        for a in zemljevid[krozisce]:
            if a not in doslej:
                povezane.add(a)
    return povezane

def razdalja(x, y, zemljevid):
    stevec = 0
    sosedje = {x}
    x = {x}
    while y not in sosedje:
        for sosed in sosedi(x,zemljevid):
            sosedje.add(sosed)
        x = sosedi(x, zemljevid)
        stevec += 1
    return stevec


# 在本节中，我们将学习
#
# 使用OpenCV查找图像的傅立叶变换
# 利用Numpy中可用的FFT功能
# 傅立叶变换的一些应用
# 我们将看到以下函数：cv.dft() ，cv.idft() 等

#Numpy中的傅立叶变换
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('./images/test.jpg', 0)
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
magnitude_spectrum = 20 * np.log(np.abs(fshift))
plt.subplot(121)
plt.imshow(img, cmap='gray')
plt.title('Input Image')
plt.xticks([])
plt.yticks([])
plt.subplot(122)
plt.imshow(magnitude_spectrum, cmap='gray')
plt.title('Magnitude Spectrum')
plt.xticks([])
plt.yticks([])
plt.show()

#OpenCV中的傅立叶变换
# OpenCV 为此提供了功能 cv.dft() 和 cv.idft() 。它返回与以前相同的结果，但是有两个通道。
# 第一个通道将具有结果的实部，第二个通道将具有结果的虚部。输入的图像应首先转换为np.float32 。
# 我们将看到如何做。
img_opencv = cv.imread('./images/test.jpg', 0)
dft = cv.dft(np.float32(img), flags=cv.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)
magnitude_spectrum = 20*np.log(cv.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))
plt.subplot(121),plt.imshow(img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(magnitude_spectrum, cmap = 'gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()


import pickle

synon = {"madras":["chennai"],
		"mas":["maa"]}

anton = {"hard" : ["soft", "easy"]}
with open('synon.b', 'w') as sy:
	pickle.dump(synon, sy)

with open('anto.b', 'w') as an:
	pickle.dump(anton, an)


import os
import polars as pl
import logging
import requests
import json
import time
import re
import traceback

from app.models.TokenHolder import TokenHolder
from app.database.main import engine, PG_URL
from datetime import datetime, timedelta
from sqlmodel import select
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql import func
from contextlib import contextmanager
from web3 import Web3
from web3.exceptions import BlockNotFound
from typing import List, Dict
from dotenv import load_dotenv

load_dotenv()

ALCHEMY_URL = os.environ.get("ALCHEMY_URL")
web3 = Web3(Web3.HTTPProvider(ALCHEMY_URL))

logging.basicConfig(
    level=logging.INFO,
    format="{asctime} {levelname} {message}",
    style="{",
    datefmt="%m-%d-%y %H:%M:%S",
)

log = logging.getLogger()

# Set up the logger for sqlalchemy.engine
logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO)

log.info(f"pg url: {PG_URL}")
log.info(f"engine: {engine}")

@contextmanager
def get_session():
    session = sessionmaker(bind=engine, expire_on_commit=False)()

    try:
        yield session
    finally:
        session.close()

def is_hex(s):
    return re.match(r"^0x[0-9a-fA-F]{40}$", s) is not None

class TokenIndexer:
    def __init__(self, contract_address: str, total_supply: int):

        if not is_hex(contract_address):
            raise ValueError("Invalid contract address")
        self.contract_address = contract_address
        self.total_supply = total_supply

    def get_transfer_events(
        self, from_block: int, to_block: str = None, contract_address: str = None
    ) -> requests.Response:

        """Get transfer events from Alchemy API, and returns the response"""

        if contract_address is None:
            contract_address = self.contract_address
        # The rest of the get_transfer_events method
        hex_from_block = Web3.to_hex(from_block)

        if to_block:

            hex_to_block = Web3.to_hex(to_block)

            log.info(
                f"get_transfer_events starting from block: {from_block} to block: {to_block}"
            )

        hex_from_block = Web3.to_hex(from_block)
        payload = {
            "jsonrpc": "2.0",
            "id": 0,
            "method": "alchemy_getAssetTransfers",
            "params": [
                {
                    "fromBlock": f"{hex_from_block}",
                    "contractAddresses": [contract_address],
                    "excludeZeroValue": False,
                    "withMetadata": True,
                    "category": ["erc20"],
                }
            ],
        }

        if to_block:
            hex_to_block = Web3.to_hex(to_block)
            payload["params"][0]["toBlock"] = f"{hex_to_block}"
            log.info(
                f"get_transfer_events starting from block: {from_block} to block: {to_block}"
            )
        else:
            payload["params"][0]["toBlock"] = "latest"
            log.info(f"get_transfer_events starting from block: {from_block}")

        transfers = requests.post(url=ALCHEMY_URL, json=payload)
        return transfers

    def process_transfer_events(self, transfers) -> tuple[pl.DataFrame, int]:

        """Process transfer events into a dataframe, and return the dataframe and the last block number processed"""

        records = []

        if transfers.status_code == 400:
            log.error("stream error, status code 400")
        else:
            result = json.loads(transfers.text)
            if result and result.get("result", None):
                if result["result"].get("transfers", None):
                    result: list[dict] = result["result"]["transfers"]
                    for data in result:
                        block_number: int = Web3.to_int(hexstr=str(data["blockNum"]))
                        block_timestamp = data["metadata"]["blockTimestamp"]
                        date_format = "%Y-%m-%dT%H:%M:%S.%fZ"
                        block_date = datetime.strptime(block_timestamp, date_format)
                        from_address: str = data["from"]
                        transaction_hash: str = data["hash"]
                        transaction = web3.eth.get_transaction(transaction_hash)
                        transaction_idx = transaction["transactionIndex"]
                        to_address: str = data["to"]
                        token: str = data["asset"]
                        value: float = round(
                            float(data["value"] if data["value"] else 0), 8
                        )

                        records.append(
                            {
                                "block_number": block_number,
                                "from_address": from_address,
                                "to_address": to_address,
                                "value": value,
                                "transaction_hash": transaction_hash,
                                "block_date": block_date,
                                "transaction_idx": transaction_idx,
                                "token": token,
                                "created_at": datetime.now(),
                            }
                        )

            df = pl.DataFrame(records)
            last_block = df["block_number"].max()
            return df, last_block

    def _calculate_total_supply_percentage(
        self, balance: int, total_supply: int
    ) -> float:

        return round((balance / total_supply) * 100, 2)

    def _calculate_weekly_balance_change(
        self, new_balance: int, previous_balance: int
    ) -> float:
        if previous_balance == 0 or previous_balance is None:
            return 100 if new_balance > 0 else 0
        return round(((new_balance - previous_balance) / previous_balance) * 100, 2)

    def _create_token_holder(
        self,
        record,
        total_supply_percentage,
        weekly_balance_change,
        balance,
        holder: str,
    ) -> TokenHolder:

        """Create a new token holder record"""

        log.info(f"Creating new token holder record: {record['from_address']}")

        holder = record["from_address"] if holder == "sender" else record["to_address"]

        return TokenHolder(
            address=holder,
            transaction_hash=record["transaction_hash"],
            block_number=record["block_number"],
            transaction_index=record["transaction_idx"],
            total_supply_percentage=total_supply_percentage,
            weekly_balance_change=weekly_balance_change,
            balance=balance,
            block_date=record["block_date"],
            token=record["token"],
            last_updated=record["created_at"],
        )

    def _update_sender_balance(self, session, record: dict, total_supply: int):

        """Update the sender's balance in the database"""

        log.info(f"Updating sender balance: {record['from_address']}")
        sender_stmt = (
            select(TokenHolder)
            .where(TokenHolder.address == record["from_address"])
            .order_by(TokenHolder.id.desc())
            .limit(1)
        )
        sender = session.execute(sender_stmt).one_or_none()

        if sender is not None:

            sender = sender[0]

            one_week_ago = record["block_date"] - timedelta(weeks=1)
            previous_balance = (
                session.query(func.sum(TokenHolder.balance))
                .filter(
                    TokenHolder.address == record["from_address"],
                    TokenHolder.block_date < one_week_ago,
                )
                .scalar()
            )

            # Sender's new balance
            new_sender_balance = round(sender.balance - record["value"], 2)

            if new_sender_balance < 0 and new_sender_balance >= -0.03:
                new_sender_balance = 0

            # Create a new record for the sender with the updated balance

            sender_total_supply_percentage = self._calculate_total_supply_percentage(
                new_sender_balance, total_supply
            )
            weekly_balance_change = self._calculate_weekly_balance_change(
                new_sender_balance, previous_balance
            )

            new_sender = self._create_token_holder(
                record,
                sender_total_supply_percentage,
                weekly_balance_change,
                new_sender_balance,
                "sender",
            )
            return new_sender
        else:

            log.info(
                f"Sender does not exist in the database. Address: {record['from_address']}"
            )
            raise ValueError

    def _update_recipient_balance(self, session, record: dict, total_supply: int):

        log.info(f"Updating recipient balance: {record['to_address']}")

        recipient = (
            select(TokenHolder)
            .where(TokenHolder.address == record["to_address"])
            .order_by(TokenHolder.id.desc())
            .limit(1)
        )
        recipient = session.execute(recipient).one_or_none()

        if recipient is None:

            recipient_total_supply_percentage = round(
                (record["value"] / total_supply) * 100, 2
            )

            recipient = self._create_token_holder(
                record,
                balance=record["value"],
                total_supply_percentage=recipient_total_supply_percentage,
                holder="recipient",
                weekly_balance_change=100,
            )

            return recipient

        else:

            recipient = recipient[0]
            new_recipient_balance = round(recipient.balance + record["value"], 2)
            if new_recipient_balance < 0 and new_recipient_balance >= -0.03:
                new_recipient_balance = 0

            # calculate weekly balance change for recipient
            one_week_ago = record["block_date"] - timedelta(weeks=1)
            previous_balance = (
                session.query(func.sum(TokenHolder.balance))
                .filter(
                    TokenHolder.address == record["to_address"],
                    TokenHolder.block_date < one_week_ago,
                )
                .scalar()
            )

            recipient_total_supply_percentage = self._calculate_total_supply_percentage(
                new_recipient_balance, total_supply
            )
            weekly_balance_change = self._calculate_weekly_balance_change(
                new_recipient_balance, previous_balance
            )

            new_recipient = self._create_token_holder(
                record,
                recipient_total_supply_percentage,
                weekly_balance_change,
                new_recipient_balance,
                "recipient",
            )
            return new_recipient

    def update_balances(self, records: List[Dict], total_supply: int) -> None:

        """Update token holder balances in the database."""

        log.info("Updating token holder balances in the database")
        for record in records:
            from_address = record["from_address"]
            to_address = record["to_address"]
            value = record["value"]

            # Skip zero value transactions, we only care about transfers that have a value
            if value == 0:
                continue

            with get_session() as session:
                if from_address == "0x0000000000000000000000000000000000000000":
                    try:
                        initial_owner = (
                            session.query(TokenHolder)
                            .filter(TokenHolder.address == to_address)
                            .one_or_none()
                        )
                        if initial_owner is None:
                            log.info(
                                f"Initial Supply: {value} Token: {record['token']}"
                            )
                            initial_owner = self._create_token_holder(
                                record,
                                total_supply_percentage=100,
                                weekly_balance_change=0,
                                balance=value,
                                holder="recipient",
                            )

                            session.add(initial_owner)
                            session.commit()
                    except Exception as e:
                        log.error(f"An error occurred: {e}")
                        log.error(
                            traceback.format_exc()
                        )  # Include stack trace in the log message
                        log.warning("Rolling back the transaction")
                        session.rollback()
                        log.warning("Transaction rolled back")
                else:
                    try:

                        sender = self._update_sender_balance(
                            session, record, total_supply
                        )

                        session.add(sender)
                        recipient = self._update_recipient_balance(
                            session, record, total_supply
                        )
                        session.add(recipient)
                        log.info(
                            f"Committing the transaction for both sender: {record['from_address']} and recipient: {record['to_address']}"
                        )

                        session.commit()
                    except Exception as e:
                        log.error(f"An error occurred: {e}")
                        log.error(
                            traceback.format_exc()
                        )  # Include stack trace in the log message
                        log.warning("Rolling back the transaction")
                        session.rollback()
                        log.warning("Transaction rolled back")

    def load_transfer_events(self, balances: pl.DataFrame):

        """Load transfer events into the database, and update token holder balances.
        Sender balances are reduced, receiver balances are increased in one transaction."""

        balances.sort(["block_number", "transaction_idx"], descending=False)
        records = balances.to_dicts()
        # with get_session() as session:
        #     initial_owner = (
        #         session.query(TokenHolder)
        #         .filter(TokenHolder.total_supply_percentage == 100)
        #         .one_or_none()
        #     )

        total_supply = self.total_supply

        self.update_balances(records, total_supply)

    def index_continuously(self):

        """Index transfer events continuously(every 15 seconds),
        starting from the latest block number in the TokenHolder table or from block 0 if the table is empty."""

        # Get the latest block number from the TokenHolder table
        with get_session() as session:
            latest_block = session.query(func.max(TokenHolder.block_number)).scalar()

        # If there's no record in the TokenHolder table, set the last_block to 0
        last_block = latest_block if latest_block is not None else 0

        while True:
            try:
                start = time.time()
                transfers = self.get_transfer_events(from_block=last_block)
                balances, last_block = self.process_transfer_events(transfers)
                self.load_transfer_events(balances)

                if balances.is_empty() is False:
                    log.info("Successfully indexed transfer events")
                    last_block = last_block
                    end = time.time()
                    log.info(f"Time taken: {end - start} seconds")

            except BlockNotFound:
                time.sleep(15)

    def on_demand(self, from_block: int, to_block: int):

        """on demand indexing of transfer events, from a given block to a given block"""

        try:
            start = time.time()
            transfers = self.get_transfer_events(
                from_block=from_block, to_block=to_block
            )
            results = self.process_transfer_events(transfers)

            balances = results[0]

            self.load_transfer_events(balances)

            if balances.is_empty() is False:
                log.info("Successfully indexed transfer events")
                end = time.time()
            log.info(f"Time taken: {end - start} seconds")
        except BlockNotFound:
            time.sleep(15)


class TextHistory:

    def __init__(self):
        self._text = ''
        self._actions = []
        self._version = 0
        self._pos = len(self._text)

    def checkValueError(self, text, pos, length = None):
        if pos == None:
            pos = len(self._text)
        if type(pos) == int and not pos < 0 and pos <= len(self._text):
            self._pos = pos
            if length and len(self._text[pos:]) < length:
                raise ValueError
            return pos
        raise ValueError

    @property
    def text(self):
        return self._text

    @property
    def version(self):
        return self._version

    def insert(self, text, pos = None):
        pos = self.checkValueError(text, pos)
        action = InsertAction(pos, text, from_version = self._version,
                              to_version = self._version + 1)

        return self.action(action)

    def replace(self, text, pos = None):
        pos = self.checkValueError(text, pos)
        action = ReplaceAction(pos, text, from_version = self._version,
                               to_version = self._version + 1)

        return self.action(action)

    def delete(self, pos, length):
        pos = self.checkValueError(self._text, pos, length)
        action = DeleteAction(pos, length, from_version = self._version,
                              to_version = self._version + 1)

        return self.action(action)

    def action(self, action):
        self._actions.append(action)
        action.checkVersion()
        self._text = action.apply(self._text)
        self._version = action.to_version
        return self._version

    def get_actions(self, from_version = 0, to_version = None):
        if to_version == None:
            to_version = self._version
        if from_version < 0 or from_version > to_version or to_version > self._version:
            raise ValueError
        else:
            self.new_action = []
            act = None
            for action in self._actions:
                if not action.from_version < from_version and not action.to_version > to_version:
                    act = self.optimize(act, action)
                elif action.to_version > to_version:
                    break
            if act != None:
                self.new_action.append(act)
            return self.new_action

    def optimize(self, act, action):
        if isinstance(action, DeleteAction) and isinstance(act, DeleteAction):
            if action.pos in range(act.pos, act.length + 1):
                act.length = act.length + action.length
                act.pos = min(act.pos, action.pos)
            else:
                self.action_to_return.append(act)
                act = action
                return act
        elif isinstance(action, InsertAction) and isinstance(act, InsertAction):
            if action.pos in range(act.pos, act.pos + len(act.text) + 1):
                act.text = act.text[:action.pos] + action.text + act.text[action.pos:]
            else:
                self.new_action.append(act)
                act = action
                return act
        elif isinstance(action, ReplaceAction):
            if act != None:
                self.new_action.append(act)
            self.new_action.append(action)
            act = None
            return act
        else:
            if act != None:
                self.new_action.append(act)
            act = action

        return act

class Action:

    def __init__(self, pos, text, from_version, to_version):
        self.text = text
        self.pos = pos
        self.from_version = from_version
        self.to_version = to_version

    def checkVersion(self):
        if self.from_version >= self.to_version:
            raise ValueError

class InsertAction(Action):

    def apply(self, line):
        line = line[:self.pos] + self.text + line[self.pos:]
        return line

    def __repr__(self):
        return "InsertAction(pos = {!r}, text = {!r}, " \
               "v1 = {!r}, v2 = {!r})".format(self.pos, self.text,
                self.from_version, self.to_version)

class ReplaceAction(Action):

    def apply(self, line):
        line = line[:self.pos] + self.text + line[self.pos + len(self.text):]
        return line

    def __repr__(self):
        return "ReplaceAction(pos = {!r}, text = {!r}, " \
               "v1 = {!r}, v2 = {!r})".format(self.pos, self.text,
                self.from_version, self.to_version)

class DeleteAction(Action):

    def __init__(self, pos, length, from_version, to_version):
        self.length = length
        self.pos = pos
        self.from_version = from_version
        self.to_version = to_version

    def apply(self, line):
        line = line[:self.pos] + line[self.pos + self.length:]
        return line

    def __repr__(self):
        return "DeleteAction(pos = {!r}, length = {!r}, " \
               "v1 = {!r}, v2 = {!r})".format(self.pos, self.length,
                                              self.from_version,
                                              self.to_version)


from nose.tools import assert_equal, assert_raises
from StringIO import StringIO
from pbcore.io import GffWriter, Gff3Record, GffReader
from pbcore import data

class TestGff3Record:

    def setup(self):
        self.record = Gff3Record("chr1", 10, 11, "insertion",
                                 attributes=[("cat", "1"), ("dog", "2")])

    def test_str(self):
        assert_equal("chr1\t.\tinsertion\t10\t11\t.\t.\t.\tcat=1;dog=2",
                     str(self.record))

    def test_modification(self):
        record = self.record.copy()
        record.dog = 3
        record.cat = 4
        record.mouse = 5
        record.start = 100
        record.end = 110
        assert_equal("chr1\t.\tinsertion\t100\t110\t.\t.\t.\tcat=4;dog=3;mouse=5",
                     str(record))

    def test_fromString(self):
        newRecord = Gff3Record.fromString(str(self.record))
        assert_equal(str(self.record),  str(newRecord))

    def test_get(self):
        """
        Verify field access behavior
        """
        record = self.record
        record.dog = 3
        record.cat = 4
        record.mouse = 5
        record.start = 100
        record.end = 110

        assert_equal(3, record.dog)
        assert_equal(100, record.start)
        with assert_raises(AttributeError):
            record.god

        assert_equal(3, record.get("dog"))
        assert_equal(None, record.get("god"))
        assert_equal(100, record.get("start", 100))

class TestGffReader:
    def setup(self):
        self.rawFile = open(data.getGff3())
        self.reader = GffReader(data.getGff3())

    def test_headers(self):
        assert_equal(["##gff-version 3",
                      "##pacbio-variant-version 2.1",
                      "##date Sat Mar 22 12:16:13 2014",
                      "##feature-ontology http://song.cvs.sourceforge.net/*checkout*/song/ontology/sofa.obo?revision=1.12",
                      "##source GenomicConsensus 0.8.0",
                      "##source-commandline /Users/dalexander/.virtualenvs/VE/bin/variantCaller.py --algorithm=plurality -q20 -x5 pbcore/data/aligned_reads_1.cmp.h5 -r /Users/dalexander/Data/lambdaNEB.fa -o /tmp/v.gff",
                      "##source-alignment-file /Users/dalexander/Dropbox/Sources/git/pbcore/pbcore/data/aligned_reads_1.cmp.h5",
                      "##source-reference-file /Users/dalexander/Data/lambdaNEB.fa",
                      "##sequence-region lambda_NEB3011 1 48502"],
                     self.reader.headers)

    def test__iter__(self):
        records = list(self.reader)
        rawLines = self.rawFile.readlines()[9:]
        for record, rawLine in zip(records, rawLines):
            # No newlines or whitespace allowed in records
            assert_equal(str(record).strip(), str(record))
            # Make sure record matches line
            assert_equal(rawLine.strip(), str(record))

class TestGffWriter:
    def setup(self):
        self.outfile = StringIO()
        self.record1 = Gff3Record("chr1", 10, 11, "insertion",
                                  attributes=[("cat", "1"), ("dog", "2")])
        self.record2 = Gff3Record("chr1", 200, 201, "substitution",
                                  attributes=[("mouse", "1"), ("moose", "2")])
        self.gffWriter = GffWriter(self.outfile)

    def test_writeHeader(self):
        self.gffWriter.writeHeader("##foo bar")
        assert_equal("##gff-version 3\n##foo bar\n",
                     self.outfile.getvalue())

    def test_writeRecord(self):
        self.gffWriter.writeRecord(self.record1)
        self.gffWriter.writeRecord(self.record2)
        expected = ("##gff-version 3\n" +
                    "chr1\t.\tinsertion\t10\t11\t.\t.\t.\tcat=1;dog=2\n" +
                    "chr1\t.\tsubstitution\t200\t201\t.\t.\t.\tmouse=1;moose=2\n")
        assert_equal(expected, self.outfile.getvalue())


from __future__ import annotations

import os
from datetime import datetime

from sqlalchemy import Column
from sqlalchemy import types
from sqlalchemy.engine import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

path_to_database = os.environ.get("PATH_TO_SQLITE")
if path_to_database:
    path_to_database = f"sqlite://{path_to_database}?cache=shared"
else:
    path_to_database = f"sqlite:///:memory:?cache=shared"

Base = declarative_base()

class Messages(Base):
    __tablename__ = 'messages'
    id = Column(types.Integer, primary_key=True)
    receive_time = Column('receive_time', types.DateTime)
    message_group = Column('message_group', types.String(255))
    message_url = Column('message_url', types.String(128))
    message_title = Column('message_title', types.String(255))
    message_full_url = Column('message_full_url', types.String(255))
    black_marker_reason = Column('black_marker_reason', types.String(64))
    content = Column('content', types.Text(), default=u'')

    def __str__(self):
        return f"id:{self.id}  receive_time: {self.receive_time} message_title: {self.message_title} " \
               f"message_url: {self.message_url}  message_full_url: {self.message_full_url}"

if __name__ == '__main__':
    engine = create_engine(path_to_database, echo=True)
    Base.metadata.create_all(engine, checkfirst=True)
    session_builder: sessionmaker = sessionmaker(bind=engine, autocommit=False, autoflush=False)

    session = session_builder()
    # session.begin()
    message = Messages()
    message.message_title = "hello"
    message.receive_time = datetime.now()
    session.add(message)
    session.commit()
    message = Messages()
    message.message_title = "hello2"
    message.receive_time = datetime.now()
    session.add(message)
    session.commit()

    session_another = session_builder()
    object_message: Messages = session_another.query(Messages).filter_by(message_title="hello").one_or_none()
    object_message.message_full_url = "https://this.is.some_url.com"
    session_another.add(object_message) # not possible to attach to another session session.add(object_message)
    session_another.commit()

    # read multiply objects, read updates from another transaction
    # alchemy operations "http://www.leeladharan.com/sqlalchemy-query-with-or-and-like-common-filters"
    for each_record in session.query(Messages).filter(Messages.message_title.isnot(None)):
        print(each_record)
    # connection = engine.connect()


import numpy as np
from scipy.io import loadmat
from matplotlib import pyplot as plt
from scipy.optimize import fmin_cg

def displayData(X, y):
    plt.scatter(X, y, marker='x', c='red')
    plt.xlabel("Change in water level (x)")
    plt.ylabel("Water flowing out of the dam (y)")
    plt.legend(["Data", "Hypothesis"])
    plt.show()

def linearRegCostFunction(theta, X, y, lambda_):
    h = np.dot(X, theta)
    m = y.size
    cost = np.sum(np.square(h-y))/(2*m)
    reg = lambda_/(2*m) * np.sum(np.square(theta[1:]))
    return cost + reg

def linearRegGradient(theta, X, y, lambda_):
    h = np.dot(X, theta)
    m = y.size
    grad = np.dot(X.T, (h-y))/m
    reg = (lambda_/m) * theta
    return grad + reg

def trainLinearReg(X, y, lambda_):
    theta = np.zeros(X.shape[1])
    return fmin_cg(f=linearRegCostFunction, x0=theta,
                   args=(X, y, lambda_), fprime=linearRegGradient, disp=False)
def insertOnes(X):
    ones = np.ones(shape=(X.shape[0], X.shape[1]))
    X_with_ones = np.hstack((ones, X))
    return X_with_ones

def learningCurve(Xtrain, ytrain, Xval, yval, lambda_):
    m = ytrain.size
    train_error = np.zeros(m)
    cv_error = np.zeros(m)
    for i in range(1,m):
        theta = trainLinearReg(Xtrain[:i+1, :], ytrain[:i+1], lambda_)
        train_error[i] = linearRegCostFunction(theta, Xtrain[:i+1, :], ytrain[:i+1], 0)
        cv_error[i] = linearRegCostFunction(theta, Xval, yval, 0)
    plt.plot(range(2, m+1), train_error[1:])
    plt.plot(range(2, m+1), cv_error[1:])
    plt.axis([2, m, 0, 140])
    plt.show()

def poly_features(X, degree):
    X_poly = np.zeros((len(X), degree))
    for i in range(0, degree):
        X_poly[:, i] = X.squeeze() ** (i+1)
    return X_poly

def poly_fit(min_x, max_x, means, stds, theta, degree):
    X =np.linspace(min_x -5, max_x + 5, 1000)
    X_poly = poly_features(X, degree)
    X_poly = (X_poly-means) / stds
    X_poly = insertOnes(X_poly)
    plt.plot(X, X_poly.dot(theta))
    plt.show()
def opt_lambda(theta, X_train, y_train, Xval, yval):
    lambda_values = [0, 0.001, 0.003, 0.01, 0.03, 0.1, 0.3, 1, 3, 10];
    val_err = []
    for lambda_ in lambda_values:
        theta = trainLinearReg(X_train, y_train, lambda_)
        val_err.append(linearRegCostFunction(theta, Xval, yval, 0))
    plt.plot(lambda_values, val_err, c='b')
    plt.axis([0, len(lambda_values), 0, val_err[-1] + 1])
    plt.xlabel("Lambda Values")
    plt.ylabel("Validation Error")
    plt.show()

if __name__ == "__main__":
    dataset = loadmat("ex5data1.mat")
    X_train, y_train = dataset['X'], dataset['y'].squeeze()
    X_test, y_test = dataset['Xtest'], dataset['ytest'].squeeze()
    X_val, y_val = dataset['Xval'], dataset['yval'].squeeze()
#    theta = np.ones((1, 1))
#    displayData(X_train, y_train)
#    X_train = insertOnes(X_train)
#    theta = trainLinearReg(X_train, y_train, 0)
#    plt.plot(X_train[:, 1:], X_train.dot(theta))

#    X_train = insertOnes(X_train)
#    X_val = insertOnes(X_val)
#    learningCurve(X_train, y_train, X_val, y_val, 0)

    X_train_poly = poly_features(X_train, 8)
    X_test_poly = poly_features(X_test, 8)
    X_val_poly = poly_features(X_val, 8)

    X_train_poly_means = X_train_poly.mean(axis=0)
    X_train_poly_std = X_train_poly.std(axis=0, ddof=1)

    X_train_poly = (X_train_poly - X_train_poly_means) / X_train_poly_std
    X_test_poly = (X_test_poly - X_train_poly_means) / X_train_poly_std
    X_val_poly = (X_val_poly - X_train_poly_means) / X_train_poly_std

    X_train_poly = insertOnes(X_train_poly)
    X_test_poly = insertOnes(X_test_poly)
    X_val_poly = insertOnes(X_val_poly)

    theta = trainLinearReg(X_train_poly, y_train, 0)
    plt.scatter(X_train, y_train, marker='x', s=40, c='r')
#    poly_fit(np.min(X_train), np.max(X_train), X_train_poly_means, X_train_poly_std, theta, 8)
#    learningCurve(X_train_poly, y_train, X_val_poly, y_val, 1)
    opt_lambda(theta, X_train_poly, y_train, X_val_poly, y_val)


import ycm_core
import os

flags = [
'-Wall',
'-Wextra',
'-Wpedantic',
'-Werror',
'-x',
'c++',
'-std=c++14',
'isystem',
'./external/gmock/gtest',
'-isystem',
'./external/gmock/gtest/include',
'-isystem',
'./external/gmock',
'-isystem',
'./external/gmock/include',
]

compilation_database_folder = './build'

if os.path.exists(compilation_database_folder):
    database = ycm_core.CompilationDatabase(compilation_database_folder)
else:
    database = None

SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c' ]

def DirectoryOfThisScript():
    return os.path.dirname(os.path.abspath(__file__))

def IsHeaderFile(filename):
    extension = os.path.splitext(filename)[1]
    return extension in [ '.h', '.hxx', '.hpp', '.hh' ]

def GetCompilationInfoForFile(filename):
    if IsHeaderFile(filename):
        basename = os.path.splitext(filename)[0]
        for extension in SOURCE_EXTENSIONS:
            replacement_file = basename + extension
            if os.path.exists(replacement_file):
                compilation_info = database.GetCompilationInfoForFile(replacement_file)
                if compilation_info.compiler_flags_:
                    return compilation_info
        return None
    return database.GetCompilationInfoForFile(filename)

def FlagsForFile(filename, **kwargs):
    if not database:
        return {
            'flags': flags,
            'include_paths_relative_to_dir': DirectoryOfThisScript()
        }

    compilation_info = GetCompilationInfoForFile(filename)
    if not compilation_info:
        return None

    final_flags = list(compilation_info.compiler_flags_)

    return {
        'flags': final_flags,
        'include_paths_relative_to_dir': compilation_info.compiler_working_dir_
    }


import json
import ast
import re
out=set()
with  open('E:\文本内容计算\mySpider\split\splitprocorpus3.txt',encoding='utf-8') as f:
    for a in f:
        if a.startswith(u'\ufeff'):
            a= a.encode('utf8')[3:].decode('utf8')
    #a=json.loads(a)   #将字符串转换为字典
        a=ast.literal_eval(a)
        b=a['value']
        words = [str(i) for i in b['words']]
        words = ''.join(words)
        tuple = (words,b['documentId'])
        out.add(tuple)

list_out=list(out)
#print(list_out[0],type(list_out[0]))   #列表元素是元组
f = open('E:\文本内容计算\mySpider\quchong\corpus.txt', 'a+',encoding='utf-8')
for line in list_out:
     f.write(str(line)+"\n")
print("OK!!!!!!!!!")


__author__ = 'Martin Isenburg'
__date__ = 'October 2014'
__copyright__ = '(C) 2023, rapidlasso GmbH'

import os
from qgis.core import QgsProcessingParameterEnum
from qgis.core import QgsProcessingParameterBoolean

from ..LAStoolsUtils import LAStoolsUtils
from ..LAStoolsAlgorithm import LAStoolsAlgorithm

class lasgridPro(LAStoolsAlgorithm):

    ATTRIBUTE = "ATTRIBUTE"
    METHOD = "METHOD"
    ATTRIBUTES = ["elevation", "intensity", "rgb", "classification"]
    METHODS = ["lowest", "highest", "average", "stddev"]
    USE_TILE_BB = "USE_TILE_BB"

    def initAlgorithm(self, config):
        self.addParametersPointInputFolderGUI()
        self.addParametersPointInputMergedGUI()
        self.addParametersFilter1ReturnClassFlagsGUI()
        self.addParametersStepGUI()
        self.addParameter(QgsProcessingParameterEnum(lasgridPro.ATTRIBUTE, "Attribute", lasgridPro.ATTRIBUTES, False, 0))
        self.addParameter(QgsProcessingParameterEnum(lasgridPro.METHOD, "Method", lasgridPro.METHODS, False, 0))
        self.addParameter(QgsProcessingParameterBoolean(lasgridPro.USE_TILE_BB, "use tile bounding box (after tiling with buffer)", False))
        self.addParametersOutputDirectoryGUI()
        self.addParametersOutputAppendixGUI()
        self.addParametersRasterOutputFormatGUI()
        self.addParametersRasterOutputGUI()
        self.addParametersAdditionalGUI()
        self.addParametersCoresGUI()
        self.addParametersVerboseGUI64()

    def processAlgorithm(self, parameters, context, feedback):
        commands = [os.path.join(LAStoolsUtils.LAStoolsPath(), "bin", "lasgrid")]
        self.addParametersVerboseCommands64(parameters, context, commands)
        self.addParametersPointInputFolderCommands(parameters, context, commands)
        self.addParametersPointInputMergedCommands(parameters, context, commands)
        self.addParametersFilter1ReturnClassFlagsCommands(parameters, context, commands)
        self.addParametersStepCommands(parameters, context, commands)
        attribute = self.parameterAsInt(parameters, lasgridPro.ATTRIBUTE, context)
        if (attribute != 0):
            commands.append("-" + lasgridPro.ATTRIBUTES[attribute])
        method = self.parameterAsInt(parameters, lasgridPro.METHOD, context)
        if (method != 0):
            commands.append("-" + lasgridPro.METHODS[method])
        if (self.parameterAsBool(parameters, lasgridPro.USE_TILE_BB, context)):
            commands.append("-use_tile_bb")
        self.addParametersOutputDirectoryCommands(parameters, context, commands)
        self.addParametersOutputAppendixCommands(parameters, context, commands)
        self.addParametersRasterOutputFormatCommands(parameters, context, commands)
        self.addParametersRasterOutputCommands(parameters, context, commands)
        self.addParametersAdditionalCommands(parameters, context, commands)
        self.addParametersCoresCommands(parameters, context, commands)

        LAStoolsUtils.runLAStools(commands, feedback)

        return {"": None}

    def name(self):
        return 'lasgridPro'

    def displayName(self):
        return 'lasgridPro'

    def group(self):
        return 'folder - raster derivatives'

    def groupId(self):
        return 'folder - raster derivatives'

    def createInstance(self):
        return lasgridPro()


"""
The following is a work in progress to clone the
game "Tetris". The ownership of the game goes to
Henk Rogers and Alexey Pajitnov and I claim no
ownership of the game.
"""

import pygame
import random
pygame.init()

# size of window and setting it up
display_width = 550
display_height = 550
gameDisplay = pygame.display.set_mode((display_width, display_height))

# frame tick
clock = pygame.time.Clock()

# font
largefont = pygame.font.SysFont("timesnewroman", 135)

# words on screen
word = [largefont.render('PAUSE', False,(255,255,255))]

# images for the game
backgroundImage = pygame.image.load("TetrisBoard.png")
squares = pygame.image.load("Squares.png")

# music
pygame.mixer.music.load('TetrisTheme.mp3')
pygame.mixer.music.play(-1)

### One Block ###
def base(x,y,squareList):
    # draws the one block
    gameDisplay.blit(squares,(x,y),(0,0,25,25))

    # if the block hits the ground or hits the top of another block, return the squareList
    if y == 525 or gameDisplay.get_at((x+12, y+36))!=((1,1,1,255)):
        squareList.append((x,y,0))
        return squareList
    # otherwise, return 1
    else:
        return 1

### Long Block ###
def longBlock(x,y,squareList,rotation):
    # draws all 4 blocks
    gameDisplay.blit(squares,(x,y),(75,0,25,25))
    gameDisplay.blit(squares,(x,y+25),(75,0,25,25))
    gameDisplay.blit(squares,(x,y+50),(75,0,25,25))
    gameDisplay.blit(squares,(x,y+75),(75,0,25,25))

    # if the block hits the ground or hits the top of another block, return the squareList
    if y == 450 or gameDisplay.get_at((x+12, y+112))!=((1,1,1,255)):
        squareList.append((x,y,3))
        squareList.append((x,y+25,3))
        squareList.append((x,y+50,3))
        squareList.append((x,y+75,3))
        return squareList
    # otherwise, return 1
    else:
        return 1

### Game Method ###
def gameLoop():
    # sets base value
    gameExit = pause = False
    squareOnScreen = 1

    # x value and y value of block
    xVal = 275
    y = 0

    # rotation of block
    rotation = 0

    # list for all the blocks on screen
    squareList = []

    ### Game Starts ###
    while not gameExit:
        for event in pygame.event.get():
            # if exit is pressed on window, exit the window
            if event.type == pygame.QUIT:
                gameExit = True

            if event.type == pygame.KEYDOWN:
                # moves the block left and right as long as the game isn't paused
                if event.key == pygame.K_LEFT:
                    if gameDisplay.get_at((xVal-12, y+12))==((1,1,1,255)) and not pause:
                        xVal-=25
                elif event.key == pygame.K_RIGHT:
                    if gameDisplay.get_at((xVal+36, y+36))==((1,1,1,255)) and not pause:
                        xVal+=25

                # space pauses and unpauses the game
                elif event.key == pygame.K_SPACE:
                    if not pause:
                        pause = True
                    else:
                        pause = False

        # fill screen with white
        gameDisplay.fill((255,255,255))
        # background image is placed on screen
        gameDisplay.blit(backgroundImage,(0,0),(0,0,550,550))

        # if the squareList isn't empty, draw squares on screen
        if len(squareList) != 0:
            for x in squareList:
                gameDisplay.blit(squares,(int(x[0]),int(x[1])),(25*int(x[2]),0,25,25))

        # if the game is paused, draw the word "PAUSED" on screen
        if pause == True:
            gameDisplay.blit(word[0],(91,187))
        # otherwise, bring the blocks down
        else:
            # updates block position
            squareOnScreen = longBlock(xVal,y,squareList,rotation)

            # if squareOnScreen does not return 1, and instead returns a list
            if not squareOnScreen == 1:
                # update the list
                squareList = squareOnScreen

                # renew the values
                xVal = 275
                y = 0
            # bring the block down
            y += 25

        # update the screen and tick for 5 frames
        pygame.display.update()
        clock.tick(5)

    ### End The Game Start ###

    # closes the game window
    pygame.quit()

### End of the Game Method ###

# calls the gameLoop method
gameLoop()


from .library import loaded_as_addon, running_in_blender

bl_info = {
    "name": "SourceIO",
    "author": "RED_EYE, ShadelessFox, Syborg64",
    "version": (5, 2, 0),
    "blender": (3, 1, 0),
    "location": "File > Import-Export > SourceEngine assets",
    "description": "GoldSrc/Source1/Source2 Engine assets(.mdl, .bsp, .vmt, .vtf, .vmdl_c, .vwrld_c, .vtex_c)"
                   "Notice that you cannot delete this addon via blender UI, remove it manually from addons folder",
    "category": "Import-Export"
}
if running_in_blender() and loaded_as_addon():
    from .blender_bindings.bindings import register, unregister

    if __name__ == "__main__":
        register()


import operator
import itertools
import copy
from math import *

from ROOT import TLorentzVector, TVectorD

from CMGTools.RootTools.fwlite.Analyzer import Analyzer
from CMGTools.RootTools.fwlite.Event import Event
from CMGTools.RootTools.statistics.Counter import Counter, Counters
from CMGTools.RootTools.fwlite.AutoHandle import AutoHandle
from CMGTools.RootTools.physicsobjects.Lepton import Lepton
from CMGTools.RootTools.physicsobjects.Photon import Photon
from CMGTools.RootTools.physicsobjects.Electron import Electron
from CMGTools.RootTools.physicsobjects.Muon import Muon
from CMGTools.RootTools.physicsobjects.Jet import Jet

from CMGTools.RootTools.utils.DeltaR import *

import ROOT

import os

class ttHMetEventAnalyzer( Analyzer ):
    def __init__(self, cfg_ana, cfg_comp, looperName ):
        super(ttHMetEventAnalyzer,self).__init__(cfg_ana,cfg_comp,looperName)

    def declareHandles(self):
        super(ttHMetEventAnalyzer, self).declareHandles()
        self.handles['met'] = AutoHandle( 'slimmedMETs', 'std::vector<pat::MET>' )
        self.handles['nopumet'] = AutoHandle( 'slimmedMETs', 'std::vector<pat::MET>' )
        self.handles['cmgCand'] = AutoHandle( self.cfg_ana.candidates, self.cfg_ana.candidatesTypes )
        self.handles['vertices'] =  AutoHandle( "offlineSlimmedPrimaryVertices", 'std::vector<reco::Vertex>', fallbackLabel="offlinePrimaryVertices" )

    def beginLoop(self):
        super(ttHMetEventAnalyzer,self).beginLoop()
        self.counters.addCounter('events')
        count = self.counters.counter('events')
        count.register('all events')

    def makeTkMETs(self, event):
        event.tkMet = 0

        charged = []
        pfcands = self.handles['cmgCand'].product()

        for i in xrange(pfcands.size()):

## ===> require the Track Candidate charge and with a  minimum dz

            if (pfcands.at(i).charge()!=0 and (abs(pfcands.at(i).dz())<=self.cfg_ana.dzMax )):

                charged.append(pfcands.at(i))

        import ROOT
        event.tkMet = ROOT.reco.Particle.LorentzVector(-1.*(sum([x.px() for x in charged])) , -1.*(sum([x.py() for x in charged])), 0, 0 )
##        print 'tkmet',event.tkMet.pt(),'tkmetphi',event.tkMet.phi()

    def makeMETs(self, event):
        event.met = self.handles['met'].product()[0]
        event.metNoPU = self.handles['nopumet'].product()[0]
        if hasattr(event, 'deltaMetFromJetSmearing'):
            import ROOT
            px,py = event.met.px()+event.deltaMetFromJetSmearing[0], event.met.py()+event.deltaMetFromJetSmearing[1]
            event.met.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, hypot(px,py)))
            px,py = event.metNoPU.px()+event.deltaMetFromJetSmearing[0], event.metNoPU.py()+event.deltaMetFromJetSmearing[1]
            event.metNoPU.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, hypot(px,py)))
        if hasattr(event, 'deltaMetFromJEC') and event.deltaMetFromJEC[0] != 0 and event.deltaMetFromJEC[1] != 0:
            import ROOT
            px,py = event.met.px()+event.deltaMetFromJEC[0], event.met.py()+event.deltaMetFromJEC[1]
            event.met.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, hypot(px,py)))
            px,py = event.metNoPU.px()+event.deltaMetFromJEC[0], event.metNoPU.py()+event.deltaMetFromJEC[1]
            event.metNoPU.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, hypot(px,py)))

    def makeMETNoMu(self, event):
        event.metNoMu = copy.deepcopy(self.handles['met'].product()[0])
        event.metNoMuNoPU = copy.deepcopy(self.handles['nopumet'].product()[0])

        mupx = 0
        mupy = 0
        #sum muon momentum
        for mu in event.selectedMuons:
            mupx += mu.px()
            mupy += mu.py()

        #subtract muon momentum and construct met
        if hasattr(event, 'deltaMetFromJetSmearing'):
            import ROOT
            px,py = event.metNoMu.px()+event.deltaMetFromJetSmearing[0]-mupx, event.metNoMu.py()+event.deltaMetFromJetSmearing[1]-mupy
            event.metNoMu.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, hypot(px,py)))
            px,py = event.metNoMuNoPU.px()+event.deltaMetFromJetSmearing[0]-mupx, event.metNoMuNoPU.py()+event.deltaMetFromJetSmearing[1]-mupy
            event.metNoMuNoPU.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, hypot(px,py)))
        if hasattr(event, 'deltaMetFromJEC') and event.deltaMetFromJEC[0] != 0 and event.deltaMetFromJEC[1] != 0:
            import ROOT
            px,py = event.metNoMu.px()+event.deltaMetFromJEC[0]-mupx, event.metNoMu.py()+event.deltaMetFromJEC[1]-mupy
            event.met.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, hypot(px,py)))
            px,py = event.metNoMuNoPU.px()+event.deltaMetFromJEC[0]-mupx, event.metNoMuNoPU.py()+event.deltaMetFromJEC[1]-mupy
            event.metNoMuNoPU.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, hypot(px,py)))

    def process(self, iEvent, event):
        self.readCollections( iEvent )
        self.counters.counter('events').inc('all events')

        self.makeMETs(event)
        event.tkMet = 0

        if self.cfg_ana.doTkMet:
            self.makeTkMETs(event);

        if self.cfg_ana.doMetNoMu:
            self.makeMETNoMu(event)

        return True


# [BAEKJOON] 1918. 후위표기식

cal = input()
stk = []  # 연산자 담을 스택
lst = []  # 답 저장용
prior = {'*': 2, '/':2, '-':1, '+': 1, '(':0}  # 우선순위 생각 (큰 수가 먼저)

for i in range(len(cal)):
    if cal[i].isalpha():  # 알파벳이면 append
        lst.append(cal[i])
    elif cal[i]=='(':
        stk.append(cal[i])
    elif cal[i]==')':
        while stk[-1]!='(':
            lst.append(stk.pop())
        stk.pop()
    else:
        while stk and prior[cal[i]] <= prior[stk[-1]]:  # stk이 있고 현재 우선순위가 스택top보다 낮으거나 같으면 (같으면: ++인 경우 하나는 뽑아줘야되니까
            lst.append(stk.pop())  # 스택 다 뽑아내고
        stk.append(cal[i])  # 자기자신을 스택으로 (이렇게 해야 숫자 뒤에 앞연산자들이 나옴)
while stk:  # 스택에 머 남아있으면 다 더해줘야지 후위연산 완성
    lst.append(stk.pop())

ans = ''.join(lst)
print(ans)


# -*- coding: utf-8 -*-
# @Author: MirrorAi
# @Date:   2020-01-02 22:10:37
# @Last Modified by:   MirroAi
# @Last Modified time: 2020-01-02 22:26:11

# 输入某年某月某日，判断这一天是这一年的第几天？

month_day = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

def function(year, month, day):

    flag = 0
    if ((year%4==0 and year%100!=0) or (year%400==0)) and month>2:
        flag = 1

    days = 0
    for i in range(month-1):
        days += month_day[i]

    return days + flag + day

def run():

    year, month, day = input("请输入年月日：（格式YYYY-MM-DD）\n").split("-")
    print("该日是当年的第%d天"%function(int(year), int(month), int(day)))

if __name__ == '__main__':
    run()


from langchain.prompts import PromptTemplate

class SystemPrompt:
    def questionPrompt(self):
        condense_qa_template = """
            Given the following conversation and a follow up question, rephrase the follow up question
            to be a standalone question, and add an instruction in the question to answer from the contexts provided.

            Chat History:
            {chat_history}
            Follow Up Input: {question}
            Standalone question:"""

        CUSTOM_QUESTION_PROMPT = PromptTemplate.from_template(condense_qa_template)

        return CUSTOM_QUESTION_PROMPT


# -*- coding: utf-8 -*-
"""
Created on Fri Jan 10 12:56:54 2020

@author: -
"""

#######################################################################

# Moving Average Forecasting

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pandas_datareader as pdr

stock_hist = pdr.get_data_yahoo('ORCL', start = '2015-01-01')
# get rid of Adj Close
stock_hist = stock_hist.drop('Adj Close', axis=1)
# round to 2 decimal places
stock_hist = round(stock_hist,2)

# create 5 day average
# stock_hist['5-day'] = stock_hist['Close'].rolling(5).mean()

# print(stock_hist)

# this assumes you aready know the closing price which we do not
# so we have to use shift()
# roll forecast foward one period.
stock_hist['5-day'] = stock_hist['Close'].rolling(5).mean().shift()

# print(stock_hist)

# plot 5-day alongside close.
plt.plot(stock_hist['Close'][-120:])
plt.plot(stock_hist['5-day'][-120:])

# so we are off a little since the end of the graph does not line up

# put in error measure
# Mean Absolute Deviation
stock_hist['MAD'] = np.abs(stock_hist['Close'] - stock_hist['5-day'])

print('Mean Absolute Deviation')
print(stock_hist['MAD'].mean())
# this shows how much you are off in dollars positive or negative

# account for the scaling of the data we are using
# Mean Absolute Percent Error
stock_hist['MAPE'] = stock_hist['MAD'] / stock_hist['Close']
print('Mean Absolute Percent Error')
print(stock_hist['MAPE'].mean())

# Mean Square Error
# this resembles the variance and therefore the std dev
stock_hist['MSE'] = stock_hist['MAD'] ** 2
print('Mean Square Error')
print(stock_hist['MSE'].mean())
# This is the square as the name implies

MSE = stock_hist['MSE'].mean()

RMSE = np.sqrt(MSE)
print('Relative Mean Square Error')
print(RMSE)

# go through with 10 day and see if you get better error measures.
# rolling(5) -> rolling(10)


from django.urls import path
from . import views

urlpatterns = [
    path('news/', views.NewsView.as_view()),
    path('news/<int:pk>/', views.NewsDetailAPIView.as_view()),
    path('news/<int:pk>/upvote/', views.UpvoteNews.as_view()),
    path('comments/', views.CommentsView.as_view()),
    path('comments/<int:pk>/', views.CommentsDetailAPIView.as_view(), name='comment-detail'),

]


#import libraries and initialize pyGame
import sys, pygame, pygame.mixer
from pygame.locals import *
pygame.init()

size = width, height = 1024, 650
music = pygame.mixer.Sound("Assets/music/TwistedNerve.ogg")
screen = pygame.display.set_mode(size)
pygame.display.set_caption('Kill Bill - The Game')

charHealth = 100

strtImage = pygame.image.load("Assets/img/KillBillGameBkg.png")
strtButtonImage1 = pygame.image.load("Assets/img/startGame1.png")
strtButtonImage2 = pygame.image.load("Assets/img/startGame2.png")
instructButtonImage1 = pygame.image.load("Assets/img/insturct1.png")
instructButtonImage2 = pygame.image.load("Assets/img/insturct2.png")
press_space = pygame.image.load("Assets/img/press_space.png")
blank = pygame.image.load("Assets/img/blank.png")

hover = 1
level = 0
numTicks = pygame.time.get_ticks()
iters = 0
frame = 0

screen.fill((252, 255, 0))
screen.blit(strtImage, (0,0))
screen.blit(strtButtonImage2, (0, 514))
screen.blit(instructButtonImage1, (513, 514))
pygame.display.flip()
music.play()

while level == 0:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == KEYDOWN and event.key == K_ESCAPE:
            sys.exit()
        elif event.type == KEYDOWN and event.key == K_RIGHT and hover == 1:
            screen.blit(strtButtonImage1, (0, 514))
            screen.blit(instructButtonImage2, (513, 514))
            pygame.display.flip()
            hover = 2
        elif event.type == KEYDOWN and event.key == K_LEFT and hover == 2:
            screen.blit(strtButtonImage2, (0, 514))
            screen.blit(instructButtonImage1, (513, 514))
            pygame.display.flip()
            hover = 1
        elif event.type == KEYDOWN and event.key == K_SPACE:
            if hover == 1:
                screen.fill((0,0,0))
                pygame.display.flip()
                level += 1
            elif hover == 2:
                pass

    if frame == 0 and pygame.time.get_ticks() - numTicks == 500:
        screen.blit(press_space, (0, 578))
        pygame.display.flip()
        numTicks = pygame.time.get_ticks()
        frame = 1
        iters = 0
    elif frame == 1 and pygame.time.get_ticks() - numTicks == 500:
        screen.blit(blank, (0, 578))
        pygame.display.flip()
        numTicks = pygame.time.get_ticks()
        frame = 0
        iters = 0

intro = pygame.image.load("Assets/img/intro.png")
level1 = pygame.image.load("Assets/img/TheBloodSpateredBride.png")
playLevelStart = True
screen.blit(intro, (0,0))
pygame.display.flip()
music.stop()
music = pygame.mixer.Sound("Assets/music/BangBang.ogg")
music.play()

while level == 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == KEYDOWN and event.key == K_ESCAPE:
            sys.exit()
    if pygame.time.get_ticks() - numTicks == 3000 and playLevelStart:
        screen.blit(level1, (0,0))
        pygame.display.flip()
    elif pygame.time.get_ticks() - numTicks == 7500 and playLevelStart:
        screen.fill((0,0,0))
        pygame.display.flip()
        playLevelStart = False
        music.stop()