def test_repr_str(self): """Test that the right representation is returned for strings. """ self.assertEqual(summary._repr(''), 'str') self.assertEqual(summary._repr('', 1), 'str') self.assertEqual(summary._repr('', verbosity=1), 'str') self.assertEqual(summary._repr('', verbosity=2), 'str') self.assertEqual(summary._repr('', verbosity=100), 'str')
def test_summarize(self): """Test summarize method. """ objects = [1, 'a', 'b', 'a', 5, [], {}] expected = [[summary._repr(''), 3, 3*getsizeof('a')],\ [summary._repr(1), 2, 2*getsizeof(1)],\ [summary._repr([]), 1, getsizeof([])],\ [summary._repr({}), 1, getsizeof({})]] res = summary.summarize(objects) for row_e in res: self.assert_(row_e in expected)
def test_summarize(self): """Test summarize method. """ objects = [1, 'a', 'b', 'a', 5, [], {}] expected = [[summary._repr(''), 3, 3*_getsizeof('a')],\ [summary._repr(1), 2, 2*_getsizeof(1)],\ [summary._repr([]), 1, _getsizeof([])],\ [summary._repr({}), 1, _getsizeof({})]] res = summary.summarize(objects) for row_e in res: self.assert_(row_e in expected)
def test_sweep(self): """Test that all and only empty entries are removed from a summary.""" objects = ['the', 'quick', 'brown', 'fox', 1298, 123, 234, [], {}] summ = summary.summarize(objects) # correct removal of rows when sizes are empty summary._subtract(summ, {}) summary._subtract(summ, []) summ = summary._sweep(summ) found_dict = found_tuple = False for row in summ: if row[0] == "<type 'dict'>": found_dict = True if row[0] == "<type 'tuple'>": found_tuple = True self.assert_(found_dict == False) self.assert_(found_tuple == False) # do not remove row if one of the sizes is not empty # e.g. if the number of objects of a type did not change, but the # total size did summ = summary._subtract(summ, 'the') summ = summary._subtract(summ, 'quick') summ = summary._subtract(summ, 'brown') summ = summary._subtract(summ, '42') summ = summary._sweep(summ) found_string = False for row in summ: if row[0] == summary._repr(''): found_string = True self.assert_(row[1] == 0) totalsize = getsizeof('fox') - getsizeof('42') self.assert_(row[2] == totalsize) self.assert_(found_string == True)
def test_sweep(self): """Test that all and only empty entries are removed from a summary.""" objects = ['the', 'quick', 'brown', 'fox', 1298, 123, 234, [], {}] summ = summary.summarize(objects) # correct removal of rows when sizes are empty summary._subtract(summ, {}) summary._subtract(summ, []) summ = summary._sweep(summ) found_dict = found_tuple = False for row in summ: if row[0] == "<type 'dict'>": found_dict = True if row[0] == "<type 'tuple'>": found_tuple = True self.assert_(found_dict == False) self.assert_(found_tuple == False) # do not remove row if one of the sizes is not empty # e.g. if the number of objects of a type did not change, but the # total size did summ = summary._subtract(summ, 'the') summ = summary._subtract(summ, 'quick') summ = summary._subtract(summ, 'brown') summ = summary._subtract(summ, '42') summ = summary._sweep(summ) found_string = False for row in summ: if row[0] == summary._repr(''): found_string = True self.assert_(row[1] == 0) totalsize = _getsizeof('fox') - _getsizeof('42') self.assert_(row[2] == totalsize) self.assert_(found_string == True)
def test_subtract(self): """Test that a single object's data is correctly subtracted from a summary. - result in correct total size and total number of objects - if object was not listed before, it should be listed negative afterwards """ objects = ['the', 'quick', 'brown', 'fox', 1298, 123, 234, [], {}] summ = summary.summarize(objects) summary._subtract(summ, 'the') summary._subtract(summ, {}) summary._subtract(summ, (1, )) # to verify that these rows where actually included afterwards checked_str = checked_dict = checked_tuple = False for row in summ: if row[0] == summary._repr(''): totalsize = getsizeof('quick') + getsizeof('brown') +\ getsizeof('fox') self.assert_(row[1] == 3, "%s != %s" % (row[1], 3)) self.assert_(row[2] == totalsize, totalsize) checked_str = True if row[0] == summary._repr({}): self.assert_(row[1] == 0) self.assert_(row[2] == 0) checked_dict = True if row[0] == summary._repr((1, )): self.assert_(row[1] == -1) self.assert_(row[2] == -getsizeof((1, ))) checked_tuple = True self.assert_(checked_str, "no str found in summary") self.assert_(checked_dict, "no dict found in summary") self.assert_(checked_tuple, "no tuple found in summary") summary._subtract(summ, 'quick') summary._subtract(summ, 'brown') checked_str = False for row in summ: if row[0] == summary._repr(''): self.assert_(row[1] == 1) self.assert_(row[2] == getsizeof('fox')) checked_str = True self.assert_(checked_str, "no str found in summ")
def test_subtract(self): """Test that a single object's data is correctly subtracted from a summary. - result in correct total size and total number of objects - if object was not listed before, it should be listed negative afterwards """ objects = ['the', 'quick', 'brown', 'fox', 1298, 123, 234, [], {}] summ = summary.summarize(objects) summary._subtract(summ, 'the') summary._subtract(summ, {}) summary._subtract(summ, (1,)) # to verify that these rows where actually included afterwards checked_str = checked_dict = checked_tuple = False for row in summ: if row[0] == summary._repr(''): totalsize = _getsizeof('quick') + _getsizeof('brown') +\ _getsizeof('fox') self.assert_(row[1] == 3, "%s != %s" % (row[1], 3)) self.assert_(row[2] == totalsize, totalsize) checked_str = True if row[0] == summary._repr({}): self.assert_(row[1] == 0) self.assert_(row[2] == 0) checked_dict = True if row[0] == summary._repr((1,)): self.assert_(row[1] == -1) self.assert_(row[2] == -_getsizeof((1,))) checked_tuple = True self.assert_(checked_str, "no str found in summary") self.assert_(checked_dict, "no dict found in summary") self.assert_(checked_tuple, "no tuple found in summary") summary._subtract(summ, 'quick') summary._subtract(summ, 'brown') checked_str = False for row in summ: if row[0] == summary._repr(''): self.assert_(row[1] == 1) self.assert_(row[2] == _getsizeof('fox')) checked_str = True self.assert_(checked_str, "no str found in summ")
def test_repr_dict(self): """Test that the right representation is returned for dicts. """ self.assertEqual(summary._repr({'a': 1}), 'dict') self.assertEqual(summary._repr({'a': 1}, 1), 'dict') self.assertEqual(summary._repr({'a': 1}, verbosity=1), 'dict') self.assertEqual(summary._repr({'a': 1}, verbosity=2), 'dict, len=1') self.assertEqual(summary._repr({'a': 1}, verbosity=3), 'dict, len=1') self.assertEqual(summary._repr({'a': 1}, verbosity=100), 'dict, len=1')
def format_(rows, limit=15, sort='size', order='descending'): """Format the rows as a summary. Keyword arguments: limit -- the maximum number of elements to be listed sort -- sort elements by 'size', 'type', or '#' order -- sort 'ascending' or 'descending' Heavily based on pympler.summary.print_ """ localrows = [] for row in rows: localrows.append(list(row)) # input validation sortby = ['type', '#', 'size'] if sort not in sortby: raise ValueError("invalid sort, should be one of" + str(sortby)) orders = ['ascending', 'descending'] if order not in orders: raise ValueError("invalid order, should be one of" + str(orders)) # sort rows if sortby.index(sort) == 0: if order == "ascending": localrows.sort(key=lambda x: summary._repr(x[0])) elif order == "descending": localrows.sort(key=lambda x: summary._repr(x[0]), reverse=True) else: if order == "ascending": localrows.sort(key=lambda x: x[sortby.index(sort)]) elif order == "descending": localrows.sort(key=lambda x: x[sortby.index(sort)], reverse=True) # limit rows localrows = localrows[0:limit] for row in localrows: row[2] = stringutils.pp(row[2]) # print rows localrows.insert(0, ["types", "# objects", "total size"]) return _format_table(localrows)
def test_repr_function(self): """Test that the right representation is returned for functions. """ def func(): pass self.assertEqual(summary._repr(func), 'function (func)') self.assertEqual(summary._repr(func, 1), 'function (func)') self.assertEqual(summary._repr(func, verbosity=1), 'function (func)') self.assertEqual(summary._repr(func, verbosity=2), 'function (muppy.test_summary.func)') self.assertEqual(summary._repr(func, verbosity=3), 'function (muppy.test_summary.func)') self.assertEqual(summary._repr(func, verbosity=100), 'function (muppy.test_summary.func)')
def gui_default_str_function(o): """Default str function for InteractiveBrowser.""" return summary._repr(o) + '(id=%s)' % id(o)
def test_repr(self): """Test that the right representation is returned. """ self.assert_(summary._repr(''), "<type 'str'>") self.assert_(summary._repr('', 1), "<type 'str'>") self.assert_(summary._repr('', verbosity=1), "<type 'str'>") self.assert_(summary._repr('', verbosity=100), "<type 'str'>")