import collections
import sys


class FrissError(Exception):
    pass


class Node:
    
    def __init__(self, name):
        self.name = name
        self.children = []
        self.transitions = []

    def __iter__(self):
        for c, t in zip(self.children, self.transitions):
            yield (c, t)

    def add_child(self, child_node, start_y, end_y, prob):
        self.children.append(child_node)
        self.transitions.append(
            {"start_y": start_y, "end_y": end_y, "prob": prob})

    @property
    def leaf(self):
        if not self.children:
            return True
        return False

    def travel(self, mode="bfs"):
        assert mode in {"bfs", "dfs"}
        yield self
        q = collections.deque(self.children)
        while q:
            if mode == "bfs":
                node = q.popleft()
            elif mode == "dfs":
                node = q.pop()
            yield node
            for c in node.children:
                q.append(c)


def friss_stderr(msg, type_=None):
    fmt = ""
    if type_ == "WARN":
        fmt = "Warning: "
    elif type_ == "ERR":
        fmt = "Error: "
    print("FRISS Rails: {}{}".format(fmt, msg), file=sys.stderr)
