Ñò
Š„[Jc @ s© d Z d d k l Z d d k l Z d g Z d
d „ Z d „ Z d „ Z d „ Z
d „ Z d
e f d „ ƒ YZ
e d j o# d d
k Z e i d e i ƒ n d
S( s
A custom list that manages index/position information for its children.
``orderinglist`` is a custom list collection implementation for mapped
relations that keeps an arbitrary "position" attribute on contained objects in
sync with each object's position in the Python list.
The collection acts just like a normal Python ``list``, with the added
behavior that as you manipulate the list (via ``insert``, ``pop``, assignment,
deletion, what have you), each of the objects it contains is updated as needed
to reflect its position. This is very useful for managing ordered relations
which have a user-defined, serialized order::
>>> from sqlalchemy import MetaData, Table, Column, Integer, String, ForeignKey
>>> from sqlalchemy.orm import mapper, relation
>>> from sqlalchemy.ext.orderinglist import ordering_list
A simple model of users their "top 10" things::
>>> metadata = MetaData()
>>> users = Table('users', metadata,
... Column('id', Integer, primary_key=True))
>>> blurbs = Table('user_top_ten_list', metadata,
... Column('id', Integer, primary_key=True),
... Column('user_id', Integer, ForeignKey('users.id')),
... Column('position', Integer),
... Column('blurb', String(80)))
>>> class User(object):
... pass
...
>>> class Blurb(object):
... def __init__(self, blurb):
... self.blurb = blurb
...
>>> mapper(User, users, properties={
... 'topten': relation(Blurb, collection_class=ordering_list('position'),
... order_by=[blurbs.c.position])})
>>> mapper(Blurb, blurbs)
Acts just like a regular list::
>>> u = User()
>>> u.topten.append(Blurb('Number one!'))
>>> u.topten.append(Blurb('Number two!'))
But the ``.position`` attibute is set automatically behind the scenes::
>>> assert [blurb.position for blurb in u.topten] == [0, 1]
The objects will be renumbered automaticaly after any list-changing operation,
for example an ``insert()``::
>>> u.topten.insert(1, Blurb('I am the new Number Two.'))
>>> assert [blurb.position for blurb in u.topten] == [0, 1, 2]
>>> assert u.topten[1].blurb == 'I am the new Number Two.'
>>> assert u.topten[1].position == 1
Numbering and serialization are both highly configurable. See the docstrings
in this module and the main SQLAlchemy documentation for more information and
examples.
The :class:`~sqlalchemy.ext.orderinglist.ordering_list` factory function is the
ORM-compatible constructor for `OrderingList` instances.
iÿÿÿÿ( t
collection( t utilt
ordering_listc s" t d | ˆ ‰ ‡ ‡ f d † S( s” Prepares an OrderingList factory for use in mapper definitions.
Returns an object suitable for use as an argument to a Mapper relation's
``collection_class`` option. Arguments are:
attr
Name of the mapped attribute to use for storage and retrieval of
ordering information
count_from (optional)
Set up an integer-based ordering, starting at ``count_from``. For
example, ``ordering_list('pos', count_from=1)`` would create a 1-based
list in SQL, storing the value in the 'pos' column. Ignored if
``ordering_func`` is supplied.
Passes along any keyword arguments to ``OrderingList`` constructor.
t
count_fromc s
t ˆ ˆ S( ( t OrderingList( ( t kwt attr( s? /usr/lib/python2.6/site-packages/sqlalchemy/ext/orderinglist.pyt ] s ( t _unsugar_count_from( R R R ( ( R R s? /usr/lib/python2.6/site-packages/sqlalchemy/ext/orderinglist.pyR I s c C s | S( s7 Numbering function: consecutive integers starting at 0.( ( t indexR ( ( s? /usr/lib/python2.6/site-packages/sqlalchemy/ext/orderinglist.pyt count_from_0` s c C s | d S( s7 Numbering function: consecutive integers starting at 1.i ( ( R R ( ( s? /usr/lib/python2.6/site-packages/sqlalchemy/ext/orderinglist.pyt count_from_1e s c s: ‡ f d † } y d ˆ | _ Wn t j
o n X| S( sE Numbering function: consecutive integers starting at arbitrary start.c s | ˆ S( N( ( R R ( t start( s? /usr/lib/python2.6/site-packages/sqlalchemy/ext/orderinglist.pyt fm s s
count_from_%i( t __name__t TypeError( R R
( ( R s? /usr/lib/python2.6/site-packages/sqlalchemy/ext/orderinglist.pyt count_from_n_factoryj s c K s† | i d d ƒ } | i d d ƒ d j oW | d j oJ | d j o t | d