{ "cells": [ { "cell_type": "markdown", "metadata": { "toc": "true" }, "source": [ "# Table of Contents\n", "

1  Testing lolviz
1.1  Testing naively
1.2  Testing from within a Jupyter notebook
1.2.1  List
1.2.2  List of lists
1.2.3  List of lists of lists???
1.2.4  Tree
1.2.5  Objects
1.2.6  Calls
1.2.7  String
1.3  Conclusion
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Testing [lolviz](https://github.com/parrt/lolviz)\n", "\n", "I liked how the [lolviz](https://github.com/parrt/lolviz) module looked like. Let's try it!" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPython 3.6.5\n", "IPython 6.4.0\n", "\n", "lolviz n\u0007\n", "\n", "compiler : GCC 7.3.0\n", "system : Linux\n", "release : 4.15.0-23-generic\n", "machine : x86_64\n", "processor : x86_64\n", "CPU cores : 4\n", "interpreter: 64bit\n" ] } ], "source": [ "%load_ext watermark\n", "%watermark -v -m -p lolviz" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from lolviz import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Testing naively" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " digraph G {\n", " nodesep=.05;\n", " node [penwidth=\"0.5\", width=.1,height=.1];\n", " node139621679300936 [shape=\"box\", space=\"0.0\", margin=\"0.01\", fontcolor=\"#444443\", fontname=\"Helvetica\", label=<\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
0123
'hi''mom'{3, 4}{'parrt': 'user'}
\n", ">];\n", "}\n", "\n" ] }, { "data": { "text/plain": [ "'Source.gv.pdf'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = ['hi', 'mom', {3, 4}, {\"parrt\": \"user\"}]\n", "g = listviz(data)\n", "print(g.source) # if you want to see the graphviz source\n", "g.view() # render and show graphviz.files.Source object" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It opened a window showing me this image:\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Testing from within a Jupyter notebook\n", "\n", "I test here all [the features of lolviz](https://github.com/parrt/lolviz#functionality) :" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "squares = [ i**2 for i in range(10) ]" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "squares" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "G\n", "\n", "\n", "\n", "node139621679302728\n", "\n", "\n", "\n", "0\n", "\n", "\n", "1\n", "\n", "\n", "2\n", "\n", "\n", "3\n", "\n", "\n", "4\n", "\n", "\n", "5\n", "\n", "\n", "6\n", "\n", "\n", "7\n", "\n", "\n", "8\n", "\n", "\n", "9\n", "\n", "\n", "0\n", "\n", "\n", "1\n", "\n", "\n", "4\n", "\n", "\n", "9\n", "\n", "\n", "16\n", "\n", "\n", "25\n", "\n", "\n", "36\n", "\n", "\n", "49\n", "\n", "\n", "64\n", "\n", "81\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "listviz(squares)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List of lists" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "n, m = 3, 4\n", "example_matrix = [[0 if i != j else 1 for i in range(n)] for j in range(m)]" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example_matrix" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "G\n", "\n", "\n", "\n", "node139621679287176\n", "\n", "\n", "\n", "0\n", "\n", "\n", "1\n", "\n", "\n", "2\n", "\n", "3\n", "\n", "\n", "\n", "node139621670526280\n", "\n", "\n", "\n", "0\n", "\n", "\n", "1\n", "\n", "\n", "2\n", "\n", "\n", "1\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "\n", "\n", "node139621679287176:0->node139621670526280:w\n", "\n", "\n", "\n", "\n", "\n", "node139621679304328\n", "\n", "\n", "\n", "0\n", "\n", "\n", "1\n", "\n", "\n", "2\n", "\n", "\n", "0\n", "\n", "\n", "1\n", "\n", "0\n", "\n", "\n", "\n", "node139621679287176:1->node139621679304328:w\n", "\n", "\n", "\n", "\n", "\n", "node139621670630344\n", "\n", "\n", "\n", "0\n", "\n", "\n", "1\n", "\n", "\n", "2\n", "\n", "\n", "0\n", "\n", "\n", "0\n", "\n", "1\n", "\n", "\n", "\n", "node139621679287176:2->node139621670630344:w\n", "\n", "\n", "\n", "\n", "\n", "node139621679286344\n", "\n", "\n", "\n", "0\n", "\n", "\n", "1\n", "\n", "\n", "2\n", "\n", "\n", "0\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "\n", "\n", "node139621679287176:3->node139621679286344:w\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lolviz(example_matrix)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List of lists of lists???" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "n, m, o = 2, 3, 4\n", "example_3D_matrix = [[[\n", " 1 if i < j < k else 0\n", " for i in range(n)]\n", " for j in range(m)]\n", " for k in range(o)]" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[[0, 0], [0, 0], [0, 0]],\n", " [[0, 0], [0, 0], [0, 0]],\n", " [[0, 0], [1, 0], [0, 0]],\n", " [[0, 0], [1, 0], [1, 1]]]" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example_3D_matrix" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "G\n", "\n", "\n", "\n", "node139621670152520\n", "\n", "\n", "\n", "0\n", "\n", "\n", "1\n", "\n", "\n", "2\n", "\n", "3\n", "\n", "\n", "\n", "node139621670150344\n", "\n", "\n", "\n", "0\n", "\n", "\n", "1\n", "\n", "\n", "2\n", "\n", "\n", "[0, 0]\n", "\n", "\n", "[0, 0]\n", "\n", "[0, 0]\n", "\n", "\n", "\n", "node139621670152520:0->node139621670150344:w\n", "\n", "\n", "\n", "\n", "\n", "node139621670441480\n", "\n", "\n", "\n", "0\n", "\n", "\n", "1\n", "\n", "\n", "2\n", "\n", "\n", "[0, 0]\n", "\n", "\n", "[0, 0]\n", "\n", "[0, 0]\n", "\n", "\n", "\n", "node139621670152520:1->node139621670441480:w\n", "\n", "\n", "\n", "\n", "\n", "node139621670152456\n", "\n", "\n", "\n", "0\n", "\n", "\n", "1\n", "\n", "\n", "2\n", "\n", "\n", "[0, 0]\n", "\n", "\n", "[1, 0]\n", "\n", "[0, 0]\n", "\n", "\n", "\n", "node139621670152520:2->node139621670152456:w\n", "\n", "\n", "\n", "\n", "\n", "node139621670441032\n", "\n", "\n", "\n", "0\n", "\n", "\n", "1\n", "\n", "\n", "2\n", "\n", "\n", "[0, 0]\n", "\n", "\n", "[1, 0]\n", "\n", "[1, 1]\n", "\n", "\n", "\n", "node139621670152520:3->node139621670441032:w\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lolviz(example_3D_matrix)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It works, even if it is not as pretty." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tree\n", "Only for binary trees, apparently. Let's try with a dictionary that looks like a binary tree:" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "anakin = {\n", " \"name\": \"Anakin Skywalker\",\n", " \"son\": {\n", " \"name\": \"Luke Skywalker\",\n", " },\n", " \"daughter\": {\n", " \"name\": \"Leia Skywalker\",\n", " },\n", "}" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'daughter': {'name': 'Leia Skywalker'},\n", " 'name': 'Anakin Skywalker',\n", " 'son': {'name': 'Luke Skywalker'}}\n" ] } ], "source": [ "from pprint import pprint\n", "pprint(anakin)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "'dict' object has no attribute '__dict__'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtreeviz\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmyDict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0manakin\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mleftfield\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'son'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrightfield\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'daugther'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/lolviz.py\u001b[0m in \u001b[0;36mtreeviz\u001b[0;34m(root, leftfield, rightfield)\u001b[0m\n\u001b[1;32m 102\u001b[0m \u001b[0mnodename\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"node%d\"\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0mid\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mp\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 103\u001b[0m \u001b[0mfields\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 104\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mv\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__dict__\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mitems\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 105\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m==\u001b[0m\u001b[0mleftfield\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m==\u001b[0m\u001b[0mrightfield\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 106\u001b[0m \u001b[0;32mcontinue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mAttributeError\u001b[0m: 'dict' object has no attribute '__dict__'" ] } ], "source": [ "treeviz(anakin, leftfield='son', rightfield='daugther')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It doesn't work out of the box for dictionaries, sadly.\n", "\n", "Let's check another example:" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "G\n", "\n", "\n", "\n", "node139621501191336\n", "\n", "\n", "\n", "Tree\n", "\n", "\n", "\n", "value \n", "\n", " 'parrt'\n", "\n", "\n", "\n", "\n", "left\n", "\n", "\n", "right\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "node139621501191168\n", "\n", "\n", "\n", "Tree\n", "\n", "\n", "\n", "value \n", "\n", " 'mary'\n", "\n", "\n", "\n", "\n", "left\n", "\n", "\n", "right\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "node139621501191336:c->node139621501191168\n", "\n", "\n", "\n", "\n", "\n", "\n", "node139621501191280\n", "\n", "\n", "\n", "Tree\n", "\n", "\n", "\n", "value \n", "\n", " 'xue'\n", "\n", "\n", "\n", "\n", "left\n", "\n", "\n", "right\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "node139621501191336:c->node139621501191280\n", "\n", "\n", "\n", "\n", "\n", "\n", "node139621501191112\n", "\n", "\n", "\n", "Tree\n", "\n", "\n", "\n", "value \n", "\n", " 'jim'\n", "\n", "\n", "\n", "\n", "left\n", "\n", "\n", "right\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "node139621501191168:c->node139621501191112\n", "\n", "\n", "\n", "\n", "\n", "\n", "node139621501191000\n", "\n", "\n", "\n", "Tree\n", "\n", "\n", "\n", "value \n", "\n", " 'srinivasan'\n", "\n", "\n", "\n", "\n", "left\n", "\n", "\n", "right\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "node139621501191112:c->node139621501191000\n", "\n", "\n", "\n", "\n", "\n", "\n", "node139621501191056\n", "\n", "\n", "\n", "Tree\n", "\n", "\n", "\n", "value \n", "\n", " 'april'\n", "\n", "\n", "\n", "\n", "left\n", "\n", "\n", "right\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "node139621501191112:c->node139621501191056\n", "\n", "\n", "\n", "\n", "\n", "\n", "node139621501191224\n", "\n", "\n", "\n", "Tree\n", "\n", "\n", "\n", "value \n", "\n", " 'mike'\n", "\n", "\n", "\n", "\n", "left\n", "\n", "\n", "right\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "node139621501191280:c->node139621501191224\n", "\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Tree:\n", " def __init__(self, value, left=None, right=None):\n", " self.value = value\n", " self.left = left\n", " self.right = right\n", " \n", "root = Tree('parrt',\n", " Tree('mary',\n", " Tree('jim',\n", " Tree('srinivasan'),\n", " Tree('april'))),\n", " Tree('xue',None,Tree('mike')))\n", "\n", "treeviz(root)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Objects" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "G\n", "\n", "\n", "\n", "node139621669833160\n", "\n", "\n", "\n", "0\n", "\n", "\n", "1\n", "\n", "\n", "2\n", "\n", "\n", "'name'\n", "\n", "\n", "'son'\n", "\n", "'daughter'\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "objviz(anakin)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "G\n", "\n", "\n", "\n", "node139621669714760\n", "\n", "\n", "\n", "0\n", "\n", "\n", "1\n", "\n", "2\n", "\n", "\n", "\n", "node139621670161144\n", "\n", "\n", "'name' \n", "\n", "\n", " 'Luke Skywalker'\n", "\n", "\n", "\n", "node139621669714760:1->node139621670161144:w\n", "\n", "\n", "\n", "\n", "\n", "node139621679147480\n", "\n", "\n", "'name' \n", "\n", "\n", " 'Leia Skywalker'\n", "\n", "\n", "\n", "node139621669714760:2->node139621679147480:w\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "objviz(anakin.values())" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "G\n", "\n", "\n", "\n", "node139621669834568\n", "\n", "\n", "\n", "0\n", "\n", "\n", "1\n", "\n", "2\n", "\n", "\n", "\n", "node139621669839688\n", "\n", "\n", "\n", "0\n", "\n", "\n", "1\n", "\n", "\n", "'name'\n", "\n", "'Anakin Skywalker'\n", "\n", "\n", "\n", "node139621669834568:0->node139621669839688:w\n", "\n", "\n", "\n", "\n", "\n", "node139621669769864\n", "\n", "\n", "\n", "0\n", "\n", "\n", "1\n", "\n", "\n", "'son'\n", "\n", "  \n", "\n", "\n", "\n", "node139621669834568:1->node139621669769864:w\n", "\n", "\n", "\n", "\n", "\n", "node139621670000264\n", "\n", "\n", "\n", "0\n", "\n", "\n", "1\n", "\n", "\n", "'daughter'\n", "\n", "  \n", "\n", "\n", "\n", "node139621669834568:2->node139621670000264:w\n", "\n", "\n", "\n", "\n", "\n", "node139621670161144\n", "\n", "\n", "'name' \n", "\n", "\n", " 'Luke Skywalker'\n", "\n", "\n", "\n", "node139621669769864:c->node139621670161144\n", "\n", "\n", "\n", "\n", "\n", "\n", "node139621679147480\n", "\n", "\n", "'name' \n", "\n", "\n", " 'Leia Skywalker'\n", "\n", "\n", "\n", "node139621670000264:c->node139621679147480\n", "\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "objviz(anakin.items())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For complex numbers for instance?" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [], "source": [ "z = 1+4j" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1+4j)\n" ] } ], "source": [ "print(z)" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "G\n", "\n", "\n", "\n", "node139621501117296\n", "CANNOT HANDLE: type=...\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "objviz(z)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "OK, this fails." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Calls" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [], "source": [ "def factorial(n):\n", " if n < 0: return 0\n", " elif n == 0: return 1\n", " else: return n * factorial(n - 1)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0! = 1\n", "1! = 1\n", "2! = 2\n", "3! = 6\n", "4! = 24\n", "5! = 120\n", "6! = 720\n", "7! = 5040\n", "8! = 40320\n", "9! = 362880\n", "10! = 3628800\n", "11! = 39916800\n" ] } ], "source": [ "for n in range(12):\n", " print(f\"{n}! = {factorial(n)}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And now with some visualization:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from IPython.display import display" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [], "source": [ "def factorial2(n):\n", " display(callsviz(varnames=[\"n\"]))\n", " if n < 0: return 0\n", " elif n == 0: return 1\n", " else: return n * factorial2(n - 1)" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "G\n", "\n", "\n", "\n", "node139621871104440\n", "\n", "\n", "\n", "globals\n", "\n", "\n", "\n", "n \n", "\n", " 4\n", "\n", "\n", "\n", "node139621870916024\n", "\n", "\n", "\n", "factorial2\n", "\n", "\n", "\n", "n \n", "\n", " 4\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "G\n", "\n", "\n", "\n", "node139621871104440\n", "\n", "\n", "\n", "globals\n", "\n", "\n", "\n", "n \n", "\n", " 4\n", "\n", "\n", "\n", "node139621870916024\n", "\n", "\n", "\n", "factorial2\n", "\n", "\n", "\n", "n \n", "\n", " 4\n", "\n", "\n", "\n", "\n", "node139621870950200\n", "\n", "\n", "\n", "factorial2\n", "\n", "\n", "\n", "n \n", "\n", " 3\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "G\n", "\n", "\n", "\n", "node139621871104440\n", "\n", "\n", "\n", "globals\n", "\n", "\n", "\n", "n \n", "\n", " 4\n", "\n", "\n", "\n", "node139621870916024\n", "\n", "\n", "\n", "factorial2\n", "\n", "\n", "\n", "n \n", "\n", " 4\n", "\n", "\n", "\n", "\n", "node139621870950200\n", "\n", "\n", "\n", "factorial2\n", "\n", "\n", "\n", "n \n", "\n", " 3\n", "\n", "\n", "\n", "\n", "node139621870776968\n", "\n", "\n", "\n", "factorial2\n", "\n", "\n", "\n", "n \n", "\n", " 2\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "G\n", "\n", "\n", "\n", "node139621871104440\n", "\n", "\n", "\n", "globals\n", "\n", "\n", "\n", "n \n", "\n", " 4\n", "\n", "\n", "\n", "node139621870916024\n", "\n", "\n", "\n", "factorial2\n", "\n", "\n", "\n", "n \n", "\n", " 4\n", "\n", "\n", "\n", "\n", "node139621870950200\n", "\n", "\n", "\n", "factorial2\n", "\n", "\n", "\n", "n \n", "\n", " 3\n", "\n", "\n", "\n", "\n", "node139621870776968\n", "\n", "\n", "\n", "factorial2\n", "\n", "\n", "\n", "n \n", "\n", " 2\n", "\n", "\n", "\n", "\n", "node139621871111880\n", "\n", "\n", "\n", "factorial2\n", "\n", "\n", "\n", "n \n", "\n", " 1\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "G\n", "\n", "\n", "\n", "node139621871104440\n", "\n", "\n", "\n", "globals\n", "\n", "\n", "\n", "n \n", "\n", " 4\n", "\n", "\n", "\n", "node139621870916024\n", "\n", "\n", "\n", "factorial2\n", "\n", "\n", "\n", "n \n", "\n", " 4\n", "\n", "\n", "\n", "\n", "node139621870950200\n", "\n", "\n", "\n", "factorial2\n", "\n", "\n", "\n", "n \n", "\n", " 3\n", "\n", "\n", "\n", "\n", "node139621870776968\n", "\n", "\n", "\n", "factorial2\n", "\n", "\n", "\n", "n \n", "\n", " 2\n", "\n", "\n", "\n", "\n", "node139621871111880\n", "\n", "\n", "\n", "factorial2\n", "\n", "\n", "\n", "n \n", "\n", " 1\n", "\n", "\n", "\n", "\n", "node40808488\n", "\n", "\n", "\n", "factorial2\n", "\n", "\n", "\n", "n \n", "\n", " 0\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "4! = 24\n" ] } ], "source": [ "n = 4\n", "print(f\"{n}! = {factorial2(n)}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We really see the \"call stack\" as the system keeps track of the nested calls. I like that! 👌" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### String" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0123456789abcdefABCDEF'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import string\n", "string.hexdigits" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "G\n", "\n", "\n", "\n", "node139622250733760\n", "\n", "\n", "\n", "0\n", "\n", "\n", "1\n", "\n", "\n", "2\n", "\n", "\n", "3\n", "\n", "\n", "4\n", "\n", "\n", "5\n", "\n", "\n", "6\n", "\n", "\n", "7\n", "\n", "\n", "8\n", "\n", "\n", "9\n", "\n", "\n", "10\n", "\n", "\n", "11\n", "\n", "\n", "12\n", "\n", "\n", "13\n", "\n", "\n", "14\n", "\n", "\n", "15\n", "\n", "\n", "16\n", "\n", "\n", "17\n", "\n", "\n", "18\n", "\n", "\n", "19\n", "\n", "\n", "20\n", "\n", "\n", "21\n", "'\n", "\n", "0\n", "\n", "1\n", "\n", "2\n", "\n", "3\n", "\n", "4\n", "\n", "5\n", "\n", "6\n", "\n", "7\n", "\n", "8\n", "\n", "9\n", "\n", "a\n", "\n", "b\n", "\n", "c\n", "\n", "d\n", "\n", "e\n", "\n", "f\n", "\n", "A\n", "\n", "B\n", "\n", "C\n", "\n", "D\n", "\n", "E\n", "\n", "F\n", "'\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "strviz(string.hexdigits)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conclusion\n", "That's it. See [this other example](https://github.com/parrt/lolviz/blob/master/examples.ipynb) for more." ] } ], "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.6.5" }, "toc": { "colors": { "hover_highlight": "#DAA520", "running_highlight": "#FF0000", "selected_highlight": "#FFD700" }, "moveMenuLeft": true, "nav_menu": { "height": "226px", "width": "252px" }, "navigate_menu": true, "number_sections": true, "sideBar": false, "threshold": 4, "toc_cell": true, "toc_section_display": "block", "toc_window_display": true }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }