Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions docs/release_notes/pending_release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ Fixes
Documentation

* Add missing reference to v0.13.0 change notes.

* Add an example for overriding get_default_config
20 changes: 18 additions & 2 deletions python/smqtk/utils/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,26 @@ def get_default_config(cls):
turning those argument names into configuration dictionary keys. If any
of those arguments have defaults, we will add those values into the
configuration dictionary appropriately. The dictionary returned should
only contain JSON compliant value types.
only contain JSON compliant value types. If the default arguments are
not json compliant then the function should be overriden to convert
Comment thread
Purg marked this conversation as resolved.
Outdated
the convert the default to a json-compliant stand in.
Comment thread
Purg marked this conversation as resolved.
Outdated

It is not be guaranteed that the configuration dictionary returned
from this method is valid for construction of an instance of this class.
>>> class NonCompliantDefault(Configurable):
Comment thread
Purg marked this conversation as resolved.
... def __init__(self, power_func=math.pow):
... self.power_func=power_func
... @classmethod
... def get_default_config(cls):
... default = super(NonCompliantDefault, cls).get_default_config()
... default['power_func'] = {'module': 'math',
... 'attribute': 'pow'}
... return default
>>> NonCompliantDefault.get_default_config()
{'power_func': {'module': 'math', 'attribute': 'pow'}}
Comment thread
Purg marked this conversation as resolved.
Outdated
>>> import json
>>> json.dumps(NonCompliantDefault.get_default_config())
'{"power_func": {"module": "math", "attribute": "pow"}}'

:return: Default configuration dictionary for the class.
:rtype: dict
Expand Down Expand Up @@ -106,7 +122,7 @@ def from_config(cls, config_dict, merge_default=True):

This base method is adequate without modification when a class's
constructor argument types are JSON-compliant. If one or more are not,
however, this method then needs to be overridden in order to convert
this method then needs to be overridden in order to convert
from a JSON-compliant stand-in into the more complex object the
constructor requires. It is recommended that when complex types *are*
used they also inherit from the :class:`Configurable` in order to
Expand Down