Python Module: nift
bearophileHUGS at lycos.com
bearophileHUGS at lycos.com
Sun Feb 8 16:24:42 EST 2009
Paul McGuire:
> LEET_LETTERS = dict( zip("eEaAiItTsSoObB", "33441177550088") )
> def leet(s):
> return ''.join( LEET_LETTERS.get(c,c) for c in s )
This may be better:
from string import maketrans
def leet(txt):
leet_chars = maketrans("eEaAiItTsSoObB", "33441177550088")
return txt.translate(leet_chars)
Moving leet_chars outside the function increases speed a bit.
Or for more expert Python programmers, you can add an attribute the
object function, I don't know if you like this:
from string import maketrans
def leet(txt):
return txt.translate(leet.chars)
leet.chars = maketrans("eEaAiItTsSoObB", "33441177550088")
It's executes maketrans once only and keeps the namespace tidy. So
it's a bit like a static variable in other languages.
Bye,
bearophile
More information about the Python-list
mailing list