Skip to content

Commit

Permalink
Update freeze to freeze values inside pmaps and pvectors
Browse files Browse the repository at this point in the history
  • Loading branch information
phil-arh authored and npfbdevopslead committed Sep 14, 2020
1 parent 9227c9e commit 6522a4e
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions pyrsistent/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
from pyrsistent._pset import PSet, pset
from pyrsistent._pvector import PVector, pvector

# get PVector and PMap types for freeze - PVector type is different if C
# extension has loaded
pmap_type = type(pmap())
pvector_type = type(pvector())

def freeze(o):
def freeze(o, nonstrict=False):
"""
Recursively convert simple Python containers into pyrsistent versions
of those containers.
Expand All @@ -14,6 +18,11 @@ def freeze(o):
- set is converted to pset, but not recursively
- tuple is converted to tuple, recursively.
If nonstrict == False:
- freeze is called on elements of pvectors
- freeze is called on values of pmaps
Sets and dict keys are not recursively frozen because they do not contain
mutable data by convention. The main exception to this rule is that
dict keys and set elements are often instances of mutable objects that
Expand All @@ -27,12 +36,12 @@ def freeze(o):
(1, pvector([]))
"""
typ = type(o)
if typ is dict:
return pmap(dict((k, freeze(v)) for k, v in o.items()))
if typ is list:
return pvector(map(freeze, o))
if typ is dict or (not nonstrict and typ is pmap_type):
return pmap(dict((k, freeze(v, nonstrict)) for k, v in o.items()))
if typ is list or (not nonstrict and typ is pvector_type):
return pvector(map(lambda x: freeze(x, nonstrict), o))
if typ is tuple:
return tuple(map(freeze, o))
return tuple(map(lambda x: freeze(x, nonstrict), o))
if typ is set:
return pset(o)
return o
Expand Down

0 comments on commit 6522a4e

Please sign in to comment.