Commit 14b91f60 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Include {vertex,edge,graph}_properties aliases: vp, ep, gp

This also modifies the behaviour of the graph_properties dict, which now
return the property values directly, instead of the map objects.
parent d2d47234
......@@ -115,8 +115,11 @@
.. autoattribute:: properties
.. autoattribute:: vertex_properties
.. autoattribute:: vp
.. autoattribute:: edge_properties
.. autoattribute:: ep
.. autoattribute:: graph_properties
.. autoattribute:: gp
.. automethod:: list_properties
......
......@@ -375,7 +375,9 @@ with the graph. Properties are internalized by including them in the
graph's dictionary-like attributes
:attr:`~graph_tool.Graph.vertex_properties`,
:attr:`~graph_tool.Graph.edge_properties` or
:attr:`~graph_tool.Graph.graph_properties`. When inserted in the graph,
:attr:`~graph_tool.Graph.graph_properties` (or their aliases,
:attr:`~graph_tool.Graph.vp`, :attr:`~graph_tool.Graph.ep` or
:attr:`~graph_tool.Graph.gp`, respectively). When inserted in the graph,
the property maps must have an unique name (between those of the same
type):
......@@ -386,6 +388,20 @@ type):
>>> g.list_properties()
some name (edge) (type: string)
Internal graph property maps behave slightly differently. Instead of
returning the property map object, the value itself is returned from the
dictionaries:
.. doctest::
>>> gprop = g.new_graph_property("int")
>>> g.graph_properties["foo"] = gprop # this sets the actual property map
>>> g.graph_properties["foo"] = 42 # this sets its value
>>> print(g.graph_properties["foo"])
42
>>> del g.graph_properties["foo"] # the property map entry is deleted from the dictionary
.. _sec_graph_io:
Graph I/O
......
......@@ -1180,11 +1180,13 @@ class Graph(object):
@_limit_args({"t": ["v", "e", "g"]})
@_require("k", str)
@_require("v", PropertyMap)
def __set_property(self, t, k, v):
if t != v.key_type():
raise ValueError("wrong key type for property map")
self.__properties[(t, k)] = v
if t == "g" and not isinstance(v, PropertyMap):
self.__properties[(t, k)][self] = v
else:
if t != v.key_type():
raise ValueError("wrong key type for property map")
self.__properties[(t, k)] = v
@_limit_args({"t": ["v", "e", "g"]})
@_require("k", str)
......@@ -1218,6 +1220,8 @@ class Graph(object):
lambda g, k: g.__del_property("v", k))
vertex_properties = property(__get_vertex_properties,
doc="Dictionary of internal vertex properties. The keys are the property names.")
vp = property(__get_vertex_properties,
doc="Alias to :attr:`~Graph.vertex_properties`.")
# edge properties
def __get_edge_properties(self):
......@@ -1227,15 +1231,19 @@ class Graph(object):
lambda g, k: g.__del_property("e", k))
edge_properties = property(__get_edge_properties,
doc="Dictionary of internal edge properties. The keys are the property names.")
ep = property(__get_vertex_properties,
doc="Alias to :attr:`~Graph.edge_properties`.")
# graph properties
def __get_graph_properties(self):
return PropertyDict(self, self.__get_specific_properties("g"),
lambda g, k: g.__properties[("g", k)],
lambda g, k: g.__properties[("g", k)][g],
lambda g, k, v: g.__set_property("g", k, v),
lambda g, k: g.__del_property("g", k))
graph_properties = property(__get_graph_properties,
doc="Dictionary of internal graph properties. The keys are the property names.")
gp = property(__get_graph_properties,
doc="Alias to :attr:`~Graph.graph_properties`.")
def own_property(self, prop):
"""'Own' property map 'prop', which may belong to another graph."""
......
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