class Bookshelf(XMLDoc): class Iterable(XMLDoc): __name__ = "Book" __query__ = "/bookshelf/book" class Iterable(XMLDoc): __query__ = "chapter" id = QueryAttr("@id") text = QueryAttr("text()") isbn = QueryAttr("isbn/text()") title = QueryAttr("title/text()") author = QueryAttr("author/text()") chapters = QueryAttr("chapter[@id]/text()") @QueryAttr("author/text()") def author_first_name(self, value): return value.split(", ")[1] @QueryAttr("author/text()") def author_last_name(self, value): return value.split(", ")[0] name = QueryAttr("/bookshelf/name/text()") undefined = QueryAttr("/bookshelf/undefined/text()")
class Bookshelf(TextDoc): class Iterable(TextDoc): __query__ = "<book>(.*?)</book>" class Iterable(TextDoc): __query__ = "<chapter[^>]*>.*?</chapter>" id = QueryAttr('<chapter[^>]+id="([^\"]*)"') text = QueryAttr(">(.*)</chapter>") isbn = QueryAttr("<isbn>(.*?)</isbn>") title = QueryAttr("<title>(.*?)</title>") author = QueryAttr("<author>(.*?)</author>") revisions = QueryAttr("<rev>(\d+)</rev>") name_tuple = QueryAttr("<author>([^,]+),\s*(.+?)</author>") name_dict = QueryAttr( "<author>(?P<lastname>[^,]+),\s*(?P<firstname>.+?)</author>") @QueryAttr("<author>(.*?)</author>") def author_first_name(self, value): return value.split(", ")[1] @QueryAttr("<author>(.*?)</author>") def author_last_name(self, value): return value.split(", ")[0] name = QueryAttr("<name>(.*?)</name>") undefined = QueryAttr("<undefined>(.*?)</undefined>")
class Iterable(YAMLDoc): __query__ = "$.books" isbn = QueryAttr("$.isbn") title = QueryAttr("$.title") author = QueryAttr("$.author") @QueryAttr("$.author") def author_first_name(self, value): return value.split(", ")[1] @QueryAttr("$.author") def author_last_name(self, value): return value.split(", ")[0]
def test_should_return_return_of_decorated_function(self): def foo(self, value): return value.upper() self.Data.foo = QueryAttr("bar")(foo) self.assertEqual(self.Data().foo, "FOOBAR")
class Iterable(TextDoc): __query__ = "<chapter[^>]*>.*?</chapter>" id = QueryAttr('<chapter[^>]+id="([^\"]*)"') text = QueryAttr(">(.*)</chapter>")
class MyDoc(CsvRow): @QueryAttr("Foo") def foo(self, value): return int(value) bar = QueryAttr("Bar", factory=float)
class MyDoc(CsvRow): foo = QueryAttr(0) bar = QueryAttr(1)
class Iterable(XMLDoc): __query__ = "chapter" id = QueryAttr("@id") text = QueryAttr("text()")
def test_should_return_query_of_obj(self): self.Data.foo = QueryAttr("bar") self.assertEqual(self.Data().foo, "foobar")