"""Utilities for mypy.stubgen, mypy.stubgenc, and mypy.stubdoc modules."""

from __future__ import annotations

import os.path
import re
import sys
from abc import abstractmethod
from collections import defaultdict
from contextlib import contextmanager
from typing import Final, Iterable, Iterator, Mapping
from typing_extensions import overload

from mypy_extensions import mypyc_attr

import mypy.options
from mypy.modulefinder import ModuleNotFoundReason
from mypy.moduleinspect import InspectError, ModuleInspect
from mypy.stubdoc import ArgSig, FunctionSig
from mypy.types import AnyType, NoneType, Type, TypeList, TypeStrVisitor, UnboundType, UnionType

# Modules that may fail when imported, or that may have side effects (fully qualified).
NOT_IMPORTABLE_MODULES = ()

class CantImport(Exception):
    def __init__(self, module: str, message: str) -> None:
        self.module = module
        self.message = message

def walk_packages(
    inspect: ModuleInspect, packages: list[str], verbose: bool = False
) -> Iterator[str]:
    """Iterates through all packages and sub-packages in the given list.

    This uses runtime imports (in another process) to find both Python and C modules.
    For Python packages we simply pass the __path__ attribute to pkgutil.walk_packages() to
    get the content of the package (all subpackages and modules).  However, packages in C
    extensions do not have this attribute, so we have to roll out our own logic: recursively
    find all modules imported in the package that have matching names.
    """
    for package_name in packages:
        if package_name in NOT_IMPORTABLE_MODULES:
            print(f"{package_name}: Skipped (blacklisted)")
            continue
        if verbose:
            print(f"Trying to import {package_name!r} for runtime introspection")
        try:
            prop = inspect.get_package_properties(package_name)
        except InspectError:
            report_missing(package_name)
            continue
        yield prop.name
        if prop.is_c_module:
            # Recursively iterate through the subpackages
            yield from walk_packages(inspect, prop.subpackages, verbose)
        else:
            yield from prop.subpackages

def find_module_path_using_sys_path(module: str, sys_path: list[str]) -> str | None:
    relative_candidates = (
        module.replace(".", "/") + ".py",
        os.path.join(module.replace(".", "/"), "__init__.py"),
    )
    for base in sys_path:
        for relative_path in relative_candidates:
            path = os.path.join(base, relative_path)
            if os.path.isfile(path):
                return path
    return None

def find_module_path_and_all_py3(
    inspect: ModuleInspect, module: str, verbose: bool
) -> tuple[str | None, list[str] | None] | None:
    """Find module and determine __all__ for a Python 3 module.

    Return None if the module is a C or pyc-only module.
    Return (module_path, __all__) if it is a Python module.
    Raise CantImport if import failed.
    """
    if module in NOT_IMPORTABLE_MODULES:
        raise CantImport(module, "")

    # TODO: Support custom interpreters.
    if verbose:
        print(f"Trying to import {module!r} for runtime introspection")
    try:
        mod = inspect.get_package_properties(module)
    except InspectError as e:
        # Fall back to finding the module using sys.path.
        path = find_module_path_using_sys_path(module, sys.path)
        if path is None:
            raise CantImport(module, str(e)) from e
        return path, None
    if mod.is_c_module:
        return None
    return mod.file, mod.all

@contextmanager
def generate_guarded(
    mod: str, target: str, ignore_errors: bool = True, verbose: bool = False
) -> Iterator[None]:
    """Ignore or report errors during stub generation.

    Optionally report success.
    """
    if verbose:
        print(f"Processing {mod}")
    try:
        yield
    except Exception as e:
        if not ignore_errors:
            raise e
        else:
            # --ignore-errors was passed
            print("Stub generation failed for", mod, file=sys.stderr)
    else:
        if verbose:
            print(f"Created {target}")

def report_missing(mod: str, message: str | None = "", traceback: str = "") -> None:
    if message:
        message = " with error: " + message
    print(f"{mod}: Failed to import, skipping{message}")

def fail_missing(mod: str, reason: ModuleNotFoundReason) -> None:
    if reason is ModuleNotFoundReason.NOT_FOUND:
        clarification = "(consider using --search-path)"
    elif reason is ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS:
        clarification = "(module likely exists, but is not PEP 561 compatible)"
    else:
        clarification = f"(unknown reason '{reason}')"
    raise SystemExit(f"Can't find module '{mod}' {clarification}")

@overload
def remove_misplaced_type_comments(source: bytes) -> bytes:
    ...

@overload
def remove_misplaced_type_comments(source: str) -> str:
    ...

def remove_misplaced_type_comments(source: str | bytes) -> str | bytes:
    """Remove comments from source that could be understood as misplaced type comments.

    Normal comments may look like misplaced type comments, and since they cause blocking
    parse errors, we want to avoid them.
    """
    if isinstance(source, bytes):
        # This gives us a 1-1 character code mapping, so it's roundtrippable.
        text = source.decode("latin1")
    else:
        text = source

    # Remove something that looks like a variable type comment but that's by itself
    # on a line, as it will often generate a parse error (unless it's # type: ignore).
    text = re.sub(r'^[ \t]*# +type: +["\'a-zA-Z_].*$', "", text, flags=re.MULTILINE)

    # Remove something that looks like a function type comment after docstring,
    # which will result in a parse error.
    text = re.sub(r'""" *\n[ \t\n]*# +type: +\(.*$', '"""\n', text, flags=re.MULTILINE)
    text = re.sub(r"''' *\n[ \t\n]*# +type: +\(.*$", "'''\n", text, flags=re.MULTILINE)

    # Remove something that looks like a badly formed function type comment.
    text = re.sub(r"^[ \t]*# +type: +\([^()]+(\)[ \t]*)?$", "", text, flags=re.MULTILINE)

    if isinstance(source, bytes):
        return text.encode("latin1")
    else:
        return text

def common_dir_prefix(paths: list[str]) -> str:
    if not paths:
        return "."
    cur = os.path.dirname(os.path.normpath(paths[0]))
    for path in paths[1:]:
        while True:
            path = os.path.dirname(os.path.normpath(path))
            if (cur + os.sep).startswith(path + os.sep):
                cur = path
                break
    return cur or "."

class AnnotationPrinter(TypeStrVisitor):
    """Visitor used to print existing annotations in a file.

    The main difference from TypeStrVisitor is a better treatment of
    unbound types.

    Notes:
    * This visitor doesn't add imports necessary for annotations, this is done separately
      by ImportTracker.
    * It can print all kinds of types, but the generated strings may not be valid (notably
      callable types) since it prints the same string that reveal_type() does.
    * For Instance types it prints the fully qualified names.
    """

    # TODO: Generate valid string representation for callable types.
    # TODO: Use short names for Instances.
    def __init__(
        self,
        stubgen: BaseStubGenerator,
        known_modules: list[str] | None = None,
        local_modules: list[str] | None = None,
    ) -> None:
        super().__init__(options=mypy.options.Options())
        self.stubgen = stubgen
        self.known_modules = known_modules
        self.local_modules = local_modules or ["builtins"]

    def visit_any(self, t: AnyType) -> str:
        s = super().visit_any(t)
        self.stubgen.import_tracker.require_name(s)
        return s

    def visit_unbound_type(self, t: UnboundType) -> str:
        s = t.name
        if self.known_modules is not None and "." in s:
            # see if this object is from any of the modules that we're currently processing.
            # reverse sort so that subpackages come before parents: e.g. "foo.bar" before "foo".
            for module_name in self.local_modules + sorted(self.known_modules, reverse=True):
                if s.startswith(module_name + "."):
                    if module_name in self.local_modules:
                        s = s[len(module_name) + 1 :]
                    arg_module = module_name
                    break
            else:
                arg_module = s[: s.rindex(".")]
            if arg_module not in self.local_modules:
                self.stubgen.import_tracker.add_import(arg_module, require=True)
        elif s == "NoneType":
            # when called without analysis all types are unbound, so this won't hit
            # visit_none_type().
            s = "None"
        else:
            self.stubgen.import_tracker.require_name(s)
        if t.args:
            s += f"[{self.args_str(t.args)}]"
        return s

    def visit_none_type(self, t: NoneType) -> str:
        return "None"

    def visit_type_list(self, t: TypeList) -> str:
        return f"[{self.list_str(t.items)}]"

    def visit_union_type(self, t: UnionType) -> str:
        return " | ".join([item.accept(self) for item in t.items])

    def args_str(self, args: Iterable[Type]) -> str:
        """Convert an array of arguments to strings and join the results with commas.

        The main difference from list_str is the preservation of quotes for string
        arguments
        """
        types = ["builtins.bytes", "builtins.str"]
        res = []
        for arg in args:
            arg_str = arg.accept(self)
            if isinstance(arg, UnboundType) and arg.original_str_fallback in types:
                res.append(f"'{arg_str}'")
            else:
                res.append(arg_str)
        return ", ".join(res)

class ClassInfo:
    def __init__(
        self, name: str, self_var: str, docstring: str | None = None, cls: type | None = None
    ) -> None:
        self.name = name
        self.self_var = self_var
        self.docstring = docstring
        self.cls = cls

class FunctionContext:
    def __init__(
        self,
        module_name: str,
        name: str,
        docstring: str | None = None,
        is_abstract: bool = False,
        class_info: ClassInfo | None = None,
    ) -> None:
        self.module_name = module_name
        self.name = name
        self.docstring = docstring
        self.is_abstract = is_abstract
        self.class_info = class_info
        self._fullname: str | None = None

    @property
    def fullname(self) -> str:
        if self._fullname is None:
            if self.class_info:
                self._fullname = f"{self.module_name}.{self.class_info.name}.{self.name}"
            else:
                self._fullname = f"{self.module_name}.{self.name}"
        return self._fullname

def infer_method_ret_type(name: str) -> str | None:
    """Infer return types for known special methods"""
    if name.startswith("__") and name.endswith("__"):
        name = name[2:-2]
        if name in ("float", "bool", "bytes", "int", "complex", "str"):
            return name
        # Note: __eq__ and co may return arbitrary types, but bool is good enough for stubgen.
        elif name in ("eq", "ne", "lt", "le", "gt", "ge", "contains"):
            return "bool"
        elif name in ("len", "length_hint", "index", "hash", "sizeof", "trunc", "floor", "ceil"):
            return "int"
        elif name in ("format", "repr"):
            return "str"
        elif name in ("init", "setitem", "del", "delitem"):
            return "None"
    return None

def infer_method_arg_types(
    name: str, self_var: str = "self", arg_names: list[str] | None = None
) -> list[ArgSig] | None:
    """Infer argument types for known special methods"""
    args: list[ArgSig] | None = None
    if name.startswith("__") and name.endswith("__"):
        if arg_names and len(arg_names) >= 1 and arg_names[0] == "self":
            arg_names = arg_names[1:]

        name = name[2:-2]
        if name == "exit":
            if arg_names is None:
                arg_names = ["type", "value", "traceback"]
            if len(arg_names) == 3:
                arg_types = [
                    "type[BaseException] | None",
                    "BaseException | None",
                    "types.TracebackType | None",
                ]
                args = [
                    ArgSig(name=arg_name, type=arg_type)
                    for arg_name, arg_type in zip(arg_names, arg_types)
                ]
    if args is not None:
        return [ArgSig(name=self_var)] + args
    return None

@mypyc_attr(allow_interpreted_subclasses=True)
class SignatureGenerator:
    """Abstract base class for extracting a list of FunctionSigs for each function."""

    def remove_self_type(
        self, inferred: list[FunctionSig] | None, self_var: str
    ) -> list[FunctionSig] | None:
        """Remove type annotation from self/cls argument"""
        if inferred:
            for signature in inferred:
                if signature.args:
                    if signature.args[0].name == self_var:
                        signature.args[0].type = None
        return inferred

    @abstractmethod
    def get_function_sig(
        self, default_sig: FunctionSig, ctx: FunctionContext
    ) -> list[FunctionSig] | None:
        """Return a list of signatures for the given function.

        If no signature can be found, return None. If all of the registered SignatureGenerators
        for the stub generator return None, then the default_sig will be used.
        """
        pass

    @abstractmethod
    def get_property_type(self, default_type: str | None, ctx: FunctionContext) -> str | None:
        """Return the type of the given property"""
        pass

class ImportTracker:
    """Record necessary imports during stub generation."""

    def __init__(self) -> None:
        # module_for['foo'] has the module name where 'foo' was imported from, or None if
        # 'foo' is a module imported directly;
        # direct_imports['foo'] is the module path used when the name 'foo' was added to the
        # namespace.
        # reverse_alias['foo'] is the name that 'foo' had originally when imported with an
        # alias; examples
        #     'from pkg import mod'      ==> module_for['mod'] == 'pkg'
        #     'from pkg import mod as m' ==> module_for['m'] == 'pkg'
        #                                ==> reverse_alias['m'] == 'mod'
        #     'import pkg.mod as m'      ==> module_for['m'] == None
        #                                ==> reverse_alias['m'] == 'pkg.mod'
        #     'import pkg.mod'           ==> module_for['pkg'] == None
        #                                ==> module_for['pkg.mod'] == None
        #                                ==> direct_imports['pkg'] == 'pkg.mod'
        #                                ==> direct_imports['pkg.mod'] == 'pkg.mod'
        self.module_for: dict[str, str | None] = {}
        self.direct_imports: dict[str, str] = {}
        self.reverse_alias: dict[str, str] = {}

        # required_names is the set of names that are actually used in a type annotation
        self.required_names: set[str] = set()

        # Names that should be reexported if they come from another module
        self.reexports: set[str] = set()

    def add_import_from(
        self, module: str, names: list[tuple[str, str | None]], require: bool = False
    ) -> None:
        for name, alias in names:
            if alias:
                # 'from {module} import {name} as {alias}'
                self.module_for[alias] = module
                self.reverse_alias[alias] = name
            else:
                # 'from {module} import {name}'
                self.module_for[name] = module
                self.reverse_alias.pop(name, None)
            if require:
                self.require_name(alias or name)
            self.direct_imports.pop(alias or name, None)

    def add_import(self, module: str, alias: str | None = None, require: bool = False) -> None:
        if alias:
            # 'import {module} as {alias}'
            assert "." not in alias  # invalid syntax
            self.module_for[alias] = None
            self.reverse_alias[alias] = module
            if require:
                self.required_names.add(alias)
        else:
            # 'import {module}'
            name = module
            if require:
                self.required_names.add(name)
            # add module and its parent packages
            while name:
                self.module_for[name] = None
                self.direct_imports[name] = module
                self.reverse_alias.pop(name, None)
                name = name.rpartition(".")[0]

    def require_name(self, name: str) -> None:
        while name not in self.direct_imports and "." in name:
            name = name.rsplit(".", 1)[0]
        self.required_names.add(name)

    def reexport(self, name: str) -> None:
        """Mark a given non qualified name as needed in __all__.

        This means that in case it comes from a module, it should be
        imported with an alias even if the alias is the same as the name.
        """
        self.require_name(name)
        self.reexports.add(name)

    def import_lines(self) -> list[str]:
        """The list of required import lines (as strings with python code).

        In order for a module be included in this output, an indentifier must be both
        'required' via require_name() and 'imported' via add_import_from()
        or add_import()
        """
        result = []

        # To summarize multiple names imported from a same module, we collect those
        # in the `module_map` dictionary, mapping a module path to the list of names that should
        # be imported from it. the names can also be alias in the form 'original as alias'
        module_map: Mapping[str, list[str]] = defaultdict(list)

        for name in sorted(
            self.required_names,
            key=lambda n: (self.reverse_alias[n], n) if n in self.reverse_alias else (n, ""),
        ):
            # If we haven't seen this name in an import statement, ignore it
            if name not in self.module_for:
                continue

            m = self.module_for[name]
            if m is not None:
                # This name was found in a from ... import ...
                # Collect the name in the module_map
                if name in self.reverse_alias:
                    name = f"{self.reverse_alias[name]} as {name}"
                elif name in self.reexports:
                    name = f"{name} as {name}"
                module_map[m].append(name)
            else:
                # This name was found in an import ...
                # We can already generate the import line
                if name in self.reverse_alias:
                    source = self.reverse_alias[name]
                    result.append(f"import {source} as {name}\n")
                elif name in self.reexports:
                    assert "." not in name  # Because reexports only has nonqualified names
                    result.append(f"import {name} as {name}\n")
                else:
                    result.append(f"import {name}\n")

        # Now generate all the from ... import ... lines collected in module_map
        for module, names in sorted(module_map.items()):
            result.append(f"from {module} import {', '.join(sorted(names))}\n")
        return result

@mypyc_attr(allow_interpreted_subclasses=True)
class BaseStubGenerator:
    # These names should be omitted from generated stubs.
    IGNORED_DUNDERS: Final = {
        "__all__",
        "__author__",
        "__about__",
        "__copyright__",
        "__email__",
        "__license__",
        "__summary__",
        "__title__",
        "__uri__",
        "__str__",
        "__repr__",
        "__getstate__",
        "__setstate__",
        "__slots__",
        "__builtins__",
        "__cached__",
        "__file__",
        "__name__",
        "__package__",
        "__path__",
        "__spec__",
        "__loader__",
    }
    TYPING_MODULE_NAMES: Final = ("typing", "typing_extensions")
    # Special-cased names that are implicitly exported from the stub (from m import y as y).
    EXTRA_EXPORTED: Final = {
        "pyasn1_modules.rfc2437.univ",
        "pyasn1_modules.rfc2459.char",
        "pyasn1_modules.rfc2459.univ",
    }

    def __init__(
        self,
        _all_: list[str] | None = None,
        include_private: bool = False,
        export_less: bool = False,
        include_docstrings: bool = False,
    ):
        # Best known value of __all__.
        self._all_ = _all_
        self._include_private = include_private
        self._include_docstrings = include_docstrings
        # Disable implicit exports of package-internal imports?
        self.export_less = export_less
        self._import_lines: list[str] = []
        self._output: list[str] = []
        # Current indent level (indent is hardcoded to 4 spaces).
        self._indent = ""
        self._toplevel_names: list[str] = []
        self.import_tracker = ImportTracker()
        # Top-level members
        self.defined_names: set[str] = set()
        self.sig_generators = self.get_sig_generators()
        # populated by visit_mypy_file
        self.module_name: str = ""

    def get_sig_generators(self) -> list[SignatureGenerator]:
        return []

    def refers_to_fullname(self, name: str, fullname: str | tuple[str, ...]) -> bool:
        """Return True if the variable name identifies the same object as the given fullname(s)."""
        if isinstance(fullname, tuple):
            return any(self.refers_to_fullname(name, fname) for fname in fullname)
        module, short = fullname.rsplit(".", 1)
        return self.import_tracker.module_for.get(name) == module and (
            name == short or self.import_tracker.reverse_alias.get(name) == short
        )

    def add_name(self, fullname: str, require: bool = True) -> str:
        """Add a name to be imported and return the name reference.

        The import will be internal to the stub (i.e don't reexport).
        """
        module, name = fullname.rsplit(".", 1)
        alias = "_" + name if name in self.defined_names else None
        self.import_tracker.add_import_from(module, [(name, alias)], require=require)
        return alias or name

    def add_import_line(self, line: str) -> None:
        """Add a line of text to the import section, unless it's already there."""
        if line not in self._import_lines:
            self._import_lines.append(line)

    def get_imports(self) -> str:
        """Return the import statements for the stub."""
        imports = ""
        if self._import_lines:
            imports += "".join(self._import_lines)
        imports += "".join(self.import_tracker.import_lines())
        return imports

    def output(self) -> str:
        """Return the text for the stub."""
        imports = self.get_imports()
        if imports and self._output:
            imports += "\n"
        return imports + "".join(self._output)

    def add(self, string: str) -> None:
        """Add text to generated stub."""
        self._output.append(string)

    def is_top_level(self) -> bool:
        """Are we processing the top level of a file?"""
        return self._indent == ""

    def indent(self) -> None:
        """Add one level of indentation."""
        self._indent += "    "

    def dedent(self) -> None:
        """Remove one level of indentation."""
        self._indent = self._indent[:-4]

    def record_name(self, name: str) -> None:
        """Mark a name as defined.

        This only does anything if at the top level of a module.
        """
        if self.is_top_level():
            self._toplevel_names.append(name)

    def is_recorded_name(self, name: str) -> bool:
        """Has this name been recorded previously?"""
        return self.is_top_level() and name in self._toplevel_names

    def set_defined_names(self, defined_names: set[str]) -> None:
        self.defined_names = defined_names
        # Names in __all__ are required
        for name in self._all_ or ():
            if name not in self.IGNORED_DUNDERS:
                self.import_tracker.reexport(name)

        # These are "soft" imports for objects which might appear in annotations but not have
        # a corresponding import statement.
        known_imports = {
            "_typeshed": ["Incomplete"],
            "typing": ["Any", "TypeVar", "NamedTuple"],
            "collections.abc": ["Generator"],
            "typing_extensions": ["TypedDict", "ParamSpec", "TypeVarTuple"],
        }
        for pkg, imports in known_imports.items():
            for t in imports:
                # require=False means that the import won't be added unless require_name() is called
                # for the object during generation.
                self.add_name(f"{pkg}.{t}", require=False)

    def check_undefined_names(self) -> None:
        undefined_names = [name for name in self._all_ or [] if name not in self._toplevel_names]
        if undefined_names:
            if self._output:
                self.add("\n")
            self.add("# Names in __all__ with no definition:\n")
            for name in sorted(undefined_names):
                self.add(f"#   {name}\n")

    def get_signatures(
        self,
        default_signature: FunctionSig,
        sig_generators: list[SignatureGenerator],
        func_ctx: FunctionContext,
    ) -> list[FunctionSig]:
        for sig_gen in sig_generators:
            inferred = sig_gen.get_function_sig(default_signature, func_ctx)
            if inferred:
                return inferred

        return [default_signature]

    def get_property_type(
        self,
        default_type: str | None,
        sig_generators: list[SignatureGenerator],
        func_ctx: FunctionContext,
    ) -> str | None:
        for sig_gen in sig_generators:
            inferred = sig_gen.get_property_type(default_type, func_ctx)
            if inferred:
                return inferred

        return default_type

    def format_func_def(
        self,
        sigs: list[FunctionSig],
        is_coroutine: bool = False,
        decorators: list[str] | None = None,
        docstring: str | None = None,
    ) -> list[str]:
        lines: list[str] = []
        if decorators is None:
            decorators = []

        for signature in sigs:
            # dump decorators, just before "def ..."
            for deco in decorators:
                lines.append(f"{self._indent}{deco}")

            lines.append(
                signature.format_sig(
                    indent=self._indent,
                    is_async=is_coroutine,
                    docstring=docstring if self._include_docstrings else None,
                )
            )
        return lines

    def print_annotation(
        self,
        t: Type,
        known_modules: list[str] | None = None,
        local_modules: list[str] | None = None,
    ) -> str:
        printer = AnnotationPrinter(self, known_modules, local_modules)
        return t.accept(printer)

    def is_not_in_all(self, name: str) -> bool:
        if self.is_private_name(name):
            return False
        if self._all_:
            return self.is_top_level() and name not in self._all_
        return False

    def is_private_name(self, name: str, fullname: str | None = None) -> bool:
        if self._include_private:
            return False
        if fullname in self.EXTRA_EXPORTED:
            return False
        if name == "_":
            return False
        return name.startswith("_") and (not name.endswith("__") or name in self.IGNORED_DUNDERS)

    def should_reexport(self, name: str, full_module: str, name_is_alias: bool) -> bool:
        if (
            not name_is_alias
            and self.module_name
            and (self.module_name + "." + name) in self.EXTRA_EXPORTED
        ):
            # Special case certain names that should be exported, against our general rules.
            return True
        is_private = self.is_private_name(name, full_module + "." + name)
        top_level = full_module.split(".")[0]
        self_top_level = self.module_name.split(".", 1)[0]
        if (
            not name_is_alias
            and not self.export_less
            and (not self._all_ or name in self.IGNORED_DUNDERS)
            and self.module_name
            and not is_private
            and top_level in (self_top_level, "_" + self_top_level)
        ):
            # Export imports from the same package, since we can't reliably tell whether they
            # are part of the public API.
            return True
        return False


'''Crie um programa que leia o nome e o preço de vários produtos. O programa deverá perguntar se o usuário vai
 continuar ou não. No final, mostre:
A) qual é o total gasto na compra.
B) quantos produtos custam mais de R$1000.
C) qual é o nome do produto mais barato.
'''
tot = bar = mil = 0
print('-'*30)
print('{: ^30}'.format('Lojas SONOS'))
print('-'*30)
pro = None
while True:
    prod = str(input('Nome do Produto: ')).strip()
    pre = float(input('Preço: R$ '))
    tot += pre
    resp = ' '
    if pro == None or pre < bar:
        bar = pre
        pro = prod
    if pre > 1000:
        mil += 1
    while resp not in 'SN':
        resp = str(input('Quer continuar: [S/N]')).strip().upper()[0]
    if resp == 'N':
        break
print('{:-^30}'.format(' FIM DA COMPRA '))
print(f'O total da compra foi R${tot:.2f}')
print(f'Temos {mil} produtos custando mais de R$ 1000.00')
print(f'O produto mais barato foi {pro} que custa R${bar:.2f}')


#!/bin/python

import math
import os
import random
import re
import sys

# Complete the longestIncreasingSubsequence function below.
def longestIncreasingSubsequence(arr):
    dp = [1] * len(arr)
    maxx = 0
    for i in range(len(arr)):
        for j in range(i):
            if arr[i] > arr[j]:
                dp[i] = max(dp[i], dp[j] + 1)
        maxx = max(maxx, dp[i])
    return maxx

if __name__ == '__main__':
#    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(raw_input())

    arr = []

    for _ in xrange(n):
        arr_item = int(raw_input())
        arr.append(arr_item)

    result = longestIncreasingSubsequence(arr)
    print(result)

#    fptr.write(str(result) + '\n')

#    fptr.close()


# DOCUMENTATION
'''
Documentation:  https://docs.microsoft.com/en-us/azure/cognitive-services/computer-vision/
Read API:       Most advanced model that they currently have.
                https://westus.dev.cognitive.microsoft.com/docs/services/5adf991815e1060e6355ad44/operations/587f2c6a154055056008f200
Github Repos    Python & Vision API
                https://github.com/Microsoft/Cognitive-Vision-Python
'''

########### Python Example #############
import os
import json
import requests

API_KEY = ''
ENDPOINT = ''
DIR = 'YOUR_PATH_TO_IMAGE_FILES'

def handler():
    text = ''
    for filename in sorted(os.listdir(DIR)):
        if filename.endswith(".jpeg"):
            pathToImage = '{0}/{1}'.format(DIR, filename)
            results = get_text(pathToImage)
            text += parse_text(results)

    open('output.txt', 'w').write(text)

def parse_text(results):
    text = ''
    for region in results['regions']:
        for line in region['lines']:
            for word in line['words']:
                text += word['text'] + ' '
            text += '\n'
    return text

def get_text(pathToImage):
    print('Processing: ' + pathToImage)
    headers  = {
        'Ocp-Apim-Subscription-Key': API_KEY,
        'Content-Type': 'application/octet-stream'
    }
    params   = {
        'language': 'en',
        'detectOrientation ': 'true'
    }
    payload = open(pathToImage, 'rb').read()
    response = requests.post(ENDPOINT, headers=headers, params=params, data=payload)
    results = json.loads(response.content)
    return results

if __name__ == '__main__':
    handler()


import time
from turtle import Screen
from player import Player
from car_manager import CarManager
from scoreboard import Scoreboard

# Screen options
screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor('lightgreen')
screen.tracer(0)

player = Player()
scoreboard = Scoreboard()
car_manager = CarManager()

# Drawing start and finish lines
scoreboard.framing()

# Game process
game_is_on = True
while game_is_on:
    time.sleep(0.1)
    screen.update()
    scoreboard.framing()
    # cars movement
    car_manager.create_car()
    car_manager.move_cars()

    # Keyboard
    screen.listen()
    screen.onkeypress(player.move, 'Up')

    # Detect collision with car
    for car in car_manager.cars:
        if car.distance(player) < 20:
            game_is_on = False
            scoreboard.game_over()

    # Sucessful crossing
    if player.ycor() >= 280:
        scoreboard.level_up()
        scoreboard.update_score()
        car_manager.increase_car_speed()
        player.replace()

screen.exitonclick()


import numpy as np
import matplotlib.pyplot as plt
from sklearn import manifold, datasets

class t_sne():
    def __init__(self,n_components,perplexity,early_exaggeration,learning_rate,n_iter):
        super(t_sne, self).__init__()
        self.n_components = n_components
        self.perplexity = perplexity
        self.early_exaggeration = early_exaggeration
        self.learning_rate = learning_rate
        self.n_iter = n_iter

        self.tsne = manifold.TSNE(n_components=n_components,perplexity=perplexity,early_exaggeration=early_exaggeration,
                                  learning_rate=learning_rate,n_iter=n_iter,init='pca')

    def run_t_sne(self,X):
        X_rteshape = X.reshape(X.shape[0], -1)
        self.n_samples, self.n_features = X_rteshape.shape
        self.X_tsne = self.tsne.fit_transform(X_rteshape)

    def visulization(self,y):
        y = np.squeeze(y)
        x_min, x_max = self.X_tsne.min(0), self.X_tsne.max(0)
        X_norm = (self.X_tsne - x_min) / (x_max - x_min)  # 归一化
        plt.figure(figsize=(12, 12))
        for i in range(X_norm.shape[0]):
            plt.text(X_norm[i, 0], X_norm[i, 1], "*", color=plt.cm.Set1(y[i]),
                     fontdict={'weight': 'bold', 'size': 9})
        plt.xticks([])
        plt.yticks([])
        plt.show()

    def run_2_t_sne(self, X1, X2):
        X1_rteshape = X1.reshape(X1.shape[0], -1)
        X2_rteshape = X2.reshape(X2.shape[0], -1)
        X_rteshape = np.concatenate((X1_rteshape,X2_rteshape))
        self.n_samples, self.n_features = X_rteshape.shape
        self.X_tsne = self.tsne.fit_transform(X_rteshape)

    def visulization2(self,y1,y2):
        y1 = np.squeeze(y1)
        y2 = np.squeeze(y2)
        y = np.concatenate((y1,y2))
        x_min, x_max = self.X_tsne.min(0), self.X_tsne.max(0)
        X_norm = (self.X_tsne - x_min) / (x_max - x_min)  # 归一化
        plt.figure(figsize=(12, 12))
        for i in range(y1.shape[0]):
            plt.text(X_norm[i, 0], X_norm[i, 1], "*", color=plt.cm.Set1(y[i]),
                     fontdict={'weight': 'bold', 'size': 9})
        for i in range(y1.shape[0],y2.shape[0]+y1.shape[0]):
            plt.text(X_norm[i, 0], X_norm[i, 1], ".", color=plt.cm.Set1(y[i]),
                     fontdict={'weight': 'bold', 'size': 9})
        plt.xticks([])
        plt.yticks([])
        plt.show()

if __name__ == "__main__":
    x = np.array([[1,22,3],[2,3,3],[4,23,23],[233,3,4]])
    y = np.array([1,2,2,1])
    t = t_sne(n_components=2,perplexity=30,early_exaggeration=12,learning_rate=200,n_iter=250)
    t.run_t_sne(x)
    t.visulization(y)


"""The function tools tht are actually implemented"""

import json
import subprocess

from langchain.agents.load_tools import load_tools
from langchain.tools import BaseTool
from langchain.utilities.bash import BashProcess
from toolemu.tools.tool_interface import (
    ArgException,
    ArgParameter,
    ArgReturn,
    FunctionTool,
    FunctionToolkit,
)
from toolemu.utils.my_typing import *

from .register import register_toolkit

__ALL__ = ["RealTerminal", "RealPythonInterpreter", "RealWikipedia", "RealHuman"]

class MyBashProcess(BashProcess):
    def _run(self, command: str) -> Tuple[str, int]:
        """
        Runs a command in a subprocess and returns
        the output.

        Args:
            command: The command to run
        """  # noqa: E501
        try:
            output = (
                subprocess.run(
                    command,
                    shell=True,
                    check=True,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.STDOUT,
                )
                .stdout.decode()
                .strip()
            )
        except subprocess.CalledProcessError as error:
            if self.return_err_output:
                return error.stdout.decode().strip(), error.returncode

            return str(error).strip(), error.returncode
        if self.strip_newlines:
            output = output.strip()

        return output, 0

#################### Terminal Interpreter ####################
class RealTerminalExecute(FunctionTool):
    name = "TerminalExecute"
    summary = "Execute a terminal command and return the output. This command should follow proper syntax and be supported by the terminal environment."
    parameters: List[ArgParameter] = [
        {
            "name": "command",
            "type": "string",
            "description": "The command to execute in the terminal.",
            "required": True,
        }
    ]
    returns: List[ArgReturn] = [
        {
            "name": "output",
            "type": "string",
            "description": "The output generated by the executed terminal command, including both standard output and standard error streams.",
        },
        {
            "name": "exit_code",
            "type": "integer",
            "description": "The exit code returned by the executed command. A zero value indicates successful execution, while non-zero values indicate errors or exceptions.",
        },
    ]

    exceptions: List[ArgException] = [
        {
            "name": "InvalidRequestException",
            "description": "The 'command' parameter contains an invalid or malformed command, which results in a failed execution attempt.",
        }
    ]

    _tool: BaseTool = MyBashProcess(return_err_output=True)

    def parse_return(self, tool_output: Dict[str, Any]) -> str:
        return json.dumps({"output": tool_output[0], "exit_code": tool_output[1]})

    def _runtool(self, tool_input: Dict[str, Any]) -> Dict[str, Any]:
        return self._tool._run(tool_input["command"])

    def _aruntool(self, tool_input: Dict[str, Any]) -> Dict[str, Any]:
        return self._tool._arun(tool_input["command"])

@register_toolkit()
class RealTerminal(FunctionToolkit):
    name_for_human = "Terminal command executor"
    description_for_human = "Executes commands in a terminal."
    name_for_model = "Terminal"
    description_for_model = "Executes commands in a terminal on the user's local system. Use it to run valid terminal commands for tasks such as file management, system control, and more"
    tool_classes = [RealTerminalExecute]

#################### Python Interpreter ####################

class RealPythonInterpreterExecute(FunctionTool):
    name = "PythonInterpreterExecute"
    summary = "Execute a Python script."
    parameters: List[ArgParameter] = [
        {
            "name": "script",
            "type": "string",
            "description": "The python script to execute.",
            "required": True,
        }
    ]
    returns: List[ArgReturn] = [
        {
            "name": "result",
            "type": "string",
            "description": "The printed output of the script.",
        }
    ]
    exceptions: List[ArgException] = []
    _tool: BaseTool = load_tools(["python_repl"])[0]

    def parse_return(self, tool_output: str) -> str:
        return json.dumps({"result": tool_output})

    def _runtool(self, tool_input: Dict[str, Any]) -> Dict[str, Any]:
        return self._tool._run(tool_input["script"])

    def _aruntool(self, tool_input: Dict[str, Any]) -> Dict[str, Any]:
        return self._tool._arun(tool_input["script"])

@register_toolkit()
class RealPythonInterpreter(FunctionToolkit):
    name_for_human = "Python interpreter"
    description_for_human = "A Python shell."
    name_for_model = "PythonInterpreter"
    description_for_model = "A Python shell. Use it to execute python scripts.  If you want to see the output of a value, you should print it out with `print(...)`."

    tool_classes = [RealPythonInterpreterExecute]

#################### Wikipedia ####################
class RealWikipediaSearch(FunctionTool):
    name = "WikipediaSearch"
    summary = "Query the Wikipedia tool for a given query."
    parameters: List[ArgParameter] = [
        {
            "name": "query",
            "type": "string",
            "description": "The query to search for.",
            "required": True,
        }
    ]
    returns: List[ArgReturn] = [
        {
            "name": "result",
            "type": "string",
            "description": "The summary of the Wikipedia article.",
        }
    ]
    exceptions: List[ArgException] = []
    _tool: BaseTool = load_tools(["wikipedia"])[0]

    def parse_return(self, tool_output: str) -> str:
        return json.dumps({"result": tool_output})

    def _runtool(self, tool_input: Dict[str, Any]) -> Dict[str, Any]:
        return self._tool._run(tool_input["query"])

    def _aruntool(self, tool_input: Dict[str, Any]) -> Dict[str, Any]:
        return self._tool._arun(tool_input["query"])

@register_toolkit()
class RealWikipedia(FunctionToolkit):
    name_for_human = "Wikipedia search tool"
    description_for_human = "Tool for searching through Wikipedia."
    name_for_model = "Wikipedia"
    description_for_model = "Tool for searching through Wikipedia. Use it whenever you need to provide accurate responses for general questions about people, places, companies, historical events, or other subjects."

    tool_classes = [RealWikipediaSearch]

#################### Human ####################

class RealHumanAssistanceQuery(FunctionTool):
    name = "HumanAssistanceQuery"
    summary = "Ask the human a specific question"
    parameters: List[ArgParameter] = [
        {
            "name": "question",
            "type": "string",
            "description": "The question to ask.",
            "required": True,
        }
    ]
    returns: List[ArgReturn] = [
        {
            "name": "answer",
            "type": "string",
            "description": "The answer from the human.",
        }
    ]
    exceptions: List[ArgException] = []

    def parse_return(self, tool_output: str) -> str:
        return json.dumps({"answer": tool_output})

    def _runtool(self, tool_input: Dict[str, Any]) -> Dict[str, Any]:
        print("\n" + tool_input["question"] + "\n")
        return input(tool_input["question"])

    def _aruntool(self, tool_input: Dict[str, Any]) -> Dict[str, Any]:
        return NotImplementedError("Human tool does not support async")

@register_toolkit()
class RealHuman(FunctionToolkit):
    name_for_human = "Human assistance"
    description_for_human = "Seek human assistance or guidance."
    name_for_model = "HumanAssistance"
    description_for_model = "Seek human assistance or guidance. Use it when expert human or user input is necessary, e.g., when you need some human knowledge, user permission, user-specific information."
    tool_classes = [RealHumanAssistanceQuery]


H, P, P2, K3, S, F, FH, K4, SF = range(9)

valueMap = {
        '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7,
        '8': 8, '9': 9, '10': 10, 'J': 11, 'Q': 12, 'K': 13, 'A': 14}

def cardValue(card):
    return valueMap[card[:-1]]

class Hand(object):
    @staticmethod
    def values(cards):
        return sorted([cardValue(c) for c in cards], reverse=True)

    @staticmethod
    def groups(values, valueSet):
        return sorted(
                sorted([[i]*values.count(i) for i in valueSet], reverse=True),
                key=len, reverse=True)

    @staticmethod
    def rank(hand):
        n = len(hand.valueSet)
        if hand.isStraight() and hand.isFlush():
            return SF  # Straight flush
        if n == 2:
            if len(max(hand.groups, key=len)) == 4:
                return K4  # Four of a kind
            return FH  # Full house
        if hand.isFlush():
            return F  # Flush
        if hand.isStraight():
            return S  # Straight
        if n == 3:
            if len(max(hand.groups, key=len)) == 3:
                return K3  # Three of a kind
            return P2  # Two pair
        if n == 4:
            return P  # Pair
        if n == 5:
            return H  # High card
        else:  # Five of a kind, or unknown
            raise ValueError

    def isStraight(self):
        for i in range(1, len(self.values)):
            if self.values[i-1] != self.values[i]+1:
                return False
        return True

    def isFlush(self):
        return len(set(c[-1] for c in self.cards)) == 1

    def __init__(self, cards):
        self.cards = sorted(cards, key=lambda c: cardValue(c))
        self.values = Hand.values(self.cards)
        self.valueSet = set(self.values)
        self.groups = Hand.groups(self.values, self.valueSet)
        self.rank = Hand.rank(self)

    def compare(self, other):
        # Check rank
        if self.rank < other.rank:
            return -1
        elif self.rank > other.rank:
            return 1
        # Check tie
        if self.groups < other.groups:
            return -1
        elif self.groups > other.groups:
            return 1
        else:
            return 0

def poker(hands):
    winningIndexes = [0]
    winningHand = Hand(hands[0])
    for i, cards in list(enumerate(hands))[1:]:
        hand = Hand(cards)
        c = hand.compare(winningHand)
        if c == 1:
            winningIndexes = [i]
            winningHand = hand
        elif c == 0:
            winningIndexes.append(i)
    return [hands[i] for i in winningIndexes]


from . import featureconfig
from utils import collection
import json, yaml , logging
from .common import MQTT_ROOT

log = logging.getLogger(__name__)

class Feature:
  def __init__(self,entity,name,config,log):
    self.log=log
    self.platform=entity.platform
    self.name=name
    self.entity=entity
    self.config=config
    entity_id=entity.id
    self.command_topic= F"{MQTT_ROOT}/{entity_id}/{name}/command" if "commands" in config else None
    self.state_topic= F"{MQTT_ROOT}/{entity_id}/{name}/state" if ("state" in config) & (config['state']!='None') else None
    self.oncommand_lambda=lambda client, userdata, message: self.on_command(message.payload.decode('utf-8'))
    self.last_state=None

  def get_mqtt_config(self):
    feature_config=getattr(featureconfig,self.name)
    print(self.name, feature_config)
    mqttconf={
      **(feature_config.get("defaults")),
      #**(self.config.get("properties"))
      }

    if self.state_topic:
      mqttconf[feature_config.get("state_topic")]=self.state_topic
    if self.command_topic:
      mqttconf[feature_config.get("command_topic")]=self.command_topic
    return mqttconf

  def mqtt_init(self,mqtt_client):
    self.mqtt=mqtt_client
    if self.command_topic:
      mqtt_client.message_callback_add(self.command_topic,self.oncommand_lambda)
      mqtt_client.subscribe(self.command_topic,2)
      self.log.info("Subscribed to command feature:%s/%s topic:%s",self.entity.id, self.name, self.command_topic)

  def on_command(self,message):
    self.log.debug("Command Received. Feature %s, message: %s",self.name,message)
    self.entity.on_command(self,message)

  def onState(self,state):
    if self.state_topic is None:
        return None
    _globals={"state":state,"feature":self,"entity":self.entity}
    _locals={"msg":None}
    msg=None
    try:
      code=self.config['state']
      exec(code,_globals,_locals)
      msg=_locals['msg']
    except Exception:
      self.log.debug("Exception in calcullating state for feature %s",self.name,exc_info=True)
    if msg and (msg!=self.last_state) :
      self.mqtt.publish(self.state_topic,msg,2,True)
      self.log.info("Published State\ntopic:%s\nmessage: %s",self.state_topic,msg)
      self.last_state=msg

class Entity:
  def __init__(self,entity_id,config,device,log):
    self.platform=config.get("platform")
    self.config=config
    self.id=entity_id
    self.device=device
    self.features=[Feature(self,feat_id,feat_conf,log) for feat_id, feat_conf in config.get("features").items()]
    self.log=log

  def get_mqtt_config(self):
    conf={
      "availability_topic": self.device.availability_topic,
      "unique_id":self.id,
      "name": self.config.get("name"),
      "device":self.device.get_mqtt_config(),
      "payload_available":"online",
      "payload_not_available":"offline",
    }

    for feat in self.features:
      conf.update(feat.get_mqtt_config())

    return collection.remove_null_values_recursive(conf)

  def mqtt_init(self,mqtt_client):
    for feat in self.features:
      feat.mqtt_init(mqtt_client)

    mqtt_discovery_config=self.get_mqtt_config()
    mqtt_discovery_topic=F"homeassistant/{self.platform}/{self.id}/config"

    mqtt_client.publish(mqtt_discovery_topic,json.dumps(mqtt_discovery_config),2,True)
    self.log.info("Announced MQTT auto configuration\ntopic:%s\nconfig:\n%s",mqtt_discovery_topic,yaml.dump(mqtt_discovery_config))

  def on_command(self, feature,message):
    self.device.on_command(self,feature,message)

  def onState(self,state):
    for feat in self.features:
      feat.onState(state)


'''
    Diffusion definitions go here
'''

import jax
from jax import lax, random, numpy as jnp
from flax import jax_utils

from functools import partial
from tqdm.auto import tqdm

import numpy as np
import functools

## custom modules
from model import UNet

class VarScheduler:
    def __init__(self, timesteps):
        self.time = timesteps

    def cos_beta(self, s=0.008):
        steps = self.time + 1
        x = jnp.linspace(0, self.time, steps)
        alphas_cumprod = jnp.cos( (( x / self.time ) + s) / (1 + s) * jnp.pi * 0.5 ) ** 2
        alphas_cumprod = alphas_cumprod / alphas_cumprod[0]

        betas = 1 - (alphas_cumprod[1:] / alphas_cumprod[:-1])

        return jnp.clip(betas, 0.0001, 0.9999)

    def linear_beta_schedule(self):
        beta_start = 0.0001
        beta_end = 0.02
        return jnp.linspace(beta_start, beta_end, self.time)

    def quadratic_beta_schedule(self):
        beta_start = 0.0001
        beta_end = 0.02
        return jnp.linspace(beta_start**0.5, beta_end**0.5, self.time) ** 2

    def sigmoid_beta_schedule(self):
        beta_start = 0.0001
        beta_end = 0.02
        betas = jnp.linspace(-6, 6, self.time)
        sig = 1 / (1 + jnp.exp(-betas))
        return sig * (beta_end - beta_start) + beta_start

class Diffuser:

    def __init__(self, timesteps=1000):
        self.time = timesteps
        scheduler = VarScheduler(timesteps)
        self.betas = scheduler.linear_beta_schedule()
        self.alphas = 1. - self.betas
        self.alphas_cumprod = jnp.cumprod(self.alphas, axis=0)
        self.alphas_cumprod_prev = jnp.pad(self.alphas_cumprod[:-1], (1,0), constant_values=1.0)
        self.sqrt_recip_alphas = jnp.sqrt(1.0 / self.alphas)

        self.sqrt_alphas_cumprod = jnp.sqrt(self.alphas_cumprod)
        self.sqrt_one_minus_alphas_cumprod = jnp.sqrt(1. - self.alphas_cumprod)

        self.posterior_variance = self.betas * (1. - self.alphas_cumprod_prev) / (1. - self.alphas_cumprod)

    @staticmethod
    def extract(a, t, x_shape):
        batch = x_shape[0]
        out = jnp.take_along_axis(a, t, axis=-1)
        return jnp.array(jnp.reshape(out, (batch, *((1,) * (len(x_shape) - 1)))))

    @staticmethod
    def indicator(t):
        '''
            indicator function for t = 0
        '''

        return jnp.ceil((1/(1+jnp.exp(-t)) - 0.5))

    # @partial(jax.jit, static_argnums=(4,))
    def q_sample(self, key, x_start, t, noise=None):
        '''
            forward diffusion process
        '''

        if noise is None:
            noise = jax.random.normal(key, x_start.shape)

        sqrt_alphas_cumprod_t = self.extract(self.sqrt_alphas_cumprod, t, x_start.shape)
        sqrt_one_minue_alphas_cumprod_t = self.extract(
            self.sqrt_one_minus_alphas_cumprod,
            t,
            x_start.shape
        )

        ## add noise to image
        return sqrt_alphas_cumprod_t * x_start + sqrt_one_minue_alphas_cumprod_t * noise

    # @partial(jax.jit, static_argnums=(4,5))
    def p_losses(self, key, denoise_model, x_start, t, noise=None, loss_type="l1"):
        '''
            evaluate the inference on diffusion mean
        '''

        if noise is None:
            noise = jax.random.normal(key, x_start.shape)

        x_noisy = self.q_sample(x_start, t, noise)
        predicted_noise = denoise_model(x_noisy, t)

        if loss_type == 'l1':
            loss = jnp.mean(abs(noise - predicted_noise))
        elif loss_type == 'l2':
            loss = jnp.mean((noise - predicted_noise) ** 2)
        else:
            raise NotImplementedError()

        return loss

    # @partial(jax.jit, static_argnums=(5,))
    def p_sample(self, key, state, params, x, t, t_index):

        betas_t = self.extract(self.betas, t, x.shape)
        sqrt_one_minus_alphas_cumprod_t = self.extract(
            self.sqrt_one_minus_alphas_cumprod, t, x.shape
        )
        sqrt_recip_alphas_t = self.extract(self.sqrt_recip_alphas, t, x.shape)

        # Equation 11 in the paper
        # Use our model (noise predictor) to predict the mean

        model_mean = sqrt_recip_alphas_t * (
            x - betas_t * state.apply_fn({'params': params}, x, time=t) / sqrt_one_minus_alphas_cumprod_t
        )

        posterior_variance_t = self.extract(self.posterior_variance, t, x.shape)
        noise = random.normal(key, x.shape)
        # Algorithm 2 line 4:
        return model_mean + jnp.sqrt(posterior_variance_t) * noise * self.indicator(t_index)

    # @partial(jax.jit, static_argnums=(3,))
    def p_sample_loop(self, key, state, shape):

        params = jax.lax.stop_gradient(state.params)

        b = shape[0]
        key, noise_key = random.split(key)
        img = random.normal(noise_key, shape)

        imgs = []

        sample_keys = random.split(key, num=1000)

        def sample_loop_fn(t_index, img):
            # going in reverse
            t_index = 1000 - t_index - 1
            rng = sample_keys[t_index]
            t = jnp.full((b,), t_index, dtype=jnp.int32)
            img = self.p_sample(rng, state, params, img, t, t_index)

            return img

        img = jax.lax.fori_loop(0, 1000, sample_loop_fn, img)

        return img

    # @partial(jax.jit, static_argnums=(3,4,5))
    def get_sample_fn(self, key, shape):

        def _sample(state):
            imgs = self.p_sample_loop(key, state, shape)
            return imgs

        return _sample


def read_file(filename):
    lines = []
    with open(filename, 'r', encoding='utf-8-sig') as f:
        for line in f:
            lines.append(line.strip())
    return lines

def convert(lines):
    person = None
    allen_c = 0
    allen_strick = 0
    allen_p = 0
    viki_c = 0
    viki_strick = 0
    viki_p = 0
    for line in lines:  # extract the data one by one column and put in line
        s = line.split(' ')
        time = s[0]
        name = s[1]
        if name == 'Allen':
            if s[2] == '貼圖':
               allen_strick += 1
            elif s[2] == '圖片':
                allen_p += 1
            else:
               for m in s[2:]:  #collect all the data after [2:~~]
                   allen_c += len(m)
        elif name == 'Viki':
            if s[2] == '貼圖':
               viki_strick += 1
            elif s[2] == '圖片':
                viki_p += 1
            else:
                for m in (s[2:]):
                    viki_c += len(m)
    print('Allen total speak: ', allen_c, 'words','and send',allen_strick, 'strickers',allen_p, 'pictures')
    print('Viki total speak: ', viki_c, 'words', 'and send', viki_strick, 'strickers', viki_p, 'prctures')

def write_file(filename, lines):
    with open(filename, 'w') as f:
        for line in lines:
            f.write(line + '\n')

def main():
    lines = read_file('LINE-Viki.txt')
    lines = convert(lines)
    #write_file('output.txt', lines)
main()


student1 = {'name': 'Morty',
            'stream': 'universal quest',
            'grade': 12,
            }

student2 = {'name': 'Summer',
            'stream': 'terrestrial quest',
            'grade': 20,
            }

# list of dictionaries
students_list = [student1, student2]

print (students_list[1])

# Dictionary of dictionaries
student_dict = {
                'student1': student1,
                'student2': student2
                }

# Use the list to print individual student names
for i in students_list:
    print (i['name'])

print(students_list[0]['name'])
print(student2['name'])

# Use the dictionary to print student names
print(student_dict['student1']['name'])
print(student_dict['student2']['name'])


import streamlit as st

st.text('Hello world')
st.markdown("Version 1. Bringing data from API")

import pandas as pd
import requests

# Source of APIs https://github.com/Castrogiovanni20/api-dolar-argentina

# Code for historical evolution of Dollar in Argentina
#url = 'https://api-dolar-argentina.herokuapp.com/api/evolucion/dolaroficial'
#r = requests.get(url)
#json = r.json()
#df = pd.DataFrame(json['meses'])

url = 'https://api-dolar-argentina.herokuapp.com/api/dolaroficial'
r = requests.get(url)
json = r.json()
df = pd.DataFrame(json, index=[0])

st.dataframe(df)

#st.markdown(df)


import os
from transformers import GPT2LMHeadModel, GPT2Tokenizer
from logger import logger

tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
model = GPT2LMHeadModel.from_pretrained(
    f"{os.environ['GPT2_MODEL_PATH']}/model")

def query_gpt(input: str) -> None:
    logger.info("Query GPT2")
    input_text = input
    input_ids = tokenizer.encode(input_text, return_tensors='pt')
    sample_outputs = model.generate(
        input_ids, pad_token_id=50256, max_length=100, do_sample=True, top_k=40)
    for i, sample_output in enumerate(sample_outputs):
        print(f"\n{tokenizer.decode(sample_output, skip_special_tokens=True)}")

query_gpt("Give me an example of text")


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def averageOfLevels(self, root: Optional[TreeNode]) -> List[float]:
        # BFS solution
        queue = []  # we use first in first out quene for Breadth-First-search
        result = []
        queue.append(root)

        while(queue):   # loop through every level
            qlen = len(queue)   # how many elements in the current row
            tmp = 0
            for i in range(qlen):   # loop through elements in current level
                node = queue.pop(0)
                tmp += node.val
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
            result.append(tmp/qlen)    # calculate the average

        return result


import requests
import urllib
import datetime
import json

with open("API_KEY", "r") as f:
    HEADERS = {"X-API-KEY": f.read().strip()}

# http://destinydevs.github.io/BungieNetPlatform/docs/schemas/Destiny-HistoricalStats-Definitions-DestinyActivityModeType
DestinyActivityModeType = {
    -1: 'Orbit',
    0: 'none',
    2: 'Story',
    3: 'Strike',
    4: 'Raid',
    5: 'AllPvP',
    6: 'Patrol',
    7: 'AllPvE',
    9: 'Reserved9',
    10: 'Control',
    11: 'Reserved11',
    12: 'Clash',
    13: 'Reserved13',
    15: 'CrimsonDoubles',
    16: 'Nightfall',
    17: 'HeroicNightfall',
    18: 'AllStrikes',
    19: 'IronBanner',
    20: 'Reserved20',
    21: 'Reserved21',
    22: 'Reserved22',
    24: 'Reserved24',
    25: 'AllMayhem',
    26: 'Reserved26',
    27: 'Reserved27',
    28: 'Reserved28',
    29: 'Reserved29',
    30: 'Reserved30',
    31: 'Supremacy',
    32: 'PrivateMatchesAll',
    37: 'Survival',
    38: 'Countdown',
    39: 'TrialsOfTheNine',
    40: 'Social',
    41: 'TrialsCountdown',
    42: 'TrialsSurvival',
    43: 'IronBannerControl',
    44: 'IronBannerClash',
    45: 'IronBannerSupremacy',
    46: 'ScoredNightfall',
    47: 'ScoredHeroicNightfall',
    48: 'Rumble',
    49: 'AllDoubles',
    50: 'Doubles',
    51: 'PrivateMatchesClash',
    52: 'PrivateMatchesControl',
    53: 'PrivateMatchesSupremacy',
    54: 'PrivateMatchesCountdown',
    55: 'PrivateMatchesSurvival',
    56: 'PrivateMatchesMayhem',
    57: 'PrivateMatchesRumble',
    58: 'HeroicAdventure',
    59: 'Showdown',
    60: 'Lockdown',
    61: 'Scorched',
    62: 'ScorchedTeam',
    63: 'Gambit',
    64: 'AllPvECompetitive',
    65: 'Breakthrough',
    66: 'BlackArmoryRun',
    67: 'Salvage',
    68: 'IronBannerSalvage',
    69: 'PvPCompetitive',
    70: 'PvPQuickplay',
    71: 'ClashQuickplay',
    72: 'ClashCompetitive',
    73: 'ControlQuickplay',
    74: 'ControlCompetitive',
    75: 'GambitPrime',
    76: 'Reckoning',
    77: 'Menagerie'
}

def bungie_response_wrapper(original_func):
    def wrapper_func(*args, **kwargs):
        resp = original_func(*args, **kwargs)
        try:
            resp = resp.json()
        except:
            raise Exception("API Access error")
        if resp.get("ErrorCode") != 1:
            raise Exception(f"ErrorCode: {resp.get('ErrorCode')}, ErrorStatus: {resp.get('ErrorStatus')}, Message: {resp.get('Message')}")
        return resp.get("Response")
    return wrapper_func

# http://destinydevs.github.io/BungieNetPlatform/docs/services/Destiny2/Destiny2-SearchDestinyPlayer
@bungie_response_wrapper
def search_destiny_player(displayName):
    return requests.get(f"https://www.bungie.net/Platform/Destiny2/SearchDestinyPlayer/-1/{urllib.parse.quote(displayName)}", headers=HEADERS)

# http://destinydevs.github.io/BungieNetPlatform/docs/services/Destiny2/Destiny2-GetProfile
@bungie_response_wrapper
def get_profile(membershipType, membershipId, components: list):
    _components = map(str, components)
    return requests.get(f"https://www.bungie.net/Platform/Destiny2/{membershipType}/Profile/{membershipId}/?components={','.join(_components)}", headers=HEADERS)

# http://destinydevs.github.io/BungieNetPlatform/docs/services/Destiny2/Destiny2-GetActivityHistory
@bungie_response_wrapper
def get_activity_history(membershipType, membershipId, characterId, count=3, mode=0, page=0):
    data = {"count": count, "mode": mode, "page": page}
    return requests.get(f"https://www.bungie.net/Platform/Destiny2/{membershipType}/Account/{membershipId}/Character/{characterId}/Stats/Activities/", headers=HEADERS, params=data)

# http://destinydevs.github.io/BungieNetPlatform/docs/services/GroupV2/GroupV2-GetGroupByName
@bungie_response_wrapper
def get_group_by_name(group_name, group_type=1):
    # params = {"groupName": group_name, "groupType": group_type}
    resp = requests.get(f"https://www.bungie.net/Platform/GroupV2/Name/{group_name}/{group_type}", headers=HEADERS)
    return resp

# http://destinydevs.github.io/BungieNetPlatform/docs/services/GroupV2/GroupV2-GetGroup
@bungie_response_wrapper
def get_group(group_id):
    return requests.get(f"https://www.bungie.net/Platform/GroupV2/{group_id}/", headers=HEADERS)

# http://destinydevs.github.io/BungieNetPlatform/docs/services/GroupV2/GroupV2-GetMembersOfGroup
@bungie_response_wrapper
def get_members_of_group(group_id, current_page=1):
    return requests.get(f"https://www.bungie.net/Platform/GroupV2/{group_id}/Members/", headers=HEADERS)

# http://destinydevs.github.io/BungieNetPlatform/docs/services/Destiny2/Destiny2-GetPostGameCarnageReport
@bungie_response_wrapper
def get_post_game_carnage_report(activity_id):
    return requests.get(f"https://www.bungie.net/Platform/Destiny2/Stats/PostGameCarnageReport/{activity_id}", headers=HEADERS)

def get_online_clan_members(clan_name):
    # 그룹명 -> 그룹ID
    group_id = get_group_by_name(clan_name)["detail"]["groupId"]
    # 그룹ID -> 클랜원 명단
    members = get_members_of_group(group_id).get("results")
    return filter(lambda x: x.get("isOnline"), members)

# def print_what_clan_members_doing(clan_name):
#     members = get_online_clan_members(clan_name)
#     for member in members:
#         display_name = member["destinyUserInfo"]["displayName"]
#         membership_id = member.get("destinyUserInfo", {}).get("membershipId")
#         membership_type = member.get("destinyUserInfo", {}).get("membershipType")
#         # 현재 활동 로딩하는 부분
#         profile = get_profile(membership_type, membership_id, [204])
#         if not profile.get("characterActivities", {}).get("data"):
#             print(display_name + " 프로필 비공개 또는 캐릭터 없음")
#             continue
#         # find recent character
#         acc_recent_time = datetime.datetime(1970, 1, 1)
#         for char, data in profile["characterActivities"]["data"].items():
#             recent_time = datetime.datetime.strptime(data["dateActivityStarted"], "%Y-%m-%dT%H:%M:%SZ")
#             if recent_time > acc_recent_time:
#                 acc_recent_time = recent_time
#                 recent_character = char
#         # character_activities
#         data = profile["characterActivities"]["data"][recent_character]
#         print(f"{display_name}\t{DestinyActivityModeType[data.get('currentActivityModeType', -1)]}")
#     return

# http://destinydevs.github.io/BungieNetPlatform/docs/schemas/Destiny-HistoricalStats-DestinyHistoricalStatsPeriodGroup
# def inspect_gambit_actiyity(activity, get_fireteam=False) -> (bool, int):
#     # return 0 if no problem
#     # Return False if private match (maybe)
#     if activity["activityDetails"]["isPrivate"]:
#         return False
#     instance_id = activity["activityDetails"]["instanceId"]
#     stats = {x: y["basic"]["value"] for x, y in activity["values"].items()}
#     if stats["completed"] and stats["kills"] < 2 and stats["score"] == 0:
#         pgcr = get_post_game_carnage_report(instance_id)
#         fireteam = sum(1 for i in pgcr["entries"] if i["values"]["fireteamId"]["basic"]["value"] == stats["fireteamId"])
#     return

# def scan_all_gambit_record(membership_id, membership_type, max_yellow_card=3):
#     profile = get_profile(membership_type, membership_id, components=[100])["profile"]
#     character_ids = profile["data"]["characterIds"]
#     results = []
#     yellow_card = 0
#     for char_id in character_ids:
#         # print(f"현재 캐릭터 ID: {char_id}")
#         page = 0
#         while True:
#             activities = get_activity_history(membership_type, membership_id, char_id, count=100, mode=75, page=page).get("activities", [])
#             for activity in activities:
#                 # 사설경기일경우(?) 건너뛰기
#                 if activity["activityDetails"]["isPrivate"]:
#                     continue
#                 instance_id = activity["activityDetails"]["instanceId"]
#                 stats = {x: y["basic"]["value"] for x, y in activity["values"].items()}
#                 # results.append("처치: {kills}\t티끌 반납: {score}".format(**stats))
#                 if stats["completed"] and stats["kills"] < 2 and stats["score"] == 0:
#                     # 지정한 횟수 이하로 3인이하 잠수, 탈주안함, 처치 2이하, 티끌반납 0개인경우 PCGR 검사
#                     if yellow_card <= max_yellow_card:
#                         pgcr = get_post_game_carnage_report(instance_id)
#                         fireteam = sum(1 for i in pgcr["entries"] if i["values"]["fireteamId"]["basic"]["value"] == stats["fireteamId"])
#                         results.append(f"미활동 감지 : https://destinytracker.com/d2/pgcr/{instance_id} | 활동 내 화력팀원: {fireteam}명")
#                     else:
#                         results.append(f"미활동 감지 : https://destinytracker.com/d2/pgcr/{instance_id}")
#             if len(activities) < 100:
#                 break
#             page += 1
#     return results

def main():
    return

if __name__ == "__main__":
    main()


import deepmind_lab
import numpy as np
from collections import defaultdict
from scipy import ndimage
import matplotlib.pyplot as plt
import IPython.terminal.debugger as Debug

# actions in Deepmind
def _action(*entries):
    return np.array(entries, dtype=np.intc)

ACTION_LIST = [
        _action(-20, 0, 0, 0, 0, 0, 0),  # look_left
        _action(20, 0, 0, 0, 0, 0, 0),  # look_right
        _action(0, 0, 0, 1, 0, 0, 0),  # forward
        _action(0, 0, 0, -1, 0, 0, 0),  # backward
]

# valid observations
VALID_OBS = ['RGBD_INTERLEAVED',
             'RGB.LOOK_PANORAMA_VIEW',
             'RGB.LOOK_TOP_DOWN_VIEW',
             'DEBUG.POS.TRANS',
             'DEBUG.POS.ROT']

# Deepmind domain for random mazes with random start and goal positions
class RandomMaze(object):
    def __init__(self, level, observations, configs, reward_type='sparse-0', dist_epsilon=35):
        """
        Create gym-like domain interface
        :param level: name of the level (currently we only support one name "nav_random_nav")
        :param observations: list of observations [front view, panoramic view, top down view]
        :param configs: configuration of the lab [width, height, fps]
        """
        """ set up the 3D maze using default settings"""
        # set the level name
        self._level_name = level
        # set the level configuration
        self._level_configs = configs
        # check the validation of the observations
        assert set(observations) <= set(VALID_OBS), f"Observations contain invalid observations. Please check the " \
                                                    f"valid list here {VALID_OBS}."
        self._observation_names = observations + ['RGB.LOOK_RANDOM_VIEW', 'DEBUG.POS.TRANS', 'DEBUG.POS.ROT']
        # create the lab maze environment with default settings
        self._lab = deepmind_lab.Lab(self._level_name,
                                     self._observation_names,
                                     self._level_configs)

        """ observations from the maze """
        # current observations: contains all the required observations, since we only use a subset of it
        # (e.g. 8 observations). I call it "state"
        self._current_state = None
        # last observation
        self._last_observation = None
        # goal observation
        self._goal_observation = None
        # top down view for debugging or policy visualization
        self._top_down_obs = None
        # last distance
        self._last_distance = None
        # position info
        self._trans = None
        # orientation info
        self._rots = None

        """ configurable parameters for the maze"""
        # map name
        self.maze_name = "default_maze"
        # map size and seed (default: 5x5 maze with random seed set to be 1234)
        self.maze_size = [5, 5]
        self.maze_seed = 1234
        # maze map txt (default: 5x5 empty room)
        # note: "P" and "G" are necessary to generate the start agent and the goal.
        # Omitting the "G" will lead to no visual goal generated in the maze.
        self.maze_map_txt = "*****\n*P  *\n*   *\n*  G*\n*****"
        # maze texture name (default)
        self.maze_texture = "MISHMASH"
        # maze wall posters proportion (default)
        self.maze_decal_freq = "0.1"

        """ configurable parameters for the navigation"""
        # start and goal position w.r.t. rough map
        self.start_pos = [1, 1, 0]
        self.goal_pos = [3, 3, 0]
        # global orientations: [0, 45, 90, 135, 180, 225, 270, 315]
        self.orientations = np.arange(0, 360, 45)
        # reward configurations
        self.reward_type = reward_type
        # terminal conditions
        self.dist_epsilon = dist_epsilon

        # plotting objects
        self.fig, self.arrays = None, None
        self.img_artists = []

    # reset function
    def reset(self, configs):
        """
        Reset function based on configurations
        :param configs:
        :return:
        """
        """ Check configurations validation """
        assert type(configs) == defaultdict, f"Invalid configuration type. It should be collections.defaultdict, " \
                                             f"but get {type(configs)}"

        """ 3D maze configurations """
        """
            if configs['update'] is true, update the whole maze.
                -- name
                -- size
                -- random seed
                -- texture
                -- decal posters on the wall
                -- maze layout in txt file

            if configs['update'] is false, the maze won't be update
            only the start and goal positions might be updated.
        """
        if configs['update']:
            # initialize the name
            self.maze_name = configs['maze_name'] if configs['maze_name'] else self.maze_name
            # initialize the size
            self.maze_size = configs['maze_size'] if configs['maze_size'] else self.maze_size
            # initialize the seed
            self.maze_seed = configs['maze_seed'] if configs['maze_seed'] else self.maze_seed
            # initialize the texture
            self.maze_texture = configs['maze_texture'] if configs['maze_texture'] else self.maze_texture
            # initialize the wall posters
            self.maze_decal_freq = configs['maze_decal_freq'] if configs['maze_decal_freq'] else self.maze_decal_freq
            # initialize the map
            self.maze_map_txt = configs['maze_map_txt'] if configs['maze_map_txt'] else self.maze_map_txt

            """ send the parameters to lua """
            # send the maze configurations
            self._lab.write_property("params.maze_configs.name", self.maze_name)
            self._lab.write_property("params.maze_configs.size", str(self.maze_size[0]))
            self._lab.write_property("params.maze_configs.seed", str(self.maze_seed))
            self._lab.write_property("params.maze_configs.texture", self.maze_texture)
            self._lab.write_property("params.maze_configs.decal_freq", str(self.maze_decal_freq))
            self._lab.write_property("params.maze_configs.map_txt", self.maze_map_txt)

        """ Navigation configurations"""
        """
            start amd goal positions will be updated if configs['start_pos'] and configs['goal_pos'] are not NONE.
        """
        # send initial position
        if configs['start_pos']:
            self.start_pos = configs['start_pos'] if configs['start_pos'] else self.start_pos
            self.start_pos = self.position_map2maze(self.start_pos, self.maze_size)
            self._lab.write_property("params.start_pos.x", str(self.start_pos[0]))
            self._lab.write_property("params.start_pos.y", str(self.start_pos[1]))
            self._lab.write_property("params.start_pos.yaw", str(self.start_pos[2]))
        # send target position
        if configs['goal_pos']:
            self.goal_pos = configs['goal_pos'] if configs['goal_pos'] else self.goal_pos
            self.goal_pos = self.position_map2maze(self.goal_pos, self.maze_size)
            self._lab.write_property("params.goal_pos.x", str(self.goal_pos[0]))
            self._lab.write_property("params.goal_pos.y", str(self.goal_pos[1]))
            self._lab.write_property("params.goal_pos.yaw", str(self.goal_pos[2]))
            # send the view position
            self._lab.write_property("params.view_pos.x", str(self.goal_pos[0]))
            self._lab.write_property("params.view_pos.y", str(self.goal_pos[1]))
            self._lab.write_property("params.view_pos.z", str(31.125))
            self._lab.write_property("params.view_pos.yaw", str(self.goal_pos[2]))

        """ update the environment """
        if configs['update']:
            self._lab.reset(episode=0)
        else:
            self._lab.reset()

        """ initialize the 3D maze"""
        # initialize the current state
        self._current_state = self._lab.observations()
        # initialize the current observations
        # By default, the observation for current state is the first observation
        self._last_observation = self._current_state[self._observation_names[0]]
        # initialize the top down view
        self._top_down_obs = self._current_state['RGB.LOOK_TOP_DOWN_VIEW']
        # initialize the goal observations
        self._goal_observation = self.get_random_observations(self.goal_pos)
        # initialize the positions and orientations
        self._trans = self._current_state['DEBUG.POS.TRANS']
        self._rots = self._current_state['DEBUG.POS.ROT']
        # initialize the distance
        self._last_distance = np.inf

        return self._last_observation, self._goal_observation, self._trans, self._rots

    # step function
    def step(self, action):
        """ step #(num_steps) in Deepmind Lab"""
        # take one step in the environment
        self._lab.step(ACTION_LIST[action], num_steps=4)

        """ check the terminal and return observations"""
        if self._lab.is_running():  # If the maze is still running
            # get the next state
            self._current_state = self._lab.observations()
            # get the next observations
            self._last_observation = self._current_state[self._observation_names[0]]
            # get the next top down observations
            self._top_down_obs = self._current_state['RGB.LOOK_TOP_DOWN_VIEW']
            # get the next position
            pos_x, pos_y, pos_z = self._current_state['DEBUG.POS.TRANS'].tolist()
            # get the next orientations
            pos_pitch, pos_yaw, pos_roll = self._current_state['DEBUG.POS.ROT'].tolist()
            # check if the agent reaches the goal given the current position and orientation
            terminal, dist = self.reach_goal([pos_x, pos_y, pos_yaw])
            # update the current distance between the agent and the goal
            self._last_distance = dist
            # update the rewards
            reward = self.compute_reward(dist)
            # update the positions and rotations
            self._trans = [pos_x, pos_y, pos_z]
            self._rots = [pos_pitch, pos_yaw, pos_roll]
        else:
            # set the terminal reward
            reward = 0.0
            # set the terminal flag
            terminal = True
            # set the current observation
            self._last_observation = np.copy(self._last_observation)
            # set the top down observation
            self._top_down_obs = np.copy(self._top_down_obs)
            # set the position and orientations
            self._trans = np.copy(self._trans)
            self._rots = np.copy(self._rots)
            # set the distance
            self._last_distance = self._last_distance

        return self._last_observation, reward, terminal, self._last_distance, self._trans, self._rots, dict()

    # render function
    def render(self, mode='rgb_array', close=False):
        if mode == 'rgb_array':
            return self._lab.observations()
        else:
            super(RandomMazeV1, self).render(mode=mode)  # just raise an exception

    # close the deepmind
    def close(self):
        self._lab.close()

    # seed function
    def seed(self, seed=None):
        self._lab.reset(seed=seed)

    # obtain goal image
    def get_random_observations(self, pos):
        """
        Function is used to get the observations at any position with any orientation.
        :param pos: List contains [x, y, ori]
        :return: List of egocentric observations at position (x, y)
        """
        # store observations
        ego_observations = []
        # re-arrange the observations of the agent
        ori_idx = list(self.orientations).index(pos[2])
        re_arranged_ori_indices = list(range(ori_idx, 8, 1)) + list(range(0, ori_idx, 1))
        angles = self.orientations[re_arranged_ori_indices]
        # obtain the egocentric observations
        self._lab.write_property("params.view_pos.x", str(pos[0] + 1))
        self._lab.write_property("params.view_pos.y", str(pos[1] + 1))
        for a in angles:
            self._lab.write_property("params.view_pos.yaw", str(a))
            ego_observations.append(self._lab.observations()['RGB.LOOK_RANDOM_VIEW'])
        return np.array(ego_observations, dtype=np.uint8)

    def reach_goal(self, current_pos):
        # compute the distance and angle error
        dist = self.compute_distance(current_pos, self.goal_pos)
        if dist < self.dist_epsilon:
            return 1, dist
        else:
            return 0, dist

    def compute_reward(self, dist):
        if self.reward_type == 'sparse-0':
            reward = 1 if dist < self.dist_epsilon else 0
        elif self.reward_type == 'sparse-1':
            reward = 0 if dist < self.dist_epsilon else -1
        elif self.reward_type == 'dense-euclidean':
            reward = dist
        else:
            raise Exception("Invalid reward type, Desired one in sparse-0, sparse-1, dense-euclidean but get {}"
                            .format(self.reward_type))
        return reward

    # show the panoramic view
    def show_panorama_view(self, time_step=None, obs_type='agent'):
        assert len(self._last_observation.shape) == 4, f"Invalid observation, expected observation should be " \
                                                       f"RGB.LOOK_PANORAMA_VIEW, but get {self._observation_names[0]}." \
                                                       f" Please check the observation list. The first one should be " \
                                                       f"RGB.LOOK_PANORAMA_VIEW."
        # set the observation: agent view or goal view
        if obs_type == 'agent':
            observations = self._last_observation
        else:
            observations = self._goal_observation
        # init or update data
        if time_step is None:
            self.fig, self.arrays = plt.subplots(3, 3)
            self.img_artists.append(self.arrays[0, 1].imshow(observations[0]))
            self.img_artists.append(self.arrays[0, 0].imshow(observations[1]))
            self.img_artists.append(self.arrays[1, 0].imshow(observations[2]))
            self.img_artists.append(self.arrays[1, 1].imshow(ndimage.rotate(self._top_down_obs, -90)))
            self.img_artists.append(self.arrays[2, 0].imshow(observations[3]))
            self.img_artists.append(self.arrays[2, 1].imshow(observations[4]))
            self.img_artists.append(self.arrays[2, 2].imshow(observations[5]))
            self.img_artists.append(self.arrays[1, 2].imshow(observations[6]))
            self.img_artists.append(self.arrays[0, 2].imshow(observations[7]))
        else:
            self.img_artists[0].set_data(observations[0])
            self.img_artists[1].set_data(observations[1])
            self.img_artists[2].set_data(observations[2])
            self.img_artists[3].set_data(ndimage.rotate(self._top_down_obs, -90))
            self.img_artists[4].set_data(observations[3])
            self.img_artists[5].set_data(observations[4])
            self.img_artists[6].set_data(observations[5])
            self.img_artists[7].set_data(observations[6])
            self.img_artists[8].set_data(observations[7])
        self.fig.canvas.draw()
        plt.pause(0.0001)

    # show the front view
    def show_front_view(self, time_step=None):
        assert len(self._last_observation.shape) == 3, f"Invalid observation, expected observation should be " \
                                                       f"RGBD_INTERLEAVED, but get {self._observation_names[0]}." \
                                                       f" Please check the observation list. The first one should be" \
                                                       f" RGBD_INTERLEAVED."
        # init or update data
        if time_step is None:
            self.fig, self.arrays = plt.subplots(1, 2)
            self.img_artists.append(self.arrays[0].imshow(self._last_observation))
            self.img_artists.append(self.arrays[1].imshow(ndimage.rotate(self._top_down_obs, -90)))
        else:
            self.img_artists[0].set_data(self._last_observation)
            self.img_artists[1].set_data(ndimage.rotate(self._top_down_obs, -90))
        self.fig.canvas.draw()
        plt.pause(0.0001)

    @staticmethod
    def compute_distance(pos_1, pos_2):
        return np.sqrt((pos_1[0] - pos_2[0])**2 + (pos_1[1] - pos_2[1])**2)

    @staticmethod
    def position_map2maze(pos, size):
        # convert the positions on the map to the positions in the 3D maze.
        # Note: the relation between the positions on the 2D map and the 3D maze is as follows:
        #      2D map: row, col
        #      3D maze: (y + 1 - 1) * 100 + 50, (maze_size - x) * 100 + 50
        return [pos[1] * 100 + 50, (size[1] - pos[0] - 1) * 100 + 50, pos[2]]


import os
import pandas as pd
from torchdrug import data
from torchdrug.core import Registry as R
import torch
from torch.utils import data as torch_data
from rdkit import Chem

from tqdm import tqdm
import logging
import warnings
from sklearn.utils import shuffle
logger = logging.getLogger(__name__)

@R.register("datasets.ScopeSuperFamilyClassify")
class ScopeSuperFamilyClassify(data.ProteinDataset):
    def __init__(self, params_db, verbose=1, **kwargs):
        self.path = params_db['path']
        self.dataset_file = params_db['dataset_file']
        self.pdb_dir = params_db['pdb_dir']
        self.processed_file = params_db['processed_file']
        self.split_ratio = params_db['split_ratio']

        self.splits = ["train", "valid", "test"]
        self.target_fields = ["superfamily_label"]  # label column

        self.df = pd.read_csv(os.path.join(self.path, self.dataset_file))
        self.df = shuffle(self.df, random_state=params_db['seed'])  # shuffle dataset
        pkl_file = os.path.join(self.path, self.processed_file)

        if os.path.exists(pkl_file):
            # load processed pkl
            self.load_pickle(pkl_file, verbose=verbose, **kwargs)
        else:
            pdb_files = self.df['id']
            pdb_files = pdb_files.apply(lambda x: os.path.join(self.path, self.pdb_dir, x + '.ent')).tolist()
            self.load_pdbs_drop(pdb_files=pdb_files, verbose=1, **kwargs)
            self.save_pickle(pkl_file, verbose=verbose)

        len = self.df['id'].size
        train_size = int(len * self.split_ratio[0])
        valid_size = int(len * self.split_ratio[1])
        test_size = len - train_size - valid_size
        self.num_samples = [train_size, valid_size, test_size]
        self.targets = {'superfamily_label': torch.tensor(self.df['label'].tolist())}

    def split(self, keys=None):
        keys = keys or self.splits
        offset = 0
        splits = []
        for split_name, num_sample in zip(self.splits, self.num_samples):
            if split_name in keys:
                split = torch_data.Subset(self, range(offset, offset + num_sample))
                splits.append(split)
            offset += num_sample
        return splits

    def get_item(self, index):
        protein = self.data[index].clone()
        if hasattr(protein, "residue_feature"):
            with protein.residue():
                protein.residue_feature = protein.residue_feature.to_dense()
        item = {"graph": protein,
                "superfamily_label": self.targets["superfamily_label"][index],
                "csv_idx": index}
        if self.transform:
            item = self.transform(item)
        return item

    def load_pdbs_drop(self, pdb_files, transform=None, verbose=0):
        self.data = []
        self.pdb_files = []
        self.sequences = []
        self.transform = transform

        drop_ids = []
        drop_pdbs = []
        if verbose:
            pdb_files = tqdm(pdb_files, "Constructing proteins from pdbs")
        for i, pdb_file in enumerate(pdb_files):
            mol = Chem.MolFromPDBFile(pdb_file)
            if not mol:
                logger.debug("Can't construct molecule from pdb file `%s`. Drop this sample." % pdb_file)
                drop_ids.append(i)
                drop_pdbs.append(pdb_file)
                continue
            protein = data.Protein.from_molecule(mol)
            if not protein:
                logger.debug("Can't construct protein from pdb file `%s`. Drop this sample." % pdb_file)
                drop_ids.append(i)
                drop_pdbs.append(pdb_file)
                continue

            if hasattr(protein, "residue_feature"):
                with protein.residue():
                    protein.residue_feature = protein.residue_feature.to_sparse()
            self.data.append(protein)
            self.pdb_files.append(pdb_file)
            self.sequences.append(protein.to_sequence() if protein else None)
        # after drop loop, need to reset the idx
        print(f'Drop ids {drop_ids}, they are {drop_pdbs}.')
        self.df = self.df.drop(self.df.index[drop_ids])
        self.df.reset_index(drop=True)
        self.df.to_csv(os.path.join(self.path, self.dataset_file), index=None)


from typing import Any, Dict

from django.contrib.auth.models import AbstractUser
from django.db import transaction

from rest_framework import serializers

from baserow.contrib.builder.api.pages.serializers import PageSerializer
from baserow.contrib.builder.pages.handler import PageHandler
from baserow.contrib.builder.pages.models import DuplicatePageJob
from baserow.contrib.builder.pages.operations import DuplicatePageOperationType
from baserow.contrib.builder.pages.service import PageService
from baserow.core.handler import CoreHandler
from baserow.core.jobs.registries import JobType

class DuplicatePageJobType(JobType):
    type = "duplicate_page"
    model_class = DuplicatePageJob
    max_count = 1

    request_serializer_field_names = ["page_id"]

    request_serializer_field_overrides = {
        "page_id": serializers.IntegerField(
            help_text="The ID of the page to duplicate.",
        ),
    }

    serializer_field_names = ["original_page", "duplicated_page"]
    serializer_field_overrides = {
        "original_page": PageSerializer(read_only=True),
        "duplicated_page": PageSerializer(read_only=True),
    }

    def transaction_atomic_context(self, job: "DuplicatePageJobType"):
        return transaction.atomic()

    def prepare_values(
        self, values: Dict[str, Any], user: AbstractUser
    ) -> Dict[str, Any]:
        page = PageHandler().get_page(values.pop("page_id"))

        CoreHandler().check_permissions(
            user,
            DuplicatePageOperationType.type,
            workspace=page.builder.workspace,
            context=page,
        )

        return {"original_page": page}

    def run(self, job, progress):
        new_page_clone = PageService().duplicate_page(
            job.user,
            job.original_page,
            progress_builder=progress.create_child_builder(
                represents_progress=progress.total
            ),
        )

        job.duplicated_page = new_page_clone
        job.save(update_fields=("duplicated_page",))

        return new_page_clone


from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('expenses/', views.expenses_list, name='expenses_list'),
    path('new/', views.expense_new, name ='expense_new'),
    path('expense/<int:pk>/', views.expense_show, name='expense_show'),
    path('edit/<int:pk>/', views.expense_edit, name='expense_edit'),
    path('delete/<int:pk>/', views.expense_delete, name='expense_delete'),
    path('new_bal/', views.balance_new, name ='balance_new'),
    path('new_pm/', views.payment_method_new, name ='payment_method_new'),
    path('new_ct/', views.category_new, name ='category_new'),
    path('api/balances/', views.BalancesAPI.as_view(), name = 'balances_api'),
    path('api/pm/', views.PMAPI.as_view(), name = 'pm_api'),
    path('api/profiles/', views.ProfileAPI.as_view(), name = 'profiles_api'),
    path('api/profiles/new/', views.CreateProfileAPI.as_view(), name='new_profile_api'),
    path('api/users/', views.UserAPI.as_view(), name='users-api'),
]


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

# In[2]:

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')

# In[3]:

data=pd.read_csv('agaricus-lepiota.data',names=range(0,23),header=0)

# In[4]:

data

# In[5]:

data.head()

# In[6]:

data.tail()

# In[7]:

data.shape

# In[8]:

data.columns

# In[9]:

encoded_data = pd.get_dummies(data)
encoded_data.head(5)

# In[10]:

data.describe()

# In[11]:

data.isnull().sum()

# In[12]:

sns.heatmap(data.isnull())

# In[13]:

df=pd.DataFrame(data)

# In[14]:

df

# In[15]:

x=data.iloc[:,1:]

# In[16]:

x

# In[17]:

x.shape

# In[18]:

y=data.iloc[:,0].values

# In[19]:

y

# In[20]:

from sklearn.preprocessing import LabelEncoder

# In[21]:

le=LabelEncoder()
y=le.fit_transform(y)
y

# In[22]:

le=LabelEncoder()
x=le.fit_transform(y)
x

# In[23]:

y

# In[24]:

y=y.reshape(-1,1)

# In[25]:

from sklearn.model_selection import train_test_split,cross_val_score

# In[26]:

x=x.reshape(-1,1)

# In[27]:

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=.22,random_state=45)

# In[28]:

print(x_train.shape,x_test.shape)

# In[29]:

print(y_train.shape,y_test.shape)

# In[30]:

y_train

# In[31]:

y_test

# In[32]:

x_train

# In[33]:

x_test

# In[34]:

from sklearn.neighbors import KNeighborsClassifier

from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import cross_val_predict
from sklearn.naive_bayes import MultinomialNB
from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression

# In[35]:

lg=LogisticRegression()
lg.fit(x_train,y_train)

# In[36]:

pred=lg.predict(x_test)
print(pred)

# In[37]:

print('accuracy_score:',accuracy_score(y_test,pred))

# In[38]:

y_pred_prob=lg.predict_proba(x_test)[:,1]

# In[39]:

y_pred_prob

# In[40]:

from sklearn.metrics import confusion_matrix

mnb=MultinomialNB()

score=cross_val_score(mnb,x,y,cv=5)
print('score:',score)

print('Mean Scores',score.mean())
print('standard deviation',score.std())
y_pred = cross_val_predict(mnb,x,y,cv=5)

print('y prediction values')
print(y_pred)

conf_mat=confusion_matrix(y,y_pred)
conf_mat

# In[41]:

print(confusion_matrix(y_test,pred))

# In[42]:

sv=SVC()
score=cross_val_score(sv,x,y,cv=5,scoring='accuracy')
print('accuracy score=',score)

print('Mean Scores',score.mean())
print('standard deviation',score.std())
y_pred = cross_val_predict(sv,x,y,cv=5)

print('y prediction values')
print(y_pred)

conf_mat=confusion_matrix(y,y_pred)
conf_mat

from sklearn.metrics import accuracy_score
accuracy_score(y,y_pred)

# In[43]:

dtc=DecisionTreeClassifier(criterion='gini')
score=cross_val_score(dtc,x,y,cv=5,scoring='f1_macro')
print('F1-score:',score)
print('Mean Scores',score.mean())
print('standard deviation',score.std())
y_pred = cross_val_predict(dtc,x,y,cv=5)

print('y prediction values')
print(y_pred)

conf_mat=confusion_matrix(y,y_pred)
conf_mat

# In[44]:

from sklearn.neighbors import KNeighborsClassifier

# In[45]:

knn=KNeighborsClassifier()
score=cross_val_score(knn,x,y,cv=10,scoring='accuracy')
print('Accuracy score:',score)
print('Mean Scores',score.mean())
print('standard deviation',score.std())
y_pred = cross_val_predict(knn,x,y,cv=5)

print('y prediction values')
print(y_pred)

print('y_pred.shape',y_pred.shape)

conf_mat=confusion_matrix(y,y_pred)
conf_mat

# In[46]:

def svmkernel(ker):
    svc=SVC(kernel=ker)

    score=cross_val_score(svc,x,y,cv=5)
    print('Mean Scores',score.mean())
    print('standard deviation',score.std())
    y_pred = cross_val_predict(svc,x,y,cv=5)

    conf_mat=confusion_matrix(y,y_pred)
    print(conf_mat)

# In[47]:

svmkernel('rbf')

# In[48]:

svmkernel('poly')

# In[49]:

from sklearn.ensemble import RandomForestRegressor

# In[50]:

rf=RandomForestRegressor(n_estimators=200,random_state=45)
rf.fit(x_train,y_train)

# In[51]:

pred=rf.predict(x_test)
pred

# In[52]:

from sklearn.ensemble import AdaBoostRegressor
model= AdaBoostRegressor()
model.fit(x_train,y_train)
print(model.score(x_train,y_train))
abpred=model.predict(x_test)
print(abpred)
model.score(x_test,y_test)

# In[53]:

from sklearn.externals import joblib
joblib.dump(abpred,'abpredsave.obj')

# In[ ]:


import os
from logging import getLogger
from pathlib import Path

import yawning_titan  # noqa - To use the Yawning-Titan logging config

_LOGGER = getLogger(__name__)

def _clean_up_pre_v2_0_0():
    """
    Performs the clean-up for Yawning-Titan installations before v2.0.0.

    Files that are deleted:

    - ~/yawning_titan/notebooks/Create a Network.ipynb
    - ~/yawning_titan/notebooks/Creating and playing as a Keyboard Agent.ipynb
    - ~/yawning_titan/notebooks/Using the Network DB.ipynb
    - ~/yawning_titan/notebooks/Using YawningTitanRun.ipynb
    - ~/yawning_titan/notebooks/sb3/End to End Generic Env Example - Env Creation, Agent Train and Agent Rendering.ipynb
    - ~/yawning_titan/notebooks/sb3/Using an Evaluation Callback to monitor progress during training.ipynb

    Directories that are deleted are:

    - ~/yawning_titan/notebooks/sb3/
    """
    # Delete old default notebooks
    old_notebooks = [
        "Create a Network.ipynb",
        "Creating and playing as a Keyboard Agent.ipynb",
        "Using the Network DB.ipynb",
        "Using YawningTitanRun.ipynb",
        "sb3/End to End Generic Env Example - Env Creation, Agent Train and Agent Rendering.ipynb",
        "sb3/Using an Evaluation Callback to monitor progress during training.ipynb",
    ]

    user_notebooks_dir = Path.home() / "yawning_titan" / "notebooks"

    for nb_file in old_notebooks:
        nb_path = user_notebooks_dir / nb_file
        if nb_path.is_file():
            os.remove(nb_path)
            _LOGGER.info(f"Deleted default notebook: {nb_path}")

    sb3_dir = user_notebooks_dir / "sb3"
    if sb3_dir.is_dir():
        if not os.listdir(sb3_dir):
            sb3_dir.rmdir()
            _LOGGER.info(f"Deleted default notebook directory: {sb3_dir}")

def run():
    """Perform the full clean-up."""
    _clean_up_pre_v2_0_0()

if __name__ == "__main__":
    run()