Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I decorate a function?

Tags:

python

Trying to determine if I should try to use decorator or some other Pythonic way to reduce code that many of my functions are doing. I would like these functions to be able to call just maybe one other function at the start of each function or somehow "decorate" the start of each function. I have never used decorator before and am struggling to implement this decorate idea in a pythonic way to reduce the common set of share code at each function.

I have many functions that will perform the same set of steps at the start of the function. However, there is some structure concerns of the common code that makes this "decorator" idea difficult :

  • The functions are all in child class of a parent class.

  • The common commands between the functions reference variable names that are specific to the function (but a subset of the function name).

  • The common commands need to return to the caller and not execute any more of the child function if a certain condition is met. ("if jobj : " block in the sample code)

For variable/attribute examples, child function get_nas_server(self) will utilize "nas_server" variable variants in the common set of code. Subtracting the get_ from the function name reveals the base of the variable name to be used in the common set of code. Example variables names and object attributes derived from "get_nas_server" function name:

nas_server
nas_server.json
self.nas_server (attribute)

Here is the common code from one of the functions:

        ####################################################################
        def get_nas_server(self):
        ####################################################################
            """\nGets COMMAND nas_server and places data into self.nas_server"""

           try:
               self.nas_server
               return self.nas_server
           except AttributeError:
               pass
           self.get_file_cmd('nas_server')
           jobj = self.fresh_json('nas_server.json')
           if jobj :
               self.nas_server = jobj
               return self.nas_server
           self.get_file_cmd('get_nas_server')

Everything below that code above in the function is specific to the function purpose and not appropriate for discussion here. Basically I am trying to make all that code above reusable in my functions, but the code has to have the variables and attribute changed depending on the function name.

Thanks for reading if you got this far and thanks for all help.

like image 791
Fragtzack Avatar asked Oct 19 '22 04:10

Fragtzack


1 Answers

Seems like something you could define as a helper method in the parent class:

class Parent(object):
    def _get_something(name):
        try:
            return getattr(self, name)
        except AttributeError:
            pass
       self.get_file_cmd(name)
       jobj = self.fresh_json(name+'.json')
       if jobj :
           setattr(self, name, jobj)
           return jobj
       self.get_file_cmd('get_'+name)

As this snippet demonstrates, you can use the getattr(), setattr() and hasattr() functions to reference object attributes by name.

like image 120
augurar Avatar answered Nov 03 '22 22:11

augurar