Skip to content

Commit 2c55215

Browse files
authored
Merge pull request #8 from cpunion/extension
Better types exporting
2 parents 27b8b5f + e4462bd commit 2c55215

File tree

23 files changed

+2027
-1222
lines changed

23 files changed

+2027
-1222
lines changed

.github/workflows/go.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ jobs:
3939
fi
4040
4141
build:
42+
continue-on-error: true
4243
strategy:
44+
fail-fast: false
4345
matrix:
4446
os:
4547
- macos-latest
@@ -51,6 +53,13 @@ jobs:
5153
- '1.23'
5254
runs-on: ${{matrix.os}}
5355
steps:
56+
- name: Install Python 3.12 (macOS only)
57+
run: brew install [email protected]
58+
if: startsWith(matrix.os, 'macos')
59+
- name: Set up Python 3.12 (Ubuntu only)
60+
run: sudo apt-get install -y python3.12-dev
61+
if: startsWith(matrix.os, 'ubuntu')
62+
5463
- uses: actions/checkout@v4
5564

5665
- name: Set up Go
@@ -62,7 +71,7 @@ jobs:
6271
run: go build -v ./...
6372

6473
- name: Test with coverage
65-
run: go test -v -race -coverprofile=coverage.txt -covermode=atomic ./...
74+
run: go test -v -coverprofile=coverage.txt -covermode=atomic ./...
6675

6776
- name: Upload coverage to Codecov
6877
if: matrix.os == 'ubuntu-24.04' && matrix.go == '1.23'

README.md

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -39,40 +39,40 @@ See the [examples](demo).
3939

4040
### Hello World: Plot a line
4141

42-
```go
43-
// demo/plot/plot.go
42+
<!-- embedme demo/plot/plot.go -->
4443

44+
```go
4545
package main
4646

47-
import gp "github.com/cpunion/go-python"
47+
import . "github.com/cpunion/go-python"
4848

4949
func main() {
50-
gp.Initialize()
51-
plt := gp.ImportModule("matplotlib.pyplot")
52-
plt.Call("plot", gp.MakeTuple(5, 10), gp.MakeTuple(10, 15), gp.KwArgs{"color": "red"})
50+
Initialize()
51+
plt := ImportModule("matplotlib.pyplot")
52+
plt.Call("plot", MakeTuple(5, 10), MakeTuple(10, 15), KwArgs{"color": "red"})
5353
plt.Call("show")
5454
}
5555

5656
```
5757

5858
### Typed Python Objects
5959

60-
```go
61-
// demo/plot2/plot2.go
60+
<!-- embedme demo/plot2/plot2.go -->
6261

62+
```go
6363
package main
6464

65-
import gp "github.com/cpunion/go-python"
65+
import . "github.com/cpunion/go-python"
6666

6767
type plt struct {
68-
gp.Module
68+
Module
6969
}
7070

7171
func Plt() plt {
72-
return plt{gp.ImportModule("matplotlib.pyplot")}
72+
return plt{ImportModule("matplotlib.pyplot")}
7373
}
7474

75-
func (m plt) Plot(args ...any) gp.Object {
75+
func (m plt) Plot(args ...any) Object {
7676
return m.Call("plot", args...)
7777
}
7878

@@ -81,26 +81,26 @@ func (m plt) Show() {
8181
}
8282

8383
func main() {
84-
gp.Initialize()
85-
defer gp.Finalize()
84+
Initialize()
85+
defer Finalize()
8686
plt := Plt()
87-
plt.Plot([]int{5, 10}, []int{10, 15}, gp.KwArgs{"color": "red"})
87+
plt.Plot([]int{5, 10}, []int{10, 15}, KwArgs{"color": "red"})
8888
plt.Show()
8989
}
9090

9191
```
9292

9393
### Define Python Objects with Go
9494

95-
```go
96-
// demo/module/foo/foo.go
95+
<!-- embedme demo/module/foo/foo.go -->
9796

97+
```go
9898
package foo
9999

100100
import (
101101
"fmt"
102102

103-
gp "github.com/cpunion/go-python"
103+
. "github.com/cpunion/go-python"
104104
)
105105

106106
type Point struct {
@@ -131,47 +131,47 @@ func Add(a, b int) int {
131131
return a + b
132132
}
133133

134-
func InitFooModule() gp.Module {
135-
m := gp.CreateModule("foo")
134+
func InitFooModule() Module {
135+
m := CreateModule("foo")
136136
// Add the function to the module
137137
m.AddMethod("add", Add, "(a, b) -> float\n--\n\nAdd two integers.")
138138
// Add the type to the module
139-
gp.AddType[Point](m, (*Point).init, "Point", "Point objects")
139+
m.AddType(Point{}, (*Point).init, "Point", "Point objects")
140140
return m
141141
}
142142

143143
```
144144

145145
Call foo module from Python and Go.
146146

147-
```go
148-
// demo/module/module.go
147+
<!-- embedme demo/module/module.go -->
149148

149+
```go
150150
package main
151151

152152
import (
153153
"fmt"
154154

155-
gp "github.com/cpunion/go-python"
155+
. "github.com/cpunion/go-python"
156156
"github.com/cpunion/go-python/demo/module/foo"
157157
)
158158

159159
func main() {
160-
gp.Initialize()
161-
defer gp.Finalize()
160+
Initialize()
161+
defer Finalize()
162162
fooMod := foo.InitFooModule()
163-
gp.GetModuleDict().SetString("foo", fooMod)
163+
GetModuleDict().SetString("foo", fooMod)
164164

165165
Main1(fooMod)
166166
Main2()
167167
}
168168

169-
func Main1(fooMod gp.Module) {
169+
func Main1(fooMod Module) {
170170
sum := fooMod.Call("add", 1, 2).AsLong()
171171
fmt.Printf("Sum of 1 + 2: %d\n", sum.Int64())
172172

173173
dict := fooMod.Dict()
174-
Point := dict.Get(gp.MakeStr("Point")).AsFunc()
174+
Point := dict.Get(MakeStr("Point")).AsFunc()
175175

176176
point := Point.Call(3, 4)
177177
fmt.Printf("dir(point): %v\n", point.Dir())
@@ -191,7 +191,7 @@ func Main1(fooMod gp.Module) {
191191

192192
func Main2() {
193193
fmt.Printf("=========== Main2 ==========\n")
194-
_ = gp.RunString(`
194+
_ = RunString(`
195195
import foo
196196
point = foo.Point(3, 4)
197197
print("dir(point):", dir(point))
@@ -213,15 +213,15 @@ point.print()
213213

214214
### Call gradio
215215

216-
```go
217-
// demo/gradio/gradio.go
216+
<!-- embedme demo/gradio/gradio.go -->
218217

218+
```go
219219
package main
220220

221221
import (
222222
"os"
223223

224-
gp "github.com/cpunion/go-python"
224+
. "github.com/cpunion/go-python"
225225
)
226226

227227
/*
@@ -243,16 +243,16 @@ with gr.Blocks() as demo:
243243
demo.launch()
244244
*/
245245

246-
var gr gp.Module
246+
var gr Module
247247

248-
func UpdateExamples(country string) gp.Object {
248+
func UpdateExamples(country string) Object {
249249
println("country:", country)
250250
if country == "USA" {
251-
return gr.Call("Dataset", gp.KwArgs{
251+
return gr.Call("Dataset", KwArgs{
252252
"samples": [][]string{{"Chicago"}, {"Little Rock"}, {"San Francisco"}},
253253
})
254254
} else {
255-
return gr.Call("Dataset", gp.KwArgs{
255+
return gr.Call("Dataset", KwArgs{
256256
"samples": [][]string{{"Islamabad"}, {"Karachi"}, {"Lahore"}},
257257
})
258258
}
@@ -264,15 +264,15 @@ func main() {
264264
return
265265
}
266266

267-
gp.Initialize()
268-
defer gp.Finalize()
269-
gr = gp.ImportModule("gradio")
270-
fn := gp.CreateFunc(UpdateExamples,
267+
Initialize()
268+
defer Finalize()
269+
gr = ImportModule("gradio")
270+
fn := CreateFunc("update_examples", UpdateExamples,
271271
"(country, /)\n--\n\nUpdate examples based on country")
272272
// Would be (in the future):
273-
// fn := gp.FuncOf(UpdateExamples)
274-
demo := gp.With(gr.Call("Blocks"), func(v gp.Object) {
275-
dropdown := gr.Call("Dropdown", gp.KwArgs{
273+
// fn := FuncOf(UpdateExamples)
274+
demo := With(gr.Call("Blocks"), func(v Object) {
275+
dropdown := gr.Call("Dropdown", KwArgs{
276276
"label": "Country",
277277
"choices": []string{"USA", "Pakistan"},
278278
"value": "USA",

0 commit comments

Comments
 (0)