From cbdf8612ec8166c3e07c89b668caa56bbc87cd4a Mon Sep 17 00:00:00 2001 From: Ameya Shringi Date: Fri, 6 Mar 2020 16:17:42 -0500 Subject: [PATCH 1/4] Add an example for overriding get_default_config --- python/smqtk/utils/configuration.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/python/smqtk/utils/configuration.py b/python/smqtk/utils/configuration.py index 728ab50e8..324acb710 100644 --- a/python/smqtk/utils/configuration.py +++ b/python/smqtk/utils/configuration.py @@ -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 + the convert the default to a json-compliant stand in. 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): + ... 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'}} + >>> import json + >>> json.dumps(NonCompliantDefault.get_default_config()) + '{"power_func": {"module": "math", "attribute": "pow"}}' :return: Default configuration dictionary for the class. :rtype: dict @@ -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 From b7c8723f0ceef90b5b17018c9c629eea7be14a53 Mon Sep 17 00:00:00 2001 From: Ameya Shringi Date: Fri, 6 Mar 2020 16:19:20 -0500 Subject: [PATCH 2/4] Update release notes --- docs/release_notes/pending_release.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/release_notes/pending_release.rst b/docs/release_notes/pending_release.rst index 96fde6e86..00d3c35d9 100644 --- a/docs/release_notes/pending_release.rst +++ b/docs/release_notes/pending_release.rst @@ -12,3 +12,5 @@ Fixes Documentation * Add missing reference to v0.13.0 change notes. + +* Add an example for overriding get_default_config From cc082425a9e12e16abc085a85959a9145c3a58a6 Mon Sep 17 00:00:00 2001 From: Ameya Shringi Date: Fri, 6 Mar 2020 17:08:37 -0500 Subject: [PATCH 3/4] Add suggested changes --- python/smqtk/utils/configuration.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/python/smqtk/utils/configuration.py b/python/smqtk/utils/configuration.py index 324acb710..4d1a92151 100644 --- a/python/smqtk/utils/configuration.py +++ b/python/smqtk/utils/configuration.py @@ -61,11 +61,12 @@ def get_default_config(cls): 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. If the default arguments are - not json compliant then the function should be overriden to convert - the convert the default to a json-compliant stand in. + not JSON compliant then the function should be overriden to convert + the convert the default to a JSON compliant stand in. It is not be guaranteed that the configuration dictionary returned from this method is valid for construction of an instance of this class. + >>> import math >>> class NonCompliantDefault(Configurable): ... def __init__(self, power_func=math.pow): ... self.power_func=power_func From 8827dc22ef6d1ba3a17e702b0e09703a85542678 Mon Sep 17 00:00:00 2001 From: Ameya Shringi Date: Sun, 3 May 2020 17:01:20 -0400 Subject: [PATCH 4/4] Fix python 2.7 unit test failure --- python/smqtk/utils/configuration.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/smqtk/utils/configuration.py b/python/smqtk/utils/configuration.py index 4d1a92151..a8f6c45e4 100644 --- a/python/smqtk/utils/configuration.py +++ b/python/smqtk/utils/configuration.py @@ -76,11 +76,11 @@ def get_default_config(cls): ... default['power_func'] = {'module': 'math', ... 'attribute': 'pow'} ... return default - >>> NonCompliantDefault.get_default_config() - {'power_func': {'module': 'math', 'attribute': 'pow'}} + >>> NonCompliantDefault.get_default_config() == + ... {'power_func': {'module': 'math', 'attribute': 'pow'}} >>> import json - >>> json.dumps(NonCompliantDefault.get_default_config()) - '{"power_func": {"module": "math", "attribute": "pow"}}' + >>> json.dumps(NonCompliantDefault.get_default_config()) == + ... {"power_func": {"module": "math", "attribute": "pow"}} :return: Default configuration dictionary for the class. :rtype: dict