File size: 880 Bytes
00e85f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import json
import tarfile

def iter_records(path):
    """Yield one shard at a time without extracting files."""
    current = None
    parts = {}
    with tarfile.open(path, mode="r|*") as archive:
        for member in archive:
            if not member.isfile():
                continue
            key, extension = member.name.split(".", 1)
            if current is not None and key != current:
                yield current, parts
                parts = {}
            current = key
            data = archive.extractfile(member).read()
            parts[extension] = json.loads(data) if extension == "json" else data
        if current is not None:
            yield current, parts

# for key, parts in iter_records("data/charts/charts-charts-00000.tar"):
#     annotation = parts["json"]
#     image_bytes = next(parts[x] for x in ("jpg", "png", "jpeg") if x in parts)