import operator
import os
import shutil
from multiprocessing import cpu_count
from time import time

import click
from tabulate import tabulate

from platformio import app, exception, fs, util
from platformio.device.monitor.command import device_monitor_cmd
from platformio.project.config import ProjectConfig
from platformio.project.exception import ProjectError
from platformio.project.helpers import find_project_dir_above, load_build_metadata
from platformio.run.helpers import clean_build_dir
from platformio.run.processor import EnvironmentProcessor
from platformio.test.runners.base import CTX_META_TEST_IS_RUNNING

# pylint: disable=too-many-arguments,too-many-locals,too-many-branches

try:
    DEFAULT_JOB_NUMS = cpu_count()
except NotImplementedError:
    DEFAULT_JOB_NUMS = 1

@click.command("run", short_help="Run project targets (build, upload, clean, etc.)")
@click.option("-e", "--environment", multiple=True)
@click.option("-t", "--target", multiple=True)
@click.option("--upload-port")
@click.option("--monitor-port")
@click.option(
    "-d",
    "--project-dir",
    default=os.getcwd,
    type=click.Path(exists=True, file_okay=True, dir_okay=True, writable=True),
)
@click.option(
    "-c",
    "--project-conf",
    type=click.Path(exists=True, file_okay=True, dir_okay=False, readable=True),
)
@click.option(
    "-j",
    "--jobs",
    type=int,
    default=DEFAULT_JOB_NUMS,
    help=(
        "Allow N jobs at once. "
        "Default is a number of CPUs in a system (N=%d)" % DEFAULT_JOB_NUMS
    ),
)
@click.option(
    "-a",
    "--program-arg",
    "program_args",
    multiple=True,
    help="A program argument (multiple are allowed)",
)
@click.option("--disable-auto-clean", is_flag=True)
@click.option("--list-targets", is_flag=True)
@click.option("-s", "--silent", is_flag=True)
@click.option("-v", "--verbose", is_flag=True)
@click.pass_context
def cli(
    ctx,
    environment,
    target,
    upload_port,
    monitor_port,
    project_dir,
    project_conf,
    jobs,
    program_args,
    disable_auto_clean,
    list_targets,
    silent,
    verbose,
):
    app.set_session_var("custom_project_conf", project_conf)

    # find project directory on upper level
    if os.path.isfile(project_dir):
        project_dir = find_project_dir_above(project_dir)

    targets = list(target) if target else []
    del target
    only_monitor = targets == ["monitor"]
    is_test_running = CTX_META_TEST_IS_RUNNING in ctx.meta
    command_failed = False

    with fs.cd(project_dir):
        config = ProjectConfig.get_instance(project_conf)
        config.validate(environment)

        if list_targets:
            return print_target_list(list(environment) or config.envs())

        # clean obsolete build dir
        if not only_monitor and not disable_auto_clean:
            build_dir = config.get("platformio", "build_dir")
            try:
                clean_build_dir(build_dir, config)
            except ProjectError as exc:
                raise exc
            except:  # pylint: disable=bare-except
                click.secho(
                    "Can not remove temporary directory `%s`. Please remove "
                    "it manually to avoid build issues" % build_dir,
                    fg="yellow",
                )

        default_envs = config.default_envs()
        results = []
        for env in config.envs():
            skipenv = any(
                [
                    environment and env not in environment,
                    not environment and default_envs and env not in default_envs,
                ]
            )
            if skipenv:
                results.append({"env": env})
                continue

            # print empty line between multi environment project
            if not silent and any(r.get("succeeded") is not None for r in results):
                click.echo()

            results.append(
                process_env(
                    ctx,
                    env,
                    config,
                    targets,
                    upload_port,
                    monitor_port,
                    jobs,
                    program_args,
                    is_test_running,
                    silent,
                    verbose,
                )
            )
        command_failed = any(r.get("succeeded") is False for r in results)
        if (
            not is_test_running
            and not only_monitor
            and (command_failed or not silent)
            and len(results) > 1
        ):
            print_processing_summary(results, verbose)

    # Reset custom project config
    app.set_session_var("custom_project_conf", None)

    if command_failed:
        raise exception.ReturnErrorCode(1)

    return True

def process_env(
    ctx,
    name,
    config,
    targets,
    upload_port,
    monitor_port,
    jobs,
    program_args,
    is_test_running,
    silent,
    verbose,
):
    if not is_test_running and not silent:
        print_processing_header(name, config, verbose)

    targets = targets or config.get(f"env:{name}", "targets", [])
    only_monitor = targets == ["monitor"]
    result = {"env": name, "duration": time(), "succeeded": True}

    if not only_monitor:
        result["succeeded"] = EnvironmentProcessor(
            ctx,
            name,
            config,
            [t for t in targets if t != "monitor"],
            upload_port,
            jobs,
            program_args,
            silent,
            verbose,
        ).process()

    if result["succeeded"] and "monitor" in targets and "nobuild" not in targets:
        ctx.invoke(
            device_monitor_cmd,
            port=monitor_port,
            environment=name,
        )

    result["duration"] = time() - result["duration"]

    # print footer on error or when is not unit testing
    if (
        not is_test_running
        and not only_monitor
        and (not silent or not result["succeeded"])
    ):
        print_processing_footer(result)

    return result

def print_processing_header(env, config, verbose=False):
    env_dump = []
    for k, v in config.items(env=env):
        if verbose or k in ("platform", "framework", "board"):
            env_dump.append("%s: %s" % (k, ", ".join(v) if isinstance(v, list) else v))
    click.echo(
        "Processing %s (%s)"
        % (click.style(env, fg="cyan", bold=True), "; ".join(env_dump))
    )
    terminal_width = shutil.get_terminal_size().columns
    click.secho("-" * terminal_width, bold=True)

def print_processing_footer(result):
    is_failed = not result.get("succeeded")
    util.print_labeled_bar(
        "[%s] Took %.2f seconds"
        % (
            (
                click.style("FAILED", fg="red", bold=True)
                if is_failed
                else click.style("SUCCESS", fg="green", bold=True)
            ),
            result["duration"],
        ),
        is_error=is_failed,
    )

def print_processing_summary(results, verbose=False):
    tabular_data = []
    succeeded_nums = 0
    failed_nums = 0
    duration = 0

    for result in results:
        duration += result.get("duration", 0)
        if result.get("succeeded") is False:
            failed_nums += 1
            status_str = click.style("FAILED", fg="red")
        elif result.get("succeeded") is None:
            if not verbose:
                continue
            status_str = "IGNORED"
        else:
            succeeded_nums += 1
            status_str = click.style("SUCCESS", fg="green")

        tabular_data.append(
            (
                click.style(result["env"], fg="cyan"),
                status_str,
                util.humanize_duration_time(result.get("duration")),
            )
        )

    click.echo()
    click.echo(
        tabulate(
            tabular_data,
            headers=[
                click.style(s, bold=True) for s in ("Environment", "Status", "Duration")
            ],
        ),
        err=failed_nums,
    )

    util.print_labeled_bar(
        "%s%d succeeded in %s"
        % (
            "%d failed, " % failed_nums if failed_nums else "",
            succeeded_nums,
            util.humanize_duration_time(duration),
        ),
        is_error=failed_nums,
        fg="red" if failed_nums else "green",
    )

def print_target_list(envs):
    tabular_data = []
    for env, data in load_build_metadata(os.getcwd(), envs).items():
        tabular_data.extend(
            sorted(
                [
                    (
                        click.style(env, fg="cyan"),
                        t["group"],
                        click.style(t.get("name"), fg="yellow"),
                        t["title"],
                        t.get("description"),
                    )
                    for t in data.get("targets", [])
                ],
                key=operator.itemgetter(1, 2),
            )
        )
        tabular_data.append((None, None, None, None, None))
    click.echo(
        tabulate(
            tabular_data,
            headers=[
                click.style(s, bold=True)
                for s in ("Environment", "Group", "Name", "Title", "Description")
            ],
        ),
    )


from concurrent.futures import ProcessPoolExecutor, as_completed
from copy import deepcopy
from dataclasses import dataclass
from typing import List

import gym
import numpy as np
import numpy.typing as npt
from omegaconf import DictConfig
from tqdm import trange

from agent import Agent
from core import Layer, LinearLayer, Model, relu

@dataclass()
class Candidate:
    model: Model
    fitness: float

@dataclass()
class EvolutionalAgentConfig:
    generations: int
    children_per_parent: int
    mutation_strength: float
    num_parents: int
    nodes: List[int]

    @staticmethod
    def fromDictConfig(config: DictConfig) -> "EvolutionalAgentConfig":
        return EvolutionalAgentConfig(
            config.generations,
            config.children_per_parent,
            config.mutation_strength,
            config.num_parents,
            config.nodes,
        )

class EvolutionalAgent(Agent):
    def __init__(self, config: EvolutionalAgentConfig) -> None:
        self.config = config
        self._policy_model = self._create_policy_model()

    def action(self, state: npt.NDArray[np.float32]) -> npt.NDArray[np.float32]:
        return self._policy_model(state)

    def train(self, env: gym.Env) -> List[float]:
        executor = ProcessPoolExecutor()
        rewards = []
        parents = [
            EvolutionalAgent._create_candidate(env, self._create_policy_model())
            for _ in range(self.config.num_parents)
        ]
        with trange(0, self.config.generations) as tgen:
            for generation in tgen:
                env.reset()
                best_parent_fitness = EvolutionalAgent._best_candidate(parents).fitness
                rewards.append(best_parent_fitness)
                tgen.set_postfix(
                    generation=generation,
                    fitness=best_parent_fitness,
                )

                candidates_f = [
                    executor.submit(
                        EvolutionalAgent._create_candidate,
                        env,
                        parent.model,
                        self.config.mutation_strength,
                    )
                    for parent in parents
                    for _ in range(self.config.children_per_parent)
                ]
                candidates_f.extend(
                    [
                        executor.submit(
                            EvolutionalAgent._create_candidate,
                            env,
                            parent.model,
                        )
                        for parent in parents
                    ]
                )
                candidates = [
                    candidate.result() for candidate in as_completed(candidates_f)
                ]
                candidates.sort(key=lambda x: x.fitness, reverse=True)
                parents = candidates[0 : self.config.num_parents]

        self._policy_model = EvolutionalAgent._best_candidate(parents).model
        executor.shutdown()
        return rewards

    @staticmethod
    def _create_candidate(
        env: gym.Env, parent: Model, mutation_strength: float = 0.0
    ) -> Candidate:
        env_c = deepcopy(env)
        child = deepcopy(parent)
        for layer in child._layers:
            layer.W += np.random.normal(scale=mutation_strength, size=layer.W.shape)
            layer.b += np.random.normal(scale=mutation_strength, size=layer.b.shape)
        fitness = EvolutionalAgent._play(env_c, child)
        return Candidate(child, fitness)

    @staticmethod
    def _play(env: gym.Env, policy_model: Layer) -> float:
        state, done, sum_reward = env.reset(), False, 0
        while not done:
            action = policy_model(state)
            new_state, reward, done, _ = env.step(action)
            sum_reward += reward
            state = new_state

        return sum_reward

    def _create_policy_model(self) -> Model:
        nodes = self.config.nodes

        layers = []
        layers.append(LinearLayer(24, nodes[0], activation=relu))

        for id in range(0, len(self.config.nodes) - 1):
            layers.append(LinearLayer(nodes[id], nodes[id + 1], activation=relu))

        layers.append(LinearLayer(nodes[-1], 4, activation=np.tanh))

        return Model(*layers)

    def _best_candidate(candidates: List[Candidate]) -> Candidate:
        id = np.argmax([candidate.fitness for candidate in candidates])
        return candidates[id]


# import pandas as pd
import expressyeaself.encode_sequences as encode
import numpy as np
from keras import optimizers
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout, LSTM
# from keras.layers import Embedding
# from keras.utils import to_categorical
# from keras.datasets import mnist
# from sklearn.preprocessing import OneHotEncoder
import matplotlib.pyplot as plt

# encode = context.encode_sequences
# construct = context.construct_neural_net_input

# def load_raw_data(data):
#     raw_data = pd.read_csv(data, names=['raw_sequence','output'], sep='\t')
#     return raw_data

# def load_raw_sequence_data(raw_matrix):
#     """
#     """
#     sequence_matrix=[]
#     for sequence in raw_matrix['raw_sequence']:
#         one_hot_array_sequence=np.array(ast.literal_eval(sequence))
#         sequence_matrix.append(one_hot_array_sequence)
#     sequence_matrix=np.array(sequence_matrix)
#     return sequence_matrix

def read_length(encoded_sequences):
    """
    this function helps to read the sequence length and the data amount
    """
    data_length = int(encoded_sequences.shape[0])
    sequence_length = int(encoded_sequences.shape[1])
    return data_length, sequence_length

# def matrix_reshape_to_array(matrix, data_amount, sequence_length):
#     """
#     this function helps to reshape the sequences matric
#     """
#     sequence_array=matrix.reshape(data_amount, sequence_length, 5)
#     return sequence_array

# def input_sequence_to_embedding(sequence_matrix, sequence_length):
#     sequence_matrix_for_enbedding=[]
#     for matrix in sequence_matrix:
#         matrix=matrix.reshape(1,sequence_length,5)
#         sequence_matrix_for_enbedding.append(matrix)
#     sequence_matrix_for_enbedding=np.array(sequence_matrix_for_enbedding)
#     return sequence_matrix_for_enbedding

def split_data(input_data, output_data):
    """
    the split function
    """
    training_sequence = input_data[:int(input_data.shape[0] * 0.8)]
    training_output = output_data[:int(output_data.shape[0] * 0.8)]
    testing_sequence = input_data[int(input_data.shape[0] * 0.8):]
    testing_output = output_data[int(output_data.shape[0] * 0.8):]
    return training_sequence, training_output, testing_sequence, testing_output

# def load_output(raw_matrix, data_length):
#     output_matrix=np.array(raw_matrix['output']).reshape(data_length, 1, 1)
#     return output_matrix

def scale_output(raw_data, data_length):
    """
    the function to scale the output
    """
#     output_data=[]
#     max_value=raw_data['output'].max()
#     for output in raw_data['output']:
#         output=output/max_value
#         output_data.append(output)
    output_data = np.array(raw_data).reshape(data_length, 1, 1)
    return output_data

def input_of_one_hot_sequence(raw_matrix, data_amount, sequence_length):
    """
    the function to reshape the matrix to the input
    format of the lstm
    """
    sequence_matrix = raw_matrix.reshape(data_amount, -1)
    one_hot_sequence_matrix = sequence_matrix.reshape(
        data_amount, 1, sequence_length * 5)
    return one_hot_sequence_matrix

def lstm_model_on_one_hot_sequence(
        sequence_length,
        training_sequence,
        training_output,
        testing_sequence,
        testing_output,
        self_loss='mean_squared_error',
        learning_rate=0.01,
        epochs_value=20,
        batch_size_value=50):
    """
    the lstm model function
    """
    model = Sequential()
    # model.add(Embedding(5, 1, input_length=1285))
    model.add(Dropout(0.8))
    model.add(
        Dense(
            1024,
            kernel_initializer='uniform',
            input_shape=(
                1,
                5 *
                sequence_length,
            )))
    model.add(Activation('softmax'))
    model.add(Dense(512, kernel_initializer='uniform', input_shape=(1, 1024,)))
    model.add(Activation('softmax'))
    model.add(Dense(256, kernel_initializer='uniform', input_shape=(1, 512,)))
    model.add(Activation('softmax'))
    model.add(Dense(128, kernel_initializer='uniform', input_shape=(1, 256,)))
    model.add(Activation('softmax'))
    model.add(Dense(64, kernel_initializer='uniform', input_shape=(1, 128,)))
    model.add(Activation('softmax'))
    model.add(Dense(32, kernel_initializer='uniform', input_shape=(1, 64,)))
    model.add(Activation('softmax'))
    model.add(Dense(16, kernel_initializer='uniform', input_shape=(1, 32,)))
    model.add(Activation('softmax'))
    model.add(Dense(8, kernel_initializer='uniform', input_shape=(1, 16,)))
    model.add(Activation('softmax'))

    model.add(LSTM(units=1, return_sequences=True))
    sgd = optimizers.SGD(
        lr=learning_rate,
        decay=1e-6,
        momentum=0.9,
        nesterov=True)
    model.compile(loss=self_loss, optimizer=sgd, metrics=['mae', 'acc'])

    model.fit(
        training_sequence,
        training_output,
        epochs=epochs_value,
        batch_size=batch_size_value,
        verbose=1)
    score = model.evaluate(testing_sequence, testing_output)
    predicted_output_raw = model.predict(testing_sequence)
    predicted_output = predicted_output_raw.reshape(
        len(predicted_output_raw), 1)

    return score, predicted_output

# def LSTM_model_training(
#         LSTM_model,
#         training_sequence,
#         training_output,
#         epochs_value=20,
#         batch_size_value=50):

# return LSTM_model.fit(
#     training_sequence,
#     training_output,
#     epochs=epochs_value,
#     batch_size=batch_size_value,
#     verbose=1)

# def model_report(
#         LSTM_model,
#         testing_sequence,
#         testing_output,
#         batch_size_value=50):
#     #     LSTM_model.summary()
#     score = model.evaluate(testing_sequence, testing_output)
#     return score

# def predict_data(LSTM_model, testing_sequence):
#     predicted_output_raw = LSTM_model.predict(testing_sequence)
#     predicted_output = predicted_output_raw.reshape(
#         len(predicted_output_raw), 1)
#     return predicted_output

def caparison_figure(predict_data, testing_output):
    """
    the function to plot the comparison figures
    """
    testing_output_array = testing_output.reshape(len(testing_output), 1)
    index = np.arange(1, len(testing_output) + 1, 1)
    fig = plt.figure(figsize=(9, 6))
    plt.scatter(index, testing_output_array, label='real output')
    plt.scatter(index, predict_data, label='predict output')
    plt.legend()
    return fig

def lstm_training(
        data_path,
        self_loss='mean_squared_error',
        learning_rate=0.01,
        epochs_value=2,
        batch_size_value=50):
    """
    the wrapping function
    """
    sequence_list, raw_output, max_value = encode.encode_sequences_with_method(
        data_path, 'One-Hot')
    data_number, bp_number = read_length(sequence_list)
    reshaped_sequence_matrix = input_of_one_hot_sequence(
        sequence_list, data_number, bp_number)
    output = scale_output(raw_output, data_number)
    train_x, train_y, test_x, text_y = split_data(
        reshaped_sequence_matrix, output)
    score, predict_data = lstm_model_on_one_hot_sequence(
        bp_number, train_x, train_y, test_x, text_y,
        self_loss=self_loss, learning_rate=learning_rate,
        epochs_value=epochs_value, batch_size_value=batch_size_value)
#     LSTM_model_training(LSTM_model, train_x, train_y,
#         epochs_value=2, batch_size_value=50)
#     socre=model_report(LSTM_model, testing_sequence,
#         testing_output, batch_size_value=50)
#     predicted_output=predict_data(LSTM_model, test_x)
    cp_figure = caparison_figure(predict_data, text_y)
    return score, cp_figure


import re
from zope.interface import Interface
from twisted.python import log
from nevow import rend, tags as T, loaders

class IPastDate(Interface):
    """Remember a past date for time travel"""
    pass

class PastConnectionPool:
    """Proxy for an existing connection pool to run queries in the past.

    This proxy intercepts runQueryInPast and modifies the request to
    make it happen in the past (if necessary). It only accepts simple
    queries (query + dict) and needs a web context (to extract the
    date).
    """

    _regexp_deleted = re.compile(r"(?:(\w+)\.|)deleted='infinity'")
    _regexp_full = re.compile(r"\B_full\b")

    def __init__(self, orig):
        self._orig = orig

    def __getattr__(self, attribute):
        return getattr(self._orig, attribute)

    def runQueryInPast(self, ctx, query, dic=None):
        """Run the specified query in the past.

        Occurences of C{deleted='infinity'} are replaced by C{(created
        < %(__date)s AND deleted > %(__date)s)}
        """

        def convert(date, mo):
            if mo.group(1):
                suffix = "%s." % mo.group(1)
            else:
                suffix = ""
            return " AND ".join(['(%screated < %%(__date)s::abstime' % suffix,
                                 '%sdeleted > %%(__date)s::abstime)' % suffix])

        # Try to get the date from the context
        try:
            date = ctx.locate(IPastDate)
        except KeyError:
            # Not in the past
            query = PastConnectionPool._regexp_full.sub("", query)
            if dic:
                return self._orig.runQuery(query, dic)
            else:
                return self._orig.runQuery(query)

        # We need to run this request in the past
        if not dic:
            dic = {}
        dic["__date"] = date
        q = PastConnectionPool._regexp_deleted.sub(
            lambda x: convert(date, x), query)
        return self._orig.runQuery(q, dic)

class PastResource(rend.Page):
    """This is a special resource that needs to be instanciated with
    another resource. This resource will register the date into the
    current context and use the given resource to handle the request.
    """

    addSlash = True
    docFactory = loaders.stan(T.html [ T.body [ T.p [ "Nothing here" ] ] ])

    def __init__(self, main):
        self.main = main
        rend.Page.__init__(self)

    def dateOk(self, ctx, date):
        # The given date is correct, insert it in the context
        ctx.remember(date, IPastDate)
        return self.main

    def badDate(self, ctx, date):
        log.msg("Got bad date: %r" % date)
        return self.main

    def childFactory(self, ctx, date):
        # We must validate the date (use runOperation to avoid proxy)
        d = self.main.dbpool.runOperation("SELECT %(date)s::abstime",
                                          {'date': date})
        d.addCallbacks(lambda x: self.dateOk(ctx, date),
                       lambda x: self.badDate(ctx, date))
        return d


 #!/usr/bin/env python3
import urllib.request
from bs4 import BeautifulSoup
import re
import csv
#test
DOMAIN = "http://posuda-mix.ru"
CATEGORY_URL = "https://www.klenmarket.ru/shop/inventory/kitchen-equipment/pans-cauldrons-saji/aluminum-pans/"

url_from_rak = 'http://rakporcelain.com/productdetails.php?catid=51'

url_of_posuda_mix= 'http://posuda-mix.ru/shop/farfor/RAK/Bez_dekora/group_824/?on_page=100'

def parse(html):
	soup = BeautifulSoup(html)

	soup2 = BeautifulSoup(get_html(url_from_rak))

	dict = {}
	dict2 = {}
	dict3 = {}
	dict4 = {}

	for div_item in soup2.find_all('div', class_="product_sub_items"):
		name_item = " ".join(div_item.find('div', class_="product_sub_items_name").get_text().replace('/','').split())

		article = div_item.find('div', class_="product_sub_items_name").get_text().split()[0]

		article_with_dig = article
		#print(article)

		article = ''.join([i for i in article if not i.isdigit()])

		image = div_item.find('img')['src'].replace('small', 'big')
		article_from_img= image.split('_').pop().replace('.png','')

		print(len(article_from_img))

		article_from_img = ''.join([i for i in article_from_img[0:14] if not i.isdigit()])+article_from_img[14:16]

		article_from_img_without_dig = ''.join([i for i in article_from_img if not i.isdigit()])

		dict.setdefault(article,image)
		dict2.setdefault(article_with_dig,image)
		dict3.setdefault(article_from_img, image)
		dict4.setdefault( article_from_img_without_dig,image)
	print(dict3)

	#name_category = '1883 > '+ soup.find('div', class_="product-list-intro").find('h1').get_text()
	#print(name_category)

	count_of_items = 0
	change_photo= 0
	for n in range(0,1):
		items = []

		categ = ' '.join(soup.find('div',class_="main-text").find('h1').get_text().split())
		print(categ)
		for item_div in soup.find_all('td',class_="b-item-name"):

			count_of_items = count_of_items + 1
			href_item = item_div.find('a')['href']

			name_of_item = item_div.find('a').get_text().split()

			name_of_item = ' '.join(name_of_item)
			print(name_of_item)

			print(href_item)

			address_of_item = DOMAIN+ href_item

			soup2 = BeautifulSoup(get_html(address_of_item))

			image_item = ''
			if(name_of_item =="Емкость глубокая квадратная BURD25" ):
				pass

			else :
				if(name_of_item !="Сливочн" ):
					image_item = DOMAIN+soup2.find('div', class_="image_cell_inn").find('a')['href']

				image_bool = False

				print(image_item)

				description_div = soup2.find('div', class_="description_row")

				div =description_div.find_all('div', class_="description_cell")

				if(div[0].find('strong').get_text()!=None):
					article_item = div[0].find('strong').get_text()
				print(article_item)

				if(div[1].find('a').get_text() != None):
					proizvoditel = div[1].find('a').get_text()
				print(proizvoditel)
				try:

					obem = div[2].get_text()
				except IndexError:
					obem = ""
				print(obem)

				price = soup2.find('div', class_="b-item-price").find('span').get_text().replace(',','.')
				print(price)

				atricle_without_dig = ''.join([i for i in article_item if not i.isdigit()])

				if (dict2.get(article_item) == None):
					if (dict.get(atricle_without_dig)!= None):
						image_item = dict.get(atricle_without_dig)
						change_photo = change_photo +1
						image_bool = True

				if(dict3.get(article_item) != None):
					image_item = dict3.get(article_item)
					change_photo = change_photo +1
					image_bool = True

				if(dict2.get(article_item)!= None):
					image_item = dict2.get(article_item)

					change_photo = change_photo +1
					image_bool = True

			#if (image_bool is False and dict4.get(atricle_without_dig)!= None ):
			#	image_item = dict4.get(atricle_without_dig)

				#.replase('href="/shop/producers/rak-porcelain/"','href="/product-category/posuda/rak-porcelain-oae/"')
			#print(description_div)

			#image_item = item_div.find('div', class_="cat-goods__item-preview").find('a').find('img')['src']#Картинка товара

			#name_of_item = soup2.find('h1', class_="product-title").get_text()#Имя товара

			#article_item = item_div.find('span', class_="text-warning").get_text()#Артикль

			#price_item = item_div.find('span', class_="price__current-value").get_text()#Цена товара

			#table = soup2.find('div', class_="col-12 col-md-8").find('table')#Описание товара

			#href_price = soup2.find('a', class_="btn btn-dark w-100 my-3")['href']

			#soup3 = BeautifulSoup(get_html(href_price))
			#price_item = soup3.find('span', class_="price_value").get_text()
			name_category = 'Фарфоровая посуда > RAK Porcelain (ОАЭ) > Без декора >  ' + categ
			items.append({

				'name': name_of_item,
				'img_src': image_item,
				'price': price,
				'proizvoditel': proizvoditel,
				'article': article_item,
				'obem':  obem,
				#'article' : article_item,
				'category': name_category,
				'def_url': address_of_item,
			})
	csvfilename = name_category+".csv"
	with open (csvfilename, 'w') as csvfile:
		writer =  csv.writer(csvfile)
		writer.writerow(("Имя", "Изображения", 'Цена','Производитель' , 'Артикул','Короткое описание', "Категории","Исходный адрес"))
		for item in items:
			writer.writerow(
				(item['name'],item['img_src'],item['price'], item['proizvoditel'],item['article'],item['obem'],item['category'],item['def_url'])
				)

	print('COunt all items:', count_of_items)

	print('COunt of all items where changed add of IMG:', change_photo)

def get_html(url):
 	resposnse = urllib.request.urlopen(url)
 	return resposnse.read()

def main():

	parse(get_html(url_of_posuda_mix))

	#soupmain = BeautifulSoup(get_html('https://www.klenmarket.ru/shop/inventory/kitchen-equipment/pans-cauldrons-saji/'))

	'''for categ in soupmain.find_all('a', class_="shop-eq-list__cats-item"):
		print(categ['href'])
		parse(get_html(DOMAIN + categ['href']))'''

if __name__ == '__main__':
	main()


import pkg_resources

from .celeri import (
    read_data,
    process_station,
    process_segment,
    process_sar,
    assign_block_labels,
    merge_geodetic_data,
    get_elastic_operators,
    get_all_mesh_smoothing_matrices,
    get_all_mesh_smoothing_matrices_simple,
    get_rotation_to_velocities_partials,
    get_global_float_block_rotation_partials,
    get_tri_displacements,
    get_ordered_edge_nodes,
    get_mesh_edge_elements,
    get_block_motion_constraints,
    get_slip_rate_constraints,
    get_rotation_to_slip_rate_partials,
    get_block_strain_rate_to_velocities_partials,
    get_mogi_to_velocities_partials,
    get_tde_slip_rate_constraints,
    get_keep_index_12,
    get_2component_index,
    get_3component_index,
    interleave2,
    post_process_estimation,
    get_tde_to_velocities,
    get_shared_sides,
    write_output,
    create_output_folder,
    assemble_and_solve_dense,
    get_index,
    get_data_vector,
    get_weighting_vector,
    get_full_dense_operator_block_only,
    get_full_dense_operator,
    get_weighting_vector_single_mesh_for_col_norms,
    get_tde_to_velocities_single_mesh,
    get_elastic_operator_single_mesh,
    get_segment_station_operator_okada,
    get_elastic_operators_okada,
    get_command,
    get_logger,
    matvec,
    matvec_wrapper,
    rmatvec,
    rmatvec_wrapper,
    post_process_estimation_hmatrix,
    get_h_matrices_for_tde_meshes,
    align_velocities,
    process_args,
    get_processed_data_structures,
    plot_input_summary,
    plot_estimation_summary,
    build_and_solve_hmatrix,
    build_and_solve_dense,
    write_output_supplemental,
    parse_args,
    get_okada_displacements,
    build_and_solve_dense_no_meshes,
    wrap2360,
    get_mesh_perimeter,
    get_transverse_projection,
)

from .celeri_vis import (
    plot_matrix_abs_log,
    plot_meshes,
    plot_rotation_components,
    plot_strain_rate_components_for_block,
    plot_segment_displacements,
)

try:
    __version__ = pkg_resources.get_distribution(__name__).version
except Exception:
    __version__ = "unknown"

__all__ = []


word = input()
lst = (('ABC', 2), ('DEF', 3), ('GHI', 4), ('JKL', 5), ('MNO', 6), ('PQRS', 7), ('TUV', 8), ('WXYZ', 9))
time = 0

for ele in word:
    for i in range(len(lst)):
        if ele in lst[i][0]:
            time += (lst[i][1] + 1)

print(time)


__version__ = '1.3.1'
__author__  = 'SGT'

import time
import threading

from b3.functions import getCmd
from b3.plugins.welcome import WelcomePlugin
from b3.plugins.welcome import F_FIRST
from b3.plugins.welcome import F_NEWB
from b3.plugins.welcome import F_USER
from b3.plugins.welcome import F_ANNOUNCE_FIRST
from b3.plugins.welcome import F_ANNOUNCE_USER
from b3.plugins.welcome import F_CUSTOM_GREETING

class GeowelcomePlugin(WelcomePlugin):

    requiresPlugins = ['geolocation']
    loadAfterPlugins = ['countryfilter', 'welcome']

    _default_messages = {
        'first': '^7Welcome $name^7, this must be your first visit, you are player ^3#$id. Type !help for help',
        'newb': '^7[^2Authed^7] Welcome back $name ^7[^3@$id^7], last visit ^3$lastVisit. '
                'Type !register in chat to register. Type !help for help',
        'user': '^7[^2Authed^7] Welcome back $name ^7[^3@$id^7], last visit ^3$lastVisit^7, '
                'you\'re a ^2$group^7, played $connections times',
        'announce_first': '^7Everyone welcome $name^7, player number ^3#$id^7, to the server',
        'announce_user': '^7Everyone welcome back $name^7, player number ^3#$id^7, to the server, '
                         'played $connections times',
        'greeting': '^7$name^7 joined: $greeting',
        'greeting_empty': '^7You have no greeting set',
        'greeting_yours': '^7Your greeting is %s',
        'greeting_bad': '^7Greeting is not formatted properly: %s',
        'greeting_changed': '^7Greeting changed to: %s',
        'greeting_cleared': '^7Greeting cleared',
        'announce_user_geo': '^7Everyone welcome back $name^7, from ^3$country^7. Player number ^3#$id^7, '
                             'played $connections times',
        'announce_first_geo': '^7Everyone welcome $name^7, from ^3$country^7. Player number ^3#$id^7'
    }

    ####################################################################################################################
    #                                                                                #
    #    STARTUP                                                                                #
    #                                                                                #
    ####################################################################################################################

    def onStartup(self):
        """
        Initialize the plugin
        """
        self._adminPlugin = self.console.getPlugin('admin')

        # register our commands
        if 'commands' in self.config.sections():
            for cmd in self.config.options('commands'):
                level = self.config.get('commands', cmd)
                sp = cmd.split('-')
                alias = None
                if len(sp) == 2:
                    cmd, alias = sp

                func = getCmd(self, cmd)
                if func:
                    self._adminPlugin.registerCommand(self, cmd, level, func, alias)

        # register events needed
        self.registerEvent('EVT_CLIENT_GEOLOCATION_SUCCESS', self.onGeolocationSuccess)
        self.registerEvent('EVT_CLIENT_GEOLOCATION_FAILURE', self.onGeolocationFailure)

        welcomePlugin = self.console.getPlugin('welcome')
        if welcomePlugin:
            self.info('NOTE: to run this plugin you don\'t need to load also the Welcome plugin: disabling Welcome plugin')
            welcomePlugin.disable()

    ####################################################################################################################
    #                                                                                #
    #    EVENTS                                                                                #
    #                                                                                #
    ####################################################################################################################

    def onGeolocationSuccess(self, event):
        """
        Handle EVT_CLIENT_GEOLOCATION_SUCCESS
        """
        if self.must_welcome(event.client):
            t = threading.Timer(self._welcomeDelay, self.geowelcome, (event.client,))
            t.start()

    def onGeolocationFailure(self, event):
        """
        Handle EVT_CLIENT_GEOLOCATION_FAILURE
        """
        if self.must_welcome(event.client):
            t = threading.Timer(self._welcomeDelay, self.welcome, (event.client,))
            t.start()

    ####################################################################################################################
    #                                                                                #
    #    OTHER METHODS                                                                                #
    #                                                                                #
    ####################################################################################################################

    def must_welcome(self, client):
        """
        Checks whether we have to show a welcome message or not.
        :return: True if we need to welcome the player, False otherwise
        """
        if self._welcomeFlags <= 0 or not client or client.id is None or \
           client.cid is None or not client.connected or client.pbid == 'WORLD':
            return False
        if self.console.upTime() < 300:
            self.debug('not welcoming player because the bot started less than 5 min ago')
            return False
        return True

    def geowelcome(self, client):
        """
        Geo Welcome a player
        """
        if client.lastVisit:
            self.debug('lastVisit: %s' % self.console.formatTime(client.lastVisit))
            _timeDiff = time.time() - client.lastVisit
        else:
            self.debug('lastVisit not available: must be the first time')
            _timeDiff = 1000000  # big enough so it will welcome new players

        # don't need to welcome people who got kicked or where already
        # welcomed in before _min_gap s ago
        if client.connected and _timeDiff > self._min_gap:
            info = self.get_client_info(client)
            if client.connections >= 2:
                if client.maskedLevel > 0:
                    if self._welcomeFlags & F_USER:
                        client.message(self.getMessage('user', info))
                elif self._welcomeFlags & F_NEWB:
                    client.message(self.getMessage('newb', info))

                if self._welcomeFlags & F_ANNOUNCE_USER and client.connections < self._newbConnections:
                    self.console.say(self.getMessage('announce_user_geo', info))
            else:
                if self._welcomeFlags & F_FIRST:
                    client.message(self.getMessage('first', info))
                if self._welcomeFlags & F_ANNOUNCE_FIRST:
                    self.console.say(self.getMessage('announce_first_geo', info))

            if self._welcomeFlags & F_CUSTOM_GREETING and client.greeting:
                _info = {'greeting': client.greeting % info}
                _info.update(info)
                self.console.say(self.getMessage('greeting', _info))
        else:
            if _timeDiff <= self._min_gap:
                self.debug('client already welcomed in the past %s seconds' % self._min_gap)

    def get_client_info(self, client):
        """
        Get client information for message substitution
        """
        info = super(GeowelcomePlugin, self).get_client_info(client)
        if client.location and client.location.country:
            info['country'] = client.location.country
        return info


# Module 3
#   Programming Assignment 4
#     Prob-2.py

# Edward Lamanna

'''
take user input for name = employees name, wage = hour much they make an hour, and hours = how many hours they work in a week
'''

def main():
    name = input("what is your name?: ")
    wage = input("How much do you make an hour?: ")
    hours = input("in a week how many hours do you work?: ")
    # now the equation is for all hours
    if hours > 40:
        regularWage = 40 * wage
        overtime = (hours - 40) * 1.5 * wage
    else:
        regularWage = hours * wage
        overtime = 0

    total = regularWage + overtime
    # now were doing taxes
    tax = total * .2
    med = total * .1
    takehome = total - (tax + med)
    # now were printing data
    print("Employes Name:", name)
    print("Employes regualr hours:", hours)
    print("you regular wage is:", regularWage)
    print("your overtime wage is:", overtime)
    print("your total wage is:", total)
    print("the total tax withholding is:", tax)
    print("the total medical insurance withholding is:", med)
    print("this is what you take home:", takehome)

main()


from functools import reduce

with open("c:\\1.txt",encoding="utf-8") as f:
    b=[]
    for i in f:
        a=i.split()
        b.append(a)
    print(b)

num=[1,23,4,6,7,8,9,10]

f=[i for i in num if i%2==0 and num.index(i)%2==0]
print(f)

with open("c:\\1.txt") as f:
    for i in f:
        print(i)

def zlb():
    print("hello")

with open("c:\\1.txt") as f:
    print("hello")

while i < 10:
    print("hello")

算法题目练习汇总/3、算法学习/实验.py


import logging

# django dependencies
from rest_framework.views import APIView
from rest_framework.response import Response

# project dependencies
from . import helpers
from .models import Video, get_video_by_id

from video_manager.constants import DEFAULT_VIDEO_FOLDER

logger = logging.getLogger('django')

class VideoAlreadyExistsException(Exception):
    pass

class VideosApi(APIView):

    def get(self, _):
        response = {
            "message": {},
            "status_code": 200
        }
        try:
            complete_video_list = []
            for each_video in Video.objects.all():
                complete_video_list.append(
                    helpers.get_video_metadata_response(each_video)
                )
            response["message"] = {"videos": complete_video_list}
            logger.info(f"Videos found {len(complete_video_list)}")
        except Exception as err:
            logger.error(err)
            response["message"] = "Fetching Video list failed"
            response['status_code'] = 500
        return Response(response['message'], status=response['status_code'], headers=None)

    def post(self, request):
        response = {
            "message": {},
            "status_code": 201
        }
        try:
            file_name = helpers.get_field_value('video_name', request)
            video_folder = helpers.get_field_value('video_folder', request)

            if helpers.video_exists(video_folder + file_name):
                raise VideoAlreadyExistsException

            file_path = helpers.save_video_file(
                video_folder + file_name, request.data.get('video_file'))
            video_metadata = helpers.get_video_data(
                DEFAULT_VIDEO_FOLDER + file_path)

            new_video = Video(name=file_name,
                              size=video_metadata['format']['size'],
                              path=video_folder,
                              duration=video_metadata['format']['duration'],
                              codec=video_metadata['format']['format_name'],
                              container=video_metadata['format']['tags']['major_brand'])
            new_video.save()
            response["message"] = helpers.get_video_metadata_response(
                new_video)
            logger.info(f"video upload successfully with id {new_video.id}")
        except VideoAlreadyExistsException as err:
            logger.error(err)
            response["message"] = f"Video with the name {file_name} already present at that path {video_folder}."
            logger.error(response["message"])
            response['status_code'] = 409
        except Exception as err:
            logger.error(err)
            try:
                # delete video if it was present
                helpers.delete_video_file(file_path)
                logger.info("video deleted")
            except:
                logger.info("video was not saved")
            response["message"] = "Video Upload failed, please try again"
            response['status_code'] = 500

        return Response(response['message'], status=response['status_code'], headers=None)

class VideoMetadataApi(APIView):
    def get(self, _, video_id):
        response = {
            "message": {},
            "status_code": 200
        }
        try:
            required_video = get_video_by_id(video_id)
            response["message"] = helpers.get_video_metadata_response(
                required_video)
            logger.info(f"video found with ID {required_video.id}")
        except Exception as err:
            logger.error(err)
            response["message"] = f"Video with ID {video_id} not found in system"
            response['status_code'] = 404

        return Response(response['message'], status=response['status_code'], headers=None)

    def put(self, request, video_id):
        response = {
            "message": {},
            "status_code": 200
        }
        try:
            required_video = get_video_by_id(video_id)
            current_path = required_video.path + required_video.name
            updated_fields = request.data

            for each_field_name, each_field_value in updated_fields.items():
                if each_field_name == "video_name":
                    required_video.name = each_field_value
                elif each_field_name == "video_folder":
                    required_video.path = each_field_value

            if current_path != required_video.path + required_video.name:       # move file if change in name/folder
                logger.info("path not same")
                logger.debug(
                    f"Current Path: {current_path}, new path: {required_video.path + required_video.name}")

                helpers.move_file(DEFAULT_VIDEO_FOLDER + current_path,
                                  DEFAULT_VIDEO_FOLDER + required_video.path + required_video.name)
                logger.info(
                    f"file moved to destination {DEFAULT_VIDEO_FOLDER + required_video.path + required_video.name} for video id {video_id}")
            required_video.save()       # save entry in db
            response["message"] = helpers.get_video_metadata_response(
                required_video)
        except Exception as err:
            logger.error(err)
            response["message"] = "Error occured while updating video metadata"
            response['status_code'] = 500
        return Response(response['message'], status=response['status_code'], headers=None)

    def delete(self, _, video_id):

        response = {
            "message": {},
            "status_code": 200
        }
        try:
            required_video = get_video_by_id(video_id)
            response["message"] = helpers.get_video_metadata_response(
                required_video)

            file_path = helpers.delete_video_file(
                required_video.path + required_video.name)  # delete video from filesystem

            required_video.delete()
            logger.error(f"Video deleted with ID {video_id}")
        except Exception as err:
            logger.error(err)
            response["message"] = f"Video with ID {video_id} not found"
            logger.error(response["message"])
            response['status_code'] = 404
        return Response(response['message'], status=response['status_code'], headers=None)


'''
Exercise of subset of Regular Expressions implement.
'''

from decorator import decorator

null = frozenset()
dot = lambda text: set([text[1:]]) if text else null
eol = lambda text: set(['']) if text == '' else null

def search(pattern, text):
    "Match pattern anywhere in text; return longest earliest match or None."
    for i in range(len(text)):
        m = match(pattern, text[i:])
        if m is not None:
            return m

def match(pattern, text):
    "Match pattern against start of text; return longest match found or None."
    remainders = pattern(text)
    if remainders:
        shortest = min(remainders, key=len)
        return text[:len(text) - len(shortest)]

def lit(s):
    return lambda text: set([text[len(s):]]) if text.startswith(s) else null

def seq(x, y):
    return lambda text: set.union(*map(y, x(text)))

def alt(x, y):
    return lambda text: set.union(x(text), y(text))

def star(x):
    return lambda text: set([text]) | set(t2 for t1 in x(text) if t1 != text for t2 in star(x)(t1))

def plus(x):
    return seq(x, star(x))

def opt(x):
    return alt(lit(''), x)  #opt(x) means that x is optional

def oneof(chars):
    return lambda text: set([text[1:]]) if text and text[0] in chars else null

@decorator
def n_ary(f):
    """Given binary function f(x, y), return an n_ary function such
    that f(x, y, z) = f(x, f(y,z)), etc. Also allow f(x) = x."""

    def n_ary_f(x, *args):
        # if not args:
        #     return x
        # elif len(args) == 1:
        #     return f(x, *args)
        # return f(x, n_ary_f(args[0], *args[1:]))
        return x if not args else f(x, n_ary_f(*args))

    return n_ary_f

def matchset(pattern, text):
    '''
    Match pattern at start of text; return a set of remainders of text.
    '''
    op, x, y = components(pattern)
    if op == 'lit':
        return set([text[len(x):]]) if text.startswith(x) else null
    elif op == 'seq':
        return set(t2 for t1 in matchset(x, text) for t2 in matchset(y, t1))
    elif op == 'alt':
        return matchset(x, text) | matchset(y, text)
    elif op == 'dot':
        return set([text[1:]]) if text else null
    elif op == 'oneof':
        return set([text[1:]]) if text.startswith(x) else null
    elif op == 'eol':
        return set(['']) if text == '' else null
    elif op == 'star':
        return (set([text]) | set(
            t2 for t1 in matchset(x, text) for t2 in matchset(pattern, t1)
            if t1 != text))
    else:
        raise ValueError('unknown pattern: {}'.format(pattern))

def components(pattern):
    "Return the op, x, and y arguments; x and y are None if missing."
    x = pattern[1] if len(pattern) > 1 else None
    y = pattern[2] if len(pattern) > 2 else None
    return pattern[0], x, y

def test():
    "Tests."
    assert lit('abc') == ('lit', 'abc')
    assert seq(('lit', 'a'), ('lit', 'b')) == ('seq', ('lit', 'a'), ('lit',
                                                                     'b'))
    assert alt(('lit', 'a'), ('lit', 'b')) == ('alt', ('lit', 'a'), ('lit',
                                                                     'b'))
    assert star(('lit', 'a')) == ('star', ('lit', 'a'))
    assert plus(('lit', 'c')) == ('seq', ('lit', 'c'), ('star', ('lit', 'c')))
    assert opt(('lit', 'x')) == ('alt', ('lit', ''), ('lit', 'x'))
    assert oneof('abc') == ('oneof', ('a', 'b', 'c'))

    assert matchset(('lit', 'abc'), 'abcdef') == set(['def'])
    assert matchset(('seq', ('lit', 'hi '), ('lit', 'there ')),
                    'hi there nice to meet you') == set(['nice to meet you'])
    assert matchset(('alt', ('lit', 'dog'), ('lit', 'cat')),
                    'dog and cat') == set([' and cat'])
    assert matchset(('dot', ), 'am i missing something?') == set(
        ['m i missing something?'])
    assert matchset(oneof('a'), 'aabc123') == set(['abc123'])
    assert matchset(oneof('ab'), 'aabc123') == set(['abc123'])
    assert matchset(('eol', ), '') == set([''])
    assert matchset(('eol', ), 'not end of line') == frozenset([])
    assert matchset(('star', ('lit', 'hey')),
                    'heyhey!') == set(['!', 'heyhey!', 'hey!'])

    assert match(('star', ('lit', 'a')), 'aaabcd') == 'aaa'
    assert match(('alt', ('lit', 'b'), ('lit', 'c')), 'ab') is None
    assert match(('alt', ('lit', 'b'), ('lit', 'a')), 'ab') == 'a'
    assert search(('alt', ('lit', 'b'), ('lit', 'c')), 'ab') == 'b'

    g = alt(lit('a'), lit('b'))
    assert g('abc') == set(['bc'])

    return 'tests pass'

def test2():
    def f(x, y):
        return x + y

    fn = n_ary(f)

    print(fn(1))
    print(fn(1, 2))
    print(fn(1, 2, 3))
    print(fn(1, 2, 3, 4))

    def test_fun(x, y):
        return ('seq', x, y)

    test_fun = n_ary(test_fun)
    print(test_fun(1, 2, 3, 4))

    @n_ary
    def test_fun2(x, y):
        "test_fun2 wrapped in decorator"
        return ('seq', x, y)

    print(test_fun2(1, 2, 3, 4))
    help(test_fun2)
    help(n_ary)
    print('test2 success')

if __name__ == '__main__':
    # print(test())
    test2()


import numpy as np

from src.rhyme import rhyme_func
from src.utils import resample_normalize

def similarity(standard, normalized_sample, norm):
    resample = np.array(resample_normalize(standard, len(normalized_sample)))
    return np.dot(resample, normalized_sample) / (np.linalg.norm(resample) * norm)

def similarity_rhyme(standard, query_signal, norm):
    standard_rhyme = rhyme_func(standard, window=1000)
    return np.dot(standard_rhyme, query_signal) / (np.linalg.norm(standard_rhyme) * norm)

def detail_search(df_value, normalized_sample, window_param, id_param):
    step = int(window_param / 32)
    window_step = int(window_param / 8)

    ID = id_param
    WINDOW = window_param

    similarity_score = 0
    norm = np.linalg.norm(normalized_sample)

    for _ in range(5):
        for id in [ID - step, ID, ID + step]:
            for window in [WINDOW - window_step, WINDOW, WINDOW + window_step]:
                similarity_now = similarity(df_value[id:id+window], normalized_sample, norm)
                if similarity_now > similarity_score:
                    similarity_score = similarity_now
                    ID_TEMP = id
                    WINDOW_TEMP = window
        ID = ID_TEMP
        WINDOW = WINDOW_TEMP
        step = int(step / 2)
        window_step = int(window_step / 2)
    return ID, WINDOW, similarity_score

def detail_rhyme_search(df_value, query_signal, window_param, id_param):
    step = int(window_param / 32)
    window_step = int(window_param / 8)

    ID = id_param
    WINDOW = window_param

    similarity_score = 0
    norm = np.linalg.norm(query_signal)

    for _ in range(5):
        for id in [ID - step, ID, ID + step]:
            for window in [WINDOW - window_step, WINDOW, WINDOW + window_step]:
                similarity_now = similarity_rhyme(df_value[id:id+window], query_signal, norm)
                if similarity_now > similarity_score:
                    similarity_score = similarity_now
                    ID_TEMP = id
                    WINDOW_TEMP = window
        ID = ID_TEMP
        WINDOW = WINDOW_TEMP
        step = int(step / 2)
        window_step = int(window_step / 2)
    return ID, WINDOW, similarity_score


bar = "hello"
md = {}
for el in bar:
    if el in md:
        md[el]+=1
    else:
        md[el]=1
#print(md)
bar = "hello world"
b = "l"
def ft(mstr,delim):
    start = 0
    stop = 0
    count = 0
    for i in range(len(mstr)):
        if mstr[i] == delim:
            start = i
            break
    for i in range(len(mstr)-1,-1,-1):
        if mstr[i] == delim:
            stop = i
            break
    return mstr[start+1:stop]
print(ft(bar,b))


import time
import h5py
import numpy as np
import pickle as pkl
import pandas as pd
from datetime import datetime
import matplotlib.pyplot as plt
from sklearn import preprocessing, manifold
import scipy.io as sio

import os
import json
import natsort
import random
from multiprocessing.dummy import Pool

from core import const, plot_utils, sobol

# region Load and Dump

def pkl_load(path):
    with open(path, 'rb') as f:
        data = pkl.load(f, encoding='bytes')
    return data

def txt_load(path):
    with open(path, 'r') as f:
        lines = f.read().splitlines()
    lines = np.array(lines)
    return lines

def byte_load(path):
    with open(path, 'rb') as f:
        data = f.read()
    return data

def json_load(path):
    with open(path, 'r') as f:
        data = json.load(f)

    return data

def h5_load(path, dataset_name='data'):
    h5_file = h5py.File(path, 'r')
    data = h5_file[dataset_name].value
    h5_file.close()
    return data

def h5_load_multi(path, dataset_names):
    h5_file = h5py.File(path, 'r')
    data = [h5_file[name].value for name in dataset_names]
    h5_file.close()
    return data

def txt_dump(data, path):
    l = len(data) - 1
    with open(path, 'w') as f:
        for i, k in enumerate(data):
            if i < l:
                k = ('%s\n' % k)
            else:
                k = ('%s' % k)
            f.writelines(k)

def byte_dump(data, path):
    with open(path, 'wb') as f:
        f.write(data)

def pkl_dump(data, path, is_highest=True):
    with open(path, 'wb') as f:
        if not is_highest:
            pkl.dump(data, f)
        else:
            pkl.dump(data, f, pkl.HIGHEST_PROTOCOL)

def json_dump(data, path):
    with open(path, 'w') as f:
        json.dump(data, f)

def h5_dump(data,  dataset_name, path):
    h5_file = h5py.File(path, 'w')
    h5_file.create_dataset(dataset_name, data=data, dtype=data.dtype)
    h5_file.close()

def h5_dump_multi(data, dataset_names, path):
    h5_file = h5py.File(path, 'w')
    n_items = len(data)
    for i in range(n_items):
        item_data = data[i]
        item_name = dataset_names[i]
        h5_file.create_dataset(item_name, data=item_data, dtype=item_data.dtype)
    h5_file.close()

def csv_load(path, sep=',', header='infer'):
    df = pd.read_csv(path, sep=sep, header=header)
    data = df.values
    return data

def mat_load(path, m_dict=None):
    """
    Load mat files.
    :param path:
    :return:
    """
    if m_dict is None:
        data = sio.loadmat(path)
    else:
        data = sio.loadmat(path, m_dict)

    return data

# endregion

# region File/Folder Names/Pathes

def file_names(path, is_nat_sort=False):
    if not os.path.exists(path):
        exp_msg = 'Sorry, folder path does not exist: %s' % (path)
        raise Exception(exp_msg)

    names = os.walk(path).__next__()[2]

    if is_nat_sort:
        names = natsort.natsorted(names)

    return names

def file_pathes(path, is_nat_sort=False):
    if not os.path.exists(path):
        exp_msg = 'Sorry, folder path does not exist: %s' % (path)
        raise Exception(exp_msg)
    names = os.walk(path).__next__()[2]

    if is_nat_sort:
        names = natsort.natsorted(names)

    pathes = ['%s/%s' % (path, n) for n in names]
    return pathes

def folder_names(path, is_nat_sort=False):
    if not os.path.exists(path):
        exp_msg = 'Sorry, folder path does not exist: %s' % (path)
        raise Exception(exp_msg)

    names = os.walk(path).__next__()[1]

    if is_nat_sort:
        names = natsort.natsorted(names)

    return names

def folder_pathes(path, is_nat_sort=False):
    if not os.path.exists(path):
        exp_msg = 'Sorry, folder path does not exist: %s' % (path)
        raise Exception(exp_msg)

    names = os.walk(path).__next__()[1]

    if is_nat_sort:
        names = natsort.natsorted(names)

    pathes = ['%s/%s' % (path, n) for n in names]
    return pathes

# endregion

# region Normalization

def normalize_mean_std(x):
    mean = np.mean(x, axis=0)
    std = np.std(x, axis=0)
    x -= mean
    x /= std
    return x

def normalize_mean(x):
    mean = np.mean(x, axis=0)
    x /= mean
    return x

def normalize_sum(x):
    sum = np.sum(x, axis=1)
    x = np.array([x_i / sum_i for x_i, sum_i in zip(x, sum)])
    return x

def normalize_l2(x):
    return preprocessing.normalize(x)

def normalize_l1(x):
    return preprocessing.normalize(x, norm='l1')

def normalize_range_0_to_1(x):
    x = np.add(x, -x.min())
    x = np.divide(x, x.max())
    return x

# endregion

# region Array Helpers

def array_to_text(a, separator=', '):
    text = separator.join([str(s) for s in a])
    return text

def get_size_in_kb(size):
    size /= float(1024)
    return size

def get_size_in_mb(size):
    size /= float(1024 * 1024)
    return size

def get_size_in_gb(size):
    size /= float(1024 * 1024 * 1024)
    return size

def get_array_memory_size(a):
    if type(a) is not np.ndarray:
        raise Exception('Sorry, input is not numpy array!')

    dtype = a.dtype
    if dtype == np.float16:
        n_bytes = 2
    elif dtype == np.float32:
        n_bytes = 4
    else:
        raise Exception('Sorry, unsupported dtype:', dtype)

    s = a.size
    size = s * n_bytes
    return size

def get_expected_memory_size(array_shape, array_dtype):
    dtype = array_dtype
    if dtype == np.float16:
        n_bytes = 2
    elif dtype == np.float32:
        n_bytes = 4
    else:
        raise Exception('Sorry, unsupported dtype:', dtype)

    s = 1
    for dim_size in array_shape:
        s *= dim_size

    size = s * n_bytes
    return size

def print_array(a):
    for item in a:
        print(item)

def print_array_joined(a):
    s = ', '.join([str(i) for i in a])
    print(s)

# endregion

# region Model Training/Testing

def debinarize_label(labels):
    debinarized = np.array([np.where(l == 1)[0][0] for l in labels])
    return debinarized

def get_model_feat_maps_info(model_type, feature_type):
    ex_feature = Exception('Sorry, unsupported feature type: %s' % (feature_type))
    ex_model = Exception('Sorry, unsupported model type: %s' % (model_type))
    info = None

    if model_type in ['vgg']:
        if feature_type in ['pool5']:
            info = 512, 7
        elif feature_type in ['conv5_3']:
            info = 512, 14
        else:
            raise ex_feature
    elif model_type in ['resnet18', 'resnet34']:
        if feature_type in ['conv5c']:
            info = 512, 7
        elif feature_type in ['conv5c_pool']:
            info = 512, 1
        else:
            raise ex_feature
    elif model_type in ['resnet3d', 'resnet152', 'resnet101', 'resnet50']:
        if feature_type in ['res4b35']:
            info = 1024, 14
        elif feature_type in ['res5c', 'res52', 'conv5c']:
            info = 2048, 7
        elif feature_type in ['pool5', 'conv5c_pool']:
            info = 2048, 1
        else:
            raise ex_feature
    elif model_type in ['i3d']:
        if feature_type in ['mixed_5c']:
            info = 1024, 7
        elif feature_type in ['mixed_5c_maxpool']:
            info = 1024, 1
        elif feature_type in ['softmax']:
            info = 400, 1
        else:
            raise ex_feature
    elif model_type in ['i3d_kinetics_keras']:
        if feature_type in ['mixed_4f']:
            info = 832, 7
        else:
            raise ex_feature
    elif model_type in ['mobilenetv3_small']:
        if feature_type in ['conv12']:
            info = 576, 7
        elif feature_type in ['convpool']:
            info = 576, 1
        else:
            raise ex_feature
    else:
        raise ex_model

    return info

def calc_num_batches(n_samples, batch_size):
    n_batch = int(n_samples / float(batch_size))
    n_batch = n_batch if n_samples % batch_size == 0 else n_batch + 1
    return n_batch

# endregion

# region Misc

def byte_array_to_string(value):
    decoded = string_decode(value)
    return decoded

def string_to_byte_array(value):
    decoded = string_encode(value)
    return decoded

def string_decode(value, coding_type='utf-8'):
    """
    Convert from byte array to string.
    :param value:
    :param coding_type:
    :return:
    """
    decoded = value.decode(coding_type)
    return decoded

def string_encode(value, coding_type='utf-8'):
    """
    Convert from byte string to array.
    :param value:
    :param coding_type:
    :return:
    """
    encoded = value.encode(coding_type)
    return encoded

def is_list(value):
    input_type = type(value)
    result = input_type is list
    return result

def is_tuple(value):
    input_type = type(value)
    result = input_type is tuple
    return result

def is_enumerable(value):
    input_type = type(value)
    result = (input_type is list or input_type is tuple)
    return result

def learn_manifold(manifold_type, feats, n_components=2):
    if manifold_type == 'tsne':
        feats_fitted = manifold.TSNE(n_components=n_components, random_state=0).fit_transform(feats)
    elif manifold_type == 'isomap':
        feats_fitted = manifold.Isomap(n_components=n_components).fit_transform(feats)
    elif manifold_type == 'mds':
        feats_fitted = manifold.MDS(n_components=n_components).fit_transform(feats)
    elif manifold_type == 'spectral':
        feats_fitted = manifold.SpectralEmbedding(n_components=n_components).fit_transform(feats)
    else:
        raise Exception('wrong maniford type!')

    # methods = ['standard', 'ltsa', 'hessian', 'modified']
    # feats_fitted = manifold.LocallyLinearEmbedding(n_components=n_components, method=methods[0]).fit_transform(pred)

    return feats_fitted

def timestamp():
    time_stamp = "{0:%y}.{0:%m}.{0:%d}-{0:%I}:{0:%M}:{0:%S}".format(datetime.now())
    return time_stamp

def remove_extension(name):
    name = name.split('.')
    name = name[:-1]
    name = ''.join(name)
    return name

def get_file_extension(name):
    name = name.split('.')[-1]
    return name

def print_counter(num, total, freq=None):
    if freq is None:
        print('... %d/%d' % (num, total))
    elif num % freq == 0:
        print('... %d/%d' % (num, total))

def generate_centroids(n_centroids, n_dims, is_sobol=True):
    if is_sobol:
        # centroids as sobol sequence
        c = sobol.sobol_generate(n_dims, n_centroids)
        c = np.array(c)
    else:
        # centroids as random vectors
        c = np.random.rand(n_centroids, n_dims)
        c = c.astype(np.float32)

    return c

# endregion

# region Classes

class Path(str):
    def __new__(self, relative_path, args=None, root_path=const.DATA_ROOT_PATH):
        relative_path = relative_path % args if args is not None else relative_path
        path = os.path.join(root_path, relative_path)

        self.__path = path
        return self.__path

    def __str__(self):
        return self.__path

    def __repr__(self):
        return self.__path

class DurationTimer(object):
    def __init__(self):
        self.start_time = time.time()

    def duration(self, is_string=True):
        stop_time = time.time()
        durtation = stop_time - self.start_time
        if is_string:
            durtation = self.format_duration(durtation)
        return durtation

    def format_duration(self, duration):
        if duration < 60:
            return str(duration) + " sec"
        elif duration < (60 * 60):
            return str(duration / 60) + " min"
        else:
            return str(duration / (60 * 60)) + " hr"

class Obj():
    """
    Dummy object so you can add properties and functions to it.
    """

    def __init__(self):
        pass

# endregion


#！ -*- coding:utf-8 -*-
import sys
import pdb
import codecs
import MySQLdb

sys.path.insert(0, '..')
from conn import ConnectDBA

class CommentJiebaDB(ConnectDBA):
    """
    create kw tables
    """
    STOP_WORDS = 0
    COMPONENT_WORDS = 1
    STOPWORDS_FILE = 'stopwords.txt'
    COMPONENT_FILE = 'component.txt'
    KEYWORD_FILE = 'keywords.txt'

    def create_kw(self):
        """
        create kw table 来自结巴
        """
        sql = """
              create table if not exists kw (
                  id int(32) not null auto_increment,
                  keyword varchar(64),
                  counter int default 1,
                  mark int default 0,
                  primary key (id),
                  unique index kw_index (keyword)
              )engine=myisam auto_increment = 1 default charset = utf8;
        """
        self.cursor.execute(sql)
        self.db.commit()

    def create_kw_search(self):
        """
        create kw table
        """
        sql = """
              create table if not exists kw_search (
                  id int(32) not null auto_increment,
                  keyword varchar(64),
                  counter int default 1,
                  mark int default 0,
                  primary key (id),
                  unique index kw_index (keyword)
              )engine=myisam auto_increment = 1 default charset = utf8;
        """
        self.cursor.execute(sql)
        self.db.commit()

    def create_kw_dict(self):
        """
        词库:存储无效词type=0，如：标点符号：，、等
             存储组合词type=1，将有特殊意义的词作为一个关键词整体
        """
        sql = """
              create table if not exists kw_dict (
                  id int(32) not null auto_increment,
                  keyword varchar(64),
                  counter int default 1,
                  type tinyint  ,
                  primary key (id),
                  unique index kw_dict_index (keyword, type)
              )engine=myisam auto_increment = 1 default charset = utf8;
        """
        self.cursor.execute(sql)
        self.db.commit()

    def insertMany_kw_dict(self, kw_list):
        """
        插入数据：kw_dict
        """
        sql = 'insert ignore into kw_dict(keyword, type) values( %s, %s)  '
        try:
            self.cursor.executemany(sql, kw_list)
            self.db.commit()
        except MySQLdb.Error as e:
            print ('kw_list:{0} exist already.'.format(str(e)))

    def create_kw_cloud(self):
        """
        词云图
        tips:
        中文分词和词性标注的性能对关键词抽取的效果至关重要
        +--------+---------+
        | 词库名称| type值  |
        +--------+---------+
        | 正面评价|   0     |
        +--------+---------+
        | 负面评价|   1     |
        +--------+---------+
        |  bug   |    2    |
        +--------+---------+
        | 建议意见|    3    |
        +--------+---------+
        |  刷 评  |    4    |
        +--------+---------+
        """
        sql = """
              create table if not exists kw_cloud (
                  id int(32) not null auto_increment,
                  keyword varchar(64),
                  counter int default 1,
                  primary key (id),
                  type tinyint
              )engine=myisam auto_increment = 1 default charset = utf8;
        """
        self.cursor.execute(sql)
        self.db.commit()

class UpdateKw(CommentJiebaDB):
    """
    更新词库
    """
    def update_dict(self, dict_type = CommentJiebaDB.STOP_WORDS):
        """
        从stopwords.txt中更新无效词/停用词库
        或
        从component.txt中更新组合词库

        默认更新的是无效词库
        dict_type = 1时，更新的是组合词库
        """
        if dict_type == self.STOP_WORDS:
            f = codecs.open(self.STOPWORDS_FILE, 'r', 'utf-8') # 停用词库
        else:
            f = codecs.open(self.COMPONENT_FILE, 'r', 'utf-8') # 组合词库

        if f:
            content = f.read()
            content = content.replace('\r\n', ' ')
            kw_list = content.split(' ')
            try:
                kw_list.remove('')
            except ValueError:
                pass
            #
            t_kw = [(x, dict_type ) for x in kw_list]
            self.insertMany_kw_dict(t_kw)
            f.close()
        else:
            print("ERROR: Can not find the files...")

    def read_kw(self, kw_tb="kw_search"):
        """
        从kw表或者kw_search表中读取未归类的数据
        当kw_tb == kw的时候，从kw表中读取，否则从kw_search表中读取
        """

        sql = "select keyword, id from {0} where mark = 0 limit 10000".format(kw_tb)
        try:
            self.cursor.execute(sql)
            keywords = self.cursor.fetchall()
            content = ''
            for keyword in keywords:
                content = content+','+keyword[0]

            f = codecs.open(self.KEYWORD_FILE, 'a', 'utf-8') # 组合词库
            f.write(content)
            f.close()

            ids = [kw[1] for kw in keywords]
            print(tuple(ids))

            # 更新数据库
            updatesql = "update {0} set mark = 1 where id in {1}".format(kw_tb, tuple(ids))
            self.cursor.execute(updatesql)
            self.db.commit()
        except MySQLdb.Error as e:
            print ('ERROR: read_kw:{0}.'.format(str(e)))

if __name__ == "__main__":
    cjd = UpdateKw()
    #cjd.update_dict() # 停用词更新
    cjd.read_kw()
    #cjd.update_dict(cjd.COMPONENT_WORDS) # 更新组合词
    cjd.close()


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

#  作者：马欣宇
#  时间：2019-3-26
#  文件描述：
#  1.用于在道路文件输入时给出地图显示的中心点，以便前端人员显示地图
#  2.用于计算道路全长

import pandas as pd
import numpy as np
from refs.utils import coordinate_cal
import json

class handle_street:
    def __init__(self, file2):
        data2 = pd.read_csv(file2)
        self.data2 = np.array(data2)

    #  用道路文件中的第一条道路中心点做地图显示的中心
    def getMapCenter(self):
        firstStreet = self.data2[0]
        location1 = [firstStreet[2], firstStreet[3]]  # 起始点经纬度
        location2 = [firstStreet[4], firstStreet[5]]  # 终止点经纬度
        cul_cp = coordinate_cal.CenterPointFromListOfCoordinates()
        return cul_cp.center_geolocation([location1, location2])

    #  计算每条道路的全长和起点，格式为： 道路编号：【长度，起点】
    def getStreetAttr(self):
        streetAttr = {}
        for street in self.data2:
            if street[0] not in streetAttr.keys():
                cal = coordinate_cal.cal_distance(lat1=street[3], lon1=street[2], lat2=street[5], lon2=street[4])
                streetAttr[street[0]] = [cal.twopoint_distance(), [street[2], street[3]]]
        return streetAttr

    # 获取地图显示中心点的json
    def getMapCenterJson(self):
        location = self.getMapCenter()
        locationDict = {}
        locationDict["center_lon"] = location[0]
        locationDict["center_lat"] = location[1]
        return json.dumps(locationDict)

# file1 = 'file_toTry/time_accident.csv'
# file2 = 'file_toTry/roads.csv'
# h = handle_street(file2)
# print(h.getMapCenterJson())


'''
Export MIDI files to csv
MS 2/18/19

What pitch-classes are being played each time step? (normalize octaves)
Format:

'''

import mido
import pandas as pd
import sys

STEP = .2 # seconds

def frange(start, stop, step):
	r = []
	tmp = start
	while (tmp < stop):
		r.append(tmp)
		tmp += step
	return r

# Import track
print("Importing track")
f = sys.argv[1]
mid = mido.MidiFile(f)

# Construct intermediate dataframe, all note_on/note_off events
print("Construting intermediate dataframe")
df0 = pd.DataFrame(columns = ['t','note','vel','type'])
t = 0
i = 0
for msg in mid:
	t += msg.time
	if (msg.type == 'note_on' or msg.type == 'note_off'):
		df0.loc[i] = [t, msg.note % 12, msg.velocity, msg.type]
		i += 1
print("df0")
print(df0)

# Construct dataframe of all pitch classes each timestep
print("Constructing final dataframe")
df = pd.DataFrame(columns = ['t']+range(12))
i = 0
note_vels = [0]*12 # initialize note columns to zero
increments = frange(STEP, df0['t'].max(), STEP)
print("starting loop")
for t in increments:
	start = df0['t'] >= (t-STEP)
	end = df0['t'] < t
	on = df0['type'] == 'note_on'
	off = df0['type'] == 'note_off'
	# find note onsets
	onsets = df0[start & end & on]
	for row in range(onsets.shape[0]):
		onset = onsets.iloc[row]
		note_vels[onset['note']] = 1

	df.loc[i] = [t*1000] + note_vels

	# find note offs
	offs = df0[start & end & off]
	for row in range(offs.shape[0]):
		off = offs.iloc[row]
		note_vels[off['note']] = 0

	i += 1

# Output to csv
print("Outputing to csv")
df.to_csv(f.replace('.mid','_time.csv'),index=False)


from fastapi import FastAPI
from pydantic import BaseModel
from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String, text
import  uvicorn
from  DbOperations import DbOperations

db = DbOperations("sqlite:///job_details.db")
meta = MetaData()
table_object = Table(
    'jobdetails', meta,
    Column('id', Integer, primary_key=True),
    Column('name', String),
    Column('salary', String),
)
meta.create_all(db.engine)

app = FastAPI()
class Jobs(BaseModel):
    job_id: int
    job_name: str
    salary: int

# GET all jobs details in array of dictionary
@app.get("/jobs/get")
def get_complete_jobdetails():
    jobsget=db.selectall(table_object)
    return jobsget

# Listing the detail of one particular job
@app.get("/jobs/{jobid}")
def get_job_details(jobid: int ):
    job_detail = db.fetchjobdetails(table_object,jobid)
    return job_detail

#Adding new job
@app.put("/jobs/post")
def job_put(newjobs: Jobs):
    db.dbinsert(table_object,newjobs.job_id ,newjobs.job_name,newjobs.salary )
    return {"jobs": newjobs.job_id, "JobName": newjobs.job_name}

#Delete one job detail from database
@app.delete("/jobs/delete/{jobid}")
def delete_job( jobid: int):
    db.deletejob(table_object,jobid)
    return {"JobidDeleted": jobid}

# Calling uvicorn run
if __name__ == "__main__":
    uvicorn.run(app,host="127.0.0.1")


"""
This is an implimentation of the structured bayesian optimisation approach based on
BOAT: Building Auto-Tuners with Structured Bayesian Optimization
Valentin Dalibard, Michael Schaarschmidt, Eiko Yoneki

"""

import copy
import numpy as np

import torch
import torch.nn as nn
from torch.distributions import constraints, transform_to
import torch.optim as optim
import torch.autograd as autograd

import pyro
import pyro.distributions as dist
import pyro.contrib.gp as gp
from pyro.nn import PyroSample, PyroModule
from pyro.infer import autoguide, SVI, Trace_ELBO

class TargetFunction:
    """A class to represent target function"""

    def __init__(self, ranges):
        """
        Constructor for a target function - processess ranges into constrains.
            ranges: range for each dimension

        """

        self.ranges = ranges

        self.contstraints = []

        for range_el in ranges:
            self.contstraints.append(
                constraints.interval(range_el[0], range_el[1]))

    def eval(self, x):
        """
        Evaluates target function any set of input points
            x: input points
        """

        raise NotImplementedError

normal_phi = lambda x: torch.exp(-x.pow(2) / 2) / np.sqrt(2 * np.pi)
normal_Phi = lambda x: (1 + torch.erf(x / np.sqrt(2))) / 2

class SemiParametricModel(PyroModule):

    def __init__(self, X, y, parametric_mean, kernel, jitter=1e-06):
        """ Defines a semi-parametric model, where the `parametric_mean` is a `PyroModule` """
        super().__init__()

        self.X = X
        self.y = y

        self.parametric_mean = parametric_mean

        self.kernel = kernel
        self.gp = gp.models.GPRegression(X, y, self.kernel, jitter=jitter)

    @pyro.nn.pyro_method
    def model(self):
        # Model definition code: explicitly subtract out the parametric mean, then call gp.model
        self.gp.set_data(self.X, self.y - self.parametric_mean(self.X))
        return self.gp.model()

    def forward(self, X):
        ''' Predict on new data points '''

        # reset "data" of GP to reflect mean estimate
        self.gp.set_data(self.X, self.y - self.parametric_mean(self.X))

        # sample mu, sigma
        mu, sigma = self.gp(X)

        # sample value of y
        y_hat = mu + self.parametric_mean(X)
        pyro.sample('y', dist.Normal(y_hat, sigma))

        # compute expected improvement
        y_min = self.y.min()
        delta = y_min - y_hat
        EI = delta.clamp_min(0.0) + sigma * normal_phi(
            delta / sigma) - delta.abs() * normal_Phi(delta / sigma)

        pyro.sample('EI', dist.Delta(-EI))

        # return the mean, in case we want to ignore the GP noise for some reason later
        return y_hat

def train(model, optimizer, loss, num_steps=1000):
    """ Trains the semi-parametric model. """

    # Autoguide
    guide = autoguide.AutoMultivariateNormal(model.model)

    svi = SVI(model.model, guide, optimizer, loss)

    # do gradient steps
    losses = []
    for _ in range(num_steps):
        losses.append(svi.step())

    return guide, losses

def transf_values(values, constr, dims, inv_mode=False):
    """ Transforming (un)constrained variables to (un)constrained domain """

    x_tmp = ()
    for i in range(dims):
        if inv_mode:
            x_tmp += (transform_to(constr[i]).inv(values[:, i]),)
        else:
            x_tmp += (transform_to(constr[i])(values[:, i]),)

    x = torch.stack(x_tmp, dim=1)

    return x

def find_a_candidate(model_predict, return_site, optimizer, x_init, constr, num_steps=1000, num_samples=5, lr=0.1):
    """ Finds new candidate """

    x_dims = x_init.shape[-1]

    x_uncon_init = transf_values(x_init, constr, x_dims, inv_mode=True)
    x_uncon = x_uncon_init.clone().detach().requires_grad_(True)

    minimizer = optimizer([x_uncon], lr=lr)

    def closure():
        minimizer.zero_grad()
        x = transf_values(x_uncon, constr, x_dims)

        y = model_predict(x)[return_site].mean(0)

        autograd.backward(x_uncon, autograd.grad(y, x_uncon))
        return y

    for _ in range(num_steps):
        minimizer.step(closure)

    x = transf_values(x_uncon, constr, x_dims)

    return x.detach()

def next_x(model_predict, return_site, optimizer, target, num_steps=1000, num_candidates=5, num_samples=5, lr=0.1):
    """ Finds the next best candidate on the acquisition function surface """

    candidates = []
    values = []

    # starts with the best candidate so far
    min_y = model_predict(model_predict.model.X)[return_site].mean(0)

    x_init = (model_predict.model.X[torch.min(min_y, dim=0)[1].item()]).unsqueeze(dim=0)

    for _ in range(num_candidates):
        x_can = find_a_candidate(model_predict, return_site,
                                 optimizer, x_init,
                                 target.contstraints,
                                 num_steps=num_steps,
                                 num_samples=num_samples,
                                 lr=lr)

        y = model_predict(x_can)[return_site].mean(0)

        candidates.append(x_can)
        values.append(y.detach())

        # a new random attempt initial point
        for _ in range(100):

            x_list = []
            for i in range(x_can.shape[-1]):
                x_list.append(x_can[:, i].new_empty(1).uniform_(
                    target.ranges[i][0], target.ranges[i][1]))

            x_init = torch.stack(x_list, dim=1)

            y_init = model_predict(x_init)[return_site].mean(0)

            # since we are minimising, we check
            if y_init < 0.0:
                break

    if num_candidates > 1:
        argmin = torch.min(torch.cat(values), dim=0)[1].item()

        return candidates[argmin]
    else:
        return candidates[0]

def update_posterior(model, optimizer, loss, obj_function, x_new, num_steps=1000):
    """
    Updates
        model:
        optimizer:
        loss:
        obj_function: objectivefunction
        x_new: a new value of x
        num_steps:

    """

    # evaluate f at new point
    bh_y = obj_function.eval(x_new)

    # incorporate new evaluation
    X = torch.cat([model.X, x_new])
    y = torch.cat([model.y, bh_y])

    model.X = X
    model.y = y

    # TODO: Check if this necessary
    model.gp.set_data(X, y)

    guide, losses = train(model, optimizer, loss, num_steps=num_steps)

    return guide, losses

def step(model,
         guide,
         optimizer,
         loss,
         target,
         acqf_optimizer,
         opti_num_steps=1000,
         acqf_opti_num_steps=1000,
         acqf_opti_lr=0.1,
         num_samples=1,
         num_candidates=5,
         return_site='EI'):
    """
    Performs a bayesian optimisation step. This includes generating a predictive model,
        which is used to find a new value of x at which the target function needs to be
        evaluated, and updating the model after the new x is obtained.

        model:
        guide:
        optimizer:
        loss:
        target:
        acqf_optimizer:
        opti_num_steps:
        acqf_opti_num_steps:
        num_samples:
        num_candidates:
        return_site:
    """

    if guide is not None:

        # TODO: copy.copy is a hack around predictive model being linked with a model
        # Constructs predictive distribution
        predict = pyro.infer.Predictive(copy.copy(model), guide=guide,
                                        num_samples=num_samples, return_sites=('y', return_site))

        # Finds the next candidate
        x_new = next_x(predict, return_site, acqf_optimizer, target,
                        num_steps=acqf_opti_num_steps, num_candidates=num_candidates,
                        num_samples=num_samples, lr=acqf_opti_lr)

        # Updates posterior
        new_guide, losses = update_posterior(model, optimizer, loss, target,
                                            x_new, num_steps=opti_num_steps)
    else:
        predict = None

        # trains the model
        new_guide, losses = train(model, optimizer, loss, num_steps=opti_num_steps)

    return new_guide, predict, losses


from rest_framework.response import Response

class APIResponse(Response):
    """
    自定义Response返回数据：
    return APIResponse(data={"name":'11111111'},request_id='11111111')
    return APIResponse(data={"name":'11111111'})
    return APIResponse(errcode='101', errmsg='错误',data={"name":'11111111'}, header={})
    """
    def __init__(self, errcode=0, errmsg=None, data=None, status=None, headers=None, **kwargs):
        dic = {'errcode': errcode, 'errmsg': errmsg}
        if data:
            dic = {'errcode': errcode, 'errmsg': errmsg, 'data': data}
        dic.update(kwargs)
        super().__init__(data=dic, status=status, headers=headers)


import pygame
from random import *
import time
import itertools
import sys
import math
from HamCycle import HCyc
pygame.init()
sys.setrecursionlimit(3000)

class Snake:
    heading = 0 # 0: Right, 1: Up, 2: Left, 3: Down
    snakeParts = []
    dirs = [[1,0],[0,-1],[-1,0],[0,1]] #Directions corresponding to Heading Vals
    speed = 1
    partSize = 0
    foodPosition = []
    def __init__(self,speed,allP,gridBSize):
        self.snakeParts = [choice(allP)]
        self.heading = 0
        self.partSize = gridBSize
        self.foodPosition = self.getFoodPos(allPos,self.snakeParts)

    def pathDist(self,start,end,hCyc):
        c = 0
        while True:
            c+=1
            if start==end:
                break
            else:
                start = hCyc[start]
        return c

    def distDict(self,possPoints,foodNode,gX,hCyc):
        d = {}
        for point in possPoints:
            node = point[0]+gX*point[1]
            d[self.pathDist(node,foodNode,hCyc)] = point
        return d

    def bestMove(self,possPoints,foodNode,gX,hCyc,hNode):
        distD = self.distDict(possPoints,foodNode,gX,hCyc)
        for dist in sorted(distD):
            point = distD[dist]
            nNode = point[0]+point[1]*gX
            if hCyc[hNode]==nNode:
                return nNode
            else:
                c = 0
                start = nNode
                while True:
                    c += 1
                    if allPos[hCyc[start]] in self.snakeParts:
                        break
                    else:
                        start = hCyc[start]
                if len(self.snakeParts)<c:
                    return nNode
        return hCyc[hNode]

    def move_forw(self,allPos,gridX,gridY, hCyc):
        head = self.snakeParts[0][:]
        hNode = head[0]+head[1]*gridX
        foodNode = self.foodPosition[0]+self.foodPosition[1]*gridX

        posP = []
        for direc in self.dirs:
            nPX = head[0]+direc[0]
            nPY = head[1]+direc[1]
            nP = [nPX,nPY]
            if 0<=nPX<gridX and 0<=nPY<gridY and nP not in self.snakeParts:
                posP.append(nP)
        nNode = self.bestMove(posP,foodNode,gridX,hCyc,hNode)
        head = allPos[nNode]

        if self.foodPosition in self.snakeParts:
            self.foodPosition = self.getFoodPos(allPos,self.snakeParts)
        else:
            self.snakeParts.pop()
        self.snakeParts=[head]+self.snakeParts

    def draw_snake_food(self,display,done):
        display.fill((0,0,0))
        fCol = (255,255,0)

        for part in range(len(self.snakeParts)):

            pygame.draw.rect(display,(0,0,0), (self.snakeParts[part][0]*self.partSize,self.snakeParts[part][1]*self.partSize,
                                                     self.partSize,self.partSize),2)
            pygame.draw.rect(display,(0,255,0), (self.snakeParts[part][0]*self.partSize+1,self.snakeParts[part][1]*self.partSize+1,
                                                     self.partSize-2,self.partSize-2))
            if part>0:
                posX = (self.snakeParts[part-1][0]*self.partSize+self.snakeParts[part][0]*self.partSize)/2+1
                posY = (self.snakeParts[part-1][1]*self.partSize+self.snakeParts[part][1]*self.partSize)/2+1
                pygame.draw.rect(display,(0,255,0), (posX,posY,
                                                     self.partSize-2,self.partSize-2))

        if not done:
            pygame.draw.rect(display,fCol, (self.foodPosition[0]*self.partSize,self.foodPosition[1]*self.partSize,
                                              self.partSize,self.partSize))

    def change_heading(self,key):
        if key==pygame.K_RIGHT:
            self.heading = 0
        elif key==pygame.K_UP:
            self.heading = 1
        elif key==pygame.K_LEFT:
            self.heading = 2
        elif key==pygame.K_DOWN:
            self.heading = 3

    def getFoodPos(self,allPositions,posTaken):
        allPo = allPositions[:]
        shuffle(allPo)
        for pos in allPo:
            if pos not in posTaken:
                return [pos[0],pos[1]]
        return [0,0] #no positions left. perfect snake.

    def containsDups(self, lis):
        for i in range(len(lis)-1):
            if lis[i] in lis[i+1:]:
                return True
        return False

    def check_death(self, gX, gY):
        head = self.snakeParts[0]
        if self.containsDups(self.snakeParts):
            return True
        elif not (0<=head[0]<gX and 0<=head[1]<gY):
            return True
        else:
                return False

gridX = 20
gridY = 20
gridBSize= 20
screen = pygame.display.set_mode([gridX*gridBSize,gridY*gridBSize])

hCyc = HCyc(gridX,gridY).hcyc

allPos = []
for y in range(gridY):
    for x in range(gridX):
        allPos.append([x,y])

mySnake = Snake(1,allPos,gridBSize)
running = True
gameDone = False
speed = 1
start = 0
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    start = time.time()
    if mySnake.check_death(gridX,gridY):
        gameDone = True
    for i in range(speed):
        if not gameDone:
            mySnake.move_forw(allPos,gridX,gridY,hCyc)
        mySnake.draw_snake_food(screen,gameDone)
    pygame.display.flip()
pygame.quit()

Classic Snake AI/Hamiltonian Method/game-with-ai.py


import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    maxDiff = None
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_Sample_Input_1(self):
        input = """105"""
        output = """1"""
        self.assertIO(input, output)

    def test_Sample_Input_2(self):
        input = """7"""
        output = """0"""
        self.assertIO(input, output)

def resolve():
  def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
      if n % i == 0:
        lower_divisors.append(i)
        if i != n // i:
          upper_divisors.append(n//i)
      i += 1
    return lower_divisors + upper_divisors[::-1]

  N = int(input())

  ans = len([i for i in range(1, N+1, 2) if len(make_divisors(i)) == 8])
  print(ans)

import sys
if sys.argv[-1] == './Main.py':
  resolve()

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


'''
basic functions

'''
import sys
import numpy as np
import torch
import cupy as cp

class Timer():
    def __init__(self,proc):
        self._begin_time = None
        self.proc = proc

    def tic(self):
        print(f'{self.proc} start!')
        self._begin_time = time.perf_counter()

    def toc(self):
        print(f'{self.proc} finish!,consuming {time.perf_counter() - self._begin_time}')
        return time.perf_counter() - self._begin_time

def binary_conversion(var):
    """
    二进制单位转换
    :param var: 需要计算的变量，bytes值
    :return: 单位转换后的变量，kb 或 mb
    """
    var_size = sys.getsizeof(var)
    assert isinstance(var_size, int)
    if var_size <= 1024:
        return f'占用 {round(var_size / 1024, 2)} KB内存'
    else:
        return f'占用 {round(var_size / (1024 ** 2), 2)} MB内存'

def show_info(show_OI = False,show_values = False,**kws):
    length = 'no length'
    keys = 'not a dict'
    values = 'not a dict'
    shape = 'not a ndarray'
    size = 'not a ndarray'
    for key,x in kws.items():
        if hasattr(x,'__len__'):
            length = len(x)
        if isinstance(x,dict):
            keys = x.keys()
            values = x.values()
        if type(x) is np.ndarray or type(x) is cp.ndarray or type(x) is torch.Tensor:
            shape = x.shape
            size = x.size
        print(key)
        print(f'allocated memory:{binary_conversion(x)}')
        print(f'\ntype:{type(x)} \nlen:{length}\nshape:{shape}\nsize:{size}\nkeys:{keys}\n')
        if show_OI:
            print(f'original info:{x}\n')
        if show_values:
            print(f'\nvalues:{values}\n')


#Entradas#
MENSAJE_BIENVENIDA = "Vamos a jugar :D"
PREGUNTA_NUMERO ='''
        En este juego debes acertar un número
        que va desde el 1 hasta al 10, intente
        hasta acabar sus vidas
        ¡Éxitos!, ingresa tu número:
'''
PREGUNTA_DIFICULTAD = '''
    1-Fácil
    2-Moderado
    3-Difícil
'''
PREGUNTA_FALLIDA = "Aahhh! Fallaste ;c, ingresa otro número: "
MENSAJE_GANAR = "Felicidades, has ganado!!"
MENSAJE_PERDER = "Perdiste, vuelva a intentarlo"
MENSAJE_CONTINUAR = ''' Aún te quedan vidas,
                     ¿deseas continuar jugando?
                      Responde
                      1- Para si
                      2- Para no
'''
PREGUNTA_NUMERODOS = "Ingrese un nuevo número: "
import random
#Entradas de código#
numerooculto = random.randint (1,10)
vidas = None
print (MENSAJE_BIENVENIDA)
dificultad =int (input(PREGUNTA_DIFICULTAD))
while (dificultad !=1 and dificultad !=2 and dificultad !=3):
    print ("Ingrese una opción valida")
    dificultad =int (input(PREGUNTA_DIFICULTAD))

if (dificultad==1):
    vidas = 5
elif (dificultad==2):
    vidas = 3
else:
    vidas=1
print (f"Tienes {vidas} vidas")
numeroingresado= int(input(PREGUNTA_NUMERO))
if (numeroingresado != numerooculto):
    vidas = vidas - 1
while (numerooculto != numeroingresado and vidas > 0):
    numeroingresado =int (input(PREGUNTA_FALLIDA))
    vidas = vidas - 1

if (vidas >= 0 and numeroingresado==numerooculto):
    print (MENSAJE_GANAR)
    print (f"Te quedaron {vidas} vidas")
else:
    print (MENSAJE_PERDER, "el número era el",numerooculto)

if (vidas>0 and numeroingresado == numerooculto):
    continuar = int(input(MENSAJE_CONTINUAR))
    if (continuar == 1):
        numerooculto = random.randint (1,10)
        print (f"Tienes {vidas} vidas")
        numeroingresado= int(input(PREGUNTA_NUMERODOS))
        if (numeroingresado != numerooculto):
            vidas = vidas - 1
        while (numerooculto != numeroingresado and vidas > 0):
            numeroingresado =int (input(PREGUNTA_FALLIDA))
            vidas = vidas - 1
        if (vidas >= 0 and numeroingresado==numerooculto):
            print (MENSAJE_GANAR)
            print (f"Te quedaron {vidas} vidas")
        else:
            print (MENSAJE_PERDER, "el número era el",numerooculto)
    elif (continuar == 2):
        print ("Gracias por jugar!!!")
    else:
        print ("Respuesta invalida")
else:
    print ("Ya no te quedan vidas, vuelva a intentarlo")