def remove_comments_and_docstrings(source):
io_obj = io.StringIO(source)<EOL>out = "<STR_LIT>"<EOL>prev_toktype = tokenize.INDENT<EOL>last_lineno = -<NUM_LIT:1><EOL>last_col = <NUM_LIT:0><EOL>for tok in tokenize.generate_tokens(io_obj.readline):<EOL><INDENT>token_type = tok[<NUM_LIT:0>]<EOL>token_string = tok[<NUM_LIT:1>]<EOL>start_line, start_col = tok[<NUM_LIT:2>]<EOL>end_line, end_col = tok[<NUM_LIT:3>]<EOL>if start_line > last_lineno:<EOL><INDENT>last_col = <NUM_LIT:0><EOL><DEDENT>if start_col > last_col:<EOL><INDENT>out += ("<STR_LIT:U+0020>" * (start_col - last_col))<EOL><DEDENT>if token_type == tokenize.COMMENT:<EOL><INDENT>pass<EOL><DEDENT>elif token_type == tokenize.STRING:<EOL><INDENT>if prev_toktype != tokenize.INDENT:<EOL><INDENT>if prev_toktype != tokenize.NEWLINE:<EOL><INDENT>if start_col > <NUM_LIT:0>:<EOL><INDENT>out += token_string<EOL><DEDENT><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>out += token_string<EOL><DEDENT>prev_toktype = token_type<EOL>last_col = end_col<EOL>last_lineno = end_line<EOL><DEDENT>return out<EOL>
Returns *source* minus comments and docstrings.

.. note:: Uses Python's built-in tokenize module to great effect.

Example::

    def noop(): # This is a comment
        '''
        Does nothing.
        '''
        pass # Don't do anything

Will become::

    def noop():
        pass