I have json data in a file and I want to load the file content only once when application is initiated instead of calling the loading file multiple times in class instances.
I created a config which loads on app init named REPOSITORY_CONFIG
.
Then created a script with the method like this:
import json
CACHE = None
def get_repo_config():
global CACHE
with open('config.json') as config_json:
CACHE = json.load(config_json)
return CACHE
I have config.py
REPOSITORY_CONFIG = CACHE if CACHE else get_repo_config
This config is used in a class method like this:
import REPOSITORY_CONFIG
class Test():
def check_config():
config_data = REPOSITORY_CONFIG
Is there any other way to do this or I am not following right approach?
REPOSITORY_CONFIG
is a dict, soimport REPOSITORY_CONFIG
doesn't make sense. Is it supposed to beimport config
and thenconfig_data = config.REPOSITORY_CONFIG
? \$\endgroup\$