Create a gist now

Instantly share code, notes, and snippets.

Gist demonstrating Unicode-awareness in Python's concept of what a digit is
#/usr/bin/env python
# encoding: utf-8
from __future__ import print_function, unicode_literals
import re
import sys
def safe_call(func, *args, **kwargs):
try:
return func(*args, **kwargs)
except ValueError as exc:
return u"Error calling %s: %s" % (func, str(exc).encode('unicode_escape'))
test_string = u'၁၄၂၈၅'
print("int() literal:")
print("\t", safe_call(int, test_string))
# Python 3 will always match but Python 2 requires re.UNICODE:
print("regex literal")
print("\t", safe_call(re.match, r'\d+', test_string))
print("regex literal, re.UNICODE")
print("\t", safe_call(re.match, r'\d+', test_string, re.UNICODE))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment