Created
September 7, 2019 03:43
-
-
Save ktbarrett/f9b7eb09bdbcb8c97a998c4e25a8732b to your computer and use it in GitHub Desktop.
Emulate Lua's dofile
This file contains hidden or 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
import itertools | |
def load_pyconf_file(filename, env=None): | |
f = '\t'.join(itertools.chain(("def __func():\n",), open(filename).readlines())) | |
if env: | |
exec(f, env) | |
return env['__func']() | |
else: | |
exec(f) | |
return __func() | |
if __name__ == "__main__": | |
print(load_pyconf_file("test.pyconf", env={'config': {'N': 1000}})) |
This file contains hidden or 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 random import randint | |
data = [randint(0, 1e4) for i in range(config['N'])] | |
return { | |
'data': data, | |
'max': max(data)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment