Skip to content

Commit

Permalink
Correct sizeof implementation (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
borgstrom authored Jan 6, 2018
1 parent a822765 commit cd63e32
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ python:
- 2.7
- 3.5
- 3.6
- pypy

install: false
script:
Expand Down
17 changes: 8 additions & 9 deletions src/kinesis/producer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,19 @@ def sizeof(obj, seen=None):

size = sys.getsizeof(obj)

# since strings are containers we return their size before we check for a container
# since strings are iterabes we return their size explicitly first
if isinstance(obj, six.string_types):
return size

if isinstance(obj, collections.Container):
return size + sum(
sizeof(item, seen)
for item in obj
)

if isinstance(obj, collections.Mapping):
elif isinstance(obj, collections.Mapping):
return size + sum(
sizeof(key, seen) + sizeof(val, seen)
for key, val in six.iteritems(obj)
)
elif isinstance(obj, collections.Iterable):
return size + sum(
sizeof(item, seen)
for item in obj
)

return size

Expand Down Expand Up @@ -116,6 +114,7 @@ def loop(self):
break

self.flush_records()
return 0

def end(self):
# At the end of our loop (before we exit, i.e. via a signal) we change our buffer time to 250ms and then re-call
Expand Down
16 changes: 15 additions & 1 deletion test/test_producer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from kinesis.producer import KinesisProducer
import sys

from kinesis.producer import KinesisProducer, sizeof


def test_producer(mocker):
Expand All @@ -16,3 +18,15 @@ def test_producer(mocker):
mocked_queue = mocker.patch.object(producer, 'queue')
producer.put('foo', explicit_hash_key='hash', partition_key='partition')
mocked_queue.put.assert_called_with(('foo', 'hash', 'partition'))


def test_sizeof():
s1 = "a"
assert sizeof(s1) == sys.getsizeof(s1)

s2 = ["a", "b", "a"]
assert sizeof(s2) == sys.getsizeof(s2) + sys.getsizeof("a") + sys.getsizeof("b")

s3 = {"a": "a", "b": "a", "c": 1}
assert sizeof(s3) == sys.getsizeof(s3) + sys.getsizeof("a") + sys.getsizeof("b") + sys.getsizeof("c") + \
sys.getsizeof(1)

0 comments on commit cd63e32

Please sign in to comment.