[FEEDBACK] Is this script efficient...is there a better way?

Steve Holden sholden at holdenweb.com
Fri Sep 13 07:56:24 EDT 2002


"Bob X" <bobx at linuxmail.org> wrote ...
> Steve Holden wrote:
> > "Bob X" <bobx at linuxmail.org> wrote ...
> > [ ... ]
> >
> >>Thank you very much...your shorter script did work. The keywords are all
> >>"normal" english words that would not have any special characters. I
> >>only added a re.IGNORECASE (since case doesn't matter to me) after a
> >>quick search of the python docs.
> >>
> >
> > Was there any significant difference in the timings for the two
different
> > methods, and how does the number of different keywords affect the
timings?
> >
> I haven't tested...is there an easy way to time it...besides my going
> "that one ran faster"?
>
> However, I had a chance to run your RE version today and it did very
> well. BTW..the line that looked like a debug statement was just a visual
> for me to see the script was actually doing something. I put in a
> counter that prints "lines written:", a where "a" is the counter. I do
> have one question on that. It writes each line sequentially like:
>
> lines written: 1
> lines written: 2
> lines written: 3
>
> Can I just get it to do it in place? Where the numbers are just replaced
> instead of scrolling? Just a thought...
>

That shouldn't be too difficult. Somebody recently posted code for Python
"spinners", that could be the bases of your efforts. See [... googles ...]

Aargh. Apparently I didn't save any attribution for the attached -- it's not
my code, somebody else wrote this and published it in c.l.py, but I don't
have a record of who it was. As far as timing goes, just subtract
time.time() at the beginnig from time.time() at the end for a naiive
approach. If this doesn't show any differences  it's probnably not worth
measuring more closely...

import threading, time, sys


class Spinner( threading.Thread ):

    DELAY = 0.1
    DISPLAY = [ '|', '/', '-', '\\' ]

    def __init__( self, before='', after='' ):
        threading.Thread.__init__( self )
        self.before = before
        self.after = after

    def run( self ):
        write, flush = sys.stdout.write, sys.stdout.flush
        self.running = 1
        pos = -1
        while self.running:
            pos = (pos + 1) % len(self.DISPLAY)
            msg = self.before + self.DISPLAY[pos] + self.after
            write( msg )
            flush()
            write( '\x08' * len(msg) )
            time.sleep( self.DELAY )
        write( ' ' * len(msg) + '\x08' * len(msg) )
        flush()

    def stop( self ):
        self.running = 0
        self.join()


class BarberPole( Spinner ):
    DISPLAY = [ '|/__/__/__|', '|_/__/__/_|', '|__/__/__/|' ]

class HAL( Spinner ):
    DELAY = 0.2
    DISPLAY = [ ' ', '.', '+', '*', '+', '.', ' ', ' ' ]

class MySpin(Spinner):
 DELAY = 0.08
 DISPLAY = [' ', '.', ':', '!', "'"]

def test():

    spinner = MySpin("My own spinner: [", ']')
    spinner.start()
    time.sleep(5)
    spinner.stop()

    spinner = Spinner( "[syncing... ", "]" )
    spinner.start()
    time.sleep( 5.0 )
    spinner.stop()
    print "Disks synced."

    pole = BarberPole( "Shutting down: " )
    pole.start()
    time.sleep( 5.0 )
    pole.stop()
    print "You may now switch off your computer."

    hal = HAL( "[", "]  I'm sorry Dave" )
    hal.start()
    time.sleep( 5.0 )
    hal.stop()
    print "I'm afraid I can't do that."


if __name__ == '__main__':
    test()

Works well, though, and will show you how to update your message in place.

regards
-----------------------------------------------------------------------
Steve Holden                                  http://www.holdenweb.com/
Python Web Programming                 http://pydish.holdenweb.com/pwp/
Previous .sig file retired to                    www.homeforoldsigs.com
-----------------------------------------------------------------------






More information about the Python-list mailing list