File: __init__.py

package info (click to toggle)
python-sentinels 1.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 124 kB
  • sloc: python: 45; sh: 7; makefile: 3
file content (47 lines) | stat: -rw-r--r-- 1,231 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from .__version__ import __version__

try:
    # python3 renamed copy_reg to copyreg
    import copyreg
except ImportError:
    import copy_reg as copyreg


class Sentinel(object):
    _existing_instances = {}

    def __init__(self, name):
        super(Sentinel, self).__init__()
        self._name = name
        self._existing_instances[self._name] = self

    def __repr__(self):
        return f"<{self._name}>"

    def __getnewargs__(self):
        return (self._name,)

    def __new__(
        cls, name, obj_id=None
    ):  # obj_id is for compatibility with previous versions pylint: disable=W0613
        existing_instance = cls._existing_instances.get(name)
        if existing_instance is not None:
            return existing_instance
        return super(Sentinel, cls).__new__(cls)


def _sentinel_unpickler(
    name, obj_id=None
):  # obj_id is for compat. with prev. versions pylint: disable=W0613
    if name in Sentinel._existing_instances:
        return Sentinel._existing_instances[name]
    return Sentinel(name)


def _sentinel_pickler(sentinel):
    return _sentinel_unpickler, sentinel.__getnewargs__()


copyreg.pickle(Sentinel, _sentinel_pickler, _sentinel_unpickler)

NOTHING = Sentinel("NOTHING")