66 lines
1.7 KiB
Python
66 lines
1.7 KiB
Python
"""
|
|
Test string_handling/string_helpers
|
|
"""
|
|
|
|
import sys
|
|
from decimal import Decimal, getcontext
|
|
from textwrap import shorten
|
|
from corelibs.string_handling.string_helpers import shorten_string, format_number
|
|
|
|
|
|
def __sh_shorten_string():
|
|
string = "hello"
|
|
length = 3
|
|
placeholder = " [very long placeholder]"
|
|
try:
|
|
result = shorten_string(string, length, placeholder=placeholder)
|
|
print(f"IN: {string} -> {result}")
|
|
except ValueError as e:
|
|
print(f"Failed: {e}")
|
|
try:
|
|
result = shorten(string, width=length, placeholder=placeholder)
|
|
print(f"IN: {string} -> {result}")
|
|
except ValueError as e:
|
|
print(f"Failed: {e}")
|
|
|
|
|
|
def __sh_format_number():
|
|
print(f"Max int: {sys.maxsize}")
|
|
print(f"Max float: {sys.float_info.max}")
|
|
number = 1234.56
|
|
precision = 0
|
|
result = format_number(number, precision)
|
|
print(f"Format {number} ({precision}) -> {result}")
|
|
number = 1234.56
|
|
precision = 100
|
|
result = format_number(number, precision)
|
|
print(f"Format {number} ({precision}) -> {result}")
|
|
number = 123400000000000001.56
|
|
if number >= sys.maxsize:
|
|
print("INT Number too big")
|
|
if number >= sys.float_info.max:
|
|
print("FLOAT Number too big")
|
|
precision = 5
|
|
result = format_number(number, precision)
|
|
print(f"Format {number} ({precision}) -> {result}")
|
|
|
|
precision = 100
|
|
getcontext().prec = precision
|
|
number = Decimal(str(1234.56))
|
|
result = f"{number:,.100f}"
|
|
print(f"Format {number} ({precision}) -> {result}")
|
|
|
|
|
|
def main():
|
|
"""
|
|
Test: corelibs.string_handling.string_helpers
|
|
"""
|
|
__sh_shorten_string()
|
|
__sh_format_number()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|
|
# __END__
|