Adding type annotations and fixing up formatting.
[kiosk.git] / utils.py
index 51a29e7680400268ea09ecda867df6ad41105545..e720ef7d565687999c1d41d5445862ddbb69b6b1 100644 (file)
--- a/utils.py
+++ b/utils.py
@@ -1,13 +1,18 @@
+#!/usr/bin/env python3
+
 import time
 import os
-import constants
 from datetime import datetime
 
-def timestamp():
+import constants
+
+
+def timestamp() -> str:
     t = datetime.fromtimestamp(time.time())
-    return t.strftime('%d/%b/%Y:%H:%M:%S%Z')
+    return t.strftime("%d/%b/%Y:%H:%M:%S%Z")
+
 
-def describe_age_of_file(filename):
+def describe_age_of_file(filename) -> str:
     try:
         now = time.time()
         ts = os.stat(filename).st_ctime
@@ -16,7 +21,8 @@ def describe_age_of_file(filename):
     except Exception as e:
         return "?????"
 
-def describe_age_of_file_briefly(filename):
+
+def describe_age_of_file_briefly(filename) -> str:
     try:
         now = time.time()
         ts = os.stat(filename).st_ctime
@@ -25,39 +31,43 @@ def describe_age_of_file_briefly(filename):
     except Exception as e:
         return "?????"
 
-def describe_duration(age):
+
+def describe_duration(age: int) -> str:
     days = divmod(age, constants.seconds_per_day)
     hours = divmod(days[1], constants.seconds_per_hour)
     minutes = divmod(hours[1], constants.seconds_per_minute)
 
     descr = ""
-    if (days[0] > 1):
-        descr = "%d days, " % days[0]
-    elif (days[0] == 1):
+    if days[0] > 1:
+        descr = f"{int(days[0]):d} days, "
+    elif days[0] == 1:
         descr = "1 day, "
-    if (hours[0] > 1):
-        descr = descr + ("%d hours, " % hours[0])
-    elif (hours[0] == 1):
+    if hours[0] > 1:
+        descr = descr + f"{int(hours[0]):d} hours, "
+    elif hours[0] == 1:
         descr = descr + "1 hour, "
-    if (len(descr) > 0):
+    if len(descr) > 0:
         descr = descr + "and "
-    if (minutes[0] == 1):
+    if minutes[0] == 1:
         descr = descr + "1 minute"
     else:
-        descr = descr + ("%d minutes" % minutes[0])
+        descr = descr + f"{int(minutes[0]):d} minutes"
     return descr
 
-def describe_duration_briefly(age):
+
+def describe_duration_briefly(age: int) -> str:
     days = divmod(age, constants.seconds_per_day)
     hours = divmod(days[1], constants.seconds_per_hour)
     minutes = divmod(hours[1], constants.seconds_per_minute)
+
     descr = ""
-    if (days[0] > 0):
-        descr = "%dd " % days[0]
-    if (hours[0] > 0):
-        descr = descr + ("%dh " % hours[0])
-    descr = descr + ("%dm" % minutes[0])
+    if days[0] > 0:
+        descr = f"{int(days[0]):d}d "
+    if hours[0] > 0:
+        descr = descr + f"{int(hours[0]):d}h "
+    descr = descr + f"{int(minutes[0]):d}m"
     return descr
 
-#x = describe_age_of_file_briefly("pages/clock_10_none.html")
-#print x
+
+# x = describe_age_of_file_briefly("pages/clock_10_none.html")
+# print x