Coverage for pycommons / strings / enforce.py: 100%
15 statements
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-11 03:04 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-11 03:04 +0000
1"""Routines for checking whether a value is a non-empty string w/o spaces."""
3from typing import Any, Final
5from pycommons.strings.chars import WHITESPACE_OR_NEWLINE
8def enforce_non_empty_str(value: Any) -> str:
9 """
10 Enforce that a text is a non-empty string.
12 :param value: the value to be checked whether it is a non-empty string
13 :returns: the value, but as type `str`
14 :raises TypeError: if `value` is not a `str`
15 :raises ValueError: if `value` is empty
17 >>> enforce_non_empty_str("1")
18 '1'
19 >>> enforce_non_empty_str(" 1 1 ")
20 ' 1 1 '
22 >>> try:
23 ... enforce_non_empty_str("")
24 ... except ValueError as ve:
25 ... print(ve)
26 Non-empty str expected, but got empty string.
28 >>> try:
29 ... enforce_non_empty_str(1)
30 ... except TypeError as te:
31 ... print(te)
32 descriptor '__len__' requires a 'str' object but received a 'int'
34 >>> try:
35 ... enforce_non_empty_str(None)
36 ... except TypeError as te:
37 ... print(te)
38 descriptor '__len__' requires a 'str' object but received a 'NoneType'
39 """
40 if str.__len__(value) <= 0:
41 raise ValueError("Non-empty str expected, but got empty string.")
42 return value
45def enforce_non_empty_str_without_ws(value: Any) -> str:
46 r"""
47 Enforce that a text is a non-empty string without white space.
49 :param value: the value to be checked whether it is a non-empty string
50 without any white space
51 :returns: the value, but as type `str`
52 :raises TypeError: if `value` is not a `str`
53 :raises ValueError: if `value` is empty or contains any white space
54 characters
56 >>> enforce_non_empty_str_without_ws("1")
57 '1'
59 >>> try:
60 ... enforce_non_empty_str_without_ws(" 1 1 ")
61 ... except ValueError as ve:
62 ... print(ve)
63 No white space allowed in string, but got ' 1 1 '.
65 >>> try:
66 ... enforce_non_empty_str_without_ws("a\tb")
67 ... except ValueError as ve:
68 ... print(ve)
69 No white space allowed in string, but got 'a\tb'.
71 >>> try:
72 ... enforce_non_empty_str_without_ws("012345678901234567890 12345678")
73 ... except ValueError as ve:
74 ... print(ve)
75 No white space allowed in string, but got '012345678901234567890 12345678'.
77 >>> try:
78 ... enforce_non_empty_str_without_ws(
79 ... "012345678901234567890 1234567801234567890123456789012345678")
80 ... except ValueError as ve:
81 ... print(str(ve)[10:])
82 pace allowed in string, but got '012345678901234567890 12345678...'.
84 >>> try:
85 ... enforce_non_empty_str_without_ws("")
86 ... except ValueError as ve:
87 ... print(ve)
88 Non-empty str expected, but got empty string.
90 >>> try:
91 ... enforce_non_empty_str_without_ws(1)
92 ... except TypeError as te:
93 ... print(te)
94 descriptor '__len__' requires a 'str' object but received a 'int'
96 >>> try:
97 ... enforce_non_empty_str_without_ws(None)
98 ... except TypeError as te:
99 ... print(te)
100 descriptor '__len__' requires a 'str' object but received a 'NoneType'
101 """
102 strlen: Final[int] = str.__len__(value)
103 if strlen <= 0:
104 raise ValueError("Non-empty str expected, but got empty string.")
105 if any(map(value.__contains__, WHITESPACE_OR_NEWLINE)):
106 if strlen > 32: # take care of strings that are too long
107 value = str.__getitem__(value, slice(0, 30, 1)) + "..."
108 raise ValueError(
109 f"No white space allowed in string, but got {value!r}.")
110 return value