forked from facebookresearch/vissl
-
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.
Extract the AttrDict class to vissl.config (facebookresearch#245)
Summary: This code move allows to remove the implicit circular dependency between `vissl.utils.hydra_config` and `vissl.config`: - `vissl.utils.hydra_config` directly imports `vissl.config` - `vissl.config` used to implicitly depend on hydra_config by using the AttrDict class This small refactoring is a first step toward implementing the feature described in issue facebookresearch#241 and also allows type-hinting the config function. All files have been migrated to the new location of AttrDict, but in case some other files are not under the scope of this PR (internal files), they will continue to work, since hydra_config imports AttrDict. Pull Request resolved: facebookresearch#245 Reviewed By: prigoyal Differential Revision: D27188806 Pulled By: QuentinDuval fbshipit-source-id: 7abcf1028c9f3e028c1e326627d28fcf1550ec1d
- Loading branch information
1 parent
5e62994
commit 2b03a74
Showing
44 changed files
with
131 additions
and
123 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
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,78 @@ | ||
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved | ||
|
||
|
||
class AttrDict(dict): | ||
""" | ||
Dictionary subclass whose entries can be accessed like attributes (as well as normally). | ||
Credits: https://aiida.readthedocs.io/projects/aiida-core/en/latest/_modules/aiida/common/extendeddicts.html#AttributeDict # noqa | ||
""" | ||
|
||
def __init__(self, dictionary): | ||
""" | ||
Recursively turn the `dict` and all its nested dictionaries into `AttrDict` instance. | ||
""" | ||
super().__init__() | ||
|
||
for key, value in dictionary.items(): | ||
if isinstance(value, dict): | ||
self[key] = AttrDict(value) | ||
else: | ||
self[key] = value | ||
|
||
def __getattr__(self, key): | ||
""" | ||
Read a key as an attribute. | ||
:raises AttributeError: if the attribute does not correspond to an existing key. | ||
""" | ||
if key in self: | ||
return self[key] | ||
else: | ||
raise AttributeError( | ||
f"{self.__class__.__name__} object has no attribute {key}." | ||
) | ||
|
||
def __setattr__(self, key, value): | ||
""" | ||
Set a key as an attribute. | ||
""" | ||
self[key] = value | ||
|
||
def __delattr__(self, key): | ||
""" | ||
Delete a key as an attribute. | ||
:raises AttributeError: if the attribute does not correspond to an existing key. | ||
""" | ||
if key in self: | ||
del self[key] | ||
else: | ||
raise AttributeError( | ||
f"{self.__class__.__name__} object has no attribute {key}." | ||
) | ||
|
||
def __getstate__(self): | ||
""" | ||
Needed for pickling this class. | ||
""" | ||
return self.__dict__.copy() | ||
|
||
def __setstate__(self, dictionary): | ||
""" | ||
Needed for pickling this class. | ||
""" | ||
self.__dict__.update(dictionary) | ||
|
||
def __deepcopy__(self, memo=None): | ||
""" | ||
Deep copy. | ||
""" | ||
from copy import deepcopy | ||
|
||
if memo is None: | ||
memo = {} | ||
retval = deepcopy(dict(self)) | ||
return self.__class__(retval) | ||
|
||
def __dir__(self): | ||
return self.keys() |
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
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
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
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
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
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
Oops, something went wrong.