Skip to main content

A dictionary that allows abbreviations

Project description

🐜 abbrev: look up abbreviations in a dictionary 🐜

Handy when the user has a choice of commands with long names.

EXAMPLE

import abbrev
d = {'one': 1, 'two': 2, 'three': 3}

assert abbrev(d, 'one') == 1
assert abbrev(d, 'o') == 1
assert abbrev(d, 'tw') == 2

abbrev(d, 'four')  # Raises a KeyError: no such key
abbrev(d, 't')  # Raises a KeyError: ambiguous

# You can "curry" a specific dictionary, and save it to call:
curry = abbrev(d)

assert curry('one') == 1
assert curry('tw') == 2

# In multi mode, you get all the results:
multi = abbrev(d, multi=True)
assert multi('t') == abbrev(d, 't', multi=True) == ('two', 'three')
assert multi('o') == abbrev(d, 'o', multi=True) == ('one', )
multi('four')  # Still raises a key error

# Turn off unique, and you get the first result:
assert abbrev(d, 't', unique=False) == ('two',)

API

abbrev()

abbrev(
     abbrevs,
     key=abbrev.NONE,
     default=abbrev.NONE,
     multi=False,
     unique=True,
)

(abbrev.py, 47-106)

Look up abbreviations in a dictionary. Handy when the user has a choice of commands with long names.

ARGUMENTS
abbrevs:

A dictionary with string keys or a sequence of strings

key:

An abbreviated key to look up in abbrevs,

If key is omitted, abbrev returns a callable that looks up abbreviations in abbrevs

default:

if key is not found in the dictionary, default is returned, if it’s set. Otherwise, missing keys throw a KeyError

multi:

If True, a tuple of matching keys is returned on a match If False, the default, only a single matching value is returned

unique:

If True, the default, abbrev raises a KeyError if more than one key matches. If False, abbrev returns the first match.

unique is ignored if multi is set

(automatically generated by doks on 2021-06-12T16:46:04.893076)

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

abbrev-1.0.1.tar.gz (2.8 kB view hashes)

Uploaded Source

Built Distribution

abbrev-1.0.1-py3-none-any.whl (5.3 kB view hashes)

Uploaded Python 3

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