refactor python code