Skip to content

Instantly share code, notes, and snippets.

@xmnlab
Created November 6, 2019 21:36
Show Gist options
  • Save xmnlab/6889541d616ab4f6388749fc13ff9dfe to your computer and use it in GitHub Desktop.
Save xmnlab/6889541d616ab4f6388749fc13ff9dfe to your computer and use it in GitHub Desktop.
Doc String template
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from typing import Callable\n",
"import re\n",
"\n",
"\n",
"DOC_DEFAULT_PARAMS = \"\"\"Parameters\n",
"----------\n",
"a : str\n",
"b : str\"\"\"\n",
"\n",
"DOC_DEFAULT_RETURNS = \"\"\"Returns\n",
"------\n",
"expr\"\"\"\n",
"\n",
"\n",
"DOC_DEFAULT = \"\"\"\n",
"{default_params}\n",
"\n",
"{default_returns}\"\"\".format(\n",
" default_params=DOC_DEFAULT_PARAMS,\n",
" default_returns=DOC_DEFAULT_RETURNS\n",
")\n",
"\n",
"\n",
"\n",
"DOCSTRING_TEMPLATE_CONTENT = {\n",
" 'default': DOC_DEFAULT,\n",
" 'default_params': DOC_DEFAULT_PARAMS,\n",
" 'default_returns': DOC_DEFAULT_RETURNS\n",
"}\n",
"\n",
"\n",
"def update_docstring(docstring, context):\n",
" \"\"\"\n",
" Update docstring.\n",
" \n",
" Parameters\n",
" ----------\n",
" docstring : str\n",
" context : dict\n",
" \n",
" Returns\n",
" -------\n",
" new_docstring : str\n",
" \"\"\"\n",
" expr = re.compile('\\{\\s*\\w+\\s*\\}')\n",
" tags = expr.findall(docstring)\n",
" _docstring = docstring.splitlines()\n",
" \n",
" for i, line in enumerate(_docstring):\n",
" for tag in tags:\n",
" loc = line.find(tag)\n",
" if loc > -1:\n",
" template_name = tag[1:-1]\n",
" # update docstring with indentation\n",
" _docstring[i] = re.sub(\n",
" '^',\n",
" ' ' * loc,\n",
" tag.format(**{template_name:context[template_name]}),\n",
" flags=re.MULTILINE\n",
" )\n",
" return '\\n'.join(_docstring)\n",
"\n",
" \n",
"\n",
"def format_template_docstring(f: Callable):\n",
" \"\"\"Decorate a function and update its docstring.\n",
" \n",
" Parameters\n",
" ----------\n",
" f : Callable\n",
"\n",
" Returns\n",
" -------\n",
" Callable\n",
" \"\"\"\n",
" f.__doc__ = update_docstring(\n",
" f.__doc__, DOCSTRING_TEMPLATE_CONTENT\n",
" )\n",
" return f\n",
"\n",
"\n",
"@format_template_docstring\n",
"def func_a(a: str, b: str):\n",
" \"\"\"Concatenate `a` and `b`.\n",
" \n",
" {default}\n",
" \"\"\"\n",
" return a + b\n",
"\n",
"\n",
"@format_template_docstring\n",
"def func_b(a: str, b: str, c: str):\n",
" \"\"\"Concatenate `a` and `b` and `c`.\n",
" \n",
" {default_params}\n",
" c : str\n",
" \n",
" {default_returns}\n",
" \"\"\"\n",
" return a + b + c\n",
" \n",
"def func_c(a: str):\n",
" \"\"\"Return a empty string.\n",
" \n",
" Parameters\n",
" ----------\n",
" a : str\n",
" \n",
" Returns\n",
" -------\n",
" empty : str\n",
" Return an empty string.\n",
" \"\"\"\n",
" return ''\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[0;31mSignature:\u001b[0m \u001b[0mfunc_a\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mDocstring:\u001b[0m\n",
"Concatenate `a` and `b`.\n",
"\n",
"\n",
"Parameters\n",
"----------\n",
"a : str\n",
"b : str\n",
"\n",
"Returns\n",
"------\n",
"expr\n",
"\u001b[0;31mFile:\u001b[0m /mnt/sda1/dev/quansight/xmnlab/ivan-sandbox/bluetooth/<ipython-input-1-7936c4f3d32b>\n",
"\u001b[0;31mType:\u001b[0m function\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"func_a?"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[0;31mSignature:\u001b[0m \u001b[0mfunc_b\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mDocstring:\u001b[0m\n",
"Concatenate `a` and `b` and `c`.\n",
"\n",
"Parameters\n",
"----------\n",
"a : str\n",
"b : str\n",
"c : str\n",
"\n",
"Returns\n",
"------\n",
"expr\n",
"\u001b[0;31mFile:\u001b[0m /mnt/sda1/dev/quansight/xmnlab/ivan-sandbox/bluetooth/<ipython-input-1-7936c4f3d32b>\n",
"\u001b[0;31mType:\u001b[0m function\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"func_b?"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[0;31mSignature:\u001b[0m \u001b[0mfunc_c\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mDocstring:\u001b[0m\n",
"Return a empty string.\n",
"\n",
"Parameters\n",
"----------\n",
"a : str\n",
"\n",
"Returns\n",
"-------\n",
"empty : str\n",
" Return an empty string.\n",
"\u001b[0;31mFile:\u001b[0m /mnt/sda1/dev/quansight/xmnlab/ivan-sandbox/bluetooth/<ipython-input-1-7936c4f3d32b>\n",
"\u001b[0;31mType:\u001b[0m function\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"func_c?"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment