Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. """
  2. :func:`include` works like Julia :func:`include` for code splitting.
  3.  
  4. When a Python code is too large to maintain, it could be split into files,
  5. as well as done in Julia code.
  6.  
  7. :func:`include` works as Julia built-in :func:`include` to merge code
  8. with given relative path. It is done with Python built-in :func:`exec`
  9. and :mod-attr:`__file__`
  10. """
  11.  
  12. def include(path):
  13. from os.path import normpath, join, dirname
  14.  
  15. my_filepath = globals().get('__file__')
  16. ex_filepath = normpath(join(dirname(globals().get('__file__')), path))
  17. code = open(ex_filepath).read()
  18.  
  19. globals()['__file__'] = ex_filepath
  20. exec(code, globals())
  21. if my_filepath is None:
  22. del globals()['__file__']
  23. else:
  24. globals()['__file__'] = my_filepath
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement