import rpyc
import sys

server = "localhost"

if len(sys.argv) > 1:
	if int(sys.argv[1]) > 0:
		try:
			conn = rpyc.connect(server, 18811)
			if conn.root:
				conn.root.initialize_connections(int(sys.argv[1]))
				while True:
					try:
						remote_command = input("Input the Command:\t").lower().split(" ")
						conn.root.handle_remote_command(remote_command)
						if remote_command[0] == "exit":
							sys.exit(0)
					except KeyboardInterrupt:
						print("\nKeyboardInterrupt detected. Disconnecting from server.")
						conn.close()
						break
		except EOFError:
			print("Connection Terminated.")
		finally:
			print("Exiting.")

	else:
		print("No of connections cannot be less than 1.")
		sys.exit(0)
else:
	print("Usage: 'driver_service.py <number_of_connections>'")
	sys.exit(0)


from torch import nn
import torch.nn.functional as f

class LSTM(nn.Module):
    def __init__(self, in_channels, hidden_dim, n_layer, n_classes):
        super(LSTM, self).__init__()

        self.n_layer = n_layer
        self.latent_dim = 32
        self.hidden_dim = hidden_dim
        self.map = nn.Linear(in_channels, self.latent_dim)
        self.lstm = nn.LSTM(self.latent_dim, hidden_dim, n_layer, batch_first=True)
        self.fc = nn.Linear(hidden_dim, n_classes)

    def forward(self, x):
        x = self.map(x)
        out, (h_n, c_n) = self.lstm(x)
        x = h_n[-1, :, :]
        y = self.fc(x)
        y = f.softmax(y)
        return y


from openpyxl import Workbook
arquivo_excel = Workbook()
planilha1 = arquivo_excel.active
planilha1.title = "Relatorios"
planilha2 = arquivo_excel.create_sheet("Ganhos")
planilha1['A1'] = 'Categoria'
planilha1['B1'] = 'Valor'
planilha1['A2'] = "Restaurante"
planilha1['B2'] = 45.99
planilha2.cell(row=3, column=1, value=2000)
planilha2.cell(row=1, column=1, value="VALOR")
arquivo_excel.save("relatorio.xlsx")


from flask import Flask, render_template, request
import pandas as pd
import numpy as np
app = Flask(__name__)

# Reading dataset in global scope
df = pd.read_csv("winequalityN.csv")

# This is the home page
@app.route("/")
def home():
    return render_template("home.html")

# This is the page where we will load the dataset
@app.route("/load", methods = ["GET", "POST"])
def load():
    if request.method == "POST" and "submit_button" in request.form:
        return render_template("results.html")

# This is the page where we will check the shape of the dataset
@app.route("/shape", methods = ["GET", "POST"])
def shape():
    if request.method == "POST" and "check_shape" in request.form:
        rc = df.shape
        shape = f"Rows: {rc[0]}, Columns: {rc[1]}"
        return render_template("results2.html", shape = shape)

# This is the page where we will check the column names of the dataset
@app.route("/column_names", methods = ["GET", "POST"])
def column_names_fun():
    if request.method == "POST" and "check_column_names" in request.form:
        col_names = df.columns
        arr = list(col_names)
        return render_template("columnr.html", colnames = arr)

# This is the page where we will check the missing values of the dataset
@app.route("/missing_values", methods = ["GET", "POST"])
def miss_val():
    if request.method == "POST" and "missing_values" in request.form:
        values = list(df.isnull().sum())
        miss_values = []
        arr = list(df.columns)
        for i in range(len(values)):
            if values[i] != 0:
                miss_values.append((arr[i], values[i])) # to get a list of tuples of feature and number of missing values

        return render_template("results4.html", missing_values = miss_values)

# This is the page where we will handle the missing values of the dataset
@app.route("/handle_miss_value", methods = ["GET", "POST"])
def handle_mis():
    df = pd.read_csv("winequalityN.csv")
    if request.method == "POST" and "handling_values" in request.form:
        # case 1 : when there are too many rows in dataset as compared to columns
        # we will proceed by deleting them

        rc = df.shape
        if rc[0] - df.dropna().shape[0] < 0.05*rc[0]:  # if the number of rows with missing values is less than 5% of total rows
            df = df.dropna()
            return render_template("results5.html", more_rows = "Missing Values have been dropped by dropping rows")
        else:
            # Here we will replace the missing values with the mean if outliers are not present
            # We dont need to find outliers as we will just see difference between mean and median
            values = list(df.isnull().sum())
            arr = list(df.columns)
            for i in range(len(values)):
                if values[i] != 0 and (arr[i].dtype == "float64" or arr[i].dtype == "int64"):
                    if df[arr[i]].mean() - df[arr[i]].median() < 2:
                        df[arr[i]] = df[arr[i]].fillna(df[arr[i]].mean())
                        return render_template("results5.html", no_outliers = "Missing Values have been replaced with mean because no outliers were present")
                    else:
                        df[arr[i]] = df[arr[i]].fillna(df[arr[i]].median())
                        return render_template("results5.html", outliers = "Missing Values have been replaced with median because outliers were present")

    return render_template("results5.html", no_missing_values = "No Missing Values were present")

# here we will count how many categorical features are present
@app.route("/categorical_feature", methods = ["GET", "POST"])
def handle():
        if request.method == "POST" and "cat_feature" in request.form:
            cat_arr = df.select_dtypes(include=['object']).columns.tolist() # list of Categorical Features
            if len(cat_arr) == 0:
                return render_template("results6.html", x = "No Categorical Features were present")
            else:
                return render_template("results6.html", x = f"{len(cat_arr)} Categorical Features were present and these are {cat_arr}")

# Now we will do OrdinalEncoding on the categorical features
@app.route("/ordinal_encoding", methods = ["GET", "POST"])
def ord_enc():
    if request.method == "POST" and "encode" in request.form:
        from sklearn.preprocessing import OrdinalEncoder
        encoder = OrdinalEncoder()
        cat_arr = df.select_dtypes(include=['object']).columns.tolist()
        for i in cat_arr:
            df[i] = encoder.fit_transform(df[[i]]).astype(int)
        return render_template("results7.html", oe = "Ordinal Encoding has been done on the Categorical Features")

# And we are not adding any king of upsampling and downsampling techniques due to lack of time
# Here we are making an assumption that last feature is the target feature

# This Sets Dataset target variable as Wine Quality
@app.route("/set_target", methods = ["GET", "POST"])
def taregt():
    if request.method == "POST" and "target" in request.form:
        global X, y
        # Independent Features
        X = df.drop(df.columns[-1], axis = 1)

        # Target Feature
        y = df[df.columns[-1]]

        return render_template("results8.html", target = "Target Feature has been set as Wine Quality")

# Now Feature Selection, checking if there are any two independent features which are highly correlated
# we will take only one of them and drop the other one, we keep threshold as 0.8
@app.route("/feature_selection", methods = ["GET", "POST"])
def feature_select():

    if request.method == "POST" and "f_select" in request.form:
        X = df.drop(df.columns[-1], axis = 1)
        dataframe = X
        threshold = 0.8

        # Correlation matrix
        corr_matrix = dataframe.corr().abs() # absolute value of correlation matrix

        # Creating a set to hold the correlated features
        corr_features = set()

        # Looping through Each Feature
        for i in range(len(corr_matrix.columns)):
            for j in range(i):

                # Check if the correlation between two features is greater than threshold
                if corr_matrix.iloc[i, j] >= threshold:
                    colname = corr_matrix.columns[i]
                    corr_features.add(colname)  # we need only 1 feature out of the two highly correlated features

        # Dropping the correlated features
        X = X.drop(labels = corr_features, axis = 1)
        X = X.dropna()
        return render_template("results9.html", feature = "Feature Selection has been done")

# Here we will import standard scaler
@app.route("/standard_scaler", methods = ["GET", "POST"])
def scaler():
    global scaler
    if request.method == "POST" and "std_scaler" in request.form:
        from sklearn.preprocessing import StandardScaler
        scaler = StandardScaler()
        return render_template("results10.html", scaler = "Standard Scaler has been imported")

# Here we will split the dataset into train and test
# Make sure to first set the target feature and then split the dataset
@app.route("/train_test_split", methods = ["GET", "POST"])
def train_test():
    global X_test, X_train
    if request.method == "POST" and "split" in request.form:
        global X_train, X_test, y_train, y_test
        from sklearn.model_selection import train_test_split
        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 42)
        trained_size = X_train.shape
        test_size = X_test.shape

    return render_template("results11.html", trained = "Dataset has been split into train and test", trained_2 = "Now you can train the model"
                           , trained_3 = f"Train Size is {trained_size} and Test Size is {test_size}")

# now we will do standard scaling on the dataset
@app.route("/scaling", methods = ["GET", "POST"])
def transform():
    global X_train_scaled, X_test_scaled
    if request.method == "POST" and "scale" in request.form:
        from sklearn.preprocessing import StandardScaler
        scaler = StandardScaler()
        X_train_scaled = scaler.fit_transform(X_train)
        X_test_scaled = scaler.transform(X_test)
        X_train_scaled = np.nan_to_num(X_train_scaled, nan=np.nanmean(X_train_scaled))
        X_test_scaled = np.nan_to_num(X_test_scaled, nan=np.nanmean(X_test_scaled))

    return render_template("results12.html", scaled = "Standard Scaling has been done on the dataset")

#1 Linear Regression Training
@app.route("/linear_regression", methods = ["GET", "POST"])
def lin_reg():
    global regressor
    if request.method == "POST" and "linear" in request.form:
        from sklearn.linear_model import LinearRegression
        regressor = LinearRegression()
        regressor.fit(X_train_scaled, y_train)
    return render_template("results13.html", linear_reg = "Linear Regression Model has been trained")

#2 Ridge Regression Training
@app.route("/ridge_regression", methods = ["GET", "POST"])
def ridge_reg():
    global ridge_regressor
    if request.method == "POST" and "ridge" in request.form:
        from sklearn.linear_model import Ridge
        ridge_regressor = Ridge()
        ridge_regressor.fit(X_train_scaled, y_train)
    return render_template("results14.html", ridge_reg = "Ridge Regression Model has been trained")

#3 Lasso Regression Training
@app.route("/lasso_regression", methods = ["GET", "POST"])
def lasso_reg():
    global lasso_regressor
    if request.method == "POST" and "lasso" in request.form:
        from sklearn.linear_model import Lasso
        lasso_regressor = Lasso()
        lasso_regressor.fit(X_train_scaled, y_train)
    return render_template("results15.html", lasso_reg = "Lasso Regression Model has been trained")

#4 Elastic Net Regression Training
@app.route("/elastic_net_regression", methods = ["GET", "POST"])
def elastic_reg():
    global elastic_regressor
    if request.method == "POST" and "elastic" in request.form:
        from sklearn.linear_model import ElasticNet
        elastic_regressor = ElasticNet()
        elastic_regressor.fit(X_train_scaled, y_train)
    return render_template("results16.html", elastic_reg = "Elastic Net Regression Model has been trained")

#5 Logistic Regression Training
@app.route("/logistic_regression", methods = ["GET", "POST"])
def log_reg():
    global log_regressor
    if request.method == "POST" and "logistic" in request.form:
        from sklearn.linear_model import LogisticRegression
        log_regressor = LogisticRegression()
        log_regressor.fit(X_train_scaled, y_train)
    return render_template("results17.html", log_reg = "Logistic Regression Model has been trained")

#6 Decision Tree Classifier Training
@app.route("/decision_tree_classifier", methods = ["GET", "POST"])
def dec_tree():
    global dec_tree_classifier
    if request.method == "POST" and "decision" in request.form:
        from sklearn.tree import DecisionTreeClassifier
        dec_tree_classifier = DecisionTreeClassifier()
        dec_tree_classifier.fit(X_train_scaled, y_train)
    return render_template("results18.html", decs_tree = "Decision Tree Classifier Model has been trained")

#7 Decision Tree Regressor Training
@app.route("/decision_tree_regressor", methods = ["GET", "POST"])
def dec_tree_reg():
    global dec_tree_regressor
    if request.method == "POST" and "decision_regressor" in request.form:
        from sklearn.tree import DecisionTreeRegressor
        dec_tree_regressor = DecisionTreeRegressor()
        dec_tree_regressor.fit(X_train_scaled, y_train)
    return render_template("results19.html", decs_tree_reg = "Decision Tree Regressor Model has been trained")

#8 Support Vector Classifier Training
@app.route("/support_vector_classifier", methods = ["GET", "POST"])
def svc():
    global svc_classifier
    if request.method == "POST" and "support" in request.form:
        from sklearn.svm import SVC
        svc_classifier = SVC()
        svc_classifier.fit(X_train_scaled, y_train)
    return render_template("results20.html", svc = "Support Vector Classifier Model has been trained")

#9 Support Vector Regressor Training
@app.route("/support_vector_regressor", methods = ["GET", "POST"])
def svr():
    global svr_regressor
    if request.method == "POST" and "support_regressor" in request.form:
        from sklearn.svm import SVR
        svr_regressor = SVR()
        svr_regressor.fit(X_train_scaled, y_train)
    return render_template("results21.html", svr = "Support Vector Regressor Model has been trained")

#10 Naive Bayes Classifier Training
@app.route("/naive_bayes_classifier", methods = ["GET", "POST"])
def naive():
    global naive_classifier
    if request.method == "POST" and "naive" in request.form:
        from sklearn.naive_bayes import GaussianNB
        naive_classifier = GaussianNB()
        naive_classifier.fit(X_train_scaled, y_train)
    return render_template("results22.html", naive = "Naive Bayes Classifier Model has been trained")

#11 Random Forest Classifier Training
@app.route("/random_forest_classifier", methods = ["GET", "POST"])
def random():
    global random_classifier
    if request.method == "POST" and "random" in request.form:
        from sklearn.ensemble import RandomForestClassifier
        random_classifier = RandomForestClassifier()
        random_classifier.fit(X_train_scaled, y_train)
    return render_template("results23.html", random = "Random Forest Classifier Model has been trained")

#12 AdaBoost Classifier Training
@app.route("/adaboost_classifier", methods = ["GET", "POST"])
def adaboost():
    global adaboost_classifier
    if request.method == "POST" and "adaboost" in request.form:
        from sklearn.ensemble import AdaBoostClassifier
        adaboost_classifier = AdaBoostClassifier()
        adaboost_classifier.fit(X_train_scaled, y_train)
    return render_template("results24.html", adaboost = "AdaBoost Classifier Model has been trained")

# All Predictions at one place
@app.route("/prediction", methods = ["GET", "POST"])
def predict():
    global y_pred_linear, y_pred_lasso, y_pred_adaboost, y_pred_dec_tree, y_pred_dec_tree_reg, y_pred_elastic, y_pred_log, y_pred_random, y_pred_ridge,y_pred_support, y_pred_support_reg, y_pred_naive

    if request.method == "POST" and "predict" in request.form:

        #1 Linear Regression Prediction
        y_pred_linear = regressor.predict(X_test_scaled)

        #2 Ridge Regression Prediction
        y_pred_ridge = ridge_regressor.predict(X_test_scaled)

        #3 Lasso Regression Prediction
        y_pred_lasso = lasso_regressor.predict(X_test_scaled)

        #4 Elastic Net Regression Prediction
        y_pred_elastic = elastic_regressor.predict(X_test_scaled)

        #5 Logistic Regression Prediction
        y_pred_log = log_regressor.predict(X_test_scaled)

        #6 Decision Tree Classifier Prediction
        y_pred_dec_tree = dec_tree_classifier.predict(X_test_scaled)

        #7 Decision Tree Regressor Prediction
        y_pred_dec_tree_reg = dec_tree_regressor.predict(X_test_scaled)

        #8 Support Vector Classifier Prediction
        y_pred_support = svc_classifier.predict(X_test_scaled)

        #9 Support Vector Regressor Prediction
        y_pred_support_reg = svr_regressor.predict(X_test_scaled)

        #10 Naive Bayes Classifier Prediction
        y_pred_naive = naive_classifier.predict(X_test_scaled)

        #11 Random Forest Classifier Prediction
        y_pred_random = random_classifier.predict(X_test_scaled)

        #12 AdaBoost Classifier Prediction
        y_pred_adaboost = adaboost_classifier.predict(X_test_scaled)

        return render_template("results25.html",predict = "All Predictions have been done")

# All Accuracy Scores at one place
@app.route("/accuracy", methods = ["GET", "POST"])
def accuracy():

    if request.method == "POST" and "accuracy" in request.form:

        #1 Linear Regression Accuracy
        from sklearn.metrics import r2_score
        r2_linear = r2_score(y_test, y_pred_linear)

        #2 Ridge Regression Accuracy
        r2_ridge = r2_score(y_test, y_pred_ridge)

        #3 Lasso Regression Accuracy
        r2_lasso = r2_score(y_test, y_pred_lasso)

        #4 Elastic Net Regression Accuracy
        r2_elastic = r2_score(y_test, y_pred_elastic)

        #5 Logistic Regression Accuracy
        from sklearn.metrics import accuracy_score
        accuracy_log = accuracy_score(y_test, y_pred_log)

        #6 Decision Tree Classifier Accuracy
        accuracy_dec_tree = accuracy_score(y_test, y_pred_dec_tree)

        #7 Decision Tree Regressor Accuracy
        from sklearn.metrics import r2_score
        r2_dec_tree_reg = r2_score(y_test, y_pred_dec_tree_reg)

        #8 Support Vector Classifier Accuracy
        accuracy_support = accuracy_score(y_test, y_pred_support)

        #9 Support Vector Regressor Accuracy
        from sklearn.metrics import r2_score
        r2_support_reg = r2_score(y_test, y_pred_support_reg)

        #10 Naive Bayes Classifier Accuracy
        accuracy_naive = accuracy_score(y_test, y_pred_naive)

        #11 Random Forest Classifier Accuracy
        accuracy_random = accuracy_score(y_test, y_pred_random)

        #12 AdaBoost Classifier Accuracy
        accuracy_adaboost = accuracy_score(y_test, y_pred_adaboost)

        max_accuracy = max(accuracy_log, accuracy_dec_tree, accuracy_support, accuracy_naive, accuracy_random, accuracy_adaboost)
        return render_template("results26.html",linea_reg = r2_linear, ridg_reg = r2_ridge, lass_reg = r2_lasso, elasti_reg = r2_elastic, lo_reg = accuracy_log, de_tree = accuracy_dec_tree, de_tree_reg = r2_dec_tree_reg, suppor_vec = accuracy_support, suppor_vec_reg = r2_support_reg, naiv = accuracy_naive, rando = accuracy_random, adaboos = accuracy_adaboost, x = max_accuracy)

# Hyperparameter Tuning
# Find Best parameters that will fit all 12 models at once
# find parameters using RandomizedSearchCV

if __name__=="__main__":
    app.run(host="0.0.0.0")


from win32com.client import Dispatch
from tkinter import *
import tkinter as tk
from PIL import Image
from PIL import ImageTk
import os
import re
import random
from threading import Thread
import pythoncom
import time

stu_path = "名单.txt"  # 学生名单路径

def speaker(str):
    """
    语音播报
    :param str: 需要播放语音的文字
    """
    speaker = Dispatch("SAPI.SpVoice")
    speaker.Speak(str)

class Rollllcall():
    def __init__(self):
        self.win = Tk()
        self.win.title("Python课堂点名器")
        self.win.iconbitmap("image/icon.ico")
        self.win.geometry("750x450")
        self.win.resizable(False, False)  # 不允许放大窗口，避免放大导致布局变形带来的麻烦
        self.start = False  # 开始按钮的状态
        # 增加背景图片
        img = Image.open('image/back.jpg')
        img = ImageTk.PhotoImage(img, size=(650, 450))
        theLabel = tk.Label(self.win,  # 绑定到一个框架
                            # justify=tk.LEFT,  # 对齐方式
                            image=img,  # 加入图片
                            compound=tk.CENTER,  # 关键:设置为背景图片
                            font=("华文行楷", 20),  # 字体和字号
                            fg="white",
                            )  # 前景色
        theLabel.place(x=0, y=0, relwidth=1, relheight=1)
        self.var = tk.StringVar()  # 储存文字的类
        self.var.set("别紧张")  # 设置文字
        NameLabel = tk.Label(self.win, textvariable=self.var,  # 绑定到一个框架
                             justify=tk.LEFT,  # 对齐方式
                             compound=tk.CENTER,  # 关键:设置为背景图片
                             font=("华文行楷", 35),  # 字体和字号
                             fg="SeaGreen",
                             width=10,
                             )  # 前景色
        NameLabel.place(x=280, y=100)

        # 多选框
        self.checkVar = IntVar()
        Checkbutton(self.win, text="语音播放", variable=self.checkVar,
                    onvalue=1, offvalue=0, height=0, width=0).place(x=170, y=410)
        tk.Button(self.win, text='编辑学生名单', height=0, width=0, command=self.pop_win).place(x=520, y=408)

        self.theButton = tk.Button(self.win, text="开始", font=("华文行楷", 13), fg="SeaGreen", width=20,
                                   command=self.callback)
        self.theButton.place(x=300, y=360)  # 调整按钮的位置
        self.win.mainloop()

    def save_names(self, pop, t):
        """
        保存名单内容
        :param win: #弹出窗
        :param t: 文本框对象

        """
        names = t.get(0.0, "end")
        if re.search("，", names):
            textlabel = tk.Label(pop, text="注意:名单不能使用中文逗号分隔", font=("华文行楷", 12),  # 字体和字号
                                 fg="red", )
            textlabel.place(y=190, x=10)
        else:
            with open(stu_path, "w", encoding="utf-8") as f:
                f.write(names)
            pop.destroy()

    # 编辑学生姓名
    def pop_win(self):
        pop = Tk(className='学生名单编辑')  # 弹出框框名
        pop.geometry('450x250')  # 设置弹出框的大小 w x h
        pop.iconbitmap("image/icon.ico")
        pop.resizable(False, False)

        # 用来编辑名单的文本框
        t = tk.Text(pop, width=61, height='10')
        t.place(x=10, y=10)
        # 判断文件存不存在
        result = os.path.exists(stu_path)
        if result:
            # 存在
            with open(stu_path, "r", encoding='utf-8') as f:
                names = f.read().strip("\n\r\t")
                t.insert("end", names)

        textlabel = tk.Label(pop, text="学生名单请以,(英文状态)的逗号分隔：\n如：刘亦菲,周迅", font=("华文行楷", 12),  # 字体和字号
                             fg="SeaGreen", )
        textlabel.place(y=150, x=10)

        # 点击确定保存数据
        tk.Button(pop, text='确定', height=0, width=0, command=lambda: self.save_names(pop, t)).place(y=200, x=340)
        tk.Button(pop, text='取消', height=0, width=0, command=pop.destroy).place(y=200, x=400)

    def callback(self):
        # 改变开始按钮的状态
        self.start = False if self.start else True
        # 开始随机名单之后修改按钮上的文字
        self.theButton["text"] = "就你了"
        # 开启一个子线程去做操作
        self.t = Thread(target=self.mod_stu_name, args=(self.var, self.checkVar))
        self.t.start()

    def mod_stu_name(self, var, checkVar):
        # 随机读取名单中的一个
        pythoncom.CoInitialize()  # 子线程中调用win32com 语音播放需要设置这一行
        if not os.path.exists(stu_path):
            var.set("请添加名单")
            return None
        with open(stu_path, "r", encoding="utf-8") as f:
            names = f.read().strip("\n\t\r,")
        if not names:
            var.set("请添加名单")
            return None
        name_list = names.split(",")

        random_name = ""
        while self.start:
            random_name = random.choice(name_list)
            var.set(random_name)  # 设置名字随机出现
            time.sleep(0.1)
        self.theButton["text"] = "开始"  # 选中之后将按钮重新修改成 开始
        # 语音播报
        if checkVar.get() == 1:
            speaker(random_name)

if __name__ == '__main__':
    Rollllcall()


from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings

# set the default Django settings module for the 'celery' program.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "applifting.settings")
app = Celery("applifting")

# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object("django.conf:settings")
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

@app.on_after_finalize.connect
def setup_periodic_tasks(sender, **kwargs):
    sender.add_periodic_task(
        60.0,
        snapshot_offer_pricestamps.s(),
        name="get_offer_pricestamps_for_all_products",
    )

@app.task(bind=True)
def debug_task(self):
    print(f"Request: {self.request!r}")

@app.task
def snapshot_offer_pricestamps():
    from catalog.tasks import get_offer_pricestamps_for_all_products

    return get_offer_pricestamps_for_all_products()


import os
import shlex
import subprocess
import datetime
import time
import shutil
from setuptools import setup, Extension

cwd = os.path.dirname(os.path.abspath(__file__))

def execute_command(cmdstring, cwd=None, timeout=None, shell=False):
    if shell:
        cmdstring_list = cmdstring
    else:
        cmdstring_list = shlex.split(cmdstring)
    if timeout:
        end_time = datetime.datetime.now() + datetime.timedelta(seconds=timeout)

    sub = subprocess.Popen(cmdstring_list, cwd=cwd, stdin=subprocess.PIPE,shell=shell,bufsize=4096)

    while sub.poll() is None:
        time.sleep(0.1)
        if timeout:
            if end_time <= datetime.datetime.now():
                raise Exception("Timeout: %s"%cmdstring)

    return sub.returncode

def build_library():
    config_command = "cmake -S {} -B {}"
    path_to_source = cwd
    path_to_build = os.path.join(cwd, "build")

    if os.path.exists(path_to_build):
        shutil.rmtree(path_to_build)
    config_command = config_command.format(path_to_source, path_to_build)

    code = execute_command(config_command)
    if code != 0:
        raise RuntimeError("Run configure command fail.")

    build_command = "cmake --build {}".format(os.path.join(cwd, "build"))
    code = execute_command(build_command)
    if code != 0:
        raise RuntimeError("Run build Command fail.")

def main():
    build_library()
    extention = Extension(
        "opqr",
        libraries=["opqr"],
        sources=["stub.cpp"],
        language="c++",
        extra_compile_args=['-std=c++17'],
        include_dirs=[cwd],
        library_dirs=[os.path.join(cwd, "build")]
    )
    setup(name="opqr",
          version="1.1.3",
          long_description="A Simple QR encode Library.",
          description="A Simple QR encode Library.",
          author="caozhanhao",
          author_email="cao2013zh@163.com",
          ext_modules=[extention]
    )

if __name__ == "__main__":
    main()


"""
FILE: kernelregression.py
LAST MODIFIED: 24-12-2015
DESCRIPTION: Module for Gaussian kernel regression

===============================================================================
This file is part of GIAS2. (https://bitbucket.org/jangle/gias2)

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
===============================================================================
"""
import logging

import numpy as np

log = logging.getLogger(__name__)

def _gaussianKernel(T, t, s):
    c1 = 1.0 / (s * np.sqrt(2 * np.pi))
    c2 = np.exp(-(T - t) ** 2.0 / (2 * s * s))
    return c1 * c2

def _gaussianKernelMax(s):
    return 1.0 / (s * np.sqrt(2 * np.pi))

def _weightedMean(x, w):
    # print x
    # print x.shape
    # print w.shape
    return (x * w).sum(-1) / w.sum()

def _weightedSD(x, w):
    u = _weightedMean(x, w)
    d = ((x.T - u).T) ** 2.0
    return np.sqrt((d * w).sum(-1) / w.sum())

class KernelRegressor(object):
    sigmaeps = 1e-6

    def __init__(self, k=2, sigma0=1.0, wmin=0.35):
        self.k = k
        self.sigma0 = sigma0
        self.wmin = wmin  # kernel weight cutoff

        self.nTarg = None  # target number of obs at each time point
        self.sigmas = None
        self.n = None
        self.y = None
        self.ytmeans = None
        self.ytSDs = None
        self.ytweights = None
        self.ytinds = None
        self.x = None
        self.xsamples = None
        self.xt = None
        self.xtn = None
        self.xtnTarg = None
        self.xmin = None
        self.xmax = None

    def fit(self, x, y, xmin, xmax, xsamples):
        """
        x is a 1-d array, the independent variable e.g. time

        if y is multivariate, each observation should be a column, so variables
        in rows.
        """
        self.x = x
        self.y = y
        self.xmin = xmin
        self.xmax = xmax
        self.sigmaeps = 1e-3 * (xmax - xmin)
        self.xsamples = xsamples
        self.xt = np.linspace(self.xmin, self.xmax, self.xsamples)

        self._fitInit()
        self._optWidths()

        return self.ytmeans

    def _fitInit(self):

        # initialise kernel width at each time point
        self.sigmas = np.ones(self.xsamples) * self.sigma0

        # calc number of obs at each y sampling (yt[i])
        self.xtn = []
        self.ytmeans = []
        self.ytweights = []
        self.ytSDs = []
        self.ytinds = []
        for t, s in zip(self.xt, self.sigmas):
            ty, tw, tyi = self._getKernelY(t, s)
            self.xtn.append(len(tw))
            self.ytweights.append(tw)
            self.ytinds.append(tyi)
            self.ytmeans.append(_weightedMean(ty, tw))
            self.ytSDs.append(_weightedSD(ty, tw))

        self.ytmeans = np.array(self.ytmeans)

        # calculate target number obs per time point
        self.xtnTarg = np.median(self.xtn)

    def _optWidths(self):
        for i in range(self.xsamples):
            tyi = None
            change = 0
            if self.xtn[i] > (self.xtnTarg + self.k):
                while self.xtn[i] > (self.xtnTarg + self.k):
                    self.sigmas[i] -= self.sigmaeps
                    ty, tw, tyi = self._getKernelY(self.xt[i], self.sigmas[i])
                    self.xtn[i] = len(tw)
                change = 1

            if self.xtn[i] < (self.xtnTarg - self.k):
                while self.xtn[i] < (self.xtnTarg - self.k):
                    self.sigmas[i] += self.sigmaeps
                    ty, tw, tyi = self._getKernelY(self.xt[i], self.sigmas[i])
                    self.xtn[i] = len(tw)
                change = 1

            # calculated weighted mean
            # if tyi is not None:
            #   print self.xt[i], tyi
            # else:
            #   print self.xt[i], 'no opt'
            if change:
                self.ytmeans[i] = _weightedMean(ty, tw)
                self.ytSDs[i] = _weightedSD(ty, tw)
                self.ytweights[i] = tw
                self.ytinds[i] = tyi

    def _getKernelY(self, t, s):
        xw = _gaussianKernel(self.x, t, s)
        validMask = xw >= (self.wmin * _gaussianKernelMax(s))
        validW = xw[validMask]
        validY = self.y[:, validMask]
        return validY, validW, np.where(validMask)[0]

def test():
    ndata = 500
    x = 3.0 * np.random.rand(ndata)
    y = np.sin(x) + 0.1 * (np.random.rand(ndata) - 0.5)

    r = KernelRegressor(k=2, sigma0=0.05, wmin=0.35)
    r.fit(x, y, 0.1, 2.9, 10)

    log.debug('xt      sigma     n     y_mean')
    for xt, s, n, ym in zip(r.xt, r.sigmas, r.xtn, r.ytmeans):
        log.debug(('{:4.3f}   {:4.3f}   {:4d}    {:4.3f}').format(xt, s, n, ym))

    import matplotlib.pyplot as plt
    f = plt.figure()
    plt.scatter(x, y)
    plt.plot(r.xt, r.ytmeans, 'r')
    plt.plot(r.xt, r.ytmeans + r.ytSDs, 'g--')
    plt.plot(r.xt, r.ytmeans - r.ytSDs, 'g--')
    plt.show()

if __name__ == '__main__':
    test()


import heapq
import sys
from typing import (
    Generic,
    Iterable,
    Iterator,
    List,
    NamedTuple,
    Optional,
    Set,
    Tuple,
    TypeVar,
)

from termcolor import cprint

from aoc.utils import Coord2D, Grid

H = TypeVar(
    "H",
    # technically can be anything comparible but obviously python's type system
    # doesn't support that so let's go with numbers because that's all I'm using
    int,
    float,
)
V = TypeVar("V")

class WorkingSet(Generic[H, V]):
    heap: List[Tuple[H, V]]
    heapset: Set[V]

    def __init__(self) -> None:
        self.heap = []
        self.heapset = set()

    def add(self, value: V, heuristic: H) -> None:
        self.heapset.add(value)
        heapq.heappush(self.heap, (heuristic, value))

    def replace(self, value: V, heuristic: H) -> None:
        self.heap = [val for val in self.heap if val[1] != value]
        heapq.heappush(self.heap, (heuristic, value))

    def add_or_replace(self, value: V, heuristic: H) -> None:
        if value in self.heapset:
            self.replace(value, heuristic)
        else:
            self.add(value, heuristic)

    def pop(self) -> V:
        _, value = heapq.heappop(self.heap)
        self.heapset.remove(value)
        return value

    def __contains__(self, value: V) -> bool:
        return value in self.heapset

    def __len__(self) -> int:
        return len(self.heap)

    def __bool__(self) -> bool:
        return bool(self.heap)

class Chiton(NamedTuple):
    risk: int
    cheapest_cost: int
    cheapest_neighbour: Optional[Coord2D]

class ChitonCave(Grid[Chiton]):
    _goal: Coord2D

    @classmethod
    def parse(cls, data: Iterator[str]):
        ret = cls()
        ret._fill(
            (
                (
                    Chiton(
                        risk=int(risk),
                        cheapest_cost=sys.maxsize,
                        cheapest_neighbour=None,
                    )
                    for risk in line
                )
                for line in data
            )
        )
        for y in range(6):
            for x in range(6):
                if x == 0 and y == 0:
                    continue
                for coord in ret._iterate_grid():
                    newcoord = Coord2D(
                        x=coord.x + ret.width * x,
                        y=coord.y + ret.height * y,
                    )
                    existing = ret[coord]
                    ret[newcoord] = existing._replace(
                        risk=((existing.risk + x + y) % 9) or 9
                    )
        ret.width *= 5
        ret.height *= 5

        ret._goal = Coord2D(ret.width - 1, ret.height - 1)
        return ret

    def _heuristic(self, coord: Coord2D) -> int:
        return coord.distance(self._goal)

    def _get_path(self) -> List[Coord2D]:
        current = self._goal
        path: List[Coord2D] = []
        while current:
            path.append(current)
            _, _, current = self[current]
        return path

    def print(self, path: Optional[Iterable[Coord2D]] = None) -> None:
        if path:
            pathset = set(path)
        else:
            pathset = set((Coord2D(0, 0), self._goal))

        for y in range(self.height):
            for x in range(self.width):
                coord = Coord2D(x, y)
                risk, _, _ = self[coord]
                if coord in pathset:
                    cprint(str(risk), attrs=["bold"], end="")
                else:
                    cprint(str(risk), attrs=["dark"], end="")

            print()
        print()

    def a_star(self) -> List[Coord2D]:
        # self.print()

        start = Coord2D(0, 0)
        self[start] = self[start]._replace(cheapest_cost=0)

        working_set = WorkingSet[int, Coord2D]()
        working_set.add(start, self._heuristic(start))

        while working_set:
            coord = working_set.pop()
            if coord == self._goal:
                path = self._get_path()
                self.print(path)
                return path
            _, cost, _ = self[coord]

            for neighbour in self._get_neighbours(coord, diagonal=False):
                neighbour_risk, neighbour_cost, _ = self[neighbour]

                new_cost = cost + neighbour_risk
                if new_cost < neighbour_cost:
                    self[neighbour] = Chiton(neighbour_risk, new_cost, coord)
                    working_set.add_or_replace(
                        neighbour,
                        new_cost + self._heuristic(neighbour),
                    )

        raise RuntimeError("Could not find goal?!")

def main(data: Iterator[str]) -> None:
    grid = ChitonCave.parse(data)

    path = grid.a_star()

    total_risk = 0
    start = Coord2D(0, 0)
    for coord in path:
        if coord != start:
            risk, _, _ = grid[coord]
            total_risk += risk

    print(total_risk)


import os
from time import sleep, time
import sys
import threading
from threading import Thread
from _thread import interrupt_main
from signal import signal
from signal import SIGINT
lst = []

def star(s):
    result = ""
    for i in range(len(s)):
        if (i % 3 == 2):
            result += "*"
        result += s[i]
    print(result)

s = "The eye is a region of mostly calm weather at the center of a tropical cyclone."

tm = time()
star(s)

pid = os.fork()
if pid > 0:
    #os.execl()
    print("Process ID:", os.getpid())
    print("Child's process ID:", pid)
    print(time() - tm)

else:
    #os.execl()
    print("Process ID:", os.getpid())
    print(time() - tm)


import numpy as np
import pandas as pd
from sklearn.svm import LinearSVC
from sklearn.feature_extraction.text import TfidfTransformer, CountVectorizer
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV

train_labels = pd.read_csv('../resources/train_labels.csv', names=['label'], header=None)
train_examples = pd.read_csv('../resources/train_examples.csv', names=['example'], engine='python', header=None, delimiter='\t\n')
test_examples = pd.read_csv('../resources/test_examples.csv', names=['example'], engine='python', header=None, delimiter='\t\n')

X = train_examples.example
y = train_labels.label

pipeline = Pipeline([('vectorizer', CountVectorizer()),
                     ('tfidf', TfidfTransformer()),
                     ('classifier', LinearSVC(loss='squared_hinge', class_weight='balanced', max_iter=10000))
                     ])
param_grid = {'classifier__C': np.arange(0.1,10,0.5)}

search = GridSearchCV(pipeline, param_grid=param_grid, iid=True, cv=5,  n_jobs=-1,refit=True)
search.fit(X, y)

print("\nBEST SCORE", search.best_score_)
print("BEST PARAMETER", search.best_params_)


"""
ProgramTwo.py
Implementation for programming assignment two of Artificial Intelligence.
"""

import numpy as np
import pandas as pd
from prog2.Environment import Environment
from prog2.UniformCostAgent import UniformCostAgent
__author__ = "Chris Campell"
__version__ = "9/18/2017"

def main(training_file):
    df = None
    with open(training_file, 'r') as fp:
        df = pd.read_csv(fp)
    for i, row in enumerate(df.iterrows()):
        train_id = df['Id'][i]
        train_board = df['board'][i]
        pet_world = Environment(puzzle_id=train_id, game_board=train_board)
        uniform_pet_agent = UniformCostAgent(location=pet_world.agent_location, pets_in_car=[],
                                     pets_on_street=pet_world.get_street_pet_locations(), environment_instance=pet_world)
        solution = uniform_pet_agent.uniform_cost_search()
        failure = False
        while not failure:
            if len(pet_agent.frontier) == 0:
                failure = True
                break
            # select lowest cost node in frontier:

if __name__ == '__main__':
    training_data = ['../prog2/train/lumosity_breadth_first_search_train.csv']
    main(training_file=training_data[0])


import re
import logging

logging.basicConfig(level=logging.INFO)

logger = logging.getLogger(__name__)

if __name__ == "__main__":

    test_string = "+375 (29) 299-00-00"
    match = re.search(r"^\+\d{1,3}\s\(\d{2}\)\s\d{3}\-\d{2}\-\d{2}$", test_string)

    if match:
        logger.info(f"Found {match.group()}")
    else:
        logger.info("Didn't not found")


#!/usr/bin/python3.5

import threading
import time

class MyThread(threading.Thread):
    def __init__(self,name,run_time):
        super(MyThread,self).__init__()
        self.name = name
        self.run_time = run_time
    def run(self):
        print("running task:",self.name)
        time.sleep(self.run_time)
        a = time.time()
        print("task %s done" %self.name)
        b = time.time() -a
        print(b)
thread_instance = [MyThread(i,1)for i in range(8)]

for a in thread_instance:
    a.start()


import csv
import models
from operator import attrgetter
import statistics

def filter_data(columns, row):
    ENERGYSTARScore = row[columns.index('ENERGYSTARScore')]
    if ENERGYSTARScore == '':
        return False

    YearBuilt = row[columns.index('YearBuilt')]
    if int(YearBuilt) < 1920:
        return False

    BuildingName = row[columns.index('BuildingName')]
    if 'CENTER' in BuildingName:
        return False

    return True

def parse_file(filename):
    print('Starting parsing file:')
    list_of_buildings = []
    # read file
    with open(filename) as file_raw:
        print(f'Opening file {filename}')
        data_raw = csv.reader(file_raw, delimiter=',')
        print('Iterating over each line in the file:')
        firstline = True
        columns = []
        for row in data_raw:
            if firstline:
                columns = row
                firstline = False
                continue
            # filter data
            if filter_data(columns, row) == False:
                continue

            OSEBuildingID = row[columns.index('OSEBuildingID')]
            # Question 1
            BuildingName = row[columns.index('BuildingName')]
            NumberofFloors = int(row[columns.index('NumberofFloors')])
            # Question 2
            ENERGYSTARScore = int(row[columns.index('ENERGYSTARScore')])
            SiteEUI = float(row[columns.index('SiteEUI(kBtu/sf)')])
            NaturalGas = row[columns.index('NaturalGas(kBtu)')]
            # Question 3
            Neighborhood = row[columns.index('Neighborhood')]
            Electricity = int(row[columns.index('Electricity(kBtu)')])
            # Question 4
            PropertyGFABuildings = int(
                row[columns.index('PropertyGFABuilding(s)')])
            PrimaryPropertyType = row[columns.index('PrimaryPropertyType')]
            LargestPropertyUseType = row[columns.index(
                'LargestPropertyUseType')]
            SecondLargestPropertyUseType = row[columns.index(
                'SecondLargestPropertyUseType')]
            ThirdLargestPropertyUseType = row[columns.index(
                'ThirdLargestPropertyUseType')]
            PropertyTypes = [
                PrimaryPropertyType,
                LargestPropertyUseType,
                SecondLargestPropertyUseType,
                ThirdLargestPropertyUseType]

            # instance a Building
            building = models.Building(
                OSEBuildingID,
                BuildingName,
                NumberofFloors,
                ENERGYSTARScore,
                SiteEUI,
                NaturalGas,
                Neighborhood,
                Electricity,
                PropertyGFABuildings,
                PropertyTypes)

            list_of_buildings.append(building)

    print('Finish parsing file and filter data.')
    print(f'list_of_buildings length is {len(list_of_buildings)}')
    return list_of_buildings

def find_buildings_with_largest_number_of_floors(
        list_of_buildings, max_number_of_floors):
    list_of_buildings_with_largest_number_of_floors = []
    for building in list_of_buildings:
        if building.NumberofFloors == max_number_of_floors:
            list_of_buildings_with_largest_number_of_floors.append(building)
            continue
        if building.NumberofFloors > max_number_of_floors:
            max_number_of_floors = building.NumberofFloors
            list_of_buildings_with_largest_number_of_floors.clear()
            list_of_buildings_with_largest_number_of_floors.append(building)
            continue
    return list_of_buildings_with_largest_number_of_floors

def question_1(list_of_buildings):
    print('Solving question 1:')
    max_number_of_floors = 0
    list_of_buildings_with_largest_number_of_floors = find_buildings_with_largest_number_of_floors(
        list_of_buildings, max_number_of_floors)
    print('Building(s) with the largest number of floors:')
    for building in list_of_buildings_with_largest_number_of_floors:
        print(
            'Id {0}, name {1}, with {2} floors.'.format(
                building.OSEBuildingID,
                building.BuildingName,
                building.NumberofFloors))

def question_2(list_of_buildings):
    print('Solving question 2:')
    energy_star_score_threshold = 97
    nums_of_building_energy = sum(
        building.ENERGYSTARScore >= energy_star_score_threshold for building in list_of_buildings)
    print(
        'The number of buildings with ENERGYSTARScore of at least {0} is {1}.'.format(
            energy_star_score_threshold,
            nums_of_building_energy))

def filter_natural_gas(list_of_buildings):
    print('Filtering buildings that used natural gas:')
    list_of_buildings_used_naturalgas = []
    for building in list_of_buildings:
        if building.NaturalGas:
            list_of_buildings_used_naturalgas.append(building)
    return list_of_buildings_used_naturalgas

def question_3(list_of_buildings):
    print('Solving question 3:')
    list_of_buildings_used_naturalgas = filter_natural_gas(list_of_buildings)
    list_of_gas_used = []
    for building in list_of_buildings_used_naturalgas:
        list_of_gas_used.append(building.SiteEUI)
    median_SiteEUI = statistics.median(list_of_gas_used)

    print(
        f'The median of SiteEUI among buildings using natural gas is {median_SiteEUI} kBtu.')

    # list_of_gas_used.sort()
    # n = len(list_of_gas_used)
    # if n % 2 != 0:
    #     median_SiteEUI = list_of_gas_used[int(n / 2)]
    # else:
    #     median_SiteEUI = (list_of_gas_used[int(n / 2)] + list_of_gas_used[int(n / 2 - 1)]) / 2
    # print(
    #     f'The median of SiteEUI among buildings using natural gas is {median_SiteEUI}.')

def filter_neighborhood(list_of_buildings, neighborhood):
    # neighborhood = 'BALLARD'
    list_of_buildings_in_neighborhood = []
    for building in list_of_buildings:
        if building.Neighborhood == neighborhood:
            list_of_buildings_in_neighborhood.append(building)
    return list_of_buildings_in_neighborhood

def get_threshold_building(filename, threshold_building_name):
    with open(filename) as file_raw:
        data_raw = csv.reader(file_raw, delimiter=',')
        firstline = True
        columns = []
        for row in data_raw:
            if firstline:
                columns = row
                firstline = False
                continue

            if threshold_building_name in row:
                threshold_building_electricity = int(
                    row[columns.index('Electricity(kBtu)')])
                print('Building {0} used the amount of Electricity of {1} kBtu.'.format(
                    threshold_building_name, threshold_building_electricity))
                return threshold_building_electricity

def filter_electricity_use(list_of_buildings, threshold_building_electricity):
    list_of_building_used_more_electricity = []
    for building in list_of_buildings:
        if building.Electricity > threshold_building_electricity:
            list_of_building_used_more_electricity.append(building)
    return list_of_building_used_more_electricity

def question_4(filename, list_of_buildings):
    print('Solving question 4:')
    neighborhood = 'BALLARD'
    threshold_building_name = 'BIOMED FAIRVIEW RESEARCH CENTER'

    threshold_building_electricity = get_threshold_building(
        filename, threshold_building_name)
    list_of_buildings_in_neighborhood = filter_neighborhood(
        list_of_buildings, neighborhood)
    list_of_buildings_used_more_electricity = filter_electricity_use(
        list_of_buildings_in_neighborhood, threshold_building_electricity)

    for building in list_of_buildings_used_more_electricity:
        print(
            'Building id {0}, name {1} used the amount of Electricity of {2} kBtu.'.format(
                building.OSEBuildingID,
                building.BuildingName,
                building.Electricity))

def filter_property_type_block_list(list_of_buildings, block_list):
    print('Filtering buildings that are not offices or hospitals:')
    list_of_buildings_not_block_listed = []
    for building in list_of_buildings:
        chosen = True
        for property_type in building.PropertyTypes:
            if not chosen:
                continue
            for block_type in block_list:
                if block_type in property_type:
                    chosen = False
                    break
        if chosen:
            list_of_buildings_not_block_listed.append(building)
    return list_of_buildings_not_block_listed

def filter_property_floor_area_building(
        list_of_buildings, floor_area_threshold):
    list_of_large_buildings = []
    for building in list_of_buildings:
        if building.PropertyGFABuildings > floor_area_threshold:
            list_of_large_buildings.append(building)
    return list_of_large_buildings

def question_5(list_of_buildings):
    print('Solving question 5:')
    football_field_area = 57600  # square feet
    floor_area_threshold = 15 * football_field_area
    print(f'Threshold property area is {floor_area_threshold} square feet.')

    block_list = ['Hospital', 'Office']
    list_of_buildings_not_offices_or_hospitals = filter_property_type_block_list(
        list_of_buildings, block_list)

    list_of_large_buildings = filter_property_floor_area_building(
        list_of_buildings_not_offices_or_hospitals, floor_area_threshold)

    for building in list_of_large_buildings:
        print('Building id {0}, name {1}, total floor area for buildings {2} square feet.'.format(
            building.OSEBuildingID, building.BuildingName, building.PropertyGFABuildings
        ))

def ada_data_challenge(filename):
    list_of_buildings = parse_file(filename)
    question_1(list_of_buildings)
    question_2(list_of_buildings)
    question_3(list_of_buildings)
    question_4(filename, list_of_buildings)
    question_5(list_of_buildings)


from turtle import Turtle
ALIGNMENT = "center"
FONT = ('Arial', 16, 'normal')

class Score(Turtle):
    def __init__(self):
        """
        Default constructor, constructs the object during it's declaration
        """
        super().__init__()
        self.score = 0
        self.penup()
        self.color("white")
        self.hideturtle()
        self.goto(0, 280)
        self.update_score_board()

    def update_score_board(self):
        """
        Makes the turtle to write the score board on the top part of the screen
        :return:
        """
        self.write(arg=f"Score: {self.score}", move=False, align=ALIGNMENT, font=FONT)

    def game_over(self):
        """
        This turtle will display the 'Game Over' at the centre of the screen when called
        :return:
        """
        self.goto(0,0)
        self.write(arg=f"Game Over", move=False, align=ALIGNMENT, font=FONT)

    def increase_score(self):
        """
        Increment the score whenever the snake ate the food and call the update the score board function
        :return:
        """
        self.score += 1
        self.clear()
        self.update_score_board()


import sys

try:
    x = int(input("x: "))
    y = int(input("y: "))
except ValueError:
    print("Erro: Invalid input")
    sys.exit(1) #0: salida limpia sin errores/problemas. 1: hubo algun problema y el programa se ciera

try:
    result = x / y
except ZeroDivisionError: #Si divido para cero
    print("Error: Cannot divide by 0.")
    sys.exit(1)

print(f"{x} / {y} = {result}")


import uiautomator2 as u2
import pytest
import allure
@allure.feature("测试首页")#类的主要测试部分
#@allure.environment(app_package='com.mobile.fm')# 具体Environment参数可自行设置
# @allure.environment(app_activity='com.mobile.fm.activity')
# @allure.environment(device_name='aad464')
# @allure.environment(platform_name='Android')

class TestInfo():
    '''
    Allure中对严重级别的定义：
    1、 Blocker级别：中断缺陷（客户端程序无响应，无法执行下一步操作）
    2、 Critical级别：临界缺陷（ 功能点缺失）
    3、 Normal级别：普通缺陷（数值计算错误）
    4、 Minor级别：次要缺陷（界面错误与UI需求不符）
    5、 Trivial级别：轻微缺陷（必输项无提示，或者提示不规范
    '''

    @allure.story('测试是否可以获取信息')  # 分支功能
    @allure.severity('blocker')
    @allure.step('')
    @allure.issue('www.baidu.com')
    @allure.testcase('www.baidu.com')

    def test_info(self):
        """
        我是一个用例描述，这个用例是用来获取Android信息的
        """
        d = u2.connect('emulator-5554')
        print(d.info)
        #d.app_install('https://imtt.dd.qq.com/16891/7E569C80A3714D58E77F6173EB8F6329.apk?fsname=com.netease.cloudmusic_5.7.2_130.apk&csr=1bbd')

    @allure.step("字符串相加：{0}，{1}")
    # 测试步骤，可通过format机制自动获取函数参数
    def str_add(str1, str2):
        if not isinstance(str1, str):
            return "%s is not a string" % str1
        if not isinstance(str2, str):
            return "%s is not a string" % str2
        return str1 + str2

    @allure.story('test_story_01')
    @allure.severity('blocker')
    def test_case(self):
        str1 = 'hello'
        str2 = 'world'
        assert self.str_add(str1, str2) == 'helloworld'
    #在报告中增加附件：allure.attach(’arg1’,’arg2’,’arg3’)：
    #file = open('../testfile/image/lqf.fpg', 'rb').read()
    #allure.attach('test_img', file, allure.attach_type.PNG)

if __name__ == '__main__':
    pytest.main(['-s', '-q', '--alluredir', '../report/xml'])


#!/usr/bin/env python3

import secrets
import sys
import subprocess
import argparse

import headerStruct

def calculateSizeOfTheImage(lastVirtAddr, size, SectAlignment):
    mult = int((size-1) / SectAlignment) + 1
    return lastVirtAddr + (SectAlignment * mult)

def generateKey(arch):
    # init values
    wordlength = 4
    mask = 0x000000FF
    if arch == 64:
        wordlength = 8
        mask = 0x00000000000000FF
    # kind of do while
    # generate random key until the two last digit != 00
    random = secrets.token_bytes(wordlength)
    while(int.from_bytes(random, 'big') & mask == 0):
        random = secrets.token_bytes(wordlength)
    return random

def hashing(data, arch): # TODO implement a real hashing function
    # init values
    wordlength = 4
    if arch == 64:
        wordlength = 8
    output = 0
    # xor each word with each other to create a kind of hash
    for i in range(int(len(data)/wordlength)):
        output ^= int.from_bytes(data[wordlength*i:wordlength*(i+1)], 'big')
    return output

def xorDat(data, key):
    data = bytearray(data)
    for i in range(len(data)):
        data[i] ^= key[i % len(key)]
    return data

def createUnpacker(ADDRESS_OEP, ADDRESS_CODE_START, TOTAL_CODE_SIZE, PARTIAL_KEY, CORRECT_HASH, arch):
    if arch == 32:
        mask = 0xFFFFFF00
        hexFormat = '08x'
        file = "unpacker32.asm"
        runtimeOffset = 0x400000
    elif arch == 64:
        mask = 0xFFFFFFFFFFFFFF00
        hexFormat = '016x'
        file = "unpacker64.asm"
        runtimeOffset = 0x140000000
    else:
        exit(5)

    # remove two last bytes
    key = int.from_bytes(PARTIAL_KEY, 'little') & mask
    # little indianing
    hs = format(CORRECT_HASH, hexFormat)
    # if [:] out of bound, it returns ''
    final_string = "0x" + hs[14:16] + hs[12:14] + hs[10:12] + hs[8:10] + hs[6:8] + hs[4:6] + hs[2:4] + hs[:2]
    subprocess.run(["cp", file, "tmpUnpack.asm"])
    subprocess.run(["sed", "-i", "-e", f"s/ADDRESS_CODE_START/{hex(ADDRESS_CODE_START + runtimeOffset)}/g", "tmpUnpack.asm"])
    subprocess.run(["sed", "-i", "-e", f"s/TOTAL_CODE_SIZE/{hex(TOTAL_CODE_SIZE)}/g", "tmpUnpack.asm"])
    subprocess.run(["sed", "-i", "-e", f"s/PARTIAL_KEY/{hex(key)}/g", "tmpUnpack.asm"])
    subprocess.run(["sed", "-i", "-e", f"s/CORRECT_HASH/{final_string}/g", "tmpUnpack.asm"])
    subprocess.run(["sed", "-i", "-e", f"s/ADDRESS_OEP/{hex(ADDRESS_OEP + runtimeOffset)}/g", "tmpUnpack.asm"])
    subprocess.run(["nasm", "tmpUnpack.asm"])
    subprocess.run(["rm", "tmpUnpack.asm"])
    with open("tmpUnpack", "rb") as f:
        output = f.read()
    subprocess.run(["rm", "tmpUnpack"])
    return output

    #######################################################
    #                       PARSING                       #
    #######################################################
def parsing(filename):
    with open(filename, "rb") as f:
        binary = f.read()

    # Offsets of header dict
    offsets = {"begin":0}

    # Parse MSDOS header
    msdos = headerStruct.MSDOS(binary[:64])

    # Get fist part of offsets
    offsets["pe"] = msdos.pe_offset
    offsets["PEOpt"] = offsets["pe"] + 24

    # Parse PE header
    pe = headerStruct.PEHeader(binary[offsets["pe"]:offsets["PEOpt"]])

    # Get second part of offsets
    offsets["section"] = offsets["PEOpt"] + pe.SizeOfOptionalHeader
    offsets["EndSection"] = offsets["section"] + 40 * pe.NumberOfSections

    if pe.getArch() == 0:
        print("Not 32 nor 64 bits")
        exit(1)

    # Parse optional header
    opt = headerStruct.PEOptHeader(binary[offsets["PEOpt"]:offsets["section"]], pe.getArch())

    # Parse sections header
    sections = headerStruct.SectionHeader(binary[offsets["section"]:offsets["EndSection"]], pe.NumberOfSections)

    return binary, offsets, msdos, pe, opt, sections

    #######################################################
    #                      INFO PRINT                     #
    #######################################################
def giveInfo(binary, offsets, msdos, pe, opt, sections):
    print(f"################# MS DOS ################\nStarts at : {hex(offsets['begin'])}")
    msdos.printAll()
    print(f"################### PE ##################\nStarts at : {hex(offsets['pe'])}")
    pe.printAll()
    print(f"############ OPTIONAL HEADERS ###########\nStarts at : {hex(offsets['PEOpt'])}")
    imgSize = calculateSizeOfTheImage(sections.getSectionLast("VirtualAddress"), sections.getSectionLast("Misc"), opt.SectionAlignment)
    opt.printAll(sections, imgSize, offsets["section"], binary)
    print(f"################ SECTIONS ###############\nStarts at : {hex(offsets['section'])}")
    sections.sectionsInfo(True)
    nbleft = (opt.SizeOfHeaders - (offsets['section'] + pe.NumberOfSections * 40)) / 40
    if nbleft >= 1:
        print(f"\n\033[32mCan add {nbleft} sections\033[39m")
    else:
        print(f"\n\033[31mCan't add any section. Size left = {nbleft} sections\033[39m")

    print("\n#########################################\n")
    print(f"\033[36m0x{format(offsets['begin'], '08x')} ################")
    print(" "*11 + "     MS DOS     ")
    print(f"0x{format(offsets['pe'] - 1, '08x')} ################")
    print(f"\033[32m0x{format(offsets['pe'], '08x')} ################")
    print(" "*11 + "       PE      ")
    print(f"0x{format(offsets['PEOpt'] - 1, '08x')} ################")
    print(f"\033[33m0x{format(offsets['PEOpt'], '08x')} ################")
    print(" "*11 + "    OPT HEAD    ")
    print(f"0x{format(offsets['section'] - 1, '08x')} ################")
    print(f"\033[34m0x{format(offsets['section'], '08x')} ################")
    print(" "*11 + "    SECTIONS    ")
    print(f"0x{format(opt.SizeOfHeaders - 1, '08x')} ################")
    print(f"\033[39m###########################")
    endOfLastSection = sections.printBox()
    if len(binary) > endOfLastSection:
        print(f"\033[31m0x{format(endOfLastSection, '08x')} ################")
        print(" "*11 + "     UNKNOWN    ")
        print(f"0x{format(len(binary) - 1, '08x')} ################")

    print(f"\033[39m###########################")

    rsrc = headerStruct.Rsrc(binary[sections.getStartAddr(b".rsrc\x00\x00\x00"):sections.getEndAddr(b".rsrc\x00\x00\x00")],
                             sections.getVirtStart(b".rsrc\x00\x00\x00")
    )
    print(rsrc)

def changeRsrc(binary, sections, filename):
    rsrc = headerStruct.Rsrc(binary[sections.getStartAddr(b".rsrc\x00\x00\x00"):sections.getEndAddr(b".rsrc\x00\x00\x00")],
                             sections.getVirtStart(b".rsrc\x00\x00\x00")
    )
    rsrc.change()
    with open(f"{filename}.rsrc.exe", "wb") as f:
        f.write(binary[:sections.getStartAddr(b".rsrc\x00\x00\x00")] +
                rsrc.repack() +
                binary[sections.getEndAddr(b".rsrc\x00\x00\x00"):]
        )

def testSectionName(sectionName, default, sections, exists):
    if sectionName == None:
        sectionName = default
    if len(sectionName) > 8:
        verboseLog(1, f"Section name {sectionName} too long")
        exit(6)
    sectionName = bytes(sectionName, 'utf-8') + b'\x00' * (8 - len(sectionName))
    if sections.isExisting(sectionName) != exists:
        verboseLog(1, "Conflict with existing sections")
        exit(7)
    return sectionName

def addNewSection(newSect, size, pe, opt, sections):
    sections.addSection(newSect, size, 0x1000, 0x60000020)
    pe.addSection()
    opt.addCode(0x1000)
    imgSize = calculateSizeOfTheImage(
        sections.getSectionLast("VirtualAddress"),
        sections.getSectionLast("Misc"),
        opt.SectionAlignment,
    )
    opt.setSizeOfImage(imgSize)
    opt.rmChecksum()

def packingBack(offsets, pe, opt, sections, packedSect, unpacker, binary, unpackingSect, sectionToPack, packedSize):
    upckStart = sections.getStartAddr(unpackingSect)
    upckEnd = sections.getEndAddr(unpackingSect)

    beginPack = sections.getStartAddr(sectionToPack)
    EndPack = sections.getEndAddr(sectionToPack)

    packedBin = (
        # Headers
        binary[0:offsets["pe"]] + pe.repack() + opt.repack() + sections.repack() +
        binary[offsets["EndSection"]:beginPack] +

        # packed Sections
        packedSect + binary[beginPack + packedSize:EndPack] +

        # other section if existing
        binary[EndPack:upckStart] +
        unpacker + b'\x00'*(0x1000-len(unpacker)) +
        # after last section if other existing data
        binary[upckStart:]
    )
    return packedBin

def verboseLog(ok, message):
    if ok == 0:
        code = "[  \033[32mOK\033[39m  ]"
    if ok == 1:
        code = "[ \033[31mFAIL\033[39m ]"
    if ok == 2:
        code = "[ \033[33mINFO\033[39m ]"
    if verbose:
        print(f"{code} {message}")

def main(args):
    (
        binary, offsets, msdos, pe, opt, sections
    ) = parsing(args.filename)

    if args.info:
        giveInfo(binary, offsets, msdos, pe, opt, sections)
        return 0

    if args.rsrc:
        changeRsrc(binary, sections, args.filename)

    verboseLog(0, "Binary parsed")

    #######################################################
    #                    SETUP VARIABLES                  #
    #######################################################

    sectionToPack = testSectionName(args.section, '.text', sections, True)
    unpackingSect = testSectionName(args.new, '.unpack', sections, False)
    # Get original entry point
    entry = opt.getOEP()

    # Get starting and finishing address of the section to pack
    beginPack = sections.getStartAddr(sectionToPack)
    EndPack = sections.getEndAddr(sectionToPack)

    # be sure packedSize is multiple of 4 (or 8) and secure the hash
    wordlength = 4
    if pe.getArch() == 64:
        wordlength = 8
    packedSize = EndPack - beginPack
    while(
        packedSize % wordlength != 0 or
        packedSize % wordlength == 0 and (packedSize/wordlength)%2==0
    ):
        packedSize -= 1

    verboseLog(2, f"Packed size : {hex(packedSize)}")

    sections.addRight(sectionToPack, 'w')

    key = generateKey(pe.getArch())
    verboseLog(0, f"Key generated with success : {key}")

    packedSect = xorDat(binary[beginPack:beginPack + packedSize], key)
    goodHash = hashing(binary[beginPack:beginPack + packedSize], pe.getArch())
    verboseLog(0, f"Hash generated with success : {hex(goodHash)}")

    unpacker = createUnpacker(entry, beginPack, packedSize, key, goodHash, pe.getArch())
    verboseLog(0, "Unpacker created with success")

    #######################################################
    #           CREATING NEW SECTION CHANGE EP            #
    #######################################################

    if sections.getLowerAddr() > opt.SizeOfHeaders:
        verboseLog(2, "Size of header inferior to their real size... fixing")
        opt.SizeOfHeaders = sections.getLowerAddr()
        verboseLog(0, "Size of header fixed")

    nbleft = (opt.SizeOfHeaders - (offsets['section'] + pe.NumberOfSections * 40)) / 40
    if nbleft >= 1:
        verboseLog(0, f"Can add {nbleft} sections")
    else:
        verboseLog(1, f"Can't add any section. Size left = {nbleft} sections")

    # Create new pack section
    addNewSection(unpackingSect, len(unpacker), pe, opt, sections)
    offsets["EndSection"] += 40

    verboseLog(0, f"New section {unpackingSect} added")

    # Change entry point
    opt.setEP(sections.getVirtStart(unpackingSect))

    verboseLog(0, "Entry point changed")

    #######################################################
    #                     PACKING BACK                    #
    #######################################################
    packedBin = packingBack(
        offsets, pe, opt, sections, packedSect, unpacker,
        binary, unpackingSect, sectionToPack, packedSize
    )

    verboseLog(0, "Binary packed")

    with open("{}.packed.exe".format(args.filename), "wb") as f:
        f.write(packedBin)

    verboseLog(0, f"{args.filename}.packed.exe created")
    return 0

verbose = False

if __name__=="__main__":

    parser = argparse.ArgumentParser(description='Packer for windows 32/64 binary')
    parser.add_argument(
        'filename', metavar='filename', type=str,
        help='Name of the executable to tweak'
    )
    parser.add_argument(
        '-i', '--info', action='store_true',
        help='Give general informations about the file', default=False
    )
    parser.add_argument(
        '-s', '--section', type=str,
        help='Section to pack'
    )
    parser.add_argument(
        '-n', '--new', type=str,
        help='Name of the new unpacking section'
    )
    parser.add_argument(
        '-v', '--verbose', action='store_true',
        help='Activate verbosity in the program', default=False
    )
    parser.add_argument(
        '-r', '--rsrc', action='store_true',
        help='Rewrite .rsrc section', default=False
    )
    args = parser.parse_args()
    verbose = args.verbose
    exit(main(args))


import serial
import matplotlib
matplotlib.use('TkAgg') # MUST BE CALLED BEFORE IMPORTING plt
from matplotlib import pyplot as plt
import queue
import threading
import animation
import seaborn as sns
import numpy as np
import time

class ArduinoReader(threading.Thread):
    def __init__(self, stop_event, sig, serport):
        threading.Thread.__init__(self)
        self.stopped = stop_event
        self.signal = sig
        self.pixalarray = [[] for _ in range(8)]
        self.pixalarray0 = [[] for _ in range(8)]
        port = serport
        # self.s = serial.Serial(port, 9600, timeout=1, rtscts=True, dsrdtr=True)
        self.s = serial.Serial(port, 115200, timeout=0.1, rtscts=True, dsrdtr=True)
        if not self.s.isOpen():
            self.s.open()
        print("connected: ", self.s)

    def run(self):
        while not self.stopped.is_set():
            # print(self.s.readline().rstrip())
            try:
                dstr = str(self.s.readline())
                # print(dstr.find('Frame'))
                if dstr.find('Frame') > 0:
                    data_d1 = str(self.s.readline()).split(',')
                    self.pixalarray = [float(x) for x in data_d1[1:-1]]
                    data_d0 = str(self.s.readline()).split(',')
                    self.pixalarray0 = [float(x) for x in data_d0[1:-1]]
                    if len(self.pixalarray) == 64 and len(self.pixalarray0) == 64:
                        self.signal.put([self.pixalarray, self.pixalarray0])
            except:
                continue
        self.clean()

    def get_signal(self):
        return self.signal

    def clean(self):
        # self.s.cancel_read()
        while self.s.isOpen():
            self.s.close()
            # print('the serial port is open? {}'.format(self.s.isOpen()))

def colorscale(data, minc, maxc):
    data_scale = 256 * (data - minc) / (maxc - minc)
    if data_scale < 0:
        data_scale = 0
    elif data_scale > 255:
        data_scale = 255
    print(data, data_scale)
    return int(data_scale)

if __name__ == '__main__':
    try:
        q = queue.Queue()
        stop_event = threading.Event()
        data_reader = ArduinoReader(stop_event, q, 'COM3')
        data_reader.start()
        fig, (ax1, ax0) = plt.subplots(1, 2)
        im1 = ax1.imshow(np.random.uniform(low=22, high=32, size=(8, 8)), vmin=22, vmax=32, cmap='jet', interpolation='lanczos')
        im0 = ax0.imshow(np.random.uniform(low=22, high=32, size=(8,8)), vmin = 22, vmax = 32, cmap='jet', interpolation='lanczos')
        plt.tight_layout()
        plt.ion()
        while True:
            [frame1, frame0] = q.get()
            im1.set_array(np.reshape(frame1, (8, 8)))
            im0.set_array(np.reshape(frame0, (8, 8)))
            # plt.draw()
            plt.pause(0.001)
        plt.ioff()
        plt.show()
    finally:
        stop_event.set()
        data_reader.clean()
        data_reader.join()


# Import required libraries
import pandas as pd
from sqlalchemy import create_engine

# Load data from source into a Pandas dataframe
df = pd.read_csv('source_data.csv')

# Perform data transformation and cleaning
df = df.dropna()
df['column_name'] = df['column_name'].str.upper()

# Load data into a SQL database
engine = create_engine('sqlite:///data.db')
df.to_sql('table_name', engine, if_exists='replace')

# Perform data reconciliation
query = '''
SELECT COUNT(*)
FROM table_name
'''
count = pd.read_sql_query(query, engine)['COUNT(*)'][0]

# Log the reconciliation result
if count == df.shape[0]:
    print('Data integration successful')
else:
    print('Data integration failed')

# This is just a basic example to illustrate the steps involved in a data integration workflow.
# ou can add more complex transformations, data quality checks, and error handling to suit your specific requirements.


import requests
import re
import random
import time
from bs4 import BeautifulSoup
import bs4
from fake_useragent import UserAgent

ua = UserAgent()

books = []
discounts = []
cookie = {
    "bid": "6183e2a207286",
    "_gcl_au": "1.1.1678734493.1636033188",
    "cid": "kypss95053",
    "pd": "B4MPDFMstRRagO9wOXmP3pNPoI",
    "stepsession": "YaCaCwo8DR4AAT5h8nYAAAAc",
    "ssid": "6183e2a207286.1637940082",
}
for i in range(10703023, 11000000):
    cookie["ssid"] = (
        "6183e2a207286.16379400" + str(random.randint(0, 9)) + str(random.randint(0, 9))
    )
    print(f"https://www.books.com.tw/products/00{i:08}", end="\r")
    time.sleep(0.5)
    flag = True
    while flag:
        try:
            res = requests.get(
                f"https://www.books.com.tw/products/00{i:08}",
                headers={
                    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
                    "accept-encoding": "gzip, deflate, br",
                    "accept-language": "en-US,en;q=0.9,zh-TW;q=0.8,zh;q=0.7",
                    "user-agent": ua.random,
                },
                cookies=cookie,
            )
            flag = False
        except:
            time.sleep(60)
            flag = True
    if res.status_code == 404:
        print(f"{i:08}: {res.status_code}", end="\r")
        continue
    else:
        retry = 0
        while res.status_code != 200 and retry < 3:
            time.sleep(60)
            try:
                res = requests.get(
                    f"https://www.books.com.tw/products/00{i:08}",
                    headers={"user-agent": ua.random},
                    cookies=cookie,
                )
            except:
                pass
            if res.status_code == 404:
                retry = 3
                break
            retry += 1
    if retry >= 3:
        continue
    try:
        res.encoding = "utf-8"
        soup = BeautifulSoup(res.text, "lxml")
        soup.prettify()
        book_id = i
        name = "None"
        author = "None"
        author_original = "None"
        translator = "None"
        publishing_date = "None"
        publishing_house = "None"
        price = "None"
        ISBN = "None"
        discount_price = "None"
        expire_date = "None"
        name = soup.find_all("div", class_="type02_p002")[0].h1.text
        basic_information = soup.find_all("div", class_="type02_p003")[0].ul
        lis = basic_information.find_all("li", recursive=False)
        for li in lis:
            # print(li.children)
            children = [_ for _ in li.children]
            if len(children) == 1:
                if re.match("出版日期*", li.text):
                    publishing_date = li.text[5:]
                    # print(publishing_date)
                elif re.match("語言*", li.text):
                    language = li.text[3:]
                    # print(language)
            else:
                if re.match("原文作者", children[0]):
                    author_original = children[1].text
                    # print(author_original)
                elif re.match("譯者", children[0]):
                    translator = children[1].text
                    # print(translator)
                elif re.match("出版社", children[0]):
                    publishing_house = children[1].text
                    # print(publishing_house)
                else:
                    if (
                        len(children) > 2
                        and type(children[2]) == bs4.element.NavigableString
                        and re.match("作者", children[2].strip())
                    ):
                        author = children[3].text
                        if "," in author:
                            raise Exception()
        price_ul = soup.find_all("ul", class_="price")[0]
        price_ul_len = len(price_ul.find_all("li", recursive=False))
        price = price_ul.find_all("li", recursive=False)[0].em.text
        # print(price)
        if price_ul_len > 1:
            discount_price = (
                price_ul.find_all("li", recursive=False)[1]
                .find_all("strong", recursive=False)[1]
                .b.text
            )
            # print(discount_price)
            try:
                expire_date = price_ul.find_all("li", recursive=False)[2].text
                if expire_date[:4] == "優惠期限":
                    expire_date = expire_date[5:]
                    expire_date = expire_date.replace("年", "/")
                    expire_date = expire_date.replace("月", "/")
                    expire_date = expire_date.replace("日", "")

                    expire_date = expire_date.replace("止", "")
                    discounts = [str(book_id), discount_price, expire_date]
                else:
                    discount = [str(book_id), discount_price, ""]
                    # print(expire_date)
            except:
                discounts = [str(book_id), discount_price, ""]

        detail_div = soup.find_all("h3", text="詳細資料")[0].parent.div
        if detail_div.ul.li.text[:4] == "ISBN":
            ISBN = detail_div.ul.li.text[5:]
        else:
            raise Exception()
        # print(ISBN)
        book = [
            str(book_id),
            ISBN,
            name,
            author,
            author_original,
            translator,
            publishing_house,
            publishing_date,
            price,
        ]

        with open("book.csv", "r") as f:
            data = f.read()
        with open("book.csv", "w") as f:
            f.write(data)
            f.write(",".join(book) + "\n")
        with open("discount.csv", "r") as f:
            data = f.read()
        with open("discount.csv", "w") as f:
            f.write(data)
            f.write(",".join(discount) + "\n")
    except Exception as e:
        # raise e
        # break
        pass


#Standard
import numpy as np
import cv2
import os
import copy
from PIL import Image, ImageFilter
import time

#Local files
from Utilities import make_directory, align_image, get_from_directory, save_to_directory, numericalSort
from HOG_functions import process_HOG_image, get_HOG_image
import JetsonYolo

#SCIPY and SKlearn
from scipy.signal import savgol_filter, fftconvolve
from sklearn.metrics import mean_squared_error
from scipy.linalg import norm
from scipy import sum, average
from skimage.metrics import structural_similarity as compare_ssim

def run_histogram_equalization(image):
    ycrcb_img = cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb)
    # equalize the histogram of the Y channel
    ycrcb_img[:, :, 0] = cv2.equalizeHist(ycrcb_img[:, :, 0])
    equalized_img = cv2.cvtColor(ycrcb_img, cv2.COLOR_YCrCb2BGR)
    return equalized_img

def edge_detect (channel):
    sobelX = cv2.Sobel(channel, cv2.CV_16S, 1, 0)
    sobelY = cv2.Sobel(channel, cv2.CV_16S, 0, 1)
    sobel = np.hypot(sobelX, sobelY)
    sobel[sobel > 255] = 255;
    return sobel

def create_special_silhouettes(mask_path = './Images/Masks/FewShot', image_path = './Images/FewShot', masks = None, single = False):
    #Allow for the passing of pre-loaded silhouettes for the video test function
    if masks == None:
        mask_instances = get_from_directory(mask_path)
    else:
        mask_instances = masks

    special_silhouettes = []
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))

    for iterator, (subdir, dirs, files) in enumerate(os.walk(image_path)):
        dirs.sort(key=numericalSort)
        if len(files) > 0:
            masks = []
            background = []
            combined_example = []
            silhouettes = []
            for file_iter, file in enumerate(sorted(files, key=numericalSort)):
                # load the input image and associated mask from disk and perform initial pre-processing
                image = cv2.imread(os.path.join(subdir, file))
                hist_image = run_histogram_equalization(image)
                blurred = cv2.GaussianBlur(hist_image, (3, 3), 0)
                cv2.imshow("Stage 1", blurred)

                #Prepare the image using a sobel edge detector, remove noise and convert to an 8-bit array
                edgeImg = np.max(np.array([edge_detect(blurred[:, :, 0]), edge_detect(blurred[:, :, 1]), edge_detect(blurred[:, :, 2])]), axis=0)
                mean = np.mean(edgeImg);
                edgeImg[edgeImg <= mean] = 0;
                edgeImg_8u = np.asarray(edgeImg, np.uint8)

                #If first frame in the sequence, set as background
                if file_iter == 0:
                    background = edgeImg_8u

                #Use morphological operations to produce an inflated silhouette from background subtraction
                background_based_silhouette = cv2.absdiff(edgeImg_8u, background)
                cv2.imshow("Stage 2", background_based_silhouette)

                background_based_silhouette = cv2.threshold(background_based_silhouette, 100, 255, cv2.THRESH_BINARY)[1]
                *_, bk_mask = cv2.threshold(background_based_silhouette, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
                opening = cv2.morphologyEx(bk_mask, cv2.MORPH_OPEN, kernel, iterations=1)
                cv2.imshow("Stage 3", opening)
                # Retrieve and dilate the mask to prevent parts of the body being excluded
                if single == False:
                    mask = mask_instances[iterator - 1][file_iter]
                else:
                    mask = mask_instances[file_iter]

                bk_expanded = cv2.dilate(mask, kernel, iterations=5)
                cv2.imshow("Stage 4", bk_expanded)
                bk_mask_test = cv2.bitwise_and(opening, opening, mask=bk_expanded)
                cv2.imshow("Stage 5", bk_mask_test)
                #Perform morphological operations on edge detected image after applying mask
                #Thresholding to produce a silhouette of the extremities of the silhouette that the mask may have missed
                edge_based_silhouette = edgeImg_8u * bk_expanded
                edge_based_silhouette = cv2.morphologyEx(edge_based_silhouette, cv2.MORPH_CLOSE, kernel)
                edge_based_silhouette = cv2.morphologyEx(edge_based_silhouette, cv2.MORPH_OPEN, kernel)

                #Get copy of the mask and turn into an actual mask (from range 0-1)
                temp_mask = copy.deepcopy(mask)
                temp_mask[temp_mask == 255] = 1

                #Remove noise from the blurred image as a template
                threshold_lower = 30
                threshold_upper = 220
                mask_based_silhouette = cv2.Canny(blurred, threshold_lower, threshold_upper)
                #Apply the mask
                mask_based_silhouette = cv2.bitwise_and(mask_based_silhouette, mask_based_silhouette, mask=temp_mask)

                #Take this and turn all pixels white, then perform open and close to tidy it up
                mask_based_silhouette = cv2.morphologyEx(mask_based_silhouette, cv2.MORPH_CLOSE, kernel)
                mask_based_silhouette = cv2.morphologyEx(mask_based_silhouette, cv2.MORPH_OPEN, kernel)

                #Align images
                mask =  align_image(mask, 30)
                bk_mask_test =  align_image(bk_mask_test, 1)
                mask_based_silhouette =  align_image(mask_based_silhouette, 30)
                edge_based_silhouette =  align_image(edge_based_silhouette, 30)

                alpha = 1.0
                beta = 1.0
                combined_example = []
                finished_example = []
                if len(combined_example) == 0:
                    combined_example = mask

                #Combine the masks, apply a mode filter to smooth the result
                combined_example = cv2.addWeighted(bk_mask_test, alpha, combined_example, beta, 0.0)
                cv2.imshow("Stage 5", combined_example)
                image_example =  Image.fromarray(combined_example)
                image_example = image_example.filter(ImageFilter.ModeFilter(size=5))
                cv2.imshow("Stage 6", np.asarray(image_example))
                key = cv2.waitKey(0) & 0xff
                silhouettes.append(np.array(image_example))
            special_silhouettes.append(silhouettes)
            print("silhouette complete")
            #If a single image has been passed instead of a whole instance, return after one iteration
            if single == True:
                return silhouettes
    #Save
    save_to_directory(special_silhouettes, './Images/SpecialSilhouettes/FewShot')
    print("operation complete, special silhouettes saved")
    return special_silhouettes

#Graph cut
def graph_cut(mask_path = './Images/Masks/FewShot', image_path = './Images/FewShot', by_mask = True, mask_edges = True, masks = None):

    #Adjust save path depending on which combination is used to create it. The best reults are hard-coded into the definition
    if by_mask and mask_edges:
        save_path = './Images/GraphCut/FewShot'
    elif by_mask and not mask_edges:
        save_path = './Images/graph_mask_noedges'
    elif not by_mask and mask_edges:
        save_path = './Images/graph_nomask_edges'
    else:
        save_path = './Images/graph_nomask_noedges'

    #Allow masks to be read directly from memory for live testing
    if masks == None:
        mask_instances = get_from_directory(mask_path)
    else:
        mask_instances = masks

    image_instances = []
    for iterator, (subdir, dirs, files) in enumerate(os.walk(image_path)):
        dirs.sort(key=numericalSort)
        if len(files) > 0:
            masks = []
            images = []
            for file_iter, file in enumerate(sorted(files, key = numericalSort)):
                # load the input image and associated mask from disk
                image = cv2.imread(os.path.join(subdir, file))
                #Emphasize outlines
                image = run_histogram_equalization(image)
                #Blur to remove noise
                blurred = cv2.GaussianBlur(image, (5, 5), 0)
                #Generate edge detection image
                edgeImg = np.max(np.array([edge_detect(blurred[:, :, 0]), edge_detect(blurred[:, :, 1]), edge_detect(blurred[:, :, 2])]), axis=0)
                mean = np.mean(edgeImg);
                # Reduce noise
                edgeImg[edgeImg <= mean] = 0;
                edgeImg_8u = np.asarray(edgeImg, np.uint8)

                rect = [(0, 1), (0, 1)]
                #Bounding box
                if by_mask == False:
                    # Get humans
                    objs = JetsonYolo.get_objs_from_frame(np.asarray(image), False)
                    seen_human = False
                    for obj in objs:
                        (xmin, ymin), (xmax, ymax) = obj['bbox']
                        rect = [xmin, ymin, xmax, ymax]
                    # Detector only returns human objs
                    if len(objs) == 0:
                        continue
                else:
                    # Mask
                    mask = mask_instances[iterator-1][file_iter]
                    if np.all(mask == 0):
                        continue
                    # any mask values greater than zero should be set to probable
                    # foreground
                    mask[mask > 0] = cv2.GC_FGD
                    mask[mask == 0] = cv2.GC_BGD

                # allocate memory for two arrays that the GrabCut algorithm internally
                # uses when segmenting the foreground from the background
                fgModel = np.zeros((1, 65), dtype="float")
                bgModel = np.zeros((1, 65), dtype="float")

                # apply GrabCut using the the mask segmentation method
                start = time.time()

                if mask_edges == True:
                    edgeImg = edgeImg.astype("uint8")
                    edgeImg[edgeImg == 255] = 1;
                    grab_image = cv2.bitwise_and(image, image, mask=edgeImg)
                else:
                    grab_image = image

                if by_mask == False:
                    mask = np.zeros(image.shape[:2], dtype="uint8")

                    (mask, bgModel, fgModel) = cv2.grabCut(grab_image, mask, rect, bgModel,
                                                           fgModel, iterCount=5, mode=cv2.GC_INIT_WITH_RECT)
                else:
                    (mask, bgModel, fgModel) = cv2.grabCut(grab_image, mask, None, bgModel,
                                                           fgModel, iterCount=5, mode=cv2.GC_INIT_WITH_MASK)

                end = time.time()
                print("[INFO] applying GrabCut took {:.2f} seconds".format(end - start))

                outputMask = np.where((mask == cv2.GC_BGD) | (mask == cv2.GC_PR_BGD),
                                      0, 1)
                # scale the mask from the range [0, 1] to [0, 255]
                outputMask = (outputMask * 255).astype("uint8")
                #Smooth to avoid noisy pixels on the mask edges
                outputMask = Image.fromarray(outputMask)
                outputMask = outputMask.filter(ImageFilter.ModeFilter(size=13))
                outputMask = np.array(outputMask)
                # apply a bitwise AND to the image using our mask generated by GrabCut to generate our final output image
                output = cv2.bitwise_and(image, image, mask=outputMask)
                outputMask = align_image(outputMask, 0)
                images.append(outputMask)
            image_instances.append(images)
    save_to_directory(image_instances, save_path)
    print("graph cut operation complete")

def process_image(image, raw_img, verbose = 0, subtractor = None):
    # Extract the contours formed by the silhouette, image is now silhouette and raw image not used, nor is subtractor
    white_mask = cv2.inRange(image, 180, 255)
    return np.asarray(align_image(white_mask, 0)) # was image

def get_silhouettes(path, verbose = 0, HOG = False, few_shot = False):
    global HOG_background
    mask_instances = get_from_directory('./Images/Masks')
    make_directory(path, "Silhouette folder already exists")
    processed_images = []

    for iterator, (subdir, dirs, files) in enumerate(os.walk(path)):
        dirs.sort(key=numericalSort)
        if len(files) > 0:
            raw_images = []
            processed_instances = []
            subtractor = cv2.createBackgroundSubtractorKNN()
            for file_iter, file in enumerate(sorted(files, key = numericalSort)):
                raw_images.append(cv2.imread(os.path.join(subdir, file)))
                #Prepare image
                gray_img = cv2.cvtColor(raw_images[file_iter], cv2.COLOR_BGR2GRAY)
                #First pass: if HOG take a background example
                if file_iter == 0 and HOG == True:
                    HOG_background = get_HOG_image(gray_img)
                #Process image according to chosen processing method
                if HOG == False:
                    processed_instances.append(process_image(gray_img, raw_images[file_iter], verbose, subtractor))
                else:
                    processed_instances.append(process_HOG_image(get_HOG_image(gray_img), HOG_background, mask_instances[iterator-1][file_iter]))

            processed_images.append(processed_instances)
    # Processed images taken, save to location
    os.chdir(os.path.abspath(os.path.join(__file__, "../../..")))

    for instance in processed_images:
        #Find the latest un-made path and save the new images to it
        path_created = False
        n = 0.0
        while path_created == False:
            try:
                if HOG == False and few_shot == False:
                    local_path = './Images/Silhouettes' + "/Instance_" + str(n) + "/"
                elif HOG == True and few_shot == False:
                    local_path = './Images/HOG_silhouettes' + "/Instance_" + str(n) + "/"
                elif HOG == False and few_shot == True:
                    local_path = './Images/HOG_silhouettes/FewShot' + "/Instance_" + str(n) + "/"
                elif HOG == True and few_shot == True:
                    local_path = './Images/HOG_silhouettes/FewShot' + "/Instance_" + str(n) + "/"
                os.makedirs(local_path, exist_ok=False)
                path_created = True
            except:
                n += 1
        for i, image in enumerate(instance):
            #Exclude entirely black or entirely white images from the sequence.
            if HOG:
                cv2.imwrite(local_path + str(i) + ".jpg", image)
            else:
                if not np.all((image == 0)) and not np.all((image == 255)):
                    cv2.imwrite(local_path + str(i) + ".jpg", image)
    print("all saved")


import random

def candy_game(num_candies, player_name):
    player1_candies = 0
    player2_candies = 0
    turn = random.randint(1, 2)
    input('\n\nPress ENTER to find out who takes the candy first\n')
    if turn == 1:
        print('             !!Player goes first!!')
    else:
        print('             !!Bot goes first!!')
    player1_move = 0

    while num_candies > 0:
        if turn == 1:
            while True:
                try:
                    player1_move = int(input(f'{player_name} move: '))
                    if 1 <= player1_move <= 28:
                        break
                    else:
                        print('You can take a maximum of 28')
                except:
                    print('Try again')
            num_candies -= player1_move
            player1_candies += player1_move + player2_candies
            player2_candies -= player2_candies
            turn = 2

            print(f'        {num_candies} candies left ')

            if num_candies <= 0:
                print(f'             !!! {player_name} WON!!!')
        else:
            if num_candies <= 57:
                player2_move = num_candies % 28
            else:
                player2_move = random.randint(1,28)
            print('Bot move:', player2_move)
            num_candies -= player2_move
            player2_candies += player2_move + player1_candies
            player1_candies -= player1_candies
            turn = 1
            input(' Press ENTER to continue')
            print(f'        {num_candies} candies left ')

            if num_candies <= 0:
                print('         !!!Bot WON!!!')

def main():
    player_name = input("\nHello, I have a game for you, what's your name?\n Enter your name and press ENTER:  ")
    print('\nThe rules are simple, whoever takes the last\n candy from the table takes everything,\n with the condition that only 28 candies can be taken at a time.')
    while True:
        try:
            total_candies = int(input('\n\nHow many sweets will we put on the table? '))
            break
        except:
            print("Can't have that many candies")
    candy_game(total_candies, player_name)

main()


"""Copied from cpython to ensure compatibility"""
import io
from typing import Any, Callable, Dict

BUFFER_SIZE = io.DEFAULT_BUFFER_SIZE  # Compressed data read chunk size

class BaseStream(io.BufferedIOBase):
    """Mode-checking helper functions."""

    def _check_not_closed(self):
        if self.closed:
            raise ValueError("I/O operation on closed file")

    def _check_can_read(self):
        if not self.readable():
            raise io.UnsupportedOperation("File not open for reading")

    def _check_can_write(self):
        if not self.writable():
            raise io.UnsupportedOperation("File not open for writing")

    def _check_can_seek(self):
        if not self.readable():
            raise io.UnsupportedOperation(
                "Seeking is only supported " "on files open for reading"
            )
        if not self.seekable():
            raise io.UnsupportedOperation(
                "The underlying file object " "does not support seeking"
            )

class DecompressReader(io.RawIOBase):
    """Adapts the decompressor API to a RawIOBase reader API"""

    def readable(self):
        return True

    def __init__(
        self,
        fp: io.IOBase,
        decomp_factory: Callable,
        **decomp_args: Dict[str, Any],
    ):
        self._fp = fp
        self._eof = False
        self._pos = 0  # Current offset in decompressed stream

        # Set to size of decompressed stream once it is known, for SEEK_END
        self._size = -1

        # Save the decompressor factory and arguments.
        # If the file contains multiple compressed streams, each
        # stream will need a separate decompressor object. A new decompressor
        # object is also needed when implementing a backwards seek().
        self._decomp_factory = decomp_factory
        self._decomp_args = decomp_args
        self._decompressor = self._decomp_factory(**self._decomp_args)

        # Exception class to catch from decompressor signifying invalid
        # trailing data to ignore
        self._buffer = bytearray()  # type: bytearray

    def close(self) -> None:
        self._decompressor = None
        return super().close()

    def seekable(self) -> bool:
        return self._fp.seekable()

    def readinto(self, b) -> int:
        with memoryview(b) as view, view.cast("B") as byte_view:
            data = self.read(len(byte_view))
            byte_view[: len(data)] = data
        return len(data)

    def read(self, size=-1) -> bytes:  # todo 这个是重点
        if size < 0:
            return self.readall()
        if size <= len(self._buffer):
            self._pos += size
            ret = bytes(self._buffer[:size])
            del self._buffer[:size]
            return ret
        if not size or self._eof:
            return b""
        # data = None  # Default if EOF is encountered
        # Depending on the input data, our call to the decompressor may not
        # return any data. In this case, try again after reading another block.
        # try:
        while True:
            rawblock = self._fp.read(BUFFER_SIZE)
            if not rawblock:
                break
            self._buffer.extend(self._decompressor.decompress(rawblock))
            if len(self._buffer) >= size:
                break
        if len(self._buffer) >= size:
            self._pos += size
            ret = bytes(self._buffer[:size])
            del self._buffer[:size]
        else:  # 不够长了
            self._pos += len(self._buffer)
            self._eof = True
            self._size = self._pos
            ret = bytes(self._buffer)
            self._buffer.clear()
        return ret

    def readall(self) -> bytes:
        while True:
            rawblock = self._fp.read(BUFFER_SIZE)
            if not rawblock:
                break
            self._buffer.extend(self._decompressor.decompress(rawblock))
        self._pos += len(self._buffer)
        ret = bytes(self._buffer)
        self._buffer.clear()
        return ret

    # Rewind the file to the beginning of the data stream.
    def _rewind(self):
        self._fp.seek(0)
        self._eof = False
        self._pos = 0
        self._buffer.clear()
        self._decompressor = self._decomp_factory(**self._decomp_args)

    def seek(self, offset, whence=io.SEEK_SET):
        # Recalculate offset as an absolute file position.
        if whence == io.SEEK_SET:
            pass
        elif whence == io.SEEK_CUR:
            offset = self._pos + offset
        elif whence == io.SEEK_END:
            # Seeking relative to EOF - we need to know the file's size.
            if self._size < 0:
                while self.read(io.DEFAULT_BUFFER_SIZE):
                    pass
            offset = self._size + offset
        else:
            raise ValueError("Invalid value for whence: {}".format(whence))

        # Make it so that offset is the number of bytes to skip forward.
        if offset < self._pos:
            self._rewind()
        else:
            offset -= self._pos

        # Read and discard data until we reach the desired position.
        while offset > 0:
            data = self.read(min(io.DEFAULT_BUFFER_SIZE, offset))
            if not data:
                break
            offset -= len(data)

        return self._pos

    def tell(self) -> int:
        """Return the current file position."""
        return self._pos