doctest with variable return value
Rob Sinclar
lunixinclar at orange.fr
Tue Jul 25 06:05:12 EDT 2006
On Tuesday 25 July 2006 09:53, 3KWA wrote:
> Hi all,
>
> I am wondering what is the standard doctest (test) practice for
> functions who's returned value change all the time e.g. forex rate:
>
> import urllib
>
> def get_rate(symbol):
> """get_rate(symbol) connects to yahoo finance to return the rate of
> symbol.
>
> >>>get_rate('AUDEUR')
>
> """
>
> url=
> "http://finance.yahoo.com/d/quotes.csv?s=%s=X&f=sl1d1t1c1ohgv&e=.csv" %
> \
> symbol
> f=urllib.urlopen(url)
> return float(f.readline().split(',')[1])
>
> As you can guess I am very new to unittest and doctest in general ...
>
> Thanks for your help,
>
> EuGeNe
Hi EuGeNe,
Pass it through a variable before returning a value.
Here's how I would do it:
import urllib2
def get_rate(symbol):
URL='http://finance.yahoo.com/d/quotes.csv?s=AUDEUR=X&f=sl1d1t1c1ohgv&e=.csv'
request_headers = { 'User-Agent': 'Linuxinclar/0.1' }
request = urllib2.Request(URL, None, request_headers)
response = urllib2.urlopen(request)
STR = response.read()
return STR.split(',')[1].strip()
SYMB='AUDEUR'
print SYMB,'=',get_rate(SYMB)
Python rocks.
That's be nice to indicate hour though (4th array element)...
Best Regards,
Rob Sinclar
More information about the Python-list
mailing list