Created
February 23, 2020 05:11
-
-
Save podhmo/c3230844ee522562220515a29e408005 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from prestring.python import Module | |
m = Module() | |
with m.def_("foo"): | |
m.return_("'foo'") | |
print(m) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from prestring.python import Module | |
m = Module() | |
re = m.import_("re") | |
sys = m.import_("sys") | |
m.sep() | |
m.stmt( | |
"pattern = {}", | |
re.compile( | |
r"^(?P<label>DEBUG|INFO|WARNING|ERROR|CRITICAL):\s*(?P<message>\S+)", | |
re.IGNORECASE, | |
), | |
) | |
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- 03use-import.py 2020-02-23 13:37:24.000000000 +0900 | |
+++ 04use-import-symbol.py 2020-02-23 13:36:11.000000000 +0900 | |
@@ -2,15 +2,19 @@ | |
m = Module() | |
-m.import_("re") | |
-m.import_("sys") | |
+re = m.import_("re") | |
+sys = 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+)", | |
+ "pattern = {}", | |
+ re.compile( | |
+ r"^(?P<label>DEBUG|INFO|WARNING|ERROR|CRITICAL):\s*(?P<message>\S+)", | |
+ re.IGNORECASE, | |
+ ), | |
) | |
-with m.for_("line", "sys.stdin"): | |
+with m.for_("line", sys.stdin): | |
m.stmt("m = pattern.search(line)") | |
with m.if_("m is not None"): | |
m.stmt("print(m.groupdict())") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- 04use-import-symbol.py 2020-02-23 13:36:11.000000000 +0900 | |
+++ 06use-import-code-object.py 2020-02-23 13:49:22.000000000 +0900 | |
@@ -1,21 +1,25 @@ | |
from prestring.python import Module | |
+from prestring.codeobject import CodeObjectModule | |
m = Module() | |
+co = CodeObjectModule(m) | |
-re = m.import_("re") | |
-sys = m.import_("sys") | |
+re = co.import_("re") | |
+sys = co.import_("sys") | |
m.sep() | |
-m.stmt( | |
- "pattern = {}", | |
+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): | |
- m.stmt("m = pattern.search(line)") | |
- with m.if_("m is not None"): | |
- m.stmt("print(m.groupdict())") | |
+ 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
00: | |
python $(shell echo $@*.py) | |
01: | |
python $(shell echo $@*.py) | |
02: | |
python $(shell echo $@*.py) | |
03: | |
python $(shell echo $@*.py) | |
04: | |
python $(shell echo $@*.py) | |
05: | |
diff -u 03*.py 04*.py > 05-0304.diff | |
06: | |
python $(shell echo $@*.py) | |
07: | |
diff -u 04*.py 06*.py > 07-0406.diff |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment