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 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/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 Distributions

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

cognihub_pygotemplate-0.0.2-cp310-cp310-macosx_12_0_arm64.whl (1.2 MB view details)

Uploaded CPython 3.10macOS 12.0+ ARM64

File details

Details for the file cognihub_pygotemplate-0.0.2-cp312-cp312-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for cognihub_pygotemplate-0.0.2-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7b54c6cbf47b69ab67441ce88007531ac46e89b53876bfacf856bd5a6148765a
MD5 c46b03f99e0a80fe5af9514a30bb1853
BLAKE2b-256 e86df4486590d56ade15fc8a48eb5439400d3458e34e379a92a721e099399b6d

See more details on using hashes here.

File details

Details for the file cognihub_pygotemplate-0.0.2-cp310-cp310-macosx_12_0_arm64.whl.

File metadata

File hashes

Hashes for cognihub_pygotemplate-0.0.2-cp310-cp310-macosx_12_0_arm64.whl
Algorithm Hash digest
SHA256 dd5ad7a75180bcacd271a518625f22ecb74a2baca7c4bcc07d21d870a2255c55
MD5 9354a5b83b169841e672b607593e8101
BLAKE2b-256 ba865a568855fb5108368d008839376257dc3b79a1a394742bce9a6a304f1827

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