Guest User

Untitled

a guest
May 28th, 2014
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. def dump(obj):
  2.     def seq(obj):
  3.         return " ".join([dump(i) for i in obj])
  4.  
  5.     if obj is None:
  6.         return "nil"
  7.     elif isinstance(obj, bool):
  8.         if obj:
  9.             return "true"
  10.         else:
  11.             return "false"
  12.     elif isinstance(obj, (int, long, float)):
  13.         return str(obj)
  14.     elif isinstance(obj, decimal.Decimal):
  15.         return "{}M".format(obj)
  16.     elif isinstance(obj, basestring):
  17.         return '"{}"'.format(obj)
  18.     elif isinstance(obj, tuple):
  19.         return "({})".format(seq(obj))
  20.     elif isinstance(obj, list):
  21.         return "[{}]".format(seq(obj))
  22.     elif isinstance(obj, set) or isinstance(obj, frozenset):
  23.         return "#{{{}}}".format(seq(obj))
  24.     elif isinstance(obj, dict):
  25.         return "{{{}}}".format(seq(itertools.chain.from_iterable(obj.items())))
  26.     elif isinstance(obj, datetime.date):
  27.         return '#inst "{}"'.format(obj.isoformat())
  28.     elif isinstance(obj, uuid.UUID):
  29.         return '#uuid "{}"'.format(obj)
  30.     else:
  31.         raise NotImplementedError(
  32.             "Don't know how to handle {} : {}", type(obj), obj)
Advertisement
Add Comment
Please, Sign In to add comment