Return to Home Page
      Blog     Consulting     Seminars     Calendar     Books     CD-ROMS     Newsletter     About     FAQ      Search
 

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)
f  e  x



Add your comment below. Use an empty line between each paragraph. Paragraphs will be automatically formatted, and single carriage returns will be respected. Use <code> to begin a code block, and </code> to end a code block. Your email address will not be visible to spam harvesters or used in any way except to contact you with further questions.

Your Email Address:

Search     Home     WebLog     Consulting     Seminars     Calendar     Books     CD-ROMS     Newsletter     About     Contact     Site Design     Server Maintenance     Powered by Zope
©2007 MindView, Inc.