[Checkins] SVN: Sandbox/ulif/z3c-testsetup/trunk/src/z3c/testsetup/util. Add keyword-param finder.

Uli Fouquet uli at gnufix.de
Tue Feb 12 08:54:34 EST 2008


Log message for revision 83763:
  Add keyword-param finder.

Changed:
  U   Sandbox/ulif/z3c-testsetup/trunk/src/z3c/testsetup/util.py
  U   Sandbox/ulif/z3c-testsetup/trunk/src/z3c/testsetup/util.txt

-=-
Modified: Sandbox/ulif/z3c-testsetup/trunk/src/z3c/testsetup/util.py
===================================================================
--- Sandbox/ulif/z3c-testsetup/trunk/src/z3c/testsetup/util.py	2008-02-12 09:03:40 UTC (rev 83762)
+++ Sandbox/ulif/z3c-testsetup/trunk/src/z3c/testsetup/util.py	2008-02-12 13:54:33 UTC (rev 83763)
@@ -13,6 +13,7 @@
 ##############################################################################
 """Helper functions for testsetup.
 """
+from inspect import getmro, ismethod, getargspec
 from martian.scan import resolve
 
 def get_package(pkg_or_dotted_name):
@@ -28,3 +29,23 @@
     if isinstance(pkg, basestring):
         pkg = resolve(pkg)
     return pkg
+
+def get_keyword_params(cls, method_name):
+    """Get a list of args of a method of a class.
+
+    Get a list containing all names of keyword parameters, that are
+    passable to a method. To get a complete list, also inherited
+    classes are visited.
+    """
+    result = set()
+    for cls in getmro(cls):
+        init = getattr(cls, method_name, None)
+        if not ismethod(init):
+            # skip 'object'...
+            continue
+        # Add all keywords, omitting parameters, for which no default
+        # exists.
+        args, varargs, varkw, defaults = getargspec(init)
+        defaultlen = len(defaults)
+        result.update(args[-defaultlen:])
+    return list(result)

Modified: Sandbox/ulif/z3c-testsetup/trunk/src/z3c/testsetup/util.txt
===================================================================
--- Sandbox/ulif/z3c-testsetup/trunk/src/z3c/testsetup/util.txt	2008-02-12 09:03:40 UTC (rev 83762)
+++ Sandbox/ulif/z3c-testsetup/trunk/src/z3c/testsetup/util.txt	2008-02-12 13:54:33 UTC (rev 83763)
@@ -18,6 +18,9 @@
 
 The `util` module defines some helper functions of general use.
 
+get_package(pkg_or_dotted_name)
+-------------------------------
+
 For most classes in this package, that accept packages as arguments,
 these packages can be delivered as real packages or as strings
 containing dotted names. To get a package of something, that is either
@@ -42,4 +45,32 @@
    <module 'z3c.testsetup.tests.cave' from '...'>
 
 
+get_keyword_params(class, method_name)
+--------------------------------------
+
+The ``get_keyword_params()`` function tries to extract all keyword
+parameters of a given method of a class. If, for example, we want to
+know, which keyword parameters are supported by the constructor of the
+``DocTestSetup`` class, we can do this like so::
+
+   >>> from z3c.testsetup.doctesting import DocTestSetup
+   >>> from z3c.testsetup.util import get_keyword_params
+   >>> kw_list = get_keyword_params(DocTestSetup, '__init__')
+   >>> kw_list.sort()
+   >>> kw_list
+   ['encoding', 'extensions', 'filter_func', 'globs', 'optionflags',
+   'regexp_list', 'setup', 'teardown']
+
+The FunctionalDocTestSetup class supports some more keywords in its
+constructor::
+
+   >>> from z3c.testsetup.doctesting import FunctionalDocTestSetup
+   >>> kw_list2 = get_keyword_params(FunctionalDocTestSetup, '__init__')
+   >>> kw_list2.sort()
+   >>> kw_list2
+   ['checker', 'encoding', 'extensions', 'filter_func', 'globs',
+    'layer', 'layer_name', 'optionflags', 'regexp_list', 'setup',
+    'teardown', 'zcml_config']
+
+
 """



More information about the Checkins mailing list