Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #29145

Re: Decorators not worth the effort

Date 2012-09-14 11:28 +0200
From Jean-Michel Pichavant <jeanmichel@sequans.com>
Subject Re: Decorators not worth the effort
Newsgroups comp.lang.python
Message-ID <mailman.690.1347614888.27098.python-list@python.org> (permalink)

Show all headers | View raw


----- Original Message -----
> On Sep 14, 3:54 am, Jean-Michel Pichavant <jeanmic...@sequans.com>
> wrote:
> > I don't like decorators, I think they're not worth the mental
> > effort.
> 
> Because passing a function to a function is a huge cognitive burden?
> --
> http://mail.python.org/mailman/listinfo/python-list
> 

I was expecting that. Decorators are very popular so I kinda already know that the fault is mine. Now to the reason why I have troubles writing them, I don't know. Every time I did use decorators, I spent way too much time writing it (and debugging it).

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... !! 

Decorators are very python specific (probably exists in any dynamic language though, I don't know), in some environment where people need to switch from C to python everyday, decorators add python magic that not everyone is familiar with. For example everyone in the team is able to understand and debug the undecorated version of the above boot method. I'm the only one capable of reading the decorated version. And don't flame my colleagues, they're amazing people (just in case they're reading this :p) who are not python developers, more of users.

Hence my original "decorators are not worth the mental effort". Context specific I must admit.

Cheers,

JM

PS : Here's the decorator, just to give you an idea about how it looks. Small piece of code, but took me more than 2 hours to write it. I removed some sensible parts so I don't expect it to run.

class timeout(object):
        """Substitute the timeout keyword argument with the appropriate value"""
	FACTORS = {
		IcebergConfig().platform.ASIC : 1,
		IcebergConfig().platform.FPGA : 3,
			}

	def __init__(self, asic, fpga=None, palladium=None):
		self.default = asic
		self.fpga = fpga
	
	def _getTimeout(self):
		platform = config().platform
		factor = self.FACTORS[platform.value]
		timeout = {
				platform.ASIC : self.default*factor,
				platform.FPGA : self.fpga or self.default*factor,
				}[platform.value]
		return timeout

	def __call__(self, func):
		def decorated(*args, **kwargs):
			names, _, _, defaults =  inspect.getargspec(func)
			defaults = defaults or []
			if 'timeout' not in names:
				raise ValueError('A "timeout" keyword argument is required')
			if 'timeout' not in kwargs: # means the timeout keyword arg is not in the call
				index = names.index('timeout')
				argsLength = (len(names) - len(defaults))
				if index < argsLength:
					raise NotImplementedError('This decorator does not support non keyword "timeout" argument')
				if index > len(args)-1: # means the timeout has not be passed using a pos argument
					timeoutDef = defaults[index-argsLength]
					if timeoutDef is not None:
						_log.warning("Decorating a function with a default timeout value <> None")
					kwargs['timeout'] = self._getTimeout()
			else:
				_log.warning('Timeout value specified during the call, please check "%s" @timeout decorator.' % func.__name__)
			ret = func(*args, **kwargs)
			return ret
		return decorated

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: Decorators not worth the effort Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-09-14 11:28 +0200
  Re: Decorators not worth the effort Duncan Booth <duncan.booth@invalid.invalid> - 2012-09-14 11:26 +0000
    Re: Decorators not worth the effort andrea crotti <andrea.crotti.0@gmail.com> - 2012-09-14 15:12 +0100
    Re: Decorators not worth the effort Chris Angelico <rosuav@gmail.com> - 2012-09-15 00:41 +1000
      Re: Decorators not worth the effort 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-14 09:37 -0700
      Re: Decorators not worth the effort 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-14 09:37 -0700
    Re: Decorators not worth the effort andrea crotti <andrea.crotti.0@gmail.com> - 2012-09-14 17:15 +0100
    Re: Decorators not worth the effort Chris Angelico <rosuav@gmail.com> - 2012-09-15 03:30 +1000
      Re: Decorators not worth the effort Neil Cerutti <neilc@norwich.edu> - 2012-09-18 13:19 +0000
        Re: Decorators not worth the effort Chris Angelico <rosuav@gmail.com> - 2012-09-18 23:25 +1000
          Re: Decorators not worth the effort 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-18 08:14 -0700
          Re: Decorators not worth the effort 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-18 08:14 -0700
    RE: Decorators not worth the effort "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-09-14 23:14 +0000
  Re: Decorators not worth the effort Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-14 12:01 +0000
    Re: Decorators not worth the effort Tim Chase <python.list@tim.thechases.com> - 2012-09-14 08:06 -0500
      Re: Decorators not worth the effort Steve Howell <showell30@yahoo.com> - 2012-09-14 18:13 -0700
  Re: Decorators not worth the effort Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-09-14 13:49 +0200

csiph-web