[tor-commits] [stem/master] Permit float in label functions

atagar at torproject.org atagar at torproject.org
Wed Oct 28 23:53:23 UTC 2020


commit 84602486f5a6eb2c4558656ca4f532e4bc81e92b
Author: Damian Johnson <atagar at torproject.org>
Date:   Wed Oct 28 16:40:36 2020 -0700

    Permit float in label functions
    
    Oops. Actually, our last release allowed floats (and our 'decimal' argument
    doesn't make terribly much sense otherwise). We *did* have a bug in that
    remainder values that are less than 1 crashed us (corrected by commit c90e87c),
    but we shouldn't have addressed that by integer normalization.
    
    Regression caught thanks to Nyx's tests...
    
      ======================================================================
      FAIL: test_draw_line (panel.connection.TestConnectionPanel)
      ----------------------------------------------------------------------
      Traceback (most recent call last):
        File "/home/atagar/Desktop/nyx/test/__init__.py", line 59, in wrapped
          return func(self, *args, **kwargs)
        File "/usr/lib/python3.8/unittest/mock.py", line 1325, in patched
          return func(*newargs, **newkeywargs)
        File "/home/atagar/Desktop/nyx/test/panel/connection.py", line 228, in test_draw_line
          self.assertEqual(expected, rendered.content)
      AssertionError: ' 75.119.206.243:22 (de)      -->  82.121.9.9:3531              15.4s (INBOUND)' != ' 75.119.206.243:22 (de)      -->  82.121.9.9:3531              14.9s (INBOUND)'
      -  75.119.206.243:22 (de)      -->  82.121.9.9:3531              15.4s (INBOUND)
      ?                                                                 ^ ^
      +  75.119.206.243:22 (de)      -->  82.121.9.9:3531              14.9s (INBOUND)
      ?                                                                 ^ ^
---
 docs/change_log.rst         |  1 +
 stem/util/str_tools.py      | 24 ++++++------------------
 test/unit/util/str_tools.py |  1 +
 3 files changed, 8 insertions(+), 18 deletions(-)

diff --git a/docs/change_log.rst b/docs/change_log.rst
index cc830d6c..b3544569 100644
--- a/docs/change_log.rst
+++ b/docs/change_log.rst
@@ -65,6 +65,7 @@ The following are only available within Stem's `git repository
 
  * **Descriptors**
 
+  * Cached CollecTor files always reported a hash mismatch (:ticket:`76`)
   * *transport* lines within extrainfo descriptors failed to validate
 
  * **Utilities**
diff --git a/stem/util/str_tools.py b/stem/util/str_tools.py
index 9803d437..a9a34364 100644
--- a/stem/util/str_tools.py
+++ b/stem/util/str_tools.py
@@ -298,7 +298,7 @@ def crop(msg: str, size: int, min_word_length: int = 4, min_crop: int = 0, endin
   return (return_msg, remainder) if get_remainder else return_msg
 
 
-def size_label(byte_count: int, decimal: int = 0, is_long: bool = False, is_bytes: bool = True, round: bool = False) -> str:
+def size_label(byte_count: Union[int, float], decimal: int = 0, is_long: bool = False, is_bytes: bool = True, round: bool = False) -> str:
   """
   Converts a number of bytes into a human readable label in its most
   significant units. For instance, 7500 bytes would return "7 KB". If the
@@ -329,16 +329,13 @@ def size_label(byte_count: int, decimal: int = 0, is_long: bool = False, is_byte
   :returns: **str** with human readable representation of the size
   """
 
-  if isinstance(byte_count, float):
-    byte_count = int(byte_count)
-
   if is_bytes:
     return _get_label(SIZE_UNITS_BYTES, byte_count, decimal, is_long, round)
   else:
     return _get_label(SIZE_UNITS_BITS, byte_count, decimal, is_long, round)
 
 
-def time_label(seconds: int, decimal: int = 0, is_long: bool = False) -> str:
+def time_label(seconds: Union[int, float], decimal: int = 0, is_long: bool = False) -> str:
   """
   Converts seconds into a time label truncated to its most significant units.
   For instance, 7500 seconds would return "2h". Units go up through days.
@@ -366,13 +363,10 @@ def time_label(seconds: int, decimal: int = 0, is_long: bool = False) -> str:
   :returns: **str** with human readable representation of the time
   """
 
-  if isinstance(seconds, float):
-    seconds = int(seconds)
-
   return _get_label(TIME_UNITS, seconds, decimal, is_long)
 
 
-def time_labels(seconds: int, is_long: bool = False) -> Sequence[str]:
+def time_labels(seconds: Union[int, float], is_long: bool = False) -> Sequence[str]:
   """
   Provides a list of label conversions for each time unit, starting with its
   most significant units on down. Any counts that evaluate to zero are omitted.
@@ -392,9 +386,6 @@ def time_labels(seconds: int, is_long: bool = False) -> Sequence[str]:
   :returns: **list** of strings with human readable representations of the time
   """
 
-  if isinstance(seconds, float):
-    seconds = int(seconds)
-
   time_labels = []
 
   for count_per_unit, _, _ in TIME_UNITS:
@@ -405,7 +396,7 @@ def time_labels(seconds: int, is_long: bool = False) -> Sequence[str]:
   return time_labels
 
 
-def short_time_label(seconds: int) -> str:
+def short_time_label(seconds: Union[int, float]) -> str:
   """
   Provides a time in the following format:
   [[dd-]hh:]mm:ss
@@ -425,9 +416,6 @@ def short_time_label(seconds: int) -> str:
   :raises: **ValueError** if the input is negative
   """
 
-  if isinstance(seconds, float):
-    seconds = int(seconds)
-
   if seconds < 0:
     raise ValueError("Input needs to be a non-negative integer, got '%i'" % seconds)
 
@@ -557,7 +545,7 @@ def _parse_iso_timestamp(entry: str) -> 'datetime.datetime':
   return timestamp + datetime.timedelta(microseconds = int(microseconds))
 
 
-def _get_label(units: Sequence[Tuple[float, str, str]], count: int, decimal: int, is_long: bool, round: bool = False) -> str:
+def _get_label(units: Sequence[Tuple[float, str, str]], count: Union[int, float], decimal: int, is_long: bool, round: bool = False) -> str:
   """
   Provides label corresponding to units of the highest significance in the
   provided set. This rounds down (ie, integer truncation after visible units).
@@ -582,7 +570,7 @@ def _get_label(units: Sequence[Tuple[float, str, str]], count: int, decimal: int
     return '%s%s' % (label_format % count, units_label)
 
   for count_per_unit, short_label, long_label in units:
-    if remainder >= count_per_unit:
+    if remainder >= count_per_unit or count_per_unit == 1:
       if not round:
         # Rounding down with a '%f' is a little clunky. Reducing the remainder
         # so it'll divide evenly as the rounded down value.
diff --git a/test/unit/util/str_tools.py b/test/unit/util/str_tools.py
index 8b0365f7..73c7b4d9 100644
--- a/test/unit/util/str_tools.py
+++ b/test/unit/util/str_tools.py
@@ -99,6 +99,7 @@ class TestStrTools(unittest.TestCase):
     self.assertEqual('0s', str_tools.time_label(0.1))
     self.assertEqual('0 seconds', str_tools.time_label(0, is_long = True))
     self.assertEqual('0.00s', str_tools.time_label(0, 2))
+    self.assertEqual('0.10s', str_tools.time_label(0.1, 2))
     self.assertEqual('-10s', str_tools.time_label(-10))
 
     self.assertRaises(TypeError, str_tools.time_label, None)



More information about the tor-commits mailing list