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
#2
PEP 257 should help
https://www.python.org/dev/peps/pep-0257...ndentation
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Jul-01-2018, 02:20 PM)buran Wrote: PEP 257 should help
https://www.python.org/dev/peps/pep-0257...ndentation

Thanks for the fast reply. The document you pointed me to would be good if I were writing a docstring processing tool that simply looks at an existing source file to pick out the docstrings, but I am attempting to write a docstring generation tool where I would either generate docstrings from scratch or add more documentation to existing docstrings. I do not see how the code in the referenced section applies in that case unless I wanted to create my own Abstract Syntax Trees. I would much rather use the existing ast module (and others if necessary) to get the required information.

I have found that ast nodes have a member called 'lineno' that contains the line number of the node in the source file, so I may be able to search the source code starting at the line after looking for an existing docstring and figure out the indentation from there. I think a simple search for """ or ''' before the next node will be enough for that.

I will code that to determine if I can get it working. If there is a simpler solution, I would certainly appreciate hearing about it.
Reply
#4
(Jul-01-2018, 02:14 PM)jimo Wrote: How do I determine the indentation in general?
(Jul-01-2018, 04:03 PM)jimo Wrote: but I am attempting to write a docstring generation tool where I would either generate docstrings from scratch or add more documentation to existing docstrings

Probably I don't understand your question/goal. The document PEP257 describes the convention of how spaces are handled. If you want to blend your text/addition into existing docstring you certainly should take this into account. Otherwise your addition will be displaced compared to the rest of the docstring
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to determine pen color from an image? robie972003 2 2,376 Mar-24-2019, 10:06 PM
Last Post: robie972003
  determine if an number is in a list Dbeah 7 3,762 Nov-06-2018, 12:11 PM
Last Post: buran
  determine if an number is in a list Dbeah 1 2,229 Nov-04-2018, 04:50 PM
Last Post: stullis
  how to determine if an object is a number Skaperen 6 3,953 Jul-11-2018, 08:18 PM
Last Post: Skaperen
  Determine whether a method was overridden porton 6 6,107 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