2013-07-17 5 views
4

나는 모든 문자열을 하나의 파일에 담아 그 문자열을 쉽게 변경하려고합니다. 나는 가독성 측면에서 모범 사례를 찾고있다. 나는 지금 같은 파일의 두 가지 버전을 가지고 있으며 두 버전 모두와의 교환을 본다. 그래서이 상황에 대한 최선의 방법이 있는지 궁금합니다.파이썬에서 상수 문자열 파일

from constants import strings 

내가 뭔가를 참조 할 때마다 나는 말을해야합니다을 :

첫 번째 버전에서 먼저 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.", 
     } 

나는 말을해야합니다

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)'으로 변경하고 null인지 확인했다. – AlexLordThorsen

+1

아하 네가 맞아 대답을 업데이트했다. – perreal