[reportlab-users] get methods for properties
Robin Becker
reportlab-users@reportlab.com
Fri, 11 Jun 2004 09:01:15 +0100
Randall Smith wrote:
> I'm new to reportlab. I've been playing with it and looking at the
> source. I like the design, especially the templates. Thank you for
> opening it to the public; I hope you get useful feedback.
>
> My comment is about getting properties. Maybe I missed it, but I don't
> see a clean way to grab some useful properties. For example, line
> width. I wrote a flowable to draw a horizontal line. I want to set the
> line width and return it back to what it was before the flowable was
> created. Because _lineWidth starts with a _, I'm guessing this is a
> semi-private variable and subject to change (not part of the API). So
> shouldn't canvas have an API attribute or call like canvas.lineWidth or
> canvas.getLineWidth()?
>
> Here is code below:
>
> class HR(Flowable):
> def __init__(self, height):
> Flowable.__init__(self)
> self.height = height
>
>
> def __repr__(self):
> return "HR(t=%s)" % (self.height,)
>
>
> def draw(self):
> orig_linewidth = self.canv._lineWidth # my problem is here
> self.canv.setLineWidth(height)
> self.canv.line(0, 0, 100, 0)
> self.canv.setLineWidth(orig_linewidth)
>
> Randall
.....
I don't thinks there's anything wrong with reading _lineWidth and
friends, but the _ indicates it shouldn't be set. In modern Python we
could use a property to allow reading and writing with the appropriate
methods called on the underlying _ value.
On the other hand the platypus/flowables framework allows you to make
any changes you like in your draw method. Each call to a draw method is
surrounded by a save & restore so you can feel free to do what you like
to the canvas inside. That means you don't need to save the linewitdh
yourself.
--
Robin Becker