Skip to content

Commit

Permalink
Swapped to using sortedcontainers
Browse files Browse the repository at this point in the history
sortedcontainers has the same functionality as bisect but is more stable and has better backwards compatibility
  • Loading branch information
theonlydvr committed Sep 11, 2024
1 parent d00517d commit bb27f2d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
20 changes: 10 additions & 10 deletions pybehave/Workstation/AddressFileCreationDialog.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

import bisect
from sortedcontainers import SortedList, SortedDict
import copy
import importlib
import os
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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()):
Expand Down Expand Up @@ -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())
Expand All @@ -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)

Expand Down
11 changes: 5 additions & 6 deletions pybehave/Workstation/ProtocolCreationDialog.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

import bisect
from sortedcontainers import SortedList
import importlib
import os
import runpy
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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())
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ dependencies = [
"pygame",
"screeninfo",
"PyQt5",
"psutil"
"psutil",
"sortedcontainers"
]

[project.optional-dependencies]
Expand Down

0 comments on commit bb27f2d

Please sign in to comment.