MCPcopy Index your code
hub / github.com/danielgtaylor/python-betterproto / _preprocess_single

Function _preprocess_single

src/betterproto/__init__.py:385–417  ·  view source on GitHub ↗

Adjusts values before serialization.

(proto_type: str, wraps: str, value: Any)

Source from the content-addressed store, hash-verified

383
384
385def _preprocess_single(proto_type: str, wraps: str, value: Any) -> bytes:
386 """Adjusts values before serialization."""
387 if proto_type in (
388 TYPE_ENUM,
389 TYPE_BOOL,
390 TYPE_INT32,
391 TYPE_INT64,
392 TYPE_UINT32,
393 TYPE_UINT64,
394 ):
395 return encode_varint(value)
396 elif proto_type in (TYPE_SINT32, TYPE_SINT64):
397 # Handle zig-zag encoding.
398 return encode_varint(value << 1 if value >= 0 else (value << 1) ^ (~0))
399 elif proto_type in FIXED_TYPES:
400 return struct.pack(_pack_fmt(proto_type), value)
401 elif proto_type == TYPE_STRING:
402 return value.encode("utf-8")
403 elif proto_type == TYPE_MESSAGE:
404 if isinstance(value, datetime):
405 # Convert the `datetime` to a timestamp message.
406 value = _Timestamp.from_datetime(value)
407 elif isinstance(value, timedelta):
408 # Convert the `timedelta` to a duration message.
409 value = _Duration.from_timedelta(value)
410 elif wraps:
411 if value is None:
412 return b""
413 value = _get_wrapper(wraps)(value=value)
414
415 return bytes(value)
416
417 return value
418
419
420def _len_preprocessed_single(proto_type: str, wraps: str, value: Any) -> int:

Callers 3

_serialize_singleFunction · 0.85
dumpMethod · 0.85
__len__Method · 0.85

Calls 5

encode_varintFunction · 0.85
_pack_fmtFunction · 0.85
_get_wrapperFunction · 0.85
from_datetimeMethod · 0.80
from_timedeltaMethod · 0.80

Tested by

no test coverage detected