rewrite in python