from pathlib import Path
import json
import collections
import pandas as pd
import numpy as np
import datetime
import csv

from experiment_utils import find_recent_file

def flatten_dict(d, parent_key='', sep='_'):
    items = []
    for k, v in d.items():
        new_key = parent_key + sep + k if parent_key else k
        if isinstance(v, collections.MutableMapping):
            items.extend(flatten_dict(v, new_key, sep=sep).items())
        else:
            items.append((new_key, v))
    return dict(items)

def read_json(path, prefix, vals=None, flat=True):
    """
    Reads the latest file in path that starts with the prefix, returning a dictionary of the values.

    :param path: folder path, the target file is the most recent file in this folder
                named "<prefix>*.json"
    :param vals: list of the values to read from the file.
    :return: a dictionary of values from the latest experiment parameter file
    """

    params_file = find_recent_file(path, prefix)
    if params_file == -1:
        # no file found
        return {x: None for x in vals}
    with open(str(params_file), 'r') as file:
        params = json.load(file)
    if vals:
        params = {x: params[x] for x in vals}
    if flat:
        params = flatten_dict(params)
    return params

def read_experiment_params(path, vals=None, flat=True):
    """
    Reads the latest experiment parameter file in path, and returns a dictionary of the values.

    :param path: folder path, the experiment parameter file is in this folder and is named
            "experiment_params_<timestamp>.json"
    :param vals: list of the values to read from the file.
    :param flat: if true, then will flatten out nested dicts
    :return: a dictionary of values from the latest experiment parameter file
    """

    if vals is None:
        vals = ['debug', 'name', 'experiment_number', 'dataset', 'model_type',
               'model_path', 'best_model_metric', 'quantization', 'prune_method',
               'prune_compression', 'finetune_epochs', 'attack_method', 'attack_kwargs',
               'email_verbose', 'gpu', 'save_one_checkpoint', 'seed', 'train_from_scratch',
               'train_kwargs', 'prune_kwargs']

    params = read_json(path=path, prefix="experiment_params", vals=vals, flat=flat)

    return params

def read_pruning_metrics(path, vals=None, flat=True):
    if vals is None:

        vals = ['size', 'size_nz', 'compression_ratio', 'flops',
                'flops_nz', 'theoretical_speedup', 'loss']

    prune_folder = str(Path(path) / "prune")
    params = read_json(path=prune_folder, prefix="metrics", vals=vals, flat=flat)

    # # rename args
    # if "size" in params:
    #     params["model_size"] = params["size"]
    #     params.pop("size")
    # else:
    #     params["model_size"] = None
    # if "size_nz" in params:
    #     params["model_size_nz"] = params["size_nz"]
    #     params.pop("size_nz")
    # else:
    #     params["model_size_nz"] = None

    return params

def read_attack_metrics(path, vals=None, flat=True, attack='pgd'):
    if vals is None:
        vals = ['results']
    attack_folder = str(Path(path) / "attacks" / attack)
    params = read_json(path=attack_folder, prefix="train_attack_results", vals=vals, flat=flat)

    # # format args
    # if "results" in params:
    #     if "adv_acc1" in params["results"]:
    #         params["adv_acc1"] = params["results"]["adv_acc1"]
    #     if "adv_acc5" in params["results"]:
    #         params["adv_acc5"] = params["results"]["adv_acc5"]
    #     params.pop("results")
    # else:
    #     params["adv_acc1"] = None
    #     params["adv_acc5"] = None
    #
    # if flat:
    #     params = flatten_dict(params)
    return params

def read_transfer_attack_metrics(path, vals=None, flat=True, attack='pgd'):
    if vals is None:
        vals = ['transfer_results', 'transfer_model_path']
    attack_folder = str(Path(path) / "attacks" / attack / "transfer_attack")
    params = read_json(path=attack_folder, prefix="train_attack_results", vals=vals, flat=flat)

    # # format args
    # if params"transfer_results" is not None:
    #     if "transfer_runtime" in params["transfer_results"]:
    #         params["transfer_results"].pop("transfer_runtime")
    # elif params["transfer_results"] is None:
    #     params["transfer_results"] = {
    #                         "inputs_tested": None,
    #                         "both_correct1": None,
    #                         "both_correct5": None,
    #                         "transfer_correct1": None,
    #                         "transfer_correct5": None,
    #                         "target_correct1": None,
    #                         "target_correct5": None,
    #                     }
    #
    # if flat:
    #     params = flatten_dict(params)
    return params

def read_accuracy_results(path, vals=None):
    """If model has been pruned, read from pruning logs, else from training logs."""

    if vals is None:
        vals = ['train_acc1', 'train_acc5', 'val_acc1', 'val_acc5']

    acc_path = Path(path) / "prune"
    if not acc_path.exists():
        acc_path = Path(path) / "train"
        if not acc_path.exists():
            raise FileNotFoundError(f"Could not find train or prune folder in {path}")

    csv_file = find_recent_file(acc_path, "logs")
    df = pd.read_csv(csv_file, usecols=vals)
    # this assumes no best_model_metric
    params = df.iloc[-1]
    params = params.to_dict()

    return {f"clean_{x}": params[x] for x in params}

def collect_one_experiment_data(path):
    experiment_params = read_experiment_params(path)
    pruning_metrics = read_pruning_metrics(path)
    attack_metrics = read_attack_metrics(path, attack=experiment_params['attack_method'])
    transfer_attack_metrics = read_transfer_attack_metrics(path, attack=experiment_params['attack_method'])
    accuracy_results = read_accuracy_results(path)

    results = {
        **experiment_params,
        **pruning_metrics,
        **attack_metrics,
        **transfer_attack_metrics,
        **accuracy_results,
    }
    return results

def find_experiment_folders(exp_folder, models, dataset='CIFAR10'):

    experiment_folders = []

    for model in models:
        model_path = exp_folder / model / dataset
        experiment_folders.extend(list(model_path.glob(f"{model}*")))

    return experiment_folders

def generate_csv(experiment_number, models=None, timestamp=True):
    if models is None:
        models = ['resnet20', 'googlenet', 'vgg_bn_drop', 'mobilenet_v2']

    experiment_x = Path.cwd() /"experiments" / f"experiment_{experiment_number}"
    experiment_folders = find_experiment_folders(experiment_x, models)

    csv_name = "collected_results.csv"
    if timestamp:
        timestamp = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
        csv_name = f"collected_results_{timestamp}.csv"
    csv_path = experiment_x / csv_name

    # want ordered & unique data type
    headers = {}
    data = []
    for exp in experiment_folders:
        results = collect_one_experiment_data(exp)
        headers.update({x:None for x in results})
        data.append(results)

    with open(csv_path, 'w') as file:
        writer = csv.DictWriter(file, fieldnames=headers.keys(), lineterminator='\n')
        writer.writeheader()

        for x in data:
            writer.writerow(x)

def generate_csv_new_workflow(experiment_number, models=None, timestamp=True):
    if models is None:
        models = ['resnet20', 'googlenet', 'vgg_bn_drop', 'mobilenet_v2']

    experiment_x = Path.cwd() /"experiments" / f"experiment_{experiment_number}"
    experiment_folders = find_experiment_folders(experiment_x, models)

    csv_name = "collected_results.csv"
    if timestamp:
        timestamp = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
        csv_name = f"collected_results_{timestamp}.csv"
    csv_path = experiment_x / csv_name

    # want ordered & unique data type
    headers = {}
    data = []
    for exp in experiment_folders:
        results = read_json(exp, prefix='experiment_results_', flat=True)
        headers.update({x:None for x in results})
        data.append(results)

    # make sure all data cases have the same headers
    for x in data:
        for key in headers:
            if key not in x:
                x[key] = None

    with open(csv_path, 'w') as file:
        writer = csv.DictWriter(file, fieldnames=headers.keys(), lineterminator='\n')
        writer.writeheader()

        for x in data:
            writer.writerow(x)

    return csv_path

def fill_empty_csv(experiment, path=None):
    """
    Fills in empty prune attack parameters and prune/quantized transfer attacks for
    models which were not pruned.  The transfer attack results will simply be the identity
    function.

    :param experiment:
    :param path:
    :return:
    """

    experiment_folder = Path.cwd() / 'experiments' / f'experiment_{experiment}'

    if path is None:
        path = find_recent_file(experiment_folder, prefix='collected_results')

    df = pd.read_csv(path)

    convert_results = [
        "clean_train_acc1",
        "clean_train_acc5",
        "clean_val_acc1",
        "clean_val_acc5",
        "model_size",
        "model_size_nz",
        "compression_ratio",
        "flops",
        "flops_nz",
        "theoretical_speedup",
        "model_file_size",
        "attack_results_inputs_tested",
        "attack_results_clean_acc1",
        "attack_results_clean_acc5",
        "attack_results_adv_acc1",
        "attack_results_adv_acc5",
        "attack_results_runtime"
    ]

    df_train_scratch = df[df['prune_model_file_size'].isna()]
    for x in convert_results:
        df[f'prune_{x}'] = np.where(df[f'prune_{x}'].isna(), df[f'train_{x}'], df[f'prune_{x}'])

    more_convert_results = [
        ("attack_transfer_both_correct1", "attack_results_adv_acc1"),
        ("attack_transfer_both_correct5", "attack_results_adv_acc5"),
        ("attack_transfer_transfer_correct1", "attack_results_adv_acc1"),
        ("attack_transfer_transfer_correct5", "attack_results_adv_acc5"),
        ("attack_transfer_target_correct1", "attack_results_adv_acc1"),
        ("attack_transfer_target_correct5", "attack_results_adv_acc5"),
    ]

    # columns attack_transfer_transfer_model, attack_transfer_transfer_runtime, and
    #   attack_transfer_inputs_tested are left blank

    for prefix_empty, prefix_replace in [('prune_', 'train_'), ('quantize_', 'quantize_')]:
        for empty, replace in more_convert_results:
            df[f'{prefix_empty}{empty}'] = np.where(
                df[f'{prefix_empty}{empty}'].isna(),
                df[f'{prefix_replace}{replace}'],
                df[f'{prefix_empty}{empty}']
            )

    new_csv = path.parent / f"formatted_{path.name}"
    # with open(new_csv, 'w') as f:
    df.to_csv(new_csv, index=False)
    return new_csv

def retrieve_formatted_csv_df(experiment, cols=None, regenerate=True):
    """
    If regenerate is true, regenerates the files and removes old ones.
    :param experiment:
    :param cols:
    :param replace:
    :return:
    """
    experiment_folder = Path.cwd() / 'experiments' / f'experiment_{experiment}'
    if not regenerate:
        raw_results = find_recent_file(experiment_folder, prefix='collected_results')
        if isinstance(raw_results, int):
            generate_csv_new_workflow(experiment)
        file = find_recent_file(experiment_folder, prefix='formatted_collected_results')
        if isinstance(file, int):
            file = fill_empty_csv(experiment)
    else:
        csvs = list(experiment_folder.glob('*.csv'))
        for f in csvs:
            f.unlink()
        generate_csv_new_workflow(experiment)
        file = fill_empty_csv(experiment)

    return pd.read_csv(file, usecols=cols)

def format_data_for_plot(experiment, metrics, pruning=None, models=None):
    """
    Returns a tuple ({model: groupby dataframe}, {model: big dataframe})

    The first item in the dict is for easy plotting, the second is for combining
    with other experiments

    Can plot the groupby dataframe using

    for pruning_strat, data in groupby_dataframe:
        x_axis = data['prune_compression']
        y_axis = data[metric]
        label = pruning_strat
        plt.plot(x_axis, y_axis, label=label)

    :param experiment: which experiment to get the data from
    :param metrics: list of columns which you want to plot on y axis.
    :param pruning: list of pruning strategies on which collect data
    :param models: list of models on which to collect data
    :return:
    """
    if not isinstance(metrics, list):
        metrics = [metrics]

    if models is None:
        models = ['resnet20', 'googlenet', 'vgg_bn_drop', 'mobilenet_v2']

    if pruning is None:
        pruning = ["RandomPruning", "GlobalMagWeight", "LayerMagWeight", "GlobalMagGrad", "LayerMagGrad",
            "GreedyPGDGlobalMagGrad", "GreedyPGDLayerMagGrad"]

    cols = [
        'name',
        'model_type',
        'prune_method',
        'prune_compression',
    ]
    cols.extend(metrics)
    df_orig = retrieve_formatted_csv_df(experiment, cols=cols)

    final_data = {}
    ungrouped_data = {}

    for model in models:
        df = df_orig[df_orig['model_type'] == model]
        zero_compression_metrics = {}
        for metric in metrics:
            zero_compression_metrics[metric] = df[df['name'] == model][metric].item()
        temp = {**zero_compression_metrics, 'prune_compression': 1}
        for prune_strat in pruning:
            df = df.append({**temp, 'prune_method': prune_strat}, ignore_index=True)
        df = df.sort_values('prune_compression')
        ungrouped_data[model] = df
        final_data[model] = df.groupby(['prune_method'])
        # for key, group in a:
        #     print(key)
        #     print("\n\n\n")
        #     print(group)
    return final_data, ungrouped_data

def format_multiple_experiment_data_for_plot(experiments, metrics, pruning=None, models=None):
    """
    Combines the data from multiple experiment runs into one result of the same format
    as format_data_for_plot(), except that the metrics columns are the averages of the
    results from each experiment, and there is an additional column for each metric which
    is the standard deviation for that metric.

    :param experiments:
    :param metrics:
    :param pruning:
    :param models:
    :return:
    """
    if not isinstance(experiments, list):
        experiments = [experiments]

    if not isinstance(metrics, list):
        metrics = [metrics]

    if models is None:
        models = ['resnet20', 'googlenet', 'vgg_bn_drop', 'mobilenet_v2']

    experiment_data = []
    for experiment_x in experiments:
        experiment_data.append(
            format_data_for_plot(
                experiment=experiment_x,
                metrics=metrics,
                pruning=pruning,
                models=models
            )[1]
        )
        a= 0

    # combine model dataframes
    model_dfs = {}

    for model in models:
        temp = pd.concat([x[model] for x in experiment_data])

        model_dfs[model] = temp.groupby(
            ['prune_method', 'prune_compression'],
            as_index=False,
        ).agg(
            {metrc:['mean', 'std'] for metrc in metrics}
        ).sort_values('prune_compression')

    return model_dfs

if __name__ == '__main__':
    #generate_csv_new_workflow(10)
    # clean_prune_acc(10)
    format_multiple_experiment_data_for_plot(
        experiments=[10,11, 12],
        metrics=['prune_clean_val_acc1', 'prune_attack_results_adv_acc1', 'prune_attack_transfer_target_correct1']
    )
    #'prune_attack_transfer_target_correct_1', 'quantize_attack_transfer_target_correct_1',
    # 'quantize_attack_results_adv_acc1'


#!/usr/bin/env python

from distutils.core import setup, Extension
import ConfigParser
import os

releaseFile = open('/etc/redhat-release','r')
distro_test = releaseFile.read()
d_version = distro_test[0].split()[0]
if d_version == 'Fedora':
    on_fedora = True
else:
    on_fedora = False

exec(open('src/_version.py').read())
name = 'newrhelic'
version = __version__
data_files=[
    ('/etc',['conf/newrhelic.conf']),
    ('/usr/share/doc/%s-%s'% (name, version), ['doc/README','doc/LICENSE']),
]
if on_fedora:
    data_files.append(('/usr/lib/systemd/system', ['scripts/newrhelic.service']))
else:
    data_files.append(('/etc/rc.d/init.d', ['scripts/newrhelic-plugin']))

setup(
    name=name,
    version=version,
    description='RHEL/CentOS monitoring plugin for New Relic',
    author='Jamie Duncan',
    author_email='jduncan@redhat.com',
    url='https://github.com/jduncan-rva/newRHELic',
    maintainer='Jamie Duncan',
    maintainer_email = 'jduncan@redhat.com',
    long_description='A RHEL 6/CentOS 6-specific monitoring plugin for New Relic (http://www.newrelic.com)',
    packages=['newrhelic'],
    package_dir={'newrhelic': 'src'},
    scripts = ['scripts/newrhelic'],
    data_files = data_files,
   )


from io import BytesIO
from PIL import Image, ImageDraw
import requests

dl = 175

def get_image_from_url(url):

    try:
        response = requests.head(url, allow_redirects=True)
        size = response.headers.get('content-length', -1)
    except:
        return "invalid image url :("
    if int(size) > 10 * (1 << 20):
        return "image too large :("

    try:
        response = requests.get(url)
        img = Image.open(BytesIO(response.content)).convert("RGBA")
    except:
        return "invalid image url :("

    img = img.resize((dl, dl))

    return img

def generate_image(arg1, arg2=None):

    if not arg2 is None:
        images = arg1
        healths = [x.get_health() for x in arg2]
    else:
        images = [arg1]
        healths = [100]

    if len(images) > 4:
        return None

    dx = 10
    dy = 10
    dh = 5
    x = 400
    y = dy * 4 + dl if len(images) <= 2 else dy * 5 + dl * 2

    image = Image.new("RGBA", (x, y))
    draw = ImageDraw.Draw(image)

    draw.rectangle([(0, 0), image.size], (25,129,153))
    draw.rectangle((dx, dy, x - dx, y - dy), (31,110,132))

    for i, img in enumerate(images):
        posx = dx * 2 + (dl + dx) * (i % 2)
        if i % 2 == 0 and i == len(images) - 1:
            posx = x // 2 - dl // 2

        posy = dy * 2 + (dl + dy) * (i // 2)

        image.paste(img, (posx, posy), img)
        draw.rectangle([(posx, posy + dl - dh), (posx + dl - 1, posy + dl)], (255,0,0,200))
        if healths[i] > 0:
            draw.rectangle([(posx, posy + dl - dh),
            (posx + dl * healths[i] // 100 - 1, posy + dl)],
            (0,255,0,200))

    return image

def generate_bytes(img):
    arr = BytesIO()
    img.save(arr, format='PNG')
    arr.seek(0)
    return arr


import fipy
import numpy as np

# All arguments on the left hand side are indices to the various constructions.
gmsh_text_box = '''
// Define the square that acts as the system boundary.

dx = %(dx)g;
Lx = %(Lx)g;
Ly = %(Ly)g;

// Define each corner of the square
// Arguments are (x, y, z, dx); dx is the desired cell size near that point.
Point(1) = {Lx / 2, Ly / 2, 0, dx};
Point(2) = {-Lx / 2, Ly / 2, 0, dx};
Point(3) = {-Lx / 2, -Ly / 2, 0, dx};
Point(4) = {Lx / 2, -Ly / 2, 0, dx};

// Line is a straight line between points.
// Arguments are indices of points as defined above.
Line(1) = {1, 4};
Line(2) = {4, 3};
Line(3) = {3, 2};
Line(4) = {2, 1};

// Loop is a closed loop of lines.
// Arguments are indices of lines as defined above.
Line Loop(1) = {1, 2, 3, 4};

'''

gmsh_text_radius = '''
R = %(R)g;

'''

gmsh_text_circle = '''
// Define a circle that acts as an obstacle

// Circle center coordinates
x = %(x)g;
y = %(y)g;
// The integer to start indexing objects from.
i = %(i)d;

// Define the center and compass points of the circle.
Point(i) = {x, y, 0, dx};
Point(i + 1) = {x - R, y, 0, dx};
Point(i + 2) = {x, y + R, 0, dx};
Point(i + 3) = {x + R, y, 0, dx};
Point(i + 4) = {x, y - R, 0, dx};

// Circle is confusingly actually an arc line between points.
// Arguments are indices of: starting point; center of curvature; end point.
Circle(i) = {i + 1, i, i + 2};
Circle(i + 1) = {i + 2, i, i + 3};
Circle(i + 2) = {i + 3, i, i + 4};
Circle(i + 3) = {i + 4, i, i + 1};

Line Loop(i) = {i, i + 1, i + 2, i + 3};

'''

gmsh_text_surface = '''
// The first argument is the outer loop boundary.
// The remainder are holes in it.
Plane Surface(1) = {%(args)s};
'''

def make_porous_mesh(r, R, dx, L):
    gmsh_text = gmsh_text_box % {'dx': dx[0], 'Lx': L[0], 'Ly': L[1]}
    loop_indexes = [1]
    if r is not None and len(r) and R:
        gmsh_text += gmsh_text_radius % {'R': R}
        for i in range(len(r)):
            index = 5 * (i + 1)
            gmsh_text += gmsh_text_circle % {'x': r[i][0], 'y': r[i][1],
                                             'i': index}
            loop_indexes += [index]
    surface_args = ', '.join([str(index) for index in loop_indexes])
    gmsh_text += gmsh_text_surface % {'args': surface_args}
    return fipy.Gmsh2D(gmsh_text)

if __name__ == '__main__':
    r = np.array([[0.0, 0.1], [0.3, 0.3]])
    R = 0.1
    dx = np.array([0.02, 0.02])
    L = np.array([1.0, 1.0])
    m = make_porous_mesh(r, R, dx, L)

    phi = fipy.CellVariable(m)
    v = fipy.Viewer(vars=phi, xmin=-L[0] / 2.0, xmax=L[0] / 2.0)
    v.plotMesh()
    raw_input()


#配置设备
deviceDic={
	'VBJDU19510007442':{#华为荣耀v10
		'platformName':'Android',
		'platformVersion':'9',
		# 'systemPort':'8200',
	},
	'iPhone 11 Pro Max':{
		"platformName": "iOS",
		# "platformVersion": "13.5",
	},
	'iPhone 11 Pro':{
		"platformName": "iOS",
		# "platformVersion": "13.5",
	},
	'iPhone 11':{
		"platformName": "iOS",
		# "platformVersion": "13.5",
	}
}

##################以下配置一般不用修改#################

receiver=['CMBI-****@****.***.**']
mailToCc=[]#抄送

import sys,os
platform=sys.platform
if platform=='darwin':
	try:
		ip=[a for a in os.popen('ifconfig en2').readlines() if 'inet' in a][1].split()[1]
	except:
		ip=[a for a in os.popen('ifconfig en1').readlines() if 'inet' in a][1].split()[1]
	port='80'
	host=f'http://{ip}:{port}/report'
	genPath=os.path.join(sys.path[0],'report/')
	webPath='~/zp/www/report/'
elif platform=='win32':
	# ip=[a for a in os.popen('route print').readlines() if ' 0.0.0.0 ' in a][0].split()[-2]
	ip='192.168.31.80'
	port='80'
	host=f'http://{ip}:{port}/report'
	genPath=os.path.join(sys.path[0],'report/')
	webPath='/WWW/report'

#regWeb开户保存的开户信息
regFilePath='testData/regFile.txt'

#regWeb开户自动修改的密码
newPwd='******'


#!/usr/bin/python3
# -*- coding:utf-8 -*-
from pyspark import SparkContext

import numpy as np
import csv
import sys
import time
import random

def swap(a, b) :
    return b, a

def init_cards(card_def, suitdef):
    cards = list()
    cards_str = list()
    a = 0
    b = 1
    sa = 0
    sb = -1
    for i in range(n) :
        if i % 13 == 0 :
            sa = 0
            sb += 1
            a += 100
            b = 1
        cards.append('%d.jpg' % (a + b))
        cards_str.append('%s%s' % (card_def[sa], suit_def[sb]))
        sa += 1
        b += 1
    return cards, cards_str

def gen_cards(cards) :
    n = 52
    for i in range(n) :
        j = random.randint(i, n - 1)
        cards[0][i], cards[0][j] = swap(cards[0][i], cards[0][j])
        cards[1][i], cards[1][j] = swap(cards[1][i], cards[1][j])
    return cards[0], cards[1]

if __name__ == '__main__' :

    logFile = "README.md"  # Should be some file on your system
    sc = SparkContext("local", "texas hold'em calc")

    n = 52
    card_def = ['A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K']
    suit_def = ['s', 'h', 'c', 'd']

    csvfile = open('cards.csv', 'w')
    writer = csv.writer(csvfile)
    count = 100000

    if len(sys.argv) > 1 :
        count = int(sys.argv[1])

    print ('num of card game:', count)
    data = list()
    t = time.time()
    cards, cards_str = init_cards(card_def, suit_def)

    lst = [[cards, cards_str] for n in range(count)]
    distData = sc.parallelize(lst)

    ori_data = distData.map(gen_cards)
    data = ori_data.map(lambda a: a[1]).collect()

    print('time to gen data:', time.time() - t)
    t = time.time()
    print('len of data:', len(data))
    writer.writerows(data)
    print('time to write:', time.time() - t)
    csvfile.close()

    sc.stop()


import subprocess
import threading
import os,signal
import psutil
import random

""" Run system commands with timeout
"""
def kill(proc_pid):
    process = psutil.Process(proc_pid)
    for proc in process.children(recursive=True):
        proc.kill()
    process.kill()

class Command(object):
    def __init__(self, cmd):
        self.cmd = cmd
        self.process = None
        self.out = None

    def run_command(self, capture = False):
        if not capture:
            self.process = subprocess.Popen(self.cmd, shell=True)
            self.process.communicate()
            return
        # capturing the outputs of shell commands
        self.process = subprocess.Popen(self.cmd, stdout=subprocess.PIPE, shell=True)
        out,err = self.process.communicate()
        if len(out) > 0:
            self.out = out.splitlines()
        else:
            self.out = None

    # set default timeout to 2 minutes
    def run(self, capture = False, timeout = 120, filename=""):
        thread = threading.Thread(target=self.run_command, args=(capture,))
        thread.start()
        thread.join(timeout)
        if thread.is_alive():
            print ("Command timeout, kill it: " + self.cmd)
            if os.path.exists(filename):
            	os.remove(filename)
            # self.process.kill()
            kill(self.process.pid)
            thread.join()
        return self.out

'''basic test cases'''

# # run shell command without capture
# Command('pwd').run()
# # capture the output of a command
# date_time = Command('date').run(capture=True)
# print (date_time)

# Command('echo "sleep 10 seconds"; sleep 10; echo "done"').run(timeout=2)

import os

def findProbs(containingDir,pddlFiles):
    probFiles = []
    for other in pddlFiles:
        head, tail = os.path.split(other)
        probName = tail.replace(".pddl","")
        dkel = containingDir+"/"+probName+"-redop.dkel"
        if other.startswith(containingDir) and not os.path.isfile(dkel) and not other.endswith("domain.pddl"):
            probFiles.append(other)
    return probFiles

benchmark = "blocksworld-large-simple"

pddlFiles = []
# r=root, d=directories, f = files
for r, d, f in os.walk(benchmark):
    for file in f:
        if '.pddl' in file:
            pddlFiles.append(os.path.join(r, file))
redop = "../../hsps/pddlcat -redop -a"
prep_domain = "../../hsps/pddlcat -prep -no-problem"
prep_prob = "../../hsps/pddlcat -prep -no-domain"

random.shuffle(pddlFiles)

for f in pddlFiles:
    if f.endswith("domain.pddl"):
        domain = f
        containingDir = domain.replace("domain.pddl","")
        probFiles = findProbs(containingDir,pddlFiles)
        probFiles.sort()
        for prob in probFiles:
            print(prob)
            date_time = Command('date').run(capture=True)
            print (date_time)
            head, tail = os.path.split(prob)
            probName = tail.replace(".pddl","")
            dkel = containingDir+"/"+probName+"-redop.dkel"

            reduced_domain = containingDir+"/"+"reduced-"+probName+"-domain.pddl"
            reduced_prob = containingDir+"/"+"reduced-"+probName+".pddl"
            Command(redop+" "+domain+" "+prob+" > "+dkel).run(timeout=1000, filename=dkel)
            if os.path.isfile(dkel):
            	Command(prep_domain+" "+domain+" "+prob+" "+dkel+" > "+reduced_domain).run(timeout=300)
            	Command(prep_prob+" "+domain+" "+prob+" "+dkel+" > "+reduced_prob).run(timeout=300)
            else:
            	break


from elasticsearch import Elasticsearch
import os

def get_es_instance(_type: str='local'):
    es_url = 'http://localhost:9200/'
    es_node = Elasticsearch(
        hosts=["localhost"],
        port=9200,
        timeout=20
    )

    if _type == 'remote':
        es_url = os.getenv('ELASTIC_URL', 'Elastic URL not found.')
        es_node = Elasticsearch(
            hosts=[es_url],
            port=9243,
            http_auth=(os.getenv('ELASTIC_USER', 'Elastic user not found.'), os.getenv('ELASTIC_PASS', 'Elastic pass not found.')),
            timeout=20
        )

    return es_url, es_node

class Config:
    SECRET_KEY = os.getenv('SECRET_KEY', 'my_secret_key')
    DEBUG = False
    JSON_SORT_KEYS = True

    ES_SIZE = 2000
    ES_SCROLL_TIMEOUT = '1m'

    URL_ENDPOINTS = {
        'references_base': '/rd-cross-referencing',
        'references_orphacodes': '/rd-cross-referencing/orphacodes',
        'references_by_orphacode': '/rd-cross-referencing/orphacodes/{}',
        'references_by_name': '/rd-cross-referencing/orphacodes/names/{}',
        'references_by_omim': '/rd-cross-referencing/omims/{}',
        'references_icds': '/rd-cross-referencing/icd-10s',
        'references_by_icd': '/rd-cross-referencing/icd-10s/{}',
        'classification_hchids': '/rd-classification/hchids',
        'classification_by_hchid': '/rd-classification/hchids/{}',
        'classification_orphacodes_by_hchid': '/rd-classification/hchids/{}/orphacodes',
        'classification_hchids_by_orphacode': '/rd-classification/orphacodes/{}/hchids',
        'classification_by_orphacode_and_hchid': '/rd-classification/orphacodes/{}/hchids/{}',
        'phenotypes_base': '/rd-phenotypes',
        'phenotypes_orphacodes': '/rd-phenotypes/orphacodes',
        'phenotypes_by_orphacode': '/rd-phenotypes/orphacodes/{}',
        'phenotypes_by_hpoids': '/rd-phenotypes/hpoids/{}',
        'genes_base': '/rd-associated-genes',
        'genes_orphacodes': '/rd-associated-genes/orphacodes',
        'genes_by_orphacode': '/rd-associated-genes/orphacodes/{}',
        'genes_genes': '/rd-associated-genes/genes',
        'genes_by_symbol': '/rd-associated-genes/genes/symbols/{}',
        'genes_by_name': '/rd-associated-genes/genes/names/{}',
        'linearization_by_orphacode': '/rd-medical-specialties/orphacodes/{}',
        'linearization_parents': '/rd-medical-specialties/parents',
        'linearization_by_parent': '/rd-medical-specialties/parents/{}',
        'epidemiology_base': '/rd-epidemiology',
        'epidemiology_orphacodes': '/rd-epidemiology/orphacodes',
        'epidemiology_by_orphacode': '/rd-epidemiology/orphacodes/{}',
        'history_base': '/rd-natural_history',
        'history_orphacodes': '/rd-natural_history/orphacodes',
        'history_by_orphacode': '/rd-natural_history/orphacodes/{}'
    }

    BACKEND_BASE_URL = "https://api.orphadata.com"

class DevelopmentConfig(Config):
    DEBUG = True

    ES_URL, ES_NODE = get_es_instance(_type='remote')

class TestingConfig(Config):
    DEBUG = True
    TESTING = True
    PRESERVE_CONTEXT_ON_EXCEPTION = False

    ES_URL, ES_NODE = get_es_instance(_type='local')

class ProductionConfig(Config):
    DEBUG = False

    ES_URL, ES_NODE = get_es_instance(_type='remote')

config_by_name = dict(
    dev=DevelopmentConfig,
    test=TestingConfig,
    production=ProductionConfig
)

key = Config.SECRET_KEY


import numpy as np
def calculate(list):
    # raise exception if the length of array is less than 9
    if len(list) < 9:
        raise ValueError("Array size is less than 9")

    elif len(list) >= 9:
        # Converting list into dictionary
        array = np.array(list)
        array = array.reshape(3,3)
        calculations = {
        'mean': [array.mean(axis=0),
                 array.mean(axis=1),
                 array.mean()],

        'variance': [array.var(axis=0),
                     array.var(axis=1),
                     array.var()],

        'standard deviation': [array.std(axis=0),
                               array.std(axis=1),
                               array.std()],

        'max': [array.max(axis=0),
                array.max(axis=1),
                array.max()],

        'min': [array.min(axis=0),
                array.min(axis=1),
                array.min()],

        'sum':[array.sum(axis=0),
               array.sum(axis=1),
               array.sum()],
        }
        return calculations
    else:
        pass
print(calculate([0,1,2,3,4,5,6,7,8]))


# Importing packages
import matplotlib.pyplot as plt
import numpy as np

# Creating a sample data for x-values
x = np.arange(2, 40000, 4)

variable = (104.33/1000*x)
fijo = (0*x) + 1140000/1000
varmasfijo = (104.33/1000*x)+ 1140000/1000
ventas = (150/1000*x)

# Plotting the line created by the linear function
plt.plot(x, variable, 'c', linewidth=2, label='C. Variable')
plt.plot(x, fijo, 'b', linewidth=2, label='C. Fijo')
plt.plot(x, varmasfijo, 'g', linewidth=2, label='CF + CV')
plt.plot(x, ventas, 'r', linewidth=2, label='I. Ventas')

plt.title('Diagrama de Knoppel')
plt.xlabel('Litros producidos')
plt.ylabel('Miles de Pesos')
plt.legend()
plt.show()


import requests, json

endpoint = 'http://91909711.ngrok.io/'

def predict(radiant, dire):
    get_args = '?'
    for hero in radiant:
        get_args += f'r={hero}&'
    for hero in dire:
        get_args += f'd={hero}&'

    data = requests.get(endpoint+get_args)
    try:
        json_data = json.loads(data.text)
        p = json_data['p'][0]
    except Exception as e:
        print(e)
        p = 1

    # 0.75; 0.86; 1; 1.16; 1.33
    if   p < 0.66:msg = 'Serious advantage for dire'
    elif p < 0.75:msg = 'Advantage for dire'
    elif p < 1.33:msg = 'Almost equal picks (within error margin)'
    elif p < 1.52:msg = 'Advantage for radiant'
    else:         msg = 'Serious advantage for radiant'
    return {'probability': p, 'msg': msg}


from .winapi import *

class MemoryRegion:
    def __init__(self, handle: int, mem_basic_info: MEMORY_BASIC_INFORMATION) -> None:
        """
        :param handle: Windows process handle, requires PROCESS_VM_READ access right
        :param mem_basic_info: Memory region information
        """
        self.handle = handle
        self.base_address = mem_basic_info.BaseAddress
        self.region_size = mem_basic_info.RegionSize
        self._buffer = None

    @property
    def buffer(self) -> ct.Array[ct.c_char]:
        """
        :return: Cached buffer of the memory region
        :raises WinApiError: If failed to read memory region
        """
        if self._buffer is None:
            self._buffer = ct.create_string_buffer(self.region_size)
            self._read_buffer()

        return self._buffer

    def clear_cache(self) -> None:
        """
        Clears the cache buffer.
        """
        self._buffer = None

    def _read_buffer(self) -> None:
        """
        Reads the memory region into the buffer.

        :raises WinApiError: If failed to read memory region
        """
        bytes_read = ct.c_size_t()
        success = ReadProcessMemory(self.handle, self.base_address, self.buffer, self.region_size, ct.byref(bytes_read))
        if not success or bytes_read.value != self.region_size:
            raise WinApiError


from flask import Flask, request, jsonify
from pathlib import Path
import os

app = Flask(__name__)

@app.route('/api/get_content', methods=['POST'])

def ordenar():
    order = ""
    reverse_order = ""
    data = request.get_json(force=True)
    path = data['path']

    if 'order' in data.keys():
        order = str(data['order'])
    if 'reverse_order' in data.keys():
        reverse_order = str(data['reverse_order'])

    if order == "size" and reverse_order == "" or reverse_order == "False":
        j=0
        dic={}
        paths = sorted(Path(path).iterdir(), key=os.path.getsize)
        for i in paths:
            dic[j] = [str(i)]
            j+=1
        return jsonify(dic)

    elif order == "size" and reverse_order == "True":
        j=0
        dic={}
        paths = sorted(Path(path).iterdir(), key=os.path.getsize)
        paths.reverse()
        for i in paths:
            dic[j] = [str(i)]
            j+=1
        return jsonify(dic)

    elif order == "" and reverse_order == "True":
        j=0
        dic={}
        paths = sorted(Path(path).iterdir(), key=os.path.getmtime)
        paths.reverse()
        for i in paths:
            dic[j] = [str(i)]
            j+=1
        return jsonify(dic)

    else:
        j=0
        dic={}
        paths = sorted(Path(path).iterdir(), key=os.path.getmtime)
        for i in paths:
            dic[j] = [str(i)]
            j+=1
        return jsonify(dic)

@app.route('/', methods=['GET'])
def index():
    return jsonify({'Mensaje':'Viva Peru!'})

if __name__ == "__main__":
    app.run('0.0.0.0', 5000, debug=True)


#!/user/bin/env python

"""null.py

This is a sample implementation of the 'Null Object' design pattern.

Roughly, the goal with Null objects is to provide an 'intelligent'
replacement for the often used primitive data type None in Python or
Null (or Null pointers) in other languages. These are used for many
purposes including the important case where one member of some group
of otherwise similar elements is special for whatever reason. Most
often this results in conditional statements to distinguish between
ordinary elements and the primitive Null value.

Among the advantages of using Null objects are the following:

  - Superfluous conditional statements can be avoided
    by providing a first class object alternative for
    the primitive value None.

  - Code readability is improved.

  - Null objects can act as a placeholder for objects
    with behaviour that is not yet implemented.

  - Null objects can be replaced for any other class.

  - Null objects are very predictable at what they do.

To cope with the disadvantage of creating large numbers of passive
objects that do nothing but occupy memory space Null objects are
often combined with the Singleton pattern.

For more information use any internet search engine and look for
combinations of these words: Null, object, design and pattern.

Dinu C. Gherman,
August 2001
"""

class Null:
    """A class for implementing Null objects.

    This class ignores all parameters passed when constructing or
    calling instances and traps all attribute and method requests.
    Instances of it always (and reliably) do 'nothing'.

    The code might benefit from implementing some further special
    Python methods depending on the context in which its instances
    are used. Especially when comparing and coercing Null objects
    the respective methods' implementation will depend very much
    on the environment and, hence, these special methods are not
    provided here.
    """

    # object constructing

    def __init__(self, *args, **kwargs):
        "Ignore parameters."
        return None

    # object calling

    def __call__(self, *args, **kwargs):
        "Ignore method calls."
        return self

    # attribute handling

    def __getattr__(self, mname):
        "Ignore attribute requests."
        return self

    def __setattr__(self, name, value):
        "Ignore attribute setting."
        return self

    def __delattr__(self, name):
        "Ignore deleting attributes."
        return self

    # misc.

    def __repr__(self):
        "Return a string representation."
        return "<Null>"

    def __str__(self):
        "Convert to a string and return it."
        return "Null"

def test():
    "Perform some decent tests, or rather: demos."

    # constructing and calling

    n = Null()
    n = Null('value')
    n = Null('value', param='value')

    n()
    n('value')
    n('value', param='value')

    # attribute handling

    n.attr1
    n.attr1.attr2
    n.method1()
    n.method1().method2()
    n.method('value')
    n.method(param='value')
    n.method('value', param='value')
    n.attr1.method1()
    n.method1().attr1

    n.attr1 = 'value'
    n.attr1.attr2 = 'value'

    del n.attr1
    del n.attr1.attr2.attr3

    # representation and conversion to a string

    assert repr(n) == '<Null>'
    assert str(n) == 'Null'

if __name__ == '__main__':
    test()


# -*- coding: utf-8 -*-

from constants import (
    ACTION_VMWARE_MANAGER_HEALTH_CHECK_HEALTH,
)
from api.constants import (
    CHANNEL_API,
    CHANNEL_SESSION,
    ROLE_GLOBAL_ADMIN,
    ROLE_ZONE_ADMIN,
    ROLE_CONSOLE_ADMIN,
    ROLE_NORMAL_USER,
    ROLE_PARTNER,
    ROLE_AGENT,
)

API_ACL_VMWARE_MANAGER_HEALTH = {
        ACTION_VMWARE_MANAGER_HEALTH_CHECK_HEALTH: {
            CHANNEL_API: [ROLE_GLOBAL_ADMIN, ROLE_NORMAL_USER,
                          ROLE_CONSOLE_ADMIN, ROLE_ZONE_ADMIN,
                          ROLE_PARTNER, ROLE_AGENT],
            CHANNEL_SESSION: [ROLE_GLOBAL_ADMIN, ROLE_NORMAL_USER,
                              ROLE_CONSOLE_ADMIN, ROLE_ZONE_ADMIN,
                              ROLE_PARTNER, ROLE_AGENT],
        }
}


import argparse
import random
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torchvision import datasets, transforms, utils
from tqdm import tqdm
from pathlib import Path
from model import BinarizeMnist, VAE_SingleLayer, VAE_TwoLayer
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"

def main(args):
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model = VAE_TwoLayer()
    model.to(device)
    checkpoint = torch.load(args.resume, map_location=lambda storage, loc: storage)
    model.load_state_dict(checkpoint["model"], strict=False)
    transform_test= transforms.Compose([
        transforms.ToTensor(),
        BinarizeMnist()
    ])
    test_dataset = datasets.MNIST("./MNIST", train=False, download=True,
                                    transform=transform_test)
    test_dataloader = DataLoader(test_dataset, batch_size=1, shuffle=False, drop_last=True,
                                 num_workers=args.num_workers, pin_memory=True)
    N = 1 * 28 * 28
    model.eval()
    average_elbo_favi = average_elbo_naive = average_eblo_appro = average_eblo_accur = 0
    valid_i = 1
    for i, (images, _) in enumerate(test_dataloader, 1):
        images = images.to(device)
        elbo_favi, elbo_naive, elbo_appro, elbo_accur = model.eval_savi(images, args.num_iter, args.learning_rate, "sgvb2", 3470)
        if torch.isnan(elbo_favi).any() or torch.isnan(elbo_naive).any() or torch.isnan(elbo_appro).any() or torch.isnan(elbo_accur).any():
            # ignore nan sample at this time
            continue
        valid_i += 1
        elbo_favi = torch.mean(elbo_favi / N, dim=0)
        elbo_naive = torch.mean(elbo_naive / N, dim=0)
        elbo_appro = torch.mean(elbo_appro / N, dim=0)
        elbo_accur = torch.mean(elbo_accur / N, dim=0)
        average_elbo_favi += elbo_favi.item()
        average_elbo_naive += elbo_naive.item()
        average_eblo_appro += elbo_appro.item()
        average_eblo_accur += elbo_accur.item()
        if i == 500:
            break
    average_elbo_favi /= valid_i
    average_elbo_naive /= valid_i
    average_eblo_appro /= valid_i
    average_eblo_accur /= valid_i
    print("[test] elbo favi: {:.10f}, elbo naive: {:.10f}, elbo approx: {:.10f}, elbo accur: {:.10f}"
        .format(average_elbo_favi * N, average_elbo_naive * N, average_eblo_appro * N, average_eblo_accur * N))

if __name__ == "__main__":

    SEED=3470
    random.seed(SEED)
    np.random.seed(SEED)
    torch.manual_seed(SEED)
    torch.cuda.manual_seed(SEED)
    torch.backends.cudnn.deterministic=True
    parser = argparse.ArgumentParser()
    parser.add_argument("--num-workers", type=int, default=4, help="Number of dataloader workers.")
    parser.add_argument("--num-iter", type=int, default=50, help="Number of training steps.")
    parser.add_argument("--learning-rate", type=float, default=1e-3, help="Learning rate.")
    parser.add_argument("--resume", type=str, default="", help="model load path.")
    args = parser.parse_args()
    main(args)

5.1. Evaluation on Density Estimation/infer.py


import json
from django.core.management.base import BaseCommand

from meetup.models import (
    MeetupProgram,
    Stage,
    Block,
    Event
)

class Command(BaseCommand):
    def handle(self, *args, **options):
        meetups_filepath = 'meetups.json'

        with open(meetups_filepath, encoding='UTF-8', mode='r') as f:
            meetups = json.loads(f.read())
        for serialized_meetup in meetups:
            meetup, _ = MeetupProgram.objects.get_or_create(
                title=serialized_meetup['title'],
                defaults={
                    'date': serialized_meetup['date'],
                    'start_time': serialized_meetup['start_time'],
                    'end_time': serialized_meetup['end_time'],
                }
            )
            for serialized_stage in serialized_meetup['stages']:
                stage, _ = Stage.objects.get_or_create(
                    title=f'{serialized_stage["title"]}. {meetup.title}',
                    defaults={
                        'program': meetup,
                        'start_time': serialized_stage['start_time'],
                        'end_time': serialized_stage['end_time'],
                    }
                )
                for serialized_block in serialized_stage['blocks']:
                    block, _ = Block.objects.get_or_create(
                        title=f'{serialized_block["title"]}. {serialized_stage["title"]}',
                        defaults={
                            'stage': stage,
                            'start_time': serialized_block['start_time'],
                            'end_time': serialized_block['end_time'],
                        }
                    )
                    for serialized_event in serialized_block['events']:
                        event, _ = Event.objects.get_or_create(
                            title=f'{serialized_event["title"]}. {serialized_stage["title"]}',
                            defaults={
                                'block': block,
                                'start_time': serialized_event['start_time'],
                                'end_time': serialized_event['end_time'],
                            }
                        )


import csv
import os, json
import codecs
import sys
import fileinput
import textdistance

class CSVtoDict:

    def __init__(self, input_file, delimiter=None, order=None):
        """Initalizes relevant variables.

        Args:
            input_file (str): Input CSV file to be read and turned into a list
            of python dictionaries.
        """
        self.order = order
        self.input_file = input_file
        self.list_of_dicts = []
        self.attr_list_of_dicts = []
        self.headers = []
        if delimiter:
            self.delimiter = delimiter
        else:
            self.delimiter = "_"

    def execute(self):
        self.cleanData()
        self.columnTitles()
        self.toDict()
        if os.path.exists(self.input_file):
            os.remove(self.input_file)
        self.list_of_dicts = self.removeDupe(self.list_of_dicts)
        p, a = self.assignRemoveAttributesTitles()
        if not self.order:
            self.order = p
        self.assignRemoveAttributes()
        return self.list_of_dicts, self.attr_list_of_dicts

    def cleanData(self):
        f = open(self.input_file, 'r')
        text = f.read()

        lines = [line.lower() for line in text]
        self.input_file = self.input_file[:self.input_file.find(".csv")] + "_temp.csv"
        with open(self.input_file, 'w') as out:
            out.writelines(lines)

    def columnTitles(self):
        """
        Grabs the column/category titles.
        """

        with codecs.open(self.input_file, encoding='UTF-8') as f:
            reader = csv.reader(f)
            self.headers = next(reader)

    def toDict(self):
        """Reads in CSV file and

        Returns:
            None
        """
        with codecs.open(self.input_file, encoding='UTF-8') as csvfile:
            scrape_dict = csv.DictReader(csvfile)
            self.list_of_dicts = [x for x in scrape_dict]

    def removeDupe(self, removeFrom):
        """
        Removes duplicate dictionary entries from the list.
        """
        s = {tuple(d.items()) for d in removeFrom}
        return [dict(t) for t in s]

    def assignRemoveAttributesTitles(self):
        """
        If there are columns in the csv files that are attributes of a larger class, this function will
        appropriately group them in the appropriate dictionary.
        A column is considered an attribute of another column if it's title is of the following structure:
        columnname_attributename
        E.g.verb_translation given that there is a separate column entitled 'verb'.
        """
        primary = set()
        attribute = {}
        headers = set()
        for col_name in self.headers:
                if self.delimiter not in col_name:
                    headers.add(col_name)

        if self.order:
            primary = set(self.order)
            for o in primary:
                if o not in headers:
                    print("------------------")
                    print("[FATAL ERROR]\n{} - Invalid column name. Check order.csv file.".format(o))
                    mispelt = list()
                    for h in headers:
                        if textdistance.levenshtein(o, h) <=2:
                            mispelt.append(h)
                    if mispelt:
                        print("Did you mean: {}?\n".format(", ".join(mispelt)))
                    print("------------------")
                    sys.exit()

        else:
            primary = headers

        for col_name in self.headers:
            if self.delimiter in col_name:
                prim_attr = col_name.split(self.delimiter)
                if prim_attr[0] in primary:
                    attribute[col_name] = {"primary":prim_attr[0], "attribute":prim_attr[1]}

        return primary, attribute

    def assignRemoveAttributes(self):
        """
        Attributes of the primary categories are redundant in the final tree. They should be removed before the tree is made.
        """
        primary, attribute = self.assignRemoveAttributesTitles()
        temp_dicts_list = []
        for col in self.list_of_dicts:
            no_attr = {}
            attr = {}
            for x in col:
                if x in primary:
                    no_attr[x] = col[x]
                    if x not in attr:
                        attr[x] = {}
                        attr[x]["id"] = col[x]
                    elif "id" not in attr[x]:
                        attr[x]["id"] = col[x]

                if x in attribute:
                    if attribute[x]["primary"] not in attr:
                        attr[attribute[x]["primary"]] = {}
                    attr[attribute[x]["primary"]][attribute[x]["attribute"]] = col[x]

            temp_dicts_list.append(no_attr)
            self.attr_list_of_dicts.append(attr)
        self.list_of_dicts = temp_dicts_list


import math
#n = int(input())
#x = [int(n) for n in input().split()]

a = []
b = 1
for i in range(100):
    a += pow(10,b)[i]
    b +=1

print(a)


#update text file

print("This program will update a text file with a new line of text.")

def main():
    #open file
    infile = open("test.txt", "r")
    #read file
    data = infile.read()
    #close file
    infile.close()
    #print file
    print(data)
    #open file
    infile = open("test.txt", "a")
    #write new line
    infile.write("\nThis is a new line of text.")
    #close file
    infile.close()
    #open file
    infile = open("test.txt", "r")
    #read file
    data = infile.read()
    #close file
    infile.close()
    #print file
    print(data)

if __name__ == "__main__":
    main()


# start working util funciton 27-step 1
import numpy as np
sin = np.sin
cos = np.cos
sqrt = np.sqrt
pi = np.pi
exp = np.exp
e = np.e
dot = np.dot
array = np.array
sum = np.sum
matmul = np.matmul
where = np.where
sign = np.sign
min = np.min
round = np.round
ceil = np.ceil
ones = np.ones
concatenate = np.concatenate

def f1_elliptic__(solution=None):
    result = 0
    for i in range(len(solution)):
        result += (10**6)**(i/(len(solution)-1)) * solution[i]**2
    return result

def f2_bent_cigar__(solution=None):
    return solution[0]**2 + 10**6 * sum(solution[1:]**2)

def f3_discus__(solution=None):
    return 10**6 * solution[0]**2 + sum(solution[1:]**2)

def f4_rosenbrock__(solution=None):
    result = 0.0
    for i in range(len(solution)-1):
        result += 100*(solution[i]**2 - solution[i+1])**2 + (solution[i] - 1)**2
    return result

def f5_ackley__(solution=None):
    return -20*exp(-0.2*sqrt(sum(solution**2)/len(solution))) - exp(sum(cos(2*pi*solution))/len(solution)) + 20 + e

def f6_weierstrass__(solution=None, a=0.5, b=3, k_max=20):
    result = 0.0
    for i in range(0, len(solution)):
        t1 = sum([a**k * cos(2*pi*b**k*(solution[i] + 0.5)) for k in range(0, k_max)])
        result += t1
    t2 = len(solution) * sum([a**k * cos(2*pi*b**k * 0.5) for k in range(0, k_max)])
    return result - t2

def f7_griewank__(solution=None):
    result = sum(solution ** 2) / 4000
    temp = 1.0
    for i in range(len(solution)):
        temp *= cos(solution[i] / sqrt(i + 1))
    return result - temp + 1

def f8_rastrigin__(solution=None):
    return sum(solution ** 2 - 10 * cos(2 * pi * solution) + 10)

def f9_modified_schwefel__(solution=None):

    z =1.0*solution
    for i in range(len(solution)):
        z[i] = z[i] + 4.209687462275036e+002

    #z = solution + 4.209687462275036e+002
    result = 418.9829 * len(solution)
    for i in range(0, len(solution)):
        if z[i] > 500:
            result -= (500 - z[i]%500)*sin(sqrt(abs(500 - z[i]%500))) - (z[i] - 500)**2 / (10000*len(solution))
        elif z[i] < -500:
            result -= (z[i]%500 - 500)*sin(sqrt(abs(z[i]%500 - 500))) - (z[i] + 500)**2 / (10000*len(solution))
        else:
            result -= z[i]*sin(abs(z[i])**0.5)
    return result

def f10_katsuura__(solution=None):
    result = 1.0
    for i in range(0, len(solution)):
        t1 = sum([abs(2 ** j * solution[i] - round(2 ** j * solution[i])) / 2 ** j for j in range(1, 33) ])
        result *= (1 + (i+1)*t1)**(10.0 / len(solution)**1.2)
    return (result - 1) * 10 / len(solution)**2

def f11_happy_cat__(solution=None):
    return (abs(sum(solution**2) - len(solution)))**0.25 + (0.5 * sum(solution**2) + sum(solution))/len(solution) + 0.5

def f12_hgbat__(solution=None):
    return (abs(sum(solution**2)**2 - sum(solution)**2))**0.5 + (0.5*sum(solution**2)+sum(solution))/len(solution) + 0.5

def f13_expanded_griewank__(solution=None):
    def __f4__(x=None, y=None):
        return 100*(x**2 - y)**2 + (x - 1)**2
    def __f7__(z=None):
        return z**2/4000 - cos(z/sqrt(1)) + 1

    result = __f7__(__f4__(solution[-1], solution[0]))
    for i in range(0, len(solution) - 1):
        result += __f7__(__f4__(solution[i], solution[i + 1]))
    return result

def f14_expanded_scaffer__(solution=None):
    def __xy__(x, y):
        return 0.5 + (sin(sqrt(x ** 2 + y ** 2)) ** 2 - 0.5) / (1 + 0.001 * (x ** 2 + y ** 2)) ** 2

    result = __xy__(solution[-1], solution[0])
    for i in range(0, len(solution) - 1):
        result += __xy__(solution[i], solution[i + 1])
    return result

#def F28(solution=None, shift_data=None, matrix=None,f_bias=None):
def F28(solution , shift_data , matrix,sda_01, ma_01):
    problem_size = len(solution)
    xichma = array([10, 20, 30, 40, 50])
    lamda = array([2.5, 10, 2.5, 5e-4, 1e-6])
    bias = array([0, 100, 200, 300, 400])

    '''
    if problem_size in SUPPORT_DIMENSION_2:
        f_matrix = f_matrix + str(problem_size) + ".txt"
    else:
        print("CEC 2014 function only support problem size 10, 20, 30, 50, 100")
        return 1
    shift_data = load_matrix_data__(f_shift_file)[:problem_size]
    shift_data = shift_data[:, :problem_size]
    matrix = load_matrix_data__(f_matrix)
    '''
    ##################################################
    ###########                            ###########
    ##################################################

    def F15(solution, sda_01, ma_01):
        problem_size = len(solution)
        bias = 0 # bias in composed is zero not 1500

        shift_data = sda_01[:problem_size]
        matrix = ma_01
        z = 5 * (solution - shift_data) / 100
        z = dot(z, matrix) + 1
        result = f13_expanded_griewank__(z) + bias
        return result

    # 1. Rotated Expanded Griewank’s plus Rosenbrock’s Function F15’
    t1 = solution - shift_data[0]
    g1 = lamda[0] * F15(solution, sda_01, ma_01) + bias[0]
    ##################################################
    ###########                            ###########
    ##################################################
    w1 = (1.0 / sqrt(sum(t1 ** 2))) * exp(-sum(t1 ** 2) / (2 * problem_size * xichma[0] ** 2))

    # 2. Rotated HappyCat Function F13’
    t2 = solution - shift_data[1]
    g2 = lamda[1] * f11_happy_cat__(dot(matrix[problem_size:2 * problem_size, :], t2)) + bias[1]
    w2 = (1.0 / sqrt(sum(t2 ** 2))) * exp(-sum(t2 ** 2) / (2 * problem_size * xichma[1] ** 2))

    # 3. Rotated Schwefel's Function F11’
    t3 = solution - shift_data[2]
    g3 = lamda[2] * f9_modified_schwefel__(dot(matrix[2 * problem_size: 3 * problem_size, :], t3)) + bias[2]
    w3 = (1.0 / sqrt(sum(t3 ** 2))) * exp(-sum(t3 ** 2) / (2 * problem_size * xichma[2] ** 2))

    # 4. Rotated Expanded Scaffer’s F6 Function F16’
    t4 = solution - shift_data[3]
    g4 = lamda[3] * f14_expanded_scaffer__(dot(matrix[3 * problem_size: 4 * problem_size, :], t4)) + bias[3]
    w4 = (1.0 / sqrt(sum(t4 ** 2))) * exp(-sum(t4 ** 2) / (2 * problem_size * xichma[3] ** 2))

    # 5. Rotated High Conditioned Elliptic Function F1’
    t5 = solution - shift_data[4]
    g5 = lamda[4] * f1_elliptic__(dot(matrix[4 * problem_size:, :], t5)) + bias[4]
    w5 = (1.0 / sqrt(sum(t5 ** 2))) * exp(-sum(t5 ** 2) / (2 * problem_size * xichma[4] ** 2))

    sw = sum([w1, w2, w3, w4, w5])
    f_bias = 2800
    result = (w1 * g1 + w2 * g2 + w3 * g3 + w4 * g4 + w5 * g5) / sw #+ f_bias

    return result

# def F29(solution=None, name="Composition Function 7", shift_data_file="shift_data_29.txt", f_bias=2900):
def F29(solution , shift_data, shuffle , matrix, sda_01, ma_01, sda_02, ma_02, sda_03, ma_03):
    num_funcs = 3
    problem_size = len(solution)
    xichma = array([10, 30, 50])
    lamda = array([1, 1, 1])
    bias = array([0, 100, 200])
    '''
    if problem_size > 100:
        print("CEC 2014 not support for problem size > 100")
        return 1
    shift_data = load_matrix_data__(shift_data_file)[:problem_size]
    shift_data = shift_data[:, :problem_size]
    '''
    ##################################################
    ###########                            ###########
    ##################################################

    def F17(solution, sda_01, ma_01, shuffle):  # sda_01 =  shift F17, matrix = matrix F17 , shufle = F29 YOU NEED TO DEFINE SHUFFLE!!!!
        problem_size = len(solution)
        p = array([0.3, 0.3, 0.4])
        n1 = int(ceil(p[0] * problem_size))
        n2 = int(ceil(p[1] * problem_size))

        shift_data = sda_01
        matrix = ma_01
        #shuffle = (shuffle[:problem_size] - ones(problem_size)).astype(int)
        idx1 = shuffle[:n1]
        idx2 = shuffle[n1:(n1+n2)]
        idx3 = shuffle[(n1+n2):]
        mz = dot(solution - shift_data, matrix)
        return f9_modified_schwefel__(mz[idx1]) + f8_rastrigin__(mz[idx2]) + f1_elliptic__(mz[idx3]) # + bias # USING BIAS ZERO!!!

    def F18(solution, sda_02, ma_02, shuffle):  # sda_02 =  shift F18, matrix = matrix F18 , YOU NEED TO DEFINE sda_02 ma_02 too!!!!
        problem_size = len(solution)
        p = array([0.3, 0.3, 0.4])
        n1 = int(ceil(p[0] * problem_size))
        n2 = int(ceil(p[1] * problem_size))

        shift_data = sda_02
        matrix = ma_02
        #shuffle = (shuffle[:problem_size] - ones(problem_size)).astype(int)
        idx1 = shuffle[:n1]
        idx2 = shuffle[n1:(n1 + n2)]
        idx3 = shuffle[(n1 + n2):]
        mz = dot(solution - shift_data, matrix)
        result = f2_bent_cigar__(mz[idx1]) + f12_hgbat__(mz[idx2]) + f8_rastrigin__(mz[idx3]) # + bias # USING BIAS ZERO!!!
        return result

    def F19(solution,sda_03, ma_03, shuffle):
        problem_size = len(solution)
        p = array([0.2, 0.2, 0.3, 0.3])
        n1 = int(ceil(p[0] * problem_size))
        n2 = int(ceil(p[1] * problem_size))
        n3 = int(ceil(p[2] * problem_size))

        shift_data = sda_03
        matrix = ma_03
        #shuffle = (shuffle[:problem_size] - ones(problem_size)).astype(int)
        idx1 = shuffle[:n1]
        idx2 = shuffle[n1:(n1 + n2)]
        idx3 = shuffle[(n1 + n2):(n1+n2+n3)]
        idx4 = shuffle[n1+n2+n3:]
        mz = dot(solution - shift_data, matrix)
        result = f7_griewank__(mz[idx1]) + f6_weierstrass__(mz[idx2]) + f4_rosenbrock__(mz[idx3]) + f14_expanded_scaffer__(mz[idx4])
        return result

    def __fi__(solution=None, idx=None):
        if idx == 0:
            result = F17(solution,sda_01, ma_01,shuffle) # sda_01, ma_01 data from F17
            return result
        elif idx == 1:
            result = F18(solution,sda_02, ma_02, shuffle) # sda_02, ma_02 data from F18
            return result
        else:
            result = F19(solution,sda_03, ma_03, shuffle) # sda_03, ma_03 data from F19
            return result

    ##################################################
    ###########                            ###########
    ##################################################
    weights = ones(num_funcs)
    fits = ones(num_funcs)
    for i in range(0, num_funcs):
        t1 = lamda[i] * __fi__(solution, i) + bias[i]
        t2 = 1.0 / sqrt(sum((solution - shift_data[i]) ** 2))
        w_i = t2 * exp(-sum((solution - shift_data[i]) ** 2) / (2 * problem_size * xichma[i] ** 2))
        weights[i] = w_i
        fits[i] = t1
    sw = sum(weights)
    result = 0.0
    f_bias = 2900
    for i in range(0, num_funcs):
        result += (weights[i] / sw) * fits[i]+ f_bias
    return result

# def F30(solution=None, name="Composition Function 8", shift_data_file="shift_data_30.txt", f_bias=3000):
def F30(solution , shift_data, shuffle , matrix, sda_01, ma_01, sda_02, ma_02, sda_03, ma_03):
    num_funcs = 3
    problem_size = len(solution)
    xichma = array([10, 30, 50])
    lamda = array([1, 1, 1])
    bias = array([0, 100, 200])
    #################################################
    #######                                     #####
    #################################################
    '''
    if problem_size > 100:
        print("CEC 2014 not support for problem size > 100")
        return 1
    shift_data = load_matrix_data__(shift_data_file)[:problem_size]
    shift_data = shift_data[:, :problem_size]
    '''
    #################################################
    #######                                     #####
    #################################################
    def F20(solution, sda_01, ma_01, shuffle):
        problem_size = len(solution)
        p = array([0.2, 0.2, 0.3, 0.3])
        n1 = int(ceil(p[0] * problem_size))
        n2 = int(ceil(p[1] * problem_size))
        n3 = int(ceil(p[2] * problem_size))

        shift_data = sda_01
        matrix = ma_01
        #shuffle = shuffle
        idx1 = shuffle[:n1]
        idx2 = shuffle[n1:(n1 + n2)]
        idx3 = shuffle[(n1 + n2):(n1 + n2 + n3)]
        idx4 = shuffle[n1 + n2 + n3:]
        mz = dot(solution - shift_data, matrix)
        result = f12_hgbat__(mz[idx1]) + f3_discus__(mz[idx2]) + f13_expanded_griewank__(mz[idx3]) + f8_rastrigin__(mz[idx4]) #+ bias
        return result

    def F21(solution, sda_02, ma_02, shuffle):
        problem_size = len(solution)
        p = array([0.1, 0.2, 0.2, 0.2, 0.3])
        n1 = int(ceil(p[0] * problem_size))
        n2 = int(ceil(p[1] * problem_size))
        n3 = int(ceil(p[2] * problem_size))
        n4 = int(ceil(p[3] * problem_size))

        shift_data = sda_02
        matrix = ma_02
        #shuffle = shuffle

        idx1 = shuffle[:n1]
        idx2 = shuffle[n1:(n1 + n2)]
        idx3 = shuffle[(n1 + n2):(n1 + n2 + n3)]
        idx4 = shuffle[(n1+n2+n3):(n1+n2+n3+n4)]
        idx5 = shuffle[n1+n2+n3+n4:]
        mz = dot(solution - shift_data, matrix)
        result = f14_expanded_scaffer__(mz[idx1]) + f12_hgbat__(mz[idx2]) + f4_rosenbrock__(mz[idx3])
        result = result + f9_modified_schwefel__(mz[idx4]) + f1_elliptic__(mz[idx5])
        return  result

    def F22(solution, sda_03, ma_03, shuffle):
        problem_size = len(solution)
        p = array([0.1, 0.2, 0.2, 0.2, 0.3])
        n1 = int(ceil(p[0] * problem_size))
        n2 = int(ceil(p[1] * problem_size))
        n3 = int(ceil(p[2] * problem_size))
        n4 = int(ceil(p[3] * problem_size))

        shift_data = sda_03
        matrix = ma_03
        #shuffle = shuffle

        idx1 = shuffle[:n1]
        idx2 = shuffle[n1:(n1 + n2)]
        idx3 = shuffle[(n1 + n2):(n1 + n2 + n3)]
        idx4 = shuffle[(n1 + n2 + n3):(n1 + n2 + n3 + n4)]
        idx5 = shuffle[n1 + n2 + n3 + n4:]
        mz = dot(solution - shift_data, matrix)
        result = f10_katsuura__(mz[idx1]) + f11_happy_cat__(mz[idx2]) + f13_expanded_griewank__(mz[idx3])
        result = result + f9_modified_schwefel__(mz[idx4]) + f5_ackley__(mz[idx5])

        return  result

    def __fi__(solution=None, idx=None):
        if idx == 0:
            return F20(solution,sda_01, ma_01,shuffle) # sda_01, ma_01 data from F17
            # F20(solution, bias=0, shuffle=30)
        elif idx == 1:
            return F21(solution,sda_02, ma_02,shuffle) # sda_02, ma_02 data from F21
            # return F21(solution, bias=0, shuffle=30)
        else:
            return F22(solution,sda_03, ma_03,shuffle) # sda_03, ma_03 data from F22
            # return F22(solution, bias=0, shuffle=30)

    weights = ones(num_funcs)
    fits = ones(num_funcs)
    for i in range(0, num_funcs):
        t1 = lamda[i] * __fi__(solution, i) + bias[i]
        t2 = 1.0 / sqrt(sum((solution - shift_data[i]) ** 2))
        w_i = t2 * exp(-sum((solution - shift_data[i]) ** 2) / (2 * problem_size * xichma[i] ** 2))
        weights[i] = w_i
        fits[i] = t1
    sw = sum(weights)
    result = 0.0
    for i in range(0, num_funcs):
        result += (weights[i] / sw) * fits[i]
    return result #+ f_bias


class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def double_linked_list(head):
    carry = 0
    dummy = ListNode(0)
    current = dummy

    while head is not None or carry > 0:
        x = head.val if head is not None else 0
        total = x * 2 + carry
        current.next = ListNode(total % 10)
        carry = total // 10

        if head is not None:
            head = head.next
        current = current.next

    if carry > 0:
        current.next = ListNode(carry)

    return dummy.next

# Приклад використання:
# Створимо вхідний список: 9 -> 9 -> 9
head = ListNode(9, ListNode(9, ListNode(9)))

# Викликаємо функцію для подвоєння числа
result_head = double_linked_list(head)

# Виведемо результат
while result_head is not None:
    print(result_head.val, end=" ")
    result_head = result_head.next


import torch
import torch.nn as nn

class Discriminiator(nn.Module):
    def __init__(self, in_filters=128, model_type='wrn'):
        super(Discriminiator, self).__init__()
        if model_type == 'wrn':
            self.out_filters = 64  # WRN[B, 128, 8 ,8]
        elif model_type == 'res':  # # 由于ResNet的layer4=>[Batch_size, 2048, 4, 4]
            self.out_filters = 256

        self.model = nn.Sequential(
            nn.Conv2d(in_filters, self.out_filters, kernel_size=3, padding=0, stride=2),
            nn.BatchNorm2d(self.out_filters),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Conv2d(self.out_filters, 1, kernel_size=1, padding=0, stride=1),
            nn.Sigmoid(),
        )

    def forward(self, img):
        validity = self.model(img)
        return validity

def test():
    discriminator = Discriminiator(in_filters=128)
    y = discriminator(torch.randn(1, 128, 8, 8))
    # y = discriminator(torch.randn(1, 3, 32, 32))
    print(y.size())  # 1, 1, 7, 7

# test()


#Tressure Map

row1 = [1, 2, 3]
row2 = [4, 5, 6]
row3 = [7, 8, 9]

map = [row1, row2, row3]

print(f"{row1}\n{row2}\n{row3}")
position = input("Where do you want to put the tressure?")

one = int((position[0])) - 1
two = int((position[1])) - 1

(map[one][two]) = 'T'

print(map)


import logging

from llpdf.repr import PDFParser, GraphicsParser
from .img.PDFImage import PDFImage
from .types.PDFObject import PDFObject
from .types.PDFName import PDFName
from .types.PDFXRef import PDFXRef
from .types.XRefTable import XRefTable
from .FileRepr import StreamRepr

class PDFDocument(object):
	_log = logging.getLogger("llpdf.PDFDocument")

	def __init__(self):
		self._objs = { }
		self._xref_table = XRefTable()
		self._trailer = { }

	@property
	def objcount(self):
		return len(self._objs)

	@property
	def xref_table(self):
		return self._xref_table

	def _identify(self):
		self._f.seek(0)
		version = self._f.readline()

		pos = self._f.tell()
		after_hdr = self._f.readline()
		if (after_hdr[0] != ord("%")) or any(value & 0x80 != 0x80 for value in after_hdr[ 1 : 5 ]):
			self._log.warning("PDF seems to violate standard, bytes read in second line are %s.", after_hdr)
			self._f.seek(pos)
		return version.rstrip(b"\r\n ")

	@property
	def trailer(self):
		return self._trailer

	@trailer.setter
	def trailer(self, value):
		self._trailer = value

	@property
	def image_objects(self):
		return [ obj for obj in self._objs.values() if obj.is_image ]

	@property
	def pattern_objects(self):
		return [ obj for obj in self._objs.values() if obj.is_pattern ]

	@property
	def objstrm_objects(self):
		return [ obj for obj in self._objs.values() if obj.is_objstrm ]

	@property
	def stream_objects(self):
		return [ obj for obj in self._objs.values() if obj.has_stream ]

	def get_objects_that_reference(self, xref):
		for obj in self.pattern_objects:
			resources = obj.content.get(PDFName("/Resources"))
			xobjects = resources.get(PDFName("/XObject"))
			xrefs = set(xobjects.values())
			if xref in xrefs:
				yield obj

	def get_extent_of_image(self, img_object):
		print("Determining extent of %s" % (img_object))
		for obj in self.get_objects_that_reference(img_object.xref):
			if obj.is_pattern:
				print("Referenced by pattern %s" % (obj), obj.content)
				bbox = obj.content.get(PDFName("/BBox"))
				(width, height) = (bbox[2] - bbox[0], bbox[3] - bbox[1])
				return (width, height)
			else:
				print("Cannot determine phyiscal extents of image, scaling probably done in page code :-(")

	def _get_pages_from_pages_obj(self, pages_obj):
		pagecontent_xrefs = pages_obj.content[PDFName("/Kids")]
		for page_xref in pagecontent_xrefs:
			page = self.lookup(page_xref)
			if page.content[PDFName("/Type")] == PDFName("/Page"):
				yield page
			elif page.content[PDFName("/Type")] == PDFName("/Pages"):
				yield from self._get_pages_from_pages_obj(page)
			else:
				raise Exception("Page object %s contains neither page nor pages (/Type = %s)." % (pages_obj, page.content[PDFName("/Type")]))

	@property
	def pages_object(self):
		if self._trailer is None:
			self._log.error("Cannot access page data without trailer; returning empty page set.")
			return [ ]
		root_xref = self._trailer.get(PDFName("/Root"))
		if root_xref is None:
			self._log.error("Cannot access page data without /Root node in trailer; returning empty page set.")
			return [ ]
		root_obj = self.lookup(root_xref)
		if root_obj is None:
			self._log.error("Cannot access page data without /Root node (failed to lookup %s); returning empty page set.", root_xref)
			return [ ]
		pages_obj = self.lookup(root_obj.content[PDFName("/Pages")])
		return pages_obj

	@property
	def pages(self):
		yield from self._get_pages_from_pages_obj(self.pages_object)

	@property
	def parsed_pages(self):
		for page in self.pages:
			content_xref = page.content[PDFName("/Contents")]
			content = self.lookup(content_xref)
			pagedata = content.stream.decode()
			pagedata = pagedata.decode("latin1")
			yield (page, GraphicsParser.parse(pagedata))

	def __getitem__(self, key):
		(objid, gennum) = key
		return self._objs.get((objid, gennum))

	def lookup(self, xref):
		return self[(xref.objid, xref.gennum)]

	def __iter__(self):
		return iter(self._objs.values())

	def _read_objects(self):
		self._log.debug("Started reading objects at 0x%x.", self._f.tell())
		objcnt = 0
		while True:
			obj = PDFObject.parse(self._f)
			if obj is None:
				break
			objcnt += 1
			self._log.debug("Read object: %s", obj)
			self._objs[(obj.objid, obj.gennum)] = obj
		self._log.debug("Finished reading %d objects at 0x%x.", objcnt, self._f.tell())
		return objcnt

	def _fix_object_sizes(self):
		self._log.debug("Fixing object sizes of indirect referenced /Length fields")
		for obj in self.stream_objects:
			length_xref = obj.content.get(PDFName("/Length"))
			if (length_xref is not None) and isinstance(length_xref, PDFXRef):
				length_obj = self.lookup(length_xref)
				length = length_obj.content
				if not isinstance(length, int):
					self._log.warning("Indirect length reference supposed to point to integer value, but points to %s (%s)", length_obj, length)
				else:
					if length != len(obj):
						obj.truncate(length)

	def _read_textline(self):
		line = self._f.readline_nonempty().decode("ascii").rstrip("\r\n")
		return line

	def _read_trailer(self):
		self._log.debug("Started reading trailer at 0x%x.", self._f.tell())
		trailer_data = self._f.read_until_token(b"startxref", rewind = True)
		trailer_data = trailer_data.decode("latin1")
		self._trailer = PDFParser.parse(trailer_data)

	def _read_endfile(self):
		self._log.debug("Reading end-of-file data at 0x%x.", self._f.tell())
		while True:
			line = self._read_textline().strip("\r\n ")
			if line == "":
				continue
			elif line == "xref":
				self._xref_table.read_xref_table_from_file(self._f)
			elif line == "trailer":
				self._read_trailer()
			elif line == "startxref":
				xref_offset = int(self._f.readline())
				if self._trailer is None:
					# Compressed XRef directory
					with self._f.tempseek(xref_offset) as marker:
						self._log.trace("Will parse XRef stream at offset 0x%x referenced from 0x%x." % (xref_offset, marker.prev_offset))
						xref_object = PDFObject.parse(self._f)
						if xref_object is None:
							self._log.error("Could not parse a valid type /XRef object at 0x%x. Corrupt PDF?", xref_offset)
						else:
							self._trailer = xref_object.content
							assert(self._trailer[PDFName("/Type")] == PDFName("/XRef"))
							self._xref_table.parse_xref_object(xref_object.stream.decode(), self._trailer.get(PDFName("/Index")), self._trailer[PDFName("/W")])
			elif line == "%%EOF":
				self._log.debug("Hit EOF marker at 0x%x.", self._f.tell())
				break
			else:
				raise Exception("Unknown end file token '%s' at offset 0x%x." % (line, self._f.tell()))

	def _read_pdf(self):
		generation = 0
		while True:
			generation += 1
			self._log.debug("Trying to read generation %d data at offset 0x%x", generation, self._f.tell())
			objcnt = self._read_objects()
			if objcnt == 0:
				self._log.debug("No more data to read at 0x%x.", self._f.tell())
				break
			self._read_endfile()

	def _unpack_objstrm(self, objstrm_obj):
		data = objstrm_obj.stream.decode()
		objcnt = objstrm_obj.content[PDFName("/N")]
		first = objstrm_obj.content[PDFName("/First")]
		self._log.debug("Object stream %s contains %d objects starting at offset %d.", objstrm_obj, objcnt, first)

		header = data[:first]
		data = data[first:]
		header = [ int(value) for value in header.decode("ascii").replace("\n", " ").split() ]
		for idx in range(0, len(header), 2):
			(objid, sub_offset) = (header[idx], header[idx + 1])
			if idx + 3 >= len(header):
				# Last object
				sub_obj_data = data[sub_offset : ]
			else:
				next_sub_offset = header[idx + 3]
				sub_obj_data = data[sub_offset : next_sub_offset]
			sub_obj = PDFObject(objid, 0, sub_obj_data)
			self.replace_object(sub_obj)
		self.delete_object(objstrm_obj.objid, objstrm_obj.gennum)

	def _unpack_objstrms(self):
		for obj in self.objstrm_objects:
			self._unpack_objstrm(obj)

	def read_stream(self):
		self._f.read_until([ b"stream\r\n", b"stream\n" ])
		(data, terminal) = self._f.read_until([ b"endstream\r\n", b"endstream\n" ])
		return data

	def get_image(self, img_xref):
		image = self.lookup(img_xref)
		if PDFName("/SMask") in image.content:
			# image has an alpha channel
			alpha_channel = self.lookup(image.content[PDFName("/SMask")])
		else:
			alpha_channel = None
		image = PDFImage.create_from_object(image, alpha_channel)
		self._log.debug("Created image from %s with alpha %s: %s",img_xref, alpha_channel, image)
		return image

	def get_info(self, key):
		info_node_xref = self.trailer[PDFName("/Info")]
		info_node = self.lookup(info_node_xref)
		key = PDFName("/" + key)
		if (info_node is None) or (key not in info_node.content):
			return ""
		else:
			bin_value = info_node.content[key]
			self._log.debug("Info directionary %s = %s", key, bin_value)
			if bin_value.startswith(b"\xfe\xff") or bin_value.startswith(b"\xff\xfe"):
				return bin_value.decode("utf-16")
			else:
				return bin_value.decode("latin1")

	def set_info(self, key, value):
		info_node_xref = self.trailer[PDFName("/Info")]
		info_node = self.lookup(info_node_xref)
		if info_node is not None:
			info_node.content[PDFName("/" + key)] = value.encode("latin1")
		else:
			self._log.error("Cannot set /Info dictionary entry \"%s\" to \"%s\": info_node is None", key, value)

	def get_free_objids(self, count = 1):
		assert(count >= 1)
		for objid in range(1, len(self._objs) + count + 1):
			if (objid, 0) not in self._objs:
				yield objid

	def add(self, obj):
		self._objs[(obj.objid, obj.gennum)] = obj
		return self

	def new_object(self, content = None, stream = None):
		if content is None:
			content = { }
		objid = self.get_free_objid()
		obj = PDFObject.create(objid = objid, gennum = 0, content = content, stream = stream)
		self.add(obj)
		return obj

	def get_free_objid(self):
		return list(self.get_free_objids())[0]

	def delete_object(self, objid, gennum):
		key = (objid, gennum)
		if key in self._objs:
			del self._objs[key]

	def replace_object(self, obj):
		self._objs[(obj.objid, obj.gennum)] = obj
		return self

	def _fix_object_sizes(self):
		self._log.debug("Fixing object sizes of indirect referenced /Length fields")
		for obj in self.stream_objects:
			length_xref = obj.content.get(PDFName("/Length"))
			if (length_xref is not None) and isinstance(length_xref, PDFXRef):
				length_obj = self.lookup(length_xref)
				length = length_obj.content
				if not isinstance(length, int):
					self._log.warning("Indirect length reference supposed to point to integer value, but points to %s (%s)", length_obj, length)
				else:
					if length != len(obj):
						obj.truncate(length)

	def _unpack_objstrm(self, objstrm_obj):
		data = objstrm_obj.stream.decode()
		objcnt = objstrm_obj.content[PDFName("/N")]
		first = objstrm_obj.content[PDFName("/First")]
		self._log.debug("Object stream %s contains %d objects starting at offset %d.", objstrm_obj, objcnt, first)

		header = data[:first]
		data = data[first:]
		header = [ int(value) for value in header.decode("ascii").replace("\n", " ").split() ]
		for idx in range(0, len(header), 2):
			(objid, sub_offset) = (header[idx], header[idx + 1])
			if idx + 3 >= len(header):
				# Last object
				sub_obj_data = data[sub_offset : ]
			else:
				next_sub_offset = header[idx + 3]
				sub_obj_data = data[sub_offset : next_sub_offset]
			sub_obj = PDFObject(objid, 0, sub_obj_data)
			self.replace_object(sub_obj)
		self.delete_object(objstrm_obj.objid, objstrm_obj.gennum)

	def unpack_objstrms(self):
		for obj in self.objstrm_objects:
			self._unpack_objstrm(obj)
		self._fix_object_sizes()


#超长发挥
formula = input()
#转换为列表找到两个符号的索引并记录
formulaList = list(formula)
for i in range(len(formulaList)):
    if formulaList[i] == '+' or formulaList[i] == '-' or formulaList[i] == '*' or formulaList[i] == '/':
        formulaList[i] = '#'
#得出xyz
L = "".join(formulaList).split('#')
##x, y, z = map(L)
x = int(L[0])
y = int(L[1])
z = int(L[2])

#两个符号的索引
firt = formulaList.index('#')
formulaList[firt] = '&'
second = formulaList.index('#')
formulaList[firt] = '#'
formulaList = "".join(formulaList).split("#")
#运算函数
def calculate(a, b, sign):
    if sign == '*':
        return a*b
    if sign == '/':
        return a/b
    if sign == '+':
        return a+b
    if sign == '-':
        return a-b
if formula[firt] == '*'or formula[firt] == '/':
    print(formula, "=", calculate(calculate(x, y, formula[firt]), z, formula[second]), sep="")
else:
    print(formula, "=", calculate(calculate(y, z, formula[second]), x, formula[firt]), sep="")
"""
1+2*3
"""


#!/usr/bin/python3

import serial
import struct
import argparse
import time

parser = argparse.ArgumentParser()
parser.add_argument("port")
args = parser.parse_args()

s = serial.Serial(args.port, 9600)
while True:
    [report_count, touch_count] = struct.unpack('<BB', s.read(2))
    data = ','.join([str(x) for x in (int(time.time()), report_count, touch_count)])

    print(data)
    with open(args.port.replace('/','-'),'a') as f:
        f.write(data)
        f.write('\n')


from Eurecom import *
from tdlbp import *
import argparse
from helper.functions import sendEmailMessage, standartParametrization

if __name__ == '__main__':

    parser = argparse.ArgumentParser(description='Process and extract FRGC database')
    parser = standartParametrization(parser)
    args = parser.parse_args()

    if not os.path.exists(args.pathImages):
        os.makedirs(args.pathImages)

    faceDataset = []
    sets = ['s1','s2']
    for s in sets:
        ek = EurecomKinect(args.pathdatabase,s,args.typeoffile,args.faceVariation.split('_'))
        ek.feedTemplates()

        if args.loadNewDepth:
            ek.loadNewDepthImage()
        elif args.loadSymmImages:
            ek.loadSymmFilledImages()

        if args.angles:
            ek.loadRotatedFaces(args.angles.split('_'),args.axis.split('_'))

        faceDataset.append(ek)

    tdlbp = ThreeDLBP(8,14,faceDataset,generateImages=args.generateImages)
    tdlbp.fullPathGallFile = args.pathImages

    if not args.steps is None:
        ppSteps = args.steps.split('_')
        for p in ppSteps:
            className = None
            parameters = None
            kwargsList = None
            if ':' in p:
                parameters = p.split(':')
                className = parameters[0]
                parameters = parameters[1].split(',')
                kwargsList = {}
                for pr in parameters:
                    lParameters = pr.split('=')
                    kwargsList[lParameters[0]] = eval(lParameters[1])

            else:
                className = p

            module = __import__(className)
            class_ = getattr(module,className)
            if kwargsList is None:
                tdlbp.preProcessingSteps = class_()
            else:
                tdlbp.preProcessingSteps = class_(**kwargsList)

    if args.operation in ['both','pp']:
        tdlbp.preProcessing(True,args.parcal)
        sendEmailMessage('Fim do pre-processamento', 'Terminou o pre-processamento EURECOM e LBP')
        #xek.saveTemplateImage()

    if args.operation in ['both', 'fe']:
        tdlbp.featureExtraction(args.points,args.radius,args.parcal,layersUtilize=[1,2,3,4],typeMeasurement=args.typeMeasure,procs=args.quantityProcesses,masks=args.generateMasks,forceImage=args.force,deformValue=args.deformValue)
        sendEmailMessage('Fim dos experimentos','Terminou a extração de características EURECOM e LBP')

    if (args.pathtrainingfile is not None) and (args.operation == 'fe'):
        faceVariationGenerate = { k : ['s1','s2'] for k in args.faceVariation.split('_') }
        galeryData = faceDataset[0].generateDatabaseFile(args.pathtrainingfile,faceVariationGenerate,[faceDataset[1]],'SVMTorchFormat')


from matplotlib import colors
import matplotlib.pyplot as plt
import pandas as pd

array = pd.read_csv("DS9.txt", delimiter=' ',names=['x', 'y'])
graphic = plt.figure(figsize=[5.4, 9.6])
ax = graphic.add_subplot(111)
ax.set_title("Data Set 9")
ax.set_ylabel("Second column")
ax.set_xlabel("First column")
ax.scatter(array.y, array.x, c=["#DD9787"], s=1)
graphic.savefig("DS9 picture.png")
plt.show()


# Properties of Directed Graphcs

test_graph_1 = {'A': ['A','B'],
                'B': ['B','C'],
                'C': ['C','A']}

test_graph_2 = {'E': ['A','B','C'],
                'A': ['C', 'B'],
                'B': ['C'],
                'C': [],
                'D': ['C']}

test_graph_3 = {'A':['E','B'],
                'B':['A','C'],
                'C':['B'],
                'D':[],
                'E':['A']}

def has_connection(graph, src, dst):
    return dst in graph[src]

def out_degree(graph,vertex):
    return len(graph[vertex])

def in_degree(graph,vertex):
    d = 0
    for v in graph:
        if vertex in v:
            d += 1
    return d

def is_reflexive(graph):
    for vertex,edges in graph.items():
        if vertex not in edges:
            return False
    return True

def is_antireflexive(graph):
    return not is_reflexive(graph)

def is_transitive(graph):
    for vertex_1,edges_1 in graph.items():
        for vertex_2 in edges_1:
            for vertex_3 in graph[vertex_2]:
                if not has_connection(graph, vertex_1, vertex_3):
                    return False
    return True

def is_symmetric(graph):
    for vertex, edges, in graph.items():
        for arrow in edges:
            if not has_connection(graph,arrow,vertex):
                return False
    return True

def is_antisymmetric(graph):
    return not is_symmetric(graph)

def walk(graph, start, end, path=[]):
    path = path + [start]
    if start == end:
        return path
    if not start in graph:
        return None
    for vertex in graph[start]:
        if vertex not in path:
            newpath = walk(graph, vertex, end, path)
            if newpath: return newpath

print("Testing Relexivity")
print(is_reflexive(test_graph_1))

print("Testing Transitivity")
print(is_transitive(test_graph_2))

print("Testing Symmetricity")
print(is_symmetric(test_graph_3))

print("Testing Walk")
print(walk(test_graph_1,'A','C'))


from PortfolioService import *
from Utilities import *

stocks = list()
stock_cache = dict()

def _print_portfolio():
    print('Portfolio resume')
    print('*' * 50)
    for stock in stocks:
        print(str(stock))
    print('\n')

def _get_or_add_financial_values_to_cache(ticker):
    try:
        financial_in_cache = stock_cache[ticker]
        if financial_in_cache:
            print('Found ticker {0} in cache with beta: {1}'.format(ticker.upper(), financial_in_cache['beta']))
            return financial_in_cache
    except KeyError:
        pass
    try:
        financial_values = PortfolioService.get_financial_values_by_ticker(ticker.upper())
        stock_cache[ticker] = financial_values
        print('Found ticker {0} in Yahoo Finance with beta: {1}'.format(ticker.upper(), financial_values['beta']))
        return financial_values
    except TypeError:
        print('Ticker does not exist')
        return None

def _create_portfolio_with_number_of_actions():
    print('Please enter the portfolio data')
    while True:
        try:
            n = int(input('How many stocks do you want to enter? '))
            i = 1
            while n > 0:
                print('Values for Stock {0}'.format(i))
                ticker = Utilities.create_ticker(stocks)
                financial_values = _get_or_add_financial_values_to_cache(ticker)
                if financial_values:
                    beta = financial_values['beta']
                    number_of_actions = Utilities.create_number_of_actions()
                    stock = PortfolioService.create_stock(ticker, 0, number_of_actions,
                                                          beta, financial_values['current_price'])
                    stocks.append(stock)
                    n -= 1
                    i += 1
            break
        except ValueError:
            print('Please enter a number')
    _print_portfolio()

def _create_portfolio_with_percentage():
    print('Please enter the portfolio data')
    while True:
        try:
            n = int(input('How many stocks do you want to enter? '))
            total_iterations = n
            i = 1
            total_percentage = 0
            while n > 0:
                print('Values for Stock {0}'.format(i))
                ticker = Utilities.create_ticker(stocks)
                financial_values = _get_or_add_financial_values_to_cache(ticker)
                if financial_values:
                    while True:
                        percentage = Utilities.create_percentage()
                        total_percentage += percentage
                        if total_percentage > 100:
                            total_percentage -= percentage
                            print('Sum of percentages exceeded')
                        elif i == total_iterations and total_percentage < 100:
                            total_percentage -= percentage
                            print('Sum of percentages must be 100%')
                        else:
                            stock = PortfolioService.create_stock(ticker, percentage, 0, financial_values['beta'],
                                                                  financial_values['current_price'])
                            stocks.append(stock)
                            n -= 1
                            i += 1
                            break
            break
        except ValueError:
            print('Please enter a number')
    _print_portfolio()

def _menu_options():
    stocks.clear()
    print('\n')
    print('*' * 50)
    print('Portfolio Management')
    print('*' * 50)
    print('Enter option to calculate')
    print('[B] - Beta Portfolio')
    print('[S] - Standard Deviation and Sharpe Ratio')
    print('[E] - Exit')

if __name__ == '__main__':
    _menu_options()

    command = 'A'
    while True:
        command = input()
        command = command.upper()

        if command == 'B':
            _create_portfolio_with_percentage()
            portfolio_beta = PortfolioService.calculate_portfolio_beta(stocks)
            print('Beta Portfolio: {0}'.format(portfolio_beta))
        elif command == 'S':
            _create_portfolio_with_number_of_actions()
            deviation_tuple = PortfolioService.calculate_annual_standard_deviation(stocks)
            print('\n')
            print('Standard deviation: {0}%'.format(round(deviation_tuple[0], 2)))
            print('Annualized Standard deviation: {0}%'.format(round(deviation_tuple[1], 2)))
            print('\n')
            sharpe_ratio = PortfolioService.calculate_sharpe_ratio(deviation_tuple)
            print('Sharpe ratio: {0}%'.format(sharpe_ratio))
        elif command == 'E':
            break
        else:
            print('Invalid command')
        _menu_options()


def encrypt_caesar(plaintext, shift):
    alpha = 'abcdefghijklmnopqrstuvwxyz'
    alpha_big = alpha.upper()
    ciphertext = ''
    for i in plaintext:
        if i in alpha:
            ciphertext += alpha[(alpha.index(i) + shift) % len(alpha)]
        elif i in alpha_big:
            ciphertext += alpha_big[(alpha_big.index(i) + shift) % len(alpha_big)]
        else:
            ciphertext += i
    return ciphertext

def decrypt_caesar(ciphertext, shift):
    alpha = 'abcdefghijklmnopqrstuvwxyz'
    alpha_big = alpha.upper()
    plaintext = ''
    for i in ciphertext:
        if i in alpha:
            plaintext += alpha[(alpha.index(i) - shift) % len(alpha)]
        elif i in alpha_big:
            plaintext += alpha_big[(alpha_big.index(i) - shift) % len(alpha_big)]
        else:
            plaintext += i
    return plaintext

print(encrypt_caesar('Python3.6', 3))
print(decrypt_caesar('Sbwkrq3.6', 3))


from django.urls import path
from . import views
from .views import Blogdetail, Bloghome
app_name='index'
urlpatterns = [
    # path('login/', views.login, name='login'),
    # path('register/', views.register, name='register'),
    path('', views.home, name='home'),
    path('cart/', views.cart, name='cart'),
    path('checkout/', views.checkout, name='checkout'),
    path('updateitem/', views.updateitem, name='updateitem'),
    path('s_and_t/', views.s_and_tlist, name='s_and_t'),
    path('blog/', views.blogger, name='blogger'),
    path('bloghome/', Bloghome.as_view(), name='bloghome'),
    path('article/<int:pk>', Blogdetail.as_view(), name='blogdetail'),
    path('services/', views.services, name='services'),
    path('tools/', views.tools, name='tools')
]


import ast

import flake8
import pytest

import flake8_secure_coding_standard as flake8_scs

def create_options_manager():
    ctor_args = {'version': '1.0', 'plugin_versions': '', 'parents': []}
    if int(flake8.__version__[0]) >= 6:
        ctor_args['formatter_names'] = []
    return flake8.options.manager.OptionManager(**ctor_args)

def results(s):
    return {'{}:{}: {}'.format(*r) for r in flake8_scs.Plugin(ast.parse(s)).run()}

# ==============================================================================

_default_modes = list(range(0, flake8_scs._DEFAULT_MAX_MODE + 1))

def _id_func(arg):
    _max_len = 4
    if arg == _default_modes:
        return 'default_modes'
    if isinstance(arg, list) and len(arg) > _max_len:
        return '[{}...{}]'.format(
            ','.join(str(val) for val in arg[:_max_len]),
            ','.join(str(val) for val in arg[-_max_len:]),
        )
    return str(arg)

# ==============================================================================

@pytest.mark.parametrize(
    ('arg', 'expected'),
    [
        ('0', 0),
        ('false', None),
        ('False', None),
        ('n', None),
        ('no', None),
        ('No', None),
        ('NO', None),
        ('y', _default_modes),
        ('yes', _default_modes),
        ('Yes', _default_modes),
        ('YES', _default_modes),
        ('true', _default_modes),
        ('True', _default_modes),
        ('1', 1),
        ('493', 493),
        ('0o755', 0o755),
        ('0o755,', [0o755]),
        ('0o644, 0o755,', [0o644, 0o755]),
    ],
    ids=_id_func,
)
def test_read_octal_mode_option(arg, expected):
    print(f'INFO: expected: {expected}')
    assert flake8_scs._read_octal_mode_option('test', arg, _default_modes) == expected

@pytest.mark.parametrize('arg', ['', ',', ',,', 'nope', 'asd', 'a,', '493, a'])
def test_read_octal_mode_option_invalid(arg):
    with pytest.raises(ValueError, match='^(Invalid value for|Calculated empty value for|Unable to convert).*'):
        flake8_scs._read_octal_mode_option('test', arg, _default_modes)

@pytest.mark.parametrize('function', ['open', 'mkdir', 'mkfifo', 'mknod'])
@pytest.mark.parametrize(
    ('arg', 'allowed_modes'),
    [
        ('n', []),
        ('y', _default_modes),
        ('0', []),
        ('0o755', _default_modes),
        ('0o644, 0o755,', [0o644, 0o755]),
    ],
    ids=_id_func,
)
def test_os_allowed_mode(function, arg, allowed_modes):
    options = create_options_manager()
    flake8_scs.Plugin.add_options(options)
    flake8_scs.Plugin.parse_options(options.parse_args([f'--os-{function}-mode={arg}']))
    assert getattr(flake8_scs.Visitor, f'os_{function}_modes_allowed') == allowed_modes


"""Tests regarding datetime for spynl.main."""

from datetime import datetime
from json import loads
import pytz
import pytest

from spynl.main.dateutils import now, localize_date, date_to_str

def test_now():
    """Test now function, assuming the system defaults to UTC."""
    utc_now = datetime.utcnow()
    system_dt = now()
    assert system_dt.tzinfo.zone == 'UTC'
    assert utc_now.strftime('%Y-%m-%d %H:%M') == system_dt.strftime('%Y-%m-%d %H:%M')

def test_localize_date_to_str():
    """Test localize_date function."""
    dt = datetime(2014, 10, 3, 4, 15, 00)
    dt = localize_date(dt, tz='UTC')
    # this should be default in dateutils module
    assert date_to_str(dt) == '2014-10-03T04:15:00+0000'
    with pytest.raises(pytz.UnknownTimeZoneError):
        localize_date(dt, False, 'Europ/Amsterdam')
    l_dt = localize_date(dt, tz='Europe/Amsterdam')
    # 2 hours difference due to daylight savings time!
    assert date_to_str(l_dt) == '2014-10-03T06:15:00+0200'

def test_localize_date_with_now():
    """Change tz of result of now with localize_date."""
    system_dt = now()
    local_dt_utc = localize_date(system_dt, tz='UTC')
    assert system_dt.hour == local_dt_utc.hour
    local_dt_ams = localize_date(system_dt, tz='Europe/Amsterdam')
    assert system_dt.hour != local_dt_ams.hour
    assert local_dt_ams.tzinfo.zone == 'Europe/Amsterdam'

def test_get_time(app):
    """Test get time."""
    response = loads(app.get('/time').text)
    dt = datetime.now(tz=pytz.UTC).strftime('%Y-%m-%dT%H:%M')
    server_time = response['server_time']
    assert dt in server_time