Skip to content

Implement strtobool? #280

@jvanderaa

Description

@jvanderaa

Environment

  • netutils version:

Proposed Functionality

Would look to reimplement the distutils strtobool function.

Use Case

The taking a string and getting a boolean from it is helpful. Could be used in YAML manipulation (yes/no), or for reading from environment settings.

The distutils library that has the implementation is being retired and has deprecation warnings in it as of Python3.10. It would be nice to have a place to be able to get the same functionality from a trusted utility source. Understand if this is something that should not be here. Their advice is for each individual to implement their own.

Seeing this as an example:

_MAP = {
    "y": True,
    "yes": True,
    "t": True,
    "true": True,
    "on": True,
    "1": True,
    "n": False,
    "no": False,
    "f": False,
    "false": False,
    "off": False,
    "0": False,
}


def strtobool(value):
    try:
        return _MAP[str(value).lower()]
    except KeyError:
        raise ValueError('"{}" is not a valid bool value'.format(value))

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions