2016-07-28 30 views
0

Ich habe ein Szenario, in dem der Benutzer mehrere Optionen übergeben kann. Für jede übergebene Option erhalte ich den Text und füge den Text schließlich aus mehreren Optionen zusammen und gebe eine einzelne Zeichenfolge zurück. Hier ist, wie ich es für drei Optionen mache, die ich heute akzeptiere. Der Code sieht bereits wartbaren und als ich mehr Optionen hinzufügen, wird die Logik noch schlimmer:besser wartbare Möglichkeit, Strings aus mehreren Optionen zu kombinieren

if (len(self.options.passedin.split(",")) > 0): #multiple options were passed in 
    ops = self.options.passedin.split(",") 
    for op in ops: 
    if (op == "option1"): 
     op1_text = get_text_for_option1() 
    elif (op == "option2"): 
     op2_text = get_text_for_option2() 
    elif (op == "option3"): 
     op3_text = get_text_for_option3() 

    #all three were passed in 
    if ("option1" in ops and "option2" in ops and "option3" in ops): 
     op1_op2 = op1_text + " " + ' '.join(w for w in op1_text.split() if w not in op2_text.split()) 
     op3_op1_op2 = op1_op2 + " " + ' '.join(w for w in op1_op2.split() if w not in op3_text.split()) 
     return op3_op1_op2 
    #option1 and option2 were passed in 
    elif ("option1" in ops and "option2" in ops and "option3" not in ops): 
     return op1_text + " " + ' '.join(w for w in op1_text.split() if w not in op2_text.split()) 
    #option1 and option3 were passed in 
    elif ("option1" in ops and "option3" in ops and "option2" not in ops): 
     return op1_text + " " + ' '.join(w for w in op1_text.split() if w not in op3_text.split()) 
    #option2 and option3 were passed in 
    elif ("option2" in ops and "option3" in ops and "option1" not in ops): 
     return op2_text + " " + ' '.join(w for w in op2_text.split() if w not in op3_text.split()) 

Die Methoden get_text_for_option1get_text_for_option2get_text_for_option3 können nicht kombiniert werden.

+0

Warum brauchen Sie, sie zu kombinieren ? – polku

+1

Ich sehe hier einige unnötige/falsche Dinge ('len (something)> 0' anstelle von' something', '&&' anstelle von 'and' vor dem Edit,' option2 'in ops' und 'option2 not in ops' für die letzte Option ...). Ich schlage vor, noch etwas Zeit zu nehmen, um alles umzugestalten und zu verbessern, was immer Sie zuerst tun können. – TigerhawkT3

+0

Wenn ich später zwei weitere Optionen hinzufüge, wird die 'if/elif'-Logik immer schlechter. – Anthony

Antwort

2

Verwenden Sie ein dict Ihre Optionsnamen an die entsprechende Funktion zur Karte, die die Option Text zurückgibt, kommen diejenigen zusammen, dann nehmen Sie die einzigartigen Wörter, zB:

from collections import OrderedDict 

options = { 
    'option1': get_text_for_option1, 
    'option2': get_text_for_option2, 
    'option3': get_text_for_option3 
} 

input_text = 'option3,option1,option2' 

all_text = ' '.join(options[opt]() for opt in input_text.split(',')) 
unique = ' '.join(OrderedDict.fromkeys(all_text.split())) 
+0

Im Dict können diese Strings durch Funktionsaufrufe ersetzt werden? Zum Beispiel Ersetzen von 'das ist Option eins fünf zwei' mit' get_text_for_option1() ' – Anthony

+1

@Anthony geändert, um Funktionen zu verwenden –

0
if (len(self.options.passedin.split(",")) > 0): #multiple options were passed in 
    ops = self.options.passedin.split(",") 
    opts = { 
     'op1': get_text_for_option1() if 'option1' in ops else ' ' 
     'op2': get_text_for_option2() if 'option2' in ops else ' ' 
     'op3': get_text_for_option3() if 'option3' in ops else ' ' 
    } 
    return (opts['op1'] + opts['op2'] + opts['op3']).strip()