From 7e5baff5b998e02c39a7f64c079a0992651557ec Mon Sep 17 00:00:00 2001 From: Mahmoud Hashemi Date: Thu, 16 Apr 2015 02:18:01 -0700 Subject: [PATCH] moved make_sentinel into typeutils and deleted the confusing compat module --- boltons/cacheutils.py | 2 +- boltons/compat.py | 37 ------------------------------------- boltons/dictutils.py | 2 +- boltons/listutils.py | 2 +- boltons/queueutils.py | 2 +- boltons/setutils.py | 2 +- boltons/tableutils.py | 2 +- boltons/typeutils.py | 35 +++++++++++++++++++++++++++++++++++ 8 files changed, 41 insertions(+), 43 deletions(-) delete mode 100644 boltons/compat.py diff --git a/boltons/cacheutils.py b/boltons/cacheutils.py index 84d23d5..7aff49e 100644 --- a/boltons/cacheutils.py +++ b/boltons/cacheutils.py @@ -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: diff --git a/boltons/compat.py b/boltons/compat.py deleted file mode 100644 index 8362274..0000000 --- a/boltons/compat.py +++ /dev/null @@ -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() diff --git a/boltons/dictutils.py b/boltons/dictutils.py index 13d1e9b..40f69a7 100644 --- a/boltons/dictutils.py +++ b/boltons/dictutils.py @@ -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() diff --git a/boltons/listutils.py b/boltons/listutils.py index ce914c6..4810feb 100644 --- a/boltons/listutils.py +++ b/boltons/listutils.py @@ -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() diff --git a/boltons/queueutils.py b/boltons/queueutils.py index 5bb8e01..92fd9ba 100644 --- a/boltons/queueutils.py +++ b/boltons/queueutils.py @@ -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() diff --git a/boltons/setutils.py b/boltons/setutils.py index 5f51184..9f9d59c 100644 --- a/boltons/setutils.py +++ b/boltons/setutils.py @@ -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() diff --git a/boltons/tableutils.py b/boltons/tableutils.py index a8e523c..4811a8e 100644 --- a/boltons/tableutils.py +++ b/boltons/tableutils.py @@ -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() diff --git a/boltons/typeutils.py b/boltons/typeutils.py index 53f6b6a..d0c5c4d 100644 --- a/boltons/typeutils.py +++ b/boltons/typeutils.py @@ -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