def clean(self, strings=True, collections=True): """ Clean the current dict instance removing all empty values: None, '', {}, [], (). If strings or collections (dict, list, set, tuple) flags are False, related empty values will not be deleted. """ _clean(self, strings=strings, collections=collections)
def test_clean(self): i = { 'a': {}, 'b': { 'x': 1 }, 'c': [], 'd': [0, 1], 'e': 0.0, 'f': '', 'g': None, 'h': '0' } o = i.copy() _clean(o) r = { 'b': { 'x': 1 }, 'd': [0, 1], 'e': 0.0, 'h': '0', } self.assertEqual(o, r) o = i.copy() _clean(o, collections=False) r = { 'a': {}, 'b': { 'x': 1 }, 'c': [], 'd': [0, 1], 'e': 0.0, 'h': '0' } self.assertEqual(o, r) o = i.copy() _clean(o, strings=False) r = { 'b': { 'x': 1 }, 'd': [0, 1], 'e': 0.0, 'f': '', 'h': '0', } self.assertEqual(o, r)