%load_ext watermark
%watermark -v -m -p lolviz
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
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
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
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
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)
treeviz(anakin, leftfield='son', rightfield='daugther')
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)
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)}")
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)}")
We really see the "call stack" as the system keeps track of the nested calls. I like that! 👌
import string
string.hexdigits
strviz(string.hexdigits)
That's it. See this other example for more.