Beispiel #1
0
def test_dictwrapper_05_visitor():
    dct = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', dict(e=4)),
                       ('f', dict(g=dict(h=5)))])
    dct['z.z.z'] = 0
    dw = DictWrapper(dct)

    dw.visit(DictWrapperVisitorDemostrator())
Beispiel #2
0
def test_dictwrapper_08_create():
    dct = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', dict(e=4)),
                       ('f', dict(g=dict(h=5)))])
    dct['z.z.z'] = 0
    dw = DictWrapper(dct, split='.')

    dw._('i.k').l = 3
    assert dw._.i.k.l == 3

    child = dw.child('child')
    assert dw['child'].unwrap() == {}
Beispiel #3
0
    def __setitem__(self, k, v):
        if k in self:
            action='Overwrite'
        else:
            action='Set'

        DictWrapper.__setitem__(self, k, v)

        key = self._path + tuple(self.iterkey(k))
        if self._skip(key):
            return

        key = '.'.join(key)
        print('{action} {key}: {value}'.format(action=action, key=key, value=v))
Beispiel #4
0
def test_dictwrapper_09_dictcopy():
    dct = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', dict(e=4)),
                       ('f', dict(g=dict(h=5)))])
    dct['z'] = {}
    dw = DictWrapper(dct, split='.')

    dw1 = dw.deepcopy()
    for i, (k, v) in enumerate(dw1.walkdicts()):
        print(i, k)
        assert k in dw
        assert v._obj == dw[k]._obj
        assert v._obj is not dw[k]._obj
        assert type(v._obj) is type(dw[k]._obj)
    assert i == 2
Beispiel #5
0
    def init(self):
        update_namespace_cwd(self.opts, 'output')
        storage = self.env.future
        data = DictWrapper({}, split='.')
        for path in self.opts.paths:
            try:
                data[path] = storage[path].unwrap()
            except KeyError:
                raise Exception('Unable to read data path: '+path)

        with open(self.opts.output, 'w') as ofile:
            ofile.write(yaml.dump(data.unwrap()))

        if self.opts.verbose:
            print('Save output file:', self.opts.output)
Beispiel #6
0
    def init(self):
        update_namespace_cwd(self.opts, 'output')
        storage = self.env.future
        data = DictWrapper(OrderedDict(), split='.')
        for path in self.opts.paths:
            try:
                data[path] = storage[path].unwrap()
            except KeyError:
                raise Exception('Unable to read data path: ' + path)

        with open(self.opts.output, 'wb') as ofile:
            pickle.dump(data.unwrap(), ofile, pickle.HIGHEST_PROTOCOL)

        if self.opts.verbose:
            print('Save output file:', self.opts.output)
Beispiel #7
0
    def __init__(self, dct, exclude=[], include=[], **kwargs):
        parent = kwargs.get('parent')
        DictWrapper.__init__(self, dct, **kwargs)
        if parent:
            self._exclude=parent._exclude
            self._include=parent._include

            path = parent._path
            for k, v in parent._obj.items():
                if v is dct:
                    self._path = parent._path + (k,)
                    break
            else:
                self._path=tuple()
        else:
            self._exclude=exclude
            self._include=include
            self._path=tuple()
Beispiel #8
0
def test_dictwrapper_11_iterkey():
    d = dict(a=1, b=2, c=3)
    dw = DictWrapper(d, split='.')

    assert ['a'] == list(dw.iterkey('a'))
    assert ['a', 'b'] == list(dw.iterkey('a.b'))
    assert ['a', 'b'] == list(dw.iterkey(('a', 'b')))
    assert [1] == list(dw.iterkey(1))
    assert [1.0] == list(dw.iterkey(1.0))
Beispiel #9
0
    def load_cfg(self):
        pars = cfg_parse(self.cfg.pars, verbose=True)
        action = self.cfg.get('hooks')
        if isinstance(action, (Mapping, NestedDict)):
            for k, act in action.items():
                v, l = act(pars)
                pars[k] = v
                if l:
                    pars.setdefault('meta', {}).setdefault('labels', {})[k] = l

        pars = DictWrapper(pars, split='.')
        return pars
Beispiel #10
0
def test_dictwrapper_04_visitor():
    dct = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', dict(e=4)),
                       ('f', dict(g=dict(h=5)))])
    dct['z.z.z'] = 0
    dw = DictWrapper(dct)

    keys0 = (('a', ), ('b', ), ('c', ), ('d', 'e'), ('f', 'g', 'h'),
             ('z.z.z', ))
    values0 = (1, 2, 3, 4, 5, 0)

    keys = tuple(dw.walkkeys())
    values = tuple(dw.walkvalues())
    assert keys == keys0
    assert values == values0

    class Visitor(object):
        keys, values = (), ()

        def __call__(self, k, v):
            self.keys += k,
            self.values += v,

    v = Visitor()
    dw.visit(v)
    assert v.keys == keys0
    assert v.values == values0
Beispiel #11
0
    def __init__(self):
        self._bindings = []

        self.globalns = namespace(None, '')
        self.nsview = nsview()
        self.nsview.add([self.globalns])

        self.parameters = parametersview()
        self.pars = self.parameters
        self.parts = envparts()

        from tools.dictwrapper import DictWrapper
        self.future = DictWrapper(OrderedDict(), split='.')
Beispiel #12
0
def test_dictwrapper_09_walk():
    dct = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', dict(e=4)),
                       ('f', dict(g=dict(h=5)))])
    dw = DictWrapper(dct)

    keys0 = [('a', ), ('b', ), ('c', ), ('d', 'e'), ('f', 'g', 'h')]
    keys = [k for k, v in dw.walkitems()]
    assert keys == keys0

    assert [(k, v) for k, v in dw.walkitems('a', appendstartkey=True)
            ] == [(('a', ), 1)]
    assert [(k, v)
            for k, v in dw.walkitems('a', appendstartkey=False)] == [((), 1)]
    assert [(k, v) for k, v in dw.walkitems('d', appendstartkey=True)
            ] == [(('d', 'e'), 4)]
    assert [(k, v) for k, v in dw.walkitems('d', appendstartkey=False)
            ] == [(('e', ), 4)]
    assert [(k, v) for k, v in dw.walkitems(('f', 'g'), appendstartkey=True)
            ] == [(('f', 'g', 'h'), 5)]
    assert [(k, v) for k, v in dw.walkitems(('f', 'g'), appendstartkey=False)
            ] == [(('h', ), 5)]
Beispiel #13
0
def test_dictwrapper_03():
    d = dict(a=1, b=2, c=3)
    dw = DictWrapper(d)

    assert dw.get('a') == 1
    assert dw.get('b') == 2
    assert dw.get('c') == 3
    assert dw.get('d') == None
    assert dw.get('d.e') == None

    assert tuple(dw.keys()) == ('a', 'b', 'c')
Beispiel #14
0
def test_dictwrapper_07_delete():
    dct = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', dict(e=4)),
                       ('f', dict(g=dict(h=5)))])
    dct['z.z.z'] = 0
    dw = DictWrapper(dct)

    assert 'a' in dw
    del dw['a']
    assert 'a' not in dw

    assert ('d', 'e') in dw
    del dw[('d', 'e')]
    assert ('d', 'e') not in dw

    assert ('f', 'g', 'h') in dw
    del dw._.f.g.h
    assert ('f', 'g', 'h') not in dw
    assert ('f', 'g') in dw
Beispiel #15
0
 def build_dict(self):
     self.storage = DictWrapper(OrderedDict(), split='.')
     for path in self.opts.paths:
         ns = self.env.globalns(path)
         for name, par in ns.walknames():
             self.storage[name] = par
Beispiel #16
0
def test_dictwrapper_01():
    dw = DictWrapper({})

    assert not dw
    assert len(dw) == 0
Beispiel #17
0
def test_dictwrapper_03(split):
    dct = dict(a=1, b=2, c=3, d=dict(e=4), f=dict(g=dict(h=5)))
    dct['z.z.z'] = 0
    print(dct)
    dw = DictWrapper(dct, split=split)

    #
    # Test self access
    #
    assert dw.get(()).unwrap() is dct
    assert dw[()].unwrap() is dct

    #
    # Test wrapping
    #
    assert isinstance(dw.get('d'), DictWrapper)
    assert isinstance(dw.get(('f', 'g')), DictWrapper)

    #
    # Test get tuple
    #
    assert dw.get(('d', 'e')) == 4
    assert dw.get(('d', 'e1')) is None
    assert dw.get(('f', 'g', 'h')) == 5
    try:
        dw.get(('z', 'z', 'z'))
        assert False
    except KeyError:
        pass

    #
    # Test getitem tuple
    #
    assert dw[('d', 'e')] == 4
    try:
        dw[('d', 'e1')]
        assert False
    except KeyError:
        pass
    assert dw[('f', 'g', 'h')] == 5

    try:
        dw[('z', 'z', 'z')]
        assert False
    except KeyError:
        pass

    #
    # Test get split
    #
    if split:
        assert dw.get('d.e') == 4
    else:
        assert dw.get('d.e') is None

    if split:
        try:
            dw.get('z.z.z')
            assert False
        except KeyError:
            pass
    else:
        assert dw.get('z.z.z') == 0

    #
    # Test getitem split
    #
    try:
        assert dw['d.e'] == 4
        assert split is not None
    except KeyError:
        pass

    try:
        assert dw['f.g.h'] == 5
        assert dw[('f.g', 'h')] == 5
        assert split is not None
    except KeyError:
        pass

    if split:
        try:
            dw['z.z.z']
            assert False
        except KeyError:
            pass
    else:
        assert dw['z.z.z'] == 0

    #
    # Test contains
    #
    assert 'a' in dw
    assert not 'a1' in dw
    assert 'd' in dw

    #
    # Test contains tuple
    #
    assert ('d', 'e') in dw
    assert not ('k', 'e') in dw
    assert ('f', 'g', 'h') in dw
    assert ('f.g.h' in dw) == bool(split)
    assert ('z.z.z' in dw) == bool(not split)

    #
    # Test parents
    #
    g = dw.get(('f', 'g'))
    assert g.parent().parent() is dw

    #
    # Test children
    #
    m = dw.child(('k', 'l', 'm'))
    assert dw.get(('k', 'l', 'm')).unwrap() is m.unwrap()

    #
    # Test recursive setitem
    #
    dw[('k', 'l', 'm', 'n')] = 5
    try:
        dw.child(tuple('klmn'))
        assert False
    except KeyError:
        pass
    assert dw.get(('k', 'l', 'm', 'n')) == 5

    dw[('o.l.m.n')] = 6
    assert dw['o.l.m.n'] == 6
    if not split:
        assert dw.unwrap()['o.l.m.n'] == 6

    #
    # Test attribute access
    #
    assert dw._.a == 1
    assert dw._.b == 2
    assert dw._.c == 3
    assert dw._.d.e == 4
    assert dw._.f.g.h == 5

    dw._.f.g.h = 6
    assert dw._.f.g.h == 6
    assert dw._._ is dw
Beispiel #18
0
def test_dictwrapper_02():
    dw = DictWrapper(dict(a=1))

    assert dw
    assert len(dw) == 1