I am testing multiple algorithms, and want to be able to add algorithms quickly. To do so I want to put algorithms in a list as a string, and then use the string to call the method. I’m also doing a simple timing of the algorithms and don’t want to duplicate the timer code between each method.
For example:
JavaScript
x
8
1
testData = (testSet1, testSet2, testSet3, ,testSetN)
2
methods = ("method1", "method2", "method3", ,"methodN")
3
for x in testData:
4
for y in methods:
5
startTime = time.time()
6
#call y with x as parameter life
7
endTime = time.time()
8
I can’t use getattr(y, x)
because everything is written as a simple script. Is there an easy way to do this, or will I need to call each method?
Advertisement
Answer
Use a dictionary
JavaScript
1
8
1
testData = (testSet1, testSet2, testSet3, ,testSetN)`
2
methods = {"method1":method1, "method2":method2, "method3":method3,...,"methodN":methodN}
3
for x in testData:
4
for y in methods.keys():
5
startTime = time.time()
6
methods[y](x)
7
endTime = time.time()
8