Python 格式化时涉及 `format()` 点符号

Python formatting when `format()` dot notation involved

我是 Python 的新手,正在寻找如何根据 PEP8 标准格式化以下代码:

未格式化:

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)

这是我想要做的(执行得很好):

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
             ))

但是这种格式符合PythonPEP8标准吗?让 . 悬在某些行的末尾感觉违反直觉。

根据 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 来帮助你。不同的代码格式化程序有不同的默认设置。例如,由 Black 格式化的代码最终会像这样:

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
)

这是因为在 Black 中,默认的每行最大字符数设置为 88,而 PEP 8 为 79。