110 lines
3.1 KiB
Python
110 lines
3.1 KiB
Python
|
||
|
||
class ListsDict:
|
||
def __init__(self):
|
||
self._data: dict[str, list] = dict()
|
||
|
||
def add(self, key, value):
|
||
if key not in self._data: self._data[key] = list()
|
||
if value not in self._data[key]: self._data[key].append(value)
|
||
|
||
|
||
def delete(self, key, value):
|
||
if self._data.get(key, None): self._data[key].remove(value)
|
||
|
||
@property
|
||
def index(self):
|
||
res = list()
|
||
for key, collection in self._data.items():
|
||
for elem in collection:
|
||
if elem not in res:
|
||
res.append(elem)
|
||
return res
|
||
|
||
def by_key(self, key):
|
||
return self._data.get(key, None)
|
||
|
||
|
||
class SetsDict:
|
||
def __init__(self):
|
||
self._data: dict[str, set] = dict()
|
||
|
||
def add(self, key, value):
|
||
if key not in self._data: self._data[key] = set()
|
||
if value not in self._data[key]: self._data[key].add(value)
|
||
|
||
def delete(self, key, value):
|
||
if self._data.get(key, None): self._data[key].remove(value)
|
||
|
||
@property
|
||
def index(self):
|
||
res = set()
|
||
for key, collection in self._data.items():
|
||
for elem in collection:
|
||
if elem not in res:
|
||
res.add(elem)
|
||
return res
|
||
|
||
def by_key(self, key):
|
||
return self._data.get(key, None)
|
||
|
||
@property
|
||
def keys(self): return self._data.keys()
|
||
|
||
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
|
||
|
||
def format_bytes(bytes_size):
|
||
"""Convert bytes to human readable format"""
|
||
if bytes_size < 1024:
|
||
return f"{bytes_size} B"
|
||
|
||
for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
|
||
if bytes_size < 1024.0:
|
||
return f"{bytes_size:.1f} {unit}"
|
||
bytes_size /= 1024.0
|
||
|
||
return f"{bytes_size:.1f} PB"
|