Question:
I have two Py files. I would like the first Py file to read through the second and find all the function names and docstrings so that I can turn it into an MD file for documentation. I made two very different attempts and I am just a little off. My first attempt’s code is unreadable, even by me, as I have no idea what it is doing. The second code is doing better, but I am still missing some stuff. @bigminiboss’ replit-bot does a similar thing. Repl link:
Please read the code and maybe fork these to understand what they’re doing. My first attempt My second attempt
@bigminiboss Why am I getting the docstring for str?
import other
for i in dir(other):
if (not i.startswith("__") and not i.endswith("__")):
print(i.__doc__ )# just string to GET the function do getattr(other, i)
import other
for i in dir(other):
if (not i.startswith("__") and not i.endswith("__")):
print(getattr(other, i).__doc__ )# just string to GET the function do getattr(other, i)
I got a code working that can generate an MD file for any imported Py file, whether local or installed via pip/poetry.
main.py:
import other
other.markdown(other)
other.py
def markdown(py_file, output_file:str='CoderElijah'):
'''This function creates an MD file with documentation on a Python Library from the docstrings. DO NOT INCLUDE THE FILE NAME EXTENSIONS!
`py_file`: The name of the Py file you wish to create documentation for. Use `CoderElijah` to get docs on the CoderElijah library.
`output_file`: The name of your file. If left blank, it will create `CoderElijah.md`.'''
md = ""
for i in dir(py_file):
if (not i.startswith("__") and not i.endswith("__")):
doc_string = getattr(py_file, i).__doc__
try:
doc_string = doc_string.split('\n')
docs = ""
for line in doc_string:
docs += line + "\n\n"
md += f'''<details><summary> The <code>{i}</code> function</summary>
{docs}</details>'''
except:
pass
with open(f'{output_file}.md', 'w') as file:
file.write(md)
I will get this added to my library so that I don’t have to create my own docs. Thanks for the help!