Skip to content

Commit

Permalink
Added the __sizeof__ method and also the getsizeof method
Browse files Browse the repository at this point in the history
  • Loading branch information
YHordijk committed Apr 12, 2024
1 parent f5e54d4 commit 3dda1aa
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/tcutility/results/result.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'''Module containing the TCutility.results.result.Result class.'''
import dictfunc
import sys


class Result(dict):
Expand Down Expand Up @@ -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 <https://docs.python.org/3/library/sys.html#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
Expand Down

0 comments on commit 3dda1aa

Please sign in to comment.