optimize python code