Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > advice on this little script

Reply
Thread Tools

advice on this little script

 
 
John Salerno
Guest
Posts: n/a
 
      03-09-2006
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);
}
}
}
}
--------------------------

8 lines vs. 31 lines!
 
Reply With Quote
 
 
 
 
John Salerno
Guest
Posts: n/a
 
      03-09-2006
John Salerno wrote:

> sleep(1.0)


Heh heh, that was for testing. Obviously it should read 60.0 (is a float
necessary at all?).
 
Reply With Quote
 
 
 
 
James Stroud
Guest
Posts: n/a
 
      03-09-2006
John Salerno wrote:
> 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/
 
Reply With Quote
 
BartlebyScrivener
Guest
Posts: n/a
 
      03-09-2006
What about a console beep? How do you add that?

rpd

 
Reply With Quote
 
John Salerno
Guest
Posts: n/a
 
      03-09-2006
James Stroud wrote:

> 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.
 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      03-09-2006
On 2006-03-09, John Salerno <(E-Mail Removed)> wrote:

> 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'

--
Grant Edwards grante Yow! My EARS are GONE!!
at
visi.com
 
Reply With Quote
 
John Salerno
Guest
Posts: n/a
 
      03-09-2006
BartlebyScrivener wrote:
> 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....
 
Reply With Quote
 
John Salerno
Guest
Posts: n/a
 
      03-09-2006
Grant Edwards wrote:
> On 2006-03-09, John Salerno <(E-Mail Removed)> wrote:
>
>> 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'
>


Nice! Cross off another line! I feel like Hemingway.
 
Reply With Quote
 
John Salerno
Guest
Posts: n/a
 
      03-09-2006
John Salerno wrote:

> 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?
 
Reply With Quote
 
Ben Cartwright
Guest
Posts: n/a
 
      03-09-2006
BartlebyScrivener wrote:
> What about a console beep? How do you add that?
>
> rpd


Just use ASCII code 007 (BEL/BEEP):

>>> import sys
>>> sys.stdout.write('\007')


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

--Ben

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
1 little 2 little 3 little Kennedys dale Digital Photography 0 03-23-2008 01:03 PM
having a little problem with some code for a little game I am creating. ThaDoctor C++ 3 09-28-2007 03:28 PM
little red X in little white box Puzzled Computer Support 8 12-13-2004 09:11 AM
can someone give me a little advice. Adam Schroeder C++ 2 09-11-2003 11:20 PM
A little advice with some frames Neil HTML 0 07-06-2003 07:46 AM



Advertisments