Search code examples
pythoncode-formattingpep8

Python formatting when `format()` dot notation involved


I'm new to Python, looking for how to format the code below per PEP8 standards:

  • Using Python 3.5 so fstrings are not available.
  • With all the .format(), its tough to know where to split the line.

Unformatted:

hist_df = spark.read.format("delta").table("{table}".format(table=selected_table))
hist_query = hist_df.where(col("status")=='{sel_status}'.format(sel_status=selected_status)).where(col("cret_dt") < '{last_date}'.format(last_date=selected_last_date)).drop("cret_ts", "cret_dt")


file_path = "abfss://{cont}@{acct}.dfs.core.windows.net/{folder}/".format(cont=storage_container, acct=storage_account, folder=selected_folder)

Here is what I want to do (which executes fine):

  • To me, this lines up the hist_query filter parameters nicely
  • Also lines up the file_path format() parameters nicely
hist_df = spark.read.format("delta").table("{table}".format(table=selected_table))
hist_query = (hist_df.
             where(col("status")=='{sel_status}'.format(sel_status=selected_status)).
             where(col("cret_dt") < '{last_date}'.format(last_date=selected_last_date)).
             drop("cret_ts", "cret_dt"))


file_path = ("abfss://{cont}@{acct}.dfs.core.windows.net/{folder}/".
             format(
               cont=storage_container, 
               acct=storage_account, 
               folder=sel_folder
             ))

But is this format in line with Python PEP8 standards? It feels counterintuitive to have the . dangling off the end of some lines.


Solution

  • According to PEP 8 - The official Python style guide, your code seems nicely formatted. Keep in mind though, that some things are a matter of preference. Adopting all of the suggestions is less important than being consistent about it in your code. You can use a code formatter to help you with that. Different code formatter have different default settings. For example, your code formated by Black would end up like this:

    hist_df = spark.read.format("delta").table("{table}".format(table=selected_table))
    hist_query = (
        hist_df.where(col("status") == "{sel_status}".format(sel_status=selected_status))
        .where(col("cret_dt") < "{last_date}".format(last_date=selected_last_date))
        .drop("cret_ts", "cret_dt")
    )
    
    
    file_path = "abfss://{cont}@{acct}.dfs.core.windows.net/{folder}/".format(
        cont=storage_container, acct=storage_account, folder=selected_folder
    )
    

    That's because in Black the default maximum characters per line setting is 88 while for PEP 8 is 79.