def test_nested_dictionary_with_empty_values(self): tests = [ ({ 'a': {} }, {}), ({ 'a': { 'a': {} } }, {}), ({ 'a': { 'a': [] } }, {}), ({ 'a': { 'a': () } }, {}), ({ 'a': { 'a': '' } }, {}), ({ 'a': { 'a': None } }, {}), ] for data, expected in tests: self.assertEqual(dcompact(data), expected)
def test_flat_dictionary_with_non_empty_values(self): tests = [ ({ 'a': 0 }, { 'a': 0 }), ({ 'a': False }, { 'a': False }), ({ 'a': [0] }, { 'a': [0] }), ({ 'a': (0, ) }, { 'a': (0, ) }), ({ 'a': '0' }, { 'a': '0' }), ({ 'a': 'string' }, { 'a': 'string' }), ] for data, expected in tests: self.assertEqual(dcompact(data), expected)
def to_dict(self): ''' Recursively convert the structure to a plain dictionary. :returns: The structure converted to a dictionary. :rtype: dict ''' convert = lambda v: v.to_dict() if type(v) == Struct else v return dcompact(dict((k, convert(v)) for k, v in self.iteritems()))
def to_dict(self): ''' Recursively convert the structure to a plain dictionary. :returns: The structure converted to a dictionary. :rtype: dict ''' convert = lambda v: v.to_dict() if type(v) == Struct else v return dcompact(dict((k, convert(v)) for k, v in self.iteritems()))
def test_nested_dictionary_with_non_empty_values(self): tests = [ ({'a': {'a': 0}}, {'a': {'a': 0}}), ({'a': {'a': False}}, {'a': {'a': False}}), ({'a': {'a': [0]}}, {'a': {'a': [0]}}), ({'a': {'a': (0,)}}, {'a': {'a': (0,)}}), ({'a': {'a': '0'}}, {'a': {'a': '0'}}), ({'a': {'a': 'string'}}, {'a': {'a': 'string'}}), ] for data, expected in tests: self.assertEqual(dcompact(data), expected)
def test_nested_dictionary_with_empty_values(self): tests = [ ({'a': {}}, {}), ({'a': {'a': {}}}, {}), ({'a': {'a': []}}, {}), ({'a': {'a': ()}}, {}), ({'a': {'a': ''}}, {}), ({'a': {'a': None}}, {}), ] for data, expected in tests: self.assertEqual(dcompact(data), expected)
def test_flat_dictionary_with_non_empty_values(self): ''' ''' tests = [ ({'a': 0}, {'a': 0}), ({'a': False}, {'a': False}), ({'a': [0]}, {'a': [0]}), ({'a': (0,)}, {'a': (0,)}), ({'a': '0'}, {'a': '0'}), ({'a': 'string'}, {'a': 'string'}), ] for data, expected in tests: self.assertEqual(dcompact(data), expected)