[Tutor] findall()

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sat Jul 3 04:27:30 EDT 2004



On Sat, 3 Jul 2004 Dragonfirebane at aol.com wrote:

> I'm trying to get the following code to work:
>
> def getlog(activity,type,endtype):
>     log = open("Multivert_Log.txt","r")
>     for line in log.readlines():
>         ac = re.compile(activity)
>         fac = re.findall(ac, line)
>         if fac:
>             print line
>             break
>         else:
>             print "Uh-oh."
>     log.close()
>     logagain = open("Multivert_Log.txt","r")
>     ty = re.compile(type)
>     fty = re.findall(ty,line)
>     for line in logagain.readlines():
>         if not fty:
>             fty = re.findall(ty,line)
>         elif fty:
>             print line
>             break
>     logagain.close()
>     ex = re.compile("\d")
>     fex = re.findall(ex,line)
>     logain = open("Multivert_Log.txt","r")
>     for line in logain.readlines():
>         if not fex:
>             fex = re.findall(ex,line)
>         elif fex:
>             print line
>             break


Hi Dragonfirebane,


The code feels like it's doing too much.  Since you're asking us to help
debug the code, we have to do everything we can to understand the code;
this might involve changing the structure of the program so we don't have
to read so much at once.


I hope you don't mind if I split it up into multiple functions?  It may
help us to see what the code is really doing.  Here's a start:

######
def getlog(activity, type, endtype):
    tryToPrintActivityLine(activity)
    tryToPrintTypeLine(type)
    tryToPrintSomeDigit()


def tryToPrintActivityLine(activity):
    log = open("Multivert_Log.txt","r")
    for line in log.readlines():
        ac = re.compile(activity)
        fac = re.findall(ac, line)
        if fac:
            print line
            break
        else:
            print "Uh-oh."
    log.close()


def trytoPrintTypeLine(type):
    logagain = open("Multivert_Log.txt","r")
    ty = re.compile(type)
    fty = re.findall(ty,line)
    for line in logagain.readlines():
        if not fty:
            fty = re.findall(ty,line)
        elif fty:
            print line
            break
    logagain.close()


def tryToPrintSomeDigit():
    ex = re.compile("\d")
    fex = re.findall(ex,line)
    logain = open("Multivert_Log.txt","r")
    for line in logain.readlines():
        if not fex:
            fex = re.findall(ex,line)
        elif fex:
            print line
            break
######

I'm guessing that the code fits has three distinct blocks of function:
'activity', 'type', and something else that I haven't figured out yet.
*grin*

Each function does almost the same thing, but with suble differences.  In
fact, the variation in the three blocks is almost certainly buggy; the
third block tries to do a findall() search, but even before it reads in
'logain'.


As far as I can tell, the 'endtype' parameter passed into the original
getlog() function is never used, so I'd drop it: it's confusing to take in
parameter that aren't used.


There's probably a bug in the third regular expression:

    ex = re.compile("\d")

Use raw strings when you write regular expression patterns:

    ex = re.compile(r"\d")

There's an explanation of why backslashes are slightly complicated:

http://www.amk.ca/python/howto/regex/regex.html#SECTION000420000000000000000




> If, for example, [readlines():] returns:
>
> "CALCULATION
>
>
>
>
>
> EXPONENTS
>
>
>
> 1 ** 1 = 1
> ",
> the code poduces:
>
> "CALCULATION
>
>
>
> "


Ok, so you showed us a "experimental input", an an "experimental output"
from the program.  But what was the "expected output"?  That is, what did
you want to see?  Pretend that we don't know what it's supposed to do.

Hope this helps!




More information about the Tutor mailing list