2013-07-17 98 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