2024-03-03 17:15:23 +00:00
|
|
|
# Copyright 2009-2024 Joshua Bronson. All rights reserved.
|
2021-05-08 14:25:29 +00:00
|
|
|
#
|
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Provide :class:`OnDup` and related functionality."""
|
2021-05-08 14:25:29 +00:00
|
|
|
|
2024-03-03 17:15:23 +00:00
|
|
|
from __future__ import annotations
|
2021-05-08 14:25:29 +00:00
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
import typing as t
|
2024-03-03 17:15:23 +00:00
|
|
|
from enum import Enum
|
2021-05-08 14:25:29 +00:00
|
|
|
|
|
|
|
|
2024-03-03 17:15:23 +00:00
|
|
|
class OnDupAction(Enum):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""An action to take to prevent duplication from occurring."""
|
2021-05-08 14:25:29 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
#: Raise a :class:`~bidict.DuplicationError`.
|
|
|
|
RAISE = 'RAISE'
|
|
|
|
#: Overwrite existing items with new items.
|
|
|
|
DROP_OLD = 'DROP_OLD'
|
|
|
|
#: Keep existing items and drop new items.
|
|
|
|
DROP_NEW = 'DROP_NEW'
|
2021-05-08 14:25:29 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def __repr__(self) -> str:
|
2022-11-07 18:06:49 +00:00
|
|
|
return f'{self.__class__.__name__}.{self.name}'
|
2021-05-08 14:25:29 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2024-03-03 17:15:23 +00:00
|
|
|
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.
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2024-03-03 17:15:23 +00:00
|
|
|
The :attr:`~OnDup.key` field specifies what action to take when a duplicate key is encountered.
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2024-03-03 17:15:23 +00:00
|
|
|
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.
|
2021-05-08 14:25:29 +00:00
|
|
|
|
|
|
|
*See also* :ref:`basic-usage:Values Must Be Unique`
|
2022-11-07 18:06:49 +00:00
|
|
|
(https://bidict.rtfd.io/basic-usage.html#values-must-be-unique)
|
2021-05-08 14:25:29 +00:00
|
|
|
"""
|
|
|
|
|
2024-03-03 17:15:23 +00:00
|
|
|
key: OnDupAction = DROP_OLD
|
|
|
|
val: OnDupAction = RAISE
|
2021-05-08 14:25:29 +00:00
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
#: Default :class:`OnDup` used for the
|
|
|
|
#: :meth:`~bidict.bidict.__init__`,
|
|
|
|
#: :meth:`~bidict.bidict.__setitem__`, and
|
|
|
|
#: :meth:`~bidict.bidict.update` methods.
|
2024-03-03 17:15:23 +00:00
|
|
|
ON_DUP_DEFAULT: t.Final[OnDup] = OnDup(key=DROP_OLD, val=RAISE)
|
2022-01-24 04:07:52 +00:00
|
|
|
#: An :class:`OnDup` whose members are all :obj:`RAISE`.
|
2024-03-03 17:15:23 +00:00
|
|
|
ON_DUP_RAISE: t.Final[OnDup] = OnDup(key=RAISE, val=RAISE)
|
2022-01-24 04:07:52 +00:00
|
|
|
#: An :class:`OnDup` whose members are all :obj:`DROP_OLD`.
|
2024-03-03 17:15:23 +00:00
|
|
|
ON_DUP_DROP_OLD: t.Final[OnDup] = OnDup(key=DROP_OLD, val=DROP_OLD)
|