diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-10-24 12:42:47 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-10-24 12:42:47 -0400 |
| commit | bfc0977063a32f1820fc02d9465e73b20f7fbaaf (patch) | |
| tree | 7e0099a07b29fe5f300d8044678cbd9754aea0e6 /examples/custom_attributes | |
| parent | a29f130be7a1589566547e94c0e283f0210f27ef (diff) | |
| download | sqlalchemy-bfc0977063a32f1820fc02d9465e73b20f7fbaaf.tar.gz | |
- remove remaining create_session() calls from examples, replace with Session
- replace all flush()/expunge_all() with commit()
Diffstat (limited to 'examples/custom_attributes')
| -rw-r--r-- | examples/custom_attributes/custom_management.py | 60 |
1 files changed, 35 insertions, 25 deletions
diff --git a/examples/custom_attributes/custom_management.py b/examples/custom_attributes/custom_management.py index 4d135edcd..34481b623 100644 --- a/examples/custom_attributes/custom_management.py +++ b/examples/custom_attributes/custom_management.py @@ -1,20 +1,24 @@ -"""this example illustrates how to replace SQLAlchemy's class descriptors with a user-defined system. +"""this example illustrates how to replace SQLAlchemy's class descriptors with +a user-defined system. -This sort of thing is appropriate for integration with frameworks that redefine class behaviors -in their own way, such that SQLA's default instrumentation is not compatible. +This sort of thing is appropriate for integration with frameworks that +redefine class behaviors in their own way, such that SQLA's default +instrumentation is not compatible. -The example illustrates redefinition of instrumentation at the class level as well as the collection -level, and redefines the storage of the class to store state within "instance._goofy_dict" instead -of "instance.__dict__". Note that the default collection implementations can be used -with a custom attribute system as well. +The example illustrates redefinition of instrumentation at the class level as +well as the collection level, and redefines the storage of the class to store +state within "instance._goofy_dict" instead of "instance.__dict__". Note that +the default collection implementations can be used with a custom attribute +system as well. """ -from sqlalchemy import (create_engine, MetaData, Table, Column, Integer, Text, - ForeignKey) -from sqlalchemy.orm import (mapper, relationship, create_session, - InstrumentationManager) +from sqlalchemy import create_engine, MetaData, Table, Column, Integer, Text,\ + ForeignKey +from sqlalchemy.orm import mapper, relationship, Session,\ + InstrumentationManager -from sqlalchemy.orm.attributes import set_attribute, get_attribute, del_attribute, is_instrumented +from sqlalchemy.orm.attributes import set_attribute, get_attribute, \ + del_attribute, is_instrumented from sqlalchemy.orm.collections import collection_adapter @@ -118,17 +122,20 @@ class MyCollectionAdapter(object): def fire_append_event(self, item, initiator=None): if initiator is not False and item is not None: - self.state.get_impl(self.key).fire_append_event(self.state, self.state.dict, item, - initiator) + self.state.get_impl(self.key).\ + fire_append_event(self.state, self.state.dict, item, + initiator) def fire_remove_event(self, item, initiator=None): if initiator is not False and item is not None: - self.state.get_impl(self.key).fire_remove_event(self.state, self.state.dict, item, - initiator) + self.state.get_impl(self.key).\ + fire_remove_event(self.state, self.state.dict, item, + initiator) def fire_pre_remove_event(self, initiator=None): - self.state.get_impl(self.key).fire_pre_remove_event(self.state, self.state.dict, - initiator) + self.state.get_impl(self.key).\ + fire_pre_remove_event(self.state, self.state.dict, + initiator) class MyCollection(object): def __init__(self): @@ -150,8 +157,13 @@ class MyCollection(object): if __name__ == '__main__': meta = MetaData(create_engine('sqlite://')) - table1 = Table('table1', meta, Column('id', Integer, primary_key=True), Column('name', Text)) - table2 = Table('table2', meta, Column('id', Integer, primary_key=True), Column('name', Text), Column('t1id', Integer, ForeignKey('table1.id'))) + table1 = Table('table1', meta, + Column('id', Integer, primary_key=True), + Column('name', Text)) + table2 = Table('table2', meta, + Column('id', Integer, primary_key=True), + Column('name', Text), + Column('t1id', Integer, ForeignKey('table1.id'))) meta.create_all() class A(MyClass): @@ -172,11 +184,10 @@ if __name__ == '__main__': assert a1.bs[0].name == 'b1' assert isinstance(a1.bs, MyCollection) - sess = create_session() + sess = Session() sess.add(a1) - sess.flush() - sess.expunge_all() + sess.commit() a1 = sess.query(A).get(a1.id) @@ -186,8 +197,7 @@ if __name__ == '__main__': a1.bs.remove(a1.bs[0]) - sess.flush() - sess.expunge_all() + sess.commit() a1 = sess.query(A).get(a1.id) assert len(a1.bs) == 1 |
