Skip to main content

A simple tool for transforming Python docstrings by embedding results from Python function calls

Project description

🏳️‍⚧️ Transdoc 🏳️‍⚧️

A simple tool for transforming Python docstrings by embedding results from Python function calls.

Installation

pip install transdoc

Usage

Creating transformation rules is as simple as defining Python functions.

# rules.py
def my_rule() -> str:
    '''
    A simple rule for rewriting docstrings
    '''
    return f"This text was added by Transdoc!"

They can then be used by placing their name within {{ double braces }} in any docstring.

# program.py
def say_hi(name: str) -> str:
    '''
    Says hello to someone.
    {{my_rule}}
    '''
    return f"Hello, {name}!"

By executing transdoc program.py -o program_transformed.py -r rules.py, Transdoc will produce a file program_transformed.py with the following contents:

# program.py
def say_hi(name: str) -> str:
    '''
    Says hello to someone.
    This text was added by Transdoc!
    '''
    return f"Hello, {name}!"

Rules can be as complex as you need, accepting any number of arguments. You can call them like you would call the original Python function.

# rules.py
def repeat(text: str, n: int = 2) -> str:
    '''
    Repeat the given text any number of times.
    '''
    return " ".join([text for _ in range(n)])

Using this rule to transform the following code

def say_hi(name: str) -> str:
    '''
    Says hello to someone.
    {{repeat('wowee!')}}
    {{repeat('WOWEE!', n=5)}}
    '''
    return f"Hello, {name}!"

will produce this result:

def say_hi(name: str) -> str:
    '''
    Says hello to someone.
    Wowee! Wowee!
    WOWEE! WOWEE! WOWEE! WOWEE! WOWEE!
    '''
    return f"Hello, {name}!"

Since passing a single string as an argument is so common, Transdoc adds a special syntax for this. Simply place the string argument in square brackets.

def mdn_link(e: str) -> str:
    '''
    Return a Markdown-formatted link for an HTML element
    '''
    return (
        f"[View <{e}> on MDN]"
        f"(https://developer.mozilla.org/en-US/docs/Web/HTML/Element/{e})"
    )

Using this rule to transform the following code

def make_link(text: str, href: str) -> str:
    '''
    Generate an HTML link.
    {{mdn_link[a]}}
    '''
    # Please don't write code this insecure in real life
    return f"<a href={href}>{text}</a>"

will produce this result:

def make_link(text: str, href: str) -> str:
    '''
    Generate an HTML link.
    [View <a> on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a)
    '''
    # Please don't write code this insecure in real life
    return f"<a href={href}>{text}</a>"

Library usage

Transdoc also offers a simple library which can be used to perform these operations programmatically.

import transdoc

def fancy():
    return "✨fancy✨"

def my_function():
    '''Wow this is some {{fancy}} documentation'''


result = transdoc.transform(
    my_function,  # Code to transform
    [fancy],  # Rules to use in transformation (or a module containing rules)
)
# Result now contains a string with the transformed source code for my_function

Integration with build systems

You can integrate Transdoc with project management systems and use it as a pre-build script, so that your docstrings can be automatically built right before packaging and distributing your project.

Poetry

The system is undocumented and unstable, however it is possible (according to this GitHub comment) to get a pre-build script added.

In pyproject.toml:

[tool.poetry.build]
generate-setup-file = false
script = "build.py"

# ...

[build-system]
requires = ["poetry-core", "transdoc"]
build-backend = "poetry.core.masonry.api"

In a file build.py:

import transdoc

exit(transdoc.main("src", "rules.py", "build_dir", force=True))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

transdoc-0.2.5.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

transdoc-0.2.5-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

Details for the file transdoc-0.2.5.tar.gz.

File metadata

  • Download URL: transdoc-0.2.5.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.14 Linux/6.5.0-1025-azure

File hashes

Hashes for transdoc-0.2.5.tar.gz
Algorithm Hash digest
SHA256 a50c85a25faeaeb282fdee2f7d4b22a357c69a68c6bc950df7873e601f38a0ac
MD5 775364d32396945aa9244168355d2b89
BLAKE2b-256 135b82e690b49d1fbf04d3f5cba09d01964e8925322c306deb4432fb23b345b5

See more details on using hashes here.

File details

Details for the file transdoc-0.2.5-py3-none-any.whl.

File metadata

  • Download URL: transdoc-0.2.5-py3-none-any.whl
  • Upload date:
  • Size: 15.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.14 Linux/6.5.0-1025-azure

File hashes

Hashes for transdoc-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 94ab2d87f97539b3f8dc235c0574335696dd27eae910b20a61a66dc7f0f8d25f
MD5 24b08aa8569d543d57b17507b0d8f678
BLAKE2b-256 e3229acba3633cc63156b1e87c6eb528f8ad9b2af557e00a02a1e6fc95d57ee2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page