GitHub

transformation

API reference for transformation.

Transformations (Effect's SchemaTransformation): a decoding getter and an encoding one, which S.decode_to puts between two schemas::

from pyfect.schema import transformation as T

S.String.pipe(S.decode_to(S.Number, T.number_from_string))
T.transform(decode=int, encode=str)

A Middleware instead takes the source's whole parse, failures included (S.middleware_decoding, S.catch_decoding).

Values

AttributeTypeDescription
big_decimal_from_stringTransformation[big_decimal.BigDecimal, str]

A BigDecimal and its text (Effect's bigDecimalFromString).

big_int_from_stringTransformation[int, str]

JavaScript's BigInt(text) and String(bigint).

byte_size_from_big_intTransformation[byte_size.ByteSize, int]

A ByteSize and its count, an int.

byte_size_from_numberTransformation[byte_size.ByteSize, float]

A ByteSize and its count as a number (a safe integer).

byte_size_from_stringTransformation[byte_size.ByteSize, str]

A ByteSize and its text, "1 byte" / "<n> bytes".

bytes_from_base64_stringTransformation[bytes, str]

Bytes and their Base64 text (Effect's uint8ArrayFromBase64String).

date_time_utc_from_stringTransformation[datetime, str]

A UTC datetime and its ISO 8601 text, in milliseconds as DateTime.formatIso (Effect's dateTimeUtcFromString).

date_time_zoned_from_stringTransformation[datetime, str]

A zoned datetime and its RFC 9557 text (Effect's dateTimeZonedFromString).

number_from_stringTransformation[float, str]

JavaScript's Number(text) and String(number).

string_from_base64_stringTransformation[str, str]
string_from_base64_url_stringTransformation[str, str]
string_from_hex_stringTransformation[str, str]
string_from_uri_componentTransformation[str, str]
time_zone_from_stringTransformation[tzinfo, str]

A zone and its text: an IANA identifier or an offset like +03:00 (Effect's timeZoneFromString).

time_zone_named_from_stringTransformation[ZoneInfo, str]

A named zone and its IANA identifier (Effect's timeZoneNamedFromString).

time_zone_offset_from_numberTransformation[timezone, int]

A fixed-offset zone and its offset in milliseconds (Effect's timeZoneOffsetFromNumber); the offset is strictly inside a day.

url_from_stringTransformation[Url, str]

A URL and its text (Effect's urlFromString).

duration_from_iso_stringTransformation[Duration, str]

A Duration from an ISO 8601 duration ("PT1H30M", "P1W2D"); years and months, which have no fixed length, are rejected. Encodes with days, hours, minutes and seconds.

duration_from_millisTransformation[Duration, float]

A Duration from a count of milliseconds (a fraction rounds to whole nanoseconds).

duration_from_nanosTransformation[Duration, int]

A Duration from a count of nanoseconds.

duration_from_stringTransformation[Duration, str]

A Duration from Effect's Duration text ("5 seconds").

Classes

Functions

capitalize

capitalize() -> Transformation[str, str]

Returns Transformation[str, str]

def capitalize() -> Transformation[str, str]:
    return _decoded_only(capitalize_getter())

from_json_string

from_json_string(*, reviver=None, replacer=None, space=None) -> Transformation[object, str]

A JSON value and its text: parsed with reviver, written through replacer and indented by space (Effect's fromJsonString). With a reviver or replacer the text's content no longer has the decoded side's schema, so JSON Schema says only its media type.

ParameterTypeDescription
reviverJsonReviver | None = None
replacerJsonReplacer | None = None
spaceint | str | None = None

Returns Transformation[object, str]

def from_json_string(
    *,
    reviver: JsonReviver | None = None,
    replacer: JsonReplacer | None = None,
    space: int | str | None = None,
) -> Transformation[object, str]:
    """A JSON value and its text: parsed with ``reviver``, written through
    ``replacer`` and indented by ``space`` (Effect's ``fromJsonString``).
    With a ``reviver`` or ``replacer`` the text's content no longer has the
    decoded side's schema, so JSON Schema says only its media type."""
    decode = parse_json() if reviver is None else parse_json(reviver=reviver)
    content = Content("application/json", preserve_schema=reviver is None and replacer is None)
    return Transformation(decode, stringify_json(replacer=replacer, space=space), content)

option_from_null_or

option_from_null_or[T]() -> Transformation[Option[T], T | None]

None is an empty Option; a value is Some. Python's one None makes the decoded side Some(value) or None.

Returns Transformation[Option[T], T | None]

def option_from_null_or[T]() -> Transformation[Option[T], T | None]:
    """``None`` is an empty ``Option``; a value is ``Some``. Python's one
    ``None`` makes the decoded side ``Some(value)`` or ``None``."""
    return transform(decode=_some_unless_none, encode=_value_or_none)

option_from_optional_key

option_from_optional_key[T]() -> Transformation[Option[T], T]

A missing key is None, a present one Some(value); encoding leaves the key out for None (Effect's optionFromOptionalKey).

Returns Transformation[Option[T], T]

def option_from_optional_key[T]() -> Transformation[Option[T], T]:
    """A missing key is ``None``, a present one ``Some(value)``; encoding
    leaves the key out for ``None`` (Effect's ``optionFromOptionalKey``)."""
    return Transformation(transform_optional_getter(Some), transform_optional_getter(_flatten))

snake_to_camel

snake_to_camel() -> Transformation[str, str]

snake_case to camelCase, and back.

Returns Transformation[str, str]

def snake_to_camel() -> Transformation[str, str]:
    """``snake_case`` to ``camelCase``, and back."""
    return Transformation(snake_to_camel_getter(), camel_to_snake())

split_key_value

split_key_value(*, separator=',', key_value_separator='=', trim=False, keep_empty_values=False) -> Transformation[dict[str, str], str]

"a=1,b=2" and {"a": "1", "b": "2"}; trim and keep_empty_values as G.split_key_value. It round-trips when keys and values hold no separator and, with trim, no surrounding whitespace.

ParameterTypeDescription
separatorstr = ','
key_value_separatorstr = '='
trimbool = False
keep_empty_valuesbool = False

Returns Transformation[dict[str, str], str]

def split_key_value(
    *,
    separator: str = ",",
    key_value_separator: str = "=",
    trim: bool = False,
    keep_empty_values: bool = False,
) -> Transformation[dict[str, str], str]:
    """``"a=1,b=2"`` and ``{"a": "1", "b": "2"}``; ``trim`` and ``keep_empty_values`` as
    ``G.split_key_value``. It round-trips when keys and values hold no separator and,
    with ``trim``, no surrounding whitespace."""
    split = split_key_value_getter(
        separator=separator,
        key_value_separator=key_value_separator,
        trim=trim,
        keep_empty_values=keep_empty_values,
    )
    join = join_key_value(separator=separator, key_value_separator=key_value_separator)
    return Transformation(split, join)

to_lower_case

to_lower_case() -> Transformation[str, str]

Returns Transformation[str, str]

def to_lower_case() -> Transformation[str, str]:
    return _decoded_only(to_lower_case_getter())

to_upper_case

to_upper_case() -> Transformation[str, str]

Returns Transformation[str, str]

def to_upper_case() -> Transformation[str, str]:
    return _decoded_only(to_upper_case_getter())

trim

trim() -> Transformation[str, str]

Trims when decoding, keeps the value when encoding.

Returns Transformation[str, str]

def trim() -> Transformation[str, str]:
    """Trims when decoding, keeps the value when encoding."""
    return _decoded_only(trim_getter())

uncapitalize

uncapitalize() -> Transformation[str, str]

Returns Transformation[str, str]

def uncapitalize() -> Transformation[str, str]:
    return _decoded_only(uncapitalize_getter())

is_transformation

is_transformation(value) -> TypeIs[Transformation[Any, Any, Any, Any]]

value is a Transformation (Effect's isTransformation).

ParameterTypeDescription
valueobject

Returns TypeIs[Transformation[Any, Any, Any, Any]]

def is_transformation(value: object) -> TypeIs[Transformation[Any, Any, Any, Any]]:
    """``value`` is a ``Transformation`` (Effect's ``isTransformation``)."""
    return isinstance(value, Transformation)

passthrough

passthrough[T2]() -> Transformation[T2, T2]

The value unchanged each way (Effect's passthrough).

Returns Transformation[T2, T2]

def passthrough[T2]() -> Transformation[T2, T2]:
    """The value unchanged each way (Effect's ``passthrough``)."""
    return _PASSTHROUGH

passthrough_subtype

passthrough_subtype() -> Transformation[Any, Any]

The value unchanged each way, from a subtype E to its supertype T (Effect's passthroughSubtype).

Returns Transformation[Any, Any]

def passthrough_subtype() -> Transformation[Any, Any]:
    """The value unchanged each way, from a subtype ``E`` to its supertype
    ``T`` (Effect's ``passthroughSubtype``)."""
    return _PASSTHROUGH

passthrough_supertype

passthrough_supertype() -> Transformation[Any, Any]

The value unchanged each way, from a supertype E to its subtype T (Effect's passthroughSupertype). Python cannot say that T is a subtype of E, so it is typed Any.

Returns Transformation[Any, Any]

def passthrough_supertype() -> Transformation[Any, Any]:
    """The value unchanged each way, from a supertype ``E`` to its subtype
    ``T`` (Effect's ``passthroughSupertype``). Python cannot say that ``T``
    is a subtype of ``E``, so it is typed ``Any``."""
    return _PASSTHROUGH

transform

transform[T2, E2](*, decode, encode) -> Transformation[T2, E2]

A function each way (Effect's transform).

ParameterTypeDescription
decodeCallable[[E2], T2]
encodeCallable[[T2], E2]

Returns Transformation[T2, E2]

def transform[T2, E2](
    *, decode: Callable[[E2], T2], encode: Callable[[T2], E2]
) -> Transformation[T2, E2]:
    """A function each way (Effect's ``transform``)."""
    return Transformation(transform_getter(decode), transform_getter(encode))

transform_effect

transform_effect[T2, E2, RD = Never, RE = Never](*, decode, encode) -> Transformation[T2, E2, RD, RE]

An effectful function each way, failing with an Issue (Effect's transformEffect).

ParameterTypeDescription
decodeCallable[[E2, ParseOptions], Outcome[T2, RD]]
encodeCallable[[T2, ParseOptions], Outcome[E2, RE]]

Returns Transformation[T2, E2, RD, RE]

def transform_effect[T2, E2, RD = Never, RE = Never](
    *,
    decode: Callable[[E2, ParseOptions], Outcome[T2, RD]],
    encode: Callable[[T2, ParseOptions], Outcome[E2, RE]],
) -> Transformation[T2, E2, RD, RE]:
    """An effectful function each way, failing with an ``Issue`` (Effect's
    ``transformEffect``)."""
    return Transformation(transform_effect_getter(decode), transform_effect_getter(encode))

transform_optional

transform_optional[T2, E2](*, decode, encode) -> Transformation[T2, E2]

A function of an Option each way (Effect's transformOptional).

ParameterTypeDescription
decodeCallable[[Option[E2]], Option[T2]]
encodeCallable[[Option[T2]], Option[E2]]

Returns Transformation[T2, E2]

def transform_optional[T2, E2](
    *,
    decode: Callable[[Option[E2]], Option[T2]],
    encode: Callable[[Option[T2]], Option[E2]],
) -> Transformation[T2, E2]:
    """A function of an ``Option`` each way (Effect's ``transformOptional``)."""
    return Transformation(transform_optional_getter(decode), transform_optional_getter(encode))