Skip to content

Instantly share code, notes, and snippets.

@DomNomNom
Last active December 25, 2015 08:19
Show Gist options
  • Save DomNomNom/6946393 to your computer and use it in GitHub Desktop.
Save DomNomNom/6946393 to your computer and use it in GitHub Desktop.
Context Manager
# 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