Python - How to add object to a list after each getter (how to repeat similar logic in getters)

F

Friday Sky

Guest
# Message Object Creation File
messageList = []

def getMessageObjectA():
msg = MessageCreator(msgAttribute1, msgAttribute2)
msgList.append(msg)
return msg

def getMessageObjectB():
msg = MessageCreator(msgAttribute3, msgAttribute4)
msgList.append(msg)
return msg

def getMessageObjectC():
msg = MessageCreator(msgAttribute5, msgAttribute6)
msgList.append(msg)
return msg

def clearMessages():
for msg in messageList:
# logic to clear messages


# Test Script #1
import MessageObjects as MsgObj

a = MsgObj.getMessageObjectA()
c = MsgObj.getMessageObjectC()

# Do stuff

MsgObj.clearMessages()

# Do more stuff



# Test Script #223423423
import MessageObjects as MsgObj

e = MsgObj.getMessageObjectE()
u = MsgObj.getMessageObjectU()
y = MsgObj.getMessageObjectY()

# Do stuff

MsgObj.clearMessages()

# Do more stuff



In the actual code, I will have over a hundred getMessageObject() functions. And in certain places, I will only call some of those getMessageObject() functions depending on what is needed, which is why I have those getters.

Adding this line msgList.append(msg) inside every function introduces human programming error and possibly unnecessarily adds to the length of the source code file.

How do I have every getter call msgList.append(msg)? Is there some sort of fancy way to wrap all of this logic in a wrapper function that I'm not thinking of? I'm pretty sure decorators won't work because they don't see the variables inside the function, and I would have to repeat those decorators too for every function I make.

NOTE: Answer has to be in Python2. Can't use Python3 at work.

NOTE: The intent is for these getters() to be inside a Constants-like file, where our many different test scripts call these getters.

Continue reading...
 
Top