Json 用python中的切换器函数更新var
我是Python新手,如果我尝试做的事情非常奇怪,请同情我,在几次尝试之后,我无法使用函数切换器(switcherMessage(args))更新变量(messageInit)中字典中的某些键对,我的目标是能够触发特定方法,根据到达的消息类型(在本例中,我用消息变量表示它们),以便它只更新字典的一部分(messageInit),然后将指定的数据传递给另一个函数(otherTask(data)),在本例中,该函数只打印这些数据。最奇怪的是,对于第一个切换器,如果它能工作(toActive()),它会给我我所期望的(ohter两个没有),但老实说,我不了解python中变量的行为,我在js中有经验,但在这里我感到困惑,因为对我来说,它只应该更新 我感谢任何事先的帮助或暗示Json 用python中的切换器函数更新var,json,python-3.x,dictionary,Json,Python 3.x,Dictionary,我是Python新手,如果我尝试做的事情非常奇怪,请同情我,在几次尝试之后,我无法使用函数切换器(switcherMessage(args))更新变量(messageInit)中字典中的某些键对,我的目标是能够触发特定方法,根据到达的消息类型(在本例中,我用消息变量表示它们),以便它只更新字典的一部分(messageInit),然后将指定的数据传递给另一个函数(otherTask(data)),在本例中,该函数只打印这些数据。最奇怪的是,对于第一个切换器,如果它能工作(toActive()),它
import json
# global dict to hold telemtry
messageInit = {
'thingId': 'CarlosTal84-Hilda-mggbCoK1pihIqDJzJf3T',
'active': 'false',
'motorSpeed': 0,
'colorValue': {
"r":0,
"g":0,
"b":0
}
}
# print init data dict
print(f"init var: {messageInit}")
# other func
def otherTask(data):
print(f"after task: {data}")
# function to handle the receive message
def on_message():
# message bin arrival
message = b'{"active":"true"}'
# message = b'{"motorSpeed": 20}'
# message = b'{"colorValue":{"r":2,"g":10,"b":100}}'
# hold message in a var
payload = str(message.decode('utf-8'))
# print payload var with message
# print(payload)
# check if exist data in the payload
if payload:
# convert string in obj
obj = json.loads(payload)
# switcher func
def switcherMessage(args):
# methods
def toActive():
global messageInit
# hold in vars
messageActive = obj.get("active")
messageInit.update({"active": messageActive})
active = messageInit.get("active")
return active
def toMotorSpeed():
global messageInit
# hold in vars
messageMotorSpeed = obj.get("motorSpeed")
messageInit.update({"motorSpeed": messageMotorSpeed})
motorSpeed = messageInit.get("motorSpeed")
return motorSpeed
def toColorValue():
global messageInit
# hold in vars
messageColorValue = obj.get("colorValue")
messageInit.update({"colorValue": messageColorValue})
colorValue = messageInit.get("colorValue")
return colorValue
# dict switch
switcher={
'messageActive': toActive(),
'messageMotorSpeed': toMotorSpeed(),
'messageColorValue': toColorValue()
}
# return switcher
return switcher.get(args, lambda:"Shit")
# Driver program to switcher
def wichMessage():
if payload.find("active"):
return 'messageActive'
elif payload.find("motorSpeed"):
return 'messageMotorSpeed'
elif payload.find("colorValue"):
return 'messageColorValue'
# type of message var
args = wichMessage()
# run methods to fill vars
print(switcherMessage(args))
# other task
otherTask(switcherMessage(args))
# run it
on_message()
# here the outputs for every switcher:
init var: {'thingId': 'CarlosTal84-Hilda-mggbCoK1pihIqDJzJf3T', 'active': 'false', 'motorSpeed': 0, 'colorValue': {'r': 0, 'g': 0, 'b': 0}}
true
after task: true
>>>
# second message var
init var: {'thingId': 'CarlosTal84-Hilda-mggbCoK1pihIqDJzJf3T', 'active': 'false', 'motorSpeed': 0, 'colorValue': {'r': 0, 'g': 0, 'b': 0}}
None
after task: None
>>>
# third message var
init var: {'thingId': 'CarlosTal84-Hilda-mggbCoK1pihIqDJzJf3T', 'active': 'false', 'motorSpeed': 0, 'colorValue': {'r': 0, 'g': 0, 'b': 0}}
None
after task: None
>>>
这有很多代码需要挖掘,但是你的
切换器
字典看起来不对劲。请注意,如果执行switcher={'messageActive':toActive()}
,则在创建字典时(仅在该时间)调用toActive
,而不是在调用switcher.get(args,lambda:“Shit”)时调用。这意味着在创建switcher
时会调用所有这三个函数,然后再也不会调用(直到再次创建switcher
)。谢谢!!现在解决。。