Coverage for src/thunderlab/consoleinput.py: 98%

57 statements  

« prev     ^ index     » next       coverage.py v7.6.8, created at 2024-11-29 17:59 +0000

1"""User input from console. 

2 

3- `read()`: read a single value from console. 

4- `select()`: select a menue option. 

5- `save_inputs()`: write all inputs from `read()` and `select()` into a file. 

6- `clear_inputs()`: clear list all inputs recorded from `read()` and `select()`. 

7- `recorded_inputs`: list of strings with all inputs received by `read()` and `select()`. 

8""" 

9 

10try: 

11 input_ = raw_input 

12except NameError: 

13 input_ = input 

14 

15 

16recorded_inputs = [] 

17"""List of strings with all inputs received by `read()` and `select()`.""" 

18 

19 

20def read(prompt, default=None, dtype=str, min=None, max=None): 

21 """Read a single input value from the console. 

22  

23 Parameters 

24 ---------- 

25 prompt: string 

26 prompt to be displayed. 

27 default: string 

28 default value if only 'return' is pressed. 

29 dtype: type 

30 data type to be returned (str, int, float, ...) 

31 min: dtype 

32 input needs to be larger than min. 

33 min: dtype 

34 input needs to be smaller than max. 

35 

36 Returns 

37 ------- 

38 x: dtype 

39 the value of the input. 

40 """ 

41 

42 if default is not None: 

43 prompt += ' [%s]: ' % default 

44 while True: 

45 s = input_(prompt) 

46 if len(s) == 0 and default is not None: 

47 s = default 

48 if len(s) > 0: 

49 try: 

50 x = dtype(s) 

51 except ValueError: 

52 x = None 

53 if x is not None: 

54 if min is not None and x < min: 

55 continue 

56 if max is not None and x > max: 

57 continue 

58 recorded_inputs.append(s) 

59 return x 

60 

61 

62def select(prompt, default, options, descriptions): 

63 """Print a menue from which the user can select an entry. 

64  

65 Parameters 

66 ---------- 

67 prompt: string 

68 A title for the menue. 

69 default: string 

70 The default selection. 

71 options: list of single character strings 

72 The characters by which the menue options are selected. 

73 descriptions: list of strings 

74 A description for each menue option. 

75 

76 Returns 

77 ------- 

78 s: string 

79 the selected value (one of the characters in options). 

80 """ 

81 print(prompt) 

82 for o, d in zip(options, descriptions): 

83 print(' [%s] %s' % (o, d)) 

84 sprompt = ' Select' 

85 if default is not None: 

86 sprompt += ' [%s]: ' % default 

87 while True: 

88 s = input_(sprompt).lower() 

89 if len(s) == 0: 

90 s = default 

91 if s in options: 

92 recorded_inputs.append(s) 

93 return s 

94 

95 

96def save_inputs(file): 

97 """Write all inputs from `read()` and `select()` into a file. 

98 

99 This file can then be used to pipe these inputs to the program 

100 instead of typing them in manually. 

101  

102 Parameters 

103 ---------- 

104 file: string 

105 Name of the file where to save the inputs. 

106 """ 

107 with open(file, 'w') as df: 

108 for line in recorded_inputs: 

109 df.write(line) 

110 df.write('\n') 

111 

112 

113def clear_inputs(): 

114 """Clear list all inputs recorded from `read()` and `select()`. 

115 """ 

116 recorded_inputs = [] 

117 

118 

119def main(): 

120 x = read('Give me a number between 1 and 10', '5', int, 1, 10) 

121 print(x) 

122 print('') 

123 

124 y = select('Your options are', 'a', ['a', 'b', 'o'], ['apples', 'bananas', 'oranges']) 

125 print(y) 

126 print('') 

127 

128 print('your successfull inputs have been:') 

129 print(recorded_inputs) 

130 

131 # save_inputs('test.txt') 

132 ## you then can call the script like this: 

133 ## python -m src.thunderlab.consoleinput < test.txt 

134 

135 

136if __name__ == '__main__': 

137 main()