rewrite the python code