Skip to content

Commit

Permalink
Updating string successor to work in Python3.
Browse files Browse the repository at this point in the history
Made especially difficult since a single char slice
in Py3 bytes returns an integer.
  • Loading branch information
dhermes committed Sep 19, 2015
1 parent b39bd6a commit 3a7070d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[report]
omit =
*/_generated/*.py
exclude_lines =
# Re-enable the standard pragma
pragma: NO COVER
34 changes: 29 additions & 5 deletions gcloud_bigtable/happybase/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,26 @@ def _partial_row_to_dict(partial_row_data, include_timestamp=False):
return result


def _next_char(str_val, index):
"""Gets the next character based on a position in a string.
:type str_val: str
:param str_val: A string containing the character to update.
:type index: int
:param index: An integer index in ``str_val``.
:rtype: str
:returns: The next character after the character at ``index``
in ``str_val``.
"""
if six.PY3: # pragma: NO COVER
ord_val = str_val[index]
else:
ord_val = ord(str_val[index])
return _to_bytes(chr(ord_val + 1), encoding='latin-1')


def _string_successor(str_val):
"""Increment and truncate a byte string.
Expand All @@ -342,20 +362,24 @@ def _string_successor(str_val):
:rtype: str
:returns: The next string in lexical order after ``str_val``.
"""
str_val = _to_bytes(str_val, encoding='latin-1')
if str_val == b'':
return str_val

n = len(str_val)
index = n - 1
index = len(str_val) - 1
while index >= 0:
if str_val[index] != b'\xff':
break
if six.PY3: # pragma: NO COVER
if str_val[index] != 0xff:
break
else:
if str_val[index] != b'\xff':
break
index -= 1

if index == -1:
return b''

return str_val[:index] + chr(ord(str_val[index]) + 1)
return str_val[:index] + _next_char(str_val, index)


class Table(object):
Expand Down
15 changes: 14 additions & 1 deletion gcloud_bigtable/happybase/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,20 @@ def _callFUT(self, *args, **kwargs):
return _string_successor(*args, **kwargs)

def test_with_alphanumeric(self):
self.assertEqual(self._callFUT('boa'), 'bob')
self.assertEqual(self._callFUT(b'boa'), b'bob')
self.assertEqual(self._callFUT(b'abc1'), b'abc2')

def test_with_last_byte(self):
self.assertEqual(self._callFUT(b'boa\xff'), b'bob')

def test_with_empty_string(self):
self.assertEqual(self._callFUT(b''), b'')

def test_with_all_last_bytes(self):
self.assertEqual(self._callFUT(b'\xff\xff\xff'), b'')

def test_with_unicode_input(self):
self.assertEqual(self._callFUT(u'boa'), b'bob')


class TestTable(unittest2.TestCase):
Expand Down

0 comments on commit 3a7070d

Please sign in to comment.