Python code for documenting Python classes?
Kiss, Arpad
AKiss at GEOMETRIA.hu
Tue Jan 18 05:49:01 EST 2000
Hi,
Someone at starship.python.net(i don't remember his name) has a nice tool
for documenting Python sources.
Arpad Kiss
Michael Hudson <mwh21 at cam.ac.uk> wrote in message
news:<m3g0vvadwx.fsf at atrus.jesus.cam.ac.uk>...
> T. Bridgman <wtbridgman at radix.net> writes:
>
> > Just so I don't try to re-invent the wheel...
> >
> > Does anyone know of a Python program for documenting Python classes?
>
> No, but I've whipped something up. The script at the end of this post
> scans through the methods of a class and collects a list of attributes
> that are assigned to self.
>
> E.g:
>
> >>> class A:
> def __init__(self,a):
> self.b = a
> self.c = 1
> self.e = "orange"
> def meth2(a,b): # even works if you don't call it self!
> a.confuse = b
>
>
> >>> classdoc.do_class(A)
> ['b', 'c', 'confuse', 'e']
>
> It uses bytecodehacks, so get that here: (where I've just uploaded a
> new version with a couple fixes)
>
> http://starship.python.net/crew/mwh/
>
> (it could be written just using dis, but not as quickly)
>
> It only does the class in question; it could be trivially modified to
> recurse into base classes (but only Python ones, I'm afraid).
>
> This is a fairly maximal set of data members; you could just scan
> __init__ for a more minimal set. Trying to work out which members will
> always be set is a job for someone else (maybe someone who can solve
> the halting problem...).
>
> It also makes no attempt to determine the type of member (that would
> be *hard*), or format the output (this should be easy).
>
> enough-caveats-for-one-post-ly y'rs
> Michael
>
> --- this is classdoc.py:
>
> from bytecodehacks.code_editor import InstanceMethod
> from bytecodehacks.ops import *
> import types
>
> def do_method(meth):
> code = InstanceMethod(meth).im_func.func_code
> ops = code.co_code
> varnames = code.co_varnames
> names = code.co_names
>
> ans = []
>
> for i in range(len(ops)):
> op = ops[i]
> if op.__class__ is LOAD_FAST:
> if op.arg == 0: # self is the first posarg
> if ops[i+1].__class__ is STORE_ATTR:
> name = names[ops[i+1].arg]
> if name not in ans:
> ans.append(name)
>
> return ans
>
> def do_class(klass):
> ans = []
> for m in dir(klass):
> a = getattr(klass,m)
> if type(a) is types.MethodType:
> for i in do_method(a):
> if i not in ans:
> ans.append( i )
>
> ans.sort()
>
> --
> http://www.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list