cramjam

1from .cramjam import *
2
3__doc__ = cramjam.__doc__
4if hasattr(cramjam, "__all__"):
5    __all__ = cramjam.__all__
class CompressionError(builtins.Exception):

Common base class for all non-exit exceptions.

class DecompressionError(builtins.Exception):

Common base class for all non-exit exceptions.

__version__ = '2.11.0'
class File:

A native Rust file-like object. Reading and writing takes place through the Rust implementation, allowing access to the underlying bytes in Python.

Python Example

from cramjam import File
file = File("/tmp/file.txt")
file.write(b"bytes")

Notes

Presently, the file's handle is managed by Rust's lifetime rules, in that once it's garbage collected from Python's side, it will be closed.

def write(self, /, input):

Write some bytes to the file, where input data can be anything in BytesType

def read(self, /, n_bytes=None):

Read from the file in its current position, returns bytes; optionally specify number of bytes to read.

def readinto(self, /, output):

Read from the file in its current position, into a BytesType object.

def seek(self, /, position, whence=None):

Seek to a position within the file. whence follows the same values as IOBase.seek where:

0: from start of the stream
1: from current stream position
2: from end of the stream
def seekable(self, /):

Whether the file is seekable; here just for compatibility, it always returns True.

def tell(self, /):

Give the current position of the file.

def set_len(self, /, size):

Set the length of the file. If less than current length, it will truncate to the size given; otherwise will be null byte filled to the size.

def truncate(self, /):

Truncate the file.

def len(self, /):

Length of the file in bytes

class Buffer:

A native Rust file-like object. Reading and writing takes place through the Rust implementation, allowing access to the underlying bytes in Python.

Python Example

>>> from cramjam import Buffer
>>> buf = Buffer(b"bytes")
>>> buf.read()
b'bytes'

NOTE: Use copy=False responsibly! That is to say, it will not copy the data, and will be referencing the underlying buffer during this Buffer's lifetime. We make an attempt to realign each time when accessing the buffer, but one should broadly take care to use locks where neccessary. Internally we increment the PyObject ref count, so it should be free from said buffer being garbage collected out from under us, but do try to avoid any funny business. :)

copy=False is not supported on PyPy distributions

def ensure_aligned_view(self, /):

When the underlying buffer view has maybe changed, call this to realign it according to the object we're referencing. Has no effect if Buffer has owned data or if it's determined no change has occurred, by comparing pointer and length.

def get_view_reference(self, /):

Get the PyObject this Buffer is referencing as its view, returns None if this Buffer owns its data.

def get_view_reference_count(self, /):

Get the PyObject reference count this Buffer is referencing as its view, returns None if this Buffer owns its data.

def len(self, /):

Length of the underlying buffer

def write(self, /, input):

Write some bytes to the buffer, where input data can be anything in BytesType

def read(self, /, n_bytes=None):

Read from the buffer in its current position, returns bytes; optionally specify number of bytes to read.

def readinto(self, /, output):

Read from the buffer in its current position, into a BytesType object.

def seek(self, /, position, whence=None):

Seek to a position within the buffer. whence follows the same values as IOBase.seek where:

0: from start of the stream
1: from current stream position
2: from end of the stream
def seekable(self, /):

Whether the buffer is seekable; here just for compatibility, it always returns True.

def tell(self, /):

Give the current position of the buffer.

def set_len(self, /, size):

Set the length of the buffer. If less than current length, it will truncate to the size given; otherwise will be null byte filled to the size.

def truncate(self, /):

Truncate the buffer