gpsd-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gpsd-dev] [PATCH 1/4] Fixes integer division for Python 3.


From: Fred Wright
Subject: [gpsd-dev] [PATCH 1/4] Fixes integer division for Python 3.
Date: Sat, 9 Apr 2016 19:57:13 -0700

This changes a few integer division cases to use the '//'
floored-division operator, matching the normal Python 2 (C-like) '/'
behavior, for compatibility with the switch to the "mathematical"
divison operator in Python 3 (and in Python 2 with the "future
division" import).  This was more about keeping the behavior the same
than determining whether floored division is actually the proper
choice.

One place where floored division is definitely wanted is in the GPS
week calculations in leapsecond.py, which are now OK for Python 3,
though currently that module is only used by SConstruct, and hence not
with Python 3.

Two other minor fixes:

1) The GPS base date is corrected in the comment in leapsecond.py.

2) The fit_to_grid() function in xgps now consistently returns floats,
rather than returning either ints or floats depending on the line
width.

TESTED:
Ran "scons build-all check" with all 6 supported Python versions.
Also ran xgps and xgpsspeed with all but 2.6.
---
 leapsecond.py |  6 +++---
 xgps          | 12 ++++++------
 xgpsspeed     |  2 +-
 3 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/leapsecond.py b/leapsecond.py
index dcf7b96..3bd13e2 100755
--- a/leapsecond.py
+++ b/leapsecond.py
@@ -74,17 +74,17 @@ __locations = [
     ),
 ]
 
-GPS_EPOCH = 315964800               # 6 Jan 1981 00:00:00
+GPS_EPOCH = 315964800               # 6 Jan 1980 00:00:00
 SECS_PER_WEEK = 60 * 60 * 24 * 7    # Seconds per GPS week
 ROLLOVER = 1024                     # 10-bit week rollover
 
 
 def gps_week(t):
-    return (t - GPS_EPOCH) / SECS_PER_WEEK % ROLLOVER
+    return (t - GPS_EPOCH) // SECS_PER_WEEK % ROLLOVER
 
 
 def gps_rollovers(t):
-    return (t - GPS_EPOCH) / SECS_PER_WEEK / ROLLOVER
+    return (t - GPS_EPOCH) // SECS_PER_WEEK // ROLLOVER
 
 
 def isotime(s):
diff --git a/xgps b/xgps
index d43ff7d..f9b986e 100755
--- a/xgps
+++ b/xgps
@@ -69,10 +69,10 @@ def fit_to_grid(x, y, line_width):
     "Adjust coordinates to produce sharp lines."
     if line_width % 1.0 != 0:
         # Can't have sharp lines for non-integral line widths.
-        return x, y
+        return float(x), float(y)  # Be consistent about returning floats
     if line_width % 2 == 0:
         # Round to a pixel corner.
-        return int(x + 0.5), int(y + 0.5)
+        return round(x), round(y)
     else:
         # Round to a pixel center.
         return int(x) + 0.5, int(y) + 0.5
@@ -107,9 +107,9 @@ class SkyView(Gtk.DrawingArea):
     def on_size_allocate(self, _unused, allocation):
         width = allocation.width
         height = allocation.height
-        x = width / 2
-        y = height / 2
-        r = (min(width, height) - SkyView.HORIZON_PAD) / 2
+        x = width // 2
+        y = height // 2
+        r = (min(width, height) - SkyView.HORIZON_PAD) // 2
         x, y, r = fit_circle_to_grid(x, y, r, 1)
         self.center_x = x
         self.center_y = y
@@ -126,7 +126,7 @@ class SkyView(Gtk.DrawingArea):
     def draw_circle(self, x, y, radius, filled=False):
         "Draw a circle centered on the specified midpoint."
         lw = self.cr.get_line_width()
-        r = int(2 * radius + 0.5) / 2
+        r = int(2 * radius + 0.5) // 2
 
         x, y, r = fit_circle_to_grid(x, y, radius, lw)
 
diff --git a/xgpsspeed b/xgpsspeed
index b0dda09..bba8ec4 100755
--- a/xgpsspeed
+++ b/xgpsspeed
@@ -311,7 +311,7 @@ class NauticalSpeedometer(Speedometer):
             self.cr.stroke()
             self.cr.set_line_width(radius / 200)
             xf, yf = NauticalSpeedometer.polar2xy(rspeed + 10, alpha, x, y)
-            stxt = (self.maxspeed / 10) * i
+            stxt = (self.maxspeed // 10) * i
             self.draw_text(xf, yf, stxt, fontsize=radius / 15)
 
         for i in range(1, 11):
-- 
2.8.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]