From 49e6b24f14bc7c14cd08519c497c108856753bbc Mon Sep 17 00:00:00 2001 From: Lawrence Hudson Date: Mon, 12 Dec 2016 13:01:21 +0000 Subject: [PATCH 1/2] [#407] Performance impact of 2to3 --- CHANGELOG.md | 1 + inselect/gui/views/boxes/boxes_scene.py | 4 ++-- inselect/lib/cookie_cutter.py | 2 +- inselect/lib/document.py | 4 ++-- inselect/lib/segment.py | 5 ----- inselect/lib/utils.py | 2 +- inselect/tests/gui/test_export_csv.py | 5 ++++- inselect/tests/scripts/test_export_metadata.py | 5 ++++- 8 files changed, 15 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 580d3b0..e7c0e36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ This is an overview of major changes. Refer to the git repository for a full log Version 0.1.35 ------------- +- #407 Performance impact of 2to3 - #406 Show Boxes view on creating new document - #405 Remove qtpy - #401 Support new pyzbar and pylibdmtx diff --git a/inselect/gui/views/boxes/boxes_scene.py b/inselect/gui/views/boxes/boxes_scene.py index ebc60bf..ab26374 100644 --- a/inselect/gui/views/boxes/boxes_scene.py +++ b/inselect/gui/views/boxes/boxes_scene.py @@ -48,7 +48,7 @@ def pixmap_item(self): """The single QGraphicsPixmapItem within this scene, or None if there is no open document """ - items = list(self.items()) + items = self.items() if not items: return None else: @@ -80,7 +80,7 @@ def set_pixmap(self, pixmap): def box_items(self): "Iterable containin just BoxItems" - return filter(lambda i: isinstance(i, BoxItem), list(self.items())) + return filter(lambda i: isinstance(i, BoxItem), self.items()) def add_box(self, rect, isvalid): """Notification from source that a box has been added. diff --git a/inselect/lib/cookie_cutter.py b/inselect/lib/cookie_cutter.py index 95e8898..425260b 100644 --- a/inselect/lib/cookie_cutter.py +++ b/inselect/lib/cookie_cutter.py @@ -53,7 +53,7 @@ def save(self, path): 'boxes': self._boxes, } with Path(path).open('w', encoding='utf8') as outfile: - outfile.write(str(json.dumps(doc, indent=4))) + outfile.write(json.dumps(doc, indent=4)) @property def document_items(self): diff --git a/inselect/lib/document.py b/inselect/lib/document.py index dfc150d..c8df3f6 100644 --- a/inselect/lib/document.py +++ b/inselect/lib/document.py @@ -290,8 +290,8 @@ def save(self): # http://stackoverflow.com/a/20776329/1773758 # Specify separators to prevent trailing whitespace with path.open("w", newline='\n', encoding='utf8') as f: - f.write(str(json.dumps(doc, ensure_ascii=False, indent=4, - separators=(',', ': '), sort_keys=True))) + f.write(json.dumps(doc, ensure_ascii=False, indent=4, + separators=(',', ': '), sort_keys=True)) debug_print('Saved [{0}] items to [{1}]'.format(len(items), path)) diff --git a/inselect/lib/segment.py b/inselect/lib/segment.py index f2f6a87..4f36fac 100644 --- a/inselect/lib/segment.py +++ b/inselect/lib/segment.py @@ -302,11 +302,6 @@ def swallow(*args, **kwargs): new_rects.append(new_rect) rects = new_rects - # Reverse order so that boxes at the top left are towards the start - # and boxes at the bottom right are towards the end - # TODO LH This is crummy - need a way to order rects - rects = list(reversed(rects)) - return rects, display diff --git a/inselect/lib/utils.py b/inselect/lib/utils.py index 93de394..599bd24 100644 --- a/inselect/lib/utils.py +++ b/inselect/lib/utils.py @@ -124,7 +124,7 @@ def duplicated(v): """Returns a generator expression of values within v that appear more than once """ - return (x for x, y in list(Counter(v).items()) if y > 1) + return (x for x, y in Counter(v).items() if y > 1) class FormatDefault(string.Formatter): diff --git a/inselect/tests/gui/test_export_csv.py b/inselect/tests/gui/test_export_csv.py index 8a0795e..053c864 100644 --- a/inselect/tests/gui/test_export_csv.py +++ b/inselect/tests/gui/test_export_csv.py @@ -33,7 +33,10 @@ def _test_csv(self): 'ItemNumber': '{0}'.format(1 + index), 'Cropped_image_name': '{0:04}.jpg'.format(1 + index), }) - actual = {k: v for k, v in list(row.items()) if v and k not in BOUNDING_BOX_FIELD_NAMES} + actual = { + k: v for k, v in row.items() + if v and k not in BOUNDING_BOX_FIELD_NAMES + } self.assertEqual(expected, actual) # Expect 4 rows diff --git a/inselect/tests/scripts/test_export_metadata.py b/inselect/tests/scripts/test_export_metadata.py index 4523c97..f00c596 100644 --- a/inselect/tests/scripts/test_export_metadata.py +++ b/inselect/tests/scripts/test_export_metadata.py @@ -64,7 +64,10 @@ def test_export_csv(self): 'ItemNumber': str(1+index), 'Cropped_image_name': '{0:04}.jpg'.format(1+index) }) - actual = {k: v for k, v in list(row.items()) if v and k not in BOUNDING_BOX_FIELD_NAMES} + actual = { + k: v for k, v in row.items() + if v and k not in BOUNDING_BOX_FIELD_NAMES + } self.assertEqual(expected, actual) def test_export_csv_with_template(self): From 3a24007c90041385e7d5a6b16669db7db4806d7a Mon Sep 17 00:00:00 2001 From: Lawrence Hudson Date: Mon, 12 Dec 2016 13:18:33 +0000 Subject: [PATCH 2/2] [#407] Performance impact of 2to3 --- inselect/lib/document.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/inselect/lib/document.py b/inselect/lib/document.py index c8df3f6..1c81fbc 100644 --- a/inselect/lib/document.py +++ b/inselect/lib/document.py @@ -249,7 +249,7 @@ def load(cls, path): properties = doc.get('properties', {}) # Parse datetimes - for dt in {'Saved on', 'Created on'}.intersection(list(properties.keys())): + for dt in {'Saved on', 'Created on'}.intersection(properties.keys()): properties[dt] = cls._parse_datetime(properties[dt]) msg = 'Loaded [{0}] items from [{1}]' @@ -274,7 +274,7 @@ def save(self): properties = deepcopy(self.properties) # Format datetimes - for dt in {'Saved on', 'Created on'}.intersection(list(properties.keys())): + for dt in {'Saved on', 'Created on'}.intersection(properties.keys()): properties[dt] = self._format_datetime(properties[dt]) doc = { @@ -336,4 +336,4 @@ def metadata_fields(self): """An iterable of metadata field names """ # The union of fields among all items - return set(chain(*(list(i['fields'].keys()) for i in self._items))) + return set(chain(*(i['fields'].keys() for i in self._items)))