I just finished watching your video about virtualfields.
Its a toss up between the two.
The one thing I really like about the class structure, is being able to pass defaults to it (like tax).
Another thing that I really like about classes, is that when you specify _tablename to something else, it creates a virtual table. This way a virtual table can be created, with access to all of the functions in the class.
Everything that I was worried about being able to do, can be accomplished with virtualfields in classes.
I think using a dictionary instead would be more restrictive. I really like what you settled on in the video.
The only thing that *might* come up in the future, is what if you wanted multiple classes passed to your virtual field, which may or may not ever be needed.
I can see a layout in db.py such as
db.define_table('products', Field('name'), Field('price', 'double'))
db.define_tables('sales', Field('product', db.products), Field('quantity', 'integer'))
qry_product_sales = (db.sales.id_product ==
db.products.id) & (
db.products.id > 0)
class VF_total:
_tablename = 'calcd'
def __init__(self, tax): self.tax = tax
def total_pre_tax: return (self.sales.product.price * self.sales.quantity)
def sales_tax: return self.total - self.total_pre_tax
def total: return self.total_pre_tax * self.tax
... #later in controllers
all_sales = db(qry_product_sales)
all_sales.virtualfields = VF_total(1.07)
print "Name, Price, Quantity, SubTotal, Tax, Total"
for sale in all_sales:
print
sale.sales.product.name, sale.sales.product.price, sale.sales.quantity, sale.calcd.total_pre_tax, sale.calcd.sales_tax, sale.calcd.total
### Now this is the only thing that I think is missing, aggregates on the rows object. These would be columns that don't necissarily need to be on all rows.
print "Total Gross Sales", all_sales.calcd.total.sum()
print "Average Sale", all_sales.calcd.total.avg()
-Thadeus