python中的常量字符串文件

2024-09-23 00:27:03 发布

您现在位置:Python中文网/ 问答频道 /正文

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

首先常量.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.",
        }

在第一个版本中,我不得不说:

^{pr2}$

然后每当我想引用一些东西的时候,我就不得不说

strings.esc_statuses["RETURNED"]

我认为常量.py这种格式的文件看起来更可读,但每次我必须使用字符串时,我都会有一个更长的名称来咀嚼。在

秒常量.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

我认为这使得使用字符串更具可读性,但也使文件本身更难读取。在

那么,有没有什么风格指南可以涵盖这一点呢?我有没有遗漏什么考虑?在


Tags: 文件ofthe字符串name版本requestcustomer
1条回答
网友
1楼 · 发布于 2024-09-23 00:27:03
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

相关问题 更多 >