SnakeBridge.Bytes (SnakeBridge v0.7.4)

View Source

Wrapper struct for binary data that should be sent to Python as bytes, not str.

By default, SnakeBridge encodes UTF-8 valid Elixir binaries as Python strings. Use this wrapper when you need to explicitly send data as Python bytes.

Examples

# Hash a string as bytes
{:ok, hash} = SnakeBridge.call("hashlib", "md5", [SnakeBridge.bytes("abc")])

# Base64 encode
{:ok, encoded} = SnakeBridge.call("base64", "b64encode", [SnakeBridge.bytes("hello")])

# Binary protocol data
{:ok, _} = SnakeBridge.call("struct", "pack", [">I", 42])

When to Use

Use SnakeBridge.bytes/1 when calling Python functions that:

  • Require bytes input (hashlib, cryptography, struct, etc.)
  • Work with binary protocols
  • Process raw byte data

Wire Format

Encoded as:

{"__type__": "bytes", "__schema__": 1, "data": "<base64-encoded>"}

Summary

Functions

Returns the raw binary data from a Bytes wrapper.

Creates a Bytes wrapper from binary data.

Types

t()

@type t() :: %SnakeBridge.Bytes{data: binary()}

Functions

data(bytes)

@spec data(t()) :: binary()

Returns the raw binary data from a Bytes wrapper.

Examples

iex> bytes = SnakeBridge.Bytes.new("hello")
iex> SnakeBridge.Bytes.data(bytes)
"hello"

new(data)

@spec new(binary()) :: t()

Creates a Bytes wrapper from binary data.

Examples

iex> SnakeBridge.Bytes.new("hello")
%SnakeBridge.Bytes{data: "hello"}

iex> SnakeBridge.Bytes.new(<<0, 1, 2, 255>>)
%SnakeBridge.Bytes{data: <<0, 1, 2, 255>>}