Skip to content

Commit af5474f

Browse files
committed
Add nquads to format target and apply
1 parent a54247a commit af5474f

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ upgrade-submodules:
1010
git submodule update --remote --init --recursive
1111

1212
# TODO: Expand to lib/ and tests/ as linting issues are resolved.
13-
RUFF_TARGET = lib/pyld/context_resolver.py lib/pyld/identifier_issuer.py lib/pyld/iri_resolver.py
13+
RUFF_TARGET = lib/pyld/context_resolver.py lib/pyld/identifier_issuer.py lib/pyld/iri_resolver.py lib/pyld/nquads.py
1414

1515
lint:
1616
ruff check $(RUFF_TARGET)

lib/pyld/nquads.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
21
import re
32

43
XSD_STRING = 'http://www.w3.org/2001/XMLSchema#string'
54
RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
65
RDF_LANGSTRING = RDF + 'langString'
76

7+
88
def escape(value: str):
99
return (
1010
value.replace("\\", "\\\\")
@@ -76,7 +76,10 @@ def parse_nquads(input_: str):
7676
# parse quad
7777
match = re.search(quad, line)
7878
if match is None:
79-
raise ParserError(f'Error while parsing N-Quads invalid quad {line} at line {line_number}.', line_number=line_number)
79+
raise ParserError(
80+
f'Error while parsing N-Quads invalid quad {line} at line {line_number}.',
81+
line_number=line_number,
82+
)
8083
match = match.groups()
8184

8285
# create RDF triple
@@ -131,6 +134,7 @@ def parse_nquads(input_: str):
131134

132135
return dataset
133136

137+
134138
def serialize_nquads(dataset):
135139
"""
136140
Converts an RDF dataset to N-Quads.
@@ -148,6 +152,7 @@ def serialize_nquads(dataset):
148152
quads.sort()
149153
return ''.join(quads)
150154

155+
151156
def serialize_nquad(triple, graph_name=None):
152157
"""
153158
Converts an RDF triple and graph name to an N-Quad string (a single
@@ -186,7 +191,7 @@ def serialize_nquad(triple, graph_name=None):
186191
# object is IRI, bnode, or literal
187192
if o['type'] == 'IRI':
188193
quad += '<' + o['value'] + '>'
189-
elif(o['type'] == 'blank node'):
194+
elif o['type'] == 'blank node':
190195
quad += o['value']
191196
else:
192197
escaped = escape(o['value'])
@@ -206,7 +211,7 @@ def serialize_nquad(triple, graph_name=None):
206211

207212
quad += ' .\n'
208213
return quad
209-
214+
210215

211216
def _compare_rdf_triples(t1, t2):
212217
"""
@@ -218,16 +223,16 @@ def _compare_rdf_triples(t1, t2):
218223
:return: True if the triples are the same, False if not.
219224
"""
220225
for attr in ['subject', 'predicate', 'object']:
221-
if(t1[attr]['type'] != t2[attr]['type'] or
222-
t1[attr]['value'] != t2[attr]['value']):
226+
if (
227+
t1[attr]['type'] != t2[attr]['type']
228+
or t1[attr]['value'] != t2[attr]['value']
229+
):
223230
return False
224231

225232
if t1['object'].get('language') != t2['object'].get('language'):
226233
return False
227-
if t1['object'].get('datatype') != t2['object'].get('datatype'):
228-
return False
234+
return t1['object'].get('datatype') == t2['object'].get('datatype')
229235

230-
return True
231236

232237
class ParserError(ValueError):
233238
"""

0 commit comments

Comments
 (0)