summaryrefslogtreecommitdiff
path: root/examples/derived_attributes/attributes.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/derived_attributes/attributes.py')
-rw-r--r--examples/derived_attributes/attributes.py32
1 files changed, 15 insertions, 17 deletions
diff --git a/examples/derived_attributes/attributes.py b/examples/derived_attributes/attributes.py
index 15af7b2ad..ade2a4ed3 100644
--- a/examples/derived_attributes/attributes.py
+++ b/examples/derived_attributes/attributes.py
@@ -1,7 +1,7 @@
import new
-class MethodDescriptor(object):
+class hybrid(object):
def __init__(self, func):
self.func = func
def __get__(self, instance, owner):
@@ -10,8 +10,8 @@ class MethodDescriptor(object):
else:
return new.instancemethod(self.func, instance, owner)
-class PropertyDescriptor(object):
- def __init__(self, fget, fset, fdel):
+class hybrid_property(object):
+ def __init__(self, fget, fset=None, fdel=None):
self.fget = fget
self.fset = fset
self.fdel = fdel
@@ -24,17 +24,15 @@ class PropertyDescriptor(object):
self.fset(instance, value)
def __delete__(self, instance):
self.fdel(instance)
-
-def hybrid(func):
- return MethodDescriptor(func)
-
-def hybrid_property(fget, fset=None, fdel=None):
- return PropertyDescriptor(fget, fset, fdel)
+
+ def setter(self, fset):
+ self.fset = fset
+ return self
### Example code
from sqlalchemy import MetaData, Table, Column, Integer
-from sqlalchemy.orm import mapper, create_session
+from sqlalchemy.orm import mapper, sessionmaker
metadata = MetaData('sqlite://')
metadata.bind.echo = True
@@ -91,7 +89,7 @@ mapper(Interval2, interval_table2)
print "Create the data"
-session = create_session()
+session = sessionmaker()()
intervals = [Interval1(1,4), Interval1(3,15), Interval1(11,16)]
@@ -99,18 +97,16 @@ for interval in intervals:
session.add(interval)
session.add(Interval2(interval.start, interval.length))
-session.flush()
-
-print "Clear the cache and do some queries"
+session.commit()
-session.expunge_all()
for Interval in (Interval1, Interval2):
print "Querying using interval class %s" % Interval.__name__
print
print '-- length less than 10'
- print [(i, i.length) for i in session.query(Interval).filter(Interval.length < 10).all()]
+ print [(i, i.length) for i in
+ session.query(Interval).filter(Interval.length < 10).all()]
print
print '-- contains 12'
@@ -119,6 +115,8 @@ for Interval in (Interval1, Interval2):
print
print '-- intersects 2..10'
other = Interval1(2,10)
- result = session.query(Interval).filter(Interval.intersects(other)).order_by(Interval.length).all()
+ result = session.query(Interval).\
+ filter(Interval.intersects(other)).\
+ order_by(Interval.length).all()
print [(interval, interval.intersects(other)) for interval in result]