[Tutor] findall()
Dragonfirebane at aol.com
Dragonfirebane at aol.com
Sat Jul 3 18:47:56 CEST 2004
In a message dated 7/3/2004 4:28:13 AM Eastern Standard Time,
dyoo at hkn.eecs.berkeley.edu writes:
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
######
That's fine. As long as it still does the same thing, it makes little
difference.
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*
The reason for the 'endtype' paramater is that activity can be either
CALCULATION or CONVERSION. If it is CONVERSION, type is the starting type (Binary, Dec
imal, Hexadecimal, or Text) and endtype is the type that the previous type
was converted to. However, in CALCULATION there is no need for endtype because
once you reach the precision of type, all thats left is the actual manipulation.
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
The pattern "\d" represents any digit, which is precisely what i want,
because that will allow it to pick up only the final manipulation (1 ** 1 = 1 in my
example) becuase those are the only times when a digit is present. However, I
have also tried using "[0-9]" with as little effect.
> 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!
The expected output was:
CALCULATION
EXPONENTS
1 ** 1 = 1
because each time a line is printed, the for loop is broken and no more lines
are printed, with the exception of the first time (where if fac is not
immediately reached, "Uh-oh." is currently printed, merely so i can see that the
findall() is working the way i want it to.)
Email: dragonfirebane at aol.com
AIM: singingxduck
Programming Python for the fun of it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20040703/ebc3052f/attachment.html
More information about the Tutor
mailing list