from tkinter import *
import tkinter as tk
import pymongo

def Salvar() :
    codigo = txt_codigo.get()
    nome = txt_nome.get()
    idade = int(txt_idade.get())
    cpf = txt_cpf.get()

    txt_codigo.delete(0,tk.END)
    txt_nome.delete(0,tk.END)
    txt_idade.delete(0,tk.END)
    txt_cpf.delete(0,tk.END)

    cliente = {"codigo": codigo, "nome": nome, "idade": idade, "cpf": cpf}
    collection.insert_one(cliente)

def Alterar() :
    codigo = txt_codigo.get()
    nome = txt_nome.get()
    idade = int(txt_idade.get())
    cpf = txt_cpf.get()

    cliente = {"codigo": codigo, "nome": nome, "idade": idade, "cpf": cpf}
    collection.update_one({"codigo":codigo},{"$set":cliente})

def Excluir() :
    codigo = txt_codigo.get()
    collection.delete_one({"codigo":codigo})

def Consultar() :
    codigo = txt_codigo.get()
    resultado = collection.find_one({"codigo":codigo})

    if resultado :
        txt_nome.insert(END, f"{resultado['nome']}\n")
        txt_idade.insert(END, f"{resultado['idade']}\n")
        txt_cpf.insert(END, f"{resultado['cpf']}\n")
    else :
        lbl_resultado.config(text="Nenhum resultado encontrado!")

# Configuração da janela
janela = Tk()
janela.title("Cadastro de clientes")

largura_janela = 600
altura_janela = 400

largura_tela = janela.winfo_screenwidth()
altura_tela = janela.winfo_screenheight()

x = (largura_tela - largura_janela) // 2
y = (altura_tela - altura_janela) // 2

janela.geometry(f"{largura_janela}x{altura_janela}+{x}+{y}")

fonte = ("Arial", 12)

# Frame para centralizar os itens na tela
tela = Frame(janela)
tela.pack(expand=True, padx=20, pady=20)

# Conexão com o BD
banco = pymongo.MongoClient("mongodb://localhost:27017/")
db = banco["banco"]
collection = db["clientes"]

# Componentes da tela

Label(tela, text="Cadastro de clientes", font=("Arial", 18)).grid(row=0, columnspan=2, pady=10)

lbl_codigo = Label(tela, text='Código:', font=fonte)
lbl_codigo.grid(row=1, column=0)
txt_codigo = Entry(tela)
txt_codigo.grid(row=1, column=1, pady=5)

lbl_nome = Label(tela, text='Nome:', font=fonte)
lbl_nome.grid(row=2, column=0)
txt_nome = Entry(tela)
txt_nome.grid(row=2, column=1, pady=5)

lbl_idade = Label(tela, text='Idade:', font=fonte)
lbl_idade.grid(row=3, column=0)
txt_idade = Entry(tela)
txt_idade.grid(row=3, column=1, pady=5)

lbl_cpf = Label(tela, text='CPF:', font=fonte)
lbl_cpf.grid(row=4, column=0)
txt_cpf = Entry(tela)
txt_cpf.grid(row=4, column=1, pady=5)

# Botões
btn_salvar = Button(tela, text='Salvar', font=fonte, command=Salvar)
btn_salvar.grid(row=5, column=0, pady=5, padx=5)

btn_alterar = Button(tela, text='Alterar', font=fonte, command=Alterar)
btn_alterar.grid(row=5, column=1, pady=5, padx=5)

btn_excluir = Button(tela, text='Excluir', font=fonte, command=Excluir)
btn_excluir.grid(row=6, column=0, pady=5, padx=5)

btn_consultar = Button(tela, text='Consultar', font=fonte, command=Consultar)
btn_consultar.grid(row=6, column=1, pady=5, padx=5)

btn_sair = Button(tela, text='Sair', font=fonte, command=janela.quit)
btn_sair.grid(row=7, column=0, pady=5, padx=5)

lbl_resultado = Label(tela, text='', font=fonte)

janela.mainloop()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('forum', '0007_proxyforumconversationattachment_proxyforumconversationpost_proxyforumconversationtopic_proxyforumco'),
    ]

    operations = [
        migrations.AlterField(
            model_name='forum',
            name='condominium',
            field=models.ForeignKey(null=True, blank=True, verbose_name='Condominium', to='system.Condominium'),
        ),
    ]


from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any

import pytest
import requests

from poetry.publishing.uploader import UploadError

if TYPE_CHECKING:
    import httpretty

    from cleo.testers.application_tester import ApplicationTester
    from pytest_mock import MockerFixture

    from tests.helpers import PoetryTestApplication

def test_publish_returns_non_zero_code_for_upload_errors(
    app: PoetryTestApplication,
    app_tester: ApplicationTester,
    http: type[httpretty.httpretty],
) -> None:
    http.register_uri(
        http.POST, "https://upload.pypi.org/legacy/", status=400, body="Bad Request"
    )

    exit_code = app_tester.execute("publish --username foo --password bar")

    assert exit_code == 1

    expected_output = """
Publishing simple-project (1.2.3) to PyPI
"""
    expected_error_output = """\
HTTP Error 400: Bad Request | b'Bad Request'
"""

    assert expected_output in app_tester.io.fetch_output()
    assert expected_error_output in app_tester.io.fetch_error()

@pytest.mark.filterwarnings("ignore::pytest.PytestUnhandledThreadExceptionWarning")
def test_publish_returns_non_zero_code_for_connection_errors(
    app: PoetryTestApplication,
    app_tester: ApplicationTester,
    http: type[httpretty.httpretty],
) -> None:
    def request_callback(*_: Any, **__: Any) -> None:
        raise requests.ConnectionError()

    http.register_uri(
        http.POST, "https://upload.pypi.org/legacy/", body=request_callback
    )

    exit_code = app_tester.execute("publish --username foo --password bar")

    assert exit_code == 1

    expected = str(UploadError(error=requests.ConnectionError()))

    assert expected in app_tester.io.fetch_error()

def test_publish_with_cert(
    app_tester: ApplicationTester, mocker: MockerFixture
) -> None:
    publisher_publish = mocker.patch("poetry.publishing.Publisher.publish")

    app_tester.execute("publish --cert path/to/ca.pem")

    assert [
        (None, None, None, Path("path/to/ca.pem"), None, False, False)
    ] == publisher_publish.call_args

def test_publish_with_client_cert(
    app_tester: ApplicationTester, mocker: MockerFixture
) -> None:
    publisher_publish = mocker.patch("poetry.publishing.Publisher.publish")

    app_tester.execute("publish --client-cert path/to/client.pem")
    assert [
        (None, None, None, None, Path("path/to/client.pem"), False, False)
    ] == publisher_publish.call_args

@pytest.mark.parametrize(
    "options",
    [
        "--dry-run",
        "--skip-existing",
        "--dry-run --skip-existing",
    ],
)
def test_publish_dry_run_skip_existing(
    app_tester: ApplicationTester, http: type[httpretty.httpretty], options: str
) -> None:
    http.register_uri(
        http.POST, "https://upload.pypi.org/legacy/", status=409, body="Conflict"
    )

    exit_code = app_tester.execute(f"publish {options} --username foo --password bar")

    assert exit_code == 0

    output = app_tester.io.fetch_output()
    error = app_tester.io.fetch_error()

    assert "Publishing simple-project (1.2.3) to PyPI" in output
    assert "- Uploading simple_project-1.2.3.tar.gz" in error
    assert "- Uploading simple_project-1.2.3-py2.py3-none-any.whl" in error

def test_skip_existing_output(
    app_tester: ApplicationTester, http: type[httpretty.httpretty]
) -> None:
    http.register_uri(
        http.POST, "https://upload.pypi.org/legacy/", status=409, body="Conflict"
    )

    exit_code = app_tester.execute(
        "publish --skip-existing --username foo --password bar"
    )

    assert exit_code == 0

    error = app_tester.io.fetch_error()
    assert "- Uploading simple_project-1.2.3.tar.gz File exists. Skipping" in error


import argparse
import importlib
import logging
import sys

# you should add the libraries which are not included in requirements.txt
MANUALLY_INSTALLED_LIBRARIES = [
    ('matplotlib', None),
]

parser = argparse.ArgumentParser()
parser.add_argument('--requirements', '-r', default='./requirements.txt', type=str,
                    help='requirements.txt')
args = parser.parse_args()

logging.basicConfig(
    level=logging.INFO,
    format="%(levelname)s: %(message)s")

logging.info("python version = " + sys.version)

# load requirements
with open(args.requirements, 'r') as f:
    lines = f.readlines()

# parse requirements
library_list = []
for line in lines:
    line = line.replace('\n', '')
    if '==' in line:
        name, version = line.split('==')
    elif '>=' in line:
        name, _ = line.split('>=')
        version = None
    else:
        name = line
        version = None
    library_list.append((name, version))

# add some library manually
library_list.extend(MANUALLY_INSTALLED_LIBRARIES)

# chech library availableness
logging.info("library availableness check start.")
logging.info("# libraries to be checked = %d" % len(library_list))
is_correct_installed_list = []
for idx, (name, version) in enumerate(library_list):
    try:
        importlib.import_module(name)
        logging.info("--> %s is installed." % name)
        is_correct_installed_list.append(True)
    except ImportError:
        logging.warn("--> %s is not installed." % name)
        is_correct_installed_list.append(False)
logging.info("library availableness check done.")
logging.info("%d / %d libraries are correctly installed." % (
    sum(is_correct_installed_list), len(library_list)))

# check library version
if len(library_list) != sum(is_correct_installed_list):
    logging.info("please try to setup again and then re-run this script.")
    sys.exit(1)
else:
    num_version_specified = sum([True if v is not None else False for n, v in library_list])
    logging.info("library version check start.")
    logging.info("# libraries to be checked = %d" % num_version_specified)
    is_correct_version_list = []
    for idx, (name, version) in enumerate(library_list):
        if version is not None:
            try:
                lib = importlib.import_module(name)
                if hasattr(lib, "__version__"):
                    assert lib.__version__ == version
                    logging.info("--> %s version is matched." % name)
                    is_correct_version_list.append(True)
                else:
                    logging.info("--> %s has no version info, but version is specified." % name)
                    logging.info("--> maybe it is better to reinstall the latest version.")
                    is_correct_version_list.append(False)
            except AssertionError:
                logging.warn("--> %s version is not matched (%s==%s)." % (lib.__version__, version))
                is_correct_version_list.append(False)
    logging.info("library version check done.")
    logging.info("%d / %d libraries are correct version." % (
        sum(is_correct_version_list), num_version_specified))

# check cuda availableness
if sum(is_correct_version_list) != num_version_specified:
    logging.info("please try to setup again and then re-run this script.")
    sys.exit(1)
else:
    logging.info("cuda availableness check start.")
    import chainer
    try:
        assert chainer.backends.cuda.available
        logging.info("--> cuda is available in chainer.")
    except AssertionError:
        logging.warn("--> it seems that cuda is not available in chainer.")
    try:
        assert chainer.backends.cuda.cudnn_enabled
        logging.info("--> cudnn is available in chainer.")
    except AssertionError:
        logging.warn("--> it seems that cudnn is not available in chainer.")
    logging.info("cuda availableness check done.")


import warnings
from abc import abstractmethod, ABC
from functools import partial

import numpy as np
from matplotlib import pyplot as plt
from scipy.integrate import dblquad

class Antenna(ABC):
    polarization = None
    amplitude = None
    _radiated_power = None

    @abstractmethod
    def field_pattern(self, theta, phi):
        """Patrón de campo de la antena"""

    def power_pattern(self, theta: float, phi: float) -> float:
        """Patrón de potencia, cuadrado del patrón de campo"""
        return self.field_pattern(theta, phi) ** 2

    @property
    def radiated_power(self) -> float:
        """Potencia radiada en todas las direcciones"""
        if self._radiated_power is None:
            f = lambda t, p: (self.field_pattern(t, p) ** 2) * np.sin(t)
            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                self._radiated_power = dblquad(f, 0, 2 * np.pi, 0, np.pi)[0]
        return self._radiated_power

    def directivity(self, theta: float, phi: float) -> float:
        """Función directividad calculada a partir del patrón de potencia y la potencia radiada"""
        return 4 * np.pi * self.power_pattern(theta, phi) / self.radiated_power

    @property
    def max_directivity(self) -> float:
        """Implementación por defecto de la directividad máxima"""
        theta = np.linspace(0, np.pi, 100)
        phi = np.linspace(0, 2 * np.pi, 100)
        theta, phi = np.meshgrid(theta, phi)
        r = np.vectorize(self.directivity)(theta, phi)
        return np.max(r)

    def _horizontal_vertical_patterns(self, theta: np.ndarray, phi: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
        horizontal = np.array(list(map(partial(self.power_pattern, np.pi / 2), phi)))
        vertical = np.array(list(map(partial(self.power_pattern, phi=np.pi / 2), theta)) +
                            list(map(partial(self.power_pattern, phi=3 * np.pi / 2), np.flip(theta))))
        return horizontal, vertical

    def plot_radiation_pattern(self, polar=True, field=False, log_scale=False):
        phi = np.linspace(0, 2 * np.pi, 200)
        theta_pattern = np.linspace(0, np.pi, 100)
        horizontal, vertical = self._horizontal_vertical_patterns(theta_pattern, phi)
        theta = np.linspace(0, 2 * np.pi, 200)
        if field:
            horizontal, vertical = np.sqrt(horizontal), np.sqrt(vertical)
        horizontal /= np.max(horizontal)
        vertical /= np.max(vertical)
        plt.subplots_adjust(wspace=0.4)
        h_plane = plt.subplot(1, 2, 1, projection='polar')
        v_plane = plt.subplot(1, 2, 2, projection='polar')
        if log_scale:
            horizontal = 10 * np.log10(1e-4 + horizontal)
            vertical = 10 * np.log10(1e-4 + vertical)
            h_plane.set_rlim(-40)
            v_plane.set_rlim(-40)
        h_plane.set_theta_zero_location('N')
        h_plane.set_theta_direction(-1)
        h_plane.plot(phi, horizontal)
        v_plane.set_theta_zero_location('N')
        v_plane.set_theta_direction(-1)
        v_plane.plot(theta, vertical)
        plt.show()

    def plot_3d_pattern(self, field=False):
        """Gráfico 3d del patrón de radiación."""
        theta, phi = np.meshgrid(np.linspace(0, np.pi, 100), np.linspace(0, 2 * np.pi, 100))
        r = np.vectorize(self.field_pattern)(theta, phi) ** (1 if field else 2)
        x = r * np.sin(theta) * np.cos(phi)
        y = r * np.sin(theta) * np.sin(phi)
        z = r * np.cos(theta)
        fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
        # to have same length in all axes
        max_range = np.array([x.max() - x.min(), y.max() - y.min(), z.max() - z.min()]).max() / 2.0
        mid_x = (x.max() + x.min()) * 0.5
        mid_y = (y.max() + y.min()) * 0.5
        mid_z = (z.max() + z.min()) * 0.5
        ax.set_xlim(mid_x - max_range, mid_x + max_range)
        ax.set_ylim(mid_y - max_range, mid_y + max_range)
        ax.set_zlim(mid_z - max_range, mid_z + max_range)
        # cmap according to r value
        norm = plt.Normalize(0, np.max(r))
        colors = norm(r)
        cmap = plt.cm.get_cmap('jet')
        ax.plot_surface(x, y, z, alpha=0.8, rstride=1, cstride=1, cmap='jet', facecolors=cmap(colors),
                        linewidth=0, shade=True)
        plt.show()


from __future__ import print_function
import os.path, stat, getopt, logging, traceback, sys
import json
import shlex
import multiprocessing
import importlib

import pysys
from pysys import log
from pysys import __version__
from pysys.constants import *
from pysys.launcher import createDescriptors
from pysys.utils.fileutils import toLongPathSafe, fromLongPathSafe
from pysys.exceptions import UserError
from pysys.config.project import Project
import pysys.utils.osutils

class ConsoleLaunchHelper(object):
	def __init__(self, workingDir, name=""):
		self.workingDir = workingDir
		self.arguments = []
		self.record = False
		self.purge = False
		self.type = None
		self.trace = None
		self.progress = False
		self.includes = []
		self.excludes = []
		self.cycle = 1
		self.outsubdir = DEFAULT_OUTDIR
		self.modeinclude = []
		self.modeexclude = []
		self.threads = '1' # as of 2.2 we leave this as a string and decide actual value later
		self.name=name
		self.userOptions = {}
		self.descriptors = []
		self.grep = None
		self.sort = None
		self.optionString = 'hrpyv:a:t:i:e:c:o:m:n:j:b:X:gG:s:'
		self.optionList = ["help","record","purge","verbosity=","type=","trace=","include=","exclude=","cycle=","outdir=",
			"mode=","modeinclude=","modeexclude=","threads=", "abort=", 'validateOnly', 'vo', 'progress', 'printLogs=', 'grep=',
			'ci', 'sort=',
			'writer=',
			'preserveEmptyOutputs',
			]

	def getProjectHelp(self):
		help = Project.getInstance().projectHelp
		if not help: return None

		lines = help.rstrip().split('\n')
		# delete blank lines at start
		while lines and not lines[0].strip():
			del lines[0]
		if not lines: return None

		# strip initial indentation and convert tabs to spaces
		for i in range(len(lines)):
			l = lines[i].replace('\t', ' '*3) # this is the amount of indentation we use for the standard help message
			if i == 0:
				indenttostrip = len(l) - len(l.lstrip())
				indenttoadd = ''
				if l.strip().startswith('-'):
					# align everything with existing content
					indenttoadd = ' '*3

			if len(l)-len(l.lstrip()) >= indenttostrip:
				l = indenttoadd+l[indenttostrip:]
			lines[i] = l

		# if user hasn't provided their own heading, add ours
		if '----' not in help:
			lines = ['Project help', '-'*12]+lines
		return '\n'.join(lines)

	def printUsage(self, printXOptions):
		"""
		Print the pysys run usage.
		"""
		# printXOptions is not documented so probably little-used
		_PYSYS_SCRIPT_NAME = os.path.basename(sys.argv[0]) if '__main__' not in sys.argv[0] else 'pysys.py'
		print("\nPySys System Test Framework (version %s)" % __version__)
		print("\nUsage: %s %s [TESTIDS | OPTIONS]*" % (_PYSYS_SCRIPT_NAME, self.name))
		# chars                                                                |80                                      | 120
		print("""
Execution options
-----------------
   -c, --cycle     NUM         run each test the specified number of times
   -o, --outdir    STRING      set the directory to use for each test's output (a relative or absolute path);
                               setting this is helpful for tagging/naming test output for different invocations
                               of PySys as you try out various changes to the application under test; tests can
                               access the final dir name of the outdir using the ${outDirName} project property
   -j, --threads   NUM | xNUM  set the number of jobs (threads) to run tests in parallel (defaults to 1);
                               specify either an absolute number, or a multiplier on the number of CPUs e.g. "x1.5";
                   auto | 0    equivalent to x1.0 (or the PYSYS_DEFAULT_THREADS env var if set)
       --ci                    set optimal options for automated/non-interactive test execution in a CI job:
                                 --purge --record -j0 --type=auto --mode=ALL --printLogs=FAILURES -XcodeCoverage
   -v, --verbosity LEVEL       set the verbosity for most pysys logging (CRIT, WARN, INFO, DEBUG)
                   CAT=LEVEL   set the verbosity for a PySys/Python logging category e.g. -vassertions=, -vprocess=
   -y, --validateOnly, --vo    test the validate() method without re-running execute()
   -h, --help                  print this message

   -Xkey[=value]               set user-defined override attributes to be set on the testcase and runner instances. The
                               value is available to Python as "self.key". Value is True if not explicitly provided.
""".rstrip())
		if printXOptions: printXOptions()
		print("""
Advanced:
   -r, --record                use configured 'writers' to record the test results (e.g. XML, JUnit, etc)
       --writer    NAME        enables the specified writer (in addition to any writers already enabled automatically),
		                       by alias or classname, for example "--writer TextResultsWriter"
   -g, --progress              enables the writer that prints progress updates after completion of each test
   -p, --purge                 purge files except run.log from the output directory to save space (unless test fails)
   --preserveEmptyOutputs      prevents the usual deletion of empty files and directories after a test completes
   --printLogs     STRING      indicates for which outcome types the run.log output will be printed to the stdout
                               console; options are: all|none|failures (default is all).
   -s, --sort      STRING      sort by: random (useful for performance testing and and reproducing test races)
   -b, --abort     STRING      set the default abort on error property (true|false, overrides
                               that specified in the project properties)
   -XcodeCoverage              enable collecting and reporting on code coverage with all coverage writers in the project
   -XupdateDiffReferences      this is a special command for automatically updating the reference files when an
                               assertDiff fails

The PYSYS_DEFAULT_ARGS environment variable can be used to specify any pysys run arguments that you wish to use on
a per-user basis, for example: PYSYS_DEFAULT_ARGS=--progress --writer=TextResultsWriter --outdir=__pysys_output

Selection and filtering options
-------------------------------
   -i, --include   STRING      set the test groups to include (can be specified multiple times)
   -e, --exclude   STRING      set the test groups to exclude (can be specified multiple times)
   -G, --grep      STRING      run only tests whose title or id contains the specified regex (case insensitive)
   -m, --mode, --modeinclude ALL,PRIMARY,!PRIMARY,MyMode1,!MyMode2,...
                               run tests in the specifies mode(s):
                                 - use PRIMARY to select the test's first/main mode(s) (this is the default)
                                 - use ALL to select all modes
                                 - use !MODE as an alias for modeexclude
                                 - regular expressions can be used
   --modeexclude MyMode1,MyMode2,...
                               run tests excluding specified mode(s); excludes take precedence over includes
   -a, --type      STRING      set the test type to run (auto or manual, default is both)"
   -t, --trace     STRING      set the requirement id for the test run

Test identifiers
----------------
By default, PySys executes all available tests under the current directory. Alternatively to run just a subset,
one or more tests or sequences of tests can be specified on the command line. In both cases, tests are filtered
based on the selection options listed above (e.g. --include/--exclude).

Tests should contain only alphanumeric and the underscore characters. The following syntax is used
to select an individual test, or a sequence of numbered tests:

   Test_001                   - a single testcase with id equal to or ending with Test_001
   _001                       - a single testcase with id equal to or ending with _001
   1                          - a single testcase ending with number 1 (but not ending '11')
                                (if it has multiple modes, runs the primary mode(s), or uses --mode)
   Test_001~ModeA             - run testcase with id Test_001 in ModeA
   :Test_002                  - all tests up to and including the testcase with id Test_002
   Test_001:                  - all tests from Test_001 onwards
   Test_001:Test_002          - all tests between tests with ids Test_001 and Test_002 (inclusive)
   2 Test_001                 - Test_001 and Test_002
   ^Test.*                    - All tests matching the specified regex

e.g.
   {scriptname} run -c2 -w4 -u -j=x1.5 Test_007 Test_001: 3:5
   {scriptname} run -vDEBUG --include MYTESTS -Xhost=localhost

To list the modes supported by the tests under the current directory, use "pysys ls --modes".
""".format(scriptname=_PYSYS_SCRIPT_NAME))

		# show project help at the end so it's more prominent
		Project.findAndLoadProject()
		help = self.getProjectHelp()
		if help: print(help)

		sys.exit()

	def parseArgs(self, args, printXOptions=None):
		# add any default args first; shlex.split does a great job of providing consistent parsing from str->list,
		# but need to avoid mangling \'s on windows; since this env var will be different for each OS no need for consistent win+unix behaviour
		if os.getenv('PYSYS_DEFAULT_ARGS',''):
			log.info('Using PYSYS_DEFAULT_ARGS = %s'%os.environ['PYSYS_DEFAULT_ARGS'])
			args = shlex.split(os.environ['PYSYS_DEFAULT_ARGS'].replace(os.sep, os.sep*2 if os.sep=='\\' else os.sep)) + args

		printLogsDefault = PrintLogs.ALL
		ci ='--ci' in args
		if ci:
			# to ensure identical behaviour, set these as if on the command line
			# (printLogs we don't set here since we use the printLogsDefault mechanism to allow it to be overridden
			# by CI writers and/or the command line)
			# Also we don't set --modes=ALL here to allow for it to be overridden explicitly if needed
			args = ['--purge', '--record', '-j0', '--type=auto', '--exclude=manual', '-XcodeCoverage']+args
			printLogsDefault = PrintLogs.FAILURES

		try:
			optlist, self.arguments = getopt.gnu_getopt(args, self.optionString, self.optionList)
		except Exception:
			log.warning("Error parsing command line arguments: %s" % (sys.exc_info()[1]))
			sys.exit(1)

		log.debug('PySys arguments: tests=%s options=%s', self.arguments, optlist)

		EXPR1 = re.compile(r"^[\w\.]*=.*$")
		EXPR2 = re.compile(r"^[\w\.]*$")

		printLogs = None
		defaultAbortOnError = None

		logging.getLogger('pysys').setLevel(logging.INFO)

		# as a special case, set a non-DEBUG log level for the implementation of assertions
		# so that it doesn't get enabled with -vDEBUG only -vassertions=DEBUG
		# as it is incredibly verbose and slow and not often useful
		logging.getLogger('pysys.assertions').setLevel(logging.INFO)

		# special hidden dict of extra values to pass to the runner, since we can't change
		# the public API now
		self.userOptions['__extraRunnerOptions'] = __extraRunnerOptions = {}

		for option, value in optlist:
			if option in ("-h", "--help"):
				self.printUsage(printXOptions)

			elif option in ['--ci']:
				continue # handled above

			elif option in ("-r", "--record"):
				self.record = True

			elif option in ("-p", "--purge"):
				self.purge = True

			elif option in ['--preserveEmptyOutputs']:
				__extraRunnerOptions['preserveEmptyOutputs'] = True

			elif option in ("-v", "--verbosity"):
				verbosity = value
				if '=' in verbosity:
					loggername, verbosity = value.split('=')
					if loggername.startswith('python:'): # this is weird but was documented, so leave it in place just in case someone is using it
						loggername = loggername[len('python:'):]
						assert not loggername.startswith('pysys'), 'Cannot use python: with pysys.*' # would produce a duplicate log handler
				else:
					loggername = None

				if verbosity.upper() == "DEBUG":
					verbosity = logging.DEBUG
				elif verbosity.upper() == "INFO":
					verbosity = logging.INFO
				elif verbosity.upper() == "WARN":
					verbosity = logging.WARN
				elif verbosity.upper() == "CRIT":
					verbosity = logging.CRITICAL
				else:
					log.warning('Invalid log level "%s"'%verbosity)
					sys.exit(1)

				if loggername is None:
					# when setting global log level to a higher level like WARN etc we want to affect stdout but
					# not necessarily downgrade the root level (would make run.log less useful and break
					# some PrintLogs behaviour)
					stdoutHandler.setLevel(verbosity)
					if verbosity == logging.DEBUG:
						logging.getLogger('pysys').setLevel(logging.DEBUG)
						logging.getLogger().setLevel(logging.DEBUG)
				else:
					# for specific level setting we need the opposite - only change stdoutHandler if we're
					# turning up the logging (since otherwise it wouldn't be seen) but also change the specified level
					# make the user of "pysys." prefix optional
					logging.getLogger(loggername).setLevel(verbosity)
					logging.getLogger('pysys.'+loggername).setLevel(verbosity)

			elif option in ("-a", "--type"):
				self.type = value
				if self.type not in ["auto", "manual"]:
					log.warning("Unsupported test type - valid types are auto and manual")
					sys.exit(1)

			elif option in ("-t", "--trace"):
				self.trace = value

			elif option in ("-i", "--include"):
				self.includes.append(value)

			elif option in ("-e", "--exclude"):
				self.excludes.append(value)

			elif option in ("-c", "--cycle"):
				try:
					self.cycle = int(value)
				except Exception:
					print("Error parsing command line arguments: A valid integer for the number of cycles must be supplied")
					sys.exit(1)

			elif option in ("-o", "--outdir"):
				value = os.path.normpath(value)
				if os.path.isabs(value) and not value.startswith('\\\\?\\'): value = fromLongPathSafe(toLongPathSafe(value))
				self.outsubdir = value

			elif option in ("-m", "--mode", "--modeinclude"):
				self.modeinclude = self.modeinclude+[x.strip() for x in value.split(',')]

			elif option in ["--modeexclude"]:
				self.modeexclude = self.modeexclude+[x.strip() for x in value.split(',')]

			elif option in ["-n", "-j", "--threads"]:
				if value.lower()=='auto': value='0'
				self.threads = value

			elif option in ("-b", "--abort"):
				defaultAbortOnError = str(value.lower()=='true')

			elif option in ["-g", "--progress"]:
				self.progress = True

			elif option in ['--writer']:
				__extraRunnerOptions.setdefault('writerEnable', []).append(value)

			elif option in ["--printLogs"]:
				printLogs = getattr(PrintLogs, value.upper(), None)
				if printLogs is None:
					print("Error parsing command line arguments: Unsupported --printLogs value '%s'"%value)
					sys.exit(1)

			elif option in ["-X"]:
				if '=' in value:
					key, value = value.split('=', 1)
				else:
					key, value = value, 'true'

				# best not to risk unintended consequences with matching of other types, but for boolean
				# it's worth it to resolve the inconsistent behaviour of -Xkey=true and -Xkey that existed until 1.6.0,
				# and because getting a bool where you expected a string is a bit more likely to give an exception
				# and be noticed that getting a string where you expected a boolean (e.g. the danger of if "false":)
				if value.lower() == 'true':
					value = True
				elif value.lower() == 'false':
					value = False

				self.userOptions[key] = value

			elif option in ("-y", "--validateOnly", "--vo"):
				self.userOptions['validateOnly'] = True

			elif option in ("-G", "--grep"):
				self.grep = value

			elif option in ("-s", "--sort"):
				self.sort = value
				if value not in ['random']:
					print("The only supported sort type for pysys run is currently 'random'")
					sys.exit(10)

			else:
				print("Unknown option: %s"%option)
				sys.exit(1)

		if ci and not self.modeinclude: # only set this if there is no explicit --mode arguments
			self.modeinclude = ['ALL']

		# log this once we've got the log levels setup
		log.debug('PySys is installed at: %s; python from %s', os.path.dirname(pysys.__file__), sys.executable)
		log.debug('Working dir=%s, args=%s', os.getcwd(), " ".join(sys.argv))
		log.debug('Main environment vars: %s', '\n  '.join(f'{envvar}={os.environ[envvar]}' for envvar in sorted(os.environ.keys())
			if envvar.endswith('PATH') or envvar.startswith(('PYTHON', 'PYSYS'))))

		# retained for compatibility, but PYSYS_DEFAULT_ARGS is a better way to achieve the same thing
		if os.getenv('PYSYS_PROGRESS','').lower()=='true': self.progress = True

		__extraRunnerOptions.update({
			'progressWritersEnabled':self.progress,
			'printLogs': printLogs,
			'printLogsDefault': printLogsDefault, # to use if not provided by a CI writer or cmdline
			'sort': self.sort
		})

		# load project AFTER we've parsed the arguments, which opens the possibility of using cmd line config in
		# project properties if needed
		Project.findAndLoadProject(outdir=self.outsubdir)

		if defaultAbortOnError is not None: setattr(Project.getInstance(), 'defaultAbortOnError', defaultAbortOnError)

		descriptors = createDescriptors(self.arguments, self.type, self.includes, self.excludes, self.trace, self.workingDir,
			modeincludes=self.modeinclude, modeexcludes=self.modeexclude, expandmodes=True)
		descriptors.sort(key=lambda d: [d.executionOrderHint, d._defaultSortKey])

		# No exception handler above, as any createDescriptors failure is really a fatal problem that should cause us to
		# terminate with a non-zero exit code; we don't want to run no tests without realizing it and return success

		if self.grep:
			regex = re.compile(self.grep, flags=re.IGNORECASE)
			descriptors = [d for d in descriptors if (regex.search(d.id) or regex.search(d.title))]

		return self.record, self.purge, self.cycle, None, self.threads, self.outsubdir, descriptors, self.userOptions

def decideWorkerThreads(threads: str):
	N_CPUS = pysys.utils.osutils._initUsableCPUCount() # may be fractional

	# Following the example of Python's ThreadPoolExecutor, put an upper bound on the number of CPUs PySys will use
	# to avoid taking over wide machines pointlessly, given the GIL will limit how much can really be used
	# Hard to know what it should be, so for now, set it to a high number
	maxThreads = Project.getInstance().getProperty('pysysMaxWorkerThreads', 100)

	if threads.lower().startswith('x'):
		threads = max(1, int(float(threads[1:])*N_CPUS))
		if threads>maxThreads: threads = maxThreads
	else:
		threads = int(threads)
		if threads <= 0: # = auto mode
			threads = int(os.getenv('PYSYS_DEFAULT_THREADS', N_CPUS))
			if threads>maxThreads: threads = maxThreads

	return threads

def runTest(args):
	try:
		launcher = ConsoleLaunchHelper(os.getcwd(), "run")
		args = launcher.parseArgs(args)

		cls = Project.getInstance().runnerClassname.split('.')
		module = importlib.import_module('.'.join(cls[:-1]))

		# calculate the threads at this point AFTER runner has been imported (allowing monkey-patching in custom runner if needed)
		args = list(args)
		args[4] = decideWorkerThreads(args[4])

		runner = getattr(module, cls[-1])(*args)
		runner.start()

		for cycledict in runner.results.values():
			for outcome in OUTCOMES:
				if outcome.isFailure() and cycledict.get(outcome, None): sys.exit(2)
		sys.exit(0)
	except Exception as e:
		sys.stderr.write('\nPYSYS FATAL ERROR: %s\n' % e)
		if not isinstance(e, UserError): traceback.print_exc()
		sys.exit({
			'The supplied options did not result in the selection of any tests':9, # special error code for specific things users might treat differently
			}.get(str(e), 10)) # by default return 10


## Brian Blaylock
## April 23, 2021

"""
==================================
HRRR-B Extension: xarray accessors
==================================

Extend the xarray capabilities with a custom accessor.
http://xarray.pydata.org/en/stable/internals.html#extending-xarray

"""
import warnings

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import xarray as xr
from paint.radar import cm_reflectivity
from paint.radar2 import cm_reflectivity
from paint.standard2 import cm_dpt, cm_pcp, cm_rh, cm_tmp, cm_wind
from toolbox.cartopy_tools_OLD import common_features, pc

@xr.register_dataset_accessor("hrrrb")
class hrrrb_accessor:
    def __init__(self, xarray_obj):
        self._obj = xarray_obj
        self._center = None

    @property
    def center(self):
        """Return the geographic center point of this dataset."""
        if self._center is None:
            # we can use a cache on our accessor objects, because accessors
            # themselves are cached on instances that access them.
            lon = self._obj.latitude
            lat = self._obj.longitude
            self._center = (float(lon.mean()), float(lat.mean()))
        return self._center

    def plot(self, ax=None, common_features_kw={}, **kwargs):
        """Plot data on a map."""
        ds = self._obj

        _level_units = dict(
            adiabaticCondensation="adiabatic condensation",
            atmosphere="atmosphere",
            atmosphereSingleLayer="atmosphere single layer",
            boundaryLayerCloudLayer="boundary layer cloud layer",
            cloudBase="cloud base",
            cloudCeiling="cloud ceiling",
            cloudTop="cloud top",
            depthBelowLand="m",
            equilibrium="equilibrium",
            heightAboveGround="m",
            heightAboveGroundLayer="m",
            highCloudLayer="high cloud layer",
            highestTroposphericFreezing="highest tropospheric freezing",
            isobaricInhPa="hPa",
            isobaricLayer="hPa",
            isothermZero="0 C",
            isothermal="K",
            level="m",
            lowCloudLayer="low cloud layer",
            meanSea="MSLP",
            middleCloudLayer="middle cloud layer",
            nominalTop="nominal top",
            pressureFromGroundLayer="Pa",
            sigma="sigma",
            sigmaLayer="sigmaLayer",
            surface="surface",
        )

        for var in ds.data_vars:
            print("cfgrib variable:", var)
            print("GRIB_cfName", ds[var].GRIB_cfName)
            print("GRIB_cfVarName", ds[var].GRIB_cfVarName)
            print("GRIB_name", ds[var].GRIB_name)
            print("GRIB_units", ds[var].GRIB_units)
            print("GRIB_typeOfLevel", ds[var].GRIB_typeOfLevel)
            print()
            ds[var].attrs["units"] = (
                ds[var]
                .attrs["units"]
                .replace("**-1", "$^{-1}$")
                .replace("**-2", "$^{-2}$")
            )

            dpi = common_features_kw.pop("dpi", 150)
            figsize = common_features_kw.pop("figsize", [10, 5])
            fig, ax = plt.subplots(
                1, 1, subplot_kw=dict(projection=ds.crs), dpi=dpi, figsize=figsize
            )

            default = dict(scale="50m", ax=ax, crs=ds.crs, STATES=True, BORDERS=True)
            common_features_kw = {**default, **common_features_kw}
            common_features(**common_features_kw)

            title = ""
            kwargs = {}
            cbar_kwargs = dict(pad=0.01)

            if ds[var].GRIB_cfVarName in ["d2m", "dpt"]:
                ds[var].attrs["GRIB_cfName"] = "dew_point_temperature"

            if ds[var].GRIB_cfName == "air_temperature":
                kwargs = {**cm_tmp().cmap_kwargs, **kwargs}
                cbar_kwargs = {**cm_tmp().cbar_kwargs, **cbar_kwargs}
                if ds[var].GRIB_units == "K":
                    ds[var] -= 273.15
                    ds[var].attrs["GRIB_units"] = "C"
                    ds[var].attrs["units"] = "C"

            elif ds[var].GRIB_cfName == "dew_point_temperature":
                kwargs = {**cm_dpt().cmap_kwargs, **kwargs}
                cbar_kwargs = {**cm_dpt().cbar_kwargs, **cbar_kwargs}
                if ds[var].GRIB_units == "K":
                    ds[var] -= 273.15
                    ds[var].attrs["GRIB_units"] = "C"
                    ds[var].attrs["units"] = "C"

            elif ds[var].GRIB_name == "Total Precipitation":
                title = "-".join(
                    [f"F{int(i):02d}" for i in ds[var].GRIB_stepRange.split("-")]
                )
                ds[var] = ds[var].where(ds[var] != 0)
                kwargs = {**cm_pcp().cmap_kwargs, **kwargs}
                cbar_kwargs = {**cm_pcp().cbar_kwargs, **cbar_kwargs}

            elif ds[var].GRIB_name == "Maximum/Composite radar reflectivity":
                ds[var] = ds[var].where(ds[var] >= 0)
                cbar_kwargs = {**cm_reflectivity().cbar_kwargs, **cbar_kwargs}
                kwargs = {**cm_reflectivity().cmap_kwargs, **kwargs}

            elif ds[var].GRIB_cfName == "relative_humidity":
                cbar_kwargs = {**cm_rh().cbar_kwargs, **cbar_kwargs}
                kwargs = {**cm_rh().cmap_kwargs, **kwargs}

            else:
                cbar_kwargs = {
                    **dict(
                        label=f"{ds[var].GRIB_parameterName.strip().title()} ({ds[var].units})"
                    ),
                    **cbar_kwargs,
                }

            p = ax.pcolormesh(
                ds.longitude, ds.latitude, ds[var], transform=pc, **kwargs
            )
            plt.colorbar(p, ax=ax, **cbar_kwargs)

            VALID = pd.to_datetime(ds.valid_time.data).strftime("%H:%M UTC %d %b %Y")
            RUN = pd.to_datetime(ds.time.data).strftime("%H:%M UTC %d %b %Y")
            FXX = f"F{pd.to_timedelta(ds.step.data).total_seconds()/3600:02.0f}"

            level_type = ds[var].GRIB_typeOfLevel
            if level_type in _level_units:
                level_units = _level_units[level_type]
            else:
                level_units = "unknown"

            if level_units.lower() in ["surface", "atmosphere"]:
                level = f"{title} {level_units}"
            else:
                level = f"{ds[var][level_type].data:g} {level_units}"

            ax.set_title(
                f"Run: {RUN} {FXX}",
                loc="left",
                fontfamily="monospace",
                fontsize="x-small",
            )
            ax.set_title(f"HRRR {level}", loc="center", fontweight="semibold")
            ax.set_title(
                f"Valid: {VALID}",
                loc="right",
                fontfamily="monospace",
                fontsize="x-small",
            )

            # Set extent (could do this more efficiently by storing the data elsewhere)
            new = ds.crs.transform_points(pc, ds.longitude.data, ds.latitude.data)
            LONS = new[:, :, 0]
            LATS = new[:, :, 1]
            ax.set_extent([LONS.min(), LONS.max(), LATS.min(), LATS.max()], crs=ds.crs)

        return ax


#!/usr/bin/python3
import os
import subprocess

import datetime
from flask import Flask, render_template, request, redirect, url_for, flash, make_response
from flask.ext.sqlalchemy import SQLAlchemy
import requests, re
import socket

from haproxy import launch_haproxy

app = Flask(__name__)
with open("secret.txt") as f:
    app.secret_key = f.read()
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///user.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
my_ip = s.getsockname()[0]
s.close()

class Port(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    timestamp = db.Column(db.DateTime, default=datetime.datetime.now() - datetime.timedelta(minutes=3))
    port = db.Column(db.Integer, unique=True)

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

class ActionByIPAdrr(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    timestamp = db.Column(db.DateTime, default=datetime.datetime.now())
    ip = db.Column(db.String, unique=True)

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

def is_valid_ip(ip: str) -> bool:
    try:
        socket.inet_aton(ip)
        return True
    except:
        return False

def is_valid_port(port: str) -> bool:
    try:
        p = int(port)
        return 0 < p < 65536
    except:
        return False

def is_http_header(header: str) -> bool:
    m = re.match(r'\w{3,20} \w{3,200}', header)
    return m is not None

def is_valid_ssl_cert(ssl_cert: str) -> bool:
    p = subprocess.Popen(["openssl", "x509", "-text", "-noout"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
    _ = p.communicate(input=ssl_cert.encode())
    return p.returncode == 0

def is_valid_ssl_key(ssl_key):
    p = subprocess.Popen(["openssl", "rsa", "-check"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
    _ = p.communicate(input=ssl_key.encode())
    return p.returncode == 0

def port_is_used(port):
    return os.system("docker ps | grep '0.0.0.0:" + str(port) + "->" + str(port) + "/tcp'") == 0

@app.route("/", methods=["GET", "POST"])
def index():
    if request.method == "GET":
        return render_template('index.html')
    else:
        try:
            url_recaptcha = "https://www.google.com/recaptcha/api/siteverify"
            data_recaptcha = {
                "secret": "XXX",
                "response": request.form["g-recaptcha-response"]
            }
            ip1 = request.form["ip_backend1"]
            ip2 = request.form["ip_backend2"]
            port1 = request.form["port_backend1"]
            port2 = request.form["port_backend2"]
            ssl_cert = request.form["ssl_cert"]
            ssl_key = request.form["ssl_key"]
            http_headers = request.form["http_headers"]

            ip_client = request.remote_addr
            if ip_client != "134.214.182.12":
                action = ActionByIPAdrr.query.filter_by(ip=ip_client).first()
                if action and action.timestamp > datetime.datetime.now() - datetime.timedelta(seconds=20):
                    flash("Sorry you can only launch an instance every 20s.")
                    return redirect("/")
                elif action:
                    action.timestamp = datetime.datetime.now()
                else:
                    action = ActionByIPAdrr(ip=ip_client)
                    db.session.add(action)
            if http_headers:
                http_headers = http_headers.split(";")
                for http_header in http_headers:
                    if http_header and not is_http_header(http_header):
                        flash("Invalid HTTP header")
                        return redirect('/')
            if (ssl_key and not ssl_cert) or (ssl_cert and not ssl_key):
                flash("If you want to use SSL, you need to specify both SSL private key and SSL certificate.")
                return redirect("/")
            if ssl_cert and not is_valid_ssl_cert(ssl_cert):
                flash("SSL Cert not valid")
                return redirect('/')
            if ssl_key and not is_valid_ssl_key(ssl_key):
                flash("SSL Private Key not valid")
                return redirect('/')
            if ssl_key:
                ssl = (ssl_cert, ssl_key)
            else:
                ssl = None
            if not is_valid_ip(ip1) or not is_valid_ip(ip2) or not is_valid_port(port1) or not is_valid_port(port2):
                flash("Invalid IP/PORT")
                return redirect("/")
            try:
                pswd = request.args["pswd"]
            except:
                pswd = ""
            if pswd != "zTOkNv6AIFFTFuZLKFRPTdiTCwHIpvqtBlUuY4103lS2Xf8nnX":
                r = requests.post(url_recaptcha, data=data_recaptcha)
                resp_data = r.json()
                if not resp_data.get("success", False):
                    flash("Invalid captcha")
                    return redirect("/")

            port = Port.query.filter(Port.timestamp < datetime.datetime.now() - datetime.timedelta(minutes=2, seconds=30)).first()
            if not port or port_is_used(port.port):
                flash("Sorry I couldn't find a free port, wait a bit please.")
                return redirect("/")
            port.timestamp = datetime.datetime.now()
            db.session.commit()
            launch_haproxy.launch(port.port, http_headers, ip1 + ":" + port1, ip2 + ":" + port2, ssl)
            http = "http"
            if ssl:
                http += "s"
            flash("The load balancer has been successfully launched. It will be available for 2 minutes at: " + http + "://" + my_ip + ":" + str(port.port))
        except Exception as e:
            print(e)
            flash("Error launching your proxy. Please try again.")
        return redirect('/')

if __name__ == '__main__':
    app.run()


import argparse
import logging

def get_args():
    """
    Parse arguments
    """
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--std_dir",
        type=str,
        required=True,
        help="The directory of standardised and typed files.",
    )
    parser.add_argument(
        "--final_dir",
        type=str,
        required=True,
        help="The directory of final derived files.",
    )
    return parser.parse_args()

def init_logger(module_name: str):
    logging.basicConfig(
        format="%(asctime)s - %(name)s:%(lineno)d - %(levelname)s - %(message)s",
        datefmt="%Y-%m-%d %H:%M:%S",
    )
    logger = logging.getLogger(module_name)
    logger.setLevel(logging.INFO)

    return logger


from django.urls import path
from .views import login_view, ConversationListCreateAPIView, ConversationRetrieveUpdateDestroyAPIView, ChatMessageListAPIView, ChatMessageDestroyAPIView
from .views.document_view import DocumentRetrieveUpdateDestroyAPIView, DocumentListCreateAPIView

urlpatterns = [
    path('auth/login', login_view, name='login'),
    path('conversations', ConversationListCreateAPIView.as_view(), name='conversation_list_create'),
    path('conversations/<int:pk>', ConversationRetrieveUpdateDestroyAPIView.as_view(), name='conversation_retrieve_update_destroy'),
    path('conversations/<int:conversation_id>/messages', ChatMessageListAPIView.as_view(), name='chat_message_list'),
    path('conversations/<int:conversation_id>/messages/<int:pk>', ChatMessageDestroyAPIView.as_view(), name='chat_message_delete'),
    path('documents', DocumentListCreateAPIView.as_view(), name='document-list-create'),
    path('documents/<int:pk>', DocumentRetrieveUpdateDestroyAPIView.as_view(), name='document-retrieve-update-destroy'),
]


import time
from libcontractvm import Wallet, ConsensusManager, DappManager
from datetime import date

class ForumManager (DappManager.DappManager):
	def __init__ (self, consensusManager, wallet = None):
		super (ForumManager, self).__init__(consensusManager, wallet)

	# This method is used to create a new post
	def createPost (self, title, body):
		print(' CREATE POST: ', title, body)
		# Retrieve the address of the user
		who = self.wallet.getAddress()
		# Start the transaction
		cid = self.produceTransaction('forum.createPost', [title, body, who])
		return cid

	# This method is used to create a comment for a post
	def commentPost (self, id_post, comment):
		print(' CREATE COMMENT: ', id_post, comment)
		who = self.wallet.getAddress()
		cid = self.produceTransaction('forum.commentPost', [id_post, comment, who])
		return cid

	# this methhod shows the list of posts in the database without comments
	def listPost (self):
		posts = self.consensusManager.jsonConsensusCall('forum.listPost',[])['result']

		count = 0
		lista = "List of the posts...\n"
		for id_post in posts:
			post = self.consensusManager.jsonConsensusCall('forum.getPost',[id_post])['result']
			lista = lista + "Post " + str(count) +"\n"
			count = count + 1
			lista = lista + "Owner:  " + post['owner'] + "\n"
			lista = lista + "Title:  " + post['title'] + "\n"
			lista = lista + "Body:  " + post['body'] + "\n\n"
		print(lista)

	# This method shows a single post with comment
	def getPostInfo(self, id_post):
		posts = self.consensusManager.jsonConsensusCall('forum.listPost',[])['result']
		if id_post in posts:
			post = self.consensusManager.jsonConsensusCall('forum.getPost',[id_post])['result']
			info = 'POST(' + id_post + ')\n' + 'Title: '+ post['title'] + '\nBody: '+ post['body'] + '\nComments:\n'
			countComment = 1
			for y in post['comments']:
				info = info + str(countComment) + " Owner: " + y['owner'] + " Text: " + y['commentValue']+"\n"
				countComment = countComment + 1

		else:
			info = 'Invalid argument: The post (' + id_post + ') not exists'

		print(info)

	# This method can be used to modify the title and the body of a post
	def editPost(self,id_post, new_title, new_body):
		print(' EDIT POST post: ', id_post, new_title, new_body)
		who = self.wallet.getAddress()
		return self.produceTransaction('forum.editPost', [id_post, new_title, new_body, who])

	# This method is used to delete a post that is stored
	def deletePost(self,id_post):
		print(' DELETE POST id_post: ', id_post)
		who = self.wallet.getAddress()
		return self.produceTransaction('forum.deletePost', [id_post, who])

	# This method is used to edit a comment
	def editComment(self,id_comment, new_comment):
		print(' EDIT COMMENT comment: ', id_comment, new_comment)
		who = self.wallet.getAddress()
		return self.produceTransaction('forum.editComment', [id_comment, new_comment, who])

	# This method is used to delete a comment
	def deleteComment(self,id_comment):
		print(' DELETE COMMENT id: ', id_comment)
		who = self.wallet.getAddress()
		return self.produceTransaction('forum.deleteComment', [id_comment, who])

	# This method is used to create a poll
	def createPoll(self,title, listAnswers, deadline):
		print(' CREATE POLL title: ', title)
		who = self.wallet.getAddress()
		return self.produceTransaction('forum.createPoll', [title, listAnswers, deadline, who])

	# This method shows the list of the polls stored in database without the score
	def listPolls(self):
		polls = self.consensusManager.jsonConsensusCall('forum.getPolls',[])['result']
		info = ''
		count = 0
		for id_poll in polls:
			poll = self.consensusManager.jsonConsensusCall('forum.getPoll',[id_poll])['result']
			info = info + "Poll " + str(count) + ": \nID: "
			count = count + 1
			info = info + id_poll + " \nTitle:  "+ poll['title'] + "\nAnswer \n[ "
			for y in poll['listAnswersScore']:
				info = info + " - " + y['answer']
			info = info + " ]" + " \ndeadline: " +  poll['deadline']

			today = date.today()
			todayStr = str(today)
			if todayStr > poll['deadline']:
				info = info + " - close"
			else:
				info = info + " - open"

			info = info +"\n\n"
		print(info)

	# This method is used to vote a post
	def vote(self, id_poll, choice):
		print(' VOTE POLL poll:', id_poll, choice)
		who = self.wallet.getAddress()
		return self.produceTransaction('forum.votePoll', [id_poll, choice, who])

	# this method is used to retrieve the information of a poll with the score for all choices
	def getPollInfo(self, id_poll):
		info = ''
		polls = self.consensusManager.jsonConsensusCall('forum.getPolls',[])['result']
		if id_poll in polls:
			poll = self.consensusManager.jsonConsensusCall('forum.getPoll',[id_poll])['result']
			info = ''
			info = info + "Poll "+ "\nID: " + id_poll
			info = info + " \nTitle:  "+ poll['title'] + "\nAnswers \n"
			for y in poll['listAnswersScore']:
				info = info + " - " + y['answer'] + " score: " + str(len(y['votes'])) +" [ "
				for vote in y['votes']:
					info = info + vote +"  "
				info = info + "]\n"
			info = info + "]\ndeadline: " +  poll['deadline']

			today = date.today()
			todayStr = str(today)

			if todayStr > poll['deadline']:
				info = info + " - close"
			else:
				info = info + " - open"

		print(info)

	# this method is used to delete a poll
	def deletePoll(self,id_poll):
		print(' DELETE POLL poll: ', id_poll)
		who = self.wallet.getAddress()
		return self.produceTransaction('forum.deletePoll', [id_poll, who])

	# This method is used to retrieve all information in the forum of the user
	def getUserInfo(self):
		print("GET USER INFO\n")
		who = self.wallet.getAddress()
		info = "USER: " + who + "\n"
		info = info + "Posts:\n"
		nPost = 0
		posts = self.consensusManager.jsonConsensusCall('forum.listPost',[])['result']
		for id_post in posts:
			info = info + "POST " + str(nPost) + ": "
			post = self.consensusManager.jsonConsensusCall('forum.getPost',[id_post])['result']
			info = info + id_post + " " + post['title'] + " " + post['body'] + "\n"
			nPost = nPost + 1

		info = info + "COMMENTS:\n"
		for id_post in posts:

			comment = ''
			if post['owner'] == who:
				info = info + "id post: " + id_post + "\n [ "
				for y in post['comments']:
					comment = comment + y['commentValue'] + " "

				info = info + comment + " ]\n"

		info = info + "\n\nPolls:\n"
		polls = self.consensusManager.jsonConsensusCall('forum.getPolls',[])['result']
		for id_poll in polls:
			poll = self.consensusManager.jsonConsensusCall('forum.getPoll',[id_poll])['result']
			if poll['owner'] == who:
				info = info + id_poll+ poll['title']+ "\n"

		print(info)


#!/usr/bin/env python
# Based on astropy affiliated package template's setup.py
# Licensed under a 3-clause BSD style license - see LICENSE.md
from __future__ import print_function

import glob
import os
import sys
import imp
import ast

__minimum_python_version__ = "3.5"

# Enforce Python version check - this is the same check as in __init__.py but
# this one has to happen before importing ah_bootstrap.
if sys.version_info < tuple((int(val) for val in __minimum_python_version__.split('.'))):
    sys.stderr.write("ERROR: poppy requires Python {} or later\n".format(__minimum_python_version__))
    sys.exit(1)

try:
    import numpy
except ImportError:
    print("""
WARNING: NumPy was not found! setup.py will attempt to install it if asked, but
\tyou may experience issues. Try installing NumPy first, separately.
\tSee https://github.com/numpy/numpy/issues/2434 for details.
""")

import ah_bootstrap
from setuptools import setup

#A dirty hack to get around some early import/configurations ambiguities
if sys.version_info[0] >= 3:
    import builtins
else:
    import __builtin__ as builtins
builtins._ASTROPY_SETUP_ = True

from astropy_helpers.setup_helpers import (
    register_commands, adjust_compiler, get_debug_option, get_package_info)
from astropy_helpers.git_helpers import get_git_devstr
from astropy_helpers.version_helpers import generate_version_py

# Get some values from the setup.cfg
try:
    from ConfigParser import ConfigParser
except ImportError:
    from configparser import ConfigParser

conf = ConfigParser()
conf.read(['setup.cfg'])
metadata = dict(conf.items('metadata'))

PACKAGENAME = metadata.get('package_name', 'packagename')
DESCRIPTION = metadata.get('description', 'unknown')
AUTHOR = metadata.get('author', '')
AUTHOR_EMAIL = metadata.get('author_email', '')
LICENSE = metadata.get('license', 'unknown')
URL = metadata.get('url', 'http://astropy.org')

# Get the long description from the package's docstring
_, module_path, _ = imp.find_module(PACKAGENAME)
with open(os.path.join(module_path, '__init__.py')) as f:
    module_ast = ast.parse(f.read())
LONG_DESCRIPTION = ast.get_docstring(module_ast)

# Store the package name in a built-in variable so it's easy
# to get from other parts of the setup infrastructure
builtins._ASTROPY_PACKAGE_NAME_ = PACKAGENAME

# VERSION should be PEP386 compatible (http://www.python.org/dev/peps/pep-0386)
VERSION = '0.9.0'

# Indicates if this version is a release version
RELEASE = 'dev' not in VERSION

if not RELEASE:
    VERSION += get_git_devstr(False)

# Populate the dict of setup command overrides; this should be done before
# invoking any other functionality from distutils since it can potentially
# modify distutils' behavior.
cmdclassd = register_commands(PACKAGENAME, VERSION, RELEASE)

# Adjust the compiler in case the default on this platform is to use a
# broken one.
adjust_compiler(PACKAGENAME)

# Freeze build information in version.py
generate_version_py(PACKAGENAME, VERSION, RELEASE,
                    get_debug_option(PACKAGENAME))

# Treat everything in scripts except README.rst as a script to be installed
scripts = [fname for fname in glob.glob(os.path.join('scripts', '*'))
           if os.path.basename(fname) != 'README.rst']

# Get configuration information from all of the various subpackages.
# See the docstring for setup_helpers.update_package_files for more
# details.
package_info = get_package_info()

# Add the project-global data
package_info['package_data'].setdefault(PACKAGENAME, [])
package_info['package_data'][PACKAGENAME].append('data/*')

# Include all .c files, recursively, including those generated by
# Cython, since we can not do this in MANIFEST.in with a "dynamic"
# directory name.
c_files = []
for root, dirs, files in os.walk(PACKAGENAME):
    for filename in files:
        if filename.endswith('.c'):
            c_files.append(
                os.path.join(
                    os.path.relpath(root, PACKAGENAME), filename))
package_info['package_data'][PACKAGENAME].extend(c_files)

install_requires_packages = [
      'numpy>=1.13.0',
      'scipy>=1.0.0',
      'matplotlib>=2.0.0',
      'astropy>=3.0.0',
]

setup(name=PACKAGENAME,
      version=VERSION,
      description=DESCRIPTION,
      scripts=scripts,
      python_requires='>=' + __minimum_python_version__,
      install_requires=install_requires_packages,
      provides=[PACKAGENAME],
      author=AUTHOR,
      author_email=AUTHOR_EMAIL,
      license=LICENSE,
      url=URL,
      long_description=LONG_DESCRIPTION,
      cmdclass=cmdclassd,
      zip_safe=False,
      **package_info
)


import collections

class Solution:

    def solve_shortest_point2point_path(self, graph, point2point_shortest_path, istart, iend):
        if istart == iend:
            return 0
        if len(graph[istart]) == 0:
            return 12
        if iend in graph[istart]:
            return 1

        shortest_path_len = 12
        for interal in graph[istart]:
            interal_shortest_path_len = 1 + self.solve_shortest_point2point_path(graph, point2point_shortest_path, interal, iend)
            if shortest_path_len > interal_shortest_path_len:
                shortest_path_len = interal_shortest_path_len
        return shortest_path_len

    def shortestPathLength1(self, graph):
        """
        :type graph: List[List[int]]
        :rtype: int
        """

        # first get point to point, the shortest path length
        point2point_shortest_path = [[12 for x in range(0, len(graph))] for y in range(0, len(graph))]
        for istart in range(0, len(graph)):
            point2point_shortest_path[istart][istart] = 0
            for iend in graph[istart]:
                point2point_shortest_path[istart][iend] = 1

        for istart in range(0, len(graph)):
            for iend in range(0, len(graph)):
                point2point_shortest_path[istart][iend] = \
                    self.solve_shortest_point2point_path(graph, point2point_shortest_path, istart, iend)

        print(point2point_shortest_path)

    def shortestPathLength1(self, graph):
        N = len(graph)
        queue = collections.deque((1 << x, x) for x in xrange(N))
        dist = collections.defaultdict(lambda: N * N)
        for x in xrange(N): dist[1 << x, x] = 0

        while queue:
            cover, head = queue.popleft()
            d = dist[cover, head]
            if cover == 2 ** N - 1: return d
            for child in graph[head]:
                cover2 = cover | (1 << child)
                if d + 1 < dist[cover2, child]:
                    dist[cover2, child] = d + 1
                    queue.append((cover2, child))

    def shortestPathLength(self, graph):
        N = len(graph)
        dist = [[float('inf')] * N for i in xrange(1 << N)]
        for x in xrange(N):
            dist[1 << x][x] = 0

        for cover in xrange(1 << N):
            repeat = True
            while repeat:
                repeat = False
                for head, d in enumerate(dist[cover]):
                    for nei in graph[head]:
                        cover2 = cover | (1 << nei)
                        if d + 1 < dist[cover2][nei]:
                            dist[cover2][nei] = d + 1
                            if cover == cover2:
                                repeat = True

        return min(dist[2 ** N - 1])

if __name__ == '__main__':
    sol = Solution()
    sol.shortestPathLength([[1],[0,2,4],[1,3,4],[2],[1,2]])


import numpy as np
from typing_extensions import ParamSpecArgs
from typing import Union

from prio_replay.maze import LinearTrack, OpenField
from prio_replay.parameters import Parameters
from mazemdp.toolbox import sample_categorical
from prio_replay.q_learning import get_action

"""==============================================================================================================="""

def create_plan_exp( m : Union[LinearTrack,OpenField], params : Parameters ) :
    """ Creates a matrix of tuple (state, action, reward, next_state) based on the previous experiences

        Arguments
        ----------
            m -- Union[LinearTrack,OpenField] from maze.py : class with the maze and the agent
            params -- Parameters from parameters.py : class with the settings of the current simulation

        Returns
        ----------
            planExp -- matrix ((state X action) X 4) : memory of last reward and next state obtained of each tuple (state, action)
    """

    """
    create a matrix with shape :
     [
       [ [ s[0] a[0] last_r[0,0] last_stp1[0,0] ] ]
       [ [ s[1] a[0] last_r[1,0] last_stp1[1,0] ] ]
       [ [ s[2] a[0] last_r[2,0] last_stp1[2,0] ] ]
                     ....
       [ [ s[0] a[1] last_r[0,1] last_stp1[0,1] ] ]
       [ [ s[1] a[1] last_r[1,1] last_stp1[1,1] ] ]
       [ [ s[2] a[1] last_r[2,1] last_stp1[2,1] ] ]
                  ..........
     ]
    """
    planExp = np.empty( (0,4) )

    for a in range(m.action_space.n) :
        for s in range(m.nb_states) :
            exp = np.array( [ s, a, m.exp_LastR[s,a], m.exp_LastStp1[s,a] ] )
            planExp = np.append( planExp, [exp], axis=0 )

    if params.remove_samestate : # remove all experiences that goes to the same states (i.e walls)
        planExp = planExp[planExp[:, 0] != planExp[:, 3]]

    planExp = planExp[ np.invert( np.isnan(planExp).any(axis=1) ) ] # remove all experiences with NaNs in it , we dont need this theoretically

    return planExp

"""==============================================================================================================="""

def expand_plan_exp(planning_backups, planExp, m : Union[OpenField,LinearTrack], params) :

    seqStart = np.argwhere(planning_backups[:, 4] == 1)[-1][0]
    seqSoFar = planning_backups[seqStart:, 0:4]

    sn = int( seqSoFar[-1, 3] )  # Final state reached in the last planning st...

    if params.onlineVSoffline == "online" : # agent is awake
        an = get_action(sn,m,params)

    else : # agent is asleep
        probs = np.zeros( np.size(m.Q[sn]) )
        probs[ m.Q[sn] == max( m.Q[sn] ) ] =  1 / ( sum(m.Q[sn]) == max(m.Q[sn]) )
        an = sample_categorical(probs)

    snp1 = m.exp_LastStp1[sn,an]
    rn = m.exp_LastR[sn,an]

    step_isNaN = np.isnan( m.exp_LastStp1[sn,an] ) or np.isnan( m.exp_LastR[sn,an] )
    step_isRepeated = np.isin( snp1 , [ seqSoFar[:, 0], seqSoFar[:, 3] ] )

    if (not step_isNaN) and (params.allowLoops or (not step_isRepeated)) :
        expanded_exp = np.array( [sn, an, rn, snp1] )
        seqUpdated = np.append( seqSoFar, [expanded_exp], axis=0 )
        planExp.append(seqUpdated)

"""==============================================================================================================="""

def update_planning_backups(planning_backups, plan_exp_arr_max) :

    if planning_backups.shape[0] > 0:
        planning_backups = np.vstack([planning_backups, np.append(plan_exp_arr_max, plan_exp_arr_max.shape[0])])
    elif planning_backups.shape[0] == 0:
        planning_backups = np.append(plan_exp_arr_max,plan_exp_arr_max.shape[0]).reshape(1, planning_backups.shape[1])
    else:
        err_msg = 'planning_backups does not have the correct shape. It is {} but should have a length equal to 1 or 2, e.g. (5,) or (2, 5)'.format(planning_backups.shape)
        raise ValueError(err_msg)

    return planning_backups

"""==============================================================================================================="""


import json
import logging
import urllib.request
import urllib.parse
import urllib.error

import requests

from cauth.service import base

logger = logging.getLogger(__name__)

class RepoxplorerServicePlugin(base.BaseServicePlugin):
    """This plugin deals with repoXplorer user backend."""

    _config_section = "repoxplorer"
    log = logging.getLogger("cauth.RepoxplorerServicePlugin")

    def register_new_user(self, user):
        transactionID = user.get('transactionID', '')
        _user = {"uid": user['login'],
                 "name": user['name'],
                 "default-email": str(user['email']),
                 "emails": [],
                 }
        emails = [
            {"email": email} for email in user.get("emails", [])]

        # If IDP driver does not add any email to emails
        # add the default email of the user
        if not emails:
            emails = [{"email": str(user['email'])}]

        _user["emails"] = emails

        headers = {
            "Remote-User": "admin",
            "Content-type": "application/json",
            "Admin-Token": self.conf["admin_token"]}
        url = "%s/api/v1/users/%s" % (
            self.conf["url"], urllib.parse.quote_plus(user["login"]))

        # Check user already exists in the DB
        try:
            resp = requests.get(url, headers=headers, timeout=5)
        except Exception as exc:
            self.twarning(
                "Skip user %s registration, repoxplorer backend down (%s)",
                transactionID, urllib.parse.quote_plus(user["login"]), exc)
            return

        if resp.status_code == 404:
            mode = 'creation'
            req = requests.put
        elif resp.status_code == 200:
            mode = 'update'
            req = requests.post
            puser = resp.json()
            # Keep user defined name
            _user["name"] = puser["name"]
            # Detect email to add or to remove
            prev_emails = set([e['email'] for e in puser['emails']])
            new_emails = set([e['email'] for e in _user['emails']])
            to_add = new_emails - prev_emails
            to_del = prev_emails - new_emails
            # Keep previous emails data, by amending with data from idp
            _user["emails"] = []
            for e in puser["emails"]:
                if e['email'] in to_del:
                    continue
                _user["emails"].append(e)
            for e in to_add:
                _user["emails"].append({'email': e})
        else:
            self.twarning(
                "Skip user %s registration, unexpected status code (%s)",
                transactionID, urllib.parse.quote_plus(user["login"]),
                resp.status_code)
            return

        data = json.dumps(_user, default=lambda o: o.__dict__)
        logger.debug('Add user %s to repoxplorer:'
                     ' %s with payload: %s' % (mode, url, data))
        try:
            resp = req(url, data=data, headers=headers, timeout=5)
        except Exception as exc:
            self.twarning(
                "Skip user %s registration, repoxplorer backend down (%s)",
                transactionID, urllib.parse.quote_plus(user["login"]), exc)
            return

        self.tdebug('repoxplorer responded with code: %s',
                    transactionID, resp.status_code)

    def set_api_key(self, user, key):
        pass

    def delete_api_key(self, user):
        pass


import os
from logging import info
from pathlib import Path
from typing import Tuple
from zipfile import ZipFile

import pandas as pd
import requests
import torch
from torch.utils.data import Dataset

from nn_repair.utils.checksums import sha256sum

class CMAPSS(Dataset):
    """
    The `Turbofan Engine Degradation Dataset (C-MAPSS)
    <https://www.nasa.gov/content/prognostics-center-of-excellence-data-set-repository>`_
    [SaxenaGoebel2008]_.

    Specifically, the FD001 dataset from the C-MAPSS dataset.
    The time series data is sliced into windows of a certain length to allow
    processing with a CNN.
    Each window corresponds to one data point in the resulting training or test set.
    The windows are overlapping with a stride of 1.

    The features of this data set are 2D tensors with shape `20, window_size`.
    The 20 channels are the setting and sensor features of the dataset with
    setting_3, sensor_1, sensor_18, and sensor_19 removed as they're constant
    in the dataset.
    The window size is set in the initializer.

    The targets are the remaining useful file (RUL) clipped to a value of 150, following
    [MathworksRULPred]_.

    .. [SaxenaGoebel2008] A. Saxena and K. Goebel (2008).
       "Turbofan Engine Degradation Simulation Data Set", NASA Prognostics
       Data Repository, NASA Ames Research Center, Moffett Field, CA

    .. [MathworksRULPred] https://de.mathworks.com/help/releases/R2021a/predmaint/ug/remaining-useful-life-estimation-using-convolutional-neural-network.html#mw_rtc_RULEstimationUsingCNNExample_98C35430

    """
    zip_url = "https://phm-datasets.s3.amazonaws.com/NASA/6.+Turbofan+Engine+Degradation+Simulation+Data+Set.zip"
    zip_checksum = "c9c5dec12a945a82e8bb4446589d7fb3cc057b5e5d81fa1a12e25ee9912ad3b2"
    subset = "FD001"
    train_inputs_file = "train_inputs.pyt"
    train_targets_file = "train_targets.pyt"
    test_inputs_file = "test_inputs.pyt"
    test_targets_file = "test_targets.pyt"

    def __init__(self, root: str, train: bool = True, window_size: int = 40, download: bool = False):
        """
        Loads the `Turbofan Engine Degradation Dataset (C-MAPSS)
        <https://www.nasa.gov/content/prognostics-center-of-excellence-data-set-repository>`_
        [SaxenaGoebel2008]_.

        :param root: The root directory where the C-MAPSS folder is placed or
          is to be downloaded to if download is set to True.
        :param train: Whether to get the training set or test set of
          the dataset.
        :param window_size: The size of the window into which the time series data
         is split.
         This is the last dimension
        :param download: Whether to download the C-MAPSS dataset from
          https://www.nasa.gov/content/prognostics-center-of-excellence-data-set-repository
          if it isn't present in the root directory.
        """
        self.files_dir = Path(root, f"C-MAPSS_window_{window_size}")
        self.window_size = window_size
        if not self.files_dir.exists():
            if not download:
                raise RuntimeError("Dataset not found. "
                                   "Download it by passing download=True.")
            os.mkdir(self.files_dir)
            self._download()

        if train:
            inputs_file = self.train_inputs_file
            targets_file = self.train_targets_file
        else:
            inputs_file = self.test_inputs_file
            targets_file = self.test_targets_file
        self.data = torch.load(Path(self.files_dir, inputs_file))
        self.targets = torch.load(Path(self.files_dir, targets_file))

    def _download(self):
        info("Downloading C-MAPSS dataset file...")
        # the dataset is in a zip file in a zip file
        zip1_path = Path(self.files_dir, "Turbofan.zip")
        temp_extracted_path = Path(
            self.files_dir, "6. Turbofan Engine Degradation Simulation Data Set"
        )
        zip2_path = Path(temp_extracted_path, "CMAPSSData.zip")

        result = requests.get(self.zip_url)
        with open(zip1_path, "xb") as file:
            file.write(result.content)

        zip1_checksum = sha256sum(zip1_path)
        if zip1_checksum != zip1_checksum:
            raise RuntimeError(
                f"Downloaded dataset file has different checksum than "
                f"expected: {zip1_path}. "
                f"Expected sha256 checksum: {zip1_checksum}"
            )

        info("Extracting data...")
        with ZipFile(zip1_path) as zip_obj:
            zip_obj.extractall(self.files_dir)

        with ZipFile(zip2_path) as zip_obj:
            with zip_obj.open(f"train_{self.subset}.txt") as file:
                train_raw = pd.read_csv(file, sep=" ", header=None)
            with zip_obj.open(f"test_{self.subset}.txt") as file:
                test_raw = pd.read_csv(file, sep=" ", header=None)
            with zip_obj.open(f"RUL_{self.subset}.txt") as file:
                test_targets_raw = pd.read_csv(file, sep=" ", header=None)

        zip1_path.unlink()
        zip2_path.unlink()
        temp_extracted_path.rmdir()

        info("Preprocessing data...")
        # the train and test files contain two training spaces that lead
        # to two extra columns
        train_raw.drop([26, 27], axis=1, inplace=True)
        test_raw.drop([26, 27], axis=1, inplace=True)
        # the RUL file has only one training space
        test_targets_raw.drop([1], axis=1, inplace=True)

        # split the data by ids => each result is a time series
        train_time_series = train_raw.groupby(0)  # column 0 is the ID
        test_time_series = test_raw.groupby(0)

        # compute the number of cycles until failure for the training set
        # column 1 is the cycle number
        train_failure_cycle = {id_: df[1].max() for id_, df in train_time_series}

        # 0, 1 are id and cycle number
        # the remaining ones are constant in the dataset (zero variance)
        cols_to_drop = [0, 1, 4, 5, 9, 14, 20, 22, 23]

        # make windows of the training data
        train_inputs = []
        train_targets = []
        for id_, df in train_time_series:
            for i in range(len(df) - self.window_size + 1):
                window = df.iloc[i:i+self.window_size]
                # predict the remaining useful life at the end of the
                # time series
                target = train_failure_cycle[id_] - window[1].max()
                # drop id, cycle number and constant features
                window = window.drop(cols_to_drop, axis=1)
                train_inputs.append(torch.as_tensor(window.to_numpy(), dtype=torch.float))
                train_targets.append(target)
        train_inputs = torch.stack(train_inputs)
        train_targets = torch.tensor(train_targets, dtype=torch.int)

        test_inputs = []
        test_targets = []
        for id_, df in test_time_series:
            final_target = test_targets_raw.iloc[id_ - 1]  # ids start from 1
            if len(df) < self.window_size:
                data = df.drop(cols_to_drop, axis=1).to_numpy()
                window = torch.full((self.window_size, data.shape[1]), fill_value=0.0)
                window[:len(df), :] = torch.as_tensor(data)
                test_inputs.append(window)
                test_targets.append(final_target)
            else:
                final_target += df[1].max()  # RUL at the start of the time series
                for i in range(len(df) - self.window_size + 1):
                    window = df.iloc[i:i+self.window_size]
                    # predict the remaining useful life at the end of the
                    # time series
                    target = final_target - window[1].max()
                    window = window.drop(cols_to_drop, axis=1)
                    test_inputs.append(torch.as_tensor(window.to_numpy(), dtype=torch.float))
                    test_targets.append(target)
        test_inputs = torch.stack(test_inputs)
        test_targets = torch.tensor(test_targets, dtype=torch.int)

        # make channel x window_size tensors
        train_inputs.transpose_(-2, -1)
        test_inputs.transpose_(-2, -1)

        # clip RULs to 150
        train_targets.clip_(max=150)
        test_targets.clip_(max=150)

        torch.save(train_inputs, Path(self.files_dir, self.train_inputs_file))
        torch.save(train_targets, Path(self.files_dir, self.train_targets_file))
        torch.save(test_inputs, Path(self.files_dir, self.test_inputs_file))
        torch.save(test_targets, Path(self.files_dir, self.test_targets_file))
        info("Download finished.")

    def __getitem__(self, index: int) -> Tuple[torch.Tensor, torch.Tensor]:
        return self.data[index], self.targets[index]

    def __len__(self) -> int:
        return len(self.targets)


# @author Metro
# @time 2021/11/24
import os.path

import gym
from agents.pdqn import P_DQN
from utilities.memory import ReplayBuffer
from utilities.utilities import *
from utilities.route_generator import generate_routefile

class Train_and_Evaluate(object):

    def __init__(self, config):
        # Environment
        generate_routefile(seed=config.seed, demand=config.demand)
        self.env = gym.make(config.environment)

        # Agent
        self.agent = P_DQN(config, self.env)

        # Memory
        self.replay_memory_size = config.hyperparameters['replay_memory_size']
        self.batch_size = config.hyperparameters['batch_size']
        self.updates_per_step = config.hyperparameters['updates_per_step']
        self.memory = ReplayBuffer(self.replay_memory_size)

        self.total_steps = 0
        self.total_updates = 0

        self.save_freq = config.save_freq
        self.file_to_save = config.file_to_save
        self.maximum_episodes = config.hyperparameters['maximum_episodes']

        self.train = config.train
        self.evaluate = config.evaluate
        self.evaluate_internal = config.evaluate_internal

        self.agent_to_color_dictionary = config.agent_to_color_dictionary
        self.standard_deviation_results = config.standard_deviation_results

        self.colors = ['red', 'blue', 'green', 'orange', 'yellow', 'purple']
        self.color_idx = 0

        self.rolling_score_window = config.rolling_score_window
        self.runs_per_agent = config.runs_per_agent
        self.agent_name = config.agent_name
        self.ceil = config.ceil

        # Training Loop

    def train_agent(self):
        """

        :return:
        """

        rolling_scores_for_diff_runs = []
        file_to_save_actor = os.path.join(self.file_to_save, 'actor/')
        file_to_save_actor_param = os.path.join(self.file_to_save, 'actor_param/')
        file_to_save_runs = os.path.join(self.file_to_save, 'runs_1/')
        file_to_save_rolling_scores = os.path.join(self.file_to_save, 'rolling_scores/')
        os.makedirs(file_to_save_actor, exist_ok=True)
        os.makedirs(file_to_save_actor_param, exist_ok=True)
        os.makedirs(file_to_save_runs, exist_ok=True)
        os.makedirs(file_to_save_rolling_scores, exist_ok=True)

        for run in range(self.runs_per_agent):
            game_full_episodes_scores = []
            game_full_episodes_rolling_scores = []

            for i_episode in range(self.maximum_episodes):

                if self.save_freq > 0 and i_episode % self.save_freq == 0:
                    actor_path = os.path.join(file_to_save_actor, 'episode{}'.format(i_episode))
                    actor_param_path = os.path.join(file_to_save_actor_param, 'episode{}'.format(i_episode))
                    self.agent.save_models(actor_path, actor_param_path)

                episode_score = []
                episode_steps = 0
                done = 0
                state = self.env.reset()  # n_steps

                while not done:
                    if len(self.memory) > self.batch_size:
                        action, action_params = self.agent.select_action(state, self.train)
                        if self.ceil:
                            action_params = np.ceil(action_params).squeeze(0)

                        action_for_env = [action, int(action_params[action])]

                        for i in range(self.updates_per_step):
                            self.agent.update(self.memory)
                            self.total_updates += 1
                    else:
                        action_params = np.random.randint(low=10, high=31, size=8)
                        action = np.random.randint(7, size=1)[0]
                        action_for_env = [action, action_params[action]]

                    next_state, reward, done, info = self.env.step(action_for_env)
                    print(reward)

                    episode_steps += 1
                    episode_score.append(info)

                    self.total_steps += 1
                    self.memory.push(state, action, action_params, reward, next_state, done)

                    state = next_state

                episode_score_so_far = np.mean(episode_score)
                game_full_episodes_scores.append(episode_score_so_far)
                game_full_episodes_rolling_scores.append(
                    np.mean(game_full_episodes_scores[-1 * self.rolling_score_window:]))

                print("Episode: {}, total steps:{}, episode steps:{}, scores:{}".format(
                    i_episode, self.total_steps, episode_steps, episode_score_so_far))

                self.env.close()
                file_path_for_pic = os.path.join(file_to_save_runs, 'episode{}_run{}.jpg'.format(i_episode, run))
                visualize_results_per_run(agent_results=game_full_episodes_scores,
                                          agent_name=self.agent_name,
                                          save_freq=1,
                                          file_path_for_pic=file_path_for_pic)
                rolling_scores_for_diff_runs.append(game_full_episodes_rolling_scores)

        file_path_for_pic = os.path.join(file_to_save_rolling_scores, 'rolling_scores.jpg')
        visualize_overall_agent_results(agent_results=rolling_scores_for_diff_runs,
                                        agent_name=self.agent_name,
                                        show_mean_and_std_range=True,
                                        agent_to_color_dictionary=self.agent_to_color_dictionary,
                                        standard_deviation_results=1,
                                        file_path_for_pic=file_path_for_pic
                                        )


import datetime

from django import forms
from django.core.exceptions import ValidationError

from .models import *

class TextName(forms.Form):
    your_name = forms.CharField(
        label="Твое имя",
        help_text="Attention!",
        initial=datetime.date.today,
        max_length=100,
        widget=forms.Textarea,
        required=False,
        error_messages={"required": "Egghead, what did you do?"}
    )

class ReviewForm(forms.ModelForm):
    class Meta:
        model = Reviews
        fields = ["Text", "Name", "Email"]

# class Comment(forms.ModelForm):
#     # slug = CharField(validators=[validate_Age])
#     class Meta:
#         model = DirectorsActors
#         fields = ["Name", "Age", "Description", "Image"]
        # widgets = {
        #     "Name": forms.Textarea(attrs={"cols": 10, "rows": 10}),
        # }
        # labels = {
        #     "Name":_("Имя быстро"),
        #     "Age":_("Возраст какой а?"),
        # }
        #
        # help_texts={
        #     "Name": _("Имя нужно в латинице")
        # }

    #
    # def clean_name(self):
    #     name = self.cleaned_data["Name"]
    #     if len(name)>6:
    #         raise ValidationError("Ты шо правила не читал?")
    #     return name


import json
import click

'''
The purpose of this script is to perform a patch to cchdo with an infile that
is a nav_file in .txt format containing LATITUDE and LONGITUDE information
to update the coordinate information for a cruise with no track
'''
@click.command()
@click.argument('infile', type = click.File('r'), nargs = 1)
@click.argument('outfile', type = click.File('w'), nargs = 1)
def nav(infile, outfile):
    points = []
    for line in infile:
        point = [float(p) for p in line.split()[:2]]
        if point not in points:
            points.append(point)

    linestring = {
    "type": "LineString",
    "coordinates": points
    }

    patch = [
        {"path":"/geometry/track", "op":"replace", "value":linestring}
        ]

    json.dump(patch, outfile)

if __name__ == '__main__':
    nav()


import logging
from datetime import datetime, timedelta
from enum import Enum
from typing import Generic, TypeVar, Optional

from vocode.streaming.models.model import TypedModel

class VoiceActivityDetectorType(str, Enum):
    BASE = "base_voice_activity_detector"
    WEB_RTC = "web_rtc_voice_activity_detector"
    SILERO = "silero_voice_activity_detector"
    LIB_ROSA = "librosa_voice_activity_detector"

class BaseVoiceActivityDetectorConfig(TypedModel, type=VoiceActivityDetectorType.BASE.value):
    frame_rate: int = 16000
    min_activity_duration: timedelta = timedelta(milliseconds=400)
    min_mute_duration: timedelta = timedelta(seconds=3)
    speach_ratio: float = .8

VoiceActivityDetectorConfigType = TypeVar("VoiceActivityDetectorConfigType", bound=BaseVoiceActivityDetectorConfig)

class BaseVoiceActivityDetector(Generic[VoiceActivityDetectorConfigType]):
    def __init__(self, config: VoiceActivityDetectorConfigType, logger: Optional[logging.Logger] = None):
        self.logger: logging.Logger = logger or logging.getLogger(__name__)
        self.config = config
        self.speach_start_timestamp = None
        self.activity_state = {True: 0, False: 0}
        self.is_speaking = False
        self.is_interrupted = False

    def is_voice_active(self, frame: bytes) -> bool:
        raise NotImplementedError

    def get_config(self) -> BaseVoiceActivityDetectorConfig:
        return self.config

    def should_interrupt(self, frame: bytes) -> bool:
        now = datetime.now()
        is_voice_active = self.is_voice_active(frame)

        self.activity_state[is_voice_active] += 1

        if is_voice_active and self.speach_start_timestamp is None:
            self.activity_state = {True: 0, False: 0}
            self.speach_start_timestamp = now

        if self.speach_start_timestamp is None:
            return False

        if now - self.speach_start_timestamp > self.config.min_mute_duration and not is_voice_active:
            self.speach_start_timestamp = None
            self.is_interrupted = False
            return False

        if (now - self.speach_start_timestamp) > self.config.min_activity_duration:
            speach_ratio = self.activity_state[True] / (self.activity_state[True] + self.activity_state[False])
            if speach_ratio > self.config.speach_ratio:
                if self.is_interrupted:
                    return False
                self.is_interrupted = True
                return True
            else:
                self.is_interrupted = False
                self.speach_start_timestamp = None


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from scipy.optimize import leastsq
import matplotlib.pyplot as plt
import numpy as np

import begin

def hp(x, α=0.05):
    n = len(x)
    y = [0] * n
    g = [0] * n
    for i in range(1, n):
        g[i] = α * g[i - 1] + (1 - α) * x[i]
        y[i] = x[i] - g[i]
    return y

def lp(x, α=0.1):
    n = len(x)
    # y = [0] * n
    y = np.array(x)
    for i in range(1, n):
        y[i] = y[i - 1] + α * (x[i] - y[i - 1])
    return list(y)

n = 100
orig = np.linspace(0, 100, num=n)

# x = np.sin(x)
np.random.seed(42)
d = np.random.normal(scale=1.2, size=n)
# d = np.sin(200 * x)
x = orig + d

def iterplot(orig, x, y):
    plt.clf()
    plt.plot(orig, label="orig")
    plt.plot(x, label="noisy")
    plt.plot(y, label="lowpass")
    plt.axes().legend(loc="upper left")

@begin.subcommand
def iterate():
    e = []
    for i in range(100, 1, -1):
        α = i / 100.
        print("α is %0.2f" % α)
        y = lp(x, α)
        error = np.power(y - orig, 2)
        # print(error.shape)
        # print(np.vstack((x, y, orig, error)).T)
        e.append(np.sum(error))
        iterplot(orig, x, y)
        plt.title(r"$\alpha$ = %0.2f" % α)
        # plt.plot(z)
        plt.show(block=False)
        plt.waitforbuttonpress(0.01)

    idx = 100 - np.argmin(e)
    print("min error: %0.2f → %d" % (np.min(e), idx))
    y = lp(x, idx / 100.)
    iterplot(orig, x, y)
    plt.title("best = 0.%d" % idx)
    plt.show()

def fn(α):
    return lp(x, α) - orig

@begin.subcommand
def lstsq():
    out = leastsq(fn, 0.99, full_output=True)
    # print(out)

    idx = out[0][0]
    print("idx: %0.2f" % idx)
    y = lp(x, idx)

    iterplot(orig, x, y)
    plt.title("best = %0.2f" % idx)
    plt.show()

@begin.start
def main():
    pass


import sys
import time
import logging
import json

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from bs4 import BeautifulSoup

from django.conf import settings

fresh = False

class RefreshEventHandler(FileSystemEventHandler):

    def on_any_event(self, event):
        global fresh
        ACCEPTED_EXTENSIONS = getattr(settings, 'FRESH_ACCEPTED_EXTENSIONS', [
            '.py',
            '.html',
            '.js',
            '.css',
        ])

        for extension in ACCEPTED_EXTENSIONS:
            if event.src_path.lower().endswith(extension):
                fresh = True

class FreshMiddleware(object):

    def process_response(self, request, response):
        if not settings.DEBUG:
            return response

        global fresh
        mimetype = response._headers['content-type'][1]
        IGNORED_PAGES = getattr(settings, 'FRESH_IGNORED_PAGES', [
            '/admin/',
            '/admin_keywords_submit/',
        ])
        ignored = False

        for ignored_page in IGNORED_PAGES:
            if request.path.lower().startswith(ignored_page):
                ignored = True

        if not ignored:
            if mimetype == 'application/json':
                items = json.loads(response.content)
                if fresh and items.get('fresh') != None:
                    fresh = False
                    items['fresh'] = True
                    response.content = json.dumps(items)
            elif mimetype == 'text/html; charset=utf-8':
                soup = BeautifulSoup(response.content)
                url = settings.STATIC_URL + 'fresh/js/refresher.js'
                script_fresh = soup.new_tag('script', src=url)
                soup.head.append(script_fresh)
                response.content = soup.prettify()

        return response

    def watcher(self):
        observer = Observer()

        path = settings.SITE_ROOT
        event_handler = RefreshEventHandler()
        observer.schedule(event_handler, path, recursive=True)

        observer.start()

    def __init__(self):
        if settings.DEBUG:
            self.watcher()