summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql/util.py
diff options
context:
space:
mode:
authorBrian Jarrett <celttechie@gmail.com>2014-07-20 12:44:40 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2014-07-20 12:44:40 -0400
commitcca03097f47f22783d42d1853faac6cf84607c5a (patch)
tree4fe1a63d03a2d88d1cf37e1167759dfaf84f4ce7 /lib/sqlalchemy/sql/util.py
parent827329a0cca5351094a1a86b6b2be2b9182f0ae2 (diff)
downloadsqlalchemy-cca03097f47f22783d42d1853faac6cf84607c5a.tar.gz
- apply pep8 formatting to sqlalchemy/sql, sqlalchemy/util, sqlalchemy/dialects,
sqlalchemy/orm, sqlalchemy/event, sqlalchemy/testing
Diffstat (limited to 'lib/sqlalchemy/sql/util.py')
-rw-r--r--lib/sqlalchemy/sql/util.py116
1 files changed, 60 insertions, 56 deletions
diff --git a/lib/sqlalchemy/sql/util.py b/lib/sqlalchemy/sql/util.py
index b4ed7a5f3..8bbae8b93 100644
--- a/lib/sqlalchemy/sql/util.py
+++ b/lib/sqlalchemy/sql/util.py
@@ -16,13 +16,13 @@ from itertools import chain
from collections import deque
from .elements import BindParameter, ColumnClause, ColumnElement, \
- Null, UnaryExpression, literal_column, Label
+ Null, UnaryExpression, literal_column, Label
from .selectable import ScalarSelect, Join, FromClause, FromGrouping
from .schema import Column
join_condition = util.langhelpers.public_factory(
- Join._join_condition,
- ".sql.util.join_condition")
+ Join._join_condition,
+ ".sql.util.join_condition")
# names that are still being imported from the outside
from .annotation import _shallow_annotate, _deep_annotate, _deep_deannotate
@@ -99,7 +99,7 @@ def visit_binary_product(fn, expr):
# those are just column elements by themselves
yield element
elif element.__visit_name__ == 'binary' and \
- operators.is_comparison(element.operator):
+ operators.is_comparison(element.operator):
stack.insert(0, element)
for l in visit(element.left):
for r in visit(element.right):
@@ -135,7 +135,7 @@ def find_tables(clause, check_columns=False,
if include_crud:
_visitors['insert'] = _visitors['update'] = \
- _visitors['delete'] = lambda ent: tables.append(ent.table)
+ _visitors['delete'] = lambda ent: tables.append(ent.table)
if check_columns:
def visit_column(column):
@@ -148,7 +148,6 @@ def find_tables(clause, check_columns=False,
return tables
-
def unwrap_order_by(clause):
"""Break up an 'order by' expression into individual column-expressions,
without DESC/ASC/NULLS FIRST/NULLS LAST"""
@@ -159,9 +158,9 @@ def unwrap_order_by(clause):
t = stack.popleft()
if isinstance(t, ColumnElement) and \
(
- not isinstance(t, UnaryExpression) or \
+ not isinstance(t, UnaryExpression) or
not operators.is_ordering_modifier(t.modifier)
- ):
+ ):
cols.add(t)
else:
for c in t.get_children():
@@ -184,6 +183,7 @@ def clause_is_present(clause, search):
else:
return False
+
def surface_selectables(clause):
stack = [clause]
while stack:
@@ -194,14 +194,16 @@ def surface_selectables(clause):
elif isinstance(elem, FromGrouping):
stack.append(elem.element)
+
def selectables_overlap(left, right):
"""Return True if left/right have some overlapping selectable"""
return bool(
- set(surface_selectables(left)).intersection(
- surface_selectables(right)
- )
- )
+ set(surface_selectables(left)).intersection(
+ surface_selectables(right)
+ )
+ )
+
def bind_values(clause):
"""Return an ordered list of "bound" values in the given clause.
@@ -237,26 +239,25 @@ class _repr_params(object):
display to the given number of 'multi' parameter sets.
"""
+
def __init__(self, params, batches):
self.params = params
self.batches = batches
def __repr__(self):
if isinstance(self.params, (list, tuple)) and \
- len(self.params) > self.batches and \
- isinstance(self.params[0], (list, dict, tuple)):
+ len(self.params) > self.batches and \
+ isinstance(self.params[0], (list, dict, tuple)):
msg = " ... displaying %i of %i total bound parameter sets ... "
return ' '.join((
- repr(self.params[:self.batches - 2])[0:-1],
- msg % (self.batches, len(self.params)),
- repr(self.params[-2:])[1:]
- ))
+ repr(self.params[:self.batches - 2])[0:-1],
+ msg % (self.batches, len(self.params)),
+ repr(self.params[-2:])[1:]
+ ))
else:
return repr(self.params)
-
-
def adapt_criterion_to_null(crit, nulls):
"""given criterion containing bind params, convert selected elements
to IS NULL.
@@ -265,14 +266,14 @@ def adapt_criterion_to_null(crit, nulls):
def visit_binary(binary):
if isinstance(binary.left, BindParameter) \
- and binary.left._identifying_key in nulls:
+ and binary.left._identifying_key in nulls:
# reverse order if the NULL is on the left side
binary.left = binary.right
binary.right = Null()
binary.operator = operators.is_
binary.negate = operators.isnot
elif isinstance(binary.right, BindParameter) \
- and binary.right._identifying_key in nulls:
+ and binary.right._identifying_key in nulls:
binary.right = Null()
binary.operator = operators.is_
binary.negate = operators.isnot
@@ -320,8 +321,8 @@ def reduce_columns(columns, *clauses, **kw):
\**kw may specify 'ignore_nonexistent_tables' to ignore foreign keys
whose tables are not yet configured, or columns that aren't yet present.
- This function is primarily used to determine the most minimal "primary key"
- from a selectable, by reducing the set of primary key columns present
+ This function is primarily used to determine the most minimal "primary
+ key" from a selectable, by reducing the set of primary key columns present
in the selectable to just those that are not repeated.
"""
@@ -353,21 +354,21 @@ def reduce_columns(columns, *clauses, **kw):
else:
raise
if fk_col.shares_lineage(c) and \
- (not only_synonyms or \
- c.name == col.name):
+ (not only_synonyms or
+ c.name == col.name):
omit.add(col)
break
if clauses:
def visit_binary(binary):
if binary.operator == operators.eq:
- cols = util.column_set(chain(*[c.proxy_set
- for c in columns.difference(omit)]))
+ cols = util.column_set(
+ chain(*[c.proxy_set for c in columns.difference(omit)]))
if binary.left in cols and binary.right in cols:
for c in reversed(columns):
if c.shares_lineage(binary.right) and \
- (not only_synonyms or \
- c.name == binary.left.name):
+ (not only_synonyms or
+ c.name == binary.left.name):
omit.add(c)
break
for clause in clauses:
@@ -378,7 +379,7 @@ def reduce_columns(columns, *clauses, **kw):
def criterion_as_pairs(expression, consider_as_foreign_keys=None,
- consider_as_referenced_keys=None, any_operator=False):
+ consider_as_referenced_keys=None, any_operator=False):
"""traverse an expression and locate binary criterion pairs."""
if consider_as_foreign_keys and consider_as_referenced_keys:
@@ -387,37 +388,37 @@ def criterion_as_pairs(expression, consider_as_foreign_keys=None,
"'consider_as_referenced_keys'")
def col_is(a, b):
- #return a is b
+ # return a is b
return a.compare(b)
def visit_binary(binary):
if not any_operator and binary.operator is not operators.eq:
return
if not isinstance(binary.left, ColumnElement) or \
- not isinstance(binary.right, ColumnElement):
+ not isinstance(binary.right, ColumnElement):
return
if consider_as_foreign_keys:
if binary.left in consider_as_foreign_keys and \
- (col_is(binary.right, binary.left) or
- binary.right not in consider_as_foreign_keys):
+ (col_is(binary.right, binary.left) or
+ binary.right not in consider_as_foreign_keys):
pairs.append((binary.right, binary.left))
elif binary.right in consider_as_foreign_keys and \
- (col_is(binary.left, binary.right) or
- binary.left not in consider_as_foreign_keys):
+ (col_is(binary.left, binary.right) or
+ binary.left not in consider_as_foreign_keys):
pairs.append((binary.left, binary.right))
elif consider_as_referenced_keys:
if binary.left in consider_as_referenced_keys and \
- (col_is(binary.right, binary.left) or
- binary.right not in consider_as_referenced_keys):
+ (col_is(binary.right, binary.left) or
+ binary.right not in consider_as_referenced_keys):
pairs.append((binary.left, binary.right))
elif binary.right in consider_as_referenced_keys and \
- (col_is(binary.left, binary.right) or
- binary.left not in consider_as_referenced_keys):
+ (col_is(binary.left, binary.right) or
+ binary.left not in consider_as_referenced_keys):
pairs.append((binary.right, binary.left))
else:
if isinstance(binary.left, Column) and \
- isinstance(binary.right, Column):
+ isinstance(binary.right, Column):
if binary.left.references(binary.right):
pairs.append((binary.right, binary.left))
elif binary.right.references(binary.left):
@@ -427,7 +428,6 @@ def criterion_as_pairs(expression, consider_as_foreign_keys=None,
return pairs
-
class AliasedRow(object):
"""Wrap a RowProxy with a translation map.
@@ -435,6 +435,7 @@ class AliasedRow(object):
to those present in a RowProxy.
"""
+
def __init__(self, row, map):
# AliasedRow objects don't nest, so un-nest
# if another AliasedRow was passed
@@ -483,10 +484,11 @@ class ClauseAdapter(visitors.ReplacingCloningVisitor):
s.c.col1 == table2.c.col1
"""
+
def __init__(self, selectable, equivalents=None,
- include=None, exclude=None,
- include_fn=None, exclude_fn=None,
- adapt_on_names=False):
+ include=None, exclude=None,
+ include_fn=None, exclude_fn=None,
+ adapt_on_names=False):
self.__traverse_options__ = {'stop_on': [selectable]}
self.selectable = selectable
if include:
@@ -505,13 +507,13 @@ class ClauseAdapter(visitors.ReplacingCloningVisitor):
def _corresponding_column(self, col, require_embedded,
_seen=util.EMPTY_SET):
newcol = self.selectable.corresponding_column(
- col,
- require_embedded=require_embedded)
+ col,
+ require_embedded=require_embedded)
if newcol is None and col in self.equivalents and col not in _seen:
for equiv in self.equivalents[col]:
- newcol = self._corresponding_column(equiv,
- require_embedded=require_embedded,
- _seen=_seen.union([col]))
+ newcol = self._corresponding_column(
+ equiv, require_embedded=require_embedded,
+ _seen=_seen.union([col]))
if newcol is not None:
return newcol
if self.adapt_on_names and newcol is None:
@@ -519,9 +521,10 @@ class ClauseAdapter(visitors.ReplacingCloningVisitor):
return newcol
magic_flag = False
+
def replace(self, col):
if not self.magic_flag and isinstance(col, FromClause) and \
- self.selectable.is_derived_from(col):
+ self.selectable.is_derived_from(col):
return self.selectable
elif not isinstance(col, ColumnElement):
return None
@@ -542,10 +545,12 @@ class ColumnAdapter(ClauseAdapter):
adapted_row() factory.
"""
+
def __init__(self, selectable, equivalents=None,
- chain_to=None, include=None,
- exclude=None, adapt_required=False):
- ClauseAdapter.__init__(self, selectable, equivalents, include, exclude)
+ chain_to=None, include=None,
+ exclude=None, adapt_required=False):
+ ClauseAdapter.__init__(self, selectable, equivalents,
+ include, exclude)
if chain_to:
self.chain(chain_to)
self.columns = util.populate_column_dict(self._locate_col)
@@ -599,4 +604,3 @@ class ColumnAdapter(ClauseAdapter):
def __setstate__(self, state):
self.__dict__.update(state)
self.columns = util.PopulateDict(self._locate_col)
-