[omaha] expand my understanding...

Steve Young wereapwhatwesow at gmail.com
Sun Dec 12 06:43:46 CET 2010


Thanks Jeff.  Good to hear from you, haven't seen you in awhile.

I just figured out that the way the test was running, the operands were mock
objects. So when m is instantiated with
m = multiply(p1, p2)
self.operands becomes a tuple containing two mock objects. (I originally
thought it was a tuple containing the numbers, and x.evaluate(bindings) was
doing some recursive wierdness with the evaluate function.  Once I realized
that x.evaluate... was returning the mocker result values everything became
clearer.)

To finish my explanation if anyone is still reading:
Then when m.evaluate({}) is called, the x.evaluate(bindings) loads the
mocker.result values to replace the mock objects with the mocker.result
numbers. It works like a normal list comprehension:
The first time thru the list comprehension x = p1. Since the test code for
p1.evaluate({}) is 97.43, it puts 97.43 as the first item in vals list. It
then does the same for p2. So vals = [97.43, -16.25]

Maybe I am spending too much time on a not so great example, because I see
how the test works, but if I try and use the multiply class normally, and
don't pass it mock objects as the operands (ie pass it numbers), then I get
an attribute error that saying the object has no attribute 'evaluate'.

the test code looks like this:
from mocker import Mocker
mocker = Mocker()
p1 = mocker.mock()
p1.evaluate({})
mocker.result(97.43)
p2 = mocker.mock()
p2.evaluate({})
mocker.result(-16.25)
mocker.replay()
m = multiply(p1, p2)
round(m.evaluate({}),2)

(the above code was originally in a docstring, I changed it and added it to
the bottom of my multiply module. it isn't a proper test this way...)

On Sat, Dec 11, 2010 at 10:38 PM, Jeff Hinrichs - DM&T
<jeffh at dundeemt.com>wrote:

> You are correct. It's a list  comprehension.
> L=[1,2,3]
> print [str(x) for x in L]
>
> Prints out
> ['1', '2','3']
>
> Jeff
>  On Dec 11, 2010 5:19 PM, "Steve Young" <wereapwhatwesow at gmail.com> wrote:
> > I am going thru a book on testing, and ran across the following code:
> >
> > class multiply:
> > def __init__(self, *operands):
> > self.operands = operands
> >
> > def evaluate(self, bindings):
> > vals = [x.evaluate(bindings) for x in self.operands]
> >
> > if len(vals) < 2:
> > raise ValueError('multiply without at least two '
> > 'operands is meaningless')
> >
> > result = 1.0
> > for val in vals:
> > result *= val
> >
> > return result
> >
> > The line " vals = [x.evaluate(bindings) for x in self.operands]"
> > appears to take operand(s) and put them into a list. I can't get my brain
> > to understand it. operands are any number of floating point numbers. Any
> > tips greatly appreciated.
> >
> >
> > --
> > Steve Young
> > _______________________________________________
> > Omaha Python Users Group mailing list
> > Omaha at python.org
> > http://mail.python.org/mailman/listinfo/omaha
> > http://www.OmahaPython.org
> _______________________________________________
> Omaha Python Users Group mailing list
> Omaha at python.org
> http://mail.python.org/mailman/listinfo/omaha
> http://www.OmahaPython.org
>



-- 
Steve Young


More information about the Omaha mailing list