Skip to content

Commit 0c277e9

Browse files
authored
Allow multi subjects & objects in graph funcs (#3086)
* subjects() * objects() & tests * blacked
1 parent 4cf2180 commit 0c277e9

File tree

2 files changed

+57
-30
lines changed

2 files changed

+57
-30
lines changed

rdflib/graph.py

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -847,26 +847,32 @@ def set(
847847
def subjects(
848848
self,
849849
predicate: Union[None, Path, _PredicateType] = None,
850-
object: Optional[_ObjectType] = None,
850+
object: Optional[Union[_ObjectType, List[_ObjectType]]] = None,
851851
unique: bool = False,
852852
) -> Generator[_SubjectType, None, None]:
853853
"""A generator of (optionally unique) subjects with the given
854-
predicate and object"""
855-
if not unique:
856-
for s, p, o in self.triples((None, predicate, object)):
857-
yield s
854+
predicate and object(s)"""
855+
# if the object is a list of Nodes, yield results from subject() call for each
856+
if isinstance(object, list):
857+
for obj in object:
858+
for s in self.subjects(predicate, obj, unique):
859+
yield s
858860
else:
859-
subs = set()
860-
for s, p, o in self.triples((None, predicate, object)):
861-
if s not in subs:
861+
if not unique:
862+
for s, p, o in self.triples((None, predicate, object)):
862863
yield s
863-
try:
864-
subs.add(s)
865-
except MemoryError as e:
866-
logger.error(
867-
f"{e}. Consider not setting parameter 'unique' to True"
868-
)
869-
raise
864+
else:
865+
subs = set()
866+
for s, p, o in self.triples((None, predicate, object)):
867+
if s not in subs:
868+
yield s
869+
try:
870+
subs.add(s)
871+
except MemoryError as e:
872+
logger.error(
873+
f"{e}. Consider not setting parameter 'unique' to True"
874+
)
875+
raise
870876

871877
def predicates(
872878
self,
@@ -894,27 +900,32 @@ def predicates(
894900

895901
def objects(
896902
self,
897-
subject: Optional[_SubjectType] = None,
903+
subject: Optional[Union[_SubjectType, List[_SubjectType]]] = None,
898904
predicate: Union[None, Path, _PredicateType] = None,
899905
unique: bool = False,
900906
) -> Generator[_ObjectType, None, None]:
901907
"""A generator of (optionally unique) objects with the given
902-
subject and predicate"""
903-
if not unique:
904-
for s, p, o in self.triples((subject, predicate, None)):
905-
yield o
908+
subject(s) and predicate"""
909+
if isinstance(subject, list):
910+
for subj in subject:
911+
for o in self.objects(subj, predicate, unique):
912+
yield o
906913
else:
907-
objs = set()
908-
for s, p, o in self.triples((subject, predicate, None)):
909-
if o not in objs:
914+
if not unique:
915+
for s, p, o in self.triples((subject, predicate, None)):
910916
yield o
911-
try:
912-
objs.add(o)
913-
except MemoryError as e:
914-
logger.error(
915-
f"{e}. Consider not setting parameter 'unique' to True"
916-
)
917-
raise
917+
else:
918+
objs = set()
919+
for s, p, o in self.triples((subject, predicate, None)):
920+
if o not in objs:
921+
yield o
922+
try:
923+
objs.add(o)
924+
except MemoryError as e:
925+
logger.error(
926+
f"{e}. Consider not setting parameter 'unique' to True"
927+
)
928+
raise
918929

919930
def subject_predicates(
920931
self, object: Optional[_ObjectType] = None, unique: bool = False

test/test_graph/test_graph_generators.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,19 @@ def test_parse_berners_lee_card_into_graph():
7575
assert len(list(graph.subjects(unique=True))) == no_of_unique_subjects
7676
assert len(list(graph.predicates(unique=True))) == no_of_unique_predicates
7777
assert len(list(graph.objects(unique=True))) == no_of_unique_objects
78+
79+
80+
def test_subjects_multi():
81+
graph = Graph()
82+
add_stuff(graph)
83+
assert len([subj for subj in graph.subjects(LIKES, [CHEESE, PIZZA])]) == 5
84+
assert len([subj for subj in graph.subjects(LIKES, [])]) == 0
85+
assert len([subj for subj in graph.subjects(LIKES | HATES, [CHEESE, PIZZA])]) == 6
86+
87+
88+
def test_objects_multi():
89+
graph = Graph()
90+
add_stuff(graph)
91+
assert len([obj for obj in graph.objects([TAREK, BOB], LIKES)]) == 6
92+
assert len([obj for obj in graph.objects([], LIKES)]) == 0
93+
assert len([obj for obj in graph.objects([TAREK, BOB], LIKES | HATES)]) == 8

0 commit comments

Comments
 (0)