[Tutor] Infinite recursion or too big a problem?
VanL
van@lindbergs.org
Wed, 04 Jul 2001 03:14:21 -0600
Hello,
Thanks to everyone who contributed insight about lambda functions. I
have a different, though related problem:
I am (re)writing a logfile analyzer for my work, and just for fun I am
doing it in a functional style.
Here is what I want to do:
Starting at a certain directory, find all files named 'log' and put
them in a list. I will analyze them later in the program.
(Yes, I know about os.path.walk, that is how I did it the first time. I
was trying something different). To give an idea of the scope of the
problem, there is not more than one logfile in each directory, and the
completed list would have between six and eight thousand items in it.
Now, just for fun, I am doing this in a functional style. (Probably bad
functional style; the result is the closest thing to obfuscated python
that I have ever seen!) When I run the code I wrote, it appears to
work, but after a minute or two I get a runtime error, "Maximum
recursion depth exceeded".
I think I wrote this correctly, but do I have infinite recursion that I
haven't caught somewhere?
Also, comments on style are appreciated.
Thanks,
Van
Begin code:::::::::
#!/usr/bin/env python
"""
Pylog, version 2. Written in functional style, just for fun.
"""
# Imports
import os, string
# Globals
# Force local evaluation of selected module functions
list_directory = os.listdir
is_directory = os.path.isdir
is_file = os.path.isfile
abs_path = os.path.abspath
string_find = string.find
# Functions
## my apply functions
do = lambda func: func()
doSequence = lambda functionlist: map(do, functionlist)
## identity functions
isDir = lambda fileobj: is_directory(fileobj)
isFile = lambda fileobj: is_file(fileobj)
isLogfile = lambda filename: (is_file(filename)) and (filename == 'log')
## list generating functions
concatenate = lambda seq1, seq2: seq1 + seq2
getFileList = lambda dir: list_directory(dir)
getDirectories = lambda fileobjs: filter(isDir, fileobjs)
getLogfiles = lambda fileobjs: filter(isLogfile, fileobjs)
getAllLogfiles = lambda dirname: reduce(concatenate, [],
getLogfiles(getFileList(abs_path(dirname))) +
map(getAllLogfiles, map(abs_path, getDirectories(getFileList(dirname)))))