from wex.extractor import named from wex.etree import xpath, text extract = named(name = xpath('//h1') | text, country = xpath('//dd[@id="country"]') | text, region = xpath('//dd[@id="region"]') | text)
def test_named_add_as_decorator(): n = named() @n.add def foo(value): return value actual = list(n('bar')) expected = [('foo', 'bar')] assert actual, expected assert foo('bar') == 'bar'
def test_named_extractor_is_generator(): n = named() def foo(value): for character in value: yield character n.add(foo) actual = list(n('bar')) expected = [('foo', 'b'), ('foo', 'a'), ('foo', 'r'),] assert actual == expected assert list(foo('bar')) == list('bar')
def test_named_extractor_raises(): n = named() def foo(value): raise ValueError(value) n.add(foo) actual = list(n('bar')) assert len(actual) == 1 actual_name, actual_value = actual[0] assert actual_name == 'foo' assert isinstance(actual_value, Exception)
def test_named_extractor_is_generator(): n = named() def foo(value): for character in value: yield character n.add(foo) actual = list(n('bar')) expected = [ ('foo', 'b'), ('foo', 'a'), ('foo', 'r'), ] assert actual == expected assert list(foo('bar')) == list('bar')
def test_named_exception_in_generator(): n = named() def foo(value): for i, character in enumerate(value): if i > 0: raise ValueError(character) yield character raise ValueError(value) n.add(foo) actual = list(n('bar')) assert len(actual) == 2 # The first value came out ok... assert actual[0] == ('foo', 'b') actual_name, actual_value = actual[1] assert actual_name == 'foo' # But the second one is an error. assert isinstance(actual_value, Exception)
def test_named_len(): n = named() n.add('foo', lambda v: v) assert len(n) == 1
def test_nameds_keywords(): n = named(foo=lambda v: v) actual = list(n('bar')) expected = [('foo', 'bar')] assert actual == expected
def test_named(): n= named() n.add(lambda v: v, 'foo') actual = list(n('bar')) expected = [('foo', 'bar')] assert actual == expected
from wex.extractor import named from wex.etree import xpath, text extract = named() @extract.add def name(response): return text(xpath('//h1')(response)) @extract.add def whoops(response): return 1/0 @extract.add def country(response): return text(xpath('//dd[@id="country"]')(response)) @extract.add def region(response): return text(xpath('//dd[@id="region"]')(response))
def test_label_named(): # bug test labeller = (lambda x: x) n = named(a1=(lambda x: 'bar')) extract = label(labeller)(n) assert list(extract("foo")) == [("foo", "a1", "bar")]
from wex.extractor import named, labelled from wex.iterable import one from wex.etree import xpath, text cheeses = xpath('//h1[@data-icin]') icin_attr = xpath('@data-icin') | one attrs = named(name = text, country = xpath('following::dd[@id="country"][1]') | text, region = xpath('following::dd[@id="region"][1]') | text) extract_cheese = labelled(icin_attr, attrs) def extract(response): for cheese in cheeses(response): for item in extract_cheese(cheese): yield item
from wex.extractor import named from wex.etree import xpath, text extract = named() @extract.add def name(response): return text(xpath('//h1')(response)) @extract.add def whoops(response): return 1 / 0 @extract.add def country(response): return text(xpath('//dd[@id="country"]')(response)) @extract.add def region(response): return text(xpath('//dd[@id="region"]')(response))
def test_named(): n = named() n.add(lambda v: v, 'foo') actual = list(n('bar')) expected = [('foo', 'bar')] assert actual == expected