"""the errors of the two systems are compared"""

with open("../../iob_outputs_from_html/emBERT_eval/result.conllup", "r", encoding="utf-8") as f:
    lines = f.readlines()

error = 0
sent = ""
diff = list()
for i in range(len(lines)):
    if i == 0:
        continue
    if lines[i] != "\n":
        if lines[i + 1].split("\t")[0] in [",", ".", "!", ":", "?", "(", ")", "_", "-", "'", '"', "/", "=", "%"]:
            sent += lines[i].split("\t")[0]
        elif lines[i].split("\t")[0] in ["-"]:
            sent += lines[i].split("\t")[0]
        else:
            sent += lines[i].split("\t")[0] + " "
        word = lines[i].split("\t")[0]
        gold = lines[i].split("\t")[1]
        spacy = lines[i].split("\t")[2]
        bert = lines[i].split("\t")[3][:-1]
        if gold == "O" and spacy == "O" and bert == "O":
            continue
        else:
        # gold != "O" and spacy != "O" and bert != "O":
            word_end_pos = len(sent) - 1
            word_start_pos = len(sent) - len(word) - 1
            diff.append(word + "\t" + gold + "\t" + spacy + "\t" + bert + "\t" + str(word_start_pos) + "\t" + str(word_end_pos))
            error += 1
    else:
        if error != 0:
            with open("../../iob_outputs_from_html/emBERT_eval/diff_v5.txt", "a", encoding="utf-8") as f:
                f.write(sent + "\n")
                for i in range(len(diff)):
                    f.write(diff[i] + "\n")
                f.write("\n")
        error = 0
        sent = ""
        diff = list()


import torch
import shutil

import torch.optim as optim
import numpy as np

from torch import nn
from torch.utils.data import DataLoader
from torchvision import transforms
from utils import AverageMeter, min_max_normalize, save_checkpoint
from dataset import DAEDataset
from model import ModelRec

transform = transforms.Compose([transforms.Resize((300, 400)),
                                transforms.ToTensor(),
                                transforms.Lambda(lambda tensor: min_max_normalize(tensor))])

#working directory
root = '/content'

train_dataset = DAEDataset(root, mode='train', transform=transform)
val_dataset = DAEDataset(root, mode='val', transform=transform)

train_loader = DataLoader(dataset=train_dataset, batch_size=16, shuffle=True)
val_loader = DataLoader(dataset=val_dataset, batch_size=2, shuffle=True)

def train(train_loader, val_loader, model, optimizer, scheduler, criterion, num_epochs):

  best_loss = 1

  for epoch in range(num_epochs):
    scheduler.step()

    train_loss = AverageMeter()
    val_loss = AverageMeter()

    model.train()
    for input, target in train_loader:
      input = input.cuda()
      target = target.cuda()

      optimizer.zero_grad()
      output = model(input)

      loss = criterion(output, target)
      train_loss.up_date(loss, input.size(0))

      loss.backward()
      optimizer.step()

    print('Epoch {} of {}, Train_loss: {:.3f}'.format(epoch + 1, num_epochs, train_loss.avg))

    model.eval()
    for input, target in val_loader:
      input = input.cuda()
      target = target.cuda()

      output = model(input)

      loss = criterion(output, target)
      val_loss.up_date(loss, input.size(0))

    is_best = val_loss.avg < best_loss
    best_loss = min(val_loss.avg, best_loss)
    save_checkpoint({
        'epoch': epoch + 1,
        'arch': experiment,
        'state_dict': model.state_dict(),
        'best_prec1': best_loss,
        'optimizer': optimizer.state_dict(),
    }, is_best)

    print('Epoch {} of {}, Val_loss: {:.3f}'.format(epoch + 1, num_epochs, val_loss.avg))

def main():
  torch.cuda.empty_cache()

  model = ModelRec().cuda()
  criterion = nn.BCELoss().cuda()
  optimizer = optim.Adam(model.parameters())
  scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size = 50,   gamma=0.1)
  experiment = "DAE_Conv"
  num_epochs = 200

  #checkpoint = torch.load('/content/checkpoint.pth.tar')
  #model.load_state_dict(checkpoint['state_dict'])

  train(train_loader, val_loader, model, optimizer, scheduler, criterion, num_epochs)

if __name__ == '__main__':
    main()


import matplotlib.pyplot as plt
import cv2

import time
start_time = time.time()

# Titik awal
x1 = 1
y1 = 7

# Titik Akhir
x2 = 7
y2 = 15

dy = y2-y1
dx = x2-x1

step = dx  if dx > dy  else dy

xInc = dx / step
yInc = dy / step

x=x1
y=y1

len=y2+x2

# x axis value list.
x_number_list = []

x_number_list.append(x1)

# y axis value list.
y_number_list = []

y_number_list.append(y1)

for i in range(1,len):

    x = x + xInc
    y = y + yInc

    xBulat=int(x)
    yBulat=int(y)

    x_number_list.append(xBulat)
    y_number_list.append(yBulat)

    if(x>=x2 and y>=y2):
        break

end_time = time.time()
delta_time = end_time-start_time
print("Execution Time : ",delta_time," ms")

from guppy import hpy

h = hpy()

print (h.heap())

# Draw point based on above x, y axis values.
plt.scatter(x_number_list, y_number_list, s=10)

# Set chart title.
plt.title("Algoritma DDA")

# Set x, y label text.
plt.xlabel("X")
plt.ylabel("Y")
plt.show()


import numpy as np
import cvxpy as cp

import matplotlib.pyplot as plt

# double integrator
# state is [x, vx, y, vy]

T = 0.1 # timestep in seconds
A = np.kron(np.eye(2),np.array([[1, T], [0, 1]]))
B = np.kron(np.eye(2), np.array([[0.5*T**2], [T]]))

class MPC:
  def __init__(self, prediction_horizon = 10, number_of_agents = 1, umax=100.0, obstacles=[]):

    self.N = prediction_horizon
    self.num_agents = number_of_agents
    self.obstacles = obstacles
    self.umax = umax

    self.solution_X = None
    self.solution_U = None # updated once solve is called

    return

  def plot(self):
    if self.solution_X is None:
      print("Please call solve first")
      return

    # plot the trajectories
    plt.figure()
    for i in range(self.num_agents):
      xs = [x[0] for x in self.solution_X[i]]
      ys = [x[2] for x in self.solution_X[i]]
      plt.plot(xs, ys, label=f"Agent {i}")
      plt.scatter([xs[0]], [ys[0]], marker="o", label=None)
      plt.scatter([xs[-1]], [ys[-1]], marker="x", label=None)

    plt.grid()
    plt.xlabel("x [m]")
    plt.ylabel("y [m]")
    plt.legend()

    # plt.show()
    plt.savefig("plot.png")

  def solve(self, x0, xT):

    # x0 is a list of initial conditions
    # xT is a list of target states

    ### constuct the MPC problem

    X = [[cp.Variable(4) for _ in range(self.N)]  for i in range(self.num_agents)]
    # X[i][k] is the i-th agents state at time k
    U = [[cp.Variable(2) for _ in range(self.N-1)] for i in range(self.num_agents)]

    ### create constraints
    constraints = []

    # initial and final conditions
    for i in range(self.num_agents):
      constraints.append(X[i][0] == x0[i])
      constraints.append(X[i][-1] == xT[i])

    # dynamics constraints
    for i in range(self.num_agents):
      for k in range(self.N-1): # at each timestep
        constraints.append(X[i][k+1] == A @ X[i][k] + B @ U[i][k])

    # input constraints
    for i in range(self.num_agents):
      for k in range(self.N-1):
        constraints.append(cp.norm(U[i][k], "inf") <= self.umax)

    ### construct the objective function

    objective = sum(sum(cp.sum_squares(uk) for uk in Ui) for Ui in U)

    ###  call a solver
    prob = cp.Problem(cp.Minimize(objective), constraints)
    prob.solve()

    ### save the trajectory
    self.solution_X = [[x.value for x in Xi] for Xi in X]
    self.solution_U = [[u.value for u in Ui] for Ui in U]

    ### return instantaneous control input
    return [u[0] for u in self.solution_U]

mpc = MPC(number_of_agents=2)

x0 = [np.array([0,0,0,0]), np.array([1,0,0,0])]
xT = [np.array([1,0,1,0]), np.array([0,0,1,0])]
mpc.solve(x0, xT)

mpc.plot()


import sys
import os.path
import re
import warnings
from io import StringIO

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import statsmodels.api as sm
from patsy import dmatrices
import six

class PerfData():
    __DATETIME_HEADER__ = "start-time"
    __PERF_HEADER__ = __DATETIME_HEADER__ + ",response-time"

    def __init__(self, filename):
        self._filename = filename

    def data(self):
        with open(self._filename) as f:
            _data = f.read()
        return _data

    def headers(self):
        return self.__PERF_HEADER__

    def datetime_headers(self):
        return self.__DATETIME_HEADER__

class PerformanceRunIterator():
    def __init__(self, data, header):
        self._data = data
        self._current_index = 0
        self._perf_header = header

    def __iter__(self):
        self._header_indexes = [m.start() for m in re.finditer(self._perf_header, self._data)]
        self._header_indexes.append(len(self._data))

        return self

    def next(self):
        if self._current_index + 1 >= len(self._header_indexes):
            raise StopIteration
        line = self._line_at_index(self._current_index)
        self._current_index = self._current_index + 1

        return line

    def _line_at_index(self, position):
        start = self._header_indexes[position]
        end = self._header_indexes[position + 1]
        line = self._data[start:end]
        return six.text_type(line)

def read_throughput_data(filename):
    perf_data = PerfData(filename)

    df = pd.DataFrame()
    for run in PerformanceRunIterator(perf_data.data(), perf_data.headers()):
        run_dataframe = pd.read_csv(StringIO(run), parse_dates=[perf_data.datetime_headers()])

        trimmed_section = trim_edges(run_dataframe)

        if len(trimmed_section) > 0:
            df = df.append(trimmed_section)

    # Reset the index because it is a Frankenstein of smaller indexes
    df = df.reset_index().drop('index', axis=1)
    return df

def trim_edges(data):
    indexes = data.set_index('start-time').resample('1S').aggregate(lambda x: 1).index
    test_start_time = indexes[0]
    test_end_time = indexes[-1]
    return data[(data['start-time'] >= test_start_time) & (data['start-time'] <= test_end_time)]

def process_throughput_data(data):
    buckets = data.set_index('start-time')['response-time'].resample('1S')
    throughput_data_set = buckets.aggregate({"throughput": lambda x: 0 if x.count() == 0 else x.count()})

    throughput_data_set = throughput_data_set.reset_index()
    throughput_data_set = throughput_data_set.fillna(method='ffill')
    return buckets, throughput_data_set

def generate_fit_line(data):
    y, x = dmatrices('latency ~ throughput', data=data, return_type='dataframe')
    fit = sm.GLM(y, x, family=sm.families.InverseGaussian(sm.families.links.inverse_squared)).fit()
    max_throughput = data['throughput'].max()
    min_throughput = data['throughput'].min()
    domain = np.arange(min_throughput, max_throughput)
    prediction_inputs = np.ones((len(domain), 2))
    prediction_inputs[:, 1] = domain
    fit_line = fit.predict(prediction_inputs)
    return domain, fit_line, round(max_throughput)

if __name__ == '__main__':
    matplotlib.style.use('ggplot')

    matplotlib.rcParams['figure.figsize'] = 9, 6
    matplotlib.rcParams['legend.loc'] = 'best'
    matplotlib.rcParams['figure.dpi'] = 120

    # We'll need these packages for plotting fit lines
    warnings.filterwarnings('ignore')
    performanceResultsFile = sys.argv[1]
    assert os.path.isfile(performanceResultsFile), 'Missing performance results file'

    compareDatasets = False

    if compareDatasets:
        assert os.path.isfile('old_perfResults.csv'), 'Missing old performance results file "old_perfResults.csv"'

    goData = read_throughput_data(performanceResultsFile)

    throughputBuckets, throughputData = process_throughput_data(goData)

    if compareDatasets:
        oldGoData = read_throughput_data('old_perfResults.csv')
        oldThroughputBuckets, oldThroughputData = process_throughput_data(oldGoData)

    goData['throughput'] = throughputBuckets.transform(len).reset_index()['response-time']
    goData.columns = ['start-time', 'latency', 'throughput']

    if compareDatasets:
        oldGoData['throughput'] = oldThroughputBuckets.transform(len).reset_index()['response-time']
        oldGoData.columns = ['start-time', 'latency', 'throughput']

    domain, goFitLine, xLimit = generate_fit_line(goData)

    if compareDatasets:
        oldDomain, oldGoFitLine, oldXLimit = generate_fit_line(oldGoData)

    fig, ax = plt.subplots()

    # Change the value of `c` to change the color. http://matplotlib.org/api/colors_api.html
    ax = goData.plot(ax=ax, kind='scatter', x='throughput', y='latency', c='b', marker='.', alpha=0.2)
    ax.plot(domain, goFitLine, c='b', lw=2)  # Plot the fit line

    if compareDatasets:
        ax = oldGoData.plot(ax=ax, kind='scatter', x='throughput', y='latency', c='r', marker='.', alpha=0.2)
        ax.plot(oldDomain, oldGoFitLine, c='r', lw=2)  # Plot the fit line
        ax.legend(['after', 'before'])

    # To update x & y axis range change the parameters in function set_(x/y)lim(lower_limit, uppper_limit)

    ax.autoscale(True)
    ax.autoscale_view(True, True, True)
    plt.xlabel('Throughput (requests/sec)')
    plt.ylabel('Latency (sec)')
    plt.title('Headroom plot', y=1.05)
    plt.plot()

    filenameForPlot = performanceResultsFile[:-4] + "Plot.png"
    plt.savefig(filenameForPlot)
    print ("saving graph to " + filenameForPlot)


#!/usr/bin/python3
"""
This module defines a class called Square.
"""

class Square:
    """
    This class represents a square shape.
    """
    def __init__(self, size=0):
        """
        Initializes a new instance of the Square class
        Args:
        size (int): The side length of the square.

        Raises:
        TypeError: If size is not an integer.
        ValueError: If size is less than 0.
        """
        if type(size) is not int:
            raise TypeError("size must be an integer")
        elif size < 0:
            raise ValueError("size must be >= 0")
        else:
            self._Square__size = size


import micropython
import machine

# Allocate memory for the case of an exception was rised in an IRQ
micropython.alloc_emergency_exception_buf(100)

class Encoder:
    """
    Scheduler is used for soft IRQ, unfortunately, on rp2 the deph is set to 8
    which appears to make lose signals
    """
    def __init__(self, pin_channel_a, pin_channel_b, reverse_direction = False, use_soft_interrupt_irq=False, sig_trigger=machine.Pin.IRQ_RISING, pull_r=None):
        self._counter = 0

        if reverse_direction:
            self._dir = -1
        else:
            self._dir = 1

        # Initialise rising edge detection
        self._pin_channel_a = pin_channel_a
        self._pin_channel_a.init(machine.Pin.IN, pull_r)

        self._pin_channel_b = pin_channel_b
        self._pin_channel_b.init(machine.Pin.IN)

        self._pin_channel_a.irq(trigger=sig_trigger, handler=self.signal_hard_handler)

    def signal_soft_handler(self, _):
        """
        First called by interrupt, then schedule the interrupt handler
        asap to avoid allocation problem in IRQ
        """
        micropython.schedule(self.signal_handler, self._pin_channel_b.value())

    def signal_hard_handler(self, _):
        """ Actual implementation of interupt routine"""
        if self._pin_channel_b.value() == 0:
            self._counter -= self._dir
        else :
            self._counter += self._dir

    def signal_handler(self, _):
        """ Actual implementation of interupt routine"""
        if self._pin_channel_b.value() == 0:
            self._counter -= self._dir
        else :
            self._counter += self._dir

    @property
    def counter(self):
        return self._counter


from django.urls import path
from myapp import views

urlpatterns = [
   path('project/create', views.projectcreate),
   path('project/show', views.projectshow),
   path('employee/create', views.employeecreate),
   path('employee/show', views.employeeshow),
   path('project/view/<int:id>', views.projectview, name='project/view'),
   path('project/addemployeetoproject/<int:id>', views.addemployeetoproject),
   path('project/delete/<int:id>', views.projectdelete),
   path('project/update/<int:id>', views.projectupdate),
   path('employee/view/<int:id>', views.employeeview, name='employee/view'),
   path('employee/asignprojecttoemployee/<int:id>', views.asignprojecttoemployee),
   path('employee/update/<int:id>', views.employeeupdate),
   path('employee/delete/<int:id>', views.employeedelete)
]


import os
import time
from pathlib import Path
template = "#include<bits/stdc++.h>\n#define mod 1000000007\n#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)\n#define pb push_back\n#define mp make_pair\n#define ll long long int\n#define fi first\n#define se second\n#define vll std::vector<ll> vl\n#define ld long double\nusing namespace std;\nint main()\n{\nfastio;\n\n\tll i,j,cnt=0,n;\n\nreturn 0;\n}"
while True:
    fname = input('Enter the cpp filename without file extension\n')
    ext = int(input('Choose the option of required extension -\n1. cpp\n2. py\n'))
    cwd = os.getcwd()
    if ext==1:
        filename = fname+".cpp"
        my_file = Path(os.path.join(cwd,filename))
        if my_file.is_file():
             print ("File exist, Enter a valid File name")
        else:
            f = open(os.path.join(cwd,filename),"w+")
            f.write(template)
            print('File created !')
            f.close()
            time.sleep(2)
    elif ext==2:
        filename = fname+".py"
        my_file = Path(os.path.join(cwd,filename))
        if my_file.is_file():
             print ("File exist, Enter a valid File name")
        else:
            f = open(os.path.join(cwd,filename),"w+")
            print('File created !')
            f.close()
            time.sleep(2)
    else:
        print('Select Valid option for extension')


#!/bin/python3

import os

# Complete the sockMerchant function below.
def sockMerchant(n, array):
    counts = {}
    for item in array:
        try:
            counts[item] += 1
        except KeyError:
            counts.update({item: 1})

    n_pairs = 0
    for key in counts.keys():
        n_pairs += counts[key] // 2
    return n_pairs

if __name__ == "__main__":
    fptr = open(os.environ["OUTPUT_PATH"], "w")
    n = int(input())
    ar = list(map(int, input().rstrip().split()))
    result = sockMerchant(n, ar)
    fptr.write(str(result) + "\n")
    fptr.close()


import sys
import pygame
import pygame.gfxdraw

class Rubrika:
    def __init__(self, x, y, a, b, szin, szoveg):
        self.x = x
        self.y = y
        self.a = a
        self.b = b
        self.szin = szin
        self.szoveg = szoveg

    def negyzet(self, kepernyo):
        return pygame.Rect(self.x, self.y, self.a, self.b)

def logo(kepernyo):
    logo = pygame.image.load("mil_logo.gif")
    kepernyo.blit(logo, (435, 60))

def inditas_negyzet():
    return pygame.Rect(412, 310, 200, 60)

def dicsoseg_negyzet():
    return pygame.Rect(412, 380, 200, 60)

def menu_kilepes_negyzet():
    return pygame.Rect(412, 450, 200, 60)

def fomenu(kepernyo):
    kepernyo.fill((0, 0, 60))
    logo(kepernyo)

    fomenu_elemek = [Rubrika(412, 310, 200, 60, pygame.Color(255, 255, 255), "Játék indítása"),
                     Rubrika(412, 380, 200, 60, pygame.Color(255, 255, 255), "Dicsőséglista"),
                     Rubrika(412, 450, 200, 60, pygame.Color(255, 255, 255), "Kilépés")]

    for f in fomenu_elemek:
        negyzet_rajzol(kepernyo, f, 0)
        szoveg_negyzetbe(kepernyo, f)

    pygame.display.update()

def segitseg_rubrika():
    return [Rubrika(12, 306, 75, 60, pygame.Color(255, 255, 255), ""),
            Rubrika(122, 306, 75, 60, pygame.Color(255, 255, 255), "")]

def segitseg_negyzet():
    '''visszaadja a segítségek dobozainak koordinátáit a kattintáshoz'''
    return [pygame.Rect(12, 306, 75, 60), pygame.Rect(122, 306, 75, 60)]

def segitseg_logo(kepernyo, hely, fajl):
    fajlbol = pygame.image.load(fajl)
    kepernyo.blit(fajlbol, (hely.x + 5, hely.y + 5))

def negyzet_rajzol(kepernyo, rubrika, teli):
    if teli == 1:
        pygame.gfxdraw.box(kepernyo, rubrika.negyzet(kepernyo), rubrika.szin)
        pygame.gfxdraw.rectangle(kepernyo, pygame.Rect(rubrika.x, rubrika.y, rubrika.a+2, rubrika.b+2), pygame.Color(255, 255, 255))

    elif teli == 0:
        pygame.gfxdraw.rectangle(kepernyo, rubrika.negyzet(kepernyo), rubrika.szin)

def szoveg_negyzetbe(kepernyo, forras):
    betustilus = pygame.font.SysFont("Bahnschrift SemiLight", 24)
    szoveg = betustilus.render(forras.szoveg, 1, (255, 255, 255))
    kepernyo.blit(szoveg, (forras.x + 10, forras.y + (forras.b)/2))

def valasz_negyzet():
    return [
            pygame.Rect(12, 500, 500, 100),
            pygame.Rect(12+500, 500, 500, 100),
            pygame.Rect(12, 500+100, 500, 100),
            pygame.Rect(12+500, 500+100, 500, 100)
            ]

def kerdes_valasz_betoltes(kepernyo, forras, statusz):
    '''betölti a következő kérdést és válaszlehetőségeket, a statusz 0,1,2 értéket vehet fel: 0=alaphelyzet, 1=jó válasz, 2=rossz válasz - ettől függően változik a jó válasz négyzetének színe'''
    kerdes = Rubrika(12, 384, 1000, 100, pygame.Color(255, 255, 255), forras.kerdes)
    kategoria = Rubrika(12, 236, 200, 50, pygame.Color(255, 255, 255), forras.kategoria)

    valaszok = [
            Rubrika(12, 500, 500, 100, pygame.Color(0, 0, 60), "A: {}".format(forras.a)),
            Rubrika(12+500, 500, 500, 100, pygame.Color(0, 0, 60), "B: {}".format(forras.b)),
            Rubrika(12, 500+100, 500, 100, pygame.Color(0, 0, 60), "C: {}".format(forras.c)),
            Rubrika(12+500, 500+100, 500, 100, pygame.Color(0, 0, 60), "D: {}".format(forras.d))
            ]

    if statusz == 0:
        pass
    elif statusz == 1:
        for jo in valaszok:
            if jo.szoveg[0] == forras.valasz:
                jo.szin = pygame.Color(50, 100, 0)

    elif statusz == 2:
        for jo in valaszok:
            if jo.szoveg[0] == forras.valasz:
                jo.szin = pygame.Color(150, 0, 0)

    negyzet_rajzol(kepernyo, kerdes, 0)
    szoveg_negyzetbe(kepernyo, kerdes)

    negyzet_rajzol(kepernyo, kategoria, 0)
    szoveg_negyzetbe(kepernyo, kategoria)

    for v in valaszok:
        negyzet_rajzol(kepernyo, v, 1)
        szoveg_negyzetbe(kepernyo, v)

    pygame.display.update()

def hatter(kepernyo):
    segitseg_hely = segitseg_rubrika()
    kepernyo.fill((0, 0, 60))
    logo(kepernyo)

    segitseg_logo(kepernyo, segitseg_hely[0], "kozonseg.png")
    segitseg_logo(kepernyo, segitseg_hely[1], "felezes.png")

    kilepes_rubrika = Rubrika(854, 20, 150, 60, pygame.Color(60, 0, 0), "Kilépés")
    negyzet_rajzol(kepernyo, kilepes_rubrika, 1)
    szoveg_negyzetbe(kepernyo, kilepes_rubrika)

def pontok_betoltes(kepernyo, forras):
    pont_rajz = Rubrika(762, 306, 250, 60, pygame.Color(0, 0, 60), "Nyeremény: {} Ft".format(forras))

    if  "100000 Ft" in pont_rajz.szoveg or "1500000 Ft" in pont_rajz.szoveg or "40000000 Ft" in pont_rajz.szoveg:
        pont_rajz.szin = pygame.Color(220, 180, 0)

    negyzet_rajzol(kepernyo, pont_rajz, 1)
    szoveg_negyzetbe(kepernyo, pont_rajz)

def idozito_keret(kepernyo, szam):
    idozito = Rubrika(482, 306, 60, 60, pygame.Color(0, 0, 60), "{}".format(szam))
    negyzet_rajzol(kepernyo, idozito, 1)
    szoveg_negyzetbe(kepernyo, idozito)
    pygame.display.update()

def kezdokepernyo(kepernyo):
    '''kirajzolja az ablakba a kezdőképernyő felületét, a rubrikák koordinátái az 1024*720-as ablak pixelei alapján lettek megadva'''
    kepernyo.fill((0, 0, 60))
    logo(kepernyo)
    kezdo_rubrikak = [Rubrika(362, 232, 300, 60, pygame.Color(0, 0, 60), "Játékos neve"),
                      Rubrika(437, 500, 150, 60, pygame.Color(0, 0, 150), "Játék indítása"),
                      Rubrika(854, 20, 150, 60, pygame.Color(60, 0, 0), "Kilépés"),
                      Rubrika(362, 322, 300, 60, pygame.Color(0, 0, 30), "")]

    for rubrika in kezdo_rubrikak:
        negyzet_rajzol(kepernyo, rubrika, 1)
        szoveg_negyzetbe(kepernyo, rubrika)

def szint_negyzetek():
    return [pygame.Rect(257, 412, 150, 60), pygame.Rect(437, 400, 150, 60), pygame.Rect(617, 400, 150, 60)]

def inditogomb_negyzet():
    return pygame.Rect(437, 500, 150, 60)

def kilepes_negyzet():
    return pygame.Rect(854, 20, 150, 60)

def felhasznalo_rubrika():
    return Rubrika(362, 322, 300, 60, pygame.Color(0, 0, 30), "")

def szintvalaszto_negyzetek(kepernyo, statusz):
    '''statusz információ: Ha 0, akkor alaphelyzet. Ha 1, Kezdő = világoskék, Ha 2 = Normal = világoskék, Ha 3 = Extrem = világoskék'''

    szintek = [Rubrika(257, 412, 150, 60, pygame.Color(0, 0, 60), "Kezdő"),
               Rubrika(437, 412, 150, 60, pygame.Color(0, 0, 60), "Normál"),
               Rubrika(617, 412, 150, 60, pygame.Color(0, 0, 60), "Extrém")]

    if statusz == 0:
        pass
    elif statusz == 1:
        szintek[0].szin = pygame.Color(0, 0, 150)

    elif statusz == 2:
        szintek[1].szin = pygame.Color(0, 0, 150)

    elif statusz == 3:
        szintek[2].szin = pygame.Color(0, 0, 150)

    for rubrika in szintek:
        negyzet_rajzol(kepernyo, rubrika, 1)
        szoveg_negyzetbe(kepernyo, rubrika)

def szint_hibauzenet(kepernyo):
    hibahely = Rubrika(437, 590, 150, 60, pygame.Color(0, 0, 60), "Válassz szintet!")
    szoveg_negyzetbe(kepernyo, hibahely)

def segitseg_megjelenes(obj, kepernyo, statusz):
    '''paraméterként megadott Segitoeszkoz objektumot rajzolja körbe egy négyzettel, ha fel lett használva piros színnel'''

    if statusz == 0:
        pass

    elif statusz == 1:
        obj.szin = pygame.Color(150, 0, 0)

    negyzet_rajzol(kepernyo, obj, 0)
    pygame.display.update()

def kozonsegszavazat(kepernyo, statusz, *forras):
        if statusz == 0:
            kozonseg_rubrika = Rubrika(212, 306, 245, 60, pygame.Color(255, 255, 255), "")
        elif statusz == 1:
            forras = str(forras)
            forras = forras[2:-3]
            kozonseg_rubrika = Rubrika(212, 306, 245, 60, pygame.Color(255, 255, 255), "{}".format(forras))

        negyzet_rajzol(kepernyo, kozonseg_rubrika, 0)
        szoveg_negyzetbe(kepernyo, kozonseg_rubrika)
        pygame.display.update()

def jatek_vege(kepernyo, forras, statusz):
    if statusz:
        vege_rubrika = Rubrika(312, 310, 400, 60, pygame.Color(0, 0, 60), "Megnyerted a játékot! Nyereményed: {} Ft".format(forras))
    else:
        vege_rubrika = Rubrika(312, 310, 400, 60, pygame.Color(0, 0, 60), "Legközelebb jobban megy majd! Nyereményed: {} Ft".format(forras))

    jvege = pygame.Surface((1024, 720))
    jvege.fill((0, 0, 60))
    logo(jvege)
    kepernyo.blit(jvege, (0, 0))

    szoveg_negyzetbe(kepernyo, vege_rubrika)
    kilepes_rubrika = Rubrika(854, 20, 150, 60, pygame.Color(60, 0, 0), "Kilépés")
    negyzet_rajzol(kepernyo, kilepes_rubrika, 1)
    szoveg_negyzetbe(kepernyo, kilepes_rubrika)

    pygame.display.update()

def megallas(kepernyo):
    megallas_rubrika = Rubrika(12, 156, 150, 50, pygame.Color(0, 100, 0), "Megállok")

    negyzet_rajzol(kepernyo, megallas_rubrika, 0)
    szoveg_negyzetbe(kepernyo, megallas_rubrika)

def megallas_negyzet():
    return pygame.Rect(12, 156, 150, 50)

def teljesites_ido_UI(ido):
    ido = int(ido)
    perc = ido//60000
    mp = (ido - (perc*60000)) // 1000
    return "{} perc {} másodperc".format(perc, mp)

def dicsoseglista_UI(kepernyo, forras):
    '''grafikusan megjeleníti a toplistát - 20 legjobb játékos eredményét, a forras parameter egy tömb referenciája, amelyben Jatekos objektumok vannak'''
    kepernyo.fill((0, 0, 60))

    adat_rubrikak = [Rubrika(12, 12, 250, 38, pygame.Color(255, 255, 255), "NÉV"),
                     Rubrika(12+250, 12, 250, 38, pygame.Color(255, 255, 255), "PONTSZÁM"),
                     Rubrika(12+500, 12, 250, 38, pygame.Color(255, 255, 255), "NEHÉZSÉGI SZINT"),
                     Rubrika(12+750, 12, 250, 38, pygame.Color(255, 255, 255), "TELJESÍTÉSI IDŐ")]

    for rubrika in adat_rubrikak:
        negyzet_rajzol(kepernyo, rubrika, 0)
        szoveg_negyzetbe(kepernyo, rubrika)

    listaadatok = []

    for f in forras:
        telj_ido = teljesites_ido_UI(f.ido)
        listaadatok.append([f.nev, f.pontszam, f.nehezseg, telj_ido, f.segitseg])

    kezdo_x = 12
    kezdo_y = 50

    for i in range(20):
        try:
            f_nev = listaadatok[i][0]

        except IndexError:
            f_nev = ""

        try:
            hasznalt_segitseget = listaadatok[i][4]

        except:
            hasznalt_segitseget = ""

        nev = Rubrika(kezdo_x, kezdo_y + i*32, 250, 32, pygame.Color(0, 0, 60), "{}".format(f_nev))
        if hasznalt_segitseget == "False":
            nev.szin = pygame.Color(220, 180, 0)

        negyzet_rajzol(kepernyo, nev, 1)
        szoveg_negyzetbe(kepernyo, nev)

    pontszamok = []

    for i in range(20):
        try:
            f_pont = listaadatok[i][1]

        except IndexError:
            f_pont = ""

        pontszam = Rubrika(kezdo_x + 250, kezdo_y + i*32, 250, 32, pygame.Color(0, 0, 60), "{}".format(f_pont))
        negyzet_rajzol(kepernyo, pontszam, 1)
        szoveg_negyzetbe(kepernyo, pontszam)

    for i in range(20):
        try:
            f_szint = listaadatok[i][2]

        except IndexError:
            f_szint = ""

        nehezseg = Rubrika(kezdo_x + 500, kezdo_y + i*32, 250, 32, pygame.Color(0, 0, 60), "{}".format(f_szint))
        negyzet_rajzol(kepernyo, nehezseg, 1)
        szoveg_negyzetbe(kepernyo, nehezseg)

    for i in range(20):
        try:
            f_ido = listaadatok[i][3]

        except IndexError:
            f_ido = ""

        ido = Rubrika(kezdo_x + 750, kezdo_y + i*32, 250, 32, pygame.Color(0, 0, 60), "{}".format(f_ido))
        negyzet_rajzol(kepernyo, ido, 1)
        szoveg_negyzetbe(kepernyo, ido)

    pygame.display.update()


#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import pandas as pd
import datetime
import matplotlib.pyplot as plt
import paths as paths

from DataBaseProxy import DataBaseProxy
from util import Utility
util = Utility()
dbp = DataBaseProxy()

year = 2017
month = 5
day = 6

#km macchine per enjoy e car2go in una settimana
start = datetime.datetime(year, month, day, 0, 0, 0)
end = datetime.datetime(year, month +2, day, 23, 59, 0)
end2 = datetime.datetime(year, month, day, 23,59,0)

def clean_durations(df):
    df = df[df.duration < df.duration.quantile(0.99)]
    df = df[df.duration > df.duration.quantile(0.01)]
    return df

def duration_per_car(df) :
    out_df= pd.DataFrame()
    out_df["plate"]  = df.plate
    out_df['duration']  = df.duration
    dur_per_car = out_df.groupby('plate',  as_index = False).sum()
    return dur_per_car

def bookings_per_car(df):
    df_freq = df.groupby('plate').count()
    df_freq = df_freq[['_id']].copy()
    df_freq = df_freq.rename(columns={'_id': 'freq'})
    return df_freq

def parkings_per_car(df) :
    out_df= pd.DataFrame()
    out_df["plate"]  = df.plate
    out_df['number_of_parkings']  = df.duration
    dur_per_car = out_df.groupby('plate', as_index = False).count()

    return dur_per_car

def total_dur_per_car(df, df2):
    provider = util.get_provider(df)
    color = util.get_color(df)
    df = clean_durations(df)
    dur_per_car = duration_per_car(df)
    freq_per_car = bookings_per_car(df2)

    fig, ax = plt.subplots(1, 1, figsize=(9,10))
    my_xticks = dur_per_car.plate
#    print len(my_xticks)
    ax.plot(dur_per_car.index, dur_per_car.duration, linestyle='-', marker='x',color=color)
#    ax.set_xticks(my_xticks)
    ax.set_title("min per car - " + provider)
    ax.set_xlabel("Plate")
    ax.set_ylabel("Total minutes")
    plt.show()
    dur_per_car.set_index('plate', inplace=True)
    dur_per_car['freq'] = freq_per_car['freq']
    dur_per_car.dropna()
    return dur_per_car

def total_dist_per_car_no_outliers (df):
    provider = util.get_provider(df)
    color = util.get_color(df)
    df = clean_durations(df)
    dur_per_car = duration_per_car(df)

    std = dur_per_car['duration'].std()
    avg = dur_per_car['duration'].median()
    normalized_durations = dur_per_car[(dur_per_car['duration'] >= (avg-std)) &
                                       (dur_per_car['duration'] <= (avg+std))]

    fig, ax = plt.subplots(1, 1, figsize=(9,10))
#    my_xticks = normalized_durations.plate
#    print len(my_xticks)
#    plt.xticks(normalized_durations.index, my_xticks)
    plt.plot(normalized_durations.index, normalized_durations['duration'], linestyle='-', marker='x',color=color)
    ax.set_title("min per car in std - " + provider)
    ax.set_xlabel("Plate")
    ax.set_ylabel("Total minutes")
    plt.show()

def hist_dur_freq(column, df, df_source, data):
    provider = util.get_provider(df_source)
    color = util.get_color(df_source)

    if column == "duration":
        xlabel = "min"
    else :
        xlabel = ""

    if column == "freq":
        df = df.dropna()

    fig, ax = plt.subplots(2, 4, figsize=(20,10))
    fig.suptitle(provider + ' - ' + column + ' distributions')

    #uncleaned data
    ax[0,0].hist(df[column], 50, facecolor=color, alpha=0.75, cumulative=True, normed=True)
    ax[0,0].set_title("CDF - " + column)
    ax[0,0].set_xlabel(xlabel)

    ax[1,0].hist(df[column], 50, facecolor=color, alpha=0.75)
    ax[1,0].set_title("PDF - " + column)
    ax[1,0].set_xlabel(xlabel)

    #filtering - only cars with at least 3 parkings at day
    df = df[df.freq > 30]
    ax[0,1].hist(df[column], 50, facecolor=color, alpha=0.75, cumulative=True, normed=True)
    ax[0,1].set_title("filtered CDF - " + column)
    ax[0,1].set_xlabel(xlabel)

    ax[1,1].hist(df[column], 50, facecolor=color, alpha=0.75)
    ax[1,1].set_title("filtered PDF - " + column)
    ax[1,1].set_xlabel(xlabel)

    #divided per number of days
    ax[0,2].hist(df[column]/data["valid_days"], 50, facecolor=color, alpha=0.75, cumulative=True, normed=True)
    ax[0,2].set_title("filtered CDF per day - " + column)
    ax[0,2].set_xlabel(xlabel)

    ax[1,2].hist(df[column]/data["valid_days"], 50, facecolor=color, alpha=0.75)
    ax[1,2].set_title("filtered PDF per day - " + column)
    ax[1,2].set_xlabel(xlabel)

    #divided per number of days in interval
    ax[0,3].hist(df[column]/data["cleaned_valid_days"], 50, facecolor=color, alpha=0.75, cumulative=True, normed=True)
    ax[0,3].set_title("filtered CDF per day clnd - " + column)
    ax[0,3].set_xlabel(xlabel)

    ax[1,3].hist(df[column]/data["cleaned_valid_days"], 50, facecolor=color, alpha=0.75)
    ax[1,3].set_title("filtered PDF per day clnd - " + column)
    ax[1,3].set_xlabel(xlabel)

    res = {
            column+"_mean" : df[column].mean(),
            column+"_median": df[column].median(),
            column+"_std" : df[column].std(),
            column+"_mean_valid_days" : (df[column]/data["valid_days"]).mean(),
            column+"_median_valid_days": (df[column]/data["valid_days"]).median(),
            column+"_std_valid_days" : (df[column]/data["valid_days"]).std(),
            column+"_mean_valid_days_clnd" : (df[column]/data["cleaned_valid_days"]).mean(),
            column+"_median_valid_days_clnd": (df[column]/data["cleaned_valid_days"]).median(),
            column+"_std_valid_days_clnd" : (df[column]/data["cleaned_valid_days"]).std()
            }

    fig.savefig(paths.plots_path3+"_"+provider+"_"+column+"_parkings_tats.png", bbox_inches='tight')
    return df,res
#
#enjoy_parkings = dbp.query_parkings_df('enjoy','Torino', start, end)
#car2go_parkings = dbp.query_parkings_df('car2go','Torino', start, end)
#enjoy_parkings.to_pickle(paths.enjoy_parkings_pickle_path, None)
#car2go_parkings.to_pickle(paths.car2go_parkings_pickle_path, None)

enjoy = pd.read_pickle(paths.enjoy_pickle_path, None)
car2go = pd.read_pickle(paths.car2go_pickle_path, None)
enjoy_parkings = pd.read_pickle(paths.enjoy_parkings_pickle_path, None)
car2go_parkings = pd.read_pickle(paths.car2go_parkings_pickle_path, None)

#enj_data = util.get_valid_days(enjoy,start,end)
#c2g_data = util.get_valid_days(car2go,start,end)

#enjoy_parkings_duration = duration_per_car(enjoy_parkings)
#enj_park_duration_freq = total_dur_per_car(enjoy_parkings, enjoy)
#total_dist_per_car_no_outliers(enjoy)
#enj_clean, enj_data["park_stats_duration"]  = hist_dur_freq("duration", enj_park_duration_freq, enjoy, enj_data)
#enj_clean, enj_data["park_stats_freq"]  = hist_dur_freq("freq", enj_park_duration_freq, enjoy, enj_data)
#
#car2go_parkings_duration = duration_per_car(car2go_parkings)
#car2go_park_duration_freq = total_dur_per_car(car2go_parkings, car2go)
#total_dist_per_car_no_outliers(car2go)
#c2g_clean, c2g_data["park_stats_duration"] = hist_dur_freq("duration", car2go_park_duration_freq, car2go, c2g_data)
#c2g_clean, c2g_data["park_stats_freq"] = hist_dur_freq("freq", car2go_park_duration_freq, car2go, c2g_data)

"""
Avg parking time per car (valid days)
"""
#enj_clean["duration_per_day"] = enj_park_duration_freq["duration"]/(enj_data["cleaned_valid_days"])
#enj_clean["freq_per_day"] = enj_park_duration_freq["freq"]/(enj_data["cleaned_valid_days"])
#c2g_clean["duration_per_day"] = car2go_park_duration_freq["duration"]/(c2g_data["cleaned_valid_days"])
#c2g_clean["freq_per_day"] = car2go_park_duration_freq["freq"]/(enj_data["cleaned_valid_days"])
#
#
#fig,ax =plt.subplots(1, 1, figsize=(9,10))
#enj_clean.hist(ax=ax, color=util.get_color(enjoy))
#fig2,ax2 = plt.subplots(1, 1, figsize=(9,10))
#c2g_clean.hist(ax=ax2, color=util.get_color(car2go))

'''
come informazione ho il numero di minuti in cui è stata ferma la macchina, e il numero di prenotazioni che questa ha
ricevuto
'''
#total_dist_per_car_no_outliers(enjoy_parkings)

#dur_per_car['index']  = dur_per_car['index'] / (dur_per_car['index'].sum())
#dur_per_car.hist(bins=100, cumulative=True, normed=True)

#df2 = parkings_per_car(enjoy_parkings)
#enjoy_parkings_duration['count'] = df2['number_of_parkings']
#
#df = enjoy_parkings[
#                    (enjoy_parkings.plate == 'EZ049TY')
#                    ]


# -*- coding: utf-8 -*-
"""
Created on Wed Oct  3 10:54:37 2018

@author: Administrator
"""

from greedyBandit import GreedyBandit
from decGreedyBandit import DecGreedyBandit
from optiBandit import OptiBandit
from UCBBandit import UCBBandit
from TSBandit import TSBandit

import matplotlib.pyplot as plt

times = 10000 #Simulation times

greedy = GreedyBandit(0,0)        #Greedy Bandit
dec_greedy = DecGreedyBandit(0) #Decreasing Greedy Bandit
opti = OptiBandit(0)            #Optimistic Initial Value Bandit
ucb = UCBBandit(0)              #UCB Bandit
ts = TSBandit(0)                #TS Bandit

#Function Arguments
m1=0.9
m2=0.5
m3=0.2
e=0.1

#Run Epsilon Greedy
greedy_result = greedy.run(m1,m2,m3,e,times)

#Run Decreasing Epsilon Greedy
dec_greedy_result = dec_greedy.run(m1,m2,m3,times)

#Run Optimistic Initial Value
opti_result = opti.run(m1,m2,m3,e,times)

#Run UCB
ucb_result = ucb.run(m1,m2,m3,times)

#Run Thompson Sampling
ts_result = ts.run(m1,m2,m3,times)

#Plot the result
plt.plot(greedy_result,label="Epsilon Greedy")
plt.plot(dec_greedy_result,label="Decreasing Epsilon Greedy")
plt.plot(opti_result,label="Optimistic Initial Value")
plt.plot(ucb_result,label="UCB1")
plt.plot(ts_result,label="Thompson Sampling")

#Show the graph
plt.legend(loc='upper right')
plt.show()


def add_numbers(n1, n2):
	result = n1+n2
	return result

def multiply_numbers(n1, n2):
	result = n1 * n2
	return result

num1 = float(input("enter first num:"))
num2 = float(input("enter second num:"))
summ = add_numbers(num1, num2)
product = multiply_numbers(num1, num2)

print("the sum of the two numbers is:", summ, " and its product is:", product)


"""
File: model_ops.py
Author: Tomáš Daniš
Login: xdanis05
Description: Module holding functions implementing model operations - such as training, predicting or evaluating
"""

import torch.nn as nn
import torch.nn.utils as tut
import torch.optim as optim
import torch
from tqdm import tqdm

import torchtext.data as torch_data

import conf
from baseline import totext

_DEBUG = False # Debugging flag

def train(net, train_iterator, dev_iterator, vocabs, epoch_num=4, lr=0.002):
    """
    Train a given model on the given dataset

    :param net: Model to train
    :param train_iterator: Iterator through a training set of the dataset to use
    :param dev_iterator: Iterator through a development set of the dataset to use
    :param vocabs: Vocabularies used in the dataset, only used for debugging
    :param epoch_num: Number of epochs to train for
    :param lr: Learning rate to train with
    :return: None
    """
    net.train()

    criterion = nn.CrossEntropyLoss()

    parameters = filter(lambda p: p.requires_grad, net.parameters())
    optimizer = optim.Adamax(parameters, lr=lr)

    # Training loop
    for epoch in tqdm(range(epoch_num), total=epoch_num, desc="Epoch"):
        epoch_loss = 0
        # Epoch loop
        for i, batch in tqdm(enumerate(train_iterator), total=len(train_iterator), desc="Iteration"):
            net.train()
            if _DEBUG:
                q = totext(batch.question[0],vocabs[0],batch_first=False)
                d = totext(batch.document[0],vocabs[0],batch_first=False)
                a1 = totext(batch.answer1[0],vocabs[0],batch_first=False)
                a2 = totext(batch.answer2[0],vocabs[0],batch_first=False)
                print("* "*20+"NEXT"+"* "*20)
                print(d[0])
                print("* " * 20 + "Question" + "* " * 20)
                print(q[0])
                print("* " * 20 + "Answers" + "* " * 20)
                print(a1[0])
                print(a2[0])

            optimizer.zero_grad()
            out = net(batch)
            loss = criterion(out, batch.correct)
            loss.backward()
            tut.clip_grad_norm_(parameters, 0.5)
            optimizer.step()
            epoch_loss += loss.item()

        # At the end of an epoch, evaluate the current performance on the development set
        with torch.no_grad():
            net.eval()
            dev_loss = 0
            j = 0
            correct = 0
            total = 0
            for j, val_batch in enumerate(dev_iterator):
                out = net(val_batch)
                total += val_batch.correct.size(0)
                loss = criterion(out, val_batch.correct)
                dev_loss += loss.item()
                _, pred_indexes = torch.max(out.data, 1)
                correct += (pred_indexes == val_batch.correct).sum().item()
            print('Epoch: {0}, Train loss: {1}, Dev loss: {2}, Dev accuracy: {3}%'.format(
                epoch, epoch_loss/len(train_iterator), dev_loss/(j+1), correct*100/total))

def predict(net, input, fields):
    """
    Predict answers for the given input
    :param net: Model to predict with
    :param input: Input to predict on
    :param fields: Structure of the data the model expects
    :return: Predictions for the given inputs
    """
    net.eval()
    example = torch_data.Example.fromlist(input, fields)
    dataset = torch_data.Dataset([example])
    iterator = torch_data.Iterator(dataset, batch_size=1)
    net_in = next(iter(iterator))
    return predict_batch(net, net_in)

def predict_batch(net, batch):
    """
    Predicts a single batch using the model provided

    :param net: Model to predict with
    :param batch: Batch to predict on
    :return: Predictions
    """
    with torch.no_grad():
        out = net(batch)
        _, predicted = torch.max(out.data, 1)
        return predicted

def eval_model(net, val_iter):
    """
    Evaluate a model's performance on the given test set

    :param net: Model to evaluate
    :param val_iter: Data to evaluate on
    :return: A tuple with the first item being the accuracy. The second item is a list of F1 scores for all classes
    in the task.
    """
    correct = 0
    total = 0
    cm = conf.ConfusionMatrix([0, 1])
    net.eval()
    with torch.no_grad():
        for batch in val_iter:
            total += batch.correct.size(0)
            prediction = predict_batch(net, batch)
            cm.add_entry(batch.correct.tolist(), prediction.tolist())
            correct += (prediction == batch.correct).sum().item()

    return correct/total, cm.get_f1()

Thesis - Machine Comprehension using Commonsense Knowledge/Source codes/Baseline/model_ops.py


#  Downloadable Modules
import os
import socket
import argparse
import atexit
import time
from threading import Thread

#  Self-made Modules
import database_utility as db_util
from ping_utility import ping_service
from handlers import conn_handler
import schemas
import config

#  Startup of server
#  Initialize Variables
HOST = '127.0.0.1'
PORT = 10000
OPEN_CONNECTION_LIMIT = 100

#  File and Storage Schemas in the database
file_info = schemas.file
storage_info = schemas.storage

#  Parse Arguments
parser = argparse.ArgumentParser()

parser.add_argument('--host', type=str,
        default=config.HOST, help='IP for the server')
parser.add_argument('--port', type=int,
        default=config.PORT, help='Port for the server')
parser.add_argument('--open_conn_limit', type=int,
        default=config.OPEN_CONNECTION_LIMIT,
        help='The limit of number of open connections')

args = parser.parse_args()
args_dict = vars(args)

#  Create Database
print("Connecting to database ...")
db_handler = db_util.DB_Interface()
db_handler.connect_db()
print("Connection Successful")

#  Start Ping Service
print("Starting Ping service ...")
ping_thread = Thread(target = ping_service,
        args= (db_handler, storage_info["table_name"], "ip"))
ping_thread.daemon = True
ping_thread.start()
print("Ping Started")

#  Create an open socket for accepting connections
print("Creating Socket for acceptings connections")
open_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
open_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
open_socket.bind((args.host, args.port))
open_socket.listen(args.open_conn_limit)
print("Socket Started")

@atexit.register
def cleanup():
    print("Closing connection to socket")
    open_socket.close()

while True:
    #  Accept connection
    conn, addr = open_socket.accept()
    print("Got a connection from {}".format(addr))
    db_handler_thread = db_util.DB_Interface()
    db_handler_thread.connect_db()
    handler_thread = Thread(
            target = conn_handler, args=(conn, addr, db_handler_thread))
    handler_thread.start()


import json
import sys
import os
import exceptions as e
import dev
from decouple import config
import time
import itemdat
import pathlib
pathto = str(pathlib.Path().absolute())

devmsg = dev.Dev.SendMessage()
devmsg("Setting up RPG module...")

class RPG():
    def __call__(self):
        self.start_up()
        pass

    def get_user_inv(self, profile):
        devmsg("Getting user inv data...")
        with open((pathto + f"\\playerdata\\player{profile}.json"), "r") as file:
            userdata = json.load(file)
        inv = userdata["INV"]
        if inv is None:
            raise e.Fatals.CantFindPlayerDataFiles(f"PLAYER{profile}.JSON IS EMPTY")
        else:
            with open((pathto + "\\data\\items.json"), "r", encoding='utf-8') as file:
                itemd = json.load(file)
                itemdata = itemd["ITEMDATA"]
            for i in inv:
                try:
                    item_name = itemdata[i]["NAME"]
                    item_lore = itemdata[i]["INFO"]
                    a = "\nПредмет:\n" + item_name + "\n" + item_lore
                    print(a)
                except KeyError:
                    raise e.JSONErrors.CantParseInventoryData(f"PLAYER{profile}.JSON HAS ILLEGAL ITEMDATA")

    def start_up(self):
        prof = {}
        for i in [1, 2, 3]:
            with open((pathto + f"\\playerdata\\player{i}.json"), "r") as file:
                devmsg(f"Checking file number {i}...")
                profiledata = json.load(file)
            isocc = profiledata["?EMPTY"]
            if not isocc:
                prof[f"prof{i}"] = 'полон'
            else:
                prof[f"prof{i}"] = 'пуст'
        profstr = "Профиль 1 - " + prof["prof1"] + "\nПрофиль 2 - " + prof["prof2"] + "\nПрофиль 3 - " + prof["prof3"]
        print("▀████    ▐████▀    ▄████████  ▄█  ")
        time.sleep(0.2)
        print("  ███▌   ████▀    ███    ███ ███  ")
        time.sleep(0.2)
        print("   ███  ▐███      ███    ███ ███▌ ")
        time.sleep(0.2)
        print("   ▀███▄███▀      ███    ███ ███▌ ")
        time.sleep(0.2)
        print("   ████▀██▄     ▀███████████ ███▌ ")
        time.sleep(0.2)
        print("  ▐███  ▀███      ███    ███ ███  ")
        time.sleep(0.2)
        print(" ▄███     ███▄    ███    ███ ███  ")
        time.sleep(0.2)
        print("████       ███▄   ███    █▀  █▀ \n")
        print("Добро пожаловать в XAI!\n")
        print("Введите 1 чтобы начать")
        print("Введите 2 чтобы выйти")
        profstr = "Профиль 1 - " + prof["prof1"] + "\nПрофиль 2 - " + prof["prof2"] + "\nПрофиль 3 - " + prof["prof3"]
        filet = True
        while filet is True:
            try:
                answ = input("Ваш ввод:\n")
                filet = False
            except ValueError:
                print("Это не число!")
        if answ == "2":
            devmsg("Exiting...")
            exit()
        elif answ == "1":
            devmsg('User input = {"answ": "1"}')
            print("Вы выбрали начать.")
            print("Введите номер профиля для открытия/создания. Если профиль полон, то он будет открыт, иначе он будет создан.")
            print(profstr)
            filet = True
            while filet is True:
                try:
                    profile_chosen = int(input("Введите номер профиля...\n"))
                    if profile_chosen == "":
                        raise ValueError
                    filet = False
                except ValueError:
                    print("Это не число!")

            devmsg(f"User chose to open profile{profile_chosen}.json")
            devmsg("Trying to open profile data...")
        if profile_chosen >= 1 and profile_chosen <= 3:
            newps = "Вы выбрали профиль " + str(profile_chosen)
            devmsg(f"Profile{profile_chosen} exists and will be opened")
            profiledata = self.Profile.new_profile(self, profile_chosen)
        else:
            devmsg(f"Profile{profile_chosen} doesnt exists!")
            print("Профиль не существует! Выходим...")
            time.sleep(1)
            exit()

        print(f"Вы выбрали и открыли профиль {profile_chosen}. Что теперь?")
        print("Доступные функции:")
        print("1 - просмотреть инвентарь")
        print("2 - добавить предметы в инвентарь (не стабильно). Предметы - это ID с 001 до 008 включительно.")
        print("3 - начать битву")
        print("4 - выйти.")
        filet = True
        while filet is True:
            try:
                answ = int(input("Введите число.\n"))
                filet = False
            except ValueError:
                print("Это не число!")

        if answ == 4:
            devmsg("Closing...")
            exit()
        elif answ == 2:
            self.Profile.give(self, profile_chosen)
        elif answ == 1:
            self.get_user_inv(profile_chosen)
        elif answ == 3:
            enemy = itemdat.generate_random_mob(itemdat.Item.Use.GetPlayerLocation(profile_chosen))
            itemdat.Item.Use.battle(itemdat.Item.Use, profile_chosen, enemy)
        answ2 = input("\n")

    class Profile():
        def __call__(self, slotnum, playername):
            self.new_profile(slotnum, playername)
            pass

        sample_profile_data = {
            "?EMPTY": False,
            "ENDINGS_COMPLETED": {
                "POSITIVE": False,
                "NEGATIVE": False,
                "MIDDLE": False,
                "SHREK": False,
                "OUTBREAK": False,
                "SECRET": False,
                "S_DEATH": False,
                "TRUTH": False
            },
            "LOCATIONS VISITED": {
                "FIELDS": False,
                "BROKEN_TOWN": False,
                "ABANDONED_FARMS": False,
                "TEMPLE": False,
                "MOUNT_VILLAGE": False,
                "SUMMIT": False,
                "LAB": False,
                "HARDMODE_LOCS": {
                    "HOPELESS_FIELDS": False,
                    "REMNANTS_OF_TOWN": False,
                    "BURNT_FARMS": False,
                    "FORBIDDEN_TEMPLE": False,
                    "HIGH_PEAKS": False,
                    "LAST_SUMMIT": False,
                    "CLONE_LAB": False
                }
            },
            "DEATH_AMOUNT": 0,
            "HM_ON": False,
            "INV": [],
            "CURRENT_LOCATION": "FIELDS",
            "BALANCE": 0,
            "ENEMIES_SLAIN": {
                "NORMAL": {
                "FIELDS": 0,
                "BROKEN_TOWN": 0,
                "ABANDONED_FARMS": 0,
                "TEMPLE": 0,
                "MOUNT_VILLAGE": 0,
                "SUMMIT": 0,
                "LAB": 0
            },
            "HM": {
                "HOPELESS_FIELDS": 0,
                "REMNANTS_OF_TOWN": 0,
                "BURNT_FARMS": 0,
                "FORBIDDEN_TEMPLE": 0,
                "HIGH_PEAKS": 0,
                "LAST_SUMMIT": 0,
                "CLONE_LAB": 0,
                "STRONGHOLD": 0
            },
            "EXPERIENCE": 0
        }
    }
        def new_profile(self, slotnum:int):
            if slotnum >= 0 and slotnum <= 3:
                with open((pathto + f"\\playerdata\\player{slotnum}.json"), "r", encoding='utf-8') as file:
                    devmsg(f"Creating new profile with number {slotnum}...")
                    profiledata = json.load(file)
                isempty = profiledata["?EMPTY"]
                if not isempty:
                    devmsg(f"Cant overwrite an existing file with number '{slotnum}'")
                    devmsg("Looking for solution...")
                    time.sleep(1)
                    with open((pathto + f"\\playerdata\\player{slotnum}.json"), "r", encoding='utf-8') as file:
                        devmsg(f"Opening the file instead")
                        profiledata = json.load(file)
                    return profiledata
                else:
                    playername = input("Введите имя нового персонажа\n")
                    self.Profile.sample_profile_data["NAME"] = playername.capitalize()
                    devmsg(f"Dumping sample data into existing json file...")
                    with open((pathto + f"\\playerdata\\player{slotnum}.json"), "w", encoding="utf-8") as file:
                        json.dump(self.Profile.sample_profile_data, file, indent=4, sort_keys=True, ensure_ascii=False)
                    with open((pathto + f"\\playerdata\\player{slotnum}.json"), "r", encoding='utf-8') as file:
                        profiledata = json.load(file)
                        return profiledata
            else:
                devmsg(f"Couldnt create a new profile with number {slotnum}, as it doesn't exist!")
                print("Профиль не существует...")
                time.sleep(1)
                raise e.Fatals.CantFindDataFiles("Профиль не существует")

        def give(self, profile):
            with open((pathto + f"\\playerdata\\player{profile}.json"), "r", encoding='utf-8') as file:
                devmsg(f"Loading profile with number {profile}...")
                profiledata = json.load(file)
            print("Выберите айди предмета, чтобы дать персонажу. АЙДИ можно посмотреть на вики, то есть на\nhttps://github.com/Maxuss/XAI/wiki/ID-предметов")
            id_item = input("Введите айди\n")
            try:
                with open((pathto + "\\data\\items.json"), "r", encoding='utf-8') as file:
                    devmsg("Opening itemdata...")
                    itemd = json.load(file)
                    itemdata = itemd["ITEMDATA"]
                _current = itemdata[id_item]
                profiledata["INV"].append(id_item)
                with open((pathto + f"\\playerdata\\player{profile}.json"), "w", encoding='utf-8') as file:
                    devmsg("Dumping data to profile...")
                    json.dump(profiledata, file, indent=4, sort_keys=True, ensure_ascii=False)
                print("Предмет добавлен!\nПерезайдите, чтобы посмотреть данные инвентаря!")
            except KeyError:
                devmsg("Item with this ID doesn't exist!")
                print("Такой предмет не существует.")
                time.sleep()
                raise e.FileErrors.CantGenerateLoot("Предмет не существует.")


import requests
import json
import re
req=requests.Session()

header={
    "user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
}

url="http://www.122.gov.cn/m/map/select"

page=req.get(url=url,headers=header)

print(page.text)

pattern=re.compile(r"<i sfdm=\"(\d+)\" sfmc=\"(.*?)\" ym=\"(.*?)\" fzjg=\"(.*?)\".*?sftjb=\"(.*?)\"></i>",re.S)
d=re.findall(pattern,page.text)
s={}
for i in d:
    s[i[0]]={"address":i[1],"url":i[2],"cp":i[3],"sftjb":i[4]}
print(s)

json.dump(s,open("./info.json","w"))


import sqlite3
import sys, os
from PyQt6.QtWidgets import *
from PyQt6.QtCore import *
from PyQt6.QtGui import *
from PyQt6 import *

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from utils.database_table_setup import *
from utils.promo_management_sql import *

class PromoManagementWidget(QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowTitle('Promo Management')
        self.create_table = CreateDatabaseTable()
        self.create_table.database_table()
        self.init_layout()

    # filter Promo flag
    def filter_Promo(self):
        filter_input = self.filter_Promo_field.text()

        if filter_input == '':
            self.list_Promo_table.display_Promo_table(filter_input)
        else:
            self.list_Promo_table.filter_Promo_table(filter_input)

    def init_layout(self):
        self.layout = QGridLayout()

        self.filter_Promo_field = QLineEdit()
        self.filter_Promo_field.setPlaceholderText('Filter Promo by Promo name, supplier, etc...')

        self.add_promo_button = QPushButton('ADD')

        self.list_Promo_table = PromoListTable() # -- class ListPromoTable(QTableWidget)

        self.filter_Promo_field.textChanged.connect(self.filter_Promo) # connects to filter_Promo functions every change of text
        self.add_promo_button.clicked.connect(lambda: self.list_Promo_table.save_Promo_window("", "", "", "",""))

        self.layout.addWidget(self.filter_Promo_field,0,0)
        self.layout.addWidget(self.add_promo_button,0,1)
        self.layout.addWidget(self.list_Promo_table,1,0,1,2)

        self.setLayout(self.layout)

class PromoListTable(QTableWidget):
    data_saved = pyqtSignal()
    def __init__(self):
        super().__init__()
        self.call_sql_utils()
        self.init_layout()

    def call_sql_utils(self):
        self.PromoSelect = SelectPromoData()
        self.update_Promo = UpdatePromoData()

    def init_layout(self):
        self.setColumnCount(5)
        self.setHorizontalHeaderLabels(["","Promo Name", "Description", "Type", "Value"])

        self.populate_PromoList()

    def populate_PromoList(self):
        self.clearContents()

        data = self.PromoSelect.all_Promo_data()

        self.setRowCount(len(data))

        for row_index, (Promo_name, Promo_desc, promo_type, promo_value) in enumerate(data):
            self.setItem(row_index, 1, QTableWidgetItem(str(Promo_name)))
            self.setItem(row_index, 2, QTableWidgetItem(str(Promo_desc)))
            self.setItem(row_index, 3, QTableWidgetItem(str(promo_type)))
            self.setItem(row_index, 4, QTableWidgetItem(str(promo_value)))
            self.edit_tem_button = QPushButton('EDIT')
            self.edit_tem_button.clicked.connect(lambda row_index=row_index, Promo_name=Promo_name, Promo_desc=Promo_desc, Promo_type=promo_type, Promo_value=promo_value : self.save_Promo_window(row_index, Promo_name, Promo_desc, Promo_type, Promo_value))
            self.setCellWidget(row_index, 0, self.edit_tem_button)

    def save_Promo_window(self, row_index, Promo_name, Promo_desc, Promo_type, Promo_value):
        edit_item_dialog = SavePromoDialog(row_index, Promo_name, Promo_desc, Promo_type, Promo_value)
        edit_item_dialog.exec()
        self.populate_PromoList()

        self.data_saved.emit()

class SavePromoDialog(QDialog):
    data_saved = pyqtSignal()
    def __init__(self, row_index, Promo_name, Promo_desc, Promo_type, Promo_value):
        super().__init__()
        self.newPromo=""

        self.call_sql_utils()
        self.init_layout(row_index, Promo_name, Promo_desc, Promo_type, Promo_value)

    def call_sql_utils(self):
        self.promo_data = InsertPromoData()
        self.update_Promo = UpdatePromoData()

    def init_layout(self, row_index, Promo_name, Promo_desc, Promo_type, Promo_value):
        self.layout = QGridLayout()

        self.numeric_input_validator = QDoubleValidator()
        self.numeric_input_validator.setDecimals(2)  # Set the number of decimal places
        self.numeric_input_validator.setNotation(QDoubleValidator.Notation.StandardNotation)
        self.numeric_input_validator.setLocale(QLocale(QLocale.Language.English, QLocale.Country.UnitedStates))

        NameLbl = QLabel("Promo Name ")

        self.NameVal = QLineEdit()
        self.NameVal.setText(str(Promo_name))

        PromoDescLbl = QLabel("Description ")
        self.PromoDescVal = QLineEdit()
        self.PromoDescVal.setText(str(Promo_desc))

        PromoTypeLbl = QLabel("Promo Type ")
        self.PromoTypeVal = QLineEdit()
        self.PromoTypeVal.setText(str(Promo_type))

        PromoValLbl = QLabel("Promo Value ")
        self.PromoVal = QLineEdit()
        self.PromoVal.setText(str(Promo_value))
        self.PromoVal.setValidator(self.numeric_input_validator)

        save_button = QPushButton('SAVE')

        save_button.clicked.connect(lambda: self.Save_Promo(Promo_name))

        self.layout.addWidget(NameLbl,0,0)
        self.layout.addWidget(self.NameVal,0,1)
        self.layout.addWidget(PromoDescLbl,1,0)
        self.layout.addWidget(self.PromoDescVal,1,1)
        self.layout.addWidget(PromoTypeLbl,2,0)
        self.layout.addWidget(self.PromoTypeVal,2,1)
        self.layout.addWidget(PromoValLbl,3,0)
        self.layout.addWidget(self.PromoVal,3,1)

        self.layout.addWidget(save_button,4,1)

        self.setLayout(self.layout)

    def Save_Promo(self, Promo_name):
        newPromo = self.NameVal.text()
        newDesc = self.PromoDescVal.text()
        newTpye = self.PromoTypeVal.text()
        newVal = self.PromoVal.text()

        if Promo_name == "":
            self.promo_data.InsertPromo(newPromo, newDesc, newTpye, newVal)
        else:
            self.update_Promo.promo_data(newPromo, newDesc, newTpye, newVal, Promo_name)
        self.close()

if __name__ == ('__main__'):
    pos_app = QApplication(sys.argv)
    window = PromoManagementWidget()
    window.show()
    sys.exit(pos_app.exec())


import discord
from discord.ext import commands
from discord.utils import get
import json
from discord.ext.commands import has_permissions, MissingPermissions
import asyncio

bot = commands.Bot(command_prefix="!")

@bot.event
async def on_ready():
    print("Ticket bot running...")

@bot.command()
async def help_me(ctx):
    em = discord.Embed(title="Auroris Tickets Help", description="", color=0x00a8ff)
    await ctx.send(embed=em)

#@bot.command()
async def new(ctx, message_content = ""):
    await bot.wait_until_ready()

    with open("data.json") as f:
        data = json.load(f)

    ticket_number = int(data["ticket-counter"])
    ticket_number += 1

    ticket_channel = await ctx.guild.create_text_channel("ticket-{}".format(ticket_number))
    await ticket_channel.set_permissions(ctx.guild.get_role(ctx.guild.id), send_messages=False, read_messages=False)

    for role_id in data["valid-roles"]:
        role = ctx.guild.get_role(role_id)

        await ticket_channel.set_permissions(role, send_messages=True, read_messages=True, add_reactions=True, embed_links=True, attach_files=True, read_message_history=True, external_emojis=True)

    await ticket_channel.set_permissions(ctx.author, send_messages=True, read_messages=True, add_reactions=True, embed_links=True, attach_files=True, read_message_history=True, external_emojis=True)

    em = discord.Embed(title="New ticket from {}#{}".format(ctx.author.name, ctx.author.discriminator), description= "{}".format(message_content), color=0x00a8ff)

    await ticket_channel.send(embed=em)

    pinged_msg_content = ""
    non_mentionable_roles = []

    if data["pinged-roles"] != []:

        for role_id in data["pinged-roles"]:
            role = ctx.guild.get_role(role_id)

            pinged_msg_content += role.mention
            pinged_msg_content += " "

            if role.mentionable:
                pass
            else:
                await role.edit(mentionable=True)
                non_mentionable_roles.append(role)

        await ticket_channel.send(pinged_msg_content)

        for role in non_mentionable_roles:
            await role.edit(mentionable=False)

    data["ticket-channel-ids"].append(ticket_channel.id)

    data["ticket-counter"] = int(ticket_number)
    with open("data.json", 'w') as f:
        json.dump(data, f)

    created_em = discord.Embed(title="Auroris Tickets", description="Your ticket has been created at {}".format(ticket_channel.mention), color=0x00a8ff)

    await ctx.send(embed=created_em)

@bot.command()
async def close(ctx):
    with open('data.json') as f:
        data = json.load(f)

    if ctx.channel.id in data["ticket-channel-ids"]:

        channel_id = ctx.channel.id

        def check(message):
            return message.author == ctx.author and message.channel == ctx.channel and message.content.lower() == "close"

        try:

            em = discord.Embed(title="Interest Tickets", description="Are you sure you want to close this ticket? Reply with `close` if you are sure.", color=0x00a8ff)

            await ctx.send(embed=em)
            await bot.wait_for('message', check=check, timeout=60)
            await ctx.channel.delete()

            index = data["ticket-channel-ids"].index(channel_id)
            del data["ticket-channel-ids"][index]

            with open('data.json', 'w') as f:
                json.dump(data, f)

        except asyncio.TimeoutError:
            em = discord.Embed(title="Auroris Tickets", description="You have run out of time to close this ticket. Please run the command again.", color=0x00a8ff)
            await ctx.send(embed=em)

@bot.command()
async def interest(ctx):
    page = discord.Embed (
        title = 'Which sport are you interested in?',
        description = '''
        '⚽': soccer ball
        '⚾': baseball
        '🥎': softball
        '🏀': basketball
        '🏐': volleyball
        Please click sport emoji you are interested in.
        ''',
        colour = discord.Colour.orange()
    )

    message = await ctx.send(embed = page)
    await message.add_reaction('⚽') #soccer ball
    await message.add_reaction('⚾') #baseball
    await message.add_reaction('🥎') #softball
    await message.add_reaction('🏀') #basketball
    await message.add_reaction('🏐') #volleyball

    def check(reaction, user):
        return user == ctx.author

    reaction = None

    while True:
        if str(reaction) == '⚽':
            await new(ctx, message_content="Welcome to Soccer Ball Ticket! If you wanna close, please type !close")
        elif str(reaction) == '⚾':
            await new(ctx, message_content="Welcome to Baseball Ticket! If you wanna close, please type !close")
        elif str(reaction) == '🥎':
            await new(ctx, message_content="Welcome to Softball Ticket! If you wanna close, please type !close")
        elif str(reaction) == '🏀':
            await new(ctx, message_content="Welcome to Basketball Ticket! If you wanna close, please type !close")
        elif str(reaction) == '🏐':
            await new(ctx, message_content="Welcome to Volleyball Ticket! If you wanna close, please type !close")

        try:
            reaction, user = await bot.wait_for('reaction_add', timeout = 30.0, check = check)
            await message.remove_reaction(reaction, user)
        except:
            break

    await message.clear_reactions()
bot.run('ODczMjQ2MjI2MDEzOTEzMTA5.YQ1n7A.PSRzth7b-scgFehCtJEs6yhGI_8')
#bot.run('ODcyNDEwOTc4MTkxNTczMDQy.YQpeCQ.FCeibb_4ee1NkZAo2irmLhH2fLI')


import os

def search_cells(cells, target, size=0):
     return_list = []
     if size == 0:
         size = len(cells)
     cell = cells[0]
     remaining_cells = []
     if len(cells) > 1:
         remaining_cells = cells.copy()
         remaining_cells.remove(cell)
     for value in cell:
         remain_tar = target - value
         if remaining_cells:
             other_vals = search_cells(remaining_cells, remain_tar, size)
             if other_vals:
                 for other_val in other_vals:
                     if len(other_val) != size:
                         other_val.append(value)
                     return_list.append(other_val)
         elif remain_tar == 0:
             return_list.append([value])
     return return_list

def clean_search(cells, target):
    cleaned = []
    raw_combos = search_cells(cells, target)
    if raw_combos:
        cells = []
        for index in range(len(raw_combos[0])):
            single_cell = []
            for combo in raw_combos:
                single_cell.append(combo[index])
            cells.append(single_cell)
        for cell in cells:
            cleaned.append(dedupe(cell))
        cleaned.reverse()
    return cleaned

def dedupe(val):
     return sorted(list(set(val)))

def validate_input(val):
    try:
        int(val)
        return True
    except:
        return False

while True:
    print('Welcome you filthy cheater')
    print('Press Enter to continue, q to quit')
    cell_list_s = []
    while True:
        cell_val = input('Cell values:')
        if not validate_input(cell_val):
            break
        else:
            cell_list_s.append(cell_val)
    if cell_val != '' and not validate_input(cell_val):
        break
    target_s = input("Target value:")
    try:
        target = int(target_s)
    except:
        target = 0
    if target and cell_list_s:
        cell_list = []
        for cell_s in cell_list_s:
            cell = []
            for i in range(len(cell_s)):
                try:
                    cell.append(int(cell_s[i]))
                except:
                    # Shouldn't be able to reach this state...
                    os.system('clear')
                    print('Invalid input found goodbye.')
                    break
            cell_list.append(cell)
        results = clean_search(cell_list, target)
        print(f"Results: {results}")
        diff_list = []
        for input_cell, result_cell in zip(cell_list, results):
            diff_list.append([val for val in input_cell if val not in result_cell])
        print(f"Unuseable values: {diff_list}")
    input('Press Enter to startover')
    os.system('clear')


"""
Pipeline modules to prepare the data for the PSF subtraction.
"""

import time
import warnings

from typing import Optional, Tuple

import numpy as np

from astropy.coordinates import EarthLocation
from astropy.time import Time
from typeguard import typechecked

from pynpoint.core.processing import ProcessingModule
from pynpoint.util.module import progress, memory_frames
from pynpoint.util.image import create_mask, scale_image, shift_image

class PSFpreparationModule(ProcessingModule):
    """
    Module to prepare the data for PSF subtraction with PCA. The preparation steps include masking
    and an optional normalization.
    """

    __author__ = 'Markus Bonse, Tomas Stolker, Timothy Gebhard, Sven Kiefer'

    @typechecked
    def __init__(self,
                 name_in: str,
                 image_in_tag: str,
                 image_out_tag: str,
                 mask_out_tag: Optional[str] = None,
                 norm: bool = False,
                 resize: Optional[float] = None,
                 cent_size: Optional[float] = None,
                 edge_size: Optional[float] = None) -> None:
        """
        Parameters
        ----------
        name_in : str
            Unique name of the module instance.
        image_in_tag : str
            Tag of the database entry that is read as input.
        image_out_tag : str
            Tag of the database entry with images that is written as output.
        mask_out_tag : str, None, optional
            Tag of the database entry with the mask that is written as output. If set to None, no
            mask array is saved.
        norm : bool
            Normalize each image by its Frobenius norm. Only supported for 3D datasets (i.e.
            regular imaging).
        resize : float, None
            DEPRECATED. This parameter is currently ignored by the module and will be removed in a
            future version of PynPoint.
        cent_size : float, None, optional
            Radius of the central mask (in arcsec). No mask is used when set to None.
        edge_size : float, None, optional
            Outer radius (in arcsec) beyond which pixels are masked. No outer mask is used when set
            to None. If the value is larger than half the image size then it will be set to half
            the image size.

        Returns
        -------
        NoneType
            None
        """

        super().__init__(name_in)

        self.m_image_in_port = self.add_input_port(image_in_tag)

        if mask_out_tag is None:
            self.m_mask_out_port = None
        else:
            self.m_mask_out_port = self.add_output_port(mask_out_tag)

        self.m_image_out_port = self.add_output_port(image_out_tag)

        self.m_cent_size = cent_size
        self.m_edge_size = edge_size
        self.m_norm = norm

        # Raise a DeprecationWarning if the resize argument is used
        if resize is not None:
            warnings.warn('The \'resize\' parameter has been deprecated. Its value is currently '
                          'being ignored, and the argument will be removed in a future version '
                          'of PynPoint.', DeprecationWarning)

    @typechecked
    def run(self) -> None:
        """
        Run method of the module. Masks and normalizes the images.
        Returns
        -------
        NoneType
            None
        """

        # Get the PIXSCALE and MEMORY attributes
        pixscale = self.m_image_in_port.get_attribute('PIXSCALE')
        memory = self._m_config_port.get_attribute('MEMORY')

        # Get the numnber of dimensions and shape
        ndim = self.m_image_in_port.get_ndim()
        im_shape = self.m_image_in_port.get_shape()

        if ndim == 3:
            # Number of images
            nimages = im_shape[-3]

            # Split into batches to comply with memory constraints
            frames = memory_frames(memory, nimages)

        elif ndim == 4:
            # Process all wavelengths per exposure at once
            frames = np.linspace(0, im_shape[-3], im_shape[-3]+1)

        if self.m_norm and ndim == 4:
            warnings.warn('The \'norm\' parameter does not support 4D datasets and will therefore '
                          'be ignored.')

        # Convert m_cent_size and m_edge_size from arcseconds to pixels

        if self.m_cent_size is not None:
            self.m_cent_size /= pixscale

        if self.m_edge_size is not None:
            self.m_edge_size /= pixscale

        # Create 2D disk mask which will be applied to every frame
        mask = create_mask((int(im_shape[-2]), int(im_shape[-1])),
                           (self.m_cent_size, self.m_edge_size)).astype(bool)

        # Keep track of the normalization vectors in case we are normalizing the images (if
        # we are not normalizing, this list will remain empty)
        norms = list()

        start_time = time.time()

        # Run the PSFpreparationModule for each subset of frames
        for i in range(frames[:-1].size):
            # Print progress to command line
            progress(i, len(frames[:-1]), 'Preparing images for PSF subtraction...', start_time)

            if ndim == 3:
                # Get the images and ensure they have the correct 3D shape with the following
                # three dimensions: (batch_size, height, width)
                images = self.m_image_in_port[frames[i]:frames[i+1], ]

                if images.ndim == 2:
                    warnings.warn('The input data has 2 dimensions whereas 3 dimensions are '
                                  'required. An extra dimension has been added.')

                    images = images[np.newaxis, ...]

            elif ndim == 4:
                # Process all wavelengths per exposure at once
                images = self.m_image_in_port[:, i, ]

            # Apply the mask, i.e., set all pixels to 0 where the mask is False
            images[:, ~mask] = 0.

            # If desired, normalize the images using the Frobenius norm
            if self.m_norm and ndim == 3:
                im_norm = np.linalg.norm(images, ord='fro', axis=(1, 2))
                images /= im_norm[:, np.newaxis, np.newaxis]
                norms.append(im_norm)

            # Write processed images to output port
            if ndim == 3:
                self.m_image_out_port.append(images, data_dim=3)
            elif ndim == 4:
                self.m_image_out_port.append(images, data_dim=4)

        # Store information about mask
        if self.m_mask_out_port is not None:
            self.m_mask_out_port.set_all(mask)
            self.m_mask_out_port.copy_attributes(self.m_image_in_port)

        # Copy attributes from input port
        self.m_image_out_port.copy_attributes(self.m_image_in_port)

        # If the norms list is not empty (i.e., if we have computed the norm for every image),
        # we can also save the corresponding norm vector as an additional attribute
        if norms:
            self.m_image_out_port.add_attribute(name='norm',
                                                value=np.hstack(norms),
                                                static=False)

        # Save cent_size and edge_size as attributes to the output port
        if self.m_cent_size is not None:
            self.m_image_out_port.add_attribute(name='cent_size',
                                                value=self.m_cent_size * pixscale,
                                                static=True)

        if self.m_edge_size is not None:
            self.m_image_out_port.add_attribute(name='edge_size',
                                                value=self.m_edge_size * pixscale,
                                                static=True)

class AngleInterpolationModule(ProcessingModule):
    """
    Module for calculating the parallactic angle values by interpolating between the begin and end
    value of a data cube.
    """

    __author__ = 'Markus Bonse, Tomas Stolker'

    @typechecked
    def __init__(self,
                 name_in: str,
                 data_tag: str) -> None:
        """
        Parameters
        ----------
        name_in : str
            Unique name of the module instance.
        data_tag : str
            Tag of the database entry for which the parallactic angles are written as attributes.

        Returns
        -------
        NoneType
            None
        """

        super().__init__(name_in)

        self.m_data_in_port = self.add_input_port(data_tag)
        self.m_data_out_port = self.add_output_port(data_tag)

    @typechecked
    def run(self) -> None:
        """
        Run method of the module. Calculates the parallactic angles of each frame by linearly
        interpolating between the start and end values of the data cubes. The values are written
        as attributes to *data_tag*. A correction of 360 deg is applied when the start and end
        values of the angles change sign at +/-180 deg.

        Returns
        -------
        NoneType
            None
        """

        parang_start = self.m_data_in_port.get_attribute('PARANG_START')
        parang_end = self.m_data_in_port.get_attribute('PARANG_END')

        steps = self.m_data_in_port.get_attribute('NFRAMES')

        if 'NDIT' in self.m_data_in_port.get_all_non_static_attributes():
            ndit = self.m_data_in_port.get_attribute('NDIT')

            if not np.all(ndit == steps):
                warnings.warn('There is a mismatch between the NDIT and NFRAMES values. The '
                              'parallactic angles are calculated with a linear interpolation by '
                              'using NFRAMES steps. A frame selection should be applied after '
                              'the parallactic angles are calculated.')

        new_angles = []

        start_time = time.time()
        for i, _ in enumerate(parang_start):
            progress(i, len(parang_start), 'Interpolating parallactic angles...', start_time)

            if parang_start[i] < -170. and parang_end[i] > 170.:
                parang_start[i] += 360.

            elif parang_end[i] < -170. and parang_start[i] > 170.:
                parang_end[i] += 360.

            if steps[i] == 1:
                new_angles = np.append(new_angles,
                                       [(parang_start[i] + parang_end[i])/2.])

            elif steps[i] != 1:
                new_angles = np.append(new_angles,
                                       np.linspace(parang_start[i],
                                                   parang_end[i],
                                                   num=steps[i]))

        self.m_data_out_port.add_attribute('PARANG',
                                           new_angles,
                                           static=False)

class SortParangModule(ProcessingModule):
    """
    Module to sort the images and attributes with increasing ``INDEX``.
    """

    __author__ = 'Tomas Stolker'

    @typechecked
    def __init__(self,
                 name_in: str,
                 image_in_tag: str,
                 image_out_tag: str) -> None:
        """
        Parameters
        ----------
        name_in : str
            Unique name of the module instance.
        image_in_tag : str
            Database tag with the input data.
        image_out_tag : str
            Database tag where the output data will be stored. Should be different from
            ``image_in_tag``.

        Returns
        -------
        NoneType
            None
        """

        super().__init__(name_in)

        self.m_image_in_port = self.add_input_port(image_in_tag)
        self.m_image_out_port = self.add_output_port(image_out_tag)

    @typechecked
    def run(self) -> None:
        """
        Run method of the module. Sorts the images and attributes with increasing ``INDEX``.
        Therefore, the images are sorted by there original (usually chronological) order.

        Returns
        -------
        NoneType
            None
        """

        memory = self._m_config_port.get_attribute('MEMORY')
        index = self.m_image_in_port.get_attribute('INDEX')

        ndim = self.m_image_in_port.get_ndim()
        nimages = self.m_image_in_port.get_shape()[-3]

        index_new = np.zeros(index.shape, dtype=int)

        if 'PARANG' in self.m_image_in_port.get_all_non_static_attributes():
            parang = self.m_image_in_port.get_attribute('PARANG')
            parang_new = np.zeros(parang.shape)

        else:
            parang_new = None

        if 'STAR_POSITION' in self.m_image_in_port.get_all_non_static_attributes():
            star = self.m_image_in_port.get_attribute('STAR_POSITION')
            star_new = np.zeros(star.shape)

        else:
            star_new = None

        index_sort = np.argsort(index)

        frames = memory_frames(memory, nimages)

        start_time = time.time()

        for i, _ in enumerate(frames[:-1]):
            progress(i, len(frames[:-1]), 'Sorting images in time...', start_time)

            index_new[frames[i]:frames[i+1]] = index[index_sort[frames[i]:frames[i+1]]]

            if parang_new is not None:
                parang_new[frames[i]:frames[i+1]] = parang[index_sort[frames[i]:frames[i+1]]]

            if star_new is not None:
                star_new[frames[i]:frames[i+1]] = star[index_sort[frames[i]:frames[i+1]]]

            # HDF5 indexing elements must be in increasing order
            for item in index_sort[frames[i]:frames[i+1]]:
                if ndim == 3:
                    self.m_image_out_port.append(self.m_image_in_port[item, ], data_dim=3)

                elif ndim == 4:
                    self.m_image_out_port.append(self.m_image_in_port[:, item, ], data_dim=4)

        self.m_image_out_port.copy_attributes(self.m_image_in_port)
        self.m_image_out_port.add_history('SortParangModule', 'sorted by INDEX')
        self.m_image_out_port.add_attribute('INDEX', index_new, static=False)

        if parang_new is not None:
            self.m_image_out_port.add_attribute('PARANG', parang_new, static=False)

        if star_new is not None:
            self.m_image_out_port.add_attribute('STAR_POSITION', star_new, static=False)

        self.m_image_out_port.close_port()

class AngleCalculationModule(ProcessingModule):
    """
    Module for calculating the parallactic angles. The start time of the observation is taken and
    multiples of the exposure time are added to derive the parallactic angle of each frame inside
    the cube. Instrument specific overheads are included.
    """

    __author__ = 'Alexander Bohn, Tomas Stolker'

    @typechecked
    def __init__(self,
                 name_in: str,
                 data_tag: str,
                 instrument: str = 'NACO') -> None:
        """
        Parameters
        ----------
        name_in : str
            Unique name of the module instance.
        data_tag : str
            Tag of the database entry for which the parallactic angles are written as attributes.
        instrument : str
            Instrument name ('NACO', 'SPHERE/IRDIS', or 'SPHERE/IFS').

        Returns
        -------
        NoneType
            None
        """

        super().__init__(name_in)

        # Parameters
        self.m_instrument = instrument

        # Set parameters according to choice of instrument
        if self.m_instrument == 'NACO':

            # pupil offset in degrees
            self.m_pupil_offset = 0.            # No offset here

            # no overheads in cube mode, since cube is read out after all individual exposures
            # see NACO manual page 62 (v102)
            self.m_O_START = 0.
            self.m_DIT_DELAY = 0.
            self.m_ROT = 0.

            # rotator offset in degrees
            self.m_rot_offset = 89.44           # According to NACO manual page 65 (v102)

        elif self.m_instrument == 'SPHERE/IRDIS':

            # pupil offset in degrees
            self.m_pupil_offset = -135.99       # According to SPHERE manual page 64 (v102)

            # overheads in cube mode (several NDITS) in hours
            self.m_O_START = 0.3 / 3600.        # According to SPHERE manual page 90/91 (v102)
            self.m_DIT_DELAY = 0.1 / 3600.      # According to SPHERE manual page 90/91 (v102)
            self.m_ROT = 0.838 / 3600.          # According to SPHERE manual page 90/91 (v102)

            # rotator offset in degrees
            self.m_rot_offset = 0.              # no offset here

        elif self.m_instrument == 'SPHERE/IFS':

            # pupil offset in degrees
            self.m_pupil_offset = -135.99 - 100.48  # According to SPHERE manual page 64 (v102)

            # overheads in cube mode (several NDITS) in hours
            self.m_O_START = 0.3 / 3600.            # According to SPHERE manual page 90/91 (v102)
            self.m_DIT_DELAY = 0.2 / 3600.          # According to SPHERE manual page 90/91 (v102)
            self.m_ROT = 1.65 / 3600.               # According to SPHERE manual page 90/91 (v102)

            # rotator offset in degrees
            self.m_rot_offset = 0.                  # no offset here

        else:
            raise ValueError('The instrument argument should be set to either \'NACO\', '
                             '\'SPHERE/IRDIS\', or \'SPHERE/IFS\'.')

        self.m_data_in_port = self.add_input_port(data_tag)
        self.m_data_out_port = self.add_output_port(data_tag)

    @typechecked
    def _attribute_check(self,
                         ndit: np.ndarray,
                         steps: np.ndarray) -> None:

        if not np.all(ndit == steps):
            warnings.warn('There is a mismatch between the NDIT and NFRAMES values. A frame '
                          'selection should be applied after the parallactic angles are '
                          'calculated.')

        if self.m_instrument == 'SPHERE/IFS':
            warnings.warn('AngleCalculationModule has not been tested for SPHERE/IFS data.')

        if self.m_instrument in ('SPHERE/IRDIS', 'SPHERE/IFS'):

            if self._m_config_port.get_attribute('RA') != 'ESO INS4 DROT2 RA':

                warnings.warn('For SPHERE data it is recommended to use the header keyword '
                              '\'ESO INS4 DROT2 RA\' to specify the object\'s right ascension. '
                              'The input will be parsed accordingly. Using the regular '
                              '\'RA\' keyword will lead to wrong parallactic angles.')

            if self._m_config_port.get_attribute('DEC') != 'ESO INS4 DROT2 DEC':

                warnings.warn('For SPHERE data it is recommended to use the header keyword '
                              '\'ESO INS4 DROT2 DEC\' to specify the object\'s declination. '
                              'The input will be parsed accordingly. Using the regular '
                              '\'DEC\' keyword will lead to wrong parallactic angles.')

    @typechecked
    def run(self) -> None:
        """
        Run method of the module. Calculates the parallactic angles from the position of the object
        on the sky and the telescope location on earth. The start of the observation is used to
        extrapolate for the observation time of each individual image of a data cube. The values
        are written as PARANG attributes to *data_tag*.

        Returns
        -------
        NoneType
            None
        """

        # Load cube sizes
        steps = self.m_data_in_port.get_attribute('NFRAMES')
        ndit = self.m_data_in_port.get_attribute('NDIT')

        self._attribute_check(ndit, steps)

        # Load exposure time [hours]
        exptime = self.m_data_in_port.get_attribute('DIT')/3600.

        # Load telescope location
        tel_lat = self.m_data_in_port.get_attribute('LATITUDE')
        tel_lon = self.m_data_in_port.get_attribute('LONGITUDE')

        # Load temporary target position
        tmp_ra = self.m_data_in_port.get_attribute('RA')
        tmp_dec = self.m_data_in_port.get_attribute('DEC')

        # Parse to degree depending on instrument
        if 'SPHERE' in self.m_instrument:

            # get sign of declination
            tmp_dec_sign = np.sign(tmp_dec)
            tmp_dec = np.abs(tmp_dec)

            # parse RA
            tmp_ra_s = tmp_ra % 100
            tmp_ra_m = ((tmp_ra - tmp_ra_s) / 1e2) % 100
            tmp_ra_h = ((tmp_ra - tmp_ra_s - tmp_ra_m * 1e2) / 1e4)

            # parse DEC
            tmp_dec_s = tmp_dec % 100
            tmp_dec_m = ((tmp_dec - tmp_dec_s) / 1e2) % 100
            tmp_dec_d = ((tmp_dec - tmp_dec_s - tmp_dec_m * 1e2) / 1e4)

            # get RA and DEC in degree
            ra = (tmp_ra_h + tmp_ra_m / 60. + tmp_ra_s / 3600.) * 15.
            dec = tmp_dec_sign * (tmp_dec_d + tmp_dec_m / 60. + tmp_dec_s / 3600.)

        else:
            ra = tmp_ra
            dec = tmp_dec

        # Load start times of exposures
        obs_dates = self.m_data_in_port.get_attribute('DATE')

        # Load pupil positions during observations
        if self.m_instrument == 'NACO':
            pupil_pos = self.m_data_in_port.get_attribute('PUPIL')

        elif self.m_instrument == 'SPHERE/IRDIS':
            pupil_pos = np.zeros(steps.shape)

        elif self.m_instrument == 'SPHERE/IFS':
            pupil_pos = np.zeros(steps.shape)

        new_angles = np.array([])
        pupil_pos_arr = np.array([])

        start_time = time.time()

        # Calculate parallactic angles for each cube
        for i, tmp_steps in enumerate(steps):
            progress(i, len(steps), 'Calculating parallactic angles...', start_time)

            t = Time(obs_dates[i].decode('utf-8'),
                     location=EarthLocation(lat=tel_lat, lon=tel_lon))

            sid_time = t.sidereal_time('apparent').value

            # Extrapolate sideral times from start time of the cube for each frame of it
            sid_time_arr = np.linspace(sid_time+self.m_O_START,
                                       (sid_time+self.m_O_START) +
                                       (exptime+self.m_DIT_DELAY + self.m_ROT)*(tmp_steps-1),
                                       tmp_steps)

            # Convert to degrees
            sid_time_arr_deg = sid_time_arr * 15.

            # Calculate hour angle in degrees
            hour_angle = sid_time_arr_deg - ra[i]

            # Conversion to radians:
            hour_angle_rad = np.deg2rad(hour_angle)
            dec_rad = np.deg2rad(dec[i])
            lat_rad = np.deg2rad(tel_lat)

            p_angle = np.arctan2(np.sin(hour_angle_rad),
                                 (np.cos(dec_rad)*np.tan(lat_rad) -
                                  np.sin(dec_rad)*np.cos(hour_angle_rad)))

            new_angles = np.append(new_angles, np.rad2deg(p_angle))
            pupil_pos_arr = np.append(pupil_pos_arr, np.ones(tmp_steps)*pupil_pos[i])

        # Correct for rotator (SPHERE) or pupil offset (NACO)
        if self.m_instrument == 'NACO':
            # See NACO manual page 65 (v102)
            new_angles_corr = new_angles - (90. + (self.m_rot_offset-pupil_pos_arr))

        elif self.m_instrument == 'SPHERE/IRDIS':
            # See SPHERE manual page 64 (v102)
            new_angles_corr = new_angles - self.m_pupil_offset

        elif self.m_instrument == 'SPHERE/IFS':
            # See SPHERE manual page 64 (v102)
            new_angles_corr = new_angles - self.m_pupil_offset

        indices = np.where(new_angles_corr < -180.)[0]
        if indices.size > 0:
            new_angles_corr[indices] += 360.

        indices = np.where(new_angles_corr > 180.)[0]
        if indices.size > 0:
            new_angles_corr[indices] -= 360.

        self.m_data_out_port.add_attribute('PARANG', new_angles_corr, static=False)

class SDIpreparationModule(ProcessingModule):
    """
    Module for preparing continuum frames for dual-band simultaneous differential imaging.
    """

    __author__ = 'Gabriele Cugno, Tomas Stolker'

    @typechecked
    def __init__(self,
                 name_in: str,
                 image_in_tag: str,
                 image_out_tag: str,
                 wavelength: Tuple[float, float],
                 width: Tuple[float, float]) -> None:
        """
        Parameters
        ----------
        name_in : str
            Unique name of the module instance.
        image_in_tag : str
            Tag of the database entry that is read as input.
        image_out_tag : str
            Tag of the database entry that is written as output. Should be different from
            *image_in_tag*.
        wavelength : tuple(float, float)
            The central wavelengths of the line and continuum filter, (line, continuum), in
            arbitrary but identical units.
        width : tuple(float, float)
            The equivalent widths of the line and continuum filter, (line, continuum), in
            arbitrary but identical units.

        Returns
        -------
        NoneType
            None
        """

        super().__init__(name_in)

        self.m_image_in_port = self.add_input_port(image_in_tag)
        self.m_image_out_port = self.add_output_port(image_out_tag)

        self.m_line_wvl = wavelength[0]
        self.m_cnt_wvl = wavelength[1]

        self.m_line_width = width[0]
        self.m_cnt_width = width[1]

    @typechecked
    def run(self) -> None:
        """
        Run method of the module. Normalizes the images for the different filter widths,
        upscales the images, and crops the images to the initial image shape in order to
        align the PSF patterns.

        Returns
        -------
        NoneType
            None
        """

        wvl_factor = self.m_line_wvl/self.m_cnt_wvl
        width_factor = self.m_line_width/self.m_cnt_width

        nimages = self.m_image_in_port.get_shape()[0]

        start_time = time.time()
        for i in range(nimages):
            progress(i, nimages, 'Preparing images for dual-band SDI...', start_time)

            image = self.m_image_in_port[i, ]

            im_scale = width_factor * scale_image(image, wvl_factor, wvl_factor)

            if i == 0:
                npix_del = im_scale.shape[-1] - image.shape[-1]

                if npix_del % 2 == 0:
                    npix_del_a = int(npix_del/2)
                    npix_del_b = int(npix_del/2)

                else:
                    npix_del_a = int((npix_del-1)/2)
                    npix_del_b = int((npix_del+1)/2)

            im_crop = im_scale[npix_del_a:-npix_del_b, npix_del_a:-npix_del_b]

            if npix_del % 2 == 1:
                im_crop = shift_image(im_crop, (-0.5, -0.5), interpolation='spline')

            self.m_image_out_port.append(im_crop, data_dim=3)

        history = f'(line, continuum) = ({self.m_line_wvl}, {self.m_cnt_wvl})'
        self.m_image_out_port.copy_attributes(self.m_image_in_port)
        self.m_image_out_port.add_history('SDIpreparationModule', history)
        self.m_image_in_port.close_port()