[PATCH 0 of 1] debugcommands.py

Adrian Buehlmann adrian at cadifra.com
Wed May 11 05:48:07 CDT 2011


On 2011-05-11 11:52, Martin Geisler wrote:
> Adrian Buehlmann <adrian at cadifra.com> writes:
>> Or (after having pondered
>> http://www.artima.com/weblogs/viewpost.jsp?thread=240845 ):
>>
>> table = {}
>>
>> class command(object):
>>     def __init__(self, name, options, synopsis):
>>         self.name = name
>>         self.options = options
>>         self.synopsis = synopsis
>>
>>     def __call__(self, func):
>>         table[self.name] = (func, self.options, self.synopsis)
> 
> We'll also need to copy the __doc__ over.

Oh yes. I need to return func on __call__ (as you did already in your
proposal):

  class command(object):
      def __init__(self, name, options, synopsis):
          self.name = name
          self.options = options
          self.synopsis = synopsis

      def __call__(self, func):
          table[self.name] = (func, self.options, self.synopsis)
          return func

For others who learn like I do right now:

As Bruce Eckel notes about decorators *with* arguments: "Notice that
__call__() is now only invoked once, during decoration, and after that
the decorated function that you return from __call__() is used for the
actual calls." [1] (same link as already posted)

(FWIW, I agree with Bruce that class decorators are a bit easier to
understand.)

> If we go this route, then maybe this can be combined with the way normal
> and shell aliases are created. They are already created as cmdalias
> objects in dispatch which are then inserted into commands.table.

Yeah. There might be more fun ahead.

[1] http://www.artima.com/weblogs/viewpost.jsp?thread=240845



More information about the Mercurial-devel mailing list