Completed
Pull Request — master (#872)
by
unknown
01:33
created

zipline.utils.result   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 19
Duplicated Lines 0 %
Metric Value
dl 0
loc 19
rs 10
wmc 5
1
"""
2
Construction of sentinel objects.
3
4
Sentinel objects are used when you only care to check for object identity.
5
"""
6
import sys
7
8
9
def sentinel(name, doc=None):
10
    try:
11
        return sentinel._cache[name, doc]  # memoized
12
    except KeyError:
13
        pass
14
15
    @object.__new__   # bind a single instance to the name 'Sentinel'
16
    class Sentinel(object):
17
        __doc__ = doc
18
        __slots__ = ('__weakref__',)
19
        __name__ = name
20
21
        def __new__(cls):
22
            raise TypeError("Can't construct new instances of %r" % name)
23
24
        def __repr__(self):
25
            return 'sentinel(%r)' % name
26
27
        def __reduce__(self):
28
            return sentinel, (name, doc)
29
30
        def __deepcopy__(self, _memo):
31
            return self
32
33
        def __copy__(self):
34
            return self
35
36
    cls = type(Sentinel)
37
    try:
38
        # traverse up one frame to find the module where this is defined
39
        cls.__module__ = sys._getframe(1).f_globals['__name__']
40
    except (AttributeError, ValueError, KeyError):
41
        # Couldn't get the name from the calling scope, just use None.
42
        cls.__module__ = None
43
44
    sentinel._cache[name, doc] = Sentinel  # cache result
45
    return Sentinel
46
sentinel._cache = {}
47