Rev 159: Add _MemObjectProxy.refs_as_dict() in http://bazaar.launchpad.net/~jameinel/meliae/mem-object-collection

John Arbash Meinel john at arbash-meinel.com
Tue Dec 29 22:17:06 GMT 2009


At http://bazaar.launchpad.net/~jameinel/meliae/mem-object-collection

------------------------------------------------------------
revno: 159
revision-id: john at arbash-meinel.com-20091229221642-p690pdl0kk8t376v
parent: john at arbash-meinel.com-20091229215959-s26x0ut338x5oku2
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: mem-object-collection
timestamp: Tue 2009-12-29 16:16:42 -0600
message:
  Add _MemObjectProxy.refs_as_dict()
-------------- next part --------------
=== modified file 'meliae/_loader.pyx'
--- a/meliae/_loader.pyx	2009-12-29 21:59:59 +0000
+++ b/meliae/_loader.pyx	2009-12-29 22:16:42 +0000
@@ -501,6 +501,33 @@
         return '{"address": %d, "type": "%s", "size": %d, %s"refs": [%s]}' % (
             self.address, self.type_str, self.size, value, ', '.join(refs))
 
+    def refs_as_dict(self):
+        """Expand the ref list considering it to be a 'dict' structure.
+
+        Often we have dicts that point to simple strings and ints, etc. This
+        tries to expand that as much as possible.
+        """
+        as_dict = {}
+        ref_list = self.ref_list
+        if self.type_str not in ('dict', 'module'):
+            # Instance dicts end with a 'type' reference
+            ref_list = ref_list[:-1]
+        for idx in xrange(0, len(ref_list), 2):
+            key = self.collection[ref_list[idx]]
+            val = self.collection[ref_list[idx+1]]
+            if key.value is not None:
+                key = key.value
+            # TODO: We should consider recursing if val is a 'known' type, such
+            #       a tuple/dict/etc
+            if val.type_str == 'bool':
+                val = (val.value == 'True')
+            elif val.value is not None:
+                val = val.value
+            elif val.type_str == 'NoneType':
+                val = None
+            as_dict[key] = val
+        return as_dict
+
 
 cdef class MemObjectCollection:
     """Track a bunch of _MemObject instances."""

=== modified file 'meliae/loader.py'
--- a/meliae/loader.py	2009-12-29 21:59:59 +0000
+++ b/meliae/loader.py	2009-12-29 22:16:42 +0000
@@ -432,7 +432,9 @@
                 continue
             collapsed += 1
             # We found an instance \o/
-            obj.ref_list = dict_obj.ref_list + extra_refs
+            new_refs = list(dict_obj.ref_list)
+            new_refs.extend(extra_refs)
+            obj.ref_list = new_refs
             obj.size = obj.size + dict_obj.size
             obj.total_size = 0
             if obj.type_str == 'instance':
@@ -455,26 +457,7 @@
         :param obj: Should be a MemObject representing an instance (that has
             been collapsed) or a dict.
         """
-        as_dict = {}
-        ref_list = obj.ref_list
-        if obj.type_str not in ('dict', 'module'):
-            # Instance dicts end with a 'type' reference
-            ref_list = ref_list[:-1]
-        for idx in xrange(0, len(ref_list), 2):
-            key = self.objs[ref_list[idx]]
-            val = self.objs[ref_list[idx+1]]
-            if key.value is not None:
-                key = key.value
-            # TODO: We should consider recursing if val is a 'known' type, such
-            #       a tuple/dict/etc
-            if val.type_str == 'bool':
-                val = (val.value == 'True')
-            elif val.value is not None:
-                val = val.value
-            elif val.type_str == 'NoneType':
-                val = None
-            as_dict[key] = val
-        return as_dict
+        return obj.refs_as_dict()
 
     def refs_as_list(self, obj):
         """Expand the ref list, considering it to be a list structure."""
@@ -505,6 +488,7 @@
         'True' to force using simplejson. None will probe to see if simplejson
         is available, and use it if it is. (With _speedups built, simplejson
         parses faster and more accurately than the regex.)
+    :param show_prog: If True, display the progress as we read in data
     """
     cleanup = None
     if isinstance(source, str):

=== modified file 'meliae/tests/test__loader.py'
--- a/meliae/tests/test__loader.py	2009-12-29 21:59:59 +0000
+++ b/meliae/tests/test__loader.py	2009-12-29 22:16:42 +0000
@@ -362,3 +362,15 @@
         self.assertEqual('int(1240 12B 12345 1.0Ktot)', repr(mop))
         mop.total_size = int(1024*1024*10.5)
         self.assertEqual('int(1240 12B 12345 10.5Mtot)', repr(mop))
+
+    def test_expand_refs_as_dict(self):
+        self.moc.add(1, 'str', 25, value='a')
+        self.moc.add(2, 'int', 12, value=1)
+        mop = self.moc.add(3, 'dict', 140, ref_list=[1, 2])
+        as_dict = mop.refs_as_dict()
+        self.assertEqual({'a': 1}, mop.refs_as_dict())
+        # It should even work if there is a 'trailing' entry, as after
+        # collapse, instances have the dict inline, and end with the reference
+        # to the type
+        mop = self.moc.add(4, 'MyClass', 156, ref_list=[2, 1, 8])
+        self.assertEqual({1: 'a'}, mop.refs_as_dict())



More information about the bazaar-commits mailing list