if sys.version_info >= (2, 5):
class PopulateDict(dict):
- """a dict which populates missing values via a creation function.
+ """A dict which populates missing values via a creation function.
+
+ Note the creation function takes a key, unlike
+ collections.defaultdict.
- note the creation function takes a key, unlike collections.defaultdict.
"""
def __init__(self, creator):
return val
else:
class PopulateDict(dict):
- """a dict which populates missing values via a creation function."""
+ """A dict which populates missing values via a creation function."""
def __init__(self, creator):
self.creator = creator
*args for the decorated method.
"""
-
def starargs_as_list(self, *args, **kwargs):
if isinstance(args, basestring) or (len(args) == 1 and not isinstance(args[0], tuple)):
return fn(self, *to_list(args[0], []), **kwargs)
*args for the decorated function.
"""
-
def starargs_as_list(*args, **kwargs):
if isinstance(args, basestring) or (len(args) == 1 and not isinstance(args[0], tuple)):
return fn(*to_list(args[0], []), **kwargs)
if sys.version_info >= (2, 5):
def decode_slice(slc):
"""decode a slice object as sent to __getitem__.
-
+
takes into account the 2.5 __index__() method, basically.
-
+
"""
ret = []
for x in slc.start, slc.stop, slc.step:
else:
def decode_slice(slc):
return (slc.start, slc.stop, slc.step)
-
-
+
def flatten_iterator(x):
"""Given an iterator of which further sub-elements may also be
iterators, flatten the sub-elements into a single iterator.
_creation_order = 1
def set_creation_order(instance):
- """assign a '_creation_order' sequence to the given instance.
-
- This allows multiple instances to be sorted in order of
- creation (typically within a single thread; the counter is
- not particularly threadsafe).
-
+ """Assign a '_creation_order' sequence to the given instance.
+
+ This allows multiple instances to be sorted in order of creation
+ (typically within a single thread; the counter is not particularly
+ threadsafe).
+
"""
global _creation_order
instance._creation_order = _creation_order
_creation_order +=1
-
+
def warn_exception(func, *args, **kwargs):
"""executes the given function, catches all exceptions and converts to a warning."""
try:
exec py in env
setattr(into_cls, method, env[method])
+
class SimpleProperty(object):
"""A *default* property accessor."""
_working_set = set
def __init__(self, iterable=None):
- self._members = _IterableUpdatableDict()
+ self._members = dict()
if iterable:
for o in iterable:
self.add(o)
def __repr__(self):
return '%s(%r)' % (type(self).__name__, self._members.values())
-if sys.version_info >= (2, 4):
- _IterableUpdatableDict = dict
-else:
- class _IterableUpdatableDict(dict):
- """A dict that can update(iterable) like Python 2.4+'s dict."""
- def update(self, __iterable=None, **kw):
- if __iterable is not None:
- if not isinstance(__iterable, dict):
- __iterable = dict(__iterable)
- dict.update(self, __iterable)
- if kw:
- dict.update(self, **kw)
def _iter_id(iterable):
"""Generator: ((id(o), o) for o in iterable)."""
for item in iterable:
yield id(item), item
-
class OrderedIdentitySet(IdentitySet):
class _working_set(OrderedSet):
# a testing pragma: exempt the OIDS working set from the test suite's
for o in iterable:
self.add(o)
+
class UniqueAppender(object):
"""Only adds items to a collection once.
def __iter__(self):
return iter(self.data)
+
class ScopedRegistry(object):
"""A Registry that can store one or multiple instances of a single
class on a per-thread scoped basis, or on a customized scope.
def _get_key(self):
return self.scopefunc()
+
class WeakCompositeKey(object):
"""an weak-referencable, hashable collection which is strongly referenced
until any one of its members is garbage collected.
def __iter__(self):
return iter(arg() for arg in self.args)
+
class _symbol(object):
def __init__(self, name):
"""Construct a new named symbol."""
return "<symbol '%s>" % self.name
_symbol.__name__ = 'symbol'
+
class symbol(object):
"""A constant symbol.
finally:
symbol._lock.release()
+
def as_interface(obj, cls=None, methods=None, required=None):
"""Ensure basic interface compliance for an instance or dict of callables.