Q:

Using python to remove all strings and comments from python code

I'd like to count the uniqueness of my variable names in all the python code I've written.  To do so, I need to strip out all the keywords and reserved words (which are known), the comments and the strings.  For example, the following code:
''' long comment '''
for x in range(y, y+foo):
    print "Hello", 'world', '''lookout for the # tricky nest''', q # comment

should be stripped to the tokens of for, x, in, range, y, foo, print, q which can be further parsed using a known set of keywords.  Is this possible using the python engine itself (maybe with the ast module)? 

A:

This is my first time playing with the ast module, but it was relatively easy to collect all the object names referenced in a source:
import ast

class Visit(ast. NodeVisitor):
    def __init__(self):
        ast. NodeVisitor.__init__(self)
        self.s = set()
    def visit_Name(self,node):
        self.s.add(node.id)

with open('x.py') as f:
    a=ast.parse(f.read())
v = Visit()
v.visit(a)
print v.s

Where x.py was:
''' long comment '''
q=7
y=0
foo=10
for x in range(y,y+foo):
    print "Hello", 'world', '''lookout for the # tricky nest''', q # comment

Output:
set(['q', 'y', 'range', 'foo', 'x'])

Note that keywords aren't included already, but it does pick up the function name range.