Skip to content

Commit 9d431e4

Browse files
EspenEnesespenenesNOVdomna
authored
Refactor channel search functionality in tdm_loader (#28)
* Refactor channel search functionality in `tdm_loader` The previous implementation of the channel search function in the `tdm_loader.py` was refactored. Search now matches channels directly without set generation and updates retrieval with more efficient lookups. The modification updates the testing to reflect the changes in the returned search results as well. Before the refactor searching throe 22000 channels took 6.5min and after it took 0.08s Also the test has changed to be similar when searching for "", all channels should be returned. * Update tdm_loader/tdm_loader.py Co-authored-by: Florian Dobener <[email protected]> * Update tdm_loader/tdm_loader.py Co-authored-by: Florian Dobener <[email protected]> * Update tdm_loader/tdm_loader.py Co-authored-by: Florian Dobener <[email protected]> * Update tdm_loader/tdm_loader.py Co-authored-by: Florian Dobener <[email protected]> * Update tdm_loader/tdm_loader.py Co-authored-by: Florian Dobener <[email protected]> * Update tdm_loader/tdm_loader.py Co-authored-by: Florian Dobener <[email protected]> * Implement caching and optimize channel search in `tdm_loader` A cache decorator is introduced for the `get_channels` function to speed up repeated lookups, replacing the previous, dictionary lookup. * Rename method get_channels to _get_channels in tdm_loader The public `get_channels` function is renamed to the private method `_get_channels` in 'tdm_loader.py'. This is due to internal use only, to provide more clarity and prevent unintended external access. * Correct typo in method name in tdm_loader The method name "get_channels" has been corrected to "_get_channels" in the 'tdm_loader.py' file. The change was necessitated by the requirement for the method to be private, hence the underscore, as it's only meant for internal use, preventing unintended external access. --------- Co-authored-by: Espen Enes <[email protected]> Co-authored-by: Florian Dobener <[email protected]>
1 parent 3cffb74 commit 9d431e4

File tree

2 files changed

+24
-25
lines changed

2 files changed

+24
-25
lines changed

tdm_loader/tdm_loader.py

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import os
2727
import zipfile
2828
import re
29+
from functools import cache
2930

3031
from xml.etree import ElementTree
3132
import warnings
@@ -168,6 +169,11 @@ def _get_usi_from_txt(txt):
168169
return []
169170
return re.findall(r'id\("(.+?)"\)', txt)
170171

172+
@cache
173+
def _get_channels(self, group_id):
174+
group = self._xml_chgs[group_id]
175+
return {v: i for i, v in enumerate(re.findall(r'id\("(.+?)"\)', group.find("channels").text))}
176+
171177
def channel_group_search(self, search_term):
172178
"""Returns a list of channel group names that contain ``search term``.
173179
Results are independent of case and spaces in the channel name.
@@ -228,31 +234,22 @@ def channel_search(self, search_term):
228234
"""
229235
search_term = str(search_term).upper().replace(" ", "")
230236

231-
ind_chg_ch = []
232-
for j in range(len(self._xml_chgs)):
233-
chs = self._channels_xml(j)
237+
matched_channels = []
238+
channel_group_ids = {v: i for i, v in enumerate(x.get("id") for x in self._xml_chgs)}
234239

235-
if search_term == "":
236-
found_terms = [
237-
ch.findtext("name") for ch in chs if ch.findtext("name") is None
238-
]
239-
else:
240-
found_terms = [
241-
ch.findtext("name")
242-
for ch in chs
243-
if ch.findtext("name") is not None
244-
and ch.findtext("name")
245-
.upper()
246-
.replace(" ", "")
247-
.find(str(search_term))
248-
>= 0
249-
]
250-
251-
for name in found_terms:
252-
i = [ch.findtext("name") for ch in chs].index(name)
253-
ind_chg_ch.append((name, j, i))
254-
255-
return ind_chg_ch
240+
for channel in self._root.findall(".//tdm_channel"):
241+
channel_name = channel.find("name").text
242+
if channel_name:
243+
group_uri = re.findall(r'id\("(.+?)"\)', channel.find("group").text)
244+
group_id = channel_group_ids.get(group_uri[0])
245+
channels = self._get_channels(group_id)
246+
247+
channel_id = channels.get(channel.get("id"))
248+
249+
if channel_name.upper().replace(" ", "").find(search_term) >= 0:
250+
matched_channels.append((channel_name, group_id, channel_id))
251+
252+
return matched_channels
256253

257254
def channel(self, channel_group, channel, occurrence=0, ch_occurrence=0):
258255
"""Returns a data channel by its channel group and channel index.

tdm_loader/tests/test_non_zip_tdm.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,9 @@ def test_channel_search(tdm_file):
204204
("Float as Float", 0, 1),
205205
]
206206

207-
assert tdm_file.channel_search("") == []
207+
assert tdm_file.channel_search("") == [('Float_4_Integers', 0, 0),
208+
('Float as Float', 0, 1),
209+
('Integer32_with_max_min', 0, 2)]
208210

209211

210212
# pylint: disable=redefined-outer-name

0 commit comments

Comments
 (0)