11.1 Abstract base class for SAGE objects

Module: sage.structure.sage_object

Abstract base class for SAGE objects

Module-level Functions

dumps( )

dumps(obj):

Dump obj to a string s. To recover obj, use loads(s).

sage: a = 2/3
sage: s = dumps(a)
sage: print len(s)
49
sage: loads(s)
2/3

load( )

load(filename):

Load Sage object from the file with name filename, which will have an .sobj extension added if it doesn't have one.

NOTE: There is also a special SAGE command (that is not available in Python) called load that you use by typing

sage: load filename.sage           # not tested

The documentation below is not for that command. The documentation for load is almost identical to that for attach. Type attach? for help on attach.

This also loads a ".sobj" file over a network by specifying the full URL. (Setting "verbose = False" suppresses the loading progress indicator.)

sage: u = 'http://sage.math.washington.edu/home/was/db/test.sobj'  # optional
sage: s = load(u)                                                  # optional
Attempting to load remote file:
http://sage.math.washington.edu/home/was/db/test.sobj
Loading: [.]        
sage: s                                                            # optional
'hello SAGE'

loads( )

Recover an object x that has been dumped to a string s using s = dumps(x).

sage: a = matrix(2, [1,2,3,-4/3])
sage: s = dumps(a)
sage: loads(s)
[   1    2]
[   3 -4/3]

One can load uncompressed data even if one messes up and doesn't specify compress=False. This is slightly slower though.

sage: v = [1..10]
sage: loads(dumps(v, compress=False)) == v
True
sage: loads(dumps(v, compress=False), compress=True) == v
True
sage: loads(dumps(v, compress=True), compress=False) == v
True

picklejar( )

Create pickled sobj of obj in dir, with name the absolute value of the hash of the pickle of obj. This is used in conjection with sage.structure.sage_object.unpickle_all.

To use this to test the whole Sage library right now, set the environment variable SAGE_PICKLE_JAR, which will make it so dumps will by default call picklejar with the default dir. Once you do that and doctest Sage, you'll find that the given directory contains a bunch of pickled objects along with corresponding txt descriptions of them. Use the sage.structure.sage_object.unpickle_all to see if they unpickle later.

INPUTS: obj - a pickleable object dir - a string or None; if None defaults to SAGE_ROOT/tmp/pickle_jar-version

sage: dir = tmp_dir()
sage: sage.structure.sage_object.picklejar(1,dir)
sage: len(os.listdir(dir))
2

save( )

save(obj, filename=None):

Save obj to the file with name filename, which will have an .sobj extension added if it doesn't have one. This will replace the contents of filename.

sage: a = matrix(2, [1,2,3,-5/2])
sage: save(a, 'test.sobj')
sage: load('test')
[   1    2]
[   3 -5/2]
sage: E = EllipticCurve([-1,0])
sage: P = plot(E)
sage: save(P, 'test')
sage: save(P, filename="sage.png", xmin=-2)
sage: print load('test.sobj')
Graphics object consisting of 2 graphics primitives
sage: save("A python string", './test')
sage: load('./test.sobj')
'A python string'
sage: load('./test')
'A python string'

unpickle_all( )

Unpickle all sobj's in the given directory, reporting failures as they occur. Also printed the number of successes and failure.

Input:

dir
- string; a directory

sage: dir = tmp_dir()
sage: sage.structure.sage_object.picklejar('hello', dir)
sage: sage.structure.sage_object.unpickle_all(dir)
Successfully unpickled 1 objects.
Failed to unpickle 0 objects.

Class: SageObject

class SageObject

Functions: category,$ \,$ db,$ \,$ dump,$ \,$ dumps,$ \,$ plot,$ \,$ rename,$ \,$ reset_name,$ \,$ save,$ \,$ version

db( )

Dumps self into the SAGE database. Use db(name) by itself to reload.

The database directory is $HOME/.sage/db

dump( )

Same as self.save(filename, compress)

dumps( )

Dump self to a string s, which can later be reconstituted as self using loads(s).

rename( )

Change self so it prints as x, where x is a string.

Note: This is only supported for Python classes that derive from SageObject.

sage: x = PolynomialRing(QQ,'x').gen()
sage: g = x^3 + x - 5
sage: g
x^3 + x - 5
sage: g.rename('a polynomial')
sage: g
a polynomial
sage: g + x
x^3 + 2*x - 5
sage: h = g^100
sage: str(h)[:20]
'x^300 + 100*x^298 - '
sage: h.rename('x^300 + ...')
sage: h
x^300 + ...

Real numbers are not Python classes, so rename is not supported:

sage: a = 3.14
sage: type(a)
<type 'sage.rings.real_mpfr.RealNumber'>
sage: a.rename('pi')
Traceback (most recent call last):
...
NotImplementedError: object does not support renaming: 3.14000000000000

Note: The reason C-extension types are not supported by default is if they were then every single one would have to carry around an extra attribute, which would be slower and waste a lot of memory.

To support them for a specific class, add a cdef public __custom_name attribute.

save( )

Save self to the given filename.

sage: f = x^3 + 5
sage: f.save(SAGE_TMP + '/file')
sage: load(SAGE_TMP + '/file.sobj')
x^3 + 5

version( )

The version of Sage.

Call this to save the version of Sage in this object. If you then save and load this object it will know in what version of Sage it was created.

This only works on Python classes that derive from SageObject.

Special Functions: __repr__,$ \,$ _axiom_,$ \,$ _axiom_init_,$ \,$ _gap_,$ \,$ _gap_init_,$ \,$ _gp_,$ \,$ _gp_init_,$ \,$ _interface_,$ \,$ _interface_init_,$ \,$ _interface_is_cached_,$ \,$ _kash_,$ \,$ _kash_init_,$ \,$ _macaulay2_,$ \,$ _macaulay2_init_,$ \,$ _magma_,$ \,$ _magma_init_,$ \,$ _maple_,$ \,$ _maple_init_,$ \,$ _mathematica_,$ \,$ _mathematica_init_,$ \,$ _maxima_,$ \,$ _maxima_init_,$ \,$ _octave_,$ \,$ _octave_init_,$ \,$ _pari_,$ \,$ _pari_init_,$ \,$ _r_init_,$ \,$ _sage_,$ \,$ _singular_,$ \,$ _singular_init_

__repr__( )

_axiom_( )

_axiom_init_( )

_gap_( )

_gap_init_( )

_gp_( )

_gp_init_( )

_interface_( )

Return coercion of self to an object of the interface I.

The result of coercion is cached, unless self is not a C extension class or self._interface_is_cached_() returns False.

_interface_init_( )

_interface_is_cached_( )

Return True if the interface objects are cached.

If you have an object x and do gp(x), the result is cached if this function returns True.

_kash_( )

_kash_init_( )

_macaulay2_( )

_macaulay2_init_( )

_magma_( )

_magma_init_( )

_maple_( )

_maple_init_( )

_mathematica_( )

_mathematica_init_( )

_maxima_( )

_maxima_init_( )

_octave_( )

_octave_init_( )

_pari_( )

_pari_init_( )

_r_init_( )

Return default string expression that evaluates in R to this object.

Output: string

sage: a = 2/3
sage: a._r_init_()
'2/3'

_sage_( )

_singular_( )

_singular_init_( )

See About this document... for information on suggesting changes.