Sharedsupport / 2015-08-16-162239-show-identifiers.sagewsOpen in CoCalc
Examples for support purposes...
show_identifiers()
['dynamic', 'hide', 'julia', 'md', 'smc', 'attach', 'jupyter', 'mediawiki', 'sage_salvus', 'typeset_mode', 'hideall', 'fortran', 'fork', 'reset', 'time', 'perl', 'default_mode', 'search_doc', 'sh', 'sage_server', 'wiki', 'require', 'show', 'checkbox', 'search_src', 'timeit', 'sage_chat', 'prun', 'modes', 'plot3d_using_matplotlib', 'runfile', 'input_box', 'pretty_print_default', 'ruby', 'input_grid', 'range_slider', 'go', 'capture', 'help', 'pandoc', 'script', 'interact', 'selector', 'clear', 'restore', 'color_selector', 'license', 'load', 'coffeescript', 'pylab', 'cython', 'button', 'cell', 'exercise', 'raw_input', 'slider', 'sage_eval', 'view', 'javascript', 'delete_last_output', 'python', 'auto', 'text_control', 'python3', 'var']

len(show_identifiers())
60
show_identifiers??
   File: /projects/sage/sage-6.7/src/sage/misc/session.pyx
   Source:
   def show_identifiers(hidden=False):
    r"""
    Returns 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`` -- bool (Default: ``False``); If ``True``, also return
      identifiers that start with an underscore.

    OUTPUT:

    A list of variable names

    EXAMPLES:

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

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

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

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

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

        sage: globals()['factor']
        20

    By default :func:`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: _hello = 10
        sage: show_identifiers()
        ['a', 'factor']
        sage: '_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: show_identifiers(hidden=True)        # random output
        ['__', '_i', '_6', '_4', '_3', '_1', '_ii', '__doc__', '__builtins__', '___', '_9', '__name__', '_', 'a', '_i12', '_i14', 'factor', '__file__', '_hello', '_i13', '_i11', '_i10', '_i15', '_i5', '_13', '_10', '_iii', '_i9', '_i8', '_i7', '_i6', '_i4', '_i3', '_i2', '_i1', '_init_cmdline', '_14']
    """
    state = caller_locals()
    return [x for x, v in state.iteritems() if _is_new_var(x, v, hidden)]

sage.misc.session._is_new_var??
   File: /projects/sage/sage-6.7/src/sage/misc/session.pyx
   Source:
   def _is_new_var(x, v, hidden):
    """
    Return whether or not the variable named ``x`` with value ``v`` is
    considered newly defined in the current session.

    INPUT:

    - ``x`` -- string

    - ``v`` -- object

    - ``hidden`` -- bool; if ``True``, always return ``False`` on variables
      that start with ``_``)

    OUTPUT:

    A bool

    EXAMPLES:

    We reset the session, then check whether the builtin factor function
    is newly defined (it isn't)::

        sage: reset()
        sage: sage.misc.session._is_new_var('factor', factor, True)
        False

    We then redefine factor, and find that it is newly defined::

        sage: factor = 10
        sage: sage.misc.session._is_new_var('factor', factor, True)
        True

    We define a new variable ``'blue'``, and test::

        sage: blue = 10
        sage: sage.misc.session._is_new_var('blue', blue, True)
        True
        sage: sage.misc.session._is_new_var('_blue', blue, True)
        True
        sage: sage.misc.session._is_new_var('_blue', blue, False)
        False
    """
    # We ignore all _ variable names unless hidden is True
    if not hidden and x.startswith('_'):
        return False
    # If a variable names was not there at init time then it is
    # definitely new.
    if x not in state_at_init:
        return True
    # A variable could also be new even if it was there at init, say if
    # its value changed.
    return x not in state_at_init or state_at_init[x] is not v

sage.misc.session.state_at_init = dict(globals())
show_identifiers()
['smc', 'require', 'salvus']
x = 10
show_identifiers()
['smc', 'require', 'x', 'salvus']
abc = 15
show_identifiers()
['smc', 'require', 'abc', 'x', 'salvus']