Skip to content

Instantly share code, notes, and snippets.

@macrat
Last active July 24, 2016 11:12
Show Gist options
  • Save macrat/584fb53a067ab23d75f16e8949a863c8 to your computer and use it in GitHub Desktop.
Save macrat/584fb53a067ab23d75f16e8949a863c8 to your computer and use it in GitHub Desktop.
あらゆる文字列が"hello!"で置き換えられるpython処理系的なもの。
import ast
import sys
if len(sys.argv) != 2:
print('usage: python {} [FILE.py]'.format(sys.argv[0]))
sys.exit(1)
with open(sys.argv[1]) as f:
a = ast.parse(f.read(), f.name)
for x in ast.walk(a):
if isinstance(x, ast.Str):
x.s = 'hello!'
eval(compile(a, f.name, 'exec'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment