Skip to main content

A synchronous one-to-one mapping type

Project description

gloss-collection

Gloss (short for glossary) is a different kind of dictionary. All values also automatically become keys and so can be looked up in the Gloss to find its corresponding 'key'. This makes it easy to lookup and maintain one-to-one relationships such as a label to a certain magnitude.

As both the key and value are gettable items from the Gloss and are indistinguishable once added, term is the preferred name for items in a Gloss, and a term pair is the 1-1 mapping each term belongs to.

Gloss is a MutableMapping and supports all the same methods that dict does.

Examples

from gloss import Gloss

example = Gloss()
example["stdin"] = 0
example.update({"stdout": 1, "stderr": 2})
print(example)
#Gloss({"stdin": 0, "stdout": 1, "stderr": 2})
print(example[1], "goes to" example["stderr"])
#stdout goes to 2
example[3] = "config.toml"
example[1] = "shell pipe"
print(example)
#Gloss({"stdin": 0, "stderr": 2, "config.toml": 3, "shell pipe": 1})
print([fd_or_desc for fd_or_desc in example])
#["stdin", "stderr", "config.toml", "shell pipe", 0, 1, 2 , 3]

Shouldn't I just use an enum?

Great thought! You can, and often should, use an Enum for these sorts of relationsips. Besides being built in to Python, Enums are probably faster and more space efficient. However, some limitations of Enums solved by Gloss are:

  • enum members are static. They are defined all at once in the class and their values cannot change. A Gloss on the other hand can be added to, altered, even have members deleted or popped, all at runtime
  • Accessing Enum member namess and their values are differnt operations. Looking up a member by name is done with either dot dereference or getitem; looking up a member by value is done with a call. With a Gloss you don't have to know which side of the mapping your key is on (if there even is a distinction to the mapping), it is all done by getitem
  • One side of an enum mapping must be a string. Because member names are attributes they must follow Python identifier naming rules. In a Gloss, all keys may be any hashable object

Shouldn't I just use a dict?

While the classic dict solves some of limitations of using an Enum, primarily being mutable at runtime, looking up any value is O(n) and therefore so is updating, deleting, poping, etc that value. In a Gloss searching for any value is O(1) time, and updating that value (for example chaning the key it maps to) can be done in a single operation.

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

gloss-collection-1.2.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

gloss_collection-1.2-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file gloss-collection-1.2.tar.gz.

File metadata

  • Download URL: gloss-collection-1.2.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.3

File hashes

Hashes for gloss-collection-1.2.tar.gz
Algorithm Hash digest
SHA256 e018cbe62720cd584308d1c2c0d959254ee48df928c31c39e0fcb9585dbdd046
MD5 0ae231982449eeeed81d48128b5fee49
BLAKE2b-256 9a6af3a06bd46c85e1ff98435c9f450fc641c634bc70d47f2c4b4e570f0e6a25

See more details on using hashes here.

File details

Details for the file gloss_collection-1.2-py3-none-any.whl.

File metadata

  • Download URL: gloss_collection-1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.3

File hashes

Hashes for gloss_collection-1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f3ce11be80d5f25251c0fe378916259f4520396711f4769913c68ca3cba1a9d5
MD5 7dc01647c64794e6e3b06f6ee1378ba8
BLAKE2b-256 076c6ee382fdc91b30dcd2ccf689db0b6a936018de8997df83cf95e82eb3ff35

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