From bb27f2d96d4c93cd6dd2da18f8e8655da033881a Mon Sep 17 00:00:00 2001 From: theonlydvr Date: Wed, 11 Sep 2024 09:23:29 -0500 Subject: [PATCH] Swapped to using sortedcontainers sortedcontainers has the same functionality as bisect but is more stable and has better backwards compatibility --- .../Workstation/AddressFileCreationDialog.py | 20 +++++++++---------- .../Workstation/ProtocolCreationDialog.py | 11 +++++----- pyproject.toml | 3 ++- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/pybehave/Workstation/AddressFileCreationDialog.py b/pybehave/Workstation/AddressFileCreationDialog.py index ba6b783..b896d28 100644 --- a/pybehave/Workstation/AddressFileCreationDialog.py +++ b/pybehave/Workstation/AddressFileCreationDialog.py @@ -1,6 +1,6 @@ from __future__ import annotations -import bisect +from sortedcontainers import SortedList, SortedDict import copy import importlib import os @@ -57,10 +57,10 @@ def __init__(self, wsg: WorkstationGUI, task: str, file_path: str = None): self.task = getattr(task_module, task) self.components = self.task.get_components() - self.available_components = {} + self.available_components = SortedDict() for key in self.components: if len(self.components[key]) > 1: - self.available_components[key] = [str(i) for i in range(len(self.components[key]))] + self.available_components[key] = SortedList([str(i) for i in range(len(self.components[key]))]) else: self.available_components[key] = None @@ -231,9 +231,9 @@ def remove_row(self): if self.addresses[self.current_row][4] is None: self.available_components[component.currentText()] = None else: - self.available_components[component.currentText()] = [self.addresses[self.current_row][4].currentText()] + self.available_components[component.currentText()] = SortedList([self.addresses[self.current_row][4].currentText()]) else: - bisect.insort(self.available_components[component.currentText()], self.addresses[self.current_row][4].currentText()) + self.available_components[component.currentText()].add(self.addresses[self.current_row][4].currentText()) for i in range(self.table.rowCount()): if i != self.current_row and self.addresses[i][0].currentText() == component.currentText(): self.replace_indices(i, component.currentText()) @@ -267,11 +267,11 @@ def component_changed(self, add_ind): index = self.addresses[add_ind][4] if component.lastSelected not in self.available_components: if isinstance(index, ComboBox): - self.available_components[component.lastSelected] = [index.currentText()] + self.available_components[component.lastSelected] = SortedList([index.currentText()]) else: self.available_components[component.lastSelected] = None else: - bisect.insort(self.available_components[component.lastSelected], index.currentText()) + self.available_components[component.lastSelected].add(index.currentText()) # Add new indices back into any corresponding combo boxes for i in range(self.table.rowCount()): @@ -322,7 +322,7 @@ def update_indices(self, add_ind): # Add old index back into any corresponding combo boxes prev_ind = self.addresses[add_ind][4].lastSelected if prev_ind is not None: - bisect.insort(self.available_components[component.currentText()], prev_ind) + self.available_components[component.currentText()].add(prev_ind) for i in range(self.table.rowCount()): if i != add_ind and self.addresses[i][0].currentText() == component.lastSelected: self.replace_indices(i, component.currentText()) @@ -331,8 +331,8 @@ def replace_indices(self, i, c_name): # Replace indices in combo box with the available ones cur_ind = self.addresses[i][4].currentText() self.addresses[i][4].clear() - new_indices = copy.copy(self.available_components[c_name]) - bisect.insort(new_indices, cur_ind) + new_indices = self.available_components[c_name].copy() + new_indices.add(cur_ind) self.addresses[i][4].addItems(new_indices) self.addresses[i][4].setCurrentText(cur_ind) diff --git a/pybehave/Workstation/ProtocolCreationDialog.py b/pybehave/Workstation/ProtocolCreationDialog.py index c6dee23..94b6de1 100644 --- a/pybehave/Workstation/ProtocolCreationDialog.py +++ b/pybehave/Workstation/ProtocolCreationDialog.py @@ -1,6 +1,6 @@ from __future__ import annotations -import bisect +from sortedcontainers import SortedList import importlib import os import runpy @@ -30,7 +30,8 @@ def __init__(self, wsg: WorkstationGUI, task: str, file_path: str = None): self.task = getattr(task_module, task) self.constant_dict = self.task.get_constants() - self.available_constants = list(self.constant_dict.keys()) + keys = list(self.constant_dict.keys()) + self.available_constants = SortedList(self.constant_dict.keys(), key=lambda x: keys.index(x)) if file_path is None: self.setWindowTitle("New " + task + " Protocol") @@ -144,8 +145,7 @@ def add_row(self): def remove_row(self): constant = self.constants[self.current_row][0] - keys = list(self.constant_dict.keys()) - bisect.insort(self.available_constants, constant.currentText(), key=lambda x: keys.index(x)) + self.available_constants.add(constant.currentText()) for i in range(self.table.rowCount()): # Should ideally be insorted if i != self.current_row: self.constants[i][0].addItem(constant.currentText()) @@ -164,8 +164,7 @@ def constant_changed(self, add_ind): # Add the previous value back into the other combo boxes if constant.lastSelected is not None: - keys = list(self.constant_dict.keys()) - bisect.insort(self.available_constants, constant.lastSelected, key=lambda x: keys.index(x)) + self.available_constants.add(constant.lastSelected) for i in range(self.table.rowCount()): # Should ideally be insorted if i != add_ind: self.constants[i][0].addItem(constant.lastSelected) diff --git a/pyproject.toml b/pyproject.toml index 397e707..9f01daa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,7 +27,8 @@ dependencies = [ "pygame", "screeninfo", "PyQt5", - "psutil" + "psutil", + "sortedcontainers" ] [project.optional-dependencies]