Skip to content

Fix format specifier #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed

Conversation

tekktrik
Copy link
Member

Fixes #95

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

@tekktrik tekktrik requested a review from a team May 23, 2023 03:18
@tekktrik
Copy link
Member Author

Please verify that I implemented to the GPS coordinates correctly!

@dhalbert
Copy link
Contributor

@AMInnovationTeam Want to test this?

@jkittner
Copy link
Contributor

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:

- --disable=consider-using-f-string

@jkittner
Copy link
Contributor

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
Copy link

@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 jkittner mentioned this pull request May 30, 2023
@jkittner
Copy link
Contributor

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
Copy link
Member Author

@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
Copy link
Contributor

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

@tekktrik
Copy link
Member Author

Closing in favor of #102

@tekktrik tekktrik closed this Jul 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ValueError: Format specifier missing precision
4 participants