adafruit / Adafruit_CircuitPython_GPS

GPS parsing module for CircuitPython. Meant to parse NMEA data from serial GPS modules.
MIT License
75 stars 58 forks source link

Fix format specifier #99

Closed tekktrik closed 1 year ago

tekktrik commented 1 year ago

Fixes #95

Should switch to using f-strings at some point, but wanted to submit a quick fix for the issue.

tekktrik commented 1 year ago

Please verify that I implemented to the GPS coordinates correctly!

dhalbert commented 1 year ago

@AMInnovationTeam Want to test this?

jkittner commented 1 year ago

easiest option would be pyupgrade https://github.com/asottile/pyupgrade#printf-style-string-formatting - could be added to pre-commit.

after that, this should be removed, since it's superfluous: https://github.com/adafruit/Adafruit_CircuitPython_GPS/blob/4b53a07978cac574f80dfc398cd202150ce628db/.pre-commit-config.yaml#L27

jkittner commented 1 year ago

That's what pyupgrade would do:

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 70ade69..78c8c61 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -7,6 +7,12 @@ repos:
     rev: 23.3.0
     hooks:
       - id: black
+  - repo: https://github.com/asottile/pyupgrade
+    rev: v3.4.0
+    hooks:
+      - id: pyupgrade
+        args:
+          - --py37-plus
   - repo: https://github.com/fsfe/reuse-tool
     rev: v1.1.2
     hooks:
diff --git a/adafruit_gps.py b/adafruit_gps.py
index e97ca46..b727c94 100644
--- a/adafruit_gps.py
+++ b/adafruit_gps.py
@@ -370,7 +370,7 @@ class GPS:
             for char in command:
                 checksum ^= char
             self.write(b"*")
-            self.write(bytes("{:02x}".format(checksum).upper(), "ascii"))
+            self.write(bytes(f"{checksum:02x}".upper(), "ascii"))
         self.write(b"\r\n")

     @property
@@ -626,7 +626,7 @@ class GPS:
         satlist = list(filter(None, data[2:-4]))
         self.sat_prns = []
         for sat in satlist:
-            self.sat_prns.append("{}{}".format(talker, sat))
+            self.sat_prns.append(f"{talker}{sat}")

         # PDOP, dilution of precision
         self.pdop = _parse_float(data[14])
@@ -671,7 +671,7 @@ class GPS:
             j = i * 4
             value = (
                 # Satellite number
-                "{}{}".format(talker, sat_tup[0 + j]),
+                f"{talker}{sat_tup[0 + j]}",
                 # Elevation in degrees
                 sat_tup[1 + j],
                 # Azimuth in degrees
diff --git a/docs/conf.py b/docs/conf.py
index ab83199..6ae5711 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -1,5 +1,3 @@
-# -*- coding: utf-8 -*-
-
 # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 #
 # SPDX-License-Identifier: MIT
diff --git a/examples/gps_simpletest.py b/examples/gps_simpletest.py
index 6434f27..b9ef922 100644
--- a/examples/gps_simpletest.py
+++ b/examples/gps_simpletest.py
@@ -82,8 +82,8 @@ while True:
                 gps.timestamp_utc.tm_sec,
             )
         )
-        print("Latitude: {0:.6f} degrees".format(gps.latitude))
-        print("Longitude: {0:.6f} degrees".format(gps.longitude))
+        print(f"Latitude: {gps.latitude:.6f} degrees")
+        print(f"Longitude: {gps.longitude:.6f} degrees")
         print(
             "Precise Latitude: {:3.0f} degs, {:2.4f} mins".format(
                 math.floor(gps.latitude_degrees), gps.latitude_minutes
@@ -94,18 +94,18 @@ while True:
                 math.floor(gps.longitude_degrees), gps.longitude_minutes
             )
         )
-        print("Fix quality: {}".format(gps.fix_quality))
+        print(f"Fix quality: {gps.fix_quality}")
         # Some attributes beyond latitude, longitude and timestamp are optional
         # and might not be present.  Check if they're None before trying to use!
         if gps.satellites is not None:
-            print("# satellites: {}".format(gps.satellites))
+            print(f"# satellites: {gps.satellites}")
         if gps.altitude_m is not None:
-            print("Altitude: {} meters".format(gps.altitude_m))
+            print(f"Altitude: {gps.altitude_m} meters")
         if gps.speed_knots is not None:
-            print("Speed: {} knots".format(gps.speed_knots))
+            print(f"Speed: {gps.speed_knots} knots")
         if gps.track_angle_deg is not None:
-            print("Track angle: {} degrees".format(gps.track_angle_deg))
+            print(f"Track angle: {gps.track_angle_deg} degrees")
         if gps.horizontal_dilution is not None:
-            print("Horizontal dilution: {}".format(gps.horizontal_dilution))
+            print(f"Horizontal dilution: {gps.horizontal_dilution}")
         if gps.height_geoid is not None:
-            print("Height geoid: {} meters".format(gps.height_geoid))
+            print(f"Height geoid: {gps.height_geoid} meters")
diff --git a/examples/gps_time_source.py b/examples/gps_time_source.py
index 0199ca5..baf4714 100644
--- a/examples/gps_time_source.py
+++ b/examples/gps_time_source.py
@@ -47,12 +47,12 @@ while True:
             print("No time data from GPS yet")
             continue
         # Time & date from GPS informations
-        print("Fix timestamp: {}".format(_format_datetime(gps.timestamp_utc)))
+        print(f"Fix timestamp: {_format_datetime(gps.timestamp_utc)}")

         # Time & date from internal RTC
-        print("RTC timestamp: {}".format(_format_datetime(the_rtc.datetime)))
+        print(f"RTC timestamp: {_format_datetime(the_rtc.datetime)}")

         # Time & date from time.localtime() function
         local_time = time.localtime()

-        print("Local time: {}".format(_format_datetime(local_time)))
+        print(f"Local time: {_format_datetime(local_time)}")
AMInnovationTeam commented 1 year ago

@AMInnovationTeam Want to test this?

Hi @dhalbert, I tested it but I don't think the names described in the string are accurate. For example, it provides the output in "Degrees" and "Minutes". When I ask an online calculate to do the conversion, it provides a different "Minute" output.

Original 874411cc-d136-4da7-981e-f1d2d2600ad9

Precise 93d3abc2-bd6b-4a6a-9085-8b31c1dce434

Online Calculator 6b7a5996-04c7-4c2e-9d86-ac96820f7e32

jkittner commented 1 year ago

There was a bug in the code. I created #102 to fix this and added tests to verify the behavior. Maybe we can merge #102 first and then this one should be correct as well?

tekktrik commented 1 year ago

@jkittner if you want to merge in changes from this PR into yours, I can close this one. Or would you prefer me to merge now?

jkittner commented 1 year ago

@tekktrik - I cherry-picked your two commits onto #102. so we can simply merge #102 and cover both I guess.

tekktrik commented 1 year ago

Closing in favor of #102