Skip to content

Commit

Permalink
added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
skjerns committed Oct 24, 2019
1 parent 6ec9afd commit 9e39932
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions spyder_line_profiler/widgets/lineprofiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,29 @@
WEBSITE_URL = 'http://pythonhosted.org/line_profiler/'

class TreeWidgetItem(QTreeWidgetItem):
"""
An extension of QTreeWidgetItem that replaces the sorting behaviour
such that the sorting is not purely by ASCII index but by natural
sorting, e.g. multi-digit numbers sorted based on their value instead
of individual digits.
taken from
https://stackoverflow.com/questions/21030719/sort-a-pyside-qtgui-
qtreewidget-by-an-alpha-numeric-column/
"""
def __lt__(self, other):
"""
compares a widget text entry to another entry
"""
column = self.treeWidget().sortColumn()
key1 = self.text(column)
key2 = other.text(column)
return self.natural_sort_key(key1) < self.natural_sort_key(key2)

@staticmethod
def natural_sort_key(key):
"""
natural sorting for both numbers and strings containing numbers
"""
regex = '(\d*\.\d+|\d+)'
parts = re.split(regex, key)
return tuple((e if i % 2 == 0 else float(e)) for i, e in enumerate(parts))
Expand Down

0 comments on commit 9e39932

Please sign in to comment.