rewrite python code