Skip to content

Commit f5104e3

Browse files
authored
Merge pull request #11 from cpunion/docs
Docs, CI
2 parents 6e896b6 + 81122e0 commit f5104e3

File tree

8 files changed

+205
-174
lines changed

8 files changed

+205
-174
lines changed

.github/workflows/go.yml

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,66 @@ on:
1010
branches: [ "main" ]
1111

1212
jobs:
13+
check:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v4
17+
18+
- name: Set up Node.js
19+
uses: actions/setup-node@v4
20+
with:
21+
node-version: '20'
22+
23+
- name: Install embedme
24+
run: npm install -g embedme
25+
26+
- name: Verify README.md embedded code
27+
run: npx embedme --verify README.md
28+
29+
- name: Set up Go
30+
uses: actions/setup-go@v4
31+
with:
32+
go-version: '1.23'
33+
34+
- name: Check formatting
35+
run: |
36+
if [ -n "$(go fmt ./...)" ]; then
37+
echo "Some files are not properly formatted. Please run 'go fmt ./...'"
38+
exit 1
39+
fi
1340
1441
build:
1542
strategy:
1643
matrix:
1744
os:
1845
- macos-latest
1946
- ubuntu-24.04
47+
go:
48+
- '1.20'
49+
- '1.21'
50+
- '1.22'
51+
- '1.23'
2052
runs-on: ${{matrix.os}}
2153
steps:
22-
- uses: actions/checkout@v4
23-
24-
- name: Set up Go
25-
uses: actions/setup-go@v4
26-
with:
27-
go-version: '1.20'
28-
29-
- name: Build
30-
run: go build -v ./...
31-
32-
- name: Test with coverage
33-
run: go test -p 1 -v -race -coverprofile=coverage.txt -covermode=atomic ./...
34-
35-
- name: Upload coverage to Codecov
36-
if: matrix.os == 'ubuntu-24.04'
37-
uses: codecov/codecov-action@v4
38-
with:
39-
token: ${{ secrets.CODECOV_TOKEN }}
40-
file: ./coverage.txt
41-
flags: unittests
42-
name: codecov-umbrella
43-
fail_ci_if_error: true
54+
- uses: actions/checkout@v4
55+
56+
- name: Set up Go
57+
uses: actions/setup-go@v4
58+
with:
59+
go-version: ${{matrix.go}}
60+
61+
- name: Build
62+
run: go build -v ./...
63+
64+
- name: Test with coverage
65+
run: go test -v -race -coverprofile=coverage.txt -covermode=atomic ./...
66+
67+
- name: Upload coverage to Codecov
68+
if: matrix.os == 'ubuntu-24.04' && matrix.go == '1.23'
69+
uses: codecov/codecov-action@v4
70+
with:
71+
token: ${{ secrets.CODECOV_TOKEN }}
72+
file: ./coverage.txt
73+
flags: unittests
74+
name: codecov-umbrella
75+
fail_ci_if_error: true

README.md

Lines changed: 58 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@
3737

3838
See the [examples](_demo).
3939

40-
### Hello World
40+
### Hello World: Plot a line
4141

4242
```go
43+
// _demo/plot/plot.go
44+
4345
package main
4446

4547
import gp "github.com/cpunion/go-python"
@@ -50,11 +52,14 @@ func main() {
5052
plt.Call("plot", gp.MakeTuple(5, 10), gp.MakeTuple(10, 15), gp.KwArgs{"color": "red"})
5153
plt.Call("show")
5254
}
55+
5356
```
5457

5558
### Typed Python Objects
5659

5760
```go
61+
// _demo/plot2/plot2.go
62+
5863
package main
5964

6065
import gp "github.com/cpunion/go-python"
@@ -79,16 +84,25 @@ func main() {
7984
gp.Initialize()
8085
defer gp.Finalize()
8186
plt := Plt()
82-
plt.Plot(gp.MakeTuple(5, 10), gp.MakeTuple(10, 15), gp.KwArgs{"color": "red"})
87+
plt.Plot([]int{5, 10}, []int{10, 15}, gp.KwArgs{"color": "red"})
8388
plt.Show()
8489
}
85-
```
8690

87-
### Define Python Objects
91+
```
8892

89-
See [autoderef/foo](_demo/autoderef/foo).
93+
### Define Python Objects with Go
9094

9195
```go
96+
// _demo/module/foo/foo.go
97+
98+
package foo
99+
100+
import (
101+
"fmt"
102+
103+
gp "github.com/cpunion/go-python"
104+
)
105+
92106
type Point struct {
93107
X float64
94108
Y float64
@@ -125,79 +139,83 @@ func InitFooModule() gp.Module {
125139
gp.AddType[Point](m, (*Point).init, "Point", "Point objects")
126140
return m
127141
}
142+
128143
```
129144

130145
Call foo module from Python and Go.
131146

132147
```go
148+
// _demo/module/module.go
149+
133150
package main
134151

135152
import (
136153
"fmt"
137-
"runtime"
138154

139155
gp "github.com/cpunion/go-python"
140-
"github.com/cpunion/go-python/_demo/autoderef/foo"
141-
pymath "github.com/cpunion/go-python/math"
156+
"github.com/cpunion/go-python/_demo/module/foo"
142157
)
143158

144-
func Main1() {
145-
gp.RunString(`
146-
import foo
147-
point = foo.Point(3, 4)
148-
print("dir(point):", dir(point))
149-
print("x:", point.x)
150-
print("y:", point.y)
151-
152-
print("distance:", point.distance())
153-
154-
point.move(1, 2)
155-
print("x:", point.x)
156-
print("y:", point.y)
157-
print("distance:", point.distance())
159+
func main() {
160+
gp.Initialize()
161+
defer gp.Finalize()
162+
fooMod := foo.InitFooModule()
163+
gp.GetModuleDict().SetString("foo", fooMod)
158164

159-
point.print()
160-
`)
165+
Main1(fooMod)
166+
Main2()
161167
}
162168

163-
func Main2(fooMod gp.Module) {
164-
sum := fooMod.Call("add", gp.MakeLong(1), gp.MakeLong(2)).AsLong()
169+
func Main1(fooMod gp.Module) {
170+
sum := fooMod.Call("add", 1, 2).AsLong()
165171
fmt.Printf("Sum of 1 + 2: %d\n", sum.Int64())
166172

167173
dict := fooMod.Dict()
168174
Point := dict.Get(gp.MakeStr("Point")).AsFunc()
169175

170-
point := Point.Call(gp.MakeLong(3), gp.MakeLong(4))
176+
point := Point.Call(3, 4)
171177
fmt.Printf("dir(point): %v\n", point.Dir())
172-
fmt.Printf("x: %v, y: %v\n", point.GetAttr("x"), point.GetAttr("y"))
178+
fmt.Printf("x: %v, y: %v\n", point.Attr("x"), point.Attr("y"))
173179

174180
distance := point.Call("distance").AsFloat()
175181
fmt.Printf("Distance of 3 * 4: %f\n", distance.Float64())
176182

177-
point.Call("move", gp.MakeFloat(1), gp.MakeFloat(2))
178-
fmt.Printf("x: %v, y: %v\n", point.GetAttr("x"), point.GetAttr("y"))
183+
point.Call("move", 1, 2)
184+
fmt.Printf("x: %v, y: %v\n", point.Attr("x"), point.Attr("y"))
179185

180186
distance = point.Call("distance").AsFloat()
181187
fmt.Printf("Distance of 4 * 6: %f\n", distance.Float64())
188+
182189
point.Call("print")
183190
}
184191

185-
func main() {
186-
gp.Initialize()
187-
defer gp.Finalize()
188-
fooMod := foo.InitFooModule()
189-
gp.GetModuleDict().Set(gp.MakeStr("foo").Object, fooMod.Object)
192+
func Main2() {
193+
fmt.Printf("=========== Main2 ==========\n")
194+
_ = gp.RunString(`
195+
import foo
196+
point = foo.Point(3, 4)
197+
print("dir(point):", dir(point))
198+
print("x:", point.x)
199+
print("y:", point.y)
190200
191-
Main1()
192-
Main2(fooMod)
201+
print("distance:", point.distance())
202+
203+
point.move(1, 2)
204+
print("x:", point.x)
205+
print("y:", point.y)
206+
print("distance:", point.distance())
207+
208+
point.print()
209+
`)
193210
}
211+
194212
```
195213

196214
### Call gradio
197215

198-
See [gradio](_demo/gradio).
199-
200216
```go
217+
// _demo/gradio/gradio.go
218+
201219
package main
202220

203221
import (
@@ -260,43 +278,10 @@ func main() {
260278
})
261279
textbox := gr.Call("Textbox")
262280
examples := gr.Call("Examples", [][]string{{"Chicago"}, {"Little Rock"}, {"San Francisco"}}, textbox)
263-
dataset := examples.GetAttr("dataset")
281+
dataset := examples.Attr("dataset")
264282
dropdown.Call("change", fn, dropdown, dataset)
265283
})
266284
demo.Call("launch")
267285
}
268-
```
269-
270-
### Call matplotlib
271-
272-
See [plot](_demo/plot).
273-
274-
```go
275-
package main
276-
277-
import gp "github.com/cpunion/go-python"
278-
279-
type plt struct {
280-
gp.Module
281-
}
282-
283-
func Plt() plt {
284-
return plt{gp.ImportModule("matplotlib.pyplot")}
285-
}
286-
287-
func (m plt) Plot(args ...any) gp.Object {
288-
return m.Call("plot", args...)
289-
}
290286

291-
func (m plt) Show() {
292-
m.Call("show")
293-
}
294-
295-
func main() {
296-
gp.Initialize()
297-
defer gp.Finalize()
298-
plt := Plt()
299-
plt.Plot(gp.MakeTuple(5, 10), gp.MakeTuple(10, 15), gp.KwArgs{"color": "red"})
300-
plt.Show()
301-
}
302287
```

_demo/autoderef/autoderef.go

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,13 @@ import (
55
"runtime"
66

77
gp "github.com/cpunion/go-python"
8-
"github.com/cpunion/go-python/_demo/autoderef/foo"
98
pymath "github.com/cpunion/go-python/math"
109
)
1110

1211
func main() {
1312
gp.Initialize()
1413
defer gp.Finalize()
15-
fooMod := foo.InitFooModule()
16-
gp.GetModuleDict().SetString("foo", fooMod)
1714

18-
Main1(fooMod)
19-
Main2()
20-
Main3()
21-
}
22-
23-
func Main1(fooMod gp.Module) {
24-
fmt.Printf("=========== Main1 ==========\n")
25-
sum := fooMod.Call("add", 1, 2).AsLong()
26-
fmt.Printf("Sum of 1 + 2: %d\n", sum.Int64())
27-
28-
dict := fooMod.Dict()
29-
Point := dict.Get(gp.MakeStr("Point")).AsFunc()
30-
31-
point := Point.Call(3, 4)
32-
fmt.Printf("dir(point): %v\n", point.Dir())
33-
fmt.Printf("x: %v, y: %v\n", point.Attr("x"), point.Attr("y"))
34-
35-
distance := point.Call("distance").AsFloat()
36-
fmt.Printf("Distance of 3 * 4: %f\n", distance.Float64())
37-
38-
point.Call("move", 1, 2)
39-
fmt.Printf("x: %v, y: %v\n", point.Attr("x"), point.Attr("y"))
40-
41-
distance = point.Call("distance").AsFloat()
42-
fmt.Printf("Distance of 4 * 6: %f\n", distance.Float64())
43-
point.Call("print")
44-
}
45-
46-
func Main2() {
47-
fmt.Printf("=========== Main2 ==========\n")
48-
_ = gp.RunString(`
49-
import foo
50-
point = foo.Point(3, 4)
51-
print("dir(point):", dir(point))
52-
print("x:", point.x)
53-
print("y:", point.y)
54-
55-
print("distance:", point.distance())
56-
57-
point.move(1, 2)
58-
print("x:", point.x)
59-
print("y:", point.y)
60-
print("distance:", point.distance())
61-
62-
point.print()
63-
`)
64-
}
65-
66-
func Main3() {
6715
pythonCode := `
6816
def allocate_memory():
6917
return bytearray(10 * 1024 * 1024)
@@ -112,6 +60,5 @@ for i in range(10):
11260
}
11361
}
11462

115-
gp.Finalize()
11663
fmt.Printf("Done\n")
11764
}
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
package foo
22

3-
/*
4-
#cgo pkg-config: python3-embed
5-
#include <Python.h>
6-
*/
7-
import "C"
8-
93
import (
104
"fmt"
115

0 commit comments

Comments
 (0)