easygraph.utils.decorators module#

easygraph.utils.decorators.hybrid(cpp_method_name)[source]#
easygraph.utils.decorators.nodes_or_number(which_args)[source]#

Decorator to allow number of nodes or container of nodes.

With this decorator, the specified argument can be either a number or a container of nodes. If it is a number, the nodes used are range(n). This allows eg.complete_graph(50) in place of eg.complete_graph(list(range(50))). And it also allows eg.complete_graph(any_list_of_nodes).

Parameters:

which_args (string or int or sequence of strings or ints) – If string, the name of the argument to be treated. If int, the index of the argument to be treated. If more than one node argument is allowed, can be a list of locations.

Returns:

_nodes_or_numbers – Function which replaces int args with ranges.

Return type:

function

Examples

Decorate functions like this:

@nodes_or_number("nodes")
def empty_graph(nodes):
    # nodes is converted to a list of nodes

@nodes_or_number(0)
def empty_graph(nodes):
    # nodes is converted to a list of nodes

@nodes_or_number(["m1", "m2"])
def grid_2d_graph(m1, m2, periodic=False):
    # m1 and m2 are each converted to a list of nodes

@nodes_or_number([0, 1])
def grid_2d_graph(m1, m2, periodic=False):
    # m1 and m2 are each converted to a list of nodes

@nodes_or_number(1)
def full_rary_tree(r, n)
    # presumably r is a number. It is not handled by this decorator.
    # n is converted to a list of nodes
easygraph.utils.decorators.not_implemented_for(*graph_types)[source]#

Decorator to mark algorithms as not implemented

Parameters:

graph_types (container of strings) – Entries must be one of “directed”, “undirected”, “multigraph”, or “graph”.

Returns:

_require – The decorated function.

Return type:

function

Raises:

Notes

Multiple types are joined logically with “and”. For “or” use multiple @not_implemented_for() lines.

Examples

Decorate functions like this:

@not_implemented_for("directed")
def sp_function(G):
    pass

# rule out MultiDiGraph
@not_implemented_for("directed","multigraph")
def sp_np_function(G):
    pass

# rule out all except DiGraph
@not_implemented_for("undirected")
@not_implemented_for("multigraph")
def sp_np_function(G):
    pass
easygraph.utils.decorators.only_implemented_for_Directed_graph(func)[source]#
easygraph.utils.decorators.only_implemented_for_UnDirected_graph(func)[source]#
easygraph.utils.decorators.open_file(path_arg, mode='r')[source]#

Decorator to ensure clean opening and closing of files.

Parameters:
  • path_arg (string or int) – Name or index of the argument that is a path.

  • mode (str) – String for opening mode.

Returns:

_open_file – Function which cleanly executes the io.

Return type:

function

Examples

Decorate functions like this:

@open_file(0,"r")
def read_function(pathname):
    pass

@open_file(1,"w")
def write_function(G, pathname):
    pass

@open_file(1,"w")
def write_function(G, pathname="graph.dot"):
    pass

@open_file("pathname","w")
def write_function(G, pathname="graph.dot"):
    pass

@open_file("path", "w+")
def another_function(arg, **kwargs):
    path = kwargs["path"]
    pass

Notes

Note that this decorator solves the problem when a path argument is specified as a string, but it does not handle the situation when the function wants to accept a default of None (and then handle it).

Here is an example of how to handle this case:

@open_file("path")
def some_function(arg1, arg2, path=None):
   if path is None:
       fobj = tempfile.NamedTemporaryFile(delete=False)
   else:
       # `path` could have been a string or file object or something
       # similar. In any event, the decorator has given us a file object
       # and it will close it for us, if it should.
       fobj = path

   try:
       fobj.write("blah")
   finally:
       if path is None:
           fobj.close()

Normally, we’d want to use “with” to ensure that fobj gets closed. However, the decorator will make path a file object for us, and using “with” would undesirably close that file object. Instead, we use a try block, as shown above. When we exit the function, fobj will be closed, if it should be, by the decorator.

easygraph.utils.decorators.retry_method_with_fix(fix_method)[source]#

Decorator that executes a fix method before retrying again when the decorated method fails once with any exception.

If the decorated method fails again, the execution fails with that exception.

Notes

This decorator only works on class methods, and the fix function must also be a class method. It would not work on functions.

Parameters:

fix_func (callable) – The fix method to execute. It should not accept any arguments. Its return values are ignored.