Files
decluttarr/src/utils/nest_functions.py
Benjamin Harder 34842a4d0c Major refactor & code-cleanup
Improved Slow Downloads (more accurate determining of progress)
Added possiblity to exclude private Trackers from cleaning
2024-02-18 18:59:32 +01:00

47 lines
1.5 KiB
Python

def nested_set(dic, keys, value, matchConditions=None):
# Sets the value of a key in a dictionary to a certain value.
# If multiple items are present, it can filter for a matching item
for key in keys[:-1]:
dic = dic.setdefault(key, {})
if matchConditions:
i = 0
match = False
for item in dic:
for matchCondition in matchConditions:
if item[matchCondition] != matchConditions[matchCondition]:
match = False
break
else:
match = True
if match:
dic = dic[i]
break
i += 1
dic[keys[-1]] = value
def add_keys_nested_dict(d, keys, defaultValue = None):
# Creates a nested value if key does not exist
for key in keys[:-1]:
if key not in d:
d[key] = {}
d = d[key]
d.setdefault(keys[-1], defaultValue)
def nested_get(dic, return_attribute, matchConditions):
# Retrieves a list contained in return_attribute, found within dic based on matchConditions
i = 0
match = False
hits = []
for item in dic:
for matchCondition in matchConditions:
if item[matchCondition] != matchConditions[matchCondition]:
match = False
break
else:
match = True
if match:
hits.append(dic[i][return_attribute])
i += 1
return hits