import struct
from typing import Callable
from .value import Value
__all__ = (
'dump',
'optional',
'assignment',
'is_value_equal',
)
[docs]
def dump(separator: str = ', ', **kwargs) -> str:
"""
Convert keyword arguments into a string.
"""
return separator.join(f"{key}={value!r}" for key, value in kwargs.items())
[docs]
def optional(function: Callable) -> Callable:
"""
Mark a function to be emptied when generating a stub.
"""
function.__optional__ = True # type: ignore
return function
[docs]
def assignment(function: Callable) -> Callable:
"""
Mark a function to be emptied when generating a stub.
"""
function.__assignment__ = True # type: ignore
return function
[docs]
def is_value_equal(value1: Value, value2: Value) -> bool:
"""
Check whether two values are equal.
Use the `struct` module to compare `float` values using 32-bit representation.
"""
if type(value1) is not type(value2):
return False
if type(value1) is float and type(value2) is float:
data1 = struct.pack('<f', value1)
data2 = struct.pack('<f', value2)
return data1 == data2
if value1 == value2:
return True
return value1 == value2