"""Tool for sorting imports alphabetically, and automatically separated into sections."""
import argparse
import functools
import json
import os
import sys
from gettext import gettext as _
from io import TextIOWrapper
from pathlib import Path
from typing import Any, Dict, List, Optional, Sequence, Union
from warnings import warn

from . import __version__, api, files, sections
from .exceptions import FileSkipped, ISortError, UnsupportedEncoding
from .format import create_terminal_printer
from .logo import ASCII_ART
from .profiles import profiles
from .settings import VALID_PY_TARGETS, Config, find_all_configs
from .utils import Trie
from .wrap_modes import WrapModes

DEPRECATED_SINGLE_DASH_ARGS = {
    "-ac",
    "-af",
    "-ca",
    "-cs",
    "-df",
    "-ds",
    "-dt",
    "-fas",
    "-fass",
    "-ff",
    "-fgw",
    "-fss",
    "-lai",
    "-lbt",
    "-le",
    "-ls",
    "-nis",
    "-nlb",
    "-ot",
    "-rr",
    "-sd",
    "-sg",
    "-sl",
    "-sp",
    "-tc",
    "-wl",
    "-ws",
}
QUICK_GUIDE = f"""
{ASCII_ART}

Nothing to do: no files or paths have have been passed in!

Try one of the following:

    `isort .` - sort all Python files, starting from the current directory, recursively.
    `isort . --interactive` - Do the same, but ask before making any changes.
    `isort . --check --diff` - Check to see if imports are correctly sorted within this project.
    `isort --help` - In-depth information about isort's available command-line options.

Visit https://pycqa.github.io/isort/ for complete information about how to use isort.
"""

class SortAttempt:
    def __init__(self, incorrectly_sorted: bool, skipped: bool, supported_encoding: bool) -> None:
        self.incorrectly_sorted = incorrectly_sorted
        self.skipped = skipped
        self.supported_encoding = supported_encoding

def sort_imports(
    file_name: str,
    config: Config,
    check: bool = False,
    ask_to_apply: bool = False,
    write_to_stdout: bool = False,
    **kwargs: Any,
) -> Optional[SortAttempt]:
    incorrectly_sorted: bool = False
    skipped: bool = False
    try:
        if check:
            try:
                incorrectly_sorted = not api.check_file(file_name, config=config, **kwargs)
            except FileSkipped:
                skipped = True
            return SortAttempt(incorrectly_sorted, skipped, True)

        try:
            incorrectly_sorted = not api.sort_file(
                file_name,
                config=config,
                ask_to_apply=ask_to_apply,
                write_to_stdout=write_to_stdout,
                **kwargs,
            )
        except FileSkipped:
            skipped = True
        return SortAttempt(incorrectly_sorted, skipped, True)
    except (OSError, ValueError) as error:
        warn(f"Unable to parse file {file_name} due to {error}")
        return None
    except UnsupportedEncoding:
        if config.verbose:
            warn(f"Encoding not supported for {file_name}")
        return SortAttempt(incorrectly_sorted, skipped, False)
    except ISortError as error:
        _print_hard_fail(config, message=str(error))
        sys.exit(1)
    except Exception:
        _print_hard_fail(config, offending_file=file_name)
        raise

def _print_hard_fail(
    config: Config, offending_file: Optional[str] = None, message: Optional[str] = None
) -> None:
    """Fail on unrecoverable exception with custom message."""
    message = message or (
        f"Unrecoverable exception thrown when parsing {offending_file or ''}! "
        "This should NEVER happen.\n"
        "If encountered, please open an issue: https://github.com/PyCQA/isort/issues/new"
    )
    printer = create_terminal_printer(
        color=config.color_output, error=config.format_error, success=config.format_success
    )
    printer.error(message)

def _build_arg_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(
        description="Sort Python import definitions alphabetically "
        "within logical sections. Run with no arguments to see a quick "
        "start guide, otherwise, one or more files/directories/stdin must be provided. "
        "Use `-` as the first argument to represent stdin. Use --interactive to use the pre 5.0.0 "
        "interactive behavior."
        " "
        "If you've used isort 4 but are new to isort 5, see the upgrading guide: "
        "https://pycqa.github.io/isort/docs/upgrade_guides/5.0.0.html",
        add_help=False,  # prevent help option from appearing in "optional arguments" group
    )

    general_group = parser.add_argument_group("general options")
    target_group = parser.add_argument_group("target options")
    output_group = parser.add_argument_group("general output options")
    inline_args_group = output_group.add_mutually_exclusive_group()
    section_group = parser.add_argument_group("section output options")
    deprecated_group = parser.add_argument_group("deprecated options")

    general_group.add_argument(
        "-h",
        "--help",
        action="help",
        default=argparse.SUPPRESS,
        help=_("show this help message and exit"),
    )
    general_group.add_argument(
        "-V",
        "--version",
        action="store_true",
        dest="show_version",
        help="Displays the currently installed version of isort.",
    )
    general_group.add_argument(
        "--vn",
        "--version-number",
        action="version",
        version=__version__,
        help="Returns just the current version number without the logo",
    )
    general_group.add_argument(
        "-v",
        "--verbose",
        action="store_true",
        dest="verbose",
        help="Shows verbose output, such as when files are skipped or when a check is successful.",
    )
    general_group.add_argument(
        "--only-modified",
        "--om",
        dest="only_modified",
        action="store_true",
        help="Suppresses verbose output for non-modified files.",
    )
    general_group.add_argument(
        "--dedup-headings",
        dest="dedup_headings",
        action="store_true",
        help="Tells isort to only show an identical custom import heading comment once, even if"
        " there are multiple sections with the comment set.",
    )
    general_group.add_argument(
        "-q",
        "--quiet",
        action="store_true",
        dest="quiet",
        help="Shows extra quiet output, only errors are outputted.",
    )
    general_group.add_argument(
        "-d",
        "--stdout",
        help="Force resulting output to stdout, instead of in-place.",
        dest="write_to_stdout",
        action="store_true",
    )
    general_group.add_argument(
        "--overwrite-in-place",
        help="Tells isort to overwrite in place using the same file handle. "
        "Comes at a performance and memory usage penalty over its standard "
        "approach but ensures all file flags and modes stay unchanged.",
        dest="overwrite_in_place",
        action="store_true",
    )
    general_group.add_argument(
        "--show-config",
        dest="show_config",
        action="store_true",
        help="See isort's determined config, as well as sources of config options.",
    )
    general_group.add_argument(
        "--show-files",
        dest="show_files",
        action="store_true",
        help="See the files isort will be run against with the current config options.",
    )
    general_group.add_argument(
        "--df",
        "--diff",
        dest="show_diff",
        action="store_true",
        help="Prints a diff of all the changes isort would make to a file, instead of "
        "changing it in place",
    )
    general_group.add_argument(
        "-c",
        "--check-only",
        "--check",
        action="store_true",
        dest="check",
        help="Checks the file for unsorted / unformatted imports and prints them to the "
        "command line without modifying the file. Returns 0 when nothing would change and "
        "returns 1 when the file would be reformatted.",
    )
    general_group.add_argument(
        "--ws",
        "--ignore-whitespace",
        action="store_true",
        dest="ignore_whitespace",
        help="Tells isort to ignore whitespace differences when --check-only is being used.",
    )
    general_group.add_argument(
        "--sp",
        "--settings-path",
        "--settings-file",
        "--settings",
        dest="settings_path",
        help="Explicitly set the settings path or file instead of auto determining "
        "based on file location.",
    )
    general_group.add_argument(
        "--cr",
        "--config-root",
        dest="config_root",
        help="Explicitly set the config root for resolving all configs. When used "
        "with the --resolve-all-configs flag, isort will look at all sub-folders "
        "in this config root to resolve config files and sort files based on the "
        "closest available config(if any)",
    )
    general_group.add_argument(
        "--resolve-all-configs",
        dest="resolve_all_configs",
        action="store_true",
        help="Tells isort to resolve the configs for all sub-directories "
        "and sort files in terms of its closest config files.",
    )
    general_group.add_argument(
        "--profile",
        dest="profile",
        type=str,
        help="Base profile type to use for configuration. "
        f"Profiles include: {', '.join(profiles.keys())}. As well as any shared profiles.",
    )
    general_group.add_argument(
        "--old-finders",
        "--magic-placement",
        dest="old_finders",
        action="store_true",
        help="Use the old deprecated finder logic that relies on environment introspection magic.",
    )
    general_group.add_argument(
        "-j",
        "--jobs",
        help="Number of files to process in parallel. Negative value means use number of CPUs.",
        dest="jobs",
        type=int,
        nargs="?",
        const=-1,
    )
    general_group.add_argument(
        "--ac",
        "--atomic",
        dest="atomic",
        action="store_true",
        help="Ensures the output doesn't save if the resulting file contains syntax errors.",
    )
    general_group.add_argument(
        "--interactive",
        dest="ask_to_apply",
        action="store_true",
        help="Tells isort to apply changes interactively.",
    )
    general_group.add_argument(
        "--format-error",
        dest="format_error",
        help="Override the format used to print errors.",
    )
    general_group.add_argument(
        "--format-success",
        dest="format_success",
        help="Override the format used to print success.",
    )
    general_group.add_argument(
        "--srx",
        "--sort-reexports",
        dest="sort_reexports",
        action="store_true",
        help="Automatically sort all re-exports (module level __all__ collections)",
    )

    target_group.add_argument(
        "files", nargs="*", help="One or more Python source files that need their imports sorted."
    )
    target_group.add_argument(
        "--filter-files",
        dest="filter_files",
        action="store_true",
        help="Tells isort to filter files even when they are explicitly passed in as "
        "part of the CLI command.",
    )
    target_group.add_argument(
        "-s",
        "--skip",
        help="Files that isort should skip over. If you want to skip multiple "
        "files you should specify twice: --skip file1 --skip file2. Values can be "
        "file names, directory names or file paths. To skip all files in a nested path "
        "use --skip-glob.",
        dest="skip",
        action="append",
    )
    target_group.add_argument(
        "--extend-skip",
        help="Extends --skip to add additional files that isort should skip over. "
        "If you want to skip multiple "
        "files you should specify twice: --skip file1 --skip file2. Values can be "
        "file names, directory names or file paths. To skip all files in a nested path "
        "use --skip-glob.",
        dest="extend_skip",
        action="append",
    )
    target_group.add_argument(
        "--sg",
        "--skip-glob",
        help="Files that isort should skip over.",
        dest="skip_glob",
        action="append",
    )
    target_group.add_argument(
        "--extend-skip-glob",
        help="Additional files that isort should skip over (extending --skip-glob).",
        dest="extend_skip_glob",
        action="append",
    )
    target_group.add_argument(
        "--gitignore",
        "--skip-gitignore",
        action="store_true",
        dest="skip_gitignore",
        help="Treat project as a git repository and ignore files listed in .gitignore."
        "\nNOTE: This requires git to be installed and accessible from the same shell as isort.",
    )
    target_group.add_argument(
        "--ext",
        "--extension",
        "--supported-extension",
        dest="supported_extensions",
        action="append",
        help="Specifies what extensions isort can be run against.",
    )
    target_group.add_argument(
        "--blocked-extension",
        dest="blocked_extensions",
        action="append",
        help="Specifies what extensions isort can never be run against.",
    )
    target_group.add_argument(
        "--dont-follow-links",
        dest="dont_follow_links",
        action="store_true",
        help="Tells isort not to follow symlinks that are encountered when running recursively.",
    )
    target_group.add_argument(
        "--filename",
        dest="filename",
        help="Provide the filename associated with a stream.",
    )
    target_group.add_argument(
        "--allow-root",
        action="store_true",
        default=False,
        help="Tells isort not to treat / specially, allowing it to be run against the root dir.",
    )

    output_group.add_argument(
        "-a",
        "--add-import",
        dest="add_imports",
        action="append",
        help="Adds the specified import line to all files, "
        "automatically determining correct placement.",
    )
    output_group.add_argument(
        "--append",
        "--append-only",
        dest="append_only",
        action="store_true",
        help="Only adds the imports specified in --add-import if the file"
        " contains existing imports.",
    )
    output_group.add_argument(
        "--af",
        "--force-adds",
        dest="force_adds",
        action="store_true",
        help="Forces import adds even if the original file is empty.",
    )
    output_group.add_argument(
        "--rm",
        "--remove-import",
        dest="remove_imports",
        action="append",
        help="Removes the specified import from all files.",
    )
    output_group.add_argument(
        "--float-to-top",
        dest="float_to_top",
        action="store_true",
        help="Causes all non-indented imports to float to the top of the file having its imports "
        "sorted (immediately below the top of file comment).\n"
        "This can be an excellent shortcut for collecting imports every once in a while "
        "when you place them in the middle of a file to avoid context switching.\n\n"
        "*NOTE*: It currently doesn't work with cimports and introduces some extra over-head "
        "and a performance penalty.",
    )
    output_group.add_argument(
        "--dont-float-to-top",
        dest="dont_float_to_top",
        action="store_true",
        help="Forces --float-to-top setting off. See --float-to-top for more information.",
    )
    output_group.add_argument(
        "--ca",
        "--combine-as",
        dest="combine_as_imports",
        action="store_true",
        help="Combines as imports on the same line.",
    )
    output_group.add_argument(
        "--cs",
        "--combine-star",
        dest="combine_star",
        action="store_true",
        help="Ensures that if a star import is present, "
        "nothing else is imported from that namespace.",
    )
    output_group.add_argument(
        "-e",
        "--balanced",
        dest="balanced_wrapping",
        action="store_true",
        help="Balances wrapping to produce the most consistent line length possible",
    )
    output_group.add_argument(
        "--ff",
        "--from-first",
        dest="from_first",
        action="store_true",
        help="Switches the typical ordering preference, "
        "showing from imports first then straight ones.",
    )
    output_group.add_argument(
        "--fgw",
        "--force-grid-wrap",
        nargs="?",
        const=2,
        type=int,
        dest="force_grid_wrap",
        help="Force number of from imports (defaults to 2 when passed as CLI flag without value) "
        "to be grid wrapped regardless of line "
        "length. If 0 is passed in (the global default) only line length is considered.",
    )
    output_group.add_argument(
        "-i",
        "--indent",
        help='String to place for indents defaults to "    " (4 spaces).',
        dest="indent",
        type=str,
    )
    output_group.add_argument(
        "--lbi", "--lines-before-imports", dest="lines_before_imports", type=int
    )
    output_group.add_argument(
        "--lai", "--lines-after-imports", dest="lines_after_imports", type=int
    )
    output_group.add_argument(
        "--lbt", "--lines-between-types", dest="lines_between_types", type=int
    )
    output_group.add_argument(
        "--le",
        "--line-ending",
        dest="line_ending",
        help="Forces line endings to the specified value. "
        "If not set, values will be guessed per-file.",
    )
    output_group.add_argument(
        "--ls",
        "--length-sort",
        help="Sort imports by their string length.",
        dest="length_sort",
        action="store_true",
    )
    output_group.add_argument(
        "--lss",
        "--length-sort-straight",
        help="Sort straight imports by their string length. Similar to `length_sort` "
        "but applies only to straight imports and doesn't affect from imports.",
        dest="length_sort_straight",
        action="store_true",
    )
    output_group.add_argument(
        "-m",
        "--multi-line",
        dest="multi_line_output",
        choices=list(WrapModes.__members__.keys())
        + [str(mode.value) for mode in WrapModes.__members__.values()],
        type=str,
        help="Multi line output (0-grid, 1-vertical, 2-hanging, 3-vert-hanging, 4-vert-grid, "
        "5-vert-grid-grouped, 6-deprecated-alias-for-5, 7-noqa, "
        "8-vertical-hanging-indent-bracket, 9-vertical-prefix-from-module-import, "
        "10-hanging-indent-with-parentheses).",
    )
    output_group.add_argument(
        "-n",
        "--ensure-newline-before-comments",
        dest="ensure_newline_before_comments",
        action="store_true",
        help="Inserts a blank line before a comment following an import.",
    )
    inline_args_group.add_argument(
        "--nis",
        "--no-inline-sort",
        dest="no_inline_sort",
        action="store_true",
        help="Leaves `from` imports with multiple imports 'as-is' "
        "(e.g. `from foo import a, c ,b`).",
    )
    output_group.add_argument(
        "--ot",
        "--order-by-type",
        dest="order_by_type",
        action="store_true",
        help="Order imports by type, which is determined by case, in addition to alphabetically.\n"
        "\n**NOTE**: type here refers to the implied type from the import name capitalization.\n"
        ' isort does not do type introspection for the imports. These "types" are simply: '
        "CONSTANT_VARIABLE, CamelCaseClass, variable_or_function. If your project follows PEP8"
        " or a related coding standard and has many imports this is a good default, otherwise you "
        "likely will want to turn it off. From the CLI the `--dont-order-by-type` option will turn "
        "this off.",
    )
    output_group.add_argument(
        "--dt",
        "--dont-order-by-type",
        dest="dont_order_by_type",
        action="store_true",
        help="Don't order imports by type, which is determined by case, in addition to "
        "alphabetically.\n\n"
        "**NOTE**: type here refers to the implied type from the import name capitalization.\n"
        ' isort does not do type introspection for the imports. These "types" are simply: '
        "CONSTANT_VARIABLE, CamelCaseClass, variable_or_function. If your project follows PEP8"
        " or a related coding standard and has many imports this is a good default. You can turn "
        "this on from the CLI using `--order-by-type`.",
    )
    output_group.add_argument(
        "--rr",
        "--reverse-relative",
        dest="reverse_relative",
        action="store_true",
        help="Reverse order of relative imports.",
    )
    output_group.add_argument(
        "--reverse-sort",
        dest="reverse_sort",
        action="store_true",
        help="Reverses the ordering of imports.",
    )
    output_group.add_argument(
        "--sort-order",
        dest="sort_order",
        help="Specify sorting function. Can be built in (natural[default] = force numbers "
        "to be sequential, native = Python's built-in sorted function) or an installable plugin.",
    )
    inline_args_group.add_argument(
        "--sl",
        "--force-single-line-imports",
        dest="force_single_line",
        action="store_true",
        help="Forces all from imports to appear on their own line",
    )
    output_group.add_argument(
        "--nsl",
        "--single-line-exclusions",
        help="One or more modules to exclude from the single line rule.",
        dest="single_line_exclusions",
        action="append",
    )
    output_group.add_argument(
        "--tc",
        "--trailing-comma",
        dest="include_trailing_comma",
        action="store_true",
        help="Includes a trailing comma on multi line imports that include parentheses.",
    )
    output_group.add_argument(
        "--up",
        "--use-parentheses",
        dest="use_parentheses",
        action="store_true",
        help="Use parentheses for line continuation on length limit instead of slashes."
        " **NOTE**: This is separate from wrap modes, and only affects how individual lines that "
        " are too long get continued, not sections of multiple imports.",
    )
    output_group.add_argument(
        "-l",
        "-w",
        "--line-length",
        "--line-width",
        help="The max length of an import line (used for wrapping long imports).",
        dest="line_length",
        type=int,
    )
    output_group.add_argument(
        "--wl",
        "--wrap-length",
        dest="wrap_length",
        type=int,
        help="Specifies how long lines that are wrapped should be, if not set line_length is used."
        "\nNOTE: wrap_length must be LOWER than or equal to line_length.",
    )
    output_group.add_argument(
        "--case-sensitive",
        dest="case_sensitive",
        action="store_true",
        help="Tells isort to include casing when sorting module names",
    )
    output_group.add_argument(
        "--remove-redundant-aliases",
        dest="remove_redundant_aliases",
        action="store_true",
        help=(
            "Tells isort to remove redundant aliases from imports, such as `import os as os`."
            " This defaults to `False` simply because some projects use these seemingly useless "
            " aliases to signify intent and change behaviour."
        ),
    )
    output_group.add_argument(
        "--honor-noqa",
        dest="honor_noqa",
        action="store_true",
        help="Tells isort to honor noqa comments to enforce skipping those comments.",
    )
    output_group.add_argument(
        "--treat-comment-as-code",
        dest="treat_comments_as_code",
        action="append",
        help="Tells isort to treat the specified single line comment(s) as if they are code.",
    )
    output_group.add_argument(
        "--treat-all-comment-as-code",
        dest="treat_all_comments_as_code",
        action="store_true",
        help="Tells isort to treat all single line comments as if they are code.",
    )
    output_group.add_argument(
        "--formatter",
        dest="formatter",
        type=str,
        help="Specifies the name of a formatting plugin to use when producing output.",
    )
    output_group.add_argument(
        "--color",
        dest="color_output",
        action="store_true",
        help="Tells isort to use color in terminal output.",
    )
    output_group.add_argument(
        "--ext-format",
        dest="ext_format",
        help="Tells isort to format the given files according to an extensions formatting rules.",
    )
    output_group.add_argument(
        "--star-first",
        help="Forces star imports above others to avoid overriding directly imported variables.",
        dest="star_first",
        action="store_true",
    )
    output_group.add_argument(
        "--split-on-trailing-comma",
        help="Split imports list followed by a trailing comma into VERTICAL_HANGING_INDENT mode",
        dest="split_on_trailing_comma",
        action="store_true",
    )

    section_group.add_argument(
        "--sd",
        "--section-default",
        dest="default_section",
        help="Sets the default section for import options: " + str(sections.DEFAULT),
    )
    section_group.add_argument(
        "--only-sections",
        "--os",
        dest="only_sections",
        action="store_true",
        help="Causes imports to be sorted based on their sections like STDLIB, THIRDPARTY, etc. "
        "Within sections, the imports are ordered by their import style and the imports with "
        "the same style maintain their relative positions.",
    )
    section_group.add_argument(
        "--ds",
        "--no-sections",
        help="Put all imports into the same section bucket",
        dest="no_sections",
        action="store_true",
    )
    section_group.add_argument(
        "--fas",
        "--force-alphabetical-sort",
        action="store_true",
        dest="force_alphabetical_sort",
        help="Force all imports to be sorted as a single section",
    )
    section_group.add_argument(
        "--fss",
        "--force-sort-within-sections",
        action="store_true",
        dest="force_sort_within_sections",
        help="Don't sort straight-style imports (like import sys) before from-style imports "
        "(like from itertools import groupby). Instead, sort the imports by module, "
        "independent of import style.",
    )
    section_group.add_argument(
        "--hcss",
        "--honor-case-in-force-sorted-sections",
        action="store_true",
        dest="honor_case_in_force_sorted_sections",
        help="Honor `--case-sensitive` when `--force-sort-within-sections` is being used. "
        "Without this option set, `--order-by-type` decides module name ordering too.",
    )
    section_group.add_argument(
        "--srss",
        "--sort-relative-in-force-sorted-sections",
        action="store_true",
        dest="sort_relative_in_force_sorted_sections",
        help="When using `--force-sort-within-sections`, sort relative imports the same "
        "way as they are sorted when not using that setting.",
    )
    section_group.add_argument(
        "--fass",
        "--force-alphabetical-sort-within-sections",
        action="store_true",
        dest="force_alphabetical_sort_within_sections",
        help="Force all imports to be sorted alphabetically within a section",
    )
    section_group.add_argument(
        "-t",
        "--top",
        help="Force specific imports to the top of their appropriate section.",
        dest="force_to_top",
        action="append",
    )
    section_group.add_argument(
        "--combine-straight-imports",
        "--csi",
        dest="combine_straight_imports",
        action="store_true",
        help="Combines all the bare straight imports of the same section in a single line. "
        "Won't work with sections which have 'as' imports",
    )
    section_group.add_argument(
        "--nlb",
        "--no-lines-before",
        help="Sections which should not be split with previous by empty lines",
        dest="no_lines_before",
        action="append",
    )
    section_group.add_argument(
        "--src",
        "--src-path",
        dest="src_paths",
        action="append",
        help="Add an explicitly defined source path "
        "(modules within src paths have their imports automatically categorized as first_party)."
        " Glob expansion (`*` and `**`) is supported for this option.",
    )
    section_group.add_argument(
        "-b",
        "--builtin",
        dest="known_standard_library",
        action="append",
        help="Force isort to recognize a module as part of Python's standard library.",
    )
    section_group.add_argument(
        "--extra-builtin",
        dest="extra_standard_library",
        action="append",
        help="Extra modules to be included in the list of ones in Python's standard library.",
    )
    section_group.add_argument(
        "-f",
        "--future",
        dest="known_future_library",
        action="append",
        help="Force isort to recognize a module as part of Python's internal future compatibility "
        "libraries. WARNING: this overrides the behavior of __future__ handling and therefore"
        " can result in code that can't execute. If you're looking to add dependencies such "
        "as six, a better option is to create another section below --future using custom "
        "sections. See: https://github.com/PyCQA/isort#custom-sections-and-ordering and the "
        "discussion here: https://github.com/PyCQA/isort/issues/1463.",
    )
    section_group.add_argument(
        "-o",
        "--thirdparty",
        dest="known_third_party",
        action="append",
        help="Force isort to recognize a module as being part of a third party library.",
    )
    section_group.add_argument(
        "-p",
        "--project",
        dest="known_first_party",
        action="append",
        help="Force isort to recognize a module as being part of the current python project.",
    )
    section_group.add_argument(
        "--known-local-folder",
        dest="known_local_folder",
        action="append",
        help="Force isort to recognize a module as being a local folder. "
        "Generally, this is reserved for relative imports (from . import module).",
    )
    section_group.add_argument(
        "--virtual-env",
        dest="virtual_env",
        help="Virtual environment to use for determining whether a package is third-party",
    )
    section_group.add_argument(
        "--conda-env",
        dest="conda_env",
        help="Conda environment to use for determining whether a package is third-party",
    )
    section_group.add_argument(
        "--py",
        "--python-version",
        action="store",
        dest="py_version",
        choices=tuple(VALID_PY_TARGETS) + ("auto",),
        help="Tells isort to set the known standard library based on the specified Python "
        "version. Default is to assume any Python 3 version could be the target, and use a union "
        "of all stdlib modules across versions. If auto is specified, the version of the "
        "interpreter used to run isort "
        f"(currently: {sys.version_info.major}{sys.version_info.minor}) will be used.",
    )

    # deprecated options
    deprecated_group.add_argument(
        "--recursive",
        dest="deprecated_flags",
        action="append_const",
        const="--recursive",
        help=argparse.SUPPRESS,
    )
    deprecated_group.add_argument(
        "-rc", dest="deprecated_flags", action="append_const", const="-rc", help=argparse.SUPPRESS
    )
    deprecated_group.add_argument(
        "--dont-skip",
        dest="deprecated_flags",
        action="append_const",
        const="--dont-skip",
        help=argparse.SUPPRESS,
    )
    deprecated_group.add_argument(
        "-ns", dest="deprecated_flags", action="append_const", const="-ns", help=argparse.SUPPRESS
    )
    deprecated_group.add_argument(
        "--apply",
        dest="deprecated_flags",
        action="append_const",
        const="--apply",
        help=argparse.SUPPRESS,
    )
    deprecated_group.add_argument(
        "-k",
        "--keep-direct-and-as",
        dest="deprecated_flags",
        action="append_const",
        const="--keep-direct-and-as",
        help=argparse.SUPPRESS,
    )

    return parser

def parse_args(argv: Optional[Sequence[str]] = None) -> Dict[str, Any]:
    argv = sys.argv[1:] if argv is None else list(argv)
    remapped_deprecated_args = []
    for index, arg in enumerate(argv):
        if arg in DEPRECATED_SINGLE_DASH_ARGS:
            remapped_deprecated_args.append(arg)
            argv[index] = f"-{arg}"

    parser = _build_arg_parser()
    arguments = {key: value for key, value in vars(parser.parse_args(argv)).items() if value}
    if remapped_deprecated_args:
        arguments["remapped_deprecated_args"] = remapped_deprecated_args
    if "dont_order_by_type" in arguments:
        arguments["order_by_type"] = False
        del arguments["dont_order_by_type"]
    if "dont_follow_links" in arguments:
        arguments["follow_links"] = False
        del arguments["dont_follow_links"]
    if "dont_float_to_top" in arguments:
        del arguments["dont_float_to_top"]
        if arguments.get("float_to_top", False):
            sys.exit("Can't set both --float-to-top and --dont-float-to-top.")
        else:
            arguments["float_to_top"] = False
    multi_line_output = arguments.get("multi_line_output", None)
    if multi_line_output:
        if multi_line_output.isdigit():
            arguments["multi_line_output"] = WrapModes(int(multi_line_output))
        else:
            arguments["multi_line_output"] = WrapModes[multi_line_output]

    return arguments

def _preconvert(item: Any) -> Union[str, List[Any]]:
    """Preconverts objects from native types into JSONifyiable types"""
    if isinstance(item, (set, frozenset)):
        return list(item)
    if isinstance(item, WrapModes):
        return str(item.name)
    if isinstance(item, Path):
        return str(item)
    if callable(item) and hasattr(item, "__name__"):
        return str(item.__name__)
    raise TypeError(f"Unserializable object {item} of type {type(item)}")

def identify_imports_main(
    argv: Optional[Sequence[str]] = None, stdin: Optional[TextIOWrapper] = None
) -> None:
    parser = argparse.ArgumentParser(
        description="Get all import definitions from a given file."
        "Use `-` as the first argument to represent stdin."
    )
    parser.add_argument(
        "files", nargs="+", help="One or more Python source files that need their imports sorted."
    )
    parser.add_argument(
        "--top-only",
        action="store_true",
        default=False,
        help="Only identify imports that occur in before functions or classes.",
    )

    target_group = parser.add_argument_group("target options")
    target_group.add_argument(
        "--follow-links",
        action="store_true",
        default=False,
        help="Tells isort to follow symlinks that are encountered when running recursively.",
    )

    uniqueness = parser.add_mutually_exclusive_group()
    uniqueness.add_argument(
        "--unique",
        action="store_true",
        default=False,
        help="If true, isort will only identify unique imports.",
    )
    uniqueness.add_argument(
        "--packages",
        dest="unique",
        action="store_const",
        const=api.ImportKey.PACKAGE,
        default=False,
        help="If true, isort will only identify the unique top level modules imported.",
    )
    uniqueness.add_argument(
        "--modules",
        dest="unique",
        action="store_const",
        const=api.ImportKey.MODULE,
        default=False,
        help="If true, isort will only identify the unique modules imported.",
    )
    uniqueness.add_argument(
        "--attributes",
        dest="unique",
        action="store_const",
        const=api.ImportKey.ATTRIBUTE,
        default=False,
        help="If true, isort will only identify the unique attributes imported.",
    )

    arguments = parser.parse_args(argv)

    file_names = arguments.files
    if file_names == ["-"]:
        identified_imports = api.find_imports_in_stream(
            sys.stdin if stdin is None else stdin,
            unique=arguments.unique,
            top_only=arguments.top_only,
            follow_links=arguments.follow_links,
        )
    else:
        identified_imports = api.find_imports_in_paths(
            file_names,
            unique=arguments.unique,
            top_only=arguments.top_only,
            follow_links=arguments.follow_links,
        )

    for identified_import in identified_imports:
        if arguments.unique == api.ImportKey.PACKAGE:
            print(identified_import.module.split(".")[0])
        elif arguments.unique == api.ImportKey.MODULE:
            print(identified_import.module)
        elif arguments.unique == api.ImportKey.ATTRIBUTE:
            print(f"{identified_import.module}.{identified_import.attribute}")
        else:
            print(str(identified_import))

def main(argv: Optional[Sequence[str]] = None, stdin: Optional[TextIOWrapper] = None) -> None:
    arguments = parse_args(argv)
    if arguments.get("show_version"):
        print(ASCII_ART)
        return

    show_config: bool = arguments.pop("show_config", False)
    show_files: bool = arguments.pop("show_files", False)
    if show_config and show_files:
        sys.exit("Error: either specify show-config or show-files not both.")

    if "settings_path" in arguments:
        if os.path.isfile(arguments["settings_path"]):
            arguments["settings_file"] = os.path.abspath(arguments["settings_path"])
            arguments["settings_path"] = os.path.dirname(arguments["settings_file"])
        else:
            arguments["settings_path"] = os.path.abspath(arguments["settings_path"])

    if "virtual_env" in arguments:
        venv = arguments["virtual_env"]
        arguments["virtual_env"] = os.path.abspath(venv)
        if not os.path.isdir(arguments["virtual_env"]):
            warn(f"virtual_env dir does not exist: {arguments['virtual_env']}")

    file_names = arguments.pop("files", [])
    if not file_names and not show_config:
        print(QUICK_GUIDE)
        if arguments:
            sys.exit("Error: arguments passed in without any paths or content.")
        return
    if "settings_path" not in arguments:
        arguments["settings_path"] = (
            arguments.get("filename", None) or os.getcwd()
            if file_names == ["-"]
            else os.path.abspath(file_names[0] if file_names else ".")
        )
        if not os.path.isdir(arguments["settings_path"]):
            arguments["settings_path"] = os.path.dirname(arguments["settings_path"])

    config_dict = arguments.copy()
    ask_to_apply = config_dict.pop("ask_to_apply", False)
    jobs = config_dict.pop("jobs", None)
    check = config_dict.pop("check", False)
    show_diff = config_dict.pop("show_diff", False)
    write_to_stdout = config_dict.pop("write_to_stdout", False)
    deprecated_flags = config_dict.pop("deprecated_flags", False)
    remapped_deprecated_args = config_dict.pop("remapped_deprecated_args", False)
    stream_filename = config_dict.pop("filename", None)
    ext_format = config_dict.pop("ext_format", None)
    allow_root = config_dict.pop("allow_root", None)
    resolve_all_configs = config_dict.pop("resolve_all_configs", False)
    wrong_sorted_files = False
    all_attempt_broken = False
    no_valid_encodings = False

    config_trie: Optional[Trie] = None
    if resolve_all_configs:
        config_trie = find_all_configs(config_dict.pop("config_root", "."))

    if "src_paths" in config_dict:
        config_dict["src_paths"] = {
            Path(src_path).resolve() for src_path in config_dict.get("src_paths", ())
        }

    config = Config(**config_dict)
    if show_config:
        print(json.dumps(config.__dict__, indent=4, separators=(",", ": "), default=_preconvert))
        return
    if file_names == ["-"]:
        file_path = Path(stream_filename) if stream_filename else None
        if show_files:
            sys.exit("Error: can't show files for streaming input.")

        input_stream = sys.stdin if stdin is None else stdin
        if check:
            incorrectly_sorted = not api.check_stream(
                input_stream=input_stream,
                config=config,
                show_diff=show_diff,
                file_path=file_path,
                extension=ext_format,
            )

            wrong_sorted_files = incorrectly_sorted
        else:
            try:
                api.sort_stream(
                    input_stream=input_stream,
                    output_stream=sys.stdout,
                    config=config,
                    show_diff=show_diff,
                    file_path=file_path,
                    extension=ext_format,
                    raise_on_skip=False,
                )
            except FileSkipped:
                sys.stdout.write(input_stream.read())
    elif "/" in file_names and not allow_root:
        printer = create_terminal_printer(
            color=config.color_output, error=config.format_error, success=config.format_success
        )
        printer.error("it is dangerous to operate recursively on '/'")
        printer.error("use --allow-root to override this failsafe")
        sys.exit(1)
    else:
        if stream_filename:
            printer = create_terminal_printer(
                color=config.color_output, error=config.format_error, success=config.format_success
            )
            printer.error("Filename override is intended only for stream (-) sorting.")
            sys.exit(1)
        skipped: List[str] = []
        broken: List[str] = []

        if config.filter_files:
            filtered_files = []
            for file_name in file_names:
                if config.is_skipped(Path(file_name)):
                    skipped.append(file_name)
                else:
                    filtered_files.append(file_name)
            file_names = filtered_files

        file_names = files.find(file_names, config, skipped, broken)
        if show_files:
            for file_name in file_names:
                print(file_name)
            return
        num_skipped = 0
        num_broken = 0
        num_invalid_encoding = 0
        if config.verbose:
            print(ASCII_ART)

        if jobs:
            import multiprocessing

            executor = multiprocessing.Pool(jobs if jobs > 0 else multiprocessing.cpu_count())
            attempt_iterator = executor.imap(
                functools.partial(
                    sort_imports,
                    config=config,
                    check=check,
                    ask_to_apply=ask_to_apply,
                    write_to_stdout=write_to_stdout,
                    extension=ext_format,
                    config_trie=config_trie,
                ),
                file_names,
            )
        else:
            # https://github.com/python/typeshed/pull/2814
            attempt_iterator = (
                sort_imports(  # type: ignore
                    file_name,
                    config=config,
                    check=check,
                    ask_to_apply=ask_to_apply,
                    show_diff=show_diff,
                    write_to_stdout=write_to_stdout,
                    extension=ext_format,
                    config_trie=config_trie,
                )
                for file_name in file_names
            )

        # If any files passed in are missing considered as error, should be removed
        is_no_attempt = True
        any_encoding_valid = False
        for sort_attempt in attempt_iterator:
            if not sort_attempt:
                continue  # pragma: no cover - shouldn't happen, satisfies type constraint
            incorrectly_sorted = sort_attempt.incorrectly_sorted
            if arguments.get("check", False) and incorrectly_sorted:
                wrong_sorted_files = True
            if sort_attempt.skipped:
                num_skipped += (
                    1  # pragma: no cover - shouldn't happen, due to skip in iter_source_code
                )

            if not sort_attempt.supported_encoding:
                num_invalid_encoding += 1
            else:
                any_encoding_valid = True

            is_no_attempt = False

        num_skipped += len(skipped)
        if num_skipped and not config.quiet:
            if config.verbose:
                for was_skipped in skipped:
                    print(
                        f"{was_skipped} was skipped as it's listed in 'skip' setting, "
                        "matches a glob in 'skip_glob' setting, or is in a .gitignore file with "
                        "--skip-gitignore enabled."
                    )
            print(f"Skipped {num_skipped} files")

        num_broken += len(broken)
        if num_broken and not config.quiet:
            if config.verbose:
                for was_broken in broken:
                    warn(f"{was_broken} was broken path, make sure it exists correctly")
            print(f"Broken {num_broken} paths")

        if num_broken > 0 and is_no_attempt:
            all_attempt_broken = True
        if num_invalid_encoding > 0 and not any_encoding_valid:
            no_valid_encodings = True

    if not config.quiet and (remapped_deprecated_args or deprecated_flags):
        if remapped_deprecated_args:
            warn(
                "W0502: The following deprecated single dash CLI flags were used and translated: "
                f"{', '.join(remapped_deprecated_args)}!"
            )
        if deprecated_flags:
            warn(
                "W0501: The following deprecated CLI flags were used and ignored: "
                f"{', '.join(deprecated_flags)}!"
            )
        warn(
            "W0500: Please see the 5.0.0 Upgrade guide: "
            "https://pycqa.github.io/isort/docs/upgrade_guides/5.0.0.html"
        )

    if wrong_sorted_files:
        sys.exit(1)

    if all_attempt_broken:
        sys.exit(1)

    if no_valid_encodings:
        printer = create_terminal_printer(
            color=config.color_output, error=config.format_error, success=config.format_success
        )
        printer.error("No valid encodings.")
        sys.exit(1)

if __name__ == "__main__":
    main()


# Data Types | Numbers | Operations | Type Conversion | f-Strings

# project: Tip-Calculator

print("Welcome to the tip calculator.")

total = float(input("What was the total bill? $"))
tip_percent = int(
    input("What percentage tip would you like to give? 10, 12, or 15? "))
num_of_people = int(input("How many people to split the bill? "))

total_with_tip = total + total * (tip_percent / 100)
per_person = total_with_tip / num_of_people

print(f"Each person should pay: ${per_person:.2f}")


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Advent of code 2021 day 14 - https://adventofcode.com/2021/day/14

"""Goals:
1) What do you get if you take the quantity of the most common element and subtract the quantity
of the least common element?
2) Same as 1 but instead of 10 iterations, 40, which exponential growth makes
harder.
"""

# Programmed by CoolCat467

__title__ = 'Advent of Code 2021 - Day 14'
__author__ = 'CoolCat467'
__version__ = '0.0.0'
__ver_major__ = 0
__ver_minor__ = 0
__ver_patch__ = 0

from collections import Counter

def get_pairs(template):
    "Return pairs in template"
    return tuple(x+y for x,y in zip(template, template[1:]))

def insert_pairs(template, rules):
    "Insert pairs in template polymer following rules"
    data = []
    for pair in get_pairs(template):
        data.append(pair[0])
        if pair in rules:
            data.append(rules[pair])
    data.append(template[-1])
    return ''.join(data)

def preform_steps(step_count, template, rules):
    "Preform step_count steps of adding stuff to template polymer following rules."
    poly = template
    for _ in range(step_count):
        poly = insert_pairs(poly, rules)
    return poly

def insert_pairs_counts(template, rules, types):
    "Insert pairs in template polymer following rules"
    for pair, count in tuple(template.items()):
        if not pair in rules or count == 0:
            continue
        new = rules[pair]
        types[new] += count
        pos = pair[0]+new, new+pair[1]
        for new_pair in pos:
            template[new_pair] += count
        template[pair] -= count
    return template, types

def preform_steps_counts(step_count, template, rules):
    "Preform step_count steps of adding stuff to template polymer following rules."
    types = Counter(template)
    poly = Counter(get_pairs(template))
    for _ in range(step_count):
        poly, types = insert_pairs_counts(poly, rules, types)
    return poly, types

def run():
    "Solve problems"
    # Read file
    data = []
    with open('adv14.txt', 'r', encoding='utf-8') as rfile:
        data = rfile.read().splitlines()
        rfile.close()
##    data = """NNCB
##
##CH -> B
##HH -> N
##CB -> H
##NH -> C
##HB -> C
##HC -> B
##HN -> C
##NN -> C
##BH -> H
##NC -> B
##NB -> B
##BN -> B
##BB -> N
##BC -> B
##CC -> N
##CN -> C""".splitlines()
    # Format data
    template = data[0]
    pair_insert_rules = {}
    for item in data[2:]:
        key, value = item.split(' -> ')
        pair_insert_rules[key] = value
    # Solve 1
##    data = preform_steps(10, template, pair_insert_rules)
##    count = Counter(data)
##    values = sorted(tuple(count.values()))
    _, types = preform_steps_counts(10, template, pair_insert_rules)
    values = sorted(tuple(types.values()))

    print(values[-1] - values[0])
    # Solve 2
    _, types = preform_steps_counts(40, template, pair_insert_rules)

    values = sorted(tuple(types.values()))
    print(values[-1] - values[0])

if __name__ == '__main__':
    print(f'{__title__} v{__version__}\nProgrammed by {__author__}.')
    run()


import sys

input = sys.stdin.read()
lines = input.split("\n")
lines = lines[:-1]

units = {0 : "zero", 1 : "one", 2 : "two", 3 : "three", 4 : "four", 5 : "five", 6 : "six", 7 : "seven", 8 : "eight", 9 : "nine"}
tens = {2 : "twenty", 3 : "thirty", 4 : "forty", 5 : "fifty", 6 : "sixty", 7 : "seventy", 8 : "eighty", 9 : "ninety"}
teens = {10 : "ten", 11 : "eleven", 12 : "twelve", 13 : "thirteen", 14 : "fourteen", 15 : "fifteen", 16 : "sixteen", 17 : "seventeen", 18 : "eighteen", 19 : "nineteen" }

for line in lines:
    line = line.split()
    for i in range(len(line)):
        if line[i].isnumeric() == True:
            num = int(line[i])
            if num // 10 == 0:
                line[i] = units[num]
            else:
                if num // 10 == 1:
                    line[i] = teens[num]
                else:
                    string = ""
                    string += tens[num//10]
                    if num%10 != 0:
                        string += "-" + units[num%10]
                    line[i] = string
    line = " ".join(line)
    first = line[0].upper()
    line = first + line[1:]
    print (line)


import os

from knack.log import get_logger
from knack.util import CLIError

logger = get_logger(__name__)

def filter_by_git_diff(selected_modules, git_source, git_target, git_repo):
    if not any([git_source, git_target, git_repo]):
        return selected_modules

    if not all([git_target, git_repo]):
        raise CLIError('usage error: [--src NAME]  --tgt NAME --repo PATH')

    files_changed = diff_branches(git_repo, git_target, git_source)
    mods_changed = summarize_changed_mods(files_changed)

    repo_path = str(os.path.abspath(git_repo)).lower()
    to_remove = {'mod': [], 'core': [], 'ext': []}
    for key in selected_modules:
        for name, path in selected_modules[key].items():
            path = path.lower()
            if path.startswith(repo_path):
                if name in mods_changed:
                    # has changed, so do not filter out
                    continue
            # if not in the repo or has not changed, filter out
            to_remove[key].append(name)

    # remove the unchanged modules
    for key, value in to_remove.items():
        for name in value:
            selected_modules[key].pop(name)
    logger.info('Filtered out: %s', to_remove)

    return selected_modules

def summarize_changed_mods(files_changed):
    from azdev.utilities import extract_module_name

    mod_set = set()
    for f in files_changed:
        try:
            mod_name = extract_module_name(f)
        except CLIError:
            # some files aren't part of a module
            continue
        mod_set.add(mod_name)
    return list(mod_set)

def diff_branches(repo, target, source):
    """ Returns a list of files that have changed in a given repo
        between two branches. """
    try:
        import git  # pylint: disable=unused-import,unused-variable
        import git.exc as git_exc
        import gitdb
    except ImportError as ex:
        raise CLIError(ex)

    from git import Repo
    try:
        git_repo = Repo(repo)
    except (git_exc.NoSuchPathError, git_exc.InvalidGitRepositoryError):
        raise CLIError('invalid git repo: {}'.format(repo))

    def get_commit(branch):
        try:
            return git_repo.commit(branch)
        except gitdb.exc.BadName:
            raise CLIError('usage error, invalid branch: {}'.format(branch))

    if source:
        source_commit = get_commit(source)
    else:
        source_commit = git_repo.head.commit
    target_commit = get_commit(target)

    logger.info('Filtering down to modules which have changed based on:')
    logger.info('cd %s', repo)
    logger.info('git --no-pager diff %s..%s --name-only -- .\n', target_commit, source_commit)

    diff_index = target_commit.diff(source_commit)
    return [diff.b_path for diff in diff_index]

def diff_branches_detail(repo, target, source):
    """ Returns compare results of files that have changed in a given repo between two branches.
        Only focus on these files: _params.py, commands.py, test_*.py """
    try:
        import git  # pylint: disable=unused-import,unused-variable
        import git.exc as git_exc
        import gitdb
    except ImportError as ex:
        raise CLIError(ex)

    from git import Repo
    try:
        git_repo = Repo(repo)
    except (git_exc.NoSuchPathError, git_exc.InvalidGitRepositoryError):
        raise CLIError('invalid git repo: {}'.format(repo))

    def get_commit(branch):
        try:
            return git_repo.commit(branch)
        except gitdb.exc.BadName:
            raise CLIError('usage error, invalid branch: {}'.format(branch))

    if source:
        source_commit = get_commit(source)
    else:
        source_commit = git_repo.head.commit
    target_commit = get_commit(target)

    logger.info('Filtering down to modules which have changed based on:')
    logger.info('cd %s', repo)
    logger.info('git --no-pager diff %s..%s --name-only -- .\n', target_commit, source_commit)

    diff_index = target_commit.diff(source_commit)
    return diff_index


#! c:\Python33/python.exe
# -*- coding:utf-8 -*-
#SRM346 division2 easy

class DiamondHunt:
	def countDiamonds(self, s):
		res = 0
		while 1:
			length = len(s)
			flg = 0
			for i in range(0, length-1):
				if s[i] == '<' and s[i+1] == '>':
					s = s[0:i]+s[i+2:length]
					res += 1
					flg += 1
					break
			if flg == 0:
				break
		return res

	def test(self, i, s, ans):
		print("Test Case #" + str(i) + "...", end='')
		res = self.countDiamonds(s)
		if res == ans:
			print("PASSED")
		else:
			print("FAILED")
			print("Expected value is " + str(ans))
			print("But " + str(res) + " is returned")

obj = DiamondHunt()
obj.test(0, "><<><>>><", 3)
obj.test(1, ">>>><<", 0)
obj.test(2, "<<<<<<<<<>>>>>>>>>", 9)
obj.test(3, "><<><><<>>>><<>><<><<>><<<>>>>>><<<", 14)


import numpy as np
import pandas as pd
import os
import matplotlib.pyplot as plt

import torch
import json
import pycocotools._mask as _mask
import wandb

class DynamicsEvaluator():
    def __init__(self, config):
        # list of the annotation files for the whole dataset
        proposals=os.listdir('CLEVRER/derender_proposals')
        # sort the annotations
        self.proposals=sorted([proposal for proposal in proposals])
        self.skip=config['interaction_history']
        self.num_keypoints=config['num_keypoints']
        self.width=config['width']
        self.height=config['height']
        self.horizon=config['prediciton_horizon']
        self.success_rates=[]
        for i in range(4):
            self.success_rates.append([])

    def collect(self, video_idx, coords, status, predicted_coords, predicted_status):
        predicted_coords=predicted_coords.detach().cpu().numpy().astype(np.int32)[0]
        predicted_status=predicted_status.detach().cpu().numpy().astype(np.int32)[0]
        coords= coords.detach().cpu().numpy().astype(np.int32)[0]
        status=status.detach().cpu().numpy().astype(np.int32)[0]
        # for multistep prediction
        for h in range(self.horizon):
            # shift the prediction one time step to compare to the ground truth
            predicted_coords[:,:,h]=np.roll(predicted_coords[:,:,h],-(h+1), axis=0)
        # print('Video_idx: {0} Annotation file: CLEVRER/derender_proposals/{1}'.format(video_idx, self.proposals[video_idx]))
        with open('CLEVRER/derender_proposals/{0}'.format(self.proposals[video_idx])) as f:
            data=json.load(f)
        # objects in the scene
        objects={}
        num_frames=len(data['frames'])-1
        num_objects=np.zeros(num_frames)
        detected_objects=np.zeros(num_frames)
        success_rate=[]
        for i in range(self.horizon):
            success_rate.append([])
        # iterate over the frames
        for i,frame in enumerate(data['frames']):
            if i>=126:
                continue
            # get the number of objects
            num_objects=len(frame['objects'])
            detected_objects=np.zeros(self.horizon)
            # iterate over objects
            for obj in frame['objects']:
                # object name
                object_name="{0}_{1}_{2}".format(obj['color'], obj['material'], obj['shape'])
                # create an entry in the dictionary if it's not already there
                if object_name not in objects.keys():
                    # kp the keypoint association at time step t-1
                    # pred predicted keypoint association for time step t (predicted at timestep t-1)
                    objects[object_name]={'kp':np.zeros((self.horizon+1,self.num_keypoints)), 'pred':np.zeros((self.horizon,self.num_keypoints))}
                # decode the mask (time step t)
                mask=_mask.decode([obj['mask']])
                # update the keypoints in the object
                for k in range(self.num_keypoints):
                    # fill in the association of keypoints and objects
                    # consider only active keypoints in time step t for predictions
                    if status[i,k,0]>0.9:
                        if i>self.skip and i+self.skip < num_frames:
                            for p in range(self.horizon):
                                x=predicted_coords[i,k,p,0]
                                y=predicted_coords[i,k,p,1]
                                if x<self.width and y<self.height and mask[y,x]==1:
                                    objects[object_name]['pred'][p][k]=1
                                else:
                                    objects[object_name]['pred'][p][k]=0
                                # keep the keypoint in the object only if the input coordinates (updated at time t-p) was detecting the object
                                objects[object_name]['pred'][p][k]*=objects[object_name]['kp'][p+1][k]
                        # update the keypoints in the object for time step t
                        # this will be used to compare with prediciton at next time step
                        if coords[i,k,0]<self.width and coords[i,k,1]<self.height and mask[coords[i,k,1],coords[i,k,0]]==1:
                            objects[object_name]['kp'][0][k]=1
                        else:
                            objects[object_name]['kp'][0][k]=0
                # shift the kp to add the last occurence
                objects[object_name]['kp']=np.roll(objects[object_name]['kp'],1,axis=0)
                if i>self.skip:
                    for p in range(self.horizon):
                        # an object is detected if a prediciton lies in it
                        if np.sum(objects[object_name]['pred'][p])>0:
                            detected_objects[p]+=1
            if i>self.skip+self.horizon and i+self.skip+self.horizon < num_frames:
                for p in range(self.horizon):
                    # the success rate for this frame is the percentage of object detected by the prediciton
                    success_rate[p].append(detected_objects[p]/num_objects)
        for p in range(self.horizon):
            # the mean of success rates over all frames
            mean_success_rate=np.array(success_rate[p]).mean()
            self.success_rates[p].append(mean_success_rate)

    def save_to_file(self, model_name=None):
        # create a folder to store the dataset
        os.makedirs('results',exist_ok=True)
        file_path="results/{0}.xlsx".format(model_name)
        writer=pd.ExcelWriter(file_path, engine='openpyxl')
        data_frame=pd.DataFrame({"Success rate - 1 step prediction" : self.success_rates[0],
                                 "Success rate - 2 steps prediction" : self.success_rates[1],
                                 "Success rate - 3 steps prediction" : self.success_rates[2],
                                 })
        table=wandb.Table(dataframe=data_frame)
        wandb.log({"Results" : table})
        data_frame.to_excel(writer, sheet_name='per-video results', index=False)
        result_stats=data_frame.describe()
        table=wandb.Table(dataframe=result_stats)
        wandb.log({"Statistics over all videos" : table})
        result_stats.to_excel(writer, sheet_name='Statistics')
        writer.save()
        writer.close()
        return data_frame


#-------------------------------------------------------------------
#
# Written by Phillip Grabovsky 03/20/2022
# This code is distributed under the GNU LGPL license.
#
# This code graphs the output produced by the Fortran code
# in the Stieltjes project. Fortran writes text files containing numbers
# This grapher reads these files and builds the plots. See README files
# for this project.
#
# ------------------------------------------------------------------

import sys
import csv
import numpy as np
import PySide2
import PySide2.QtWidgets as Qt
import PySide2.QtGui as Gui
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT as NavigationToolbar
from matplotlib.gridspec import GridSpec
from matplotlib.figure import Figure

#Same Curve should be set to true if the input data lies on the same curve as the
#extrapolation data. This is only needed in Nyquist mode, it is true always in
#Voigt mode regardless the value below.
SameCurve = True

class MplCanvas(FigureCanvasQTAgg):
    def __init__(self, parent, mode, status, dpi=100):
        if status:
            self.initializeNormally(parent, mode, dpi=100)
        else:
            self.disableCaprinis(parent, mode, dpi=100)
        super(MplCanvas, self).__init__(self.fig)

    #initialize normally with caprinis.
    def initializeNormally(self, parent, mode, dpi=100):
        self.dataLabels = []
        self.lw = 1
        self.fig = Figure(dpi=dpi, constrained_layout=True)
        gs = GridSpec(2, 2, width_ratios=[3,2], figure=self.fig) #2x2 grid, more space alotted for left column.
        self.topCapriniAxes = self.fig.add_subplot(gs[0,1]) #top caprini is top left
        self.bottomCapriniAxes = self.fig.add_subplot(gs[1,1]) #bottom caprini is bottom left
        self.axisDict = {"TC":self.topCapriniAxes,
                        "BC":self.bottomCapriniAxes}
        if(mode == "Voigt"):
            self.topVoigtAxes = self.fig.add_subplot(gs[0,0])
            self.bottomVoigtAxes = self.fig.add_subplot(gs[1,0])
            self.axisDict["TV"] = self.topVoigtAxes
            self.axisDict["BV"] = self.bottomVoigtAxes
            self.setPlotInfo(self.topVoigtAxes, "Real Part", "Freq/Hz", "Z Real/Ohm")
            self.setPlotInfo(self.bottomVoigtAxes, "Imaginary Part", "Freq/Hz", "Z Imag/Ohm)")
            self.setPlotInfo(self.topCapriniAxes,"Caprini Function", "Freq/Hz", "")
            self.setPlotInfo(self.bottomCapriniAxes,"Zeros of the Caprini Function", "Freq/Hz", "")
        elif(mode == "Nyquist"):
            self.nyquistAxes = self.fig.add_subplot(gs[:,0]) #nyquist takes up whole left column
            self.axisDict["N"] = self.nyquistAxes
            self.setPlotInfo(self.nyquistAxes, "Nyquist Plot", "Re", "Im")
            self.setPlotInfo(self.topCapriniAxes,"Caprini Function", "t/S", "")
            self.setPlotInfo(self.bottomCapriniAxes,"Zeros of the Caprini Function", "t/S", "")

    #initialize without caprinis because they shouldn't be shown.
    def disableCaprinis(self, parent, mode, dpi=100):
        self.dataLabels = []
        self.lw = 1
        self.axisDict = {}
        self.fig = Figure(dpi=dpi, constrained_layout=True)
        if(mode == "Voigt"):
            gs = GridSpec(2, 1, figure=self.fig)
            self.topVoigtAxes = self.fig.add_subplot(gs[0,0])
            self.bottomVoigtAxes = self.fig.add_subplot(gs[1,0])
            self.axisDict["TV"] = self.topVoigtAxes
            self.axisDict["BV"] = self.bottomVoigtAxes
            self.setPlotInfo(self.topVoigtAxes, "Real Part", "Freq/Hz", "Z Real/Ohm")
            self.setPlotInfo(self.bottomVoigtAxes, "Imaginary Part", "Freq/Hz", "Z Imag/Ohm)")
        elif(mode == "Nyquist"):
            self.nyquistAxes = self.fig.add_subplot(111) #nyquist takes up whole left column
            self.axisDict["N"] = self.nyquistAxes
            self.setPlotInfo(self.nyquistAxes, "Nyquist Plot", "Re", "Im")

    def setGraphScale(self, axes, xMin, xMax, yMin, yMax):
        self.axisDict[axes].set_xlim(xMin, xMax)
        self.axisDict[axes].set_ylim(yMin, yMax)

    def drawSemiLogX(self, axes, x, y, dataLabel, curveColor):
        if not (dataLabel in self.dataLabels):
            self.dataLabels.append(dataLabel)
            self.axisDict[axes].semilogx(x,y, label=dataLabel, color=curveColor, linewidth=self.lw)
        else:
            self.axisDict[axes].semilogx(x,y,color=curveColor,linewidth=self.lw)
        if(axes == "TV" or axes == "N"):
            self.axisDict[axes].legend()

    def drawSemiLogXDots(self, axes, x, y, dataLabel, curveColor):
        if not (dataLabel in self.dataLabels):
            self.dataLabels.append(dataLabel)
            self.axisDict[axes].semilogx(x,y, '.',label=dataLabel, color=curveColor, linewidth=self.lw)
        else:
            self.axisDict[axes].semilogx(x,y,'.',color=curveColor,linewidth=self.lw)
        if(axes == "TV" or axes == "N"):
            self.axisDict[axes].legend()

    def drawLinLin(self, axes, x, y, dataLabel, curveColor):
        if not (dataLabel in self.dataLabels):
            self.dataLabels.append(dataLabel)
            self.axisDict[axes].plot(x,y, label=dataLabel, color=curveColor,linewidth=self.lw)
        else:
            self.axisDict[axes].plot(x,y, color = curveColor,linewidth=self.lw)
        if(axes == "TV" or axes == "N"):
            self.axisDict[axes].legend()

    def drawLinLinDots(self, axes, x, y, dataLabel, curveColor):
        if not (dataLabel in self.dataLabels):
            self.dataLabels.append(dataLabel)
            self.axisDict[axes].plot(x,y, '.',label=dataLabel, color=curveColor,linewidth=self.lw)
        else:
            self.axisDict[axes].plot(x,y, '.',color = curveColor,linewidth=self.lw)
        if(axes == "TV" or axes == "N"):
            self.axisDict[axes].legend()

    def setPlotInfo(self, axes, title, xTitle, yTitle):
        axes.set_title(title)
        axes.set_xlabel(xTitle)
        axes.set_ylabel(yTitle)

class StieltjesGraphWindow(Qt.QDialog):
    def __init__(self, sameCurveArg):
        super().__init__()#set up Qt
        self.topLevelLayout = Qt.QHBoxLayout()
        self.graphData = [[],[]] #set by populateData()
        self.mode, zetaFZeta = self.detectMode() #is set by populateData()
        self.sameCurve = sameCurveArg #set by user. whether the data lies on the extrapolation curve.
        self.status, fZeta = self.detectStatus() #whether to show caprini functions

        #setup graphs!
        self.graph = Qt.QFrame()
        graphLayout = Qt.QVBoxLayout()
        self.graphCanvas = MplCanvas(self,self.mode, self.status, dpi=100)
        toolbar = NavigationToolbar(self.graphCanvas, self)
        graphLayout.addWidget(toolbar)
        graphLayout.addWidget(self.graphCanvas)
        self.graph.setLayout(graphLayout)
        self.graph.setFrameShape(Qt.QFrame.Panel)
        self.graph.setFrameShadow(Qt.QFrame.Sunken)
        self.graph.setLineWidth(2)

        graphLayout = Qt.QVBoxLayout()  #update topLevelLayout to include graphs!
        graphLayout.addWidget(self.graph)
        self.topLevelLayout.addItem(graphLayout)
        self.populateData(zetaFZeta, fZeta)      #take in txt file data and add it to graphs.
        self.setLayout(self.topLevelLayout)
        self.setWindowState(PySide2.QtCore.Qt.WindowState.WindowMaximized)

    def populateData(self, zetaFZeta, fZeta): #figure out graph contents
        #populate data for correct graph
        if self.mode == "Nyquist":
            self.populateNyquist(zetaFZeta, fZeta)
        elif self.mode == "Voigt":
            self.populateVoigt(zetaFZeta, fZeta)
        if(self.status):
            self.populateCaprinis()

    def populateNyquist(self, zetaFZeta, fZeta): #update nyquist plot
        #Spectral rep and Extrapolation curves
        Z, W = [], []
        for row in zetaFZeta:
            Z.append(complex(row[0], row[1]))   #needed for spectral rep
            W.append(complex(row[2], row[3]))   #extrapolation
        fZ = self.computeSpectralRep(Z, fZeta)  #finish computing spectral rep
        #Uncertainty curves
        dataSizes = self.loadFileIntoNestedArrays("data_sizes.txt")
        NMC = int(dataSizes[2][0])
        nZeta = int(dataSizes[1][0])
        count = 0
        if NMC > 0:
            MonteCarlo = self.loadFileIntoNestedArrays("WMC.txt")
            WMC = []
            for k in range(nZeta): #iteration is not row->col to suit WMC.txt format.
                for j in range(NMC):
                    if(k==0):
                        WMC.append([])
                    WMC[j].append(complex(MonteCarlo[count][0], MonteCarlo[count][1]))
                    count=count+1
        #actually plot the results
        for j in range(NMC):
            self.graphCanvas.drawLinLin("N",np.real(WMC[j]),np.imag(WMC[j]),"Uncertainty", "silver")
        self.graphCanvas.drawLinLin("N",np.real(W), np.imag(W), "Extrapolation", "orange") #graph extrapolation
        self.graphCanvas.drawLinLin("N",np.real(fZ), np.imag(fZ), "Spectral Rep", "cyan") #graph spectral rep
        #plot original points if extrap is on same curve is on original data.
        if self.sameCurve:
            expData = self.loadFileIntoNestedArrays("exp_data.txt")
            if self.status:
                wfix = np.transpose(self.loadFileIntoNestedArrays("wfix.txt"))
                self.graphCanvas.drawLinLinDots("N", wfix[0], wfix[1], "Alternative Data", "red")
            wOrig = []
            for row in expData:
                wOrig.append(complex(row[2], row[3]))
            self.graphCanvas.drawLinLinDots("N", np.real(wOrig), np.imag(wOrig), "W Original", "black")

    def computeSpectralRep(self, Z, fZeta):
        sk = [] #sk is the 2nd column of fzeta.
        tk = [] #tk is the 1st column of fzeta, w/out the 1st element
        for i in range(len(fZeta)):
            sk.append(fZeta[i][1])
            if i != 0:
                tk.append(fZeta[i][0])
        M=[]  #M = 1/(tk - Z), tk is a column, Z is a row.
        for i in range(len(tk)):
            M.append([])
            for j in range(len(Z)):
                M[i].append( 1/(tk[i] - Z[j]) )
        Mt = np.transpose(M)
        fZ = np.matmul(Mt,sk[1:]) #fZ=[] #fZ=sk[0] + M.'* sk[1:]
        fZ = [sk[0] + i for i in fZ]
        return fZ

    def populateVoigt(self, zetaFZeta, fZeta):
        # fZ exp data [freq,re(Z),im(Z)]; wfix=alternative data
        # f=all frequencies, F=Z(f) is the model
        # Z(n_zeta,2) is the extrapolation
        # EIS(f)=F_spectral(f), WZ(n_zeta,Nr)=monte-carlo
        dataSizes=self.loadFileIntoNestedArrays('data_sizes.txt');
        fZ1Split = self.loadFileIntoNestedArrays("Voigt_data.txt")
        Nr = int(dataSizes[2][0])
        #unpack data
        freq, fZ1, f1, sk, tk, zetaFZetaCombined  = [],[],[],[],[],[]
        for row in fZ1Split:
            freq.append(row[0])
            fZ1.append(complex(row[1], row[2]))
        for row in zetaFZeta:
            f1.append(row[0])
        nZeta = len(f1)
        for i in range(len(fZeta)):
            if i > 0:
                tk.append(fZeta[i][0])
            sk.append(fZeta[i][1])
        for row in zetaFZeta:
            zetaFZetaCombined.append(complex(row[1], row[2]))
        #compute M and EIS - to show voigt circuit on graphs.
        M=[]  #M=1./(tk+2i*pi*f1');
        for i in range(len(tk)):
            M.append([])
            for j in range(len(f1)):
                M[i].append( 1/(complex(tk[i], f1[j]*2*np.pi)) )
        Mt = np.transpose(M)
        EIS = sk[0] + np.matmul(Mt, sk[1:])
        #uncertainty lines retrieved and computed
        if Nr > 0:
            WZ = []
            WMC = self.loadFileIntoNestedArrays("WMC.txt");
            count=0;
            for k in range(nZeta):
                for j in range(Nr):
                    if(k==0):
                        WZ.append([])
                    WZ[j].append( complex(WMC[count][0], WMC[count][1]) )
                    count+=1
        #uncertainty
        for i in range(Nr):
            self.graphCanvas.drawSemiLogX("TV", f1, np.real(WZ[i]), "Uncertainty", "silver")
            self.graphCanvas.drawSemiLogX("BV", f1, np.imag(WZ[i]), "Uncertainty", "silver")
        if self.status: #alternative data is only printed if status is true.
            wfix = self.loadFileIntoNestedArrays("wfix.txt")
            wfix = np.transpose(wfix)
            self.graphCanvas.drawSemiLogXDots("TV", freq, wfix[0], "Alternative Data","red")
            self.graphCanvas.drawSemiLogXDots("BV", freq, wfix[1], "Alternative Data","red")
        #upper graph - real part
        self.graphCanvas.drawSemiLogXDots("TV", freq, np.real(fZ1), "Data","black")
        self.graphCanvas.drawSemiLogX("TV", f1, np.real(zetaFZetaCombined), "Extrapolation", "black")
        self.graphCanvas.drawSemiLogX("TV", f1, np.real(EIS), "Voigt Circuit", "cyan")
        #lower graph - imaginary part
        self.graphCanvas.drawSemiLogXDots("BV", freq, np.imag(fZ1), "Data","black")
        self.graphCanvas.drawSemiLogX("BV", f1, np.imag(zetaFZetaCombined), "Extrapolation", "black")
        self.graphCanvas.drawSemiLogX("BV", f1, np.imag(EIS), "Voigt Circuit", "cyan")

    def populateCaprinis(self):
        capriniData = self.loadFileIntoNestedArrays("tC.txt") #The Caprini function [t(k),C(t(k))]
        tC = [] #data to be graphed - x
        C = []     #y axis data
        for row in capriniData:
            tC.append(row[0])
            C.append(row[1])
        self.graphCanvas.drawSemiLogX("TC",[tC[0],tC[len(tC)-1]],[0,0],"","red") #draw baseline
        self.graphCanvas.drawSemiLogX("TC", tC, C, "", "blue" )   #draw caprini
        self.graphCanvas.drawSemiLogX("BC",[tC[0],tC[len(tC)-1]],[0,0],"","red") #draw baseline
        self.graphCanvas.drawSemiLogX("BC",tC, C, "", "blue")    #draw caprini mins
        #scale the bottom graph to show zeros
        CMin, CMax = self.getgraphScale(C)
        tMin = min(tC)
        tMax = max(tC)
        self.graphCanvas.setGraphScale("BC",tMin, tMax, CMin, CMax)
        self.graphCanvas.axisDict["TC"].set_xlim(tMin, tMax)

    def getgraphScale(self, C): #find local minima, and figure out suitable graph scale.
        diffs = np.diff(C)
        CLocalMins = []
        lastNegative = False
        for i in range(len(diffs)):
            thisPositive = diffs[i] > 0
            if(lastNegative and thisPositive):
                CLocalMins.append(C[i])
            lastNegative = diffs[i] < 0
        biggestMin = max(CLocalMins)
        smallestMin = min(CLocalMins)
        CMax=max([-10*smallestMin,1.1*biggestMin,1.e-9*max(C)])
        CMin = -1.e-10*max(C)
        return CMin, CMax

    def detectMode(self):
        zetaFZeta=self.loadFileIntoNestedArrays('W_extr.txt')
        if len(zetaFZeta[0]) == 4:
            return "Nyquist", zetaFZeta
        elif len(zetaFZeta[0]) == 3:
            return "Voigt", zetaFZeta
        else:
            return "UnknownData", zetaFZeta

    def detectStatus(self):
        fZeta = self.loadFileIntoNestedArrays("spectral_measure.txt")
        if(fZeta[0][0] == 0): #determine if caprinis should be shown.
            return False, fZeta
        else:
            return True, fZeta

    def loadFileIntoNestedArrays(self, filename): #load a data file into a 2d array
        dataList = []
        with open(filename, 'r') as csvfile:
            csvreader = csv.reader(csvfile, delimiter=" ")
            #fields = next(csvreader)
            i = 0 #counter variable needed iteration over len(csvreader) not possible
            for row in csvreader: #fill in data
                dataList.append([]) #add next column of dataList
                for j in range(len(row)):
                    if(row[j] != ""):
                        dataList[i].append(float(row[j]))
                i+=1
        return dataList

if __name__ == '__main__':
    # Create the Qt Application
    app = Qt.QApplication(sys.argv)
    # Create and show the form
    form = StieltjesGraphWindow(SameCurve)
    form.show()
    # Run the main Qt loop
    sys.exit(app.exec_())


# -*- coding: utf-8 -*-
"""
Created on Fri Mar 11 16:03:50 2022

@author: ethan
"""

# -*- coding: utf-8 -*-
"""
Created on Sat Feb 19 14:05:58 2022

@author: ethan
"""

from datetime import date
from datetime import datetime as dt
from time import time
import math

import numpy as np
import pandas as pd
from scipy import stats

from bokeh.io import export_png, output_file, show
from bokeh.plotting import figure
from bokeh.models import NumeralTickFormatter, LabelSet, ColumnDataSource
from bokeh.models.tickers import FixedTicker
from bokeh.layouts import row, column

df1 = (
    pd.read_excel(
        "data.xlsx", sheet_name="Sheet1", index_col="date_daily", parse_dates=True
    ).dropna()
    / 100
)

df2 = pd.read_excel("data.xlsx", sheet_name="Sheet2").dropna()
date_convert = lambda x: dt(
    year=int(x[-2:]) + 1900, day=int(x[-4:-2]), month=int(x[:-4])
)
df2.index = df2["MTGDATE"].astype(str).apply(date_convert)
df2 = df2 / 100

print(df2)

NIUred = (200, 16, 46)
NIUpantone = (165, 167, 168)
#%%
def set_up(x, y, truncated=True, margins=None):
    if truncated:
        b = (3 * y.min() - y.max()) / 2
    else:
        b = y.min()
    if margins == None:
        xrng = (x.min(), x.max())
        yrng = (b, y.max())
    else:
        xrng = (x.min() - margins, x.max() + margins)
        yrng = (b - margins, y.max() + margins)

    x = x.dropna()
    y = y.dropna()

    return (x, y, xrng, yrng)

# Chart of approximately stionary time series, e.g. PCE-Core inflation from 2008 to 2020
def chart1(df, series, title, name):
    xdata, ydata, xrng, yrng = set_up(df.index, df[series], truncated=False)
    scale = 1
    p = figure(
        width= int(1000 * scale),
        height=int(666 * scale),
        title=title,
        x_axis_label="Date",
        x_axis_type="datetime",
        y_range=yrng,
        x_range=xrng,
        toolbar_location=None,
    )
    p.line(xrng, [0, 0], color="black", width=1)

    p.line(xdata, ydata, color=NIUred, width=2)

    p.xaxis[0].ticker.desired_num_ticks = 10
    # p.legend.location = "bottom_right"
    p.xgrid.grid_line_color = None
    p.ygrid.grid_line_color = None
    p.yaxis.formatter = NumeralTickFormatter(format="0.0%")
    #p.xaxis.major_label_orientation = math.pi/4
    p.title.text_font_size = "16pt"
    p.xaxis.axis_label_text_font_size = "14pt"
    p.yaxis.axis_label_text_font_size = "14pt"
    # p.legend.label_text_font_size = "14pt"
    export_png(p, filename=name)

    return p

# Chart of a regression e.g. inflation vs money supply
def chart2(df):
    xdata, ydata, xrng, yrng = set_up(
        df["DFF"], df["GDPC1_PCA"], truncated=False, margins=0.005
    )
    xrng = (0, xrng[1])
    p = figure(
        width=700,
        height=500,
        title="Do Rate Hikes Decrease Real GDP? 1954 to 2019",
        x_axis_label="Federal Funds Effective Rate (Quarterly Average)",
        y_axis_label="RGDP Growth (Annualized)",
        y_range=yrng,
        x_range=xrng,
    )
    p.line(xrng, [0, 0], color="black", width=3)
    p.line([0, 0], yrng, color="black", width=3)

    slope, intercept, r_value, p_value, std_err = stats.linregress(xdata, ydata)
    leg = "R = {:.4f}, Slope = {:.4f}".format(r_value, slope)
    p.line(xdata, xdata * slope + intercept, legend=leg, color=NIUpantone, width=4)
    p.circle(xdata, ydata, color=NIUred, size=5)

    p.xaxis[0].ticker.desired_num_ticks = 10
    p.xgrid.grid_line_color = None
    p.ygrid.grid_line_color = None
    p.xaxis.formatter = NumeralTickFormatter(format="0.0%")
    p.yaxis.formatter = NumeralTickFormatter(format="0.0%")
    p.title.text_font_size = "16pt"
    p.xaxis.axis_label_text_font_size = "14pt"
    p.yaxis.axis_label_text_font_size = "14pt"
    p.legend.label_text_font_size = "14pt"

    export_png(p, filename="imgs/ffrRgdp.png")

    return p

show(
    column(
        chart1(df1, "FFR_shock", "Gertler and Karadi Baseline Indicator", "gk15.png"),
        chart1(df2, "RESIDF", "Romer and Romer Indicator", "rr04.png"),
    )
)


# -*- coding: utf-8 -*-
import datetime
from openerp import models, fields, api
from openerp.exceptions import Warning
from openerp.tools.translate import _
import logging

class hr_contract(models.Model):
    _inherit = "hr.contract"

    no_renewal = fields.Boolean('No Renewal', default=False)

    @api.multi
    def get_email_to_send(self):
        """
        Get list emails of users in group HR officer.
        """
        group = self.env.ref('base.group_hr_user')
        mail_list = ''
        for user in group.users:
            mail_list += user.email and user.email + ', ' or ''
        return mail_list

    @api.multi
    def _get_contract_link(self):
        """
        Build a link to the contract.
        """
        param_obj = self.env['ir.config_parameter']
        base_url = param_obj.get_param('web.base.url', default='')
        # Example link: http://0.0.0.0:8069/?#id=4
        #    &view_type=form&model=hr.contract&action=...
        act_window = self.env.ref('hr_contract.action_hr_contract')
        link = base_url + '/?#id=%s&view_type=form&model=hr.contract&action=%s'\
            % (self.id, act_window.id)
        return link

    @api.model
    def _get_expired_contracts_by_department(self, date):
        """
        Get the contract which will be expired before inputed date
            without the next contract created
            without the No Renewal checkbox set
        """
        self._cr.execute("""
            SELECT con.id, emp.name_related as employee,
            con.name, con.date_end,
            dept.name as department, job.name as job
            FROM hr_contract con
                JOIN hr_employee emp ON con.employee_id = emp.id
                LEFT JOIN hr_department dept ON emp.department_id = dept.id
                LEFT JOIN hr_job job ON con.job_id = job.id
            WHERE con.date_end <= '%s'
                AND con.no_renewal = False
                AND NOT EXISTS(
                    SELECT 1 FROM hr_contract
                    WHERE date_start > con.date_end
                    AND employee_id = con.employee_id
                )
            ORDER BY date_end
           """ % date)
        res = self._cr.dictfetchall()
        expired_contracts_department = {}
        for line in res:
            department = line['department'] or 'Undefined'
            if department not in expired_contracts_department:
                expired_contracts_department[department] = []
            expired_contracts_department[department].append(line)
        return expired_contracts_department

    @api.model
    def send_email_contract_end_next_x_days(self):
        """
        Send email weekly to remind the contracts expired next 30 days
        """
        context = dict(self._context)
        param_obj = self.env['ir.config_parameter']
        days_param = param_obj.get_param('contract_expiring_days', default='30')
        try:
            next_x_date = (datetime.date.today()
                            + datetime.timedelta(int(days_param))).strftime('%Y-%m-%d')
        except Exception as exc:
            logging.error(exc)
            raise Warning(_('Wrong value defined in parameter expiring_days.'))

        email_data = self._get_expired_contracts_by_department(next_x_date)
        if not email_data:
            return True
        context.update({'email_data': email_data})
        template = self.env.ref(
            'trobz_hr_mail_contract_end.email_template_contract_end_next_x_days'
        )
        if not template:
            return True
        template.with_context(context).send_mail(
            email_data.values()[0][0]['id'], True
        )
        return True

    @api.model
    def send_email_contract_end(self):
        """
        Send email daily to remind the contracts expired
        """
        context = dict(self._context)
        date = datetime.date.today().strftime('%Y-%m-%d')

        email_data = self._get_expired_contracts_by_department(date)
        if not email_data:
            return True
        context.update({'email_data': email_data})
        template = self.env.ref(
            'trobz_hr_mail_contract_end.email_template_contract_end'
        )
        if not template:
            return True
        first_contract_id = email_data.values()[0][0]['id']
        template.with_context(context).send_mail(
            first_contract_id, True
        )
        return True

    @api.multi
    def get_email_information(self):
        """
        Get content of renewal contracts email
        """
        email_data = self._context.get('email_data', False)
        department = email_data.keys()
        str_email_info = ''
        for department in email_data:
            str_email_info += """
 Department %s
            """ % department
            str_email_info += '
'
            for contract in email_data[department]:
                link = self._get_contract_link()
                str_email_info += '
%s - %s - %s
'\
                    % (contract['date_end'], link,
                       contract['name'], contract['job'] or 'Undefined Job')
            str_email_info += '
'

            str_email_info += '
'
        return str_email_info

    @api.model
    def get_contract_expiring_days(self):
        """
        """
        param_obj = self.env['ir.config_parameter']
        return param_obj.get_param('contract_expiring_days', default='30')

hr_contract()


import pytest
import os

from conflow import from_env
from conflow.manager import Config

DEFAULT_SETTINGS = {
    'db': {
        'master': {
            'host': 'localhost',
            'port': 5432,
        },
        'slave': {
            'host': 'localhost',
            'port': 5433,
        }
    }
}

ENV_SETTINGS = {
    'db': {
        'master': {
            'host': 'env_master_host',
            'port': 5432,
        },
        'slave': {
            'host': 'env_slave_host',
            'port': 5433,
        }
    }
}

@pytest.fixture
def config():
    return Config()

def test_config_first_merge(config):
    config.merge(DEFAULT_SETTINGS)
    assert config.layer is not None

def test_config_merge(config):
    config = config.merge(DEFAULT_SETTINGS)
    assert config.layer.tree().db.master.host == 'localhost'
    config = config.merge(ENV_SETTINGS)
    assert config.layer.tree().db.master.host == 'env_master_host'

def test_config_get_attr(config):
    config = config.merge(DEFAULT_SETTINGS)
    assert config.db.master.host == 'localhost'

def test_config_get_item(config):
    config = config.merge(DEFAULT_SETTINGS)
    assert config['db']['master']['host'] == 'localhost'

def test_config_with_froms(config):
    os.environ['APP_DB__MASTER__HOST'] = 'env_host'
    os.environ['APP_DB__SLAVE__HOST'] = 'env_host'
    config = config.merge(DEFAULT_SETTINGS).merge(from_env('APP'))
    assert config.db.master.host() == 'env_host'
    assert config.db.slave.host() == 'env_host'


from Machine import Machine

class Gunner(Machine):
    def attack(self, other_machine, attack_buff):
        if self.check_at_attack_range(other_machine):
            armor_hit = self.check_armor(other_machine)
            weak_spot_hit = self.check_weak_spots(other_machine)
            self.deal_damage(attack_buff, armor_hit, weak_spot_hit, other_machine)

    def deal_damage(self, attack_buff, armor_hit, weak_spot_hit, enemy):
        damage = self.get_attack() - attack_buff + armor_hit + weak_spot_hit
        enemy_health = enemy.get_health()
        enemy.set_health(enemy_health - damage)

    def check_at_attack_range(self, other_machine):
        facings = ["front", "right", "back", "left"]
        attack_tile_interval = [-8, 1, 8, -1]
        current_facing_index = facings.index(self.get_facing())
        checked_tile = self.get_tile_position() + attack_tile_interval[current_facing_index] * self.get_attack_range()
        if other_machine.get_tile_position() == checked_tile:
            return True
        return False