Issue
My old code stored a device_name.id and its assigned sACN universe in this form:
prev = {
"312398": 1,
"232312": 2,
"312353": 3
}
Now im storing it as a nested dict with the additional attribute
device_name.model
as follows:
current = {
"312341": {
"model": "Some-Model",
"universe": 1
},
"532342": {
"model": "Some-other-model",
"universe": 2
}
}
My old code had a function
def get_free_universe():
return next(
i
for i in range(1, 128)
if i not in conf.values()
)
Called like this:
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 conf.keys == " ":
print("config empty, universer = 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 = conf[device_name.id]
Now of course the part if i not in conf.values()
doesnt work anymore. I need to look at every dict for its key universe
. How can I achive that? Currently, since none of the values of the dicts with device_name.id
has an own value itself, so I cant get a next number and it just gives every device_name.id the same universe (1).
Solution
One option can be to create a set
of all the universe values in the new mapping. For example, with the mapping in the above example:
>>> universes = {v['universe'] for v in current.values()}
>>> universes
{1, 2}
Then you can use the new variable universes
instead of conf.values()
which you were doing above. Of course if you generate and assign a new universe, you also need to remember to add it to this hashset like so:
>>> universes.add(3)
>>> universes
>>> {1, 2, 3}
Then a slight modification is needed in the get_free_universe
function:
def get_free_universe():
# generate a new universe
new_universe = next(
i
for i in range(1, 128)
if i not in universes
)
# add to known universes
universes.add(new_universe)
return new_universe
Answered By - rv.kvetch
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.