Compare commits
2 Commits
f03cc4a44c
...
2aac36e15c
Author | SHA1 | Date | |
---|---|---|---|
2aac36e15c | |||
aa3110d564 |
32
README.md
32
README.md
@ -62,7 +62,37 @@ All the configuration can be done in the [config.json](config.json) file in JSON
|
||||
| "LCD\_I2C\_NUM\_ROWS" | int | how many rows for character display has the display? | 2 |
|
||||
| "LCD\_I2C\_NUM\_COLS" | int | and how many characters can it display per row? | 16 |
|
||||
|
||||
Note that this software has it's own small wrapper for the config file, e.g. to have instant access to an LCD object generated on the fly. These are all documented in the [utils.py](utils.py) file!
|
||||
Note that this software has it's own small wrapper for the config file, e.g. to have instant access to an LCD object generated on the fly. These are all documented in the [utils.py](utils.py) file! When setting configuration options from your custom code, keep in mind that doing this via the `Config().<ATTR_NAME>` way just means writing the value directly to the file, while getting it goes through the wrapper to make e.g. the pin a machine.Pin object. But you just can't write a pin back into an attribute.
|
||||
|
||||
This wont work:
|
||||
|
||||
```python
|
||||
from utils import Config
|
||||
from machine import Pin
|
||||
cfg = Config()
|
||||
|
||||
btn1 = cfg.PIN_IN_BTN_1
|
||||
|
||||
# Now we don't like this setting anymore
|
||||
new_btn1 = Pin(10, Pin.IN, Pin.PULL_DOWN)
|
||||
cfg.PIN_IN_BTN_1 = new_btn1
|
||||
```
|
||||
|
||||
Instead, you have to do it that way:
|
||||
|
||||
```python
|
||||
from utils import Config
|
||||
from machine import Pin
|
||||
cfg = Config
|
||||
|
||||
btn1 = cfg.PIN_IN_BTN_1
|
||||
|
||||
# Now we don't like it so we write the new pin in our json format
|
||||
cfg.PIN_IN_BTN_1 = {"pin": 10, "pull": "down"}
|
||||
```
|
||||
|
||||
So, as the `Config` class uses python's magic function for getting and setting attributes, the process of changing some config is not completely intuitive, but when keeping that in mind, it's very handy!
|
||||
|
||||
|
||||
## Used libraries
|
||||
|
||||
|
17
utils.py
17
utils.py
@ -54,7 +54,7 @@ class Config:
|
||||
def save_config(self):
|
||||
with open(self._config_file, "w") as f:
|
||||
from json import dump
|
||||
dump(self._config, f)
|
||||
dump(self._config, f, separators=(',\n', ': '))
|
||||
del dump
|
||||
collect()
|
||||
|
||||
@ -102,8 +102,17 @@ class Config:
|
||||
|
||||
|
||||
def __setattr__(self, name, value):
|
||||
#print(f"Someone tried to edit my poor attributes! Affected: '{name}' should be set to '{value}'")
|
||||
object.__setattr__(self, name, value)
|
||||
if name.startswith("_"): # make private attributes settable as normal
|
||||
object.__setattr__(self, name, value)
|
||||
elif name in self._attr_list: # valid attributes (only capital letters and -_ etc. are allowed)
|
||||
try:
|
||||
self._config[name] = value
|
||||
self.save_config()
|
||||
except KeyError:
|
||||
raise AttributeError(f"Attribute '{name}' does not exist in the config file '{self._config_file}'")
|
||||
else:
|
||||
raise AttributeError(f"Can't set attribute '{name}' for a '{self.__class__.__name__}' object: forbidden")
|
||||
|
||||
|
||||
def __delattr__(self, name):
|
||||
raise AttributeError(f"You may not delete any attribute of the '{self.__class__.__name__}' object")
|
||||
@ -124,3 +133,5 @@ def log(log_level: int, message: str):
|
||||
elif cfg.LOG_LEVEL >= log_level: # if log level is valid
|
||||
print(f"[{log_mapping[log_level]}] {message}")
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user