A Property to store Python objects (API).
Introduction
The problem to solve here is how to decorate a DBo in Python code. In the C++ realm, Hurricane provides the Property mechanism. But a Property is a template that needs to be defined at compile time, whether a Python object is only known at run time.
So, instead of trying to create one property per Python attribute, we create one PyHolderProperty property, which has one PyAttributesHolder attribute (a basic derived class of PyObject with only a dictionnary). All the Python attributes of the DBo are then stored as attributes of PyAttributesHolder.
Finally, to make the Python syntax straigthforward, we modify the PyTypeEntity.tp_setatto and PyTypeEntity.tp_getattro so that when trying to access an attribute, we get redirected towards the PyAttributesHolder (which is another PyObject). So we can write the following code:
class MyAttribute ( object ):
count = 0
def __init__ ( self ):
self.value = MyAttribute.count
print( '{} has been created'.format(self) )
MyAttribute.count += 1
def __del__ ( self ):
print( '{} has been deleted'.format(self) )
def __str__ ( self ):
return '<MyAttribute {}>'.format(self.value)
def demoAttributes ( cell ):
PythonAttributes.enable( cell )
cell.myAttribute0 = MyAttribute()
cell.myAttribute1 = MyAttribute()
print( 'cell.myAttribute0 =', cell.myAttribute0 )
del cell.myAttribute0
PythonAttributes.disableAll()
Some interresting references concerning the Python/C API: