@@ -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
0 commit comments