def test_extends_dict_accesses(): """dotdict.DotDict should have the dict type as a base class. dotdict.DotDict should be able to access keys via attrtibutes. dotdict.DotDict should be able to access nested keys via chained attrs. """ dct = dotdict.DotDict() assert isinstance(dct, (dict,)), \ "dotdict.DotDict is not based off a dict" dct2 = dotdict.DotDict(key='value') assert getattr(dct2, 'key', None) == 'value', \ "dotdict.DotDict cannot access keys via attrs"
def test_access_raises(): """dotdict.DotDict should raise an AttributeError when a key does not exist. """ try: dct = dotdict.DotDict() _ = dct.bad_key raise AssertionError( "dotdict.DotDict should raise Exceptions on bad keys") except AttributeError: pass except KeyError as ex: raise AssertionError( "dotdictord.DotDict should raise AttributeError not KeyError" ) from ex dct_nested = dotdict.DotDict({'a': {'b': 'value'}}) assert dct_nested.a.b == 'value', \ "dotdict.DotDict cannot access nested keys via chained attrs"
def test_extends_dict_mutations(): """dotdict.DotDict should be able to set keys via attrtibutes. dotdict.DotDict should be able to set nested keys via chained attrs. """ dct = dotdict.DotDict(key='value') dct.key = 'other' assert getattr(dct, 'key', None) == 'other', \ "dotdict.DotDict cannot update keys via attrs" dct_nested = dotdict.DotDict({'a': {'b': 'value'}}) dct_nested.a.b = 'other' assert dct_nested.a.b == 'other', \ "dotdict.DotDict cannot update nested keys via chained attrs" dct_nested2 = dotdict.DotDict() dct_nested2.should_convert = {'a': {'b': 5}} # pylint: disable=no-member assert dct_nested2.should_convert.a.b == 5, \ "dotdict.DotDict cannot access keys as attrs after updating"
def __getattr__(self, name): if self._wrapped is None: self._setup() value = getattr(self._wrapped, name, None) # if value is None: return getattr(self._conf_service, name) if type(value) is dict: value = dotdict.DotDict(value) setattr(self._wrapped, name, value) return value
def test_dotdict_build_from_object(): """dotdict.DotDict should provide convenience methods for creation. """ testobj = ExampleObject() testdct = dotdict.DotDict(a_val=7, b_val=8, c_val=9, dct={ "list": [1], "set": {2}, "tuple": (3, ) }) assert 'build_from_object' in vars(dotdict.DotDict), \ "dotdict.DotDict does not define a from_object static method" assert dotdict.DotDict.build_from_object(testobj) == testdct, \ "dotdict.DotDict.from_object should retain data values"
def test_update_raises(): """dotdict.DotDict should raise AttribbuteErrors when deleting bad keys. """ dct_nested = dotdict.DotDict({'should_convert': {'a': 43}}) try: del dct_nested.should_convert.a except (AttributeError, KeyError) as ex: raise AssertionError( "dotdict.DotDict should delete attribute keys") from ex try: del dct_nested.does_not_exist raise AssertionError( "dotdict.DotDict should throw on deleting non existing keys") except AttributeError: pass except KeyError as ex: raise AssertionError( "dotdictord.DotDict should raise AttributeError not KeyError" ) from ex
'check {:s} and {:s}'.format(key, current_key) for i in range(num_values): for key in current_dictionary: current_hparams[key] = current_dictionary[key]['values'][i] for item in search_hyperparams(dictionary, current_hparams): yield item for key in current_dictionary: dictionary[key] = copy.deepcopy(current_dictionary[key]) if __name__ == '__main__': meta_configs_free = dotdict.DotDict({ 'n': { 'values': [3, 4, 5], 'flag': None }, 'plus_one': { 'values': [True, False], 'flag': None } }) config_list_free = list(search_hyperparams(meta_configs_free)) from IPython import embed embed(using=False) meta_configs_bind = dotdict.DotDict({ 'n': { 'values': [3, 4, 5], 'flag': 'group1' }, 'plus_one': { 'values': [True, False, True],
import copy import dotdict import sys from factorial import F from hyperparam import search_hyperparams if __name__ == '__main__': idx = int(sys.argv[1]) meta_configs = dotdict.DotDict( { 'n': { 'values': [3, 4, 5], 'flag': None }, 'plus_one': { 'values': [True, False], 'flag': None } } ) config_list = list(search_hyperparams(meta_configs)) configs = config_list[idx] print(F(**configs))