Skip to content

Instantly share code, notes, and snippets.

@polyrand
Created July 27, 2020 20:48
Show Gist options
  • Save polyrand/fb4d945c2ab94797484a1a5817e70325 to your computer and use it in GitHub Desktop.
Save polyrand/fb4d945c2ab94797484a1a5817e70325 to your computer and use it in GitHub Desktop.
Write python docstrings in a separate text file.
# docd.py
def doc(f):
with open("docs.txt") as d:
docfile = d.read()
# save old docstring
if f.__doc__:
prev_doc = f.__doc__
f.__doc__ = prev_doc + "\n\n# docdoc\n" + docfile
else:
f.__doc__ = docfile
print(f.__doc__)
return f
# decorated.py
@doc
def myfunc():
"""Some docs before."""
return 1
# docs.txt
This are the docs
of the function
>>> print("a")
a
>>> print("b")
b
# RUN:
# python3 -m doctest -v dec.py
# OUTPUT:
# Some docs before.
#
# # docdoc
# This are the docs
#
# of the function
#
# >>> print("a")
# a
# >>> print("b")
# b
#
#
# Trying:
# print("a")
# Expecting:
# a
# ok
# Trying:
# print("b")
# Expecting:
# b
# ok
# 1 items had no tests:
# dec
# 1 items passed all tests:
# 2 tests in dec.myfunc
# 2 tests in 2 items.
# 2 passed and 0 failed.
# Test passed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment