[reportlab-users] How to inherit LinePlot class and add attributes?

Robin Becker reportlab-users@reportlab.com
Wed, 17 Sep 2003 19:50:58 +0100


In article <41747.199.169.240.132.1063820410.squirrel@svr1.turboweb.net>
, reportlab@sarcastic-horse.com writes
>Hi-
>
>I'm working on a time-series line plot.  I want to add an attribute to the
>LinePlot class, so I tried to inherit the class and just add a few new
>attributes.
>
>However, the whole thing didn't work, as you can tell here:
>
>>>> LinePlot
><class reportlab.graphics.charts.lineplots.LinePlot at 0x00D5B1B0>
>>>> class timeseriesLinePlot(LinePlot):
>       def __init__(self):
>               LinePlot.__init__(self)
>               self.startdate = None
>               self.enddate = None
>
>
>>>> ts = timeseriesLinePlot()
>
>Traceback (most recent call last):
>  File "<pyshell#42>", line 1, in -toplevel-
>    ts = timeseriesLinePlot()
>  File "<pyshell#41>", line 4, in __init__
>    self.startdate = None
>  File "c:\python23\lib\reportlab\graphics\widgetbase.py", line 50, in
>__setattr__
>    validateSetattr(self,name,value)
>  File "c:\python23\lib\reportlab\lib\attrmap.py", line 81, in
>validateSetattr
>    raise AttributeError, "Illegal attribute '%s' in class %s" % (name,
>obj.__class__.__name__)
>AttributeError: Illegal attribute 'startdate' in class timeseriesLinePlot
>>>>
>
>
>What am I doing wrong?  I just want to be able to set the start and dates
>of my chart. as attributes of the chart itself.
>
>I realize that this probably should occur at the xAxis level, rather than
>at the lineplot level, but I'm going to worry about that issue later.
Either try as Oliver says and turn off the shape checking, or use
attributes that start with an _ (these are let through) or add some
checked attributes to your derived class.

from reportlab.lib.validators import *
from reportlab.lib.attrmap import *
class timeseriesLinePlot(LinePlot):
        _attrMap = AttrMap(BASE=LinePlot,
                startdate = AttrMapValue(None, desc='start date of
plot'),
                enddate = AttrMapValue(None, desc='end date of plot'),
                )

        def __init__(self):
                LinePlot.__init__(self)
                self.startdate = None
                self.enddate = None

the above would allow all assignments to startdate & enddate, but you
can make the first argument of the AttrMapValues a checking function to
at least allow you to prevent assigning the wrong kind of object to
them. Have a look in validators to see what kinds of existing validators
are available.
-- 
Robin Becker