Added dqrepr.

This commit is contained in:
Jeremy Fincher 2003-04-12 12:50:20 +00:00
parent 7f90fdc712
commit 6287d61d9e
2 changed files with 16 additions and 0 deletions

View File

@ -194,4 +194,8 @@ def soundex(s, length=4):
s = ''.join(L) s = ''.join(L)
return length and s[:length] or s.rstrip('0') return length and s[:length] or s.rstrip('0')
def dqrepr(s):
"""Returns a repr() of s guaranteed to be in double quotes."""
return '"' + repr("'\x00" + s)[6:]
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:

View File

@ -97,3 +97,15 @@ 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))
def testDQRepr(self):
L = [('foo', '"foo"'),
('foo\'bar', '"foo\'bar"'),
('foo"bar', '"foo\\"bar"'),
('"', '"\\""'),
('', '""'),
('\x00', '"\\x00"')]
for (s, r) in L:
self.assertEqual(r, utils.dqrepr(s))
self.assertEqual(s, eval(utils.dqrepr(s)))