Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you can import the function definitions, <a href="http://docs.python.org/dev/library/ast.html" rel="nofollow">walk AST</a> or use <a href="http://docs.python.org/dev/library/inspect.html#inspect.getargspec" rel="nofollow">inspect</a>.</p> <p>If you have more parsing to do beside signatures, consider <a href="http://pyparsing.wikispaces.com/" rel="nofollow">pyparsing</a> or <a href="http://code.google.com/p/funcparserlib/" rel="nofollow">funcparselib</a>.</p> <p>If you still need to use regexs, bear with me.</p> <pre><code>import re # Python identifiers start with a letter or _, #and continue with these or digits. IDENT = '[A-Za-z_][A-Za-z_0-9]*' # Commas between identifiers can have any amout of space on either side. COMMA = '\s*,\s*' # Parameter list can contain some positional parameters. # For simplicity we ignore now named parameters, *args, and **kwargs. # We catch the entire list. PARAM_LIST = '\((' + IDENT+'?' + '(?:' + COMMA+IDENT + ')*'+ ')?\)' # Definition starts with 'def', then identifier, some space, and param list. DEF = 'def\s+(' + IDENT + ')\s*' + PARAM_LIST ident_rx = re.compile(IDENT) def_rx = re.compile(DEF) def test(s): match = def_rx.match(s) if match: name, paramlist = match.groups() # extract individual params params = [x.group() for x in ident_rx.finditer(paramlist or '')] print s, name, params else: print s, 'does not match' test('def foo(a, b)') test('def foo()') test('def foo(a,b,c , d, e)') test('deff foo()') test('def foo(a, 2b)') </code></pre> <p>Note that the above code cannot handle parameters with default values, <code>*args</code> or <code>**kwargs</code>, or the trailing comma, let alone things like <code>def foo(a, (b, c))</code> legal in Python 2. This all can be added, but the complexity will soar.</p> <p>So, unless your case is rather simple (the code example above is borderline), see parser links above.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload