[Tutor] More problems with Learning Python example
->Terry<-
tvbare at socket.net
Fri Aug 12 03:25:37 CEST 2005
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Thanks Danny and Bob. Your explanations not only helped
me solve the error, but understand why i got the error.
Unfortunately, I have run into another problem with the
same example. Here is where I'm at now:
My system is Slackware 10.1 and Python version is 2.4.1
- --------------------------------
* makezeros.py
- --------------------------------
def lots_of_appends():
zeros = []
for i in range(10000):
zeros.append(0)
def one_multiply():
zeros = [0] * 10000
- --------------------------------
* corrected timings.py
- --------------------------------
import time, makezeros
def do_timing(num_times, *funcs):
totals = {}
for func in funcs:
totals[func] = 0.0
starttime = time.clock() # record starting time
for x in range(num_times):
for func in funcs:
apply(func)
stoptime = time.clock() # record ending time
elapsed = stoptime - starttime # difference yields time elapsed
totals[func] = totals[func] + elapsed
for func in funcs:
print "Running %s %d times took %.3f seconds" % (func.__name__,
num_times, totals[func])
do_timing(100, makezeros.lots_of_appends, makezeros.one_multiply)
- -------------------------------
* new error message
- -------------------------------
Traceback (most recent call last):
File "timings.py", line 16, in ?
do_timing(100, makezeros.lots_of_appends, makezeros.one_multiply)
File "timings.py", line 12, in do_timing
totals[func] = totals[func] + elapsed
KeyError: <function one_multiply at 0x403eaf0c>
help(KeyError) tells me Mapping key not found. Have I
stumbled on another error in the book? I don't know
how to proceed in tracking this down. any suggestions?
Thanks again,
- --
Terry
,-~~-.___. Terry Randall <tvbareATsocketDOTnet>
/ | ' \
< ) 0 Linux Counter Project User# 98233
\_/, ,-----'
==== //
/ \-'~; /~~~(0)
/ __/~| / | If only Snoopy had Slackware...
=( ______| (________|
"He is your friend, your partner, your defender, your dog.
You are his life, his love, his leader. He will be yours,
faithful and true, to the last beat of his heart. You owe
it to him to be worthy of such devotion." -- Unknown
(Best viewed with a mono-spaced font.)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)
iD8DBQFC+/qWQvSnsfFzkV0RAmPzAJ9bsffZryIZebPZzhTQg7GfR+vPbwCeN6qc
spEguzDew7DkhvxleyoZ+Zs=
=QTh6
-----END PGP SIGNATURE-----
More information about the Tutor
mailing list