Mark Dickinson added the comment:

> Mark, what was the use case you found?

It's essentially an IdentityDict, though I've found other more specific 
transforms useful.

I was writing a tool to find reference cycles between Python objects (we have a 
customer application that's working in a multithreaded COM environment and has 
to ensure that COM objects are released on the same types of threads they were 
created on, so we have to be careful about cyclic garbage and delayed garbage 
collection).

The graph of Python objects (class 'ObjectGraph') is modelled as a fairly 
standard directed graph (set of vertices, set of edges, two dictionaries 
mapping each edge to its head and tail), but of course for this application the 
dict and set have to be based on object identity rather than normal equality.  
Using a TransformDict (and an IdentitySet) lets me write the standard graph 
algorithms (e.g., for finding strongly connected components) in a natural way, 
leaving it to the TransformDict and IdentitySet to do the necessary id() 
conversions under the hood.)

I also have a similar AnnotatedGraph object (a sort of offline version of the 
ObjectGraph), where the edges and vertices carry additional information and 
it's convenient to be able to use a lightweight ID rather than an entire vertex 
or edge as a dictionary key.  Again, using a TransformDict lets one hide the 
details and present the graph manipulation code readably and naturally.

Some code here, if you're interested:

https://github.com/mdickinson/refcycle/blob/refactor/refcycle/object_graph.py

Caveat: it's work in progress.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue18986>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to