Search code examples
pythonpython-2.7ooppyramid

Parse file upon initialization and make accessible to all classes


Essentially, when the pyramid app starts, I need to parse a file and make the data from that file available to the whole app. On top of that, I need to monitor the file for changes, it's essentially a config file. I was considering just making a singleton and initializing it when the app starts up when Configurator is initialized. Is that the best way to do it, or are there any other approaches I should follow/consider?

Sorry, new to pyramid and python, thank you!


Solution

  • As noted in many places, Singleton is considered a code smell and as such, should be avoided. IMHO, a Singleton can be used if there's a reasonable trade-off. For example, if using a Singleton boosts the performance in a significant way. In your situation this just might be the case: If the config file is used in many places across you app, reading the file multiple times might impact your performance. However, if you choose to use a Singleton, keep in mind the dangers that come with it.

    To answer you question, I don't think it's possible to tell what's the best way for you to go, because that depends on your specific app. I can only suggest that you take into account the pros and cons of each option, and making your choice depending on your needs.