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#
| Attribute | Type | Description |
|---|---|---|
big_decimal_from_string | Transformation[big_decimal.BigDecimal, str] | A |
big_int_from_string | Transformation[int, str] | JavaScript's |
byte_size_from_big_int | Transformation[byte_size.ByteSize, int] | A |
byte_size_from_number | Transformation[byte_size.ByteSize, float] | A |
byte_size_from_string | Transformation[byte_size.ByteSize, str] | A |
bytes_from_base64_string | Transformation[bytes, str] | Bytes and their Base64 text (Effect's |
date_time_utc_from_string | Transformation[datetime, str] | A UTC |
date_time_zoned_from_string | Transformation[datetime, str] | A zoned |
number_from_string | Transformation[float, str] | JavaScript's |
string_from_base64_string | Transformation[str, str] | |
string_from_base64_url_string | Transformation[str, str] | |
string_from_hex_string | Transformation[str, str] | |
string_from_uri_component | Transformation[str, str] | |
time_zone_from_string | Transformation[tzinfo, str] | A zone and its text: an IANA identifier or an offset like |
time_zone_named_from_string | Transformation[ZoneInfo, str] | A named zone and its IANA identifier (Effect's |
time_zone_offset_from_number | Transformation[timezone, int] | A fixed-offset zone and its offset in milliseconds (Effect's
|
url_from_string | Transformation[Url, str] | A URL and its text (Effect's |
duration_from_iso_string | Transformation[Duration, str] | A |
duration_from_millis | Transformation[Duration, float] | A |
duration_from_nanos | Transformation[Duration, int] | A |
duration_from_string | Transformation[Duration, str] | A |
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.
| Parameter | Type | Description |
|---|---|---|
reviver | JsonReviver | None = None | |
replacer | JsonReplacer | None = None | |
space | int | 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.
| Parameter | Type | Description |
|---|---|---|
separator | str = ',' | |
key_value_separator | str = '=' | |
trim | bool = False | |
keep_empty_values | bool = 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).
| Parameter | Type | Description |
|---|---|---|
value | object |
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 _PASSTHROUGHpassthrough_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 _PASSTHROUGHpassthrough_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 _PASSTHROUGHtransform#
transform[T2, E2](*, decode, encode) -> Transformation[T2, E2]A function each way (Effect's transform).
| Parameter | Type | Description |
|---|---|---|
decode | Callable[[E2], T2] | |
encode | Callable[[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).
| Parameter | Type | Description |
|---|---|---|
decode | Callable[[E2, ParseOptions], Outcome[T2, RD]] | |
encode | Callable[[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).
| Parameter | Type | Description |
|---|---|---|
decode | Callable[[Option[E2]], Option[T2]] | |
encode | Callable[[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))