From bfc0977063a32f1820fc02d9465e73b20f7fbaaf Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 24 Oct 2010 12:42:47 -0400 Subject: [PATCH] - remove remaining create_session() calls from examples, replace with Session - replace all flush()/expunge_all() with commit() --- examples/association/basic_association.py | 10 ++-- examples/association/proxied_association.py | 10 ++-- .../custom_attributes/custom_management.py | 60 +++++++++++-------- examples/sharding/attribute_shard.py | 4 +- examples/vertical/dictlike-polymorphic.py | 11 ++-- examples/vertical/dictlike.py | 11 ++-- 6 files changed, 54 insertions(+), 52 deletions(-) diff --git a/examples/association/basic_association.py b/examples/association/basic_association.py index 9c280d7ea7..d3d7641678 100644 --- a/examples/association/basic_association.py +++ b/examples/association/basic_association.py @@ -14,7 +14,7 @@ from datetime import datetime from sqlalchemy import (create_engine, MetaData, Table, Column, Integer, String, DateTime, Numeric, ForeignKey, and_) -from sqlalchemy.orm import mapper, relationship, create_session +from sqlalchemy.orm import mapper, relationship, Session # Uncomment these to watch database activity. #import logging @@ -70,14 +70,14 @@ mapper(OrderItem, orderitems, properties={ 'item': relationship(Item, lazy='joined') }) -session = create_session() +session = Session() # create our catalog session.add(Item('SA T-Shirt', 10.99)) session.add(Item('SA Mug', 6.50)) session.add(Item('SA Hat', 8.99)) session.add(Item('MySQL Crowbar', 16.99)) -session.flush() +session.commit() # function to return items from the DB def item(name): @@ -91,9 +91,7 @@ order.order_items.append(OrderItem(item('SA Mug'))) order.order_items.append(OrderItem(item('MySQL Crowbar'), 10.99)) order.order_items.append(OrderItem(item('SA Hat'))) session.add(order) -session.flush() - -session.expunge_all() +session.commit() # query the order, print items order = session.query(Order).filter_by(customer_name='john smith').one() diff --git a/examples/association/proxied_association.py b/examples/association/proxied_association.py index ac258121ed..fa41f21c32 100644 --- a/examples/association/proxied_association.py +++ b/examples/association/proxied_association.py @@ -4,7 +4,7 @@ the usage of the associationproxy extension.""" from datetime import datetime from sqlalchemy import (create_engine, MetaData, Table, Column, Integer, String, DateTime, Float, ForeignKey, and_) -from sqlalchemy.orm import mapper, relationship, create_session +from sqlalchemy.orm import mapper, relationship, Session from sqlalchemy.ext.associationproxy import AssociationProxy engine = create_engine('sqlite://') @@ -55,14 +55,14 @@ mapper(OrderItem, orderitems, properties={ 'item':relationship(Item, lazy='joined') }) -session = create_session() +session = Session() # create our catalog session.add_all([Item('SA T-Shirt', 10.99), Item('SA Mug', 6.50), Item('SA Hat', 8.99), Item('MySQL Crowbar', 16.99)]) -session.flush() +session.commit() # function to return items def item(name): @@ -81,9 +81,7 @@ order.items.append(item('SA Mug')) order.items.append(item('SA Hat')) session.add(order) -session.flush() - -session.expunge_all() +session.commit() # query the order, print items order = session.query(Order).filter_by(customer_name='john smith').one() diff --git a/examples/custom_attributes/custom_management.py b/examples/custom_attributes/custom_management.py index 4d135edcd6..34481b6235 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 diff --git a/examples/sharding/attribute_shard.py b/examples/sharding/attribute_shard.py index 1a39f5de32..fd1fa50ae5 100644 --- a/examples/sharding/attribute_shard.py +++ b/examples/sharding/attribute_shard.py @@ -248,9 +248,7 @@ quito.reports.append(Report(85)) sess = create_session() for c in [tokyo, newyork, toronto, london, dublin, brasilia, quito]: sess.add(c) -sess.flush() - -sess.expunge_all() +sess.commit() t = sess.query(WeatherLocation).get(tokyo.id) assert t.city == tokyo.city diff --git a/examples/vertical/dictlike-polymorphic.py b/examples/vertical/dictlike-polymorphic.py index e4046b3ad2..1acb6aee5c 100644 --- a/examples/vertical/dictlike-polymorphic.py +++ b/examples/vertical/dictlike-polymorphic.py @@ -132,7 +132,7 @@ if __name__ == '__main__': from sqlalchemy import (MetaData, Table, Column, Integer, Unicode, ForeignKey, UnicodeText, and_, not_, or_, String, Boolean, cast, text, null, case) - from sqlalchemy.orm import mapper, relationship, create_session + from sqlalchemy.orm import mapper, relationship, Session from sqlalchemy.orm.collections import attribute_mapped_collection metadata = MetaData() @@ -191,7 +191,7 @@ if __name__ == '__main__': metadata.bind = 'sqlite:///' metadata.create_all() - session = create_session() + session = Session() stoat = Animal(u'stoat') stoat[u'color'] = u'red' @@ -199,8 +199,7 @@ if __name__ == '__main__': stoat[u'weasel-like'] = True session.add(stoat) - session.flush() - session.expunge_all() + session.commit() critter = session.query(Animal).filter(Animal.name == u'stoat').one() print critter[u'color'] @@ -210,7 +209,7 @@ if __name__ == '__main__': critter[u'cuteness'] = u'very cute' metadata.bind.echo = True - session.flush() + session.commit() metadata.bind.echo = False marten = Animal(u'marten') @@ -225,7 +224,7 @@ if __name__ == '__main__': shrew[u'poisonous'] = True session.add(shrew) - session.flush() + session.commit() q = (session.query(Animal). filter(Animal.facts.any( diff --git a/examples/vertical/dictlike.py b/examples/vertical/dictlike.py index ce76b31402..e288d70ba9 100644 --- a/examples/vertical/dictlike.py +++ b/examples/vertical/dictlike.py @@ -125,7 +125,7 @@ class VerticalPropertyDictMixin(object): if __name__ == '__main__': from sqlalchemy import (MetaData, Table, Column, Integer, Unicode, ForeignKey, UnicodeText, and_, not_) - from sqlalchemy.orm import mapper, relationship, create_session + from sqlalchemy.orm import mapper, relationship, Session from sqlalchemy.orm.collections import attribute_mapped_collection metadata = MetaData() @@ -175,7 +175,7 @@ if __name__ == '__main__': metadata.bind = 'sqlite:///' metadata.create_all() - session = create_session() + session = Session() stoat = Animal(u'stoat') stoat[u'color'] = u'reddish' @@ -186,8 +186,7 @@ if __name__ == '__main__': print stoat.facts[u'color'] session.add(stoat) - session.flush() - session.expunge_all() + session.commit() critter = session.query(Animal).filter(Animal.name == u'stoat').one() print critter[u'color'] @@ -197,7 +196,7 @@ if __name__ == '__main__': print 'changing cuteness:' metadata.bind.echo = True - session.flush() + session.commit() metadata.bind.echo = False marten = Animal(u'marten') @@ -214,7 +213,7 @@ if __name__ == '__main__': loris[u'cuteness'] = u'fairly' loris[u'poisonous-part'] = u'elbows' session.add(loris) - session.flush() + session.commit() q = (session.query(Animal). filter(Animal.facts.any( -- 2.47.2