Page 1 of 1

Storing global configuration settings

Posted: Fri Feb 25, 2022 5:59 am
by p_j
Any thoughts on what is the most efficient way to store global config data that can be accessed from all modules?

I have been using a settings.py file with GLOBAL but after watching one of Damien's pycon talks on optimisation I'm concerned my method is horribly inefficient!

This is what I am doing at the moment

settings.py

Code: Select all

import os
import time
import sys
from machine import UART, Pin, WDT, Timer, SoftI2C, SoftSPI, RTC
from utils import AESTtime
import DS3231
import logging


def init():
    global DATE_STRING, EX_RTC, RTC, INA3221, WLAN_SSID, WLAN_PASS, HOSTNAME, BROADCAST_ADDRESS, BROADCAST_PORT, UART, I2C

    RTC = RTC()
    INA3221 = None

    WLAN_SSID = "GIGI-BEAR"
    WLAN_PASS = "xxx"
    BROADCAST_ADDRESS = "192.168.17.255"
    BROADCAST_PORT = 3000
    UART = UART(1, baudrate=115200, tx=37 , rx=38, txbuf=4096, rxbuf=4096, timeout=0)
    I2C = None

    current_date = AESTtime(time.time())
    DATE_STRING = f'{current_date[0]}-{current_date[1]:02d}-{current_date[2]:02d}'

    # Setup I2C
    try:
        I2C = SoftI2C(sda=Pin(33), scl=Pin(34), freq=400000)
        LOGGER.debug("I2C started")
    except:
        LOGGER.debug("Couldnt start I2C")

    try:
        EX_RTC = DS3231.DS3231(I2C)
        EX_RTC.get_time(set_rtc=True)
    except:
        print("Couldnt connect to DS3231")

boot.py

Code: Select all

import settings
settings.init()
other files

Code: Select all

import settings

settings.UART.write("Hello World!\n)

Re: Storing global configuration settings

Posted: Wed Jul 13, 2022 8:55 pm
by scruss
Such a complex set of shared settings only makes sense if you're deploying lots of the same board. As soon as you start including different hardware, access methods like serial and I2C go out the window.

I rather like - even if it stomps on a standard Python library name - Adafruit's "secrets.py" from CircuitPython. It's a very simple hash table:

Code: Select all

# This file is where you keep secret settings, passwords, and tokens!
# If you put them in the code you risk committing that info or sharing it

secrets = {
    'ssid' : 'your-ssid-here',
    'password' : 'your-password-here',
}
If you import this, secrets.secrets["ssid"] will access the shared value

Re: Storing global configuration settings

Posted: Thu Jul 14, 2022 12:40 am
by jimmo
p_j wrote:
Fri Feb 25, 2022 5:59 am
I have been using a settings.py file with GLOBAL but after watching one of Damien's pycon talks on optimisation I'm concerned my method is horribly inefficient!
This is a fine way to do it.

The optimisation issue is that accessing settings.UART.write is expensive because each layer is a dictionary lookup. (globals -> settings -> UART -> write). If this is an issue in a tight loop you can do:

Code: Select all

...
   uart_write = settings.UART.write
   for ...:
     uart_write(...)
...

Re: Storing global configuration settings

Posted: Mon Jul 18, 2022 10:03 am
by p_j
jimmo wrote:
Thu Jul 14, 2022 12:40 am
p_j wrote:
Fri Feb 25, 2022 5:59 am
I have been using a settings.py file with GLOBAL but after watching one of Damien's pycon talks on optimisation I'm concerned my method is horribly inefficient!
This is a fine way to do it.

The optimisation issue is that accessing settings.UART.write is expensive because each layer is a dictionary lookup. (globals -> settings -> UART -> write). If this is an issue in a tight loop you can do:

Code: Select all

...
   uart_write = settings.UART.write
   for ...:
     uart_write(...)
...
Thanks I'll keep that in mind, I do have some places where the lookups are a few levels deep.

Re: Storing global configuration settings

Posted: Mon Jul 18, 2022 10:12 am
by p_j
scruss wrote:
Wed Jul 13, 2022 8:55 pm
Such a complex set of shared settings only makes sense if you're deploying lots of the same board. As soon as you start including different hardware, access methods like serial and I2C go out the window.

I rather like - even if it stomps on a standard Python library name - Adafruit's "secrets.py" from CircuitPython. It's a very simple hash table:

Code: Select all

# This file is where you keep secret settings, passwords, and tokens!
# If you put them in the code you risk committing that info or sharing it

secrets = {
    'ssid' : 'your-ssid-here',
    'password' : 'your-password-here',
}
If you import this, secrets.secrets["ssid"] will access the shared value
I like the simplicity of this but yes I'm planning to deploy a large amount of boards with the same hardware.

Re: Storing global configuration settings

Posted: Thu Aug 25, 2022 7:06 pm
by danhalbert
CircuitPython recently added `.env` (dotenv) support: https://docs.circuitpython.org/en/lates ... index.html, which we use to setup credentials for the new web workflow. It is meant to be accessible outside the VM if necessary.