]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- remove remaining create_session() calls from examples, replace with Session
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 24 Oct 2010 16:42:47 +0000 (12:42 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 24 Oct 2010 16:42:47 +0000 (12:42 -0400)
- replace all flush()/expunge_all() with commit()

examples/association/basic_association.py
examples/association/proxied_association.py
examples/custom_attributes/custom_management.py
examples/sharding/attribute_shard.py
examples/vertical/dictlike-polymorphic.py
examples/vertical/dictlike.py

index 9c280d7ea763ea03a8dc3388b7831fdf65824ddf..d3d7641678bbf51d93ddbdf79f8e48a56a15ce7f 100644 (file)
@@ -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()
index ac258121edc5719c7c9f26bed1b73674c1a6e6b1..fa41f21c32c1639627075110f8773f8393a0919b 100644 (file)
@@ -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()
index 4d135edcd6dc25c45e234154ac2698baae6aa8d1..34481b623502735eb64e968578144520095d48d8 100644 (file)
@@ -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
index 1a39f5de32c488c1917936547a0035ebafb93869..fd1fa50ae5d4b573e391a82018a159d435133526 100644 (file)
@@ -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
index e4046b3ad293f36177b0edff1dc1c451c274778b..1acb6aee5c7d545f739365822003ad5bd7af2fc2 100644 (file)
@@ -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(
index ce76b31402edc584e18c5a0a9d3b2ff58deb4d34..e288d70ba9e9f036e4f76e0d5d28d3ba080007f9 100644 (file)
@@ -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(