Print a tree.

  Sometimes it's useful to print datastructures as a tree. This function prints
  out a pretty tree with root `node`. A tree is represented as a :class:`dict`,
  whose keys are node names and values are :class:`dict` objects for sub-trees
  and :class:`None` for terminals.

  :param dict node: The root of the tree to print.
  :param callable formatter: A callable that takes a single argument, the key,
    that formats the key in the tree.
  :param callable prefix: A callable that takes a single argument, the key,
    that adds any additional text before the formatted key.
  :param callable postfix: A callable that takes a single argument, the key,
    that adds any additional text after the formatted key.

def tree(node, formatter=None, prefix=None, postfix=None, _depth=1):
  """Print a tree.

  Sometimes it's useful to print datastructures as a tree. This function prints
  out a pretty tree with root `node`. A tree is represented as a :class:`dict`,
  whose keys are node names and values are :class:`dict` objects for sub-trees
  and :class:`None` for terminals.

  :param dict node: The root of the tree to print.
  :param callable formatter: A callable that takes a single argument, the key,
    that formats the key in the tree.
  :param callable prefix: A callable that takes a single argument, the key,
    that adds any additional text before the formatted key.
  :param callable postfix: A callable that takes a single argument, the key,
    that adds any additional text after the formatted key.

  """
  current = 0
  length = len(node.keys())
  tee_joint = '\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80'
  elbow_joint = '\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80'
  for key, value in node.iteritems():
    current += 1
    k = formatter(key) if formatter else key
    pre = prefix(key) if prefix else ''
    post = postfix(key) if postfix else ''
    space = elbow_joint if current == length else tee_joint
    yield ' {space} {prefix}{key}{postfix}'.format(space=space, key=k, prefix=pre, postfix=post)
    if value:
      for e in tree(value, formatter=formatter, prefix=prefix, postfix=postfix, _depth=_depth + 1):
        yield (' |  ' if current != length else '    ') + e