Passing additional arguments to event listeners ?

178 views
Skip to first unread message

Łukasz Czuja

unread,
Dec 1, 2011, 7:24:34 PM12/1/11
to sqlalchemy
Hi,

I do not see anywhere in the docs a way to pass custom attributes to
event listeners:

event.listen(cls, 'before_insert', before_insert_listener, arg1, arg2,
kwarg1 = 'value', kwarg2 = 'value2')

so that the before_insert_listener can have mixed signature:

def before_insert_listener(mapper, connection, target, arg1, *args,
**kwargs):

the only other solution would be to store extra processing information
on the 'target' itself.

Should be reasonable if there is not another way to pass around
arguments. Should I open a ticket then?

Thanks

Michael Bayer

unread,
Dec 1, 2011, 7:52:15 PM12/1/11
to sqlal...@googlegroups.com

this kind of pollutes the API with kwargs that might be needed for the listen() function itself someday, these are external use cases that are easily handled in Python:

def before_insert_listener(arg1, arg2, k1='value', k2='value'):
def before_insert(mapper, conn, target):
... body
return before_insert

event.listen(cls, 'before_insert', before_insert_listener(arg1, arg2, k1='x', k2='y'))


Tate Kim

unread,
Dec 1, 2011, 8:33:01 PM12/1/11
to sqlal...@googlegroups.com
How about functools.partial ?
As far as I know, functools.partial will simply do this.

Usually, I import this when I need to make an argument-less function equip extra arguments.


Best regards,
Tate
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to sqlal...@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.

Jackson, Cameron

unread,
Dec 1, 2011, 8:44:26 PM12/1/11
to sqlal...@googlegroups.com
Not sure if this helps or not, but how about using a lambda that that calls your function with the arguments you want? This is the solution I've been using for passing arguments to wxPython event handlers. This tutorial might help: http://wiki.wxpython.org/Passing%20Arguments%20to%20Callbacks


-------------------------------------------------------------------------
DISCLAIMER: This e-mail transmission and any documents, files and
previous e-mail messages attached to it are private and confidential.
They may contain proprietary or copyright material or information that
is subject to legal professional privilege. They are for the use of
the intended recipient only. Any unauthorised viewing, use, disclosure,
copying, alteration, storage or distribution of, or reliance on, this
message is strictly prohibited. No part may be reproduced, adapted or
transmitted without the written permission of the owner. If you have
received this transmission in error, or are not an authorised recipient,
please immediately notify the sender by return email, delete this
message and all copies from your e-mail system, and destroy any printed
copies. Receipt by anyone other than the intended recipient should not
be deemed a waiver of any privilege or protection. Thales Australia
does not warrant or represent that this e-mail or any documents, files
and previous e-mail messages attached are error or virus free.

-------------------------------------------------------------------------

Tate Kim

unread,
Dec 1, 2011, 8:56:05 PM12/1/11
to sqlal...@googlegroups.com
Yes, I think also lambda can be good one.

Though lambda reduces an effort to type a predefined function, functools.partial is a bit familiar to me.

Łukasz Czuja

unread,
Dec 4, 2011, 4:31:30 AM12/4/11
to sqlalchemy
Thank you all for tips. I'll probably stick to the functools method
though.

I recommend adding a paragraph about this to docs as more people will
stumble upon this as this was possible with the extension system (one
could pass additional vars to extension constructor and use them in
callbacks) and no longer (directly) with events.

cheers.

> For more options, visit this group athttp://groups.google.com/group/sqlalchemy?hl=en.


>
> --
> You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
> To post to this group, send email to sqlal...@googlegroups.com.
> To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.

> For more options, visit this group athttp://groups.google.com/group/sqlalchemy?hl=en.

Reply all
Reply to author
Forward
0 new messages