Skip to content

Instantly share code, notes, and snippets.

@julzhk
Last active January 27, 2018 13:54
Show Gist options
  • Save julzhk/4cfd9d7eecf723c6d01d02be6ce2c599 to your computer and use it in GitHub Desktop.
Save julzhk/4cfd9d7eecf723c6d01d02be6ce2c599 to your computer and use it in GitHub Desktop.
Using a context manager to hide complexity & separate concerns
class ExcelContext():
def __enter__(self, filename='numbers.xls'):
#..set up an excel document: eg: self.fn = filename..
return self
def write(self,data):
#..do the excel cell writing & any formatting etc etc.
pass
def __exit__(self):
#..close the excel doc etc.
pass
# Then in the code the writing is easy & only about the writing of the data:
with ExcelContext() as excel:
excel.write('mydata')
excel.write('otherdata')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment