db.payment_allocations.insert(**{'payment_ref': payment.id,
ref_field: record.id,
'amount_allocated': allocation})That's not valid Python code
def some_function(the_real_parameter):
print the_real_parameter
parameter_name = 'the_real_parameter'
some_function(parameter_name='hello')TypeError: some_function() got an unexpected keyword argument 'parameter_name'
if session.allocations_table_name == 'task':
db.payment_allocations.insert(payment_ref=payment.id,
task_ref=record.id,
amount_allocated=allocation)
elif session.allocations_table_name == 'invoice':
db.payment_allocations.insert(payment_ref=payment.id,
invoice_ref=record.id,
amount_allocated=allocation)
db.payment_allocations.insert(**{'payment_ref': payment.id,
ref_field: record.id,
'amount_allocated': allocation})
Re:
What's wrong with this approachdb.payment_allocations.insert(**{'payment_ref': payment.id,
ref_field: record.id,
'amount_allocated': allocation})
To be honest I was trying to up my game with a bit of abstraction but haven't used the ** kwargs much (I don't think at all in web2py),
so with my limited understanding I thought it fell into the 'non pythonic' category you mentioned,
I don't think I said anything was "non pythonic" (I identified some invalid code -- but that code literally doesn't work at all, it is not merely non-Pythonic).
The above code was my suggested (working) alternative, and I think the use of dictionary unpacking would be considered Pythonic.