コード例 #1
0
 def test_getitem(self):
     x = NestedDict({"a": {"b": 1}})
     self.assertEqual(x['a'], {"b": 1})
     self.assertEqual(x['a.b'], 1)
     x = NestedDict({'a': {'b': [1, 2, 3]}})
     self.assertEqual(x['a.b'], [1, 2, 3])
     self.assertRaises(KeyError, x.__getitem__, "w.z")
コード例 #2
0
 def test_leaf_values_nested(self):
     d = NestedDict()
     d[1, 2, 3, 4] = 5
     d[1, 2, 3, 5] = 'hello'
     d[2, (3, 2, 1)] = (1, 2, 3)
     d[2, 4] = 16
     assert (set(d.leaf_values()) == {5, 'hello', (1, 2, 3), 16})
コード例 #3
0
    def test_dict_init_tuples(self):
        d = NestedDict({(1, 2, 3): 4, (5, 6, 7): 8})
        assert (d[(1, 2, 3), ] == 4)
        assert (d.get((1, 2, 3)) == 4)
        with pytest.raises(KeyError, message="Expecting KeyError"):
            i = d[(1, 2, 3)]

        d = NestedDict({
            (1, 2, 3): {
                (4, 3, 2): 1,
                'hello': 'goodbye'
            },
            'a': {
                ('a', 'b', 'c'): 2
            }
        })
        assert (d == {
            (1, 2, 3): {
                (4, 3, 2): 1,
                'hello': 'goodbye'
            },
            'a': {
                ('a', 'b', 'c'): 2
            }
        })
コード例 #4
0
    def test_shallow_get_tuples(self):
        d = NestedDict()
        d[1, 2, 3] = 4
        d[(1, 2, 3), ] = 5

        assert (d.get((1, 2, 3)) == 5)
        assert (d.get(1) == {2: {3: 4}})
        assert (d.get((1, 2, 4), 'arbitrary') == 'arbitrary')
コード例 #5
0
    def test_shallow_get(self):
        d = NestedDict()
        d[1, 2, 3] = 4
        d[(1, 2, 3), ] = 5

        assert (d.get(1) == {2: {3: 4}})
        assert (d.get(2) is None)
        assert (d.get(2, 'arbitrary') == 'arbitrary')
コード例 #6
0
 def test_delitem(self):
     x = NestedDict({"a": {"b": 1}})
     self.assertEqual(x.get('a'), {"b": 1})
     del x['a.b']
     self.assertTrue('a' in x)
     self.assertFalse('a.b' in x)
     x = NestedDict({"a": {"b": 1}})
     del x['a']
     self.assertFalse('a.b' in x)
     self.assertFalse('a' in x)
コード例 #7
0
    def test_set(self):
        d = NestedDict()
        d.set((1, 2, 3), 4)

        assert (d[(1, 2, 3), ] == 4)
        assert (d.get((1, 2, 3)) == 4)

        with pytest.raises(KeyError,
                           message="Unintentional assignment in `set`"):
            item = d[1, 2, 3]
コード例 #8
0
 def test_update(self):
     """ This maintains `dict`s functionality, only merging the
         top-level keys.
     """
     d, e, = NestedDict(), NestedDict()
     d[1, 2, 3] = [1, 2]
     d[2, 'k'] = 16
     e[1, 2, 4] = [3, 4]
     e[3] = 'hello'
     d.update(e)
     assert (d == {1: {2: {4: [3, 4]}}, 2: {'k': 16}, 3: 'hello'})
コード例 #9
0
    def test_nested(self):
        d = {1: {2: {3: {4: 5, 5: 'hello'}}}, 2: {(3, 2, 1): (1, 2, 3), 4: 16}}
        assert(set(paths(d)) == set([(1, 2, 3, 4), (1, 2, 3, 5), (2, (3, 2, 1)), (2, 4)]))

        assert(set(paths(NestedDict(d))) == set([(1, 2, 3, 4), (1, 2, 3, 5), (2, (3, 2, 1)), (2, 4)]))

        d = NestedDict()
        d[1, 2, 3, 4] = 5
        d[1, 2, 3, 5] = 'hello'
        d[2, (3, 2, 1)] = (1, 2, 3)
        d[2, 4] = 16
        assert (set(paths(d)) == set([(1, 2, 3, 4), (1, 2, 3, 5), (2, (3, 2, 1)), (2, 4)]))
コード例 #10
0
    def test_shallow(self):
        d = {'a': 1, 'b': 'hello', 'c': (3, 2, 1), (1, 2, 3): 9}
        assert(set(paths(d)) == set([('a',), ('b',), ('c',), ((1, 2, 3),)]))

        d = NestedDict({'a': 1, 'b': 'hello', 'c': (3, 2, 1), (1, 2, 3): 9})
        assert (set(paths(d)) == set([('a',), ('b',), ('c',), ((1, 2, 3),)]))

        d = NestedDict()
        d[1] = 4
        d['hello'] = (3, 2, 1)
        d[(1, 2, 3), ] = 'hello'
        assert(set(paths(d)) == set([(1,), ('hello',), ((1, 2, 3),)]))
コード例 #11
0
    def test_shallow(self):
        d = {'a': 1, 'b': 'hello', 'c': (3, 2, 1), (1, 2, 3): 9}
        assert(set(leaf_values(d)) == set([1, 'hello', (3, 2, 1), 9]))

        d = NestedDict({'a': 1, 'b': 'hello', 'c': (3, 2, 1), (1, 2, 3): 9})
        assert(set(leaf_values(d)) == set([1, 'hello', (3, 2, 1), 9]))

        d = NestedDict()
        d[1] = 4
        d[2] = (3, 2, 1)
        d[4] = 'hello'
        assert(set(leaf_values(d)) == set([4, (3, 2, 1), 'hello']))
コード例 #12
0
    def test_nested(self):
        d = {1: {2: {3: {4: 5, 5: 'hello'}}}, 2: {(3, 2, 1): (1, 2, 3), 4: 16}}
        assert(set(leaf_values(d)) == set([5, 'hello', (1, 2, 3), 16]))

        assert(set(leaf_values(NestedDict(d))) == set([5, 'hello', (1, 2, 3), 16]))

        d = NestedDict()
        d[1, 2, 3, 4] = 5
        d[1, 2, 3, 5] = 'hello'
        d[2, (3, 2, 1)] = (1, 2, 3)
        d[2, 4] = 16
        assert (set(leaf_values(d)) == set([5, 'hello', (1, 2, 3), 16]))
コード例 #13
0
    def test_nested_update(self):
        d = NestedDict({1: {2: {3: {4: 5, 5: 6}}}, 2: {3: 5, 4: 16}})
        e = {1: {2: {3: {5: 7}}}, 2: {5: 1}}
        d.nested_update(e)
        assert (d == {1: {2: {3: {4: 5, 5: 7}}}, 2: {3: 5, 4: 16, 5: 1}})

        d = NestedDict({1: {2: {3: {4: 5, 5: 6}}}, 2: {3: 5, 4: 16}})
        e = NestedDict({1: {2: {3: {5: 7}}}, 2: {5: 1}})
        d.nested_update(e)
        assert (d == {1: {2: {3: {4: 5, 5: 7}}}, 2: {3: 5, 4: 16, 5: 1}})
コード例 #14
0
 def test_in(self):
     x = NestedDict({"a": {"b": 1}})
     self.assertTrue("a" in x)
     self.assertTrue("a.b" in x)
     self.assertTrue("a" in x)
     self.assertFalse("z" in x)
     self.assertFalse("a.z" in x)
コード例 #15
0
 def test_dict_init(self):
     d = NestedDict({1: {2: {3: {4: {5: 6}}}}})
     assert (d[1, 2, 3, 4, 5] == 6)
     assert (isinstance(d, NestedDict))
     assert (all(
         isinstance(d[i], NestedDict)
         for i in [(1, ), (1, 2), (1, 2, 3), (1, 2, 3, 4)]))
コード例 #16
0
 def test_nesteddict(self):
     x = NestedDict([("a", 1), ("b", 2), ("a.b.c", 3)])
     self.assertTrue("a" in x)
     self.assertTrue("a.b" in x)
     self.assertTrue("a.b.c" in x)
     self.assertEqual(3, x['a.b.c'])
     self.assertNotEqual(1, x['a'])
コード例 #17
0
    def test_getter(self):
        d = NestedDict()
        d['a', 'b', 'c'] = 'hello'
        d['a', 'b', 'd'] = 'goodbye'

        assert (d['a', 'b', 'c'] == 'hello')
        assert (d['a', 'b'] == {'c': 'hello', 'd': 'goodbye'})
コード例 #18
0
 def test_setitem(self):
     x = NestedDict({"a": {"b": 1}})
     x['c'] = 2
     self.assertEqual(x['c'], 2)
     x['a.b'] = 3
     self.assertEqual(x['a.b'], 3)
     x['a.b.c'] = 6
     x['a.b'] = {'c': 6}
コード例 #19
0
    def test_setter(self):
        d = NestedDict()
        d[1, 'a', 34] = [1, 2]
        assert (d == {1: {'a': {34: [1, 2]}}})

        d[1, 'a', 34].extend([4, 3])
        assert (d == {1: {'a': {34: [1, 2, 4, 3]}}})

        d[1, 'a'] = 'hello'
        assert (d == {1: {'a': 'hello'}})
コード例 #20
0
ファイル: cursor.py プロジェクト: jdrumgoole/pymag
    def fieldMapper(doc, fields):
        """
        Take 'doc' and create a new doc using only keys from the 'fields' list.
        Supports referencing fields using dotted notation "a.b.c" so we can parse
        nested fields the way MongoDB does.
        """

        if fields is None or len(fields) == 0:
            return doc

        new_doc = NestedDict()
        old_doc = NestedDict(doc)

        for i in fields:
            if i in old_doc:
                # print( "doc: %s" % doc )
                # print( "i: %s" %i )
                new_doc[i] = old_doc[i]
        return dict(new_doc)
コード例 #21
0
    def test_get_errors(self):
        d = NestedDict()
        d['a', 'b', 'c'] = 23
        with pytest.raises(KeyError, message="Expecting KeyError"):
            val = d['a', 'c']

        with pytest.raises(KeyError, message="Expecting KeyError"):
            val = d['b']

        with pytest.raises(TypeError, message="Expecting TypeError"):
            val = d['a', 'b', 'c', 'd']
コード例 #22
0
    def test_init(self):
        x = NestedDict()
        x['a'] = 1
        self.assertEqual(x['a'], 1)

        x = NestedDict({'a': 1, 'b': 2})
        self.assertEqual(x['a'], 1)
        self.assertEqual(x['b'], 2)

        x = NestedDict([("a", 1), ("b", 2)])
        self.assertEqual(x['a'], 1)
        self.assertEqual(x['b'], 2)

        x = NestedDict({("a", 1), ("b", 2)})
        self.assertEqual(x['a'], 1)
        self.assertEqual(x['b'], 2)

        x = NestedDict({'a.b.c': 1, 'x.y.z': 2})
        self.assertEqual(x['a.b.c'], 1)
        self.assertEqual(x['x.y.z'], 2)

        self.assertRaises(ValueError, NestedDict, {7: 1, 'b': 2})
        self.assertRaises(ValueError, NestedDict, {(7, 1), ('b', 2)})
        self.assertRaises(ValueError, NestedDict, [(7, 1), ('b', 2)])

        x = NestedDict({'a.b.c': 1, 'x.y.z': 2}, m=5, n=6)
        self.assertEqual(x['a.b.c'], 1)
        self.assertEqual(x['x.y.z'], 2)
        self.assertEqual(x['m'], 5)
        self.assertEqual(x['n'], 6)
コード例 #23
0
    def test_update(self):
        x = NestedDict({"a": 1, "b": 2})
        y = NestedDict({"c": 3, "d": 4})
        x.update(y)
        self.assertEqual(len(x), 4)
        self.assertEqual(len(y), 2)
        self.assertEqual(x['a'], 1)
        self.assertEqual(x['b'], 2)
        self.assertEqual(x['c'], 3)
        self.assertEqual(x['d'], 4)

        x = NestedDict({"a": 1, "b": 2})
        y = NestedDict({"c": 3, "d": 4})
        x.update(y, w=10, x=11)
        self.assertEqual(len(x), 6)
        self.assertEqual(len(y), 2)
        self.assertEqual(x['a'], 1)
        self.assertEqual(x['b'], 2)
        self.assertEqual(x['c'], 3)
        self.assertEqual(x['d'], 4)
        self.assertEqual(x['w'], 10)
        self.assertEqual(x['x'], 11)
コード例 #24
0
    def test_nested_update_complex_keys(self):
        d = NestedDict({
            (1, 2, 3): {
                (4, 3, 2): 1,
                'hello': 'goodbye'
            },
            'a': {
                ('a', 'b', 'c'): 2
            }
        })
        e = {1: 2, (1, 2, 3): 4, 'hello': {'good': 'bye', 'bon': 'voyage'}}
        d.nested_update(e)

        assert (d == {
            1: 2,
            (1, 2, 3): 4,
            'hello': {
                'good': 'bye',
                'bon': 'voyage'
            },
            'a': {
                ('a', 'b', 'c'): 2
            }
        })
コード例 #25
0
ファイル: cursor.py プロジェクト: jdrumgoole/pymag
    def dateMapField(doc, field, time_format=None):
        '''
        Given a field that contains a datetime we want it to be output as a string otherwise
        pprint and other functions will abondon ship when they meet BSON time objects
        '''

        if time_format is None:
            time_format = "%d-%b-%Y %H:%M"
        d = NestedDict(doc)
        if field in d:
            value = d[field]
            if isinstance(value, datetime):
                d[field] = value.strftime(time_format)
            else:
                d[field] = datetime.fromtimestamp(value / 1000)

        return dict(d)
コード例 #26
0
    def test_del(self):
        d = NestedDict({
            (1, 2, 3): {
                (4, 3, 2): 1,
                'hello': 'goodbye'
            },
            'a': {
                ('a', 'b', 'c'): 2
            }
        })
        del d[(1, 2, 3), 'hello']
        assert (d == {(1, 2, 3): {(4, 3, 2): 1}, 'a': {('a', 'b', 'c'): 2}})

        del d['a']
        assert (d == {(1, 2, 3): {(4, 3, 2): 1}})

        with pytest.raises(KeyError, message="Expected KeyError"):
            del d[1, 2, 3]
            assert (d == {(1, 2, 3): {(4, 3, 2): 1}})
コード例 #27
0
    def test_nested_keys(self):
        d = NestedDict()
        d[1] = 4
        d['hello'] = (3, 2, 1)
        d[(1, 2, 3), ] = 'hello'
        assert (set(d.paths()) == {(1, ), ('hello', ), ((1, 2, 3), )})

        d = NestedDict()
        d[1, 2, 3, 4] = 5
        d[1, 2, 3, 5] = 'hello'
        d[2, (3, 2, 1)] = (1, 2, 3)
        d[2, 4] = 16
        assert (set(d.paths()) == {(1, 2, 3, 4), (1, 2, 3, 5), (2, (3, 2, 1)),
                                   (2, 4)})
コード例 #28
0
 def test_has_key(self):
     x = NestedDict({"a": {"b": 1}})
     self.assertTrue(x.has_key('a'))
     self.assertTrue(x.has_key('a.b'))
     self.assertFalse(x.has_key('z'))
コード例 #29
0
 def test_get(self):
     x = NestedDict({"a": {"b": 1}})
     self.assertEqual(x.get('a'), {"b": 1})
     self.assertEqual(x.get('z'), None)
     self.assertEqual(x.get('z', 20), 20)
     self.assertEqual(x.get('a.b'), 1)
コード例 #30
0
 def test_valueerror(self):
     x = NestedDict()
     self.assertRaises(ValueError, x.__setitem__, 10, 1)