Skip to main content

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.

Metadata

Release files for cognihub-pygotemplate 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for cognihub-pygotemplate 0.0.2
File Interpreter ABI Platform Actions
cognihub_pygotemplate-0.0.2-cp312-cp312-manylinux1_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.5+ x86-64 Details for cognihub_pygotemplate-0.0.2-cp312-cp312-manylinux1_x86_64.whl
cognihub_pygotemplate-0.0.2-cp310-cp310-macosx_12_0_arm64.whl CPython 3.10 CPython 3.10 macOS 12.0+ ARM64 Details for cognihub_pygotemplate-0.0.2-cp310-cp310-macosx_12_0_arm64.whl

Total release size: 3.6 MB

Release files / cognihub_pygotemplate-0.0.2-cp312-cp312-manylinux1_x86_64.whl

Download URL cognihub_pygotemplate-0.0.2-cp312-cp312-manylinux1_x86_64.whl
Size 2.4 MB
Tags CPython 3.12 Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
7b54c6cbf47b69ab67441ce88007531ac46e89b53876bfacf856bd5a6148765a
BLAKE2b-256 checksum
How to use checksums
e86df4486590d56ade15fc8a48eb5439400d3458e34e379a92a721e099399b6d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.12

Release files / cognihub_pygotemplate-0.0.2-cp310-cp310-macosx_12_0_arm64.whl

Download URL cognihub_pygotemplate-0.0.2-cp310-cp310-macosx_12_0_arm64.whl
Size 1.2 MB
Tags CPython 3.10 macOS 12.0+ ARM64
SHA-256 checksum
How to use checksums
dd5ad7a75180bcacd271a518625f22ecb74a2baca7c4bcc07d21d870a2255c55
BLAKE2b-256 checksum
How to use checksums
ba865a568855fb5108368d008839376257dc3b79a1a394742bce9a6a304f1827
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.11

Security for cognihub-pygotemplate 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page