Skip to main content
Rollback to Revision 2
Source Link
Vogel612
  • 25.3k
  • 7
  • 58
  • 141

I need to handle configuration errors and ask the user for the right credentials if something is wrong. I cannot decide between three implementations. Which style is better in Python and why?

A:

while not backend.check_config():
    click.echo('Invalid Configuration parameters!')
    for param_name, question in backend.config.check_config_requires:
        value = prompt(question, default=getattr(backend.config, param_name))
        setattr(backend.config, param_name, value)

B:

while True:
    try:
        backend.validate_configcheck_config()
        break
    except ConfigurationError:
        click.echo('Invalid Configuration parameters!')
        for param_name, question in backend.config.check_config_requires:
            value = prompt=prompt(question, default=getattr(backend.config, param_name))
            setattr(backend.config, param_name, value)

C:

while True:
    try:
        backend.validate_configcheck_config()
    except ConfigurationError:
        click.echo('Invalid Configuration parameters!')
        for param_name, question in backend.config.check_config_requires:
            value = prompt=prompt(question, default=getattr(backend.config, param_name))
            setattr(backend.config, param_name, value)
    else:
        break

check_config() returns True when everything is ok, validate_config() raises exceptions on errors.

I need to handle configuration errors and ask the user for the right credentials if something is wrong. I cannot decide between three implementations. Which style is better in Python and why?

A:

while not backend.check_config():
    click.echo('Invalid Configuration parameters!')
    for param_name, question in backend.config.check_config_requires:
        value = prompt(question, default=getattr(backend.config, param_name))
        setattr(backend.config, param_name, value)

B:

while True:
    try:
        backend.validate_config()
        break
    except ConfigurationError:
        click.echo('Invalid Configuration parameters!')
        for param_name, question in backend.config.check_config_requires:
            value = prompt(question, default=getattr(backend.config, param_name))
            setattr(backend.config, param_name, value)

C:

while True:
    try:
        backend.validate_config()
    except ConfigurationError:
        click.echo('Invalid Configuration parameters!')
        for param_name, question in backend.config.check_config_requires:
            value = prompt(question, default=getattr(backend.config, param_name))
            setattr(backend.config, param_name, value)
    else:
        break

check_config() returns True when everything is ok, validate_config() raises exceptions on errors.

I need to handle configuration errors and ask the user for the right credentials if something is wrong. I cannot decide between three implementations. Which style is better in Python and why?

A:

while not backend.check_config():
    click.echo('Invalid Configuration parameters!')
    for param_name, question in backend.config.check_config_requires:
        value = prompt(question, default=getattr(backend.config, param_name))
        setattr(backend.config, param_name, value)

B:

while True:
    try:
        backend.check_config()
        break
    except ConfigurationError:
        click.echo('Invalid Configuration parameters!')
        for param_name, question in backend.config.check_config_requires:
            value =prompt(question, default=getattr(backend.config, param_name))
            setattr(backend.config, param_name, value)

C:

while True:
    try:
        backend.check_config()
    except ConfigurationError:
        click.echo('Invalid Configuration parameters!')
        for param_name, question in backend.config.check_config_requires:
            value =prompt(question, default=getattr(backend.config, param_name))
            setattr(backend.config, param_name, value)
    else:
        break
changed method names to clarify different implementation
Source Link
kissgyorgy
  • 245
  • 2
  • 10

I need to handle configuration errors and ask the user for the right credentials if something is wrong. I cannot decide between three implementations. Which style is better in Python and why?

A:

while not backend.check_config():
    click.echo('Invalid Configuration parameters!')
    for param_name, question in backend.config.check_config_requires:
        value = prompt(question, default=getattr(backend.config, param_name))
        setattr(backend.config, param_name, value)

B:

while True:
    try:
        backend.check_configvalidate_config()
        break
    except ConfigurationError:
        click.echo('Invalid Configuration parameters!')
        for param_name, question in backend.config.check_config_requires:
            value =prompt= prompt(question, default=getattr(backend.config, param_name))
            setattr(backend.config, param_name, value)

C:

while True:
    try:
        backend.check_configvalidate_config()
    except ConfigurationError:
        click.echo('Invalid Configuration parameters!')
        for param_name, question in backend.config.check_config_requires:
            value =prompt= prompt(question, default=getattr(backend.config, param_name))
            setattr(backend.config, param_name, value)
    else:
        break

check_config() returns True when everything is ok, validate_config() raises exceptions on errors.

I need to handle configuration errors and ask the user for the right credentials if something is wrong. I cannot decide between three implementations. Which style is better in Python and why?

A:

while not backend.check_config():
    click.echo('Invalid Configuration parameters!')
    for param_name, question in backend.config.check_config_requires:
        value = prompt(question, default=getattr(backend.config, param_name))
        setattr(backend.config, param_name, value)

B:

while True:
    try:
        backend.check_config()
        break
    except ConfigurationError:
        click.echo('Invalid Configuration parameters!')
        for param_name, question in backend.config.check_config_requires:
            value =prompt(question, default=getattr(backend.config, param_name))
            setattr(backend.config, param_name, value)

C:

while True:
    try:
        backend.check_config()
    except ConfigurationError:
        click.echo('Invalid Configuration parameters!')
        for param_name, question in backend.config.check_config_requires:
            value =prompt(question, default=getattr(backend.config, param_name))
            setattr(backend.config, param_name, value)
    else:
        break

I need to handle configuration errors and ask the user for the right credentials if something is wrong. I cannot decide between three implementations. Which style is better in Python and why?

A:

while not backend.check_config():
    click.echo('Invalid Configuration parameters!')
    for param_name, question in backend.config.check_config_requires:
        value = prompt(question, default=getattr(backend.config, param_name))
        setattr(backend.config, param_name, value)

B:

while True:
    try:
        backend.validate_config()
        break
    except ConfigurationError:
        click.echo('Invalid Configuration parameters!')
        for param_name, question in backend.config.check_config_requires:
            value = prompt(question, default=getattr(backend.config, param_name))
            setattr(backend.config, param_name, value)

C:

while True:
    try:
        backend.validate_config()
    except ConfigurationError:
        click.echo('Invalid Configuration parameters!')
        for param_name, question in backend.config.check_config_requires:
            value = prompt(question, default=getattr(backend.config, param_name))
            setattr(backend.config, param_name, value)
    else:
        break

check_config() returns True when everything is ok, validate_config() raises exceptions on errors.

edited tags; edited title
Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Which style is better on configuration error and why? Requesting credentials from users

Source Link
kissgyorgy
  • 245
  • 2
  • 10
Loading