import matplotlib.pyplot as plt
import pandas as pd

"""
Read in and analyze the Oregon dam data.
"""

df = pd.read_csv("data/dam/oregondams.csv")
cfreqs = df["county"].value_counts() # frequency of each county
cindex = df["county"].value_counts().index # county name

plt.bar(cindex, cfreqs)
plt.xticks(range(len(cindex)), cindex, rotation="vertical")
plt.xlabel("County")
plt.ylabel("Number of dams")
plt.title("Dam frequency by county")
plt.savefig("output/dam/freq.png")


import os
import torch
from tqdm import tqdm

from baseline.retriever.eval_sentence_retriever import eval_sentence_obj
from roberta_sentence_arg_parser import get_parser

from transformers import AutoTokenizer
from torch.utils.data import DataLoader

from roberta_sentence_preprocessor import BasePreprocessor
from my_utils import set_seed, save_jsonl_data

from roberta_cls import RobertaCls
from roberta_sentence_generator import RobertaSentenceGenerator
import graph_cell_config as config

from database.feverous_db import FeverousDB

def main(args):
    if args is None:
        args = get_parser().parse_args()
    assert args.test_ckpt is not None
    set_seed(args.seed)

    print(args)

    tokenizer = AutoTokenizer.from_pretrained(args.bert_name)

    preprocessor = BasePreprocessor(args)

    args.label2id = config.label2idx
    args.id2label = dict(zip([value for _, value in args.label2id.items()], [key for key, _ in args.label2id.items()]))
    args.config = config
    args.tokenizer = tokenizer
    args.db = FeverousDB(args.wiki_path)

    if args.model_type == "RobertaCls":
        model = RobertaCls(args)
        data_generator = RobertaSentenceGenerator
    else:
        assert False, args.model_type

    args.test_mode = True

    # train_data, valid_data, test_data = preprocessor.process(
    #     args.data_dir, args.cache_dir, data_generator, args.tokenizer, dataset=["dev"])
    # train_data, valid_data, test_data = preprocessor.process(
    #     args.data_dir, args.cache_dir, data_generator, args.tokenizer, dataset=["dev", "train", "test"])
    train_data, valid_data, test_data = preprocessor.process(
        args.data_dir, args.cache_dir, data_generator, args.tokenizer, dataset=["test"])

    load_model_path = os.path.join(args.ckpt_root_dir, args.test_ckpt)

    ckpt_meta = model.load(load_model_path)
    model.to(args.device)

    if valid_data:
        dev_dataloader = DataLoader(valid_data, args.batch_size, shuffle=False)
        val(model, dev_dataloader, "dev", args)
    if train_data:
        train_dataloader = DataLoader(train_data, args.batch_size, shuffle=False)
        val(model, train_dataloader, "train", args)
    if test_data:
        test_dataloader = DataLoader(test_data, args.batch_size, shuffle=False)
        val(model, test_dataloader, "test", args)

@torch.no_grad()
def val(model, dataloader, data_type, args):
    """
    计算模型在验证集上的准确率等信息
    """
    dataloader.dataset.print_example()

    output_path = os.path.join(args.data_dir, f"{data_type}.sentences.roberta.p5.s5.jsonl" + "." + args.test_ckpt)
    print(f"save sentence scores to {output_path}")

    preds_epoch = []

    model.eval()
    for ii, data_entry in tqdm(enumerate(dataloader)):
        res = model(data_entry, args, test_mode=True)
        score_pos, _, golds = res
        preds = list(score_pos.cpu().detach().numpy())
        preds_epoch.extend(preds)

    cand_id_lst = dataloader.dataset.cand_id_lst
    assert len(preds_epoch) == sum([len(ci) for ci in cand_id_lst]), print(len(preds_epoch), sum([len(ci) for ci in cand_id_lst]))
    res = []
    sent_scores = []
    stat = 0
    preds_epoch = [float(p) for p in preds_epoch]
    for cand_ids in cand_id_lst:
        pred_scores = preds_epoch[stat:stat+len(cand_ids)]
        preds = list(torch.topk(torch.tensor(pred_scores), k=min(5, len(pred_scores)))[1].numpy())
        pred_ids = [cand_ids[idx] for idx in preds]
        sent_scores.append(list(zip(cand_ids, pred_scores)))
        res.append(pred_ids)
        stat += len(cand_ids)

    odata = []
    for entry, preds, sent_score in zip(dataloader.dataset.raw_data, res, sent_scores):
        oentry = {
            "id": entry["id"],
            "claim": entry["claim"],
            "predicted_sentences": preds,
            "sentence_scores": sent_score
        }
        odata.append(oentry)
    if not data_type == "test":
        eval_sentence_obj(odata, data_type)
    save_jsonl_data(odata, output_path)
    return res

if __name__ == '__main__':
    main(None)


# -*- coding: utf-8 -*-
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import argparse

# See https://github.com/pytorch/examples/blob/main/time_sequence_prediction/train.py

def fake_sine_wave_date():
    np.random.seed(2)
    T = 20
    L = 1000
    N = 100

    x = np.empty((N, L), 'int64')  # shape: [100 x 1000]
    x[:] = np.array(range(L)) + np.random.randint(-4 * T, 4 * T, N).reshape(N, 1)  # shape: [100 x 1000]
    data = np.sin(x / 1.0 / T).astype('float64')  # shape: [100 x 1000]
    print(f"data shape: {np.shape(data)}")
    torch.save(data, open('traindata.pt', 'wb'))

class Sequence(nn.Module):

    def __init__(self):
        super(Sequence, self).__init__()
        self.lstm1 = nn.LSTMCell(1, 51)
        self.lstm2 = nn.LSTMCell(51, 51)
        self.linear = nn.Linear(51, 1)

    def forward(self, input, future=0):
        outputs = []

        # init
        h_t = torch.zeros(input.size(0), 51, dtype=torch.double).to(device)
        c_t = torch.zeros(input.size(0), 51, dtype=torch.double).to(device)
        h_t2 = torch.zeros(input.size(0), 51, dtype=torch.double).to(device)
        c_t2 = torch.zeros(input.size(0), 51, dtype=torch.double).to(device)

        for input_t in input.split(1, dim=1):
            h_t, c_t = self.lstm1(input_t, (h_t, c_t))
            h_t2, c_t2 = self.lstm2(h_t, (h_t2, c_t2))
            output = self.linear(h_t2)
            outputs += [output]

        for i in range(future):
            h_t, c_t = self.lstm1(output, (h_t, c_t))
            h_t2, c_t2 = self.lstm2(h_t, (h_t2, c_t2))
            output = self.linear(h_t2)
            outputs += [output]
        outputs = torch.cat(outputs, dim=1)
        return outputs

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="time sequence example")
    parser.add_argument('--steps', type=int, default=15, help='steps to run')
    opt = parser.parse_args()

    # set device
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    device = torch.device(device)

    # set random seed to 0
    np.random.seed(0)
    torch.manual_seed(0)

    # load data and make training set.
    data = torch.load('traindata.pt')
    input = torch.from_numpy(data[3:, :-1]).to(device)
    target = torch.from_numpy(data[3:, 1:]).to(device)
    test_input = torch.from_numpy(data[:3, :-1]).to(device)
    test_target = torch.from_numpy(data[:3, 1:]).to(device)

    # build model
    seq = Sequence()
    seq.double().to(device)
    mse_loss = nn.MSELoss().to(device)

    # use LBFGS as optimizer since we can load the whole data to train
    optimizer = optim.LBFGS(seq.parameters(), lr=0.8)

    # begin to train
    for i in range(opt.steps):
        print('STEP: ', i)
        def closure():
            optimizer.zero_grad()
            out = seq(input)
            loss = mse_loss(out, target)
            print("loss:", loss.item())
            loss.backward()
            return loss
        optimizer.step(closure)

        # begin to predict, no need to track gradient here
        with torch.no_grad():
            future = 1000
            pred = seq(test_input, future=future)
            loss = mse_loss(pred[:, :-future], test_target)
            print("test loss:", loss.item())


import socket
import random
import re
import logging
"""
class Sink(object):
    def write(self, *args, **kwargs):
        pass
    def writeln(self, *args, **kwargs):
        pass

class Base(object):
    def __init__(self, **kwargs):
        super(Base,self).__init__()
        if "logTo" in kwargs:
            if kwargs["logTo"] == None:
                self.log = Sink()
                self.errlog = Sink()
            else:
                self.log = kwargs["logTo"]
                self.errlog = kwargs["logTo"]
        else:
            self.log = sys.stdout
            self.errlog = sys.stderr
"""
def wsUrl_str(t):
    if t.startswith("ws://"):
        return t
    else:
        raise ValueError("Not a ws:// format url")

def udpUrl_str(t):
    m = re.match("^udp://([^:/]+):([0-9]+)$",t)
    if m:
        return iphost_str(m.group(1)), int(m.group(2))
    else:
        raise ValueError("Not a udp://<host>:<port> url")

def iphost_str(t):
    """Pass through a string provide it represents an ip address, or convert from a host name to string representation of ip address
    otherwise raise an exception if not possible"""
    try:
        socket.inet_aton(t)
        return t
    except socket.error:
        try:
            return socket.gethostbyname(t)
        except:
            raise ValueError("Not a recognised/resolvable host name or is an IP address string not of the form nnn.nnn.nnn.nnn")

def ipaddr_str(t):
    """Pass through a string provided it represents an ip address (not a host name) otherwise raise an exception"""
    try:
        socket.inet_aton(t)
    except socket.error:
        raise ValueError("IP address string not of the form nnn.nnn.nnn.nnn")
    return t

def port_int(p):
    """Pass through a port number (as a number or a string) provided it is valid and in range, otherwise raise an exception"""
    try:
        port=int(p)
    except:
        raise ValueError("Invalid port number")

    if port>=0 and port<=65535:
        return port
    else:
        raise ValueError("Port number out of range")

def port_int_or_random(p):
    """Same as port_int() except will also accept "random" in which case a random port between 10000 and 20000 is chosen"""
    if p=="random":
        return random.randrange(10000,20000)
    else:
        return port_int(p)

def parse_logLevel(levelName):
    levelName=levelName.lower()
    for n in range(logging.NOTSET,logging.CRITICAL+1):
        name=logging.getLevelName(n).lower()
        if levelName == name:
            return n
    raise ValueError("Logging level name not recognised")


import sqlite3
import matplotlib.pyplot as plt

def text_format(text):
    text = text.replace("&", "&")
    text = text.replace(">", ">")
    text = text.replace("<", "<")
    return text

def loadData(c):
    c.execute("SELECT TWEET_TEXT, RTS, FAVS FROM TWEETS")
    cursor = list(c)
    tweetInfo = [(len(text_format(record[0])), record[1], record[2])
                 for record in cursor]
    return tweetInfo

def spreadInfo(tweetInfo):
    rtsPerTweet = [0] * 141
    favsPerTweet = [0] * 141
    numberOfTweet = [0] * 141

    for tweet in tweetInfo:
        rtsPerTweet[tweet[0]] += tweet[1]
        favsPerTweet[tweet[0]] += tweet[2]
        numberOfTweet[tweet[0]] += 1

    return [rtsPerTweet, favsPerTweet, numberOfTweet]

if __name__ == "__main__":
    print ("Connecting to database...")
    conn = sqlite3.connect('db/tweetBank.db')
    c = conn.cursor()

    print ("Loading data...")
    tweetInfo = loadData(c)

    # Closing the connection
    conn.close()

    print ("Doing some calculations...")
    info = spreadInfo(tweetInfo)

    rtsPerTweet = info[0]
    favsPerTweet = info[1]
    numberOfTweet = info[2]

    rtsAverage = [float(rt)/length if length != 0 else 0
                  for rt, length in zip(rtsPerTweet, numberOfTweet)]
    favsAverage = [float(fav)/length if length != 0 else 0
                   for fav, length in zip(favsPerTweet, numberOfTweet)]

    fig, ax = plt.subplots()
    # Minimum 7 to avoid seeing the points of 0 retweets and
    # a little bit more in the max
    plt.xlabel("Number of characters in a tweet")
    plt.ylabel("Average of interactions")
    lineRts, = ax.plot(range(141), rtsAverage, alpha=0.5,
                       label='average Rts', color='green')
    lineFavs, = ax.plot(range(141), favsAverage, alpha=0.5,
                        label='average Favs', color='orange')
    first_legend = plt.legend(handles=[lineRts, lineFavs], loc=1)
    ax = plt.gca().add_artist(first_legend)

    fig2, ax2 = plt.subplots()
    line, = ax2.plot(range(141), numberOfTweet, alpha=0.5,
                     label='amountOfTweets')
    plt.xlabel("Number of characters in a tweet")
    plt.ylabel("Amount of tweets gathered")
    second_legend = plt.legend(handles=[line], loc=1)
    ax2 = plt.gca().add_artist(second_legend)
    plt.show()


from common.python.storage.impl.clickhouse_storage import ClickHouseStorage
from common.python.utils.log_utils import schedule_logger
from flow.cycle_actions.flow_action_queue.worker.base_flow_action_worker import BaseFlowActionWorker

class SaveOutputDataWorker(BaseFlowActionWorker):
    """
    The processor when the save_output_data signal is received
    """

    def work(self, params):
        name = params.get('name')
        namespace = params.get('namespace')
        # fcs
        fcs_name = params.get('fcs_info').get('name')
        fcs_namespace = params.get('fcs_info').get('namespace')
        partitions = params.get('partitions')

        schedule_logger().info('schedule save data {}'.format(params))

        self.save_to_ck(name, namespace, fcs_name, fcs_namespace, partitions)

    @staticmethod
    def save_to_ck(name, namespace, fcs_name, fcs_namespace, partitions):
        """
            save fc data into clickHouse table
        Parameters
        ----------
        name: clickHouse table name
        namespace: clickHouse namespace
        fcs_name: fc table name
        fcs_namespace: fc namespace
        partitions: data is divided into several partitions on fc table

        Returns
        -------

        """
        from common.python.calculation.fc.fc_storage import FCStorage
        # get fcs object
        fcs = FCStorage(namespace=fcs_namespace, name=fcs_name, partitions=partitions)
        data = fcs.collect()
        ck_storage = ClickHouseStorage(_type=None, namespace=namespace, name=name)
        ck_storage.put_all(data)


from django.db import models
from django.contrib.auth.models import AbstractUser

# Create your models here.

class User(AbstractUser):
    is_rider = models.BooleanField('Rider', default=False)
    is_driver = models.BooleanField('Driver', default=False)

class RideHistory(models.Model):

    SHARE = 1
    MINI_GO = 2
    RIDE_TYPE = (
        (SHARE, 'share'),
        (MINI_GO, 'mini_go'),
    )
    driver = models.ForeignKey(User, related_name="driver", on_delete=models.CASCADE, blank=True, null=True)
    ride_type = models.IntegerField(default=MINI_GO, choices=RIDE_TYPE)
    active = models.BooleanField(default=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

class RideTrack(models.Model):

    REQUESTED = 1
    ACCEPTED = 2
    ONGOING = 3
    COMPLETED = 4
    REJECTED = 5
    RIDE_STATE = (
        (REQUESTED, 'requested'),
        (ACCEPTED, 'accepted'),
        (ONGOING, 'ongoing'),
        (COMPLETED, 'completed'),
        (REJECTED, 'rejected'),
    )
    ride_state = models.IntegerField(default=0, choices=RIDE_STATE)
    history = models.ForeignKey(RideHistory, related_name="history", on_delete=models.CASCADE, blank=True, null=True)
    rider = models.ForeignKey(User, related_name="rider", on_delete=models.CASCADE, blank=True, null=True)
    seats = models.IntegerField(default=0, blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)


import base64
import json
import logging
import os
import time
import uuid

import certifi
from Crypto.Cipher import AES
from requests.packages.urllib3.util.retry import Retry
from urllib3 import PoolManager, exceptions

log_level = os.getenv('LogLevel')
mp_env = os.getenv('mParticleEnvironment')
mP_feed_key = os.getenv('mParticleFeedKey')
mP_feed_secret = os.getenv('mParticleFeedSecret')
uid_key = os.getenv('UIDKey')
uid_secret = os.getenv('UIDSecret')
uid_url = os.getenv('UIDURL')
event_name = os.getenv('EVENT_NAME')

logging.basicConfig(level=log_level)

def encrypt_request(secret: str, email: str):
    """
    This function will accept a secret and an email and build a request to trade-desk per the spec's defined
    here: https://github.com/UnifiedID2/uid2docs/blob/main/api/v2/encryption-decryption.md#encrypting-requests
    :param secret: Secret to retrieve UID2.0 from a public or private operator
    :param email: Email or list of emails to retrieve the raw UID2.0 values for
    :return: Encrypted request to be sent to trade-desk
    """

    # Secret key and payload
    secret = base64.b64decode(secret)
    payload = f'{{"email": ["{email}"]}}'

    # Random 12 byte initialization vector
    iv = os.urandom(12)
    cipher = AES.new(secret, AES.MODE_GCM, nonce=iv)
    time_milliseconds = int(time.time() * 1000)
    nonce = os.urandom(8)

    # Construct a request body
    body = bytearray(time_milliseconds.to_bytes(8, 'big'))
    body += bytearray(nonce)
    body += bytearray(bytes(payload, 'utf-8'))
    ciphertext, tag = cipher.encrypt_and_digest(body)
    # version of the envelope format
    envelope = bytearray(b'\x01')
    # 96 digit initialization vector, to randomize data encryption
    envelope += bytearray(iv)
    # Payload encrypted using AES/GCM/NO Padding algorithm
    envelope += bytearray(ciphertext)
    # 128 big authentication tage to verify data integrity
    envelope += bytearray(tag)

    return base64.b64encode(bytes(envelope)).decode()

def ttd_connector(api_key: str, payload: str, uid_url: str):
    """
    This function accepts an API Key and pay load to make a request to TTD and returns the status
    :param api_key: API Key to retrieve UID2.0 from a public or private operator
    :param payload: Encrypted payload
    :return: status
    """
    retry_strategy = Retry(
        total=5,
        status_forcelist=[429, 500, 502, 503, 504],
        allowed_methods=["GET", "POST"],
        # {backoff factor} * (2 ** ({number of total retries} - 1))
        backoff_factor=1
    )
    http = PoolManager(num_pools=2, retries=retry_strategy, ca_certs=certifi.where())
    headers = {
        'Authorization': f"Bearer {api_key}",
    }
    try:
        response = http.request('POST', url=uid_url, body=payload, headers=headers)
        if response.status == 200:
            return {"status": "success", "response_body": response.data.decode('utf-8'), "status_code": 200}
        if response.status == 401:
            return {"status": "Invalid UID Credentials", "status_code": 401}
        else:
            return {"status": "Error from UID Service", "status_code": 500}
    except Exception as error:
        logging.debug(error)
        return {"status": "error", "message": error, "status_code": 500}
    except exceptions.MaxRetryError as error:
        logging.debug(error)
        return {"status": "error", "status_code": 500}

def decrypt_request(secret: str, response: str):
    """
    This function will accept a secret and decrypt the response per the spec's defined
    here: https://github.com/UnifiedID2/uid2docs/blob/main/api/v2/encryption-decryption.md#decrypting-responses
    :param secret: Secret to retrieve UID2.0 from a public or private operator
    :param response: Response string from UID API
    :return: advertising_id and email
    """
    secret = base64.b64decode(secret)
    response_bytes = base64.b64decode(response)
    iv = response_bytes[:12]
    data = response_bytes[12:len(response_bytes) - 16]
    tag = response_bytes[len(response_bytes) - 16:]
    cipher = AES.new(secret, AES.MODE_GCM, nonce=iv)
    decrypted_response = cipher.decrypt_and_verify(data, tag)
    json_response = json.loads(decrypted_response[16:].decode('utf-8'))
    email = json_response.get('body').get('mapped')[0].get('identifier')
    advertising_id = json_response.get('body').get('mapped')[0].get('advertising_id')
    return {"email": email, "advertising_id": advertising_id, "status_code": 200}

def mparticle_connector(feed_key: str, feed_secret: str, advertising_id: str, email: str, ):
    """
    This function loads the user UID token as other ID in mParticle and a custom event
    :param email: User's email
    :param advertising_id: User's advertising_id token
    :param feed_key: Customer Feed Key
    :param feed_secret: Customer Feed Secret
    :param event_name: Custom Event name
    :return: status
    """
    retry_strategy = Retry(
        total=5,
        status_forcelist=[429, 500, 502, 503, 504],
        allowed_methods=["GET", "POST"],
        # {backoff factor} * (2 ** ({number of total retries} - 1))
        backoff_factor=1
    )
    api_key = f'{feed_key}:{feed_secret}'
    encoded_api_key = base64.b64encode(api_key.encode())
    authentication_key = encoded_api_key.decode()
    http = PoolManager(num_pools=2, retries=retry_strategy, ca_certs=certifi.where())
    headers = {
        'Authorization': f"Basic {authentication_key}",
        'Content-Type': 'application/json'
    }
    request_body = json.dumps({
        "events": [
            {
                "data": {
                    "event_name": event_name,
                    "custom_event_type": "other",
                    "source_message_id": str(uuid.uuid4())
                },
                "event_type": "custom_event"
            }
        ],
        "partner_identities": {
            "ttd_uid2": advertising_id
        },
        "user_identities": {
            "email": email,

        },
        "environment": mp_env
    })
    try:
        response = http.request('POST', url='https://s2s.mparticle.com/v2/events', body=request_body, headers=headers)
        if response.status == 202:
            return {"status": "success", "status_code": 202}
        if response.status == 401:
            return {"status": "Invalid Feed Credentials", "status_code": 401}
    except Exception as error:
        logging.debug(error)
        return {"status": "error", "message": error, "status_code": 500}
    except exceptions.MaxRetryError as error:
        logging.debug(error)
        return {"status": "error", "status_code": 500}

def lambda_handler(event: dict, context: dict):
    print(json.dumps(event))
    """
    :param event: Event from mParticle Audience Engine
    :param context: Context from the lambda invocation
    :return: Status code for the operation
    """

    # Accept new audience connection requests:
    event_body = json.loads(event.get('body'))

    logging.debug(event_body)
    if event_body.get('type') == 'audience_subscription_request':
        return json.dumps({
            "type": "audience_subscription_response",
            "id": str(uuid.uuid4()),
            "timestamp_ms": round(time.time()),
            "firehose_version": "2.8.0",
        })
    # Process the change requests
    # Handle Verification requests
    user_profiles = event_body.get('user_profiles')
    if user_profiles is None:
        return json.dumps({
            "type": "audience_membership_change_response",
            "id": str(uuid.uuid4()),
            "timestamp_ms": round(time.time()),
            "firehose_version": "2.8.0",
            "suspend_subscription": False

        })

    # Parse out the credentials from the request
    api_key = event_body.get('account').get('account_settings').get('uidKey')
    api_secret = event_body.get('account').get('account_settings').get('uidSecret')
    feed_key = event_body.get('account').get('account_settings').get('feedKey')
    feed_secret = event_body.get('account').get('account_settings').get('feedSecret')
    for user_identities in user_profiles:
        for i in (user_identities.get('user_identities')):
            if i.get('type') == 'email':
                # get the users email
                user_email = i.get('value')
                # Encrypt users email
                encrypted_payload = encrypt_request(api_secret, user_email)
                # Request UID for the user
                ttd_response = ttd_connector(api_key, encrypted_payload, uid_url)
                # If there is a successful response from TTD, decrypt the response
                if ttd_response.get('status_code') == 200:
                    encrypted_response = ttd_response.get('response_body')
                    decrypted_response = decrypt_request(api_secret, encrypted_response)
                    # If the decryption is successful,parse out email and UID
                    if decrypted_response.get('status_code') == 200:
                        email = decrypted_response.get('email')
                        uid = decrypted_response.get('advertising_id')
                        # Upload to mParticle
                        mp_response = mparticle_connector(feed_key, feed_secret, uid, email)
                        logging.debug(mp_response)

    return json.dumps(
        {
            "type": "audience_membership_change_response",
            "id": str(uuid.uuid4()),
            "timestamp_ms": round(time.time()),
            "firehose_version": "2.8.0",
            "suspend_subscription": False

        }
    )


from ninja import ModelSchema
from pydantic import Field

from example.models import Coin, CoinMarketData

class CoinSchema(ModelSchema):
    class Config:
        model = Coin
        model_fields = ["name", "symbol"]

class CoinMarketDataSchema(ModelSchema):
    coin: str = Field(..., alias="coin.name")

    class Config:
        model = CoinMarketData
        model_fields = [
            "id",
            "operation_id",
            "date",
            "high",
            "low",
            "open",
            "close",
            "volume",
            "marketcap",
        ]

class CoinMarketDataListSchema(ModelSchema):
    class Config:
        model = CoinMarketData
        model_fields = ["date", "high", "low", "open", "close", "volume", "marketcap"]


#coding: UTF-8
from selenium import webdriver
from time import sleep
# 下载地址
# http://chromedriver.storage.googleapis.com/index.html
driver=webdriver.Chrome()
driver.maximize_window()
driver.get('http://www.baidu.com')
driver.find_element_by_id("kw").send_keys("刘德华")
driver.find_element_by_id("su").click()

sleep(5)
driver.quit()


import Queue

from flask import Flask
app = Flask(__name__)

my_queue = Queue.Queue()

with open("data.txt") as f:
    for s in f.read().splitlines():
        my_queue.put(s)

@app.route('/')
def next_item_from_queue():
    item = my_queue.get()
    my_queue.put(item)
    return item

if __name__ == '__main__':
    app.run()


#User function Template for python3
class Unionfind:
    def __init__(self,n):
        self.parent=[i for i in range(n+1)]
        self.rank=[0 for i in range(n+1)]
    def union(self,u,v):
        parent_u=self.find(u)
        parent_v=self.find(v)
        if parent_u==parent_v:
            return True
        if self.rank[parent_u]<self.rank[parent_v]:
            self.parent[parent_u]=parent_v
            self.rank[parent_v]+=self.rank[parent_u]
        else:
            self.parent[parent_v]=parent_u
            self.rank[parent_u]+=self.rank[parent_v]

    def find(self,u):
        if self.parent[u]==u:
            return u
        self.parent[u]=self.find(self.parent[u])
        return self.parent[u]

from typing import List
class Solution:
    def numOfIslands(self, rows: int, cols : int, operators : List[List[int]]) -> List[int]:
        obj=Unionfind(rows*cols)
        m1=[0,-1,0,1]
        m2=[1,0,-1,0]
        ans=[]
        count=0
        visit=[[0]*m for i in range(n)]
        for row,col in operators:
            if visit[row][col]:
                ans.append(count)
                continue
            visit[row][col]=1
            count+=1
            for i in range(4):
                newrow=row+m1[i]
                newcol=col+m2[i]
                if newrow>=0 and newrow<rows and newcol>=0 and newcol<cols:
                    if visit[newrow][newcol]==1:
                        n1=(newrow*m)+newcol
                        n2=(row*m)+col
                        if obj.find(n1)!=obj.find(n2):
                            count-=1
                            obj.union(n1,n2)
            ans.append(count)
        return ans

#{
 # Driver Code Starts
#Initial Template for Python 3

if __name__=="__main__":
    T = int(input())
    for t in range(T):

        n = int(input())
        m = int(input())
        k = int(input())
        operators = []
        for i in range(k):
            u, v = map(int, input().strip().split())
            operators.append([u, v])
        obj = Solution()
        ans = obj.numOfIslands(n, m, operators)
        for i in ans:
            print(i, end = ' ')
        print()

# } Driver Code Ends

Number Of Islands - GFG/number-of-islands.py


# -*- coding: utf-8 -*-
import time
import pyautogui
import os

# 屏幕DPR
DPR = 2

_region = (0, 40, 420, 750)
REGION = tuple([v * DPR for v in _region])

cwd = os.getcwd()
btnBox = os.path.join(cwd, 'img/btnBox.png')

# sc = pyautogui.screenshot(region = REGION)
# sc.show()

class Click:
    def __init__(self, offsetx: int, offsety: int):
        self.offsetx = offsetx
        self.offsety = offsety

    # 对找到的location去重
    def unify(self, locations):
        dict = {}
        for location in locations:
            top = location.top
            dict[top] = location

        return list(dict.values())

    def run(self, appImg, answers, rightAnswer):
        locations = pyautogui.locateAllOnScreen(
            btnBox, region=REGION, confidence=0.9)
        locations = [v for v in locations]

        unifyLocations = self.unify(locations)
        locationsLen = len(unifyLocations)

        rightAnswerIdx = answers.index(rightAnswer)

        try:
            if rightAnswerIdx >= 0 and locationsLen > 0 and locationsLen < 4:
                rightLocation = unifyLocations[rightAnswerIdx]
                x, y = pyautogui.center(rightLocation)

                pyautogui.click(x / DPR, y / DPR)
                time.sleep(0.05)
                pyautogui.click(x / DPR, y / DPR)
            else:
                print('--无法自动化操作--', rightAnswerIdx, unifyLocations, locations)
        except:
            print('自动化报错：', rightAnswerIdx, unifyLocations, locations)

        # return self._splitCapture(img)

# cc = Click(0, 77)

# cc.run(None, None, None)


import cv2
import numpy as np

# callback fucntion for Trackbar returns None
def non(x):
    return None

# assingning Window name for Trackbar
cv2.namedWindow("img")
# creating Trackbar for Lower and Uper value of Hue , Saturation and Value
cv2.createTrackbar('hueL', 'img', 0, 359, non)
cv2.createTrackbar('satL', 'img', 0, 255, non)
cv2.createTrackbar('valL', 'img', 0, 255, non)
cv2.createTrackbar('hueU', 'img', 0, 359, non)
cv2.createTrackbar('satU', 'img', 0, 255, non)
cv2.createTrackbar('valU', 'img', 0, 255, non)

# the main loop
while (True):
    img = cv2.imread('colorballs.jpg')
    img = cv2.resize(img, (400, 400), interpolation=cv2.INTER_AREA)

    # gettind Trackbars' positions and storing it.
    hueL = cv2.getTrackbarPos('hueL', 'img')
    satL = cv2.getTrackbarPos('satL', 'img')
    valL = cv2.getTrackbarPos('valL', 'img')
    hueU = cv2.getTrackbarPos('hueU', 'img')
    satU = cv2.getTrackbarPos('satU', 'img')
    valU = cv2.getTrackbarPos('valU', 'img')

    # converting picture from BGR format to HSV
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    # creating a array of lower and uper HSV value
    l_b = np.array([hueL, satL, valL])
    u_b = np.array([hueU, satU, valU])

    mask = cv2.inRange(hsv, l_b, u_b)

    res = cv2.bitwise_and(img, img, mask=mask)

    cv2.imshow('img', res)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()


#  python-holidays
#  ---------------
#  A fast, efficient Python library for generating country, province and state
#  specific sets of holidays on the fly. It aims to make determining whether a
#  specific date is a holiday as fast and flexible as possible.
#
#  Authors: dr-prodigy <dr.prodigy.github@gmail.com> (c) 2017-2023
#           ryanss <ryanssdev@icloud.com> (c) 2014-2017
#  Website: https://github.com/dr-prodigy/python-holidays
#  License: MIT (see LICENSE file)

from datetime import date

from holidays.calendars import _get_nth_weekday_of_month
from holidays.constants import JAN, FEB, MAR, MAY, SEP, NOV, DEC, MON
from holidays.holiday_base import HolidayBase

class Mexico(HolidayBase):
    """
    References:
    - https://en.wikipedia.org/wiki/Public_holidays_in_Mexico
    - https://es.wikipedia.org/wiki/Anexo:D%C3%ADas_festivos_en_M%C3%A9xico
    - https://www.gob.mx/cms/uploads/attachment/file/156203/1044_Ley_Federal_del_Trabajo.pdf  # noqa: E501
    - http://www.diputados.gob.mx/LeyesBiblio/ref/lft/LFT_orig_01abr70_ima.pdf
    """

    country = "MX"
    default_language = "es"

    def _populate(self, year):
        super()._populate(year)

        # New Year's Day.
        self[date(year, JAN, 1)] = self.tr("Año Nuevo")

        if year >= 1917:
            self[
                _get_nth_weekday_of_month(1, MON, FEB, year)
                if year >= 2006
                else date(year, FEB, 5)
                # Constitution Day.
            ] = self.tr("Día de la Constitución")

        if year >= 1917:
            self[
                _get_nth_weekday_of_month(3, MON, MAR, year)
                # no 2006 due to celebration of the 200th anniversary
                # of Benito Juárez in 2006
                if year >= 2007
                else date(year, MAR, 21)
                # Benito Juárez's birthday.
            ] = self.tr("Natalicio de Benito Juárez")

        if year >= 1923:
            # Labour Day.
            self[date(year, MAY, 1)] = self.tr("Día del Trabajo")

        # Independence Day.
        self[date(year, SEP, 16)] = self.tr("Día de la Independencia")

        if year >= 1917:
            self[
                _get_nth_weekday_of_month(3, MON, NOV, year)
                if year >= 2006
                else date(year, NOV, 20)
                # Revolution Day.
            ] = self.tr("Día de la Revolución")

        if year >= 1970 and (year - 1970) % 6 == 0:
            # Change of Federal Government.
            self[date(year, DEC, 1)] = self.tr(
                "Transmisión del Poder Ejecutivo Federal"
            )

        # Christmas Day.
        self[date(year, DEC, 25)] = self.tr("Navidad")

class MX(Mexico):
    pass

class MEX(Mexico):
    pass


import os
import torch
import loaddatas as lds
import torch.nn.functional as F
import numpy as np
from baselines import TLCGNN as TLCGNN
from sklearn.metrics import roc_auc_score,average_precision_score
from torch.nn.init import xavier_normal_ as xavier

def train():
    model.train()
    optimizer.zero_grad()
    emb = model.encode(data)
    x, y = model.decode(data, emb)
    loss = F.binary_cross_entropy(x,y)
    loss.backward()
    optimizer.step()
    return x

def test():
    model.eval()
    accs = []
    emb = model.encode(data)
    for type in ["val", "test"]:
        pred,y = model.decode(data,emb,type=type)
        pred,y = pred.cpu(),y.cpu()
        if type == "val":
            accs.append(F.binary_cross_entropy(pred, y))
            pred = pred.data.numpy()
            roc = roc_auc_score(y, pred)
            accs.append(roc)
            acc = average_precision_score(y,pred)
            accs.append(acc)
        else:
            pred = pred.data.numpy()
            roc = roc_auc_score(y, pred)
            accs.append(roc)
            acc = average_precision_score(y, pred)
            accs.append(acc)
    return accs

def weights_init(m):
    if isinstance(m, torch.nn.Linear):
        xavier(m.weight)
        if not m.bias is None:
            torch.nn.init.constant_(m.bias, 0)

def setup_seed(seed):
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    np.random.seed(seed)
    torch.backends.cudnn.deterministic = True

#d_names = ['PPI']; times=range(20)
d_names = ["Photo", "PubMed", "Computers"]; times=range(50)

wait_total= 200
total_epochs = 2000

pipelines=['TLCGNN']
pipeline_acc={'TLCGNN':[i for i in times]}
pipeline_acc_sum={'TLCGNN':0}
pipeline_roc={'TLCGNN':[i for i in times]}
pipeline_roc_sum={'TLCGNN':0}
pipeline_acc_same={'TLCGNN':[i for i in times]}
pipeline_acc_same_sum={'TLCGNN':0}
pipeline_roc_same={'TLCGNN':[i for i in times]}
pipeline_roc_same_sum={'TLCGNN':0}
pipeline_acc_diff={'TLCGNN':[i for i in times]}
pipeline_acc_diff_sum={'TLCGNN':0}
pipeline_roc_diff={'TLCGNN':[i for i in times]}
pipeline_roc_diff_sum={'TLCGNN':0}

if not os.path.exists("./scores"):
    os.mkdir("./scores")

for d_name in d_names:
    f2 = open('scores/pipe_benchmark_' + d_name + '_LP_scores.txt', 'w+')
    f2.write('{0:7} {1:7}\n'.format(d_name, 'TLCGNN'))
    f2.flush()
    dataset = lds.loaddatas(d_name)
    for data_cnt in times:
        for Conv_method in pipelines:
            if d_name in ['Rand_nnodes_github1000', 'PPI']:
                data = dataset[data_cnt]
            else:
                data = dataset[0]
            if d_name in ['Rand_nnodes_github1000']:
                data.x = data.x[:, :10]
            #data.x = torch.ones(data.x.size())
            index = [i for i in range(len(data.y))]
            if d_name != "PPI":
                model, data = locals()[Conv_method].call(data, dataset.name, data.x.size(1), dataset.num_classes,
                                                     data_cnt)
            else:
                model, data = locals()[Conv_method].call(data, 'PPI', data.x.size(1), dataset.num_classes,
                                                         data_cnt)
            model.apply(weights_init)
            optimizer = torch.optim.Adam(model.parameters(), lr=0.005, weight_decay=0)
            best_val_acc = test_acc_same = test_acc_diff = test_acc = 0.0
            best_val_roc = test_roc_same = test_roc_diff = test_roc = 0.0
            best_val_loss = np.inf
            # train and val/test
            wait_step = 0

            # train and test
            for epoch in range(1, total_epochs + 1):
                pred = train()
                val_loss, val_roc, val_acc, tmp_test_roc, tmp_test_acc = test()
                if val_roc >= best_val_roc:
                    test_acc = tmp_test_acc
                    test_roc = tmp_test_roc
                    best_val_acc = val_acc
                    best_val_roc = val_roc
                    best_val_loss = val_loss
                    wait_step = 0
                else:
                    wait_step += 1
                    if wait_step == wait_total:
                        print('Early stop! Min loss: ', best_val_loss, ', Max accuracy: ', best_val_acc,
                              ', Max roc: ', best_val_roc)
                        break
            del model
            del data
            # print result

            pipeline_acc[Conv_method][data_cnt] = test_acc
            pipeline_roc[Conv_method][data_cnt] = test_roc

            log = 'Epoch: ' + str(
                total_epochs) + ', dataset name: ' + d_name + ', Method: ' + Conv_method + ' Test pr: {:.4f}, roc: {:.4f} \n'
            print((log.format(pipeline_acc[Conv_method][data_cnt], pipeline_roc[Conv_method][data_cnt])))
            #print(pred)

            f2.write('{}, {:.4f}, {:.4f}\n'.format(data_cnt, pipeline_acc[Conv_method][data_cnt],
                                                     pipeline_roc[Conv_method][data_cnt],))
            f2.flush()
    f2.write('{0:4} {1:4f}\n'.format('std', np.std(pipeline_acc[Conv_method])))
    f2.write('{0:4} {1:4f}\n'.format('mean', np.mean(pipeline_acc[Conv_method])))
    f2.write('{0:4} {1:4f}\n'.format('std', np.std(pipeline_roc[Conv_method])))
    f2.write('{0:4} {1:4f}\n'.format('mean', np.mean(pipeline_roc[Conv_method])))
    f2.close()


"""
Project: myP2PSync
@author: Francesco Lorenzo Casciaro - Politecnico di Torino - UPC

This code manages the organization of synchronized files in a myP2PSync client application.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
"""

import json

import fileManagement

class FileTree:
    """
    Customized data structure used to store the organization of files in directories.
    It's similar to a tree with a set of roots.
    Each root is related to a group of the peer.
    """

    def __init__(self):
        """
        Initialize FileTree dictionary for group roots.
        """
        self.groups = dict()

    def addGroup(self, groupTree):
        """
        Add a group root to the user tree.
        :param groupTree: root of the group tree
        :return: void
        """
        groupName = groupTree.nodeName
        self.groups[groupName] = groupTree

    def getGroup(self, groupName):
        """
        Return the root of the group tree associated to the name groupName.
        :param groupName: name of the group
        :return: Node object representing the root of the group tree
        """
        if groupName in self.groups:
            return self.groups[groupName]
        else:
            return None

    def print(self):
        """
        Function used to print the tree.
        :return: void
        """
        for group in self.groups:
            group.print(0)

class Node:
    """
    Class used to describe the structure of a node of the FileTree.
    """

    def __init__(self, nodeName, isDir, file = None):

        # name of the node, e.g. filename or directory name
        self.nodeName = nodeName
        # boolean value: True -> node represents a directory, otherwise a file
        self.isDir = isDir

        if self.isDir:
            self.file = None
            # dict used to store all the nodes associated to file in the directory
            self.childs = dict()
        else:
            # File object
            self.file = file
            self.childs = None

    def addChild(self, child):
        """
        Add a child node to the self node dictionary.
        :param child: Node object
        :return: void
        """

        if self.isDir:
            if child.nodeName not in self.childs:
                self.childs[child.nodeName] = child
            else:
                print("ERROR: CHILD ALREADY ADDED")
        else:
            print("ERROR: TRYING TO ADD A CHILD TO A FILE NODE")

    def print(self, level):
        """
        Function used to print the files tree.
        :param level: level of the node in the tree in terms of depth
        :return: void
        """

        levelStr = "-" * level * 5
        levelStr += ">"

        if self.isDir:
            print("{} Dirname: {}".format(levelStr, self.nodeName))
            for child in self.childs.values():
                child.print(level+1)
        else:
            print("{} Filename: {}".format(levelStr, self.file.filename))

    def findNode(self, treePath):
        """
        Find and return a node following a path in the tree.
        :param treePath: it's the path to follow
        :return: Node object if found or None
        """
        current = self

        for name in treePath.split("/"):
            if name in current.childs:
                current = current.childs[name]
            else:
                # path not found
                return None

        return current

    def addNode(self, treePath, file):
        """
        Add a node in a tree with root in the node.
        If a directory node in the middle is not present, the function creates it.
        :param treePath: path to follow to reach where the node will be placed
        :param file: file Object
        :return: boolean (True for success, False for failure)
        """

        current = self
        treePathFields = treePath.split("/")
        n = len(treePathFields)
        # traverse the tree
        for i in range(0, n):
            field = treePathFields[i]

            if field in current.childs:
                if i == n - 1:
                    # file node found
                    print("NODE ALREADY INSERTED")
                    return False
                else:
                    current = current.childs[field]

            else:
                if i < n - 1:
                    # addDir node
                    node = Node(field, True)
                    current.addChild(node)
                    current = node
                else:
                    # add File node
                    node = Node(field, False, file)
                    current.addChild(node)
                    return True

    def removeNode(self, treePath, removeFileObj):
        """
        Remove a node from the tree with root in the node.
        :param treePath: path to follow to reach the node that will be removed
        :param removeFileObj: boolean value, if True remove also the file Object in the node
        :return: void
        """

        current = self

        # use a nodeList to store all the nodes in the middle
        # between the root and the node that will be removed
        nodeList = list()
        # append root
        nodeList.append(self)

        # traverse the tree
        for name in treePath.split("/"):

            if name in current.childs:
                current = current.childs[name]
                nodeList.append(current)

            else:
                print("NODE NOT FOUND")
                return

        last = nodeList.pop()
        if removeFileObj:
            # delete File object
            del last.file

        parent = nodeList.pop()
        # remove node from the parent dictionary
        del parent.childs[last.nodeName]
        # remove Node object
        del last

        # cut from the tree directory Node without childs
        while len(nodeList) > 0:
            last = parent
            parent = nodeList.pop()
            if len(last.childs) == 0:
                del parent.childs[last.nodeName]
                del last
            else:
                break

    def getFileTreePaths(self):
        """
        Return a list of all the treePath in the tree with root in self Node.
        :return: a list of treePaths
        """
        treePaths = list()

        if self.isDir:
            for child in self.childs.values():
                treePaths.extend(child.getFileTreePathsR(""))
        else:
            return None

        return treePaths

    def getFileTreePathsR(self, treePath):
        """
        Recursive function used by getFileTreePaths in order to
        extract the list of all the treePath inside a tree.
        :param treePath: treePath string
        :return: treePaths list
        """

        treePaths = list()

        if treePath == "":
            treePath = self.nodeName
        else:
            treePath = treePath + "/" + self.nodeName

        if self.isDir:
            for child in self.childs.values():
                treePaths.extend(child.getFileTreePathsR(treePath))
        else:
            treePaths.append(treePath)

        return treePaths

def getFileStatus(previousSessionFile):
    """
    Load a FileTree with information stored in a JSON file.
    :param previousSessionFile: name of the file
    :return: FileTree onject
    """

    fileTree = FileTree()

    try:
        f = open(previousSessionFile, 'r')
        try:
            fileTreeJson = json.load(f)
        except ValueError:
            return fileTree
        f.close()
    except FileNotFoundError:
        print("No previous session session information found")
        return fileTree

    # convert the nested JSON dictionaries into a tree
    for group in fileTreeJson:
        # create root node for the group
        groupTree = Node(group["nodeName"], True, None)
        # fill the group tree
        fillNode(groupTree, group["childs"])
        # add the group tree to the main tree
        fileTree.addGroup(groupTree)

    del fileTreeJson

    print("Previous session information successfully retrieved")
    return fileTree

def fillNode(node, childs):
    """
    Recursively build a tree node.
    :param node: Node object
    :param childs: list of child dictionaries
    :return: void
    """

    for child in childs:
        if child["isDir"]:
            # directory node
            newNode = Node(child["nodeName"], True, None)
            fillNode(newNode, child["childs"])
        else:
            # file node
            fileInfo = child["info"]
            file = fileManagement.File(fileInfo["groupName"], fileInfo["treePath"],
                                       fileInfo["filename"], fileInfo["filepath"],
                                       fileInfo["filesize"], fileInfo["timestamp"],
                                       fileInfo["status"], fileInfo["previousChunks"])
            newNode = Node(child["nodeName"], False, file)
        node.addChild(newNode)

def saveFileStatus(fileTree, sessionFile):
    """
    Save the structure of a File Tree object into a JSON  file.
    :param fileTree: FileTree object
    :param sessionFile: name of the file
    :return: boolean (True for success)
    """

    fileTreeJson = list()

    # convert the tree into a nested dictionaries format (JSON-like)
    for group in fileTree.groups.values():

        # build a dict from a node
        groupInfo = dict()
        groupInfo["nodeName"] = group.nodeName
        groupInfo["isDir"] = True
        groupInfo["childs"] = list()
        groupInfo["info"] = dict()

        # handle possible childs node
        fillChildsInfo(groupInfo, group.childs)
        fileTreeJson.append(groupInfo)

    try:
        f = open(sessionFile, 'w')
        json.dump(fileTreeJson, f, indent=4)
        del fileTreeJson
        f.close()
    except FileNotFoundError:
        print("Error while saving the current file status")
        del fileTreeJson
        return False

    print("Session information successfully saved")
    return True

def fillChildsInfo(groupInfo, childs):
    """
    Fill recursively a dictionary with info about childs node.
    :param groupInfo: dictionary for a certain node
    :param childs: list of childs of the node
    :return: void
    """

    for child in childs.values():
        if child.isDir:
            nestedInfo = dict()
            nestedInfo["nodeName"] = child.nodeName
            nestedInfo["isDir"] = True
            nestedInfo["childs"] = list()
            nestedInfo["info"] = dict()

            fillChildsInfo(nestedInfo, child.childs)

        else:
            nestedInfo = dict()
            nestedInfo["nodeName"] = child.nodeName
            nestedInfo["isDir"] = False
            nestedInfo["childs"] = list()
            nestedInfo["info"] = dict()

            nestedInfo["info"]["groupName"] = child.file.groupName
            nestedInfo["info"]["treePath"] = child.file.treePath
            nestedInfo["info"]["filename"] = child.file.filename
            nestedInfo["info"]["filepath"] = child.file.filepath
            nestedInfo["info"]["filesize"] = child.file.filesize
            nestedInfo["info"]["timestamp"] = child.file.timestamp
            nestedInfo["info"]["status"] = child.file.status
            nestedInfo["info"]["previousChunks"] = child.file.previousChunks

        groupInfo["childs"].append(nestedInfo)


from scipy.signal import convolve2d
from einops import repeat
import numpy as np
import array

import dearpygui.dearpygui as dpg
dpg.create_context()

def gol_step(start_matrix, edges="fill"):
    """Convolve over grid then test for "alive" values"""
    counts = convolve2d(start_matrix, [[1, 1, 1], [1, 7, 1], [
                        1, 1, 1]], boundary=edges, mode="same")
    return np.isin(counts, [9, 10, 3]).astype(np.uint8)

def update_raw_texture():
    # Get current texture data, and convert to 2d slice of 3d space
    state_view = np.frombuffer(state, dtype=np.float32).view().reshape(
        GOL_SIZE, GOL_SIZE, 4)[:, :, 0]
    # Run GOL on state
    state_view = gol_step(state_view)
    # Reshape to texture format
    state_view = np.repeat(state_view, 4)
    state[:] = array.array('f', state_view)

def update_raw_texture_nocopy():
    # Get current texture data, and convert to 3d space
    state_view = np.frombuffer(state, dtype=np.float32).view().reshape(
        GOL_SIZE, GOL_SIZE, 4)
    # Run GOL on 2d slice of state (all positions are equal)
    tmp = state_view[:, :, 0]
    tmp = gol_step(tmp)
    state_view[:] = repeat(tmp, 'h w -> h w c', c=4)

GOL_SIZE = 1000
PAD = int(GOL_SIZE*0.1)
INDENT = int(PAD/2)
WINDOW_SIZE = int(GOL_SIZE + PAD)

# Define start texture
state = np.repeat(np.random.randint(0, 2, size=(GOL_SIZE, GOL_SIZE)), 4)
state = array.array('f', state)
with dpg.texture_registry(show=False):
    dpg.add_raw_texture(width=GOL_SIZE, height=GOL_SIZE,
                        format=dpg.mvFormat_Float_rgba, default_value=state, tag="gol_texture")

with dpg.window(tag="MainWindow"):
    dpg.add_image("gol_texture", pos=(INDENT, INDENT))

dpg.create_viewport(title='GOL', width=WINDOW_SIZE, height=WINDOW_SIZE)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.set_primary_window("MainWindow", True)
while dpg.is_dearpygui_running():
    update_raw_texture_nocopy()
    dpg.render_dearpygui_frame()
dpg.destroy_context()


import base64
import io
from flask import Flask, jsonify, render_template, request, send_file, render_template
import numpy as np
import cv2

app = Flask(__name__)

# Global variable to store the most recent image
streamed_image = None
end = False
capture = False
calib = 0
baseline = 0
modeling = 0
mode = 0
image_data = []

@app.route('/upload-stream', methods=['POST'])
def upload_stream():
    global streamed_image

    content_type = request.headers['Content-Type']
    if content_type != 'application/octet-stream':
        return 'Invalid content type', 400

    byte_list = bytearray()
    while True:
        chunk = request.stream.read(1024)
        if not chunk:
            break
        byte_list.extend(chunk)

    img_arr = np.frombuffer(byte_list, dtype=np.uint8)
    img = cv2.imdecode(img_arr, cv2.IMREAD_COLOR)

    streamed_image = img

    return 'Image stream received and processed'

@app.route('/get-streamed-image', methods=['GET'])
def get_streamed_image():
    global streamed_image

    if streamed_image is None:
        return 'No streamed image available', 404

    # rotated_image = cv2.rotate(streamed_image, cv2.ROTATE_90_CLOCKWISE)
    _, img_encoded = cv2.imencode('.jpg', streamed_image)

    file_object = io.BytesIO(img_encoded.tobytes())

    file_object.seek(0)
    file_object.content_type = 'image/jpeg'

    return send_file(file_object, mimetype='image/jpeg', as_attachment=True, download_name="streamed_image.jpg")

@app.route('/set-measured-image', methods=['POST'])
def process_image():
    global image_data
    frame_bytes = request.get_data()

    frame_arr = np.frombuffer(frame_bytes, np.uint8)

    frame = cv2.imdecode(frame_arr, cv2.IMREAD_COLOR)

    ret, jpeg = cv2.imencode('.jpg', frame)
    frame_bytes = jpeg.tobytes()

    # Convert the frame bytes to Uint8List
    image_data = list(frame_bytes)

    # Return the image data as JSON response
    return jsonify({'imageData': image_data})

#-------------------------------------Getter-----------------------------------------

@app.route('/get-mode', methods=['GET'])
def get_mode():
    global mode
    if mode == 0 :
        calib = 0
        baseline = 0
        modeling = 0
    return jsonify({'mode':mode})

@app.route('/get-calib', methods=['GET'])
def get_calib():
    global calib
    return jsonify({'calib':calib})

@app.route('/get-capture', methods=['GET'])
def get_capture():
    global capture
    result = {'capture':capture}
    capture = False
    return jsonify(result)

@app.route('/get-end', methods=['GET'])
def get_end():
    global end
    result = {'end':end}
    end = False
    return jsonify(result)

@app.route('/get-modeling', methods=['GET'])
def get_modeling():
    global modeling
    global baseline
    return jsonify({'modeling':modeling,'baseline':baseline})

@app.route('/get-measured-image', methods=['GET'])
def get_measured_image():
    global image_data
    return jsonify({'image_data':image_data})

#-------------------------------------Setter-----------------------------------------

@app.route('/set-mode', methods=['POST'])
def set_mode():
    global mode
    mode = request.get_json()['mode']
    return 'saved'

@app.route('/set-calib', methods=['POST'])
def set_calib():
    global calib
    calib = request.get_json()['calib']
    return 'saved'

@app.route('/set-capture', methods=['POST'])
def set_capture():
    global capture
    capture = request.get_json()['capture']
    return 'saved'

@app.route('/set-end', methods=['POST'])
def set_end():
    global end
    end = request.get_json()['end']
    return 'saved'

@app.route('/set-modeling', methods=['POST'])
def set_modeling():
    global modeling
    global baseline
    modeling = request.get_json()['modeling']
    baseline = request.get_json()['baseline']
    return 'saved'

@app.route('/reset', methods=['GET'])
def reset():
    global capture
    global end
    global calib
    global baseline
    global modeling
    capture = False
    end = False
    calib = 0
    baseline = 0
    modeling = 0

    return 'saved'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)


from msilib.schema import ComboBox
from urllib.parse import ParseResultBytes
import requests as r
import math
import os
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from tkinter import filedialog
import threading
from moviepy.editor import *
import webbrowser
headers = {
    "user-agent":"BDownload.exe v1.0",
    "referer":"https://www.bilibili.com",
    }
def BvToAv(Bv):
    BvNo1 = Bv[2:]
    keys = {
        '1':'13', '2':'12', '3':'46', '4':'31', '5':'43', '6':'18', '7':'40', '8':'28', '9':'5',
        'A':'54', 'B':'20', 'C':'15', 'D':'8', 'E':'39', 'F':'57', 'G':'45', 'H':'36', 'J':'38', 'K':'51', 'L':'42', 'M':'49', 'N':'52', 'P':'53', 'Q':'7', 'R':'4', 'S':'9', 'T':'50', 'U':'10', 'V':'44', 'W':'34', 'X':'6', 'Y':'25', 'Z':'1',
        'a': '26', 'b': '29', 'c': '56', 'd': '3', 'e': '24', 'f': '0', 'g': '47', 'h': '27', 'i': '22', 'j': '41', 'k': '16', 'm': '11', 'n': '37', 'o': '2',
        'p': '35', 'q': '21', 'r': '17', 's': '33', 't': '30', 'u': '48', 'v': '23', 'w': '55', 'x': '32', 'y': '14','z':'19'
    }
    BvNo2 = []
    for index, ch in enumerate(BvNo1):
        BvNo2.append(int(str(keys[ch])))
    BvNo2[0] = int(BvNo2[0] * math.pow(58, 6));
    BvNo2[1] = int(BvNo2[1] * math.pow(58, 2));
    BvNo2[2] = int(BvNo2[2] * math.pow(58, 4));
    BvNo2[3] = int(BvNo2[3] * math.pow(58, 8));
    BvNo2[4] = int(BvNo2[4] * math.pow(58, 5));
    BvNo2[5] = int(BvNo2[5] * math.pow(58, 9));
    BvNo2[6] = int(BvNo2[6] * math.pow(58, 3));
    BvNo2[7] = int(BvNo2[7] * math.pow(58, 7));
    BvNo2[8] = int(BvNo2[8] * math.pow(58, 1));
    BvNo2[9] = int(BvNo2[9] * math.pow(58, 0));
    sum = 0
    for i in BvNo2:
        sum += i
    sum -= 100618342136696320
    temp = 177451812

    return str(sum ^ temp)

def AvToCid(Av):
    try:
       r.get("https://www.bilibili.com",headers=headers)
    except:
        cid_list = "connection error"
    else:
        url = "https://api.bilibili.com/x/player/pagelist?aid="+Av+"&jsonp=jsonp"
        text = r.get(url,headers=headers).text
        list_ = eval(text)
        try:
            data_list = list_["data"]
            cid_list = []
        except:
            cid_list = "cannot get value"
        else:
            for i in data_list:
                cid = i["cid"]
                cid_list.append(str(cid))
    return cid_list

def download(Av,Cid,Dpi,Paths,P,L):
    try:
       r.get("https://www.bilibili.com",headers=headers)
    except:
        return_ = "connection error"
    else:
        url = "https://api.bilibili.com/x/player/playurl?fnval=80&avid="+Av+"&cid="+Cid
        text = r.get(url,headers=headers).text
        print(url)
        null = None
        dic = eval(text)
        data = dic['data']
        #print(data)
        dash = data['dash']
        video = dash['video']
        #print(video)
        for item in video:
            if item["id"] == Dpi:# and item["codecid"] == 7:
                base_url = item['base_url']
                print(base_url)
            if item["id"] != Dpi:
                pass
        audio = dash['audio']
        for item in audio:
            if item["id"] == 30232:
                base_url2 = item['base_url']

        exists = os.path.exists(Paths)
        try:
            print(base_url)
        except:
            return_ = "Dpi isnt exists"
        else:
            if exists:
                video_content = r.get(base_url,headers=headers).content
                audio_content = r.get(base_url2,headers=headers).content
                P = str(P)
                try:
                    with open(Paths+"Av"+Av+"Part"+P+".mp4","wb+") as file1,open(Paths+"AV"+Av+"Part"+P+".mp3","wb+") as file2:
                        file1.write(video_content)
                        file2.write(audio_content)
                    ButtomLabel['text'] = '正在合成中……（'+str(int(P))+'/'+str(L)+'）请耐心等待。'
                    video_path = VideoFileClip(Paths+"Av"+Av+"Part"+P+".mp4")
                    audio_path = AudioFileClip(Paths+"AV"+Av+"Part"+P+".mp3")
                    video = video_path.set_audio(audio_path)
                    video.write_videofile(f''+Paths+"Downloaded_Av"+Av+"Part"+P+".mp4",audio_codec='aac')

                except PermissionError:
                    return_ = "permission error"
                else:
                    os.remove(Paths+"Av"+Av+"Part"+P+".mp4")
                    os.remove(Paths+"AV"+Av+"Part"+P+".mp3")
                    return_ = "successfully download"

            if exists == False:
                return_ = "paths error"
    return return_
pink = "#FB7299"
pink2 = "#C7425D"
root = Tk()
root.geometry("310x282")
root.title("BDownloder 必下载 v1.0")
Frame0 = Frame(root,bg="white")
Frame0.pack(fill=BOTH,expand=True)
Label1 = Label(root,text="视频Bv号：",bg="white")
Label1.place(x=20,y=15)
Entry1 = ttk.Entry(root,width=20)
Entry1.place(x=90,y=15)
def Question():
    messagebox.showinfo('说明','''关于Bv号的说明：
    例：https://www.bilibili.com/video/BV1GW411g7mc?spm_id_from=333.337.search-card.all.click
    其中www.bilibili.com/video/后BV**********就是BV号（*有十位）''')
def ChangeColor(event):
    Button0['image'] = Photo12
def ChangeColor2(event):
    Button0['image'] = Photo11
Photo11 = PhotoImage(file="Img/b1-1.gif")
Photo12 = PhotoImage(file="Img/b1-2.gif")
Button0 = Button(root,image=Photo11,bd=0,bg="white",command=Question)
Button0.place(x=250,y=13)
Button0.bind("<Enter>",ChangeColor)
Button0.bind("<Leave>",ChangeColor2)
Label2 = Label(root,text="下载路径：",bg="white",)
Label2.place(x=20,y=55)
Entry2 = ttk.Entry(root,width=20)
Entry2.place(x=90,y=55)
#Entry2.insert(END,"")
Entry2["state"] = "readonly"
def ViewFolder():
    #tf = True
    path = filedialog.askdirectory()
    if path == '':
        pass
    if path != '':
        Entry2['state'] = 'normal'
        Entry2.delete(0,END)
        if len(path) == 3:
            Entry2.insert(END,path)
        if len(path) != 3:
            Entry2.insert(END,path+'/')
        Entry2['state'] = 'readonly'
def ChangeColor11(event):
    Button1['image'] = Photo22
def ChangeColor12(event):
    Button1['image'] = Photo21
Photo21 = PhotoImage(file="Img/b2-1.gif")
Photo22 = PhotoImage(file="Img/b2-2.gif")
Button1 = Button(root,bd=0,image=Photo21,bg="white",command=ViewFolder)
Button1.place(x=250,y=53)
Button1.bind("<Enter>",ChangeColor11)
Button1.bind("<Leave>",ChangeColor12)
Label3 = Label(root,text="选择画质：",bg="white")
Label3.place(x=20,y=95)
Combobox0 = ttk.Combobox(root,width=18,values=("高清 1080P","高清 720P","清晰 480P","流畅 360P"),state="readonly")
Combobox0.place(x=90,y=95)
Combobox0.current(3)
def thread_it(func, *args):
    '''将函数打包进线程'''
    t = threading.Thread(target=func, args=args)
    t.setDaemon(True)
    t.start()
def none():
    messagebox.showerror('错误','正在下载中，请不要操作。')
def download_command():
    global success
    Button2['command'] = none
    Bv = Entry1.get()
    Path = Entry2.get()
    Dpi_ =Combobox0.get()
    if Dpi_ == "高清 1080P":
        Dpi = 80
    elif Dpi_ == "高清 720P":
        Dpi = 64
    elif Dpi_ == "清晰 480P":
        Dpi = 32
    elif Dpi_ == "流畅 360P":
        Dpi = 16
    try:
        Av = BvToAv(Bv)
    except:
        messagebox.showerror('错误','Bv号参数错误。')
    else:
        Cid = AvToCid(Av)
        if Cid =="connection error":
            messagebox.showerror('错误','连接错误。')
        elif Cid == "cannot get value":
            messagebox.showerror('错误','Bv号参数错误。')
        elif Cid != "connection error" and Cid != "cannot get value":
            j = 0
            len_ = len(Cid)
            ButtomLabel['text'] = '状态：正在下载中……（'+str(j+1)+'/'+str(len_)+'）'
            #messagebox.showinfo('提示','开始下载，请点击“确定”继续。')
            for i in Cid:
                j = j + 1
                download_ = download(Av,i,Dpi,Path,j,len_)
                if download_ == "Dpi isnt exists":
                    messagebox.showerror('错误','该视频不支持该清晰度。')
                    success = False
                    break
                elif download_ == "paths error":
                    messagebox.showerror('错误','该路径不存在。')
                    success = False
                    break
                elif download_ == "permission error":
                    messagebox.showerror('错误','本应用程序无权限访问此目录，请以管理员身份运行本应用。')
                    success = False
                    break
                elif download_ != "Dpi isnt exists" and download_ != "paths error" and download_ != "permission error":
                    if j == len_:
                        pass
                    else:
                        ButtomLabel['text'] = '状态：正在下载中……（'+str(j+1)+'/'+str(len_)+'）'
                        #messagebox.showinfo('提示','已下载'+str(j)+'/'+str(len_)+'，请点击“确定”继续。')
                    success = True
            if success == True:
                messagebox.showinfo('提示','下载成功。')
        ButtomLabel['text'] = '状态：等待中……'
        Entry1.delete(0,END)
    Button2['command'] = download_command
photo31 = PhotoImage(file="Img/b3-1.gif")
photo32 = PhotoImage(file="Img/b3-2.gif")
def ChangeColor21(event):
    Button2['image'] = photo32
def ChangeColor22(event):
    Button2['image'] = photo31
Button2 = Button(root,image=photo31,bg="white",bd=0,command=lambda :thread_it(download_command))
Button2.bind("<Enter>",ChangeColor21)
Button2.bind("<Leave>",ChangeColor22)
Button2.place(x=120,y=150)
grey_ = "#F5F5F5"
ButtomLabel = Label(root,text="    状态：等待中……", bd=0, relief=SUNKEN,anchor=W,height=2,bg=grey_)
ButtomLabel.pack(side=BOTTOM, fill=X)
about_ = '''【关于】
应用名称：BDownloder必下载.exe （bilibili站视频下载工具）
应用版本：v1.0
开发者：SbyYz-x00（伞兵营营长）
时间：2022.6
开发者邮箱：stoneyueryouyou@outlook.com
开发者QQ：1932972859

【用户许可协议】
在安装、使用本应用之前，为了保障您和他人的权利，请务必仔细阅读本《用户许可协议》。
1. 本应用为开源、免费软件，
（1） 开发者不接受任何形式对本应用的售卖；
（2） 源码已在Github公布，开发者谢绝任何反编译、反破解以及对应用的注入、篡改的行为；
（3） 欢迎交流，转载，版权归开发者所有，请标明来源。
2. 本应用的使用建立在遵守《中华人民共和国著作权法》的前提条件下，
（1） 请务必依照bilibili和作者对视频版权的说明使用以本应用下载的视频。未经作者允许，不得转载、篡改等操作；
（2） 本应用仅供交流娱乐，为了保护作者的著作权，请务必在24小时内删除本应用下载的视频；
（3） 严禁将由本应用下载的视频用于除娱乐以外的用途，尤其是盈利目的。
3. 本应用有权利对您的计算机作出更改。当涉及权限问题时，会自动请求管理员权限，当管理员同意后，才将继续操作。
如果同意本《用户许可协议》，请继续使用本应用；若不同意，请停止下载/将本应用从您的计算机中卸载。

【免责声明】
1. 本应用旨在倡导文明绿色上网，若用户将由本应用下载的视频用于非法用途，以及不当使用本应用导致的损失，均与开发者无关。
2. 若用户使用本应用下载的视频内容非法，与开发者无关。
3. 若有侵权，请事先协商解决。
开发者邮箱：stoneyueryouyou@outlook.com 开发者QQ：1932972859'''
photo41 = PhotoImage(file="Img/b4-1.gif")
photo42 = PhotoImage(file="Img/b4-2.gif")
def about():
    messagebox.showinfo("关于",about_)
def ChangeColor31(event):
    Button3['image'] = photo42
def ChangeColor32(event):
    Button3['image'] = photo41
Button3 = Button(root,image=photo41,bg=grey_,bd=0,command=about)
Button3.bind("<Enter>",ChangeColor31)
Button3.bind("<Leave>",ChangeColor32)
Button3.place(x=270,y=250)
def openweb():
    webbrowser.open("https://space.bilibili.com/1635778169",new=0,autoraise=True)
    webbrowser.open("https://github.com/SbyYz-x00",new=0,autoraise=True)
    webbrowser.open("https://blog.csdn.net/yueryouyou_sbyyz",new=0,autoraise=True)
def ChangeColor41(event):
    Button4['image'] = photo52
def ChangeColor42(event):
    Button4['image'] = photo51
photo51 = PhotoImage(file="Img/b5-1.gif")
photo52 = PhotoImage(file="Img/b5-2.gif")
Button4 = Button(root,image=photo51,bg=grey_,bd=0,command=openweb)
Button4.bind("<Enter>",ChangeColor41)
Button4.bind("<Leave>",ChangeColor42)
Button4.place(x=240,y=250)
root.mainloop()


import azure.functions as func
import datetime
import json
import base64
import hashlib
import hmac
import requests
import re
import os
import logging
from .state_manager import StateManager

token = os.environ['SentinelOneAPIToken']
domain = os.environ['SentinelOneUrl']
table_name = "SentinelOne"
chunksize = 10000
customer_id = os.environ['WorkspaceID']
shared_key = os.environ['WorkspaceKey']
connection_string = os.environ['AzureWebJobsStorage']
logAnalyticsUri = os.environ.get('logAnalyticsUri')

if ((logAnalyticsUri in (None, '') or str(logAnalyticsUri).isspace())):
    logAnalyticsUri = 'https://' + customer_id + '.ods.opinsights.azure.com'

pattern = r'https:\/\/([\w\-]+)\.ods\.opinsights\.azure.([a-zA-Z\.]+)$'
match = re.match(pattern,str(logAnalyticsUri))
if(not match):
    raise Exception("SentinelOne Data Connector: Invalid Log Analytics Uri.")

class SOne():

    def __init__(self):
        self.domain = domain
        self.header = {
            'Authorization': 'ApiToken {}'.format(token),
            'Content-Type': 'application/json',
            }
        self.from_date, self.to_date = self.generate_date()
        self.results_array = []

    def generate_date(self):
        current_time = datetime.datetime.utcnow() - datetime.timedelta(minutes=10)
        state = StateManager(connection_string)
        past_time = state.get()
        if past_time is not None:
            logging.info("The last time point is: {}".format(past_time))
        else:
            logging.info("There is no last time point, trying to get events for last day.")
            past_time = (current_time - datetime.timedelta(days=1)).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
        state.post(current_time.strftime("%Y-%m-%dT%H:%M:%S.%fZ"))
        return (past_time, current_time.strftime("%Y-%m-%dT%H:%M:%S.%fZ"))

    def get_report(self, report_type_suffix, report_type_name, params = None):
        try:
            r = requests.get(self.domain + report_type_suffix, headers = self.header, params = params)
            if r.status_code == 200:
                self.results_array_join(r.json(), report_type_name)
                next_page_token = (r.json().get('pagination')).get('nextCursor')
                return next_page_token
            elif r.status_code == 400:
                logging.error("Invalid user input received. See error details for further information."
                      " Error code: {}".format(r.status_code))
            elif r.status_code == 401:
                logging.error("Unauthorized access - please sign in and retry. Error code: {}".format(r.status_code))
            else:
                logging.error("Something wrong. Error code: {}".format(r.status_code))
        except Exception as err:
            logging.error("Something wrong. Exception error text: {}".format(err))

    def results_array_join(self, result_element, api_req_name):
        for element in result_element['data']:
            element['event_name'] = api_req_name
            if "data" in element:
                element["DataFields"] = str(json.dumps(element['data']))
                del element['data']
            self.results_array.append(element)

    def reports_list(self):
        reports_api_requests_dict = \
            {
                "activities_created_events": {"api_req": "/web/api/v2.1/activities", "name": "Activities."},
                "agents_created_events": {"api_req": "/web/api/v2.1/agents", "name": "Agents."},
                "agents_updated_events": {"api_req": "/web/api/v2.1/agents", "name": "Agents."},
                "groups_updated_events": {"api_req": "/web/api/v2.1/groups", "name": "Groups."},
                "threats_created_events": {"api_req": "/web/api/v2.1/threats", "name": "Threats."},
                "threats_updated_events": {"api_req": "/web/api/v2.1/threats", "name": "Threats."},
                "alerts_created_events": {"api_req": "/web/api/v2.1/cloud-detection/alerts", "name": "Alerts."}
            }
        for api_req_id, api_req_info in reports_api_requests_dict.items():
            api_req = api_req_info['api_req']
            api_req_name = api_req_info['name']
            if "created_events" in api_req_id:
                api_req_params = {
                    "limit": 1000,
                    "createdAt__gt": self.from_date,
                    "createdAt__lt": self.to_date
                }
            elif "updated_events" in api_req_id:
                api_req_params = {
                    "limit": 200,
                    "updatedAt__gt": self.from_date,
                    "updatedAt__lt": self.to_date
                }
            logging.info("Getting report: {}".format(api_req_id))
            next_page_token = self.get_report(report_type_suffix = api_req, report_type_name = api_req_name, params = api_req_params)
            while next_page_token:
                api_req_params.update({"cursor": next_page_token})
                next_page_token = self.get_report(report_type_suffix=api_req, report_type_name=api_req_name,
                                                  params = api_req_params)

class Sentinel:

    def __init__(self):
        self.logAnalyticsUri = logAnalyticsUri
        self.success_processed = 0
        self.fail_processed = 0
        self.table_name = table_name
        self.chunksize = chunksize
    def gen_chunks_to_object(self, data, chunksize=100):
        chunk = []
        for index, line in enumerate(data):
            if (index % chunksize == 0 and index > 0):
                yield chunk
                del chunk[:]
            chunk.append(line)
        yield chunk

    def gen_chunks(self, data):
        for chunk in self.gen_chunks_to_object(data, chunksize=self.chunksize):
            obj_array = []
            for row in chunk:
                if row != None and row != '':
                    obj_array.append(row)
            body = json.dumps(obj_array)
            self.post_data(body, len(obj_array))

    def build_signature(self, date, content_length, method, content_type, resource):
        x_headers = 'x-ms-date:' + date
        string_to_hash = method + "\n" + str(content_length) + "\n" + content_type + "\n" + x_headers + "\n" + resource
        bytes_to_hash = bytes(string_to_hash, encoding="utf-8")
        decoded_key = base64.b64decode(shared_key)
        encoded_hash = base64.b64encode(
            hmac.new(decoded_key, bytes_to_hash, digestmod=hashlib.sha256).digest()).decode()
        authorization = "SharedKey {}:{}".format(customer_id, encoded_hash)
        return authorization

    def post_data(self, body, chunk_count):
        method = 'POST'
        content_type = 'application/json'
        resource = '/api/logs'
        rfc1123date = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
        content_length = len(body)
        signature = self.build_signature(rfc1123date, content_length, method, content_type,
                                         resource)
        uri = self.logAnalyticsUri + resource + '?api-version=2016-04-01'
        headers = {
            'content-type': content_type,
            'Authorization': signature,
            'Log-Type': self.table_name,
            'x-ms-date': rfc1123date
        }
        response = requests.post(uri, data=body, headers=headers)
        if (response.status_code >= 200 and response.status_code <= 299):
            logging.info("Chunk was processed({} events)".format(chunk_count))
            self.success_processed = self.success_processed + chunk_count
        else:
            logging.info("Error during sending events to Azure Sentinel. Response code:{}".format(response.status_code))
            self.fail_processed = self.fail_processed + chunk_count

def main(mytimer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()
    if mytimer.past_due:
        logging.info("The timer is past due!")
    logging.info("Python timer trigger function ran at %s", utc_timestamp)
    logging.info("Starting program")
    SO = SOne()
    sentinel = Sentinel()
    SO.reports_list()
    SOne_class_vars = vars(SO)
    from_date, to_date = SOne_class_vars["from_date"], SOne_class_vars["to_date"]
    results_array = SOne_class_vars["results_array"]
    sentinel.gen_chunks(results_array)
    sentinel_class_vars = vars(sentinel)
    success_processed, fail_processed = sentinel_class_vars["success_processed"], \
                                        sentinel_class_vars["fail_processed"]
    logging.info("Total events processed successfully: {}, failed: {}. Period: {} - {}"
          .format(success_processed, fail_processed, from_date, to_date))


"""
This module contains functions for linear ODE
"""

import numpy as np
import scipy.integrate as integrate
from utilities import halfcube

def linear_ode_fourier(t,an,bn,trun=30, period=1,y0=5):
    r"""
    Compute the exact solution of a linear differential equation:
     y'=lambda(t)y
     y(0)=y0
    when the lambda(t)=1/2a0+\sum_{i=1}^trun aicos(2\pi/T it)+\sum_{i=1}^trun bisin(2\pi/T it)

    :param t: vector of points in [0,T] where the function need to be evaluated
    :param an: List/np.ndarray of coefficients of sin in Lambda, from a0 to an
    :param bn: List/np.ndarray of coefficients of cos in Lambda, from b0 to bn, b0 is always set to 0 for easier programming
    :param trun: Number of truncation terms
    :param period: Period
    :param y0: y(0)
    :return: function value of y(t)
    """
    trunAn=trunBn=trun
    t = np.array(t)
    if trunAn>len(an)-1:
        trunAn=len(an)-1

    if trunBn>len(bn)-1:
        trunBn=len(bn)-1

    an0=an[0]*0.5*t
    an_coe=np.asarray(an[1:])
    bn_coe=np.asarray(bn[1:])
    an_ran=np.arange(1,trunAn+1,dtype=float)
    bn_ran=np.arange(1,trunBn+1,dtype=float)
    if t.shape:
        length = t.shape[0]
    else:
        length = 1
    an_val=np.reshape(np.repeat(an_coe/(2*np.pi*an_ran/period),length),
                      (trunAn,length))*np.sin(2*np.pi*np.outer(an_ran,t)/period)
    bn_val=np.reshape(np.repeat(bn_coe/(2*np.pi*bn_ran/period),length),
                      (trunBn,length))*(1-np.cos(2*np.pi*np.outer(bn_ran,t)/period))
    return y0*np.exp(an0+np.sum(an_val,axis=0)+np.sum(bn_val,axis=0))

def origin_fun(x,case=1):
    """
    Some functions defined on [0,1] to be expanded by Fourier basis
    :param x:The point where the function need to be evaluated
    :param case: Considered case
    :return: Corresponding function value
    """
    if case==1:
        return 32*x*x*(1-x)*(1-x)
    if case==2:
        return 20*x*(1-x)*(0.5-x)
    if case==3:
        return (1-x)*(x)
    if case==4:
        return x**3

def linear_ode_origin(t,y0=5,case=1):
    """
    Compute the numerical solution of a linear differential equation of one of the original function
    :param t: The point where the function need to be evaluated
    :param y0: y(0)
    :param case: Considered case
    :return:Function value
    """
    return y0*np.exp(integrate.quad(origin_fun, 0, t,args=(case))[0])

def coef_random(an,bn,random_trun_start=0,random_start=1,random_end= 32, halfwidth0=1,pow=-1):
    """
    Takes Fourier coefficients and return a random sample of
    uniform distribution on a hypercube around the input coefficients.
    :param an, bn: Base Fourier coefficient
    :param random_trun_start: Until which term the random perturbation will be added
    :param random_start: decay starting parameter
    :param random_end: decay endding parameter
    :param halfwidth: Half width of interval
    :param pow: Decay power
    :return: random coefficients an, bn
    """

    an=np.asarray(an)
    bn=np.asarray(bn)
    half=halfcube(random_start,random_end,halfwidth0,pow)
    an_random=half*np.random.uniform(-1,1,(random_end-random_start,))
    bn_random=half*np.random.uniform(-1,1,(random_end-random_start,))

    an_random=np.append(np.zeros(random_trun_start-0),an_random)
    bn_random=np.append(np.zeros(random_trun_start-0),bn_random)

    if an.shape[0]>an_random.shape[0]:
        an_random.resize(an.shape)
        bn_random.resize(bn.shape)
    else:
        an.resize(an_random.shape)
        bn.resize(bn_random.shape)
    an_random=an+an_random
    bn_random=bn+bn_random

    return an_random,bn_random

def int_random_fourier(an,bn,random_trun_start=0,random_start=1,random_end= 32,
                  halfwidth0=1,pow=-1,lower=0,upper=1):
    """
    Takes Fourier coefficients and return a random integration value
    :param an, bn: Base Fourier coefficients
    :param random_trun_start: until which term the perturbation will be added
    :param random_start
    :param halfwidth: Half width of interval
    :param pow: Decay power
    :param lower: Lower limit of integration
    :param upper: Upper limit of integration
    :return: Integration value
    """
    an_random,bn_random=coef_random(an,bn,random_trun_start=random_trun_start,random_start=random_start,
                                    random_end= random_end, halfwidth0=halfwidth0,pow=pow)
    int_random=integrate.quad(linear_ode_fourier, lower, upper, args=(an_random,bn_random,len(an_random)-1))[0]
    return int_random


from Bio import SeqIO
import sys

def divide_fasta_into_ref_and_query(ref_query, ref):

    with    open(ref,'r')                as ihandle       :
        refseq_itr         = SeqIO.parse(ihandle, "fasta")
        refseq_name_set    = set()
        for record in refseq_itr:
            refseq_name_set.add(record.name)

    with    open(ref_query,'r')          as ihandle       ,\
            open(ref_query+".ref",'w')   as ohandle_ref   ,\
            open(ref_query+".query",'w') as ohandle_query :
        ref_query_itr      = SeqIO.parse(ihandle,"fasta")
        ref_query_name_set = {}
        for record in ref_query_itr:
            if(record.name in refseq_name_set):
                SeqIO.write(
                    record        ,
                    ohandle_ref   ,
                    "fasta"
                    )
            else:
                SeqIO.write(
                    record        ,
                    ohandle_query ,
                    "fasta"
                    )

if __name__ == "__main__":
    #main function
    argvs=sys.argv
    divide_fasta_into_ref_and_query(argvs[1],argvs[2])


from discord.ext import commands
import discord
import asyncio
import requests
import csv

class Log(commands.Cog):

    def __init__(self, crdbot):
        self.crdbot = crdbot

    @commands.group()
    @commands.has_permissions(manage_channels=True)
    async def log(self, ctx):
        '''
        Group for log-related commands.
        '''
        if ctx.invoked_subcommand is None:
           await ctx.send("Please provide a subcommand.")

    @log.command()
    async def setchannel(self, ctx):
        '''
        Sets the channel logs are dumped to, to the one the command is run in. If no channel is specified logs will not be created.
        Takes no arguments.
        '''
        print("A")
        channel_id = str(ctx.message.channel.id)
        print("B")
        #we use code X001 for log channels
        data = list(csv.reader(open("allinfodump.csv","r"),delimiter=","))
        print("C")

        for line in data:

            if line[0] == "X001":
                #find the line with the log code
                x = 0
                for instance in line:

                    print(instance.split(":")[0])
                    if str(ctx.message.guild.id) == instance.split(":")[0]:
                        print("MATCH! index: {}".format(x))
                        del line[x]
                        line.insert(x,"{}:{}".format(str(ctx.message.guild.id),channel_id))
                        writer = csv.writer(open("allinfodump.csv","w",newline=""))
                        writer.writerows(data)
                        await ctx.send("Log channel updated!")
                        return

                        #there is already an entry for this server so we will overwrite
                    x = x + 1

                #if the code gets here it didn't find the server in the info dump csv, so let's make a new entry
                line.append(str(ctx.message.guild.id)+":"+channel_id)
                await ctx.send("Log channel selected!")
                writer = csv.writer(open("allinfodump.csv","w",newline=""))
                writer.writerows(data)

                #writer = csv.writer(open("allinfodump.csv","w",newline=""))
                #writer.writerows(data)

    async def sendlog(self, server_id, content):
        '''
        Small function that sends logs when called by another function to their respective server.
        '''
        data = list(csv.reader(open("allinfodump.csv","r"),delimiter=","))
        for line in data:
            if line[0] == "X001":
                for instance in line:
                    print(instance.split(":")[0])
                    if str(server_id) == instance.split(":")[0]:
                        logchannel = self.crdbot.get_channel(int(instance.split(":")[1]))

        try:
            await logchannel.send(embed=content)
        except:
            #no log channel
            return

    #log events, I don't think these can be added to a group
    @commands.Cog.listener()
    async def on_message_edit(self, before, after):
        if before.author.bot:
            return

        emb = discord.Embed(
                title = "Message Edit",
                type = "rich",
                description = "[Click to jump]({})".format(after.jump_url),
                colour = 0x0388fc,
                )

        emb.set_author(name=before.author, icon_url=before.author.avatar_url)
        emb.add_field(name="Before",value=before.content, inline=False)
        emb.add_field(name="After",value=after.content, inline=False)
        if before.content == after.content:
            pass
        else:
            emb.add_field(name="Additional Information",value="User ID: {0.author.id}\nMessage ID: {0.id}\nChannel ID: {0.channel.id}".format(before),inline=False)

        emb.add_field(name="Channel",value=before.channel.mention, inline=False)

        await Log.sendlog(self, str(before.guild.id), emb)

    @commands.Cog.listener()
    async def on_message_delete(self, message):

        emb = discord.Embed(
                title = "Message Deleted",
                type = "rich",
                colour = 0xf0280a,
                )

        emb.set_author(name=message.author, icon_url=message.author.avatar_url)
        emb.add_field(name="Content",value=message.content, inline=False)
        emb.add_field(name="Channel",value=message.channel.mention, inline=False)
        emb.add_field(name="Additional Information",value="User ID: {0.author.id}\nMessage ID: {0.id}\nChannel ID: {0.channel.id}".format(message),inline=False)

        await Log.sendlog(self, str(message.guild.id), emb)

    @commands.Cog.listener()
    async def on_guild_channel_delete(self, channel):
        audit = True
        try:
            async for entry in channel.guild.audit_logs(action=discord.AuditLogAction.channel_delete):

                #compare ids because channels probably won't be identical
                if entry.target.id == channel.id and entry.action == discord.AuditLogAction.channel_delete:
                    #print("mhm")

                    user = entry.user
                    break
        except:
            audit = False

        emb = discord.Embed(
                title = "Channel Deleted",
                type = "rich",
                colour = 0xf0280a,
                )

        emb.add_field(name="Channel",value="#"+channel.name, inline=False)

        if audit == True:
            emb.set_author(name=user, icon_url=user.avatar_url)
            emb.add_field(name="Additional Information",value="User ID: {0.id}\nChannel ID: {1.id}".format(user,channel,inline=False))

        else:
            emb.add_field(name="Missing Permissions",value="Missing audit log permissions! Some data will be missing.",inline=False)
            emb.add_field(name="Additional Information",value="Channel ID: {}".format(channel.id),inline=False)

        await Log.sendlog(self, str(channel.guild.id), emb)

    @commands.Cog.listener()
    async def on_guild_channel_create(self, channel):
        audit = True
        try:
            async for entry in channel.guild.audit_logs(action=discord.AuditLogAction.channel_create):

                #compare ids because channels probably won't be identical
                if entry.target.id == channel.id and entry.action == discord.AuditLogAction.channel_create:
                    #print("mhm")

                    user = entry.user
                    break
        except:
            audit = False

        emb = discord.Embed(
                title = "Channel Created",
                type = "rich",
                colour = 0xf0280a,
                )

        emb.add_field(name="Channel",value="#"+channel.name, inline=False)

        if audit == True:
            emb.set_author(name=user, icon_url=user.avatar_url)
            emb.add_field(name="Additional Information",value="User ID: {0.id}\nChannel ID: {1.id}".format(user,channel,inline=False))

        else:
            emb.add_field(name="Missing Permissions",value="Missing audit log permissions! Some data will be missing.",inline=False)
            emb.add_field(name="Additional Information",value="Channel ID: {}".format(channel.id),inline=False)

        await Log.sendlog(self, str(channel.guild.id), emb)

    @commands.Cog.listener()
    async def on_guild_channel_update(self, before, after):

        audit = True
        #try:

        emb = discord.Embed(
                title = "Channel Updated",
                type = "rich",
                colour = 0x0388fc,
                )

        try:
            async for entry in before.guild.audit_logs(action=discord.AuditLogAction.channel_update):
                if entry.target.id == before.id and entry.action == discord.AuditLogAction.channel_update:
                    user = entry.user

                    print(entry)
                    print(entry.changes)

                    print(entry.changes.before)
                    #print(str(entry.changes.before.topic))

                    if hasattr(entry.changes.before, "name"):
                        emb.add_field(name="Channel Name",value="Before: {0.before.name}\nAfter: {0.after.name}".format(entry.changes),inline=False)

                    if hasattr(entry.changes.before, "type"):
                        emb.add_field(name="Channel Type",value="Before: {0.before.type}\nAfter: {0.after.type}".format(entry.changes),inline=False)

                    #position and overwrites cause the bot to read off the last log

                    #if hasattr(entry.changes.before, "position"):
                    #    emb.add_field(name="Channel Position",value="Before: {0.before.position}\nAfter: {0.after.position}".format(entry.changes),inline=False)

                    if hasattr(entry.changes, "overwrites"):
                        emb.add_field(name="Overwrites",value="Placeholder")
                        #embed.add_field(name="Channel Overwrites",value="Before:\n{0.before.}\nAfter: {0.after.}".format(entry.changes),inline=False)

                    if hasattr(entry.changes.before, "topic"):
                        emb.add_field(name="Channel Topic",value="Before: {0.before.topic}\nAfter: {0.after.topic}".format(entry.changes),inline=False)

                    if hasattr(entry.changes.before, "bitrate"):
                        emb.add_field(name="Channel Bitrate",value="Before: {0.before.bitrate}kbps\nAfter: {0.after.bitrate}kbps".format(entry.changes),inline=False)

                    break
        except:
            audit = False

        if audit == True:
            try:
                assert user
            except:
                user = self.crdbot.user
            emb.set_author(name=user, icon_url=user.avatar_url)
            emb.add_field(name="Additional Information",value="User ID: {0.id}\nChannel ID: {1.id}".format(user,after),inline=False)

        else:
            emb.add_field(name="Missing Permissions",value="Missing audit log permissions! Some data will be missing.",inline=False)
            emb.add_field(name="Additional Information",value="Channel ID: {}".format(after.id),inline=False)

        await Log.sendlog(self, str(after.guild.id), emb)

    @commands.Cog.listener()
    async def on_webhooks_update(self, channel):

        emb = discord.Embed(

                type = "rich",
                colour = 0xf0280a,
                )
        #try:
        async for entry in channel.guild.audit_logs(action=discord.AuditLogAction.webhook_create):
            print(entry)

            if entry.action == discord.AuditLogAction.webhook_create:
                user = entry.user
                web_name = entry.after.name
                emb.title = "Webhook Created"

        async for entry in channel.guild.audit_logs(action=discord.AuditLogAction.webhook_update):
            print(entry)
            if entry.action == discord.AuditLogAction.webhook_update:
                user = entry.user
                web_name = entry.after.name
                emb.title = "Webhook Updated"

        async for entry in channel.guild.audit_logs(action=discord.AuditLogAction.webhook_delete):
            print(entry)
            if entry.action == discord.AuditLogAction.webhook_delete:
                user = entry.user
                web_name = entry.after.name
                emb.title = "Webhook Deleted"
        #except:
        #    audit = False

        emb.set_author(name=user, icon_url=user.avatar_url)
        emb.add_field(name="Channel",value=channel.mention, inline=False)
        emb.add_field(name="Webhook Name",value=web_name, inline=False)
        emb.add_field(name="Additional Information",value="User ID: {0.id}\nMessage ID: {0.id}\nChannel ID: {1.id}".format(user,channel),inline=False)

        await Log.sendlog(self, str(channel.guild.id), emb)

    '''
    let's just forget about this one
    @commands.Cog.listener()
    async def on_guild_channel_pins_update(self, channel, foo):
        #"foo" is a timestamp of when the message was pinned which is sort of useless
        audit = True
        try:
            async for entry in channel.guild.audit_logs(action=discord.AuditLogAction.message_pin):
                print(entry)
                #compare ids because channels probably won't be identical
                if entry.target.id == channel.id and entry.action == discord.AuditLogAction.channel_pins_update:
                    #print("mhm")

                    user = entry.user
                    print("YEA")
                    break
        except:
            try:
                async for entry in channel.guild.audit_logs(action=discord.AuditLogAction.message_pin):
                    print(entry)
                    #compare ids because channels probably won't be identical
                    if entry.target.id == channel.id and entry.action == discord.AuditLogAction.channel_pins_update:
                        #print("mhm")

                        user = entry.user
                        print("YUH")
                        break

            except:
                audit = False

        emb = discord.Embed(
                title = "Pins Updated",
                type = "rich",
                colour = 0x0388fc,
                )

        emb.add_field(name="Channel",value="#"+channel.name, inline=False)

        if audit == True:
            emb.set_author(name=user, icon_url=user.avatar_url)
            emb.add_field(name="Additional Information",value="User ID: {0.id}\nChannel ID: {1.id}".format(user,channel,inline=False))

        else:
            emb.add_field(name="Missing Permissions",value="Missing audit log permissions! Some data will be missing.",inline=False)
            emb.add_field(name="Additional Information",value="Channel ID: {}".format(channel.id),inline=False)

        await Log.sendlog(self, str(channel.guild.id), emb)

    '''

    '''
    todo, requires formatting messages into a suitable format
    @commands.Cog.listener()
    async def on_bulk_message_delete(self, messages):
        #first we need to upload the messages to a hosting site, since it's probably going to be more than 2k characters - also downloading is overrated
        print(messages)
        messages2 = ""
        for message in messages:
            messages2.append(message+"\n")
        print(messages2)
        response = requests.post("http://hastebin.com", data=list(messages2))
        if response.status_code == 200:
            print(response.json()['key'])

        emb = discord.Embed(
                title = "Message Purge",
                type = "rich",
                colour = 0xf0280a,
                )

        emb.add_field(name="Content",value="temp", inline=False)
        emb.add_field(name="Channel",value=messages[0].channel.mention, inline=False)
        emb.add_field(name="Additional Information",value="Channel ID: {0.channel.id}".format(message))

        await Log.sendlog(self, str(message.guild.id), emb)
    '''
def setup(crdbot):

    crdbot.add_cog(Log(crdbot))