python中的常量字符串文件

python中的常量字符串文件,python,standards,Python,Standards,我试图将所有面向用户的字符串放在一个文件中,以使更改这些字符串更容易。我正在寻找可读性方面的最佳实践。我现在有两个版本的同一个文件,我看到两个版本的权衡。所以我想知道在这种情况下是否有一个最佳实践 First constants.py文件 class strings: esc_statuses = { "RETURNED": "Returned", "SUBMITTED": "Submitted", "DRAFT": "Draft",

我试图将所有面向用户的字符串放在一个文件中,以使更改这些字符串更容易。我正在寻找可读性方面的最佳实践。我现在有两个版本的同一个文件,我看到两个版本的权衡。所以我想知道在这种情况下是否有一个最佳实践

First 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文件在这种格式下看起来更可读,但每次我必须使用字符串时,我都会有一个更长的名称

Second 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
我认为这使使用字符串更具可读性,但也使文件本身更难读取


那么,是否有任何样式指南涵盖了这一点?有什么我没有考虑的吗?

你有能力加载一个包含字符串的外部配置文件吗?这个文件应该是外部文件。我觉得以前没有注意到这一点很愚蠢,但是
NewEscFieldText
是在
\uu getattr\uu
函数中硬编码的。当我完成时,这将是一个大文件,我将有许多不同的字典。我的解决方案是将
stringer.NewEscFieldText[name]
更改为
stringer.NewEscFieldText.get(name)
,并检查它是否为空。
class stringer(type):
    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.",
        }

def __getattr__(self, name):
    if name in stringer.NewEscFieldText: 
        return stringer.NewEscFieldText[name]
    else:
        return stringer.esc_statuses[name]

class strings:
    __metaclass__ = stringer

print strings.customer_name