merge variables from one module into another

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

merge variables from one module into another

Post by devnull » Sun Mar 24, 2019 6:33 am

I want to use the same python code with different devices and want to do this by using a gpio.py file that will contain all of the gpio definitions as well as some common functions.

the gpio.py will be called in several files, and the boot.py file will be the only file that gets changed to define which device pins file to use.

I will have multiple pins files, i.e pins_8266. pins_pyb, pins_esp32 and

gpio.py

Code: Select all

_power = 10
power = mc.Pin(_power, mode=mc.Pin.OUT)

def init(pins):
  import pins
  for var in dir():
    if not var.startswith("__"):
        this.var = pins[var]
pins_8266.py

Code: Select all

_power = 15
pins_pyb.py

Code: Select all

_power = 12

boot.py

Code: Select all

import gpio
gpio.init('pins_pyb')
The gpio.init() function is 'pseudo code' as of course this won't work, but how can I efficiently load all of the variables from the imported file to override the local variables without turning the gpio file into a class and using this.xxx ?

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: merge variables from one module into another

Post by devnull » Sun Mar 24, 2019 6:48 am

Just tried this as an alternative method but it does not work:

Code: Select all

def init(pins):
  from pins import *
  globals().update(locals())
 

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: merge variables from one module into another

Post by devnull » Sun Mar 24, 2019 7:12 am

Oops, answering my own question, but maybe someone else will find this useful:

Code: Select all

def init(pfile='pins'):
  pins = __import__(pfile)
  for var in dir(pins):
    if not var.startswith("__"):
      globals()[var] = getattr(pins,var)
But is the 'best' way ??

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: merge variables from one module into another

Post by kevinkk525 » Sun Mar 24, 2019 9:33 am

I don't konw what the best way is but locals() doesn't work in micropython.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: merge variables from one module into another

Post by pfalcon » Sun Mar 24, 2019 7:17 pm

https://github.com/micropython/micropyt ... ples/hwapi has suggestions and examples available on these matters for quite some time.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: merge variables from one module into another

Post by devnull » Sun Apr 07, 2019 6:29 am

I know that I can do this to import a complete module:

Code: Select all

from network import *
But how can I import a module's class the same way ?

Code: Select all

from network import WLAN as wlan
from wlan import *

Post Reply