from lxml import etree from pyquery.pyquery import PyQuery class Animator(PyQuery): def listfill(self, selector, data): """Bind list of items to HTML list. In the event of selector yielding more than prototype item, these are cycled over to be cloned and filled. """ proto = self(selector) parent = proto.parent() proto.remove() N = len(proto) for idx, item in enumerate(data): parent.append(proto.eq(idx % N).clone().text(item)) def tablefill(self, selector, subselector, data): """Bind list of tuples to table. Typically selector='tr' and subselector='td'. In the event of selector yielding more than one row, these are cycled over to be cloned and filled.""" proto = self(selector) parent = proto.parent() proto.remove() N = len(proto) for idx, row in enumerate(data): parent.append(proto.eq(idx % N).clone().rowfill(subselector, row)) def rowfill(self, selector, values): """Bind array to selector's children's text nodes. Length of values must be no greater than children available in selector.""" q = self(selector) for idx, value in enumerate(values): q.eq(idx).text(value) return self def test(): template = '''
Lorem | Ipsum |
Lorem | Ipsum |
Error in names
Your name is Lorem Ipsum
John | Smith |
Joe | Bloggs |
Razor | Blade |
Your name is Jimmy Choo