import asyncio, pickle, discord,spider
with open('config.pkl',mode='rb') as file: config=pickle.load(file)

class MyClient(discord.Client):
    def __init__(self, *args, **kwargs):
        super().__init__(*args,**kwargs)

    async def backgrount_task(self):
        await self.wait_until_ready()
        channel=self.get_channel(config['channel_id'])
        sleep_time=60*60
        fail_count=0
        while not self.is_closed():
            try: news_list=await spider.get_tnfsh_news()
            except Exception:
                print('cannot connect to source')
                if fail_count >= 5:
                    print('retry in 3600s')
                    sleep_time=60*60
                else:
                    print('retry in 5s')
                    fail_count+=1
                    sleep_time=5
            else:
                fail_count=0
                if len(news_list)==0: print('no news detected')
                for item in news_list:
                    message=spider.news_to_str(item)
                    print(message)
                    self.loop.create_task(channel.send(message))
                    await asyncio.sleep(.5)
                sleep_time=60*60

            print('sleep')
            await asyncio.sleep(sleep_time)

    async def setup_hook(self) -> None:
        self.loop.create_task(self.backgrount_task())

client=MyClient(intents=discord.Intents.default())
client.run(config['token'])


import random

hangMan0 = '''
 ___
|   o
|  /|\

Deid!

'''
hangMan1 = '''
 ___
|   o
|   |

1 Guess Left!

'''
hangMan2 = '''
 ___
|   o
|

2 Guesses Left!

'''
hangMan3 = '''
 ___
|
|

3 Guesses Left!

'''
hangMan4 = '''

|
|

4 Guesses Left!

'''
hangMan5 = '''

|

5 Guesses left!

'''

selectWord = ['dog','cat','egg']

selection = random.choice(selectWord)

print(selection)

letters = list(selection)

letter1 = letters[0]
letter2 = letters[1]
letter3 = letters[2]

g1 = '_'
g2 = '_'
g3 = '_'

guesses = g1 + g2 + g3

hangMan = 6

def turn():

  global letters
  global letter1
  global letter2
  global letter3
  global g1
  global g2
  global g3
  global guesses

  guesses = g1 + letter2 + g3

  print(guesses)

while len(letters) > 0:
  guess = input("Guess a letter?")
  if guess == letter1:
    print('yes')
    letters.remove(guess)
    turn()
  elif guess == letter2:
    print('yes')
    letters.remove(guess)
    turn()
  elif guess == letter3:
    print('yes')
    letters.remove(guess)
    turn()
  else:
    hangMan -= 1
    print('Wrong')
    if hangMan == 5:
      print(hangMan5)
    if hangMan == 4:
      print(hangMan4)
    if hangMan == 3:
      print(hangMan3)
    if hangMan == 2:
      print(hangMan2)
    if hangMan == 1:
      print(hangMan1)
    if hangMan == 0:
      print(hangMan0)
      print('You Lose!')
      break

if len(letters) < 1:
  print('You win!')

print('end')


import glob
import os
import sys
from pathlib import Path
import contextlib
import concurrent.futures

# if blender rendering is interupted, we may not have all data for each image. Delete those which aren't complete...
from PIL import Image

def valid_syn_pairs(base, dataset_root):

    global dirs
    files = []

    for d, e in dirs:
        files.append(os.path.join(dataset_root, d, f"{base}.{e}"))

    print (base)
    good = lambda f: os.path.exists(f) and os.path.getsize(f) > 0

    bad = False
    try:
        for d, e in dirs:
            file = os.path.join(dataset_root, d, f"{base}.{e}")
            if e in ["png", "jpg"]:
                img = Image.open(file)
                img.verify()
                if img is None:
                    bad = True
                    print(f"failed to verify {dataset_root}!")

            if not os.path.exists(file) and os.path.getsize(file):
                bad = True

    except:
        bad = True

    if not bad:
        return 0 # good
    else:
        for d, e in dirs:
            file = os.path.join(dataset_root, d, f"{base}.{e}")
            if not good(file):
                print(f"missing {d} -> {base}.{e}")

            if len (sys.argv) > 2:
                print("removing " + str ( file ) )
                with contextlib.suppress(FileNotFoundError):
                    os.remove(file)
            else:
                print ( "if this wasn't a dry run, I'd be removing " + file )

        return 1 # bad

def find_roots(files):
    out = {}
    for f in files:
        out[os.path.splitext(Path(f).name)[0]] = True

    return list ( out.keys() )

dirs = [ ["rgb","png"], ["labels","png"], ["labels_8bit", "png"], ["exposed", "png"], ["attribs", "txt"] ]

_pool = concurrent.futures.ThreadPoolExecutor()

roots = []

for d, e in dirs:
    roots.extend(glob.glob(os.path.join( sys.argv[1], d, "*."+e )))

roots = find_roots(roots)

processes = []
count = 0
for root in roots:

    processes.append(_pool.submit ( valid_syn_pairs, root, sys.argv[1] ))

    for r in concurrent.futures.as_completed(processes):
        count += r.result()

if len (sys.argv) > 2:
    print (f"have deleted {count}")
else:
    print(f"if this wasn't a dry run, I would have deleted {count}")


import numpy as np
import torch
import torch.nn.functional as F
from timeit import default_timer

from utilities3 import *
from Adam import *
from hjb_fourier_3d import FNO3d

################################################################
# configs
################################################################
ntrain = 100
ntest = 10

modes = 8
width = 20

batch_size = 10
batch_size2 = batch_size

epochs = 500
learning_rate = 0.001
scheduler_step = 100
scheduler_gamma = 0.5

R = 6
grid_res = 2**R
sub = 2**2
S = grid_res // sub

# Dubins car value function data
TRAIN_PATH = f'data/N110_R{R}_corner_fill.mat'  # filled goal set
# TRAIN_PATH = f'data/N1100_R{R}_corner.mat'  # 0 values fixed to the corners
# TRAIN_PATH = f'data/N1100_R{R}_clip.mat'
TEST_PATH = TRAIN_PATH

path = f'fno_R{R}_sub{sub}_ep{epochs}_ba{batch_size}_lr{learning_rate}_step{scheduler_step}_gamma{scheduler_gamma}'
path_model = 'model/'+path
path_pred = 'pred/'+path+'.mat'
path_train_err = 'results/'+path+'train.txt'
path_test_err = 'results/'+path+'test.txt'

runtime = np.zeros(2, )
t1 = default_timer()

################################################################
# load data
################################################################

reader = MatReader(TRAIN_PATH)
train_a = reader.read_field('a_out')[:ntrain,::sub,::sub,::sub]
train_u = reader.read_field('u_out')[:ntrain,::sub,::sub,::sub]

reader = MatReader(TEST_PATH)
test_a = reader.read_field('a_out')[-ntest:,::sub,::sub,::sub]
test_u = reader.read_field('u_out')[-ntest:,::sub,::sub,::sub]
# test_a = reader.read_field('a_out')[:ntest,::sub,::sub,::sub]
# test_u = reader.read_field('u_out')[:ntest:,::sub,::sub,::sub]

# print("Where 0?: ", type((test_u[:,:,:,:] == 0).nonzero()))
# print("train 0s: ", (train_a[:,:,:,:] == 0).nonzero().shape)
# print("where 0?: ", (test_a[:,:,:,:] == 0).nonzero().shape)
# assert ntrain == (train_a[:,:,:,:] == 0).nonzero().shape[0],    "0s not picked up in subsampling"
# assert ntrain == (train_u[:,:,:,:] == 0).nonzero().shape[0],    "0s not picked up in subsampling"
# assert ntest == (test_a[:,:,:,:] == 0).nonzero().shape[0],      "0s not picked up in subsampling"
# assert ntest == (test_u[:,:,:,:] == 0).nonzero().shape[0],      "0s not picked up in subsampling"
# print("train 0s: ", (train_a[:,:,:,:] == 0).nonzero().shape, )
# print("where 0?: ", (test_a[:,:,:,:] == 0).nonzero().shape)

print("shape of input data", reader.read_field('a_out').shape)
print("Shape of subsampled data: ", train_a.shape)
assert (S == train_u.shape[-2])
assert (S == train_u.shape[-1])

# Normalization
# a_normalizer = ScaleNormalizer(train_a, ub=200, lb=0)
# train_a = a_normalizer.encode(train_a)
# a_normalizer = ScaleNormalizer(test_a, ub=200, lb=0)
# test_a = a_normalizer.encode(test_a)
# y_normalizer = ScaleNormalizer(train_u, ub=200, lb=0)
# train_u = y_normalizer.encode(train_u)

train_a = train_a.reshape(ntrain,S,S,S,1)
test_a = test_a.reshape(ntest,S,S,S,1)

train_loader = torch.utils.data.DataLoader(torch.utils.data.TensorDataset(train_a, train_u), batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(torch.utils.data.TensorDataset(test_a, test_u), batch_size=batch_size, shuffle=False)

t2 = default_timer()

print('preprocessing finished, time used:', t2-t1)
device = torch.device('cuda')

# Data output arrays
train_l2s = []
test_l2s = []
train_mses = []
pred = torch.cat((torch.zeros(train_u.shape), torch.zeros(test_u.shape)), 0)

################################################################
# training and evaluation
################################################################
model = FNO3d(modes, modes, modes, width).cuda()
print("Number of model parameters: ", count_params(model))

optimizer = Adam(model.parameters(), lr=learning_rate, weight_decay=1e-4)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=scheduler_step, gamma=scheduler_gamma)
# myloss = LpLoss(size_average=False)
myloss = torch.nn.MSELoss(reduction='mean')

for ep in range(epochs):
    model.train()
    t1 = default_timer()
    train_mse = 0
    train_l2 = 0
    for x, y in train_loader:
        x, y = x.cuda(), y.cuda()

        optimizer.zero_grad()
        out = model(x).view(batch_size, S, S, S)

        l2 = myloss(out, y)
        # l2 = myloss(out.view(batch_size, -1), y.view(batch_size, -1))
        l2.backward()

        # y = y_normalizer.decode(y)
        # out = y_normalizer.decode(out)
        mse = F.mse_loss(out, y, reduction='mean')
        # mse.backward()

        optimizer.step()
        train_mse += mse.item()
        train_l2 += l2.item()

    scheduler.step()

    model.eval()
    test_l2 = 0.0
    with torch.no_grad():
        for x, y in test_loader:
            x, y = x.cuda(), y.cuda()
            out = model(x).view(batch_size, S, S, S)

            # out = y_normalizer.decode(out)
            # test_l2 += myloss(out.view(batch_size, -1), y.view(batch_size, -1)).item()
            test_l2 += myloss(out, y).item()

    train_mse = train_mse / len(train_loader)  # mse is mean of batch, so divide it by num batches
    train_l2 = np.sqrt(train_l2 / len(train_loader))
    test_l2 = np.sqrt(test_l2 / len(test_loader))

    train_l2s.append(train_l2)
    train_mses.append(train_mse)
    test_l2s.append(test_l2)

    t2 = default_timer()
    print(ep, t2-t1, train_mse, train_l2, test_l2)

# Save the trained model
save_flag = input("Save model? (y/n): ")
# save_flag = 'y'
if save_flag == 'y':
    torch.save(model, path_model)
    print("Model saved as: ", path_model)

### GENERATE PREDICITONS FOR TRAIN AND TEST DATA WITH TRAINED MODEL
# test_mses = []
# test_l2s = []
train_loader = torch.utils.data.DataLoader(torch.utils.data.TensorDataset(train_a, train_u),
                                          batch_size=1,
                                          shuffle=False,
                                          )
test_loader = torch.utils.data.DataLoader(torch.utils.data.TensorDataset(test_a, test_u),
                                          batch_size=1,
                                          shuffle=False,
                                          )
with torch.no_grad():
    index = 0
    for x, y in train_loader:
        train_l2 = 0
        x, y = x.cuda(), y.cuda()

        out = model(x).view(1,S,S,S)
        # train_l2 += np.sqrt(myloss(out.view(1, -1), y.view(1, -1)).item())
        train_l2 += np.sqrt(myloss(out, y).item())
        pred[index] = out.view(S,S,S)

        # print(index, train_l2)
        index += 1

    for x, y in test_loader:
        test_l2 = 0
        x, y = x.cuda(), y.cuda()

        out = model(x).view(1,S,S,S)
        # out = y_normalizer.decode(out)

        # test_l2 += np.sqrt(myloss(out.view(1, -1), y.view(1, -1)).item())
        test_l2 += np.sqrt(myloss(out, y).item())
        pred[index] = out.view(S,S,S)

        print(index, test_l2)
        # test_l2s.append(test_l2)
        index = index + 1

train_a = train_a.reshape(ntrain,S,S,S)
test_a = test_a.reshape(ntest,S,S,S)

save_flag = input("Save predictions? (y/n): ")
if save_flag == 'y':
    scipy.io.savemat(path_pred, mdict={'x'    : torch.cat((train_a, test_a), dim=0).numpy(),
                                       'y'    : torch.cat((train_u, test_u), dim=0).numpy(),
                                       'pred': pred.cpu().numpy(),
                                       'train_mses': np.array(train_mses),
                                       'train_l2s': np.array(train_l2s),
                                       'test_l2s': np.array(test_l2s)
                                       })
    print("Predictions saved as: ", path_pred)


import matplotlib
matplotlib.use('TkAgg')

from Tkinter import *
import Tkinter as tk
import time
from datetime import datetime
from PIL import ImageTk, Image
import Client
import Server

class Janela_Principal():

    def __init__(self):

        self.window = tk.Tk()
        self.window.geometry("298x298+100+100")
        self.window.title("GATONET")
        self.window.configure(background = 'white')
        self.window.resizable(False, False)

        # Geometria da pagina
        self.window.rowconfigure(0, minsize = 100)
        self.window.rowconfigure(1, minsize = 10)
        self.window.rowconfigure(2, minsize = 10)
        self.window.rowconfigure(3, minsize = 10)
        self.window.rowconfigure(4, minsize = 10)
        self.window.rowconfigure(5, minsize = 10)
        self.window.columnconfigure(0, minsize = 10)
        self.window.columnconfigure(1, minsize = 10)

        #Label
        self.Logo = ImageTk.PhotoImage(Image.open("cat.jpg"))
        self.Logo_label = tk.Label(self.window, image = self.Logo, height = 1, width = 1)
        self.Logo_label.grid(row = 0, column = 0, sticky = "nsew")

        #Botoes
        self.button_treinar = tk.Button(self.window, text = "SEND", height = 3, width = 30)
        self.button_treinar.grid(row = 1, columnspan = 1)
        self.button_treinar.configure(command = self.Send)

        self.button_Reconhecimento = tk.Button(self.window, text = "RECEIVE", height = 3, width = 30)
        self.button_Reconhecimento.grid(row   = 2, columnspan = 1)
        self.button_Reconhecimento.configure(command = self.Receive)

        # self.button_des = tk.Text(self.window, text = "Disconnect", height = 3, width = 30)
        # self.button_des.grid(row = 3, columnspan = 1)
        # self.button_des.configure(command = self.Send)

        self.var = StringVar(self.window)
        self.var.set("Choose your port") # initial value

        self.option = OptionMenu(self.window, self.var, "/dev/cu.usbmodem1461","/dev/cu.usbmodem1451", "/dev/cu.usbmodem1441", "/dev/cu.usbmodem1431", "/dev/cu.usbmodem1411","/dev/ttyACM0","/dev/ttyACM1","COM1","COM2","COM3","COM4","COM5","COM6" )
        self.option.grid(row   = 4)

        self.text = "Waiting"
        self.w = Label(self.window, text=self.text,font=("Helvetica", 20))
        self.w.grid(row = 5, columnspan = 1)
        # w.pack()

    def refreshText(self,text):
        self.w = Label(self.window, text=text ,font=("Helvetica", 20))
        self.w.grid(row = 5, columnspan = 1)

    #Loop do codigo
    def iniciar(self):
        self.window.mainloop()

    #Acoes dos botoes
    def Receive(self):
        self.refreshText("Receiving")
        time.sleep(0.3)
        self.refreshText(Server.main(self.var.get()))

    def Send(self):
        # print(self.var.get())
        # self.window.filename = filedialog.askopenfilename()
        # self.refreshText("Sending")
        time.sleep(0.3)
        self.refreshText(Client.main(self.var.get()))

#Loop do codigo
app = Janela_Principal()
app.iniciar()


class Student :
    def __init__(self,name,age,gender,level,grades):
        self.name = name
        self.age = age
        self.gender = gender
        self. level = level
        self.grades = grades

    def display(self):
        print ( self.name,
                self.age,
                self.gender,
                self.level,
                self.grades)

John = Student("John",15, 'boy' , 3, 'Good')

John.display()


import argparse

def parse_args(args=None):
    parser = argparse.ArgumentParser(
        fromfile_prefix_chars='@', description="JWST space dust trainer"
    )
    parser.add_argument(
        "--likelihood_model",
        default='baseline',
        type=str,
        help="which likelihood model to train"
    )
    parser.add_argument(
        "--posterior_model",
        default='baseline',
        type=str,
        help="which posterior model to train"
    )
    parser.add_argument(
        "--likelihood_model_kwargs",
        default='{}',
        type=str,
        help="likelihood model's keyword arguments passed as a dictionary"
    )
    parser.add_argument(
        "--posterior_model_kwargs",
        default='{}',
        type=str,
        help="posterior model's keyword arguments passed as a dictionary"
    )
    parser.add_argument(
        "--likelihood_trained_model",
        default='',
        type=str,
        help="If set as non-empty string, likelihood model will be loaded"
    )
    parser.add_argument(
        "--posterior_trained_model",
        default='',
        type=str,
        help="If set as non-empty string, posterior model will be loaded"
    )
    parser.add_argument(
        "--batch", default=64, type=int, help="batch size"
    )
    parser.add_argument(
        "--lr", default=1e-3, type=float, help="learning rate"
    )
    parser.add_argument(
        "--val_frac",
        default=0.2,
        type=float,
        help="fraction of the dataset to be left as validation set"
    )
    parser.add_argument(
        "--n_epochs", default=100, type=int, help="number of epochs"
    )
    parser.add_argument(
        "--n_workers",
        default=8,
        type=int,
        help="number of workers used in dataloader"
    )
    parser.add_argument(
        "--min_d",
        default=0.,
        type=float,
        help="minimal value of elements on the diagonal of D matrix in LDL " +
             "decomposition of the covariance matrix"
    )
    parser.add_argument(
        "--n_betas",
        default=10,
        type=int,
        help="number of temperature samples in the annealing schedule"
    )
    parser.add_argument(
        "--beta_1",
        default=1e-10,
        type=float,
        help="value of first temperature in the annealing schedule"
    )
    parser.add_argument(
        "--n_importance_samples",
        default=5,
        type=int,
        help="number of samples in importance sampling estimating the evidence"
    )
    parser.add_argument(
        "--target_acc_prob",
        default=0.7,
        type=float,
        help="target acceptance probability in HMC"
    )
    parser.add_argument(
        "--mixture_alpha",
        default=1.,
        type=float,
        help="weight for posterior model in the mixture model"
    )
    parser.add_argument(
        "--normalize",
        action="store_true",
        help="if set, datasets will be normalized"
    )
    parser.add_argument(
        "--drop_unstable_filters",
        action="store_true",
        help="if set, filters with unstable predictions will be ignored"
    )
    parser.add_argument('data_path', type=str, help="path to ECGs' directory")
    parser.add_argument('stats_path', type=str)
    if args is None:
        parsed_args = parser.parse_args()
    else:
        parsed_args = parser.parse_args(args)

    # print(f'@{parsed_args.stats_path}/args.txt')
    """
    parsed_args = parser.parse_args(
        f'@./local-test-experiment/args.txt', namespace=parsed_args
    )
    """
    return parsed_args


from time import time
import pygame, os

# Constants related to overall gameplay.
# Sizes are made relative to the overall HEIGHT (of the window).
ASPECT_RATIO = 1.6
HEIGHT = 720
WIDTH = int(HEIGHT * ASPECT_RATIO)
FPS = 60
YELLOW = (255, 255, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
START_TIME = time()
SCREEN_TYPES = ["space", "pepper_trip"]
SMUG_FREQ = 10 # For every 10 scores give smug soundbite.

# Set up window
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Space Cadets: Lemon Defenders")  # Title of window.
SPACE = pygame.transform.scale(
    pygame.image.load(os.path.join("assets", "simpsons_space.jpg")),
    (WIDTH, HEIGHT)
)
PEPPER_LAND = pygame.transform.scale(
    pygame.image.load(os.path.join("assets", "pepper_land.png")),
    (WIDTH, HEIGHT)
)


from __future__ import print_function
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import Perceptron, LogisticRegression
from sklearn.metrics import accuracy_score, recall_score, f1_score, precision_score
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeClassifier
from sklearn.decomposition import PCA
import progressbar

# Progressbar setup
bar = progressbar.ProgressBar(widgets=[
    ' [', progressbar.Timer(), '] ',
    progressbar.Bar(),
    ' (', progressbar.ETA(), ') ',
])

# Parameters
pca_val = [10, 50, 100, 200, 300, 400, 500, 600]
# Perceptron
pe_error = []
pe_acc = []
pe_pre = []
pe_rc = []
pe_f1 = []
pe_v = [pe_error, pe_acc, pe_pre, pe_rc, pe_f1]

# Naive Bayes
nb_error = []
nb_acc = []
nb_pre = []
nb_rc = []
nb_f1 = []
nb_v = [nb_error, nb_acc, nb_pre, nb_rc, nb_f1]

# Decision Tree
dt_error = []
dt_acc = []
dt_pre = []
dt_rc = []
dt_f1 = []
dt_v = [dt_error, dt_acc, dt_pre, dt_rc, dt_f1]

# Random Forest
rf_error = []
rf_acc = []
rf_pre = []
rf_rc = []
rf_f1 = []
rf_v = [rf_error, rf_acc, rf_pre, rf_rc, rf_f1]

# Logistic Regression
lr_error = []
lr_acc = []
lr_pre = []
lr_rc = []
lr_f1 = []
lr_v = [lr_error, lr_acc, lr_pre, lr_rc, lr_f1]

# KNN
kn_error = []
kn_acc = []
kn_pre = []
kn_rc = []
kn_f1 = []
kn_v = [kn_error, kn_acc, kn_pre, kn_rc, kn_f1]

print("Loading data...")

# loading the data set
df = pd.read_csv('LABTESTS_DEATH_V2.csv', header=None)
X = df.iloc[1:, 1:-2].values
Y = df.iloc[1:, -1].values
Y = Y.astype(np.float)

# Split train test
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.1, random_state=0)

# Standardization
sc = StandardScaler()
sc.fit(X_train)
X_train_sd = sc.transform(X_train)
X_test_sd = sc.transform(X_test)

print("Data loaded...")

print("With PCA")

# Fit models using PCA
print("Applying PCA...")

for n_c in bar(pca_val):
    # Feature extraction using PCA with number of components as 500
    pca = PCA(n_components=n_c)
    X_train_sd_pca = pca.fit_transform(X_train_sd)
    X_test_sd_pca = pca.transform(X_test_sd)
    pca1 = PCA(n_components=n_c)
    X_train_pca = pca1.fit_transform(X_train)
    X_test_pca = pca1.transform(X_test)
    # Perceptron Model
    pe_pca = Perceptron(n_iter=10, eta0=10, n_jobs=-1)
    pe_pca.fit(X_train_sd_pca, Y_train)

    # Naive Bayes Classification
    nb_pca = GaussianNB()
    nb_pca.fit(X_train_pca, Y_train)

    # Decision Tree Classifier
    dt_pca = DecisionTreeClassifier(max_depth=4)
    dt_pca.fit(X_train_pca, Y_train)

    # RandomForest Classifier
    rf_pca = RandomForestClassifier(criterion='entropy', n_estimators=10, n_jobs=-1)
    rf_pca.fit(X_train_pca, Y_train)

    # Logistic Regression trained with PCA
    lr_pca = LogisticRegression()
    lr_pca.fit(X_train_sd_pca, Y_train)

    # Gaussian Process Classifier with PCA
    gpc_pca = KNeighborsClassifier()
    gpc_pca.fit(X_train_sd_pca, Y_train)

    print("Models with PCA trained")
    print("Testing...")

    # Prediction for Perceptron
    Y_pred_pe_pca = pe_pca.predict(X_test_sd_pca)

    # Prediction for Naive Bayes
    Y_pred_nb_pca = nb_pca.predict(X_test_pca)

    # Prediction for Decision Tree Classifier
    Y_pred_dt_pca = dt_pca.predict(X_test_pca)

    # Prediction for RandomForest Classifier
    Y_pred_rf_pca = rf_pca.predict(X_test_pca)

    # Prediction for Logistic Regression
    Y_pred_lr_pca = lr_pca.predict(X_test_sd_pca)

    # Gaussian Process Classifier
    Y_pred_gpc_pca = gpc_pca.predict(X_test_sd_pca)

    print("PCA n_components : %d" % n_c)

    # Accuracy, precision, recall and F1 score
    for y, x, p in zip([Y_pred_pe_pca, Y_pred_nb_pca, Y_pred_dt_pca, Y_pred_rf_pca, Y_pred_lr_pca, Y_pred_gpc_pca],
                    ['PERCEPTRON PCA', 'NAIVE_BAYES PCA', 'DECISION_TREE PCA',
                     'RANDOM_FOREST PCA', 'LOGISTIC_REGRESSION PCA', 'KNeighborsClassifier'],
                       [pe_v, nb_v, dt_v, rf_v, lr_v, kn_v]):
        print(x)
        error = (Y_test != y).sum()
        p[0].append(error)
        print("Errors    : %d" % error)
        acc = accuracy_score(y, Y_test) * 100
        p[1].append(acc)
        print("Accuracy  : %.2f%%" % acc)
        ps = precision_score(y, Y_test) * 100
        p[2].append(ps)
        print("Precision : %.2f%%" % ps)
        rs = recall_score(y, Y_test) * 100
        p[3].append(rs)
        print("Recall    : %.2f%%" % rs)
        f1 = f1_score(y, Y_test) * 100
        p[4].append(f1)
        print("F1 Score  : %.2f%% \n" % f1)

    print("\n")

print("WITHOUT PCA")
# Perceptron Model
pe = Perceptron(n_iter=10, eta0=10, n_jobs=-1)
pe.fit(X_train_sd, Y_train)

# Naive Bayes Classification
nb = GaussianNB()
nb.fit(X_train, Y_train)

# Decision Tree Classifier
dt = DecisionTreeClassifier(max_depth=4)
dt.fit(X_train, Y_train)

# RandomForest Classifier
rf = RandomForestClassifier(criterion='entropy', n_estimators=10, n_jobs=-1)
rf.fit(X_train, Y_train)

# Logistic Regression
lr = LogisticRegression()
lr.fit(X_train_sd, Y_train)

# KNeighborsClassifier
gpc = KNeighborsClassifier()
gpc.fit(X_train_sd, Y_train)

print("Models trained")
print("Predicting...")

# Prediction for Perceptron
Y_pred_pe = pe.predict(X_test_sd)

# Prediction for Naive Bayes
Y_pred_nb = nb.predict(X_test)

# Prediction for Decision Tree Classifier
Y_pred_dt = dt.predict(X_test)

# Prediction for RandomForest Classifier
Y_pred_rf = rf.predict(X_test)

# Prediction for Logistic Regression
Y_pred_lr = lr.predict(X_test_sd)

# Prediction for Gaussian Process Classifier
Y_pred_gpc = gpc.predict(X_test_sd)

# Accuracy, precision, recall and F1 score
for y, x, p in zip([Y_pred_pe, Y_pred_nb, Y_pred_dt, Y_pred_rf, Y_pred_lr, Y_pred_gpc],
                ['PERCEPTRON', 'NAIVE_BAYES', 'DECISION_TREE', 'RANDOM_FOREST', 'LOGISTIC_REGRESSION',
                 'KNeighborsClassifier'], [pe_v, nb_v, dt_v, rf_v, lr_v, kn_v]):
    print(x)
    error = (Y_test != y).sum()
    p[0].append(error)
    print("Errors    : %d" % error)
    acc = accuracy_score(y, Y_test) * 100
    p[1].append(acc)
    print("Accuracy  : %.2f%%" % acc)
    ps = precision_score(y, Y_test) * 100
    p[2].append(ps)
    print("Precision : %.2f%%" % ps)
    rs = recall_score(y, Y_test) * 100
    p[3].append(rs)
    print("Recall    : %.2f%%" % rs)
    f1 = f1_score(y, Y_test) * 100
    p[4].append(f1)
    print("F1 Score  : %.2f%% \n" % f1)

print("************************************************************************")
print("\n")
print("Values for graphs")
for i, j in zip(['PERCEPTRON', 'NAIVE_BAYES', 'DECISION_TREE', 'RANDOM_FOREST', 'LOGISTIC_REGRESSION',
                 'KNeighborsClassifier'], [pe_v, nb_v, dt_v, rf_v, lr_v, kn_v]):
    print(i)
    print(j)
    print("**********************************************************************")
    print("\n")
# Plotting Graphs
# x-axis : without PCA,
per_x_axis = [10, 50, 100, 200, 300, 400, 500, 600, 726]
for p, t in zip([pe_v, nb_v, dt_v, rf_v, lr_v, kn_v], ['PERCEPTRON', 'NAIVE_BAYES', 'DECISION_TREE', 'RANDOM_FOREST',
                                                       'LOGISTIC_REGRESSION','KNeighborsClassifier']):

    plt.subplot(1, 2, 1)
    plt.plot(per_x_axis, p[1], marker='o', color='blue', label='Accuracy')
    plt.plot(per_x_axis, p[2], marker='s', color='red', label='Precision')
    plt.plot(per_x_axis, p[3], marker='x', color='green', label='Recall')
    plt.plot(per_x_axis, p[4], marker='D', color='black', label='F1 Score')
    if t != 'NAIVE_BAYES':
        plt.ylim([80.0, 100.0])
    else:
        plt.ylim([10.0, 95.0])
    plt.title('{} Feature Extraction'.format(t))
    plt.xlabel('Features Extracted')
    plt.ylabel('Scores')
    plt.legend(loc='lower right')

    plt.subplot(1, 2, 2)
    plt.plot(per_x_axis, p[0], label='No. of Errors')
    plt.xlabel('Features Extracted')
    plt.ylabel('Errors')
    plt.title('Error Plot')

    plt.show()


from tabulate import tabulate
import logging
from datetime import datetime, timedelta
from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth.models import Group, Permission

logger = logging.getLogger(__name__)

class Command(BaseCommand):
    help = "Print permissions by group"

    def add_arguments(self, parser):
        parser.add_argument("-g", "--group", help="group to print", type=str)

    def handle(self, *args, **options):
        groups = None
        if options.get("group"):
            groups = Group.objects.filter(name=options.get("group")).all()
        else:
            groups = Group.objects.all()

        headers = ["Permission", "Description"]
        for group in groups:
            print(group)
            data = []
            for perm in group.permissions.all():
                data.append([perm.codename, perm.name])
            print(tabulate(data, headers=headers, tablefmt="rst"))


# Define your item pipelines here
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html

from kafka import KafkaProducer
from json import dumps
import time

class Module03Pipeline(object):
    def __init__(self):
        self.producer = KafkaProducer(acks=0,
                        compression_type='gzip',
                        bootstrap_servers=['127.0.0.1:9092'],
                        value_serializer=lambda x :dumps(x).encode('utf-8')
                        # dumps 파이썬의 dict를 json형태로 바꿔줌
                        # bite를 원래형태로 할때는 역직렬화
                        # 직렬화는 json타입으로 전달해줘야한다.
        )
    def process_item(self, item, spider):

        item = dict(item)

        data = {"schema":{"type":"struct","fields":[{"type":"string","field":"title"},{"type":"string","field":"weekend"},{"type":"string","field":"gross"},{"type":"string","field":"weeks"},{"type":"string","field":"genre"},{"type":"string","field":"rating"},{"type":"string","field":"movie_release"},{"type":"string","field":"people"},{"type":"int64","name":"org.apache.kafka.connect.data.Timestamp","version":1,"field":"created_at"}],"name":"users"},"payload":{"title":item['title'],"weekend":item['weekend'],"gross":item['gross'],"weeks":item['weeks'],"genre":item['genre'],"rating":item['rating'],"movie_release":item['movie_release'],"people":item['people'],"created_at":int(time.time()*1000)}}

        self.producer.send('my_topic_users', value = data)
        time.sleep(10)
        self.producer.flush()
        print('Done:')

       # print('*********************',item,'*********************')


from http.client import HTTPResponse
from django.http import Http404, HttpResponseNotFound, HttpResponseRedirect
from django.urls import reverse
from django.template.loader import render_to_string
from django.shortcuts import render
# Create your views here.
challenges = {
    "jan": "Jan",
    "feb": "feb",
    "mar": "mar",
    "apr": "apr",
    "may": "may",
    "jun": "jun",
    "jul": "jul",
    "aug": "aug",
    "sep": "sep",
    "oct": "oct",
    "nov": "nov",
    "dec": "dec",
}

def index(request):
    list_items = "
"
    months = list(challenges.keys())
    for month in months:
        redirect_path = reverse('month-challenge', args=[month])
        list_items += f"
{month.capitalize()}
"
    list_items += "
"

    return render(request,'challenges/index.html',{
        'months':challenges,
    })

def monthly_challanges_by_number(request, month):
    months = list(challenges.keys())
    forward_month = None
    try:
        forward_month = months[month-1]
    except:
        return HttpResponseNotFound("Not valid url")

    redirect_path = reverse('month-challenge', args=[forward_month])
    return HttpResponseRedirect(redirect_path)

def monthly_challanges(request, month):

    try:

        return render(request, 'challenges/challange.html',{
            'text': challenges[month],
            'month': month
        })
    except:
        raise Http404()
        response = render_to_string('404.html')
        return HttpResponseNotFound(response)