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