From eb10085de7783303fbebb0526e7f08c61bdb8aad Mon Sep 17 00:00:00 2001 From: Pascal van Kooten Date: Sat, 30 Sep 2017 21:45:25 +0200 Subject: [PATCH 1/3] recursive for list and tuple seems like a safe default --- cachey/nbytes.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cachey/nbytes.py b/cachey/nbytes.py index 7eac6d0..a2b4b60 100644 --- a/cachey/nbytes.py +++ b/cachey/nbytes.py @@ -34,5 +34,7 @@ def nbytes(o): return _array(o) elif hasattr(o, 'nbytes'): return o.nbytes + elif isinstance(o, (list, tuple)): + return sum(nbytes(x) for x in o) else: return sys.getsizeof(o) From e2d1811aec9e6308ddfe1013908cae5608fa4210 Mon Sep 17 00:00:00 2001 From: Pascal van Kooten Date: Sat, 30 Sep 2017 22:45:04 +0200 Subject: [PATCH 2/3] added getsizeof for list, added test --- cachey/nbytes.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cachey/nbytes.py b/cachey/nbytes.py index a2b4b60..8ec1e50 100644 --- a/cachey/nbytes.py +++ b/cachey/nbytes.py @@ -22,6 +22,9 @@ def nbytes(o): >>> import numpy as np >>> nbytes(np.ones(1000, dtype='i4')) 4000 + + >>> nbytes([123]) + 64 """ name = type(o).__module__ + '.' + type(o).__name__ @@ -35,6 +38,6 @@ def nbytes(o): elif hasattr(o, 'nbytes'): return o.nbytes elif isinstance(o, (list, tuple)): - return sum(nbytes(x) for x in o) + return sys.getsizeof(o) + sum(nbytes(x) for x in o) else: return sys.getsizeof(o) From c2f849fea89d07ab6a6e4dc961b4c5f38130549e Mon Sep 17 00:00:00 2001 From: Pascal van Kooten Date: Sat, 30 Sep 2017 23:00:33 +0200 Subject: [PATCH 3/3] added test that shows the correct value for python 3 Does require skipping as a test to pass though because of other python versions. --- cachey/nbytes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cachey/nbytes.py b/cachey/nbytes.py index 8ec1e50..c32e918 100644 --- a/cachey/nbytes.py +++ b/cachey/nbytes.py @@ -23,8 +23,8 @@ def nbytes(o): >>> nbytes(np.ones(1000, dtype='i4')) 4000 - >>> nbytes([123]) - 64 + >>> nbytes([123]) # doctest: +SKIP + 100 """ name = type(o).__module__ + '.' + type(o).__name__