Commit 6aa4cfb9 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Improve conversion of compatible types in search functions

parent 2a4f9320
......@@ -193,6 +193,15 @@ def _python_type(type_name):
return object
def _convert(prop, val):
# attempt to convert to a compatible python type. This is useful,
# for instance, when dealing with numpy types.
vtype = _python_type(prop.value_type())
if type(vtype) is tuple:
return [vtype[1](x) for x in val]
return vtype(val)
def show_config():
"""Show ``graph_tool`` build configuration."""
info = libcore.mod_info()
......@@ -373,14 +382,7 @@ class PropertyMap(object):
try:
self.__map[key] = v
except TypeError:
# attempt to convert to a compatible python type. This is useful,
# for instance, when dealing with numpy scalar types.
valtype = self.python_value_type()
if isinstance(valtype, tuple):
val = [valtype[1](x) for x in v]
else:
val = valtype(v)
self.__map[key] = val
self.__map[key] = _convert(self, v)
def __repr__(self):
# provide some more useful information
......
......@@ -41,7 +41,7 @@ Contents
from .. dl_import import dl_import
dl_import("import libgraph_tool_util")
from .. import _degree, _prop
from .. import _degree, _prop, _convert
import weakref
__all__ = ["find_vertex", "find_vertex_range", "find_edge", "find_edge_range"]
......@@ -51,9 +51,10 @@ def find_vertex(g, prop, match):
"""Find all vertices `v` for which `prop[v] = match`. The parameter prop
can be either a :class:`~graph_tool.PropertyMap` or string with value "in",
"out" or "total", representing a degree type."""
val = _convert(prop, match)
ret = libgraph_tool_util.\
find_vertex_range(weakref.ref(g._Graph__graph), _degree(g, prop),
(match, match))
(val, val))
return ret
......@@ -63,22 +64,24 @@ def find_vertex_range(g, prop, range):
with value"in", "out" or "total", representing a degree type."""
ret = libgraph_tool_util.\
find_vertex_range(weakref.ref(g._Graph__graph), _degree(g, prop),
range)
(_convert(prop, range[0]), _convert(prop, range[1])))
return ret
def find_edge(g, prop, match):
"""Find all vertices `e` for which `prop[e] = match`. The parameter prop
must be a :class:`~graph_tool.PropertyMap`."""
val = _convert(prop, match)
ret = libgraph_tool_util.\
find_edge_range(weakref.ref(g._Graph__graph), _prop("e", g, prop),
(match, match))
(val, val))
return ret
def find_edge_range(g, prop, range):
"""Find all vertices `e` for which `range[0] <= prop[e] <= range[1]`. The
parameter prop can be either a :class:`~graph_tool.PropertyMap`."""
ret = libgraph_tool_util.\
find_edge_range(weakref.ref(g._Graph__graph), _prop("e", g, prop),
range)
(_convert(prop, range[0]), _convert(prop, range[1])))
return ret
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