I have a pretty bad habit of porting codes from one language to another. PHP and Python both are my favorites. But sometimes I feel that I enjoy coding more in Python than PHP. It’s just because I am a lazy guy who hates using text editors to complete a script entirely before executing the program. I like interactive shells. Now that I have set up interactive shell for PHP as well, I write little throw away programs in both PHP and Python. But it still happens that the Python interactive interpreter is loads better than the PHP one. I have to type “echo” every time I want some input from the PHP shell, where as I have to type the name of an element (variable, object, list, tuple or whatever) and python displays it on the shell. So it becomes easier to experiment and debug.
Back to the topic, I was thinking of porting the alphabet class I wrote in PHP to Python. And finally I got some time today morning and finished it.
How to use ?
|
#!/usr/bin/env python from alphabet import * a = alphabet() print a.getChar(23) # outputs 'w' print a.getNum('b') # outputs 2 |
Source Code
|
#!/usr/bin/env python class alphabet: def __init__(self): self.l1 = [chr(x) for x in range(ord('a'),ord('z')+1)] self.l2 = [x for x in range(1,27)] def getNum(self,char): mydict = dict(zip(self.l1,self.l2)) return mydict[char] def getChar(self,num): mydict = dict(zip(self.l2,self.l1)) return mydict[num] |