cengal.data_manipulation.conversion.bit_cast_like.versions.v_0.bit_cast_like

 1#!/usr/bin/env python
 2# coding=utf-8
 3
 4# Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>
 5# 
 6# Licensed under the Apache License, Version 2.0 (the "License");
 7# you may not use this file except in compliance with the License.
 8# You may obtain a copy of the License at
 9# 
10#     http://www.apache.org/licenses/LICENSE-2.0
11# 
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18
19__all__ = ['bit_cast__general', 'bit_cast__float_to_uint32', 'bit_cast__uint32_to_float']
20
21
22from struct import pack, unpack
23from typing import Any
24
25
26"""
27Module Docstring
28Docstrings: http://www.python.org/dev/peps/pep-0257/
29"""
30
31__author__ = "ButenkoMS <gtalk@butenkoms.space>"
32__copyright__ = "Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>"
33__credits__ = ["ButenkoMS <gtalk@butenkoms.space>", ]
34__license__ = "Apache License, Version 2.0"
35__version__ = "4.4.1"
36__maintainer__ = "ButenkoMS <gtalk@butenkoms.space>"
37__email__ = "gtalk@butenkoms.space"
38# __status__ = "Prototype"
39__status__ = "Development"
40# __status__ = "Production"
41
42
43def bit_cast__general(data: Any, bit_cast_from: str, bit_cast_to: str) -> Any:
44    """See https://docs.python.org/3/library/struct.html
45
46    Args:
47        data (Any): _description_
48        bit_cast_from (str): see https://docs.python.org/3/library/struct.html#format-characters
49        bit_cast_to (str): see https://docs.python.org/3/library/struct.html#format-characters
50
51    Returns:
52        Any: _description_
53    """
54    return unpack(bit_cast_to, pack(bit_cast_from, data))
55
56
57def bit_cast__float_to_uint32(x: float) -> int:
58    return unpack('I', pack('f', x))
59
60
61def bit_cast__uint32_to_float(x: int) -> float:
62    return unpack('f', pack('I', x))
63
64
65def bit_cast__double_to_uint64(x: float) -> int:
66    return unpack('Q', pack('d', x))
67
68
69def bit_cast__uint64_to_double(x: int) -> float:
70    return unpack('d', pack('Q', x))
def bit_cast__general(data: typing.Any, bit_cast_from: str, bit_cast_to: str) -> Any:
44def bit_cast__general(data: Any, bit_cast_from: str, bit_cast_to: str) -> Any:
45    """See https://docs.python.org/3/library/struct.html
46
47    Args:
48        data (Any): _description_
49        bit_cast_from (str): see https://docs.python.org/3/library/struct.html#format-characters
50        bit_cast_to (str): see https://docs.python.org/3/library/struct.html#format-characters
51
52    Returns:
53        Any: _description_
54    """
55    return unpack(bit_cast_to, pack(bit_cast_from, data))

See https://docs.python.org/3/library/struct.html

Args: data (Any): _description_ bit_cast_from (str): see https://docs.python.org/3/library/struct.html#format-characters bit_cast_to (str): see https://docs.python.org/3/library/struct.html#format-characters

Returns: Any: _description_

def bit_cast__float_to_uint32(x: float) -> int:
58def bit_cast__float_to_uint32(x: float) -> int:
59    return unpack('I', pack('f', x))
def bit_cast__uint32_to_float(x: int) -> float:
62def bit_cast__uint32_to_float(x: int) -> float:
63    return unpack('f', pack('I', x))