#!/usr/bin/python3
from _basic_functions import *

def define_experiment(exp_dict, args):
    set_dir_compile ( exp_dict, os.getcwd() + '/../../microbench' )   ## working directory where the compilation command should be executed
    set_dir_tools   ( exp_dict, os.getcwd() + '/../../tools' )        ## directory where the prerequisite tools library is located
    set_dir_run     ( exp_dict, os.getcwd() + '/bin' )                ## working directory where your program should be executed
    set_dir_data    ( exp_dict, os.getcwd() + '/data' )               ## directory for data files produced by individual runs of your program
    ## note: the above can be accessed using functions get_dir_[...]

    ##
    ## add parameters that you want your experiments to be run with.
    ## your program will be run once for each set of values in the CROSS PRODUCT of all parameters.
    ## (i.e., we will run your program with every combination of parameters)
    ##
    ## if you want to perform repeated trials of each experimental configuration, add a run_param called "__trial"
    ##     and specify a list of trial numbers (as below).
    ##
    ## (the run_param doesn't *need* to be called __trials exactly, but if it is called __trials exactly,
    ##     then extra sanity checks will be performed to verify, for example, that each data point in a graphical plot
    ##     represents the average of precisely as many experimental runs as there are entries in the __trials list.)
    ##

    add_run_param     ( exp_dict, 'INS_DEL_FRAC'    , ['0.0 0.0', '0.5 0.5', '20.0 10.0'] )
    add_run_param     ( exp_dict, 'MAXKEY'          , [2000000, 20000000] )
    add_run_param     ( exp_dict, 'DS_TYPENAME'     , ['brown_ext_ist_lf', 'brown_ext_abtree_lf', 'bronson_pext_bst_occ'] )
    add_run_param     ( exp_dict, 'thread_pinning'  , ['-pin ' + shell_to_str('cd ' + get_dir_tools(exp_dict) + ' ; ./get_pinning_cluster.sh', exit_on_error=True)] )
    add_run_param     ( exp_dict, '__trials'        , [1, 2, 3] )
    add_run_param     ( exp_dict, 'TOTAL_THREADS'   , [1] + shell_to_listi('cd ' + get_dir_tools(exp_dict) + ' ; ./get_thread_counts_numa_nodes.sh', exit_on_error=True) )

    ## i like to have a testing mode (enabled with argument --testing) that runs for less time,
    ##  with fewer parameters (to make sure nothing will blow up before i run for 12 hours...)
    if args.testing:
        add_run_param ( exp_dict, '__trials'        , [1] )
        add_run_param ( exp_dict, 'TOTAL_THREADS'   , [1] + shell_to_listi('cd ' + get_dir_tools(exp_dict) + ' ; ./get_thread_counts_max.sh', exit_on_error=True) )

    ##
    ## specify how to compile and run your program.
    ##
    ## you can use any of the parameters you defined above to dynamically replace {_tokens_like_this}.
    ## you can also get the directories saved above by using:
    ##      {__dir_compile}
    ##      {__dir_tools}
    ##      {__dir_run}
    ##      {__dir_data}
    ##
    ## the following replacement token is also defined for you:
    ##      {__step}            the number of runs done so far, padded to six digits with leading zeros
    ##
    ## your compile command will be executed in the compile directory above.
    ## your run command will be executed in the run directory above.
    ##

    set_cmd_compile     ( exp_dict, 'make bin_dir={__dir_run} -j' )

    if args.testing:
        set_cmd_run     ( exp_dict, 'LD_PRELOAD=../../../lib/libjemalloc.so timeout 300 numactl --interleave=all time ./{DS_TYPENAME}.debra -nwork {TOTAL_THREADS} -insdel {INS_DEL_FRAC} -k {MAXKEY} -t 100 {thread_pinning} -rq 0 -rqsize 1 -nrq 0' )
    else:
        set_cmd_run     ( exp_dict, 'LD_PRELOAD=../../../lib/libjemalloc.so timeout 300 numactl --interleave=all time ./{DS_TYPENAME}.debra -nwork {TOTAL_THREADS} -nprefill {TOTAL_THREADS} -insdel {INS_DEL_FRAC} -k {MAXKEY} -t 10000 {thread_pinning} -rq 0 -rqsize 1 -nrq 0' )

    ## pattern for output filenames. note 1: these files will be placed in {__dir_data}/. note 2: filenames cannot contain spaces.
    set_file_data   ( exp_dict, 'data{__step}.txt' )

    ##
    ## add data fields to be fetched from all output files.
    ## each of these becomes a column in a data table in a sqlite database.
    ##
    ## by default, a field "XYZ" will be fetched from each data file using extractor grep_line,
    ##      which greps (searches) for a line of the form "XYZ=[arbitrary string]"
    ##
    ## if your field is not stored in that format, you can specify a custom "extractor" function,
    ##      as we do in our example "get_maxres" BELOW, to extract the max resident size
    ##      from the 6th space-separated column of the output of the linux "time" command
    ##
    ## also note: each of these fields becomes a replacement token, e.g., {PAPI_L3_TCM}.
    ##
    ## the following special fields are defined for you and automatically added to the resulting sqlite data table:
    ##      {__step}            the number of runs done so far, padded to six digits with leading zeros
    ##      {__cmd_run}         your cmd_run string with any tokens replaced appropriately for this run
    ##      {__file_data}       the output filename for the current run's data
    ##      {__path_data}       the relative path to the output file for the current run's data
    ##      {__hostname}        the result of running the hostname command on the machine
    ##

    ## note: in the following, defaults are "validator=is_nonempty" and "extractor=grep_line"

    add_data_field ( exp_dict, 'total_throughput'  , coltype='INTEGER' , validator=is_positive )
    add_data_field ( exp_dict, 'PAPI_L3_TCM'       , coltype='REAL' )
    add_data_field ( exp_dict, 'PAPI_L2_TCM'       , coltype='REAL' )
    add_data_field ( exp_dict, 'PAPI_TOT_CYC'      , coltype='REAL' )
    add_data_field ( exp_dict, 'PAPI_TOT_INS'      , coltype='REAL' )
    add_data_field ( exp_dict, 'maxresident_mb'    , coltype='REAL'    , validator=is_positive , extractor=get_maxres ) ## note the custom extractor
    add_data_field ( exp_dict, 'tree_stats_height' , coltype='INTEGER' )
    add_data_field ( exp_dict, 'validate_result'   , coltype='TEXT'    , validator=is_equal('success') )
    add_data_field ( exp_dict, 'MILLIS_TO_RUN'     , coltype='TEXT'    , validator=is_positive )
    add_data_field ( exp_dict, 'RECLAIM'           , coltype='TEXT' )
    add_data_field ( exp_dict, 'POOL'              , coltype='TEXT' )

    ##
    ## add_plot_set() will cause a SET of plots to be rendered as images in the data directory.
    ##
    ## the precise SET of plots is defined by the fields included in varying_cols_list[].
    ##  we will iterate over all distinct combinations of values in varying_cols_list,
    ##  and will render a plot for each.
    ##
    ## note: a plot's title and filename can only use replacement {tokens} that correspond
    ##       to fields THAT ARE INCLUDED in varying_cols_list[]
    ##       (this is because only those tokens are well defined and unique PER PLOT)
    ##

    ## can do direct passthrough of command line args to the plotting script (see, e.g.,. ../../tools/plotbars.py to see what kind of customization of plots can be done this way)
    plot_cmd_args = '--legend-include --legend-columns 3'

    ## you can also specify a python file containing hooks for configuring matplotlib's plotting style:
    ##    just after importing, and just before and after plotting.
    ## (your callback/hook functions will be provided with the mpl, plt, fig and ax variables
    ##    for matplotlib's environment, plot, figure and axes, so you can customize as you like.)

    set_plot_style_hooks_file( exp_dict, os.getcwd() + '/' + '_user_plot_style.py' )
    ## if you prefer, you can set this PER-PLOT SET instead of globally, by providing argument plot_style_hooks_file below

    add_plot_set(
            exp_dict
          , name='throughput-u{INS_DEL_FRAC}-k{MAXKEY}.png'
          , title='u={INS_DEL_FRAC} k={MAXKEY}: Throughput vs thread count'
          , varying_cols_list=['INS_DEL_FRAC', 'MAXKEY']
          , series='DS_TYPENAME'
          , x_axis='TOTAL_THREADS'
          , y_axis='total_throughput'
          , plot_type='bars'
          , plot_cmd_args=plot_cmd_args
    )
    add_plot_set(
            exp_dict
          , name='l2miss-u{INS_DEL_FRAC}-k{MAXKEY}.png'
          , title='u={INS_DEL_FRAC} k={MAXKEY}: L2 misses/op vs thread count'
          , varying_cols_list=['INS_DEL_FRAC', 'MAXKEY']
          , series='DS_TYPENAME'
          , x_axis='TOTAL_THREADS'
          , y_axis='PAPI_L2_TCM'
          , plot_type='bars'
          , plot_cmd_args=plot_cmd_args
    )
    add_plot_set(
            exp_dict
          , name='l3miss-u{INS_DEL_FRAC}-k{MAXKEY}.png'
          , title='u={INS_DEL_FRAC} k={MAXKEY}: L3 misses/op vs thread count'
          , varying_cols_list=['INS_DEL_FRAC', 'MAXKEY']
          , series='DS_TYPENAME'
          , x_axis='TOTAL_THREADS'
          , y_axis='PAPI_L3_TCM'
          , plot_type='bars'
          , plot_cmd_args=plot_cmd_args
    )
    add_plot_set(
            exp_dict
          , name='cycles-u{INS_DEL_FRAC}-k{MAXKEY}.png'
          , title='u={INS_DEL_FRAC} k={MAXKEY}: Cycles/op vs thread count'
          , varying_cols_list=['INS_DEL_FRAC', 'MAXKEY']
          , series='DS_TYPENAME'
          , x_axis='TOTAL_THREADS'
          , y_axis='PAPI_TOT_CYC'
          , plot_type='bars'
          , plot_cmd_args=plot_cmd_args
    )
    add_plot_set(
            exp_dict
          , name='instructions-u{INS_DEL_FRAC}-k{MAXKEY}.png'
          , title='u={INS_DEL_FRAC} k={MAXKEY}: Instructions/op vs thread count'
          , varying_cols_list=['INS_DEL_FRAC', 'MAXKEY']
          , series='DS_TYPENAME'
          , x_axis='TOTAL_THREADS'
          , y_axis='PAPI_TOT_INS'
          , plot_type='bars'
          , plot_cmd_args=plot_cmd_args
    )

    ##
    ## we can also filter data to a single series before plotting it
    ##
    ## example: a plot with data filtered to single threaded runs
    ##

    ## note that with only a single series, there is no need for a legend (so we update the plot_cmd_args)
    plot_cmd_args = ''

    add_plot_set(
            exp_dict
          , name='sequential-u{INS_DEL_FRAC}-k{MAXKEY}.png'
          , title='u={INS_DEL_FRAC} k={MAXKEY}: Single threaded throughput'
          , filter='TOTAL_THREADS == 1'
          , varying_cols_list=['INS_DEL_FRAC', 'MAXKEY']
          , x_axis='DS_TYPENAME'
          , y_axis='total_throughput'
          , plot_type='bars'
          , plot_cmd_args=plot_cmd_args
    )

    ##
    ## rather than filtering the thread count to a single value,
    ## we could also make it part of the name, so one plot is produced for each thread count.
    ## moreover, supposing we only want plots for the minimum and maximum thread counts,
    ##     (where the min/max thread counts are obtained directly from our run_param that we added above)
    ##     then we can filter appropriately first...
    ##

    ## construct filter string
    p = get_run_param( exp_dict, 'TOTAL_THREADS' )
    min_threads = p[0]
    max_threads = p[len(p)-1]
    filter_string = 'TOTAL_THREADS in ({}, {})'.format(min_threads, max_threads)

    add_plot_set(
            exp_dict
          , name='maxresident-u{INS_DEL_FRAC}-k{MAXKEY}-n{TOTAL_THREADS}.png'
          , title='u={INS_DEL_FRAC} k={MAXKEY} n={TOTAL_THREADS}: Max resident size (MB)'
          , filter=filter_string, varying_cols_list=['INS_DEL_FRAC', 'MAXKEY', 'TOTAL_THREADS']
          , x_axis='DS_TYPENAME'
          , y_axis='maxresident_mb'
          , plot_type='bars'
          , plot_cmd_args=plot_cmd_args
    )

    ##
    ## we can also generate html web pages to display various layouts of plots
    ## more specifically, you can define pages (or sets of pages) that
    ##  arrange the plots specified above into html tables, either:
    ##  - by specifying explicitly that a particular PLOT belongs in a particular page, table, row, column, or
    ##  - by specifying that a PAGE should use particular FIELD to define tables, rows and columns
    ##

    add_page_set( exp_dict, image_files='throughput-u{INS_DEL_FRAC}-k{MAXKEY}.png' )
    ## note: the above is interpreted the same as:
    ##  add_page_set(
    ##          exp_dict
    ##        , image_files='throughput-u{INS_DEL_FRAC}-k{MAXKEY}.png'
    ##        , name='throughput'
    ##        , column_field='INS_DEL_FRAC'
    ##        , row_field='MAXKEY'
    ##  )
    ## the more verbose expression simply gives you more control over which fields are placed in rows and columns
    ##
    ## how this interpretation is done:
    ## the image_files string has its .png extension removed, then it is tokenized by hypens ('-'), and
    ##  - the first token serving becomes the name argument
    ##  - the second defines the columns in each table (column_field argument)
    ##  - the third defines the rows in each table (row_field argument)
    ##  - the fourth, if it exists, defines a SET of tables in the same page (table_field argument)
    ##  - any further tokens define a SET of pages (i.e., the cross product of the remaining tokens),
    ##    and the filename for each such page is suffixed with a particular assigment of those fields
    ##    (page_field_list argument -- a python list)

    add_page_set( exp_dict, image_files='maxresident-u{INS_DEL_FRAC}-k{MAXKEY}-n{TOTAL_THREADS}.png' )
    ## note: the above is interpreted the same as:
    ##  add_page_set(
    ##          exp_dict
    #$        , image_files='maxresident-u{INS_DEL_FRAC}-k{MAXKEY}-n{TOTAL_THREADS}.png'
    ##        , name='maxresident'
    ##        , table_field='TOTAL_THREADS'
    ##        , column_field='INS_DEL_FRAC'
    ##        , row_field='MAXKEY'
    ##  )

    add_page_set( exp_dict, image_files='l2miss-u{INS_DEL_FRAC}-k{MAXKEY}.png' )
    add_page_set( exp_dict, image_files='l3miss-u{INS_DEL_FRAC}-k{MAXKEY}.png' )
    add_page_set( exp_dict, image_files='cycles-u{INS_DEL_FRAC}-k{MAXKEY}.png' )
    add_page_set( exp_dict, image_files='instructions-u{INS_DEL_FRAC}-k{MAXKEY}.png' )
    add_page_set( exp_dict, image_files='sequential-u{INS_DEL_FRAC}-k{MAXKEY}.png' )

## extractor functions take as arguments: a file to load data from, and a field name
def get_maxres(exp_dict, file_name, field_name):
    ## manually parse the maximum resident size from the output of `time` and add it to the data file
    maxres_kb_str = shell_to_str('grep "maxres" {} | cut -d" " -f6 | cut -d"m" -f1'.format(file_name))
    return float(maxres_kb_str) / 1000


import sys
import random

MAX_VALUE = 100

class Task:
    def __init__(self, p, r, d):
        self.p = p
        self.r = r
        self.d = d

    def __str__(self):
        return "|r={}, p={}, d={}|".format(self.r, self.p, self.d)

class Generator:
    def generate(self, n):
        self.tasks = []
        self.s = []
        s_sum = 0
        p_sum = 0
        for i in range(n):
            self.s.append([])
            p_num = random.randint(1, MAX_VALUE)
            p_sum+=p_num
            self.tasks.append(Task(p_num, 0, 0))
            for j in range(n):
                if i == j:
                    self.s[i].append(0)
                else:
                    number = random.randint(1, MAX_VALUE/10)
                    s_sum+=number
                    self.s[i].append(number)

        s_mean = int(s_sum / (n*n - n))
        p_mean = int(p_sum / n)
        random_order_tasks = random.sample(range(n), n)
        r = 0
        for i in random_order_tasks:
            self.tasks[i].r = r
            self.tasks[i].d = r + self.tasks[i].p + random.randint(p_mean, p_mean + s_mean)
            r += random.randint(0, abs(p_mean - self.tasks[i].p))

    def __init__(self):
        self.tasks = []
        self.s = []

    def print_tasks(self):
        for i in range(len(self.tasks)):
            print("{}{}".format((i+1), self.tasks[i]))

    def print_s(self):
        for i in self.s:
            print(i)

    def save_to_file(self, filename):
        f = open(filename, "w")
        f.write(str(len(self.tasks)))
        f.write("\n")
        for task in self.tasks:
            f.write("{} {} {}\n".format(task.p, task.r, task.d))

        for i in range(len(self.s)):
            for j in range(len(self.s)):
                f.write(str(self.s[i][j]))
                f.write(" ")
            f.write("\n")
        f.close()

if __name__ == '__main__':
    g = Generator()
    args = sys.argv[1:]
    if len(args) < 2:
        print("Wrong number of arguments. Should be '{} filename start end step'".format(__file__))
        sys.exit()

    filename = args[0]
    if len(args) == 2:
        n = int(args[1])
        g.generate(n)
        #g.print_tasks()
        #g.print_s()
        g.save_to_file(filename)

    if len(args) == 4:
        for i in range(int(args[1]), int(args[2]), int(args[3])):
            g.generate(i)
            g.save_to_file(filename + str(i))


load("//:visibility.bzl", "RULES_KOTLIN")
load("//kotlin/common:is_eligible_friend.bzl", "is_eligible_friend")

visibility(RULES_KOTLIN)

def _get_output_jars(target, _ctx_rule):
    # We can't simply use `JavaInfo.compile_jars` because we only want the JARs directly created by
    # `target`, and not JARs from its `exports`
    return [output.compile_jar for output in target[JavaInfo].java_outputs if output.compile_jar]

kt_friend_jars_visitor = struct(
    name = "friend_jars",
    visit_target = _get_output_jars,
    filter_edge = is_eligible_friend,
    finish_expansion = None,
    process_unvisited_target = None,
)


#Iterative Binary Search Algorithm
def binary_search_iterative(numbersList, value):
    low = 0
    high = len(numbersList)-1
    while low <= high:
        mid = (low+high)//2
        if numbersList[mid] > value: high = mid-1
        elif numbersList[mid] < value: low = mid+1
        else: return mid
    return -1

A = [534,246,933,127,277,321,454,565,220]
print(binary_search_iterative(A,277))

#Recursive Binary Search Algorithm
def binary_search_recursive(numbersList, value, low = 0, high = -1):
    if not numbersList: return -1
    if(high == -1): high = len(numbersList)-1
    if low == high:
        if numbersList[low] == value: return low
        else: return -1
    mid = low + (high-low)//2
    if numbersList[mid] > value:
        return binary_search_recursive(numbersList, value, low, mid-1)
    elif numbersList[mid] < value:
        return binary_search_recursive(numbersList, value, mid+1, high)
    else: return mid

A = [534,246,933,127,277,321,454,565,220]
print(binary_search_recursive(A,277))


import datetime as dt
import math
import random
import sys
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
from pathlib import Path
from typing import Dict, List, Union

import botocore.exceptions
import pandas as pd

from sm_serverless_benchmarking.analysis import (compute_cost_savings,
                                                 summarize_concurrency_results,
                                                 summarize_stability_results)
from sm_serverless_benchmarking.endpoint import ServerlessEndpoint
from sm_serverless_benchmarking.report import generate_html_report
from sm_serverless_benchmarking.utils import read_example_args_file

def create_endpoint(
    model_name: str, memory_size: int = 1024, max_concurrency: int = 1
) -> ServerlessEndpoint:
    ep = ServerlessEndpoint(
        model_name=model_name, memory_size=memory_size, max_concurrency=max_concurrency
    )
    ep.create_endpoint()
    return ep

def timed_invocation(endpoint: ServerlessEndpoint, invoke_args: Dict[str, str]):
    time.sleep(random.random())
    t1 = time.perf_counter()
    response_size = 0
    try:
        response = endpoint.invoke_endpoint(invoke_args)
        response_size = int(
            response["ResponseMetadata"]["HTTPHeaders"]["content-length"]
        )
        # body = response["Body"].read()
        # response_size = sys.getsizeof(
        #     body
        # )  # calculate response size to estimate per inference cost

    except botocore.exceptions.ClientError as error:
        if error.response["Error"]["Code"] == "ThrottlingException":
            return {
                "invocation_latency": -1,
                "throttle_exception": 1,
                "start_time": t1,
                "end_time": time.perf_counter(),
            }

        elif error.response["Error"]["Code"] == "ModelError":
            if "insufficient memory" in error.response["Error"]["Message"]:
                return {
                    "invocation_latency": -1,
                    "throttle_exception": 0,
                    "insufficient_memory_error": 1,
                    "other_model_error": 0,
                    "start_time": t1,
                    "end_time": time.perf_counter(),
                }
            else:
                print(
                    f"error in endpoint {endpoint.endpoint_name} ",
                    error.response["Error"]["Message"],
                )
                return {
                    "invocation_latency": -1,
                    "throttle_exception": 0,
                    "insufficient_memory_error": 0,
                    "other_model_error": 1,
                    "start_time": t1,
                    "end_time": time.perf_counter(),
                }

    t2 = time.perf_counter()

    return {
        "invocation_latency": (t2 - t1) * 1000,
        "response_size": response_size,
        "throttle_exception": 0,
        "insufficient_memory_error": 0,
        "other_model_error": 0,
        "start_time": t1,
        "end_time": t2,
    }

def create_endpoint_configs(
    memory_size: Union[int, List[int]], max_concurrency: Union[int, List[int]]
):

    if type(memory_size) == int:
        memory_size = [memory_size]

    if type(max_concurrency) == int:
        max_concurrency = [max_concurrency]

    endpoint_configs = []
    for mem_size in memory_size:
        for max_conc in max_concurrency:
            endpoint_configs.append(
                {"memory_size": mem_size, "max_concurrency": max_conc}
            )

    return endpoint_configs

def setup_endpoints(
    model_name: str,
    memory_size: Union[int, List[int]],
    max_concurrency: Union[int, List[int]],
    sleep: int = 0,
):

    update_configs = create_endpoint_configs(memory_size, max_concurrency)

    endpoint_futures = []

    with ThreadPoolExecutor(max_workers=len(update_configs)) as executor:

        for config_kwargs in update_configs:

            future = executor.submit(create_endpoint, model_name, **config_kwargs)
            endpoint_futures.append(future)
            time.sleep(2)

        endpoints = [future.result() for future in as_completed(endpoint_futures)]

    endpoints = [endpoint for endpoint in endpoints if endpoint._created]
    time.sleep(sleep)  # sleep to increase chance of cold start

    return endpoints

def stability_benchmark(
    endpoint: ServerlessEndpoint, invoke_args_list, num_invocations=1000, error_thresh=3
):

    results = []
    errors = 0

    for _ in range(num_invocations):
        invoke_arg_idx = random.randint(0, len(invoke_args_list)-1)
        invoke_args = invoke_args_list[invoke_arg_idx]
        result = timed_invocation(endpoint, invoke_args)
        result["invoke_arg_index"] = invoke_arg_idx

        if result["invocation_latency"] == -1:
            errors += 1

        if errors >= error_thresh:
            print(
                f"Terminating benchmark for {endpoint.endpoint_name} due to excessive endpoint errors"
            )

            return results

        results.append(result)

    return results

def run_stability_benchmark(
    endpoints: List[ServerlessEndpoint],
    invoke_args_list: List[Dict[str, str]],
    num_invocations: int = 1000,
    error_thresh: int = 3,
    result_save_path: str = ".",
):

    endpoint_benchmark_futures = {}
    benchmark_results = []
    all_endpoint_metrics = []

    save_path = Path(result_save_path) / "stability_benchmark_raw_results"
    save_path.mkdir(exist_ok=True, parents=True)

    benchmark_start_time = dt.datetime.utcnow()

    with ThreadPoolExecutor(max_workers=len(endpoints)) as executor:

        for endpoint in endpoints:
            future = executor.submit(
                stability_benchmark,
                endpoint,
                invoke_args_list,
                num_invocations,
                error_thresh,
            )
            endpoint_benchmark_futures[future] = endpoint

        for future in as_completed(endpoint_benchmark_futures):
            endpoint = endpoint_benchmark_futures[future]

            result = future.result()
            df_result = pd.DataFrame(result)
            df_result["memory_size"] = endpoint.memory_size
            benchmark_results.append(df_result)

            benchmark_end_time = dt.datetime.utcnow()
            lookback_window = (
                benchmark_end_time - benchmark_start_time
            ) + dt.timedelta(hours=1)

            endpoint_metrics = endpoint.get_endpoint_metrics(lookback_window)
            df_endpoint_metrics = pd.DataFrame(endpoint_metrics)
            df_endpoint_metrics["memory_size"] = endpoint.memory_size
            all_endpoint_metrics.append(df_endpoint_metrics)

    df_benchmark_results = pd.concat(benchmark_results).reset_index(drop=True)
    df_endpoint_metrics = pd.concat(all_endpoint_metrics).reset_index(drop=True)

    df_benchmark_results.to_csv(
        save_path / "invocation_benchmark_results.csv", index=False
    )
    df_endpoint_metrics.to_csv(save_path / "endpoint_metrics.csv", index=False)

    return df_benchmark_results, df_endpoint_metrics

def concurrency_benchmark(
    endpoint: ServerlessEndpoint, invoke_args_list, num_invocations=50, num_clients=1
):

    futures = []

    with ThreadPoolExecutor(max_workers=num_clients) as executor:

        for _ in range(num_invocations):
            invoke_args = random.choice(invoke_args_list)

            futures.append(executor.submit(timed_invocation, endpoint, invoke_args))

        results = [future.result() for future in futures]

    return results

def run_concurrency_benchmark(
    endpoints: List[ServerlessEndpoint],
    invoke_args_list: List[Dict[str, str]],
    num_invocations: int = 1000,
    num_clients_multipliers: List[float] = [1, 1.5, 1.75, 2],
    result_save_path: str = ".",
):

    endpoint_benchmark_futures = {}
    benchmark_results = []
    all_endpoint_metrics = []

    save_path = Path(result_save_path) / "concurrency_benchmark_raw_results"
    save_path.mkdir(exist_ok=True, parents=True)

    benchmark_start_time = dt.datetime.now()

    with ThreadPoolExecutor(max_workers=len(endpoints)) as executor:
        seen_conc_clients = set()

        for multiplier in num_clients_multipliers:
            futures = []
            for endpoint in endpoints:
                num_clients = math.ceil(endpoint.max_concurrency * multiplier)
                max_conc_num_clients = (endpoint.max_concurrency, num_clients)

                if max_conc_num_clients in seen_conc_clients:
                    continue
                seen_conc_clients.add(max_conc_num_clients)

                future = executor.submit(
                    concurrency_benchmark,
                    endpoint,
                    invoke_args_list,
                    num_invocations,
                    num_clients,
                )
                endpoint_benchmark_futures[future] = (endpoint, num_clients)
                futures.append(future)

            while any([future.running() for future in futures]):
                time.sleep(5)

        for future in as_completed(endpoint_benchmark_futures):
            endpoint = endpoint_benchmark_futures[future][0]

            result = future.result()
            df_result = pd.DataFrame(result)
            df_result["memory_size"] = endpoint.memory_size
            df_result["max_concurrency"] = endpoint.max_concurrency
            df_result["num_clients"] = endpoint_benchmark_futures[future][1]
            benchmark_results.append(df_result)

            benchmark_end_time = dt.datetime.now()
            lookback_window = (
                benchmark_end_time - benchmark_start_time
            ) + dt.timedelta(hours=1)
            endpoint_metrics = endpoint.get_endpoint_metrics(lookback_window)
            df_endpoint_metrics = pd.DataFrame(endpoint_metrics)
            df_endpoint_metrics["memory_size"] = endpoint.memory_size
            df_endpoint_metrics["max_concurrency"] = endpoint.max_concurrency
            df_endpoint_metrics["num_clients"] = endpoint_benchmark_futures[future][1]
            all_endpoint_metrics.append(df_endpoint_metrics)

    df_benchmark_results = pd.concat(benchmark_results).reset_index(drop=True)
    df_endpoint_metrics = pd.concat(all_endpoint_metrics).reset_index(drop=True)

    df_benchmark_results.to_csv(
        save_path / "invocation_benchmark_results.csv", index=False
    )
    df_endpoint_metrics.to_csv(save_path / "endpoint_metrics.csv", index=False)

    return df_benchmark_results, df_endpoint_metrics

def tear_down_endpoints(endpoints: List[ServerlessEndpoint]):
    for endpoint in endpoints:
        try:
            endpoint.clean_up()
        except:
            pass

def run_serverless_benchmarks(
    model_name: str,
    invoke_args_examples_file: Path,
    cold_start_delay: int = 0,
    memory_sizes: List[int] = [1024, 2048, 3072, 4096, 5120, 6144],
    stability_benchmark_invocations: int = 1000,
    stability_benchmark_error_thresh: int = 3,
    include_concurrency_benchmark: bool = True,
    concurrency_benchmark_max_conc: List[int] = [2, 4, 8],
    concurrency_benchmark_invocations: int = 1000,
    concurrency_num_clients_multiplier: List[float] = [1, 1.5, 1.75, 2],
    result_save_path: str = ".",
)->str:
    """Runs a suite of SageMaker Serverless Benchmarks on the specified model_name.
    Will automatically deploy endpoints for the specified model_name and perform a tear down
    upon completion of the benchmark or an error.

    There are two types of benchmarks that are supported and both are executed by defaults
    - Stability Benchmark: Deploys an endpoint for each of the specified memory configurations and
    max concurrency of 1. Invokes the endpoint the specified number of times and determines the stable
    and most cost effective configuration

    - Concurrency Benchmark: Deploys endpoints with different max_concurrency configurations and performs
    a load test with a simulated number of concurrent clients

    Args:
        model_name (str): Name of the SageMaker Model resource
        invoke_args_examples_file (Path: Path to the jsonl file containing the example invocation arcguments
        cold_start_delay (int, optional): Number of seconds to sleep before starting the benchmark. Helps to induce a cold start on initial invocation. Defaults to 0.
        memory_sizes (List[int], optional): List of memory configurations to benchmark Defaults to [1024, 2048, 3072, 4096, 5120, 6144].
        stability_benchmark_invocations (int, optional): Total number of invocations for the stability benchmark. Defaults to 1000.
        stability_benchmark_error_thresh (int, optional): The allowed number of endpoint invocation errors before the benchmark is terminated for a configuration. Defaults to 3.
        include_concurrency_benchmark (bool, optional): Set True to run the concurrency benchmark with the optimal configuration from the stability benchmark. Defaults to True.
        concurrency_benchmark_max_conc (List[int], optional): A list of max_concurency settings to benchmark. Defaults to [2, 4, 8].
        concurrency_benchmark_invocations (int, optional): Total number of invocations for the concurency benchmark. Defaults to 1000.
        concurrency_num_clients_multiplier (List[int], optional): List of multipliers to specify the number of simulated clients which is determined by max_concurency * multiplier. Defaults to [1, 1.5, 1.75, 2].
        result_save_path (str, optional): The location to which the output artifacts will be saved. Defaults to ".".

    Returns:
        str: HTML for the generated benchmarking report
    """

    function_args = locals()
    benchmark_config = pd.Series(function_args).to_frame()

    invoke_args_examples = read_example_args_file(invoke_args_examples_file)

    stability_endpoints = setup_endpoints(
        model_name, memory_size=memory_sizes, max_concurrency=1, sleep=cold_start_delay
    )

    try:
        (
            df_stability_benchmark_results,
            df_stability_endpoint_metrics,
        ) = run_stability_benchmark(
            stability_endpoints,
            invoke_args_list=invoke_args_examples,
            num_invocations=stability_benchmark_invocations,
            error_thresh=stability_benchmark_error_thresh,
            result_save_path=result_save_path,
        )
        (
            df_stability_results,
            df_stability_summary,
            df_stability_metric_summary,
            stability_latency_distribution_fig,
        ) = summarize_stability_results(
            df_stability_benchmark_results,
            df_stability_endpoint_metrics,
            result_save_path=result_save_path,
        )

        avg_response_size = df_stability_results["response_size"].mean()
        (
            df_cost_savings,
            minimal_successful_config,
            comparable_sagemaker_instance,
            cost_vs_performance,
        ) = compute_cost_savings(
            df_stability_metric_summary,
            invoke_args_examples,
            average_response_size=avg_response_size,
            result_save_path=result_save_path
        )

    except Exception as e:
        print(f"Could not complete benchmark due to Exception: {e}")
        raise e

    finally:
        tear_down_endpoints(stability_endpoints)

    if include_concurrency_benchmark:
        concurrency_endpoints = setup_endpoints(
            model_name,
            memory_size=minimal_successful_config,
            max_concurrency=concurrency_benchmark_max_conc,
        )
        try:
            (
                df_conc_benchmark_results,
                df_conc_endpoint_metrics,
            ) = run_concurrency_benchmark(
                concurrency_endpoints,
                invoke_args_examples,
                num_invocations=concurrency_benchmark_invocations,
                num_clients_multipliers=concurrency_num_clients_multiplier,
                result_save_path=result_save_path,
            )
            (
                df_concurrency_metrics,
                df_concurrency_metric_summary,
                concurrency_latency_distribution_fig,
            ) = summarize_concurrency_results(
                df_conc_benchmark_results,
                df_conc_endpoint_metrics,
                result_save_path=result_save_path,
            )

        except Exception as e:
            print(f"Could not complete benchmark due to Exception: {e}")

        finally:
            tear_down_endpoints(concurrency_endpoints)

        report = generate_html_report(
            benchmark_config=benchmark_config,
            df_stability_summary=df_stability_summary,
            df_stability_metric_summary=df_stability_metric_summary,
            stability_latency_distribution=stability_latency_distribution_fig,
            df_cost_savings=df_cost_savings,
            cost_vs_performance=cost_vs_performance,
            optimal_memory_config=minimal_successful_config,
            comparable_instance=comparable_sagemaker_instance,
            df_concurrency_metrics=df_concurrency_metrics,
            df_concurrency_metric_summary=df_concurrency_metric_summary,
            concurrency_latency_distribution=concurrency_latency_distribution_fig,
            result_save_path=result_save_path,
        )
        return report

    else:
        report = generate_html_report(
            benchmark_config=benchmark_config,
            df_stability_summary=df_stability_summary,
            df_stability_metric_summary=df_stability_metric_summary,
            stability_latency_distribution=stability_latency_distribution_fig,
            df_cost_savings=df_cost_savings,
            cost_vs_performance=cost_vs_performance,
            optimal_memory_config=minimal_successful_config,
            comparable_instance=comparable_sagemaker_instance,
            result_save_path=result_save_path,
        )
        return report


from telethon.tl.functions.channels import EditBannedRequest
from telethon.tl.types import ChatBannedRights

from Cosmic import cosmo
from Cosmic.functions.handler import cosmic
from Cosmic.functions.misc import eor, get_user

BANNED_RIGHTS = ChatBannedRights(
    until_date=None,
    view_messages=True,
    send_messages=True,
    send_media=True,
    send_stickers=True,
    send_gifs=True,
    send_games=True,
    send_inline=True,
    embed_links=True,
)

UNBAN_RIGHTS = ChatBannedRights(
    until_date=None,
    send_messages=None,
    send_media=None,
    send_stickers=None,
    send_gifs=None,
    send_games=None,
    send_inline=None,
    embed_links=None,
)

KICK_RIGHTS = ChatBannedRights(until_date=None, view_messages=True)

MUTE_RIGHTS = ChatBannedRights(until_date=None, send_messages=True)

UNMUTE_RIGHTS = ChatBannedRights(until_date=None, send_messages=False)

@cosmic(pattern="kick ?(.*)")
async def kick(event):
    if event.is_private:
        return await eor(event, "Please use this in groups.")
    try:
        user, extra = await get_user(event)
        await cosmo(EditBannedRequest(event.chat_id, user, KICK_RIGHTS))
        await event.delete()
    except:
        pass

@cosmic(pattern="pin$")
async def pin(event):
    if event.is_private:
        return
    r = await event.get_reply_message()
    await r.pin()

@cosmic(pattern="kickme")
async def lmao(event):
    await event.client.delete_dialog(event.chat_id)

@cosmic(pattern="del")
async def bruh(event):
    x = await event.get_reply_message()
    await x.delete()
    await event.delete()

@cosmic(pattern="ban  ?(.*)")
async def ban(event):
    if event.is_private:
        return await eor(event, "Please use this in groups.")
    try:
        user, extra = await get_user(event)
        await cosmo(EditBannedRequest(event.chat_id, user, BANNED_RIGHTS))
        await event.delete()
    except:
        pass

@cosmic(pattern="dban ?(.*)")
async def dban(event):
    if event.is_private:
        return await event.edit("Please use this in groups.")
    try:
        user, _ = await get_user(event)
        await cosmo(EditBannedRequest(event.chat_id, user, BANNED_RIGHTS))
        await event.delete()
        await (
            await event.get_reply_message()
        ).delete() if event.reply_to_msg_id else await event.delete()
    except:
        pass

@cosmic(pattern="unban ?(.*)")
async def unban(event):
    if event.is_private:
        return await eor(event, "Please use this in groups.")
    try:
        user, extra = await get_user(event)
        await cosmo(EditBannedRequest(event.chat_id, user, UNBAN_RIGHTS))
        await event.delete()
    except:
        pass


import csv

from os import listdir
from os.path import isfile, join

FOLDER_PATH_SOURCE = 'data'
FOLDER_PATH_XML = 'data_xml'

def read_folder(folder_path):
    filenames = [str(f) for f in listdir(folder_path) if isfile(join(folder_path, f))]
    return filenames

def read_csv(folder, file):
    with open('{}/{}'.format(folder, file), newline='', encoding='utf8') as csvfile:
        csv_data = csv.reader(csvfile, delimiter=',')
        return_data = []

        # url, matchdatetime, station, show, showid, preview, Snippet

        for (index, data) in enumerate(csv_data):
            if index!=0:
                row_dict = dict()
                row_dict['filename'] = str(file)
                row_dict['url'] = str(data[0])
                row_dict['datetime'] = str(data[1])
                row_dict['station'] = str(data[2])
                row_dict['show'] = str(data[3])
                row_dict['showid'] = str(data[4])
                row_dict['preview'] = str(data[5])
                row_dict['snippet'] = str(data[6])

                return_data.append(row_dict)

    return return_data

def write_xml(folder, file, row_dict):
    with open('{}/{}.xml'.format(folder, file), "w", encoding="utf-8") as xml_file:
        xml_file.write("<add>\n")
        xml_file.write("<doc>\n")

        for key in row_dict:
            # print(key, row_dict[key])
            xml_file.write('<field name="{}">{}</field>\n'.format(key, row_dict[key]))

        xml_file.write("</doc>\n")
        xml_file.write("</add>\n")

if __name__ == "__main__":

    all_csv_data = []
    file_paths = read_folder(FOLDER_PATH_SOURCE)
    for file in file_paths:
        file_data = read_csv(FOLDER_PATH_SOURCE, file)
        for row in file_data:
            all_csv_data.append(row)
    index = 10
    for row in all_csv_data:
        write_xml(FOLDER_PATH_XML, str(index), row)
        index = index + 1


from django.test import TestCase
from Players.models import DimPlayers, FactPlayerstats, DimPositions, DimPositioncategory
from ProjectFifa.Players.models import DimClubs
from django.db.models import Q, Avg

from ProjectFifa.ProjectFifa.settings import MIDDLEWARE

# Create your tests here.

class correctplayerandyear(TestCase):

    def setUp(self):
        DimPlayers.objects.create(sofifa_id=123, short_name= 'Cristiano Ronaldo', long_name = 'fifjskjfks', dob='1990-10-10')
        FactPlayerstats.objects.create(sofifa_id=123, year= 2016)

    def test_player_is_ronaldo_and_year_is_2016(self):
        #Checks if the correct player and correct year is filtered
        query = 'Cristiano Ronaldo'
        request_year = 2015
        player = FactPlayerstats.objects.filter(sofifa_id__short_name__icontains=query, year=request_year)
        ronaldo = player[0].sofifa_id.short_name
        year = player[0].year
        self.assertEqual(ronaldo, 'Cristiano Ronaldo')
        self.assertEqual(year, 2015)

class correctclub(TestCase):
    #Checks to see if the correct club is selected
    def setUp(self):
        DimClubs.objects.create(clubid=1, club_name='London United', leagueid=1)

    def test_club_is_london_year(self):
        query = 'London United'
        club = DimClubs.objects.get(clubid=1)

class correctclubstats(TestCase):
    #Checks to see if the correct stats are calculated
    def setUp(self):
        DimClubs.objects.create(clubid=1, club_name="London United", leagueid=1)
        DimPositions.objects.create(positionid=1, position="ST", position_categoryid=1)
        DimPositions.objects.create(positionid=2, position="LW", position_categoryid=1)
        DimPositions.objects.create(positionid=3, position="CM", position_categoryid=2)
        DimPositions.objects.create(positionid=4, position="DM", position_categoryid=2)
        DimPositions.objects.create(positionid=5, position="CB", position_categoryid=3)
        DimPositions.objects.create(positionid=6, position="GK", position_categoryid=3)
        DimPositions.objects.create(positionid=7, position="SUB", position_categoryid=4)
        DimPositions.objects.create(positionid=8, position="RES", position_categoryid=4)
        DimPositioncategory.objects.create(position_category="ATT", position_categoryid=1)
        DimPositioncategory.objects.create(position_category="MID", position_categoryid=2)
        DimPositioncategory.objects.create(position_category="DEF", position_categoryid=3)
        DimPositioncategory.objects.create(position_category="NONE", position_categoryid=4)
        FactPlayerstats.objects.create(sofifa_id=1, club_name='London United', positionid=1, overall= 50, year=2021)
        FactPlayerstats.objects.create(sofifa_id=2, club_name='London United', positionid=2, overall= 20, year=2021)
        FactPlayerstats.objects.create(sofifa_id=3, club_name='London United', positionid=3, overall= 30, year=2021)
        FactPlayerstats.objects.create(sofifa_id=4, club_name='London United', positionid=4, overall= 70, year=2021)
        FactPlayerstats.objects.create(sofifa_id=5, club_name='London United', positionid=5, overall= 90, year=2021)
        FactPlayerstats.objects.create(sofifa_id=6, club_name='London United', positionid=6, overall= 25, year=2021)

    def test_if_clubstats_is_correct(self):
        clubname = "London United"
        request_year = 2021
        #Check if the stats of ATT MID DEF is calculated correctly
        from django.db.models import Func
        class Round_club_avg(Func):
            function = "ROUND"
            template = "%(function)s(%(expressions)s::numeric, 0)"
        aggregate_scores = DimPositioncategory.objects.annotate(num_players = Round_club_avg(Avg('dimpositions__factplayerstats__overall', filter=Q(dimpositions__factplayerstats__club_name= clubname)&Q(dimpositions__factplayerstats__year=request_year)))).filter(~Q(position_categoryid = 3))
        ATT = aggregate_scores[0].position_category
        MID = aggregate_scores[1].position_category
        DEF = aggregate_scores[2].position_category
        self.assertEqual(ATT, 35)
        self.assertEqual(MID, 50)
        #true value is 57.5 but rounds up to 60
        self.assertEqual(DEF, 60)


'''
Source: https://leetcode.com/problems/counting-bits/
Author: Vishal Prabhachandar
Date: 2021-July-25

Time complexity: O(n)
Space complexity: O(n)
'''

class Solution(object):
    def countBits(self, n):
        '''
        returning the bits in a range

        :param  n: integer
        :return counter: list of integers
        '''
        counter = [0]
        for index in range(1, n+1):
            counter.append(counter[index >> 1] + (index & 1))
        return counter


def restoreIpAddresses(self, s: str) -> List[str]:
    results = []
    self.dfs(s, 4, [], results)
    # print(results)
    return ['.'.join(items) for items in results]

def isValid(self, s):
    if s == '0':
        return True
    if s[0] == '0':
        return False
    return 0 <= int(s) <= 255

def dfs(self, s, n, path, results):
    if n == 0:
        if s == "":
            results.append(path[:])
        return

    for i in range(3):
        if i < len(s):
            pre_ip = s[:i + 1]
            if self.isValid(pre_ip):
                path.append(pre_ip)
                self.dfs(s[i + 1:], n - 1, path, results)
                path.pop()


import sys
sys.stdin = open("input.txt", "r")
from pandas import DataFrame

T = int(input())
for tc in range(1, T+1):
    N = int(input())

    vel = 0
    result = 0
    for i in range(N):
        IN = list(map(int, input().split()))

        if IN[0] == 0:        # 유지
            result += vel
        else:                 # 가속(1)/감속(2)
            if IN[0] == 1:    # 가속
                vel += IN[1]
            else:             # 감속
                vel -= IN[1]
                if vel <= 0:  # 속도가 0 이하이면 0으로
                    vel = 0
            result += vel

    print("#{} {}".format(tc, result))


peoplelist = []
complist = []

with open('People.txt') as f:
    for line in f:
        l = line.strip()
        peoplelist.append(l)

with open('Compliment.txt') as f:
    for line in f:
        l = line.strip()
        complist.append(l)

import random
peoplenum = random.randrange(0, len(peoplelist))
compnum = random.randrange(0, len(complist))

print(peoplelist[peoplenum] + " " + complist[compnum])


import numpy as np
import cv2 as cv
# 鼠标回调函数

def draw_circle(event, x, y, flags, param):
    if event == cv.EVENT_LBUTTONDBLCLK:
        cv.circle(img1, (x, y), 100, (255, 0, 0), 3)

# 创建一个黑色图像，一个窗口，然后和回调绑定
img1 = cv.imread("001.jpg")
cv.namedWindow('image', cv.WINDOW_AUTOSIZE)
cv.setMouseCallback('image', draw_circle)
while(1):
    cv.imshow('image', img1)
    if cv.waitKey(20) & 0xFF == 27:
        break
cv.destroyAllWindows()


total = 0
for num in range(101):
    total = total + num
print(total)

'''
1+99
2+98
3+97
49+51
50*100
5000+50
'''

# range(x, y, z)
# start, stop, step
# stop is not included
# step = how much to increase by


class Node():
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList():
    def __init__(self):
        self.head = None
        self.sizes = 0

    def append(self, data):
        node = Node(data)
        if self.head == None:
            self.head = node
            self.sizes=1
        else:
            run = self.head
            while run.next != None:
                run = run.next
            run.next = node
            self.sizes += 1

    def isEmpty(self):
        return True if self.size == 0 else False

    def pop(self):
        if self.size() == 1:
            i = self.head
            self.head = None
            self.sizes = 0
            return i
        elif self.size()==2:
            run=self.head
            i=run.next
            run.next=None
            self.sizes=1
            return i
        elif self.size()>1:
            run = self.head
            while run.next.next != None:
                run = run.next
            i = run.next
            run.next = None
            self.sizes -= 1
            return i

    def size(self):
        return self.sizes

    def __str__(self):
        item = ''
        if self.size()>1:
            run = self.head
            while run.next != None:
                item += str(run.data)+' '
                run = run.next
            item += str(run.data)+' '
            return item
        elif self.size()==1:
            return str(self.head.data)
        else:
            return item

inp = input('Enter Input (L1,l2) : ').split()

l1 = inp[0].split('->')
l2 = inp[1].split('->')

ll1 = LinkedList()
ll2 = LinkedList()

for i in l1:
    ll1.append(i)
for i in l2:
    ll2.append(i)

print('L1    :', ll1)
print('L2    :', ll2)

while not ll2.isEmpty():
    ll1.append(ll2.pop())

print('Merge :', ll1)


import copy
from clause import Clause
from form import Form

def flatten(xs):
	if (xs == None or len(xs) == 0):
		return []

	ys = []
	for x in xs:
		if isinstance(x, list):
			ys = ys + flatten(x)
		else:
			ys.append(x)
	return ys

def is_literal(l):
	return isinstance(l, str)

def is_clause(clause):
	return isinstance(clause, Clause)

def is_formula(F):
	return isinstance(F, Form)

def make_singleton_clause(l):
	return Clause(-1, [l])

def unpack_unit_clause(clause):
	return clause.get_unit_literal()

# logical not
def lnot(l):
	assert is_literal(l), "lnot assert" + str(l)
	if (is_neg_literal(l)):
		return l[1:]
	else:
		return "-" + l

def is_neg_literal(l):
	assert is_literal(l), "is_neg_literal assert" + str(l)
	return l[0] == "-"

def ap_literal(l):
	assert is_literal(l), "ap_literal assert" + str(l)
	if (is_neg_literal(l)):
		return lnot(l)
	else:
		return l

def ap_clause(clause):
	assert is_clause(clause), "ap_clause assert" + str(clause)
	lit_set = set()
	for l in clause.all_literals():
		lit_set.add(ap_literal(l))
	return lit_set

def ap_formula(F):
	assert is_formula(F), "ap_formula assert" + str(F)
	lit_set = set()
	for clause in F.all_clauses():
		lit_set = lit_set | ap_clause(clause)
	return lit_set

def is_empty_cnf(F):
	assert is_formula(F), "is_empty_cnf assert" + str(F)
	return len(F) == 0

def contains_empty_clause(F):
	assert is_formula(F), "contains_empty_clause assert" + str(F)
	for clause in F.all_clauses():
		if (len(clause) == 0):
			return True
	return False

def find_empty_clause(F):
	assert is_formula(F), "find_empty_clause assert" + str(F)
	for clause in F.all_clauses():
		if (len(clause) == 0):
			return clause

def exists_unit_clause(F):
	assert is_formula(F), "exists_unit_clause assert" + str(F)
	for clause in F.all_clauses():
		if (len(clause) == 1):
			return True

	return False

def find_unit_clause(F):
	assert is_formula(F), "find_unit_clause assert" + str(F)
	for clause in F.all_clauses():
		if clause.id == -1:
			return clause
	for clause in F.all_clauses():
		if (len(clause) == 1):
			return clause
	return None

# to convert the output to a human-readable array
def final_dec_set_to_bool_arr(dec, num_vars):
	rv = []
	for i in range(1,num_vars+1):
		if str(i) in dec:
			rv.append((i,True))
		elif '-' + str(i) in dec:
			rv.append((i,False))
		else:
			rv.append((i,False))
	return rv


#!/usr/bin/env python3

"""Small example that shows how to work with extendeable arrays of different
types, strings included."""

import numpy as np
import tables as tb

# Open a new empty HDF5 file
filename = "earray2.h5"
fileh = tb.open_file(filename, mode="w")
# Get the root group
root = fileh.root

# Create an string atom
a = tb.StringAtom(itemsize=1)
# Use it as a type for the enlargeable array
hdfarray = fileh.create_earray(root, 'array_c', a, (0,), "Character array")
hdfarray.append(np.array(['a', 'b', 'c']))
# The next is legal:
hdfarray.append(np.array(['c', 'b', 'c', 'd']))
# but these are not:
# hdfarray.append(array([['c', 'b'], ['c', 'd']]))
# hdfarray.append(array([[1,2,3],[3,2,1]], dtype=uint8).reshape(2,1,3))

# Create an atom
a = tb.UInt16Atom()
hdfarray = fileh.create_earray(root, 'array_e', a, (2, 0, 3),
                               "Unsigned short array")

# Create an enlargeable array
a = tb.UInt8Atom()
hdfarray = fileh.create_earray(root, 'array_b', a, (2, 0, 3),
                               "Unsigned byte array",
                               tb.Filters(complevel=1))

# Append an array to this table
hdfarray.append(
    np.array([[1, 2, 3], [3, 2, 1]], dtype=np.uint8).reshape(2, 1, 3))
hdfarray.append(
    np.array([[1, 2, 3], [3, 2, 1], [2, 4, 6], [6, 4, 2]],
             dtype=np.uint8).reshape(2, 2, 3) * 2)
# The next should give a type error:
# hdfarray.append(array([[1,0,1],[0,0,1]], dtype=Bool).reshape(2,1,3))

# Close the file
fileh.close()

# Open the file for reading
fileh = tb.open_file(filename, mode="r")
# Get the root group
root = fileh.root

a = root.array_c.read()
print("Character array -->", repr(a), a.shape)
a = root.array_e.read()
print("Empty array (yes, this is suported) -->", repr(a), a.shape)
a = root.array_b.read(step=2)
print("Int8 array, even rows (step = 2) -->", repr(a), a.shape)

print("Testing iterator:", end=' ')
# for x in root.array_b.iterrows(step=2):
for x in root.array_b:
    print("nrow-->", root.array_b.nrow)
    print("Element-->", x)

print("Testing getitem:")
for i in range(root.array_b.shape[0]):
    print("array_b[" + str(i) + "]", "-->", root.array_b[i])
# The nrows counts the growing dimension, which is different from the
# first index
for i in range(root.array_b.nrows):
    print("array_b[:," + str(i) + ",:]", "-->", root.array_b[:, i, :])
print("array_c[1:2]", repr(root.array_c[1:2]))
print("array_c[1:3]", repr(root.array_c[1:3]))
print("array_b[:]", root.array_b[:])

print(repr(root.array_c))
# Close the file
fileh.close()


import numpy as np
import matplotlib.pyplot as plt
from random import randint
from  matplotlib.animation import FuncAnimation

from os import system
cls = lambda: system("clear")

rndGrid = lambda r, c: np.array([[randint(0,1) for n in range(c)] for m in range(r)])

# cw = rndGrid(8, 10)

ax = [(r,c) for r in [-1, 0, 1] for c in [-1, 0, 1] if (r!=0) or (c!=0)]

cwSum = lambda cw: np.sum([np.roll(cw, a, (0, 1)) for a in ax], 0)

# np.logical_or(np.logical_and(cw == 1, cnt == 2), cnt == 3)*1

def newGen(cw):
    cnt = cwSum(cw)
    return np.logical_or(np.logical_and(cw == 1, cnt == 2), cnt == 3)*1

plot = plt.matshow(cw)

def init():
    plot.set_data(cw)
    return plot

def update(j):
    plot.set_data(cw)
    return [plot]

# anim = FuncAnimation(fig, update, init_func = init, frames=n_frames, interval = 30, blit=True)

plt.show()

def testcw(r, c=0):
    if c == 0:
        c = r
    cw = rndGrid(r, c)
    cnt = cwSum(cw)
    for w, n in zip(cw, cnt):
        print('\t', w, '\t', n)
    cw = newGen(cw)
    print(cw, '\n')

    cw = newGen(cw)
    print(cw, '\n')

    cw = newGen(cw)
    print(cw, '\n')

    cw = newGen(cw)
    print(cw, '\n')

    plt.matshow(cw)
    plt.show()


class Solution:
    def nextGreaterElements(self, nums):
        next_great = []
        for i in range(len(nums)):
            start = i
            next = start + 1
            cur_max = nums[start]
            while True:
                if next == len(nums): next = 0
                if next == start: break
                cur_max = max(cur_max, nums[next])
                if cur_max > nums[start]:
                    break
                if next < start:
                    if nums[next] == nums[start]:
                        cur_max = next_great[next]
                        break
                    if nums[start] > nums[next] and next_great[next] > nums[start]:
                        cur_max = next_great[next]
                        break
                next += 1
            next_great.append(-1 if cur_max == nums[start] else cur_max)
        return next_great

s = Solution()
nums = [1,2,3,4,3]
nums2 = [1,3,2,1]
print (s.nextGreaterElements(nums2))


from flask import Flask, render_template, redirect
from config import ConfigDict
import os
import pyqrcode
import json

conf_dict = ConfigDict().conf_dict
config_path = 'config.py'
app = Flask(__name__, instance_relative_config=False)
app.config.from_pyfile(config_path)

@app.route('/', methods=['GET'])
def main_reroute():
    return redirect('index.html')

@app.route('/index.html', methods=['GET'])
def index_html():
        with open('tap_list.json', mode='r') as file_stream:
            beers_dict = json.loads(file_stream.read())
        return render_template('index.html', beers_dict=beers_dict)

@app.route('/qr_code.html', methods=['GET'])
def qr_code():
        return render_template('qr_code.html')

@app.route('/setup_keg.html', methods=['GET'])
def setup_keg():
    return render_template('setup_keg.html')

def main():
    HOST = os.getenv('SERVER_HOST', '0.0.0.0')
    PORT = int(conf_dict['server_port'])
    THREADED = app.config['THREADED']
    PROCESSES = app.config['PROCESSES']
    FLASK_DEBUG = app.config['FLASK_DEBUG']
    if app.config['USE_SSL']:
        app.run(debug=FLASK_DEBUG, host=HOST, port=PORT,
                threaded=THREADED,
                processes=PROCESSES,
                ssl_context=('server.crt', 'server.key'))
    else:
        app.run(debug=FLASK_DEBUG,
                host=HOST,
                port=PORT,
                threaded=THREADED,
                processes=PROCESSES)

if '__main__' == __name__:
    url = pyqrcode.create(conf_dict['site_url'])
    url.svg('./static/img/qr_code.svg', scale=8)
    main()


# reading FS sheet and calculate revenue
import csv
import pandas as pd
import matplotlib.pyplot as plt
import os
import re

# plot config
plt.style.use('ggplot')
plt.rcParams['figure.figsize'] = (15, 5)

filename = 'FSsheet2020.csv'

if os.path.exists(filename):
    df = pd.read_csv(filename, sep=',', error_bad_lines = False)
    df.drop(df.index[[0, 1, 2, 3, 4]], inplace = True)
    df.rename(columns = {'Unnamed: 0':'Date', 'Unnamed: 1':'Job Number'}, inplace=True)
    new_df = df[['Date', 'Job Number', 'Company - Name', 'Due date', 'Date out', 'Engineer', 'Description of samples', 'Hours']]
    new_df['Hours'].fillna(value = 1, inplace=True)
    new_df['Company - Name'].dropna()
    #pd.set_option('display.max_columns', 10)
    print(new_df[:5])

new_df['Engineer'].value_counts()
#df_wrong = df[df['Company - Name'].str.contains('-')]
#pd.set_option('display.max_rows', 1000)
#print(df['Company - Name'])
#new_df[['Company', 'Customer']] = df['Company - Name'].str.split("-", expand = True)
#print(new_df[:5])
#company_rank = new_df['Company'].value_counts().index.tolist()
#top_five = []
#for i in range(5):
#    top_five.append(comapy_rank[i])
#print(*[name for name in top_five], sep = '\n')
#
#
#new_df['Date'] = pd.to_datetime(new_df['Date'])
#new_df = new_df.set_index(new_df['Date'])
#performance = new_df.loc['1/01/2021':'2/01/2021']
#performance


import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
import PIL

#######################################
# parameters to play with
#######################################

# just take arbitrary models, thtat were trained on large image datasets
uri_vgg = 'https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2'

# img dimensions
content_size = 512
style_size = 512

# downsampling parameters
kernel_size = 3
strides = 1

# paths
p_style = "imgs/style/ships.jpg"
p_content = "imgs/content/rotwein.png"
p_result = "result.png"

def load_img_tensor(path_to_img: str, max_dim: int):
    img = tf.io.read_file(path_to_img)
    img = tf.image.decode_image(img, channels=3)
    img = tf.image.convert_image_dtype(img, tf.float32)

    shape = tf.cast(tf.shape(img)[:-1], tf.float32)
    long_dim = max(shape)
    scale = max_dim / long_dim

    new_shape = tf.cast(shape * scale, tf.int32)

    img = tf.image.resize(img, new_shape)
    img = img[tf.newaxis, :]
    return img

def tensor_to_image(tensor: tf.Tensor):
    tensor = tensor * 255
    tensor = np.array(tensor, dtype=np.uint8)
    if np.ndim(tensor) > 3:
        assert tensor.shape[0] == 1
        tensor = tensor[0]
    return PIL.Image.fromarray(tensor)

def get_hub_model(uri: str):
    return hub.load(uri)

class Predictor:
    def __init__(self, uri_model: str, kernel_size: int=3, strides: int=1):
        self.model = get_hub_model(uri_model)
        self.kernel_size = kernel_size
        self.strides = strides

    def predict(self, img_content: tf.Tensor, img_style: tf.Tensor):
        img_style = tf.nn.avg_pool(
            img_style,
            ksize=[self.kernel_size, self.kernel_size],
            strides=[self.strides, self.strides],
            padding='SAME')
        outputs = self.model(tf.constant(img_content), tf.constant(img_style))
        return outputs[0]

predictor = Predictor(uri_vgg, kernel_size, strides)
img_content = load_img_tensor(p_content, content_size)
img_style = load_img_tensor(p_style, style_size)
img_prediction = predictor.predict(img_content, img_style)
result_img = tensor_to_image(img_prediction)
result_img.save(p_result)


#!/usr/bin/env python

# SteamID To BattleEye GUID
# Author: AlFaMoDz | Twitter: @AlFaMoDzZ | GitHub: /alfamodz

import sys
from hashlib import md5

def uid2guid(steam_id):
    temp = ['BE']
    for x in xrange(8):
        temp.append(chr(steam_id & 0xFF))
        steam_id >>= 8
    return md5(''.join(temp)).hexdigest()

def main(argc, argv):
    if argc < 2:
        print('Usage: %s SteamID or .txt with IDs' % argv[0])
        return 1

    SteamID = argv[1]
    print('|--------------------------------------------------|')
    print('|       ~  SteamID to GUID by AlFaMoDz ~           |')
    print('| ~ github.com/alfamodz - twitter.com/alfamodzz ~  |')
    print('|--------------------------------------------------|\n')

    if (argv[1].isdigit()):
        print(' %s - %s' % (SteamID, uid2guid(int(SteamID))))
    else:
        with open(argv[1]) as f:
            array = []
            for line in f:
                array.append(line)
        array = [x.strip() for x in array]

        for x in range (0, (len(array))):
            print(' %s - %s \n' % (array[x],uid2guid(int(array[x]))))

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


import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np

class Model(nn.Module):
    """
    Normalization-Linear
    """
    def __init__(self, configs):
        super(Model, self).__init__()
        self.seq_len = configs.seq_len
        self.pred_len = configs.pred_len

        # Use this line if you want to visualize the weights
        # self.Linear.weight = nn.Parameter((1/self.seq_len)*torch.ones([self.pred_len,self.seq_len]))
        self.channels = configs.enc_in
        self.individual = configs.individual

        self.activations = {'relu': nn.ReLU(),
                    'softplus': nn.Softplus(),
                    'tanh': nn.Tanh(),
                    'selu': nn.SELU(),
                    'lrelu': nn.LeakyReLU(),
                    'prelu': nn.PReLU(),
                    'sigmoid': nn.Sigmoid()}

        if self.individual:
            self.mu = nn.ModuleList()
            for i in range(self.channels):
                self.mu.append(nn.Linear(self.seq_len,self.seq_len))
        else:
            self.mu=nn.Linear(self.seq_len,self.seq_len)

        if self.individual:
            self.var = nn.ModuleList()
            for i in range(self.channels):
                self.var.append(nn.Linear(self.seq_len,self.seq_len))
        else:
            hidden_layers = []
            hidden_layers.append(nn.Linear(self.seq_len, self.seq_len))
            hidden_layers.append(self.activations['selu'])
            hidden_layers.append(nn.InstanceNorm1d(num_features=self.seq_len))
            layers = hidden_layers
            self.var= nn.Sequential(*layers)

        if self.individual:
            self.Linear = nn.ModuleList()
            for i in range(self.channels):
                self.Linear.append(nn.Linear(self.seq_len,self.pred_len))
        else:

            self.Linear = nn.Linear(self.seq_len, self.pred_len)

        if self.individual:
            self.back = nn.ModuleList()
            for i in range(self.channels):
                self.Linear.append(nn.Linear(self.seq_len,self.seq_len))
        else:
            self.back = nn.Linear(self.seq_len, self.seq_len)

    def sample(self, mu, log_var):
        std = torch.exp(0.5*log_var)
        eps = torch.randn_like(std)
        return mu + eps*std

    def vae_loss(self,mu, logvar):
        KL_loss = -0.5 * (1 + logvar - mu.pow(2) - logvar.exp()).mean()
        return KL_loss

    def forward(self, x):
        # x: [Batch, Input length, Channel]
        seq_last = x[:,-1:,:].detach()
        x = x - seq_last

        if self.individual:
            mu = torch.zeros([x.size(0),self.seq_len,x.size(2)],dtype=x.dtype).to(x.device)
            log_var = torch.zeros([x.size(0),self.seq_len,x.size(2)],dtype=x.dtype).to(x.device)
            for i in range(self.channels):
                mu[:,:,i] = self.mu(x[:,:,i])
                log_var[:,:,i] = self.var(x[:,:,i])
            back = self.sample(mu, log_var)
            kl_divergence=self.vae_loss(mu,log_var)

        else:
            mu = self.mu(x.permute(0,2,1)).permute(0,2,1)
            log_var = self.var(x.permute(0,2,1)).permute(0,2,1)
            back = self.sample(mu, log_var)
            kl_divergence=self.vae_loss(mu,log_var)

            reproduce=self.back(back.permute(0,2,1)).permute(0,2,1)

        backward= reproduce+seq_last

        if self.individual:
            output = torch.zeros([x.size(0),self.pred_len,x.size(2)],dtype=x.dtype).to(x.device)
            for i in range(self.channels):
                output[:,:,i] = self.Linear[i](back[:,:,i])
        else:
            output = self.Linear(back.permute(0,2,1)).permute(0,2,1)
        output = output + seq_last

        return output,backward,kl_divergence # [Batch, Output length, Channel]


import csv

import networkx as nx
import pandas as pd

import nltk
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from langdetect import detect, DetectorFactory
import yake

# data are scraped from https://www.dimensions.ai/products/all-products/dimensions-free-version/
# Load the dataset
data = pd.read_csv("publications.csv", index_col="title")
data.index.name = None
data["title"] = data.index
print("size of the data:/n", data.shape)

# Preprocess the abstracts
nltk.download('stopwords')
nltk.download('wordnet')
nltk.download('punkt')
nltk.download('omw-1.4')

# Add domain-specific stopwords
additional_stopwords = ['however', 'important', 'also', 'understanding', 'one', 'many', 'several', 'within', 'among','study','area','new','tp','tibetan','plateau','sensitive','region','mountain','used']
stop_words = set(stopwords.words('english'))
stop_words.update(additional_stopwords)

# remove the abstracts that are empty
data = data[data['abstract'].notna()]

# Set the seed to ensure consistent language detection results
DetectorFactory.seed = 0

def is_english(text):
    try:
        return detect(text) == 'en'
    except:
        return False

# Filter out non-English abstracts
data = data[data['abstract'].apply(is_english)]

# Set a similarity threshold to determine which keywords are connected to which publications
similarity_threshold = 0.3

def yake_keywords(text, n_values=(1, 2, 3), top=5):
    all_keywords = []

    for n in n_values:
        kw_extractor = yake.KeywordExtractor(lan="en", n=n, top=top, features=None)
        ignored = stop_words
        keywords = kw_extractor.extract_keywords(text)
        unique_keywords = list(set(kw for kw, score in keywords if kw.lower() not in ignored))
        all_keywords.extend(unique_keywords)

    return all_keywords[:top]

# Create an empty graph
G = nx.Graph()

# Add keyword nodes to the graph
for abstract in data['abstract']:
    keywords = yake_keywords(abstract)
    for keyword in keywords:
        G.add_node(keyword)

# Create a TF-IDF vectorizer
vectorizer = TfidfVectorizer(stop_words=stop_words)
term_document_matrix = vectorizer.fit_transform(data['abstract'])

cosine_sim_matrix = cosine_similarity(term_document_matrix)

# Add publication nodes to the graph
for i in range(cosine_sim_matrix.shape[0]):
    G.add_node(i)

# Connect keyword nodes to publication nodes based on the similarity threshold
for i, abstract in enumerate(data['abstract']):
    keywords = yake_keywords(abstract)
    document_vector = term_document_matrix[i]

    for keyword in keywords[:5]:  # Extract top 5 keywords
        keyword_vector = vectorizer.transform([keyword])
        cosine_similarity_value = cosine_similarity(document_vector, keyword_vector)

        if cosine_similarity_value >= similarity_threshold:
            G.add_edge(i, keyword, weight=cosine_similarity_value)

# Compute the degree centrality for each keyword
degree_centrality = nx.degree_centrality(G)

keyword_impact = {}
for keyword in G.nodes():
    if isinstance(keyword, str):
        impact = 0
        for pub in G.neighbors(keyword):
            if isinstance(pub, int):
                impact += G.edges[keyword, pub]['weight']
        keyword_impact[keyword] = impact

# Extract the top 10 keywords
sorted_keywords = sorted(keyword_impact.items(), key=lambda x: -x[1])
top_10_keywords = [(keyword, len(list(G.neighbors(keyword))), impact) for keyword, impact in sorted_keywords[:10]]

# Print the top 10 keywords
print("Top 10 keywords:")
for keyword, impact in top_10_keywords:
    print(f"{keyword}: impact: {impact}")


# Name: Swap Adjacent in LR String
# Link: https://leetcode.com/problems/swap-adjacent-in-lr-string/
# Method: Check relative positions of L, R
# Time: O(n)
# Space: O(n)
# Difficulty: Medium

class Solution:
    def canTransform(self, start: str, end: str) -> bool:
        start_poz = [(poz, elem) for poz, elem in enumerate(start) if elem != "X"]
        end_poz = [(poz, elem) for poz, elem in enumerate(end) if elem != "X"]
        if len(start_poz) != len(end_poz) or len(start) != len(end):
            return False
        for start_tpl, end_tpl in zip(start_poz, end_poz):
            start_idx, start_ltr = start_tpl
            end_idx, end_ltr = end_tpl
            if start_ltr != end_ltr:
                return False
            if (start_ltr == "L" and start_idx < end_idx) or (
                start_ltr == "R" and start_idx > end_idx
            ):
                return False
        return True


#!/Users/pranab/Tools/anaconda/bin/python

# Package imports
import os
import sys
import numpy as np
import sklearn as sk
import random
import jprops
import abc
import math
import random
sys.path.append(os.path.abspath("../lib"))
from util import *

#base parameter search
class BaseParameterSearch(object):
	__metaclass__ = abc.ABCMeta

	def __init__(self, verbose):
		self.verbose = verbose
		self.parameters = []
		self.paramData = {}
		self.currentParams = []
		self.curIter = 0
		self.bestSolution = None

	# add param name and type
	def addParam(self, param):
		self.parameters.append(param)

	# add param data
	def addParamVaues(self, paramName, paramData):
		self.paramData[paramName] = paramData

	# max iterations
	def setMaxIter(self, maxIter):
		self.maxIter = maxIter

	@abc.abstractmethod
	def prepare(self):
		pass

	@abc.abstractmethod
	def nextParamValues(self):
		pass

	@abc.abstractmethod
	def setCost(self, cost):
		pass

	# get best solution
	def getBestSolution(self):
		return self.bestSolution

#enumerate through provided list of param values
class GuidedParameterSearch:
	def __init__(self, verbose=False):
		self.verbose = verbose
		self.parameters = []
		self.paramData = {}
		self.paramIndexes = []
		self.numParamValues = []
		self.currentParams = []
		self.bestSolution = None

	# max iterations
	def setMaxIter(self,maxIter):
		self.maxIter = maxIter

	# add param name and type
	def addParam(self, param):
		self.parameters.append(param)

	# add param data
	def addParamVaues(self, paramName, paramData):
		self.paramData[paramName] = paramData

	# prepare
	def prepare(self):
		self.numParams = len(self.parameters)
		for i in range(self.numParams):
			self.paramIndexes.append(0)

			#number of values for each parameter
			paramName = self.parameters[i][0]
			self.numParamValues.append(len(self.paramData[paramName]))
		self.curParamIndex = 0

		paramValueCombList = []
		paramValueComb = []
		paramValueCombList.append(paramValueComb)

		# all params
		for i in range(self.numParams):
			paramValueCombListTemp = []
			for paramValueComb in paramValueCombList:
				# all param values
				for j in range(self.numParamValues[i]):
					paramValueCombTemp = paramValueComb[:]
					paramValueCombTemp.append(j)
					paramValueCombListTemp.append(paramValueCombTemp)
			paramValueCombList = paramValueCombListTemp
		self.paramValueCombList = paramValueCombList
		self.numParamValueComb = len(self.paramValueCombList)
		self.curParamValueCombIndx = 0;

	# next param combination
	def nextParamValues(self):
		retParamNameValue = None
		if self.curParamValueCombIndx < len(self.paramValueCombList):
			retParamNameValue = []
			curParams = self.paramValueCombList[self.curParamValueCombIndx]
			print (curParams)
			for i in range(len(curParams)):
				paramName = self.parameters[i][0]
				paramValue = self.paramData[paramName][curParams[i]]
				retParamNameValue.append((paramName, paramValue))
			self.curParamValueCombIndx = self.curParamValueCombIndx + 1
			self.currentParams = retParamNameValue
		return 	retParamNameValue

	# set cost of current parameter set
	def setCost(self, cost):
		if self.bestSolution is not None:
			if cost < self.bestSolution[1]:
				self.bestSolution = (self.currentParams, cost)
		else:
			self.bestSolution = (self.currentParams, cost)

	# get best solution
	def getBestSolution(self):
		return 	self.bestSolution

#random search through provided list of parameter values
class RandomParameterSearch(BaseParameterSearch):
	def __init__(self, verbose=False):
		super(RandomParameterSearch, self).__init__(verbose)

	# prepare
	def prepare(self):
		pass

	# next param combination
	def nextParamValues(self):
		retParamNameValue = None
		if (self.curIter < self.maxIter):
			retParamNameValue = []
			for pName, pValues in self.paramData.iteritems():
				pValue = selectRandomFromList(pValues)
				retParamNameValue.append((pName, pValue))
			self.curIter = self.curIter + 1
			self.currentParams = retParamNameValue
		return retParamNameValue

	# set cost of current parameter set
	def setCost(self, cost):
		if self.bestSolution is not None:
			if cost < self.bestSolution[1]:
				self.bestSolution = (self.currentParams, cost)
		else:
			self.bestSolution = (self.currentParams, cost)

#random search through provided list of parameter values
class SimulatedAnnealingParameterSearch(BaseParameterSearch):
	def __init__(self, verbose=False):
		self.curSolution = None
		self.nextSolution = None
		super(SimulatedAnnealingParameterSearch, self).__init__(verbose)

	# prepare
	def prepare(self):
		pass

	def setTemp(self, temp):
		self.temp = temp

	def setTempReductionRate(self, tempRedRate):
		self.tempRedRate = tempRedRate

	# next param combination
	def nextParamValues(self):
		retParamNameValue = None
		if (self.curIter == 0):
			#initial random solution
			retParamNameValue = []
			for pName, pValues in self.paramData.iteritems():
				pValue = selectRandomFromList(pValues)
				retParamNameValue.append((pName, pValue))
			self.curIter = self.curIter + 1
			self.currentParams = retParamNameValue
		elif (self.curIter < self.maxIter):
			#perturb current solution
			retParamNameValue = []

			#randomly mutate one parameter value
			(pNameSel, pValue) = selectRandomFromList(self.currentParams)
			pValueNext = selectRandomFromList(self.paramData[pNameSel])
			while (pValueNext == pValue):
				pValueNext = selectRandomFromList(self.paramData[pNameSel])

			#copy
			for (pName, pValue) in self.currentParams:
				if (pName == pNameSel):
					pValueNew = pValueNext
				else:
					pValueNew = pValue
				retParamNameValue.append((pName, pValueNew))
			self.curIter = self.curIter + 1
			self.currentParams = retParamNameValue
		return retParamNameValue

	# set cost of current parameter set
	def setCost(self, cost):
		if self.curSolution is None:
			self.curSolution = (self.currentParams, cost)
			self.bestSolution = (self.currentParams, cost)
		else:
			self.nextSolution = (self.currentParams, cost)
			if (self.nextSolution[1] < self.curSolution[1]):
				if (self.verbose):
					print ("next soln better")
				self.curSolution = self.nextSolution
				if (self.nextSolution[1] < self.bestSolution[1]):
					if (self.verbose):
						print ("next soln better than best")
					self.bestSolution = self.nextSolution
			else:
				if (self.verbose):
					print ("next soln worst")
				pr = math.exp((self.curSolution[1] - self.nextSolution[1]) / self.temp)
				if (pr > random.random()):
					self.curSolution = self.nextSolution
					if (self.verbose):
						print ("next soln worst but accepted")
				else:
					if (self.verbose):
						print ("next soln worst and rejected")

			self.temp = self.temp * self.tempRedRate


"""
БИБЛИОТЕКА ДЛЯ РАБОТЫ С ФАЙЛАМИ

- создать папку
после выбора пользователь вводит название папки, создаем её в рабочей директории;
- удалить (файл/папку)
после выбора пользователь вводит название папки или файла, удаляем из рабочей директории если такой есть;
- копировать (файл/папку)
после выбора пользователь вводит название папки/файла и новое название папки/файла. Копируем;
- просмотр содержимого рабочей директории
вывод всех объектов в рабочей папке;
- посмотреть только папки
вывод только папок которые находятся в рабочей папке;
- посмотреть только файлы
вывод только файлов которые находятся в рабочей папке;
- просмотр информации об операционной системе
вывести информацию об операционной системе (можно использовать пример из 1-го урока);
- создатель программы
вывод информации о создателе программы;
- играть в викторину
запуск игры викторина из предыдущего дз;
- мой банковский счет
запуск программы для работы с банковским счетом из предыдущего дз
(задание учебное, после выхода из программы управлением счетом в главной программе
сумму и историю покупок можно не запоминать);
- смена рабочей директории (*необязательный пункт)
усложненное задание пользователь вводит полный /home/user/...
или относительный user/my/... путь. Меняем рабочую директорию на ту что ввели и работаем уже в ней;
- выход
выход из программы.
Так же можно добавить любой другой интересный или полезный функционал по своему желанию
После выполнения какого либо из пунктов снова возвращаемся в меню, пока пользователь не выберет выход
"""
import os
import shutil
import sys

def correct_file_name(name):
    """
    Функция более строгая, чем обычно принято
    Функция проверяет, может ли строка 'name' быть именем файла или папки
        в имени можно использовать маленькие латинские буквы, цифры, '_', '.' без пробелов
        Имя должно начинаться с буквы
        имя не заканчивается символом '.'
        общая длина имени - не более 16 символов

    :name: строка с проверяемым именем
    :return: name_is_correct - boolean - допустима ли переданная строка в качестве имени файла
    """

    # длина более 0 и не более 16 символов
    MAX_NAME_LEN = 16
    name_is_correct: bool = len(name) > 0 and len(name) <= MAX_NAME_LEN

    # первый символ - маленькая буква
    correct_1_symbols = 'abcdefghijklmnopqrstuvwxyz'
    name_is_correct = name_is_correct and (name[0] in correct_1_symbols)

    # последний символ - не точка
    name_is_correct = name_is_correct and (name[-1] != '.')

    # все символы - допустимые
    correct_symbols = correct_1_symbols + '._123456789'
    name_is_correct = name_is_correct and (all(item in correct_symbols for item in name))

    return name_is_correct

def my_create_folder(folder_name=''):
    """
    создать папку
    если не передается имя папки, то пользователь вводит название папки,
    создаем папку в рабочей директории;
    :folder_name:  имя папки, необязательный.
    Если задан, то папка создается с переданным именем,
    если не задан, то осуществляется запрос ввода с клавиатуры
    :return: success - получилось ли создать папку
    """

    success = False

    # если имя не передано в функцию, запрашиваем ввод пользователя
    if len(folder_name) == 0:
        print('Текущая папка:', os.getcwd())
        folder_name = input('Введите имя новой папки: ')

    # папка создается, если имя корректно и если папки c таким именем еще нет

    if correct_file_name(folder_name):
        if not (os.path.isdir(folder_name)):
            os.mkdir(folder_name)
            success = True
        else:
            print('такая папка \'', folder_name, '\' уже существует.')
    else:
        print('Некорректное имя папки: \'', folder_name, '\'')

    return success

def my_list_dir(path_name=''):
    """
    просмотр содержимого папки path_name
    если не передается имя папки, то возвращается содержимое текущей папки
    вывод всех объектов в рабочей папке
    :param path_name: имя папки, необязательный.
    :return: folders_list - список имен папок и файлов в папке
    """

    # если путь не передан, то возвращаем содержимое текущей папки
    if len(path_name) == 0:
        path_name = os.getcwd()
    return os.listdir(path_name)

def my_list_only_folders(path_name=''):
    """
    посмотреть только папки
   если не передается имя папки, то возвращается содержимое текущей папки
    :param path_name: имя папки, необязательный.
    :return: only_folders_list - список имен папок
    """

    # если путь не передан, то смотрим содержимое текущей папки
    if len(path_name) == 0:
        path_name = os.getcwd()

    total_f_list = os.listdir(path_name)

    only_folders_list = list(filter(lambda f: True if os.path.isdir(f) else False, total_f_list))

    return only_folders_list

def my_list_only_files(path_name=''):
    """
    посмотреть только  файлы
    если не передается имя папки, то возвращается содержимое текущей папки
    :param path_name: имя папки, необязательный.
    :return: only_files_list - список имен папок
    """

    # если путь не передан, то смотрим содержимое текущей папки
    if len(path_name) == 0:
        path_name = os.getcwd()

    total_f_list = os.listdir(path_name)

    only_files_list = list(filter(lambda f: True if not os.path.isdir(f) else False, total_f_list))

    return only_files_list

def my_copy(src_name='', dist_name=''):
    """
    копировать (файл/папку)
    если хотя бы одна из переменных - имён не передается, то
    пользователь вводит и название папки/файла - источника,
    и новое название папки/файла, в который копируется.
    В случае, если источником является не файл, а папка,
    то создаем просто пустую папку с именем dist_name
    Копируем;
    :param src_name: файл/папка источник
    :param dist_name: файл/папка, куда копируется
    :return: success - boolean - успех копирования
    """

    success = False

    # Если хотя бы один из файлов не задан, запрашиваем оба файла/папки:
    if len(src_name) == 0 or len(dist_name) == 0:
        print()
        print(my_list_dir())     # выводим содержимое текущей папки
        src_name = input('Введите имя исходного файла/папки: ')
        dist_name = input('Введите имя файла/папки, в который копируем: ')

    # Если источник копирования - папка,
    # То просто создаем копию - пустую папку
    if os.path.isdir(src_name):
        my_create_folder(dist_name)  # требования к имени жёстче обычных
        print('Создана пустая папка', dist_name)
        print(my_list_dir())
        success = True
    else:
        # источник - файл
        try:
            shutil.copy(src_name, dist_name)
            print('Копирование завершено')
            print(my_list_dir())
            success = True
        except:
            print('Возникла ошибка. Проверьте корректность введенных имен')

    return success

def my_delete(name=''):
    """
    удалить (файл/папку)
    если переменная не передается, то
    пользователь вводит название папки/файла
    :param name: файл/папка для удаления
    :return: success - boolean - успех удаления
    """

    success = False

    # Если имя файла не задано, запрашиваем
    if len(name) == 0 :
        print()
        print(my_list_dir())     # выводим содержимое текущей папки
        name = input('Введите имя файла/папки для удаления: ')

    # Если удаляемый объект - папка,
    # то удаляем папку вместе с содержимым
    if os.path.isdir(name):
        try:
            shutil.rmtree(name)
            print('Выполнено удаление.')
            print(my_list_dir())
            success = True
        except:
            print('Возникла ошибка. Проверьте корректность введенных имен')
    # и если удаляемый объект -файл, то удаляем файл
    elif os.path.isfile(name):
        try:
            os.remove(name)
            print('Выполнено удаление.')
            print(my_list_dir())
            success = True
        except:
            print('Возникла ошибка. Проверьте корректность введенных имен')

    return success

def my_os_info():
    """
    просмотр информации об операционной системе
    вывести информацию об операционной системе (можно использовать пример из 1-го урока);
    :return: info - текстовая строка
    """
    info = f'My OS is {sys.platform} ({os.name})'
    return info

def my_get_username():
    """
    возвращает строку с именем пользователя
    :return:
    """
    # получаем путь до папки пользователя
    path = os.path.expanduser('~')
    # очищаем имя пользователя от пути
    slash = path.rfind('/')
    info = path[slash+1:]

    return info

def my_chdir(path=''):
    """
    смена рабочей директории
    пользователь вводит полный /home/user/...
    или относительный user/my/... путь.
    Меняем рабочую директорию на ту что ввели и работаем уже в ней;
    :return: success - boolean - успех смены рабочей директории
    """

    success = False

    # если в функцию ничего не передано, запрашиваем ввод пользователя
    if len(path) == 0:
        print(os.getcwd())
        print(my_list_dir())
        path = input('Введите адрес папки для перехода: ')

    # если передан полный путь без ошибок,
    #     переходим в запрошенную папку
    if os.path.exists(path):
        os.chdir(path)
        print(os.getcwd())
        success = True
    else:
        # надеемся, что передан локальный путь
        try:
            os.path.join(os.getcwd(), path)
            os.chdir(path)
            print(os.getcwd())
            success = True
        except:
            print('Возникла ошибка. Проверьте корректность введенного пути')

    return success

def good_buy():

    print('*'*15, 'До новых встреч', '*'*15)

    return '*'*47

# good_buy()

"""
Тестирование функции correct_file_name():
    # test_names = ['america', 'readme.txt', 'Queen', '1asdf', 'qwertyuiopasdfghj', 'index.dat']
    # print('Тестирование функции correct_file_name():')
    # for str in test_names:
    #     print(str, ':', correct_file_name(str))
-------------
Результат работы:
-------------
Тестирование функции correct_file_name():
america : True
readme.txt : True
Queen : False
1asdf : False
qwertyuiopasdfghj : False
index.dat : True

Тестирование функции my_create_folder()
    # while True:
    #     my_create_folder()

-------------
Результат работы:
-------------
папки создаются, неверные вводы обрабатываются корректно

Тестирование функции my_list_dir()

    # print(my_list_dir())
-------------
Результат работы:
-------------
['LICENSE', 'console_lib.py', '.gitignore', 'console_file_manager.py', '.git', '.idea']

    # print(my_list_dir('/Users/nikolayagafonov/Music'))
-------------
Результат работы:
-------------
['Audio Music Apps', 'Music', '.DS_Store', '.localized', 'Ableton', 'iTunes', 'GarageBand']

Проверка функции my_list_only_folders()
     # print(my_list_only_folders())
-------------
Результат работы:
-------------
['Test1', 'Test2', '.git', '.idea']

Проверка функции my_list_only_folders()
     # print(my_list_only_folders())
-------------
Результат работы:
-------------
['LICENSE', 'console_lib.py', '.gitignore', 'console_file_manager.py']

Проверка функции my_copy() для копирования папок:
    # my_copy()
-------------
Результат работы:
--------------
/для существующих имен/

['LICENSE', 'console_lib.py', 'test', 'book.txt', '.gitignore', 'console_file_manager.py', '.git', '.idea']
Введите имя исходного файла/папки: test
Введите имя файла/папки, в который копируем: test1
Создана пустая папка test1
['LICENSE', 'console_lib.py', 'test', 'book.txt', 'test1', '.gitignore', 'console_file_manager.py', '.git', '.idea']

-------------
/для несуществующих имен/
['LICENSE', 'console_lib.py', 'test', 'book.txt', 'test1', '.gitignore', 'console_file_manager.py', '.git', '.idea']
Введите имя исходного файла/папки: Папка
Введите имя файла/папки, в который копируем: тест2
Возникла ошибка. Проверьте корректность введенных имен

-------------
/копирование файла в папку/
['LICENSE', 'console_lib.py', 'test', 'book.txt', 'test1', '.gitignore', 'console_file_manager.py', '.git', '.idea']
Введите имя исходного файла/папки: book.txt
Введите имя файла/папки, в который копируем: test
Копирование завершено
['LICENSE', 'console_lib.py', 'test', 'book.txt', 'test1', '.gitignore', 'console_file_manager.py', '.git', '.idea']
-------------
/копирование файла в файл/
['LICENSE', 'console_lib.py', 'test', 'book.txt', 'test1', '.gitignore', 'console_file_manager.py', '.git', '.idea']
Введите имя исходного файла/папки: book.txt
Введите имя файла/папки, в который копируем: fiction.txt
Копирование завершено
['LICENSE', 'console_lib.py', 'test', 'fiction.txt', 'book.txt', 'test1', '.gitignore', 'console_file_manager.py', '.git', '.idea']

Проверка функции my_delete() для удаления файлов и папок
-------------
Результат работы:
--------------
/удаление папки c содержимым/
['LICENSE', 'console_lib.py', 'book.txt', 'Test1', 'bank.py', '.gitignore', 'victory.py', 'readme.txt', 'Test2', '.git', 'main.py', '.idea']
Введите имя файла/папки для удаления: Test2
Выполнено удаление.
['LICENSE', 'console_lib.py', 'book.txt', 'Test1', 'bank.py', '.gitignore', 'victory.py', 'readme.txt', '.git', 'main.py', '.idea']
--------------
/удаление файла/
['LICENSE', 'console_lib.py', 'book.txt', 'Test1', 'bank.py', 'rrr.txt', '.gitignore', 'victory.py', 'readme.txt', 'Test2', '.git', 'main.py', '.idea']
Введите имя файла/папки для удаления: rrr.txt
Выполнено удаление.
['LICENSE', 'console_lib.py', 'book.txt', 'Test1', 'bank.py', '.gitignore', 'victory.py', 'readme.txt', 'Test2', '.git', 'main.py', '.idea']

Проверка функции my_os_info()
     # print(my_os_info())
-------------
Результат работы:
-------------
My OS is darwin (posix)

Проверка функции my_get_username()
     # print(os.path.expanduser('~'))
     # print(my_get_username())
-------------
Результат работы:
-------------
/Users/nikolayagafonov
nikolayagafonov

Проверка функции my_chdir()
    # my_chdir()
 -------------
Результат работы:
-------------
 переход по абсолютному адресу:
/Users/nikolayagafonov/Desktop/Python progs/HomeWork_5_1
['LICENSE', 'console_lib.py', 'Test1', '.gitignore', 'console_file_manager.py', 'Test2', '.git', '.idea']
Введите адрес папки для перехода: /Users/nikolayagafonov/Music
/Users/nikolayagafonov/Music

 переход по относительному адресу:
/Users/nikolayagafonov/Desktop/Python progs/HomeWork_5_1
['LICENSE', 'console_lib.py', 'Test1', '.gitignore', 'console_file_manager.py', 'Test2', '.git', '.idea']
Введите адрес папки для перехода: Test2
/Users/nikolayagafonov/Desktop/Python progs/HomeWork_5_1/Test2

переход по ошибочному адресу:
/Users/nikolayagafonov/Desktop/Python progs/HomeWork_5_1
['LICENSE', 'console_lib.py', 'Test1', '.gitignore', 'console_file_manager.py', 'Test2', '.git', '.idea']
Введите адрес папки для перехода: ttt
Возникла ошибка. Проверьте корректность введенного пути
"""


#pip install bacdive
import bacdive
client = bacdive.BacdiveClient('17326099485@189.cn', 'M032711m')

# the search method fetches all BacDive-IDs matching your query
# and returns the number of IDs found
count = client.search(taxonomy='Blautia')
print(count, 'strains found.')

# the retrieve method lets you iterate over all strains
# and returns the full entry as dict
# Entries can be further filtered using a list of keys (e.g. ['keywords'])
for strain in client.retrieve():
    print(strain)


from ast import alias
import os, json

dir = "/home/xelpmoc/Documents/Mailer/whatsapp"

files = os.listdir(dir)

def addAlias():
    for filename in files:
        if filename.endswith("json"):
            f = open(filename)
            print(filename)

            data = json.load(f)

            fields = data["data"]

            i = 1
            for field in fields:
                _alias =  str(field["page"]) + "_" +   str(i)
                field["alias"] = _alias

                i += 1
            f.close()

            f = open(filename, "w")
            f.write(str({"data" : fields}))
            f.close()

def testjson():
    for filename in files:
        if filename.endswith("json"):
            try:
                f = open(filename)
                data = json.load(f)
                f.close()
                if data != None:
                    print("OK:", filename)
            except:
                print("error:"  ,filename )

    print("all files tested")

testjson()