-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a6e31f
commit b161843
Showing
9 changed files
with
682 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
********************* | ||
Composite Serializers | ||
********************* | ||
|
||
.. automodule:: easynetwork.serializers.composite | ||
|
||
.. autoclass:: StapledPacketSerializer | ||
:members: | ||
:inherited-members: | ||
|
||
.. autoclass:: StapledIncrementalPacketSerializer | ||
:members: | ||
:inherited-members: | ||
|
||
.. autoclass:: StapledBufferedIncrementalPacketSerializer | ||
:members: | ||
:inherited-members: | ||
|
||
----- | ||
|
||
.. seealso:: | ||
|
||
:doc:`/howto/advanced/serializer_composition` | ||
Describes where and when a :term:`composite serializer` is used. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ Serializers | |
:caption: Built-In Serializers | ||
|
||
abc | ||
composite | ||
json | ||
pickle | ||
line | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ Advanced Guide | |
serializers | ||
buffered_serializers | ||
serializer_combinations | ||
serializer_composition | ||
standalone_servers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
******************************* | ||
How-to — Serializer Composition | ||
******************************* | ||
|
||
.. contents:: Table of Contents | ||
:local: | ||
|
||
------ | ||
|
||
The Basics | ||
========== | ||
|
||
A :term:`composite serializer` fulfills the same need as a :term:`composite converter`, | ||
which is to handle two disjoint formats between sent and received packet data. | ||
|
||
This is typically done using the :class:`.StapledPacketSerializer`: | ||
|
||
>>> import pickle | ||
>>> from easynetwork.serializers import * | ||
>>> from easynetwork.serializers.composite import * | ||
>>> s = StapledPacketSerializer(sent_packet_serializer=PickleSerializer(), received_packet_serializer=JSONSerializer()) | ||
>>> s.deserialize(b'{"data": 42}') | ||
{'data': 42} | ||
>>> data = s.serialize({"data": 42}) | ||
>>> pickle.loads(data) | ||
{'data': 42} | ||
|
||
:class:`.StapledPacketSerializer` will return the correct implementation according to | ||
the base class of ``sent_packet_serializer`` and ``received_packet_serializer``: | ||
|
||
>>> from easynetwork.serializers.abc import * | ||
>>> | ||
>>> StapledPacketSerializer(sent_packet_serializer=PickleSerializer(), received_packet_serializer=JSONSerializer()) | ||
StapledPacketSerializer(...) | ||
>>> isinstance(_, (AbstractIncrementalPacketSerializer, BufferedIncrementalPacketSerializer)) | ||
False | ||
>>> | ||
>>> StapledPacketSerializer(sent_packet_serializer=StringLineSerializer(), received_packet_serializer=JSONSerializer()) | ||
StapledIncrementalPacketSerializer(...) | ||
>>> isinstance(_, AbstractIncrementalPacketSerializer) | ||
True | ||
>>> | ||
>>> StapledPacketSerializer(sent_packet_serializer=JSONSerializer(), received_packet_serializer=StringLineSerializer()) | ||
StapledBufferedIncrementalPacketSerializer(...) | ||
>>> isinstance(_, BufferedIncrementalPacketSerializer) | ||
True | ||
|
||
|
||
Use Case: Different Structure Between A Request And A Response | ||
============================================================== | ||
|
||
>>> from typing import NamedTuple | ||
>>> from easynetwork.serializers import NamedTupleStructSerializer | ||
>>> from easynetwork.serializers.composite import StapledPacketSerializer | ||
>>> class Request(NamedTuple): | ||
... type: int | ||
... data: bytes | ||
... | ||
>>> class Response(NamedTuple): | ||
... rc: int | ||
... message: str | ||
... | ||
>>> s = StapledPacketSerializer( | ||
... sent_packet_serializer=NamedTupleStructSerializer(Request, {"type": "B", "data": "1024s"}, encoding=None), | ||
... received_packet_serializer=NamedTupleStructSerializer(Response, {"rc": "h", "message": "10s"}, encoding="utf8"), | ||
... ) | ||
>>> s.serialize(Request(type=42, data=b"some data to send")) | ||
b'*some data to send\x00\x00\x00\x00\x00...' | ||
>>> s.deserialize(b"\x00\xc8OK\x00\x00\x00\x00\x00\x00\x00\x00") | ||
Response(rc=200, message='OK') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.