Hi! Ich habe eine Klassen-Funktion die langsam ins Uferlose wägst. Es handelt sich um eine abgeleitet Klasse von xml.parsers.expat und dessen Tag-Funktionen. Sie besteht mittlerweile auf fast 600 Zeilen elif-Anweisungen. Jetzt habe ich überlegt wie ich das aufdröseln kann um daraus mehere Klassen mit mehren Funktionen zu machen. Ich bin jetzt zu folgendem Ergebnis gekommen, wo bei ich sagen muss, das ich auch die elif Anweisungen hatte völlig weglassen können, wenn ich noch eine weiter Klasse gemacht hätte, die die zuständigen Objekte 'ausspuckt' aber ich glaube dann hätte die Lesbarkeit des Codes gelitten. Aber vielleicht fällt euch ja noch was ganz geniales ein... Der Code als 'Robinsonade': class unter_class1: """ Die Klasse repräsentiert einen Namensraum 'obj_eins' """ def print_func_1(self,arg): print "Class 1 Func1 arg: ",arg def print_func_2(self,arg): print "Class 1 Func2 arg: ",arg def print_func_3(self,arg): print "Class 1 Func3 arg: ",arg class unter_class2: """ Die Klasse repräsentiert einen Namensraum 'obj_zwei' """ def print_func_1(self,arg): print "Class 2 Func 1 arg: ",arg def print_func_2(self,arg): print "Class 2 Func 2 arg: ",arg def print_func_3(self,arg): print "Class 2 Func 3 arg: ",arg # Die Funktion dieser beiden Klassen waren zuvor # in einer class test: def var_func(self, arg1, arg2): if arg1 == "obj_eins": func = getattr(c1, "print_" + arg2) elif arg1 == "obj_zwei": func = getattr(c2, "print_" + arg2) return func c1 = unter_class1() c2 = unter_class2() t = test() t.var_func("obj_eins","func_1")("a") # Der tag hätte lauten können : <obj_eins:func_1 wert='a'> t.var_func("obj_eins","func_2")("b") # Der tag hätte lauten können : <obj_eins:func_2 wert='b'> t.var_func("obj_eins","func_3")("c") # usw. t.var_func("obj_zwei","func_1")("a") t.var_func("obj_zwei","func_2")("b") t.var_func("obj_zwei","func_3")("c") # Gruß Olaf _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
Warum machst Du keine Sprungtabelle: def method1(args): ... def method2(args). .. table = { 'v1' : method1, 'v2' : method2, } method = table.get(somevalue) method(args) Andreas --On Mittwoch, 21. April 2004 9:17 Uhr +0200 Olaf 'Rübezahl' Radicke <olaf_rad@gmx.de> wrote:
Hi!
Ich habe eine Klassen-Funktion die langsam ins Uferlose wägst. Es handelt sich um eine abgeleitet Klasse von xml.parsers.expat und dessen Tag-Funktionen.
Sie besteht mittlerweile auf fast 600 Zeilen elif-Anweisungen. Jetzt habe ich überlegt wie ich das aufdröseln kann um daraus mehere Klassen mit mehren Funktionen zu machen. Ich bin jetzt zu folgendem Ergebnis gekommen, wo bei ich sagen muss, das ich auch die elif Anweisungen hatte völlig weglassen können, wenn ich noch eine weiter Klasse gemacht hätte, die die zuständigen Objekte 'ausspuckt' aber ich glaube dann hätte die Lesbarkeit des Codes gelitten.
Aber vielleicht fällt euch ja noch was ganz geniales ein...
Der Code als 'Robinsonade':
class unter_class1: """ Die Klasse repräsentiert einen Namensraum 'obj_eins' """ def print_func_1(self,arg): print "Class 1 Func1 arg: ",arg
def print_func_2(self,arg): print "Class 1 Func2 arg: ",arg
def print_func_3(self,arg): print "Class 1 Func3 arg: ",arg
class unter_class2: """ Die Klasse repräsentiert einen Namensraum 'obj_zwei' """ def print_func_1(self,arg): print "Class 2 Func 1 arg: ",arg
def print_func_2(self,arg): print "Class 2 Func 2 arg: ",arg
def print_func_3(self,arg): print "Class 2 Func 3 arg: ",arg
# Die Funktion dieser beiden Klassen waren zuvor # in einer
class test:
def var_func(self, arg1, arg2): if arg1 == "obj_eins": func = getattr(c1, "print_" + arg2) elif arg1 == "obj_zwei": func = getattr(c2, "print_" + arg2) return func
c1 = unter_class1() c2 = unter_class2()
t = test()
t.var_func("obj_eins","func_1")("a") # Der tag hätte lauten können : <obj_eins:func_1 wert='a'>
t.var_func("obj_eins","func_2")("b") # Der tag hätte lauten können : <obj_eins:func_2 wert='b'>
t.var_func("obj_eins","func_3")("c") # usw. t.var_func("obj_zwei","func_1")("a") t.var_func("obj_zwei","func_2")("b") t.var_func("obj_zwei","func_3")("c")
# Gruß Olaf
_______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
_______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
Andreas Jung wrote:
Warum machst Du keine Sprungtabelle:
Wenn man die Funktionen nach den eingabe-Strings (hier tags) benennen kann, kann man sich die Sprungtabelle sparen. Denn nach Murphy vergisst man bestimmt, die immer mitzuziehen. Die folgende Idee habe ich von John Acock ('spark'): def f_aaa1(...): ... def f_aaa2(...): ... def call(name, *args) # lookup function f_<name> in this module THIS = inspect.getmodule(call) func = getattr(THIS, 'f_%s' % op) func(*args) -- Regards Hartmut Goebel | Hartmut Goebel | We build the crazy compilers | | h.goebel@crazy-compilers.com | Compiler Manufacturer | _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
On Wed, 2004-04-21 at 10:08, Hartmut Goebel wrote:
Wenn man die Funktionen nach den eingabe-Strings (hier tags) benennen kann, kann man sich die Sprungtabelle sparen. Denn nach Murphy vergisst man bestimmt, die immer mitzuziehen.
Die folgende Idee habe ich von John Acock ('spark'):
def f_aaa1(...): ... def f_aaa2(...): ...
def call(name, *args) # lookup function f_<name> in this module THIS = inspect.getmodule(call) func = getattr(THIS, 'f_%s' % op) func(*args)
Oder: def call(name, *args): # lookup function f_<name> in this module globals()['f_%s' % name](*args) -- Michael Haggerty JPK Instruments, AG haggerty@jpk.com _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
Ich habe mich jetzt für eine Mischung entschieden: class unter_class1: """ Die Klassse representiert einen Namensraum 'obj_eins' """ def print_func_1(self,arg): print "Class 1 Func1 arg: ",arg def print_func_2(self,arg): print "Class 1 Func2 arg: ",arg def print_func_3(self,arg): print "Class 1 Func3 arg: ",arg class unter_class2: """ Die Klassse representiert einen Namensraum 'obj_zwei' """ def print_func_1(self,arg): print "Class 2 Func 1 arg: ",arg def print_func_2(self,arg): print "Class 2 Func 2 arg: ",arg def print_func_3(self,arg): print "Class 2 Func 3 arg: ",arg # Die Funktionn dieser beiden Klassen waren zuvor # in einer o_liste = {} o_liste["obj_eins"] = unter_class1() o_liste["obj_zwei"] = unter_class1() class test: def var_func(self, arg1, arg2): return getattr(o_liste[arg1], "print_" + arg2) t = test() t.var_func("obj_eins","func_1")("a") # Der tag hätte lauten können : <obj_eins:func_1 wert='a'> t.var_func("obj_eins","func_2")("b") # Der tag hätte lauten können : <obj_eins:func_2 wert='b'> t.var_func("obj_eins","func_3")("c") # usw. t.var_func("obj_zwei","func_1")("a") t.var_func("obj_zwei","func_2")("b") t.var_func("obj_zwei","func_3")("c") _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
participants (4)
-
Andreas Jung
-
Hartmut Goebel
-
Michael Haggerty
-
Olaf 'Rübezahl' Radicke