Simeon Leyzerzon
Simeon Leyzerzon

Reputation: 19076

How to split a statement into multiple lines?

What would be the proper Pythonic way to break the first line of the below expression (to appear on multiple lines) so it can be more readable:

if props.getProperty("app.auth.idp.strategy") == '' or props.getProperty("app.auth.idp.strategy") == 'saml/simpleSAMLphp' and PROXYING_MECHANISM == "ngrok":
    IDP_STRATEGY = "saml/simpleSAMLphp"
elif props.getProperty("app.auth.idp.strategy") == 'saml/gsuite':
    IDP_STRATEGY = "saml/gsuite"
elif props.getProperty("app.auth.idp.strategy") == 'saml/remote-simpleSAMLphp':
    IDP_STRATEGY = "saml/remote-simpleSAMLphp"
else:
     IDP_STRATEGY = "saml"

Upvotes: 0

Views: 164

Answers (4)

chepner
chepner

Reputation: 530960

Refactor

I would start by not calling props.getProperty("app.auth.idp.strategy") repeatedly. Call it once, and you immediately have less reason to split any lines.

strategy = props.getProperty("app.auth.idp.strategy")
if not strategy or strategy == 'saml/simpleSAMLphp' and PROXYING_MECHANISM == "ngrok":
    IDP_STRATEGY = "saml/simpleSAMLphp"
elif strategy == 'saml/gsuite':
    IDP_STRATEGY = "saml/gsuite"
elif strategy == 'saml/remote-simpleSAMLphp':
    IDP_STRATEGY = "saml/remote-simpleSAMLphp"
else:
     IDP_STRATEGY = "saml"

Line continuation

For the first long line, your options are line continuation, either explicit:

if not strategy or \
   strategy == 'saml/simpleSAMLphp' and \
   PROXYING_MECHANISM == "ngrok":

or implicit, inside parentheses:

if (not strategy or
    strategy == 'saml/simpleSAMLphp' and
    PROXYING_MECHANISM == "ngrok"):

Use lookup, not repeated comparisons

An even better option is to replace a long string of comparisons with a dict lookup:

strategies = {
    "saml/gsuite": "saml/gsuite",
    "saml/remote-simpleSAMLphp": "saml/remote-simpleSAMLphp",
}
if PROXYING_MECHANISM == "ngrok":
    strategies['saml/simpleSAMLphp'] = 'saml/simpleSAMLphp'

IDP_STRATEGY = strategies.get(props.getProperty("app.auth.idp.strategy"), "saml")

And because each key of the dict is just mapped to itself, you can replace that with a simple set lookup.

strategies = {
    "saml/gsuite",
    "saml/remote-simpleSAMLphp",
}
if PROXYING_MECHANISM == "ngrok":
    strategies.add('saml/simpleSAMLphp')

IDP_STRATEGY = "saml"

strategy = props.getProperty("app.auth.idp.strategy")
if strategy in strategies:
    IDP_STRATEGY = strategy

Take your pick which of the last two you find more readable. The dict is more redundant in its definition, but allows a single assignment to IDP_STRATEGY.

Upvotes: 3

Ernie Yang
Ernie Yang

Reputation: 114

You can either break it explicitly with '\' or break it by putting the condition inside a parentheses

if (a
    ==b
    ==c):

Upvotes: 0

D Dhaliwal
D Dhaliwal

Reputation: 552

Probably something like this as per PEP8

if props.getProperty("app.auth.idp.strategy") == '' \
        or props.getProperty("app.auth.idp.strategy") == 'saml/simpleSAMLphp' \
        and PROXYING_MECHANISM == "ngrok":
    IDP_STRATEGY = "saml/simpleSAMLphp"
elif props.getProperty("app.auth.idp.strategy") == 'saml/gsuite':
    IDP_STRATEGY = "saml/gsuite"
elif props.getProperty("app.auth.idp.strategy") == 'saml/remote-simpleSAMLphp':
    IDP_STRATEGY = "saml/remote-simpleSAMLphp"
else:
     IDP_STRATEGY = "saml"

Upvotes: 1

W Stokvis
W Stokvis

Reputation: 1439

prop_var = props.getProperty("app.auth.idp.strategy")    
if prop_var == '' or prop_var == 'saml/simpleSAMLphp' and PROXYING_MECHANISM == "ngrok":
        IDP_STRATEGY = "saml/simpleSAMLphp"
    elif prop_var == 'saml/gsuite':
        IDP_STRATEGY = "saml/gsuite"
    elif prop_var == 'saml/remote-simpleSAMLphp':
        IDP_STRATEGY = "saml/remote-simpleSAMLphp"
    else:
         IDP_STRATEGY = "saml"

You add a \ at the end of each line as stated in the comment. You can also replace each getProperty("app.auth.idp.strategy") with a variable so it's called once.

Upvotes: 1

Related Questions