import sys
sys.path.insert(0, r"D:\BaiduNetdiskDownload\temp\multilabel")

import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim

from data_process import dataset
from torch.utils.data import DataLoader

from model import vgg16_bn_body
from data_process.train_val_spilt import train_val_split
import copy

if __name__ == "__main__":

  root_path = r"D:\BaiduNetdiskDownload\temp\TinyMind"
  train_tags_path = "tag_train.npz"
  train_img_dir = "train"
  train_img_order_csv = "visual_china_train1.csv"

  tiny_mind_dataset = dataset.TinyMindDataset(root_path, train_tags_path, train_img_dir, train_img_order_csv)

  train_loader, validation_loader = train_val_split(tiny_mind_dataset)
  # tiny_mind_dataloader = DataLoader(tiny_mind_dataset, batch_size=5, shuffle=True)

  dataloaders = {"train": train_loader,
                 "val": validation_loader,}

  dataset_sizes =  {"train": 30000,
                    "val": 5000,}
# print(torch.cuda.current_device())
#   device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
  device = torch.device("cpu")
  print(device)

  model_ft = vgg16_bn_body.model_vgg(device)

# model_ft = models.inception_v3(pretrained=True)
# n = model_ft.fc.in_features
# model_ft.fc = nn.Linear(n, 6941)

  criterion = nn.MultiLabelSoftMarginLoss()

  optimizer_ft = optim.Adam(model_ft.classifier.parameters(), lr=1e-5)
# def train()

# Decay LR by a factor of 0.1 every 7 epochs
# exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=7, gamma=0.1)
  num_epochs = 20
  best_loss = 99999

  for epoch in range(num_epochs):
    print('Epoch {}/{}'.format(epoch, num_epochs - 1))
    print('-' * 10)
    for phase in ['train', 'val']:
      if phase == 'train':
        model_ft.train()  # Set model to training mode
      else:
        model_ft.eval()  # Set model to evaluate mode

      running_loss = 0.0
      for inputs, labels in dataloaders[phase]:
        inputs = inputs.to(device)
        labels = labels.to(device)

        # zero the parameter gradients
        optimizer_ft.zero_grad()

        # forward
        # track history if only in train
        with torch.set_grad_enabled(phase == 'train'):
          outputs = model_ft(inputs)

          loss = criterion(outputs, labels)

          # backward + optimize only if in training phase
          if phase == 'train':
            loss.backward()
            optimizer_ft.step()

        # statistics
        running_loss += loss.item() * inputs.size(0)
      epoch_loss = running_loss / dataset_sizes[phase]

      print('{} Loss: {:.4f} '.format(
        phase, epoch_loss))

      if phase == 'val' and epoch_loss < best_loss:
        best_loss = epoch_loss
        best_model_wts = copy.deepcopy(model_ft.state_dict())
        print("saving ....")
        torch.save(model_ft.state_dict(), 'params.pkl')

    # print("training")
    # print('-' * 10)
    # train_running_loss = 0.0
    # model_ft.train()
    #
    # for idx, (imgs, labels) in enumerate(tiny_mind_dataloader):
    #   imgs = imgs.to(device)
    #   labels = labels.to(device)
    #
    #   # print(imgs.size())
    #   # print(labels.size())
    #
    #   optimizer_ft.zero_grad()
    #
    #   outputs = model_ft(imgs)
    #   loss = criterion(outputs, labels)
    #
    #   loss.backward()
    #   optimizer_ft.step()
    #
    #   if idx % 10 == 0:
    #     print("train_loss: {}".format(loss))
    #
    #     # val_loss = 0.0
    #     # for imgs_val, labels_val in validation_loader:
    #     #   imgs_val = imgs_val.to(device)
    #     #   labels_val = labels_val.to(device)
    #     #   outputs = model_ft(imgs_val)
    #     #   loss = criterion(outputs, labels_val)
    #     #   val_loss += loss
    #     # print("val_loss: {}".format(val_loss))
    #
    #   if idx % 5000 == 0:
    #     print("saving model")
    #     torch.save(model_ft.state_dict(), 'params.pkl')
        #

    # break


#!/usr/bin/env python3
"""Write a function def np_slice(matrix, axes={}):
that slices a matrix along specific axes"""

def np_slice(matrix, axes={}):
    """Slices a matrix like a ninja"""
    ax_values = {}
    max_axis = max(axes.keys())

    for j in range(max_axis):
        ax_values[j] = (None, None, None)

    for key in axes.keys():
        ax_values[key] = axes[key]

    return(matrix[tuple(slice(*ax_values[i]) for i in ax_values.keys())])


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time

start = time.time()

chrome_options = Options()
chrome_options.add_argument('--disable-gpu')
#chrome_options.add_argument('--headless')

web = webdriver.Chrome(options=chrome_options)
web.get('https://lagou.com')

#找到元素
el = web.find_element_by_xpath('//*[@id="changeCityBox"]/p[1]/a')
el.click()  #点击事件

#避免错误
time.sleep(1)

#找到输入框
web.find_element_by_xpath('//*[@id="search_input"]').send_keys("渗透测试",Keys.ENTER)
time.sleep(1)

# 查找存放的数据
li_list = web.find_elements_by_xpath('//*[@id="s_position_list"]/ul/li')

for li in li_list:
    job_name = li.find_element_by_tag_name("h3").text
    job_money = li.find_element_by_xpath('./div[1]/div[1]/div[2]/div/span').text
    place = li.find_element_by_xpath('./div/div[2]/div/a').text
    with open(".\\lagou.txt",'a') as f:
        print(job_name,job_money,place)
        f.write(f"{job_name},{job_money},{place}\n")

end = time.time()
print('cost time %s s' %(end - start))


import tensorflow as tf

import aiaiTools
from ..linear import Scale, Tensordot

class Q_Tensordot(Tensordot):
    def __init__(self,massAttenuationSpectra,axes=[[1],[0]],**kwargs):
        self.massAttenuationSpectra = massAttenuationSpectra
        if axes is None:
            axes = [[1],[0]]
        super(Q_Tensordot,self).__init__(self.massAttenuationSpectra, axes=axes,**kwargs)

class S0_Scale(Scale):
    def __init__(self,sourceSpectra,preFilterSpectra=None,**kwargs):
        if preFilterSpectra is None:
            preFilterSpectra = 1
        self.sourceSpectra = sourceSpectra
        self.preFilterSpectra = preFilterSpectra
        scaleSpectra = sourceSpectra*preFilterSpectra
        super(S0_Scale,self).__init__(scaleSpectra,**kwargs)

class S1_Scale(Scale):
    def __init__(self,interactionSpectra,**kwargs):
        self.interactionSpectra = interactionSpectra
        super(S1_Scale,self).__init__(self.interactionSpectra,**kwargs)

class S2_Tensordot(Tensordot):
    def __init__(self,conversionSpectra,axes=None,**kwargs):
        if axes is None:
            axes = [[1],[0]]
        self.conversionSpectra = conversionSpectra
        super(S2_Tensordot,self).__init__(self.conversionSpectra,axes=axes,**kwargs)

class G_Scale(Scale):
    def __init__(self,gains,**kwargs):
        self.gains = gains
        super(G_Scale,self).__init__(self.gains,**kwargs)


import time
import threading
from client import PlaneSocket
import random

active_threads = 0
def flight_number_generator():
        num = 1
        while True:
            yield num
            num += 1

def create_client(flight_num):
    client = PlaneSocket(flight_num)
    client.send_socket()

flight_num_gen = flight_number_generator()

while True:
    flight_num = next(flight_num_gen)
    thread = threading.Thread(target=create_client, args=(flight_num,))
    thread.start()
    active_threads +=1
    time.sleep(random.randint(10,50)*0.1)


'''
Created on Sep 15, 2012

@author: hande
'''
import unittest
from main.plotter import Plotter
import sys

sys.path.append("../")

class TestPlotter(unittest.TestCase):

    def setUp(self):
        self.x_array = [1, 5, 3, 4, 7, 10]
        self.y_array = [0, 3, 8, 2, 5, 1]
        self.size_array = [1, 2, 3, 4, 5, 6]

    def tearDown(self):
        self.x_array = None
        self.y_array = None
        self.size_array = None

    def test_scatter(self):
        '''
        Tests the scatter method of the Plotter class.
        '''
        Plotter.scatter('Title', 'file_name', self.x_array, \
                        self.y_array, self.size_array, 'Xaxis', 'Yaxis', 10, 20, 1)

if __name__ == "__main__":
    unittest.main()


import pandas as pd
import openpyxl
import os
import urllib
import datetime
from db_utils.base import Session, Session_ax
import matplotlib.pyplot as plt
import seaborn as sns
from extract_info import get_armoires_etuves, GetPrice, moteur2demarrer, var2art, map_moteur
from tqdm import tqdm

with Session() as session:
    # Read table: dimensional_ax.schema_special_plus
    df = pd.read_sql('SELECT * FROM dimensional_ax.schema_special_plus', session.bind)

# df.to_csv('schema_special.csv', index=False)

#  DISTRIBUTION EQUIPMENTS
types = df['Type']
# Count the number of each type
types_count = types.groupby(types).count()
# Sort values
types_count.sort_values(inplace=True, ascending=False)
# Keep only the first 10
types_count = types_count[:10]
# Plot
plt.figure(figsize=(9, 6))
plt.bar(types_count.index, types_count.values)
plt.title("Distribution équipements")
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

#  Fix number of motors Soufflage
for i, row in df.iterrows():
    if pd.isna(row['MS1']):
        df.at[i, 'Nb_MS'] = 0
    elif pd.isna(row['MS2']):
        df.at[i, 'Nb_MS'] = 1
    elif pd.isna(row['MS3']):
        df.at[i, 'Nb_MS'] = 2
    elif pd.isna(row['MS4']):
        df.at[i, 'Nb_MS'] = 3

#  Fix number of motors Extraction
for i, row in df.iterrows():
    if pd.isna(row['ME1']):
        df.at[i, 'Nb_ME'] = 0
    elif pd.isna(row['ME2']):
        df.at[i, 'Nb_ME'] = 1
    elif pd.isna(row['ME3']):
        df.at[i, 'Nb_ME'] = 2
    elif pd.isna(row['ME4']):
        df.at[i, 'Nb_ME'] = 3

# ETUVE
# Filter only etuve
df_etuve = df.loc[df['Type'] == 'ETUVE'].copy()

# NOMBRE DE MOTEURS
# Plot Nb_MS distribution
fig, ax = plt.subplots(1, 1, figsize=(9, 6))

# Calculate frequencies for column Nb_MS
freq_MS = df_etuve['Nb_MS'].value_counts()
freq_ME = df_etuve['Nb_ME'].value_counts()

# Put distribution of Nb_MS and Nb_ME side by side. Same width, use an offset
plt.bar(freq_MS.index - 0.2, freq_MS.values, width=0.4, label='Soufflage')
plt.bar(freq_ME.index + 0.2, freq_ME.values, width=0.4, label='Extraction')
plt.title("Nombre de moteurs - Etuves")
plt.legend()
plt.tight_layout()
plt.show()

# Puissance moteurs
df_etuve['Tot_puiss_MS'] = df_etuve['MS1'].fillna(0) + df_etuve['MS2'].fillna(0)  + df_etuve['MS3'].fillna(0)  + df_etuve['MS4'].fillna(0)
df_etuve['Tot_puiss_ME'] = df_etuve['ME1'].fillna(0)  + df_etuve['ME2'].fillna(0)  + df_etuve['ME3'].fillna(0)  + df_etuve['ME4'].fillna(0)
df_etuve['Mean_puiss_MS'] = df_etuve['Tot_puiss_MS'] / df_etuve['Nb_MS']
df_etuve['Mean_puiss_ME'] = df_etuve['Tot_puiss_ME'] / df_etuve['Nb_ME']

# Make a boxplot for MS and ME (share x axis)
fig, ax = plt.subplots(2, 1, figsize=(9, 10), sharex=True)
sns.boxplot(y=df_etuve['Mean_puiss_MS'], ax=ax[0], x=df_etuve['Nb_MS'])
ax[0].set_title("Puissance moyenne moteurs soufflage")
sns.boxplot(y=df_etuve['Mean_puiss_ME'], ax=ax[1], x=df_etuve['Nb_ME'])
ax[1].set_title("Puissance moyenne moteurs extraction")
plt.tight_layout()
plt.show()

get_price = GetPrice()

#  Plot motors prices as scatter X=power, Y=price
fig, ax = plt.subplots(1, 1, figsize=(9, 6))
x = [ motor['power'] for motor in map_moteur]
y = [ get_price.get_price(motor['code']) for motor in map_moteur]
y_demarr = [ get_price.get_price(motor['code']) + get_price.get_price(moteur2demarrer(motor['power'])) for motor in map_moteur]
ax.scatter(x, y, label='Prix Moteur', color='red', marker='x')
ax.scatter(x, y_demarr, label='Prix Moteur + Demarrer', color='blue', marker='o')

#  add a simple linear regression line for y_demarr
import numpy as np
from scipy.stats import linregress
slope, intercept, r_value, p_value, std_err = linregress(x, y_demarr)
x_line = np.linspace(0, max(x), 100)
y_line = slope * x_line + intercept
ax.plot(x_line, y_line, label='Regression', color='green')

plt.xlabel('Puissance moteur (kW)')
plt.ylabel('Prix (€)')
plt.legend()
# Add grid
plt.grid()
#  Show rounded equation of regression line by adding a text box
#  https://stackoverflow.com/questions/15910019/annotate-data-points-while-plotting-from-pandas-dataframe
text = "y = {:.1f}x + {:.0f}".format(slope, intercept)
props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)
ax.text(0.05, 0.8, text, transform=ax.transAxes, fontsize=14,
        verticalalignment='top', bbox=props)

plt.title('Prix moteurs en fonction de la puissance')
plt.tight_layout()
#  Save figure in ./plots
plt.savefig('./plots/price_motor.png')
plt.show()

# DISTRIBUTION PUISSANCE MOTEURS
df_etuve = get_armoires_etuves(cache=False)
df1 = df_etuve.loc[(df_etuve['Nb_MS'] == 1) & (df_etuve['Nb_ME'] == 1)].copy()

#  Make a bubble plot using df1 where x = puissance soufflage, y = puissance extraction, size = nb armoires
distinct_cases = df1.groupby(['MS1', 'ME1'], as_index=False)['Num_AF'].count()
# Add number of armoires as text on each bubble
distinct_cases['text'] = distinct_cases['Num_AF'].apply(lambda x: str(x))

#  Plot
fig, ax = plt.subplots(1, 1, figsize=(9, 6))
ax.scatter(distinct_cases['MS1'], distinct_cases['ME1'], s=distinct_cases['Num_AF']*10)
# Add text
for i, txt in enumerate(distinct_cases['text']):
    ax.annotate(txt, (distinct_cases['MS1'].iloc[i], distinct_cases['ME1'].iloc[i]))
plt.xlabel('Puissance soufflage (kW)')
plt.ylabel('Puissance extraction (kW)')
plt.title('Distribution des puissances moteurs- pour les étuves Nb_MS=1, Nb_ME=1')
plt.tight_layout()
#  Save figure in ./plots
plt.savefig('./plots/distribution_puissances.png')
plt.show()

# DISTRIBUTION NOMBRE DE MOTEURS
# Make a bubble plot using df_etuve where x= Nb_MS, y = Nb_ME, size = nb armoires
distinct_cases = df_etuve.groupby(['Nb_MS', 'Nb_ME'], as_index=False)['Num_AF'].count()
#  Plot
fig, ax = plt.subplots(1, 1, figsize=(9, 6))
ax.scatter(distinct_cases['Nb_MS'], distinct_cases['Nb_ME'], s=distinct_cases['Num_AF']*10)
plt.xlabel('Nombre de moteurs soufflage')
plt.ylabel('Nombre de moteurs extraction')
plt.title('Distribution des nombres de moteurs - pour les étuves')
plt.tight_layout()
#  Save figure in ./plots
plt.savefig('./plots/distribution_nb_moteurs.png')
plt.show()


# The function localize takes the following arguments:
#
# colors:
#        2D list, each entry either 'R' (for red cell) or 'G' (for green cell)
#
# measurements:
#        list of measurements taken by the robot, each entry either 'R' or 'G'
#
# motions:
#        list of actions taken by the robot, each entry of the form [dy,dx],
#        where dx refers to the change in the x-direction (positive meaning
#        movement to the right) and dy refers to the change in the y-direction
#        (positive meaning movement downward)
#        NOTE: the *first* coordinate is change in y; the *second* coordinate is
#              change in x
#
# sensor_right:
#        float between 0 and 1, giving the probability that any given
#        measurement is correct; the probability that the measurement is
#        incorrect is 1-sensor_right
#
# p_move:
#        float between 0 and 1, giving the probability that any given movement
#        command takes place; the probability that the movement command fails
#        (and the robot remains still) is 1-p_move; the robot will NOT overshoot
#        its destination in this exercise
#
# The function should RETURN (not just show or print) a 2D list (of the same
# dimensions as colors) that gives the probabilities that the robot occupies
# each cell in the world.
#
# Compute the probabilities by assuming the robot initially has a uniform
# probability of being in any cell.
#
# Also assume that at each step, the robot:
# 1) first makes a movement,
# 2) then takes a measurement.
#
# Motion:
#  [0,0] - stay
#  [0,1] - right
#  [0,-1] - left
#  [1,0] - down
#  [-1,0] - up

import random
def sense(p, world, measurement, sensor_right):
    # This is an initialization of the p that you will return
    q = [[0 for _ in range(len(p[0]))] for _ in range(len(p))]
    sensor_wrong = 1 - sensor_right
    # Now we loop over all of the probabilities and determine if we observed what we were looking for
    # we then update our beliefs based on our prior. We make allowances for incorrect sensor
    # readings.
    for i in range(len(p)):
        for j in range(len(p[0])):
            hit = (measurement == world[i][j])
            q[i][j] = p[i][j] * (hit * sensor_right + (1-hit) * sensor_wrong)
    # calculate total probability
    # you have to wrap the sum because its ugly. much better in numpy
    s = sum([sum(i) for i in q])
    # normalize by total probability
    for i in range(len(q)):
        for j in range(len(p[0])):
            q[i][j] = q[i][j] / s
    return q

def move(p, motion, p_move):
    q = [[0 for _ in range(len(p[0]))] for _ in range(len(p))]
    p_stay = 1-p_move
    # if we are staying put then just return p
    # this will make for a weird edge case
    if motion[0] == 0 and motion[1] == 0:
        return p
    elif motion[0] == 0:
        # moving horizontally
        motion = motion[1]
        for i in range(len(p)):
            for j in range(len(p[0])):
                q[i][j] = p_move * p[i][(j - motion) % len(p[0])] + p_stay * p[i][j]
    else:
        # moving vertically
        motion = motion[0]
        for i in range(len(p)):
            for j in range(len(p[0])):
                q[i][j] = p_move * p[(i - motion) % len(p)][j] + p_stay * p[i][j]
    return q

def localize(colors, measurements, motions, sensor_right, p_move):
    # initializes p to a uniform distribution over a grid of the same dimensions as colors
    pinit = 1.0 / float(len(colors)) / float(len(colors[0]))
    p = [[pinit for row in range(len(colors[0]))] for col in range(len(colors))]
    for i in range(len(measurements)):
        p = move(p, motions[i], p_move)
        p = sense(p, colors, measurements[i], sensor_right)
    return p

def show(p):
    rows = ['[' + ','.join(map(lambda x: '{0:.5f}'.format(x), r)) + ']' for r in p]
    print('[' + ',\n '.join(rows) + ']')

#############################################################
# For the following test case, your output should be
# [[0.01105, 0.02464, 0.06799, 0.04472, 0.02465],
#  [0.00715, 0.01017, 0.08696, 0.07988, 0.00935],
#  [0.00739, 0.00894, 0.11272, 0.35350, 0.04065],
#  [0.00910, 0.00715, 0.01434, 0.04313, 0.03642]]
# (within a tolerance of +/- 0.001 for each entry)

colors = [['R', 'G', 'G', 'R', 'R'],
          ['R', 'R', 'G', 'R', 'R'],
          ['R', 'R', 'G', 'G', 'R'],
          ['R', 'R', 'R', 'R', 'R']]
measurements = ['G', 'G', 'G', 'G', 'G']
motions = [[0, 0], [0, 1], [1, 0], [1, 0], [0, 1]]
p = localize(colors, measurements, motions, sensor_right=0.7, p_move=0.8)
show(p)  # displays your answer

colors = [['G', 'G', 'G'],
          ['G', 'R', 'G'],
          ['G', 'G', 'G']]
measurements = ['R']
motions = [[0,0]]
sensor_right = 1.0
p_move = 1.0
p = localize(colors, measurements, motions, sensor_right=sensor_right, p_move=p_move)
show(p)  # displays your answer

# test 2
colors = [['G', 'G', 'G'],
          ['G', 'R', 'R'],
          ['G', 'G', 'G']]
measurements = ['R']
motions = [[0,0]]
sensor_right = 1.0
p_move = 1.0
p = localize(colors,measurements,motions,sensor_right,p_move)
show(p)

colors = [['G', 'G', 'G'],
          ['G', 'R', 'R'],
          ['G', 'G', 'G']]
measurements = ['R', 'R']
motions = [[0,0], [0,1]]
sensor_right = 1.0
p_move = 0.5
p = localize(colors,measurements,motions,sensor_right,p_move)
show(p)

colors = [['G', 'G', 'R','G','R']]
measurements = ['R', 'R']
motions = [[0,1]]
sensor_right = 0.9
p_move = 1.0
p = localize(colors,measurements,motions,sensor_right,p_move)
show(p)


import sqlite3
import tkinter as tk
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
import fileworker

class Doctor(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.grid_columnconfigure(0, weight=1)

        label = tk.Label(self, text="DOCTORS MENU", font=('Helvetica', 16))
        main_frame = MainFrame(self, self.controller)
        button = tk.Button(self, text="Back", font=('Helvetica', 14), width=20, bg="#669", fg='White', bd=1, command=lambda: controller.show_frame("StartPage"))

        label.grid(row=0, column=0, pady=(20, 0))
        main_frame.grid(row=1, column=0, pady=25, ipady=5)
        button.grid(row=2, column=0, pady=0)

    def menubar(self, root):
        menubar = tk.Menu(root, tearoff=0)

        fileMenu = tk.Menu(menubar, tearoff=0)
        fileMenu2 = tk.Menu(fileMenu, tearoff=0)

        menubar.add_cascade(label="File", menu=fileMenu)
        fileMenu.add_cascade(label="Export as...", menu=fileMenu2)
        fileMenu2.add_command(label="*.docx", command=fileworker.export_docx)
        fileMenu2.add_command(label="*.xlsx", command=fileworker.export_xlsx)
        fileMenu.add_separator()
        fileMenu.add_command(label="Quit", command=quit)
        menubar.add_command(label="Help", command=self.show_help)

        return menubar

    def show_help(self):
        text = '''Information for help'''
        window = tk.Toplevel()
        label = tk.Label(window, text=text, font=("Helvetica", 10), bd=1, relief='sunken', justify="left")
        label.pack(pady=10, padx=10, ipady=10, ipadx=10)
        button = tk.Button(window, text='Close', command=window.destroy)
        button.pack(pady=(0, 10))

class MainFrame(tk.LabelFrame):
    def __init__(self, parent, controller):
        tk.LabelFrame.__init__(self, parent)
        self.controller = controller
        self.columnconfigure(0, weight=0)
        self.columnconfigure(1, weight=0)
        self.columnconfigure(2, weight=1)
        self.columnconfigure(3, weight=0)

        self.photo = tk.PhotoImage(file=r"C:\Users\kripi\Downloads\refresh.png")
        refresh_button = tk.Button(self, image=self.photo, command=lambda: self.update_trv())
        refresh_button.grid(row=0, column=4, padx=(0, 10))
        self.trv = ttk.Treeview(self, columns=(1, 2, 3, 4, 5, 6), show='headings', height=13)
        self.trv.grid(row=0, column=0, columnspan=3, pady=10, padx=10)
        self.scroll_bar = ttk.Scrollbar(self, orient="vertical")
        self.scroll_bar.grid(row=0, column=3, sticky='ns', padx=(0, 10), pady=10)
        self.trv.configure(yscrollcommand=self.scroll_bar.set)
        self.scroll_bar.configure(command=self.trv.yview)
        self.trv.bind('<ButtonRelease-1>', self.clicker)

        self.trv.heading(1, text="ID")
        self.trv.column(1, width=10)
        self.trv.heading(2, text="Full name")
        self.trv.column(2, width=200)
        self.trv.heading(3, text="Speciality")
        self.trv.column(3, width=150)
        self.trv.heading(4, text="Appointment cost")
        self.trv.column(4, width=110)
        self.trv.heading(5, text="Payroll percentage")
        self.trv.column(5, width=110)
        self.trv.heading(6, text="Salary")
        self.trv.column(6, width=100)

        self.update_trv()

        info_text = """To add a doctor, fill out the form and click the 'Add' button.\nTo update/delete a doctor, click on desired doctor in table and click the 'Update'/'Delete' button."""
        info_label = tk.Label(self, text=info_text, font=("Helvetica", 10), bd=1, relief='sunken', justify="left")
        label1 = tk.Label(self, text="Full name:")
        label2 = tk.Label(self, text="Speciality:")
        label3 = tk.Label(self, text="Appointment cost:")
        label4 = tk.Label(self, text="Payroll percentage:")
        self.entry1 = tk.Entry(self)
        self.entry2 = tk.Entry(self)
        self.entry3 = tk.Entry(self)
        self.entry4 = tk.Entry(self)
        button_add = tk.Button(self, text="Add", width=20, command=lambda: self.add_doctor())
        button_update = tk.Button(self, text="Update", width=20, command=lambda: self.update_doctor())
        button_delete = tk.Button(self, text="Delete", width=20, command=lambda: self.delete_doctor())
        button_clear = tk.Button(self, text='Clear', width=20, command=lambda: self.clear_fields())

        self.entry1.grid(row=2, column=1, sticky='w', padx=0, pady=0)
        self.entry2.grid(row=3, column=1, sticky='w', padx=0, pady=0)
        self.entry3.grid(row=4, column=1, sticky='w', padx=0, pady=0)
        self.entry4.grid(row=5, column=1, sticky='w', padx=0, pady=0)
        info_label.grid(row=1, columnspan=3, sticky='we', padx=10, pady=(0, 10), ipadx=10, ipady=5)
        label1.grid(row=2, column=0, sticky='w', padx=10, pady=0)
        label2.grid(row=3, column=0, sticky='w', padx=10, pady=0)
        label3.grid(row=4, column=0, sticky='w', padx=10, pady=0)
        label4.grid(row=5, column=0, sticky='w', padx=10, pady=0)
        button_add.grid(row=2, column=2, sticky='w', padx=10, pady=0)
        button_update.grid(row=3, column=2, sticky='w', padx=10, pady=0)
        button_delete.grid(row=4, column=2, sticky='w', padx=10, pady=0)
        button_clear.grid(row=5, column=2, sticky='w',padx=10)

    def clear_fields(self):
        self.entry1.delete(0, END)
        self.entry2.delete(0, END)
        self.entry3.delete(0, END)
        self.entry4.delete(0, END)

    def clicker(self, e):
        self.clear_fields()
        selected = self.trv.focus()
        values = self.trv.item(selected, 'values')

        self.entry1.insert(0, values[1])
        self.entry2.insert(0, values[2])
        self.entry3.insert(0, values[3])
        self.entry4.insert(0, values[4])

    def update_trv(self):
        for i in self.trv.get_children():
            self.trv.delete(i)
        con = sqlite3.connect('polyclinic.db')
        cur = con.cursor()
        for i in cur.execute('SELECT * FROM doctor'):
            self.trv.insert('', 'end', values=i)

    def add_doctor(self):
        con = sqlite3.connect("polyclinic.db")
        cur = con.cursor()
        dname = self.entry1.get()
        dspec = self.entry2.get()
        acost = self.entry3.get()
        perc = self.entry4.get()

        try:
            if dname is None or len(dname) == 0 or dname.isnumeric():
                raise ValueError
            if dspec is None or len(dspec) == 0 or dspec.isnumeric():
                raise ValueError
            if int(acost) < 0:
                raise ValueError
            if int(perc) < 0:
                raise ValueError
        except ValueError:
            messagebox.showerror("Error!", "Input correct values!")
        else:
            values = (dname, dspec, acost, perc)
            sql = """\
                   INSERT INTO doctor (doctor_name, doctor_speciality, appointment_cost, payroll_percentage)
                   VALUES (?,?,?,?);
               """
            cur.execute(sql, values)
            con.commit()
            cur.close()
            con.close()
            self.clear_fields()
            self.update_trv()

    def update_doctor(self):
        con = sqlite3.connect("polyclinic.db")
        cur = con.cursor()
        try:
            did = self.trv.item(self.trv.selection())['values'][0]
            dname = self.entry1.get()
            dspec = self.entry2.get()
            acost = self.entry3.get()
            perc = self.entry4.get()

            if did is None:
                raise IndexError
            if dname is None or len(dname) == 0 or dname.isnumeric():
                raise ValueError
            if dspec is None or len(dspec) == 0 or dname.isnumeric():
                raise ValueError
            if int(acost) < 0:
                raise ValueError
            if int(perc) < 0:
                raise ValueError
        except ValueError:
            messagebox.showerror("Error!", "Input correct values!")
        except IndexError:
            messagebox.showerror("Error!", "Select doctor to update!")
        else:
            if messagebox.askyesno("Confirm updation!", "Are you sure?"):
                cur.execute('UPDATE doctor SET doctor_name = ?, doctor_speciality = ?, appointment_cost = ?, payroll_percentage = ? WHERE id = ?;', (dname, dspec, acost, perc, did))
                con.commit()
                cur.close()
                con.close()
                self.clear_fields()
                self.update_trv()

    def delete_doctor(self):
        con = sqlite3.connect("polyclinic.db")
        cur = con.cursor()
        try:
            did = self.trv.item(self.trv.selection())['values'][0]
            lst = []
            for row in cur.execute('SELECT id FROM doctor;'):
                for key in row:
                    lst.append(key)
            if int(did) not in lst:
                raise ValueError
            if did is None:
                raise IndexError
        except ValueError:
            messagebox.showerror("Error!", "No such doctor!")
        except IndexError:
            messagebox.showerror("Error!", "Select doctor to delete!")
        else:
            if messagebox.askyesno("Confirm deleting!", "Are you sure?"):
                cur.execute('DELETE FROM receipt WHERE doctor_id = ?', (did,))
                con.commit()
                cur.execute('DELETE FROM doctor WHERE id = ?', (did,))
                con.commit()
                cur.close()
                con.close()
                self.clear_fields()
                self.update_trv()


from django.contrib import admin
from django.urls import path, include
from django.contrib.auth.views import LoginView, LogoutView

from rest_framework_jwt.views import obtain_jwt_token, refresh_jwt_token

urlpatterns = [
    path('admin/', admin.site.urls),
    path('login/', LoginView.as_view(), name='login'),
    path('logout/', LogoutView.as_view(), name='logout'),
    path('', include('accounts.urls', namespace='accounts')),
]

urlpatterns += [
    path('api-auth-token/', obtain_jwt_token),
    path('api-token-refresh/', refresh_jwt_token),
]


'''Advent of code, day 18: SNAILFISH'''

# Basically a Python version of the solution here:
# https://www.reddit.com/r/adventofcode/comments/rjekr4/2021_day_18_simpler_solution_with_data_structure/
# Still many of my own personal implementations though!

from math import floor, ceil, inf
from copy import deepcopy

def opt_chain(value, index):
    '''A bad implementation of the .? operator in JavaScript'''
    if isinstance(value, dict) and index in value:
        return value[index]
    return None

def to_value(node):
    '''Change from {} to []'''
    if opt_chain(node, "value") == 0:
        return 0
    return list(map(to_value, node))\
        if opt_chain(node, "value") is None\
        else opt_chain(node, "value")

def to_tree(array):
    '''Convert a value:depth type snailfish number to a tree'''
    if len(array) == 2:
        return to_value(array)
    for i, item in enumerate(array[:-1]):
        v1, d1, v2, d2 = \
            item["value"], item["depth"],\
            array[i + 1]["value"], array[i + 1]["depth"]
        if d1 == d2:
            return to_tree([
                *array[0:i],
                {"value" : [v1, v2], "depth" : d1 - 1},
                *array[i+2:]
            ])

    return array

def magnitude_sfn(value):
    '''Get the magnitude of a tree type snailfish number'''
    if isinstance(value, int):
        return value

    return 3 * magnitude_sfn(value[0]) + 2 * magnitude_sfn(value[1])

def list_to_depths(sfn: list, depth: int = 0) -> list:
    '''Convert snailfish lists into snailfish depths'''
    final = []
    if isinstance(sfn[0], int):
        final.append([sfn[0], depth])
    if isinstance(sfn[0], list):
        vals = list_to_depths(sfn[0], depth + 1)
        if isinstance(vals[0], list):
            final.extend(vals)
        else:
            final.append(vals)

    if isinstance(sfn[1], int):
        final.append([sfn[1], depth])
    if isinstance(sfn[1], list):
        vals = list_to_depths(sfn[1], depth + 1)
        if isinstance(vals[1], list):
            final.extend(vals)
        else:
            final.append(vals)

    return final

def explode_sfn(sfn: list) -> bool:
    '''Explode first explodable in a snailfish number'''
    for i, (value, depth) in enumerate(sfn[:]):
        if depth >= 4:
            if i > 0:
                sfn[i - 1][0] += value
            if i < len(sfn) - 2:
                sfn[i + 2][0] += sfn[i + 1][0]
            sfn[i:i+2] = [[0, depth - 1]]
            return True
    return False

def split_sfn(sfn: list) -> bool:
    '''Split the first splittable in a snailfish number'''
    for i, (value, depth) in enumerate(sfn[:]):
        if value > 9:
            sfn[i:i+1] = [[
                floor(value / 2),
                depth + 1],[
                ceil(value / 2),
                depth + 1]]
            return True
    return False

def reduce_sfn(sfn: list) -> list:
    '''Reduce a snailfish number'''
    while explode_sfn(sfn) or split_sfn(sfn):
        pass
    return sfn

def add_sfn(sfn: list, sfn2: list) -> list:
    '''Add two snailfish numbers and reduce'''
    copy_sfn = deepcopy(sfn)
    copy_sfn.extend(sfn2)
    nsfn = [[n[0], n[1] + 1] for n in copy_sfn]
    return reduce_sfn(nsfn)

def task_1(sfnumbers):
    '''Code for task 1:
    '''
    copy_sfnumbers = deepcopy(sfnumbers)
    for i, sfn in enumerate(copy_sfnumbers):
        if i >= len(copy_sfnumbers) - 1:
            continue
        added = add_sfn(sfn, copy_sfnumbers[i + 1])
        copy_sfnumbers[i + 1] = added

    return magnitude_sfn(to_tree([{"value" : i[0], "depth" : i[1]} for i in copy_sfnumbers[-1]]))

def task_2(sfnumbers):
    '''Code for task 2:
    '''
    highest = -inf
    for sfn1 in sfnumbers:
        for sfn2 in sfnumbers:
            magnitude = \
                magnitude_sfn(
                    to_tree(
                        [{"value" : i[0], "depth" : i[1]}
                        for i in add_sfn(sfn1, sfn2)]
                    )
                )
            if magnitude > highest:
                highest = magnitude
    return highest

def main():
    '''Main function'''
    with open("aoc18_input.txt", "r", encoding = "utf-8") as file:
        contents = file.readlines()
        # Evaluate into list and convert into depths type
        sfnumbers = [list_to_depths(eval(l)) for l in contents]

    print(task_1(sfnumbers))
    print(task_2(sfnumbers))

if __name__ == "__main__":
    main()


from typing import Optional
import uuid
from pydantic import BaseModel, Field

class BlogModel(BaseModel):
    id: str = Field(default_factory=uuid.uuid4, alias="_id")
    title: str = Field(...)
    body: str = Field(...)
    published: bool = False

    class Config:
        allow_population_by_field_name = True
        schema_extra = {
            "example": {
                "id": "00010203-0405-0607-0809-0a0b0c0d0e0f",
                "title": "My crazy blogpost",
                "body": "Lorem ipsum topsy turfy Wurtle the turtle is great. How weird it is to write random words.",
                "published": False,
            }
        }

class UpdateBlogModel(BaseModel):
    name: Optional[str]
    completed: Optional[bool]

    class Config:
        schema_extra = {
            "example": {
                "title": "My crazy blogpost",
                "body": "Lorem ipsum topsy turfy Wurtle the turtle is great. How weird it is to write random words.",
                "published": False,
            }
        }


import numpy as np
from ..utils import Dataset
from .interface import TopicModel
from .man_model.layers import *
from .man_model.models import *
from .man_model import utils
import torch.utils.data as data_utils

def create_minibatch(data, label,z, batch_size):
    rng = np.random.RandomState(10)

    while True:
        # Return random data samples of a size 'minibatch_size' at each iteration
        ixs = rng.randint(data.shape[0], size=batch_size)
        yield data[ixs], label[ixs], z[ixs]

def softmax(x):
    """Compute softmax values for each sets of scores in x."""
    e_x = np.exp(x - np.max(x, axis=1).reshape(-1, 1))
    return e_x / np.sum(e_x, axis=1).reshape(-1, 1)

class MultinomialAdversarialNetwork(TopicModel):
    def __init__(self, k, m, domains, model_params=None, log_params=None):
        super().__init__(k,m,model_params,log_params)
        self.domains = domains
        self.loss = 'gr'
        self.clf = None

    def prepare_data(self,d):
        """
        Assume d is a dictionary of dataset where d[domain] = another dataset class
        Assume labeled domain = train set, unlabeled = test
        """
        train_loaders, train_iters = {}, {}
        for domain in self.domains:
            features, target = torch.from_numpy(d[domain].X.todense()), torch.from_numpy(d[domain].y)
            train = data_utils.TensorDataset(features,target)
            train_loaders[domain] = DataLoader(train, self.model_params['batch_size'], shuffle = True)
            train_iters[domain] = iter(train_loaders[domain])
        return train_loaders, train_iters

    def fit(self, d, *args, **kwargs):
        X = d.X
        y = d.y.reshape((-1, 1))
        z = d.z.reshape((-1,1))
        n_samples = d.X.shape[0]
        batch_size = self.model_params["batch_size"]
        if issparse(X):
            X = X.todense()
        #minibatches = create_minibatch(X, y, z, batch_size)
        train_loaders, train_iters = self.prepare_data(d)

        #Training
        F_s = MlpFeatureExtractor(X.shape[1], [1000,500],128, 0.4)
        F_d = {}
        for domain in self.domains:
            F_d[domain] = MlpFeatureExtractor(X.shape[1], [1000,500],128, 0.4)

        C = SentimentClassifier(3, 128+64,128+64, 1,0.4, True)
        D = DomainClassifier(3, 128, 128,len(self.domains), 'gr', 0.4, True)

        F_s, C, D = F_s.to('cuda'), C.to('cuda'), D.to('cuda')
        for f_d in F_d.values():
            f_d = f_d.to('cuda')
        # optimizers
        optimizer = optim.Adam(itertools.chain(*map(list, [F_s.parameters() if F_s else [], C.parameters()] + [f.parameters() for f in F_d.values()])), lr=0.0001)
        optimizerD = optim.Adam(D.parameters(), lr=0.0001)
        # training
        best_acc, best_avg_acc = defaultdict(float), 0.0

        for epoch in range(kwargs["training_epochs"]):
            F_s.train()
            C.train()
            D.train()
            for f in F_d.values():
                f.train()

            # training accuracy
            correct, total = defaultdict(int), defaultdict(int)
            # D accuracy
            d_correct, d_total = 0, 0
            # conceptually view 1 epoch as 1 epoch of the first domain
            num_iter = len(train_loaders[self.domains[0]])
            for i in tqdm(range(num_iter)):
                # D iterations
                utils.freeze_net(F_s)
                map(utils.freeze_net, F_d.values())
                utils.freeze_net(C)
                utils.unfreeze_net(D)
                # optional WGAN n_critic trick
                n_critic = 5

                for _ in range(n_critic):
                    D.zero_grad()
                    loss_d = {}
                    # train on both labeled and unlabeled domains
                    for domain in self.domains:
                        # targets not used
                        #Thay unlabeled_loaders/iters = train_loaders/iters?
                        d_inputs, _, d_targets = utils.endless_get_next_batch(
                            train_loaders, train_iters, domain)
                        shared_feat = F_s(d_inputs)
                        d_outputs = D(shared_feat)
                        # D accuracy
                        _, pred = torch.max(d_outputs, 1)
                        d_total += len(d_inputs)
                        if self.loss.lower() == 'l2':
                            _, tgt_indices = torch.max(d_targets, 1)
                            d_correct += (pred==tgt_indices).sum().item()
                            l_d = functional.mse_loss(d_outputs, d_targets)
                            l_d.backward()
                        else:
                            d_correct += (pred==d_targets).sum().item()
                            l_d = functional.nll_loss(d_outputs, d_targets)
                            l_d.backward()
                        loss_d[domain] = l_d.item()
                    optimizerD.step()

                # F&C iteration
                utils.unfreeze_net(F_s)
                map(utils.unfreeze_net, F_d.values())
                utils.unfreeze_net(C)
                utils.freeze_net(D)
                #if opt.fix_emb:
                #    utils.freeze_net(F_s.word_emb)
                #    map(utils.freeze_net, F_d.values())
                F_s.zero_grad()
                for f_d in F_d.values():
                    f_d.zero_grad()
                C.zero_grad()
                shared_feats, domain_feats = [], []
                for domain in self.domains:
                    inputs, targets = utils.endless_get_next_batch(
                            train_loaders, train_iters, domain)
                    targets = targets.to('cuda')
                    shared_feat = F_s(inputs)
                    shared_feats.append(shared_feat)
                    domain_feat = F_d[domain](inputs)
                    domain_feats.append(domain_feat)
                    features = torch.cat((shared_feat, domain_feat), dim=1)
                    c_outputs = C(features)
                    l_c = functional.nll_loss(c_outputs, targets)
                    l_c.backward(retain_graph=True)
                    # training accuracy
                    _, pred = torch.max(c_outputs, 1)
                    total[domain] += targets.size(0)
                    correct[domain] += (pred == targets).sum().item()
                # update F with D gradients on all domains
                for domain in self.domains:
                    d_inputs, _ = utils.endless_get_next_batch(
                            unlabeled_loaders, unlabeled_iters, domain)
                    shared_feat = F_s(d_inputs)
                    d_outputs = D(shared_feat)
                    if self.loss.lower() == 'gr':
                        d_targets = utils.get_domain_label(self.loss, domain, len(d_inputs))
                        l_d = functional.nll_loss(d_outputs, d_targets)
                        log.debug(f'D loss: {l_d.item()}')
                        if opt.lambd > 0:
                            l_d *= -opt.lambd
                    elif self.loss.lower() == 'l2':
                        d_targets = utils.get_random_domain_label(opt.loss, len(d_inputs))
                        l_d = functional.mse_loss(d_outputs, d_targets)
                        if opt.lambd > 0:
                            l_d *= opt.lambd
                    l_d.backward()

                optimizer.step()

            # end of epoch
            log.info('Ending epoch {}'.format(epoch+1))


import os, json, time
import pandas as pd
import datetime
from dash import dash, dcc, html, callback_context
import plotly.express as px
from dash.dependencies import Input, Output, State
import dash_daq as daq
import dash_bootstrap_components as dbc
import PiCar
import socket

df = None
car = PiCar.SensorCar(filter_depth=2)

def get_ip_address():
    """Ermittlung der IP-Adresse im Netzwerk

    Returns:
        str: lokale IP-Adresse
    """
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(("8.8.8.8", 80))
    socket_ip = s.getsockname()[0]
    s.close()
    return socket_ip

def getLoggerFiles():
    """Files im Ordner "Logger" auflisten

    Returns:
        list: Log-Dateien
    """
    fileList = []
    outputList = []
    if os.path.isdir("Logger"):
        fileList = os.listdir("Logger")
        for file in fileList:
            if file.partition(".")[2] == "log":
                outputList.append(file)
            outputList.sort()
            outputList.reverse()
    return outputList

def getLogItemsList():
    """Definition der Spaltennamen für die Daten im Logfile

    Returns:
        list: Spaltennamen
    """
    return [
        "time",
        "v",
        "dir",
        "st_angle",
        "US",
        "IR1",
        "IR2",
        "IR3",
        "IR4",
        "IR5",
    ]

def load_data_to_df(pfad):
    """aktualisiert die Daten im Dataframe

    Args:
        pfad (str): Dateipfad zum File das geladen werden soll
    """
    global df
    df = pd.read_json(os.path.join("Logger", pfad))
    df.columns = getLogItemsList()

FP_LISTE = [  # Liste der auswählbaren Fahrprogramme
    {"label": "FP 1, vor-zurück", "value": 1},
    {"label": "FP 2, im Kreis", "value": 2},
    {"label": "FP 3, US-Testfahrt", "value": 3},
    {"label": "FP 4, Erkundungsfahrt", "value": 4},
    {"label": "FP 5, LineFollower", "value": 5},
    {"label": "FP 6, LineFollower Eng", "value": 6},
    {"label": "FP 7, LineFollower Eng + Hindernis", "value": 7},
]

kpi_1 = dbc.Card([dbc.CardBody([html.H6("vMax"), html.P(id="kpi1")])])
kpi_2 = dbc.Card([dbc.CardBody([html.H6("vMin"), html.P(id="kpi2")])])
kpi_3 = dbc.Card([dbc.CardBody([html.H6("vMean"), html.P(id="kpi3")])])
kpi_4 = dbc.Card([dbc.CardBody([html.H6("time"), html.P(id="kpi4")])])
kpi_5 = dbc.Card([dbc.CardBody([html.H6("vm"), html.P(id="kpi5")])])

row_joystick = dbc.Row(
    [
        dbc.Col([html.P("Manuell on/off"), dbc.Switch(id="sw_manual")], width=4),
        dbc.Col(
            daq.Joystick(id="joystick", size=100, className="mb-3"),
            width=4,
        ),
        dbc.Col(
            [
                html.P(id="value_joystick"),
            ],
            width=4,
        ),
    ]
)
CARD_ManuelleSteuerung = dbc.Card(
    [
        dbc.Row(
            [  # Titel Manuelle Steuerung
                html.H3(
                    id="label_test",
                    children="Manuelle Steuerung",
                    style={
                        "textAlign": "left",
                        "paddingTop": 20,
                        "paddingBottom": 20,
                    },
                )
            ]
        ),
        dbc.Row(
            [  # Slider Speed
                dbc.Col([html.H6("max. speed:")], width=4),
                dbc.Col(
                    [
                        dcc.Slider(
                            min=0,
                            max=100,
                            step=10,
                            id="slider_speed",
                            value=40,
                            updatemode="drag",
                        )
                    ],
                    width=8,
                ),
            ]
        ),
        row_joystick,
    ],
)

COL_Logfiles = [
    dbc.Row(
        [
            html.H2(  # Überschrift
                id="title_Logfiles",
                children="Logfiles",
                style={"textAlign": "center", "paddingBottom": 20},
            ),
            dcc.Dropdown(  # Dropdown Logfiles
                id="dd_Logfiles",
                placeholder="Bitte Logging-File wählen!",
                options=getLoggerFiles(),
                value="0",
                multi=False,
                style={"color": "black"},
            ),
        ]
    ),
    dbc.Row(
        [
            dbc.Col([kpi_1], width=4),
            dbc.Col([kpi_2], width=4),
            dbc.Col([kpi_3], width=4),
        ],
        style={"paddingTop": 20, "paddingBottom": 20},
    ),
    dbc.Row(
        [
            dbc.Col([kpi_4], width=4),
            dbc.Col([kpi_5], width=4),
        ],
        style={"paddingBottom": 20},
    ),
    dbc.Row(
        [
            html.H3(id="titel_LogDetails", children="Plot-Line Auswahl"),
            dcc.Dropdown(  # Dropdown Log-Details
                id="dd_LogDetails",
                options=getLogItemsList()[1:],
                value=getLogItemsList()[1:4],
                multi=True,
                style={"color": "black"},
            ),
            # dcc.Checklist(
            #     id="cl_Graph_Lines", options=getLogGraphList(),
            # ),
        ],
        style={"paddingBottom": 10},
    ),
]
COL_Fahrzeugsteuerung = [  # Col Fahrzeugsteuerung
    dbc.Row(
        [  # Titel
            html.H2(
                id="titel_Fahrzeugsteuerung",
                children="Fahrzeugsteuerung",
                style={
                    "textAlign": "center",
                    "paddingBottom": 20,
                },
            )
        ]
    ),
    dbc.Row(
        [  # Dropdown Fahrprogramm
            dcc.Dropdown(
                id="dd_Fahrprogramm",
                placeholder="Bitte Fahrprogramm wählen:",
                options=FP_LISTE,
                value=0,
                style={"color": "black"},
            )
        ]
    ),
    dbc.Row(
        [  # Buttons
            dbc.Col(
                [
                    dbc.Button(
                        children="Start Prog",
                        id="btn_start",
                        color="dark",
                        className="me-1",
                        n_clicks=0,
                    )
                ],
                width=4,
            ),
            dbc.Col(
                [],
                width=4,
            ),
            dbc.Col(
                [
                    dbc.Button(
                        children="STOP",
                        id="btn_stop",
                        color="dark",
                        className="me-1",
                        n_clicks=0,
                    )
                ],
                width=4,
            ),
        ],
        style={"paddingTop": 20, "paddingBottom": 20},
        justify="center",
    ),
    dbc.Row(
        [
            CARD_ManuelleSteuerung,
        ]
    ),
]

app = dash.Dash(
    __name__,
    external_stylesheets=[dbc.themes.SUPERHERO],
    meta_tags=[
        {"name": "viewport"},
        {"content": "width = device,width, initial-scale=1.0"},
    ],
)

app.layout = dbc.Container(
    [
        dbc.Row(
            [  # Row 1
                dbc.Col(
                    [  # Col 1
                        html.H1(
                            id="title_main",
                            children="Camp2Code - Gruppe 4",
                            style={
                                "textAlign": "center",
                                "marginTop": 40,
                                "marginBottom": 40,
                            },
                        )
                    ],
                    width=12,
                ),
            ],
            justify="center",
        ),
        dbc.Row(
            [  # Row 2
                dbc.Col(  # Logfile-Handling
                    COL_Logfiles,
                    width=5,
                    # style={"backgroundColor": "darkgrey"},
                ),
                dbc.Col([], width=2),  # Col Space
                dbc.Col(
                    COL_Fahrzeugsteuerung,
                    width=5,
                    # style={"backgroundColor": "darkgrey"},
                ),
            ],
            justify="center",
        ),
        dbc.Row(
            [
                dcc.Graph(id="plot_Logfile"),
                html.P(
                    id="Fussnote",
                    children="hier das Copyright ;)",
                    style={"textAlign": "right"},
                ),
                html.Div(id="dummy"),
                html.Div(id="dummy2"),
                dcc.Interval(id="intervall_10s", interval=10000),
                dcc.Interval(
                    id="interval_startup",
                    max_intervals=1,
                ),
            ],
            style={"paddingTop": 10, "paddingBottom": 10},
        ),  # Row 3
    ]
)

@app.callback(
    Output("value_joystick", "children"),
    Input("joystick", "angle"),
    Input("joystick", "force"),
    State("sw_manual", "value"),
    State("slider_speed", "value"),
)
def joystick_values(angle, force, switch, max_Speed):
    """Steuerung über Joystick
        berechnet anhand der Joystick-Werte den Lenkeinschlag
        und zusätzlich mit der eingestellten Maximalgeschwindigkeit die Fahrgeschwindigkeit

    Args:
        angle (float): Winkelwert des Joysticks
        force (float): Kraftweg des Joysticks
        switch (bool): Schalter "manuelle Fahrt"
        max_Speed (int): Wert Slider "slider_speed"

    Returns:
        str: gibt die ermittelten Sollwerte zurück
    """
    debug = ""
    if angle != None and force != None:
        if switch:
            power = round(force, 1)
            winkel = 0
            dir = 0
            if force == 0:
                winkel = 0
                dir = 0
                car.drive(0, 0)
                car.steering_angle = 0
            else:
                power = power * max_Speed
                if power > max_Speed:
                    power = max_Speed
                if angle <= 180:
                    dir = 1
                    winkel = round(45 - (angle / 2), 0)
                else:
                    dir = -1
                    winkel = round(((angle - 180) / 2) - 45, 0)
                car.drive(int(power), dir)
                car.steering_angle = winkel
                debug = f"Angle: {winkel} speed: {power} dir: {dir}"
        else:
            debug = "Man. mode off"
            car.drive(0, 0)
            car.steering_angle = 0
    else:
        debug = "None-Value"
    return debug

def computeKPI(data):
    """Berechnung der Kenndaten des Log-Files

    Args:
        data (pandas.DataFrame): Log-Daten als DataFrame

    Returns:
        tuple of str: berechnete Werte
    """
    vmax = data["v"].max()
    vmin = data["v"][1:].min()
    vmean = round(data["v"].mean(), 2)
    duration = round(data["time"].max(), 2)
    route = round(vmean * duration, 2)
    return vmax, vmin, vmean, duration, route

@app.callback(
    Output(component_id="kpi1", component_property="children"),
    Output(component_id="kpi2", component_property="children"),
    Output(component_id="kpi3", component_property="children"),
    Output(component_id="kpi4", component_property="children"),
    Output(component_id="kpi5", component_property="children"),
    Input("dd_Logfiles", "value"),
)
def update_KPI_DD(logFile):
    """Aktualisieren der Kennwerte nach auswahl eines neuen Files

    Args:
        logFile (str): nur wegen input nötig

    Returns:
        str: setzt die "children" der kpi's
    """
    global df
    time.sleep(0.2)  # damit das File erst geladen werden kann
    try:
        vmax, vmin, vmean, duration, route = computeKPI(df)
        return vmax, vmin, vmean, duration, route
    except:
        return 0, 0, 0, 0, 0

@app.callback(
    Output("plot_Logfile", "figure"),
    Input("dd_Logfiles", "value"),
    Input("dd_LogDetails", "value"),
)
def selectedLog(logFile, logDetails):
    """Auswahl des Logfiles

    Args:
        logFile (str): Log-File
        logDetails (list): Liste mit Elementen die angezeigt werden sollen

    Returns:
        plotly figure: Graph mit ausgewählten Daten
    """
    global df
    if logFile != "0":
        load_data_to_df(logFile)
        # df = pd.read_json(os.path.join("Logger", logFile))
        # time.sleep(0.1)
        # df.columns = getLogItemsList()
        if logDetails != []:
            fig = px.line(df, x="time", y=logDetails, title=logFile)
        else:
            fig = px.line(df, x="time", y="st_angle", title=logFile)
        return fig
    else:
        return px.line()

@app.callback(
    Output("dd_Logfiles", "options"),
    Input("intervall_10s", "n_intervals"),
)
def updateFileList(value):
    """alle 10 Sekunden den Logger-Ordner auf neue Files prüfen"""
    return getLoggerFiles()

@app.callback(
    Output("sw_manual", "value"),
    Input("btn_start", "n_clicks"),
    Input("btn_stop", "n_clicks"),
    State("dd_Fahrprogramm", "value"),
    State("slider_speed", "value"),
)
def button_action(btn_start, btn_stop, fp, speed):
    """Buttons "Start" und "Stop" verarbeiten

    Returns:
        int: Schalter für manuellen Betrieb auf 0 setzen
    """
    changed_id = [p["prop_id"] for p in callback_context.triggered][0]
    if "btn_start" in changed_id:
        car.generischerFahrparcour(fp, speed)

    if "btn_stop" in changed_id:
        car._active = False
        car._worker.shutdown(wait=True)
        time.sleep(1)
    return 0

if __name__ == "__main__":
    app.run_server(debug=True, host=get_ip_address())


import os
from solver import Solver

"""
训练设置(适用于 "train.py"):

vgg_path:
    预训练VGG16(".pth")的路径, 用于初始化参数来训练您自己的CDNet.

ckpt_root:
    保存检查点文件(".pth")的文件夹路径, 每个epoch训练后都会自动保存.
    第i个epoch训练完成后, 检查点文件会被保存在 "ckpt_root/Weights_{}.pth".format(i).
    * 注意: 第一阶段训练时，检查点文件被命名为 "Weights_0.pth"，并不断被覆盖；

pretrain_init_epoch:
    第一阶段训练的起始epoch.
    当 "pretrain_init_epoch == 0" 时, CDNet用预训练的VGG16的参数对 RGB Encoder 进行初始化;
    否则, CDNet加载 "ckpt_root/Weights_0.pth" 来进行初始化,

pretrain_end_epoch:
    第一阶段训练的结束epoch.
    论文中该阶段为35个epochs.

train_init_epoch:
    第二阶段训练的起始epoch.
    当 "train_init_epoch == 0" 时, CDNet用预训练的VGG16的参数对 Depth Encoder 进行初始化，并使用第一阶段的参数
    "ckpt_root/Weights_0.pth" 对相关模块进行初始化;
    否则, CDNet加载 "ckpt_root/Weights_{}.pth".format(train_init_epoch) 处的检查点文件(".pth")来进行初始化,

train_end_epoch:
    第二阶段训练的结束epoch.
    论文中该阶段为60个epochs.

device:
    用于训练的GPU编号.

train_doc_path:
    用于保存训练过程所产生信息的文件(".txt"文档)路径.

train_roots:
    一个dict, 包含训练集 RGB 图片, GTs 和 Depth 图片的文件夹路径, 其格式为:
    train_roots = {'img': 训练集的 RGB 图片的文件夹路径,
                   'gt': 训练集的 GTs 的文件夹路径,
                   'depth': 训练集的 Depth 图片的文件夹路径}
"""

vgg_path = './vgg16_feat.pth'
ckpt_root = './ckpt/'
pretrain_init_epoch = 0
pretrain_end_epoch = 35
train_init_epoch = 0
train_end_epoch = 61
device = '0'
doc_path = './training.txt'
learning_rate = 5e-3
weight_decay = 1e-4
batch_size = 10
num_thread = 4

# 下面是一个构建 "train_roots" 的例子.
train_roots = {'img': '/mnt/jwd/data/NJU+NLPR/img/',
               'gt': '/mnt/jwd/data/NJU+NLPR/gt/',
               'depth': '/mnt/jwd/data/NJU+NLPR/depth/',
               'name_list': './train_1529.txt'}  # 其中 train_1529.txt 记录了保留下来的 depth maps 的图片名
# ------------ 示例结束 ------------

if __name__ == '__main__':
    os.environ['CUDA_VISIBLE_DEVICES'] = device
    solver = Solver()

    # 第一阶段训练
    solver.train(roots=train_roots,
                 vgg_path=vgg_path,
                 init_epoch=pretrain_init_epoch,
                 end_epoch=pretrain_end_epoch,
                 learning_rate=learning_rate,
                 batch_size=batch_size,
                 weight_decay=weight_decay,
                 ckpt_root=ckpt_root,
                 doc_path=doc_path,
                 num_thread=num_thread,
                 pretrain_mode=True,
                 pin=False)

    # 第二阶段训练
    solver.train(roots=train_roots,
                 vgg_path=vgg_path,
                 init_epoch=train_init_epoch,
                 end_epoch=train_end_epoch,
                 learning_rate=learning_rate,
                 batch_size=batch_size,
                 weight_decay=weight_decay,
                 ckpt_root=ckpt_root,
                 doc_path=doc_path,
                 num_thread=num_thread,
                 pretrain_mode=False,
                 pin=False)

    # 注: 经验上来看, 不通过第一阶段训练而直接进行第二阶段的联合训练, 性能会出现下降.


'''
Read an integer N. This N will be the number of integer numbers X that
will be read.

Print how many these numbers X are in the interval [10,20] and how many
values are out of this interval.

Input
The first line of input is an integer N (N < 10000), that indicates the
total number of test cases.
Each case is an integer number X (-107 < X < 107).

Output
For each test case, print how many numbers are in and how many values are
out of the interval.
'''

m_InputN = int(input())
m_InputX = []
m_In, m_Out = 0, 0
m_Min, m_Max = 10, 20

if m_InputN > 0:
    for i in range(m_InputN):
        m_InputX.append(int(input()))

for i in m_InputX:
    if i >= m_Min and i <= m_Max:
        m_In += 1
    else:
        m_Out += 1

print("{} in".format(m_In))
print("{} out".format(m_Out))


import random

rock = '''
    _______
---'   ____)
      (_____)
      (_____)
      (____)
---.__(___)
'''

paper = '''
    _______
---'   ____)____
          ______)
          _______)
         _______)
---.__________)
'''

scissors = '''
    _______
---'   ____)____
          ______)
       __________)
      (____)
---.__(___)
'''
user_number = int(input("What do you choose? Type 0 for Rock, 1 for Paper or 2 for Scissors. "))
if user_number < 0 or user_number > 2:
    print("Invalid number. You lose.")
else:
    rps_list = [rock, paper, scissors]

    user_choice = rps_list[user_number]
    print(user_choice)
    comp_number = random.randint(0, 2)
    comp_choice = rps_list[comp_number]
    print(f"Computer chose:{comp_choice}")

    if user_number == 0 and comp_number == 2:
        print("You win.")
    elif user_number == 2 and comp_number == 0:
        print("You lose.")
    elif user_number > comp_number:
        print("You win.")
    elif user_number < comp_number:
        print("You lose.")
    elif user_number == comp_number:
        print("It's a draw.")


import torch
from torch.distributions.distribution import Distribution
from torch.distributions.multinomial import Multinomial
from torch.distributions import Categorical
import torch
import numpy as np

# Referencef
# https://pytorch.org/docs/stable/distributions.html
# https://pytorch.org/docs/stable/_modules/torch/distributions/multinomial.html

print("-----------------------------------------")
print('Multinomial Distribution\n')

m = Categorical(torch.tensor([ 0.25, 0.25, 0.25, 0.25 ]))
print(m.sample())  # equal probability of 0, 1, 2, 3

m = Categorical(torch.tensor([ 0.8, 0.15, 0.05]))
sample = m.sample()
print(sample)
print(type(sample))  # equal probability of 0, 1, 2, 3
print(m.log_prob(sample))

probs_2d = [0.1, 0.9]
probs_2d = torch.Tensor(probs_2d)
sample_2d = torch.multinomial(probs_2d, 1, True)
print(sample_2d)

# Multinomial = (100, torch.tensor([1, 1]))
# x = m.sample()
# print(x)
# print(type(x))
# Multinomial(torch.tensor([1., 1., 1., 1.])).log_prob(x)

# https://pytorch.org/docs/stable/distributions.html#categorical
print("-----------------------------------------\n")

#
#
# print("-----------------------------------------")
# print('Torch Cat Sum\n')
# torch.manual_seed(999)
# print("##########  Input   ##########")
# x = torch.randn(2, 2)
# y = torch.randn(2, 2)
# print(x)
# print(y)
# print("##########  torch cat   ##########")
# z = torch.cat((x,y))
# print(z)
# z = torch.cat((x,y),0)
# print(z)
# z = torch.cat((x,y),1)
# print(z)
# print("##########  torch cat sum   ##########")
# z = torch.cat((x,y)).sum()
# print(z)
# print("-----------------------------------------")

# a.append(torch.randn(1))
# a.append(torch.randn(1))
# a.append(torch.randn(1))
# print(a)
# print(np.shape(a))
# # print(torch.sum(a)) #<-- 계산이 안됨.
# print(torch.cat(a).sum())

9. Basic for Reinforcement (numpy, pytorch, nn)/reinforce_test.py


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

import os
import sys
import plistlib

# 文件列表，其原理就是两个目录的文件进行比较，查找出同名文件
fileList = {}

def getFrames(plist):
	for k in plist:
		if k == 'frames':
			dlist = list(plist['frames'].keys())
			return dlist
	return None

# 查找同名文件
def checkSame(root, filename, name, srclist, srcIndex):
	isHave = False
	for index, filelist in enumerate(fileList.values()):
		if filelist[2] == name and index > srcIndex:
			if not isHave:
				isHave = True
				print('------------------------ SameFile ------------------------')
				print(root + '\\' + filename + '(' + name + ')')
				srclist[3] = True
			filelist[3] = True
			print(filelist[0] + '\\' + filelist[1] + '(' + filelist[2] + ')')

# 搜索文件
def searchFile(path):
	# 遍历
	for root, dirs, files in os.walk(path):
		for filename in files:
			# 分离文件名和后缀
			suffix = os.path.splitext(filename)[1]
			if suffix == '.png':
				fileList.setdefault(len(fileList.keys()), [root, filename, filename, False])
			elif suffix == '.plist':
				plist = getFrames(plistlib.readPlist(root + '\\' + filename))
				if plist is None:
					continue
				for k in plist:
					fileList.setdefault(len(fileList.keys()), [root, filename, k, False])

	'''
	for key, value in fileList.items():
		print('{key}:{value}'.format(key=key, value=value))
	'''
	# 查找,通过enumerate将数据组合为[index,value]的索引序列
	for index, flist in enumerate(fileList.values()):
		if flist[3] is False:
			checkSame(flist[0], flist[1], flist[2], flist, index)

if __name__ == '__main__':
	path = ''
	if len(sys.argv) == 2:
		path = sys.argv[1]
	else:
		curpath = os.getcwd() 							# 当前目录
		parentpath = os.path.dirname(curpath)			# curpath的父目录
		grandparentpath = os.path.dirname(parentpath) 	# parentpath的父目录
		path = os.path.join(grandparentpath, 'res')

	print(u'查找的目录为:' + path)
	if os.path.isdir(path) is False:
		print(u'目录不存在!!!')
		sys.exit()

	# 查找同名文件
	searchFile(path)


import scrapy
from COTO.items import CotoItem
from COTO.itemsloaders import CotoItemLoader

class TarantulaSpider(scrapy.Spider):
    name = 'tarantula'
    custom_settings = {
        'ITEM_PIPELINES': {
            #'COTO.pipelines.CotoPipeline': 300
        }
    }
    allowed_domains = ['cotodigital3.com.ar']
    # start_urls      = ['https://www.cotodigital3.com.ar/sitios/cdigi/browse/catalogo-frescos/_/N-1ewuqo6?Nf=product.endDate%7CGTEQ+1.670976E12%7C%7Cproduct.startDate%7CLTEQ+1.670976E12&Nr=AND%28product.sDisp_200%3A1004%2Cproduct.language%3Aespa%C3%B1ol%2COR%28product.siteId%3ACotoDigital%29%29']
    start_urls = [
        'https://www.cotodigital3.com.ar/sitios/cdigi/browse/catalogo-electro-informatica/_/N-u4pa0j']
    visited_pages = []

    def parse(self, response):
        products = response.css('#products li.clearfix')

        for product in products:
            product_item = CotoItemLoader(item=CotoItem(), selector=product)
            product_item.add_css('name', 'div.descrip_full::text')
            product_item.add_css('price', 'span.atg_store_newPrice::text')
            try:
                product_item.add_css('discount', 'span.price_discount::text')
            except Exception:
                product_item.add_css('discount', 'span.atg_store_newPrice::text')

            yield product_item.load_item()

        pages = response.css('div.atg_store_pagination a ::attr(href)')

        next_page = None

        for page in pages:
            total_page = 'cotodigital3.com.ar' + page.get()
            if total_page not in self.visited_pages:
                next_page = total_page
                self.visited_pages.append(next_page)

        if next_page != None:
            yield response.follow(next_page, callback=self.parse)


import discord
from discord.ext import commands

class Core(commands.Cog):
	def __init__(self,bot,config):
		self.bot = bot
		self.config = config
	def listStr(self,listIn):
		strOut=""
		if len(listIn)==1:
			strOut=listIn[0]
		elif len(listIn)==2:
			strOut=f"{listIn[0]} and {listIn[1]}"
		else:
			for i in listIn[:-1]:
				strOut+=i+", "
			strOut+="and "+listIn[-1]
		return strOut
	@commands.command()
	async def info(self, ctx):
		embed = discord.Embed(title="Bot Info")
		ownerNames=[]
		for i in self.config["owner_ids"]:
			tempUser= await self.bot.fetch_user(i)
			try:
				ownerNames.append(str(tempUser))
			except:
				ownerNames.append("Unknown")
		embed.add_field(name="Owner(s)",value=self.listStr(ownerNames))
		cogNames=[]
		for i in self.bot.cogs:
			cogNames.append(i)
		embed.add_field(name="Extension(s)",value=self.listStr(cogNames))
		links=[]
		for i in self.config["links"]:
			links.append("[{0}]({1})".format(i, self.config["links"][i]))
		embed.add_field(name="GitHub",value="\n".join(links))
		await ctx.send(None,embed=embed)

cogs=[Core]


from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import FAISS, Milvus, Pinecone, Chroma
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationalRetrievalChain
from models.llm_model import get_openai_model, get_huggingfacehub
import pinecone
import streamlit as st
from config.templates import bot_template, user_template
from config.keys import Keys
from PyPDF2 import PdfReader

def extract_text_from_PDF(files):
    # 参考官网链接：https://python.langchain.com/docs/modules/data_connection/document_loaders/pdf
    # 加载多个PDF文件
    text = ""
    for pdf in files:
        pdf_reader = PdfReader(pdf)
        for page in pdf_reader.pages:
            text += page.extract_text()
    return text

def split_content_into_chunks(text):
    # 参考官网链接：https://python.langchain.com/docs/modules/data_connection/document_transformers/text_splitters/character_text_splitter
    text_spliter = CharacterTextSplitter(separator="\n",
                                         chunk_size=500,
                                         chunk_overlap=80,
                                         length_function=len)
    chunks = text_spliter.split_text(text)
    return chunks

def save_chunks_into_vectorstore(content_chunks, embedding_model):
    # 参考官网链接：https://python.langchain.com/docs/modules/data_connection/vectorstores/
    # ① FAISS
    # pip install faiss-gpu (如果没有GPU，那么 pip install faiss-cpu)
    vectorstore = FAISS.from_texts(texts=content_chunks,
                                      embedding=embedding_model)

    # ② Pinecone
    # 官网链接：https://python.langchain.com/docs/integrations/vectorstores/pinecone
    # Pinecone官网链接：https://docs.pinecone.io/docs/quickstart
    # pip install pinecone-client==2.2.2
    # 初始化
    # pinecone.init(api_key=Keys.PINECONE_KEY, environment="asia-southeast1-gcp")
    # # 创建索引
    # index_name = "pinecone-chatbot-demo"
    # # 检查索引是否存在，如果不存在，则创建
    # if index_name not in pinecone.list_indexes():
    #     pinecone.create_index(name=index_name,
    #                           metric="cosine",
    #                           dimension=1536)
    # vectorstore = Pinecone.from_texts(texts=content_chunks,
    #                                       embedding=embedding_model,
    #                                       index_name=index_name)

    # ③ Milvus, pip install pymilvus
    # 官网链接：https://python.langchain.com/docs/integrations/vectorstores/milvus
    # vectorstore = Milvus.from_texts(texts=content_chunks,
    #                                     embedding=embedding_model,
    #                                     connection_args={"host": "localhost", "port": "19530"},
    # )

    return vectorstore

def get_chat_chain(vector_store):
    # ① 获取 LLM model
    llm = get_openai_model()
    #llm = get_huggingfacehub(model_name="google/flan-t5-xxl")

    # ② 存储历史记录
    # 参考官网链接：https://python.langchain.com/docs/use_cases/question_answering/how_to/chat_vector_db
    # 用于缓存或者保存对话历史记录的对象
    memory = ConversationBufferMemory(
        memory_key='chat_history', return_messages=True)
    # ③ 对话链
    conversation_chain = ConversationalRetrievalChain.from_llm(
        llm=llm,
        retriever=vector_store.as_retriever(),
        memory=memory
    )
    return conversation_chain

def process_user_input(user_input):
    if st.session_state.conversation is not None:
        # 调用函数st.session_state.conversation，并把用户输入的内容作为一个问题传入，返回响应。
        response = st.session_state.conversation({'question': user_input})
        # session状态是Streamlit中的一个特性，允许在用户的多个请求之间保存数据。
        st.session_state.chat_history = response['chat_history']
        # 显示聊天记录
        # chat_history : 一个包含之前聊天记录的列表
        for i, message in enumerate(st.session_state.chat_history):
            # 用户输入
            if i % 2 == 0:
                st.write(user_template.replace(
                    "{{MSG}}", message.content), unsafe_allow_html=True) # unsafe_allow_html=True表示允许HTML内容被渲染
            else:
                # 机器人响应
                st.write(bot_template.replace(
                    "{{MSG}}", message.content), unsafe_allow_html=True)


import glob
from importlib import import_module
from os.path import dirname, basename, isfile, join

class ModuleLoader:
    def __init__(self, file, namespaceGlobals):
        self.file = file
        self.globals = namespaceGlobals
        self.namespace = None
        self.all = []
        self.__loadDirectory()

    ##
    # returns array of module names
    # found within the current module directory.
    def Modules(self):
        if len(self.all) <= 0:
            self.__loadModules()
        return self.all

    ##
    # loads classes from all of the current directory modules
    # set global variabes for the passed in file for the loaded classes
    # and returns an array of class names.
    # this only loads and sets classes where the class name is the same as the module name.
    def Classes(self):
        if len(self.all) <= 0:
            self.__loadClasses()
        return self.all

    ##
    # returns the app namespace for the current module directory.
    def getAppModuleNamespace(self):
        if self.namespace is None:
            index = self.directory.find('/app/')
            if index >= 0:
                self.namespace = self.directory[index+1::].replace('/','.')
        return self.namespace

    ##
    # set the member variables:
    #  directory, files, modules
    def __loadDirectory(self):
        self.directory = dirname(self.file)
        self.files = glob.glob(join(self.directory, "*.py"))
        self.modules = [ basename(f)[:-3] for f in self.files if isfile(f) and not f.endswith('__init__.py')]

    ##
    # sets the values intended for the __all__ container
    # with all module names found  within the current directory
    def __loadModules(self):
        self.all = [ m for m in self.modules ]

    ##
    # sets the values intended for the __all__ container
    # with all module-class names found  within the current directory
    def __loadClasses(self):
        namespace = self.getAppModuleNamespace()
        for className in self.modules:
            namespaceModule = f"{namespace}.{className}"
            module = import_module(namespaceModule)

            middleware = getattr(module,className)

            self.globals[className] = middleware
            self.all.extend([className])


# boj_숨바꼭질

# 예제코드만
# 역추적도 가능함 연습해보기
import heapq

n, m = map(int, input().split())
s = int(input())
v = [[] for i in range(n + 1)]
dist = [1000000000 for i in range(n + 1)]

for i in range(m):
    a, b, c = map(int, input().split())

    v[a].append([b, c])

pq = []

heapq.heappush(pq, [0, s])
dist[s] = 0
while len(pq) > 0:
    cur = pq[0][1]
    d = pq[0][0]
    heapq.heappop(pq)

    if dist[cur] != d:
        continue

    for i in v[cur]:
        nxt = i[0]
        nd = d + i[1]

        if dist[nxt] <= nd:
            continue

        dist[nxt] = nd
        heapq.heappush(pq, [nd, nxt])

for i in range(1, n + 1):
    if dist[i] == 1000000000:
        print("INF")
    else:
        print(dist[i])


import logging
import pytest
import copy
import os
import ldap
from lib389._constants import *
from lib389.topologies import topology_st as topo

from lib389.replica import Replicas
from lib389.agreement import Agreements
from lib389.utils import ds_is_older

pytestmark = pytest.mark.tier1

DEBUGGING = os.getenv("DEBUGGING", default=False)
if DEBUGGING:
    logging.getLogger(__name__).setLevel(logging.DEBUG)
else:
    logging.getLogger(__name__).setLevel(logging.INFO)
log = logging.getLogger(__name__)

notnum = 'invalid'
too_big = '9223372036854775807'
overflow = '9999999999999999999999999999999999999999999999999999999999999999999'

replica_dict = {'nsDS5ReplicaRoot': 'dc=example,dc=com',
                'nsDS5ReplicaType': '3',
                'nsDS5Flags': '1',
                'nsDS5ReplicaId': '65534',
                'nsds5ReplicaPurgeDelay': '604800',
                'nsDS5ReplicaBindDN': 'cn=u',
                'cn': 'replica'}

agmt_dict = {'cn': 'test_agreement',
             'nsDS5ReplicaRoot': 'dc=example,dc=com',
             'nsDS5ReplicaHost': 'localhost.localdomain',
             'nsDS5ReplicaPort': '5555',
             'nsDS5ReplicaBindDN': 'uid=tester',
             'nsds5ReplicaCredentials': 'password',
             'nsDS5ReplicaTransportInfo': 'LDAP',
             'nsDS5ReplicaBindMethod': 'SIMPLE'}

repl_add_attrs = [('nsDS5ReplicaType', '-1', '4', overflow, notnum, '1'),
                  ('nsDS5Flags', '-1', '2', overflow, notnum, '1'),
                  ('nsDS5ReplicaId', '0', '65536', overflow, notnum, '1'),
                  ('nsds5ReplicaPurgeDelay', '-2', too_big, overflow, notnum, '1'),
                  ('nsDS5ReplicaBindDnGroupCheckInterval', '-2', too_big, overflow, notnum, '1'),
                  ('nsds5ReplicaTombstonePurgeInterval', '-2', too_big, overflow, notnum, '1'),
                  ('nsds5ReplicaProtocolTimeout', '-1', too_big, overflow, notnum, '1'),
                  ('nsds5ReplicaReleaseTimeout', '-1', too_big, overflow, notnum, '1'),
                  ('nsds5ReplicaBackoffMin', '0', too_big, overflow, notnum, '3'),
                  ('nsds5ReplicaBackoffMax', '0', too_big, overflow, notnum, '6'),
                  ('nsds5ReplicaKeepAliveUpdateInterval', '59', too_big, overflow, notnum, '60'),]

repl_mod_attrs = [('nsDS5Flags', '-1', '2', overflow, notnum, '1'),
                  ('nsds5ReplicaPurgeDelay', '-2', too_big, overflow, notnum, '1'),
                  ('nsDS5ReplicaBindDnGroupCheckInterval', '-2', too_big, overflow, notnum, '1'),
                  ('nsds5ReplicaTombstonePurgeInterval', '-2', too_big, overflow, notnum, '1'),
                  ('nsds5ReplicaProtocolTimeout', '-1', too_big, overflow, notnum, '1'),
                  ('nsds5ReplicaReleaseTimeout', '-1', too_big, overflow, notnum, '1'),
                  ('nsds5ReplicaBackoffMin', '0', too_big, overflow, notnum, '3'),
                  ('nsds5ReplicaBackoffMax', '0', too_big, overflow, notnum, '6'),
                  ('nsds5ReplicaKeepAliveUpdateInterval', '59', too_big, overflow, notnum, '60'),]

agmt_attrs = [
              ('nsds5ReplicaPort', '0', '65535', overflow, notnum, '389'),
              ('nsds5ReplicaTimeout', '-1', too_big, overflow, notnum, '6'),
              ('nsds5ReplicaBusyWaitTime', '-1', too_big, overflow, notnum, '6'),
              ('nsds5ReplicaSessionPauseTime', '-1', too_big, overflow, notnum, '6'),
              ('nsds5ReplicaFlowControlWindow', '-1', too_big, overflow, notnum, '6'),
              ('nsds5ReplicaFlowControlPause', '-1', too_big, overflow, notnum, '6'),
              ('nsds5ReplicaProtocolTimeout', '-1', too_big, overflow, notnum, '6')
              ]

def replica_reset(topo):
    """Purge all existing replica details"""
    replicas = Replicas(topo.standalone)
    for r in replicas.list():
        r.delete()

def replica_setup(topo):
    """Add a valid replica config entry to modify
    """
    replicas = Replicas(topo.standalone)
    for r in replicas.list():
        r.delete()
    return replicas.create(properties=replica_dict)

def agmt_reset(topo):
    """Purge all existing agreements for testing"""
    agmts = Agreements(topo.standalone)
    for a in agmts.list():
        a.delete()

def agmt_setup(topo):
    """Add a valid replica config entry to modify
    """
    # Reset the agreements too.
    replica = replica_setup(topo)
    agmts = Agreements(topo.standalone, basedn=replica.dn)
    for a in agmts.list():
        a.delete()
    return agmts.create(properties=agmt_dict)

def perform_invalid_create(many, properties, attr, value):
    my_properties = copy.deepcopy(properties)
    my_properties[attr] = value
    with pytest.raises(ldap.LDAPError) as ei:
        many.create(properties=my_properties)
    return ei.value

def perform_invalid_modify(o, attr, value):
    with pytest.raises(ldap.LDAPError) as ei:
        o.replace(attr, value)
    return ei.value

@pytest.mark.parametrize("attr, too_small, too_big, overflow, notnum, valid", repl_add_attrs)
def test_replica_num_add(topo, attr, too_small, too_big, overflow, notnum, valid):
    """Test all the number values you can set for a replica config entry

    :id: a8b47d4a-a089-4d70-8070-e6181209bf92
    :parametrized: yes
    :setup: standalone instance
    :steps:
        1. Use a value that is too small
        2. Use a value that is too big
        3. Use a value that overflows the int
        4. Use a value with character value (not a number)
        5. Use a valid value
    :expectedresults:
        1. Add is rejected
        2. Add is rejected
        3. Add is rejected
        4. Add is rejected
        5. Add is allowed
    """
    replica_reset(topo)

    replicas = Replicas(topo.standalone)

    # Test too small
    perform_invalid_create(replicas, replica_dict, attr, too_small)
    # Test too big
    perform_invalid_create(replicas, replica_dict, attr, too_big)
    # Test overflow
    perform_invalid_create(replicas, replica_dict, attr, overflow)
    # test not a number
    perform_invalid_create(replicas, replica_dict, attr, notnum)
    # Test valid value
    my_replica = copy.deepcopy(replica_dict)
    my_replica[attr] = valid
    replicas.create(properties=my_replica)

@pytest.mark.parametrize("attr, too_small, too_big, overflow, notnum, valid", repl_mod_attrs)
def test_replica_num_modify(topo, attr, too_small, too_big, overflow, notnum, valid):
    """Test all the number values you can set for a replica config entry

    :id: a8b47d4a-a089-4d70-8070-e6181209bf93
    :parametrized: yes
    :setup: standalone instance
    :steps:
        1. Replace a value that is too small
        2. Repalce a value that is too big
        3. Replace a value that overflows the int
        4. Replace a value with character value (not a number)
        5. Replace a vlue with a valid value
    :expectedresults:
        1. Value is rejected
        2. Value is rejected
        3. Value is rejected
        4. Value is rejected
        5. Value is allowed
    """
    replica = replica_setup(topo)

    # Value too small
    perform_invalid_modify(replica, attr, too_small)
    # Value too big
    perform_invalid_modify(replica, attr, too_big)
    # Value overflow
    perform_invalid_modify(replica, attr, overflow)
    # Value not a number
    perform_invalid_modify(replica, attr, notnum)
    # Value is valid
    replica.replace(attr, valid)

@pytest.mark.xfail(reason="Agreement validation current does not work.")
@pytest.mark.parametrize("attr, too_small, too_big, overflow, notnum, valid", agmt_attrs)
def test_agmt_num_add(topo, attr, too_small, too_big, overflow, notnum, valid):
    """Test all the number values you can set for a replica config entry

    :id: a8b47d4a-a089-4d70-8070-e6181209bf94
    :parametrized: yes
    :setup: standalone instance
    :steps:
        1. Use a value that is too small
        2. Use a value that is too big
        3. Use a value that overflows the int
        4. Use a value with character value (not a number)
        5. Use a valid value
    :expectedresults:
        1. Add is rejected
        2. Add is rejected
        3. Add is rejected
        4. Add is rejected
        5. Add is allowed
    """

    agmt_reset(topo)
    replica = replica_setup(topo)

    agmts = Agreements(topo.standalone, basedn=replica.dn)

    # Test too small
    perform_invalid_create(agmts, agmt_dict, attr, too_small)
    # Test too big
    perform_invalid_create(agmts, agmt_dict, attr, too_big)
    # Test overflow
    perform_invalid_create(agmts, agmt_dict, attr, overflow)
    # test not a number
    perform_invalid_create(agmts, agmt_dict, attr, notnum)
    # Test valid value
    my_agmt = copy.deepcopy(agmt_dict)
    my_agmt[attr] = valid
    agmts.create(properties=my_agmt)

@pytest.mark.xfail(reason="Agreement validation current does not work.")
@pytest.mark.parametrize("attr, too_small, too_big, overflow, notnum, valid", agmt_attrs)
def test_agmt_num_modify(topo, attr, too_small, too_big, overflow, notnum, valid):
    """Test all the number values you can set for a replica config entry

    :id: a8b47d4a-a089-4d70-8070-e6181209bf95
    :parametrized: yes
    :setup: standalone instance
    :steps:
        1. Replace a value that is too small
        2. Replace a value that is too big
        3. Replace a value that overflows the int
        4. Replace a value with character value (not a number)
        5. Replace a vlue with a valid value
    :expectedresults:
        1. Value is rejected
        2. Value is rejected
        3. Value is rejected
        4. Value is rejected
        5. Value is allowed
    """

    agmt = agmt_setup(topo)

    # Value too small
    perform_invalid_modify(agmt, attr, too_small)
    # Value too big
    perform_invalid_modify(agmt, attr, too_big)
    # Value overflow
    perform_invalid_modify(agmt, attr, overflow)
    # Value not a number
    perform_invalid_modify(agmt, attr, notnum)
    # Value is valid
    agmt.replace(attr, valid)

@pytest.mark.skipif(ds_is_older('1.4.1.4'), reason="Not implemented")
@pytest.mark.bz1546739
def test_same_attr_yields_same_return_code(topo):
    """Test that various operations with same incorrect attribute value yield same return code

    :id: 4bae88d7-0da8-4a71-b062-9d0ff4e472cf
    :setup: standalone instance
    :steps:
        1. Purge all replica details
        2. Perform an invalid create operation
        3. Setup replica
        4. Perform an invalid modify operation
    :expectedresults:
        1. Success
        2. Value is rejected
        3. Success
        4. Value is rejected
    """
    attr = 'nsDS5ReplicaId'

    replica_reset(topo)
    replicas = Replicas(topo.standalone)
    e = perform_invalid_create(replicas, replica_dict, attr, too_big)
    assert type(e) is ldap.UNWILLING_TO_PERFORM

    replica = replica_setup(topo)
    e = perform_invalid_modify(replica, attr, too_big)
    assert type(e) is ldap.UNWILLING_TO_PERFORM

if __name__ == '__main__':
    # Run isolated
    # -s for DEBUG mode
    CURRENT_FILE = os.path.realpath(__file__)
    pytest.main(["-s", CURRENT_FILE])


import os
import pandas as pd
from pandas import DataFrame
import pickle

os.chdir('C:/Users/user/Desktop/social_media')

#데이터 전처리(매크로, 뉴스 등 의미없는 계정 삭제)
tweet_df = pd.read_csv('twitter_data.csv',encoding='utf-8')
del_names=pd.read_table('del_names.txt',header=None)
#총 255개의 계정 삭제

#의미없는 계정 삭제
del_idx=[]
for i in del_names:
    for j in enumerate(list(tweet_df.username)):
        if i==j[1]:
            del_idx.append(j[0])

complete_df=tweet_df[['author_id','username','text']]
complete_df=complete_df.drop(del_idx)
#6010개의 트윗만 남김(author_id, username, text)

DataFrame.to_csv(complete_df, 'complete_df.csv', encoding='utf-8-sig',index=False)
complete_df=pd.read_csv('complete_df.csv')

#%%text tokenize
from nltk.tokenize import word_tokenize
from nltk.tag import pos_tag
from konlpy.tag import Komoran

komoran=Komoran()

noun=[]
for i in list(complete_df.text):
    noun.append(komoran.nouns(i))

complete_df['noun']=noun
#토큰화 및 명사 추출

token_df=complete_df

del token_df['text']
#R로 읽을 때 text열로 인한 에러 발생 -> 제거하고 진행

DataFrame.to_csv(complete_df, 'token_df.csv', encoding='utf-8-sig',index=False)
complete_df=pd.read_csv('token_df.csv')


import time, sys, math, random, glob
import os.path

data_directory    = "data"

#data_directory    = os.path.join(args.data_root, args.data_folder)

def get_random_training_filename_pair():
    input_voice_mask = random.randint(1, 14)
    free_voices = [];
    for i in range(4):
        if (input_voice_mask & (1 << i)) == 0:
            free_voices.append(i)

    output_voice_mask = 1 << (free_voices[random.randint(0, len(free_voices)-1)]);

    print("Audio: {} MIDI{}".format(bin(input_voice_mask), bin(output_voice_mask)))
    wavs = glob.glob("dataset/audio/*_{}.wav".format(input_voice_mask));
    wav =  wavs[random.randint(0, len(wavs)-1)];
    print(wav);

    mid = os.path.basename(wav)
    mid = mid.replace("_{}.wav".format(input_voice_mask), "_{}.mid".format(output_voice_mask))
    mid = os.path.join("dataset/MIDI", mid)
    print(mid);

get_random_training_filename_pair()


import matplotlib.pyplot as plt
import matplotlib as mpl
import networkx as nx
import os
import pickle as pk
from functions import *

# %%
getcontext().prec = 200
mpl.style.use('seaborn-deep')
mpl.rcParams['text.usetex'] = True
mpl.rcParams['font.family'] = 'serif'
mpl.rcParams['text.latex.preamble'] = r'\usepackage{bm}'

# %% Figure and Data paths
FIG_PATH = 'figs/'
if not os.path.isdir(FIG_PATH):
    os.makedirs(FIG_PATH)

DATA_PATH = 'data/'
if not os.path.isdir(DATA_PATH):
    os.makedirs(DATA_PATH)

# %% Setup
N = 10
M = 3
np.random.seed(0)

#%% Graph
G = np.random.choice([0.0, 1.0], size=(N, N), p=[0.5, 0.5])
G = G + np.eye(N)
G = (G > 0) * 1.0

A = np.zeros((N,N))
A = G / G.sum(axis=0)

# %%Check that A^i converges
print('A is primitive: ', np.all(np.isclose(np.linalg.matrix_power(A, 100),
                                            np.linalg.matrix_power(A, 101))))

# %% Hypotheses
theta = np.array([1., 2., 3.]) * 0.6
var = 1
x = np.linspace(-10, 10, 1000)
dt = (max(x) - min(x)) / len(x)

#%% Initialization
np.random.seed(0)
mu_0 = np.random.rand(N,M)
mu_0 = mu_0 / np.sum(mu_0, axis = 1)[:, None]

#%% Monte Carlo runs
delta = 0.2
N_MC = 10000
N_ITER = 100

MU_mc1 = []
for i in range(N_MC):
    csi = []
    for l in range(0, N):
        csi.append(theta[0] + np.sqrt(var) * np.random.randn(N_ITER))
    csi = np.array(csi)
    if i % 10000 == 0:
        print(i)
    m_aux = np.array(asl(mu_0, csi, A, N_ITER, theta, var, delta, is_gaussian=True))
    MU_mc1.append((np.argmax(m_aux, axis=2) != 0) * 1)

#%% Compute prob. of error for agents 1 and 10
p1 = sum(MU_mc1)[:,0] / N_MC
p9 = sum(MU_mc1)[:,9] / N_MC

# %% Save data
# pk.dump((p1, p9), open( DATA_PATH + "p_error.p", "wb" ) )
# p1, p9 = pk.load( open( DATA_PATH + "p_error.p", "rb" ) )

# %% Plot prob. of error
p1[0] = 1.0
p9[0] = 1.0
f, ax = plt.subplots(1, 1, figsize=(6,3))
ax.plot(p1, color='C5', linewidth=2)
ax.plot(p9, color='C0', linewidth=2)
ax.tick_params(axis='both', which='major', labelsize=18)
ax.set_yscale('log')
ax.set_xlim([0, len(p1) - 1])
ax.set_ylim([3e-4,2])
ax.set_xlabel(r'$i$', fontsize=18)
ax.set_ylabel(r'$p_{k,i}^{(\delta)}$', fontsize=18)
ax.legend(['Agent 1', 'Agent 10'], fontsize=18, ncol=2)
ax.annotate(r'$p^{(\delta)}_{1}=7.7 \times 10^{-3}$', (.65, p1[-1]-0.0008),
            xycoords=('axes fraction', 'data'), color='k', fontsize=16, va='center')
ax.annotate(r'$p^{(\delta)}_{10}=1.8 \times 10^{-3}$', (.65, p9[-1]+0.01),
            xycoords=('axes fraction', 'data'), color='k', fontsize=16, va='center')
f.savefig(FIG_PATH + '3.pdf', bbox_inches='tight')


import requests
from bs4 import BeautifulSoup

url = 'http://www.ibm.com'
data = requests.get(url).text
soup = BeautifulSoup(data, 'html.parser')

for link in soup.find_all('a', href= True):
    print(link.get('href'))

for link in soup.find_all('img'):
    print(link)
    print(link.get('src'))


'''https://www.codewars.com/kata/550f22f4d758534c1100025a/train/python

'''

# directions = { "NORTH":"SOUTH" ,  "EAST":"WEST" }
# directions_flipped = {directions[x] : x for x in directions.keys()}
# directions = {**directions, **directions_flipped}
# print(directions)
directions_pair = {'NORTH': 'SOUTH', 'EAST': 'WEST', 'SOUTH': 'NORTH', 'WEST': 'EAST'}

opposite = {'NORTH': 'SOUTH', 'EAST': 'WEST', 'SOUTH': 'NORTH', 'WEST': 'EAST'}

def dirReduc(plan):
    new_plan = []
    for d in plan:
        if new_plan and new_plan[-1] == opposite[d]:
            new_plan.pop()
        else:
            new_plan.append(d)
    return new_plan

# def dirReduc(arr):
#     processing = True
#     up_to = 0
#     processing_list = list(enumerate(arr))
#     while processing:
#         remaining = len(arr) - up_to

#         for x,y in processing_list[up_to:]:
#             previous = x - 1
#             if x <= up_to and remaining != 1: # if at the start and length isnt 0
#                 continue
#             elif processing_list[previous][1] == directions_pair[y]:  # if last entry matches current oposite
#                 del arr[ previous : previous + 2]
#                 up_to = x-1
#                 break
#             elif x == len(arr)-1:
#                 processing = False
#     return arr

a = ["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]
# cProfile.run('dirReduc(a)')
print(dirReduc(a), ['WEST'])
u=["NORTH", "WEST", "SOUTH", "EAST"]
print(dirReduc(u), ["NORTH", "WEST", "SOUTH", "EAST"])

# import cProfile
# import random
# directions = ['NORTH','SOUTH','EAST','WEST']
# c = [random.choice(directions) for x in range(1000)]
# cProfile.run('dirReduc(c)')


import random
import numpy as np

import torch

from hyper_dti.settings import constants

def get_collate(mode, split):
    """ Wrapper for different collate functions. """
    if mode == 'pairs':
        return collate_triplets
    elif mode == 'drug':
        return collate_drug
    elif split == 'train':
        return collate_target
    else:
        return collate_target_eval

def collate_triplets(elem_dicts):
    """ Data loading for individual drug-target-interaction triplets. """
    batch = {'pids':  [], 'targets': torch.Tensor(), 'mids': [], 'drugs': torch.Tensor()}
    labels = torch.Tensor()

    for elem_dict in elem_dicts:
        label = torch.tensor(elem_dict['label']).unsqueeze(0)
        labels = label if len(labels) == 0 else torch.cat((labels, label), 0)
        batch['mids'].append(elem_dict['mid'])
        drug = torch.tensor(elem_dict['drug']).float().unsqueeze(0)
        batch['drugs'] = drug if len(batch['drugs']) == 0 else torch.cat((batch['drugs'], drug), 0)
        batch['pids'].append(elem_dict['pid'])
        target = torch.tensor(elem_dict['target']).float().unsqueeze(0)
        batch['targets'] = target if len(batch['targets']) == 0 else torch.cat((batch['targets'], target), 0)

    return batch, labels

def collate_target_single(elem_dicts):
    elem_dict = elem_dicts[0]
    labels = torch.tensor(elem_dict['label'])

    batch = {}
    batch['mids'] = [elem_dict['mid']]
    batch['drugs'] = torch.tensor(elem_dict['drug']).float()
    batch['pids'] = [elem_dict['pid']]
    batch['targets'] = torch.tensor(elem_dict['target']).float()

    return batch, labels

def collate_drug(elem_dicts):
    """ Data loading for interactions based on drug compound. """
    batch = {'pids': [], 'targets': None, 'mids': [], 'drugs': []}
    labels = torch.Tensor()

    for elem_dict in elem_dicts:
        batch_labels = torch.tensor(elem_dict['label'])
        labels = torch.cat((labels, batch_labels), 0)
        batch['mids'].append(elem_dict['mid'])
        drugs = torch.tensor(elem_dict['drugs']).float().unsqueeze(0)
        batch['drugs'] = drugs if len(batch['drugs']) == 0 else torch.cat((batch['drugs'], drugs), 0)
        batch['pids'].append(elem_dict['pid'])

    return batch, labels

def collate_target_eval(elem_dicts):
    """ Data loading for interactions based on protein target, without over- or under-sampling. """
    return collate_target(elem_dicts, eval=True)

def collate_target(elem_dicts, eval=False):
    """ Data loading for interactions based on protein target. """
    batch = {'pids': [], 'targets': torch.Tensor(), 'mids': [], 'drugs': []}
    labels = torch.Tensor()

    for elem_dict in elem_dicts:
        labeled_pairs = list(zip(elem_dict['mid'], elem_dict['drug'], elem_dict['label']))
        # Oversample for low frequency or shuffle otherwise
        if not eval and len(labeled_pairs) < constants.OVERSAMPLING:
            labeled_pairs = random.choices(labeled_pairs, k=constants.OVERSAMPLING)
        elif not eval and constants.MAIN_BATCH_SIZE != -1:
            if len(labeled_pairs) < constants.MAIN_BATCH_SIZE:
                random.shuffle(labeled_pairs)
            else:
                labeled_pairs = random.sample(labeled_pairs, k=constants.MAIN_BATCH_SIZE)
        else:
            random.shuffle(labeled_pairs)
        elem_dict['mid'], elem_dict['drug'], elem_dict['label'] = zip(*labeled_pairs)

        batch_labels = torch.tensor(elem_dict['label'])
        labels = torch.cat((labels, batch_labels), 0)

        batch['mids'].append(elem_dict['mid'])
        batch['drugs'].append(torch.tensor(np.stack(elem_dict['drug'], axis=0)).float())

        batch['pids'].append([elem_dict['pid'] for _ in range(len(elem_dict['mid']))])
        target = torch.tensor(elem_dict['target']).float().unsqueeze(0)
        batch['targets'] = target if len(batch['targets']) == 0 else torch.cat((batch['targets'], target), 0)

    return batch, labels


'''
Setup for the fast-align algorithm for pairwise sequence alignment and
sequence distance calculation.
'''

# Always prefer setuptools over distutils
from setuptools import setup, find_packages
# To use a consistent encoding
from codecs import open
from os import path

here = path.abspath(path.dirname(__file__))

# Get the long description from the README file
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
    long_description = f.read()

setup(
    name='fast-align',

    # Versions should comply with PEP440.  For a discussion on
	# single-sourcing
    # the version across setup.py and the project code, see
    # https://packaging.python.org/en/latest/single_source_version.html
    version='0.0.0',

    description='Fast pairwise string alignment using breadth-first-search',
    long_description=long_description,

    # The project's main homepage.
    url='https://github.com/enewe101/fast-align',

    # Author details
    author='Edward Newell',
    author_email='edward.newell@gmail.com',

    # Choose your license
    license='MIT',

    # See https://pypi.python.org/pypi?%3Aaction=list_classifiers
    classifiers=[
        # How mature is this project? Common values are
        #   3 - Alpha
        #   4 - Beta
        #   5 - Production/Stable
        'Development Status :: 3 - Alpha',

        # Indicate who your project is intended for
        'Intended Audience :: Developers',
        'Topic :: Software Development :: Build Tools',

        # Pick your license as you wish (should match "license" above)
        'License :: OSI Approved :: MIT License',

        # Specify the Python versions you support here.
        'Programming Language :: Python :: 2.7',
    ],

    # What does your project relate to?
    keywords= 'pairwise sequence alignment string matching distance',

    # You can just specify the packages manually here if your project is
    # simple. Or you can use find_packages().
    packages=['fast_align'],
    install_requires=[]
)


'''
Created by: Matthew Smith, Michael Rodriguez, and Darryl Balderas
Programmed in: Python 2.7
Purpose: This module was created to utilize and organize the
functionality needt to access the battery
'''
import RPi.GPIO as GPIO
import time
import numpy as np
import Adafruit_ADS1x15 #library for ADC hardware

class Battery:
    def __init__(self):
        self.voltage_level = 13.0
        self.adc = Adafruit_ADS1x15.ADS1015()
        self.GAIN = 2
        self.adc_pin = 1
        self.max_voltage_reading = 14

    def get_adc_voltage(self, voltage_level):
        voltage_reading = 13.0 #default voltage level
        upper_bound = 1367
        lower_bound = 0
        if voltage_level < lower_bound:
            voltage_level = lower_bound
        elif voltage_level > upper_bound:
            voltage_level = upper_bound
        total_boundary = upper_bound - (lower_bound)
        tolerance = round((lower_bound/float(total_boundary)) * self.max_voltage_reading, 2)
        voltage_reading = round((voltage_level/float(total_boundary)) * self.max_voltage_reading, 2) - tolerance
        return voltage_reading

    def get_voltage_level(self):
        voltage_level = 0
        value = self.adc.read_adc(self.adc_pin, gain=self.GAIN)
        time.sleep(0.25)
        voltage_level = np.median(value)
        self.voltage_level = self.get_adc_voltage(voltage_level)
        return self.voltage_level