Python Forum
How Do I Determine indentation in AST?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How Do I Determine indentation in AST?
#1
I am new to Python though I have many, many years of programming experience. Using Python 3.6.5 on Fedora 28, I am investigating automatic generation of docstrings. To start with, I have created the following script just to work out some of the details:

import ast
import astor
import inspect

source = '''
class cl:
    """This is doc for cl class"""

    pass'''

tree = ast.parse(source)
for node in ast.iter_child_nodes(tree):
    if isinstance(node, ast.ClassDef):
        body = node.body[0]
        body.value.s += "\n\n    More documentation."
        doc = ast.get_docstring(node)
        print(doc)
        print()

src = ast.dump(tree)
print(src)
print()
newsource = astor.to_source(tree)
print(newsource)
print()
newSrc = inspect.cleandoc(newsource)
print(newSrc)
At line 15, I am adding text to the docstring for the class and have included 4 spaces at the beginning of the text because that fits the existing source code. How do I determine the indentation in general?

Here is the output from running the script:

Output:
This is doc for cl class More documentation. Module(body=[ClassDef(name='cl', bases=[], keywords=[], body=[Expr(value=Str(s='This is doc for cl class\n\n More documentation.')), Pass()], decorator_list=[])]) class cl: """This is doc for cl class More documentation.""" pass class cl: """This is doc for cl class More documentation.""" pass
Reply


Messages In This Thread
How Do I Determine indentation in AST? - by jimo - Jul-01-2018, 02:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to determine pen color from an image? robie972003 2 2,421 Mar-24-2019, 10:06 PM
Last Post: robie972003
  determine if an number is in a list Dbeah 7 3,829 Nov-06-2018, 12:11 PM
Last Post: buran
  determine if an number is in a list Dbeah 1 2,271 Nov-04-2018, 04:50 PM
Last Post: stullis
  how to determine if an object is a number Skaperen 6 4,055 Jul-11-2018, 08:18 PM
Last Post: Skaperen
  Determine whether a method was overridden porton 6 6,171 Nov-14-2016, 09:51 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020