[Tutor] problems with time.sleep()

Bunting, Glen, IG (x) GBunting864@Worldsavings.com
Wed, 20 Jun 2001 13:24:29 -0500


I made the changes you suggested and everything works now.

Thanks,
Glen

 -----Original Message-----
From: 	D-Man [mailto:dsh8290@rit.edu] 
Sent:	Wednesday, June 20, 2001 10:36 AM
To:	tutor@python.org
Subject:	Re: [Tutor] problems with time.sleep()

On Wed, Jun 20, 2001 at 11:59:16AM -0500, Bunting, Glen, IG (x) wrote:
| Hi,
| 
| Below is a script that I am trying to write.  Basically it grabs a url and
| downloads a file, calculates the speed of the download and place the speed
| and time in a dictionary.  The next step to figure out a way to use
Tkinter
| to graph the results.  The problem I'm getting is it is not printing out
the
| list that is in the dictionary, and gives no error for that, and the
| time.sleep() function fails.  I am on windows NT using ActivePython.  The
| script, output, and errors are below.  Any improvements would be
| appreciated.
| 
| Thanks
| Glen
| 
| 
| #!/usr/bin/env python
| 
| import os, urllib
| from time import *

Bad, bad.  It is not recommended to use "from-import-*".  Note that
this does NOT create a name at the module level called 'time'.

| def updateSpeedvar():
|         while 1:
|                 import time

Here you import time again.  Any reason?  (Other than to create the
binding 'time' to the module object, which wasn't done at the top)?

|                 first = time.time()
|                 print first
|  
| urllib.urlretrieve('http://theworks.tucows.com/files3/cooolftp.exe',
|                            'test')
|                 second = time.time()
|                 print second
|                 final = second - first
|                 print final
|                 speed1 = 1622/final
|                 speed = round(speed1, 1)
|                 print speed
|                 time = strftime("%X", localtime())
                  ^^^^^^
Here you rebind the local name 'time' to a string object.  It no
longer refers to the module object it used to.

|                 print time
|                 dict = {time:speed}
|                 dict.items()
|                 time.sleep(180)

Here time is a string and has no 'sleep' attribute.



I would highly recommend changing the 'from time import *' line at the
beginning to 'import time', then removing the import from inside the
loop (it is redundant now).  Then always qualify the functions.  Ex
use 'time.strftime' not 'strftime'.  As a final fix, change the name
of the local reference to a string to something other than 'time'.
Use 'time_str' or some other name.

The not recommended fix would be to remove the 'time.' from the sleep
call because 'sleep' (and other stuff) was already brought into the
module's namespace, and hasn't been rebound since then.

HTH,
-D


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


*****************************************************************************
If you are not the intended recipient of this e-mail, please notify 
the sender immediately. The contents of this e-mail do not amend 
any existing disclosures or agreements unless expressly stated.
*****************************************************************************