summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2010-11-06 11:49:45 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2010-11-06 11:49:45 -0400
commit4e2c0f10cd164511b9c6377b72a8c0527e4eb716 (patch)
tree830319060dc68ec7de0eaa04eaf8ba8e7948d535 /examples
parentd9dc05adb689bc4eab2227a96af0d874696cc63d (diff)
parent30bc42403754110df1fdec3037c7700cc4f26b70 (diff)
downloadsqlalchemy-4e2c0f10cd164511b9c6377b72a8c0527e4eb716.tar.gz
- merge tip
Diffstat (limited to 'examples')
-rw-r--r--examples/association/basic_association.py10
-rw-r--r--examples/association/proxied_association.py10
-rw-r--r--examples/custom_attributes/custom_management.py60
-rw-r--r--examples/sharding/attribute_shard.py4
-rw-r--r--examples/vertical/dictlike-polymorphic.py11
-rw-r--r--examples/vertical/dictlike.py11
6 files changed, 54 insertions, 52 deletions
diff --git a/examples/association/basic_association.py b/examples/association/basic_association.py
index 9c280d7ea..d3d764167 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 ac258121e..fa41f21c3 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 03d38c26a..2e2689140 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
+from sqlalchemy.orm.attributes import set_attribute, get_attribute, \
+ del_attribute
from sqlalchemy.orm.instrumentation import is_instrumented
from sqlalchemy.orm.collections import collection_adapter
@@ -119,17 +123,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):
@@ -151,8 +158,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):
@@ -173,11 +185,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)
@@ -187,8 +198,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 1a39f5de3..fd1fa50ae 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 e4046b3ad..1acb6aee5 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 ce76b3140..e288d70ba 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(