Improved subset method to accept a single key or args.

This commit is contained in:
Fabio Caccamo 2019-10-04 15:52:14 +02:00
parent 50c0439f9b
commit 9edd2c6bf3
2 changed files with 23 additions and 1 deletions

View File

@ -104,8 +104,11 @@ class benedict(IODict, KeypathDict, ParseDict):
continue
@benediction
def subset(self, keys):
def subset(self, keys, *args):
d = self.__class__()
if isinstance(keys, string_types):
keys = [keys]
keys += args
for key in keys:
d[key] = self.get(key, None)
return d

View File

@ -1025,6 +1025,25 @@ class BenedictTestCase(unittest.TestCase):
self.assertEqual(f, r)
self.assertFalse(f is b)
def test_subset_with_keys_args(self):
d = {
'a': 1,
'b': 2,
'c': '4',
'e': '5',
'f': 6,
'g': 7,
}
b = benedict(d)
f = b.subset('c', 'f', 'x')
r = {
'c': '4',
'f': 6,
'x': None,
}
self.assertEqual(f, r)
self.assertFalse(f is b)
def test_subset_with_keypath(self):
d = {
'x': {