# License: MIT
# Author: Karl Stelzner

import os
import sys

import torch
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
import numpy as np
from numpy.random import random_integers
from PIL import Image
from torch.utils.data._utils.collate import default_collate

import json

def progress_bar(count, total, status=''):
    bar_len = 60
    filled_len = int(round(bar_len * count / float(total)))

    percents = round(100.0 * count / float(total), 1)
    bar = '=' * filled_len + '-' * (bar_len - filled_len)

    sys.stdout.write('[%s] %s%s ...%s\r' % (bar, percents, '%', status))
    sys.stdout.flush()

def make_sprites(n=50000, height=64, width=64):
    images = np.zeros((n, height, width, 3))
    counts = np.zeros((n,))
    print('Generating sprite dataset...')
    for i in range(n):
        num_sprites = random_integers(0, 2)
        counts[i] = num_sprites
        for j in range(num_sprites):
            pos_y = random_integers(0, height - 12)
            pos_x = random_integers(0, width - 12)

            scale = random_integers(12, min(16, height-pos_y, width-pos_x))

            cat = random_integers(0, 2)
            sprite = np.zeros((height, width, 3))

            if cat == 0:  # draw circle
                center_x = pos_x + scale // 2.0
                center_y = pos_y + scale // 2.0
                for x in range(height):
                    for y in range(width):
                        dist_center_sq = (x - center_x)**2 + (y - center_y)**2
                        if  dist_center_sq < (scale // 2.0)**2:
                            sprite[x][y][cat] = 1.0
            elif cat == 1:  # draw square
                sprite[pos_x:pos_x + scale, pos_y:pos_y + scale, cat] = 1.0
            else:  # draw square turned by 45 degrees
                center_x = pos_x + scale // 2.0
                center_y = pos_y + scale // 2.0
                for x in range(height):
                    for y in range(width):
                        if abs(x - center_x) + abs(y - center_y) < (scale // 2.0):
                            sprite[x][y][cat] = 1.0
            images[i] += sprite
        if i % 100 == 0:
            progress_bar(i, n)
    images = np.clip(images, 0.0, 1.0)

    return {'x_train': images[:4 * n // 5],
            'count_train': counts[:4 * n // 5],
            'x_test': images[4 * n // 5:],
            'count_test': counts[4 * n // 5:]}

class Sprites(Dataset):
    def __init__(self, directory, n=50000, canvas_size=64,
                 train=True, transform=None):
        np_file = 'sprites_{}_{}.npz'.format(n, canvas_size)
        full_path = os.path.join(directory, np_file)
        if not os.path.isfile(full_path):
            try:
                os.mkdir('./data')
            except:
                print("data folder found !")
            gen_data = make_sprites(n, canvas_size, canvas_size)
            np.savez(full_path, **gen_data)

        data = np.load(full_path)

        self.transform = transform
        self.images = data['x_train'] if train else data['x_test']
        self.counts = data['count_train'] if train else data['count_test']

    def __len__(self):
        return self.images.shape[0]

    def __getitem__(self, idx):
        img = self.images[idx]
        if self.transform is not None:
            img = self.transform(img).float()
        return img, self.counts[idx]

class Clevr(Dataset):
    def __init__(self, directory, train=True, transform=None):
        self.images_path = directory + 'images/train/'
        self.filenames = os.listdir(self.images_path)

        json_path = directory + 'scenes/CLEVR_train_scenes.json'
        with open(json_path) as json_file:
            data = json.load(json_file)
        self.labels = data['scenes']

        self.n = len(self.filenames)
        self.transform = transform

    def __len__(self):
        return self.n

    def _name2idx(self, key, value):
        if key == 'shape':
            if value == 'cube':
                return 0
            elif value == 'cylinder':
                return 1
            elif value == 'sphere':
                return 2
        elif key == 'size':
            if value == 'small':
                return 0
            elif value == 'large':
                return 1
        elif key == 'material':
            if value == 'metal':
                return 0
            elif value == 'rubber':
                return 1
        elif key == 'color':
            if value == 'red':
                return 0
            elif value == 'blue':
                return 1
            elif value == 'purple':
                return 2
            elif value == 'gray':
                return 3
            elif value == 'cyan':
                return 4
            elif value == 'brown':
                return 5
            elif value == 'yellow':
                return 6
            elif value == 'green':
                return 7
        elif key == '3d_coords':
            return (np.array(value) + 3)/6
        else:
            return value

    def __getitem__(self, idx):
        #Image
        imgpath = os.path.join(self.images_path, self.filenames[idx])
        img = Image.open(imgpath)
        if self.transform is not None:
            img = self.transform(img).float()

        #Label
        image_idx = self.labels[idx]['image_index']
        assert image_idx == idx

        objects = self.labels[idx]['objects']
        num_objects = len(objects)
        assert num_objects != 0
        keys = objects[0].keys()
        label = {k:[] for k in keys}
        for i in range(num_objects):
            for k in keys:
                label[k].append(self._name2idx(k, objects[i][k]))
        for k in keys:
            t = label[k]
            label[k] = torch.as_tensor(t)

        return img, label

class ClevrRela(Dataset):
    def __init__(self, directory, train=True, transform=None):
        self.images_path = directory + 'images/'
        self.filenames = os.listdir(self.images_path)

        json_path = directory + 'CLEVR_scenes.json'
        with open(json_path) as json_file:
            data = json.load(json_file)
        self.labels = data['scenes']

        self.n = len(self.filenames)
        self.transform = transform

    def __len__(self):
        return self.n

    def _name2idx(self, key, value):
        if key == 'shape':
            if value == 'cube':
                return 0
            elif value == 'cylinder':
                return 1
            elif value == 'sphere':
                return 2
        elif key == 'size':
            if value == 'small':
                return 0
            elif value == 'large':
                return 1
        elif key == 'material':
            if value == 'metal':
                return 0
            elif value == 'rubber':
                return 1
        elif key == 'color':
            if value == 'red':
                return 0
            elif value == 'blue':
                return 1
            elif value == 'purple':
                return 2
            elif value == 'gray':
                return 3
            elif value == 'cyan':
                return 4
            elif value == 'brown':
                return 5
            elif value == 'yellow':
                return 6
            elif value == 'green':
                return 7
        elif key == '3d_coords':
            return (np.array(value) + 3)/6
        else:
            return value

    def __getitem__(self, idx):
        #Image
        imgpath = os.path.join(self.images_path, self.filenames[idx])
        img = Image.open(imgpath)
        if self.transform is not None:
            img = self.transform(img).float()

        #Label
        image_idx = self.labels[idx]['image_index']
        assert image_idx == idx

        objects = self.labels[idx]['objects']
        num_objects = len(objects)
        assert num_objects != 0
        keys = objects[0].keys()
        label = {k:[] for k in keys}
        for i in range(num_objects):
            for k in keys:
                label[k].append(self._name2idx(k, objects[i][k]))
        for k in keys:
            t = label[k]
            label[k] = torch.as_tensor(t)
        label['relation'] = torch.as_tensor(self.labels[idx]['relationships'])

        return img, label

########################################################################
#
#  ADDED
#
#########################################################################

class MultiObjectDataset(Dataset):

    def __init__(self, data_path, train, split=0.9, transform = None):
        super().__init__()

        # Load data
        data = np.load(data_path, allow_pickle=True)

        # Rescale images and permute dimensions
        x = np.asarray(data['x'], dtype=np.float32) / 255
        x = np.transpose(x, [0, 3, 1, 2])  # batch, channels, h, w

        # Get labels
        try:
            labels = data['labels'].item()
        except:
            labels = data['labels']
            print(type(labels))

        # Split train and test
        split = int(split * len(x))
        if train:
            indices = range(split)
        else:
            indices = range(split, len(x))

        # From numpy/ndarray to torch tensors (labels are lists of tensors as
        # they might have different sizes)
        self.x = torch.from_numpy(x[indices])

        try:
            labels.pop('text', None)
            labels.pop('vertices', None)
        except:
            print("No text to pop !")

        self.labels = self._labels_to_tensorlist(labels, indices)

    @staticmethod
    def _labels_to_tensorlist(labels, indices):
        out = {k: [] for k in labels.keys()}
        for i in indices:
            for k in labels.keys():
                t = labels[k][i]
                t = torch.as_tensor(t)
                out[k].append(t)
        return out

    def __getitem__(self, index):
        x = self.x[index]
        try:
            labels = {k: self.labels[k][index] for k in self.labels.keys()}
        except:
            labels = self.labels
        return x, labels

    def __len__(self):
        return self.x.size(0)

class MultiObjectDataLoader(DataLoader):

    def __init__(self, *args, **kwargs):
        assert 'collate_fn' not in kwargs
        kwargs['collate_fn'] = self.collate_fn
        super().__init__(*args, **kwargs)

    @staticmethod
    def collate_fn(batch):

        # The input is a batch of (image, label_dict)
        _, item_labels = batch[0]
        keys = item_labels.keys()

        # Max label length in this batch
        # max_len[k] is the maximum length (in batch) of the label with name k
        # If at the end max_len[k] is -1, labels k are (probably all) scalars
        max_len = {k: -1 for k in keys}

        # If a label has more than 1 dimension, the padded tensor cannot simply
        # have size (batch, max_len). Whenever the length is >0 (i.e. the sequence
        # is not empty, store trailing dimensions. At the end if 1) all sequences
        # (in the batch, and for this label) are empty, or 2) this label is not
        # a sequence (scalar), then the trailing dims are None.
        trailing_dims = {k: None for k in keys}

        # Make first pass to get shape info for padding
        for _, labels in batch:
            for k in keys:
                try:
                    max_len[k] = max(max_len[k], len(labels[k]))
                    if len(labels[k]) > 0:
                        trailing_dims[k] = labels[k].size()[1:]
                except TypeError:   # scalar
                    pass

        # For each item in the batch, take each key and pad the corresponding
        # value (label) so we can call the default collate function
        pad = MultiObjectDataLoader._pad_tensor
        for i in range(len(batch)):
            for k in keys:
                if trailing_dims[k] is None:
                    continue

                if k == 'relation':
                    size = [max_len[k], max_len[k]] + list(trailing_dims[k])[1:]
                    batch[i][1][k] = MultiObjectDataLoader._pad_tensor_relation(batch[i][1][k], size, value = 0.)
                else:
                    size = [max_len[k]] + list(trailing_dims[k])
                    batch[i][1][k] = pad(batch[i][1][k], size)

        return default_collate(batch)

    @staticmethod
    def _pad_tensor(x, size, value=None):
        assert isinstance(x, torch.Tensor)
        input_size = len(x)
        if value is None:
            value = float('nan')

        # Copy input tensor into a tensor filled with specified value
        # Convert everything to float, not ideal but it's robust
        out = torch.zeros(*size, dtype=torch.float)
        out.fill_(value)
        if input_size > 0:  # only if at least one element in the sequence
            out[:input_size] = x.float()
        return out

    @staticmethod
    def _pad_tensor_relation(x, size, value=None):
        assert isinstance(x, torch.Tensor)
        input_size = x.shape[:2]
        if value is None:
            value = float('nan')

        # Copy input tensor into a tensor filled with specified value
        # Convert everything to float, not ideal but it's robust
        out = torch.zeros(*size, dtype=torch.float)
        out.fill_(value)
        #if input_size > 0:  # only if at least one element in the sequence
        out[:input_size[0], :input_size[1],:] = x.float()
        return out


"""
Script to turn csv of GloFAS stations into config format
for aa toolbox. Input is country ISO2 code.
"""
import os
import sys
from pathlib import Path

import pandas as pd

DATA_DIR = Path(os.environ["AA_DATA_DIR"])
FILENAME = (
    DATA_DIR / "private/exploration/glb/glofas/Qgis_World_outlet_20221004.csv"
)

country_code = sys.argv[1].upper()

df = pd.read_csv(
    FILENAME,
    usecols=[
        "station_id",
        "StationName",
        "Country code",
        "CountryName",
        "LisfloodX",
        "LisfloodY",
    ],
)
df = df.loc[df["Country code"] == country_code].sort_values(
    by=["StationName", "station_id"]
)

# Add suffix
g = df.groupby("StationName")
df["StationName"] += (
    g.cumcount()
    .add(1)
    .astype(str)
    .radd(" ")
    .mask(g["StationName"].transform("count") == 1, "")
)

print("glofas:")
print("  reporting_points:")
for _, row in df.iterrows():
    # print(f"  - id: {row['station_id']}")
    print(f"  - name: {row['StationName']}")
    print(f"    lon: {row['LisfloodX']}")
    print(f"    lat: {row['LisfloodY']}")


from psycopg2 import connect
from json import JSONEncoder, dumps
from decimal import Decimal
from datetime import date

class jsonEncoder(JSONEncoder):
  def default(self, obj):
    if isinstance(obj, Decimal): return float(obj)
    elif isinstance(obj, date): return str(obj)
    return JSONEncoder.default(self, obj)

class dbObj:
  conn = None
  cur = None
  def __init__(self, privilege):
    try:
      self.conn = connect("dbname='granjalibre' user='%s_user'" % privilege)
      self.cur = self.conn.cursor()
    except Exception as e:
      raise Exception("db conn error: " + str(e))
  def rollback(self):
    self.conn.rollback()
  def execute(self, sql, args=None):
    try:
      self.cur.execute(sql, args)
    except Exception as e:
      raise Exception("db exec error: " + str(e))
  def setSearchPath(self, schema):
    self.execute("SET search_path TO %s;", (schema, ))
  def getRow(self, sql, args=None):
    self.execute(sql, args)
    return self.cur.fetchone()
  def getRows(self, sql, args=None):
    self.execute(sql, args)
    return self.cur.fetchall()
  def getRowsAssoc(self, sql, args=None):
    self.execute(sql, args)
    cols, rows = self.cur.description, self.cur.fetchall()
    return [{cols[i][0]:row[i] for i in range(len(cols))} for row in rows]
  def getDescription(self):
    return [cn[0] for cn in self.cur.description]
  def __del__(self):
    self.conn.commit()
    self.cur.close()
    self.conn.close()

def wrapper(func, rq, dump=False):
  try:
    obj = dbObj(rq['privilege'] if 'privilege' in rq else 'select')
    obj.setSearchPath(rq['schema'] if 'schema' in rq else 'public')
    rs = func(obj, rq)
  except Exception as e:
    try:
      obj.rollback()
    except:
      pass
    raise e
  finally:
    try:
      del obj
    except:
      pass
  return dumps(rs, cls=jsonEncoder) if dump else rs


import sys
import numpy as np
import matplotlib.pyplot as plt

def read_data (input_file):
	data = np.genfromtxt(input_file)
	return data[:,0], data[:,1]

def plot_data(x, y):
	plt.grid()
	plt.plot(x, y, label="y", c="black", linewidth=3.0)
	plt.ylabel("y", fontsize=15)
	plt.xlabel("x", fontsize=15)
	plt.title("f(x)", fontsize=14)
	plt.legend(loc=0,fontsize=10)
	plt.show()
	#plt.savefig("function.svg")

def linear_function (x1, y1, x2, y2):
	a = (y2-y1)/(x2-x1)
	b = (y1*x2 - y2*x1)/(x2-x1)
	x = np.linspace(x1,x2,100)
	y = a*x + b
	print("a = %g" % (a))
	print("b = %g" % (b))
	return x, y

def main():
	if len(sys.argv) != 2:
		print("-------------------------------------------------------------------------")
		print("Usage:> python %s <input_file>" % sys.argv[0])
		print("-------------------------------------------------------------------------")
		print("<input_file> = Input file with the function data")
		print("-------------------------------------------------------------------------")
		return 1

	input_file = sys.argv[1]

	x, y = read_data(input_file)

	plot_data(x,y)
	#xfit, yfit = linear_function(50,0.0013,250,0.00724)
	#plt.plot(x,y,label="ref",c="red",linewidth=3.0)
	#plt.plot(xfit,yfit,label="aprox",c="blue",linewidth=3.0)
	#plt.show()

if __name__ == "__main__":
	main()


import random

import torch
from torch.autograd import Variable
import torch.nn.functional as F
import numpy as np
import matplotlib.pyplot as plt

from skimage import color

def train_model_class(model, optimizer, loader, class_weights, num_epochs=10, show_every=20):
    for epoch in range(num_epochs):
        print('Epoch %s' % epoch)
        print('=' * 10)

        running_loss = []
        for i, data in enumerate(iter(loader)):
            input, labels = data
            input, labels = Variable(input), Variable(labels)
            output = model(input)

            optimizer.zero_grad()
            logits = -F.log_softmax(output)
            loss_per_pixel = logits * labels
            loss_per_pixel = torch.sum(loss_per_pixel, 1)

            _, true_labels = torch.max(labels.data, 1)
            true_labels = true_labels.type(torch.LongTensor)

            b, w, h = true_labels.size()

            px_weights = torch.index_select(class_weights, 0, true_labels.view(b, -1).view(-1))
            px_weights = px_weights.view(b, -1).view(b, w, h)

            loss_per_pixel = loss_per_pixel * Variable(px_weights)
            loss = torch.mean(loss_per_pixel)

            loss.backward()
            optimizer.step()

            running_loss.append(loss.data[0])
            if show_every is not None and i % show_every == 0:
                print('Iter %s: %s' % (i, np.mean(running_loss)))
        print('Average loss: %s' % (np.mean(running_loss)))

    return model

def train_model_reg(model, optimizer, loader, num_epochs=10, show_every=20):
    losses = []
    for input, label in iter(loader):
        zero_pred = torch.zeros(label.size())
        losses.append(F.mse_loss(zero_pred, label))
    print('Average: {}'.format(np.mean(losses)))

    for epoch in range(num_epochs):
        print('Epoch %s' % epoch)
        print('=' * 10)

        running_loss = []
        for i, data in enumerate(iter(loader)):
            input, labels = data
            input, labels = Variable(input), Variable(labels)
            output = model(input)

            optimizer.zero_grad()
            loss = F.l1_loss(output, labels)

            loss.backward()
            optimizer.step()

            running_loss.append(loss.data[0])
            if show_every is not None and i % show_every == 0:
                print('Iter %s: %s' % (i, np.mean(running_loss)))
        print('Average loss: %s' % (np.mean(running_loss)))

    return model

def predict_class(model, dset, cat2ab):
    input, label = dset[random.choice(np.arange(len(dset)))]
    L = input.numpy() + 50
    input = Variable(input.unsqueeze(0))
    out = model(input).squeeze(0)
    _, out = torch.max(out, 0)
    out = out.data.numpy()
    plt.hist(out.reshape(-1))
    out_actual = np.zeros((2,) + out.shape)
    for i in range(out_actual.shape[1]):
        for j in range(out_actual.shape[2]):
            a, b = cat2ab[out[i, j]]
            out_actual[:, i, j] = [a + 5, b + 5]
    pred = np.concatenate((L, out_actual), axis=0).transpose(1, 2, 0).clip(-128, 128)
    pred = color.lab2rgb(pred.astype(np.float64))

    plt.figure()
    plt.title('Grayscale')
    plt.imshow(L.squeeze(0), cmap='gray')

    plt.figure()
    plt.title('Predicted')
    plt.imshow(pred)

def predict_reg(model, dset):
    input, label = dset[random.choice(np.arange(len(dset)))]
    L = input.numpy() + 50
    input = Variable(input.unsqueeze(0))
    out = model(input).squeeze(0)

    out_actual = out.data.numpy()
    out_actual = zoom(out_actual, (1, 4, 4)).clip(-1, 1)
    pred = out_actual * 256 - 128
    pred = np.concatenate((L, out_actual), axis=0).transpose(1, 2, 0)
    pred = color.lab2rgb(pred.astype(np.float64))

    plt.figure()
    plt.title('Predicted')
    plt.imshow(pred)


from bs4 import BeautifulSoup
from datetime import datetime as dt
import requests
import re
import urllib.request

print(""" codeforces """)

data=[]
limit=5

site = 'http://codeforces.com'
tab = '/submissions/'
#users = ['tourist', 'mmaxio', 'Petr', 'Slamur', 'uwi', 'I_love_Tanya_Romanova', 'AlexDmitriev', 'RomaWhite', 'akashd', 'anta', 'Um_nik', 'W4yneb0t', 'FatalEagle', 'Radewoosh', 'rajat1603', 'Sumeet.Varma']
users = ['pulkitkapoor15', 'amand26', 'Qvod', 'vinay29', 'kuldeepsharma1312', 'ayushjainpnp', 'pulkit.kapoor', 'zlover', 'dbhrockzz']
#users = ['zlover','dbhrockzz','pulkit.kapoor','vinay29']
#users = ['zlover']
for user in users:
    try:
        scrap = requests.get(site+tab+user)
        soup = BeautifulSoup(scrap.text,'html.parser')

        for tr in soup.find('table',{'class':'status-frame-datatable'}).findAll('tr')[1:limit+1]:
            date = tr.find('td',{'class':'status-small'}).string.strip()
            lang = tr.find('td',class_=None).string.strip()
            status = ' '.join(tr.find('td',{'class':'status-cell'}).stripped_strings)
            ques = tr.find('td',{'data-problemid':re.compile("^\d*$") }).a.string.strip()
            qlink = tr.find('td',{'data-problemid':re.compile("^\d*$") }).a['href']
            link = tr.find('td',{'class':'id-cell'}).a['href']
            data.append({'user':user,'ques':ques,'status':status,'date':date,'lang':lang,'qlink':qlink,'link':link})
    except:
        print('Something Fishy with '+user)
        continue
def cfDateSorter(record):
        date = record['date']
        date, time = date.split()
        year, month, date = date.split('-')
        second, minute, hour = time.split(':')
        return year,month,date,hour,minute,second

data.sort(key=cfDateSorter ,reverse=True)

for box in data:
    print(box['date'].split()[0],site+box['qlink'],box['user'],box['status'])

""" codeforces """

print(""" codechef """)

data = []
limit = 10
site = 'https://www.codechef.com'
url = 'https://www.codechef.com/submissions?sort_by=All&sorting_order=asc&language=All&status=All&year=2017&handle=__user__&pcode=&ccode=&Submit=GO'
#users = ['rajat1603', 'gennady.korotkevich', 'anushi', 'uwi', 'mmaxio', 'anushi5']
users = ['pulkit_hr', 'dbhrockzz1', 'zlover', 'mittal_tanisha', 'vinay2991997', 'navkrishna21']

for user in users:
    try:
        scrap = requests.get(url.replace('__user__',user))
        soup = BeautifulSoup(scrap.text, 'html.parser')

        for tr in soup.find('table',{'class':'dataTable'}).tbody.findAll('tr')[:limit]:
            date = tr.find('td',{'width':130}).string
            temp = tr.findAll('td',{'width':110})
            ques = temp[0].string
            qlink = temp[0].a['href']
            contest = temp[1].string
            clink = temp[1].a['href']
            lang = tr.find('td',{'width':109}).string
            temp = tr.find('td',{'width':75}).a.attrs
            link = temp['href'] if 'href' in temp else None
            temp = tr.find('td',{'width':71}).span
            status=''.join(temp.stripped_strings) if temp['title']=='' else temp['title']
            data.append({'user':user,'date':date,'ques':ques,'qlink':qlink,'contest':contest,'clink':clink,'lang':lang,'link':link,'status':status})
    except:
        print('Something Fishy with '+user)
        continue

def ccDateSorter(record):
    time, ampm, date=record['date'].split()
    if date =='ago':
        now=dt.now()
        return now.year,now.month,now.day,now.hour-int(time),now.minute
    date, month, year = map(int,date.split('/'))
    hour, minute = map(int,time.split(':'))
    if ampm=='PM' and hour<12:
        hour+=12
    return year,month,date,hour,minute

data.sort(key=ccDateSorter, reverse=True)

for row in data:
        print(row['date'],row['ques'],row['user'])

""" codechef """


import numpy as np
import pyopencl as cl

class HeightMapFilter:
    def __init__(self, ctx):
        self.ctx = ctx
        self.program = cl.Program(self.ctx, open("island_filter.cl").read()).build()

    def run_filter(self, height_map):
        queue = cl.CommandQueue(self.ctx)
        destination_buf = cl.Buffer(self.ctx, cl.mem_flags.COPY_HOST_PTR,
                                    hostbuf=height_map)
        events = [self.program.filter(queue, height_map.shape, None,
                                      destination_buf,
                                      np.int32(height_map.shape[1]),
                                      np.int32(height_map.shape[0]),
                                      )]
        cl.enqueue_copy(queue, height_map, destination_buf, wait_for=events)
        return height_map


import sys

import pygame
from alien import Alien
import random
#pylint: disable=no-member
def check_events(ship,stats, play_button,enemigos,bigotes):
    """ responde a los eventos de teclado y mouse """
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event,ship,bigotes)
        elif event.type == pygame.KEYUP:
            check_keyup_events(event,ship,bigotes)
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_x,mouse_y = pygame.mouse.get_pos()
            check_button_juega(stats, play_button,mouse_x,mouse_y)

def check_button_juega(stats, play_button,mouse_x,mouse_y):
    button_clicked = play_button.rect.collidepoint(mouse_x,mouse_y)
    if button_clicked and not stats.game_active:
        pygame.mouse.set_visible(False)
        stats.reset_stats()
        stats.game_active = True

def check_keydown_events(event,ship,bigotes):
    """responde al presionar un boton"""
    if event.key == pygame.K_RIGHT:
        ship.moving_right = True
        bigotes.moving_right = True
    elif event.key == pygame.K_LEFT:
        ship.moving_left = True
        bigotes.moving_left = True
    elif event.key == pygame.K_SPACE:
        ship.jumping = True
def check_keyup_events(event,ship,bigotes):
    """ responde al soltar un boton"""
    if event.key == pygame.K_RIGHT:
        ship.moving_right = False
        bigotes.moving_right = False
    elif event.key == pygame.K_LEFT:
        ship.moving_left = False
        bigotes.moving_left = False

def create_fleet(ai_settings,screen):
    lista = []
    for i in range(6):
        alien = Alien(ai_settings,screen,random.randint(0,2))
        alien.rect.x = 900 + 600*i
        lista.append(alien)

    return lista

def update_aliens(alien):
    alien.update()

def update_collitions(ai_settings,stats,screen,ship,alien,enemigos):
    if ship.rect.colliderect(alien.rect):
        ship_hit(ai_settings,stats,screen,ship)
        posicion = alien_mas_lejos(enemigos)
        alien.rect.x = posicion + 600

def update_enemigo(ai_settings,screen,enemigos,sb):
    for alien in enemigos:
        if alien.rect.x < - alien.rect.width:
            ai_settings.puntaje += 10
            #alien.rect.x = enemigos[-1].rect.x + 500
            posicion = alien_mas_lejos(enemigos)
            alien.rect.x = posicion + 600
        elif alien.rect.x < - alien.rect.width and ai_settings.puntaje > 100 :
            ai_settings.puntaje += 10
            #alien.rect.x = enemigos[-1].rect.x + 500
            posicion = alien_mas_lejos(enemigos)
            alien.rect.x = posicion + random.randint(500,600)
        elif alien.rect.x < - alien.rect.width and ai_settings.puntaje > 200 :
            ai_settings.puntaje += 10
            #alien.rect.x = enemigos[-1].rect.x + 500
            posicion = alien_mas_lejos(enemigos)
            alien.rect.x = posicion + random.randint(400,600)
        elif alien.rect.x < - alien.rect.width and ai_settings.puntaje > 300 :
            ai_settings.puntaje += 10
            #alien.rect.x = enemigos[-1].rect.x + 500
            posicion = alien_mas_lejos(enemigos)
            alien.rect.x = posicion + random.randint(300,600)
        elif alien.rect.x < - alien.rect.width and ai_settings.puntaje > 400 :
            ai_settings.puntaje += 10
            #alien.rect.x = enemigos[-1].rect.x + 500
            posicion = alien_mas_lejos(enemigos)
            alien.rect.x = posicion + random.randint(300,500)
        elif alien.rect.x < - alien.rect.width and ai_settings.puntaje > 500 :
            ai_settings.puntaje += 10
            #alien.rect.x = enemigos[-1].rect.x + 500
            posicion = alien_mas_lejos(enemigos)
            alien.rect.x = posicion + random.randint(300,350)

    sb.show_score(ai_settings.puntaje)
def ship_hit(ai_settings,stats,screen,ship):
    #quita vidas
    if stats.ships_left > 0:
        stats.ships_left -= 1

    else:
        stats.game_active = False
        pygame.mouse.set_visible(True)

def alien_mas_lejos(enemigos):
    lista_de_posiciones = []
    for alien in enemigos:
        lista_de_posiciones.append(alien.rect.x)
    return max(lista_de_posiciones)

def resetear_posiciones(ship,enemigos,bigotes):

    ship.rect.x = 100
    bigotes.rect.x = 10
    for i in range(0,len(enemigos)):
        enemigos[i].rect.x = 900 + 600*i

def lives_left(stats,luna,screen):
    for vida in range(stats.ships_left):
        screen.blit(luna,(950 + vida*70,0))

def aumentar_dficultad(ai_settings):
    if ai_settings.puntaje != 0:
        if ai_settings.puntaje % 50 == 0 and ai_settings.activo:
            ai_settings.alien_speed_factor += 1
            ai_settings.activo = False
        elif  ai_settings.puntaje % 60 == 0 and not ai_settings.puntaje % 50 == 0 :
            ai_settings.activo = True


import argparse

from pathlib import Path

import numpy as np
import pandas as pd
import datetime
import os
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.ensemble import AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import ExtraTreesClassifier
from collections import Counter
from sklearn.metrics import f1_score, accuracy_score, precision_score, recall_score, roc_auc_score, ConfusionMatrixDisplay, confusion_matrix
from imblearn.over_sampling import RandomOverSampler
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import LabelEncoder

from sklearn.model_selection import RandomizedSearchCV
from sklearn import metrics
from sklearn.metrics import classification_report
from sklearn.metrics import roc_auc_score
from sklearn.metrics import roc_curve

import mlflow
import mlflow.sklearn

def parse_args():
    '''Parse input arguments'''

    parser = argparse.ArgumentParser()
    parser.add_argument("--input_data", type=str, help="Path to data")
    parser.add_argument("--model_output", type=str, help="Path to model")

    parser.add_argument("--base_estimator", type=object, default = DecisionTreeClassifier())

    parser.add_argument('--n_estimators', type=int, default = 10000,
                        help='Number of weak learners')

    parser.add_argument('--learning_rate', type=float, default = 1.0,
                        help='learning_rate')

    args = parser.parse_args()

    return args

def main(args):
    '''Read train dataset, train model, save trained model'''

    mlflow.sklearn.autolog()
    mlflow.log_param("adaboost_model", "AdaBoostClassifier")

    mlflow.log_param('base_estimator', object(args.estimator))
    mlflow.log_param('n_estimators', int(args.n_estimators))
    mlflow.log_param('learning_rate', float(args.learning_rate))

    print("reading data")
    df = pd.read_csv(Path(args.input_data) / "data.csv", sep=";")

    Numeric_Var = ["List of numerical variables in your data frame"]
    Cat_Var = ["List of categorical variables in your data frame"]

    print("encoding variables")
    label_encoder = LabelEncoder()
    for column in Cat_Var:
        df[column] = label_encoder.fit_transform(df[column])

    df[Cat_Var] = df[Cat_Var].astype('category')

    print("normalisation des variables continues")
    scaler = StandardScaler()
    #scaler = MaxAbsScaler()
    df[Numeric_Var] = scaler.fit_transform(df[Numeric_Var])

    print("splitting")
    X = df.drop("fraud", axis=1)
    Y = df[["fraud"]]
    X_train, X_test, Y_train, Y_test = train_test_split(X,Y,test_size=0.3,random_state=15,shuffle=True,stratify=Y)

    print("Y_train:\n",  Y_train.value_counts(),
      "\nY_test:\n", Y_test.value_counts())

    sampling_strategy = {0: 11710063, 1:4687331}
    ros = RandomOverSampler(random_state=0, sampling_strategy=sampling_strategy)
    X_train_resampled, Y_train_resampled = ros.fit_resample(X_train, Y_train)

    print("Class distribution after under-sampling:")
    print("X_train_resampled shape:", X_train_resampled.shape)
    print("Y_train_resampled shape:", Y_train_resampled.shape)
    print("Y_train_Count:", Y_train_resampled.value_counts())
    print(sorted(Counter(Y_train_resampled).items()))

    adaboost_model = AdaBoostClassifier(n_estimators= args.n_estimators,
                                                #base_estimator = base_classifier,
                                                learning_rate = args.learning_rate,
                                            )
    print("training model")
    adaboost_model = adaboost_model.fit(X_train_resampled, Y_train_resampled)
    print("predicting values")
    y_adaboost = adaboost_model.predict(X_test)
    print("evaluating model")

    accuracyscore = metrics.accuracy_score(Y_test,y_adaboost)*100
    f1score = metrics.f1_score(Y_test,y_adaboost)*100
    recallscore = metrics.recall_score(Y_test,y_adaboost)*100
    specificityscore = metrics.recall_score(Y_test, y_adaboost, pos_label=0)*100
    precisionscore = metrics.precision_score(Y_test,y_adaboost)*100

    mlflow.log_metric("accuracy_score", accuracyscore)
    mlflow.log_metric("f1_score", f1score)
    mlflow.log_metric("precision_score", precisionscore)
    mlflow.log_metric("recall_score", recallscore)
    mlflow.log_metric("recall_score", specificityscore)
    mlflow.log_metric("specificity_score", specificityscore)
    print ("Accuracy=", accuracyscore)
    print ("f1.score=", f1score)
    print ("precision.score=", precisionscore)
    print ("recall_score=", recallscore)
    print("Specificity=", specificityscore)

    # Visualize results
    cm = confusion_matrix(Y_test, y_adaboost)
    cm_display = ConfusionMatrixDisplay(cm).plot()
    cm_display.figure_.savefig('confusion_matrix.png')
    mlflow.log_artifact("confusion_matrix.png")

    ######## COURBE DE ROC
    # calculate the fpr and tpr for all thresholds of the classification
    prob = adaboost_model.predict_proba(X_test)
    pred = prob[:,1]
    fpr_d, tpr_d, threshold = metrics.roc_curve(Y_test, pred)
    roc_auc = metrics.auc(fpr_d, tpr_d)
    print("AUC Adaboost=", roc_auc)
    auc_d = roc_auc_score(Y_test, pred)
    plt.title('Courbe de ROC - Adaboost')
    plt.plot(fpr_d, tpr_d, 'b', label = 'AUC = %0.2f' % auc_d)
    plt.legend(loc = 'lower right')
    plt.plot([0, 1], [0, 1],'r--')
    plt.xlim([0, 1])
    plt.ylim([0, 1])
    plt.ylabel('True Positive Rate - Sensitivity')
    plt.xlabel('False Positive Rate - (1-Specificity)')
    plt.savefig('roc_curve.png')
    mlflow.log_artifact("roc_curve.png")

    from sklearn.metrics import precision_recall_curve

    precision, recall, _ = precision_recall_curve(Y_test, adaboost_model.predict_proba(X_test)[:, 1])

    plt.figure(figsize=(8, 6))
    plt.plot(recall, precision, color='blue', lw=2)
    plt.xlabel('Recall')
    plt.ylabel('Precision')
    plt.title('Precision-Recall Curve')
    plt.xlim([0.0, 1.0])
    plt.ylim([0.0, 1.05])
    plt.grid()
    plt.savefig('precision_recall.png')
    mlflow.log_artifact("precision_recall.png")

    feature_importance = adaboost_model.feature_importances_
    print("Importance des caractéristiques :")
    importance_df = pd.DataFrame({'Caractéristiques': X_train_resampled.columns, 'Importance': feature_importance})
    importance_df = importance_df.sort_values(by='Importance', ascending=False)
    print(importance_df)

    feature_importances = adaboost_model.feature_importances_

    feature_names = X_train_resampled.columns

    feature_importance_df = pd.DataFrame({'Feature': feature_names, 'Importance': feature_importances})

    feature_importance_df = feature_importance_df.sort_values(by='Importance', ascending=False)

    plt.figure(figsize=(10, 8))
    sns.barplot(x='Importance', y='Feature', data=feature_importance_df)
    plt.title('Importance des Variables')
    plt.xlabel('Importance')
    plt.ylabel('Variable')
    plt.savefig('Importance des Variables.png')
    mlflow.log_artifact("Importance des Variables.png")

    registered_model_name="sklearn-Adaboost-model"

    print("Registering the model via MLFlow")
    mlflow.sklearn.log_model(
        sk_model=adaboost_model,
        registered_model_name=registered_model_name,
        artifact_path=registered_model_name
    )

    #Saving the model to a file
    print("Saving the model via MLFlow")
    mlflow.sklearn.save_model(
        sk_model=adaboost_model,
        path=os.path.join(registered_model_name, "Adaboost_model"),
    )

if __name__ == "__main__":

    mlflow.start_run()

    # ---------- Parse Arguments ----------- #
    # -------------------------------------- #

    args = parse_args()

    lines = [
        f"input_data: {args.input_data}",
        f"model_output: {args.model_output}"
        f"n_estimators: {args.n_estimators}"
        f"learning_rate: {args.learning_rate}"
        f"base_estimator: {args.base_estimator}"
    ]

    for line in lines:
        print(line)

    main(args)

    mlflow.end_run()


#!/usr/bin/env python

import os, sys
from pprint import pprint

import angr

from helpers import *

fpath = '../testbins/tests-macos-x64-macho'
if sys.argv[1:]:
    fpath = sys.argv[1]

sname = '_main'
if sys.argv[2:]:
    sname = sys.argv[2]

print(f'opening: {fpath}')
project = angr.Project(fpath, load_options={"auto_load_libs": False})

symbols = GetSymbolsByName(project, sname)
for s in symbols:
    print(s)


import heapq
import random
from typing import List

from simulator.network_simulator.constants import BITS_PER_BYTE, BYTES_PER_PACKET, EVENT_TYPE_ACK, EVENT_TYPE_SEND
from simulator.network_simulator.packet import Packet
from simulator.network_simulator.link import Link
from simulator.network_simulator.sender import SenderType

USE_LATENCY_NOISE = False
# USE_LATENCY_NOISE = True
MAX_LATENCY_NOISE = 1.01

# DEBUG = True
DEBUG = False

class Network:
    """Network model"""

    def __init__(self, senders: List[SenderType], links: List[Link],
                 record_pkt_log: bool = False):
        self.q = []
        self.cur_time = 0.0
        self.senders = senders
        self.links = links
        self.record_pkt_log = record_pkt_log
        self.pkt_log = []
        self.extra_delays = []  # time used to put packet onto the network
        self.queue_initial_packets()

    def queue_initial_packets(self):
        for sender in self.senders:
            sender.register_network(self)
            sender.reset_obs()

            sender.schedule_send(True)

    def add_packet(self, pkt: Packet) -> None:
        """Add a packet to the packet event queue."""
        heapq.heappush(self.q, pkt)

    def reset(self) -> None:
        self.cur_time = 0.0
        self.q = []
        for link in self.links:
            link.reset()
        for sender in self.senders:
            sender.reset()
        self.queue_initial_packets()
        self.pkt_log = []
        self.extra_delays = []  # time used to put packet onto the network

    def get_cur_time(self) -> float:
        """Return current network time."""
        return self.cur_time

    def run(self, dur: float):
        """Run the network with specified duration."""
        for sender in self.senders:
            sender.reset_obs()
        end_time = min(self.cur_time + dur, self.links[0].trace.timestamps[-1])
        self.extra_delays = []  # time used to put packet onto the network
        while True:
            pkt = self.q[0]
            # pkt.debug_print()
            # use got_data here to make sure aurora receives at least a pkt ack
            # in MI at the beginning of the connection. got_data does not
            # affect other congestion controls
            # if pkt.sender.got_data and pkt.ts >= end_time and pkt.event_type == EVENT_TYPE_SEND:
            if pkt.sender.stop_run(pkt, end_time):
                end_time = pkt.ts
                self.cur_time = end_time
                break
            pkt = heapq.heappop(self.q)

            self.cur_time = pkt.ts
            push_new_event = False

            # if len(self.q) == 0:
            #     self.add_packet(Packet(self.get_cur_time() + 0.001, pkt.sender, 0, 0))
            # if pkt.pkt_size == 0:
            #     continue
            # debug_print("Got %d event %s, to link %d, latency %f at time %f, "
            #             "next_hop %d, dropped %s, event_q length %f, "
            #             "sender rate %f, duration: %f, queue_size: %f, "
            #             "rto: %f, cwnd: %f, ssthresh: %f, sender rto %f, "
            #             "pkt in flight %d, wait time %d" % (
            #                 event_id, event_type, next_hop, cur_latency,
            #                 event_time, next_hop, dropped, len(self.q),
            #                 sender.rate, dur, self.links[0].queue_size,
            #                 rto, sender.cwnd, sender.ssthresh, sender.rto,
            #                 int(sender.bytes_in_flight/BYTES_PER_PACKET),
            #                 sender.pkt_loss_wait_time))
            if pkt.event_type == EVENT_TYPE_ACK:
                sender = pkt.sender
                if pkt.next_hop == len(self.links):
                    # if cur_latency > 1.0:
                    #     sender.timeout(cur_latency)
                    # sender.on_packet_lost(cur_latency)
                    # if sender.rto >= 0 and pkt.cur_latency > sender.rto and sender.pkt_loss_wait_time <= 0:
                    #     sender.timeout()
                    #     pkt.drop()
                    if pkt.dropped:
                        sender.on_packet_lost(pkt)
                        if self.record_pkt_log:
                            self.pkt_log.append(
                                [self.cur_time, pkt.pkt_id, 'lost',
                                 pkt.pkt_size, pkt.cur_latency, pkt.queue_delay,
                                 self.links[0].pkt_in_queue,
                                 sender.pacing_rate * BITS_PER_BYTE,
                                 self.links[0].get_bandwidth(self.cur_time) * BYTES_PER_PACKET * BITS_PER_BYTE,
                                 sender.cwnd, sender.rto])
                    else:
                        sender.on_packet_acked(pkt)
                        if self.record_pkt_log:
                            self.pkt_log.append(
                                [self.cur_time, pkt.pkt_id, 'acked',
                                 pkt.pkt_size, pkt.cur_latency,
                                 pkt.queue_delay, self.links[0].pkt_in_queue,
                                 sender.pacing_rate * BITS_PER_BYTE,
                                 self.links[0].get_bandwidth(self.cur_time) * BYTES_PER_PACKET * BITS_PER_BYTE])
                else:  # in acklink
                    # comment out to save disk usage
                    # if self.record_pkt_log:
                    #     self.pkt_log.append(
                    #         [self.cur_time, pkt.pkt_id, 'arrived',
                    #          pkt.pkt_size, pkt.cur_latency, pkt.queue_delay,
                    #          self.links[0].pkt_in_queue,
                    #          sender.pacing_rate * BITS_PER_BYTE,
                    #          self.links[0].get_bandwidth(self.cur_time) * BYTES_PER_PACKET * BITS_PER_BYTE,
                    #          sender.cwnd, sender.rto])
                    link_prop_latency = self.links[pkt.next_hop].get_cur_propagation_latency(
                        self.cur_time)
                    # link_latency *= self.env.current_trace.get_delay_noise_replay(self.cur_time)
                    # if USE_LATENCY_NOISE:
                    # link_latency *= random.uniform(1.0, MAX_LATENCY_NOISE)
                    # TODO: add delay noise of acklink
                    pkt.add_propagation_delay(link_prop_latency)
                    #
                    # rand = random.uniform(0, 1)
                    # if rand > 0.75:
                    #     noise = abs(random.uniform(0.0, 200) / 1000)
                    #     pkt.add_propagation_delay(noise)
                    pkt.next_hop += 1
                    push_new_event = True
            elif pkt.event_type == EVENT_TYPE_SEND:  # in datalink
                sender = pkt.sender
                if pkt.next_hop == 0:
                    if sender.on_packet_sent(pkt):
                        if self.record_pkt_log:
                            self.pkt_log.append(
                                [self.cur_time, pkt.pkt_id, 'sent',
                                 pkt.pkt_size, pkt.cur_latency,
                                 pkt.queue_delay, self.links[0].pkt_in_queue,
                                 sender.pacing_rate * BITS_PER_BYTE,
                                 self.links[0].get_bandwidth(self.cur_time) * BYTES_PER_PACKET * BITS_PER_BYTE,])
                        push_new_event = True
                    else:
                        continue
                else:
                    push_new_event = True

                if pkt.next_hop == sender.dest:
                    pkt.event_type = EVENT_TYPE_ACK

                # print(pkt.next_hop, pkt.pkt_id, q_delay, self.links[pkt.next_hop].pkt_in_queue)
                # if USE_LATENCY_NOISE:
                # link_latency *= random.uniform(1.0, MAX_LATENCY_NOISE)
                # link_latency += self.env.current_trace.get_delay_noise(
                #     self.cur_time, self.links[pkt.next_hop].get_bandwidth(self.cur_time)) / 1000
                # link_latency *= self.env.current_trace.get_delay_noise_replay(self.cur_time)
                # rand = random.uniform(0, 1)
                # if rand > 0.9:
                #     noise = random.uniform(0.0, self.links[pkt.next_hop].trace.delay_noise) / 1000
                #     pkt.add_propagation_delay(noise)
                # pkt.add_transmission_delay(1 / self.links[0].get_bandwidth(self.cur_time))
                if not self.links[pkt.next_hop].packet_enters_link(self.cur_time):
                    pkt.drop()
                link_prop_latency, q_delay = self.links[pkt.next_hop].get_cur_latency(
                    self.cur_time)
                pkt.add_propagation_delay(link_prop_latency)
                pkt.add_queue_delay(q_delay)
                self.extra_delays.append(
                    1 / self.links[pkt.next_hop].get_bandwidth(self.cur_time))
                pkt.next_hop += 1
                # if not pkt.dropped:
                #     sender.queue_delay_samples.append(new_event_queue_delay)

            if push_new_event:
                heapq.heappush(self.q, pkt)
        return


from datetime import datetime

from app.flash.domain import BaseFlash, FLASH_DATE_FORMAT

class GetRawFlashes:
    async def __call__(self, records: list[str]) -> list[BaseFlash]:
        """
        Parses a list of string records into a list of BaseFlash objects.

        :param records: A list of string records to parse.

        :return: A list of BaseFlash objects parsed from the input records.
        """
        flashes = []

        for index, record in enumerate(records):
            try:
                date, time, lat, lon, resid, stations = record.split(",")

                date = date.strip()
                if date[0] == "\ufeff":
                    date = date[1:]

                time = time.strip()
                flash = BaseFlash(
                    occurrence_date=datetime.strptime(f'{date} {time}', FLASH_DATE_FORMAT),
                    lat=float(lat),
                    lon=float(lon),
                    residual_fit_error=float(resid),
                    stations=int(stations)
                )
                flashes.append(flash)
            except ValueError:
                raise ValueError(f"error on line {index + 1}: {record}")

        return flashes


import torch
import torch.nn as nn

class MLP(nn.Module):
    def __init__(self, enable_cuda=True):
        super().__init__()
        self.enable_cuda = enable_cuda
        self.mlp = nn.Sequential(nn.Conv2d(3, 6, 5), nn.ReLU(),
                                 nn.MaxPool2d(2, 2), nn.Conv2d(6, 16, 5),
                                 nn.ReLU(), nn.MaxPool2d(2, 2), nn.Flatten(),
                                 nn.Linear(16 * 5 * 5, 120), nn.ReLU(),
                                 nn.Linear(120, 84), nn.ReLU(),
                                 nn.Linear(84, 10))

    def forward(self, x):
        if self.enable_cuda:
            return self.mlp(x.cuda())
        else:
            return self.mlp(x)


#coding=utf=8

import re
import sys
import base64
from xml.dom.minidom import parse

'''
导出burp proxy history中筛选的item为xml文件，解析响应，从中搜索域名、邮箱、IP、11位号码以帮助发现敏感信息。
'''

def domain_filter(response_data):
    return list(set(re.findall("((?:[a-zA-Z0-9][-a-zA-Z0-9]{0,62}\.)+(?:com|cn|net|org|cc|com\.cn|vip|top|xin|club|xyz|wang|xin))[^a-zA-Z]",response_data)))

def email_filter(response_data):
    return list(set(re.findall("([a-zA-Z0-9][-a-zA-Z0-9_\.]{0,62}@(?:[a-zA-Z0-9][-a-zA-Z0-9]{0,62}\.)+(?:com|cn|net|org|cc|com\.cn|vip|top|xin|club|xyz|wang|xin))[^a-zA-Z]",response_data)))

def ip_filter(response_data):
    return list(set(re.findall("[^\d]((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))[^d]",response_data)))

def phone_filter(response_data):
    return list(set(re.findall("[^\d]([01][\d]{2}[ -]?[\d]{4}[ -]?[\d]{4})[^\d]", response_data)))

def dir_filter(response_data):
    #令人蛋疼的内存处理机制? 毛病remove? 还必须新建列表的内存来存储新数据？ 目录匹配排除不能用作文件名的字符、以及不太可能作为目录的字符或者是它们会被urlencode
    #多匹配
    dirs = list(set(re.findall("\"((?:/[^/\\\\:\*\"\<\>\|\?,()'+$ ]+)+/?)\"", response_data)))
    dirs = dirs + list(set(re.findall("'((?:/[^/\\\\:\*\"\<\>\|\?,()'+$ ]+)+/?)'", response_data)))   #js中可能存在'/abc/def' 这样的目录
    dirs = dirs + list(set(re.findall("href=\"((?:[^/\\\\:\*\"\<\>\|\?,()'+$ ]+)+/?)\"", response_data)))  #href可能存在不以/开头写法的目录
    #dirs = dirs + list(set(re.findall("([CDEFGH]:\\\\(?:[^/\\\\:\*\"\<\>\|\?,()'+$ ]+\\\\?)+)", response_data)))  #报错页面可能存在绝对路径泄露 多余的过滤

    tmp = []
    for dir in dirs:
        if re.match("^/.*\.jpg$",dir):
            tmp.append(dir)
    for dir in dirs:
        if re.match("^/.*\.png$",dir):
            tmp.append(dir)
    for dir in dirs:
        if re.match("^/.*\.css$",dir):
            tmp.append(dir)
    for dir in dirs:
        if re.match("^/.*\.jpeg$",dir):
            tmp.append(dir)
    for dir in dirs:
        if re.match("^/.*\.gif$",dir):
            tmp.append(dir)
    for dir in dirs:
        if re.match("^/.*\.ico$",dir):
            tmp.append(dir)
    for dir in dirs:
        if re.match("^/.*\.js$",dir):
            tmp.append(dir)
    dirs = list(set(dirs) - set(tmp))
    return dirs

def all_filter(response_data):
    return domain_filter(response_data) + email_filter(response_data) + ip_filter(response_data) + phone_filter(response_data) + dir_filter(response_data)

def data_parse(url,response_data,data_filter):
    global domain_blacklist
    results = []
    if data_filter == 'all':
        results = all_filter(response_data)
    elif data_filter == 'domain':
        results = domain_filter(response_data)
    elif data_filter == 'email':
        results = email_filter(response_data)
    elif data_filter == 'ip':
        results = ip_filter(response_data)
    elif data_filter == 'phone':
        results = phone_filter(response_data)
    elif data_filter == 'dir':
        results = dir_filter(response_data)
    if results:
        print("\nURL:"+url)
        for info in results:
            if info in domain_blacklist:
                continue
            print(info)

if __name__ == '__main__':
    burp_history_item = None
    data_filter = None
    domain_blacklist = []
    with open("domain_blacklist.txt",'r',encoding='utf-8') as f:
        domain_blacklist = [line.strip() for line in f.readlines()]
    try:
        burp_history_item = sys.argv[1]
        data_filter = sys.argv[2]
    except:
        pass

    if not burp_history_item or data_filter not in ['domain','email','ip','phone','dir','all']:
        print("\n[+] Usage: python3 %s burp_item_xml_file filter\n[+] Filter:\n"
              "\tdomain\tonly grep domain\n"
              "\tip\tonly grep ip\n"
              "\tdir\tonly grep directory\n"
              "\temail\tonly grep email\n"
              "\tphone\tonly grep phone\n"
              "\tall\tgrep all\n" % sys.argv[0])
        sys.exit(0)
    try:
        DomTree = parse(sys.argv[1])
        collection = DomTree.documentElement
        items = collection.getElementsByTagName("item")
    except:
        print("[!] No such file or xml parse error: %s" % sys.argv[1])
        sys.exit(0)
    result = []
    for item in items:
        url = item.getElementsByTagName("url")[0].childNodes[0].data
        try:
            response_data = str(base64.b64decode(item.getElementsByTagName("response")[0].childNodes[0].data),encoding='utf-8')
        except Exception as e:
            if "list index out of range" in str(e):
                response_data = ' '
            else:
                response_data = str(base64.b64decode(item.getElementsByTagName("response")[0].childNodes[0].data))
        data_parse(url,response_data,data_filter)


import argparse
import glob
import json
import os
from PIL import ExifTags, Image

def get_exif(img):
    exif_dict = {}
    exif = img._getexif()
    for key, value in exif.items():
        if key in ExifTags.TAGS:
            if type(value) is str or type(value) is int or type(value) is float or type(value) is tuple:
                exif_dict[ExifTags.TAGS[key]] = value
    return exif_dict, exif.get(0x112, 1)

def rotate_image(img, orientation):
    if orientation == 1:
        return img
    if orientation == 2:
        return img.transpose(Image.FLIP_LEFT_RIGHT)
    if orientation == 3:
        return img.transpose(Image.ROTATE_180)
    if orientation == 4:
        return img.transpose(Image.FLIP_TOP_BOTTOM)
    if orientation == 5:
        return img.transpose(Image.FLIP_LEFT_RIGHT).transpose(Image.ROTATE_90)
    if orientation == 6:
        return img.transpose(Image.ROTATE_270)
    if orientation == 7:
        return img.transpose(Image.FLIP_LEFT_RIGHT).transpose(Image.ROTATE_270)
    if orientation == 8:
        return img.transpose(Image.ROTATE_90)

if __name__== "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("org_dir")
    parser.add_argument("dst_dir")
    parser.add_argument("-f", "--fromindex", type=int, default=0)
    args = parser.parse_args()

    if not os.path.exists(args.dst_dir):
        os.mkdir(args.dst_dir)

    matches = os.path.join(args.org_dir, "*")
    sorted_files = sorted(glob.glob(matches))

    for i, path in enumerate(sorted_files):
        if i < args.fromindex:
            continue

        filename = os.path.splitext(os.path.basename(path))
        webp_path = os.path.join(args.dst_dir, filename[0] + ".webp")
        json_path = os.path.join(args.dst_dir, filename[0] + ".json")

        if os.path.exists(webp_path):
            print(f"{i}\talready exists: {webp_path}")
            continue

        img = Image.open(path)
        exif, orientation = get_exif(img)
        with open(json_path, "w") as fp:
            json.dump(exif, fp, indent=2, ensure_ascii=False)

        rotated_img = rotate_image(img, orientation)
        rotated_img.save(webp_path)
        print(f"{i}\tsaved: {webp_path}")


import logging

from django.core.management.base import BaseCommand

from promgen import prometheus, tasks

logger = logging.getLogger(__name__)

class Command(BaseCommand):
    def add_arguments(self, parser):
        parser.add_argument(
            "--reload",
            action="store_true",
            help="Trigger Prometheus Reload",
        )
        parser.add_argument(
            "out",
            nargs="?",
            help="Optionally specify an output file to use an atomic write operation",
        )

    def handle(self, **kwargs):
        if kwargs["out"]:
            tasks.write_rules(
                path=kwargs["out"],
                reload=kwargs["reload"],
            )
        else:
            # Since we're already working with utf8 encoded data, we can skip
            # the newline ending here
            self.stdout.ending = None
            self.stdout.buffer.write(prometheus.render_rules())


import collections
import functools
import bint

def solution(A,B,C):
    from collections import  deque

    def inner(a, b):
        return a, b, b & a == b, a | b, b | a, a & (a | b), b & (a | b)

    dq = deque([A, B, C])
    dq2 = deque()
    dq3 = deque()
    for l in range(len(dq)):
        a = dq[0]
        b = dq[1]
        print ("<1>",inner(a, b))
        if not (b & a == b):
            dq2.append(a | b)
        dq.rotate()

    for l in range(len(dq2)):
        a = dq2[0]
        b = dq2[1]
        print("<2>",inner(a, b))
        if not (b & a == b):
            dq3.append(a | b)
        dq2.rotate()

    print(dq, dq2, dq3)
    # if B&A == B A is conforming to B
    #return A, B, B&A == B, A|B, B|A, A & (A|B), B & (A|B)

def hash(s: str, mod: int) -> int:
    mult = 997
    return functools.reduce(lambda z, c: (z * mult + ord(c)) % mod, s, 0)

def can_be_palindrome(s:str) -> bool:
    return sum(v % 2 for v in collections.Counter(s).values()) <= 1

if __name__ == '__main__':
    inputs = [

        (1073741727, 1073741631, 1073741679),
        (1,2,3)

    ]

    for i in inputs:
        #print(i, "->", solution(i[0], i[1], i[2]))
        print(hash(str(i[0]), 522))
        print(hash(str(i[1]), 522))
        print(hash(str(i[2]), 522))

    print(can_be_palindrome("abba"))
    print(can_be_palindrome("baba"))


import pytest

from universal import algos, tools

EPS = 1e-10

@pytest.fixture(scope="module")
def S():
    """Random portfolio for testing."""
    return tools.random_portfolio(n=100, k=3, mu=0.0, sd=0.01)

@pytest.mark.parametrize(
    "algo_class",
    [
        algos.CRP,
        algos.RMR,
        algos.OLMAR,
        algos.PAMR,
    ],
)
def test_bias(algo_class, S):
    """Test forward bias of algo. Test on a portion of given data set, then add several
    data points and see if weights has changed.
    """
    m = 10
    B1 = algo_class().run(S.iloc[:-m]).B
    B2 = algo_class().run(S).B

    assert (B1 == B2.iloc[:-m]).all().all()

# BAH
def test_bah(S):
    """There should be no fees for BAH."""
    FEE = 0.01
    result = algos.BAH().run(S)
    wealth_no_fees = result.total_wealth
    result.fee = FEE
    wealth_with_fees = result.total_wealth

    assert abs(wealth_no_fees - wealth_with_fees) < EPS

# CRP
def test_crp(S):
    """Make sure that equity of a portfolio [1,0,...,0] with NaN values
    is the same as asset itself."""
    b = [1.0] + [0.0] * (len(S.columns) - 1)
    result = algos.CRP(b).run(S)

    assert abs(result.total_wealth - S[S.columns[0]].iloc[-1]) < EPS

def test_tco1(S):
    """Zero turnover with extremely high fees."""
    result = algos.TCO1(eta=1, trx_fee_pct=1e6).run(S)
    assert abs(result.turnover) < 1e-8


#General Imports

import os
import statistics
import json

#Creating traceroute text file

json_list = []
json_list_all = []

num_times_run = 3

print('')
print ("Running traceroute " + str(num_times_run) + " times...")

for x in range(0, num_times_run):

    print('')
    print('Run ' + str(x + 1) + '...')

    website = 'google.com'
    cmd = 'traceroute ' + website + ' > trt.txt'
    cmd2 = 'traceroute ' + website

    os.system(cmd)

    #*******************************************************************************************

    #Parsing text file in .json format

    with open('trt.txt', 'r') as file:
        trt_text = file.read()

    trt_list = trt_text.splitlines()

    count = 0
    avg = None
    mx = None
    md = None
    mn = None
    hop = None
    hosts = None
    host_list = []
    lat_list = []

    for line in trt_list:

        count = count + 1

        each_list = line.split()

        while 'ms' in each_list:
            each_list.remove('ms')
        while '*' in each_list:
            each_list.remove('*')

    #     print(each_list)

        if len(each_list) == 6:

            hop = int(each_list[0])
            hosts = [each_list[1], each_list[2]]

            lat_list = [float(each_list[3]), float(each_list[4]), float(each_list[5])]

            avg = round(statistics.mean(lat_list), 3)
            mx = max(lat_list)
            md = round(statistics.median(lat_list), 3)
            mn = min(lat_list)

            json_dict ={'avg': avg,
                        'hop': hop,
                        'hosts': hosts,
                        'max': mx,
                        'med': md,
                        'min': mn}

            json_dict_all ={'hop': hop,
                            'lat': lat_list}

            json_list.append(json_dict)

            json_list_all.append(json_dict_all)

        elif len(each_list) == 5:

            hop = int(each_list[0])

            host_list = []
            host_list = [each_list[1], each_list[2]]

            lat_list = []
            lat_list = [float(each_list[3]), float(each_list[4])]

            avg = round(statistics.mean(lat_list), 3)
            mx = max(lat_list)
            md = round(statistics.median(lat_list), 3)
            mn = min(lat_list)

            json_dict = {'avg': avg,
                        'hop': hop,
                        'hosts': host_list,
                        'max': mx,
                        'med': md,
                        'min': mn}

            json_dict_all ={'hop': hop,
                            'lat': lat_list}

            json_list.append(json_dict)

            json_list_all.append(json_dict_all)

        elif len(each_list) == 4:

            if len(each_list[0]) < 3:

                hop = int(each_list[0])

                host_list = []
                host_list = [each_list[1], each_list[2]]

                lat_list = []
                lat_list = [float(each_list[3])]

                avg = round(statistics.mean(lat_list), 3)
                mx = max(lat_list)
                md = round(statistics.median(lat_list), 3)
                mn = min(lat_list)

                json_dict = {'avg': avg,
                            'hop': hop,
                            'hosts': host_list,
                            'max': mx,
                            'med': md,
                            'min': mn}

                json_dict_all ={'hop': hop,
                                'lat': lat_list}

                json_list.append(json_dict)

                json_list_all.append(json_dict_all)

            else:

                host_list.append(each_list[0])
                host_list.append(each_list[1])

                lat_list.append(float(each_list[2]))
                lat_list.append(float(each_list[3]))

                avg = round(statistics.mean(lat_list), 3)
                mx = max(lat_list)
                md = round(statistics.median(lat_list), 3)
                mn = min(lat_list)

                json_dict = {'avg': avg,
                            'hop': hop,
                            'hosts': host_list,
                            'max': mx,
                            'med': md,
                            'min': mn}

                json_dict_all ={'hop': hop,
                                'lat': lat_list}

                json_list.append(json_dict)

                json_list_all.append(json_dict_all)

        elif len(each_list) == 3:

                host_list.append(each_list[0])
                host_list.append(each_list[1])

                lat_list.append(float(each_list[2]))

                avg = round(statistics.mean(lat_list), 3)
                mx = max(lat_list)
                md = round(statistics.median(lat_list), 3)
                mn = min(lat_list)

                json_dict = {'avg': avg,
                            'hop': hop,
                            'hosts': host_list,
                            'max': mx,
                            'med': md,
                            'min': mn}

                json_dict_all ={'hop': hop,
                            'lat': lat_list}

                json_list.append(json_dict)

                json_list_all.append(json_dict_all)

        else:
            pass

    # print('')
    # for one in json_list:
    #     print(one)

    #*******************************************************************************************

    #Removing extra entries json_list

    for row in range(0, len(json_list) + 1):
        try:
            if json_list[row].get('hop') == json_list[row + 1].get('hop'):
                json_list.remove(json_list[row])
        except:
            pass

    for row in range(0, len(json_list) + 1):
        try:
            if json_list[row].get('hop') == json_list[row + 1].get('hop'):
                json_list.remove(json_list[row])
        except:
            pass

#     print('')
#     for one in json_list:
#         print(one)

    #*******************************************************************************************

    #Removing extra entries json_list_all

    for row in range(0, len(json_list_all) + 1):
        try:
            if json_list_all[row].get('hop') == json_list_all[row + 1].get('hop'):
                json_list_all.remove(json_list_all[row])
        except:
            pass

    for row in range(0, len(json_list_all) + 1):
        try:
            if json_list_all[row].get('hop') == json_list_all[row + 1].get('hop'):
                json_list_all.remove(json_list_all[row])
        except:
            pass

#     print('')
#     for one in json_list_all:
#         print(one)

    print('Run ' + str(x + 1) + ' finished!')

print('')
print('Exporting json data...')

#Exporting data in json_list file

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

#Exporting data in json_list_all file

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

print('Export successful!')

#Imports for plotting data as boxplot

import pandas as pd
import plotly
pd.options.plotting.backend = "plotly"

# import seaborn as sns
# import matplotlib.pyplot as plt
# %matplotlib inline

print('')
print('Generating boxplots...')

#Creating boxplot for the average values for each number of hops each time traceroute was run

raw_data = pd.read_json('trt_data.json')

raw_data.head()

df = raw_data [['hop','avg']]

df_all = pd.DataFrame()

raw_data.head(100)

for hops in range(1, df.hop.max() + 1):
    df_by_hops = df[df.hop == hops][['avg']]
    if df_by_hops.empty != True:
        tmplist=[]
        for row in range(0, len(df_by_hops)):
            tmplist.append(df_by_hops.iloc[row]['avg'])

        if len(tmplist) != num_times_run:
            count = 0
            for each in tmplist:
                count = count + 1
            for times in range(0, num_times_run-count):
                tmplist.append(None)

        df_all[str(hops) + " hop(s)"] = tmplist

# print(df_all)

# print(tmplist)

fig1 = df_all.plot.box()

fig1.update_layout(
    font_family="Courier New",
    font_color="blue",
    title="Boxplot of Avg Latency Values for Each Traceroute Run"
)
fig1.update_yaxes(title="latency (s)")
fig1.update_xaxes(title="")

if not os.path.exists("charts"):
    os.mkdir("charts")

fig1.write_image("charts/bxplt_avg.pdf", format="pdf")

#Creating boxplot for all latency data points for each hop

raw_data = pd.read_json('trt_data_all.json')

df_all_lats = pd.DataFrame()

hops_list = []

# print(raw_data)

for row in range(0, len(raw_data)):

    hops_list.append(raw_data.loc[row]['hop'])

hops_list = list(set(hops_list))

# print(hops_list)

for each in hops_list:

    hop_proc = raw_data[ raw_data['hop'] == each  ]

    hop_proc = hop_proc.reset_index()

    tmplist=[]
    latlist=[]

    for row in range(0, len(hop_proc)):

        hop = hop_proc.loc[row]['hop']

        tmplist = hop_proc.loc[row]['lat']

        for each in tmplist:
            latlist.append(each)

    if len(latlist) < num_times_run * 3:

        filler = 3 * num_times_run - len(latlist)

        for each in range(0, filler):
            latlist.append(None)

    df_all_lats[str(hop) + " hop(s)"] = latlist

df_all_lats.head(100)

fig2 = df_all_lats.plot.box()

fig2.update_layout(
    font_family="Courier New",
    font_color="Black",
    title="Boxplot of All Latency Values for Each Traceroute Run"
)
fig2.update_yaxes(title="latency (s)")
fig2.update_xaxes(title="")

if not os.path.exists("charts"):
    os.mkdir("charts")

fig2.write_image("charts/bxplt_all.pdf", format="pdf")

print('Boxplots generated successfully to the charts folder!')


from flask import Flask

app=Flask(__name__)

@app.route('/ /<a>',methods=["GET"] )
def hello(a):
    if(a==''):
        return "Hello World"
    else:
        return "Sorry! No response. Try again."

@app.route('/dojo')
def dojo():
    return "Dojo!"

@app.route('/say/<name>')
def name(name):
    return f"Hi {name.capitalize()}!"

@app.route('/repeat/<int:num>/<string:word>')
def repeat(num, word):
    output = ''
    for i in range(0,num):
        output += f"<p>{word}</p>"
    return output

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


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import copy
import os
import logging
import random

import torch
import numpy as np
import cv2
from lib.dataset.coco import COCODataset
from lib.dataset.transformations.multi_keypoint_transformation import MultiKeypointTransformation
from lib.dataset.transformations.random_image_transform import RandomImageTransform
from lib.dataset.transformations.targeted_blurring import TargetedBlurring

from lib.dataset.transformations.targeted_cutmix import TargetedCutmix
from lib.dataset.transformations.targeted_cutout import Cutout

from lib.utils.transforms import affine_transform, get_affine_transform, fliplr_joints
from train import get_image_dir_path

logger = logging.getLogger(__name__)

class ContrastiveCOCODataset(COCODataset):
    '''
    "keypoints": {
        0: "nose",
        1: "left_eye",
        2: "right_eye",
        3: "left_ear",
        4: "right_ear",
        5: "left_shoulder",
        6: "right_shoulder",
        7: "left_elbow",
        8: "right_elbow",
        9: "left_wrist",
        10: "right_wrist",
        11: "left_hip",
        12: "right_hip",
        13: "left_knee",
        14: "right_knee",
        15: "left_ankle",
        16: "right_ankle"
    },
	"skeleton": [
        [16,14],[14,12],[17,15],[15,13],[12,13],[6,12],[7,13], [6,7],[6,8],
        [7,9],[8,10],[9,11],[2,3],[1,2],[1,3],[2,4],[3,5],[4,6],[5,7]]
    '''

    def __init__(self, cfg, root, image_set, is_train, transform=None):
        super().__init__(cfg, root, image_set, is_train, transform)
        self.occlusion_transforms_keypoint = []
        self.occlusion_transforms_parts = []
        ## REMEMBER TO NOT USE JITTERING WHEN USING CONTRASTIVE!
        if cfg.CONTRASTIVE.CUTMIX:
            self.occlusion_transforms_parts.append(
                RandomImageTransform(
                    TargetedCutmix(self.get_image_dir_path(cfg), self._get_ann_file_keypoint(), dataset = cfg.DATASET.DATASET), 1.0,
                    "parts", dataset=cfg.DATASET.DATASET))
        if cfg.CONTRASTIVE.CUTOUT.KEYPOINT:
            self.occlusion_transforms_keypoint.append(
                RandomImageTransform(Cutout(cfg.DATASET.DATASET), 1.0, "keypoint", dataset=cfg.DATASET.DATASET))
        if cfg.CONTRASTIVE.CUTOUT.PARTS:
            self.occlusion_transforms_parts.append(
                RandomImageTransform(Cutout(cfg.DATASET.DATASET), 1.0, "parts", dataset=cfg.DATASET.DATASET))
        if cfg.CONTRASTIVE.BLURRING.KEYPOINT:
            self.occlusion_transforms_keypoint.append(
                RandomImageTransform(TargetedBlurring(cfg.DATASET.DATASET), 1.0, "keypoints",
                                     dataset=cfg.DATASET.DATASET))
        if cfg.CONTRASTIVE.BLURRING.PARTS:
            self.occlusion_transforms_parts.append(
                RandomImageTransform(TargetedBlurring(cfg.DATASET.DATASET), 1.0, "parts", dataset=cfg.DATASET.DATASET))
        if cfg.CONTRASTIVE.MULTIKEYPOINT.USE:

            multi_transforms = []
            if cfg.CONTRASTIVE.MULTIKEYPOINT.CUTOUT:
                multi_transforms.append(Cutout(cfg.DATASET.DATASET))
            if cfg.CONTRASTIVE.MULTIKEYPOINT.BLURRING:
                multi_transforms.append(TargetedBlurring(cfg.DATASET.DATASET))
            if len(multi_transforms) > 0:
                self.occlusion_transforms_keypoint.append(
                    MultiKeypointTransformation(multi_transforms, cfg.CONTRASTIVE.MULTIKEYPOINT.MAX_NUM,
                                                1.0, dataset=cfg.DATASET.DATASET))

    def __getitem__(self, idx):
        db_rec = copy.deepcopy(self.db[idx])

        image_file = db_rec['image']
        filename = db_rec['filename'] if 'filename' in db_rec else ''
        imgnum = db_rec['imgnum'] if 'imgnum' in db_rec else ''

        if self.data_format == 'zip':
            from utils import zipreader
            data_numpy = zipreader.imread(
                image_file, cv2.IMREAD_COLOR | cv2.IMREAD_IGNORE_ORIENTATION
            )
        else:
            data_numpy = cv2.imread(
                image_file, cv2.IMREAD_COLOR | cv2.IMREAD_IGNORE_ORIENTATION
            )

        if self.color_rgb:
            data_numpy = cv2.cvtColor(data_numpy, cv2.COLOR_BGR2RGB)

        if data_numpy is None:
            logger.error('=> fail to read {}'.format(image_file))
            raise ValueError('Fail to read {}'.format(image_file))

        joints = db_rec['joints_3d']
        joints_vis = db_rec['joints_3d_vis']

        c = db_rec['center']
        s = db_rec['scale']
        score = db_rec['score'] if 'score' in db_rec else 1
        r = 0

        if self.is_train:
            if (np.sum(joints_vis[:, 0]) > self.num_joints_half_body
                    and np.random.rand() < self.prob_half_body):
                c_half_body, s_half_body = self.half_body_transform(
                    joints, joints_vis
                )

                if c_half_body is not None and s_half_body is not None:
                    c, s = c_half_body, s_half_body

            sf = self.scale_factor
            rf = self.rotation_factor
            s = s * np.clip(np.random.randn() * sf + 1, 1 - sf, 1 + sf)
            r = np.clip(np.random.randn() * rf, -rf * 2, rf * 2) \
                if random.random() <= 0.6 else 0

            if self.flip and random.random() <= 0.5:
                data_numpy = data_numpy[:, ::-1, :]
                joints, joints_vis = fliplr_joints(
                    joints, joints_vis, data_numpy.shape[1], self.flip_pairs)
                c[0] = data_numpy.shape[1] - c[0] - 1

        trans = get_affine_transform(c, s, r, self.image_size)
        input = cv2.warpAffine(
            data_numpy,
            trans,
            (int(self.image_size[0]), int(self.image_size[1])),
            flags=cv2.INTER_LINEAR)

        for i in range(self.num_joints):
            if joints_vis[i, 0] > 0.0:
                joints[i, 0:2] = affine_transform(joints[i, 0:2], trans)

        keypoint_input = copy.deepcopy(input)
        keypoint_joints = copy.deepcopy(joints)
        keypoint_joints_vis = copy.deepcopy(joints_vis)
        parts_input = copy.deepcopy(input)
        parts_joints = copy.deepcopy(joints)
        parts_joints_vis = copy.deepcopy(joints_vis)

        if self.transform:
            if len(self.occlusion_transforms_keypoint) > 0:
                sample = {'image': keypoint_input, 'target': keypoint_joints, 'joints_vis': keypoint_joints_vis}
                choose_transform = random.choice(self.occlusion_transforms_keypoint)
                sample = choose_transform(sample)
                sample = self.transform(sample)
                keypoint_input = sample['image']
                keypoint_joints = sample['target']
                keypoint_joints_vis = sample['joints_vis']

            if len(self.occlusion_transforms_parts) > 0:
                sample = {'image': parts_input, 'target': parts_joints, 'joints_vis': parts_joints_vis}
                choose_transform = random.choice(self.occlusion_transforms_parts)
                sample = choose_transform(sample)
                sample = self.transform(sample)
                parts_input = sample['image']
                parts_joints = sample['target']
                parts_joints_vis = sample['joints_vis']

            # Casual image tranform
            sample = {'image': input, 'target': joints, 'joints_vis': joints_vis}
            sample = self.transform(sample)
            input = sample['image']
            joints = sample['target']
            joints_vis = sample['joints_vis']

        target, target_weight = self.generate_target(joints, joints_vis)
        target = torch.from_numpy(target)
        target_weight = torch.from_numpy(target_weight)

        input_combined = {'normal': input, 'keypoints_occluded': keypoint_input, 'parts_occluded': parts_input}
        meta = {
            'image': image_file,
            'filename': filename,
            'imgnum': imgnum,
            'joints': joints,
            'joints_vis': joints_vis,
            'parts_joints_vis': parts_joints_vis,
            'center': c,
            'scale': s,
            'rotation': r,
            'score': score
        }

        return input_combined, target, target_weight, meta

    def get_image_dir_path(self, cfg):
        prefix = 'test2017' if 'test' in cfg.DATASET.TRAIN_SET else cfg.DATASET.TRAIN_SET
        prefix = 'train2017' if 'train' in cfg.DATASET.TRAIN_SET else prefix
        prefix = 'val2017' if 'val' in cfg.DATASET.TRAIN_SET else prefix

        return os.path.join(cfg.DATASET.ROOT, 'images', prefix)


from django import template
from django.conf import settings

register = template.Library()

@register.simple_tag(takes_context=True)
def render_gallery(context, plugin):
    try:
        return plugin.render_plugin(context, context['placeholder'])
    except:
        if settings.TEMPLATE_DEBUG:
            raise
        else:
            return u''


import sqlite3

conn = sqlite3.connect("vaccitrack-backend.db")
c = conn.cursor()
# Create table - USERS
c.execute(
    """CREATE TABLE USERS
             ([username] text PRIMARY KEY,[name] text,[app_id] text, [password] text,[email_id] text, [timestamp] date)"""
)
c.execute(
    """CREATE TABLE ADMIN
             ([username] text PRIMARY KEY,[name] text,[password] text, [timestamp] date)"""
)
c.execute(
    """ INSERT INTO USERS(username,name,app_id,email_id,password,timestamp)
              VALUES('admin_user','admin',123,'admin@vaccitrack.com','admin@123',123) """
)
conn.commit()


# -*- coding: utf-8 -*-
"""
AGS Gateway app factory
"""

import logging
import logging.config
import os

from flask import Flask, render_template

def create_app(config='config.py', **kwargs):
    app = Flask(__name__)
    app.config.from_pyfile(config)
    app.config.update(kwargs)

    configure_logger(app)

    register_blueprints(app)
    register_context_processors(app)
    register_error_handlers(app)
    register_extensions(app)

    return app

def configure_logger(app):
    app.logger
    logging.config.dictConfig(app.config.get('LOGGING', {}))

def register_blueprints(app):

    from app.main import main
    app.register_blueprint(main)

    from app.oidc_provider import oidc_provider
    app.register_blueprint(oidc_provider)

    from app.broker import broker
    app.register_blueprint(broker)

def register_context_processors(app):

    def base_context_processor():
        return {
            'asset_path': '/static/govuk_template/assets/',
        }

    app.context_processor(base_context_processor)

def register_error_handlers(app):

    error_handlers = {
        '404.html': [404],
        # avoid flask 0.11 bug with 402 and 407
        '4xx.html': [401, 405, 406, 408, 409],
        '5xx.html': [500, 501, 502, 503, 504, 505]}

    def make_handler(code, template):
        template = os.path.join('errors', template)

        def handler(e):
            return render_template(template, code=code), code

        return handler

    for template, codes in error_handlers.items():
        for code in codes:
            app.register_error_handler(code, make_handler(code, template))

def register_extensions(app):

    from app.assets import env
    env.init_app(app)

    from app.oidc_provider import init_oidc_provider
    app.provider = init_oidc_provider(app)

    from app.oidc_client import OIDCClient
    OIDCClient(app)


import sys
from itertools import combinations

sys.stdin = open('1767.txt', 'r')

dx = (-1, 1, 0, 0)
dy = (0, 0, -1, 1)

def dfs(n, combi, visit_k_origin, k=0, acc=0):
    global result

    if k == n:
        if acc < result:
            result = acc

        return

    for i in range(4):
        flag = False
        cnt = 0
        visit_k = [visit_k_origin[i][:] for i in range(N)]
        new_x = not_connect[combi[k]][0] + dx[i]
        new_y = not_connect[combi[k]][1] + dy[i]

        if not (0 <= new_x < N and 0 <= new_y < N):
            continue

        while 0 <= new_x < N and 0 <= new_y < N:
            if visit_k[new_x][new_y] != 0:
                flag = True
                break

            cnt += 1
            visit_k[new_x][new_y] = 2
            new_x += dx[i]
            new_y += dy[i]

        if flag:
            continue

        dfs(n, combi, visit_k, k+1, acc+cnt)

T = int(input())
for test_case in range(1, T+1):
    N = int(input())
    matrix = [list(map(int, input().split())) for _ in range(N)]

    not_connect = []
    for i in range(1, N-1):
        for j in range(1, N-1):
            if matrix[i][j] == 1:
                not_connect.append((i, j))

    connect = []
    for i in [0, N-1]:
        for j in range(N):
            if matrix[i][j] == 1:
                connect.append((i, j))

    for j in [0, N-1]:
        for i in range(1, N-1):
            if matrix[i][j] == 1:
                connect.append((i, j))

    l = len(not_connect)
    for n in range(l, -1, -1):
        result = 0xffffff
        for combi in combinations(range(l), n):
            visit_0_origin = [matrix[i][:] for i in range(N)]
            dfs(n, combi, visit_0_origin)

        if result != 0xffffff:
            break

    print('#{} {}'.format(test_case, result))


import os
import sys
import importlib
import argparse
import csv
import math
import numpy as np
import time
import pickle
import pathlib
import pyscipopt as scip

import tensorflow as tf
import torch
import utilities

class PolicyBranching(scip.Branchrule):

    def __init__(self, policy, device):
        super().__init__()

        self.policy_type = policy['type']
        self.policy_name = policy['name']
        self.device = device

        model = policy['model']
        model.restore_state(policy['parameters'])
        model.to(device)
        model.eval()
        self.policy = model.forward

    def branchinitsol(self):
        self.ndomchgs = 0
        self.ncutoffs = 0
        self.state_buffer = {}
        self.khalil_root_buffer = {}

    def branchexeclp(self, allowaddcons):

        candidate_vars, *_ = self.model.getPseudoBranchCands()
        candidate_mask = [var.getCol().getIndex() for var in candidate_vars]

        state = utilities.extract_state(self.model, self.state_buffer)
        c,e,v = state
        v = v['values'][candidate_mask]

        state_khalil = utilities.extract_khalil_variable_features(self.model, candidate_vars, self.khalil_root_buffer)

        var_feats = np.concatenate([v, state_khalil, np.ones((v.shape[0],1))], axis=1)
        var_feats = utilities._preprocess(var_feats, mode="min-max-2")

        var_feats = torch.as_tensor(var_feats, dtype=torch.float32).to(device)
        with torch.no_grad():
            var_logits = self.policy(var_feats).cpu().numpy()

        best_var = candidate_vars[var_logits.argmax()]
        self.model.branchVar(best_var)
        result = scip.SCIP_RESULT.BRANCHED

        # fair node counting
        if result == scip.SCIP_RESULT.REDUCEDDOM:
            self.ndomchgs += 1
        elif result == scip.SCIP_RESULT.CUTOFF:
            self.ncutoffs += 1

        return {'result': result}

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument(
        'problem',
        help='MILP instance type to process.',
        choices=['setcover', 'cauctions', 'facilities', 'indset'],
    )
    parser.add_argument(
        '-g', '--gpu',
        help='CUDA GPU id (-1 for CPU).',
        type=int,
        default=-1,
    )
    parser.add_argument(
        '-s', '--seed',
        help='seed for parallelizing the evaluation. Uses all seeds if not provided.',
        type=int,
        default=-1
    )
    parser.add_argument(
        '-l', '--level',
        help='size of instances to evaluate. Default is all.',
        type=str,
        default='all',
        choices=['all', 'small', 'medium', 'big']
    )
    parser.add_argument(
        '--trained_models',
        help='Directory of trained models.',
        type=str,
        default='trained_models/'
    )
    parser.add_argument(
        '-m', '--model_string',
        help='searches for this string in respective trained_models folder',
        type=str,
        default='',
    )
    parser.add_argument(
        '--model_name',
        help='searches for this model_name in respective trained_models folder',
        type=str,
        default='',
    )
    args = parser.parse_args()

    instances = []
    seeds = [0, 1, 2]
    time_limit = 2700

    ### OUTPUT DIRECTORY
    result_dir = f"eval_results/{args.problem}"
    os.makedirs(result_dir, exist_ok=True)
    device = "CPU" if args.gpu == -1 else "GPU"
    result_file = f"{result_dir}/mlp_{device}_{time.strftime('%Y%m%d-%H%M%S')}.csv"

    ### MODELS TO EVALUATE ###
    basedir = f"{args.trained_models}/{args.problem}"
    if args.model_string != "":
        models_to_evaluate = [y for y in pathlib.Path(basedir).iterdir() if args.model_string in y.name and 'mlp_' in y.name]
        assert len(models_to_evaluate) > 0, f"no model matched the model_string: {args.model_string}"
    elif args.model_name != "":
        model_path = pathlib.Path(f"{basedir}/{args.model_name}")
        assert model_path.exists(), f"path: {model_path} doesn't exist"
        assert 'mlp_' in model_path.name, f"only tests mlp models. model_path doesn't look like its mlp: {model_path}"
        models_to_evaluate = [model_path]
    else:
        models_to_evaluate = [y for y in pathlib.Path(f"{basedir}").iterdir() if 'mlp_' in y.name]
        assert len(models_to_evaluate) > 0, f"no model found in {basedir}"

    if args.problem == 'setcover':
        instances += [{'type': 'small', 'path': f"data/instances/setcover/transfer_500r_1000c_0.05d/instance_{i+1}.lp"} for i in range(20)]
        instances += [{'type': 'medium', 'path': f"data/instances/setcover/transfer_1000r_1000c_0.05d/instance_{i+1}.lp"} for i in range(20)]
        instances += [{'type': 'big', 'path': f"data/instances/setcover/transfer_2000r_1000c_0.05d/instance_{i+1}.lp"} for i in range(20)]

    elif args.problem == 'cauctions':
        instances += [{'type': 'small', 'path': f"data/instances/cauctions/transfer_100_500/instance_{i+1}.lp"} for i in range(20)]
        instances += [{'type': 'medium', 'path': f"data/instances/cauctions/transfer_200_1000/instance_{i+1}.lp"} for i in range(20)]
        instances += [{'type': 'big', 'path': f"data/instances/cauctions/transfer_300_1500/instance_{i+1}.lp"} for i in range(20)]

    elif args.problem == 'facilities':
        instances += [{'type': 'small', 'path': f"data/instances/facilities/transfer_100_100_5/instance_{i+1}.lp"} for i in range(20)]
        instances += [{'type': 'medium', 'path': f"data/instances/facilities/transfer_200_100_5/instance_{i+1}.lp"} for i in range(20)]
        instances += [{'type': 'big', 'path': f"data/instances/facilities/transfer_400_100_5/instance_{i+1}.lp"} for i in range(20)]

    elif args.problem == 'indset':
        instances += [{'type': 'small', 'path': f"data/instances/indset/transfer_750_4/instance_{i+1}.lp"} for i in range(20)]
        instances += [{'type': 'medium', 'path': f"data/instances/indset/transfer_1000_4/instance_{i+1}.lp"} for i in range(20)]
        instances += [{'type': 'big', 'path': f"data/instances/indset/transfer_1500_4/instance_{i+1}.lp"} for i in range(20)]

    else:
        raise NotImplementedError

    ### SEEDS TO EVALUATE ###
    if args.seed != -1:
        seeds = [args.seed]

    ### PROBLEM SIZES TO EVALUATE ###
    if args.level != "all":
        instances = [x for x in instances if x['type'] == args.level]

    branching_policies = []

    # GCNN models
    for model_path in models_to_evaluate:
        for seed in seeds:
            branching_policies.append({
                'type': 'mlp',
                'name': model_path.name,
                'seed': seed,
                'parameters': str(model_path / f"{seed}/best_params.pkl")
            })

    print(f"problem: {args.problem}")
    print(f"gpu: {args.gpu}")
    print(f"time limit: {time_limit} s")

    ### NUMPY / TORCH SETUP ###
    if args.gpu == -1:
        os.environ['CUDA_VISIBLE_DEVICES'] = ''
        device = torch.device("cpu")
    else:
        os.environ['CUDA_VISIBLE_DEVICES'] = f'{args.gpu}'
        device = torch.device(f"cuda" if torch.cuda.is_available() else "cpu")

    # load and assign tensorflow models to policies (share models and update parameters)
    loaded_models = {}
    for policy in branching_policies:
        if policy['type'] not in loaded_models:
            sys.path.insert(0, os.path.abspath(f"models/{policy['type']}"))
            import model
            importlib.reload(model)
            loaded_models[policy['type']] = model.Policy()
            del sys.path[0]
            loaded_models[policy['type']].to(device)
            loaded_models[policy['type']].eval()
        policy['model'] = loaded_models[policy['type']]

    print("running SCIP...")

    fieldnames = [
        'problem',
        'device',
        'policy',
        'seed',
        'type',
        'instance',
        'nnodes',
        'nlps',
        'stime',
        'gap',
        'status',
        'ndomchgs',
        'ncutoffs',
        'walltime',
        'proctime',
    ]

    with open(result_file, 'w', newline='') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        writer.writeheader()

        for instance in instances:
            print(f"{instance['type']}: {instance['path']}...")

            for policy in branching_policies:
                torch.manual_seed(policy['seed'])

                m = scip.Model()
                m.setIntParam('display/verblevel', 0)
                m.readProblem(f"{instance['path']}")
                utilities.init_scip_params(m, seed=policy['seed'])
                m.setIntParam('timing/clocktype', 1)  # 1: CPU user seconds, 2: wall clock time
                m.setRealParam('limits/time', time_limit)

                brancher = PolicyBranching(policy, device)
                m.includeBranchrule(
                    branchrule=brancher,
                    name=f"{policy['type']}:{policy['name']}",
                    desc=f"Custom MLPOpt branching policy.",
                    priority=666666, maxdepth=-1, maxbounddist=1)

                walltime = time.perf_counter()
                proctime = time.process_time()

                m.optimize()

                walltime = time.perf_counter() - walltime
                proctime = time.process_time() - proctime

                stime = m.getSolvingTime()
                nnodes = m.getNNodes()
                nlps = m.getNLPs()
                gap = m.getGap()
                status = m.getStatus()
                ndomchgs = brancher.ndomchgs
                ncutoffs = brancher.ncutoffs

                writer.writerow({
                    'policy': f"{policy['type']}:{policy['name']}",
                    'seed': policy['seed'],
                    'type': instance['type'],
                    'instance': instance['path'],
                    'nnodes': nnodes,
                    'nlps': nlps,
                    'stime': stime,
                    'gap': gap,
                    'status': status,
                    'ndomchgs': ndomchgs,
                    'ncutoffs': ncutoffs,
                    'walltime': walltime,
                    'proctime': proctime,
                    'problem':args.problem,
                    'device': device
                })

                csvfile.flush()
                m.freeProb()

                print(f"  {policy['type']}:{policy['name']} {policy['seed']} - {nnodes} ({nnodes+2*(ndomchgs+ncutoffs)}) nodes {nlps} lps {stime:.2f} ({walltime:.2f} wall {proctime:.2f} proc) s. {status}")


import pendulum
import os
from .helpers.s3_resource import S3_resource
from .helpers.fs_resource import FS_resource

datastores = {
    "S3": S3_resource,
    "filesystem": FS_resource
}

evKeys = [
    "LOCAL_DATA",
    "DATASTORE_NAME",
    "DATASTORE_PATH",
    "TMP_DIR",
    "METRO_LINES",
    "METRO_AGENCY",
    "TIMEZONE",
    "SCHEDULE_URL",
    "VEHICLE_API_URL",
    "LOG_TIMESTAMPS"
]

class Context:
    def __init__(self):
        self.config = {}

        for k in evKeys:
           self.config[k]=os.environ[k]

        self.config["METRO_LINES"] = [int(line) for line in self.config["METRO_LINES"].split(",")]
        self.datastore = datastores[self.config["DATASTORE_NAME"]](self.config["DATASTORE_PATH"])
        self.tmp = FS_resource(self.config["TMP_DIR"])

    def logger(self, stuff, datetime=pendulum.now('UTC')):
        if self.config["LOG_TIMESTAMPS"] == "TRUE":
            print(datetime, stuff, sep=",")
        else:
            print(stuff)