import copy
from typing import Dict, List, Optional

import six
from django.conf import settings
from django.utils.functional import cached_property
from django.utils.translation import ugettext as _

from bkmonitor.utils.country import CHINESE_PROVINCE_MAP, COUNTRY_MAP, ISP_MAP

def _split_member_list(member_str):
    """
    将字符串类型的人员数据转换为列表
    :param member_str: 人员数据字符串，例如 a,b,c,d
    :return: list[str]
    """
    if not member_str:
        return []
    if not isinstance(member_str, six.string_types):
        return member_str
    return [member for member in member_str.split(",") if member]

class BaseNode:
    """
    基础节点
    """

    def __init__(self, attrs: dict):
        super().__setattr__("_extra_attr", attrs)

    def __getattribute__(self, item: str):
        try:
            return super().__getattribute__(item)
        except AttributeError as e:
            attrs = super().__getattribute__("_extra_attr")
            if item in attrs:
                return attrs[item]
            raise e

    def __setattr__(self, key: str, value):
        attrs = super().__getattribute__("_extra_attr")
        if key not in attrs:
            super().__setattr__(key, value)
        else:
            attrs[key] = value

    def __getitem__(self, item):
        return getattr(self, item)

    def __setitem__(self, key, value):
        setattr(self, key, value)

class TopoNode(object):
    """
    拓扑节点
    {
        "bk_inst_id": 2,
        "bk_inst_name": "blueking",
        "bk_obj_id": "biz",
        "bk_obj_name": "business",
    }
    """

    def __init__(self, bk_obj_id, bk_inst_id, bk_obj_name="", bk_inst_name="", **kwargs):
        """
        :param str bk_obj_id: 节点对象(层级)ID
        :param int bk_inst_id: 节点实例ID
        :param str bk_obj_name: 节点对象(层级)名称
        :param str bk_inst_name: 节点实例名称
        """
        self.bk_inst_id = int(bk_inst_id)
        self.bk_inst_name = bk_inst_name or ""
        self.bk_obj_id = bk_obj_id or ""
        self.bk_obj_name = bk_obj_name or ""

    @property
    def id(self):
        return "{}|{}".format(self.bk_obj_id, self.bk_inst_id)

    def __eq__(self, other):
        return (self.bk_inst_id, self.bk_obj_id) == (other.bk_inst_id, other.bk_obj_id)

    def __hash__(self):
        return hash((self.bk_inst_id, self.bk_obj_id))

    def __repr__(self):
        return "<TopoNode: {}({})-{}({})>".format(self.bk_obj_name, self.bk_obj_id, self.bk_inst_name, self.bk_inst_id)

    def get_attrs(self):
        node_attrs = {}
        for key, value in self.__dict__.items():
            if key == "_extra_attr":
                node_attrs.update(value)
            else:
                node_attrs[key] = value
        return node_attrs

class Business(TopoNode):
    """
    业务
    {
        "bk_biz_id": 2,
        "language": "1",
        "life_cycle": "2",
        "bk_biz_developer": ["layman", "durant2"],
        "bk_biz_maintainer": ["admin", "layman", "durant2", "selina-test"],
        "bk_biz_tester": ["duranttest"],
        "time_zone": "Asia/Shanghai",
        "default": 0,
        "create_time": "2019-08-12T17:47:37.737+08:00",
        "bk_biz_productor": ["duranttest", "durant2"],
        "bk_supplier_account": "0",
        "operator": "",
        "bk_biz_name": "蓝鲸",
        "last_time": "2019-08-26T21:13:19.464+08:00",
        "bk_supplier_id": 0
    }
    """

    def __init__(
        self,
        bk_biz_id,
        bk_biz_name="",
        bk_biz_developer=None,
        bk_biz_maintainer=None,
        bk_biz_tester=None,
        bk_biz_productor=None,
        operator=None,
        time_zone="Asia/Shanghai",
        **kwargs,
    ):
        """
        :param int bk_biz_id: 业务ID
        :param str or unicode bk_biz_name: 业务名称
        :param str time_zone: 时区
        """
        super(Business, self).__init__(
            bk_obj_id="biz", bk_obj_name="business", bk_inst_id=bk_biz_id, bk_inst_name=bk_biz_name, **kwargs
        )
        self.bk_biz_id = int(bk_biz_id)
        if not bk_biz_name:
            space_name = kwargs.get("space_name", "")
            space_id = kwargs.get("space_id", "")
            if space_id and space_name:
                bk_biz_name = f"[{space_id}]{space_name}"

        self.bk_biz_name = bk_biz_name
        self.bk_biz_developer = _split_member_list(bk_biz_developer)
        self.bk_biz_maintainer = _split_member_list(bk_biz_maintainer)
        self.bk_biz_tester = _split_member_list(bk_biz_tester)
        self.bk_biz_productor = _split_member_list(bk_biz_productor)
        self.operator = _split_member_list(operator)
        self.time_zone = kwargs.get("extend", {}).get("time_zone") or time_zone
        self.language = kwargs.pop("language", "1")
        self.life_cycle = kwargs.pop("life_cycle", "2")

        # 将自定义属性置于外层，便于通过 __dict__ 取到
        for field in kwargs.copy():
            if not hasattr(self, field):
                setattr(self, field, kwargs.pop(field))

        # 自定义属性
        self._extra_attr = kwargs

    def __getattr__(self, item):
        """
        获取自定义属性
        """
        if item == "_extra_attr":
            return super(Business, self).__getattribute__(item)
        if item in self._extra_attr:
            return self._extra_attr[item]
        return super(Business, self).__getattribute__(item)

    def __repr__(self):
        return "<Business: {}-{}>".format(self.bk_biz_id, self.display_name)

    @property
    def display_name(self):
        # bk_biz_name:
        # "蓝鲸"
        # "[xxx]xx系统"

        # display_name:
        # '[xxx]xx系统 (研发项目)'
        # '[2]蓝鲸 (业务)'
        type_name = getattr(self, "type_name", "业务")
        return (f"[{self.bk_biz_id}]{self.bk_biz_name} " if self.bk_biz_id > 0 else self.bk_biz_name) + f"({type_name})"

class Set(TopoNode):
    """
    模块(最底层的拓扑节点)
    """

    def __init__(self, bk_set_id, bk_set_name: str = "", set_template_id: int = 0, **kwargs):
        """
        :param int bk_set_id: 模块ID
        :param str bk_set_name: 模块名称
        """
        super(Set, self).__init__(
            bk_obj_id="set", bk_obj_name="set", bk_inst_id=bk_set_id, bk_inst_name=bk_set_name, **kwargs
        )
        self.bk_set_id = int(bk_set_id)
        self.bk_set_name = bk_set_name or ""
        self.set_template_id = set_template_id

        self._extra_attr = kwargs

    def __getattr__(self, item):
        """
        获取自定义属性
        """
        if item == "_extra_attr":
            return super(Set, self).__getattribute__(item)
        if item in self._extra_attr:
            return self._extra_attr[item]
        return super(Set, self).__getattribute__(item)

    def __repr__(self):
        return "<Set: {}-{}>".format(self.bk_set_id, self.bk_set_name)

class Module(TopoNode):
    """
    模块(最底层的拓扑节点)
    """

    def __init__(
        self,
        bk_module_id,
        bk_module_name="",
        service_category_id=0,
        service_template_id=0,
        operator=None,
        bk_bak_operator=None,
        **kwargs,
    ):
        """
        :param int bk_module_id: 模块ID
        :param str bk_module_name: 模块名称
        :param int service_category_id: 服务分类ID
        :param int service_template_id: 服务实例ID
        :param list[str] operator: 主负责人
        :param list[str] bk_bak_operator: 备份负责人
        """
        super(Module, self).__init__(
            bk_obj_id="module", bk_obj_name="module", bk_inst_id=bk_module_id, bk_inst_name=bk_module_name, **kwargs
        )
        self.bk_module_id = int(bk_module_id)
        self.bk_module_name = bk_module_name or ""
        self.service_category_id = int(service_category_id)
        self.service_template_id = int(service_template_id)
        self.operator = operator or []
        self.bk_bak_operator = bk_bak_operator or []

        # 自定义属性
        self._extra_attr = kwargs

    def __getattr__(self, item):
        """
        获取自定义属性
        """
        if item == "_extra_attr":
            return super(Module, self).__getattribute__(item)
        if item in self._extra_attr:
            return self._extra_attr[item]
        return super(Module, self).__getattribute__(item)

    def __repr__(self):
        return "<Module: {}-{}>".format(self.bk_module_id, self.bk_module_name)

class TopoTree(TopoNode):
    """
    拓扑树
    {
        "bk_inst_id": 2,
        "bk_inst_name": "blueking",
        "bk_obj_id": "biz",
        "bk_obj_name": "business",
        "child": [
            {
                "bk_inst_id": 3,
                "bk_inst_name": "job",
                "bk_obj_id": "set",
                "bk_obj_name": "set",
                "child": [
                    {
                        "bk_inst_id": 5,
                        "bk_inst_name": "job",
                        "bk_obj_id": "module",
                        "bk_obj_name": "module",
                        "child": []
                    }
                ]
            }
        ]
    }
    """

    def __init__(self, tree_data, parent=None):
        child = tree_data.pop("child", [])
        super(TopoTree, self).__init__(**tree_data)
        self._parent = parent
        # 子节点信息
        self.child = [TopoTree(c, parent=copy.deepcopy(self)) for c in child]

    def get_node_info(self):
        return TopoNode(**self.__dict__)

    @classmethod
    def get_leaf_nodes(cls, tree, leaf_nodes):
        """
        找出所有叶子节点
        :param TopoTree tree: 拓扑树对象
        :param list leaf_nodes: 叶子节点
        """
        if not tree.child:
            # 没有子节点说明是叶子节点
            leaf_nodes.append(tree)
            return

        for child in tree.child:
            cls.get_leaf_nodes(child, leaf_nodes)

    def convert_to_topo_link(self) -> Dict[str, List[TopoNode]]:
        """
        将拓扑树对象转换为拓扑链，拓扑链从叶子节点开始，自底向上
        :return: example: {
            "module|1": [<TopoNode module-1>, <TopoNode set-2>, <TopoNode biz-3>],
            "module|2": [<TopoNode module-1>, <TopoNode set-2>, <TopoNode biz-3>],
        }
        """
        leaf_nodes = []  # type: list[TopoTree]
        self.get_leaf_nodes(self, leaf_nodes)

        topo_link_dict = {}
        for node in leaf_nodes:
            # 向上遍历父节点
            topo_link = []
            while True:
                topo_link.append(node.get_node_info())
                if not node._parent:
                    break
                node = node._parent
            topo_link_dict[topo_link[0].id] = topo_link
        return topo_link_dict

    @classmethod
    def get_all_nodes(cls, tree, nodes):
        """
        获取所有节点
        :param TopoTree tree: 拓扑树对象
        :param list nodes: 节点列表
        """
        nodes.append(tree.get_node_info())

        if not tree.child:
            # 没有子节点说明是叶子节点
            return

        for child in tree.child:
            cls.get_all_nodes(child, nodes)

    def get_all_nodes_with_relation(self):
        """
        获取所有节点（带关联信息）
        """
        nodes = {}
        queue = [self]
        while queue:
            topo = queue.pop()
            nodes[f"{topo.bk_obj_id}|{topo.bk_inst_id}"] = topo
            queue.extend(topo.child)

        return nodes

    def convert_to_flat_nodes(self):
        """
        将拓扑树扁平化为多个节点
        :return:
        """
        nodes = []  # type: list[TopoTree]
        self.get_all_nodes(self, nodes)
        return nodes

class Host(BaseNode):
    """
    主机
    """

    bk_host_id: int
    bk_agent_id: str

    Fields = (
        "bk_host_innerip",
        "bk_host_innerip_v6",
        "bk_cloud_id",
        "bk_host_id",
        "bk_biz_id",
        "bk_agent_id",
        "bk_host_outerip",
        "bk_host_outerip_v6",
        "bk_host_name",
        "bk_os_name",
        "bk_os_type",
        "operator",
        "bk_bak_operator",
        "bk_state_name",
        "bk_isp_name",
        "bk_province_name",
        "bk_supplier_account",
        "bk_state",
        "bk_os_version",
        "service_template_id",
        "srv_status",
        "bk_comment",
        "idc_unit_name",
        "net_device_id",
        "rack_id",
        "bk_svr_device_cls_name",
        "svr_device_class",
        "docker_client_version",
        "docker_server_version",
        "bk_mem",
        "bk_disk",
        "bk_os_bit",
        "bk_os_version",
        "bk_cpu_module",
        "bk_cpu",
    )

    def __init__(self, attrs: Optional[dict] = None, **kwargs):
        if attrs is None:
            attrs = {}
        attrs.update(kwargs)

        # 主要必须字段的默认值补全
        attrs["ip"] = attrs["bk_host_innerip"]
        attrs["bk_state"] = attrs.get("srv_status") or attrs.get("bk_state") or ""
        attrs["bk_set_ids"] = attrs.get("bk_set_ids") or []
        attrs["bk_module_ids"] = attrs.get("bk_module_ids") or []
        for field in self.Fields:
            if field in ["bk_cloud_id", "bk_host_id", "bk_biz_id"]:
                attrs[field] = int(attrs.get(field) or 0)
                continue
            empty_value = ""
            if field in ["operator", "bk_bak_operator"]:
                empty_value = []
            attrs[field] = attrs.get(field) or empty_value
        for field in settings.HOST_DYNAMIC_FIELDS:
            attrs[field] = attrs.get(field) or ""

        # 补充展示字段display_name
        for field in settings.HOST_DISPLAY_FIELDS:
            if attrs.get(field):
                if field == "bk_host_innerip_v6":
                    value = attrs[field]
                else:
                    value = attrs[field]
                attrs["display_name"] = value
                break
        if not attrs.get("display_name"):
            attrs["display_name"] = attrs["bk_host_innerip"] or attrs["bk_host_innerip_v6"]

        super().__init__(attrs)

    @cached_property
    def ignore_monitoring(self):
        return self.bk_state in settings.HOST_DISABLE_MONITOR_STATES

    @cached_property
    def is_shielding(self):
        return self.bk_state in settings.HOST_DISABLE_NOTICE_STATES

    @cached_property
    def bk_province_name(self):
        name = self._extra_attr.get("bk_province_name", "")
        if name in CHINESE_PROVINCE_MAP:
            return str(CHINESE_PROVINCE_MAP[name]["cn"])
        else:
            return name

    @cached_property
    def bk_state_name(self):
        name = self._extra_attr.get("bk_state_name", "")
        if name in COUNTRY_MAP:
            return str(COUNTRY_MAP[name]["cn"])
        else:
            return name

    @cached_property
    def bk_isp_name(self):
        name = self._extra_attr["bk_isp_name"]
        if name in ISP_MAP:
            return str(ISP_MAP[name]["cn"])
        else:
            return name

    @cached_property
    def bk_os_type_name(self):
        """
        操作系统类型名
        """
        if self.bk_os_type:
            return settings.OS_TYPE_NAME_DICT.get(int(self.bk_os_type), "")
        return None

    @cached_property
    def host_id(self):
        return f"{self.ip}|{self.bk_cloud_id}"

    def __eq__(self, other):
        return self.bk_host_id == other.bk_host_id

    def __hash__(self):
        return self.bk_host_id

    def __repr__(self):
        return f"<Host: {self.display_name}>"

    def get_attrs(self):
        return super().__getattribute__("_extra_attr")

class Process(object):
    """
    进程信息
    {
        "bk_supplier_account": "0",
        "bind_ip": "1",
        "description": "",
        "start_cmd": "",
        "restart_cmd": "",
        "pid_file": "",
        "auto_start": false,
        "timeout": 30,
        "protocol": "1",
        "auto_time_gap": 60,
        "reload_cmd": "",
        "bk_func_name": "java",
        "work_path": "/data/bkee",
        "stop_cmd": "",
        "face_stop_cmd": "",
        "port": "8008,8443",
        "bk_process_name": "job_java",
        "user": "",
        "proc_num": 1,
        "priority": 1,
        "bk_biz_id": 2,
        "bk_func_id": "",
        "bk_process_id": 1
    }
    """

    def __init__(
        self,
        bk_process_id,
        bk_process_name,
        bk_func_name,
        service_instance_id,
        bk_host_id,
        bind_ip="",
        port="",
        protocol="",
        process_template_id=0,
        **kwargs,
    ):
        """
        :param int bk_process_id: 进程ID
        :param str bk_process_name: 进程名称
        :param str bk_func_name: 进程功能名称
        :param str bind_ip: 绑定IP
        :param str port: 绑定端口，格式: 1,2,3-5,7-10
        :param str service_instance_id: 服务实例ID
        :param str process_template_id: 进程模板ID
        :param str bk_host_id: 主机ID
        """
        self.bk_process_id = int(bk_process_id)
        self.bk_process_name = bk_process_name
        self.bk_func_name = bk_func_name
        self.bind_ip = bind_ip or ""
        self.port = port or ""
        self.service_instance_id = int(service_instance_id)
        self.process_template_id = int(process_template_id)
        self.bk_host_id = int(bk_host_id)
        self.protocol = protocol

    def __eq__(self, other):
        return self.bk_process_id == other.bk_process_id

    def __hash__(self):
        return hash(self.bk_process_id)

    def __repr__(self):
        return "<Process: {}({})>".format(self.bk_process_name, self.bk_func_name)

    @classmethod
    def _parse_port_num(cls, port_num):
        """
        检查端口号是否合法
        """
        if isinstance(port_num, six.string_types) and port_num.strip().isdigit():
            port_num = int(port_num)
        elif isinstance(port_num, int):
            pass
        else:
            raise ValueError(_("无法解析的端口号：%s") % port_num)

        if 0 <= port_num <= 65535:
            return port_num

        raise ValueError(_("不在合法范围内的端口号：%s") % port_num)

    @property
    def port_range(self):
        """
        将端口范围字符串解析为结构化数据
        :return: 二元组列表，元组的两个元素分别代表起始端口和结束端口（闭区间）
        :example [(1, 1), (3, 5), (7, 10)]
        """

        port_range_list = []

        # 为空直接返回
        if not self.port:
            return port_range_list

        try:
            # 以逗号拆开
            range_str_list = [p for p in self.port.split(",") if p != ""]
            for range_str in range_str_list:
                try:
                    # 先判断是不是单个数字
                    port_num = self._parse_port_num(range_str)
                    # 如果是单个数字，则转化为区间并保存
                    port_range_list.append((port_num, port_num))
                except ValueError:
                    # 如果不是单个数字，尝试识别为区间字符串
                    port_range_tuple = range_str.split("-")

                    # 尝试拆分为上界和下界
                    if len(port_range_tuple) != 2:
                        raise ValueError(_("不合法的端口范围定义格式：%s") % range_str)

                    # 对上界和下界分别进行解析
                    port_num_min, port_num_max = port_range_tuple
                    port_num_min = self._parse_port_num(port_num_min)
                    port_num_max = self._parse_port_num(port_num_max)

                    if port_num_min > port_num_max:
                        # 下界 > 上界 也是不合法的范围
                        raise ValueError(_("不合法的端口范围定义格式：%s") % range_str)
                    port_range_list.append((port_num_min, port_num_max))

        except Exception as e:
            raise ValueError(_("端口范围字符串解析失败：%s") % e)

        return port_range_list

class ServiceInstance(object):
    """
    服务实例
    """

    def __init__(
        self,
        service_instance_id,
        name=None,
        bk_host_id=None,
        bk_module_id=None,
        service_category_id=0,
        labels=None,
        topo_link=None,
        **kwargs,
    ):
        """
        :param int service_instance_id: 服务实例ID
        :param str or unicode name: 服务实例名称
        :param int bk_host_id: 绑定的主机ID
        :param int bk_module_id: 所属模块ID
        :param int service_category_id: 所属服务分类ID
        :param dict[str, list[TopoNode]] topo_link: 服务实例所属拓扑链
        """
        self.service_instance_id = int(service_instance_id)
        self.name = name
        self.bk_host_id = int(bk_host_id or 0)
        self.bk_module_id = int(bk_module_id or 0)
        self.service_category_id = int(service_category_id or 0)
        self.labels = labels or {}
        self.topo_link = topo_link

        # 自定义属性
        self._extra_attr = kwargs

    def __getattr__(self, item):
        """
        获取自定义属性
        """
        if item == "_extra_attr":
            return super(ServiceInstance, self).__getattribute__(item)
        if item in self._extra_attr:
            return self._extra_attr[item]
        return super(ServiceInstance, self).__getattribute__(item)

    def __eq__(self, other):
        return self.service_instance_id == other.service_instance_id

    def __hash__(self):
        return hash(self.service_instance_id)

    def __repr__(self):
        return "<ServiceInstance: {}({})>".format(self.name, self.service_instance_id)

class ServiceCategoryNode(object):
    """
    服务分类节点
    """

    def __init__(self, category_id, category_name, parent_id=0, root_id=0, **kwargs):
        """
        :param int category_id: 分类ID
        :param str category_name: 分类名称
        :param int parent_id: 父节点ID
        :param int root_id: 根节点ID
        """
        self.category_id = int(category_id)
        self.category_name = category_name
        self.parent_id = int(parent_id)
        self.root_id = int(root_id)

class ServiceCategoryTopo(ServiceCategoryNode):
    """
    服务分类拓扑
    {
        "id": 1,
        "name":"数据库",
        "parent_id": 0,
        "root_id": 0,
        "child": [
            {
                "id": 2,
                "name":"mysql",
                "parent_id": 1,
                "root_id": 1,
            }
        ]
    }
    """

    def __init__(self, tree_data):
        child = tree_data.pop("child", [])
        super(ServiceCategoryTopo, self).__init__(**tree_data)
        # 子节点信息
        self.child = [ServiceCategoryTopo(**c) for c in child]


def flutuacao_do_pib():
    """
    Esta função tem como retorno a flutuação do pib entre
    o primeiro e segundo anos, dado a taxas de variação
    para cada ano.
    :return:
    """
    f1, f2 = input().split(" ")
    f1, f2 = float(f1), float(f2)
    pib_float = (((1 + f1 / 100) * (1 + f2 / 100)) - 1) * 100
    return print(f"{pib_float:.6f}")

flutuacao_do_pib()


from modules.classes import Enemy
import random

commonality_choices = ('common', 'rare', 'epic', 'legendary')

def loot_generator(loot_level, commonality):
  if loot_level > 0 and loot_level <= 3:
    if commonality == commonality_choices[0]:  # common
      return {
        'gold': random.randint(3, 7)  # generates random integer from 3 to 7
      }

enemies = {
  'Bandit': Enemy('Bandit', 'Bandits', 'Humanoid', 1, 10, 3, 5, loot_generator(1, 'common')),
  'Bandit Leader': Enemy('Bandit Leader', 'Bandit Leaders', 'Humanoid', 2, 12, 4, 5, loot_generator(2, 'common')),
  'Wolf': Enemy('Wolf', 'Wolves', 'Beast', 2, 12, 4, 4, loot_generator(2, 'common')),
}

# It runs this if it's not commented out but imported elsewhere

# for enemy in enemies:
#   enemies[enemy].printAllInfo()


import pandas as pd
import plotly.express as px

class Chart:
    def __init__(self, df, agg_col):
        self.df = df
        self.agg_col = agg_col

    def histogram(self, title: str, label: str, yaxis_title: str):
        fig = px.histogram(
            self.df[self.df[self.agg_col] > 0],
            x=self.agg_col,
            marginal="box",
            title=f"{title}",
            labels={self.agg_col: label},
            height=400,
            width=900,
        )
        fig.update_layout(
            plot_bgcolor="rgba(0, 0, 0, 0)",
            paper_bgcolor="rgba(0, 0, 0, 0)",
            yaxis_title=yaxis_title,
        )

        return fig

    def bar_year(self, title: str, label: str):
        """Vertical bar chart"""
        df_year = (
            self.df.groupby("YEAR")
            .agg({self.agg_col: "sum"})
            .sort_values(self.agg_col, ascending=False)
            .reset_index()
        )
        df_year["category"] = [str(i) for i in df_year.index]
        df_year["color"] = "rgb(189,189,189)"
        df_year.loc[df_year["category"] == "0", "color"] = "rgb(255,106,95)"
        df_year.loc[df_year["category"] == "1", "color"] = "rgb(120,120,120)"
        colors = df_year["color"].values

        fig = px.bar(
            df_year,
            x="YEAR",
            y=self.agg_col,
            color="category",
            hover_name="YEAR",
            hover_data={"category": False, "YEAR": False},
            labels={self.agg_col: label, "YEAR": ""},
            color_discrete_sequence=colors,
            title=f"{title}",
            height=500,
            width=800,
        )

        fig.update_layout(
            showlegend=False,
            plot_bgcolor="rgba(0, 0, 0, 0)",
            paper_bgcolor="rgba(0, 0, 0, 0)",
            xaxis=dict(tickmode="linear", tick0=1),
        )

        return fig

    def bar_year_uf(self, title: str, label: str):
        """Vertical bar chart"""
        df_per_year_uf = (
            self.df[self.df["YEAR"] >= 2013]
            .groupby(["YEAR", "SIGLA_UF"], as_index=False)
            .agg({self.agg_col: "sum"})
        )
        df_per_year_uf = df_per_year_uf.assign(
            UF_YEAR=lambda x: x["SIGLA_UF"] + " - " + x["YEAR"].astype(str)
        )

        fig = px.bar(
            df_per_year_uf,
            x="YEAR",
            y=self.agg_col,
            color="SIGLA_UF",
            barmode="group",
            color_discrete_sequence=px.colors.qualitative.Pastel,
            hover_name="UF_YEAR",
            hover_data={"YEAR": False, "SIGLA_UF": False},
            height=500,
            width=900,
            labels={"YEAR": "", self.agg_col: label, "SIGLA_UF": "State"},
        )

        fig.update_layout(
            plot_bgcolor="rgba(0, 0, 0, 0)",
            paper_bgcolor="rgba(0, 0, 0, 0)",
            xaxis=dict(tickmode="linear", tick0=1),
        )

        return fig

    def bar_mun_year(self, title: str, label: str):
        """Horizontal bar chart"""
        apb_mun_sorted = (
            self.df[self.df["YEAR"] >= 2013]
            .sort_values(self.agg_col, ascending=False)
            .head(20)
        )

        fig = px.bar(
            apb_mun_sorted,
            x=self.agg_col,
            y="MUN_UF",
            color="YEAR",
            color_continuous_scale="Viridis",
            title=f"{title}",
            height=500,
            width=800,
            hover_name="MUN_UF",
            hover_data={"MUN_UF": False},
            labels={self.agg_col: label, "MUN_UF": "Municipalities", "YEAR": "Year"},
        )
        fig.update_layout(
            plot_bgcolor="rgba(0, 0, 0, 0)", paper_bgcolor="rgba(0, 0, 0, 0)"
        )
        return fig

    def bar_mun_uf_total(self, agg_method: str, title: str, label: str):
        df_mun_group = (
            self.df[self.df["YEAR"] >= 2013]
            .groupby(["TRASE_ID", "MUN_UF"], as_index=False)
            .agg({self.agg_col: agg_method})
            .sort_values(self.agg_col, ascending=False)
            .head(20)
        )

        df_mun_group["category"] = [str(i) for i in df_mun_group.index]
        colors = ["rgb(189,189,189)"] * df_mun_group["TRASE_ID"].nunique()
        colors[0] = "rgb(255,106,95)"
        colors[1] = "rgb(120,120,120)"

        fig = px.bar(
            df_mun_group,
            x=self.agg_col,
            y="MUN_UF",
            color="category",
            color_discrete_sequence=colors,
            title=f"{title}",
            hover_name="MUN_UF",
            hover_data={"category": False, "MUN_UF": False},
            height=600,
            width=800,
            labels={self.agg_col: label, "MUN_UF": "Municipalities"},
        )
        fig.update_layout(
            showlegend=False,
            plot_bgcolor="rgba(0, 0, 0, 0)",
            paper_bgcolor="rgba(0, 0, 0, 0)",
            yaxis={"categoryorder": "total ascending"},
        )
        return fig

    def subplot_hist(self, title: str):
        import plotly.graph_objects as go
        from plotly.subplots import make_subplots

        fig = make_subplots(rows=2, cols=4)

        df = self.df[self.df[self.agg_col] > 0]

        trace0 = go.Histogram(
            x=df[df["YEAR"] == 2020][self.agg_col],
            autobinx=True,
            name="2020",
            marker_color="rgb(255,106,95)",
        )
        trace1 = go.Histogram(
            x=df[df["YEAR"] == 2019][self.agg_col],
            autobinx=True,
            name="2019",
            marker_color="rgb(120,120,120)",
        )
        trace2 = go.Histogram(
            x=df[df["YEAR"] == 2018][self.agg_col],
            autobinx=True,
            name="2018",
            marker_color="rgb(189,189,189)",
        )
        trace3 = go.Histogram(
            x=df[df["YEAR"] == 2017][self.agg_col],
            autobinx=True,
            name="2017",
            marker_color="#EE7A68",
        )
        trace4 = go.Histogram(
            x=df[df["YEAR"] == 2016][self.agg_col],
            autobinx=True,
            name="2016",
            marker_color="#604693",
        )
        trace5 = go.Histogram(
            x=df[df["YEAR"] == 2015][self.agg_col],
            autobinx=True,
            name="2015",
            marker_color="rgb(189,189,189)",
        )
        trace6 = go.Histogram(
            x=df[df["YEAR"] == 2014][self.agg_col],
            autobinx=True,
            name="2014",
            marker_color="#330C73",
        )
        trace7 = go.Histogram(
            x=df[df["YEAR"] == 2013][self.agg_col],
            autobinx=True,
            name="2013",
            marker_color="#EB89B5",
        )

        fig.append_trace(trace0, 1, 1)
        fig.append_trace(trace1, 1, 2)
        fig.append_trace(trace2, 1, 3)
        fig.append_trace(trace3, 1, 4)
        fig.append_trace(trace4, 2, 1)
        fig.append_trace(trace5, 2, 2)
        fig.append_trace(trace6, 2, 3)
        fig.append_trace(trace7, 2, 4)

        fig.update_layout(
            autosize=False,
            height=500,
            width=900,
            plot_bgcolor="rgba(0, 0, 0, 0)",
            paper_bgcolor="rgba(0, 0, 0, 0)",
        )

        fig.update_xaxes(title=f"{title}")
        fig.update_yaxes(title="Num. Municipalities")

        return fig.show()


from setuptools import setup, find_packages
import sys, os

version = '0.1'

setup(
    name='ckanext-cendari',
    version=version,
    description="CENDARI authentication for CKAN",
    long_description='''
    ''',
    classifiers=[], # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
    keywords='',
    author='Carsten Thiel',
    author_email='thiel@sub.uni-goettingen.de',
    url='',
    license='',
    packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
    namespace_packages=['ckanext', 'ckanext.cendari'],
    include_package_data=True,
    zip_safe=False,
    install_requires=[
        # -*- Extra requirements: -*-
    ],
    entry_points='''
        [ckan.plugins]
        cendari=ckanext.cendari.plugin:CendariAuthPlugin
    ''',
)


'''
Author: Georgio Feghali
Date: July 11 2023
'''

# UI Dependencies
import streamlit as st
from PIL import Image

# Logic Dependencies
import openai

# Page Configuration.
favicon = Image.open("./admin/branding/logos/favicon-32x32.png")
st.set_page_config(
    page_title="Solvr.ai - Image Generation",
    page_icon=favicon,
    layout="wide",
    initial_sidebar_state="collapsed"
)

## Logic
def create_image(prompt):
    openai.api_key = st.secrets['openai_api_key']
    response = openai.Image.create(
        prompt=prompt,
        n=1,
        size="1024x1024"
    )

    img_url = response['data'][0]['url']

    return img_url

# UI
st.markdown("
Image Generator ðŸ–¼ï¸�
", unsafe_allow_html=True)

prompt = st.text_input("Prompt", placeholder="Describe the image you want us to generate for you!")
if st.button("Generate!"):
    if prompt == "":
        st.warning("Please enter a prompt!")
    else:
        with st.spinner("Generating Image..."):
            img_url = create_image(prompt)
            st.text(img_url)


import cv2
import serial
import sys
import os
import time
from gaze_tracking import GazeTracking
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

chrome = webdriver.Chrome()
chrome.get('http://127.0.0.1:5500/web/move.html')
ser = serial.Serial('/dev/cu.usbmodem14201', 9600, timeout = 1)

gaze = GazeTracking()
webcam = cv2.VideoCapture(0)
l=list()
while True:
    # We get a new frame from the webcam
    _, frame = webcam.read()

    # We send this frame to GazeTracking to analyze it
    try:
        gaze.refresh(frame)
    except:
        pass

    frame = gaze.annotated_frame()
    text = ""

    if gaze.is_blinking():
        text = "Blinking"

    elif gaze.is_right():
        text = "Looking right"

    elif gaze.is_left():
        text = "Looking left"

    elif gaze.is_center():
        text = "Looking center"

    if len(l)==0:
        l.append(text)
    elif l[0]!=text:
        l = list()
        l.append(text)
    elif len(l)==9:
        print(text)
        l = list()
        if (text == "Looking left"):
            start = time.time()
            while True:
                ser.write(b'l')
                time.sleep(0.5)
                end = time.time()
                if end - start >= 5:
                    break
            time.sleep(0.5)
        elif text == "Looking right":
            start = time.time()
            while True:
                ser.write(b'r')
                time.sleep(0.5)
                end = time.time()
                if end - start >= 5:
                    break
            time.sleep(0.5)
        elif text == "Blinking":
            start = time.time()
            while True:
                #ser.write(b's')
                #time.sleep(0.5)
                end = time.time()
                if end - start >= 5:
                    ser.close()
                    webcam.release()
                    cv2.destroyAllWindows()
                    chrome.close()
                    os.system("python hover.py")
                    sys.exit()
                    #break
            #time.sleep(0.5)
        elif text == "Looking center":
            start = time.time()
            while True:
                ser.write(b'f')
                time.sleep(0.5)
                end = time.time()
                if end - start >= 5:
                    break
            time.sleep(0.5)
    else:
        l.append(text)

    cv2.putText(frame, text, (90, 60), cv2.FONT_HERSHEY_DUPLEX, 1.6, (255,178,45), 2)

    left_pupil = gaze.pupil_left_coords()
    right_pupil = gaze.pupil_right_coords()
    cv2.putText(frame, "Left pupil:  " + str(left_pupil), (90, 130), cv2.FONT_HERSHEY_DUPLEX, 0.9, (255,178,45), 1)
    cv2.putText(frame, "Right pupil: " + str(right_pupil), (90, 165), cv2.FONT_HERSHEY_DUPLEX, 0.9, (255,178,45), 1)

    cv2.imshow("Demo", frame)

    if cv2.waitKey(10) & 0xFF == ord('q'):
        break

ser.close()
webcam.release()
cv2.destroyAllWindows()
chrome.close()
os.system("python hover.py")
sys.exit()


# A base class for solar collectors with a main reflector and square
# homogenizer after the focus, with a receiver surface at the end of the
# homogenizer.

import numpy as N

from .. import spatial_geometry as sp
from ..assembly import Assembly
from ..object import AssembledObject

from .one_sided_mirror import one_sided_receiver
from .homogenizer import rect_homogenizer

class HomogenizedLocalReceiver(Assembly):
    def __init__(self, main_reflector, receiver_pos, receiver_dims, \
        homogenizer_depth, homog_opt_eff):
        """
        Arguments:
        main_reflector - a Surface object representing the reflector that
            focuses rays into a homogenized receiver.
        receiver_pos - the distance along the optical axis from the main reflector to the
            receiver's end surface - the PV panel (should be about the focal
            length)
        receiver_dims - if scalar, the receiver is square, with this side
            length. If a tuple, it's the x, y lengths respectively.
        homogenizer_depth - the homogenizer has base dimensions to fit the PV
            square, and this height.
        homog_opt_eff - the optical efficiency of each mirror in the homogenizer
        """
        if type(receiver_dims) is type(tuple()):
            self._sides = receiver_dims
        else:
            self._sides = (receiver_dims, receiver_dims)
        self._rec_pos = receiver_pos

        self._rec, rec_obj = one_sided_receiver(*self._sides)
        receiver_frame = N.dot(sp.translate(0, 0, receiver_pos), sp.rotx(N.pi))
        rec_obj.set_transform(receiver_frame)

        self._hom = rect_homogenizer(self._sides[0], self._sides[1], \
            homogenizer_depth, homog_opt_eff)
        self._hom.set_transform(receiver_frame)

        self._mr = main_reflector
        refl = AssembledObject(surfs=[main_reflector])
        Assembly.__init__(self, objects=[rec_obj, refl], subassemblies=[self._hom])

    def get_receiver_surf(self):
        """for anyone wishing to directly access the receiver"""
        return self._rec

    def get_homogenizer(self):
        """Direct access to the homogenizer subassembly"""
        return self._hom

    def get_main_reflector(self):
        return self._mr

    def histogram_hits(self, bins=50):
        """
        Generates a 2D histogram of energy absorbed at the receiver surface,
        assuming a trace has been run using this assembly.

        Arguments:
        bins - hom many bins per axis to use (default 50)

        Returns:
        H - a 2D array with the energy falling on each bean, x axis along
            the first dimension, y along second
        xbins, ybins - the edges of the bins (so one point more than the number
            of beans for each axis)

        See Also:
        numpy.histogram2D()
        """
        energy, pts = self._rec.get_optics_manager().get_all_hits()
        x, y = self._rec.global_to_local(pts)[:2]
        rngx = self._sides[0]/2.
        rngy = self._sides[1]/2.

        H, xbins, ybins = N.histogram2d(x, y, bins, \
            range=([-rngx,rngx], [-rngy,rngy]), weights=energy)
        return H, xbins, ybins


import random

FRAMES_ = 5000

def getExampleIntervals():
    testing_intervals = []
    for i in range(FRAMES_):
        start = random.randint(0, (i + 1) * 10000)
        stop = start + 1000000
        testing_intervals.append((start, stop))
    return testing_intervals

intervals = getExampleIntervals()
int_file = open("example_intervals_chr_3.csv", "w")
for inter in intervals:
    int_file.write(str(inter[0]) + "\t" + str(inter[1]) + "\n")
int_file.close()


import torch
import torch.nn as nn
import torch.utils.data as Data
import pickle
import numpy as np
import torch.nn.functional as F
from sklearn import preprocessing
from sklearn.metrics import roc_auc_score, classification_report
from torch.nn.parameter import Parameter

def one_hot(y_, maxvalue = None):
    if maxvalue==None:
        y_ = y_.reshape(len(y_))
        n_values = np.max(y_) + 1
    else:
        n_values = maxvalue
    return np.eye(n_values)[np.array(y_, dtype=np.int32)]

with_gpu = torch.cuda.is_available()
if with_gpu:
    device = torch.device("cuda")
else:
    device = torch.device("cpu")
print('We are using %s now.' %device)

EPOCH = 1000
LR = 0.001

# load datasets
data = pickle.load(open( '../ECG_code/MIT_BIH_data_5class.p', "rb" ), encoding='latin1')
n_class=5
# print(data.shape)

data = data[:, :-1]
n_fea = data.shape[-1] - 1

# normalization
feature_all = data[:, 0:n_fea]
label_all = data[:, n_fea:n_fea+1]
feature_normalized=preprocessing.scale(feature_all)
all = np.hstack((feature_normalized, label_all))

n_seg = all.shape[0]
np.random.shuffle(all)
all = torch.Tensor(all).to(device)

# split train and test data,train
ratio = 0.8
train_data = all[: int(ratio*n_seg)]
test_data = all[int(ratio*n_seg):]

BATCH_SIZE = int(all.shape[0]*0.05)

train_x_3d = train_data[:, 0:480].reshape([-1, 2, 240])
train_x_3d = torch.unsqueeze(train_x_3d, dim=1)

train_y_ = train_data[:, 480:481].long().to(device)

train_data = Data.TensorDataset(train_x_3d, train_y_)
train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=False)

test_data_3d = test_data[:, 0:480].reshape([-1, 2, 240])
test_data_3d = torch.unsqueeze(test_data_3d, dim=1)

test_y = test_data[:, 480:481].long().to(device)

class CNN(nn.Module):
    def __init__(self,):
        super(CNN, self).__init__()
        self.conv1 = nn.Sequential(
            nn.Conv2d(
                in_channels=1,
                out_channels=4,
                kernel_size=[2, 2],
                stride=1,
                padding=[1,2],
            ),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=[1, 2])
        )
        self.conv2 = nn.Sequential(
            nn.Conv2d(4, 8, [2, 2], 1, padding=[1,2]),
            nn.ReLU(),
            nn.MaxPool2d([1,2])
        )

        self.fc = nn.Linear(1984, 200)
        self.out = nn.Linear(200, 5)
        self.weight = Parameter(torch.Tensor(1, 240))

    def forward(self, x):
        x = x * self.weight  # element-wise multiply by the weight. If the shape doesn't consistant, please transpose the variable.
        x = self.conv1(x)
        x = self.conv2(x)
        x = x.view(x.size(0), -1)

        x = F.relu(self.fc(x))
        x = F.dropout(x, 0.2)

        output = self.out(x)
        return output, x

cnn = CNN()
cnn.to(device)

optimizer = torch.optim.Adam(cnn.parameters(), lr=LR)
loss_func = nn.CrossEntropyLoss()
import time

best_acc = 0
best_auc = 0
time_s = time.perf_counter()
# training & testing
for epoch in range(EPOCH):
    for step, (train_x, train_y) in enumerate(train_loader):

        output = cnn(train_x)[0]

        loss = loss_func(output, train_y.squeeze(1))
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        if epoch % 1 == 0 and step == 0:
            test_output, last_layer = cnn(test_data_3d)
            test_loss = loss_func(test_output, test_y.squeeze(1))

            test_y_score = one_hot(test_y.data.cpu().numpy())
            pred_score = F.softmax(test_output, dim=0).data.cpu().numpy()
            auc_score = roc_auc_score(test_y_score, pred_score)

            pred_y = torch.max(test_output, 1)[1].data.cpu().numpy()
            pred_train = torch.max(output, 1)[1].data.cpu().numpy()
            train_acc = float((pred_train == train_y.squeeze(1).data.cpu().numpy()).astype(int).sum()) / float(train_y.size(0))
            test_acc = float((pred_y == test_y.squeeze(1).data.cpu().numpy()).astype(int).sum()) / float(test_y.size(0))
            print('Epoch: ', epoch, '| STEP: ', step, '|train loss: %.4f' % loss.item(),
                  ' train ACC: %.4f' % train_acc, '| test loss: %.4f' % test_loss.item(),
                  'test ACC: %.4f' % test_acc,
                  '| AUC: %.4f' % auc_score)

            """Save model"""
            if auc_score > best_auc:
                best_auc = auc_score
                torch.save(cnn.state_dict(), '../ECG_code/saved_model/ECG_ours.pt')
                print('model saved, for auc: {}'.format(best_auc))

"""load best model"""
cnn.load_state_dict(torch.load('../ECG_code/saved_model/ECG_ours.pt'))
cnn.eval()
test_output = cnn(test_data_3d)[0]

test_y_score = one_hot(test_y.data.cpu().numpy())
pred_score = F.softmax(test_output, dim=0).data.cpu().numpy()
pred_y = torch.argmax(torch.from_numpy(pred_score), dim=1).data.cpu().numpy()

auc_score = roc_auc_score(test_y_score, pred_score)

print('===========================================================')

print(classification_report(test_y.data.cpu().numpy(), pred_y, digits=4))
print("AUC: ", auc_score)


'''
11. Дана строка my_str. Создать список в который поместить те символы из my_str, которые встречаются
в строке ТОЛЬКО ОДИН раз.
'''
my_string = 'fgfgfgddddd H jjjjjdddkk L hhdhd'
new_list = []
for i in my_string:
    count1 = my_string.count(i)
    if count1 == 1:
        new_list.extend(i)
print(new_list)


import unittest
import collections

class Solution(object):
    def validTree(self, n, edges):
        """
        :type n: int
        :type edges: List[List[int]]
        :rtype: bool
        """

        adj_list = self.get_adj_list(edges)

        queue = [0]
        seen = set([0])

        while queue:
            current = queue.pop(0)
            for neighbor in adj_list[current]:
                if neighbor not in seen:
                    queue.append(neighbor)
                    seen.add(neighbor)
                elif neighbor in seen:
                    return False

        return True

    def get_adj_list(self, edges):
        adj_list = collections.defaultdict(list)

        for x, y in edges:
            adj_list[x].append(y)

        return adj_list

class TestSolution(unittest.TestCase):

    def test_valid_tree(self):
        n = 5
        valid_edges = [[0,1],
                     [0,2],
                     [0,3],
                     [1,4]]
        s = Solution()
        valid_answer = s.validTree(n, valid_edges)
        self.assertTrue(valid_answer)

    def test_invalid_tree(self):
        n = 5
        invalid_edges = [
                        [0,1],
                        [1,2],
                        [2,3],
                        [1,3],
                        [1,4]]
        s = Solution()
        invalid_answer = s.validTree(n, invalid_edges)
        self.assertFalse(invalid_answer)

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

Algorithms/Leetcode/261 - Valid Graph Tree.py


"""
    1st: string parsing

    Time    O(N)
    Space   O(N)
    32 ms, faster than 66.67%
"""

class Solution:
    def areNumbersAscending(self, s: str) -> bool:
        words = s.split()
        prev = -1
        for w in words:
            if w.isdigit():
                if int(w) <= prev:
                    return False
                prev = int(w)
        return True


from fecfiler.soft_delete.models import SoftDeleteModel
from fecfiler.committee_accounts.models import CommitteeOwnedModel
from fecfiler.reports.models import ReportMixin
from fecfiler.shared.utilities import generate_fec_uid
from django.db import models
import uuid
import logging

logger = logging.getLogger(__name__)

class MemoText(SoftDeleteModel, CommitteeOwnedModel, ReportMixin):
    id = models.UUIDField(
        default=uuid.uuid4,
        editable=False,
        primary_key=True,
        serialize=False,
        unique=True,
    )
    rec_type = models.TextField(null=True, blank=True)
    is_report_level_memo = models.BooleanField(null=False, blank=False, default=True)
    transaction_id_number = models.TextField(null=True, blank=True)
    transaction_uuid = models.TextField(null=True, blank=True)
    text4000 = models.TextField(null=True, blank=True)

    class Meta:
        db_table = "memo_text"

    # This is intended to be useable without instantiating a transaction object
    @staticmethod
    def check_for_uid_conflicts(uid, report_id):  # noqa
        return (
            len(MemoText.objects.filter(transaction_id_number=uid, report_id=report_id))
            > 0
        )

    def generate_report_id(self):
        report_memos = MemoText.objects.filter(
            report_id=self.report_id, transaction_uuid=None
        )
        return "REPORT_MEMO_TEXT" + str(len(report_memos) + 1)

    def generate_unique_transaction_id(self):
        id_generator = generate_fec_uid
        if self.is_report_level_memo:
            id_generator = self.generate_report_id
        unique_id = id_generator()

        attempts = 0
        while MemoText.check_for_uid_conflicts(unique_id, self.report_id):
            unique_id = id_generator()
            attempts += 1
            logger.info("Transaction unique ID generation: collision detected")
            if attempts > 5:
                logger.info("Unique ID generation failed: Over 5 conflicts in a row")
                return ""
        return unique_id

    def save(self, *args, **kwargs):
        if not self.transaction_id_number:
            self.transaction_id_number = self.generate_unique_transaction_id()

        super(MemoText, self).save(*args, **kwargs)


#!/usr/bin/env python3
import serial
import sys
import hashlib
import time

with serial.Serial('/dev/ttyUSB0', 38400) as ser:
    ser.write(b'\xCA\xFE\x00')    # Send erase command
    resp = ser.read(4)
    if resp != b'HI\x00\x01':
        print("Failed to erase chip")
        sys.exit(1)

    print("Chip erased")

#############################################################
    with open('julia.rbf', mode='rb') as file:
        rbf_bytes = file.read()
    checksum = hashlib.sha256(rbf_bytes).hexdigest()

    rbf_len = len(rbf_bytes)
    ser.write(b'\xCA\xFE\x02')    # Send write command
    time.sleep(1);
    ser.write(rbf_len.to_bytes(length=4, byteorder='big'))

    resp = ser.read(4)
    if resp != b'HI\x02\x02':
        print("Failure sending length")
        sys.exit(1)

    length = rbf_len
    i = 0
    while (length):
        to_write = min(length, 256)
        ser.write(rbf_bytes[:to_write])
        rbf_bytes = rbf_bytes[to_write:]

        resp = ser.read(4)
        if resp == b'HI\x02\x02':
            print(i)
        elif resp == b'HI\x02\x01':
            print("Write complete")
        else:
            print("Failure during write")
            sys.exit(1)

        length = length - to_write
        i = i + 1

#############################################################
    ser.write(b'\xCA\xFE\x01')    # Send dump command
    resp = ser.read(4)
    print(resp)
    length = int.from_bytes(resp, byteorder='big')

    print("Expecting length {}".format(length))

    rbf_bytes = b''
    i = 0
    while (length):
        to_read = min(length, 256)
        resp = ser.read(to_read)
        rbf_bytes = rbf_bytes + resp
        print(i)

        length = length - to_read
        i = i + 1

    resp = ser.read(4)
    if resp == b'HI\x01\x01':
        print("Dump complete")
    else:
        print("Failure during dump")
        sys.exit(1)

    checksum2 = hashlib.sha256(rbf_bytes).hexdigest()
    if checksum != checksum2:
        print("Checksum mismatch, aborting")
        sys.exit(1)
    else:
        print("Verified, configuring")


import os
import datetime
import sys
import dateutil.rrule as dr
import dateutil.relativedelta as drel
import pandas as pd
import logging
def today():
    return datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d')
class TradeDate(object):
    def __init__(self, market='NOHOLIDAY'):
        self.holidays = set()
        self.weekend = [5,6]
        if(market=='NOHOLIDAY'):
            return
        holiday_file_dir = '/data/prod/infra/refdata/holiday/'
        if(not os.path.isdir(holiday_file_dir)):
            holiday_file_dir = os.environ['HOLIDAY_FILE_DIR']
        holiday_file_path = holiday_file_dir + market + '.txt'
        if(market=='cme'):
            holiday_file_path = holiday_file_dir + 'nyse' + '.txt'
        if(not os.path.isfile(holiday_file_path)):
            logging.error("can't find holiday file %s" % (holiday_file_path))
            return
        file = open(holiday_file_path, 'r')
        for line in file:
            holiday = datetime.datetime.strptime(line.rstrip('\n'), '%Y%m%d')
            self.holidays.add(holiday)
        if(market=='cme'):
            self.weekend = [5]

    def is_trade_day(self, day):
        day_ = datetime.datetime.combine(day, datetime.datetime.min.time())
        if(day_.weekday() in self.weekend):
            return False
        if(day_ in self.holidays):
            return False
        return True

    def get_month_option_expiration(self, day_str, day_str_format = '%Y%m%d'):
        day = datetime.datetime.strptime(day_str, day_str_format);
        rr = dr.rrule(dr.MONTHLY,byweekday=drel.FR(3), count=1, dtstart=datetime.datetime(day.year, day.month, 1))
        third_friday = rr[0]
        expire_day = third_friday
        if(not self.is_trade_day(third_friday)):
            expire_day = self.previous_day(third_friday)
        return datetime.datetime.strftime(expire_day, day_str_format)

    def _get_last_day_in_month(self, day_str, day_str_format = '%Y%m%d'):
        day = datetime.datetime.strptime(day_str, day_str_format)
        rr = dr.rrule(dr.MONTHLY,bymonthday=-1, count=1, dtstart=datetime.datetime(day.year, day.month, 1))
        month_end = rr[0]
        if(not self.is_trade_day(month_end)):
            month_end = self.previous_day(month_end)
        return datetime.datetime.strftime(month_end, day_str_format)

    def next_day(self, day):
        _day = day + datetime.timedelta(days=1)
        while not self.is_trade_day(_day):
            _day += datetime.timedelta(days=1)
        return _day

    def day_str_diff(self, day1_str, day2_str, day_str_format = '%Y%m%d'):
        day1 = datetime.datetime.strptime(day1_str, day_str_format);
        day2 = datetime.datetime.strptime(day2_str, day_str_format);
        n = 0;
        while(day1!=day2):
            if(day1>day2):
                n += 1
                day1 = self.previous_day(day1)
            else:
                n -= 1
                day1 = self.next_day(day1)
        return n;

    def next_day_str(self, day_str, day_str_format = '%Y%m%d'):
        day = datetime.datetime.strptime(day_str, day_str_format);
        return datetime.datetime.strftime(self.next_day(day), day_str_format)

    def previous_day(self, day):
        _day = day - datetime.timedelta(days=1)
        while not self.is_trade_day(_day):
            _day -= datetime.timedelta(days=1)
        return _day

    def previous_day_str(self, day_str, day_str_format = '%Y%m%d'):
        day = datetime.datetime.strptime(day_str, day_str_format);
        return datetime.datetime.strftime(self.previous_day(day), day_str_format)

    def next_n_days(self, day, n):
        ndays = []
        _day = day
        while len(ndays)<n:
            _day = self.next_day(_day)
            ndays.append(_day)
        ndays.sort();
        return ndays

    def previous_n_days(self, day, n):
        ndays = []
        _day = day
        while len(ndays)<n:
            _day = self.previous_day(_day)
            ndays.append(_day)
        ndays.sort();
        return ndays

    def previous_n_days_str(self, day_str, day_str_format = '%Y%m%d', n=20):
        ndays = []
        day = day_str
        while len(ndays)<n:
            day = self.previous_day_str(day, day_str_format)
            ndays.append(day)
        ndays.sort();
        return ndays

    def next_n_days_str(self, day_str, day_str_format = '%Y%m%d', n=20):
        ndays = []
        day = day_str
        while len(ndays)<n:
            day = self.next_day_str(day, day_str_format)
            ndays.append(day)
        ndays.sort();
        return ndays

    def get_trade_days(self, start_date, end_date):
        if(start_date>end_date):
            return []
        ndays = []
        day = start_date
        while day<=end_date:
            if(self.is_trade_day(day)):
                ndays.append(day)
            day += datetime.timedelta(days=1)
        ndays.sort()
        return ndays

def get_option_expiration_day():
    nyse = TradeDate("nyse")
    str_today = datetime.datetime.strftime(datetime.date.today(), '%Y%m%d')
    option_expire_day = nyse.get_month_option_expiration(str_today)
    return option_expire_day

def get_last_day_in_month():
    nyse = TradeDate("nyse")
    day = datetime.date.today()
    return nyse._get_last_day_in_month(datetime.datetime.strftime(day, '%Y%m%d'), day_str_format='%Y%m%d')

def get_weekday(td, day_str_format='%Y%m%d'):
    day = datetime.datetime.strptime(td, day_str_format)
    return datetime.datetime.weekday(day)

if __name__ == '__main__':
    import rlipy.logger
    '''countries = ["AV", #austria
                 "BB", #belgium
                 "DC", #Denmark
                 "FH", #Finland
                 "FP", #France
                 "GA", #Greece
                 "GR", #Germany
                 "ID",#Ireland
                 "IM",#Italy
                 "IR",#Iceland
                 "LN",#England
                 "LX",#Luxembourg
                 "MV",#Malta
                 "NA",#Netherlands
                 "NO",#Norway
                 "PL",#Portugal
                 "PW",#Porland
                 "SM",#Spain
                 "SS",#Sweden
                 "SW",#Switzerland
                 ]  '''
    countries = ['US','JP','HK']
    td_dir = '/home/VERITIONFUND/rli/tmp/country/'
    weekday = pd.read_csv(td_dir+'all.qap')
    for country in countries:
        td = pd.read_csv(td_dir+country+'.qap')
        holiday = weekday[-(weekday['$Weekday'].isin(td['$TradeDate']))]
        out_file = td_dir+'holiday/'+country+'.txt'
        holiday['$Weekday'].apply(lambda x: x[6:]+x[0:2]+x[3:5]).to_csv(out_file,index=False)


from app import create_app, db

# script import manger
from flask_script import Manager, Server

# user imports models
from app.models import User, Blog, Comment, Subscribe

# flash extension for migrating
from flask_migrate import Migrate, MigrateCommand

# app = create_app('development')
app = create_app('production')

# difineing a manager to app
manager = Manager(app)

# access to the shell

@manager.shell
def make_shell_context():
    return dict(app=app, db=db, User=User, Blog=Blog, Comment=Comment, Subscribe=Subscribe)

# intiate migrate class
migrate = Migrate(app, db)
manager.add_command('db', MigrateCommand)

# adding manager to serve
manager.add_command('server', Server)

@manager.command
def test():
    """Run the unit tests."""
    import unittest
    tests = unittest.TestLoader().discover('tests')
    unittest.TextTestRunner(verbosity=2).run(tests)

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


from __future__ import print_function

import getpass
import json
import optparse
import os
import re
import shlex
import subprocess
import socket
import sys
import time

from collections import defaultdict
from nuclide_server import LOG_FILE
from nuclide_server import NuclideServer
from nuclide_certificates_generator import NuclideCertificatesGenerator
from process_info import ProcessInfo

try:
    from fb.nuclide_config import EXTRA_NODE_PATHS, OPEN_PORTS
except ImportError as e:
    # Default extra $PATH elements for Node v0.12.
    EXTRA_NODE_PATHS = []
    # Default open ports.
    OPEN_PORTS = [9090, 9091, 9092, 9093]
    pass

# Certificates store is ~/.certs
CERTS_DIR = os.path.join(os.path.expanduser('~'), '.certs')
NODE_PATHS = EXTRA_NODE_PATHS + ['/opt/local/bin', '/usr/local/bin']

# This class manages Nuclide servers.
class NuclideServerManager(object):

    version_file = os.path.join(os.path.dirname(__file__), '../node_modules/nuclide-version/version.json')

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

    def _is_port_open(self, port):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            # If you can connect socket, that means the somebody is listening to the port.
            # Therefore, the port is not available for Nuclide server.
            # connect requires the address in tuple format (address, port).
            # That's why the double () below.
            s.connect(('localhost', port))
            s.shutdown(socket.SHUT_RDWR)
            return False
        except socket.error:
            return True

    def _find_open_port(self):
        for port in OPEN_PORTS:
            if self._is_port_open(port):
                return port
        return None

    # Get the protocol version of the build.
    @staticmethod
    def _get_version():
        # Read version from version file if it exists.
        # Otherwise, skip version checking.
        version = None
        try:
            with open(NuclideServerManager.version_file) as f:
                version_json = json.load(f)
            version = str(version_json['Version'])
        except IOError as e:
            print('No version.json. Skip version verification.', file=sys.stderr)
        except (KeyError, ValueError) as e:
            print('Corrupted version.json. Skip version verification.', file=sys.stderr)
        return version

    def _ensure_certs_dir(self):
        if not os.path.exists(CERTS_DIR):
            os.makedirs(CERTS_DIR)
        return CERTS_DIR

    # If port_filter is given, only list servers in that list.
    @staticmethod
    def list_servers(port_filter=[]):
        servers = []
        for proc in NuclideServer.get_processes():
            port = int(proc.get_command_param('port'))
            if not port_filter or port in port_filter:
                server = NuclideServer(port, proc=proc)
                servers.append(server)
        return servers

    @staticmethod
    def stop_all():
        for server in NuclideServerManager.list_servers():
            server.stop()

    # Clean up bad processes.
    def cleanup(self):

        # TODO: Remove it after migration is complete.
        # For migration, stop the forever monitor processes of Nuclide server.
        # This does not stop existing Nuclide server processes themselves.
        # It just removes the monitor so that we can kill them on upgrade.
        for proc in ProcessInfo.get_processes(getpass.getuser(),
            '%s.*%s' % (re.escape('forever/bin/monitor'), re.escape('nuclide-main.js'))):
            print('Stopping %s' % proc, file=sys.stderr)
            proc.stop()

        # Clean up multiple Nuclide processes on same port.
        # There should be no more than one on a given port.
        # TODO: log the error to analytics db.
        # { port1 => [proc1, proc2, ...], ...}
        server_proc_map = defaultdict(list)
        # Group the processes by port.
        for proc in NuclideServer.get_processes():
            port = int(proc.get_command_param('port'))
            server_proc_map[port].append(proc)
        for port in server_proc_map:
            if len(server_proc_map[port]) > 1:
                print('Multiple Nuclide processes on port %d. Something wrong. Clean them up...' % port, file=sys.stderr)
                for proc in server_proc_map[port]:
                    proc.stop()

    # Find and use existing Nuclide server's port if there is a match,
    # or obtain an open port.
    def _obtain_nuclide_server_port(self):
        servers = self.list_servers(OPEN_PORTS)
        if len(servers) > 0:
            for server in servers:
                # Return existing server port if the protocol matches.
                if server.is_https() == (not self.options.insecure):
                    return server.port

        # If no existing servers, find an open port.
        port = self._find_open_port()
        if port is None:
            print('No ports available.', file=sys.stderr)
            return None
        else:
            return port

    def start_nuclide(self):
        if self.options.port is None:
            port = self._obtain_nuclide_server_port()
            if port is None:
                return 1
        else:
            port = self.options.port

        server = NuclideServer(port, self.options.workspace)

        # If given port is being used by somebody else, you shall not pass.
        if not self._is_port_open(port) and not server.is_mine():
            print('You are not the owner of Nuclide server at port %d. Try a different port.' %
                port, file=sys.stderr)
            return 1

        # At this moment, the port is either open, or we have an existing server running.
        if server.is_running():
            version = NuclideServerManager._get_version()
            running_version = server.get_version()
            # If the common names don't match, we restart.
            if (version and version != running_version) or \
                (self.options.common_name and server.get_common_name() != self.options.common_name):
                print('Restarting Nuclide server on port %d' % port, file=sys.stderr)
                server.stop()
                return self.start_server(server)
                # Don't use restart() here, so that we regenerate the certificates.
            else:
                print('Nuclide already running on port %d. You may connect.' % port, file=sys.stderr)
                server.print_json()
                return 0
        else:
            return self.start_server(server)

    def start_server(self, server):
        if self.options.insecure:
            # Use http.
            return server.start(self.options.timeout, quiet=self.options.quiet)
        else:
            # Use https.
            certs_dir = self.options.certs_dir or self._ensure_certs_dir()
            # Add prefix "user.nuclide" to avoid collision.
            common_name = self.options.common_name or \
                '%s.nuclide.%s' % (getpass.getuser(), socket.gethostname())

            # TODO: Client common name is 'nuclide'.
            #       We may want to generate unique common name and verify it.
            certs_generator = NuclideCertificatesGenerator(certs_dir, common_name, 'nuclide', expiration_days=30)
            return server.start(self.options.timeout, cert=certs_generator.server_cert,
                key=certs_generator.server_key, ca=certs_generator.ca_cert, quiet=self.options.quiet)

def get_option_parser():
    parser = optparse.OptionParser(description='Nuclide server manager')
    parser.add_option('-p', '--port', type=int, help='port number')
    parser.add_option('-k', '--insecure', help='use http instead of https', action="store_true", default=False)
    # Don't set default value of certs_dir, so that we can just check certs_dir for None.
    parser.add_option('-d', '--certs_dir', type=str, help='directory to store certificate files, default: ~/.certs')
    parser.add_option('-n', '--common_name', type=str, help='the common name to use in certificate')
    parser.add_option('-t', '--timeout', type=int, help='timeout in seconds, default: %default', default=10)
    parser.add_option('-w', '--workspace', type=str, help='the workspace directory')
    parser.add_option('-c', '--command', type=str, help='commands: list, start, stopall; default: %default', default='start')
    parser.add_option('-q', '--quiet', help='suppress nohup logging', action="store_true", default=False)
    return parser

if __name__ == '__main__':
    os.environ['PATH'] = os.pathsep.join(NODE_PATHS) + os.pathsep + os.environ.get('PATH', '')
    parser = get_option_parser()
    options, args = parser.parse_args(sys.argv[1:])

    manager = NuclideServerManager(options)
    manager.cleanup();
    if options.command == 'start':
        ret = manager.start_nuclide()
        print('The log file can be found at %s.' % LOG_FILE, file=sys.stderr)
    elif options.command == 'list':
        servers = manager.list_servers()
        for server in servers:
            server.print_json()
        ret = 0
        print('The log file can be found at %s.' % LOG_FILE, file=sys.stderr)
    elif options.command == 'stopall':
        manager.stop_all()
        ret = 0
    else:
        print('Unrecognized command: %s' % options.command, file=sys.stderr)
        ret = 1
    sys.exit(ret)


def fizzbuzz(num):
    if x % 3 == 0 and x % 5 == 0:
        return "Fizz Buzz"
    elif x % 3 == 0:
        return "Fizz"
    elif x % 5 == 0:
        return "Buzz"
    else:
        return x

MAX_NUM = int(input("Enter Maximum Number: "))
for x in range (MAX_NUM):
    print (fizzbuzz(x))


from datetime import datetime

from services.errorhandlers import errorlogger
from services.util import get_db_check_initialized
from util.customerrors import InvalidDateRangeError

@errorlogger
def get_samples(sample_classes: str, start_time: str, end_time: str, quant_method: str):
    if not (start_time is None):
        start_time = datetime.utcfromtimestamp(int(start_time))
    if not (end_time is None):
        end_time = datetime.utcfromtimestamp(int(end_time))
    if start_time and end_time and start_time > end_time:
        raise InvalidDateRangeError(
            "Error while getting the samples: \"start_time\" parameter was greater than \"end_time\" parameter."
        )
    if sample_classes:
        sample_classes = sample_classes.split(",")
    if quant_method:
        quant_method = quant_method.split(",")
    try:
        sample_list = get_db_check_initialized().get_samples(sample_classes, start_time, end_time, quant_method)
    except InterruptedError:
        sample_list = []
    return sample_list


# Problem: Find the longest substring without repeating characters.

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        maxWord = ""
        word = ""
        counter = 0
        while counter < len(s):
            if s[counter] in word:
                if len(word) > len(maxWord):
                    maxWord = word
                if len(word) <= 1:
                    word = word[-1]
                else:
                    print(word)
                    word = word[word.index(s[counter])+1:] + s[counter]
            else:
                word += s[counter]
                print(word)
            counter+=1
        if len(maxWord) > len(word):
            return len(maxWord)
        return len(word)

# Time Complexity: O(n^n)
# Space Complexity: O(n)


from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPool2D, Flatten, Dense, Dropout

import numpy as np
import os
import cv2
import random

class TensorFlowNetwork():

	def __init__(self, number_of_classes, dataset_path, sep=0.1, epochs=10):
		self.number2letter = dict()
		self.letter2number = dict()
		((X_train, y_train), (X_test, y_test)) = self.load_data(dataset_path, sep)

		self.X_train = np.asarray(X_train).astype(np.float64)
		self.X_test = np.asarray(X_test).astype(np.float64)
		self.y_train = np.asarray(y_train).astype(int)
		self.y_test = np.asarray(y_test).astype(int)

		print("Train:", self.X_train.shape[0])
		print("Test: ", self.X_test.shape[0])

		# defining model
		self.model=Sequential()
		# adding convolution layer
		self.model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
		# adding pooling layer
		self.model.add(MaxPool2D(2, 2))
		# adding convolution layer
		self.model.add(Conv2D(64, (3, 3), activation='relu'))
		# adding pooling layer
		self.model.add(MaxPool2D(2, 2))
		# adding fully connected layer
		self.model.add(Flatten())
		self.model.add(Dense(100, activation='relu'))
		# adding output layer
		self.model.add(Dense(number_of_classes, activation='softmax'))
		# compiling the model
		self.model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
		# fitting the model
		self.model.fit(self.X_train, self.y_train, epochs=epochs)

	def evaluate(self):
		# evaluting the model
		self.model.evaluate(self.X_test, self.y_test)

	def predict(self, X_pred):
		X_pred = np.array(X_pred)
		X_pred = X_pred.reshape((X_pred.shape[0], X_pred.shape[1], X_pred.shape[2], 1))
		return self.model.predict(np.asarray(X_pred).astype(np.float64))

	def get_letter(self, number):
		return self.number2letter[number]

	def load_data(self, dataset_path, sep):
		#letter2number = dict()
		#number2letter = dict()
		data = []  # np.array([[]], dtype=np.ndarray)
		target = []  # np.array([[]], dtype=np.ndarray)

		ii = 0
		for letter_path in os.listdir(dataset_path):
			if letter_path not in "йцукенгшщзхъфывапролджэячсмитьбю?.,":
				# "йцукенгшщзхъфывапролджэячсмитьбю":
				continue
			path = dataset_path + "/" + letter_path + "/"
			target_letter = open(path + "target.txt", "r")
			letter = str(target_letter.read())
			self.number2letter[ii] = letter
			self.letter2number[letter] = ii

			files = os.listdir(path)
			for j, picture_path in enumerate(files):
				if picture_path.endswith(".png"):
					img = cv2.imread(path + "/" + picture_path)
					gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
					picture = 1 - gray/255
					data.append(np.array(picture, dtype=np.float32))  # .reshape(-1, 1)
					target.append(ii)  # vectorized_result(letter))
			ii += 1
		d = np.array(data, dtype=np.ndarray)
		print(len(data), d.shape)

		d = d.reshape((d.shape[0], d.shape[1], d.shape[2], 1))
		t = np.array(target, dtype=np.ndarray)

		# (X_, y_) = pickle.load(dataset_path)
		# print(X_.shape, y_.shape)
		d = np.array(d, dtype=np.ndarray)
		t = np.array(t, dtype=np.ndarray)
		num = int(t.shape[0] * (1-sep))
		rand_ids = random.sample(range(t.shape[0]), t.shape[0])
		train_ids = rand_ids[:num]
		test_ids = rand_ids[num:]
		# number_of_classes = np.max(t) + 1

		X_train = d[train_ids, :, :]
		y_train = t[train_ids]
		X_test = d[test_ids, :, :]
		y_test = t[test_ids]
		return (X_train, y_train), (X_test, y_test)

def vectorized_result(letter):
		e = np.zeros((number_of_classes, 1), dtype=np.float32)
		e[letter2number[letter]] = 1.0
		return e

#dataset_file_read = open("/home/alex/Proga/Project/second_dataset.pickle", "rb")
'''dataset_path = "/home/alex/Proga/Project/second_dataset"
number_of_classes = 91
network = TensorFlowNetwork(number_of_classes, dataset_path)
network.evaluate()'''

'''
number_of_classes = 10
#loading data
(X_train,y_train) , (X_test,y_test)=mnist.load_data()
#reshaping data
X_train = X_train.reshape((X_train.shape[0], X_train.shape[1], X_train.shape[2], 1))[:400, :, :, :]
X_test = X_test.reshape((X_test.shape[0],X_test.shape[1],X_test.shape[2],1))[:400, :, :, :]
y_train = y_train[:400]
y_test = y_test[:400]
#normalizing the pixel values
X_train=X_train/255
X_test=X_test/255
#checking the shape after reshaping
print(X_train.shape, X_train.dtype, y_train)#, X_train[:1, :, :, :])
print(X_test.shape, y_test.shape, y_test)'''


import logging

from blockchainetl_common.jobs.exporters.console_item_exporter import ConsoleItemExporter
from blockchainetl_common.jobs.exporters.in_memory_item_exporter import InMemoryItemExporter
from bandetl.enumeration.entity_type import EntityType
from bandetl.jobs.export_blocks_job import ExportBlocksJob

from bandetl.streaming.item_id_calculator import ItemIdCalculator

class BandStreamerAdapter:
    def __init__(
            self,
            band_service,
            item_exporter=ConsoleItemExporter(),
            batch_size=100,
            max_workers=5,
            entity_types=tuple(EntityType.ALL_FOR_STREAMING)):
        self.band_service = band_service
        self.item_exporter = item_exporter
        self.batch_size = batch_size
        self.max_workers = max_workers
        self.entity_types = entity_types
        self.item_id_calculator = ItemIdCalculator()

    def open(self):
        self.item_exporter.open()

    def get_current_block_number(self):
        latest_block = self.band_service.get_latest_block()
        return int(latest_block['block']['header']['height'])

    def export_all(self, start_block, end_block):
        blocks, transactions, messages, logs, block_events, oracle_requests = [], [], [], [], [], []
        if self._should_export(EntityType.BLOCK) or \
                self._should_export(EntityType.BLOCK_EVENT) or \
                self._should_export(EntityType.TRANSACTION) or \
                self._should_export(EntityType.LOG) or \
                self._should_export(EntityType.MESSAGE) or \
                self._should_export(EntityType.ORACLE_REQUEST):
            blocks, transactions, messages, logs, block_events, oracle_requests = self._export_blocks(start_block, end_block)

        logging.info('Exporting with ' + type(self.item_exporter).__name__)

        all_items = blocks + \
            transactions + \
            messages + \
            logs + \
            block_events + \
            oracle_requests

        self.calculate_item_ids(all_items)

        self.item_exporter.export_items(all_items)

    def _export_blocks(self, start_block, end_block):
        item_exporter = InMemoryItemExporter(item_types=EntityType.ALL_FOR_STREAMING)
        job = ExportBlocksJob(
            start_block=start_block,
            end_block=end_block,
            batch_size=self.batch_size,
            band_service=self.band_service,
            max_workers=self.max_workers,
            item_exporter=item_exporter,
            export_blocks=self._should_export(EntityType.BLOCK),
            export_logs=self._should_export(EntityType.LOG),
            export_block_events=self._should_export(EntityType.BLOCK_EVENT),
            export_oracle_requests=self._should_export(EntityType.ORACLE_REQUEST),
            export_messages=self._should_export(EntityType.MESSAGE),
            export_transactions=self._should_export(EntityType.TRANSACTION)
        )
        job.run()
        blocks = item_exporter.get_items(EntityType.BLOCK)
        transactions = item_exporter.get_items(EntityType.TRANSACTION)
        messages = item_exporter.get_items(EntityType.MESSAGE)
        logs = item_exporter.get_items(EntityType.LOG)
        block_events = item_exporter.get_items(EntityType.BLOCK_EVENT)
        oracle_requests = item_exporter.get_items(EntityType.ORACLE_REQUEST)
        return blocks, transactions, messages, logs, block_events, oracle_requests

    def _should_export(self, entity_type):
        if entity_type == EntityType.BLOCK \
                or entity_type == EntityType.BLOCK_EVENT \
                or entity_type == EntityType.LOG \
                or entity_type == EntityType.ORACLE_REQUEST \
                or entity_type == EntityType.MESSAGE \
                or entity_type == EntityType.TRANSACTION:
            return entity_type in self.entity_types

        raise ValueError('Unexpected entity type ' + entity_type)

    def calculate_item_ids(self, items):
        for item in items:
            item['item_id'] = self.item_id_calculator.calculate(item)

    def close(self):
        self.item_exporter.close()


"""Stream type classes for tap-impact-publisher."""

import copy
import urllib.parse
from pathlib import Path
import time
from typing import Any, cast, Dict, Optional, Union, List, Iterable, Tuple

import pendulum
import requests
from singer_sdk import typing as th  # JSON Schema typing helpers
from singer_sdk.helpers.jsonpath import extract_jsonpath

from tap_impact_publisher.client import impactPublisherStream

SCHEMAS_DIR = Path(__file__).parent / Path("./schemas")

class AccountsStream(impactPublisherStream):
    """Define custom stream."""
    name = "accounts"
    path = "/CompanyInformation"
    primary_keys = ["CompanyName"]
    replication_key = None
    schema_filepath = SCHEMAS_DIR / "accounts.schema.json"

class ReportsStream(impactPublisherStream):
    """Define custom stream."""
    name = "reports"
    path = "/Reports/mp_action_listing_sku.json"
    primary_keys = ["@uri"]
    replication_key = 'Action_Date'
    schema_filepath = SCHEMAS_DIR / "reports.schema.json"
    records_jsonpath = '$.Records[*]'

    def get_starting_timestamp(
        self, context: Optional[dict]
    ) -> Optional[pendulum.datetime]:
        """Get starting replication timestamp.

        Will return the value of the stream's replication key when `--state` is passed.
        If no state exists, will return `start_date` if set, or `None` if neither
        the stream state nor `start_date` is set.

        Developers should use this method to seed incremental processing for date
        and datetime replication keys. For non-datetime replication keys, use
        :meth:`~singer_sdk.Stream.get_starting_replication_key_value()`

        Args:
            context: Stream partition or context dictionary.

        Returns:
            `start_date` from config, or state value if using timestamp replication.

        Raises:
            ValueError: If the replication value is not a valid timestamp.
        """
        state = self.get_context_state(context)
        self.logger.info(state)
        if state:
            value = state['starting_replication_value']
        else:
            value = self.config['start_date']

        return pendulum.parse(value, tz="America/New_York")

    def _make_time_chunks(
        self,
        context: Optional[dict]
    ) -> Iterable[Tuple[int, int]]:
        """generate chunks of time to be used in URL parameterization.
           Since data can be updated up to six months after the action date, we need to
           subtract six month back from the latest replication value to get the starting date

        Args:
            context (Optional[dict]): Optional stream context.

        Returns:
            Iterable[Tuple[int, int]]: Iterable with start/end dates for the pertinent months
        """
        start = self.get_starting_timestamp(context)
        if start != pendulum.parse(self.config['start_date'], tz="America/New_York"):
            start = start.subtract(months=12).start_of('week')

        iterable = (
            (start.add(weeks=i), start.add(weeks=i).end_of("week"))
            for i in range(
                pendulum.yesterday('America/New_York').diff(
                    start
                ).in_weeks() + 1
            )
        )
        return iterable

    def get_url_params(
        self,
        start_date: Optional[str],
        end_date: Optional[str],
        next_page_token: Optional[Any] = None
    ) -> Dict[str, Any]:
        """Return a dictionary of values to be used in URL parameterization."""
        params: dict = {
            "SUPERSTATUS_MS": ["APPROVED", "NA", "PENDING", "REVERSED"],
            "SHOW_STATUS_DETAIL": 1,
            "SHOW_PAYSTUB": 1,
            "SHOW_SKU": 1,
            "SHOW_LOCKING_DATE": 1,
            "SHOW_MODIFICATION_DATE": 1,
            "SHOW_SCHEDULED_CLEARING_DATE": 1,
            "SHOW_AD": 1,
            "SHOW_GEO_LOCATION": 1,
            "SHOW_IP_ADDRESS": 1,
            "SHOW_REFERRAL_DATE": 1,
            "SHOW_REFERRAL_TYPE": 1,
            "SHOW_REFERRING_URL": 1,
            "SHOW_ORIGINAL_SALEAMOUNT": 1,
            "SHOW_CUSTOMER_ID": 1,
            "SHOW_ORIGINAL_PAYOUT": 1,
            "SHOW_CUSTDATE1": 1,
            "SHOW_CUSTDATE2": 1,
            "SHOW_ADVSTRING1": 1,
            "SHOW_ADVSTRING2": 1,
            "SHOW_CALL": 1,
            "SHOW_USER_AGENT2": 1,
            "SHOW_CURRENCY_CONV": 1,
            "START_DATE": start_date,
            "END_DATE": end_date
        }
        if next_page_token:
            return urllib.parse.parse_qs(urllib.parse.urlparse(next_page_token).query)
        if self.replication_key:
            params["sort"] = "asc"
            params["order_by"] = self.replication_key
        return params

    def prepare_request(
        self, start_date, end_date, context: Optional[dict], next_page_token: Optional[Any]
    ) -> requests.PreparedRequest:
        """Prepare a request object.

        If partitioning is supported, the `context` object will contain the partition
        definitions. Pagination information can be parsed from `next_page_token` if
        `next_page_token` is not None.

        Args:
            context: Stream partition or context dictionary.
            next_page_token: Token, page number or any request argument to request the
                next page of data.

        Returns:
            Build a request with the stream's URL, path, query parameters,
            HTTP headers and authenticator.
        """
        http_method = self.rest_method
        url: str = self.get_url(context)
        self.logger.info(
            f"Making params for {start_date.to_date_string()} to {end_date.to_date_string()}"
        )
        params: dict = self.get_url_params(
            start_date=start_date.format('YYYY-MM-DD'),
            end_date=end_date.format('YYYY-MM-DD'),
            next_page_token=next_page_token,
        )
        request_data = self.prepare_request_payload(context, next_page_token)
        headers = self.http_headers

        authenticator = self.authenticator
        if authenticator:
            headers.update(authenticator.auth_headers or {})
            params.update(authenticator.auth_params or {})

        request = cast(
            requests.PreparedRequest,
            self.requests_session.prepare_request(
                requests.Request(
                    method=http_method,
                    url=url,
                    params=params,
                    headers=headers,
                    json=request_data,
                ),
            ),
        )
        self.logger.info(f'requesting url :{request.url}')
        return request

    def request_records(self, context: Optional[dict]) -> Iterable[dict]:
        """Request records from REST endpoint(s), returning response records.

        If pagination is detected, pages will be recursed automatically.

        Args:
            context: Stream partition or context dictionary.

        Yields:
            An item for every record in the response.

        Raises:
            RuntimeError: If a loop in pagination is detected. That is, when two
                consecutive pagination tokens are identical.
        """
        next_page_token: Any = None
        decorated_request = self.request_decorator(self._request)

        for start, end in self._make_time_chunks(context):
            finished = False
            self.logger.info(f"Fetching {self.name} from {start} to {end}")

            while not finished:
                prepared_request = self.prepare_request(
                    start_date=start,
                    end_date=end,
                    context=context,
                    next_page_token=next_page_token
                )
                resp = decorated_request(prepared_request, context)
                for row in self.parse_response(resp):
                    yield row
                time.sleep(1)
                self.finalize_state_progress_markers(
                    {"bookmarks": {self.name: {"replication_key_value": end}}}
                )
                previous_token = copy.deepcopy(next_page_token)
                next_page_token = self.get_next_page_token(
                    response=resp, previous_token=previous_token
                )
                if next_page_token and next_page_token == previous_token:
                    raise RuntimeError(
                        f"Loop detected in pagination. "
                        f"Pagination token {next_page_token} is identical to prior token."
                    )
                # Cycle until get_next_page_token() no longer returns a value
                finished = not next_page_token

    def parse_response(self, response: requests.Response) -> Iterable[dict]:
        """Parse the response and return an iterator of result rows."""
        yield from extract_jsonpath(self.records_jsonpath, input=response.json())


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

# # 9-2. Three Restaurants: Start with your class from Exercise 9-1. Create three different instances from the class, and call describe_restaurant() for each instance.

# In[4]:

class Restaurant():
    """A class that represents a restaurant."""

    def __init__(self, restaurant_name, cuisine_type):
        """A method which represents the arguments which will be passing here instead of passing in the Restaurant()"""
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type

    def describe_restaurant(self):
        """A methos which just simply describes the restaurant."""
        print(f"Restaurant name : {self.restaurant_name.title()}")
        print(f"Cuisine type : {self.cuisine_type.title()}")

    def open_restaurant(self):
        """A method that indicates whether the reaturant is open or not."""
        print(f"{self.restaurant_name.title()} is Open.")

# In[5]:

restaurant1 = Restaurant("royal dine", "a/c")
restaurant2 = Restaurant("pizzarino", "a/c")
restaurant3 = Restaurant("pasta ride", "non a/c")

# In[6]:

restaurant1.describe_restaurant()
restaurant1.open_restaurant()

# In[8]:

restaurant2.describe_restaurant()
restaurant2.open_restaurant()

# In[11]:

restaurant3.describe_restaurant()
restaurant3.open_restaurant()

# In[14]:

restaurant1.restaurant_name

# In[15]:

restaurant1.cuisine_type

# In[16]:

restaurant2.restaurant_name

# In[17]:

restaurant2.cuisine_type

# In[20]:

restaurant3.restaurant_name

# In[21]:

restaurant3.cuisine_type


#!/usr/bin/env python
from __future__ import absolute_import, unicode_literals
import environ
import logging

env = environ.Env()
environ.Env.read_env()

def configure():
    from .queue_listner import queue_listner
    from .loggers import get_logger_settings
    logging_dict = get_logger_settings(
        env_name=env("LOG_ENV_NAME"),
        log_dir=env("LOG_DIR"),
        log_file_name=env("LOG_FILE_NAME"),
        application_log_level=env("APPLICATION_LOG_LEVEL", default="INFO"),
        logstash_listner_ip=env("LOGSTASH_LISTENER_IP", default=None),
        logstash_listner_port=env("LOGSTASH_LISTENER_PORT", default=None),
        logstash_tags=env.list("LOGSTASH_TAGS", default=[]),

        cloudwatch_logging_enabled=False,  # env.bool("CLOUDWATCH_LOGGING_ENABLED", default=False),
        aws_access_key_id=env("AWS_ACCESS_KEY_ID"),
        aws_secret_access_key=env("AWS_SECRET_ACCESS_KEY"),
        aws_region_name=env("AWS_REGION_NAME"),
        cloudwatch_log_group=env("CLOUDWATCH_LOG_GROUP"),
        cloud_watch_log_stream=env("CLOUD_WATCH_LOG_STREAM"),
    )
    logging.config.dictConfig(logging_dict)
    queue_listner.start()


import os
import copy

def part1(inp):
	stacks, moves = inp
	for move in moves:
		amount, src, dest = move
		for _ in range(amount):
			stacks[dest].append(stacks[src].pop())

	return ''.join([s[-1] for s in stacks])

def part2(inp):
	stacks, moves = inp
	for move in moves:
		amount, src, dest = move

		stacks[dest].extend(stacks[src][-amount:])
		stacks[src] = stacks[src][:-amount]

	return ''.join([s[-1] for s in stacks])

def parse_input(inp):
	stack_img_str = []
	moves = []

	state = 0
	for line in inp:
		if line.strip() == '':
			state = 1
		elif state == 0:
			stack_img_str.append(line)
		elif state == 1:
			_, amount, _, src, _, dest = line.split(' ')
			moves.append((int(amount), int(src)-1, int(dest)-1)) # 0 indexing
	# parse stacks
	stacks = []
	stacks_amount = None
	for s in reversed(stack_img_str):
		if state == 2:
			# stack
			for i in range(stacks_amount):
				char = s[4 * i + 1]
				if char != ' ':
					stacks[i].append(char)
		elif state == 1:
			# init stacks
			stacks_amount = int(s.strip()[-1])
			for i in range(stacks_amount): # 0 indexing
				stacks.append([]) # new list
			state = 2
	return (stacks, moves)

def get_input(fname):
	inp_file_name = f"{fname}_input.txt"
	with open(inp_file_name, 'r') as f:
		return [line.strip('\n') for line in f]

def main():
	fname = os.path.splitext(os.path.basename(__file__))[0]
	day = int(fname[3:]) # dayXX
	inp = get_input(fname)
	inp = parse_input(inp)
	print(f"\n === Day {day} ===")
	print(f" - Part 1: {part1(copy.deepcopy(inp))}") # pass a deep copy so the stacks remain pristine for p2
	print(f" - Part 2: {part2(inp)}")

if __name__ == '__main__':
	main()


from app import db, app_config
from run import config_name
from ..company.model import Company

def dump_date(data):
    """Deserialize datetime object into string form for JSON processing.
    :param data:
    """
    if data is None:
        return None
    return data.strftime("%Y-%m-%d")

class Transfer(db.Model):
    __tablename__ = 'transferencia'
    id = db.Column(db.Integer, primary_key=True)
    empresa = db.Column(db.Integer, nullable=False)
    nome = db.Column(db.String(65), nullable=False)
    currency = db.Column(db.String(10), nullable=False)
    valor_compra = db.Column(db.Float, nullable=False)
    valor_deposit = db.Column(db.Float, nullable=False)
    valor_currency = db.Column(db.Float, nullable=False)
    banco = db.Column(db.Integer, nullable=False)
    banco_name = db.Column(db.String(30), nullable=False)
    banco_img = db.Column(db.String(60), nullable=False)
    data = db.Column(db.String(65), nullable=False)
    n_transferencia = db.Column(db.Integer, nullable=False)
    imglink = db.Column(db.String(60), nullable=False)
    status_id = db.Column(db.Integer, nullable=False)
    status = db.Column(db.String(20), nullable=False)
    ip = db.Column(db.String(45), nullable=True, default=None)

    def insert(self):
        db.session.add(self)
        db.session.commit()

    def check_status(self):
        if self.status_id == 2:
            return True
        return False

    def update(self, nome, currency, valor_compra, valor_deposit, valor_currency, banco, banco_name,
               data, n_transferencia, imglink, status_id, status):
        valor_anterior = float(self.valor_deposit)
        self.nome = nome
        self.currency = currency
        self.valor_compra = valor_compra
        self.valor_deposit = valor_deposit
        self.valor_currency = valor_currency
        self.banco = banco
        self.banco_name = banco_name
        self.data = data
        self.n_transferencia = n_transferencia
        self.imglink = imglink
        self.status_id = status_id
        self.status = status
        try:
            db.session.commit()
            if self.check_status():
                company = Company.get_company(self.empresa)
                company.update_saldo(valor_anterior, valor_deposit)
            return 200

        except ConnectionRefusedError:
            return 500

    def cancel(self):
        self.status_id = 1
        try:
            db.session.commit()
            company = Company.get_company(self.empresa)
            company.decrease_saldo(self.valor_brl)
            return 200
        except ConnectionRefusedError:
            return 500

    def delete(self):
        trans = self
        print(self)
        try:
            if self.check_status():
                company = Company.get_company(self.empresa)
                company.decrease_saldo(self.valor_brl)
            db.session.delete(trans)
            db.session.commit()
            return 200
        except ConnectionRefusedError:
            return 500

    @staticmethod
    def serialize(transfer):
        return {
            'id': transfer.id,
            'empresa': transfer.empresa,
            'nome': transfer.nome,
            'currency': transfer.currency,
            'valor_compra': transfer.valor_compra,
            'valor_deposit': transfer.valor_deposit,
            'valor_currency': transfer.valor_currency,
            'banco': transfer.banco,
            'banco_name': transfer.banco_name,
            'banco_img': transfer.banco_img,
            'data': dump_date(transfer.data),
            'n_transferencia': transfer.n_transferencia,
            'imglink': transfer.imglink,
            'status_id': transfer.status_id,
            'status': transfer.status,
            'ip': transfer.ip
        }

    @staticmethod
    def get_transfers():
        transfer = Transfer.query.filter_by().order_by(Transfer.id.desc())

        if not transfer:
            response = {
                'error_msg': "There's no card to show.",
                'return': False
            }
        else:
            resp = [Transfer.serialize(aux) for aux in transfer]
            response = {
                'error_msg': False,
                'return': resp
            }
        return response

    @staticmethod
    def get_transfer(transfer_id):
        trans = Transfer.query.filter_by(id=transfer_id).first()
        if not trans:
            return None
        else:
            return trans