Skip to content

Commit

Permalink
Change serialize to serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
csaroff committed Apr 19, 2023
1 parent a73399c commit 3f78bbc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
22 changes: 11 additions & 11 deletions rele/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ def __init__(
suffix="",
filter_by=None,
backend_filter_by=None,
deserialize=lambda x: json.loads(x.data.decode("utf-8")),
deserializer=lambda x: json.loads(x.data.decode("utf-8")),
):
self._func = func
self.topic = topic
self._prefix = prefix
self._suffix = suffix
self._filters = self._init_filters(filter_by)
self.backend_filter_by = backend_filter_by
self._deserialize = self._init_deserialize(deserialize)
self._deserializer = self._init_deserializer(deserializer)

def _init_filters(self, filter_by):
if filter_by and not (
Expand All @@ -69,10 +69,10 @@ def _init_filters(self, filter_by):

return None

def _init_deserialize(self, deserialize):
if not callable(deserialize):
raise ValueError("deserialize must be a callable or None.")
return deserialize
def _init_deserializer(self, deserializer):
if not callable(deserializer):
raise ValueError("deserializer must be a callable or None.")
return deserializer

@property
def name(self):
Expand Down Expand Up @@ -122,7 +122,7 @@ def __call__(self, message):
start_time = time.time()

try:
data = self._subscription._deserialize(message)
data = self._subscription._deserializer(message)
except Exception as e:
message.ack()
run_middleware_hook(
Expand Down Expand Up @@ -164,7 +164,7 @@ def sub(
suffix=None,
filter_by=None,
backend_filter_by=None,
deserialize=lambda x: json.loads(x.data.decode("utf-8")),
deserializer=lambda x: json.loads(x.data.decode("utf-8")),
):
"""Decorator function that makes declaring a PubSub Subscription simple.
Expand Down Expand Up @@ -201,7 +201,7 @@ def purpose_2(data, **kwargs):
def sub_process_landscape_photos(data, **kwargs):
pass
@sub(topic='string-messages', deserialize=lambda x: x.data.decode('utf-8'))
@sub(topic='string-messages', deserializer=lambda x: x.data.decode('utf-8'))
def sub_process_non_json(data, **kwargs):
pass
Expand All @@ -215,7 +215,7 @@ def sub_process_non_json(data, **kwargs):
:param filter_by: Union[function, list] An optional function or tuple of
functions that filters the messages to be processed by
the sub regarding their attributes.
:param deserialize: function An optional deserialization function that
:param deserializer: function An optional deserialization function that
replaces the default json deserialization.
:return: :class:`~rele.subscription.Subscription`
"""
Expand All @@ -241,7 +241,7 @@ def decorator(func):
suffix=suffix,
filter_by=filter_by,
backend_filter_by=backend_filter_by,
deserialize=deserialize,
deserializer=deserializer,
)

return decorator
6 changes: 3 additions & 3 deletions tests/test_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ def sub_process_landscape_gif_photos(data, **kwargs):
return f'Received a {kwargs.get("format")} photo of type {kwargs.get("type")}'


def deserialize_handler(message):
def deserializer_handler(message):
return message.data.decode("utf-8")


@sub(topic="string-data", deserialize=deserialize_handler)
@sub(topic="string-data", deserializer=deserializer_handler)
def sub_string_deserializer(data, **kwargs):
return data

Expand Down Expand Up @@ -303,7 +303,7 @@ def crashy_sub_stub(data, **kwargs):
}
assert failed_log.subscription_message == str(message_wrapper)

def test_deserialize_handler(self, message_wrapper_invalid_json):
def test_deserializer_handler(self, message_wrapper_invalid_json):
callback = Callback(sub_string_deserializer)
res = callback(message_wrapper_invalid_json)

Expand Down

0 comments on commit 3f78bbc

Please sign in to comment.