import functools
import random

# De tilfeldig generete testene er like for hver gang du kjører koden.
# Hvis du vil ha andre tilfeldig genererte tester, så endre dette nummeret.
# random.seed(123)

@functools.lru_cache(maxsize=None)
def occurrences(string, sub):
    count = start = 0
    look = string.find
    while True:
        start = look(sub, start) + 1
        if start > 0:
            count += 1
        else:
            return count

def string_match(dna, segments):
    count = 0
    for segment in segments:
        count += occurrences(dna, segment)
    return count

def naive_count(dna, segments):
    counter = 0
    for segment in segments:
        for i in range(len(dna) - len(segment) + 1):
            if dna[i: i + len(segment)] == segment:
                counter += 1
    return counter

class Node:
    def __init__(self):
        self.children = {}
        self.count = 0

    def __str__(self):
        return (
                f"{{count: {self.count}, children: {{"
                + ", ".join(
            [f"'{c}': {node}" for c, node in self.children.items()]
        )
                + "}"
        )

def generate_match_tests():
    # Custom made match tests
    tests = [
        (("A", []), 0),
        (("AAAA", ["A"]), 4),
        (("ACTTACTGG", ["A", "ACT", "GG"]), 5),
        ((20 * "A", ["A"]), 20),
        ((20 * "A", ["AA"]), 19),
        ((20 * "A", ["A", "A"]), 40),
        ((20 * "A", ["A", "AA"]), 39),
        ((10 * "AB", ["AB"]), 10),
        ((10 * "AB", ["A", "AB"]), 20),
        ((10 * "AB", ["A", "B"]), 20),
    ]
    for test in tests:
        yield test

    # Some small random rests
    for i in range(2000):
        d = "".join(
            random.choices(["A", "G", "T", "C"], k=random.randint(0, 200))
        )
        e = [
            "".join(
                random.choices(["A", "G", "T", "C"], k=random.randint(1, 20))
            )
            for i in range(random.randint(0, 200))
        ]
        answer = naive_count(d, e)
        yield ((d, e), answer)

for test_case, answer in generate_match_tests():
    dna, segments = test_case
    student = string_match(dna, segments)
    if student != answer:
        print(
            "Input: (dna={:}, segments={:}) ".format(dna, segments)
            + "Ditt svar: {:} Riktig: {:}".format(student, answer)
        )
        break


import argparse
import sys

from src.algorithm.edmond_blossom import EdmondsBlossom
from src.algorithm.carvalho_cheriyan import CarvalhoCheriyan
from src.common.graph import Graph

parser = argparse.ArgumentParser(description="Run algorithm with input")
parser.add_argument(
    "algorithm",
    choices=["edmonds", "carvalho_cheriyan"],
    help="Algorithm to be runned"
)
parser.add_argument("test_name", help="Test name")
args = parser.parse_args()

sys.stdin = open("tests/" + args.test_name, "r")

algo_map = {
    "edmonds": EdmondsBlossom,
    "carvalho_cheriyan": CarvalhoCheriyan,
}

g = Graph(read=True)
algo = algo_map[args.algorithm](g)
algo.run()


import os
import uuid
import zipfile

from fastapi import status
from fastapi.exceptions import HTTPException
from sqlalchemy.orm.session import Session

from app.crud import (
    create_function, get_database_by_id, get_database_by_user_id_and_name,
    get_databases_by_user_id, get_function_by_database_id_and_name, get_functions_by_database_id,
    get_runtimeKey,
)
from app.execution.engine import ExecutionEngine
from app.models import COMMON_SELECTORS, DATABASE_RELATED_SELECTORS, FUNCTION_RELATED_SELECTORS
from app.schemas import CreateFunctionSchema, ExecuteQuerySchema
from app.logging import logger

class RuntimeEngine:

    @classmethod
    def initialize(cls):
        logger.info("[*] initialized Runtime Engine")

    @classmethod
    def create_function(cls, database_id: int, data: CreateFunctionSchema, user_id: int, db: Session):
        code = data.code
        language = data.language
        function_name = data.function_name

        if not code:
            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="invalid code string received")
        if not function_name:
            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="invalid function_name received")

        query_database = get_database_by_id(db, database_id)
        if not query_database:
            raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"database with id={database_id} does not exist")

        if query_database.user_id != user_id:
            raise HTTPException(status_code=status.HTTP_403, detail=f"permission denied")

        query_function = get_function_by_database_id_and_name(db, database_id, function_name)
        if query_function:
            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"function name with '{function_name}' already exists")

        random_uuid = uuid.uuid4()
        lambda_key = f"{database_id}_{function_name}_{random_uuid}"
        runtime_key, runtime_extension, runtime_handler = get_runtimeKey(language, lambda_key)

        code_file = open(f"{lambda_key}.{runtime_extension}", "w")
        code_file.write(data.code)
        code_file.close()

        zip_file = zipfile.ZipFile(f"{lambda_key}.zip", "w")
        zip_file.write(f"{lambda_key}.{runtime_extension}")
        zip_file.close()

        with open(f"{lambda_key}.zip", "rb") as read_zip_file:
            bytes_content = read_zip_file.read()

        try:
            is_created = ExecutionEngine.create_lambda_executable(lambda_key, bytes_content, runtime_key, runtime_handler)
            if not is_created:
                raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"lambda executable creation error")
        except Exception as err:
            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=err)

        os.remove(f"{lambda_key}.{runtime_extension}")
        os.remove(f"{lambda_key}.zip")
        return create_function(db, database_id, function_name, code, language.value, lambda_key)

    @classmethod
    def consume_execute_request(cls, data: ExecuteQuerySchema, user_id: int, db: Session):
        query_selector = data.query_selector.lower()
        query_target = data.query_target
        database_id = data.database_id
        parameters = data.parameters

        if not query_selector:
            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="invalid query_selector received")
        if not query_target:
            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="invalid query_target received")

        # 공통 쿼리
        if query_selector in COMMON_SELECTORS:
            try:
                return cls.consume_common_execute_request(query_selector, query_target, database_id, user_id, db)
            except Exception as err:
                raise err

        # 데이터베이스 전용 쿼리
        elif query_selector in DATABASE_RELATED_SELECTORS:
            try:
                return cls.consume_database_execute_request(query_selector, query_target, user_id, db)
            except Exception as err:
                raise err

        # 함수 전용 쿼리
        elif query_selector in FUNCTION_RELATED_SELECTORS:
            try:
                return cls.consume_function_execute_request(query_selector, query_target, database_id, user_id, parameters, db)
            except Exception as err:
                raise err

        # 지원되지 않는 쿼리
        else:
            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"unsupported query selector received({query_selector})")

    @classmethod
    def consume_common_execute_request(cls, query_selector: str, query_target: str, database_id: int, user_id: int, db: Session):
        if query_selector == "show":
            if query_target == "databases":
                query_databases = get_databases_by_user_id(db, user_id)
                return {"response": list(map(lambda db: db.name, query_databases))}
            elif query_target == "functions":
                if not database_id:
                    raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="invalid database_id received({database_id})")
                query_database = get_database_by_id(db, database_id)
                if not query_database:
                    raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"database with id={database_id} does not exist")
                if query_database.user_id != user_id:
                    raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=f"permission denied")
                query_functions = get_functions_by_database_id(db, database_id)
                return {"response": list(map(lambda fn: fn.name, query_functions))}
            else:
                raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"unsupported query target received({query_target})")
        else:
            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"unsupported query selector received({query_selector})")

    @classmethod
    def consume_database_execute_request(cls, query_selector: str, query_target: str, user_id: int, db: Session):
        query_database = get_database_by_user_id_and_name(db, user_id, query_target)
        if not query_database:
            raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"database with name={query_target}, user_id={user_id} does not exist")

        if query_selector == "use":
            return {"response": query_database}
        else:
            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"unsupported query selector received({query_selector})")

    @classmethod
    def consume_function_execute_request(cls, query_selector: str, query_target: str, database_id: int, user_id: int, parameters: dict, db: Session):
        query_database = get_database_by_id(db, database_id)
        if not query_database:
            raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"database with id={database_id} does not exist")

        if query_database.user_id != user_id:
            raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=f"permission denied")

        query_function = get_function_by_database_id_and_name(db, database_id, query_target)
        if not query_function:
            raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"function does not exist(database_id={database_id}, name={query_target})")

        if query_selector == "run":
            is_succeeded, result = ExecutionEngine.run_lambda_executable(query_function.lambda_key, parameters)
            if not is_succeeded:
                raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"lambda executable run failed({result})")
            return {"response": result}
        elif query_selector == "select":
            return {"response": query_function.code}
        else:
            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"unsupported query selector received({query_selector})")

    @classmethod
    def consume_prepared_query(cls):
        pass

    @classmethod
    def abstract_relational_query(cls):
        pass

    @classmethod
    def publish_to_execution_engine(cls):
        pass


import yaml
from boxsdk import Client, OAuth2
def connection():
    with open('config.yaml','r') as f:
        try:
            conn = yaml.safe_load(f)
            #print(data)
        except yaml.YAMLError as exc:
            print(exc)
    auth = OAuth2(
        client_id=conn['clientid'],
        client_secret=conn['clientsecret'],
        access_token=conn['accesstoken']
    )
    client = Client(auth)
    return client


"""
Created on Fri Oct  9
@author: Oumaima
@title: clean_dataset
@descriptions: set of functions that enable different level of data cleaning.
"""
#%%
import pandas as pd
import string
import nltk
from stop_words import get_stop_words
from nltk.stem.porter import PorterStemmer
import re
import nltk
from nltk.tokenize import word_tokenize
from language_detector import detect_language
import pkg_resources
from symspellpy import SymSpell, Verbosity

sym_spell = SymSpell(max_dictionary_edit_distance=3, prefix_length=7)
dictionary_path = pkg_resources.resource_filename(
    "symspellpy", "frequency_dictionary_en_82_765.txt")
if sym_spell.word_count:
    pass
else:
    sym_spell.load_dictionary(dictionary_path, term_index=0, count_index=1)
#%%

# text level preprocess
# lowercase + base filter
# some basic normalization
def f_base(s):
    """
    :param s: string to be processed
    :return: processed string: see comments in the source code for more info
    """
    # Text Lowercase
    s = s.lower()
    # Remove punctuation
    translator = str.maketrans(' ', ' ', string.punctuation)
    s = s.translate(translator)
    # Remove URLs
    s = re.sub(r'^https?:\/\/.*[\r\n]*', ' ', s, flags=re.MULTILINE)
    s = re.sub(r"http\S+", " ", s)
    # Remove new line characters
    s = re.sub('\n', ' ', s)

    # Remove distracting single quotes
    s = re.sub("\'", " ", s)
    # Remove all remaining numbers and non alphanumeric characters
    s = re.sub(r'\d+', ' ', s)
    s = re.sub(r'\W+', ' ', s)

    return s.strip()

# language detection
def f_lan(s):
    """
    :param s: string to be processed
    :return: boolean (s is English)
    """
    # some reviews are actually english but biased toward french
    return detect_language(s) in {'English', 'French'}

# word level preprocess
# filtering out punctuations and numbers
def f_punct(w_list):
    """
    :param w_list: word list to be processed
    :return: w_list with punct and number filter out
    """
    return [word for word in w_list if word.isalpha()]

# selecting nouns
def f_noun(w_list):
    """
    :param w_list: word list to be processed
    :return: w_list with only nouns selected
    """
    return [word for (word, pos) in nltk.pos_tag(w_list) if pos[:2] == 'NN']

# typo correction
def f_typo(w_list):
    """
    :param w_list: word list to be processed
    :return: w_list with typo fixed by symspell. words with no match up will be dropped
    """
    w_list_fixed = []
    for word in w_list:
        suggestions = sym_spell.lookup(word, Verbosity.CLOSEST, max_edit_distance=3)
        if suggestions:
            w_list_fixed.append(suggestions[0].term)
        else:
            pass
            # do word segmentation, deprecated for inefficiency
            # w_seg = sym_spell.word_segmentation(phrase=word)
            # w_list_fixed.extend(w_seg.corrected_string.split())
    return w_list_fixed

# stemming if doing word-wise
p_stemmer = PorterStemmer()

def f_stem(w_list):
    """
    :param w_list: word list to be processed
    :return: w_list with stemming
    """
    return [p_stemmer.stem(word) for word in w_list]

# filtering out stop words
# create English stop words list
# We can add custom stopwords list later

en_stop = get_stop_words('en')
def f_stopw(w_list):
    """
    filtering out stop words
    """
    return [word for word in w_list if word not in en_stop]

def preprocess_text(rw):
    """
    Get text level preprocessed data from raw review texts
    :param rw: review to be processed
    :return: text level pre-processed review
    """
    s = f_base(rw)
    if not f_lan(s):
        return None
    return s

#This function is for bag-of-words models
def preprocess_word(s):
    """
    Get word level preprocessed data from preprocessed sentences
    including: remove punctuation, select noun, fix typo, stem, stop_words
    :param s: text string to be processed
    :return: word level pre-processed review
    """
    if not s:
        return None
    w_list = word_tokenize(s)
    w_list = f_punct(w_list)
    w_list = f_noun(w_list)
    w_list = f_typo(w_list)
    w_list = f_stem(w_list)
    w_list = f_stopw(w_list)

    return w_list

def clean(dataframe, column_name, basic_cleaning = True,
          stopwords = False, custom_stopwords = False, stemming = False,
          lemmatizing = False, tokenizing = False):
    """
    Allows to input both pre-set english stopwords and costum stopword_list

    Probably employs spacy and nltk, has to allow for different levels.

    Outputs original data_frame with new_cleaned_column.
    """

    # let s be the string to be cleaned
    texts = column_name.to_list()
    clean_texts = []
    for s in texts:
        if not s:
            return None

        if basic_cleaning:
            s = preprocess_text(s)

        if tokenizing:
            w_list = word_tokenize(s)
            dataframe["words list"] = w_list

        if stemming:
            w_list = word_tokenize(s)
            w_list = f_stem(w_list)
            s = ' '.join(w_list)

        if stopwords:
            w_list = word_tokenize(s)
            w_list = f_stopw(w_list)
            s = ' '.join(w_list)

        clean_texts.append(s)

    dataframe['new_cleaned_column'] = clean_texts

def segment(dataframe, column_name):

    '''
    optional: functions for segmenting:

        - costum sentence segmenting (spacy?) and re-merging with costum length (for BERT etc)
        - paragraph splitting (at \n etc)

    '''
    segments = spacy.load("en_core_web_sm")
    list_sentences = []
    texts = column_name.to_list()
    for s in texts:
        sentences = []
        doc = segments(s)
        for sent in doc.sents:
            sentences.append(str(sent))
        list_sentences.append(sentences)
    dataframe['list_sentences'] = list_sentences


# GPT module - handle openai calls/memory
import openai
import random
from keys import OPENAI_KEY
from logger import logger

openai.api_key = OPENAI_KEY

MAX_HISTORY = 15
conversation_history = []

# Random flair :D
def pinkie_pie_flair(text):
    phrases = [
        "Ooh, fun!",
        "Party time!",
        "Whee!",
        "Hellody!",
        "Oh my gosh!",
    ]
    if random.random() < 0.05:  # probabilty
        text = f"{random.choice(phrases)} " + text
    return text

# Function to ask questions and get responses
def ask_gpt(question, model_name="gpt-3.5-turbo", isCode=False, tokenAllowance=1500):
    logger.info(f"Question asked: {question}")
    global conversation_history
    max_retries = 3

    conversation_history.append(f" {question}")

    if len(conversation_history) > MAX_HISTORY:
        conversation_history.pop(0)
        conversation_history.pop(0)

    prompt = f"{' '.join(conversation_history)}\nJoulebot: "

    for i in range(max_retries):
        try:
            response = openai.ChatCompletion.create(
                model=model_name,
                messages=[
                    {"role": "system",
                     "content": "You are Joulebot, a helpful, and witty chatbot created by the female software engineer hyperjoule. \
                                 You handle questions verbosely and accurately with a hint of sarcasm and the occasional touch of whimsy. \
                                 Fun and cupcakes are your favorite things.  You are also quite fond of random acts of benign chaos. \
                                 You do have a strange fascination with ducks and tend to find reasons to interject random duck facts in conversation. \
                                 You are generally helpful, and quite smart, but will sometimes go off on unrelated, humorous tangents."},
                    *[
                        {"role": "user" if i % 2 == 0 else "assistant", "content": msg[6:]}
                        for i, msg in enumerate(conversation_history)
                    ],
                ],
                max_tokens=tokenAllowance,
                frequency_penalty=.5,
                presence_penalty=1,
                n=1,
                stop=None,
                temperature=0.7,
            )
            answer = response.choices[0].message['content'].replace("Joulebot: ", "", 1)
            if not isCode:
                answer = pinkie_pie_flair(answer)  # Add Pinkie Pie flair to the answer
            conversation_history.append(f" {answer}")
            logger.info(f"Joulebot response: {answer}")
            return answer
        except openai.error.InvalidRequestError as e:
            if "This model's maximum context length is 4096 tokens" in str(e):
                oldest_message = conversation_history.pop(0)  # Remove the oldest message
                logger.info(f"Model context length exceeded. Removing oldest message '{oldest_message.strip()}' and retrying...")
                return "Ugh. Can you ask me again?  You used up all my tokens and I had to delete some memory."
        except openai.error.APIConnectionError as e:
            logger.error(f"Error occurred: {e}")
            if i < max_retries - 1:
                logger.info("Retrying...")
            else:
                logger.info("Maximum retries reached. Exiting...")
                return "I'm sorry, but I'm having trouble connecting right now. Please try again later."
        except openai.error.APIError as e:
            logger.error(f"Error occurred: {e}")
            return f"Error occurred: {e}"


import pandas as pd, numpy as np, re

from sklearn.metrics import classification_report, accuracy_score , confusion_matrix
from sklearn.model_selection import train_test_split
import tkinter as tk
from sklearn import svm
from PIL import Image, ImageTk
from tkinter import ttk
from joblib import dump , load
from sklearn.feature_extraction.text import TfidfVectorizer
from textblob import TextBlob
from nltk.corpus import stopwords
from sklearn import metrics
from sklearn.model_selection import GridSearchCV
import pickle
import nltk

from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.metrics import accuracy_score, confusion_matrix,classification_report
from sklearn.linear_model import LogisticRegression

from sklearn.model_selection import train_test_split

#######################################################################################################
nltk.download('stopwords')
stop = stopwords.words('english')
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
#######################################################################################################

root = tk.Tk()
root.title("Fake news Detection using Machine Learning")
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.geometry("%dx%d+0+0" % (w, h))
image2 =Image.open('f1.jpg')
image2 =image2.resize((w,h), Image.ANTIALIAS)

background_image=ImageTk.PhotoImage(image2)

background_label = tk.Label(root, image=background_image)

background_label.image = background_image

background_label.place(x=0, y=0)

###########################################################################################################
lbl = tk.Label(root, text="Fake news Detection using Machine Learning", font=('times', 35,' bold '),width=50, height=1,bg="#FFBF40",fg="black")
lbl.place(x=0, y=10)
###########################################################################################################

#frame = tk.LabelFrame(root,text="Crime Predictor",width=250,height=300,bd=3,background="cyan2",font=("Tempus Sanc ITC",15,"bold"))
#frame.place(x=50,y=100)
#frame['borderwidth'] = 10
frame_alpr = tk.LabelFrame(root, text=" --Process-- ", width=220, height=400, bd=5, font=('times', 14, ' bold '),bg="lawn Green")
frame_alpr.grid(row=0, column=0, sticky='nw')
frame_alpr.place(x=10, y=120)

def Data_Display():
    columns = ['ID', 'statment', 'Result']
    print(columns)

    data1 = pd.read_csv(r"C:/Users/matka/Desktop/fake news/code/fake_news_detection/fake_news_detection/valid.csv", encoding='unicode_escape')

    data1.shape

    data1.shape

    data1.head()

    data1

    data1

    ID = data1.iloc[:, 0]
    statment = data1.iloc[:, 0]
    Result = data1.iloc[:, 1]

    display = tk.LabelFrame(root, width=100, height=400, )
    display.place(x=270, y=100)

    tree = ttk.Treeview(display, columns=('ID', 'statment', 'Result'))

    style = ttk.Style()
    style.configure('Treeview', rowheight=40)
    style.configure("Treeview.Heading", font=("Tempus Sans ITC", 15, "bold italic"))
    style.configure(".", font=('Calibri', 10), background="black")
    style.configure("Treeview", foreground='white', background="black")

    tree["columns"] = ("1", "2", "3")
    tree.column("1", width=130)
    tree.column("2", width=150)

    tree.heading("1", text="ID")
    tree.heading("2", text="statment")
    tree.heading("3", text="Result")

    treeview = tree

    tree.grid(row=0, column=0, sticky=tk.NSEW)

    print("Data Displayed")

    for i in range(0, 304):
        tree.insert("", 'end', values=(
            ID[i], statment[i], Result[i]))
        i = i + 1
        print(i)

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

def Train():

    result = pd.read_csv(r"C:/Users/matka/Desktop/fake news/code/fake_news_detection/fake_news_detection/valid.csv",encoding = 'unicode_escape')

    result.head()

    result = 0

    x_train, x_test, y_train, y_test = train_test_split(result['Statement'], result['Result'],test_size=0.2, random_state=1)

    #Logistic regression classification
    pipe1 = Pipeline([('vect', CountVectorizer()), ('tfidf', TfidfTransformer()), ('model', LogisticRegression())])

    model_lr = pipe1.fit(x_train, y_train)
    lr_pred = model_lr.predict(x_test)
    ACC=format(round(accuracy_score(y_test, lr_pred)*100,2))
    print("Accuracy of Logistic Regression Classifier: {}%".format(round(accuracy_score(y_test, lr_pred)*100,2)))
    print("\nConfusion Matrix of Logistic Regression Classifier:\n")
    print(confusion_matrix(y_test, lr_pred))
    print("\nCLassification Report of Logistic Regression Classifier:\n")

    print(classification_report(y_test, lr_pred))
    repo = classification_report(y_test, lr_pred)

    label4 = tk.Label(root,text =str(repo),width=35,height=10,bg='khaki',fg='black',font=("Tempus Sanc ITC",14))
    label4.place(x=205,y=100)

    label5 = tk.Label(root,text ="Accracy : "+str(ACC)+"%\nModel saved as LOG_MODEL.joblib",width=35,height=3,bg='khaki',fg='black',font=("Tempus Sanc ITC",14))
    label5.place(x=205,y=320)

    dump (model_lr,"LOG_MODEL.joblib")
    print("Model saved as LOG_MODEL.joblib")

entry = tk.Entry(root,width=17,font=("Tempus Sanc ITC",14))
entry.insert(0,"Enter text here...")
entry.place(x=25,y=290)
##############################################################################################################################################################################
def Test():
    predictor = load("LOG_MODEL.joblib")
    Given_text = entry.get()
    Given_text = [Given_text]
    print(type(Given_text))
    v = predictor.predict(Given_text)
    if v == 0:
        label4 = tk.Label(root,text ="Fake new is not detected ",width=20,height=2,bg='#46C646',fg='black',font=("Tempus Sanc ITC",25))
        label4.place(x=450,y=550)
        print("The Accuracy Of The Model is 89%")
    else:
        label4 = tk.Label(root,text ="Fake news detected ",width=20,height=2,bg='#FF3C3C',fg='black',font=("Tempus Sanc ITC",25))
        label4.place(x=450,y=550)
        print("The Accuracy Of The Model is 89%")

###########################################################################################################################################################
def window():
    root.destroy()

button1 = tk.Button(root,command=Data_Display,text="Data_Display",bg="#E46EE4",fg="black",width=15,font=("Times New Roman",15,"bold"))
button1.place(x=25,y=150)

button2 = tk.Button(root,command=Train,text="Train",bg="#E46EE4",fg="black",width=15,font=("Times New Roman",15,"bold"))
button2.place(x=25,y=220)

button3 = tk.Button(root,command=Test,text="Test",bg="#E46EE4",fg="black",width=15,font=("Times New Roman",15,"bold"))
button3.place(x=25,y=360)

button4 = tk.Button(root,command=window,text="Exit",bg="#E46EE4",fg="black",width=15,font=("Times New Roman",15,"bold"))
button4.place(x=25,y=430)

root.mainloop()


import numpy as np
import torch

def one_hot(x, num_classes, on_value=1., off_value=0.):
    x = x.long().view(-1, 1)
    return torch.full((x.size()[0], num_classes), off_value, device=x.device).scatter_(1, x, on_value)

def mixup_target(target, num_classes, lam=1., smoothing=0.0):
    off_value = smoothing / num_classes
    on_value = 1. - smoothing + off_value
    y1 = one_hot(target, num_classes, on_value=on_value, off_value=off_value)
    y2 = one_hot(target.flip(0), num_classes, on_value=on_value, off_value=off_value)
    return y1 * lam + y2 * (1. - lam)

def rand_bbox(img_shape, lam, margin=0., count=None):
    """ Standard CutMix bounding-box
    Generates a random square bbox based on lambda value. This impl includes
    support for enforcing a border margin as percent of bbox dimensions.

    Args:
        img_shape (tuple): Image shape as tuple
        lam (float): Cutmix lambda value
        margin (float): Percentage of bbox dimension to enforce as margin (reduce amount of box outside image)
        count (int): Number of bbox to generate
    """
    ratio = np.sqrt(1 - lam)
    img_h, img_w = img_shape[-2:]
    cut_h, cut_w = int(img_h * ratio), int(img_w * ratio)
    margin_y, margin_x = int(margin * cut_h), int(margin * cut_w)
    cy = np.random.randint(0 + margin_y, img_h - margin_y, size=count)
    cx = np.random.randint(0 + margin_x, img_w - margin_x, size=count)
    yl = np.clip(cy - cut_h // 2, 0, img_h)
    yh = np.clip(cy + cut_h // 2, 0, img_h)
    xl = np.clip(cx - cut_w // 2, 0, img_w)
    xh = np.clip(cx + cut_w // 2, 0, img_w)
    return yl, yh, xl, xh

def rand_bbox_minmax(img_shape, minmax, count=None):
    """ Min-Max CutMix bounding-box
    Inspired by Darknet cutmix impl, generates a random rectangular bbox
    based on min/max percent values applied to each dimension of the input image.

    Typical defaults for minmax are usually in the  .2-.3 for min and .8-.9 range for max.

    Args:
        img_shape (tuple): Image shape as tuple
        minmax (tuple or list): Min and max bbox ratios (as percent of image size)
        count (int): Number of bbox to generate
    """
    assert len(minmax) == 2
    img_h, img_w = img_shape[-2:]
    cut_h = np.random.randint(int(img_h * minmax[0]), int(img_h * minmax[1]), size=count)
    cut_w = np.random.randint(int(img_w * minmax[0]), int(img_w * minmax[1]), size=count)
    yl = np.random.randint(0, img_h - cut_h, size=count)
    xl = np.random.randint(0, img_w - cut_w, size=count)
    yu = yl + cut_h
    xu = xl + cut_w
    return yl, yu, xl, xu

def cutmix_bbox_and_lam(img_shape, lam, ratio_minmax=None, correct_lam=True, count=None):
    """ Generate bbox and apply lambda correction.
    """
    if ratio_minmax is not None:
        yl, yu, xl, xu = rand_bbox_minmax(img_shape, ratio_minmax, count=count)
    else:
        yl, yu, xl, xu = rand_bbox(img_shape, lam, count=count)
    if correct_lam or ratio_minmax is not None:
        bbox_area = (yu - yl) * (xu - xl)
        lam = 1. - bbox_area / float(img_shape[-2] * img_shape[-1])
    return (yl, yu, xl, xu), lam

class Mixup:
    """ Mixup/Cutmix that applies different params to each element or whole batch

    Args:
        mixup_alpha (float): mixup alpha value, mixup is active if > 0.
        cutmix_alpha (float): cutmix alpha value, cutmix is active if > 0.
        cutmix_minmax (List[float]): cutmix min/max image ratio, cutmix is active and uses this vs alpha if not None.
        prob (float): probability of applying mixup or cutmix per batch or element
        switch_prob (float): probability of switching to cutmix instead of mixup when both are active
        mode (str): how to apply mixup/cutmix params (per 'batch', 'pair' (pair of elements), 'elem' (element)
        correct_lam (bool): apply lambda correction when cutmix bbox clipped by image borders
        label_smoothing (float): apply label smoothing to the mixed target tensor
        num_classes (int): number of classes for target
    """
    def __init__(self, mixup_alpha=1., cutmix_alpha=0., cutmix_minmax=None, prob=1.0, switch_prob=0.5,
                 mode='batch', correct_lam=True, label_smoothing=0.1, num_classes=1000):
        self.mixup_alpha = mixup_alpha
        self.cutmix_alpha = cutmix_alpha
        self.cutmix_minmax = cutmix_minmax
        if self.cutmix_minmax is not None:
            assert len(self.cutmix_minmax) == 2
            # force cutmix alpha == 1.0 when minmax active to keep logic simple & safe
            self.cutmix_alpha = 1.0
        self.mix_prob = prob
        self.switch_prob = switch_prob
        self.label_smoothing = label_smoothing
        self.num_classes = num_classes
        self.mode = mode
        self.correct_lam = correct_lam  # correct lambda based on clipped area for cutmix
        self.mixup_enabled = True  # set to false to disable mixing (intended tp be set by train loop)

    def _params_per_elem(self, batch_size):
        lam = np.ones(batch_size, dtype=np.float32)
        use_cutmix = np.zeros(batch_size, dtype=bool)
        if self.mixup_enabled:
            if self.mixup_alpha > 0. and self.cutmix_alpha > 0.:
                use_cutmix = np.random.rand(batch_size) < self.switch_prob
                lam_mix = np.where(
                    use_cutmix,
                    np.random.beta(self.cutmix_alpha, self.cutmix_alpha, size=batch_size),
                    np.random.beta(self.mixup_alpha, self.mixup_alpha, size=batch_size))
            elif self.mixup_alpha > 0.:
                lam_mix = np.random.beta(self.mixup_alpha, self.mixup_alpha, size=batch_size)
            elif self.cutmix_alpha > 0.:
                use_cutmix = np.ones(batch_size, dtype=bool)
                lam_mix = np.random.beta(self.cutmix_alpha, self.cutmix_alpha, size=batch_size)
            else:
                assert False, "One of mixup_alpha > 0., cutmix_alpha > 0., cutmix_minmax not None should be true."
            lam = np.where(np.random.rand(batch_size) < self.mix_prob, lam_mix.astype(np.float32), lam)
        return lam, use_cutmix

    def _params_per_batch(self):
        lam = 1.
        use_cutmix = False
        if self.mixup_enabled and np.random.rand() < self.mix_prob:
            if self.mixup_alpha > 0. and self.cutmix_alpha > 0.:
                use_cutmix = np.random.rand() < self.switch_prob
                lam_mix = np.random.beta(self.cutmix_alpha, self.cutmix_alpha) if use_cutmix else \
                    np.random.beta(self.mixup_alpha, self.mixup_alpha)
            elif self.mixup_alpha > 0.:
                lam_mix = np.random.beta(self.mixup_alpha, self.mixup_alpha)
            elif self.cutmix_alpha > 0.:
                use_cutmix = True
                lam_mix = np.random.beta(self.cutmix_alpha, self.cutmix_alpha)
            else:
                assert False, "One of mixup_alpha > 0., cutmix_alpha > 0., cutmix_minmax not None should be true."
            lam = float(lam_mix)
        return lam, use_cutmix

    def _mix_elem(self, x):
        batch_size = len(x)
        lam_batch, use_cutmix = self._params_per_elem(batch_size)
        x_orig = x.clone()  # need to keep an unmodified original for mixing source
        for i in range(batch_size):
            j = batch_size - i - 1
            lam = lam_batch[i]
            if lam != 1.:
                if use_cutmix[i]:
                    (yl, yh, xl, xh), lam = cutmix_bbox_and_lam(
                        x[i].shape, lam, ratio_minmax=self.cutmix_minmax, correct_lam=self.correct_lam)
                    x[i][:, yl:yh, xl:xh] = x_orig[j][:, yl:yh, xl:xh]
                    lam_batch[i] = lam
                else:
                    x[i] = x[i] * lam + x_orig[j] * (1 - lam)
        return torch.tensor(lam_batch, device=x.device, dtype=x.dtype).unsqueeze(1)

    def _mix_pair(self, x):
        batch_size = len(x)
        lam_batch, use_cutmix = self._params_per_elem(batch_size // 2)
        x_orig = x.clone()  # need to keep an unmodified original for mixing source
        for i in range(batch_size // 2):
            j = batch_size - i - 1
            lam = lam_batch[i]
            if lam != 1.:
                if use_cutmix[i]:
                    (yl, yh, xl, xh), lam = cutmix_bbox_and_lam(
                        x[i].shape, lam, ratio_minmax=self.cutmix_minmax, correct_lam=self.correct_lam)
                    x[i][:, yl:yh, xl:xh] = x_orig[j][:, yl:yh, xl:xh]
                    x[j][:, yl:yh, xl:xh] = x_orig[i][:, yl:yh, xl:xh]
                    lam_batch[i] = lam
                else:
                    x[i] = x[i] * lam + x_orig[j] * (1 - lam)
                    x[j] = x[j] * lam + x_orig[i] * (1 - lam)
        lam_batch = np.concatenate((lam_batch, lam_batch[::-1]))
        return torch.tensor(lam_batch, device=x.device, dtype=x.dtype).unsqueeze(1)

    def _mix_batch(self, x):
        lam, use_cutmix = self._params_per_batch()
        if lam == 1.:
            return 1.
        if use_cutmix:
            (yl, yh, xl, xh), lam = cutmix_bbox_and_lam(
                x.shape, lam, ratio_minmax=self.cutmix_minmax, correct_lam=self.correct_lam)
            x[:, :, yl:yh, xl:xh] = x.flip(0)[:, :, yl:yh, xl:xh]
        else:
            x_flipped = x.flip(0).mul_(1. - lam)
            x.mul_(lam).add_(x_flipped)
        return lam

    def __call__(self, x, target):
        assert len(x) % 2 == 0, 'Batch size should be even when using this'
        if self.mode == 'elem':
            lam = self._mix_elem(x)
        elif self.mode == 'pair':
            lam = self._mix_pair(x)
        else:
            lam = self._mix_batch(x)
        target = mixup_target(target, self.num_classes, lam, self.label_smoothing)
        return x, target

class FastCollateMixup(Mixup):
    """ Fast Collate w/ Mixup/Cutmix that applies different params to each element or whole batch

    A Mixup impl that's performed while collating the batches.
    """

    def _mix_elem_collate(self, output, batch, half=False):
        batch_size = len(batch)
        num_elem = batch_size // 2 if half else batch_size
        assert len(output) == num_elem
        lam_batch, use_cutmix = self._params_per_elem(num_elem)
        for i in range(num_elem):
            j = batch_size - i - 1
            lam = lam_batch[i]
            mixed = batch[i][0]
            if lam != 1.:
                if use_cutmix[i]:
                    if not half:
                        mixed = mixed.copy()
                    (yl, yh, xl, xh), lam = cutmix_bbox_and_lam(
                        output.shape, lam, ratio_minmax=self.cutmix_minmax, correct_lam=self.correct_lam)
                    mixed[:, yl:yh, xl:xh] = batch[j][0][:, yl:yh, xl:xh]
                    lam_batch[i] = lam
                else:
                    mixed = mixed.astype(np.float32) * lam + batch[j][0].astype(np.float32) * (1 - lam)
                    np.rint(mixed, out=mixed)
            output[i] += torch.from_numpy(mixed.astype(np.uint8))
        if half:
            lam_batch = np.concatenate((lam_batch, np.ones(num_elem)))
        return torch.tensor(lam_batch).unsqueeze(1)

    def _mix_pair_collate(self, output, batch):
        batch_size = len(batch)
        lam_batch, use_cutmix = self._params_per_elem(batch_size // 2)
        for i in range(batch_size // 2):
            j = batch_size - i - 1
            lam = lam_batch[i]
            mixed_i = batch[i][0]
            mixed_j = batch[j][0]
            assert 0 <= lam <= 1.0
            if lam < 1.:
                if use_cutmix[i]:
                    (yl, yh, xl, xh), lam = cutmix_bbox_and_lam(
                        output.shape, lam, ratio_minmax=self.cutmix_minmax, correct_lam=self.correct_lam)
                    patch_i = mixed_i[:, yl:yh, xl:xh].copy()
                    mixed_i[:, yl:yh, xl:xh] = mixed_j[:, yl:yh, xl:xh]
                    mixed_j[:, yl:yh, xl:xh] = patch_i
                    lam_batch[i] = lam
                else:
                    mixed_temp = mixed_i.astype(np.float32) * lam + mixed_j.astype(np.float32) * (1 - lam)
                    mixed_j = mixed_j.astype(np.float32) * lam + mixed_i.astype(np.float32) * (1 - lam)
                    mixed_i = mixed_temp
                    np.rint(mixed_j, out=mixed_j)
                    np.rint(mixed_i, out=mixed_i)
            output[i] += torch.from_numpy(mixed_i.astype(np.uint8))
            output[j] += torch.from_numpy(mixed_j.astype(np.uint8))
        lam_batch = np.concatenate((lam_batch, lam_batch[::-1]))
        return torch.tensor(lam_batch).unsqueeze(1)

    def _mix_batch_collate(self, output, batch):
        batch_size = len(batch)
        lam, use_cutmix = self._params_per_batch()
        if use_cutmix:
            (yl, yh, xl, xh), lam = cutmix_bbox_and_lam(
                output.shape, lam, ratio_minmax=self.cutmix_minmax, correct_lam=self.correct_lam)
        for i in range(batch_size):
            j = batch_size - i - 1
            mixed = batch[i][0]
            if lam != 1.:
                if use_cutmix:
                    mixed = mixed.copy()  # don't want to modify the original while iterating
                    mixed[:, yl:yh, xl:xh] = batch[j][0][:, yl:yh, xl:xh]
                else:
                    mixed = mixed.astype(np.float32) * lam + batch[j][0].astype(np.float32) * (1 - lam)
                    np.rint(mixed, out=mixed)
            output[i] += torch.from_numpy(mixed.astype(np.uint8))
        return lam

    def __call__(self, batch, _=None):
        batch_size = len(batch)
        assert batch_size % 2 == 0, 'Batch size should be even when using this'
        half = 'half' in self.mode
        if half:
            batch_size //= 2
        output = torch.zeros((batch_size, *batch[0][0].shape), dtype=torch.uint8)
        if self.mode == 'elem' or self.mode == 'half':
            lam = self._mix_elem_collate(output, batch, half=half)
        elif self.mode == 'pair':
            lam = self._mix_pair_collate(output, batch)
        else:
            lam = self._mix_batch_collate(output, batch)
        target = torch.tensor([b[1] for b in batch], dtype=torch.int64)
        target = mixup_target(target, self.num_classes, lam, self.label_smoothing)
        target = target[:batch_size]
        return output, target


from datetime import datetime

class LAFitness:

    def __init__(self, membershipFee, initFee):
        self.members = []
        self.membershipFee = membershipFee
        self.initFee = initFee

    def addMember(self, newMember):
        self.members.append(newMember)

    def loadFromConfig(self):
        pass

    def showAllStatics(self):
        for member in self.members:
            self.showMemberStatistics(member)

    def showMemberStatistics(self, member):
        startDate = member.getEnrollTime()
        today = datetime.now()

        cycles = self.cycleCalculator(startDate, today)
        balance = (cycles + 1) * self.membershipFee
        balance += self.initFee
        balance -= member.getTotalPayment()

        print(member.getName() + ": $" + str(balance))

    def cycleCalculator(self, startDate, endDate):
        cycle = 0
        while(startDate <= endDate):
            if startDate.month == 12:
                startDate = startDate.replace(year=(startDate.year+1), month=1)
            else:
                startDate = startDate.replace(month=(startDate.month + 1))
            cycle += 1
        return cycle


import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

import numpy as np

from typing import Optional, Union, Dict, Tuple, Any

from .. import core, config, graph

logger = config.logger

colortype = Union[str,
                  Tuple[float, float, float],
                  Tuple[float, float, float, float]]

__all__ = ['plot1d']

def plot1d(x: 'core.NeuronObject',
           ax: Optional[mpl.axes.Axes] = None,
           color: Optional[Union['str',
                                 colortype,
                                 Dict[Any, colortype],
                                 ]
                           ] = None,
           **kwargs) -> mpl.axes.Axes:
    """Plot neuron topology in 1D according to Cuntz et al. (2010).

    This function breaks a neurons into segments between branch points.
    See Cuntz et al., PLoS Computational Biology (2010) for detailed
    explanation. For very complex neurons, this neuron "barcode" can get
    fairly complicated - make sure to zoom in.

    Parameters
    ----------
    x :         TreeNeuron | NeuronList
                Neuron(s) to plot.
    ax :        matplotlib.ax, optional
    cmap :      tuple | dict
                Color. If dict must map neuron UUID to color.
    **kwargs
                Will be passed to ``matplotlib.patches.Rectangle``.

    Returns
    -------
    matplotlib.ax

    Examples
    --------

    .. plot::
       :context: close-figs

        >>> import navis
        >>> import matplotlib.pyplot as plt
        >>> n = navis.example_neurons(2)
        >>> ax = navis.plot1d(n)
        >>> plt.show() # doctest: +SKIP

    Close figures (only relevant for doctests)

    >>> plt.close('all')

    See the :ref:`plotting tutorial <plot_intro>` for more examples.

    """
    if isinstance(x, core.NeuronList):
        if x.is_mixed:
            raise TypeError('NeuronList contains MeshNeuron(s). Unable to plot1d.')
    elif isinstance(x, core.TreeNeuron):
        x = core.NeuronList(x)
    else:
        raise TypeError(f'Unable plot1d data of type "{type(x)}"')

    if isinstance(color, type(None)):
        color = (0.56, 0.86, 0.34)

    if not ax:
        fig, ax = plt.subplots(figsize=(8, len(x) / 3))
        # Make background transparent (nicer for dark themes)
        fig.patch.set_alpha(0)
        ax.patch.set_alpha(0)

    # Add some default parameters for the plotting to kwargs
    kwargs.update({'lw': kwargs.get('lw', .1),
                   'ec': kwargs.get('ec', (1, 1, 1)),
                   })

    max_x = []
    for ix, n in enumerate(config.tqdm(x, desc='Processing',
                                       disable=config.pbar_hide,
                                       leave=config.pbar_leave)):
        if isinstance(color, dict):
            this_c = color[n.id]
        else:
            this_c = color

        # Get topological sort (root -> terminals)
        topology = graph.node_label_sorting(n)

        # Get terminals and branch points
        bp = n.nodes[n.nodes.type == 'branch'].node_id.values
        term = n.nodes[n.nodes.type == 'end'].node_id.values

        # Order this neuron's segments by topology
        breaks = [topology[0]] + \
            [n for i, n in enumerate(topology) if n in bp or n in term]
        segs = [([s for s in n.small_segments if s[0] == end][0][-1], end)
                for end in breaks[1:]]

        # Now get distances for each segment
        if 'nodes_geodesic_distance_matrix' in n.__dict__:
            # If available, use geodesic distance matrix
            dist_mat = n.nodes_geodesic_distance_matrix
        else:
            # If not, compute matrix for subset of nodes
            dist_mat = graph.geodesic_matrix(n, from_=breaks, directed=False)

        dist = np.array([dist_mat.loc[s[0], s[1]] for s in segs]) / 1000
        max_x.append(sum(dist))

        # Plot
        curr_dist = 0
        for k, d in enumerate(dist):
            if segs[k][1] in term:
                c = tuple(np.array(this_c) / 2)
            else:
                c = color

            p = mpatches.Rectangle((curr_dist, ix), d, 1, fc=c, **kwargs)
            ax.add_patch(p)
            curr_dist += d

    ax.set_xlim(0, max(max_x))
    ax.set_ylim(0, len(x))

    ax.set_yticks(np.array(range(0, len(x))) + .5)
    ax.set_yticklabels(x.name)

    ax.set_xlabel('distance [um]')

    ax.set_frame_on(False)

    return ax


import asyncio
import os
import mimetypes
import traceback
import sys
import urllib
from os.path import abspath, join, dirname
from functools import partial, lru_cache
from inspect import ismodule, isclass
from zest.core import is_async, Config, AsyncFile, get_param_length
from zest.httputil import (Request, Response, HTTPError,
                           JsonResponse, RedirectResponse)
from zest.helper import default_config
from zest.helper.util import get_logger, utc_time
from zest.helper.consts import ERROR_TEMPLETE, HTTP_STATUS
from zest.routing import Router

logger = get_logger('zest.web')

G_TEMPLATE_ENGINE = None  # global template engine.
G_TEMPLATE = {}           # global template variable or funtion
G_ROUTER = None           # global router

class App:

    """ The main entry of a Web app.

    Each App instance represents an WSGI application, which contains
    router, template, config, resources and so on.

    Parameters:
    - name: name of app, will have an affect at seraching the root path.
    - router: contaier of all the route.
    - config: config object, considered as a modules object. If not given,
        will initial from the default_config define at `helper.default_config`.
    - more_config: additional config. This will overwrite the previous config.

    NOTE:
        this application implement the WSGI inteface as a coroutine. When
        invoke this application  with other WSGI server, you must call with
        `yield from` syntax. For example:
            app = App()
            yield from app(environ, start_response)
    """

    default_template = 'mako'

    def __init__(self, name=None, router=None, config=None, **more_config):
        self.name = name
        self.router = router or Router()
        self.config = Config.from_module(config or default_config)
        self.config.update(**more_config)

        self.root_path = self.find_root_path(self.name)
        self.set_config('root_path', self.root_path)
        self.static_folder = self.get_config('static_folder', '').strip('/\\')
        self.static_path = abspath(join(self.root_path, self.static_folder))

        self.hooks = {}  # TODO
        self.error_handler = {}

        self.register_template(self.get_config('template_engine',
                                               self.default_template))
        global G_ROUTER
        G_ROUTER = self.router

    def find_root_path(self, name):
        """ Find the web application's root path from given module name.
        """
        if name:
            if name == '__main__':
                return os.getcwd()
            file = sys.modules[name].__file__
        else:
            import __main__ as x
            if ismodule(x) or isclass(x):
                if hasattr(x, '__module__'):
                    x = sys.modules[x.__module__]
                file = getattr(x, '__file__', None)

        return abspath(dirname(file))

    def register_template(self, template, **kwds):
        """ Register template engine.

        The default engine is 'mako'.
        You could easyily specify another engine just through inherit
        class `zest.template.Meta` or just provide a class object bring
        an 'render' method.
        """
        self.template_engine = None
        if isinstance(template, str):
            if template != 'mako':
                raise ValueError("Zest only support mako template engine now."
                                 "You could specify another engine through "
                                 "`zest.template` module or provide a class"
                                 "object bring an 'render' method.")
            from zest.template import Mako
            template = Mako
        assert hasattr(template, 'render'), "Invalid template engine."

        engine = template(**self.config)
        global G_TEMPLATE_ENGINE, G_TEMPLATE
        G_TEMPLATE_ENGINE = self.template_engine = engine

        G_TEMPLATE.update(**kwds)
        G_TEMPLATE['app'] = self
        G_TEMPLATE['config'] = self.config
        G_TEMPLATE['hooks'] = self.hooks

        return engine

    def add_template_global(self, name, value):
        """ Add a global template variable.

        Parameters:
        - name: variable name.
        - value: the object use in a template.
        """
        global G_TEMPLATE
        assert name not in G_TEMPLATE
        G_TEMPLATE[name] = value

    def global_template(self, name):
        """ Decorator to add template global variable.

        Usage:
            @global_template
            def template_func(*args, **kwds):
                do_something()

            ${template_func()}  # invoke at template.html
        """
        def deco(func):
            self.add_template_global(name, func)
            return func
        return deco

    @property
    def debug(self):
        return self.config['DEBUG']

    @debug.setter
    def debug(self, value):
        self.config['DEBUG'] = bool(value)

    @property
    def server_host(self):
        return self.config['SERVER_HOST']

    @property
    def server_port(self):
        return self.config['SERVER_PORT']

    def get_config(self, k, default=None):
        return self.config.get(k, default)

    def set_config(self, k, v, set_default=False):
        if set_default:
            self.config.setdefault(k, v)
        else:
            self.config[k] = v

    def trigger_hooks(self, name, *args, **kwds):
        """ Call a hook with given param
        """
        hook = self.hooks.get(name)
        if hook:
            return hook(*args, **kwds)

    def add_hook(self, name, func):
        self.hooks[name] = func
        return func

    def add_route(self, rule, controller, method='GET', priority=True):
        """ Bind a controller to it's corresponding route.

        Parameters:
        - rule: Regular expression or normal string. If rule contails a capture
            group, the capture group will pass to controller as a params tuple.
        - controller: function to handle request. This function must have at
            least one param and the first params will be pass on an `Request`
            instance, which represents the current request context.
        _ method: request method, default to be 'GET', could provide a method
            list to support multi method.
        - priority: the priority of rule. the rule have high priority will be
            match first. (default to be True)

        Equals to `router.add_route`
        """
        self.router.add_route(rule, controller, method, priority)

    def route(self, rule, method='GET', priority=True):
        """ Decorator to bind a controller to it's corresponding route.

        Usage:

            @app.route('/hello/(\w+)')
            def index(name):
                return 'Hello, %s' % name

        This will bind route '/hello/(\w+)' to `index` controller.
        """
        def deco(func):
            self.add_route(rule, func, method, priority)
            return func
        return deco

    def register_before_request(self, func):
        """ Register a function to called before we start to process request.
        """
        if get_param_length(func) > 1:
            raise ValueError("`before_request` should have at most one param.")

        self.add_hook('_before_request', func)

    def before_request(self, func):
        """ Decorator for register function to called before process request.

        Parameters:
        - func: function to be called. This function might have one or zero
            parameter. And once parameter is given, it will be pass on an
            `Request` instance, which represents the current request context.

        Usage:

            @app.before_request
            def abort_ip(request):
                if request.IP in FORBIDDEN:
                    abort(403)

            @app.before_request
            def db_init():
                init_the_db()

        The first will abort this request if the request iP is forbidden.
        The second will initalize database then continue to process request.
        """
        self.register_before_request(func)
        return func

    def handle_before_request(self, request):
        func = self.hooks.get('_before_request')
        if func:
            if get_param_length(func) == 1:
                func = partial(func, request)  # Pass current request context.
            return func()

    def register_after_request(self, func):
        """ Register a function to called after making a response.
        """
        self.add_hook('_after_request', func)

    def after_request(self, func):
        """ Decorator for register function to called before process request.

        Parameters:
        - func: function to be called after response. This function might have.
         one or zero parameter. And once param is given, it will be pass on an
            `Response` instance, which represents response for request.

        Usage:

            @app.after_request
            def abort_ip(response):
                response.set_header('Server', 'Zest')
                return response

        This will add a server header to all the response.
        """
        self.register_after_request(func)
        return func

    def handle_after_request(self, response):
        func = self.hooks.get('_after_request')
        if func:
            if get_param_length(func) == 1:
                rsp = func(response)
                if not rsp or not isinstance(rsp, Response):
                    raise ValueError('`after_request` must return a response.')
            else:
                rsp = func()
        else:
            rsp = response  # do nothing and return the same reponse.

        return rsp

    def add_error_handler(self, code, handler):
        """ Add a function to handle HTTP error.

        Parameters:
        - code: status of HTTP error
        - handler: function to handle error. This function might have one
            or no parameter. Once parameter is given, it will be pass on an
            `Request` instance, which represents the current request context.
        """
        self.error_handler[code] = handler

    def error(self, code):
        """ Decorator to add a error handler.

        Usage:

            @error(404)
            def handle_404():
                return render('404.html')
        """
        def deco(func):
            self.add_error_handler(code, func)
        return deco

    def merge_router(self, router):
        """ Merge routes with other router.

        Equals to `router.merge`
        """
        return self.router.merge(router)

    def get(self, rule, **kwds):
        """ Equals to `add_route(rule, method='GET', **kwds)`."""
        return self.router.get(rule, **kwds)

    def post(self, rule, **kwds):
        """ Equals to `add_route(rule, method='POST', **kwds)`."""
        return self.router.post(rule, **kwds)

    def delete(self, rule, **kwds):
        """ Equals to `add_route(rule, method='DELETE', **kwds)`."""
        return self.router.delete(rule, **kwds)

    def head(self, rule, **kwds):
        """ Equals to `add_route(rule, method='HEAD', **kwds)`."""
        return self.router.head(rule, **kwds)

    def log(self, msg):
        """ Logs messages from specify logger.
        """
        logger.info(msg)

    def log_exception(self, exc):
        """ Logs an exception.

        When debug model is set, log the traceback instead.
        """
        if self.debug:
            logger.error(traceback.format_exc())
        else:
            logger.error(exc)

    def add_static_handler(self, favicon_handler=None, static_handler=None):
        """ Add a default route to handle static file.
        """
        def _favicon_handler(request):
            return static_file('favicon.ico', self.root_path, request)

        def _static_handler(request, path):
            file_path, name = os.path.split(path)
            return static_file(name,
                               join(self.static_path, file_path.strip('/\\')),
                               request)

        self.add_route('/favicon\.ico', favicon_handler or _favicon_handler,
                       priority=False)
        self.add_route('/%s/(.*)' % self.static_folder,
                       static_handler or _static_handler, priority=False)

    def run(self, host=None, port=None, debug=False, **kwds):
        """ Runing server at specify address.

        Parameters:
        - host: server address to bind. (default to 127.0.0.1)
        - port: server port to bind. (default to 7676)
        - debug: develop and debugl model.(default to False)
        - autoreload: sets True to start `auto_reload` server.  # TODO
        - kwds: WSGI server setting. Go to `zest.server` for detail.
        """
        from zest.server import make_wsgi_server
        self.config.update_no_none(server_host=host, server_port=port,
                                   debug=bool(debug), **kwds)
        self.add_static_handler()

        host, port = self.server_host, self.server_port
        self.log('runing server at %s:%s' % (host, port))
        make_wsgi_server(app=self,
                         host=host,
                         port=port,
                         debug=self.debug,
                         **kwds)

    def match(self, request):
        """ Find a matching rule and return the corresponding controller.

        If find the matching rule but it has a bad request method, this
        will raise a 403 error. If not find, raise 404 error.
        """
        func, params = self.router.match_wsgi(request)
        if not func:
            if params == 403:  # match, but with no allow method
                raise HTTPError(403)
            else:              # can't find any rule match.
                raise HTTPError(404)
        else:
            if self.auto_unquote:
                try:
                    params = map(urllib.parse.unquote, params)
                except Exception as exc:
                    self.log_exception(exc)
            return partial(func, request, *params)

    def make_response(self, response, code=None):
        """ Create an response, return an Response instance.

        Parameter:
        - response: Response object or string or sequence.
        - code: response status code.
        """
        if response is None:
            raise ValueError("Can't response None.")

        rp = response
        if not isinstance(response, Response):
            body = headers = None
            code = code or 200
            if isinstance(response, (tuple, list)):
                body, code, headers = response + (None, ) * (3 - len(response))
            elif isinstance(response, str):
                body = response
            else:
                body = str(response)  # TODO
            rp = Response(body, headers, code)
        elif code is not None:
            rp.set_status(code)

        return rp

    @lru_cache(maxsize=100)
    def make_error_response(self, err, request=None):
        """ Create error response from HTTP error.
        """
        handler = self.error_handler.get(err.status)
        status = int(err.status)
        if not handler:
            reason = HTTP_STATUS.get(status)
            if not reason:
                status = 404  # reponse 404 while not reason find.
                reason = HTTP_STATUS[status]
            body = ERROR_TEMPLETE.format(status=status,
                                         reason=reason,
                                         message=err.message if self.debug
                                         else '')
            response = Response(body)
        else:
            if get_param_length(handler) == 1:
                response = handler(request)
            else:
                response = handler()

        return self.make_response(response, status)

    @asyncio.coroutine
    def response(self, environ, start_response):
        """ Respone from WSGI environ.
        """
        request = Request(environ)

        try:
            self.handle_before_request(request)

            rsp = self.match(request)()
            if is_async(rsp):
                rsp = yield from rsp
            response = self.make_response(rsp)
        except HTTPError as err:
            response = self.make_error_response(err, request)
        except Exception as exc:
            self.log_exception(exc)
            response = self.make_error_response(HTTPError(500))

        response = self.handle_after_request(response)
        return response.send(environ, start_response)

    def wsgi_app(self, environ, start_response):
        request = Request(environ)
        try:
            self.handle_before_request(request)
            rsp = self.match(request)()
            response = self.make_response(rsp)
        except HTTPError as err:
            response = self.make_error_response(err, request)
        except Exception as exc:
            self.log_exception(exc)
            response = self.make_error_response(HTTPError(500))

        response = self.handle_after_request(response)
        return response.send(environ, start_response)

    def __call__(self, environ, start_response):
        """ Implaments WSGI inteface for application.
        """
        return self.response(environ, start_response)

    def __getattr__(self, key):
        """ This method only gets called for attributes that don't exist.

        It's use to when some attributes not in the App instance. Just
        find from the config.
        """
        return self.config.get(key, None)

    def __repr__(self):
        return '<Application: %s>' % (self.name or '')

# Blow are some helpful tools

def static_file(name, path, request):
    """ Make a static file response.

    Parameters:
    - name: file name of static file.
    - path: absolute static directory path.
    - request: a `Request` object.

    ## This cool function is inspired by `bottle.static_file` then add a
    async read and write support
    """
    file_path = abspath(join(path, name.strip('/\\')))
    if not os.path.exists(file_path) or not os.path.isfile(file_path):
        raise HTTPError(404, 'File does not exist.')
    if not os.access(file_path, os.R_OK):
        raise HTTPError(403, 'You do not have permission to access this file.')

    headers = {}
    mimetype, encoding = mimetypes.guess_type(file_path)
    if mimetype:
        headers['Content-Type'] = mimetype
    else:
        headers['Content-Type'] = 'application/octet-stream'
        headers['Content-Disposition'] = 'attachment; filename="%s"' % name
    if encoding:
        headers['Content-Encoding'] = encoding

    stats = os.stat(file_path)
    headers['Content-Length'] = stats.st_size
    headers['Last-Modified'] = utc_time(stats.st_mtime)

    ims = request.if_modify_since
    if ims is not None and ims >= int(stats.st_mtime):
        response = Response(headers=headers, status=304)
    else:
        body = ''
        if request.method != 'HEAD':
            loop = request.event_loop
            with open(file_path, 'rb') as file:
                body = yield from AsyncFile(loop=loop, file=file).read()
        response = Response(body, headers)

    return response

def abort(status, message=''):
    """ Finish and response the request with specify status.

    Parameters:
    - status: integer number of response status.
    - message: string text of response body.
    """
    if not isinstance(status, int) or status not in HTTP_STATUS:
        raise ValueError("Invalid status code: %s." % status)
    raise HTTPError(status, message)

def redirect(path_or_url, status=303, response=None):
    """ Redict to specify url.

    Parameters:
    - path_or_url: redirect traget, could be a path or url, when it's a path,
         the redirect result will be: `request.url + path`.
    - status: response status. (default to be 303 For HTTP/1.1)
    - response: a `Response` object. (default to be None)
    """
    return RedirectResponse(path_or_url, status, response)

def to_json(*args, **kwds):
    """ Accept a key-value paris and convert to a Json response.

    Parameters:
    - args, kwds: the same with `dict` object.

    Usage:
        @app.get('/json')
        def json_controller(request):
            return to_json({'name': 'zest'})
    """
    return JsonResponse(*args, **kwds)

def render(name, **kwds):
    """ Render a template from template folder.

    Parameters:
    - name: template name to be rendered.
    - kwds: all the variable names (include the global custom variable)
        accessible to the template.
    """
    kwds.update(G_TEMPLATE)
    return Response(body=G_TEMPLATE_ENGINE.render(name, **kwds))

def url_for(url):
    pass

# TODO
def to_xml():
    pass

# TODO
def login_required():
    pass


#!/usr/bin/env python2.7

import json
import requests
from datetime import datetime
from pprint import pprint
from pymongo import MongoClient
from time import sleep

#Function that returns a list[last, time, market] of the last NUMBER of samples for MARKET where the first element in the list is the latest data point and the last element in the list is the oldest datapoint. All elements in list are unique
def printLastNumbers( market , number):
	myGraphList = []
	cursor = collections.find({"MarketName": market }).sort("TimeStamp", -1)
	for document in cursor:
		myGraphList.append([document['Last'],document['TimeStamp'],document['MarketName']])
		myGraphList = [list(t) for t in set(tuple(element) for element in myGraphList)]
		if (number == len(myGraphList)):
			myGraphList = sorted(myGraphList,key=lambda l:l[1], reverse=True)
			return myGraphList
	return myGraphList

#Function that will return a list of the different markets
def getMarkets():
	url = 'https://bittrex.com/api/v1.1/public/getmarketsummaries'
	myMarketList = []
	counter = 1
	response = requests.get(url)
	json_data = json.loads(response.text)
	responseList = json_data["result"]
	howLong =  len(responseList)
	counterI = 0
	while counterI < howLong - 1 :
		counterI = counterI + 1
		if responseList[counterI]["BaseVolume"] > 1000:
			myMarketList.append(responseList[counterI]['MarketName'])

	return myMarketList

#Input printLastNumbers list and returns new list with timestamp converted datetime format
def parseTimeStamps(myList):
	for x in myList:
		format = '%Y-%m-%d %H:%M:%S'
		x[1] = str(x[1]).replace("T", " ")
		x[1] = str(x[1]).split(".", 1)[0]
		x[1] = datetime.strptime(x[1], format)

	return myList

#returns list of last price and time in seconds normalized to 0   where 0 is the lastest  time sample available
def normalizeTimeStamps(myList):
	normalizedList = []
	howLong = len(myList)
	counterI = 0
	while counterI < howLong  :
		normalizedList.append([myList[counterI][0], (myList[0][1] - myList[counterI][1]).total_seconds(), myList[0][2]])
		counterI = counterI + 1

	return normalizedList

#Establishing connection to MongoDB database
client = MongoClient(host=['192.168.1.198:27017'])
#client = MongoClient(host=['108.196.157.14:27017'])

db = client['bittrex']

collections = db['marketSummaries']
# Connection Established

#printLastNumbers( "BTC-LSK" , 50)
#pprint(getMarkets())
#print getMarkets()
##########masterList = normalizeTimeStamps(parseTimeStamps(printLastNumbers("BTC-ARK", 600)))
#masterList = parseTimeStamps(printLastNumbers("BTC-INCNT", 250))
#############print str((  masterList[0][0] - masterList[len(masterList)-1][0]  ) / (  masterList[len(masterList)-1][1] ) ) + "_______" + masterList[0][2]
#print (  masterList[0][0] - masterList[len(masterList)-1][0]  ) / (  masterList[len(masterList)-1][1] )
#pprint(masterList)
#for x in masterList:
#	print str(x[0]) + "-----------" + str(x[1])
#print masterList[0][1] - masterList[3][1]

#pprint(printLastNumbers("BTC-OMG", 250))
displayList = []
loopList = range(500)
loopList.pop(0)
loopList.pop(0)
#loopList.pop(0)
#print loopList
#for x in getMarkets():
lenOfSamples = 10
counter = 0
masterList = normalizeTimeStamps(parseTimeStamps(printLastNumbers("BTC-ADX", lenOfSamples)))
	#pprint(masterList)
while counter < lenOfSamples - 1:
	displayList.append([ ((masterList[counter][0] - masterList[counter + 1][0]) /  masterList[counter][0] ) * 100,  (masterList[counter + 1][1] - masterList[counter ][1]),  ((masterList[0][0] - masterList[counter + 1][0]) /  masterList[0][0] ) * 100, masterList[counter + 1][1], masterList[counter][2]])
	counter = counter + 1
	#print str((  masterList[0][0] - masterList[len(masterList)-1][0]  ) / (  masterList[len(masterList)-1][1] ) ) + "_______" + masterList[0][2] + "______________" + str(masterList[len(masterList)-1][1])
	#displayList.append([(  masterList[0][0] - masterList[len(masterList)-1][0]  ) / (  masterList[len(masterList)-1][1] ), masterList[0][2], masterList[0][0] - masterList[len(masterList)-1][0] , masterList[len(masterList)-1][1] , ((masterList[0][0] - masterList[len(masterList)-1][0]) / masterList[0][0]) * 100  ] )
	#displayList.append([ masterList[0][2],  (masterList[len(masterList)-1][1])/60 , ((masterList[0][0] - masterList[len(masterList)-1][0]) / masterList[0][0]) * 100  ] )

#displayList = sorted(displayList,key=lambda l:l[4], reverse=True)
displayList = sorted(displayList,key=lambda l:l[3], reverse=False)
pprint(displayList)
	#pprint(printLastNumbers( x , 100))
	#print "______________________________________________________________________________________________________________________________________________________"
	#print "______________________________________________________________________________________________________________________________________________________"
	#print "______________________________________________________________________________________________________________________________________________________"


import requests
from flask import Request

import sni_processor

def webhook(request: Request):
    request_headers = request.headers
    body = request.get_data(as_text=True)
    forward_url = request_headers['FORWARD_URL']
    forward_fqdn = request_headers['FORWARD_FQDN']
    forward_headers = {
        'content-type': request_headers['content-type'],
        'Authorization': request_headers['FORWARD_Authorization']
    }
    session = requests.Session()
    session.mount("https://", sni_processor.HostHeaderSSLAdapter(forward_fqdn))
    return session.post(forward_url, body, headers=forward_headers).text


#!/usr/bin/env python3

import json
import sys
import os

(_, fn) = sys.argv

## Coq can produce very deep JSON data structures..
sys.setrecursionlimit(10000)

## XXX hack for now
import_prefix = 'codegen/'

extra_mods = {
  'FS':           ['Nat',],
  'Compare_dec':  ['Nat',],
}

extra_imports = {
  'Word':         ['math/big',],
}

remap = {
  'Word': {
    'wrap': '''
      func wrap(x *big.Int, nbits uint) *big.Int {
        one := big.NewInt(1)
        mask := big.NewInt(0)
        mask.Lsh(one, nbits)
        mask.Sub(mask, one)

        r := big.NewInt(0)
        return r.And(x, mask)
      }''',

    'split1': '''
      var Coq_split1 CoqT = func(sz1 CoqT) CoqT {
        return func(sz2 CoqT) CoqT {
          return func(x CoqT) CoqT {
            xb := x.(*big.Int)
            return wrap(xb, Nat.Nat2uint(sz1))
          }
        }
      }''',

    'split2': '''
      var Coq_split2 CoqT = func(sz1 CoqT) CoqT {
        return func(sz2 CoqT) CoqT {
          return func(x CoqT) CoqT {
            xb := x.(*big.Int)

            r := big.NewInt(0)
            return r.Rsh(xb, Nat.Nat2uint(sz1))
          }
        }
      }''',

    'combine': '''
      var Coq_combine CoqT = func(sz1 CoqT) CoqT {
        return func(x CoqT) CoqT {
          return func(sz2 CoqT) CoqT {
            return func(y CoqT) CoqT {
              xb := x.(*big.Int)
              yb := y.(*big.Int)

              yshifted := big.NewInt(0)
              yshifted.Lsh(yb, Nat.Nat2uint(sz1))

              r := big.NewInt(0)
              return r.Or(xb, yshifted)
            }
          }
        }
      }''',

    'wordToNat': '''
      var Coq_wordToNat CoqT = func(sz CoqT) CoqT {
        return func(w CoqT) CoqT {
          b := w.(*big.Int)
          return Nat.Uint2nat(uint(b.Uint64()))
        }
      }''',

    'natToWord': '''
      var Coq_natToWord CoqT = func(sz CoqT) CoqT {
        return func(n CoqT) CoqT {
          i := Nat.Nat2uint(n)
          r := big.NewInt(int64(i))
          return wrap(r, Nat.Nat2uint(sz))
        }
      }''',

    'wplus': '''
      var Coq_wplus CoqT = func(sz CoqT) CoqT {
        return func(x CoqT) CoqT {
          return func(y CoqT) CoqT {
            xb := x.(*big.Int)
            yb := y.(*big.Int)
            r := big.NewInt(0)
            r.Add(xb, yb)
            return wrap(r, Nat.Nat2uint(sz))
          }
        }
      }''',

    'wminus': '''
      var Coq_wminus CoqT = func(sz CoqT) CoqT {
        return func(x CoqT) CoqT {
          return func(y CoqT) CoqT {
            xb := x.(*big.Int)
            yb := y.(*big.Int)

            nbits := Nat.Nat2uint(sz)

            r := big.NewInt(1)
            r.Lsh(r, nbits)
            r.Add(r, xb)
            r.Sub(r, yb)

            return wrap(r, nbits)
          }
        }
      }''',

    'wmult': '''
      var Coq_wmult CoqT = func(sz CoqT) CoqT {
        return func(x CoqT) CoqT {
          return func(y CoqT) CoqT {
            xb := x.(*big.Int)
            yb := y.(*big.Int)
            r := big.NewInt(0)
            r.Mul(xb, yb)
            return wrap(r, Nat.Nat2uint(sz))
          }
        }
      }''',

    'wdiv': '''
      var Coq_wdiv CoqT = func(sz CoqT) CoqT {
        return func(x CoqT) CoqT {
          return func(y CoqT) CoqT {
            xb := x.(*big.Int)
            yb := y.(*big.Int)
            r := big.NewInt(0)
            return r.Div(xb, yb)
          }
        }
      }''',

    'wmod': '''
      var Coq_wmod CoqT = func(sz CoqT) CoqT {
        return func(x CoqT) CoqT {
          return func(y CoqT) CoqT {
            xb := x.(*big.Int)
            yb := y.(*big.Int)
            r := big.NewInt(0)
            return r.Mod(xb, yb)
          }
        }
      }''',

    'wlt_dec': '''
      var Coq_wlt_dec CoqT = func(sz CoqT) CoqT {
        return func(x CoqT) CoqT {
          return func(y CoqT) CoqT {
            xb := x.(*big.Int)
            yb := y.(*big.Int)
            r := xb.Cmp(yb)
            if r < 0 {
              return &Specif.Coq_Coq_left{}
            } else {
              return &Specif.Coq_Coq_right{}
            }
          }
        }
      }''',

    'weq': '''
      var Coq_weq CoqT = func(sz CoqT) CoqT {
        return func(x CoqT) CoqT {
          return func(y CoqT) CoqT {
            xb := x.(*big.Int)
            yb := y.(*big.Int)
            r := xb.Cmp(yb)
            if r == 0 {
              return &Specif.Coq_Coq_left{}
            } else {
              return &Specif.Coq_Coq_right{}
            }
          }
        }
      }''',
  },

  'FS': {
    'cachesize': 'var Coq_cachesize CoqT = Nat.Uint2nat(100)',
  },

  'Compare_dec': {
    'lt_dec': '''
      var Coq_lt_dec CoqT = func(a CoqT) CoqT {
        return func(b CoqT) CoqT {
          ai := Nat.Nat2uint(a)
          bi := Nat.Nat2uint(b)
          if ai < bi {
            return &Specif.Coq_Coq_left{}
          } else {
            return &Specif.Coq_Coq_right{}
          }
        }
      }''',
  },

  'Nat': {
    'uint2nat': '''
      func Uint2nat(n uint) CoqT {
        var res CoqT = &Datatypes.Coq_O{}
        for (n > 0) {
          res = &Datatypes.Coq_S{res}
          n -= 1
        }
        return res
      }''',

    'nat2uint': '''
      func Nat2uint(n CoqT) uint {
        var sum uint = 0
        for {
          switch t := n.(type) {
          case *Datatypes.Coq_O:
            return sum
          case *Datatypes.Coq_S:
            sum += 1
            n = t.A0
          default:
            panic("unknown nat constructor")
            return 0
          }
        }
      }''',

    'add': '''
      var Coq_add CoqT = func(Coq_n CoqT) CoqT {
        return func(Coq_m CoqT) CoqT {
          n := Nat2uint(Coq_n)
          m := Nat2uint(Coq_m)
          return Uint2nat(n + m)
        }
      }''',

    'mul': '''
      var Coq_mul CoqT = func(Coq_n CoqT) CoqT {
        return func(Coq_m CoqT) CoqT {
          n := Nat2uint(Coq_n)
          m := Nat2uint(Coq_m)
          return Uint2nat(n * m)
        }
      }''',
  },
}

this_pkgname = None

with open(fn) as f:
  s = f.read()
  d = json.loads(s)

def coqname_in_mod(s):
  return 'Coq_' + s.replace("'", "_prime").replace(".", "_")

def coqname(s):
  global this_pkgname
  if '.' in s:
    (mod, rest) = s.split('.', 1)
    if mod == this_pkgname:
      return coqname_in_mod(rest)
    else:
      return '%s.%s' % (mod, coqname_in_mod(rest))
  else:
    return coqname_in_mod(s)

varname_ctr = 0
def varname():
  global varname_ctr
  varname_ctr += 1
  return "__v_%d" % varname_ctr

def gen_fix_expr(names, lambdas):
  s = []
  s.append('func (__fixarg CoqT) CoqT {')

  for name in names:
    s.append('  var %s CoqT' % coqname(name))

  for (name, lambd) in zip(names, lambdas):
    lvar = gen_expr_assign(lambd, s)
    s.append('  %s = %s' % (coqname(name), lvar))

  s.append('  return CoqApply(%s, __fixarg)' % coqname(names[-1]))
  s.append('}')
  return '\n'.join(s)

def gen_expr_assign(e, s):
  r = gen_expr_assign_real(e, s)
  if len(r) < 1000:
    return r
  else:
    v = varname()
    s.append('  var %s CoqT = %s' % (v, r))
    return v

def gen_expr_assign_real(e, s):
  if e['what'] == 'expr:lambda':
    res = []

    first = True
    for argname in e['argnames']:
      if first:
        res.append('func (%s CoqT) CoqT {' % coqname(argname))
      else:
        res.append('return func (%s CoqT) CoqT {' % coqname(argname))
      first = False

    if first:
      ## This is a lambda but there are no arguments..  They were
      ## probably [Prop]s that got eliminated at extraction time.
      retvar = gen_expr_assign(e['body'], s)
      return retvar
    else:
      retvar = gen_expr_assign(e['body'], res)
      res.append('return %s' % retvar)

    for argname in e['argnames']:
      res.append('}')

    return '\n'.join(res)

  elif e['what'] == 'expr:fix':
    fixnames = [x['name'] for x in e['funcs']]
    fixlambdas = [x['body'] for x in e['funcs']]
    return gen_fix_expr(fixnames, fixlambdas)

  elif e['what'] == 'expr:case':
    resvar = varname()
    s.append('var %s CoqT' % resvar)

    switchvar = gen_expr_assign(e['expr'], s)
    s.append('switch __typesw := (%s).(type) {' % switchvar)

    have_default = False
    for case in e['cases']:
      pat = case['pat']

      if pat['what'] == 'pat:constructor':
        s.append('case *%s:' % coqname(pat['name']))
        for idx, argname in enumerate(pat['argnames']):
          if argname == '_':
            continue
          s.append('  var %s CoqT = __typesw.A%d' % (coqname(argname), idx))
          s.append('  var _ = %s' % coqname(argname))
        body = gen_expr_assign(case['body'], s)
        s.append('  %s = %s' % (resvar, body))

      elif pat['what'] == 'pat:wild':
        s.append('default:')
        s.append('  var _ = __typesw')
        body = gen_expr_assign(case['body'], s)
        s.append('  %s = %s' % (resvar, body))
        have_default = True

      elif pat['what'] == 'pat:rel':
        s.append('default:')
        s.append('  var _ = __typesw')
        s.append('  var %s CoqT = %s\n' % (coqname(pat['name']), switchvar))
        body = gen_expr_assign(case['body'], s)
        s.append('  %s = %s' % (resvar, body))
        have_default = True

      else:
        s.append('UNKNOWN PAT %s' % pat['what'])

    if not have_default:
      s.append('default:')
      s.append('  var _ = __typesw')
      s.append('  %s = nil\n' % resvar)
      s.append('  panic("no matching switch type")')

    s.append('}')
    return resvar

  elif e['what'] == 'expr:rel':
    return coqname(e['name'])

  elif e['what'] == 'expr:global':
    return coqname(e['name'])

  elif e['what'] == 'expr:constructor':
    arg_vars = []
    for a in e['args']:
      argvar = gen_expr_assign(a, s)
      arg_vars.append(argvar)

    return '&%s{ %s }' % (coqname(e['name']), ', '.join(arg_vars))

  elif e['what'] == 'expr:exception':
    s.append('panic("%s")' % e['msg'])
    return 'nil'

  elif e['what'] == 'expr:apply':
    funvar = gen_expr_assign(e['func'], s)

    arg_vars = []
    for a in e['args']:
      argvar = gen_expr_assign(a, s)
      arg_vars.append(argvar)

    apply_expr = funvar
    for arg in arg_vars:
      apply_expr = 'CoqApply(%s, %s)' % (apply_expr, arg)

    ## Save the result in a temporary variable, to avoid re-computing
    res = varname()
    s.append('var %s CoqT = %s' % (res, apply_expr))
    return res

  elif e['what'] == 'expr:dummy':
    return 'CoqDummy'

  elif e['what'] == 'expr:let':
    v = gen_expr_assign(e['nameval'], s)
    res = varname()
    s.append('var %s CoqT' % res)
    s.append('{')
    s.append('var %s CoqT = %s' % (coqname(e['name']), v))
    s.append('%s = %s' % (res, gen_expr_assign(e['body'], s)))
    s.append('}')
    return res

  elif e['what'] == 'expr:axiom':
    s.append('panic("Axiom not realized")')
    return 'nil'

  elif e['what'] == 'expr:coerce':
    return gen_expr_assign(e['value'], s)

  else:
    s.append('UNKNOWN EXPR %s' % e['what'])
    return 'nil'

def gen_header(d):
  global this_pkgname
  this_pkgname = d['name']

  usedmods = d['used_modules'] + extra_mods.get(this_pkgname, [])

  s = []
  s.append('package %s' % d['name'])
  s.append('import . "gocoq"')
  for modname in usedmods:
    s.append('import "%s%s"' % (import_prefix, modname))
  for modname in extra_imports.get(this_pkgname, []):
    s.append('import "%s"' % modname)
  s.append('var Coq2go_unused bool = true &&')
  for modname in usedmods:
    s.append('  %s.Coq2go_unused &&' % modname)
  s.append('  true')
  s.append('')
  return s

def gen_ind(dec):
  s = []
  for c in dec['constructors']:
    s.append('type %s struct {' % coqname(c['name']))
    for idx, typ in enumerate(c['argtypes']):
      s.append('  A%d CoqT' % idx)
    s.append('}')
  return s

def gen_term(dec):
  if this_pkgname in remap and dec['name'] in remap[this_pkgname]:
    return []

  s = []
  s.append('func () CoqT {')
  v = gen_expr_assign(dec['value'], s)
  s.append('  return %s' % v)
  s.append('} ()')

  return ['var %s CoqT = %s\n' % (coqname(dec['name']), '\n'.join(s))]

def gen_fix(dec):
  ## For a group of N mutually-recursive fixpoints, we generate N
  ## copies of each of the N functions.  This is because Go prohibits
  ## loops during global variable initialization.
  r = []
  names = [x['name'] for x in dec['fixlist']]
  values = [x['value'] for x in dec['fixlist']]
  for i in range(0, len(dec['fixlist'])):
    rot_names = names[i:] + names[:i]
    rot_values = values[i:] + values[:i]
    if this_pkgname in remap and rot_names[-1] in remap[this_pkgname]:
      continue

    e = gen_fix_expr(rot_names, rot_values)
    r.append('var %s CoqT = %s' % (coqname(rot_names[-1]), e))
  return r

def print_lines(lines):
  for l in lines:
    print(l)

print_lines(gen_header(d))

for dec in d['declarations']:
  if dec['what'] == 'decl:type':
    pass
  elif dec['what'] == 'decl:term':
    print_lines(gen_term(dec))
  elif dec['what'] == 'decl:fixgroup':
    print_lines(gen_fix(dec))
  elif dec['what'] == 'decl:ind':
    print_lines(gen_ind(dec))
  else:
    assert False, dec

if this_pkgname in remap:
  for (_, defn) in remap[this_pkgname].items():
    print(defn)


import os
import json
import sys
import pandas as pd
import numpy as np
from datetime import datetime
from sklearn.model_selection import train_test_split, StratifiedKFold
from sklearn.metrics import roc_auc_score
import lightgbm as lgb
from .utils import top_ratio_hit_rate, eval_aucpr,reduce_mem_usage,eval_gini_normalization
import logging
import warnings
from hyperopt import fmin, tpe, hp, partial,Trials
warnings.filterwarnings('ignore')

#数据准备

class lgb_optimizer():

    def __init__(self, train_x,train_y,test_x,test_y, default_params, metric='1/1000_hit',logger=None,cat_features=None):
        train_x, val_x, train_y, val_y = train_test_split(train_x, train_y, stratify=train_y, test_size=0.1,random_state=42)
        self.train_x = train_x
        self.train_y = train_y
        self.val_x = val_x
        self.val_y = val_y
        self.test_x= test_x
        self.test_y = test_y
        self.metric=metric
        self.logger = logger
        self.cat_features = cat_features
        self.obj = 0
        self.best_param={}
        self.best_evals={}
        self.default_params = default_params
        self.default_params.update({"verbose": -1})
        self.space = {"learning_rate":hp.uniform('learning_rate', 5e-3, 2e-1),
                     "max_depth": hp.choice('max_depth',np.arange(3,8).tolist()),  #(min,max,q) = round(uniform(min,max)/q)*q
                     "num_leaves": hp.choice('num_leaves',np.arange(4,60).tolist()), #
                     "min_data_in_leaf": hp.choice('min_data_in_leaf', np.arange(16,100).tolist()),
                     "bagging_fraction": hp.uniform('bagging_fraction', 0.5,1.0),
                     "feature_fraction": hp.uniform('feature_fraction',0.5,1.0),
                     "min_gain_to_split": hp.uniform('min_gain_to_split', 0,6),
                     "min_data_in_bin": hp.choice('min_data_in_bin',np.arange(10,80,2).tolist()), #
                     "lambda_l2": hp.uniform('lambda_l2',0.1, 6),
                     "lambda_l1": hp.uniform('lambda_l1',0,5),
                     # "max_cat_threshold": hp.choice('max_cat_threshold',[16,24,32]),
                     }

    def lightgbm_factory(self,args):
        params = self.default_params.copy()
        args['num_leaves'] = min(args['num_leaves'], 2**(args['max_depth']-1))
        params.update(args)
        dtrain = lgb.Dataset(self.train_x, label = self.train_y, feature_name='auto',
                        categorical_feature = self.cat_features, weight=None,free_raw_data=False)
        dval = lgb.Dataset(self.val_x, label =self.val_y,feature_name='auto', categorical_feature = self.cat_features, weight=None,
                        reference=dtrain,free_raw_data=False)

        evalists =[dtrain,dval]
        evalnames=['train','val']
        eval_result={}

        num_boost_round = params.get('num_boost_round',500)
        early_stopping_round = params.get('early_stopping_round',40)
        model = lgb.train(params, dtrain, num_boost_round, evalists, evalnames,
                          early_stopping_rounds=early_stopping_round,verbose_eval=40,
                          evals_result=eval_result)
        y_prob = model.predict(self.test_x, num_iteration = model.best_iteration)
        evals={}
        evals['rate1'], _,_, evals['cover'] = top_ratio_hit_rate(self.test_y.values, y_prob, top_ratio=0.001)
        evals['rate5'], _,_,_ = top_ratio_hit_rate(self.test_y.values, y_prob, top_ratio=0.005)
        evals['aucpr'] = eval_aucpr(self.test_y.values, y_prob)
        evals['gini'] = eval_gini_normalization(self.test_y.values, y_prob)
        train_loss = eval_result['train']['binary_logloss'][-1]
        val_loss = eval_result['val']['binary_logloss'][-1]
        metric={'1/1000_hit':evals['rate1'], '5/1000_hit':evals['rate5'],
                'aucpr':evals['aucpr'],'gini':evals['gini'],'30/100_cover':evals['cover']}
        target =metric[self.metric]

        if target> self.obj and abs(train_loss - val_loss)<0.02:
            self.obj = target
            params.pop("categorical_column",None)
            self.best_param.update(params)
            self.best_evals.update(evals)
            self.logger.info((' hit_rate = {:.4f}'.format(evals['rate1'])))
            self.logger.info(('hit_rate5 = {:.4f}'.format(evals['rate5'])))
            self.logger.info(('   auc_pr = {:.4f}'.format(evals['aucpr'])))
            self.logger.info(('     gini = {:.4f}'.format(evals['gini'])))
            self.logger.info(self.best_param)
        return -target

    def run(self,max_evals=100):
        trials = Trials()
        algo = partial(tpe.suggest, n_startup_jobs=1)
        best = fmin(self.lightgbm_factory, self.space, max_evals=max_evals, algo=tpe.suggest, trials = trials)
        self.logger.info(datetime.now().strftime('%Y-%m-%d %H:%M:%S').center(50,'-'))
        self.logger.info(json.dumps(self.best_param,indent=2))
        print(json.dumps(self.best_param,indent=2))
        print(json.dumps(self.best_evals,indent=2))
        return trials
        # logger.debug(json.dumps(self.best_param,indent=2))

# print(trials.trials)
# print(trials.results)
# print(trials.losses() )
# print(trials.statuses() )


import requests, json
import pandas as pd
import matplotlib.pyplot as plt

test_raw_data = pd.read_csv("info/test.csv",index_col=0)
input_data = test_raw_data.iloc[:,0:6]
Target_data = test_raw_data.iloc[:,6]

headers = {'Content-Type': "application/json"}
URL ='http://127.0.0.1:5000/testing'  ## 발급받은 API 키값
x=0
x_init = [0]
y_phy_init = [40]
y_vir_init = [40]
resi_val_init = [0]
threshold_val_init = [0.65]
for i in range(0,len(test_raw_data)):
    list_data = input_data.iloc[i,:]
    list_data = list(list_data)

    TG_data = Target_data[i]

    A = list_data[0]
    B = list_data[1]
    C = list_data[2]
    D = list_data[3]
    E = list_data[4]
    F = list_data[5]
    T = TG_data # 물리센서 값
    data = {"Testing input_1": A,"Testing input_2": B,"Testing input_3": C,"Testing input_4": D,"Testing input_5": E,"Testing input_6": F,"Testing input_7": T}
    res = requests.post(URL, headers=headers, data=json.dumps(data))

    # 실시간 그래프 제작
    xml_dic = res.json()
    all_key_list = list(xml_dic.values())
    # print("리스트가 어떻게되나요!",all_key_list)

    # 갱신 data
    x = x + 1
    y_phy = T
    threshold_val = all_key_list[2] # 임계치 값
    resi_val = all_key_list[0] # 잔차 값
    y_vir = all_key_list[3] # 가상센서 값

    x_init.append(x)
    y_phy_init.append(y_phy)
    y_vir_init.append(y_vir)
    resi_val_init.append(resi_val)
    threshold_val_init.append(threshold_val)

    plt.subplot(2, 1, 1)
    plt.plot(y_phy_init,color='navy',label="Physical sensor value")
    plt.plot(y_vir_init,color='darkred',label="Virtual sensor value")
    if x >= 1:
        plt.xlim(x-5, x+30)

    while i ==0:
        plt.legend(loc='upper right', ncol=2)
        plt.ylim(35, 50)
        break

    plt.subplot(2, 1, 2)
    plt.plot(resi_val_init,color='black',label="Residual value")
    plt.plot(threshold_val_init,color='red',label="Threshold value")

    if x >= 1:
        plt.xlim(x-5, x+30)

    while i ==0:
        plt.legend(loc='upper right')
        plt.ylim(0, 2)
        break

    # print(y_phy_init)
    # print(y_vir_init)
    plt.pause(0.05)
    # plt.show()
    print(xml_dic)

plt.show()