Skip to main content

A dict-like container that allows multiple keys to address the same value.

Project description

Multi-Lookup-Dict

A dict-like container that allows multiple keys to address the same value.


Installation

pip install multilookupdict

Usage

>>> d = MultiLookupDict()
>>> d["a_key"] = "some_value"
>>> d.map_key("a_key", "another_key") # Make "another_key" an alias of "a_key"

Implemented as two dicts: - MultiLookupDict._data holds the 'canonical key' and value - MultiLookupDict._key_to_canonical_map maps 'alias keys' onto canonical keys. (Canonical keys are mapped to themselves in this dict)

Externally, all keys (canonical and alias) are treated identically, and all refer to the same value, unless a key is reassigned to another value using map_key.

Multi-key lookups and assignments

Iterables of keys can also be accessed, set, and mapped.

>>> d = MultiLookupDict()
>>> d[("key_a", "key_b", "key_c")] = "some_value"
>>> d["key_a"] == "some_value"

Where items are accessed with multiple keys, all distinct matching values are returned as a list (where multiple keys are requested, the result is always a list, for consistency)

>>> d["key_d"] = "some_other_value" # Add a distinct value
>>> d[("key_a", "key_b", "key_d")] == ["some_value", "some_other_value"]


>>> d.map_key("key_a", ("key_e", "key_f")) # Also do multiple mappings

To use a tuple as a single key, rather than a group of independent keys, make sure it is contained within another iterable, i.e. d[(("tuple_part1", "tuple_part2"),)] = "some value". Remember that when accessing this tuple-key, a list of items will be returned.

Methods

__setitem__(key/iterable_of_keys, value)
Sets a key to the value. If a (non-string) iterable is provided as key, each key will be assigned the value.
__getitem__(key/iterable_of_keys)
Gets a value from a key. If a (non-string) iterable is provided as a key, a list of distinct values matching all provided keys will be returned.
map_key(existing_key, new_key)
Assign the value of one key to another key. Both keys now point to the same value.
keys()
Returns all keys in MultiLookupDict. Returned keys refer to same or different objects.
values()
[Same as dict.values]
items()
Same as dict.items, except "key" part of the tuple is a set of keys for the corresponding value
pop(key)
Same as dict.pop. All keys pointing to value are removed.
aliases(key, omit_requested_key=False)
Returns all aliases of a given key, including the key provided. (Set omit_requested_key to True to exclude the provided key.)

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

multilookupdict-0.1.7.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

multilookupdict-0.1.7-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file multilookupdict-0.1.7.tar.gz.

File metadata

  • Download URL: multilookupdict-0.1.7.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.4 Darwin/21.6.0

File hashes

Hashes for multilookupdict-0.1.7.tar.gz
Algorithm Hash digest
SHA256 61e6d287508b9d22c775d42009dd4eee0b1bb5e2778b5b8f37a6d865478b3f70
MD5 5274fc56626fae6dbac1e8303961e761
BLAKE2b-256 2da91491bd7524491efcfe607689014ae065aa2d997d47a7180d3724e2f45097

See more details on using hashes here.

File details

Details for the file multilookupdict-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: multilookupdict-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.4 Darwin/21.6.0

File hashes

Hashes for multilookupdict-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 e9c98764a8129eaddf3a709648e693dfdcb1121a91e6cae52d3cd422c280abed
MD5 0b63a3e5ffdbe1ba92197ab264922105
BLAKE2b-256 47d696b1e11a610d7d45b09455c433914af05669d78673053e383699555982a8

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