2

Hi I would like to have a file that contain an enum list of all the items I will be using in my configuration. E.G

{
  "SUCCESS": 1,
  "FAILED": 1,
  "PENDING": 1,
}

I wanted something like in Laravel where in you will only place all your Enum in one of the files. In Laravel I can place this inside the folder config and file list_item e.g

return [
  "SUCCESS": 1,
  "FAILED": 1,
  "PENDING": 1,
]

so if I reference this in laravel its like config('config.list_item') and this will contain the array that I defined in my list_item file. Is there a way to achieve the same approach in Python/Flask? The only way I can think of is creating a file and inside it I will be defining a function like the example below

def StatusEnum():
    return {
       "SUCCESS": 1,
       "FAILED": 1,
       "PENDING": 1,
    }

and referencing it with StatusEnum() but I want to have a cleaner way to this.

So basically I want a cleaner and best approach to have a file that contains all my Enum list. I wan't to know how to do this in python/flask.

4
  • Can you clarify what you want to accomplish? Commented Aug 15, 2017 at 14:48
  • @LelandBarton edited my question. I wan't something like how Laravel loads and store all config/constants variable (Enum) Commented Aug 15, 2017 at 15:45
  • Check ConfigParser Commented Aug 15, 2017 at 16:56
  • thanks for asking this question, as a little appreciation I'm upvoting your answer, and the reason I'm telling this in the comment is so that just people would know that they have to upvote a good question too. Commented Nov 25, 2020 at 19:15

1 Answer 1

2

You can use configparser from stand library.

#config.cfg
[status]
FAILED = 0
SUCCESS = 1
PENDING = 1

# python source code 
import configparser
config = configparser.ConfigParser()
config.read("config.cfg")
config.getint("status", "FAILED")

Or just place a dict variable in your configuration file. Import it where you want to use it.

# config.py
status = {"FAILED": 0, "SUCCESS": 1, "PENDING": 2}

from config import status

print(status["FAILED"])
Sign up to request clarification or add additional context in comments.

1 Comment

It seems this is a good idea almost the same with Laravel. I'll accept this as the answer thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.