{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Advanced (2023-09-25 - 2023-09-27)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## JSON" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "user = {\n", " 'id': 1,\n", " 'firstname': 'Joerg',\n", " 'lastname': 'Faschingbauer',\n", "}" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'id': 1, 'firstname': 'Joerg', 'lastname': 'Faschingbauer'}" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "user" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(user)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import json" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "data_to_send = json.dumps(user)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(data_to_send)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"id\": 1, \"firstname\": \"Joerg\", \"lastname\": \"Faschingbauer\"}\n" ] } ], "source": [ "print(data_to_send)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'\\'{\"id\": 1, \"firstname\": \"Joerg\", \"lastname\": \"Faschingbauer\"}\\''" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "repr(data_to_send)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now ``data_to_send`` is sent over a tcp connection, and received into a variable ``data_received``" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "data_received = data_to_send" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(data_received)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "user_received = json.loads(data_received)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'id': 1, 'firstname': 'Joerg', 'lastname': 'Faschingbauer'}" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "user_received" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Equality vs. Identity" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "d1 = {\n", " 'id': 1,\n", " 'firstname': 'Joerg',\n", "}" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "d2 = {\n", " 'id': 1,\n", " 'firstname': 'Joerg',\n", "}" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d1 == d2" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d1 is d2" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140048588115008" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(d1)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140048588341376" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(d2)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(d1) == id(d2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Functions Are Objects" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "def add(a, b):\n", " return a + b" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "add(1, 2)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "summe = add" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "summe(1,2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.11" } }, "nbformat": 4, "nbformat_minor": 4 }