Last active
January 27, 2018 13:54
-
-
Save julzhk/4cfd9d7eecf723c6d01d02be6ce2c599 to your computer and use it in GitHub Desktop.
Using a context manager to hide complexity & separate concerns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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