advice on this little script

J

John Salerno

My first project when I started learning C# was to make a little timer
to tell me when my laundry was done :) and I thought it would be fun to
convert this to Python. Here's what I came up with after much struggling
with the Timer class from the threading module -- as you can see, I
abandoned it for the sleep() function from timer.

Please let me know if this is a good (i.e. Pythonic) way of doing this,
and if it can be improved (although I don't need anything fancier than
this basic functionality).
---------------------
from time import sleep

minutes = input('Enter the number of minutes to wait: ')

for x in range(minutes):
sleep(1.0)
minutes -= 1
print minutes, 'minutes remaining.'
---------------------

And if you are interested, here is the original C# code in all its
monstrous glory. I can't believe the difference!
----------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace MyTimer
{
class Timer
{
static void Main()
{
Console.Write("Enter the number of minutes to wait: ");
int iTime = Int32.Parse(Console.ReadLine());
Thread.Sleep(60000);

for (int i = iTime - 1; i > 0; i--)
{
Console.WriteLine("{0} minute(s) remaining.", i);
Thread.Sleep(60000);
}

Console.Write("{0} minutes have elapsed.", iTime);

for (int i = 3; i > 0; i--)
{
Console.Beep();
Thread.Sleep(1000);
}
}
}
}
 
J

James Stroud

John said:
My first project when I started learning C# was to make a little timer
to tell me when my laundry was done :) and I thought it would be fun to
convert this to Python. Here's what I came up with after much struggling
with the Timer class from the threading module -- as you can see, I
abandoned it for the sleep() function from timer.

Please let me know if this is a good (i.e. Pythonic) way of doing this,
and if it can be improved (although I don't need anything fancier than
this basic functionality).
---------------------
from time import sleep

minutes = input('Enter the number of minutes to wait: ')

for x in range(minutes):
sleep(1.0)
minutes -= 1
print minutes, 'minutes remaining.'
---------------------

Very nice, but maybe

....
sleep(60.0)

This corrects for the number of seconds in a minute.

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 
J

John Salerno

James said:
Very nice, but maybe

...
sleep(60.0)

This corrects for the number of seconds in a minute.

James

Thanks! And yeah, I fixed that little issue. If only laundry could be
done that fast. :)
 
G

Grant Edwards

from time import sleep

minutes = input('Enter the number of minutes to wait: ')

for x in range(minutes):
sleep(1.0)
minutes -= 1
print minutes, 'minutes remaining.'

for x in range(minutes,0,-1):
sleep(60.0)
print minutes, 'minutes remaining'
 
J

John Salerno

BartlebyScrivener said:
What about a console beep? How do you add that?

rpd

Ooh, good point! I forgot about the most important part, otherwise I'd
never know it was done and someone would steal my clothes! :)

Time to do some library reference research....
 
J

John Salerno

John said:
from time import sleep ....
sleep(1.0)

Very picky point, but I'd like to know what others think of this. Should
I import as above, or should I do this:

import time
.....
time.sleep(60.0) ???

I think the 'from time import sleep' looks cleaner, because I'm only
taking what I need (is an import any more expensive than this from?),
but I also feel like the 'time.sleep' syntax is much more
self-describing and better to read than just 'sleep'.

So what do you guys think between these two choices?
 
B

Ben Cartwright

BartlebyScrivener said:
What about a console beep? How do you add that?

rpd

Just use ASCII code 007 (BEL/BEEP):

Or if you're on Windows, use the winsound standard module.

--Ben
 
G

Grant Edwards

Nice! Cross off another line! I feel like Hemingway. :)

Was he the one who once apologized to his editor for a story
being so long because he was in a hurry and didn't have time to
make it shorter?
 
J

John Salerno

Grant said:
Was he the one who once apologized to his editor for a story
being so long because he was in a hurry and didn't have time to
make it shorter?

Hmm, not sure. He doesn't seem like the type to apologize, or even have
long stories. :) He was ruthless with his edits, that's for sure.
 
J

John Salerno

Grant said:
for x in range(minutes,0,-1):
sleep(60.0)
print minutes, 'minutes remaining'

I might be doing something wrong, but this just keeps printing out '10
minutes remaining' each time.
 
J

John Salerno

Ben said:
Just use ASCII code 007 (BEL/BEEP):


Or if you're on Windows, use the winsound standard module.

--Ben

I'd prefer to be platform neutral when possible. Is the first option
that, or does this change depending on OS?
 
B

Benjamin Liebald

John said:
I might be doing something wrong, but this just keeps printing out '10
minutes remaining' each time.

should be:

for x in range(minutes,0,-1):
sleep(60.0)
print x, 'minutes remaining'

Ben
 
P

Paul Rubin

John Salerno said:
Nice! Cross off another line! I feel like Hemingway. :)

Besides the bug mentioned, I don't think you should really do it that
way, since sleep(60.0) might not sleep for exactly 60 sec (it could be
longer or shorter). Preferable is something like (untested):

now = time.time()
sleep_until = now + 60*minutes
while now < sleep_until:
print int((now - sleep_until)/60), 'minutes remaining'
sleep (60)
now = time.time()

You might actually want to stop sleeping a little bit early (say if
you wake up 5 seconds before sleep_until), or round the message to the
nearest number of minutes, etc.
 
A

Alex Martelli

Grant Edwards said:
Was he the one who once apologized to his editor for a story
being so long because he was in a hurry and didn't have time to
make it shorter?

Nope, that immortal quote is by Blaise Pascal (also known as a
philosopher, a mathematician, and the developer of one of the best early
mechanical calculators, as well as for the programming language Niklaus
Wirth named after him, and the SI standard unit of pressure, 1 Pa == 1 N
per square meter, also named in his honor because he was the first to
use a barometer to measure altitude); it's found in his "Lettres
Provinciales" (which is not a collection of "real" letters, but rather a
philosophical, theological and political polemic cast in epistular
form). It's often misattributed to Mark Twain, but this the first time
I've heard it associated with Hemingway!



Alex
 
A

Alex Martelli

John Salerno said:
I think the 'from time import sleep' looks cleaner, because I'm only
taking what I need (is an import any more expensive than this from?),
but I also feel like the 'time.sleep' syntax is much more
self-describing and better to read than just 'sleep'.

So what do you guys think between these two choices?

I only use the 'from' statement to import specific modules from a
package, never to import specific objects (functions, classes, or
whatever) from a module. It scales much better: when reading a long
module, I _know_ that any barename X always refers to a local,
nested-scope, or global name, never to something snatched from
who-recalls-what module -- in the latter case I'll never see a barename,
but always somemodule.X, and searching for '... import somemodule' will
immediately tell me where somemodule was coming from (should I need to
be reminded of that information).


Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top