Package pyraf :: Module describe
[hide private]
[frames] | no frames]

Source Code for Module pyraf.describe

  1  # http://www.dejanews.com/getdoc.xp?AN=382948703 
  2  # 
  3  # Instant Python 
  4  # $Id: describe.py 678 2005-07-01 21:19:43Z perry $ 
  5  # 
  6  # utilities to describe functions, methods, and classes 
  7  # 
  8  # history: 
  9  # 96-10-27 fl     created 
 10  # 98-02-24 fl     added code to handle unpacked arguments 
 11  # 01-11-13 rlw    added UNPACK_SEQUENCE to UNPACK_TUPLE for tuple args 
 12  #                 (Changed for Python2.0) 
 13  # 
 14  # notes: 
 15  # This has been tested with Python 1.4 and 1.5.  The code and 
 16  # function object attributes might change in future versions of 
 17  # Python. 
 18  # 
 19  # written by fredrik lundh.  last updated february 1998. 
 20  # 
 21  # fredrik@pythonware.com 
 22  # http://www.pythonware.com 
 23  # 
 24   
 25  import string 
 26  from dis import opname, HAVE_ARGUMENT 
 27   
 28  # -------------------------------------------------------------------- 
 29  # code object attributes 
 30  # -------------------------------------------------------------------- 
 31  # co_argcount   INT 
 32  # co_nlocals    INT 
 33  # co_flags      INT 
 34  #       CO_OPTIMIZED 
 35  #       CO_NEWLOCALS 
 36  #       CO_VARARGS 
 37  #       CO_VARKEYWORDS 
 38  # co_code       OBJECT 
 39  # co_consts     OBJECT 
 40  # co_names      OBJECT 
 41  # co_varnames   OBJECT 
 42  # co_filename   OBJECT 
 43  # co_name       OBJECT 
 44   
 45  # -------------------------------------------------------------------- 
 46  # function object attributes 
 47  # -------------------------------------------------------------------- 
 48  # func_code     OBJECT 
 49  # func_globals  OBJECT 
 50  # func_name     OBJECT (__name__) 
 51  # func_defaults OBJECT 
 52  # func_doc      OBJECT (__doc__) 
 53   
 54  # copied from Python header file 
 55  CO_OPTIMIZED = 0x0001 
 56  CO_NEWLOCALS = 0x0002 
 57  CO_VARARGS = 0x0004 
 58  CO_VARKEYWORDS = 0x0008 
 59   
60 -def describeParams(func, name = None):
61 # get argument list 62 63 code = func.func_code 64 65 n = code.co_argcount 66 a = list(code.co_varnames[:n]) 67 p = 0 68 for i in range(n): 69 # anonymous arguments 70 c = code.co_code 71 if not a[i] or a[i][0] == ".": 72 vars = [] 73 while p < len(c): 74 v = ord(c[p]) 75 if v >= HAVE_ARGUMENT: 76 s, v = opname[v], ord(c[p+1]) + ord(c[p+2])*256 77 p = p + 3 78 if s in ("UNPACK_SEQUENCE", "UNPACK_TUPLE"): 79 count = v 80 elif s == "STORE_FAST": 81 vars.append(code.co_varnames[v]) 82 if len(vars) >= count: 83 break 84 else: 85 p = p + 1 86 if vars: 87 a[i] = "(" + string.join(vars, ", ") + ")" 88 if func.func_defaults: 89 # defaults 90 i = n - len(func.func_defaults) 91 for d in func.func_defaults: 92 a[i] = (a[i], d) 93 i = i + 1 94 if code.co_flags & CO_VARARGS: 95 # extra arguments 96 a.append("*"+code.co_varnames[n]) 97 n = n + 1 98 if code.co_flags & CO_VARKEYWORDS: 99 # extra keyword arguments 100 a.append("**"+code.co_varnames[n]) 101 n = n + 1 102 return a
103
104 -def describe(func, name = None):
105 "Return the function or method declaration as a string" 106 107 # argument list 108 a = describeParams(func) 109 args = [] 110 for arg in a: 111 if type(arg) == type(""): 112 args.append(arg) 113 else: 114 args.append("%s=%s" % (arg[0], repr(arg[1]))) 115 args = string.join(args, ", ") 116 117 # function name 118 if not name: 119 # func_name is considered obsolete, use __name__ instead 120 # name = func.func_name 121 name = func.__name__ 122 if name == "<lambda>": 123 return "lambda %s" % args 124 return "%s(%s)" % (name, args)
125
126 -def __getmethods(c, m):
127 for k, v in c.__dict__.items(): 128 if type(v) == type(__getmethods): # and k[0] != "_": 129 if not m.has_key(k): 130 m[k] = describe(v, k), c.__name__ 131 for c in c.__bases__: 132 __getmethods(c, m)
133
134 -def describe_class(cls):
135 "Return a dictionary describing all methods available in a class" 136 137 m = {} 138 __getmethods(cls, m) 139 return m
140
141 -def describe_instance(self):
142 "Return a dictionary describing all methods available in an instance" 143 144 return describe_class(self.__class__)
145 146 # 147 # -------------------------------------------------------------------- 148 149 if __name__ == "__main__": 150
151 - def foo(a, b=1, *c, **d):
152 e = a + b + c 153 f = None
154 155 bar = lambda a: 0 156 157 # from Duncan Booth
158 - def baz(a, (b, c) = ('foo','bar'), (d, e, f) = (None, None, None), g = None):
159 pass
160 161 print "describeParams(foo)", describeParams(foo) 162 print "describeParams(bar)", describeParams(bar) 163 print "describeParams(baz)", describeParams(baz) 164 165 print describe(foo) 166 print describe(bar) 167 print describe(baz) 168