I am struggling with callback objects, I am a newbie please be nice:
I am using the win32com package to interact with a windows application (The application is not important).
In short what I am trying to achieve is a subscription to a table that updates.
I have successfully implemented a callback that receives the returned data on an update to the table but what I need now is to act on the data received.
This problem would be very easy to solve if I could instantiate the callback object with additional arguments (see code below) But I am at a loss as to how to do this.
CallBack Class:
class callBackEvents(object):
""" Callback Object for win32com
"""
def OnNewData(self, XMLData):
logging.info("Subscription returned information")
print "HERE : {}".format(XMLData))
# Would like to use some argument to access logic
# For how to use the new data
def OnActionResult(self, job, msg):
return True
def OnServerDisconnect(self):
logging.debug("Server Disconnected")
def OnServerConnect(self):
logging.debug("Trader Connected To Server")
Instantiate the Callback Object:
# Instantiate API com object
self.app = win32com.client.DispatchWithEvents("WindowsApplication" callBackEvents)
# I would like to give the callback object extra arguments e.g. callBackEvents(params)
EDIT
Instantiate the Callback Objects:
# Instatiate two com objects
self.com1 = win32com.client.DispatchWithEvents("WindowsApplication" callBackEvents)
self.com2 = win32com.client.DispatchWithEvents("WindowsApplication" callBackEvents)
# Create multiple subscriptions (Note these are asynchronous)
# Pushing the subscribed info is not a problem and done elsewhere
self.com1.Subscribe(<subscription info>)
self.com2.Subscribe(<subscription info>)
Now when subscription info hits the callback object I have no idea which com object set up the subscription (I could guess based on the information returned but this is going to cause problems when identical subscriptions are setup)
Since you likely have only one app instance and therefore one
DispatchWithEvents
, you could simply make the params a member of the class:You could of course make params a global but you should use globals only as a last resort or for constants.
I had the same issue and ended up augmenting DispatchWithEvents. Please see below my solution (which I think is more elegant):
Your handler class will have to have an init function and be ready to accept the arguments in order:
And you would initialize it as such:
As you might have guessed, the application here is for a data science project where incoming emails are automatically classified, but the new DispatchWithEvents method is very generic and accepts a dynamic number of arguments.