Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve config file usage #128

Open
blaylockbk opened this issue Dec 24, 2022 · 1 comment
Open

Improve config file usage #128

blaylockbk opened this issue Dec 24, 2022 · 1 comment

Comments

@blaylockbk
Copy link
Owner

I would like to make changes to the way the config file is used, like as the ability to change the config values temporarily (adjust the defaults inside a single script)

Here is what ChatGPT says I could do:


To allow a user to temporarily update config attributes without modifying the config file, you can modify the Config class to store the temporary updates in a separate dictionary. Here's an example of how you can do this:

class Config:
    def __init__(self, config_file):
        self.config_file = config_file
        self.config = {}
        self.temp_updates = {}
        with open(self.config_file, 'r') as f:
            for line in f:
                key, value = line.strip().split('=')
                self.config[key] = value

    def get(self, key):
        if key in self.temp_updates:
            return self.temp_updates[key]
        return self.config[key]

    def set(self, key, value, temporary=False):
        if temporary:
            self.temp_updates[key] = value
        else:
            self.config[key] = value
            with open(self.config_file, 'w') as f:
                for k, v in self.config.items():
                    f.write(f'{k}={v}\n')

    def reset(self):
        self.temp_updates = {}

With this modified Config class, you can use the set method to temporarily update a config value by setting the temporary argument to True. The temporary update will be stored in the temp_updates dictionary, and the get method will return the temporary value if it exists.

To reset all temporary updates and restore the original config values, you can use the reset method. This will clear the temp_updates dictionary.

Here's an example of how you might use these new methods:

# Load the config file
config = Config('/path/to/config.txt')

# Get the original value of a config key
value = config.get('key')

# Temporarily update the value of the key
config.set('key', 'new_value', temporary=True)

# Get the temporary updated value of the key
temp_value = config.get('key')

# Reset the temporary updates and restore the original value
config.reset()

# Get the original value of the key again
value = config.get('key')


There, that might give you a starting place.

@blaylockbk
Copy link
Owner Author

I asked ChatGPT to do the same but like how matplotlib rc_params are set


To allow a user to temporarily update config attributes in a way similar to how matplotlib's rcParams are set, you can modify the Config class to use a context manager. Here's an example of how you can do this:

import contextlib

class Config:
    def __init__(self, config_file):
        self.config_file = config_file
        self.config = {}
        self.temp_updates = {}
        with open(self.config_file, 'r') as f:
            for line in f:
                key, value = line.strip().split('=')
                self.config[key] = value

    def get(self, key):
        if key in self.temp_updates:
            return self.temp_updates[key]
        return self.config[key]

    def set(self, key, value, temporary=False):
        if temporary:
            self.temp_updates[key] = value
        else:
            self.config[key] = value
            with open(self.config_file, 'w') as f:
                for k, v in self.config.items():
                    f.write(f'{k}={v}\n')

    def reset(self):
        self.temp_updates = {}

    @contextlib.contextmanager
    def update(self, **kwargs):
        original_values = {}
        for key, value in kwargs.items():
            original_values[key] = self.get(key)
            self.set(key, value, temporary=True)
        yield
        for key, value in original_values.items():
            self.set(key, value, temporary=True)

With this modified Config class, you can use the update method as a context manager to temporarily update multiple config values at once. The original values will be restored when the context manager is exited.

Here's an example of how you might use the update method:

# Load the config file
config = Config('/path/to/config.txt')

# Temporarily update multiple config values
with config.update(key1='value1', key2='value2'):
    # Do something with the updated config values
    pass

# The original config values have been restored


Well that's cool, I didn't know about context manager. Maybe I'll try it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant