Porting python 2 source code to python 3 and compatible with six module