The example code for ChainLink is more recursive than it needs to be. The "solutions" list at the end is also a bit weirder than necessary. Try this:
class Chain:
""" This class replaces ChainLink. """
def __init__(self, strategies):
self.strategies = strategies
def __call__(self, messenger):
result = Result() # default failure result
for strategy in self.strategies:
result = strategy(messenger)
if result.isSuccessful():
return result
# If we get here, no strategy succeeded.
return result
# This code replaces the top-level code at the bottom
# of your example. Much more elegant, I think.
solver = Chain([
LeastSquares(),
NewtonsMethod(),
Bisection(),
ConjugateGradient()
])
line = LineData([
1.0, 2.0, 1.0, 2.0, -1.0,
3.0, 4.0, 5.0, 4.0
])
print solver(line)