├── _examples └── py-hello │ └── main.go ├── README.md ├── dict.go ├── LICENSE └── object.go /_examples/py-hello/main.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015 The go-python Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package main 6 | 7 | import ( 8 | "fmt" 9 | 10 | "github.com/go-python/py" 11 | ) 12 | 13 | func init() { 14 | err := py.Initialize() 15 | if err != nil { 16 | panic(err) 17 | } 18 | } 19 | 20 | func main() { 21 | gostr := "foo" 22 | pystr := py.NewString(gostr) 23 | fmt.Printf("hello [%v]\n", pystr) 24 | } 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | py 2 | == 3 | 4 | `py` is a high-level API wrapping the low-level `CPython` C-API, for `go`. 5 | 6 | ## Installation 7 | 8 | ```sh 9 | $ go get github.com/go-python/py 10 | ``` 11 | 12 | 13 | ## Documentation 14 | 15 | Documentation is available on [godoc](https://godoc.org): 16 | 17 | [github.com/go-python/py](https://godoc.org/github.com/go-python/py) 18 | 19 | 20 | ## Examples 21 | 22 | ```go 23 | package main 24 | 25 | import ( 26 | "fmt" 27 | 28 | "github.com/go-python/py" 29 | ) 30 | 31 | func init() { 32 | err := py.Initialize() 33 | if err != nil { 34 | panic(err) 35 | } 36 | } 37 | 38 | func main() { 39 | gostr := "foo" 40 | pystr := py.NewString(gostr) 41 | fmt.Printf("hello [%v]\n", pystr) 42 | } 43 | ``` 44 | 45 | ```sh 46 | $ go run ./main.go 47 | hello [foo] 48 | ``` 49 | -------------------------------------------------------------------------------- /dict.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015 The go-python Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package py 6 | 7 | // Dict represents the python dict-like protocol 8 | type Dict interface { 9 | // Clear empties an existing dictionay of all key-value pairs. 10 | Clear() 11 | 12 | // Contains determines whether a dictionary contains the key k 13 | Contains(k Object) bool 14 | 15 | // SetItem inserts the value v into the dictionary with a key k 16 | SetItem(k, v Object) error 17 | 18 | // DelItem removes the entry with key k from the dictionary 19 | DelItem(k Object) error 20 | 21 | // GetItem returns the object with key from the dictionary 22 | GetItem(k Object) (Object, error) 23 | 24 | // Items returns all the items from the dictionary 25 | Items() [][2]Object 26 | 27 | // Keys returns all the keys from the dictionary 28 | Keys() []Object 29 | 30 | // Values returns all the values from the dictionary 31 | Values() []Object 32 | 33 | // Size returns the number of items in the dictionary 34 | Size() int 35 | } 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright ©2015 The go-python Authors. All rights reserved. 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are met: 5 | * Redistributions of source code must retain the above copyright 6 | notice, this list of conditions and the following disclaimer. 7 | * Redistributions in binary form must reproduce the above copyright 8 | notice, this list of conditions and the following disclaimer in the 9 | documentation and/or other materials provided with the distribution. 10 | * Neither the name of the gonum project nor the names of its authors and 11 | contributors may be used to endorse or promote products derived from this 12 | software without specific prior written permission. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 18 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 21 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 22 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | -------------------------------------------------------------------------------- /object.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015 The go-python Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package py 6 | 7 | import ( 8 | "reflect" 9 | ) 10 | 11 | // Object represents any Python object and codifies the Python object protocol. 12 | type Object interface { 13 | // Increment the reference count. 14 | // The Object may be nil, in which case the function has no effect. 15 | IncRef() 16 | 17 | // Decrement the reference count. 18 | // If the Object is nil, nothing happens. 19 | DecRef() 20 | 21 | // HasAttr returns whether this object has an attribute with name 'n' 22 | HasAttr(n string) bool 23 | 24 | // GetAttr returns the attribute with name 'n' 25 | GetAttr(n string) (Object, error) 26 | 27 | // SetAttr sets the value of the attribute named 'n' of the object to the value 'v' 28 | SetAttr(n string, v Object) error 29 | 30 | // DelAttr deletes the attribute named 'n' of the object 31 | DelAttr(n string) error 32 | 33 | String() string 34 | 35 | // Call calls a callable Object with args as arguments 36 | Call(args ...Object) (Object, error) 37 | 38 | // Hash computes and returns the hash value of an Object 39 | // On failure, Hash returns -1. 40 | Hash() int64 41 | 42 | // IsTrue returns whether an Object is considered to be true. 43 | //IsTrue() bool 44 | 45 | // Not returns whether an Object is not considered to be true. 46 | //Not() bool 47 | 48 | // Type returns the Type of this Object. 49 | Type() Object 50 | } 51 | 52 | // New creates a new py.Object value from a Go value 53 | func New(v interface{}) Object { 54 | rt := reflect.TypeOf(v) 55 | switch rt.Kind() { 56 | case reflect.Int: 57 | return nil 58 | } 59 | return nil 60 | } 61 | --------------------------------------------------------------------------------