I have following structure:
app.py
modules
|
-- __init__.py
|
-- update.py
I try to call function from modules/update.py
dynamicaly:
import sys
arguments = sys.argv
module = arguments[1]
command = arguments[2]
moduleObj = __import__('modules.'+module) # returns module object "modules"
submodule = getattr(moduleObj, module) # returns the module object "modules.update"
moduleClass = getattr(submodule, module) # returns the class object "update"
# this works because the class has the same name as the file it contains it.
# if the names are different then you have to change that accordingly
result = getattr(moduleClass, command) # returns the attribute with the name =command
print result()
but I catch a error:
File "app.py", line 12, in print result() TypeError: unbound method test() must be called with update instance as first argument (got nothing instead)
What's wrong? Help me please!
ps: update.py
code:
import os
class update:
def repositories(self):
os.system('sudo apt-get update')
def system(self):
os.system('sudo apt-get -y upgrade')
def distributive(self):
os.system('sudo apt-get -y dist-upgrade')
def all(self):
self.repositories()
self.system()
self.distributive()
def test(self):
print 'test complete!'