Skip to main content

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, block=True )

This shows the graph with the starting point of your 'data' drawn using thick lines, the program blocks until the ENTER key is pressed.

image

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 and open it yourself.

memory_graph.render( data, "my_graph.png", block=True )

Graph all Local Variables

Often it is useful to graph all the local variables using:

memory_graph.show( locals(), block=True )

Also useful to set as 'watch' in a debugger tool:

memory_graph.render( locals(), "my_debug_graph.pdf" )

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, 10]

class My_Class:
    my_class_var = 20 # class variable: shared by different objects
    
    def __init__(self):
        self.var1 = "foo"
        self.var2 = "bar"
        self.var3 = 20

obj1 = My_Class()
obj2 = My_Class()

data=[my_list, my_list, obj1, obj2]

my_list.append(data) # recursive reference

memory_graph.show( memory_graph.filter(locals()) )

image

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
  • memory_graph.graphviz_nodes.padding : int
    • amount of padding for node cells
  • memory_graph.graphviz_nodes.spacing : int
    • amount of spacing for node cells

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 Graph 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 add 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 reference (to dictionary) for classes

Config Node Creation, rewrite

Configure what nodes are created based on reading the given data structure:

  • memory_graph.rewrite.ignore_types : set
    • all types that we ignore, these will not be in the graph
  • 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)
  • memory_graph.rewrite.dict_ignore_dunder_keys : bool
    • determines if we ignore dunder keys ('__example') in dict_types

Config example

With configuration:

memory_graph.graphviz_nodes.layout_vertical = False                       # draw lists,tuples,sets,... horizontally
memory_graph.graphviz_nodes.type_category_to_color_map['list'] = 'yellow' # change color of 'list' type
memory_graph.rewrite_to_node.reduce_reference_types.remove(int)           # draw references to 'int' type

the last example looks like:

image

Troubleshooting

When edges overlap it can be hard to distinguish them. Using an interactive graphviz viewer, such as xdot, on a '*.gv' output file will help.

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

memory_graph-0.1.9.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

memory_graph-0.1.9-py2.py3-none-any.whl (10.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file memory_graph-0.1.9.tar.gz.

File metadata

  • Download URL: memory_graph-0.1.9.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.6

File hashes

Hashes for memory_graph-0.1.9.tar.gz
Algorithm Hash digest
SHA256 c38898cbe24b3029aded33ea5d1ba304bbb23ab0015234a13425a0ca87a94158
MD5 395e3ffd901811396d80811b96e9164b
BLAKE2b-256 af2586c2ab5d5bd86f4af6f19fd1104ca1247c443c64139abd419736359dc1c5

See more details on using hashes here.

File details

Details for the file memory_graph-0.1.9-py2.py3-none-any.whl.

File metadata

  • Download URL: memory_graph-0.1.9-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.6

File hashes

Hashes for memory_graph-0.1.9-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 32e125f3151768009563cfc2bd3a95308d44318f8e47d6ef00337867acc84ee7
MD5 1c8187d1ce778f42f749d1847d4b0b11
BLAKE2b-256 9c1281970bb3f9dba008f0d6d1522f7436a419b50466b73eaeb9e15f11823091

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page