Skip to content

Multi-context execution (py.Context) #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 81 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
fc66b7f
py.CompileMode now a string enum
Dec 8, 2021
d10c35f
addressed benign Go warnings
Dec 8, 2021
2161b71
make Float implement M__index__
Dec 8, 2021
247ec42
added string helper functions
Dec 8, 2021
9a75271
updated to 1.15
Dec 8, 2021
90a8988
new Ctx-aware module model
Dec 9, 2021
c51644e
py.Ctx model allows concurrent execution
Dec 9, 2021
7f7d213
repl.New() now accepts an existing py.Ctx
Dec 9, 2021
708bb7e
fixed typo in callInternal()
Dec 9, 2021
a244256
unbroke test compatibilty
Dec 9, 2021
75d8743
cleaned up py.Ctx initialization wrt sys and DefaultCtxOpts()
Dec 10, 2021
d5fb8e8
added multi Ctx benchmark
Dec 10, 2021
269bdc0
WIP
Dec 10, 2021
4db9c65
fixed py.NewCtx not being set
Dec 10, 2021
53d05db
fixed List.sort() detection of instance invocation
Dec 10, 2021
7fd1c29
separated path resolution from path evaluation
Dec 11, 2021
8f3a104
removed cruft
Dec 18, 2021
5ebaf66
place ctx at top
Dec 18, 2021
d423190
err msg tweak
drew-512 Dec 18, 2021
b33c266
typo fix
drew-512 Dec 18, 2021
efca761
for loops we can all agree on
drew-512 Dec 18, 2021
1569475
removed outdated comments
Dec 18, 2021
a9c5f1f
Merge branch 'master' of https://github.com/drew-512/gpython
Dec 18, 2021
6372aee
added info for Frame.Ctx
Dec 19, 2021
4af4f90
removed unnecessary assignment
Dec 19, 2021
aec59f8
copyright and formatting
Dec 19, 2021
74f1a19
Ctx and NewCtx docs
Dec 19, 2021
8fa6622
switch cleanup
Dec 19, 2021
26fb9aa
leaner ModuleImpl schema
Dec 20, 2021
8614aeb
added GetDict() impl
Dec 20, 2021
86531c6
Compile() now returns py.Code (vs py.Object)
Dec 20, 2021
f71b961
tighter py.Ctx invocation patterns
Dec 20, 2021
77a7954
WIP
Dec 20, 2021
6bba7da
reverted to 644
Dec 20, 2021
46d1052
chmod 644
Dec 20, 2021
66fad23
enhanced RunFile() to enclose RunInNewModule()
Dec 27, 2021
95afd7b
Update examples/multi-ctx/main.go
drew-512 Jan 10, 2022
319edb7
Update modules/runtime.go
drew-512 Jan 10, 2022
06a2eae
Update modules/runtime.go
drew-512 Jan 10, 2022
8d4d586
Update modules/runtime.go
drew-512 Jan 10, 2022
d475e67
Update vm/eval.go
drew-512 Jan 10, 2022
6afac99
Update py/run.go
drew-512 Jan 10, 2022
931c346
Update modules/runtime.go
drew-512 Jan 10, 2022
a7ce4bb
Update modules/runtime.go
drew-512 Jan 10, 2022
c1f8809
Update modules/runtime.go
drew-512 Jan 10, 2022
9f02cf1
Update modules/runtime.go
drew-512 Jan 10, 2022
85e3a4a
Update modules/runtime.go
drew-512 Jan 10, 2022
fbec34e
Update modules/runtime.go
drew-512 Jan 10, 2022
dbd54d8
code cleanuo
Jan 10, 2022
71a5604
Module doc str
Jan 10, 2022
634823c
consistent arg order of NewFunction
Jan 10, 2022
6a0c11e
license and formatting
Jan 10, 2022
6242836
comment and cruft cleanup
Jan 10, 2022
61c7830
renamed Store to ModuleStore + docs
Jan 10, 2022
bb98aa8
added import docs
Jan 10, 2022
36b1018
shadowing cleanup
Jan 10, 2022
f2602bc
docs tweaks
Jan 10, 2022
e25e3bb
code cleanup
Jan 10, 2022
9421b6b
synced with gpython
Jan 10, 2022
92aed8a
Merge branch 'go-python:master' into master
drew-512 Jan 10, 2022
96515ea
Update modules/runtime.go
drew-512 Jan 10, 2022
f62f2b7
Update modules/runtime.go
drew-512 Jan 10, 2022
3bdba62
reverted from %w to %v (linux CI compile failure)
Jan 10, 2022
8033615
renamed py.Ctx to py.Context
Jan 10, 2022
681abf2
added Context.Close() and ModuleImpl.OnContextClosed()
Jan 18, 2022
7508556
docs and LoadIntsFromList
Jan 19, 2022
99bcb40
rename edits
Jan 21, 2022
0cc7650
push/popBusy now private
Jan 21, 2022
9afb2d3
doc edits
Jan 21, 2022
7d2a8d9
fixed kwarg issues in ParseTupleAndKeywords() and builtin_print test
Jan 25, 2022
ba7db80
added Flush kwarg to print test
Jan 25, 2022
61e42c8
added embedding example
Jan 26, 2022
168c337
main README makeover
Jan 26, 2022
2b7e1ab
fixed type conversion
Jan 26, 2022
3821b4b
Update README.md
drew-512 Jan 28, 2022
170f0d2
Update README.md
drew-512 Jan 28, 2022
b68b9f6
Update py/util.go
drew-512 Jan 28, 2022
759023b
Update py/util.go
drew-512 Jan 28, 2022
0c77716
Update py/util.go
drew-512 Jan 28, 2022
505755d
LoadIntsFromList cleanup
Jan 28, 2022
02d6b3e
comment edits
Jan 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added embedding example
  • Loading branch information
Drew O'Meara committed Jan 26, 2022
commit 61e42c8b6eddecce05958f913a231df27e2650dc
102 changes: 102 additions & 0 deletions examples/embedding/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
## Embedding gpython

This example demonstrates how embed gpython into a Go application.


### Why embed gpython?

Embedding a highly capable and familiar "interpreted" language allows your users
to easily augment app behavior, configuration, and customization -- all post-deployment.

Have you ever found an exciting software project but lose interest when you discover that you
also need to learn an esoteric language schema? In an era of limited attention span,
most people are generally turned off if they have to learn a new language in addition to learning
to use your app.

If you consider [why use Python](https://www.stxnext.com/what-is-python-used-for/), then perhaps also
consider your users could immediately feel interested to hear that your software offers
an additional familiar dimension of value.

Python is widespread in finance, sciences of all kinds, hobbyist programming and is often
endearingly regarded as most popular programming language for non-developers.
If your application can be driven by embedded Python, then chances are others will
feel excited and empowered that your project can be used out of the box
with positive feelings of being in familiar territory.

### But what about the lack of python modules?

There are only be a small number of native modules available, but don't forget you have the entire
Go standard library and *any* Go package you can name at your fingertips to expose!
This plus multi-context capability gives gpython enormous potential on how it can
serve your project.

So basically, gpython is only off the table if you need to run python that makes heavy use of
modules that are only available in CPython.

### Packing List

| | |
|---------------------- | ------------------------------------------------------------------|
| `main.go` | if no args, runs in REPL mode, otherwise runs the given file |
| `lib/mylib.py` | models a library that your application would expose |
| `lib/REPL-startup.py` | invoked by `main.go` when starting REPL mode |
| `mylib-demo.py` | models a user-authored script that consumes `mylib` |
| `mylib.module.go` | Go implementation of `mylib_go` consumed by `mylib` |


### Invoking a Python Script

```bash
$ cd examples/embedding/
$ go build .
$ ./embedding mylib-demo.py
```
```
Welcome to a gpython embedded example,
where your wildest Go-based python dreams come true!

==========================================================
Python 3.4 (github.com/go-python/gpython)
go1.17.6 on darwin amd64
==========================================================

Spring Break itinerary:
Stop 1: Miami, Florida | 7 nights
Stop 2: Mallorca, Spain | 3 nights
Stop 3: Ibiza, Spain | 14 nights
Stop 4: Monaco | 12 nights
### Made with Vacaton 1.0 by Fletch F. Fletcher

I bet Monaco will be the best!
```

### REPL Mode

```bash
$ ./embedding
```
```
======= Entering REPL mode, press Ctrl+D to exit =======

==========================================================
Python 3.4 (github.com/go-python/gpython)
go1.17.6 on darwin amd64
==========================================================

>>> v = Vacation("Spring Break", Stop("Florida", 3), Stop("Nice", 7))
>>> print(str(v))
Spring Break, 2 stop(s)
>>> v.PrintItinerary()
Spring Break itinerary:
Stop 1: Florida | 3 nights
Stop 2: Nice | 7 nights
### Made with Vacaton 1.0 by Fletch F. Fletcher
```

## Takeways

- `main.go` demonstrates high-level convenience functions such as `py.RunFile()`.
- Embedding any Go `struct` only requires that it implements `py.Object`, which is a single function:
`Type() *py.Type`
- See [py/run.go](https://github.com/go-python/gpython/tree/master/py/run.go) for more about interpreter instances and `py.Context`
- There are many helper functions available for you in [py/util.go](https://github.com/go-python/gpython/tree/master/py/util.go) and your contributions are welcome!
8 changes: 8 additions & 0 deletions examples/embedding/lib/REPL-startup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@


# This file is called from main.go when in REPL mode

# This is here to demonstrate making life easier for your users in REPL mode
# by doing pre-setup here so they don't have to import every time they start.
from mylib import *

53 changes: 53 additions & 0 deletions examples/embedding/lib/mylib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import mylib_go as _go

PY_VERSION = _go.PY_VERSION
GO_VERSION = _go.GO_VERSION


print('''
==========================================================
%s
%s
==========================================================
''' % (PY_VERSION, GO_VERSION))


def Stop(location, num_nights = 2):
return _go.VacationStop_new(location, num_nights)


class Vacation:

def __init__(self, tripName, *stops):
self._v, self._libVers = _go.Vacation_new()
self.tripName = tripName
self.AddStops(*stops)

def __str__(self):
return "%s, %d stop(s)" % (self.tripName, self.NumStops())

def NumStops(self):
return self._v.num_stops()

def GetStop(self, stop_num):
return self._v.get_stop(stop_num)

def AddStops(self, *stops):
self._v.add_stops(stops)

def PrintItinerary(self):
print(self.tripName, "itinerary:")
i = 1
while 1:

try:
stop = self.GetStop(i)
except IndexError:
break

print(" Stop %d: %s" % (i, str(stop)))
i += 1

print("### Made with %s " % self._libVers)


51 changes: 51 additions & 0 deletions examples/embedding/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2022 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
"flag"
"fmt"

// This initializes gpython for runtime execution and is essential.
// It defines forward-declared symbols and registers native built-in modules, such as sys and time.
_ "github.com/go-python/gpython/modules"

// Commonly consumed gpython
"github.com/go-python/gpython/py"
"github.com/go-python/gpython/repl"
"github.com/go-python/gpython/repl/cli"
)

func main() {
flag.Parse()
runWithFile(flag.Arg(0))
}

func runWithFile(pyFile string) error {

// See type Context interface and related docs
ctx := py.NewContext(py.DefaultContextOpts())

var err error
if len(pyFile) == 0 {
replCtx := repl.New(ctx)

fmt.Print("\n======= Entering REPL mode, press Ctrl+D to exit =======\n")

_, err = py.RunFile(ctx, "lib/REPL-startup.py", py.CompileOpts{}, replCtx.Module)
if err == nil {
cli.RunREPL(replCtx)
}

} else {
_, err = py.RunFile(ctx, pyFile, py.CompileOpts{}, nil)
}

if err != nil {
py.TracebackDump(err)
}

return err
}
15 changes: 15 additions & 0 deletions examples/embedding/mylib-demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
print('''
Welcome to a gpython embedded example,
where your wildest Go-based python dreams come true!''')

# This is a model for a public/user-side script that you or users would maintain,
# offering an open canvas to drive app behavior, customization, or anything you can dream up.
#
# Modules you offer for consumption can also serve to document such things.
from mylib import *

springBreak = Vacation("Spring Break", Stop("Miami, Florida", 7), Stop("Mallorca, Spain", 3))
springBreak.AddStops(Stop("Ibiza, Spain", 14), Stop("Monaco", 12))
springBreak.PrintItinerary()

print("\nI bet %s will be the best!\n" % springBreak.GetStop(4).Get()[0])
Loading