0

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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
ericOnline
  • 1,586
  • 1
  • 19
  • 54
  • 1
    If you are new to Python, why are you using Python 3.5? It's seriously outdated. – MisterMiyagi Jul 09 '21 at 16:47
  • 1
    [Databricks 5.5LTS is "stuck" on 3.5](https://docs.databricks.com/clusters/configure.html#will-my-existing-pypi-libraries-work-with-python-3). I'm unwilling to rally and pass an Act of Enterprise Congress to update the cluster :). What are your thoughts on the code format? – ericOnline Jul 09 '21 at 16:56
  • @Nat Riddle it's not 1 year old , it's almost 6 years old ! – Tanish Sarmah Jul 09 '21 at 16:59

1 Answers1

1

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.

itroulli
  • 2,044
  • 1
  • 10
  • 21
  • Thanks for the resources and sanity check. I will pull the code over to VS Code (being written in Databricks Notebook) and see how it formats too. As much as I don't like the dangling `.`, I do like the parameter alignment. – ericOnline Jul 09 '21 at 17:14