Pyjo.Asset.Memory - In-memory storage for HTTP content¶
from Pyjo.Asset.Memory import Memory
mem = Memory()
mem.add_chunk(b'foo bar baz')
print(mem.slurp())
# Or import the module, as in Google Python Style Guide
import Pyjo.Asset.Memory
mem = Pyjo.Asset.Memory.new()
mem.add_chunk(b'foo bar baz')
print(mem.slurp())
Pyjo.Asset.Memory is an in-memory storage backend for HTTP content.
Events¶
Pyjo.Asset.Memory inherits all events from Pyjo.Asset and can
emit the following new ones.
upgrade¶
mem.on('upgrade', lambda mem, file: ...)
Emitted when asset gets upgraded to a Pyjo.Asset.File object.
mem.on('upgrade', lambda mem, file: file.set(tmpdir='/tmp'))
See also¶
Pyjoyment, https://mojolicious.org.
Ported from Mojo::Asset::Memory of Mojolicious 9.51.
- class Pyjo.Asset.Memory.Pyjo_Asset_Memory(*, auto_upgrade: bool | None = None, end_range: int | None = None, max_memory_size: int | None = None, mtime: float | None = None, start_range: int = 0)[source]¶
Pyjo.Asset.Memoryinherits all methods fromPyjo.Assetand implements the following new ones.- add_chunk(chunk: bytes) Pyjo_Asset[source]¶
mem = mem.add_chunk(b'foo bar baz') file = mem.add_chunk(b'abc' * 262144)
Add chunk of data and upgrade to
Pyjo.Asset.Fileobject if necessary.
- auto_upgrade¶
boolean = mem.auto_upgrade mem.auto_upgrade = boolean
Try to detect if content size exceeds
max_memory_sizelimit and automatically upgrade to aPyjo.Asset.Fileobject.
- contains(string: bytes) int[source]¶
position = mem.contains(b'bar')
Check if asset contains a specific string.
- get_chunk(offset: int, max: int | None = None) bytes[source]¶
chunk = mem.get_chunk(offset) chunk = mem.get_chunk(offset, max)
Get chunk of data starting from a specific position, defaults to a maximum chunk size of
131072bytes (128KiB).
- property max_memory_size: int[source]¶
size = mem.max_memory_size mem.max_memory_size = 1024
Maximum size in bytes of data to keep in memory before automatically upgrading to a
Pyjo.Asset.Fileobject, defaults to the value of thePYJO_MAX_MEMORY_SIZEenvironment variable or262144(256KiB).
- move_to(to: Anything) Self[source]¶
mem = mem.move_to('/home/sri/foo.txt')
Move asset data into a specific file.
- property mtime: float[source]¶
mtime = mem.mtime mem.mtime = 1408567500
Modification time of asset, defaults to the time the program started, as Perl’s
$^T.
- to_file() Pyjo_Asset_File[source]¶
file = mem.to_file()
Convert asset to
Pyjo.Asset.Fileobject.