Storing global configuration settings

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
p_j
Posts: 102
Joined: Mon Aug 23, 2021 1:08 pm
Location: Sydney

Storing global configuration settings

Post by p_j » Fri Feb 25, 2022 5:59 am

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)

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Storing global configuration settings

Post by scruss » 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

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Storing global configuration settings

Post by jimmo » 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(...)
...

p_j
Posts: 102
Joined: Mon Aug 23, 2021 1:08 pm
Location: Sydney

Re: Storing global configuration settings

Post by p_j » Mon Jul 18, 2022 10:03 am

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.

p_j
Posts: 102
Joined: Mon Aug 23, 2021 1:08 pm
Location: Sydney

Re: Storing global configuration settings

Post by p_j » Mon Jul 18, 2022 10:12 am

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.

danhalbert
Posts: 18
Joined: Mon Jan 16, 2017 3:58 am

Re: Storing global configuration settings

Post by danhalbert » Thu Aug 25, 2022 7:06 pm

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.

Post Reply