sleep() function, perhaps.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ryan Spencer

    sleep() function, perhaps.


    Hello Everyone,

    I want to have a row of periods, separated by small, say, .5 second
    intervals between each other. Thus, for example, making it have the
    appearance of a progress "bar".

    [code]
    import time

    sleep(.5)
    print "."
    sleep(.5)
    print "."
    [end code]

    But, it would (with those .5 second intervals)
    print out much like the following.

    ..
    (pause)
    ..
    (pause)

    I would rather those periods be on a single line, not printing on a new
    line each time.

    Any suggestions?

    Thank you in advance,
    ~Ryan

  • vincent wehren

    #2
    Re: sleep() function, perhaps.

    "Ryan Spencer" <jeder@earthlin k.net> schrieb im Newsbeitrag
    news:pan.2003.1 1.25.05.26.36.7 47748@earthlink .net...
    |
    | Hello Everyone,
    |
    | I want to have a row of periods, separated by small, say, .5 second
    | intervals between each other. Thus, for example, making it have the
    | appearance of a progress "bar".
    |
    | [code]
    | import time
    |
    | sleep(.5)
    | print "."
    | sleep(.5)
    | print "."
    | [end code]
    |
    | But, it would (with those .5 second intervals)
    | print out much like the following.

    |
    | .
    | (pause)
    | .
    | (pause)
    |
    | I would rather those periods be on a single line, not printing on a new
    | line each time.

    import time,sys

    while 1:
    sys.stdout.writ e(".")
    time.sleep(0.5)

    You could also use:

    print ".",

    (note the trailing comma)

    but that will leave you with an additional space after the dot

    HTH,

    Vincent Wehren
    | Any suggestions?
    |
    | Thank you in advance,
    | ~Ryan
    |


    Comment

    • Christopher Koppler

      #3
      Re: sleep() function, perhaps.

      On Tue, 25 Nov 2003 05:26:25 GMT, Ryan Spencer <jeder@earthlin k.net>
      wrote:
      [color=blue]
      >
      >Hello Everyone,
      >
      > I want to have a row of periods, separated by small, say, .5 second
      >intervals between each other. Thus, for example, making it have the
      >appearance of a progress "bar".
      >
      >[code]
      >import time
      >
      >sleep(.5)
      >print "."
      >sleep(.5)
      >print "."
      >[end code]
      >
      >But, it would (with those .5 second intervals)
      >print out much like the following.
      >
      >.
      >(pause)
      >.
      >(pause)
      >
      >I would rather those periods be on a single line, not printing on a new
      >line each time.
      >
      >Any suggestions?[/color]

      Try print with added comma or sys.stdout.writ e, like so:
      [color=blue][color=green][color=darkred]
      >>> import time
      >>> for i in range(10):[/color][/color][/color]
      .... print '\b.',
      .... time.sleep(1.5)
      ....
      ...........[color=blue][color=green][color=darkred]
      >>> import sys
      >>> for i in range(10):[/color][/color][/color]
      .... sys.stdout.writ e('.')
      .... time.sleep(0.5)
      ....
      ...........


      --
      Christopher

      Comment

      • Ryan Spencer

        #4
        Re: sleep() function, perhaps.

        On Tue, 25 Nov 2003 06:14:20 +0000, Christopher Koppler wrote:
        [color=blue]
        > On Tue, 25 Nov 2003 05:26:25 GMT, Ryan Spencer <jeder@earthlin k.net>
        > wrote:
        >[color=green]
        >>
        >>Hello Everyone,
        >>
        >> I want to have a row of periods, separated by small, say, .5 second
        >>intervals between each other. Thus, for example, making it have the
        >>appearance of a progress "bar".
        >>
        >>[code]
        >>import time
        >>
        >>sleep(.5)
        >>print "."
        >>sleep(.5)
        >>print "."
        >>[end code]
        >>
        >>But, it would (with those .5 second intervals)
        >>print out much like the following.
        >>
        >>.
        >>(pause)
        >>.
        >>(pause)
        >>
        >>I would rather those periods be on a single line, not printing on a new
        >>line each time.
        >>
        >>Any suggestions?[/color]
        >
        > Try print with added comma or sys.stdout.writ e, like so:
        >[color=green][color=darkred]
        >>>> import time
        >>>> for i in range(10):[/color][/color]
        > ... print '\b.',
        > ... time.sleep(1.5)
        > ...
        > ..........[color=green][color=darkred]
        >>>> import sys
        >>>> for i in range(10):[/color][/color]
        > ... sys.stdout.writ e('.')
        > ... time.sleep(0.5)
        > ...
        > ..........[/color]


        Heya', Thanks,

        Actually though, None of those suggestions give me the desired result
        I was looking for. I used both with the for loops, even the one with the
        while loop, and for the first suggested it prints all of them out on new
        lines (as opposed to all on the same line as I'd been hoping for) and the
        second posts on one full line, yet, the periods still don't have pauses
        between themselves. Perhaps something else is amiss?

        As well, the trailing commas gives the exact same result as doing the
        sys.stdout.writ e function.

        Is the code that you suggested giving you a result such as...

        ..(pause).(paus e).(pause).

        I raised everything up to a 1.5 second interval to exaggerate the results,
        and I'm afraid I still don't notice the pauses.

        Perchance I simply need to remove whatever is terminating the line?
        Does the time.sleep() function itself terminate a line? It would seem
        if I could bypass that, it would allow the pauses and keep the periods
        on one line.

        Thank you for your advice though, It's highly appreciated.

        ~Ryan

        Comment

        • Isaac To

          #5
          Re: sleep() function, perhaps.

          >>>>> "Ryan" == Ryan Spencer <jeder@earthlin k.net> writes:

          Ryan> On Tue, 25 Nov 2003 06:14:20 +0000, Christopher Koppler wrote:[color=blue][color=green]
          >> On Tue, 25 Nov 2003 05:26:25 GMT, Ryan Spencer <jeder@earthlin k.net>
          >> wrote:
          >>[color=darkred]
          >>> Hello Everyone,
          >>>
          >>> I want to have a row of periods, separated by small, say, .5 second
          >>> intervals between each other. Thus, for example, making it have the
          >>> appearance of a progress "bar".
          >>>
          >>> [code] import time
          >>>
          >>> sleep(.5) print "." sleep(.5) print "." [end code]
          >>>
          >>> But, it would (with those .5 second intervals) print out much like
          >>> the following.
          >>>
          >>> . (pause) . (pause)
          >>>
          >>> I would rather those periods be on a single line, not printing on a
          >>> new line each time.
          >>>
          >>> Any suggestions?[/color]
          >> Try print with added comma or sys.stdout.writ e, like so:
          >>[color=darkred]
          >>>>> import time for i in range(10):[/color]
          >> ... print '\b.', ... time.sleep(1.5) ... ..........[color=darkred]
          >>>>> import sys for i in range(10):[/color]
          >> ... sys.stdout.writ e('.') ... time.sleep(0.5) ... ..........[/color][/color]


          Ryan> Heya', Thanks,

          Ryan> Actually though, None of those suggestions give me the desired
          Ryan> result I was looking for. I used both with the for loops, even the
          Ryan> one with the while loop, and for the first suggested it prints all
          Ryan> of them out on new lines (as opposed to all on the same line as
          Ryan> I'd been hoping for) and the second posts on one full line, yet,
          Ryan> the periods still don't have pauses between themselves. Perhaps
          Ryan> something else is amiss?

          Ryan> As well, the trailing commas gives the exact same result as doing
          Ryan> the sys.stdout.writ e function.

          Ryan> Is the code that you suggested giving you a result such as...

          Ryan> .(pause).(pause ).(pause).

          Ryan> I raised everything up to a 1.5 second interval to exaggerate the
          Ryan> results, and I'm afraid I still don't notice the pauses.

          Ryan> Perchance I simply need to remove whatever is terminating the
          Ryan> line? Does the time.sleep() function itself terminate a line? It
          Ryan> would seem if I could bypass that, it would allow the pauses and
          Ryan> keep the periods on one line.

          Ryan> Thank you for your advice though, It's highly appreciated.

          Without a newline character, the normal sys.stdout writes to a buffer and
          won't try to flush it out. Try this:
          [color=blue][color=green][color=darkred]
          >>> for i in range(10):[/color][/color][/color]
          .... sys.stdout.writ e('.')
          .... sys.stdout.flus h()
          .... time.sleep(.5)
          ....
          ...........>>>

          Regards,
          Isaac.

          Comment

          • Padraig@Linux.ie

            #6
            Re: sleep() function, perhaps.

            Ryan Spencer wrote:[color=blue]
            > Hello Everyone,
            >
            > I want to have a row of periods, separated by small, say, .5 second
            > intervals between each other. Thus, for example, making it have the
            > appearance of a progress "bar".[/color]

            You've got the answer for dots, here's a spinner in case it's useful:

            import sys, time

            spinner="|/-\\"
            pos=0

            while 1:
            sys.stdout.writ e("\r"+spinne r[pos])
            sys.stdout.flus h()
            time.sleep(.5)
            pos+=1
            pos%=4

            Comment

            • Skip Montanaro

              #7
              Re: sleep() function, perhaps.


              Ryan> I want to have a row of periods, separated by small, say, .5
              Ryan> second intervals between each other. Thus, for example, making it
              Ryan> have the appearance of a progress "bar".

              You might find my progress module at



              a good starting point. Something like

              import progress, time
              ticker = progress.Progre ss(major=1, minor=1, majormark='.')
              while True:
              ticker.tick()
              time.sleep(0.5)

              running in a separate thread should do what you want.

              In many situations you want to actually measure progress of some
              computation. If you can wedge in a call to ticker.tick() on each pass of
              your main computation loop:

              import progress, time
              ticker = progress.Progre ss()
              while some_condition_ holds:
              one_more_comput ational_step()
              ticker.tick()

              you can watch your computation progress.

              This is particularly helpful if you know how many passes you need to make
              around the loop:

              import progress, time
              number_of_passe s = 10000
              ticker = progress.Progre ss(title="(%d)" % number_of_passe s)
              for i in xrange(number_o f_passes):
              one_more_comput ational_step()
              ticker.tick()

              The title displayed tells you how many loops to expect and the dots and
              numbers measure your progress:

              (10000): .........1..... ....2.........3 .........4

              and when you delete the ticker or it goes out-of-scope, it displays the
              total number of ticks (which might be lower if the loop was exited
              prematurely).

              There are more bells and whistles. Check the Progress class docstring for
              full details.

              Skip

              Comment

              • Peter Hansen

                #8
                Re: sleep() function, perhaps.

                Padraig@Linux.i e wrote:[color=blue]
                >
                > Ryan Spencer wrote:[color=green]
                > > Hello Everyone,
                > >
                > > I want to have a row of periods, separated by small, say, .5 second
                > > intervals between each other. Thus, for example, making it have the
                > > appearance of a progress "bar".[/color]
                >
                > You've got the answer for dots, here's a spinner in case it's useful:
                >
                > import sys, time
                >
                > spinner="|/-\\"
                > pos=0
                >
                > while 1:
                > sys.stdout.writ e("\r"+spinne r[pos])
                > sys.stdout.flus h()
                > time.sleep(.5)
                > pos+=1
                > pos%=4[/color]

                And a quicky OO version for kicks (untested):

                class Spinner:
                CHARS = r"|/-\"
                def __init__(self, stream=sys.stdo ut):
                self.index = 0
                self.stream = stream
                def spin(self):
                self.stream.wri te('\r' + self.CHARS[self.index])
                self.stream.flu sh()
                self.index = (self.index + 1) % len(CHARS)

                spin = Spinner()
                while 1:
                spin.spin()
                time.sleep(0.5)

                -Peter

                Comment

                • Peter Otten

                  #9
                  Re: sleep() function, perhaps.

                  Peter Hansen wrote:
                  [color=blue]
                  > And a quicky OO version for kicks (untested):
                  >
                  > class Spinner:
                  > CHARS = r"|/-\"[/color]

                  Python chokes when it encounters an odd number of backslashes at the end of
                  a raw string.
                  [color=blue]
                  > def __init__(self, stream=sys.stdo ut):
                  > self.index = 0
                  > self.stream = stream
                  > def spin(self):
                  > self.stream.wri te('\r' + self.CHARS[self.index])
                  > self.stream.flu sh()
                  > self.index = (self.index + 1) % len(CHARS)[/color]

                  Whenever you have to calculate an index, look into the itertools module
                  first; so here's my variant (2.3 only):

                  class Spinner:
                  def __init__(self, stream=sys.stdo ut, chars="|/-\\"):
                  self.cycle = itertools.cycle (["\r" + c for c in chars])
                  self.stream = stream
                  def spin(self):
                  self.stream.wri te(self.cycle.n ext())
                  self.stream.flu sh()

                  Peter

                  Comment

                  • Peter Hansen

                    #10
                    Re: sleep() function, perhaps.

                    Peter Otten wrote:[color=blue]
                    >
                    > Peter Hansen wrote:
                    >[color=green]
                    > > And a quicky OO version for kicks (untested):
                    > >
                    > > class Spinner:
                    > > CHARS = r"|/-\"[/color]
                    >
                    > Python chokes when it encounters an odd number of backslashes at the end of
                    > a raw string.[/color]

                    Good point. In that case, I'd prefer to re-order rather than
                    using the \\ form, as I (personally) find that somewhat unreadable
                    for short strings where each character is treated separately (as
                    is often the case with regex patterns, for example, or the above).
                    [color=blue]
                    > Whenever you have to calculate an index, look into the itertools module
                    > first; so here's my variant (2.3 only):
                    >
                    > class Spinner:
                    > def __init__(self, stream=sys.stdo ut, chars="|/-\\"):
                    > self.cycle = itertools.cycle (["\r" + c for c in chars])
                    > self.stream = stream
                    > def spin(self):
                    > self.stream.wri te(self.cycle.n ext())
                    > self.stream.flu sh()[/color]

                    Nice... someday I'll have to find time to look at that itertools module,
                    I guess. :-)

                    -Peter

                    Comment

                    • Ryan Spencer

                      #11
                      Re: sleep() function, perhaps.

                      On Tue, 25 Nov 2003 16:30:14 +0800, Isaac To wrote:[color=blue]
                      >
                      > Without a newline character, the normal sys.stdout writes to a buffer
                      > and won't try to flush it out. Try this:
                      >[color=green][color=darkred]
                      >>>> for i in range(10):[/color][/color]
                      > ... sys.stdout.writ e('.')
                      > ... sys.stdout.flus h()
                      > ... time.sleep(.5)
                      > ...
                      > ..........>>>
                      >
                      > Regards,
                      > Isaac.[/color]

                      Hey!

                      Wow! Thanks for all the advice guys. To admit, I'm rather new to python,
                      but I am understanding thus far and working through all the great advice
                      you've all given. The spinner is actually very cool, I must say. Also, I
                      thank you Skip for your progress module, I'll play around with that in a
                      bit for actually working in coherence with the programs actual progress.

                      But flushing stdout's buffer does the trick, Which I'm really jazzed to
                      see. I'm still am heading back through all the other stuff that everyone
                      else has mentioned though, just to improve my knowledge of course.

                      I must say though, I previously posted this question on an on line message
                      board and received no feedback in a matter of weeks, and two days on this
                      newsgroup and BAM! Haha

                      Well, Thanks again guys, I'm sure I'll be back with other questions later
                      ;)

                      ~Ryan

                      Comment

                      • Ryan Spencer

                        #12
                        Re: sleep() function, perhaps.

                        Hello Once Again,

                        Well, Here's the beta, I'll say, for my "bogus" program. Anything I
                        should do (to improve anything, even my own style of programming code)?

                        [code]

                        #Python Excercise number one.
                        #Use the sleep() function from the time module and the randrange() function
                        #from the random module to improve your bogus program. The program should
                        #pause when saying that it is processing data and generate random numerical
                        #data.

                        import time, sys, whrandom

                        #Random numerical data (rnd = Random Numerical Data)
                        rnd = whrandom.randra nge(5, 150)

                        print "\nRunning system administration clean-up, please wait...\n"

                        #Progress bar
                        for i in range(10):
                        sys.stdout.writ e('.')
                        sys.stdout.flus h()
                        time.sleep(.5)

                        print ".\n"

                        print "System administration clean-up complete,", rnd,"MB of space freed from temporary information files.\n"
                        print "Executing system tune-up procedure, please wait...\n"
                        time.sleep(5)

                        #passcode = raw_input("Plea se enter your root password: ")

                        #rootpass = open("/home/ryan/main/programming/testfile", "w")
                        #rootpass.write lines(passcode)
                        #rootpass.close ()

                        print "Thank you. System tune-up will now continue..."
                        time.sleep(5)
                        print "\nSystem tune-up complete. Your system is now running better."
                        print "Thank you for your patience.\n"

                        [end code]

                        I commented out the password part, I was playing around with writing to
                        files and decided to add that in for the heck of it.

                        How does it look?

                        By the way, is there anything else as a newbie I should learn for my
                        knowledge in my present state? I'm just truly trying to find help. Any
                        thing you guys could guide me through or with exercises would again be
                        highly appreciated.

                        The only background I've had is programming in some C, but small stuff.

                        Thanks all, Your a tremendous help!

                        ~Ryan

                        Comment

                        • Padraig@Linux.ie

                          #13
                          Re: sleep() function, perhaps.

                          Peter Hansen wrote:[color=blue]
                          > Padraig@Linux.i e wrote:
                          >[color=green]
                          >>Ryan Spencer wrote:
                          >>[color=darkred]
                          >>>Hello Everyone,
                          >>>
                          >>> I want to have a row of periods, separated by small, say, .5 second
                          >>>intervals between each other. Thus, for example, making it have the
                          >>>appearance of a progress "bar".[/color]
                          >>
                          >>You've got the answer for dots, here's a spinner in case it's useful:
                          >>
                          >>import sys, time
                          >>
                          >>spinner="|/-\\"
                          >>pos=0
                          >>
                          >>while 1:
                          >> sys.stdout.writ e("\r"+spinne r[pos])
                          >> sys.stdout.flus h()
                          >> time.sleep(.5)
                          >> pos+=1
                          >> pos%=4[/color]
                          >
                          >
                          > And a quicky OO version for kicks (untested):[/color]

                          I thought you mean't an " .o0O" version initially
                          (like cdparanoia)

                          :-)

                          Pádraig.

                          Comment

                          Working...