diff --git a/notebooks/seminar07.ipynb b/notebooks/seminar07.ipynb index 174243dfe565990aaf6fb160010561e2e7299171..550bd18c56ed06900d84e9afb402a910c2d1a2ed 100644 --- a/notebooks/seminar07.ipynb +++ b/notebooks/seminar07.ipynb @@ -29,32 +29,80 @@ "source": [ "## Exkurs: Was mir an Python gefällt\n", "\n", - "Das [Modul os](https://docs.python.org/3/library/os.html) stellt Funktionen bereit, um Funktionalitäten des Betriebssystems zu nutzen. Beispielsweise können wir damit Verzeichnis-Inhalte auflisten, durch Verzeichnisse navigieren, Informationen zu Dateien bekommen und Dateieigenschaften verändern. Das folgende Programm gibt eine Liste aller Jupyter-Notebooks im aktuellen Verzeichnis zusammen mit der Dateigröße aus und berechnet die Gesamtgröße der Dateien:" + "Wir wünschen Ihnen ein frohes Fest und einen guten Rutsch ins neue Jahr." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, + "outputs": [], "source": [ - "import os\n", - "\n", - "# Tabellenkopf ausgeben\n", - "print(\"Bytes\\tName\")\n", - "print(\"--------------------------------------------------------------\")\n", + "\"\"\" \n", + "Quelle: https://teampython.wordpress.com/2015/12/12/print-a-christmas-tree/\n", + "Python 3 version by antiloquax (2015), based on code from datamungeblog.com.\n", + "\"\"\"\n", + " \n", + "from random import choice\n", + "from random import random\n", + " \n", + "# If you change this, use an odd number.\n", + "size = 21\n", + "\n", + "# Probability that a character will be green.\n", + "prob_gr = 0.6\n", + "# Colour codes.\n", + "colours = [31, 33, 34, 35, 36, 37]\n", + "# Characters to use for decorations. Experiment with these.\n", + "# The chr(169) and chr(174) characters may not work in all terminals\n", + "# (extended ASCII, c and r in a circle).\n", + "decs = ['@', '&', '*', chr(169), chr(174)]\n", + "\n", + "# Format string for printing blinking characters.\n", + "blink_col = \"\\033[5;{0}m{1}\\033[0m\"\n", + "# String to print a green octothorpe ('#').\n", + "leaf = \"\\033[32m#\\033[0m\"\n", + "\n", + "# Width of the tree, will grow by 2 each time.\n", + "width = 1\n", + "# Initialise the tree string, with a star at the top.\n", + "tree = \"\\n{}*\\n\".format(' ' * (size))\n", + "\n", + "\"\"\" Main Loop starts now.\"\"\"\n", + " \n", + "\"\"\" We can't use the normal \"format\" centering approach:\n", + " (\"{:^nn}\".format(string) where \"nn\" is the width of the line), \n", + " with these ansi codes. This is because Python sees the strings as being\n", + " more than one character long (15 & 10 for baubles and leaves).\"\"\"\n", + "\n", + "# Loop from (size - 1) down to 0, using the counter as the padding size.\n", + "for pad in range(size - 1, -1, -1):\n", + " # Increase the width of the tree by 2.\n", + " width += 2\n", + " \n", + " # Put the characters for the line in \"temp\".\n", + " temp = \"\"\n", + " for j in range(width):\n", + " # Make some leaves.\n", + " if random() < prob_gr:\n", + " temp += leaf\n", + " # And also some baubles.\n", + " else:\n", + " temp += blink_col.format(choice(colours), choice(decs))\n", "\n", - "# Gesamtgröße in Bytes\n", - "bytes_sum = 0\n", + " # Add that string to the line, with padding.\n", + " tree += \"{0}{1}\\n\".format(' ' * pad, temp)\n", "\n", - "# Inhalt des aktuellen Verzeichnisses durchlaufen\n", - "for entry in os.scandir():\n", - " if entry.is_file() and entry.name.endswith(\".ipynb\"):\n", - " size = entry.stat().st_size\n", - " bytes_sum +=size\n", - " print(\"{:5d}\".format(size), entry.name, sep='\\t')\n", - " \n", - "print(\"--------------------------------------------------------------\")\n", - "print(bytes_sum, \"bytes =\", bytes_sum/1000, \"kilobytes =\", bytes_sum/1000000, \"Megabytes\")" + "# Add a \"trunk\" of 2 lines and return.\n", + "print(tree + \"{0}{1}\\n\".format(' ' * (size - 1), \"000\") * 2)\n", + "print(\"\\x46\\x72\\x6f\\x68\\x65\\x20\\x46\\x65\\x73\\x74\\x74\\x61\\x67\\x65\\x21\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "[Und noch viele weitere schöne Beispiele](https://codegolf.stackexchange.com/questions/15860/)" ] }, {