Commits

Jason R. Coombs  committed 7a066ec

Adding a couple of functions to jaraco.util.string

  • Participants
  • Parent commits 6d01d1a
  • Tags 3.8

Comments (0)

Files changed (2)

   both OptionParser and ArgumentParser and is deprecated.
 * Added `jaraco.util.exceptions` with a function for determining if a
   callable throws a specific exception.
+* Added `is_decodable` and `is_binary` to `jaraco.util.string`.
 
 3.7
 ~~~

File jaraco/util/string.py

+from __future__ import absolute_import
 import re
 import inspect
-from itertools import starmap
-from jaraco.util.functools import compose
+import itertools
+import functools
+from .functools import compose
+from .exceptions import throws_exception
+
 
 def substitution(old, new):
 	"""
 	>>> multi_substitution(('foo', 'bar'), ('bar', 'baz'))('foo')
 	'baz'
 	"""
-	substitutions = starmap(substitution, substitutions)
+	substitutions = itertools.starmap(substitution, substitutions)
 	# compose function applies last function first, so reverse the
 	#  substitutions to get the expected order.
 	substitutions = reversed(tuple(substitutions))
 	'    3'
 	"""
 	return string.format(**inspect.currentframe().f_back.f_locals)
+
+def is_decodable(value):
+	"""
+	Return True if the supplied value is decodable (using the 'unicode'
+	constructor and thus the default encoding).
+	"""
+	return not throws_exception(functools.partial(unicode, value),
+		UnicodeDecodeError)
+	
+def is_binary(value):
+	"""
+	Return True if the value appears to be binary (that is, it's a byte
+	string and isn't decodable).
+	"""
+	return isinstance(value, bytes) and not is_decodable(value)