Skip to content

Commit

Permalink
Merge pull request #225 from Rotzbua/feat_yield_from
Browse files Browse the repository at this point in the history
feat: use `yield from` syntax
  • Loading branch information
lopuhin committed Apr 17, 2024
2 parents f4cdd1c + 5947771 commit 7fc70f1
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 11 deletions.
3 changes: 1 addition & 2 deletions extruct/jsonld.py
Expand Up @@ -41,7 +41,6 @@ def _extract_items(self, node):
# sometimes JSON-decoding errors are due to leading HTML or JavaScript comments
data = jstyleson.loads(HTML_OR_JS_COMMENTLINE.sub("", script), strict=False)
if isinstance(data, list):
for item in data:
yield item
yield from data
elif isinstance(data, dict):
yield data
3 changes: 1 addition & 2 deletions extruct/microformat.py
Expand Up @@ -7,5 +7,4 @@ def extract(self, htmlstring, base_url=None, encoding="UTF-8"):
return list(self.extract_items(htmlstring, base_url=base_url))

def extract_items(self, html, base_url=None):
for obj in mf2py.parse(html, html_parser="lxml", url=base_url)["items"]:
yield obj
yield from mf2py.parse(html, html_parser="lxml", url=base_url)["items"]
11 changes: 4 additions & 7 deletions extruct/w3cmicrodata.py
Expand Up @@ -165,10 +165,9 @@ def _extract_item(self, node, items_seen, base_url, itemids):

def _extract_properties(self, node, items_seen, base_url, itemids):
for prop in self._xp_prop(node): # type: ignore[union-attr]
for p, v in self._extract_property(
yield from self._extract_property(
prop, items_seen=items_seen, base_url=base_url, itemids=itemids
):
yield p, v
)

def _extract_property_refs(self, node, refid, items_seen, base_url, itemids):
ref_node = node.xpath("id($refid)[1]", refid=refid)
Expand All @@ -184,16 +183,14 @@ def _extract_property_refs(self, node, refid, items_seen, base_url, itemids):
if "itemprop" in ref_node.keys() and "itemscope" in ref_node.keys():
# An full item will be extracted from the node, no need to look
# for individual properties in child nodes
for p, v in extract_fn(ref_node):
yield p, v
yield from extract_fn(ref_node)
else:
base_parent_scope = ref_node.xpath("ancestor-or-self::*[@itemscope][1]")
for prop in ref_node.xpath("descendant-or-self::*[@itemprop]"):
parent_scope = prop.xpath("ancestor::*[@itemscope][1]")
# Skip properties defined in a different scope than the ref_node
if parent_scope == base_parent_scope:
for p, v in extract_fn(prop):
yield p, v
yield from extract_fn(prop)

def _extract_property(self, node, items_seen, base_url, itemids):
props = node.get("itemprop").split()
Expand Down

0 comments on commit 7fc70f1

Please sign in to comment.