import six
TRUE_STRINGS =('1','t','true','on','y','yes')
FALSE_STRINGS =('0','f','false','off','n','no')defbool_from_string(subject, strict=False, default=False):"""Interpret a string as a boolean.
A case-insensitive match is performed such that strings matching 't',
'true', 'on', 'y', 'yes', or '1' are considered True and, when
`strict=False`, anything else returns the value specified by 'default'.
Useful for JSON-decoded stuff and config file parsing.
If `strict=True`, unrecognized values, including None, will raise a
ValueError which is useful when parsing values passed in from an API call.
Strings yielding False are 'f', 'false', 'off', 'n', 'no', or '0'.
"""ifnotisinstance(subject, six.string_types):
subject = six.text_type(subject)
lowered = subject.strip().lower()if lowered in TRUE_STRINGS:returnTrueelif lowered in FALSE_STRINGS:returnFalseelif strict:
acceptable =', '.join("'%s'"% s for s insorted(TRUE_STRINGS + FALSE_STRINGS))
msg = _("Unrecognized value '%(val)s', acceptable values are:"" %(acceptable)s")%{'val': subject,'acceptable': acceptable}raise ValueError(msg)else:return default
print(bool_from_string('false'))