Exemple #1
0
 def test_groupby(self):
     l = [
         {'id':1, 'name':'John'},
         {'id':2, 'name':'Frank'},
         {'id':3, 'name':'Tony'},
         {'id':4, 'name':'Jimmy'},
         {'id':3, 'name':'Sam'},
         {'id':1, 'name':'Charles'},
         {'id':3, 'name':'Bob'},
         {'id':4, 'name':'Paul'},
         {'id':1, 'name':'Michael'},
     ]
     l_clone = _clone(l)
     d = _groupby(l, 'id')
     self.assertEqual(l, l_clone)
     self.assertTrue(isinstance(d, dict))
     self.assertEqual(len(d), 4)
     self.assertTrue(all([
         1 in d, 2 in d, 3 in d, 4 in d, ]))
     self.assertTrue(all([
         isinstance(d[1], list), isinstance(d[2], list),
         isinstance(d[3], list), isinstance(d[4], list), ]))
     self.assertEqual(len(d[1]), 3)
     self.assertEqual(len(d[2]), 1)
     self.assertEqual(len(d[3]), 3)
     self.assertEqual(len(d[4]), 2)
 def clone(self):
     """
     Creates and return a clone of the current dict instance (deep copy).
     """
     return benedict(
         _clone(self),
         keypath_separator=self._keypath_separator)
 def test_clone(self):
     i = {'a': {'b': {'c': 1}}}
     o = _clone(i)
     self.assertEqual(type(i), type(o))
     self.assertEqual(i, o)
     self.assertFalse(i is o)
     o['a']['b']['c'] = 2
     self.assertEqual(i['a']['b']['c'], 1)
     self.assertEqual(o['a']['b']['c'], 2)
Exemple #4
0
 def test_traverse(self):
     i = {
         'a': {
             'x': 2,
             'y': 3,
             'z': {
                 'ok': 5,
             }
         },
         'b': {
             'x': 7,
             'y': 11,
             'z': {
                 'ok': 13,
             }
         },
         'c': {
             'x': 17,
             'y': 19,
             'z': {
                 'ok': 23,
             }
         },
     }
     o = _clone(i)
     with self.assertRaises(ValueError):
         _traverse(o, True)
     def f(parent, key, value):
         if not isinstance(value, dict):
             parent[key] = (value + 1)
     _traverse(o, f)
     r = {
         'a': {
             'x': 3,
             'y': 4,
             'z': {
                 'ok': 6,
             }
         },
         'b': {
             'x': 8,
             'y': 12,
             'z': {
                 'ok': 14,
             }
         },
         'c': {
             'x': 18,
             'y': 20,
             'z': {
                 'ok': 24,
             }
         },
     }
     self.assertEqual(o, r)
 def test_clone_empty(self):
     i = {'a': {'b': {'c': 1}}}
     o = _clone(i, empty=True)
     self.assertEqual(type(i), type(o))
     self.assertEqual(o, {})
Exemple #6
0
 def clone(self):
     """
     Creates and return a clone of the current dict instance (deep copy).
     """
     return self._cast(_clone(self))
Exemple #7
0
 def __deepcopy__(self, memo):
     obj = benedict(keypath_separator=self._keypath_separator)
     for key, value in self.items():
         obj[key] = _clone(value, memo=memo)
     return obj
Exemple #8
0
 def test_nest(self):
     l = [
         {
             'id': 1,
             'parent_id': None,
             'name': 'John'
         },
         {
             'id': 2,
             'parent_id': 1,
             'name': 'Frank'
         },
         {
             'id': 3,
             'parent_id': 2,
             'name': 'Tony'
         },
         {
             'id': 4,
             'parent_id': 3,
             'name': 'Jimmy'
         },
         {
             'id': 5,
             'parent_id': 1,
             'name': 'Sam'
         },
         {
             'id': 6,
             'parent_id': 3,
             'name': 'Charles'
         },
         {
             'id': 7,
             'parent_id': 2,
             'name': 'Bob'
         },
         {
             'id': 8,
             'parent_id': 3,
             'name': 'Paul'
         },
         {
             'id': 9,
             'parent_id': None,
             'name': 'Michael'
         },
     ]
     l_clone = _clone(l)
     n = _nest(l, 'id', 'parent_id', 'children')
     r = [
         {
             'id':
             1,
             'parent_id':
             None,
             'name':
             'John',
             'children': [
                 {
                     'id':
                     2,
                     'parent_id':
                     1,
                     'name':
                     'Frank',
                     'children': [
                         {
                             'id':
                             3,
                             'parent_id':
                             2,
                             'name':
                             'Tony',
                             'children': [
                                 {
                                     'id': 4,
                                     'parent_id': 3,
                                     'name': 'Jimmy',
                                     'children': [],
                                 },
                                 {
                                     'id': 6,
                                     'parent_id': 3,
                                     'name': 'Charles',
                                     'children': [],
                                 },
                                 {
                                     'id': 8,
                                     'parent_id': 3,
                                     'name': 'Paul',
                                     'children': [],
                                 },
                             ],
                         },
                         {
                             'id': 7,
                             'parent_id': 2,
                             'name': 'Bob',
                             'children': [],
                         },
                     ],
                 },
                 {
                     'id': 5,
                     'parent_id': 1,
                     'name': 'Sam',
                     'children': [],
                 },
             ]
         },
         {
             'id': 9,
             'parent_id': None,
             'name': 'Michael',
             'children': [],
         },
     ]
     self.assertEqual(l, l_clone)
     self.assertEqual(n, r)