Skip to main content

packmem

A list of dicts that takes a fraction of the memory. Records are stored as packed columns that you can still index, iterate, append to and update, and that can be filtered and aggregated without unpacking.

rows = [json.loads(line) for line in open("events.jsonl")]   # before
rows = packmem.load_jsonl("events.jsonl")                    # after

rows keeps working as a sequence of dicts:

len(rows); rows[0]; rows[-5:]; rows.append({...}); rows.extend(more)
rows.insert(0, {...}); rows.pop(); del rows[10:20]; rows.reverse()
rows.sort(key="bytes")         # a column name, or a function of a record as in list.sort
rows.take([3, 1, 2]); rows.copy(); rows.shuffle(); pickle.dumps(rows)
rows[3] = {...}                 # replace a record
rows.update(3, status=500)      # change fields in place
for r in rows: ...

and gains queries that run on the packed data:

rows.count(status=404)
rows.sum("bytes", agent__contains="curl")
rows.group_count("country", path__prefix="/api/")
rows.where(delay__gt=60, carrier="UA").rows(limit=10)

Loops that read a few fields per record are fastest over lazy rows, which decode a field only when it is read and write back on assignment:

for r in rows.lazy():
    total += r["bytes"]          # about as fast as looping over the original list
    r["seen"] = True             # writes to the table

A lazy row is a mapping, not a dict; use r.to_dict() where a real dict is required.

Records can be written as JSON straight from the columns, without building a dict per record:

rows.to_json()                                   # bytes holding one JSON array
rows.where(carrier="UA").to_json(limit=50, offset=100)
for chunk in rows.iter_json(batch=4096): ...     # for streaming responses

On the flight data rows.to_json() takes 0.24 s against 0.43 s for json.dumps on the original list. Values always read back equal; a few floats are written with a different, equally short, last digit than json.dumps would choose.

Filters: col=value, col__prefix, col__contains, col__gt, col__gte, col__lt, col__lte, col__between=(lo, hi).

One-line swaps

Before After
[json.loads(l) for l in f] packmem.load_jsonl(f) (path or file)
json.load(f) for a list of records packmem.load_json(f) (path or file, streamed)
list(csv.DictReader(f)) packmem.read_csv(path) or packmem.Table(csv.DictReader(f))
[dict(r) for r in cursor] packmem.Table(cursor)
[dict(zip(names, r)) for r in cursor] packmem.from_cursor(cursor) (any DB-API cursor)
any iterable of dicts packmem.Table(records)

What to expect

Measured on 336,776 real flight records on an Apple M4 Pro (see benchmarks/ in the repository):

List of dicts packmem
Resident memory 712 MB 20 MB
Load from JSON lines 0.61 s 0.25 s
Filter and aggregate queries 31 to 59 ms 0.13 to 1.3 ms
Loop reading one field 31 ms 24 ms with rows.lazy(), 153 ms with plain iteration
10,000 random rows as dicts 1.3 ms 8.7 ms

Plain iteration builds a full dict per record, which is why it is slower than the list it replaces. Loading from csv.DictReader or a database cursor is 1.2 to 1.5 times slower than building the list.

Semantics to know

  • Values round-trip exactly: ints, floats (including NaN), bools, None, strings, nested lists and dicts, integers beyond 64 bits. Tuples come back as lists.
  • A returned row is a fresh dict; mutating it does not change the table. Use rows[i] = ... or rows.update(i, ...).
  • Key order in returned dicts is the order columns were first seen.
  • sum adds ints, floats and bools and skips everything else. Integers beyond 64 bits are stored exactly but are not treated as numbers by filters or sum.
  • sort, del, pop, reverse and insert anywhere but the end rebuild the table, so each takes time proportional to its size. Row numbers held by a View or a lazy row refer to positions, not records.
  • load_jsonl and load_json accept what json.loads accepts, including NaN, Infinity and strings with lone surrogate escapes.
  • Pickling the flight table gives 8 MB in 3 ms and loads in 1 ms (46 MB, 125 ms and 163 ms for the list).
  • A table can be pickled (as its packed columns, with no re-encoding on load), so it can be handed to multiprocessing and DataLoader workers started with spawn. Each worker then holds its own copy; under fork the packed data is shared.
  • rows.shuffle() and rows.take(indices) rebuild once. random.shuffle(rows) works but is far slower.

Status: alpha. Requires Python 3.10 or later. Built and tested on Linux x86-64 and macOS arm64; the CI workflow has not run yet.

Metadata

Release files for packmem 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for packmem 0.1.0
File Size Uploaded Provenance Actions
packmem-0.1.0.tar.gz 77.9 kB Without provenance Details for packmem-0.1.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for packmem 0.1.0
File Actions
packmem-0.1.0-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details for packmem-0.1.0-cp310-abi3-win_amd64.whl
packmem-0.1.0-cp310-abi3-musllinux_1_2_x86_64.whl CPython 3.10 abi3 Linux musl 1.2+ x86-64 Details for packmem-0.1.0-cp310-abi3-musllinux_1_2_x86_64.whl
packmem-0.1.0-cp310-abi3-musllinux_1_2_aarch64.whl CPython 3.10 abi3 Linux musl 1.2+ ARM64 Details for packmem-0.1.0-cp310-abi3-musllinux_1_2_aarch64.whl
packmem-0.1.0-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.10 abi3 Linux glibc 2.17+ x86-64 Details for packmem-0.1.0-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
packmem-0.1.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl CPython 3.10 abi3 Linux glibc 2.17+ ARM64 Details for packmem-0.1.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
packmem-0.1.0-cp310-abi3-macosx_11_0_arm64.whl CPython 3.10 abi3 macOS 11.0+ ARM64 Details for packmem-0.1.0-cp310-abi3-macosx_11_0_arm64.whl
packmem-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl CPython 3.10 abi3 macOS 10.12+ x86-64 Details for packmem-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl

Total release size: 4.0 MB

Release files / packmem-0.1.0.tar.gz

Download URL packmem-0.1.0.tar.gz
Size 77.9 kB
Tags Source
SHA-256 checksum
How to use checksums
b60bf49d7e980bb3bb32adc1a9d12e1ba55056f5fe73ab63b2d3a758f10a5067
BLAKE2b-256 checksum
How to use checksums
bd9194dfc7a732b792012e0f12bf10b5eb94d67bf05265880e50979bfb56c405
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Release files / packmem-0.1.0-cp310-abi3-win_amd64.whl

Download URL packmem-0.1.0-cp310-abi3-win_amd64.whl
Size 412.8 kB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
67c7a0bf031bbbb27773ad7e3ad05c8fc735f5723034feb91a7261d36a36cec2
BLAKE2b-256 checksum
How to use checksums
12ff25b882108c40a99e84f477637013dbe6df2a230ca8a792e3ceb2020fdb4b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Release files / packmem-0.1.0-cp310-abi3-musllinux_1_2_x86_64.whl

Download URL packmem-0.1.0-cp310-abi3-musllinux_1_2_x86_64.whl
Size 757.7 kB
Tags CPython 3.10 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
63c8f3264e978f4fcc609365ce8e1f193a284c0abd3a31c883afe5e20213e6fb
BLAKE2b-256 checksum
How to use checksums
1fb3b61db380bd944011ad1ca959b7a776201454f126dba7f00d760abc82b6c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Release files / packmem-0.1.0-cp310-abi3-musllinux_1_2_aarch64.whl

Download URL packmem-0.1.0-cp310-abi3-musllinux_1_2_aarch64.whl
Size 709.8 kB
Tags CPython 3.10 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
cbf616038fdfc7c2a60a700addd6e14ef81722998e84b52d83fd1b808c47bc13
BLAKE2b-256 checksum
How to use checksums
710cc101c433d2fd76f179b57c00e43c66709dc1ef363a08d87525a6899a6161
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Release files / packmem-0.1.0-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL packmem-0.1.0-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 544.5 kB
Tags CPython 3.10 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
924f087924680ade7e89380262fdf917f5625cea039792df1fdbc2f98308908f
BLAKE2b-256 checksum
How to use checksums
43307128f58107025a034b30821d8faca1ed66f07ed33e30dd67b3d12f3ce9b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Release files / packmem-0.1.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl

Download URL packmem-0.1.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Size 534.8 kB
Tags CPython 3.10 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
c080f80c87b10cccdbb315e11a360373210658d65d8c1b09de51980b0d23cf98
BLAKE2b-256 checksum
How to use checksums
0a1d09f720b2ed0223d08298d2bfd470414530366b87f2fa2bb2ca090d6d2227
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Release files / packmem-0.1.0-cp310-abi3-macosx_11_0_arm64.whl

Download URL packmem-0.1.0-cp310-abi3-macosx_11_0_arm64.whl
Size 499.7 kB
Tags CPython 3.10 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
bc4f175f692e30ce126a4b8e2f419bd721341ed8699f5e21a74985df5a78f697
BLAKE2b-256 checksum
How to use checksums
e64c958de2e6669111134ab5bc41a21194a4e6fe9261a02f01b4c89e2cf65008
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Release files / packmem-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl

Download URL packmem-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl
Size 503.9 kB
Tags CPython 3.10 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
a0f961c259ca139913b31b3dd4dc9f14c73da745cb12088d80d319fa0fc62431
BLAKE2b-256 checksum
How to use checksums
cfe96680600e96170a1bf68242755adf40a8d2e575cd314636c496db644c5c6f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Security for packmem 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page