Draw a graph of your data to see the structure of its references.
Project description
Graph your Memory
Want to draw a graph of your data in Python to better understand its structure or the Python memory model in general?
Just call memory_graph.show(your_data)
, an example:
import memory_graph
data = [ (1, 2), [3, 4], {5:'five', 6:'six'} ]
memory_graph.show( data )
This shows the graph with the starting point of your 'data' drawn using thick lines, the program blocks until the ENTER key is pressed.
If show()
doesn't work well on your system (the PDF viewer
integration is platform specific) use render()
to output the graph
in the format of your choosing. Use block=False
to turn off
blocking.
memory_graph.render( data, "my_graph.png", block=False )
Larger Example
This larger example shows objects that share a class (static) variable and also shows we can handle recursive references just fine.
import memory_graph
my_list = [10, 20, 30]
class My_Class:
my_class_var = 1000 # class variable: shared by different objects
def __init__(self):
self.var1 = "foo"
self.var2 = "bar"
obj1 = My_Class()
obj2 = My_Class()
data=[my_list, my_list, obj1, obj2]
my_list.append(data) # recursive reference
memory_graph.show( data )
Often it is useful to show all local variables using:
memory_graph.show( memory_graph.filter(locals()) )
Install
Install using pip:
pip install memory-graph
Config
Different aspects of memory_graph can be configured.
Config visualization, graphviz_nodes
Configure how the nodes of the graph are visualized with:
- memory_graph.graphviz_nodes.layout_vertical : bool
- determines if list/tuple/... are drawn vertically
- memory_graph.graphviz_nodes.type_category_to_color_map : dict
- a mapping from type to color
- memory_graph.graphviz_nodes.uncategorized_color : string
- color used for uncategorized types
See for color names: graphviz colors
To configure more about the visualization use:
digraph = memory_graph.create_graph( memory_graph.filter(locals()) )
and see the graphviz api to render it in many different ways.
Config node structure, rewrite_to_node
Configure the structure of the nodes in the graph with:
- memory_graph.rewrite_to_node.reduce_reference_types : set
- the types we copy to a node instead of drawing a reference to it
- memory_graph.rewrite_to_node.reduce_references_for_classes : bool
- determines if we reduce the references (to dict) in objects of classes
- memory_graph.rewrite_to_node.class_variables_label : str
- the label used to reference the class varibles (mappingproxy)
Config node creation, rewrite
Configure what nodes are created based on reading the given data structure:
- memory_graph.rewrite.singular_types : set
- all types rewritten to node as singular values (bool, int, float, ...)
- memory_graph.rewrite.linear_types : set
- all types rewritten to node as linear values (tuple, list, set, ...)
- memory_graph.rewrite.dict_types : set
- all types rewritten to node as dictionary values (dict, mappingproxy)
Config example
With configuration:
memory_graph.graphviz_nodes.layout_vertical = True # draw list/tuple/set/... vertically
memory_graph.graphviz_nodes.type_category_to_color_map['list'] = 'yellow' # change color of 'list' types
memory_graph.rewrite_to_node.reduce_reference_types.remove(int) # draw references to 'int' types
the last example looks like:
Author
Bas Terwijn
Inspiration
Inspired by PythonTutor.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for memory_graph-0.1.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e1abaab88687e683b2c9d597579874d9b243c09127e5421b2a1bfedead7f2339 |
|
MD5 | cad468207ffda9eb07a1273c6db7a0c4 |
|
BLAKE2b-256 | cf5075670dc4f419da83bcda7f234646c7630d8200368b4fbad0f82bc71e27f2 |