예제 #1
0
    def parse_args(self, *args, **kwargs):
        self.history.append({
            "name": "parse_args",
            "args": args,
            "kwargs": kwargs
        })
        from prestring.python import Module, LazyArgumentsAndKeywords

        def _make_args(history, default=""):
            name = history["name"]
            if name == "__init__":
                name = default
            kwargs = {k: repr(v) for k, v in history["kwargs"].items()}
            args = [repr(v) for v in history["args"]]
            return f"{name}({LazyArgumentsAndKeywords(args, kwargs)})"

        m = Module()
        with m.def_("Main"):
            m.import_("argparse")
            m.stmt(
                f"parser = argparse.ArgumentParser{_make_args(self.history[0])}"
            )
            for x in self.history[1:-1]:
                m.stmt(f"parser.{_make_args(x)}")
            m.stmt(f"args = parser.{_make_args(self.history[-1])}")
            m.stmt("main(**vars(args))")

        with m.if_("__name__ == '__main__'"):
            m.stmt("Main()")

        print(m)
        sys.exit(0)
예제 #2
0
    def parse_args(self, *args, **kwargs):
        self.history.append({"name": "parse_args", "args": args, "kwargs": kwargs})
        from prestring.python import Module, LazyArgumentsAndKeywords

        def _make_args(history, default=""):
            name = history["name"]
            if name == "__init__":
                name = default
            kwargs = {k: repr(v) for k, v in history["kwargs"].items()}
            args = [repr(v) for v in history["args"]]
            return f"{name}({LazyArgumentsAndKeywords(args, kwargs)})"

        m = Module()
        with m.def_("Main"):
            m.import_("argparse")
            m.stmt(f"parser = argparse.ArgumentParser{_make_args(self.history[0])}")
            for x in self.history[1:-1]:
                m.stmt(f"parser.{_make_args(x)}")
            m.stmt(f"args = parser.{_make_args(self.history[-1])}")
            m.stmt("main(**vars(args))")

        with m.if_("__name__ == '__main__'"):
            m.stmt("Main()")

        print(m)
        sys.exit(0)
예제 #3
0
def gen() -> Module:
    m = Module()
    with m.def_("foo", "x", "y", return_type="int"):
        m.stmt("pass")

    with m.def_("bar", "x", return_type="int"):
        m.stmt("pass")
    return m
예제 #4
0
def emit(ctx: Context):
    from prestring.python import Module

    m = Module()
    for name, cls in ctx.types.items():
        with m.class_(name):
            # TODO: omit class inheritance
            for field_name, field_type in t.get_type_hints(cls).items():
                # TODO: to pytype
                m.stmt(f"{field_name}: {field_type.__name__}")
    return m
예제 #5
0
def transform(node, *, m=None, is_whole=None):
    is_whole = is_whole or m is None
    if m is None:
        m = Module()
        m.g = m.submodule()

    if is_whole:
        m.g.from_("prestring.python", "Module")
        m.g.stmt("m = Module()  # noqa")

    t = Transformer(node, m=m)
    t.visit(node)

    if len(m.g.imported_set) > 0:
        m.g.stmt("m.sep()")
        m.g.sep()

    if is_whole:
        m.stmt("print(m)")
    return m
예제 #6
0
    path = path.parent
d = loading.loadfile(path / ("data/sqs/2012-11-05/service-2.json"))
"""
operations:
  <name>:
    name: <>
    input: {"shapee": <>}
    output: {"shape": <>,  "resultWrapper": <>}
    errors: {}
    documentation
"""

m = Module()
m.from_("__future__").import_("annotations")
m.sep()
m.stmt("# operations")
with m.class_("SQS"):
    for name, sd in d["operations"].items():
        with m.def_(
                name,
                f"input: {sd['input']['shape']}",
                return_type=sd["output"]["shape"] if "output" in sd else None,
        ):
            m.stmt("...")
m.stmt("# shapes")
with m.class_("SQS"):
    for name, sd in d["shapes"].items():
        with m.class_(name):
            # structure, type
            m.stmt("pass")
예제 #7
0
__mod__
__rmod__
__pos__
__neg__
__call__

__getitem__
__lt__
__le__
__gt__
__ge__
__int__

__float__
__complex__
__pow__
__rpow__
__sub__
__rsub__
"""

from prestring.python import Module
m = Module()
with m.scope():
    for x in xs.strip().split("\n"):
        if not x.strip():
            continue
        with m.def_(x, "self", "*args", "**kwargs"):
            m.stmt(f'return self.__getattr__({x!r})(*args, **kwargs)')
print(m)
예제 #8
0
    **metadata: t.Optional[t.Any],
) -> None:
    pass


m = Module()
m.toplevel = m.submodule(import_unique=True)
m.sep()

spec = fnspec(f)
with m.class_("F"):
    for name, typ, kind in spec.parameters:
        if typ.__module__ != "builtins":
            m.toplevel.import_(typ.__module__)

        info = typeinfo(typ)
        rhs = spec.type_str_of(info.normalized)
        if info.is_optional:
            rhs = LazyFormat("typing.Optional[{}]", rhs)

        if kind == "var_kw":
            rhs = LazyFormat("typing.Dict[str, {}]", rhs)
        elif kind == "var_args":
            rhs = LazyFormat("typing.List[{}]", rhs)
        elif kind == "kw_defaults" or kind == "args_defaults":
            rhs = LazyFormat("{} = {}", rhs, spec.default_of(name))

        m.stmt("{}: {}", name, rhs)

print(m)
예제 #9
0
def Person(m: Module) -> None:
    with m.class_("Person"):
        m.stmt("name: str")
예제 #10
0
def f(name: str, *vals: int) -> None:
    pass


m = Module()
m.toplevel = m.submodule(import_unique=True)
m.sep()

spec = fnspec(f)
with m.class_("F"):
    for name, typ, kind in spec.parameters:
        if typ.__module__ != "builtins":
            m.toplevel.import_(typ.__module__)

        info = typeinfo(typ)
        type_str = spec.type_str_of(info.normalized)
        if info.is_optional:
            type_str = LazyFormat("typing.Optional[{}]", type_str)
        elif kind == "var_kw":
            type_str = LazyFormat("typing.Dict[str, {}]", type_str)
        elif kind == "var_args":
            type_str = LazyFormat("typing.List[{}]", type_str)
        m.stmt("{}: {}", name, type_str)

        if kind == "kw_defaults" or kind == "args_defaults":
            m.unnewline()
            m.stmt(" = {}", spec.default_of(name))

print(m)
예제 #11
0
from prestring.python import Module
from prestring.python._codeobject import CodeobjectModule

m = Module()
co = CodeobjectModule(m)
re = co.import_("re")
sys = co.import_("sys")

m.sep()
pattern = co.let(
    "pattern",
    re.compile(
        r"^(?P<label>DEBUG|INFO|WARNING|ERROR|CRITICAL):\s*(?P<message>\S+)",
        re.IGNORECASE,
    ),
)

with m.for_("line", sys.stdin):
    matched = co.let("matched", pattern.search(co.symbol("line")))
    with m.if_(f"{matched} is not None"):
        m.stmt("print(matched.groupdict())")
print(m)
예제 #12
0
from prestring.python import Module
from prestring.codeobject import CodeObjectModule

m = Module()
co = CodeObjectModule(m)

re = co.import_("re")
sys = co.import_("sys")

m.sep()
pattern = co.let(
    "pattern",
    re.compile(
        r"^(?P<label>DEBUG|INFO|WARNING|ERROR|CRITICAL):\s*(?P<message>\S+)",
        re.IGNORECASE,
    ),
)

with m.for_("line", sys.stdin):
    matched = co.let("matched", pattern.search(co.symbol("line")))
    with m.if_(f"{matched} is not None"):
        print_ = co.symbol("print")
        m.stmt(print_(matched.groupdict()))
print(m)
예제 #13
0
from prestring.python import Module

m = Module()

m.import_("re")
m.import_("sys")
m.sep()
m.stmt(
    "pattern = re.compile({!r}, re.IGNORECASE)",
    r"^(?P<label>DEBUG|INFO|WARNING|ERROR|CRITICAL):\s*(?P<message>\S+)",
)

with m.for_("line", "sys.stdin"):
    m.stmt("m = pattern.search(line)")
    with m.if_("m is not None"):
        m.stmt("print(m.groupdict())")
print(m)
예제 #14
0
def code(name):
    m = Module()
    m.stmt("{} = 1", name)
예제 #15
0
from prestring.python import Module
import matplotlib.cm as cm

m = Module()  # noqa
m.from_('nbreversible', 'code')
m.import_('pandas as pd')
m.import_('numpy as np')
m.import_('seaborn as sns')
m.sep()

m.stmt('"# [jupyter][matplotlib][python] color mapの一覧をheatmapで"')
m.stmt('# %matplotlib inline')
with m.with_('code()'):
    m.stmt('xs = np.arange(1, 10)')
    m.stmt('ys = np.arange(1, 10).reshape(9, 1)')
    m.stmt('m = xs * ys')
    m.stmt('df = pd.DataFrame(m)')
    m.stmt('df')

for name in cm.cmap_d.keys():
    m.stmt(f'"## {name}"')
    m.sep()
    with m.with_("code()"):
        m.stmt(f"sns.heatmap(df, {name!r})")
    m.sep()

print(m)
예제 #16
0
from prestring.python import Module
from dictknife import DictWalker
from dictknife import loading

w = DictWalker(["lines"])
d = loading.loadfile(format="json")

r = []
for _, d in w.walk(d):
    if d["language"] == "python" or d["language"] == "py":
        r.append(d["lines"])

m = Module()
m.from_("nbreversible", "code")
for lines in r:
    with m.with_("code()"):
        for line in lines:
            if line.startswith("%"):
                m.stmt("#{}", line)
            else:
                m.stmt(line.rstrip())
    m.sep()
print(m)
예제 #17
0
from prestring.python import Module, Symbol

m = Module()
retry_ = Symbol("retry")

m.stmt("@{}", retry_)
with m.def_("foo"):
    m.stmt("send(10)")
print(m)
예제 #18
0
def print_name(m: Module):
    with m.def_("print_name", "p: Person", return_type="None"):
        m.stmt("print(p.name)")
예제 #19
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)
예제 #20
0
from prestring.python import Module

m = Module()
dataclasses = m.import_("dataclasses")

m.stmt("@{}", dataclasses.dataclass)
with m.class_("Person") as Person:
    m.stmt("name: str")
    m.stmt("age: int")

m.stmt(Person(name="foo", age=20))

print(m)

예제 #21
0
from prestring.python import Module

m = Module()
with m.def_("hello"):
    m.stmt("""print("hello")""")
print(m)
예제 #22
0
from prestring.python import Module
from prestring.utils import LParams

m = Module()

with m.class_("A0"):
    params0 = LParams()
    with m.method("f0", params0):
        m.stmt("pass")

with m.class_("A1"):
    params1 = LParams()
    with m.method("f1", params1):
        m.stmt("pass")

params1.append_tail("*args")
print(m)

# class A0:
#     def f0(self): <- this (not f0(self,))
#         pass

# class A1:
#     def f1(self, *args):
#         pass
예제 #23
0
from prestring.python import Module
from prestring.utils import LazyFormat, LazyArgumentsAndKeywords
from typestr import typestr


class Hello:
    name: str
    age: int
    nickname: t.Optional[str] = None


m = Module()
w = get_walker([Hello])
for cls in w.walk():
    name = w.resolver.resolve_typename(cls)
    args = []
    for fieldname, info, metadata in w.for_type(cls).walk():
        # todo: default
        if "default" in metadata:
            args.append(
                LazyFormat(
                    "{}: {} = {}", fieldname, typestr(info.raw), metadata["default"]
                )
            )
        else:
            args.append(LazyFormat("{}: {}", fieldname, typestr(info.raw)))

    with m.def_(name, LazyArgumentsAndKeywords(args)):
        m.stmt("...")
print(m)
예제 #24
0
def run(*, path: str, disable_docstring) -> None:
    d = loading.loadfile(path)
    m = Module()
    a = Accessor(d, m, disable_docstring=disable_docstring)

    m.import_("typing", as_="t")
    m.sep()
    m.stmt("AnyService = t.Any  # TODO")
    m.stmt("AnyResource = t.Any  # TODO")
    m.sep()
    for rname, resource in a.resources.items():
        with m.class_(titleize(rname), ""):

            with m.method("__init__", "resource: AnyResource"):
                m.stmt("self.internal = resource")

            m.stmt("# methods")
            for mname, method, params in a.iterate_methods(resource):
                with m.method(mname, params):
                    a.emit_docstring(method["description"])
                    m.stmt(
                        f"""# {method["httpMethod"]}: {method["flatPath"]}""")
                    m.stmt(f"""# id: {method["id"]}""")
                    m.stmt(f"return self.internal.{mname}({params})")

            m.stmt("# nested resources")
            for srname, subresource in a.iterate_nested_resources(resource):
                with m.method(srname):
                    m.stmt(f"return self.internal.{srname}({params})")

            # m.stmt("# nested resources")
            # for mname, subresource in resource.get("resources", {}).items():
            #     params = LParams()
            #     for is_positional, (pname, parameter) in itertools.zip_longest(subresource.get("parameterOrder", []), subresource.get("parameters", {}).items()):
            #         if is_positional:
            #             params.append(pname)  # TODO type:
            #         else:
            #             params[pname] = None  # TODO type:
            #     with m.method(mname, params):
            #         docstring(subresource["description"])
            #         m.stmt(f"""# id: {subresource["id"]}""")
            #         m.stmt(f"return self.{mname}({params})")

    with m.class_("Service"):
        with m.method("__init__", "service: AnyService"):
            m.stmt("self.internal = service")

        for rname in a.resources.keys():
            with m.method(rname, return_type=titleize(rname)):
                m.stmt(f"return {titleize(rname)}(self.internal.{rname}())")

    with m.def_("build", "*args", "**kwargs", return_type="Service"):
        m.stmt("# TODO: use the signature of googleapiclient.discovery.build")
        m.submodule().from_("googleapiclient.discovery", "build")
        m.stmt(
            f"return Service(build({a.name!r}, {a.version!r}, *args, **kwargs))"
        )

    print(m)
예제 #25
0
def Person(m: Module, name: str) -> Module:
    # todo: import
    with m.class_(name, "BaseModel"):
        m.stmt("name: str")
        m.stmt("age : int = 0")
    return m
from prestring.python import Module
from prestring.utils import LParams

m = Module()

with m.class_("A0"):
    params0 = LParams()
    with m.method("f0", params0):
        m.stmt("pass")

with m.class_("A1"):
    params1 = LParams()
    with m.method("f1", params1):
        m.stmt("pass")

params1.append_tail("*args")
print(m)

# class A0:
#     def f0(self): <- this (not f0(self,))
#         pass



# class A1:
#     def f1(self, *args):
#         pass

예제 #27
0
from prestring.python import Module

m = Module()

with m.def_("main"):
    m.stmt("print('hello world')")

with m.if_("__name__ == '__main__'"):
    m.stmt("main()")

print(m)
예제 #28
0
from prestring.python import Module
m = Module()  # noqa
m.sep()


with m.def_('hello', 'name', '*', 'message: str =  "hello world"'):
    m.docstring('greeting message')
    m.stmt('print(f"{name}: {message}")')


print(m)
예제 #29
0
import typing as t
from prestring.python import Module
from prestring.naming import titleize
from monogusa.web.codegen._fnspec import fnspec


def hello(name: str,
          *,
          age: int,
          nickname: t.Optional[str] = None) -> t.Dict[str, t.Any]:
    pass


m = Module()
fn = hello
spec = fnspec(fn)
with m.class_(titleize(fn.__name__)):
    if len(spec.keyword_arguments) == 0:
        m.stmt("pass")
    for name, typ, kind in spec.parameters:
        if kind.endswith("defaults"):
            m.stmt("{}: {} = {}", name, spec.type_str_of(typ),
                   spec.default_str_of(name))
        else:
            m.stmt("{}: {}", name, spec.type_str_of(typ))
print(m)
예제 #30
0
from prestring.python import Module

# 何が嫌だったのかを整理してみるか。

m = Module()

# こういう感じでクラス定義をするんだけれど。このクラスを利用する関数を書きづらい。
with m.class_("Person"):
    m.stmt("name: str")

# ここで "Person" って何? importされているの?と感じてしまう。
with m.def_("print_name", "p: Person"):
    m.stmt("print(p.name)")

# そしてここで定義したprint_name()も利用できない。何がおきているかと言うと。
#
# - "Person"というクラス定義を生成する記述は値ではないので持ち運べない
# - "print_name"という関数定義を生成する記述は値ではないので持ち運べない
#
# どれも値として扱えないことが問題? 例えば以下の様に関数で包むのはどうだろう。


def Person(m: Module) -> None:
    with m.class_("Person"):
        m.stmt("name: str")


# 何が嫌なのかと言えば、関数名とクラス名を二度書かないといけないと感じる点。
# ただ今度は値として持つことができる。
# 次にやりたいことはなんだろう?その値を使ってのコードとはなんのことだろう?
#
예제 #31
0
import inspect
from yaml.constructor import Constructor
from prestring.python import Module

m = Module()
m.from_("yaml.constructor", "Constructor")
m.sep()
with m.class_("WrappedConstructor", "Constructor"):
    with m.def_("wrap", "self", "path", "name", "node", "r"):
        with m.if_("r is None"):
            m.stmt("return r")
        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))
예제 #32
0
        return self.__class__(name=self.name,
                              emit=self._emit,
                              args=args,
                              kwargs=kwargs)

    def emit(self, *, m: t.Optional[Module] = None) -> Module:
        m = m or Module()
        return self._emit(m, name=self.name)


def codeobject(emit: t.Callable[..., Module]) -> CodeObject:
    name = emit.__name__
    return CodeObject(name, emit=emit)


@codeobject
def Person(m: Module, name: str) -> Module:
    # todo: import
    with m.class_(name, "BaseModel"):
        m.stmt("name: str")
        m.stmt("age : int = 0")
    return m


m = Module()
Person.emit(m=m)
m.stmt("{} # is class? {}", Person, Person.is_class)
m.stmt("p = {} # is class? {}", Person("foo", age=20),
       Person("foo", age=20).is_class)
print(m)
예제 #33
0
    def parse_args(self, *args, **kwargs):
        self.history.append({"name": "parse_args", "args": args, "kwargs": kwargs})
        from prestring.python import Module, LazyArgumentsAndKeywords

        def _make_call_stmt(history, default=""):
            name = history["name"]
            if name == "__init__":
                name = default
            kwargs = {k: repr(v) for k, v in history["kwargs"].items()}
            args = [repr(v) for v in history["args"]]
            return f"{name}({LazyArgumentsAndKeywords(args, kwargs)})"

        m = Module()
        m.sep()
        with m.def_("main"):
            m.import_("argparse")
            m.stmt(f"parser = argparse.ArgumentParser{_make_call_stmt(self.history[0])}")
            m.stmt("parser.print_usage = parser.print_help")
            for x in self.history[1:-1]:
                m.stmt(f"parser.{_make_call_stmt(x)}")
            m.stmt(f"args = parser.{_make_call_stmt(self.history[-1])}")
            m.stmt(f"{self.fn.__name__}(**vars(args))")

        with m.if_("__name__ == '__main__'"):
            m.stmt("main()")

        with open(inspect.getsourcefile(self.fn)) as rf:
            source = rf.read()
        rx = re.compile("(?:@([\S]+\.)?as_command.*|^.*import ascommand.*)\n", re.MULTILINE)
        exposed = rx.sub("", "".join(source))
        print(exposed)
        print(m)
        sys.exit(0)
예제 #34
0
from prestring.python import Module


def emit_foo(m: Module, name: str, *, sep: str):
    with m.def_(name, "message: str"):
        m.return_(f"f'foo{sep}{{message}}'")
    return m


m = Module()
m = emit_foo(m, "do_foo", sep=":")

with m.for_("i", "range(5)"):
    m.stmt("do_foo(str(i))")

print(m)