我試圖將我所有的用戶都面對字符串放入單個文件中,以便更改這些字符串。我正在尋找可讀性方面的最佳做法。我現在有兩個版本的相同的文件,我看到兩個版本的權衡。所以我想知道是否有關於這種情況的最佳做法。python中的常量字符串文件
首先constants.py文件
class strings:
esc_statuses = {
"RETURNED": "Returned",
"SUBMITTED": "Submitted",
"DRAFT": "Draft",
"CANCELED": "Canceled",
"ESCALATED": "Escalated"
}
NewEscFieldText = {
"customer_name": "The name of the customer who encountered this bug.",
"summary": "A brief summary of the bug.",
"request": "The request.",
"customer_impact": "How the customer is impacted.",
"severity": "The severity of the bug.",
"component": "The component of this bug.",
"related_bugs": "Bugs which are related to this one.",
"logs": "The logs assosciated with this bug.",
"description": "A detailed discription of the problem and any work \
put into reproducting it.",
"documentation": "Documentation consulted before escalation.",
}
在第一個版本我不得不說:
from constants import strings
,然後每當我要引用一些我不得不說
strings.esc_statuses["RETURNED"]
我認爲constants.py文件看起來更多這種格式可讀,但每次我必須使用一個字符串,我會有一個更長的名字來咀嚼。
第二個constants.py文件。
class strings:
# ------------------------ Escalation status -----------------------------
RETURNED = "Returned"
SUBMITTED = "Submitted"
DRAFT =: "Draft"
CANCELED =: "Canceled"
ESCALATED =: "Escalated"
# ----------------------- New Escalation Field Text ----------------------
customer_name = "The name of the customer who encountered this bug."
summary = "A brief summary of the bug."
request = "The request."
customer_impact = "How the customer is impacted."
severity = "The severity of the bug."
component = "The component of this bug."
related_bugs = "Bugs which are related to this one."
logs = "The logs assosciated with this bug."
description = "A detailed discription of the problem and any work put \
into reproducting it."
documentation = "Documentation consulted before escalation."
在這個版本中所有我要說的是
from constants import strings
strings.RETURNED
我想這使得使用字符串更具可讀性也使得文件本身難以閱讀。
那麼,有沒有任何風格指南,涵蓋了這一點?有沒有我錯過的考慮?
您是否有能力加載包含字符串的外部配置文件? – JeremyFromEarth
這個文件應該是外部文件。 – AlexLordThorsen