Common Interface Functionality - Interpreter Interfaces

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

# Common Interface Functionality

See the examples in the other sections for how to use specific interfaces. The interface classes all derive from the generic interface that is described in this section.

AUTHORS:

- William Stein (2005): initial version

- William Stein (2006-03-01): got rid of infinite loop on startup if client system missing

- Felix Lawrence (2009-08-21): edited ._sage_() to support lists and float exponents in foreign notation.

- Simon King (2010-09-25): Expect._local_tmpfile() depends on Expect.pid() and is cached; Expect.quit() clears that cache, which is important for forking.

- Jean-Pierre Flori (2010,2011): Split non Pexpect stuff into a parent class.

- Simon King (2015): Improve pickling for InterfaceElement

class sage.interfaces.interface.AsciiArtString

  Bases: `str`

class sage.interfaces.interface.Interface(name)

  Bases: `WithEqualityById`, `ParentWithBase`

  Interface interface object.

  Note

  Two interfaces compare equal if and only if they are identical objects (this is a critical constraint so that caching of representations of objects in interfaces works correctly). Otherwise they are never equal.

  call(function_name, *args, **kwds)
  clear(var)

    Clear the variable named var.

  console()
  cputime()

    CPU time since this process started running.

  eval(code, **kwds)

    Evaluate code in an interface.

    This method needs to be implemented in sub-classes.

    Note that it is not always to be expected that it returns a non-empty string. In contrast, `get()` is supposed to return the result of applying a print command to the object so that the output is easier to parse.

    Likewise, the method `_eval_line()` for evaluation of a single line, often makes sense to be overridden.

  execute(*args, **kwds)
  function_call(function, args=None, kwds=None)

    EXAMPLES:

    Sage

        sage: from sage.interfaces.maxima_lib import maxima
        sage: maxima.quad_qags(x, x, 0, 1, epsrel=1e-4)
        [0.5,5.5511151231257...e-15,21,0]
        sage: maxima.function_call('quad_qags', [x, x, 0, 1], {'epsrel':'1e-4'})
        [0.5,5.5511151231257...e-15,21,0]

    Python

        >>> from sage.all import *
        >>> from sage.interfaces.maxima_lib import maxima
        >>> maxima.quad_qags(x, x, Integer(0), Integer(1), epsrel=RealNumber('1e-4'))
        [0.5,5.5511151231257...e-15,21,0]
        >>> maxima.function_call('quad_qags', [x, x, Integer(0), Integer(1)], {'epsrel':'1e-4'})
        [0.5,5.5511151231257...e-15,21,0]

  get(var)

    Get the value of the variable var.

    Note that this needs to be overridden in some interfaces, namely when getting the string representation of an object requires an explicit print command.

  get_seed()

    Return the seed used to set the random number generator in this interface.

    The seed is initialized as `None` but should be set when the interface starts.

    EXAMPLES:

    Sage

        sage: s = Singular()
        sage: s.set_seed(107)
        107
        sage: s.get_seed()
        107

    Python

        >>> from sage.all import *
        >>> s = Singular()
        >>> s.set_seed(Integer(107))
        107
        >>> s.get_seed()
        107

  get_using_file(var)

    Return the string representation of the variable var in self, possibly using a file. Use this if var has a huge string representation, since it may be way faster.

    Warning

    In fact unless a special derived class implements this, it will not be any faster. This is the case for this class if you’re reading it through introspection and seeing this.

  help(s)
  interact()

    This allows you to interactively interact with the child interpreter.

    Press `Ctrl` + `D` or type ‘quit’ or ‘exit’ to exit and return to Sage.

    Note

    This is completely different than the console() member function. The console function opens a new copy of the child interpreter, whereas the interact function gives you interactive access to the interpreter that is being used by Sage. Use sage(xxx) or interpretername(xxx) to pull objects in from sage to the interpreter.

  name(new_name=None)
  new(code)
  rand_seed()

    Return a random seed that can be put into `set_seed` function for any interpreter.

    This should be overridden if the particular interface needs something other than a small positive integer.

    EXAMPLES:

    Sage

        sage: from sage.interfaces.interface import Interface
        sage: i = Interface("")
        sage: i.rand_seed() # random
        318491487

        sage: s = Singular()
        sage: s.rand_seed() # random
        365260051

    Python

        >>> from sage.all import *
        >>> from sage.interfaces.interface import Interface
        >>> i = Interface("")
        >>> i.rand_seed() # random
        318491487

        >>> s = Singular()
        >>> s.rand_seed() # random
        365260051

  read(filename)

    EXAMPLES:

    Sage

        sage: filename = tmp_filename()
        sage: f = open(filename, 'w')
        sage: _ = f.write('x = 2\n')
        sage: f.close()
        sage: octave.read(filename)  # optional - octave
        sage: octave.get('x')        # optional - octave
        ' 2'
        sage: import os
        sage: os.unlink(filename)

    Python

        >>> from sage.all import *
        >>> filename = tmp_filename()
        >>> f = open(filename, 'w')
        >>> _ = f.write('x = 2\n')
        >>> f.close()
        >>> octave.read(filename)  # optional - octave
        >>> octave.get('x')        # optional - octave
        ' 2'
        >>> import os
        >>> os.unlink(filename)

  set(var, value)

    Set the variable var to the given value.

  set_seed(seed=None)

    Set the random seed for the interpreter and return the new value of the seed.

    This is dependent on which interpreter so must be implemented in each separately. For examples see gap.py or singular.py.

    If seed is `None` then should generate a random seed.

    EXAMPLES:

    Sage

        sage: s = Singular()
        sage: s.set_seed(1)
        1
        sage: [s.random(1,10) for i in range(5)]
        [8, 10, 4, 9, 1]

        sage: from sage.interfaces.interface import Interface
        sage: i = Interface("")
        sage: i.set_seed()
        Traceback (most recent call last):
        ...
        NotImplementedError: This interpreter did not implement a set_seed function

    Python

        >>> from sage.all import *
        >>> s = Singular()
        >>> s.set_seed(Integer(1))
        1
        >>> [s.random(Integer(1),Integer(10)) for i in range(Integer(5))]
        [8, 10, 4, 9, 1]

        >>> from sage.interfaces.interface import Interface
        >>> i = Interface("")
        >>> i.set_seed()
        Traceback (most recent call last):
        ...
        NotImplementedError: This interpreter did not implement a set_seed function

class sage.interfaces.interface.InterfaceElement(parent, value, is_name=False, name=None)

  Bases: `Element`

  Interface element.

  attribute(attrname)

    If this wraps the object x in the system, this returns the object x.attrname. This is useful for some systems that have object oriented attribute access notation.

    EXAMPLES:

    Sage

        sage: g = gap('SO(1,4,7)')
        sage: k = g.InvariantQuadraticForm()
        sage: k.attribute('matrix')
        [ [ 0*Z(7), Z(7)^0, 0*Z(7), 0*Z(7) ], [ 0*Z(7), 0*Z(7), 0*Z(7), 0*Z(7) ],
          [ 0*Z(7), 0*Z(7), Z(7), 0*Z(7) ], [ 0*Z(7), 0*Z(7), 0*Z(7), Z(7)^0 ] ]

    Python

        >>> from sage.all import *
        >>> g = gap('SO(1,4,7)')
        >>> k = g.InvariantQuadraticForm()
        >>> k.attribute('matrix')
        [ [ 0*Z(7), Z(7)^0, 0*Z(7), 0*Z(7) ], [ 0*Z(7), 0*Z(7), 0*Z(7), 0*Z(7) ],
          [ 0*Z(7), 0*Z(7), Z(7), 0*Z(7) ], [ 0*Z(7), 0*Z(7), 0*Z(7), Z(7)^0 ] ]

    Sage

        sage: e = gp('ellinit([0,-1,1,-10,-20])')
        sage: e.attribute('j')
        -122023936/161051

    [Python]

        >>> from sage.all import *
        >>> e = gp('ellinit([0,-1,1,-10,-20])')
        >>> e.attribute('j')
        -122023936/161051

  bool()

    Convert this element to a boolean.

    EXAMPLES:

    Sage

        sage: singular(0).bool()
        False
        sage: singular(1).bool()
        True

    Python

        >>> from sage.all import *
        >>> singular(Integer(0)).bool()
        False
        >>> singular(Integer(1)).bool()
        True

  gen(n)
  get_using_file()

    Return this element’s string representation using a file. Use this if `self` has a huge string representation. It’ll be way faster.

    EXAMPLES:

    Sage

        sage: from sage.interfaces.maxima_lib import maxima
        sage: a = maxima(str(2^1000))
        sage: a.get_using_file()
        '10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376'

    Python

        >>> from sage.all import *
        >>> from sage.interfaces.maxima_lib import maxima
        >>> a = maxima(str(Integer(2)**Integer(1000)))
        >>> a.get_using_file()
        '10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376'

  hasattr(attrname)

    Return whether the given attribute is already defined by this object, and in particular is not dynamically generated.

    EXAMPLES:

    Sage

        sage: from sage.interfaces.maxima_lib import maxima
        sage: m = maxima('2')
        sage: m.hasattr('integral')
        True
        sage: m.hasattr('gcd')
        False

    Python

        >>> from sage.all import *
        >>> from sage.interfaces.maxima_lib import maxima
        >>> m = maxima('2')
        >>> m.hasattr('integral')
        True
        >>> m.hasattr('gcd')
        False

  is_string()

    Tell whether this element is a string.

    By default, the answer is negative.

  name(new_name=None)

    Return the name of `self`. If `new_name` is passed in, then this function returns a new object identical to `self` whose name is `new_name`.

    Note that this can overwrite existing variables in the system.

    EXAMPLES:

    Sage

        sage: # optional - rpy2
        sage: x = r([1,2,3]); x
        [1] 1 2 3
        sage: x.name()
        'sage...'
        sage: x = r([1,2,3]).name('x'); x
        [1] 1 2 3
        sage: x.name()
        'x'

    Python

        >>> from sage.all import *
        >>> # optional - rpy2
        >>> x = r([Integer(1),Integer(2),Integer(3)]); x
        [1] 1 2 3
        >>> x.name()
        'sage...'
        >>> x = r([Integer(1),Integer(2),Integer(3)]).name('x'); x
        [1] 1 2 3
        >>> x.name()
        'x'

    Sage

        sage: s5 = gap.SymmetricGroup(5).name('s5')
        sage: s5
        SymmetricGroup( [ 1 .. 5 ] )
        sage: s5.name()
        's5'

    [Python]

        >>> from sage.all import *
        >>> s5 = gap.SymmetricGroup(Integer(5)).name('s5')
        >>> s5
        SymmetricGroup( [ 1 .. 5 ] )
        >>> s5.name()
        's5'

  sage(*args, **kwds)

    Attempt to return a Sage version of this object.

    This method does nothing more than calling `_sage_()`, simply forwarding any additional arguments.

    EXAMPLES:

    Sage

        sage: gp(1/2).sage()
        1/2
        sage: _.parent()
        Rational Field
        sage: singular.lib("matrix")
        sage: R = singular.ring(0, '(x,y,z)', 'dp')
        sage: singular.matrix(2,2).sage()
        [0 0]
        [0 0]

    Python

        >>> from sage.all import *
        >>> gp(Integer(1)/Integer(2)).sage()
        1/2
        >>> _.parent()
        Rational Field
        >>> singular.lib("matrix")
        >>> R = singular.ring(Integer(0), '(x,y,z)', 'dp')
        >>> singular.matrix(Integer(2),Integer(2)).sage()
        [0 0]
        [0 0]

class sage.interfaces.interface.InterfaceFunction(parent, name)

  Bases: `SageObject`

  Interface function.

class sage.interfaces.interface.InterfaceFunctionElement(obj, name)

  Bases: `SageObject`

  Interface function element.

  help()