Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. import types
  2.  
  3. class XTuple(tuple):
  4.  
  5. def _tree_map(f, o, tree_types=(list, tuple)):
  6. if isinstance(o, tree_types):
  7. print("TREE_TYPE! %s" % [value for value in o])
  8. return type(o)(XTuple._tree_map(f, value) for value in o)
  9.  
  10. if isinstance(o,types.GeneratorType):
  11. print("GENERATOR!")
  12. return [XTuple._tree_map(f, value) for value in o]
  13.  
  14. if isinstance(o,dict):
  15. print("DICT_TYPE! %s" % [value for value in o.items()])
  16. return {k:XTuple._tree_map(f,v) for k,v in o.items()}
  17. else:
  18. return f(o)
  19.  
  20. def __new__(cls,*args,**kwargs):
  21. return tuple.__new__(XTuple, XTuple._tree_map(lambda x: x or "*NONE*",*args or "*NONE*"))
  22.  
  23. def __str__(self):
  24. return tuple.__str__(self)
  25.  
  26.  
  27. if __name__ == "__main__":
  28.  
  29. input = [1,2,None,[3,[None,5],55],{'a':1,'b':None}]
  30.  
  31. print("input=%s"%input)
  32.  
  33. t = XTuple(input)
  34.  
  35. print("Here's T:")
  36. print(str(t))
  37.  
  38. print("That was T.")
  39. print(str(t))
  40. print("T again.")
  41.  
  42. print("type(t)=%s" % type(t))
  43. print("isinstance(t,XTuple)=%s" % isinstance(t,XTuple))
  44.  
  45. # Breaks
  46. print("XTuple(input)=%s" % str(t))
  47.  
  48. # Breaks
  49. u = XTuple(t)
  50.  
  51. print(str(u))
  52. print("XTuple(t)=%s" % str(u))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement