Skip to main content

MoaT-Lib-PrioMap

% start synopsis % start main

A heap that behaves like a dict (or vice versa).

The keys are ordered by their associated value.

% end synopsis

Features

  • Dictionary-style access:

    • h[key] = priority (insert/update)
    • prio = h[key] (lookup)
    • del h[key] (remove)
  • Bulk initialization: PrioMap({'a':1, 'b':2})

  • Priority operations:

    • h.popitem() & h.peekitem() for root (min)
    • h.update(key, new_prio) to change an existing key’s priority
  • Introspection:

    • len(h), key in h, h.is_empty()
  • Safe iteration:

    • .keys(), .values(), .items(), and plain for k, v in h:

    • Detects concurrent modifications and raises RuntimeError.

% end main

Non-Features

  • Storing more than the priority. Workaround: use a (prio, other_data) tuple.

  • Sorting by highest instead of lowest priority first. Workaround: store the negative priority value.

Installation

pip install moat-lib-priomap

Usage

PrioMap

from moat.lib.priomap import PrioMap

# Min-heap example
h = PrioMap({'a':5, 'b':2, 'c':3})
print(h.peekitem())  # ('b', 2)

# Insert
h['d'] = 1
print(h.popitem())   # ('d', 1)

# Update
h.update('a', 0)
print(h.peekitem())  # ('a', 0)

# Iterate. Does not consume the data.
for key, prio in h.items():  # keys(), values()
    print(f"{key} -> {prio}")
# emits a->0, d->1, b->2, c->3

# Async Iteration. Does consume the data!
# Waits for more data if/when it runs out.
async for key, prio in h:
    print(f"{key} -> {prio}")

TimerMap

from moat.lib.priomap import TimerMap

# example
h = TimerMap({'a':5, 'b':2, 'c':3})
print(h.peekitem())  # ('b', 1.995)

# Iterate
async for key in h:
    print(key)
# > waits two seconds
# b
# > waits another second
# c
# > two seconds later
# a

License

MIT.

Release files for moat-lib-priomap 0.2.7

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

Source distribution (sdist)

Source distribution for moat-lib-priomap 0.2.7
File Size Uploaded
moat_lib_priomap-0.2.7.tar.gz 9.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for moat-lib-priomap 0.2.7
File Interpreter ABI Platform
moat_lib_priomap-0.2.7-py3-none-any.whl Python 3 none any Details

Total release size: 17.0 kB

Release files / moat_lib_priomap-0.2.7.tar.gz

Download URL moat_lib_priomap-0.2.7.tar.gz
Size 9.3 kB
Tags Source
SHA-256 checksum
How to use checksums
35fcedc9f3e38c60f97ee3c622e1d030c66b935ede189cbc82ee0772155c43ac
BLAKE2b-256 checksum
How to use checksums
3f13108585d55bd6b807ec41fd3fb2613d7dbca8ee62f804a6d4e2d148bb43f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release files / moat_lib_priomap-0.2.7-py3-none-any.whl

Download URL moat_lib_priomap-0.2.7-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2f8d89ee385e05c364241d0675f222c0c84b4f1f1de3aea39d7cf7a006730d72
BLAKE2b-256 checksum
How to use checksums
c794da424459dbf3bcdf1391f8cdefff4bb98f242e7a404e5e2b3ff80c055f15
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release history Release notifications | RSS feed

This release

0.2.7 This release

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.1.1

2 release files

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