archive_streamer¶
Archive-like streaming of data files.
This module provides ArchiveStreamer, an iterator over the data files of an archive: besides the classic zip and tar (with optional compression) formats, parquet and msgpack files are streamed as well - with each record generating a set of UID.ENTITY-named entries, similar to what a Web Dataset would expect.
Example
import os
import tempfile
import zipfile
import py_misc_utils.archive_streamer as ars
tdir = tempfile.mkdtemp()
path = os.path.join(tdir, 'data.zip')
with zipfile.ZipFile(path, 'w') as zf:
zf.writestr('0.txt', 'hello')
zf.writestr('1.bin', b'0A0B')
entries = list(ars.ArchiveStreamer(path))
assert [(e.name, e.data) for e in entries] == [
('0.txt', b'hello'), ('1.bin', b'0A0B')]
ArchiveStreamer
¶
An iterator over the data files of an archive.
Supported archive types: zip, tar (optionally compressed with gz/xz/bz2), parquet and msgpack files.
Example
import os
import tempfile
import msgpack
import py_misc_utils.archive_streamer as ars
import py_misc_utils.fs_utils as fsu
tdir = tempfile.mkdtemp()
path = os.path.join(tdir, 'recs.msgpack')
fsu.write_content(path, b''.join(msgpack.packb(r) for r in
[{'id': 1, 'val': 'one'},
{'id': 2, 'val': 'two'}]))
# Parquet/msgpack records generate UID.ENTITY-named entries (like
# a Web Dataset).
entries = [(e.name, e.data) for e in ars.ArchiveStreamer(path)]
assert len(entries) == 4
assert sorted(e[0].rsplit('.', 1)[1] for e in entries) == [
'id', 'id', 'val', 'val']
Source code in py_misc_utils/archive_streamer.py
91 92 93 94 95 96 97 98 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 207 208 209 210 | |
generate()
¶
Generates the data files of the archive.
Returns:
| Type | Description |
|---|---|
|
A generator yielding ArchiveEntry objects - one per data file - |
|
|
whose kind is selected by the archive URL extension. |
Source code in py_misc_utils/archive_streamer.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
parse_specs(url)
¶
Parses an archive URL into its archive specs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
The archive URL - with its archive-type extension, optionally followed by a compression extension. |
required |
Returns:
| Type | Description |
|---|---|
|
The ArchiveSpecs of the archive. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If the archive type extension cannot be inferred. |
Example
import py_misc_utils.archive_streamer as ars
specs = ars.parse_specs('http://host/data.tar.gz')
assert specs.base_path == '/data'
specs = ars.parse_specs('file.parquet')
assert specs.kind == 'parquet' and specs.compression is None
Source code in py_misc_utils/archive_streamer.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |