Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use yield from syntax #225

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions extruct/jsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading