-1

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?

Ivajlo Iliev
  • 303
  • 1
  • 3
  • 19

1 Answers1

0

Why you want these methods to be static? I don't get it. by the way "config" is reserved word in flask and maybe that's your problem. The better solution for this particular question maybe is using regular methods and then instantiate an object from it. like the following:

import json   

class FeedbackMessagesReader:
    feedbacks = None
    
    def __init__(self):
        self.read_feedback_config()
    
    
    def read_feedback_config(self):
        with open ("feedback.json") as file:
            self.feedbacks = json.load(file)
 
    def get_feedback_message(self, key : str) -> str:
        return self.feedbacks[key]

feeds = FeedbackMessagesReader()
print(feeds.get_feedback_message("name"))

for the following feedback.json:

{
"name": "John Doe",
"age": 120
}

the output is John Doe

  • Dear Mohsen, thank you very much for your feedback. I will use the get_feedback_message() method in a lot of classes. That is why if i put into every class this kind of code: feeds = FeedbackMessagesReader() print(feeds.get_feedback_message("name")) every time the "feedback.json" file will be read. And i do not want to to do that - i want the file to be read only once and put it in a static class variable. Also i changed the name of the variable from "config" to something else and the problem still exists. – Ivajlo Iliev Dec 16 '21 at 11:57
  • @[Ivajlo Iliev](https://stackoverflow.com/users/4053840/ivajlo-iliev) lets assume you instantiate `feeds = FeedbackMessagesReader()` in your app or app factory. then you can call it in other your files and class like `from app import feeds` – Mohsen Hashemi Dec 16 '21 at 18:39