Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Builder Pattern

Reply
Thread Tools

Builder Pattern

 
 
Jason
Guest
Posts: n/a
 
      01-29-2006
Hi

I really need an example of a builder pattern in python, the closest I
could find to something resembling builder was on this thread...
http://groups.google.co.uk/group/it....0adefeefa5f3fa

Unfortunately I can't read the discussion it's possibly in Spanish.

Does anyone know of a builder example for python? Thanking you in
advance for your help.

Jason

 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      01-29-2006
Jason <(E-Mail Removed)> wrote:

> Hi
>
> I really need an example of a builder pattern in python, the closest I
> could find to something resembling builder was on this thread...
>

http://groups.google.co.uk/group/it....e_thread/threa
d/44f79c1def2583ca/200adefeefa5f3fa?lnk=st&q=design+pattern+builder+p yth
on+-build&rnum=25#200adefeefa5f3fa
>
> Unfortunately I can't read the discussion it's possibly in Spanish.


No, the leading 'it' in the newsgroup name means 'italian'. I was one
of the discussants.

> Does anyone know of a builder example for python? Thanking you in
> advance for your help.


The *example* is right there, e.g. at
<http://groups.google.co.uk/group/it....4edfee13b6fb51
6f> for the Python translation of the example found at a URL quoted in
the same post. If you want an EXAMPLE, there it is -- it's all code, no
comments, so the fact that the following discussion is in Italian should
be irrelevant, no?

What the following discussion says is that the C++ -> Python
transliteration is totally trivial and obvious and berates the original
requestor for making me waste 10 minutes to provide it.


Alex
 
Reply With Quote
 
 
 
 
Paddy
Guest
Posts: n/a
 
      01-29-2006
> What the following discussion says is that the C++ -> Python
> transliteration is totally trivial and obvious and berates the original
> requestor for making me waste 10 minutes to provide it.


Thanks for the giggle

 
Reply With Quote
 
Jason
Guest
Posts: n/a
 
      02-02-2006
I have converted another example of strategy which I prefer to the 2
described earlier, here it is:

class FindMinima:
def algorithm(self):raise NotImplementedError


class LeastSquares (FindMinima):
def algorithm(self,line):
return (1.1,2.2)


class NewtonsMethod (FindMinima):
def algorithm(self,line):
return (3.3,4.4)

class Bisection (FindMinima):
def algorithm(self,line):
return (5.5,6.6)

class ConjugateGradient (FindMinima):
def algorithm(self,line):
return (3.3,4.4)



class MinimaSolver: # context class
strategy=''
def __init__ (self,strategy):
self.strategy=strategy

def minima(self,line):
return self.strategy.algorithm(line)

def changeAlgorithm(self,newAlgorithm):
self.strategy = newAlgorithm

def test():
solver=MinimaSolver(LeastSquares())
print solver.minima((5.5,5.5))
solver.changeAlgorithm(Bisection())
print solver.minima((5.5,5.5))
test()

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      02-02-2006
> I have converted another example of strategy which I prefer to the 2
> described earlier, here it is:
>
> class FindMinima:
> ********def algorithm(self):raise NotImplementedError


When most of your code does nothing in a pompous way that is a sure sign
that you are heading in the wrong direction. Here's a translation into
python:

def least_squares(line):
return 1.1, 2.2

def newtons_method(line):
return 3.3, 4.4

def bisection(line):
return 5.5, 6.6

def conjugate_gradient(line):
return 3.3, 4.4

def test():
solver = least_squares
print solver((5.5,5.5))
solver = bisection
print solver((5.5,5.5))

test()

Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Where's a DOM builder that uses the Builder Pattern to ... buildDOMs? Phlip Python 5 01-13-2010 12:48 PM
dysfunctional Builder pattern? marlow.andrew@googlemail.com Java 6 06-11-2009 02:23 PM
Is this design pattern correct (builder) Mike C++ 2 05-22-2006 09:08 AM
Design problem while using Builder pattern qazmlp Java 2 01-31-2004 03:32 PM
Design problem while using Builder pattern qazmlp C++ 2 01-31-2004 03:32 PM



Advertisments