import os
import unittest

import numpy as np
from common import (
    j_loader,
)

from deepmd.descriptor import (
    DescrptSeA,
)
from deepmd.env import (
    tf,
)

GLOBAL_ENER_FLOAT_PRECISION = tf.float64
GLOBAL_TF_FLOAT_PRECISION = tf.float64
GLOBAL_NP_FLOAT_PRECISION = np.float64

#
from common import (
    tests_path,
)

from deepmd.nvnmd.data.data import (
    jdata_deepmd_input,
)
from deepmd.nvnmd.utils.config import (
    nvnmd_cfg,
)

class TestModel(tf.test.TestCase):
    def setUp(self):
        # generate data of GeTe
        a = 3.01585  # crystal constant
        natom = 64
        box = np.zeros([9])
        box[0] = a * 4
        box[4] = a * 4
        box[8] = a * 4
        box = box.reshape([-1, 9])
        types = np.zeros([natom], dtype=np.int32)
        types[32:] = 1
        types = types.reshape([1, natom])
        coord = np.zeros([natom, 3])
        ct = 0
        ct2 = 32
        for ix in range(4):
            for iy in range(4):
                for iz in range(4):
                    if (ix + iy + iz) % 2 == 0:
                        coord[ct] = np.array([ix * a, iy * a, iz * a])
                        ct += 1
                    else:
                        coord[ct2] = np.array([ix * a, iy * a, iz * a])
                        ct2 += 1
        coord = coord.reshape([1, natom * 3])
        natoms = np.array([64, 64, 32, 32])
        mesh = np.array([0, 0, 0, 2, 2, 2])
        #
        self.box = box
        self.types = types
        self.coord = coord
        self.natoms = natoms
        self.mesh = mesh

    def test_descriptor_one_side_qnn(self):
        """: test se_a of NVNMD with quantized value.

        Reference:
            test_descrpt_se_a_type.py

        Note:
        ----
            The test_nvnmd_se_a.py must be run after test_nvnmd_entrypoints.py.
            Because the data file map.npy ia generated in running test_nvnmd_entrypoints.py.
        """
        tf.reset_default_graph()
        # open NVNMD
        jdata_cf = jdata_deepmd_input["nvnmd"]
        jdata_cf["config_file"] = str(tests_path / os.path.join("nvnmd", "config.npy"))
        jdata_cf["weight_file"] = str(tests_path / os.path.join("nvnmd", "weight.npy"))
        jdata_cf["map_file"] = str(tests_path / os.path.join("nvnmd", "map.npy"))
        jdata_cf["enable"] = True
        nvnmd_cfg.init_from_jdata(jdata_cf)
        nvnmd_cfg.quantize_descriptor = True
        nvnmd_cfg.restore_descriptor = True
        # load input
        jfile = str(tests_path / os.path.join("nvnmd", "train_cnn.json"))
        jdata = j_loader(jfile)
        ntypes = nvnmd_cfg.dscp["ntype"]

        # build descriptor
        jdata["model"]["descriptor"].pop("type", None)
        descrpt = DescrptSeA(**jdata["model"]["descriptor"], uniform_seed=True)

        t_coord = tf.placeholder(
            GLOBAL_TF_FLOAT_PRECISION, [None, None], name="i_coord"
        )
        t_type = tf.placeholder(tf.int32, [None, None], name="i_type")
        t_natoms = tf.placeholder(tf.int32, [ntypes + 2], name="i_natoms")
        t_box = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None, 9], name="i_box")
        t_mesh = tf.placeholder(tf.int32, [None], name="i_mesh")
        is_training = tf.placeholder(tf.bool)

        dout = descrpt.build(
            t_coord,
            t_type,
            t_natoms,
            t_box,
            t_mesh,
            {},
            reuse=False,
            suffix="_se_a_nvnmd",
        )
        # data
        feed_dict_test = {
            t_coord: self.coord,
            t_box: self.box,
            t_type: self.types,
            t_natoms: self.natoms,
            t_mesh: self.mesh,
            is_training: False,
        }
        # run
        sess = self.test_session().__enter__()
        sess.run(tf.global_variables_initializer())
        [model_dout] = sess.run([dout], feed_dict=feed_dict_test)
        model_dout = model_dout.reshape([-1])
        # compare
        ref_dout = [
            0.0136348009,
            0.0083287954,
            -0.0639075041,
            0.0129179955,
            0.0050876141,
            -0.0390378237,
            0.0078909397,
            0.0022796392,
            0.2995386124,
            -0.0605479479,
        ]
        places = 10
        np.testing.assert_almost_equal(model_dout[0:10], ref_dout, places)
        # close NVNMD
        jdata_cf["enable"] = False
        nvnmd_cfg.init_from_jdata(jdata_cf)

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


import functools
import types
import typing
from http import client
from timeit import default_timer
from typing import Collection, Dict
from urllib.request import (  # pylint: disable=no-name-in-module,import-error
    OpenerDirector,
    Request,
)

from opentelemetry import context

# FIXME: fix the importing of this private attribute when the location of the _SUPPRESS_HTTP_INSTRUMENTATION_KEY is defined.
from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.instrumentation.urllib.package import _instruments
from opentelemetry.instrumentation.urllib.version import __version__
from opentelemetry.instrumentation.utils import (
    _SUPPRESS_INSTRUMENTATION_KEY,
    http_status_to_status_code,
)
from opentelemetry.metrics import Histogram, get_meter
from opentelemetry.propagate import inject
from opentelemetry.semconv.metrics import MetricInstruments
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.trace import Span, SpanKind, get_tracer
from opentelemetry.trace.status import Status
from opentelemetry.util.http import (
    ExcludeList,
    get_excluded_urls,
    parse_excluded_urls,
    remove_url_credentials,
)

_excluded_urls_from_env = get_excluded_urls("URLLIB")

_RequestHookT = typing.Optional[typing.Callable[[Span, Request], None]]
_ResponseHookT = typing.Optional[
    typing.Callable[[Span, Request, client.HTTPResponse], None]
]

class URLLibInstrumentor(BaseInstrumentor):
    """An instrumentor for urllib
    See `BaseInstrumentor`
    """

    def instrumentation_dependencies(self) -> Collection[str]:
        return _instruments

    def _instrument(self, **kwargs):
        """Instruments urllib module

        Args:
            **kwargs: Optional arguments
                ``tracer_provider``: a TracerProvider, defaults to global
                ``request_hook``: An optional callback invoked that is invoked right after a span is created.
                ``response_hook``: An optional callback which is invoked right before the span is finished processing a response
                ``excluded_urls``: A string containing a comma-delimited
                    list of regexes used to exclude URLs from tracking
        """
        tracer_provider = kwargs.get("tracer_provider")
        tracer = get_tracer(
            __name__,
            __version__,
            tracer_provider,
            schema_url="https://opentelemetry.io/schemas/1.11.0",
        )
        excluded_urls = kwargs.get("excluded_urls")
        meter_provider = kwargs.get("meter_provider")
        meter = get_meter(
            __name__,
            __version__,
            meter_provider,
            schema_url="https://opentelemetry.io/schemas/1.11.0",
        )

        histograms = _create_client_histograms(meter)

        _instrument(
            tracer,
            histograms,
            request_hook=kwargs.get("request_hook"),
            response_hook=kwargs.get("response_hook"),
            excluded_urls=_excluded_urls_from_env
            if excluded_urls is None
            else parse_excluded_urls(excluded_urls),
        )

    def _uninstrument(self, **kwargs):
        _uninstrument()

    def uninstrument_opener(
        self, opener: OpenerDirector
    ):  # pylint: disable=no-self-use
        """uninstrument_opener a specific instance of urllib.request.OpenerDirector"""
        _uninstrument_from(opener, restore_as_bound_func=True)

def _instrument(
    tracer,
    histograms: Dict[str, Histogram],
    request_hook: _RequestHookT = None,
    response_hook: _ResponseHookT = None,
    excluded_urls: ExcludeList = None,
):
    """Enables tracing of all requests calls that go through
    :code:`urllib.Client._make_request`"""

    opener_open = OpenerDirector.open

    @functools.wraps(opener_open)
    def instrumented_open(opener, fullurl, data=None, timeout=None):
        if isinstance(fullurl, str):
            request_ = Request(fullurl, data)
        else:
            request_ = fullurl

        def get_or_create_headers():
            return getattr(request_, "headers", {})

        def call_wrapped():
            return opener_open(opener, request_, data=data, timeout=timeout)

        return _instrumented_open_call(
            opener, request_, call_wrapped, get_or_create_headers
        )

    def _instrumented_open_call(
        _, request, call_wrapped, get_or_create_headers
    ):  # pylint: disable=too-many-locals
        if context.get_value(
            _SUPPRESS_INSTRUMENTATION_KEY
        ) or context.get_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY):
            return call_wrapped()

        url = request.full_url
        if excluded_urls and excluded_urls.url_disabled(url):
            return call_wrapped()

        method = request.get_method().upper()

        span_name = method.strip()

        url = remove_url_credentials(url)

        labels = {
            SpanAttributes.HTTP_METHOD: method,
            SpanAttributes.HTTP_URL: url,
        }

        with tracer.start_as_current_span(
            span_name, kind=SpanKind.CLIENT, attributes=labels
        ) as span:
            exception = None
            if callable(request_hook):
                request_hook(span, request)

            headers = get_or_create_headers()
            inject(headers)

            token = context.attach(
                context.set_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY, True)
            )
            start_time = default_timer()
            try:
                result = call_wrapped()  # *** PROCEED
            except Exception as exc:  # pylint: disable=W0703
                exception = exc
                result = getattr(exc, "file", None)
            finally:
                elapsed_time = round((default_timer() - start_time) * 1000)
                context.detach(token)

            if result is not None:
                code_ = result.getcode()
                labels[SpanAttributes.HTTP_STATUS_CODE] = str(code_)

                if span.is_recording() and code_ is not None:
                    span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, code_)
                    span.set_status(Status(http_status_to_status_code(code_)))

                ver_ = str(getattr(result, "version", ""))
                if ver_:
                    labels[
                        SpanAttributes.HTTP_FLAVOR
                    ] = f"{ver_[:1]}.{ver_[:-1]}"

            _record_histograms(
                histograms, labels, request, result, elapsed_time
            )

            if callable(response_hook):
                response_hook(span, request, result)

            if exception is not None:
                raise exception.with_traceback(exception.__traceback__)

        return result

    instrumented_open.opentelemetry_instrumentation_urllib_applied = True
    OpenerDirector.open = instrumented_open

def _uninstrument():
    """Disables instrumentation of :code:`urllib` through this module.

    Note that this only works if no other module also patches urllib."""
    _uninstrument_from(OpenerDirector)

def _uninstrument_from(instr_root, restore_as_bound_func=False):
    instr_func_name = "open"
    instr_func = getattr(instr_root, instr_func_name)
    if not getattr(
        instr_func,
        "opentelemetry_instrumentation_urllib_applied",
        False,
    ):
        return

    original = instr_func.__wrapped__  # pylint:disable=no-member
    if restore_as_bound_func:
        original = types.MethodType(original, instr_root)
    setattr(instr_root, instr_func_name, original)

def _create_client_histograms(meter) -> Dict[str, Histogram]:
    histograms = {
        MetricInstruments.HTTP_CLIENT_DURATION: meter.create_histogram(
            name=MetricInstruments.HTTP_CLIENT_DURATION,
            unit="ms",
            description="Measures the duration of outbound HTTP requests.",
        ),
        MetricInstruments.HTTP_CLIENT_REQUEST_SIZE: meter.create_histogram(
            name=MetricInstruments.HTTP_CLIENT_REQUEST_SIZE,
            unit="By",
            description="Measures the size of HTTP request messages.",
        ),
        MetricInstruments.HTTP_CLIENT_RESPONSE_SIZE: meter.create_histogram(
            name=MetricInstruments.HTTP_CLIENT_RESPONSE_SIZE,
            unit="By",
            description="Measures the size of HTTP response messages.",
        ),
    }

    return histograms

def _record_histograms(
    histograms, metric_attributes, request, response, elapsed_time
):
    histograms[MetricInstruments.HTTP_CLIENT_DURATION].record(
        elapsed_time, attributes=metric_attributes
    )

    data = getattr(request, "data", None)
    request_size = 0 if data is None else len(data)
    histograms[MetricInstruments.HTTP_CLIENT_REQUEST_SIZE].record(
        request_size, attributes=metric_attributes
    )

    if response is not None:
        response_size = int(response.headers.get("Content-Length", 0))
        histograms[MetricInstruments.HTTP_CLIENT_RESPONSE_SIZE].record(
            response_size, attributes=metric_attributes
        )


from patchman.models import Plate,Brand, PlateLog, DBSession
from formencode import validators
from formencode.schema import Schema
from pyramid.httpexceptions import HTTPFound
from pyramid.renderers import render_to_response
from pyramid.view import view_config
from pyramid_uniform import Form, FormRenderer
from sqlalchemy.exc import IntegrityError
from webhelpers import paginate
from webhelpers.paginate import Page
import logging
import transaction

log = logging.getLogger(__name__)

class PlateForm(Schema):
    """ plate form schema for validation
        TODO: DRY code, sqlalchemy model validation instead?
    """
    filter_extra_fields = True
    allow_extra_fields = True
    code = validators.String(not_empty=True)
    brand_id = validators.Int()
    active = validators.Bool()
    notes = validators.String()

@view_config(route_name="plate_list")
def list(request):
    """plates list """
    search = request.params.get("search", "")

    sort= "code"
    if request.GET.get("sort") and request.GET.get("sort") in ["code", "brand"]:
        sort = request.GET.get("sort")
    if sort == "brand":
        sort = "brand.name"

    direction = "asc"
    if request.GET.get("direction") and request.GET.get("direction") in ["asc", "desc"]:
        direction = request.GET.get("direction")

    # db query
    dbsession = DBSession()
    query = dbsession.query(Plate).outerjoin(Brand).\
        filter(Plate.code.like(search + "%")).\
                   order_by(sort + " " + direction)

    # paginate
    page_url = paginate.PageURL_WebOb(request)
    plates = Page(query,
                     page=int(request.params.get("page", 1)),
                     items_per_page=10,
                     url=page_url)

    if "partial" in request.params:
        # Render the partial list page
        return render_to_response("plate/listPartial.html",
                                  {"plates": plates},
                                  request=request)
    else:
        # Render the full list page
        return render_to_response("plate/list.html",
                                  {"plates": plates},
                                  request=request)

@view_config(route_name="plate_search")
def search(request):
    """plates list searching """
    sort = request.GET.get("sort") if request.GET.get("sort") else "code"
    direction = "desc" if request.GET.get("direction") == "asc" else "asc"
    query = {"sort": sort, "direction": direction}

    return HTTPFound(location = request.route_url("plate_list", _query=query))

@view_config(route_name="plate_new", renderer="plate/new.html", permission="add")
def new(request):
    """new plate """
    brands = get_brands()

    form = Form(request, schema=PlateForm)
    if "form_submitted" in request.POST and form.validate():
        dbsession = DBSession()
        plate = form.bind(Plate())
        existing = dbsession.query(Plate).filter_by(code=plate.code).count()
        if not existing:
            dbsession.add(plate)
            request.session.flash("warning;Nueva Patente guardada!")
        else:
            request.session.flash("error; Ya existe la patente!")
        return HTTPFound(location = request.route_url("plate_list"))

    return dict(form=FormRenderer(form),
                brands=brands,
                action_url=request.route_url("plate_new"))

@view_config(route_name="plate_edit", renderer="plate/edit.html", permission="edit")
def edit(request):
    """plate edit """
    id = request.matchdict['id']
    dbsession = DBSession()
    plate = dbsession.query(Plate).filter_by(id=id).first()
    if plate is None:
        request.session.flash("error;Patente no encontrada!")
        return HTTPFound(location=request.route_url("plate_list"))

    brands = get_brands()

    form = Form(request, schema=PlateForm, obj=plate)
    if "form_submitted" in request.POST and form.validate():
        form.bind(plate)
        dbsession.add(plate)
        request.session.flash("warning;Se guardo la patente!")
        return HTTPFound(location = request.route_url("plate_list"))

    action_url = request.route_url("plate_edit", id=id)
    return dict(form=FormRenderer(form),
                brands=brands,
                action_url=action_url)

@view_config(route_name="plate_delete", permission="delete")
def delete(request):
    """plate delete """
    id = request.matchdict['id']
    dbsession = DBSession()
    plate = dbsession.query(Plate).filter_by(id=id).first()
    if plate is None:
        request.session.flash("error;Patente no encontrada!")
        return HTTPFound(location=request.route_url("plate_list"))

    try:
        transaction.begin()
        dbsession.delete(plate);
        transaction.commit()
        request.session.flash("warning;Se elimino la patente!")
    except IntegrityError:
        # delete error
        transaction.abort()
        request.session.flash("error;La patente no se pudo eliminar!")

    return HTTPFound(location=request.route_url("plate_list"))

def get_brands():
    """Gets all brands with id, name value pairs """
    dbsession = DBSession()
    brands_q = dbsession.query(Brand).order_by(Brand.name)
    brands = [(brand.id, brand.name) for brand in brands_q.all()]

    return brands


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#----------------------------------------------------------------------------
# Created By  : SpeedCode210
# version ='2.0'
# System dependencies : tar, gzip
# Pip dependencies : Pillow
# ---------------------------------------------------------------------------
"""Builds a Flatpak source archive from binaries"""
# ---------------------------------------------------------------------------
import config
import os
from PIL import Image

def generate_flatpak_source():
    try:
        print("-"*os.get_terminal_size().columns)
    except:
        print("-------------------------------------------")
    print("  SmartPackager - Building Flatpak sources archive")
    try:
        print("-"*os.get_terminal_size().columns)
    except:
        print("-------------------------------------------")
    path = f"/tmp/flatpaksource"
    os.system(f"mkdir -p {path}")
    os.system(f'cp -R {config.BASE_DIR}/* {path}')
    if os.path.exists(f"{config.BASE_DIR}/App.png") :
        image = Image.open(f"{config.BASE_DIR}/App.png")
        new_image = image.resize((128, 128))
        new_image.save(f'{path}/128.png')
        new_image = image.resize((64, 64))
        new_image.save(f'{path}/64.png')
    os.system(f"sh -c 'cd {path} && tar -czvf /tmp/{config.NAME.lower()}-source-{config.VERSION}.tar.gz ./'")
    os.system(f"cp /tmp/{config.NAME.lower()}-source-{config.VERSION}.tar.gz {config.OUTPUT_FOLDER}")
    os.system(f"rm -rf {path}")

if __name__ == "__main__":
    generate_flatpak_source()


#Step 1
import random
import hangman_art as art
import hangman_words as words

print(art.logo)

chosen_word = random.choice(words.word_list)

lives = 6
end_of_game = False

image = "_"
display = []

for word in chosen_word:
    display.append(image)

count = 0
while not end_of_game:
    print(f"hint {chosen_word}")
    guess = input("Guess a letter:").lower()

    if guess in chosen_word:
        print(f"You've already guessed {guess}")

    for chose in chosen_word:
        if guess == chose:
            display[count] = (guess)
            print(display)
        count += 1

    if image in display:
        # guess = input("Guess a letter:").lower()
        count = 0

    if image not in display:
        end_of_game = True
        print("You win")

    if guess not in chosen_word:
        print(f"You guessed {guess}, that's not in the word. You lose a life")
        lives -= 1
        print(display)
        if lives == 0:
            print("You lost")
            end_of_game = True

    print(art.stages[lives])


import os
from configparser import ConfigParser
import threading
import signal
import csv
import zmq
import random
import time

def main():
    """
    This data feeder simulates the idea of having a constant stream of data entering
    our system, by reading csv files and pushing them row by row (as dicts) to the DAG
    source.

    It reads from two csvs (posts and comments) and sends them, indiscriminately (the
    DAG will then sort them out according to their type)
    """

    config = ConfigParser()
    config.read("./config.ini")

    # We can set up an env var 'TEST_LINES' to limit the number of rows sent
    test_lines = int(os.environ.get("TEST_LINES", 0))
    source_host = (
        config["NETWORK"]["source_hostname"],
        int(config["NETWORK"]["source_port"]),
    )
    posts_file, comments_file = (
        config["FEEDER"]["posts_file"],
        config["FEEDER"]["comments_file"],
    )

    # Have a dedicated event to track if we are shutting down (on SIGTERMs) and need to
    # stop our loop
    is_shutdown = threading.Event()
    signal.signal(signal.SIGTERM, lambda _n, _f: is_shutdown.set())

    context = zmq.Context()
    socket = context.socket(zmq.PUSH)
    socket.connect(f"tcp://{source_host[0]}:{source_host[1]}")

    with open(posts_file) as posts, open(comments_file) as comments:
        posts_reader = csv.DictReader(
            posts, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL
        )
        comments_reader = csv.DictReader(
            comments, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL
        )

        comments_row, posts_row = None, None
        i, j = 0, 0
        while not is_shutdown.is_set():
            # We randomly send data to the system
            # We prefer sending posts over comments as those are more valuable to the
            # system, but in reality we should have a bigger influx of comments than
            # posts
            coin_toss = random.random()
            if coin_toss <= 0.75:
                posts_row = next(posts_reader, None)
                if posts_row:
                    socket.send_json(posts_row)
                    i += 1
            else:
                comments_row = next(comments_reader, None)
                if comments_row:
                    socket.send_json(comments_row)
                    j += 1

            # Breaking conditions:
            # - SIGTERM
            if is_shutdown.is_set():
                break
            # - Finished sending our n test lines
            if test_lines and (i + j >= test_lines):
                break
            # - End of file for both csvs
            if not comments_row and not posts_row:
                break
        # Wait a bit before shutting down
        time.sleep(5)

    print(f"Finished sending {i+j} lines from {posts_file} and {comments_file}")
    print(f"Sending EOF")
    socket.send_json({'type': 'EOF'})

    socket.setsockopt(zmq.LINGER, 0)
    socket.close()
    context.term()

if __name__ == "__main__":
    main()


import queue
import time

import pytest

from johnsnowlabs import nlp, settings
from johnsnowlabs.auto_install.databricks.install_utils import (
    cluster_exist_with_name_and_runtime,
    get_db_client_for_token,
    get_cluster_id,
    wait_till_cluster_running,
    _get_cluster_id,
)
from johnsnowlabs.auto_install.health_checks.generate_endpoint_test import (
    generate_endpoint_test,
)
from johnsnowlabs.utils.enums import DatabricksClusterStates
from tests.utilsz import secrets as sct

@pytest.fixture()
def azure_gpu_node_type():
    return "Standard_NC4as_T4_v3"

@pytest.fixture()
def azure_cpu_node():
    return "Standard_DS3_v2"

@pytest.fixture()
def aws_cpu_node():
    return "i3.xlarge"

@pytest.fixture()
def azure_creds():
    import tests.utilsz.secrets as sct

    lic = sct.db_lic_azure
    host = sct.ckl_host_azure
    token = sct.ckl_token_azure
    return lic, host, token

@pytest.fixture()
def azure_trial_creds():
    import tests.utilsz.secrets as sct

    lic = sct.azure_trail_lic2
    host = sct.azure_trail_host2
    token = sct.azure_trail_token2
    return lic, host, token

@pytest.fixture()
def aws_creds():
    import tests.utilsz.secrets as sct

    lic = sct.ckl_lic_aws
    host = sct.ckl_host_aws
    token = sct.ckl_token_aws
    return lic, host, token

@pytest.fixture
def creds(request):
    return request.getfixturevalue(request.param)

@pytest.fixture
def node_type(request):
    return request.getfixturevalue(request.param)

def host_creds(host):
    if host == "aws":
        return aws_creds()
    elif host == "azure":
        return azure_creds()
    else:
        raise Exception("Unknown host")

def assert_job_suc(state):
    assert state["state"]["result_state"] == "SUCCESS"

def run_endpoint_tests(test_cluster_id, host, token, model):
    # generate job.py script and submit it
    assert_job_suc(
        nlp.run_in_databricks(
            generate_endpoint_test(model, sct.container_lic_json),
            databricks_cluster_id=test_cluster_id,
            databricks_host=host,
            databricks_token=token,
            run_name=f"endpoint_creation_test_run_{model}",
        )
    )

def run_cluster_test_suite(test_cluster_id, host, token):
    # run test suite again a existing cluster

    # Test modules
    import johnsnowlabs.auto_install.health_checks.hc_test as hc_test
    import johnsnowlabs.auto_install.health_checks.ocr_test as ocr_test
    import johnsnowlabs.auto_install.health_checks.nlp_test as nlp_test

    assert_job_suc(
        nlp.run_in_databricks(
            nlp_test,
            databricks_cluster_id=test_cluster_id,
            databricks_host=host,
            databricks_token=token,
            run_name="nlp_test",
        )
    )

    assert_job_suc(
        nlp.run_in_databricks(
            hc_test,
            databricks_cluster_id=test_cluster_id,
            databricks_host=host,
            databricks_token=token,
            run_name="hc_test",
        )
    )

    assert_job_suc(
        nlp.run_in_databricks(
            ocr_test,
            databricks_cluster_id=test_cluster_id,
            databricks_host=host,
            databricks_token=token,
            run_name="ocr_test",
        )
    )

def get_one_model_per_class():
    # todo actually generate from spellbook
    return ["sentiment", "ner", "spell", "bert", "elmo", "albert", "roberta"]

def delete_all_test_clusters(db_client):
    for i in range(10):
        while _get_cluster_id(db_client, f"TEST_CLUSTER_{i}"):
            cluster_id = _get_cluster_id(db_client, f"TEST_CLUSTER_{i}")
            db_client.cluster.permanent_delete_cluster(cluster_id)
            print(f"Deleted cluster {cluster_id}")
            time.sleep(5)

def get_or_create_test_cluster(
    creds, node_type, n=0, runtime=None, clean_workspace=False
):
    """
    Creates a cluster with name TEST_CLUSTER_{n} and runtime runtime if it doesn't exist.
    If it exists, it checks if it's running and if not, it starts it.
    "exists" means another cluster with same name and runtime exists.
    """
    lic, host, token = creds
    # runtime = "9.1.x-scala2.12"
    if runtime is None:
        runtime = settings.db_spark_version
    cluster_name = f"TEST_CLUSTER_{n}"
    db_client = get_db_client_for_token(host, token)

    # 1) Check if cluster with name and correct runtime exists and it's running state If not, create it.
    if cluster_exist_with_name_and_runtime(db_client, cluster_name, runtime):
        cluster_id = get_cluster_id(db_client, cluster_name, runtime)

        status = DatabricksClusterStates(
            db_client.cluster.get_cluster(
                cluster_id,
            )["state"]
        )

        # if status is terminated, try restart it
        if status == DatabricksClusterStates.TERMINATED:
            db_client.cluster.start_cluster(cluster_id=cluster_id)
            wait_till_cluster_running(db_client, cluster_id)
            status = DatabricksClusterStates(
                db_client.cluster.get_cluster(
                    cluster_id,
                )["state"]
            )
        if status == DatabricksClusterStates.RUNNING:
            return cluster_id
        else:
            print(
                f"Could not find or start {cluster_name} with runtime {runtime} creating a new one"
            )

    else:
        #  no cluster exists, create new one
        cluster_id = nlp.install(
            spark_version=runtime,
            json_license_path=lic,
            databricks_host=host,
            databricks_token=token,
            driver_node_type_id=node_type,
            node_type_id=node_type,
            cluster_name=cluster_name,
            clean_cluster=clean_workspace,
            block_till_cluster_ready=True,
        )
        return cluster_id

def subtester_thread(cluster_id, job_que, host, token, results):
    while not job_que.empty():
        try:
            model = job_que.get_nowait()
        except queue.Empty:
            return
        try:
            run_endpoint_tests(cluster_id, host, token, model)
            print(f"{model} success!")
            results[model] = "SUCCESS"
        except Exception as e:
            print(f"{model} failed!")
            results[model] = f"FAILED: {str(e)}"

db_cloud_node_params = pytest.mark.parametrize(
    "creds, node_type",
    [
        ("aws_creds", "aws_cpu_node"),
        ("azure_creds", "azure_cpu_node"),
        # ("azure_creds", "azure_gpu_node_type"),
    ],
    indirect=["creds", "node_type"],
)
db_cloud_params = pytest.mark.parametrize(
    "creds",
    [
        "aws_creds",
        "azure_creds",
    ],
    indirect=["creds"],
)


from sc2.bot_ai import BotAI, Race
from sc2.data import Race, Difficulty
from sc2.player import Bot, Computer
from sc2.position import Point2
from sc2.unit import Unit
from sc2.units import Units
from sc2.ids.unit_typeid import UnitTypeId
from sc2.constants import *
from sc2.ids.ability_id import AbilityId
from sc2 import maps
from sc2.bot_ai import BotAI
from sc2.ids.buff_id import BuffId
import asyncio

class bot_tech:
    bot: BotAI

    def __init__(self, bot: BotAI):
        self.bot = bot

    # method to research the upgrades at the forge
    async def research(self):
        # Research warp gate if cybercore is completed
        if (
            self.bot.structures(UnitTypeId.CYBERNETICSCORE).ready
            and self.bot.can_afford(AbilityId.RESEARCH_WARPGATE)
            and self.bot.already_pending_upgrade(UpgradeId.WARPGATERESEARCH) == 0
            and self.bot.startingGame_rusherBuilt >= 2
        ):
            ccore = self.bot.structures(UnitTypeId.CYBERNETICSCORE).ready.first
            ccore.research(UpgradeId.WARPGATERESEARCH)
        # Research blink if VC is completed
        if (
            self.bot.structures(UnitTypeId.TWILIGHTCOUNCIL).ready
            and self.bot.can_afford(AbilityId.RESEARCH_BLINK)
            and self.bot.already_pending_upgrade(UpgradeId.BLINKTECH) == 0
        ):
            vc = self.bot.structures(UnitTypeId.TWILIGHTCOUNCIL).ready.first
            vc.research(UpgradeId.BLINKTECH)

        if (
            self.bot.structures(UnitTypeId.TWILIGHTCOUNCIL).ready
            and self.bot.can_afford(AbilityId.RESEARCH_CHARGE)
            and self.bot.already_pending_upgrade(UpgradeId.CHARGE) == 0
            and self.bot.already_pending_upgrade(UpgradeId.BLINKTECH) == 1
        ):
            vc = self.bot.structures(UnitTypeId.TWILIGHTCOUNCIL).ready.first
            vc.research(UpgradeId.CHARGE)

        if self.bot.structures(UnitTypeId.FORGE).ready:
            # we get our forge and if we can afford and not already upgrading we cycle through the upgrades until lvl 2
            forge = self.bot.structures(UnitTypeId.FORGE).ready.first
            nexuses = self.bot.structures(UnitTypeId.NEXUS).ready
            if not forge.is_idle:
                for loop_nexus in nexuses:
                    if loop_nexus.energy >= 50:
                        loop_nexus(AbilityId.EFFECT_CHRONOBOOSTENERGYCOST, forge)
                        break
            await self.reseach_forge(
                AbilityId.FORGERESEARCH_PROTOSSGROUNDWEAPONSLEVEL1,
                UpgradeId.PROTOSSGROUNDWEAPONSLEVEL1,
            )
            await self.reseach_forge(
                AbilityId.FORGERESEARCH_PROTOSSGROUNDARMORLEVEL1,
                UpgradeId.PROTOSSGROUNDARMORSLEVEL1,
            )
            await self.reseach_forge(
                AbilityId.FORGERESEARCH_PROTOSSSHIELDSLEVEL1,
                UpgradeId.PROTOSSSHIELDSLEVEL1,
            )
            await self.reseach_forge(
                AbilityId.FORGERESEARCH_PROTOSSGROUNDWEAPONSLEVEL2,
                UpgradeId.PROTOSSGROUNDWEAPONSLEVEL2,
            )
            await self.reseach_forge(
                AbilityId.FORGERESEARCH_PROTOSSSHIELDSLEVEL2,
                UpgradeId.PROTOSSSHIELDSLEVEL2,
            )
            await self.reseach_forge(
                AbilityId.FORGERESEARCH_PROTOSSGROUNDARMORLEVEL2,
                UpgradeId.PROTOSSGROUNDARMORSLEVEL2,
            )
            await self.reseach_forge(
                AbilityId.FORGERESEARCH_PROTOSSGROUNDWEAPONSLEVEL3,
                UpgradeId.PROTOSSGROUNDWEAPONSLEVEL3,
            )
            await self.reseach_forge(
                AbilityId.FORGERESEARCH_PROTOSSSHIELDSLEVEL3,
                UpgradeId.PROTOSSSHIELDSLEVEL3,
            )
            await self.reseach_forge(
                AbilityId.FORGERESEARCH_PROTOSSGROUNDARMORLEVEL3,
                UpgradeId.PROTOSSGROUNDARMORSLEVEL3,
            )

    async def reseach_forge(self, ability: AbilityId, upgrade: UpgradeId) -> bool:
        bot = self.bot
        if bot.already_pending_upgrade(upgrade) > 0:
            return
        forge = self.bot.structures(UnitTypeId.FORGE).ready.random
        if forge.is_idle and bot.can_afford(ability):
            forge.research(upgrade)


from typing import Any, List, Tuple
from .constants._sync_mode import ASYNC
from .opencv._cv_border_types import BORDER_DEFAULT
from ..native import make_native_object

import sys
matx = sys.modules['matx']

class _LaplacianBlurOpImpl:
    """ LaplacianBlur Impl """

    def __init__(self,
                 device: Any,
                 pad_type: str = BORDER_DEFAULT) -> None:
        self.op: matx.NativeObject = make_native_object(
            "VisionLaplacianBlurGeneralOp", device())
        self.pad_type: str = pad_type

    def __call__(self,
                 images: List[matx.runtime.NDArray],
                 ksizes: List[int],
                 scales: List[float],
                 sync: int = ASYNC) -> List[matx.runtime.NDArray]:
        batch_size: int = len(images)
        assert len(
            ksizes) == batch_size, "The ksize number for laplacian blur should be equal to batch size."
        assert len(
            scales) == batch_size, "The scale number for laplacian blur should be equal to batch size."

        return self.op.process(images, ksizes, scales, self.pad_type, sync)

class LaplacianBlurOp:
    """ Apply laplacian blur on input images.
    """

    def __init__(self,
                 device: Any,
                 pad_type: str = BORDER_DEFAULT) -> None:
        """ Initialize LaplacianBlurOp

        Args:
            device (Any) : the matx device used for the operation
            pad_type (str, optional) : pixel extrapolation method, if border_type is BORDER_CONSTANT, 0 would be used as border value.
        """
        self.op: _LaplacianBlurOpImpl = matx.script(_LaplacianBlurOpImpl)(device, pad_type)

    def __call__(self,
                 images: List[matx.runtime.NDArray],
                 ksizes: List[int],
                 scales: List[float],
                 sync: int = ASYNC) -> List[matx.runtime.NDArray]:
        """ Apply average blur on input images.

        Args:
            images (List[matx.runtime.NDArray]): target images.
            ksizes (List[int]): conv kernel size for each image, laplacian kernel is a square shaped kernel, so each item in this list is an integer.
            scales (List[float]): scale factor for laplacian blur
            sync (int, optional): sync mode after calculating the output. when device is cpu, the params makes no difference.
                                    ASYNC -- If device is GPU, the whole calculation process is asynchronous.
                                    SYNC -- If device is GPU, the whole calculation will be blocked until this operation is finished.
                                    SYNC_CPU -- If device is GPU, the whole calculation will be blocked until this operation is finished, and the corresponding CPU array would be created and returned.
                                  Defaults to ASYNC.
        Returns:
            List[matx.runtime.NDArray]: converted images

        Example:

        >>> import cv2
        >>> import matx
        >>> from matx.vision import LaplacianBlurOp

        >>> # Get origin_image.jpeg from https://github.com/bytedance/matxscript/tree/main/test/data/origin_image.jpeg
        >>> image = cv2.imread("./origin_image.jpeg")
        >>> device_id = 0
        >>> device_str = "gpu:{}".format(device_id)
        >>> device = matx.Device(device_str)
        >>> # Create a list of ndarrays for batch images
        >>> batch_size = 3
        >>> nds = [matx.array.from_numpy(image, device_str) for _ in range(batch_size)]
        >>> ksizes = [3, 5, 3]
        >>> scales = [1.0, 0.5, 0.5]

        >>> op = LaplacianBlurOp(device)
        >>> ret = op(nds, ksizes, scales)
        """
        return self.op(images, ksizes, scales, sync)


import hashlib
import random

class CountingBloomFilter:
    def __init__(self, size, hash_count):
        self.size = size
        self.hash_count = hash_count
        self.counters = [0] * self.size

    def add(self, string):
        indices = []
        for seed in range(self.hash_count):
            result = int(hashlib.sha256(f"{seed}{string}".encode()).hexdigest(), 16) % self.size
            indices.append(result)
            self.counters[result] += 1
        return indices

    def remove(self, string, indices=None):
        if indices is None:
            indices = []
            for seed in range(self.hash_count):
                result = int(hashlib.sha256(f"{seed}{string}".encode()).hexdigest(), 16) % self.size
                indices.append(result)
        for index in indices:
            if self.counters[index] > 0:
                self.counters[index] -= 1

    def lookup(self, string):
        for seed in range(self.hash_count):
            result = int(hashlib.sha256(f"{seed}{string}".encode()).hexdigest(), 16) % self.size
            if self.counters[result] == 0:
                return False
        return True

def generate_random_elements(num_elements, element_size=10):
    elements = set()
    while len(elements) < num_elements:
        elements.add(''.join(random.choices('abcdefghijklmnopqrstuvwxyz', k=element_size)))
    return elements

def main():
    num_elements = 1000
    num_to_remove = 500
    num_to_add = 500

    cbf = CountingBloomFilter(size=10000, hash_count=7)

    elements_a = generate_random_elements(num_elements)

    indices_a = {element: cbf.add(element) for element in elements_a}

    elements_to_remove = set(random.sample(elements_a, num_to_remove))
    for element in elements_to_remove:
        cbf.remove(element, indices_a[element])

    elements_to_add = generate_random_elements(num_to_add)
    for element in elements_to_add:
        cbf.add(element)

    count_found_a = sum(1 for element in elements_a if cbf.lookup(element))

    with open("count_bloom.txt", "w") as f:
        f.write(f"{count_found_a}\n")

if __name__ == "__main__":
    main()


import random

# Сортировка слиянием

def merge(A:list, B:list):
    """ Слияние отсортированных массивов """
    C=[0] * (len(A) + len(B))
    i = k = n = 0
    while i < len(A) and k < len(B):
        if A[i] <= B[k]:
            C[n] = A[i]
            n += 1
            i += 1
        else:
            C[n] = B[k]
            n += 1
            k += 1
    while i < len[A]:
        C[n] = A[i]
        n += 1
        i += 1
    while k < len(B):
        C[n] = B[k]
        n += 1
        k += 1
    return C

def merge_sort(A):
    """ Сортировка слиянием"""
    if len(A) <= 1:
        return
    middle = len(A) / 2
    L = [A[i] for i in range(0, middle)]
    R = [A[i] for i in range(0, middle)]
    merge_sort(L)
    merge_sort(R)
    C = merge(L, R)
    for i in range(len(A)):
        A[i] = C[i]

# Быстрая сортировка (Сортировка Тони Хоара)

def quick_sort(A):
    if len(A) <= 1:
        return
    L = []
    M = []
    R = []
    barrier = random.randrange(len(A))
    for x in A:
        for x in A:
            if x < barrier:
                L.append(x)
            elif x == barrier:
                M.append(x)
            else:
                R.append(x)
        quick_sort(L)
        quick_sort(R)
        k = 0                           #можно сделать через срезы
        for x in L + M + R:
            A[k] = x
            k += 1


import numpy as np
import time
import datetime
# Creating 5x4 array
"""array1 = np.arange(1000)
array2 = np.arange(1000)

tempo_inicial = time.time()

for i in array1:
    for j in array2:
        print(i+j)

tempo_final = time.time()

resultado = tempo_final - tempo_inicial
print(round(resultado, 2))"""

time1 = datetime.datetime.now()

lista1 = list(range(1, 1000))
lista2 = list(range(1, 1000))

for i in range(len(lista1)):
    for j in range(len(lista2)):
        print(i+j)

time = datetime.datetime.now()
print(time-time1)


# From baselines.bench.monitor.py. Here so the client outside docker does not need to install baselines.
# Only pandas, tkinter (sudo apt-get install python3-tk), numpy, matplotlib.
# cd ~/custom_roboschool/servorobots/servorobots/tools
# python3 plot_results.py --dirs /progress_1
from glob import glob
import os.path as osp
import json

def load_results(dir):
    import pandas
    monitor_files = (
        glob(osp.join(dir, "*monitor.json")) +
        glob(osp.join(dir, "*monitor.csv"))) # get both csv and (old) json files
    if not monitor_files:
        raise LoadMonitorResultsError("no monitor files of the form *%s found in %s" % (Monitor.EXT, dir))
    dfs = []
    headers = []
    for fname in monitor_files:
        with open(fname, 'rt') as fh:
            if fname.endswith('csv'):
                firstline = fh.readline()
                if not firstline:
                    continue
                assert firstline[0] == '#'
                header = json.loads(firstline[1:])
                df = pandas.read_csv(fh, index_col=None)
                headers.append(header)
            elif fname.endswith('json'): # Deprecated json format
                episodes = []
                lines = fh.readlines()
                header = json.loads(lines[0])
                headers.append(header)
                for line in lines[1:]:
                    episode = json.loads(line)
                    episodes.append(episode)
                df = pandas.DataFrame(episodes)
            else:
                assert 0, 'unreachable'
            df['t'] += header['t_start']
        dfs.append(df)
    df = pandas.concat(dfs)
    df.sort_values('t', inplace=True)
    df.reset_index(inplace=True)
    df['t'] -= min(header['t_start'] for header in headers)
    df.headers = headers # HACK to preserve backwards compatibility
    return df

import numpy as np
import matplotlib
matplotlib.use('TkAgg') # Can change to 'Agg' for non-interactive mode

import matplotlib.pyplot as plt
plt.rcParams['svg.fonttype'] = 'none'

X_TIMESTEPS = 'timesteps'
X_EPISODES = 'episodes'
X_WALLTIME = 'walltime_hrs'
Y_REWARD = 'reward'
Y_TIMESTEPS = 'timesteps'
POSSIBLE_X_AXES = [X_TIMESTEPS, X_EPISODES, X_WALLTIME]
EPISODES_WINDOW = 90
COLORS = ['blue', 'green', 'red', 'cyan', 'magenta', 'yellow', 'black', 'purple', 'pink',
        'brown', 'orange', 'teal', 'coral', 'lightblue', 'lime', 'lavender', 'turquoise',
        'darkgreen', 'tan', 'salmon', 'gold', 'lightpurple', 'darkred', 'darkblue']

def rolling_window(a, window):
    shape = a.shape[:-1] + (a.shape[-1] - window + 1, window)
    strides = a.strides + (a.strides[-1],)
    return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)

def window_func(x, y, window, func):
    yw = rolling_window(y, window)
    yw_func = func(yw, axis=-1)
    return x[window-1:], yw_func

def ts2xy(ts, xaxis, yaxis):
    if xaxis == X_TIMESTEPS:
        x = np.cumsum(ts.l.values)
    elif xaxis == X_EPISODES:
        x = np.arange(len(ts))
    elif xaxis == X_WALLTIME:
        x = ts.t.values / 3600.
    else:
        raise NotImplementedError
    if yaxis == Y_REWARD:
        y = ts.r.values
    elif yaxis == Y_TIMESTEPS:
        y = ts.l.values
    else:
        raise NotImplementedError
    return x, y

def plot_curves(fig, xy_list, xaxis, yaxis, title):

    maxx = max(xy[0][-1] for xy in xy_list)
    minx = 0
    for (i, (x, y)) in enumerate(xy_list):
        color = COLORS[i]
        plt.scatter(x, y, s=2)
        x, y_mean = window_func(x, y, EPISODES_WINDOW, np.mean) #So returns average of last EPISODE_WINDOW episodes
        plt.plot(x, y_mean, color=color)
    plt.xlim(minx, maxx)
    plt.title(title)
    plt.xlabel(xaxis)
    plt.ylabel(yaxis)
    plt.tight_layout()
    fig.canvas.mpl_connect('resize_event', lambda event: plt.tight_layout())
    plt.grid(True)

def plot_results(dirs, num_timesteps, xaxis, task_name):
    tslist = []
    for dir in dirs:
        ts = load_results(dir)
        ts = ts[ts.l.cumsum() <= num_timesteps]
        tslist.append(ts)
    yaxes = ['reward', 'timesteps']
    fig = plt.figure(figsize=(8, 4))
    for i in range(len(yaxes)):
        plt.subplot(2, 1, i+1)
        xy_list = [ts2xy(ts, xaxis, yaxes[i]) for ts in tslist]
        plot_curves(fig, xy_list, xaxis, yaxes[i], task_name)

# Example usage in jupyter-notebook
# from baselines import log_viewer
# %matplotlib inline
# log_viewer.plot_results(["./log"], 10e6, log_viewer.X_TIMESTEPS, "Breakout")
# Here ./log is a directory containing the monitor.csv files

def main():
    import argparse
    import os
    parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--dirs', help='List of log directories', nargs = '*', default=['./log'])
    parser.add_argument('--num_timesteps', type=int, default=int(10e6))
    parser.add_argument('--xaxis', help = 'Varible on X-axis', default = X_TIMESTEPS)
    parser.add_argument('--task_name', help = 'Title of plot', default = 'Breakout')
    parser.add_argument('--show', help="Show the window", default = True, dest='show', action='store_true')
    parser.add_argument('--no-show', help="Don't show the window", dest='show', action='store_false')
    parser.add_argument('--save_dir', help='Show the window')
    parser.add_argument('--format', help='png, pdf (+ what is supported by matplotlib)', default='png')
    args = parser.parse_args()
    args.dirs = [os.path.abspath(dir) for dir in args.dirs]
    plot_results(args.dirs, args.num_timesteps, args.xaxis, args.task_name)
    if args.show:
        print('showing plot')
        plt.show()
    if args.save_dir is not None:
        plt.savefig(args.save_dir + '/results.' + args.format)
        print('Saved plot in ' + args.save_dir + '/results.' + args.format)

if __name__ == '__main__':
    main()


from odoo import models, _

import logging
_logger = logging.getLogger(__name__)

class PaymentTransaction(models.Model):
    _inherit = 'payment.transaction'

    # Override to confirm payments totaling the amount_total_deposit
    def _check_amount_and_confirm_order(self):
        self.ensure_one()
        for order in self.sale_order_ids.filtered(lambda so: so.state in ('draft', 'sent')):
            # default amount as originally calculated
            amount = order.amount_total
            if order.amount_total_deposit:
                amount = order.amount_total_deposit
            if order.currency_id.compare_amounts(self.amount, amount) == 0:
                order.with_context(send_email=True).action_confirm()
            else:
                _logger.warning(
                    '<%s> transaction AMOUNT MISMATCH for order %s (ID %s): expected %r, got %r',
                    self.acquirer_id.provider,order.name, order.id,
                    amount, self.amount,
                )
                order.message_post(
                    subject=_("Amount Mismatch (%s)") % self.acquirer_id.provider,
                    body=_("The order was not confirmed despite response from the acquirer (%s): order total is %r but acquirer replied with %r.") % (
                        self.acquirer_id.provider,
                        amount,
                        self.amount,
                    )
                )


# Import required modules.
import json

def lambda_handler(event, context):
    """
    Main Amazon Lambda event handler.

    :param event: Name of event.
    :param context: Context of event.
    :type event: String.
    :type context: String.
    """
    intent_name = event["currentIntent"]["name"]
    session_attributes = event["sessionAttributes"]

    if intent_name == "Profiler":
        return close(session_attributes, "Fulfilled", build_response_message("All good!"))

    raise Exception("Intent with name " + intent_name + " not supported")

def close(session_attributes, fulfillment_state, message):
    """
    Close an Amazon Lex Intent.

    :param session_attributes: Session Attributes.
    :param fulfillment_state: State of fulfillment.
    :param message: Message to deliver.
    :type session_attributes: String.
    :type fulfillment_state: String.
    :type message: String.
    """
    response = {
        "sessionAttributes" : session_attributes,
        "dialogAction" : {
            "type" : "Close",
            "fulfillmentState" : fulfillment_state,
            "message" : message
        }
    }

    return response

def build_response_message(message):
    """
    Returns the response message for any givent Amazon Intent.

    :param message: Content of message to be returned by Lex bot.
    :type message: String.
    """
    return {"contentType" : "PlainText", "content" : message}


__all__ = [
    'to_node_dict',
    'to_container_dict',
    'to_object_dict'
]

def to_node_dict(node):
    result = {
        'id': node.id,
        'name': node.name,
        'state': node.state,
        'public_ips': node.public_ips,
        'private_ips': node.private_ips,
        'extra': node.extra or {}
    }

    return result

def to_container_dict(container):
    result = {
        'name': container.name,
        'extra': container.extra
    }

    return result

def to_object_dict(obj):
    result = {
        'name': obj.name,
        'container_name': obj.container.name,
        'size': obj.size,
        'meta_data': obj.meta_data,
        'extra': obj.extra
    }
    return result


class Solution:
    def longestPalindrome(self, s: str) -> int:
        d = {}
        for i in range(len(s)):
            if s[i] not in d.keys():
                d[s[i]] = 1
            else:
                d[s[i]] += 1

        flag = False
        max_len = 0
        for i in d.keys():
            if d[i] % 2 != 0:
                flag = True
            max_len += (d[i]//2)*2

        return max_len + flag


import logging
import math
import time
import warnings
from itertools import cycle, islice

import torch
import numpy as np
from PIL import Image
from torch.utils.data import Dataset
from torchvision import datasets
from torchvision import transforms
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import make_moons

from .augmentation import RandAugment, CutOutAug
from .autoaugment import CIFAR10Policy, SVHNPolicy
from .utils import AttrDict

cifar10_mean = (0.4914, 0.4822, 0.4465)
cifar10_std = (0.2471, 0.2435, 0.2616)
cifar100_mean = (0.5071, 0.4867, 0.4408)
cifar100_std = (0.2675, 0.2565, 0.2761)
normal_mean = (0.5, 0.5, 0.5)
normal_std = (0.5, 0.5, 0.5)

class MyDataset(Dataset):
    def __init__(self, data, targets, transform=None):
        self.data = data
        self.targets = torch.LongTensor(targets)
        self.transform = transform

    def __getitem__(self, index):
        x = self.data[index]
        y = self.targets[index]

        if self.transform:
            x = self.transform(x)
        return x, y

    def __len__(self):
        return len(self.data)

class TransformToy(object):
    def __init__(self):
        self.ori = transforms.Compose([])
        self.aug = transforms.Compose([])

    def __call__(self, x):
        ori = self.ori(x)
        aug = self.aug(x)
        return ori, aug

def prepare_cifar10(num_labeled=4000, classes=10, eval_step=1000, batch_size=64, resize=32, data_path="./data",randaug=[2,16]):
    args = {"num_labeled": num_labeled,
        "num_classes": classes,
        "eval_step": eval_step,
        "batch_size": batch_size,
        "resize": resize,
        "data_path": data_path,
        "expand_labels": False,
        "randaug": randaug
       }
    args = AttrDict(args)
    return get_cifar10(args)

def get_toy(args):
    n_samples = 1500
    np.random.seed(0)
    noisy_moons = make_moons(n_samples=n_samples, noise=0.05)
    X, y = noisy_moons

    X = StandardScaler().fit_transform(X)
    random_choice = np.random.choice(X.shape[0], 5, replace=False)
    labeled_data = X[random_choice]
    targets  = y[random_choice]

    labeled_dataset = MyDataset(labeled_data, torch.from_numpy(targets).long())
    unlabeled_dataset =MyDataset(X, torch.from_numpy(y).long(), transform=TransformToy())
    test_dataset =MyDataset(X, torch.from_numpy(y).long())

    return labeled_dataset, unlabeled_dataset, test_dataset

def get_cifar10(args):
    transform_labeled = transforms.Compose([
        transforms.RandomHorizontalFlip(),
        CIFAR10Policy(),
        transforms.RandomCrop(size=args.resize,
                              padding=int(args.resize*0.125),
                              padding_mode='reflect'),
        transforms.ToTensor(),
        transforms.Normalize(mean=cifar10_mean, std=cifar10_std)
    ])
    transform_val = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize(mean=cifar10_mean, std=cifar10_std)
    ])
    base_dataset = datasets.CIFAR10(args.data_path, train=True, download=True)

    train_labeled_idxs, train_unlabeled_idxs = x_u_split(args, base_dataset.targets)

    train_labeled_dataset = CIFAR10SSL(
        args.data_path, train_labeled_idxs, train=True,
        transform=transform_labeled
    )

    train_unlabeled_dataset = CIFAR10SSL(
        args.data_path, train_unlabeled_idxs,
        train=True,
        transform=TransformMPL(args, mean=cifar10_mean, std=cifar10_std)
    )

    test_dataset = datasets.CIFAR10(args.data_path, train=False,
                                    transform=transform_val, download=False)

    return train_labeled_dataset, train_unlabeled_dataset, test_dataset

def get_cifar100(args):

    transform_labeled = transforms.Compose([
        transforms.RandomHorizontalFlip(),
        CIFAR10Policy(),
        transforms.RandomCrop(size=args.resize,
                              padding=int(args.resize*0.125),
                              padding_mode='reflect'),
        transforms.ToTensor(),
        transforms.Normalize(mean=cifar100_mean, std=cifar100_std)])

    transform_val = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize(mean=cifar100_mean, std=cifar100_std)])

    base_dataset = datasets.CIFAR100(args.data_path, train=True, download=True)

    train_labeled_idxs, train_unlabeled_idxs = x_u_split(args, base_dataset.targets)

    train_labeled_dataset = CIFAR100SSL(
        args.data_path, train_labeled_idxs, train=True,
        transform=transform_labeled
    )

    train_unlabeled_dataset = CIFAR100SSL(
        args.data_path, train_unlabeled_idxs, train=True,
        transform=TransformMPL(args, mean=cifar100_mean, std=cifar100_std)
    )

    test_dataset = datasets.CIFAR100(args.data_path, train=False,
                                     transform=transform_val, download=False)

    return train_labeled_dataset, train_unlabeled_dataset, test_dataset

def x_u_split(args, labels):
    label_per_class = args.num_labeled // args.num_classes
    labels = np.array(labels)
    labeled_idx = []
    # unlabeled data: all training data
    unlabeled_idx = np.array(range(len(labels)))
    for i in range(args.num_classes):
        idx = np.where(labels == i)[0]
        idx = np.random.choice(idx, label_per_class, False)
        labeled_idx.extend(idx)
    labeled_idx = np.array(labeled_idx)
    assert len(labeled_idx) == args.num_labeled

    if args.expand_labels or args.num_labeled < args.batch_size:
        num_expand_x = math.ceil(
            args.batch_size * args.eval_step / args.num_labeled)
        labeled_idx = np.hstack([labeled_idx for _ in range(num_expand_x)])
    np.random.shuffle(labeled_idx)
    return labeled_idx, unlabeled_idx

class TransformMPL(object):
    def __init__(self, args, mean, std):
        if args.randaug:
            n, m = args.randaug
        else:
            n, m = 2, 16  # default

        self.ori = transforms.Compose([
            transforms.RandomHorizontalFlip(),
            transforms.RandomCrop(size=args.resize,
                                  padding=int(args.resize*0.125),
                                  padding_mode='reflect')])
        self.aug = transforms.Compose([
            CIFAR10Policy(),
            transforms.RandomHorizontalFlip(),
            transforms.RandomCrop(size=args.resize,
                                  padding=int(args.resize*0.125),
                                  padding_mode='reflect'),
            RandAugment(n=n, m=m),
            CutOutAug(n=1, size=args.resize//4),
            transforms.RandomHorizontalFlip(),
            transforms.RandomCrop(size=args.resize,
                                  padding=int(args.resize*0.125),
                                  padding_mode='reflect')
            ])
        self.normalize = transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize(mean=mean, std=std)])

    def __call__(self, x):
        ori = self.ori(x)
        aug = self.aug(x)
        return self.normalize(ori), self.normalize(aug)

class CIFAR10SSL(datasets.CIFAR10):
    def __init__(self, root, indexs, train=True,
                 transform=None, target_transform=None,
                 download=False):
        super().__init__(root, train=train,
                         transform=transform,
                         target_transform=target_transform,
                         download=download)
        if indexs is not None:
            self.data = self.data[indexs]
            self.targets = np.array(self.targets)[indexs]

    def __getitem__(self, index):
        img, target = self.data[index], self.targets[index]
        img = Image.fromarray(img)

        if self.transform is not None:
            img = self.transform(img)

        if self.target_transform is not None:
            target = self.target_transform(target)

        return img, target

class CIFAR100SSL(datasets.CIFAR100):
    def __init__(self, root, indexs, train=True,
                 transform=None, target_transform=None,
                 download=False):
        super().__init__(root, train=train,
                         transform=transform,
                         target_transform=target_transform,
                         download=download)
        if indexs is not None:
            self.data = self.data[indexs]
            self.targets = np.array(self.targets)[indexs]

    def __getitem__(self, index):
        img, target = self.data[index], self.targets[index]
        img = Image.fromarray(img)

        if self.transform is not None:
            img = self.transform(img)

        if self.target_transform is not None:
            target = self.target_transform(target)

        return img, target


import errno
import multiprocessing
import socket
import struct
import time

from config import *

class listener():

  def __init__(self,
                member_list=[],
                sdfs_local_map={},
                local_sdfs_map={},
                sdfs_owners={}):
    self.member_list = member_list
    self.name = socket.gethostbyname(socket.gethostname())
    self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    self.server_socket.bind((socket.gethostname(), listener_port))
    self.sdfs_local_map = sdfs_local_map
    self.local_sdfs_map = local_sdfs_map
    self.sdfs_owners = sdfs_owners

  def wait_for_order(self):
    # print("Listening for updates on:")
    # print("host:{} port:{}".format(socket.gethostname(), listener_port))
    update, updater_address = self.server_socket.recvfrom(4096)
    if (update != 'p'):
      print("Update from {}: {}".format(updater_address, update))
    self.server_socket.sendto(execution_ack_string, updater_address)
    return update

  def parse_order(self, order):

    # simple ping

    if (order == ping_message):
      return

    # Add a machine to the member list
    elif (order[0] == 'a'):
      joiner_address = order[1:]
      if joiner_address in self.member_list:
        return
      else:
        print ("Join - {}".format(joiner_address))
        self.member_list.append(joiner_address)
        self.member_list.sort()

    # Remove a machine from the member list
    elif (order[0] == 'r'):
      leaver_address = order[1:]
      if leaver_address in self.member_list:
        print ("Fail - {}".format(leaver_address))
        self.member_list.remove(leaver_address)
        self.member_list.sort()
      else:
        return

    # Remove a machine from the member list
    elif (order[0] == 'l'):
      leaver_address = order[1:]
      if leaver_address in self.member_list:
        print ("Leave - {}".format(leaver_address))
        self.member_list.remove(leaver_address)
        self.member_list.sort()
      else:
        return

    # Respond to a PUT
    elif(order[0] == 'z'):
      order_details = order[1:]
      sdfs_fn, o1, o2, o3, data = order_details.split()
      owners = [o1, o2, o3]
      self.sdfs_owners[sdfs_fn] = owners
      local_file = open(sdfs_fn, 'w')
      local_file.write(data)
      local_file.close()

    # Respond to a delete request
    elif(order[0] == 'd'):
      sdfs_fn = order[1:]
      local_fn = sdfs_local_map[sdfs_fn]
      sdfs_local_map.pop(sdfs_fn, None)
      sdfs_owners.pop(sdfs_fn, None)
      local_sdfs_map.pop(local_fn, None)


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

# In[75]:

import os
import math
import operator
import numpy as np
import random
import pandas as pd
import GLAD
import glob, operator
import matplotlib
import matplotlib.pyplot as plt
import numpy as np

# In[2]:

music = pd.read_csv('music_mturk_answers.csv')
sentiment = pd.read_csv('sentiment_mturk_answers.csv')
rte = pd.read_csv('rte.anno.csv')
data1 = music.values
data2 = sentiment.values
data3 = rte.values

# In[6]:

#for sentiment data(data2)
workers = np.unique(data2[:,0])
samples = np.unique(data2[:,1])

labelM = np.full((workers.shape[0],samples.shape[0]),-1)
for i in range(data2.shape[0]):
    x = int(np.where(workers == data2[i,0])[0])
    y = int(np.where(samples == data2[i,1])[0])
    labelM[x][y] = toint(data2[i][5])

# In[7]:

def toint(char):
    if char == 'pos':
        return 1
    else:
        return 0

# In[31]:

def getP(alpha,beta):
    pM = np.empty([])
    for i in range(alpha.shape[0]):
        for j in range(beta.shape[0]):
            pM = np.append(pM, 1/(1+math.exp(-alpha[i]*beta[j])))
            print(i)
    pM = pM.reshape(alpha.shape[0],beta.shape[0])
    return pM

# In[74]:

fig = plt.figure()
ax = plt.axes()
ax.set(xlabel='number of labels', ylabel='accuracy',title='sentiment')
x = [5,10,20,40,80,150,200]
y = [0.777,0.795,0.810,0.807,0.815,0.825,0.832]
ax.plot(x, y,'-g', label ='sentiment',color='blue')

# In[62]:

np.unique(data3[:,1]).shape

# In[93]:

labels = ['voting','spam','volcanoes']
id3 = [0.985,0.648,0.743]
lr = [0.959,0.605,0.635]
nb = [0.94,0.635,0.61]
glad = [0.988,0.689,0.765]

x = np.arange(len(labels))  # the label locations
width = 0.35

ig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, id3, width/3, label='ID3')
rects2 = ax.bar(x - width/6, lr, width/3, label='LogR')
rects3 = ax.bar(x + width/6, nb, width/3, label='NaiveB', color = 'purple')
rects4 = ax.bar(x + width/2, glad, width/3, label='GLAD', color = 'red')

ax.set_ylabel('Accuracy')
ax.set_title('Accurcy comparesion')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()

def autolabel(rects):
    """Attach a text label above each bar in *rects*, displaying its height."""
    for rect in rects:
        height = rect.get_height()
        ax.annotate('{}'.format(height),
                    xy=(rect.get_x() + rect.get_width() / 2, height),
                    xytext=(0, 3),  # 3 points vertical offset
                    textcoords="offset points",
                    ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)

fig.tight_layout()

plt.show()

# In[ ]:


from gurobipy import GRB, quicksum, Model

# Initialise empty data
Nodes = []
Arcs = []
Omega = None
FromNode = -1
EPS = 0.00001
FlowINF = 10000

# Read in the data
with open("Outmax_flow2.dat") as f:
    while True:
        l = f.readline().strip()
        if l[0] == "n":
            FromNode = len(Nodes)
            Nodes.append(l)
        elif l[0] == "a":
            t = l.split(" ")
            flow = int(t[4])
            ToNode = int(t[3])
            if flow == FlowINF:
                Omega = len(Arcs)
            Arcs.append((FromNode, ToNode, flow))
        else:
            break
N = range(len(Nodes))
A = range(len(Arcs))

print("Nodes and Arcs", len(Nodes), len(Arcs))

# Empty array of jobs
Jobs = []
with open("max_flow2.dat_Job0") as f:
    while True:
        l = f.readline().strip()
        if len(l) <= 1:
            break
        nArc = int(l.split(" ")[1])
        t = f.readline().strip().split(" ")
        Jobs.append((nArc, int(t[1]) - 1, int(t[2]) - 1, int(t[3])))

T = range(1000)
J = range(len(Jobs))
# The jobs for an arc
JobsA = [[j for j in J if Jobs[j][0] == a] for a in A]
# The starting time periods for job A
JP = [range(Jobs[j][1], Jobs[j][2]) for j in J]

print("Jobs", len(Jobs))
# The jobs and their indices that impact on arc a in time t
JobsTA = [
    [
        {(j, p) for j in JobsA[a] for p in JP[j] if p <= t and p + Jobs[j][3] > t}
        for a in A
    ]
    for t in T
]
print("Calculated JobsTA")

# Set up the sub-problem
BSP = Model()
BSP.setParam("OutputFlag", 0)
X = {a: BSP.addVar() for a in A}

BSP.setObjective(X[Omega], GRB.MAXIMIZE)

FlowConservation = {
    n: BSP.addConstr(
        quicksum(X[a] for a in A if Arcs[a][0] == n)
        == quicksum(X[a] for a in A if Arcs[a][1] == n)
    )
    for n in N
}

UpperBound = {a: BSP.addConstr(X[a] <= Arcs[a][2]) for a in A}
# Solve unrestricted sub-problem to calculate MaxFlow
BSP.optimize()
MaxFlow = BSP.objVal
print("Maximum flow", MaxFlow)

# Set up master problem
BMP = Model()
Y = {(j, t): BMP.addVar(vtype=GRB.BINARY) for j in J for t in JP[j]}
Theta = {t: BMP.addVar(ub=MaxFlow) for t in T}

BMP.setObjective(quicksum(Theta[t] for t in T), GRB.MAXIMIZE)

EachJobScheduled = {j: BMP.addConstr(quicksum(Y[j, t] for t in JP[j]) == 1) for j in J}

_SolveBSP = {}
SolvesCalled = 0

def SolveBSP(AZero):
    global SolvesCalled
    SolvesCalled += 1
    if AZero not in _SolveBSP:
        for a in A:
            if a in AZero:
                UpperBound[a].RHS = 0
            else:
                UpperBound[a].RHS = Arcs[a][2]
        BSP.optimize()
        _SolveBSP[AZero] = (BSP.objVal, {a: UpperBound[a].pi for a in A})
    return _SolveBSP[AZero]

BMP._Best = 0

def Callback(model, where):
    if where == GRB.Callback.MIPSOL:
        YV = model.cbGetSolution(Y)
        YSet = {k for k in YV if YV[k] > 0.9}
        ThetaV = model.cbGetSolution(Theta)

        TotalObj = 0
        CutsAdded = 0
        for t in T:
            AZero = frozenset(a for a in A if JobsTA[t][a] & YSet)
            Obj, Pi = SolveBSP(AZero)
            TotalObj += Obj
            if Obj < ThetaV[t] - EPS:
                model.cbLazy(
                    Theta[t]
                    <= quicksum(
                        Pi[a]
                        * Arcs[a][2]
                        * (1 - quicksum(Y[j, tt] for (j, tt) in JobsTA[t][a]))
                        for a in A
                    )
                )
                CutsAdded += 1
            ThetaV[t] = Obj
        # Test if TotalObj is better than the incumbent
        if CutsAdded > 0 and TotalObj > model._Best + EPS:
            # Save the solution away
            print("#### Saving", TotalObj)
            model._Best = TotalObj
            model._Y = [YV[k] for k in Y]
            model._Theta = [ThetaV[k] for k in Theta]
        elif TotalObj > model._Best:
            model._Best = TotalObj

    if (
        where == GRB.Callback.MIPNODE
        and model.cbGet(GRB.Callback.MIPNODE_STATUS) == GRB.OPTIMAL
    ):
        # Do we have a saved solution that is better than the incumbent?
        if model._Best > model.cbGet(GRB.Callback.MIPNODE_OBJBST) + EPS:
            # If so, suggest it to Gurobi
            model.cbSetSolution(list(Y.values()), model._Y)
            model.cbSetSolution(list(Theta.values()), model._Theta)

# Warm start!
# Count = 1
# while True:
#     BSP.optimize()
#     if BSP.objVal>=2*MaxFlow:
#         break
#     print(Count, BSP.objVal)
#     Count+=1
#     for t in T:
#         BMP.addConstr(Theta[t]<=quicksum(
#                 UpperBound[a].pi*Arcs[a][2]*
#                 (1-quicksum(Y[j,tt] for (j,tt) in JobsTA[t][a]))
#                     for a in A))
#     for a in A:
#         if UpperBound[a].pi > EPS:
#             UpperBound[a].RHS += FlowINF

# Relax the problem
for k in Y:
    Y[k].vType = GRB.CONTINUOUS

# Add Benders cuts in a loop
BMP.setParam("OutputFlag", 0)
while True:
    BMP.optimize()
    CutsAdded = 0
    for t in T:
        for a in A:
            UpperBound[a].RHS = Arcs[a][2] * (
                1 - sum(Y[j, tt].x for (j, tt) in JobsTA[t][a])
            )
        BSP.optimize()
        if BSP.objVal < Theta[t].x - EPS:
            CutsAdded += 1
            BMP.addConstr(
                Theta[t]
                <= quicksum(
                    UpperBound[a].pi
                    * Arcs[a][2]
                    * (1 - quicksum(Y[j, tt] for (j, tt) in JobsTA[t][a]))
                    for a in A
                )
            )

    if CutsAdded == 0:
        break
    print("Added", CutsAdded, BMP.objVal)

BMP.setParam("OutputFlag", 1)
# Make the problem integer again
for k in Y:
    Y[k].vType = GRB.BINARY

BMP.setParam("MIPGap", 0)
BMP.setParam("LazyConstraints", 1)
# BMP.setParam('BranchDir',1)
BMP.optimize(Callback)


from aiogram import Dispatcher, types
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters.state import State, StatesGroup
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, ReplyKeyboardRemove

from datetime import datetime
import logging

from database import memory_bot
from other import my_func

class FSMCommon(StatesGroup):
    authorization = State()
    authorization_enter_family = State()
    authorization_select_num = State()
    choice = State()

async def cmd_start(message: types.Message, state: FSMContext):
    await state.reset_state(with_data=True)
    async with state.proxy() as data:
        data["id_telegram"] = message.from_user.id
        kb = InlineKeyboardMarkup(row_width=1, resize_keyboard=True, one_time_keyboard=True)
        kb.insert(InlineKeyboardButton(text="Авторизоваться", callback_data="authorization"))
        await message.answer("Привет, {USERNAME}! Я буду помогать тебе с обучением.\n"
                             "Но, сначала мне нужно найти тебя в базе.\n"
                             "Нажми на кнопку ниже"
                             .format(USERNAME=message.from_user.first_name),
                             reply_markup=kb)
        await FSMCommon.authorization.set()

    await memory_bot.save_memory(state)

async def cmd_del_message(message: types.Message):
    await message.delete()

async def query_authorization(query: types.CallbackQuery, state: FSMContext):
    await query.message.delete()
    await query.message.answer("Напишите вашу ФАМИЛИЮ:", reply_markup=ReplyKeyboardRemove())
    await query.answer()
    await FSMCommon.authorization_enter_family.set()
    await memory_bot.save_memory(state)

async def cmd_authorization(message: types.Message, state: FSMContext):
    await state.reset_state(with_data=True)
    async with state.proxy() as data:
        data["id_telegram"] = message.from_user.id
    await message.answer("Напишите вашу ФАМИЛИЮ:", reply_markup=ReplyKeyboardRemove())
    await FSMCommon.authorization_enter_family.set()
    await memory_bot.save_memory(state)

async def authorization_enter_family(message: types.Message, state: FSMContext):
    user_data = await my_func.data_users_from_1c(message.text)
    if len(user_data) >= 1:
        text = "0: Меня нет в этом списке списке\n"
        for num, elem in enumerate(user_data):
            text += f"{num + 1}:{elem[0]}\n"
        await message.answer(f"{text}", reply_markup=types.ReplyKeyboardRemove())
        await message.answer(f"Вы под каким номером?")
        async with state.proxy() as data:
            data["User"] = user_data
        await FSMCommon.authorization_select_num.set()
        await memory_bot.save_memory(state)

    else:
        await message.answer(f"Что-то таких в списках нет, попробуйте написать еще раз или проверьте данные в 1с",
                             reply_markup=types.ReplyKeyboardRemove())

async def authorization_select_num(message: types.Message, state: FSMContext):
    if message.text.isdigit():
        async with state.proxy() as data:
            if 1 <= int(message.text) <= len(data["User"]):
                await message.answer(f"Отлично! {data['User'][int(message.text)-1][0]} так и запишем...")
                data["User_filial"] = data['User'][int(message.text)-1][2]
                data['User_otdel'] = data['User'][int(message.text)-1][1]
                data['User'] = data['User'][int(message.text) - 1][0]
            elif int(message.text) == 0:
                await message.answer("Вы точно написали вашу фамилию так-же как в 1С?\n"
                                     "Попробуйте написать еще раз.\nЕсли все равно не находит совпадения,"
                                     "то возможно вашу карточку еще не завели в сотрудниках 1С или допущена ошибка"
                                     , reply_markup=types.ReplyKeyboardRemove())
            else:
                await message.reply(f"Издеваетесь?) Выберите значение от 0 до {len(data['User'])}",
                                    reply_markup=types.ReplyKeyboardRemove())
                return

        await FSMCommon.choice.set()
        await memory_bot.save_memory(state)
        await variants_actions(message=message, state=state)
    else:
        await message.answer(f"Извините, давайте только по делу, укажите цифры с вашем ФИО:",
                             reply_markup=types.ReplyKeyboardRemove())

async def about_me(message: types.Message, state: FSMContext):
    await message.delete()
    async with state.proxy() as data:
        await message.answer("ID телеграмм: {}\n"
                             "ФИО: {}\n"
                             "Отдел: {}\n"
                             "Подразделение: {}".format(data.get("id_telegram"),
                                                        data.get("User"),
                                                        data.get("User_otdel"),
                                                        data.get("User_filial")),
                             reply_markup=types.ReplyKeyboardRemove())
        data["id_telegram"] = message.from_user.id

async def variants_actions(message: types.Message, state: FSMContext):
    async with state.proxy() as data:
        if not isinstance(data.get("User"), str):
            await message.delete()
            return
        data.pop("Current proc")
    kb = InlineKeyboardMarkup(row_width=2, resize_keyboard=True, one_time_keyboard=True)
    kb.insert(InlineKeyboardButton(text="Начать обучение", callback_data="start_teach"))
    kb.insert(InlineKeyboardButton(text="Начать наблюдение", callback_data="start_watch"))
    await message.answer("Что хочешь сделать?"
                         .format(USERNAME=message.from_user.first_name),
                         reply_markup=kb)
    await FSMCommon.choice.set()
    await memory_bot.save_memory(state)

async def query_variants_actions(query: types.CallbackQuery, state: FSMContext):
    if query.data == "start_teach":
        await query.message.answer("Напишите ФАМИЛИЮ обучаемого:", reply_markup=types.ReplyKeyboardRemove())
        async with state.proxy() as data:
            data["Current proc"] = {"Type": "Обучение", "Users": dict()}
        await state.set_state("FSMStudies:enter_family")
    elif query.data == "start_watch":
        await query.message.answer("Напишите ФАМИЛИЮ наблюдаемого:", reply_markup=types.ReplyKeyboardRemove())
        async with state.proxy() as data:
            data["Current proc"] = {"Type": "Наблюдение",
                                    "Users": dict(),
                                    "Time start": datetime.now().strftime("%d.%m.%Y %H:%M:%S"),
                                    "Time finish": None,
                                    "Duration": None,
                                    "message id to delete": [],
                                    "Check list": {
                                        "Установление контакта": {
                                            "1;Проявил инициативу и сам подошел к клиенту": [0, 2],
                                            "2;Поприветствовал покупателя, представился": [0, 1, 2],
                                            "3;Применил фразу на установление контакта"
                                            " Клиент охотно вступил в диалог": [0, 2],
                                            "4;Узнал имя покупателя": [0, 2],
                                        },
                                        "Выяснение потребностей": {
                                            "5;Уточнил запрос клиента, используя воронку вопросов. "
                                            "опросы построены грамотно и последовательно": [0, 1, 3],
                                            "6;Резюмировал выявленные потребности": [0, 3],
                                        },
                                        "Презентация": {
                                            "7;Сотрудник хорошо ориентируется в ассортименте": [0, 3],
                                            "8;Говорит простым языком, объясняет терминологию": [0, 1],
                                            "9;В предложении товара были использованы выявленные точки воздействия":
                                                [0, 1, 3],
                                            "10;Сделал комплексное предложение (как минимум, два "
                                            "дополнительных товара, которые могут подойти)": [0, 2],
                                        },
                                        "Работа с возражениями": {
                                            "11;Работал с возражением по алгоритму": [0, 2],
                                            "12;Говорил убедительно и грамотно, опираясь на потребности клиента":
                                                [0, 1, 2],
                                        },
                                        "Завершение диалога": {
                                            "13;Предложил дополнительные услуги "
                                            "(нарезка, монтаж, доставка, подъем на этаж)": [0, 2],
                                            "14;Договорился с клиентом о дальнейших действиях. Пригласил приехать в"
                                            " магазин или договарился о следующем звонке": [0, 2],
                                            "15;Вежливо завершает разговор": [0, 1],
                                        },
                                        "Общее впечатление ": {
                                            "16;Доброжелательное выражение лица и интонация на "
                                            "протяжении всего диалога": [0, 2],
                                            "17;Нет долгих пауз и неуместных повторений вопросов ": [0, 2],
                                            "18;Производит впечатление профессионала, убедителен в коммуникации": [0, 3],
                                        }
                                    }}
        await state.set_state("FSMObservation:enter_family")
    else:
        return
    await query.message.delete()
    await memory_bot.save_memory(state)

def register_handlers(dp: Dispatcher):
    dp.register_message_handler(cmd_start, commands="start", state="*")
    dp.register_message_handler(cmd_authorization, commands="authorization", state="*")
    dp.register_message_handler(about_me, commands="about_me", state="*")
    dp.register_message_handler(variants_actions, commands="actions", state="*")
    dp.register_callback_query_handler(query_variants_actions, state=FSMCommon.choice)

    dp.register_message_handler(cmd_del_message, state=[FSMCommon.authorization, FSMCommon.choice,
                                                        "FSMStudies:add_user_or_next_step",
                                                        "FSMStudies:choice_train",
                                                        "FSMStudies:resume",
                                                        "FSMStudies:time"])
    dp.register_callback_query_handler(query_authorization, state=FSMCommon.authorization)
    dp.register_message_handler(authorization_enter_family, state=FSMCommon.authorization_enter_family)
    dp.register_message_handler(authorization_select_num, state=FSMCommon.authorization_select_num)


"""
Utils for logistic regression.
Author: Zainab batool
Date: 10/22/2020
"""
import math
import numpy as np
import optparse
import sys
import os

def parse_args():
    """Parse command line arguments (train and test csv files)."""
    parser = optparse.OptionParser(description='run Naive Bayes')

    parser.add_option('-r', '--train_filename', type='string', help='path to' +\
        ' train arff file')
    parser.add_option('-e', '--test_filename', type='string', help='path to' +\
        ' test arff file')

    (opts, args) = parser.parse_args()

    mandatories = ['train_filename', 'test_filename']
    for m in mandatories:
        if not opts.__dict__[m]:
            print('mandatory option ' + m + ' is missing\n')
            parser.print_help()
            sys.exit()

    return opts


import math
from ultralytics import YOLO
import cv2
from gtts import gTTS
import os

cap = cv2.VideoCapture(0)

frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
out = cv2.VideoWriter('output.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (frame_width, frame_height))

model = YOLO('yolov8n.pt')

classNames = ["person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat",
              "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat",
              "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella",
              "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat",
              "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup",
              "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli",
              "carrot", "hot dog", "pizza", "donut", "cake", "chair", "sofa", "pottedplant", "bed",
              "diningtable", "toilet", "tvmonitor", "laptop", "mouse", "remote", "keyboard", "cell phone",
              "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors",
              "teddy bear", "hair drier", "toothbrush"
              ]

box_colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]

detected_classes = set()

while True:
    success, img = cap.read()
    results = model(img, stream=True)

    for r in results:
        boxes = r.boxes
        for i, box in enumerate(boxes):
            x1, y1, x2, y2 = box.xyxy[0]
            x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
            color = box_colors[i % len(box_colors)]
            cv2.rectangle(img, (x1, y1), (x2, y2), color, 3)

            conf = math.ceil((box.conf[0] * 100)) / 100
            cls = int(box.cls[0])
            class_name = classNames[cls]
            label = f'{class_name}{conf}'
            t_size = cv2.getTextSize(label, 0, fontScale=1, thickness=2)[0]
            c2 = x1 + t_size[0], y1 - t_size[1] - 3
            cv2.rectangle(img, (x1, y1), c2, color, -1, cv2.LINE_AA)
            cv2.putText(img, label, (x1, y1 - 2), 0, 1, [255, 255, 255], thickness=1, lineType=cv2.LINE_AA)

            if class_name not in detected_classes:
                tts = gTTS(text=class_name, lang='en')
                tts.save("object_audio.mp3")
                os.system("mpg321 object_audio.mp3")
                detected_classes.add(class_name)

    out.write(img)
    cv2.imshow("Image", img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

out.release()
cap.release()
cv2.destroyAllWindows()


import sys
sys.stdin = open('12385.txt', 'r')

T = int(input())
for tc in range(1, T + 1):
    # K : 이동 가능 수, N: 종점 번호, M : 충전기 설치된 정류장 번호
    K, N, M = map(int, input().split())
    station = [0] + list(map(int, input().split())) + [N]
    cur = 0
    cnt = 0

    for i in range(1, M + 2):
        # 충전 불가능한 경우
        if station[i] - station[i - 1] > K:
            cnt = 0
            break
        # 충전 해야하는 경우
        if cur + K < station[i]:
            cur = station[i - 1]
            cnt += 1

    print(f'#{tc} {cnt}')


# Pygame library
import pygame

# Dash Display
class Dash(pygame.sprite.Sprite):
    # Properties
    surface = pygame.Surface((150,40))
    font = None

    # Constructor
    def __init__(self):
        """
        Entrée: None
        Sortie: None
        """
        # Call Sprite Constructor
        pygame.sprite.Sprite.__init__(self)

    # Update
    def update(self, rpm, gear, text=" "):
        """
        Entrée: RPM, Vitesse Actuelle, Texte Optionel
        Sortie: None
        """

        # Font
        if self.font == None:
            self.font = pygame.font.Font("../assets/font.ttf", 10)

        # Reset Dash
        self.surface.fill((0,255,0))
        self.surface.set_colorkey((0,255,0))

        # Display RPM
        self.surface.blit(
            self.font.render(str(rpm)+" RPM", False, (0,0,0)),
            (0,0)
        )
        # Display Gear
        self.surface.blit(
            self.font.render("Gear "+str(gear), False, (0,0,0)),
            (0,15)
        )
        # Display Text for Debug
        self.surface.blit(
            self.font.render(text, False, (0,0,0)),
            (0,30)
        )


import json
from rdflib import Graph, Namespace, URIRef, Literal
from modules.mention_handler import MentionHandler
from modules.attribute_handler import AttributeHandler
from modules.entity_handler import EntityHandler
from modules.relation_handler import RelationHandler
from modules.coref_handler import CorefHandler

SCHEMA_ORG_NAMESPACE = "http://schema.org/"
SCHEMA_ORG = Namespace(SCHEMA_ORG_NAMESPACE)

class Pipeline():

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

    def setup(self, config, metadata):
        # self.tokenizer = CoreNLPProcessor(config)

        # self.tokenizer.process("test sentence")

        self.mention_handler = MentionHandler(config)
        self.attribute_handler = AttributeHandler(config, self.mention_handler.client, self.mention_handler.annotators,
                                          self.mention_handler.props)
        self.coref_handler = CorefHandler(config, self.mention_handler.client, self.mention_handler.annotators,
                                          self.mention_handler.props)

        relations = json.load(open("../rules/edges/relations.json"))
        queries = json.load(open("../rules/nodes/queries.json"))

        self.g = Graph()
        self.metadata = metadata

        self.entity_handler = EntityHandler(relations, queries, self.g, self.metadata)

        self.relation_handler = RelationHandler(relations, self.mention_handler.client, self.mention_handler.annotators,
                                                self.mention_handler.props)

    def process(self, sentence):
        result, extractions = self.mention_handler.process_sentence(sentence)
        print(*[ent for ent in extractions], sep='\n')
        attribute_response = self.attribute_handler.process(sentence, result, extractions)
        self.coref_handler.process(sentence, result, extractions)
        print(*[ent for ent in extractions], sep='\n')

        entity_mentions_index = {}
        entity_response = self.entity_handler.process(extractions, entity_mentions_index)
        relation_response = self.relation_handler.process_relations(sentence, entity_mentions_index, self.g, result)

        for s, p, o in self.g:
            print((s, p, o))

        #visualize_graph(self.g)

        response = ''
        if len(attribute_response) > 0:
            response += 'Attributes: ' + '
'.join(attribute_response) + '
'

        if len(entity_response) > 0:
            response += 'Entities: ' + ', '.join(entity_response) + '
'

        if len(relation_response) > 0:
            response += 'Relations: ' + '
'.join(relation_response) + '
'

        return response


import pygame; from pygame.locals import *
import math; from math import *
from lookup import sine, cosine

def rotate(a,angle,b=(0,0)):
    """ Rotate a about b by angle. Make b the origin, then
        rotate a by angle. Then translate by b. """

    #Put the angle in radians, then calculate sine and cosine of it.
    angle = radians(angle)
    sinval = sin(angle)
    cosval = cos(angle)

    #Make about origin.
    rel_A_axis = a[0] - b[0]
    rel_B_axis = a[1] - b[1]

    #Rotate + translate.
    nA = (rel_A_axis * cosval - rel_B_axis * sinval) + b[0]
    nB = (rel_A_axis * sinval + rel_B_axis * cosval) + b[1]

    return (nA,nB)

def rotate_3D(point, angle):
    """ Rotate the point by the angle about the
        origin."""
    if angle == (0,0,0) or angle == [0,0,0]:return point
    #Rotate x and z coordinates about y axis.
    xz = rotate((point[0],point[2]),angle[1])
    #Rotate y and z coordinates about x axis.
    yz = rotate((point[1],xz[1]),angle[0])
    #Rotate x and y coordinates about z axis.
    xy = rotate((xz[0],yz[0]),angle[2])

    return [xy[0],xy[1],yz[1]]

def rotate_point_about_camera(point, camera_position, camera_angle):
    """Rotates a point about the camera."""
    #point[0]-camera_position[0] makes the camera the origin.
    #Angle is made negative to get it to go in the right direction.
    xyz = rotate_3D(
                    (
                        point[0]-camera_position[0],
                        point[1]-camera_position[1],
                        point[2]-camera_position[2]),
                    (
                        -camera_angle[0],
                        -camera_angle[1],
                        -camera_angle[2])
                    )
    return xyz


#!/usr/bin/python3
"""class BaseModel"""

import uuid
from datetime import datetime
import time
import models

class BaseModel:
    """class BaseModel defines common sttributes for all classes"""

    def __init__(self, *args, **kwargs):
        """initializes object"""
        if kwargs:
            for key, value in kwargs.items():
                if key == "created_at" or key == "updated_at":
                    value = datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f")
                self.__dict__[key] = value

        else:
            self.id = str(uuid.uuid4())
            self.created_at = datetime.now()
            self.updated_at = datetime.now()
            models.storage.new(self)

    def __str__(self):
        """prints class name and id"""
        return ("[{}] ({}) {}".format(type(self).__name__,
                self.__class__.__name__, self.id, self.__dict__))

    def to_dict(self):
        """returns dictionary containing key values"""
        newDict = self.__dict__.copy()
        newDict["__class__"] = self.__class__.__name__
        newDict["created_at"] = self.created_at.isoformat()
        newDict["updated_at"] = self.updated_at.isoformat()

        return newDict

    def save(self):
        """updates public instance with current datetime"""
        self.updated_at = datetime.now()
        models.storage.new(self)
        models.storage.save()