Skip to content

Commit 4392ed6

Browse files
author
Ossian O'Reilly
committed
Rename package.
1 parent b59e25a commit 4392ed6

File tree

15 files changed

+136
-214
lines changed

15 files changed

+136
-214
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ If you are contributing new code, then
1313
reports via `pylint my_feature.py --reports=n` and then enable again once all
1414
issues have been corrected. Make sure that you test all parts of your code
1515
using a coverage tool such as `py-cov`.
16-
3. Update [ACKNOWLEDGMENTS](ACKNOWLEDGMENTS.md) to make sure you get credit for
16+
3. Update [CONTRIBUTORS](CONTRIBUTORS.md) to make sure you get credit for
1717
your contribution.
1818
4. Update [README](README.md) to explain how to use your feature.
1919
5. Make all your commits to your feature branch and check that all tests pass.

MANIFEST.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include mydocstring/*.py
2+
include mydocstring/tests/*.py

README.md

Lines changed: 31 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -1,216 +1,81 @@
1-
# Docstring
2-
[Docstring](README.md) is a small Python package that allows you to extract docstrings from
3-
source code and display them as plain-text, markdown, or JSON data. It is meant
4-
to serve as a basic building-block for building customized documentation
5-
systems. Simply put, [Docstring](README.md) extracts and parses docstrings and
6-
gives you access to their data so you can decide what you want to do with it.
7-
8-
* Support for `Python` code (support `C` is planned).
1+
# 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.
3+
4+
* Support for Python-code (support for C-code is planned).
95
* Support for [Google-style docstrings](http://google.github.io/styleguide/pyguide.html)
106
* Produces [JSON](https://www.json.org/), plain-text, and [markdown](http://commonmark.org/) output for modules, classes, functions, and
117
methods.
128

13-
## Contents
14-
* [Getting Started](#getting-started)
15-
* [Usage](#usage)
16-
* [Python Examples](#python-examples)
17-
* [Installing](#installing)
18-
* [Dependencies](#dependencies)
19-
* [Troubleshooting](#troubleshooting)
20-
* [Contributing](#contributing)
21-
* [Showcase](#showcase)
22-
* [Pull requests](#pull-requests)
23-
* [Running tests](#running-tests)
24-
* [License](#license)
25-
269
## Getting Started
27-
Once [installed](#installing), you can begin extracting and converting docstrings using the
28-
command-line interface application `docstring`.
29-
30-
### Usage
31-
32-
```
33-
$ docstring --help
34-
docstring
35-
36-
Usage:
37-
docstring <file> <name> [-tmj] [-T=<tpl>]
38-
docstring -h | --help
39-
docstring --version
40-
41-
Options:
42-
-h --help Show help (this screen).
43-
--version Show version.
44-
-m --markdown Output extracted docstring as Markdown.
45-
-t --text Output extracted docstring as plain-text.
46-
-j --json Output extracted docstring as JSON.
47-
-T=<tpl> --template=<tpl> Set template for Markdown output.
48-
49-
Examples:
50-
Extract the module docstring
51-
docstring module.py . --markdown
52-
Extract a module function docstring
53-
docstring module.py function --markdown
54-
Extract a class docstring
55-
docstring module.py Class --markdown
56-
Extract a method docstring
57-
docstring module.py Class.method --markdown
58-
59-
Help:
60-
Please see the issue tracker for the Github repository:
61-
https://github.com/ooreilly/docstring
62-
```
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.
6312

13+
Let's extract the docstring from the following example code and convert it to
14+
markdown:
6415
```python
6516
def example_function(arg1, arg2=1):
6617
"""
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.
18+
This is an example of a Google-style docstring.
7019
7120
Arguments:
7221
7322
arg1 (`int`): This description for this argument fits on one line.
7423
arg2 (`int`, optional): This description is too long to fit on a
7524
single line. Note that it is continued by being indented.
76-
77-
Returns:
78-
79-
`bool` : Stating the return type here is optional.
80-
81-
We can continue putting explanations in this section as long as the text
82-
is indented.
83-
84-
This text is no longer indented and therefore not part of the `Returns`
85-
section.
86-
87-
Raises:
88-
89-
ValueError: This is exception is raised when arg1 and arg2 are equal.
90-
9125
"""
92-
if arg1 == arg2:
93-
raise ValueError("`arg1` and `arg2` cannot be equal.")
94-
if arg1 > arg2:
95-
return True
96-
else:
97-
return False
98-
```
99-
100-
#### Plain-text
101-
102-
Next, we extract the docstring for the function above and output it as
103-
plain-text by calling
104-
```
105-
$ docstring examples/example.py example_function --text > examples/example_py.txt
106-
26+
pass
10727
```
108-
Go to [examples/example.txt](examples/example_py.txt) to view the output.
109-
110-
#### Markdown
111-
To output markdown, we simply call
28+
A more detailed example code is found in [examples/example.py](examples/example.py).
11229

11330
```
114-
$ docstring examples/example.py --markdown > examples/example_py.md
115-
31+
$ docstring examples/example.py example_function --markdown > examples/example_py.md
11632
```
11733
Go to [examples/example_py.md](examples/example_py.md) to
118-
view the output.
119-
120-
#### Customization
121-
122-
If you are not satisfied with the resulting markdown, you can provide your own
34+
view the output. If you are not satisfied with the resulting markdown, you can provide your own
12335
[mako](http://makotemplates.org) template
36+
12437
```
12538
$ docstring examples/example.py example_function --markdown --template customization.md
126-
12739
```
128-
Go to [docstring/templates/](docstring/templates/) to see how to make your own
40+
Go to [mydocstring/templates/](mydocstring/templates/) to see how to make your own
12941
template.
13042

131-
#### JSON
132-
133-
Another possibility is to output JSON data (then its up to you how you to
134-
format the data)
135-
136-
```
137-
$ docstring examples/example.py --json > examples/example_py.md
138-
139-
```
140-
Go to [examples/example_py.json](examples/example_py.json) to
141-
view the output for this example.
43+
It is also possible to output plain-text, or JSON-data using the flags args
44+
`--text` and `--json`. Example output can be found [here](examples/).
14245

143-
### Installing
144-
Use setup tools or pip to install this package.
14546

47+
## Installation
48+
The package is available on the Python packaging index [PyPi](https://pypi.python.org/pypi) and can be installed via pip as follows.
14649
```bash
147-
$ pip install docstring
148-
```
149-
*or*
150-
```bash
151-
$ sudo python setup.py install
50+
$ pip install mydocstring
15251
```
15352

154-
### Dependencies
53+
## Dependencies
15554
This project uses:
15655
* [docopt](http://docopt.org/) for the command-line interface application.
15756
* [mako](http://www.makotemplates.org/) for producing markdown templates.
15857
* [pytest](https://docs.pytest.org/en/latest/) for testing.
15958

160-
### Troubleshooting
59+
## Issues
16160
If you are having problems extracting your docstrings, or parts of their content
162-
end up missing. Please make sure that your are only using spaces (no tabs).
163-
Four spaces should be used for each level of indentation as stated in PEP .
61+
end up missing, then please make sure that your are only using spaces (no tabs).
62+
Four spaces should be used for each level of indentation.
16463
Also, make sure that you conform to the [Google style
16564
guide](http://google.github.io/styleguide/pyguide.html) when writing your
166-
docstrings. It's easy to forget indentation or mess something else up!
167-
168-
That said, this tool likely breaks for some edge-cases and it is completely
169-
possible that you have discovered a bug. Please see the issue tracker to see if
170-
this problem is known. If not, please submit a new issue with a minimum example
171-
that introduces the problem and also what the excepted output is supposed to be.
65+
docstrings.
17266

67+
Otherwise, please submit a new issue using the [issue tracker](https://github.com/ooreilly/mydocstring/issues) and explain the problem.
17368

17469
## Contributing
175-
### Showcase
70+
Contributions are more than welcome. Please reach out via the issue tracker to
71+
discuss and also see [here](contributing.md) for
72+
some guidelines.
73+
74+
## Showcase
17675
If you end up using this tool in your project in one way or another. I would
17776
love to hear about it and showcase it here. Please go ahead and make a pull
17877
request.
17978

180-
## Pull requests
181-
You are more than welcome to contribute to this project. In fact, getting some help
182-
developing and maintaining this package would be fantastic! Please use the issue
183-
tracker to look for any issues you think you can resolve, or feel free to post a
184-
new issue if you want to suggest a new feature. Ideally, post a feature
185-
request to discuss it before you implement it and make a pull request.
186-
187-
If you are contributing new code, then
188-
1. First, fork the repository and create your own branch. Commit your changes to
189-
this branch.
190-
2. In your commit, please include a test case that demonstrates that your
191-
contribution works as expected using `pytest`.
192-
Having a test case will make it easier to review your code and therefore lead
193-
to your pull request being approved faster. Please also use `pylint` to check
194-
for issues. During development it is usually simplest to disable
195-
reports via `pylint my_feature.py --reports=n` and then enable again once all
196-
issues have been corrected. Make sure that you test all parts of your code
197-
using a coverage tool such as `py-cov`.
198-
3. Update [ACKNOWLEDGMENTS](ACKNOWLEDGMENTS.md) to make sure you get credit for
199-
your contribution.
200-
4. Update [README](README.md) to explain how to use your feature.
201-
5. Make all your commits to your feature branch and check that all tests pass.
202-
Then go ahead and submit a pull request!
203-
204-
## Running tests
205-
[Pytest](https://docs.pytest.org/en/latest/) is used for testing. Currently, the
206-
tests are quite limited in scope. More tests should definitely be developed to identify
207-
edge cases and facilitate future code refactoring.
208-
209-
To run the tests, go to the `docstring` directory and type
210-
211-
```bash
212-
$ pytest
213-
```
21479
## Acknowledgments
21580
These are some projects that inspired me to develop this tool.
21681
* [pdoc](https://github.com/BurntSushi/pdoc/) A tool for auto-generating API

gdocstr/templates/class.md

Lines changed: 0 additions & 7 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.

gdocstr/docstring.py renamed to mydocstring/docstring.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
"""
2-
docstring
2+
docstringout
33
44
Usage:
5-
docstring <file> <name> [-tmj]
6-
docstring -h | --help
7-
docstring --version
5+
docstringout <file> <name> [-tmj] [-T=<tpl>]
6+
docstringout -h | --help
7+
docstringout --version
88
99
Options:
1010
-h --help Show help (this screen).
1111
--version Show version.
12-
-m --markdown Render docstring as Markdown.
13-
-t --text Render docstring as text.
14-
-j --json Render docstring as JSON.
12+
-m --markdown Output extracted docstring as Markdown.
13+
-t --text Output extracted docstring as plain-text.
14+
-j --json Output extracted docstring as JSON.
15+
-T=<tpl> --template=<tpl> Set template for Markdown output.
1516
1617
Examples:
1718
Extract the module docstring
@@ -25,7 +26,7 @@
2526
2627
Help:
2728
Please see the issue tracker for the Github repository:
28-
https://github.com/ooreilly/docstring
29+
https://github.com/ooreilly/docstringout
2930
"""
3031
from docopt import docopt
3132
from . import command

gdocstr/extract.py renamed to mydocstring/extract.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ def find(self, pattern):
135135
signature = matches[0][2]
136136
indent = len(matches[0][3])
137137
docstring = remove_indent(matches[0][4], indent)
138-
source = textwrap.dedent('def ' + function + signature + ':\n' + matches[0][5])
138+
if self.dtype == 'function' or self.dtype == 'method':
139+
source = textwrap.dedent('def ' + function + signature + ':\n' +
140+
matches[0][5])
141+
else:
142+
source = ''
139143

140144
out = {}
141145
out['class'] = cls
@@ -155,7 +159,7 @@ class PyExtract(Extract):
155159
"""
156160

157161
def extract_function(self):
158-
pattern = (r'^\s*()def\s(%s)(\((?!self)[:=,\s\w]*\)):' % self.funcname
162+
pattern = (r'^\s*()def\s(%s)(\((?!self).*\)):.*' % self.funcname
159163
+ r'\n*(\s+)"""([\w\W]*?)"""\n((\4.*\n+)+)?')
160164
return self.find(pattern)
161165

@@ -166,7 +170,7 @@ def extract_class(self):
166170

167171
def extract_method(self):
168172
pattern = (r'class\s+(%s)\(?\w*\)?:[\n\s]+[\w\W]*?' % self.classname +
169-
r'[\n\s]+def\s+(%s)(\(self[:=\w,\s]*\)):\n' % self.funcname +
173+
r'[\n\s]+def\s+(%s)(\(self.*\)):.*\n' % self.funcname +
170174
r'(\s+)"""([\w\W]*?)"""\n((?:\4.*\n+)+)?')
171175
return self.find(pattern)
172176

mydocstring/fixtures/example.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
Module docstring
3+
"""
4+
5+
def function_with_docstring(arg1, arg2=True):
6+
"""Short description.
7+
Example of a function with a doc string.
8+
9+
Args:
10+
arg1(type): description for arg1.
11+
arg2 : description for arg2
12+
that spans multiple lines.
13+
14+
Returns:
15+
bool: `True` or `False`. Defaults to `True`.
16+
17+
The return section can also have a detailed description that spans
18+
multiple lines. It is important that this description is indented.
19+
20+
"""
21+
pass
22+
23+
class ExampleOldClass:
24+
"""
25+
An example of a docstring for an old-style class.
26+
"""
27+
28+
def __init__(self):
29+
"""
30+
Description of the __init__ function.
31+
32+
"""
33+
pass
34+
35+
def class_function_with_docstring(self, arg1, arg2):
36+
"""
37+
Description of a class function.
38+
39+
Args:
40+
arg1(type) : description for arg1.
41+
arg2 : description for arg1.
42+
43+
"""
44+
pass
45+
46+
class ExampleNewClass(object):
47+
"""
48+
An example of a docstring for a new-style class.
49+
"""
50+
def __init__(self):
51+
"""
52+
Example of a docstring for the init function.
53+
"""
54+
pass
55+
56+
def __init__(arg1):
57+
"""
58+
Example of docstring for a function with the same name as a method.
59+
"""
60+
pass

0 commit comments

Comments
 (0)