Harden image fetch algorithm
Prepare for database integration
This commit is contained in:
44
modules/shared/select_elements.py
Normal file
44
modules/shared/select_elements.py
Normal file
@@ -0,0 +1,44 @@
|
||||
def select_elements(lst, selection_string):
|
||||
"""
|
||||
Выбирает элементы из списка согласно строке выбора
|
||||
|
||||
Args:
|
||||
lst: Исходный список
|
||||
selection_string: Строка вида "1 2 4-6 all"
|
||||
|
||||
Returns:
|
||||
Новый список с выбранными элементами, отсортированными по номерам
|
||||
"""
|
||||
selection_string = selection_string.strip()
|
||||
if not selection_string.strip():
|
||||
return []
|
||||
|
||||
if selection_string == "all":
|
||||
return lst.copy()
|
||||
|
||||
selected_indices = set()
|
||||
parts = selection_string.split()
|
||||
|
||||
for part in parts:
|
||||
if '-' in part:
|
||||
# Обработка диапазона
|
||||
start, end = map(int, part.split('-'))
|
||||
# Обработка диапазона в любом направлении
|
||||
if start <= end:
|
||||
selected_indices.update(range(start, end + 1))
|
||||
else:
|
||||
selected_indices.update(range(start, end - 1, -1))
|
||||
else:
|
||||
# Обработка отдельного элемента
|
||||
selected_indices.add(int(part))
|
||||
|
||||
# Преобразуем в список и сортируем по номерам
|
||||
sorted_indices = sorted(selected_indices)
|
||||
|
||||
# Выбираем элементы
|
||||
result = []
|
||||
for idx in sorted_indices:
|
||||
if 0 <= idx < len(lst):
|
||||
result.append(lst[idx])
|
||||
|
||||
return result
|
||||
Reference in New Issue
Block a user