How to create MD documentation on a Py file

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. @bigminibossreplit-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

# There's no way I could fit a code snippet here.

To be honest, it sounds simple; if you don’t mind, I can whip up something for you.

I would love that. Thanks.

Probably best and most reliable to use the built-in syntax parser. Here are some docs for getting a docstring: ast — Abstract Syntax Trees — Python 3.11.2 documentation

EDIT: If you are using decorators like discord.py and bigminiboss’s replit-bot, all you need to do is <decorated func>.__doc__

I don’t know what I’m doing.

You are trying to get the docstring of an actual function, not the AST function. First, try saving the parsed AST, then, call print(ast.dump()) on it.

hi! Try doing things like:

getting the docstring:

import other

print(other.__doc__)

getting docstring of particular function:

import other

print(other.main.__doc__)

all functions:

for i in dir(other):
    if (not i.startswith("__") and not i.endswith("__")):
        print(i) # just string to GET the function do getattr(other, i)

@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)

like I said you should get getattr for that

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)
3 Likes

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!

Update: Added to CoderElijah v0.3.5 as auto_docs.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.