Digits int() doesn’t dig
Some time ago I wrote in More Than Just ASCII Digits about all the unicode characters Python’s int() function converts to numbers.
Recently I’ve discovered a discrepancy between those characters and the isdigit() method — there are characters where isdigit() returns True but int() does not convert them.
Chasing a dot on a HTML5 canvas
A forum question about how to detect a collision of two balls for a school project (in Python) lead me to write my first HTML5 <canvas> element code.
Move the mouse over the square below and try to catch the blue dot with the red one.
More Than Just ASCII Digits
Python’s int() converts more than just ASCII digits. It also converts decimal digits from other scripts.
>>> int('42')
42
>>> int(u'୨୩')
23
It’s also possible to mix differnt scripts. int() only cares about the decimal value of each digit character.
>>> int(u'௧౩౩೭')
1337
The following snippet creates CSV data for all digits int() can convert:
#!/usr/bin/env python
# coding: utf8
from __future__ import absolute_import, division, print_function
import sys
import unicodedata
def main():
for i in xrange(sys.maxunicode + 1):
try:
character = unichr(i)
value = int(character)
except ValueError:
pass
else:
print(
u'{0},{1},{2:x},{3}'.format(
character, value, i, unicodedata.name(character).lower()
).encode('utf8')
)
if __name__ == '__main__':
main()
And here’s the table generated by that script: