Loading and saving sessions and listing all variables - Utilities

Skip to content
Back to top
View this page
Edit this page

# Loading and saving sessions and listing all variables

EXAMPLES:

We reset the current session, then define a rational number `2/3`, and verify that it is listed as a newly defined variable:

Sage

    sage: reset()
    sage: w = 2/3; w
    2/3
    sage: show_identifiers()
    ['w']

Python

    >>> from sage.all import *
    >>> reset()
    >>> w = Integer(2)/Integer(3); w
    2/3
    >>> show_identifiers()
    ['w']

We next save this session. We are using a temporary directory to hold the session file but we do this for testing only. Please do not do this if you want to save your session permanently. Also note that the `tempfile` module weasels its way into the session:

    ::

> sage: from tempfile import TemporaryDirectory sage: d = TemporaryDirectory() sage: save_session(os.path.join(d.name, ‘session’))

This saves a dictionary with `w` as one of the keys:

Sage

    sage: z = load(os.path.join(d.name, 'session'))
    sage: list(z)
    ['w', 'd']
    sage: z['w']
    2/3

[Python]

    >>> from sage.all import *
    >>> z = load(os.path.join(d.name, 'session'))
    >>> list(z)
    ['w', 'd']
    >>> z['w']
    2/3

Next we reset all variables in the session except for the temporary directory name. We verify that the session is reset, and then load it back.:

Sage

    sage: sage.misc.reset.EXCLUDE.add('d')
    sage: reset()
    sage: show_identifiers()
    ['d']
    sage: load_session(os.path.join(d.name, 'session'))

Python

    >>> from sage.all import *
    >>> sage.misc.reset.EXCLUDE.add('d')
    >>> reset()
    >>> show_identifiers()
    ['d']
    >>> load_session(os.path.join(d.name, 'session'))

Indeed `w` is now defined again.:

Sage

    sage: show_identifiers()
    ['d', 'w']
    sage: w
    2/3

[Python]

    >>> from sage.all import *
    >>> show_identifiers()
    ['d', 'w']
    >>> w
    2/3

Finally, we clean up the temporary directory:

Sage

    sage: d.cleanup()

Python

    >>> from sage.all import *
    >>> d.cleanup()

AUTHOR:

- William Stein

sage.misc.session.init(state=None)

  Initialize some dictionaries needed by the `show_identifiers()`, `save_session()`, and `load_session()` functions.

  INPUT:

  - `state` – dictionary or `None`; if `None` the `locals()` of the caller is used

  EXAMPLES:

  Sage

      sage: reset()
      sage: w = 10
      sage: show_identifiers()
      ['w']

  Python

      >>> from sage.all import *
      >>> reset()
      >>> w = Integer(10)
      >>> show_identifiers()
      ['w']

  When we call `init()` below it reinitializes the internal table, so the `w` we just defined doesn’t count as a new identifier:

  Sage

      sage: sage.misc.session.init()
      sage: show_identifiers()
      []

  [Python]

      >>> from sage.all import *
      >>> sage.misc.session.init()
      >>> show_identifiers()
      []

sage.misc.session.load_session(name='sage_session', verbose=False)

  Load a saved session.

  This merges in all variables from a previously saved session. It does not clear out the variables in the current sessions, unless they are overwritten. You can thus merge multiple sessions, and don’t necessarily loose all your current work when you use this command.

  Note

  In the Sage notebook the session name is searched for both in the current working cell and the `DATA` directory.

  EXAMPLES:

  Sage

      sage: a = 5
      sage: f = lambda x: x^2

  Python

      >>> from sage.all import *
      >>> a = Integer(5)
      >>> f = lambda x: x**Integer(2)

  For testing, we use a temporary file, that will be removed as soon as Sage is left. Of course, for permanently saving your session, you should choose a permanent file.

  Sage

      sage: tmp_f = tmp_filename()
      sage: save_session(tmp_f)
      sage: del a; del f
      sage: load_session(tmp_f)
      sage: print(a)
      5

  [Python]

      >>> from sage.all import *
      >>> tmp_f = tmp_filename()
      >>> save_session(tmp_f)
      >>> del a; del f
      >>> load_session(tmp_f)
      >>> print(a)
      5

  Note that `f` does not come back, since it is a function, hence couldn’t be saved:

  Sage

      sage: print(f)
      Traceback (most recent call last):
      ...
      NameError: name 'f' is not defined

  Python

      >>> from sage.all import *
      >>> print(f)
      Traceback (most recent call last):
      ...
      NameError: name 'f' is not defined

sage.misc.session.save_session(name='sage_session', verbose=False)

  Save all variables that can be saved to the given filename. The variables will be saved to a dictionary, which can be loaded using `load(name)` or `load_session()`.

  Note

  1. Function and anything else that can’t be pickled is not saved. This failure is silent unless you set `verbose=True`.

  2. One can still make sessions that can’t be reloaded. E.g., define a class with:

         class Foo: pass

  > and make an instance with:

  >     f = Foo()

  > Then `save_session()` followed by `quit` and `load_session()` fails. I doubt there is any good way to deal with this. Fortunately, one can simply re-evaluate the code to define `Foo`, and suddenly `load_session()` works fine.

  INPUT:

  > - `name` – string (default: `'sage_session'`); name of `sobj` to save the session to

  > - `verbose` – boolean (default: `False`); if `True`, print info about why certain variables can’t be saved

  OUTPUT: creates a file and returns silently

  EXAMPLES:

  For testing, we use a temporary file that will be removed as soon as Sage is left. Of course, for permanently saving your session, you should choose a permanent file.

  Sage

      sage: a = 5
      sage: tmp_f = tmp_filename()
      sage: save_session(tmp_f)
      sage: del a
      sage: load_session(tmp_f)
      sage: print(a)
      5

  Python

      >>> from sage.all import *
      >>> a = Integer(5)
      >>> tmp_f = tmp_filename()
      >>> save_session(tmp_f)
      >>> del a
      >>> load_session(tmp_f)
      >>> print(a)
      5

  We illustrate what happens when one of the variables is a function:

  Sage

      sage: f = lambda x : x^2
      sage: save_session(tmp_f)
      sage: save_session(tmp_f, verbose=True)
      ...
      Not saving f: f is a function or method

  [Python]

      >>> from sage.all import *
      >>> f = lambda x : x**Integer(2)
      >>> save_session(tmp_f)
      >>> save_session(tmp_f, verbose=True)
      ...
      Not saving f: f is a function or method

  Something similar happens for cython-defined functions:

  Sage

      sage: g = cython_lambda('double x', 'x*x + 1.5')
      sage: save_session(tmp_f, verbose=True)
      ...
      Not saving g: g is a cython function or method

  Python

      >>> from sage.all import *
      >>> g = cython_lambda('double x', 'x*x + 1.5')
      >>> save_session(tmp_f, verbose=True)
      ...
      Not saving g: g is a cython function or method

  And the same for a lazy import:

  Sage

      sage: from sage.misc.lazy_import import LazyImport
      sage: lazy_ZZ = LazyImport('sage.rings.integer_ring', 'ZZ')
      sage: save_session(tmp_f, verbose=True)
      ...
      Not saving lazy_ZZ: lazy_ZZ is a lazy import

  [Python]

      >>> from sage.all import *
      >>> from sage.misc.lazy_import import LazyImport
      >>> lazy_ZZ = LazyImport('sage.rings.integer_ring', 'ZZ')
      >>> save_session(tmp_f, verbose=True)
      ...
      Not saving lazy_ZZ: lazy_ZZ is a lazy import

sage.misc.session.show_identifiers(hidden=False)

  Return a list of all variable names that have been defined during this session. By default, this returns only those identifiers that don’t start with an underscore.

  INPUT:

  - `hidden` – boolean (default: `False`); if `True`, also return identifiers that start with an underscore

  OUTPUT: list of variable names

  EXAMPLES:

  We reset the state of all variables, and see that none are defined:

  Sage

      sage: reset()
      sage: show_identifiers()
      []

  Python

      >>> from sage.all import *
      >>> reset()
      >>> show_identifiers()
      []

  We then define two variables, one which overwrites the default factor function; both are shown by `show_identifiers()`:

  Sage

      sage: a = 10
      sage: factor = 20
      sage: show_identifiers()
      ['factor', 'a']

  [Python]

      >>> from sage.all import *
      >>> a = Integer(10)
      >>> factor = Integer(20)
      >>> show_identifiers()
      ['factor', 'a']

  To get the actual value of a variable from the list, use the `globals()` function.:

  Sage

      sage: globals()['factor']
      20

  Python

      >>> from sage.all import *
      >>> globals()['factor']
      20

  By default `show_identifiers()` only returns variables that don’t start with an underscore. There is an option hidden that allows one to list those as well:

  Sage

      sage: _hello = 10
      sage: show_identifiers()
      ['factor', 'a']
      sage: '_hello' in show_identifiers(hidden=True)
      True

  [Python]

      >>> from sage.all import *
      >>> _hello = Integer(10)
      >>> show_identifiers()
      ['factor', 'a']
      >>> '_hello' in show_identifiers(hidden=True)
      True

  Many of the hidden variables are part of the IPython command history, at least in command line mode.:

  Sage

      sage: show_identifiers(hidden=True)        # random output
      ['__builtin__', '_ih', '_oh', '_dh', 'exit', 'quit', '_', '__', '___',
      '_i', '_ii', '_iii', '_i1', 'factor', '_i2', '_2', '_i3', 'a', '_i4',
      '_i5', '_5', '_i6', '_6', '_i7', '_hello', '_i8', '_8', '_i9', '_9',
      '_i10']

  Python

      >>> from sage.all import *
      >>> show_identifiers(hidden=True)        # random output
      ['__builtin__', '_ih', '_oh', '_dh', 'exit', 'quit', '_', '__', '___',
      '_i', '_ii', '_iii', '_i1', 'factor', '_i2', '_2', '_i3', 'a', '_i4',
      '_i5', '_5', '_i6', '_6', '_i7', '_hello', '_i8', '_8', '_i9', '_9',
      '_i10']