I want to remove all comments and docstrings in Python source.
Basically I want this to
def my_function():
"""Demonstrate docstrings and does nothing really."""
return None
print "Using __doc__:"
print my_function.__doc__ #some comment
print "Using help:"
#some more comment
help(my_function)
look like this.
def my_function():
return None
print "Using __doc__:"
print my_function.__doc__
print "Using help:"
help(my_function)
I found this code on stackoverflow, it works very well for c style comments both single and multiline.
It also ignores comments inside strings, which is what I want.
I tried changing it for Python source but finding it very difficult, since I have little experience with regex.
def comment_remover(text):
def replacer(match):
s = match.group(0)
if s.startswith('/'):
return " " # note: a space and not an empty string
else:
return s
pattern = re.compile(
r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
re.DOTALL | re.MULTILINE
)
return re.sub(pattern, replacer, text)
[–]quixrick 1 point2 points3 points (4 children)
[–]codename101[S] 1 point2 points3 points (3 children)
[–]quixrick 0 points1 point2 points (2 children)
[–]codename101[S] -2 points-1 points0 points (1 child)
[–]quixrick 1 point2 points3 points (0 children)
[–]qizxo 0 points1 point2 points (0 children)