forked from spotify/luigi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* move params freezing to separated module * freeze dict for toml parser * FIX FrozenOrderedDict name * Freeze only params of config, not all config * +docstring
- Loading branch information
Showing
4 changed files
with
80 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
"""Internal-only module with immutable data structures. | ||
Please, do not use it outside of Luigi codebase itself. | ||
""" | ||
|
||
|
||
from collections import OrderedDict, Mapping | ||
import operator | ||
import functools | ||
|
||
|
||
class FrozenOrderedDict(Mapping): | ||
""" | ||
It is an immutable wrapper around ordered dictionaries that implements the complete :py:class:`collections.Mapping` | ||
interface. It can be used as a drop-in replacement for dictionaries where immutability and ordering are desired. | ||
""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
self.__dict = OrderedDict(*args, **kwargs) | ||
self.__hash = None | ||
|
||
def __getitem__(self, key): | ||
return self.__dict[key] | ||
|
||
def __iter__(self): | ||
return iter(self.__dict) | ||
|
||
def __len__(self): | ||
return len(self.__dict) | ||
|
||
def __repr__(self): | ||
# We should use short representation for beautiful console output | ||
return repr(dict(self.__dict)) | ||
|
||
def __hash__(self): | ||
if self.__hash is None: | ||
hashes = map(hash, self.items()) | ||
self.__hash = functools.reduce(operator.xor, hashes, 0) | ||
|
||
return self.__hash | ||
|
||
def get_wrapped(self): | ||
return self.__dict | ||
|
||
|
||
def recursively_freeze(value): | ||
""" | ||
Recursively walks ``Mapping``s and ``list``s and converts them to ``FrozenOrderedDict`` and ``tuples``, respectively. | ||
""" | ||
if isinstance(value, Mapping): | ||
return FrozenOrderedDict(((k, recursively_freeze(v)) for k, v in value.items())) | ||
elif isinstance(value, list) or isinstance(value, tuple): | ||
return tuple(recursively_freeze(v) for v in value) | ||
return value |
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