moved make_sentinel into typeutils and deleted the confusing compat module

This commit is contained in:
Mahmoud Hashemi 2015-04-16 02:18:01 -07:00
parent a7afa9bde6
commit 7e5baff5b9
8 changed files with 41 additions and 43 deletions

View File

@ -49,7 +49,7 @@ except:
pass
try:
from compat import make_sentinel
from typeutils import make_sentinel
_MISSING = make_sentinel(var_name='_MISSING')
_KWARG_MARK = make_sentinel(var_name='_KWARG_MARK')
except ImportError:

View File

@ -1,37 +0,0 @@
# -*- coding: utf-8 -*-
import sys
IS_PY2 = sys.version_info[0] == 2
IS_PY3 = sys.version_info[0] == 3
if IS_PY2:
from StringIO import StringIO
unicode, str, bytes, basestring = unicode, str, str, basestring
xrange = xrange
lrange = range
elif IS_PY3:
from io import StringIO
unicode, str, bytes, basestring = str, bytes, bytes, str
xrange = range
lrange = lambda *args: list(range(*args))
else:
raise NotImplementedError('welcome to the future, I guess. (report this)')
def make_sentinel(name='_MISSING', var_name=None):
class Sentinel(object):
def __init__(self):
self.name = name
self.var_name = var_name
def __repr__(self):
if self.var_name:
return self.var_name
return '%s(%r)' % (self.__class__.__name__, self.name)
if var_name:
def __reduce__(self):
return self.var_name
def __nonzero__(self):
return False
return Sentinel()

View File

@ -42,7 +42,7 @@ except ImportError:
izip = zip # Python 3
try:
from compat import make_sentinel
from typeutils import make_sentinel
_MISSING = make_sentinel(var_name='_MISSING')
except ImportError:
_MISSING = object()

View File

@ -16,7 +16,7 @@ from math import log as math_log
from itertools import chain, islice
try:
from compat import make_sentinel
from typeutils import make_sentinel
_MISSING = make_sentinel(var_name='_MISSING')
except ImportError:
_MISSING = object()

View File

@ -34,7 +34,7 @@ from bisect import insort
import itertools
try:
from compat import make_sentinel
from typeutils import make_sentinel
_REMOVED = make_sentinel(var_name='_REMOVED')
except ImportError:
_REMOVED = object()

View File

@ -18,7 +18,7 @@ from collections import MutableSet
import operator
try:
from compat import make_sentinel
from typeutils import make_sentinel
_MISSING = make_sentinel(var_name='_MISSING')
except ImportError:
_MISSING = object()

View File

@ -32,7 +32,7 @@ except:
string_types, integer_types = (str, bytes), (int,)
try:
from compat import make_sentinel
from typeutils import make_sentinel
_MISSING = make_sentinel(var_name='_MISSING')
except ImportError:
_MISSING = object()

View File

@ -10,6 +10,41 @@ from collections import deque
_issubclass = issubclass
def make_sentinel(name='_MISSING', var_name=None):
"""Creates and returns a new instance of a new class, suitable for
usage as a "sentinel", a kind of singleton often used to indicate
a value is missing when ``None`` is a valid input.
Args:
name (str): Name of the Sentinel, used in ``__repr__``s
var_name (str): Set this name to the name of the variable in
its respective module enable pickleability.
>>> make_sentinel(var_name='_MISSING')
_MISSING
The most common use cases here in boltons are as default values
for optional function arguments, partly because of its
less-confusing appearance in automatically generated
documentation. Sentinels also function well as placeholders in queues
and linked lists.
"""
class Sentinel(object):
def __init__(self):
self.name = name
self.var_name = var_name
def __repr__(self):
if self.var_name:
return self.var_name
return '%s(%r)' % (self.__class__.__name__, self.name)
if var_name:
def __reduce__(self):
return self.var_name
def __nonzero__(self):
return False
return Sentinel()
def issubclass(subclass, baseclass):
"""Just like the built-in :func:`issubclass`, this function checks
whether *subclass* is inherited from *baseclass*. Unlike the