473,385 Members | 1,329 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

advice on this little script

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! :)
Mar 9 '06 #1
32 1817
John Salerno wrote:
sleep(1.0)


Heh heh, that was for testing. Obviously it should read 60.0 (is a float
necessary at all?).
Mar 9 '06 #2
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/
Mar 9 '06 #3
What about a console beep? How do you add that?

rpd

Mar 9 '06 #4
On 2006-03-09, John Salerno <jo******@NOSPAMgmail.com> 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
Mar 9 '06 #5
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. :)
Mar 9 '06 #6
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....
Mar 9 '06 #7
Grant Edwards wrote:
On 2006-03-09, John Salerno <jo******@NOSPAMgmail.com> 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. :)
Mar 9 '06 #8
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?
Mar 9 '06 #9
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

Mar 9 '06 #10
On 2006-03-09, John Salerno <jo******@NOSPAMgmail.com> wrote:
Grant Edwards wrote:
On 2006-03-09, John Salerno <jo******@NOSPAMgmail.com> 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. :)


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?

--
Grant Edwards grante Yow! NEWARK has been
at REZONED!! DES MOINES has
visi.com been REZONED!!
Mar 9 '06 #11
Grant Edwards wrote:
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.
Mar 9 '06 #12
Grant Edwards wrote:
On 2006-03-09, John Salerno <jo******@NOSPAMgmail.com> 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'


I might be doing something wrong, but this just keeps printing out '10
minutes remaining' each time.
Mar 9 '06 #13
Ben Cartwright wrote:
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


I'd prefer to be platform neutral when possible. Is the first option
that, or does this change depending on OS?
Mar 9 '06 #14
John Salerno wrote:
Grant Edwards wrote:
On 2006-03-09, John Salerno <jo******@NOSPAMgmail.com> 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'


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


print x, 'minutes remaining'

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

http://www.jamesstroud.com/
Mar 9 '06 #15
John Salerno wrote:
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.


should be:

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

Ben
Mar 9 '06 #16
James Stroud wrote:
John Salerno wrote:
Grant Edwards wrote:
On 2006-03-09, John Salerno <jo******@NOSPAMgmail.com> 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'

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


print x, 'minutes remaining'

Ah! Thanks to both of you!
Mar 9 '06 #17
John Salerno <jo******@NOSPAMgmail.com> writes:
for x in range(minutes,0,-1):
sleep(60.0)
print minutes, 'minutes remaining'

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.
Mar 9 '06 #18
Grant Edwards <gr****@visi.com> wrote:
...
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?


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
Mar 9 '06 #19
John Salerno <jo******@NOSPAMgmail.com> wrote:
...
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
Mar 9 '06 #20
Paul Rubin wrote:
John Salerno <jo******@NOSPAMgmail.com> writes:
for x in range(minutes,0,-1):
sleep(60.0)
print minutes, 'minutes remaining'


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.


You might also want to synchronize to a caesium clock, but the guy is
timing his laundry, for Pete's sake! Can we agree your approach, while
theoretically sound, might be a little over-complicated for a first
application?

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd www.holdenweb.com
Love me, love my blog holdenweb.blogspot.com

Mar 9 '06 #21
Alex Martelli wrote:
John Salerno <jo******@NOSPAMgmail.com> wrote:
...
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 use both fairly interchangeably. When I just need one or two things
from the module, or if I am going to use something a lot, I use from xx
import yy.

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).


If you use from xx import yy, searching for yy will show you its
provenance as well.

From xx import * is evil because it does hide the provenance of names.
It can also give unexpected results - if xx contains from zz import *,
then all of the names from zz will also be imported into the module
importing xx!

Kent
Mar 9 '06 #22
On 2006-03-09, John Salerno <jo******@NOSPAMgmail.com> wrote:
Grant Edwards wrote:
On 2006-03-09, John Salerno <jo******@NOSPAMgmail.com> 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'


print x, 'minutes remaining'

Doh!

--
Grant Edwards grante Yow! Now KEN is having
at a MENTAL CRISIS beacuse
visi.com his "R.V." PAYMENTS are
OVER-DUE!!
Mar 9 '06 #23
Steve Holden wrote:
You might also want to synchronize to a caesium clock, but the guy is
timing his laundry, for Pete's sake! Can we agree your approach, while
theoretically sound, might be a little over-complicated for a first
application?


LOL. Thanks, I was about to fall out of my chair! :)
Mar 9 '06 #24
Alex Martelli wrote:
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.


I like that. So in my case I'd use 'import time' (which I actually
already changed last night). I think especially right now, while I'm new
to Python, it helps me to see 'time.sleep' instead of just 'sleep', so I
can begin to associate certain functions with their proper modules.

Not to mention, like you said, that importing specific pieces like
functions tends to look a little messy in the code when they aren't
qualified.
Mar 9 '06 #25
Kent Johnson <ke**@kentsjohnson.com> wrote:
...
If you use from xx import yy, searching for yy will show you its
provenance as well.


But when seeing the barename yy, it gives no clue whether the module
you're reading used 'from xx import yy' or defined yy in any other way
(localy, globally, or in any intermediate scope).

'from' has many other disadvantages, such as working badly with reload
_except_ when used specifically to import a module.
Alex

Mar 9 '06 #26
John Salerno <jo******@NOSPAMgmail.com> wrote:
Alex Martelli wrote:
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.
I like that. So in my case I'd use 'import time' (which I actually
already changed last night). I think especially right now, while I'm new
to Python, it helps me to see 'time.sleep' instead of just 'sleep', so I
can begin to associate certain functions with their proper modules.


I'm hardly new to Python, yet it keeps helping me too;-).

Not to mention, like you said, that importing specific pieces like
functions tends to look a little messy in the code when they aren't
qualified.


I wouldn't call it messy, but rather potentially "confusing" in a large
module with many imports -- in the middle of the module,

a = b(c)

always gives the strong impression that b and cc are local or global (or
from an intermediate lexical scope), while

a = X.b(Y.c)

contains hints suggesting otherwise (if I had no clue what X and Y were
at this point, I'd _first_ check if they were imported modules, while it
would be my last resort in the former snippet regarding b and c).
Alex
Mar 9 '06 #27
Alex Martelli wrote:
I'm hardly new to Python, yet it keeps helping me too;-).


Yeah, from what I hear and read, you know a thing or two about the
language. :)
Mar 9 '06 #28
There are several of these writing quotes, all good in their own way,
because they emphasize concision as the first order of business for any
writer.

"If I had more time, I would write a short letter."--Blaise Pascal

"If the author had been less industrious, this book would be twice as
long."--Evelyn Waugh

Telegram from Mark Twain's publisher: "NEED 2-PAGE SHORT STORY IN TWO
DAYS." Twain replied: "NO CAN DO 2 PAGES TWO DAYS. CAN DO 30 PAGES 2
DAYS. NEED 30 DAYS TO DO 2 PAGES."

rpd
http://dooling.com

Mar 9 '06 #29
>>You might also want to synchronize to a caesium clock, but the guy is
timing his laundry, <<

My morning coffee just streamed out of my nose. Air. I need air.

rpd

Mar 9 '06 #30
John Salerno wrote:
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?


I used to do both, depending on what was more convenient. These days,
as I've been writing larger and more complex programs, I pretty much
just import the module. I find that a consistent approach just keeps
things easier to digest, gives me less to think about and fewer
decisions to make. One can consistently import the module, but not the
symbols within because sometimes there's just too many.

The exception is I still import global singleton objects using
from...import. Such objects behave more like modules in my code
anyways, and probably they'd actually be modules if not for the
ugliness of using the global statement.

Having said all that, I still cringe slightly when writing stuff like
glob.glob() and time.time(). (Which brings me to a related piece of
advice: a good way to name modules that avoids confusion with other
symbols is to use "act of" words, i.e., words that end in ing, ion,
age, or ment. I would have named the glob module "globbing", the time
module "timing", and so on; then I wouldn't have to cringe.)
Carl

Mar 9 '06 #31

"John Salerno" <jo******@NOSPAMgmail.com> wrote in message
news:zN********************@rcn.net...
Ben Cartwright wrote:
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.


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


It is display dependent what display does with control codes. \007 is ANSI
ascii control char which only works to beep on ansi terminals or emulation
thereof. On PCs and Windows, displays have extended char set that prints
most all char codes. \007 prints as a square box. IBM EBCDIC terminals
would probably do third thing.

Terry Jan Reedy

Mar 9 '06 #32
"BartlebyScrivener" <rp*******@gmail.com> writes:
There are several of these writing quotes, all good in their own way,


And from Hamlet: brevity is the soul of wit.
Apr 8 '06 #33

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: rebeccatre | last post by:
hi, i am still figuring out joins and whatnot, could someone help me achieve this goal? <script> var setnumber = 5; var total5 = 100; alert(total+setnumber); </script>
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.