Reputation: 610
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]
Upvotes: 1
Views: 4229
Reputation: 3776
To keep your data together and let it extend if you need to you could use this example:
LBMethods=[
("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 show_methods(method_list):
print "Load balance Methods"
print "____________________"
for index, method in enumerate(method_list):
print "%d - %s"%(index+1, method[0])
if __name__=='__main__':
show_methods(LBMethods)
index = input('Please select Load Balance method by number from list above: ')
print LBMethods[index-1][1]
Upvotes: 1
Reputation: 3776
__name__=='__main__'
list[x:x+1]
. Just access list[x]
I did a small rewrite:
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)"
if __name__=='__main__':
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: ')
LBMethod = ListLBMethod[LBMethodSelect-1]
print LBMethod
Upvotes: 1
Reputation: 4043
Why don't you use simple indexing since you only need one item? Pretty sure slices are less efficient than indexes:
LBMethodSelect = int(input('Please select Load Balance method by number from list above: '))
LBMethod = ListLBMethod[LBMethodSelect-1]
Upvotes: 2