Skip to content

Commit 7552a8c

Browse files
committed
minor code improvements
1 parent a4ce747 commit 7552a8c

2 files changed

Lines changed: 16 additions & 22 deletions

File tree

lib/pyld/iri_resolver.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,14 @@ def remove_dot_segments_of_path(iri: str, colon_position: int) -> str:
123123

124124

125125
def resolve(relative_iri: str, base_iri: str = None) -> str:
126-
# """
127-
# Resolves a given relative IRI to an absolute IRI.
126+
"""
127+
Resolves a given relative IRI to an absolute IRI.
128128
129-
# :param base_iri: the base IRI.
130-
# :param relative_iri: the relative IRI.
129+
:param base_iri: the base IRI.
130+
:param relative_iri: the relative IRI.
131131
132-
# :return: the absolute IRI.
133-
# """
132+
:return: the absolute IRI.
133+
"""
134134
base_iri = base_iri or ''
135135
base_fragment_pos = base_iri.find("#")
136136

@@ -289,7 +289,7 @@ def unresolve(absolute_iri: str, base_iri: str = ""):
289289
rval = './' + rval
290290

291291
# build relative IRI using urlunparse with empty scheme/netloc
292-
return urlunparse(('', '', rval, '', rel.query or '', rel.fragment or '')) or './'
292+
return urlunparse(('', '', rval, '', rel.query, rel.fragment)) or './'
293293

294294

295295
def parse_authority(parsed_iri: ParseResult) -> str:

lib/pyld/jsonld.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,12 +1309,9 @@ def remove_value(subject, property, value, options=None):
13091309
options = {}
13101310
options.setdefault('propertyIsArray', False)
13111311

1312-
# filter out value
1313-
def filter_value(e):
1314-
return not JsonLdProcessor.compare_values(e, value)
1315-
13161312
values = JsonLdProcessor.get_values(subject, property)
1317-
values = list(filter(filter_value, values))
1313+
# filter out value
1314+
values = [e for e in values if not JsonLdProcessor.compare_values(e, value)]
13181315

13191316
if len(values) == 0:
13201317
JsonLdProcessor.remove_property(subject, property)
@@ -1686,7 +1683,7 @@ def _compact(self, active_ctx, active_property, element, options):
16861683
)
16871684
del compacted_value[compacted_property]
16881685

1689-
if len(compacted_value.keys()) > 0:
1686+
if compacted_value:
16901687
# use keyword alias and add value
16911688
alias = self._compact_iri(active_ctx, expanded_property)
16921689
JsonLdProcessor.add_value(rval, alias, compacted_value)
@@ -1999,7 +1996,7 @@ def _compact(self, active_ctx, active_property, element, options):
19991996
# whose key maps to @id, recompact without @type
20001997
if (
20011998
_is_object(compacted_item)
2002-
and len(compacted_item.keys()) == 1
1999+
and len(compacted_item) == 1
20032000
and '@id' in expanded_item
20042001
):
20052002
compacted_item = self._compact(
@@ -3040,7 +3037,7 @@ def _from_rdf(self, dataset: Dataset, options):
30403037
# 3. Have an array for rdf:rest that has 1 item
30413038
# 4. Have no keys other than: @id, rdf:first, rdf:rest
30423039
# and, optionally, @type where the value is rdf:List.
3043-
node_key_count = len(node.keys())
3040+
node_key_count = len(node)
30443041
while (
30453042
property == str(RDF.rest)
30463043
and _is_object(referenced_once.get(node['@id']))
@@ -3066,7 +3063,7 @@ def _from_rdf(self, dataset: Dataset, options):
30663063
node = usage['node']
30673064
property = usage['property']
30683065
head = usage['value']
3069-
node_key_count = len(node.keys())
3066+
node_key_count = len(node)
30703067

30713068
# if node is not a blank node, then list head found
30723069
if not node['@id'].startswith('_:'):
@@ -4920,11 +4917,7 @@ def _remove_embed(self, state, id_):
49204917
def remove_dependents(id_):
49214918
# get embed keys as a separate array to enable deleting keys
49224919
# in map
4923-
try:
4924-
ids = list(embeds.iterkeys())
4925-
except AttributeError:
4926-
ids = list(embeds.keys())
4927-
for next in ids:
4920+
for next in list(embeds):
49284921
if (
49294922
next in embeds
49304923
and _is_object(embeds[next]['parent'])
@@ -6502,7 +6495,8 @@ def _is_graph(v):
65026495
return (
65036496
_is_object(v)
65046497
and '@graph' in v
6505-
and len([k for k, vv in v.items() if (k != '@id' and k != '@index')]) == 1
6498+
# count non-@id/@index keys
6499+
and set(v) <= {'@graph', '@id', '@index'}
65066500
)
65076501

65086502

0 commit comments

Comments
 (0)