I dont know how to best describe my problem in one sentence in the title, so here the explanation:
I have a websocket connection and get data from it. The data is json format as string. There are for example 3 key/value pairs:
data = json.loads('{"k1":"id1_a","k2":"id2_a","k3":"value"}')
Now i want to call for different k1
and k2
combinations accordingly different functions.
My approach is the following: I defined a config file (yaml) like:
combination_a:
k1: "id1_a"
k2: "id2_a"
combination_b:
k1: "id1_b"
k2: "id2_b"
[...]
Then i can import the config file as dict and do the following:
import yaml
config = yaml.safe_load(open("config.yml"))
if config["combination_a"].items() <= data.items():
function_a()
elif config["combination_b"].items() <= data.items():
function_b()
[...]
Now two questions:
- How can i "map" the combination to a specific function, without using a big
if elif [...]
block? as far as i know, there is no switch function in python?
- Or is there a completely different, better solution?
question from:
https://stackoverflow.com/questions/65836260/python-subset-of-key-value-pairs-of-a-dict-determines-the-function-to-call 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…