Skip to content

Instantly share code, notes, and snippets.

@gbirke
Last active August 29, 2015 14:08
Show Gist options
  • Save gbirke/1e9d2a99b5c8a787945a to your computer and use it in GitHub Desktop.
Save gbirke/1e9d2a99b5c8a787945a to your computer and use it in GitHub Desktop.
Python Experimente iPython notebook
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:300f35dc168f47e63d10a2e1ad40c3f703b38832bf04a5bd557f508baaccd406"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Dynamischer Dispatch"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Der folgende Code zeigt, dass mit `getattr` nicht nur Werte sondern auch Methoden einer Klasse zur\u00fcckgeliefert und benutzt werden k\u00f6nnen."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"class MyClass(object):\n",
"\n",
" def test1(self):\n",
" print \"test 1\"\n",
"\n",
" def test2(self):\n",
" print \"test 2\"\n",
"\n",
" \n",
" def dispatch(self, testnumber):\n",
" method_name = \"test{:d}\".format(testnumber)\n",
" try:\n",
" getattr(self, method_name)()\n",
" except AttributeError:\n",
" print \"Methode '{:s}' nicht gefunden\".format(method_name)\n",
"\n",
"m = MyClass()\n",
"\n",
"t1 = getattr(m, \"test1\")\n",
"print t1\n",
"t1()\n",
"\n",
"m.dispatch(2)\n",
"m.dispatch(3)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"<bound method MyClass.test1 of <__main__.MyClass object at 0x3760b10>>\n",
"test 1\n",
"test 2\n",
"Methode 'test3' nicht gefunden\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Typen"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print type([])\n",
"print type({})\n",
"print type(\"\")\n",
"print type(0)\n",
"print type(0.0)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"<type 'list'>\n",
"<type 'dict'>\n",
"<type 'str'>\n",
"<type 'int'>\n",
"<type 'float'>\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"List, Tuple und Dictionary Comprehensions"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t = (\"a\", \"b\")\n",
"l1 = [i.upper() for i in t]\n",
"print l1\n",
"d = {\"a\": \"foo\", \"b\": \"bar\", \"z\":\"baz\"}\n",
"d2 = {k : v.upper() for (k,v) in d.iteritems() }\n",
"print d2"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"['A', 'B']\n",
"{'a': 'FOO', 'b': 'BAR', 'z': 'BAZ'}\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Flattening nested lists"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"l = [[1,2], [3,4], [5,6]]\n",
"print [e for i in l for e in i]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[1, 2, 3, 4, 5, 6]\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Multiple return values"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Nesting multiple return values in a set or list is optional\n",
"def ret1():\n",
" return (2,3)\n",
"\n",
"def ret2():\n",
" return 4,2\n",
"\n",
"def ret3():\n",
" return [47,11]\n",
"\n",
"v1,v2 = ret1()\n",
"v3,v4 = ret2()\n",
"v5,v6 = ret3()\n",
"\n",
"print [v1,v2,v3,v4,v5,v6]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[2, 3, 4, 2, 47, 11]\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Encoding Conversions"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"u1 = u\"\u00d6lk\u00fcmmel\"\n",
"s1 = u1.encode(\"utf-8\") # Convert to str\n",
"u2 = s1.decode(\"utf-8\") # Back to unicode\n",
"# print as list to see internal representation\n",
"print [u1,s1,u2]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[u'\\xd6lk\\xfcmmel', '\\xc3\\x96lk\\xc3\\xbcmmel', u'\\xd6lk\\xfcmmel']\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Test JSON encoding\n",
"import json\n",
"j1 = json.dumps(u1)\n",
"j2 = json.dumps(s1)\n",
"print json.dumps(j1)\n",
"print json.dumps(j2)\n",
"# print as list to see internal representation\n",
"print [json.loads(j1), json.loads(j2)]\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\"\\\"\\\\u00d6lk\\\\u00fcmmel\\\"\"\n",
"\"\\\"\\\\u00d6lk\\\\u00fcmmel\\\"\"\n",
"[u'\\xd6lk\\xfcmmel', u'\\xd6lk\\xfcmmel']\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Test regular expression splitting\n",
"import re\n",
"splitted = re.split(\"k\", u1)\n",
"print splitted"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[u'\\xd6l', u'\\xfcmmel']\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment