python code optimization