Ich hielt es für ganze interessant sowas zu machen:
Code: Alles auswählen
#!/usr/bin/python
class CommandParser(object):
def __init__(self):
self.callbacks = {}
def register_callback(self, command, callback):
self.callbacks[command] = callback
def parse(self, string):
tokens = string.split()
command = tokens[0]
if self.callbacks.has_key(command):
self.callbacks[command](tokens[1:])
print
parser = CommandParser()
##############################################################################
#Define Callbacks:
def add(tokens):
print int(tokens[0]) + int(tokens[1])
#Register Callbacks:
parser.register_callback('add', add)
##############################################################################
command = None
while command != 'exit':
command = str(raw_input(''))
if command != '':
parser.parse(command)
So kann man sich seine eigene kleine "Environment" schreiben in dem man einfach seine python scripte/funktion alles andere aufrufen kann.
Man könnte auch ein config.py importieren in dem ein dictionary namens settings importiert ist etc. Je nachdem was man damit vorhat.
Und richtig cool ist es dann wenn man es in Zusammenhang mit xmlrpclib benutzt und es fernsteuern kann

Ihr wisst wodrauf ich rauswill ^^. *verspielt ist*
MfG,
CracKPod