{"id":20459743,"url":"https://github.com/rosbit/go-epy","last_synced_at":"2025-04-13T05:51:25.164Z","repository":{"id":49372830,"uuid":"451790183","full_name":"rosbit/go-epy","owner":"rosbit","description":"Extending starlark-go and making Starlark as a pragmatic embeddable Python. 基于starlark-go的嵌入式python","archived":false,"fork":false,"pushed_at":"2023-11-19T06:14:02.000Z","size":38,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-26T22:36:09.547Z","etag":null,"topics":["embeddable","extensions","golang","python","starlark","starlark-go"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rosbit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-01-25T08:12:17.000Z","updated_at":"2023-07-05T16:42:22.000Z","dependencies_parsed_at":"2023-11-19T04:25:01.042Z","dependency_job_id":"1396c6e7-5c25-44e6-a7ac-22dd77513e4a","html_url":"https://github.com/rosbit/go-epy","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rosbit%2Fgo-epy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rosbit%2Fgo-epy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rosbit%2Fgo-epy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rosbit%2Fgo-epy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rosbit","download_url":"https://codeload.github.com/rosbit/go-epy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248670500,"owners_count":21142901,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["embeddable","extensions","golang","python","starlark","starlark-go"],"created_at":"2024-11-15T12:17:08.473Z","updated_at":"2025-04-13T05:51:25.139Z","avatar_url":"https://github.com/rosbit.png","language":"Go","readme":"# go-epy, an embeddable Python\n\n[Starlark in Go](https://github.com/google/starlark-go), a Python-like script language, is an interpreter for Starlark implemented in pure Go. \n\n`go-epy` is a package extending the starlark-go and making it a **pragmatic embeddable** language.\nWith some helper functions provided by `go-epy`, calling Golang functions or modules from Starlark, \nor calling Starlark functions from Golang are both very simple. So, with the help of `go-epy`, starlark-go\ncan be looked as **an embeddable Python**.\n\n### Usage\n\nThe package is fully go-getable, so, just type\n\n  `go get github.com/rosbit/go-epy`\n\nto install.\n\n#### 1. Evaluate expressions\n\n```go\npackage main\n\nimport (\n  \"github.com/rosbit/go-epy\"\n  \"fmt\"\n)\n\nfunc main() {\n  ctx := epy.New()\n\n  res, _ := ctx.Eval(\"1 + 2\", nil)\n  fmt.Println(\"result is:\", res)\n}\n```\n\n#### 2. Go calls Starlark function\n\nSuppose there's a Starlark file named `a.py` like this:\n\n```python\ndef add(a, b):\n    return a+b\n```\n\none can call the Starlark function `add()` in Go code like the following:\n\n```go\npackage main\n\nimport (\n  \"github.com/rosbit/go-epy\"\n  \"fmt\"\n)\n\nvar add func(int, int)int\n\nfunc main() {\n  ctx := epy.New()\n  if err := ctx.LoadFile(\"a.py\", nil); err != nil {\n     fmt.Printf(\"%v\\n\", err)\n     return\n  }\n\n  if err := ctx.BindFunc(\"add\", \u0026add); err != nil {\n     fmt.Printf(\"%v\\n\", err)\n     return\n  }\n\n  res := add(1, 2)\n  fmt.Println(\"result is:\", res)\n}\n```\n\n#### 3. Starlark calls Go function\n\nStarlark calling Go function is also easy. In the Go code, make a Golang function\nas Starlark built-in function by calling `MakeBuiltinFunc(\"funcname\", function)`. There's the example:\n\n```go\npackage main\n\nimport \"github.com/rosbit/go-epy\"\n\n// function to be called by Starlark\nfunc adder(a1 float64, a2 float64) float64 {\n    return a1 + a2\n}\n\nfunc main() {\n  ctx := epy.New()\n\n  ctx.MakeBuiltinFunc(\"adder\", adder)\n  ctx.EvalFile(\"b.py\", nil)  // b.py containing code calling \"adder\"\n}\n```\n\nIn Starlark code, one can call the registered function directly. There's the example `b.py`.\n\n```python\nr = adder(1, 100)   # the function \"adder\" is implemented in Go\nprint(r)\n```\n\n#### 4. Make Go struct instance as a Starlark module\n\nThis package provides a function `SetModule` which will convert a Go struct instance into\na Starlark module. There's the example `c.py`, `m` is the module provided by Go code:\n\n```python\nm.incAge(10)\nprint(m)\n\nprint('m.name', m.name)\nprint('m.age', m.age)\n```\n\nThe Go code is like this:\n\n```go\npackage main\n\nimport \"github.com/rosbit/go-epy\"\n\ntype M struct {\n   Name string\n   Age int\n}\nfunc (m *M) IncAge(a int) {\n   m.Age += a\n}\n\nfunc main() {\n  ctx := epy.New()\n  ctx.SetModule(\"m\", \u0026M{Name:\"rosbit\", Age: 1}) // \"m\" is the module name\n\n  ctx.EvalFile(\"c.py\", nil)\n}\n```\n\n#### 5. Set many built-in functions and modules at one time\n\nIf there're a lot of functions and modules to be registered, a map could be constructed and put as an\nargument for functions `LoadFile`, `LoadScript`, `EvalFile` or `Eval`.\n\n```go\npackage main\n\nimport \"github.com/rosbit/go-epy\"\nimport \"fmt\"\n\ntype M struct {\n   Name string\n   Age int\n}\nfunc (m *M) IncAge(a int) {\n   m.Age += a\n}\n\nfunc adder(a1 float64, a2 float64) float64 {\n    return a1 + a2\n}\n\nfunc main() {\n  vars := map[string]interface{}{\n     \"m\": \u0026M{Name:\"rosbit\", Age:1}, // to Starlark module\n     \"adder\": adder,                // to Starlark built-in function\n     \"a\": []int{1,2,3}              // to Starlark array\n  }\n\n  ctx := epy.New()\n  if err := ctx.LoadFile(\"file.py\", vars); err != nil {\n     fmt.Printf(\"%v\\n\", err)\n     return\n  }\n\n  res, err := ctx.GetGlobal(\"a\") // get the value of var named \"a\". Any variables in script could be get by GetGlobal\n  if err != nil {\n     fmt.Printf(\"%v\\n\", err)\n     return\n  }\n  fmt.Printf(\"res:\", res)\n}\n```\n\n#### 6. Wrap Go functions as Starlark module\n\nThis package also provides a function `CreateModule` which will create a Starlark module integrating any\nGo functions as module methods. There's the example `d.py` which will use module `tm` provided by Go code:\n\n```python\na = tm.newA(\"rosbit\", 10)\na.incAge(10)\nprint(a)\n\ntm.printf('a.name: %s\\n', a.name)\ntm.printf('a.age: %d\\n', a.age)\n```\n\nThe Go code is like this:\n\n```go\npackage main\n\nimport (\n  \"github.com/rosbit/go-epy\"\n  \"fmt\"\n)\n\ntype A struct {\n   Name string\n   Age int\n}\nfunc (m *A) IncAge(a int) {\n   m.Age += a\n}\nfunc newA(name string, age int) *A {\n   return \u0026A{Name: name, Age: age}\n}\n\nfunc main() {\n  ctx := epy.New()\n  ctx.CreateModule(\"tm\", map[string]interface{}{ // module name is \"tm\"\n     \"newA\": newA,            // make user defined function as module method named \"tm.newA\"\n     \"printf\": fmt.Printf,    // make function in a standard package named \"tm.printf\"\n  })\n\n  ctx.EvalFile(\"d.py\", nil)\n}\n```\n\n### Status\n\nThe package is not fully tested, so be careful.\n\n### Contribution\n\nPull requests are welcome! Also, if you want to discuss something send a pull request with proposal and changes.\n\n__Convention:__ fork the repository and make changes on your fork in a feature branch.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frosbit%2Fgo-epy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frosbit%2Fgo-epy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frosbit%2Fgo-epy/lists"}