Right. Not constructing the map every time is an obvious enhancement.
My point was simply that using a regex for this particular simple task
doesn't make much sense.
On Fri, Jan 15, 2010 at 9:15 AM, Mike Axiak wrote:
> If you really want to be fast, you can do the following, a la urllib.quote::
>
Or you could use the builtin maketrans/translate pair:
import string
_phone2number_transtab = string.maketrans(string.ascii_letters,
"222333444555666888"*2)
def phone2number(szinput):
return szinput.translate(_phone2number_transtab)
2010/1/15 Mike Axiak :
> If you really want to be f
I forgot to handle upper case. Note that the following is (~5%)
faster than calling .upper() on the input::
_phone_chars = {
'a': '2',
'b': '2',
'c': '2',
'd': '3',
'e': '3',
'f': '3',
'g': '4',
'h': '4',
'i': '4',
If you really want to be fast, you can do the following, a la urllib.quote::
_phone_chars = {'a': '2', 'b': '2', 'c': '2', 'd': '3', 'e': '3',
'f': '3', 'g': '4', 'h': '4', 'i': '4', 'j': '5', 'k':
'5', 'l': '5',
'm': '6', 'n': '6', 'o': '6', 'p': '7', 'q': '7', 'r':
Why use regular expressions at all for this? A timeit benchmark shows
a greater than 4x speedup with a rangetest in a loop over the string:
def phone2number(str):
chars = {'a': '2', 'b': '2', 'c': '2', 'd': '3', 'e': '3',
'f': '3', 'g': '4', 'h': '4', 'i': '4', 'j': '5', 'k': '5', 'l'
I've opened a ticket and submitted a patch that fixes this strange
oversight: http://code.djangoproject.com/ticket/12613
Thanks!
- Gabriel
On Jan 14, 5:05 am, Harro wrote:
> hmm that's indeed weird. The regex excludes those as well
> specifically.
> The Q and Z should be added or a comment s
hmm that's indeed weird. The regex excludes those as well
specifically.
The Q and Z should be added or a comment should be added to the code
explaining the reason for leaving them out.
On Jan 14, 11:23 am, Gabriel Hurley wrote:
> 1. Is there a reason Django's phone2numeric method doesn't work for
2010/1/14 Gabriel Hurley :
> 2. I was also wondering if there's a reason that the dictionary of
> numbers/letters used in that function is in such a seemingly random
> order... is there some brilliant logic behind it?
Yes, there is. Someone probably copy&pasted it from python's output,
so they're i
1. Is there a reason Django's phone2numeric method doesn't work for
the letters Q or Z? I realize that those two letters are the ones that
share four letters to a number instead of the standard three, but
that's no reason to leave them out. Most modern phones include the
full alphabet on their keys