Returns whether a file can be loaded into Sage. This checks only whether its name ends in one of the supported extensions .py, .pyx, .sage, .spyx, and .m. Note: load() assumes the latter signifies a Magma file.
INPUT:
OUTPUT:
EXAMPLES:
sage: sage.repl.load.is_loadable_filename('foo.bar')
False
sage: sage.repl.load.is_loadable_filename('foo.c')
False
sage: sage.repl.load.is_loadable_filename('foo.sage')
True
sage: sage.repl.load.is_loadable_filename('foo.m')
True
Executes a file in the scope given by globals. The filename itself is also evaluated in the scope. If the name starts with http://, it is treated as a URL and downloaded.
Note
For Cython files, the situation is more complicated – the module is first compiled to a temporary module t and executed via:
from t import *
INPUT:
EXAMPLES:
Note that .py files are not preparsed:
sage: t = tmp_filename(ext='.py')
sage: open(t,'w').write("print 'hi', 2/3; z = -2/7")
sage: z = 1
sage: sage.repl.load.load(t, globals())
hi 0
sage: z
-1
A .sage file is preparsed:
sage: t = tmp_filename(ext='.sage')
sage: open(t,'w').write("print 'hi', 2/3; z = -2/7")
sage: z = 1
sage: sage.repl.load.load(t, globals())
hi 2/3
sage: z
-2/7
Cython files are not preparsed:
sage: t = tmp_filename(ext='.pyx')
sage: open(t,'w').write("print 'hi', 2/3; z = -2/7")
sage: z = 1
sage: sage.repl.load.load(t, globals())
Compiling ...
hi 0
sage: z
-1
If the file isn’t a Cython, Python, or a Sage file, a ValueError is raised:
sage: sage.repl.load.load('a.foo',globals())
Traceback (most recent call last):
...
ValueError: argument (='a.foo') to load or attach must have extension py, pyx, sage, spyx, or m
A filename given as an expression get evaluated. This ensures that load DATA+'foo.sage' works in the Notebook, say:
sage: t=tmp_filename(ext='.py'); open(t,'w').write("print 'hello world'")
sage: sage.repl.load.load(t, globals())
hello world
We load a file given at a remote URL:
sage: sage.repl.load.load('http://wstein.org/loadtest.py', globals()) # optional - internet
hi from the net
5
We can load files using secure http (https):
sage: sage.repl.load.load('https://github.com/jasongrout/minimum_rank/raw/minimum_rank_1_0_0/minrank.py', globals()) # optional - internet
We attach a file:
sage: t = tmp_filename(ext='.py')
sage: open(t,'w').write("print 'hello world'")
sage: sage.repl.load.load(t, globals(), attach=True)
hello world
sage: t in attached_files()
True
You can’t attach remote URLs (yet):
sage: sage.repl.load.load('http://wstein.org/loadtest.py', globals(), attach=True) # optional - internet
Traceback (most recent call last):
...
NotImplementedError: you can't attach a URL
The default search path for loading and attaching files is the current working directory, i.e., '.'. But you can modify the path with load_attach_path():
sage: sage.repl.attach.reset(); reset_load_attach_path()
sage: load_attach_path()
['.']
sage: t_dir = tmp_dir()
sage: fullpath = os.path.join(t_dir, 'test.py')
sage: open(fullpath, 'w').write("print 37 * 3")
sage: load_attach_path(t_dir)
sage: attach('test.py')
111
sage: sage.repl.attach.reset(); reset_load_attach_path() # clean up
or by setting the environment variable SAGE_LOAD_ATTACH_PATH to a colon-separated list before starting Sage:
$ export SAGE_LOAD_ATTACH_PATH="/path/to/my/library:/path/to/utils"
$ sage
sage: load_attach_path() # not tested
['.', '/path/to/my/library', '/path/to/utils']
Make sure that load handles filenames with spaces in the name or path:
sage: t = tmp_filename(ext=' b.sage'); open(t,'w').write("print 2")
sage: sage.repl.load.load(t, globals())
2
Helper function to load a Cython file.
INPUT:
OUTPUT:
Encodes a load or attach command as valid Python code.
INPUT:
OUTPUT:
EXAMPLES:
sage: sage.repl.load.load_wrap('foo.py', True)
'sage.repl.load.load(sage.repl.load.base64.b64decode("Zm9vLnB5"),globals(),True)'
sage: sage.repl.load.load_wrap('foo.sage')
'sage.repl.load.load(sage.repl.load.base64.b64decode("Zm9vLnNhZ2U="),globals(),False)'
sage: sage.repl.load.base64.b64decode("Zm9vLnNhZ2U=")
'foo.sage'