LazyWriter
Bases: LazyBuffer
Source code in src/msglc/writer.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
__init__(buffer_or_path, *, packer=None, fs=None, toc_cls=None)
It is possible to provide a custom packer object to be used for packing the object.
However, this packer must be implement encode and decode methods.
The buffer_or_path can be
1. a plain str pointing to a file on local filesystem, or a file on target filesystem if fs is provided,
2. a UPath object that points to a file on supported filesystem (either local or remote),
3. a IO object that has .tell(), .seek(), .write() methods, this object must have random write access.
It is possible to provide a customized TOC packer via the toc_cls parameter to customize how TOC is generated.
It will be initialized with two keyword arguments: packer=self._packer, buffer=self._buffer
where packer is the packer object to be used for encoding msgpack data and
buffer is a file-like IO object where serialized data will be written to.
It needs to have a public method def pack(self, obj) -> dict: ... that will be called by the writer.
See the implementation of TOC class for further details.
Warning:
Some of backend filesystems only supports sequential write rather than random write thus not all filesystems
supported by UPath can be used.
One must always check if the target filesystem supports random write access.
If not, the most generic approach is to provide a plain string for buffer_or_path and explicitly assign a fs object.
In this case, a local cache will be used to temporarily handle the serialization and the binary blob
will be uploaded to the remote once everything is processed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
buffer_or_path
|
str | UPath | BufferWriter
|
target buffer or file path |
required |
packer
|
type[LazyCodec] | LazyCodec | None
|
packer object to be used for packing the object |
None
|
fs
|
FileSystem | None
|
|
None
|
toc_cls
|
type[TOC] | None
|
a |
None
|
Source code in src/msglc/writer.py
write(obj)
This function is used to write the object to the file.
Only one write is allowed. The function raises a ValueError if it is called more than once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
the object to be written to the file |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the function is called more than once |