For anyone who is interested attached is a standalone class using DAL. It
using a singleton for the database connection not sure if this is good or
bad for all applications. However I am working on classes that will remain
in memory.
As long as gluon is in your python path you can test this file with: python
body.py
PS: Make sure to edit the database connection string
--
--
Regards,
Bruce Wade
http://ca.linkedin.com/in/brucelwade
http://www.wadecybertech.com
http://www.warplydesigned.com
http://www.fitnessfriendsfinder.com
# Author: Bruce Wade
# 2012-03-10
import datetime
from gluon import DAL, Field
class Body:
db = None
def __init__(self, db):
if not Body.db:
Body.db = db
self.define_tables(Body.db)
def add_weight(self, member, weight):
now = datetime.datetime.now()
new_entry = Body.db.body_weight.insert(
member = member,
weight = weight,
updated_at = now
)
self.db.commit()
def delete_weight(self, member, weight_id):
weight = Body.db((Body.db.body_weight.member == member) & (Body.db.body_weight.id == weight_id)).select().first()
if weight:
weight.delete()
def get_body_weights(self, member):
return Body.db(Body.db.body_weight.member == member).select()
def get_last_weight(self, member):
return Body.db(Body.db.body_weight.member == member).select(orderby=~Body.db.body_weight.id).first()
def get_weight_loss(self, member):
first, last = self._get_max_last(member)
if not first and not last:
return None, "Not enough data!"
if first > last:
loss = first - last
return True, loss
else:
gained = last - first
return False, gained
def get_weight_gain(self, member):
first, last = self._get_max_last(member)
if not first and not last:
return None, "Not enough data!"
if first > last:
loss = first - last
return False, loss
else:
gained = last - first
return True, gained
def get_max_weight(self, member):
body_max = Body.db.body_weight.weight.max()
return Body.db(Body.db.body_weight.member == member).select(body_max).first()['MAX(body_weight.weight)']
def get_min_weight(self, member):
body_min = Body.db.body_weight.weight.min()
return Body.db(Body.db.body_weight.member == member).select(body_min).first()['MIN(body_weight.weight)']
def get_last_x_weights(self, member, weights_to_get = 5):
return Body.db(Body.db.body_weight.member == member).select(orderby=~Body.db.body_weight.id, limitby=(0, weights_to_get))
def _get_max_last(self, member):
max = self.get_max_weight(member)
last = Body.db(Body.db.body_weight.member == member).select(orderby=~Body.db.body_weight.id).first()
count = Body.db(Body.db.body_weight.member == member).count()
if count >= 2:
return max, last.weight
else:
return None, None
def define_tables(self, db):
print "Trying to define tables"
if 'body_weight' not in self.db.tables:
now = datetime.datetime.now()
Body.db.define_table('body_weight',
Field('member', 'integer'),
Field('weight', 'double'),
Field('record_date', 'date', default=now),
Field('updated_at', 'datetime', update=now)
)
self.db.commit()
if __name__ == "__main__":
db = DAL('postgres:psycopg2://user_name:password@localhost/database')
body = Body(db)
member = 1
weight = 155
print "Adding body weight"
#body.add_weight(member, weight)
print "Getting body weights"
print body.get_body_weights(member)
print "Get last entry"
print body.get_last_weight(member)
lost, amount = body.get_weight_loss(member)
if lost:
print "You lost %slbs" % amount
else:
print "Sorry you gained %slbs" % amount
gain, amount = body.get_weight_gain(member)
if gain:
print "You gained %slbs" % amount
else:
print "Sorry you lost %slbs" % amount
print "Max weight", body.get_max_weight(member)
print "Min weight", body.get_min_weight(member)
print "Last weights\n", body.get_last_x_weights(member)
import random, datetime
random.seed(datetime.datetime.now())
newbody = Body(db)
member = 2
for weight in xrange(100):
weight = random.randint(0, 500)
newbody.add_weight(member, weight)
print "Get last entry"
print newbody.get_last_weight(member)
lost, amount = newbody.get_weight_loss(member)
if lost:
print "You lost %slbs" % amount
else:
print "Sorry you gained %slbs" % amount
gain, amount = body.get_weight_gain(member)
if gain:
print "You gained %slbs" % amount
else:
print "Sorry you lost %slbs" % amount
print "Max weight", newbody.get_max_weight(member)
print "Min weight", newbody.get_min_weight(member)
print "Last weights\n", newbody.get_last_x_weights(member)