import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score,precision_score,classification_report,recall_score
from sklearn import metrics
import time as tt

import texttable
import matplotlib.pyplot as plt
import warnings

warnings.filterwarnings('ignore') #To ignore warnings

#reading data and defining column names
df = pd.read_csv('/home/potti/Desktop/Btp_Project/data_set.csv',
	names = ['age','sex','cp','trestbps','chol','fbs','restecg','thalach','exang','oldpeak','slope','ca','thal','num'])
df.head() #display's attribute names and 5 rows from dataframe

rows_before = len(df) #no.of rows in df before removing null values
print('No.of rows in data_set are : ', rows_before)

df = df.dropna(how='any',axis=0) #delete rows with null values

rows_after = len(df)#no.of rows in df after removing null values
print('No.of rows after removing null values in data_set are : ',rows_after)

#store feature matrix and response vector
X_features = df[['age','sex','cp','trestbps','chol','fbs','restecg','thalach','exang','oldpeak','slope','ca','thal']] # feature matrix
print(X_features.head())
Y_labels = df[['num']]   # response vector
#print(Y_labels.head())

# splitting X and Y into training and testing sets
X_train, X_test, Y_train, Y_test = train_test_split(X_features, Y_labels, test_size=0.10,random_state = 50)
X1_train, X1_test, Y1_train, Y1_test = train_test_split(X_features, Y_labels, test_size=0.20,random_state =50)
X2_train, X2_test, Y2_train, Y2_test = train_test_split(X_features, Y_labels, train_size=0.25,random_state = 50)

#print(Y_train,"\n",Y_test)

print("********************************************************************************")
print("                          Naive Bayes Classifier                                ")
print("********************************************************************************")
# fitting the model to the training set
gauss_clf = GaussianNB()
t0 = tt.time()
gauss_clf.fit(X_train, Y_train)
print("Time taken by the training data is(90%) :", round(tt.time()-t0, 8), "s")
d0 = round(tt.time()-t0,4) #time taken for building model(train)

gauss_clf1 = GaussianNB()
t1 = tt.time()
gauss_clf1.fit(X1_train, Y1_train)
print("Time taken by the training data is(80%) :", round(tt.time()-t1, 8), "s")
d1 = round(tt.time()-t1,4)

gauss_clf2 = GaussianNB()
t2 = tt.time()
gauss_clf2.fit(X2_train, Y2_train)
print("Time taken by the training data is(75%) :", round(tt.time()-t2, 8), "s")
d2 = round(tt.time()-t2,4)

#predictions
t3 = tt.time()
pred_gauss = gauss_clf.predict(X_test)
print("Time taken by the testing data is(10%) :", round(tt.time()-t3, 8), "s")
d3 = round(tt.time()-t3,4) #time taken for prediction

t4 = tt.time()
pred_gauss1 = gauss_clf1.predict(X1_test)
print("Time taken by the testing data is(20%) :", round(tt.time()-t4, 8), "s")
d4 = round(tt.time()-t4,4)

t5 = tt.time()
pred_gauss2 = gauss_clf2.predict(X2_test)
print("Time taken by the testing data is(25%) :", round(tt.time()-t5, 8), "s")
d5 = round(tt.time()-t5,4)
print("\n")

#confusion matrix
confusion_matrix = metrics.confusion_matrix(Y_test, pred_gauss)
print("Confusion matrix(testing data = 10%) :\n",confusion_matrix)
print("\n")

confusion_matrix1 = metrics.confusion_matrix(Y1_test, pred_gauss1)
print("Confusion matrix(testing data = 20%) :\n",confusion_matrix1)
print("\n")

confusion_matrix2 = metrics.confusion_matrix(Y2_test, pred_gauss2)
print("Confusion matrix(testing data = 25%) :\n",confusion_matrix2)
print("\n")

#calculate the accuracy
accuracy_gauss = accuracy_score(Y_test, pred_gauss)
accuracy_gauss1 = accuracy_score(Y1_test, pred_gauss1)
accuracy_gauss2 = accuracy_score(Y2_test, pred_gauss2)

#classification report
print("\nClassification_report(testing data = 10%) :\n" ,metrics.classification_report(Y_test, pred_gauss))
print("\nClassification_report(testing data = 20%) :\n" ,metrics.classification_report(Y1_test, pred_gauss1))
print("\nClassification_report(testing data = 25%) :\n" ,metrics.classification_report(Y2_test, pred_gauss2))

#calculate recall
recall_gauss = recall_score(Y_test, pred_gauss,average="weighted")
recall_gauss1 = recall_score(Y1_test, pred_gauss1,average="weighted")
recall_gauss2 = recall_score(Y2_test, pred_gauss2,average="weighted")

#calculate precision
prec_gauss = precision_score(Y_test, pred_gauss,average="weighted")
prec_gauss1 = precision_score(Y1_test, pred_gauss1,average="weighted")
prec_gauss2 = precision_score(Y2_test, pred_gauss2,average="weighted")
print("********************************************************************************\n")

print("********************************************************************************")
print("                          K Nearest Neighbors                                   ")
print("********************************************************************************")
#training
knn = KNeighborsClassifier(n_neighbors=60)
t6 = tt.time()
knn.fit(X_train, Y_train)
print("Time taken by the training data is(90%) :", round(tt.time()-t6, 8), "s")
d6 = round(tt.time()-t6,4)

knn1 = KNeighborsClassifier(n_neighbors=20)
t7 = tt.time()
knn1.fit(X1_train, Y1_train)
print("Time taken by the training data is(80%) :", round(tt.time()-t7, 8), "s")
d7 = round(tt.time()-t7,4)

knn2 = KNeighborsClassifier(n_neighbors=20)
t8 = tt.time()
knn2.fit(X2_train, Y2_train)
print("Time taken by the training data is(75%) :", round(tt.time()-t8, 8), "s")
d8 = round(tt.time()-t8,4)

#prediction
t9 = tt.time()
pred_knneigh = knn.predict(X_test)
print("Time taken by the testing data is(10%) :", round(tt.time()-t9, 8), "s")
d9 = round(tt.time()-t9,4)

t10 = tt.time()
pred_knneigh1 = knn1.predict(X1_test)
print("Time taken by the testing data is(20%) :", round(tt.time()-t10, 8), "s")
d10 = round(tt.time()-t10,4)

t11 = tt.time()
pred_knneigh2 = knn2.predict(X2_test)
print("Time taken by the testing data is(25%) :", round(tt.time()-t11, 8), "s")
d11 = round(tt.time()-t11,4)
print("\n")

#confusion matrix
confusion_matrix3 = metrics.confusion_matrix(Y_test, pred_knneigh)
print("Confusion matrix(testing data = 10%) :\n",confusion_matrix3)
print("\n")
confusion_matrix4 = metrics.confusion_matrix(Y1_test, pred_knneigh1)
print("Confusion matrix(testing data = 20%) :\n",confusion_matrix4)
print("\n")
confusion_matrix5 = metrics.confusion_matrix(Y2_test, pred_knneigh2)
print("Confusion matrix(testing data = 25%) :\n",confusion_matrix5)
print("\n")

#accuracy
score_knn = accuracy_score(Y_test, pred_knneigh)
score_knn1 = accuracy_score(Y1_test, pred_knneigh1)
score_knn2 = accuracy_score(Y2_test, pred_knneigh2)

#report
print("\nClassification_report (testing data = 10%):\n" ,metrics.classification_report(Y_test, pred_knneigh))
print("\nClassification_report(testing data = 20%):\n" ,metrics.classification_report(Y1_test, pred_knneigh1))
print("\nClassification_report(testing data = 25%):\n" ,metrics.classification_report(Y2_test, pred_knneigh2))

#recall
recall_knn = recall_score(Y_test, pred_knneigh,average="weighted")
recall_knn1 = recall_score(Y1_test, pred_knneigh1,average="weighted")
recall_knn2 = recall_score(Y2_test, pred_knneigh2,average="weighted")

#precision
prec_knn = precision_score(Y_test, pred_knneigh,average="weighted")
prec_knn1 = precision_score(Y1_test, pred_knneigh1,average="weighted")
prec_knn2 = precision_score(Y2_test, pred_knneigh2,average="weighted")
print("********************************************************************************\n")

print("********************************************************************************")
print("                          Decision Tree Classifier                              ")
print("********************************************************************************")
#testing
tree_clf = DecisionTreeClassifier(min_samples_split=50,random_state=0)
t12=tt.time()
tree_clf.fit(X_train, Y_train)
print("Time taken by the training data is(90%) :", round(tt.time()-t12, 8), "s")
d12 = round(tt.time()-t12, 4)

tree_clf1 = DecisionTreeClassifier(min_samples_split=50,random_state=0)
t13=tt.time()
tree_clf1.fit(X1_train, Y1_train)
print("Time taken by the training data is(80%) :", round(tt.time()-t13, 8), "s")
d13 = round(tt.time()-t13, 4)

tree_clf2 = DecisionTreeClassifier(min_samples_split=40,random_state=0)
t14=tt.time()
tree_clf2.fit(X2_train, Y2_train)
print("Time taken by the training data is(75%) :", round(tt.time()-t14, 8), "s")
d14 = round(tt.time()-t14, 4)

#prediction
t15 = tt.time()
pred_tree = tree_clf.predict(X_test)
print("Time taken by the testing data is(10%) :", round(tt.time()-t15, 8), "s")
d15 = round(tt.time()-t15, 4)

t16 = tt.time()
pred_tree1 = tree_clf1.predict(X1_test)
print("Time taken by the testing data is(20%) :", round(tt.time()-t16, 8), "s")
d16 = round(tt.time()-t16, 4)

t17 = tt.time()
pred_tree2 = tree_clf2.predict(X2_test)
print("Time taken by the testing data is(25%) :", round(tt.time()-t17, 8), "s")
d17 = round(tt.time()-t17, 4)
print("\n")

#matrix
confusion_matrix6 = metrics.confusion_matrix(Y_test, pred_tree)
print("Confusion matrix(testing data = 10%) :\n",confusion_matrix6)
print("\n")
confusion_matrix7 = metrics.confusion_matrix(Y1_test, pred_tree1)
print("Confusion matrix(testing data = 20%) :\n",confusion_matrix7)
print("\n")
confusion_matrix8 = metrics.confusion_matrix(Y2_test, pred_tree2)
print("Confusion matrix(testing data = 25%) :\n",confusion_matrix8)
print("\n")

#accuracy
score_tree = accuracy_score(Y_test, pred_tree)
score_tree1 = accuracy_score(Y1_test, pred_tree1)
score_tree2 = accuracy_score(Y2_test, pred_tree2)

#report
print("\nClassification_report (testing data = 10%):\n" ,metrics.classification_report(Y_test, pred_tree))
print("\nClassification_report(testing data = 20%):\n" ,metrics.classification_report(Y1_test, pred_tree1))
print("\nClassification_report(testing data = 25%):\n" ,metrics.classification_report(Y2_test, pred_tree2))

#recall
recall_tree = recall_score(Y_test, pred_tree,average="weighted")
recall_tree1 = recall_score(Y1_test, pred_tree1,average="weighted")
recall_tree2 = recall_score(Y2_test, pred_tree2,average="weighted")

#precision
prec_tree = precision_score(Y_test, pred_tree,average="weighted")
prec_tree1 = precision_score(Y1_test, pred_tree1,average="weighted")
prec_tree2 = precision_score(Y2_test, pred_tree2,average="weighted")

print("********************************************************************************\n")

print("********************************************************************************")
print("                          Random Forest Classifier                              ")
print("********************************************************************************")
#training
randfor_clf = RandomForestClassifier(min_samples_split=50,random_state=60)
t18=tt.time()
randfor_clf.fit(X_train, Y_train)
print("Time taken by the training data is(90%) :", round(tt.time()-t18, 8), "s")
d18 = round(tt.time()-t18, 5)

randfor_clf1 = RandomForestClassifier(min_samples_split=30,random_state=0)
t19=tt.time()
randfor_clf1.fit(X1_train, Y1_train)
print("Time taken by the training data is(80%) :", round(tt.time()-t19, 8), "s")
d19 = round(tt.time()-t19, 5)

randfor_clf2 = RandomForestClassifier(min_samples_split=10,random_state=0)
t20=tt.time()
randfor_clf2.fit(X2_train, Y2_train)
print("Time taken by the training data is(75%) :", round(tt.time()-t20, 8), "s")
d20 = round(tt.time()-t20, 5)

#prediction
t21=tt.time()
pred_randfor = randfor_clf.predict(X_test)
print("Time taken by the testing data is(10%) :", round(tt.time()-t21, 8), "s")
d21 = round(tt.time()-t21, 5)

t22=tt.time()
pred_randfor1 = randfor_clf1.predict(X1_test)
print("Time taken by the testing data is(20%) :", round(tt.time()-t22, 8), "s")
d22 = round(tt.time()-t22, 5)

t23=tt.time()
pred_randfor2 = randfor_clf2.predict(X2_test)
print("Time taken by the testing data is(25%) :", round(tt.time()-t23, 8), "s")
d23 = round(tt.time()-t23, 5)
print("\n")

#matrix
confusion_matrix9 = metrics.confusion_matrix(Y_test, pred_randfor)
print("Confusion matrix(testing data = 10%) :\n",confusion_matrix9)
print("\n")
confusion_matrix10 = metrics.confusion_matrix(Y1_test, pred_randfor1)
print("Confusion matrix(testing data = 20%) :\n",confusion_matrix10)
print("\n")
confusion_matrix11 = metrics.confusion_matrix(Y2_test, pred_randfor2)
print("Confusion matrix(testing data = 25%) :\n",confusion_matrix11)

#accuracy
score_randfor = accuracy_score(Y_test, pred_randfor)
score_randfor1 = accuracy_score(Y1_test, pred_randfor1)
score_randfor2 = accuracy_score(Y2_test, pred_randfor2)

#report
print("\nClassification_report (testing data = 10%):\n" ,metrics.classification_report(Y_test, pred_tree))
print("\nClassification_report(testing data = 20%):\n" ,metrics.classification_report(Y1_test, pred_tree1))
print("\nClassification_report(testing data = 25%):\n" ,metrics.classification_report(Y2_test, pred_tree2))

#recall
recall_randfor = recall_score(Y_test, pred_randfor,average="weighted")
recall_randfor1 = recall_score(Y1_test, pred_randfor1,average="weighted")
recall_randfor2 = recall_score(Y2_test, pred_randfor2,average="weighted")

#precision
prec_randfor = precision_score(Y_test, pred_randfor,average="weighted")
prec_randfor1 = precision_score(Y1_test, pred_randfor1,average="weighted")
prec_randfor2 = precision_score(Y2_test, pred_randfor2,average="weighted")
print("********************************************************************************\n")

#------------------------------90%--------------------------------------#
table1 = texttable.Texttable()
table1.add_rows([["Classifier", "Accuracy","Precision","Recall"],
	["Naive Bayes",accuracy_gauss,prec_gauss,recall_gauss],
	["KNN",score_knn,prec_knn,recall_knn], ["Decision Tree",score_tree,prec_tree,recall_tree],
	["Random Forest",score_randfor,prec_randfor,recall_randfor]])
print("When training data is 90%\n")
print(table1.draw())

#------------------------------80%--------------------------------------#
table2 = texttable.Texttable()
table2.add_rows([["Classifier", "Accuracy","Precision","Recall"],
	["Naive Bayes",accuracy_gauss1,prec_gauss1,recall_gauss1],
	["KNN",score_knn1,prec_knn1,recall_knn1], ["Decision Tree",score_tree1,prec_tree1,recall_tree1],
	["Random Forest",score_randfor1,prec_randfor1,recall_randfor1]])
print("When training data is 80%\n")
print(table2.draw())

#------------------------------75%--------------------------------------#
table3 = texttable.Texttable()
table3.add_rows([["Classifier", "Accuracy","Precision","Recall"],
	["Naive Bayes",accuracy_gauss2,prec_gauss2,recall_gauss2],
	["KNN",score_knn2,prec_knn2,recall_knn2], ["Decision Tree",score_tree2,prec_tree2,recall_tree2],
	["Random Forest",score_randfor2,prec_randfor2,recall_randfor2]])
print("When training data is 75%\n")
print(table3.draw())

#print(gauss_clf.predict([[57,1,3,150,168,0,0,174,0,1.6,1,0,3]]))
#print(gauss_clf.predict([[60,1,4,140,293,0,2,170,0,1.2,2,2,7]]))

########90%###########
n_groups = 4

a1 = accuracy_gauss;a2 = score_knn;a3=score_tree;a4 = score_randfor
b1=prec_gauss;b2 = prec_knn;b3 = prec_tree;b4=prec_randfor
c1 = recall_knn;c2 = recall_knn;c3=recall_tree;c4=recall_randfor

acc = (a1, a2, a3, a4)
prec = (b1, b2, b3, b4)
rec =(c1,c2,c3,c4)

# create plot
fig, ax = plt.subplots()
index = np.arange(n_groups)
bar_width = 0.2
opacity = 0.8

rects1 = plt.bar(index, acc, bar_width,
                 alpha=opacity,
                 color='b',
                 label='Accuracy')

rects2 = plt.bar(index + bar_width, prec, bar_width,
                 alpha=opacity,
                 color='g',
                 label='Precision')

rects3 = plt.bar(index + bar_width+bar_width, rec, bar_width,
                 alpha=opacity,
                 color='r',
                 label='Recall')

plt.xlabel('Method')
plt.ylabel('Scores')
plt.title('Scores by Methods for training 90%')
plt.xticks(index + bar_width, ('Naive Bayes', 'KNN', 'Decision Tree', 'Random Forest'))
plt.legend()

plt.tight_layout()
plt.show()

####### 2nd grph(80%) ##########

g1 = accuracy_gauss1;g2 = score_knn1;g3=score_tree1;g4 = score_randfor1
e1=prec_gauss1;e2 = prec_knn1;e3 = prec_tree1;e4=prec_randfor1
f1 = recall_knn1;f2 = recall_knn1;f3=recall_tree1;f4=recall_randfor1

acc1 = (g1, g2, g3, g4)
prec1 = (e1, e2, e3, e4)
rec1 = (f1,f2,f3,f4)

rects1 = plt.bar(index, acc1, bar_width,
                 alpha=opacity,
                 color='b',
                 label='Accuracy')

rects2 = plt.bar(index + bar_width, prec1, bar_width,
                 alpha=opacity,
                 color='g',
                 label='Precision')

rects3 = plt.bar(index + bar_width+bar_width, rec1, bar_width,
                 alpha=opacity,
                 color='r',
                 label='Recall')

plt.xlabel('Method')
plt.ylabel('Scores')
plt.title('Scores by Methods for training 80%')
plt.xticks(index + bar_width, ('Naive Bayes', 'KNN', 'Decision Tree', 'Random Forest'))
plt.legend()

plt.tight_layout()
plt.show()

############ 3rd grap(75%) ##############
h1 = accuracy_gauss2;h2 = score_knn2;h3=score_tree2;h4 = score_randfor2
i1=prec_gauss2;i2 = prec_knn2;i3 = prec_tree2;i4=prec_randfor2
j1 = recall_knn2;j2 = recall_knn2;j3=recall_tree2;j4=recall_randfor2

acc2 = (h1, h2, h3, h4)
prec2 = (i1, i2, i3, i4)
rec2 = (j1,j2,j3,j4)

rects1 = plt.bar(index, acc2, bar_width,
                 alpha=opacity,
                 color='b',
                 label='Accuracy')

rects2 = plt.bar(index + bar_width, prec2, bar_width,
                 alpha=opacity,
                 color='g',
                 label='Precision')

rects3 = plt.bar(index + bar_width+bar_width, rec2, bar_width,
                 alpha=opacity,
                 color='r',
                 label='Recall')

plt.xlabel('Method')
plt.ylabel('Scores')
plt.title('Scores by Methods for training 75%')
plt.xticks(index + bar_width, ('Naive Bayes', 'KNN', 'Decision Tree', 'Random Forest'))
plt.legend()
plt.tight_layout()
plt.show()


import pandas as pd
import numpy as np
import requests

ticker = input("Please name a stock ticker").upper()
# period_q = input("Annual or Quarter?").lower()

income_annual_address = f"https://quotes.wsj.com/{ticker}/financials/annual/income-statement"
balance_annual_address = f"https://quotes.wsj.com/{ticker}/financials/annual/balance-sheet"
cash_annual_address = f"https://quotes.wsj.com/{ticker}/financials/annual/cash-flow"

html = requests.get(income_annual_address).content
df_list = pd.read_html(html)
df = df_list[1]

##finding first column name to rename-
headers = [df.columns.values]
header = headers[0][0]

##finding by 'Thousands' or 'Millions'-
ending = header[48:58]
ending = ending.replace('.','')
ending = ending.replace(' ','')
ending = str(ending)

##formating and droping NAs
df = df.rename(columns={header:'Labels'})
header1 = headers[0][1]
header2 = headers[0][2]
header3 = headers[0][3]
header4 = headers[0][4]
header5 = headers[0][5]
df = df[['Labels',header1,header2,header3,header4,header5]]
income = df.dropna()
income = income.set_index('Labels')

##Deleting rows with %s
income['percentage'] = ''
income['percentage'][income[header1].str.contains('%')] = 'percentage'
income['percentage'][income[header2].str.contains('%')] = 'percentage'
income = income[income['percentage'] != 'percentage']
income = income.drop(['percentage'],axis=1)

income[header1][income[header1].str.contains('-')] = "0"
income[header2][income[header2].str.contains('-')] = "0"
income[header3][income[header3].str.contains('-')] = "0"
income[header4][income[header4].str.contains('-')] = "0"
income[header5][income[header5].str.contains('-')] = "0"

income[header1] = income[header1].str.replace('\(', '-')
income[header1] = income[header1].str.replace('\)', '')
income[header2] = income[header2].str.replace('\(', '-')
income[header2] = income[header2].str.replace('\)', '')
income[header3] = income[header3].str.replace('\(', '-')
income[header3] = income[header3].str.replace('\)', '')
income[header4] = income[header4].str.replace('\(', '-')
income[header4] = income[header4].str.replace('\)', '')
income[header5] = income[header5].str.replace('\(', '-')
income[header5] = income[header5].str.replace('\)', '')

income[header1] = income[header1].str.replace('\,', '')
income[header2] = income[header2].str.replace('\,', '')
income[header3] = income[header3].str.replace('\,', '')
income[header4] = income[header4].str.replace('\,', '')
income[header5] = income[header5].str.replace('\,', '')

print(income.head(50))


import random
import tkinter

def generator():
    val = random.randint(1,6)
    value_label['text'] = f'Dice roll value is: {val}'

window = tkinter.Tk()
window.title('Dice Simulator GUI')

window.columnconfigure(0, weight=1, minsize = 300)
window.rowconfigure([0,1,2], weight=1, minsize=60)

frame = tkinter.Frame(master=window, relief=tkinter.RAISED, borderwidth=3, bg='#003040', padx=30, pady=20)
frame.grid(pady=20)
name_label = tkinter.Label(master=frame, text='Dice Simulator', width=10, fg='#ff5040', padx=20, pady=10)
name_label.grid(row=0, column=0, pady=5)

value_label = tkinter.Label(master=frame, fg='#003040', relief=tkinter.RIDGE, borderwidth=2, padx=10, pady=10)
value_label.grid(row=1,column=0, pady=5)

btn = tkinter.Button(master=frame, text='Roll', padx=20, pady=10, command=generator)
btn.grid(row=2, column=0)

window.mainloop()


from src.shared.adapters.backen_platform_adapter import BackendPlatformAdapter
from src.shared.tools.logger import internal_logger

logger = internal_logger.get_logger()

class UserService:
    def __init__(self, adapter: BackendPlatformAdapter):
        self.adapter = adapter

    def get_user_minimal(self, user_id: int, sms_code: str):
        try:
            user = self.adapter.get_user_minimal(
                user_id=user_id,
                sms_code=sms_code
            )
            return user
        except Exception as e:
            logger.error(f'An error occurred: {str(e)}')
            raise Exception


import requests
import voice
import map

def get_weather_by_code(code):
    api_url = "https://api.openweathermap.org/data/2.5/weather?q=%s&appid=%s&units=%s"
    app_id = "보안"
    unit = "metric"	# 미터법을 이용하겠다 명시

    response = requests.get(api_url % (code, app_id, unit))
    if response.status_code == 200:
        return response.json()
    else:
        return None

def get_weather_by_coord(lat, lon):
    api_url = "https://api.openweathermap.org/data/2.5/weather?lat=%s&lon=%s&appid=%s&units=%s"
    app_id = "보안"
    unit = "metric"	# 미터법을 이용하겠다 명시

    response = requests.get(api_url % (lat, lon, app_id, unit))
    if response.status_code == 200:
        return response.json()
    else:
        return None

def create_weather_text(weather_info, address):
    response = "현재 %s의 온도는 %d 도이고, 습도는 %d 퍼센트입니다."
    if weather_info != None:
        return response % (address, weather_info['main']['temp'], weather_info['main']['humidity'])
    else:
    	return "날씨를 알 수 없습니다."

def main():
    voice.speech("어디의 날씨를 알려드릴까요?")
    address = voice.get_text_from_voice()

    coord = map.get_coord_by_address(address) #존재하는 도시인지 체크
    if coord == None: #지역이 없을경우
        voice.speech("%s을 찾지 못했습니다." % address)
    else:
        weather_info = get_weather_by_coord(coord[0], coord[1]) # 경도와 위도 정보 가져오기

        response_text = create_weather_text(weather_info, address) #weater_info 온도,습도 주소 말하기
        voice.speech(response_text)

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-

from Scripts.fastapp.utils.file_module.regex_dic_manager import regexDictionaryManager
from Scripts.fastapp.utils.file_module.keyword_extract import KeywordExtract
from Scripts.fastapp.utils.file_module.load_file_manager import loadFileManager
import pandas as pd

from Scripts.fastapp.common.consts import SAMPLE_FOLDER_PATH, KEYWORD_DICTIONARY_PATH

def preprocess_reg(textdata:str):
    """
    정규표현식
    return: Dataframe
    """
    origin_regex = regexDictionaryManager()

    # 사전에서 키워드 추출
    ke = KeywordExtract(KEYWORD_DICTIONARY_PATH)

    # 정규식 검출 수 Column을 Keyword List에 추가
    ke.keywords.insert(0, "reg_count")
    print(ke.keyword_dictionary)
    data = []

    # df = pd.DataFrame(data, columns= ke.keywords)

    # regex name, count, regex_ruslt_list
    _, c, _= origin_regex.get_all_regex(textdata)

    # 정규식 검출 수 Column 값 추가
    row_list=[]
    row_list.append(c)

    # Keyword List에 Regex Count가 추가되어있기 때문에 -1 해줘야 함
    for i in range(0, len(ke.keywords)-1):
        # fileData / search() or findall()
        if ke.keyword_dictionary[i].search(textdata):
            # print(ke.keyword_dictionary[i].search(file.data[page]))
            row_list.append(1)
        else:
            row_list.append(0)

    data.append(row_list)

    # print(data)
    c = ['reg_count','col1','col2','col3','col4','col5','col6','col7','col8','col9','col10']
    df = pd.DataFrame(data, columns= c)
    print(df)
    print("#")

    # print(df.to_json(orient='records'))
    return df

# text = "asfsdfajksfdkal;sdafkl;jsdfkl"
# df = preprocess_reg(text)
# print(type(df["col1"][0]))
# print(df["col1"][0])
# print(df["reg_count"].iloc[0])


import numpy as np
import mglearn
import matplotlib.pyplot as plt
from sklearn.decomposition import NMF, PCA

S = mglearn.datasets.make_signals()
plt.figure(figsize=(11, 2))
plt.plot(S, '-')
plt.xlabel("Time")
plt.ylabel("Signal")
plt.tight_layout()
plt.show()

A = np.random.RandomState(0).uniform(size=(100, 3))
X = np.dot(S, A.T)
print("Shape of measurements: {}".format(X.shape))

nmf = NMF(n_components=3, random_state=42)
S_ = nmf.fit_transform(X)
print("Recovered signal shape: {}".format(S_.shape))

pca = PCA(n_components=3)
H = pca.fit_transform(X)

models = [X, S, S_, H]
names = ['Observations (first three measurements)',
         'True sources',
         'NMF recovered signals',
         'PCA recovered signals']

fig, axes = plt.subplots(4, figsize=(12, 6), gridspec_kw={'hspace': .5},
                         subplot_kw={'xticks': (), 'yticks': ()})

for model, name, ax in zip(models, names, axes):
    ax.set_title(name)
    ax.plot(model[:, :3], '-')

# plt.savefig('fig.png', bbox_inches='tight')


import os, tempfile, time
# legacy indico imports

# indico api imports
from werkzeug.exceptions import NotFound
from indico.util import json
from indico.web.flask.util import send_file
from indico.web.handlers import RHHtdocs
from MaKaC.webinterface.rh.conferenceDisplay import RHConferenceBaseDisplay
from MaKaC.plugins.Collaboration.base import SpeakerStatusEnum
from MaKaC.conference import LocalFile
from indico.core.logger import Logger
from MaKaC.webinterface.rh.conferenceBase import RHConferenceBase
from MaKaC.webinterface.rh.conferenceModif import RHConferenceModifBase
from MaKaC.webinterface.rh.conferenceDisplay import RHConferenceBaseDisplay
from MaKaC.plugins.Collaboration.collaborationTools import CollaborationTools
from indico.core.index import Catalog
from MaKaC.plugins import PluginsHolder, Plugin
from MaKaC.errors import MaKaCError
from MaKaC.webinterface.pages import conferences
from MaKaC.i18n import _
from MaKaC.webinterface.rh.admins import RCAdmin, RHAdminBase
from MaKaC.user import Group, Avatar
from MaKaC.plugins.Collaboration.pages import WPAdminCollaboration, WPCollaborationDisplay, \
    WPConfModifCollaboration, WPConfModifCollaborationProtection, WPElectronicAgreementFormConference, \
    WPElectronicAgreementForm, WPElectronicAgreement
from indico.core.config import Config

class RCCollaborationAdmin(object):
    @staticmethod
    def hasRights(user):
        """ Returns True if the user is a Server Admin or a Collaboration admin
            user: an Avatar object
        """

        if user:
            # check if user is Server Admin, Collaboration Admin
            collaborationAdmins = PluginsHolder().getPluginType('Collaboration').getOption('collaborationAdmins').getValue()
            return RCAdmin.hasRights(None, user) or user in collaborationAdmins
        return False

class RCCollaborationPluginAdmin(object):
    @staticmethod
    def hasRights(user, plugins=[]):
        """ Returns True if the user is an admin of one of the plugins corresponding to pluginNames
            plugins: a list of Plugin objects (e.g. EVO, RecordingRequest) or strings with the plugin name ('EVO', 'RecordingRequest')
                     or the string 'any' (we will then check if the user is manager of any plugin),
        """
        if user:
            plist = CollaborationTools.getCollaborationPluginType().getPluginList() if plugins == 'any' else plugins

            if plist:
                for plugin in plist:
                    if not isinstance(plugin, Plugin):
                        plugin = CollaborationTools.getPlugin(plugin)
                    if user in plugin.getOption('admins').getValue():
                        return True
        return False

class RCVideoServicesManager(object):
    @staticmethod
    def hasRights(user, conf, plugins=[]):
        """ Returns True if the logged in user has rights to operate with bookings of at least one of a list of plugins, for an event.
            This is true if:
                -the user is a Video Services manager (can operate with all plugins)
                -the user is a plugin manager of one of the plugins
            Of course, it's also true if the user is event manager or server admin, but this class does not cover that case.

            request: an RH or Service object
            plugins: either a list of plugin names, or Plugin objects (we will then check if the user is manager of any of those plugins),
                     or the string 'any' (we will then check if the user is manager of any plugin),
                     or nothing (we will then check if the user is a Video Services manager).
        """

        if user:
            csbm = Catalog.getIdx("cs_bookingmanager_conference").get(conf.getId())
            if csbm.isVideoServicesManager(user):
                return True

            plugins = plugins
            if plugins == 'any':
                return csbm.isPluginManagerOfAnyPlugin(user)

            for plugin in plugins:
                if isinstance(plugin, Plugin):
                    plugin = plugin.getName()
                if csbm.isPluginManager(plugin, user):
                    return True
        return False

class RCVideoServicesUser(object):
    @staticmethod
    def hasRights(user, pluginName=""):
        """ Returns True if the logged in user is an authorised user to create bookings.
            This is true if:
                  - The user is in the list of authorised user and groups
            request: the user
            pluginName: the plugin to check
        """
        if user:
            plugin = CollaborationTools.getPlugin(pluginName)
            if plugin.hasOption("AuthorisedUsersGroups"):
                if plugin.getOption("AuthorisedUsersGroups").getValue():
                    for entity in plugin.getOption("AuthorisedUsersGroups").getValue():
                        if isinstance(entity, Group) and entity.containsUser(user) or \
                            isinstance(entity, Avatar) and entity == user:
                                return True
                    return False
                else:
                    return True
            else:
                return True
        return False

################################################### Event Modification Request Handlers ####################################
class RHConfModifCSBase(RHConferenceModifBase):
    def _checkParams(self, params):
        RHConferenceModifBase._checkParams(self, params)

        self._activeTabName = params.get("tab", None)

        # we build the list 'allowedTabs', a list of all tabs that the user can see
        allowedTabs = CollaborationTools.getTabs(self._conf, self._getUser())

        if self._target.canModify(self.getAW()) or RCVideoServicesManager.hasRights(self._getUser(), self._conf):
            allowedTabs.append('Managers')

        tabOrder = CollaborationTools.getCollaborationOptionValue('tabOrder')
        self._tabs = []

        for tabName in tabOrder:
            if tabName in allowedTabs:
                self._tabs.append(tabName)
                allowedTabs.remove(tabName)

        for tabName in allowedTabs:
            if tabName != 'Managers':
                self._tabs.append(tabName)

        if 'Managers' in allowedTabs:
            self._tabs.append('Managers')

class RHConfModifCSBookings(RHConfModifCSBase):
    _tohttps = True

    def _checkParams(self, params):
        RHConfModifCSBase._checkParams(self, params)

        if self._activeTabName and not self._activeTabName in self._tabs:
            self._cannotViewTab = True
        else:
            self._cannotViewTab = False
            if not self._activeTabName and self._tabs:
                self._activeTabName = self._tabs[0]

            self._tabPlugins = CollaborationTools.getPluginsByTab(self._activeTabName, self._conf, self._getUser())

    def _checkProtection(self):
        self._checkSessionUser()

        hasRights = (not self._cannotViewTab) and \
                    (RCCollaborationAdmin.hasRights(self._getUser()) or
                     RCCollaborationPluginAdmin.hasRights(self._getUser(), self._tabPlugins) or
                     RCVideoServicesManager.hasRights(self._getUser(), self._conf, self._tabPlugins) )

        if not hasRights:
            RHConferenceModifBase._checkProtection(self)

    def _process( self ):

        if self._conf.isClosed():
            p = conferences.WPConferenceModificationClosed( self, self._target )
            return p.display()
        else:
            if self._cannotViewTab:
                raise MaKaCError(_("That Video Services tab doesn't exist"), _("Video Services"))
            else:
                p = WPConfModifCollaboration( self, self._conf)
                return p.display()

class RHConfModifCSProtection(RHConfModifCSBase):

    def _checkParams(self, params):
        RHConfModifCSBase._checkParams(self, params)
        self._activeTabName = 'Managers'

    def _checkProtection(self):

        if not RCVideoServicesManager.hasRights(self._getUser(), self._conf):
            RHConferenceModifBase._checkProtection(self)

    def _process( self ):

        if self._conf.isClosed():
            p = conferences.WPConferenceModificationClosed( self, self._target )
            return p.display()
        else:
            p = WPConfModifCollaborationProtection( self, self._conf)
            return p.display()

class RHElectronicAgreement(RHConfModifCSBookings):
    def _checkParams(self, params):
        RHConfModifCSBookings._checkParams(self, params)
        self._activeTabName = 'Electronic Agreement'
        self.sortCriteria = params.get('sortBy', 'speaker')
        self.order = params.get('order', 'down')

    def _process(self):
        if self._cannotViewTab:
            raise MaKaCError(_("That Video Services tab doesn't exist"), _("Video Services"))
        else:
            p = WPElectronicAgreement(self, self._conf)
            return p.display(sortCriteria=self.sortCriteria, order=self.order)

class RHUploadElectronicAgreement(RHConferenceModifBase):
    def _checkParams(self, params):
        RHConferenceModifBase._checkParams(self, params)
        self.spkUniqueId = params.get('spkUniqueId', None)
        self.file = params.get('file', None)
        self.filePath = ''

        if self.file and self.spkUniqueId:
            try:
                self.filePath = self.saveFileToTemp(self.file.file)
                self._tempFilesToDelete.append(self.filePath)
            except AttributeError:
                raise MaKaCError("Problem when storing file")

    def getNewTempFile( self ):
        cfg = Config.getInstance()
        tempPath = cfg.getUploadedFilesTempDir()
        tempFileName = tempfile.mkstemp( suffix="IndicoFileUpload.tmp", dir = tempPath )[1]
        return tempFileName

    def saveFileToTemp( self, fd ):
        fileName = self.getNewTempFile()
        f = open( fileName, "wb" )
        f.write( fd.read() )
        f.close()
        return fileName

    def uploadProcess(self):
        # We save the file
        try:
            f = LocalFile()
            __, extension = os.path.splitext(self.file.filename)
            f.setFileName("EAForm-{0}{1}".format(self.spkUniqueId, extension))
            f.setFilePath(self.filePath)
            f.setId(self.spkUniqueId)
            # Update status for speaker wrapper
            manager = Catalog.getIdx("cs_bookingmanager_conference").get(self._conf.getId())
            spkWrapper = manager.getSpeakerWrapperByUniqueId(self.spkUniqueId)
            repo = self._conf._getRepository()

            f.setOwner(spkWrapper)
            f.archive(repo)
            # if no error, update the speaker wrapper...
            spkWrapper.setStatus(SpeakerStatusEnum.FROMFILE) #change status
            spkWrapper.setLocalFile(f) # set path to file
            spkWrapper.triggerNotification() # trigger notification task
        except:
            Logger.get('file_upload').exception("Error uploading file")
            raise MaKaCError("Unexpected error while uploading file")

    def _process(self):
        if self.spkUniqueId and self.file:
            self.uploadProcess()
            return json.dumps({'status': 'OK'}, textarea=True)

class RHElectronicAgreementGetFile(RHConfModifCSBookings):
    def _checkParams(self, params):
        RHConfModifCSBookings._checkParams( self, params )
        self.spkUniqueId = params.get("spkId","")

    def _process(self):
        manager = Catalog.getIdx("cs_bookingmanager_conference").get(self._conf.getId())
        sw = manager.getSpeakerWrapperByUniqueId(self.spkUniqueId)
        if not sw:
            raise MaKaCError("The speaker wrapper id does not match any existing speaker.")
        self.file = sw.getLocalFile()
        return send_file(self.file.getFileName(), self.file.getFilePath(), self.file.getFileType(),
                         self.file.getCreationDate())

class RHElectronicAgreementForm(RHConferenceBase):
    def _checkParams(self, params):
        RHConferenceBase._checkParams(self, params)
        self.authKey = params['authKey']

    def _process(self):
        return self.wpEvaluation()

    def wpEvaluation(self):
        wf = self.getWebFactory()
        if wf !=None: # Event: Meeting/Lecture
            return WPElectronicAgreementForm(self, self._conf, self.authKey).display()
        else:# Event: Conference
            return WPElectronicAgreementFormConference(self, self._conf, self.authKey).display()

################################################### Server Wide pages #########################################
class RHAdminCollaboration(RHAdminBase):

    def _checkParams( self, params ):
        RHAdminBase._checkParams( self, params )
        self._queryParams = {}
        self._queryParams["queryOnLoad"] = (params.get('queryOnLoad', None) == 'true')
        self._queryParams["page"] = params.get("page", 1)
        self._queryParams["resultsPerPage"] = params.get("resultsPerPage", 10)
        self._queryParams["indexName"] = params.get('indexName', None)
        self._queryParams["viewBy"] = params.get('viewBy', 'startDate')
        self._queryParams["orderBy"] = params.get('orderBy', '')
        self._queryParams["sinceDate"] = params.get('fromDate', '').strip()
        self._queryParams["toDate"] = params.get('toDate', '').strip()
        self._queryParams["fromDays"] = params.get('fromDays', '').strip()
        self._queryParams["toDays"] = params.get('toDays', '').strip()
        self._queryParams["fromTitle"] = params.get('fromTitle', '').strip()
        self._queryParams["toTitle"] = params.get('toTitle', '').strip()
        self._queryParams["onlyPending"] = (params.get('onlyPending', None) == 'true')
        self._queryParams["conferenceId"] = params.get('conferenceId', '').strip()
        self._queryParams["categoryId"] = params.get('categoryId', '').strip()

    def _checkProtection( self ):

        if not RCCollaborationAdmin.hasRights(self._getUser()) and \
           not RCCollaborationPluginAdmin.hasRights(self._getUser(), plugins="any"):
            RHAdminBase._checkProtection(self)

    def _process(self):
        p = WPAdminCollaboration( self , self._queryParams)
        return p.display()

################################################### Event Display Request Handlers ####################################
class RHCollaborationDisplay(RHConferenceBaseDisplay):

    def _process( self ):
        p = WPCollaborationDisplay( self, self._target )
        return p.display()


from flask import Blueprint, render_template, request
from flask_login import login_required
from app import app, cmdb

cmdb_client = cmdb.Client(app.config.get("CMDB_URL"), cacert=app.config.get("CMDB_CA_CERT"))

provider_bp = Blueprint('provider_bp', __name__,
                           template_folder='templates',
                           static_folder='static')

@provider_bp.route('/list', methods=["GET"])
@login_required
def list():

    providers = cmdb_client.get_providers(detailed=True)
    return render_template('providers.html', providers=providers)

@provider_bp.route('/services', methods=["GET"])
@login_required
def get_services():
    id = request.args.get('provider_id', None)
    services = cmdb_client.get_services(provider_id=id, detailed=True)
    filtered_services = [s for s in services if s['doc']['data']['service_type'] != 'eu.indigo-datacloud.mesos']
    return render_template('services.html', title="{} Services".format(id), services=filtered_services)


import torch
import torch.nn as nn

class Model(nn.Module):
    """
    A Simple Convolution Nerual NetWorker
    """

    def __init__(self, configs):
        super(Model, self).__init__()
        self.seq_len = configs.seq_len
        self.pred_len = configs.pred_len
        self.kernel_size = configs.kernel_size
        # self.layer_num = self._upate_layer_num(configs)

        self.module_1 = nn.Sequential(
            nn.Conv1d(1, 8, 3, 1, padding=1), nn.ReLU(),
            nn.Conv1d(8, 16, 3, 1, padding=1), nn.ReLU(),
            nn.Conv1d(16, 32, 3, 1, padding=1), nn.ReLU(),
            nn.Conv1d(32, 64, 3, 1, padding=1), nn.ReLU(),
            nn.Flatten()
        )

        self.predict = nn.Sequential(
            nn.Linear(64*self.seq_len, 1024), nn.ReLU(),
            nn.Linear(1024, self.pred_len)
        )

    def forward(self, input):
        # input: [Batch, Input length, Channel]
        x = input.permute(0, 2, 1)
        x = self.module_1(x)
        x = self.predict(x)
        return x.unsqueeze(2)

    def _upate_layer_num(self, configs):
        if configs.auto_capture:
            return int((configs.seq_len - 1)/(configs.kernel_size-1))+1      # 感受野和kernel_size关系：receptive field=(kernel_size -1)*n+1
        else:
            return configs.layer_num


from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework import permissions
from rest_framework.response import Response
from datetime import datetime
from django.db.models import Q, Min
# Create your views here.

from listings.models import BookingInfo, HotelRoom, Reservation
from .serializers import ListingSerializer

class Units(APIView):
    permission_classes = (permissions.AllowAny,)

    def get(self, request):
        # /api/v1/units/?max_price=100&check_in=2021-12-09&check_out=2021-12-12
        max_price = request.GET.get('max_price', None)
        check_in = request.GET.get('check_in', None)
        check_out = request.GET.get('check_out', None)

        queryset = BookingInfo.objects.all().order_by('price')
        if max_price:
            # price must be lower than max_price
            queryset = queryset.filter(price__lt=int(max_price))

        # if date range is given
        if check_in and check_out:
            check_in_date = datetime.strptime(check_in, '%Y-%m-%d').date()
            check_out_date = datetime.strptime(check_out, '%Y-%m-%d').date()

            # get reserved objects
            blocked = Reservation.objects.filter(
                Q(check_in_date__range=[check_in_date, check_out_date])
                | Q(check_out_date__range=[check_in_date, check_out_date])
                | Q(check_in_date__lte=check_in_date, check_out_date__gte=check_out_date)
            )

            # get available hotel room types objects
            blocked_hotel_room = blocked.filter(
                hotel_room__isnull=False
            ).values_list('hotel_room__id', flat=True)
            show_hotel_room_type = HotelRoom.objects.all().exclude(
                id__in=blocked_hotel_room
            ).values_list('hotel_room_type', flat=True)

            # get available apartment listing objects
            blocked_apartment_listing = blocked.filter(
                hotel_room__isnull=True
            ).values_list('listing', flat=True)
            show_apartment_listing = queryset.filter(hotel_room_type__isnull=True).exclude(
                Q(listing__in=blocked_apartment_listing)
            ).values_list('listing', flat=True)

            # filter available BookingInfo queryset
            queryset = queryset.filter(
                Q(hotel_room_type__in=show_hotel_room_type)
                | Q(listing__in=show_apartment_listing)
            )

        listing_completed = []
        items = []

        for bi in queryset:
            listing = None

            # if Hotel
            if bi.hotel_room_type:
                listing = bi.hotel_room_type.hotel

            # if Apartment
            elif bi.listing.listing_type == 'apartment':
                listing = bi.listing

            if not listing or listing.id in listing_completed:
                # no listing object found
                # or already added (hotels should display the price of the cheapest HotelRoomType with available HotelRoom.)
                # This the price of the first Hotel Room Type with a Room without blocked days in the range
                continue

            # build json
            json_data = ListingSerializer(listing).data
            json_data['price'] = str(int(bi.price))

            items.append(json_data)
            listing_completed.append(listing.id)

        return Response({'items': items})


from pb_max17043 import max17043
from utime import sleep_ms
from machine import I2C, Pin

# ESP32 - Pin assignement
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=10000)
# ESP8266 - Pin assignement
#i2c = I2C(scl=Pin(5), sda=Pin(4), freq=10000)
if i2c == None:
    raise ValueError('I2C create failed')

try:
    m = max17043(i2c)
    print(m)
    for i in range(5): # multiple read to stabilize ?
        vcell = round(m.getVCell(), 1)
        soc = round(m.getSoc(), 0)
        print(vcell, soc)
        sleep_ms(500)
    print('vcell %0.1f, soc %0.0f: '% (vcell, soc))

except Exception as e:
    print('exception reading Lipo gauge ' , str(e))


import string

def is_pangram(s):
    # input_string = input()
    letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
    for letter in letters:
        if letter not in s.lower():
            return False
        else:
            continue
    return True


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from collections import OrderedDict
from random import SystemRandom
from requests import codes
from requests import post
from requests.auth import HTTPBasicAuth
from string import ascii_letters
from string import digits

try:
    from urllib.parse import parse_qs
    from urllib.parse import urlparse
except ImportError:
    from urlparse import parse_qs
    from urlparse import urlparse

from lyft_rides.errors import ClientError
from lyft_rides.errors import LyftIllegalState
from lyft_rides.session import OAuth2Credential
from lyft_rides.session import Session
from lyft_rides.utils import auth
from lyft_rides.utils.request import build_url

class OAuth2(object):
    """The parent class for all OAuth 2.0 grant types."""

    def __init__(self, client_id, scopes):
        """Initialize OAuth 2.0 Class.
        Parameters
            client_id (str)
                Your app's Client ID.
            scopes (set)
                Set of permission scopes to request.
                (e.g. {'profile', 'rides.request'})
        """
        self.client_id = client_id
        self.scopes = scopes

    def _build_authorization_request_url(
        self,
        response_type,
        state=None
    ):
        """Form URL to request an auth code or access token.
        Parameters
            response_type (str)
                Only 'code' (Authorization Code Grant) supported at this time
            state (str)
                Optional CSRF State token to send to server.
        Returns
            (str)
                The fully constructed authorization request URL.
        Raises
            LyftIllegalState (ApiError)
                Raised if response_type parameter is invalid.
        """
        if response_type not in auth.VALID_RESPONSE_TYPES:
            message = '{} is not a valid response type.'
            raise LyftIllegalState(message.format(response_type))

        args = OrderedDict([
            ('scope', ' '.join(self.scopes)),
            ('state', state),
            ('response_type', response_type),
            ('client_id', self.client_id),
        ])

        return build_url(auth.SERVER_HOST, auth.AUTHORIZE_PATH, args)

    def _extract_query(self, redirect_url):
        """Extract query parameters from a url.
        Parameters
            redirect_url (str)
                The full URL that the Lyft server redirected to after
                the user authorized your app.
        Returns
            (dict)
                A dictionary of query parameters.
        """
        qs = urlparse(redirect_url)

        # redirect_urls return data after query identifier (?)
        qs = qs.query

        query_params = parse_qs(qs)
        query_params = {qp: query_params[qp][0] for qp in query_params}

        return query_params

class AuthorizationCodeGrant(OAuth2):
    """Class for Authorization Code Grant type.
    The authorization code grant type is used to obtain both access
    tokens and refresh tokens and is optimized for confidential clients.
    It involves a two-step authorization process. The first step is having
    the user authorize your app. The second involves getting an OAuth 2.0
    access token from Lyft.
    """

    def __init__(
        self,
        client_id,
        client_secret,
        scopes,
        is_sandbox_mode=True,
        state_token=None,
    ):
        """Initialize AuthorizationCodeGrant Class.
        Parameters
            client_id (str)
                Your app's Client ID.
            client_secret (str)
                Your app's Client Secret.
            scopes (set)
                Set of permission scopes to request.
                (e.g. {'profile', 'rides.request'}) Keep this list minimal so
                users feel safe granting your app access to their information.
            is_sandbox_mode (bool)
                Optional. Set to False if your code is in production.
            state_token (str)
                The CSRF State Token used to create an authorization.
        """
        super(AuthorizationCodeGrant, self).__init__(client_id, scopes)
        self.client_secret = "-".join([auth.SANDBOX_MODE_PREFIX, client_secret]) if is_sandbox_mode else client_secret
        if state_token is not None:
            self.state_token = state_token
        else:
            self.state_token = self._generate_state_token()

    def _generate_state_token(self, length=32):
        """Generate CSRF State Token.
        CSRF State Tokens are passed as a parameter in the authorization
        URL and are checked when receiving responses from the Lyft Auth
        server to prevent request forgery.
        """
        choices = ascii_letters + digits
        return ''.join(SystemRandom().choice(choices) for _ in range(length))

    def get_authorization_url(self):
        """Start the Authorization Code Grant process.
        This function starts the OAuth 2.0 authorization process and builds an
        authorization URL. You should redirect your user to this URL, where
        they can grant your application access to their Lyft account.
        Returns
            (str)
                The fully constructed authorization request URL.
                Tell the user to visit this URL and approve your app.
        """
        return self._build_authorization_request_url(
            response_type=auth.CODE_RESPONSE_TYPE,
            state=self.state_token,
        )

    def _verify_query(self, query_params):
        """Verify response from the Lyft Auth server.
        Parameters
            query_params (dict)
                Dictionary of query parameters attached to your redirect URL
                after user approved your app and was redirected.
        Returns
            authorization_code (str)
                Code received when user grants your app access. Use this code
                to request an access token.
        Raises
            LyftIllegalState (ApiError)
                Thrown if the redirect URL was missing parameters or if the
                given parameters were not valid.
        """
        error_message = None

        # Check CSRF State Token against returned state token from GET request
        received_state_token = query_params.get('state')
        if received_state_token is None:
            error_message = 'Bad Request. Missing state parameter.'
            raise LyftIllegalState(error_message)

        if self.state_token is None:
            error_message = 'Missing CSRF State Token in session.'
            raise LyftIllegalState(error_message)

        if self.state_token != received_state_token:
            error_message = 'CSRF Error. Expected {}, got {}'
            error_message = error_message.format(
                self.state_token,
                received_state_token,
            )
            raise LyftIllegalState(error_message)

        # Verify either 'code' or 'error' parameter exists
        error = query_params.get('error')
        authorization_code = query_params.get(auth.CODE_RESPONSE_TYPE)

        if error and authorization_code:
            error_message = (
                'Code and Error query params code and error '
                'can not both be set.'
            )
            raise LyftIllegalState(error_message)

        if error is None and authorization_code is None:
            error_message = 'Neither query parameter code or error is set.'
            raise LyftIllegalState(error_message)

        if error:
            raise LyftIllegalState(error)

        return authorization_code

    def get_session(self, redirect_url):
        """Complete the Authorization Code Grant process.
        The redirect URL received after the user has authorized
        your application contains an authorization code. Use this
        authorization code to request an access token.
        Parameters
            redirect_url (str)
                The full URL that the Lyft server redirected to after
                the user authorized your app.
        Returns
            (Session)
                A Session object with OAuth 2.0 credentials.
        """
        query_params = self._extract_query(redirect_url)
        authorization_code = self._verify_query(query_params)

        response = request_access_token(
            grant_type=auth.AUTHORIZATION_CODE_GRANT,
            client_id=self.client_id,
            client_secret=self.client_secret,
            code=authorization_code,
        )

        oauth2credential = OAuth2Credential.make_from_response(
            response=response,
            grant_type=auth.AUTHORIZATION_CODE_GRANT,
            client_id=self.client_id,
            client_secret=self.client_secret,
        )

        return Session(oauth2credential=oauth2credential)

class ClientCredentialGrant(OAuth2):
    """Class for Client Credential Grant type.
    This is the (2-legged) flow based authentication for public endpoints.
    The tokens granted here are valid for endpoints that don't require access
    to user data (eg ETA, cost, ride types).
    """

    def __init__(self, client_id, client_secret, scopes, is_sandbox_mode=True):
        """Initialize ClientCredential Class.
        Parameters
            client_id (str)
                Your app's Client ID.
            client_secret (str)
                Your app's Client Secret. This must be kept confidential.
            scopes (set)
                Set of permission scopes to request.
                (e.g. {'public', 'rides.read'})
            is_sandbox_mode (bool)
                Optional. Set to False if your code is in production.
        """
        super(ClientCredentialGrant, self).__init__(client_id, scopes)
        self.client_secret = "-".join([auth.SANDBOX_MODE_PREFIX, client_secret]) if is_sandbox_mode else client_secret

    def get_session(self):
        """Create Session to store credentials.
        Returns
            (Session)
                A Session object with OAuth 2.0 credentials.
        """
        response = request_access_token(
            grant_type=auth.CLIENT_CREDENTIAL_GRANT,
            client_id=self.client_id,
            client_secret=self.client_secret,
            scopes=self.scopes,
        )

        oauth2credential = OAuth2Credential.make_from_response(
            response=response,
            grant_type=auth.CLIENT_CREDENTIAL_GRANT,
            client_id=self.client_id,
            client_secret=self.client_secret,
        )

        return Session(oauth2credential=oauth2credential)

def request_access_token(
    grant_type,
    client_id=None,
    client_secret=None,
    scopes=None,
    code=None,
    refresh_token=None
):
    """Make an HTTP POST to request an access token.
    Parameters
        grant_type (str)
            Either 'client_credientials' (Client Credentials Grant)
            or 'authorization_code' (Authorization Code Grant).
        client_id (str)
            Your app's Client ID.
        client_secret (str)
            Your app's Client Secret.
        scopes (set)
            Set of permission scopes to request.
            (e.g. {'profile', 'history'})
        code (str)
            The authorization code to switch for an access token.
            Only used in Authorization Code Grant.
        refresh_token (str)
            Refresh token used to get a new access token.
            Only used for Authorization Code Grant.
    Returns
        (requests.Response)
            Successful HTTP response from a 'POST' to request
            an access token.
    Raises
        ClientError (APIError)
            Thrown if there was an HTTP error.
    """
    url = build_url(auth.SERVER_HOST, auth.ACCESS_TOKEN_PATH)

    if isinstance(scopes, set):
        scopes = ' '.join(scopes)

    args = {
        'grant_type': grant_type,
        'client_id': client_id,
        'client_secret': client_secret,
        'scope': scopes,
        'code': code,
        'refresh_token': refresh_token,
    }

    auth_header = HTTPBasicAuth(client_id, client_secret)

    response = post(url=url, auth=auth_header, data=args)

    if response.status_code == codes.ok:
        return response

    message = 'Failed to request access token: {}.'
    message = message.format(response.reason)
    raise ClientError(response, message)

def refresh_access_token(credential):
    """Use a refresh token to request a new access token.
    Not suported for access tokens obtained via Implicit Grant.
    Parameters
        credential (OAuth2Credential)
            An authorized user's OAuth 2.0 credentials.
    Returns
        (Session)
            A new Session object with refreshed OAuth 2.0 credentials.
    Raises
        LyftIllegalState (APIError)
            Raised if OAuth 2.0 grant type does not support
            refresh tokens.
    """
    if credential.grant_type == auth.AUTHORIZATION_CODE_GRANT:
        response = request_access_token(
            grant_type=auth.REFRESH_TOKEN,
            client_id=credential.client_id,
            client_secret=credential.client_secret,
            refresh_token=credential.refresh_token,
        )

        oauth2credential = OAuth2Credential.make_from_response(
            response=response,
            grant_type=credential.grant_type,
            client_id=credential.client_id,
            client_secret=credential.client_secret,
        )

        return Session(oauth2credential=oauth2credential)

    elif credential.grant_type == auth.CLIENT_CREDENTIAL_GRANT:
        response = request_access_token(
            grant_type=auth.CLIENT_CREDENTIAL_GRANT,
            client_id=credential.client_id,
            client_secret=credential.client_secret,
            scopes=credential.scopes,
        )

        oauth2credential = OAuth2Credential.make_from_response(
            response=response,
            grant_type=credential.grant_type,
            client_id=credential.client_id,
            client_secret=credential.client_secret,
        )

        return Session(oauth2credential=oauth2credential)

    message = '{} Grant Type does not support Refresh Tokens.'
    message = message.format(credential.grant_type)
    raise LyftIllegalState(message)

def revoke_access_token(credential):
    """Revoke an access token.
    All future requests with the access token will be invalid.
    Parameters
        credential (OAuth2Credential)
            An authorized user's OAuth 2.0 credentials.
    Raises
        ClientError (APIError)
            Thrown if there was an HTTP error.
    """
    url = build_url(auth.SERVER_HOST, auth.REVOKE_PATH)

    args = {
        'token': credential.access_token,
    }

    response = post(url=url, params=args)

    if response.status_code == codes.ok:
        return

    message = 'Failed to revoke access token: {}.'
    message = message.format(response.reason)
    raise ClientError(response, message)


import requests
from bs4 import BeautifulSoup

res = requests.get("https://www.codewithtomi.com/")

soup = BeautifulSoup(res.content, 'html.parser')

#print(soup.title.parent)

s = soup.find_all("p", class_="post-snippet")

for data in s:
    print(data.text)


from torch import nn
from torch.nn import functional as F
from neural.utils.hub import configure_model

__all__ = [
    'ContextNet',
    'contextnet12',
    'contextnet14',
    'contextnet18',
]

@configure_model({})
def contextnet12(in_channels=3, out_channels=19):
    return ContextNet(in_channels, out_channels,
                      scale_factor=2)

@configure_model({
    'cityscapes': {
        'in_channels': 3, 'out_channels': 19,
        'state_dict': 'http://files.deeplar.tk/neural/weights/contextnet/contextnet14-cityscapes-951f4835.pth',
    },
    'cityscapes-simple': {
        'in_channels': 3, 'out_channels': 19,
        'state_dict': 'http://files.deeplar.tk/neural/weights/contextnet/contextnet14-cityscapes-9cc70c56.pth',
    },
    'cityscapes-knowledge-destilation': {
        'in_channels': 3, 'out_channels': 19,
        'state_dict': 'http://files.deeplar.tk/neural/weights/contextnet/contextnet14-cityscapes-951f4835.pth',
    },
    'cityscapes-2x': {
        'in_channels': 3, 'out_channels': 19,
        'width_multiplier': 2,
        'state_dict': 'http://files.deeplar.tk/neural/weights/contextnet/contextnet14_2x-cityscapes-073a0b7d.pth',
    },
    'bdd100k-2x': {
        'in_channels': 3, 'out_channels': 19,
        'width_multiplier': 2,
        'state_dict': 'http://files.deeplar.tk/neural/weights/contextnet/contextnet14_2x-bdd100k-586cfea9.pth',
    }
})
def contextnet14(in_channels=3, out_channels=19, width_multiplier=1):
    return ContextNet(in_channels, out_channels,
                      scale_factor=4,
                      width_multiplier=width_multiplier)

@configure_model({})
def contextnet18(in_channels=3, out_channels=19):
    return ContextNet(in_channels, out_channels,
                      scale_factor=8)

class ContextNet(nn.Module):

    def __init__(self, in_channels, out_channels,
                 scale_factor=4,
                 width_multiplier=1):
        super(ContextNet, self).__init__()

        self.scale_factor = scale_factor

        def c(channels): return int(channels * width_multiplier)

        self.spatial = nn.Sequential(
            ConvBlock(in_channels, c(32), 3, padding=1, stride=2),
            DWConvBlock(c(32), c(32), kernel_size=3, padding=1, stride=2),
            ConvBlock(c(32), c(64), 1),
            DWConvBlock(c(64), c(64), kernel_size=3, padding=1, stride=2),
            ConvBlock(c(64), c(128), 1),
            DWConvBlock(c(128), c(128), kernel_size=3, padding=1, stride=1),
            ConvBlock(c(128), c(128), 1),
        )

        self.context = nn.Sequential(
            ConvBlock(in_channels, c(32), 3, padding=1, stride=2),
            BottleneckBlock(c(32), c(32), expansion=1),
            BottleneckBlock(c(32), c(32), expansion=6),
            LinearBottleneck(c(32), c(48), 3, stride=2),
            LinearBottleneck(c(48), c(64), 3, stride=2),
            LinearBottleneck(c(64), c(96), 2),
            LinearBottleneck(c(96), c(128), 2),
            ConvBlock(c(128), c(128), 3, padding=1),
        )

        self.feature_fusion = FeatureFusionModule((c(128), c(128)), c(128))

        self.classifier = Classifier(c(128), out_channels)

    def forward(self, input):
        spatial = self.spatial(input)

        context = F.interpolate(
            input, scale_factor=1 / self.scale_factor,
            mode='bilinear', align_corners=True)
        context = self.context(context)

        fusion = self.feature_fusion(context, spatial)

        classes = self.classifier(fusion)

        return F.interpolate(
            classes, size=input.shape[2:],
            mode='bilinear', align_corners=True)

def Classifier(in_channels, out_channels):
    return nn.Sequential(
        DWConvBlock(in_channels, in_channels, 3, padding=1),
        ConvBlock(in_channels, in_channels, 1),
        DWConvBlock(in_channels, in_channels, 3, padding=1),
        ConvBlock(in_channels, in_channels, 1),
        nn.Dropout(p=0.1),
        nn.Conv2d(in_channels, out_channels, 1),
    )

def LinearBottleneck(in_channels, out_channels, num_blocks,
                     expansion=6, stride=1):
    layers = [
        BottleneckBlock(
            in_channels, out_channels,
            stride=stride, expansion=expansion)]

    for _ in range(1, num_blocks):
        layers += [
            BottleneckBlock(
                out_channels, out_channels, expansion=expansion)
        ]
    return nn.Sequential(*layers)

class FeatureFusionModule(nn.Module):

    def __init__(self, in_channels, out_channels):
        super(FeatureFusionModule, self).__init__()

        lowres_channels, highres_channels = in_channels
        self.lowres = nn.Sequential(
            DWConvBlock(lowres_channels, lowres_channels,
                        kernel_size=3, padding=4, dilation=4),
            ConvBlock(lowres_channels, out_channels, 1, use_relu=False)
        )
        self.highres = ConvBlock(
            highres_channels, out_channels, 1, use_relu=False)

    def forward(self, lowres, highres):
        lowres = F.interpolate(
            lowres, size=highres.shape[2:],
            mode='bilinear', align_corners=True)
        lowres = self.lowres(lowres)

        highres = self.highres(highres)

        return F.relu(lowres + highres)

class BottleneckBlock(nn.Module):
    def __init__(self, in_channels, out_channels, stride=1, expansion=6):
        super(BottleneckBlock, self).__init__()

        expansion_channels = in_channels * expansion
        self.conv1 = ConvBlock(in_channels, expansion_channels, 1)
        self.conv2 = DWConvBlock(
            expansion_channels, expansion_channels, 3,
            padding=1, stride=stride)
        self.conv3 = ConvBlock(
            expansion_channels, out_channels, 1, use_relu=False)

    def forward(self, input):
        x = self.conv1(input)
        x = self.conv2(x)
        x = self.conv3(x)
        if x.shape == input.shape:
            x = input + x
        return F.relu(x)

def ConvBlock(in_channels, out_channels, kernel_size,
              padding=0, stride=1,
              groups=1, dilation=1,
              use_relu=True):
    layers = [
        nn.Conv2d(in_channels, out_channels, kernel_size,
                  padding=padding, stride=stride,
                  groups=groups, dilation=dilation,
                  bias=False),
        nn.BatchNorm2d(out_channels),
    ]
    if use_relu:
        layers += [nn.ReLU(inplace=True)]
    return nn.Sequential(*layers)

def DWConvBlock(in_channels, out_channels, kernel_size,
                padding=0, stride=1, dilation=1,
                use_relu=True):
    return ConvBlock(in_channels, out_channels, kernel_size,
                     padding=padding, stride=stride, dilation=dilation,
                     groups=in_channels, use_relu=use_relu)


import tensorflow as tf
import numpy as np

# Model used to choose next subgoal using Deep Q-Learning
# Given a pair (observation, subgoal) encoded as a one-hot observation matrix
# the model predicts the length of the plan that gets that subgoal and, then,
# completes the level

class DQNetwork:

	# Create CNN architecture
    def __init__(self, name="DQNetwork", writer_name="DQNetwork", create_writer = True,
                 l1_num_filt = 2, l1_window = [4,4], l1_strides = [2,2],
                 padding_type = "SAME",
                 max_pool_size = [2, 2],
                 max_pool_str = [1, 1],
                 fc_num_units = [16, 1], dropout_prob = 0.5,
                 l2_regularization=0.0,
                 learning_rate = 0.005):

        self.variable_scope = name

        with tf.variable_scope(self.variable_scope):

            # --- Constants, Variables and Placeholders ---

            # Batch of inputs (game states + goals, one-hot encoded)
            self.X = tf.placeholder(tf.float32, [None, 13, 26, 9], name="X") # type tf.float32 is needed for the rest of operations

            # Batch of outputs (correct predictions of number of actions)
            ## self.Y_corr = tf.placeholder(tf.float32, [None, 1], name="Y")

            # Q_target = R(s,a) + gamma * min Q(s', a') (s' next state after s, R(s,a) : plan length from state s to subgoal a)
            self.Q_target = tf.placeholder(tf.float32, [None, 1], name="Q_target")

            # Placeholder for batch normalization
            # During training (big batches) -> true, during test (small batches) -> false
            self.is_training = tf.placeholder(tf.bool, name="is_training")

            # Learning Rate
            self.alfa = tf.constant(learning_rate)

            # Dropout Probability (probability of deactivation)
            self.dropout_placeholder = tf.placeholder(tf.float32)
            self.dropout_prob = dropout_prob

            # --- Architecture ---

            """
            Batch Normalization of inputs
            """

            self.X_norm = tf.layers.batch_normalization(self.X, axis = 3, momentum=0.99, training=self.is_training)

            """
            First convnet:
            """

            # Padding = "VALID" -> no padding, "SAME" -> padding to keep the output dimension the same as the input one

            self.conv1 = tf.layers.conv2d(inputs = self.X_norm,
                                         filters = l1_num_filt,
                                         kernel_size = l1_window,
                                         strides = l1_strides,
                                         padding = padding_type,
                                         activation = tf.nn.relu,
                                         use_bias = True,
                                         kernel_initializer=tf.contrib.layers.xavier_initializer_conv2d(),
                                         name = "conv1")

            # Max pooling

            self.conv1 = tf.layers.max_pooling2d(inputs = self.conv1,
                                                pool_size = max_pool_size,
                                                strides = max_pool_str,
                                                padding = "VALID"
                                                )

            """
            Second convnet:
            """

            """
            self.conv2 = tf.layers.conv2d(inputs = self.conv1,
                                         filters = l2_num_filt,
                                         kernel_size = l2_window,
                                         strides = l2_strides,
                                         padding = padding_type,
                                         activation = tf.nn.relu,
                                         use_bias = True,
                                         kernel_initializer=tf.contrib.layers.xavier_initializer_conv2d(),
                                         name = "conv2")

            # Max pooling

            self.conv2 = tf.layers.max_pooling2d(inputs = self.conv2,
                                                pool_size = max_pool_size,
                                                strides = max_pool_str,
                                                padding = "VALID"
                                                )
            """

            # Flatten output of conv layers

            self.flatten = tf.contrib.layers.flatten(self.conv1)

            # Fully connected layer 1

            self.fc_1 = tf.layers.dense(inputs = self.flatten,
                                  units = fc_num_units[0],
                                  activation = tf.nn.relu,
                                  kernel_initializer=tf.contrib.layers.xavier_initializer(),
                                  kernel_regularizer=tf.contrib.layers.l2_regularizer(l2_regularization),
                                  name="fc_1")

            # Dropout 1

            self.fc_1 = tf.layers.dropout(self.fc_1, rate=self.dropout_placeholder, name="Dropout_1")

            # Fully connected layer 2

            self.fc_2 = tf.layers.dense(inputs = self.fc_1,
                                  units = fc_num_units[1],
                                  activation = tf.nn.relu,
                                  kernel_initializer=tf.contrib.layers.xavier_initializer(),
                                  kernel_regularizer=tf.contrib.layers.l2_regularizer(l2_regularization),
                                  name="fc_2")

            # Dropout

            self.fc_2 = tf.layers.dropout(self.fc_2, rate=self.dropout_placeholder, name="Dropout_2")

            # Output Layer -> outputs the Q_value for the current (game state, subgoal) pair

            self.Q_val = tf.layers.dense(inputs = self.fc_2,
                                          kernel_initializer=tf.contrib.layers.xavier_initializer(),
                                          units = 1,
                                          activation=None,
                                          name="Q_value")

            # Train

            # The loss is the difference between our predicted Q_values and the Q_targets
            # Sum(Qtarget - Q)^2
            self.loss = tf.reduce_mean(tf.square(self.Q_target - self.Q_val), name="loss")

            self.optimizer = tf.train.AdamOptimizer(learning_rate=self.alfa, name="optimizer")

            # Mean and Variance Shift Operations needed for Batch Normalization
            self.update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)

            # Execute mean and variance updates of batch norm each training step
            with tf.control_dependencies(self.update_ops):
                self.train_op = self.optimizer.minimize(self.loss, name="train_op")

            # --- Summaries ---

            if create_writer:
              self.train_loss_sum = tf.summary.scalar('train_loss', self.loss) # Training loss
              # self.test_loss_sum = tf.summary.scalar('test_loss', self.loss) # Validation loss

              self.writer = tf.summary.FileWriter("DQNetworkLogs/" + writer_name)
              self.writer.add_graph(tf.get_default_graph())

        # --- Initialization ---

        # Create Session

        # Run on GPU
        """
        # Needed for running on GPU
        gpu_options = tf.GPUOptions(allow_growth=True)
        self.sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
        """

        # Run on CPU (it's faster if model and batch_size is small)
        self.sess = tf.Session(config=tf.ConfigProto(device_count = {'GPU': 0}))

        # Initialize variables
        self.sess.run(tf.global_variables_initializer())

    # Closes the current tensorflow session and frees the resources.
    # Should be called at the end of the program
    def close_session(self):
        self.sess.close()

    # Predicts the associated y-value (plan length) for x (a (subgoal, game state) pair one-hot encoded)
    # Dropout is not activated
    def predict(self, x):
        # Reshape x so that it has the shape of a one-element batch and can be fed into the placeholder
        x_resh = np.reshape(x, (1, 13, 26, 9))
        data_dict = {self.X : x_resh, self.is_training : False, self.dropout_placeholder : 0.0}

        prediction = self.sess.run(self.Q_val, feed_dict=data_dict)

        return prediction

    # Execute num_it training steps using X, Y (Q_targets) as the current batches. They must have the same number of elements
    # Dropout is activated
    def train(self, X, Y, num_it = 1):
        data_dict = {self.X : X, self.Q_target : Y, self.is_training : True, self.dropout_placeholder : self.dropout_prob}

        for it in range(num_it):
            self.sess.run(self.train_op, feed_dict=data_dict)

    # Calculate Training Loss and store it as a log
    # Dropout is not activated
    def save_logs(self, X, Y, it):
        # Training Loss
        data_dict_train = {self.X : X, self.Q_target : Y, self.is_training : True, self.dropout_placeholder : 0.0}

        train_loss_log = self.sess.run(self.train_loss_sum, feed_dict=data_dict_train)
        self.writer.add_summary(train_loss_log, it)

    # Saves the model variables in the file given by 'path', so that it can be loaded next time
    def save_model(self, path = "./SavedModels/DQmodel.ckpt", num_it = None):
        saver = tf.train.Saver(
          tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, self.variable_scope)) # Only save the variables within this variable scope

        if num_it is None:
          saver.save(self.sess, path)
        else:
          saver.save(self.sess, path, global_step = num_it)

    # Loads a model previously saved with 'save_model'
    def load_model(self, path = "./SavedModels/DQmodel.ckpt", num_it = None):
        saver = tf.train.Saver(
          tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, self.variable_scope)) # Only load the variables within this variable scope

        if num_it is None:
          saver.restore(self.sess, path)
        else:
          saver.restore(self.sess, path + '-' + str(num_it))

    # Runs update_ops to update the current weights. This method is used to update the target network's weights
    # every tau steps
    def update_weights(self, update_ops):
        self.sess.run(update_ops)


from copy import deepcopy

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import scipy.spatial.distance as dist
from clasificacion_humedales.utils.utils_maia import directories, read_image, save_clustering
from scipy.cluster import hierarchy
import os

PATH_IN, PATH_OUT = directories()
PATH_OUT += 'agglomerative/'

def agglomerative(clustering, metric):
    history_of_labels = []
    initial_labels = np.unique(clustering['label'].tolist())
    initial_amount_clusters = len(initial_labels)
    amount_remaining_clusters = initial_amount_clusters
    while amount_remaining_clusters > 1:
        closest = _closest_clusters_and_their_distance(clustering, metric)
        labels_to_merge = closest['labels']
        distance_between_clusters = closest['distance']
        new_label = 2 * initial_amount_clusters - amount_remaining_clusters
        _merge(clustering, labels_to_merge, new_label)
        _record(history_of_labels, labels_to_merge, distance_between_clusters)
        amount_remaining_clusters = len(np.unique(clustering['label'].tolist()))
    return history_of_labels

def find_and_replace(new_image, old_label, new_label):
    new_image[new_image == old_label] = new_label

def rename(clustering, old_label, new_label):
    clustering.loc[clustering['label'] == old_label, 'label'] = new_label

def create_clustering_df(image, clustering):
    return pd.DataFrame({'NDVI': image.tolist(), 'label': clustering})

def _merge(clustering, labels_to_merge, new_label):
    rename(clustering, labels_to_merge[0], new_label)
    rename(clustering, labels_to_merge[1], new_label)

def _record(history_of_labels, labels_to_merge, distance_between_clusters):
    history_of_labels.append([labels_to_merge[0], labels_to_merge[1], distance_between_clusters])

def average_distance(cluster_1, cluster_2):
    distance_matrix = dist.cdist(cluster_1, cluster_2)
    return np.average(distance_matrix)

def maximum_distance(cluster_1, cluster_2):
    distance_matrix = dist.cdist(cluster_1, cluster_2)
    return np.amax(distance_matrix)

def ward_distance(cluster_1, cluster_2):
    center_cluster_1 = np.mean(cluster_1)
    center_cluster_2 = np.mean(cluster_2)
    c1 = len(cluster_1)
    c2 = len(cluster_2)
    return ((c1 * c2) / (c1 + c2)) * dist.euclidean(center_cluster_1, center_cluster_2)

def _closest_clusters_and_their_distance(clustering, metric):
    distance = np.infty
    labels_of_closest = ()
    labels = np.unique(clustering['label'].tolist())
    for label_1 in labels:
        for label_2 in labels:
            if label_1 != label_2:
                cluster_1 = _find_cluster_with(clustering, label_1)
                cluster_2 = _find_cluster_with(clustering, label_2)
                distance_between_clusters = metric(cluster_1, cluster_2)
                if distance_between_clusters < distance:
                    distance = distance_between_clusters
                    labels_of_closest = (label_1, label_2)
    return {'labels': labels_of_closest, 'distance': distance}

def _find_cluster_with(clustering, label):
    return clustering.query('label==%s' % label)['NDVI'].tolist()

def generate_images_history(original_clustering, label_history, template_path, folder_name):
    os.makedirs(PATH_OUT+folder_name)
    template_image = read_image(template_path)[0]
    dimensions = template_image.shape
    image = deepcopy(template_image)
    mask = image >= 0
    image[mask] = original_clustering
    image[np.invert(mask)] = -3000
    steps = len(label_history)
    amount_of_original_clusters = len(np.unique(original_clustering))-1
    for i in range(steps+1):
        new_label = amount_of_original_clusters + i
        if i!=0:
            find_and_replace(image, label_history[i-1][0], new_label)
            find_and_replace(image, label_history[i-1][1], new_label)
        save_clustering(template_path, f'{PATH_OUT}{folder_name}/iteracion_{i}.tif', np.reshape(image, dimensions))

def _fake_amount_of_points(history):
    matrix = deepcopy(history)
    iterations = len(matrix)
    points_for_each_cluster = [1] * (iterations + 1)
    for i in range(iterations):
        label_1 = matrix[i][0]
        label_2 = matrix[i][1]
        amount_points_new_cluster = points_for_each_cluster[label_1] + points_for_each_cluster[label_2]
        points_for_each_cluster.append(amount_points_new_cluster)
        matrix[i].append(amount_points_new_cluster)
    return matrix, points_for_each_cluster

def create_dendrogram(clustering, plot_title):
    history, _ = _fake_amount_of_points(clustering)
    plt.figure(figsize=(30, 30))
    dendro = hierarchy.dendrogram(history, leaf_font_size=35)
    plt.savefig(f'{PATH_OUT}/{plot_title}.jpg')
    plt.show()
    return dendro


def count_divisors(num):
  m = num // 2
  counter = 0
  for z in range(1, m + 1):
    if num % z == 0:
      counter += 1
  return counter

def is_palindrome(num):
  temp = num
  sum = 0
  while num > 0:
    d = num % 10
    sum = (sum * 10) + d
    num //= 10
  return temp == sum

def main():
  size = int(input())
  arr = [0] * size

  arr= list(map(int,input().split()))

  arr.sort()
  print("The maximum number :", arr[-1])
  print("The minimum number :", arr[0])

  count_prime = 0
  count_palindrome = 0
  for i in range(size):
    if is_palindrome(arr[i]):
      count_palindrome += 1
    m = arr[i] // 2
    flag = 0
    for z in range(2, m + 1):
      if arr[i] % z == 0:
        flag = 1
        break
    if flag == 0 and arr[i] != 1:
      count_prime += 1

  print("The number of prime numbers :", count_prime)
  print("The number of palindrome numbers :", count_palindrome)

  max_divisors = 0
  max_index = 0
  for i in range(size):
    c = count_divisors(arr[i])
    if i == 0:
      max_divisors = c
      max_index = i
    else:
      if c >= max_divisors:
        max_divisors = c
        max_index = i

  print("The number that has the maximum number of divisors :", arr[max_index])

if __name__ == "__main__":
  main()

Codeforces/Assiut University Training - sheets/Sheet 5 Function/O five in one.py


#!/bin/env python

import boto3

def get_paginated_results(product, action, key, region_name, credentials):
    return [y for sublist in [x[key] for x in boto3.client(product, region_name=region_name, **credentials).get_paginator(action).paginate()] for y in sublist]

def main(arn='', credentials={}):
    """Count the number of CloudFormation stacks in each region"""
    result = {}
    regions = boto3.session.Session().get_available_regions('cloudformation')
    for region in regions:
        stacks = get_paginated_results('cloudformation', 'list_stacks', 'StackSummaries', region, credentials)
        result[region] = len(stacks)
    return result

if __name__ == "__main__":
    print(main())


def solution(brown, yellow):
    answer = [0,0]
    board = brown + yellow

    for i in range(3,board):
        if board % i == 0:
            column = i
            row = board // column
            if (row-2) * (column-2) == yellow and row>=column:
                answer[0] = row
                answer[1] = column
    return answer


# -*- coding: utf-8 -*-
"""
Created on Tue Aug  2 14:23:37 2022

@author: Michele Nardini
"""

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Dataset:

# Country,  Age,   Salary,   Purchased
# France,   44,    72000,    No
# Spain,    27,    48000,    Yes
# Germany,  30,    54000,    No
# Spain,    38,    61000,    No
# Germany,  40,         ,    Yes
# France,   35,    58000,    Yes
# Spain,      ,    52000,    No
# France,   48,    79000,    Yes
# Germany,  50,    83000,    No
# France,   37,    67000,    Yes

dataset = pd.read_csv('Data.csv')

# Qui andremo a prendere i dati in un determinato intervallo.
# Con : andremo a prendere tutte le righe/colonne.

# x sarà la matrice delle caratteristiche del dataset
x = dataset.iloc[:, :-1].values

# y sarà la variabile dipendente, ovvero quella di cui vorremo
# prevedere il cambiamento.
y = dataset.iloc[:, -1].values

print(x)

print(y)

#Ora andremo a sostituire i valori mancanti
# tramite la classe SimpleImputer che richiederà come primo
# valore, i valori da sostituire e come secondo la strategia di
# sostituzione.
# Con 'mean' si sostituiscono i valori mancanti utilizzando
# la media lungo ciascuna colonna.
# Può essere utilizzato solo con dati numerici.
from sklearn.impute import SimpleImputer
imputer = SimpleImputer(missing_values=np.nan, strategy='mean')
imputer.fit(x[:, 1:3])
x[:, 1:3] = imputer.transform(x[:, 1:3])

print(x)

# Questo stimatore consente di trasformare separatamente diverse
# colonne o sottoinsiemi di colonne dell'input e le caratteristiche
# generate da ciascun trasformatore verranno concatenate per formare
# un unico spazio delle caratteristiche.
from sklearn.compose import ColumnTransformer

from sklearn.preprocessing import OneHotEncoder

#ColumnTransformer parametri:
    #lista trasformatori:
        # nome: serve a impostare il trasformatore
        # trasformatore: viene specificato il trasformatore
        # colonne: indicherà l'indice della colonna che subirà la trasformazione
    # reminder: Specificando remainder='passthrough', tutte le colonne
    # rimanenti che non sono state specificate in transformersverranno
    # automaticamente passate.
ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder(), [0])], remainder='passthrough')
x = np.array(ct.fit_transform(x))

print(x)

# Codifica le etichette di destinazione con un valore compreso tra
#  0 e n_classes-1.
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
y = le.fit_transform(y)

print(y)

# Splitting the dataset into Training set and Test set

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 = 1)

print(X_train)
print(X_test)
print(y_train)
print(y_test)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train[:, 3:] = sc.fit_transform(X_train[:, 3:])
X_test[:, 3:] = sc.transform(X_test[:, 3:])

print(X_train)
print(X_test)

# A seguito di queste operazioni avremo le caratteristiche del
# dataset con i valori scalati

Part 1 - Data Preprocessing/Section 2 -------------------- Part 1 - Data Preprocessing --------------------/Python/data_preprocessing_tools.py


import time
import os
from variables import *

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
# Set the default download location on chrome

import chromedriver_autoinstaller

# Check if the current version of chromedriver exists
# and if it doesn't exist, download it automatically,
# then add chromedriver to path
chromedriver_autoinstaller.install()

# Initialize the translations directory
dir = os.path.join(path, trans_dir)
if not os.path.exists(dir):
    os.mkdir(dir)

# Set the default path to download the texts in to
# make sure it leads to the same directory as the program
chrome_options = webdriver.ChromeOptions()
preferences = {"download.default_directory": path+trans_dir,
                "directory_upgrade": True,
                "safebrowsing.enabled": True }
chrome_options.add_experimental_option("prefs", preferences)
driver = webdriver.Chrome(options=chrome_options)
driver.delete_all_cookies()

# Login to law site
driver.get('http://www.japaneselawtranslation.go.jp/law/?re=2')
# Click on the second menu
search_1 = driver.find_element_by_xpath("//*[contains(text(), '投資')]").click()
time.sleep(1)

# Open the list of downloaded documents as a string\
'''
# Use this if you already have a list_of_documents.txt file

with open('list_of_documents.txt', 'r') as f1:
    docs = []
    for doc in f1.readlines():
        docs.append(doc[:-1])
'''

downloaded = []
# Chooses the first page of results by default.
# The indices for the pages will be [-10 to -3]
#driver.find_elements_by_tag_name("a")[-6].click() # Pick a page among the pages of search results.
while True:
    # Get the second <ul> element
    document_list = driver.find_elements_by_tag_name('ul')[1]
    # List all the documents in the list
    items = document_list.find_elements_by_tag_name("li")

    # Get the window with the search results as the base window
    base_window = driver.current_window_handle
    for item in items:
        # Check if the document has the default format, skip if it doesn't
        if len(item.find_elements_by_tag_name('img')) > 0:
            continue
        # Check if the document has already been downloaded
        '''
        # Use this if you already have a list_of_documents.txt file

        in_list = False
        title = str(item.find_elements_by_tag_name('a')[0].get_attribute('text'))[28:-10]
        for d in docs:
            print(d)
            title = str(item.find_elements_by_tag_name('a')[0].get_attribute('text'))[28:28+len(d)-1]
            print(title)
            if d[:-1] in title:
                in_list = True
                break
        if in_list == True:
            continue
        '''
        # Click on the link associated with the document
        item.find_element_by_tag_name("a").click()
        for w in driver.window_handles:
        #switch focus to child window
            if (w!=base_window):
                driver.switch_to.window(w)

        divs = driver.find_elements_by_tag_name('div')
        # Go to the footer div to access the downloads
        footer = driver.find_elements_by_class_name('footer')[0]
        # Dig deeper until the download button has been reached
        driver.switch_to.frame("footerFrame")
        lf = driver.find_element_by_id('lawsFooter')
        dl = lf.find_element_by_id('dateDl')
        form = dl.find_element_by_name('law_dl')
        # Download the files for the english and japanese translations
        select_object = Select(form.find_element_by_name('ff'))
        select_object.select_by_value('03')
        download_button = driver.find_elements_by_tag_name("input")[1].click()
        # Switch to the alert button that pops up and accept it
        obj = driver.switch_to.alert
        obj.accept()
        time.sleep(1)
        # Do the same for english translations
        select_object.select_by_value('08')
        download_button = driver.find_elements_by_tag_name("input")[1].click()
        obj = driver.switch_to.alert
        obj.accept()
        time.sleep(1)
        driver.close()
        driver.switch_to.window(base_window)
    # Move to the next page of search results
    driver.find_elements_by_tag_name("a")[-2].click()


import bpy
import numpy as np
import mathutils

scn = bpy.context.scene
base_intensity = list(scn.world.color)
view_layer = bpy.context.view_layer
cam_mat = scn.camera.matrix_world
cam_loc, _, _ = cam_mat.decompose()
lamps = [ob for ob in scn.objects if ob.type=='LIGHT']
cam = scn.camera.data
render = scn.render
horizontal = cam.sensor_width/2
vertical = horizontal/render.resolution_x*render.resolution_y

width = render.resolution_x
height = render.resolution_y
rays = np.zeros((height, width, 3), dtype=float)

ray_width = np.linspace(-horizontal, horizontal, num=width, dtype=float).reshape(1, -1)
ray_width = np.repeat(ray_width, height, axis = 0)
ray_height = np.linspace(-vertical, vertical, num=height, dtype=float).reshape(-1, 1)
ray_height = np.repeat(ray_height, width, axis = 1)
ray_depth = np.zeros((height, width), dtype=float) - cam.lens

rays = np.stack([ray_width, ray_height, ray_depth], axis = 2)

pixels = np.zeros((height, width, 4), dtype=float)

for y in range(height):
    for x in range(width):
        ray = cam_mat @ mathutils.Vector(rays[y, x]) - cam_loc
        result, loc, nor, ind, ob, mat = scn.ray_cast(view_layer, cam_loc, ray)

        if (result):
            intensity = base_intensity[:]

            for lamp in lamps:
                dir = lamp.location - loc
                dirn = dir.normalized()

                start = loc + dirn * 1e-4
                hit,_,_,_,_,_ = scn.ray_cast(view_layer, start, dirn)
                if not hit:
                    multiplier = max(0, min(1, 1 - dir.length / lamp.data.distance)) * lamp.data.energy * max(0, dirn.dot(nor))
                    intensity[0] += multiplier * lamp.data.color[0]
                    intensity[1] += multiplier * lamp.data.color[1]
                    intensity[2] += multiplier * lamp.data.color[2]

            pixels[y, x] = intensity[0], intensity[1], intensity[2], 255

img = bpy.data.images.get("name")
if (    (not img) or
        (img.size[0] != width or img.size[1] != height)):
    img = bpy.data.images.new("name", width, height)
img.pixels = pixels.reshape(-1)


n = int(input())
ld = [0 for _ in range(2*n)]
rd = [0 for _ in range(2*n)]
cl = [0 for _ in range(2*n)]

def find_right_solution(board, column):
    if column >= n:
        return True

    for i in range(n):
        if (ld[i - column + n - 1] != 1 and rd[i + column] != 1) and cl[i] != 1:
            board[i][column] = 1
            ld[i - column + n - 1] = rd[i + column] = cl[i] = 1

            if find_right_solution(board, column + 1):
                return True

            board[i][column] = 0
            ld[i - column + n - 1] = rd[i + column] = cl[i] = 0

    return False

board = [[0 for _ in range(n)] for _ in range(n)]
result = find_right_solution(board, 0)

if result:
    for i in board:
        print(*i)
else:
    print(result)


from django.conf import settings

def login_url(request):

    '''
    adds a variable `paytmoauth_login_url`
    in the context which is the login url for Paytm Oauth
    '''

    url = settings.PAYTMOAUTH_PROVIDER_URL + settings.PAYTMOAUTH_AUTHORIZATION_ENDPOINT
    url += '?response_type=code&client_id={}&scope=paytm&redirect_uri={}&theme-web'.format(
        settings.PAYTMOAUTH_CLIENT_ID, settings.PAYTMOAUTH_REDIRECT_URL)

    return {
            'paytmoauth_login_url': url
        }


def hazard_analysis(df):
    """Get the total number of hazardous asteroids and non-hazardous asteroids in the given dataframe."""

    # Get the number of hazardous asteroids
    hazardous = int(df[df['is_potentially_hazardous_asteroid'] == True].shape[0])

    # Get the number of non-hazardous asteroids
    non_hazardous = int(df[df['is_potentially_hazardous_asteroid'] == False].shape[0])

    return {
        'hazardous': hazardous,
        'non_hazardous': non_hazardous,
        'ratio': round(hazardous / non_hazardous, 2)
    }


def mergeTwoSortedArrays(array1Size, array1, array2Size, array2):
    mergedArraySize = 0
    mergedArray = []
    pointer1 = 0
    pointer2 = 0
    for pointer in range(array1Size+array2Size):
        if pointer1 == array1Size:
            mergedArray.extend(array2[pointer2:])
            mergedArraySize += (array2Size-pointer2-1)
            break
        elif pointer2 == array2Size:
            mergedArray.extend(array1[pointer1:])
            mergedArraySize += (array1Size -pointer1-1)
            mergedArraySize += 1
            break
        if array1[pointer1] <= array2[pointer2]:
            mergedArray.append(array1[pointer1])
            pointer1+=1
            mergedArraySize+=1
        else:
            mergedArray.append(array2[pointer2])
            pointer2 += 1
            mergedArraySize += 1
    print(len(mergedArray), " ".join(map(str, mergedArray)))

array_A = list(map(int, input().strip().split()))  # takes integers input, puts them in list
len_A = array_A.pop(0)

array_B = list(map(int, input().strip().split()))  # takes integers input, puts them in list
len_B = array_B.pop(0)

mergeTwoSortedArrays(len_A, array_A, len_B, array_B)