2

I'm trying to read python code (specifically, unit tests) as structured objects.

For example.

class ProjectA(unittest.TestCase):

    def testB(self):
        """
        hello world B
        """
        assert False

    def testA(self):
        """
        hello world
        """
        assert False

I will like to read this code file into an object a dict like this:

{
    'classes': [{'ProjectA': [__init__, testA, testB]}]
}

For which I can read testA's via testA['docstring'].


Basically, I'd like to get the structure of python code into an object for which I can parse.

What will something like this be called? (So I can read up about it)

Thank you!

2
  • Two routes that immediately come to mind: Instantiate the class and access the dunder methods in the namespace. Read the .py file containing the class into Python as a text file and extract information based on class and def keywords and triple quotes. Commented Aug 18, 2016 at 13:57
  • import <py_file> ; help(<py_file>) will give you automatically generated documentation for that file. It isn't the dict you are looking for but if you just want something to quickly see all the docstrings that is probably the way to go. Commented Aug 18, 2016 at 14:10

2 Answers 2

2

This is what the ast module is for -- generate abstract syntax trees of Python source:

>>> import ast
>>> source = '''import unittest
...
...
... class ProjectA(unittest.TestCase):
...
...     def testB(self):
...         """
...         hello world B
...         """
...         assert False
...
...     def testA(self):
...         """
...         hello world
...         """
...         assert False'''
>>> tree = ast.parse(source)
>>> for node in ast.walk(tree):
...    print node
...
<_ast.Module object at 0x103aa5f50>
<_ast.Import object at 0x103b0a810>
<_ast.ClassDef object at 0x103b0a890>
<_ast.alias object at 0x103b0a850>
<_ast.Attribute object at 0x103b0a8d0>
<_ast.FunctionDef object at 0x103b0a950>
<_ast.FunctionDef object at 0x103b0ab10>
<_ast.Name object at 0x103b0a910>
<_ast.Load object at 0x103b02190>
<_ast.arguments object at 0x103b0a990>
<_ast.Expr object at 0x103b0aa10>
<_ast.Assert object at 0x103b0aa90>
<_ast.arguments object at 0x103b0ab50>
<_ast.Expr object at 0x103b0abd0>
<_ast.Assert object at 0x103b0ac50>
<_ast.Load object at 0x103b02190>
<_ast.Name object at 0x103b0a9d0>
<_ast.Str object at 0x103b0aa50>
<_ast.Name object at 0x103b0aad0>
<_ast.Name object at 0x103b0ab90>
<_ast.Str object at 0x103b0ac10>
<_ast.Name object at 0x103b27d50>
<_ast.Param object at 0x103b02410>
<_ast.Load object at 0x103b02190>
<_ast.Param object at 0x103b02410>
<_ast.Load object at 0x103b02190>
Sign up to request clarification or add additional context in comments.

Comments

0

You can explore the class using inspect

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.