Skip to content

Commit

Permalink
fix flake8
Browse files Browse the repository at this point in the history
discovered a few real errors in the tests doing this!

Also setup isort to play nice with black
  • Loading branch information
ianhi committed Feb 3, 2022
1 parent 816493c commit db27831
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 44 deletions.
9 changes: 8 additions & 1 deletion ipycytoscape/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
# The full license is in the file LICENSE, distributed with this software.

from ._version import __version__, version_info
from .cytoscape import *
from .cytoscape import (
MONITORED_USER_INTERACTIONS,
MONITORED_USER_TYPES,
CytoscapeWidget,
Edge,
Graph,
Node,
)
from .nbextension import _jupyter_nbextension_paths

npm_pkg_name = "jupyter-cytoscape"
Expand Down
29 changes: 17 additions & 12 deletions ipycytoscape/cytoscape.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,17 @@
"vmousedown", # alias for 'tapstart'
"tapdrag", # normalised move event (either touchmove or mousemove)
"vmousemove", # alias for 'tapdrag'
"tapdragover", # normalised over element event (either touchmove or mousemove/mouseover)
"tapdragout", # normalised off of element event (either touchmove or mousemove/mouseout)
"tapdragover", # normalised over element event (either touchmove or mousemove/mouseover) # noqa
"tapdragout", # normalised off of element event (either touchmove or mousemove/mouseout) # noqa
"tapend", # normalised tap end event (either mouseup or touchend)
"vmouseup", # alias for 'tapend'
"tap", # normalised tap event (either click, or touchstart followed by touchend without touchmove)
"tap", # normalised tap event (either click, or touchstart followed by touchend without touchmove) # noqa
"vclick", # alias for 'tap'
"taphold", # normalised tap hold event
"cxttapstart", # normalised right-click mousedown or two-finger tapstart
"cxttapend", # normalised right-click mouseup or two-finger tapend
"cxttap", # normalised right-click or two-finger tap
"cxtdrag", # normalised mousemove or two-finger drag after cxttapstart but before cxttapend
"cxtdrag", # normalised mousemove or two-finger drag after cxttapstart but before cxttapend # noqa
"cxtdragover", # when going over a node via cxtdrag
"cxtdragout", # when going off a node via cxtdrag
"boxstart", # when starting box selection
Expand Down Expand Up @@ -335,8 +335,9 @@ def add_edge(self, edge, directed=False, multiple_edges=False):

def add_edges(self, edges, directed=False, multiple_edges=False):
"""
Appends edges from the end of the list. If either the source or target Node of an
Edge is not already in the graph it will be created and added to he Nodes list.
Appends edges from the end of the list. If either the source or target Node
of an Edge is not already in the graph it will be created and added to
the Nodes list.
Parameters
----------
Expand All @@ -354,13 +355,16 @@ def add_edges(self, edges, directed=False, multiple_edges=False):
if multiple_edges and "multiple_edges" not in edge.classes:
edge.classes += " multiple_edges "

# If multiple edges are allowed, it's okay to add more edges between the source and target
# If multiple edges are allowed, it's okay to add more
# edges between the source and target
if multiple_edges:
new_edge = True
# Check to see if the edge source -> target exists in the graph (don't add it again)
# Check to see if the edge source -> target exists in the graph
# If it does then don't add it again
elif source in self._adj and target in self._adj[source]:
new_edge = False
# Check to see if the edge target-> source exists in an undirected graph (don't add it again)
# Check to see if the edge target-> source exists in an
# undirected graph (don't add it again)
elif not directed and target in self._adj and source in self._adj[target]:
new_edge = False
# If the edge doesn't exist already
Expand Down Expand Up @@ -419,7 +423,8 @@ def remove_edge(self, edge):
self._adj[target][source] -= 1
except ValueError:
raise ValueError(
f"Edge from {edge.data['source']} to {edge.data['target']} is not present in the graph."
f"Edge from {edge.data['source']} to {edge.data['target']} "
"is not present in the graph."
)

def remove_edge_by_id(self, source_id, target_id):
Expand Down Expand Up @@ -717,7 +722,7 @@ def create_tooltip(node_attributes, node_labels):
node_attributes = convert_types_to_string(node_attributes)

# create tooltip text string
if not "tooltip" in node_attributes:
if "tooltip" not in node_attributes:
tooltip_text = create_tooltip(node_attributes, node.labels)
node_attributes["tooltip"] = tooltip_text

Expand Down Expand Up @@ -750,7 +755,7 @@ def create_tooltip(node_attributes, node_labels):
rel_attributes = convert_types_to_string(rel_attributes)

# assign name of the relationship
if not "name" in rel_attributes:
if "name" not in rel_attributes:
rel_attributes["name"] = rel.__class__.__name__

# assign unique node ids
Expand Down
19 changes: 19 additions & 0 deletions ipycytoscape/tests/_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from typing import List

from ipycytoscape.cytoscape import Edge, Node


def compare_nodes(expected_nodes: List[Node], actual_nodes: List[Node]):
# if one list is empty
assert bool(expected_nodes) == bool(actual_nodes)
for expected, actual in zip(expected_nodes, actual_nodes):
assert expected.data == actual.data
assert expected.classes == actual.classes
assert expected.position == actual.position


def compare_edges(expected_edges: List[Edge], actual_edges: List[Edge]):
assert bool(expected_edges) == bool(actual_edges)
for expected, actual in zip(expected_edges, actual_edges):
assert expected.data == actual.data
assert expected.classes == actual.classes
13 changes: 1 addition & 12 deletions ipycytoscape/tests/test_graph_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,7 @@

from ipycytoscape.cytoscape import Edge, Graph, Node


def compare_nodes(expected_nodes, actual_nodes):
for expected, actual in zip(expected_nodes, actual_nodes):
assert expected.data == actual.data
assert expected.classes == actual.classes
assert expected.position == actual.position


def compare_edges(expected_edges, actual_edges):
for expected, actual in zip(expected_edges, actual_edges):
assert expected.data == actual.data
assert expected.classes == actual.classes
from ._util import compare_edges, compare_nodes


class TestNetworkx:
Expand Down
6 changes: 6 additions & 0 deletions ipycytoscape/tests/test_graph_deletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from ipycytoscape.cytoscape import Graph, Node

from ._util import compare_nodes


class TestNetworkx:
def test_lonely_nodes(self):
Expand All @@ -34,6 +36,7 @@ def test_lonely_nodes(self):
Node(data={"id": "unconnected_node"}, position={}),
]

compare_nodes(expected_nodes, graph.nodes)
# remove individual node using node as input
graph.remove_node(graph.nodes[0])

Expand All @@ -44,6 +47,7 @@ def test_lonely_nodes(self):
Node(data={"id": "4"}, position={}),
Node(data={"id": "unconnected_node"}, position={}),
]
compare_nodes(expected_nodes, graph.nodes)

# remove individual node using index node as input
graph.remove_node_by_id("3")
Expand All @@ -54,6 +58,8 @@ def test_lonely_nodes(self):
Node(data={"id": "4"}, position={}),
Node(data={"id": "unconnected_node"}, position={}),
]
compare_nodes(expected_nodes, graph.nodes)

# remove all nodes of the graph
graph.clear()
compare_nodes([], graph.nodes)
22 changes: 5 additions & 17 deletions ipycytoscape/tests/test_graph_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,7 @@

from ipycytoscape.cytoscape import Edge, Graph, Node


def compare_nodes(expected_nodes, actual_nodes):
# if one list is empty
assert bool(expected_nodes) == bool(actual_nodes)
for expected, actual in zip(expected_nodes, actual_nodes):
assert expected.data == actual.data
assert expected.classes == actual.classes
assert expected.position == actual.position


def compare_edges(expected_edges, actual_edges):
assert bool(expected_edges) == bool(actual_edges)
for expected, actual in zip(expected_edges, actual_edges):
assert expected.data == actual.data
assert expected.classes == actual.classes
from ._util import compare_edges, compare_nodes


@pytest.fixture(name="edges", scope="function")
Expand Down Expand Up @@ -264,7 +250,8 @@ def test_add_node(self):

def test_add_edges(self, edges):
"""
Test to ensure that edges with the corresponding nodes will be added to the graph
Test to ensure that edges with the corresponding nodes will be
added to the graph.
"""

expected_nodes = [
Expand Down Expand Up @@ -336,7 +323,8 @@ def test_add_edges_multiple_edges(self, edges):

def test_add_edges_2(self):
"""
Test to ensure that an edge with the corresponding nodes will be added to the graph
Test to ensure that an edge with the corresponding nodes will be
added to the graph.
"""
edge = Edge(data={"source": "0", "target": "1"})
edge_inv = Edge(data={"source": "1", "target": "0"})
Expand Down
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[build-system]
requires = ["jupyter_packaging~=0.7.0", "jupyterlab>=3.0.0rc4,==3.*", "setuptools>=40.8.0", "wheel"]
build-backend = "setuptools.build_meta"


[tool.isort]
profile = "black"
src_paths = "ipycytoscape"
multi_line_output = 3
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ universal=1
license_file = LICENSE

[flake8]
exclude = docs, _version.py, .eggs, example
exclude = docs/*, _version.py, .eggs, example/*
max-line-length = 88
docstring-convention = "numpy"
extend-ignore =
# See https://github.com/PyCQA/pycodestyle/issues/373
E203,
E203, E302, E303
per-file-ignores =
ipycytoscape/__init__.py:F401
ipycytoscape/nbextension/__init__.py:F401
Expand Down

0 comments on commit db27831

Please sign in to comment.