mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-02-18 14:40:51 +01:00
Raise ValueError in the case of bad input.
This commit is contained in:
parent
66b62a42cd
commit
6aa3e06402
@ -175,10 +175,11 @@ _soundextrans = string.maketrans(string.ascii_uppercase,
|
|||||||
_notUpper = string.ascii.translate(string.ascii, string.ascii_uppercase)
|
_notUpper = string.ascii.translate(string.ascii, string.ascii_uppercase)
|
||||||
def soundex(s, length=4):
|
def soundex(s, length=4):
|
||||||
"""Returns the soundex hash of a given string."""
|
"""Returns the soundex hash of a given string."""
|
||||||
assert s
|
|
||||||
s = s.upper() # Make everything uppercase.
|
s = s.upper() # Make everything uppercase.
|
||||||
firstChar = s[0] # Save the first character.
|
|
||||||
s = s.translate(string.ascii, _notUpper) # Delete non-letters.
|
s = s.translate(string.ascii, _notUpper) # Delete non-letters.
|
||||||
|
if not s:
|
||||||
|
raise ValueError, 'Invalid string for soundex: %s'
|
||||||
|
firstChar = s[0] # Save the first character.
|
||||||
s = s.translate(_soundextrans) # Convert to soundex numbers.
|
s = s.translate(_soundextrans) # Convert to soundex numbers.
|
||||||
s = s.lstrip(s[0]) # Remove all repeated first characters.
|
s = s.lstrip(s[0]) # Remove all repeated first characters.
|
||||||
L = [firstChar]
|
L = [firstChar]
|
||||||
|
@ -98,6 +98,9 @@ class UtilsTest(unittest.TestCase):
|
|||||||
soundex = utils.soundex(name)
|
soundex = utils.soundex(name)
|
||||||
self.assertEqual(soundex, key,
|
self.assertEqual(soundex, key,
|
||||||
'%s was %s, not %s' % (name, soundex, key))
|
'%s was %s, not %s' % (name, soundex, key))
|
||||||
|
self.assertRaises(ValueError, utils.soundex, '3')
|
||||||
|
self.assertRaises(ValueError, utils.soundex, "'")
|
||||||
|
|
||||||
|
|
||||||
def testDQRepr(self):
|
def testDQRepr(self):
|
||||||
L = ['foo', 'foo\'bar', 'foo"bar', '"', '\\', '', '\x00']
|
L = ['foo', 'foo\'bar', 'foo"bar', '"', '\\', '', '\x00']
|
||||||
|
Loading…
x
Reference in New Issue
Block a user