FAQ:How to use DybPythonAlg decorators?

From Daya Bay

Jump to: navigation, search

Decorator is a syntax sugar that makes the code shorter and stands out the key components of the function (PEP 318)

The old way of writing an Algorithm in Python:

class Test(DybPythonAlg):

    def initialize(self):
        status = DybPythonAlg.initialize(self)
        if status.isFailure(): 
            return FAILURE

        self.execNumber = 0

        self.cableSvc =  self.svc('ICableSvc','CableSvc')
        if self.cablSvc == None:
            self.error("Failed to get %s" % (svc,))
            return FAILURE

        return SUCCESS

Most of code in the above example is just doing routine sanity checks. It's boring to write and cluttered to read. In decorator's way, the above code becomes

from DybPython.Util.decorators import register_svc, initialize_alg

class Test(DybPythonAlg):
    
    @register_svc('cableSvc', ('ICableSvc','CableSvc'))
    @initialize_alg
    def initialize(self):
        return SUCCESS

This is a much simplified and cleaner syntax. It let programmer focus more on the actually content of the code.

Personal tools