summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.pre-commit-config.yaml4
-rw-r--r--CONTRIBUTING.rst7
-rw-r--r--README.rst12
-rw-r--r--doc/developer/nxeps/nxep-0003.rst71
-rw-r--r--requirements/developer.txt2
5 files changed, 55 insertions, 41 deletions
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index addbf14b..46ae1704 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -11,3 +11,7 @@ repos:
hooks:
- id: pyupgrade
args: [--py38-plus]
+- repo: https://github.com/asottile/blacken-docs
+ rev: v1.12.1
+ hooks:
+ - id: blacken-docs
diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst
index 58b036dd..0ad02d92 100644
--- a/CONTRIBUTING.rst
+++ b/CONTRIBUTING.rst
@@ -291,13 +291,14 @@ Guidelines
.. code-block:: python
- @nx.not_implemented_for('directed', 'multigraph')
+ @nx.not_implemented_for("directed", "multigraph")
def function_not_for_MultiDiGraph(G, others):
# function not for graphs that are directed *and* multigraph
pass
- @nx.not_implemented_for('directed')
- @nx.not_implemented_for('multigraph')
+
+ @nx.not_implemented_for("directed")
+ @nx.not_implemented_for("multigraph")
def function_only_for_Graph(G, others):
# function not for directed graphs *or* for multigraphs
pass
diff --git a/README.rst b/README.rst
index f28fb8a9..7f5224e6 100644
--- a/README.rst
+++ b/README.rst
@@ -27,15 +27,15 @@ Simple example
Find the shortest path between two nodes in an undirected graph:
-.. code:: python
+.. code:: pycon
>>> import networkx as nx
>>> G = nx.Graph()
- >>> G.add_edge('A', 'B', weight=4)
- >>> G.add_edge('B', 'D', weight=2)
- >>> G.add_edge('A', 'C', weight=3)
- >>> G.add_edge('C', 'D', weight=4)
- >>> nx.shortest_path(G, 'A', 'D', weight='weight')
+ >>> G.add_edge("A", "B", weight=4)
+ >>> G.add_edge("B", "D", weight=2)
+ >>> G.add_edge("A", "C", weight=3)
+ >>> G.add_edge("C", "D", weight=4)
+ >>> nx.shortest_path(G, "A", "D", weight="weight")
['A', 'B', 'D']
Install
diff --git a/doc/developer/nxeps/nxep-0003.rst b/doc/developer/nxeps/nxep-0003.rst
index 954991b3..22f3aa79 100644
--- a/doc/developer/nxeps/nxep-0003.rst
+++ b/doc/developer/nxeps/nxep-0003.rst
@@ -121,28 +121,28 @@ starting from an empty graph G:
.. code-block:: python
for C in edgelist:
- if len(C) == 1:
- if not hashable(C[-1]):
- G.graph.update(C[-1]) # C[-1] is a dict of graph attributes
- else:
- G.add_node(C[-1]) # C[-1] is a node
- elif len(C) == 2:
- if not hashable(C[-1]):
- G.add_node(C[0], **C[-1]) # C[-1] is a dict of node attributes
- else:
- G.add_edge(*C) # C is a node-pair indicating an edge
- elif len(C) == 3:
- if not hashable(C[-1]):
- G.add_edge(*C[:2], **C[-1]) # C -> (u, v, attrdict)
- else:
- G.add_edge(*C) # C -> (u, v, edge_key)
- elif len(C) == 4:
- assert not hashable(C[-1])
- G.add_edge(*C) # C -> (u, v, edge_key, attr_dict)
- else:
- raise NetworkXInvalidEdgelist(
- "no container in an edgelist should be larger than 4 objects."
- )
+ if len(C) == 1:
+ if not hashable(C[-1]):
+ G.graph.update(C[-1]) # C[-1] is a dict of graph attributes
+ else:
+ G.add_node(C[-1]) # C[-1] is a node
+ elif len(C) == 2:
+ if not hashable(C[-1]):
+ G.add_node(C[0], **C[-1]) # C[-1] is a dict of node attributes
+ else:
+ G.add_edge(*C) # C is a node-pair indicating an edge
+ elif len(C) == 3:
+ if not hashable(C[-1]):
+ G.add_edge(*C[:2], **C[-1]) # C -> (u, v, attrdict)
+ else:
+ G.add_edge(*C) # C -> (u, v, edge_key)
+ elif len(C) == 4:
+ assert not hashable(C[-1])
+ G.add_edge(*C) # C -> (u, v, edge_key, attr_dict)
+ else:
+ raise NetworkXInvalidEdgelist(
+ "no container in an edgelist should be larger than 4 objects."
+ )
Usage and Impact
----------------
@@ -181,7 +181,7 @@ has underlying code that yields from an edgelist, or returns a graph.
@graph_builder
@py_random_state(4)
- def extended_barabasi_albert_graph(n, m, p, q, seed=None)
+ def extended_barabasi_albert_graph(n, m, p, q, seed=None):
# some fancy code that requires we construct G to use graph properties
# while we decide what edges to add next.
return G
@@ -249,16 +249,21 @@ Later generators could use `@node_and_edge_builder`.
@wraps(f)
def graph(*args, **kwargs):
return nx.Graph(f(*args, **kwargs))
+
def digraph(*args, **kwargs):
return nx.DiGraph(f(*args, **kwargs))
+
def multigraph(*args, **kwargs):
return nx.MultiGraph(f(*args, **kwargs))
+
def multidigraph(*args, **kwargs):
return nx.MultiDiGraph(f(*args, **kwargs))
+
def custom_graph(*args, create_using=None, **kwargs):
g = create_using()
g.update(f(*args, **kwargs))
return g
+
graph.Graph = graph
graph.DiGraph = digraph
graph.MultiGraph = multigraph
@@ -267,23 +272,28 @@ Later generators could use `@node_and_edge_builder`.
graph.edgelist = f
return graph
+
def graph_builder(f):
@wraps(f)
def edgelist(*args, **kwargs):
g = f(*args, **kwargs)
- return itertools.ichain(
- map(tuple, G.nodes.data()), map(tuple, G.edges.data())
- )
+ return itertools.ichain(map(tuple, G.nodes.data()), map(tuple, G.edges.data()))
+
f.edgelist = edgelist
f.CustomGraph = f
+
def graph(*args, **kwargs):
- return f(*args, create_using=nx.Graph, **kwargs))
+ return f(*args, create_using=nx.Graph, **kwargs)
+
def digraph(*args, **kwargs):
- return f(*args, create_using=nx.DiGraph, **kwargs))
+ return f(*args, create_using=nx.DiGraph, **kwargs)
+
def multigraph(*args, **kwargs):
- return f(*args, create_using=nx.MultiGraph, **kwargs))
+ return f(*args, create_using=nx.MultiGraph, **kwargs)
+
def multidigraph(*args, **kwargs):
- return f(*args, create_using=nx.MultiDiGraph, **kwargs))
+ return f(*args, create_using=nx.MultiDiGraph, **kwargs)
+
f.Graph = graph
f.DiGraph = digraph
f.MultiGraph = multigraph
@@ -308,6 +318,7 @@ Example developer usage:
"""an overly simplified path graph implementation"""
return pairwise(range(n))
+
@graph_builder
def complete_graph(n, create_using=None):
"""an overly simplified complete graph implementation"""
diff --git a/requirements/developer.txt b/requirements/developer.txt
index 49cb06a8..3cf72967 100644
--- a/requirements/developer.txt
+++ b/requirements/developer.txt
@@ -1,4 +1,2 @@
-black==22.3.0
-pyupgrade>=2.31
pre-commit>=2.18
mypy>=0.942