Skip to content

Commit

Permalink
v0.5.1 - protecting params() -> __params()
Browse files Browse the repository at this point in the history
  • Loading branch information
kpe committed Mar 27, 2019
1 parent 51a3768 commit 04cb2c4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion params/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

from .params import Params

__version__ = '0.5.0'
__version__ = '0.5.1'
20 changes: 10 additions & 10 deletions params/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,43 +29,43 @@ class MyParams(Params):

def __init__(self, *args, **kwargs):
super(Params, self).__init__()
self.update(self._defaults())
self.update(self.__defaults())
self.update(dict(*args))
self.update(kwargs)

def __getattribute__(self, attr):
if not attr.startswith("_") and attr in self._defaults():
if not attr.startswith("_") and attr in self.__defaults():
return self.__getitem__(attr)
return object.__getattribute__(self, attr)

def __setattr__(self, key, value):
self.__setitem__(key, value)

def __setitem__(self, key, value):
if key not in self._defaults():
if key not in self.__defaults():
raise AttributeError("Setting unexpected parameter '{}' "
"in Params instance '{}'".format(key, self.__class__.__name__))
super(Params, self).__setitem__(key, value)

@classmethod
def _defaults(cls):
def __defaults(cls):
""" Aggregate all class fields in the class hierarchy to a dict. """

if '__defaults' in cls.__dict__:
return cls.__defaults
if '_defaults' in cls.__dict__:
return cls._defaults

result = {}
for base in cls.__bases__:
if issubclass(base, Params):
result.update(base._defaults())
result.update(base.__defaults())

for attr, value in cls.__dict__.items():
if attr.startswith("_") or callable(getattr(cls, attr)):
continue
result[attr] = value

cls.__defaults = result
return cls.__defaults
cls._defaults = result
return cls._defaults

@classmethod
def from_dict(cls, args, return_instance=True, return_unused=True):
Expand All @@ -87,7 +87,7 @@ def is_not_none(x):
cls_args, unused_args = {}, {}
if args:
# extract unused args
keys = cls._defaults().keys()
keys = cls.__defaults().keys()
cls_args, unused_args = zip(*list(map(lambda p: (p, None) if p[0] in keys else (None, p),
args.items())))

Expand Down

0 comments on commit 04cb2c4

Please sign in to comment.