-
Notifications
You must be signed in to change notification settings - Fork 157
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 to_dot method to PyGraph and PyDiGraph #103
Conversation
This commit adds a new method to PyGraph and PyDiGraph, to_dot(), which is used to generate a graphviz dot file for a graph object. The implementation of this is influenced by petgraph's built-in dot module [1]. [1] https://github.com/petgraph/petgraph/blob/0.5.1/src/dot.rs
src/dot_utils.rs
Outdated
pub fn build_dot<G, N, E, T>( | ||
graph: G, | ||
file: &mut T, | ||
graph_attrs: Option<BTreeMap<String, String>>, | ||
mut node_attrs: N, | ||
mut edge_attrs: E, | ||
) -> PyResult<()> | ||
where | ||
T: Write, | ||
G: GraphBase | ||
+ IntoEdgeReferences | ||
+ IntoNodeReferences | ||
+ NodeIndexable | ||
+ GraphProp, | ||
G: Data<NodeWeight = PyObject, EdgeWeight = PyObject>, | ||
N: FnMut(&PyObject) -> PyResult<BTreeMap<String, String>>, | ||
E: FnMut(&PyObject) -> PyResult<BTreeMap<String, String>>, |
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 don't follow what's going on with G N E T here
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.
These are bounds for generic types on this function. Basically it's saying that the parameter graph
is of generic type G
where any object passed in via graph implements a bunch of traits. A trait defines a set of common methods and attributes that can be implemented for a type. The specific list of traits for G
are from petgraph and basically are just shared functions for graph traversal and access (I got most of them from compiler errors and the similar function in petgraph that generates dot). The definitions of them are here: https://docs.rs/petgraph/0.5.1/petgraph/visit/index.html#traits but they basically just enable us to pass in either a PyGraph
or PyDiGraph
and have the function work as expected for both (the trait definitions for either type are at the top of both digraph.rs
and graph.rs
).
T
, N
, and E
are easier to follow because they're much simpler. Parameter file
is a mutable borrow of generic type T
, where T
is anything that implements the Write trait. This can be a file object, a buffer, etc. Parameter node_attrs
is a generic type N
whiere N
is any function that takes in a &PyObject
and returns a PyResult<BTreeMap<String, String>
and parameter edge_attrs
is a generic type E
where it's also a function like that (E
and N
are different though because they are not necessarily going to be the same function).
Co-authored-by: Lauren Capelluto <[email protected]>
This commit changes the node_attrs and edge_attrs callables to be opt-in and default to None. This makes the user burden lower because if you don't require extra formatting this simplifies the call signature.
The to_dot methods were documented as returning a string if filename was not specified, but we were returing a bytes object not a string. This commit corrects the return type to convert the bytes array to a string primitive and returning that instead of the raw PyBytes object.
Since the introduction of the to_dot() methods in #103 and the use of jupyter-sphinx in the docs, the read the docs builds have been failing. It looks like this is due to: jupyter/jupyter-sphinx#126 This commit fixes this issue by pinning the jupyter sphinx version to a version that is known working on read the docs.
This commit adds a new method to PyGraph and PyDiGraph, to_dot(), which
is used to generate a graphviz dot file for a graph object. The
implementation of this is influenced by petgraph's built-in dot module
[1].
[1] https://github.com/petgraph/petgraph/blob/0.5.1/src/dot.rs