I’m trying to parse some code with AST, but I’m having an issue because of backslash continuation character.
When I have a continuation character \, textwrap will not manage to dedent the code, I would like to know how to get rid of it.
code = """
def foo():
message = "This is a very long message that will probably need to wrap at the end of the line!\n \
And it actually did!"
"""
import textwrap
print textwrap.dedent(code)
import ast
ast.parse(textwrap.dedent(code))
I’m adding more details to clarify the question:
I have a module nemo.py with the following content:
class Foo(object):
def bar(self):
message = "This is a very long message that will probably need to wrap at the end of the line!\n \
And it actually did!"
and the main module trying to parse the code:
import ast
import nemo
import inspect
import textwrap
code = str().join(inspect.getsourcelines(nemo.Foo.bar)[0])
ast.parse(textwrap.dedent(code))
And the traceback:
Traceback (most recent call last):
File "/Users/kelsolaar/Documents/Development/Research/_BI.py", line 7, in <module>
ast.parse(textwrap.dedent(code))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ast.py", line 37, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
File "<unknown>", line 1
def bar(self):
^
IndentationError: unexpected indent
This is because you misunderstood what
textwrap.dedent()does.It only remove any common leading white spaces. In your case there’s no common leading white space, therefore nothing is removed.
Moreover, what you want is actually
\\instead of\n \in this case. This is because you actually want what is printed to be parsed.\\will print only one\and it’s what you want.\n \will print a new line within"..."clause which is invalid.Now consider this code:
In this case there is common leading white spaces, and hence they are removed.
Edit:
If you want to get rid of the
\all together, you can consider using"""My sentence"""formessageindef bar.