whitespace , comment stripper, and EOL converter

M.E.Farmer mefjr75 at hotmail.com
Fri Apr 22 07:54:29 EDT 2005


Hello Jean,
Glad to see your still playing along.
I have tested your script and it is broken too :(
Good idea about checking for the ':' , it just doesn't cover every
case.
This is the very reason I had not included docstring support before!
The problem is more diffcult than it first appears,
I am sure you have noticed ;)
Python is fairly flexible in it's layout and very dynamic in it's
execution.
This can lead to some hard to spot and hard to remove docstrings.

After staring at the problem for a day or so ( for the second time ),
*I am still stumped*

#####################################################################
# this is a test I have put together for docstrings
#####################################################################
"""This is a module doc it should be removed""" \
"This is really nasty but legal" \
'''Dang this is even worse''' + \
'this should be removed'#This is legal too
#####################################################################
assignment = \
"""
this should stay
so should this
"""
more_assignment = 'keep me,' \
           'keep me too,' \
           'keep me.'

#####################################################################
def func():
    'This should be removed' \
    """This should be removed"""
    pass
######################################################################
def funq(d = {'MyPass':
                """This belongs to a dict and should stay"""
                ,'MyOtherPass':
                'Kepp this string %s'\
"Keep this too" % 42 + """dfgffdgfdgdfg"""}):
    """This docstring is ignored""" + ''' by Python introspection
why?'''
    pass
######################################################################
def Usage():
    """This should be removed but how, removal will break the function.
This should be removed %s """# what do we do here
    return Usage.__doc__% '42'
######################################################################
class Klass:
    u"This should be removed" \
    ''' this too '''
    def __init__(self, num):
        """ This is should be removed but how ?  %d """ % num
        return None
    'People do this sometime for a block comment type of thing' \
"This type of string should be removed also"
    def func2(self):
        r'erase/this\line\sdfdsf\sdf\dfsdf'
        def inner():
            u'''should be removed'''
            return 42
        return inner
######################################################################
u'People do this sometime for a block comment type of thing' \
r"This type of string should be removed also" \
""" and this one too! """
# did I forget anything obvious ?
#####################################################################

When the docstring is removed it should also consume the blank line
that is left behind.
Got to go to work, I'll think about it over the weekend.
If anyone else wants to play you are welcome to join.
Can pyparsing do this easily?( Paul probably has a 'six-line' solution
tucked away somewhere ;)
M.E.Farmer




More information about the Python-list mailing list