[Tutor] Problems with Gauge Bar.

Alan Gauld alan.gauld at btinternet.com
Sun Aug 10 15:51:32 CEST 2008

"Olrik Lenstra" <o.lenstra at gmail.com> wrote

> This is the part I tried to get the Bar in. I actually got it in GUI 
> wise.
> But I had no idea to let it run the moment I clicked "Scan"

OK, Normally you have to update the bar values from inside a
loop on your Scan method. In practice you probably need to
use a timer to relinquish control back to the GUI so that the
Guage refreshes itself.  In pseudo code:

def onScan(self):
     self.myfile = open('foo.txt')
     self.count = 0
     self.setTimer(0.01, self.processLine)

def processLine(self)
    line = self.myfile.readline()
    if line:
        self.count += 1
        self.setTimer(0.01, self.processLine)

This also allows the user to use the GUI while the scan is running

> Alan, I'm using pyinstaller. is it possible to make several python 
> files
> into a single exe?

Yes, it must be since every time you import a module you are
using another file. Its quite hard to write any useful application
in Python without importing at least one module. Your files are
no different to the standard modules like os, sys, time, etc...

> If so, Can you direct me to maybe some documentation? (I
> might have overlooked it.)

Sorry, I've never used pyinstaller but if it can't do this it isn't
of much use!

> while I'm asking for the bar, can I ask for some advice on the 
> following bit
> of code too?

Of course! :-)

> ## Define all the private IP ranges that we're not going to filter 
> in
> ## the ipconfig part of the program.
> ## From private4 to private19 is actually 1 range. (Needs to be 
> looked at.)
> ##------------------------------------------------------------------------------
> private1 = r"192\.168\.\d+\.\d+"
> private2 = r"169\.254\.\d+\.\d+"
> private3 = r"10\.\d+\.\d+\.\d+"
> ...
> private19 = r"172\.31\.\d+\.\d+"

Why not put them in a list or tuple called privates?
It will save typing and:

>    lines = file('ipconfig.txt', "r+").readlines()
>    for i in range(len(lines)):
>        if re.search("IP\D+ \:", lines[i]):
>            if not re.search(private1, lines[i]):
>                if not re.search(private2, lines[i]):
> ....
>                     if not re.search(private19, lines[i]):

The if not search lines could then be replaced by a loop.
This has the added advantage that you can change the
list of  privates(adding or deleting entries)) without
changing this code.

    lines = file('ipconfig.txt', "r+").readlines()
    for i in range(len(lines)):
        if re.search("IP\D+ \:", lines[i]):
             for p in patterns:
                 if search(p,lines[i]):
                 # equivalent of the end of your chain

Another approach would be to compbine all the private IP
addresses into a single long regex. You can then do a
single search for the pattern. This would be faster but
slightly harder to read/debug.

Choosing the right data structure is half the battle in
writing clean code. If you find yourself writing code that is
obviously cumbersome go back and think about whether
you can design a better data struvcture.


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list