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=<object object at 0x10bb0d7f0>,
default=<object object at 0x10bb0d7f0>,
multi=False,
unique=True,
)
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
- 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-03-27T15:44:24.818774)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.