cardinal_pythonlib.convert


Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).

This file is part of cardinal_pythonlib.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


Miscellaneous other conversions.

cardinal_pythonlib.convert.base64_64format_decode(s: str) bytes | None[source]

Reverse base64_64format_encode().

Original purpose and notes:

cardinal_pythonlib.convert.base64_64format_encode(v: bytes) str[source]

Encode in 64'{base64encoded}' format.

Example:

base64_64format_encode(b"hello") == "64'aGVsbG8='"
cardinal_pythonlib.convert.convert_attrs_to_bool(obj: Any, attrs: Iterable[str], default: bool | None = None) None[source]

Applies convert_to_bool() to the specified attributes of an object, modifying it in place.

cardinal_pythonlib.convert.convert_attrs_to_int(obj: Any, attrs: Iterable[str], default: int | None = None) None[source]

Applies convert_to_int() to the specified attributes of an object, modifying it in place.

cardinal_pythonlib.convert.convert_attrs_to_lowercase(obj: Any, attrs: Iterable[str]) None[source]

Converts the specified attributes of an object to lower case, modifying the object in place.

cardinal_pythonlib.convert.convert_attrs_to_uppercase(obj: Any, attrs: Iterable[str]) None[source]

Converts the specified attributes of an object to upper case, modifying the object in place.

cardinal_pythonlib.convert.convert_to_bool(x: Any, default: bool | None = None) bool[source]

Transforms its input to a bool (or returns default if x is falsy but not itself a boolean). Accepts various common string versions.

cardinal_pythonlib.convert.convert_to_int(x: Any, default: int | None = None) int[source]

Transforms its input into an integer, or returns default.

cardinal_pythonlib.convert.hex_xformat_decode(s: str) bytes | None[source]

Reverse hex_xformat_encode().

The parameter is a hex-encoded BLOB like

"X'CDE7A24B1A9DBA3148BCB7A0B9DA5BB6A424486C'"

Original purpose and notes:

cardinal_pythonlib.convert.hex_xformat_encode(v: bytes) str[source]

Encode its input in X'{hex}' format.

Example:

special_hex_encode(b"hello") == "X'68656c6c6f'"