Fallacy11 Fallacy11 - 7 months ago 9
Python Question

Python - efficiently select from list based on user input

I'm wondering if anyone can help me figure out a more efficient way to select an index out of a list based on user input this is the best way I've come up to do it, the only other way I could think is to manually set the value with a loop. Just to note those print statements are in a function so I can collapse them to make the code more readable in the IDE. Here's what I have, any feedback would be appreciated:

def LBMethodList():
print "Load balance Methods"
print "____________________"
print "1 - Round Robin"
print "2 - Ratio (member)"
print "3 - Least Connections (member)"
print "4 - Observed (member)"
print "5 - Predictive (member)"
print "6 - Ratio (node)"
print "7 - Least Connections (node)"
print "8 - Fastest (node)"
print "9 - Observed (node)"
print "10 - Predictive (node)"
print "11 - Dynamic Ratio (node)"
print "12 - Fastest (application)"
print "13 - Least Sessions"
print "14 - Dynamic Ratio (member)"
print "15 - Weighted Least Connections (member)"
print "16 - Weighted Least Connections (node)"
print "17 - Ratio (session)"
print "18 - Ratio Least Connections (member)"
print "19 - Ratio Least Connections (node)"

class LBMethodConvert:
LBMethodList()
ListLBMethod = ["LB_METHOD_ROUND_ROBIN", "LB_METHOD_RATIO_MEMBER", "LB_METHOD_LEAST_CONNECTION_MEMBER",
"LB_METHOD_OBSERVED_MEMBER", "LB_METHOD_PREDICTIVE_MEMBER", "LB_METHOD_RATIO_NODE_ADDRESS",
"LB_METHOD_LEAST_CONNECTION_NODE_ADDRESS", "LB_METHOD_FASTEST_NODE_ADDRESS",
"LB_METHOD_OBSERVED_NODE_ADDRESS", "LB_METHOD_PREDICTIVE_NODE_ADDRESS", "LB_METHOD_DYNAMIC_RATIO",
"LB_METHOD_FASTEST_APP_RESPONSE", "LB_METHOD_LEAST_SESSIONS", "LB_METHOD_DYNAMIC_RATIO_MEMBER",
"LB_METHOD_L3_ADDR", "LB_METHOD_WEIGHTED_LEAST_CONNECTION_MEMBER",
"LB_METHOD_WEIGHTED_LEAST_CONNECTION_NODE_ADDRESS", "LB_METHOD_RATIO_SESSION",
"LB_METHOD_RATIO_LEAST_CONNECTION_MEMBER", "LB_METHOD_RATIO_LEAST_CONNECTION_NODE_ADDRESS"]
LBMethodSelect = input('Please select Load Balance method by number from list above: ')
MethodConstraint = LBMethodSelect - 1
LBMethod = ListLBMethod[MethodConstraint:LBMethodSelect]

Answer

To keep your data together and let it extend if you need to you could use this example:

ListLBMethod=[
  ("Round Robin", "LB_METHOD_ROUND_ROBIN"), 
  ("Ratio (member)", "LB_METHOD_RATIO_MEMBER"), 
  ("Least Connections (member)", "LB_METHOD_LEAST_CONNECTION_MEMBER"), 
  ("Observed (member)", "LB_METHOD_OBSERVED_MEMBER"), 
  ("Predictive (member)", "LB_METHOD_PREDICTIVE_MEMBER"), 
  ("Ratio (node)", "LB_METHOD_RATIO_NODE_ADDRESS"), 
  ("Least Connections (node)", "LB_METHOD_LEAST_CONNECTION_NODE_ADDRESS"), 
  ("Fastest (node)", "LB_METHOD_FASTEST_NODE_ADDRESS"), 
  ("Observed (node)", "LB_METHOD_OBSERVED_NODE_ADDRESS"), 
  ("Predictive (node)", "LB_METHOD_PREDICTIVE_NODE_ADDRESS"), 
  ("Dynamic Ratio (node)", "LB_METHOD_DYNAMIC_RATIO"), 
  ("Fastest (application)", "LB_METHOD_FASTEST_APP_RESPONSE"), 
  ("Least Sessions", "LB_METHOD_LEAST_SESSIONS"), 
  ("Dynamic Ratio (member)", "LB_METHOD_DYNAMIC_RATIO_MEMBER"), 
  ("Weighted Least Connections (member)", "LB_METHOD_WEIGHTED_LEAST_CONNECTION_MEMBER"), 
  ("Weighted Least Connections (node)", "LB_METHOD_WEIGHTED_LEAST_CONNECTION_NODE_ADDRESS"), 
  ("Ratio (session)", "LB_METHOD_RATIO_SESSION"), 
  ("Ratio Least Connections (member)", "LB_METHOD_RATIO_LEAST_CONNECTION_MEMBER"), 
  ("Ratio Least Connections (node)", "LB_METHOD_RATIO_LEAST_CONNECTION_NODE_ADDRESS"), 
]

def LBMethodList(method_list):
     print "Load balance Methods"
     print "____________________"
     for index, method in enumerate(method_list):
       print "%d - %s"%(index+1, method[0])

if __name__=='__main__':
  LBMethodList(ListLBMethod)
  LBMethodSelect = input('Please select Load Balance method by number from list above: ')
  LBMethod = ListLBMethod[LBMethodSelect-1][1]
  print LBMethod
Comments