Skip to content

Commit 36ab0c2

Browse files
author
ooreilly
committed
This commit adds support for pybind11 docstrings. It also contains several updates
explained below. * Add a tutorial to explain how to get started using the package. * Support PEP-484 annotations. * Allow type information to be passed to the parser. * Issue warnings for args that have no documentation. * Add several configurable options to the parser. * Fix extraction of multi-line function signatures * Fix over-loaded functions.
1 parent f8a3bd4 commit 36ab0c2

22 files changed

+1860
-223
lines changed

README.md

Lines changed: 20 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,24 @@
11
# MyDocstring
2-
[MyDocstring](README.md) is a small Python package that allows you to extract docstrings display them as either plain-text, [Markdown](http://commonmark.org/), or [JSON](https://www.json.org/) data.
2+
[MyDocstring](README.md) is a small Python package that allows you to extract and parse docstrings. It suited for building your own documentation system. Docstrings can be displayed as either plain-text, [Markdown](http://commonmark.org/), or [JSON](https://www.json.org/) data.
33

4-
* Support for Python-code (support for C-code is planned).
5-
* Support for [Google-style docstrings](http://google.github.io/styleguide/pyguide.html)
4+
* Supports [Python PEP484 type hints](https://www.python.org/dev/peps/pep-0484/).
5+
* Supports [pybind11](https://github.com/pybind/pybind11) docstrings.
6+
* Supports [Google-style docstrings](http://google.github.io/styleguide/pyguide.html).
67
* Produces [JSON](https://www.json.org/), plain-text, and [Markdown](http://commonmark.org/) output for modules, classes, functions, and
78
methods.
89

910
## Getting Started
10-
You can begin extracting and converting docstrings using the command line tool
11-
`mydocstring` that comes with package. Simply type `mydocstring --help` to see how to use it.
11+
If you are interested in building a documentation solution for your own
12+
project, a good place to
13+
start is this [tutorial](tutorials/begin.ipynb). This tutorial will teach you
14+
how to extract and parse docstrings.
1215

13-
Let's extract the docstring from the following example code and convert it to
14-
Markdown:
15-
```python
16-
def example_function(arg1, arg2=1):
17-
"""
18-
This is an example of a docstring that conforms to the Google style guide.
19-
The indentation uses four spaces (no tabs). Note that each section starts
20-
with a header such as `Arguments` or `Returns` and its contents is indented.
21-
22-
Arguments:
23-
24-
arg1 (`int`): This description for this argument fits on one line.
25-
arg2 (`int`, optional): This description is too long to fit on a
26-
single line. Note that it is continued by being indented.
27-
28-
Returns:
29-
30-
`bool` : Stating the return type here is optional.
31-
32-
We can continue putting explanations in this section as long as the text
33-
is indented.
34-
35-
This text is no longer indented and therefore not part of the `Returns`
36-
section.
37-
38-
Raises:
39-
40-
ValueError: This is exception is raised when arg1 and arg2 are equal.
41-
42-
"""
43-
if arg1 == arg2:
44-
raise ValueError("`arg1` and `arg2` cannot be equal.")
45-
if arg1 > arg2:
46-
return True
47-
else:
48-
return False
49-
```
50-
This example code can be found here: [examples/example.py](examples/example.py).
51-
52-
To convert to Markdown, we simply use
53-
```
54-
$ docstring examples/example.py example_function --markdown > examples/example_py.md
55-
```
56-
The following rendered Markdown is produced:
57-
58-
---
59-
# example_function
60-
```python
61-
def example_function(arg1, arg2=1):
62-
```
63-
64-
---
65-
66-
67-
This is an example of a docstring that conforms to the Google style guide.
68-
The indentation uses four spaces (no tabs). Note that each section starts
69-
with a header such as `Arguments` or `Returns` and its contents is indented.
70-
71-
## Arguments
72-
* **arg1** (`int`) : This description for this argument fits on one line.
73-
* **arg2** (`int`, optional) : This description is too long to fit on a
74-
single line. Note that it is continued by being indented.
75-
76-
77-
## Returns
78-
* Stating the return type here is optional.
79-
80-
81-
We can continue putting explanations in this section as long as the text
82-
is indented.
83-
84-
---
85-
This text is no longer indented and therefore not part of the `Returns`
86-
section.
87-
88-
## Raises
89-
* **ValueError** : This is exception is raised when arg1 and arg2 are equal.
90-
91-
92-
93-
94-
## Source
95-
```python
96-
def example_function(arg1, arg2=1):
97-
if arg1 == arg2:
98-
raise ValueError("`arg1` and `arg2` cannot be equal")
99-
if arg1 > arg2:
100-
return True
101-
else:
102-
return False
103-
104-
```
105-
106-
---
107-
The output above can also be found here: [examples/example_py.md](examples/example_py.md).
108-
109-
If you are not satisfied with the resulting Markdown, you can provide your own
110-
[mako](http://makotemplates.org) template
111-
112-
```
113-
$ docstring examples/example.py example_function --markdown --template customization.md
114-
```
115-
Go to [mydocstring/templates/](mydocstring/templates/) to see how to make your own
116-
template.
117-
118-
It is also possible to output plain-text, or JSON-data using the flags args
119-
`--text` and `--json`. Example output can be found here: [examples/](examples/).
16+
If you are after a complete documentation solution, then see the
17+
[showcase](#showcase) section that features solutions built on *MyDocstring*.
12018

19+
The project also comes with a command line tool that serves as an example of
20+
what you can build with the package. See [here](examples/README.md) for learning
21+
how to use the command line tool and to see some example output.
12122

12223
## Installation
12324
The package is available on the Python packaging index [PyPi](https://pypi.python.org/pypi) and can be installed via pip as follows.
@@ -127,7 +28,7 @@ $ pip install mydocstring
12728

12829
## Dependencies
12930
This project uses:
130-
* [docopt](http://docopt.org/) for the command-line interface application.
31+
* [docopt](http://docopt.org/) for the command line interface application.
13132
* [mako](http://www.makotemplates.org/) for producing markdown templates.
13233
* [pytest](https://docs.pytest.org/en/latest/) for testing.
13334

@@ -143,28 +44,19 @@ Otherwise, please submit a new issue using the [issue tracker](https://github.co
14344

14445
## Contributing
14546
Contributions are more than welcome. Please reach out via the issue tracker to
146-
discuss and also see [here](contributing.md) for
47+
discuss and also see [here](CONTRIBUTING.md) for
14748
some guidelines.
14849

14950
## Showcase
15051
If you end up using this tool in your project in one way or another. I would
15152
love to hear about it and showcase it here. Please go ahead and make a pull
15253
request.
15354

154-
## Acknowledgments
155-
These are some projects that inspired me to develop this tool.
156-
* [pdoc](https://github.com/BurntSushi/pdoc/) A tool for auto-generating API
157-
documentation for Python libraries.
158-
* [mkdocs](http://www.mkdocs.org/) Static site generator for building
159-
documentation using markdown.
160-
* [moxygen](https://github.com/sourcey/moxygen) Doxygen XML to Markdown
161-
converter.
162-
* [napoleon](https://pypi.python.org/pypi/sphinxcontrib-napoleon) Sphinx
163-
extension that can parse numpy and Google-style docstrings.
164-
* [pypydoxify](https://pypi.python.org/pypi/doxypypy/0.8.8.6) Converts Python
165-
comments into Doxygen's syntax.
166-
* [docsify](https://github.com/QingWei-Li/docsify/) Markdown-based documentation
167-
site generator.
55+
* [NetKet](https://www.netket.org) is a open-source project for machine learning and
56+
many-body quantum systems. It uses *mydocstring* for generating reference
57+
documentation from [pybind11](https://github.com/pybind/pybind11) docstrings.
58+
See
59+
[here](https://github.com/netket/netket/tree/v2.0/Docs) for the source.
16860

16961
## License
17062

examples.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mydocstring examples/example.py example_function --json >> examples/example.json
2+
mydocstring examples/example.py example_function --markdown >> examples/example.md

examples/README.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Command line tool
2+
To learn how to use the command line tool, simply type `mydocstring --help`.
3+
4+
Let's extract the docstring from the following example code and convert it to
5+
Markdown:
6+
```python
7+
def example_function(arg1, arg2=1):
8+
"""
9+
This is an example of a docstring that conforms to the Google style guide.
10+
The indentation uses four spaces (no tabs). Note that each section starts
11+
with a header such as `Arguments` or `Returns` and its contents is indented.
12+
13+
Arguments:
14+
15+
arg1 (`int`): This description for this argument fits on one line.
16+
arg2 (`int`, optional): This description is too long to fit on a
17+
single line. Note that it is continued by being indented.
18+
19+
Returns:
20+
21+
`bool` : Stating the return type here is optional.
22+
23+
We can continue putting explanations in this section as long as the text
24+
is indented.
25+
26+
This text is no longer indented and therefore not part of the `Returns`
27+
section.
28+
29+
Raises:
30+
31+
ValueError: This is exception is raised when arg1 and arg2 are equal.
32+
33+
"""
34+
if arg1 == arg2:
35+
raise ValueError("`arg1` and `arg2` cannot be equal.")
36+
if arg1 > arg2:
37+
return True
38+
else:
39+
return False
40+
```
41+
This example code can be found here: [example.py](example.py).
42+
43+
To convert to Markdown, we simply use
44+
```
45+
$ docstring examples/example.py example_function --markdown > examples/example_py.md
46+
```
47+
The following rendered Markdown is produced:
48+
49+
---
50+
# example_function
51+
```python
52+
def example_function(arg1, arg2=1):
53+
```
54+
55+
---
56+
57+
58+
This is an example of a docstring that conforms to the Google style guide.
59+
The indentation uses four spaces (no tabs). Note that each section starts
60+
with a header such as `Arguments` or `Returns` and its contents is indented.
61+
62+
## Arguments
63+
* **arg1** (`int`) : This description for this argument fits on one line.
64+
* **arg2** (`int`, optional) : This description is too long to fit on a
65+
single line. Note that it is continued by being indented.
66+
67+
68+
## Returns
69+
* Stating the return type here is optional.
70+
71+
72+
We can continue putting explanations in this section as long as the text
73+
is indented.
74+
75+
---
76+
This text is no longer indented and therefore not part of the `Returns`
77+
section.
78+
79+
## Raises
80+
* **ValueError** : This is exception is raised when arg1 and arg2 are equal.
81+
82+
83+
84+
85+
## Source
86+
```python
87+
def example_function(arg1, arg2=1):
88+
if arg1 == arg2:
89+
raise ValueError("`arg1` and `arg2` cannot be equal")
90+
if arg1 > arg2:
91+
return True
92+
else:
93+
return False
94+
95+
```
96+
97+
---
98+
The output above can also be found here: [example_py.md](example_py.md).
99+
100+
If you are not satisfied with the resulting Markdown, you can provide your own
101+
[mako](http://makotemplates.org) template
102+
103+
```
104+
$ docstring examples/example.py example_function --markdown --template customization.md
105+
```
106+
Go to [mydocstring/templates/](../mydocstring/templates/) to see how to make your own
107+
template.
108+
109+
It is also possible to output plain-text, or JSON-data using the flags args
110+
`--text` and `--json`.
111+
112+

examples/example_py.json renamed to examples/example.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
}
1919
],
2020
"header": "Arguments",
21-
"text": "\n"
21+
"text": ""
2222
},
2323
{
2424
"args": [
@@ -29,7 +29,7 @@
2929
}
3030
],
3131
"header": "Returns",
32-
"text": "\n\nWe can continue putting explanations in this section as long as the text\nis indented.\n"
32+
"text": "\nWe can continue putting explanations in this section as long as the text\nis indented.\n"
3333
},
3434
{
3535
"args": [],
@@ -39,13 +39,18 @@
3939
{
4040
"args": [
4141
{
42-
"description": "This is exception is raised when arg1 and arg2 are the same.",
42+
"description": "This is exception is raised when arg1 and arg2 are equal.",
4343
"field": "ValueError",
4444
"signature": ""
4545
}
4646
],
4747
"header": "Raises",
48-
"text": "\n\n"
48+
"text": ""
49+
},
50+
{
51+
"args": [],
52+
"header": "Examples",
53+
"text": "\nCode block 1.\n>>> a = 0\n>>> a\n0\n\nCode block 2.\n>>> b = 1\n>>> b\n1\n\n"
4954
},
5055
{}
5156
]

examples/example_py.md renamed to examples/example.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@ with a header such as `Arguments` or `Returns` and its contents is indented.
1616
* **arg2** (`int`, optional) : This description is too long to fit on a
1717
single line. Note that it is continued by being indented.
1818

19-
2019
## Returns
2120
* Stating the return type here is optional.
2221

23-
2422
We can continue putting explanations in this section as long as the text
2523
is indented.
2624

@@ -31,14 +29,31 @@ section.
3129
## Raises
3230
* **ValueError** : This is exception is raised when arg1 and arg2 are equal.
3331

32+
## Examples
33+
34+
Code block 1.
35+
36+
```python
37+
38+
>>> a
39+
0
3440

41+
```
42+
Code block 2.
43+
44+
```python
45+
46+
>>> b
47+
1
48+
49+
```
3550

3651

3752
## Source
3853
```python
3954
def example_function(arg1, arg2=1):
4055
if arg1 == arg2:
41-
raise ValueError("`arg1` and `arg2` cannot be equal")
56+
raise ValueError("`arg1` and `arg2` cannot be equal.")
4257
if arg1 > arg2:
4358
return True
4459
else:

0 commit comments

Comments
 (0)