KeyError that I don't Understand

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
DKuchen
Posts: 2
Joined: Mon Apr 20, 2020 2:14 pm

KeyError that I don't Understand

Post by DKuchen » Sat Jul 04, 2020 12:09 am

I have a simple piece of code that is throwing me for a loop,

CONTROLLER_CONFIG = {
'ID': 1986,
'CE': 1,
'NI': 'Controller',
'AP': 4
}

def configure_node(**kwargs):
for cmd, value in kwargs.items():
print(cmd, ' ', value)

>>configure_node(**CONTROLLER_CONFIG)

When I run this i get a "KeyError: N". I can run this on my computer fine, but when I try to run in it through the micropython terminal from my xbee dev board it throws the error. I can also just run the For loop in the terminal with the same dictionary no problem.

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

Re: KeyError that I don't Understand

Post by jimmo » Sat Jul 04, 2020 6:37 am

On current github version of MicroPython this works fine.

Code: Select all

MicroPython v1.12-618-gfeed2a046 on 2020-07-03; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> CONTROLLER_CONFIG = {
... 'ID': 1986,
... 'CE': 1,
... 'NI': 'Controller',
... 'AP': 4
... }
>>> def configure_node(**kwargs):
...     for cmd, value in kwargs.items():
...         print(cmd, ' ', value)
...         
...         
... 
>>> configure_node(**CONTROLLER_CONFIG)
ID   1986
AP   4
NI   Controller
CE   1
>>> 
It also works on the (very old!) version of MicroPython running on the online simulator at http://micropython.org/unicorn/ so I'm don't think that this is a bug that's been fixed since Digi's version was forked?

I don't know much about MicroPython-on-xbee. As far as I know Digi haven't ever released source code for it? I wonder if maybe there's some feature they're disabling that's changing the behaviour of kwargs?

Maybe can you try running:

Code: Select all

CONTROLLER_CONFIG = {
'ID': 1986,
'CE': 1,
'NI': 'Controller',
'AP': 4
}

def configure_node(**kwargs):
  print(kwargs)

ESpy
Posts: 7
Joined: Sun Jun 28, 2020 6:58 am

Re: KeyError that I don't Understand

Post by ESpy » Tue Jul 07, 2020 7:11 am

Not just on the simulator:
MicroPython v1.12 on 2019-12-20; ESP module with ESP8266
Type "help()" for more information.
>>>
>>> CONTROLLER_CONFIG = {
... 'ID': 1986,
... 'CE': 1,
... 'NI': 'Controller',
... 'AP': 4
... }
>>> print(CONTROLLER_CONFIG)
{'ID': 1986, 'AP': 4, 'NI': 'Controller', 'CE': 1}
>>> def configure_node(**kwargs):
... for cmd, value in kwargs.items():
... print(cmd, " ", value)
...
...
...
>>> configure_node(**CONTROLLER_CONFIG)
ID 1986
AP 4
NI Controller
CE 1
>>>

Post Reply