You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python嵌套字典结构下获取sACN universe空闲值的实现方法问询

修改方案

你需要同时调整空闲universe获取函数、以及原有调用逻辑中的两处错误,修改后代码如下:


1. 调整get_free_universe函数

原逻辑判断i not in conf.values()失效是因为当前conf的value是嵌套字典,需要先提取所有子字典的universe字段做判断:

def get_free_universe():
    # 提取所有已占用的universe编号,设备量多的话可以改成集合{}提升查询效率
    used_universes = [v["universe"] for v in conf.values()]
    return next(
        i
        for i in range(1, 128)
        if i not in used_universes
    )

原有逻辑所有新设备都分配1的原因:数字1永远不会等于子字典对象,所以i=1永远满足条件,每次都返回1。


2. 调整调用逻辑

原有调用逻辑存在两个错误:

  • 配置为空的判断if conf.keys == " "完全不生效,正确判断为空的方式是if not conf
  • 已有设备取universe的逻辑错误,当前conf的value是嵌套字典,需要显式取universe字段

修改后代码:

device_count = sdk.get_device_count() #setup Corsair devices config
for device_index in range(device_count):
    device_name = sdk.get_device_info(device_index)
    device_type = device_name.type
    if device_name.id not in conf:
        # 修正配置为空的判断逻辑
        if not conf:
            print("config empty, universe = 1")
            universe = 1
        else: 
            universe = get_free_universe()
        conf.update({device_name.id: {"model": device_name.model, "universe": universe}}) 
        print(f"conf= {conf}")
    else:
        # 修正取值逻辑,从嵌套字典中取universe字段
        universe = conf[device_name.id]["universe"]

内容的提问来源于stack exchange,提问作者tenn0

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 04:24:01