Forked from schuster-rainer/pythonnet-formatter.ipynb
Last active
October 27, 2016 08:00
-
-
Save den-run-ai/108d9221843605d306c5be5a07ab4c2a to your computer and use it in GitHub Desktop.
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
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:fd0c2add09b883972321a5d7599c3a0f9c46fc9d427d6453724150f7fc29bb1e" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# python.net\n", | |
"\n", | |
"http://pythonnet.github.io/readme.html\n", | |
"\n", | |
"## Installation\n", | |
"\n", | |
"In order to use https://github.com/pythonnet/pythonnet in my virtualenvs and to run this notebook I had to run the following commands. I intentionally cloned and installed locally in dev mode.\n", | |
"* sudo apt-get install mono-complete\n", | |
"* mkvirtualenv bootstrap-npython\n", | |
"* git clone https://github.com/pythonnet/pythonnet.git\n", | |
"* cd pythonnet\n", | |
"* sudo apt-get install libglib2.0-cil \n", | |
" * setup.py of pythonnet runs pkg-config on glib-2, which coulnd't be found, therefore I install gtksharp\n", | |
"* pip install -e . \n", | |
"* pip install ipython[notebook]\n", | |
"\n", | |
"I then bootstrapped a new virtualenv from this one with npython as the python interpreter. \n", | |
"* which npython (on my machine it is $WORKON_HOME/bootstrap-python/bin/npython.exe)\n", | |
"* mkvirtualenv npython -p <path to the npython.exe>\n", | |
"\n", | |
"Finally you are ready to launch ipython and all other modules, which are creating console scripts with npython as python executable to be able to _import clr_" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import clr\n", | |
"import System\n", | |
"from System import String, Int32, Environment\n", | |
"from System.Collections.Generic import Dictionary\n", | |
"from pprint import pprint" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Custom display formatter for .NET types\n", | |
"\n", | |
"IPython can register custom display formaters (like Debug Visualizers in Visual Studio) for types and therefore uses IPython.lib.pretty\n", | |
"\n", | |
"* https://github.com/ipython/ipython/blob/master/IPython/lib/pretty.py\n", | |
"* http://ipython.org/ipython-doc/dev/api/generated/IPython.lib.pretty.html\n", | |
"* https://github.com/ipython/ipython/blob/master/IPython/core/formatters.py\n", | |
"* http://ipython.org/ipython-doc/dev/api/generated/IPython.core.formatters.html\n", | |
"\n", | |
"I had issues to get a matching condition on n.GetType().IsAssignableFrom(System.Collections.IDictionary) and other types. I guess it is the boxing or me not knowing how to pass the right type in. Therefore I use a duck typing approach to probe on the items for item.Key and item.Value. As all .NET types derive from System.Object I decided to register a single formatter for type System.Object\n", | |
"\n", | |
"**TODO:** Package formatter as an extension" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"formatter = get_ipython().display_formatter.formatters['text/plain']\n", | |
"\n", | |
"def format_dotnet(n, p, cycle):\n", | |
" my_type = n.GetType()\n", | |
" try:\n", | |
" dict_like = {i.Key: i.Value for i in n}\n", | |
" except:\n", | |
" dict_like = None\n", | |
" \n", | |
" def format_iter(iterable, format_item):\n", | |
" if cycle:\n", | |
" p.text('{}(...)'.format(my_type))\n", | |
" else:\n", | |
" p.begin_group(4, '{}(['.format(my_type))\n", | |
" p.breakable()\n", | |
" for idx, item in enumerate(iterable):\n", | |
" if idx:\n", | |
" p.text(',')\n", | |
" p.breakable()\n", | |
" format_item(item, p)\n", | |
" p.end_group(4, '])')\n", | |
" \n", | |
" if dict_like:\n", | |
" def format_dict_item(item, p):\n", | |
" p.pretty(\"{}: \".format(item[0]))\n", | |
" p.pretty(item[1])\n", | |
" format_iter(dict_like.iteritems(), format_dict_item)\n", | |
" elif my_type.IsArray:\n", | |
" format_iter(n, lambda item: item)\n", | |
" else:\n", | |
" p.text(n.ToString())\n", | |
"formatter.for_type(System.Object, format_dotnet)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"dict1 = Dictionary[String, String]()\n", | |
"dict2 = Dictionary[String, Int32]()\n", | |
"dict1[\"test\"] = \"hallo\"\n", | |
"dict1[\"test2\"] = \"welt\"\n", | |
"dict2[\"test\"] = 1\n", | |
"dict2[\"test2\"] = 2" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"dict1" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 4, | |
"text": [ | |
"System.Collections.Generic.Dictionary`2[System.String,System.String]([\n", | |
" 'test: 'u'hallo',\n", | |
" 'test2: 'u'welt'])" | |
] | |
} | |
], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"dict2" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 5, | |
"text": [ | |
"System.Collections.Generic.Dictionary`2[System.String,System.Int32]([\n", | |
" 'test: '1,\n", | |
" 'test2: '2])" | |
] | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": true, | |
"input": [ | |
"if Environment.OSVersion.VersionString.lower().startswith(\"unix\"):\n", | |
" release = !cat /etc/*release\n", | |
" release.append(('KERNEL={}'.format(Environment.OSVersion.Version)))\n", | |
" pprint(release)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"['DISTRIB_ID=Ubuntu',\n", | |
" 'DISTRIB_RELEASE=14.04',\n", | |
" 'DISTRIB_CODENAME=trusty',\n", | |
" 'DISTRIB_DESCRIPTION=\"Ubuntu 14.04 LTS\"',\n", | |
" 'NAME=\"Ubuntu\"',\n", | |
" 'VERSION=\"14.04, Trusty Tahr\"',\n", | |
" 'ID=ubuntu',\n", | |
" 'ID_LIKE=debian',\n", | |
" 'PRETTY_NAME=\"Ubuntu 14.04 LTS\"',\n", | |
" 'VERSION_ID=\"14.04\"',\n", | |
" 'HOME_URL=\"http://www.ubuntu.com/\"',\n", | |
" 'SUPPORT_URL=\"http://help.ubuntu.com/\"',\n", | |
" 'BUG_REPORT_URL=\"http://bugs.launchpad.net/ubuntu/\"',\n", | |
" 'KERNEL=3.13.0.29']\n" | |
] | |
} | |
], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"get_ipython().display_formatter.active_types" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 7, | |
"text": [ | |
"[u'text/plain',\n", | |
" u'image/jpeg',\n", | |
" u'text/html',\n", | |
" u'image/svg+xml',\n", | |
" u'image/png',\n", | |
" u'application/javascript',\n", | |
" u'text/markdown',\n", | |
" u'text/latex',\n", | |
" u'application/json',\n", | |
" u'application/pdf']" | |
] | |
} | |
], | |
"prompt_number": 7 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"Environment.OSVersion" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 8, | |
"text": [ | |
"Unix 3.13.0.29" | |
] | |
} | |
], | |
"prompt_number": 8 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment