Public
Authored by Pierre

Script to parse python code and retrieve a list of classes and functions

This is a simple script that takes a path to a python project and returns a list of symbols (classes and definitions) found in every python files.

Note: the script itself is compatible with both Python 2.7 and Python 3.x, but since it's using the Abstract Tree Syntax Python module (ast), it will need to be called using the same Python interpreter as the code it's parsing.

Example:

$ python3 python_symbol_parser.py ~/dev/requests
[{'begin_line': 14,                                                                             
  'begin_line_offset': 0,                                                                       
  'file': '/home/pierre/dev/requests/setup.py',                                                 
  'kind': 'class',                                                                                  
  'name': 'PyTest'},                                                                            
 {'begin_line': 17,                                                                             
  'begin_line_offset': 4,                                                                       
  'file': '/home/pierre/dev/requests/setup.py',                                                 
  'kind': 'function',                                                                               
  'name': 'initialize_options'},
(...)
 {'begin_line': 115,
  'begin_line_offset': 4,
  'file': '/home/pierre/dev/requests/tests/testserver/server.py',
  'kind': 'function',
  'name': '__exit__'},
 {'begin_line': 7,
  'begin_line_offset': 0,
  'file': '/home/pierre/dev/requests/docs/_themes/flask_theme_support.py',
  'kind': 'class',
  'name': 'FlaskyStyle'}]
python_parser.py 2.32 KB
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment