Skip to main content

A map with priority values

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

moat_lib_priomap-0.2.6.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

moat_lib_priomap-0.2.6-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file moat_lib_priomap-0.2.6.tar.gz.

File metadata

  • Download URL: moat_lib_priomap-0.2.6.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for moat_lib_priomap-0.2.6.tar.gz
Algorithm Hash digest
SHA256 e1d612466755f0a316855d32063c90d94556c9a644e386e945d91775c7c011c2
MD5 12c9a03676a5c80d7dbe7d0ae344a5fa
BLAKE2b-256 15ba9bd21554544fbb0132fbb118d7c9835719c331086579fbfc7a9b07148e5b

See more details on using hashes here.

File details

Details for the file moat_lib_priomap-0.2.6-py3-none-any.whl.

File metadata

File hashes

Hashes for moat_lib_priomap-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 c6bbd5afb844f8d50b7dc208a541237ad31f91089ed2f808ec08176e730dad7e
MD5 1989157594163e6b0465c1329374a773
BLAKE2b-256 7be8ee53ffe58c32debb09b33a163b1ae5d709f7563be12d089b448d981b5fe0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page