Use one write with heredocs and move to the with statement as per PEP343