diff options
| author | Diana Clarke <diana.joan.clarke@gmail.com> | 2012-11-19 11:32:00 -0500 |
|---|---|---|
| committer | Diana Clarke <diana.joan.clarke@gmail.com> | 2012-11-19 11:32:00 -0500 |
| commit | 6480e75a7c53db5fc31bbe87a1c68535caf61143 (patch) | |
| tree | 3abcc6d5e1c333f69cc10052436ab3db271523cd /lib/sqlalchemy/sql/visitors.py | |
| parent | 7bd1dbc4cf009c92f943a9f5313f99e8db604c41 (diff) | |
| download | sqlalchemy-6480e75a7c53db5fc31bbe87a1c68535caf61143.tar.gz | |
just a pep8 pass of lib/sqlalchemy/sql/
Diffstat (limited to 'lib/sqlalchemy/sql/visitors.py')
| -rw-r--r-- | lib/sqlalchemy/sql/visitors.py | 44 |
1 files changed, 34 insertions, 10 deletions
diff --git a/lib/sqlalchemy/sql/visitors.py b/lib/sqlalchemy/sql/visitors.py index 6f2c82992..09c50a934 100644 --- a/lib/sqlalchemy/sql/visitors.py +++ b/lib/sqlalchemy/sql/visitors.py @@ -24,7 +24,6 @@ http://techspot.zzzeek.org/2008/01/23/expression-transformations/ """ from collections import deque -import re from .. import util import operator @@ -33,6 +32,7 @@ __all__ = ['VisitableType', 'Visitable', 'ClauseVisitor', 'iterate_depthfirst', 'traverse_using', 'traverse', 'cloned_traverse', 'replacement_traverse'] + class VisitableType(type): """Metaclass which assigns a `_compiler_dispatch` method to classes having a `__visit_name__` attribute. @@ -43,7 +43,8 @@ class VisitableType(type): def _compiler_dispatch (self, visitor, **kw): '''Look for an attribute named "visit_" + self.__visit_name__ on the visitor, and call it with the same kw params.''' - return getattr(visitor, 'visit_%s' % self.__visit_name__)(self, **kw) + visit_attr = 'visit_%s' % self.__visit_name__ + return getattr(visitor, visit_attr)(self, **kw) Classes having no __visit_name__ attribute will remain unaffected. """ @@ -68,6 +69,7 @@ def _generate_dispatch(cls): # the string name of the class's __visit_name__ is known at # this early stage (import time) so it can be pre-constructed. getter = operator.attrgetter("visit_%s" % visit_name) + def _compiler_dispatch(self, visitor, **kw): return getter(visitor)(self, **kw) else: @@ -75,14 +77,16 @@ def _generate_dispatch(cls): # __visit_name__ is not yet a string. As a result, the visit # string has to be recalculated with each compilation. def _compiler_dispatch(self, visitor, **kw): - return getattr(visitor, 'visit_%s' % self.__visit_name__)(self, **kw) + visit_attr = 'visit_%s' % self.__visit_name__ + return getattr(visitor, visit_attr)(self, **kw) - _compiler_dispatch.__doc__ = \ + _compiler_dispatch.__doc__ = \ """Look for an attribute named "visit_" + self.__visit_name__ on the visitor, and call it with the same kw params. """ cls._compiler_dispatch = _compiler_dispatch + class Visitable(object): """Base class for visitable objects, applies the ``VisitableType`` metaclass. @@ -91,6 +95,7 @@ class Visitable(object): __metaclass__ = VisitableType + class ClauseVisitor(object): """Base class for visitor objects which can traverse using the traverse() function. @@ -106,8 +111,10 @@ class ClauseVisitor(object): return meth(obj, **kw) def iterate(self, obj): - """traverse the given expression structure, returning an iterator of all elements.""" + """traverse the given expression structure, returning an iterator + of all elements. + """ return iterate(obj, self.__traverse_options__) def traverse(self, obj): @@ -143,6 +150,7 @@ class ClauseVisitor(object): tail._next = visitor return self + class CloningVisitor(ClauseVisitor): """Base class for visitor objects which can traverse using the cloned_traverse() function. @@ -150,14 +158,18 @@ class CloningVisitor(ClauseVisitor): """ def copy_and_process(self, list_): - """Apply cloned traversal to the given list of elements, and return the new list.""" + """Apply cloned traversal to the given list of elements, and return + the new list. + """ return [self.traverse(x) for x in list_] def traverse(self, obj): """traverse and visit the given expression structure.""" - return cloned_traverse(obj, self.__traverse_options__, self._visitor_dict) + return cloned_traverse( + obj, self.__traverse_options__, self._visitor_dict) + class ReplacingCloningVisitor(CloningVisitor): """Base class for visitor objects which can traverse using @@ -184,6 +196,7 @@ class ReplacingCloningVisitor(CloningVisitor): return e return replacement_traverse(obj, self.__traverse_options__, replace) + def iterate(obj, opts): """traverse the given expression structure, returning an iterator. @@ -197,6 +210,7 @@ def iterate(obj, opts): for c in t.get_children(**opts): stack.append(c) + def iterate_depthfirst(obj, opts): """traverse the given expression structure, returning an iterator. @@ -212,25 +226,35 @@ def iterate_depthfirst(obj, opts): stack.append(c) return iter(traversal) + def traverse_using(iterator, obj, visitors): - """visit the given expression structure using the given iterator of objects.""" + """visit the given expression structure using the given iterator of + objects. + """ for target in iterator: meth = visitors.get(target.__visit_name__, None) if meth: meth(target) return obj + def traverse(obj, opts, visitors): - """traverse and visit the given expression structure using the default iterator.""" + """traverse and visit the given expression structure using the default + iterator. + """ return traverse_using(iterate(obj, opts), obj, visitors) + def traverse_depthfirst(obj, opts, visitors): - """traverse and visit the given expression structure using the depth-first iterator.""" + """traverse and visit the given expression structure using the + depth-first iterator. + """ return traverse_using(iterate_depthfirst(obj, opts), obj, visitors) + def cloned_traverse(obj, opts, visitors): """clone the given expression structure, allowing modifications by visitors.""" |
