aas_core3_2.common

Provide common functions shared among the modules.

aas_core3_2.common.assert_never(value: NoReturn) → NoReturn[source]

Signal to mypy to perform an exhaustive matching.

Please see the following page for more details: https://hakibenita.com/python-mypy-exhaustive-checking

aas_core3_2.common.try_to_cast_to_array_like(value: Any) → Optional[Sequence[Any]][source]

Try to cast value to something like a JSON array.

A mapping and a set are refused, as neither is a JSON array, and so are a str, a bytes and a bytearray, which Python counts among the sequences but JSON does not.

Only a sequence is admitted, and never an arbitrary iterable. A one-shot iterable such as a generator is spent by the first walk over it, so a verification followed by a serialization would silently give an empty array – this function is the single place where all the modules agree on what an array is, so the refusal has to hold for all of them.

>>> assert try_to_cast_to_array_like(True) is None
>>> assert try_to_cast_to_array_like(0) is None
>>> assert try_to_cast_to_array_like(2.2) is None
>>> assert try_to_cast_to_array_like("hello") is None
>>> assert try_to_cast_to_array_like(b"hello") is None
>>> try_to_cast_to_array_like([1, 2])
[1, 2]
>>> assert try_to_cast_to_array_like({"a": 3}) is None
>>> assert try_to_cast_to_array_like(collections.OrderedDict()) is None
>>> try_to_cast_to_array_like(range(1, 2))
range(1, 2)
>>> try_to_cast_to_array_like((1, 2))
(1, 2)
>>> assert try_to_cast_to_array_like({1, 2, 3}) is None
>>> assert try_to_cast_to_array_like(iter([1, 2])) is None
aas_core3_2.common.parse_safe_int(text: str) → int[source]

Parse text as a safe integer.

The meta-model calls int on strings, and this is its transpilation. We are stricter than the built-in int() so that all the SDKs behave the same. We accept only an optional sign followed by the ASCII digits, and only the safe integers, i.e., the integers within -(2 ** 53 - 1) and 2 ** 53 - 1, which a double-precision floating-point number represents exactly.

Parameters

text – to be parsed

Returns

parsed integer

Raise

ValueError if text is not a safe integer

>>> parse_safe_int("-0042")
-42
aas_core3_2.common.try_to_convert_int_to_float(value: int) → Optional[float][source]

Try to convert value to the float which JSON prescribes.

JSON knows a single numeric type, so an int has to become a float on its way into and out of a JSON-able value. The conversion has to be exact: a float which does not convert back to value would quietly say a different number than the one which was given, so such a value is refused instead of rounded.

Mind that a bool is an int in Python. The callers all sort out the booleans before they come here.

>>> try_to_convert_int_to_float(1)
1.0
>>> try_to_convert_int_to_float(-3)
-3.0
>>> assert try_to_convert_int_to_float(2 ** 53 + 1) is None
>>> assert try_to_convert_int_to_float(10 ** 400) is None