[reportlab-users] pycanvas.py

Christoph Zwerschke cito at online.de
Thu Nov 28 08:12:15 EST 2013


Am 28.11.2013 12:15, schrieb Robin Becker:

> It is a module that implements a reporting proxy with a canvas that

> records all access calls and attribute settings etc etc. This fails

> under python3 because of differences between old and new style classes.


That difference is explained here:
http://docs.python.org/2/reference/datamodel.html

Essentially, what you need to do is add some more of the special methods
to the PDFAction class, i.e. implement __str__ in the same way as it is
done for __hash__ already.

I also wonder whether the PDFAction class can be simplified like this:

###

class PDFAction(object):
"""Base class to fake method calls or attributes on Canvas"""

def __init__(self, parent, action) :
self.__attr = getattr(parent._underlying, action)

def __str__(self):
return str(self.__attr)

def __repr__(self):
return repr(self.__attr)

def __getattr__(self, name):
"""Probably a method call on an attribute, returns real one."""
return getattr(self.__attr, name)

def __call__(self, *args, **kwargs) :
"""The fake method is called, print it then call real one."""
if not self._parent._parent._in :
self._precomment()
self._postcomment()
self._parent._parent._in += 1
retcode = self.__attr(*args, **kwargs)
self._parent._parent._in -= 1
return retcode

def __hash__(self) :
return hash(self.__attr)

def _precomment(self) :
print('%s(__dict__=%s)._precomment()' %
(self.__class__.__name__,repr(self.__dict__)))

def _postcomment(self) :
print('%s(__dict__=%s)._postcomment()' %
(self.__class__.__name__,repr(self.__dict__)))

###

-- Christoph


More information about the reportlab-users mailing list