Decorators not worth the effort

Duncan Booth duncan.booth at invalid.invalid
Fri Sep 14 07:26:10 EDT 2012


Jean-Michel Pichavant <jeanmichel at sequans.com> wrote:

> I wrote the following one, used to decorate any function that access
> an equipment, it raises an exception when the timeout expires. The
> timeout is adapted to the platform, ASIC of FPGA so people don't need
> to specify everytime one timeout per platform. 
> 
> In the end it would replace 
> 
> def boot(self, timeout=15):
>     if FPGA:
>         self.sendCmd("bootMe", timeout=timeout*3)
>     else:
>         self.sendCmd("bootMe", timeout=timeout)
> 
> with
> 
> @timeout(15)
> def boot(self, timeout=None):
>     self.sendCmd("bootMe", timeout)
> 
> I wrote a nice documentation with sphinx to explain this, how to use
> it, how it can improve code. After spending hours on the decorator +
> doc, feedback from my colleagues : What the F... !! 
> 

I'd agree with your colleagues. How are you going to ensure that all 
relevant functions are decorated and yet no decorated function ever 
calls another decorated one?

>From the code you posted it would seem appropriate that the adjustment 
of the timeout parameter happen in the `sendCmd()` method itself and 
nowhere else. Alternatively use named values for different categories of 
timeouts and adjust them on startup so instead of a default of `timeout=
15` you would have a default `timeout=MEDIUM_TIMEOUT` or whatever name 
is appropriate.

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list