Skip to content

Instantly share code, notes, and snippets.

@acdha
Last active April 28, 2017 17:23
Show Gist options
  • Save acdha/e9d22a7c37961d043f58 to your computer and use it in GitHub Desktop.
Save acdha/e9d22a7c37961d043f58 to your computer and use it in GitHub Desktop.
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