-
Notifications
You must be signed in to change notification settings - Fork 193
from_dot feature #1489
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
base: main
Are you sure you want to change the base?
from_dot feature #1489
Conversation
Built a basic grammar and wired it up with functions to parse it and added it in the pymodule. Next steps: 1) Fix the grammar, make it robust. 2) Add tests and documentations
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 is an excellent start!
I will give it a deeper review, specially about graphviz's dot grammar which I need to understand more deeply.
I left some comments about tests and the grammar to get started
src/dot_parser/mod.rs
Outdated
} | ||
|
||
Rule::subgraph => { | ||
// TODO: subgraph handling |
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's ok to return an error for subgraphs now. dot
might be too expresive. We just want to make sure the output from a rustworkx entry can be re-imported as the original goal.
Once we have that, we can support more advanced features
src/dot_parser/mod.rs
Outdated
}); | ||
} else { | ||
let graph = StablePyGraph::<Undirected>::with_capacity(0, 0); | ||
undi_graph = Some(PyGraph { |
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.
Some thoughts about this handling:
- I'd make a function that is parametrized on StablePyGraph e.g.
Line 37 in f0240d4
fn union<Ty: EdgeType>( - Upon detecting the type, call the parametrization on the if/else
If you need to detect the direction of the graph, call https://docs.rs/petgraph/latest/petgraph/stable_graph/struct.StableGraph.html#method.is_directed within the parametrized function.
I think that choice will simplify your code, especially for handling both di_graph
and undi_graph
on every statement
src/dot_parser/mod.rs
Outdated
let src_idx = *node_map.entry(src.clone()).or_insert_with(|| { | ||
let py_node = PyString::new(py, &src).into(); | ||
if is_directed { | ||
di_graph.as_mut().unwrap().graph.add_node(py_node) |
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 you follow the previous recommendation, this becomes graph.add_node
which arguably is simpler that handling the Option<PyGraph>
each time
1) Added a new function to use stable graph with is_directed as a parameter to avoid creating python object every time. 2) Added more tests to include round trip from to_dot and from_dot for graph and digraph.
Hey @IvanIsCoding, could you take a look again. |
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.
I left some minor comments, but this is very close to getting merged:
- Use the hashmap from
hashbrown
for consistency. It should be slightly faster because of the hasher it uses vsstd
which uses a more conservative hasher - Avoid panicking, be careful with
unwrap()
. Python users cannot recover easily from panic, they'd rather deal with exceptions.
And last but not least, don't forget to add a release note: https://github.com/Qiskit/rustworkx/blob/main/CONTRIBUTING.md#release-notes. I am planning on promoting this as one of the key new features in the 0.18.0 launch. People will want to read about it, it's handy!
src/dot_parser/mod.rs
Outdated
use crate::StablePyGraph; | ||
|
||
use rustworkx_core::petgraph::prelude::{Directed, NodeIndex, Undirected}; | ||
use std::collections::HashMap; |
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.
Use the hashmap from hashbrown: https://docs.rs/hashbrown/latest/hashbrown/struct.HashMap.html
That's what we use everywhere
src/dot_parser/mod.rs
Outdated
continue; | ||
} | ||
let mut inner = pair.into_inner(); | ||
let first = inner.next().unwrap(); |
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.
Let's avoid unwrapping if possible, do inner.next()?
.
src/dot_parser/mod.rs
Outdated
match stmt.as_rule() { | ||
Rule::node_stmt => { | ||
let mut it = stmt.into_inner(); | ||
let nid = it.next().unwrap(); |
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.
Same comment here. If you keep needing a conversion from pest errors to PyResult
, make a helper closure and do a .map_err
or something along those lines
|
||
Rule::assignment => { | ||
let mut parts = stmt.into_inner(); | ||
let key = parts.next().map(|p| p.as_str()).unwrap_or(""); |
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 unwrap_or
is fine. Keep it as is.
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.
Also, I think you'll need to downgrade pest
to compile with our MSRV.
I can also bump the MSRV in a separate PR
Cargo.toml
Outdated
@@ -64,6 +64,8 @@ serde_json = "1.0" | |||
smallvec = { version = "1.0", features = ["union"] } | |||
rustworkx-core = { path = "rustworkx-core", version = "=0.17.0" } | |||
flate2 = "1.0.35" | |||
pest = "2.7" |
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.
By the way, changes to Cargo.toml should also have a corresponding Cargo.lock change. Did you forget to commit Cargo.lock?
8232427
to
dbe10e7
Compare
Feat: Add from_dot parser support