-
Notifications
You must be signed in to change notification settings - Fork 577
Allow multi subjects & objects in graph funcs #3086
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -847,26 +847,32 @@ def set( | |
def subjects( | ||
self, | ||
predicate: Union[None, Path, _PredicateType] = None, | ||
object: Optional[_ObjectType] = None, | ||
object: Optional[Union[_ObjectType, List[_ObjectType]]] = None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nicholascar : Was there a reason this used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use case I have in mind: I often load |
||
unique: bool = False, | ||
) -> Generator[_SubjectType, None, None]: | ||
"""A generator of (optionally unique) subjects with the given | ||
predicate and object""" | ||
if not unique: | ||
for s, p, o in self.triples((None, predicate, object)): | ||
yield s | ||
predicate and object(s)""" | ||
# if the object is a list of Nodes, yield results from subject() call for each | ||
if isinstance(object, list): | ||
for obj in object: | ||
for s in self.subjects(predicate, obj, unique): | ||
yield s | ||
else: | ||
subs = set() | ||
for s, p, o in self.triples((None, predicate, object)): | ||
if s not in subs: | ||
if not unique: | ||
for s, p, o in self.triples((None, predicate, object)): | ||
yield s | ||
try: | ||
subs.add(s) | ||
except MemoryError as e: | ||
logger.error( | ||
f"{e}. Consider not setting parameter 'unique' to True" | ||
) | ||
raise | ||
else: | ||
subs = set() | ||
for s, p, o in self.triples((None, predicate, object)): | ||
if s not in subs: | ||
yield s | ||
try: | ||
subs.add(s) | ||
except MemoryError as e: | ||
logger.error( | ||
f"{e}. Consider not setting parameter 'unique' to True" | ||
) | ||
raise | ||
|
||
def predicates( | ||
self, | ||
|
@@ -894,27 +900,32 @@ def predicates( | |
|
||
def objects( | ||
self, | ||
subject: Optional[_SubjectType] = None, | ||
subject: Optional[Union[_SubjectType, List[_SubjectType]]] = None, | ||
predicate: Union[None, Path, _PredicateType] = None, | ||
unique: bool = False, | ||
) -> Generator[_ObjectType, None, None]: | ||
"""A generator of (optionally unique) objects with the given | ||
subject and predicate""" | ||
if not unique: | ||
for s, p, o in self.triples((subject, predicate, None)): | ||
yield o | ||
subject(s) and predicate""" | ||
if isinstance(subject, list): | ||
for subj in subject: | ||
for o in self.objects(subj, predicate, unique): | ||
yield o | ||
else: | ||
objs = set() | ||
for s, p, o in self.triples((subject, predicate, None)): | ||
if o not in objs: | ||
if not unique: | ||
for s, p, o in self.triples((subject, predicate, None)): | ||
yield o | ||
try: | ||
objs.add(o) | ||
except MemoryError as e: | ||
logger.error( | ||
f"{e}. Consider not setting parameter 'unique' to True" | ||
) | ||
raise | ||
else: | ||
objs = set() | ||
for s, p, o in self.triples((subject, predicate, None)): | ||
if o not in objs: | ||
yield o | ||
try: | ||
objs.add(o) | ||
except MemoryError as e: | ||
logger.error( | ||
f"{e}. Consider not setting parameter 'unique' to True" | ||
) | ||
raise | ||
|
||
def subject_predicates( | ||
self, object: Optional[_ObjectType] = None, unique: bool = False | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
object
gets a plurality expansion, why notpredicate
too (and likewise with other functions)? Something to do withPath
?