Daemon: unabridged
This is an unabridged version of the Daemon: basics example. See that section for a description for how these file contents fit into a broader scheme.
precious.py
import mKTL
class Store(mKTL.Daemon):
def setup(self):
self.add_item(Gold, 'GOLD')
self.add_item(Silver, 'SILVER')
self.add_item(Platinum, 'PLATINUM')
class MarketPriced(mKTL.Item):
def __init__(self, *args, **kwargs):
mKTL.Item.__init__(self, *args, **kwargs)
self.poll(86400) # Update once per day.
class Gold(MarketPriced)
def req_refresh(self):
return get_spot_value('gold', 'usd', 'grams')
class Platinum(MarketPriced)
def req_refresh(self):
return get_spot_value('platinum', 'usd', 'grams')
class Silver(MarketPriced)
def req_refresh(self):
return get_spot_value('silver', 'usd', 'grams')
def get_spot_value(metal, currency, units):
# Presumably this involves something like a curl/wget call to an
# external website. Assume that is exactly what would occur in this
# space, and we retrieved a bare number for the metal, currency,
# and units of interest.
#
# current_price = some magical invocation of external resources
current_price = float(current_price)
payload = dict()
payload['value'] = current_price
payload['time'] = time.time()
return payload
# vim: set expandtab tabstop=8 softtabstop=4 shiftwidth=4 autoindent:
precious.json
{
"GOLD": {
"type": "numeric",
"units": {
"base": "USD/gram"
"formatted": "USD/gram",
},
"description": "Current spot price for pure gold.",
"settable": false
},
"PLATINUM": {
"type": "numeric",
"units": {
"base": "USD/gram"
"formatted": "USD/gram",
},
"description": "Current spot price for pure platinum.",
"settable": false
},
"SILVER": {
"type": "numeric",
"units": {
"base": "USD/gram"
"formatted": "USD/gram",
},
"description": "Current spot price for pure silver.",
"settable": false
}
}