def test_list_of_int(self): input_str = '1,2,3\n' with redirect_stdin(StringIO(input_str)): get_int_list = GetInput(convertor=IntConvertor()) result = get_list( prompt= 'List of integers between 3 and 5 numbers long, separated by ","', elem_get_input=get_int_list) assert (result == [1, 2, 3])
def test_list_of_three_ints(self): input_str = '1,2\n-1,4,6\n2,4,11\n2, 4, 10' not_1_3_5_validator = NoneOfValidator([1, 3, 5]) zero_to_ten_validator = RangeValidator(0, 10) complex_in_input = GetInput( convertor=IntConvertor(), validators=[zero_to_ten_validator, not_1_3_5_validator]) with redirect_stdin(StringIO(input_str)): result = get_list(elem_get_input=complex_in_input) assert (result == [2, 4, 10])
def test_get_input_list_3(self): input_str = u""" foo, bar -1,2,3 """ lc = ListConvertor(delimiter=None, elem_get_input=GetInput(convertor=IntConvertor())) with redirect_stdin(StringIO(input_str)): result = get_input(cleaners=StripCleaner(), convertor=lc) print(result) assert (result == [-1, 2, 3])
def test_list_of_lists(self): input_str = '0:0:0, 112:67:255, 255: 255: 128' sublist_elem_get_input = GetInput(convertor=IntConvertor(), validators=RangeValidator(0, 255)) sublist_get_input = GetInput(convertor=ListConvertor( elem_get_input=sublist_elem_get_input, delimiter=":"), validators=LengthValidator(3, 3)) list_get_input = GetInput(convertor=ListConvertor( elem_get_input=sublist_get_input)) with redirect_stdin(StringIO(input_str)): result = list_get_input.get_input() assert (result == [[0, 0, 0], [112, 67, 255], [255, 255, 128]])
def test_list_of_three_ints(self): input_str = '1,2\n1,4,6,7,9' with redirect_stdin(StringIO(input_str)): get_int_list = GetInput(convertor=IntConvertor()) len_3_validator = RangeValidator(min_val=3, max_val=5) result = get_list( prompt='List of 3-5 integers (separated by ",")', elem_get_input=get_int_list, validators=len_3_validator, value_error_str='list of values', ) assert (result == [1, 4, 6, 7, 9])
Len Wanger, 2017 """ from cooked_input import get_input, silent_error, log_error, get_int from cooked_input.convertors import IntConvertor from cooked_input.validators import RangeValidator, EqualToValidator, NoneOfValidator, AnyOfValidator def my_print_error(fmt_str, value, error_content): print('<<< ' + fmt_str.format(value=value, error_content=error_content) + ' >>>') if __name__ == '__main__': int_convertor = IntConvertor() pos_int_validator = RangeValidator(min_val=1, max_val=None) zero_to_ten_validator = RangeValidator(min_val=0, max_val=10) exactly_0_validator = EqualToValidator(value=0) exactly_5_validator = EqualToValidator(value=5) not_0_validator = NoneOfValidator(validators=[exactly_0_validator]) not_5_validator = NoneOfValidator(validators=[exactly_5_validator]) in_0_or_5_validator = AnyOfValidator( validators=[exactly_0_validator, exactly_5_validator]) not_0_or_5_validator = NoneOfValidator( validators=[exactly_0_validator, exactly_5_validator]) convertor_fmt = '# {value} cannot be converted to {error_content} #' validator_fmt = '@ {value} {error_content} @' # get any integer - simplest way print(get_int())
from prompt_toolkit.key_binding.defaults import load_key_bindings_for_prompt from prompt_toolkit.keys import Keys from cooked_input import get_input, GetInputInterrupt from cooked_input.convertors import IntConvertor from cooked_input.validators import RangeValidator, EqualToValidator, NoneOfValidator, AnyOfValidator def my_print_error(fmt_str, value, error_content): print('<<< ' + fmt_str.format(value=value, error_content=error_content) + ' >>>') if __name__ == '__main__': int_convertor = IntConvertor() pos_int_validator = RangeValidator(min_val=1, max_val=None) zero_to_ten_validator = RangeValidator(min_val=0, max_val=10) exactly_0_validator = EqualToValidator(value=0) exactly_5_validator = EqualToValidator(value=5) not_0_validator = NoneOfValidator(validators=[exactly_0_validator]) not_5_validator = NoneOfValidator(validators=[exactly_5_validator]) in_0_or_5_validator = AnyOfValidator( validators=[exactly_0_validator, exactly_5_validator]) not_0_or_5_validator = NoneOfValidator( validators=[exactly_0_validator, exactly_5_validator]) convertor_fmt = '# {value} cannot be converted to {error_content} #' validator_fmt = '@ {value} {error_content} @' registry = load_key_bindings_for_prompt()