Python Forum

Full Version: Call and execute methods from a list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In command line I give as arguments script --args startApp() login() buyProduct() for example. I have a method that generates a list by given args wich are actually methods that define actions and will look like this in that case [startApp(),login(),buyProduct()].

What I have inside firstTest now, does not work. It does not throw errors, but it simply does nothing and stops.

class Tests(unittest.TestCase):

    def argsCollector(self):
        argumentsList = list()
        for i in sys.argv:
            argumentsList.append(i)
        argumentsList.pop(0)
        return argumentsList

    def firstTest(self):
        self.argsCollector()

def hagerTests():
    suite = unittest.TestSuite()
    suite.addTest(TestSuites('firstTest'))
    return suite

if __name__== "__main__":
    runner.run(hagerTests())
From here I want to give the list to the firstTest method then execute the suite that contains only one test firstTest(). Then this method when I execute the code/test should have something like this:

def firstTest():
    #somehow recive the list of steps given in cmd described above
    #then if the list received is succesfull the test can be executed because it will contain something like this
    #startApp()
    #login()
    #buyProduct()  
So how can I call and execute the methods inside my list that are given as args in cmd?
I have almost reached a solution.
class TestSuites(unittest.TestCase):

    def firstHagerTest(self):
        argumentsList = [] #creates empty list
        for i in sys.argv: #iterates trough arguments and adds them to list
            argumentsList.append(i) 
        argumentsList.pop(0) #remove first argument because it's the path to the script by default
        HagercadLogger.Logger.Log(HagercadLogger.LEVEL_WARNING, "PRINT MY ARGS LIST: " + ', '.join(argumentsList)) #prints the list     
        #map(str, argumentsList)
        #results = [f() for f in argumentsList]
        try:
            func_to_run = globals()[argumentsList] #i have to find a way to make this line work as the line func_to_run2 somehow, and of course no matter the no. of elements
            #func_to_run2 = globals()[HagercadUtilities.Utilities.startApp(), HagercadSteps.Steps.createNewProject()] #this work ok 
        except KeyError:
           pass
If I execute the script with func_to_run2 where the steps are harcoded it works. But if I run with my list inside wich also contains the same steps i get and error like this:
TypeError: unhashable type: 'list'

What could be the problem?
Also if i run it like this:
func_to_run = globals()[argumentsList[0],argumentsList[1]], i have no error but nothing happens so the methods are not called.

Does anyone have an idea about this? Or a solution I could try?

newStrList = [x.encode('UTF8') for x in argumentsList]
try:
#print(newStrList)
getattr(yourFile.yourClass,newStrList[0])()
getattr(yourFile.yourClass,newStrList[1])()
except KeyError:
pass


This is the solution that works for me. Of course you can iterate through the list but just to showcase the code.