Skip to content

Instantly share code, notes, and snippets.

@EstebanMqz
Last active May 6, 2024 21:14
Show Gist options
  • Save EstebanMqz/95a2eba739c9a85a84b22ed31385e50f to your computer and use it in GitHub Desktop.
Save EstebanMqz/95a2eba739c9a85a84b22ed31385e50f to your computer and use it in GitHub Desktop.
Docstrings on every file.

Automatically create & write docstrings in scripts

Note: Their usage is intended to automate docstrings in cwd of several scripts with an .ext at once.

#Pre-built module
import glob 
import os

def docstring(repository, requirements, author, license, environment):
    {"""
    This function creates a docstring to introduce a script (.py, .txt, .rmd, .m , etc.)

    Parameters:
    ----------
    + repository: str
        Name of the repository.
    + script: str
        Name of the script where the docstring is introduced.
    + author: str
        Contact of the author(s) of the repository.
    + license: str
        License of the repository.
    + environment: str
        Remote of the script.
    Returns:
    -------
    + docstring: str
        Introductory docstring of a script in a repository.
    """}
    return (f"""# -- --------------------------------------------------------------------------------------------------  -- #       
# -- Repository: {repository}
# -- Requirements: {requirements}
# -- Author(s): {author}
# -- License: {license}
# -- Environment: {environment}
# -- --------------------------------------------------------------------------------------------------  -- #
\n""")
def write_docstring(docstring, script):
    {"""
    Inserts introductory docstring in scripts (.py, .txt, .rmd, .m , etc.) for the cwd.

    Parameters:
    ----------
    + docstring: str 
        The docstring to insert.
    + script: str
        The name of the script in cwd.

    Returns:
    -------
    * (docstring: str) + (script: str)
        The docstring inserted in the beginning of the .py script.    
    """}

    cwd = os.getcwd()
    path = os.path.join(cwd, script)

    with open(script, "r+") as f:
        old = f.read()

        if str(docstring) not in old:
            f.seek(0)
            f.write(str('{""" ') + docstring + str('"""} ') + '\r' + old)
        else:
            pass

    return print(str('Succesfully created docstring for: ') + os.path.join('.', script))
Iterables:
scripts = glob.glob('*.py') #Define scripts considered with their extensions.
docstrings = [fn.docstring('repo_name', scripts[i], author, 'CC BY 3.0', remote + 
              str(scripts[i])) for i in range(len(scripts))] #Create docstrings for all scripts in cwd.
              
[dt.write_docstring(docstrings[i], scripts[i]) for i in range(len(scripts))][0] 
#Write docstrings to the beginning of their respective scripts (if it doesn't exist).
Description
Type: Code Snippets ( .py )
Usage: Python scripts to automatically create and write docstrings in various scripts that share an .ext in cwd (.ipynb / .py / .txt / .rmd / .m / .md / .cs / etc...)
Author: Resume LinkedInBusiness Gmail Github GitLab
Tags: glob os
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment