networkx 2.8.2 Pypi GitHub Homepage
Other Docs
NotesParametersReturnsBackRef
open_file(path_arg, mode='r')

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 :None:None:`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.

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

Function which cleanly executes the io.

Decorator to ensure clean opening and closing of files.

Examples

@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

See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

networkx.utils.decorators.argmap

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /networkx/utils/decorators.py#103
type: <class 'function'>
Commit: