I have a sequence of messages in json and they include a field called part
, which is an integer from 0 to 2. I have three message queues and the value of part
determines which queue I send the message over to.
This is my current code.
output0 = queue.Queue()
output1 = queue.Queue()
output2 = queue.Queue()
json = json.loads('{"test": "message", "part": "2"}')
part = int(json["part"])
if part == 0:
output0.put(json)
elif part == 1:
output1.put(json)
elif part == 2:
output2.put(json)
I'd like to simplify it with something like.
chooseQueue = "output" + str(json["part"])
chooseQueue.put(json)
It serves me this error AttributeError: 'str' object has no attribute 'put'
In R, I can use a string as a variable name by using as.formula()
or get()
.