Beispiel #1
0
def load_config(m: Module, filename: Symbol) -> Symbol:
    LoadConfig = Symbol("LoadConfig")  # todo: import
    config = Symbol("config")
    err = Symbol("err")

    m.stmt("{}, {} := {}", config, err, LoadConfig(filename))
    with m.if_(LazyFormat("{} != nil", err)):
        m.return_(err)
    return config
Beispiel #2
0
import typing as t
from appkit import Config, Error, Cleanup, gen, Symbol
from prestring.go import Module


class Foo:
    __gomodule__ = "github.com/podhmo/appkit/foo"


def FromConfig(c: Config) -> Foo:
    pass


c = Symbol("c", Config)
m = Module()

with m.func("run", return_="error"):
    m.stmt("filename := *config")
    m.stmt("c, err := conf.LoadConfig(filename)")
    with m.if_("err != nil"):
        m.return_("err")
    m = gen(m, c, FromConfig)
    m.stmt("fooOb := foo.FromConfig(c)")
    m.return_("use(fooOb)")
print(m)
Beispiel #3
0
from prestring.go import Module
m = Module()
m.package('main')

with m.import_group() as im:
    im.import_("fmt")

with m.func('Fizzbuzz', 'v int', return_='(string, error)'):
    with m.if_("v == 1"):
        m.return_('"1", nil')
    for i in range(2, 101):
        with m.elif_("v == {}".format(i)):
            if i % 15 == 0:
                m.return_('"fizzbuzz", nil')
            elif i % 3 == 0:
                m.return_('"fizz", nil')
            elif i % 5 == 0:
                m.return_('"buzz", nil')
            else:
                m.return_('"{}", nil'.format(i))
    with m.else_():
        m.return_('"", fmt.Errorf("unsupported value: %q", v)')

print(m)
Beispiel #4
0
from prestring.go import Module
m = Module()
m.package('main')

with m.import_group() as im:
    im.import_("fmt")

with m.func('main'):
    with m.for_("i := 1; i <= 100; i++"):
        m.stmt("v, err := FizzBuzz(i)")
        with m.if_("err != nil"):
            m.stmt("panic(err)")
        m.stmt("fmt.Println(v)")


with m.func('FizzBuzz', 'v int', return_='(string, error)'):
    with m.if_("v == 1"):
        m.return_('"1", nil')
    for i in range(2, 101):
        with m.elif_("v == {}".format(i)):
            if i % 15 == 0:
                m.return_('"fizzbuzz", nil')
            elif i % 3 == 0:
                m.return_('"fizz", nil')
            elif i % 5 == 0:
                m.return_('"buzz", nil')
            else:
                m.return_('"{}", nil'.format(i))
    with m.else_():
        m.return_('"", fmt.Errorf("unsupported value: %q", v)')