Commit 7a3620b2 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Add short aliases to Graph.new_vertex/edge/graph_property()

parent 31013589
......@@ -82,8 +82,11 @@
.. automethod:: new_property
.. automethod:: new_vertex_property
.. automethod:: new_vp
.. automethod:: new_edge_property
.. automethod:: new_ep
.. automethod:: new_graph_property
.. automethod:: new_gp
New property maps can be created by copying already existing
ones.
......
......@@ -120,7 +120,7 @@ import collections
if sys.version_info < (3,):
import StringIO
from .decorators import _wraps, _require, _attrs, _limit_args
from .decorators import _wraps, _require, _attrs, _limit_args, _copy_func
from inspect import ismethod
__all__ = ["Graph", "GraphView", "Vertex", "Edge", "VertexBase", "EdgeBase",
......@@ -2101,6 +2101,9 @@ class Graph(object):
prop.set_value(val)
return prop
new_vp = _copy_func(new_vertex_property, "new_vp")
new_vp.__doc__ = "Alias to :func:`~graph_tool.Graph.new_vertex_property`."
def new_edge_property(self, value_type, vals=None, val=None):
"""Create a new edge property map of type ``value_type``, and return it. If
provided, the values will be initialized by ``vals``, which should be
......@@ -2120,6 +2123,9 @@ class Graph(object):
prop.set_value(val)
return prop
new_ep = _copy_func(new_edge_property, "new_ep")
new_ep.__doc__ = "Alias to :func:`~graph_tool.Graph.new_edge_property`."
def new_graph_property(self, value_type, val=None):
"""Create a new graph property map of type ``value_type``, and return
it. If ``val`` is not None, the property is initialized to its value."""
......@@ -2131,6 +2137,9 @@ class Graph(object):
prop[self] = val
return prop
new_gp = _copy_func(new_graph_property, "new_gp")
new_gp.__doc__ = "Alias to :func:`~graph_tool.Graph.new_graph_property`."
# property map copying
@_require("src", PropertyMap)
@_require("tgt", (PropertyMap, type(None)))
......
......@@ -30,6 +30,7 @@ __license__ = "GPL version 3 or above"
import inspect
import functools
import sys
import types
################################################################################
# Decorators
......@@ -145,3 +146,9 @@ def _require(arg_name, *allowed_types):
wrapper.wrapped_args = wrapped_args
return wrapper
return make_wrapper
def _copy_func(f, name=None):
fn = types.FunctionType(f.__code__, f.__globals__, name or f.__name__,
f.__defaults__, f.__closure__)
fn.__dict__.update(f.__dict__)
return fn
\ No newline at end of file
Supports Markdown
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