Trying to determine if you should try to use a decorator or some other Pythonic method to shorten the code that performs many of my functions. I would like these functions to be able to call only one function at the beginning of each function or to somehow "decorate" the beginning of each function. I have never used a decorator before and am struggling to implement this decorating idea in a pythonic way to reduce the overall set of access code for each function.
I have many functions that will perform the same set of steps at the beginning of a function. However, there are some structural problems of common code that make this decorator idea difficult:
All functions are in the child class of the parent class.
General commands between function reference names that are function specific (but a subset of the function name).
General commands must return to the caller and not perform any more child functions if a certain condition is met. (block "if jobj:" in the sample code)
For examples with variables / attributes, the child function get_nas_server (self) will use the variable variables "nas_server" in the general set of code. Subtracting get_ from the function name shows the base of the variable name that will be used in the general set of code. Examples of variable names and attributes of objects obtained from the function name "get_nas_server":
nas_server
nas_server.json
self.nas_server (attribute)
Here is the generic code from one of the functions:
Anything below the above code in a function is specific to the purpose of the function and is not suitable for discussion here. Basically, I am trying to make all of this code higher than reusable in my functions, but the code should have variables and attribute changed depending on the name of the function.
Thank you for reading, if you succeeded, and thanks for the help.
source share