Mike Bayer avatar Mike Bayer committed 481e7b4

fix up docs a bit here, not sure if some enhancements had already
been made as things didn't seem as bad as [ticket:2306] indicated

Comments (0)

Files changed (1)

lib/sqlalchemy/ext/hybrid.py

 
 The :mod:`~sqlalchemy.ext.hybrid` extension provides a special form of method
 decorator, is around 50 lines of code and has almost no dependencies on the rest 
-of SQLAlchemy.  It can in theory work with any class-level expression generator.
+of SQLAlchemy.  It can, in theory, work with any descriptor-based expression 
+system.
 
-Consider a table ``interval`` as below::
+Consider a mapping ``Interval``, representing integer ``start`` and ``end``
+values. We can define higher level functions on mapped classes that produce
+SQL expressions at the class level, and Python expression evaluation at the
+instance level.  Below, each function decorated with :class:`.hybrid_method` or
+:class:`.hybrid_property` may receive ``self`` as an instance of the class, or
+as the class itself::
 
-    from sqlalchemy import MetaData, Table, Column, Integer
+    from sqlalchemy import Column, Integer
+    from sqlalchemy.ext.declarative import declarative_base
+    from sqlalchemy.orm import Session, aliased
+    from sqlalchemy.ext.hybrid import hybrid_property, hybrid_method
+    
+    Base = declarative_base()
+    
+    class Interval(Base):
+        __tablename__ = 'interval'
 
-    metadata = MetaData()
+        id = Column(Integer, primary_key=True)
+        start = Column(Integer, nullable=False)
+        end = Column(Integer, nullable=False)
 
-    interval_table = Table('interval', metadata,
-        Column('id', Integer, primary_key=True),
-        Column('start', Integer, nullable=False),
-        Column('end', Integer, nullable=False)
-    )
-
-We can define higher level functions on mapped classes that produce SQL
-expressions at the class level, and Python expression evaluation at the
-instance level.  Below, each function decorated with :func:`.hybrid_method`
-or :func:`.hybrid_property` may receive ``self`` as an instance of the class,
-or as the class itself::
-
-    from sqlalchemy.ext.hybrid import hybrid_property, hybrid_method
-    from sqlalchemy.orm import mapper, Session, aliased
-
-    class Interval(object):
         def __init__(self, start, end):
             self.start = start
             self.end = end
         def intersects(self, other):
             return self.contains(other.start) | self.contains(other.end)
     
-    mapper(Interval, interval_table)
-
 Above, the ``length`` property returns the difference between the ``end`` and
 ``start`` attributes.  With an instance of ``Interval``, this subtraction occurs
 in Python, using normal Python descriptor mechanics::
     >>> i1 = Interval(5, 10)
     >>> i1.length
     5
-    
-At the class level, the usual descriptor behavior of returning the descriptor
-itself is modified by :class:`.hybrid_property`, to instead evaluate the function 
-body given the ``Interval`` class as the argument::
+
+When dealing with the ``Interval`` class itself, the :class:`.hybrid_property`
+descriptor evaluates the function body given the ``Interval`` class as 
+the argument, which when evaluated with SQLAlchemy expression mechanics
+returns a new SQL expression::
     
     >>> print Interval.length
     interval."end" - interval.start
     FROM interval 
     WHERE interval."end" - interval.start = :param_1
 
-The ``contains()`` and ``intersects()`` methods are decorated with :class:`.hybrid_method`.
-This decorator applies the same idea to methods which accept
-zero or more arguments.   The above methods return boolean values, and take advantage 
+The ``Interval`` class example also illustrates two methods, ``contains()`` and ``intersects()``,
+decorated with :class:`.hybrid_method`.
+This decorator applies the same idea to methods that :class:`.hybrid_property` applies
+to attributes.   The methods return boolean values, and take advantage 
 of the Python ``|`` and ``&`` bitwise operators to produce equivalent instance-level and 
 SQL expression-level boolean behavior::
 
 
     >>> sw1 = aliased(SearchWord)
     >>> sw2 = aliased(SearchWord)
-    >>> print Session().query(sw1.word_insensitive, sw2.word_insensitive).filter(sw1.word_insensitive > sw2.word_insensitive)
+    >>> print Session().query(
+    ...                    sw1.word_insensitive, 
+    ...                    sw2.word_insensitive).\\
+    ...                        filter(
+    ...                            sw1.word_insensitive > sw2.word_insensitive
+    ...                        )
     SELECT lower(searchword_1.word) AS lower_1, lower(searchword_2.word) AS lower_2 
     FROM searchword AS searchword_1, searchword AS searchword_2 
     WHERE lower(searchword_1.word) > lower(searchword_2.word)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.