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 Workflow
Full development cycle: Clean -> Build -> Type Check -> Test. Iterate until requirements are met, then package.
Clean
python setup.py clean
Build
python setup.py build_py
Type Check
# Use mypy for type checking
python setup.py type_check [--strict]
Test
# 运行所有测试
python setup.py test
# 或者执行单条测试
python -m unittest tests.test_engine.TestGoTemplateEngine.test_render
# 或者使用自定义测试运行器
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 Distributions
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.2-cp312-cp312-manylinux1_x86_64.whl.
File metadata
- Download URL: cognihub_pygotemplate-0.0.2-cp312-cp312-manylinux1_x86_64.whl
- Upload date:
- Size: 2.4 MB
- Tags: CPython 3.12
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b54c6cbf47b69ab67441ce88007531ac46e89b53876bfacf856bd5a6148765a
|
|
| MD5 |
c46b03f99e0a80fe5af9514a30bb1853
|
|
| BLAKE2b-256 |
e86df4486590d56ade15fc8a48eb5439400d3458e34e379a92a721e099399b6d
|
File details
Details for the file cognihub_pygotemplate-0.0.2-cp310-cp310-macosx_12_0_arm64.whl.
File metadata
- Download URL: cognihub_pygotemplate-0.0.2-cp310-cp310-macosx_12_0_arm64.whl
- Upload date:
- Size: 1.2 MB
- Tags: CPython 3.10, macOS 12.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd5ad7a75180bcacd271a518625f22ecb74a2baca7c4bcc07d21d870a2255c55
|
|
| MD5 |
9354a5b83b169841e672b607593e8101
|
|
| BLAKE2b-256 |
ba865a568855fb5108368d008839376257dc3b79a1a394742bce9a6a304f1827
|