Skip to content

Commit

Permalink
Update docstring formatter (#7276)
Browse files Browse the repository at this point in the history
Switch from blacken-docs to ruff-format for docstrings.
  • Loading branch information
cnpryer committed Feb 9, 2024
1 parent 64489da commit 3c0f096
Show file tree
Hide file tree
Showing 53 changed files with 303 additions and 243 deletions.
4 changes: 0 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
# pre-commit install

repos:
- repo: https://github.com/adamchainz/blacken-docs
rev: 1.16.0
hooks:
- id: blacken-docs
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.1.0
hooks:
Expand Down
69 changes: 52 additions & 17 deletions networkx/algorithms/approximation/traveling_salesman.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,10 @@ def asadpour_atsp(G, weight="weight", seed=None, source=None):
>>> import networkx as nx
>>> import networkx.algorithms.approximation as approx
>>> G = nx.complete_graph(3, create_using=nx.DiGraph)
>>> nx.set_edge_attributes(G, {(0, 1): 2, (1, 2): 2, (2, 0): 2, (0, 2): 1, (2, 1): 1, (1, 0): 1}, "weight")
>>> tour = approx.asadpour_atsp(G,source=0)
>>> nx.set_edge_attributes(
... G, {(0, 1): 2, (1, 2): 2, (2, 0): 2, (0, 2): 1, (2, 1): 1, (1, 0): 1}, "weight"
... )
>>> tour = approx.asadpour_atsp(G, source=0)
>>> tour
[0, 2, 1, 0]
"""
Expand Down Expand Up @@ -951,11 +953,22 @@ def greedy_tsp(G, weight="weight", source=None):
--------
>>> from networkx.algorithms import approximation as approx
>>> G = nx.DiGraph()
>>> G.add_weighted_edges_from({
... ("A", "B", 3), ("A", "C", 17), ("A", "D", 14), ("B", "A", 3),
... ("B", "C", 12), ("B", "D", 16), ("C", "A", 13),("C", "B", 12),
... ("C", "D", 4), ("D", "A", 14), ("D", "B", 15), ("D", "C", 2)
... })
>>> G.add_weighted_edges_from(
... {
... ("A", "B", 3),
... ("A", "C", 17),
... ("A", "D", 14),
... ("B", "A", 3),
... ("B", "C", 12),
... ("B", "D", 16),
... ("C", "A", 13),
... ("C", "B", 12),
... ("C", "D", 4),
... ("D", "A", 14),
... ("D", "B", 15),
... ("D", "C", 2),
... }
... )
>>> cycle = approx.greedy_tsp(G, source="D")
>>> cost = sum(G[n][nbr]["weight"] for n, nbr in nx.utils.pairwise(cycle))
>>> cycle
Expand Down Expand Up @@ -1114,11 +1127,22 @@ def simulated_annealing_tsp(
--------
>>> from networkx.algorithms import approximation as approx
>>> G = nx.DiGraph()
>>> G.add_weighted_edges_from({
... ("A", "B", 3), ("A", "C", 17), ("A", "D", 14), ("B", "A", 3),
... ("B", "C", 12), ("B", "D", 16), ("C", "A", 13),("C", "B", 12),
... ("C", "D", 4), ("D", "A", 14), ("D", "B", 15), ("D", "C", 2)
... })
>>> G.add_weighted_edges_from(
... {
... ("A", "B", 3),
... ("A", "C", 17),
... ("A", "D", 14),
... ("B", "A", 3),
... ("B", "C", 12),
... ("B", "D", 16),
... ("C", "A", 13),
... ("C", "B", 12),
... ("C", "D", 4),
... ("D", "A", 14),
... ("D", "B", 15),
... ("D", "C", 2),
... }
... )
>>> cycle = approx.simulated_annealing_tsp(G, "greedy", source="D")
>>> cost = sum(G[n][nbr]["weight"] for n, nbr in nx.utils.pairwise(cycle))
>>> cycle
Expand Down Expand Up @@ -1331,11 +1355,22 @@ def threshold_accepting_tsp(
--------
>>> from networkx.algorithms import approximation as approx
>>> G = nx.DiGraph()
>>> G.add_weighted_edges_from({
... ("A", "B", 3), ("A", "C", 17), ("A", "D", 14), ("B", "A", 3),
... ("B", "C", 12), ("B", "D", 16), ("C", "A", 13),("C", "B", 12),
... ("C", "D", 4), ("D", "A", 14), ("D", "B", 15), ("D", "C", 2)
... })
>>> G.add_weighted_edges_from(
... {
... ("A", "B", 3),
... ("A", "C", 17),
... ("A", "D", 14),
... ("B", "A", 3),
... ("B", "C", 12),
... ("B", "D", 16),
... ("C", "A", 13),
... ("C", "B", 12),
... ("C", "D", 4),
... ("D", "A", 14),
... ("D", "B", 15),
... ("D", "C", 2),
... }
... )
>>> cycle = approx.threshold_accepting_tsp(G, "greedy", source="D")
>>> cost = sum(G[n][nbr]["weight"] for n, nbr in nx.utils.pairwise(cycle))
>>> cycle
Expand Down
12 changes: 6 additions & 6 deletions networkx/algorithms/assortativity/mixing.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ def attribute_mixing_matrix(G, attribute, nodes=None, mapping=None, normalized=T
Examples
--------
>>> G = nx.path_graph(3)
>>> gender = {0: 'male', 1: 'female', 2: 'female'}
>>> nx.set_node_attributes(G, gender, 'gender')
>>> mapping = {'male': 0, 'female': 1}
>>> mix_mat = nx.attribute_mixing_matrix(G, 'gender', mapping=mapping)
>>> gender = {0: "male", 1: "female", 2: "female"}
>>> nx.set_node_attributes(G, gender, "gender")
>>> mapping = {"male": 0, "female": 1}
>>> mix_mat = nx.attribute_mixing_matrix(G, "gender", mapping=mapping)
>>> # mixing from male nodes to female nodes
>>> mix_mat[mapping['male'], mapping['female']]
>>> mix_mat[mapping["male"], mapping["female"]]
0.25
"""
d = attribute_mixing_dict(G, attribute, nodes)
Expand Down Expand Up @@ -201,7 +201,7 @@ def degree_mixing_matrix(
have that degree, use `mapping` as follows,
>>> max_degree = max(deg for n, deg in G.degree)
>>> mapping = {x: x for x in range(max_degree + 1)} # identity mapping
>>> mapping = {x: x for x in range(max_degree + 1)} # identity mapping
>>> mix_mat = nx.degree_mixing_matrix(G, mapping=mapping)
>>> mix_mat[3, 1] # mixing from node degree 3 to node degree 1
0.5
Expand Down
13 changes: 2 additions & 11 deletions networkx/algorithms/bipartite/projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ def collaboration_weighted_projected_graph(B, nodes):
[0, 2, 4, 5]
>>> for edge in sorted(G.edges(data=True)):
... print(edge)
...
(0, 2, {'weight': 0.5})
(0, 5, {'weight': 0.5})
(2, 4, {'weight': 1.0})
Expand Down Expand Up @@ -451,22 +450,18 @@ def generic_weighted_projected_graph(B, nodes, weight_function=None):
... unbrs = set(G[u])
... vnbrs = set(G[v])
... return float(len(unbrs & vnbrs)) / len(unbrs | vnbrs)
...
>>> def my_weight(G, u, v, weight="weight"):
... w = 0
... for nbr in set(G[u]) & set(G[v]):
... w += G[u][nbr].get(weight, 1) + G[v][nbr].get(weight, 1)
... return w
...
>>> # A complete bipartite graph with 4 nodes and 4 edges
>>> B = nx.complete_bipartite_graph(2, 2)
>>> # Add some arbitrary weight to the edges
>>> for i, (u, v) in enumerate(B.edges()):
... B.edges[u, v]["weight"] = i + 1
...
>>> for edge in B.edges(data=True):
... print(edge)
...
(0, 2, {'weight': 1})
(0, 3, {'weight': 2})
(1, 2, {'weight': 3})
Expand All @@ -476,14 +471,10 @@ def generic_weighted_projected_graph(B, nodes, weight_function=None):
>>> print(list(G.edges(data=True)))
[(0, 1, {'weight': 2})]
>>> # To specify a custom weight function use the weight_function parameter
>>> G = bipartite.generic_weighted_projected_graph(
... B, [0, 1], weight_function=jaccard
... )
>>> G = bipartite.generic_weighted_projected_graph(B, [0, 1], weight_function=jaccard)
>>> print(list(G.edges(data=True)))
[(0, 1, {'weight': 1.0})]
>>> G = bipartite.generic_weighted_projected_graph(
... B, [0, 1], weight_function=my_weight
... )
>>> G = bipartite.generic_weighted_projected_graph(B, [0, 1], weight_function=my_weight)
>>> print(list(G.edges(data=True)))
[(0, 1, {'weight': 10})]
Expand Down
3 changes: 2 additions & 1 deletion networkx/algorithms/clique.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,9 @@ def make_max_clique_graph(G, create_using=None):
This function behaves like the following code::
import networkx as nx
G = nx.make_clique_bipartite(G)
cliques = [v for v in G.nodes() if G.nodes[v]['bipartite'] == 0]
cliques = [v for v in G.nodes() if G.nodes[v]["bipartite"] == 0]
G = nx.bipartite.projected_graph(G, cliques)
G = nx.relabel_nodes(G, {-v: v - 1 for v in G})
Expand Down
11 changes: 5 additions & 6 deletions networkx/algorithms/components/strongly_connected.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ def strongly_connected_components(G):
>>> G = nx.cycle_graph(4, create_using=nx.DiGraph())
>>> nx.add_cycle(G, [10, 11, 12])
>>> [
... len(c)
... for c in sorted(nx.strongly_connected_components(G), key=len, reverse=True)
... ]
>>> [len(c) for c in sorted(nx.strongly_connected_components(G), key=len, reverse=True)]
[4, 3]
If you only want the largest component, it's more efficient to
Expand Down Expand Up @@ -277,7 +274,9 @@ def number_strongly_connected_components(G):
Examples
--------
>>> G = nx.DiGraph([(0, 1), (1, 2), (2, 0), (2, 3), (4, 5), (3, 4), (5, 6), (6, 3), (6, 7)])
>>> G = nx.DiGraph(
... [(0, 1), (1, 2), (2, 0), (2, 3), (4, 5), (3, 4), (5, 6), (6, 3), (6, 7)]
... )
>>> nx.number_strongly_connected_components(G)
3
Expand Down Expand Up @@ -391,7 +390,7 @@ def condensation(G, scc=None):
>>> H = nx.condensation(G)
>>> H.nodes.data()
NodeDataView({0: {'members': {0, 1, 2, 3}}, 1: {'members': {4, 5, 6, 7}}})
>>> H.graph['mapping']
>>> H.graph["mapping"]
{0: 0, 1: 0, 2: 0, 3: 0, 4: 1, 5: 1, 6: 1, 7: 1}
Contracting a complete graph into one single SCC.
Expand Down
5 changes: 1 addition & 4 deletions networkx/algorithms/components/weakly_connected.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ def weakly_connected_components(G):
>>> G = nx.path_graph(4, create_using=nx.DiGraph())
>>> nx.add_path(G, [10, 11, 12])
>>> [
... len(c)
... for c in sorted(nx.weakly_connected_components(G), key=len, reverse=True)
... ]
>>> [len(c) for c in sorted(nx.weakly_connected_components(G), key=len, reverse=True)]
[4, 3]
If you only want the largest component, it's more efficient to
Expand Down
2 changes: 0 additions & 2 deletions networkx/algorithms/connectivity/connectivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ def local_node_connectivity(
>>> # You also have to explicitly import the function for
>>> # building the auxiliary digraph from the connectivity package
>>> from networkx.algorithms.connectivity import build_auxiliary_node_connectivity
...
>>> H = build_auxiliary_node_connectivity(G)
>>> # And the function for building the residual network from the
>>> # flow package
Expand All @@ -131,7 +130,6 @@ def local_node_connectivity(
>>> for u, v in itertools.combinations(G, 2):
... k = local_node_connectivity(G, u, v, auxiliary=H, residual=R)
... result[u][v] = k
...
>>> all(result[u][v] == 5 for u, v in itertools.combinations(G, 2))
True
Expand Down
8 changes: 4 additions & 4 deletions networkx/algorithms/dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def lexicographical_topological_sort(G, key=None):
The sort will fail for any graph with integer and string nodes. Comparison of integer to strings
is not defined in python. Is 3 greater or less than 'red'?
>>> DG = nx.DiGraph([(1, 'red'), (3, 'red'), (1, 'green'), (2, 'blue')])
>>> DG = nx.DiGraph([(1, "red"), (3, "red"), (1, "green"), (2, "blue")])
>>> list(nx.lexicographical_topological_sort(DG))
Traceback (most recent call last):
...
Expand Down Expand Up @@ -853,7 +853,7 @@ def transitive_reduction(G):
To perform transitive reduction on a DiGraph and transfer node/edge data:
>>> DG = nx.DiGraph()
>>> DG.add_edges_from([(1, 2), (2, 3), (1, 3)], color='red')
>>> DG.add_edges_from([(1, 2), (2, 3), (1, 3)], color="red")
>>> TR = nx.transitive_reduction(DG)
>>> TR.add_nodes_from(DG.nodes(data=True))
>>> TR.add_edges_from((u, v, DG.edges[u, v]) for u, v in TR.edges)
Expand Down Expand Up @@ -988,7 +988,7 @@ def dag_longest_path(G, weight="weight", default_weight=1, topo_order=None):
Examples
--------
>>> DG = nx.DiGraph([(0, 1, {'cost':1}), (1, 2, {'cost':1}), (0, 2, {'cost':42})])
>>> DG = nx.DiGraph([(0, 1, {"cost": 1}), (1, 2, {"cost": 1}), (0, 2, {"cost": 42})])
>>> list(nx.all_simple_paths(DG, 0, 2))
[[0, 1, 2], [0, 2]]
>>> nx.dag_longest_path(DG)
Expand Down Expand Up @@ -1078,7 +1078,7 @@ def dag_longest_path_length(G, weight="weight", default_weight=1):
Examples
--------
>>> DG = nx.DiGraph([(0, 1, {'cost':1}), (1, 2, {'cost':1}), (0, 2, {'cost':42})])
>>> DG = nx.DiGraph([(0, 1, {"cost": 1}), (1, 2, {"cost": 1}), (0, 2, {"cost": 42})])
>>> list(nx.all_simple_paths(DG, 0, 2))
[[0, 1, 2], [0, 2]]
>>> nx.dag_longest_path_length(DG)
Expand Down
12 changes: 3 additions & 9 deletions networkx/algorithms/flow/maxflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,7 @@ def maximum_flow(flowG, _s, _t, capacity="capacity", flow_func=None, **kwargs):
maximum flow by using the flow_func parameter.
>>> from networkx.algorithms.flow import shortest_augmenting_path
>>> flow_value == nx.maximum_flow(G, "x", "y", flow_func=shortest_augmenting_path)[
... 0
... ]
>>> flow_value == nx.maximum_flow(G, "x", "y", flow_func=shortest_augmenting_path)[0]
True
"""
Expand Down Expand Up @@ -281,9 +279,7 @@ def maximum_flow_value(flowG, _s, _t, capacity="capacity", flow_func=None, **kwa
maximum flow by using the flow_func parameter.
>>> from networkx.algorithms.flow import shortest_augmenting_path
>>> flow_value == nx.maximum_flow_value(
... G, "x", "y", flow_func=shortest_augmenting_path
... )
>>> flow_value == nx.maximum_flow_value(G, "x", "y", flow_func=shortest_augmenting_path)
True
"""
Expand Down Expand Up @@ -582,9 +578,7 @@ def minimum_cut_value(flowG, _s, _t, capacity="capacity", flow_func=None, **kwar
minimum cut by using the flow_func parameter.
>>> from networkx.algorithms.flow import shortest_augmenting_path
>>> cut_value == nx.minimum_cut_value(
... G, "x", "y", flow_func=shortest_augmenting_path
... )
>>> cut_value == nx.minimum_cut_value(G, "x", "y", flow_func=shortest_augmenting_path)
True
"""
Expand Down
8 changes: 2 additions & 6 deletions networkx/algorithms/graph_hashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,9 @@ def weisfeiler_lehman_subgraph_hashes(
Finding similar nodes in different graphs:
>>> G1 = nx.Graph()
>>> G1.add_edges_from([
... (1, 2), (2, 3), (2, 4), (3, 5), (4, 6), (5, 7), (6, 7)
... ])
>>> G1.add_edges_from([(1, 2), (2, 3), (2, 4), (3, 5), (4, 6), (5, 7), (6, 7)])
>>> G2 = nx.Graph()
>>> G2.add_edges_from([
... (1, 3), (2, 3), (1, 6), (1, 5), (4, 6)
... ])
>>> G2.add_edges_from([(1, 3), (2, 3), (1, 6), (1, 5), (4, 6)])
>>> g1_hashes = nx.weisfeiler_lehman_subgraph_hashes(G1, iterations=3, digest_size=8)
>>> g2_hashes = nx.weisfeiler_lehman_subgraph_hashes(G2, iterations=3, digest_size=8)
Expand Down
1 change: 0 additions & 1 deletion networkx/algorithms/isomorphism/matchhelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,6 @@ def generic_multiedge_match(attr, default, op):
>>> nm = generic_node_match("weight", 1.0, isclose)
>>> nm = generic_node_match("color", "red", eq)
>>> nm = generic_node_match(["weight", "color"], [1.0, "red"], [isclose, eq])
...
"""

Expand Down
8 changes: 2 additions & 6 deletions networkx/algorithms/isomorphism/temporalisomorphvf2.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ def __init__(self, G1, G2, temporal_attribute_name, delta):
>>> G2 = nx.Graph(nx.path_graph(4, create_using=nx.Graph()))
>>> GM = isomorphism.TimeRespectingGraphMatcher(
... G1, G2, "date", timedelta(days=1)
... )
>>> GM = isomorphism.TimeRespectingGraphMatcher(G1, G2, "date", timedelta(days=1))
"""
self.temporal_attribute_name = temporal_attribute_name
self.delta = delta
Expand Down Expand Up @@ -158,9 +156,7 @@ def __init__(self, G1, G2, temporal_attribute_name, delta):
>>> G2 = nx.DiGraph(nx.path_graph(4, create_using=nx.DiGraph()))
>>> GM = isomorphism.TimeRespectingDiGraphMatcher(
... G1, G2, "date", timedelta(days=1)
... )
>>> GM = isomorphism.TimeRespectingDiGraphMatcher(G1, G2, "date", timedelta(days=1))
"""
self.temporal_attribute_name = temporal_attribute_name
self.delta = delta
Expand Down
9 changes: 4 additions & 5 deletions networkx/algorithms/minors/contraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ def equivalence_classes(iterable, relation):
`X` and a function implementation of `R`.
>>> X = set(range(10))
>>> def mod3(x, y): return (x - y) % 3 == 0
>>> equivalence_classes(X, mod3) # doctest: +SKIP
>>> def mod3(x, y):
... return (x - y) % 3 == 0
>>> equivalence_classes(X, mod3) # doctest: +SKIP
{frozenset({1, 4, 7}), frozenset({8, 2, 5}), frozenset({0, 9, 3, 6})}
"""
# For simplicity of implementation, we initialize the return value as a
Expand Down Expand Up @@ -202,9 +203,7 @@ def quotient_graph(
are equivalent if they are not adjacent but have the same neighbor set.
>>> G = nx.complete_bipartite_graph(2, 3)
>>> same_neighbors = lambda u, v: (
... u not in G[v] and v not in G[u] and G[u] == G[v]
... )
>>> same_neighbors = lambda u, v: (u not in G[v] and v not in G[u] and G[u] == G[v])
>>> Q = nx.quotient_graph(G, same_neighbors)
>>> K2 = nx.complete_graph(2)
>>> nx.is_isomorphic(Q, K2)
Expand Down

0 comments on commit 3c0f096

Please sign in to comment.