Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This program will help you with the first part of your question, namely <strong>finding all functions that start with <code>test_</code></strong>. You might expand it to find variable definitions of your choice as well, but this is a little more complicated.</p> <p>The basic idea is to use the <code>ast</code> package to parse your Python source, and scan the result for function definitions. Thus, you won't detect <strong>false positives</strong>, for example a function definition inside of a string or comment. Example:</p> <pre><code># define strings for function test_foobar(). foobar="def test_foobar(): pass" </code></pre> <p>This input contains two bad matches and would be detected by using regular expressions, if not <em>very</em> carefully crafted. Using <code>ast</code>, this is much simpler than you may think. Working example:</p> <pre><code>#!/usr/bin/env python import ast import sys def test_foobar(): pass class FunctionNameFinder(ast.NodeVisitor): def visit_FunctionDef(self, node): if node.name.startswith("test_"): print node.name, "on line", node.lineno with open(sys.argv[1], 'rU') as f: FunctionNameFinder().visit(ast.parse("".join(f.readlines()))) </code></pre> <p>This program can be run on itself to detect the dummy function on line 6!</p> <p><strong>EDIT:</strong> The following extension will detect some variable assignments, but not all. Just to point out the direction.</p> <pre><code> def visit_FunctionDef(self, node): if node.name.startswith("test_"): print node.name, "on line", node.lineno self.generic_visit(node) def visit_Subscript(self, node): if isinstance(node.value, ast.Name) and \ isinstance(node.slice, ast.Index): if isinstance(node.slice.value, ast.Str): print '%s["%s"] on line %s' % ( str(node.value.id), str(node.slice.value.s), node.lineno) elif isinstance(node.slice.value, ast.Num): print '%s[%s] on line %s' % ( str(node.value.id), str(node.slice.value.n), node.lineno) </code></pre> <p>HTH.</p>
    singulars
    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. VO
      singulars
      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