Skip to main content

synonym_dict

A class that allows retrieval of a given object by any of its synonyms.

Build StatusCoverage Status

Overview

There are many situations in which an object may be known by several names. synonym_dict provides a way to:

  1. Retrieve an object by its name or any synonyms
  2. Ensure that synonyms are distinct and non-overlapping
  3. Support case-insensitive tests

Installation

$ pip install synonym_dict

The package has no dependencies.

Testing

$ python -m unittest

Or, on python2:

$ python -m unittest discover

Code Design

SynonymSet

A SynonymSet a set of synonyms called "terms" in a hashable collection. Its "name" is canonically its first term, but can be set to any term in the collection. It can also have child objects, all of whose terms are taken to be synonyms.

# from TestSynonymSet.test_name()
s = SynonymSet('hello', 'aloha', 'Ni hao')
assert str(s) == 'hello'
assert s.object == 'hello'
s.set_name('aloha')
assert s.object == 'aloha'

Each synonym set can represent a particular object, such that the terms are synonymous names for that object. The object for the base SynonymSet is simply the name of the set, but subclasses can override this.

SynonymDict

# from TestSynonymDict.test_explicit_merge()
g = SynonymDict(ignore_case=False)  # default
g.new_entry('hello', 'hola', 'hi', 'aloha')
g.new_entry('Hello', 'HELLO', 'Hi', 'HI')
assert g['hi'] == 'hello'
assert g['HI'] == 'Hello'
g.merge('hi', 'HI')
assert g['HI'] == 'hello'

A SynonymDict is a typed collection of SynonymSets or subclasses, each of which is called an entry. The SynonymDict is responsible for managing the set of terms and preventing collisions. It can be case-sensitive or case-insensitive.

A key functionality of the dict is in combining entries. When creating a new entry, the dict first checks to see if any terms are already assigned to an existing entry. If they are, the merge strategy determines what to do among the choices of "merge", "prune", or "strict":

  • The default is to merge the terms into the existing entry. This fails with MergeError if the incoming terms match two or more entries.
  • If "prune" is specified, the duplicate terms are removed from the new entry and it is created using only unknown terms.
  • If neither "merge" nor "prune" are specified, the new entry is created only if every term is unknown; otherwise a TermExists error is raised.

LowerDict

d = LowerDict()
d['smeeb'] = 42
assert d['   SMeeB '] == 42
d[' dRoOl '] = 17
assert d['drool'] == 17
assert list(d.keys()) == ['smeeb', 'dRoOl']

A simple dict subclass that implements case-insensitivity. Also strips leading and trailing whitespace. Used to implement case-insensitivity in SynonymDicts

Subclasses

The main utility of these classes comes in subclassing. The standard approach is to create a subclass of SynonymSet that describes an object of some sort, and then to subclass SynonymDict to manage the set of entries. Two examples are provided and tested and will someday be documented.

Contributing

Fork or open an issue! Please! I crave critical appraisals of my design and/or implementation decisions.

Release files for synonym-dict 0.2.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for synonym-dict 0.2.7
File Size Uploaded
synonym_dict-0.2.7.tar.gz 26.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for synonym-dict 0.2.7
File Interpreter ABI Platform
synonym_dict-0.2.7-py3-none-any.whl Python 3 none any Details

Total release size: 67.4 kB

Release files / synonym_dict-0.2.7.tar.gz

Download URL synonym_dict-0.2.7.tar.gz
Size 26.6 kB
Tags Source
SHA-256 checksum
How to use checksums
7c85c6b4a18804117f3a2b388ccbf648835443e7fcc2570e2e9c919dffefb1b7
BLAKE2b-256 checksum
How to use checksums
c30d3a41c1a85f5a1596f814b8f7c2b4b0787bf36a9edc30a1e4e933e1ef1d28
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.12

Release files / synonym_dict-0.2.7-py3-none-any.whl

Download URL synonym_dict-0.2.7-py3-none-any.whl
Size 40.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0ff9ed0c7c0779466b08b75cb631d53ce4aea04d8347e17979a643beee743d54
BLAKE2b-256 checksum
How to use checksums
8d798ee52376286fdf1e2436e82ae764a8079bc6ece8cebefbf705b7242f1b93
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.2.7 This release

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page