当前位置: 动力学知识库 > 问答 > 编程问答 >

Python docstring parsing(and generally parsing languages in Python)

问题描述:

I would like to generate useful documentation for a REST API written in Python.

I imagine it should be something like this:

'''

/kittens/

This method creates kittens in the browser

HTTP Params:

format(default=json): The format in which kittens will be generated.

count(default=10): The number of kittens to generate.

Returns:

A list of kittens.

'''

My syntax is obviously:

program = dict

dict = repeated(name:string | name:dict)

I would like to parse this format and get (for example) a Python dict, preferably without using regex.

Is there a parser for such a syntax already available?

If not, then what is the simplest way of defining and parsing DSLs in Python?

Note that I would prefer not to use reStructuredText or any format different from what I described.

网友答案:

I'm not sure I understand exactly what you want, but reStructuredText can be used to capture the information in your example:

.. method:: kittens(format="json", count=10)

   Creates kittens in the browser.

   :param format: The format in which kittens will be generated.
   :param count: The number of kittens to generate.
   :return: A list of kittens

Sphinx understands this syntax, which is an extension of the basic reST markup defined by Docutils.

网友答案:

http://sphinx.pocoo.org/

Its a online documentation tool. Its awesome and it will do exactly what you want.

分享给朋友:
您可能感兴趣的文章:
随机阅读: