diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-08-08 20:58:05 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-08-08 20:58:05 -0400 |
commit | c14a3651a4ea624e0539c4ee7d45e23bf6139403 (patch) | |
tree | b990928374e5934af0c0a59c87cb1a5ed0cddbd4 /examples/derived_attributes/attributes.py | |
parent | 0daf6e174d596e14e34f098f14d298a04a1b4d9b (diff) | |
download | sqlalchemy-c14a3651a4ea624e0539c4ee7d45e23bf6139403.tar.gz |
- modernizing examples
Diffstat (limited to 'examples/derived_attributes/attributes.py')
-rw-r--r-- | examples/derived_attributes/attributes.py | 32 |
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] |