Small code optimizations
authorThierry Florac <thierry.florac@onf.fr>
Mon, 02 Jul 2018 17:07:48 +0200
changeset 114 06e85cf61575
parent 113 97e1ed0fc7c7
child 115 6d17113a6f9e
Small code optimizations
src/pyams_file/image.py
src/pyams_file/thumbnail.py
--- a/src/pyams_file/image.py	Mon Jul 02 17:07:25 2018 +0200
+++ b/src/pyams_file/image.py	Mon Jul 02 17:07:48 2018 +0200
@@ -68,7 +68,7 @@
     def create_thumbnail(self, target, format=None):
         # check thumbnail name
         if isinstance(target, str):
-            width, height = tuple((int(x) for x in target.split('x')))
+            width, height = tuple(map(int, target.split('x')))
         elif IThumbnailGeometry.providedBy(target):
             width = target.x2 - target.x1
             height = target.y2 - target.y1
@@ -108,8 +108,7 @@
             geometry = IThumbnail(self.context).get_geometry(target)
             match = THUMB_SIZE.match(target)
             if match:
-                selection_name, width, height = match.groups()
-                width, height = int(width), int(height)
+                width, height = tuple(map(int, match.groups()))
             else:
                 width = abs(geometry.x2 - geometry.x1)
                 height = abs(geometry.y2 - geometry.y1)
--- a/src/pyams_file/thumbnail.py	Mon Jul 02 17:07:25 2018 +0200
+++ b/src/pyams_file/thumbnail.py	Mon Jul 02 17:07:48 2018 +0200
@@ -23,6 +23,7 @@
 from zope.traversing.interfaces import ITraversable
 
 # import packages
+from BTrees import OOBTree
 from persistent.dict import PersistentDict
 from pyams_file.file import FileFactory
 from pyams_file.zmi.image import render_image
@@ -50,7 +51,7 @@
 
     def __init__(self, image):
         self.image = image
-        self.thumbnails = get_annotation_adapter(image, THUMBNAIL_ANNOTATIONS_KEY, PersistentDict,
+        self.thumbnails = get_annotation_adapter(image, THUMBNAIL_ANNOTATIONS_KEY, OOBTree.OOBTree,
                                                  notify=False, locate=False)
 
     def get_image_size(self):
@@ -104,15 +105,15 @@
         geometries = get_annotation_adapter(self.image, THUMBNAIL_GEOMETRY_KEY, PersistentDict,
                                             notify=False, locate=False)
         geometries[selection_name] = geometry
-        for current_thumbnail_name in self.thumbnails.copy():
+        for current_thumbnail_name in list(self.thumbnails.keys()):
             if (current_thumbnail_name == selection_name) or \
-               current_thumbnail_name.startswith(selection_name + ':'):
+               current_thumbnail_name.startswith('{0}:'.format(selection_name)):
                 self.delete_thumbnail(current_thumbnail_name)
 
     def clear_geometries(self):
         geometries = get_annotation_adapter(self.image, THUMBNAIL_GEOMETRY_KEY)
         if geometries is not None:
-            for geometry_name in geometries.copy():
+            for geometry_name in list(geometries.keys()):
                 del geometries[geometry_name]
 
     def get_thumbnail_name(self, thumbnail_name, with_size=False):
@@ -211,7 +212,7 @@
             del self.thumbnails[thumbnail_name]
 
     def clear_thumbnails(self):
-        [self.delete_thumbnail(thumbnail_name) for thumbnail_name in self.thumbnails.copy()]
+        [self.delete_thumbnail(thumbnail_name) for thumbnail_name in list(self.thumbnails.keys())]
 
 
 @subscriber(IFileModifiedEvent, context_selector=IImage)