Ich hoffe, ihr könnt mir helfen; bitte um Verständnis, da ich mich erst 3 Wochen mit python befasse..
Ich möchte folgendes erreichen: es gibt einen Wrapper, der aufgrund eines Eintrags in einer cfg Datei entweder einen Simulator lädt oder den echten Connector zur HW. Das funkt inzwischen (lines 3-21).
Ausserdem soll es die Möglichkeit geben, dasselbe auch zur Laufzeit zu tun - durch einen Aufruf von wrapperReInit. Gewünscht ist, wenn man einen Aufruf "executor.testit()" macht, der je nachdem real.testit() oder simulator.testit() aufruft. Oder gibt's da schon eine einfachere Möglichkeit?
Code: Alles auswählen
global executor
global a
a = "yes"
##################################################
# select the executor due to a configuration value
# in a configuration file (code deleted for clarity)
print "a = " + a
if a is "Yes":
print "#### using simulator ####"
import simulator
reload(simulator)
executor = simulator
else :
print "#### using real connector to HW ####"
import real
reload(real)
executor = real
def wrapperReInit(entityToAttach):
"""
dynamically imports either real or its simulator
and uses either this or that to execute calls
"""
print "entityToAttach = " + entityToAttach
if entityToAttach is "simulator" :
executor = None
exec "import simulator"
__import__("simulator")
reload(simulator)
executor = simulator
print "reloaded simulator"
return
if entityToAttach == "real" :
executor = None
print "#### using real connector to HW ####"
#sys.path.append("/vobs/nexta/src8/lib/libFmSatTest")
exec "import real"
__import__("real")
reload(real)
executor = real
print "reloaded real"
return
def main():
input = 1
while (input != 0):
if input==1 :
wrapperReInit("simulator")
else:
wrapperReInit("real")
executor.testit()
input = raw_input('Geben Sie eine Zahl ein: ');
input = int(input)
if __name__ == "__main__":
main()