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/templateengine - 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 formatTEMPLATE_PARSE_ERROR: Template syntax errorsTEMPLATE_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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cognihub_pygotemplate-0.0.1-cp310-cp310-macosx_11_0_x86_64.whl.
File metadata
- Download URL: cognihub_pygotemplate-0.0.1-cp310-cp310-macosx_11_0_x86_64.whl
- Upload date:
- Size: 1.2 MB
- Tags: CPython 3.10, macOS 11.0+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
020d437e8b186778caeae9be8f5e45ee4e914e576e34a0c72b3a3e2a3403d9b0
|
|
| MD5 |
a1dd48860de19ec15b0d35cc9883f0bb
|
|
| BLAKE2b-256 |
57d92d62a9730ab00ff7501921f8084c5567b6bbb0eae783c5b9c0824db2469f
|