Skip to content

Instantly share code, notes, and snippets.

@carlthome
Created January 21, 2017 18:17
Show Gist options
  • Save carlthome/31ba4093a6f7029053de02d70fc48ef1 to your computer and use it in GitHub Desktop.
Save carlthome/31ba4093a6f7029053de02d70fc48ef1 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"ExecuteTime": {
"start_time": "2017-01-21T19:16:59.074594",
"end_time": "2017-01-21T19:16:59.080718"
},
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "from IPython.display import display\nfrom contextlib import ContextDecorator\n\nclass mycontext(ContextDecorator):\n def __enter__(self):\n display('Starting')\n return self\n\n def __exit__(self, *exc):\n display('Finishing')\n return False",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2017-01-21T19:16:59.083941",
"end_time": "2017-01-21T19:16:59.107331"
},
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "with mycontext():\n display('The bit in the middle')",
"execution_count": 2,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": "'Starting'"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": "'The bit in the middle'"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": "'Finishing'"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2017-01-21T19:16:59.108500",
"end_time": "2017-01-21T19:16:59.116399"
},
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "@mycontext()\ndef function():\n display('The bit in the middle')\n \nfunction()",
"execution_count": 3,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": "'Starting'"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": "'The bit in the middle'"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": "'Finishing'"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2017-01-21T19:16:59.118702",
"end_time": "2017-01-21T19:16:59.129791"
},
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "mycontext()(lambda: display('The bit in the middle'))()",
"execution_count": 4,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": "'Starting'"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": "'The bit in the middle'"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": "'Finishing'"
},
"metadata": {}
}
]
}
],
"metadata": {
"kernelspec": {
"name": "conda-root-py",
"display_name": "Python [conda root]",
"language": "python"
},
"anaconda-cloud": {},
"language_info": {
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"version": "3.5.2",
"mimetype": "text/x-python",
"file_extension": ".py",
"name": "python",
"codemirror_mode": {
"name": "ipython",
"version": 3
}
},
"gist": {
"id": "",
"data": {
"description": "",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment