Serialization and deserialization between Elixir and Python.
See the Snex module documentation for more detail.
Summary
Types
Options for encoding Elixir terms to a desired representation on the Python side.
Functions
Wraps an iodata value to encode as a desired representation on the Python side.
Wraps a float value for decoding in Python.
Builds a Python object from a module, class name, and arguments. Arguments are encoded and passed to the Python object constructor.
Wraps an arbitrary Erlang term for efficient passing to Python. The value will be opaque on the Python side and decoded back to the original Erlang term when returned to Elixir.
Types
@type encoding_opt() ::
{:binary_as, :str | :bytes | :bytearray}
| {:set_as, :set | :frozenset}
| {:atom_as, :atom | :distinct_atom}
@type encoding_opts() :: [encoding_opt()]
Options for encoding Elixir terms to a desired representation on the Python side.
binary_as: The representation of binaries on the Python side. Only applies to <<binary>> values, not to values wrapped withSnex.Serde.binary/2. Defaults to:str.:strencodes binary asstr:bytesencodes binary asbytes:bytearrayencodes binary asbytearray
set_as: The representation of sets on the Python side. Defaults to:set.atom_as: The representation of atoms on the Python side. Defaults to:atom.:atomencodes atom assnex.Atom, which inherits and equalsstr:distinct_atomencodes atom assnex.DistinctAtom, which does inherit fromstrbut is not equal, i.e."foo" != snex.DistinctAtom("foo"). This helps when encoding a map with both"foo"and:fookeys
@opaque serde_binary()
@opaque serde_float()
@opaque serde_object()
@opaque serde_term()
Functions
@spec binary(iodata(), :str | :bytes | :bytearray) :: serde_binary()
Wraps an iodata value to encode as a desired representation on the Python side.
Arguments
as: The representation of the binary on the Python side. Defaults to:bytes.
@spec float(:"-inf" | :inf | :nan | float()) :: serde_float()
Wraps a float value for decoding in Python.
Since Erlang floats can't represent infinity, -infinity or NaN, this function can be used to wrap
atoms :inf, :"-inf" or :nan to decode as the corresponding Python float.
@spec object(String.t(), String.t(), list()) :: serde_object()
Builds a Python object from a module, class name, and arguments. Arguments are encoded and passed to the Python object constructor.
Example
{:ok, "date"} =
Snex.pyeval(env,
"return type(d).__name__",
%{"d" => Snex.Serde.object("datetime", "date", [2025, 12, 28])})
@spec term(term()) :: serde_term()
Wraps an arbitrary Erlang term for efficient passing to Python. The value will be opaque on the Python side and decoded back to the original Erlang term when returned to Elixir.