Skip to content

Commit

Permalink
Fixing docs for nx.info(), along with necessary tests (networkx#3893)
Browse files Browse the repository at this point in the history
* documenting the behaviour correctly

* adding tests to lock the behaviour

* PEP

* one-line docstring

* pep
  • Loading branch information
raamana authored Apr 7, 2020
1 parent be3449d commit 49dee1c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
16 changes: 15 additions & 1 deletion networkx/classes/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,14 +525,28 @@ def create_empty_copy(G, with_data=True):


def info(G, n=None):
"""Print short summary of information for the graph G or the node n.
"""Return a summary of information for the graph G or a single node n.
The summary includes the number of nodes and edges (or neighbours for a single
node), and their average degree.
Parameters
----------
G : Networkx graph
A graph
n : node (any hashable)
A node in the graph G
Returns
-------
info : str
A string containing the short summary
Raises
------
NetworkXError
If n is not in the graph G
"""
info = '' # append this all to a string
if n is None:
Expand Down
4 changes: 4 additions & 0 deletions networkx/classes/tests/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,16 @@ def test_info(self):
assert info == expected_graph_info

info = nx.info(G, n=1)
assert type(info) == str
expected_node_info = '\n'.join(
['Node 1 has the following properties:',
'Degree: 2',
'Neighbors: 0 2'])
assert info == expected_node_info

# must raise an error for a non-existent node
pytest.raises(nx.NetworkXError, nx.info, G, 1248)

def test_info_digraph(self):
G = nx.DiGraph(name='path_graph(5)')
nx.add_path(G, [0, 1, 2, 3, 4])
Expand Down

0 comments on commit 49dee1c

Please sign in to comment.