from anonymization_modules.anonymization import convert_value
from anonymization_modules.constraint_solver_module import constraint_solver

excluding_fields = ['Sex', 'Makes', 'Capital-Gain', 'Capital-Loss']

# generate the constraints according to the No Field Repeat algorithm
def no_field_repeat(bucket):
    set_of_constraints = []
    for t in bucket:
        for key, value in t.items():
            if key not in excluding_fields:
                set_of_constraints += [(key, '!=', value)]
    return set_of_constraints

# generate the constraints according to the No Tuple Repeat algorithm, taking first field to do that
def no_tuple_repeat(bucket):
    set_of_constraints = []
    for t in bucket:
        set_of_constraints += [('Age', '!=', t['Age'])]
    return set_of_constraints

# this method select the algorithm to apply on the dataset, generate all the constraints and invoke the solver
# method to obtain the released dataset
def constraint_generation(buckets, type_of_algorithm):
    released_dataset = []

    for pc, B in buckets.items():
        if type_of_algorithm == 'P-F':
            set_of_constraints = no_field_repeat(B)
        else:
            set_of_constraints = no_tuple_repeat(B)

        for s in pc.split('|'):
            result = eval(s)
            temp = convert_value(result[0], result[2])
            set_of_constraints += [(result[0], result[1], temp)]

        result_solution = constraint_solver(set_of_constraints)

        # here check if the solver have produced a solution, if yes can add to the released dataset
        if result_solution:
            released_dataset += [result_solution]

    return released_dataset


import math
import os
import numpy as np

SS = "\\"
NEW_LINE = "\n"
RESULTS_HEAD = "heads"
STATS_FOLDER = "statistics"
TASKS = 4 + 1  # The actual count is 4 but for boundary reasons added +1
VIDEOS = 15 + 1  # for boundary reasons added +1
USERS = 30 + 1  # for boundary reasons added +1

def record_stats(file, speeds):
	stat_file = open(STATS_FOLDER + SS + file + '.txt', 'w')
	quantiles = np.quantile(speeds, [0.25, 0.5, 0.75])
	mean = round(np.mean(speeds), 2)
	stat_file.write(str(mean) + NEW_LINE)
	for q in quantiles:
		stat_file.write(str(q) + NEW_LINE)

def to_float(list_):
	return list(map(float, list_.replace(NEW_LINE, '').split()))

def get_diffs(prev_line, curr_line):
	prev_line, curr_line = to_float(prev_line), to_float(curr_line)
	return [curr_line[0] - prev_line[0], prev_line[1] - curr_line[1], prev_line[2] - curr_line[2]]

def calculate_speed(prev_line, curr_line):
	diffs = get_diffs(prev_line, curr_line)
	return round(math.sqrt(pow(diffs[1], 2) + pow(diffs[2], 2)) / (diffs[0] / 1000), 2)

def record(file, list_, speed):
	file.write(str(speed) + NEW_LINE)  # speed is 0 on the first line
	list_.append(speed)

def write_stats(files):
	task_1_speeds = []
	task_2_speeds = []
	task_3_speeds = []
	task_4_speeds = []
	for file in files:
		if 'Task_1' in file:
			speeds = task_1_speeds
		elif 'Task_2' in file:
			speeds = task_2_speeds
		elif 'Task_3' in file:
			speeds = task_3_speeds
		else:
			speeds = task_4_speeds
		speed_file = open(STATS_FOLDER + SS + file, 'w')
		head_file = open(RESULTS_HEAD + SS + file)
		lines = head_file.readlines()
		record(speed_file, speeds, 0)  # speed is 0 on the first line
		for i in range(1, len(lines)):
			prev_line, curr_line = lines[i - 1], lines[i]
			speed = calculate_speed(prev_line, curr_line)
			record(speed_file, speeds, speed)

	record_stats('stats_task_1', task_1_speeds)
	record_stats('stats_task_2', task_2_speeds)
	record_stats('stats_task_3', task_3_speeds)
	record_stats('stats_task_4', task_4_speeds)

def get_file_names():
	users = [1]
	user_count = 30 + 1
	video_per_user = 1
	tasks = [1, 2, 3, 4]
	# tasks = [1, 3]
	files = []
	for user_start in users:
		for user in range(user_start, user_count, 5):
			for video in range(user_start, user_start + video_per_user):
				for task in tasks:
					u = '0' + str(user) if user < 10 else str(user)
					v = '0' + str(video) if video < 10 else str(video)
					file_name = 'User_' + str(u) + '_Video_' + str(v) + '_Task_' + str(task)
					files.append(file_name)
	return files

def init():
	try:
		os.makedirs(STATS_FOLDER)
	except:
		print("statistics folder is exist. Dont forget to create a backup")

if __name__ == '__main__':
	init()
	files = get_file_names()
	write_stats(files)


import json, time, datetime
from MyMQTT_Reqs import MyMQTT, MyRequest
import requests

class MoistController:
    def __init__(self,userID, conf_file = 'controller_conf.json'):
        with open(conf_file, 'r') as file:
            conf = json.load(file)
        self.rc_add = conf['rc_address']
        self.rc_port = conf['rc_port']

        self.my_req = MyRequest(
            web_server= self.rc_add,
            web_server_port = self.rc_port
            )
        broker, port, baseTopic = self.my_req.get_broker()
        self.baseTopic = baseTopic
        self.topic = f'{baseTopic}/data/#'
        self.temp_values = []
        self.mois_values = []
        self.pre_times = 5
        self.status = ''
        self.IDs={'user':'','farm':'','section':''}
        self.client = MyMQTT(userID,broker, port,notifier=self)
        self.telegram_message={"alert":"", "farm":"", "section":""}
        self.alarm_topic = f'{baseTopic}/alarm/'

        self.static_rc = requests.get(f'http://{self.rc_add}:{self.rc_port}/services/statis_webserver').json()

        TS_API = requests.get(f'http://{self.rc_add}:{self.rc_port}/services/third_party').json()
        self.TS_API_address = TS_API['address']

    def start(self):
        self.client.start()
        self.client.mySubscribe(self.topic)

    def stop(self):
        self.stop()

    def notify(self, topic, message):
        msg = json.loads(message)
        # print(f'Received message :\n{msg}\nfrom topic: {topic}\n')

        self.IDs['farm'] = msg['farmID']
        self.IDs['section'] = msg['sectionID']
        # for msg_event in msg['e']:
        #     self.my_req.put_sen_val(self.IDs, msg_event['n'], msg_event['value'])

        control_status = self.my_req.get_control_status(self.IDs)
        if control_status == 'auto':
            for msg_event in msg['e']:
                if msg_event['n'] == 'Temperature':
                    tmp_value = msg_event['value']
                    self.temp_values.append(tmp_value)
                elif msg_event['n'] == 'Soil_Moisture':
                    mois_value = msg_event['value']
                    self.mois_values.append(mois_value)

            if len(self.temp_values) == len(self.mois_values) and len(self.mois_values) >1:
                self.ControlServo(self.temp_values, self.mois_values)

        elif control_status == 'manual':
            self.checkalarm(msg)
            self.manual_control()

    def manual_control(self):
        _schedul = self.my_req.get_manual_schedul(self.IDs)
        if _schedul["timers"] != []:
            for period in _schedul["timers"]:
                # print(period["days"])
                days = period["days"][1:] + [period["days"][0]]
                # print(days)
                weekday = [i for i in range(len(days)) if days[i] == 1]
                for i in weekday:
                    _time_now = datetime.datetime.now()
                    if i == _time_now.weekday():
                        _time_start = datetime.datetime.fromtimestamp(period["starttime"]/1000)
                        _time_stop = datetime.datetime.fromtimestamp(period["endtime"]/1000)
                        if _time_start.hour <= _time_now.hour <= _time_stop.hour:
                            if _time_start.minute <= _time_now.minute <= _time_stop.minute:
                                self.status = 'on'
                            else:
                                    self.status = 'off'
                        else:
                            self.status = 'off'

                        # prev_status = self.my_req.get_status(self.IDs)
                        ch_ID = requests.get(f'http://{self.rc_add}:{self.rc_port}/catalog/channelID?farmID={self.IDs["farm"]}&sectionID={self.IDs["section"]}').json()

                        value = requests.get(f"{self.TS_API_address}/{ch_ID['ch_ID']}/fields/3.json?results=1").json()

                        prev_status = value["feeds"][-1]["field3"]

                        if self.status != prev_status:
                            # self.my_req.put_status(self.IDs,self.status)
                            # print(f'------>>>> become {self.status} <<<<<--------')
                            self.sendActStatus(self.status)

    def ControlServo(self,temp_values, mois_values):
        if len(temp_values) > self.pre_times:
            temp_values.pop(0)
            mois_values.pop(0)
        mean_temp = sum(temp_values)/len(temp_values)
        mean_mois = sum(mois_values)/ len(mois_values)

        thresh = self.my_req.get_threshold(self.IDs)
        thresh['temp'], thresh['mois_min'], thresh['mois_max']
        # print(f'Temp Thresh:{thresh["temp"]}  Mois min Thresh:{thresh["mois_min"]} Mois max Thresh:{thresh["mois_max"]}')
        # print(f'mean Temp:{mean_temp}   mean Mois: {mean_mois}')

        if mean_mois > thresh["mois_min"]:
            if mean_mois < thresh["mois_max"]:
                if mean_temp > thresh["temp"]:
                    self.status = 'on'
                else:
                    self.status = 'off'
            else:
                self.status = 'off'
        else:
            self.status = 'on'

        ch_ID = requests.get(f'http://{self.rc_add}:{self.rc_port}/catalog/channelID?farmID={self.IDs["farm"]}&sectionID={self.IDs["section"]}').json()

        value = requests.get(f"{self.TS_API_address}/{ch_ID['ch_ID']}/fields/3.json?results=1").json()

        prev_status = value["feeds"][-1]["field3"]
        # prev_status = self.my_req.get_status(self.IDs)

        if self.status != prev_status:
            # self.my_req.put_status(self.IDs,self.status)
            # print(f'------>>>> become {self.status} <<<<<--------')
            self.sendActStatus(self.status)

    def sendActStatus(self, pump_state):
        topic = '/'.join([self.baseTopic, self.IDs['farm'],
                              self.IDs['section'], 'Devices', 'Pump_status'])
        # print(topic)
        __message = {
            'farmID': self.IDs['farm'],
            'sectionID': self.IDs['section'],
            'bn': '',
            'e': [{'n': '', 'value': '', 'timestamp': '', 'unit': ''}]
        }
        __message['bn'] = 'Pump_status'
        __message['e'][0]['n'] = 'pump'
        __message['e'][0]['value'] = pump_state
        __message['e'][0]['timestamp'] = str('{:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()))
        __message['e'][0]['unit'] = 'boolean'

        self.client.myPublish(topic, __message)

        # self.my_req.post_status(self.IDs, pump_state)
        print('**************************')
        requests.post(f'http://{self.static_rc["st_address"]}:{int(self.static_rc["st_port"])}/statistic?farmID={self.IDs["farm"]}&sectionID={self.IDs["section"]}&status={pump_state}')

        print('**********************************')
        print(f'post :\n http://{self.static_rc["st_address"]}:{int(self.static_rc["st_port"])}/statistic?farmID={self.IDs["farm"]}&sectionID={self.IDs["section"]}&status={pump_state}')

    def checkalarm(self, msg):
        for msg_event in msg['e']:
            if msg_event['n'] == 'Soil_Moisture':
                thresh_min_mois = self.my_req.get_threshold(self.IDs)['mois_min']
                if msg_event['value'] < thresh_min_mois:
                    # print(f"alarm {msg['farmID']} {msg['sectionID']}")
                    self.telegram_message['alert'] = 'The sensor of Soil Moisture is under the threshold'
                    self.telegram_message['farm'] = msg['farmID']
                    self.telegram_message['section'] = msg['sectionID']
                    self.client.myPublish(self.alarm_topic, self.telegram_message)

if __name__ == "__main__":

    test = MoistController("hh", conf_file = 'controller_conf.json')
    test.start()

    while True:
        time.sleep(1)


import numpy as np
import wave
import sys
import argparse
from contextlib import closing
from struct import pack
from pylab import *
# from itertools import *

nchannels = 0
framerate = 0
nframes = 0
width = 0

def pcm2float(sig, dtype=np.float64):
    """
    Convert PCM signal to floating point with a range from -1 to 1.

    Use dtype=np.float32 for single precision.

    Parameters
    ----------
    sig : array_like
        Input array, must have (signed) integral type.
    dtype : data-type, optional
        Desired (floating point) data type.

    Returns
    -------
    ndarray
        normalized floating point data.
    """

    sig = np.asarray(sig)  # make sure it's a NumPy array
    assert sig.dtype.kind == 'i', "'sig' must be an array of signed integers!"
    dtype = np.dtype(dtype)  # allow string input (e.g. 'f')

    # Note that 'min' has a greater (by 1) absolute value than 'max'!
    # Therefore, we use 'min' here to avoid clipping.
    return sig.astype(dtype) / dtype.type(-np.iinfo(sig.dtype).min)

def read_data(f, verbose, plotf):
    global nchannels, framerate, nframes, width
    with closing(wave.open(f)) as w:
        nchannels = w.getnchannels()
        framerate = w.getframerate()
        nframes = w.getnframes()
        width = w.getsampwidth()

        print("sampling rate = {0} Hz, length = {1} samples, channels = {2}, sample \
            width = {3} bytes".format(framerate, nframes, nchannels, width))
        data = w.readframes(nframes)

    sig = np.frombuffer(data, dtype='<i2')
    sig = sig.reshape(-1, nchannels)
    if (verbose):
        print(sig)

    if (plotf):
        normalized = pcm2float(sig, float32)
        plot(normalized)
        show()

    return sig

def string2dec(str):
    return [ord(c) for c in str]

def padd_message(msg, nframes):
    pad_amount = nframes - len(msg)
    if (pad_amount > 0):
        msg+=[0]*(pad_amount)
    return msg

def normalize_data(data):
    normalized = []
    for arr in data:
        mean = sum(arr)//len(arr)
        normalized.append(mean)
    return normalized

def rot13(msg, normalized):
    for i in range(len(normalized)):
        if normalized[i]+255 < sys.maxsize:
            msg[i]+= normalized[i]
        else:
            msg[i]+= (normalized[i]-255)
    return msg

# TODO: grouper to chunk large files for more efficient writing
# def grouper(n, iterable, fillvalue=None):
#     "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
#     args = [iter(iterable)] * n
#     return zip_longest(fillvalue=fillvalue, *args)

def main():
    global plot_flag
    parser = argparse.ArgumentParser()
    parser.add_argument('-o', '--output', help="Name of file to ouput")
    parser.add_argument('-p', '--plot', action='store_true', help="Show Plot of Audio Channels")
    parser.add_argument('-t', '--text', help="Text to encode" )
    parser.add_argument('-v', '--verbose', action='store_true')
    parser.add_argument('filename', help="The file containing message or in which we wish to encode one.")
    args = parser.parse_args()

    if args.output == None:
        output = 'new.wav'
    else:
        output = args.output

    print('output file: ', output)

    data = read_data(args.filename, args.verbose, args.plot)

    if args.text == None:
        print('frames: {frames}'.format(frames=nframes))
        while True:
            word = input("> ")
            if len(word) <= nframes:
                break
            print('Error: Length of input is greater than {0}'.format(nframes))
    else:
        word = args.text
        if len(word) > nframes:
            print('Error: Length of input is greater than {0}, the max size of a message for this file'.format(nframes))
            return

    msg = padd_message(string2dec(word), nframes)

    msg = rot13(msg, normalize_data(data))

    if(args.verbose):
        print('encode: ', msg)

    transform = np.hstack((data, np.atleast_2d(msg).T))
    transform = np.reshape(transform, (np.product(transform.shape),))

    with closing(wave.open(output, 'w')) as w:
        w.setparams((nchannels+1, width, framerate, nframes, 'NONE', 'not compressed'))
        frames = b''.join(pack('h', int(sample)) for sample in transform)
        w.writeframes(frames)

if __name__ == '__main__':
    sys.exit(main())


from models import resnet_lc as resnet

import torch
import torch.optim as optim
from torchsummary import summary

import torchvision.datasets as dset
import torchvision.transforms as transforms
import torchvision.utils as vutils
from torch.autograd import Variable, grad
from torch import autograd
import numpy as np
import matplotlib.pyplot as plt

import os, time
from datetime import datetime
import logging

# Model Run Code
run_code = 'wgan-gp_LC_64'

# Define Optimizer
lr = 1e-4
beta1=.5
beta2=.9
batch_size = 64

num_epochs = 20

# noise length
nz = 128

n_critic = 5

image_size=64
num_classes=10
# Number of workers for dataloader
workers = 4

# Number of GPUs available. Use 0 for CPU mode.
ngpu = 1

# Print Model Structure.
print_model = False

# Logging..
log_dir='../logs/' + run_code + '/'
model_dir='../logs/' + run_code + '/model/'

# for Log
if not os.path.isdir(log_dir): os.makedirs(log_dir)
logging.basicConfig(filename=log_dir+'logging.log', level=logging.INFO)

# for Model Save
if not os.path.isdir(model_dir): os.makedirs(model_dir)

G_save_path = model_dir + 'generator.pth'
D_save_path = model_dir + 'critic.pth'

# Device configuration
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
use_cuda = torch.cuda.is_available()

# onehot converter
onehot = torch.zeros(num_classes, num_classes)
onehot = onehot.scatter_(1,
                         torch.LongTensor(list(range(num_classes)))
                         .view(num_classes,1),1)\
                            .view(num_classes, num_classes,1,1)

# random
noise_label = (torch.rand(batch_size, 1)*num_classes).type(torch.LongTensor).squeeze()
noise_label_onehot = onehot[noise_label]
noise_label_onehot = noise_label_onehot.to(device)

# Layer Conditional
netG = resnet.Generator64(resnet.ResBlock, num_classes=num_classes, nz_dim=nz, in_channels=64,
                                        labels=noise_label_onehot).to(device)
netD = resnet.Discriminator64(resnet.ResBlock, num_classes=num_classes, in_channels=64,
                                            labels=noise_label_onehot).to(device)

optimizerD = optim.Adam(netD.parameters(), lr=lr, betas=(beta1, beta2))
optimizerG = optim.Adam(netG.parameters(), lr=lr, betas=(beta1, beta2))

if print_model:
    summary(netG, input_size=[(128, 1, 1), (num_classes,1,1)])
    summary(netD, input_size=[(3, image_size, image_size), (num_classes,1,1)])

dataroot = "../data/"
# We can use an image folder dataset the way we have it setup.
# Create the dataset
dataset = dset.ImageFolder(root=dataroot,
                           transform=transforms.Compose([
                               transforms.Resize(image_size),
                               transforms.CenterCrop(image_size),
                               transforms.ToTensor(),
                               transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
                           ]))
# Create the dataloader
dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size,
                                         shuffle=True, num_workers=workers)

# Decide which device we want to run on
device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu")

# Gradient Penalty..
def calc_gradient_penalty(netD, real_data, fake_data, labels=None):
    alpha = torch.rand(batch_size, 1)
    alpha = alpha.expand(batch_size, int(real_data.nelement()/batch_size)).contiguous()
    alpha = alpha.view(batch_size, 3, 64, 64)
    alpha = alpha.to(device)

    fake_data = fake_data.view(batch_size, 3, 64, 64)
    interpolates = alpha * real_data.detach() + ((1 - alpha) * fake_data.detach())

    interpolates = interpolates.to(device)
    interpolates.requires_grad_(True)

    interp_label = labels

    disc_interpolates = netD(interpolates, interp_label)

    gradients = autograd.grad(outputs=disc_interpolates, inputs=interpolates,
                              grad_outputs=torch.ones(disc_interpolates.size()).to(device),
                              create_graph=True, retain_graph=True, only_inputs=True)[0]

    gradients = gradients.view(gradients.size(0), -1)
    gradient_penalty = ((gradients.norm(2, dim=1) - 1) ** 2).mean() * 10
    return gradient_penalty

# fixed noise for test
fixed_noise = torch.randn(batch_size, nz, 1, 1, device=device)

# Training Loop
# Lists to keep track of progress
img_list = []
G_losses = []
D_losses = []
GP = []

iters = 0

one = torch.FloatTensor([1])
mone = one * -1
one = one.cuda()
mone = mone.cuda()

print("Starting Training Loop...")
logging.debug('Starting Training Loop...')

# For each epoch
start_time = time.time()
for iters in range(num_epochs * len(dataloader)):
    # For each batch in the dataloader
    ############################
    # Train D
    ###########################
    for p in netD.parameters():  # reset requires_grad
        p.requires_grad = True  # they are set to False below in netG update

    for i in range(n_critic):
        real_data, real_label = next(iter(dataloader))
        netD.zero_grad()

        b_size = real_data.size(0)
        real_label = torch.tensor(np.array(real_label), dtype=torch.long)
        real_label_onehot = torch.tensor(np.eye(num_classes)[real_label], dtype=torch.long)
        real_label_onehot = real_label_onehot.view(real_label_onehot.shape[0], real_label_onehot.shape[1],1,1)
        if use_cuda:
            real_label_onehotv = Variable(real_label_onehot.type(torch.FloatTensor).cuda())

        # train with real
        if use_cuda:
            real_data = real_data.cuda()
        real_data_v = Variable(real_data)

        D_real = netD(real_data_v)
        D_real = D_real.mean()
        D_real= netD(real_data_v, real_label_onehotv)
        D_real.backward(mone)

        noise = torch.randn(b_size, nz, 1, 1, device=device)

        with torch.no_grad():
            noisev = Variable(noise)  # totally freeze netG

        noise_label = (torch.rand(batch_size, 1)*num_classes).type(torch.LongTensor).squeeze()
        noise_label_onehot = onehot[noise_label]
        if use_cuda:
            noise_label_onehot = noise_label_onehot.cuda()
        with torch.no_grad():
            noise_label_onehotv = Variable(noise_label_onehot.type(torch.FloatTensor).cuda())

        fake = Variable(netG(noisev, noise_label_onehotv).data)
        D_fake = netD(fake, noise_label_onehotv)
        D_fake = D_fake.mean()
        D_fake.backward(one)

        # train with gradient penalty
        gradient_penalty = calc_gradient_penalty(netD, real_data_v.data, fake.data, real_label_onehotv)
        GP_cost = gradient_penalty

        D_cost = D_fake - D_real + GP_cost
        GP_cost.backward()

        optimizerD.step()

        D_losses.append(D_cost.cpu().data.numpy())
        GP.append(GP_cost.cpu().data.numpy())

    ############################
    # Train G
    ###########################
    for p in netD.parameters():
        p.requires_grad = False  # to avoid computation
    netG.zero_grad()

    noise = torch.randn(batch_size, nz, 1, 1, device=device)
    noisev = autograd.Variable(noise)
    noise_label = (torch.rand(batch_size, 1)*num_classes).type(torch.LongTensor).squeeze()
    noise_label_onehot = onehot[noise_label]
    if use_cuda:
        noise_label_onehot = noise_label_onehot.cuda()
        noise_label_onehotv = Variable(noise_label_onehot.type(torch.FloatTensor).cuda())

    fake = netG(noisev, noise_label_onehotv)
    G= netD(fake, noise_label_onehotv)
    G = G.mean()
    G.backward(mone)

    G_cost = -G
    optimizerG.step()

    G_losses.append(G_cost.cpu().data.numpy())

    # Output training stats
    if iters % 10 == 0:
        s = datetime.now().strftime('[%Y-%m-%d %H:%M:%S]')
        iter_time = time.time() - start_time  # 현재시각 - 시작시간 = 실행 시간
        start_time = time.time()
        print(s + ' [%d/%d]\t GP: %.4f\t D: %.4f\t G: %.4f\t %.4f sec'
              % (iters, num_epochs * len(dataloader), GP[-1], D_losses[-1], G_losses[-1], iter_time ))
        logging.info(s + ' [%d/%d]\t GP: %.4f\t D: %.4f\t G: %.4f\t %.4f sec'
              % (iters, num_epochs * len(dataloader), GP[-1], D_losses[-1], G_losses[-1], iter_time ))

        torch.save(netG.state_dict(), G_save_path)
        torch.save(netD.state_dict(), D_save_path)
    if iters % 10 == 0:
        vutils.save_image(fake.reshape([64,3,64,64]), log_dir + 'fake_curren.png', nrow=8, padding=2, normalize=True)
    iters += 1


from intcode import run
from intcode import ProgramState

# Initialize: open file, turn all op codes into integers
with open('day02.txt') as f:
    # split line into operation list
    opsAsStrings = f.read().split(",")
    # turn them all into integers
    ops = list(map(int, opsAsStrings))

# Part One: Find the value at position 0 after the program halts when position 1 is 12 and position 2 is 2
myOps = ops.copy()
myOps[1] = 12
myOps[2] = 2
state = run(ProgramState(myOps))
print ("Part One: %i" % state.ops[0])

# Part Two: Find the input noun and verb that cause the program to produce the output 19690720. What is 100 * noun + verb?
for i in range(0,98):
    for j in range(0,98):
        myOps = ops.copy()
        myOps[1] = i
        myOps[2] = j
        state = run(ProgramState(myOps))
        if state.ops[0] == 19690720:
            print ("Part Two: noun is %i, verb is %i. Answer is %i" % (i,j,100*i+j))


import csv
import sys
import diffevo
import bayesopt
import ga
import random_search
import grid_search
from tqdm import tqdm

def run_n_times(algorithm, segments, n, iters):
    """
    Perform a meta-experiment. The chosen algorithm is run n times for a given
    number of gradient segments. The CRF score per iteration and the cumulative
    runtime per iteration are written to csv files. Filepaths need to be specified
    manually and should indicate which sample was used in read_data.py. This has
    to be done manually because optimization algorithm packages don't allow
    for extra arguments in the objective function, other than the parameters
    to be optimized.

    :algorithm: Optimization algorithm. Choose from:
                BayesOpt/DiffEvo/GenAlgo/GridSearch/RandomSearch
    :segments: Number of gradient segments in the gradient profile.
    """

    filename = algorithm + "_" + str(segments) + "segments_sample1.csv"
    filename_runtime = algorithm + "_" + str(segments) + "segments" + "_runtime_sample1" + ".csv"
    filepath = "results/" + str(segments) + "segments/" + filename
    filepath_runtime = "results/" + str(segments) + "segments/" + filename_runtime

    if(algorithm == "BayesOpt"):

        for nth_experiment in tqdm(range(n)):
            # n = number of meta experiments
            return_list = bayesopt.bayesopt(iters, segments)
            func_vals = return_list[4]
            runtime_per_iteration = return_list[5]
            # write the data from the list to a (csv?) file as a single line
            f = open(filepath, 'a', newline ='\n')

            # writing the data into the file
            with f:
                writer = csv.writer(f)
                writer.writerow(func_vals)

            f = open(filepath_runtime, 'a', newline ='\n')

            # writing the data into the file
            with f:
                writer = csv.writer(f)
                writer.writerow(runtime_per_iteration)

    elif(algorithm == "DiffEvo"):

        for nth_experiment in tqdm(range(n)):
            # n = number of meta experiments
            return_list = diffevo.diffevo(iters, segments)

            func_vals = return_list[4]
            runtime_per_iteration = return_list[5]
            f = open(filepath, 'a', newline ='\n')

            with f:
                writer = csv.writer(f)
                writer.writerow(func_vals)

            f = open(filepath_runtime, 'a', newline ='\n')

            with f:
                writer = csv.writer(f)
                writer.writerow(runtime_per_iteration)

    elif(algorithm == "GenAlgo"):

        for nth_experiment in tqdm(range(n)):
            return_list = ga.ga(iters, segments)
            func_vals = return_list[4]
            runtime_per_iteration = return_list[5]
            f = open(filepath, 'a', newline ='\n')

            with f:
                writer = csv.writer(f)
                writer.writerow(func_vals)

            f = open(filepath_runtime, 'a', newline ='\n')

            with f:
                writer = csv.writer(f)
                writer.writerow(runtime_per_iteration)

    elif(algorithm == "RandomSearch"):

        for nth_experiment in tqdm(range(n)):
            # n = number of meta experiments
            return_list = random_search.run_rs(iters, segments)
            func_vals = return_list[4]
            runtime_per_iteration = return_list[5]
            f = open(filepath, 'a', newline ='\n')

            with f:
                writer = csv.writer(f)
                writer.writerow(func_vals)

            f = open(filepath_runtime, 'a', newline ='\n')

            with f:
                writer = csv.writer(f)
                writer.writerow(runtime_per_iteration)

    elif(algorithm == "GridSearch"):

        return_list = grid_search.grid_search(iters, segments)
        func_val = return_list[2]
        runtime = return_list[1]
        f = open(filepath, 'a', newline ='\n')

        with f:
            writer = csv.writer(f)
            writer.writerow([func_val])

        f = open(filepath_runtime, 'a', newline ='\n')

        with f:
            writer = csv.writer(f)
            writer.writerow([runtime])

def main():
    if len(sys.argv) > 5:
        print('You have specified too many arguments.')
        sys.exit()

    if len(sys.argv) < 5:
        print('Please specify the following parameters in order:')
        print("- Choose an optimization algorithm (BayesOpt/DiffEvo/GenAlgo/GridSearch/RandomSearch)")
        print("- Number of segments in the gradient profile")
        print("- Number of sub-experiments the meta-experiment should consist of")
        print("- Number of iterations. Note that if the chosen algorithm is grid search, this is the number of grid points per dimension.")
        sys.exit()

    algorithm = sys.argv[1]
    number_of_segments = int(sys.argv[2])
    sub_experiments = int(sys.argv[3])
    iterations = int(sys.argv[4])

    run_n_times(algorithm, number_of_segments, number_of_segments, iterations)

if __name__ == '__main__':
    main()


# (c) Nelen & Schuurmans.  GPL licensed, see LICENSE.rst.
from django.template.defaultfilters import slugify

import factory

from lizard_maptree.models import Category

class CategoryFactory(factory.DjangoModelFactory):
    """Factory for creating Category instances"""
    class Meta:
        model = Category

    @classmethod
    def _prepare(cls, create, **kwargs):
        name = kwargs.get('name', None)
        category = super(CategoryFactory, cls)._prepare(create, **kwargs)
        if name:
            category.slug = slugify(name)
            if create:
                category.save()
        return category


import shlex

from host.utils.sub_process import run_command
from host.exceptions import ProcessException

def chroot_command(chroot_path,
                   command,
                   command_input=None,
                   exception_class=ProcessException):

    if isinstance(command, str):
        command = shlex.split(command)

    return run_command('chroot', chroot_path, *command,
                       command_input=command_input,
                       exception_class=exception_class)


import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import LSTM
from keras.layers import RNN
from keras.utils import np_utils
from keras.callbacks import ModelCheckpoint

text = (open("dataset/dataset2.txt").read())
text=text.lower()

text=text.split('\n')
lenght=len(text)
sentences=[]
for  i  in range(lenght):
    temp=text[i].split("\t")
    if len(temp)>1:

        sentences.append(temp[1])
sentences=sentences[0:3000]
input_data_set=[]
words=[]

for i in sentences:
    temp=[]
    i=i[:-1]
    temp=i.split(" ")
    temp[-1]="."
    input_data_set.append(temp)
lengt2=len(input_data_set)
for i in range(lengt2):
    for j in range(len(input_data_set[i])):
        words.append(input_data_set[i][j])

words_set= list(set(words))
words_set.sort()
words_set.insert(0,' ')

num2word = {n:char for n, char in enumerate(words_set)}

word2num = {char:n for n, char in enumerate(words_set)}

X=[]
Y=[]
length=len(temp)
sequence_length=5

for sentence in input_data_set:
    length4=len(sentence)

    for i in range(length4):

        displacement=abs(sequence_length-(1))

        j=0
        for k in range(sequence_length):
            input_train=[]

            if not (abs(displacement)<0):

                for j in range(abs(displacement)):
                    input_train.append(word2num[' '])
            j=0

            while (len(input_train))!=sequence_length:

                if i+j >=length4:
                    j+=1
                    break
                input_train.append(word2num[sentence[i+j]])
                j+=1

            displacement-=1

            if len(input_train)==sequence_length:

                X.append(input_train)

                if (i+j)>=length4:
                    X.pop()
                else:
                    Y.append(word2num[sentence[i+j]])

for i in range(length-sequence_length):
    sequence=temp[i:i+sequence_length]
    label=temp[i+sequence_length]
    X.append([word2num[word] for word in sequence])
    Y.append(word2num[label])

X_modified = np.reshape(X, (len(X), sequence_length, 1))
X_modified = X_modified / float(len(words_set))
Y_modified = np_utils.to_categorical(Y)

model = Sequential()
model.add(LSTM(400, input_shape=(X_modified.shape[1], X_modified.shape[2]), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(400, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(400))
model.add(Dropout(0.2))
model.add(Dense(Y_modified.shape[1], activation='softmax'))

model.compile(loss='categorical_crossentropy', optimizer='adam')

#filepath="checkpoint_model_{epoch:02d}.hdf5"
#checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=False, mode='auto',period=5)
#callbacks_list = [checkpoint]

#model.fit(X_modified, Y_modified, validation_split=0.33, epochs=100, batch_size=64, callbacks=callbacks_list, verbose=1)
#model.fit(X_modified, Y_modified, epochs=100, batch_size=32)

#model.save_weights('flicker.h5')

model.load_weights('checkpoints/checkpoint_model_100.hdf5')

string_mapped = X[5]
while True:
    strings=input().split(' ')

    while (len(strings)<sequence_length):
        strings.insert(0,' ')
    if len(strings)>sequence_length:
        strings=strings[-4:]
    full_string=strings
    string_mapped=[]

    for i in range(sequence_length):
        string_mapped.append(word2num[strings[i]])

    # generating characters
    for i in range(20):
        x = np.reshape(string_mapped,(1,len(string_mapped), 1))
        x = x / float(len(words_set))
        pred_index = np.argmax(model.predict(x, verbose=0))
        seq = [num2word[value] for value in string_mapped]
        full_string.append(num2word[pred_index])

        string_mapped.append(pred_index)
        string_mapped = string_mapped[1:len(string_mapped)]
    txt=""
    for char in full_string:
        txt = txt+" "+ char
        if char==".":
            break
    print("Output :")
    print(txt)


num = int(input())
num2 = input().split()
num3 = [[int(num2[2*i]),int(num2[2*i+1])] for i in range(int(len(num2)/2))]
print(num3)
def merge(intervals):
    intervals.sort(key=lambda x: x[0])
    print(intervals)
    merged = []
    for interval in intervals:
        # 如果列表为空，或者当前区间与上一区间不重合，直接添加
        if not merged or merged[-1][1] < interval[0]:
            merged.append(interval)
        else:
            # 否则的话，我们就可以与上一区间进行合并
            merged[-1][1] = max(merged[-1][1], interval[1])
    output = [str(merged[i][j]) for i in range(len(merged)) for j in range(2)]
    output = " ".join(output)
    #print(*merged)
    #return output
print(merge(num3))
#1 3 2 6 8 10 15 18


from publisher.models import LayoutTemplate, Book, BookDownload, BookRating, BookPage, BookImage, BookComment, Translation,  Publisher
from serializers import LayoutTemplateSerializer, BookSerializer, BookPageSerializer, TranslationSerializer
from serializers import ImageSerializer, CommentSerializer
from rest_framework.decorators import detail_route, list_route, permission_classes
from rest_framework import status
from rest_framework.parsers import FormParser, MultiPartParser
from django.http import HttpResponse
from django.core.files.base import ContentFile
from rest_framework import viewsets
from django.http import FileResponse
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import authentication, permissions
from django.http import JsonResponse, FileResponse, Http404
from rest_framework import generics
from rest_framework.decorators import api_view, parser_classes
from django.db.models import Count, Max, Q
from django.conf import settings
from polli import constants
from users.models import User
import csv
import string
import random
import urllib2
import json
import time
import boto3
import json

# Layout Template - API
class LayoutTemplateViewSet(viewsets.ModelViewSet):
    queryset = LayoutTemplate.objects.all()
    serializer_class = LayoutTemplateSerializer

    def create(self, request):
        serializer = LayoutTemplateSerializer(data=request.data)
        if serializer.is_valid():
            user = self.request.user
            publisher = user.publisher if user.user_type == User.PUBLISHER else None
            serializer.save(publisher=publisher)
            return Response(serializer.data)

# Book - API Views
class BookViewSet(viewsets.ModelViewSet):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

    def create(self, request):
        serializer = BookSerializer(data=request.data)
        if serializer.is_valid():
            user = self.request.user
            publisher = user.publisher if user.user_type == User.PUBLISHER else None
            book = serializer.save(publisher=publisher)
            translation = request.data['translation']
            if translation:
                Translation.objects.create(book=book, language=translation)

            # Send Notification Email
            subject = 'Book Uploaded: ({}) {}'.format(book.id, book.name)
            heading = 'New Book Uploaded'
            book.send_upload_notification(subject=subject, heading=heading, request=request)
            return Response(serializer.data)
        else:
            return Response(serializer.errors)

    def list(self, request):
        books = Book.objects.filter(translations__status='published')
        page = self.paginate_queryset(books)
        serializer = BookSerializer(page, many=True)
        return self.get_paginated_response(serializer.data)

    @detail_route(methods=['get'])
    def get_book_bundle(self, request, pk=None):
            # Record Download
            primary_language = request.query_params['primary_language']
            secondary_language = request.query_params['secondary_language']
            BookDownload.objects.create(
                user=request.user,
                book_id=pk,
                primary_language=primary_language,
                secondary_language=secondary_language
            )

            # Get Bundle
            book = Book.objects.get(pk=pk)
            bundle_file_path = book.generate_bundle(primary_language=primary_language, secondary_language=secondary_language)
            return FileResponse(open(bundle_file_path, 'rb'), content_type='application/octet-stream')

    @detail_route(methods=['post'])
    def rate_book(self, request, pk):
            book_rating, created = BookRating.objects.get_or_create(user=request.user, book_id=pk)
            book_rating.rating = float(request.data['rating'])
            book_rating.save()
            return Response('Rate Book Complete')

    @detail_route(methods=['post'])
    def process_book(self, request, pk=None):
        book = Book.objects.get(pk=pk)
        book.process_book_pdf()
        serializer = BookSerializer(instance=book)
        return Response(serializer.data)

    @detail_route(methods=['post'])
    def get_book_processing_status(self, request, pk):
        book = Book.objects.get(pk=pk)
        serializer = BookSerializer(instance=book)
        return Response(serializer.data)

    @detail_route(methods=['post'])
    def update_book_cover(self, request, pk=None):
        book = Book.objects.get(pk=pk)
        book.set_cover_image(image_url=request.data['image_url'])
        serializer = BookSerializer(instance=book)
        return Response(serializer.data)

# Page - API Views
class PageViewSet(viewsets.ModelViewSet):
    queryset = BookPage.objects.all()
    serializer_class = BookPageSerializer

    def create(self, request):
        serializer = BookPageSerializer(data=request.data)
        if serializer.is_valid():
            book_id = request.data['book']
            order = Book.objects.get(pk=book_id).pages.aggregate(Max('order'))['order__max'] + 1
            serializer.save(order=order)
            return Response(serializer.data)
        else:
            return Response(serializer.errors)

    @list_route(methods=['post'])
    def update_page_sorting(self, request):
        for page in json.loads(request.data['pages']):
            BookPage.objects.filter(id=int(page['id'])).update(order=int(page['order']))
        return Response('Page Sorting Updated')

# Translation - API Views
class TranslationViewSet(viewsets.ModelViewSet):
    queryset = Translation.objects.all()
    serializer_class = TranslationSerializer

    def create(self, request):
        serializer = TranslationSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        else:
            return Response(serializer.errors)

    @detail_route(methods=['get'])
    def get_translation_csv(self, request, pk):
        translation_csv = HttpResponse(content_type='text/csv')
        translation_csv['Content-Disposition'] = 'attachment; filename="translation_{}.csv"'.format(pk)
        translation = Translation.objects.get(pk=pk)
        translation.generate_translation_csv(translation_csv)
        return translation_csv

    @detail_route(methods=['post'])
    def upload_translation_csv(self, request, pk=None):
        translation = Translation.objects.get(pk=pk)
        csv_data = ContentFile(request.FILES['csv'].read())
        translation.set_translation_csv(csv_data)
        translation.process_translation_csv()
        return Response('upload translation complete')

    @detail_route(methods=['post'])
    def update_translation_review(self, request, pk):
        data = request.data
        translation = Translation.objects.get(pk=pk)
        translation.update_translation_review(page_num=data['page_num'], line_num=data['line_num'], l2=data['l2'], complete=data['complete'])
        return Response('update translation review complete')

    @detail_route(methods=['post'])
    def update_blend_review(self, request, pk):
        data = request.data
        translation = Translation.objects.get(pk=pk)
        translation.update_blend_review(page_num=data['page_num'], line_num=data['line_num'], blend=data['blend'], complete=data['complete'])
        return Response('update blend review complete')

    @detail_route(methods=['post'])
    def create_blend_request(self, request, pk):
        # Update Translation
        translation = Translation.objects.get(pk=pk)
        translation.status = Translation.BLEND
        translation.blend_status = Translation.BLEND_PENDING
        translation.save()

        # Add Blend Request to SQS
        sqs = boto3.resource('sqs', region_name='us-east-1', aws_access_key_id=settings.AWS_ACCESS_KEY_ID, aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY)
        queue = sqs.get_queue_by_name(QueueName=settings.AWS_SQS_NAME)
        queue.send_message(MessageBody=json.dumps({'translation_id': pk}))
        return Response('create blend request')

    @list_route(permission_classes=[permissions.AllowAny], methods=['post'])
    def generate_blends(self, request):
        translation = Translation.objects.get(pk=request.data['translation_id'])
        translation.generate_blends()
        return Response('Generate Blends')

# Images - API Views
class ImageViewSet(viewsets.ModelViewSet):
    queryset = BookImage.objects.all()
    serializer_class = ImageSerializer

    def create(self, request):
        serializer = ImageSerializer(data=request.data)
        if serializer.is_valid():
            image = serializer.save()

            # Send Notification Email
            book = image.book
            subject = 'Image Uploaded: ({}) {}'.format(book.id, book.name)
            heading = 'New Image Uploaded'
            book.send_upload_notification(subject=subject, heading=heading, request=request)
            return Response(serializer.data)
        else:
            return Response(serializer.errors)

# Comment - API Views
class CommentViewSet(viewsets.ModelViewSet):
    queryset = BookComment.objects.all()
    serializer_class = CommentSerializer

    def create(self, request):
        serializer = CommentSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save(creator=request.user)
            return Response(serializer.data)
        else:
            return Response(serializer.errors)

    @detail_route(methods=['post'])
    def mark_comment_read(self, request, pk):
        comments = BookComment.objects.filter(Q(id=pk)|Q(parent_comment_id=pk))
        for c in comments:
            c.read_by.add(request.user)
        return Response('Comment Read')

    @detail_route(methods=['post'])
    def resolve_comment(self, request, pk):
        BookComment.objects.filter(Q(id=pk) | Q(parent_comment_id=pk)).update(status=BookComment.CLOSED_STATUS)
        comments.update(status=BookComment.CLOSED_STATUS)
        return Response('Comment Resolved')


# Problem
# Given: At most 10 DNA strings in FASTA format (of length at most 1 kbp each).
# Return: The ID of the string having the highest GC-content, followed by the GC-content of that string.
# Rosalind allows for a default error of 0.001 in all decimal answers unless otherwise stated;
# please see the note on absolute error below.

# alternative apporach
def gc_count(fastaname):
    # convert multiple line seq to one line
    seq_and_gc = {}  # {header : seq}
    header = ""
    seq = ""
    firstline = True

    # save fasta multiline seq to single line
    with open(fastaname) as f:
        for line in f:
            if line.startswith(">"):
                if firstline:
                    header = line[1:]
                    firstline = False
                else:
                    seq_and_gc[header.strip()] = seq
                    seq = ""
                    header = line[1:]
            else:
                seq += line.strip()

        seq_and_gc[header] = seq

        # count GC
        gc_content = {}  # {header : gc-percentage}
        for header, seq in seq_and_gc.items():
            count_c = seq.count("C")
            count_g = seq.count("G")
            perc = (count_c + count_g) / len(seq) * 100
            gc_content[header] = perc

        # print header and max gc
        for header, perc in gc_content.items():
            if perc == max(gc_content.values()):
                return header.strip(), gc_content[header]

fasta = "fasta4.txt"
header, gc = gc_count(fasta)
print(f"{header}\n{gc}")


from model.Utils import dump_result, stdout_log
from model.Trainer import ModelTrainer
import time

class Singleton(type):
    _instances = {}
    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
        else:
            cls._instances[cls].__init__(*args, **kwargs)
        return cls._instances[cls]

class Queue(metaclass=Singleton):

    def __init__(self):
        if not hasattr(self,'list'):
            self.list = []

    def insert(self, mt):
        if isinstance(mt, ModelTrainer):
            self.list.append(mt)
        else:
            stdout_log("invalid varible insertion to Queue, breaking...")

    def pop(self):
        self.list.pop(0)

    def monitor(self):
        while True:
            if len(self.list) > 0 and self.list[0].result is None:
                self.list[0].execute()
                dump_result(self.list[0])
                self.pop()
            time.sleep(5)

    def get_current_queue(self):
        return [str(mt) for mt in self.list]


#Functions and classes for general use.
import urllib.parse
import engine.signal as signal
import sys, inspect

ERR_PAGE_HREF = "./engine/html/error.html"
err_page = signal.ValueLoader(ERR_PAGE_HREF)

BASE_URL_HREF = "../public/base_url.txt"
base_url = signal.ValueLoader(BASE_URL_HREF)

###URL CHECKERS###

#Function to check if a given "path" begins with a given "sought".
#NOTE: If "sought" does not end with a "/", returns False.
def isAPI(path,sought):
    #Check if "sought" ends with a "/".
    if sought[-1] != "/":
        return False
    else:
        url_pieces = urllib.parse.urlparse(path)
        my_path = url_pieces.path

        #Check if "path" begins with "sought".
        if my_path[:len(sought)] == sought:
            return True
        else:
            return False

#Function to check if a given "path" equals a given "sought".
def isEndpoint(path,sought,returnVar="True"):
    """
    Check if the path of a url is the same as the value of "sought", (i.e. check if "path" and "sort" have the same API endpoint), and return either a value of False, or an affirmative value of the requested type.

    Parameters
    ----------
    path : str
        Required: the url to be evaluated.
    sought : str
        Required: the endpoint path for the url to be checked against.
    returnVar : str = "True"
        Optional: the sort of value to return. Options are:
            "True": returns True.
            "Pieces": returns the pieces of the parsed url.
            "Queries": returns the parsed queries from the url query string.
            "Path": returns the path that was evaluated.
            "QTuple": returns a tuple of (True, parsed queries)
        Returns True if any other value is given.
    """
    url_pieces = urllib.parse.urlparse(path)
    my_path = url_pieces.path

    if my_path == sought:
        if returnVar == "True":
            return True
        elif returnVar == "Pieces":
            return url_pieces
        elif returnVar == "Queries":
            queries = urllib.parse.parse_qs(url_pieces.query)
            return queries
        elif returnVar == "Path":
            return path
        elif returnVar == "QTuple":
            queries = urllib.parse.parse_qs(url_pieces.query)
            return(True,queries)
        else:
            return True
    else:
        return False

###PAGE BUILDERS###

#A few boilerplate parts of responses
XML_HEADERS = (("Content-type","text/xml"),("charset","utf-8"))
HTML_HEADERS = (("Content-type","text/html"),("charset","utf-8"))
JSON_HEADERS = (("Content-type","application/json"),("charset","utf-8"))

def resp_builder(Code, Headers, Body):
    return {"Code": Code, "Headers": Headers, "Body": Body}

async def display_page(data):
    return data

def fill_template(temp, d):
    pre = temp
    for k in d:
        pre = pre.replace("{"+k+"}", d[k])

    return pre

def error_page(code, message, title="Oooops..."):
    d = {"err_num": str(code), "title": title, "message": message}

    global err_page
    o = fill_template(err_page.content, d)

    Headers = HTML_HEADERS

    return resp_builder(code, Headers, o)

###HOOK SYSTEM###

def add_hook(modname, listname, hook):
    l = getattr(sys.modules[modname], listname)
    l += [hook]

def hooker(modname, listname, hookname):
    def wrapper(func):
        def new_func():
            frm = inspect.stack()[1]
            mod = inspect.getmodule(frm[0])
            hook = getattr(mod, hookname)

            print("Called by: %s." % mod.__name__)

            def onload():
                add_hook(modname, listname, hook)
                print("%s loaded successfully." % mod.__name__)

            def cleanup():
                print("Cleaning up %s..." % mod.__name__)
                l = getattr(sys.modules[modname], listname)
                l.remove(hook)

            func()

            setattr(mod, "onload", onload)
            setattr(mod, "cleanup", cleanup)
        return new_func
    return wrapper


# -*- encoding: utf-8 -*-

import os
import logging
from gs_pipe.pipeline import PipeLine
from redis import Redis
from rq import Queue

LOG = logging.getLogger(__name__)

task_queue = Queue(connection=Redis())

class Task(object):

    def __init__(self, id, status, result=None, created_at=None, description=None):
        self.id = id
        self.status = status
        self.result = result
        self.created_at = created_at
        self.desrciption = description

    @classmethod
    def from_job(cls, job):
        return cls(
            id=job.id,
            status=job.status,
            created_at=job.enqueued_at,
            description=job.description,
            result = job.result
        )

    def as_dict(self):
        return dict(
            id=self.id,
            status=self.status,
            result=self.result,
            created_at=str(self.created_at),
            description=self.desrciption
        )

def create_task(input_argument, pipeline):
    # queue.enque
    job = task_queue.enqueue(evaluate_pipeline, input_argument, pipeline)

    return Task.from_job(job).as_dict()

def get_task(task_id):
    job = task_queue.fetch_job(task_id)
    return Task.from_job(job).as_dict()

def list_mod():
    mods_dir = os.path.join(os.path.dirname(__file__), "mods")
    # todo ....
    # iterate mods directory, find all funcs ..
    # def find_funcs(fpath):
    #     with open(fpath, "r") as f :
    #         lines = f.read()
    # for dirpath, _ , files in os.walk(mods_dir):
    #     fpaths = [os.path.join(dirpath,f) for f in files]
    # todo

    return [
        'gs_pipe.mods.example.square',
        'gs_pipe.mods.example.minus_one'
    ]

def evaluate_pipeline(input_argument, funcs):
    '''
        input_argument: 10
        funcs: [
            'mods.example.square',
            'mods.example.minus_one'
        ]
        result: minus_one(square(10)) -> 99

    '''
    import importlib

    def find_func(func_str):
        func_name = func_str.split('.')[-1]
        mod_str = '.'.join(func_str.split('.')[:-1])
        # import pdb
        # pdb.set_trace()
        mod = importlib.import_module(mod_str)
        func = getattr(mod, func_name, None)
        if not callable(func):
            return None
        return func

    # invalid mod check
    # find mod func, remove none func
    # create pipeline
    # evaluate

    funcs = [find_func(func_str) for func_str in funcs]
    pipeline = PipeLine(*funcs)
    result = pipeline.evaluate(input_argument)
    return list(result)

# if __name__ == '__main__':
#     print(evaluate_pipeline(
#         10, ['mods.example.square', 'mods.example.minus_one']))

#     print(evaluate_pipeline(
#         [10, 11, 12, 13, 18, 100, 1112], [
#             'mods.example.square', 'mods.example.minus_one']
#     ))


from sqlalchemy import create_engine, Column, INTEGER, String, DATETIME, BOOLEAN, FLOAT, ForeignKey
from sqlalchemy.orm import scoped_session, sessionmaker, relationship
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('sqlite:///bookstore.db', convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False, bind=engine))

Base = declarative_base()
Base.query = db_session.query_property()

class Book(Base):
    __tablename__ = 'book'
    id = Column(INTEGER, primary_key=True)
    title = Column(String(256), index=True)
    status = Column(String(20))
    reserved = Column(BOOLEAN)
    fine = Column(FLOAT)
    dateRent = Column(DATETIME)

    def __repr__(self):
        return '<Book {}>'.format(self.id)

    def save(self):
        db_session.add(self)
        db_session.commit()

    def delete(self):
        db_session.delete(self)
        db_session.commit()

class Client(Base):
    __tablename__ = 'client'
    id = Column(INTEGER, primary_key=True)
    name = Column(String(256), index=True)

    def save(self):
        db_session.add(self)
        db_session.commit()

    def delete(self):
        db_session.delete(self)
        db_session.commit()

class Rent(Base):
    __tablename__ = 'rent'
    id = Column(INTEGER, primary_key=True)
    client_id = Column(INTEGER)
    book_id = Column(INTEGER, ForeignKey('book.id'))
    book = relationship('Book')
    dateReservation = Column(DATETIME)

    def __repr__(self):
        return '<Rent {}>'.format(self.book_id, self.client_id)

    def save(self):
        db_session.add(self)
        db_session.commit()

    def delete(self):
        db_session.delete(self)
        db_session.commit()

def init_db():
    Base.metadata.create_all(bind=engine)

if __name__ == '__main__':
    init_db()


# -*- encoding: utf-8 -*-
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
"""
Utils for testing the API service.
"""

import datetime
import hashlib
import json

from ironic.api.controllers.v1 import allocation as al_controller
from ironic.api.controllers.v1 import chassis as chassis_controller
from ironic.api.controllers.v1 import deploy_template as dt_controller
from ironic.api.controllers.v1 import node as node_controller
from ironic.api.controllers.v1 import port as port_controller
from ironic.api.controllers.v1 import portgroup as portgroup_controller
from ironic.api.controllers.v1 import utils as api_utils
from ironic.api.controllers.v1 import volume_connector as vc_controller
from ironic.api.controllers.v1 import volume_target as vt_controller
from ironic.tests.unit.db import utils as db_utils

ADMIN_TOKEN = '4562138218392831'
MEMBER_TOKEN = '4562138218392832'

ADMIN_BODY = {
    'access': {
        'token': {'id': ADMIN_TOKEN,
                  'expires': '2100-09-11T00:00:00'},
        'user': {'id': 'user_id1',
                 'name': 'user_name1',
                 'tenantId': '123i2910',
                 'tenantName': 'mytenant',
                 'roles': [{'name': 'admin'}]},
    }
}

MEMBER_BODY = {
    'access': {
        'token': {'id': MEMBER_TOKEN,
                  'expires': '2100-09-11T00:00:00'},
        'user': {'id': 'user_id2',
                 'name': 'user-good',
                 'tenantId': 'project-good',
                 'tenantName': 'goodies',
                 'roles': [{'name': 'Member'}]},
    }
}

DEFAULT_CACHE_VALUES = {
    ADMIN_TOKEN: ADMIN_BODY,
    MEMBER_TOKEN: MEMBER_BODY
}

class FakeMemcache(object):
    """Fake cache that is used for keystone tokens lookup."""

    def __init__(self, cache_values=None):
        if not cache_values:
            cache_values = DEFAULT_CACHE_VALUES
        self._cache = {}
        for k, v in cache_values.items():
            self._cache['tokens/%s' % k] = v
            # NOTE(lucasagomes): keystonemiddleware >= 2.0.0 the token cache
            # keys are sha256 hashes of the token key. This was introduced in
            # https://review.opendev.org/#/c/186971
            self._cache['tokens/%s' %
                        hashlib.sha256(k.encode()).hexdigest()] = v
        self.set_key = None
        self.set_value = None
        self.token_expiration = None

    def get(self, key):
        dt = datetime.datetime.utcnow() + datetime.timedelta(minutes=5)
        return json.dumps((self._cache.get(key), dt.isoformat()))

    def set(self, key, value, time=0, min_compress_len=0):
        self.set_value = value
        self.set_key = key

def remove_internal(values, internal):
    # NOTE(yuriyz): internal attributes should not be posted, except uuid
    int_attr = [attr.lstrip('/') for attr in internal if attr != '/uuid']
    return {k: v for (k, v) in values.items() if k not in int_attr}

def remove_other_fields(values, allowed_fields):
    return {k: v for (k, v) in values.items() if k in allowed_fields}

def node_post_data(**kw):
    node = db_utils.get_test_node(**kw)

    # NOTE(jroll): pop out fields that were introduced in later API versions,
    # unless explicitly requested. Otherwise, these will cause tests using
    # older API versions to fail.
    for field in api_utils.VERSIONED_FIELDS:
        if field not in kw:
            node.pop(field, None)

    return remove_other_fields(
        node, node_controller.node_schema()['properties'])

def port_post_data(**kw):
    port = db_utils.get_test_port(**kw)
    return remove_other_fields(port,
                               port_controller.PORT_SCHEMA['properties'])

def volume_connector_post_data(**kw):
    connector = db_utils.get_test_volume_connector(**kw)
    return remove_other_fields(connector,
                               vc_controller.CONNECTOR_SCHEMA['properties'])

def volume_target_post_data(**kw):
    target = db_utils.get_test_volume_target(**kw)
    return remove_other_fields(target,
                               vt_controller.TARGET_SCHEMA['properties'])

def chassis_post_data(**kw):
    chassis = db_utils.get_test_chassis(**kw)
    return remove_other_fields(
        chassis, chassis_controller.CHASSIS_SCHEMA['properties'])

def post_get_test_node(**kw):
    # NOTE(lucasagomes): When creating a node via API (POST)
    #                    we have to use chassis_uuid
    node = node_post_data(**kw)
    chassis = db_utils.get_test_chassis()
    node['chassis_uuid'] = kw.get('chassis_uuid', chassis['uuid'])
    return node

def portgroup_post_data(**kw):
    """Return a Portgroup object without internal attributes."""
    portgroup = db_utils.get_test_portgroup(**kw)

    # These values are not part of the API object
    portgroup.pop('version')
    portgroup.pop('node_id')

    # NOTE(jroll): pop out fields that were introduced in later API versions,
    # unless explicitly requested. Otherwise, these will cause tests using
    # older API versions to fail.
    new_api_ver_arguments = ['mode', 'properties']
    for arg in new_api_ver_arguments:
        if arg not in kw:
            portgroup.pop(arg)

    return remove_other_fields(
        portgroup, portgroup_controller.PORTGROUP_SCHEMA['properties'])

def post_get_test_portgroup(**kw):
    """Return a Portgroup object with appropriate attributes."""
    portgroup = portgroup_post_data(**kw)
    node = db_utils.get_test_node()
    portgroup['node_uuid'] = kw.get('node_uuid', node['uuid'])
    return portgroup

def allocation_post_data(node=None, **kw):
    """Return an Allocation object without internal attributes."""
    allocation = db_utils.get_test_allocation(**kw)
    if node:
        # This is not a database field, so it has to be handled explicitly
        allocation['node'] = node
    return remove_other_fields(
        allocation, al_controller.ALLOCATION_SCHEMA['properties'])

def deploy_template_post_data(**kw):
    """Return a DeployTemplate object without internal attributes."""
    template = db_utils.get_test_deploy_template(**kw)
    # These values are not part of the API object
    template.pop('version')
    # Remove internal attributes from each step.
    step_internal = api_utils.DEPLOY_STEP_SCHEMA['properties']
    template['steps'] = [remove_other_fields(step, step_internal)
                         for step in template['steps']]
    # Remove internal attributes from the template.
    return remove_other_fields(
        template, dt_controller.TEMPLATE_SCHEMA['properties'])

def post_get_test_deploy_template(**kw):
    """Return a DeployTemplate object with appropriate attributes."""
    return deploy_template_post_data(**kw)


#encoding: utf-8

'''
题目描述
1

1  1  1

1  2  3  2  1

1  3  6  7  6  3  1

1  4  10 16 19  16 10  4  1

以上三角形的数阵，第一行只有一个数1，以下每行的每个数，是恰好是它上面的数，左上角数到右上角的数，3个数之和（如果不存在某个数，认为该数就是0）。

求第n行第一个偶数出现的位置。如果没有偶数，则输出-1。例如输入3,则输出2，输入4则输出3。

输入n(n <= 1000000000)
本题有多组输入数据，输入到文件末尾，请使用while(cin>>)等方式读入
输入描述:
输入一个int整数

输出描述:
输出返回的int值

示例1
输入
4
输出
3
'''

def solution(layer):
    if layer < 2:
        return -1
    column = 2 * layer - 1
    middle = column // 2
    matrix = [[0] * column for _ in range(layer)]
    matrix[0][middle] = 1
    def getValue(i, j):
        if i < 0 or i >= layer:
            return 0
        elif j < 0 or j >= 2*layer-1:
            return 0
        else:
            return matrix[i][j]

    for i in range(1, layer):
        for j in range(middle-i, middle+i+1):
            matrix[i][j] = getValue(i-1, j-1) + getValue(i-1, j) + getValue(i-1, j+1)

    for idx, num in enumerate(matrix[-1]):
        if num % 2 == 0:
            return idx + 1
    return -1

if __name__ == '__main__':
    while True:
        try:
            print(solution(int(input())))
        except Exception as e:
            print(e)
            break


# Programa: ej18sec.py
# Propósito: Pedir el nombre y los dos apellidos de una persona y mostrar las iniciales.
# Autor: Antonio Quesada
# Fecha: 13/10/2019.
#
# Variables a usar:
#   * nombre
#   * apellido1
#   * apellido2
#   * inicial_nombre
#   * inicial_apellido1
#   * inicial_apellido2
#
# Algoritmo:
#   LEER nombre, apellido1, apellido2
#   inicial_nombre <- nombre[0]
#   inicial_apellido1 <- apellido1[0]
#   inicial_apellido2 <- apellido2[0]
#   ESCRIBIR inicial_nombre, inicial_apellido1, inicial_apellido2

# Petición de datos
nombre = str(input("Inserte su nombre: "))
apellido1 = str(input("Inserte su primer apellido: "))
apellido2 = str(input("Inserte su segundo apellido: "))
print("-------------------------------------------")

#A través del [0] selecciona el primer carácter de la cadena introducida por el usuario.
inicial_nombre = nombre[0]
inicial_apellido1 = apellido1[0]
inicial_apellido2 = apellido2[0]

#Salida
print("La inicial de tu nombre es", inicial_nombre.upper(),",de tu primer apellido", inicial_apellido1.upper(),"y de tu segundo apellido", inicial_apellido2.upper())


# -*- coding: utf-8 -*-
"""BE Proj.ipynb

 Suspicious Link Detection

"""

import warnings
warnings.filterwarnings('ignore')

import numpy as np
import pandas as pd
import re
import nltk

############## Model 1 new########################
import joblib
import numpy as np

import re
#Use of IP or not in domain
def having_ip_address(url):
    match = re.search(
        '(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.'
        '([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\/)|'  # IPv4
        '((0x[0-9a-fA-F]{1,2})\\.(0x[0-9a-fA-F]{1,2})\\.(0x[0-9a-fA-F]{1,2})\\.(0x[0-9a-fA-F]{1,2})\\/)' # IPv4 in hexadecimal
        '(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}', url)  # Ipv6
    if match:
        # print match.group()
        return 1
    else:
        # print 'No matching pattern found'
        return 0

from urllib.parse import urlparse

def abnormal_url(url):
    hostname = urlparse(url).hostname
    hostname = str(hostname)
    match = re.search(hostname, url)
    if match:
        # print match.group()
        return 1
    else:
        # print 'No matching pattern found'
        return 0

def count_dot(url):
    count_dot = url.count('.')
    return count_dot

def count_www(url):
    url.count('www')
    return url.count('www')

def count_atrate(url):

    return url.count('@')

def no_of_dir(url):
    urldir = urlparse(url).path
    return urldir.count('/')

def no_of_embed(url):
    urldir = urlparse(url).path
    return urldir.count('//')

def shortening_service(url):
    match = re.search('bit\.ly|goo\.gl|shorte\.st|go2l\.ink|x\.co|ow\.ly|t\.co|tinyurl|tr\.im|is\.gd|cli\.gs|'
                      'yfrog\.com|migre\.me|ff\.im|tiny\.cc|url4\.eu|twit\.ac|su\.pr|twurl\.nl|snipurl\.com|'
                      'short\.to|BudURL\.com|ping\.fm|post\.ly|Just\.as|bkite\.com|snipr\.com|fic\.kr|loopt\.us|'
                      'doiop\.com|short\.ie|kl\.am|wp\.me|rubyurl\.com|om\.ly|to\.ly|bit\.do|t\.co|lnkd\.in|'
                      'db\.tt|qr\.ae|adf\.ly|goo\.gl|bitly\.com|cur\.lv|tinyurl\.com|ow\.ly|bit\.ly|ity\.im|'
                      'q\.gs|is\.gd|po\.st|bc\.vc|twitthis\.com|u\.to|j\.mp|buzurl\.com|cutt\.us|u\.bb|yourls\.org|'
                      'x\.co|prettylinkpro\.com|scrnch\.me|filoops\.info|vzturl\.com|qr\.net|1url\.com|tweez\.me|v\.gd|'
                      'tr\.im|link\.zip\.net',
                      url)
    if match:
        return 1
    else:
        return 0

def count_https(url):
    return url.count('https')

def count_http(url):
    return url.count('http')

def count_per(url):
    return url.count('%')

def count_ques(url):
    return url.count('?')

def count_hyphen(url):
    return url.count('-')

def count_equal(url):
    return url.count('=')

def url_length(url):
    return len(str(url))

#Hostname Length

def hostname_length(url):
    return len(urlparse(url).netloc)

def suspicious_words(url):
    match = re.search('PayPal|login|signin|bank|account|update|free|lucky|service|bonus|ebayisapi|webscr',
                      url)
    if match:
        return 1
    else:
        return 0

def digit_count(url):
    digits = 0
    for i in url:
        if i.isnumeric():
            digits = digits + 1
    return digits

def letter_count(url):
    letters = 0
    for i in url:
        if i.isalpha():
            letters = letters + 1
    return letters

#Importing dependencies
from urllib.parse import urlparse
from tld import get_tld
import os.path

#First Directory Length
def fd_length(url):
    urlpath= urlparse(url).path
    try:
        return len(urlpath.split('/')[1])
    except:
        return 0

def tld_length(tld):
    try:
        return len(tld)
    except:
        return -1

def main(url):

    status = []

    status.append(having_ip_address(url))
    status.append(abnormal_url(url))
    status.append(count_dot(url))
    status.append(count_www(url))
    status.append(count_atrate(url))
    status.append(no_of_dir(url))
    status.append(no_of_embed(url))

    status.append(shortening_service(url))
    status.append(count_https(url))
    status.append(count_http(url))

    status.append(count_per(url))
    status.append(count_ques(url))
    status.append(count_hyphen(url))
    status.append(count_equal(url))

    status.append(url_length(url))
    status.append(hostname_length(url))
    status.append(suspicious_words(url))
    status.append(digit_count(url))
    status.append(letter_count(url))
    status.append(fd_length(url))
    tld = get_tld(url,fail_silently=True)

    status.append(tld_length(tld))

    print(status)
    return status

def get_prediction_from_url(test_url):
    try:

        features_test = main(test_url)

        # Due to updates to scikit-learn, we now need a 2D array as a parameter to the predict function.
        features_test = np.array(features_test).reshape((1, -1))
        print(features_test)
        # features_test=[0, 1, 2, 0, 0, 3, 0, 0, 0, 1, 0, 0, 1, 0, 73, 34, 0, 3, 61, 10, 3]
        LGB_C = joblib.load('Models/rf_model1.h5')

        pred = LGB_C.predict(features_test)

        print(pred)

        if int(pred[0]) == 0:

            res="SAFE"
            return res
        elif int(pred[0]) == 1.0:

            res="DEFACEMENT"
            return res
        elif int(pred[0]) == 2.0:
            res="PHISHING"
            return res

        elif int(pred[0]) == 3.0:

            res="MALWARE"
        return 1
    except Exception as e:
        print("error occurs",e)

##############End  Model 1 new########################

############## Model 2 old########################

def getTokens(input):
    tokensBySlash = str(input.encode('utf-8')).split('/')
    allTokens=[]
    for i in tokensBySlash:
        tokens = str(i).split('-')
        tokensByDot = []
        for j in range(0,len(tokens)):
            tempTokens = str(tokens[j]).split('.')
            tokentsByDot = tokensByDot + tempTokens
        allTokens = allTokens + tokens + tokensByDot
    allTokens = list(set(allTokens))
    if 'com' in allTokens:
        allTokens.remove('com')
    return allTokens

############## End Model 2########################

import requests
from bs4 import BeautifulSoup

from flask import *
import json
app = Flask(__name__)
@app.route('/')
def home():
    return render_template("index.html")

@app.route('/news')
def news():
    return render_template("news2.html")

@app.route('/preventions')
def prevention():
    return render_template("prevention.html")

import joblib
import pickle
from sklearn.feature_extraction.text import TfidfVectorizer
from scipy.sparse import csr_matrix

@app.route('/getanswer', methods =["GET", "POST"])
def getrecm():
    if request.method == "POST":
        predictx = request.form.get("mname")
        if not predictx.startswith("http://") and not predictx.startswith("https://"):
            predictx = "http://" + predictx

        url=predictx
        print(predictx)

        try:
            X_predict = [predictx]
            # Load the serialized vectorizer object from the file
            with open('Models/vectorizer.pkl', 'rb') as f:
                vectorizer = pickle.load(f)

            # Define some new URLs to transform
            new_urls = X_predict

            # Use the loaded vectorizer to transform the new URLs into TF-IDF format
            new_urls_tfidf = vectorizer.transform(new_urls)

            # Print the transformed URLs
            X_predict=new_urls_tfidf.toarray()
            print(new_urls_tfidf.toarray())
            print("****")
            print(X_predict)

            # load the saved model
            clfsm = joblib.load('Models/rf_model2.h5')
            y_Predict = clfsm.predict(X_predict)
            #print(y_Predict)

            ## for new model
            y_Predict1= get_prediction_from_url(url)
            print(url)
            try:
                page = requests.get(url)
                soup = BeautifulSoup(page.content, "html.parser")

                final_url = page.url
                # serving_ip = page.raw._connection.sock.getpeername()[0]
                serving_ip=123
                status_code = page.status_code
                body_length = len(page.content)
                headers = json.dumps(dict(page.headers))
                meta_tags = soup.find_all("meta")
                arr=[y_Predict[0],url,status_code,body_length, headers,  meta_tags,y_Predict1]

            except:

                arr=[y_Predict[0],url,'NA','NA', 'NA',  'NA',y_Predict1]

            # url_details=f"Final URL: {final_url}
Serving IP Address: {serving_ip}
Status Code: {status_code}
Body Length: {body_length} B
Headers:
{headers}
Meta Tags:
{meta_tags}"

            # return y_Predict[0]+"
"+url_details
            return render_template("result2.html",url_details=arr)

        except Exception as e:
            print(e)
            return ("error")

if __name__ == '__main__':
   app.run(debug = True)


from constAndStyle import *
from PGinterfaces import *
from display import *

def FSinitialize(variables):
    variables["furnitureSelected"] = "chair"
    variables["orientation"] = "north"
    variables["furnitureCount"] = variables["currentMap"].countFurniture()

def FSmainDisplay(window,variables):
    global mapLength,mapWidth,itemNames
    squareSize = variables["squareSize"]
    shift = variables["shift"]

    if variables["currentMap"] != None:
        variables["currentMap"].display(window,variables["shift"],squareSize)

    pygame.draw.rect(window,(250,230,180),(mapLength*squareSize,0,8*squareSize,mapWidth*squareSize),0)
    pygame.draw.line(window,(200,184,144),(mapLength*squareSize,0),(mapLength*squareSize,mapWidth*squareSize),6)

    button = variables["buttons"]["back"]
    displayButton(window,button.rectangle,2,4,button.inColor,button.outColor,"Back",25,(50,50,50))

    button = variables["buttons"]["rotate"]
    displayButton(window,button.rectangle,2,4,button.inColor,button.outColor,"Change orientation: "+variables["orientation"],25,(50,50,50))

    for itemName in furniture:
        button = variables["buttons"][itemName]
        displayButton(window,button.rectangle,2,4,button.inColor,button.outColor,itemName,25,(50,50,50))

        XcopyText = button.rectangle.x + button.rectangle.w*1.1
        YcopyText = button.rectangle.y + button.rectangle.h/2
        itemAmount = variables["furnitureCount"][itemName]
        text(window,str(itemAmount)+"/"+str(furnitureAmounts[itemName]),int(squareSize*(1/2)),(50,50,50),"midleft",XcopyText,YcopyText)

def FSmainProcess(variables):
    global mapLength,mapWidth
    squareSize = variables["squareSize"]
    xMargin = (mapLength+2/3)*squareSize
    medButWidth = 5*squareSize
    longButWidth = 7*squareSize
    medButHeight = (4/5)*squareSize
    vSpace = squareSize

    variables["buttons"]["back"].rectangle = pygame.Rect(xMargin,17*squareSize,medButWidth,medButHeight)

    for index,buttonName in enumerate(furniture):
        variables["buttons"][buttonName].rectangle = pygame.Rect(xMargin,index*vSpace+(1/3)*squareSize,medButWidth,medButHeight)

    middleVspace = squareSize
    variables["buttons"]["rotate"].rectangle = pygame.Rect(xMargin,middleVspace+(index+1)*vSpace+(1/3)*squareSize,medButWidth,medButHeight)

def FSback(variables,event):
    variables["state"] = "inEditor"

def rotate(variables,event):
    nextOrientation = {"north":"east","east":"south","south":"west","west":"north"}
    variables["orientation"] = nextOrientation[variables["orientation"]]

furnitureSelectInterface = Interface()

furnitureSelectInterface.initialize = FSinitialize
furnitureSelectInterface.mainDisplay = FSmainDisplay
furnitureSelectInterface.mainProcess = FSmainProcess

furnitureSelectInterface.buttons += [Button("rotate",but2InCol,but2OutCol,rotate)]
furnitureSelectInterface.buttons += [Button("back",otherInCol,otherOutCol,FSback)]

for buttonName in furniture:

    def selectFurnitureItem(variables,event,itemName = buttonName):
        variables["furnitureSelected"] = itemName
        variables["state"] = "inEditor"

    furnitureSelectInterface.buttons += [Button(buttonName,butInCol,butOutCol,selectFurnitureItem)]


import math, random

# Solution 1, rejection-sampling
class Solution:
    def __init__(self, radius: float, x_center: float, y_center: float):
        self.r = radius
        self.x = x_center
        self.y = y_center

    def randPoint(self) -> List[float]:
        while True:
            i = random.uniform(-1, 1) * self.r
            j = random.uniform(-1, 1) * self.r
            if i * i + j * j <= self.r * self.r:
                return [i + self.x, j + self.y]

# Solution 2, polar coordinates
class Solution:
    def __init__(self, radius: float, x_center: float, y_center: float):
        self.r = radius
        self.x = x_center
        self.y = y_center

    def randPoint(self) -> List[float]:
        r_t = math.pow(random.random(), 0.5) * self.r
        theta = random.random() * math.pi * 2

        i = self.x + r_t * math.cos(theta)
        j = self.y + r_t * math.sin(theta)

        return [i, j]

# Your Solution object will be instantiated and called as such:
# obj = Solution(radius, x_center, y_center)
# param_1 = obj.randPoint()

Algorithms/0478 Generate Random Point in a Circle.py


import numpy as np

def outlier_count(col, data):
    print(15*'-' + col + 15*'-')
    q75, q25 = np.percentile(data[col], [75, 25])
    iqr = q75 - q25
    min_val = q25 - (iqr*1.5)
    max_val = q75 + (iqr*1.5)
    outlier_count = len(np.where((data[col] > max_val) | (data[col] < min_val))[0])
    outlier_percent = round(outlier_count/len(data[col])*100, 2)
    return {outlier_count, outlier_percent}


import json
import string
import random
import time

from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt

from ubskin_web_django.order import models as order_models
from ubskin_web_django.member import models as member_models
from ubskin_web_django.item import models as item_models
from ubskin_web_django.common import lib_data
from ubskin_web_django.common import decorators
from ubskin_web_django.common import common

def get_recv(request):
    if request.method == 'GET':
        return_value = {
            'status': 'error',
            'message': '',
        }
        current_page = request.GET.get('page', 1)
        value = request.GET.get('search_value')
        recv_list = order_models.Recv.get_recv_list(current_page, value)
        return_value['status'] = 'success'
        return_value['data'] = recv_list
        return JsonResponse(return_value)

@csrf_exempt
@decorators.wx_api_authenticated
def create_stock_batch_api(request):
    return_value = {
        'status': 'error',
        'message': '',
    }
    if request.method == 'POST':
        stock_batch_id = ''.join(
            random.choice(string.ascii_lowercase + string.digits) \
            for i in range(8)
        )
        while True:
            stock_batch = order_models.StockBatch.get_stock_dict_by_stock_batch_id(stock_batch_id)
            if stock_batch:
                stock_batch_id = ''.join(
                random.choice(string.ascii_lowercase + string.digits) \
                for i in range(8)
                )
            else:
                break
        data = json.loads(request.body)
        recv_code = data.get('shop_id')
        item_codes_dict = data.get('item_codes_dict')
        openid = request.COOKIES.get('openid')
        member = member_models.Member.get_member_by_wx_openid(openid)
        if not member and not member.is_staff:
            return_value['message'] = '无权限'
            return JsonResponse(return_value)
        order_models.create_model_data(
            order_models.StockBatch,
            {
                "stock_batch_id": stock_batch_id,
                "recv_code": recv_code,
                "create_user": member.member_id,
                "create_time": int(time.time()),
            }
        )
        for key, item in item_codes_dict.items():
            if not item:
                continue
            for i in item:
                if not (len(i) == 9 and i.startswith('U')):
                    return_value['message'] = '商品二维码格式错误'
                    return JsonResponse(return_value)
            stock_batch_count = order_models.create_model_data(
                order_models.StockBatchCount,
                {
                    'item_barcode': key,
                    'stock_batch_id': stock_batch_id,
                    'item_count': len(item),
                }
            )
            for i in item:
                qr_code_obj = order_models.ItemQRCode.get_qr_code_obj_by_qr_code(i)
                qr_code_obj.stock_batch_count_id = stock_batch_count.stock_batch_count_id
                qr_code_obj.create_user = member.member_id
            else:
                qr_code_obj.save()

        return_value['status'] = 'success'
        return JsonResponse(return_value)

def item_code(request, qr_code):
    return_value = {
        'status': 'error',
        'message': '',
    }
    # qr_code = request.GET.get('qr_code')
    if not (len(qr_code) == 9 and qr_code.startswith('U')):
        return_value['message'] = '商品码格式错误'
        return JsonResponse(return_value)

    qr_code_obj = order_models.ItemQRCode.get_qr_code_obj_by_qr_code(qr_code)
    if not qr_code_obj:
        return_value['message'] = '没有记录'
        return JsonResponse(return_value)
    qr_code_obj.search_count += 1
    qr_code_obj.save()
    batch_qr_code_id = qr_code_obj.batch_qr_code_id
    batch_qr_code_obj = order_models.get_model_obj_by_pk(
        order_models.BatchQrCode,
        batch_qr_code_id
    )
    if not qr_code_obj.stock_batch_count_id and batch_qr_code_obj.recv_code:
        recv_addr = order_models.Recv.get_recv_addr_by_recv_code(batch_qr_code_obj.recv_code)
        date = batch_qr_code_obj.create_time
        date = lib_data.parse_timestamps(date)
        member_obj = member_models.Member.get_member_by_id(batch_qr_code_obj.create_member)
        if member_obj:
            is_admin = member_obj.is_admin
            action = "出库扫码" if is_admin else '店铺扫码'
        else:
            action = '店铺扫码'
        return_value['data'] = [
            {"action": action, "to": recv_addr, "date": date},
        ]
        return_value["status"] = "success"
        return JsonResponse(return_value)
    elif not qr_code_obj.stock_batch_count_id and not batch_qr_code_obj.recv_code:
        return_value['messge'] = "该二维码未绑定"
        return JsonResponse(return_value)
    stock_batch_count_id = qr_code_obj.stock_batch_count_id
    obj = order_models.get_model_obj_by_pk(
        order_models.StockBatchCount,
        stock_batch_count_id,
    )
    item_obj = item_models.Items.get_item_obj_by_barcode(obj.item_barcode)
    item_name = '未查询到相关商品信息'
    brand_name = '未查询到相关品牌信息'
    item_photo = common.build_photo_url(None, pic_version='title', cdn=True)
    if item_obj:
        item_name = item_obj.item_name
        brand_id = item_obj.brand_id
        item_photo = common.build_photo_url(item_obj.photo_id, pic_version='title', cdn=True)
        if brand_id:
            brand_obj = item_models.Brands.get_brand_by_id(brand_id)
            brand_name = brand_obj.cn_name if brand_obj else brand_name
    stock_batch_dict = order_models.StockBatch.get_stock_dict_by_stock_batch_id(obj.stock_batch_id)
    recv_code = stock_batch_dict.get('recv_code')
    recv_addr = order_models.Recv.get_recv_addr_by_recv_code(recv_code)
    date = stock_batch_dict.get('create_time')
    date = lib_data.parse_timestamps(date)
    member_obj = member_models.Member.get_member_by_id(stock_batch_dict.get('create_user'))
    if member_obj:
        is_admin = member_obj.is_admin
        action = "出库扫码" if is_admin else '店铺扫码'
    else:
        action = '店铺扫码'
    return_value['data'] = [
        {
            "action": action, "to": recv_addr, "date": date,
            'item_name': item_name, 'brand_name': brand_name, 'image': item_photo,
        },
    ]
    return_value["status"] = "success"
    return JsonResponse(return_value)

def check_has_item_qr_code(request):
    return_value = {
        'status': 'error',
        'message': '',
    }
    if request.method == 'GET':
        item_qr_code = request.GET.get('item_qr_code')
        if not (len(item_qr_code) == 9 and item_qr_code.startswith('U')):
            return_value['message'] = '当前二维码无效'
            return JsonResponse(return_value)
        has = order_models.ItemQRCode.check_has_item_qr_code(item_qr_code)
        if not has:
            return_value['message'] = '当前二维码不在数据库中'
            return JsonResponse(return_value)
        else:
            if has.stock_batch_count_id:
                return_value['message'] = '当前二维码已被绑定'
                return JsonResponse(return_value)
            else:
                return_value['status'] = 'success'
                return JsonResponse(return_value)


from collections import defaultdict

from flask import Flask, request
from flask_restful import Resource, Api
from sqlalchemy import extract

from models import db_session
from models import ModeloColaborador, ModeloPonto

app = Flask(__name__)
api = Api(app)

class Colaborador(Resource):
    def get(self, id):
        colaboradores = ModeloColaborador.query.filter_by(
            id=id).first()
        try:
            response = {
                'id': colaboradores.id,
                'nome': colaboradores.nome,
                'cpf': colaboradores.cpf,
                'email': colaboradores.email,
                'celular': colaboradores.celular,
                'empresa': colaboradores.empresa,
                'cargo': colaboradores.cargo,
                'endereco': colaboradores.endereco,
                'cep': colaboradores.cep,
                'ativo': colaboradores.ativo,
            }
        except AttributeError:
            response = {
                'status': 'error',
                'message': 'Colaborador não encontrado'
            }
        return response

    def put(self, id):
        colaboradores = ModeloColaborador.query.filter_by(
            id=id).first()
        dados = request.json

        if 'id' in dados:
            colaboradores.id = dados['id']

        if 'nome' in dados:
            colaboradores.nome = dados['nome']

        if 'cpf' in dados:
            colaboradores.cpf = dados['cpf']

        if 'email' in dados:
            colaboradores.email = dados['email']

        if 'celular' in dados:
            colaboradores.celular = dados['celular']

        if 'empresa' in dados:
            colaboradores.empresa = dados['empresa']

        if 'cargo' in dados:
            colaboradores.cargo = dados['cargo']

        if 'endereco' in dados:
            colaboradores.endereco = dados['endereco']

        if 'cep' in dados:
            colaboradores.cep = dados['cep']

        if 'ativo' in dados:
            colaboradores.ativo = dados['ativo']

        colaboradores.save()
        response = {
            'id': colaboradores.id,
            'nome': colaboradores.nome,
            'cpf': colaboradores.cpf,
            'email': colaboradores.email,
            'celular': colaboradores.celular,
            'empresa': colaboradores.empresa,
            'cargo': colaboradores.cargo,
            'endereco': colaboradores.endereco,
            'cep': colaboradores.cep,
            'ativo': colaboradores.ativo
        }

        return response

    def delete(self, id):
        colaboradores = ModeloColaborador.query.filter_by(id=id).first()
        mensagem = 'Colaborador {} excluido com sucesso'.format(colaboradores)
        colaboradores.delete()
        return {'status': 'sucesso', 'mensagem': mensagem}

class ListaColaborador(Resource):

    def get(self):
        colaborador = ModeloColaborador.query.all()
        response = [{
            'id': i.id,
            'nome': i.nome,
            'cpf': i.cpf,
            'email': i.email,
            'celular': i.celular,
            'empresa': i.empresa,
            'cargo': i.cargo,
            'endereco': i.endereco,
            'cep': i.cep,
            'ativo': i.ativo

        } for i in colaborador]
        return response

    def post(self):
        dados = request.json
        retorno = []
        retorno.append(- dados['id'])
        for i in range(0, int(dados['id'])):
            retorno.append(dados['nome'])
        colaborador = ModeloColaborador(
            nome=dados['nome'],
            cpf=dados['cpf'],
            email=dados['email'],
            celular=dados['celular'],
            empresa=dados['empresa'],
            cargo=dados['cargo'],
            endereco=dados['endereco'],
            cep=dados['cep'],
            ativo=dados['ativo']
        )

        colaborador.save()
        response = {
            'id': colaborador.id,
            'nome': colaborador.nome,
            'cpf': colaborador.cpf,
            'email': colaborador.email,
            'celular': colaborador.celular,
            'empresa': colaborador.empresa,
            'cargo': colaborador.cargo,
            'endereco': colaborador.endereco,
            'cep': colaborador.cep,
            'ativo': colaborador.ativo
        }
        return response

class Ponto(Resource):
    def get(self, id):
        ponto = ModeloPonto.query.filter_by(
            id=id).first()
        try:
            response = {
                'id': ponto.id,
                'tipo_de_ponto': ponto.tipo_de_ponto,
                'created_at': str(ponto.created_at),
                'last_modified_at': str(ponto.last_modified_at),
                'colaborador_id': ponto.colaborador_id,
            }
        except AttributeError:
            response = {
                'status': 'error',
                'message': 'Ponto não encontrado'
            }
        return response

    def put(self, id):
        ponto = ModeloPonto.query.filter_by(
            id=id).first()
        dados = request.json

        if 'id' in dados:
            ponto.id = dados['id']

        if 'tipo_de_ponto' in dados:
            ponto.tipo_de_ponto = dados['tipo_de_ponto']

        if 'created_at' in dados:
            ponto.created_at = dados['created_at']

        if 'last_modified_at' in dados:
            ponto.last_modified_at = dados['last_modified_at']

        if 'colaborador_id' in dados:
            ponto.colaborador_id = dados['colaborador_id']

        ponto.save()
        response = {
            'id': ponto.id,
            'tipo_de_ponto': ponto.tipo_de_ponto,
            'created_at': ponto.created_at,
            'last_modified_at': ponto.last_modified_at,
            'colaborador_id': ponto.colaborador_id,
        }

        return response

    def delete(self, id):
        ponto = ModeloPonto.query.filter_by(id=id).first()
        mensagem = 'Colaborador {} excluido com sucesso'.format(ponto)
        ponto.delete()
        return {'status': 'sucesso', 'mensagem': mensagem}

class ListaPonto(Resource):

    def get(self):
        ponto = ModeloPonto.query.all()
        response = [{
            'id': i.id,
            'tipo_de_ponto': i.tipo_de_ponto,
            'created_at': i.created_at,
            'last_modified_at': i.last_modified_at,
            'colaborador_id': i.colaborador_id,
        } for i in ponto]
        return response

    def post(self):
        dados = request.json
        retorno = []
        retorno.append(- dados['id'])
        for i in range(0, int(dados['id'])):
            retorno.append(dados['colaborador_id'])
        ponto = ModeloPonto(
            tipo_de_ponto=dados['tipo_de_ponto'],
            created_at=dados['created_at'],
            last_modified_at=dados['last_modified_at'],
            colaborador_id=dados['colaborador_id'],
        )

        ponto.save()
        response = {
            'id': ponto.id,
            'tipo_de_ponto': ponto.tipo_de_ponto,
            'created_at': ponto.created_at,
            'last_modified_at': ponto.last_modified_at,
            'colaborador_id': ponto.colaborador_id,
        }
        return response

api.add_resource(Colaborador, '/colaborador/<int:id>/')
api.add_resource(ListaColaborador, '/listacolaborador/')
api.add_resource(Ponto, '/ponto/<int:id>/')
api.add_resource(ListaPonto, '/listaponto/')

if __name__ == '__main__':
    app.run(debug=True)