diff options
author | Brian Jarrett <celttechie@gmail.com> | 2014-07-20 12:44:40 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-07-20 12:44:40 -0400 |
commit | cca03097f47f22783d42d1853faac6cf84607c5a (patch) | |
tree | 4fe1a63d03a2d88d1cf37e1167759dfaf84f4ce7 /lib/sqlalchemy/sql/base.py | |
parent | 827329a0cca5351094a1a86b6b2be2b9182f0ae2 (diff) | |
download | sqlalchemy-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/base.py')
-rw-r--r-- | lib/sqlalchemy/sql/base.py | 109 |
1 files changed, 62 insertions, 47 deletions
diff --git a/lib/sqlalchemy/sql/base.py b/lib/sqlalchemy/sql/base.py index 1e02b3fb0..5358d95b5 100644 --- a/lib/sqlalchemy/sql/base.py +++ b/lib/sqlalchemy/sql/base.py @@ -19,6 +19,7 @@ import collections PARSE_AUTOCOMMIT = util.symbol('PARSE_AUTOCOMMIT') NO_ARG = util.symbol('NO_ARG') + class Immutable(object): """mark a ClauseElement as 'immutable' when expressions are cloned.""" @@ -32,10 +33,10 @@ class Immutable(object): return self - def _from_objects(*elements): return itertools.chain(*[element._from_objects for element in elements]) + @util.decorator def _generative(fn, *args, **kw): """Mark a method as generative.""" @@ -50,6 +51,7 @@ class _DialectArgView(collections.MutableMapping): <dialectname>_<argument_name>. """ + def __init__(self, obj): self.obj = obj @@ -76,7 +78,7 @@ class _DialectArgView(collections.MutableMapping): dialect, value_key = self._key(key) except KeyError: raise exc.ArgumentError( - "Keys must be of the form <dialectname>_<argname>") + "Keys must be of the form <dialectname>_<argname>") else: self.obj.dialect_options[dialect][value_key] = value @@ -86,15 +88,17 @@ class _DialectArgView(collections.MutableMapping): def __len__(self): return sum(len(args._non_defaults) for args in - self.obj.dialect_options.values()) + self.obj.dialect_options.values()) def __iter__(self): return ( util.safe_kwarg("%s_%s" % (dialect_name, value_name)) for dialect_name in self.obj.dialect_options - for value_name in self.obj.dialect_options[dialect_name]._non_defaults + for value_name in + self.obj.dialect_options[dialect_name]._non_defaults ) + class _DialectArgDict(collections.MutableMapping): """A dictionary view of dialect-level arguments for a specific dialect. @@ -103,6 +107,7 @@ class _DialectArgDict(collections.MutableMapping): and dialect-specified default arguments. """ + def __init__(self): self._non_defaults = {} self._defaults = {} @@ -150,24 +155,26 @@ class DialectKWArgs(object): some_index = Index('a', 'b', mydialect_length=5) The :meth:`.DialectKWArgs.argument_for` method is a per-argument - way adding extra arguments to the :attr:`.DefaultDialect.construct_arguments` - dictionary. This dictionary provides a list of argument names accepted by - various schema-level constructs on behalf of a dialect. + way adding extra arguments to the + :attr:`.DefaultDialect.construct_arguments` dictionary. This + dictionary provides a list of argument names accepted by various + schema-level constructs on behalf of a dialect. - New dialects should typically specify this dictionary all at once as a data - member of the dialect class. The use case for ad-hoc addition of + New dialects should typically specify this dictionary all at once as a + data member of the dialect class. The use case for ad-hoc addition of argument names is typically for end-user code that is also using a custom compilation scheme which consumes the additional arguments. - :param dialect_name: name of a dialect. The dialect must be locatable, - else a :class:`.NoSuchModuleError` is raised. The dialect must - also include an existing :attr:`.DefaultDialect.construct_arguments` collection, - indicating that it participates in the keyword-argument validation and - default system, else :class:`.ArgumentError` is raised. - If the dialect does not include this collection, then any keyword argument - can be specified on behalf of this dialect already. All dialects - packaged within SQLAlchemy include this collection, however for third - party dialects, support may vary. + :param dialect_name: name of a dialect. The dialect must be + locatable, else a :class:`.NoSuchModuleError` is raised. The + dialect must also include an existing + :attr:`.DefaultDialect.construct_arguments` collection, indicating + that it participates in the keyword-argument validation and default + system, else :class:`.ArgumentError` is raised. If the dialect does + not include this collection, then any keyword argument can be + specified on behalf of this dialect already. All dialects packaged + within SQLAlchemy include this collection, however for third party + dialects, support may vary. :param argument_name: name of the parameter. @@ -179,9 +186,10 @@ class DialectKWArgs(object): construct_arg_dictionary = DialectKWArgs._kw_registry[dialect_name] if construct_arg_dictionary is None: - raise exc.ArgumentError("Dialect '%s' does have keyword-argument " - "validation and defaults enabled configured" % - dialect_name) + raise exc.ArgumentError( + "Dialect '%s' does have keyword-argument " + "validation and defaults enabled configured" % + dialect_name) if cls not in construct_arg_dictionary: construct_arg_dictionary[cls] = {} construct_arg_dictionary[cls][argument_name] = default @@ -243,8 +251,8 @@ class DialectKWArgs(object): options to this construct. This is a two-level nested registry, keyed to ``<dialect_name>`` - and ``<argument_name>``. For example, the ``postgresql_where`` argument - would be locatable as:: + and ``<argument_name>``. For example, the ``postgresql_where`` + argument would be locatable as:: arg = my_object.dialect_options['postgresql']['where'] @@ -257,8 +265,8 @@ class DialectKWArgs(object): """ return util.PopulateDict( - util.portable_instancemethod(self._kw_reg_for_dialect_cls) - ) + util.portable_instancemethod(self._kw_reg_for_dialect_cls) + ) def _validate_dialect_kwargs(self, kwargs): # validate remaining kwargs that they all specify DB prefixes @@ -269,29 +277,30 @@ class DialectKWArgs(object): for k in kwargs: m = re.match('^(.+?)_(.+)$', k) if not m: - raise TypeError("Additional arguments should be " - "named <dialectname>_<argument>, got '%s'" % k) + raise TypeError( + "Additional arguments should be " + "named <dialectname>_<argument>, got '%s'" % k) dialect_name, arg_name = m.group(1, 2) try: construct_arg_dictionary = self.dialect_options[dialect_name] except exc.NoSuchModuleError: util.warn( - "Can't validate argument %r; can't " - "locate any SQLAlchemy dialect named %r" % - (k, dialect_name)) + "Can't validate argument %r; can't " + "locate any SQLAlchemy dialect named %r" % + (k, dialect_name)) self.dialect_options[dialect_name] = d = _DialectArgDict() d._defaults.update({"*": None}) d._non_defaults[arg_name] = kwargs[k] else: if "*" not in construct_arg_dictionary and \ - arg_name not in construct_arg_dictionary: + arg_name not in construct_arg_dictionary: raise exc.ArgumentError( - "Argument %r is not accepted by " - "dialect %r on behalf of %r" % ( - k, - dialect_name, self.__class__ - )) + "Argument %r is not accepted by " + "dialect %r on behalf of %r" % ( + k, + dialect_name, self.__class__ + )) else: construct_arg_dictionary[arg_name] = kwargs[k] @@ -424,11 +433,13 @@ class SchemaEventTarget(object): self._set_parent(parent) self.dispatch.after_parent_attach(self, parent) + class SchemaVisitor(ClauseVisitor): """Define the visiting for ``SchemaItem`` objects.""" __traverse_options__ = {'schema_visitor': True} + class ColumnCollection(util.OrderedProperties): """An ordered dictionary that stores a list of ColumnElement instances. @@ -478,11 +489,10 @@ class ColumnCollection(util.OrderedProperties): self._data[column.key] = column if remove_col is not None: self._all_columns[:] = [column if c is remove_col - else c for c in self._all_columns] + else c for c in self._all_columns] else: self._all_columns.append(column) - def add(self, column): """Add a column to this collection. @@ -492,7 +502,7 @@ class ColumnCollection(util.OrderedProperties): """ if not column.key: raise exc.ArgumentError( - "Can't add unnamed column to column collection") + "Can't add unnamed column to column collection") self[column.key] = column def __delitem__(self, key): @@ -512,8 +522,8 @@ class ColumnCollection(util.OrderedProperties): if not existing.shares_lineage(value): util.warn('Column %r on table %r being replaced by ' '%r, which has the same key. Consider ' - 'use_labels for select() statements.' % (key, - getattr(existing, 'table', None), value)) + 'use_labels for select() statements.' % + (key, getattr(existing, 'table', None), value)) # pop out memoized proxy_set as this # operation may very well be occurring @@ -530,17 +540,20 @@ class ColumnCollection(util.OrderedProperties): def remove(self, column): del self._data[column.key] self._all_col_set.remove(column) - self._all_columns[:] = [c for c in self._all_columns if c is not column] + self._all_columns[:] = [ + c for c in self._all_columns if c is not column] def update(self, iter): cols = list(iter) - self._all_columns.extend(c for label, c in cols if c not in self._all_col_set) + self._all_columns.extend( + c for label, c in cols if c not in self._all_col_set) self._all_col_set.update(c for label, c in cols) self._data.update((label, c) for label, c in cols) def extend(self, iter): cols = list(iter) - self._all_columns.extend(c for c in cols if c not in self._all_col_set) + self._all_columns.extend(c for c in cols if c not in + self._all_col_set) self._all_col_set.update(cols) self._data.update((c.key, c) for c in cols) @@ -574,7 +587,8 @@ class ColumnCollection(util.OrderedProperties): return col in self._all_col_set def as_immutable(self): - return ImmutableColumnCollection(self._data, self._all_col_set, self._all_columns) + return ImmutableColumnCollection( + self._data, self._all_col_set, self._all_columns) class ImmutableColumnCollection(util.ImmutableProperties, ColumnCollection): @@ -609,6 +623,7 @@ class ColumnSet(util.ordered_column_set): def __hash__(self): return hash(tuple(x for x in self)) + def _bind_or_error(schemaitem, msg=None): bind = schemaitem.bind if not bind: @@ -621,7 +636,7 @@ def _bind_or_error(schemaitem, msg=None): item = '%s object' % name if msg is None: msg = "%s is not bound to an Engine or Connection. "\ - "Execution can not proceed without a database to execute "\ - "against." % item + "Execution can not proceed without a database to execute "\ + "against." % item raise exc.UnboundExecutionError(msg) return bind |