Skip to content

Instantly share code, notes, and snippets.

@mutaku
Last active January 2, 2016 08:28
Show Gist options
  • Save mutaku/8276234 to your computer and use it in GitHub Desktop.
Save mutaku/8276234 to your computer and use it in GitHub Desktop.
# Trying to filter on a hybrid_property
# This should return all but one of them (12 out of 13)
In [590]: active = db.session.query(db.Trade).filter(db.Trade.active == True).all()
# Nope
In [591]: active
Out[591]: []
# Try this?
In [592]: active = db.session.query(db.Trade).filter(db.Trade.active).all()
# Nope
In [593]: active
Out[593]: []
# This should select one of them (1 out of 13)
In [594]: active = db.session.query(db.Trade).filter(db.Trade.active == False).all()
# Return all of them (13/13) but can see active property is indeed properly set and working with each
In [595]: active
Out[595]:
[<Trade (symbol=BBDO, buy_date=2014-01-02 12:12:35.804996, perc=-5, active=False)>,
<Trade (symbol=DEG, buy_date=2014-01-02 12:12:35.804996, perc=-5, active=True)>,
<Trade (symbol=YZC, buy_date=2014-01-02 12:12:35.804996, perc=-5, active=True)>,
<Trade (symbol=HLF, buy_date=2014-01-03 11:33:02.190683, perc=-3, active=True)>,
<Trade (symbol=NUS, buy_date=2014-01-03 11:33:02.190683, perc=-3, active=True)>,
<Trade (symbol=S, buy_date=2014-01-03 11:33:02.190683, perc=-3, active=True)>,
<Trade (symbol=TMUS, buy_date=2014-01-03 11:33:02.190683, perc=-3, active=True)>,
<Trade (symbol=CTRP, buy_date=2014-01-03 11:33:02.190683, perc=-5, active=True)>,
<Trade (symbol=ASR, buy_date=2014-01-03 16:53:44.450940, perc=-3, active=True)>,
<Trade (symbol=GM, buy_date=2014-01-03 16:53:44.450940, perc=-3, active=True)>,
<Trade (symbol=IGLD, buy_date=2014-01-03 16:53:44.450940, perc=-3, active=True)>,
<Trade (symbol=MU, buy_date=2014-01-03 16:53:44.450940, perc=-3, active=True)>,
<Trade (symbol=VIP, buy_date=2014-01-03 16:53:44.450940, perc=-3, active=True)>]
# Okay, this is now odd -
In [610]: active = db.session.query(db.Trade).filter(db.Trade.active==True)
In [611]: print active
SELECT trades.id AS trades_id, trades.stock_id AS trades_stock_id, trades.quantity AS trades_quantity, trades.buy_price AS trades_buy_price, trades.buy_datetime AS trades_buy_datetime, trades.sell_price AS trades_sell_price, trades.sell_datetime AS trades_sell_datetime, trades.buy_percent_drop AS trades_buy_percent_drop, trades.sell_percent_change AS trades_sell_percent_change
FROM trades
WHERE false
# However, this works fine since we exclude that first one where active is False -
In [687]: for trade in db.session.query(db.Trade).all():
if trade.active == True:
print trade
.....:
<Trade (symbol=DEG, buy_date=2014-01-02 12:12:35.804996, perc=-5, active=True)>
<Trade (symbol=YZC, buy_date=2014-01-02 12:12:35.804996, perc=-5, active=True)>
<Trade (symbol=HLF, buy_date=2014-01-03 11:33:02.190683, perc=-3, active=True)>
<Trade (symbol=NUS, buy_date=2014-01-03 11:33:02.190683, perc=-3, active=True)>
<Trade (symbol=S, buy_date=2014-01-03 11:33:02.190683, perc=-3, active=True)>
<Trade (symbol=TMUS, buy_date=2014-01-03 11:33:02.190683, perc=-3, active=True)>
<Trade (symbol=CTRP, buy_date=2014-01-03 11:33:02.190683, perc=-5, active=True)>
<Trade (symbol=ASR, buy_date=2014-01-03 16:53:44.450940, perc=-3, active=True)>
<Trade (symbol=GM, buy_date=2014-01-03 16:53:44.450940, perc=-3, active=True)>
<Trade (symbol=IGLD, buy_date=2014-01-03 16:53:44.450940, perc=-3, active=True)>
<Trade (symbol=MU, buy_date=2014-01-03 16:53:44.450940, perc=-3, active=True)>
<Trade (symbol=VIP, buy_date=2014-01-03 16:53:44.450940, perc=-3, active=True)>
# FIXED... has to be something like:
return self.sell_datetime == None
# rather than since this won't operate at the db level in sqlalchemy
if not self.sell_datetime:
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment