2013-07-17 48 views
4

我试图将我所有的用户都面对字符串放入单个文件中,以便更改这些字符串。我正在寻找可读性方面的最佳做法。我现在有两个版本的相同的文件,我看到两个版本的权衡。所以我想知道是否有关于这种情况的最佳做法。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 

我想这使得使用字符串更具可读性也使得文件本身难以阅读。

那么,有没有任何风格指南,涵盖了这一点?有没有我错过的考虑?

+0

您是否有能力加载包含字符串的外部配置文件? – JeremyFromEarth

+0

这个文件应该是外部文件。 – AlexLordThorsen

回答

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 
+0

我觉得以前不会注意到这些,但'NewEscFieldText'在'__getattr__'函数中被硬编码。到我完成时,这将会是一个很大的文件,我将会有很多不同的字典。我的解决方案是将'stringer.NewEscFieldText [name]'更改为'stringer.NewEscFieldText.get(name)'并检查它是否为空。 – AlexLordThorsen

+1

啊,你是对的,更新了答案 – perreal