Improved subset method to accept a single key or args.
This commit is contained in:
parent
50c0439f9b
commit
9edd2c6bf3
|
@ -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
|
||||
|
|
|
@ -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': {
|
||||
|
|
Loading…
Reference in New Issue