I am going to write a set of scripts, each independent from the others but with some similarities. The structure will most likely be the same for all the scripts and probably looks like:
# -*- coding: utf-8 -*-
"""
Small description and information
@author: Author
"""
# Imports
import numpy as np
import math
from scipy import signal
...
# Constant definition (always with variable in capital letters)
CONSTANT_1 = 5
CONSTANT_2 = 10
# Main class
class Test():
def __init__(self, run_id, parameters):
# Some stuff not too important
def _run(self, parameters):
# Main program returning a result object.
For each script, I would like to write documentation and export it in PDF. I need a library/module/parser which reads the scripts, extracts the noted comment, code and puts it back together in the desired output format.
For instance, in the _run()
method, there might be several steps detailed in the comments:
def _run(self, parameters):
# Step 1: we start by doing this
code to do it
# Step 2: then we do this
code to do it
code
code # this code does that
Which library/parser could I use to analyze the python script and output a PDF? At first, I was thinking of sphinx, but it is not suited to my need as I would have to design a custom extension. Moreover, sphinx strength lies in the links and hierarchy between multiple scripts of a same or of different modules. In my case, I will only be documenting one script, one file at a time.
Then, my second idea is to use the RST format and RST2PDF to create the PDF. For the parser, I could then design a parser which reads the .py
file and extract the commented/decorated lines or set of lines as proposed below, and then write the RST file.
#-description
## Title of something
# doing this here
#-
#-code
some code to extract and put in the doc
some more code
#-
Finally, I would also like to be able to execute some code and catch the result in order to put it in the output PDF file. For instance, I could run a python code to compute the SHA1 hash of the .py
file content and include this as a reference in the PDF documentation.