Skip to content

Commit

Permalink
Add string utilities to convert string to unicode and vice versa.
Browse files Browse the repository at this point in the history
  • Loading branch information
shyuep committed Sep 15, 2014
1 parent b5b065e commit 9989008
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
30 changes: 30 additions & 0 deletions monty/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
__date__ = "2/28/14"


import sys


def remove_non_ascii(s):
"""
Remove non-ascii characters in a file. Needed when support for non-ASCII
Expand All @@ -26,6 +29,33 @@ def remove_non_ascii(s):
return "".join(i for i in s if ord(i) < 128)


def unicode2str(s):
"""
Forces a unicode to a string in Python 2, but transparently handles
Python 3.
Args:
s (str/unicode): Input string / unicode.
Returns:
str in Python 2. Unchanged otherwise.
"""
return s.encode('utf-8') if sys.version_info.major < 3 else s


def str2unicode(s):
"""
Converts strings to unicode in python 2. Ignores Python 3 strings.
Args:
s (str/unicode): Input string / unicode.
Returns:
Unicode.
"""
return unicode(s) if sys.version_info.major < 3 else s


def marquee(text="", width=78, mark='*'):
"""
Return the input string centered in a 'marquee'.
Expand Down
14 changes: 13 additions & 1 deletion tests/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

import unittest
import random
import sys

from monty.string import remove_non_ascii
from monty.string import remove_non_ascii, str2unicode, unicode2str


class FuncTest(unittest.TestCase):
Expand All @@ -25,6 +26,17 @@ def test_remove_non_ascii(self):
clean = remove_non_ascii(s)
self.assertEqual(len(clean), 10)

def test_str2unicode(self):
if sys.version_info.major < 3:
self.assertEqual(type(str2unicode("a")), unicode)
else:
self.assertEqual(type(str2unicode("a")), str)

def test_unicode2str(self):
if sys.version_info.major < 3:
self.assertEqual(type(unicode2str("a")), str)
else:
self.assertEqual(type(unicode2str("a")), str)

if __name__ == '__main__':
unittest.main()

0 comments on commit 9989008

Please sign in to comment.