Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import types
- class XTuple(tuple):
- def _tree_map(f, o, tree_types=(list, tuple)):
- if isinstance(o, tree_types):
- print("TREE_TYPE! %s" % [value for value in o])
- return type(o)(XTuple._tree_map(f, value) for value in o)
- if isinstance(o,types.GeneratorType):
- print("GENERATOR!")
- return [XTuple._tree_map(f, value) for value in o]
- if isinstance(o,dict):
- print("DICT_TYPE! %s" % [value for value in o.items()])
- return {k:XTuple._tree_map(f,v) for k,v in o.items()}
- else:
- return f(o)
- def __new__(cls,*args,**kwargs):
- return tuple.__new__(XTuple, XTuple._tree_map(lambda x: x or "*NONE*",*args or "*NONE*"))
- def __str__(self):
- return tuple.__str__(self)
- if __name__ == "__main__":
- input = [1,2,None,[3,[None,5],55],{'a':1,'b':None}]
- print("input=%s"%input)
- t = XTuple(input)
- print("Here's T:")
- print(str(t))
- print("That was T.")
- print(str(t))
- print("T again.")
- print("type(t)=%s" % type(t))
- print("isinstance(t,XTuple)=%s" % isinstance(t,XTuple))
- # Breaks
- print("XTuple(input)=%s" % str(t))
- # Breaks
- u = XTuple(t)
- print(str(u))
- print("XTuple(t)=%s" % str(u))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement