SHARE
TWEET

Untitled

a guest May 22nd, 2019 43 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top