Compose docstrings with repeated arguments
Project description
Use DocHelper
to simplify your python docstrings
If you have been writing classes or functions with many shared arguments, you
might have going through the tedious and error prone process of copy-pasting,
searching-updating same docstring again and again. Passing through **kwargs
might not always be the solution not to say it's implicit and makes user to
look up multiple documents to find available arguments.
So how about one place to store and update them all? DocHelper
keeps the
docstrings for arguments in the same place and you only need to write the
template for each class or functions - it will compose and fill in the
template in Google format.
Works from Jupyter notebook to Sphinx where docstrings were read by importing.
Installation
Install using pip
(recommended)
pip install doc-helper
Install from source
# make sure you have setuptools and wheel installed
git clone https://github.com/ynshen/DocHelper.git
cd DocHelper
python setup.py bdist_wheel
pip install dist/the-wheel-file-of-your-version.whl
Usage
Initialize a DocHelper
with argument docstrings
from doc_helper import DocHelper
my_doc = DocHelper(
arg1='Just an simple argument', # defined as keyword arguments
arg2=('Also an simple argument'), # or a (not really) tuple
arg3=('int', 'This argument is integer') # tuple with data type
)
Add arguments when you needed
my_doc.add(
arg4=('pd.DataFrame', 'Just a new argument')
)
Use DocHelper.get()
to get formatted docstring for arguments
# Pass a list/tuple of argument names to `DocHelper.get()`
>>> my_doc.get(['arg1', 'arg3'])
arg1: Just an simple argument
arg3 (int): This argument is integer
# Pass a callable (function) to automatic find related arguments (except 'self')
def awesome_function(arg1, arg3):
pass
>>> my_doc.get(awesome_function)
arg1: Just an simple argument
arg3 (int): This argument is integer
Use decorator DocHelper.compose
to write template
Simply write a string with comma separated arguments name in <<
and >>
.
Default indents is 4, just add a number to change to desired indents.
# Example 1
@my_doc.compose("""This is my awesome function
Args:
<<arg1, arg2>>
arg3: This arg3 is special
""")
def awesome_function(arg1, arg2, arg3):
pass
>>> awesome_function.__doc__
This is my awesome function
Args:
arg1: Just an simple argument
arg2: Also an simple argument
arg3: This arg3 is special
# Example 2
@my_doc.compose("""This is another awesome function only takes arg1, arg3, and I want indent = 8
Args:
<<arg1, arg2, 8>>
""")
def another_awesome_function(arg1, arg3):
pass
>>> another_awesome_function.__doc__
This is another awesome function only takes arg1, arg3, and I wand indent = 8
Args:
arg1: Just an simple argument
arg3 (int): This argument is integer
# Example 3
@my_doc.compose("""Feeling lazy? left it blank to include all arguments (except 'self')
Args:
<<>>
""")
def another_another_awesome_function(arg1, arg2, arg3):
pass
>>> another_another_awesome_function.__doc__
This is my awesome function
Args:
arg1: Just an simple argument
arg2: Also an simple argument
arg3 (int): This argument is integer
TODO
Add docstring formatting function to subtract extra indents- Include different formatting (Numpy, reStructuredText)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for doc_helper-1.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 97923b8cce747193233668a3db5d9cc0b39f15e0b7405619514428ded5141655 |
|
MD5 | c695d94df1336e08349cc67b8910f364 |
|
BLAKE2b-256 | 24779218f4591a24e3f36ce2d1a917852479234e93d5c796c20abae7635fb701 |