https://github.com/rec/abbrev
🐜 Expand abbreviations 🐜
https://github.com/rec/abbrev
abbreviations python text
Last synced: about 2 months ago
JSON representation
🐜 Expand abbreviations 🐜
- Host: GitHub
- URL: https://github.com/rec/abbrev
- Owner: rec
- License: mit
- Created: 2021-03-25T09:22:02.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-02-14T14:42:57.000Z (over 2 years ago)
- Last Synced: 2025-03-24T04:04:16.381Z (over 1 year ago)
- Topics: abbreviations, python, text
- Language: Python
- Homepage: https://rec.github.io/abbrev/
- Size: 738 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG
- Funding: FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# 🐜 `abbrev`: Expand abbreviations 🐜
Expand a `Sequence` or `Mapping` of string abbreviations.
Handy when the user has a choice of commands with long names.
## Example 1: Use a list of choices
import abbrev
a = ['one', 'two', 'three']
assert abbrev(a, 'one') == 'one'
assert abbrev(a, 'o') == 'one'
assert abbrev(a, 'tw') == 'two'
abbrev(a, 'four') # Raises a KeyError: no such key
abbrev(a, 't') # Raises a KeyError: ambiguous key ('two' or 'three'?)
## Example 2: Use a dictionary of choices
import abbrev
d = {'one': 100, 'two': 200, 'three': 300}
assert abbrev(d, 'one') == 100
assert abbrev(d, 'o') == 100
assert abbrev(d, 'tw') == 200
## Example 3: Make an abbreviator to re-use
import abbrev
d = {'one': 100, 'two': 200, 'three': 300}
abbreviator = abbrev(d)
assert abbreviator('one') == my_abbrevs('o') == 100
assert abbreviator('tw') == 200
## Example 4: Get all matches, when `multi=True`
import abbrev
a = ['one', 'two, 'three'}
multi = abbrev(a, multi=True) # Make an abbreviator
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
## Example 5: Get only the first result, when `unique=False`
import abbrev
d = {'one': 100, 'two': 200, 'three': 300}
assert abbrev(d, 't', unique=False) == (200, 300)
### [API Documentation](https://rec.github.io/abbrev#abbrev--api-documentation)