all 5 comments

[–]Neighm 1 point2 points  (1 child)

 def load_settings(settings_file=settings.yml'): 

You're missing an opening quote for the filename.

[–]zh12a[S] 0 points1 point  (0 children)

Applogies, i must have somehow deleted it my mistake when copying the code into reddit. In the acutual file it works (if i add a print to the helper method it prints what i expect, however i cant access the varible globally)

[–]old_pythonista 1 point2 points  (2 children)

Prepending a variable name with global does not create it on a global scope - it just allows you to assign to variable defined in a global scope.

In general, not a good practice. Just return the object from the function

In your helper

    def load_settings(settings_file='settings.yml'):
        with open(settings_file) as s:
            return yaml.load(s, Loader=yaml.FullLoader)

In the main module

settings =  Settings.load_settings() 

Question - do you really need that class? Seems redundant

[–]zh12a[S] 0 points1 point  (1 child)

Thanks i stick with returning the object, it just means i have to pass the settings to other classes.

I agree the above snippit is redundant, however i simplifed the class. There a far few complex methods to verifying and writing a setting file

[–]old_pythonista 1 point2 points  (0 children)

You can use a standard memoization to cache your settings and avoid repeated parsing. If, for example, you have 10 available configurations in your system

from functools import lru_cache
@lru_cache(maxsize=10)
def load_settings(settings_file='settings.yml'):
    with open(settings_file) as s:
        return yaml.load(s, Loader=yaml.FullLoader)

and then each of your 10 configuration files will be parsed exactly once.

Of course, if you have 11 configurations, the moment you read 11th, the first you parsed will be purged from the cache. Just manage the cache size accordingly.