def process(self, baton): xml = util.dict_get_path(baton, self.xml_path) e = etree.XML(xml) for xpath, output_path in self.xpaths.items(): result = e.xpath(xpath) util.dict_setdefault_path(baton, output_path, []).extend(result) return baton
def test_setdefault_through_attributes(self): class Bar(object): def __init__(self, wrapped): self.wrapped = wrapped d = dict(a=Bar(dict(b='c'))) self.assertEquals(util.dict_setdefault_path(d, 'a.wrapped.b', 'd'), 'c') self.assertEquals(util.dict_get_path(d, 'a.wrapped.b'), 'c') self.assertEquals(util.dict_setdefault_path(d, 'a.wrapped.c', 'd'), 'd') self.assertEquals(util.dict_get_path(d, 'a.wrapped.c'), 'd')
def setdefault(self, path, value): """ Sets the configuration key specified by *path* to *value*, unless a value is already defined for that path. Returns the value of *path* --- i.e. either *value* or the already existing value. .. :seealso: :: :ref:`~piped.util.dict_setdefault_path` for more on the behaviour of setting with key paths. """ return util.dict_setdefault_path(self._config, path, value)
def test_setdefault(self): d = dict(a=dict(b='c')) self.assertEquals(util.dict_setdefault_path(d, 'a.b', 'd'), 'c') self.assertEquals(d, dict(a=dict(b='c'))) self.assertEquals(util.dict_setdefault_path(d, 'a.c', 'd'), 'd') self.assertEquals(d, dict(a=dict(b='c', c='d')))