Use *list instead of list for initial_dependants
This commit is contained in:
parent
9e717a5f5d
commit
422a420637
2 changed files with 15 additions and 14 deletions
|
@ -55,7 +55,7 @@ class TestDependencySolver(unittest.TestCase):
|
||||||
depone_instance = DepOne()
|
depone_instance = DepOne()
|
||||||
|
|
||||||
# DepEmtpy should be added before
|
# DepEmtpy should be added before
|
||||||
solution = DependencySolver([depone_instance]).solve()
|
solution = DependencySolver(depone_instance).solve()
|
||||||
self.assertEqual(len(solution), 2, 'solution length invalid')
|
self.assertEqual(len(solution), 2, 'solution length invalid')
|
||||||
self.assertIsInstance(solution[0], DepEmtpy)
|
self.assertIsInstance(solution[0], DepEmtpy)
|
||||||
self.assertIs(solution[1], depone_instance)
|
self.assertIs(solution[1], depone_instance)
|
||||||
|
@ -65,13 +65,13 @@ class TestDependencySolver(unittest.TestCase):
|
||||||
depone_instance = DepOne()
|
depone_instance = DepOne()
|
||||||
|
|
||||||
# Already in order
|
# 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.assertEqual(len(solution), 2, 'solution length invalid')
|
||||||
self.assertIs(solution[0], depempty_instance, 'wrong solution order')
|
self.assertIs(solution[0], depempty_instance, 'wrong solution order')
|
||||||
self.assertIs(solution[1], depone_instance, 'wrong solution order')
|
self.assertIs(solution[1], depone_instance, 'wrong solution order')
|
||||||
|
|
||||||
# Out of 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.assertEqual(len(solution), 2, 'solution length invalid')
|
||||||
self.assertIs(solution[0], depempty_instance, 'solution does not use existing instance')
|
self.assertIs(solution[0], depempty_instance, 'solution does not use existing instance')
|
||||||
self.assertIs(solution[1], depone_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()
|
deptwo_instance = DepTwo()
|
||||||
|
|
||||||
# DepOne and DepOne2 both depend on DepEmpty
|
# 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.assertEqual(len(solution), 4, 'solution length invalid')
|
||||||
self.assertIsInstance(solution[0], DepEmtpy)
|
self.assertIsInstance(solution[0], DepEmtpy)
|
||||||
self.assertIsInstance(solution[1], DepOne)
|
self.assertIsInstance(solution[1], DepOne)
|
||||||
|
@ -89,7 +89,7 @@ class TestDependencySolver(unittest.TestCase):
|
||||||
|
|
||||||
# Existing instance of DepEmpty, in order
|
# Existing instance of DepEmpty, in order
|
||||||
depempty_instance = DepEmtpy()
|
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.assertEqual(len(solution), 4, 'solution length invalid')
|
||||||
self.assertIsInstance(solution[0], DepEmtpy)
|
self.assertIsInstance(solution[0], DepEmtpy)
|
||||||
self.assertIsInstance(solution[1], DepOne)
|
self.assertIsInstance(solution[1], DepOne)
|
||||||
|
@ -97,7 +97,7 @@ class TestDependencySolver(unittest.TestCase):
|
||||||
self.assertIs(solution[3], deptwo_instance)
|
self.assertIs(solution[3], deptwo_instance)
|
||||||
|
|
||||||
# Existing instance of DepEmpty, not in order
|
# 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.assertEqual(len(solution), 4, 'solution length invalid')
|
||||||
self.assertIsInstance(solution[0], DepEmtpy)
|
self.assertIsInstance(solution[0], DepEmtpy)
|
||||||
self.assertIsInstance(solution[1], DepOne)
|
self.assertIsInstance(solution[1], DepOne)
|
||||||
|
@ -108,7 +108,7 @@ class TestDependencySolver(unittest.TestCase):
|
||||||
depone_instance = DepOne()
|
depone_instance = DepOne()
|
||||||
|
|
||||||
# Pass parameter and check that the new instance has it
|
# 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(len(solution), 2, 'solution length invalid')
|
||||||
self.assertEqual(solution[0].myparam, 'myvalue', 'parameter not passed on')
|
self.assertEqual(solution[0].myparam, 'myvalue', 'parameter not passed on')
|
||||||
|
|
||||||
|
|
|
@ -156,7 +156,7 @@ class DependencySolver():
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
initial_dependants: List[Dependant],
|
*initial_dependants: Dependant,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""
|
"""
|
||||||
|
@ -165,7 +165,7 @@ class DependencySolver():
|
||||||
:param initial_dependants: the initial dependant instances, in order
|
:param initial_dependants: the initial dependant instances, in order
|
||||||
:param kwargs: the parameters to pass on to new instances
|
:param kwargs: the parameters to pass on to new instances
|
||||||
"""
|
"""
|
||||||
self._initial_deps = initial_dependants
|
self._initial_deps = list(initial_dependants)
|
||||||
self._kwargs = kwargs
|
self._kwargs = kwargs
|
||||||
|
|
||||||
def solve(self) -> List[Dependant]:
|
def solve(self) -> List[Dependant]:
|
||||||
|
@ -241,10 +241,10 @@ class Processor():
|
||||||
Constructor.
|
Constructor.
|
||||||
|
|
||||||
:param handlers: the `EventHandler`s to use for processing
|
: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 handlers])
|
||||||
print('handlers before:', [type(handler).__name__ for handler in self._handlers])
|
self._handlers = self._expand_dependencies(*handlers, **kwargs)
|
||||||
self._handlers = self._expand_dependencies(self._handlers, **kwargs)
|
|
||||||
print('handlers after:', [type(handler).__name__ for handler in self._handlers])
|
print('handlers after:', [type(handler).__name__ for handler in self._handlers])
|
||||||
self._register_with_handlers()
|
self._register_with_handlers()
|
||||||
input()
|
input()
|
||||||
|
@ -256,15 +256,16 @@ class Processor():
|
||||||
|
|
||||||
def _expand_dependencies(
|
def _expand_dependencies(
|
||||||
self,
|
self,
|
||||||
handlers: List[EventHandler],
|
*handlers: EventHandler,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
) -> List[EventHandler]:
|
) -> List[EventHandler]:
|
||||||
"""
|
"""
|
||||||
Check handlers and add dependencies if not included.
|
Check handlers and add dependencies if not included.
|
||||||
|
|
||||||
:param handlers: the list of primary `EventHandler`s
|
: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:
|
def _get_handler_maps(self) -> HandlerMultimap:
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue