-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6ff4ef
commit da593a7
Showing
5 changed files
with
49 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from collections import abc | ||
|
||
|
||
class ImmutableDict(abc.Mapping): | ||
def __init__(self, d): | ||
self._d = d | ||
|
||
def __getitem__(self, key): | ||
return self._d[key] | ||
|
||
def __iter__(self): | ||
return iter(self._d) | ||
|
||
def __len__(self): | ||
return len(self._d) | ||
|
||
def __contains__(self, item): | ||
return item in self._d | ||
|
||
def __hash__(self): | ||
return hash(frozenset(self._d.items())) | ||
|
||
|
||
def make_immutable(thing): | ||
if isinstance(thing, abc.Mapping): | ||
return ImmutableDict( | ||
{key: make_immutable(value) for key, value in thing.items()} | ||
) | ||
|
||
if isinstance(thing, str): | ||
return thing | ||
|
||
if isinstance(thing, abc.Collection): | ||
return tuple(map(make_immutable, thing)) | ||
|
||
return thing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters