diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..674f001 --- /dev/null +++ b/Makefile @@ -0,0 +1,75 @@ +# Basic Go makefile + +GOCMD=go +GOBUILD=$(GOCMD) build +GOCLEAN=$(GOCMD) clean +GOTEST=$(GOCMD) test +GOGET=$(GOCMD) get + +DIRS=`go list ./...` + +all: build + +build: + @echo "GO111MODULE = $(value GO111MODULE)" + $(GOBUILD) -v $(DIRS) + +test: + @echo "GO111MODULE = $(value GO111MODULE)" + $(GOTEST) -v $(DIRS) + +clean: + @echo "GO111MODULE = $(value GO111MODULE)" + $(GOCLEAN) ./... + +fmts: + gofmt -s -w . + +vet: + @echo "GO111MODULE = $(value GO111MODULE)" + $(GOCMD) vet $(DIRS) | grep -v unkeyed + +tidy: export GO111MODULE = on +tidy: + @echo "GO111MODULE = $(value GO111MODULE)" + go mod tidy + +mod-update: export GO111MODULE = on +mod-update: + @echo "GO111MODULE = $(value GO111MODULE)" + go get -u ./... + go mod tidy + +# gopath-update is for GOPATH to get most things updated. +# need to call it in a target executable directory +gopath-update: export GO111MODULE = off +gopath-update: + @echo "GO111MODULE = $(value GO111MODULE)" + go get -u ./... + +# NOTE: MUST update version number here prior to running 'make release' and edit this file! +VERS=v0.4.1 +PACKAGE=main +GIT_COMMIT=`git rev-parse --short HEAD` +VERS_DATE=`date -u +%Y-%m-%d\ %H:%M` +VERS_FILE=version.go + +release: + /bin/rm -f $(VERS_FILE) + @echo "// WARNING: auto-generated by Makefile release target -- run 'make release' to update" > $(VERS_FILE) + @echo "" >> $(VERS_FILE) + @echo "package $(PACKAGE)" >> $(VERS_FILE) + @echo "" >> $(VERS_FILE) + @echo "const (" >> $(VERS_FILE) + @echo " Version = \"$(VERS)\"" >> $(VERS_FILE) + @echo " GitCommit = \"$(GIT_COMMIT)\" // the commit JUST BEFORE the release" >> $(VERS_FILE) + @echo " VersionDate = \"$(VERS_DATE)\" // UTC" >> $(VERS_FILE) + @echo ")" >> $(VERS_FILE) + @echo "" >> $(VERS_FILE) + goimports -w $(VERS_FILE) + /bin/cat $(VERS_FILE) + git commit -am "$(VERS) release" + git tag -a $(VERS) -m "$(VERS) release" + git push + git push origin --tags + diff --git a/README.md b/README.md index 85d7338..d383e78 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,12 @@ GoToPy is a Go to Python converter -- translates Go code into Python code. +To install, do standard: + +```Go +$ go install github.com/go-python/gotopy@latest +``` + It is based on the Go `gofmt` command source code and the go `printer` package, which parses Go files and writes them out according to standard go formatting. We have modified the `printer` code in the `pyprint` package to instead print out Python code. @@ -19,4 +25,9 @@ The `-gogi` flag generates [GoGi](https:://github.com/goki/gi) specific Python c * switch -> ifs.. -- grab switch expr and put into each if +* string .contains -> "el" in str + +* map access with 2 vars = if el in map: mv = map[el] + +* for range with 2 vars = enumerate(slice) diff --git a/format.go b/format.go index 5e596e8..31d1380 100644 --- a/format.go +++ b/format.go @@ -15,7 +15,7 @@ import ( "go/ast" "go/token" - "github.com/goki/gotopy/pyprint" + "github.com/go-python/gotopy/pyprint" ) // format formats the given package file originally obtained from src diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..de8f464 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/go-python/gotopy + +go 1.15 diff --git a/gotopy.go b/gotopy.go index 1e00622..66b7715 100644 --- a/gotopy.go +++ b/gotopy.go @@ -26,8 +26,8 @@ import ( "runtime/pprof" "strings" - "github.com/goki/gotopy/diff" - "github.com/goki/gotopy/pyprint" + "github.com/go-python/gotopy/diff" + "github.com/go-python/gotopy/pyprint" ) var ( diff --git a/pyedits.go b/pyedits.go index faa3fff..9b6f3d9 100644 --- a/pyedits.go +++ b/pyedits.go @@ -8,7 +8,7 @@ import ( "bytes" "strings" - "github.com/goki/gotopy/pyprint" + "github.com/go-python/gotopy/pyprint" ) // moveLines moves the st,ed region to 'to' line @@ -146,7 +146,7 @@ func pyEditsReplace(lines [][]byte) { floatp := []byte("float(") stringp := []byte("string(") strp := []byte("str(") - slicestr := []byte("[]string(") + slicestr := []byte("[]str(") sliceint := []byte("[]int(") slicefloat64 := []byte("[]float64(") slicefloat32 := []byte("[]float32(") @@ -156,6 +156,10 @@ func pyEditsReplace(lines [][]byte) { goslicefloat32 := []byte("go.Slice_float32([") stringsdot := []byte("strings.") copyp := []byte("copy(") + eqgonil := []byte(" == go.nil") + eqgonil0 := []byte(" == 0") + negonil := []byte(" != go.nil") + negonil0 := []byte(" != 0") gopy := (printerMode&pyprint.GoPy != 0) // gogi := (printerMode&pyprint.GoGi != 0) @@ -165,6 +169,8 @@ func pyEditsReplace(lines [][]byte) { ln = bytes.Replace(ln, float32p, floatp, -1) ln = bytes.Replace(ln, stringp, strp, -1) ln = bytes.Replace(ln, forblank, fornoblank, -1) + ln = bytes.Replace(ln, eqgonil, eqgonil0, -1) + ln = bytes.Replace(ln, negonil, negonil0, -1) if bytes.Contains(ln, fmtSprintf) { if bytes.Contains(ln, []byte("%")) { diff --git a/pyprint/nodes.go b/pyprint/nodes.go index bcfdd94..5e1a48b 100644 --- a/pyprint/nodes.go +++ b/pyprint/nodes.go @@ -793,13 +793,13 @@ func isBinary(expr ast.Expr) bool { } func (p *printer) ident(id *ast.Ident) { - // if id.Name == "AlphaCycle" { - // fmt.Printf("%+V\n", id) + // todo: this was an attempt to deal with "enums" by printing type then + // name -- didn't work -- could return to this but not high priority + // if id.Obj != nil && id.Obj.Kind == ast.Con && id.Name != id.Obj.Name { + // // constants are assumed to be Enums -- use type scoping for python + // // fmt.Printf("id: %+v\n", id) + // p.print(id.Obj.Name, token.PERIOD) // } - // todo: seems like Obj is typically nil.. not sure how to fix this.. - if id.Obj != nil && id.Obj.Kind == ast.Con { // constants are assumed to be Enums -- use type scoping for python - p.print(id.Obj.Name, token.PERIOD) - } p.print(id.Pos(), id) } diff --git a/testdata/ra25.golden b/testdata/ra25.golden index 1904589..0c4074b 100644 --- a/testdata/ra25.golden +++ b/testdata/ra25.golden @@ -55,7 +55,7 @@ def guirun(): win.StartEventLoop() # LogPrec is precision for saving float values in logs -LogPrec.LogPrec = 4 +LogPrec = 4 # ParamSets is the default set of parameters -- Base is always applied, and others can be optionally # selected to apply on top of that @@ -279,7 +279,7 @@ class Sim(pygiv.ClassViewObj): ss.TrainUpdt = leabra.AlphaCycle ss.TestUpdt = leabra.Cycle ss.TestInterval = 5 - ss.LayStatNms = []str("Hidden1", "Hidden2", "Output") + ss.LayStatNms = go.Slice_string(["Hidden1", "Hidden2", "Output"]) def Config(ss): """ @@ -467,7 +467,7 @@ class Sim(pygiv.ClassViewObj): (training, testing, etc). """ - lays = []str("Input", "Output") + lays = go.Slice_string(["Input", "Output"]) for lnm in lays : ly = leabra.LeabraLayer(ss.Net.LayerByName(lnm)).AsLeabra() pats = en.State(ly.Nm) @@ -699,7 +699,7 @@ class Sim(pygiv.ClassViewObj): """ if sheet == "": - ss.Params.ValidateSheets([]str("Network", "Sim")) + ss.Params.ValidateSheets(go.Slice_string(["Network", "Sim"])) err = ss.SetParamsSet("Base", sheet, setMsg) if ss.ParamSet != "" and ss.ParamSet != "Base": sps = ss.ParamSet.split() @@ -735,8 +735,8 @@ class Sim(pygiv.ClassViewObj): dt.SetMetaData("desc", "Training patterns") dt.SetFromSchema(etable.Schema( ("Name", etensor.STRING, go.nil, go.nil), - ("Input", etensor.FLOAT32, go.Slice_int([5, 5]), []str("Y", "X")), - ("Output", etensor.FLOAT32, go.Slice_int([5, 5]), []str("Y", "X")), + ("Input", etensor.FLOAT32, go.Slice_int([5, 5]]), go.Slice_string(["Y", "X")), + ("Output", etensor.FLOAT32, go.Slice_int([5, 5]]), go.Slice_string(["Y", "X")), ), 25) patgen.PermutedBinaryRows(dt.Cols[1], 6, 1, 0) @@ -849,7 +849,7 @@ class Sim(pygiv.ClassViewObj): dt.SetMetaData("name", "TrnEpcLog") dt.SetMetaData("desc", "Record of performance over epochs of training") dt.SetMetaData("read-only", "true") - dt.SetMetaData("precision", str(LogPrec.LogPrec)) + dt.SetMetaData("precision", str(LogPrec)) sch = etable.Schema( ("Run", etensor.INT64, go.nil, go.nil), @@ -928,7 +928,7 @@ class Sim(pygiv.ClassViewObj): dt.SetMetaData("name", "TstTrlLog") dt.SetMetaData("desc", "Record of testing per input pattern") dt.SetMetaData("read-only", "true") - dt.SetMetaData("precision", str(LogPrec.LogPrec)) + dt.SetMetaData("precision", str(LogPrec)) nt = ss.TestEnv.Table.Len() # number in view sch = etable.Schema( @@ -1012,7 +1012,7 @@ class Sim(pygiv.ClassViewObj): dt.SetMetaData("name", "TstEpcLog") dt.SetMetaData("desc", "Summary stats for testing trials") dt.SetMetaData("read-only", "true") - dt.SetMetaData("precision", str(LogPrec.LogPrec)) + dt.SetMetaData("precision", str(LogPrec)) dt.SetFromSchema(etable.Schema( ("Run", etensor.INT64, go.nil, go.nil), @@ -1060,7 +1060,7 @@ class Sim(pygiv.ClassViewObj): dt.SetMetaData("name", "TstCycLog") dt.SetMetaData("desc", "Record of activity etc over one trial by cycle") dt.SetMetaData("read-only", "true") - dt.SetMetaData("precision", str(LogPrec.LogPrec)) + dt.SetMetaData("precision", str(LogPrec)) np = 100 # max cycles sch = etable.Schema( @@ -1111,7 +1111,7 @@ class Sim(pygiv.ClassViewObj): dt.SetCellFloat("CosDiff", row, agg.Mean(epcix, "CosDiff")[0]) runix = etable.NewIdxView(dt) - spl = split.GroupBy(runix, []str("Params")) + spl = split.GroupBy(runix, go.Slice_string(["Params"])) split.Desc(spl, "FirstZero") split.Desc(spl, "PctCor") ss.RunStats = spl.AggsToTable(etable.AddAggName) @@ -1127,7 +1127,7 @@ class Sim(pygiv.ClassViewObj): dt.SetMetaData("name", "RunLog") dt.SetMetaData("desc", "Record of performance at end of training") dt.SetMetaData("read-only", "true") - dt.SetMetaData("precision", str(LogPrec.LogPrec)) + dt.SetMetaData("precision", str(LogPrec)) dt.SetFromSchema(etable.Schema( ("Run", etensor.INT64, go.nil, go.nil), @@ -1311,7 +1311,7 @@ class Sim(pygiv.ClassViewObj): # main menu appnm = gi.AppName() mmen = win.MainMenu - mmen.ConfigMenus([]str(appnm, "File", "Edit", "Window")) + mmen.ConfigMenus(go.Slice_string([appnm, "File", "Edit", "Window"])) amen = *gi.Action(win.MainMenu.ChildByName(appnm, 0)) amen.Menu.AddAppMenu(win) diff --git a/version.go b/version.go new file mode 100644 index 0000000..a84d5df --- /dev/null +++ b/version.go @@ -0,0 +1,9 @@ +// WARNING: auto-generated by Makefile release target -- run 'make release' to update + +package main + +const ( + Version = "v0.4.1" + GitCommit = "cd0e95d" // the commit JUST BEFORE the release + VersionDate = "2021-09-25 19:54" // UTC +)