# Copyright 2014, Solly Ross (see LICENSE.txt)
# Portions Copyright Python Software Foundation
# (see COPYRIGHT.txt and PYTHON-LICENSE.txt)

# main part
import code
import contextlib
import doctest
import getpass
import linecache
import pdb
import re
import sys
import traceback
import warnings

import six

from yalpt import ansi_helper as ansi
from yalpt import formatters
from yalpt import parsers

__all__ = ["LiterateInterpreter"]

@contextlib.contextmanager
def noop_mgr(writer):
    yield writer

class LiterateInterpreter(code.InteractiveConsole):
    def __init__(self, text_formatter=formatters.NoopFormatter(),
                 code_parser=parsers.DocTestParser(), use_ansi=True,
                 use_readline=True, env_driver=None, *args, **kwargs):
        code.InteractiveConsole.__init__(self, *args, **kwargs)

        self._output_checker = doctest.OutputChecker()
        self._fakeout = doctest._SpoofOut()
        self.chunks = None
        self.exc_msg = None
        self.name = 'literate program'
        self.text_formatter = text_formatter
        self.code_parser = code_parser
        self.use_ansi = use_ansi
        self.pause = True
        self.interactive = True

        if use_readline:
            self._readline = __import__('readline')
            self._add_readline()
        else:
            self._readline = None

        self._env_driver = env_driver

        self._correct_path()

    def runfunction(self, func):
        self.runcode(six.get_function_code(func))

    def _correct_path(self):
        def correct_path():
            import sys
            if '' not in sys.path:
                sys.path.insert(0, '')
            del sys

        self.runfunction(correct_path)

    def _add_readline(self):
        self.locals['__console_locals__'] = self.locals
        self.locals['readline'] = self._readline

        def add_readline():
            # add support for completion
            import atexit
            import os
            import readline
            import rlcompleter

            completer = rlcompleter.Completer(__console_locals__)  # noqa
            readline.set_completer(completer.complete)

            # tab completion
            readline.parse_and_bind('Control-space: complete')
            # history file
            histfile = os.path.join(os.environ['HOME'],
                                    '.literate-python-history')
            try:
                readline.read_history_file(histfile)
            except IOError:
                pass
            atexit.register(readline.write_history_file, histfile)
            del os, histfile, readline, rlcompleter

        self.runfunction(add_readline)

        del self.locals['__console_locals__']
        del self.locals['readline']

    def _interact_once(self, more):
        try:
            if more:
                prompt = sys.ps2
            else:
                prompt = sys.ps1
            try:
                line = self.raw_input(prompt)
                # Can be None if sys.stdin was redefined
                encoding = getattr(sys.stdin, "encoding", None)
                if encoding and not isinstance(line, six.text_type):
                    line = line.decode(encoding)
            except EOFError:
                self.write("\n")
                return (False, None)
            else:
                if len(line) == 0:
                    blank = True
                else:
                    blank = False
                more = self.push(line)
        except KeyboardInterrupt:
            self.write("\nKeyboardInterrupt\n")
            self.resetbuffer()
            more = False
            blank = False

        return (blank, more)

    # BEGIN FROM PYTHON STD LIB
    # (extracted from the doctest module, made slight changes to the
    # filename RE, and refactored _capture_output into its own method)
    __LINECACHE_FILENAME_RE = re.compile(r'<literate '
                                         r'(?P<name>.+)'
                                         r'\[(?P<chunknum>\d+)\]>$')

    def _patched_linecache_getlines(self, filename, module_globals=None):
        m = self.__LINECACHE_FILENAME_RE.match(filename)
        if m and m.group('name') == self.name:
            chunk = self.chunks[int(m.group('chunknum'))]
            source = chunk.source
            if isinstance(source, six.text_type):
                source = source.encode('ascii', 'backslashreplace')
            return source.splitlines(True)
        else:
            return self.save_linecache_getlines(filename, module_globals)

    @contextlib.contextmanager
    def _capture_output(self):
        save_stdout = sys.stdout
        sys.stdout = self._fakeout

        # set up pdb to work properly
        save_set_trace = pdb.set_trace
        self.debugger = doctest._OutputRedirectingPdb(save_stdout)
        self.debugger.reset()
        pdb.set_trace = self.debugger.set_trace
        self.save_linecache_getlines = linecache.getlines
        linecache.getlines = self._patched_linecache_getlines
        save_displayhook = sys.displayhook
        sys.displayhook = sys.__displayhook__

        try:
            yield sys.stdout
        finally:
            sys.stdout = save_stdout
            pdb.set_trace = save_set_trace
            linecache.getlines = self.save_linecache_getlines
            sys.displayhook = save_displayhook
    # END FROM PYTHON STD LIB

    def _process_code_line(self, line, res, more):
        if self._readline is not None:
            self._readline.add_history(line)

        if more:
            self.write(sys.ps2)
        else:
            self.write(sys.ps1)

        if self.use_ansi:
            self.write(ansi.with_codes(line, 38, 5, 0xdf))
        else:
            self.write(line)

        self.write("\n")
        with self._capture_output() as output:
            more = self.push(line)

            res += output.getvalue()
            output.truncate(0)

        exc = self.exc_msg
        self.exc_msg = None
        return (res, exc, more)

    def _format_tb(self):
        try:
            extype, value, tb = sys.exc_info()
            sys.last_type = extype
            sys.last_value = value
            sys.last_traceback = tb
            res = traceback.format_exception_only(extype, value)
        finally:
            tb = None

        if res:
            return res[-1]

    def showtraceback(self):
        try:
            extype, value, tb = sys.exc_info()
            sys.last_type = extype
            sys.last_value = value
            sys.last_traceback = tb
            tblist = traceback.extract_tb(tb)
            del tblist[:1]
            lst = traceback.format_list(tblist)
            if lst:
                lst.insert(0, "Traceback (most recent call last):\n")
            exc_msg = traceback.format_exception_only(extype, value)
            lst[len(lst):] = exc_msg
        finally:
            tblist = tb = None

        if self.filename.startswith("<literate "):
            self._fakeout.write(''.join(lst))
        else:
            if sys.excepthook is sys.__excepthook__:
                self.write(''.join(lst))
            else:
                sys.excepthook(extype, value, tb)

        self.exc_msg = ''.join(exc_msg)

    def _run_code(self, chunk, chunk_ind, pause=True):
        self.filename = "<literate {name}[{num}]>".format(name=self.name,
                                                          num=chunk_ind)
        more = False
        res = ""
        lines = chunk.source.split("\n")
        for line in lines[:-1]:
            res, exc, more = self._process_code_line(line, res, more)

        if more:
            res, exc, more = self._process_code_line(lines[-1], res, more)

        if self.use_ansi:
            mgr = ansi.BoxMaker(self)
        else:
            mgr = noop_mgr(self)

        if chunk.want is not None or chunk.exc_msg is not None:
            if len(res) > 0 or exc is not None:
                # compare to the expected output
                # TODO(sross): convert options to optionsflags
                optionsflags = 0

                checker = self._output_checker
                if exc is None:
                    same = self._output_checker.check_output(chunk.want,
                                                             res, 0)
                    if not same:
                        self.write('\n')
                        with mgr as maker:
                            maker.write('Warning, output different from '
                                        'expected:\n')
                            maker.write('================================'
                                        '========\n\n')
                            diff = checker.output_difference(chunk, res,
                                                             optionsflags)
                            maker.write(diff)
                        self.write('\n')
                    else:
                        self.write(res)
                elif chunk.exc_msg is None:
                    self.write('\n')
                    with mgr as maker:
                        maker.write('Warning, unexpected exception:\n')
                        maker.write('==============================\n\n')
                        maker.write(exc)
                    self.write('\n')
                else:
                    same_ex = checker.check_output(chunk.exc_msg,
                                                   exc, optionsflags)
                    if not same_ex:
                        self.write('\n')
                        with mgr as maker:
                            maker.write('Warning, exception different from '
                                        'expected:\n')
                            maker.write('=================================='
                                        '=========\n\n')
                            diff = checker.output_difference(chunk, res,
                                                             optionsflags)
                            maker.write(diff)
                        self.write('\n')
                    else:
                        self.write(res)
        else:
            if exc is not None:
                self.write('\n')
                with mgr as maker:
                    maker.write('Warning, unexpected exception:\n')
                    maker.write('==============================\n\n')
                    maker.write(exc)
                self.write('\n')
            else:
                self.write(res)

        if not self.pause:
            self.write(sys.ps1 + '\n')

    def no_echo_input(self, prompt):
        with warnings.catch_warnings():
            res = getpass.getpass(prompt)
        return res

    def interact(self, lit_string, name, pause=True, interactive=True):
        self.name = name
        self.pause = pause
        self.interactive = interactive

        try:
            sys.ps1
        except AttributeError:
            sys.ps1 = ">>> "

        try:
            sys.ps2
        except AttributeError:
            sys.ps2 = "... "

        try:
            sys.ps3
        except AttributeError:
            sys.ps3 = '>>> '

        if self._env_driver is not None:
            extra_locals = self._env_driver.setup()
            self.locals.update(extra_locals)
            extra_banner = self._env_driver.banner
            driver_text = " ({0})".format(self._env_driver.DRIVER_NAME)
        else:
            extra_banner = ""
            driver_text = ""

        cprt = ('Type "help", "copyright", "credits" or "license" for '
                'more information about Python.')
        self.write("Literate Python Shell{driver_text}\nPython {ver} "
                   "on {platform}\n{cprt}\n"
                   "{extra_banner}\n\n".format(driver_text=driver_text,
                                               ver=sys.version,
                                               platform=sys.platform,
                                               cprt=cprt,
                                               extra_banner=extra_banner))

        if not interactive and pause:
            self.write('Press enter to continue after a code block\n\n')

        try:
            parser = self.code_parser
            start = True
            self.chunks = list(parser.parse(lit_string, name))
            for chunk_ind, chunk in enumerate(self.chunks):
                if isinstance(chunk, parsers.CodeChunk):
                    self._run_code(chunk, chunk_ind)
                elif not chunk:
                    continue
                else:
                    if not start and pause and interactive:
                        self.filename = "<stdin>"
                        more = False
                        blanks = 0
                        while blanks < 2:
                            blank, more = self._interact_once(more)

                            if blank:
                                blanks += 1
                            else:
                                blanks = 0

                            if more is None:
                                return

                        # reset exc_msg so it doesn't get
                        # raised after the next code block
                        self.exc_msg = None
                    elif not start and pause:
                        self.no_echo_input(sys.ps3)

                    self.write(self.text_formatter.format(chunk))

                start = False

            complete_msg = ("\n{file} complete! Continuing to interactive "
                            "console...\n\n".format(file=self.name))

            if self.use_ansi:
                self.write(ansi.with_codes(complete_msg, 1))
            else:
                self.write(complete_msg)

            self.filename = "<stdin>"
            self.locals['con'] = self
            more = False
            while more is not None:
                blank, more = self._interact_once(more)
        finally:
            if self._env_driver is not None:
                self._env_driver.teardown()


# print absolute value of an integer
# python的语法比较简单,采用缩进方式
a = 100
if a>=0 :
    print(a)
else :
    print(-a)


"""
datauri, library for "data:" URIs as defined in RFC 2397.
"""

from .datauri import (  # noqa: F401
    DataURIError,
    discover,
    parse)


#   Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function

import sys

sys.path.append("..")
import unittest
import numpy as np
from op_test import OpTest
import paddle
import paddle.fluid as fluid
from paddle.fluid import compiler, Program, program_guard

#Situation 1: repeat_times is a list (without tensor)
class TestTileOpRank1(OpTest):

    def setUp(self):
        self.op_type = "tile"
        self.place = paddle.device.MLUPlace(0)
        self.__class__.use_mlu = True
        self.init_data()
        self.inputs = {'X': np.random.random(self.ori_shape).astype("float32")}
        self.attrs = {'repeat_times': self.repeat_times}
        output = np.tile(self.inputs['X'], self.repeat_times)
        self.outputs = {'Out': output}

    def init_data(self):
        self.ori_shape = [100]
        self.repeat_times = [2]

    def test_check_output(self):
        self.check_output_with_place(self.place)

    def test_check_grad(self):
        self.check_grad(['X'], 'Out')

# with dimension expanding
class TestTileOpRank2Expanding(TestTileOpRank1):

    def init_data(self):
        self.ori_shape = [120]
        self.repeat_times = [2, 2]

class TestTileOpRank2(TestTileOpRank1):

    def init_data(self):
        self.ori_shape = [12, 14]
        self.repeat_times = [2, 3]

class TestTileOpRank3_Corner(TestTileOpRank1):

    def init_data(self):
        self.ori_shape = (2, 10, 5)
        self.repeat_times = (1, 1, 1)

class TestTileOpRank3_Corner2(TestTileOpRank1):

    def init_data(self):
        self.ori_shape = (2, 10, 5)
        self.repeat_times = (2, 2)

class TestTileOpRank3(TestTileOpRank1):

    def init_data(self):
        self.ori_shape = (2, 4, 15)
        self.repeat_times = (2, 1, 4)

class TestTileOpRank4(TestTileOpRank1):

    def init_data(self):
        self.ori_shape = (2, 4, 5, 7)
        self.repeat_times = (3, 2, 1, 2)

# Situation 2: repeat_times is a list (with tensor)
class TestTileOpRank1_tensor_attr(OpTest):

    def setUp(self):
        self.op_type = "tile"
        self.place = paddle.device.MLUPlace(0)
        self.__class__.use_mlu = True
        self.init_data()
        repeat_times_tensor = []
        for index, ele in enumerate(self.repeat_times):
            repeat_times_tensor.append(("x" + str(index), np.ones(
                (1)).astype('int32') * ele))

        self.inputs = {
            'X': np.random.random(self.ori_shape).astype("float32"),
            'repeat_times_tensor': repeat_times_tensor,
        }
        self.attrs = {"repeat_times": self.infer_repeat_times}
        output = np.tile(self.inputs['X'], self.repeat_times)
        self.outputs = {'Out': output}

    def init_data(self):
        self.ori_shape = [100]
        self.repeat_times = [2]
        self.infer_repeat_times = [-1]

    def test_check_output(self):
        self.check_output_with_place(self.place)

    def test_check_grad(self):
        self.check_grad(['X'], 'Out')

class TestTileOpRank2_Corner_tensor_attr(TestTileOpRank1_tensor_attr):

    def init_data(self):
        self.ori_shape = [12, 14]
        self.repeat_times = [1, 1]
        self.infer_repeat_times = [1, -1]

class TestTileOpRank2_attr_tensor(TestTileOpRank1_tensor_attr):

    def init_data(self):
        self.ori_shape = [12, 14]
        self.repeat_times = [2, 3]
        self.infer_repeat_times = [-1, 3]

# Situation 3: repeat_times is a tensor
class TestTileOpRank1_tensor(OpTest):

    def setUp(self):
        self.op_type = "tile"
        self.place = paddle.device.MLUPlace(0)
        self.__class__.use_mlu = True
        self.init_data()
        self.inputs = {
            'X': np.random.random(self.ori_shape).astype("float32"),
            'RepeatTimes': np.array(self.repeat_times).astype("int32"),
        }
        self.attrs = {}
        output = np.tile(self.inputs['X'], self.repeat_times)
        self.outputs = {'Out': output}

    def init_data(self):
        self.ori_shape = [100]
        self.repeat_times = [2]

    def test_check_output(self):
        self.check_output_with_place(self.place)

    def test_check_grad(self):
        self.check_grad(['X'], 'Out')

class TestTileOpRank2_tensor(TestTileOpRank1_tensor):

    def init_data(self):
        self.ori_shape = [12, 14]
        self.repeat_times = [2, 3]

# Situation 4: input x is Integer
class TestTileOpInteger(OpTest):

    def setUp(self):
        self.op_type = "tile"
        self.place = paddle.device.MLUPlace(0)
        self.__class__.use_mlu = True
        self.inputs = {
            'X': np.random.randint(10, size=(4, 4, 5)).astype("int32")
        }
        self.attrs = {'repeat_times': [2, 1, 4]}
        output = np.tile(self.inputs['X'], (2, 1, 4))
        self.outputs = {'Out': output}

    def test_check_output(self):
        self.check_output_with_place(self.place)

# Situation 5: input x is Bool
class TestTileOpBoolean(OpTest):

    def setUp(self):
        self.op_type = "tile"
        self.place = paddle.device.MLUPlace(0)
        self.__class__.use_mlu = True
        self.inputs = {'X': np.random.randint(2, size=(2, 4, 5)).astype("bool")}
        self.attrs = {'repeat_times': [2, 1, 4]}
        output = np.tile(self.inputs['X'], (2, 1, 4))
        self.outputs = {'Out': output}

    def test_check_output(self):
        self.check_output_with_place(self.place)

# Situation 56: input x is Integer
class TestTileOpInt64_t(OpTest):

    def setUp(self):
        self.op_type = "tile"
        self.place = paddle.device.MLUPlace(0)
        self.__class__.use_mlu = True
        self.inputs = {
            'X': np.random.randint(10, size=(2, 4, 5)).astype("int64")
        }
        self.attrs = {'repeat_times': [2, 1, 4]}
        output = np.tile(self.inputs['X'], (2, 1, 4))
        self.outputs = {'Out': output}

    def test_check_output(self):
        self.check_output_with_place(self.place)

class TestTileError(unittest.TestCase):

    def test_errors(self):
        with program_guard(Program(), Program()):
            x1 = fluid.create_lod_tensor(np.array([[-1]]), [[1]],
                                         fluid.CPUPlace())
            repeat_times = [2, 2]
            self.assertRaises(TypeError, paddle.tile, x1, repeat_times)
            x2 = fluid.layers.data(name='x2', shape=[4], dtype="uint8")
            self.assertRaises(TypeError, paddle.tile, x2, repeat_times)
            x3 = fluid.layers.data(name='x3', shape=[4], dtype="bool")
            x3.stop_gradient = False
            self.assertRaises(ValueError, paddle.tile, x3, repeat_times)

class TestTileAPIStatic(unittest.TestCase):

    def test_api(self):
        with program_guard(Program(), Program()):
            repeat_times = [2, 2]
            x1 = fluid.layers.data(name='x1', shape=[4], dtype="int32")
            out = paddle.tile(x1, repeat_times)
            positive_2 = fluid.layers.fill_constant([1], dtype="int32", value=2)
            out2 = paddle.tile(x1, repeat_times=[positive_2, 2])

# Test python API
class TestTileAPI(unittest.TestCase):

    def test_api(self):
        with fluid.dygraph.guard():
            np_x = np.random.random([12, 14]).astype("float32")
            x = paddle.to_tensor(np_x)

            positive_2 = np.array([2]).astype("int32")
            positive_2 = paddle.to_tensor(positive_2)

            repeat_times = np.array([2, 3]).astype("int32")
            repeat_times = paddle.to_tensor(repeat_times)

            out_1 = paddle.tile(x, repeat_times=[2, 3])
            out_2 = paddle.tile(x, repeat_times=[positive_2, 3])
            out_3 = paddle.tile(x, repeat_times=repeat_times)

            assert np.array_equal(out_1.numpy(), np.tile(np_x, (2, 3)))
            assert np.array_equal(out_2.numpy(), np.tile(np_x, (2, 3)))
            assert np.array_equal(out_3.numpy(), np.tile(np_x, (2, 3)))

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


# Copyright 2016-2019 Dirk Thomas
# Licensed under the Apache License, Version 2.0

from pathlib import Path

import pytest
from scspell import Report
from scspell import SCSPELL_BUILTIN_DICT
from scspell import spell_check

spell_check_words_path = Path(__file__).parent / 'spell_check.words'

@pytest.fixture(scope='module')
def known_words():
    global spell_check_words_path
    return spell_check_words_path.read_text().splitlines()

def test_spell_check(known_words):
    source_filenames = [Path(__file__).parents[1] / 'setup.py'] + \
        list(
            (Path(__file__).parents[1] / 'colcon_metadata')
            .glob('**/*.py')) + \
        list((Path(__file__).parents[1] / 'test').glob('**/*.py'))

    for source_filename in sorted(source_filenames):
        print('Spell checking:', source_filename)

    # check all files
    report = Report(known_words)
    spell_check(
        [str(p) for p in source_filenames], base_dicts=[SCSPELL_BUILTIN_DICT],
        report_only=report, additional_extensions=[('', 'Python')])

    unknown_word_count = len(report.unknown_words)
    assert unknown_word_count == 0, \
        'Found {unknown_word_count} unknown words: '.format_map(locals()) + \
        ', '.join(sorted(report.unknown_words))

    unused_known_words = set(known_words) - report.found_known_words
    unused_known_word_count = len(unused_known_words)
    assert unused_known_word_count == 0, \
        '{unused_known_word_count} words in the word list are not used: ' \
        .format_map(locals()) + ', '.join(sorted(unused_known_words))

def test_spell_check_word_list_order(known_words):
    assert known_words == sorted(known_words), \
        'The word list should be ordered alphabetically'

def test_spell_check_word_list_duplicates(known_words):
    assert len(known_words) == len(set(known_words)), \
        'The word list should not contain duplicates'


#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bootProj.settings")
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


# -*- coding: utf-8 -*-
"""
    sphinx.builders.gettext
    ~~~~~~~~~~~~~~~~~~~~~~~

    The MessageCatalogBuilder class.

    :copyright: Copyright 2007-2015 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""

from __future__ import unicode_literals

from os import path, walk
from codecs import open
from time import time
from datetime import datetime, tzinfo, timedelta
from collections import defaultdict
from uuid import uuid4

from six import iteritems

from sphinx.builders import Builder
from sphinx.util import split_index_msg
from sphinx.util.nodes import extract_messages, traverse_translatable_index
from sphinx.util.osutil import safe_relpath, ensuredir, find_catalog, SEP
from sphinx.util.console import darkgreen, purple, bold
from sphinx.locale import pairindextypes

POHEADER = r"""
# SOME DESCRIPTIVE TITLE.
# Copyright (C) %(copyright)s
# This file is distributed under the same license as the %(project)s package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: %(project)s %(version)s\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: %(ctime)s\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

"""[1:]

class Catalog(object):
    """Catalog of translatable messages."""

    def __init__(self):
        self.messages = []  # retain insertion order, a la OrderedDict
        self.metadata = {}  # msgid -> file, line, uid

    def add(self, msg, origin):
        if not hasattr(origin, 'uid'):
            # Nodes that are replicated like todo don't have a uid,
            # however i18n is also unnecessary.
            return
        if msg not in self.metadata:  # faster lookup in hash
            self.messages.append(msg)
            self.metadata[msg] = []
        self.metadata[msg].append((origin.source, origin.line, origin.uid))

class MsgOrigin(object):
    """
    Origin holder for Catalog message origin.
    """

    def __init__(self, source, line):
        self.source = source
        self.line = line
        self.uid = uuid4().hex

class I18nBuilder(Builder):
    """
    General i18n builder.
    """
    name = 'i18n'
    versioning_method = 'text'
    versioning_compare = None  # be set by `gettext_uuid`

    def __init__(self, app):
        self.versioning_compare = app.env.config.gettext_uuid
        super(I18nBuilder, self).__init__(app)

    def init(self):
        Builder.init(self)
        self.catalogs = defaultdict(Catalog)

    def get_target_uri(self, docname, typ=None):
        return ''

    def get_outdated_docs(self):
        return self.env.found_docs

    def prepare_writing(self, docnames):
        return

    def compile_catalogs(self, catalogs, message):
        return

    def write_doc(self, docname, doctree):
        catalog = self.catalogs[find_catalog(docname,
                                             self.config.gettext_compact)]

        for node, msg in extract_messages(doctree):
            catalog.add(msg, node)

        if 'index' in self.env.config.gettext_enables:
            # Extract translatable messages from index entries.
            for node, entries in traverse_translatable_index(doctree):
                for typ, msg, tid, main in entries:
                    for m in split_index_msg(typ, msg):
                        if typ == 'pair' and m in pairindextypes.values():
                            # avoid built-in translated message was incorporated
                            # in 'sphinx.util.nodes.process_index_entry'
                            continue
                        catalog.add(m, node)

# determine tzoffset once to remain unaffected by DST change during build
timestamp = time()
tzdelta = datetime.fromtimestamp(timestamp) - \
    datetime.utcfromtimestamp(timestamp)

class LocalTimeZone(tzinfo):

    def __init__(self, *args, **kw):
        super(LocalTimeZone, self).__init__(*args, **kw)
        self.tzdelta = tzdelta

    def utcoffset(self, dt):
        return self.tzdelta

    def dst(self, dt):
        return timedelta(0)

ltz = LocalTimeZone()

class MessageCatalogBuilder(I18nBuilder):
    """
    Builds gettext-style message catalogs (.pot files).
    """
    name = 'gettext'

    def init(self):
        I18nBuilder.init(self)
        self.create_template_bridge()
        self.templates.init(self)

    def _collect_templates(self):
        template_files = set()
        for template_path in self.config.templates_path:
            tmpl_abs_path = path.join(self.app.srcdir, template_path)
            for dirpath, dirs, files in walk(tmpl_abs_path):
                for fn in files:
                    if fn.endswith('.html'):
                        filename = path.join(dirpath, fn)
                        filename = filename.replace(path.sep, SEP)
                        template_files.add(filename)
        return template_files

    def _extract_from_template(self):
        files = self._collect_templates()
        self.info(bold('building [%s]: ' % self.name), nonl=1)
        self.info('targets for %d template files' % len(files))

        extract_translations = self.templates.environment.extract_translations

        for template in self.app.status_iterator(
                files, 'reading templates... ', purple, len(files)):
            with open(template, 'r', encoding='utf-8') as f:
                context = f.read()
            for line, meth, msg in extract_translations(context):
                origin = MsgOrigin(template, line)
                self.catalogs['sphinx'].add(msg, origin)

    def build(self, docnames, summary=None, method='update'):
        self._extract_from_template()
        I18nBuilder.build(self, docnames, summary, method)

    def finish(self):
        I18nBuilder.finish(self)
        data = dict(
            version = self.config.version,
            copyright = self.config.copyright,
            project = self.config.project,
            ctime = datetime.fromtimestamp(
                timestamp, ltz).strftime('%Y-%m-%d %H:%M%z'),
        )
        for textdomain, catalog in self.app.status_iterator(
                iteritems(self.catalogs), "writing message catalogs... ",
                darkgreen, len(self.catalogs),
                lambda textdomain__: textdomain__[0]):
            # noop if config.gettext_compact is set
            ensuredir(path.join(self.outdir, path.dirname(textdomain)))

            pofn = path.join(self.outdir, textdomain + '.pot')
            pofile = open(pofn, 'w', encoding='utf-8')
            try:
                pofile.write(POHEADER % data)

                for message in catalog.messages:
                    positions = catalog.metadata[message]

                    if self.config.gettext_location:
                        # generate "#: file1:line1\n#: file2:line2 ..."
                        pofile.write("#: %s\n" % "\n#: ".join("%s:%s" %
                            (safe_relpath(source, self.outdir), line)
                            for source, line, _ in positions))
                    if self.config.gettext_uuid:
                        # generate "# uuid1\n# uuid2\n ..."
                        pofile.write("# %s\n" % "\n# ".join(
                            uid for _, _, uid in positions))

                    # message contains *one* line of text ready for translation
                    message = message.replace('\\', r'\\'). \
                                      replace('"', r'\"'). \
                                      replace('\n', '\\n"\n"')
                    pofile.write('msgid "%s"\nmsgstr ""\n\n' % message)

            finally:
                pofile.close()


import json
from pathlib import Path
from typing import List, Optional

class ConfJSON:
    def __init__(self, dir_path: Path, name: str, prefix: str):
        self._dir_path = dir_path
        self.name = f'{prefix.rstrip("-")}-{name}'
        self._file_path = dir_path / name
        self.path = str(self._file_path)
        self.write_json()

    def write_json(
            self,
            tests: Optional[List[str]] = None,
            skipped_tests: Optional[List[str]] = None,
            skipped_integrations: Optional[List[str]] = None,
            nightly_integrations: Optional[List[str]] = None,
            unmockable_integrations: Optional[List[str]] = None,
            docker_thresholds: Optional[dict] = None
    ):
        if tests is None:
            tests = []
        if skipped_tests is None:
            skipped_tests = None
        if skipped_integrations is None:
            skipped_integrations = []
        if nightly_integrations is None:
            nightly_integrations = []
        if unmockable_integrations is None:
            unmockable_integrations = []
        if docker_thresholds is None:
            docker_thresholds = {}
        self._file_path.write_text(json.dumps({
            'tests': tests,
            'skipped_tests': skipped_tests,
            'skipped_integrations': skipped_integrations,
            'nightly_integrations': nightly_integrations,
            'unmockable_integrations': unmockable_integrations,
            'docker_thresholds': docker_thresholds
        }))


#!/usr/bin/env python3
# Copyright (c) 2018-2020 The Wflscoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Verify that starting wflscoin with -h works as expected."""

from test_framework.test_framework import WflscoinTestFramework
from test_framework.util import assert_equal

class HelpTest(WflscoinTestFramework):
    def set_test_params(self):
        self.setup_clean_chain = True
        self.num_nodes = 1

    def setup_network(self):
        self.add_nodes(self.num_nodes)
        # Don't start the node

    def get_node_output(self, *, ret_code_expected):
        ret_code = self.nodes[0].process.wait(timeout=60)
        assert_equal(ret_code, ret_code_expected)
        self.nodes[0].stdout.seek(0)
        self.nodes[0].stderr.seek(0)
        out = self.nodes[0].stdout.read()
        err = self.nodes[0].stderr.read()
        self.nodes[0].stdout.close()
        self.nodes[0].stderr.close()

        # Clean up TestNode state
        self.nodes[0].running = False
        self.nodes[0].process = None
        self.nodes[0].rpc_connected = False
        self.nodes[0].rpc = None

        return out, err

    def run_test(self):
        self.log.info("Start wflscoin with -h for help text")
        self.nodes[0].start(extra_args=['-h'])
        # Node should exit immediately and output help to stdout.
        output, _ = self.get_node_output(ret_code_expected=0)
        assert b'Options' in output
        self.log.info("Help text received: {} (...)".format(output[0:60]))

        self.log.info("Start wflscoin with -version for version information")
        self.nodes[0].start(extra_args=['-version'])
        # Node should exit immediately and output version to stdout.
        output, _ = self.get_node_output(ret_code_expected=0)
        assert b'version' in output
        self.log.info("Version text received: {} (...)".format(output[0:60]))

        # Test that arguments not in the help results in an error
        self.log.info("Start wflscoind with -fakearg to make sure it does not start")
        self.nodes[0].start(extra_args=['-fakearg'])
        # Node should exit immediately and output an error to stderr
        _, output = self.get_node_output(ret_code_expected=1)
        assert b'Error parsing command line arguments' in output
        self.log.info("Error message received: {} (...)".format(output[0:60]))

if __name__ == '__main__':
    HelpTest().main()


import sys
import click
import json
import random

from .bakefile import Bakefile, TaskFilter
from .exceptions import NoBakefileFound
from .clint import eng_join

import pygments
import pygments.lexers
import pygments.formatters

from .constants import SKIP_NEXT, SAFE_ENVIRONS

def indent(line):
    return f'{" " * 4}{line}'

def do_help(exit=0):
    with click.Context(entrypoint) as ctx:
        help = entrypoint.get_help(ctx)
        help = help.replace(
            "  bake",
            str(click.style(" $ ", fg="green", bold=True))
            + str(click.style("bake", fg="yellow", bold=True)),
        )
        help = help.replace(
            "the strangely familiar task–runner",
            str(
                click.style("the strangely familiar task–runner", fg="white", bold=True)
            ),
        )
        help = help.replace(
            "Options", str(click.style("Options", fg="white", bold=True))
        )

        help = help.replace(
            "--insecure", str(click.style("--insecure", fg="red", bold=True))
        )
        help = help.replace("--yes", str(click.style("--yes", fg="red", bold=True)))
        help = help.replace(
            "--allow", str(click.style("--allow", fg="green", bold=True))
        )
        help = help.replace(
            "--no-deps", str(click.style("--no-deps", fg="yellow", bold=True))
        )
        help = help.replace(
            "--continue", str(click.style("--continue", fg="red", bold=True))
        )
        help = help.replace(
            "--environ-json", str(click.style("--environ-json", fg="green", bold=True))
        )
        help = help.replace("-e,", str(click.style("-e", fg="green", bold=True) + ","))

        click.echo(help, err=True)
        sys.exit(exit)

def echo_json(obj):
    _json = json.dumps(obj, indent=2)

    if sys.stdin.isatty():
        _json = pygments.highlight(
            _json, pygments.lexers.JsonLexer(), pygments.formatters.TerminalFormatter()
        )

    click.echo(_json, err=False)

@click.command(context_settings=dict(help_option_names=["-h", "--help"]))
@click.argument(
    "task",
    type=click.STRING,
    default="__LIST_ALL__",
    envvar="BAKE_TASK",
    # required=False,
)
@click.option(
    "--bakefile",
    "-b",
    default="__BAKEFILE__",
    envvar="BAKEFILE_PATH",
    nargs=1,
    type=click.Path(),
    help="The Bakefile to use.",
)
@click.option(
    "--list",
    "_list",
    default=False,
    is_flag=True,
    help="Lists available tasks (and their dependencies).",
)
@click.option(
    "--levels",
    "-l",
    default=None,
    nargs=1,
    type=click.INT,
    help="List only a given number of '/' levels of tasks.",
)
@click.option(
    "--help", "-h", default=False, is_flag=True, help="Show this message and exit."
)
@click.option("--debug", default=False, is_flag=True, hidden=True)
@click.option("--source", default=False, nargs=1, hidden=True)
@click.option(
    "--allow",
    default=False,
    nargs=1,
    multiple=True,
    hidden=False,
    help="Whitelist an environment variable for use.",
)
@click.option(
    "--no-deps",
    default=False,
    is_flag=True,
    hidden=False,
    help="Do not run dependent tasks.",
)
@click.option("--yes", is_flag=True, help="Set medium–security prompts to yes.")
@click.option(
    "--continue",
    "_continue",
    is_flag=True,
    # type=click.BOOL,
    help="Continue, if a task fails.",
)
@click.option(
    "--interactive",
    "-i",
    is_flag=True,
    # type=click.BOOL,
    help="Run in interactive mode.",
)
@click.option(
    "--insecure",
    is_flag=True,
    # type=click.BOOL,
    help="Inherit parent shell's environment variables.",
)
@click.argument("arguments", nargs=-1, type=click.STRING)
@click.option(
    "--silent",
    "-s",
    is_flag=True,
    # type=click.BOOL,
    help="Reduce output.",
    envvar="BAKE_SILENT",
)
@click.option(
    "--sort", is_flag=True, type=click.BOOL, help="Sort tasks, alphabetially."
)
@click.option(
    "--environ-json",
    "-e",
    nargs=1,
    type=click.STRING,
    help="Provide environment variables via JSON.",
)
@click.option(
    "--json",
    "-j",
    "_json",
    is_flag=True,
    # type=click.BOOL,
    help="Output in JSON format (stdout).",
)
def entrypoint(
    *,
    task,
    bakefile,
    arguments,
    _list,
    levels,
    _continue,
    environ_json,
    debug,
    silent,
    sort,
    insecure,
    allow,
    _json,
    no_deps,
    interactive,
    yes,
    help,
    source,
):
    """bake — the strangely familiar task–runner."""

    if help:
        do_help(0)

    # Default to list behavior, when no task is provided.
    if _json or source:
        silent = True

    # Allow explicitly–passed environment variables.
    SAFE_ENVIRONS.extend(allow)

    # Enable list functionality, by default.
    if task == "__LIST_ALL__":
        _list = True
        task = None

    # Establish the Bakefile.
    try:
        if bakefile == "__BAKEFILE__":
            bakefile = Bakefile.find(root=".", filename="Bakefile")
        else:
            bakefile = Bakefile(path=bakefile)

    except NoBakefileFound:
        click.echo(click.style("No Bakefile found!", fg="red"), err=True)
        do_help(1)
        sys.exit(0)

    if debug:
        click.echo(f" + Bakefile: {bakefile.path}", err=True)

    # --source (internal API)
    if source:

        def echo_generator(g):
            for g in g:
                click.echo(g)

        if source == "__init__":
            source = random.choice(list(bakefile.tasks.keys()))
            task = bakefile.tasks[source]
            source = task.gen_source(
                sources=[task.bashfile.funcs_source, task.bashfile.root_source]
            )
        else:
            task = bakefile.tasks[source]
            source = task.gen_source(
                sources=[
                    task.bashfile.funcs_source,
                    task.bashfile.root_source,
                    task.source,
                ]
            )

        for source_line in source:
            click.echo(source_line)
        sys.exit(0)

    if not insecure:
        for key in bakefile.environ:
            if key not in SAFE_ENVIRONS:
                del bakefile.environ[key]

    if environ_json:
        bakefile.add_environ_json(environ_json)

    argv = []
    environ = []

    for i, argument in enumerate(arguments[:]):
        if "=" in argument:
            key, value = argument.split("=", 1)
            environ.append((key, value))
        else:
            argv.append(argument)

    if debug:
        click.echo(f" + argv: {argv!r}", err=True)
        click.echo(f" + environ: {environ!r}", err=True)
        click.echo(err=True)
    for env in environ:
        key, value = env
        if debug:
            click.echo(
                f" + Setting environ: {click.style(key, fg='red')} {click.style('=', fg='white')} {value}.",
                err=True,
            )
        bakefile.add_environ(key, value)

    bakefile.add_args(*argv)

    if _list:
        __list_json = {"tasks": {}}

        # Enable level filtering.
        if levels is not None:
            task_list = []
            for _task in bakefile.tasks:
                if len(_task.split("/")) <= levels:
                    task_list.append(_task)
        else:
            task_list = bakefile.tasks

        if sort:
            task_list = sorted(task_list)

        for _task in task_list:
            depends_on = bakefile[_task].depends_on(
                include_filters=False, recursive=True
            )

            if no_deps:
                depends_on = ()

            if depends_on:
                deps = [str(a) for a in depends_on]
                deps = f"\n    {click.style('+', fg='yellow', bold=True)} {eng_join(deps, conj='&')}."
            else:
                deps = ""
            colon = "" if not deps else "…"

            __list_json["tasks"].update(
                {_task: {"depends_on": [str(d) for d in depends_on]}}
            )

            if not silent:
                click.echo(
                    f" {click.style('-', fg='green', bold=True)} {click.style(_task, bold=True)}{colon}{deps}",
                    err=False,
                )

        if not silent:
            tasks_unechoed = len(bakefile.tasks) - len(task_list)

            if tasks_unechoed:
                bake_command = str(click.style(f"bake --levels {levels + 1}", fg="red"))
                click.echo(
                    f"Note: {tasks_unechoed} more tasks are available. "
                    f"Please use $ {bake_command} to see more.",
                    err=True,
                )

        if _json:
            echo_json(__list_json)

        sys.exit(0)

    if task:
        try:
            task = bakefile[task]
        except KeyError:
            click.echo(click.style(f"Task {task} does not exist!", fg="red"))
            sys.exit(1)

        def execute_task(task, *, silent=False):
            try:
                edges = list(bakefile.graph.out_edges(task))[0]
            except IndexError:
                edges = list()

            skips = []
            for edge in edges:
                if edge.do_skip is not None:
                    skips.append(edge.do_skip)

            if not all(skips or [False]):
                # TODO: fully implement this?
                if "@" in f"{task}":
                    silent = True

                if not silent:
                    click.echo(
                        click.style(" + ", fg="white")
                        + click.style(f"Executing {task}", fg="yellow")
                        + click.style(":", fg="white"),
                        err=True,
                    )
                usually_bash_task = task.execute(
                    yes=yes, debug=debug, silent=silent, interactive=interactive
                )

                if not _continue:
                    if hasattr(usually_bash_task, "ok"):

                        if usually_bash_task.return_code > 0:
                            if not silent:
                                click.echo(
                                    click.style(f"Task {task} failed!", fg="red"),
                                    err=True,
                                )
                            sys.exit(usually_bash_task.return_code)

                    # This happens when it's a task filter.
                    elif isinstance(usually_bash_task, tuple):
                        key, value = (
                            usually_bash_task
                        )  # But, in this instance, clearly isn't.
            else:
                click.echo(
                    click.style(" + ", fg="green")
                    + click.style(f"Skipping {task}", fg="white")
                    + click.style(".", fg="white"),
                    err=True,
                )

        if not no_deps:
            tasks = task.depends_on(recursive=True) + [task]

        else:
            tasks = [task]

        for task in tasks:
            execute_task(task, silent=silent)

        if not silent:
            click.echo(
                click.style(" + ", fg="white")
                + click.style("Done", fg="green")
                + click.style(".", fg="white"),
                err=True,
            )
        sys.exit(0)

if __name__ == "__main__":
    entrypoint()


#!/usr/bin/python3

ARD_DEVICE_ID = "1"

ADC_READ_INTERVAL = 0.1
ADC_KEEP_VALS = 20
LIGHT_KEEP_VALS = 20

LOGFILE = "./logs/smarthome.log"

# --- Unicodes --- #
HOME = ' ⌂'
HOMEON = ' ☗'
KEY = ' ⚿'
CLOUD = ' ☁'
STAR = ' ★'
SUN = ' ☀'
REFRESH = ' 🗘'
CHECKED = ' ✔'
POINT = ' ☛'
MISSING = ' ✘'
MODESSYMBOL = ' ❖'
MENUSYMBOL = ' ☰'
TERMOMETER = ' 🌡'
MUSIC = ' ♫'
EMAIL = ' ✉'
NOTIFICATION = ' 🔔'
DEGREES = ' °C'
SMILE = ' ☺'
MOON = ' ☾'
QUATER_MOON = '☽'
PEACE = ' ☮'

# --- Modes --- #

LIGHT_TRESHOLD = 400


# the required python libraries imported
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from trcrpm import TRCRP_Mixture
from collections import Counter
print "fuck yeah"
data = pd.read_csv("./data/anomaly0245.csv", index_col=0)
data = data.iloc[156600:240000].reset_index(drop=True)

# Setup the placekeeping and initilizing variables
chain = 0
x, eng_val, states, num_states = [], [], [], []
i = 0
step = 30
print(i)

rng = np.random.RandomState(1)
model = TRCRP_Mixture(chains=1, lag=10, variables=data.columns, rng=rng)
model.incorporate(data[i:i + step])
model.resample_all(seconds=10)
model.resample_hyperparameters(seconds=10)
s = model.get_temporal_regimes('anomaly')[chain]
num_states = step * [len(sorted(set(s)))]
states = list(s[i:i + step])
eng_val = data.iloc[i:i + step, 0].tolist()
x = list(range(i, i + step ))

for i in range(step, len(data) - step, step):
    model.incorporate(data[i:i + step])
    model.resample_all(seconds=10)
    model.resample_hyperparameters(seconds=10)
    s = model.get_temporal_regimes("anomaly")[chain]
    num_states = step * [len(sorted(set(s)))]
    states = list(s[i:i + step])
    eng_val = data.iloc[i:i + step, 0].tolist()
    x = list(range(i, i + step ))
    print(i)


import pytest
import os
import utils
import io
import numpy
import json
import pickle
import gzip

from utils import kfp_client_utils
from utils import minio_utils
from utils import sagemaker_utils

def run_predict_mnist(boto3_session, endpoint_name, download_dir):
    """ https://github.com/awslabs/amazon-sagemaker-examples/blob/a8c20eeb72dc7d3e94aaaf28be5bf7d7cd5695cb
        /sagemaker-python-sdk/1P_kmeans_lowlevel/kmeans_mnist_lowlevel.ipynb """
    # Download and load dataset
    region = boto3_session.region_name
    download_path = os.path.join(download_dir, "mnist.pkl.gz")
    boto3_session.resource("s3", region_name=region).Bucket(
        "sagemaker-sample-data-{}".format(region)
    ).download_file("algorithms/kmeans/mnist/mnist.pkl.gz", download_path)
    with gzip.open(download_path, "rb") as f:
        train_set, valid_set, test_set = pickle.load(f, encoding="latin1")

    # Function to create a csv from numpy array
    def np2csv(arr):
        csv = io.BytesIO()
        numpy.savetxt(csv, arr, delimiter=",", fmt="%g")
        return csv.getvalue().decode().rstrip()

    # Run prediction on an image
    runtime = boto3_session.client("sagemaker-runtime")
    payload = np2csv(train_set[0][30:31])

    response = runtime.invoke_endpoint(
        EndpointName=endpoint_name, ContentType="text/csv", Body=payload,
    )
    return json.loads(response["Body"].read().decode())

@pytest.mark.parametrize(
    "test_file_dir",
    [
        pytest.param(
            "resources/config/kmeans-mnist-endpoint", marks=pytest.mark.canary_test
        )
    ],
)
def test_create_endpoint(
    kfp_client, experiment_id, boto3_session, sagemaker_client, test_file_dir
):

    download_dir = utils.mkdir(os.path.join(test_file_dir + "/generated"))
    test_params = utils.load_params(
        utils.replace_placeholders(
            os.path.join(test_file_dir, "config.yaml"),
            os.path.join(download_dir, "config.yaml"),
        )
    )

    # Generate random prefix for model, endpoint config and endpoint name
    # to avoid errors if resources with same name exists
    test_params["Arguments"]["model_name"] = test_params["Arguments"][
        "endpoint_config_name"
    ] = test_params["Arguments"]["endpoint_name"] = input_endpoint_name = (
        utils.generate_random_string(5) + "-" + test_params["Arguments"]["model_name"]
    )
    print(f"running test with model/endpoint name: {input_endpoint_name}")

    _, _, workflow_json = kfp_client_utils.compile_run_monitor_pipeline(
        kfp_client,
        experiment_id,
        test_params["PipelineDefinition"],
        test_params["Arguments"],
        download_dir,
        test_params["TestName"],
        test_params["Timeout"],
    )

    try:
        outputs = {"sagemaker-deploy-model": ["endpoint_name"]}

        output_files = minio_utils.artifact_download_iterator(
            workflow_json, outputs, download_dir
        )

        output_endpoint_name = utils.read_from_file_in_tar(
            output_files["sagemaker-deploy-model"]["endpoint_name"]
        )
        print(f"endpoint name: {output_endpoint_name}")

        # Verify output from pipeline is endpoint name
        assert output_endpoint_name == input_endpoint_name

        # Verify endpoint is running
        assert (
            sagemaker_utils.describe_endpoint(sagemaker_client, input_endpoint_name)[
                "EndpointStatus"
            ]
            == "InService"
        )

        # Validate the model for use by running a prediction
        result = run_predict_mnist(boto3_session, input_endpoint_name, download_dir)
        print(f"prediction result: {result}")
        assert json.dumps(result, sort_keys=True) == json.dumps(
            test_params["ExpectedPrediction"], sort_keys=True
        )
        utils.remove_dir(download_dir)
    finally:
        # delete endpoint
        sagemaker_utils.delete_endpoint(sagemaker_client, input_endpoint_name)


# proxy module
from pyface.ui.wx.grid.trait_grid_cell_adapter import *


default_app_config = 'sandbox.order.apps.OrderConfig'


import random

class PriQ(object):
    '''Binary-Heap based Priority Queue with uniquely named elements, name may \
be any hashable type.  Defaults to min-heap, set maxpq=True for max-heap. \
Public methods: put, get, remove, update, contains, front, get_priority.'''

    def __init__(self, maxpq = False):
        self.q =[]          # The priority queue, contains (priority, name) tuples
        self.elements = {}  # Dict of all elements currently in queue, with current index
        self.maxmod = -1 if maxpq else 1   # modify pq to max instead of min

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

    def __str__(self):
        return str(self.q)

    def _propUp(self, index):
        '''Propagate up element to proper place in binary heap'''
        current = index
        while current > 0:
            parent = (current - 1) // 2             # parent node
            # swap with parent until parent <= child (>= for maxPQ)
            if self.q[parent][0] * self.maxmod <= self.q[current][0] * self.maxmod:
                break
            self.elements[self.q[current][1]], self.elements[self.q[parent][1]] = parent, current
            self.q[parent], self.q[current] = self.q[current], self.q[parent]
            current = parent

    def _propDown(self, index):
        '''Propagate down element to proper place in binary heap'''
        if len(self.q) == 1:
            self.elements[self.q[0][1]] = 0         # update index of last element
        current = index
        while current * 2 + 1 < len(self.q):        # node has a child
            left, right = current * 2 + 1, current * 2 + 2
            if right == len(self.q):                # left child only
                if self.q[current][0] * self.maxmod >= self.q[left][0] * self.maxmod:
                    # swap with left child and update elements dict
                    self.elements[self.q[current][1]], self.elements[self.q[left][1]] = left, current
                    self.q[current], self.q[left] = self.q[left], self.q[current]
                break
            if self.maxmod == 1:    # min PQ
                minChild = left if self.q[left] <= self.q[right] else right
                if self.q[current] <= self.q[minChild]: # swap with lowest priority child as needed
                    break
                self.elements[self.q[current][1]], self.elements[self.q[minChild][1]] = minChild, current
                self.q[current], self.q[minChild] = self.q[minChild], self.q[current]
                current = minChild
            else:                   # max PQ
                maxChild = left if self.q[left] >= self.q[right] else right
                if self.q[current] >= self.q[maxChild]: # swap with highest priority child as needed
                    break
                self.elements[self.q[current][1]], self.elements[self.q[maxChild][1]] = maxChild, current
                self.q[current], self.q[maxChild] = self.q[maxChild], self.q[current]
                current = maxChild

    def put(self, name, priority):
        '''Add named element to priorty queue and place in binary heap'''
        if self.contains(name): return ValueError(name)
        self.q.append((priority, name))
        self.elements[name] = len(self.q) - 1
        self._propUp(self.elements[name])

    def front(self):
        '''Element at front of queue'''
        return self.q[0]

    def get_priority(self, name):
        '''Current priority of named element'''
        return self.q[self.elements[name]][0]

    def get(self):
        '''Return element at front of queue and re-heapify queue'''
        if not self.q:
            return False # empty queue
        result = self.q[0]
        del(self.elements[result[1]])
        if len(self.q) > 1:
            self.q[0] = self.q.pop()
            self._propDown(0)
        else:
            self.q = []
            self.elements = {}
        return result

    def update(self, name, priority):
        '''Change priority of named element and re-heapify'''
        if not self.contains(name): return ValueError(name)
        index = self.elements[name]
        old_priority = self.q[index][0]
        self.q[index] = (priority, name)
        if priority * self.maxmod < old_priority * self.maxmod:
            self._propUp(index)
        if priority * self.maxmod > old_priority * self.maxmod:
            self._propDown(index)

    def contains(self, name):
        '''True if name currently exists in the queue'''
        return (name in self.elements)

    def remove(self, name):
        '''Remove named element and re-heapify'''
        if not self.contains(name): return ValueError(name)
        index = self.elements[name]
        old_priority = self.q[index][0]
        del(self.elements[name])
        if len(self.q) > 1:
            self.q[index] = self.q.pop()    # replace with last item in queue
            self.elements[self.q[index][1]] = index
            # re-heapify
            if self.q[index][0] * self.maxmod < old_priority * self.maxmod:
                self._propUp(index)
            elif self.q[index][0] * self.maxmod > old_priority * self.maxmod:
                self._propDown(index)
        else:
            self.q = []

###############################################################################
# Following are examples, including Dijkstra's shortest path
###############################################################################

if __name__ == "__main__":
    # Sequential letter names with random integer weights (demonstration)
    pq = PriQ()
    name = 'a'
    for _ in range(26):
        pq.put(name, random.randint(1,99))
        name = chr(ord(name) + 1)
    print("Initial: ['a'-'z' = randint(1,99)]\n", pq, "\nLength: ", len(pq))
    print("Priority of 'a':", pq.get_priority('a'))
    print("Front:", pq.front())
    pq.put('a', 17) # testing for duplicate put
    pq.update('a', 1)
    pq.update('b', 99)
    print("After updating 'a' to 1 and 'b' to 99:\n", pq)
    pq.remove('c')
    print("After removing 'c':\n", pq)
    print("Contains 'c'?", pq.contains('c'))
    print("Get elements until empty:")
    while pq:
        print(pq.get(), "contains 'd'?", pq.contains('d'))

    '''
    # Max-heap priority queue with random float weights and integer names
    pq = PriQ(True)
    for i in range(20):
        pq.put(i, random.random())
    print("['1'-'20' = random()]:\n", pq)
    while pq: print(pq.get())
    '''

    # Dijkstra's shortest path algorithm example
    def dsp(G, s):
        '''Dijkstra's shortest path algorithm.  Input: weighted graph G, an\
    adjacency list mapping each named node to it's neighbors with a {name: weight}\
    dict, and s, the name of the starting node.  Outputs a mapping for each node\
    to a tuple of (weight of shortest path, name of predecessor node).
        '''
        pq = PriQ()
        result = {}
        predecessor = {s: None}
        for key in G.keys():
            pq.put(key, 0 if key == s else float('inf'))
        while pq:
            cur_weight, cur_node = pq.get()
            result[cur_node] = (cur_weight, predecessor[cur_node])
            for adj in G[cur_node]:     # for neighboring nodes
                # update weight from starting node if less than previous paths
                if adj not in result:
                    if pq.get_priority(adj) > cur_weight + G[cur_node][adj]:
                        pq.update(adj, cur_weight + G[cur_node][adj])
                        predecessor[adj] = cur_node
        return result

    G = {
        'a': {'b': 2, 'f': 6, 'c': 1},
        'b': {'a': 2, 'd': 6, 'f': 3, 'g': 4},
        'c': {'a': 1, 'f': 2, 'g': 3, 'e': 5},
        'd': {'b': 6, 'f': 2, 'g': 1, 'h': 2},
        'e': {'c': 5, 'f': 4, 'g': 2, 'h': 3},
        'f': {'a': 6, 'b': 3, 'd': 2, 'g': 7, 'e': 4, 'c': 2},
        'g': {'f': 7, 'b': 4, 'd': 1, 'h': 5, 'e': 2, 'c': 3},
        'h': {'d': 2, 'g': 5, 'e': 3}
        }
    print(
    '''
        B  6  D
      2 3 2 4 1 2
    A 6 F  7  G 5 H
      1 2 3 4 2 3
        C  5  E
    ''')
    print("\nCalculating shortest paths with Dijkstra's algorithm...")
    print("Shortest paths from 'a':", dsp(G, 'a'))
    print("Shortest paths from 'd':", dsp(G, 'd'))


# Lint as: python3
# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""An abstract layer for processing sequences step-by-step.

E.g.::

  def ProcessSeq(step, external_inputs, input_batch):
    prepared_inputs = step.PrepareExternalInputs(
        step.theta, external_inputs)
    batch_size, T = tf.shape(input_batch.paddings)[:2]
    state = step.ZeroState(
        step.theta, prepared_inputs, batch_size)
    for t in range(T):
      step_inputs = input_batch.Transform(lambda x: x[:, i, ...])
      step_outputs, state = step.FProp(
          step.theta, prepared_inputs, step_inputs, state)
      (processing step_outputs...)
"""

import collections

from lingvo import compat as tf
from lingvo.core import base_layer
from lingvo.core import builder_layers
from lingvo.core import py_utils
from lingvo.core import recurrent

class Step(base_layer.BaseLayer):
  """A layer that processes input sequences step-by-step.

  This can be seen as an RNNCell extended with optional external inputs.
  """

  def PrepareExternalInputs(self, theta, external_inputs):
    """Returns the prepared external inputs, e.g., packed_src for attention."""
    if not external_inputs:
      external_inputs = py_utils.NestedMap()
    packed = external_inputs.DeepCopy()
    for name, child in self.children.items():
      child_external_inputs = external_inputs.get(name, py_utils.NestedMap())
      if isinstance(child, (tuple, list)):
        output = []
        for i, sub in enumerate(child):
          if isinstance(sub, Step):
            output.append(
                sub.PrepareExternalInputs(theta[name][i],
                                          child_external_inputs))
        if output:
          if len(output) != len(child):
            raise ValueError('Expecting child list to be instances of Step.')
          packed[name] = type(child)(output)
      elif isinstance(child, Step):
        packed[name] = child.PrepareExternalInputs(theta[name],
                                                   child_external_inputs)
    return packed

  def ZeroState(self, theta, prepared_inputs, batch_size):
    """Returns the initial state given external inputs and batch size.

    Args:
      theta: A `.NestedMap` object containing weights' values of this layer and
        its children layers.
      prepared_inputs: External inputs returned by PrepareExternalInputs().
      batch_size: An int scalar representing the batch size of per-step inputs.

    Returns:
      A `.NestedMap` representing the initial state, which can be passed to
      FProp() for processing the first time step.
    """
    state0 = py_utils.NestedMap()
    for name, child in self.children.items():
      if isinstance(child, (tuple, list)):
        output = []
        for i, sub in enumerate(child):
          if isinstance(sub, Step):
            output.append(
                sub.ZeroState(theta[name][i], prepared_inputs[name][i],
                              batch_size))
        if output:
          if len(output) != len(child):
            raise ValueError('Expecting child list to be instances of Step.')
          state0[name] = type(child)(output)
      elif isinstance(child, Step):
        state0[name] = child.ZeroState(theta[name], prepared_inputs[name],
                                       batch_size)
    return state0

  def FProp(self, theta, prepared_inputs, step_inputs, padding, state0):
    """Forward function.

    step_inputs, state0, step_outputs, and state1 should each be a `.NestedMap`
    of tensor values. Each tensor must be of shape [batch_size ...]. The
    structure of NestedMaps are determined by the implementation. state0 and
    state1 must have exactly the same structure and tensor shapes.

    Args:
      theta: A `.NestedMap` object containing weights' values of this layer and
        its children layers.
      prepared_inputs: External inputs returned by PrepareExternalInputs().
      step_inputs: The inputs for this time step.
      padding: A 0/1 float tensor of shape [batch_size]; 1.0 means that this
        batch element is empty in this step.
      state0: The previous recurrent state.

    Returns:
      A tuple (step_outputs, state1).

      - outputs: The outputs of this step.
      - state1: The next recurrent state.
    """
    raise NotImplementedError(type(self))

class StatelessLayerStep(Step):
  """Allows BaseLayer subclasses to be used as Steps.

  Layers used with this class should be stateless: they should not return
  anything that must be passed back in the next invocation.
  """

  @classmethod
  def Params(cls):
    p = super().Params()
    p.Define('layer', None, 'Params for the layer that this step wraps.')
    return p

  def __init__(self, params):
    super().__init__(params)
    p = params
    self.CreateChild('layer', p.layer)

  def FProp(self, theta, prepared_inputs, step_inputs, padding, state0):
    """Perform inference on a stateless layer.

    Args:
      theta: A `.NestedMap` object containing weights' values of this layer and
        its children layers.
      prepared_inputs: unused.
      step_inputs: A NestedMap containing 'inputs', which are passed directly to
        the layer.
      padding: A 0/1 float tensor of shape [batch_size]; 1.0 means that this
        batch element is empty in this step.
      state0: unused.

    Returns:
      (output, state1), where output is the output of the layer, and
      state1 is an empty NestedMap.
    """
    del state0
    del prepared_inputs
    args = {}
    if padding is not None:
      args['padding'] = padding
    output = self.layer.FProp(theta.layer, step_inputs.inputs, **args)
    return output, py_utils.NestedMap()

class StackStep(Step):
  """A stack of steps.

  Each sub-step is assumed to accept step_inputs of type NestedMap(inputs=[])
  and return a primary output of type NestedMap(output=tensor). The
  output of layer n-1 is sent to input of layer n.

  Per-step context vectors and per-sequence context vectors can also be
  supplied; see FProp for more details.
  """

  @classmethod
  def Params(cls):
    p = super().Params()
    p.Define(
        'sub', [], 'A list of sub-stack params. Each layer is '
        'expected to accept its input as NestedMap(inputs=[]), and '
        'produce output as NestedMap(output=tensor). '
        'The external_inputs parameter is passed directly to the '
        'PrepareExternalInputs method of each sub-step. ')
    p.Define(
        'residual_start', -1, 'An index of the layer where residual '
        'connections start. Setting this parameter to a negative value turns '
        'off residual connections.'
        'More precisely, when i >= residual_start, the output of each step '
        'is defined as: '
        'output[i] = output[i - residual_stride] + sub[i](output[i - 1]) '
        'where output[-1] is the step input.')
    p.Define(
        'residual_stride', 1, 'If residual connections are active, this '
        'is the number of layers that each connection skips. For '
        'instance, setting residual_stride = 2 means the output of layer '
        'n is added to layer n + 2')
    return p

  def __init__(self, params):
    super().__init__(params)
    p = params
    self.sub_steps = []
    self.CreateChildren('sub', p.sub)

  def PrepareExternalInputs(self, theta, external_inputs):
    """Delegates external inputs preparation to sub-layers.

    Args:
      theta: A `.NestedMap` object containing weights' values of this layer and
        its children layers.
      external_inputs: A `.NestedMap` object. The structure of the internal
        fields is defined by the sub-steps.

    Returns:
      A `.NestedMap` containing a pre-processed version of the external_inputs,
      one per sub-step.
    """
    packed = py_utils.NestedMap(sub=[])
    for i in range(len(self.sub)):
      packed.sub.append(self.sub[i].PrepareExternalInputs(
          theta.sub[i], external_inputs))
    return packed

  def ZeroState(self, theta, prepared_inputs, batch_size):
    """Computes a zero state for each sub-step.

    Args:
      theta: A `.NestedMap` object containing weights' values of this layer and
        its children layers.
      prepared_inputs: An output from PrepareExternalInputs.
      batch_size: The number of items in the batch that FProp will process.

    Returns:
      A `.NestedMap` containing a state0 object for each sub-step.
    """
    state = py_utils.NestedMap(sub=[])
    for i in range(len(self.sub)):
      state.sub.append(self.sub[i].ZeroState(theta.sub[i], prepared_inputs,
                                             batch_size))
    return state

  def FProp(self, theta, prepared_inputs, step_inputs, padding, state0):
    """Performs inference on the stack of sub-steps.

    There are three possible ways to feed input to the stack:

      * step_inputs.inputs: These tensors are fed only to the lowest layer.
      * step_inputs.context: [Optional] This tensor is fed to every layer.
      * prepared_inputs: [Optional] This tensor is fed to every layer and
          is assumed to stay constant over all steps.

    Args:
      theta: A `.NestedMap` object containing weights' values of this layer and
        its children layers.
      prepared_inputs: An output from PrepareExternalInputs.
      step_inputs: A `.NestedMap` containing a list called 'inputs', an
        optionally a tensor called 'context'.
      padding: A 0/1 float tensor of shape [batch_size]; 1.0 means that this
        batch element is empty in this step.
      state0: The previous recurrent state.

    Returns:
      A tuple (output, state1):

      - output: A `.NestedMap` containing the output of the top-most step.
      - state1: The recurrent state to feed to next invocation of this graph.
    """
    state1 = py_utils.NestedMap(sub=[])
    inputs = list(step_inputs.inputs)
    # We pretend that the input is the output of layer -1 for the purposes
    # of residual connections.
    residual_inputs = [tf.concat(inputs, axis=1)]
    additional = []
    if 'context' in step_inputs:
      additional.append(step_inputs.context)
    for i in range(len(self.sub)):
      sub_inputs = py_utils.NestedMap(inputs=inputs + additional)
      sub_output, state1_i = self.sub[i].FProp(theta.sub[i],
                                               prepared_inputs.sub[i],
                                               sub_inputs, padding,
                                               state0.sub[i])
      state1.sub.append(state1_i)
      output = sub_output.output
      if i >= self.params.residual_start >= 0:
        # residual_inputs contains the step input at residual_inputs[0].
        assert i + 1 - self.params.residual_stride < len(residual_inputs)
        output += residual_inputs[i + 1 - self.params.residual_stride]
      residual_inputs.append(output)
      inputs = [output]
    return py_utils.NestedMap(output=output), state1

class ParallelStep(Step):
  """Runs many steps on the same input and concatenates their outputs."""

  @classmethod
  def Params(cls):
    p = super().Params()
    p.Define(
        'sub', [], 'A list of step params. Each step is '
        'expected to accept its input as NestedMap(inputs=[]), and '
        'produce output as NestedMap(output=tensor). '
        'The external_inputs parameter is passed directly to the '
        'PrepareExternalInputs method of each sub-step. ')
    return p

  def __init__(self, params):
    super().__init__(params)
    p = params
    self.CreateChildren('sub', p.sub)

  def FProp(self, theta, prepared_inputs, step_inputs, padding, state0):
    """Performs inference on N steps at once and concatenates the result.

    Args:
      theta: A `.NestedMap` object containing weights' values of this layer and
        its children layers.
      prepared_inputs: An output from PrepareExternalInputs.
      step_inputs: A `.NestedMap` containing a list called 'inputs'.
      padding: A 0/1 float tensor of shape [batch_size]; 1.0 means that this
        batch element is empty in this step.
      state0: The previous recurrent state.

    Returns:
      A tuple (output, state1):

      - output: A `.NestedMap` containing the output of the top-most step.
      - state1: The recurrent state to feed to next invocation of this graph.
    """
    state1 = py_utils.NestedMap(sub=[None] * len(self.sub))
    outputs = [None] * len(self.sub)

    for i in range(len(self.sub)):
      outputs[i], state1.sub[i] = self.sub[i].FProp(theta.sub[i],
                                                    prepared_inputs.sub[i],
                                                    step_inputs, padding,
                                                    state0.sub[i])

    output = py_utils.NestedMap(output=tf.concat(outputs, axis=1))
    return output, state1

# signature: A GraphSignature string defining the input and output parameters
#   of this step. For example, (inputs=[a,b])->c means that step_inputs
#   should be NestedMap(inputs=[a,b]), and the output of FProp should be
#   stored in c.
# external_signature: A GraphSignature string defining the input to
#   PrepareExternalInputs. For example, 'external_inputs.foo' means that
#   the tensor external_inputs.foo should be the 'external_inputs' parameter
#   when calling PrepareExternalInputs on this sub-step.
# params: The parameters to use when constructing the sub-step.
SubStep = collections.namedtuple('SubStep',
                                 ['signature', 'external_signature', 'params'])

class GraphStep(Step):
  r"""A step that connects sub-steps in a simple data flow graph.

  This is an adaptation of builder_layers.GraphLayer to support steps.

  Params.sub specifies a list of Specs that define each sub-step.

  A spec contains:

    * step_inputs: The signature describing how to assemble the input and output
      for this step. The input part describes the 'step_inputs' parameter,
      while the output part describes the name of the output. The state0
      input and state1 output are handled automatically and should not be
      specified.
    * external_inputs: if this Step requires external_inputs, this
      is the signature describing how to find those inputs.
      This value can also be set to None.
    * params: the params used to construct the sub-step.

  The format of signature strings is defined in detail in the GraphSignature
  class documentation.

  All inputs to a layer must have been produced by some previous layer. No
  cycles are allowed. All outputs must be uniquely named; no overwriting
  of previous names is allowed.

  Example
    ('(act=[layer_0.output,step_inputs.context])->layer_1',
     'external_inputs.extra',
     step_params)

  This constructs the step defined by step_params. Its FProp method will be
  called with {act=[layer_0.output,step_inputs.context]} as the step_inputs
  parameter. Its PrepareExternalInputs method will be called with
  'external_inputs.extra' as the external_inputs parameter. The output of that
  method will be passed to ZeroState and FProp.
  """

  @classmethod
  def Params(cls):
    p = super().Params()
    p.Define('output_signature', '', 'Signature of the step output.')
    p.Define('sub', [], 'A list of SubSteps (defined above).')
    p.Define('dict_type', py_utils.NestedMap, 'Type of nested dicts.')
    return p

  _seq = collections.namedtuple(
      '_Seq', ['name', 'signature', 'external_signature', 'step'])

  def __init__(self, params):
    super().__init__(params)
    p = self.params
    assert p.name
    self._seq = []
    for i, (signature, external_signature, sub_params) in enumerate(p.sub):
      assert signature
      sig = builder_layers.GraphSignature(signature)
      assert len(sig.inputs) == 1
      assert sig.outputs
      external_sig = None
      if external_signature:
        external_sig = builder_layers.GraphSignature(external_signature)
        assert len(external_sig.inputs) == 1
        assert not external_sig.outputs
      name = sub_params.name
      if not name:
        name = '%s_%02d' % (sig.outputs[0], i)
        sub_params.name = name
      self.CreateChild(name, sub_params)
      self._seq.append(
          GraphStep._seq(name, sig, external_sig, self.children[name]))
    self.output_signature = builder_layers.GraphSignature(p.output_signature)

  def PrepareExternalInputs(self, theta, external_inputs):
    """Prepares external inputs for each sub-step.

    The external_inputs parameter of this method is processed by the
    external_inputs of each sub-step, then processed by the sub-step's
    PrepareExternalInputs method.

    Args:
      theta: variables used by sub-steps.
      external_inputs: A NestedMap of [n_batch, ...] tensors.

    Returns:
      A NestedMap of prepared inputs, where the keys are the names of
        each sub-step.
    """
    graph_tensors = builder_layers.GraphTensors()
    graph_tensors.StoreTensor('external_inputs', external_inputs)
    prepared_inputs = py_utils.NestedMap()
    with tf.name_scope(self.params.name):
      for seq in self._seq:
        if seq.external_signature:
          template = py_utils.NestedMap(inputs=seq.external_signature.inputs)
          packed = template.Transform(graph_tensors.GetTensor)
          seq_external_inputs = packed.inputs[0]
          prepared_inputs[seq.name] = seq.step.PrepareExternalInputs(
              theta[seq.name], seq_external_inputs)
        else:
          prepared_inputs[seq.name] = py_utils.NestedMap()
    return prepared_inputs

  def ZeroState(self, theta, prepared_inputs, batch_size):
    """Creates a zero state NestedMap for this step.

    Args:
      theta: variables used by sub-steps.
      prepared_inputs: Output from a call to PrepareExternalInputs.
      batch_size: The number of items in the batch that FProp will process.

    Returns:
      A NestedMap of ZeroState results for each sub-step.
    """
    state0 = py_utils.NestedMap()
    with tf.name_scope(self.params.name):
      for seq in self._seq:
        state0[seq.name] = seq.step.ZeroState(theta[seq.name],
                                              prepared_inputs[seq.name],
                                              batch_size)
    return state0

  def FProp(self, theta, prepared_inputs, step_inputs, padding, state0):
    """A single inference step for this step graph.

    Args:
      theta: variables used by sub-steps.
      prepared_inputs: A NestedMap containing external_inputs that were
        pre-processed by the PrepareExternalInputs method of each sub-step. The
        keys are the names of the sub-steps.
      step_inputs: A NestedMap of [batch, ...] tensors. The structure of this
        depends on the graph implementation.
      padding: A 0/1 float tensor of shape [batch_size]; 1.0 means that this
        batch element is empty in this step.
      state0: A NestedMap of state variables produced by either ZeroState or a
        previous invocation of this FProp step. The keys are the names of the
        sub-steps.

    Returns:
      (output, state1), both of which are NestedMaps.
      output is implementation-dependent and is defined by the output_signature
      parameter.
      state1 is a NestedMap where the keys are names of sub-steps and the values
      are state outputs from their FProp methods.
    """
    p = self.params
    graph_tensors = builder_layers.GraphTensors()
    graph_tensors.StoreTensor('prepared_inputs', prepared_inputs)
    graph_tensors.StoreTensor('step_inputs', step_inputs)
    state1 = py_utils.NestedMap()
    with tf.name_scope(p.name):
      for seq in self._seq:
        tf.logging.vlog(1, 'GraphStep: call %s', seq.name)
        external = None
        if seq.external_signature:
          external = prepared_inputs[seq.name]
        template = py_utils.NestedMap(inputs=seq.signature.inputs)
        packed = template.Transform(graph_tensors.GetTensor)
        input_args = packed.inputs[0]
        out, seq_state1 = seq.step.FProp(theta[seq.name], external, input_args,
                                         padding, state0[seq.name])
        graph_tensors.StoreTensor(seq.signature.outputs[0], out)
        state1[seq.name] = seq_state1
    template = py_utils.NestedMap(inputs=self.output_signature.inputs)
    output_tensors = template.Transform(graph_tensors.GetTensor).inputs[0]
    return output_tensors, state1

class IteratorStep(Step):
  """An iterator over the time dimension of some tensors.

  It's common to have a tensor of shape [batch, time, ...] or
  [time, batch, ...]. This object will step through the time dimension,
  producing tensors of shape [batch, ...] in succession.

  The input tensors are passed to PrepareExternalInputs. The step_inputs
  argument of FProp is unused.
  """

  @classmethod
  def Params(cls):
    p = super().Params()
    p.Define('axis', 1, 'The time dimension of the tensors.')
    return p

  def PrepareExternalInputs(self, theta, external_inputs):
    """Prepares the input for iteration.

    Args:
      theta: unused.
      external_inputs: A NestedMap containing tensors. The time axis of each
        tensor should be params.axis.

    Returns:
      A prepared NestedMap (current the same as the input).
    """
    return external_inputs

  def ZeroState(self, theta, prepared_inputs, batch_size):
    """Returns the initial iterator state.

    Args:
      theta: unused.
      prepared_inputs: Output from a call to PrepareExternalInputs.
      batch_size: The number of items in the batch that FProp will process.

    Returns:
      An initial state NestedMap.
    """
    return py_utils.NestedMap(t=tf.constant(0, dtype=tf.int32))

  def FProp(self, theta, prepared_inputs, step_inputs, padding, state0):
    """Returns a A single inference step for this step graph.

    Args:
      theta: unused.
      prepared_inputs: Output from a call to PrepareExternalInputs.
      step_inputs: unused.
      padding: unused.
      state0: A NestedMap of state variables produced by either ZeroState or a
        previous invocation of this FProp step.

    Returns:
      (output, state1), both of which are NestedMaps.
      output is implementation-dependent and is defined by the output_signature
      parameter.
      state1 is a NestedMap where the keys are names of sub-steps and the values
      are state outputs from their FProp methods.
    """
    del theta
    del step_inputs
    del padding

    def _Slice(tensor):
      """Return a slice of this tensor at time=state0.t."""
      shape = py_utils.GetShape(tensor)
      # All zeros except for t in the time dimension.
      # e.g. if params.axis=1, begin is [0, t, 0, 0, 0, ...]
      begin = tf.one_hot(self.params.axis, tf.rank(tensor), on_value=state0.t)
      # Same as shape, but with a 1 in the time dimension.
      # e.g. if params.axis=1, shape is [shape[0], 1, shape[2], shape[3], ...]
      size = tf.concat([
          shape[0:self.params.axis],
          tf.constant([1], dtype=tf.int32), shape[self.params.axis + 1:]
      ],
                       axis=0)
      # Make a slice where the time dimension is fixed at state0.t.
      time_slice = tf.slice(tensor, begin, size)
      # Remove the time dimension.
      return tf.squeeze(time_slice, axis=self.params.axis)

    output = prepared_inputs.Transform(_Slice)
    state1 = py_utils.NestedMap(t=state0.t + 1)
    return output, state1

class RecurrentStepWrapper(base_layer.BaseLayer):
  """A layer that wraps a step in a recurrent.Recurrent call."""

  @classmethod
  def Params(cls):
    p = super().Params()
    p.Define('step', None, 'The step params that this class wraps.')
    return p

  def __init__(self, params):
    super().__init__(params)
    self.CreateChild('step', self.params.step)

  def _CreateChildrenVariables(self):
    # Backwards compatibility: manually call child.InstantiateVariables()
    # outside of tf.variable_scope(p.name).
    self.step.InstantiateVariables()
    super()._CreateChildrenVariables()

  def PrepareExternalInputs(self, theta, external_inputs):
    """See Step.PrepareExternalInputs."""
    return self.step.PrepareExternalInputs(theta.step, external_inputs)

  def ZeroState(self, theta, prepared_inputs, batch_size):
    """See Step.ZeroState."""
    return self.step.ZeroState(theta.step, prepared_inputs, batch_size)

  def FProp(self, theta, prepared_inputs, inputs, padding, state0, **kwargs):
    """Runs a Step layer over multiple timesteps using Recurrent.

    Args:
      theta: A NestedMap containing weights' values of this layer and its
        children layers.
      prepared_inputs: External inputs returned by Step.PrepareExternalInputs().
      inputs: A NestedMap of inputs of shape [time, batch_size, dim].
      padding: A 0/1 float tensor of shape [time, batch_size]; 1.0 means that
        this batch element is empty in this step.
      state0: A NestedMap containing the initial recurrent state.
      **kwargs: Additional kwargs to pass to Recurrent.

    Returns:
      A tuple (outputs, state1).

      - outputs: A NestedMap containing the accumulated outputs of all steps,
        containing Tensors shaped [time, batch_size, dim].
      - state1: A NestedMap containing the accumulated recurrent states,
        containing Tensors shaped [time, batch_size, dim].
    """

    def RnnStep(recurrent_theta, recurrent_state0, recurrent_inputs):
      """Compute a single timestep."""
      output, state1 = self.step.FProp(
          theta=recurrent_theta.theta,
          prepared_inputs=recurrent_theta.prepared_inputs,
          step_inputs=recurrent_inputs.inputs,
          padding=recurrent_inputs.padding,
          state0=recurrent_state0.state)
      recurrent_state1 = py_utils.NestedMap(output=output, state=state1)
      return recurrent_state1, py_utils.NestedMap()

    # In order to pass Step outputs through Recurrent, they need to be
    # included as part of state.
    output0, _ = self.step.FProp(theta.step, prepared_inputs,
                                 inputs.Transform(lambda x: x[0]), padding[0],
                                 state0)

    accumulated_states, _ = recurrent.Recurrent(
        theta=py_utils.NestedMap(
            theta=theta.step, prepared_inputs=prepared_inputs),
        state0=py_utils.NestedMap(output=output0, state=state0),
        inputs=py_utils.NestedMap(inputs=inputs, padding=padding),
        cell_fn=RnnStep,
        **kwargs)

    return accumulated_states.output, accumulated_states.state


# USAGE
# python facial_landmarks.py --shape-predictor shape_predictor_68_face_landmarks.dat --image images/example_01.jpg

# import the necessary packages
from imutils import face_utils
import numpy as np
import argparse
import imutils
import dlib
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--shape-predictor", required=True,
	help="path to facial landmark predictor")
ap.add_argument("-i", "--image", required=True,
	help="path to input image")
args = vars(ap.parse_args())

# initialize dlib's face detector (HOG-based) and then create
# the facial landmark predictor
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(args["shape_predictor"])

# load the input image, resize it, and convert it to grayscale
image = cv2.imread(args["image"])
image = imutils.resize(image, width=500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# detect faces in the grayscale image
rects = detector(gray, 1)

# loop over the face detections
for (i, rect) in enumerate(rects):
	# determine the facial landmarks for the face region, then
	# convert the facial landmark (x, y)-coordinates to a NumPy
	# array
	shape = predictor(gray, rect)
	shape = face_utils.shape_to_np(shape)

	# convert dlib's rectangle to a OpenCV-style bounding box
	# [i.e., (x, y, w, h)], then draw the face bounding box
	(x, y, w, h) = face_utils.rect_to_bb(rect)
	cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

	# show the face number
	cv2.putText(image, "Face #{}".format(i + 1), (x - 10, y - 10),
		cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

	# loop over the (x, y)-coordinates for the facial landmarks
	# and draw them on the image
	for (x, y) in shape:
		cv2.circle(image, (x, y), 1, (0, 0, 255), -1)

# show the output image with the face detections + facial landmarks
cv2.imshow("Output", image)
cv2.waitKey(0)


# -*- coding: utf-8 -*-
# ----------------------------------------------------------------------------
# Copyright © 2021, Spyder Bot
#
# Licensed under the terms of the MIT license
# ----------------------------------------------------------------------------
"""
Status bar widgets setup.
"""
from setuptools import find_packages
from setuptools import setup

from status_bar_widgets import __version__

setup(
    # See: https://setuptools.readthedocs.io/en/latest/setuptools.html
    name="status-bar-widgets",
    version=__version__,
    author="Spyder Bot",
    author_email="spyder.python@gmail.com",
    description="Example that shows how to add wigets to the status bar",
    license="MIT license",
    url="https://github.com/spyder-bot/status-bar-widgets",
    python_requires='>= 3.7',
    install_requires=[
        "qtpy",
        "qtawesome",
        "spyder>=5.1.1",
    ],
    packages=find_packages(),
    entry_points={
        "spyder.plugins": [
            "status_bar_widgets = status_bar_widgets.spyder.plugin:StatusbarWidgets"
        ],
    },
    classifiers=[
        "Operating System :: MacOS",
        "Operating System :: Microsoft :: Windows",
        "Operating System :: POSIX :: Linux",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.7",
        "Programming Language :: Python :: 3.8",
        "Programming Language :: Python :: 3.9",
        "Development Status :: 5 - Production/Stable",
        "Intended Audience :: Education",
        "Intended Audience :: Science/Research",
        "Intended Audience :: Developers",
        "Topic :: Scientific/Engineering",
    ],
)


"""Support for Homematic thermostats."""
import logging

from homeassistant.components.climate import ClimateDevice
from homeassistant.components.climate.const import (
    HVAC_MODE_AUTO,
    HVAC_MODE_HEAT,
    HVAC_MODE_OFF,
    PRESET_BOOST,
    PRESET_COMFORT,
    PRESET_ECO,
    SUPPORT_PRESET_MODE,
    SUPPORT_TARGET_TEMPERATURE,
)
from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS

from . import ATTR_DISCOVER_DEVICES, HM_ATTRIBUTE_SUPPORT, HMDevice

_LOGGER = logging.getLogger(__name__)

HM_TEMP_MAP = ["ACTUAL_TEMPERATURE", "TEMPERATURE"]

HM_HUMI_MAP = ["ACTUAL_HUMIDITY", "HUMIDITY"]

HM_PRESET_MAP = {
    "BOOST_MODE": PRESET_BOOST,
    "COMFORT_MODE": PRESET_COMFORT,
    "LOWERING_MODE": PRESET_ECO,
}

HM_CONTROL_MODE = "CONTROL_MODE"
HMIP_CONTROL_MODE = "SET_POINT_MODE"

SUPPORT_FLAGS = SUPPORT_TARGET_TEMPERATURE | SUPPORT_PRESET_MODE

def setup_platform(hass, config, add_entities, discovery_info=None):
    """Set up the Homematic thermostat platform."""
    if discovery_info is None:
        return

    devices = []
    for conf in discovery_info[ATTR_DISCOVER_DEVICES]:
        new_device = HMThermostat(conf)
        devices.append(new_device)

    add_entities(devices)

class HMThermostat(HMDevice, ClimateDevice):
    """Representation of a Homematic thermostat."""

    @property
    def supported_features(self):
        """Return the list of supported features."""
        return SUPPORT_FLAGS

    @property
    def temperature_unit(self):
        """Return the unit of measurement that is used."""
        return TEMP_CELSIUS

    @property
    def hvac_mode(self):
        """Return hvac operation ie. heat, cool mode.

        Need to be one of HVAC_MODE_*.
        """
        if self.target_temperature <= self._hmdevice.OFF_VALUE + 0.5:
            return HVAC_MODE_OFF
        if "MANU_MODE" in self._hmdevice.ACTIONNODE:
            if self._hm_control_mode == self._hmdevice.MANU_MODE:
                return HVAC_MODE_HEAT
            return HVAC_MODE_AUTO

        # Simple devices
        if self._data.get("BOOST_MODE"):
            return HVAC_MODE_AUTO
        return HVAC_MODE_HEAT

    @property
    def hvac_modes(self):
        """Return the list of available hvac operation modes.

        Need to be a subset of HVAC_MODES.
        """
        if "AUTO_MODE" in self._hmdevice.ACTIONNODE:
            return [HVAC_MODE_AUTO, HVAC_MODE_HEAT, HVAC_MODE_OFF]
        return [HVAC_MODE_HEAT, HVAC_MODE_OFF]

    @property
    def preset_mode(self):
        """Return the current preset mode, e.g., home, away, temp."""
        if self._data.get("BOOST_MODE", False):
            return "boost"

        # Get the name of the mode
        mode = HM_ATTRIBUTE_SUPPORT[HM_CONTROL_MODE][1][self._hm_control_mode]
        mode = mode.lower()

        # Filter HVAC states
        if mode not in (HVAC_MODE_AUTO, HVAC_MODE_HEAT):
            return None
        return mode

    @property
    def preset_modes(self):
        """Return a list of available preset modes."""
        preset_modes = []
        for mode in self._hmdevice.ACTIONNODE:
            if mode in HM_PRESET_MAP:
                preset_modes.append(HM_PRESET_MAP[mode])
        return preset_modes

    @property
    def current_humidity(self):
        """Return the current humidity."""
        for node in HM_HUMI_MAP:
            if node in self._data:
                return self._data[node]

    @property
    def current_temperature(self):
        """Return the current temperature."""
        for node in HM_TEMP_MAP:
            if node in self._data:
                return self._data[node]

    @property
    def target_temperature(self):
        """Return the target temperature."""
        return self._data.get(self._state)

    def set_temperature(self, **kwargs):
        """Set new target temperature."""
        temperature = kwargs.get(ATTR_TEMPERATURE)
        if temperature is None:
            return None

        self._hmdevice.writeNodeData(self._state, float(temperature))

    def set_hvac_mode(self, hvac_mode):
        """Set new target hvac mode."""
        if hvac_mode == HVAC_MODE_AUTO:
            self._hmdevice.MODE = self._hmdevice.AUTO_MODE
        elif hvac_mode == HVAC_MODE_HEAT:
            self._hmdevice.MODE = self._hmdevice.MANU_MODE
        elif hvac_mode == HVAC_MODE_OFF:
            self._hmdevice.turnoff()

    def set_preset_mode(self, preset_mode: str) -> None:
        """Set new preset mode."""
        if preset_mode == PRESET_BOOST:
            self._hmdevice.MODE = self._hmdevice.BOOST_MODE
        elif preset_mode == PRESET_COMFORT:
            self._hmdevice.MODE = self._hmdevice.COMFORT_MODE
        elif preset_mode == PRESET_ECO:
            self._hmdevice.MODE = self._hmdevice.LOWERING_MODE

    @property
    def min_temp(self):
        """Return the minimum temperature."""
        return 4.5

    @property
    def max_temp(self):
        """Return the maximum temperature."""
        return 30.5

    @property
    def target_temperature_step(self):
        """Return the supported step of target temperature."""
        return 0.5

    @property
    def _hm_control_mode(self):
        """Return Control mode."""
        if HMIP_CONTROL_MODE in self._data:
            return self._data[HMIP_CONTROL_MODE]
        # Homematic
        return self._data["CONTROL_MODE"]

    def _init_data_struct(self):
        """Generate a data dict (self._data) from the Homematic metadata."""
        self._state = next(iter(self._hmdevice.WRITENODE.keys()))
        self._data[self._state] = None

        if (
            HM_CONTROL_MODE in self._hmdevice.ATTRIBUTENODE
            or HMIP_CONTROL_MODE in self._hmdevice.ATTRIBUTENODE
        ):
            self._data[HM_CONTROL_MODE] = None

        for node in self._hmdevice.SENSORNODE.keys():
            self._data[node] = None