Skip to content

Commit

Permalink
switched operator overloading to singledispatch (see issue #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxfischer2781 committed Oct 17, 2018
1 parent e4f471a commit 29537a0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
8 changes: 6 additions & 2 deletions graphi/operators/_density.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from __future__ import division
from . import interface

try:
from singledispatch import singledispatch
except ImportError:
from functools import singledispatch

@interface.graph_operator

@singledispatch
def density(graph):
"""
Return the density of the graph, i.e. the connectedness of its nodes
Expand Down
7 changes: 5 additions & 2 deletions graphi/operators/_neighbours.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from . import interface
try:
from singledispatch import singledispatch
except ImportError:
from functools import singledispatch


@interface.graph_operator
@singledispatch
def neighbours(graph, node, maximum_distance=None):
"""
Yield all nodes to which there is an outgoing edge from ``node`` in ``graph``
Expand Down
18 changes: 11 additions & 7 deletions graphi_unittests/operators_unittests/test_neighbours.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,19 @@ def test_operator(self):
)
self.assertEqual(set(neighbours(graph, 'node', float('-inf'))), set())

def test_method(self):
def test_override(self):
"""Overridden neighbours(graph, node, ...)"""
class NeighboursGraph(adjacency_graph.AdjacencyGraph):
def __graphi_neighbours__(self, node, maximum_distance=None):
if maximum_distance is None:
return range(len(self[node]))
if maximum_distance < 0:
return []
return range(int(maximum_distance))
pass

@neighbours.register(NeighboursGraph)
def test_neighbours(self, node, maximum_distance=None):
if maximum_distance is None:
return range(len(self[node]))
if maximum_distance < 0:
return []
return range(int(maximum_distance))

for edges in self._get_edge_values():
with self.subTest(edges=edges):
graph = NeighboursGraph(
Expand Down
8 changes: 5 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
)
] if platform.python_implementation() == 'CPython' else []

install_requires = ['six']
if sys.version_info < (3, 4):
install_requires.append('singledispatch')

if __name__ == '__main__':
setup(
name=package_about['__title__'],
Expand All @@ -34,9 +38,7 @@
url=package_about['__url__'],
packages=find_packages(),
# dependencies
install_requires=[
'six',
],
install_requires=install_requires,
ext_modules=CEXTENSIONS,
zip_safe=False,
# metadata for package search
Expand Down

0 comments on commit 29537a0

Please sign in to comment.