#NEWLINE# @ classmethod #NEWLINE# def setup_class( cls ) : #NEWLINE# #INDENT# from sqlalchemy import literal #NEWLINE# symbols = ( 'usd' , 'gbp' , 'cad' , 'eur' , 'aud' ) #NEWLINE# currency_lookup = dict ( ( ( ( currency_from , currency_to ) , Decimal ( str ( rate ) ) ) for ( currency_to , values ) in zip ( symbols , [ ( 1 , 1.59009 , 0.988611 , 1.37979 , 1.02962 ) , ( 0.628895 , 1 , 0.621732 , 0.867748 , 0.647525 ) , ( 1.01152 , 1.6084 , 1 , 1.39569 , 1.04148 ) , ( 0.724743 , 1.1524 , 0.716489 , 1 , 0.746213 ) , ( 0.971228 , 1.54434 , 0.960166 , 1.34009 , 1 ) ] ) for ( currency_from , rate ) in zip ( symbols , values ) ) ) #NEWLINE# class Amount ( object ) : #NEWLINE# #INDENT# def __init__( self , amount , currency ) : #NEWLINE# #INDENT# self . currency = currency #NEWLINE# self . amount = amount #NEWLINE# #UNINDENT# def __add__( self , other ) : #NEWLINE# #INDENT# return Amount ( ( self . amount + other . as_currency ( self . currency ) . amount ) , self . currency ) #NEWLINE# #UNINDENT# def __sub__( self , other ) : #NEWLINE# #INDENT# return Amount ( ( self . amount - other . as_currency ( self . currency ) . amount ) , self . currency ) #NEWLINE# #UNINDENT# def __lt__( self , other ) : #NEWLINE# #INDENT# return ( self . amount < other . as_currency ( self . currency ) . amount ) #NEWLINE# #UNINDENT# def __gt__( self , other ) : #NEWLINE# #INDENT# return ( self . amount > other . as_currency ( self . currency ) . amount ) #NEWLINE# #UNINDENT# def __eq__( self , other ) : #NEWLINE# #INDENT# return ( self . amount == other . as_currency ( self . currency ) . amount ) #NEWLINE# #UNINDENT# def as_currency( self , other_currency ) : #NEWLINE# #INDENT# return Amount ( ( currency_lookup [ ( self . currency , other_currency ) ] * self . amount ) , other_currency ) #NEWLINE# #UNINDENT# def __clause_element__( self ) : #NEWLINE# #INDENT# if isinstance ( self . amount , ( float , int , Decimal ) ) : #NEWLINE# #INDENT# return literal ( self . amount ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return self . amount #NEWLINE# #UNINDENT# #UNINDENT# def __str__( self ) : #NEWLINE# #INDENT# return ( '%2.4f %s' % ( self . amount , self . currency ) ) #NEWLINE# #UNINDENT# def __repr__( self ) : #NEWLINE# #INDENT# return ( 'Amount(%r, %r)' % ( self . amount , self . currency ) ) #NEWLINE# #UNINDENT# #UNINDENT# Base = declarative_base ( ) #NEWLINE# class BankAccount ( Base ) : #NEWLINE# #INDENT# __tablename__ = 'bank_account' #NEWLINE# id = Column ( Integer , primary_key = True ) #NEWLINE# _balance = Column ( 'balance' , Numeric ) #NEWLINE# @ hybrid . hybrid_property #NEWLINE# def balance( self ) : #NEWLINE# #INDENT# 'Return an Amount view of the current balance.' #NEWLINE# return Amount ( self . _balance , 'usd' ) #NEWLINE# #UNINDENT# @ balance . setter #NEWLINE# def balance( self , value ) : #NEWLINE# #INDENT# self . _balance = value . as_currency ( 'usd' ) . amount #NEWLINE# #UNINDENT# #UNINDENT# cls . Amount = Amount #NEWLINE# cls . BankAccount = BankAccount