{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Datenstrukturen" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## List" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Leere Liste" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "l = []" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[42]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.append(42)\n", "l" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[42, 'hallo', 'schatzi']" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2 = ['hallo', 'schatzi']\n", "l.extend(l2)\n", "l" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hallo'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Suchen: lineare Zeitkomplexität" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[42, 'hallo', 'schatzi']" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(l)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1 Vergleich:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "42 in l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3 Vergleiche" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'schatzi' in l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Iteration" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[42, 'hallo', 'schatzi']" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(l)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42\n", "hallo\n", "schatzi\n" ] } ], "source": [ "i = 0\n", "while i < len(l):\n", " print(l[i])\n", " i += 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Das geht besser: ``for`` (JavaScript: ``foreach``)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42\n", "hallo\n", "schatzi\n" ] } ], "source": [ "for element in l:\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dictionary" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\"Assoziatives Array\", oder kurz: key/value mapping" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "color_translations = {\n", " 'rot': 'red',\n", " 'blau': 'blue',\n", " 'gelb': 'yellow'\n", "}" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(color_translations)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Suche: Konstante Zeitkomplexität (als Hashtable)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'rot' in color_translations" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'red'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "color_translations['rot']" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "color_translations['schwarz'] = 'black'" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'rot': 'red', 'blau': 'blue', 'gelb': 'yellow', 'schwarz': 'black'}" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "color_translations" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'blau': 'blue', 'gelb': 'yellow', 'schwarz': 'black'}" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "del color_translations['rot']\n", "color_translations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Iteration" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'blau': 'blue', 'gelb': 'yellow', 'schwarz': 'black'}" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "color_translations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Iteration über Keys ..." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "blau\n", "gelb\n", "schwarz\n" ] } ], "source": [ "for element in color_translations:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "blau\n", "gelb\n", "schwarz\n" ] } ], "source": [ "for element in color_translations.keys():\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Iteration über Values ..." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "blue\n", "yellow\n", "black\n" ] } ], "source": [ "for element in color_translations.values():\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Iteration über beides ..." ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "----\n", "der ganze entry: ('blau', 'blue')\n", "key auf deutsch: blau\n", "value auf englisch: blue\n", "----\n", "der ganze entry: ('gelb', 'yellow')\n", "key auf deutsch: gelb\n", "value auf englisch: yellow\n", "----\n", "der ganze entry: ('schwarz', 'black')\n", "key auf deutsch: schwarz\n", "value auf englisch: black\n" ] } ], "source": [ "for entry in color_translations.items():\n", " print('----')\n", " print('der ganze entry:', entry)\n", " print('key auf deutsch:', entry[0])\n", " print('value auf englisch:', entry[1])\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Same, mit Tuple Unpacking" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "----\n", "key auf deutsch: blau\n", "value auf englisch: blue\n", "----\n", "key auf deutsch: gelb\n", "value auf englisch: yellow\n", "----\n", "key auf deutsch: schwarz\n", "value auf englisch: black\n" ] } ], "source": [ "for deutsch, englisch in color_translations.items():\n", " print('----')\n", " print('key auf deutsch:', deutsch)\n", " print('value auf englisch:', englisch)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Set" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sack von Elementen (auch als Hashtable implementiert-> schnelle Suche)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "set" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_colors = {'rot', 'blau', 'schwarz', 'gelb'}\n", "type(my_colors)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "her_colors = {'pink', 'blau', 'schwarz', 'grün', 'beige'}" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'rot' in my_colors" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Union (Vereiningungsmenge)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'beige', 'blau', 'gelb', 'grün', 'pink', 'rot', 'schwarz'}" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_colors | her_colors" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Schnittmenge" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'blau', 'schwarz'}" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_colors & her_colors" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Symmetrische Differenzmenge" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'beige', 'gelb', 'grün', 'pink', 'rot'}" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_colors ^ her_colors" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# User Datenbank: Showcase Full Stack Development" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5: Response kommt von der DB" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Der Request an die DB sei gewesen: ``select * from userdb;``" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "# Man denke sich, das waere PHP:\n", "response = [\n", " ['1037190666', 'Jörg', 'Faschingbauer'],\n", " ['6666010185', 'Oliver', 'Schwarzbauer'],\n", "]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6: Aufgabe: Formulieren eines JSON Strings, damit man diese Daten über die HTTP Verbindung zum Client/Browser schicken kann. " ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "# Man denke sich wieder, dass das in PHP waere\n", "import json\n", "json_string_fuer_den_browser = json.dumps(response)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "# anders: JSON als dictionary\n", "response_dict = {}\n", "for svnr, vorname, nachname in response:\n", " response_dict[svnr] = { 'vorname': vorname, 'nachname': nachname, 'svnr': svnr }" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'1037190666': {'vorname': 'Jörg',\n", " 'nachname': 'Faschingbauer',\n", " 'svnr': '1037190666'},\n", " '6666010185': {'vorname': 'Oliver',\n", " 'nachname': 'Schwarzbauer',\n", " 'svnr': '6666010185'}}" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response_dict" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'1037190666': {'nachname': 'Faschingbauer',\n", " 'svnr': '1037190666',\n", " 'vorname': 'Jörg'},\n", " '6666010185': {'nachname': 'Schwarzbauer',\n", " 'svnr': '6666010185',\n", " 'vorname': 'Oliver'}}\n" ] } ], "source": [ "import pprint\n", "pprint.pprint(response_dict)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Formatieren wir endlich in JSON ..." ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "import json\n", "transfer_string_in_php_am_server = json.dumps(response_dict)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'{\"1037190666\": {\"vorname\": \"J\\\\u00f6rg\", \"nachname\": \"Faschingbauer\", \"svnr\": \"1037190666\"}, \"6666010185\": {\"vorname\": \"Oliver\", \"nachname\": \"Schwarzbauer\", \"svnr\": \"6666010185\"}}'" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "transfer_string_in_php_am_server" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 7. Jetzt schickman weg: vom Server (PHP) zum Browser (JS)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "transfer_string_in_js_am_browser = transfer_string_in_php_am_server" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8. in JS JSON string empfangen" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'{\"1037190666\": {\"vorname\": \"J\\\\u00f6rg\", \"nachname\": \"Faschingbauer\", \"svnr\": \"1037190666\"}, \"6666010185\": {\"vorname\": \"Oliver\", \"nachname\": \"Schwarzbauer\", \"svnr\": \"6666010185\"}}'" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "transfer_string_in_js_am_browser" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 9. Nun muessen wir den JSON-**String** in JS verwandlen" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "# man denke sich hier, das waere JS\n", "response_dict_am_browser = json.loads(transfer_string_in_js_am_browser)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'1037190666': {'nachname': 'Faschingbauer',\n", " 'svnr': '1037190666',\n", " 'vorname': 'Jörg'},\n", " '6666010185': {'nachname': 'Schwarzbauer',\n", " 'svnr': '6666010185',\n", " 'vorname': 'Oliver'}}\n" ] } ], "source": [ "pprint.pprint(response_dict_am_browser)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 10. Nun gilt es, die JS Datenstruktur in eine HTML zu verwandeln, und diese Table schlussendlich im DOM an geeignete Stelle zu platzieren. " ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "meine_table = '
\\n'\n", "meine_table += ' \\n'\n", "meine_table += ' \\n'\n", "meine_table += ' \\n'\n", "meine_table += ' \\n'\n", "meine_table += ' \\n'\n", "\n", "for user in response_dict_am_browser.values():\n", " meine_table += ' \\n'\n", " meine_table += ' \\n'\n", " meine_table += ' \\n'\n", " meine_table += ' \\n'\n", " meine_table += ' \\n'\n", " \n", "meine_table += '
SozialversicherungsnummerVornameNachname
' + user['svnr'] + '' + user['vorname'] + '' + user['nachname'] + '
\\n'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Und dieses Element, die ````, soll nun an eine geeignete Stelle im DOM platziert werden ..." ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
SozialversicherungsnummerVornameNachname
1037190666JörgFaschingbauer
6666010185OliverSchwarzbauer
\n", "\n" ] } ], "source": [ "print(meine_table)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.4" } }, "nbformat": 4, "nbformat_minor": 4 }