# This module is part of SQLAlchemy and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
-import md5, sys, warnings, sets
+import sys, warnings, sets
import __builtin__
from sqlalchemy import exceptions, logging
if sys.version_info >= (2, 5):
class PopulateDict(dict):
"""a dict which populates missing values via a creation function.
-
+
note the creation function takes a key, unlike collections.defaultdict.
"""
-
+
def __init__(self, creator):
self.creator = creator
def __missing__(self, key):
else:
raise ValueError("String is not true/false: %r" % obj)
return bool(obj)
-
+
def coerce_kw_type(kw, key, type_, flexi_bool=True):
"""If 'key' is present in dict 'kw', coerce its value to type 'type_' if
necessary. If 'flexi_bool' is True, the string '0' is considered false
the basic collection types: list, set and dict. If the __emulates__
property is present, return that preferentially.
"""
-
+
if hasattr(specimen, '__emulates__'):
return specimen.__emulates__
return func(*args, **kwargs)
except:
warnings.warn(RuntimeWarning("%s('%s') ignored" % sys.exc_info()[0:2]))
-
+
class SimpleProperty(object):
"""A *default* property accessor."""
class NotImplProperty(object):
"""a property that raises ``NotImplementedError``."""
-
+
def __init__(self, doc):
self.__doc__ = doc
-
+
def __set__(self, obj, value):
raise NotImplementedError()
return self
else:
raise NotImplementedError()
-
+
class OrderedProperties(object):
"""An object that maintains the order in which attributes are set upon it.
def __getstate__(self):
return {'_data': self.__dict__['_data']}
-
+
def __setstate__(self, state):
self.__dict__['_data'] = state['_data']
return dict.__getitem__(self, key)
except KeyError:
return self.decorate[key]
-
+
def __contains__(self, key):
return dict.__contains__(self, key) or key in self.decorate
-
+
def has_key(self, key):
return key in self
-
+
def __repr__(self):
return dict.__repr__(self) + repr(self.decorate)
def __iter__(self):
return iter(self._list)
+ def __repr__(self):
+ return '%s(%r)' % (self.__class__.__name__, self._list)
+
+ __str__ = __repr__
+
def update(self, iterable):
add = self.add
for i in iterable:
add(i)
return self
- def __repr__(self):
- return '%s(%r)' % (self.__class__.__name__, self._list)
-
- __str__ = __repr__
+ __ior__ = update
def union(self, other):
result = self.__class__(self)
__sub__ = difference
- __ior__ = update
-
def intersection_update(self, other):
Set.intersection_update(self, other)
self._list = [ a for a in self._list if a in other]
class UniqueAppender(object):
"""appends items to a collection such that only unique items
are added."""
-
+
def __init__(self, data, via=None):
self.data = data
self._unique = Set()
elif hasattr(data, 'add'):
# TODO: we think its a set here. bypass unneeded uniquing logic ?
self._data_appender = data.add
-
+
def append(self, item):
if item not in self._unique:
self._data_appender(item)
self._unique.add(item)
-
+
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.
return self.registry[key]
except KeyError:
return self.registry.setdefault(key, self.createfunc())
-
+
def has(self):
return self._get_key() in self.registry
-
+
def set(self, obj):
self.registry[self._get_key()] = obj