[Tutor] Find files without __doc__ strings

David david at abbottdavid.com
Mon May 18 20:02:38 CEST 2009


Lie Ryan wrote:
> David wrote:
>> spir wrote:
>>> Le Sat, 16 May 2009 21:46:02 -0400,
>>> David <david at abbottdavid.com> s'exprima ainsi:
>>>
>>>> I am doing an exercise in Wesley Chun's book. Find files in the
>>>> standard   library modules that have doc strings. Then find the
>>>> ones that don't, "the shame list". I came up with this to find the
>>>> ones with;
>
> why not __import__() it then test whether its .__doc__ is None?
>
> def test(filename):
>     if __import__(filename).__doc__ is None:
>         shame_list.append(filename)
>     else:
>         fame_list.append(filename)
>
Thanks Spir and Lie,
How about;

#!/usr/bin/python

import os
import glob
import os.path

shame_list = []
fame_list = []
pypath = "/usr/lib/python2.5/"

def grab_files():
    fnames = glob.glob(os.path.join(pypath, '*.py'))
    fnames.sort()
    sendall(fnames)

def sendall(fnames):
    for filename in fnames:
        filename = os.path.split(filename)
        filename = filename[1]
        filename = os.path.splitext(filename)
        filename = filename[0]
        test(filename)


def test(filename):
    try:
        if __import__(filename).__doc__ is None:
            shame_list.append(filename)
        else:
            fame_list.append(filename)
    except ImportError:
        pass

grab_files()
print 'Shame List: ', shame_list

How can I do;
def sendall(fnames):
[snip]
More efficient.

-- 
powered by Gentoo/GNU Linux
http://linuxcrazy.com



More information about the Tutor mailing list