mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-27 05:09:23 +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)
|
||||
def soundex(s, length=4):
|
||||
"""Returns the soundex hash of a given string."""
|
||||
assert s
|
||||
s = s.upper() # Make everything uppercase.
|
||||
firstChar = s[0] # Save the first character.
|
||||
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.lstrip(s[0]) # Remove all repeated first characters.
|
||||
L = [firstChar]
|
||||
|
@ -98,6 +98,9 @@ class UtilsTest(unittest.TestCase):
|
||||
soundex = utils.soundex(name)
|
||||
self.assertEqual(soundex, key,
|
||||
'%s was %s, not %s' % (name, soundex, key))
|
||||
self.assertRaises(ValueError, utils.soundex, '3')
|
||||
self.assertRaises(ValueError, utils.soundex, "'")
|
||||
|
||||
|
||||
def testDQRepr(self):
|
||||
L = ['foo', 'foo\'bar', 'foo"bar', '"', '\\', '', '\x00']
|
||||
|
Loading…
Reference in New Issue
Block a user