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

1"""Routines for checking whether a value is a non-empty string w/o spaces.""" 

2 

3from typing import Any, Final 

4 

5from pycommons.strings.chars import WHITESPACE_OR_NEWLINE 

6 

7 

8def enforce_non_empty_str(value: Any) -> str: 

9 """ 

10 Enforce that a text is a non-empty string. 

11 

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 

16 

17 >>> enforce_non_empty_str("1") 

18 '1' 

19 >>> enforce_non_empty_str(" 1 1 ") 

20 ' 1 1 ' 

21 

22 >>> try: 

23 ... enforce_non_empty_str("") 

24 ... except ValueError as ve: 

25 ... print(ve) 

26 Non-empty str expected, but got empty string. 

27 

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' 

33 

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 

43 

44 

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. 

48 

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 

55 

56 >>> enforce_non_empty_str_without_ws("1") 

57 '1' 

58 

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 '. 

64 

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'. 

70 

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'. 

76 

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...'. 

83 

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. 

89 

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' 

95 

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