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:
THIS IS ANOTHER WAY OF DOING BLOBS: base64 encoding, e.g. a string like
64'cGxlYXN1cmUu'
is a base-64-encoded BLOB (the64'...'
bit is my representation).regex from https://stackoverflow.com/questions/475074
better one from https://www.perlmonks.org/?node_id=775820
- 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 returnsdefault
ifx
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:
SPECIAL HANDLING for BLOBs: a string like
X'01FF'
means a hex-encoded BLOB. Titanium is rubbish at BLOBs, so we encode them as special string literals.SQLite uses this notation: https://sqlite.org/lang_expr.html
Strip off the start and end and convert it to a byte array: https://stackoverflow.com/questions/5649407