Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
This is from my blog post on on 2013-03-01
from sqlalchemy import create_engine
engine = create_engine('sqlite:///:memory', echo=True)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.hybrid import hybrid_property
Base = declarative_base()
from sqlalchemy import Column, Integer, String, Float
class Order(Base):
__tablename__ = 'orders'
order_id = Column(Integer, primary_key=True)
order_reference_code = Column(String)
merchandise_cost = Column(Float)
tax = Column(Float)
shipping = Column(Float)
def __init__(self, order_reference_code, merchandise_cost, tax, shipping):
self.order_reference_code = order_reference_code
self.merchandise_cost = merchandise_cost = tax
self.shipping = shipping
def grand_total(self):
rollup_fields = [
total = sum([self.__getattribute__(x) for x in rollup_fields])
return round(total, 2)
order1 = Order('ABC', 57.50, 11.75, 6.00)
order2 = Order('BCD', 21.42, 4.72, 3.00)
order3 = Order('CDE', 1000.50, 80.34, 0.00)
order4 = Order('DEF', 500.50, 45.34, 100.00)
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind=engine)
db = Session()
for order in db.query(Order).all():
print order.order_reference_code, order.grand_total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.