pyppin.file.memfile¶
Allow access to a single block of RAM with multiple file API handles.
Classes
|
Access a block of RAM as a file-like object. |
- class pyppin.file.memfile.MemFile(name: str = 'memfile')[source]¶
Bases:
FileLikeObject
Access a block of RAM as a file-like object.
Unlike using io.BytesIO or io.StringIO, which do similar things, a MemFile lets you create a single block of memory and have multiple things refer to the same block by open()ing it. This is especially useful if you need to make the same chunk of data simultaneously available to two API’s that each expect files. (Alas, a common issue with cloud API’s)
The simplest way to use this object is to just allocate it and call open() on it to get as many file handles (of as many types) as you need.
This class is thread-compatible: it provides no synchronization of its own, and (just like with files on local disk) if multiple threads are reading and writing a file at once, you’d better coordinate that.
- open(mode: str = 'r', buffering: int = -1, encoding: Optional[str] = None, errors: Optional[str] = None, newline: Optional[str] = None) Union[RawIOBase, BufferedIOBase, TextIOBase] [source]¶
Create a new file handle which accesses the data in this MemFile.
The resulting object is of the appropriate type (io.RawIO, io.BufferedIO, or io.TextIO) depending on the arguments in the usual fashion.
Note that there is no inherent thread-safety in these files: If multiple threads are reading and writing the same MemFile at once, they need to synchronize their access to it, just like they would for ordinary files on disk.
- property writable: bool[source]¶
Return true if it’s possible to open this file for writing or otherwise mutate it.
- pread(offset: int, size: int, buffer: Optional[Union[bytearray, array, memoryview, mmap, _CData, PickleBuffer]] = None) Tuple[int, Union[bytes, bytearray, array, memoryview, mmap, _CData, PickleBuffer]] [source]¶
Read [size] bytes from absolute position [offset].
- Parameters
offset – The absolute offset from which to read.
size – The maximum number of bytes to read.
buffer – If given, transfer the resulting data into this bytes-like object. If not, allocate a fresh one.
- Returns
A tuple of the number of bytes read, and the object into which the data was read.
- Raises
OSError if anything here is invalid, including if not self.readable. –
ValueError if a buffer was provided that was too small to read into. –
- pwrite(offset: int, data: Union[bytes, bytearray, array, memoryview, mmap, _CData, PickleBuffer]) int [source]¶
Write the bytes in [data] to absolute position [offset].
- Parameters
offset – The absolute offset at which to write.
data – The data to write.
- Returns
The number of bytes actually written.
Raises: OSError if anything here is invalid, including if not self.writable.