Hi Eric, I played around with some similar ideas a while back - I just blew the dust off it and published it here: https://github.com/pwaller/pp2g
My approach is a bit different, and works by printing the Python AST in Go. There is an overlap in our philosophy of 'produce standalone code' and 'help a human do the translation into idiomatic Go'. However, mine was a Saturday morning project which was never used in anger, so it is naturally quite incomplete. Regards, - Peter On Sun, 30 Sep 2018 at 11:48, Eric Raymond <e...@thyrsus.com> wrote: > I have mentioned before on this list that I am engaged in source > translation of a largeish Python prgram - 14KLOC - to Go. I found the > automated AST-based translation tools already available unsatisfactory; > they don't produce a quality of Go code I would consider maintainable. So > I wrote my own using crude, old-school regexp-bashing. > > This turned out to be surprisingly effective. The following is from the > manual page: > > * Opens Python block scopes with { and closes them with }. > > * Changes docstrings to comments. > > * Changes comments from #-led to //-led > > * Translates Python boolean constants True, False to Go true, false > > * Translates Python "in range" to Go ":= range", You will need to fix > up the comma and range clauses by hand. > > * Maps Python single-quote literals to Go double-quote literals. > > * Translates Python logical connectives and or not to Go && || !. > > * Changes Python None to Go nil. > > * Changes "def " at the left margin to "func ". > > * Common cases of various Python string library methods - capitalize(), > count(), endswith(), find(), join(), lower(), lstrip(), rfind(), replace(), > rstrip(), split(), startswith(), split(), upper() - are translated to Go > string library calls. > > * Some common Python standard library calls, notably from os and > os.filepath, are translated into Go equivalents. The result is not > guaranteed to be perfectly correct; a Python call that throws a signal on > failure may map into a Go function with an error return. But it will always > be a step in the right direction, and the Go compiler will respond with > indicative error messages. > > * The append() method of Python is translated to a call to Go's append > intrinsic. > > * Python's "del foo[bar]" is translated to Go "delete(foo, bar)". Note > that this can false-match on a list del with a numeric key; this > will throw a compilation error in Go and you can hand-fix it. > > * Trailing line-continuation backslashes are removed. > > * Python r-prefix string literals become Go backtick literals > > * Python try/finally/catch become pseudo-statements with delimited block > scopes. You will need to fix these up by hand. > > * Single-line % expressions with a string-literal left hand and either > tuple or single-variable right hands are translated into fmt.Sprintf > calls. You will need to translate %-cookies by hand; %r -> %v or %q is the > usual trouble spot. > > * Changes multiline strings to backtick literals. You'll need to fix up > backslash escapes and %-cookies yourself. > > * Many, but not necessarily all, cases where Go ":=" assignment can > replace a plain "=" assignment in Python are translated. > > This doesn't do a complete job, of course. But it takes a lot of the pain > out of hand-translation, and it does preserve comments and structure. > Probably the automated block scope closing is the biggest single win; > that's a tedious, fiddly job by hand and mistakes are all too easy. > > Future releases will translate a larger subset of Python standard library > calls. > > The repository is at https://gitlab.com/esr/pytogo and the project home > page at http://www.catb.org/esr/pytogo/ > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.