Skip to content

Commit

Permalink
add support for child custom delegates
Browse files Browse the repository at this point in the history
  • Loading branch information
hgromer committed Apr 3, 2021
1 parent 62c85d3 commit 7bff385
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pymarshaler/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.2.1'
__version__ = '0.2.2'
__all__ = ['Marshal', 'utils', 'arg_delegates', 'errors']

from pymarshaler.marshal import Marshal
Expand Down
22 changes: 12 additions & 10 deletions pymarshaler/marshal.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ def __init__(self):
self.registered_delegates = {}

def register(self, cls, delegate: ArgBuilderDelegate):
self.registered_delegates[cls.__name__] = delegate
self.registered_delegates[cls] = delegate

def get(self, cls):
return self.registered_delegates[cls.__name__]

def contains(self, cls):
def get_for(self, cls):
try:
return cls.__name__ in self.registered_delegates
except AttributeError:
return False
for val in self.registered_delegates:
if cls == val or issubclass(cls, val):
return self.registered_delegates[val]
return None
except TypeError:
return None


class _ArgBuilderFactory:
Expand All @@ -53,8 +53,10 @@ def register(self, cls, delegate_cls):
self._registered_delegates.register(cls, delegate_cls(cls))

def get_delegate(self, cls) -> ArgBuilderDelegate:
if self._registered_delegates.contains(cls):
return self._registered_delegates.get(cls)
cls_maybe = self._registered_delegates.get_for(cls)

if cls_maybe:
return cls_maybe
elif is_user_defined(cls):
return self._default_arg_builder_delegates['UserDefined'](cls)
elif '_name' in cls.__dict__:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setuptools.setup(
name="pymarshaler",
version="0.2.1",
version="0.2.2",
author="Hernan Romer",
author_email="[email protected]",
description="Package to marshal and unmarshal python objects",
Expand Down
6 changes: 6 additions & 0 deletions tests/test_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ def __init__(self):
pass


class ChildWithCustomDelegate(ClassWithCustomDelegate):

def __init__(self):
super().__init__()


class CustomNoneDelegate(ArgBuilderDelegate):

def __init__(self, cls):
Expand Down
6 changes: 6 additions & 0 deletions tests/test_marshaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ def test_custom_delegate(self):
result = marshal.unmarshal(ClassWithCustomDelegate, {})
self.assertEqual(result, ClassWithCustomDelegate())

@timed
def test_child_custom_delegate(self):
marshal.register_delegate(ClassWithCustomDelegate, CustomNoneDelegate)
result = marshal.unmarshal(ChildWithCustomDelegate, {})
self.assertEqual(result, ChildWithCustomDelegate())

@timed
def test_nested_lists(self):
nested_lists = NestedList([[Inner("Inner_1", 1)], [Inner("Inner_2", 2)]])
Expand Down

0 comments on commit 7bff385

Please sign in to comment.