Skip to content

Commit

Permalink
Re-added ability to call len on serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilnaa committed Jan 7, 2020
1 parent c1596c2 commit 6c3f073
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
6 changes: 6 additions & 0 deletions hydras/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ def is_constant_size(cls) -> bool:
def byte_size(cls) -> int:
return cls.__hydras_metadata__.size

def __len__(self):
return self.byte_size

@property
def __hydras_metadata__(cls) -> SerializerMetadata:
return getattr(cls, cls.METAATTR)
Expand All @@ -130,6 +133,9 @@ def byte_size(self) -> int:
def __hydras_metadata__(self) -> SerializerMetadata:
return type(self).__hydras_metadata__

def __len__(self):
return self.byte_size

def __init__(self, default_value, validator: Callable[[Any], bool] = None):
"""
Creates a new formatter.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
long_description = f.read()

setup(name='Hydras',
version='3.0.8',
version='3.0.9',
description='A module for constructions of structured binary packets.',
author='Gilad Naaman',
author_email='gilad@naaman.io',
Expand Down
8 changes: 8 additions & 0 deletions tests/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ class ComplicatedStruct(Struct):
class StructTests(HydrasTestCase):
""" A testcase checking for a few of `Struct`'s features. """

def test_member_length(self):
self.assertEqual(len(SimpleStruct.b_first_variable), 1)
self.assertEqual(len(SimpleStruct.a_second_variable), 2)
self.assertEqual(len(SimpleStruct.x_third_variable), 1)

self.assertEqual(len(ComplicatedStruct.other_struct), 1)
self.assertEqual(len(ComplicatedStruct.some_field), 12)

def test_serialize_simple(self):
""" Test serialization of a simple struct. """
obj = SimpleStruct()
Expand Down

0 comments on commit 6c3f073

Please sign in to comment.