Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

constant string file in python

I'm attempting to put all of my user facing strings into a single file to make changing those strings easier. I'm looking for a best practice in terms of readability. I have two version of the same file right now and I see trade off to both versions. So I was wondering if there's a best practice about this situation.

First constants.py file

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.",
        }

In the first version I'll have to say:

from constants import strings

and then whenever I want to reference something I'll have to say

strings.esc_statuses["RETURNED"]

I think the constants.py file looks more readable in this format but every time I have to use a string I'll have a much longer name to chew.

Second constants.py file.

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."

In this version all I have to say is

from constants import strings
strings.RETURNED

Which I think makes using strings more readable but also makes the file itself harder to read.

So, are there any style guides that cover this? Are there any considerations I've missed?

like image 421
AlexLordThorsen Avatar asked Jul 17 '13 02:07

AlexLordThorsen


People also ask

How do you write a constant file in Python?

In Python, constants are usually declared and assigned in a module. Here, the module is a new file containing variables, functions, etc which is imported to the main file. Inside the module, constants are written in all capital letters and underscores separating the words.

How do you declare a string constant?

To define a string constant in C++, you have to include the string header library, then create the string constant using this class and the const keyword.

Does Python have consts?

In programming, the term constant refers to names representing values that don't change during a program's execution. Constants are a fundamental concept in programming, and Python developers use them in many cases.

Should constants be in separate file?

Even if constants are related, they should not be put into a single file.


1 Answers

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
like image 167
perreal Avatar answered Sep 21 '22 14:09

perreal