Uploaded image for project: 'WiredTiger'
  1. WiredTiger
  2. WT-502

Implement Python event callbacks

    • Type: Icon: Task Task
    • Resolution: Done
    • None
    • Affects Version/s: None
    • Component/s: None

      The Python API does not currently implement event callbacks. It should.

      I propose an API that looks like:

      def myErrorHandler(error):
      	print error
      
      def myMessageHandler(message):
      	print message
      
      def myProgressHandler(message, progress):
      	print message + progress
      
      def myCloseHandler(session, cursor):
      	if cursor:
      		cursor.close()
      	else:
      		session.close()
      
      handlers = namedtuple('EventHandler', 'myErrorHandler, myMessageHandler, myProgressHandler, myCloseHandler')
      conn = wiredtiger.wiredtiger_open("WT_HOME", handlers, "create")
      

      The namedtuple can be implemented as:

      from collections import namedtuple
      from operator import itemgetter
      
      import wiredtiger
      
      # This will become part of wiredtiger.py
      class EventHandler(tuple):
      	'Event Handler function pointers.'
      	__slots__ = ()
      
      	_fields = ('errorHandler', 'messageHandler', 'progressHandler', 'closeHandler')
      
      	def __new__(_cls, errorHandler, messageHandler, progressHandler, closeHandler):
      		'Create a new instance of EventHandler'
      		return _tuple.__new__(_cls, (errorHandler, messageHandler, progressHandler, closeHandler))
      
      	@classmethod
      	def _make(cls, iterable, new=tuple.__new__, len=len):
      		'Make a new EventHandler object from a sequence or iterable'
      		result = new(cls, iterable)
      		if len(result) != 4:
      			raise TypeError('Expected 4 arguments, got %d' % len(result))
      		return result
      
      	def _asdict(self):
      		'Return a new OrderedDict which maps field names to their values'
      		return OrderedDict(zip(self._fields, self))
      
      	__dict__ = property(_asdict)
      
      	def _replace(_self, **kwds):
      		'Return a new EventHandler object replacing specified fields with new values'
      		result = _self._make(map(kwds.pop, ('errorHandler', 'messageHandler', 'progressHandler', 'closeHandler'), _self))
      		if kwds:
      			raise ValueError('Got unexpected field names: %r' % kwds.keys())
      		return result
      
      	def __getnewargs__(self):
      		'Return self as a plain tuple.	Used by copy and pickle.'
      		return tuple(self)
      
      	errorHandler = property(itemgetter(0), doc='Alias for field number 0')
      	messageHandler = property(itemgetter(1), doc='Alias for field number 1')
      	progressHandler = property(itemgetter(1), doc='Alias for field number 2')
      	closeHandler = property(itemgetter(1), doc='Alias for field number 3')
      
      

            Assignee:
            alexander.gorrod@mongodb.com Alexander Gorrod
            Reporter:
            alexander.gorrod@mongodb.com Alexander Gorrod
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: