Rewrite query to python3