summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/util
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sqlalchemy/util')
-rw-r--r--lib/sqlalchemy/util/__init__.py2
-rw-r--r--lib/sqlalchemy/util/_collections.py62
-rw-r--r--lib/sqlalchemy/util/compat.py4
-rw-r--r--lib/sqlalchemy/util/deprecations.py8
-rw-r--r--lib/sqlalchemy/util/langhelpers.py58
-rw-r--r--lib/sqlalchemy/util/topological.py6
6 files changed, 70 insertions, 70 deletions
diff --git a/lib/sqlalchemy/util/__init__.py b/lib/sqlalchemy/util/__init__.py
index 9ce223939..6950aa8e6 100644
--- a/lib/sqlalchemy/util/__init__.py
+++ b/lib/sqlalchemy/util/__init__.py
@@ -15,7 +15,7 @@ from _collections import NamedTuple, ImmutableContainer, frozendict, \
UniqueAppender, PopulateDict, EMPTY_SET, to_list, to_set, \
to_column_set, update_copy, flatten_iterator, WeakIdentityMapping, \
LRUCache, ScopedRegistry, ThreadLocalRegistry
-
+
from langhelpers import iterate_attributes, class_hierarchy, \
portable_instancemethod, unbound_method_to_callable, \
getargspec_init, format_argspec_init, format_argspec_plus, \
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
diff --git a/lib/sqlalchemy/util/compat.py b/lib/sqlalchemy/util/compat.py
index fb2a14633..6b6051973 100644
--- a/lib/sqlalchemy/util/compat.py
+++ b/lib/sqlalchemy/util/compat.py
@@ -52,7 +52,7 @@ else:
# a controversial feature, required by MySQLdb currently
def buffer(x):
return x
-
+
# Py2K
buffer = getattr(__builtin__, 'buffer', buffer)
# end Py2K
@@ -136,7 +136,7 @@ except ImportError:
class _probe(dict):
def __missing__(self, key):
return 1
-
+
try:
try:
_probe()['missing']
diff --git a/lib/sqlalchemy/util/deprecations.py b/lib/sqlalchemy/util/deprecations.py
index 532594b72..d9018a26d 100644
--- a/lib/sqlalchemy/util/deprecations.py
+++ b/lib/sqlalchemy/util/deprecations.py
@@ -73,7 +73,7 @@ def pending_deprecation(version, message=None,
if message is None:
message = "Call to deprecated function %(func)s"
-
+
def decorate(fn):
return _decorate_with_warning(
fn, exc.SAPendingDeprecationWarning,
@@ -87,13 +87,13 @@ def _sanitize_restructured_text(text):
name += "()"
return name
return re.sub(r'\:(\w+)\:`~?\.?(.+?)`', repl, text)
-
-
+
+
def _decorate_with_warning(func, wtype, message, docstring_header=None):
"""Wrap a function with a warnings.warn and augmented docstring."""
message = _sanitize_restructured_text(message)
-
+
@decorator
def warned(fn, *args, **kwargs):
warnings.warn(wtype(message), stacklevel=3)
diff --git a/lib/sqlalchemy/util/langhelpers.py b/lib/sqlalchemy/util/langhelpers.py
index 38260cd66..4ac78bd16 100644
--- a/lib/sqlalchemy/util/langhelpers.py
+++ b/lib/sqlalchemy/util/langhelpers.py
@@ -59,10 +59,10 @@ def get_cls_kwargs(cls):
__init__ defines a \**kwargs catch-all, then the constructor is presumed to
pass along unrecognized keywords to it's base classes, and the collection
process is repeated recursively on each of the bases.
-
+
Uses a subset of inspect.getargspec() to cut down on method overhead.
No anonymous tuple arguments please !
-
+
"""
for c in cls.__mro__:
@@ -79,10 +79,10 @@ def get_cls_kwargs(cls):
if not ctr or not isinstance(ctr, types.FunctionType):
stack.update(class_.__bases__)
continue
-
+
# this is shorthand for
# names, _, has_kw, _ = inspect.getargspec(ctr)
-
+
names, has_kw = inspect_func_args(ctr)
args.update(names)
if has_kw:
@@ -106,12 +106,12 @@ except ImportError:
def get_func_kwargs(func):
"""Return the set of legal kwargs for the given `func`.
-
+
Uses getargspec so is safe to call for methods, functions,
etc.
-
+
"""
-
+
return inspect.getargspec(func)[0]
def format_argspec_plus(fn, grouped=True):
@@ -203,7 +203,7 @@ def getargspec_init(method):
else:
return (['self'], 'args', 'kwargs', None)
-
+
def unbound_method_to_callable(func_or_cls):
"""Adjust the incoming callable such that a 'self' argument is not required."""
@@ -215,7 +215,7 @@ def unbound_method_to_callable(func_or_cls):
class portable_instancemethod(object):
"""Turn an instancemethod into a (parent, name) pair
to produce a serializable callable.
-
+
"""
def __init__(self, meth):
self.target = meth.im_self
@@ -223,7 +223,7 @@ class portable_instancemethod(object):
def __call__(self, *arg, **kw):
return getattr(self.target, self.name)(*arg, **kw)
-
+
def class_hierarchy(cls):
"""Return an unordered sequence of all classes related to cls.
@@ -468,22 +468,22 @@ class group_expirable_memoized_property(object):
class importlater(object):
"""Deferred import object.
-
+
e.g.::
-
+
somesubmod = importlater("mypackage.somemodule", "somesubmod")
-
+
is equivalent to::
-
+
from mypackage.somemodule import somesubmod
-
+
except evaluted upon attribute access to "somesubmod".
-
+
"""
def __init__(self, path, addtl=None):
self._il_path = path
self._il_addtl = addtl
-
+
@memoized_property
def module(self):
if self._il_addtl:
@@ -501,7 +501,7 @@ class importlater(object):
for token in self._il_path.split(".")[1:]:
m = getattr(m, token)
return m
-
+
def __getattr__(self, key):
try:
attr = getattr(self.module, key)
@@ -528,7 +528,7 @@ def asbool(obj):
def bool_or_str(*text):
"""Return a callable that will evaulate a string as
boolean, or one of a set of "alternate" string values.
-
+
"""
def bool_or_value(obj):
if obj in text:
@@ -536,7 +536,7 @@ def bool_or_str(*text):
else:
return asbool(obj)
return bool_or_value
-
+
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
@@ -552,11 +552,11 @@ def coerce_kw_type(kw, key, type_, flexi_bool=True):
def constructor_copy(obj, cls, **kw):
"""Instantiate cls using the __dict__ of obj as constructor arguments.
-
+
Uses inspect to match the named arguments of ``cls``.
-
+
"""
-
+
names = get_cls_kwargs(cls)
kw.update((k, obj.__dict__[k]) for k in names if k in obj.__dict__)
return cls(**kw)
@@ -645,13 +645,13 @@ class classproperty(property):
module, but note that the
:class:`~.sqlalchemy.ext.declarative.declared_attr`
decorator should be used for this purpose with declarative.
-
+
"""
-
+
def __init__(self, fget, *arg, **kw):
super(classproperty, self).__init__(fget, *arg, **kw)
self.__doc__ = fget.__doc__
-
+
def __get__(desc, self, cls):
return desc.fget(cls)
@@ -719,15 +719,15 @@ def warn_exception(func, *args, **kwargs):
def warn(msg, stacklevel=3):
"""Issue a warning.
-
+
If msg is a string, :class:`.exc.SAWarning` is used as
the category.
-
+
.. note:: This function is swapped out when the test suite
runs, with a compatible version that uses
warnings.warn_explicit, so that the warnings registry can
be controlled.
-
+
"""
if isinstance(msg, basestring):
warnings.warn(msg, exc.SAWarning, stacklevel=stacklevel)
diff --git a/lib/sqlalchemy/util/topological.py b/lib/sqlalchemy/util/topological.py
index aeb212d4d..8f3406472 100644
--- a/lib/sqlalchemy/util/topological.py
+++ b/lib/sqlalchemy/util/topological.py
@@ -17,7 +17,7 @@ def sort_as_subsets(tuples, allitems):
edges = util.defaultdict(set)
for parent, child in tuples:
edges[child].add(parent)
-
+
todo = set(allitems)
while todo:
@@ -55,7 +55,7 @@ def find_cycles(tuples, allitems):
edges[parent].add(child)
output = set()
-
+
while todo:
node = todo.pop()
stack = [node]
@@ -66,7 +66,7 @@ def find_cycles(tuples, allitems):
cyc = stack[stack.index(node):]
todo.difference_update(cyc)
output.update(cyc)
-
+
if node in todo:
stack.append(node)
todo.remove(node)