2015-07-24 14:16:31 +00:00
gopy
====
2015-01-30 17:01:15 +00:00
2015-08-11 19:01:36 +00:00
[](https://godoc.org/github.com/go-python/gopy)
2015-08-11 19:08:04 +00:00
[](https://travis-ci.org/go-python/gopy)
2015-08-11 19:01:36 +00:00
2015-07-24 14:16:31 +00:00
`gopy` generates (and compiles) a `CPython` extension module from a `go` package.
2015-01-30 17:01:15 +00:00
## Installation
```sh
2015-07-24 14:16:31 +00:00
$ go get github.com/go-python/gopy
2015-01-30 17:01:15 +00:00
```
2015-11-16 12:47:36 +00:00
You will need `Go >= 1.5` .
2017-03-06 14:56:38 +00:00
## Community
The `go-python` community can be reached out at [go-python@googlegroups.com ](mailto:go-python@googlegroups.com ) or via the web forum: [go-python group ](https://groups.google.com/forum/#!forum/go-python ).
2017-05-09 11:52:55 +00:00
See the [CONTRIBUTING ](https://github.com/go-python/gopy/blob/master/CONTRIBUTE.md ) guide for pointers on how to contribute to `gopy` .
2017-03-06 14:56:38 +00:00
2015-01-30 17:01:15 +00:00
## Documentation
2015-11-16 12:47:36 +00:00
A presentation was given at [dotgo-2015 ](http://talks.godoc.org/github.com/sbinet/talks/2015/20151109-gopy-dotgo/gopy-dotgo.slide ).
A longer version of that talk is also available [here ](http://talks.godoc.org/github.com/sbinet/talks/2015/20150929-gopy-lyon/gopy-lyon.slide#17 ).
2015-12-19 18:10:19 +00:00
An article was also posted on the [GopherAcademy Advent-2015 ](https://blog.gopheracademy.com/advent-2015/gopy/ ).
2015-11-16 12:47:36 +00:00
2015-01-30 17:01:15 +00:00
Documentation is available on [godoc ](https://godoc.org ):
2015-07-24 14:16:31 +00:00
https://godoc.org/github.com/go-python/gopy
2015-01-30 17:01:15 +00:00
or directly from the command-line prompt:
```sh
2015-07-24 14:16:31 +00:00
$ gopy help
gopy -
2015-01-30 17:01:15 +00:00
2015-07-24 14:16:31 +00:00
Commands:
2015-01-30 17:01:15 +00:00
2015-07-24 14:16:31 +00:00
bind generate and compile (C)Python language bindings for Go
gen generate (C)Python language bindings for Go
2015-01-30 17:01:15 +00:00
2015-07-24 14:16:31 +00:00
Use "gopy help < command > " for more information about a command.
2015-01-30 17:01:15 +00:00
2015-07-24 14:16:31 +00:00
$ gopy help gen
Usage: gopy gen < go-package-name >
2015-01-30 17:01:15 +00:00
2015-07-24 14:16:31 +00:00
gen generates (C)Python language bindings for a Go package.
2015-01-30 17:01:15 +00:00
2015-07-24 14:16:31 +00:00
ex:
$ gopy gen [options] < go-package-name >
$ gopy gen github.com/go-python/gopy/_examples/hi
2015-01-30 17:01:15 +00:00
2015-07-24 14:16:31 +00:00
Options:
2015-09-11 12:40:47 +00:00
-lang="py2": target language for bindings
2015-07-24 14:16:31 +00:00
-output="": output directory for bindings
2015-01-30 17:01:15 +00:00
2015-07-24 14:16:31 +00:00
$ gopy help bind
Usage: gopy bind < go-package-name >
2015-01-30 17:01:15 +00:00
2015-07-24 14:16:31 +00:00
bind generates and compiles (C)Python language bindings for a Go package.
2015-01-30 17:01:15 +00:00
2015-07-24 14:16:31 +00:00
ex:
$ gopy bind [options] < go-package-name >
$ gopy bind github.com/go-python/gopy/_examples/hi
2015-01-30 17:01:15 +00:00
2015-07-24 14:16:31 +00:00
Options:
2017-08-07 13:00:27 +00:00
-lang="py2": python version to use for bindings (python2|py2|python3|py3|cffi)
2015-07-24 14:16:31 +00:00
-output="": output directory for bindings
```
2015-01-30 17:01:15 +00:00
2015-07-24 14:16:31 +00:00
## Examples
2015-01-30 17:01:15 +00:00
2015-08-07 11:17:55 +00:00
### From the `python` shell
`gopy` comes with a little `python` module allowing to wrap and compile `go`
packages directly from the `python` interactive shell:
```python
>>> import gopy
>>> hi = gopy.load("github.com/go-python/gopy/_examples/hi")
gopy> inferring package name...
gopy> loading 'github.com/go-python/gopy/_examples/hi'...
gopy> importing 'github.com/go-python/gopy/_examples/hi'
>>> print hi
< module ' github . com / go-python / gopy / _examples / hi ' from ' / some / path / . . . / hi . so ' >
>>> print hi.__doc__
package hi exposes a few Go functions to be wrapped and used from Python.
```
### From the command line
2015-07-24 14:16:31 +00:00
```sh
$ gopy bind -output=out github.com/go-python/gopy/_examples/hi
$ ls out
hi.so
2015-01-30 17:01:15 +00:00
2015-07-24 14:16:31 +00:00
$ cd out
$ python2
2015-01-30 17:01:15 +00:00
>>> import hi
2015-07-24 14:16:31 +00:00
>>> dir(hi)
['Add', 'Concat', 'Hello', 'Hi', 'NewPerson', 'Person', '__doc__', '__file__', '__name__', '__package__']
2015-01-30 17:01:15 +00:00
>>> hi.Hello("you")
hello you from go
2015-07-24 14:16:31 +00:00
```
You can also run:
```sh
go test -v -run=TestBind
=== RUN TestBind
processing "Add"...
processing "Concat"...
processing "Hello"...
processing "Hi"...
processing "NewPerson"...
processing "Person"...
processing "Add"...
processing "Concat"...
processing "Hello"...
processing "Hi"...
processing "NewPerson"...
processing "Person"...
github.com/go-python/gopy/_examples/hi
_/home/binet/dev/go/root/tmp/gopy-431003574
--- hi.Hi()...
hi from go
--- hi.Hello('you')...
hello you from go
--- hi.Add(1, 41)...
42
--- hi.Concat('4', '2')...
42
--- doc(hi.Person):
Person is a simple struct
--- p = hi.Person()...
< hi.Person object at 0x7fc46cc330f0 >
['Age', 'Name', '__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
--- p.Name: None
--- p.Age: None
--- doc(p):
Person is a simple struct
--- PASS: TestBind (2.13s)
PASS
ok github.com/go-python/gopy 2.135s
2015-01-30 17:01:15 +00:00
```
2015-01-30 17:23:06 +00:00
2015-12-15 17:56:44 +00:00
## Binding generation using Docker (for cross-platform builds)
```
$ cd github.com/go-python/gopy/_examples/hi
2015-12-15 19:04:29 +00:00
$ docker run --rm -v `pwd` :/go/src/in -v `pwd` :/out gopy/gopy app bind -output=/out in
2015-12-15 17:56:44 +00:00
$ file hi.so
hi.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, not stripped
```
The docker image can also be built on local machine:
```
$ cd $GOPATH/src/github.com/go-python/gopy
$ docker build -t go-python/gopy .
$ docker run -it --rm go-python/gopy
```
2015-01-30 17:23:06 +00:00
## Limitations
2015-07-29 12:48:44 +00:00
- wrap `go` structs into `python` classes ** [DONE]**
- better pythonization: turn `go` `errors` into `python` exceptions ** [DONE]**
2015-08-05 17:51:22 +00:00
- wrap arrays and slices into types implementing `tp_as_sequence` ** [DONE]**
2017-08-07 13:00:27 +00:00
- only `python-2` supported for now ** [DONE]**
2015-08-05 17:51:22 +00:00
2015-08-31 08:39:02 +00:00
## Contribute
`gopy` is part of the `go-python` organization and licensed under `BSD-3` .
When you want to contribute a patch or some code to `gopy` , please send a pull
request against the `gopy` issue tracker **AND** a pull request against
2017-07-13 17:30:34 +00:00
[go-python/license ](https://github.com/go-python/license ) adding yourself to the
2015-08-31 08:39:02 +00:00
`AUTHORS` and `CONTRIBUTORS` files.