python bindings for go template
Project description
Go-template
Overview
Python bindings for go text/template
Quickstart
Pip
go_template
works Python 2.7, 3.5, 3.6, 3.7.
pip install go_template
Example
Content of sample.tmpl
{{.Count}} items are made of {{.Material}}
Content of values.yml
Count: 12
Material: Wool
- Print rendered output to stdout
>>> import go_template
>>> go_template.render_template('tests/sample.tmpl','tests/values.yml','')
12 items are made of Wool
- Get rendered output in a file
>>> import go_template
>>> go_template.render_template('tests/sample.tmpl','tests/values.yml','output.txt')
Content of output.txt
12 items are made of Wool
NOTE: Paths provided to render_template should either be absolute path or relative to directory where it is ran.
Build shared library
For building a fresh shared object of text/template, you must have golang^1.5 installed.
./build.sh
This will create template.so in the bind
folder.
Motivation
Currently, there is no python package which exposes golang text/template
functionality to python. And I am in the process of learning about interoperability between different languages. So, I started working on this as a learning project.
Explanation
Golang library cannot be directly used in python. Firstly, we have to compile it as shared object or archive for interoperability with C. And then create python bindings for this C object.
CPython is the original Python implementation and provides cpython API for creating python wrapper, but the wrapping code is in C. There is a library gopy which exactly uses this approach. But it works only on go1.5 and for python2.
If we want to write the wrapping code in python, there are Cython and ctypes. Ctypes allow directly importing the C library and calling functions through its interface. This project uses ctypes
for calling go functions.
When a golang library is compiled as shared object, cgo handles exposing functions and data type conversion. Using ctypes, we can only modify simple data type, string, int, float, bool. I tried converting python class to golang struct, but it failed.
So, I created a golang wrapper over text/template library, which takes simple datatypes. And handles complex operation in this layer. Then a python wrapper over this layer using ctypes
.
It is far from complete and doesn't use the best approach. Currently, it has only one function which takes path of template and value file. And depending on the third argument, either writes to stdout if empty or to file if given its path.
License
This project is licensed under MIT License.
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 Distribution
Built Distribution
File details
Details for the file go-template-0.0.3.tar.gz
.
File metadata
- Download URL: go-template-0.0.3.tar.gz
- Upload date:
- Size: 2.0 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.1 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d4da46b0fae1a9583317550c0b62d208e082d4edcafeabd5cd870441d82490a5 |
|
MD5 | baab297babdd89ba7cda547260b998ce |
|
BLAKE2b-256 | 76c5d5fe34278d5cc2bf17b0b495285d9eb36a1422a3ccc4e0ef8d2ee38aa06d |
File details
Details for the file go_template-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: go_template-0.0.3-py3-none-any.whl
- Upload date:
- Size: 2.0 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.1 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cfa48cc98f4f13f9542b83571b8a459f0be649ea8a994bf269a43405a28b0cda |
|
MD5 | 5df5a37b31834c38d63211ba96969592 |
|
BLAKE2b-256 | 994a3f47b050fc59a66d69303cd25b1dedfba505c611709d2ca681fea146cc11 |