예제 #1
0
        m.stmt('# print("@", id(r), repr(r))')
        m.stmt("mem[id(r)] = node")
        m.stmt("return r")

    seen = set()
    for cls in Constructor.mro():
        for name, attr in cls.__dict__.items():
            if name in seen:
                continue
            seen.add(name)
            if name.startswith("construct_") and callable(attr):
                sigs = inspect.signature(attr)
                m.stmt("def {}{}:", name, sigs)
                with m.scope():
                    args = []
                    for v in sigs.parameters.values():
                        if v.name == "self":
                            continue
                        if v.default is inspect._empty:
                            args.append(str(v))
                        else:
                            args.append(f"{v.name}={v.name}")
                    nodeval = "node" if "node" in sigs.parameters else "None"
                    m.stmt("path.append(node)")
                    m.stmt(f"r = super().{name}({', '.join(args)})")
                    m.stmt(f"self.wrap(path, {name!r}, {nodeval}, r)")
                    m.stmt("path.pop()")
                    m.return_("r")
                m.sep()
print(m)
예제 #2
0
from prestring.python import Module

m = Module()
m.import_("math")
m.sep()

with m.def_('rmse', 'xs', 'ys'):
    m.stmt('acc = 0')
    m.stmt('assert len(xs) == len(ys)')
    with m.for_('x, y', 'zip(xs, ys)'):
        m.stmt('acc += (x - y) ** 2')
    m.return_('math.sqrt(acc / len(xs))')

# m.stmt('xs = [92, 95, 110, 114, 100, 98, 93]')
# m.stmt('ys = [95, 93, 100, 114, 105, 100, 96]')
print(m)
예제 #3
0
from prestring.python import Module

m = Module()
with m.def_("foo"):
    m.return_("'foo'")
print(m)
예제 #4
0
def emit_foo(m: Module, name: str, *, sep: str):
    with m.def_(name, "message: str"):
        m.return_(f"f'foo{sep}{{message}}'")
    return m