Quick Reference from module doc strings.

Ron Adam rrr at ronadam.com
Mon May 16 00:16:31 EDT 2005


Scott David Daniels wrote:

> Althought object is a horrible name for your own value (there is a builtin
> object which you use for defining new-style classes), you probably want:

Good point, I agree.  It's a bad habit to start, sooner or later it 
would cause a problem.  I'll find something else to call it.

>      if isinstance(object, (str,int,bool,tuple,list,dict)):
>          ...
> or (as John Machin was trying to suggest):
> 
>      if isinstance(object, TYPES_WHICH_whatever):
>          ...
> 
> This allows you to use instances of those builtin types and any
> user-defined subtypes.
> 
> 
>>Thanks, I don't need the isinstance(), type works here just as well.
> 
> 
> But the isinstance version is better than the type(...) in ... version.

Ok, done.  I didn't think to look it up in the quick reference this 
prints out.  LOL,  Thanks Scott  :-)

I'm not sure if isinstance() would make a difference, it doesn't for 
__builtins__, but it might in some other modules.

> --Scott David Daniels
> Scott.Daniels at Acm.Org


Do you have any feature suggestions, additional information that could 
go in, something that would extend the content in some way and make it 
more useful?

As it stands now, it could be just a module, so you could...


Regards,
_Ron



The current version is now....


# qref.py
"""
Generate a quick reference for a module by analyzing
a modules contents.

Use example:
     import qref
     import sys
     print qref.quickref("sys")
"""

def getobjs(obj, dlist=[], lvl=0, maxlevel=1):
     """ Retreive a list of sub objects from an object. """
     if obj not in dlist:
         dlist.append(obj)
     if lvl<maxlevel:
         dobj = dir(eval(obj))
         for item in dobj:
             try:
                 dlist = getobjs(obj+'.'+item, dlist, lvl+1)
             except:
                 pass
     return dlist

def printdoc(objectlist):
     """ Return a sorted printable quick reference
         guide from a list of objects. """
     outtext = []
     objectlist.sort(lambda x, y: cmp(x.lower(), y.lower()))
     for obj in objectlist:
         objt = eval(obj)
         objt_type = type(objt)
         outtext.append('-'*40+'\n')
         outtext.append(str(obj)+'\n')
         if hasattr(objt, '__module__'):
             outtext.append("Module:"+str(objt.__module__)+'\n')
         if hasattr( objt,'__class__'):
             outtext.append("Class:"+str(objt.__class__)+'\n\n')
         else:
             outtext.append("Type:"+str(objt_type)+'\n\n')
         if isinstance(objt,str):
             if len(objt)>200:
                 s = objt[0:200]+"......"
             else:
                 s = objt
             outtext.append(obj+'=')
             if '\n' in s: quotes='"""'
             else: quotes ='"'
             if len(s)>60: print
             outtext.append(quotes+s+quotes+'\n\n')
         elif isinstance(objt,(int,bool,tuple,list,dict)):
             s = str(objt)
             if len(s)<200:
                 outtext.append(obj+'='+s+'\n\n')
             else:
                 outtext.append(obj+'='+s[0:200]+'......\n\n')
         if hasattr(objt,'__doc__'):
             if objt.__doc__ != type(objt).__doc__:
                 outtext.append(str(objt.__doc__)+'\n\n')
     outtext.append('-'*40+'\n')
     return ''.join(outtext)

def quickref(name):
     """
     quickref(module_name) -> printable string

     Generate a sorted quick reference guide from an objects
     doc strings.  The module_name is a string with the name of
     the module or class to get documents string from.

     Example:
         import os
         print quickref('os')
     """
     objlist = getobjs(name)
     return printdoc(objlist)

if __name__ == "__main__":
     print quickref('__builtins__')













More information about the Python-list mailing list