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))
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: | |
Tags: |