Пример #1
0
 def test_getitem(self):
     d = OrderedDictWithDefaults()
     d['name'] = 'Joseph'
     d.defaults_ = {
         'surname': 'Wyndham'
     }
     self.assertEqual('Joseph', d['name'])
     self.assertEqual('Wyndham', d['surname'])
Пример #2
0
 def test_contains(self):
     d = OrderedDictWithDefaults()
     self.assertEqual([], list(d.items()))
     d['key'] = 'Birds'
     d.defaults_ = {'value': '10'}
     self.assertTrue('key' in d)
     self.assertTrue('value' in d)
     self.assertFalse('test' in d)
Пример #3
0
 def test_getitem(self):
     d = OrderedDictWithDefaults()
     d['name'] = 'Joseph'
     d.defaults_ = {
         'surname': 'Wyndham'
     }
     self.assertEqual('Joseph', d['name'])
     self.assertEqual('Wyndham', d['surname'])
Пример #4
0
 def test_keys(self):
     d = OrderedDictWithDefaults()
     d['name'] = 'Andrew'
     d['surname'] = 'Gray'
     d.defaults_ = {'address': 'N/A'}
     keys = list(d.keys())
     self.assertTrue(len(keys) == 3)
     self.assertTrue('name' in keys)
     self.assertTrue('surname' in keys)
     self.assertTrue('address' in keys)
Пример #5
0
 def test_contains(self):
     d = OrderedDictWithDefaults()
     self.assertEqual([], list(d.items()))
     d['key'] = 'Birds'
     d.defaults_ = {
         'value': '10'
     }
     self.assertTrue('key' in d)
     self.assertTrue('value' in d)
     self.assertFalse('test' in d)
Пример #6
0
 def test_itervalues(self):
     d = OrderedDictWithDefaults()
     self.assertEqual([], list(d.items()))
     d['key'] = 'Birds'
     d['len'] = '89'
     d.defaults_ = {'surname': 'Wyndham'}
     count = 0
     for k in d.values():
         self.assertTrue(k in ['Birds', '89', 'Wyndham'])
         count += 1
     self.assertEqual(3, count)
Пример #7
0
 def test_keys(self):
     d = OrderedDictWithDefaults()
     d['name'] = 'Andrew'
     d['surname'] = 'Gray'
     d.defaults_ = {
         'address': 'N/A'
     }
     keys = list(d.keys())
     self.assertTrue(len(keys) == 3)
     self.assertTrue('name' in keys)
     self.assertTrue('surname' in keys)
     self.assertTrue('address' in keys)
Пример #8
0
def replicate(target, source):
    """Replicate source *into* target.

    Source elements need not exist in target already, so source overrides
    common elements in target and otherwise adds elements to it.
    """
    if not source:
        target = OrderedDictWithDefaults()
        return
    if hasattr(source, "defaults_"):
        target.defaults_ = pdeepcopy(source.defaults_)
    for key, val in source.items():
        if isinstance(val, dict):
            if key not in target:
                target[key] = OrderedDictWithDefaults()
            if hasattr(val, 'defaults_'):
                target[key].defaults_ = pdeepcopy(val.defaults_)
            replicate(target[key], val)
        elif isinstance(val, list):
            target[key] = val[:]
        else:
            target[key] = val
Пример #9
0
def replicate( target, source ):
    """
    Replicate source *into* target. Source elements need not exist in
    target already, so source overrides common elements in target and
    otherwise adds elements to it.
    """
    if not source:
        target = OrderedDictWithDefaults()
        return
    if hasattr(source, "defaults_"):
        target.defaults_ = pdeepcopy(source.defaults_)
    for key,val in source.items():
        if isinstance( val, dict ):
            if key not in target:
                target[key] = OrderedDictWithDefaults()
            if hasattr(val, 'defaults_'):
                target[key].defaults_ = pdeepcopy(val.defaults_)
            replicate( target[key], val )
        elif isinstance( val, list ):
            target[key] = val[:]
        else:
            target[key] = val
Пример #10
0
 def test_itervalues(self):
     d = OrderedDictWithDefaults()
     self.assertEqual([], list(d.items()))
     d['key'] = 'Birds'
     d['len'] = '89'
     d.defaults_ = {
         'surname': 'Wyndham'
     }
     count = 0
     for k in d.values():
         self.assertTrue(k in ['Birds', '89', 'Wyndham'])
         count += 1
     self.assertEqual(3, count)