I am using Python in another program that sends and receives many similar messages. Essentially, the program sends an "address" and a "value". To be as concise as possible, I made a dictionary to act as a switch / case statement. However, before I go to far in my code I want to make sure I am writing the most efficient code I can.
Many of the messages are very similar (with only the last digit being different) and I am wondering if there is somehow a way for me to concatenate the observe_volume_(digit)
cases. There are going to be perhaps hundreds of messages told and it seems a little ridiculous to add each one to the dictionary as it is already getting long with six messages.
def observe_volume_0(v):
op('constant4').par.value0 = float(v[0])
def observe_volume_1(v):
op('constant4').par.value1 = float(v[0])
def observe_volume_2(v):
op('constant4').par.value2 = float(v[0])
def track_0_name(v):
op('text1').par.text = v[0]
print(v[0])
def track_1_name(v):
op('text1').par.text = v[0]
print(v[0])
def track_2_name(v):
op('text1').par.text = v[0]
print(v[0])
incoming = {
'/track_0_name/x': track_0_name,
'/track_1_name/x': track_1_name,
'/track_2_name/x': track_2_name,
'/observe_volume_0/x': observe_volume_0,
'/observe_volume_1/x': observe_volume_1,
'/observe_volume_2/x': observe_volume_2,
}
def receive(message, args):
full = message.split()
target = full[0]
incoming[target](args)
return