Function cleans up url paths (without domain) by ensuring they start with a single slash and removing double slashes.
102 lines
2.8 KiB
Python
102 lines
2.8 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, prepare_url_slash
|
|
from corelibs.string_handling.text_colors import Colors
|
|
|
|
|
|
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"{Colors.red}Failed: {Colors.bold}{e}{Colors.end}")
|
|
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 __sh_colors():
|
|
for color in [
|
|
"black",
|
|
"red",
|
|
"green",
|
|
"yellow",
|
|
"blue",
|
|
"magenta",
|
|
"cyan",
|
|
"white",
|
|
]:
|
|
for change in ['', '_bold', '_bright']:
|
|
_color = f"{color}{change}"
|
|
print(f"Color: {getattr(Colors, _color)}{_color}{Colors.end}")
|
|
|
|
print(f"Underline: {Colors.underline}UNDERLINE{Colors.reset}")
|
|
print(f"Bold: {Colors.bold}BOLD{Colors.reset}")
|
|
print(f"Underline/Yellow: {Colors.underline}{Colors.yellow}UNDERLINE YELLOW{Colors.reset}")
|
|
print(f"Underline/Yellow/Bold: {Colors.underline}{Colors.bold}{Colors.yellow}UNDERLINE YELLOW BOLD{Colors.reset}")
|
|
|
|
|
|
def __prepare_url_slash():
|
|
urls = [
|
|
"api/v1/resource",
|
|
"/api/v1/resource",
|
|
"///api//v1//resource//",
|
|
"api//v1/resource/",
|
|
]
|
|
for url in urls:
|
|
prepared = prepare_url_slash(url)
|
|
print(f"IN: {url} -> OUT: {prepared}")
|
|
|
|
|
|
def main():
|
|
"""
|
|
Test: corelibs.string_handling.string_helpers
|
|
"""
|
|
__sh_shorten_string()
|
|
__sh_format_number()
|
|
__sh_colors()
|
|
__prepare_url_slash()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|
|
# __END__
|