----- Message from [EMAIL PROTECTED] ---------
Date: Sun, 10 Aug 2008 14:03:38 -0400
From: Kent Johnson <[EMAIL PROTECTED]>
Reply-To: Kent Johnson <[EMAIL PROTECTED]>
Subject: Re: [Tutor] using generators to mock raw_input for doctest
On Sun, Aug 10, 2008 at 1:38 AM, <[EMAIL PROTECTED]> wrote:
2) Is there some better way to enable doctesting of code that uses
raw_input? All I found when googling was the suggestion that in place of:
def myfunc():
# code employing raw_input here
one could write:
def myfunc(input_meth=raw_input):
# code with raw_input calls replaced with input_meth calls
but that seems like complicating my code for the non-doctest case.
It is not going to make your code more complicated - the client code
doesn't change at all and in myfunc you just change your use of
raw_input() to input_meth(). If you declare
def myfunc(raw_input=raw_input):
you don't even have to change the body of myfunc.
Thanks for the reply, Kent.
I didn't express myself well. What I meant was I didn't quite like the
complication of the signature for no gain from the non-testing
client's side. I'm thinking not that it would make the code harder to
use, but rather a bit harder to figure out how to use---but I may well
over-rate the cognitive overhead involved :-)
<snip my smelly attempt to use a generator>
This is needlessly complex.
I suspected as much :-)
The values argument must be iterable (or
the for loop would break) so you might as well use its iterator
instead of making your own:
class myraw(object):
def __init__(self, values):
self.stream = iter(values)
def readline(self):
return str(self.stream.next())
<Smacks forehead>
Thanks again,
Brian vdB
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor