-
Notifications
You must be signed in to change notification settings - Fork 0
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
Switch Config
to singleton design pattern
#198
Conversation
This revamps the implementation of the `Config` class to a singleton approach. Instead of offering a regular constructor to the rest of the application, this keeps track of a single static instance of the config and returns it on subsequent calls.
This allows saving to the same file we loaded from when saving after initialisation and makes it possible to back up the config to do some tests and restore it afterwards.
Regarding potentially using a custom config, should we keep track of the config path? Currently, if we load a config from a file, all's well. But if we modify the config and want to save it back to file after changing it, we don't know where the config came from, or even if it came from a custom path rather than the default. |
Current methods were modifying the class attributes rather than modifying the actual singleton (`_instance`), meaning modifications to the instance were not actually reflected when saving, etc... With this, the modifications actually happen on the instance and the functions are also applied to that instance.
Since we now keep track of the config path in the config itself, we should update it when saving it to a new location.
Looking at the currently opened issues, it seems this also somewhat addresses #116. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very elegant!!
Check the comment re classes importing from object explicitly, otherwise happy to merge
@@ -2,18 +2,17 @@ | |||
|
|||
# This file is part of visiomode. | |||
# Copyright (c) 2020 Constantinos Eleftheriou <[email protected]> | |||
# Copyright (c) 2024 Olivier Delree <[email protected]> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes nice please do remember to add this as you go, and we should also probably create an authors or contributors txt
Yeah I don't see why not, sounds like a useful addition and would also allow for easier troubleshooting on-the-fly on an individual device in the event the application doesn't start on account of a misconfiguration / the default settings not playing well on a particular device. |
I think we can safely assume only one config will ever be necessary. I think baking-in the alternative introduces unnecessary complexity for what is a mild gain for testing imho |
No reason to inherit from `object` since we're working with Python 3.
Happy to merge if you are! |
Closes #197.
We used to have a
Config
class with a regular__init__
constructor that would result in a newConfig
object being instantiated for each module that called it.This PR restructures the class to be static and use the
__new__
constructor. The class now keeps track of a single instance that is created with the first__new__
call and is returned with subsequent calls to the constructor. This has the advantage of synchronising the config across the whole application as only one config ever exists at the same time. Additionally, it means that the current syntax does not need to be modified throughout the application asConfig()
naturally makes use of__new__
alone if no__init__
exists.This implementation assumes that only one config will ever be necessary for the application. However, if for some reason in the future (e.g., for simultaneous tests), we need multiple configs to exist at once, the class could be made to instead accept a config ID and keep track of a hashmap of configs.