-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from cpunion/update
update docs and demo
- Loading branch information
Showing
4 changed files
with
125 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# Design | ||
|
||
## Python types wrapper design | ||
|
||
To automatically DecRef Python objects, we need to wrap them in a Go struct that will call DecRef when it is garbage collected. This is done by embedding a PyObject in a Go struct and registering a finalizer on the Go struct. Below is an example of how this is done: | ||
|
||
```go | ||
type pyObject struct { | ||
obj *C.PyObject | ||
} | ||
|
||
func newObject(obj *C.PyObject) *pyObject { | ||
o := &pyObject{obj} | ||
runtime.SetFinalizer(o, func(o *pyObject) { | ||
C.Py_DecRef(o.obj) | ||
}) | ||
return o | ||
} | ||
``` | ||
|
||
To wrap generic PyObject(s) to typed Python objects, the best way is using alias types. Below is an example of how this is done: | ||
|
||
```go | ||
type Object *pyObject | ||
|
||
func (o Object) GetAttrString(name string) Object { | ||
return newObject(o.obj.GetAttrString(name)) | ||
} | ||
|
||
type Dict Object | ||
|
||
func (d Dict) SetItemString(name string, value Object) { | ||
d.obj.SetItemString(name, value.obj) | ||
} | ||
``` | ||
|
||
Unfortunately, Go does not allow defining methods on alias types like the above. | ||
|
||
```shell | ||
invalid receiver type PyObject (pointer or interface type) | ||
invalid receiver type PyDict (pointer or interface type) | ||
``` | ||
|
||
We can define a new type that embeds the alias type and define methods on the new type. Below is an example of how this is done: | ||
|
||
```go | ||
type Object struct { | ||
*pyObject | ||
} | ||
|
||
func (o *Object) GetAttrString(name string) *Object { | ||
return &Object{newObject(o.obj.GetAttrString(name))} | ||
} | ||
|
||
type Dict struct { | ||
*Object | ||
} | ||
|
||
func (d *Dict) SetItemString(name string, value *Object) { | ||
d.obj.SetItemString(name, value.obj) | ||
} | ||
``` | ||
|
||
But allocating a `PyDict` object will allocate a `PyObject` object and a `pyObject` object. This is not efficient. | ||
|
||
We can use a `struct` instead of a `pointer` to avoid this. Below is an example of how this is done: | ||
|
||
```go | ||
type Object struct { | ||
*pyObject | ||
} | ||
|
||
func (o Object) GetAttrString(name string) Object { | ||
return Object{newObject(o.obj.GetAttrString(name))} | ||
} | ||
|
||
type Dict struct { | ||
Object | ||
} | ||
|
||
func (d Dict) SetItemString(name string, value Object) { | ||
d.obj.SetItemString(name, value.obj) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,27 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/cpunion/go-python" | ||
) | ||
import gp "github.com/cpunion/go-python" | ||
|
||
type plt struct { | ||
python.Module | ||
gp.Module | ||
} | ||
|
||
func Plt() plt { | ||
return plt{python.ImportModule("matplotlib.pyplot")} | ||
return plt{gp.ImportModule("matplotlib.pyplot")} | ||
} | ||
|
||
func (m plt) Plot(args ...any) func(kw any) python.Object { | ||
return m.CallKeywords("plot", args...) | ||
func (m plt) Plot(args ...any) gp.Object { | ||
return m.Call("plot", args...) | ||
} | ||
|
||
func (m plt) Show() { | ||
m.Call("show") | ||
} | ||
|
||
func plot1() { | ||
plt := python.ImportModule("matplotlib.pyplot") | ||
plt.CallKeywords("plot", python.MakeTuple(5, 10), python.MakeTuple(10, 15))(python.DictFromPairs("color", "red")) | ||
plt.Call("show") | ||
} | ||
|
||
func plot2() { | ||
func main() { | ||
gp.Initialize() | ||
defer gp.Finalize() | ||
plt := Plt() | ||
plt.Plot(python.MakeTuple(5, 10), python.MakeTuple(10, 15))(python.DictFromPairs("color", "red")) | ||
plt.Plot(gp.MakeTuple(5, 10), gp.MakeTuple(10, 15), gp.KwArgs{"color": "red"}) | ||
plt.Show() | ||
} | ||
|
||
func main() { | ||
python.Initialize() | ||
plot1() | ||
plot2() | ||
} |