← Back to team overview

openlp-core team mailing list archive

[Merge] lp:~googol/openlp/python3-true-division into lp:openlp

 

Andreas Preikschat has proposed merging lp:~googol/openlp/python3-true-division into lp:openlp.

Requested reviews:
  OpenLP Core (openlp-core)

For more details, see:
https://code.launchpad.net/~googol/openlp/python3-true-division/+merge/164576

Hello,

- use python3 division

There might still be places where we use the old division. Somebody did some refactoring to the slidecontroller so I left the changes there.

http://ci.openlp.org/view/Specific%20Branch/job/OpenLP-Pull_and_Run_Interface_Tests/33/
http://ci.openlp.org/view/Specific%20Branch/job/OpenLP-Pull_and_Run_Functional_Tests/93/

How future division works: Use number // number if you want an integer (like number / number in python2). Number / number gives you a float (like float(number) / float(number in python2).

[andreas@andylaptop python3-true-division]$ python2
Python 2.7.5 (default, May 12 2013, 12:00:47) 
[GCC 4.8.0 20130502 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 1 / 2
0
>>> float(1) / float(2)
0.5
>>> from __future__ import division
>>> 1 / 2
0.5
>>> 1 // 2
0
>>> 
-- 
https://code.launchpad.net/~googol/openlp/python3-true-division/+merge/164576
Your team OpenLP Core is requested to review the proposed merge of lp:~googol/openlp/python3-true-division into lp:openlp.
=== modified file 'openlp/core/lib/__init__.py'
--- openlp/core/lib/__init__.py	2013-04-28 17:17:45 +0000
+++ openlp/core/lib/__init__.py	2013-05-18 08:57:35 +0000
@@ -30,6 +30,7 @@
 The :mod:`lib` module contains most of the components and libraries that make
 OpenLP work.
 """
+from __future__ import division
 from distutils.version import LooseVersion
 import logging
 import os
@@ -202,12 +203,13 @@
         States if an icon should be build and returned from the thumb. Defaults to ``True``.
 
     ``size``
-        Allows to state a own size to use. Defaults to ``None``, which means that a default height of 88 is used.
+        Allows to state a own size (QtCore.QSize) to use. Defaults to ``None``, which means that a default height of 88
+        is used.
     """
     ext = os.path.splitext(thumb_path)[1].lower()
     reader = QtGui.QImageReader(image_path)
     if size is None:
-        ratio = float(reader.size().width()) / float(reader.size().height())
+        ratio = reader.size().width() / reader.size().height()
         reader.setScaledSize(QtCore.QSize(int(ratio * 88), 88))
     else:
         reader.setScaledSize(size)
@@ -260,8 +262,8 @@
     log.debug(u'resize_image - start')
     reader = QtGui.QImageReader(image_path)
     # The image's ratio.
-    image_ratio = float(reader.size().width()) / float(reader.size().height())
-    resize_ratio = float(width) / float(height)
+    image_ratio = reader.size().width() / reader.size().height()
+    resize_ratio = width / height
     # Figure out the size we want to resize the image to (keep aspect ratio).
     if image_ratio == resize_ratio:
         size = QtCore.QSize(width, height)
@@ -282,7 +284,7 @@
     new_image = QtGui.QImage(width, height, QtGui.QImage.Format_ARGB32_Premultiplied)
     painter = QtGui.QPainter(new_image)
     painter.fillRect(new_image.rect(), QtGui.QColor(background))
-    painter.drawImage((width - real_width) / 2, (height - real_height) / 2, preview)
+    painter.drawImage((width - real_width) // 2, (height - real_height) // 2, preview)
     return new_image
 
 

=== modified file 'openlp/core/lib/dockwidget.py'
--- openlp/core/lib/dockwidget.py	2013-03-16 11:05:52 +0000
+++ openlp/core/lib/dockwidget.py	2013-05-18 08:57:35 +0000
@@ -30,6 +30,7 @@
 """
 Provide additional functionality required by OpenLP from the inherited QDockWidget.
 """
+from __future__ import division
 import logging
 
 from PyQt4 import QtGui
@@ -55,7 +56,7 @@
             self.setWindowIcon(build_icon(icon))
         # Sort out the minimum width.
         screens = ScreenList()
-        main_window_docbars = screens.current[u'size'].width() / 5
+        main_window_docbars = screens.current[u'size'].width() // 5
         if main_window_docbars > 300:
             self.setMinimumWidth(300)
         else:

=== modified file 'openlp/core/lib/htmlbuilder.py'
--- openlp/core/lib/htmlbuilder.py	2013-03-19 19:43:22 +0000
+++ openlp/core/lib/htmlbuilder.py	2013-05-18 08:57:35 +0000
@@ -26,7 +26,7 @@
 # with this program; if not, write to the Free Software Foundation, Inc., 59  #
 # Temple Place, Suite 330, Boston, MA 02111-1307 USA                          #
 ###############################################################################
-
+from __future__ import division
 import logging
 
 from PyQt4 import QtWebKit
@@ -276,7 +276,7 @@
     ``item``
         Service Item containing theme and location information
     """
-    width = int(width) / 2
+    width = int(width) // 2
     theme = item.themedata
     background = u'background-color: black'
     if theme:

=== modified file 'openlp/core/lib/renderer.py'
--- openlp/core/lib/renderer.py	2013-03-14 20:21:04 +0000
+++ openlp/core/lib/renderer.py	2013-05-18 08:57:35 +0000
@@ -26,7 +26,7 @@
 # with this program; if not, write to the Free Software Foundation, Inc., 59  #
 # Temple Place, Suite 330, Boston, MA 02111-1307 USA                          #
 ###############################################################################
-
+from __future__ import division
 import logging
 
 from PyQt4 import QtGui, QtCore, QtWebKit
@@ -327,7 +327,7 @@
         screen_size = self.screens.current[u'size']
         self.width = screen_size.width()
         self.height = screen_size.height()
-        self.screen_ratio = float(self.height) / float(self.width)
+        self.screen_ratio = self.height / self.width
         log.debug(u'_calculate default %s, %f' % (screen_size, self.screen_ratio))
         # 90% is start of footer
         self.footer_start = int(self.height * 0.90)
@@ -546,15 +546,15 @@
         """
         smallest_index = 0
         highest_index = len(html_list) - 1
-        index = int(highest_index / 2)
+        index = highest_index // 2
         while True:
             if not self._text_fits_on_slide(previous_html + separator.join(html_list[:index + 1]).strip()):
                 # We know that it does not fit, so change/calculate the new index and highest_index accordingly.
                 highest_index = index
-                index = int(index - (index - smallest_index) / 2)
+                index = index - (index - smallest_index) // 2
             else:
                 smallest_index = index
-                index = int(index + (highest_index - index) / 2)
+                index = index + (highest_index - index) // 2
             # We found the number of words which will fit.
             if smallest_index == index or highest_index == index:
                 index = smallest_index
@@ -582,7 +582,7 @@
                 html_list[0] = html_tags + html_list[0]
                 smallest_index = 0
                 highest_index = len(html_list) - 1
-                index = int(highest_index / 2)
+                index = highest_index // 2
         return previous_html, previous_raw
 
     def _text_fits_on_slide(self, text):

=== modified file 'openlp/core/lib/screen.py'
--- openlp/core/lib/screen.py	2013-03-26 11:35:29 +0000
+++ openlp/core/lib/screen.py	2013-05-18 08:57:35 +0000
@@ -30,6 +30,7 @@
 The :mod:`screen` module provides management functionality for a machines'
 displays.
 """
+from __future__ import division
 import logging
 import copy
 
@@ -232,8 +233,8 @@
         ``window``
             A QWidget we are finding the location of.
         """
-        x = window.x() + (window.width() / 2)
-        y = window.y() + (window.height() / 2)
+        x = window.x() + (window.width() // 2)
+        y = window.y() + (window.height() // 2)
         for screen in self.screen_list:
             size = screen[u'size']
             if x >= size.x() and x <= (size.x() + size.width()) and y >= size.y() and y <= (size.y() + size.height()):

=== modified file 'openlp/core/lib/searchedit.py'
--- openlp/core/lib/searchedit.py	2013-03-06 22:11:30 +0000
+++ openlp/core/lib/searchedit.py	2013-05-18 08:57:35 +0000
@@ -26,7 +26,7 @@
 # with this program; if not, write to the Free Software Foundation, Inc., 59  #
 # Temple Place, Suite 330, Boston, MA 02111-1307 USA                          #
 ###############################################################################
-
+from __future__ import division
 import logging
 
 from PyQt4 import QtCore, QtGui
@@ -85,10 +85,10 @@
         size = self.clear_button.size()
         frame_width = self.style().pixelMetric(QtGui.QStyle.PM_DefaultFrameWidth)
         self.clear_button.move(self.rect().right() - frame_width - size.width(),
-            (self.rect().bottom() + 1 - size.height()) / 2)
+            (self.rect().bottom() + 1 - size.height()) // 2)
         if hasattr(self, u'menu_button'):
             size = self.menu_button.size()
-            self.menu_button.move(self.rect().left() + frame_width + 2, (self.rect().bottom() + 1 - size.height()) / 2)
+            self.menu_button.move(self.rect().left() + frame_width + 2, (self.rect().bottom() + 1 - size.height()) // 2)
 
     def current_search_type(self):
         """

=== modified file 'openlp/core/lib/settingstab.py'
--- openlp/core/lib/settingstab.py	2013-03-17 21:29:00 +0000
+++ openlp/core/lib/settingstab.py	2013-05-18 08:57:35 +0000
@@ -30,6 +30,7 @@
 The :mod:`~openlp.core.lib.settingstab` module contains the base SettingsTab class which plugins use for adding their
 own tab to the settings dialog.
 """
+from __future__ import division
 
 from PyQt4 import QtGui
 
@@ -90,7 +91,7 @@
             QtGui.QWidget.resizeEvent(self, event)
         width = self.width() - self.tab_layout.spacing() - \
             self.tab_layout.contentsMargins().left() - self.tab_layout.contentsMargins().right()
-        left_width = min(width - self.right_column.minimumSizeHint().width(), width / 2)
+        left_width = min(width - self.right_column.minimumSizeHint().width(), width // 2)
         left_width = max(left_width, self.left_column.minimumSizeHint().width())
         self.left_column.setFixedWidth(left_width)
 

=== modified file 'openlp/core/ui/maindisplay.py'
--- openlp/core/ui/maindisplay.py	2013-03-30 08:40:49 +0000
+++ openlp/core/ui/maindisplay.py	2013-05-18 08:57:35 +0000
@@ -35,6 +35,7 @@
 * `http://html5demos.com/two-videos`_
 
 """
+from __future__ import division
 import cgi
 import logging
 import sys
@@ -207,8 +208,8 @@
             painter_image.begin(self.initial_fame)
             painter_image.fillRect(self.initial_fame.rect(), background_color)
             painter_image.drawImage(
-                (self.screen[u'size'].width() - splash_image.width()) / 2,
-                (self.screen[u'size'].height() - splash_image.height()) / 2,
+                (self.screen[u'size'].width() - splash_image.width()) // 2,
+                (self.screen[u'size'].height() - splash_image.height()) // 2,
                 splash_image)
             service_item = ServiceItem()
             service_item.bg_image_bytes = image_to_byte(self.initial_fame)
@@ -268,7 +269,7 @@
                 self.resize(self.width(), alert_height)
                 self.setVisible(True)
                 if location == AlertLocation.Middle:
-                    self.move(self.screen[u'size'].left(), (self.screen[u'size'].height() - alert_height) / 2)
+                    self.move(self.screen[u'size'].left(), (self.screen[u'size'].height() - alert_height) // 2)
                 elif location == AlertLocation.Bottom:
                     self.move(self.screen[u'size'].left(), self.screen[u'size'].height() - alert_height)
             else:

=== modified file 'openlp/core/ui/themestab.py'
--- openlp/core/ui/themestab.py	2013-03-25 07:27:54 +0000
+++ openlp/core/ui/themestab.py	2013-05-18 08:57:35 +0000
@@ -29,6 +29,8 @@
 """
 The Themes configuration tab
 """
+from __future__ import division
+
 from PyQt4 import QtCore, QtGui
 
 from openlp.core.lib import Registry, Settings, SettingsTab, UiStrings, translate
@@ -90,7 +92,7 @@
         self.global_level_label.setObjectName(u'global_level_label')
         self.level_layout.addRow(self.global_level_radio_button, self.global_level_label)
         label_top_margin = (self.song_level_radio_button.sizeHint().height() -
-            self.song_level_label.sizeHint().height()) / 2
+            self.song_level_label.sizeHint().height()) // 2
         for label in [self.song_level_label, self.service_level_label, self.global_level_label]:
             rect = label.rect()
             rect.setTop(rect.top() + label_top_margin)

=== modified file 'tests/functional/openlp_core_lib/test_lib.py'
--- tests/functional/openlp_core_lib/test_lib.py	2013-03-30 08:46:34 +0000
+++ tests/functional/openlp_core_lib/test_lib.py	2013-05-18 08:57:35 +0000
@@ -1,13 +1,20 @@
 """
 Package to test the openlp.core.lib package.
 """
+import os
+
 from unittest import TestCase
 from datetime import datetime, timedelta
 
 from mock import MagicMock, patch
-
-from openlp.core.lib import str_to_bool, translate, check_directory_exists, get_text_file_string, build_icon, \
-    image_to_byte, check_item_selected, validate_thumb, create_separated_list, clean_tags, expand_tags
+from PyQt4 import QtCore, QtGui
+
+from openlp.core.lib import str_to_bool, create_thumb, translate, check_directory_exists, get_text_file_string, \
+    build_icon, image_to_byte, check_item_selected, validate_thumb, create_separated_list, clean_tags, expand_tags
+
+
+TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'..', u'..', u'resources'))
+
 
 class TestLib(TestCase):
 
@@ -125,7 +132,7 @@
         Test the check_directory_exists() function
         """
         with patch(u'openlp.core.lib.os.path.exists') as mocked_exists, \
-             patch(u'openlp.core.lib.os.makedirs') as mocked_makedirs:
+                patch(u'openlp.core.lib.os.makedirs') as mocked_makedirs:
             # GIVEN: A directory to check and a mocked out os.makedirs and os.path.exists
             directory_to_check = u'existing/directory'
 
@@ -219,7 +226,7 @@
         Test the build_icon() function with a resource URI
         """
         with patch(u'openlp.core.lib.QtGui') as MockedQtGui, \
-             patch(u'openlp.core.lib.QtGui.QPixmap') as MockedQPixmap:
+                patch(u'openlp.core.lib.QtGui.QPixmap') as MockedQPixmap:
             # GIVEN: A mocked QIcon and a mocked QPixmap
             MockedQtGui.QIcon = MagicMock
             MockedQtGui.QIcon.Normal = 1
@@ -261,9 +268,43 @@
             mocked_byte_array.toBase64.assert_called_with()
             assert result == u'base64mock', u'The result should be the return value of the mocked out base64 method'
 
+    def create_thumb_with_size_test(self):
+        """
+        Test the create_thumb() function
+        """
+        # GIVEN: An image to create a thumb of.
+        image_path = os.path.join(TEST_PATH, u'church.jpg')
+        thumb_path = os.path.join(TEST_PATH, u'church_thumb.jpg')
+        thumb_size = QtCore.QSize(10, 20)
+
+        # Remove the thumb so that the test actually tests if the thumb will be created. Maybe it was not deleted in the
+        # last test.
+        try:
+            os.remove(thumb_path)
+        except:
+            pass
+
+        # Only continue when the thumb does not exist.
+        assert not os.path.exists(thumb_path), u'Test was not ran, because the thumb already exists.'
+
+        # WHEN: Create the thumb.
+        icon = create_thumb(image_path, thumb_path, size=thumb_size)
+
+        # THEN: Check if the thumb was created.
+        assert os.path.exists(thumb_path), u'Test was not ran, because the thumb already exists.'
+        assert isinstance(icon, QtGui.QIcon), u'The icon should be a QIcon.'
+        assert not icon.isNull(), u'The icon should not be null.'
+        assert QtGui.QImageReader(thumb_path).size() == thumb_size, u'The thumb should have the given size.'
+
+        # Remove the thumb so that the test actually tests if the thumb will be created.
+        try:
+            os.remove(thumb_path)
+        except:
+            pass
+
     def check_item_selected_true_test(self):
         """
-        Test that the check_item_selected() function returns True when there are selected indexes.
+        Test that the check_item_selected() function returns True when there are selected indexes
         """
         # GIVEN: A mocked out QtGui module and a list widget with selected indexes
         MockedQtGui = patch(u'openlp.core.lib.QtGui')
@@ -423,7 +464,7 @@
 
     def create_separated_list_qlocate_test(self):
         """
-        Test the create_separated_list function using the Qt provided method.
+        Test the create_separated_list function using the Qt provided method
         """
         with patch(u'openlp.core.lib.Qt') as mocked_qt, \
                 patch(u'openlp.core.lib.QtCore.QLocale.createSeparatedList') as mocked_createSeparatedList:
@@ -442,7 +483,7 @@
 
     def create_separated_list_empty_list_test(self):
         """
-        Test the create_separated_list function with an empty list.
+        Test the create_separated_list function with an empty list
         """
         with patch(u'openlp.core.lib.Qt') as mocked_qt:
             # GIVEN: An empty list and the mocked Qt module.
@@ -458,7 +499,7 @@
 
     def create_separated_list_with_one_item_test(self):
         """
-        Test the create_separated_list function with a list consisting of only one entry.
+        Test the create_separated_list function with a list consisting of only one entry
         """
         with patch(u'openlp.core.lib.Qt') as mocked_qt:
             # GIVEN: A list with a string and the mocked Qt module.
@@ -474,7 +515,7 @@
 
     def create_separated_list_with_two_items_test(self):
         """
-        Test the create_separated_list function with a list of two entries.
+        Test the create_separated_list function with a list of two entries
         """
         with patch(u'openlp.core.lib.Qt') as mocked_qt, patch(u'openlp.core.lib.translate') as mocked_translate:
             # GIVEN: A list of strings and the mocked Qt module.
@@ -491,7 +532,7 @@
 
     def create_separated_list_with_three_items_test(self):
         """
-        Test the create_separated_list function with a list of three items.
+        Test the create_separated_list function with a list of three items
         """
         with patch(u'openlp.core.lib.Qt') as mocked_qt, patch(u'openlp.core.lib.translate') as mocked_translate:
             # GIVEN: A list with a string and the mocked Qt module.


Follow ups