reduce docs

This commit is contained in:
2026-03-09 15:41:24 +07:00
parent c896af234d
commit 3fcd27f41a
5 changed files with 320 additions and 1650 deletions

View File

@@ -71,8 +71,9 @@ def _serialize_data(data: Any, payload_type: str) -> bytes:
Args:
data: Data to serialize (string for "text", JSON-serializable for "dictionary",
table-like for "table", binary for "image", "audio", "video", "binary")
payload_type: Target format: "text", "dictionary", "table", "image", "audio", "video", "binary"
table-like for "arrowtable"/"jsontable", binary for "image", "audio", "video", "binary")
payload_type: Target format: "text", "dictionary", "arrowtable", "jsontable",
"image", "audio", "video", "binary"
Returns:
Binary representation of the serialized data
@@ -80,7 +81,8 @@ def _serialize_data(data: Any, payload_type: str) -> bytes:
Raises:
Error: If payload_type is not one of the supported types
Error: If payload_type is "image", "audio", or "video" but data is not bytes
Error: If payload_type is "table" but data is not a pandas DataFrame or pyarrow Table
Error: If payload_type is "arrowtable" but data is not a pandas DataFrame or pyarrow Table
Error: If payload_type is "jsontable" but data is not a list of dicts
"""
if payload_type == 'text':
if isinstance(data, str):
@@ -90,9 +92,9 @@ def _serialize_data(data: Any, payload_type: str) -> bytes:
elif payload_type == 'dictionary':
json_str = json.dumps(data)
return json_str.encode('utf-8')
elif payload_type == 'table':
elif payload_type == 'arrowtable':
if not ARROW_AVAILABLE:
raise RuntimeError('pyarrow not available for table serialization')
raise RuntimeError('pyarrow not available for arrowtable serialization')
import io
buf = io.BytesIO()
@@ -110,7 +112,14 @@ def _serialize_data(data: Any, payload_type: str) -> bytes:
sink.close()
return buf.getvalue()
else:
raise ValueError('Table data must be a pandas DataFrame or pyarrow Table')
raise ValueError('Arrow table data must be a pandas DataFrame or pyarrow Table')
elif payload_type == 'jsontable':
# Serialize list of dicts to JSON format
if isinstance(data, list) and all(isinstance(row, dict) for row in data):
json_str = json.dumps(data)
return json_str.encode('utf-8')
else:
raise ValueError('JSON table data must be a list of dicts')
elif payload_type == 'image':
if isinstance(data, (bytes, bytearray)):
return bytes(data)
@@ -141,12 +150,13 @@ def _deserialize_data(data: bytes, payload_type: str, correlation_id: str) -> An
Args:
data: Serialized data as bytes
payload_type: Data type ("text", "dictionary", "table", "image", "audio", "video", "binary")
payload_type: Data type ("text", "dictionary", "arrowtable", "jsontable",
"image", "audio", "video", "binary")
correlation_id: Correlation ID for logging
Returns:
Deserialized data (String for "text", DataFrame for "table", JSON data for "dictionary",
bytes for "image", "audio", "video", "binary")
Deserialized data (String for "text", DataFrame for "arrowtable",
Vector{Dict} for "jsontable"/"dictionary", bytes for "image", "audio", "video", "binary")
Raises:
Error: If payload_type is not one of the supported types
@@ -156,14 +166,18 @@ def _deserialize_data(data: bytes, payload_type: str, correlation_id: str) -> An
elif payload_type == 'dictionary':
json_str = data.decode('utf-8')
return json.loads(json_str)
elif payload_type == 'table':
elif payload_type == 'arrowtable':
if not ARROW_AVAILABLE:
raise RuntimeError('pyarrow not available for table deserialization')
raise RuntimeError('pyarrow not available for arrowtable deserialization')
import io
buf = io.BytesIO(data)
reader = ipc.open_file(buf)
return reader.read_all().to_pandas()
elif payload_type == 'jsontable':
# Deserialize JSON to list of dicts
json_str = data.decode('utf-8')
return json.loads(json_str)
elif payload_type == 'image':
return data
elif payload_type == 'audio':
@@ -317,7 +331,7 @@ class NATSClient:
self._client = nats.connect(self.url)
await self._client
else:
raise Error('nats-py not available')
raise RuntimeError('nats-py not available')
return self._client
async def publish(self, subject: str, message: str, correlation_id: str = "") -> None:
@@ -397,12 +411,19 @@ def _build_payload(
Returns:
Payload object
"""
# Determine encoding based on payload type (matching Julia/JS implementation)
encoding = 'base64'
if payload_type == 'jsontable':
encoding = 'json'
elif payload_type == 'arrowtable':
encoding = 'arrow-ipc'
return {
'id': str(uuid.uuid4()),
'dataname': dataname,
'payload_type': payload_type,
'transport': transport,
'encoding': 'base64' if transport == 'direct' else 'none',
'encoding': encoding,
'size': len(payload_bytes),
'data': data,
'metadata': {'payload_bytes': len(payload_bytes)} if transport == 'direct' else {}
@@ -476,7 +497,7 @@ async def smartsend(
data: List of (dataname, data, type) tuples to send
- dataname: Name of the payload
- data: The actual data to send
- type: Payload type: "text", "dictionary", "table", "image", "audio", "video", "binary"
- type: Payload type: "text", "dictionary", "arrowtable", "jsontable", "image", "audio", "video", "binary"
broker_url: URL of the NATS server
fileserver_url: URL of the HTTP file server for large payloads
fileserver_upload_handler: Function to handle fileserver uploads (must return Dict with "status",
@@ -514,14 +535,21 @@ async def smartsend(
>>> data2 = [1, 2, 3, 4, 5]
>>> env, env_json_str = await smartsend(
... "my.subject",
... [("dataname1", data1, "dictionary"), ("dataname2", data2, "table")]
... [("dataname1", data1, "dictionary"), ("dataname2", data2, "arrowtable")]
... )
>>>
>>> # Send a large array using fileserver upload
>>> data = list(range(10_000_000)) # ~80 MB
>>> env, env_json_str = await smartsend(
... "large.data",
... [("large_table", data, "table")]
... [("large_table", data, "arrowtable")]
... )
>>>
>>> # Send jsontable (JSON format for human-readable tabular data)
>>> users = [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]
>>> env, env_json_str = await smartsend(
... "json.data",
... [("users", users, "jsontable")]
... )
>>>
>>> # Mixed content (e.g., chat with text and image)