save/load settings working
@@ -95,7 +95,6 @@ class Options(sc.objdict):
self.update(options) # Update this object with them
self.setattribute('optdesc', optdesc) # Set the description as an attribute, not a dict entry
self.setattribute('orig_options', sc.dcp(options)) # Copy the default options
- self.load_fonts() # Load custom fonts on module import
return
@@ -243,10 +242,10 @@ class Options(sc.objdict):
if key in numba_keys:
reload_required = True
if key in matplotlib_keys:
- self.set_matplotlib_global(key, value)
+ set_matplotlib_global(key, value)
if reload_required:
- self.reload_numba()
+ reload_numba()
return
@@ -291,13 +290,15 @@ class Options(sc.objdict):
print(f' Environment: {entry.variable}')
print(f' Description: {entry.desc}')
- sc.heading('Methods', spacesafter=0)
+ sc.heading('Methods:', spacesafter=0)
print('''
- cv.options.set() -- set options
+ cv.options(key=value) -- set key to value
+ cv.options[key] -- get or set key
+ cv.options.set() -- set option(s)
+ cv.options.get_default() -- get default setting(s)
cv.options.load() -- load settings from file
cv.options.save() -- save settings to file
- cv.options.get_default() -- get default options
- cv.options.load_fonts() -- add custom fonts
+ cv.options.to_dict() -- convert to dictionary
''')
if output:
@@ -315,12 +316,14 @@ class Options(sc.objdict):
kwargs (dict): passed to ``sc.loadjson()``
'''
json = sc.loadjson(filename=filename, **kwargs)
- self.set(**json)
+ current = self.to_dict()
+ new = {k:v for k,v in json.items() if v != current[k]} # Don't reset keys that haven't changed
+ self.set(**new)
if verbose: print(f'Settings loaded from {filename}')
return
- def save(self, filename, **kwargs):
+ def save(self, filename, verbose=True, **kwargs):
'''
Save current settings as a JSON file.
@@ -329,10 +332,11 @@ class Options(sc.objdict):
kwargs (dict): passed to ``sc.savejson()``
'''
json = self.to_dict()
- return sc.savejson(filename=filename, obj=json, **kwargs)
+ output = sc.savejson(filename=filename, obj=json, **kwargs)
+ if verbose: print(f'Settings saved to {filename}')
+ return output
- @staticmethod
def set_matplotlib_global(key, value, available_fonts=None):
''' Set a global option for Matplotlib -- not for users '''
if value: # Don't try to reset any of these to a None value
@@ -354,7 +358,6 @@ class Options(sc.objdict):
return
- @staticmethod
def handle_show(do_show):
''' Convenience function to handle the slightly complex logic of show -- not for users '''
backend = pl.get_backend()
@@ -367,7 +370,6 @@ class Options(sc.objdict):
return do_show
- @staticmethod
def reload_numba():
'''
Apply changes to Numba functions -- reloading modules is necessary for
@@ -391,7 +393,6 @@ class Options(sc.objdict):
return
- @staticmethod
def load_fonts(folder=None):
'''
Load custom fonts for plotting -- alias to ``sc.fonts()``
@@ -402,8 +403,6 @@ class Options(sc.objdict):
return
-
-
-# Add these here to be more accessible to the user
+# Create the options on module load, and load the fonts
options = Options()
-
+load_fonts()
