2016-03-24 11 views
1

elif文を書くより効率的な方法があるかどうかを試してみます。クラスが呼び出されるパラメータの数に基づいてURLを生成するためのAPIを記述する。例についてはPythonで大規模なelif文を縮小する必要があります

def Cars(self, model=null, color=null, miles=null) 

    if model == null and color == null and miles ==null: 
     url = cars/ 
    elif model != null and color == null and miles ==null: 
     url = cars/model=%s)% model 
    elif model != null and color != null and miles ==null: 
     url = cars/model=%s/color=%s)% model, color 
    else url = someting 

    return url 

私は10個の以上のパラメータを持っており、すべての組み合わせで、多くのelif文を書きたくない...

+1

文字列の前後に引用符がありません。 –

+3

問題の投稿されたコードは無効ですPython。 –

+0

不器用なコードを私たちの友人に許しましょう。 :P – Marcus

答えて

8

の属性が依存するように表示されません。お互いに;

def cars(self, model=None, color=None, miles=None) 
    url = "cars" 

    if model is not None: 
     url += "/model=%s" % (model,) 
    if color is not None: 
     url += "/color=%s" % (color,) 
    if miles is not None: 
     url += "/miles=%s" % (miles,) 

    return url 

これはおそらく、任意のキーワード引数を受け入れ、特定のセットが存在するかどうかを確認したい実現につながる:

def cars(self, **kwargs): 
    url = "cars" 
    for kw in ["model", "color", "miles"]: 
     if kwargs.get(kw) is not None: 
      url += "/%s=%s" % (kw, kwargs[kw]) 
    return url 

これは、かどうかの問題を無視してそれぞれを個別に扱います実際に構築している文字列は有効なURLではありません。

+0

ありがとうございました応答..あなたのアプローチを使用して..はい、例としてURLを使用した.. –

0

あなたは、おそらくこのような何かを行うことができます:

def Cars(self, model=null, color=null, miles=null) 

    url = "cars/" 
    if (model) : 
     url += "/models=%s" % model 

    if (color) : 
     url += "/color=%s" % color 

    if (miles) : 
     url += "/miles=%s" % miles 

    return url 

をこのようにそれを書くには、組み合わせ論を使用する必要がなくなります。あなたのコードでは、if..else文が9つあります。ここでは3つしかありません。あなたは、単に各パラメータが文字列をURLに与え、あなたのURLを、パターニングされている場合は

url = "cars" 
if model: 
    url += "/model={}".format(model) 
if color: 
    url += "/color={}".format(color) 
if ... 
0

あなたは、パラメータごとに任意のカスタム書式を必要としない場合
url = 'http://foo.com/' 
if model is not None: 
    url += 'model={}'.format(model) 
if color is not None: 
    url += 'color={}'.format(model) 
if miles is not None: 
    url += 'miles={0:.1f}'.format(model) 

、あなたはこれにそのすべてを折りたたむことができます。

url = 'http://foo.com/' 
for parameter in ['model', 'color', 'miles']: 
    url += '{}={}'.format(parameter, locals()[parameter]) 
+0

ありがとう..これは私の実際のコードではありませんでした.. :)))あなたが推測するかもしれないように.. –

0

、あなたはこのような何かを行うことができます:についてどのようなコードのすべてのあなたの過ちを...無視

0

chepner's answer(およびコメントの他の部分)と同様に、私は キーワード引数を使用することを考えました。しかし、ループのたびにurl += ...を使用するのではなく、一般的にはパラメータをリストに追加し、.join()を使用してパラメータリストが作成されたら最終的な文字列を作成します。そうすれば、文字列を正しく書式設定しているかどうか心配する必要はありません。私はPythonにその頭痛の世話をさせることができます。

これは、同じ出発点を使用した別の方法を示すためにのみ投稿しています。

def cars(**kwargs): # If using as part of a class, would be cars(self, **kwargs) 
    params = [] 
    for key in kwargs.keys(): 
     # Append `key=value` to `params`. 
     if kwargs.get(key): 
      params.append('{!s}={!s}'.format(key, kwargs[key])) 
    url = '/cars?{!s}'.format('&'.join(params)) 
    return url 

print(cars(make='Ford', model='Taurus', year=2000, colors='white')) 
関連する問題