python中的常量字符串文件

时间:2013-07-17 02:06:50

标签: python standards

我正在尝试将所有面向用户的字符串放入单个文件中,以便更轻松地更改这些字符串。我正在寻找可读性方面的最佳实践。我现在有两个版本的同一个文件,我看到两个版本的权衡。所以我想知道这种情况是否有最好的做法。

首先是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

我认为使用字符串更易读,但也会使文件本身更难阅读。

那么,是否有任何风格指南涵盖这个?有没有我错过的考虑因素?

1 个答案:

答案 0 :(得分:6)

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