Beispiel #1
0
    def test_list_of_choices(self):
        input_str = 'r g b\nr g bl brown'

        colors = ['red', 'green', 'blue', 'brown']
        color_cleaner = ChoiceCleaner(colors)
        color_validator = ChoiceValidator(colors)
        color_input = GetInput(cleaners=color_cleaner,
                               validators=color_validator)

        with redirect_stdin(StringIO(input_str)):
            result = get_list(elem_get_input=color_input, delimiter=' ')
            assert (result == ['red', 'green', 'blue', 'brown'])
Beispiel #2
0
    def test_choices(self):
        input_str_blank = """
 
            """

        input_str = """licorice
            booger
            lemon 
            """

        colors = ['red', 'green', 'blue']
        good_flavors = ['cherry', 'lime', 'lemon', 'orange']
        bad_flavors = 'licorice'
        choices_validator = ChoiceValidator(choices=colors)
        good_flavor_validator = ChoiceValidator(choices=good_flavors)
        bad_flavor_validator = ChoiceValidator(choices=bad_flavors)
        not_in_choices_validator = NoneOfValidator(
            validators=[bad_flavor_validator])
        strip_cleaner = StripCleaner()
        lower_cleaner = CapitalizationCleaner()
        strip_and_lower_cleaners = [strip_cleaner, lower_cleaner]

        with redirect_stdin(StringIO(input_str_blank)):
            result = get_input(cleaners=strip_and_lower_cleaners,
                               validators=not_in_choices_validator,
                               default='cherry')
            assert (result == 'cherry')

        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=strip_and_lower_cleaners,
                               validators=not_in_choices_validator,
                               default='cherry')
            assert (result == 'booger')

        with redirect_stdin(StringIO(input_str)):
            validators = [good_flavor_validator, not_in_choices_validator]
            result = get_input(cleaners=strip_and_lower_cleaners,
                               validators=validators,
                               default='cherry')
            assert (result == 'lemon')
Beispiel #3
0
    roles_list = [
        'admin', 'arthur', 'praline', 'mandy', 'animator', 'anchovy', 'stig',
        'timmy', 'travel-guide'
    ]

    strip_cleaner = StripCleaner()
    default_cleaners = [StripCleaner(), CapitalizationCleaner(style='lower')]
    name_cleaners = [StripCleaner(), CapitalizationCleaner(style='all_words')]
    strong_password_validator = PasswordValidator(disallowed='[]',
                                                  min_len=5,
                                                  max_len=15,
                                                  min_lower=2,
                                                  min_puncts=2)
    email_validator = SimpleValidator(
        isemail, name='email')  # validator from validus function
    role_validator = ListValidator(elem_validators=ChoiceValidator(roles_list))
    role_prompt = 'Roles ({}, separated by commas)'.format(sorted(roles_list))
    password_confirm_fmt_str = 'password does not match'

    # Give a hint to new users...
    print(
        '\n\nThis is an example of using cooked_input to simulate getting login information for a user.'
    )
    print(
        '\nFor test purposes, you can log in as user: gc with password: IWasBrian2!\n'
    )
    print('\nFor more examples look at the source code\n\n')

    # Simulate logging the user in:
    try:
        user_name = get_input(prompt='Username',
Beispiel #4
0
cooked input examples of getting an string values

Len Wanger, 2017
"""

from cooked_input import get_input, get_string
from cooked_input.validators import LengthValidator, ChoiceValidator, NoneOfValidator
from cooked_input.cleaners import StripCleaner, CapitalizationCleaner
from cooked_input.convertors import YesNoConvertor

if __name__ == '__main__':
    colors = ['red', 'green', 'blue']
    good_flavors = ['cherry', 'lime', 'lemon', 'orange']
    bad_flavors = 'licorice'

    good_flavor_validator = ChoiceValidator(choices=good_flavors)
    not_in_choices_validator = NoneOfValidator(
        validators=[ChoiceValidator(choices=bad_flavors)])
    strip_and_lower_cleaners = [
        StripCleaner(), CapitalizationCleaner(style='lower')
    ]

    # simplest way
    print(get_string(prompt='Enter some text'))

    # strip and capitalization cleaners
    print(
        get_input(
            prompt=
            'Enter any string (will be stripped of trailing spaces and converted to upper)',
            cleaners=[
Beispiel #5
0
"""

import re
from cooked_input import get_input
from cooked_input.cleaners import ChoiceCleaner, ReplaceCleaner, RegexCleaner
from cooked_input.validators import ChoiceValidator

if __name__ == '__main__':
    # ChoiceCleaner examples:
    color_choices = ['Black', 'Brown', 'BLUE', 'red', 'green']
    color_choice_cleaner = ChoiceCleaner(color_choices)

    color_choice_cleaner_case_insensitive = ChoiceCleaner(color_choices,
                                                          case_sensitive=False)

    color_choice_validator = ChoiceValidator(color_choices)
    float_choice_cleaner = ChoiceCleaner(choices=[1.0, 10.0, 1.11, 3.141569])

    print(
        get_input(prompt='Enter a color (choices: {})'.format(
            ', '.join(color_choices)),
                  cleaners=color_choice_cleaner,
                  validators=color_choice_validator))
    print(
        get_input(
            prompt='Enter a color (choices: {} - case insensitive)'.format(
                ', '.join(color_choices)),
            cleaners=[color_choice_cleaner_case_insensitive],
            validators=color_choice_validator))
    print(
        get_input(