Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the __sizeof__ method and also the getsizeof method #200

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading