%load_ext watermark
%watermark -v -m -p lolviz
CPython 3.6.5 IPython 6.4.0 lolviz n compiler : GCC 7.3.0 system : Linux release : 4.15.0-23-generic machine : x86_64 processor : x86_64 CPU cores : 4 interpreter: 64bit
from lolviz import *
data = ['hi', 'mom', {3, 4}, {"parrt": "user"}]
g = listviz(data)
print(g.source) # if you want to see the graphviz source
g.view() # render and show graphviz.files.Source object
digraph G { nodesep=.05; node [penwidth="0.5", width=.1,height=.1]; node139621679300936 [shape="box", space="0.0", margin="0.01", fontcolor="#444443", fontname="Helvetica", label=<<table BORDER="0" CELLBORDER="0" CELLSPACING="0"> <tr> <td cellspacing="0" cellpadding="0" bgcolor="#fefecd" border="1" sides="br" valign="top"><font color="#444443" point-size="9">0</font></td> <td cellspacing="0" cellpadding="0" bgcolor="#fefecd" border="1" sides="br" valign="top"><font color="#444443" point-size="9">1</font></td> <td cellspacing="0" cellpadding="0" bgcolor="#fefecd" border="1" sides="br" valign="top"><font color="#444443" point-size="9">2</font></td> <td cellspacing="0" cellpadding="0" bgcolor="#fefecd" border="1" sides="b" valign="top"><font color="#444443" point-size="9">3</font></td> </tr> <tr> <td port="0" bgcolor="#fefecd" border="1" sides="r" align="center"><font point-size="11">'hi'</font></td> <td port="1" bgcolor="#fefecd" border="1" sides="r" align="center"><font point-size="11">'mom'</font></td> <td port="2" bgcolor="#fefecd" border="1" sides="r" align="center"><font point-size="11">{3, 4}</font></td> <td port="3" bgcolor="#fefecd" border="0" align="center"><font point-size="11">{'parrt': 'user'}</font></td> </tr></table> >]; }
'Source.gv.pdf'
It opened a window showing me this image:
I test here all the features of lolviz :
squares = [ i**2 for i in range(10) ]
squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
listviz(squares)
n, m = 3, 4
example_matrix = [[0 if i != j else 1 for i in range(n)] for j in range(m)]
example_matrix
[[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]]
lolviz(example_matrix)
n, m, o = 2, 3, 4
example_3D_matrix = [[[
1 if i < j < k else 0
for i in range(n)]
for j in range(m)]
for k in range(o)]
example_3D_matrix
[[[0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0]], [[0, 0], [1, 0], [0, 0]], [[0, 0], [1, 0], [1, 1]]]
lolviz(example_3D_matrix)
It works, even if it is not as pretty.
Only for binary trees, apparently. Let's try with a dictionary that looks like a binary tree:
anakin = {
"name": "Anakin Skywalker",
"son": {
"name": "Luke Skywalker",
},
"daughter": {
"name": "Leia Skywalker",
},
}
from pprint import pprint
pprint(anakin)
{'daughter': {'name': 'Leia Skywalker'}, 'name': 'Anakin Skywalker', 'son': {'name': 'Luke Skywalker'}}
treeviz(anakin, leftfield='son', rightfield='daugther')
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-42-93eee6442074> in <module>() ----> 1 treeviz(myDict(anakin), leftfield='son', rightfield='daugther') /usr/local/lib/python3.6/dist-packages/lolviz.py in treeviz(root, leftfield, rightfield) 102 nodename = "node%d" % id(p) 103 fields = [] --> 104 for k, v in p.__dict__.items(): 105 if k==leftfield or k==rightfield: 106 continue AttributeError: 'dict' object has no attribute '__dict__'
It doesn't work out of the box for dictionaries, sadly.
Let's check another example:
class Tree:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
root = Tree('parrt',
Tree('mary',
Tree('jim',
Tree('srinivasan'),
Tree('april'))),
Tree('xue',None,Tree('mike')))
treeviz(root)
objviz(anakin)
objviz(anakin.values())
objviz(anakin.items())
For complex numbers for instance?
z = 1+4j
print(z)
(1+4j)
objviz(z)
OK, this fails.
def factorial(n):
if n < 0: return 0
elif n == 0: return 1
else: return n * factorial(n - 1)
for n in range(12):
print(f"{n}! = {factorial(n)}")
0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320 9! = 362880 10! = 3628800 11! = 39916800
And now with some visualization:
from IPython.display import display
def factorial2(n):
display(callsviz(varnames=["n"]))
if n < 0: return 0
elif n == 0: return 1
else: return n * factorial2(n - 1)
n = 4
print(f"{n}! = {factorial2(n)}")
4! = 24
We really see the "call stack" as the system keeps track of the nested calls. I like that! 👌
import string
string.hexdigits
'0123456789abcdefABCDEF'
strviz(string.hexdigits)
That's it. See this other example for more.