summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/ext/declarative/__init__.py
diff options
context:
space:
mode:
authorDiana Clarke <diana.joan.clarke@gmail.com>2012-11-19 13:31:21 -0500
committerDiana Clarke <diana.joan.clarke@gmail.com>2012-11-19 13:31:21 -0500
commitef326d9fe39ab493dd4aeaf2cecf9052a04d49b7 (patch)
tree0684ba72e53a9321a36f07a3a8a5dcec833b55e5 /lib/sqlalchemy/ext/declarative/__init__.py
parent34b8b22659999eae459ca33baa3ca479f8eb5bf1 (diff)
downloadsqlalchemy-ef326d9fe39ab493dd4aeaf2cecf9052a04d49b7.tar.gz
just a pep8 pass of lib/sqlalchemy/ext/declarative
Diffstat (limited to 'lib/sqlalchemy/ext/declarative/__init__.py')
-rw-r--r--lib/sqlalchemy/ext/declarative/__init__.py80
1 files changed, 43 insertions, 37 deletions
diff --git a/lib/sqlalchemy/ext/declarative/__init__.py b/lib/sqlalchemy/ext/declarative/__init__.py
index bf6e6786e..1fb3feb6a 100644
--- a/lib/sqlalchemy/ext/declarative/__init__.py
+++ b/lib/sqlalchemy/ext/declarative/__init__.py
@@ -51,7 +51,8 @@ assigned.
To name columns explicitly with a name distinct from their mapped attribute,
just give the column a name. Below, column "some_table_id" is mapped to the
-"id" attribute of `SomeClass`, but in SQL will be represented as "some_table_id"::
+"id" attribute of `SomeClass`, but in SQL will be represented as
+"some_table_id"::
class SomeClass(Base):
__tablename__ = 'some_table'
@@ -312,7 +313,8 @@ such as those which already take advantage of the data-driven nature of
Note that when the ``__table__`` approach is used, the object is immediately
usable as a plain :class:`.Table` within the class declaration body itself,
as a Python class is only another syntactical block. Below this is illustrated
-by using the ``id`` column in the ``primaryjoin`` condition of a :func:`.relationship`::
+by using the ``id`` column in the ``primaryjoin`` condition of a
+:func:`.relationship`::
class MyClass(Base):
__table__ = Table('my_table', Base.metadata,
@@ -324,8 +326,8 @@ by using the ``id`` column in the ``primaryjoin`` condition of a :func:`.relatio
primaryjoin=Widget.myclass_id==__table__.c.id)
Similarly, mapped attributes which refer to ``__table__`` can be placed inline,
-as below where we assign the ``name`` column to the attribute ``_name``, generating
-a synonym for ``name``::
+as below where we assign the ``name`` column to the attribute ``_name``,
+generating a synonym for ``name``::
from sqlalchemy.ext.declarative import synonym_for
@@ -383,9 +385,9 @@ Mapper Configuration
Declarative makes use of the :func:`~.orm.mapper` function internally
when it creates the mapping to the declared table. The options
-for :func:`~.orm.mapper` are passed directly through via the ``__mapper_args__``
-class attribute. As always, arguments which reference locally
-mapped columns can reference them directly from within the
+for :func:`~.orm.mapper` are passed directly through via the
+``__mapper_args__`` class attribute. As always, arguments which reference
+locally mapped columns can reference them directly from within the
class declaration::
from datetime import datetime
@@ -521,8 +523,8 @@ In a situation like this, Declarative can't be sure
of the intent, especially if the ``start_date`` columns had, for example,
different types. A situation like this can be resolved by using
:class:`.declared_attr` to define the :class:`.Column` conditionally, taking
-care to return the **existing column** via the parent ``__table__`` if it already
-exists::
+care to return the **existing column** via the parent ``__table__`` if it
+already exists::
from sqlalchemy.ext.declarative import declared_attr
@@ -654,12 +656,13 @@ Using the Concrete Helpers
^^^^^^^^^^^^^^^^^^^^^^^^^^^
Helper classes provides a simpler pattern for concrete inheritance.
-With these objects, the ``__declare_last__`` helper is used to configure the "polymorphic"
-loader for the mapper after all subclasses have been declared.
+With these objects, the ``__declare_last__`` helper is used to configure the
+"polymorphic" loader for the mapper after all subclasses have been declared.
.. versionadded:: 0.7.3
-An abstract base can be declared using the :class:`.AbstractConcreteBase` class::
+An abstract base can be declared using the
+:class:`.AbstractConcreteBase` class::
from sqlalchemy.ext.declarative import AbstractConcreteBase
@@ -757,8 +760,8 @@ Augmenting the Base
In addition to using a pure mixin, most of the techniques in this
section can also be applied to the base class itself, for patterns that
-should apply to all classes derived from a particular base. This
-is achieved using the ``cls`` argument of the :func:`.declarative_base` function::
+should apply to all classes derived from a particular base. This is achieved
+using the ``cls`` argument of the :func:`.declarative_base` function::
from sqlalchemy.ext.declarative import declared_attr
@@ -778,9 +781,9 @@ is achieved using the ``cls`` argument of the :func:`.declarative_base` function
class MyModel(Base):
name = Column(String(1000))
-Where above, ``MyModel`` and all other classes that derive from ``Base`` will have
-a table name derived from the class name, an ``id`` primary key column, as well as
-the "InnoDB" engine for MySQL.
+Where above, ``MyModel`` and all other classes that derive from ``Base`` will
+have a table name derived from the class name, an ``id`` primary key column,
+as well as the "InnoDB" engine for MySQL.
Mixing in Columns
~~~~~~~~~~~~~~~~~
@@ -840,7 +843,8 @@ extension can use the resulting :class:`.Column` object as returned by
the method without the need to copy it.
.. versionchanged:: > 0.6.5
- Rename 0.6.5 ``sqlalchemy.util.classproperty`` into :class:`~.declared_attr`.
+ Rename 0.6.5 ``sqlalchemy.util.classproperty``
+ into :class:`~.declared_attr`.
Columns generated by :class:`~.declared_attr` can also be
referenced by ``__mapper_args__`` to a limited degree, currently
@@ -933,12 +937,13 @@ Mixing in Association Proxy and Other Attributes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mixins can specify user-defined attributes as well as other extension
-units such as :func:`.association_proxy`. The usage of :class:`.declared_attr`
-is required in those cases where the attribute must be tailored specifically
-to the target subclass. An example is when constructing multiple
-:func:`.association_proxy` attributes which each target a different type
-of child object. Below is an :func:`.association_proxy` / mixin example
-which provides a scalar list of string values to an implementing class::
+units such as :func:`.association_proxy`. The usage of
+:class:`.declared_attr` is required in those cases where the attribute must
+be tailored specifically to the target subclass. An example is when
+constructing multiple :func:`.association_proxy` attributes which each
+target a different type of child object. Below is an
+:func:`.association_proxy` / mixin example which provides a scalar list of
+string values to an implementing class::
from sqlalchemy import Column, Integer, ForeignKey, String
from sqlalchemy.orm import relationship
@@ -1138,8 +1143,8 @@ Creating Indexes with Mixins
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To define a named, potentially multicolumn :class:`.Index` that applies to all
-tables derived from a mixin, use the "inline" form of :class:`.Index` and establish
-it as part of ``__table_args__``::
+tables derived from a mixin, use the "inline" form of :class:`.Index` and
+establish it as part of ``__table_args__``::
class MyMixin(object):
a = Column(Integer)
@@ -1160,9 +1165,9 @@ Special Directives
~~~~~~~~~~~~~~~~~~~~~~
The ``__declare_last__()`` hook allows definition of
-a class level function that is automatically called by the :meth:`.MapperEvents.after_configured`
-event, which occurs after mappings are assumed to be completed and the 'configure' step
-has finished::
+a class level function that is automatically called by the
+:meth:`.MapperEvents.after_configured` event, which occurs after mappings are
+assumed to be completed and the 'configure' step has finished::
class MyClass(Base):
@classmethod
@@ -1178,9 +1183,9 @@ has finished::
~~~~~~~~~~~~~~~~~~~
``__abstract__`` causes declarative to skip the production
-of a table or mapper for the class entirely. A class can be added within a hierarchy
-in the same way as mixin (see :ref:`declarative_mixins`), allowing subclasses to extend
-just from the special class::
+of a table or mapper for the class entirely. A class can be added within a
+hierarchy in the same way as mixin (see :ref:`declarative_mixins`), allowing
+subclasses to extend just from the special class::
class SomeAbstractBase(Base):
__abstract__ = True
@@ -1195,8 +1200,8 @@ just from the special class::
class MyMappedClass(SomeAbstractBase):
""
-One possible use of ``__abstract__`` is to use a distinct :class:`.MetaData` for different
-bases::
+One possible use of ``__abstract__`` is to use a distinct
+:class:`.MetaData` for different bases::
Base = declarative_base()
@@ -1208,9 +1213,10 @@ bases::
__abstract__ = True
metadata = MetaData()
-Above, classes which inherit from ``DefaultBase`` will use one :class:`.MetaData` as the
-registry of tables, and those which inherit from ``OtherBase`` will use a different one.
-The tables themselves can then be created perhaps within distinct databases::
+Above, classes which inherit from ``DefaultBase`` will use one
+:class:`.MetaData` as the registry of tables, and those which inherit from
+``OtherBase`` will use a different one. The tables themselves can then be
+created perhaps within distinct databases::
DefaultBase.metadata.create_all(some_engine)
OtherBase.metadata_create_all(some_other_engine)