David wrote:

------------------------------------------------------------------------

Subject:
Re: [Tutor] Find files without __doc__ strings
From:
David <[email protected]>
Date:
Mon, 18 May 2009 14:02:38 -0400

CC:
[email protected]


Lie Ryan wrote:
David wrote:
spir wrote:
Le Sat, 16 May 2009 21:46:02 -0400,
David <[email protected]> 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.

I don't know about being "more efficient" but using os.path.basename() can save one step:
filename = os.path.splitext(os.path.basename(filename))[0]

and you may try to use os.path.walk() for your grab_files(). os.path.walk will walk recursively through subdirectories and grabs other stdlib packages you may have missed (note: although it also grabs non standard-lib packages; note: you also cannot use glob with it; these are tradeoffs, so find out which is easier for you)

Also in this code:

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

avoid globals...

_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to