Next / Previous / Contents / Shipman's homepage

14.10. ElementMaker.__handleArg(): Process one positional argument

This method processes one of the positional arguments when the factory instance is called.

etbuilder.py
# - - -   E l e m e n t M a k e r . _ _ h a n d l e A r g

    def __handleArg(self, elt, arg):
        '''Process one positional argument to the factory instance.

          [ (elt is an et.Element) ->
              if type(arg) is a key in self.__typeMap ->
                elt  :=  elt modified as per self.__typeMap[type(arg)]
              else -> raise TypeError ]
        '''

As a convenience, if the caller passes some callable object, we'll call that object and use its result. Otherwise we'll use the object itself. (This is another Lundh feature, the utility of which I don't fully understand.)

etbuilder.py
        #-- 1 --
        # [ if arg is callable ->
        #     value  :=  arg()
        #   else ->
        #     value  :=  arg ]
        if callable(arg):
            value  =  arg()
        else:
            value  =  arg

Next we look up the value's type in self.__typeMap, and call the corresponding function.

etbuilder.py
        #-- 2 --
        # [ if type(value) is a key in self.__typeMap ->
        #     elt  :=  elt modified as per self.__typeMap[type(value)]
        #   else -> raise TypeError ]
        try:
            handler  =  self.__typeMap[type(value)]
            handler(elt, value)
        except KeyError:
            raise TypeError("Invalid argument type: %r" % value)