from tkinter import *
from tkinter import ttk as ttk
from tkinter import messagebox as mb
import datetime
import sqlite3
from tkcalendar import DateEntry

# list all the expenses
def listAllExpenses():

    # global variables
    global dbconnector, data_table

    data_table.delete(*data_table.get_children())

    all_data = dbconnector.execute('SELECT * FROM BUDGET')
    data = all_data.fetchall()

    # inserting the values in the tkinter data table
    for val in data:
        data_table.insert('', END, values = val)

# view an expense information
def viewExpenseInfo():

    # global variables
    global data_table
    global dateField, payee, description, amount, paymentMethod

    # error message box
    if not data_table.selection():
        mb.showerror('No expense record selected', 'Please select an expense record from the table to view')

    currentSelectedExpense = data_table.item(data_table.focus())
    val = currentSelectedExpense['values']
    expenditureDate = datetime.date(int(val[1][:4]), int(val[1][5:7]), int(val[1][8:]))

    dateField.set_date(expenditureDate) ; payee.set(val[2]) ; description.set(val[3]) ; amount.set(val[4]) ; paymentMethod.set(val[5])

# clear the entries from the entry fields
def clearFields():

    #global variable
    global description, payee, amount, paymentMethod, dateField, data_table

    todayDate = datetime.datetime.now().date()

    description.set('') ; payee.set('') ; amount.set(0.0) ; paymentMethod.set('Cash'), dateField.set_date(todayDate)
    data_table.selection_remove(*data_table.selection())

# remove the selected expense
def removeExpense():

    # error message box
    if not data_table.selection():
        mb.showerror('No expense record selected!', 'Please select a expense record to remove!', icon='warning')
        return

    currentSelectedExpense = data_table.item(data_table.focus())

    valuesSelected = currentSelectedExpense['values']

    # message box when the user hit the button
    confirmation = mb.askyesno('Confirmation', f'Are you sure that you want to remove the record of {valuesSelected[2]}')

    if confirmation:
        dbconnector.execute('DELETE FROM BUDGET WHERE ID=%d' % valuesSelected[0])
        dbconnector.commit()
        listAllExpenses()
        mb.showinfo('Expense removed successfully!', f'The record you wanted to remove has been removed successfully')

# delete all the entries
def removeAllExpenses():
    confirmation = mb.askyesno('Confirmation', 'Are you sure that you want to remove all the expense items from the database?', icon='warning')

    if confirmation:
        data_table.delete(*data_table.get_children())
        dbconnector.execute('DELETE FROM BUDGET')
        dbconnector.commit()
        clearFields()
        listAllExpenses()
        # message for successfullt remove the expense from database
        mb.showinfo('All Expenses has been removed!', 'All the expenses were successfully removed!')
    else:
        # message for cancel the request to remove all expenses
        mb.showinfo('Confirmation', 'Removed all expenses request has been cancelled!')

# add an expense
def addAnotherExpense():

    # global variables
    global dateField, payee, description, amount, paymentMethod
    global dbconnector

    # error message box
    if not dateField.get() or not payee.get() or not description.get() or not amount.get() or not paymentMethod.get():
        mb.showerror('Fields are empty!', "Please fill all the missing fields then hit the 'Add Expense' button!")
    else:
        dbconnector.execute(
            'INSERT INTO BUDGET (Date, Payee, Description, Amount, paymentMethod) VALUES (?, ?, ?, ?, ?)',
            (dateField.get_date(), payee.get(), description.get(), amount.get(), paymentMethod.get())
        )
        dbconnector.commit()
        clearFields()
        listAllExpenses()

        # message box when the user hit the button
        mb.showinfo('Expense added', 'The expense whose details you just entered has been added to the database')

# edit expense
def editExpense():
    # global variable
    global data_table

    def editExistingExpense():

        # global variable
        global dateField, amount, description, payee, paymentMethod
        global dbconnector, data_table

        currentSelectedExpense = data_table.item(data_table.focus())

        content = currentSelectedExpense['values']

        # update the information to database
        dbconnector.execute(
            'UPDATE BUDGET SET Date = ?, Payee = ?, Description = ?, Amount = ?, paymentMethod = ? WHERE ID = ?',
            (dateField.get_date(), payee.get(), description.get(), amount.get(), paymentMethod.get(), content[0])
        )
        dbconnector.commit()
        clearFields()
        listAllExpenses()

        mb.showinfo('Data edited', 'We have updated the data and stored in the database as you wanted')
        editSelectedButton.destroy()

    # if no expense is selectted, returning a error message box
    if not data_table.selection():
        mb.showerror('No expense selected!', 'You have not selected any expense in the table for us to edit; please do that!')
        return

    viewExpenseInfo()

    # edit button
    editSelectedButton = Button(
        frameL3,
        text = "Edit Expense",
        font = ("Bahnschrift Condensed", "13"),
        width = 30,
        bg = "#90EE90",
        fg = "#000000",
        relief = GROOVE,
        activebackground = "#008000",
        activeforeground = "#98FB98",
        command = editExistingExpense
        )

    editSelectedButton.grid(row = 0, column = 0, sticky = W, padx = 50, pady = 10)

# main function
if __name__ == "__main__":

    # connecting to the database
    dbconnector = sqlite3.connect("Budget Budget Budget.db")
    dbcursor = dbconnector.cursor()

    dbconnector.execute(
        'CREATE TABLE IF NOT EXISTS BUDGET (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, Date DATETIME, Payee TEXT, Description TEXT, Amount FLOAT, paymentMethod TEXT)'
    )
    dbconnector.commit()

    # setting for the app pop up window
    main_win = Tk()
    main_win.title("BUDGET BUDGET BUDGET")
    main_win.geometry("1440x700+400+100")
    main_win.resizable(0, 0)
    main_win.config(bg = "#FFFAF0")

    # custom frames
    frameLeft = Frame(main_win, bg = "#80ced6")
    frameRight = Frame(main_win, bg = "#DEB887")
    frameL1 = Frame(frameLeft, bg = "#80ced6")
    frameL2 = Frame(frameLeft, bg = "#80ced6")
    frameL3 = Frame(frameLeft, bg = "#80ced6")
    frameR1 = Frame(frameRight, bg = "#3e4444")
    frameR2 = Frame(frameRight, bg = "#DEB887")

    frameLeft.pack(side=LEFT, fill = "both")
    frameRight.pack(side = RIGHT, fill = "both", expand = True)
    frameL1.pack(fill = "both")
    frameL2.pack(fill = "both")
    frameL3.pack(fill = "both")
    frameR1.pack(fill = "both")
    frameR2.pack(fill = "both", expand = True)

    # Heading lable
    headingLabel = Label(
        frameL1,
        text = "BUDGET BUDGET BUDGET",
        font = ("Bahnschrift Condensed", "25"),
        width = 20,
        bg = "#82b74b",
        fg = "#FFFAF0"
        )
    # second heading lable
    subheadingLabel = Label(
        frameL1,
        text = "SAVE MONEY    SAVE LIFE",
        font = ("Bahnschrift Condensed", "15"),
        width = 20,
        bg = "#405d27",
        fg = "#000000"
        )

    headingLabel.pack(fill = "both")
    subheadingLabel.pack(fill = "both")

    # input labels
    dateLabel = Label(
        frameL2,
        text = "Date:",
        font = ("consolas", "12", "bold"),
        bg = "#80ced6",
        fg = "#000000"
        )

    descriptionLabel = Label(
        frameL2,
        text = "Description:",
        font = ("consolas", "12", "bold"),
        bg = "#80ced6",
        fg = "#000000"
        )

    amountLabel = Label(
        frameL2,
        text = "Amount:",
        font = ("consolas", "12", "bold"),
        bg = "#80ced6",
        fg = "#000000"
        )

    payeeLabel = Label(
        frameL2,
        text = "Payee:",
        font = ("consolas", "12", "bold"),
        bg = "#80ced6",
        fg = "#000000"
        )

    paymentLabel = Label(
        frameL2,
        text = "Payment Method:",
        font = ("consolas", "12", "bold"),
        bg = "#80ced6",
        fg = "#000000"
        )

    dateLabel.grid(row = 0, column = 0, sticky = W, padx = 10, pady = 10)
    descriptionLabel.grid(row = 1, column = 0, sticky = W, padx = 10, pady = 10)
    amountLabel.grid(row = 2, column = 0, sticky = W, padx = 10, pady = 10)
    payeeLabel.grid(row = 3, column = 0, sticky = W, padx = 10, pady = 10)
    paymentLabel.grid(row = 4, column = 0, sticky = W, padx = 10, pady = 10)

    # input types
    description = StringVar()
    payee = StringVar()
    paymentMethod = StringVar(value = "Cash")
    amount = DoubleVar()

    # date input
    dateField = DateEntry(
        frameL2,
        date = datetime.datetime.now().date(),
        font = ("consolas", "11"),
        relief = GROOVE
        )
    # description input
    descriptionField = Entry(
        frameL2,
        text = description,
        width = 20,
        font = ("consolas", "11"),
        bg = "#FFFFFF",
        fg = "#000000",
        relief = GROOVE
        )
    # amount of money input
    amountField = Entry(
        frameL2,
        text = amount,
        width = 20,
        font = ("consolas", "11"),
        bg = "#FFFFFF",
        fg = "#000000",
        relief = GROOVE
        )
    # payee input
    payeeField = Entry(
        frameL2,
        text = payee,
        width = 20,
        font = ("consolas", "11"),
        bg = "#FFFFFF",
        fg = "#000000",
        relief = GROOVE
        )

    # payment input
    paymentField = OptionMenu(
        frameL2,
        paymentMethod,
        *['Cash', 'Credit Card', 'Debit Card', 'Google Pay', 'Apple Pay', 'PayPal']
        )
    paymentField.config(
        width = 15,
        font = ("consolas", "10"),
        relief = GROOVE,
        bg = "#FFFFFF"
        )

    dateField.grid(row = 0, column = 1, sticky = W, padx = 10, pady = 10)
    descriptionField.grid(row = 1, column = 1, sticky = W, padx = 10, pady = 10)
    amountField.grid(row = 2, column = 1, sticky = W, padx = 10, pady = 10)
    payeeField.grid(row = 3, column = 1, sticky = W, padx = 10, pady = 10)
    paymentField.grid(row = 4, column = 1, sticky = W, padx = 10, pady = 10)

    # function buttons under the inputs
    insertButton = Button(
        frameL3,
        text = "Add Expense",
        font = ("Bahnschrift Condensed", "13"),
        width = 30,
        bg = "#90EE90",
        fg = "#000000",
        relief = GROOVE,
        activebackground = "#008000",
        activeforeground = "#98FB98",
        command = addAnotherExpense
        )

    resetButton = Button(
        frameL3,
        text = "Clear The Fields",
        font = ("Bahnschrift Condensed", "13"),
        width = 30,
        bg = "#FF0000",
        fg = "#FFFFFF",
        relief = GROOVE,
        activebackground = "#8B0000",
        activeforeground = "#FFB4B4",
        command = clearFields
        )

    insertButton.grid(row = 0, column = 0, sticky = W, padx = 50, pady = 30)
    resetButton.grid(row = 1, column = 0, sticky = W, padx = 50, pady = 30)

    # function buttons on top of the table
    viewButton = Button(
        frameR1,
        text = "View Selected Expense",
        font = ("Bahnschrift Condensed", "14"),
        width = 30,
        bg = "#92a8d1",
        fg = "#000000",
        relief = GROOVE,
        activebackground = "#A0522D",
        activeforeground = "#FFF8DC",
        command = viewExpenseInfo
        )

    editButton = Button(
        frameR1,
        text = "Edit Selected Expense",
        font = ("Bahnschrift Condensed", "14"),
        width = 30,
        bg = "#92a8d1",
        fg = "#000000",
        relief = GROOVE,
        activebackground = "#A0522D",
        activeforeground = "#FFF8DC",
        command = editExpense
        )

    deleteButton = Button(
        frameR1,
        text = "Remove Selected Expense",
        font = ("Bahnschrift Condensed", "14"),
        width = 30,
        bg = "#92a8d1",
        fg = "#000000",
        relief = GROOVE,
        activebackground = "#A0522D",
        activeforeground = "#FFF8DC",
        command = removeExpense
        )

    deleteAllButton = Button(
        frameR1,
        text = "Remove All Expenses",
        font = ("Bahnschrift Condensed", "14"),
        width = 30,
        bg = "#92a8d1",
        fg = "#000000",
        relief = GROOVE,
        activebackground = "#A0522D",
        activeforeground = "#FFF8DC",
        command = removeAllExpenses
        )

    # set up UI for fucntion buttons
    viewButton.grid(row = 0, column = 0, sticky = W, padx = 10, pady = 10)
    editButton.grid(row = 0, column = 1, sticky = W, padx = 10, pady = 10)
    deleteButton.grid(row = 0, column = 2, sticky = W, padx = 10, pady = 10)
    deleteAllButton.grid(row = 0, column = 3, sticky = W, padx = 10, pady = 10)

    # data table column labels
    data_table = ttk.Treeview(
        frameR2,
        selectmode = BROWSE,
        columns = ('ID', 'Date', 'Payee', 'Description', 'Amount', 'Payment Method')
        )

    # scrollbar for horizontal
    Xaxis_Scrollbar = Scrollbar(
        data_table,
        orient = HORIZONTAL,
        command = data_table.xview
        )

    # scrollbar for vertical
    Yaxis_Scrollbar = Scrollbar(
        data_table,
        orient = VERTICAL,
        command = data_table.yview
        )

    Xaxis_Scrollbar.pack(side = BOTTOM, fill = X)
    Yaxis_Scrollbar.pack(side = RIGHT, fill = Y)

    data_table.config(yscrollcommand = Yaxis_Scrollbar.set, xscrollcommand = Xaxis_Scrollbar.set)

    # data table headings
    data_table.heading('ID', text = 'ID', anchor = CENTER)
    data_table.heading('Date', text = 'Date', anchor = CENTER)
    data_table.heading('Payee', text = 'Payee', anchor = CENTER)
    data_table.heading('Description', text = 'Description', anchor = CENTER)
    data_table.heading('Amount', text = 'Amount', anchor = CENTER)
    data_table.heading('Payment Method', text = 'Payment Method', anchor = CENTER)

    # data table column width for every elements
    data_table.column('#0', width = 0, stretch = NO)
    data_table.column('#1', width = 50, stretch = NO)
    data_table.column('#2', width = 95, stretch = NO)
    data_table.column('#3', width = 200, stretch = NO)
    data_table.column('#4', width = 450, stretch = NO)
    data_table.column('#5', width = 135, stretch = NO)
    data_table.column('#6', width = 140, stretch = NO)

    data_table.place(relx = 0, y = 0, relheight = 1, relwidth = 1)
    main_win.mainloop()


import socket

# SERVER IP, PORT
IP = "192.168.0.195"
PORT = 8086

client = True # We create a while loop so the client keep asking the user for entering new values for new future connections once the server has finished the previous one

while client:

    # Here we have created a menu to inform the user about the program and to give her/him the instructions to be followed

    print('\nHi, you are about to connect with a server.')
    print('Please, follow the instructions.')
    print('\nINSTRUCTIONS\n')
    print('First, you will be asked for a sequence.')
    print('Second, you will be asked for the processes you want to be undertaken for the sequence. Remember to separate them by spaces. ')
    print('If you do not write anything, the server will send you if it is alive or not')

    print('\nThese are the possible processes:\n-len: to calculate the length of the sequence\n-complement: calculate the complement sequence\n-reverse : calculate the reverse sequence\n-countBASIS; count the number of basis\n-percBASIS: calculate the percentage of the basis \n exit: if you want to finish the server')

   # Once we've given the instructions, we ask the user if he/she wants to continue. Maybe the user does not want to continue after the first connection or maybe he is not interested.

    print('Do you want to continue?')
    print('(A) Yes')
    print('(B) No')

    ask =input('Choose between A or B: ')
    ask = ask.upper()

    # If the user wants to continue
    if ask == 'A':
        sequence = input('Please enter a valid DNA sequence to proceed: ')
        sequence = sequence.upper()
        sequence = ' '.join(sequence.split())

        process = input('Please enter the calculations you want to perform separated by spaces: ')
        process = process.lower()
        process = process.lower()
        process = ' '.join(process.split())

        process_list = process.split(" ")
        info = ','.join(process_list)

        print('These are the processes you have selected: ', info)

        msg_list = []
        msg_list.append(sequence)

        msg_list = msg_list + process_list

        msg = '\n'.join(msg_list)

    # If the user does not want to continue
    else:
        client = False # We make the boolean False and we break the loop
        break

    # Now we can create the socket and connect to the server
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # establish the connection to the Server (IP, PORT)
    s.connect((IP, PORT))

    # Send the request message to the server
    s.send(str.encode(msg))

    # Receive the servers respoinse
    response = s.recv(2048).decode()

    # Print the server's response
    print("Response: {}".format(response))

    s.close()


import numpy as np
import pandas as pd
from fit import transform_distribution

from sklearn.experimental import enable_iterative_imputer
from sklearn.impute import IterativeImputer
from sklearn.preprocessing import OneHotEncoder, LabelEncoder
from sklearn import feature_selection
from sklearn import model_selection
from sklearn import metrics

from pandas.api.types import is_string_dtype
from pandas.api.types import is_numeric_dtype

from datetime import datetime

def basic_preprocess(train_complete,
                     test_complete,
                     out_column,
                     drop_columns=None,
                     forced_categorical = None,
                     forced_numeric = None,
                     columns_to_normalize = None,
                     use_labeler = None,
                     manual_processing = None,
                     seed=42,
                     perc=10):
  complete_features = pd.concat([train_complete, test_complete], sort=False).reset_index(drop=True)
  train = train_complete.copy()
  test = test_complete.copy()

  normalize_output = columns_to_normalize and out_column in columns_to_normalize
  if normalize_output:
    columns_to_normalize.remove(out_column)

  if use_labeler:
    if not columns_to_normalize:
      columns_to_normalize = []
    for column in use_labeler:
      if column in columns_to_normalize:
        columns_to_normalize.remove(column)

  convert_dict = {}
  if forced_categorical:
    for column in forced_categorical:
      convert_dict[column] = 'str'

  if forced_numeric:
    for column in forced_numeric:
      convert_dict[column] = 'float64'

  train = train.astype(convert_dict)
  test = test.astype(convert_dict)

  if drop_columns:
    train.drop(drop_columns, axis=1, inplace=True)
    test.drop(drop_columns, axis=1, inplace=True)

  train_data = np.array(train[out_column])

  if normalize_output:
    normalize, denormalize = transform_distribution(train_data)
  else:
    normalize = lambda x: x
    denormalize = lambda x: x

  y = np.array(normalize(train_data))

  train_features = train.drop([out_column], axis=1)
  features = pd.concat([train_features, test], sort=False).reset_index(drop=True)

  impute_with_mode(features)

  numerics = list(features.select_dtypes(include=[np.number]).columns.values)
  if len(numerics) >= 2:
    imp = IterativeImputer(max_iter=10, sample_posterior=False, random_state=seed)
    imp.fit(features[numerics])
    features[numerics] = imp.transform(features[numerics])
  elif numerics:
    impute_with_median(features)

  if use_labeler:
    labeler = LabelEncoder()
    for column in use_labeler:
      features[column] = labeler.fit_transform(features[column])

  final_features = pd.get_dummies(features).reset_index(drop=True)
  if columns_to_normalize:
    normalize_columns(final_features, columns_to_normalize)

  if manual_processing:
    final_features = manual_processing(final_features, complete_features)

  X = final_features.iloc[:len(y), :]
  X_sub = final_features.iloc[len(X):, :]

  #print('selecting relevant features')
  #X, X_sub = select_features(X, y, X_sub, final_features.columns, perc=perc)

  return X, y, X_sub, denormalize

def impute_with_median(dataframe):
  columns = list(dataframe.select_dtypes(include=[np.number]).columns.values)
  for c in columns:
    the_median = dataframe[c].median()
    dataframe[c] = dataframe[c].fillna(the_median)

def impute_with_mode(dataframe):
  columns = list(dataframe.select_dtypes(exclude=[np.number]).columns.values)
  for c in columns:
    the_mode = dataframe[c].mode()[0]
    dataframe[c] = dataframe[c].fillna(the_mode)

def normalize_columns(dataframe, columns_to_normalize):
  for c in columns_to_normalize:
    data = dataframe[c]
    if is_numeric_dtype(data):
      if data.nunique() >= 3:
        normalize, _ = transform_distribution(data)
        dataframe[c] = normalize(data)

def select_features(X, y, X_sub, feature_name, perc=10, max_depth=5, verbose=2):
  import sklearn.ensemble
  from boruta import BorutaPy

  rf = sklearn.ensemble.RandomForestRegressor(max_depth=max_depth)
  feat_selector = BorutaPy(rf, n_estimators='auto', perc=perc, verbose=verbose)
  feat_selector.fit(X.values, y)

  used_features = [feature_name[i] for i, x in enumerate(feat_selector.support_) if x]
  print(used_features)
  return feat_selector.transform(X.values), feat_selector.transform(X_sub.values)

def preprocess( root,
                out_column,
                drop_columns=None,
                forced_categorical = None,
                forced_numeric = None,
                columns_to_normalize = None,
                use_labeler = None,
                manual_processing = None,
                perc=10):
  train = pd.read_csv(root + 'train.csv')
  test = pd.read_csv(root + 'test.csv')

  X, y, X_sub, denormalize = basic_preprocess(train,
                                              test,
                                              out_column,
                                              drop_columns,
                                              forced_categorical,
                                              forced_numeric,
                                              columns_to_normalize,
                                              use_labeler,
                                              manual_processing,
                                              perc)

  import dill
  dill.dump(denormalize, open(root + 'denormalize.dill', 'wb'))

  X.to_csv(root + 'X.csv', index = False)
  ydf = pd.DataFrame()
  ydf[out_column] = y
  ydf.to_csv(root + 'y.csv', index = False)
  X_sub.to_csv(root + 'X_sub.csv', index = False)


import numpy as np
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.cluster import KMeans
from skopt import gp_minimize

class ProcessMissingValues(BaseEstimator, TransformerMixin):
    def __init__(self,
                columns,
                cat_columns,
                type_columns='categorical',
                num_imputer=None):
        self.columns = columns
        self.cat_columns = cat_columns
        self.type_columns = type_columns
        self.num_imputer = None

    def fit(self, X, y=None):
        return self

    def transform(self, X):
        if self.type_columns == 'categorical':
            X = self.transform_categorical(X)

        else:
            X = self.transform_numerical(X)

        return X

    def transform_categorical(self, X):
        for col in self.columns:
            X[col] = X[col].replace(-999, np.nan)
            X['NA_' + col] = X[col].isna().astype(np.int8)
            X[col].fillna('UNKNOWN', inplace=True)

        return X

    def transform_numerical(self, X):
        self.columns = [col for col in X.columns if 'NA_' not in col and col not in self.cat_columns]
        for col in self.columns:
            X[col] = X[col].replace(-999, np.nan)
            if self.num_imputer == None:
                imputer = X[col].dropna().median()
            else:
                imputer = self.imputer
            X['NA_' + col] = X[col].isna().astype(np.int8)
            X = X.fillna(imputer)

        return X

class Categorify(BaseEstimator, TransformerMixin):
    def __init__(self,
                columns,
                freq_treshhold=5,
                lowfrequency_id=0,
                unkown_id=1):

        self.columns = columns
        self.freqs = []
        self.freq_treshhold = freq_treshhold
        self.lowfrequency_id = lowfrequency_id
        self.unkown_id = unkown_id

    def fit(self, X, y=None):
        return self

    def transform(self, X):
        self.make_columns(X)
        for idx, col in enumerate(self.columns):
            col_name = self.freqs[idx].columns[0]
            X = X.merge(self.freqs[idx], how='left', on=col, suffixes=("_", ""))
            if col_name + "_" in X.columns:
                X.drop(col_name + "_", axis=1, inplace=True)

        return X

    def make_columns(self, X):
        self.freqs = []
        for col in self.columns:
            freq = X[col].value_counts()
            freq = freq.reset_index()
            freq.columns = [col, 'count']
            freq = freq.reset_index()
            freq.columns = [col + '_Categorify', col, 'count']
            freq[col + '_Categorify'] = freq[col + '_Categorify'] + 2
            freq.loc[freq['count'] < self.freq_treshhold, col + '_Categorify'] = self.lowfrequency_id
            freq.loc[freq[col]=='UNKNOWN', col + '_Categorify'] = self.unkown_id
            freq = freq.drop('count', axis=1)
            self.freqs.append(freq)

class GetRidCategoricalFeatures(BaseEstimator, TransformerMixin):
    def __init__(self, columns):
        self.columns = columns

    def fit(self, X, y=None):
        return self

    def transform(self, X):
        for col in self.columns:
            categority_col = col + '_Categorify'
            if col in X.columns:
                X.drop([col], axis=1,inplace=True)

        return X

class CountEncoding(BaseEstimator, TransformerMixin):
    def __init__(self,
                columns):
        self.columns = columns
        self.ces = []

    def fit(self, X, y=None):
        return self

    def transform(self, X):
        for idx, col in enumerate(self.columns):
            col_name = col + '_Categorify'
            ce = X[col_name].value_counts()
            ce = ce.reset_index()
            ce.columns = [col_name, 'CE_' + col]
            X = X.merge(ce, how='left', on=col_name)

        return X

class StandardNumerical(BaseEstimator, TransformerMixin):
    def __init__(self):
        self.columns = []

    def fit(self, X, y=None):
        return self

    def transform(self, X):
        self.columns = [col for col in X.columns if "NA" not in col]
        for col in self.columns:
            X[col] = X[col].replace(-999, np.nan)
            median = X[col].dropna().median()
            if median != median:
                X.drop(col, axis=1, inplace=True)
            else:
                X[col].fillna(median, inplace=True)
                X[col]= (X[col] - np.mean(X[col]))/np.std(X[col])

        return X

class CombiningColumns(BaseEstimator, TransformerMixin):
    def __init__(self, list_combined, train_size, col_type='categorical'):
        self.list_combined = list_combined
        self.col_type = col_type
        self.space = [(2, 20),
                    (200, 1000),
                    (2, 20),
                    (1e-5, 1e-3, 'log-uniform')]
        self.train_size = train_size
        self.clusters = {}
        self.names_col = []

    def fit(self, X, y):
        if self.col_type == 'numerical':
            X_ = X[:self.train_size, :]
            y_ = y[:self.train_size]
            for cols in self.list_combined:
                def tune_kmeans(params):
                    n_clusters, max_iter, n_init, tol = params

                    kmeans = KMeans(n_clusters=n_clusters,
                                    max_iter=max_iter,
                                    tol=tol, n_init=n_init,
                                    n_jobs=-1,
                                    random_state=42)
                    cluster = df_copy.copy()
                    cluster["Cluster"] = kmeans.fit_predict(cluster, y)
                    cluster["Cluster"] = cluster["Cluster"].astype("category")
                    cluster["y"] = y_.to_numpy()
                    return -cluster[["Cluster", "y"]].groupby("Cluster").mean().var()["y"]

                df_copy = X_[cols].copy()
                res = gp_minimize(tune_kmeans, self.space, random_state=42, verbose=0, n_calls=30)

                name_col = cols[0] + "_" + cols[1]

                n_clusters, max_iter, n_init, tol = res.x
                kmeans = KMeans(n_clusters=n_clusters,
                                max_iter=max_iter,
                                tol=tol, n_init=n_init,
                                n_jobs=-1,
                                random_state=42)

                df_copy[name_col] = kmeans.fit_predict(df_copy, y)
                df_copy[name_col] = df_copy[name_col].astype("category")
                self.clusters[name_col] = kmeans
                self.names_col.append(name_col)

        return self

    def transform(self, X):
        if self.col_type == 'categorical':
            X = self.transform_cat(X)

        else:
            X = self.transform_num(X)

        return X

    def transform_cat(self, X):
        for cols in self.list_combined:
            if len(cols) == 2:
                name_col = cols[0] + '_' + cols[1]
                X[name_col] = X[cols[0]].astype(str) + "_" +  X[cols[1]].astype(str)
            else:
                name_col = cols[0] + '_' + cols[1] + '_' + cols[2]
                X[name_col] = X[cols[0]].astype(str) + "_" +  X[cols[1]].astype(str) + '_' + X[cols[2]].astype(str)

        return X

    def transform_num(self, X):
        for idx, cols in enumerate(self.list_combined):
            name_col = self.names_col[idx]
            X[name_col] = self.clusters[name_col].predict(X[cols].copy())
            X[name_col] = X[name_col].astype("category")

        return X


"""
Created on Mar, 2021
@author: Morteza Moghaddassian
@Project: ECE1508 - NetSoft Course
"""
# paho is a client library to communicate with Mosquitto broker that implements MQTT V3.1.1
import paho.mqtt.client as paho
import time
from datetime import datetime
from multiprocessing import Process, Manager
import os
import csv
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import matplotlib.dates as mpl_dates
import pandas

'''
    @class_name: Subscriber
    @role: Subscribing for contents and keeping them in memory.
    @number of methods: 4
    @access modifier: public
'''

class Subscriber:
    # The topic that the subscriber needs to use for receiving the content from the message broker.
    subscriber_topic = None
    # Broker IP address.
    broker_address = None
    # The port that the broker is listening on for incoming connections. The default is 1883 for Mosquitto broker.
    broker_port = None
    # Is used to save the message received from the broker.
    message_received = ''

    '''
        @input: address, port, topic
        @role: Constructor method
    '''
    def __init__(self, address, port, topic, shared_dict):
        # The broker IP address (142.150.208.235)
        self.broker_address = str(address)
        # The broker port number (1883)
        self.broker_port = int(port)
        # Subscription Topic
        self.subscriber_topic = str(topic)

        # Shared dict to record data received from broker
        self.shared_dict = shared_dict

    '''
        @input: client, userdate, flags, rc
        @role: Is being used to pair with the on_connect method on the broker to exchange the subscription topic.
    '''
    def on_connect(self, client, userdata, flags, rc):
        # Subscribing for contents with the specified topic.
        client.subscribe(self.subscriber_topic)

    '''
        @input: client, userdata, message
        @role: Is being used to pair with the on_message method on the broker to exchange the message with the topic of interest.
    '''
    def on_message(self, client, userdata, message):
	    # Printing the message which is obtained from the broker.
        msg_str = str(message.payload, encoding='utf-8')
        print(msg_str)

        # Update the topic data in shared_dict
        info = self.subscriber_topic[self.subscriber_topic.rfind('/')+1:]
        self.shared_dict[info] = (msg_str, datetime.now())
        # print(self.shared_dict)

        # Disconnecting from the broker.
        client.disconnect()

    '''
        @role: Subscribing to the broker and enabling the retrieval of the contents specified by the topics of interest.
    '''
    def subscribe(self):
        # Creating an MQTT client object.
        sub = paho.Client()
        # An infinite loop to keep the subscriber alive.
        while True:
            # Connecting to the broker.
            sub.connect(self.broker_address, self.broker_port)
            # Receiving the message (MMG)
            sub.on_message = self.on_message
            # Providing the topic of interest.
            sub.on_connect = self.on_connect
            # Keep the subscriber running until the whole message is received.
            sub.loop_forever()

'''
    @role: Process that runs a Subscriber object with a specific topic
'''
def do_subscribe(topic, shared_dict):
    print("do_subscribe process id = ", os.getpid())
    # signal.signal(signal.SIGINT, signal.SIG_IGN)    # ignore Interrupt from keyboard (CTRL + C), do not raise KeyboardInterrupt

    broker_address = "142.150.208.235"
    broker_port = 1883

    subscriber = Subscriber(broker_address, broker_port, topic, shared_dict)
    subscriber.subscribe()

'''
    @role: Process that periodically writes data in shared_dict to the timeseries file
'''
def do_writefile(tsfilename, info_topics, shared_dict, interval_sec):
    """will use info_topics[-1]'s timestamp in shared_dict to write row"""

    print("do_writefile process id = ", os.getpid())

    with open(tsfilename, 'w', newline='') as csvfile:
        csvwriter = csv.writer(csvfile)
        # Write header row
        csvwriter.writerow(info_topics + ["timestamp", "ts_writerow"])  # timestamp: time when lastinfo was received; ts_writerow: time when this row is written to file

    while True:
        time.sleep(interval_sec)

        lastinfo = info_topics[-1]
        lastinfo_ts = None
        data = []
        try:
            for info in info_topics:
                data.append(shared_dict[info][0])
                if info == lastinfo:
                    lastinfo_ts = shared_dict[info][1]

            data.append(lastinfo_ts)
            data.append(datetime.now())

            # Append a new row in the timeseries file
            with open(tsfilename, 'a', newline='') as csvfile:  # Windows newline characters:'\r\n', Unix:'\n', newline='' writes '\n' untranslated
                csvwriter = csv.writer(csvfile)
                csvwriter.writerow(data)

        except KeyError as keyerr:  # KeyError when the Subscriber has not received anything, thus has not created its entry in shared_dict
            print("Caught: \t", keyerr)

'''
    @role: The function that updates the matplotlib figure
'''
def animate(frame, *fargs):
    tsfilename, info_topics = fargs

    try:
        data = pandas.read_csv(tsfilename, parse_dates=["timestamp"])
        # print(plt.gcf().get_axes())
        # print(data)

        plt.clf()   # clear the current figure
        fig = plt.gcf()
        fig.set_constrained_layout(True)    # automatically adjust subplots and decorations like legends so that they fit in the figure
        # Divide the figure to a grid layout with 4 rows x 2 cols
        gridspec = fig.add_gridspec(4, 2)  # add_gridspec(nrows, ncols) return a GridSpec: grid layout to place subplots within figure

        numinfo = len(info_topics)

        # Plot in each subplot
        for i in range(numinfo):
            info = info_topics[i]
            # add_subplot(nrows, ncols, index): add a subplot that will take the index position on a grid with nrows rows and ncols columns, index starts at 1 in the upper left corner
            # ax = fig.add_subplot(numinfo, 1, i+1)

            if i == 0:      # first subplot occupies the entire first row: gridspec[0, :]
                ax = fig.add_subplot(gridspec[i, :])
                # Format the dates in x majortick labels
                date_formatter = mpl_dates.DateFormatter('%H:%M')  # "%Y-%m-%d %H:%M:%S"
                ax.xaxis.set_major_formatter(date_formatter)
                plt.xticks(horizontalalignment="right", rotation=10)    # set rotation of xtick labels
            else:
                ax = fig.add_subplot(gridspec[int((i+1)/2), (i+1)%2])
                # Do not show xtick labels for subplots except the first one to save space
                plt.setp(ax.get_xticklabels(), visible=False)   # set the xtick labels to be invisible

            # Plot the data in the subplot
            ax.plot_date(data["timestamp"], data[info])

            # Set relevant title for the subplot based on topic name
            if info.endswith("_percent"):
                title = info.replace("_percent", " utilization")
                title = title.replace("cpu", "CPU")
                title = title.replace("mem", "Memory")
                ax.set_title(title + " (%)")
                # ax.set_ylabel(title + " (%)")
            elif info.startswith("mem_"):
                title = info[4:]
                title = title.capitalize() + " memory"
                ax.set_title(title + " (MB)")
                # ax.set_ylabel(title + " (MB)")
            elif info.startswith("bytes_"):
                title = info.replace("_", " ")
                title = title.replace("recv", "received")
                title = title.capitalize() + " on eth0"
                ax.set_title(title)
                # ax.set_ylabel(title + " (byte)")

    except (pandas.errors.EmptyDataError, FileNotFoundError) as err:
        # pandas.errors.EmptyDataError when pandas.read_csv() reads an empty csv file, i.e. the file writing process has yet to write anything
        # FileNotFoundError when pandas.read_csv() cannot find the timeseries file, i.e. the file writing process has yet to create the file
        print("Caught: \t",err)

'''
    @role: Process that plots data in the timeseries file in real time
'''
def do_plotfile(tsfilename, info_topics, interval_sec):
    print("do_plotfile process id = ", os.getpid())

    # animate() is called every interval_sec seconds to update the figure
    ani = FuncAnimation(plt.gcf(), animate, interval=interval_sec*1000, fargs=(tsfilename, info_topics))
    plt.show()

if __name__ == "__main__":
    # to be completeled by students...

    print("Parent process id = ", os.getpid())

    # Create a shared dict
    manager = Manager()
    shared_dict = manager.dict()

    # Write to file and plot from file every interval_sec seconds
    interval_sec = 60
    tsfilename = "timeseries.csv"

    info_topics = ["cpu_percent", "mem_total", "mem_used", "mem_free", "mem_percent", "bytes_sent", "bytes_recv"]
    processes = []

    # create do_subscribe() processes
    for info in info_topics:
        topic = "ece1508/netsoft7/" + info
        process = Process(target=do_subscribe, args=[topic, shared_dict])
        # process.daemon = True     # When a process exits, it attempts to terminate all of its daemonic child processes. Note that a daemonic process is not allowed to create child processes.
        process.start()
        processes.append(process)

    # create do_writefile() process
    process = Process(target=do_writefile, args=[tsfilename, info_topics, shared_dict, interval_sec])
    process.start()
    processes.append(process)

    # create do_plotfile() process
    process = Process(target=do_plotfile, args=[tsfilename, info_topics, interval_sec])
    process.start()
    processes.append(process)

    try:
        for process in processes:
            process.join()
    except KeyboardInterrupt:
        # when parent process receives CTRL-C, terminate all created processes
        print("Parent received CTRL-C")
        for process in processes:
            process.terminate()
            process.join()

    # do_subscribe("ece1508/netsoft7/cpu_percent", shared_dict)


class ListNode:
    def __init__ (self, val=0, next=None):
        self.val = val
        self.next = next

class Solution:
    def gcd(self, a, b):
        while b:
            a, b = b, a%b
        return a

    def insertGCDs(self,head):
        """
        :type head: Optional[ListNode]
        :rtype: Optional[ListNode]
        """
        if not head:
            return None

        curr = head

        while curr and curr.next:
            gcd = self.gcd(curr.val, curr.next.val)

            new_node = ListNode(gcd)
            new_node.next = curr.next
            curr.next = new_node

            curr = curr.next.next

        return head

# refer hrlperimgs/GCDinsert.png


import whisper
import numpy as np

class WhisperModel():
    def __init__(self, model_type="large"):
        print("loading whisper")
        self.model = whisper.load_model(model_type, download_root="/app/model/whisper/")
        self.language = 'en'
        self.task = 'transcribe'

    async def set_model_settings(self, language, task):
        self.language = language
        self.task = task
        message = {"message": "Settings set correctly"}
        return message

    async def process_audio(self, data):
        audio = np.array([], dtype=np.int16)
        buffer = np.frombuffer(data["bytes"], dtype=np.int16).astype(np.float32) / 2**15
        audio = np.concatenate([audio, buffer])
        result = self.model.transcribe(audio, language=self.language, task=self.task)
        print(result["text"])
        return result["text"].strip()


#!/user/bin/env python3
'''
* make `data_set`, dict object {index: data}
    `data` is dict object, whose keys are:
    *  'label' : `label_vector`, list object subset in [0,...,L-1]
    * 'feature': `feature_vector`, dict object {coordinate index: value}
'''

class FileReader(object):
    def __init__(self):
        self.L = None # label vector space dimension
        self.M = None # feature vector space dimension
        self.N = None # the size of `data_set`

    def read(self, file_name: str) -> dict:
        with open(file_name, "r") as f:
            header = f.readline()
            N, M, L = (int(x) for x in header.split())
            self.L, self.M, self.N = L, M, N

            data_index = 0
            data_set = {i:{} for i in range(N)}

            line = f.readline()
            while line:
                data = {}
                label_term = line.split()[0].split(',')
                if ':' in label_term[0]:
                    data['label'] = []
                    feature_term = label_term
                else:
                    data['label'] = [int(l) for l in label_term]
                    feature_term = line.split()[1:]

                data['feature'] = {int(m.split(':')[0]): float(m.split(':')[1]) for m in feature_term}

                data_set[data_index] = data
                data_index += 1
                line = f.readline()

        return data_set


#Machine Vision and Robotics Course Project
#Fire detection using Image Processing
#import modules
import cv2#Computer vision module for video capture and image processing
import numpy as np#Python numbers module for matrix manipulation
import time#time module to measure time for pauses
import winsound# windows sound module for alarm
v=cv2.VideoCapture(0)#Capture live video using camera 0 (default webcam) and store values in v
time.sleep(2)#pause for 2 seconds
fdc=cv2.CascadeClassifier(r'C:\python\python36\fire_detection.xml')#Load generated cascade classifier into fdc
frequency = 2500#Define alarm frequency as 2500 Hz
duration = 500#Define alarm duration as 500 ms
while(1):#Loop code continously
        d,i=v.read()#Get frame from video capture. If frame is obtained, store TRUE in d and store image matrix in i
        print(i.shape)#Print image dimensions in console
        fire=fdc.detectMultiScale(i,1.3,9)#Use detectMultiScale function to detect objects, passing image,scale factor and minNeighbors as parameters
        print(fire)#Print dimensions of detected object (fire) matrix in console

        if(len(fire)>=1):#If 1 or more objects (fires) are detected
         for x,y,w,h in fire:#Store coordinates of upper-left corner in x and y, and widht and height in w and h
              font=cv2.FONT_HERSHEY_SIMPLEX#Define font
              cv2.putText(i,'FIRE DETECTED',(x-w,y-h),font,0.5,(0,255,255),2,cv2.LINE_AA)#Display "FIRE DETECTED" test
              cv2.rectangle(i,(x,y),(x+w,y+h),(0,0,255),2)#Display rectangle around fire
              time.sleep(0.2)#Pause of  for 0.2 seconds
              winsound.Beep(frequency, duration)#Sound alarm
        cv2.imshow('image',i)#Display processed image on screen

        if(len(fire)>=1):#If 1 or more objects (fires) are detected

           print('FIRE DETECTED')#Print "FIRE DETECTED" to console
        k=cv2.waitKey(5)#If keypress is detected, wait 5 seconds
        if(k==ord('q')):#If detected key is q
                cv2.destroyAllWindows()#Stop all processing
                break#Exit loop


import json
import os
import re

def read_json_file(file):
    if os.path.isfile(file):
        print(f'file {file}')
    else:
        exit()

    log = json.load(open(file, 'r'))

    return log

def flat_json_to_kv(an_object, key, kv_result, layer) -> object:
    '''

    :param key:
    :param layer:
    :param an_object:
    :param kv_result:
    :param json_object: a dict object with multiple layers
    :return: a flat dict
    '''

    if isinstance(an_object, dict):
        for key, value in an_object.items():
            flat_json_to_kv(value, key, kv_result, 0)
    elif isinstance(an_object, list):
        for item in an_object:
            flat_json_to_kv(item, key, kv_result, 0)
    else:
        if isinstance(an_object, int) or isinstance(an_object, float):
            an_object = str(an_object)
        if kv_result.get(key, None):
            kv_result[key] = kv_result[key] + " , " + an_object
        else:
            kv_result.update({key: an_object})

    return kv_result

if __name__ == "__main__":
    file = 'log.json'
    log = read_json_file(file)
    print(log)
    result = flat_json_to_kv(log, "", {}, 0)
    print(result)


''' Inference for the DreamBooth model. '''

import os
import zipfile
import predictor

import runpod
from runpod.serverless.utils import download, upload, validator, rp_cleanup

MODEL = predictor.Predictor()
MODEL.setup()

def run(job):
    '''
    Run inference on the model.

    input format:
    {
        "instance_prompt": str,
        "class_prompt": str,
        "instance_data": str,
        "samples":[
            {
                "save_sample_prompt": str,
                "save_sample_negative_prompt": str,
                "guidance_scale": float,
                "save_inference_steps": int,
                "n_save_sample": int,
            },
             {
                "save_sample_prompt": str,
                "save_sample_negative_prompt": str,
                "save_guidance_scale": float,
                "save_infer_steps": int,
                "n_save_sample": int,
            },
        ]
    }

    '''
    job_input = job['input']

    job_input['seed'] = job_input.get('seed', int.from_bytes(os.urandom(2), "big"))

    job_input["instance_data"] = download.download_input_objects(job_input["instance_data"])[0]

    MODEL.samples = job_input.get("samples", None)

    job_results = MODEL.predict(
        instance_prompt=job_input["instance_prompt"],
        class_prompt=job_input["class_prompt"],
        instance_data=job_input["instance_data"],
        class_data=job_input.get("class_data", None),
        num_class_images=job_input.get("num_class_images", 50),
        pad_tokens=job_input.get("pad_tokens", False),
        with_prior_preservation=job_input.get("with_prior_preservation", True),
        prior_loss_weight=job_input.get("prior_loss_weight", 1.0),
        seed=job_input.get("seed", 512),
        resolution=job_input.get("resolution", 512),
        center_crop=job_input.get("center_crop", False),
        train_text_encoder=job_input.get("train_text_encoder", True),
        train_batch_size=job_input.get("train_batch_size", 1),
        sample_batch_size=job_input.get("sample_batch_size", 2),
        num_train_epochs=job_input.get("num_train_epochs", 1),
        max_train_steps=job_input.get("max_train_steps", 2000),
        gradient_accumulation_steps=job_input.get("gradient_accumulation_steps", 1),
        gradient_checkpointing=job_input.get("gradient_checkpointing", False),
        learning_rate=job_input.get("learning_rate", 1e-6),
        scale_lr=job_input.get("scale_lr", False),
        lr_scheduler=job_input.get("lr_scheduler", "constant"),
        lr_warmup_steps=job_input.get("lr_warmup_steps", 0),
        use_8bit_adam=job_input.get("use_8bit_adam", True),
        adam_beta1=job_input.get("adam_beta1", 0.9),
        adam_beta2=job_input.get("adam_beta2", 0.999),
        adam_weight_decay=job_input.get("adam_weight_decay", 1e-2),
        adam_epsilon=job_input.get("adam_epsilon", 1e-8),
        max_grad_norm=job_input.get("max_grad_norm", 1.0),

        # samples=job_input.get("samples", None),
        save_guidance_scale=job_input.get("save_guidance_scale", 7.5),
        save_sample_prompt=job_input.get("save_sample_prompt", None),
        save_sample_negative_prompt=job_input.get("save_sample_negative_prompt", None),
        n_save_sample=job_input.get("n_save_sample", 1),
        save_infer_steps=job_input.get("save_infer_steps", 50),
    )

    os.makedirs("output_objects", exist_ok=True)

    with zipfile.ZipFile(job_results, 'r') as zip_ref:
        zip_ref.extractall("output_objects")

    img_paths = []
    for file in os.listdir("output_objects/samples"):
        img_paths.append(os.path.join("output_objects/samples", file))

    job_output = []

    image_urls = upload.files(job['id'], img_paths)

    for index, image_url in enumerate(image_urls):
        job_output.append({
            "image": image_url,
            "seed": job_input['seed'] + index
        })

    rp_cleanup.clean(['cog_class_data', 'cog_instance_data', 'checkpoints'])

    return job_output

runpod.serverless.start({"handler": run})


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
from datetime import datetime

file_in = "RngStream.cpp funclass.cpp RngStreamSupp.cpp driverLTM_mpi_mod.cpp"
file_out = "mpi_ltm"
file_time = "ResultTime"
max_np = 6
max_ntest = 10

date = datetime.now().strftime('%d-%m-%Y')
hour = datetime.now().strftime('%H-%M-%S')
file_time = (file_time+'_'+date+'_'+hour+".txt")

print("Compiling MPI Files ...		",end="",flush=True)
print("[WAIT]",end='\r')
os.system("mpic++ " + file_in + " -o " + file_out)
print("Compiling MPI Files ...          ",end="",flush=True)
print("[DONE]\n")
print("Tests Per Process: "+str(max_ntest)+"\n")
print("Starting ...\n")

file2 = open (file_time,"a")
file2.write("np,time,speedup,effiency\n")
file2.close()

for numX in range(max_np):
	np = str(numX + 1)
	Sum = 0

	for numY in range(max_ntest):
		print("Test With " + np + " Processes ...         ",end="",flush=True)
		print("[" + str(numY+1) + "]",end='\r')
		os.system("mpirun -np " + np + " ./" + file_out + "> temp.txt")
		file1 = open("temp.txt","r+")
		file1.seek(0)
		Sum += float(file1.readline())
		file1.close()

	time = Sum/max_ntest
	if numX == 0:
		Sec=time
	speed = Sec/time
	efic = speed/(numX+1)

	print("Test With " + np + " Processes ...         ",end="",flush=True)
	print("[DONE]",end='\n')

	print("Tiempo: ",end="",flush=True)
	print('{0:.6f}'.format(time),end="",flush=True)
	print(" Speedup: ",end="",flush=True)
	print('{0:.6f}'.format(speed),end="",flush=True)
	print(" Efficiency: ",end="",flush=True)
	print('{0:.6f}'.format(efic),end="\n\n")

	file2 = open (file_time,"a")
	file2.write(np+","+str(time)+","+str(speed)+","+str(efic)+"\n")
	file2.close()

os.system("rm temp.txt")
print("... Finished.")


import imp
import time
import json
import uvicorn
from threading import Thread
from datetime import datetime
from kafka import KafkaProducer

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from app.config.settings import config
from app.config.docs import documentation_config
from app.config.cors import cors_config
from app.osm.osm_adapter import OSM_Adapter
from app.routers import metrics, service_discovery, db_helper, images, collectors, hosts, perfsonar
from app.data.postgres import postgres_manager

from sqlalchemy import select

from app.data.models.collector_type import CollectorType
from app.data.models.collector import Collector
from app.data.models.image import Image
from app.data.models.host_state import HostState
from app.gnocchi.gnocchi_puller import GnocchiPuller
from app.grafana_authorization.grafana_authorization import GrafanaAuthorizationMiddleware, GrafanaDashboard

app = FastAPI(**documentation_config)

app.add_middleware(
    CORSMiddleware,
    **cors_config
)

# Service Discovery
def service_discover_start():
    print("---------Initiating Service Discovery----------")
    osm_adapter = OSM_Adapter(config.OSM_HOST, config.OSM_USER, config.OSM_PWD, config.OSM_PROJECT)

    while True:
        time.sleep(30)
        new_vdus_ips = osm_adapter.get_vdus_ip()

# Service Discovery
def gnocchi_puller_start():
    print("---------Initiating Gnocchi Puller----------")
    gnocchi_puller = GnocchiPuller(config.OPENSTACK_HOST, config.OPENSTACK_ID, config.OPENSTACK_SECRET)

    producer = KafkaProducer(
        bootstrap_servers=['%s:%s' % (config.KAFKA_HOST, config.KAFKA_LISTENER_PORT)],
        value_serializer=lambda x: json.dumps(x).encode('utf-8')
    )

    while True:
        time.sleep(30)
        hosts = gnocchi_puller.get_gnocchi_targets()

        if hosts:
            for host in hosts:
                metrics_list = gnocchi_puller.get_metrics_list(host)

                metrics = {
                    "instance": host.ip
                }
                for metric_name, metric_id in metrics_list.items():
                    values = gnocchi_puller.get_metric_value(metric_id)
                    value = values[len(values) - 1]

                    # Gnocchi's date
                    date = value[0].split('+')[0]
                    dt = datetime.strptime(date, "%Y-%m-%dT%H:%M:%S")

                    # Actual date
                    actual_timestamp = time.time()

                    metrics[metric_name] = [
                        int(actual_timestamp),
                        value[1],
                        value[2]
                    ]

                print(metrics)
                # escrever no kafka
                producer.send('gnocchi', value=metrics)

# Initialize Database
def db_init():
    host_states = [
        HostState('ACTIVE'),
        HostState('INACTIVE'),
    ]
    postgres_manager.save_all(host_states)

    collectors_type = [
        CollectorType('PULL'),
        CollectorType('PUSH'),
    ]
    postgres_manager.save_all(collectors_type)

    pull = postgres_manager.get_by_name(CollectorType, 'PULL')
    push = postgres_manager.get_by_name(CollectorType, 'PUSH')

    collectors = [
        Collector('Prometheus', 'metrics', 9100, pull.id),
        Collector('Gnocchi', '', 0, pull.id),
        Collector('Telegraf', '', 0, push.id),
    ]
    postgres_manager.save_all(collectors)

    os_images = [
        Image('ubuntu_2004_5gasp_monitoring_prometheus'),
        Image('ubuntu_2004_5gasp_monitoring_telegraf'),
        Image('ubuntu-20.04-server-cloudimg-amd64')
    ]
    postgres_manager.save_all(os_images)

    image = postgres_manager.get_by_name(Image, 'ubuntu_2004_5gasp_monitoring_prometheus')
    prometheus = postgres_manager.get_by_name(Collector, 'Prometheus')

    image.collectors.append(prometheus)
    postgres_manager.commit()

    image = postgres_manager.get_by_name(Image, 'ubuntu_2004_5gasp_monitoring_telegraf')
    telegraf = postgres_manager.get_by_name(Collector, 'Telegraf')

    image.collectors.append(telegraf)
    postgres_manager.commit()

    image = postgres_manager.get_by_name(Image, 'ubuntu-20.04-server-cloudimg-amd64')
    gnocchi = postgres_manager.get_by_name(Collector, 'Gnocchi')

    image.collectors.append(gnocchi)
    postgres_manager.commit()

# Init Grafana Dashboard
GrafanaAuthorizationMiddleware.get_and_create_dashboard_for_user_if_not_exists(config.GRAFANA_USER, GrafanaDashboard.ESIGHT_INTERFACES)
GrafanaAuthorizationMiddleware.get_and_create_dashboard_for_user_if_not_exists(config.GRAFANA_USER, GrafanaDashboard.ESIGHT_SLOTS)

app.include_router(metrics.router)
app.include_router(service_discovery.router)
app.include_router(db_helper.router)
app.include_router(images.router)
app.include_router(collectors.router)
app.include_router(hosts.router)
app.include_router(perfsonar.router)

if not postgres_manager.get_by_name(CollectorType, 'PULL'):
    print("---------Initiating Empty DataBase----------")
    db_init()

# Threads
thread_service_discovery = Thread(target=service_discover_start)
thread_service_discovery.start()

time.sleep(5)

thread_gnocchi_puller = Thread(target=gnocchi_puller_start)
thread_gnocchi_puller.start()


import requests
from lxml import etree

def get_mhname():
    try:
        url = 'https://18comic.live/'
        header = {
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36",
        }
        res = requests.get(url, headers=header)
        tree = etree.HTML(res.content)
        names = tree.xpath('//*[@class="video-title title-truncate m-t-5"]/text()')
        href = tree.xpath('//*[@class="well well-sm"]/a/@href')
        cover = tree.xpath('//*[@class="well well-sm"]//div/img/@src')
        author = tree.xpath('//*[@class="well well-sm"]/div[1]/a/text()')
        time = tree.xpath('//*[@class="video-views pull-left"]/text()')
        json = []
        for (m, x, c, a, t) in zip(names, href, cover, author, time):
            # json.append({'name': m, 'url': x, 'cover': f, 'author': z, 'tag': b, 'time': g, 'latest': zx})
            json.append({'name': m, 'url': url + x, 'cover': c, 'author': a, 'time': t})
        print(json)
        return json
    except:
        print('出现错误')
        return 'null'

def mhurl1(url1):
    try:
        url = 'https://18comic.live'
        header = {
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36",
        }
        res = requests.get(url1, headers=header)
        tree = etree.HTML(res.content)
        go_url = tree.xpath('//*[@class="btn btn-primary dropdown-toggle"][1]/@href')
        for i in go_url:
            urls = url + i
        ress = requests.get(urls, headers=header)
        trees = etree.HTML(ress.content)
        names = trees.xpath('//*[@class="top-comname"]/text()')
        imgs_url = trees.xpath('//*[@style="text-align:center;"]/img/@src')
        url1_json = []
        for i in imgs_url:
            url1_json.append({'img': i})
        print(url1_json)
        return url1_json
    except:
        print('出现错误')
        return 'null'

def search_photos(search_query):
    try:
        urls = 'https://18comic.live'
        url = 'https://18comic.live/search/photos?'
        search_querys = 'search_query=' + search_query
        urlss = url + search_querys
        header = {
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36",
        }
        res = requests.get(urlss, headers=header)
        tree = etree.HTML(res.content)
        name = tree.xpath('//*[@class="video-title title-truncate m-t-5"]/text()')
        href = tree.xpath('//*[@class="well well-sm"]/a/@href')
        cover = tree.xpath('//*[@class="lazy_img img-responsive "]/@data-original')
        time = tree.xpath('//*[@class="video-views pull-left"]/text()')
        json = []
        for (n, h, c, t) in zip(name, href, cover, time):
            json.append({'name': n, 'url': urls + h, 'cover': c, 'time': t})
        print(json)
        return json
    except:
        print('出现错误')
        return 'null'

if __name__ == '__main__':
    search_photos('少女前线')
    print('获取最新漫画中\n')
    get_mhname()
    urls = input('请输入漫画url:\n')
    mhurl1(urls)


from typing import List

class Solution:
    def letterCombinations(self, digits: str) -> List[str]:
        map = {
            "2": ["a", "b", "c"],
            "3": ["d", "e", "f"],
            "4": ["g", "h", "i"],
            "5": ["j", "k", "l"],
            "6": ["m", "n", "o"],
            "7": ["p", "q", "r", "s"],
            "8": ["t", "u", "v"],
            "9": ["w", "x", "y", "z"],
        }
        res = []
        for d in digits:
            if len(res) == 0:
                res.extend(map[d])
            else:
                r_len = len(res)
                for i in range(r_len):
                    for letter in map[d]:
                        res.append(res[i] + letter)
                res = res[r_len:]
        return res

Python/Letter Combinations of a Phone Number.py


from pathlib import Path

from libdotfiles.packages import has_installed, try_install
from libdotfiles.util import (
    HOME_DIR,
    PKG_DIR,
    create_symlink,
    distro_name,
    run,
)

FZF_DIR = HOME_DIR / ".fzf"

if distro_name() == "arch":
    try_install("fzf")  # super opener
    try_install("ripgrep")  # super grep

elif distro_name() == "linuxmint":
    if not has_installed("ripgrep"):
        run(
            [
                "curl",
                "-LO",
                "https://github.com/BurntSushi/ripgrep/releases/download/11.0.2/ripgrep_11.0.2_amd64.deb",
            ],
            check=True,
        )
        run(["sudo", "dpkg", "-i", "ripgrep_11.0.2_amd64.deb"], check=True)

    run(
        [
            "git",
            "clone",
            "--depth",
            "1",
            "https://github.com/junegunn/fzf.git",
            FZF_DIR,
        ],
        check=True,
    )
    run(
        [
            FZF_DIR / "install",
            "--key-bindings",
            "--completion",
            "--no-update-rc",
        ],
        check=True,
    )

create_symlink(PKG_DIR / "agignore", HOME_DIR / ".agignore")


import logging
import pickle
import sys
from pathlib import Path
from timeit import timeit

import pandas as pd
import yaml
from nyx_space.cosmic import Cosm, Orbit, Spacecraft, SrpConfig
from nyx_space.mission_design import (
    Event,
    SpacecraftDynamics,
    StateParameter,
    TrajectoryLoader,
    propagate,
    two_body,
)
from nyx_space.monte_carlo import StateParameter, generate_orbits
from nyx_space.plots import plot_traj_errors
from nyx_space.time import Duration, Epoch, TimeSeries, Unit

def test_propagate():
    # Initialize logging
    FORMAT = "%(levelname)s %(name)s %(filename)s:%(lineno)d\t%(message)s"
    logging.basicConfig(format=FORMAT)
    logging.getLogger().setLevel(logging.INFO)

    # Base path
    root = Path(__file__).joinpath("../../../").resolve()

    config_path = root.joinpath("./data/tests/config/")

    sc = Spacecraft.load(str(config_path.joinpath("spacecraft.yaml")))
    # Check that we have loaded this correctly by checking the values from the YAML file
    assert sc.value_of(StateParameter.X) == -9042.862234
    assert sc.value_of(StateParameter.Y) == 18536.333069
    assert sc.value_of(StateParameter.Z) == 6999.957069
    assert sc.value_of(StateParameter.VX) == -3.288789
    assert sc.value_of(StateParameter.VY) == -2.226285
    assert sc.value_of(StateParameter.VZ) == 1.646738
    assert sc.value_of(StateParameter.Cr) == 1.0
    assert sc.value_of(StateParameter.Cd) == 2.2
    assert sc.value_of(StateParameter.FuelMass) == 50.0
    assert sc.epoch.timedelta(Epoch("2018-09-15T00:15:53.098 UTC")) == Duration.zero()
    # Check other stuff that is computed on request
    assert sc.value_of(StateParameter.SMA) == 21999.99774705774

    dynamics = SpacecraftDynamics.load_named(str(config_path.joinpath("dynamics.yaml")))
    # So far, there are no accessors on the dynamics, so we will check what they print out =(
    assert (
        f"{dynamics['lofi']}"
        == "Spacecraft dynamics (with guidance = false): No force models; Orbital dynamics: Point masses of Sun J2000, Earth J2000"
    )
    assert (
        f"{dynamics['hifi']}"
        == "Spacecraft dynamics (with guidance = false): SRP with φ = 1367 W/m^2 and eclipse light-source: Sun J2000, shadows casted by: Sun J2000, Moon J2000;  Orbital dynamics: Point masses of Sun J2000, Earth J2000, Moon J2000; IAU Earth gravity field 10x10 (order x degree)"
    )

    rslt, traj = propagate(sc, dynamics["lofi"], Unit.Day * 5.159)
    # Check that we propagated for the correct duration
    assert rslt.epoch.timedelta(sc.epoch) == Duration("5 days 3 h 48 min 57 s 600 ms")
    # We aren't in two body dynamics so the SMA has changed among other things.
    assert rslt.value_of(StateParameter.SMA) == 22000.024381883788
    # Grab a specific epoch from the trajectory
    sc_state = traj.at(Epoch("2018-09-16T00:16:53 TDB"))
    # Check that we got the right epoch
    assert sc_state.epoch.timedelta(Epoch("2018-09-16T00:16:53 TDB")) == Duration.zero()
    # Save the file to parquet
    traj.to_parquet("lofi.parquet")

    # We can also propagate with a different method
    rslt, traj = propagate(sc, dynamics["lofi"], Unit.Day * 5.159, method="Dormand78")
    assert rslt.epoch.timedelta(sc.epoch) == Duration("5 days 3 h 48 min 57 s 600 ms")

    # Let's now propagate the original spacecraft to its apoapsis, but let's search no more than 2 orbit periods
    rslt_apo, _ = propagate(
        sc,
        dynamics["lofi"],
        sc.orbit.period() * 2,
        event=Event(StateParameter.Apoapsis, 0.0),
    )
    # Let's make sure that worked to within the default precision
    assert abs(rslt_apo.value_of(StateParameter.TrueAnomaly) - 180.0) < 0.001

    event = Event(StateParameter.Apoapsis, 0.0, value_precision=1e-6)

    # But we can also be even more precise
    rslt_apo, traj = propagate(
        sc,
        dynamics["lofi"],
        sc.orbit.period() * 2,
        event=event,
    )
    assert abs(rslt_apo.orbit.ta_deg() - 180.0) <= 1e-6

    # Resample the trajectory at fixed step size of 25 seconds
    traj = traj.resample(Unit.Second * 25.0)

    # Export this trajectory with additional metadata and the events
    # Base path
    root = Path(__file__).joinpath("../../../").resolve()
    outpath = root.joinpath("output_data/")

    # Note: Python interface only supports strings for paths, not Path objects.
    traj.to_parquet(
        str(outpath.joinpath("./lofi_with_events.parquet")),
        metadata={"dynamics": str(dynamics["lofi"])},
        events=[event],
    )

    # Propagate until the lo-fi epoch to compare
    _, traj_hifi = propagate(
        sc,
        dynamics["hifi"],
        epoch=rslt_apo.epoch,
    )

    # Plot both trajectories in RIC frame
    if sys.platform != "win32":
        diff_path = str(outpath.joinpath("./lofi_hifi_ric_diff.parquet"))
        traj.ric_diff_to_parquet(traj_hifi, diff_path)

        traj_diff_ric = pd.read_parquet(diff_path)

        plot_traj_errors(
            traj_diff_ric,
            "LoFi vs HiFi",
            html_out=outpath.joinpath("./md_ric_lofi_hifi_diff.html"),
            show=False,
        )

        plot_traj_errors(
            traj_diff_ric,
            "LoFi vs HiFi",
            html_out=outpath.joinpath("./md_ric_lofi_hifi_diff_velocity.html"),
            vertical=True,
            velocity=True,
            show=False,
        )

    # Resample the trajectory at fixed step size of 25 seconds
    traj = traj.resample(Unit.Second * 25.0)

    # Also export this ground track
    cosm = Cosm.de438()
    iau_earth = cosm.frame("IAU Earth")
    traj.to_parquet("iau_earth_lofi.parquet", groundtrack=iau_earth)

    # Let's also search for this event in the trajectory
    for sc_at_event in traj.find(event):
        print(sc_at_event)
        assert abs(sc_at_event.value_of(StateParameter.TrueAnomaly) - 180.0) <= 1e-6

def test_build_spacecraft():
    """
    Tests that we can build a spacecraft from scratch without an input file
    """
    # Initialize logging
    FORMAT = "%(levelname)s %(name)s %(filename)s:%(lineno)d\t%(message)s"
    logging.basicConfig(format=FORMAT)
    logging.getLogger().setLevel(logging.INFO)

    cosm = Cosm.de438()
    eme2k = cosm.frame("EME2000")  # Earth Mean Equator J2000
    orbit = Orbit.from_keplerian_altitude(
        400.0, 0.01, 15.6, 45.0, 90.0, 75.0, Epoch.system_now(), eme2k
    )
    # Define the SRP
    srp = SrpConfig(2.0)  # 2.0 will be the area
    print(srp)
    assert srp.cr == 1.8  # Default value
    assert srp.area_m2 == 2.0
    sc = Spacecraft(orbit, 150.0, 15.0, srp)
    print(sc)

    assert sc.srp() == srp
    assert sc.drag().area_m2 == 0.0
    assert (
        sc.drag().cd == 2.2
    )  # Default value, but the area is zero, so it doesn't have any effect

    # Using this spacecraft as a template, let's load an OEM file, convert it to Parquet, and ensure we can load it back in.
    # The orbit data will be overwritten with data from the OEM file.
    root = Path(__file__).joinpath("../../../").resolve()

    config_path = root.joinpath("./data/tests/ccsds/oem/LEO_10s.oem")
    output_path = root.joinpath("./output_data/LEO_10s.parquet")
    # Convert from OEM to Parquet will happen on load
    traj = TrajectoryLoader(str(config_path), "oem", str(output_path), sc)
    print(traj)
    # Check that we can pickle the trajectory loader object
    traj_pkl = pickle.dumps(traj)
    traj_unpkl = pickle.loads(traj_pkl)
    assert traj_unpkl == traj
    # Check that we can convert this to a spacecraft trajectory
    traj_sc = traj.to_spacecraft_traj()
    traj_orbit = traj.to_orbit_traj()
    traj_orbit_dc = traj_sc.downcast()
    # Check that we can query it (will raise an exception if we can't, thereby failing the test)
    ts = TimeSeries(
        Epoch("2020-06-01T12:00:00.000000"),
        Epoch("2020-06-01T13:00:00.000000"),
        step=Unit.Minute * 17 + Unit.Second * 13.8,
        inclusive=True,
    )
    for epoch in ts:
        orbit = traj_orbit.at(epoch)
        dc_orbit = traj_orbit_dc.at(epoch)
        sc_orbit = traj_sc.at(epoch).orbit
        # Check params individually
        assert orbit.epoch == sc_orbit.epoch
        assert orbit.x_km == sc_orbit.x_km
        assert orbit.vx_km_s == sc_orbit.vx_km_s
        # Check the downcasted version
        assert dc_orbit.x_km == sc_orbit.x_km
        assert dc_orbit.vx_km_s == sc_orbit.vx_km_s
        assert dc_orbit == sc_orbit

def test_two_body():
    # Build a demo orbit
    cosm = Cosm.de438()
    eme2k = cosm.frame("EME2000")

    assert eme2k.gm() == 398600.435392  # SPICE data, not GMAT data (398600.4415)
    assert eme2k.is_geoid()
    assert eme2k.equatorial_radius() == 6378.1363

    e = Epoch.system_now()

    orbit = Orbit.from_keplerian_altitude(
        400,
        ecc=1e-4,
        inc_deg=30.5,
        raan_deg=35.0,
        aop_deg=65.0,
        ta_deg=590,
        epoch=e,
        frame=eme2k,
    )

    orbits = generate_orbits(
        orbit,
        [
            (StateParameter("SMA"), 0.05),
            (StateParameter.Eccentricity, 0.1),
            (StateParameter.Inclination, 0.1),
        ],
        1000,
        kind="prct",
    )

    # And propagate in parallel using a single duration
    proped_orbits = two_body(orbits, durations=[Unit.Day * 531.5])
    assert len(proped_orbits) == len(orbits)

    # And propagate in parallel using many epochs
    ts = TimeSeries(e, e + Unit.Day * 1000, step=Unit.Day * 1, inclusive=False)
    epochs = [e for e in ts]
    proped_orbits = two_body(orbits, new_epochs=epochs)
    # Allow up to two to fail
    assert len(proped_orbits) >= len(orbits) - 2

    timing = timeit(lambda: two_body(orbits, new_epochs=epochs), number=1)
    print(f"two body propagation of {len(orbits)} orbits in {timing} s")

def test_merge_traj():
    # Initialize logging
    FORMAT = "%(levelname)s %(name)s %(filename)s:%(lineno)d\t%(message)s"
    logging.basicConfig(format=FORMAT)
    logging.getLogger().setLevel(logging.INFO)

    # Base path
    root = Path(__file__).joinpath("../../../").resolve()

    config_path = root.joinpath("./data/tests/config/")

    sc1 = Spacecraft.load(str(config_path.joinpath("spacecraft.yaml")))

    dynamics = SpacecraftDynamics.load_named(
        str(config_path.joinpath("dynamics.yaml"))
    )["lofi"]

    # Check loading from the YAML read from Python
    with open(config_path.joinpath("dynamics.yaml")) as fh:
        data = yaml.safe_load(fh)

    loaded = SpacecraftDynamics.loads(data["lofi"])
    assert loaded == dynamics

    sc2, traj1 = propagate(sc1, dynamics, Unit.Day * 5)
    # And propagate again
    sc3, traj2 = propagate(sc2, dynamics, Unit.Day * 5)
    # Add the trajectories
    traj = traj1 + traj2

    assert traj.last().epoch == sc3.epoch, f"{traj.last()} != {sc3}"

    # Convert into another frame and try to add them too.
    # We only check the epoch this time.
    traj1_moon = traj1.to_frame("Moon J2000")
    traj2_moon = traj2.to_frame("Moon J2000")

    traj_moon = traj1_moon + traj2_moon

    assert traj_moon.last().epoch == sc3.epoch
    print(traj_moon)

if __name__ == "__main__":
    # test_propagate()
    test_merge_traj()


TYPES = {
    'double': float,
    'float': float,
    'int32': int,
    'int64': int,
    'uint32': int,
    'uint64': int,
    'sint32': int,
    'sint64': int,
    'fixed32': int,
    'fixed64': int,
    'sfixed32': int,
    'sfixed64': int,
    'bool': bool,
    'string': str
}

WIRE_TYPES = {
    'double': 1,
    'float': 5,
    'int32': 0,
    'int64': 0,
    'uint32': 0,
    'uint64': 0,
    'sint32': 0,
    'sint64': 0,
    'fixed32': 5,
    'fixed64': 1,
    'sfixed32': 5,
    'sfixed64': 1,
    'bool': 0,
    'string': 2
}

class Descriptor:
    def __init__(self, name=None):
        self.name = name

    def __set__(self, instance, value):
        instance.__dict__[self.name] = value

class Typed(Descriptor):
    ty = object

    def __set__(self, instance, value):
        if not isinstance(value, self.ty) and value is not None:
            raise TypeError(f'Expected {self.name} is {self.ty} but was {type(value)}')
        super().__set__(instance, value)

def get_type(ty):
    return type(TYPES[ty].__name__, (Typed,), {'ty': TYPES[ty]})()


import random
import time

def search_lineal(list, objective):
  match = False

  for element in list: # O(n)
    print(f"Iterable times {time.time()} ")
    if element == objective:
      match = True
      print(f"Iterable times {time.time()} END")
      break

  return match

def main():
  list_size = int(input("What is your size list? "))
  objective = int(input("What is your prefer number? "))
  my_list = [random.randint(0, list_size) + 1 for i in range(0, list_size)]

  searched = search_lineal(my_list, objective)
  print(f"The element {objective} {'is not' if not searched else 'is'} into the list")

if __name__ == "__main__":
  main()


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import random

import networkx as nx
import torch
from torch.utils.data import Dataset

from dataloader import DataLoaderSubstructContext
from loader import graph_data_obj_to_nx, nx_to_graph_data_obj

class NewBioDataset(Dataset):
    def __init__(self, l1, center):
        self.datasets = []
        self.center = center
        self.l1 = l1

    def __getitem__(self, item):
        var = self.datasets[item]
        return var

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

    def process(self, data, root_idx=None):
        num_atoms = data.x.size()[0]
        G = graph_data_obj_to_nx(data)

        if root_idx == None:
            if self.center == True:
                root_idx = data.center_node_idx.item()
            else:
                root_idx = random.sample(range(num_atoms), 1)[0]

        # in the PPI case, the subgraph is the entire PPI graph
        data.x_substruct = data.x
        data.edge_attr_substruct = data.edge_attr
        data.edge_index_substruct = data.edge_index
        data.center_substruct_idx = data.center_node_idx

        # Get context that is between l1 and the max diameter of the PPI graph
        l1_node_idxes = nx.single_source_shortest_path_length(G, root_idx,
                                                              self.l1).keys()
        # l2_node_idxes = nx.single_source_shortest_path_length(G, root_idx,
        #                                                       self.l2).keys()
        l2_node_idxes = range(num_atoms)
        context_node_idxes = set(l1_node_idxes).symmetric_difference(
            set(l2_node_idxes))
        if len(context_node_idxes) > 0:
            context_G = G.subgraph(context_node_idxes)
            context_G, context_node_map = reset_idxes(context_G)  # need to
            # reset node idx to 0 -> num_nodes - 1, other data obj does not
            # make sense
            context_data = nx_to_graph_data_obj(context_G, 0)  # use a dummy
            # center node idx
            data.x_context = context_data.x
            data.edge_attr_context = context_data.edge_attr
            data.edge_index_context = context_data.edge_index

        # Get indices of overlapping nodes between substruct and context,
        # WRT context ordering
        context_substruct_overlap_idxes = list(context_node_idxes)
        if len(context_substruct_overlap_idxes) > 0:
            context_substruct_overlap_idxes_reorder = [context_node_map[old_idx]
                                                       for
                                                       old_idx in
                                                       context_substruct_overlap_idxes]
            data.overlap_context_substruct_idx = \
                torch.tensor(context_substruct_overlap_idxes_reorder)

        return data

def reset_idxes(G):
    """
    Resets node indices such that they are numbered from 0 to num_nodes - 1
    :param G:
    :return: copy of G with relabelled node indices, mapping
    """
    mapping = {}
    for new_idx, old_idx in enumerate(G.nodes()):
        mapping[old_idx] = new_idx
    new_G = nx.relabel_nodes(G, mapping, copy=True)
    return new_G, mapping

datasets = NewBioDataset(l1=1, center=0)
loader = DataLoaderSubstructContext(datasets, batch_size=5, shuffle=False)
for batch in loader:
    print(batch)


import glob

list_files = glob.glob("C:\\Users\\nv.solomennikova\\Documents\\pythonProject\\p4ne\\Lab1.5\\config_files\\*.txt")
str  = " ip address "

list_result = []
for fl in list_files:
    with open (fl) as f:
        for s in f:

            if str in s:
                pos = s.find(str)
                res_s = s.replace(" ip address ", "")
                list_result.append(res_s.strip())

res = list(set(list_result))
print(res)
print('\n'.join(res))


lst = []
N = int(input())
A = list(map(int, input().split()))
sum_A = sum(A)
for j in A:
    if (sum_A-j) % 7 == 0:
        lst.append(j)
if len (lst)==0:
    print("-1")
else:
    print(A.index(min(lst)))


# coding: utf-8

import pytest
import numpy as np
from imageio import imread
from AxonDeepSeg.data_management.data_augmentation import *

class TestCore(object):
    def setup(self):
        # Remember that the stop value in "arrange" is not included
        x = np.arange(0, 16, dtype='uint8')
        y = np.arange(0, 16, dtype='uint8')
        xv, yv = np.meshgrid(x, y)
        self.testImage = xv+yv

        self.mask = np.ones((16, 16, 3), dtype=int)

    def teardown(self):
        pass

    # --------------data_augmentation.py tests-------------- #
    # **NOTE** Because most data augmentation functions chose the parameters
    # randomly within bounds set by the arguments, it's impossible to target
    # test cases for known shifts, rotations, etc. Thus, only broad tests are
    # defined (output dim = input dim, output patch different or same as input
    # patch)

    @pytest.mark.unit
    def test_shifting_returns_different_image(self):
        patch = [self.testImage, self.mask]
        augmentedPatch = shifting(patch, verbose=1)

        assert augmentedPatch[0].shape == patch[0].shape
        assert augmentedPatch[1].shape == patch[1].shape
        assert np.not_equal(augmentedPatch[0], patch[0]).any()

    @pytest.mark.unit
    def test_rescaling_factor_1_returns_same_image(self):
        patch = [self.testImage, self.mask]
        augmentedPatch = rescaling(patch, factor_max=1, verbose=1)

        assert augmentedPatch[0].shape == patch[0].shape
        assert augmentedPatch[1].shape == patch[1].shape
        assert np.equal(augmentedPatch[0], patch[0]).all()

    @pytest.mark.unit
    def test_large_max_rescaling_factor_returns_different_image(self):
        # Note: Since there
        patch = [self.testImage, self.mask]
        augmentedPatch = rescaling(patch, factor_max=100, verbose=1)

        assert augmentedPatch[0].shape == patch[0].shape
        assert augmentedPatch[1].shape == patch[1].shape

        assert np.not_equal(augmentedPatch[0], patch[0]).any()
        # Note: due to  therandomized rescaling factor choice,
        # there is an unavoidable possibility that this test fails
        # simply due to bad luck, so try running the test suite again.

    @pytest.mark.unit
    def test_random_rotation_returns_different_image(self):
        patch = [self.testImage, self.mask]
        augmentedPatch = random_rotation(patch, verbose=1)

        assert augmentedPatch[0].shape == patch[0].shape
        assert augmentedPatch[1].shape == patch[1].shape
        assert np.not_equal(augmentedPatch[0], patch[0]).any()

    @pytest.mark.unit
    def test_elastic_returns_different_image(self):
        patch = [self.testImage, self.mask]
        augmentedPatch = elastic(patch, verbose=1)

        assert augmentedPatch[0].shape == patch[0].shape
        assert augmentedPatch[1].shape == patch[1].shape
        assert np.not_equal(augmentedPatch[0], patch[0]).any()

    @pytest.mark.unit
    def test_flipping_returns_different_image(self):
        # Since the flipping only occurs randomly thus has a probability of
        # not getting flipped, this low-level test only covers the same image
        # size assertions.

        patch = [self.testImage, self.mask]
        augmentedPatch = flipping(patch, verbose=1)

        assert augmentedPatch[0].shape == patch[0].shape
        assert augmentedPatch[1].shape == patch[1].shape

    @pytest.mark.unit
    def test_gaussian_blur_returns_image_of_same_size(self):
        # Because the sigma blur size is also random, it's very difficult to
        # test for a known case (different/same image), so this low-level test
        # only covers the same image size assertions.

        patch = [self.testImage, self.mask]
        augmentedPatch = gaussian_blur(patch, verbose=1)

        assert augmentedPatch[0].shape == patch[0].shape
        assert augmentedPatch[1].shape == patch[1].shape


import os
import hashlib
import re
import traceback

from loguru import logger

def printe(e):
    print(e)
    logger.error(e)
    traceback.print_exc()

def is_decimal_or_comma(s):
    pattern = r'^\d*\.?\d*$|^\d*[,]?\d*$'
    return bool(re.match(pattern, s))

# ================对文件算MD5================

def md5(path, blksize=2 ** 20):

    # 判断目标是否文件,及是否存在
    if os.path.isfile(path) and os.path.exists(path):
        file_size = os.path.getsize(path)

        # if file_size <= 512MB
        if file_size <= 256 * 1024 * 1024:
            with open(path, 'rb') as f:
                cont = f.read()
            hash_object = hashlib.md5(cont)
            t_md5 = hash_object.hexdigest()
            return t_md5, file_size
        else:
            m = hashlib.md5()
            with open(path, 'rb') as f:
                while True:
                    buf = f.read(blksize)
                    if not buf:
                        break
                    m.update(buf)
            t_md5 = m.hexdigest()
            return t_md5, file_size
    else:
        return None


def isPalindrome(x):
    if x==x[::-1]:
        return True
def solution(s):
    answer=0
    for i in range(len(s)): #0~6
        for j in range(i+1,len(s)+1): #1~7
            if isPalindrome(s[i:j]):
                if answer < j - i:
                    answer = j - i
    return answer

def is_Palindrom(s):
    if s[:] == s[::-1]:
        return True
    else:
        return False

def solution(s):
    answer = 0
    for i in range(len(s)): #starting idx
        for j in range(i+1,len(s)+1): #ending idx
            if is_Palindrom(s[i:j]):
                if answer < j - i:
                    answer = j - i
    return answer