예제 #1
0
 def _getitem_by_keys(self, keys):
     parent, key, _ = keylist_util.get_item(self, keys)
     if type_util.is_dict(parent):
         return parent[key]
     elif type_util.is_list(parent):
         return parent[key]
     raise KeyError
예제 #2
0
 def _get_by_keys(self, keys, default=None):
     parent, key, _ = keylist_util.get_item(self, keys)
     if type_util.is_dict(parent):
         return parent.get(key, default)
     elif type_util.is_list(parent):
         return parent[key]
     return default
예제 #3
0
 def _getitem_by_keys(self, keys):
     parent, key, _ = keylist_util.get_item(self, keys)
     if type_util.is_dict(parent):
         return parent[key]
     elif type_util.is_list(parent):
         return parent[key]
     raise KeyError('Invalid keys: "{}"'.format(keys))
예제 #4
0
 def _contains_by_keys(self, keys):
     parent, _, _ = keylist_util.get_item(self, keys)
     if type_util.is_dict(parent):
         return True
     elif type_util.is_list(parent):
         return True
     return False
예제 #5
0
 def _pop_by_keys(self, keys, *args):
     parent, key, _ = keylist_util.get_item(self, keys)
     if type_util.is_dict(parent):
         return parent.pop(key, *args)
     elif type_util.is_list(parent):
         return parent.pop(key)
     if args:
         return args[0]
     raise KeyError
예제 #6
0
    def test_get_item_with_empty_keys(self):
        d = {}

        item = keylist_util.get_item(d, [])
        self.assertEqual(item, (
            None,
            None,
            None,
        ))
예제 #7
0
 def _delitem_by_keys(self, keys):
     parent, key, _ = keylist_util.get_item(self, keys)
     if type_util.is_dict_or_list(parent):
         del parent[key]
         return
     elif type_util.is_tuple(parent):
         # raise the standard TypeError
         del parent[key]
     raise KeyError('Invalid keys: "{}"'.format(keys))
예제 #8
0
 def _pop_by_keys(self, keys, *args):
     parent, key, _ = keylist_util.get_item(self, keys)
     if type_util.is_dict(parent):
         return parent.pop(key, *args)
     elif type_util.is_list(parent):
         return parent.pop(key)
     elif type_util.is_tuple(parent):
         # raise the standard TypeError
         del parent[key]
     if args:
         return args[0]
     raise KeyError('Invalid keys: "{}"'.format(keys))
예제 #9
0
    def test_get_item_with_empty_dict(self):
        d = {}

        item = keylist_util.get_item(d, ['a'])
        self.assertEqual(item, (
            None,
            None,
            None,
        ))

        item = keylist_util.get_item(d, ['a', 'b'])
        self.assertEqual(item, (
            None,
            None,
            None,
        ))

        item = keylist_util.get_item(d, ['a', 0])
        self.assertEqual(item, (
            None,
            None,
            None,
        ))
예제 #10
0
    def test_get_item_with_valid_keys(self):
        d = {
            'a': {
                'b': {
                    'c': [
                        1,
                        2,
                        3,
                    ],
                },
            },
        }

        item = keylist_util.get_item(d, ['a', 'b'])
        self.assertEqual(item, (
            d['a'],
            'b',
            d['a']['b'],
        ))

        item = keylist_util.get_item(d, ['a', 'b', 'c'])
        self.assertEqual(item, (
            d['a']['b'],
            'c',
            [
                1,
                2,
                3,
            ],
        ))

        item = keylist_util.get_item(d, ['a', 'b', 'c', 0])
        self.assertEqual(item, (
            d['a']['b']['c'],
            0,
            1,
        ))