[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Add an IntString class



commit 3f8493851bd538bc4aa3d868b880271697ecf516
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Wed Jan 17 11:37:40 2018 -0800

    Add an IntString class
    
    Most stem.client enumerations are backed by integer values. This class will let
    us avoid storing two variables for all of those fields.
---
 docs/change_log.rst         |  4 ++++
 stem/util/str_tools.py      | 26 ++++++++++++++++++++++++++
 test/unit/doctest.py        |  1 +
 test/unit/util/str_tools.py |  6 ++++++
 4 files changed, 37 insertions(+)

diff --git a/docs/change_log.rst b/docs/change_log.rst
index fcfc0115..ebdf8e3e 100644
--- a/docs/change_log.rst
+++ b/docs/change_log.rst
@@ -56,6 +56,10 @@ The following are only available within Stem's `git repository
   * `Fallback directory v2 support <https://lists.torproject.org/pipermail/tor-dev/2017-December/012721.html>`_, which adds *nickname* and *extrainfo*
   * Reduced maximum descriptors fetched by the remote module to match tor's new limit (:trac:`24743`)
 
+ * **Utilities**
+
+  * Added :class:`~stem.util.str_tools.IntString`
+
  * **Website**
 
   * Added `terminal styling <tutorials/east_of_the_sun.html#terminal-styling>`_ to our utilities tutorial
diff --git a/stem/util/str_tools.py b/stem/util/str_tools.py
index bc0446fa..108d21ea 100644
--- a/stem/util/str_tools.py
+++ b/stem/util/str_tools.py
@@ -12,6 +12,8 @@ Toolkit for various string activity.
 
 ::
 
+  IntString - string that can also be used as an integer
+
   crop - shortens string to a given length
 
   size_label - human readable label for a number of bytes
@@ -185,6 +187,30 @@ def _split_by_length(msg, size):
 Ending = stem.util.enum.Enum('ELLIPSE', 'HYPHEN')
 
 
+class IntString(str):
+  """
+  String that can be used as an integer as well...
+
+  ::
+
+    >>> s = IntString('me', 5)
+    >>> print('hi %s, the number is %i' % (s, s))
+    hi me, the number is 5
+
+  .. versionadded:: 1.7.0
+  """
+
+  def __new__(self, str_value, int_value):
+    return str.__new__(self, str_value)
+
+  def __init__(self, str_value, int_value):
+    self._str_value = str_value
+    self._int_value = int_value
+
+  def __int__(self):
+    return self._int_value
+
+
 def crop(msg, size, min_word_length = 4, min_crop = 0, ending = Ending.ELLIPSE, get_remainder = False):
   """
   Shortens a string to a given length.
diff --git a/test/unit/doctest.py b/test/unit/doctest.py
index 7e40e873..9dda1422 100644
--- a/test/unit/doctest.py
+++ b/test/unit/doctest.py
@@ -67,6 +67,7 @@ class TestDocumentation(unittest.TestCase):
         args['globs'] = {
           '_to_camel_case': stem.util.str_tools._to_camel_case,
           '_split_by_length': stem.util.str_tools._split_by_length,
+          'IntString': stem.util.str_tools.IntString,
           'crop': stem.util.str_tools.crop,
           'size_label': stem.util.str_tools.size_label,
           'time_label': stem.util.str_tools.time_label,
diff --git a/test/unit/util/str_tools.py b/test/unit/util/str_tools.py
index ee270717..7349bd56 100644
--- a/test/unit/util/str_tools.py
+++ b/test/unit/util/str_tools.py
@@ -50,6 +50,12 @@ class TestStrTools(unittest.TestCase):
     self.assertEqual(['hello'], str_tools._split_by_length('hello', 5))
     self.assertEqual(['hello'], str_tools._split_by_length('hello', 6))
 
+  def test_int_string(self):
+    s = str_tools.IntString('me', 5)
+    self.assertEqual('me', s)
+    self.assertEqual(5, int(s))
+    self.assertEqual('hi me, the number is 5', 'hi %s, the number is %i' % (s, s))
+
   def test_crop(self):
     # test the pydoc examples
     self.assertEqual('This is a looo...', str_tools.crop('This is a looooong message', 17))



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits