import torch
import pandas as pd
from ActiveLearning.Bert_NER.dataset import EntityDataset
import config
from transformers import AdamW
from transformers import get_linear_schedule_with_warmup
from sklearn.model_selection import KFold
import engine
import numpy as np
import dataset_processors
from ActiveLearning.Bert_NER.bert_crf import Bert_CRF, Bert
import json
from sklearn import model_selection
import random

def activeLearning(num_incorrect_predictions, avg_entropy, avg_confidence, avg_margin, doi, metric, correction_ratio):
    N = int(len(doi) * correction_ratio) # Number of paragraphs
    doi = np.array(doi)
    if metric in ['Traditional', 'all']:
        dois = doi[(-np.array(num_incorrect_predictions)).argsort()[:N]].tolist()
        with open('./ActiveLearning/Bert_predictions/traditional.json', 'w+') as f:
            json.dump(dois, f)
    if metric in ['Uncertainty', 'all']:
        dois = doi[(np.array(avg_confidence)).argsort()[:N]].tolist()
        with open('./ActiveLearning/Bert_predictions/uncertainty.json', 'w+') as f:
            json.dump(dois, f)
    if metric in ['Entropy', 'all']:
        dois = doi[(-np.array(avg_entropy)).argsort()[:N]].tolist()# Verify
        with open('./ActiveLearning/Bert_predictions/entropy.json', 'w+') as f:
            json.dump(dois, f)
    if metric in ['Margin', 'all']:
        dois = doi[(np.array(avg_margin)).argsort()[:N]].tolist()
        with open('./ActiveLearning/Bert_predictions/margin.json', 'w+') as f:
            json.dump(dois, f)

def build_model(device, dataset):
    model = Bert(len(config.NER_LABELS))
    model.to(device)

    param_optimizer = list(model.named_parameters())
    no_decay = ["bias", "LayerNorm.bias", "LayerNorm.weight"]
    optimizer_parameters = [
        {
            "params": [
                p for n, p in param_optimizer if not any(nd in n for nd in no_decay)
            ],
            "weight_decay": 0.001,
        },
        {
            "params": [
                p for n, p in param_optimizer if any(nd in n for nd in no_decay)
            ],
            "weight_decay": 0.0,
        },
    ]
    # Set optimizer
    optimizer = AdamW(optimizer_parameters, lr=3e-5)

    # Set scheduler
    num_train_steps = int(len(dataset) / config.TRAIN_BATCH_SIZE * config.EPOCHS)
    scheduler = get_linear_schedule_with_warmup(
        optimizer, num_warmup_steps=0, num_training_steps=num_train_steps
    )
    return model, optimizer, scheduler

if __name__ == '__main__':
    dataset_path = './ActiveLearning/Data/'
    bert_output_path = './ActiveLearning/Bert_predictions'

    train_text_list, _, train_label_list, _, doi_list = dataset_processors.process_PIPELINE(dataset_path=dataset_path, is_PSC=False)
    train_text_list = np.array(train_text_list)
    train_label_list = np.array(train_label_list)
    doi_list = np.array(doi_list)

    doi2doi_id = {doi:i for i, doi in enumerate(doi_list)}
    doi_id2doi = {v:k for k, v in doi2doi_id.items()}

    doi_id_list = np.array([doi2doi_id[key] for key in doi_list])

    device =torch.device('cuda:0')

    best_loss = np.inf
    # Implement Cross Validation
    kf = KFold(n_splits=5)

    num_incorrect_predictions_all = []
    avg_entropy_all = []
    avg_confidence_all = []
    avg_margin_all = []
    doi_all = []

    indices = np.array(range(len(train_text_list)))
    train_paragraphs, test_paragraphs, train_labels, test_labels, indices_train, indices_test = model_selection.train_test_split(train_text_list, train_label_list, indices, random_state=3, test_size=0.1)

    # for train_idx, validation_idx in kf.split(train_paragraphs):

    #     train_split = train_paragraphs[train_idx]
    #     test_split = train_paragraphs[validation_idx]
    #     train_label = train_labels[train_idx]
    #     test_label = train_labels[validation_idx]
    #     doi_train = doi_id_list[train_idx]
    #     doi_test = doi_id_list[validation_idx]

    #     train_dataset = EntityDataset(train_split, train_label, doi_train, paragraph_split_length=512)
    #     train_data_loader = torch.utils.data.DataLoader(train_dataset, batch_size=config.TRAIN_BATCH_SIZE)

    #     test_dataset = EntityDataset(test_split, test_label, doi_test, paragraph_split_length=512)
    #     test_data_loader = torch.utils.data.DataLoader(test_dataset, batch_size=config.VALID_BATCH_SIZE)

    #     # Create model
    #     model, optimizer, scheduler = build_model(device, train_dataset)

    #     # Training model
    #     for epoch in range(config.EPOCHS):
    #         train_loss = engine.train_fn(train_data_loader, model, optimizer, device, scheduler)
    #         print(f"Train Loss = {train_loss}")

    #     # Make prediction on test fold
    #     num_incorrect_predictions, avg_entropy, avg_confidence, avg_margin, doi = engine.test_fn(test_data_loader, model, device)

    #     num_incorrect_predictions_all.extend(num_incorrect_predictions)
    #     avg_entropy_all.extend(avg_entropy)
    #     avg_confidence_all.extend(avg_confidence)
    #     avg_margin_all.extend(avg_margin)
    #     doi_all.extend(doi)

    # activeLearning(num_incorrect_predictions_all, avg_entropy_all, avg_confidence_all, avg_margin_all, doi_all, metric='all', correction_ratio=0.1)

    # Perform inference on original data
    train_whole_dataset = EntityDataset(train_paragraphs, train_labels, doi_list=None, paragraph_split_length=512)
    train_whole_data_loader = torch.utils.data.DataLoader(train_whole_dataset, batch_size=config.TRAIN_BATCH_SIZE)

    _, _, clean_label_list, _, _ = dataset_processors.process_PIPELINE(dataset_path=dataset_path, is_PSC=False, replace_dois=indices_test.tolist())
    clean_label_list = np.array(clean_label_list)
    test_labels = clean_label_list[indices_test]

    test_whole_dataset = EntityDataset(test_paragraphs, test_labels, doi_list=None, paragraph_split_length=512)
    test_whole_data_loader = torch.utils.data.DataLoader(test_whole_dataset, batch_size=config.VALID_BATCH_SIZE)

    model, optimizer, scheduler = build_model(device, train_whole_dataset)

    for epoch in range(config.EPOCHS):
        train_loss = engine.train_fn(train_whole_data_loader, model, optimizer, device, scheduler)
        print(f"Train Loss = {train_loss}")

    metrics = engine.actual_test(test_whole_data_loader, model, device)
    print("Baseline metrics:")
    print(metrics)

    # Perform inference on active learned data

    # Uncertainty
    with open('./ActiveLearning/Bert_predictions/uncertainty.json', 'r') as uncertainty_file:
        uncertainty_id_list = json.load(uncertainty_file)

    replace_doi_list = [doi_id2doi[uncertainty_id] for uncertainty_id in uncertainty_id_list]

    uncertainty_text_list, _, uncertainty_label_list, _, doi_list = dataset_processors.process_PIPELINE(dataset_path=dataset_path, is_PSC=False, replace_dois=replace_doi_list)

    train_uncertainty_dataset = EntityDataset(uncertainty_text_list, uncertainty_label_list, doi_list=None, paragraph_split_length=512)
    train_uncertainty_data_loader = torch.utils.data.DataLoader(train_uncertainty_dataset, batch_size=config.TRAIN_BATCH_SIZE)

    model, optimizer, scheduler = build_model(device, train_uncertainty_dataset)

    for epoch in range(config.EPOCHS):
        train_loss = engine.train_fn(train_uncertainty_data_loader, model, optimizer, device, scheduler)
        print(f"Train Loss = {train_loss}")

    metrics = engine.actual_test(test_whole_data_loader, model, device)
    print("Uncertainty metrics:")
    print(metrics)

    # Entropy
    with open('./ActiveLearning/Bert_predictions/entropy.json', 'r') as entropy_file:
        entropy_id_list = json.load(entropy_file)

    replace_doi_list = [doi_id2doi[entropy_id] for entropy_id in entropy_id_list]

    entropy_text_list, _, entropy_label_list, _, doi_list = dataset_processors.process_PIPELINE(dataset_path=dataset_path, is_PSC=False, replace_dois=replace_doi_list)

    train_entropy_dataset = EntityDataset(entropy_text_list, entropy_label_list, doi_list=None, paragraph_split_length=512)
    train_entropy_data_loader = torch.utils.data.DataLoader(train_entropy_dataset, batch_size=config.TRAIN_BATCH_SIZE)

    model, optimizer, scheduler = build_model(device, train_entropy_dataset)

    for epoch in range(config.EPOCHS):
        train_loss = engine.train_fn(train_entropy_data_loader, model, optimizer, device, scheduler)
        print(f"Train Loss = {train_loss}")

    metrics = engine.actual_test(test_whole_data_loader, model, device)
    print("Entropy metrics:")
    print(metrics)

    # Traditional
    with open('./ActiveLearning/Bert_predictions/traditional.json', 'r') as traditional_file:
        traditional_id_list = json.load(traditional_file)

    replace_doi_list = [doi_id2doi[traditional_id] for traditional_id in traditional_id_list]

    traditional_text_list, _, traditional_label_list, _, doi_list = dataset_processors.process_PIPELINE(dataset_path=dataset_path, is_PSC=False, replace_dois=replace_doi_list)

    train_traditional_dataset = EntityDataset(traditional_text_list, traditional_label_list, doi_list=None, paragraph_split_length=512)
    train_traditional_data_loader = torch.utils.data.DataLoader(train_traditional_dataset, batch_size=config.TRAIN_BATCH_SIZE)

    model, optimizer, scheduler = build_model(device, train_traditional_dataset)

    for epoch in range(config.EPOCHS):
        train_loss = engine.train_fn(train_traditional_data_loader, model, optimizer, device, scheduler)
        print(f"Train Loss = {train_loss}")

    metrics = engine.actual_test(test_whole_data_loader, model, device)
    print("Traditional metrics:")
    print(metrics)

    # Margin
    with open('./ActiveLearning/Bert_predictions/margin.json', 'r') as margin_file:
        margin_id_list = json.load(margin_file)

    replace_doi_list = [doi_id2doi[margin_id] for margin_id in margin_id_list]

    margin_text_list, _, margin_label_list, _, doi_list = dataset_processors.process_PIPELINE(dataset_path=dataset_path, is_PSC=False, replace_dois=replace_doi_list)

    train_margin_dataset = EntityDataset(margin_text_list, margin_label_list, doi_list=None, paragraph_split_length=512)
    train_margin_data_loader = torch.utils.data.DataLoader(train_margin_dataset, batch_size=config.TRAIN_BATCH_SIZE)

    model, optimizer, scheduler = build_model(device, train_margin_dataset)

    for epoch in range(config.EPOCHS):
        train_loss = engine.train_fn(train_margin_data_loader, model, optimizer, device, scheduler)
        print(f"Train Loss = {train_loss}")

    metrics = engine.actual_test(test_whole_data_loader, model, device)
    print("Margin metrics:")
    print(metrics)

    # Random
    replace_doi_list = random.sample(range(0,90), 8)

    random_text_list, _, random_label_list, _, doi_list = dataset_processors.process_PIPELINE(dataset_path=dataset_path, is_PSC=False, replace_dois=replace_doi_list)

    train_random_dataset = EntityDataset(random_text_list, random_label_list, doi_list=None, paragraph_split_length=512)
    train_random_data_loader = torch.utils.data.DataLoader(train_random_dataset, batch_size=config.TRAIN_BATCH_SIZE)

    model, optimizer, scheduler = build_model(device, train_random_dataset)

    for epoch in range(config.EPOCHS):
        train_loss = engine.train_fn(train_random_data_loader, model, optimizer, device, scheduler)
        print(f"Train Loss = {train_loss}")

    metrics = engine.actual_test(test_whole_data_loader, model, device)
    print("Random metrics:")
    print(metrics)


import Herramientas
from bs4 import BeautifulSoup
from xml.etree.ElementTree import Element, SubElement, Comment

def obtenerPortada(directorio, codigo):
    try:

        #codigo = Herramientas.obtenerCodigoTiempo()
        Herramientas.crearCarpeta(directorio, codigo, "AMELIARUEDA.COM")

        info = Herramientas.get_simple("https://www.ameliarueda.com")

        links = []

        homePage = BeautifulSoup(info, 'html.parser')

        #Noticia_principal
        portada_1  = homePage.find('div', attrs= {'class': 'main-hero__content'}).find('a')
        links.append("https://www.ameliarueda.com" + portada_1.attrs['href'])

        #Slider de titulares
        portada_2 = homePage.find('ul', attrs={'class': 'de-primero__listing'}).find_all('a')

        for noticia in portada_2 :
            links.append(noticia.attrs['href'])

        #Slider vertical
        portada_3 = homePage.find('div', attrs={'class': 'owl-carousel'}).find_all('a')

        for noticia in portada_3 :
            if noticia.attrs['href'].startswith('/') :
                links.append("https://www.ameliarueda.com" + noticia.attrs['href'])

        # Resto de noticias
        portada_4 = homePage.find('div', attrs={'class': 'container'}).find_all('div', attrs={'class': 'main-news-article'})

        for div_con_a in portada_4:
            noticia = div_con_a.find('a')
            links.append("https://www.ameliarueda.com" + noticia.attrs['href'])

        indice = 1

        for link in links:

            try:
                info1 = Herramientas.get_simple(link)
                sou = BeautifulSoup(info1, 'html.parser')

                archivo_xml = Element('nota')
                archivo_txt = ""

                contenidos = sou.find('div', attrs= {'class': 'note__hero'})

                # Titulo
                titulo = contenidos.find('h1').getText()
                archivo_txt = archivo_txt + titulo + "\n"
                tit = SubElement(archivo_xml, 'titulo')
                tit.text = titulo
                nombre = "top" + str(indice) + "__" + titulo.replace(":", ",").replace("?", "¿").replace("\"", "")

                # Bajada
                bajada = contenidos.find('div', attrs= {'class': 'note__article__summary'})
                baj = SubElement(archivo_xml, 'bajada')
                baj.text = bajada.getText().replace("\n", "")
                archivo_txt = archivo_txt + bajada.getText() + "\n"

                # Parrafos
                texto = SubElement(archivo_xml, 'texto')

                aux = contenidos.find('div', attrs={'class': 'note__article__main-content'})
                textos = aux.find_all(['p','h4'])

                es_lead = True

                for p in textos:
                    if p.getText().replace('\n', '') != '':
                        if p.name == 'p':
                            if (es_lead):
                                parr = SubElement(texto, 'lead')
                                parr.text = p.getText()
                                es_lead = False
                            else:
                                parr = SubElement(texto, 'parrafo')
                                parr.text = p.getText()
                        else :
                            subt = SubElement(texto, 'subtitulo')
                            subt.text = p.getText()

                        archivo_txt = archivo_txt + p.getText() + "\n"

                # Tags NO APLICA

                # Categoría NO APLICA

                # Codificacion
                archivo_txt = archivo_txt.encode('iso-8859-1', 'ignore').decode('iso-8859-1', 'ignore').replace('"', '')
                archivo_xml = Herramientas.prettify(archivo_xml).encode('iso-8859-1', 'ignore').decode('iso-8859-1', 'ignore').replace('"', '')

                # Creacion de archivos
                Herramientas.hacerArchivo( archivo_txt, nombre, '.txt')
                Herramientas.hacerArchivo( archivo_xml, nombre, '.xml')

            except Exception as e:
                err = str(e) + "-AR-" + str(link) + "---"
                print(err)
                Herramientas.guardarError(err)

            indice = indice + 1

    except Exception as e:
        err = str(e) + "-AR-"
        print(err)
        Herramientas.guardarError(err)


from loguru import logger
import uuid
from sqlalchemy import Column, String, BigInteger
import os.path
import time
from typing import Dict, List

from magic_assistant.agent.role_play.role_play_agent import RolePlayAgent
from magic_assistant.agent.role_play.agent_react import AgentReact
from magic_assistant.agent.agent_manager import AgentManager
from magic_assistant.utils.globals import Globals
from magic_assistant.io.base_io import BaseIo
from magic_assistant.db.orm import BASE

class SandboxMeta(BASE):
    __tablename__ = "sandbox"
    id = Column(String, primary_key=True, nullable=False)
    name = Column(String, nullable=False)
    user_id = Column(String)
    sandbox_timestamp = Column(BigInteger, nullable=False)

    def __init__(self):
        self.id = uuid.uuid1().hex
        self.user_id = "default"
        self.sandbox_timestamp = int(time.time() * 1000)

class Sandbox():
    def __init__(self, sandbox_meta: SandboxMeta, role_play_config_path: str, globals: Globals, io: BaseIo):
        self.meta = sandbox_meta
        self.role_play_config_path: str = role_play_config_path
        self.globals: Globals = globals
        self.io: BaseIo = io
        self.time_interval_per_loop: int = 60000
        self.agent_manager = AgentManager(globals=globals)
        self.agents: Dict[str, RolePlayAgent] = {}
        self.agent_names: List[str] = []
        self.__post_init__()

    def __post_init__(self):
        agent_config_path = os.path.join(self.role_play_config_path, "agent.yml")
        self.agents = self.agent_manager.get_or_create_batch(agent_config_path, self.io, self.get_sandbox_timestamp, self.meta.id)
        for _, agent in self.agents.items():
            self.agent_names.append(agent.meta.name)

    def start_loop(self):
        while True:
            for _, agent in self.agents.items():
                agent_react_list: List[AgentReact] = agent.process(agents_in_context=self.agent_names)
                if agent_react_list is None or len(agent_react_list) == 0:
                    logger.error("agent %s process react is None" % agent.meta.name)
                    continue

                for agent_react in agent_react_list:
                    if (agent_react.react_type == "communicate" or agent_react.react_type == "respond") and agent_react.target_entity in self.agents:
                        target_agent: RolePlayAgent = self.agents[agent_react.target_entity]
                        target_agent.memory_operator.add_memory_item(content=agent_react.react_content, src_entity=agent.meta.name, relation="said to", target_entity=target_agent.meta.name)
                    else:
                        logger.error("react processed failed, react:%s, agents:%s" % (agent_react.__dict__, self.agents))

            self.meta.sandbox_timestamp += self.time_interval_per_loop
            time.sleep(3)

    def get_sandbox_timestamp(self) -> int:
        return self.meta.sandbox_timestamp


from __future__ import print_function
import logging

import sys
from time import sleep
import signal
import threading

import grpc

import deepthought_pb2
import deepthought_pb2_grpc

def run():
    # !! do NOT use insecure_channel for production !!
    with grpc.insecure_channel('localhost:50051') as channel:
        stub = deepthought_pb2_grpc.ComputeStub(channel)
        # stub.<rpc>(*_pb2.<message>(param))
        #for response in stub.Boot(deepthought_pb2.BootRequest()):
        #    print(">>> BootRequest Sent. response: %s" % (response.message))
        response_generator = stub.Boot(deepthought_pb2.BootRequest())

        def cancel_request():
            response_generator.cancel()
            sys.exit(0)
        # send cancel and exit(0) when receiving SIGINT (e.g. Ctrl+C)
        def cancel_request_sigint(unused_signum, unused_frame):
            cancel_request()
        signal.signal(signal.SIGINT, cancel_request_sigint)
        # send cancel after 1.5 seconds
        threading.Timer(1.5, cancel_request).start()

        # receive message from server
        try:
            for response in response_generator:
                print(">>> BootRequest Sent. response: %s" % (response.message))
        except grpc.RpcError as e:
            print(">>> Cancelled: Boot Server-side Streaming")
            #print(e)

        # class grpc.UnaryUnaryMultiCallable
        response = stub.Infer(deepthought_pb2.InferRequest(query='Infer Query'))
        response = stub.Infer(deepthought_pb2.InferRequest(query='Infer Query'), timeout=300)
        print(">>> type(response): " + str(type(response)))
        print(">>> InferRequest Sent. response: %s, %s" % (response.answer, response.description))

if __name__ == '__main__':
    logging.basicConfig()
    run()


def edit_purchases(request,pk):
    instance = get_object_or_404(transaction_models.Purchase.objects.filter(pk=pk,is_deleted=False))
    PurchaseDetailsFormset = formset_factory(transaction_froms.PurchaseDetailsForm)
    if transaction_models.PurchaseDetails.objects.filter(PurchaseId=instance).exists():
        extra = 0
    else:
        extra= 1
    PurchaseDetailsFormset = inlineformset_factory(
        transaction_models.Purchase,
        transaction_models.PurchaseDetails,
        can_delete=True,
        extra=extra,
        form=transaction_froms.PurchaseDetailsForm,
    )

    if request.method == 'POST':
        form = transaction_froms.PurchaseForm(request.POST,request.FILES,instance=instance)
        purchase_details_formset = PurchaseDetailsFormset(request.POST,prefix="purchase_details_formset",instance=instance)

         # taiking current financial year
        if master_models.FinancialYear.objects.filter(is_deleted=False,IsClosed=False).exists():
            financial_year = get_object_or_404(master_models.FinancialYear.objects.filter(is_deleted=False,IsClosed=False))
            if form.is_valid() and purchase_details_formset.is_valid():
                data = form.save(commit=False)
                data.updater = request.user
                data.date_updated = datetime.datetime.now()
                data.FinancialYearId = financial_year
                data.Action = 'M'
                data.save()

                if master_models.LedgerPosting.objects.filter(MasterID=instance.auto_id).exists():
                    ledger_post = master_models.LedgerPosting.objects.filter(MasterID=instance.auto_id)
                    for i in ledger_post:
                        i.delete()

                # taiking currency instance
                sar = None
                inr = None
                if master_models.Currency.objects.filter().exists():
                    sar = get_object_or_404(master_models.Currency.objects.filter(Name="SAR"))
                    inr = get_object_or_404(master_models.Currency.objects.filter(Name="INR"))

                purchase_items = purchase_details_formset.save(commit=False)
                print(purchase_items)
                print('purchase_items')
                for item in purchase_items:
                    item.PurchaseId = data
                    item.Action = "M"
                    LedgerID = item.LedgerID
                    INRAmount = item.INRAmount
                    CurrencyRate = item.CurrencyRate
                    Amount = item.Amount
                    item.save()

                    print(item.pk)
                    print("++++++++++")

                for item in purchase_details_formset:
                    LedgerID = item.cleaned_data['LedgerID']
                    INRAmount = item.cleaned_data['INRAmount']
                    CurrencyRate = item.cleaned_data['CurrencyRate']
                    Amount = item.cleaned_data['Amount']
                    Notes = item.cleaned_data['Notes']

                    # purchase details log
                    transaction_models.PurchaseDetailsLog.objects.create(
                        LedgerID = LedgerID,
                        INRAmount = INRAmount,
                        CurrencyRate = CurrencyRate,
                        Notes = Notes,
                        Amount = Amount,
                        Action = "M",
                        PurchaseId = data,
                    )

                for obj in purchase_details_formset.deleted_objects:
                    print('======++++')
                    LedgerID = item.cleaned_data['LedgerID']
                    INRAmount = item.cleaned_data['INRAmount']
                    CurrencyRate = item.cleaned_data['CurrencyRate']
                    Amount = item.cleaned_data['Amount']
                    obj.delete()

                purch_formsets = transaction_models.PurchaseDetails.objects.filter(PurchaseId=data)
                for i in purch_formsets:
                    print("'''''''''''''purch'''''''''''''")
                    LedgerID = i.LedgerID
                    INRAmount = i.INRAmount
                    CurrencyRate = i.CurrencyRate
                    Amount = i.Amount
                    # ledger post sundry creaditer
                    master_models.LedgerPosting.objects.create(
                        auto_id = get_auto_id(master_models.LedgerPosting),
                        creator = request.user,
                        updater = request.user,
                        MasterID = data.auto_id,
                        LedgerID = LedgerID.pk,
                        Action = 'M',
                        VoucharType = "Purchase",
                        DetailID = i.pk,
                        IsActive = True,
                        Date = data.Date,
                        Debit = 0,
                        Credit = Amount,
                        CurrencyId = sar,
                    )
                    # ledger post log sundry creaditer
                    master_models.LedgerPostingLog.objects.create(
                        auto_id = get_auto_id(master_models.LedgerPostingLog),
                        creator = request.user,
                        updater = request.user,
                        MasterID = data.auto_id,
                        LedgerID = LedgerID.pk,
                        Action = 'M',
                        VoucharType = "Purchase",
                        DetailID = i.pk,
                        IsActive = True,
                        Date = data.Date,
                        Debit = 0,
                        Credit = Amount,
                        CurrencyId = sar,
                    )
                    # <<<<<<<<<<<<<<<<<<<<<<
                    # ledger post Agent
                    master_models.LedgerPosting.objects.create(
                        auto_id = get_auto_id(master_models.LedgerPosting),
                        creator = request.user,
                        updater = request.user,
                        MasterID = data.auto_id,
                        LedgerID = data.AgentID.pk,
                        Action = 'M',
                        VoucharType = "Purchase",
                        IsActive = True,
                        DetailID = i.pk,
                        Date = data.Date,
                        Debit = 0,
                        Credit = Amount,
                        CurrencyId = inr,
                    )
                    # ledger post log Agent
                    master_models.LedgerPostingLog.objects.create(
                        auto_id = get_auto_id(master_models.LedgerPostingLog),
                        creator = request.user,
                        updater = request.user,
                        MasterID = data.auto_id,
                        LedgerID = data.AgentID.pk,
                        Action = 'M',
                        VoucharType = "Purchase",
                        IsActive = True,
                        DetailID = i.pk,
                        Date = data.Date,
                        Debit = 0,
                        Credit = Amount,
                        CurrencyId = inr,
                    )

                # purchase log
                transaction_models.PurchaseLog.objects.create(
                    auto_id = get_auto_id(transaction_models.PurchaseLog),
                    creator = data.creator,
                    updater = data.updater,
                    Action = "M",
                    PurchaseId = data.pk,
                    Notes = data.Notes,
                    VoucherNo = data.VoucherNo,
                    Date = data.Date,
                    AgentID = data.AgentID,
                    TotalINRAmount = data.TotalINRAmount,
                    CurrencyRate = data.CurrencyRate,
                    Amount = data.Amount,
                    FinancialYearId = data.FinancialYearId,
                )

                if master_models.AccountLedger.objects.filter().exists():
                    ledger_purchase_id = get_object_or_404(master_models.AccountLedger.objects.filter(LedgerName="Purchase"))

                # ledger post INR
                master_models.LedgerPosting.objects.create(
                    auto_id = get_auto_id(master_models.LedgerPosting),
                    creator = request.user,
                    updater = request.user,
                    MasterID = data.auto_id,
                    LedgerID = ledger_purchase_id.id,
                    Action = 'M',
                    VoucharType = "Purchase",
                    IsActive = True,
                    Date = data.Date,
                    Debit = data.TotalINRAmount,
                    Credit = 0,
                    CurrencyId = inr,
                )
                # ledger post log INR
                master_models.LedgerPostingLog.objects.create(
                    auto_id = get_auto_id(master_models.LedgerPostingLog),
                    creator = request.user,
                    updater = request.user,
                    MasterID = data.auto_id,
                    LedgerID = ledger_purchase_id.id,
                    Action = 'M',
                    VoucharType = "Purchase",
                    IsActive = True,
                    Date = data.Date,
                    Debit = data.TotalINRAmount,
                    Credit = 0,
                    CurrencyId = inr,
                )
                 # ledger post SAR
                master_models.LedgerPosting.objects.create(
                    auto_id = get_auto_id(master_models.LedgerPosting),
                    creator = request.user,
                    updater = request.user,
                    MasterID = data.auto_id,
                    LedgerID = ledger_purchase_id.id,
                    Action = 'M',
                    VoucharType = "Purchase",
                    IsActive = True,
                    Date = data.Date,
                    Debit = data.TotalINRAmount,
                    Credit = 0,
                    CurrencyId = sar,
                )
                # ledger post log SAR
                master_models.LedgerPostingLog.objects.create(
                    auto_id = get_auto_id(master_models.LedgerPostingLog),
                    creator = request.user,
                    updater = request.user,
                    MasterID = data.auto_id,
                    LedgerID = ledger_purchase_id.id,
                    Action = 'M',
                    VoucharType = "Purchase",
                    IsActive = True,
                    Date = data.Date,
                    Debit = data.TotalINRAmount,
                    Credit = 0,
                    CurrencyId = sar,
                )

                response_data = {
                    "status" : "true",
                    "title" : "Successfully Updated",
                    "message" : "Purchase Successfully Updated.",
                    "redirect" : "true",
                    "redirect_url" : reverse('transactions:purchases')
                }

            else:
                message = generate_form_errors_new(form,formset=False)
                message = generate_form_errors_new(purchase_details_formset,formset=True)
                print(form.errors)
                print(purchase_details_formset.errors)

                response_data = {
                    "status" : "false",
                    "stable" : "true",
                    "title" : "Form validation error",
                    "message" : str(message)
                }

            return HttpResponse(json.dumps(response_data), content_type='application/javascript')
        else:
            message = "Please Create a Financial Year"
            response_data = {
                "status" : "false",
                "stable" : "true",
                "title" : str(_("Financial Year is not exists")),
                "message" : str(message)
            }

            return HttpResponse(json.dumps(response_data), content_type='application/javascript')
    else:
        sundry_creaditer = master_models.AccountLedger.objects.filter(MasterGroup__GroupName="Sundry Creditors")
        form = transaction_froms.PurchaseForm(instance=instance)
        form.fields['AgentID'].queryset = master_models.AccountLedger.objects.filter(MasterGroup__GroupName="Agent")
        purchase_details_formset = PurchaseDetailsFormset(prefix='purchase_details_formset',instance=instance)
        for forms in purchase_details_formset:
            forms.fields['LedgerID'].queryset = sundry_creaditer
        context = {
            "form" : form,
            "title" : str(_("Edit Purchase :")) + str(instance.AgentID),
            "purchase_details_formset" : purchase_details_formset,
            "url" : reverse('transactions:edit_purchases',kwargs={'pk':instance.pk}),
            "redirect" : True,

        }
        return render(request,"transactions/create_purchase.html",context)


import sqlite3
import random, string
from flask import Flask, request, redirect, url_for, render_template, flash
from werkzeug.exceptions import abort

rnd = random.SystemRandom()

app = Flask(__name__)
app.config['SECRET_KEY'] = ''.join(rnd.choice(string.ascii_letters) for i in range(999))

def get_db_connection():
    conn = sqlite3.connect('database.db')
    conn.row_factory = sqlite3.Row
    return conn

def get_post(post_id):
    conn = get_db_connection()
    post = conn.execute('SELECT * FROM posts WHERE id = ?', (post_id,)).fetchone()
    conn.close()
    if post is None:
        abort(404)
    return post

@app.route('/')
def index():
  conn = get_db_connection()
  post_u = conn.execute('SELECT * FROM posts WHERE id=(SELECT MAX(id) FROM posts)').fetchall()
  posts = conn.execute('SELECT * FROM posts').fetchall()
  conn.close()
  return render_template('index.html', post_u=post_u, posts=posts)

@app.route('/admin', methods=('GET','POST'))
def admin():
  if request.method == 'POST':
    title = request.form['title']
    content = request.form['content']

    if not title:
        flash('Title is required!')
    else:
        conn = get_db_connection()
        conn.execute('INSERT INTO posts(title, content) VALUES (?,?)', (title, content))
        conn.commit()
        conn.close()
        return redirect(url_for('admin'))

  conn = get_db_connection()
  posts = conn.execute('SELECT * FROM posts ORDER BY id DESC').fetchall()
  conn.close()

  return render_template('admin.html', posts=posts)

@app.route('/<int:post_id>')
def post(post_id):
    post = get_post(post_id)
    return render_template('post.html', post=post)

@app.route('/<int:id>/edit', methods=('PUT'))
def edit(id):
  post = get_post(id)

  title = request.form['title']
  content = request.form['content']

  conn = get_db_connection()
  conn.execute('UPDATE posts SET title = ?, content =?'
                   ' WHERE id = ?', (title, content, id))
  conn.commit()
  conn.close()
  #return redirect(url_for('admin'))
  return render_template('edit.html', post=post)

@app.route('/<int:id>/delete')
def delete(id):
    post = get_post(id)
    conn = get_db_connection()
    conn.execute('DELETE FROM posts WHERE id = ?', (id,))
    conn.commit()
    conn.close()
    flash('"{}" was successfully deleted!'.format(post['title']))
    return redirect(url_for('admin'))

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


# Brute Force
class Solution:
    def isNumber(self, s: str) -> bool:
        SET = set(s)
        if len(set("abcdfghijklmnopqrstuvwxyz") & SET) > 0: return False
        if len(set("ABCDFGHIJKLMNOPQRSTUVWXYZ") & SET) > 0: return False
        counter = Counter(s)
        if counter["e"] > 1 or counter["E"] > 1: return False

        def check(s):
            if not s: return False
            counter = Counter(s)
            if counter["+"] > 1 or counter["-"] > 1: return False

            operator = None
            if s[0] == "+" or s[0] == "-":
                operator = s[0]
                s = s[1:]
            if operator is not None and len(s) == 0: return False

            if "." in set(s):
                i = s.index(".")
                a, b = s[:i], s[i+1:]
                if not a and not b: return False
                if a and not a.isdigit(): return False
                if b and not b.isdigit(): return False

                return True
            else:
                return s.isdigit()

        if "e" in SET or "E" in SET:
            e = s.index("e") if "e" in SET else -1
            E = s.index("E") if "E" in SET else -1
            idx = max(e, E)
            a, b = s[:idx], s[idx+1:]

            # "6e6.5"
            if "." in b: return False
            return check(a) and check(b)
        else:
            return check(s)


import streamlit as st
import os

# Function to read markdown content
def read_markdown_file(markdown_file):
    with open(markdown_file, 'r') as file:
        content = file.read()
    return content

# List all markdown files in the current directory
markdown_files = [f for f in os.listdir() if f.endswith('.md')]

# Create the Streamlit app
st.title("Markdown Viewer")

# Dropdown menu for file selection
selected_file = st.selectbox("Choose a markdown file", markdown_files)

# Display the selected markdown file
content = read_markdown_file(selected_file)
st.markdown(content, unsafe_allow_html=True)


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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', index, name='index'),
    path('dashboard/', include('blog.urls')),
    path('about', about, name='about'),
    path('service', service, name='service'),
    path('contact', contact, name='contact'),
    path('blog', blog, name='blog'),
    path('login/', login, name='login'),
    path('logout/', logout_view, name='logout'),
]


def main():

    cantidad_numeros = int(input("Ingresa la cantidad de números para la serie de Fibonacci: "))

    if cantidad_numeros < 1:
        print("Error: La cantidad de números debe ser al menos 1.")
        return

    fibonacci = [0, 1]

    while len(fibonacci) < cantidad_numeros:
        next_fibo = fibonacci[-1] + fibonacci[-2]
        fibonacci.append(next_fibo)

    print(f"Los {cantidad_numeros} primeros números de la serie de Fibonacci:")
    for num in fibonacci:
        print(num)

print(main())


#--------------------------------------------------------------#
#---------------------- Shooting Module -----------------------#
#--------------------------------------------------------------#

def firstorder(ic, target, tspan, eom, \
               solver  = 'rk4', \
               tstep   =     0, \
               damping =   0.1, \
               iLimit  =    25, \
               tol     =  1E-3, \
               TU      =     1, \
               guess   =  None, \
               print_status = False, \
               trim_output  = True):

    #  import statements
    from numpy.linalg import norm, solve
    from numpy import zeros, eye, concatenate
    from primervectormodule import extractSTM

    #  if eom argument is not an eom model with variational equations,
    #+ then turn on the appropriate model
    if eom == 'P2BP': eom = 'P2BP_varEqns'
    if eom == 'S2BP': eom = 'S2BP_varEqns'

    #  determine dimension of problem (i.e. planar or spatial)
    #+ and initialize the state vector and residual vector
    if len(target) == 2:
        from flow import P2B as flow
        # initialize a state vector
        if len(ic) == 21:
            s = ic
        if len(ic) == 5:
            s = zeros(21)
            s[0:5] = ic
            #  tack identity matrix on end for STM
            s[5:] = eye(4).reshape(16,1)
        #  residual vector
        res = s[0:2]
        #  make a copy of the velocity for computing/printing delta-v
        v0 = s[2:4].copy()
    elif len(target) == 3:
        from flow import S2B as flow
        #  initialize a state vector
        if len(ic) == 43:
            s = ic
        if len(ic) == 7:
            s = zeros(43)
            s[0:7] = ic
            #  tack identity matrix on end for STM
            s[7:] = eye(6).reshape(36,1)
        #  residual vector
        res = s[0:3]
        #  make a copy of the velocity for computing/printing delta-v
        v0 = s[3:6].copy()

    #  calculate an appropriate tstep if not defined
    if tstep == 0: tstep = abs(tspan[1] - tspan[0])/5E1

    #  if an initial guess for perturbation is given then use it,
    #+ otherwise perturb the shooting method in the direction of current
    #+ velocity, so as to generally seek a lower detla-v solution
    if len(target) == 2:
        if guess != None: s[2:4] += guess
        else: s[2:4] += 0.1*s[2:4]
    elif len(target) == 3:
        if guess != None: s[3:6] += guess
        else: s[3:6] += 0.1*s[3:6]

    #  set counter
    i   = 0
    #  iterate with a first-order shooting method
    while ((norm(res) > tol) and (i < iLimit + 1)) or (i == 0):

        #  solve for 'flow'
        solution = flow(s, tspan, tstep, solver, eom, TU, \
                        trim_output)
        #  extract the state transition matrix
        #  and relevant blocks for delta-v update
        stm, mblk, nblk = extractSTM(solution, -1)
        #  solve for delta-v update
        dv = solve(nblk, res)
        #+ update residual
        if   len(dv) == 2: res = (solution['y'][-1][0:2] - target)
        elif len(dv) == 3: res = (solution['y'][-1][0:3] - target)
        #  print iterative updates if desired
        if print_status and len(dv) == 2:
            print ('Iter: %4i   Res: %8e   ' \
                   'dv1x: %8.5f   dv1y: %8.5f ' \
                   %(i, norm(res), (s[2] - v0[0]), (s[3] - v0[1])))
        if print_status and len(dv) == 3:
            print ('Iter: %4i   Res: %8e   ' \
                  'dv1x: %8.5f   dv1y: %8.5f   dv1z: %8.5f ' \
                   %(i, norm(res), (s[3] - v0[0]), \
                    (s[4] - v0[1]), (s[5] - v0[2])))
        #  update initial velocity vector
        if   len(dv) == 2: s[2:4] = s[2:4] - damping*dv
        elif len(dv) == 3: s[3:6] = s[3:6] - damping*dv

        #  update counter
        i += 1

    return solution

#--------------------------------------------------------------#
#---------------------- Shooting Module -----------------------#
#--------------------------------------------------------------#


import gzip
import json

class Dataset:
    def __init__(self, fsentry):
        with gzip.open(fsentry.path, 'rt') as f:
            docs = json.load(f)
            self.roles = docs['metadata']['roles']

            d = dict()
            for doc in docs['articles']:
                d[doc['Index']] = doc

            self.key = fsentry.name.replace('.gz', '')
            self.content = docs
            self.articles = d
            self.geolocations = docs['geolocations']
            self.name = docs['metadata']['name']
            self.length = len(d)

    def allowed(self, user_roles):
        return type(user_roles) is list \
                and any(map(lambda role: role in user_roles, self.roles))


def calculate(s: str) -> int:
    stack = []
    # 记录数字的符号, 因为题目说没有负数,说明第一个为正数,设为1
    sign = 1
    # 数字
    num = 0
    # 结果
    res = 0
    for c in s:
        if c.isdigit():
            num = num * 10 + int(c)
        elif c == "+":
            res += sign * num
            # 为下一次做准备
            num = 0
            sign = 1
        elif c == "-":
            res += sign * num
            # 为下一次做准备
            num = 0
            sign = -1
        elif c == "(":
            stack.append(res)
            stack.append(sign)
            sign = 1
            res = 0
        elif c == ")":
            res += sign * num
            num = 0
            res = stack.pop() * res + stack.pop()
    res += sign * num
    return res

def main():
    s = '1 + 1'
    print(calculate(s))

if __name__ == '__main__':
    main()


"""
Redis storage for Policies.
"""

import logging
import pickle
import itertools

from ..storage.abc import Storage
from ..policy import Policy
from ..exceptions import PolicyExistsError

log = logging.getLogger(__name__)

DEFAULT_COLLECTION = 'vakt_policies'

class JSONSerializer:
    """
    Serializes/de-serializes policies to JSON for Redis storage
    """
    @staticmethod
    def serialize(policy):
        return policy.to_json()

    @staticmethod
    def deserialize(data):
        return Policy.from_json(data.decode('utf-8'))

class PickleSerializer:
    """
    Serializes/de-serializes policies Python pickle representation for Redis storage
    """
    def __init__(self, *args, **kwargs):
        """
        Use args and kwargs to pass them to customized pickle module calls.
        """
        self.args = args
        self.kwargs = kwargs

    def serialize(self, policy):
        return pickle.dumps(policy, *self.args, **self.kwargs)

    def deserialize(self, data):
        return pickle.loads(data, **self.kwargs)

class RedisStorage(Storage):
    """
    Stores Policies in Redis.

    Stores all policies in a single hash whose name is a `collection` argument.
    Each filed in this hash is a Policy's UID and the value of this key is a serialized Policy representation.
    """

    class Scripts:
        """
        Helper class to register and store Redis Lua scripts.
        """
        def __init__(self, client):
            self.updater = client.register_script("""
                local exists = redis.call('HEXISTS', KEYS[1], ARGV[1])
                if exists == 1 then
                    return redis.call('HSET', KEYS[1], ARGV[1], ARGV[2])
                end
                return 0
                """)

    def __init__(self, client, collection=DEFAULT_COLLECTION, serializer=None):
        self.client = client
        self.collection = collection
        self.sr = serializer
        self.scripts = self.Scripts(client)
        if serializer is None:
            self.sr = PickleSerializer()

    def add(self, policy):
        uid = policy.uid
        try:
            done = self.client.hsetnx(self.collection, uid, self.sr.serialize(policy))
            if done == 0:
                log.error('Error trying to create already existing policy with UID=%s.', uid)
                raise PolicyExistsError(uid)
        except Exception:
            log.exception('Error trying to create already existing policy with UID=%s.', uid)
            raise PolicyExistsError(uid)
        log.info('Added Policy: %s', policy)

    def get(self, uid):
        ret = self.client.hget(self.collection, uid)
        if not ret:
            return None
        return self.sr.deserialize(ret)

    def get_all(self, limit, offset):
        # According to docs https://redis.io/commands/scan#the-count-option
        # Redis doesn't guarantee the exact number of elements returned,
        # so we opt to fetching all data and manual slicing on the client side.
        self._check_limit_and_offset(limit, offset)
        data = self.client.hgetall(self.collection)
        sliced = itertools.islice(data.items(), offset, limit+offset)
        return self.__feed_policies(dict(sliced))

    def find_for_inquiry(self, inquiry, checker=None):
        data = self.client.hgetall(self.collection)
        if not data:
            return []
        return self.__feed_policies(data)

    def update(self, policy):
        uid = policy.uid
        try:
            res = self.scripts.updater(keys=[self.collection], args=[uid, self.sr.serialize(policy)])
            if res == 1:
                log.info('Updated Policy with UID=%s. New value is: %s', uid, policy)
        except Exception as e:
            log.exception('Error trying to update policy with UID=%s.', uid)
            raise e

    def delete(self, uid):
        res = self.client.hdel(self.collection, uid)
        if res == 0:
            log.info('Nothing to delete by UID=%s', uid)
        else:
            log.info('Deleted Policy with UID=%s', uid)

    def __feed_policies(self, data):
        """
        Yields Policies from the given cursor.
        """
        for uid in data:
            yield self.sr.deserialize(data[uid])


import torch.nn as nn
from models.utils import Conv2DBatchNormReLU, ResidualBlock, PyramidPooling
import torch.nn.functional as F

class PSPNet(nn.Module):
    def __init__(self, num_classes, resnet_type='50', in_channels=3):
        super(PSPNet, self).__init__()

        assert resnet_type in ['50', '101']
        if resnet_type == 50:
            num_units_list = [3, 4, 6, 3]
        else:
            num_units_list = [3, 4, 23, 3]

        # Encoder
        self.convbnrelu1 = Conv2DBatchNormReLU(in_channels, 64, kernel_size=3, padding=1,
                                               stride=2, bias=False)
        self.convbnrelu2 = Conv2DBatchNormReLU(64, 64, kernel_size=3, padding=1, stride=1,
                                               bias=False)
        self.convbnrelu3 = Conv2DBatchNormReLU(64, 128, kernel_size=3, padding=1, stride=1,
                                               bias=False)
        self.maxpool1 = nn.MaxPool2d(kernel_size=3, padding=1, stride=2)

        # Vanilla residual blocks
        self.res_block1 = ResidualBlock(num_units_list[0], 128, 64, 256, 1, dilation=1)
        self.res_block2 = ResidualBlock(num_units_list[1], 256, 128, 512, 2, dilation=1)

        # Dilated residual blocks
        self.res_block3 = ResidualBlock(num_units_list[2], 512, 256, 1024, 1, dilation=2)
        self.res_block4 = ResidualBlock(num_units_list[3], 1024, 512, 2048, 1, dilation=4)

        # Pyramid pooling module
        self.pyramid_pooling = PyramidPooling(2048, [6, 3, 2, 1])

        # Final conv layers
        self.convbnrelu4 = Conv2DBatchNormReLU(4096, 512, 3, 1, 1, bias=False)
        self.dropout = nn.Dropout2d(p=0.1, inplace=False)
        self.class_scores = nn.Conv2d(512, num_classes, 1, 1, 0)

        # Aux layers for training
        self.convbnrelu4_aux = Conv2DBatchNormReLU(1024, 256, 3, 1, 1, bias=False)
        self.class_scores_aux = nn.Conv2d(256, num_classes, 1, 1, 0)

    def forward(self, x):
        input_shape = x.shape[2:]

        x = self.convbnrelu1(x)
        x = self.convbnrelu2(x)
        x = self.convbnrelu3(x)

        x = self.maxpool1(x)

        x = self.res_block1(x)
        x = self.res_block2(x)
        x = self.res_block3(x)

        if self.training:
            x_aux = self.convbnrelu4_aux(x)
            x_aux = self.dropout(x_aux)
            aux_output = self.class_scores_aux(x_aux)

        x = self.res_block4(x)

        x = self.pyramid_pooling(x)

        x = self.convbnrelu4(x)
        x = self.dropout(x)
        x = self.class_scores(x)
        output = F.interpolate(x, size=input_shape, mode='bilinear', align_corners=True)

        if self.training:
            return [output, aux_output]
        else:
            return output


import struct

from scapy.packet import *
from scapy.fields import *
from scapy.layers.inet import IP
from scapy.layers.inet6 import IP6Field

IPPROTO_SCTP=132

# crc32-c (Castagnoli) (crc32c_poly=0x1EDC6F41)
crc32c_table = [
    0x00000000, 0xF26B8303, 0xE13B70F7, 0x1350F3F4,
    0xC79A971F, 0x35F1141C, 0x26A1E7E8, 0xD4CA64EB,
    0x8AD958CF, 0x78B2DBCC, 0x6BE22838, 0x9989AB3B,
    0x4D43CFD0, 0xBF284CD3, 0xAC78BF27, 0x5E133C24,
    0x105EC76F, 0xE235446C, 0xF165B798, 0x030E349B,
    0xD7C45070, 0x25AFD373, 0x36FF2087, 0xC494A384,
    0x9A879FA0, 0x68EC1CA3, 0x7BBCEF57, 0x89D76C54,
    0x5D1D08BF, 0xAF768BBC, 0xBC267848, 0x4E4DFB4B,
    0x20BD8EDE, 0xD2D60DDD, 0xC186FE29, 0x33ED7D2A,
    0xE72719C1, 0x154C9AC2, 0x061C6936, 0xF477EA35,
    0xAA64D611, 0x580F5512, 0x4B5FA6E6, 0xB93425E5,
    0x6DFE410E, 0x9F95C20D, 0x8CC531F9, 0x7EAEB2FA,
    0x30E349B1, 0xC288CAB2, 0xD1D83946, 0x23B3BA45,
    0xF779DEAE, 0x05125DAD, 0x1642AE59, 0xE4292D5A,
    0xBA3A117E, 0x4851927D, 0x5B016189, 0xA96AE28A,
    0x7DA08661, 0x8FCB0562, 0x9C9BF696, 0x6EF07595,
    0x417B1DBC, 0xB3109EBF, 0xA0406D4B, 0x522BEE48,
    0x86E18AA3, 0x748A09A0, 0x67DAFA54, 0x95B17957,
    0xCBA24573, 0x39C9C670, 0x2A993584, 0xD8F2B687,
    0x0C38D26C, 0xFE53516F, 0xED03A29B, 0x1F682198,
    0x5125DAD3, 0xA34E59D0, 0xB01EAA24, 0x42752927,
    0x96BF4DCC, 0x64D4CECF, 0x77843D3B, 0x85EFBE38,
    0xDBFC821C, 0x2997011F, 0x3AC7F2EB, 0xC8AC71E8,
    0x1C661503, 0xEE0D9600, 0xFD5D65F4, 0x0F36E6F7,
    0x61C69362, 0x93AD1061, 0x80FDE395, 0x72966096,
    0xA65C047D, 0x5437877E, 0x4767748A, 0xB50CF789,
    0xEB1FCBAD, 0x197448AE, 0x0A24BB5A, 0xF84F3859,
    0x2C855CB2, 0xDEEEDFB1, 0xCDBE2C45, 0x3FD5AF46,
    0x7198540D, 0x83F3D70E, 0x90A324FA, 0x62C8A7F9,
    0xB602C312, 0x44694011, 0x5739B3E5, 0xA55230E6,
    0xFB410CC2, 0x092A8FC1, 0x1A7A7C35, 0xE811FF36,
    0x3CDB9BDD, 0xCEB018DE, 0xDDE0EB2A, 0x2F8B6829,
    0x82F63B78, 0x709DB87B, 0x63CD4B8F, 0x91A6C88C,
    0x456CAC67, 0xB7072F64, 0xA457DC90, 0x563C5F93,
    0x082F63B7, 0xFA44E0B4, 0xE9141340, 0x1B7F9043,
    0xCFB5F4A8, 0x3DDE77AB, 0x2E8E845F, 0xDCE5075C,
    0x92A8FC17, 0x60C37F14, 0x73938CE0, 0x81F80FE3,
    0x55326B08, 0xA759E80B, 0xB4091BFF, 0x466298FC,
    0x1871A4D8, 0xEA1A27DB, 0xF94AD42F, 0x0B21572C,
    0xDFEB33C7, 0x2D80B0C4, 0x3ED04330, 0xCCBBC033,
    0xA24BB5A6, 0x502036A5, 0x4370C551, 0xB11B4652,
    0x65D122B9, 0x97BAA1BA, 0x84EA524E, 0x7681D14D,
    0x2892ED69, 0xDAF96E6A, 0xC9A99D9E, 0x3BC21E9D,
    0xEF087A76, 0x1D63F975, 0x0E330A81, 0xFC588982,
    0xB21572C9, 0x407EF1CA, 0x532E023E, 0xA145813D,
    0x758FE5D6, 0x87E466D5, 0x94B49521, 0x66DF1622,
    0x38CC2A06, 0xCAA7A905, 0xD9F75AF1, 0x2B9CD9F2,
    0xFF56BD19, 0x0D3D3E1A, 0x1E6DCDEE, 0xEC064EED,
    0xC38D26C4, 0x31E6A5C7, 0x22B65633, 0xD0DDD530,
    0x0417B1DB, 0xF67C32D8, 0xE52CC12C, 0x1747422F,
    0x49547E0B, 0xBB3FFD08, 0xA86F0EFC, 0x5A048DFF,
    0x8ECEE914, 0x7CA56A17, 0x6FF599E3, 0x9D9E1AE0,
    0xD3D3E1AB, 0x21B862A8, 0x32E8915C, 0xC083125F,
    0x144976B4, 0xE622F5B7, 0xF5720643, 0x07198540,
    0x590AB964, 0xAB613A67, 0xB831C993, 0x4A5A4A90,
    0x9E902E7B, 0x6CFBAD78, 0x7FAB5E8C, 0x8DC0DD8F,
    0xE330A81A, 0x115B2B19, 0x020BD8ED, 0xF0605BEE,
    0x24AA3F05, 0xD6C1BC06, 0xC5914FF2, 0x37FACCF1,
    0x69E9F0D5, 0x9B8273D6, 0x88D28022, 0x7AB90321,
    0xAE7367CA, 0x5C18E4C9, 0x4F48173D, 0xBD23943E,
    0xF36E6F75, 0x0105EC76, 0x12551F82, 0xE03E9C81,
    0x34F4F86A, 0xC69F7B69, 0xD5CF889D, 0x27A40B9E,
    0x79B737BA, 0x8BDCB4B9, 0x988C474D, 0x6AE7C44E,
    0xBE2DA0A5, 0x4C4623A6, 0x5F16D052, 0xAD7D5351,
    ]

def crc32c(buf):
    crc = 0xffffffff
    for c in buf:
        crc = (crc>>8) ^ crc32c_table[(crc^(ord(c))) & 0xFF]
    crc = (~crc) & 0xffffffff
    # reverse endianness
    return struct.unpack(">I",struct.pack("<I", crc))[0]

# old checksum (RFC2960)
"""
BASE = 65521 # largest prime smaller than 65536
def update_adler32(adler, buf):
    s1 = adler & 0xffff
    s2 = (adler >> 16) & 0xffff
    print s1,s2

    for c in buf:
        print ord(c)
        s1 = (s1 + ord(c)) % BASE
        s2 = (s2 + s1) % BASE
        print s1,s2
    return (s2 << 16) + s1

def sctp_checksum(buf):
    return update_adler32(1, buf)
"""

sctpchunktypescls = {
    0 : "SCTPChunkData",
    1 : "SCTPChunkInit",
    2 : "SCTPChunkInitAck",
    3 : "SCTPChunkSACK",
    4 : "SCTPChunkHeartbeatReq",
    5 : "SCTPChunkHeartbeatAck",
    6 : "SCTPChunkAbort",
    7 : "SCTPChunkShutdown",
    8 : "SCTPChunkShutdownAck",
    9 : "SCTPChunkError",
    10 : "SCTPChunkCookieEcho",
    11 : "SCTPChunkCookieAck",
    14 : "SCTPChunkShutdownComplete",
    }

sctpchunktypes = {
    0 : "data",
    1 : "init",
    2 : "init-ack",
    3 : "sack",
    4 : "heartbeat-req",
    5 : "heartbeat-ack",
    6 : "abort",
    7 : "shutdown",
    8 : "shutdown-ack",
    9 : "error",
    10 : "cookie-echo",
    11 : "cookie-ack",
    14 : "shutdown-complete",
    }

sctpchunkparamtypescls = {
    1 : "SCTPChunkParamHearbeatInfo",
    5 : "SCTPChunkParamIPv4Addr",
    6 : "SCTPChunkParamIPv6Addr",
    7 : "SCTPChunkParamStateCookie",
    8 : "SCTPChunkParamUnrocognizedParam",
    9 : "SCTPChunkParamCookiePreservative",
    11 : "SCTPChunkParamHostname",
    12 : "SCTPChunkParamSupportedAddrTypes",
    32768 : "SCTPChunkParamECNCapable",
    49152 : "SCTPChunkParamFwdTSN",
    49158 : "SCTPChunkParamAdaptationLayer",
    }

sctpchunkparamtypes = {
    1 : "heartbeat-info",
    5 : "IPv4",
    6 : "IPv6",
    7 : "state-cookie",
    8 : "unrecognized-param",
    9 : "cookie-preservative",
    11 : "hostname",
    12 : "addrtypes",
    32768 : "ecn-capable",
    49152 : "fwd-tsn-supported",
    49158 : "adaptation-layer",
    }

############## SCTP header

# Dummy class to guess payload type (variable parameters)
class _SCTPChunkGuessPayload:
    def default_payload_class(self,p):
        if len(p) < 4:
            return conf.padding_layer
        else:
            t = ord(p[0])
            return globals().get(sctpchunktypescls.get(t, "Raw"), conf.raw_layer)

class SCTP(_SCTPChunkGuessPayload, Packet):
    fields_desc = [ ShortField("sport", None),
                    ShortField("dport", None),
                    XIntField("tag", None),
                    XIntField("chksum", None), ]
    def answers(self, other):
        if not isinstance(other, SCTP):
            return 0
        if conf.checkIPsrc:
            if not ((self.sport == other.dport) and
                    (self.dport == other.sport)):
                return 0
        return 1
    def post_build(self, p, pay):
        p += pay
        if self.chksum is None:
            crc = crc32c(str(p))
            p = p[:8]+struct.pack(">I", crc)+p[12:]
        return p

############## SCTP Chunk variable params

class ChunkParamField(PacketListField):
    islist = 1
    holds_packets=1
    def __init__(self, name, default, count_from=None, length_from=None):
        PacketListField.__init__(self, name, default, conf.raw_layer, count_from=count_from, length_from=length_from)
    def m2i(self, p, m):
        cls = conf.raw_layer
        if len(m) >= 4:
            t = ord(m[0]) * 256 + ord(m[1])
            cls = globals().get(sctpchunkparamtypescls.get(t, "Raw"), conf.raw_layer)
        return cls(m)

# dummy class to avoid Raw() after Chunk params
class _SCTPChunkParam:
    def extract_padding(self, s):
        return "",s[:]

class SCTPChunkParamHearbeatInfo(_SCTPChunkParam, Packet):
    fields_desc = [ ShortEnumField("type", 1, sctpchunkparamtypes),
                    FieldLenField("len", None, length_of="data",
                                  adjust = lambda pkt,x:x+4),
                    PadField(StrLenField("data", "",
                                         length_from=lambda pkt: pkt.len-4),
                             4, padwith="\x00"),]

class SCTPChunkParamIPv4Addr(_SCTPChunkParam, Packet):
    fields_desc = [ ShortEnumField("type", 5, sctpchunkparamtypes),
                    ShortField("len", 8),
                    IPField("addr","127.0.0.1"), ]

class SCTPChunkParamIPv6Addr(_SCTPChunkParam, Packet):
    fields_desc = [ ShortEnumField("type", 6, sctpchunkparamtypes),
                    ShortField("len", 20),
                    IP6Field("addr","::1"), ]

class SCTPChunkParamStateCookie(_SCTPChunkParam, Packet):
    fields_desc = [ ShortEnumField("type", 7, sctpchunkparamtypes),
                    FieldLenField("len", None, length_of="cookie",
                                  adjust = lambda pkt,x:x+4),
                    PadField(StrLenField("cookie", "",
                                         length_from=lambda pkt: pkt.len-4),
                             4, padwith="\x00"),]

class SCTPChunkParamUnrocognizedParam(_SCTPChunkParam, Packet):
    fields_desc = [ ShortEnumField("type", 8, sctpchunkparamtypes),
                    FieldLenField("len", None, length_of="param",
                                  adjust = lambda pkt,x:x+4),
                    PadField(StrLenField("param", "",
                                         length_from=lambda pkt: pkt.len-4),
                             4, padwith="\x00"),]

class SCTPChunkParamCookiePreservative(_SCTPChunkParam, Packet):
    fields_desc = [ ShortEnumField("type", 9, sctpchunkparamtypes),
                    ShortField("len", 8),
                    XIntField("sug_cookie_inc", None), ]

class SCTPChunkParamHostname(_SCTPChunkParam, Packet):
    fields_desc = [ ShortEnumField("type", 11, sctpchunkparamtypes),
                    FieldLenField("len", None, length_of="hostname",
                                  adjust = lambda pkt,x:x+4),
                    PadField(StrLenField("hostname", "",
                                         length_from=lambda pkt: pkt.len-4),
                             4, padwith="\x00"), ]

class SCTPChunkParamSupportedAddrTypes(_SCTPChunkParam, Packet):
    fields_desc = [ ShortEnumField("type", 12, sctpchunkparamtypes),
                    FieldLenField("len", None, length_of="addr_type_list",
                                  adjust = lambda pkt,x:x+4),
                    PadField(FieldListField("addr_type_list", [ "IPv4" ],
                                            ShortEnumField("addr_type", 5, sctpchunkparamtypes),
                                            length_from=lambda pkt: pkt.len-4),
                             4, padwith="\x00"), ]

class SCTPChunkParamECNCapable(_SCTPChunkParam, Packet):
    fields_desc = [ ShortEnumField("type", 32768, sctpchunkparamtypes),
                    ShortField("len", 4), ]

class SCTPChunkParamFwdTSN(_SCTPChunkParam, Packet):
    fields_desc = [ ShortEnumField("type", 49152, sctpchunkparamtypes),
                    ShortField("len", 4), ]

class SCTPChunkParamAdaptationLayer(_SCTPChunkParam, Packet):
    fields_desc = [ ShortEnumField("type", 49158, sctpchunkparamtypes),
                    ShortField("len", 8),
                    XIntField("indication", None), ]

############## SCTP Chunks

class SCTPChunkData(_SCTPChunkGuessPayload, Packet):
    fields_desc = [ ByteEnumField("type", 0, sctpchunktypes),
                    BitField("reserved", None, 4),
                    BitField("delay_sack", 0, 1),
                    BitField("unordered", 0, 1),
                    BitField("beginning", 0, 1),
                    BitField("ending", 0, 1),
                    FieldLenField("len", None, length_of="data", adjust = lambda pkt,x:x+16),
                    XIntField("tsn", None),
                    XShortField("stream_id", None),
                    XShortField("stream_seq", None),
                    XIntField("proto_id", None),
                    PadField(StrLenField("data", None, length_from=lambda pkt: pkt.len-16),
                             4, padwith="\x00"),
                    ]

class SCTPChunkInit(_SCTPChunkGuessPayload, Packet):
    fields_desc = [ ByteEnumField("type", 1, sctpchunktypes),
                    XByteField("flags", None),
                    FieldLenField("len", None, length_of="params", adjust = lambda pkt,x:x+20),
                    XIntField("init_tag", None),
                    IntField("a_rwnd", None),
                    ShortField("n_out_streams", None),
                    ShortField("n_in_streams", None),
                    XIntField("init_tsn", None),
                    ChunkParamField("params", None, length_from=lambda pkt:pkt.len-20),
                    ]

class SCTPChunkInitAck(_SCTPChunkGuessPayload, Packet):
    fields_desc = [ ByteEnumField("type", 2, sctpchunktypes),
                    XByteField("flags", None),
                    FieldLenField("len", None, length_of="params", adjust = lambda pkt,x:x+20),
                    XIntField("init_tag", None),
                    IntField("a_rwnd", None),
                    ShortField("n_out_streams", None),
                    ShortField("n_in_streams", None),
                    XIntField("init_tsn", None),
                    ChunkParamField("params", None, length_from=lambda pkt:pkt.len-20),
                    ]

class GapAckField(Field):
    def __init__(self, name, default):
        Field.__init__(self, name, default, "4s")
    def i2m(self, pkt, x):
        if x is None:
            return "\0\0\0\0"
        sta, end = map(int, x.split(":"))
        args = tuple([">HH", sta, end])
        return struct.pack(*args)
    def m2i(self, pkt, x):
        return "%d:%d"%(struct.unpack(">HH", x))
    def any2i(self, pkt, x):
        if type(x) is tuple and len(x) == 2:
            return "%d:%d"%(x)
        return x

class SCTPChunkSACK(_SCTPChunkGuessPayload, Packet):
    fields_desc = [ ByteEnumField("type", 3, sctpchunktypes),
                    XByteField("flags", None),
                    ShortField("len", None),
                    XIntField("cumul_tsn_ack", None),
                    IntField("a_rwnd", None),
                    FieldLenField("n_gap_ack", None, count_of="gap_ack_list"),
                    FieldLenField("n_dup_tsn", None, count_of="dup_tsn_list"),
                    FieldListField("gap_ack_list", [ ], GapAckField("gap_ack", None), count_from=lambda pkt:pkt.n_gap_ack),
                    FieldListField("dup_tsn_list", [ ], XIntField("dup_tsn", None), count_from=lambda pkt:pkt.n_dup_tsn),
                    ]

    def post_build(self, p, pay):
        if self.len is None:
            p = p[:2] + struct.pack(">H", len(p)) + p[4:]
        return p+pay

class SCTPChunkHeartbeatReq(_SCTPChunkGuessPayload, Packet):
    fields_desc = [ ByteEnumField("type", 4, sctpchunktypes),
                    XByteField("flags", None),
                    FieldLenField("len", None, length_of="params", adjust = lambda pkt,x:x+4),
                    ChunkParamField("params", None, length_from=lambda pkt:pkt.len-4),
                   ]

class SCTPChunkHeartbeatAck(_SCTPChunkGuessPayload, Packet):
    fields_desc = [ ByteEnumField("type", 5, sctpchunktypes),
                    XByteField("flags", None),
                    FieldLenField("len", None, length_of="params", adjust = lambda pkt,x:x+4),
                    ChunkParamField("params", None, length_from=lambda pkt:pkt.len-4),
                   ]

class SCTPChunkAbort(_SCTPChunkGuessPayload, Packet):
    fields_desc = [ ByteEnumField("type", 6, sctpchunktypes),
                    BitField("reserved", None, 7),
                    BitField("TCB", 0, 1),
                    FieldLenField("len", None, length_of="error_causes", adjust = lambda pkt,x:x+4),
                    PadField(StrLenField("error_causes", "", length_from=lambda pkt: pkt.len-4),
                             4, padwith="\x00"),
                   ]

class SCTPChunkShutdown(_SCTPChunkGuessPayload, Packet):
    fields_desc = [ ByteEnumField("type", 7, sctpchunktypes),
                    XByteField("flags", None),
                    ShortField("len", 8),
                    XIntField("cumul_tsn_ack", None),
                   ]

class SCTPChunkShutdownAck(_SCTPChunkGuessPayload, Packet):
    fields_desc = [ ByteEnumField("type", 8, sctpchunktypes),
                    XByteField("flags", None),
                    ShortField("len", 4),
                   ]

class SCTPChunkError(_SCTPChunkGuessPayload, Packet):
    fields_desc = [ ByteEnumField("type", 9, sctpchunktypes),
                    XByteField("flags", None),
                    FieldLenField("len", None, length_of="error_causes", adjust = lambda pkt,x:x+4),
                    PadField(StrLenField("error_causes", "", length_from=lambda pkt: pkt.len-4),
                             4, padwith="\x00"),
                   ]

class SCTPChunkCookieEcho(_SCTPChunkGuessPayload, Packet):
    fields_desc = [ ByteEnumField("type", 10, sctpchunktypes),
                    XByteField("flags", None),
                    FieldLenField("len", None, length_of="cookie", adjust = lambda pkt,x:x+4),
                    PadField(StrLenField("cookie", "", length_from=lambda pkt: pkt.len-4),
                             4, padwith="\x00"),
                   ]

class SCTPChunkCookieAck(_SCTPChunkGuessPayload, Packet):
    fields_desc = [ ByteEnumField("type", 11, sctpchunktypes),
                    XByteField("flags", None),
                    ShortField("len", 4),
                   ]

class SCTPChunkShutdownComplete(_SCTPChunkGuessPayload, Packet):
    fields_desc = [ ByteEnumField("type", 12, sctpchunktypes),
                    BitField("reserved", None, 7),
                    BitField("TCB", 0, 1),
                    ShortField("len", 4),
                    ]

bind_layers( IP,           SCTP,          proto=IPPROTO_SCTP)


import pygame
import sys
from math import sqrt

# define some constants
WIDTH, HEIGHT = 800, 600
BUBBLE_RADIUS = 50
FPS = 60
EXPANSION_SPEED = 1  # speed of bubble expansion

# define a bubble class
class Bubble:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.radius = BUBBLE_RADIUS
        self.expanding = False

    def draw(self, surface):
        pygame.draw.circle(surface, (255, 255, 255), (self.x, self.y), self.radius, 1)

    def expand(self):
        if self.expanding:
            self.radius += EXPANSION_SPEED

def get_intersection_points(b1, b2):
    d = sqrt((b2.x - b1.x) ** 2 + (b2.y - b1.y) ** 2)

    if d > b1.radius + b2.radius:  # circles are separate
        return []
    if d < abs(b1.radius - b2.radius):  # one circle is contained within the other
        return []
    if d == 0 and b1.radius == b2.radius:  # circles coincide
        return []

    a = (b1.radius ** 2 - b2.radius ** 2 + d ** 2) / (2 * d)
    h = sqrt(b1.radius ** 2 - a ** 2)
    x2 = b1.x + a * (b2.x - b1.x) / d
    y2 = b1.y + a * (b2.y - b1.y) / d
    rx = -(b2.y - b1.y) * (h / d)
    ry = -(b2.x - b1.x) * (h / d)

    return [(x2 + rx, y2 - ry), (x2 - rx, y2 + ry)]

# initialize pygame
pygame.init()

# set up the display
screen = pygame.display.set_mode((WIDTH, HEIGHT))

# set up the clock
clock = pygame.time.Clock()

# set up the bubbles
bubbles = []

# main loop
while True:
    # event handling
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            x, y = pygame.mouse.get_pos()
            # check if click on any bubble
            for bubble in bubbles:
                if (bubble.x - x)**2 + (bubble.y - y)**2 <= bubble.radius**2:
                    bubble.expanding = True
                    break
            else:
                bubbles.append(Bubble(x, y))
        elif event.type == pygame.MOUSEBUTTONUP:
            # stop expansion on mouse release
            for bubble in bubbles:
                bubble.expanding = False

        # drawing
    screen.fill((0, 0, 0))
    for bubble in bubbles:
        bubble.expand()
        bubble.draw(screen)

    # check for intersections
    for i in range(len(bubbles)):
        for j in range(i+1, len(bubbles)):
            points = get_intersection_points(bubbles[i], bubbles[j])
            if len(points) == 2:
                pygame.draw.line(screen, (255, 255, 255), points[0], points[1], 1)

    # flip the display
    pygame.display.flip()

    # cap the frame rate
    clock.tick(FPS)


from abc import ABCMeta
import sys
import json
from datetime import date, datetime, timedelta as td
import matplotlib.pyplot as plt

#sys.path.append('../basic_source_code/')
#
#from MongoDbClient import MongoDbClient

class BasicPlotter(metaclass=ABCMeta):
    def __init__(self, start_date, end_date):
        plt.rcParams.update({"xtick.labelsize": 25.0,\
                             "ytick.labelsize": 25.0,\
                             "legend.fontsize": 25.0})
        self.start_date = start_date
        self.end_date = end_date

        self.ground_truth, self.likely_domains = self.get_data_from_file()

#        self.colors = ["#f7f7f7", "#cccccc", "#969696", "#636363", "#252525"]
        self.colors = [(1.0, 0.4980392156862745, 0.054901960784313725),\
                       (0.17254901960784313, 0.6274509803921569, 0.17254901960784313),\
                       (0.8392156862745098, 0.15294117647058825, 0.1568627450980392),\
                       (0.5803921568627451, 0.403921568627451, 0.7411764705882353),\
                       (0.12156862745098039, 0.4666666666666667, 0.7058823529411765),\
                       (0.5490196078431373, 0.33725490196078434, 0.29411764705882354),\
                       (0.8901960784313725, 0.4666666666666667, 0.7607843137254902),\
                       (0.7372549019607844, 0.7411764705882353, 0.13333333333333333),\
                       (0.4980392156862745, 0.4980392156862745, 0.4980392156862745),\
                       (0.09019607843137255, 0.7450980392156863, 0.8117647058823529)]
        self.patterns = ("////", "\\\\\\\\", "----", "++++", "....", "****", "oooo", "0000", "====", "::::")

    def get_data_from_file(self):
        ground_truth = []
        with open("../backups/ground_truth.json", "r") as f:
            for line in f:
                ground_truth.append(json.loads(line))
        likely_domains = []
        with open("../backups/likely_domains.json", "r") as f:
            for line in f:
                likely_domains.append(json.loads(line))

        return ground_truth, likely_domains

#    def get_data_from_db(self):
#        mongo_db_client = MongoDbClient()
#        ground_truth = self.mongo_db_client.get_all("ground_truth")
#        likely_domains = self.mongo_db_client.get_all("likely_domain")
#
#        return ground_truth, likely_domains

    def initialize_date_dict(self):
        date_dict = {}
        for i in range((self.end_date - self.start_date).days + 1):
            date_dict[str(self.start_date + td(days=i))] = 0

        return date_dict

    def prepare_dates_values(self, data_dict):
        dates = [datetime.strptime(key, "%Y-%m-%d").date() for key in data_dict.keys()]
        values = [int(value) for value in data_dict.values()]

        return dates, values

    def add_important_events(self, ax, start_y=85.0, stepsize=5.0):
        fontsize_value = 25.0
        ax.text(x=datetime.strptime("2016-08-05", "%Y-%m-%d").date(), y=start_y, s="2", fontsize=fontsize_value)
        ax.axvline(x=datetime.strptime("2016-08-04", "%Y-%m-%d").date(), color="k")
#        ax.text(x=datetime.strptime("2016-08-17", "%Y-%m-%d").date(), y=start_y-stepsize, s="Check Point Decryptor", fontsize=fontsize_value)
#        ax.axvline(x=datetime.strptime("2016-08-16", "%Y-%m-%d").date(), color="k")
        ax.text(x=datetime.strptime("2016-08-31", "%Y-%m-%d").date(), y=start_y, s="3", fontsize=fontsize_value)
        ax.axvline(x=datetime.strptime("2016-08-30", "%Y-%m-%d").date(), color="k")
        ax.text(x=datetime.strptime("2016-10-05", "%Y-%m-%d").date(), y=start_y, s="4(X)", fontsize=fontsize_value)
        ax.axvline(x=datetime.strptime("2016-10-04", "%Y-%m-%d").date(), color="k")
        ax.text(x=datetime.strptime("2016-10-20", "%Y-%m-%d").date(), y=start_y, s="4.0.2", fontsize=fontsize_value)
        ax.axvline(x=datetime.strptime("2016-10-19", "%Y-%m-%d").date(), color="k")
        ax.text(x=datetime.strptime("2016-10-28", "%Y-%m-%d").date(), y=start_y-stepsize, s="4.1.0", fontsize=fontsize_value)
        ax.axvline(x=datetime.strptime("2016-10-27", "%Y-%m-%d").date(), color="k")
        ax.text(x=datetime.strptime("2016-11-01", "%Y-%m-%d").date(), y=start_y-2*stepsize, s="4.1.1", fontsize=fontsize_value)
        ax.axvline(x=datetime.strptime("2016-10-31", "%Y-%m-%d").date(), color="k")
        ax.text(x=datetime.strptime("2016-11-08", "%Y-%m-%d").date(), y=start_y-3*stepsize, s="4.1.3", fontsize=fontsize_value)
        ax.axvline(x=datetime.strptime("2016-11-07", "%Y-%m-%d").date(), color="k")
        ax.text(x=datetime.strptime("2016-11-09", "%Y-%m-%d").date(), y=start_y-4*stepsize, s="4.1.4", fontsize=fontsize_value)
        ax.axvline(x=datetime.strptime("2016-11-08", "%Y-%m-%d").date(), color="k")
        ax.text(x=datetime.strptime("2016-11-10", "%Y-%m-%d").date(), y=start_y, s="4.1.5", fontsize=fontsize_value)
        ax.axvline(x=datetime.strptime("2016-11-09", "%Y-%m-%d").date(), color="k")
        ax.text(x=datetime.strptime("2016-11-23", "%Y-%m-%d").date(), y=start_y-stepsize, s="4.1.6", fontsize=fontsize_value)
        ax.axvline(x=datetime.strptime("2016-11-22", "%Y-%m-%d").date(), color="k")
        ax.text(x=datetime.strptime("2016-11-24", "%Y-%m-%d").date(), y=start_y-2*stepsize, s="5.0.0", fontsize=fontsize_value)
        ax.axvline(x=datetime.strptime("2016-11-23", "%Y-%m-%d").date(), color="k")
        ax.text(x=datetime.strptime("2016-11-26", "%Y-%m-%d").date(), y=start_y-3*stepsize, s="5.0.1", fontsize=fontsize_value)
        ax.axvline(x=datetime.strptime("2016-11-25", "%Y-%m-%d").date(), color="k")
        ax.text(x=datetime.strptime("2016-12-02", "%Y-%m-%d").date(), y=start_y, s="Red", fontsize=fontsize_value)
        ax.axvline(x=datetime.strptime("2016-12-01", "%Y-%m-%d").date(), color="k")
        ax.text(x=datetime.strptime("2017-01-04", "%Y-%m-%d").date(), y=start_y, s="Red 1.1", fontsize=fontsize_value)
        ax.axvline(x=datetime.strptime("2017-01-03", "%Y-%m-%d").date(), color="k")
        ax.text(x=datetime.strptime("2017-01-26", "%Y-%m-%d").date(), y=start_y, s="Red 1.2", fontsize=fontsize_value)
        ax.axvline(x=datetime.strptime("2017-01-25", "%Y-%m-%d").date(), color="k")
        ax.text(x=datetime.strptime("2017-03-31", "%Y-%m-%d").date(), y=start_y, s="6", fontsize=fontsize_value)
        ax.axvline(x=datetime.strptime("2017-03-30", "%Y-%m-%d").date(), color="k")


fil = "data8"
f = open(fil).readlines()

def terminates(x):
    acc = 0
    i = 0
    s = set()
    while i < len(x):
        if i in s:
            return False, acc
        s.add(i)
        ins = x[i].split(" ")[0]
        num = x[i].split(" ")[1]
        num = int(num[1:]) if num[0] == "+" else -int(num[1:])
        if ins == "nop":
            i += 1
        elif ins == "jmp":
            i += num
        elif ins == "acc":
            acc += num
            i += 1
    return True, acc

print(terminates(f)[1])

for i in range(len(f)):
    x = f.copy()
    ins = x[i].split(" ")[0]
    num = x[i].split(" ")[1]
    if ins == "nop":
        x[i] = "jmp " + num
    elif ins == "jmp":
        x[i] = "nop " + num
    else:
        continue
    if terminates(x)[0]:
        print(terminates(x)[1])
        break


from django.urls import path
from . import views

app_name = 'monitor'

urlpatterns = [
    # トップ画面
    path('', views.IndexView.as_view(), name='index'),

    # 詳細画面
    path('monitor/<int:pk>/', views.DetailView.as_view(), name='detail'),

    # グラフ描画
    path('monitor/<int:pk>/plot/', views.get_svg, name='plot'),

    # データダウンロード
    path('monitor/<int:pk>/download/', views.download, name='download'),

    # データダウンロード（ストリーミング）
    path('monitor/<int:pk>/streaming/', views.some_streaming_csv_view, name='streaming'),

    # ファイルアップロード用
    path('monitor/upload/', views.upload, name='upload'),
    path('monitor/upload_complete/', views.upload_complete, name='upload_complete'),
]


from tkinter import *
from tkinter import colorchooser
from tkinter import commondialog

class notepad:
    def __init__(self,root):
        self.root=root
        self.root.title("notepad")
        self.root.minsize(800,400)

        def do_pop(event):
            m.tk_popup(event.x_root,event.y_root)
            m.grab_release()

        def function():
            pass

        def Background_color():
            bgcolor=colorchooser.askcolor(title="Background Color")
            TXT.config(bg=bgcolor[1])

        def Foreground_color():
            fgcolor=colorchooser.askcolor(title="Foreground Color")
            TXT.config(fg=fgcolor[1])

        #right click menu
        m=Menu(self.root,tearoff=0)
        m.add_command(label="Select All",command=function)
        m.add_command(label="Copy",command=function)
        m.add_command(label="Cut",command=function)
        m.add_command(label="Paste",command=function)
        m.add_command(label="Refresh",command=function)

        self.root.bind("<Button-3>",do_pop)
        #=================================================================================
        #menubar
        menubar = Menu(self.root)

        #filemenu bar
        filemenu = Menu(menubar, tearoff=0)
        filemenu.add_command(label="New   Clt+N",)
        filemenu.add_command(label="Open   Clt+O",)
        filemenu.add_command(label="Save    Clt+S",)
        filemenu.add_command(label="Save as...   Clt+Shift+S",)
        filemenu.add_separator()
        filemenu.add_command(label="Exit", command=root.destroy)
        menubar.add_cascade(label="File", menu=filemenu)
        #======================================================================================
        #editmenu bar
        editmenu = Menu(menubar, tearoff=0)
        #submenu
        sub_color=Menu(editmenu)
        sub_color.add_command(label="Background Color",command=Background_color)
        sub_color.add_command(label="Foreground Color",command=Foreground_color)
        #========================================
        editmenu.add_command(label="Undo  Clt+Z",)
        editmenu.add_separator()
        editmenu.add_command(label="Cut  Clt+X", )
        editmenu.add_command(label="Copy  Clt+C",)
        editmenu.add_command(label="Paste   Clt+V", )
        editmenu.add_command(label="Delete", )
        editmenu.add_command(label="Select All  Clt+A",)
        editmenu.add_cascade(label="Color",menu=sub_color)
        menubar.add_cascade(label="Edit", menu=editmenu)
        #=====================================================================================
        #menubar help

        helpmenu = Menu(menubar, tearoff=0)
        helpmenu.add_command(label="Help Index",)
        helpmenu.add_command(label="About...",)
        menubar.add_cascade(label="Help", menu=helpmenu)
        self.root.config(menu=menubar)

        #=======================scrollbar ===============================and text------------------------------
        scroll=Scrollbar(self.root)
        scroll.pack(side=RIGHT,fill=Y)

        TXT=Text(self.root, width=168,height=43,font=('arial',11),bg="white",yscrollcommand=scroll.set)
        TXT.place(x=0,y=0)
        scroll.config(command=TXT.yview)

if __name__ == "__main__":
    root=Tk()
    app=notepad(root)

    root.mainloop()


from django.urls import path

from carousel import views

urlpatterns = [
    path('query/', views.query_carousel),
    path('index/', views.index),
    path('demo/', views.demo),
    path('upload_file/', views.upload_file),
    path('editBanner/', views.edit_Banner),
]


from tinydb import TinyDB, Query
db = TinyDB('counter.json')

import os

string1 ='counternull'
User = Query()
pdfsea = bool(db.search(User.pdfname == string1))
if(pdfsea):
    os.system('python3 pdf_to_image_project.py')
else:
    print('g')
db.insert({'pdfname': 'counternull'})


#wall of Richard Burgins hard work, may it stand eternal and unto it I pledge my life===============================================================================
#===================================================================================================================================================================
#===================================================================================================================================================================

import math
import random
import numpy

#Objects==========
class NeuralNet:
    def __init__(self):
        #Inputs
        self.Altitude = Neuron(1)
        self.Velocity = Neuron(1)
        self.Acceleration = Neuron(1)
        self.Fuel = Neuron(1)

        #Hidden
        self.H1 = Neuron(4)
        self.H2 = Neuron(4)
        self.H3 = Neuron(4)
        self.H4 = Neuron(4)

        #Outputs
        self.Thrust = Neuron(4)

        #Score
        self.score = 0

    def Forward(self, Alt, Velo, Acc, Fuel):
        #Input
        a = self.Altitude.ActivationFunction(Alt)
        b = self.Velocity.ActivationFunction(Velo)
        c = self.Acceleration.ActivationFunction(Acc)
        d = self.Fuel.ActivationFunction(Fuel)

        #Hidden
        HiddenInputs = [a,b,c,d]
        e = self.H1.ActivationFunction(HiddenInputs)
        f = self.H2.ActivationFunction(HiddenInputs)
        g = self.H3.ActivationFunction(HiddenInputs)
        h = self.H4.ActivationFunction(HiddenInputs)

        #Output
        return self.Thrust.ActivationFunction([e,f,g,h])

    def Scoring(self, Velo, Fuel, success):
        Score_V = -Velo *10
        Score_F = Fuel
        Score_S = 0
        if success == True:
            Score_S = 10000

        self.score = Score_S  + Score_V + Score_F

#=======
class Neuron:
    def __init__(self, num_inputs):
        self.bias = 0
        self.weight = []
        for i in range(0,num_inputs):
            self.weight.append(random.uniform(-1,1))

    def ActivationFunction(self, Input):
        total = sum(numpy.multiply(Input,self.weight))
        return (self.ReLU(total)+ self.bias)

    def ReLU(self, x):
        result = max(x, 0)
        return result

    def sigmoid(self, x):
        result = 1/(1+math.exp(-x))
        return result

    def step(self, x):
        if x > 0:
            return 1
        else:
            return -1

#Functions==========
def Review(Pop):
    #for generation size 20
    Netlist = []
    NewNetlist = []
    for i in range(0,len(Pop)):
        Netlist.append(Pop[i].Nn)
        Netlist[len(Netlist)-1].Scoring(Pop[i].testU, Pop[i].fuel, Pop[i].SUCCESS)

    Netlist = Sort(Netlist)

    #clone
    for i in range(0,int(len(Pop)//6)):
        result = clone(Netlist[0])
        NewNetlist.append(result)
        Netlist.pop(0)

    #mutate
    Mutatelist = []
    for i in range(0,int(len(Netlist)/2)):
        result = Netlist[i]
        if random.uniform(0,1)> 0.5:
            result = Mutate(Netlist[i])
        Mutatelist.append(result)

    #The rest of Netlist are removed
    NewNetlist = NewNetlist + Mutatelist

    return NewNetlist

def Sort(List):
    #Bubble sort
    Sorted = False
    while Sorted == False:
        Sorted = True
        for i in range(0, len(List)-1):
            if List[i].score < List[i+1].score:
                Sorted = False
                Buffer = List[i]
                List[i] = List[i+1]
                List[i+1] = Buffer

    return List

def Mutate(Net):
    #change the weights and biases a very small amount
    fence = 0.05
    #Inputs
    Net.Altitude.weight[0] += random.uniform(-fence,fence)
    Net.Altitude.bias += random.uniform(-fence,fence)
    Net.Velocity.weight[0] += random.uniform(-fence,fence)
    Net.Velocity.bias += random.uniform(-fence,fence)
    Net.Acceleration.weight[0] += random.uniform(-fence,fence)
    Net.Acceleration.bias += random.uniform(-fence,fence)
    Net.Fuel.weight[0] += random.uniform(-fence,fence)
    Net.Fuel.bias += random.uniform(-fence,fence)

    #Hidden
    for i in range(0,len(Net.H1.weight)):
        Net.H1.weight[i] += random.uniform(-fence,fence)
    Net.H1.bias += random.uniform(-fence,fence)
    for i in range(0,len(Net.H2.weight)):
        Net.H2.weight[i] += random.uniform(-fence,fence)
    Net.H2.bias += random.uniform(-fence,fence)
    for i in range(0,len(Net.H3.weight)):
        Net.H3.weight[i] += random.uniform(-fence,fence)
    Net.H3.bias += random.uniform(-fence,fence)
    for i in range(0,len(Net.H4.weight)):
        Net.H4.weight[i] += random.uniform(-fence,fence)
    Net.H4.bias += random.uniform(-fence,fence)

    #Outputs
    for i in range(0,len(Net.Thrust.weight)):
        Net.Thrust.weight[i] += random.uniform(-fence,fence)
    Net.Thrust.bias += random.uniform(-fence,fence)

    return Net

def clone(TopPerformer):
    result = TopPerformer
    return result


#!/home/ec2-user/my_app/env/bin/python
'''

age=23

if age>30:
	print("Valid age")
else:
	raise ValueError("Age is less than 30")

'''
age=20

try:
	assert age>30    #use like if
	print("Valid age")
except AssertionError:
	print("Raised with assert because age is lessthan 30")
except:
	print("Exception occured")


from email.policy import default
from enum import unique
import os
from sqlalchemy import Column, String, Integer,Boolean, ForeignKey, create_engine
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
import json
from datetime import datetime

database_name = 'wills_blog'
database_path = 'postgresql://{}:{}@{}/{}'.format('mike-savy','dreamlife!','localhost:5432',database_name)

db = SQLAlchemy()

def setup_db(app, database_path=database_path):
  app.config["SQLALCHEMY_DATABASE_URI"] = database_path
  app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
  app.config["PYTHONUNBUFFERED"]= ""
  db.app = app
  Migrate(app, db)
  db.init_app(app)
  db.create_all()

"""
User Model
"""
class User(db.Model):
  __tablename__ = 'users'

  id = Column(Integer, primary_key=True)
  first_name= Column(String)
  last_name = Column(String)
  nick_name = Column(String, unique=True)
  disable_account = Column(Boolean, default=False)
  facebook_link = Column(String)
  linkedIn_link = Column(String)
  instagram_link =Column(String)
  github_link = Column(String)
  role = Column(String, default='user')

  def __init__(self, first_name, last_name, nick_name, facebook_link,linkedIn_link,github_link, instagram_link, role, disable_account):
    self.first_name=first_name
    self.last_name=last_name
    self.nick_name=nick_name
    self.linkedIn_link=linkedIn_link
    self.facebook_link=facebook_link
    self.github_link=github_link
    self.instagram_link=instagram_link
    self.role = role
    self.disable_account = disable_account

  def insert(self):
    db.session.add(self)
    db.session.commit()

  def delete(self):
    db.session.delete(self)
    db.session.commit()

  def update(self):
    db.session.commit()

  def format(self):
    return {
      'id':self.id,
      'nick_name':self.nick_name,
      'linkedIn_link': self.linkedIn_link,
      'github_link':self.github_link,
      'instagram_link':self.instagram_link,
      'facebook_link':self.facebook_link,
      'role':self.role,
      'disable_account': self.disable_account
    }

"""
Comments
"""

"""

class Comment(db.Model):
  __tablename__ = 'comments'

  id = Column(Integer, primary_key= True)
  text = Column(String)
  user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
  post_id = Column(Integer, ForeignKey('post.id'), nullable= False)

  def __init__(self, text, user_id, post_id):
      self.text = text
      self.user_id = user_id
      self.post_id = post_id

  def insert(self):
    db.session.add(self)
    db.session.commit()

  def delete(self):
    db.session.delete(self)
    db.session.commit()

  def update(self):
    db.session.commit()

  def format(self):
    return ({
      'id': self.id,
      'user_id': self.user_id,
      'post_id': self.post_id
    })

"""

"""
Categories
"""

class Category(db.Model):
  __tablename__ = 'categories'

  id = Column(Integer, primary_key= True)
  type = Column(String)

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

  def insert(self):
    db.session.add(self)
    db.session.commit()

  def delete(self):
    db.session.delete(self)
    db.session.commit()

  def format(self):
    return({
      'id': self.id,
      'type': self.type
    })

"""
Posts
"""
class Post(db.Model):
  __tablename__ = 'posts'

  id = Column(Integer, primary_key=True)
  title = Column(String)
  content = Column(String)
  category_id = Column(Integer, ForeignKey('categories.id'))
  like_count = Column(Integer)
  created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)

  def  __init__(self, title, content, category_id, like_count):
    self.title = title
    self.content = content
    self.category_id = category_id
    self.like_count = like_count

  def insert(self):
    db.session.add(self)
    db.session.commit()

  def delete(self):
    db.session.delete(self)
    db.session.commit()

  def update(self):
    db.session.commit()

  def format(self):
    return({
      'id': self.id,
      'title': self.title,
      'content': self.content,
      'category_id': self.category_id,
      'like_count': self.like_count
    })


__author__ = 'asuspc'

import random
import unittest

def rand5():
    return random.randint(0,4)

class program():

    table = ((0,1,2,3,4),
             (5,6,0,1,2),
             (3,4,5,6,0),
             (1,2,3,4,5),
             (6,7,7,7,7))

    def __init__(self):
        pass

    def rand7(self):
        x = rand5()
        y = rand5()
        if y == 4 and x > 0:
            return self.rand7()
        result = self.table[y][x]
        return result

class TestStringMethods(unittest.TestCase):
    def test_rand7_equally_division(self):
        gen = program()

        set = {0 : 0, 1:0, 2 :0, 3: 0, 4: 0, 5: 0, 6 :0}
        #fill set
        for x in range(1000):
            num = gen.rand7()
            set[num] += 1

        #print set
        for x in set:
            print(str(x) + ' count: ' + str(set[x]))

        #check spread
        diff = 70

        counts = list([set[x] for x in set])
        counts.sort()
        self.assertTrue(counts[0] > counts[-1] - diff)

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


from sys import argv
import csv
import re

def max_repeat_length(s, substring):
    # how many times each substring repeats
    repeat = [0] * len(s)
    for i in range(len(s) - len(substring), -1, -1):
        if s[i: i + len(substring)] == substring:
            if i + len(substring) > len(s) - 1:
                repeat[i] = 1
            else:
                repeat[i] = 1 + repeat[i + len(substring)]

    return max(repeat)

def match(reader, substring_repeat):
     for line in reader:
         person = line[0]
         values = [int(val) for val in line[1:]]
         if values == substring_repeat:
             print(person)
             return

     print("No match")

def main():
    db_size = str(argv[1])
    with open(db_size) as db:
        reader = csv.reader(db)
        substring = next(reader)[1:]

        sequence = str(argv[2])
        with open(sequence) as seq:
            s = seq.read()
            substring_repeat = [max_repeat_length(s, seq) for seq in substring]

        match(reader, substring_repeat)

if __name__ == "__main__":
    main()