-
Notifications
You must be signed in to change notification settings - Fork 26
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
Add containment for trees and graphs #118
base: main
Are you sure you want to change the base?
Conversation
Failing lint: which command should I run? I see that flake8 is in the setup.py dependencies but I am not sure which arguments to run it with for your preferences. Failing tests: seems unrelated to this PR (an error about py36 not available). |
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.
Sorry, this fell off my radar. I've reviewed the PR. Generally it seems good, and I now think that subgraph checks should be done via a special method rather than via the in
operator. The linting errors are due to raising NotImplemented
instead of NotImplementedError
, as I've pointed out below, as well as some lines being longer than 79 characters (which is the default and current line length limit).
I also wonder if we should simplify it so the in
check only looks for triples in graphs and use the Graph._filter_triples()
method, so you could use None
wildcards to do things like:
(None, ":ARG9", None) in g # any triple with role :ARG9
(None, ":instance", "foo") in g # any instance with concept "foo"
It's a bit more verbose for checking for concepts, but it's more consistent. We could also change the Graph.instances()
method to accept a concept
parameter to make things easier:
g.instances(concept="foo") # -> [("f", ":instance", "foo")]
It would help to know what people's use cases are for the functionality. What do you think?
penman/tree.py
Outdated
if isinstance(item, str): | ||
terminals = [target for _, (role, target) in self.walk() if role == "/" and is_atomic(target)] | ||
return item in terminals | ||
else: | ||
raise NotImplemented |
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.
It seems like you could look for branches as you did for triples above. Otherwise, same comments as before:
- make a set
- Try not to raise a specific error, or if it's an unsupported type use
TypeError
tests/test_graph.py
Outdated
|
||
assert "alpha" in g | ||
assert "beta" in g | ||
assert "b" not in g |
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.
This one is confusing because b
is in g, it's just not defined as such. Not sure what the best path forward is. But you could have something obviously false, like assert "gamma" not in g
Co-authored-by: Michael Wayne Goodman <[email protected]>
@goodmami I have considered your comments and I like the suggestion of just allowing for triple matching in the graphs only, and then also allowing users to specify "None" in a triple to ignore that field. Two questions/concerns:
What do you think? PS: linter fails on files that I have not touched, so not sure how I can help that. |
>>> import penman
>>> penman.decode('()').triples
[(None, ':instance', None)]
>>> penman.decode('(a)').triples
[('a', ':instance', None)]
>>> penman.decode('(a :ARG0)').triples
Missing target: (a :ARG0)
[('a', ':instance', None), ('a', ':ARG0', None)] I don't think roles are ever >>> penman.decode('(a :)').triples
Missing target: (a :)
[('a', ':instance', None), ('a', ':', None)] So, yes, it's true that you cannot use the graph methods like Graph.edges() to look for those that have a literal
I think you're right. Let's hold off on changing |
Added simple containment
__contains__
for graphs and trees. Tests are added, too.For graphs:
str
: whether a given string is part of the graph as a terminal instanceNotImplemented
in other cases - so testing for subgraphs is not yet implement. Open to suggestions how to do that though. I was thinking of just doing triplet matching, but perhaps we want to be less strict and allow a match with different varnames as well?For trees:
str
: whether a given string is part of the tree as a terminal instanceI get the terminal nodes in the tree like this:
which feels elaborate so perhaps there is a simpler way? Happy to change it if there is!
closes #10 (partially, at least, since subgraph testing is not included in this PR)