Skip to main content

Minimalist Python template generation with type-safe annotations. Perfect for chat bots and LLM prompts.

Project description

Py-Templatify


Documentation: WIP

Source Code: https://github.com/EzyGang/py-templatify/

Roadmap: WIP


Py-Templatify is a lightweight library designed for developers who want a straightforward and efficient way to generate simple string templates in Python applications.

It is particularly well-suited for building templates for chat bots, LLM prompts, and other scenarios where concise and easily managed string outputs are required.

Key Features:

  • Simple Templating: Transform standard functions into templated functions using the @templatify decorator, enabling straightforward dynamic string generation without the complexity of traditional templating engines.

  • Minimal Logic: Py-Templatify is tailored for small templates, making it ideal for quick formatting tasks without extensive in-template transformation logic.

  • Markdown Support: Integrate Markdown formatting by default, allowing for structured output, and extend functionality using base classes for custom tags and formatting.

  • Strong Typing with Annotations: Utilize Python's type hints to define the format and behavior of each argument in the template, ensuring clarity and maintainability.

  • Optional Values: Manage optional template values gracefully with the Option class, providing fallback values when needed.

  • Roadmap: A roadmap for future improvements and features is in the works.


Table of Contents

Installation

$ pip install py-templatify
# ---> 100% Successfully installed py-templatify

How to use

To start using the library, decorate your function with @templatify:

from py_templatify import templatify

@templatify(description='Short description')  # The `description` param is not utilized anywhere
def your_function(arg1: str, ...):
    """
    This is where your template message goes.
    
    Here is the {arg1}!
    """
    ...
    

print(your_function(arg1='Awesome argument'))

After running the above you'll get the following output:

This is where your template message goes.

Here is the Awesome argument!

Basic example

Here's a generic example demonstrating the functionality of the library:

from decimal import Decimal
from typing import Annotated, Any
from py_templatify import templatify, Option, Boolean
from py_templatify.markdown.shortcuts import BoldTag, CodeTag, LinkTag
from py_templatify.markdown.tags import Bold, Spoiler, Code


# You can define a reusable tag like this
MyOption = Option[str](if_none='No notes provided')


@templatify(description='Template for user information')
def user_info_template(
    username: BoldTag[str],
    email: Annotated[str, Bold],  # this is the same as `username`, but explicitly
    account_status: CodeTag[str],
    plan_price: Annotated[Decimal, Code, Spoiler],  # You can chain tags, they are executed from left to right
    subscription_active: Annotated[bool, Boolean(if_true='Yes')],
    optional_note: Annotated[str | None, MyOption],
    optional_note_2: Annotated[str | None, MyOption],
    user_link: LinkTag[tuple[str, str]],
) -> None:
    """
    User Information:
    
    Welcome, {username}!
    Your email: {email}.
    
    Account Status: {account_status}
    Plan price: {plan_price}

    Link: {user_link}
    
    Note: {optional_note}
    Note 2: {optional_note_2}
    
    Subscription Active: {subscription_active}
    """

if __name__ == '__main__':
    print(
        user_info_template(
            username='john_doe', 
            email='john.doe@google.com', 
            account_status='Active', 
            plan_price=Decimal('4.99'),
            subscription_active=True,
            optional_note=None,
            optional_note_2='Note number 2',
            user_link=('User Profile', 'https://example.com/user'),
        )
    )

After running the above:

User Information:

Welcome, **john_doe**!
Your email: **john.doe@google.com**.

Account Status: `Active`
Plan price: ||`4.99`||

Link: [User Profile](https://example.com/user)

Note: No notes provided
Note 2: Note number 2

Subscription Active: Yes

Using Tags

Utilize the built-in tags like Bold, Link, and Code to format your templates effortlessly. Basic Markdown formatting is shipped by default, but you can extend and create custom tags using the provided base classes.

Contributing

If you'd like to contribute to Py-Templatify, please discuss your changes using Issues or open a Pull Request which will be reviewed soon.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

py_templatify-0.1.0.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_templatify-0.1.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file py_templatify-0.1.0.tar.gz.

File metadata

  • Download URL: py_templatify-0.1.0.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for py_templatify-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7435ef5a90fec3bb055a5213ef624822933cc75226a5daf259bd7507426179ee
MD5 d14be2fd3dc88b6fcf5f3c5c39ba9aab
BLAKE2b-256 9bef5671010c0770045891db975b922b485ec59531ac6240ebd86f24c2c6b08b

See more details on using hashes here.

Provenance

The following attestation bundles were made for py_templatify-0.1.0.tar.gz:

Publisher: build-and-publish.yml on EzyGang/py-templatify

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py_templatify-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: py_templatify-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for py_templatify-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 81118c57bb89bb88dfdb5a3f48e7f988203ee2028cc2988d4bc56945591b71bd
MD5 f31b6090b165a3b1bbdfc13bba3d12de
BLAKE2b-256 87f115856c4cbb8aef4579dd6c40475d575fc33bc5f4fa033323a35957650a3c

See more details on using hashes here.

Provenance

The following attestation bundles were made for py_templatify-0.1.0-py3-none-any.whl:

Publisher: build-and-publish.yml on EzyGang/py-templatify

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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