Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format code with black #316

Merged
merged 8 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/setup-python@v2
with:
python-version: 3.8
- run: pip install -U flake8
- run: pip install -U flake8 black==21.5b0
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
Expand All @@ -26,6 +26,8 @@ jobs:
run: cargo fmt -- --check
- name: Clippy
run: cargo clippy -- -D warnings
- name: Black Codestyle Format
run: black --check --diff retworkx tests
- name: Python Lint
run: flake8 setup.py tests
tests:
Expand Down
7 changes: 5 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ cargo clippy

Python is used primarily for tests and some small pieces of packaging
and namespace configuration code in the actual library.
[flake8](https://flake8.pycqa.org/en/latest/) is used to enforce consistent
style in the python code in the repository. You can run it via tox using:
[black](https://github.com/psf/black) and [flake8](https://flake8.pycqa.org/en/latest/) are used to enforce consistent
style in the python code in the repository. You can run them via tox using:

```bash
tox -elint
Expand All @@ -116,6 +116,9 @@ tox -elint
This will also run `cargo fmt` in check mode to ensure that you ran `cargo fmt`
and will fail if the Rust code doesn't conform to the style rules.

If black returns a code formatting error you can run `tox -eblack` to automatically
update the code formatting to conform to the style.

### Building documentation

Just like with tests building documentation is done via tox. This will handle
Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
[build-system]
requires = ["setuptools", "wheel", "setuptools-rust"]

[tool.black]
line-length = 80
target-version = ['py36', 'py37', 'py38', 'py39']
168 changes: 107 additions & 61 deletions retworkx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import functools

from .retworkx import *
sys.modules['retworkx.generators'] = generators

sys.modules["retworkx.generators"] = generators


class PyDAG(PyDiGraph):
Expand Down Expand Up @@ -82,6 +83,7 @@ class PyDAG(PyDiGraph):
the same time, leveraging :meth:`PyDAG.add_child` or
:meth:`PyDAG.add_parent` will avoid this overhead.
"""

pass


Expand Down Expand Up @@ -114,11 +116,14 @@ def distance_matrix(graph, parallel_threshold=300):


@distance_matrix.register(PyDiGraph)
def _digraph_distance_matrix(graph, parallel_threshold=300,
as_undirected=False):
return digraph_distance_matrix(graph,
parallel_threshold=parallel_threshold,
as_undirected=as_undirected)
def _digraph_distance_matrix(
graph, parallel_threshold=300, as_undirected=False
):
return digraph_distance_matrix(
graph,
parallel_threshold=parallel_threshold,
as_undirected=as_undirected,
)


@distance_matrix.register(PyGraph)
Expand Down Expand Up @@ -160,14 +165,16 @@ def adjacency_matrix(graph, weight_fn=None, default_weight=1.0):

@adjacency_matrix.register(PyDiGraph)
def _digraph_adjacency_matrix(graph, weight_fn=None, default_weight=1.0):
return digraph_adjacency_matrix(graph, weight_fn=weight_fn,
default_weight=default_weight)
return digraph_adjacency_matrix(
graph, weight_fn=weight_fn, default_weight=default_weight
)


@adjacency_matrix.register(PyGraph)
def _graph_adjacency_matrix(graph, weight_fn=None, default_weight=1.0):
return graph_adjacency_matrix(graph, weight_fn=weight_fn,
default_weight=default_weight)
return graph_adjacency_matrix(
graph, weight_fn=weight_fn, default_weight=default_weight
)


@functools.singledispatch
Expand Down Expand Up @@ -195,19 +202,24 @@ def all_simple_paths(graph, from_, to, min_depth=None, cutoff=None):

@all_simple_paths.register(PyDiGraph)
def _digraph_all_simple_paths(graph, from_, to, min_depth=None, cutoff=None):
return digraph_all_simple_paths(graph, from_, to, min_depth=min_depth,
cutoff=cutoff)
return digraph_all_simple_paths(
graph, from_, to, min_depth=min_depth, cutoff=cutoff
)


@all_simple_paths.register(PyGraph)
def _graph_all_simple_paths(graph, from_, to, min_depth=None, cutoff=None):
return graph_all_simple_paths(graph, from_, to, min_depth=min_depth,
cutoff=cutoff)
return graph_all_simple_paths(
graph, from_, to, min_depth=min_depth, cutoff=cutoff
)


@functools.singledispatch
def floyd_warshall_numpy(
graph, weight_fn=None, default_weight=1.0, parallel_threshold=300,
graph,
weight_fn=None,
default_weight=1.0,
parallel_threshold=300,
):
"""Find all-pairs shortest path lengths using Floyd's algorithm

Expand Down Expand Up @@ -303,22 +315,32 @@ def astar_shortest_path(graph, node, goal_fn, edge_cost_fn, estimate_cost_fn):


@astar_shortest_path.register(PyDiGraph)
def _digraph_astar_shortest_path(graph, node, goal_fn, edge_cost_fn,
estimate_cost_fn):
return digraph_astar_shortest_path(graph, node, goal_fn, edge_cost_fn,
estimate_cost_fn)
def _digraph_astar_shortest_path(
graph, node, goal_fn, edge_cost_fn, estimate_cost_fn
):
return digraph_astar_shortest_path(
graph, node, goal_fn, edge_cost_fn, estimate_cost_fn
)


@astar_shortest_path.register(PyGraph)
def _graph_astar_shortest_path(graph, node, goal_fn, edge_cost_fn,
estimate_cost_fn):
return graph_astar_shortest_path(graph, node, goal_fn, edge_cost_fn,
estimate_cost_fn)
def _graph_astar_shortest_path(
graph, node, goal_fn, edge_cost_fn, estimate_cost_fn
):
return graph_astar_shortest_path(
graph, node, goal_fn, edge_cost_fn, estimate_cost_fn
)


@functools.singledispatch
def dijkstra_shortest_paths(graph, source, target=None, weight_fn=None,
default_weight=1.0, as_undirected=False):
def dijkstra_shortest_paths(
graph,
source,
target=None,
weight_fn=None,
default_weight=1.0,
as_undirected=False,
):
"""Find the shortest path from a node

This function will generate the shortest path from a source node using
Expand All @@ -345,20 +367,35 @@ def dijkstra_shortest_paths(graph, source, target=None, weight_fn=None,


@dijkstra_shortest_paths.register(PyDiGraph)
def _digraph_dijkstra_shortest_path(graph, source, target=None, weight_fn=None,
default_weight=1.0, as_undirected=False):
return digraph_dijkstra_shortest_paths(graph, source, target=target,
weight_fn=weight_fn,
default_weight=default_weight,
as_undirected=as_undirected)
def _digraph_dijkstra_shortest_path(
graph,
source,
target=None,
weight_fn=None,
default_weight=1.0,
as_undirected=False,
):
return digraph_dijkstra_shortest_paths(
graph,
source,
target=target,
weight_fn=weight_fn,
default_weight=default_weight,
as_undirected=as_undirected,
)


@dijkstra_shortest_paths.register(PyGraph)
def _graph_dijkstra_shortest_path(graph, source, target=None, weight_fn=None,
default_weight=1.0):
return graph_dijkstra_shortest_paths(graph, source, target=target,
weight_fn=weight_fn,
default_weight=default_weight)
def _graph_dijkstra_shortest_path(
graph, source, target=None, weight_fn=None, default_weight=1.0
):
return graph_dijkstra_shortest_paths(
graph,
source,
target=target,
weight_fn=weight_fn,
default_weight=default_weight,
)


@functools.singledispatch
Expand Down Expand Up @@ -387,17 +424,19 @@ def dijkstra_shortest_path_lengths(graph, node, edge_cost_fn, goal=None):


@dijkstra_shortest_path_lengths.register(PyDiGraph)
def _digraph_dijkstra_shortest_path_lengths(graph, node, edge_cost_fn,
goal=None):
return digraph_dijkstra_shortest_path_lengths(graph, node, edge_cost_fn,
goal=goal)
def _digraph_dijkstra_shortest_path_lengths(
graph, node, edge_cost_fn, goal=None
):
return digraph_dijkstra_shortest_path_lengths(
graph, node, edge_cost_fn, goal=goal
)


@dijkstra_shortest_path_lengths.register(PyGraph)
def _graph_dijkstra_shortest_path_lengths(graph, node, edge_cost_fn,
goal=None):
return graph_dijkstra_shortest_path_lengths(graph, node, edge_cost_fn,
goal=goal)
def _graph_dijkstra_shortest_path_lengths(graph, node, edge_cost_fn, goal=None):
return graph_dijkstra_shortest_path_lengths(
graph, node, edge_cost_fn, goal=goal
)


@functools.singledispatch
Expand Down Expand Up @@ -427,14 +466,14 @@ def k_shortest_path_lengths(graph, start, k, edge_cost, goal=None):

@k_shortest_path_lengths.register(PyDiGraph)
def _digraph_k_shortest_path_lengths(graph, start, k, edge_cost, goal=None):
return digraph_k_shortest_path_lengths(graph, start, k, edge_cost,
goal=goal)
return digraph_k_shortest_path_lengths(
graph, start, k, edge_cost, goal=goal
)


@k_shortest_path_lengths.register(PyGraph)
def _graph_k_shortest_path_lengths(graph, start, k, edge_cost, goal=None):
return graph_k_shortest_path_lengths(graph, start, k, edge_cost,
goal=goal)
return graph_k_shortest_path_lengths(graph, start, k, edge_cost, goal=goal)


@functools.singledispatch
Expand Down Expand Up @@ -467,8 +506,9 @@ def _graph_dfs_edges(graph, source):


@functools.singledispatch
def is_isomorphic(first, second, node_matcher=None, edge_matcher=None,
id_order=True):
def is_isomorphic(
first, second, node_matcher=None, edge_matcher=None, id_order=True
):
"""Determine if 2 graphs are isomorphic

This checks if 2 graphs are isomorphic both structurally and also
Expand Down Expand Up @@ -514,17 +554,21 @@ def is_isomorphic(first, second, node_matcher=None, edge_matcher=None,


@is_isomorphic.register(PyDiGraph)
def _digraph_is_isomorphic(first, second, node_matcher=None,
edge_matcher=None, id_order=True):
return digraph_is_isomorphic(first, second, node_matcher,
edge_matcher, id_order)
def _digraph_is_isomorphic(
first, second, node_matcher=None, edge_matcher=None, id_order=True
):
return digraph_is_isomorphic(
first, second, node_matcher, edge_matcher, id_order
)


@is_isomorphic.register(PyGraph)
def _graph_is_isomorphic(first, second, node_matcher=None,
edge_matcher=None, id_order=True):
return graph_is_isomorphic(first, second, node_matcher,
edge_matcher, id_order)
def _graph_is_isomorphic(
first, second, node_matcher=None, edge_matcher=None, id_order=True
):
return graph_is_isomorphic(
first, second, node_matcher, edge_matcher, id_order
)


@functools.singledispatch
Expand Down Expand Up @@ -720,7 +764,9 @@ def networkx_converter(graph):
nodes = list(graph.nodes)
node_indices = dict(zip(nodes, new_graph.add_nodes_from(nodes)))
new_graph.add_edges_from(
[(node_indices[x[0]],
node_indices[x[1]],
x[2]) for x in graph.edges(data=True)])
[
(node_indices[x[0]], node_indices[x[1]], x[2])
for x in graph.edges(data=True)
]
)
return new_graph
Loading