Rename lib.py to utils.py and add test cases for natural sort
This commit is contained in:
parent
054f63f6f9
commit
ca28daddec
23
labelImg.py
23
labelImg.py
@ -29,7 +29,7 @@ except ImportError:
|
||||
import resources
|
||||
# Add internal libs
|
||||
from libs.constants import *
|
||||
from libs.lib import struct, newAction, newIcon, addActions, fmtShortcut, generateColorByText
|
||||
from libs.utils import *
|
||||
from libs.settings import Settings
|
||||
from libs.shape import Shape, DEFAULT_LINE_COLOR, DEFAULT_FILL_COLOR
|
||||
from libs.stringBundle import StringBundle
|
||||
@ -49,27 +49,6 @@ from libs.hashableQListWidgetItem import HashableQListWidgetItem
|
||||
|
||||
__appname__ = 'labelImg'
|
||||
|
||||
# Utility functions and classes.
|
||||
|
||||
def have_qstring():
|
||||
'''p3/qt5 get rid of QString wrapper as py3 has native unicode str type'''
|
||||
return not (sys.version_info.major >= 3 or QT_VERSION_STR.startswith('5.'))
|
||||
|
||||
def util_qt_strlistclass():
|
||||
return QStringList if have_qstring() else list
|
||||
|
||||
|
||||
def natural_sort(list, key=lambda s:s):
|
||||
"""
|
||||
Sort the list into natural alphanumeric order.
|
||||
"""
|
||||
def get_alphanum_key_func(key):
|
||||
convert = lambda text: int(text) if text.isdigit() else text
|
||||
return lambda s: [convert(c) for c in re.split('([0-9]+)', key(s))]
|
||||
sort_key = get_alphanum_key_func(key)
|
||||
list.sort(key=sort_key)
|
||||
|
||||
|
||||
class WindowMixin(object):
|
||||
|
||||
def menu(self, title, actions=None):
|
||||
|
||||
@ -10,7 +10,7 @@ except ImportError:
|
||||
#from PyQt4.QtOpenGL import *
|
||||
|
||||
from libs.shape import Shape
|
||||
from libs.lib import distance
|
||||
from libs.utils import distance
|
||||
|
||||
CURSOR_DEFAULT = Qt.ArrowCursor
|
||||
CURSOR_POINT = Qt.PointingHandCursor
|
||||
@ -42,7 +42,7 @@ class Canvas(QWidget):
|
||||
self.selectedShape = None # save the selected shape here
|
||||
self.selectedShapeCopy = None
|
||||
self.drawingLineColor = QColor(0, 0, 255)
|
||||
self.drawingRectColor = QColor(0, 0, 255)
|
||||
self.drawingRectColor = QColor(0, 0, 255)
|
||||
self.line = Shape(line_color=self.drawingLineColor)
|
||||
self.prevPoint = QPointF()
|
||||
self.offsets = QPointF(), QPointF()
|
||||
@ -662,7 +662,7 @@ class Canvas(QWidget):
|
||||
self.shapes[-1].label = text
|
||||
if line_color:
|
||||
self.shapes[-1].line_color = line_color
|
||||
|
||||
|
||||
if fill_color:
|
||||
self.shapes[-1].fill_color = fill_color
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@ except ImportError:
|
||||
from PyQt4.QtGui import *
|
||||
from PyQt4.QtCore import *
|
||||
|
||||
from libs.lib import newIcon, labelValidator
|
||||
from libs.utils import newIcon, labelValidator
|
||||
|
||||
BB = QDialogButtonBox
|
||||
|
||||
@ -77,7 +77,7 @@ class LabelDialog(QDialog):
|
||||
# PyQt5: AttributeError: 'str' object has no attribute 'trimmed'
|
||||
text = tQListWidgetItem.text().strip()
|
||||
self.edit.setText(text)
|
||||
|
||||
|
||||
def listItemDoubleClick(self, tQListWidgetItem):
|
||||
self.listItemClick(tQListWidgetItem)
|
||||
self.validate()
|
||||
|
||||
@ -9,7 +9,7 @@ except ImportError:
|
||||
from PyQt4.QtGui import *
|
||||
from PyQt4.QtCore import *
|
||||
|
||||
from libs.lib import distance
|
||||
from libs.utils import distance
|
||||
import sys
|
||||
|
||||
DEFAULT_LINE_COLOR = QColor(0, 255, 0, 128)
|
||||
|
||||
@ -35,7 +35,8 @@ class StringBundle:
|
||||
locale.getlocale()) > 0 else os.getenv('LANG')
|
||||
except:
|
||||
print('Invalid locale')
|
||||
locale = 'en'
|
||||
localeStr = 'en'
|
||||
|
||||
return StringBundle(cls.__create_key, localeStr)
|
||||
|
||||
def getString(self, stringId):
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
from math import sqrt
|
||||
from libs.ustr import ustr
|
||||
import hashlib
|
||||
import re
|
||||
import sys
|
||||
|
||||
try:
|
||||
from PyQt5.QtGui import *
|
||||
from PyQt5.QtCore import *
|
||||
@ -81,3 +84,20 @@ def generateColorByText(text):
|
||||
g = int((hashCode / 65025) % 255)
|
||||
b = int((hashCode / 16581375) % 255)
|
||||
return QColor(r, g, b, 100)
|
||||
|
||||
def have_qstring():
|
||||
'''p3/qt5 get rid of QString wrapper as py3 has native unicode str type'''
|
||||
return not (sys.version_info.major >= 3 or QT_VERSION_STR.startswith('5.'))
|
||||
|
||||
def util_qt_strlistclass():
|
||||
return QStringList if have_qstring() else list
|
||||
|
||||
def natural_sort(list, key=lambda s:s):
|
||||
"""
|
||||
Sort the list into natural alphanumeric order.
|
||||
"""
|
||||
def get_alphanum_key_func(key):
|
||||
convert = lambda text: int(text) if text.isdigit() else text
|
||||
return lambda s: [convert(c) for c in re.split('([0-9]+)', key(s))]
|
||||
sort_key = get_alphanum_key_func(key)
|
||||
list.sort(key=sort_key)
|
||||
@ -1,15 +0,0 @@
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
from libs.lib import struct, newAction, newIcon, addActions, fmtShortcut, generateColorByText
|
||||
|
||||
class TestLib(unittest.TestCase):
|
||||
|
||||
def test_generateColorByGivingUniceText_noError(self):
|
||||
res = generateColorByText(u'\u958B\u555F\u76EE\u9304')
|
||||
self.assertTrue(res.green() >= 0)
|
||||
self.assertTrue(res.red() >= 0)
|
||||
self.assertTrue(res.blue() >= 0)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
22
tests/test_utils.py
Normal file
22
tests/test_utils.py
Normal file
@ -0,0 +1,22 @@
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
from libs.utils import struct, newAction, newIcon, addActions, fmtShortcut, generateColorByText, natural_sort
|
||||
|
||||
class TestUtils(unittest.TestCase):
|
||||
|
||||
def test_generateColorByGivingUniceText_noError(self):
|
||||
res = generateColorByText(u'\u958B\u555F\u76EE\u9304')
|
||||
self.assertTrue(res.green() >= 0)
|
||||
self.assertTrue(res.red() >= 0)
|
||||
self.assertTrue(res.blue() >= 0)
|
||||
|
||||
def test_nautalSort_noError(self):
|
||||
l1 = ['f1', 'f11', 'f3' ]
|
||||
exptected_l1 = ['f1', 'f3', 'f11']
|
||||
natural_sort(l1)
|
||||
for idx, val in enumerate(l1):
|
||||
self.assertTrue(val == exptected_l1[idx])
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Loading…
x
Reference in New Issue
Block a user