advice on this little script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Salerno

    #1

    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.Collecti ons.Generic;
    using System.Text;
    using System.Threadin g;

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

    for (int i = iTime - 1; i > 0; i--)
    {
    Console.WriteLi ne("{0} minute(s) remaining.", i);
    Thread.Sleep(60 000);
    }

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

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

    8 lines vs. 31 lines! :)
  • John Salerno

    #2
    Re: advice on this little script

    John Salerno wrote:
    [color=blue]
    > sleep(1.0)[/color]

    Heh heh, that was for testing. Obviously it should read 60.0 (is a float
    necessary at all?).

    Comment

    • James Stroud

      #3
      Re: advice on this little script

      John Salerno wrote:[color=blue]
      > 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.'
      > ---------------------[/color]

      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


      Comment

      • BartlebyScrivener

        #4
        Re: advice on this little script

        What about a console beep? How do you add that?

        rpd

        Comment

        • Grant Edwards

          #5
          Re: advice on this little script

          On 2006-03-09, John Salerno <johnjsal@NOSPA Mgmail.com> wrote:
          [color=blue]
          > 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.'[/color]

          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

          Comment

          • John Salerno

            #6
            Re: advice on this little script

            James Stroud wrote:
            [color=blue]
            > Very nice, but maybe
            >
            > ...
            > sleep(60.0)
            >
            > This corrects for the number of seconds in a minute.
            >
            > James
            >[/color]

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

            Comment

            • John Salerno

              #7
              Re: advice on this little script

              BartlebyScriven er wrote:[color=blue]
              > What about a console beep? How do you add that?
              >
              > rpd
              >[/color]

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

              Comment

              • John Salerno

                #8
                Re: advice on this little script

                Grant Edwards wrote:[color=blue]
                > On 2006-03-09, John Salerno <johnjsal@NOSPA Mgmail.com> wrote:
                >[color=green]
                >> 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.'[/color]
                >
                > for x in range(minutes,0 ,-1):
                > sleep(60.0)
                > print minutes, 'minutes remaining'
                >[/color]

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

                Comment

                • John Salerno

                  #9
                  Re: advice on this little script

                  John Salerno wrote:
                  [color=blue]
                  > from time import sleep[/color]
                  ....[color=blue]
                  > sleep(1.0)[/color]

                  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?

                  Comment

                  • Ben Cartwright

                    #10
                    Re: advice on this little script

                    BartlebyScriven er wrote:[color=blue]
                    > What about a console beep? How do you add that?
                    >
                    > rpd[/color]

                    Just use ASCII code 007 (BEL/BEEP):
                    [color=blue][color=green][color=darkred]
                    >>> import sys
                    >>> sys.stdout.writ e('\007')[/color][/color][/color]

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

                    --Ben

                    Comment

                    • Grant Edwards

                      #11
                      Re: advice on this little script

                      On 2006-03-09, John Salerno <johnjsal@NOSPA Mgmail.com> wrote:[color=blue]
                      > Grant Edwards wrote:[color=green]
                      >> On 2006-03-09, John Salerno <johnjsal@NOSPA Mgmail.com> wrote:
                      >>[color=darkred]
                      >>> 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.'[/color]
                      >>
                      >> for x in range(minutes,0 ,-1):
                      >> sleep(60.0)
                      >> print minutes, 'minutes remaining'[/color]
                      >
                      > Nice! Cross off another line! I feel like Hemingway. :)[/color]

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

                      Comment

                      • John Salerno

                        #12
                        Re: advice on this little script

                        Grant Edwards wrote:
                        [color=blue]
                        > 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?[/color]

                        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.

                        Comment

                        • John Salerno

                          #13
                          Re: advice on this little script

                          Grant Edwards wrote:[color=blue]
                          > On 2006-03-09, John Salerno <johnjsal@NOSPA Mgmail.com> wrote:
                          >[color=green]
                          >> 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.'[/color]
                          >
                          > for x in range(minutes,0 ,-1):
                          > sleep(60.0)
                          > print minutes, 'minutes remaining'
                          >[/color]

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

                          Comment

                          • John Salerno

                            #14
                            Re: advice on this little script

                            Ben Cartwright wrote:[color=blue]
                            > BartlebyScriven er wrote:[color=green]
                            >> What about a console beep? How do you add that?
                            >>
                            >> rpd[/color]
                            >
                            > Just use ASCII code 007 (BEL/BEEP):
                            >[color=green][color=darkred]
                            > >>> import sys
                            > >>> sys.stdout.writ e('\007')[/color][/color]
                            >
                            > Or if you're on Windows, use the winsound standard module.
                            >
                            > --Ben
                            >[/color]

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

                            Comment

                            • James Stroud

                              #15
                              Re: advice on this little script

                              John Salerno wrote:[color=blue]
                              > Grant Edwards wrote:
                              >[color=green]
                              >> On 2006-03-09, John Salerno <johnjsal@NOSPA Mgmail.com> wrote:
                              >>[color=darkred]
                              >>> 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.'[/color]
                              >>
                              >>
                              >> for x in range(minutes,0 ,-1):
                              >> sleep(60.0)
                              >> print minutes, 'minutes remaining'
                              >>[/color]
                              >
                              > I might be doing something wrong, but this just keeps printing out '10
                              > minutes remaining' each time.[/color]

                              print x, 'minutes remaining'



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


                              Comment

                              Working...