'''
Create a 1D/2D grid object as a connected network.
'Mask' marks any obstacles on the grid.
It is an array of the same size as the grid, where a location is True if clear and False is blocked.
'Wrap' marks whether the grid is wrapped in a direction so the sensor can move directly from one end to the other.
Internally always uses index (ind) instead of coordinate (loc).
'''

import numpy as np
from scipy.linalg import qr, svd
from collections import deque

class Grid():
    dest = False

    def bfs(self, src_ind, max_dist):
        '''Use Breadth-first search to get the shortest path from x to all nodes within a max distance.'''
        queue = deque()
        visited = [False for ind in range(self.size)]
        dist = [10**9 for ind in range(self.size)]
        visited[src_ind] = True
        dist[src_ind] = 0
        queue.append(src_ind)

        while len(queue) != 0:
            ind = queue.popleft()
            if dist[ind] >= max_dist:
                continue

            for i in range(len(self.adj[ind])):
                temp = self.adj[ind][i] # adjacent node index
                if not visited[temp]:
                    visited[temp] = True
                    dist[temp] = dist[ind] + 1
                    queue.append(temp)

        return dist

    def set_destination(self, inds, max_dist=None):
        '''Set the ending sensor positions, construct shortest path lists for sensors.
           Max distance is the time step to reach the ending position.
           If the ending position is the same at where it starts, it is a cycle.
           Max distance is half of the cycle period.
        '''
        # self.start = inds
        self.dist_to_dest = np.zeros((len(inds), self.size))
        self.dest = True

        for i in range(len(inds)):
            self.dist_to_dest[i] = self.bfs(inds[i], max_dist)

    def reset_destination(self):
        self.dist_to_dest = None
        self.dest = False

    def set_radius(self, r):
        self.adj = dict()
        for ind in range(self.size):
            self.adj[ind] = self.get_adj(ind)

    # def set_start(self, inds, max_dist=None):
    #     '''Set the ending sensor positions, construct shortest path lists for sensors.
    #        Max distance is the time step to reach the ending position.
    #        If the ending position is the same at where it starts, it is a cycle.
    #        Max distance is half of the cycle period.
    #     '''
    #     # self.start = inds
    #     self.dist_from_start = np.zeros((len(inds), self.size))
    #     self.cycle = True

    #     for i in range(len(inds)):
    #         self.dist_from_start[i] = self.bfs(inds[i], max_dist)

    def get_neighbors(self, inds, nbg_dist_to_start=None):
        if nbg_dist_to_start is None:
            nbg_dist_to_start = 1e9
        neighbors = set()
        neighbor_dict = dict()
        for i in range(len(inds)):
            if self.dest:
                ns_ind = [j for j in self.adj[inds[i]] if self.dist_to_dest[i, j] <= nbg_dist_to_start]
            else:
                ns_ind = self.adj[inds[i]]

            if len(ns_ind) == 0:
                raise ValueError("No valid neighbor is found for sensor {}.".format(i+1))

            neighbors.update(ns_ind)
            for n in ns_ind:
                if n in neighbor_dict:
                    neighbor_dict[n].update({inds[i]})
                else:
                    neighbor_dict[n] = {inds[i]}

        return list(neighbors), neighbor_dict

    def get_adj(self, ind):
        pass

class Grid1D(Grid):
    def __init__(self, length, r=None, mask=None, wrap=False):
        '''Initialize. Build an adjacency dictionary. '''
        if mask is not None:
            assert length == mask.size
        self.length = length
        self.mask = mask
        self.wrap = wrap
        self.size = length if mask is None else np.sum(mask)
        self.r = r
        if r is not None:
            self.set_radius(r)

    def ind_to_loc(self, ind):
        if self.mask is not None:
            x = np.where(self.mask==True)[0]
            return x[ind]
        else:
            return ind

    def loc_to_ind(self, loc):
        if self.mask is not None:
            return np.sum(self.mask[:loc])
        else:
            return loc

    def get_adj(self, ind):
        x = self.ind_to_loc(ind)
        x_min = x-self.r if self.wrap else max(0,x-self.r)
        x_max = x+self.r if self.wrap else min(self.length-1,x+self.r)

        neighbors = [(i % self.length) for i in range(x_min,x_max+1)]
        if self.mask is not None:
            neighbors = [self.loc_to_ind(loc) for loc in neighbors if self.mask[loc]]
        return neighbors

class Grid2D(Grid):
    '''
    Indexing the grid from the top left corner.
    For example, a grid with width 3 and height 2 is
        1,2,3,
        4,5,6.

    '''
    def __init__(self, width, height, r=None, mask=None,
                 wrap_x=False, wrap_y=False, dist_type='euclidean'):
        if mask is not None:
            assert width * height == mask.size
        self.width = width # x
        self.height = height # y
        self.mask = mask
        self.wrap_x = wrap_x
        self.wrap_y = wrap_y
        self.dist_type = dist_type
        self.size = width * height if mask is None else int(np.sum(mask))
        self.r = r
        if r is not None:
            self.set_radius(r)
        else:
            self.adj = dict()
            for ind in range(self.size):
                self.adj[ind] = list(range(self.size))

    def ind_to_loc(self, ind):
        if self.mask is not None:
            y, x = np.where(self.mask==True)
            return (x[ind], y[ind])
        else:
            return (ind % self.width, ind // self.width)

    def loc_to_ind(self, loc):
        x = loc[0] % self.width
        y = loc[1] % self.height
        val = x + y * self.width
        if self.mask is not None:
            if self.mask[y,x]:
                return int(np.sum(self.mask.flatten()[:val]))
            else:
                return None
        else:
            return val

    def dist(self, loc1, loc2):
        if self.dist_type=='euclidean':
            dx = abs(loc1[0]-loc2[0])
            if self.wrap_x:
                dx = min(dx, self.width-dx)
            dy = abs(loc1[1]-loc2[1])
            if self.wrap_y:
                dy = min(dy, self.height-dy)
            return np.sqrt(dx**2+dy**2)
        elif self.dist_type=='sphere':
            lat1 = (90-loc1[1]) * np.pi / 180
            lat2 = (90-loc2[1]) * np.pi / 180
            dx = abs(loc1[0]-loc2[0])
            dx = min(dx, self.width-dx)
            dlong = dx * np.pi / 180
            R = 6371
            d = np.arccos(np.sin(lat1) * np.sin(lat2) + np.cos(lat1) * np.cos(lat2) * np.cos(dlong)) * R
            return d

        return None

    def has_obs(self, loc1, loc2):
        if self.mask is None:
            return False

        x_range = np.sort([loc1[0], loc2[0]])
        y_range = np.sort([loc1[1], loc2[1]])
        dist_x = x_range[1]-x_range[0]
        dist_y = y_range[1]-y_range[0]
        if self.wrap_x and dist_x > (self.width-dist_x):
            x_range = np.array([x_range[1],x_range[0]+self.width])
            dist_x = x_range[1]-x_range[0]
        if self.wrap_y and dist_y > (self.height-dist_y):
            y_range = np.array([y_range[1],y_range[0]+self.height])
            dist_y = y_range[1]-y_range[0]
        if dist_x > dist_y:
            for i in range(dist_x):
                x = int(x_range[0] + i + 1)
                y = int(y_range[0] + np.round((i+1)/dist_x*dist_y))
                if not self.mask[y%self.height, x%self.width]:
                    return True
        else:
            for i in range(dist_y):
                y = int(y_range[0] + i + 1)
                x = int(x_range[0] + np.round((i+1)/dist_y*dist_x))
                if not self.mask[y%self.height, x%self.width]:
                    return True
        return False

    def get_adj(self, ind):
        origin = self.ind_to_loc(ind)
        x,y = origin
        x_min = x-self.r if self.wrap_x else max(0,x-self.r)
        x_max = x+self.r if self.wrap_x else min(self.width-1,x+self.r)
        y_min = y-self.r if self.wrap_y else max(0,y-self.r)
        y_max = y+self.r if self.wrap_y else min(self.height-1,y+self.r)

        neighbors = list()
        for i in range(x_min,x_max+1):
            for j in range(y_min,y_max+1):
                loc = (i%self.width, j%self.height)

                # within radius r
                if self.dist(loc, origin) > self.r:
                    continue

                # mask/obstacle
                if (self.mask is not None) and (not self.mask[loc[1],loc[0]]):
                    continue

                if (self.mask is not None) and (self.has_obs(origin, loc)):
                    continue

                neighbors.append(loc)

        ns_ind = [self.loc_to_ind(n) for n in neighbors]

        return ns_ind


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()


# coding: utf-8

# In[6]:

import sys

# In[7]:

def fibR(n):
 assert(n>0)
 if n==1 or n==2:
  return 1
 return fibR(n-1)+fibR(n-2)

def param(nb):
    m1 = nb-2
    m2 = 4*(nb-1)
    m3 = 4*nb
    return m1, m2, m3

# ### Fibonacci
#     I_mem_L2Cache
#         number = len(fib_list)+1 (0)
#         transform
#         //addi $3 $0 14   / addi r3, r0, 0x000E, r3 = 16-2
#         //addi $7 $0 003c / addi r7, r0, 0x003c, r7 = 4*(number-1)
#         //addi $7 $0 0040 / addi r7, r0, 0x0040, r7 = 4*number)
#         to
#         //addi $3 $0 m1
#         //addi $7 $0 m2
#         //addi $7 $0 m3
#      TestBed_L2Cache
#         `define	CheckNum	6'd33
#
#             6'd0 :	answer = 32'd0;
#             6'd1 :	answer = 32'd1;
#             6'd2 :	answer = 32'd1;
#             6'd3 :	answer = 32'd2;
#             6'd4 :	answer = 32'd3;
#             6'd5 :	answer = 32'd5;
#             6'd6 :	answer = 32'd8;
#             6'd7 :	answer = 32'd13;
#             6'd8 :	answer = 32'd21;
#             6'd9 :	answer = 32'd34;
#             6'd10:	answer = 32'd55;
#             6'd11:	answer = 32'd89;
#             6'd12:	answer = 32'd144;
#             6'd13:	answer = 32'd233;
#             6'd14:	answer = 32'd377;
#             6'd15:	answer = 32'd610;
#             6'd16:	answer = 32'd610;
#             6'd17:	answer = 32'd377;
#             6'd18:	answer = 32'd233;
#             6'd19:	answer = 32'd144;
#             6'd20:	answer = 32'd89;
#             6'd21:	answer = 32'd55;
#             6'd22:	answer = 32'd34;
#             6'd23:	answer = 32'd21;
#             6'd24:	answer = 32'd13;
#             6'd25:	answer = 32'd8;
#             6'd26:	answer = 32'd5;
#             6'd27:	answer = 32'd3;
#             6'd28:	answer = 32'd2;
#             6'd29:	answer = 32'd1;
#             6'd30:	answer = 32'd1;
#             6'd31:	answer = 32'd0;
#             6'd32:	answer = `EndSymbol;

# In[8]:

nb = sys.argv[1]

# nb = 20
CheckNum = nb*2 + 1 #`EndSymbol

m1, m2,  m3 = param(nb)

fib_list = [0]+[fibR(i) for i in range(1,nb)]
fib_list_reverse = fib_list.copy()
fib_list_reverse.reverse()
write_list = fib_list + fib_list_reverse

# In[9]:

TestBed_L2Cache_file = open('TestBed_L2Cache.v','w')

with open("TestBed_hasHazard.v", "r") as f:
    for line in f:
        if 'modify' in line:
            line = line.replace("33", format(CheckNum, 'd'))

        if 'modify2' in line:
            line = line.replace("xxx", format(fib_list[-1], 'd'))
            TestBed_L2Cache_file.write(line)
            break
        TestBed_L2Cache_file.write(line)

for i, ans in enumerate(write_list):
    TestBed_L2Cache_file.write("\t\t6'd%d :answer = 32'd%d;\n" %(i,ans))

TestBed_L2Cache_file.write("\t\t6'd%d :answer = `EndSymbol;\n\t\tendcase\n\tend\n\nendmodule" %(CheckNum-1))
TestBed_L2Cache_file.close()

# In[10]:

I_mem_L2Cache_file = open('I_mem_L2Cache','w')

with open("I_mem_hasHazard", "r") as f:
    for line in f:
        if 'modify1' in line:
            # annotation
            line = line.replace("0x000e",             format(m1, 'x').zfill(4))  # I-type operand immediate 16 bit
            line = line.replace("610",                format(fib_list[-1], 'd'))
            line = line.replace("16",                 format(nb, 'd'))
            line = line.replace("14",                 format(m1, 'd'))

            # instruction
            line = line.replace("00000_00000_001110", format(m1, 'b').zfill(16)) # I-type operand immediate 16 bit
        elif 'modify2' in line:
            # annotation
            line = line.replace("0x03c",              format(m2, 'x').zfill(4))  # I-type operand immediate 16 bit
            line = line.replace("60",                 format(m2, 'd'))

            # instruction
            line = line.replace("00000_00000_111100", format(m2, 'b').zfill(16)) # I-type operand immediate 16 bit
        elif 'modify3' in line:
            # annotation
            line = line.replace("0x0040",             format(m3, 'x').zfill(4))  # I-type operand immediate 16 bit
            line = line.replace("64",                 format(m3, 'd'))

            # instruction
            line = line.replace("0000000001000000",   format(m3, 'b').zfill(16)) # I-type operand immediate 16 bit

        I_mem_L2Cache_file.write(line)
I_mem_L2Cache_file.close()


"""
Handles the authentication of Slack API and the use it for inviting and removing of the
participants.

The code presume that the slack bot is already created and the token is stored in the
environment variable USER_TOKEN.

To communicate with the slack api and add or remove the participants from specific
channel, we need to have following user token scope in the slack bot:
1. channels:read
2. groups:read
3. groups:write
4. users:read
5. users:read.email
6. admin.conversations:write
"""
import os
import slack

client = slack.WebClient(token=os.environ["USER_TOKEN"])

def get_all_user_ids(emails: list[str]) -> set[str]:
    """
    Get all the user Ids from the Workspace
    that match the email addresses in the input list.
    Return a set of IDs.
    Args:
        email (str): The email to use

    Returns:
        list[str]:  Gets all the user Ids from the Workspace
    """
    id_set = set()
    for email in emails:
        response = client.users_lookupByEmail(email=email)
        id_set.add(response["user"]["id"])
    return id_set

def get_user_ids_from_channel(channel_id: str) -> set[str]:
    """
    Get all the user IDs from a specific channel.
    Return a set of user IDs.
    Args:
        channelId (str): The Channel Id.

    Returns:
        list[str]:  Return lists of user Ids that are already invited to the channel
    """
    response = client.conversations_members(channel=channel_id)
    member_ids = response["members"]
    return set(member_ids)

def invite_members(emails: list[str], channel_id: str):
    """
    Invite all the users with their email addresses to a specific channel.
    Args:
        emailList list[str]: The list of the email address.
        channelId (str): The Channel Id.

    Returns:
        list[str]:  Return lists of user Ids that are already invited to the channel
    """
    all_id_set = get_all_user_ids(emails)
    members_in_channel_set = get_user_ids_from_channel(channel_id)

    new_members_set = all_id_set - members_in_channel_set
    kicked_members_set = members_in_channel_set - all_id_set

    for member in new_members_set:
        client.admin_conversations_invite(channel=channel_id, user=member)

    for member in kicked_members_set:
        client.conversations_kick(channel=channel_id, user=member)

if __name__ == "__main__":
    invite_members(["email1@example.com", "email2@example.com"], "ABC123")


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


from typing import Any, Optional

import torch
import torch.distributed as dist
from torch.distributed import ReduceOp

from pipegoose.distributed._p2p import _P2P
from pipegoose.distributed.parallel_context import ParallelContext
from pipegoose.distributed.parallel_mode import ParallelMode

def scatter(
    tensor: torch.Tensor,
    dim: int,
    parallel_context: Optional[ParallelContext] = None,
    parallel_mode: Optional[ParallelMode] = None,
) -> torch.Tensor:
    """Scatter tensors to all ranks in parallel group."""
    world_size = parallel_context.get_world_size(parallel_mode)
    rank = parallel_context.get_local_rank(parallel_mode)

    if world_size == 1:
        return tensor

    assert tensor.size(dim) % world_size == 0

    tensor_list = torch.chunk(tensor, world_size, dim=dim)
    return tensor_list[rank]

def reduce(
    tensor: torch.Tensor,
    dst: int,
    op: ReduceOp = ReduceOp.SUM,
    async_op: bool = False,
    parallel_context: Optional[ParallelContext] = None,
    parallel_mode: Optional[ParallelContext] = None,
) -> torch.Tensor:
    """Reduce tensors from all processes in parallel group."""
    world_size = parallel_context.get_world_size(parallel_mode)

    if world_size == 1:
        return tensor

    group = parallel_context.get_group(parallel_mode)
    work = dist.reduce(tensor, dst=dst, op=op, group=group)

    if async_op:
        return tensor, work
    else:
        return tensor

def broadcast(
    tensor: torch.Tensor,
    src: int,
    async_op: bool = False,
    parallel_context: Optional[ParallelContext] = None,
    parallel_mode: Optional[ParallelMode] = None,
) -> torch.Tensor:
    """Broadcast a tensor from src rank to all processes in parallel group."""
    world_size = parallel_context.get_world_size(parallel_mode)

    if world_size == 1:
        return tensor

    group = parallel_context.get_group(parallel_mode)
    work = dist.broadcast(tensor, src=src, group=group, async_op=async_op)

    if async_op:
        return tensor, work
    else:
        return tensor

def all_gather(
    tensor: torch.Tensor,
    dim: int = 0,
    async_op: bool = False,
    parallel_context: Optional[ParallelContext] = None,
    parallel_mode: Optional[ParallelMode] = None,
) -> torch.Tensor:
    """All gather tensors from all processes in parallel group.

    Args:
        input (torch.Tensor): The tensor you want to gather.
        dim (int, optional): The dimension along which to gather the tensor.. Defaults to 0.
        async_op (bool, optional): _description_. Defaults to False.
        parallel_context (Optional[ParallelContext], optional): _description_. Defaults to None.
        parallel_mode (Optional[ParallelMode], optional): _description_. Defaults to None.

    Returns:
        torch.Tensor: _description_
    """
    world_size = parallel_context.get_world_size(parallel_mode)
    group = parallel_context.get_group(parallel_mode)

    if world_size == 1:
        return tensor

    tensor_list = [torch.empty_like(tensor) for _ in range(world_size)]
    work = dist.all_gather(tensor_list=tensor_list, tensor=tensor, async_op=async_op, group=group)

    if tensor.dim() == 0:
        tensor_list = [tensor.unsqueeze(dim=0) for tensor in tensor_list]

    tensor_list = torch.cat(tensor_list, dim=dim)

    if async_op:
        return tensor_list, work
    else:
        return tensor_list

def all_reduce(
    tensor: torch.Tensor,
    op: ReduceOp = ReduceOp.SUM,
    async_op: bool = False,
    parallel_context: Optional[ParallelContext] = None,
    parallel_mode: Optional[ParallelMode] = None,
) -> torch.Tensor:
    """All reduce a tensor from all ranks in parallel group."""
    world_size = parallel_context.get_world_size(parallel_mode)

    if world_size == 1:
        return tensor

    group = parallel_context.get_group(parallel_mode)
    work = dist.all_reduce(tensor, op=op, group=group, async_op=async_op)

    if async_op:
        return tensor, work
    else:
        return tensor

def reduce_scatter():
    pass

def send(
    data: Any,
    src: int,
    dst: int,
    parallel_context: ParallelContext,
    parallel_mode: ParallelMode = ParallelMode.PIPELINE,
):
    """P2P communication: Send an object from src rank to dst rank."""
    if src == parallel_context.get_local_rank(parallel_mode):
        _P2P().send(data, dst, parallel_context, parallel_mode)

def recv(
    src: int, dst: int, parallel_context: ParallelContext, parallel_mode: ParallelMode = ParallelMode.PIPELINE
) -> Optional[Any]:
    """P2P communication: Receive an object from src rank to dst rank."""
    if dst == parallel_context.get_local_rank(parallel_mode):
        return _P2P().recv(src, parallel_context, parallel_mode)

def barrier(parallel_context: ParallelContext, parallel_mode: ParallelMode):
    """Wait until all processes in the parallel mode reach this barrier."""
    process_group = parallel_context.get_group(parallel_mode)
    dist.barrier(group=process_group)


from itertools import permutations

# users_input = 'aasdfg'
# for i in list(map("".join, permutations(users_input, 3))):
#     print(i)

# for item in permutations('aasdfg', 3):
#     print(''.join(item))

# def names_combinations(users_input, words_amount):
#     if words_amount <= len(users_input):
#         for name in list(map("".join, permutations(users_input, words_amount))):
#             print(name.capitalize())
#     else:
#         print(f'The number of letters should be no more than {len(users_input)}')
#
#
# names_combinations('Alexander', 5)

# Create a function to sort a word with one given length.
def letters_permutation(users_input, words_amount):
    names_variants = sorted(list(set([''.join(name_variant).capitalize()
                                      for name_variant
                                      in permutations(users_input, words_amount)])))
    return names_variants

# Create a function to sort words with a range of preset lengths.
def letters_permutation_range(users_input, min_amount, max_amount):
    names_variants = [letters_permutation(users_input, words_amount)
                      for words_amount in range(min_amount, max_amount + 1)]
    return names_variants

print(letters_permutation('bfFf', 4))
print(letters_permutation_range('axXx', 3, 4))


import sys

from multipledispatch import dispatch

# operations
OPS = {"+": "ADD", "-": "SUB", "*": "MUL", "/": "DIV"}

# key words
KEYWORDS = {"if": "IF", "for": "FOR", "while": "WHILE", "print": "PRINT", "set": "SET", "cls": "CLS", "quit": "QUIT"}
INVALID_CHARS = ""
BREAK_CHARS = "(){}[]"

# assignments
SET = "SET"
ASSIGN = "ASSIGN"
ADD_ASSIGN = "ADD_ASSIGN"
SUB_ASSIGN = "SUB_ASSIGN"
MUL_ASSIGN = "MUL_ASSIGN"
DIV_ASSIGN = "DIV_ASSIGN"

# compare
EQU = "EQU"
GTE = "GTE"
LTE = "LTE"
GT = "GT"
LT = "LT"

# types
FLOAT = "FLOAT"
INT = "INT"
STR = "STR"
VAR = "VAR"

# braces
L_PAR = "L_PAR"
R_PAR = "R_PAR"
L_BKT = "L_BKT"
R_BKT = "R_BKT"
L_CUR = "L_CUR"
R_CUR = "R_CUR"

# idk
EOF = "EOF"

class DivideByZeroException(Exception):
	def __init__(self, message):
		self.message = message
		super().__init__(message)

class InvalidPeriodInNumberException(Exception):
	def __init__(self, message):
		self.message = message
		super().__init__(message)

class UnrecognizedCharException(Exception):
	def __init__(self, message):
		self.message = message
		super().__init__(message)

class MismatchTokenTypeException(Exception):
	def __init__(self, message):
		self.message = message
		super().__init__(message)

class InvalidCharInWord(Exception):
	def __init__(self, message):
		self.message = message
		super().__init__(message)

class NotRecognizedCommandException(Exception):
	def __init__(self, message):
		self.message = message
		super().__init__(message)

class VariableUsedBeforeDeclared(Exception):
	def __init__(self, message):
		self.message = message
		super().__init__(message)

class Token():
	type = None
	value = None

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

	def __str__(self):
		return f"Token({self.type}, {self.value})"

	def __repr__(self):
		return self.__str__()

class Lexer:
	def __init__(self, text):
		self.text = text
		self.ptr = 0
		self.curr_char = self.text[self.ptr]

	# advance ptr and update current char
	def advance(self):
		self.ptr += 1
		if self.ptr > len(self.text) - 1:
			self.curr_char = None
		else:
			self.curr_char = self.text[self.ptr]

	# skip white space
	def skip_whitespace(self):
		while self.curr_char is not None and self.curr_char.isspace():
			self.advance()

	# generate number token from text, returns int if int, float if not
	def generate_num_token(self, isNegative):
		result = ""
		has_period = False
		while self.curr_char is not None and (self.curr_char.isdigit() or self.curr_char == "."):
			# check if there's a decimal point
			if self.curr_char == ".":
				if has_period:
					raise InvalidPeriodInNumberException("Too many periods in number")
				has_period = True
				result += self.curr_char
				self.advance()
			else:
				result += self.curr_char
				self.advance()

		if isNegative:
			result = float(result) * -1

		if has_period:
			return Token(FLOAT, float(result))
		else:
			return Token(INT, int(result))

	# generate operation token
	def generate_op_token(self):
		value = self.curr_char
		type = OPS[self.curr_char]
		self.advance()
		return Token(type, value)

	def generate_word(self):
		word = ""
		while self.curr_char is not None:
			if self.curr_char in INVALID_CHARS:
				raise InvalidCharInWord(f"Invalid char '{self.curr_char}' in word")
			if self.curr_char in OPS.keys():
				break
			if self.curr_char in BREAK_CHARS:
				break
			if self.curr_char == " ":
				break
			word += self.curr_char
			self.advance()

		# generate key word
		if word in KEYWORDS.keys():
			return Token(KEYWORDS[word], word)
		# generate var
		else:
			return Token(VAR, word)

	def generate_string(self):
		string = ""
		while self.curr_char is not None:
			if self.curr_char == "\"":
				self.advance()
				break
			string += self.curr_char
			self.advance()
		return Token(STR, string)

	# automatically gets the next token
	def get_next_token(self):
		while self.curr_char is not None:

			# skip whitespace
			if self.curr_char.isspace():
				self.skip_whitespace()
				continue

			# check if negative number or subracting
			if self.curr_char == "-":
				self.advance()
				if self.curr_char.isdigit():
					return self.generate_num_token(True)

			# generate number token?
			if self.curr_char.isdigit() or self.curr_char == ".":
				return self.generate_num_token(False)

			# generate op token?
			if self.curr_char in OPS.keys():
				return self.generate_op_token()

			# generate key word token?
			if self.curr_char.isalpha():
				return self.generate_word()

			# parentheses
			if self.curr_char == "(":
				self.advance()
				return Token(L_PAR, "(")
			if self.curr_char == ")":
				self.advance()
				return Token(R_PAR, ")")

			# square braces
			if self.curr_char == "[":
				self.advance()
				return Token(L_BKT, "[")
			if self.curr_char == "]":
				self.advance()
				return Token(R_BKT, "]")

			# curly brackets
			if self.curr_char == "{":
				self.advance()
				return Token(L_CUR, "{")
			if self.curr_char == "}":
				self.advance()
				return Token(R_CUR, "}")

			# assignments
			if self.curr_char == "=":
				self.advance()
				return Token(ASSIGN, "=")

			if self.curr_char == "\"":
				self.advance()
				return self.generate_string()

			raise UnrecognizedCharException(f"Cannot parse char: '{self.curr_char}'")

		return Token(EOF, None)

class Interpreter:
	vars = dict()
	def __init__(self, lexer):
		self.lexer = lexer
		self.curr_token = self.lexer.get_next_token()

	def eat(self, token_type):
		if self.curr_token.type == token_type:
			self.curr_token = self.lexer.get_next_token()
		else:
			raise MismatchTokenTypeException(f"Mismatch token type: {self.curr_token.type} != {token_type}")

	def factor(self):
		token = self.curr_token
		if token.type == STR:
			self.eat(token.type)
			return token.value

		if token.type == VAR:
			self.eat(token.type)
			result = ""
			try:
				result = self.vars[token.value]
			except KeyError:
				raise VariableUsedBeforeDeclared(f"Variable {token.value} used before declared")
			return result

		if token.type in ("FLOAT", "INT"):
			self.eat(token.type)
			return token.value
		elif token.type == L_PAR:
			self.eat(L_PAR)
			result = self.expr()
			self.eat(R_PAR)
			return result

	def term(self):
		result = self.factor()

		while self.curr_token.type in ("MUL", "DIV"):
			if self.curr_token.type == "MUL":
				self.eat(self.curr_token.type)
				result *= self.factor()
			elif self.curr_token.type == "DIV":
				self.eat(self.curr_token.type)
				# checks
				value = self.factor()
				if value == 0:
					raise DivideByZeroException("Cannot divide by zero")
				if result % value == 0:
					result = int(result / value)
				else:
					result /= value
		return result

	def assign_var(self):
		token = self.curr_token
		self.eat(SET)

		# var name
		var_name = self.curr_token.value
		self.eat(VAR)

		# assignment
		if self.curr_token.type == ASSIGN:
			self.eat(ASSIGN)

		# value
		value = self.curr_token.value
		self.eat(self.curr_token.type)

		# save var name and value
		self.vars[var_name] = value

	def script(self, isConsole):
		while self.curr_token.type != EOF:
			if self.curr_token.type == "PRINT":
				self.eat("PRINT")
				self.eat(L_PAR)
				result = self.expr()
				self.eat(R_PAR)
				print(result)
				continue

			if self.curr_token.type in ("INT", "FLOAT"):
				if isConsole:
					print(self.expr())

			if self.curr_token.type == "SET":
				self.assign_var()
				continue

			if self.curr_token.type == "CLS":
				self.eat("CLS")
				print("\033[H\033[J", end="")
				continue

			if self.curr_token.type == "QUIT":
				exit()

			if self.curr_token.value == None:
				continue

			raise NotRecognizedCommandException(f"Did not recoginize command: {self.curr_token.value}")

	def expr(self):
		result = self.term()
		while self.curr_token.type in ("ADD", "SUB"):
			if self.curr_token.type == "ADD":
				self.eat("ADD")
				result += self.term()
			elif self.curr_token.type == "SUB":
				self.eat("SUB")
				result -= self.term()
		return result

def console():
	while True:
		try:
			text = input("JB> ")
		except EOFError:
			break
		if not text:
			continue
		lexer = Lexer(text)
		interpreter = Interpreter(lexer)
		interpreter.script(True)

def get_file_contents(path):
	with open(path) as f:
		return f.read()

def main():

	if len(sys.argv) == 1:
		console()
	if len(sys.argv) == 2:
		script = get_file_contents(sys.argv[1])
		lexer = Lexer(script)
		interpreter = Interpreter(lexer)
		interpreter.script(False)

if __name__ == "__main__":
	main()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import random
import numpy as np
from collections import deque
import os
import sys

import torch
import torch.nn.functional as F
import torch.optim as optim

from agent import Agent
from dqn_model import DQN
from collections import deque
"""
you can import any package and define any extra function as you need
"""

torch.manual_seed(595)
np.random.seed(595)
random.seed(595)

class Agent_DQN(Agent):
    def __init__(self, env, args):
        """
        Initialize everything you need here.
        For example:
            paramters for neural network
            initialize Q net and target Q net
            parameters for repaly buffer
            parameters for q-learning; decaying epsilon-greedy
            ...
        """
        super(Agent_DQN,self).__init__(env)
        ###########################
        # YOUR IMPLEMENTATION HERE #
        self.replay_buffer_size = 10000
        self.start_to_learn = 5000
        self.update_target_net = 5000
        self.learning_rate = 1.5e-4
        self.batch_size = 32
        self.buffer_ = deque(maxlen=self.replay_buffer_size)
        self.epsilon = 0.99
        self.epsilon_ = 0.2

        if args.test_dqn:
            #you can load your model here
            print('loading trained model')
            ###########################
            # YOUR IMPLEMENTATION HERE #
            self.Net = DQN()

    def init_game_setting(self):
        """
        Testing function will call this function at the begining of new game
        Put anything you want to initialize if necessary.
        If no parameters need to be initialized, you can leave it as blank.
        """
        ###########################
        # YOUR IMPLEMENTATION HERE #

        ###########################
        pass

    def make_action(self, observation, epsilon, device='cpu', test=True):
        """
        Return predicted action of your agent
        Input:
            observation: np.array
                stack 4 last preprocessed frames, shape: (84, 84, 4)
        Return:
            action: int
                the predicted action from trained model
        """
        ###########################
        # YOUR IMPLEMENTATION HERE #
        if random.random() < epsilon:
            action = self.env.action_space.sample()
        else:
            observation = torch.tensor(observation).to(device)
            q_values = self.Net(observation)
            _,action_value = torch.max(q_values,dim=-1)
            action = int(action_value.item())
        ###########################
        return action

    def push(self, sample):
        """ You can add additional arguments as you need.
        Push new data to buffer and remove the old one if the buffer is full.

        Hints:
        -----
            you can consider deque(maxlen = 10000) list
        """
        ###########################
        # YOUR IMPLEMENTATION HERE #
        self.buffer_.append(sample)
        ###########################

    def replay_buffer(self,batch_size):
        """ You can add additional arguments as you need.
        Select batch from buffer.
        """
        ###########################
        # YOUR IMPLEMENTATION HERE #
        idx_s = random.sample(range(len(self.buffer_)),batch_size)
        minibatch = []
        for _,i in enumerate(idx_s):
            minibatch.append(i)
        ###########################
        return np.array(minibatch)

    def get_reward(self,action):
        next_state,reward,terminal,_ = env.step(action)
        return next_state,reward,terminal

    def train(self):
        """
        Implement your training algorithm here
        """
        ###########################
        # YOUR IMPLEMENTATION HERE #
        optimizer = optim.Adam(net.parameters(),lr=self.learning_rate)
        total_r = []
        best_r = None
        device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

        while True:
            observation = env.reset()
            sum_reward = 0
            i = 0
            self.epsilon = max(epsilon_,self.epsilon-0.1/i)
            while True:
                action_training = self.make_action(observation,self.epsilon,device)
                next_state,reward,terminal = get_reward(action_training)
                sum_reward += reward
                if terminal:
                    break
                else:
                    observation = next_state
            total_r.append(sum_reward)
            if total_r is not None:
                mean_reward = np.mean(total_r)
                if mean_reward > 40:
                    print("DOne")
                    break

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


import numpy as np
import pandas as pd
from  sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import joblib
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.ensemble import RandomForestRegressor
import json

# data cleaning

data_file = 'immo.csv'
df = pd.read_csv(data_file)

# drop column has nan missing values for more than 60%
df = pd.DataFrame(df)
df = df.drop(['ID','PostCode', 'Sub type', 'Floor', 'IsIsolated', 'HasSeaView', 'TotalRoomCount', 'HasAttic',  'HasDiningRoom',    'GardenArea', 'LivingRoomArea', 'NetHabitableSurface(msq)', 'SchoolDistance', 'ShopDistance', 'TransportDistance', 'BuildingCondition','RegionCode','street'], axis=1)

# age of building
list_years = []
for year in df['ConstructionYear']:
    years = 2022 - year
    list_years.append(years)
df = df.assign(ConstructionYear = list_years)

# replace & drop nan values
df[['NetHabitableSurface', 'ConstructionYear']] = df[['NetHabitableSurface','ConstructionYear']].fillna(df[['NetHabitableSurface', 'ConstructionYear']].mean())
columns_to_n = ['Type', 'locality', 'KitchekType']
df[columns_to_n] = df[columns_to_n].replace(np.nan, 'No Value')
df = df.drop_duplicates()
df = df.dropna(subset=['BedroomCount', 'Price', 'Province'])

# replace nan value in heating type with most frequent value
heating_type = df['HeatingType'].mode()[0]
facade_count = df['FacadeCount'].mode()[0]
flood_zone_type = df['FloodZoneType'].mode()[0]

df = df.fillna({'HasLift': False, 'HasBalcony': False,  'HasGarden': False, 'HasBasement': False,  'IsDoubleGlaze': False, 'HeatingType':heating_type, 'FacadeCount': facade_count, 'FloodZoneType':flood_zone_type})

#turn data type to int
df['NetHabitableSurface'] = df['NetHabitableSurface'].astype('int')
df['Price'] = df['Price'].astype('int')
df['BedroomCount'] = df['BedroomCount'].astype('int')
df['ConstructionYear'] = df['ConstructionYear'].astype('int')
df['FacadeCount'] = df['FacadeCount'].astype('int')

df = df.dropna(subset=['Price', 'NetHabitableSurface', 'BedroomCount', 'Province', 'Region'])

df.columns

# pipeline normorlize ohe
num_feature = ['BedroomCount', 'NetHabitableSurface']
cate_feature = ['Type', 'Province', 'locality', 'Region', 'FloodZoneType', 'HeatingType', 'KitchekType']

num_trans = Pipeline([('scaler', StandardScaler())])
cate_trans = Pipeline([('ohe', OneHotEncoder(handle_unknown='ignore', sparse=False))])

preprocessor = ColumnTransformer([('num_transformer', num_trans, num_feature),
                                ('cate_transformer', cate_trans, cate_feature)])

# split data
X = df.drop('Price', axis=1)
y = df['Price']

X_train, X_test, y_train, y_test = train_test_split(X,y,random_state=0,  test_size=0.2)
X.columns

trans_train = preprocessor.fit_transform(X_train)
trans_test = preprocessor.fit_transform(X_test)

# random forest
rf = RandomForestRegressor()

rf.fit(trans_train, y_train)
y_predict = rf.predict(trans_test)

#
model = joblib.load('rt_model.pkl')

predictions = model.predict(trans_test)
predictions


import math

def GaussEliminate(A,b):
    '''高斯消去，不选主元，省略归一化操作'''
    n=len(A)
    for k in range(n-1):
        if A[k][k]==0:
            print('Fail!')
            break
        for i in range(k+1,n):
            c=-A[i][k]/A[k][k]              #计算倍乘因子
            for j in range(k+1,n):
                A[i][j]+=c*A[k][j]          #更新矩阵元素
            b[i]+=c*b[k]
    return A,b

def GaussEliminatePermutated (A,b):
    n=len(A)
    for k in range(n-1):
        Tmp_column=[abs(A[i][k]) for i in range(k,n)]
        max_i=Tmp_column.index(max(Tmp_column))+k               #找到最大主元在第几行
        if max_i!=k:
            Tmp_row=A[k]                        #交换系数矩阵的当前行与最大主元所在行
            A[k]=A[max_i]
            A[max_i]=Tmp_row
            Tmp_b=b[k]                          #交换b的当前行与最大主元所在行
            b[k]=b[max_i]
            b[max_i]=Tmp_b
        if A[k][k]==0:                      #主元为0，无法继续
            print('Fail!')
            break
        for i in range(k+1,n):
            c=-A[i][k]/A[k][k]              #计算倍乘因子
            for j in range(k+1,n):
                A[i][j]+=c*A[k][j]          #更新矩阵元素
            b[i]+=c*b[k]
    return A,b

def HilbertMatrix (n):
    '''产生n阶希尔伯特矩阵'''
    H=[[1.0/(i+j+1) for j in range(n)]for i in range(n)]
    return H

def BackSubstitutionU(A,b):
    '''系数矩阵为上三角矩阵时的回代求解'''
    n=len(A)
    x=[0]*n
    for i in range (n-1,-1,-1):
        if A[i][i]==0:
            print('A pivot is 0. BackSubstitutionU Failed./n')
            break
        x[i]=b[i]
        for j in range(i+1,n):
            x[i]-=A[i][j]*x[j]
        x[i]/=A[i][i]
    return x

def BackSubstitutionL(A,b):
    '''系数矩阵为下三角矩阵时的回代求解'''
    n=len(A)
    x=[0]*n
    for i in range (n):
        if A[i][i]==0:
            print('A pivot is 0. BackSustitutionL Failed./n')
            break
        x[i]=b[i]
        for j in range(i):
            x[i]-=A[i][j]*x[j]
        x[i]/=A[i][i]
    return x

def transpose(A):
    AT=[[A[i][j] for i in range(len(A))]for j in range(len(A[0]))]
    return AT

def GEM(A,b):
    GaussEliminate(A,b)
    return BackSubstitutionU(A,b)

def GEMPermutated(A,b):         #部分选主元的GEM算法
    GaussEliminatePermutated(A,b)
    return BackSubstitutionU(A,b)

def CholeskyDecomposition(A):
    '''对称正定矩阵的Cholesky分解,分解成下三角矩阵L及其转置的乘积，返回L（其中只有下三角部分有意义）'''
    n=len(A)
    for j in range(n):
        for k in range(j):                                  #利用A[j][j]=Sum k from 1 to j L[j][k]^2，求出L[j][j]
            A[j][j]-=A[j][k]*A[j][k]
        if(A[j][j]>=0):A[j][j]=math.sqrt(A[j][j])
        else:                                               #Hilbert矩阵过于奇异，由于误差导致A[j][j]值变成负
            print("A[j][j]<0!")
            A[j][j]=float('nan')
        for i in range(j+1,n):                              #利用A[i][j]=Sum k from 1 to j L[i][k]*L[j][k]， 求第j列剩余的元素L[i][j]
            for k in range(j):
                A[i][j]-=A[i][k]*A[j][k]
            A[i][j]/=A[j][j]
    return A

'''A=[[0.0000000000000001,2],[3,1]]
b=[2,4]
x=GEM(A,b)
print(x)
A=[[0.0000000000000001,2],[3,1]]
b=[2,4]
x=GEMPermutated(A,b)
print(x)
print(A,b)#A,b的值被修改
B=[[5,-1,-1],[-1,3,-1],[-1,-1,5]]
L=CholeskyDecomposition(B)
print(L)'''

while True:
    print("n=",end='')
    dim=int(input())                        #输入问题的维数
    H=HilbertMatrix(dim)
    b=[1]*dim
    #print('Hilbert Matrix:\n',H)
    L=CholeskyDecomposition(H)
    LT=transpose(L)
    #print('Lower Triangular Matrix (The upper part is meaningless):\n',L)
    #解L.LT.x=b
    x1=BackSubstitutionL(L,b)#求解L.x1=b
    x_CH=BackSubstitutionU(LT,x1)#求解Lt.x=x1
    print('Cholesky Method Solution:\n',x_CH)
    x_GEM=GEM(HilbertMatrix(dim),[1]*dim)
    print('GEM Solution:\n',x_GEM)
    x_GEMP=GEMPermutated(HilbertMatrix(dim),[1]*dim)
    print('GEM(Permutated) Solution:\n',x_GEMP)
    print("Continue?(y or n)")
    if input()=='n':break
    else:print()


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


def P4(nums):
    L1 = []
    for i in nums:
        if i+1 in nums:
            L1.append([i, i+1])
        else:
            continue
    L1.sort(key=lambda x:x[0])
    x = len(L1)-1
    count = 0
    L3 = []
    for a in range(x):
        if L1[a][1] == L1[a+1][0]:
            count = count + 1
            L3.append(count)
        else:
            count = 0
    if len(L3) == 0:
        return 1
    y = max(L3)+2
    return y


#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from hmm_model import HMM
from data_process import HMMDataLoader
from unigram_model import UniGramSeg

data_path="../seg-data/training/pku_training.utf8"
hmm = HMM(data_path)
hmm.load("model")
cut = hmm.cut_sentence

seg = UniGramSeg()
data_loader = HMMDataLoader(data_path)
vocab_dict = data_loader.generate_vocab_dict()
seg.set_dict(vocab_dict)

s = ""
print("这是一个分词Demo, 输入'q'退出.")
while s != 'q':
    s = input("请输入待分词句子: ")
    q = input("选择模型 hmm or ngram?")
    if q == "hmm":
        print("HMM分词结果：")
        words = cut(s)
    elif q == "ngram":
        print("N-gram分词结果：")
        words = seg.cut(s)
    else:
        print("模式输入错误！")
        continue
    print("/".join(words))


# Example of using customized Config object
from Package.defines import Config

config = Config()
config.reload_logger('develop')
config.reload_cfg()

def main():
    print('---------------------------------------------------------')
    print(config.peek())
    config.set('Module 3', 'says', 'It should be a brand new configure')
    print(config.peek())


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()


"""
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


#!/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)


'''
Created on Feb 11, 2014

@author: u0490822
'''
import os
import unittest

import nornir_imageregistration as nir
import nornir_imageregistration.tileset as tiles

from . import setup_imagetest

class TestTiles(setup_imagetest.ImageTestBase):

    def testBrightfieldShading(self):

        ReferenceImagePath = self.GetImagePath("400.png")
        ShadedImagePath = self.GetImagePath("400_Shaded.png")
        ShadingReferencePath = self.GetImagePath("BrightfieldShading.png")

        self.assertTrue(os.path.exists(ReferenceImagePath))
        self.assertTrue(os.path.exists(ShadedImagePath))
        self.assertTrue(os.path.exists(ShadingReferencePath))

        originalImage = nir.LoadImage(ReferenceImagePath)
        shadedImage = nir.LoadImage(ShadedImagePath)
        shadingMask = nir.LoadImage(ShadingReferencePath)

        shadedImageV2 = originalImage * shadingMask
        shadedImageV2Path = os.path.join(self.TestOutputPath, "TestGeneratedShadedImage.png")

        nir.SaveImage(shadedImageV2Path, shadedImageV2)

        OutputPaths = tiles.ShadeCorrect([ShadedImagePath, shadedImageV2Path], shadingMask, self.TestOutputPath, correction_type=tiles.ShadeCorrectionTypes.BRIGHTFIELD)

        shownImages = [originalImage, shadedImage, shadingMask]

        for path in OutputPaths:
            correctedImage = nir.LoadImage(path)
            shownImages.append(correctedImage)

        # nir.ShowGrayscale(shownImages)

        pass

    def testBrightfieldShadingA(self):
        ShadedImagePath = self.GetImagePath("CorrectionA_Tile.png")
        ShadingReferencePath = self.GetImagePath("CorrectionA_Shading.png")

        self.ExamineBrightfieldShading(ShadedImagePath, ShadingReferencePath)

    def testBrightfieldShadingB(self):
        ShadedImagePath = self.GetImagePath("CorrectionB_Tile.png")
        ShadingReferencePath = self.GetImagePath("CorrectionB_Shading.png")

        self.ExamineBrightfieldShading(ShadedImagePath, ShadingReferencePath)

    def ExamineBrightfieldShading(self, ShadedImagePath, ShadingReferencePath):

        self.assertTrue(os.path.exists(ShadedImagePath))
        self.assertTrue(os.path.exists(ShadingReferencePath))

        shadedImage = nir.LoadImage(ShadedImagePath)
        shadingMask = nir.LoadImage(ShadingReferencePath)

        OutputPaths = tiles.ShadeCorrect([ShadedImagePath], shadingMask, self.TestOutputPath, correction_type=tiles.ShadeCorrectionTypes.BRIGHTFIELD)

        shownImages = [shadedImage, shadingMask]

        for path in OutputPaths:
            correctedImage = nir.LoadImage(path)
            shownImages.append(correctedImage)

        # nir.ShowGrayscale(shownImages)

        pass

if __name__ == "__main__":
    # import sys;sys.argv = ['', 'Test.testName']
    unittest.main()


# 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}"


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:

    def dfs(self,root,currMax,curr):
        if not root:
            return

        curr = max(curr,root.val)
        currMax[(root.val,root)] = curr

        self.dfs(root.left,currMax,curr)
        self.dfs(root.right,currMax,curr)

    def goodNodes(self, root: TreeNode) -> int:

        currMax = defaultdict(list)
        curr = root.val
        self.dfs(root,currMax,curr)
        count = 0

        for k, v in currMax.items():
            if k[0]>=v:
                count+=1

        return count


import re
import torch
import logging
import random
from torch.utils.data import DataLoader, Dataset
from transformers import BertTokenizer

logger = logging.getLogger(__name__)

class ModelDataLoader(Dataset):
    def __init__(self, file_path, args, metric, tokenizer):
        self.args = args
        self.metric = metric

        self.label = []
        self.sentence = []
        self.token_type_ids = []
        self.attention_mask = []

        self.masked_input = []
        self.masked_labels = []

        self.file_path = file_path
        self.bert_tokenizer = tokenizer
        self.vocab_size = len(self.bert_tokenizer)
        self.masking_ratio = self.args.masking_ratio

        """
        init token, idx = [CLS], 101
        sep token, idx = [SEP], 102
        pad token, idx = [PAD], 0
        unk token, idx = [UNK], 100
        eos token, idx = [EOS], 30522
        max token, idx = [MASK], 103
        """
        self.init_token = self.bert_tokenizer.cls_token
        self.sep_token = self.bert_tokenizer.sep_token
        self.pad_token = self.bert_tokenizer.pad_token
        self.mask_token = self.bert_tokenizer.mask_token

        self.init_token_idx = self.bert_tokenizer.convert_tokens_to_ids(self.init_token)
        self.sep_toke_idx = self.bert_tokenizer.convert_tokens_to_ids(self.sep_token)
        self.pad_token_idx = self.bert_tokenizer.convert_tokens_to_ids(self.pad_token)
        self.mask_token_idx = self.bert_tokenizer.convert_tokens_to_ids(self.mask_token)

    # Load train, valid, test data in args.path_to_data
    def load_data(self, type):

        with open(self.file_path) as file:
            lines = file.readlines()

            for line in lines:

                sentence, token_type, attention_mask, label = self.data2tensor(line)

                if sentence == None:
                    continue

                self.sentence.append(sentence)
                self.token_type_ids.append(token_type)
                self.attention_mask.append(attention_mask)
                self.label.append(label)

        assert len(self.sentence) == \
               len(self.label)

    def preprocess_text(self, review):
        review = re.sub("<br />", " ", review)
        alphanum = re.sub("[^a-zA-Z0-9]", " ", review)
        words = alphanum.lower().split()
        tokens = words
        return (" ".join(tokens))

    """
    Converting text data to tensor &
    expanding length of sentence to args.max_len filled with PAD idx
    """
    def data2tensor(self, line):
        split_data = line.split('\t')

        sentence, label = self.preprocess_text(split_data[0].strip()), split_data[1].strip()

        if len(sentence) == 0:
            return None, 0, 0, 0

        sentence_tokens = self.bert_tokenizer(sentence,
                                              return_tensors="pt",
                                              max_length=self.args.max_len,
                                              pad_to_max_length="right")

        input_ids = sentence_tokens['input_ids']
        token_type_ids = sentence_tokens['token_type_ids']
        attention_mask = sentence_tokens['attention_mask']

        try:
            start_padding_idx = (input_ids.squeeze(0) == self.pad_token_idx).nonzero()[0].data.cpu().numpy()[0]
        except IndexError:
            start_padding_idx = self.args.max_len

        mask = torch.zeros(start_padding_idx)

        mask_num = int(start_padding_idx * self.masking_ratio)
        random_list = [random.randint(0, start_padding_idx-2) for r in range(mask_num)]

        for idx in random_list:
            mask[idx] = 1.0

        mask_input = input_ids.squeeze(0)[:start_padding_idx]
        masked_inputs = mask_input.masked_fill(mask == 1, self.mask_token_idx)

        for idx in range(self.args.max_len - len(masked_inputs)):
            masked_inputs = torch.cat([masked_inputs, torch.tensor([self.pad_token_idx])], dim=-1)

        mask_labels = input_ids.squeeze(0).masked_fill(masked_inputs != self.mask_token_idx, -100)

        self.masked_input.append(masked_inputs)
        self.masked_labels.append(mask_labels)

        return torch.tensor(input_ids).squeeze(0), \
               torch.tensor(token_type_ids).squeeze(0), \
               torch.tensor(attention_mask).squeeze(0), \
               torch.tensor(int(label))

    def __getitem__(self, index):

        mask_data = {'masked_input': self.masked_input[index],
                     'masked_label': self.masked_labels[index]}

        input_data = {'input_ids': self.sentence[index],
                      'token_type_ids': self.token_type_ids[index],
                      'attention_mask': self.attention_mask[index],
                      'label': self.label[index].to(self.args.device)}

        input_data = self.metric.move2device(input_data, self.args.device)
        mask_data = self.metric.move2device(mask_data, self.args.device)

        return input_data, mask_data

    def __len__(self):
        return len(self.label)

# Get train, valid, test data loader and BERT tokenizer
def get_loader(args, metric):
    path_to_train_data = args.path_to_data + '/' + args.task + '/' + args.train_data
    path_to_valid_data = args.path_to_data + '/' + args.task + '/' + args.valid_data
    path_to_test_data = args.path_to_data + '/' + args.task + '/' + args.test_data

    tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')

    if args.MLM == 'True':
        test_iter = ModelDataLoader(path_to_test_data, args, metric, tokenizer)
        test_iter.load_data('test')

        loader = {'test': DataLoader(dataset=test_iter,
                                     batch_size=args.batch_size,
                                     shuffle=True)}
    else:
        if args.train == 'True' and args.test == 'False':
            train_iter = ModelDataLoader(path_to_train_data, args, metric, tokenizer)
            valid_iter = ModelDataLoader(path_to_valid_data, args, metric, tokenizer)
            train_iter.load_data('train')
            valid_iter.load_data('valid')

            loader = {'train': DataLoader(dataset=train_iter,
                                          batch_size=args.batch_size,
                                          shuffle=True),
                      'valid': DataLoader(dataset=valid_iter,
                                          batch_size=args.batch_size,
                                          shuffle=True)}

        elif args.train == 'False' and args.test == 'True':
            test_iter = ModelDataLoader(path_to_test_data, args, metric, tokenizer)
            test_iter.load_data('test')

            loader = {'test': DataLoader(dataset=test_iter,
                                         batch_size=args.batch_size,
                                         shuffle=True)}

        elif args.train == 'True' and args.test == 'True':
            train_iter = ModelDataLoader(path_to_train_data, args, metric, tokenizer)
            valid_iter = ModelDataLoader(path_to_valid_data, args, metric, tokenizer)
            test_iter = ModelDataLoader(path_to_test_data, args, metric, tokenizer)

            train_iter.load_data('train')
            valid_iter.load_data('valid')
            test_iter.load_data('test')
            loader = {'train': DataLoader(dataset=train_iter,
                                          batch_size=args.batch_size,
                                          shuffle=True),
                      'valid': DataLoader(dataset=valid_iter,
                                          batch_size=args.batch_size,
                                          shuffle=True),
                      'test': DataLoader(dataset=test_iter,
                                         batch_size=args.batch_size,
                                         shuffle=True)}
    return loader, tokenizer

if __name__ == '__main__':
    get_loader('test')


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