Commit 52eaa36f authored by Tiago Peixoto's avatar Tiago Peixoto

Add convenient neighbour traversal functions

Add support for v.{out|in}_neighbours(), for direct iteration over the
neighbours, as well as v.all_edges() and v.all_neighbours(). This also
properly exports the Vertex and Edge classes, with docstrings.
parent 96b9c5e7
......@@ -312,10 +312,14 @@ struct export_python_interface
{
using namespace boost::python;
class_<PythonEdge<Graph> > ("Edge", no_init)
.def("source", &PythonEdge<Graph>::GetSource)
.def("target", &PythonEdge<Graph>::GetTarget)
.def("is_valid", &PythonEdge<Graph>::IsValid)
class_<PythonEdge<Graph> >
("Edge", "This class represents an edge in a graph", no_init)
.def("source", &PythonEdge<Graph>::GetSource,
"Return the source vertex")
.def("target", &PythonEdge<Graph>::GetTarget,
"Return the target vertex")
.def("is_valid", &PythonEdge<Graph>::IsValid,
"Return whether the edge is valid")
.def(python::self == python::self)
.def(python::self != python::self)
.def("__str__", &PythonEdge<Graph>::GetString)
......@@ -386,12 +390,18 @@ void GraphInterface::ExportPythonInterface() const
{
using namespace boost::python;
class_<PythonVertex>("Vertex", no_init)
.def("in_degree", &PythonVertex::GetInDegree)
.def("out_degree", &PythonVertex::GetOutDegree)
.def("out_edges", &PythonVertex::OutEdges)
.def("in_edges", &PythonVertex::InEdges)
.def("is_valid", &PythonVertex::IsValid)
class_<PythonVertex>
("Vertex", "This class represents a vertex in a graph", no_init)
.def("in_degree", &PythonVertex::GetInDegree,
"Return the in-degree")
.def("out_degree", &PythonVertex::GetOutDegree,
"Return the out-degree")
.def("out_edges", &PythonVertex::OutEdges,
"Return an iterator over the out-edges")
.def("in_edges", &PythonVertex::InEdges,
"Return an iterator over the in-edges")
.def("is_valid", &PythonVertex::IsValid,
"Return whether the vertex is valid")
.def(python::self == python::self)
.def(python::self != python::self)
.def("__str__", &PythonVertex::GetString)
......
......@@ -86,8 +86,9 @@ __URL__="http://graph-tool.forked.de"
from . core import __version__, Graph, GraphError, Vector_bool, \
Vector_int32_t, Vector_int64_t, Vector_double, Vector_long_double,\
Vector_string, value_types, load_graph, PropertyMap
Vector_string, value_types, load_graph, PropertyMap, Vertex, Edge
__all__ = ["Graph", "GraphError", "Vector_bool", "Vector_int32_t",
"Vector_int64_t", "Vector_double", "Vector_long_double",
"Vector_string", "value_types", "load_graph", "PropertyMap"]
__all__ = ["Graph", "Vertex", "Edge", "GraphError", "Vector_bool",
"Vector_int32_t", "Vector_int64_t", "Vector_double",
"Vector_long_double", "Vector_string", "value_types", "load_graph",
"PropertyMap"]
......@@ -750,3 +750,37 @@ def load_graph(filename, format="auto"):
def value_types():
"""Return a list of possible properties value types"""
return libcore.get_property_types()
# Vertex and Edge Types
# =====================
from libgraph_tool_core import Vertex, Edge
def _out_neighbours(self):
"""Return an iterator over the out-neighbours"""
for e in self.out_edges():
yield e.target()
Vertex.out_neighbours = _out_neighbours
def _in_neighbours(self):
"""Return an iterator over the in-neighbours"""
for e in self.in_edges():
yield e.source()
Vertex.in_neighbours = _in_neighbours
def _all_edges(self):
"""Return an iterator over all edges (both in or out)"""
for e in self.out_edges():
yield e
for e in self.in_edges():
yield e
Vertex.all_edges = _all_edges
def _all_neighbours(self):
"""Return an iterator over all neighbours (both in or out)"""
for v in self.out_neighbours():
yield v
for v in self.in_neighbours():
yield v
Vertex.all_neighbours = _all_neighbours
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment