_helpers.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import collections
  2. from functools import wraps
  3. from pyrsistent._pmap import PMap, pmap
  4. from pyrsistent._pset import PSet, pset
  5. from pyrsistent._pvector import PVector, pvector
  6. def freeze(o, strict=True):
  7. """
  8. Recursively convert simple Python containers into pyrsistent versions
  9. of those containers.
  10. - list is converted to pvector, recursively
  11. - dict is converted to pmap, recursively on values (but not keys)
  12. - defaultdict is converted to pmap, recursively on values (but not keys)
  13. - set is converted to pset, but not recursively
  14. - tuple is converted to tuple, recursively.
  15. If strict == True (default):
  16. - freeze is called on elements of pvectors
  17. - freeze is called on values of pmaps
  18. Sets and dict keys are not recursively frozen because they do not contain
  19. mutable data by convention. The main exception to this rule is that
  20. dict keys and set elements are often instances of mutable objects that
  21. support hash-by-id, which this function can't convert anyway.
  22. >>> freeze(set([1, 2]))
  23. pset([1, 2])
  24. >>> freeze([1, {'a': 3}])
  25. pvector([1, pmap({'a': 3})])
  26. >>> freeze((1, []))
  27. (1, pvector([]))
  28. """
  29. typ = type(o)
  30. if typ is dict or (strict and isinstance(o, PMap)):
  31. return pmap({k: freeze(v, strict) for k, v in o.items()})
  32. if typ is collections.defaultdict or (strict and isinstance(o, PMap)):
  33. return pmap({k: freeze(v, strict) for k, v in o.items()})
  34. if typ is list or (strict and isinstance(o, PVector)):
  35. curried_freeze = lambda x: freeze(x, strict)
  36. return pvector(map(curried_freeze, o))
  37. if typ is tuple:
  38. curried_freeze = lambda x: freeze(x, strict)
  39. return tuple(map(curried_freeze, o))
  40. if typ is set:
  41. # impossible to have anything that needs freezing inside a set or pset
  42. return pset(o)
  43. return o
  44. def thaw(o, strict=True):
  45. """
  46. Recursively convert pyrsistent containers into simple Python containers.
  47. - pvector is converted to list, recursively
  48. - pmap is converted to dict, recursively on values (but not keys)
  49. - pset is converted to set, but not recursively
  50. - tuple is converted to tuple, recursively.
  51. If strict == True (the default):
  52. - thaw is called on elements of lists
  53. - thaw is called on values in dicts
  54. >>> from pyrsistent import s, m, v
  55. >>> thaw(s(1, 2))
  56. {1, 2}
  57. >>> thaw(v(1, m(a=3)))
  58. [1, {'a': 3}]
  59. >>> thaw((1, v()))
  60. (1, [])
  61. """
  62. typ = type(o)
  63. if isinstance(o, PVector) or (strict and typ is list):
  64. curried_thaw = lambda x: thaw(x, strict)
  65. return list(map(curried_thaw, o))
  66. if isinstance(o, PMap) or (strict and typ is dict):
  67. return {k: thaw(v, strict) for k, v in o.items()}
  68. if typ is tuple:
  69. curried_thaw = lambda x: thaw(x, strict)
  70. return tuple(map(curried_thaw, o))
  71. if isinstance(o, PSet):
  72. # impossible to thaw inside psets or sets
  73. return set(o)
  74. return o
  75. def mutant(fn):
  76. """
  77. Convenience decorator to isolate mutation to within the decorated function (with respect
  78. to the input arguments).
  79. All arguments to the decorated function will be frozen so that they are guaranteed not to change.
  80. The return value is also frozen.
  81. """
  82. @wraps(fn)
  83. def inner_f(*args, **kwargs):
  84. return freeze(fn(*[freeze(e) for e in args], **dict(freeze(item) for item in kwargs.items())))
  85. return inner_f