def calc_price(base_price, option_factors): price = base_price for f in option_factors: price *= float(f.factor) return round(price, 2) def apply_adjust(db, machine_id, brand_id, price): rows = db.execute(""" SELECT factor FROM price_adjust_factor WHERE level='global' UNION ALL SELECT factor FROM price_adjust_factor WHERE level='brand' AND ref_id=:bid UNION ALL SELECT factor FROM price_adjust_factor WHERE level='machine' AND ref_id=:mid """, { "mid": machine_id, "bid": brand_id }).fetchall() for r in rows: price *= float(r.factor) return price