Use *list instead of list for initial_dependants

This commit is contained in:
Christophe Bedard 2019-08-04 10:39:09 +02:00
parent 9e717a5f5d
commit 422a420637
2 changed files with 15 additions and 14 deletions

View file

@ -55,7 +55,7 @@ class TestDependencySolver(unittest.TestCase):
depone_instance = DepOne()
# DepEmtpy should be added before
solution = DependencySolver([depone_instance]).solve()
solution = DependencySolver(depone_instance).solve()
self.assertEqual(len(solution), 2, 'solution length invalid')
self.assertIsInstance(solution[0], DepEmtpy)
self.assertIs(solution[1], depone_instance)
@ -65,13 +65,13 @@ class TestDependencySolver(unittest.TestCase):
depone_instance = DepOne()
# Already in order
solution = DependencySolver([depempty_instance, depone_instance]).solve()
solution = DependencySolver(depempty_instance, depone_instance).solve()
self.assertEqual(len(solution), 2, 'solution length invalid')
self.assertIs(solution[0], depempty_instance, 'wrong solution order')
self.assertIs(solution[1], depone_instance, 'wrong solution order')
# Out of order
solution = DependencySolver([depone_instance, depempty_instance]).solve()
solution = DependencySolver(depone_instance, depempty_instance).solve()
self.assertEqual(len(solution), 2, 'solution length invalid')
self.assertIs(solution[0], depempty_instance, 'solution does not use existing instance')
self.assertIs(solution[1], depone_instance, 'solution does not use existing instance')
@ -80,7 +80,7 @@ class TestDependencySolver(unittest.TestCase):
deptwo_instance = DepTwo()
# DepOne and DepOne2 both depend on DepEmpty
solution = DependencySolver([deptwo_instance]).solve()
solution = DependencySolver(deptwo_instance).solve()
self.assertEqual(len(solution), 4, 'solution length invalid')
self.assertIsInstance(solution[0], DepEmtpy)
self.assertIsInstance(solution[1], DepOne)
@ -89,7 +89,7 @@ class TestDependencySolver(unittest.TestCase):
# Existing instance of DepEmpty, in order
depempty_instance = DepEmtpy()
solution = DependencySolver([depempty_instance, deptwo_instance]).solve()
solution = DependencySolver(depempty_instance, deptwo_instance).solve()
self.assertEqual(len(solution), 4, 'solution length invalid')
self.assertIsInstance(solution[0], DepEmtpy)
self.assertIsInstance(solution[1], DepOne)
@ -97,7 +97,7 @@ class TestDependencySolver(unittest.TestCase):
self.assertIs(solution[3], deptwo_instance)
# Existing instance of DepEmpty, not in order
solution = DependencySolver([deptwo_instance, depempty_instance]).solve()
solution = DependencySolver(deptwo_instance, depempty_instance).solve()
self.assertEqual(len(solution), 4, 'solution length invalid')
self.assertIsInstance(solution[0], DepEmtpy)
self.assertIsInstance(solution[1], DepOne)
@ -108,7 +108,7 @@ class TestDependencySolver(unittest.TestCase):
depone_instance = DepOne()
# Pass parameter and check that the new instance has it
solution = DependencySolver([depone_instance], myparam='myvalue').solve()
solution = DependencySolver(depone_instance, myparam='myvalue').solve()
self.assertEqual(len(solution), 2, 'solution length invalid')
self.assertEqual(solution[0].myparam, 'myvalue', 'parameter not passed on')

View file

@ -156,7 +156,7 @@ class DependencySolver():
def __init__(
self,
initial_dependants: List[Dependant],
*initial_dependants: Dependant,
**kwargs,
) -> None:
"""
@ -165,7 +165,7 @@ class DependencySolver():
:param initial_dependants: the initial dependant instances, in order
:param kwargs: the parameters to pass on to new instances
"""
self._initial_deps = initial_dependants
self._initial_deps = list(initial_dependants)
self._kwargs = kwargs
def solve(self) -> List[Dependant]:
@ -241,10 +241,10 @@ class Processor():
Constructor.
:param handlers: the `EventHandler`s to use for processing
:param kwargs: the parameters to pass on to new handlers
"""
self._handlers = list(handlers)
print('handlers before:', [type(handler).__name__ for handler in self._handlers])
self._handlers = self._expand_dependencies(self._handlers, **kwargs)
print('handlers before:', [type(handler).__name__ for handler in handlers])
self._handlers = self._expand_dependencies(*handlers, **kwargs)
print('handlers after:', [type(handler).__name__ for handler in self._handlers])
self._register_with_handlers()
input()
@ -256,15 +256,16 @@ class Processor():
def _expand_dependencies(
self,
handlers: List[EventHandler],
*handlers: EventHandler,
**kwargs,
) -> List[EventHandler]:
"""
Check handlers and add dependencies if not included.
:param handlers: the list of primary `EventHandler`s
:param kwargs: the parameters to pass on to new instances
"""
return DependencySolver(handlers, **kwargs).solve()
return DependencySolver(*handlers, **kwargs).solve()
def _get_handler_maps(self) -> HandlerMultimap:
"""