a guest May 22nd, 2019 45 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- :func:`include` works like Julia :func:`include` for code splitting.
- When a Python code is too large to maintain, it could be split into files,
- as well as done in Julia code.
- :func:`include` works as Julia built-in :func:`include` to merge code
- with given relative path. It is done with Python built-in :func:`exec`
- and :mod-attr:`__file__`
- def include(path):
- from os.path import normpath, join, dirname
- my_filepath = globals().get('__file__')
- ex_filepath = normpath(join(dirname(globals().get('__file__')), path))
- code = open(ex_filepath).read()
- globals()['__file__'] = ex_filepath
- exec(code, globals())
- if my_filepath is None:
- del globals()['__file__']
- globals()['__file__'] = my_filepath
RAW Paste Data