Skip to main content

A Python wrapper for Go's text/template engine via CGo

Project description

cognihub_pygotemplate

A simple, high-performance Python wrapper for Go's native text/template engine.

中文文档

This library allows you to use Go templates (like those from Ollama) directly in your Python projects with 100% compatibility, by calling a compiled Go shared library via CGo and Python's ctypes.

Prerequisites

  • Python 3.10+
  • Go compiler (1.21+ recommended) installed and available in your system's PATH

Installation

From PyPI (Recommended)

pip install cognihub_pygotemplate

From Source

# Clone the repository
git clone https://github.com/hsz1273327/cognihub_pygotemplate.git
cd cognihub_pygotemplate

# Install the package
pip install .

The library will automatically compile the Go source code during installation.

Usage

from cognihub_pygotemplate import GoTemplateEngine

# Your Go template string
template_str = """
Hello, {{.Name}}!
{{if .Items}}You have {{len .Items}} items:
{{range .Items}}- {{.}}
{{end}}{{else}}You have no items.{{end}}
"""

# Data to render
data = {
    "Name": "World",
    "Items": ["apple", "banana", "cherry"]
}

try:
    # Create an engine instance
    engine = GoTemplateEngine(template_str)

    # Render the template
    output = engine.render(data)

    print(output)

except (RuntimeError, ValueError) as e:
    print(f"An error occurred: {e}")

Development Build

python setup.py build_py

Build Wheels

# For current platform
python setup.py bdist_wheel

Features

  • 100% Go Template Compatibility: Uses Go's native text/template engine
  • High Performance: Direct CGo bindings with minimal overhead
  • Cross-Platform: Supports Windows, macOS (Intel & ARM64), and Linux
  • Memory Safe: Proper memory management to prevent leaks
  • Easy Integration: Simple Python API that feels native

Error Handling

The library provides clear error messages for common issues:

  • JSON_ERROR: Invalid data format
  • TEMPLATE_PARSE_ERROR: Template syntax errors
  • TEMPLATE_EXECUTE_ERROR: Runtime template execution errors

License

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

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

cognihub_pygotemplate-0.0.1-cp310-cp310-macosx_11_0_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.10macOS 11.0+ x86-64

File details

Details for the file cognihub_pygotemplate-0.0.1-cp310-cp310-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for cognihub_pygotemplate-0.0.1-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 020d437e8b186778caeae9be8f5e45ee4e914e576e34a0c72b3a3e2a3403d9b0
MD5 a1dd48860de19ec15b0d35cc9883f0bb
BLAKE2b-256 57d92d62a9730ab00ff7501921f8084c5567b6bbb0eae783c5b9c0824db2469f

See more details on using hashes here.

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