This is the fundamental unit of rich output, a single immutable buffer (either in-memory or as a file). Rich output always consists of one or more buffers. Ideally, the Sage library always uses the buffer object as an in-memory buffer. But you can also ask it for a filename, and it will save the data to a file if necessary. Either way, the buffer object presents the same interface for getting the content of an in-memory buffer or a temporary file. So any rich output backends do not need to know where the buffer content is actually stored.
EXAMPLES:
sage: from sage.repl.rich_output.buffer import OutputBuffer
sage: buf = OutputBuffer('this is the buffer content'); buf
buffer containing 26 bytes
sage: buf.get()
'this is the buffer content'
Bases: sage.structure.sage_object.SageObject
Data stored either in memory or as a file
This class is an abstraction for “files”, in that they can either be defined by a bytes array (Python 3) or string (Python 2) or by a file (see from_file()).
INPUT:
EXAMPLES:
sage: from sage.repl.rich_output.buffer import OutputBuffer
sage: buf = OutputBuffer('this is the buffer content'); buf
buffer containing 26 bytes
sage: buf2 = OutputBuffer(buf); buf2
buffer containing 26 bytes
sage: buf.get()
'this is the buffer content'
sage: buf.filename(ext='.txt')
'/....txt'
Return the filename.
INPUT:
OUTPUT:
Name of a file, most likely a temporary file. If ext is specified, the filename will have that extension.
You must not modify the returned file. Its permissions are set to readonly to help with that.
EXAMPLES:
sage: from sage.repl.rich_output.buffer import OutputBuffer
sage: buf = OutputBuffer('test')
sage: buf.filename() # random output
'/home/user/.sage/temp/hostname/26085/tmp_RNSfAc'
sage: os.path.isfile(buf.filename())
True
sage: buf.filename(ext='txt') # random output
'/home/user/.sage/temp/hostname/26085/tmp_Rjjp4V.txt'
sage: buf.filename(ext='txt').endswith('.txt')
True
Construct buffer from data in file.
Warning
The buffer assumes that the file content remains the same during the lifetime of the Sage session. To communicate this to the user, the file permissions will be changed to read only.
INPUT:
OUTPUT:
String containing the buffer data.
EXAMPLES:
sage: from sage.repl.rich_output.buffer import OutputBuffer
sage: name = sage.misc.temporary_file.tmp_filename()
sage: with open(name, 'w') as f:
....: f.write('file content')
sage: buf = OutputBuffer.from_file(name); buf
buffer containing 12 bytes
sage: buf.filename() == name
True
sage: buf.get()
'file content'
Return the buffer content
OUTPUT:
Bytes. A string in Python 2.x.
EXAMPLES:
sage: from sage.repl.rich_output.buffer import OutputBuffer
sage: OutputBuffer('test1234').get()
'test1234'
Save a copy of the buffer content.
You may edit the returned file, unlike the file returned by filename().
INPUT:
EXAMPLES:
sage: from sage.repl.rich_output.buffer import OutputBuffer
sage: buf = OutputBuffer('test')
sage: buf.filename(ext='txt') # random output
sage: tmp = tmp_dir()
sage: filename = os.path.join(tmp, 'foo.txt')
sage: buf.save_as(filename)
sage: with open(filename, 'r') as f:
....: f.read()
'test'