Я пытаюсь проанализировать некоторый код с помощью AST, но у меня возникла проблема из-за символа продолжения обратной косой черты.
Когда у меня есть символ продолжения \, textwrap не сможет уменьшить вмятину в коде, хотелось бы знать, как от этого избавиться.
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))
Я добавляю дополнительную информацию, чтобы прояснить вопрос:
У меня есть модуль nemo.py с следующий контент:
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!"
и основной модуль пытается разобрать код:
import ast
import nemo
import inspect
import textwrap
code = str().join(inspect.getsourcelines(nemo.Foo.bar)[0])
ast.parse(textwrap.dedent(code))
И обратная трассировка:
Traceback (most recent call last):
File "/Users/kelsolaar/Documents/Development/Research/_BI.py", line 7, in
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 "", line 1
def bar(self):
^
IndentationError: unexpected indent
Подробнее здесь: https://stackoverflow.com/questions/133 ... -character
Удалить символ продолжения обратной косой черты ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение