Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions freezedata/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
from .freezedata import freeze_data

DIR = os.path.dirname(__file__)
README = os.path.join(DIR, 'README.rst')

__all__ = ['freeze_data']

__doc__ = """
Recursively convert lists to tuples, sets to frozensets, dicts to mappingproxy etc.
Expand Down Expand Up @@ -32,5 +32,3 @@
>> TypeError: unhashable type: 'mappingproxy'
"""

del os, DIR, README
del freezedata
19 changes: 15 additions & 4 deletions freezedata/freezedata.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import types
from collections import namedtuple


def freeze_data(obj, *, allow=frozenset()):
if not hasattr(types, "MappingProxyType"):
# to support Python2 platform
try:
from frozen_dict import FrozenDict as MappingProxyType
except ImportError:
try:
from frozendict import frozendict as MappingProxyType
except ImportError:
# use default dict instead (making more sense then raising exception)
MappingProxyType = dict

types.MappingProxyType = MappingProxyType

def freeze_data(obj, allow=frozenset()):
"""Convert 'obj' recursively to a read-only object but selectively
allow functions, modules and other hashables, which may not be read-only.
This means that recursively:
Expand Down Expand Up @@ -48,11 +60,10 @@ class instances (beside from built-in immutables like 9, "er", 9.7 etc.) will ra
if isinstance(obj, types.ModuleType):
return freeze_class_or_istance_or_module(obj, allow=allow)
else:
print(123)
return freeze_data_inner(obj, allow=allow)


def freeze_data_inner(obj, *, allow=frozenset()):
def freeze_data_inner(obj, allow=frozenset()):
# check type and recursively return a new read-only object
if isinstance(obj, (str, int, float, bytes, type(None), bool)):
return obj
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
frozendict