On 25 Feb 2014, at 05:01, Eric Kiser <[email protected]> wrote:
> Okay so i'm playing with sqlalchemy and got a problem. here is the code
>
> ........
> def Create(cur_date, trans_status):
>
> new_trans
> = 'Transaction(date=cur_date, status=[Status(%s = 0)])' %(trans_status)
>
>
> session
> .add(new_trans)
>
> session
> .commit()
> Create('1/12/14', 'processed')
>
> NOTE: class Status has processed, rejected and forwarded as keys/parameters
>
> GOAL: create new transaction where Status key would be dependent on the
> input(trans_status)
>
> EX: if user selects status as processed, the Status(processed = 1) should be
> created
>
I'm not sure I really understand your example, but based on the last line, you
could write it out in long form like this:
assert trans_status in ('processed', 'rejected', 'forwarded')
if trans_status == 'processed':
status = Status(processed=1)
elif trans_status == 'rejected':
status = Status(rejected=1)
elif trans_status == 'forwarded':
status = Status(forwarded=1)
new_trans = Transaction(date=cur_date, status=status)
However, Python has a handy shortcut for passing arbitrary keyword arguments to
a function:
status_args = {trans_status: 1}
status = Status(**status_args)
new_trans = Transaction(date=cur_date, status=status)
Hope that helps,
Simon
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.