[web2py] Re: Trying to use a variable as a field name in a table insert
> 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. > Absolutely no argument from me Anthony. I simply didn't fully understand - that's on me - but thanks to you, taking the time to explain, I understand more now. If I haven't said it before... *I really do appreciate the time and effort you and others give to the forum and noobs like me - I'd be lost without it!* Incidentally, I may have another query to post, so hoping for some more support shortly! Regards Peter -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[web2py] Re: Trying to use a variable as a field name in a table insert
On Monday, June 12, 2017 at 2:11:12 PM UTC-4, Peter wrote: > > > Re: > What's wrong with this approach > db.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. Anthony -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[web2py] Re: Trying to use a variable as a field name in a table insert
Re: What's wrong with this approach db.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, though I do understand it is passing a dictionary of values as opposed to over-riding an expected variable as in my earlier case. Will give it a go so, thanks Anthony! Thanks Peter -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[web2py] Re: Trying to use a variable as a field name in a table insert
What's wrong with this approach: https://groups.google.com/d/msg/web2py/_VsOlcA2qFk/6whMJl8tAwAJ On Friday, June 9, 2017 at 12:03:00 AM UTC-4, Peter wrote: > > > I see... >> >> TypeError: some_function() got an unexpected keyword argument >> 'parameter_name' >> > > > I was trying to avoid the if else but if it's the better way, so be it > and I have changed the code using a session cookie... > > 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) > > Thanks for talking the time to explain Anthony! > > Peter > > > > > > -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[web2py] Re: Trying to use a variable as a field name in a table insert
I see... > > TypeError: some_function() got an unexpected keyword argument > 'parameter_name' > I was trying to avoid the if else but if it's the better way, so be it and I have changed the code using a session cookie... 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) Thanks for talking the time to explain Anthony! Peter -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[web2py] Re: Trying to use a variable as a field name in a table insert
This is not valid: def some_function(the_real_parameter): print the_real_parameter parameter_name = 'the_real_parameter' some_function(parameter_name='hello') Anthony On Thursday, June 8, 2017 at 2:49:17 PM UTC-4, Peter wrote: > > > Thanks for that Anthony! (I have a feeling of deja vu about that one) > > If you have time can you tell me what you mean... > > That's not valid Python code > > > I'm not sure what it refers to and would like to understand where I am > doing something wrong. > > > Regards > Peter > -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[web2py] Re: Trying to use a variable as a field name in a table insert
Thanks for that Anthony! (I have a feeling of deja vu about that one) If you have time can you tell me what you mean... That's not valid Python code I'm not sure what it refers to and would like understand where I am doing something wrong. Regards Peter -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[web2py] Re: Trying to use a variable as a field name in a table insert
That's not valid Python code, but you can do this: db.payment_allocations.insert(**{'payment_ref': payment.id, ref_field: record.id, 'amount_allocated': allocation}) Anthony On Wednesday, June 7, 2017 at 10:42:28 PM UTC-4, Peter wrote: > > > Good Morning. > > I'm hoping there is a way to solve this... > > This works fine for me... > > db.payment_allocations.insert(payment_ref=payment.id, > task_ref=record.id, > amount_allocated=allocation) > > but I want to add a little abstraction like this... > > ref_field = '%s_ref' % table_name# where resulting field name > will be either 'task_ref' or 'invoice_ref' (both fields are valid in the > insert table). > db.payment_allocations.insert(payment_ref=payment.id, > ref_field=record.id, > amount_allocated=allocation) > > which currently gives an Attribute Error (taking ref_field literally and > not substituting the variable's assigned value) > > Is this possible to achieve what I want? > Any pointers would be appreciated. > > Thanks > Peter > -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.