2022-11-07 18:06:49 +00:00
|
|
|
# Copyright 2009-2022 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
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
from enum import Enum
|
2022-11-07 18:06:49 +00:00
|
|
|
import typing as t
|
2021-05-08 14:25:29 +00:00
|
|
|
|
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
class OD(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
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
RAISE = OD.RAISE
|
|
|
|
DROP_OLD = OD.DROP_OLD
|
|
|
|
DROP_NEW = OD.DROP_NEW
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
class OnDup(t.NamedTuple('_OnDup', [('key', OD), ('val', OD), ('kv', OD)])):
|
|
|
|
r"""A 3-tuple of :class:`OD`\s specifying how to handle the 3 kinds of duplication.
|
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)
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
If *kv* is not specified, *val* will be used for *kv*.
|
2021-05-08 14:25:29 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
__slots__ = ()
|
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
def __new__(cls, key: OD = DROP_OLD, val: OD = RAISE, kv: t.Optional[OD] = None) -> 'OnDup':
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Override to provide user-friendly default values."""
|
|
|
|
return super().__new__(cls, key, val, kv or val)
|
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.
|
2022-11-07 18:06:49 +00:00
|
|
|
ON_DUP_DEFAULT = OnDup(key=DROP_OLD, val=RAISE, kv=RAISE)
|
2022-01-24 04:07:52 +00:00
|
|
|
#: An :class:`OnDup` whose members are all :obj:`RAISE`.
|
|
|
|
ON_DUP_RAISE = OnDup(key=RAISE, val=RAISE, kv=RAISE)
|
|
|
|
#: An :class:`OnDup` whose members are all :obj:`DROP_OLD`.
|
|
|
|
ON_DUP_DROP_OLD = OnDup(key=DROP_OLD, val=DROP_OLD, kv=DROP_OLD)
|