From 3dda1aa4379e9ea3db5c1366280fdb7782145971 Mon Sep 17 00:00:00 2001 From: Yuman Hordijk Date: Fri, 12 Apr 2024 15:10:58 +0200 Subject: [PATCH] Added the __sizeof__ method and also the getsizeof method --- src/tcutility/results/result.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/tcutility/results/result.py b/src/tcutility/results/result.py index 3b800411..ff06ec57 100644 --- a/src/tcutility/results/result.py +++ b/src/tcutility/results/result.py @@ -1,5 +1,6 @@ '''Module containing the TCutility.results.result.Result class.''' import dictfunc +import sys class Result(dict): @@ -99,6 +100,21 @@ def __bool__(self): '''Make sure that keys starting and ending in "__" are skipped''' return len([key for key in self.keys() if not (key.startswith('__') and key.endswith('__'))]) > 0 + def __sizeof__(self): + ''' + Magic method used by `sys.getsizeof `_ to determine the memory footprint of this object. + ''' + s = super().__sizeof__() + for key in self.multi_keys(): + s += sys.getsizeof(self.get_multi_key(key)) + return s + + def getsizeof(self): + ''' + Return the size of this object in bytes. + ''' + return self.__sizeof__() + def get_parent_tree(self): '''Method to get the path from this object to the parent object. The result is presented in a formatted string''' # every parent except the top-most parent has defined a __parent__ attribute