예제 #1
0
 def test_keypaths(self):
     i = {
         'a': 1,
         'b': {
             'c': {
                 'x': 2,
                 'y': 3,
             },
             'd': {
                 'x': 4,
                 'y': 5,
             },
         },
     }
     o = _keypaths(i)
     r = [
         'a',
         'b',
         'b.c',
         'b.c.x',
         'b.c.y',
         'b.d',
         'b.d.x',
         'b.d.y',
     ]
     self.assertEqual(o, r)
예제 #2
0
 def test_keypaths_with_custom_separator(self):
     i = {
         'a': 1,
         'b': {
             'c': {
                 'x': 2,
                 'y': 3,
             },
             'd': {
                 'x': 4,
                 'y': 5,
             },
         },
     }
     o = _keypaths(i, separator='/')
     r = [
         'a',
         'b',
         'b/c',
         'b/c/x',
         'b/c/y',
         'b/d',
         'b/d/x',
         'b/d/y',
     ]
     self.assertEqual(o, r)
예제 #3
0
 def test_keypaths_without_separator(self):
     i = {
         'a': 1,
         'b': {
             'c': {
                 'x': 2,
                 'y': 3,
             },
             'd': {
                 'x': 4,
                 'y': 5,
             },
         },
     }
     # with self.assertRaises(ValueError):
     #     o = _keypaths(i, separator=None)
     o = _keypaths(i)
     r = [
         'a',
         'b',
         'b.c',
         'b.c.x',
         'b.c.y',
         'b.d',
         'b.d.x',
         'b.d.y',
     ]
     self.assertEqual(o, r)
예제 #4
0
 def test_keypaths_with_nested_lists_and_indexes_included(self):
     i = {
         'a': {
             'b': [
                 [1, 2],
                 [3, 4, 5],
                 [
                     {
                         'x': 1,
                         'y': -1,
                     },
                 ] ,
             ],
         },
     }
     o = _keypaths(i, indexes=True)
     r = [
         'a',
         'a.b',
         'a.b[0]',
         'a.b[0][0]',
         'a.b[0][1]',
         'a.b[1]',
         'a.b[1][0]',
         'a.b[1][1]',
         'a.b[1][2]',
         'a.b[2]',
         'a.b[2][0]',
         'a.b[2][0].x',
         'a.b[2][0].y',
     ]
     self.assertEqual(o, r)
예제 #5
0
 def keypaths(self, indexes=False):
     """
     Return a list of all keypaths in the dict.
     If indexes is True, the output will include list values indexes.
     """
     return _keypaths(self,
                      separator=self._keypath_separator,
                      indexes=indexes)
예제 #6
0
 def test_keypaths_with_invalid_separator(self):
     i = {
         'a': 1,
         'b': {
             'c': {
                 'x': 2,
                 'y': 3,
             },
             'd': {
                 'x': 4,
                 'y': 5,
             },
         },
     }
     with self.assertRaises(ValueError):
         o = _keypaths(i, separator=True)
예제 #7
0
 def test_keypaths_with_lists_and_indexes_not_included(self):
     i = {
         'a': 1,
         'b': {
             'c': {
                 'x': 2,
                 'y': 3,
             },
             'd': {
                 'x': 4,
                 'y': 5,
             },
             'e': [
                 {
                     'x': 1,
                     'y': -1,
                     'z': [1, 2, 3],
                 },
                 {
                     'x': 2,
                     'y': -2,
                     'z': [2, 3, 4],
                 },
                 {
                     'x': 3,
                     'y': -3,
                     'z': [3, 4, 5],
                 },
             ]
         },
     }
     o = _keypaths(i, indexes=False)
     r = [
         'a',
         'b',
         'b.c',
         'b.c.x',
         'b.c.y',
         'b.d',
         'b.d.x',
         'b.d.y',
         'b.e',
     ]
     self.assertEqual(o, r)
예제 #8
0
 def test_keypaths_with_non_string_keys(self):
     i = {
         True: {
             True: 1,
         },
         False: {
             False: 1,
         },
         None: {
             None: 1,
         },
     }
     o = _keypaths(i)
     r = [
         'False',
         'False.False',
         'None',
         'None.None',
         'True',
         'True.True',
     ]
     self.assertEqual(o, r)
예제 #9
0
 def keypaths(self):
     """
     Return a list of all keypaths in the dict.
     """
     sep = self._keypath_separator or '.'
     return _keypaths(self, separator=sep)