Skip to main content

A multidirectional map where an arbitrary number of columns can be used as keys.

Project description

multidirmap

Multidirectional map where an arbitrary number of columns can be used as keys.

Status

Documentation Status Travis-CI Build Status AppVeyor Build Status Coverage Status PyPI Package latest release PyPI Wheel Supported versions Supported implementations License

Installation

$ pip install multidirmap

Documentation

https://multidirmap.readthedocs.io/en/latest/

Quick Start

>>> from multidirmap import MultiDirMap
>>> crew = MultiDirMap(
        ["character", "portrayed_by", "role", "nicknames"],
        key_columns=2,
        data=[["Malcolm Reynolds", "Nathan Fillion", "Captain", ["Mal", "Captain Tight Pants"]],
              ["Zoë Washburne", "Gina Torres", "First Mate"],
              ["Hoban Washburne", "Alan Tudyk", "Pilot", "Wash"]])
>>> crew["Malcolm Reynolds"].role
Captain
>>> crew.portrayed_by["Nathan Fillion"].nicknames
["Mal", "Captain Tight Pants"]

Features

  • As many columns as desired can be used as key columns for the mapping

  • O(1) retrieval from any key column

  • Internal consistency is maintained through any modifications to the contents

  • Insertion order is maintained in the primary key column

  • Built-in pretty printing of the mapping

Use Cases

Dictionaries are ubiquitous in Pyton and provide an extremely useful and fast mapping from keys to values. Sometimes, a single, uni-directional mapping is not enough, though, and while bidict extends this functionality to a bidirectional mapping, multidirmap provides an array-like datastructure where any number of columns can be used for O(1) retrieval. In its simplest implementation (2 columns, one of which is a key column), it essentially provides the same functionality as a dict, albeit with additional overhead (don’t do that…). 2 columns that are both key columns will behave somewhat like a bidict, albeit with slightly different syntax. But multidirmap is significantly more flexible in that any number of key and non-key columns can be used. A somewhat similar effect could be achieved with pandas DataFrames, though these (1) will not ensure uniqueness of keys, hence a retrieval may return any number of rows, (2) use an array structure, hence retrieval is O(n) which for large arrays can get very slow, and (3) require the installation of pandas, which is a rather large library to include just for this feature.

Say we want to work with information from the Periodic Table of Elements, like

[["H", "Hydrogen", 1, [1, 2, 3]],
 ["He", "Helium", 2, [4, 3]],
 ["Li", "Lithium", 3, [7, 6]],
 ...
 ["Og", "Oganesson", 118, [295, 294]]]

where the columns indicate symbol, name, atomic number, and nucleon numbers of isotopes respectively. The first three columns are obvious candidates for key columns as they are by definition unique. multidirmap allows placing this information in a unified datastructure where questions like “What are the isotope nucleon numbers of Lithium?”, “What is the chemical element symbol of Potassium?”, or “What is the name of the element with atomic number 46?” can be asked with a simple syntax and O(1) retrieval. Any number of additional key and non-key columns could be added.

The use case that prompted the development on this package involved the struct module: For a binary interface I needed to convert back and forth between (1) a string representation of the variable type, (2) an integer representation of the variable type, (3) the struct format char, and (4) the size in bytes of the variable. Again, 1-3 are obvious candidates for key columns, with 4 being a non-key column. Without multidirmap, several separate dicts have to be used to provide each needed mapping from one column to another and there is easy way to ensure that these dicts remain consistent with each other through possible changes.

Changelog

0.3.0 (2019-10-03)

  • overwrite now takes an enum Overwrite (backported to 2.7 via enum34) with values NONE, PRIMARY, SECONDARY, or ALL instead of string values (though the implementation is backwards compatible to using the old string values).

  • copy.copy() and copy.deepcopy() now work properly on a MultiDirMap

  • aslist() and asdict() on row elements renamed to to_list() and to_dict()

  • new method to_list() which returns complete map data as a list of lists

0.2.0 (2019-07-12)

  • Custom sorting

  • Reordering of secondary keys

0.1.0 (2018-07-28)

  • First release on PyPI.

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

multidirmap-0.3.0.tar.gz (29.5 kB view details)

Uploaded Source

Built Distribution

multidirmap-0.3.0-py2.py3-none-any.whl (11.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file multidirmap-0.3.0.tar.gz.

File metadata

  • Download URL: multidirmap-0.3.0.tar.gz
  • Upload date:
  • Size: 29.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.2

File hashes

Hashes for multidirmap-0.3.0.tar.gz
Algorithm Hash digest
SHA256 c98f23b633449e9fd4c4120d3277a22d58016ce7a1c61b7f0fb583f744a676b1
MD5 463a3409cff37b682ab556c2ba1fde7c
BLAKE2b-256 88ba0704841bad8fe0bb1a2f1ae8f20e0872ea0b83377df81952b7950d85ad3f

See more details on using hashes here.

File details

Details for the file multidirmap-0.3.0-py2.py3-none-any.whl.

File metadata

  • Download URL: multidirmap-0.3.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.2

File hashes

Hashes for multidirmap-0.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 84a57461dbfe5aa5b6f23ad67fb4c8d0c9db12db968e61c2789c7f0ad420a813
MD5 631721805d26d5fe1e81c8730f98bc74
BLAKE2b-256 9e9a9f80501e64d0664a33cf630fd78f8dc955edc59d7210596e0ad085eb366e

See more details on using hashes here.

Supported by

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