I'm working on a requirement that needs to take several dicts as input arguments and transform/merge them into a single dict as the output, there will be some formulas applied to the input dicts. The output is something like below:
output = {
'pnl': 100,
'pnl_usd': 400,
'pnl_eur': 500,
'ytd': 200,
'ytd_usd': 2000,
'ytd_eur': 3000,
'mtd': 300,
'mtd_eur': 300,
'cost': [
{'prod_1': {'labour': 1000, 'material': 2000}},
{'prod_2': {'labour': 2000, 'material': 3000}}
]
# many more fields
}
I wonder what is the best practice of implementing this. Right now our developers defined many local variables first, and then calculate the numbers and then populate the dict and return it.
def produce_dict(cost_dict, revenue_dict, date):
pnl = revenue_dict.get('revenue') - cost_dict.get('cost')
pnl_in_usd = pnl * get_fx_rate(date)
pnl_in_usd_prev = pnl * get_fx_rate(date - 1)
pnl_mtd = revenue_dict.get('revenue_mtd') - cost_dict.get('cost_mtd')
pnl_mtd_in_usd = pnl_mtd * get_fx_rate(date)
pnl_mtd_in_usd_prev = pnl_mtd * get_fx_rate(date - 1)
dail_pnl_gain = pnl_mtd_in_usd - pnl_mtd_in_usd_prev
# many more local variables here
Is there a cleaner way of doing this, separate the calculation logic out of building the dict? Thanks.
Aucun commentaire:
Enregistrer un commentaire