In [1]:
%load_ext watermark
%watermark -v -m -p lolviz
In [3]:
from lolviz import *
In [4]:
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
Out[4]:
It opened a window showing me this image:
I test here all the features of lolviz :
In [13]:
squares = [ i**2 for i in range(10) ]
In [14]:
squares
Out[14]:
In [15]:
listviz(squares)
Out[15]:
In [16]:
n, m = 3, 4
example_matrix = [[0 if i != j else 1 for i in range(n)] for j in range(m)]
In [17]:
example_matrix
Out[17]:
In [18]:
lolviz(example_matrix)
Out[18]:
In [22]:
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)]
In [23]:
example_3D_matrix
Out[23]:
In [25]:
lolviz(example_3D_matrix)
Out[25]:
It works, even if it is not as pretty.
In [26]:
anakin = {
"name": "Anakin Skywalker",
"son": {
"name": "Luke Skywalker",
},
"daughter": {
"name": "Leia Skywalker",
},
}
In [27]:
from pprint import pprint
pprint(anakin)
In [42]:
treeviz(anakin, leftfield='son', rightfield='daugther')
It doesn't work out of the box for dictionaries, sadly.
Let's check another example:
In [67]:
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)
Out[67]:
In [48]:
objviz(anakin)
Out[48]:
In [49]:
objviz(anakin.values())
Out[49]:
In [50]:
objviz(anakin.items())
Out[50]:
For complex numbers for instance?
In [74]:
z = 1+4j
In [75]:
print(z)
In [77]:
objviz(z)
Out[77]:
OK, this fails.
In [55]:
def factorial(n):
if n < 0: return 0
elif n == 0: return 1
else: return n * factorial(n - 1)
In [57]:
for n in range(12):
print(f"{n}! = {factorial(n)}")
And now with some visualization:
In [ ]:
from IPython.display import display
In [72]:
def factorial2(n):
display(callsviz(varnames=["n"]))
if n < 0: return 0
elif n == 0: return 1
else: return n * factorial2(n - 1)
In [73]:
n = 4
print(f"{n}! = {factorial2(n)}")
We really see the "call stack" as the system keeps track of the nested calls. I like that! 👌
In [8]:
import string
string.hexdigits
Out[8]:
In [9]:
strviz(string.hexdigits)
Out[9]:
That's it. See this other example for more.