summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/util/_collections.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sqlalchemy/util/_collections.py')
-rw-r--r--lib/sqlalchemy/util/_collections.py62
1 files changed, 31 insertions, 31 deletions
diff --git a/lib/sqlalchemy/util/_collections.py b/lib/sqlalchemy/util/_collections.py
index 4f9c5dc8a..269a3d539 100644
--- a/lib/sqlalchemy/util/_collections.py
+++ b/lib/sqlalchemy/util/_collections.py
@@ -18,9 +18,9 @@ EMPTY_SET = frozenset()
class NamedTuple(tuple):
"""tuple() subclass that adds labeled names.
-
+
Is also pickleable.
-
+
"""
def __new__(cls, vals, labels=None):
@@ -40,7 +40,7 @@ class ImmutableContainer(object):
__delitem__ = __setitem__ = __setattr__ = _immutable
class frozendict(ImmutableContainer, dict):
-
+
clear = pop = popitem = setdefault = \
update = ImmutableContainer._immutable
@@ -62,7 +62,7 @@ class frozendict(ImmutableContainer, dict):
d2 = frozendict(self)
dict.update(d2, d)
return d2
-
+
def __repr__(self):
return "frozendict(%s)" % dict.__repr__(self)
@@ -107,12 +107,12 @@ class Properties(object):
def __contains__(self, key):
return key in self._data
-
+
def as_immutable(self):
"""Return an immutable proxy for this :class:`.Properties`."""
-
+
return ImmutableProperties(self._data)
-
+
def update(self, value):
self._data.update(value)
@@ -136,12 +136,12 @@ class OrderedProperties(Properties):
as backing store."""
def __init__(self):
Properties.__init__(self, OrderedDict())
-
+
class ImmutableProperties(ImmutableContainer, Properties):
"""Provide immutable dict/object attribute to an underlying dictionary."""
-
-
+
+
class OrderedDict(dict):
"""A dict that returns keys/values/items in the order they were added."""
@@ -266,10 +266,10 @@ class OrderedSet(set):
def __iter__(self):
return iter(self._list)
-
+
def __add__(self, other):
return self.union(other)
-
+
def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, self._list)
@@ -340,11 +340,11 @@ class IdentitySet(object):
This strategy has edge cases for builtin types- it's possible to have
two 'foo' strings in one of these sets, for example. Use sparingly.
-
+
"""
_working_set = set
-
+
def __init__(self, iterable=None):
self._members = dict()
if iterable:
@@ -501,10 +501,10 @@ class IdentitySet(object):
result._members.update(
self._working_set(self._member_id_tuples()).symmetric_difference(_iter_id(iterable)))
return result
-
+
def _member_id_tuples(self):
return ((id(v), v) for v in self._members.itervalues())
-
+
def __xor__(self, other):
if not isinstance(other, IdentitySet):
return NotImplemented
@@ -544,7 +544,7 @@ class OrderedIdentitySet(IdentitySet):
# but it's safe here: IDS operates on (id, instance) tuples in the
# working set.
__sa_hash_exempt__ = True
-
+
def __init__(self, iterable=None):
IdentitySet.__init__(self)
self._members = OrderedDict()
@@ -564,7 +564,7 @@ if sys.version_info >= (2, 5):
def __init__(self, creator):
self.creator = creator
-
+
def __missing__(self, key):
self[key] = val = self.creator(key)
return val
@@ -574,7 +574,7 @@ else:
def __init__(self, creator):
self.creator = creator
-
+
def __getitem__(self, key):
try:
return dict.__getitem__(self, key)
@@ -652,13 +652,13 @@ def to_column_set(x):
def update_copy(d, _new=None, **kw):
"""Copy the given dict and update with the given values."""
-
+
d = d.copy()
if _new:
d.update(_new)
d.update(**kw)
return d
-
+
def flatten_iterator(x):
"""Given an iterator of which further sub-elements may also be
iterators, flatten the sub-elements into a single iterator.
@@ -748,7 +748,7 @@ class WeakIdentityMapping(weakref.WeakKeyDictionary):
del self.by_id[key]
except (KeyError, AttributeError): # pragma: no cover
pass # pragma: no cover
-
+
class _keyed_weakref(weakref.ref):
def __init__(self, object, callback):
weakref.ref.__init__(self, object, callback)
@@ -761,7 +761,7 @@ class WeakIdentityMapping(weakref.WeakKeyDictionary):
class LRUCache(dict):
"""Dictionary with 'squishy' removal of least
recently used items.
-
+
"""
def __init__(self, capacity=100, threshold=.5):
self.capacity = capacity
@@ -809,7 +809,7 @@ class LRUCache(dict):
class ScopedRegistry(object):
"""A Registry that can store one or multiple instances of a single
class on the basis of a "scope" function.
-
+
The object implements ``__call__`` as the "getter", so by
calling ``myregistry()`` the contained object is returned
for the current scope.
@@ -823,14 +823,14 @@ class ScopedRegistry(object):
def __init__(self, createfunc, scopefunc):
"""Construct a new :class:`.ScopedRegistry`.
-
+
:param createfunc: A creation function that will generate
a new value for the current scope, if none is present.
-
+
:param scopefunc: A function that returns a hashable
token representing the current scope (such as, current
thread identifier).
-
+
"""
self.createfunc = createfunc
self.scopefunc = scopefunc
@@ -845,17 +845,17 @@ class ScopedRegistry(object):
def has(self):
"""Return True if an object is present in the current scope."""
-
+
return self.scopefunc() in self.registry
def set(self, obj):
"""Set the value forthe current scope."""
-
+
self.registry[self.scopefunc()] = obj
def clear(self):
"""Clear the current scope, if any."""
-
+
try:
del self.registry[self.scopefunc()]
except KeyError:
@@ -864,7 +864,7 @@ class ScopedRegistry(object):
class ThreadLocalRegistry(ScopedRegistry):
"""A :class:`.ScopedRegistry` that uses a ``threading.local()``
variable for storage.
-
+
"""
def __init__(self, createfunc):
self.createfunc = createfunc