From 52c35e595f2275d9cb3910102c4ce7663ac4ab74 Mon Sep 17 00:00:00 2001 From: Christian Biasuzzi Date: Tue, 23 Jul 2024 18:21:34 +0200 Subject: [PATCH] reworks SelectionContext class, also to make naming less confusing Signed-off-by: Christian Biasuzzi --- src/pypowsybl_jupyter/networkexplorer.py | 2 +- src/pypowsybl_jupyter/selectcontext.py | 34 ++++++++++-------------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/pypowsybl_jupyter/networkexplorer.py b/src/pypowsybl_jupyter/networkexplorer.py index c229683..f19e149 100644 --- a/src/pypowsybl_jupyter/networkexplorer.py +++ b/src/pypowsybl_jupyter/networkexplorer.py @@ -113,7 +113,7 @@ def on_nadslider_changed(d): def on_text_changed(d): nonlocal found - sel_ctx.apply_filter_by_name(d['new']) + sel_ctx.apply_filter(d['new']) found.value=None found.options = sel_ctx.get_filtered_vls_as_list() if sel_ctx.is_selected_in_filtered_vls(): diff --git a/src/pypowsybl_jupyter/selectcontext.py b/src/pypowsybl_jupyter/selectcontext.py index 7dc5ec8..9e5b4c9 100644 --- a/src/pypowsybl_jupyter/selectcontext.py +++ b/src/pypowsybl_jupyter/selectcontext.py @@ -14,19 +14,17 @@ class SelectContext: def __init__(self, network:Network = None, vl_id : str = None, use_name:bool = True): self.network = network self.use_name = use_name + self.display_attribute = 'name' if use_name else 'id' self.vls = network.get_voltage_levels(attributes=['name']) + self.vls['name'] = self.vls['name'].replace('', pd.NA).fillna(self.vls.index.to_series().astype(str)) self.vls['id'] = self.vls.index - if use_name: - self.vls['name'] = self.vls['name'].replace('', pd.NA).fillna(self.vls['id']) - else: - self.vls['name'] = self.vls.index - - self.vls = self.vls.sort_values(by='name') - self.apply_filter_by_name(None) + self.vls = self.vls.sort_values(by=self.display_attribute) if use_name else self.vls.sort_index() + + self.apply_filter(None) - self.set_selected(self.vls.iloc[0].id if vl_id is None else vl_id) + self.set_selected(self.vls.index[0] if vl_id is None else vl_id) def get_vls(self): return self.vls @@ -40,23 +38,19 @@ def set_selected(self, id): def get_selected(self): return self.selected_vl - def apply_filter_by_name(self, sfilter): + def apply_filter(self, sfilter, search_attribute = None): if sfilter is not None and sfilter != '': - vls_filtered = self.vls[self.vls['name'].str.contains(sfilter, case=False, na=False, regex=False)] + search_by = self.display_attribute if search_attribute is None else search_attribute + self.vls_filtered = self.vls[self.vls[search_by].str.contains(sfilter, case=False, na=False, regex=False)] else: - vls_filtered = self.vls - - self.vls_filtered_dict = OrderedDict(zip(vls_filtered.index, vls_filtered['name'])) + self.vls_filtered = self.vls def is_selected_in_filtered_vls(self): - return self.selected_vl in self.vls_filtered_dict + return self.selected_vl in self.vls_filtered.index def get_filtered_vls_as_list(self): - names=list(self.vls_filtered_dict.values()) - ids=list(self.vls_filtered_dict.keys()) - name_id = list(zip(names,ids)) - return name_id + return list(zip(self.vls_filtered[self.display_attribute].values.tolist(), self.vls_filtered.index)) def extend_filtered_vls(self, id): - if (id in self.vls.index) and (id not in self.vls_filtered_dict): - self.vls_filtered_dict[id]=self.vls.loc[id, 'name'] \ No newline at end of file + if (id in self.vls.index) and (id not in self.vls_filtered.index): + self.vls_filtered = pd.concat([self.vls_filtered, self.vls.loc[[id]]])