| |
| |
| |
| |
| |
|
|
|
|
| """Provide :class:`OnDup` and related functionality.""" |
|
|
| from __future__ import annotations |
|
|
| import typing as t |
| from enum import Enum |
|
|
|
|
| class OnDupAction(Enum): |
| """An action to take to prevent duplication from occurring.""" |
|
|
| |
| RAISE = 'RAISE' |
| |
| DROP_OLD = 'DROP_OLD' |
| |
| DROP_NEW = 'DROP_NEW' |
|
|
| def __repr__(self) -> str: |
| return f'{self.__class__.__name__}.{self.name}' |
|
|
|
|
| RAISE: t.Final[OnDupAction] = OnDupAction.RAISE |
| DROP_OLD: t.Final[OnDupAction] = OnDupAction.DROP_OLD |
| DROP_NEW: t.Final[OnDupAction] = OnDupAction.DROP_NEW |
|
|
|
|
| class OnDup(t.NamedTuple): |
| r"""A combination of :class:`~bidict.OnDupAction`\s specifying how to handle various types of duplication. |
| |
| The :attr:`~OnDup.key` field specifies what action to take when a duplicate key is encountered. |
| |
| The :attr:`~OnDup.val` field specifies what action to take when a duplicate value is encountered. |
| |
| In the case of both key and value duplication across two different items, |
| only :attr:`~OnDup.val` is used. |
| |
| *See also* :ref:`basic-usage:Values Must Be Unique` |
| (https://bidict.rtfd.io/basic-usage.html#values-must-be-unique) |
| """ |
|
|
| key: OnDupAction = DROP_OLD |
| val: OnDupAction = RAISE |
|
|
|
|
| |
| |
| |
| |
| ON_DUP_DEFAULT: t.Final[OnDup] = OnDup(key=DROP_OLD, val=RAISE) |
| |
| ON_DUP_RAISE: t.Final[OnDup] = OnDup(key=RAISE, val=RAISE) |
| |
| ON_DUP_DROP_OLD: t.Final[OnDup] = OnDup(key=DROP_OLD, val=DROP_OLD) |
|
|