advice on this little script

Discussion in 'Python' started by John Salerno, Mar 9, 2006.

  1. John Salerno

    John Salerno Guest

    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! :)
     
    John Salerno, Mar 9, 2006
    #1
    1. Advertising

  2. John Salerno

    John Salerno Guest

    John Salerno wrote:

    > sleep(1.0)


    Heh heh, that was for testing. Obviously it should read 60.0 (is a float
    necessary at all?).
     
    John Salerno, Mar 9, 2006
    #2
    1. Advertising

  3. John Salerno

    James Stroud Guest

    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/
     
    James Stroud, Mar 9, 2006
    #3
  4. What about a console beep? How do you add that?

    rpd
     
    BartlebyScrivener, Mar 9, 2006
    #4
  5. John Salerno

    John Salerno Guest

    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. :)
     
    John Salerno, Mar 9, 2006
    #5
  6. On 2006-03-09, John Salerno <> 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
     
    Grant Edwards, Mar 9, 2006
    #6
  7. John Salerno

    John Salerno Guest

    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....
     
    John Salerno, Mar 9, 2006
    #7
  8. John Salerno

    John Salerno Guest

    Grant Edwards wrote:
    > On 2006-03-09, John Salerno <> 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. :)
     
    John Salerno, Mar 9, 2006
    #8
  9. John Salerno

    John Salerno Guest

    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?
     
    John Salerno, Mar 9, 2006
    #9
  10. 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
     
    Ben Cartwright, Mar 9, 2006
    #10
  11. On 2006-03-09, John Salerno <> wrote:
    > Grant Edwards wrote:
    >> On 2006-03-09, John Salerno <> 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!!
     
    Grant Edwards, Mar 9, 2006
    #11
  12. John Salerno

    John Salerno Guest

    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.
     
    John Salerno, Mar 9, 2006
    #12
  13. John Salerno

    John Salerno Guest

    Grant Edwards wrote:
    > On 2006-03-09, John Salerno <> 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.
     
    John Salerno, Mar 9, 2006
    #13
  14. John Salerno

    John Salerno Guest

    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?
     
    John Salerno, Mar 9, 2006
    #14
  15. John Salerno

    James Stroud Guest

    John Salerno wrote:
    > Grant Edwards wrote:
    >
    >> On 2006-03-09, John Salerno <> 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/
     
    James Stroud, Mar 9, 2006
    #15
  16. 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
     
    Benjamin Liebald, Mar 9, 2006
    #16
  17. John Salerno

    John Salerno Guest

    James Stroud wrote:
    > John Salerno wrote:
    >> Grant Edwards wrote:
    >>
    >>> On 2006-03-09, John Salerno <> 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!
     
    John Salerno, Mar 9, 2006
    #17
  18. John Salerno

    Paul Rubin Guest

    John Salerno <> 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.
     
    Paul Rubin, Mar 9, 2006
    #18
  19. Grant Edwards <> 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
     
    Alex Martelli, Mar 9, 2006
    #19
  20. John Salerno <> 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
     
    Alex Martelli, Mar 9, 2006
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil
    Replies:
    0
    Views:
    390
  2. Adam Schroeder

    can someone give me a little advice.

    Adam Schroeder, Sep 11, 2003, in forum: C++
    Replies:
    2
    Views:
    332
    Govindan
    Sep 12, 2003
  3. ash

    a little advice

    ash, Jan 15, 2006, in forum: C Programming
    Replies:
    2
    Views:
    343
    Mark McIntyre
    Jan 15, 2006
  4. ThaDoctor
    Replies:
    3
    Views:
    385
    Alan Woodland
    Sep 28, 2007
  5. Daniel
    Replies:
    1
    Views:
    214
    Bart van Ingen Schenau
    Jul 9, 2013
Loading...

Share This Page