Last active
December 25, 2015 08:19
-
-
Save DomNomNom/6946393 to your computer and use it in GitHub Desktop.
Context Manager
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
# custom context manager | |
import contextlib, sys | |
p = sys.stdout.write # shorthand for print (without spaces) | |
@contextlib.contextmanager | |
def printcolour(colour): | |
p('\033[{0}m'.format(colour)) | |
yield | |
p('\033[m') | |
p('Hello would you like some ') | |
with printcolour(93): | |
p('differently coloured ') | |
p('text ') | |
p('or just normal text?\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment