On Mon, Oct 3, 2016 at 11:43 AM, Jinghui Niu <niujing...@gmail.com> wrote:
> This really helps. Thank you Simon! I still have a couple of smaller
> questions.
>
>> When you access <class>.fullname, the "self" parameter is now the
>> *class*, so self.firstname and self.lastname are SQLAlchemy column
>> properties.
>
>
> Here by *column properties* do you mean the object returned by
> column_property() function? Are they used interchangeably with
> *InstrumentedAttribute object*?
>

In this case they are in fact InstrumentedAttributes. (Actually, I
think properties created using column_property are *also*
InstrumentedAttributes).

>
>> but you'll need to accept that you can't simply use your
>> currency_exchange_rate_lookup
>> dictionary as it is.
>
>
> I have a dream, that one day SQL side and Python side can truly mingle in
> such a way that when you query SQL you can directly refer to variables
> defined in the Python model class:-)
>

In your case, I guess you need to construct some sort of case
statement based on the values in the dictionary. Here's some untested
code:

@amountInCAD.expression
def amountInCAD(cls):
    conditions = []
    rates = cls.currency_exchange_rate_lookup.items()
    for ((from_currency, to_currency), exchange_rate) in rates:
        assert to_currency == 'CAD'
        conditions.append((cls.currency == from_currency), cls.amount
* exchange_rate))
    # You'll need to implement the "ELSE" case yourself
    return sa.case(conditions)

Hope that helps,

Simon

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to