We are implementing a Flask application. We shall have json file for feedback messages - with key - value pairs( The keys will be some constants, and the values will be actual messages, and then during the runtime the actual messages will be read and displayed. I would like to define a class, which reads the json file at the start of the application, and method, which returns the actual message based on the key. I implemented the following:
import json
class FeedbackMessagesReader:
@staticmethod
def read_feedback_config():
with open ("feedback_messages_json") as file:
return json.load(file)
@staticmethod
def get_feedback_message(key : str) -> str:
return config[key]
from feedback_messages_reader import FeedbackMessagesReader
config = FeedbackMessagesReader.read_feedback_config()
in a feedback_messages_reader.py, but i do not know whether and how much this is correct and is the best approach. Also i define the "config" at the end of the class, because if i put it at the beginning (like this)
import json
from feedback_messages_reader import FeedbackMessagesReader
config = FeedbackMessagesReader.read_feedback_config()
class FeedbackMessagesReader:
@staticmethod
def read_feedback_config():
with open ("feedback_messages_json") as file:
return json.load(file)
@staticmethod
def get_feedback_message(key : str) -> str:
return config[key]
i get error "Class already defined line 3"
So i would like to get advices which is the best approach to implement this?